#include "fusion-phases.h"
module Data.Array.Parallel.Unlifted.Sequential.Flat.Combinators (
mapU,
filterU,
packU,
foldlU, foldl1U, foldl1MaybeU,
foldU, fold1U, fold1MaybeU,
scanlU, scanl1U, scanU, scan1U,
scanResU,
mapAccumLU,
zipU, zip3U, unzipU, unzip3U, fstU, sndU,
zipWithU, zipWith3U,
combineU
) where
import Data.Array.Parallel.Base (
(:*:)(..), MaybeS(..), checkNotEmpty, checkEq, sndS, Rebox(..), ST, runST)
import Data.Array.Parallel.Stream (
Step(..), Stream(..),
mapS, filterS, foldS, fold1MaybeS, scan1S, scanS, mapAccumS,
zipWithS, zipWith3S, combineS)
import Data.Array.Parallel.Unlifted.Sequential.Flat.UArr (
UA, UArr, MUArr,
writeMU, newDynResU,
zipU, unzipU, fstU, sndU)
import Data.Array.Parallel.Unlifted.Sequential.Flat.Stream (
streamU, unstreamU)
import Data.Array.Parallel.Unlifted.Sequential.Flat.Basics (
lengthU, (!:))
import Data.Array.Parallel.Unlifted.Sequential.Flat.Subarrays (
sliceU)
import Debug.Trace
here s = "Data.Array.Parallel.Unlifted.Sequential.Flat.Combinators." ++ s
mapU :: (UA e, UA e') => (e -> e') -> UArr e -> UArr e'
mapU f = unstreamU . mapS f . streamU
filterU :: UA e => (e -> Bool) -> UArr e -> UArr e
filterU p = unstreamU . filterS p . streamU
packU:: UA e => UArr e -> UArr Bool -> UArr e
packU xs = fstU . filterU sndS . zipU xs
foldlU :: UA a => (b -> a -> b) -> b -> UArr a -> b
foldlU f z xs = foldS f z (streamU xs)
foldl1U :: UA a => (a -> a -> a) -> UArr a -> a
foldl1U f arr = checkNotEmpty (here "foldl1U") (lengthU arr) $
foldlU f (arr !: 0) (sliceU arr 1 (lengthU arr 1))
foldl1MaybeU :: UA a => (a -> a -> a) -> UArr a -> MaybeS a
foldl1MaybeU f = fold1MaybeS f . streamU
foldU :: UA a => (a -> a -> a) -> a -> UArr a -> a
foldU = foldlU
fold1MaybeU :: UA a => (a -> a -> a) -> UArr a -> MaybeS a
fold1MaybeU = foldl1MaybeU
fold1U :: UA a => (a -> a -> a) -> UArr a -> a
fold1U = foldl1U
scanlU :: (UA a, UA b) => (b -> a -> b) -> b -> UArr a -> UArr b
scanlU f z = unstreamU . scanS f z . streamU
scanl1U :: UA a => (a -> a -> a) -> UArr a -> UArr a
scanl1U f arr = checkNotEmpty (here "scanl1U") (lengthU arr) $
unstreamU (scan1S f (streamU arr))
scanU :: UA a => (a -> a -> a) -> a -> UArr a -> UArr a
scanU = scanlU
scan1U :: UA a => (a -> a -> a) -> UArr a -> UArr a
scan1U = scanl1U
scanResU :: UA a => (a -> a -> a) -> a -> UArr a -> UArr a :*: a
scanResU f z = unstreamScan f z . streamU
unstreamScan :: UA a => (a -> a -> a) -> a -> Stream a -> UArr a :*: a
unstreamScan f z st@(Stream _ _ n)
= newDynResU n (\marr -> unstreamScanM marr f z st)
unstreamScanM :: UA a => MUArr a s -> (a -> a -> a) -> a -> Stream a
-> ST s (Int :*: a)
unstreamScanM marr f z (Stream next s n) = fill s z 0
where
fill s !z !i = case next s of
Done -> return (i :*: z)
Skip s' -> s' `dseq` fill s' z i
Yield x s' -> s' `dseq`
do
writeMU marr i z
fill s' (f z x) (i+1)
mapAccumLU :: (UA a, UA b) => (c -> a -> c :*: b) -> c -> UArr a -> UArr b
mapAccumLU f z = unstreamU . mapAccumS f z . streamU
zip3U :: (UA e1, UA e2, UA e3)
=> UArr e1 -> UArr e2 -> UArr e3 -> UArr (e1 :*: e2 :*: e3)
zip3U a1 a2 a3 = (a1 `zipU` a2) `zipU` a3
zipWithU :: (UA a, UA b, UA c)
=> (a -> b -> c) -> UArr a -> UArr b -> UArr c
zipWithU f a1 a2 = unstreamU (zipWithS f (streamU a1) (streamU a2))
zipWith3U :: (UA a, UA b, UA c, UA d)
=> (a -> b -> c -> d) -> UArr a -> UArr b -> UArr c -> UArr d
zipWith3U f a1 a2 a3 = unstreamU (zipWith3S f (streamU a1)
(streamU a2)
(streamU a3))
unzip3U :: (UA e1, UA e2, UA e3)
=> UArr (e1 :*: e2 :*: e3) -> (UArr e1 :*: UArr e2 :*: UArr e3)
unzip3U a = let (a12 :*: a3) = unzipU a
(a1 :*: a2) = unzipU a12
in
(a1 :*: a2 :*: a3)
combineU :: UA a
=> UArr Bool -> UArr a -> UArr a -> UArr a
combineU f a1 a2 = checkEq (here "combineU")
("flag length not equal to sum of arg length")
(lengthU f) (lengthU a1 + lengthU a2) $
unstreamU (combineS (streamU f) (streamU a1) (streamU a2))