/
Prelude.hs
2628 lines (2368 loc) · 82 KB
/
Prelude.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PatternGuards #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RebindableSyntax #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# OPTIONS_GHC -fno-warn-incomplete-patterns #-} -- pattern synonyms
-- |
-- Module : Data.Array.Accelerate.Prelude
-- Copyright : [2009..2020] The Accelerate Team
-- License : BSD3
--
-- Maintainer : Trevor L. McDonell <trevor.mcdonell@gmail.com>
-- Stability : experimental
-- Portability : non-portable (GHC extensions)
--
-- Standard functions that are not part of the core set (directly represented in
-- the AST), but are instead implemented in terms of the core set.
--
module Data.Array.Accelerate.Prelude (
-- * Element-wise operations
indexed,
imap,
-- * Zipping
zipWith3, zipWith4, zipWith5, zipWith6, zipWith7, zipWith8, zipWith9,
izipWith, izipWith3, izipWith4, izipWith5, izipWith6, izipWith7, izipWith8, izipWith9,
zip, zip3, zip4, zip5, zip6, zip7, zip8, zip9,
-- * Unzipping
unzip, unzip3, unzip4, unzip5, unzip6, unzip7, unzip8, unzip9,
-- * Reductions
foldAll, fold1All,
foldSeg, fold1Seg,
-- ** Specialised folds
all, any, and, or, sum, product, minimum, maximum,
-- * Scans
prescanl, postscanl, prescanr, postscanr,
-- ** Segmented scans
scanlSeg, scanl'Seg, scanl1Seg, prescanlSeg, postscanlSeg,
scanrSeg, scanr'Seg, scanr1Seg, prescanrSeg, postscanrSeg,
-- * Shape manipulation
flatten,
-- * Enumeration and filling
fill, enumFromN, enumFromStepN,
-- * Concatenation
(++), concatOn,
-- * Working with predicates
-- ** Filtering
filter, compact,
-- ** Scatter / Gather
scatter, scatterIf,
gather, gatherIf,
-- * Permutations
reverse, transpose,
reverseOn, transposeOn,
-- * Extracting sub-vectors
init, tail, take, drop, slit,
initOn, tailOn, takeOn, dropOn, slitOn,
-- * Controlling execution
compute,
-- * Flow control
IfThenElse(..),
-- ** Array-level
(?|),
-- ** Expression-level
(?), match,
-- * Scalar iteration
iterate,
-- * Scalar reduction
sfoldl, -- sfoldr,
-- * Lifting and unlifting
Lift(..), Unlift(..),
lift1, lift2, lift3, ilift1, ilift2, ilift3,
-- ** Tuple construction and destruction
fst, afst, snd, asnd, curry, uncurry,
-- ** Index construction and destruction
index0, index1, unindex1, index2, unindex2, index3, unindex3,
-- * Array operations with a scalar result
the, null, length,
-- * Irregular data-parallelism
expand,
-- * Sequence operations
-- fromSeq, fromSeqElems, fromSeqShapes, toSeqInner, toSeqOuter2, toSeqOuter3, generateSeq,
) where
import Data.Array.Accelerate.Analysis.Match
import Data.Array.Accelerate.Language
import Data.Array.Accelerate.Lift
import Data.Array.Accelerate.Pattern
import Data.Array.Accelerate.Pattern.Maybe
import Data.Array.Accelerate.Smart
import Data.Array.Accelerate.Sugar.Array ( Arrays, Array, Scalar, Vector, Segments, fromList )
import Data.Array.Accelerate.Sugar.Elt
import Data.Array.Accelerate.Sugar.Shape ( Shape, Slice, Z(..), (:.)(..), All(..), DIM1, DIM2, empty )
import Data.Array.Accelerate.Type
import Data.Array.Accelerate.Classes.Eq
import Data.Array.Accelerate.Classes.FromIntegral
import Data.Array.Accelerate.Classes.Integral
import Data.Array.Accelerate.Classes.Num
import Data.Array.Accelerate.Classes.Ord
import Data.Array.Accelerate.Data.Bits
import Lens.Micro ( Lens', (&), (^.), (.~), (+~), (-~), lens, over )
import Prelude ( (.), ($), Maybe(..), const, id, flip )
-- $setup
-- >>> :seti -XFlexibleContexts
-- >>> import Data.Array.Accelerate
-- >>> import Data.Array.Accelerate.Interpreter
-- >>> :{
-- let runExp :: Elt e => Exp e -> e
-- runExp e = indexArray (run (unit e)) Z
-- :}
-- Element-wise operations
-- -----------------------
-- | Pair each element with its index
--
-- >>> let xs = fromList (Z:.5) [0..] :: Vector Float
-- >>> run $ indexed (use xs)
-- Vector (Z :. 5) [(Z :. 0,0.0),(Z :. 1,1.0),(Z :. 2,2.0),(Z :. 3,3.0),(Z :. 4,4.0)]
--
-- >>> let mat = fromList (Z:.3:.4) [0..] :: Matrix Float
-- >>> run $ indexed (use mat)
-- Matrix (Z :. 3 :. 4)
-- [ (Z :. 0 :. 0,0.0), (Z :. 0 :. 1,1.0), (Z :. 0 :. 2,2.0), (Z :. 0 :. 3,3.0),
-- (Z :. 1 :. 0,4.0), (Z :. 1 :. 1,5.0), (Z :. 1 :. 2,6.0), (Z :. 1 :. 3,7.0),
-- (Z :. 2 :. 0,8.0), (Z :. 2 :. 1,9.0), (Z :. 2 :. 2,10.0), (Z :. 2 :. 3,11.0)]
--
indexed :: (Shape sh, Elt a) => Acc (Array sh a) -> Acc (Array sh (sh, a))
indexed = imap T2
-- | Apply a function to every element of an array and its index
--
imap :: (Shape sh, Elt a, Elt b)
=> (Exp sh -> Exp a -> Exp b)
-> Acc (Array sh a)
-> Acc (Array sh b)
imap f xs = zipWith f (generate (shape xs) id) xs
-- | Used to define the zipWith functions on more than two arrays
--
zipWithInduction
:: (Shape sh, Elt a, Elt b)
=> ((Exp (a,b) -> rest) -> Acc (Array sh (a,b)) -> result) -- The zipWith function operating on one fewer array
-> (Exp a -> Exp b -> rest)
-> Acc (Array sh a)
-> Acc (Array sh b)
-> result
zipWithInduction prev f as bs = prev (\(T2 a b) -> f a b) (zip as bs)
-- | Zip three arrays with the given function, analogous to 'zipWith'.
--
zipWith3
:: (Shape sh, Elt a, Elt b, Elt c, Elt d)
=> (Exp a -> Exp b -> Exp c -> Exp d)
-> Acc (Array sh a)
-> Acc (Array sh b)
-> Acc (Array sh c)
-> Acc (Array sh d)
zipWith3 = zipWithInduction zipWith
-- | Zip four arrays with the given function, analogous to 'zipWith'.
--
zipWith4
:: (Shape sh, Elt a, Elt b, Elt c, Elt d, Elt e)
=> (Exp a -> Exp b -> Exp c -> Exp d -> Exp e)
-> Acc (Array sh a)
-> Acc (Array sh b)
-> Acc (Array sh c)
-> Acc (Array sh d)
-> Acc (Array sh e)
zipWith4 = zipWithInduction zipWith3
-- | Zip five arrays with the given function, analogous to 'zipWith'.
--
zipWith5
:: (Shape sh, Elt a, Elt b, Elt c, Elt d, Elt e, Elt f)
=> (Exp a -> Exp b -> Exp c -> Exp d -> Exp e -> Exp f)
-> Acc (Array sh a)
-> Acc (Array sh b)
-> Acc (Array sh c)
-> Acc (Array sh d)
-> Acc (Array sh e)
-> Acc (Array sh f)
zipWith5 = zipWithInduction zipWith4
-- | Zip six arrays with the given function, analogous to 'zipWith'.
--
zipWith6
:: (Shape sh, Elt a, Elt b, Elt c, Elt d, Elt e, Elt f, Elt g)
=> (Exp a -> Exp b -> Exp c -> Exp d -> Exp e -> Exp f -> Exp g)
-> Acc (Array sh a)
-> Acc (Array sh b)
-> Acc (Array sh c)
-> Acc (Array sh d)
-> Acc (Array sh e)
-> Acc (Array sh f)
-> Acc (Array sh g)
zipWith6 = zipWithInduction zipWith5
-- | Zip seven arrays with the given function, analogous to 'zipWith'.
--
zipWith7
:: (Shape sh, Elt a, Elt b, Elt c, Elt d, Elt e, Elt f, Elt g, Elt h)
=> (Exp a -> Exp b -> Exp c -> Exp d -> Exp e -> Exp f -> Exp g -> Exp h)
-> Acc (Array sh a)
-> Acc (Array sh b)
-> Acc (Array sh c)
-> Acc (Array sh d)
-> Acc (Array sh e)
-> Acc (Array sh f)
-> Acc (Array sh g)
-> Acc (Array sh h)
zipWith7 = zipWithInduction zipWith6
-- | Zip eight arrays with the given function, analogous to 'zipWith'.
--
zipWith8
:: (Shape sh, Elt a, Elt b, Elt c, Elt d, Elt e, Elt f, Elt g, Elt h, Elt i)
=> (Exp a -> Exp b -> Exp c -> Exp d -> Exp e -> Exp f -> Exp g -> Exp h -> Exp i)
-> Acc (Array sh a)
-> Acc (Array sh b)
-> Acc (Array sh c)
-> Acc (Array sh d)
-> Acc (Array sh e)
-> Acc (Array sh f)
-> Acc (Array sh g)
-> Acc (Array sh h)
-> Acc (Array sh i)
zipWith8 = zipWithInduction zipWith7
-- | Zip nine arrays with the given function, analogous to 'zipWith'.
--
zipWith9
:: (Shape sh, Elt a, Elt b, Elt c, Elt d, Elt e, Elt f, Elt g, Elt h, Elt i, Elt j)
=> (Exp a -> Exp b -> Exp c -> Exp d -> Exp e -> Exp f -> Exp g -> Exp h -> Exp i -> Exp j)
-> Acc (Array sh a)
-> Acc (Array sh b)
-> Acc (Array sh c)
-> Acc (Array sh d)
-> Acc (Array sh e)
-> Acc (Array sh f)
-> Acc (Array sh g)
-> Acc (Array sh h)
-> Acc (Array sh i)
-> Acc (Array sh j)
zipWith9 = zipWithInduction zipWith8
-- | Used to define the izipWith functions on two or more arrays
--
izipWithInduction
:: (Shape sh, Elt a, Elt b)
=> ((Exp sh -> Exp (a,b) -> rest) -> Acc (Array sh (a,b)) -> result) -- The zipWith function operating on one fewer array
-> (Exp sh -> Exp a -> Exp b -> rest)
-> Acc (Array sh a)
-> Acc (Array sh b)
-> result
izipWithInduction prev f as bs = prev (\ix (T2 a b) -> f ix a b) (zip as bs)
-- | Zip two arrays with a function that also takes the element index
--
izipWith
:: (Shape sh, Elt a, Elt b, Elt c)
=> (Exp sh -> Exp a -> Exp b -> Exp c)
-> Acc (Array sh a)
-> Acc (Array sh b)
-> Acc (Array sh c)
izipWith = izipWithInduction imap
-- | Zip three arrays with a function that also takes the element index,
-- analogous to 'izipWith'.
--
izipWith3
:: (Shape sh, Elt a, Elt b, Elt c, Elt d)
=> (Exp sh -> Exp a -> Exp b -> Exp c -> Exp d)
-> Acc (Array sh a)
-> Acc (Array sh b)
-> Acc (Array sh c)
-> Acc (Array sh d)
izipWith3 = izipWithInduction izipWith
-- | Zip four arrays with the given function that also takes the element index,
-- analogous to 'zipWith'.
--
izipWith4
:: (Shape sh, Elt a, Elt b, Elt c, Elt d, Elt e)
=> (Exp sh -> Exp a -> Exp b -> Exp c -> Exp d -> Exp e)
-> Acc (Array sh a)
-> Acc (Array sh b)
-> Acc (Array sh c)
-> Acc (Array sh d)
-> Acc (Array sh e)
izipWith4 = izipWithInduction izipWith3
-- | Zip five arrays with the given function that also takes the element index,
-- analogous to 'zipWith'.
--
izipWith5
:: (Shape sh, Elt a, Elt b, Elt c, Elt d, Elt e, Elt f)
=> (Exp sh -> Exp a -> Exp b -> Exp c -> Exp d -> Exp e -> Exp f)
-> Acc (Array sh a)
-> Acc (Array sh b)
-> Acc (Array sh c)
-> Acc (Array sh d)
-> Acc (Array sh e)
-> Acc (Array sh f)
izipWith5 = izipWithInduction izipWith4
-- | Zip six arrays with the given function that also takes the element index,
-- analogous to 'zipWith'.
--
izipWith6
:: (Shape sh, Elt a, Elt b, Elt c, Elt d, Elt e, Elt f, Elt g)
=> (Exp sh -> Exp a -> Exp b -> Exp c -> Exp d -> Exp e -> Exp f -> Exp g)
-> Acc (Array sh a)
-> Acc (Array sh b)
-> Acc (Array sh c)
-> Acc (Array sh d)
-> Acc (Array sh e)
-> Acc (Array sh f)
-> Acc (Array sh g)
izipWith6 = izipWithInduction izipWith5
-- | Zip seven arrays with the given function that also takes the element
-- index, analogous to 'zipWith'.
--
izipWith7
:: (Shape sh, Elt a, Elt b, Elt c, Elt d, Elt e, Elt f, Elt g, Elt h)
=> (Exp sh -> Exp a -> Exp b -> Exp c -> Exp d -> Exp e -> Exp f -> Exp g -> Exp h)
-> Acc (Array sh a)
-> Acc (Array sh b)
-> Acc (Array sh c)
-> Acc (Array sh d)
-> Acc (Array sh e)
-> Acc (Array sh f)
-> Acc (Array sh g)
-> Acc (Array sh h)
izipWith7 = izipWithInduction izipWith6
-- | Zip eight arrays with the given function that also takes the element
-- index, analogous to 'zipWith'.
--
izipWith8
:: (Shape sh, Elt a, Elt b, Elt c, Elt d, Elt e, Elt f, Elt g, Elt h, Elt i)
=> (Exp sh -> Exp a -> Exp b -> Exp c -> Exp d -> Exp e -> Exp f -> Exp g -> Exp h -> Exp i)
-> Acc (Array sh a)
-> Acc (Array sh b)
-> Acc (Array sh c)
-> Acc (Array sh d)
-> Acc (Array sh e)
-> Acc (Array sh f)
-> Acc (Array sh g)
-> Acc (Array sh h)
-> Acc (Array sh i)
izipWith8 = izipWithInduction izipWith7
-- | Zip nine arrays with the given function that also takes the element index,
-- analogous to 'zipWith'.
--
izipWith9
:: (Shape sh, Elt a, Elt b, Elt c, Elt d, Elt e, Elt f, Elt g, Elt h, Elt i, Elt j)
=> (Exp sh -> Exp a -> Exp b -> Exp c -> Exp d -> Exp e -> Exp f -> Exp g -> Exp h -> Exp i -> Exp j)
-> Acc (Array sh a)
-> Acc (Array sh b)
-> Acc (Array sh c)
-> Acc (Array sh d)
-> Acc (Array sh e)
-> Acc (Array sh f)
-> Acc (Array sh g)
-> Acc (Array sh h)
-> Acc (Array sh i)
-> Acc (Array sh j)
izipWith9 = izipWithInduction izipWith8
-- | Combine the elements of two arrays pairwise. The shape of the result is the
-- intersection of the two argument shapes.
--
-- >>> let m1 = fromList (Z:.5:.10) [0..] :: Matrix Int
-- >>> let m2 = fromList (Z:.10:.5) [0..] :: Matrix Float
-- >>> run $ zip (use m1) (use m2)
-- Matrix (Z :. 5 :. 5)
-- [ (0,0.0), (1,1.0), (2,2.0), (3,3.0), (4,4.0),
-- (10,5.0), (11,6.0), (12,7.0), (13,8.0), (14,9.0),
-- (20,10.0), (21,11.0), (22,12.0), (23,13.0), (24,14.0),
-- (30,15.0), (31,16.0), (32,17.0), (33,18.0), (34,19.0),
-- (40,20.0), (41,21.0), (42,22.0), (43,23.0), (44,24.0)]
--
zip :: (Shape sh, Elt a, Elt b)
=> Acc (Array sh a)
-> Acc (Array sh b)
-> Acc (Array sh (a, b))
zip = zipWith T2
-- | Take three arrays and return an array of triples, analogous to zip.
--
zip3 :: (Shape sh, Elt a, Elt b, Elt c)
=> Acc (Array sh a)
-> Acc (Array sh b)
-> Acc (Array sh c)
-> Acc (Array sh (a, b, c))
zip3 = zipWith3 T3
-- | Take four arrays and return an array of quadruples, analogous to zip.
--
zip4 :: (Shape sh, Elt a, Elt b, Elt c, Elt d)
=> Acc (Array sh a)
-> Acc (Array sh b)
-> Acc (Array sh c)
-> Acc (Array sh d)
-> Acc (Array sh (a, b, c, d))
zip4 = zipWith4 T4
-- | Take five arrays and return an array of five-tuples, analogous to zip.
--
zip5 :: (Shape sh, Elt a, Elt b, Elt c, Elt d, Elt e)
=> Acc (Array sh a)
-> Acc (Array sh b)
-> Acc (Array sh c)
-> Acc (Array sh d)
-> Acc (Array sh e)
-> Acc (Array sh (a, b, c, d, e))
zip5 = zipWith5 T5
-- | Take six arrays and return an array of six-tuples, analogous to zip.
--
zip6 :: (Shape sh, Elt a, Elt b, Elt c, Elt d, Elt e, Elt f)
=> Acc (Array sh a)
-> Acc (Array sh b)
-> Acc (Array sh c)
-> Acc (Array sh d)
-> Acc (Array sh e)
-> Acc (Array sh f)
-> Acc (Array sh (a, b, c, d, e, f))
zip6 = zipWith6 T6
-- | Take seven arrays and return an array of seven-tuples, analogous to zip.
--
zip7 :: (Shape sh, Elt a, Elt b, Elt c, Elt d, Elt e, Elt f, Elt g)
=> Acc (Array sh a)
-> Acc (Array sh b)
-> Acc (Array sh c)
-> Acc (Array sh d)
-> Acc (Array sh e)
-> Acc (Array sh f)
-> Acc (Array sh g)
-> Acc (Array sh (a, b, c, d, e, f, g))
zip7 = zipWith7 T7
-- | Take seven arrays and return an array of seven-tuples, analogous to zip.
--
zip8 :: (Shape sh, Elt a, Elt b, Elt c, Elt d, Elt e, Elt f, Elt g, Elt h)
=> Acc (Array sh a)
-> Acc (Array sh b)
-> Acc (Array sh c)
-> Acc (Array sh d)
-> Acc (Array sh e)
-> Acc (Array sh f)
-> Acc (Array sh g)
-> Acc (Array sh h)
-> Acc (Array sh (a, b, c, d, e, f, g, h))
zip8 = zipWith8 T8
-- | Take seven arrays and return an array of seven-tuples, analogous to zip.
--
zip9 :: (Shape sh, Elt a, Elt b, Elt c, Elt d, Elt e, Elt f, Elt g, Elt h, Elt i)
=> Acc (Array sh a)
-> Acc (Array sh b)
-> Acc (Array sh c)
-> Acc (Array sh d)
-> Acc (Array sh e)
-> Acc (Array sh f)
-> Acc (Array sh g)
-> Acc (Array sh h)
-> Acc (Array sh i)
-> Acc (Array sh (a, b, c, d, e, f, g, h, i))
zip9 = zipWith9 T9
-- | The converse of 'zip', but the shape of the two results is identical to the
-- shape of the argument.
--
-- If the argument array is manifest in memory, 'unzip' is a no-op.
--
unzip :: (Shape sh, Elt a, Elt b)
=> Acc (Array sh (a, b))
-> (Acc (Array sh a), Acc (Array sh b))
unzip arr = (map fst arr, map snd arr)
-- | Take an array of triples and return three arrays, analogous to 'unzip'.
--
unzip3 :: (Shape sh, Elt a, Elt b, Elt c)
=> Acc (Array sh (a, b, c))
-> (Acc (Array sh a), Acc (Array sh b), Acc (Array sh c))
unzip3 xs = (map get1 xs, map get2 xs, map get3 xs)
where
get1 (T3 a _ _) = a
get2 (T3 _ b _) = b
get3 (T3 _ _ c) = c
-- | Take an array of quadruples and return four arrays, analogous to 'unzip'.
--
unzip4 :: (Shape sh, Elt a, Elt b, Elt c, Elt d)
=> Acc (Array sh (a, b, c, d))
-> (Acc (Array sh a), Acc (Array sh b), Acc (Array sh c), Acc (Array sh d))
unzip4 xs = (map get1 xs, map get2 xs, map get3 xs, map get4 xs)
where
get1 (T4 a _ _ _) = a
get2 (T4 _ b _ _) = b
get3 (T4 _ _ c _) = c
get4 (T4 _ _ _ d) = d
-- | Take an array of 5-tuples and return five arrays, analogous to 'unzip'.
--
unzip5 :: (Shape sh, Elt a, Elt b, Elt c, Elt d, Elt e)
=> Acc (Array sh (a, b, c, d, e))
-> (Acc (Array sh a), Acc (Array sh b), Acc (Array sh c), Acc (Array sh d), Acc (Array sh e))
unzip5 xs = (map get1 xs, map get2 xs, map get3 xs, map get4 xs, map get5 xs)
where
get1 (T5 a _ _ _ _) = a
get2 (T5 _ b _ _ _) = b
get3 (T5 _ _ c _ _) = c
get4 (T5 _ _ _ d _) = d
get5 (T5 _ _ _ _ e) = e
-- | Take an array of 6-tuples and return six arrays, analogous to 'unzip'.
--
unzip6 :: (Shape sh, Elt a, Elt b, Elt c, Elt d, Elt e, Elt f)
=> Acc (Array sh (a, b, c, d, e, f))
-> ( Acc (Array sh a), Acc (Array sh b), Acc (Array sh c)
, Acc (Array sh d), Acc (Array sh e), Acc (Array sh f))
unzip6 xs = (map get1 xs, map get2 xs, map get3 xs, map get4 xs, map get5 xs, map get6 xs)
where
get1 (T6 a _ _ _ _ _) = a
get2 (T6 _ b _ _ _ _) = b
get3 (T6 _ _ c _ _ _) = c
get4 (T6 _ _ _ d _ _) = d
get5 (T6 _ _ _ _ e _) = e
get6 (T6 _ _ _ _ _ f) = f
-- | Take an array of 7-tuples and return seven arrays, analogous to 'unzip'.
--
unzip7 :: (Shape sh, Elt a, Elt b, Elt c, Elt d, Elt e, Elt f, Elt g)
=> Acc (Array sh (a, b, c, d, e, f, g))
-> ( Acc (Array sh a), Acc (Array sh b), Acc (Array sh c)
, Acc (Array sh d), Acc (Array sh e), Acc (Array sh f)
, Acc (Array sh g))
unzip7 xs = ( map get1 xs, map get2 xs, map get3 xs
, map get4 xs, map get5 xs, map get6 xs
, map get7 xs )
where
get1 (T7 a _ _ _ _ _ _) = a
get2 (T7 _ b _ _ _ _ _) = b
get3 (T7 _ _ c _ _ _ _) = c
get4 (T7 _ _ _ d _ _ _) = d
get5 (T7 _ _ _ _ e _ _) = e
get6 (T7 _ _ _ _ _ f _) = f
get7 (T7 _ _ _ _ _ _ g) = g
-- | Take an array of 8-tuples and return eight arrays, analogous to 'unzip'.
--
unzip8 :: (Shape sh, Elt a, Elt b, Elt c, Elt d, Elt e, Elt f, Elt g, Elt h)
=> Acc (Array sh (a, b, c, d, e, f, g, h))
-> ( Acc (Array sh a), Acc (Array sh b), Acc (Array sh c)
, Acc (Array sh d), Acc (Array sh e), Acc (Array sh f)
, Acc (Array sh g), Acc (Array sh h) )
unzip8 xs = ( map get1 xs, map get2 xs, map get3 xs
, map get4 xs, map get5 xs, map get6 xs
, map get7 xs, map get8 xs )
where
get1 (T8 a _ _ _ _ _ _ _) = a
get2 (T8 _ b _ _ _ _ _ _) = b
get3 (T8 _ _ c _ _ _ _ _) = c
get4 (T8 _ _ _ d _ _ _ _) = d
get5 (T8 _ _ _ _ e _ _ _) = e
get6 (T8 _ _ _ _ _ f _ _) = f
get7 (T8 _ _ _ _ _ _ g _) = g
get8 (T8 _ _ _ _ _ _ _ h) = h
-- | Take an array of 9-tuples and return nine arrays, analogous to 'unzip'.
--
unzip9 :: (Shape sh, Elt a, Elt b, Elt c, Elt d, Elt e, Elt f, Elt g, Elt h, Elt i)
=> Acc (Array sh (a, b, c, d, e, f, g, h, i))
-> ( Acc (Array sh a), Acc (Array sh b), Acc (Array sh c)
, Acc (Array sh d), Acc (Array sh e), Acc (Array sh f)
, Acc (Array sh g), Acc (Array sh h), Acc (Array sh i))
unzip9 xs = ( map get1 xs, map get2 xs, map get3 xs
, map get4 xs, map get5 xs, map get6 xs
, map get7 xs, map get8 xs, map get9 xs )
where
get1 (T9 a _ _ _ _ _ _ _ _) = a
get2 (T9 _ b _ _ _ _ _ _ _) = b
get3 (T9 _ _ c _ _ _ _ _ _) = c
get4 (T9 _ _ _ d _ _ _ _ _) = d
get5 (T9 _ _ _ _ e _ _ _ _) = e
get6 (T9 _ _ _ _ _ f _ _ _) = f
get7 (T9 _ _ _ _ _ _ g _ _) = g
get8 (T9 _ _ _ _ _ _ _ h _) = h
get9 (T9 _ _ _ _ _ _ _ _ i) = i
-- Reductions
-- ----------
-- | Reduction of an array of arbitrary rank to a single scalar value. The first
-- argument needs to be an /associative/ function to enable efficient parallel
-- implementation. The initial element does not need to be an identity element.
--
-- >>> let vec = fromList (Z:.10) [0..] :: Vector Float
-- >>> run $ foldAll (+) 42 (use vec)
-- Scalar Z [87.0]
--
-- >>> let mat = fromList (Z:.5:.10) [0..] :: Matrix Float
-- >>> run $ foldAll (+) 0 (use mat)
-- Scalar Z [1225.0]
--
foldAll
:: (Shape sh, Elt a)
=> (Exp a -> Exp a -> Exp a)
-> Exp a
-> Acc (Array sh a)
-> Acc (Scalar a)
foldAll f e arr = fold f e (flatten arr)
-- | Variant of 'foldAll' that requires the reduced array to be non-empty and
-- does not need a default value. The first argument must be an /associative/
-- function.
--
fold1All
:: (Shape sh, Elt a)
=> (Exp a -> Exp a -> Exp a)
-> Acc (Array sh a)
-> Acc (Scalar a)
fold1All f arr = fold1 f (flatten arr)
-- | Segmented reduction along the innermost dimension of an array. The segment
-- descriptor specifies the lengths of the logical sub-arrays, each of which is
-- reduced independently. The innermost dimension must contain at least as many
-- elements as required by the segment descriptor (sum thereof).
--
-- >>> let seg = fromList (Z:.4) [1,4,0,3] :: Segments Int
-- >>> seg
-- Vector (Z :. 4) [1,4,0,3]
--
-- >>> let mat = fromList (Z:.5:.10) [0..] :: Matrix Int
-- >>> mat
-- Matrix (Z :. 5 :. 10)
-- [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
-- 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
-- 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
-- 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
-- 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
--
-- >>> run $ foldSeg (+) 0 (use mat) (use seg)
-- Matrix (Z :. 5 :. 4)
-- [ 0, 10, 0, 18,
-- 10, 50, 0, 48,
-- 20, 90, 0, 78,
-- 30, 130, 0, 108,
-- 40, 170, 0, 138]
--
foldSeg
:: forall sh e i. (Shape sh, Elt e, Elt i, i ~ EltR i, IsIntegral i)
=> (Exp e -> Exp e -> Exp e)
-> Exp e
-> Acc (Array (sh:.Int) e)
-> Acc (Segments i)
-> Acc (Array (sh:.Int) e)
foldSeg f z arr seg = foldSeg' f z arr (scanl plus zero seg)
where
(plus, zero) =
case integralType @i of
TypeInt{} -> ((+), 0)
TypeInt8{} -> ((+), 0)
TypeInt16{} -> ((+), 0)
TypeInt32{} -> ((+), 0)
TypeInt64{} -> ((+), 0)
TypeWord{} -> ((+), 0)
TypeWord8{} -> ((+), 0)
TypeWord16{} -> ((+), 0)
TypeWord32{} -> ((+), 0)
TypeWord64{} -> ((+), 0)
-- | Variant of 'foldSeg' that requires /all/ segments of the reduced array
-- to be non-empty, and does not need a default value. The segment
-- descriptor species the length of each of the logical sub-arrays.
--
fold1Seg
:: forall sh e i. (Shape sh, Elt e, Elt i, i ~ EltR i, IsIntegral i)
=> (Exp e -> Exp e -> Exp e)
-> Acc (Array (sh:.Int) e)
-> Acc (Segments i)
-> Acc (Array (sh:.Int) e)
fold1Seg f arr seg = fold1Seg' f arr (scanl plus zero seg)
where
plus :: Exp i -> Exp i -> Exp i
zero :: Exp i
(plus, zero) =
case integralType @(EltR i) of
TypeInt{} -> ((+), 0)
TypeInt8{} -> ((+), 0)
TypeInt16{} -> ((+), 0)
TypeInt32{} -> ((+), 0)
TypeInt64{} -> ((+), 0)
TypeWord{} -> ((+), 0)
TypeWord8{} -> ((+), 0)
TypeWord16{} -> ((+), 0)
TypeWord32{} -> ((+), 0)
TypeWord64{} -> ((+), 0)
-- Specialised reductions
-- ----------------------
--
-- Leave the results of these as scalar arrays to make it clear that these are
-- array computations, and thus can not be nested.
-- | Check if all elements along the innermost dimension satisfy a predicate.
--
-- >>> let mat = fromList (Z :. 4 :. 10) [1,2,3,4,5,6,7,8,9,10,1,1,1,1,1,2,2,2,2,2,2,4,6,8,10,12,14,16,18,20,1,3,5,7,9,11,13,15,17,19] :: Matrix Int
-- >>> mat
-- Matrix (Z :. 4 :. 10)
-- [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
-- 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
-- 2, 4, 6, 8, 10, 12, 14, 16, 18, 20,
-- 1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
--
-- >>> run $ all even (use mat)
-- Vector (Z :. 4) [False,False,True,False]
--
all :: (Shape sh, Elt e)
=> (Exp e -> Exp Bool)
-> Acc (Array (sh:.Int) e)
-> Acc (Array sh Bool)
all f = and . map f
-- | Check if any element along the innermost dimension satisfies the predicate.
--
-- >>> let mat = fromList (Z :. 4 :. 10) [1,2,3,4,5,6,7,8,9,10,1,1,1,1,1,2,2,2,2,2,2,4,6,8,10,12,14,16,18,20,1,3,5,7,9,11,13,15,17,19] :: Matrix Int
-- >>> mat
-- Matrix (Z :. 4 :. 10)
-- [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
-- 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
-- 2, 4, 6, 8, 10, 12, 14, 16, 18, 20,
-- 1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
--
-- >>> run $ any even (use mat)
-- Vector (Z :. 4) [True,True,True,False]
--
any :: (Shape sh, Elt e)
=> (Exp e -> Exp Bool)
-> Acc (Array (sh:.Int) e)
-> Acc (Array sh Bool)
any f = or . map f
-- | Check if all elements along the innermost dimension are 'True'.
--
and :: Shape sh
=> Acc (Array (sh:.Int) Bool)
-> Acc (Array sh Bool)
and = fold (&&) True_
-- | Check if any element along the innermost dimension is 'True'.
--
or :: Shape sh
=> Acc (Array (sh:.Int) Bool)
-> Acc (Array sh Bool)
or = fold (||) False_
-- | Compute the sum of elements along the innermost dimension of the array. To
-- find the sum of the entire array, 'flatten' it first.
--
-- >>> let mat = fromList (Z:.2:.5) [0..] :: Matrix Int
-- >>> run $ sum (use mat)
-- Vector (Z :. 2) [10,35]
--
sum :: (Shape sh, Num e)
=> Acc (Array (sh:.Int) e)
-> Acc (Array sh e)
sum = fold (+) 0
-- | Compute the product of the elements along the innermost dimension of the
-- array. To find the product of the entire array, 'flatten' it first.
--
-- >>> let mat = fromList (Z:.2:.5) [0..] :: Matrix Int
-- >>> run $ product (use mat)
-- Vector (Z :. 2) [0,15120]
--
product
:: (Shape sh, Num e)
=> Acc (Array (sh:.Int) e)
-> Acc (Array sh e)
product = fold (*) 1
-- | Yield the minimum element along the innermost dimension of the array. To
-- find find the minimum element of the entire array, 'flatten' it first.
--
-- The array must not be empty. See also 'fold1'.
--
-- >>> let mat = fromList (Z :. 3 :. 4) [1,4,3,8, 0,2,8,4, 7,9,8,8] :: Matrix Int
-- >>> mat
-- Matrix (Z :. 3 :. 4)
-- [ 1, 4, 3, 8,
-- 0, 2, 8, 4,
-- 7, 9, 8, 8]
--
-- >>> run $ minimum (use mat)
-- Vector (Z :. 3) [1,0,7]
--
minimum
:: (Shape sh, Ord e)
=> Acc (Array (sh:.Int) e)
-> Acc (Array sh e)
minimum = fold1 min
-- | Yield the maximum element along the innermost dimension of the array. To
-- find the maximum element of the entire array, 'flatten' it first.
--
-- The array must not be empty. See also 'fold1'.
--
-- >>> let mat = fromList (Z :. 3 :. 4) [1,4,3,8, 0,2,8,4, 7,9,8,8] :: Matrix Int
-- >>> mat
-- Matrix (Z :. 3 :. 4)
-- [ 1, 4, 3, 8,
-- 0, 2, 8, 4,
-- 7, 9, 8, 8]
--
-- >>> run $ maximum (use mat)
-- Vector (Z :. 3) [8,8,9]
--
maximum
:: (Shape sh, Ord e)
=> Acc (Array (sh:.Int) e)
-> Acc (Array sh e)
maximum = fold1 max
-- Composite scans
-- ---------------
-- | Left-to-right pre-scan (aka exclusive scan). As for 'scan', the first
-- argument must be an /associative/ function. Denotationally, we have:
--
-- > prescanl f e = afst . scanl' f e
--
-- >>> let vec = fromList (Z:.10) [1..10] :: Vector Int
-- >>> run $ prescanl (+) 0 (use vec)
-- Vector (Z :. 10) [0,1,3,6,10,15,21,28,36,45]
--
prescanl
:: (Shape sh, Elt a)
=> (Exp a -> Exp a -> Exp a)
-> Exp a
-> Acc (Array (sh:.Int) a)
-> Acc (Array (sh:.Int) a)
prescanl f e = afst . scanl' f e
-- | Left-to-right post-scan, a variant of 'scanl1' with an initial value. As
-- with 'scanl1', the array must not be empty. Denotationally, we have:
--
-- > postscanl f e = map (e `f`) . scanl1 f
--
-- >>> let vec = fromList (Z:.10) [1..10] :: Vector Int
-- >>> run $ postscanl (+) 42 (use vec)
-- Vector (Z :. 10) [43,45,48,52,57,63,70,78,87,97]
--
postscanl
:: (Shape sh, Elt a)
=> (Exp a -> Exp a -> Exp a)
-> Exp a
-> Acc (Array (sh:.Int) a)
-> Acc (Array (sh:.Int) a)
postscanl f e = map (e `f`) . scanl1 f
-- | Right-to-left pre-scan (aka exclusive scan). As for 'scan', the first
-- argument must be an /associative/ function. Denotationally, we have:
--
-- > prescanr f e = afst . scanr' f e
--
prescanr
:: (Shape sh, Elt a)
=> (Exp a -> Exp a -> Exp a)
-> Exp a
-> Acc (Array (sh:.Int) a)
-> Acc (Array (sh:.Int) a)
prescanr f e = afst . scanr' f e
-- | Right-to-left postscan, a variant of 'scanr1' with an initial value.
-- Denotationally, we have:
--
-- > postscanr f e = map (e `f`) . scanr1 f
--
postscanr
:: (Shape sh, Elt a)
=> (Exp a -> Exp a -> Exp a)
-> Exp a
-> Acc (Array (sh:.Int) a)
-> Acc (Array (sh:.Int) a)
postscanr f e = map (`f` e) . scanr1 f
-- Segmented scans
-- ---------------
-- | Segmented version of 'scanl' along the innermost dimension of an array. The
-- innermost dimension must have at least as many elements as the sum of the
-- segment descriptor.
--
-- >>> let seg = fromList (Z:.4) [1,4,0,3] :: Segments Int
-- >>> seg
-- Vector (Z :. 4) [1,4,0,3]
--
-- >>> let mat = fromList (Z:.5:.10) [0..] :: Matrix Int
-- >>> mat
-- Matrix (Z :. 5 :. 10)
-- [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
-- 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
-- 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
-- 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
-- 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
--
-- >>> run $ scanlSeg (+) 0 (use mat) (use seg)
-- Matrix (Z :. 5 :. 12)
-- [ 0, 0, 0, 1, 3, 6, 10, 0, 0, 5, 11, 18,
-- 0, 10, 0, 11, 23, 36, 50, 0, 0, 15, 31, 48,
-- 0, 20, 0, 21, 43, 66, 90, 0, 0, 25, 51, 78,
-- 0, 30, 0, 31, 63, 96, 130, 0, 0, 35, 71, 108,
-- 0, 40, 0, 41, 83, 126, 170, 0, 0, 45, 91, 138]
--
scanlSeg
:: forall sh e i. (Shape sh, Slice sh, Elt e, Integral i, Bits i, FromIntegral i Int)
=> (Exp e -> Exp e -> Exp e)
-> Exp e
-> Acc (Array (sh:.Int) e)
-> Acc (Segments i)
-> Acc (Array (sh:.Int) e)
scanlSeg f z arr seg =
if null arr || null flags
then fill (sh ::. sz + length seg) z
else scanl1Seg f arr' seg'
where
-- Segmented exclusive scan is implemented by first injecting the seed
-- element at the head of each segment, and then performing a segmented
-- inclusive scan.
--
-- This is done by creating a vector entirely of the seed element, and
-- overlaying the input data in all places other than at the start of
-- a segment.
--
sh ::. sz = shape arr
seg' = map (+1) seg
arr' = permute const
(fill (sh ::. sz + length seg) z)
(\(sx ::. i) -> Just_ (sx ::. i + fromIntegral (inc ! I1 i)))