-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathLanguage.hs
More file actions
1516 lines (1390 loc) · 50.2 KB
/
Language.hs
File metadata and controls
1516 lines (1390 loc) · 50.2 KB
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 ConstraintKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE ViewPatterns #-}
-- |
-- Module : Data.Array.Accelerate.Language
-- Copyright : [2008..2020] The Accelerate Team
-- License : BSD3
--
-- Maintainer : Trevor L. McDonell <trevor.mcdonell@gmail.com>
-- Stability : experimental
-- Portability : non-portable (GHC extensions)
--
-- We use the dictionary view of overloaded operations (such as arithmetic and
-- bit manipulation) to reify such expressions. With non-overloaded
-- operations (such as, the logical connectives) and partially overloaded
-- operations (such as comparisons), we use the standard operator names with a
-- \'*\' attached. We keep the standard alphanumeric names as they can be
-- easily qualified.
--
module Data.Array.Accelerate.Language (
-- * Array construction
use, unit, replicate, generate,
-- * Shape manipulation
reshape,
-- * Extraction of sub-arrays
slice,
-- * Map-like functions
map, zipWith,
-- -- * Sequence collection
-- collect,
-- -- * Sequence producers
-- streamIn, toSeq,
-- -- * Sequence transducers
-- mapSeq, zipWithSeq, scanSeq,
-- -- * Sequence consumers
-- foldSeq, foldSeqFlatten,
-- * Reductions
fold, fold1, foldSeg', fold1Seg',
-- * Scan functions
scanl, scanl', scanl1, scanr, scanr', scanr1,
-- * Permutations
permute, backpermute,
-- * Stencil operations
stencil, stencil2,
-- ** Stencil specification
Boundary, Stencil,
clamp, mirror, wrap, function,
-- ** Common stencil types
Stencil3, Stencil5, Stencil7, Stencil9,
Stencil3x3, Stencil5x3, Stencil3x5, Stencil5x5,
Stencil3x3x3, Stencil5x3x3, Stencil3x5x3, Stencil3x3x5, Stencil5x5x3, Stencil5x3x5,
Stencil3x5x5, Stencil5x5x5,
-- * Foreign functions
foreignAcc,
foreignExp,
-- * Pipelining
(>->),
-- * Index construction and destruction
indexHead, indexTail, toIndex, fromIndex,
intersect, union,
-- * Flow-control
acond, awhile,
cond, while,
-- * Array operations with a scalar result
(!), (!!), shape, size, shapeSize,
-- * Numeric functions
subtract, even, odd, gcd, lcm, (^), (^^),
-- * Conversions
ord, chr, boolToInt, bitcast,
Stencil1,Stencil1x3,Stencil1x5,Stencil1x1,Stencil3x1,Stencil5x1) where
import Data.Array.Accelerate.AST ( PrimFun(..) )
import Data.Array.Accelerate.Pattern
import Data.Array.Accelerate.Representation.Array ( ArrayR(..) )
import Data.Array.Accelerate.Representation.Shape ( ShapeR(..) )
import Data.Array.Accelerate.Representation.Type
import Data.Array.Accelerate.Smart hiding ( arraysR )
import Data.Array.Accelerate.Sugar.Array ( Arrays(..), Array, Scalar, Segments, arrayR )
import Data.Array.Accelerate.Sugar.Elt
import Data.Array.Accelerate.Sugar.Foreign
import Data.Array.Accelerate.Sugar.Shape ( Shape(..), Slice(..), (:.) )
import Data.Array.Accelerate.Type
import qualified Data.Array.Accelerate.Representation.Array as R
import Data.Array.Accelerate.Classes.Eq
import Data.Array.Accelerate.Classes.Fractional
import Data.Array.Accelerate.Classes.Integral
import Data.Array.Accelerate.Classes.Num
import Data.Array.Accelerate.Classes.Ord
import Prelude ( ($), (.), Maybe(..), Char )
import Data.Array.Accelerate.Sugar.Stencil
-- $setup
-- >>> :seti -XFlexibleContexts
-- >>> :seti -XScopedTypeVariables
-- >>> :seti -XTypeOperators
-- >>> :seti -XViewPatterns
-- >>> import Data.Array.Accelerate
-- >>> import Data.Array.Accelerate.Data.Maybe
-- >>> import Data.Array.Accelerate.Interpreter
-- >>> :{
-- let runExp :: Elt e => Exp e -> e
-- runExp e = indexArray (run (unit e)) Z
-- :}
-- Array introduction
-- ------------------
-- | Make an array from vanilla Haskell available for processing within embedded
-- Accelerate computations.
--
-- Depending upon which backend is used to eventually execute array
-- computations, 'use' may entail data transfer (e.g. to a GPU).
--
-- 'use' is overloaded so that it can accept tuples of 'Arrays':
--
-- >>> let vec = fromList (Z:.10) [0..] :: Vector Int
-- >>> vec
-- Vector (Z :. 10) [0,1,2,3,4,5,6,7,8,9]
--
-- >>> 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]
--
-- >>> let vec' = use vec :: Acc (Vector Int)
-- >>> let mat' = use mat :: Acc (Matrix Int)
-- >>> let tup = use (vec, mat) :: Acc (Vector Int, Matrix Int)
--
use :: forall arrays. Arrays arrays => arrays -> Acc arrays
use = Acc . use' (arraysR @arrays) . fromArr
where
use' :: R.ArraysR a -> a -> SmartAcc a
use' TupRunit () = SmartAcc $ Anil
use' (TupRsingle repr@ArrayR{}) a = SmartAcc $ Use repr a
use' (TupRpair r1 r2) (a1, a2) = SmartAcc $ use' r1 a1 `Apair` use' r2 a2
-- | Construct a singleton (one element) array from a scalar value (or tuple of
-- scalar values).
--
unit :: forall e. Elt e => Exp e -> Acc (Scalar e)
unit (Exp e) = Acc $ SmartAcc $ Unit (eltR @e) e
-- | Replicate an array across one or more dimensions as specified by the
-- /generalised/ array index provided as the first argument.
--
-- For example, given the following vector:
--
-- >>> let vec = fromList (Z:.10) [0..] :: Vector Int
-- >>> vec
-- Vector (Z :. 10) [0,1,2,3,4,5,6,7,8,9]
--
-- ...we can replicate these elements to form a two-dimensional array either by
-- replicating those elements as new rows:
--
-- >>> run $ replicate (constant (Z :. (4::Int) :. All)) (use vec)
-- Matrix (Z :. 4 :. 10)
-- [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
-- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
-- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
-- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
--
-- ...or as columns:
--
-- >>> run $ replicate (lift (Z :. All :. (4::Int))) (use vec)
-- Matrix (Z :. 10 :. 4)
-- [ 0, 0, 0, 0,
-- 1, 1, 1, 1,
-- 2, 2, 2, 2,
-- 3, 3, 3, 3,
-- 4, 4, 4, 4,
-- 5, 5, 5, 5,
-- 6, 6, 6, 6,
-- 7, 7, 7, 7,
-- 8, 8, 8, 8,
-- 9, 9, 9, 9]
--
-- Replication along more than one dimension is also possible. Here we replicate
-- twice across the first dimension and three times across the third dimension:
--
-- >>> run $ replicate (constant (Z :. (2::Int) :. All :. (3::Int))) (use vec)
-- Array (Z :. 2 :. 10 :. 3) [0,0,0,1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,9,0,0,0,1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,9]
--
-- The marker 'Any' can be used in the slice specification to match against some
-- arbitrary dimension. For example, here 'Any' matches against whatever shape
-- type variable @sh@ takes.
--
-- >>> :{
-- let rep0 :: (Shape sh, Elt e) => Exp Int -> Acc (Array sh e) -> Acc (Array (sh :. Int) e)
-- rep0 n a = replicate (lift (Any :. n)) a
-- :}
--
-- >>> let x = unit 42 :: Acc (Scalar Int)
-- >>> run $ rep0 10 x
-- Vector (Z :. 10) [42,42,42,42,42,42,42,42,42,42]
--
-- >>> run $ rep0 5 (use vec)
-- Matrix (Z :. 10 :. 5)
-- [ 0, 0, 0, 0, 0,
-- 1, 1, 1, 1, 1,
-- 2, 2, 2, 2, 2,
-- 3, 3, 3, 3, 3,
-- 4, 4, 4, 4, 4,
-- 5, 5, 5, 5, 5,
-- 6, 6, 6, 6, 6,
-- 7, 7, 7, 7, 7,
-- 8, 8, 8, 8, 8,
-- 9, 9, 9, 9, 9]
--
-- Of course, 'Any' and 'All' can be used together.
--
-- >>> :{
-- let rep1 :: (Shape sh, Elt e) => Exp Int -> Acc (Array (sh :. Int) e) -> Acc (Array (sh :. Int :. Int) e)
-- rep1 n a = replicate (lift (Any :. n :. All)) a
-- :}
--
-- >>> run $ rep1 5 (use vec)
-- Matrix (Z :. 5 :. 10)
-- [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
-- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
-- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
-- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
-- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
--
replicate
:: forall slix e.
(Slice slix, Elt e)
=> Exp slix
-> Acc (Array (SliceShape slix) e)
-> Acc (Array (FullShape slix) e)
replicate = Acc $$ applyAcc (Replicate $ sliceIndex @slix)
-- | Construct a new array by applying a function to each index.
--
-- For example, the following will generate a one-dimensional array
-- (`Vector`) of three floating point numbers:
--
-- >>> run $ generate (I1 3) (\_ -> 1.2) :: Vector Float
-- Vector (Z :. 3) [1.2,1.2,1.2]
--
-- Or equivalently:
--
-- >>> run $ fill (constant (Z :. 3)) 1.2 :: Vector Float
-- Vector (Z :. 3) [1.2,1.2,1.2]
--
-- The following will create a vector with the elements @[1..10]@:
--
-- >>> run $ generate (I1 10) (\(I1 i) -> i + 1) :: Vector Int
-- Vector (Z :. 10) [1,2,3,4,5,6,7,8,9,10]
--
-- [/NOTE:/]
--
-- Using 'generate', it is possible to introduce nested data parallelism, which
-- will cause the program to fail.
--
-- If the index given by the scalar function is then used to dispatch further
-- parallel work, whose result is returned into 'Exp' terms by array indexing
-- operations such as ('!') or 'Data.Array.Accelerate.Prelude.the', the program
-- will fail with the error:
-- @.\/Data\/Array\/Accelerate\/Trafo\/Sharing.hs:447 (convertSharingExp): inconsistent valuation \@ shared \'Exp\' tree ...@.
--
generate
:: forall sh a.
(Shape sh, Elt a)
=> Exp sh
-> (Exp sh -> Exp a)
-> Acc (Array sh a)
generate = Acc $$ applyAcc (Generate $ arrayR @sh @a)
-- Shape manipulation
-- ------------------
-- | Change the shape of an array without altering its contents. The 'size' of
-- the source and result arrays must be identical.
--
-- > precondition: shapeSize sh == shapeSize sh'
--
-- If the argument array is manifest in memory, 'reshape' is a no-op. If the
-- argument is to be fused into a subsequent operation, 'reshape' corresponds to
-- an index transformation in the fused code.
--
reshape
:: forall sh sh' e.
(Shape sh, Shape sh', Elt e)
=> Exp sh
-> Acc (Array sh' e)
-> Acc (Array sh e)
reshape = Acc $$ applyAcc (Reshape $ shapeR @sh)
-- Extraction of sub-arrays
-- ------------------------
-- | Index an array with a /generalised/ array index, supplied as the second
-- argument. The result is a new array (possibly a singleton) containing the
-- selected dimensions ('All's) in their entirety.
--
-- 'slice' is the opposite of 'replicate', and can be used to /cut out/ entire
-- dimensions. For example, for the two dimensional array 'mat':
--
-- >>> 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]
--
-- ...will can select a specific row to yield a one dimensional result by fixing
-- the row index (2) while allowing the column index to vary (via 'All'):
--
-- >>> run $ slice (use mat) (constant (Z :. (2::Int) :. All))
-- Vector (Z :. 10) [20,21,22,23,24,25,26,27,28,29]
--
-- A fully specified index (with no 'All's) returns a single element (zero
-- dimensional array).
--
-- >>> run $ slice (use mat) (constant (Z :. 4 :. 2 :: DIM2))
-- Scalar Z [42]
--
-- The marker 'Any' can be used in the slice specification to match against some
-- arbitrary (lower) dimension. Here 'Any' matches whatever shape type variable
-- @sh@ takes:
--
-- >>> :{
-- let
-- sl0 :: (Shape sh, Elt e) => Acc (Array (sh:.Int) e) -> Exp Int -> Acc (Array sh e)
-- sl0 a n = slice a (lift (Any :. n))
-- :}
--
-- >>> let vec = fromList (Z:.10) [0..] :: Vector Int
-- >>> run $ sl0 (use vec) 4
-- Scalar Z [4]
--
-- >>> run $ sl0 (use mat) 4
-- Vector (Z :. 5) [4,14,24,34,44]
--
-- Of course, 'Any' and 'All' can be used together.
--
-- >>> :{
-- let sl1 :: (Shape sh, Elt e) => Acc (Array (sh:.Int:.Int) e) -> Exp Int -> Acc (Array (sh:.Int) e)
-- sl1 a n = slice a (lift (Any :. n :. All))
-- :}
--
-- >>> run $ sl1 (use mat) 4
-- Vector (Z :. 10) [40,41,42,43,44,45,46,47,48,49]
--
-- >>> let cube = fromList (Z:.3:.4:.5) [0..] :: Array DIM3 Int
-- >>> cube
-- Array (Z :. 3 :. 4 :. 5) [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,50,51,52,53,54,55,56,57,58,59]
--
-- >>> run $ sl1 (use cube) 2
-- Matrix (Z :. 3 :. 5)
-- [ 10, 11, 12, 13, 14,
-- 30, 31, 32, 33, 34,
-- 50, 51, 52, 53, 54]
--
slice :: forall slix e.
(Slice slix, Elt e)
=> Acc (Array (FullShape slix) e)
-> Exp slix
-> Acc (Array (SliceShape slix) e)
slice = Acc $$ applyAcc (Slice $ sliceIndex @slix)
-- Map-like functions
-- ------------------
-- | Apply the given function element-wise to an array. Denotationally we have:
--
-- > map f [x1, x2, ... xn] = [f x1, f x2, ... f xn]
--
-- >>> let xs = fromList (Z:.10) [0..] :: Vector Int
-- >>> xs
-- Vector (Z :. 10) [0,1,2,3,4,5,6,7,8,9]
--
-- >>> run $ map (+1) (use xs)
-- Vector (Z :. 10) [1,2,3,4,5,6,7,8,9,10]
--
map :: forall sh a b.
(Shape sh, Elt a, Elt b)
=> (Exp a -> Exp b)
-> Acc (Array sh a)
-> Acc (Array sh b)
map = Acc $$ applyAcc (Map (eltR @a) (eltR @b))
-- | Apply the given binary function element-wise to the two arrays. The extent
-- of the resulting array is the intersection of the extents of the two source
-- arrays.
--
-- >>> let xs = fromList (Z:.3:.5) [0..] :: Matrix Int
-- >>> xs
-- Matrix (Z :. 3 :. 5)
-- [ 0, 1, 2, 3, 4,
-- 5, 6, 7, 8, 9,
-- 10, 11, 12, 13, 14]
--
-- >>> let ys = fromList (Z:.5:.10) [1..] :: Matrix Int
-- >>> ys
-- Matrix (Z :. 5 :. 10)
-- [ 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]
--
-- >>> run $ zipWith (+) (use xs) (use ys)
-- Matrix (Z :. 3 :. 5)
-- [ 1, 3, 5, 7, 9,
-- 16, 18, 20, 22, 24,
-- 31, 33, 35, 37, 39]
--
zipWith :: forall sh a b c.
(Shape sh, Elt a, Elt b, Elt c)
=> (Exp a -> Exp b -> Exp c)
-> Acc (Array sh a)
-> Acc (Array sh b)
-> Acc (Array sh c)
zipWith = Acc $$$ applyAcc (ZipWith (eltR @a) (eltR @b) (eltR @c))
-- Reductions
-- ----------
-- | Reduction of the innermost dimension of an array of arbitrary rank.
--
-- The shape of the result obeys the property:
--
-- > shape (fold f z xs) == indexTail (shape xs)
--
-- The first argument needs to be an /associative/ function to enable an
-- efficient parallel implementation. The initial element does not need to be an
-- identity element of the combination function.
--
-- >>> 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 $ fold (+) 42 (use mat)
-- Vector (Z :. 5) [87,187,287,387,487]
--
-- Reductions with non-commutative operators are supported. For example, the
-- following computes the maximum segment sum problem along each innermost
-- dimension of the array.
--
-- <https://en.wikipedia.org/wiki/Maximum_subarray_problem>
--
-- >>> :{
-- let maximumSegmentSum
-- :: forall sh e. (Shape sh, Num e, Ord e)
-- => Acc (Array (sh :. Int) e)
-- -> Acc (Array sh e)
-- maximumSegmentSum
-- = map (\(T4 x _ _ _) -> x)
-- . fold1 f
-- . map g
-- where
-- f :: (Num a, Ord a) => Exp (a,a,a,a) -> Exp (a,a,a,a) -> Exp (a,a,a,a)
-- f x y =
-- let T4 mssx misx mcsx tsx = x
-- T4 mssy misy mcsy tsy = y
-- in
-- T4 (mssx `max` (mssy `max` (mcsx+misy)))
-- (misx `max` (tsx+misy))
-- (mcsy `max` (mcsx+tsy))
-- (tsx+tsy)
-- --
-- g :: (Num a, Ord a) => Exp a -> Exp (a,a,a,a)
-- g x = let y = max x 0
-- in T4 y y y x
-- :}
--
-- >>> let vec = fromList (Z:.10) [-2,1,-3,4,-1,2,1,-5,4,0] :: Vector Int
-- >>> run $ maximumSegmentSum (use vec)
-- Scalar Z [6]
--
-- See also 'Data.Array.Accelerate.Data.Fold.Fold', which can be a useful way to
-- compute multiple results from a single reduction.
--
fold :: forall sh a.
(Shape sh, Elt a)
=> (Exp a -> Exp a -> Exp a)
-> Exp a
-> Acc (Array (sh:.Int) a)
-> Acc (Array sh a)
fold f (Exp x) = Acc . applyAcc (Fold (eltR @a) (unExpBinaryFunction f) (Just x))
-- | Variant of 'fold' that requires the innermost dimension of the array to be
-- non-empty and doesn't need an default value.
--
-- The shape of the result obeys the property:
--
-- > shape (fold f z xs) == indexTail (shape xs)
--
-- The first argument needs to be an /associative/ function to enable an
-- efficient parallel implementation, but does not need to be commutative.
--
fold1 :: forall sh a.
(Shape sh, Elt a)
=> (Exp a -> Exp a -> Exp a)
-> Acc (Array (sh:.Int) a)
-> Acc (Array sh a)
fold1 f = Acc . applyAcc (Fold (eltR @a) (unExpBinaryFunction f) Nothing)
-- | Segmented reduction along the innermost dimension of an array. The
-- segment descriptor specifies the starting index (offset) along the
-- innermost dimension to the beginning of each logical sub-array.
--
-- The value in the output array at index i is the reduction of values
-- between the indices of the segment descriptor at index i and (i+1).
--
-- We have that:
--
-- > foldSeg f z xs seg == foldSeg' f z xs (scanl (+) 0 seg)
--
-- @since 1.3.0.0
--
foldSeg'
:: forall sh a i.
(Shape sh, Elt a, Elt i, IsIntegral i, i ~ EltR i)
=> (Exp a -> Exp a -> Exp a)
-> Exp a
-> Acc (Array (sh:.Int) a)
-> Acc (Segments i)
-> Acc (Array (sh:.Int) a)
foldSeg' f (Exp x) = Acc $$ applyAcc (FoldSeg (integralType @i) (eltR @a) (unExpBinaryFunction f) (Just x))
-- | Variant of 'foldSeg'' that requires /all/ segments of the reduced
-- array to be non-empty, and doesn't need a default value. The segment
-- descriptor specifies the offset to the beginning of each of the logical
-- sub-arrays.
--
-- @since 1.3.0.0
--
fold1Seg'
:: forall sh a i.
(Shape sh, Elt a, Elt i, IsIntegral i, i ~ EltR i)
=> (Exp a -> Exp a -> Exp a)
-> Acc (Array (sh:.Int) a)
-> Acc (Segments i)
-> Acc (Array (sh:.Int) a)
fold1Seg' f = Acc $$ applyAcc (FoldSeg (integralType @i) (eltR @a) (unExpBinaryFunction f) Nothing)
-- Scan functions
-- --------------
-- | Data.List style left-to-right scan along the innermost dimension of an
-- arbitrary rank array. The first argument needs to be an /associative/
-- function to enable efficient parallel implementation. The initial value
-- (second argument) may be arbitrary.
--
-- >>> let vec = fromList (Z :. 10) [0..] :: Vector Int
-- >>> run $ scanl (+) 10 (use vec)
-- Vector (Z :. 11) [10,10,11,13,16,20,25,31,38,46,55]
--
-- >>> let mat = fromList (Z :. 4 :. 10) [0..] :: Matrix Int
-- >>> run $ scanl (+) 0 (use mat)
-- Matrix (Z :. 4 :. 11)
-- [ 0, 0, 1, 3, 6, 10, 15, 21, 28, 36, 45,
-- 0, 10, 21, 33, 46, 60, 75, 91, 108, 126, 145,
-- 0, 20, 41, 63, 86, 110, 135, 161, 188, 216, 245,
-- 0, 30, 61, 93, 126, 160, 195, 231, 268, 306, 345]
--
scanl :: forall sh a.
(Shape sh, Elt a)
=> (Exp a -> Exp a -> Exp a)
-> Exp a
-> Acc (Array (sh:.Int) a)
-> Acc (Array (sh:.Int) a)
scanl f (Exp x) (Acc a) = Acc $ SmartAcc $ Scan LeftToRight (eltR @a) (unExpBinaryFunction f) (Just x) a
-- | Variant of 'scanl', where the last element (final reduction result) along
-- each dimension is returned separately. Denotationally we have:
--
-- > scanl' f e arr = (init res, unit (res!len))
-- > where
-- > len = shape arr
-- > res = scanl f e arr
--
-- >>> let vec = fromList (Z:.10) [0..] :: Vector Int
-- >>> let (res,sum) = run $ scanl' (+) 0 (use vec)
-- >>> res
-- Vector (Z :. 10) [0,0,1,3,6,10,15,21,28,36]
-- >>> sum
-- Scalar Z [45]
--
-- >>> let mat = fromList (Z:.4:.10) [0..] :: Matrix Int
-- >>> let (res,sums) = run $ scanl' (+) 0 (use mat)
-- >>> res
-- Matrix (Z :. 4 :. 10)
-- [ 0, 0, 1, 3, 6, 10, 15, 21, 28, 36,
-- 0, 10, 21, 33, 46, 60, 75, 91, 108, 126,
-- 0, 20, 41, 63, 86, 110, 135, 161, 188, 216,
-- 0, 30, 61, 93, 126, 160, 195, 231, 268, 306]
-- >>> sums
-- Vector (Z :. 4) [45,145,245,345]
--
scanl' :: forall sh a.
(Shape sh, Elt a)
=> (Exp a -> Exp a -> Exp a)
-> Exp a
-> Acc (Array (sh:.Int) a)
-> Acc (Array (sh:.Int) a, Array sh a)
scanl' = Acc . mkPairToTuple $$$ applyAcc (Scan' LeftToRight $ eltR @a)
-- | Data.List style left-to-right scan along the innermost dimension without an
-- initial value (aka inclusive scan). The innermost dimension of the array must
-- not be empty. The first argument must be an /associative/ function.
--
-- >>> let mat = fromList (Z:.4:.10) [0..] :: Matrix Int
-- >>> run $ scanl1 (+) (use mat)
-- Matrix (Z :. 4 :. 10)
-- [ 0, 1, 3, 6, 10, 15, 21, 28, 36, 45,
-- 10, 21, 33, 46, 60, 75, 91, 108, 126, 145,
-- 20, 41, 63, 86, 110, 135, 161, 188, 216, 245,
-- 30, 61, 93, 126, 160, 195, 231, 268, 306, 345]
--
scanl1 :: forall sh a.
(Shape sh, Elt a)
=> (Exp a -> Exp a -> Exp a)
-> Acc (Array (sh:.Int) a)
-> Acc (Array (sh:.Int) a)
scanl1 f (Acc a) = Acc $ SmartAcc $ Scan LeftToRight (eltR @a) (unExpBinaryFunction f) Nothing a
-- | Right-to-left variant of 'scanl'.
--
scanr :: forall sh a.
(Shape sh, Elt a)
=> (Exp a -> Exp a -> Exp a)
-> Exp a
-> Acc (Array (sh:.Int) a)
-> Acc (Array (sh:.Int) a)
scanr f (Exp x) (Acc a) = Acc $ SmartAcc $ Scan RightToLeft (eltR @a) (unExpBinaryFunction f) (Just x) a
-- | Right-to-left variant of 'scanl''.
--
scanr' :: forall sh a.
(Shape sh, Elt a)
=> (Exp a -> Exp a -> Exp a)
-> Exp a
-> Acc (Array (sh:.Int) a)
-> Acc (Array (sh:.Int) a, Array sh a)
scanr' = Acc . mkPairToTuple $$$ applyAcc (Scan' RightToLeft $ eltR @a)
-- | Right-to-left variant of 'scanl1'.
--
scanr1 :: forall sh a.
(Shape sh, Elt a)
=> (Exp a -> Exp a -> Exp a)
-> Acc (Array (sh:.Int) a)
-> Acc (Array (sh:.Int) a)
scanr1 f (Acc a) = Acc $ SmartAcc $ Scan RightToLeft (eltR @a) (unExpBinaryFunction f) Nothing a
-- Permutations
-- ------------
-- | Generalised forward permutation operation (array scatter).
--
-- Forward permutation specified by a function mapping indices from the source
-- array to indices in the result array. The result array is initialised with
-- the given defaults and any further values that are permuted into the result
-- array are added to the current value using the given combination function.
--
-- The combination function must be /associative/ and /commutative/.
-- Elements for which the permutation function returns 'Nothing' are
-- dropped.
--
-- The combination function is given the new value being permuted as its first
-- argument, and the current value of the array as its second.
--
-- For example, we can use 'permute' to compute the occurrence count (histogram)
-- for an array of values in the range @[0,10)@:
--
-- >>> :{
-- let histogram :: Acc (Vector Int) -> Acc (Vector Int)
-- histogram xs =
-- let zeros = fill (constant (Z:.10)) 0
-- ones = fill (shape xs) 1
-- in
-- permute (+) zeros (\ix -> Just_ (I1 (xs!ix))) ones
-- :}
--
-- >>> let xs = fromList (Z :. 20) [0,0,1,2,1,1,2,4,8,3,4,9,8,3,2,5,5,3,1,2] :: Vector Int
-- >>> run $ histogram (use xs)
-- Vector (Z :. 10) [2,4,4,3,2,2,0,0,2,1]
--
-- As a second example, note that the dimensionality of the source and
-- destination arrays can differ. In this way, we can use 'permute' to create an
-- identity matrix by overwriting elements along the diagonal:
--
-- >>> :{
-- let identity :: Num a => Exp Int -> Acc (Matrix a)
-- identity n =
-- let zeros = fill (I2 n n) 0
-- ones = fill (I1 n) 1
-- in
-- permute const zeros (\(I1 i) -> Just_ (I2 i i)) ones
-- :}
--
-- >>> run $ identity 5 :: Matrix Int
-- Matrix (Z :. 5 :. 5)
-- [ 1, 0, 0, 0, 0,
-- 0, 1, 0, 0, 0,
-- 0, 0, 1, 0, 0,
-- 0, 0, 0, 1, 0,
-- 0, 0, 0, 0, 1]
--
-- [/Note:/]
--
-- Regarding array fusion:
--
-- 1. The 'permute' operation will always be evaluated; it can not be fused
-- into a later step.
--
-- 2. Since the index permutation function might not cover all positions in
-- the output array (the function is not surjective), the array of default
-- values must be evaluated. However, other operations may fuse into this.
--
-- 3. The array of source values can fuse into the permutation operation.
--
-- 4. If the array of default values is only used once, it will be updated
-- in-place. This behaviour can be disabled this with @-fno-inplace@.
--
-- Regarding the defaults array:
--
-- If you are sure that the default values are not necessary---they are not used
-- by the combination function and every element will be overwritten---a default
-- array created by 'Data.Array.Accelerate.Prelude.fill'ing with the value
-- 'Data.Array.Accelerate.Unsafe.undef' will give you a new uninitialised array.
--
-- Regarding the combination function:
--
-- The function 'const' can be used to replace elements of the defaults
-- array with the new values. If the permutation function maps multiple
-- values to the same location in the results array (the function is not
-- injective) then this operation is non-deterministic.
--
-- Since Accelerate uses an unzipped struct-of-array representation, where
-- the individual components of product types (for example, pairs) are
-- stored in separate arrays, storing values of product type requires
-- multiple store instructions.
--
-- Accelerate prior to version 1.3.0.0 performs this operation atomically,
-- to ensure that the stored values are always consistent (each component
-- of the product type is written by the same thread). Later versions relax
-- this restriction, but this behaviour can be disabled with
-- @-fno-fast-permute-const@.
--
permute
:: forall sh sh' a. (Shape sh, Shape sh', Elt a)
=> (Exp a -> Exp a -> Exp a) -- ^ combination function
-> Acc (Array sh' a) -- ^ array of default values
-> (Exp sh -> Exp (Maybe sh')) -- ^ index permutation function
-> Acc (Array sh a) -- ^ array of source values to be permuted
-> Acc (Array sh' a)
permute = Acc $$$$ applyAcc (Permute $ arrayR @sh @a)
-- | Generalised backward permutation operation (array gather).
--
-- Backward permutation specified by a function mapping indices in the
-- destination array to indices in the source array. Elements of the output
-- array are thus generated by reading from the corresponding index in the
-- source array.
--
-- For example, backpermute can be used to
-- 'Data.Array.Accelerate.Prelude.transpose' a matrix; at every index @Z:.y:.x@
-- in the result array, we get the value at that index by reading from the
-- source array at index @Z:.x:.y@:
--
-- >>> :{
-- let swap :: Exp DIM2 -> Exp DIM2
-- swap = lift1 f
-- where
-- f :: Z :. Exp Int :. Exp Int -> Z :. Exp Int :. Exp Int
-- f (Z:.y:.x) = Z :. x :. y
-- :}
--
-- >>> 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]
--
-- >>> let mat' = use mat
-- >>> run $ backpermute (swap (shape mat')) swap mat'
-- Matrix (Z :. 10 :. 5)
-- [ 0, 10, 20, 30, 40,
-- 1, 11, 21, 31, 41,
-- 2, 12, 22, 32, 42,
-- 3, 13, 23, 33, 43,
-- 4, 14, 24, 34, 44,
-- 5, 15, 25, 35, 45,
-- 6, 16, 26, 36, 46,
-- 7, 17, 27, 37, 47,
-- 8, 18, 28, 38, 48,
-- 9, 19, 29, 39, 49]
--
backpermute
:: forall sh sh' a. (Shape sh, Shape sh', Elt a)
=> Exp sh' -- ^ shape of the result array
-> (Exp sh' -> Exp sh) -- ^ index permutation function
-> Acc (Array sh a) -- ^ source array
-> Acc (Array sh' a)
backpermute = Acc $$$ applyAcc (Backpermute $ shapeR @sh')
-- Stencil operations
-- ------------------
-- Common stencil types
--
-- DIM1 stencil type
type Stencil1 a = Unary (Exp a)
type Stencil3 a = (Exp a, Exp a, Exp a)
type Stencil5 a = (Exp a, Exp a, Exp a, Exp a, Exp a)
type Stencil7 a = (Exp a, Exp a, Exp a, Exp a, Exp a, Exp a, Exp a)
type Stencil9 a = (Exp a, Exp a, Exp a, Exp a, Exp a, Exp a, Exp a, Exp a, Exp a)
-- DIM2 stencil type
type Stencil1x1 a = Unary (Stencil1 a)
type Stencil3x1 a = Unary (Stencil3 a)
type Stencil5x1 a = Unary (Stencil5 a)
type Stencil1x3 a = (Stencil1 a, Stencil1 a, Stencil1 a)
type Stencil3x3 a = (Stencil3 a, Stencil3 a, Stencil3 a)
type Stencil5x3 a = (Stencil5 a, Stencil5 a, Stencil5 a)
type Stencil1x5 a = (Stencil1 a, Stencil1 a, Stencil1 a, Stencil1 a, Stencil1 a)
type Stencil3x5 a = (Stencil3 a, Stencil3 a, Stencil3 a, Stencil3 a, Stencil3 a)
type Stencil5x5 a = (Stencil5 a, Stencil5 a, Stencil5 a, Stencil5 a, Stencil5 a)
-- DIM3 stencil type
type Stencil3x3x3 a = (Stencil3x3 a, Stencil3x3 a, Stencil3x3 a)
type Stencil5x3x3 a = (Stencil5x3 a, Stencil5x3 a, Stencil5x3 a)
type Stencil3x5x3 a = (Stencil3x5 a, Stencil3x5 a, Stencil3x5 a)
type Stencil3x3x5 a = (Stencil3x3 a, Stencil3x3 a, Stencil3x3 a, Stencil3x3 a, Stencil3x3 a)
type Stencil5x5x3 a = (Stencil5x5 a, Stencil5x5 a, Stencil5x5 a)
type Stencil5x3x5 a = (Stencil5x3 a, Stencil5x3 a, Stencil5x3 a, Stencil5x3 a, Stencil5x3 a)
type Stencil3x5x5 a = (Stencil3x5 a, Stencil3x5 a, Stencil3x5 a, Stencil3x5 a, Stencil3x5 a)
type Stencil5x5x5 a = (Stencil5x5 a, Stencil5x5 a, Stencil5x5 a, Stencil5x5 a, Stencil5x5 a)
-- | Map a stencil over an array. In contrast to 'map', the domain of a stencil
-- function is an entire /neighbourhood/ of each array element. Neighbourhoods
-- are sub-arrays centred around a focal point. They are not necessarily
-- rectangular, but they are symmetric and have an extent of at least three
-- along each axis. Due to the symmetry requirement the extent is necessarily
-- odd. The focal point is the array position that is determined by the stencil.
--
-- For those array positions where the neighbourhood extends past the boundaries
-- of the source array, a boundary condition determines the contents of the
-- out-of-bounds neighbourhood positions.
--
-- Stencil neighbourhoods are specified via nested tuples, where the nesting
-- depth is equal to the dimensionality of the array. For example, a 3x1 stencil
-- for a one-dimensional array:
--
-- > s31 :: Stencil3 a -> Exp a
-- > s31 (l,c,r) = ...
--
-- ...where @c@ is the focal point of the stencil, and @l@ and @r@ represent the
-- elements to the left and right of the focal point, respectively. Similarly,
-- a 3x3 stencil for a two-dimensional array:
--
-- > s33 :: Stencil3x3 a -> Exp a
-- > s33 ((_,t,_)
-- > ,(l,c,r)
-- > ,(_,b,_)) = ...
--
-- ...where @c@ is again the focal point and @t@, @b@, @l@ and @r@ are the
-- elements to the top, bottom, left, and right of the focal point, respectively
-- (the diagonal elements have been elided).
--
-- For example, the following computes a 5x5
-- <https://en.wikipedia.org/wiki/Gaussian_blur Gaussian blur> as a separable
-- 2-pass operation.
--
-- >
-- > convolve5x1 :: Num a => [Exp a] -> Stencil5x1 a -> Exp a
-- > convolve5x1 kernel (a,b,c,d,e)
-- > = Prelude.sum $ Prelude.zipWith (*) kernel [a,b,c,d,e]
-- >
-- > convolve1x5 :: Num a => [Exp a] -> Stencil1x5 a -> Exp a
-- > convolve1x5 kernel (a,b,c,d,e)
-- > = Prelude.sum $ Prelude.zipWith (*) kernel [a,b,c,d,e]
-- >
-- > gaussian = [0.06136,0.24477,0.38774,0.24477,0.06136]
-- >
-- > blur :: Num a => Acc (Matrix a) -> Acc (Matrix a)
-- > blur = stencil (convolve5x1 gaussian) clamp
-- > . stencil (convolve1x5 gaussian) clamp
--
-- [/Note:/]
--
-- Since accelerate-1.3.0.0, we allow the source array to fuse into the stencil
-- operation. However, since a stencil computation (typically) requires multiple
-- values from the source array, this means that the work of the fused operation
-- will be duplicated for each element in the stencil pattern.
--
-- For example, suppose we write:
--
-- > blur . map f
--
-- The operation `f` will be fused into each element of the first Gaussian blur
-- kernel, resulting in a stencil equivalent to:
--
-- > f_and_convolve1x5 :: Num a => (Exp a -> Exp b) -> [Exp b] -> Stencil1x5 a -> Exp b
-- > f_and_convolve1x5 f kernel ((_,a,_), (_,b,_), (_,c,_), (_,d,_), (_,e,_))
-- > = Prelude.sum $ Prelude.zipWith (*) kernel [f a, f b, f c, f d, f e]
--
-- This duplication is often beneficial, however you may choose to instead force
-- the array to be evaluated first, preventing fusion, using the
-- `Data.Array.Accelerate.Prelude.compute` operation. Benchmarking should reveal
-- which approach is best for your application.
--
stencil
:: forall sh stencil a b.
(Stencil sh a stencil, Elt b)
=> (stencil -> Exp b) -- ^ stencil function
-> Boundary (Array sh a) -- ^ boundary condition
-> Acc (Array sh a) -- ^ source array
-> Acc (Array sh b) -- ^ destination array
stencil f (Boundary b) (Acc a)
= Acc $ SmartAcc $ Stencil
(stencilR @sh @a @stencil)
(eltR @b)
(unExp . f . stencilPrj @sh @a @stencil)
b
a
-- | Map a binary stencil of an array. The extent of the resulting array is the
-- intersection of the extents of the two source arrays. This is the stencil
-- equivalent of 'zipWith'.
--
stencil2
:: forall sh stencil1 stencil2 a b c.
(Stencil sh a stencil1, Stencil sh b stencil2, Elt c)
=> (stencil1 -> stencil2 -> Exp c) -- ^ binary stencil function
-> Boundary (Array sh a) -- ^ boundary condition #1
-> Acc (Array sh a) -- ^ source array #1
-> Boundary (Array sh b) -- ^ boundary condition #2
-> Acc (Array sh b) -- ^ source array #2
-> Acc (Array sh c) -- ^ destination array
stencil2 f (Boundary b1) (Acc a1) (Boundary b2) (Acc a2)
= Acc $ SmartAcc $ Stencil2
(stencilR @sh @a @stencil1)
(stencilR @sh @b @stencil2)
(eltR @c)
(\x y -> unExp $ f (stencilPrj @sh @a @stencil1 x) (stencilPrj @sh @b @stencil2 y))
b1
a1
b2
a2
-- | Boundary condition where elements of the stencil which would be
-- out-of-bounds are instead clamped to the edges of the array.
--
-- In the following 3x3 stencil, the out-of-bounds element @b@ will instead
-- return the value at position @c@:
--
-- > +------------+