diff options
author | Alberto Ruiz <aruiz@um.es> | 2008-06-10 08:31:27 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2008-06-10 08:31:27 +0000 |
commit | 53b361e5b9840273ccfd50e5b256002498a97469 (patch) | |
tree | c96ae1a373cd6884821f4b74b9e6cabd5e6f2204 | |
parent | 9cb72979acc7bcd9df2fa8eab05169d9c5ca84f5 (diff) |
added examples/benchmarks.hs
-rw-r--r-- | examples/benchmarks.hs | 44 | ||||
-rw-r--r-- | lib/Data/Packed/Internal.hs | 2 | ||||
-rw-r--r-- | lib/Data/Packed/Internal/Matrix.hs | 27 | ||||
-rw-r--r-- | lib/Data/Packed/Internal/Vector.hs | 22 | ||||
-rw-r--r-- | lib/Data/Packed/Matrix.hs | 2 |
5 files changed, 46 insertions, 51 deletions
diff --git a/examples/benchmarks.hs b/examples/benchmarks.hs new file mode 100644 index 0000000..7fea3ed --- /dev/null +++ b/examples/benchmarks.hs | |||
@@ -0,0 +1,44 @@ | |||
1 | {-# OPTIONS -fbang-patterns #-} | ||
2 | |||
3 | -- compile as: | ||
4 | -- ghc --make -O2 -optc-O2 -fvia-C benchmarks.hs -o benchmarks | ||
5 | |||
6 | import Numeric.LinearAlgebra | ||
7 | import System.Time | ||
8 | import System.CPUTime | ||
9 | import Text.Printf | ||
10 | |||
11 | |||
12 | time act = do | ||
13 | t0 <- getCPUTime | ||
14 | act | ||
15 | t1 <- getCPUTime | ||
16 | putStrLn $ show ((t1-t0) `div` 10^10) ++ " cs" | ||
17 | |||
18 | time' act = do | ||
19 | t0 <- getClockTime | ||
20 | act | ||
21 | t1 <- getClockTime | ||
22 | putStrLn $ timeDiffToString $ normalizeTimeDiff $ diffClockTimes t1 t0 | ||
23 | |||
24 | -------------------------------------------------------------------------------- | ||
25 | |||
26 | main = sequence_ [bench1] | ||
27 | |||
28 | bench1 = do | ||
29 | putStrLn "sum of a vector with 30 million doubles:" | ||
30 | time $ printf " BLAS: %.2f: " $ sumV sumVB 30000000 | ||
31 | time $ printf " Haskell: %.2f: " $ sumV sumVH 30000000 | ||
32 | time $ printf " BLAS: %.2f: " $ sumV sumVB 30000000 | ||
33 | time $ printf " Haskell: %.2f: " $ sumV sumVH 30000000 | ||
34 | |||
35 | sumV f n = f (constant (1::Double) n) | ||
36 | |||
37 | sumVB v = constant 1 (dim v) <.> v | ||
38 | |||
39 | sumVH v = go (d - 1) 0 | ||
40 | where | ||
41 | d = dim v | ||
42 | go :: Int -> Double -> Double | ||
43 | go 0 s = s + (v @> 0) | ||
44 | go !j !s = go (j - 1) (s + (v @> j)) | ||
diff --git a/lib/Data/Packed/Internal.hs b/lib/Data/Packed/Internal.hs index e5028d3..0bf9f8c 100644 --- a/lib/Data/Packed/Internal.hs +++ b/lib/Data/Packed/Internal.hs | |||
@@ -21,4 +21,4 @@ module Data.Packed.Internal ( | |||
21 | 21 | ||
22 | import Data.Packed.Internal.Common | 22 | import Data.Packed.Internal.Common |
23 | import Data.Packed.Internal.Vector | 23 | import Data.Packed.Internal.Vector |
24 | import Data.Packed.Internal.Matrix \ No newline at end of file | 24 | import Data.Packed.Internal.Matrix |
diff --git a/lib/Data/Packed/Internal/Matrix.hs b/lib/Data/Packed/Internal/Matrix.hs index 7a35a61..caf3699 100644 --- a/lib/Data/Packed/Internal/Matrix.hs +++ b/lib/Data/Packed/Internal/Matrix.hs | |||
@@ -394,30 +394,3 @@ fromFile filename (r,c) = do | |||
394 | --free charname -- TO DO: free the auxiliary CString | 394 | --free charname -- TO DO: free the auxiliary CString |
395 | return res | 395 | return res |
396 | foreign import ccall "auxi.h matrix_fscanf" c_gslReadMatrix:: Ptr CChar -> TM | 396 | foreign import ccall "auxi.h matrix_fscanf" c_gslReadMatrix:: Ptr CChar -> TM |
397 | |||
398 | ---------------------------------------------------------------------------- | ||
399 | |||
400 | {- | creates a new matrix with the given position updated with a modification function | ||
401 | |||
402 | @> updateMatrix (0,2) (const 57) (ident 3 :: Matrix Double) | ||
403 | (3><3) | ||
404 | [ 1.0, 0.0, 57.0 | ||
405 | , 0.0, 1.0, 0.0 | ||
406 | , 0.0, 0.0, 1.0 ]@ | ||
407 | |||
408 | -} | ||
409 | updateMatrix :: Storable t | ||
410 | => (Int,Int) -- ^ position (row,column) | ||
411 | -> (t -> t) -- ^ modification function | ||
412 | -> Matrix t -- ^ source matrix | ||
413 | -> Matrix t -- ^ result | ||
414 | |||
415 | updateMatrix (i,j) f (m@MC {rows = r, cols = c, cdat = v}) | ||
416 | | i<0 || i>=r || j<0 || j>=c = error $ "updateMatrix out of range(size="++show (r,c)++", pos="++show (i,j)++")" | ||
417 | | otherwise = let pos = i*c+j | ||
418 | in m { cdat = updateVector pos f v } | ||
419 | |||
420 | updateMatrix (i,j) f (m@MF {rows = r, cols = c, fdat = v}) | ||
421 | | i<0 || i>=r || j<0 || j>=c = error $ "updateMatrix out of range(size="++show (r,c)++", pos="++show (i,j)++")" | ||
422 | | otherwise = let pos = j*r+i | ||
423 | in m { fdat = updateVector pos f v } | ||
diff --git a/lib/Data/Packed/Internal/Vector.hs b/lib/Data/Packed/Internal/Vector.hs index 6274e48..2df33e0 100644 --- a/lib/Data/Packed/Internal/Vector.hs +++ b/lib/Data/Packed/Internal/Vector.hs | |||
@@ -189,28 +189,6 @@ liftVector2 f u v = fromList $ zipWith f (toList u) (toList v) | |||
189 | 189 | ||
190 | ----------------------------------------------------------------- | 190 | ----------------------------------------------------------------- |
191 | 191 | ||
192 | {- | creates a new vector with a desired position updated with a modification function | ||
193 | |||
194 | @> updateVector 3 (+7) (fromList [1..5]) | ||
195 | 5 |> [1.0,2.0,3.0,11.0,5.0]@ | ||
196 | |||
197 | -} | ||
198 | updateVector :: Storable t => Int -- ^ position | ||
199 | -> (t->t) -- ^ modification function | ||
200 | -> Vector t -- ^ source | ||
201 | -> Vector t -- ^ result | ||
202 | updateVector k h (v@V {dim=n}) | ||
203 | | k<0 || k >= n = error $ "updateVector out of range (dim="++show n++", pos="++show k++")" | ||
204 | | otherwise = unsafePerformIO $ do | ||
205 | r <- createVector n | ||
206 | let f _ s _ d = copyArray d s n | ||
207 | >> pokeElemOff d k (h (v`at'`k)) | ||
208 | >> return 0 | ||
209 | app2 f vec v vec r "updateVector" | ||
210 | return r | ||
211 | |||
212 | ----------------------------------------------------------------- | ||
213 | |||
214 | cloneVector :: Storable t => Vector t -> IO (Vector t) | 192 | cloneVector :: Storable t => Vector t -> IO (Vector t) |
215 | cloneVector (v@V {dim=n}) = do | 193 | cloneVector (v@V {dim=n}) = do |
216 | r <- createVector n | 194 | r <- createVector n |
diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs index 62d28b1..7d2c564 100644 --- a/lib/Data/Packed/Matrix.hs +++ b/lib/Data/Packed/Matrix.hs | |||
@@ -226,4 +226,4 @@ extractRows l m = fromRows $ extract (toRows $ m) l | |||
226 | 226 | ||
227 | -} | 227 | -} |
228 | repmat :: (Element t) => Matrix t -> Int -> Int -> Matrix t | 228 | repmat :: (Element t) => Matrix t -> Int -> Int -> Matrix t |
229 | repmat m r c = fromBlocks $ partit c $ replicate (r*c) m \ No newline at end of file | 229 | repmat m r c = fromBlocks $ partit c $ replicate (r*c) m |