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