gpt4 book ai didi

haskell - parTraversable 不会产生任何 Spark

转载 作者:行者123 更新时间:2023-12-02 10:29:57 25 4
gpt4 key购买 nike

当我使用 -s 运行该程序时:

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)

Spark 产生:

SPARKS: 1000 (993 converted, 0 overflowed, 0 dud, 6 GC'd, 1 fizzled)

如果我将 parList 更改为 parTraversable

x  = product (xs `using` parTraversable rseq)

没有产生 Spark :

SPARKS: 0 (0 converted, 0 overflowed, 0 dud, 0 GC'd, 0 fizzled)

如果我将 rseq 更改为 rdeepseq:

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

没有产生 Spark

SPARKS: 0 (0 converted, 0 overflowed, 0 dud, 0 GC'd, 0 fizzled)

我使用的是parallel-3.2.1.1,在源代码中,parList是使用parTraversable定义的!

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

我错过了什么?

最佳答案

我可以重现您的行为(ghc-8.2.1、parallel-3.2.1.1)。

稍后,Strategies.hs 是一个 RULES 编译指示,用于特殊情况 parList rseq。我猜这是一个错误,它与 parTraversable 具有不同的行为(我对内部结构了解不够,无法确定错误所在)。我建议在并行问题跟踪器中提交票证: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
#-}

关于haskell - parTraversable 不会产生任何 Spark ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46613397/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com