c6d4f1e91c9c0c5ca0440d11867c5673075ace60
1 import DotPVect ( dotp )
3 import Control.Exception (evaluate)
4 import System.Console.GetOpt
5 import qualified System.Random as R
7 import qualified Data.Array.Parallel.Unlifted as U
8 import qualified Data.Array.Parallel.PArray as P
9 import Data.Array.Parallel.PArray (PArray)
13 generateVectorU :: Int -> IO (U.Array Double)
14 generateVectorU n =
15 do
16 let seed = 42742
17 let rg = R.mkStdGen seed
18 let -- The std random function is too slow to generate really big vectors
19 -- with. Instead, we generate a short random vector and repeat that.
20 randvec = U.randomRs k (-100, 100) rg
21 vec = U.map (\i -> U.index "generateVectorU" randvec (i `mod` k)) (U.enumFromTo 0 (n-1))
22 evaluate vec
23 return vec
24 where
25 k = 1000
27 generateVector :: Int -> IO (PArray Double)
28 generateVector n
29 = do
30 vec <- generateVectorU n
31 return \$ P.fromUArray vec
33 generateVectors :: Int -> IO (PArray Double, PArray Double)
34 generateVectors n =
35 do
36 v <- generateVector n
37 w <- generateVector n
38 return (v,w)
40 main
41 = do -- compute dot product with NDP
42 vectors <- generateVectors 100000
43 let resultViaNDP = (uncurry dotp) vectors
45 -- compute with lists
46 let (aVecX, aVecY) = vectors
47 let vecX = P.toList aVecX
48 let vecY = P.toList aVecY
49 let resultViaList = sum \$ zipWith (*) vecX vecY
51 -- ignore wibbles in low order bits
52 putStr \$ (take 12 \$ show resultViaNDP) ++ "\n"
53 putStr \$ (take 12 \$ show resultViaList) ++ "\n"