您可以尝试将引发计算的结果放入严格的数据结构中
{-# LANGUAGE BangPatterns #-}
module Main where
import Control.Parallel
fib :: Int -> Int
fib n
| n < 1 = 0
| n == 1 = 1
| otherwise = fib (n-1) + fib (n-2)
trib :: Int -> Int
trib n
| n < 1 = 0
| n < 3 = 1
| otherwise = trib (n-1) + trib (n-2) + trib (n-3)
data R = R { res1, res2 :: !Int }
main :: IO ()
main = do
let !r = let a = fib 38
b = trib 31
in a `par` b `pseq` (R a b)
print $ res1 r
print $ fib 28
print $ res2 r
这在这里工作:
$ ./spark +RTS -N2 -s
39088169
317811
53798080
65,328 bytes allocated in the heap
9,688 bytes copied during GC
5,488 bytes maximum residency (1 sample(s))
30,680 bytes maximum slop
2 MB total memory in use (0 MB lost due to fragmentation)
Tot time (elapsed) Avg pause Max pause
Gen 0 1 colls, 0 par 0.00s 0.00s 0.0001s 0.0001s
Gen 1 1 colls, 1 par 0.00s 0.00s 0.0001s 0.0001s
Parallel GC work balance: 1.33 (686 / 515, ideal 2)
MUT time (elapsed) GC time (elapsed)
Task 0 (worker) : 0.59s ( 0.59s) 0.00s ( 0.00s)
Task 1 (worker) : 0.00s ( 0.59s) 0.00s ( 0.00s)
Task 2 (bound) : 0.59s ( 0.59s) 0.00s ( 0.00s)
Task 3 (worker) : 0.00s ( 0.59s) 0.00s ( 0.00s)
SPARKS: 1 (1 converted, 0 overflowed, 0 dud, 0 GC'd, 0 fizzled)
INIT time 0.00s ( 0.00s elapsed)
MUT time 1.17s ( 0.59s elapsed)
GC time 0.00s ( 0.00s elapsed)
EXIT time 0.00s ( 0.00s elapsed)
Total time 1.18s ( 0.59s elapsed)
Alloc rate 55,464 bytes per MUT second
Productivity 99.9% of total user, 199.1% of total elapsed
gc_alloc_block_sync: 0
whitehole_spin: 0
gen[0].sync: 0
gen[1].sync: 0