dph-prim-par/seq: comments and formatting only
[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
25 -- | Yield an empty array
26 emptyUP :: Unbox e => Vector e
27 emptyUP = Seq.new 0 (const $ return ())
28
29 lengthUP :: Unbox e => Vector e -> Int
30 lengthUP = Seq.length
31
32
33 -- | Yield an array where all elements contain the same value
34 replicateUP :: Unbox e => Int -> e -> Vector e
35 {-# INLINE_UP replicateUP #-}
36 replicateUP n !e
37 = joinD theGang balanced
38 . mapD theGang (\n ->Seq.replicate n e)
39 $ splitLenD theGang n
40
41
42 -- | Repeat an array the given number of times.
43 repeatUP :: Unbox e => Int -> Vector e -> Vector e
44 {-# INLINE_UP repeatUP #-}
45 repeatUP n es
46 = seq m
47 . bpermuteUP es
48 . mapUP (\i -> i `remInt` m)
49 $ enumFromToUP 0 (m*n-1)
50 where
51 m = Seq.length es
52
53
54 -- | Interleave elements of two arrays
55 interleaveUP :: Unbox e => Vector e -> Vector e -> Vector e
56 {-# INLINE_UP interleaveUP #-}
57 interleaveUP xs ys
58 = joinD theGang unbalanced
59 $ zipWithD theGang Seq.interleave
60 (splitD theGang balanced xs)
61 (splitD theGang balanced ys)
62
63
64 -- | Associate each element of the array with its index
65 indexedUP :: (DT e, Unbox e) => Vector e -> Vector (Int,e)
66 {-# INLINE_U indexedUP #-}
67 indexedUP
68 = splitJoinD theGang indexedFn
69 where
70 sizes arr = fst $ scanD theGang (+) 0 $ lengthD arr
71 indexedFn = \arr -> zipWithD theGang (\o -> Seq.map (\(x,y) -> (x + o, y))) (sizes arr)
72 $ mapD theGang Seq.indexed arr
73
74