#include "fusion-phases.h"
module Data.Array.Parallel.Unlifted.Parallel.Combinators
( mapUP
, filterUP
, packUP
, combineUP, combine2UP
, zipWithUP
, foldUP, foldlUP, fold1UP, foldl1UP
, scanUP)
where
import Data.Array.Parallel.Base
import Data.Array.Parallel.Unlifted.Distributed
import Data.Array.Parallel.Unlifted.Distributed.What
import Data.Array.Parallel.Unlifted.Parallel.UPSel
import Data.Array.Parallel.Unlifted.Sequential.Vector as Seq
here :: String -> String
here s = "Data.Array.Parallel.Unlifted.Parallel.Combinators." Prelude.++ s
mapUP :: (Unbox a, Unbox b) => (a -> b) -> Vector a -> Vector b
mapUP f xs
= splitJoinD theGang
(mapD (What "mapUP/map") theGang (Seq.map f)) xs
filterUP :: Unbox a => (a -> Bool) -> Vector a -> Vector a
filterUP f
= joinD theGang unbalanced
. mapD (What "filterUP/filter") theGang (Seq.filter f)
. splitD theGang unbalanced
packUP :: Unbox e => Vector e -> Vector Bool -> Vector e
packUP xs flags
= Seq.fsts . filterUP snd $ Seq.zip xs flags
combineUP :: Unbox a => Vector Bool -> Vector a -> Vector a -> Vector a
combineUP flags xs ys
= checkEq (here "combineUP")
("tags length /= sum of args length")
(Seq.length flags) (Seq.length xs + Seq.length ys)
$ combine2UP tags (mkUPSelRep2 tags) xs ys
where tags = Seq.map (fromBool . not) flags
combine2UP :: Unbox a => Vector Tag -> UPSelRep2 -> Vector a -> Vector a -> Vector a
combine2UP tags rep !xs !ys
= checkEq (here "combine2UP")
("tags length /= sum of args length")
(Seq.length tags) (Seq.length xs + Seq.length ys)
$ joinD theGang balanced
$ zipWithD (What "combine2UP/go") theGang go rep
$ splitD theGang balanced tags
where go ((i,j), (m,n)) ts
= Seq.combine2ByTag ts
(Seq.slice (here "combine2UP") xs i m)
(Seq.slice (here "combine2UP") ys j n)
zipWithUP :: (Unbox a, Unbox b, Unbox c)
=> (a -> b -> c) -> Vector a -> Vector b -> Vector c
zipWithUP f xs ys
= splitJoinD theGang
(mapD (What "zipWithUP/map") theGang (Seq.map (uncurry f)))
(Seq.zip xs ys)
foldUP :: (Unbox a, DT a) => (a -> a -> a) -> a -> Vector a -> a
foldUP f !z xs
= foldD (What "foldUP/f") theGang f
$ mapD (What "foldUP/fold") theGang (Seq.fold f z)
$ splitD theGang unbalanced xs
foldlUP :: (DT a, Unbox a) => (a -> a -> a) -> a -> Vector a -> a
foldlUP f z arr
| Seq.null arr = z
| otherwise = foldl1UP f arr
fold1UP :: (DT a, Unbox a) => (a -> a -> a) -> Vector a -> a
fold1UP = foldl1UP
foldl1UP :: (DT a, Unbox a) => (a -> a -> a) -> Vector a -> a
foldl1UP f arr
= (maybe z (f z)
. foldD (What "fold1UP/foldD") theGang combine'
. mapD (What "fold1UP/fold1Maybe") theGang (Seq.foldl1Maybe f)
. splitD theGang unbalanced) arr
where
z = Seq.index (here "fold1UP") arr 0
combine' (Just x) (Just y) = Just (f x y)
combine' (Just x) Nothing = Just x
combine' Nothing (Just y) = Just y
combine' Nothing Nothing = Nothing
scanUP :: (DT a, Unbox a) => (a -> a -> a) -> a -> Vector a -> Vector a
scanUP f z
= splitJoinD theGang go
where go xs
= let (ds,zs) = unzipD
$ mapD (What "scanUP/scanRes") theGang (Seq.scanRes f z) xs
zs' = fst
$ scanD (What "scanUP/scan") theGang f z zs
in zipWithD (What "scanUP/map") theGang (Seq.map . f) zs' ds