ac77457966162b8727be0af6e75b53a1276c3ab7
[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 ( emptyUP
7 , replicateUP
8 , repeatUP
9 , lengthUP
10 , nullUP
11 , interleaveUP
12 , indexedUP)
13 where
14 import Data.Array.Parallel.Unlifted.Sequential.Vector as Seq
15 import Data.Array.Parallel.Unlifted.Distributed
16 import Data.Array.Parallel.Unlifted.Parallel.Combinators (mapUP)
17 import Data.Array.Parallel.Unlifted.Parallel.Enum (enumFromToUP)
18 import Data.Array.Parallel.Unlifted.Parallel.Permute (bpermuteUP)
19 import GHC.Base (remInt)
20
21
22 -- | O(1). Construct an empty array.
23 emptyUP :: Unbox e => Vector e
24 emptyUP = Seq.new 0 (const $ return ())
25 {-# INLINE_UP emptyUP #-}
26
27
28 -- | Yield an array where all elements contain the same value
29 replicateUP :: Unbox e => Int -> e -> Vector e
30 replicateUP n !e
31 = joinD theGang balanced
32 . mapD theGang (\n' ->Seq.replicate n' e)
33 $ splitLenD theGang n
34 {-# INLINE_UP replicateUP #-}
35
36
37 -- | Repeat an array the given number of times.
38 repeatUP :: Unbox e => Int -> Vector e -> Vector e
39 repeatUP n es
40 = seq m
41 . bpermuteUP es
42 . mapUP (\i -> i `remInt` m)
43 $ enumFromToUP 0 (m*n-1)
44 where
45 m = Seq.length es
46 {-# INLINE_UP repeatUP #-}
47
48
49 -- | O(1). Take the length of an array.
50 lengthUP :: Unbox e => Vector e -> Int
51 lengthUP = Seq.length
52 {-# INLINE_UP lengthUP #-}
53
54
55 -- | O(1). Test whether the given array is empty
56 nullUP :: Unbox e => Vector e -> Bool
57 nullUP = (== 0) . Seq.length
58 {-# INLINE_UP nullUP #-}
59
60
61 -- | Interleave elements of two arrays
62 interleaveUP :: Unbox e => Vector e -> Vector e -> Vector e
63 interleaveUP xs ys
64 = joinD theGang unbalanced
65 $ zipWithD theGang Seq.interleave
66 (splitD theGang balanced xs)
67 (splitD theGang balanced ys)
68 {-# INLINE_UP interleaveUP #-}
69
70
71 -- | Associate each element of the array with its index
72 indexedUP :: (DT e, Unbox e) => Vector e -> Vector (Int,e)
73 indexedUP
74 = splitJoinD theGang indexedFn
75 where
76 sizes arr = fst $ scanD theGang (+) 0 $ lengthD arr
77 indexedFn = \arr -> zipWithD theGang
78 (\o -> Seq.map (\(x,y) -> (x + o, y)))
79 (sizes arr)
80 $ mapD theGang Seq.indexed arr
81 {-# INLINE_UP indexedUP #-}