Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- open System.Threading
- open System
- // Initialize an array by a parallel init using all available processors // Note, this primitive doesn't support cancellation.
- let parallelArrayInit n f =
- let currentLine = ref -1
- let res = Array.zeroCreate n
- let rec loop () =
- let y = Interlocked.Increment(currentLine)
- if y < n then res.[y] <- f y; loop()
- // Start just the right number of tasks, one for each physical CPU
- Async.Parallel [for i in 1 .. Environment.ProcessorCount -> async {do loop()}]
- |> Async.Ignore
- |> Async.RunSynchronously
- res
- // example
- let rec fib x = if x < 2 then 1 else fib (x - 1) + fib (x - 2)
- parallelArrayInit 25 (fun x -> fib x);;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement