06b13d600538992bf169980ebcc48b0c19d8b156
1 -- | Sum-like parallel combinators for unlifted arrays
2 module Data.Array.Parallel.Unlifted.Parallel.Sums (
3 andUP, orUP, sumUP
4 ) where
6 import Data.Array.Parallel.Unlifted.Sequential.Vector as Seq
7 import Data.Array.Parallel.Unlifted.Distributed
8 import Data.Array.Parallel.Unlifted.Parallel.Combinators (
9 foldUP, foldl1UP, fold1UP, mapUP)
10 import Data.Array.Parallel.Unlifted.Parallel.Basics (
11 indexedUP)
13 -- | Compute the logical AND of all the elements in a array.
14 andUP :: Vector Bool -> Bool
15 {-# INLINE andUP #-}
16 andUP = foldUP (&&) True
19 -- | Compute the logical OR of all the elements in a array.
20 orUP :: Vector Bool -> Bool
21 {-# INLINE orUP #-}
22 orUP = foldUP (||) False
24 -- | Check whether all the elements in a array meet the given predicate.
25 allUP :: Unbox e => (e -> Bool) -> Vector e -> Bool
26 {-# INLINE allUP #-}
27 allUP p = andUP . mapUP p
29 -- | Check whether any of the elements in a array meet the given predicate.
30 anyUP :: Unbox e => (e -> Bool) -> Vector e -> Bool
31 {-# INLINE anyUP #-}
32 anyUP p = orUP . mapUP p
35 -- | Compute the sum all the elements of a array.
36 sumUP :: (Unbox a, DT a, Num a) => Vector a -> a
37 {-# INLINE sumUP #-}
38 sumUP = foldUP (+) 0
41 -- | Compute the product of all the elements of an array.
42 productUP :: (DT e, Num e, Unbox e) => Vector e -> e
43 {-# INLINE productUP #-}
44 productUP = foldUP (*) 1
47 -- | Determine the maximum element in an array.
48 maximumUP :: (DT e, Ord e, Unbox e) => Vector e -> e
49 {-# INLINE maximumUP #-}
50 maximumUP = fold1UP max
53 -- |Determine the maximum element in an array under the given ordering
54 maximumByUP :: (DT e, Unbox e) => (e -> e -> Ordering) -> Vector e -> e
55 {-# INLINE maximumByUP #-}
56 maximumByUP = fold1UP . maxBy
57 where
58 maxBy compare x y = case x `compare` y of
59 LT -> y
60 _ -> x
62 -- | Determine the index of the maximum element in an array under the given
63 -- ordering
64 maximumIndexByUP :: (DT e, Unbox e) => (e -> e -> Ordering) -> Vector e -> Int
65 {-# INLINE maximumIndexByUP #-}
66 maximumIndexByUP cmp = fst . maximumByUP cmp' . indexedUP
67 where
68 cmp' (_,x) (_,y) = cmp x y