1 / 17

Parallel Programming: F#

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:

xuefang-jun
Download Presentation

Parallel Programming: F#

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Parallel Programming:F# Robert Vitolo CS474

  2. 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

  3. 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

  4. F# Interactive Allows you to quickly evaluate expressions or perform computation on-the-fly

  5. F# Interactive Allows you to quickly evaluate expressions or perform computation on-the-fly

  6. 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

  7. 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

  8. 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

  9. C# Approach C# CODE OUTPUT

  10. F# Approach 1 2 3

  11. 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.

  12. 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.

  13. F# Parallel using PLINQ F# CODE AND OUTPUT

  14. 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.

  15. Example: Parallel Finding Max Problem: Find the largest value in a list of values in parallel and return that value. Code:

  16. Finding Max Result Result:

  17. 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

More Related