summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2010-04-24 09:47:42 +0000
committerAlberto Ruiz <aruiz@um.es>2010-04-24 09:47:42 +0000
commit0c1b8611b9cc55cc646c708b63442f3aaac1249d (patch)
tree4dbd5b25ec0833aaeaaae5157f74e11b94359624 /examples
parente36a58715c9be88bb9091f0e86fda45465c61c0b (diff)
examples/parallel using toBlocks
Diffstat (limited to 'examples')
-rw-r--r--examples/parallel.hs19
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
6import System.Time 6import System.Time
7 7
8inParallel = parMap rwhnf id 8inParallel = 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
12parMul p x y = fromBlocks [ inParallel ( map (x <>) ys ) ] 12parMul 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
17main = do 15main = 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
32splitColumns 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
41splitRowsAt p m = (takeRows p m, dropRows p m)
42splitColumnsAt p m = (takeColumns p m, dropColumns p m)