public
Created

Parallel strategy applied to Array

  • Download Gist
parr.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
import qualified Data.Array as A
import Control.Parallel.Strategies
 
ack 0 n = n+1
ack m 0 = ack (m-1) 1
ack m n = ack (m-1) (ack m (n-1))
 
main = do
let arr = A.listArray (0,999) [1..] :: A.Array Int Int
let res = (fmap (ack 2) arr) `using` parTraversable rdeepseq
print res
 
{-
Speed-up ≈ 1.9. Works also with Data.Array.Unboxed.
 
$ ghc --make -threaded -O2 parr.hs
[1 of 1] Compiling Main ( parr.hs, parr.o )
Linking parr ...
 
$ time ./parr +RTS -N1 >/dev/null
 
real 0m7.058s
user 0m6.870s
sys 0m0.030s
 
$ time ./parr +RTS -N2 >/dev/null
 
real 0m3.649s
user 0m7.140s
sys 0m0.000s
 
-}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.