#include "fusion-phases.h"
module Data.Array.Parallel.Unlifted.Parallel.Basics (
lengthUP, nullUP, indexedUP,
replicateUP, repeatUP
) where
import Data.Array.Parallel.Base (
(:*:)(..), fstS, sndS, uncurryS)
import Data.Array.Parallel.Unlifted.Sequential (
UA, UArr, (!:), unitsU, lengthU, newU,
foldU, mapU, zipU, unzipU,
indexedU, enumFromToU, replicateU)
import Data.Array.Parallel.Unlifted.Distributed
import Data.Array.Parallel.Unlifted.Parallel.Combinators ( mapUP )
import Data.Array.Parallel.Unlifted.Parallel.Enum ( enumFromToUP )
import Data.Array.Parallel.Unlifted.Parallel.Permute ( bpermuteUP )
nullUP :: UA e => UArr e -> Bool
nullUP = (== 0) . lengthU
emptyUP :: UA e => UArr e
emptyUP = newU 0 (const $ return ())
lengthUP :: UA e => UArr e -> Int
lengthUP = lengthU
replicateUP :: UA e => Int -> e -> UArr e
replicateUP n e = joinD theGang balanced
. mapD theGang (\n ->replicateU n e)
$ splitLenD theGang n
repeatUP :: UA e => Int -> UArr e -> UArr e
repeatUP n es = seq m
. bpermuteUP es
. mapUP (\i -> i `mod` m)
$ enumFromToUP 0 (m*n1)
where
m = lengthU es
indexedUP :: (DT e, UA e) => UArr e -> UArr (Int :*: e)
indexedUP = splitJoinD theGang indexedFn
where
sizes arr = fstS $ scanD theGang (+) 0 $ lengthD arr
indexedFn = \arr -> (zipWithD theGang (\o -> mapU (\xy -> (fstS xy + o :*: sndS xy))) (sizes arr) $
mapD theGang indexedU arr)