Understanding Parallel Programming with F# and C#: CPU-bound and Asynchronous Tasks, Slides of Human Resource Management

The importance of parallel programming in light of the limitations of cpu performance growth. It discusses cpu-bound parallelism in f# 3.0 and i/o-bound parallelism using tasks in .net, illustrated with examples in c#. The document also covers asynchronous programming in c# 4.5.

Typology: Slides

2012/2013

Uploaded on 07/25/2013

deveshwar
deveshwar 🇮🇳

3.9

(15)

108 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Asynchronous and parallel F# 3
&
Asynchronous and parallel C# 4.5
1
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Understanding Parallel Programming with F# and C#: CPU-bound and Asynchronous Tasks and more Slides Human Resource Management in PDF only on Docsity!

Asynchronous and parallel F# 3

Asynchronous and parallel C# 4.

1

Agenda

• Why is parallel programming important?

• CPU-bound parallelism in F# 3.

• I/O-bound parallelism in F# 3.

• Tasks in .NET 4.0, illustrated with C#

• Asynchronous programming in C# 4.

2

Why functional parallel

programming?

• What is the purpose of synchronization?

– To avoid conflicting updates of shared data

• Functional programming

– no updates to shared data

– Instead: message passing, agents, ...

• Some consensus this is the way forward

– Even in the press: Economist, 2 June 2011

http://www.economist.com/node/

– Hiperfit project, www.hiperfit.dk

– Actulus project, www.actulus.dk

4

Nvidia's chief scientist says...

5

Making it easy to program a machine that requires 10 billion threads to use at full capacity is also a challenge.

While a backward compatible path will be provided to allow existing MPI codes to run, MPI plus C++ or Fortran is not a productive programming environment for a machine of this scale.

We need to move toward higher-level programming models where the programmer describes the algorithm with all available parallelism and locality exposed , and tools automate much of the process of efficiently mapping and tuning the program to a particular target machine.

Bill Dally in HPC Wire, 15 April 2013

More CPU-bound

parallel programming in F#

7

• Computing 41 Fibonacci numbers:

let fibs = [ for i in 0..40 -> slowfib(i) ];; // Real: 00:00:06.908, CPU: 00:00:06.

• Doing it in parallel:

let fibs = let tasks = [ for i in 0..40 -> async { return slowfib(i) } ] Async.RunSynchronously (Async.Parallel tasks);; // Real: 00:00:03.665, CPU: 00:00:06.

Same as

"do yield"

Dissecting the example

8

async { return slowfib(i) }

let tasks = [ for i in 0..40 -> async { return slowfib(i) } ]

Async.Parallel tasks

Async.RunSynchronously (Async.Parallel tasks)

let tasks = [ for i in 0..40 -> async { return slowfib(i) } ] Async.RunSynchronously (Async.Parallel tasks);;

Async

Async list

Async<float []>

float []

An asynchronous task that will produce a float

List of asynchronous tasks that each will produce a float

An asynchronous task that will produce a float array

A float array

Finding prime factors

• Prime factors of a number

• Prime factors of 0..

• Same, in parallel

10

factors 973475;; val it : int list = [5; 5; 23; 1693]

Array.init 100000 factors;; Real: 00:00:03.036, CPU: 00:00:03.035, GC gen0: 1, gen1: 0 val it : int list [] = [|[]; []; [2]; [3]; [2; 2]; [5]; [2; 3]; [7]; ... |]

let factors100000 = Array.Parallel.init 100000 factors;; Real: 00:00:01.550, CPU: 00:00:03.048, GC gen0: 1, gen1: 0 val factors100000 : int list [] = [|[]; []; [2]; [3]; [2; 2]; [5]; [2; 3]; [7]; ... |]

Array.init : int -> (int -> 'a) -> 'a []