summaryrefslogtreecommitdiff
path: root/examples/benchmarks.hs
diff options
context:
space:
mode:
Diffstat (limited to 'examples/benchmarks.hs')
-rw-r--r--examples/benchmarks.hs151
1 files changed, 0 insertions, 151 deletions
diff --git a/examples/benchmarks.hs b/examples/benchmarks.hs
deleted file mode 100644
index d7f8c40..0000000
--- a/examples/benchmarks.hs
+++ /dev/null
@@ -1,151 +0,0 @@
1{-# LANGUAGE BangPatterns #-}
2
3-- $ ghc --make -O2 benchmarks.hs
4
5
6import Numeric.LinearAlgebra
7import System.Time
8import System.CPUTime
9import Text.Printf
10import Data.List(foldl1')
11
12
13time act = do
14 t0 <- getCPUTime
15 act
16 t1 <- getCPUTime
17 printf "%.3f s CPU\n" $ (fromIntegral (t1 - t0) / (10^12 :: Double)) :: IO ()
18
19--------------------------------------------------------------------------------
20
21main = sequence_ [bench1,
22 bench2,
23 bench4,
24 bench5 1000000 3, bench5 100000 50,
25 bench6 100 (100000::Double), bench6 100000 (100::Double), bench6 10000 (1000::Double)]
26
27w :: Vector Double
28w = constant 1 5000000
29w2 = 1 * w
30
31v = flatten $ ident 500 :: Vector Double
32
33
34bench1 = do
35 time $ print$ vectorMax (w+w2) -- evaluate it
36 putStrLn "Sum of a vector with 5M doubles:"
37 print$ vectorMax (w+w2) -- evaluate it
38 time $ printf " BLAS: %.2f: " $ sumVB w
39 time $ printf "BLAS only dot: %.2f: " $ w <.> w2
40 time $ printf " Haskell: %.2f: " $ sumVH w
41 time $ printf " innerH: %.2f: " $ innerH w w2
42 time $ printf "foldVector: %.2f: " $ sumVector w
43 let getPos k s = if k `mod` 500 < 200 && w@>k > 0 then k:s else s
44 putStrLn "foldLoop for element selection:"
45 time $ print $ (`divMod` 500) $ maximum $ foldLoop getPos [] (dim w)
46 putStrLn "constant 5M:"
47 time $ print $ constant (1::Double) 5000001 @> 7
48 time $ print $ constant i 5000001 @> 7
49 time $ print $ conj (constant i 5000001) @> 7
50 putStrLn "zips C vs H:"
51 time $ print $ (w / w2) @> 7
52 time $ print $ (zipVector (/) w w2) @> 7
53 putStrLn "folds C/BLAS vs H:"
54 let t = constant (1::Double) 5000002
55 print $ t @> 7
56 time $ print $ foldVector max (t@>0) t
57 time $ print $ vectorMax t
58 time $ print $ sqrt $ foldVector (\v s -> v*v+s) 0 t
59 time $ print $ pnorm PNorm2 t
60 putStrLn "scale C/BLAS vs H:"
61 time $ print $ mapVector (*2) t @> 7
62 time $ print $ (2 * t) @> 7
63
64sumVB v = constant 1 (dim v) <.> v
65
66sumVH v = go (d - 1) 0
67 where
68 d = dim v
69 go :: Int -> Double -> Double
70 go 0 s = s + (v @> 0)
71 go !j !s = go (j - 1) (s + (v @> j))
72
73innerH u v = go (d - 1) 0
74 where
75 d = min (dim u) (dim v)
76 go :: Int -> Double -> Double
77 go 0 s = s + (u @> 0) * (v @> 0)
78 go !j !s = go (j - 1) (s + (u @> j) * (v @> j))
79
80
81-- sumVector = foldVectorG (\k v s -> v k + s) 0.0
82sumVector = foldVector (+) 0.0
83
84--------------------------------------------------------------------------------
85
86bench2 = do
87 putStrLn "-------------------------------------------------------"
88 putStrLn "Multiplication of 1M different 3x3 matrices:"
89-- putStrLn "from [[]]"
90-- time $ print $ manymult (10^6) rot'
91-- putStrLn "from (3><3) []"
92 time $ print $ manymult (10^6) rot
93 print $ cos (10^6/2)
94
95
96rot' :: Double -> Matrix Double
97rot' a = matrix [[ c,0,s],
98 [ 0,1,0],
99 [-s,0,c]]
100 where c = cos a
101 s = sin a
102 matrix = fromLists
103
104rot :: Double -> Matrix Double
105rot a = (3><3) [ c,0,s
106 , 0,1,0
107 ,-s,0,c ]
108 where c = cos a
109 s = sin a
110
111manymult n r = foldl1' (<>) (map r angles)
112 where angles = toList $ linspace n (0,1)
113 -- angles = map (k*) [0..n']
114 -- n' = fromIntegral n - 1
115 -- k = recip n'
116
117--------------------------------------------------------------------------------
118
119bench4 = do
120 putStrLn "-------------------------------------------------------"
121 putStrLn "1000x1000 inverse"
122 let a = ident 1000 :: Matrix Double
123 let b = 2*a
124 print $ vectorMax $ flatten (a+b) -- evaluate it
125 time $ print $ vectorMax $ flatten $ linearSolve a b
126
127--------------------------------------------------------------------------------
128
129op1 a b = a <> trans b
130op2 a b = a + trans b
131
132timep = time . print . vectorMax . flatten
133
134bench5 n d = do
135 putStrLn "-------------------------------------------------------"
136 putStrLn "transpose in add"
137 let ms = replicate n ((ident d :: Matrix Double))
138 timep $ foldl1' (+) ms
139 timep $ foldl1' op2 ms
140 putStrLn "-------------------------------------------------------"
141 putStrLn "transpose in multiply"
142
143 timep $ foldl1' (<>) ms
144 timep $ foldl1' op1 ms
145
146--------------------------------------------------------------------------------
147
148bench6 sz n = do
149 putStrLn "-------------------------------------------------------"
150 putStrLn "many constants"
151 time $ print $ sum $ map ((@>0). flip constant sz) [1..n] \ No newline at end of file