1 {-# LANGUAGE CPP #-}
2 #include "fusion-phases.h"
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
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 = unstream
21 \$ foldSS f z (stream (lengthsUSegd segd)) (stream xs)
24 -- | Segmented array reduction that requires an associative combination
25 -- function with its unit
26 foldSU :: Unbox a => (a -> a -> a) -> a -> USegd -> Vector a -> Vector a
27 foldSU = foldlSU
30 -- | Segmented array reduction from left to right with non-empty subarrays only
31 foldl1SU :: Unbox a => (a -> a -> a) -> USegd -> Vector a -> Vector a
32 {-# INLINE_U foldl1SU #-}
33 foldl1SU f segd xs = unstream
34 \$ fold1SS f (stream (lengthsUSegd segd)) (stream xs)
37 -- | Segmented array reduction with non-empty subarrays and an associative
38 -- combination function
39 fold1SU :: Unbox a => (a -> a -> a) -> USegd -> Vector a -> Vector a
40 fold1SU = foldl1SU
43 -- | Merge two segmented arrays according to flag array
44 combineSU :: Unbox a => Vector Bool -> USegd -> Vector a -> USegd -> Vector a -> Vector a
45 {-# INLINE_U combineSU #-}
46 combineSU bs xd xs yd ys = unstream
47 \$ combineSS (stream bs)
48 (stream (lengthsUSegd xd)) (stream xs)
49 (stream (lengthsUSegd yd)) (stream ys)
52 -- | Regular arrar reduction
53 foldlRU :: (Unbox a, Unbox b) => (b -> a -> b) -> b -> Int -> Vector a -> Vector b
54 {-# INLINE_U foldlRU #-}
55 foldlRU f z segSize = unstream . foldValuesR f z segSize . stream