d920d23b284fcbed0fbd5133ccb4b5f96e47dcc9
[packages/dph.git] / dph-common / Data / Array / Parallel / Lifted / PArray.hs
1 {-# LANGUAGE CPP #-}
2
3 #include "fusion-phases.h"
4
5 module Data.Array.Parallel.Lifted.PArray (
6 PArray,
7
8 PA(..),
9 lengthPA#, replicatePA#, replicatelPA#, repeatPA#, emptyPA,
10 indexPA#, extractPA#, bpermutePA#, appPA#, applPA#,
11 packPA#, combine2PA#, fromListPA#, fromListPA, nfPA,
12
13 PRepr, PR(..), mkPR, mkReprPA
14 ) where
15
16 import qualified Data.Array.Parallel.Unlifted as U
17 import Data.Array.Parallel.Lifted.Unboxed ( Segd, PArray_Int#, PArray_Bool# )
18 import GHC.Exts (Int#, Int(..))
19
20 -- |Lifted parallel arrays
21 --
22 data family PArray a
23
24 -- |Representation types
25 --
26 type family PRepr a
27
28 -- |Dictionaries
29 --
30
31 data PA a = PA {
32 toPRepr :: a -> PRepr a
33 , fromPRepr :: PRepr a -> a
34 , toArrPRepr :: PArray a -> PArray (PRepr a)
35 , fromArrPRepr :: PArray (PRepr a) -> PArray a
36 , dictPRepr :: PR (PRepr a)
37 }
38
39 lengthPA# :: PA a -> PArray a -> Int#
40 {-# INLINE_PA lengthPA# #-}
41 lengthPA# pa x = lengthPR (dictPRepr pa) (toArrPRepr pa x)
42
43 emptyPA :: PA a -> PArray a
44 {-# INLINE_PA emptyPA #-}
45 emptyPA pa = fromArrPRepr pa
46 $ emptyPR (dictPRepr pa)
47
48 replicatePA# :: PA a -> Int# -> a -> PArray a
49 {-# INLINE_PA replicatePA# #-}
50 replicatePA# pa n# = fromArrPRepr pa
51 . replicatePR (dictPRepr pa) n#
52 . toPRepr pa
53
54 replicatelPA# :: PA a -> Segd -> PArray a -> PArray a
55 {-# INLINE_PA replicatelPA# #-}
56 replicatelPA# pa segd = fromArrPRepr pa
57 . replicatelPR (dictPRepr pa) segd
58 . toArrPRepr pa
59
60 repeatPA# :: PA a -> Int# -> Int# -> PArray a -> PArray a
61 {-# INLINE_PA repeatPA# #-}
62 repeatPA# pa n# len# = fromArrPRepr pa
63 . repeatPR (dictPRepr pa) n# len#
64 . toArrPRepr pa
65
66 repeatcPA# :: PA a -> Int# -> PArray_Int# -> Segd -> PArray a -> PArray a
67 {-# INLINE_PA repeatcPA# #-}
68 repeatcPA# pa n# ns segd = fromArrPRepr pa
69 . repeatcPR (dictPRepr pa) n# ns segd
70 . toArrPRepr pa
71
72 indexPA# :: PA a -> PArray a -> Int# -> a
73 {-# INLINE_PA indexPA# #-}
74 indexPA# pa xs i# = fromPRepr pa
75 $ indexPR (dictPRepr pa) (toArrPRepr pa xs) i#
76
77 extractPA# :: PA a -> PArray a -> Int# -> Int# -> PArray a
78 {-# INLINE_PA extractPA# #-}
79 extractPA# pa xs i# n# = fromArrPRepr pa
80 $ extractPR (dictPRepr pa) (toArrPRepr pa xs) i# n#
81
82 bpermutePA# :: PA a -> Int# -> PArray a -> PArray_Int# -> PArray a
83 {-# INLINE bpermutePA# #-}
84 bpermutePA# pa n# xs is = fromArrPRepr pa
85 $ bpermutePR (dictPRepr pa) n# (toArrPRepr pa xs) is
86
87 appPA# :: PA a -> PArray a -> PArray a -> PArray a
88 {-# INLINE_PA appPA# #-}
89 appPA# pa xs ys = fromArrPRepr pa
90 $ appPR (dictPRepr pa) (toArrPRepr pa xs) (toArrPRepr pa ys)
91
92 applPA# :: PA a -> Segd -> PArray a -> Segd -> PArray a -> PArray a
93 {-# INLINE_PA applPA# #-}
94 applPA# pa is xs js ys = fromArrPRepr pa
95 $ applPR (dictPRepr pa) is (toArrPRepr pa xs)
96 js (toArrPRepr pa ys)
97
98 packPA# :: PA a -> PArray a -> Int# -> PArray_Bool# -> PArray a
99 {-# INLINE_PA packPA# #-}
100 packPA# pa arr n# = fromArrPRepr pa
101 . packPR (dictPRepr pa) (toArrPRepr pa arr) n#
102
103 combine2PA# :: PA a -> Int# -> PArray_Int# -> PArray_Int#
104 -> PArray a -> PArray a -> PArray a
105 {-# INLINE_PA combine2PA# #-}
106 combine2PA# pa n# sel# is# as bs
107 = fromArrPRepr pa
108 $ combine2PR (dictPRepr pa) n# sel# is# (toArrPRepr pa as) (toArrPRepr pa bs)
109
110 fromListPA# :: PA a -> Int# -> [a] -> PArray a
111 {-# INLINE_PA fromListPA# #-}
112 fromListPA# pa n# xs = fromArrPRepr pa
113 $ fromListPR (dictPRepr pa) n# (map (toPRepr pa) xs)
114
115 fromListPA :: PA a -> [a] -> PArray a
116 {-# INLINE fromListPA #-}
117 fromListPA pa xs = case length xs of
118 I# n# -> fromListPA# pa n# xs
119
120 nfPA :: PA a -> PArray a -> ()
121 {-# INLINE nfPA #-}
122 nfPA pa xs = nfPR (dictPRepr pa) $ toArrPRepr pa xs
123
124 data PR a = PR {
125 lengthPR :: PArray a -> Int#
126 , emptyPR :: PArray a
127 , replicatePR :: Int# -> a -> PArray a
128 , replicatelPR :: Segd -> PArray a -> PArray a
129 , repeatPR :: Int# -> Int# -> PArray a -> PArray a
130 , repeatcPR :: Int# -> PArray_Int# -> Segd -> PArray a -> PArray a
131 , indexPR :: PArray a -> Int# -> a
132 , extractPR :: PArray a -> Int# -> Int# -> PArray a
133 , bpermutePR :: Int# -> PArray a -> PArray_Int# -> PArray a
134 , appPR :: PArray a -> PArray a -> PArray a
135 , applPR :: Segd -> PArray a -> Segd -> PArray a -> PArray a
136 , packPR :: PArray a -> Int# -> PArray_Bool# -> PArray a
137 , combine2PR :: Int# -> PArray_Int# -> PArray_Int#
138 -> PArray a -> PArray a -> PArray a
139 , fromListPR :: Int# -> [a] -> PArray a
140 , nfPR :: PArray a -> ()
141 }
142
143 mkPR :: PA a -> PR a
144 {-# INLINE mkPR #-}
145 mkPR pa = PR {
146 lengthPR = lengthPA# pa
147 , emptyPR = emptyPA pa
148 , replicatePR = replicatePA# pa
149 , replicatelPR = replicatelPA# pa
150 , repeatPR = repeatPA# pa
151 , repeatcPR = repeatcPA# pa
152 , indexPR = indexPA# pa
153 , bpermutePR = bpermutePA# pa
154 , appPR = appPA# pa
155 , applPR = applPA# pa
156 , packPR = packPA# pa
157 , combine2PR = combine2PA# pa
158 , fromListPR = fromListPA# pa
159 , nfPR = nfPA pa
160 }
161
162 mkReprPA :: (a ~ PRepr a) => PR a -> PA a
163 {-# INLINE mkReprPA #-}
164 mkReprPA pr = PA {
165 toPRepr = id
166 , fromPRepr = id
167 , toArrPRepr = id
168 , fromArrPRepr = id
169 , dictPRepr = pr
170 }
171