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