1b9ff0c873789084dc44b035ded5c83e082c0cce
[packages/dph.git] / dph-prim-par / Data / Array / Parallel / Unlifted / Parallel / Basics.hs
1 {-# LANGUAGE CPP #-}
2 #include "fusion-phases.h"
3
4 -- | Basic operations on parallel unlifted arrays.
5 module Data.Array.Parallel.Unlifted.Parallel.Basics (
6 lengthUP, nullUP, indexedUP,
7 replicateUP, repeatUP, interleaveUP
8 ) where
9
10 import Data.Array.Parallel.Unlifted.Sequential.Vector as Seq
11 import Data.Array.Parallel.Unlifted.Distributed
12 import Data.Array.Parallel.Unlifted.Parallel.Combinators ( mapUP )
13 import Data.Array.Parallel.Unlifted.Parallel.Enum ( enumFromToUP )
14 import Data.Array.Parallel.Unlifted.Parallel.Permute ( bpermuteUP )
15
16 import GHC.Base ( remInt )
17
18 -- NOTE: some of the functions are exactly the same as the U version
19
20 -- | Test whether the given array is empty
21 nullUP :: Unbox e => Vector e -> Bool
22 nullUP = (== 0) . Seq.length
23
24 -- | Yield an empty array
25 emptyUP :: Unbox e => Vector e
26 emptyUP = Seq.new 0 (const $ return ())
27
28 lengthUP :: Unbox e => Vector e -> Int
29 lengthUP = Seq.length
30
31
32 -- | Yield an array where all elements contain the same value
33 replicateUP :: Unbox e => Int -> e -> Vector e
34 {-# INLINE_UP replicateUP #-}
35 replicateUP n !e = joinD theGang balanced
36 . mapD theGang (\n ->Seq.replicate n e)
37 $ splitLenD theGang n
38
39
40 -- | Repeat an array the given number of times.
41 repeatUP :: Unbox e => Int -> Vector e -> Vector e
42 {-# INLINE_UP repeatUP #-}
43 repeatUP n es = seq m
44 . bpermuteUP es
45 . mapUP (\i -> i `remInt` m)
46 $ enumFromToUP 0 (m*n-1)
47 where
48 m = Seq.length es
49
50 -- | Interleave elements of two arrays
51 interleaveUP :: Unbox e => Vector e -> Vector e -> Vector e
52 {-# INLINE_UP interleaveUP #-}
53 interleaveUP xs ys = joinD theGang unbalanced
54 (zipWithD theGang Seq.interleave
55 (splitD theGang balanced xs)
56 (splitD theGang balanced ys))
57
58 -- | Associate each element of the array with its index
59 indexedUP :: (DT e, Unbox e) => Vector e -> Vector (Int,e)
60 {-# INLINE_U indexedUP #-}
61 indexedUP = splitJoinD theGang indexedFn
62 where
63 sizes arr = fst $ scanD theGang (+) 0 $ lengthD arr
64 indexedFn = \arr -> (zipWithD theGang (\o -> Seq.map (\(x,y) -> (x + o, y))) (sizes arr) $
65 mapD theGang Seq.indexed arr)
66
67