170 likes | 276 Views
Parallel Programming: F#. Robert Vitolo CS474. F#: A Brief Overview. Branched off of ML ( metalanguage ) Developed at Microsoft, available as part of the Visual Studio 2010 software package, integrated into the .NET environment just as C#, Visual C Allows for three programming paradigms:
E N D
Parallel Programming:F# Robert Vitolo CS474
F#: A Brief Overview Branched off of ML (metalanguage) Developed at Microsoft, available as part of the Visual Studio 2010 software package, integrated into the .NET environment just as C#, Visual C Allows for three programming paradigms: Imperative, Object-Oriented, or Functional
A Focus On: Functional Programming Modeled on mathematical functions, rooted in lamda calculus Relatively simple syntax and semantic structure compared to imperative languages that give it high readability. Uses ‘functions’ that are conceptually similar to math functions, making it a highly appropriate paradigm for representing equations or performing math operations
F# Interactive Allows you to quickly evaluate expressions or perform computation on-the-fly
F# Interactive Allows you to quickly evaluate expressions or perform computation on-the-fly
A Focus On: Functional Programming • Data structures make it a fitting language candidate for computation over large sets of data. • List (homogeneous data) • Tuple (related data) • Record (heterogeneous data) • Discriminated Unions
Functional Programming and How It Applies To Parallelism • Purely functional languages do not use mutable data or mutable states, and thus are inherently thread safe – the output value of a function depends entirely on its arguments. • List, for example, is a data structure that stores an immutable series of ordered elements as a linked list, all of the same type. • F# does allow mutable data if you explicitly define it with a ‘mutable’ keyword i.e. let mutable x = 5. • This frees up the programmer from having to manage complex thread synchronization / locks
Example: Sequential Squares • Problem: Return the square of a range of numbers. • Return the squares of 1 to n: [12, 22… n2] • In this example, from integers 1 to 10. • Two approaches: • C# approach • F# approach
C# Approach C# CODE OUTPUT
F# Approach 1 2 3
F# Parallelism Using the Task Parallel Library you can create parallel programs in F#. You have access to all of the same .NET functions in C#, including Parallel.For, Parallel LINQ (PLINQ) and Parallel CPU Asyncs In this example we’ll use PLINQ, which is the best one to use if you want to program in a purely functional manner.
Example: Sum of Squares • Problem: Find the sum for all squares in a range of values in parallel and return the execution time and the sum. • In this example, from integers 1 to 1500.
F# Parallel using PLINQ F# CODE AND OUTPUT
F# Parallel using PLINQ: A Closer Look • Think of the execution of this statement in this manner: • Take our list of numbers (1 to 1500) and map the squaring function sqr to each item, and return that set of values. • Take the set of values and run the function “sum” on that set, and return the answer to ParallelSumOfSquares.
Example: Parallel Finding Max Problem: Find the largest value in a list of values in parallel and return that value. Code:
Finding Max Result Result:
Is F# better than C#? • The burning question: Is F# “faster” or “better” than C# ? How about for parallel programming? • Answer: • In terms of Productivity? Yes, for many applications • Parallel Programming? Yes, for ease of setup • Execution Time? No