Mercurial > hg > Members > toma > Jungle-haskell
annotate test/runEvalTest/ParSum.hs @ 26:02e7f2f50bdc
add test2
author | Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 04 Feb 2014 02:33:27 +0900 |
parents | efb4e880ac7f |
children |
rev | line source |
---|---|
25
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
1 {-# LANGUAGE OverloadedStrings #-} |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
2 |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
3 import Control.Parallel |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
4 import Control.Parallel.Strategies |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
5 import Text.Printf |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
6 import Data.Time.Clock |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
7 import Control.Exception |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
8 import System.Environment |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
9 |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
10 num = 1000000 |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
11 |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
12 main = do |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
13 t0 <- getCurrentTime |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
14 printTimeSince t0 |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
15 --a <- evaluate $ runEval test3 |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
16 --a <- evaluate $ runEval (test1 100) |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
17 a <- evaluate $ ((map (\x -> (sum' (x*num) ((x+1)*num))) [0..10]) `using` parList' rpar) |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
18 print a |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
19 printTimeSince t0 |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
20 |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
21 parList' s [x] = do |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
22 a <- s x |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
23 return [a] |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
24 parList' s (x:xs) = do |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
25 a <- s x |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
26 b <- parList' s xs |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
27 return (a:b) |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
28 |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
29 test = do |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
30 a <- rpar (sum' 0 num) |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
31 b <- rpar (sum' num (num*2)) |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
32 return (a,b) |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
33 |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
34 test1 i = do |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
35 a <- rpar (sum' (i*num) ((i+1) * num)) |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
36 if i == 0 |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
37 then (return [a]) |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
38 else do b <- test1 (i-1) |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
39 return (a:b) |
26 | 40 |
41 test2 :: Eval (Integer, Integer) | |
42 test2 = rpar (sum' 0 num) >>= (\a -> | |
43 rpar (sum' num (num*2)) >>= (\b -> | |
44 return (a,b))) | |
45 | |
25
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
46 test3 = do |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
47 a <- rpar (sum' 0 num) |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
48 b <- rpar (sum' num (if a < num then a else (num*2))) |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
49 return (a,b) |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
50 |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
51 sum' begin end = if begin < end |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
52 then begin + (sum' (begin + 1) end) |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
53 else begin |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
54 |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
55 printTimeSince t0 = do |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
56 t1 <- getCurrentTime |
efb4e880ac7f
add simple parallel execution test
Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
57 printf "time: %.2fs\n" (realToFrac (diffUTCTime t1 t0) :: Double) |