[commit: dph] master: Fix reverse example (c223c04)
Ben Lippmeier
benl at ouroborus.net
Wed Apr 20 04:03:49 CEST 2011
Repository : ssh://darcs.haskell.org//srv/darcs/packages/dph
On branch : master
http://hackage.haskell.org/trac/ghc/changeset/c223c04920bbd45d82d0943dd958c4e59224a627
>---------------------------------------------------------------
commit c223c04920bbd45d82d0943dd958c4e59224a627
Author: Ben Lippmeier <benl at ouroborus.net>
Date: Thu Apr 14 15:42:09 2011 +1000
Fix reverse example
>---------------------------------------------------------------
dph-examples/broken/Reverse/dph/ReverseVector.hs | 21 -------
.../broken/Reverse/dph/ReverseVectorised.hs | 30 -----------
dph-examples/imaginary/Reverse/dph/Main.hs | 56 ++++++++++++++++++++
.../imaginary/Reverse/dph/ReverseVector.hs | 21 +++++++
.../imaginary/Reverse/dph/ReverseVectorised.hs | 29 ++++++++++
5 files changed, 106 insertions(+), 51 deletions(-)
diff --git a/dph-examples/broken/Reverse/dph/ReverseVector.hs b/dph-examples/broken/Reverse/dph/ReverseVector.hs
deleted file mode 100644
index 42a10b4..0000000
--- a/dph-examples/broken/Reverse/dph/ReverseVector.hs
+++ /dev/null
@@ -1,21 +0,0 @@
-
-module ReverseVector
- (treeReverse)
-where
-import qualified Data.Vector.Unboxed as V
-import Data.Vector.Unboxed (Vector)
-
--- | Reverse the elements in an array using a tree.
-treeReverse :: Vector Double -> Vector Double
-{-# NOINLINE treeReverse #-}
-treeReverse xx
- | V.length xx == 1
- = xx
-
- | otherwise
- = let len = V.length xx
- half = len `div` 2
- s1 = V.slice 0 half xx
- s2 = V.slice half half xx
- in treeReverse s2 V.++ treeReverse s1
-
diff --git a/dph-examples/broken/Reverse/dph/ReverseVectorised.hs b/dph-examples/broken/Reverse/dph/ReverseVectorised.hs
deleted file mode 100644
index 2a3f97b..0000000
--- a/dph-examples/broken/Reverse/dph/ReverseVectorised.hs
+++ /dev/null
@@ -1,30 +0,0 @@
-{-# LANGUAGE ParallelArrays #-}
-{-# OPTIONS -fvectorise #-}
-module ReverseVectorised
- (treeReversePA)
-where
-import Data.Array.Parallel.Prelude
-import Data.Array.Parallel.Prelude.Double
-import qualified Data.Array.Parallel.Prelude.Int as Int
-import qualified Prelude as P
-
-
-treeReversePA :: PArray Double -> PArray Double
-{-# NOINLINE treeReversePA #-}
-treeReversePA ps
- = toPArrayP (treeReverse (fromPArrayP ps))
-
-
--- | Reverse the elements in an array using a tree.
-treeReverse :: [:Double:] -> [:Double:]
-{-# NOINLINE treeReverse #-}
-treeReverse xx
- | lengthP xx Int.== 1
- = xx
-
- | otherwise
- = let len = lengthP xx
- half = len `Int.div` 2
- s1 = sliceP 0 half xx
- s2 = sliceP half half xx
- in concatP (mapP treeReverse [:s1, s2:])
diff --git a/dph-examples/imaginary/Reverse/dph/Main.hs b/dph-examples/imaginary/Reverse/dph/Main.hs
new file mode 100644
index 0000000..e1b3242
--- /dev/null
+++ b/dph-examples/imaginary/Reverse/dph/Main.hs
@@ -0,0 +1,56 @@
+
+import Util
+import Timing
+import Randomish
+import System.Environment
+import System.Random
+import Control.Exception
+import qualified ReverseVector as RV
+import qualified ReverseVectorised 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]
+ | not $ isPowerOfTwo (read count)
+ -> error "reverse: length of array must be a power of two."
+
+ | otherwise -> run alg (read count)
+
+ _ -> usage
+
+
+run "vectorised" count
+ = do let arr = P.fromList [0 .. count - 1]
+ arr `seq` return ()
+
+ (arrReversed, tElapsed)
+ <- time
+ $ let arr' = RD.treeReversePA arr
+ in arr' `seq` return arr'
+
+ putStr $ prettyTime tElapsed
+
+run "vector" count
+ = do let arr = V.fromList [0 .. count - 1]
+ arr `seq` return ()
+
+ (arrReversed, tElapsed)
+ <- time
+ $ let arr' = RV.treeReverse arr
+ in arr' `seq` return arr'
+
+ putStr $ prettyTime tElapsed
+
+run _ _
+ = usage
+
+
+usage = putStr $ unlines
+ [ "usage: reverse <algorithm> <count>\n"
+ , " algorithm one of " ++ show ["vectorised"]
+ , ""]
diff --git a/dph-examples/imaginary/Reverse/dph/ReverseVector.hs b/dph-examples/imaginary/Reverse/dph/ReverseVector.hs
new file mode 100644
index 0000000..e0d275c
--- /dev/null
+++ b/dph-examples/imaginary/Reverse/dph/ReverseVector.hs
@@ -0,0 +1,21 @@
+
+module ReverseVector
+ (treeReverse)
+where
+import qualified Data.Vector.Unboxed as V
+import Data.Vector.Unboxed (Vector)
+
+-- | Reverse the elements in an array using a tree.
+treeReverse :: Vector Int -> Vector Int
+{-# NOINLINE treeReverse #-}
+treeReverse xx
+ | V.length xx == 1
+ = xx
+
+ | otherwise
+ = let len = V.length xx
+ half = len `div` 2
+ s1 = V.slice 0 half xx
+ s2 = V.slice half half xx
+ in treeReverse s2 V.++ treeReverse s1
+
diff --git a/dph-examples/imaginary/Reverse/dph/ReverseVectorised.hs b/dph-examples/imaginary/Reverse/dph/ReverseVectorised.hs
new file mode 100644
index 0000000..a4ace75
--- /dev/null
+++ b/dph-examples/imaginary/Reverse/dph/ReverseVectorised.hs
@@ -0,0 +1,29 @@
+{-# LANGUAGE ParallelArrays #-}
+{-# OPTIONS -fvectorise #-}
+module ReverseVectorised
+ (treeReversePA)
+where
+import Data.Array.Parallel.Prelude
+import Data.Array.Parallel.Prelude.Int
+import qualified Prelude as P
+
+
+treeReversePA :: PArray Int -> PArray Int
+{-# NOINLINE treeReversePA #-}
+treeReversePA ps
+ = toPArrayP (treeReverse (fromPArrayP ps))
+
+
+-- | Reverse the elements in an array using a tree.
+treeReverse :: [:Int:] -> [:Int:]
+{-# NOINLINE treeReverse #-}
+treeReverse xx
+ | lengthP xx == 1
+ = xx
+
+ | otherwise
+ = let len = lengthP xx
+ half = len `div` 2
+ s1 = sliceP 0 half xx
+ s2 = sliceP half len xx
+ in treeReverse s2 +:+ treeReverse s1
More information about the Cvs-libraries
mailing list