-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathData.hs
More file actions
212 lines (194 loc) · 5.89 KB
/
Data.hs
File metadata and controls
212 lines (194 loc) · 5.89 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
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleContexts #-}
module ArrayFire.Data where
import Control.Exception
import Control.Monad
import Data.Complex
import Data.Proxy
import Data.Word
import Foreign.C.String
import Foreign.C.Types
import Foreign.Marshal hiding (void)
import Foreign.Marshal.Array
import Foreign.ForeignPtr
import Foreign.Ptr
import Foreign.Storable
import GHC.Int
import GHC.TypeLits
import ArrayFire.Internal.Array
import ArrayFire.Exception
import ArrayFire.FFI
import ArrayFire.Types
import ArrayFire.Internal.Defines
import ArrayFire.Internal.Data
constant
:: forall dims
. (Dims dims)
=> Double -> IO (Array Double)
constant val = do
ptr <- alloca $ \ptrPtr -> mask_ $ do
dimArray <- newArray dimt
throwAFError =<< af_constant ptrPtr val n dimArray typ
free dimArray
peek ptrPtr
Array <$>
newForeignPtr
af_release_array_finalizer
ptr
where
n = fromIntegral (length dimt)
dimt = toDims (Proxy @ dims)
typ = afType (Proxy @ Double)
constantComplex
:: forall dims
. (Dims dims)
=> Complex Double
-> IO (Array (Complex Double))
constantComplex val = do
ptr <- alloca $ \ptrPtr -> mask_ $ do
dimArray <- newArray dimt
throwAFError =<< af_constant_complex ptrPtr (realPart val) (imagPart val) n dimArray typ
free dimArray
peek ptrPtr
Array <$>
newForeignPtr
af_release_array_finalizer
ptr
where
n = fromIntegral (length dimt)
dimt = toDims (Proxy @ dims)
typ = afType (Proxy @ (Complex Double))
constantLong
:: forall dims
. (Dims dims)
=> Int64
-> IO (Array Int64)
constantLong val = do
ptr <- alloca $ \ptrPtr -> mask_ $ do
dimArray <- newArray dimt
throwAFError =<< af_constant_long ptrPtr (fromIntegral val) n dimArray
free dimArray
peek ptrPtr
Array <$>
newForeignPtr
af_release_array_finalizer
ptr
where
n = fromIntegral (length dimt)
dimt = toDims (Proxy @ dims)
typ = afType (Proxy @ Int64)
constantULong
:: forall dims
. (Dims dims)
=> Word64
-> IO (Array Word64)
constantULong val = do
ptr <- alloca $ \ptrPtr -> mask_ $ do
dimArray <- newArray dimt
throwAFError =<< af_constant_ulong ptrPtr (fromIntegral val) n dimArray
free dimArray
peek ptrPtr
Array <$>
newForeignPtr
af_release_array_finalizer
ptr
where
n = fromIntegral (length dimt)
dimt = toDims (Proxy @ dims)
typ = afType (Proxy @ (Complex Double))
range
:: forall dims a
. (Dims dims, AFType a)
=> Int
-> IO (Array a)
range k = do
ptr <- alloca $ \ptrPtr -> mask_ $ do
dimArray <- newArray dimt
throwAFError =<< af_range ptrPtr n dimArray k typ
free dimArray
peek ptrPtr
Array <$>
newForeignPtr
af_release_array_finalizer
ptr
where
n = fromIntegral (length dimt)
dimt = toDims (Proxy @ dims)
typ = afType (Proxy @ a)
iota
:: forall dims tdims a
. (Dims dims, Dims tdims, AFType a, KnownNat tdims)
=> IO (Array a)
iota = do
ptr <- alloca $ \ptrPtr -> mask_ $ do
dimArray <- newArray dimt
tdimArray <- newArray tdimt
throwAFError =<< af_iota ptrPtr n dimArray tn tdimArray typ
free dimArray
peek ptrPtr
Array <$>
newForeignPtr
af_release_array_finalizer
ptr
where
n = fromIntegral (length dimt)
dimt = toDims (Proxy @ dims)
tn = fromIntegral (length dimt)
tdimt = toDims (Proxy @ tdims)
typ = afType (Proxy @ a)
identity
:: forall dims a
. (Dims dims, AFType a)
=> IO (Array a)
identity = do
ptr <- alloca $ \ptrPtr -> mask_ $ do
dimArray <- newArray dimt
throwAFError =<< af_identity ptrPtr n dimArray typ
free dimArray
peek ptrPtr
Array <$>
newForeignPtr
af_release_array_finalizer
ptr
where
n = fromIntegral (length dimt)
dimt = toDims (Proxy @ dims)
typ = afType (Proxy @ a)
diagCreate
:: AFType (a :: *)
=> Array a
-> Int
-> Array a
diagCreate x n =
x `op1` (\p a -> af_diag_create p a n)
diagExtract
:: AFType (a :: *)
=> Array a
-> Int
-> Array a
diagExtract x n =
x `op1` (\p a -> af_diag_extract p a n)
-- af_err af_join(af_array *out, const int dim, const af_array first, const af_array second);
-- af_err af_join_many(af_array *out, const int dim, const unsigned n_arrays, const af_array *inputs);
-- af_err af_tile(af_array *out, const af_array in, const unsigned x, const unsigned y, const unsigned z, const unsigned w);
-- af_err af_reorder(af_array *out, const af_array in, const unsigned x, const unsigned y, const unsigned z, const unsigned w);
-- af_err af_shift(af_array *out, const af_array in, const int x, const int y, const int z, const int w);
-- af_err af_moddims(af_array *out, const af_array in, const unsigned ndims, const dim_t * const dims);
-- af_err af_flat(af_array *out, const af_array in);
-- af_err af_flip(af_array *out, const af_array in, const unsigned dim);
-- af_err af_lower(af_array *out, const af_array in, bool is_unit_diag);
-- af_err af_upper(af_array *out, const af_array in, bool is_unit_diag);
-- af_err af_select(af_array *out, const af_array cond, const af_array a, const af_array b);
-- af_err af_select_scalar_r(af_array *out, const af_array cond, const af_array a, const double b);
-- af_err af_select_scalar_l(af_array *out, const af_array cond, const double a, const af_array b);
-- 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);