我正在尝试将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,它没有。
有人知道解决方案吗?
谢谢, 弗朗兹
答案 0 :(得分:5)
我认为问题在于您正在呼叫as' `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 。并且并发用于IO
中带有线程及其通信的显式工作。请参阅the GHC manual。
答案 1 :(得分:4)
我认为knuthBendix
不像那样天真地并行化。文档说:
knuthBendix :: Ord a => [([a], [a])] -> [([a], [a])]
Implementation of the Knuth-Bendix algorithm. Given a list of relations, return a
confluent rewrite system. The algorithm is not guaranteed to terminate.
在我看来,分裂你的方式会改变语义。您可能需要一些更具侵略性的并行性。