dph-prim-par/seq: comments and formatting only
[packages/dph.git] / dph-prim-seq / Data / Array / Parallel / Unlifted / Sequential / Segmented / Combinators.hs
1 {-# LANGUAGE CPP #-}
2 #include "fusion-phases.h"
3
4 -- | Standard combinators for segmented unlifted arrays.
5 module Data.Array.Parallel.Unlifted.Sequential.Segmented.Combinators (
6 foldlSU, foldSU, foldl1SU, fold1SU, {-scanSU,-} {-scan1SU,-}
7 foldlRU,
8 combineSU
9 ) where
10 import Data.Array.Parallel.Stream (
11 foldSS, fold1SS, combineSS, foldValuesR )
12 import Data.Array.Parallel.Unlifted.Sequential.Vector as V
13 import Data.Array.Parallel.Unlifted.Sequential.Segmented.USegd
14 import Debug.Trace
15
16
17 -- | Segmented array reduction proceeding from the left
18 foldlSU :: (Unbox a, Unbox b) => (b -> a -> b) -> b -> USegd -> Vector a -> Vector b
19 {-# INLINE_U foldlSU #-}
20 foldlSU f z segd xs
21 = unstream
22 $ foldSS f z (stream (lengthsUSegd segd)) (stream xs)
23
24
25 -- | Segmented array reduction that requires an associative combination
26 -- function with its unit
27 foldSU :: Unbox a => (a -> a -> a) -> a -> USegd -> Vector a -> Vector a
28 foldSU = foldlSU
29
30
31 -- | Segmented array reduction from left to right with non-empty subarrays only
32 foldl1SU :: Unbox a => (a -> a -> a) -> USegd -> Vector a -> Vector a
33 {-# INLINE_U foldl1SU #-}
34 foldl1SU f segd xs
35 = unstream
36 $ fold1SS f (stream (lengthsUSegd segd)) (stream xs)
37
38
39 -- | Segmented array reduction with non-empty subarrays and an associative
40 -- combination function
41 fold1SU :: Unbox a => (a -> a -> a) -> USegd -> Vector a -> Vector a
42 fold1SU = foldl1SU
43
44
45 -- | Merge two segmented arrays according to flag array
46 combineSU :: Unbox a => Vector Bool -> USegd -> Vector a -> USegd -> Vector a -> Vector a
47 {-# INLINE_U combineSU #-}
48 combineSU bs xd xs yd ys
49 = unstream
50 $ combineSS (stream bs)
51 (stream (lengthsUSegd xd)) (stream xs)
52 (stream (lengthsUSegd yd)) (stream ys)
53
54
55 -- | Regular arrar reduction
56 foldlRU :: (Unbox a, Unbox b) => (b -> a -> b) -> b -> Int -> Vector a -> Vector b
57 {-# INLINE_U foldlRU #-}
58 foldlRU f z segSize
59 = unstream . foldValuesR f z segSize . stream
60