/
Language.hs
1082 lines (908 loc) · 32.4 KB
/
Language.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 TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# OPTIONS_GHC -fno-warn-missing-methods #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
-- |
-- Module : Data.Array.Accelerate.Language
-- Copyright : [2008..2014] Manuel M T Chakravarty, Gabriele Keller
-- [2009..2014] Trevor L. McDonell
-- [2014..2014] Frederik M. Madsen
-- License : BSD3
--
-- Maintainer : Manuel M T Chakravarty <chak@cse.unsw.edu.au>
-- 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 and scalar expressions
Acc, Seq, Exp, -- re-exporting from 'Smart'
-- * Scalar introduction
constant, -- re-exporting from 'Smart'
-- * Array construction
use, unit, replicate, generate,
-- * Shape manipulation
reshape,
-- * Extraction of subarrays
slice,
-- * Map-like functions
map, zipWith,
-- * Sequence collection
collect,
-- * Sequence producers
streamIn, toSeq,
-- * Sequence transudcers
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,
-- ** Common stencil types
Stencil3, Stencil5, Stencil7, Stencil9,
Stencil3x3, Stencil5x3, Stencil3x5, Stencil5x5,
Stencil3x3x3, Stencil5x3x3, Stencil3x5x3, Stencil3x3x5, Stencil5x5x3, Stencil5x3x5,
Stencil3x5x5, Stencil5x5x5,
-- * Foreign functions
foreignAcc, foreignAcc2, foreignAcc3,
foreignExp, foreignExp2, foreignExp3,
-- * Pipelining
(>->),
-- * Array-level flow-control
acond, awhile,
-- * Index construction and destruction
indexHead, indexTail, toIndex, fromIndex,
intersect, union,
-- * Flow-control
cond, while,
-- * Array operations with a scalar result
(!), (!!), shape, size, shapeSize,
-- * Methods of H98 classes that we need to redefine as their signatures change
(==*), (/=*), (<*), (<=*), (>*), (>=*),
bit, setBit, clearBit, complementBit, testBit,
shift, shiftL, shiftR,
rotate, rotateL, rotateR,
truncate, round, floor, ceiling,
even, odd, isNaN,
-- * Standard functions that we need to redefine as their signatures change
(&&*), (||*), not,
-- * Conversions
ord, chr, boolToInt, fromIntegral, realToFrac, bitcast,
-- * Constants
ignore
-- Instances of Bounded, Enum, Eq, Ord, Bits, Num, Real, Floating,
-- Fractional, RealFrac, RealFloat
) where
-- standard libraries
import Prelude ( Bounded, Enum, Num, Real, Integral, Floating, Fractional,
RealFloat, RealFrac, Eq, Ord, Bool, Char, String, (.), ($), error )
import Data.Bits ( Bits((.&.), (.|.), xor, complement) )
import qualified Prelude as P
import Text.Printf
-- friends
import Data.Array.Accelerate.Type
import Data.Array.Accelerate.Smart
import Data.Array.Accelerate.Array.Sugar hiding ((!), ignore, shape, size, toIndex, fromIndex, intersect, union)
import qualified Data.Array.Accelerate.Array.Sugar as Sugar
-- Array introduction
-- ------------------
-- | Array inlet: makes an array available for processing using the Accelerate
-- language.
--
-- Depending upon the backend used to execute array computations, this may
-- trigger (asynchronous) data transfer.
--
use :: Arrays arrays => arrays -> Acc arrays
use = Acc . Use
-- | Scalar inlet: injects a scalar (or a tuple of scalars) into a singleton
-- array for use in the Accelerate language.
--
unit :: Elt e => Exp e -> Acc (Scalar e)
unit = Acc . Unit
-- | Replicate an array across one or more dimensions as specified by the
-- /generalised/ array index provided as the first argument.
--
-- For example, assuming 'arr' is a vector (one-dimensional array),
--
-- > replicate (lift (Z :. (2::Int) :. All :. (3::Int))) arr
--
-- yields a three dimensional array, where 'arr' is replicated twice across the
-- first and three times across the third dimension.
--
replicate :: (Slice slix, Elt e)
=> Exp slix
-> Acc (Array (SliceShape slix) e)
-> Acc (Array (FullShape slix) e)
replicate = Acc $$ Replicate
-- | 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:
--
-- > generate (index1 3) (\_ -> 1.2)
--
-- Or, equivalently:
--
-- > generate (constant (Z :. (3::Int))) (\_ -> 1.2)
--
-- Finally, the following will create an array equivalent to '[1..10]':
--
-- > generate (index1 10) $ \ ix ->
-- > let (Z :. i) = unlift ix
-- > in fromIntegral i
--
-- [/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 `the`, the program will fail with the error:
-- '.\/Data\/Array\/Accelerate\/Trafo\/Sharing.hs:447 (convertSharingExp): inconsistent valuation \@ shared \'Exp\' tree ...'.
--
generate :: (Shape ix, Elt a)
=> Exp ix
-> (Exp ix -> Exp a)
-> Acc (Array ix a)
generate = Acc $$ Generate
-- Shape manipulation
-- ------------------
-- | Change the shape of an array without altering its contents. The 'size' of
-- the source and result arrays must be identical.
--
-- > precondition: size ix == size ix'
--
reshape :: (Shape ix, Shape ix', Elt e)
=> Exp ix
-> Acc (Array ix' e)
-> Acc (Array ix e)
reshape = Acc $$ Reshape
-- 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.
--
-- This can be used to /cut out/ entire dimensions. The opposite of
-- `replicate`. For example, if 'mat' is a two dimensional array, the
-- following will select a specific row and yield a one dimensional
-- result:
--
-- > slice mat (lift (Z :. (2::Int) :. All))
--
-- A fully specified index (with no `All`s) would return a single element (zero
-- dimensional array).
--
slice :: (Slice slix, Elt e)
=> Acc (Array (FullShape slix) e)
-> Exp slix
-> Acc (Array (SliceShape slix) e)
slice = Acc $$ Slice
-- Map-like functions
-- ------------------
-- | Apply the given function element-wise to the given array.
--
map :: (Shape ix, Elt a, Elt b)
=> (Exp a -> Exp b)
-> Acc (Array ix a)
-> Acc (Array ix b)
map = Acc $$ Map
-- | 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.
--
zipWith :: (Shape ix, Elt a, Elt b, Elt c)
=> (Exp a -> Exp b -> Exp c)
-> Acc (Array ix a)
-> Acc (Array ix b)
-> Acc (Array ix c)
zipWith = Acc $$$ ZipWith
-- Reductions
-- ----------
-- | Reduction of the innermost dimension of an array of arbitrary rank. The
-- first argument needs to be an /associative/ function to enable an efficient
-- parallel implementation.
--
fold :: (Shape ix, Elt a)
=> (Exp a -> Exp a -> Exp a)
-> Exp a
-> Acc (Array (ix:.Int) a)
-> Acc (Array ix a)
fold = Acc $$$ Fold
-- | Variant of 'fold' that requires the reduced array to be non-empty and
-- doesn't need an default value. The first argument needs to be an
-- /associative/ function to enable an efficient parallel implementation.
--
fold1 :: (Shape ix, Elt a)
=> (Exp a -> Exp a -> Exp a)
-> Acc (Array (ix:.Int) a)
-> Acc (Array ix a)
fold1 = Acc $$ Fold1
-- | Segmented reduction along the innermost dimension. Performs one individual
-- reduction per segment of the source array. These reductions proceed in
-- parallel.
--
-- The source array must have at least rank 1. The 'Segments' array determines
-- the lengths of the logical sub-arrays, each of which is folded separately.
--
foldSeg :: (Shape ix, Elt a, Elt i, IsIntegral i)
=> (Exp a -> Exp a -> Exp a)
-> Exp a
-> Acc (Array (ix:.Int) a)
-> Acc (Segments i)
-> Acc (Array (ix:.Int) a)
foldSeg = Acc $$$$ FoldSeg
-- | Variant of 'foldSeg' that requires /all/ segments of the reduced array to
-- be non-empty and doesn't need a default value.
--
-- The source array must have at least rank 1. The 'Segments' array determines
-- the lengths of the logical sub-arrays, each of which is folded separately.
--
fold1Seg :: (Shape ix, Elt a, Elt i, IsIntegral i)
=> (Exp a -> Exp a -> Exp a)
-> Acc (Array (ix:.Int) a)
-> Acc (Segments i)
-> Acc (Array (ix:.Int) a)
fold1Seg = Acc $$$ Fold1Seg
-- Scan functions
-- --------------
-- | Data.List style left-to-right scan, but with the additional restriction
-- that the first argument needs to be an /associative/ function to enable an
-- efficient parallel implementation. The initial value (second argument) may be
-- arbitrary.
--
scanl :: Elt a
=> (Exp a -> Exp a -> Exp a)
-> Exp a
-> Acc (Vector a)
-> Acc (Vector a)
scanl = Acc $$$ Scanl
-- | Variant of 'scanl', where the final result of the reduction is returned
-- separately. Denotationally, we have
--
-- > scanl' f e arr = (init res, unit (res!len))
-- > where
-- > len = shape arr
-- > res = scanl f e arr
--
scanl' :: Elt a
=> (Exp a -> Exp a -> Exp a)
-> Exp a
-> Acc (Vector a)
-> (Acc (Vector a), Acc (Scalar a))
scanl' = unatup2 . Acc $$$ Scanl'
-- | Data.List style left-to-right scan without an initial value (aka inclusive
-- scan). Again, the first argument needs to be an /associative/ function.
-- Denotationally, we have
--
-- > scanl1 f e arr = tail (scanl f e arr)
--
scanl1 :: Elt a
=> (Exp a -> Exp a -> Exp a)
-> Acc (Vector a)
-> Acc (Vector a)
scanl1 = Acc $$ Scanl1
-- | Right-to-left variant of 'scanl'.
--
scanr :: Elt a
=> (Exp a -> Exp a -> Exp a)
-> Exp a
-> Acc (Vector a)
-> Acc (Vector a)
scanr = Acc $$$ Scanr
-- | Right-to-left variant of 'scanl''.
--
scanr' :: Elt a
=> (Exp a -> Exp a -> Exp a)
-> Exp a
-> Acc (Vector a)
-> (Acc (Vector a), Acc (Scalar a))
scanr' = unatup2 . Acc $$$ Scanr'
-- | Right-to-left variant of 'scanl1'.
--
scanr1 :: Elt a
=> (Exp a -> Exp a -> Exp a)
-> Acc (Vector a)
-> Acc (Vector a)
scanr1 = Acc $$ Scanr1
-- Permutations
-- ------------
-- | Forward permutation specified by an index mapping. 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
-- that are mapped to the magic value 'ignore' by the permutation function are
-- dropped.
--
permute :: (Shape ix, Shape ix', Elt a)
=> (Exp a -> Exp a -> Exp a) -- ^combination function
-> Acc (Array ix' a) -- ^array of default values
-> (Exp ix -> Exp ix') -- ^permutation
-> Acc (Array ix a) -- ^array to be permuted
-> Acc (Array ix' a)
permute = Acc $$$$ Permute
-- | Backward permutation specified by an index mapping from the destination
-- array specifying which element of the source array to read.
--
backpermute :: (Shape ix, Shape ix', Elt a)
=> Exp ix' -- ^shape of the result array
-> (Exp ix' -> Exp ix) -- ^permutation
-> Acc (Array ix a) -- ^source array
-> Acc (Array ix' a)
backpermute = Acc $$$ Backpermute
-- Stencil operations
-- ------------------
-- Common stencil types
--
-- DIM1 stencil type
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 Stencil3x3 a = (Stencil3 a, Stencil3 a, Stencil3 a)
type Stencil5x3 a = (Stencil5 a, Stencil5 a, Stencil5 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 in each dimension
-- and have an extent of at least three in each dimensions — 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 :: (Shape ix, Elt a, Elt b, Stencil ix a stencil)
=> (stencil -> Exp b) -- ^stencil function
-> Boundary a -- ^boundary condition
-> Acc (Array ix a) -- ^source array
-> Acc (Array ix b) -- ^destination array
stencil = Acc $$$ Stencil
-- | Map a binary stencil of an array. The extent of the resulting array is the
-- intersection of the extents of the two source arrays.
--
stencil2 :: (Shape ix, Elt a, Elt b, Elt c,
Stencil ix a stencil1,
Stencil ix b stencil2)
=> (stencil1 -> stencil2 -> Exp c) -- ^binary stencil function
-> Boundary a -- ^boundary condition #1
-> Acc (Array ix a) -- ^source array #1
-> Boundary b -- ^boundary condition #2
-> Acc (Array ix b) -- ^source array #2
-> Acc (Array ix c) -- ^destination array
stencil2 = Acc $$$$$ Stencil2
-- Sequence operations
-- ------------------
-- Common sequence types
--
streamIn :: Arrays a
=> [a]
-> Seq [a]
streamIn arrs = Seq (StreamIn arrs)
-- | Convert the given array to a sequence by dividing the array up into subarrays.
-- The first argument captures how to the division should be performed. The
-- presence of `All` in the division descriptor indicates that elements in the
-- corresponding dimension should be retained in the subarrays, whereas `Split`
-- indicates that the input array should divided along this dimension.
--
toSeq :: (Division slsix, Elt a)
=> slsix
-> Acc (Array (FullShape (DivisionSlice slsix)) a)
-> Seq [Array (SliceShape (DivisionSlice slsix)) a]
toSeq spec acc = Seq (ToSeq spec acc)
-- | Apply the given array function element-wise to the given sequence.
--
mapSeq :: (Arrays a, Arrays b)
=> (Acc a -> Acc b)
-> Seq [a]
-> Seq [b]
mapSeq = Seq $$ MapSeq
-- | Apply the given binary function element-wise to the two sequences. The length of the resulting
-- sequence is the minumum of the lengths of the two source sequences.
--
zipWithSeq :: (Arrays a, Arrays b, Arrays c)
=> (Acc a -> Acc b -> Acc c)
-> Seq [a]
-> Seq [b]
-> Seq [c]
zipWithSeq = Seq $$$ ZipWithSeq
-- | scanSeq (+) a0 x seq. Scan a sequence x by combining each
-- element using the given binary operation (+). (+) must be
-- associative:
--
-- Forall a b c. (a + b) + c = a + (b + c),
--
-- and a0 must be the identity element for (+):
--
-- Forall a. a0 + a = a = a + a0.
--
scanSeq :: Elt a
=> (Exp a -> Exp a -> Exp a)
-> Exp a
-> Seq [Scalar a]
-> Seq [Scalar a]
scanSeq = Seq $$$ ScanSeq
-- | foldSeq (+) a0 x seq. Fold a sequence x by combining each
-- element using the given binary operation (+). (+) must be
-- associative:
--
-- Forall a b c. (a + b) + c = a + (b + c),
--
-- and a0 must be the identity element for (+):
--
-- Forall a. a0 + a = a = a + a0.
--
foldSeq :: Elt a
=> (Exp a -> Exp a -> Exp a)
-> Exp a
-> Seq [Scalar a]
-> Seq (Scalar a)
foldSeq = Seq $$$ FoldSeq
-- | foldSeqFlatten f a0 x seq. A specialized version of
-- FoldSeqAct where reduction with the companion operator
-- corresponds to flattening. f must be semi-associative, with vecotor
-- append (++) as the companion operator:
--
-- Forall b sh1 a1 sh2 a2.
-- f (f b sh1 a1) sh2 a2 = f b (sh1 ++ sh2) (a1 ++ a2).
--
-- It is common to ignore the shape vectors, yielding the usual
-- semi-associativity law:
--
-- f b a _ = b + a,
--
-- for some (+) satisfying:
--
-- Forall b a1 a2. (b + a1) + a2 = b + (a1 ++ a2).
--
foldSeqFlatten :: (Arrays a, Shape jx, Elt b)
=> (Acc a -> Acc (Vector jx) -> Acc (Vector b) -> Acc a)
-> Acc a
-> Seq [Array jx b]
-> Seq a
foldSeqFlatten = Seq $$$ FoldSeqFlatten
collect :: Arrays arrs => Seq arrs -> Acc arrs
collect = Acc . Collect
-- Foreign function calling
-- ------------------------
-- | Call a foreign function. The form the function takes is dependent on the backend being used.
-- The arguments are passed as either a single array or as a tuple of arrays. In addition a pure
-- Accelerate version of the function needs to be provided to support backends other than the one
-- being targeted.
foreignAcc :: (Arrays acc, Arrays res, Foreign ff)
=> ff acc res
-> (Acc acc -> Acc res)
-> Acc acc
-> Acc res
foreignAcc = Acc $$$ Aforeign
-- | Call a foreign function with foreign implementations for two different backends.
foreignAcc2 :: (Arrays acc, Arrays res, Foreign ff1, Foreign ff2)
=> ff1 acc res
-> ff2 acc res
-> (Acc acc -> Acc res)
-> Acc acc
-> Acc res
foreignAcc2 ff1 = Acc $$$ Aforeign ff1 $$ Acc $$$ Aforeign
-- | Call a foreign function with foreign implementations for three different backends.
foreignAcc3 :: (Arrays acc, Arrays res, Foreign ff1, Foreign ff2, Foreign ff3)
=> ff1 acc res
-> ff2 acc res
-> ff3 acc res
-> (Acc acc -> Acc res)
-> Acc acc
-> Acc res
foreignAcc3 ff1 ff2 = Acc $$$ Aforeign ff1 $$ Acc $$$ Aforeign ff2 $$ Acc $$$ Aforeign
-- | Call a foreign expression function. The form the function takes is dependent on the
-- backend being used. The arguments are passed as either a single scalar element or as a
-- tuple of elements. In addition a pure Accelerate version of the function needs to be
-- provided to support backends other than the one being targeted.
foreignExp :: (Elt e, Elt res, Foreign ff)
=> ff e res
-> (Exp e -> Exp res)
-> Exp e
-> Exp res
foreignExp = Exp $$$ Foreign
-- | Call a foreign function with foreign implementations for two different backends.
foreignExp2 :: (Elt e, Elt res, Foreign ff1, Foreign ff2)
=> ff1 e res
-> ff2 e res
-> (Exp e -> Exp res)
-> Exp e
-> Exp res
foreignExp2 ff1 = Exp $$$ Foreign ff1 $$ Exp $$$ Foreign
-- | Call a foreign function with foreign implementations for three different backends.
foreignExp3 :: (Elt e, Elt res, Foreign ff1, Foreign ff2, Foreign ff3)
=> ff1 e res
-> ff2 e res
-> ff3 e res
-> (Exp e -> Exp res)
-> Exp e
-> Exp res
foreignExp3 ff1 ff2 = Exp $$$ Foreign ff1 $$ Exp $$$ Foreign ff2 $$ Exp $$$ Foreign
-- Composition of array computations
-- ---------------------------------
-- | Pipelining of two array computations.
--
-- Denotationally, we have
--
-- > (acc1 >-> acc2) arrs = let tmp = acc1 arrs in acc2 tmp
--
infixl 1 >->
(>->) :: (Arrays a, Arrays b, Arrays c) => (Acc a -> Acc b) -> (Acc b -> Acc c) -> (Acc a -> Acc c)
(>->) = Acc $$$ Pipe
-- Flow control constructs
-- -----------------------
-- | An array-level if-then-else construct.
--
acond :: Arrays a
=> Exp Bool -- ^ if-condition
-> Acc a -- ^ then-array
-> Acc a -- ^ else-array
-> Acc a
acond = Acc $$$ Acond
-- | An array-level while construct
--
awhile :: Arrays a
=> (Acc a -> Acc (Scalar Bool))
-> (Acc a -> Acc a)
-> Acc a
-> Acc a
awhile = Acc $$$ Awhile
-- Shapes and indices
-- ------------------
-- | Get the outermost dimension of a shape
--
indexHead :: (Slice sh, Elt a) => Exp (sh :. a) -> Exp a
indexHead = Exp . IndexHead
-- | Get all but the outermost element of a shape
--
indexTail :: (Slice sh, Elt a) => Exp (sh :. a) -> Exp sh
indexTail = Exp . IndexTail
-- | Map a multi-dimensional index into a linear, row-major representation of an
-- array. The first argument is the array shape, the second is the index.
--
toIndex :: Shape sh => Exp sh -> Exp sh -> Exp Int
toIndex = Exp $$ ToIndex
-- | Inverse of 'toIndex'
--
fromIndex :: Shape sh => Exp sh -> Exp Int -> Exp sh
fromIndex = Exp $$ FromIndex
-- | Intersection of two shapes
--
intersect :: Shape sh => Exp sh -> Exp sh -> Exp sh
intersect = Exp $$ Intersect
-- | Union of two shapes
--
union :: Shape sh => Exp sh -> Exp sh -> Exp sh
union = Exp $$ Union
-- Flow-control
-- ------------
-- | A scalar-level if-then-else construct.
--
cond :: Elt t
=> Exp Bool -- ^ condition
-> Exp t -- ^ then-expression
-> Exp t -- ^ else-expression
-> Exp t
cond = Exp $$$ Cond
-- | While construct. Continue to apply the given function, starting with the
-- initial value, until the test function evaluates to true.
--
while :: Elt e
=> (Exp e -> Exp Bool)
-> (Exp e -> Exp e)
-> Exp e
-> Exp e
while = Exp $$$ While
-- Array operations with a scalar result
-- -------------------------------------
-- |Expression form that extracts a scalar from an array
--
infixl 9 !
(!) :: (Shape ix, Elt e) => Acc (Array ix e) -> Exp ix -> Exp e
(!) = Exp $$ Index
-- |Expression form that extracts a scalar from an array at a linear index
--
infixl 9 !!
(!!) :: (Shape ix, Elt e) => Acc (Array ix e) -> Exp Int -> Exp e
(!!) = Exp $$ LinearIndex
-- |Expression form that yields the shape of an array
--
shape :: (Shape ix, Elt e) => Acc (Array ix e) -> Exp ix
shape = Exp . Shape
-- |Expression form that yields the size of an array
--
size :: (Shape ix, Elt e) => Acc (Array ix e) -> Exp Int
size = shapeSize . shape
-- |The total number of elements in an array of the given 'Shape'
--
shapeSize :: Shape ix => Exp ix -> Exp Int
shapeSize = Exp . ShapeSize
-- Instances of all relevant Haskell 98 classes
-- --------------------------------------------
preludeError :: String -> String -> a
preludeError x y = error (printf "Prelude.%s applied to EDSL types: use %s instead" x y)
instance (Elt t, IsBounded t) => Bounded (Exp t) where
minBound = mkMinBound
maxBound = mkMaxBound
instance (Elt t, IsScalar t) => Enum (Exp t)
-- FIXME: Provided only to fulfil superclass constraints; e.g. Integral
-- succ = mkSucc
-- pred = mkPred
instance (Elt t, IsScalar t) => Eq (Exp t) where
-- FIXME: Provided only to fulfil superclass constraints; e.g. Ord
(==) = preludeError "Eq.==" "(==*)"
(/=) = preludeError "Eq./=" "(/=*)"
instance (Elt t, IsScalar t) => Ord (Exp t) where
-- FIXME: Provided only to fulfil superclass constraints; e.g. Real
-- FIXME: Instance makes no sense with standard signatures
min = mkMin
max = mkMax
--
compare = error "Prelude.Ord.compare applied to EDSL types"
(<) = preludeError "Ord.<" "(<*)"
(<=) = preludeError "Ord.<=" "(<=*)"
(>) = preludeError "Ord.>" "(>*)"
(>=) = preludeError "Ord.>=" "(>=*)"
instance (Elt t, IsNum t, IsIntegral t) => Bits (Exp t) where
(.&.) = mkBAnd
(.|.) = mkBOr
xor = mkBXor
complement = mkBNot
-- FIXME: argh, the rest have fixed types in their signatures
-- | @'shift' x i@ shifts @x@ left by @i@ bits if @i@ is positive, or right by
-- @-i@ bits otherwise. Right shifts perform sign extension on signed number
-- types; i.e. they fill the top bits with 1 if the @x@ is negative and with 0
-- otherwise.
--
shift :: (Elt t, IsIntegral t) => Exp t -> Exp Int -> Exp t
shift x i
= cond (i ==* 0) x
$ cond (i <* 0) (x `mkBShiftR` (-i))
(x `mkBShiftL` i)
-- | Shift the argument left by the specified number of bits
-- (which must be non-negative).
--
shiftL :: (Elt t, IsIntegral t) => Exp t -> Exp Int -> Exp t
shiftL x i
= cond (i ==* 0) x
$ mkBShiftL x i
-- | Shift the first argument right by the specified number of bits. The result
-- is undefined for negative shift amounts and shift amounts greater or equal to
-- the 'bitSize'.
--
-- Right shifts perform sign extension on signed number types; i.e. they fill
-- the top bits with 1 if the @x@ is negative and with 0 otherwise.
--
shiftR :: (Elt t, IsIntegral t) => Exp t -> Exp Int -> Exp t
shiftR x i
= cond (i ==* 0) x
$ mkBShiftR x i
-- | @'rotate' x i@ rotates @x@ left by @i@ bits if @i@ is positive, or right by
-- @-i@ bits otherwise.
--
rotate :: (Elt t, IsIntegral t) => Exp t -> Exp Int -> Exp t
rotate x i
= cond (i ==* 0) x
$ cond (i <* 0) (x `mkBRotateR` (-i))
(x `mkBRotateL` i)
-- | Rotate the argument left by the specified number of bits
-- (which must be non-negative).
--
rotateL :: (Elt t, IsIntegral t) => Exp t -> Exp Int -> Exp t
rotateL x i
= cond (i ==* 0) x
$ mkBRotateL x i
-- | Rotate the argument right by the specified number of bits
-- (which must be non-negative).
--
rotateR :: (Elt t, IsIntegral t) => Exp t -> Exp Int -> Exp t
rotateR x i
= cond (i ==* 0) x
$ mkBRotateR x i
-- | @bit i@ is a value with the @i@th bit set and all other bits clear
--
bit :: (Elt t, IsIntegral t) => Exp Int -> Exp t
bit x = 1 `shiftL` x
-- | @x \`setBit\` i@ is the same as @x .|. bit i@
--
setBit :: (Elt t, IsIntegral t) => Exp t -> Exp Int -> Exp t
x `setBit` i = x .|. bit i
-- | @x \`clearBit\` i@ is the same as @x .&. complement (bit i)@
--
clearBit :: (Elt t, IsIntegral t) => Exp t -> Exp Int -> Exp t
x `clearBit` i = x .&. complement (bit i)
-- | @x \`complementBit\` i@ is the same as @x \`xor\` bit i@
--
complementBit :: (Elt t, IsIntegral t) => Exp t -> Exp Int -> Exp t
x `complementBit` i = x `xor` bit i
-- | Return 'True' if the @n@th bit of the argument is 1
--
testBit :: (Elt t, IsIntegral t) => Exp t -> Exp Int -> Exp Bool
x `testBit` i = (x .&. bit i) /=* 0
instance (Elt t, IsNum t) => Num (Exp t) where
(+) = mkAdd
(-) = mkSub
(*) = mkMul
negate = mkNeg
abs = mkAbs
signum = mkSig
fromInteger = constant . P.fromInteger
instance (Elt t, IsNum t) => Real (Exp t)
-- FIXME: Provided only to fulfil superclass constrains; e.g. Integral
-- FIXME: We won't need `toRational' until we support rational numbers in AP
-- computations.
instance (Elt t, IsIntegral t) => Integral (Exp t) where
quot = mkQuot
rem = mkRem
div = mkIDiv
mod = mkMod
quotRem = mkQuotRem
divMod = mkDivMod
-- toInteger = -- makes no sense
instance (Elt t, IsFloating t) => Floating (Exp t) where
pi = mkPi
sin = mkSin
cos = mkCos
tan = mkTan
asin = mkAsin
acos = mkAcos
atan = mkAtan
sinh = mkSinh
cosh = mkCosh
tanh = mkTanh
asinh = mkAsinh
acosh = mkAcosh
atanh = mkAtanh
exp = mkExpFloating
sqrt = mkSqrt
log = mkLog
(**) = mkFPow
logBase = mkLogBase
instance (Elt t, IsFloating t) => Fractional (Exp t) where
(/) = mkFDiv
recip = mkRecip
fromRational = constant . P.fromRational
instance (Elt t, IsFloating t) => RealFrac (Exp t)
-- FIXME: add other ops
instance (Elt t, IsFloating t) => RealFloat (Exp t) where
atan2 = mkAtan2
-- FIXME: add other ops
-- Methods from H98 classes, where we need other signatures
-- --------------------------------------------------------
infix 4 ==*, /=*, <*, <=*, >*, >=*
-- |Equality lifted into Accelerate expressions.
--
(==*) :: (Elt t, IsScalar t) => Exp t -> Exp t -> Exp Bool
(==*) = mkEq
-- |Inequality lifted into Accelerate expressions.
--
(/=*) :: (Elt t, IsScalar t) => Exp t -> Exp t -> Exp Bool
(/=*) = mkNEq
-- compare :: a -> a -> Ordering -- we have no enumerations at the moment
-- compare = ...
-- |Smaller-than lifted into Accelerate expressions.
--
(<*) :: (Elt t, IsScalar t) => Exp t -> Exp t -> Exp Bool
(<*) = mkLt
-- |Greater-or-equal lifted into Accelerate expressions.
--
(>=*) :: (Elt t, IsScalar t) => Exp t -> Exp t -> Exp Bool
(>=*) = mkGtEq
-- |Greater-than lifted into Accelerate expressions.
--
(>*) :: (Elt t, IsScalar t) => Exp t -> Exp t -> Exp Bool
(>*) = mkGt
-- |Smaller-or-equal lifted into Accelerate expressions.
--
(<=*) :: (Elt t, IsScalar t) => Exp t -> Exp t -> Exp Bool
(<=*) = mkLtEq
-- Conversions from the RealFrac class
--
-- | @truncate x@ returns the integer nearest @x@ between zero and @x@.
--
truncate :: (Elt a, Elt b, IsFloating a, IsIntegral b) => Exp a -> Exp b
truncate = mkTruncate
-- | @round x@ returns the nearest integer to @x@, or the even integer if @x@ is
-- equidistant between two integers.
--
round :: (Elt a, Elt b, IsFloating a, IsIntegral b) => Exp a -> Exp b
round = mkRound
-- | @floor x@ returns the greatest integer not greater than @x@.
--
floor :: (Elt a, Elt b, IsFloating a, IsIntegral b) => Exp a -> Exp b
floor = mkFloor
-- | @ceiling x@ returns the least integer not less than @x@.
--
ceiling :: (Elt a, Elt b, IsFloating a, IsIntegral b) => Exp a -> Exp b
ceiling = mkCeiling