나는 haskell 연습에서 Andre Loh의 결정 론적 병렬 프로그래밍의 연습을하고있었습니다. 전략을 사용하여 N-Queens 순차 코드를 병렬로 변환하려고했지만 병렬 코드가 순차 코드보다 훨씬 느리게 실행되고 스택 공간이 부족하여 오류가 발생하는 것으로 나타났습니다. Haskell에서 병렬 전략을 사용할 때 속도 저하
이
는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)
라인 (\l -> concat (map (addQueen n) l using parListChunk (n div numCapabilities) rdeepseq))
는 I 원본 코드로 변경 무엇 병렬 N-퀸즈 코드이다. Simon Marlow의 솔루션을 보았지만 코드가 느려지고 오류가 발생하는 이유를 알고 싶었습니다.
미리 감사드립니다.
어떻게 컴파일하고 실행 했습니까? – is7s
'-O2'로 컴파일하고'-threaded -Nn' (여기서'n'은 cpu 카운트입니까?)로 실행합니까? –
'-threaded'는 런타임 옵션이 아니라 컴파일 타임 옵션입니다. 더구나, 언제 베일리에 다시 올거야, 돈? 너 꼭꼭가있어. –