diff options
Diffstat (limited to 'packages/hmatrix/examples/parallel.hs')
-rw-r--r-- | packages/hmatrix/examples/parallel.hs | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/packages/hmatrix/examples/parallel.hs b/packages/hmatrix/examples/parallel.hs new file mode 100644 index 0000000..e875407 --- /dev/null +++ b/packages/hmatrix/examples/parallel.hs | |||
@@ -0,0 +1,28 @@ | |||
1 | -- $ ghc --make -O -rtsopts -threaded parallel.hs | ||
2 | -- $ ./parallel 3000 +RTS -N4 -s -A200M | ||
3 | |||
4 | import System.Environment(getArgs) | ||
5 | import Numeric.LinearAlgebra | ||
6 | import Control.Parallel.Strategies | ||
7 | import System.Time | ||
8 | |||
9 | inParallel = parMap rwhnf id | ||
10 | |||
11 | -- matrix product decomposed into p parallel subtasks | ||
12 | parMul p x y = fromBlocks [ inParallel ( map (x <>) ys ) ] | ||
13 | where [ys] = toBlocksEvery (rows y) (cols y `div` p) y | ||
14 | |||
15 | main = 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 | |||
23 | time act = do | ||
24 | t0 <- getClockTime | ||
25 | act | ||
26 | t1 <- getClockTime | ||
27 | print $ tdSec $ normalizeTimeDiff $ diffClockTimes t1 t0 | ||
28 | |||