Separate length from data in array representation
[packages/dph.git] / dph-common / Data / Array / Parallel / Lifted / Unboxed.hs
1 {-# LANGUAGE CPP #-}
2
3 #include "fusion-phases.h"
4
5 module Data.Array.Parallel.Lifted.Unboxed (
6 Segd, elementsSegd#, mkSegd#,
7
8 PArray_Int#,
9 lengthPA_Int#, emptyPA_Int#,
10 replicatePA_Int#, replicatelPA_Int#, repeatPA_Int#, repeatcPA_Int#,
11 indexPA_Int#, extractPA_Int#, bpermutePA_Int#,
12 appPA_Int#, applPA_Int#,
13 packPA_Int#, pack'PA_Int#, combine2PA_Int#, combine2'PA_Int#,
14 fromListPA_Int#,
15 upToPA_Int#, enumFromToPA_Int#, enumFromThenToPA_Int#,
16 enumFromStepLenPA_Int#, enumFromToEachPA_Int#,
17 selectPA_Int#, selectorToIndices2PA#,
18 -- lengthSegdPA#, lengthsSegdPA#, indicesSegdPA#, elementsSegdPA#,
19 -- lengthsToSegdPA#, mkSegdPA#,
20 sumPA_Int#, sumPAs_Int#,
21 unsafe_mapPA_Int#, unsafe_zipWithPA_Int#, unsafe_foldPA_Int#,
22 unsafe_scanPA_Int#,
23
24 PArray_Word8#,
25 lengthPA_Word8#, emptyPA_Word8#,
26 replicatePA_Word8#, replicatelPA_Word8#, repeatPA_Word8#, repeatcPA_Word8#,
27 indexPA_Word8#, extractPA_Word8#, bpermutePA_Word8#,
28 appPA_Word8#, applPA_Word8#,
29 packPA_Word8#, pack'PA_Word8#, combine2PA_Word8#, combine2'PA_Word8#,
30 fromListPA_Word8#,
31 unsafe_zipWithPA_Word8#, unsafe_foldPA_Word8#, unsafe_fold1PA_Word8#,
32 unsafe_foldPAs_Word8#,
33
34 PArray_Double#,
35 lengthPA_Double#, emptyPA_Double#,
36 replicatePA_Double#, replicatelPA_Double#, repeatPA_Double#, repeatcPA_Double#,
37 indexPA_Double#, extractPA_Double#, bpermutePA_Double#,
38 appPA_Double#, applPA_Double#,
39 packPA_Double#, pack'PA_Double#, combine2PA_Double#, combine2'PA_Double#,
40 fromListPA_Double#,
41 unsafe_zipWithPA_Double#, unsafe_foldPA_Double#, unsafe_fold1PA_Double#,
42 unsafe_foldPAs_Double#,
43
44 PArray_Bool#,
45 lengthPA_Bool#, replicatelPA_Bool#,
46 packPA_Bool#, truesPA_Bool#, truesPAs_Bool#,
47
48 fromBoolPA#, toBoolPA#
49 ) where
50
51 import qualified Data.Array.Parallel.Unlifted as U
52 import Data.Array.Parallel.Base ((:*:)(..), fromBool, toBool)
53
54 import GHC.Exts ( Int#, Int(..), Word#,
55 Double#, Double(..) )
56 import GHC.Word ( Word8(..) )
57
58
59 import Debug.Trace
60
61 type Segd = U.Segd
62
63 {-
64 lengthSegd# :: Segd -> Int#
65 lengthSegd# segd = case U.lengthSegd segd of { I# n# -> n# }
66 {-# INLINE_PA lengthSegd# #-}
67 -}
68
69 elementsSegd# :: Segd -> Int#
70 elementsSegd# segd = case U.elementsSegd segd of { I# n# -> n# }
71 {-# INLINE_PA elementsSegd# #-}
72
73 {-
74 lengthsToSegdPA# :: PArray_Int# -> Segd
75 lengthsToSegdPA# = U.lengthsToSegd
76 {-# INLINE_PA lengthsToSegdPA# #-}
77 -}
78
79 mkSegd# :: U.Array Int -> U.Array Int -> Int# -> Segd
80 mkSegd# ns is n# = U.mkSegd ns is (I# n#)
81 {-# INLINE_PA mkSegd# #-}
82
83 {-# RULES
84
85 "mkSegd#" forall ns is n#.
86 mkSegd# ns is n# = U.mkSegd ns is (I# n#)
87
88 #-}
89
90
91 type PArray_Int# = U.Array Int
92
93 lengthPA_Int# :: PArray_Int# -> Int#
94 lengthPA_Int# arr = case U.length arr of { I# n# -> n# }
95 {-# INLINE_PA lengthPA_Int# #-}
96
97 emptyPA_Int# :: PArray_Int#
98 emptyPA_Int# = U.empty
99 {-# INLINE_PA emptyPA_Int# #-}
100
101 replicatePA_Int# :: Int# -> Int# -> PArray_Int#
102 replicatePA_Int# n# i# = U.replicate (I# n#) (I# i#)
103 {-# INLINE_PA replicatePA_Int# #-}
104
105 {-# RULES
106
107 "replicatePA_Int#" forall n# i#.
108 replicatePA_Int# n# i# = U.replicate (I# n#) (I# i#)
109
110 #-}
111
112 replicatelPA_Int# :: Segd -> PArray_Int# -> PArray_Int#
113 replicatelPA_Int# segd is = U.replicate_s segd is
114 {-# INLINE_PA replicatelPA_Int# #-}
115
116 {-# RULES
117
118 "replicatelPA_Int#" forall segd is.
119 replicatelPA_Int# segd is = U.replicate_s segd is
120
121 #-}
122
123 repeatPA_Int# :: Int# -> Int# -> PArray_Int# -> PArray_Int#
124 repeatPA_Int# n# len# is = U.repeat (I# n#) (I# len#) is
125 {-# INLINE_PA repeatPA_Int# #-}
126
127 repeatcPA_Int# :: Int# -> PArray_Int# -> Segd -> PArray_Int# -> PArray_Int#
128 repeatcPA_Int# n# ns segd is = U.repeat_c (I# n#) ns segd is
129 {-# INLINE_PA repeatcPA_Int# #-}
130
131 indexPA_Int# :: PArray_Int# -> Int# -> Int#
132 indexPA_Int# ns i# = case ns U.!: I# i# of { I# n# -> n# }
133 {-# INLINE_PA indexPA_Int# #-}
134
135 extractPA_Int# :: PArray_Int# -> Int# -> Int# -> PArray_Int#
136 extractPA_Int# xs i# n# = U.extract xs (I# i#) (I# n#)
137 {-# INLINE_PA extractPA_Int# #-}
138
139 bpermutePA_Int# :: PArray_Int# -> PArray_Int# -> PArray_Int#
140 bpermutePA_Int# ns is = U.bpermute ns is
141 {-# INLINE_PA bpermutePA_Int# #-}
142
143 appPA_Int# :: PArray_Int# -> PArray_Int# -> PArray_Int#
144 appPA_Int# ms ns = ms U.+:+ ns
145 {-# INLINE_PA appPA_Int# #-}
146
147 applPA_Int# :: Segd -> PArray_Int# -> Segd -> PArray_Int# -> PArray_Int#
148 applPA_Int# is xs js ys = U.append_s is xs js ys
149 {-# INLINE_PA applPA_Int# #-}
150
151 pack'PA_Int# :: PArray_Int# -> PArray_Bool# -> PArray_Int#
152 pack'PA_Int# ns bs = U.pack ns bs
153 {-# INLINE_PA pack'PA_Int# #-}
154
155 packPA_Int# :: PArray_Int# -> Int# -> PArray_Bool# -> PArray_Int#
156 packPA_Int# ns _ bs = pack'PA_Int# ns bs
157 {-# INLINE_PA packPA_Int# #-}
158
159 combine2'PA_Int# :: PArray_Int# -> PArray_Int# -> PArray_Int# -> PArray_Int#
160 combine2'PA_Int# sel xs ys = U.combine (U.map (== 0) sel) xs ys
161 {-# INLINE_PA combine2'PA_Int# #-}
162
163 combine2PA_Int# :: Int# -> PArray_Int# -> PArray_Int#
164 -> PArray_Int# -> PArray_Int# -> PArray_Int#
165 combine2PA_Int# _ sel _ xs ys = combine2'PA_Int# sel xs ys
166 {-# INLINE_PA combine2PA_Int# #-}
167
168 fromListPA_Int# :: Int# -> [Int] -> PArray_Int#
169 fromListPA_Int# _ xs = U.fromList xs
170 {-# INLINE_PA fromListPA_Int# #-}
171
172 upToPA_Int# :: Int# -> PArray_Int#
173 upToPA_Int# n# = U.enumFromTo 0 (I# n# - 1)
174 {-# INLINE_PA upToPA_Int# #-}
175
176 enumFromToPA_Int# :: Int# -> Int# -> PArray_Int#
177 enumFromToPA_Int# m# n# = U.enumFromTo (I# m#) (I# n#)
178 {-# INLINE_PA enumFromToPA_Int# #-}
179
180 enumFromThenToPA_Int# :: Int# -> Int# -> Int# -> PArray_Int#
181 enumFromThenToPA_Int# k# m# n# = U.enumFromThenTo (I# k#) (I# m#) (I# n#)
182 {-# INLINE_PA enumFromThenToPA_Int# #-}
183
184 enumFromStepLenPA_Int# :: Int# -> Int# -> Int# -> PArray_Int#
185 enumFromStepLenPA_Int# k# m# n# = U.enumFromStepLen (I# k#) (I# m#) (I# n#)
186 {-# INLINE_PA enumFromStepLenPA_Int# #-}
187
188 enumFromToEachPA_Int# :: Int# -> PArray_Int# -> PArray_Int# -> PArray_Int#
189 enumFromToEachPA_Int# n# is js = U.enumFromToEach (I# n#) (U.zip is js)
190 {-# INLINE_PA enumFromToEachPA_Int# #-}
191
192 selectPA_Int# :: PArray_Int# -> Int# -> PArray_Bool#
193 selectPA_Int# ns i# = U.map (\n -> n == I# i#) ns
194 {-# INLINE_PA selectPA_Int# #-}
195
196
197 selectorToIndices2PA# :: PArray_Int# -> PArray_Int#
198 selectorToIndices2PA# sel
199 = U.zipWith pick sel
200 . U.scan index (0 :*: 0)
201 $ U.map init sel
202 where
203 init 0 = 1 :*: 0
204 init _ = 0 :*: 1
205
206 index (i1 :*: j1) (i2 :*: j2) = (i1+i2 :*: j1+j2)
207
208 pick 0 (i :*: j) = i
209 pick _ (i :*: j) = j
210 {-# INLINE_PA selectorToIndices2PA# #-}
211
212 sumPA_Int# :: PArray_Int# -> Int#
213 sumPA_Int# ns = case U.sum ns of I# n# -> n#
214 {-# INLINE_PA sumPA_Int# #-}
215
216 sumPAs_Int# :: Segd -> PArray_Int# -> PArray_Int#
217 sumPAs_Int# segd ds = U.sum_s segd ds
218 {-# INLINE_PA sumPAs_Int# #-}
219
220 unsafe_mapPA_Int# :: (Int -> Int) -> PArray_Int# -> PArray_Int#
221 unsafe_mapPA_Int# f ns = U.map f ns
222 {-# INLINE_PA unsafe_mapPA_Int# #-}
223
224 unsafe_zipWithPA_Int# :: (Int -> Int -> Int)
225 -> PArray_Int# -> PArray_Int# -> PArray_Int#
226 unsafe_zipWithPA_Int# f ms ns = U.zipWith f ms ns
227 {-# INLINE_PA unsafe_zipWithPA_Int# #-}
228
229 unsafe_foldPA_Int# :: (Int -> Int -> Int) -> Int -> PArray_Int# -> Int
230 unsafe_foldPA_Int# f z ns = U.fold f z ns
231 {-# INLINE_PA unsafe_foldPA_Int# #-}
232
233 unsafe_scanPA_Int# :: (Int -> Int -> Int) -> Int -> PArray_Int# -> PArray_Int#
234 unsafe_scanPA_Int# f z ns = U.scan f z ns
235 {-# INLINE_PA unsafe_scanPA_Int# #-}
236
237 type PArray_Word8# = U.Array Word8
238
239 lengthPA_Word8# :: PArray_Word8# -> Int#
240 lengthPA_Word8# arr = case U.length arr of { I# n# -> n# }
241 {-# INLINE_PA lengthPA_Word8# #-}
242
243 emptyPA_Word8# :: PArray_Word8#
244 emptyPA_Word8# = U.empty
245 {-# INLINE_PA emptyPA_Word8# #-}
246
247 replicatePA_Word8# :: Int# -> Word# -> PArray_Word8#
248 replicatePA_Word8# n# d# = U.replicate (I# n#) (W8# d#)
249 {-# INLINE_PA replicatePA_Word8# #-}
250
251 replicatelPA_Word8# :: Segd -> PArray_Word8# -> PArray_Word8#
252 replicatelPA_Word8# segd ds = U.replicate_s segd ds
253 {-# INLINE_PA replicatelPA_Word8# #-}
254
255 repeatPA_Word8# :: Int# -> Int# -> PArray_Word8# -> PArray_Word8#
256 repeatPA_Word8# n# len# ds = U.repeat (I# n#) (I# len#) ds
257 {-# INLINE_PA repeatPA_Word8# #-}
258
259 repeatcPA_Word8# :: Int# -> PArray_Int# -> Segd -> PArray_Word8# -> PArray_Word8#
260 repeatcPA_Word8# n# ns segd is = U.repeat_c (I# n#) ns segd is
261 {-# INLINE_PA repeatcPA_Word8# #-}
262
263 indexPA_Word8# :: PArray_Word8# -> Int# -> Word#
264 indexPA_Word8# ds i# = case ds U.!: I# i# of { W8# d# -> d# }
265 {-# INLINE_PA indexPA_Word8# #-}
266
267 extractPA_Word8# :: PArray_Word8# -> Int# -> Int# -> PArray_Word8#
268 extractPA_Word8# xs i# n# = U.extract xs (I# i#) (I# n#)
269 {-# INLINE_PA extractPA_Word8# #-}
270
271 bpermutePA_Word8# :: PArray_Word8# -> PArray_Int# -> PArray_Word8#
272 bpermutePA_Word8# ds is = U.bpermute ds is
273 {-# INLINE_PA bpermutePA_Word8# #-}
274
275 appPA_Word8# :: PArray_Word8# -> PArray_Word8# -> PArray_Word8#
276 appPA_Word8# ms ns = ms U.+:+ ns
277 {-# INLINE_PA appPA_Word8# #-}
278
279 applPA_Word8# :: Segd -> PArray_Word8# -> Segd -> PArray_Word8#
280 -> PArray_Word8#
281 applPA_Word8# is xs js ys = U.append_s is xs js ys
282 {-# INLINE_PA applPA_Word8# #-}
283
284 pack'PA_Word8# :: PArray_Word8# -> PArray_Bool# -> PArray_Word8#
285 pack'PA_Word8# ns bs = U.pack ns bs
286 {-# INLINE_PA pack'PA_Word8# #-}
287
288 packPA_Word8# :: PArray_Word8# -> Int# -> PArray_Bool# -> PArray_Word8#
289 packPA_Word8# ns _ bs = pack'PA_Word8# ns bs
290 {-# INLINE_PA packPA_Word8# #-}
291
292 combine2'PA_Word8# :: PArray_Int#
293 -> PArray_Word8# -> PArray_Word8# -> PArray_Word8#
294 combine2'PA_Word8# sel xs ys = U.combine (U.map (== 0) sel) xs ys
295 {-# INLINE_PA combine2'PA_Word8# #-}
296
297 combine2PA_Word8# :: Int# -> PArray_Int# -> PArray_Int#
298 -> PArray_Word8# -> PArray_Word8# -> PArray_Word8#
299 combine2PA_Word8# _ sel _ xs ys = combine2'PA_Word8# sel xs ys
300 {-# INLINE_PA combine2PA_Word8# #-}
301
302 fromListPA_Word8# :: Int# -> [Word8] -> PArray_Word8#
303 fromListPA_Word8# _ xs = U.fromList xs
304 {-# INLINE_PA fromListPA_Word8# #-}
305
306 unsafe_zipWithPA_Word8# :: (Word8 -> Word8 -> Word8)
307 -> PArray_Word8# -> PArray_Word8# -> PArray_Word8#
308 unsafe_zipWithPA_Word8# f ms ns = U.zipWith f ms ns
309 {-# INLINE_PA unsafe_zipWithPA_Word8# #-}
310
311 unsafe_foldPA_Word8# :: (Word8 -> Word8 -> Word8)
312 -> Word8 -> PArray_Word8# -> Word8
313 unsafe_foldPA_Word8# f z ns = U.fold f z ns
314 {-# INLINE_PA unsafe_foldPA_Word8# #-}
315
316 unsafe_fold1PA_Word8#
317 :: (Word8 -> Word8 -> Word8) -> PArray_Word8# -> Word8
318 unsafe_fold1PA_Word8# f ns = U.fold1 f ns
319 {-# INLINE_PA unsafe_fold1PA_Word8# #-}
320
321 unsafe_foldPAs_Word8# :: (Word8 -> Word8 -> Word8) -> Word8
322 -> Segd -> PArray_Word8# -> PArray_Word8#
323 unsafe_foldPAs_Word8# f z segd ds = U.fold_s f z segd ds
324 {-# INLINE_PA unsafe_foldPAs_Word8# #-}
325
326 type PArray_Double# = U.Array Double
327
328 lengthPA_Double# :: PArray_Double# -> Int#
329 lengthPA_Double# arr = case U.length arr of { I# n# -> n# }
330 {-# INLINE_PA lengthPA_Double# #-}
331
332 emptyPA_Double# :: PArray_Double#
333 emptyPA_Double# = U.empty
334 {-# INLINE_PA emptyPA_Double# #-}
335
336 replicatePA_Double# :: Int# -> Double# -> PArray_Double#
337 replicatePA_Double# n# d# = U.replicate (I# n#) (D# d#)
338 {-# INLINE_PA replicatePA_Double# #-}
339
340 replicatelPA_Double# :: Segd -> PArray_Double# -> PArray_Double#
341 replicatelPA_Double# segd ds = U.replicate_s segd ds
342 {-# INLINE_PA replicatelPA_Double# #-}
343
344 repeatPA_Double# :: Int# -> Int# -> PArray_Double# -> PArray_Double#
345 repeatPA_Double# n# len# ds = U.repeat (I# n#) (I# len#) ds
346 {-# INLINE_PA repeatPA_Double# #-}
347
348 {-# RULES
349
350 "repeatPA_Double#" forall n# len# ds.
351 repeatPA_Double# n# len# ds = U.repeat (I# n#) (I# len#) ds
352
353 #-}
354
355 repeatcPA_Double# :: Int# -> PArray_Int# -> Segd -> PArray_Double# -> PArray_Double#
356 repeatcPA_Double# n# ns segd is = U.repeat_c (I# n#) ns segd is
357 {-# INLINE_PA repeatcPA_Double# #-}
358
359 indexPA_Double# :: PArray_Double# -> Int# -> Double#
360 indexPA_Double# ds i# = case ds U.!: I# i# of { D# d# -> d# }
361 {-# INLINE_PA indexPA_Double# #-}
362
363 extractPA_Double# :: PArray_Double# -> Int# -> Int# -> PArray_Double#
364 extractPA_Double# xs i# n# = U.extract xs (I# i#) (I# n#)
365 {-# INLINE_PA extractPA_Double# #-}
366
367 bpermutePA_Double# :: PArray_Double# -> PArray_Int# -> PArray_Double#
368 bpermutePA_Double# ds is = U.bpermute ds is
369 {-# INLINE_PA bpermutePA_Double# #-}
370
371 appPA_Double# :: PArray_Double# -> PArray_Double# -> PArray_Double#
372 appPA_Double# ms ns = ms U.+:+ ns
373 {-# INLINE_PA appPA_Double# #-}
374
375 applPA_Double# :: Segd -> PArray_Double# -> Segd -> PArray_Double#
376 -> PArray_Double#
377 applPA_Double# is xs js ys = U.append_s is xs js ys
378 {-# INLINE_PA applPA_Double# #-}
379
380 pack'PA_Double# :: PArray_Double# -> PArray_Bool# -> PArray_Double#
381 pack'PA_Double# ns bs = U.pack ns bs
382 {-# INLINE_PA pack'PA_Double# #-}
383
384 packPA_Double# :: PArray_Double# -> Int# -> PArray_Bool# -> PArray_Double#
385 packPA_Double# ns _ bs = pack'PA_Double# ns bs
386 {-# INLINE_PA packPA_Double# #-}
387
388 combine2'PA_Double# :: PArray_Int#
389 -> PArray_Double# -> PArray_Double# -> PArray_Double#
390 combine2'PA_Double# sel xs ys = U.combine (U.map (== 0) sel) xs ys
391 {-# INLINE_PA combine2'PA_Double# #-}
392
393 combine2PA_Double# :: Int# -> PArray_Int# -> PArray_Int#
394 -> PArray_Double# -> PArray_Double# -> PArray_Double#
395 combine2PA_Double# _ sel _ xs ys = combine2'PA_Double# sel xs ys
396 {-# INLINE_PA combine2PA_Double# #-}
397
398 fromListPA_Double# :: Int# -> [Double] -> PArray_Double#
399 fromListPA_Double# _ xs = U.fromList xs
400 {-# INLINE_PA fromListPA_Double# #-}
401
402 unsafe_zipWithPA_Double# :: (Double -> Double -> Double)
403 -> PArray_Double# -> PArray_Double# -> PArray_Double#
404 unsafe_zipWithPA_Double# f ms ns = U.zipWith f ms ns
405 {-# INLINE_PA unsafe_zipWithPA_Double# #-}
406
407 unsafe_foldPA_Double# :: (Double -> Double -> Double)
408 -> Double -> PArray_Double# -> Double
409 unsafe_foldPA_Double# f z ns = U.fold f z ns
410 {-# INLINE_PA unsafe_foldPA_Double# #-}
411
412 unsafe_fold1PA_Double#
413 :: (Double -> Double -> Double) -> PArray_Double# -> Double
414 unsafe_fold1PA_Double# f ns = U.fold1 f ns
415 {-# INLINE_PA unsafe_fold1PA_Double# #-}
416
417 unsafe_foldPAs_Double# :: (Double -> Double -> Double) -> Double
418 -> Segd -> PArray_Double# -> PArray_Double#
419 unsafe_foldPAs_Double# f z segd ds = U.fold_s f z segd ds
420 {-# INLINE_PA unsafe_foldPAs_Double# #-}
421
422 type PArray_Bool# = U.Array Bool
423
424 lengthPA_Bool# :: PArray_Bool# -> Int#
425 lengthPA_Bool# arr = case U.length arr of { I# n# -> n# }
426 {-# INLINE_PA lengthPA_Bool# #-}
427
428 replicatelPA_Bool# :: Segd -> PArray_Bool# -> PArray_Bool#
429 replicatelPA_Bool# segd ds = U.replicate_s segd ds
430 {-# INLINE_PA replicatelPA_Bool# #-}
431
432 packPA_Bool# :: PArray_Bool# -> Int# -> PArray_Bool# -> PArray_Bool#
433 packPA_Bool# ns _ bs = U.pack ns bs
434 {-# INLINE_PA packPA_Bool# #-}
435
436 truesPA_Bool# :: PArray_Bool# -> Int#
437 truesPA_Bool# bs = sumPA_Int# (fromBoolPA# bs)
438 {-# INLINE_PA truesPA_Bool# #-}
439
440 truesPAs_Bool# :: Segd -> PArray_Bool# -> PArray_Int#
441 truesPAs_Bool# segd = sumPAs_Int# segd . fromBoolPA#
442 {-# INLINE truesPAs_Bool# #-}
443
444 fromBoolPA# :: PArray_Bool# -> PArray_Int#
445 fromBoolPA# = U.map fromBool
446 {-# INLINE_PA fromBoolPA# #-}
447
448 toBoolPA# :: PArray_Int# -> PArray_Bool#
449 toBoolPA# = U.map toBool
450 {-# INLINE_PA toBoolPA# #-}
451