私は、haskell演習でAndre Lohの決定論的並列プログラミングの演習を行っていました。戦略を使用してN-Queensのシーケンシャルコードをパラレルに変換しようとしていましたが、パラレルコードがシーケンシャルコードよりも遅く実行され、スタックスペースが不足しているというエラーが発生しました。Haskellで並列ストラテジーを使用した場合のスローダウン
これはライン(\l -> concat (map (addQueen n) l using parListChunk (n div numCapabilities) rdeepseq))
私は元のコードから変更何
import Control.Monad
import System.Environment
import GHC.Conc
import Control.Parallel.Strategies
import Data.List
import Data.Function
type PartialSolution = [Int] -- per column, list the row the queen is in
type Solution = PartialSolution
type BoardSize = Int
chunk :: Int -> [a] -> [[a]]
chunk n [] = []
chunk n xs = case splitAt n xs of
(ys, zs) -> ys : chunk n zs
-- Generate all solutions for a given board size.
queens :: BoardSize -> [Solution]
--queens n = iterate (concatMap (addQueen n)) [[]] !! n
queens n = iterate (\l -> concat (map (addQueen n) l `using` parListChunk (n `div` numCapabilities) rdeepseq)) [[]] !! n
-- Given the size of the problem and a partial solution for the
-- first few columns, find all possible assignments for the next
-- column and extend the partial solution.
addQueen :: BoardSize -> PartialSolution -> [PartialSolution]
addQueen n s = [ x : s | x <- [1..n], safe x s 1 ]
-- Given a row number, a partial solution and an offset, check
-- that a queen placed at that row threatens no queen in the
-- partial solution.
safe :: Int -> PartialSolution -> Int -> Bool
safe x [] n = True
safe x (c:y) n = x /= c && x /= c + n && x /= c - n && safe x y (n + 1)
main = do
[n] <- getArgs
print $ length $ queens (read n)
、並列N - クイーンのためのコードです。私はSimon Marlowのソリューションを見てきましたが、私のコードでは減速とエラーの理由を知りたかったのです。
ありがとうございます。
どのようにコンパイルして実行しましたか? – is7s
'-O2'でコンパイルし、' -threaded -Nn'で実行していますか?(ここで 'n'はCPUのカウントです) –
' -threaded'は実行時オプションではなくコンパイル時のオプションです。また、いつBaily'sに戻ってきますか、Don?タップで恋しい。 –