diff options
author | Alberto Ruiz <aruiz@um.es> | 2008-11-14 11:01:14 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2008-11-14 11:01:14 +0000 |
commit | e1b4cc06a5f98e576524b37ad0d9132f0678d722 (patch) | |
tree | 8c07f44ea5b138a131eea68348698822239ab5a8 /examples | |
parent | 52305f136a2ea232e354cb2b55c387c2f8930fbc (diff) |
constantD
Diffstat (limited to 'examples')
-rw-r--r-- | examples/benchmarks.hs | 66 |
1 files changed, 22 insertions, 44 deletions
diff --git a/examples/benchmarks.hs b/examples/benchmarks.hs index 6490b25..0e95851 100644 --- a/examples/benchmarks.hs +++ b/examples/benchmarks.hs | |||
@@ -18,7 +18,7 @@ time act = do | |||
18 | 18 | ||
19 | -------------------------------------------------------------------------------- | 19 | -------------------------------------------------------------------------------- |
20 | 20 | ||
21 | main = sequence_ [bench1,bench2,bench3,bench4, | 21 | main = sequence_ [bench1,bench2,bench4, |
22 | bench5 1000000 3, | 22 | bench5 1000000 3, |
23 | bench5 100000 50] | 23 | bench5 100000 50] |
24 | 24 | ||
@@ -26,16 +26,25 @@ w :: Vector Double | |||
26 | w = constant 1 5000000 | 26 | w = constant 1 5000000 |
27 | w2 = 1 * w | 27 | w2 = 1 * w |
28 | 28 | ||
29 | v = flatten $ ident 500 :: Vector Double | ||
30 | |||
31 | |||
29 | bench1 = do | 32 | bench1 = do |
33 | time $ print$ vectorMax (w+w2) -- evaluate it | ||
30 | putStrLn "Sum of a vector with 5M doubles:" | 34 | putStrLn "Sum of a vector with 5M doubles:" |
31 | print$ vectorMax (w+w2) -- evaluate it | 35 | print $ vectorMax v -- evaluate it |
32 | time $ printf " BLAS: %.2f: " $ sumVB w | 36 | -- time $ printf " BLAS: %.2f: " $ sumVB w |
33 | time $ printf " Haskell: %.2f: " $ sumVH w | 37 | time $ printf " Haskell: %.2f: " $ sumVH w |
34 | time $ printf " BLAS: %.2f: " $ sumVB w | 38 | time $ printf " BLAS: %.2f: " $ w <.> w2 |
35 | time $ printf " Haskell: %.2f: " $ sumVH w | 39 | time $ printf " Haskell: %.2f: " $ sumVH w |
36 | time $ printf " innerH: %.2f: " $ innerH w w2 | 40 | time $ printf " innerH: %.2f: " $ innerH w w2 |
37 | 41 | time $ printf "foldVector: %.2f: " $ sumVector w | |
38 | sumVB v = constant 1 (dim v) <.> v | 42 | let getPos k s = if k `mod` 500 < 200 && w@>k > 0 then k:s else s |
43 | putStrLn "foldLoop for element selection:" | ||
44 | time $ print $ (`divMod` 500) $ maximum $ foldLoop getPos [] (dim w) | ||
45 | putStrLn "constant 5M:" | ||
46 | time $ print $ constant (1::Double) 5000001 @> 7 | ||
47 | time $ print $ constant i 5000001 @> 7 | ||
39 | 48 | ||
40 | sumVH v = go (d - 1) 0 | 49 | sumVH v = go (d - 1) 0 |
41 | where | 50 | where |
@@ -51,8 +60,10 @@ innerH u v = go (d - 1) 0 | |||
51 | go 0 s = s + (u @> 0) * (v @> 0) | 60 | go 0 s = s + (u @> 0) * (v @> 0) |
52 | go !j !s = go (j - 1) (s + (u @> j) * (v @> j)) | 61 | go !j !s = go (j - 1) (s + (u @> j) * (v @> j)) |
53 | 62 | ||
54 | -- These functions are much faster if the library | 63 | |
55 | -- is configured with -funsafe | 64 | -- sumVector = foldVectorG (\k v s -> v k + s) 0.0 |
65 | sumVector = foldVector (+) 0.0 | ||
66 | |||
56 | 67 | ||
57 | -------------------------------------------------------------------------------- | 68 | -------------------------------------------------------------------------------- |
58 | 69 | ||
@@ -89,39 +100,6 @@ manymult n r = foldl1' (<>) (map r angles) | |||
89 | 100 | ||
90 | -------------------------------------------------------------------------------- | 101 | -------------------------------------------------------------------------------- |
91 | 102 | ||
92 | bench3 = do | ||
93 | putStrLn "-------------------------------------------------------" | ||
94 | putStrLn "foldVector" | ||
95 | let v = flatten $ ident 500 :: Vector Double | ||
96 | print $ vectorMax v -- evaluate it | ||
97 | |||
98 | putStrLn "sum, dim=5M:" | ||
99 | -- time $ print $ foldLoop (\k s -> w@>k + s) 0.0 (dim w) | ||
100 | time $ print $ sumVector w | ||
101 | |||
102 | putStrLn "sum, dim=0.25M:" | ||
103 | --time $ print $ foldLoop (\k s -> v@>k + s) 0.0 (dim v) | ||
104 | time $ print $ sumVector v | ||
105 | |||
106 | let getPos k s = if k `mod` 500 < 200 && v@>k > 0 then k:s else s | ||
107 | putStrLn "foldLoop for element selection, dim=0.25M:" | ||
108 | time $ print $ (`divMod` 500) $ maximum $ foldLoop getPos [] (dim v) | ||
109 | |||
110 | foldLoop f s d = go (d - 1) s | ||
111 | where | ||
112 | go 0 s = f (0::Int) s | ||
113 | go !j !s = go (j - 1) (f j s) | ||
114 | |||
115 | foldVector f s v = foldLoop g s (dim v) | ||
116 | where g !k !s = f k (v@>) s | ||
117 | {-# INLINE g #-} -- Thanks Ryan Ingram (http://permalink.gmane.org/gmane.comp.lang.haskell.cafe/46479) | ||
118 | |||
119 | sumVector = foldVector (\k v s -> v k + s) 0.0 | ||
120 | |||
121 | -- foldVector is slower if used in two places unless we use the above INLINE | ||
122 | -- this does not happen with foldLoop | ||
123 | -------------------------------------------------------------------------------- | ||
124 | |||
125 | bench4 = do | 103 | bench4 = do |
126 | putStrLn "-------------------------------------------------------" | 104 | putStrLn "-------------------------------------------------------" |
127 | putStrLn "1000x1000 inverse" | 105 | putStrLn "1000x1000 inverse" |