Tob*_*ias 7 concurrency multithreading haskell
我有一个haskell函数,我想用精确的中间结果进行评估:
f 0 x = 0
f n x = let tmp = f (n-1) x in
tmp + (x-tmp^2)/2
Run Code Online (Sandbox Code Playgroud)
由于(^ 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
Run Code Online (Sandbox Code Playgroud)
但令我惊讶的是,这并没有真正扩展(在我的HT双核i3上):
$ 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
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?它似乎花了很长时间在锁(sys?)而不是做有用的工作.
归档时间: |
|
查看次数: |
109 次 |
最近记录: |