Skip to content

Commit

Permalink
Add Rank and Indices examples
Browse files Browse the repository at this point in the history
  • Loading branch information
benl23x5 committed Jun 2, 2011
1 parent 9abf8f2 commit 5e7d2b2
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 0 deletions.
50 changes: 50 additions & 0 deletions dph-examples/imaginary/Indices/dph/IndicesVectorised.hs
@@ -0,0 +1,50 @@
{-# LANGUAGE ParallelArrays #-}
{-# OPTIONS -fvectorise #-}
module IndicesVectorised
(indicesPA, indices)
where
import Data.Array.Parallel.Prelude
import Data.Array.Parallel.Prelude.Int
import qualified Prelude as P


indicesPA :: PArray Int -> PArray Int -> PArray Int
{-# NOINLINE indicesPA #-}
indicesPA arr ixs
= toPArrayP (indices (fromPArrayP arr) (fromPArrayP ixs))


indices :: [:Int:] -> [:Int:] -> [:Int:]
indices arr ixs
= treeLookup arr ixs

{-
= mapP (thing arr) ixs
thing :: [:Int:] -> Int -> Int
thing arr i
= (sliceP i 1 arr) !: 0
-- arr !: i
thingo :: [:Int:] -> [:Int:] -> [:Int:]
thingo table is
= go is
where go is'
=
-}

treeLookup :: [:Int:] -> [:Int:] -> [:Int:]
{-# NOINLINE treeLookup #-}
treeLookup table xx
| lengthP xx == 1
= [: table !: (xx !: 0) :]

| otherwise
= let len = lengthP xx
half = len `div` 2
s1 = sliceP 0 half xx
s2 = sliceP half half xx
in concatP (mapP (treeLookup table) [: s1, s2 :])
39 changes: 39 additions & 0 deletions dph-examples/imaginary/Indices/dph/Main.hs
@@ -0,0 +1,39 @@

import Util
import Timing
import Randomish
import System.Environment
import Control.Exception
import qualified IndicesVectorised as ID
import qualified Data.Array.Parallel.PArray as P
import qualified Data.Vector.Unboxed as V


main
= do args <- getArgs

case args of
[alg, count] -> run alg (read count)
_ -> usage


run "vectorised" count
= do let arr = P.fromList [0 .. count - 1]
arr `seq` return ()

(arrResult, tElapsed)
<- time
$ let arr' = ID.indicesPA arr arr
in P.nf arr' `seq` return arr'

print $ P.length arrResult
putStr $ prettyTime tElapsed

run _ _
= usage


usage = putStr $ unlines
[ "usage: indices <algorithm> <count>\n"
, " algorithm one of " ++ show ["vectorised"]
, ""]
38 changes: 38 additions & 0 deletions dph-examples/imaginary/Rank/dph/Main.hs
@@ -0,0 +1,38 @@
import Util
import Timing
import Randomish
import System.Environment
import Control.Exception
import qualified RankVectorised as RD
import qualified Data.Array.Parallel.PArray as P
import qualified Data.Vector.Unboxed as V


main
= do args <- getArgs

case args of
[alg, count] -> run alg (read count)
_ -> usage


run "vectorised" count
= do let arr = P.fromList [0 .. count - 1]
arr `seq` return ()

(arrRanks, tElapsed)
<- time
$ let arr' = RD.ranksPA arr
in P.nf arr' `seq` return arr'

print $ P.length arrRanks
putStr $ prettyTime tElapsed

run _ _
= usage


usage = putStr $ unlines
[ "usage: rank <algorithm> <count>\n"
, " algorithm one of " ++ show ["vectorised"]
, ""]
19 changes: 19 additions & 0 deletions dph-examples/imaginary/Rank/dph/RankVectorised.hs
@@ -0,0 +1,19 @@
{-# LANGUAGE ParallelArrays #-}
{-# OPTIONS -fvectorise #-}
module RankVectorised
(ranksPA)
where
import Data.Array.Parallel.Prelude
import Data.Array.Parallel.Prelude.Int
import qualified Prelude as P


ranksPA :: PArray Int -> PArray Int
{-# NOINLINE ranksPA #-}
ranksPA ps
= toPArrayP (ranks (fromPArrayP ps))

ranks :: [:Int:] -> [:Int:]
{-# NOINLINE ranks #-}
ranks arr = [: lengthP [: a | a <- arr, a < b :] | b <- arr :]

0 comments on commit 5e7d2b2

Please sign in to comment.