dph-lifted-vseg: eliminate sharing in arrays during zipl
[packages/dph.git] / dph-lifted-vseg / Data / Array / Parallel / PArray / PRepr / Base.hs
1 #include "fusion-phases.h"
2
3 -- | Definition of the PRepr/PA family and class.
4 -- PA functions are the same as the methods of the PR class,
5 -- except that they take a PA dictinoary instead of a PR
6 -- d ictionary.
7 --
8 module Data.Array.Parallel.PArray.PRepr.Base
9 ( PRepr
10 , PA (..)
11
12 -- * House Keeping
13 , validPA
14 , nfPA
15 , similarPA
16 , coversPA
17 , pprpPA
18 , pprpDataPA
19
20 -- * Constructors
21 , emptyPA
22 , replicatePA, replicatesPA
23 , appendPA, appendsPA
24
25 -- * Projections
26 , lengthPA
27 , indexPA, indexlPA
28 , extractPA, extractsPA
29
30 -- * Pack and Combine
31 , packByTagPA
32 , combine2PA
33
34 -- * Conversions
35 , fromVectorPA, toVectorPA
36
37 -- * PDatas
38 , emptydPA
39 , singletondPA
40 , lengthdPA
41 , indexdPA
42 , appenddPA
43 , concatdPA
44 , fromVectordPA, toVectordPA)
45 where
46 import Data.Array.Parallel.Pretty
47 import Data.Array.Parallel.PArray.PData.Base
48 import Data.Array.Parallel.Base (Tag)
49 import Data.Vector (Vector)
50 import qualified Data.Array.Parallel.Unlifted as U
51 import qualified Data.Vector as V
52
53 -- | Representable types.
54 --
55 -- The family of types that we know how to represent generically.
56 -- PRepr takes an arbitrary type and produces the generic type we use to
57 -- represent it.
58 --
59 -- Instances for simple types are defined in D.A.P.PArray.PRepr.Instances.
60 -- For algebraic types, it's up to the vectoriser/client module to create
61 -- a suitable instance.
62 --
63 type family PRepr a
64
65
66 -- | A PA dictionary contains the functions that we use to convert a
67 -- representable type to and from its generic representation.
68 -- The conversion methods should all be O(1).
69 class PR (PRepr a) => PA a where
70 toPRepr :: a -> PRepr a
71 fromPRepr :: PRepr a -> a
72
73 toArrPRepr :: PData a -> PData (PRepr a)
74 fromArrPRepr :: PData (PRepr a) -> PData a
75
76 toArrPReprs :: PDatas a -> PDatas (PRepr a)
77 fromArrPReprs :: PDatas (PRepr a) -> PDatas a
78
79 toNestedArrPRepr :: PData (PArray a) -> PData (PArray (PRepr a))
80 fromNestedArrPRepr :: PData (PArray (PRepr a)) -> PData (PArray a)
81
82 -- PD Wrappers ----------------------------------------------------------------
83 -- These wrappers work on (PData a) arrays when we know the element type 'a'
84 -- is representable. We implement them by converting the PData to the
85 -- underlying generic representation type, and use the corresponding method
86 -- from the PR dictionary.
87 --
88 -- The wrappers are used in situations when we only have PA dictionary,
89 -- instead of a PR dictionary. This happens in the PR (a :-> b) instance,
90 -- as we need to work on a generically represented environment, and only
91 -- have an existential PA dictionary. We also use them in the PA functions
92 -- defined by D.A.P.PArray.
93 --
94 -- See the D.A.P.PArray.PData.Base for docs on what these functions do.
95 --
96 {-# INLINE_PA validPA #-}
97 validPA :: PA a => PData a -> Bool
98 validPA arr
99 = validPR (toArrPRepr arr)
100
101
102 {-# INLINE_PA nfPA #-}
103 nfPA :: PA a => PData a -> ()
104 nfPA arr
105 = nfPR
106 $ toArrPRepr arr
107
108
109 {-# INLINE_PA similarPA #-}
110 similarPA :: PA a => a -> a -> Bool
111 similarPA x y
112 = similarPR (toPRepr x) (toPRepr y)
113
114
115 {-# INLINE_PA coversPA #-}
116 coversPA :: PA a => Bool -> PData a -> Int -> Bool
117 coversPA weak pdata ix
118 = coversPR weak (toArrPRepr pdata) ix
119
120
121 {-# INLINE_PA pprpPA #-}
122 pprpPA :: PA a => a -> Doc
123 pprpPA x
124 = pprpPR (toPRepr x)
125
126
127 {-# INLINE_PA pprpDataPA #-}
128 pprpDataPA :: PA a => PData a -> Doc
129 pprpDataPA x
130 = pprpDataPR (toArrPRepr x)
131
132
133 -- Constructors ---------------------------------
134 {-# INLINE_PA emptyPA #-}
135 emptyPA :: PA a => PData a
136 emptyPA
137 = fromArrPRepr emptyPR
138
139
140 {-# INLINE_PA replicatePA #-}
141 replicatePA :: PA a => Int -> a -> PData a
142 replicatePA n x
143 = fromArrPRepr
144 $ replicatePR n $ toPRepr x
145
146
147 {-# INLINE_PA replicatesPA #-}
148 replicatesPA :: PA a => U.Segd -> PData a -> PData a
149 replicatesPA segd xs
150 = fromArrPRepr
151 $ replicatesPR segd (toArrPRepr xs)
152
153
154 {-# INLINE_PA appendPA #-}
155 appendPA :: PA a => PData a -> PData a -> PData a
156 appendPA xs ys
157 = fromArrPRepr
158 $ appendPR (toArrPRepr xs) (toArrPRepr ys)
159
160
161 {-# INLINE_PA appendsPA #-}
162 appendsPA :: PA a => U.Segd -> U.Segd -> PData a -> U.Segd -> PData a -> PData a
163 appendsPA segdResult segd1 xs segd2 ys
164 = fromArrPRepr
165 $ appendsPR segdResult segd1 (toArrPRepr xs) segd2 (toArrPRepr ys)
166
167
168 -- Projections ----------------------------------
169 {-# INLINE_PA lengthPA #-}
170 lengthPA :: PA a => PData a -> Int
171 lengthPA xs
172 = lengthPR (toArrPRepr xs)
173
174
175 {-# INLINE_PA indexPA #-}
176 indexPA :: PA a => PData a -> Int -> a
177 indexPA xs i
178 = fromPRepr
179 $ indexPR (toArrPRepr xs) i
180
181
182 {-# INLINE_PA indexlPA #-}
183 indexlPA :: PA a => PData (PArray a) -> PData Int -> PData a
184 indexlPA xss ixs
185 = fromArrPRepr
186 $ indexlPR (toNestedArrPRepr xss) ixs
187
188
189 {-# INLINE_PA extractPA #-}
190 extractPA :: PA a => PData a -> Int -> Int -> PData a
191 extractPA xs start len
192 = fromArrPRepr
193 $ extractPR (toArrPRepr xs) start len
194
195
196 {-# INLINE_PA extractsPA #-}
197 extractsPA :: PA a => PDatas a -> U.SSegd -> PData a
198 extractsPA xss segd
199 = fromArrPRepr
200 $ extractsPR (toArrPReprs xss) segd
201
202
203 -- Pack and Combine -----------------------------
204 {-# INLINE_PA packByTagPA #-}
205 packByTagPA :: PA a => PData a -> U.Array Tag -> Tag -> PData a
206 packByTagPA xs tags tag
207 = fromArrPRepr
208 $ packByTagPR (toArrPRepr xs) tags tag
209
210
211 {-# INLINE_PA combine2PA #-}
212 combine2PA :: PA a => U.Sel2 -> PData a -> PData a -> PData a
213 combine2PA sel xs ys
214 = fromArrPRepr
215 $ combine2PR sel (toArrPRepr xs) (toArrPRepr ys)
216
217
218 -- Conversions ----------------------------------
219 {-# INLINE_PA fromVectorPA #-}
220 fromVectorPA :: PA a => Vector a -> PData a
221 fromVectorPA vec
222 = fromArrPRepr
223 $ fromVectorPR (V.map toPRepr vec)
224
225
226 {-# INLINE_PA toVectorPA #-}
227 toVectorPA :: PA a => PData a -> Vector a
228 toVectorPA pdata
229 = V.map fromPRepr
230 $ toVectorPR (toArrPRepr pdata)
231
232
233 {-# INLINE_PA emptydPA #-}
234 emptydPA :: PA a => PDatas a
235 emptydPA
236 = fromArrPReprs
237 $ emptydPR
238
239
240 {-# INLINE_PA singletondPA #-}
241 singletondPA :: PA a => PData a -> PDatas a
242 singletondPA pdata
243 = fromArrPReprs
244 $ singletondPR (toArrPRepr pdata)
245
246
247 {-# INLINE_PA lengthdPA #-}
248 lengthdPA :: PA a => PDatas a -> Int
249 lengthdPA pdatas
250 = lengthdPR (toArrPReprs pdatas)
251
252
253 {-# INLINE_PA indexdPA #-}
254 indexdPA :: PA a => PDatas a -> Int -> PData a
255 indexdPA pdatas ix
256 = fromArrPRepr
257 $ indexdPR (toArrPReprs pdatas) ix
258
259
260 {-# INLINE_PA appenddPA #-}
261 appenddPA :: PA a => PDatas a -> PDatas a -> PDatas a
262 appenddPA xs ys
263 = fromArrPReprs
264 $ appenddPR (toArrPReprs xs) (toArrPReprs ys)
265
266
267 {-# INLINE_PA concatdPA #-}
268 concatdPA :: PA a => V.Vector (PDatas a) -> PDatas a
269 concatdPA vec
270 = fromArrPReprs
271 $ concatdPR (V.map toArrPReprs vec)
272
273
274 {-# INLINE_PA fromVectordPA #-}
275 fromVectordPA :: PA a => V.Vector (PData a) -> PDatas a
276 fromVectordPA vec
277 = fromArrPReprs
278 $ fromVectordPR (V.map toArrPRepr vec)
279
280
281 {-# INLINE_PA toVectordPA #-}
282 toVectordPA :: PA a => PDatas a -> V.Vector (PData a)
283 toVectordPA pdatas
284 = V.map fromArrPRepr
285 $ toVectordPR (toArrPReprs pdatas)
286