summaryrefslogtreecommitdiff
path: root/examples/parallel.hs
diff options
context:
space:
mode:
Diffstat (limited to 'examples/parallel.hs')
-rw-r--r--examples/parallel.hs28
1 files changed, 28 insertions, 0 deletions
diff --git a/examples/parallel.hs b/examples/parallel.hs
new file mode 100644
index 0000000..e875407
--- /dev/null
+++ b/examples/parallel.hs
@@ -0,0 +1,28 @@
1-- $ ghc --make -O -rtsopts -threaded parallel.hs
2-- $ ./parallel 3000 +RTS -N4 -s -A200M
3
4import System.Environment(getArgs)
5import Numeric.LinearAlgebra
6import Control.Parallel.Strategies
7import System.Time
8
9inParallel = parMap rwhnf id
10
11-- matrix product decomposed into p parallel subtasks
12parMul p x y = fromBlocks [ inParallel ( map (x <>) ys ) ]
13 where [ys] = toBlocksEvery (rows y) (cols y `div` p) y
14
15main = do
16 n <- (read . head) `fmap` getArgs
17 let m = ident n :: Matrix Double
18 time $ print $ maxElement $ takeDiag $ m <> m
19 time $ print $ maxElement $ takeDiag $ parMul 2 m m
20 time $ print $ maxElement $ takeDiag $ parMul 4 m m
21 time $ print $ maxElement $ takeDiag $ parMul 8 m m
22
23time act = do
24 t0 <- getClockTime
25 act
26 t1 <- getClockTime
27 print $ tdSec $ normalizeTimeDiff $ diffClockTimes t1 t0
28