-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathFFI.hs
More file actions
248 lines (229 loc) · 7.41 KB
/
FFI.hs
File metadata and controls
248 lines (229 loc) · 7.41 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
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
module ArrayFire.FFI where
import ArrayFire.Exception
import ArrayFire.Types
import ArrayFire.Internal.Defines
import ArrayFire.Internal.Array
import Control.Exception
import Control.Monad
import Foreign.ForeignPtr
import Foreign.Storable
import Foreign.Ptr
import Foreign.C
import Foreign.Marshal.Alloc
import System.IO.Unsafe
op3
:: Array a
-> Array a
-> Array a
-> (Ptr AFArray -> AFArray -> AFArray -> AFArray -> IO AFErr)
-> Array a
op3 (Array fptr1) (Array fptr2) (Array fptr3) op =
unsafePerformIO $ do
withForeignPtr fptr1 $ \ptr1 ->
withForeignPtr fptr2 $ \ptr2 -> do
withForeignPtr fptr3 $ \ptr3 -> do
ptr <-
alloca $ \ptrInput -> do
throwAFError =<< op ptrInput ptr1 ptr2 ptr3
peek ptrInput
fptr <- newForeignPtr af_release_array_finalizer ptr
pure (Array fptr)
op2
:: Array a
-> Array a
-> (Ptr AFArray -> AFArray -> AFArray -> IO AFErr)
-> Array a
op2 (Array fptr1) (Array fptr2) op =
unsafePerformIO $ do
withForeignPtr fptr1 $ \ptr1 ->
withForeignPtr fptr2 $ \ptr2 -> do
ptr <-
alloca $ \ptrInput -> do
throwAFError =<< op ptrInput ptr1 ptr2
peek ptrInput
fptr <- newForeignPtr af_release_array_finalizer ptr
pure (Array fptr)
op2p
:: Array a
-> (Ptr AFArray -> Ptr AFArray -> AFArray -> IO AFErr)
-> (Array a, Array a)
op2p (Array fptr1) op =
unsafePerformIO $ do
(x,y) <- withForeignPtr fptr1 $ \ptr1 -> do
alloca $ \ptrInput1 -> do
alloca $ \ptrInput2 -> do
throwAFError =<< op ptrInput1 ptrInput2 ptr1
(,) <$> peek ptrInput1 <*> peek ptrInput2
fptrA <- newForeignPtr af_release_array_finalizer x
fptrB <- newForeignPtr af_release_array_finalizer y
pure (Array fptrA, Array fptrB)
op3p
:: Array a
-> (Ptr AFArray -> Ptr AFArray -> Ptr AFArray -> AFArray -> IO AFErr)
-> (Array a, Array a, Array a)
op3p (Array fptr1) op =
unsafePerformIO $ do
(x,y,z) <- withForeignPtr fptr1 $ \ptr1 -> do
alloca $ \ptrInput1 -> do
alloca $ \ptrInput2 -> do
alloca $ \ptrInput3 -> do
throwAFError =<< op ptrInput1 ptrInput2 ptrInput3 ptr1
(,,) <$> peek ptrInput1 <*> peek ptrInput2 <*> peek ptrInput3
fptrA <- newForeignPtr af_release_array_finalizer x
fptrB <- newForeignPtr af_release_array_finalizer y
fptrC <- newForeignPtr af_release_array_finalizer z
pure (Array fptrA, Array fptrB, Array fptrC)
op3p1
:: Storable b
=> Array a
-> (Ptr AFArray -> Ptr AFArray -> Ptr AFArray -> Ptr b -> AFArray -> IO AFErr)
-> (Array a, Array a, Array a, b)
op3p1 (Array fptr1) op =
unsafePerformIO $ do
(x,y,z,g) <- withForeignPtr fptr1 $ \ptr1 -> do
alloca $ \ptrInput1 -> do
alloca $ \ptrInput2 -> do
alloca $ \ptrInput3 -> do
alloca $ \ptrInput4 -> do
throwAFError =<< op ptrInput1 ptrInput2 ptrInput3 ptrInput4 ptr1
(,,,) <$> peek ptrInput1
<*> peek ptrInput2
<*> peek ptrInput3
<*> peek ptrInput4
fptrA <- newForeignPtr af_release_array_finalizer x
fptrB <- newForeignPtr af_release_array_finalizer y
fptrC <- newForeignPtr af_release_array_finalizer z
pure (Array fptrA, Array fptrB, Array fptrC, g)
op2p2
:: Array a
-> Array a
-> (Ptr AFArray -> Ptr AFArray -> AFArray -> AFArray -> IO AFErr)
-> (Array a, Array a)
op2p2 (Array fptr1) (Array fptr2) op =
unsafePerformIO $ do
(x,y) <-
withForeignPtr fptr1 $ \ptr1 -> do
withForeignPtr fptr2 $ \ptr2 -> do
alloca $ \ptrInput1 -> do
alloca $ \ptrInput2 -> do
throwAFError =<< op ptrInput1 ptrInput2 ptr1 ptr2
(,) <$> peek ptrInput1 <*> peek ptrInput2
fptrA <- newForeignPtr af_release_array_finalizer x
fptrB <- newForeignPtr af_release_array_finalizer y
pure (Array fptrA, Array fptrB)
op1
:: Array a
-> (Ptr AFArray -> AFArray -> IO AFErr)
-> Array a
op1 (Array fptr1) op =
unsafePerformIO $ do
withForeignPtr fptr1 $ \ptr1 -> do
ptr <-
alloca $ \ptrInput -> do
throwAFError =<< op ptrInput ptr1
peek ptrInput
fptr <- newForeignPtr af_release_array_finalizer ptr
pure (Array fptr)
op1b
:: Storable b
=> Array a
-> (Ptr AFArray -> Ptr b -> AFArray -> IO AFErr)
-> (b, Array a)
op1b (Array fptr1) op =
unsafePerformIO $
withForeignPtr fptr1 $ \ptr1 -> do
(y,x) <-
alloca $ \ptrInput1 -> do
alloca $ \ptrInput2 -> do
throwAFError =<< op ptrInput1 ptrInput2 ptr1
(,) <$> peek ptrInput1 <*> peek ptrInput2
fptr <- newForeignPtr af_release_array_finalizer y
pure (x, Array fptr)
afCall
:: IO AFErr
-> IO ()
afCall = (throwAFError =<<)
inPlace :: Array a -> (AFArray -> IO AFErr) -> Array a
inPlace r@(Array fptr) op =
(unsafePerformIO $
withForeignPtr fptr $ \ptr ->
throwAFError =<< op ptr) `seq` r
afCall1
:: Storable a
=> (Ptr a -> IO AFErr)
-> IO a
afCall1 op =
alloca $ \ptrInput -> do
throwAFError =<< op ptrInput
peek ptrInput
infoFromArray
:: Storable a
=> Array b
-> (Ptr a -> AFArray -> IO AFErr)
-> a
infoFromArray (Array fptr1) op =
unsafePerformIO $ do
withForeignPtr fptr1 $ \ptr1 -> do
alloca $ \ptrInput -> do
throwAFError =<< op ptrInput ptr1
peek ptrInput
infoFromArray2
:: (Storable a, Storable b)
=> Array arr
-> (Ptr a -> Ptr b -> AFArray -> IO AFErr)
-> (a,b)
infoFromArray2 (Array fptr1) op =
unsafePerformIO $ do
withForeignPtr fptr1 $ \ptr1 -> do
alloca $ \ptrInput1 -> do
alloca $ \ptrInput2 -> do
throwAFError =<< op ptrInput1 ptrInput2 ptr1
(,) <$> peek ptrInput1 <*> peek ptrInput2
infoFromArray22
:: (Storable a, Storable b)
=> Array arr
-> Array arr
-> (Ptr a -> Ptr b -> AFArray -> AFArray -> IO AFErr)
-> (a,b)
infoFromArray22 (Array fptr1) (Array fptr2) op =
unsafePerformIO $ do
withForeignPtr fptr1 $ \ptr1 -> do
withForeignPtr fptr2 $ \ptr2 -> do
alloca $ \ptrInput1 -> do
alloca $ \ptrInput2 -> do
throwAFError =<< op ptrInput1 ptrInput2 ptr1 ptr2
(,) <$> peek ptrInput1 <*> peek ptrInput2
infoFromArray3
:: (Storable a, Storable b, Storable c)
=> Array arr
-> (Ptr a -> Ptr b -> Ptr c -> AFArray -> IO AFErr)
-> (a,b,c)
infoFromArray3 (Array fptr1) op =
unsafePerformIO $
withForeignPtr fptr1 $ \ptr1 -> do
alloca $ \ptrInput1 -> do
alloca $ \ptrInput2 -> do
alloca $ \ptrInput3 -> do
throwAFError =<< op ptrInput1 ptrInput2 ptrInput3 ptr1
(,,) <$> peek ptrInput1
<*> peek ptrInput2
<*> peek ptrInput3
infoFromArray4
:: (Storable a, Storable b, Storable c, Storable d)
=> Array arr
-> (Ptr a -> Ptr b -> Ptr c -> Ptr d -> AFArray -> IO AFErr)
-> (a,b,c,d)
infoFromArray4 (Array fptr1) op =
unsafePerformIO $
withForeignPtr fptr1 $ \ptr1 ->
alloca $ \ptrInput1 ->
alloca $ \ptrInput2 ->
alloca $ \ptrInput3 ->
alloca $ \ptrInput4 -> do
throwAFError =<< op ptrInput1 ptrInput2 ptrInput3 ptrInput4 ptr1
(,,,) <$> peek ptrInput1
<*> peek ptrInput2
<*> peek ptrInput3
<*> peek ptrInput4