2017-10-06 31 views
4

當我與-s運行此程序:parTraversable不產生任何火花

module Main where 

import Control.Parallel.Strategies 

main :: IO() 
main = do let xs = take 1000 $ product . take 1000 . repeat <$> [1..] 
       x = product (xs `using` parList rseq) 
      putStrLn (show x) 

火花被創建:

SPARKS:1000(993轉換,0溢出,0啞彈,6 GC'd ,1失敗了)

如果更改parListparTraversable

x = product (xs `using` parTraversable rseq) 

無火花被創建:

SPARKS:0(0轉換,0溢出,0啞彈,0 GC'd,0失敗了)

如果更改rseqrdeepseq

main = do let xs = (take 1000 $ product . take 1000 . repeat <$> [1..]) :: [Integer] 
       x = product (xs `using` parList rdeepseq) 
      putStrLn (show x) 

無火花產生

SPARKS:0(0轉換,0溢出,0啞彈,0 GC'd,0失敗了)

我使用平行3.2.1.1和在源代碼,parList使用parTraversable定義!

parList :: Strategy a -> Strategy [a] 
parList = parTraversable 

我在想什麼?

回答

5

我可以重現你的行爲(ghc-8.2.1,parallel-3.2.1.1)。

後來在Strategies.hs下是一個RULES編譯指示,特殊情況parList rseq。我想這是一個錯誤,它有不同的行爲parTraversable(我不知道有關內部,以確定錯誤在哪裏)。我建議在parallel問題跟蹤提交門票:https://github.com/haskell/parallel/issues

這裏是有問題的代碼,開始在該文件的505行:

-- Non-compositional version of 'parList', evaluating list elements 
-- to weak head normal form. 
-- Not to be exported; used for optimisation. 

-- | DEPRECATED: use @'parList' 'rseq'@ instead 
parListWHNF :: Strategy [a] 
parListWHNF xs = go xs `pseq` return xs 
    where -- go :: [a] -> [a] 
      go []  = [] 
      go (y:ys) = y `par` go ys 

-- The non-compositional 'parListWHNF' might be more efficient than its 
-- more compositional counterpart; use RULES to do the specialisation. 

{-# NOINLINE [1] parList #-} 
{-# NOINLINE [1] rseq #-} 
{-# RULES 
"parList/rseq" parList rseq = parListWHNF 
#-}