Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. open System.Threading
  2. open System
  3. // Initialize an array by a parallel init using all available processors // Note, this primitive doesn't support cancellation.
  4. let parallelArrayInit n f =
  5. let currentLine = ref -1
  6. let res = Array.zeroCreate n
  7. let rec loop () =
  8. let y = Interlocked.Increment(currentLine)
  9. if y < n then res.[y] <- f y; loop()
  10. // Start just the right number of tasks, one for each physical CPU
  11. Async.Parallel [for i in 1 .. Environment.ProcessorCount -> async {do loop()}]
  12. |> Async.Ignore
  13. |> Async.RunSynchronously
  14. res
  15.  
  16. // example
  17. let rec fib x = if x < 2 then 1 else fib (x - 1) + fib (x - 2)
  18. parallelArrayInit 25 (fun x -> fib x);;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement