Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run code in parallel?

Tags:

f#

f#-3.0

How can i run these two independent loops simultaneously in parallel.

let a1=Seq.map2 (fun a b->(1.0-a)/(a-b)) a2  a3

let b1=Seq.map2 (fun a b->a*b) b2 b3
like image 606
HaagenDaz Avatar asked Dec 25 '22 14:12

HaagenDaz


1 Answers

You can use standard .NET tasks for this - there are no special F# functions or special syntax for spawning a computation in the background:

let a1Work = Task.Factory.StartNew(fun () ->
  Array.map2 (fun a b->(1.0-a)/(a-b)) a2  a3)
let b1 = Array.map2 (fun a b->a*b) b2 b3
let a1 = a1Work.Value

I also changed your Seq.map2 to Array.map2 - computations on sequences are lazy and so running them inside a task would not actually do anything. With arrays, the whole calculation is completed immediately.

like image 145
Tomas Petricek Avatar answered Jan 07 '23 11:01

Tomas Petricek