Created
June 5, 2012 14:22
-
-
Save dungpa/2875314 to your computer and use it in GitHub Desktop.
A sequence of prime under n: parallel version
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let pfilterRange predicate (i, j) = | |
let results = ResizeArray(j-i+1) | |
let monitor = new Object() | |
Parallel.For( | |
i, j, new ParallelOptions(), | |
(fun () -> ResizeArray(j-i+1)), | |
(fun k _ (localList: ResizeArray<_>) -> | |
if predicate k then localList.Add(k) | |
localList), | |
(fun local -> lock (monitor) (fun () -> results.AddRange(local))) | |
) |> ignore | |
results.ToArray() | |
let rec primesUnderParallel = function | |
| n when n<=2 -> [||] | |
| 3 -> [|2|] | |
| n when n<=100 -> primesUnder n | |
| n -> let ns = n |> float |> sqrt |> ceil |> int | |
let smallers = primesUnderParallel ns | |
Array.append smallers (pfilterRange (indivisible smallers) (ns, n-1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment