diff options
Diffstat (limited to 'examples/parallel.hs')
-rw-r--r-- | examples/parallel.hs | 19 |
1 files changed, 2 insertions, 17 deletions
diff --git a/examples/parallel.hs b/examples/parallel.hs index 3c83c54..435e367 100644 --- a/examples/parallel.hs +++ b/examples/parallel.hs | |||
@@ -6,19 +6,16 @@ import Control.Parallel.Strategies | |||
6 | import System.Time | 6 | import System.Time |
7 | 7 | ||
8 | inParallel = parMap rwhnf id | 8 | inParallel = parMap rwhnf id |
9 | -- rdeepseq (or older rnf) not needed in this case | 9 | |
10 | 10 | ||
11 | -- matrix product decomposed into p parallel subtasks | 11 | -- matrix product decomposed into p parallel subtasks |
12 | parMul p x y = fromBlocks [ inParallel ( map (x <>) ys ) ] | 12 | parMul p x y = fromBlocks [ inParallel ( map (x <>) ys ) ] |
13 | where ys = splitColumns p y | 13 | where [ys] = toBlocksEvery (rows y) (cols y `div` p) y |
14 | |||
15 | -- x <||> y = fromColumns . inParallel . map (x <>) . toColumns $ y | ||
16 | 14 | ||
17 | main = do | 15 | main = do |
18 | n <- (read . head) `fmap` getArgs | 16 | n <- (read . head) `fmap` getArgs |
19 | let m = ident n :: Matrix Double | 17 | let m = ident n :: Matrix Double |
20 | time $ print $ vectorMax $ takeDiag $ m <> m | 18 | time $ print $ vectorMax $ takeDiag $ m <> m |
21 | -- time $ print $ vectorMax $ takeDiag $ m <||> m | ||
22 | time $ print $ vectorMax $ takeDiag $ parMul 2 m m | 19 | time $ print $ vectorMax $ takeDiag $ parMul 2 m m |
23 | time $ print $ vectorMax $ takeDiag $ parMul 4 m m | 20 | time $ print $ vectorMax $ takeDiag $ parMul 4 m m |
24 | time $ print $ vectorMax $ takeDiag $ parMul 8 m m | 21 | time $ print $ vectorMax $ takeDiag $ parMul 8 m m |
@@ -28,15 +25,3 @@ time act = do | |||
28 | act | 25 | act |
29 | t1 <- getClockTime | 26 | t1 <- getClockTime |
30 | print $ tdSec $ normalizeTimeDiff $ diffClockTimes t1 t0 | 27 | print $ tdSec $ normalizeTimeDiff $ diffClockTimes t1 t0 |
31 | |||
32 | splitColumns n m = splitColumns' (f n (cols m)) m | ||
33 | where | ||
34 | splitColumns' [] m = [] | ||
35 | splitColumns' ((a,b):rest) m = subMatrix (0,a) (rows m, b-a+1) m : splitColumns' rest m | ||
36 | |||
37 | f :: Int -> Int -> [(Int,Int)] | ||
38 | f n c = zip ks (map pred $ tail ks) | ||
39 | where ks = map round $ toList $ linspace (fromIntegral n+1) (0,fromIntegral c) | ||
40 | |||
41 | splitRowsAt p m = (takeRows p m, dropRows p m) | ||
42 | splitColumnsAt p m = (takeColumns p m, dropColumns p m) | ||