module Data.Array.Parallel.Unlifted.Parallel.Sums (
andUP, orUP, sumUP
) where
import Data.Array.Parallel.Unlifted.Sequential
import Data.Array.Parallel.Unlifted.Distributed
import Data.Array.Parallel.Unlifted.Parallel.Combinators (
foldUP, foldl1UP, fold1UP, mapUP)
import Data.Array.Parallel.Unlifted.Parallel.Basics (
indexedUP)
import Data.Array.Parallel.Base (
(:*:)(..), fstS)
andUP :: UArr Bool -> Bool
andUP = foldUP (&&) True
orUP :: UArr Bool -> Bool
orUP = foldUP (||) False
allUP :: UA e => (e -> Bool) -> UArr e -> Bool
allUP p = andUP . mapUP p
anyUP :: UA e => (e -> Bool) -> UArr e -> Bool
anyUP p = orUP . mapUP p
sumUP :: (UA a, DT a, Num a) => UArr a -> a
sumUP = foldUP (+) 0
productUP :: (DT e, Num e, UA e) => UArr e -> e
productUP = foldUP (*) 1
maximumUP :: (DT e, Ord e, UA e) => UArr e -> e
maximumUP = fold1UP max
maximumByUP :: (DT e, UA e) => (e -> e -> Ordering) -> UArr e -> e
maximumByUP = fold1UP . maxBy
where
maxBy compare x y = case x `compare` y of
LT -> y
_ -> x
maximumIndexByUP :: (DT e, UA e) => (e -> e -> Ordering) -> UArr e -> Int
maximumIndexByUP cmp = fstS . maximumByUP cmp' . indexedUP
where
cmp' (_ :*: x) (_ :*: y) = cmp x y