Created
February 10, 2012 16:29
-
-
Save dungpa/1790646 to your computer and use it in GitHub Desktop.
Count all solutions sequentially and in parallel
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
// Place the first queen beforehand for parallel execution. | |
let countParallel1 size = | |
let boards = Array.init size (fun _ -> new Board(size)) | |
for i in 0..size-1 do | |
boards.[i].placeQueen(i) | |
boards |> Array.Parallel.map (fun b -> b.countSolutions()) | |
|> Array.sum | |
// Place two first queens beforehand for parallel execution. | |
let countParallel2 size = | |
let n = (size-1)*(size-2) | |
let boards = Array.init n (fun _ -> new Board(size)) | |
let mutable count = 0 | |
for i in 0..size-1 do | |
for j in 0..size-1 do | |
if abs(i-j) > 1 then // safe to place two queens together | |
boards.[count].placeQueen(i) | |
boards.[count].placeQueen(j) | |
count <- count+1 | |
boards |> Array.Parallel.map (fun b -> b.countSolutions()) | |
|> Array.sum |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment