#include "fusion-phases.h"
module Data.Array.Parallel.PArray.PData.Sum2
( PData(..)
, PDatas(..)
, Sels2, lengthSels2)
where
import Data.Array.Parallel.PArray.PData.Int ()
import Data.Array.Parallel.PArray.PData.Base
import Data.Array.Parallel.PArray.PData.Nested
import Data.Array.Parallel.PArray.Types
import Data.Array.Parallel.Base (intToTag)
import Data.Array.Parallel.Unlifted as U
import qualified Data.Vector as V
import qualified Data.Typeable as T
import Text.PrettyPrint
import Prelude as P
import Data.Array.Parallel.Pretty
data instance PData (Sum2 a b)
= PSum2 U.Sel2
(PData a)
(PData b)
data instance PDatas (Sum2 a b)
= PSum2s Sels2
(PDatas a)
(PDatas b)
type Sels2
= V.Vector U.Sel2
lengthSels2 :: Sels2 -> Int
lengthSels2 sels2
= V.length sels2
instance (PR a, PR b) => PR (Sum2 a b) where
validPR _
= True
similarPR x y
= case (x, y) of
(Alt2_1 x', Alt2_1 y') -> similarPR x' y'
(Alt2_2 x', Alt2_2 y') -> similarPR x' y'
_ -> False
nfPR (PSum2 sel xs ys)
= sel `seq` nfPR xs `seq` nfPR ys `seq` ()
coversPR weak (PSum2 sel _ _) ix
| weak = ix <= U.length (U.tagsSel2 sel)
| otherwise = ix < U.length (U.tagsSel2 sel)
pprpPR xx
= case xx of
Alt2_1 x -> text "Alt2_1" <+> parens (pprpPR x)
Alt2_2 y -> text "Alt2_2" <+> parens (pprpPR y)
pprpDataPR (PSum2 sel pdatas1 pdatas2)
= text "PSum2"
$+$ (nest 4 $ vcat
[ pprp sel
, text "ALTS0: " <+> pprp pdatas1
, text "ALTS1: " <+> pprp pdatas2])
typeRepPR ss
= case ss of
Alt2_1 x -> T.typeOf2 ss `T.mkAppTy` typeRepPR x `T.mkAppTy` typeRepPR x
Alt2_2 y -> T.typeOf2 ss `T.mkAppTy` typeRepPR y `T.mkAppTy` typeRepPR y
typeRepDataPR (PSum2 _ xs ys)
= T.typeOf2 (Alt2_1 ())
`T.mkAppTy` typeRepDataPR xs
`T.mkAppTy` typeRepDataPR ys
typeRepDatasPR (PSum2s _ pdatas1 pdatas2)
= T.typeOf2 (Alt2_1 ())
`T.mkAppTy` typeRepDatasPR pdatas1
`T.mkAppTy` typeRepDatasPR pdatas2
emptyPR
= PSum2 (U.mkSel2 U.empty U.empty 0 0 (U.mkSelRep2 U.empty)) emptyPR emptyPR
replicatePR n aa
= case aa of
Alt2_1 x
-> PSum2 (U.mkSel2 (U.replicate n 0)
(U.enumFromStepLen 0 1 n)
n 0
(U.mkSelRep2 (U.replicate n 0)))
(replicatePR n x)
emptyPR
Alt2_2 x
-> PSum2 (U.mkSel2 (U.replicate n 1)
(U.enumFromStepLen 0 1 n)
0 n
(U.mkSelRep2 (U.replicate n 1)))
emptyPR
(replicatePR n x)
replicatesPR segd (PSum2 sel as bs)
= let tags = U.tagsSel2 sel
tags' = U.replicate_s segd tags
sel' = U.tagsToSel2 tags'
lens = U.lengthsSegd segd
asegd = U.lengthsToSegd (U.packByTag lens tags 0)
bsegd = U.lengthsToSegd (U.packByTag lens tags 1)
as' = replicatesPR asegd as
bs' = replicatesPR bsegd bs
in PSum2 sel' as' bs'
appendPR (PSum2 sel1 as1 bs1)
(PSum2 sel2 as2 bs2)
= let !sel = U.tagsToSel2 $ U.tagsSel2 sel1 U.+:+ U.tagsSel2 sel2
as = appendPR as1 as2
bs = appendPR bs1 bs2
in PSum2 sel as bs
lengthPR (PSum2 sel _ _)
= U.length $ tagsSel2 sel
indexPR (PSum2 sel as bs) i
= let !k = U.index "indexPR[Sum2]" (U.indicesSel2 sel) i
in case U.index "indexPR[Sum2]" (U.tagsSel2 sel) i of
0 -> Alt2_1 (indexPR as k)
_ -> Alt2_2 (indexPR bs k)
indexsPR (PSum2s sels ass bss) srcixs
= let (srcids, ixs) = U.unzip srcixs
getFlagIndex !src !ix
= let !sel = V.unsafeIndex sels src
!elemFlag = U.index "indexPR[Sum2]" (U.tagsSel2 sel) ix
!elemIndex = U.index "indexPR[Sum2]" (U.indicesSel2 sel) ix
in (elemFlag, elemIndex)
(flags', indices')
= U.unzip $ U.zipWith getFlagIndex srcids ixs
sel' = U.tagsToSel2 flags'
asIndices = U.packByTag indices' flags' 0
bsIndices = U.packByTag indices' flags' 1
as' = indexsPR ass (U.zip srcids asIndices)
bs' = indexsPR bss (U.zip srcids bsIndices)
in PSum2 sel' as' bs'
extractPR (PSum2 sel as bs) start len
= let
tags' = U.extract (U.tagsSel2 sel) start len
sel' = U.tagsToSel2 tags'
indices' = U.extract (U.indicesSel2 sel) start len
indices0 = U.packByTag indices' tags' 0
indices1 = U.packByTag indices' tags' 1
as' = bpermutePR as indices0
bs' = bpermutePR bs indices1
in PSum2 sel' as' bs'
extractssPR (PSum2s sels pdatas0 pdatas1) ssegd
= let
tagss = V.map U.tagsSel2 sels
tags' = U.extracts_nss ssegd tagss
sel' = U.tagsToSel2 tags'
indices' = U.extracts_nss ssegd (V.map U.indicesSel2 sels)
lens0 = U.count_ss ssegd tagss 0
indices0 = U.scan (+) 0 lens0
lens1 = U.count_ss ssegd tagss 1
indices1 = U.scan (+) 0 lens1
sel0 = U.packByTag indices' tags' 0
sel0_len = U.length sel0
starts0 = U.map (\i -> if i >= sel0_len
then 0
else U.index "extractssPR[Sum2]" sel0 i)
indices0
sel1 = U.packByTag indices' tags' 1
sel1_len = U.length sel1
starts1 = U.map (\i -> if i >= sel1_len
then 0
else U.index "extractssPR[Sum2]" sel1 i)
indices1
pdata0 = extractssPR pdatas0
$ U.mkSSegd starts0
(U.sourcesOfSSegd ssegd)
(U.lengthsToSegd lens0)
pdata1 = extractssPR pdatas1
$ U.mkSSegd starts1
(U.sourcesOfSSegd ssegd)
(U.lengthsToSegd lens1)
in
PSum2 sel' pdata0 pdata1
extractvsPR pdatas vsegd
= extractssPR pdatas (unsafeDemoteToSSegdOfVSegd vsegd)
packByTagPR (PSum2 sel as bs) tags tag
= let flags = U.tagsSel2 sel
flags' = U.packByTag flags tags (intToTag tag)
sel' = U.tagsToSel2 flags'
atags = U.packByTag tags flags 0
btags = U.packByTag tags flags 1
as' = packByTagPR as atags tag
bs' = packByTagPR bs btags tag
in PSum2 sel' as' bs'
combine2PR sel (PSum2 sel1 as1 bs1) (PSum2 sel2 as2 bs2)
= let tags = U.tagsSel2 sel
tags' = U.combine2 (U.tagsSel2 sel) (U.repSel2 sel)
(U.tagsSel2 sel1) (U.tagsSel2 sel2)
sel' = U.tagsToSel2 tags'
asel = U.tagsToSel2 (U.packByTag tags tags' 0)
bsel = U.tagsToSel2 (U.packByTag tags tags' 1)
as = combine2PR asel as1 as2
bs = combine2PR bsel bs1 bs2
in PSum2 sel' as bs
fromVectorPR vec
= let tags = V.convert $ V.map tagOfSum2 vec
sel2 = U.tagsToSel2 tags
as' = fromVectorPR $ V.fromList $ [x | Alt2_1 x <- V.toList vec]
bs' = fromVectorPR $ V.fromList $ [x | Alt2_2 x <- V.toList vec]
in PSum2 sel2 as' bs'
toVectorPR pdata@(PSum2 sel _ _)
= let len = U.length $ U.tagsSel2 sel
in if len == 0
then V.empty
else V.map (indexPR pdata)
$ V.enumFromTo 0 (len 1)
emptydPR
= PSum2s V.empty emptydPR emptydPR
singletondPR (PSum2 sel2 xs ys)
= PSum2s (V.singleton sel2)
(singletondPR xs)
(singletondPR ys)
lengthdPR (PSum2s sel2s _ _)
= V.length sel2s
indexdPR (PSum2s sel2s xss yss) ix
= PSum2 (sel2s `V.unsafeIndex` ix)
(indexdPR xss ix)
(indexdPR yss ix)
appenddPR (PSum2s sels1 xss1 yss1)
(PSum2s sels2 xss2 yss2)
= PSum2s (sels1 V.++ sels2)
(xss1 `appenddPR` xss2)
(yss1 `appenddPR` yss2)
fromVectordPR vec
= let (sels, pdatas1, pdatas2)
= P.unzip3
$ [ (sel, pdata1, pdata2)
| PSum2 sel pdata1 pdata2 <- V.toList vec]
in PSum2s (V.fromList sels)
(fromVectordPR $ V.fromList pdatas1)
(fromVectordPR $ V.fromList pdatas2)
toVectordPR (PSum2s sels pdatas1 pdatas2)
= let vecs1 = toVectordPR pdatas1
vecs2 = toVectordPR pdatas2
in V.zipWith3 PSum2 sels vecs1 vecs2
instance PprPhysical U.Sel2 where
pprp sel2
= text "Sel2"
$+$ (nest 4 $ vcat
[ text "TAGS: " <+> text (show $ U.toList $ U.tagsSel2 sel2)
, text "INDICES:" <+> text (show $ U.toList $ U.indicesSel2 sel2)])