GHC/Data Parallel Haskell/MainTimed

From HaskellWiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

The following variant of the main module for the dot product example determines and prints the runtime of the dot product kernel in microseconds.

import System.CPUTime (getCPUTime)
import System.Random (newStdGen)
import Control.Exception (evaluate)
import Data.Array.Parallel.PArray (PArray, randomRs, nf)
 
import DotP (dotp_wrapper)  -- import vectorised code
 
main :: IO ()
main
  = do 
      -- generate random input vectors
      gen1 <- newStdGen
      gen2 <- newStdGen
      let v = randomRs n range gen1
          w = randomRs n range gen2
      
      -- force the evaluation of the input vectors
      evaluate $ nf v
      evaluate $ nf w
      
      -- timed computations
      start <- getCPUTime
      let result = dotp_wrapper v w
      evaluate result
      end <- getCPUTime
      
      -- print the result
      putStrLn $ show result ++ " in " ++ show ((end - start) `div` 1000000) ++ "us"
  where
    n     = 10000        -- vector length
    range = (-100, 100)  -- range of vector elements