我正在尝试将 knuthBendix 应用于大量重写规则。因此,我尝试让它在不同的集合上并行工作。
作为示例,我尝试运行:
import Control.Parallel
import Control.Parallel.Strategies
import Math.Algebra.Group.StringRewriting
knuthBendixOptimized rs = as' `par` bs' `pseq` as' ++ bs' where
(as, bs) = splitAt 3000 rs
as' = knuthBendix as
bs' = knuthBendix bs
我使用 ghc -threaded
编译并通过 +RTS -N
执行。如果我并行运行其他算法,它会起作用。但对于 knuthBendix,它没有。
有人知道解决方案吗?
谢谢, 弗朗茨
最佳答案
我认为问题在于您将 称为 `pseq`
。这会将 as'
评估为 WHNF,这意味着它仅确定列表是否为空。但是您希望对列表进行全面评估:
import Control.Parallel.Strategies
forceList :: [a] -> [a]
forceList = withStrategy (evalList rseq)
-- or use rdeepseq to force the evaluation of everything
knuthBendixOptimized rs = forceList as'
`par` forceList bs'
`pseq` as' ++ bs'
where
(as, bs) = splitAt 3000 rs
as' = knuthBendix as
bs' = knuthBendix bs
请注意,Haskell 对此的常用术语是parallelism。 concurrency 用于在 IO
中与线程及其通信的显式工作。见 the GHC manual .
关于multithreading - knuthBendix 算法不能被 Control.Parallel 并行化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17657463/