Multithreading parBuffer求值未给出预期的加速比
我有一个haskell函数,我想用精确的中间结果进行评估:Multithreading parBuffer求值未给出预期的加速比,multithreading,haskell,concurrency,Multithreading,Haskell,Concurrency,我有一个haskell函数,我想用精确的中间结果进行评估: f 0 x = 0 f n x = let tmp = f (n-1) x in tmp + (x-tmp^2)/2 由于(^2),复杂性在n中呈指数增长。因为我想做一个绘图,并且两个不同x的计算是完全独立的,所以我希望并行计算的加速比接近最优。我的代码: import Data.Ratio import Control.Parallel.Strategies f 0 x = 0 f n x = let tmp =
f 0 x = 0
f n x = let tmp = f (n-1) x in
tmp + (x-tmp^2)/2
由于(^2),复杂性在n中呈指数增长。因为我想做一个绘图,并且两个不同x的计算是完全独立的,所以我希望并行计算的加速比接近最优。我的代码:
import Data.Ratio
import Control.Parallel.Strategies
f 0 x = 0
f n x = let tmp = f (n-1) x in
tmp + (x-tmp^2)/2
main = do
it <- readLn
let fn = fromRational . f it
values = map fn [0,1%2..10] :: [Double]
computed = values `using` parBuffer 16 rseq
mapM_ (putStrLn . show) computed
我做错了什么?看起来它在锁(sys?)上花费了相当长的时间,而没有做有用的工作。我认为,由于整个运行时相对较小,在垃圾收集期间,堆的初始大小调整会使您遭受很大的痛苦。您可以通过通过“代码> > RTS -A100M < /代码>使初始分配区域更大。看来,您需要“代码> PARLIST/<代码>,然后<代码> PalpHuff谢谢,也可以用线程加速,考虑使用THEDCADE来查看每个内核上正在发生的事情。
$ ghc -threaded -O f.hs
[1 of 1] Compiling Main ( f.hs, f.o )
Linking f ...
$ time echo 20 | (./f +RTS -N1 > /dev/null)
real 0m4.760s
user 0m4.736s
sys 0m0.016s
$ time echo 20 | (./f +RTS -N2 > /dev/null)
real 0m4.041s
user 0m5.416s
sys 0m2.548s
$ time echo 20 | (./f +RTS -N3 > /dev/null)
real 0m4.884s
user 0m10.936s
sys 0m3.464s
$ time echo 20 | (./f +RTS -N4 > /dev/null)
real 0m5.536s
user 0m17.028s
sys 0m3.888s