#include "fusion-phases.h"
module Data.Array.Parallel.Unlifted.Sequential.Segmented.Combinators (
mapSU, zipWithSU,
foldlSU, foldSU, foldl1SU, fold1SU,
combineSU, combineCU,
filterSU, packCU
) where
import Data.Array.Parallel.Base (
sndS)
import Data.Array.Parallel.Stream (
Stream, SStream, mapS, foldValuesSS, fold1ValuesSS, combineSS)
import Data.Array.Parallel.Unlifted.Sequential.Flat (
UA, UArr, mapU, zipWithU,
unstreamU, streamU)
import Data.Array.Parallel.Unlifted.Sequential.Segmented.SUArr (
SUArr, segdSU, flattenSU, (>:), lengthsSU)
import Data.Array.Parallel.Unlifted.Sequential.Segmented.Basics (
concatSU, segmentArrU,segmentU, replicateSU)
import Data.Array.Parallel.Unlifted.Sequential.Segmented.Stream (
streamSU)
import Data.Array.Parallel.Unlifted.Sequential.Flat.Combinators (
filterU, combineU, packU)
import Debug.Trace
mapSU :: (UA a, UA b) => (a -> b) -> SUArr a -> SUArr b
mapSU f sa = segdSU sa >: mapU f (concatSU sa)
zipWithSU :: (UA a, UA b, UA c)
=> (a -> b -> c) -> SUArr a -> SUArr b -> SUArr c
zipWithSU f sa sb = segdSU sa >: zipWithU f (concatSU sa) (concatSU sb)
foldlSU :: (UA a, UA b) => (b -> a -> b) -> b -> SUArr a -> UArr b
foldlSU f z = unstreamU . foldValuesSS f z . streamSU
foldSU :: UA a => (a -> a -> a) -> a -> SUArr a -> UArr a
foldSU = foldlSU
foldl1SU :: UA a => (a -> a -> a) -> SUArr a -> UArr a
foldl1SU f = unstreamU . fold1ValuesSS f . streamSU
fold1SU :: UA a => (a -> a -> a) -> SUArr a -> UArr a
fold1SU = foldl1SU
combineSU:: UA a => UArr Bool -> SUArr a -> SUArr a -> UArr a
combineSU fs xs1 xs2 =
unstreamU $ combineSS (streamU fs) (streamSU xs1) (streamSU xs2)
combineCU:: UA e => UArr Bool -> SUArr e -> SUArr e -> SUArr e
combineCU flags xssArr1 xssArr2 = trace "combineCU"
segmentArrU newLengths flatData
where
newLengths = combineU flags (lengthsSU xssArr1) (lengthsSU xssArr2)
repFlags = replicateSU newLengths flags
flatData = combineU (flattenSU repFlags) (flattenSU xssArr1) (flattenSU xssArr2)
filterSU:: (UA e) => (e -> Bool) -> SUArr e -> SUArr e
filterSU p xssArr = segmentArrU newLengths flatData
where
flatData = filterU p $ flattenSU xssArr
segdFlags = segmentU xssArr $ mapU (\x -> if p x then 1 else 0) $ flattenSU xssArr
newLengths = foldSU (+) 0 segdFlags
packCU:: (UA e) => UArr Bool -> SUArr e -> SUArr e
packCU flags xssArr = segmentArrU newLengths flatData
where
repFlags = flattenSU $ replicateSU (lengthsSU xssArr) flags
flatData = packU (flattenSU xssArr) repFlags
newLengths = packU (lengthsSU xssArr) flags