-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathData.hs
619 lines (592 loc) · 16.7 KB
/
Data.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
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleContexts #-}
--------------------------------------------------------------------------------
-- |
-- Module : ArrayFire.Data
-- Copyright : David Johnson (c) 2019-2020
-- License : BSD 3
-- Maintainer : David Johnson <[email protected]>
-- Stability : Experimental
-- Portability : GHC
--
-- Functions for populating 'Array' with Data.
--
-- @
-- >>> constant @Double [2,2] 2.0
-- ArrayFire Array
-- [2 2 1 1]
-- 2.0000 2.0000
-- 2.0000 2.0000
-- @
--
--------------------------------------------------------------------------------
module ArrayFire.Data where
import Control.Exception
import Data.Complex
import Data.Int
import Data.Proxy
import Data.Word
import Foreign.C.Types
import Foreign.ForeignPtr
import Foreign.Marshal hiding (void)
import Foreign.Ptr (Ptr)
import Foreign.Storable
import System.IO.Unsafe
import Unsafe.Coerce
import ArrayFire.Exception
import ArrayFire.FFI
import ArrayFire.Internal.Data
import ArrayFire.Internal.Defines
import ArrayFire.Internal.Types
import ArrayFire.Arith
-- | Creates an 'Array' from a scalar value from given dimensions
--
-- >>> constant @Double [2,2] 2.0
-- ArrayFire Array
-- [2 2 1 1]
-- 2.0000 2.0000
-- 2.0000 2.0000
constant
:: forall a . AFType a
=> [Int]
-- ^ Dimensions
-> a
-- ^ Scalar value
-> Array a
constant dims val =
case dtyp of
x | x == c64 ->
cast $ constantComplex dims (unsafeCoerce val :: Complex Double)
| x == c32 ->
cast $ constantComplex dims (unsafeCoerce val :: Complex Float)
| x == s64 ->
cast $ constantLong dims (unsafeCoerce val :: Int)
| x == u64 ->
cast $ constantULong dims (unsafeCoerce val :: Word64)
| x == s32 ->
cast $ constant' dims (fromIntegral (unsafeCoerce val :: Int32) :: Double)
| x == s16 ->
cast $ constant' dims (fromIntegral (unsafeCoerce val :: Int16) :: Double)
| x == u32 ->
cast $ constant' dims (fromIntegral (unsafeCoerce val :: Word32) :: Double)
| x == u8 ->
cast $ constant' dims (fromIntegral (unsafeCoerce val :: Word8) :: Double)
| x == u16 ->
cast $ constant' dims (fromIntegral (unsafeCoerce val :: Word16) :: Double)
| x == f64 ->
cast $ constant' dims (unsafeCoerce val :: Double)
| x == b8 ->
cast $ constant' dims (fromIntegral (unsafeCoerce val :: CBool) :: Double)
| x == f32 ->
cast $ constant' dims (realToFrac (unsafeCoerce val :: Float))
| otherwise -> error "constant: Invalid array fire type"
where
dtyp = afType (Proxy @a)
constant'
:: [Int]
-- ^ Dimensions
-> Double
-- ^ Scalar value
-> Array Double
constant' dims' val' =
unsafePerformIO . mask_ $ do
ptr <- alloca $ \ptrPtr -> do
zeroOutArray ptrPtr
withArray (fromIntegral <$> dims') $ \dimArray -> do
throwAFError =<< af_constant ptrPtr val' n dimArray typ
peek ptrPtr
Array <$>
newForeignPtr
af_release_array_finalizer
ptr
where
n = fromIntegral (length dims')
typ = afType (Proxy @Double)
-- | Creates an 'Array (Complex Double)' from a scalar val'ue
--
-- @
-- >>> constantComplex [2,2] (2.0 :+ 2.0)
-- @
--
constantComplex
:: forall arr . (Real arr, AFType (Complex arr))
=> [Int]
-- ^ Dimensions
-> Complex arr
-- ^ Scalar val'ue
-> Array (Complex arr)
constantComplex dims' ((realToFrac -> x) :+ (realToFrac -> y)) = unsafePerformIO . mask_ $ do
ptr <- alloca $ \ptrPtr -> do
zeroOutArray ptrPtr
withArray (fromIntegral <$> dims') $ \dimArray -> do
throwAFError =<< af_constant_complex ptrPtr x y n dimArray typ
peek ptrPtr
Array <$>
newForeignPtr
af_release_array_finalizer
ptr
where
n = fromIntegral (length dims')
typ = afType (Proxy @(Complex arr))
-- | Creates an 'Array Int64' from a scalar val'ue
--
-- @
-- >>> constantLong [2,2] 2.0
-- @
--
constantLong
:: [Int]
-- ^ Dimensions
-> Int
-- ^ Scalar val'ue
-> Array Int
constantLong dims' val' = unsafePerformIO . mask_ $ do
ptr <- alloca $ \ptrPtr -> do
zeroOutArray ptrPtr
withArray (fromIntegral <$> dims') $ \dimArray -> do
throwAFError =<< af_constant_long ptrPtr (fromIntegral val') n dimArray
peek ptrPtr
Array <$>
newForeignPtr
af_release_array_finalizer
ptr
where
n = fromIntegral (length dims')
-- | Creates an 'Array Word64' from a scalar val'ue
--
-- @
-- >>> constantULong [2,2] 2.0
-- @
--
constantULong
:: [Int]
-> Word64
-> Array Word64
constantULong dims' val' = unsafePerformIO . mask_ $ do
ptr <- alloca $ \ptrPtr -> do
zeroOutArray ptrPtr
withArray (fromIntegral <$> dims') $ \dimArray -> do
throwAFError =<< af_constant_ulong ptrPtr (fromIntegral val') n dimArray
peek ptrPtr
Array <$>
newForeignPtr
af_release_array_finalizer
ptr
where
n = fromIntegral (length dims')
-- | Creates a range of values in an Array
--
-- >>> range @Double [10] (-1)
-- ArrayFire Array
-- [10 1 1 1]
-- 0.0000
-- 1.0000
-- 2.0000
-- 3.0000
-- 4.0000
-- 5.0000
-- 6.0000
-- 7.0000
-- 8.0000
-- 9.0000
range
:: forall a
. AFType a
=> [Int]
-> Int
-> Array a
range dims (fromIntegral -> k) = unsafePerformIO $ do
ptr <- alloca $ \ptrPtr -> mask_ $ do
withArray (fromIntegral <$> dims) $ \dimArray -> do
throwAFError =<< af_range ptrPtr n dimArray k typ
peek ptrPtr
Array <$>
newForeignPtr
af_release_array_finalizer
ptr
where
n = fromIntegral (length dims)
typ = afType (Proxy @a)
-- | Create an sequence [0, dims.elements() - 1] and modify to specified dimensions dims and then tile it according to tile_dims.
--
-- <http://arrayfire.org/docs/group__data__func__iota.htm>
--
-- >>> iota @Double [5,3] []
-- ArrayFire Array
-- [5 3 1 1]
-- 0.0000 5.0000 10.0000
-- 1.0000 6.0000 11.0000
-- 2.0000 7.0000 12.0000
-- 3.0000 8.0000 13.0000
-- 4.0000 9.0000 14.0000
--
-- >>> iota @Double [5,3] [1,2]
-- ArrayFire Array
-- [5 6 1 1]
-- 0.0000 5.0000 10.0000 0.0000 5.0000 10.0000
-- 1.0000 6.0000 11.0000 1.0000 6.0000 11.0000
-- 2.0000 7.0000 12.0000 2.0000 7.0000 12.0000
-- 3.0000 8.0000 13.0000 3.0000 8.0000 13.0000
-- 4.0000 9.0000 14.0000 4.0000 9.0000 14.0000
iota
:: forall a . AFType a
=> [Int]
-- ^ is the array containing sizes of the dimension
-> [Int]
-- ^ is array containing the number of repetitions of the unit dimensions
-> Array a
-- ^ is the generated array
iota dims tdims = unsafePerformIO $ do
let dims' = take 4 (dims ++ repeat 1)
tdims' = take 4 (tdims ++ repeat 1)
ptr <- alloca $ \ptrPtr -> mask_ $ do
zeroOutArray ptrPtr
withArray (fromIntegral <$> dims') $ \dimArray ->
withArray (fromIntegral <$> tdims') $ \tdimArray -> do
throwAFError =<< af_iota ptrPtr 4 dimArray 4 tdimArray typ
peek ptrPtr
Array <$>
newForeignPtr
af_release_array_finalizer
ptr
where
typ = afType (Proxy @a)
-- | Creates the identity `Array` from given dimensions
--
-- >>> identity [2,2]
-- ArrayFire Array
-- [2 2 1 1]
-- 1.0000 0.0000
-- 0.0000 1.0000
identity
:: forall a . AFType a
=> [Int]
-- ^ Dimensions
-> Array a
identity dims = unsafePerformIO . mask_ $ do
let dims' = take 4 (dims ++ repeat 1)
ptr <- alloca $ \ptrPtr -> mask_ $ do
zeroOutArray ptrPtr
withArray (fromIntegral <$> dims') $ \dimArray -> do
throwAFError =<< af_identity ptrPtr n dimArray typ
peek ptrPtr
Array <$>
newForeignPtr
af_release_array_finalizer
ptr
where
n = fromIntegral (length dims)
typ = afType (Proxy @a)
-- | Create a diagonal matrix from input array when extract is set to false
--
-- >>> diagCreate (vector @Double 2 [1..]) 0
-- ArrayFire Array
-- [2 2 1 1]
-- 1.0000 0.0000
-- 0.0000 2.0000
diagCreate
:: AFType (a :: *)
=> Array a
-- ^ is the input array which is the diagonal
-> Int
-- ^ is the diagonal index
-> Array a
diagCreate x (fromIntegral -> n) =
x `op1` (\p a -> af_diag_create p a n)
-- | Create a diagonal matrix from input array when extract is set to false
--
-- >>> diagExtract (matrix @Double (2,2) [[1,2],[3,4]]) 0
-- ArrayFire Array
-- [2 1 1 1]
-- 1.0000
-- 4.0000
diagExtract
:: AFType (a :: *)
=> Array a
-> Int
-> Array a
diagExtract x (fromIntegral -> n) =
x `op1` (\p a -> af_diag_extract p a n)
-- | Join two Arrays together along a specified dimension
--
-- >>> join 0 (matrix @Double (2,2) [[1,2],[3,4]]) (matrix @Double (2,2) [[5,6],[7,8]])
-- ArrayFire Array
-- [4 2 1 1]
-- 1.0000 3.0000
-- 2.0000 4.0000
-- 5.0000 7.0000
-- 6.0000 8.0000
--
join
:: Int
-> Array (a :: *)
-> Array a
-> Array a
join (fromIntegral -> n) arr1 arr2 = op2 arr1 arr2 (\p a b -> af_join p n a b)
-- | Join many Arrays together along a specified dimension
--
-- *FIX ME*
--
-- >>> joinMany 0 [1,2,3]
-- ArrayFire Array
-- [3 1 1 1]
-- 1.0000 2.0000 3.0000
--
joinMany
:: Int
-> [Array a]
-> Array a
joinMany (fromIntegral -> n) (fmap (\(Array fp) -> fp) -> arrays) = unsafePerformIO . mask_ $ do
newPtr <- alloca $ \aPtr -> do
zeroOutArray aPtr
(throwAFError =<<) $
withManyForeignPtr arrays $ \(fromIntegral -> nArrays) fPtrsPtr ->
af_join_many aPtr n nArrays fPtrsPtr
peek aPtr
Array <$>
newForeignPtr af_release_array_finalizer newPtr
withManyForeignPtr :: [ForeignPtr a] -> (Int -> Ptr (Ptr a) -> IO b) -> IO b
withManyForeignPtr fptrs action = go [] fptrs
where
go ptrs [] = withArrayLen (reverse ptrs) action
go ptrs (fptr:others) = withForeignPtr fptr $ \ptr -> go (ptr : ptrs) others
-- | Tiles an Array according to specified dimensions
--
-- >>> tile @Double (scalar 22.0) [5,5]
-- ArrayFire Array
-- [5 5 1 1]
-- 22.0000 22.0000 22.0000 22.0000 22.0000
-- 22.0000 22.0000 22.0000 22.0000 22.0000
-- 22.0000 22.0000 22.0000 22.0000 22.0000
-- 22.0000 22.0000 22.0000 22.0000 22.0000
-- 22.0000 22.0000 22.0000 22.0000 22.0000
--
tile
:: Array (a :: *)
-> [Int]
-> Array a
tile a (take 4 . (++repeat 1) -> [x,y,z,w]) =
a `op1` (\p k -> af_tile p k (fromIntegral x) (fromIntegral y) (fromIntegral z) (fromIntegral w))
tile _ _ = error "impossible"
-- | Reorders an Array according to newly specified dimensions
--
-- *FIX ME*
--
-- >>> reorder @Double (scalar 22.0) [5,5]
-- ArrayFire Array
-- [5 5 1 1]
-- 22.0000 22.0000 22.0000 22.0000 22.0000
-- 22.0000 22.0000 22.0000 22.0000 22.0000
-- 22.0000 22.0000 22.0000 22.0000 22.0000
-- 22.0000 22.0000 22.0000 22.0000 22.0000
-- 22.0000 22.0000 22.0000 22.0000 22.0000
--
reorder
:: Array (a :: *)
-> [Int]
-> Array a
reorder a (take 4 . (++ repeat 0) -> [x,y,z,w]) =
a `op1` (\p k -> af_reorder p k (fromIntegral x) (fromIntegral y) (fromIntegral z) (fromIntegral w))
reorder _ _ = error "impossible"
-- | Shift elements in an Array along a specified dimension (elements will wrap).
--
-- >>> shift (vector @Double 4 [1..]) 2 0 0 0
-- ArrayFire Array
-- [4 1 1 1]
-- 3.0000
-- 4.0000
-- 1.0000
-- 2.0000
--
shift
:: Array (a :: *)
-> Int
-> Int
-> Int
-> Int
-> Array a
shift a (fromIntegral -> x) (fromIntegral -> y) (fromIntegral -> z) (fromIntegral -> w) =
a `op1` (\p k -> af_shift p k x y z w)
-- | Modify dimensions of array
--
-- >>> moddims (vector @Double 3 [1..]) [1,3]
-- ArrayFire Array
-- [1 3 1 1]
-- 1.0000 2.0000 3.0000
--
moddims
:: forall a
. Array (a :: *)
-> [Int]
-> Array a
moddims (Array fptr) dims =
unsafePerformIO . mask_ . withForeignPtr fptr $ \ptr -> do
newPtr <- alloca $ \aPtr -> do
zeroOutArray aPtr
withArray (fromIntegral <$> dims) $ \dimsPtr -> do
throwAFError =<< af_moddims aPtr ptr n dimsPtr
peek aPtr
Array <$> newForeignPtr af_release_array_finalizer newPtr
where
n = fromIntegral (length dims)
-- | Flatten an Array into a single dimension
--
-- >>> flat (matrix @Double (2,2) [[1..],[1..]])
-- ArrayFire Array
-- [4 1 1 1]
-- 1.0000
-- 2.0000
-- 1.0000
-- 2.0000
--
-- >>> flat $ cube @Int (2,2,2) [[[1,1],[1,1]],[[1,1],[1,1]]]
-- ArrayFire Array
-- [8 1 1 1]
-- 1
-- 1
-- 1
-- 1
-- 1
-- 1
-- 1
-- 1
--
flat
:: Array a
-> Array a
flat = (`op1` af_flat)
-- | Flip the values of an Array along a specified dimension
--
-- >>> matrix @Double (2,2) [[2,2],[3,3]]
-- ArrayFire Array
-- [2 2 1 1]
-- 2.0000 3.0000
-- 2.0000 3.0000
--
-- >>> A.flip (matrix @Double (2,2) [[2,2],[3,3]]) 1
-- ArrayFire Array
-- [2 2 1 1]
-- 3.0000 2.0000
-- 3.0000 2.0000
--
flip
:: Array a
-> Int
-> Array a
flip a (fromIntegral -> dim) =
a `op1` (\p k -> af_flip p k dim)
-- | Create a lower triangular matrix from input array.
--
-- >>> lower (constant [2,2] 10 :: Array Double) True
-- ArrayFire Array
-- [2 2 1 1]
-- 1.0000 0.0000
-- 10.0000 1.0000
--
lower
:: Array a
-- ^ is the input matrix
-> Bool
-- ^ boolean parameter specifying if the diagonal elements should be 1
-> Array a
lower a (fromIntegral . fromEnum -> b) =
a `op1` (\p k -> af_lower p k b)
-- | Create an upper triangular matrix from input array.
--
-- >>> upper (constant [2,2] 10 :: Array Double) True
-- ArrayFire Array
-- [2 2 1 1]
-- 1.0000 10.0000
-- 0.0000 1.0000
--
upper
:: Array a
-> Bool
-> Array a
upper a (fromIntegral . fromEnum -> b) =
a `op1` (\p k -> af_upper p k b)
-- | Selects elements from two arrays based on the values of a binary conditional array.
--
-- >>> cond = vector @CBool 5 [1,0,1,0,1]
-- >>> arr1 = vector @Double 5 (repeat 1)
-- >>> arr2 = vector @Double 5 (repeat 2)
-- >>> select cond arr1 arr2
-- ArrayFire Array
-- [5 1 1 1]
-- 1.0000
-- 2.0000
-- 1.0000
-- 2.0000
-- 1.0000
--
select
:: Array CBool
-- ^ is the conditional array
-> Array a
-- ^ is the array containing elements from the true part of the condition
-> Array a
-- ^ is the array containing elements from the false part of the condition
-> Array a
-- ^ is the output containing elements of a when cond is true else elements from b
select a b c = op3 a b c af_select
-- | Selects elements from two arrays based on the values of a binary conditional array.
--
-- <http://arrayfire.org/docs/group__data__func__select.htm#gab6886120d0bac4717276910e468bbe88>
--
-- >>> cond = vector @CBool 5 [1,0,1,0,1]
-- >>> arr1 = vector @Double 5 (repeat 1)
-- >>> x = 99
-- >>> selectScalarR cond arr1 x
-- ArrayFire Array
-- [5 1 1 1]
-- 1.0000
-- 99.0000
-- 1.0000
-- 99.0000
-- 1.0000
--
selectScalarR
:: Array CBool
-- ^ is the conditional array
-> Array a
-- ^ is the array containing elements from the true part of the condition
-> Double
-- ^ is a scalar assigned to out when cond is false
-> Array a
-- ^ the output containing elements of a when cond is true else elements from b
selectScalarR a b c = op2 a b (\p w x -> af_select_scalar_r p w x c)
-- | Selects elements from two arrays based on the values of a binary conditional array.
--
-- [ArrayFire Docs](http://arrayfire.org/docs/group__data__func__select.htm#ga0ccdc05779f88cab5095bce987c2da9d)
--
-- >>> cond = vector @CBool 5 [1,0,1,0,1]
-- >>> arr1 = vector @Double 5 (repeat 1)
-- >>> x = 99
-- >>> selectScalarL cond x arr1
-- ArrayFire Array
-- [5 1 1 1]
-- 99.0000
-- 1.0000
-- 99.0000
-- 1.0000
-- 99.0000
--
selectScalarL
:: Array CBool
-- ^ the conditional array
-> Double
-- ^ a scalar assigned to out when cond is true
-> Array a
-- ^ the array containing elements from the false part of the condition
-> Array a
-- ^ is the output containing elements of a when cond is true else elements from b
selectScalarL a n b = op2 a b (\p w x -> af_select_scalar_l p w n x)
-- af_err af_replace(af_array a, const af_array cond, const af_array b);
-- af_err af_replace_scalar(af_array a, const af_array cond, const double b);