-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathUtil.hs
More file actions
285 lines (270 loc) · 6.91 KB
/
Util.hs
File metadata and controls
285 lines (270 loc) · 6.91 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
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ViewPatterns #-}
--------------------------------------------------------------------------------
-- |
-- Module : ArrayFire.Util
-- Copyright : David Johnson (c) 2019-2020
-- License : BSD 3
-- Maintainer : David Johnson <djohnson.m@gmail.com>
-- Stability : Experimental
-- Portability : GHC
--
-- Various utilities for working with the ArrayFire C library
--
-- @
-- import qualified ArrayFire as A
-- import Control.Monad
--
-- main :: IO ()
-- main = do
-- let arr = A.constant [1,1,1,1] 10
-- idx <- A.saveArray "key" arr "file.array" False
-- foundIndex <- A.readArrayKeyCheck "file.array" "key"
-- when (idx == foundIndex) $ do
-- array <- A.readArrayKey "file.array" "key"
-- print array
-- @
-- @
-- ArrayFire Array
-- [ 1 1 1 1 ]
-- 10
-- @
--------------------------------------------------------------------------------
module ArrayFire.Util where
import Control.Exception
import Data.Proxy
import Foreign.C.String
import Foreign.ForeignPtr
import Foreign.Marshal hiding (void)
import Foreign.Storable
import System.IO.Unsafe
import ArrayFire.Internal.Types
import ArrayFire.Internal.Util
import ArrayFire.Exception
import ArrayFire.FFI
-- | Retrieve version for ArrayFire API
--
-- @
-- >>> 'print' '=<<' 'getVersion'
-- @
-- @
-- (3.6.4)
-- @
getVersion :: IO (Int,Int,Int)
getVersion =
alloca $ \x ->
alloca $ \y ->
alloca $ \z -> do
throwAFError =<< af_get_version x y z
(,,) <$> (fromIntegral <$> peek x)
<*> (fromIntegral <$> peek y)
<*> (fromIntegral <$> peek z)
-- | Prints array to stdout
--
-- @
-- >>> 'printArray' (constant \@'Double' [1] 1)
-- @
-- @
-- ArrayFire Array
-- [ 1 1 1 1 ]
-- 1.0
-- @
printArray
:: Array a
-- ^ Input 'Array'
-> IO ()
printArray (Array fptr) =
mask_ . withForeignPtr fptr $ \ptr ->
throwAFError =<< af_print_array ptr
-- | Gets git revision of ArrayFire
--
-- @
-- >>> 'putStrLn' '=<<' 'getRevision'
-- @
-- @
-- 1b8030c5
-- @
getRevision :: IO String
getRevision = peekCString =<< af_get_revision
-- | Prints 'Array' with error codes
--
-- @
-- >>> printArrayGen "test" (constant \@'Double' [1] 1) 2
-- @
-- @
-- ArrayFire Array
-- [ 1 1 1 1 ]
-- 1.00
-- @
printArrayGen
:: String
-- ^ is the expression or name of the array
-> Array a
-- ^ is the input array
-> Int
-- ^ precision for the display
-> IO ()
printArrayGen s (Array fptr) (fromIntegral -> prec) = do
mask_ . withForeignPtr fptr $ \ptr ->
withCString s $ \cstr ->
throwAFError =<< af_print_array_gen cstr ptr prec
-- | Saves 'Array' to disk
--
-- Save an array to a binary file.
-- The 'saveArray' and readArray functions are designed to provide store and read access to arrays using files written to disk.
-- <http://arrayfire.org/docs/group__stream__func__save.htm>
--
-- @
-- >>> saveArray "my array" (constant \@'Double' [1] 1) "array.file" 'True'
-- @
-- @
-- 0
-- @
saveArray
:: String
-- ^ An expression used as tag/key for the 'Array' during readArray
-> Array a
-- ^ Input 'Array'
-> FilePath
-- ^ Path that 'Array' will be saved
-> Bool
-- ^ Used to append to an existing file when 'True' and create or overwrite an existing file when 'False'
-> IO Int
-- ^ The index location of the 'Array' in the file
saveArray key (Array fptr) filename (fromIntegral . fromEnum -> append) = do
mask_ . withForeignPtr fptr $ \ptr ->
alloca $ \ptrIdx -> do
withCString key $ \keyCstr ->
withCString filename $ \filenameCstr -> do
throwAFError =<<
af_save_array ptrIdx keyCstr
ptr filenameCstr append
fromIntegral <$> peek ptrIdx
-- | Reads Array by index
--
-- The 'saveArray' and readArray functions are designed to provide store and read access to arrays using files written to disk.
-- <http://arrayfire.org/docs/group__stream__func__save.htm>
--
-- @
-- >>> readArrayIndex "array.file" 0
-- @
-- @
-- ArrayFire Array
-- [ 1 1 1 1 ]
-- 10.0000
-- @
readArrayIndex
:: FilePath
-- ^ Path to 'Array' location
-> Int
-- ^ Index into 'Array'
-> IO (Array a)
readArrayIndex str (fromIntegral -> idx') =
withCString str $ \cstr ->
createArray' (\p -> af_read_array_index p cstr idx')
-- | Reads 'Array' by key
--
-- @
-- >>> readArrayKey "array.file" "my array"
-- @
-- @
-- ArrayFire 'Array'
-- [ 1 1 1 1 ]
-- 10.0000
-- @
readArrayKey
:: FilePath
-- ^ Path to 'Array'
-> String
-- ^ Key of 'Array' on disk
-> IO (Array a)
-- ^ Returned 'Array'
readArrayKey fn key =
withCString fn $ \fcstr ->
withCString key $ \kcstr ->
createArray' (\p -> af_read_array_key p fcstr kcstr)
-- | Reads Array, checks if a key exists in the specified file
--
-- When reading by key, it may be a good idea to run this function first to check for the key and then call the readArray using the index.
-- <http://arrayfire.org/docs/group__stream__func__read.htm#ga31522b71beee2b1c06d49b5aa65a5c6f>
--
-- @
-- >>> readArrayCheck "array.file" "my array"
-- @
-- @
-- 0
-- @
readArrayKeyCheck
:: FilePath
-- ^ Path to file
-> String
-- ^ Key
-> IO Int
-- ^ is the tag/name of the array to be read. The key needs to have an exact match.
readArrayKeyCheck a b =
withCString a $ \acstr ->
withCString b $ \bcstr ->
fromIntegral <$>
afCall1 (\p -> af_read_array_key_check p acstr bcstr)
-- | Convert ArrayFire 'Array' to 'String', used for 'Show' instance.
--
-- @
-- >>> 'putStrLn' '$' 'arrayString' (constant \@'Double' 10 [1,1,1,1])
-- @
-- @
-- ArrayFire 'Array'
-- [ 1 1 1 1 ]
-- 10.0000
-- @
arrayString
:: Array a
-- ^ Input 'Array'
-> String
-- ^ 'String' representation of 'Array'
arrayString a = arrayToString "ArrayFire Array" a 4 True
-- | Convert ArrayFire Array to String
--
-- @
-- >>> print (constant \@'Double' 10 [1,1,1,1]) 4 'False'
-- @
-- @
-- ArrayFire 'Array'
-- [ 1 1 1 1 ]
-- 10.0000
-- @
arrayToString
:: String
-- ^ Name of 'Array'
-> Array a
-- ^ 'Array' input
-> Int
-- ^ Precision of 'Array' values.
-> Bool
-- ^ If 'True', performs takes the transpose before rendering to 'String'
-> String
-- ^ 'Array' rendered to 'String'
arrayToString expr (Array fptr) (fromIntegral -> prec) (fromIntegral . fromEnum -> trans) =
unsafePerformIO . mask_ . withForeignPtr fptr $ \aptr ->
withCString expr $ \expCstr ->
alloca $ \ocstr -> do
throwAFError =<< af_array_to_string ocstr expCstr aptr prec trans
peekCString =<< peek ocstr
-- | Retrieve size of ArrayFire data type
--
-- @
-- >>> 'getSizeOf' ('Proxy' \@ 'Double')
-- @
-- @
-- 8
-- @
getSizeOf
:: forall a . AFType a
=> Proxy a
-- ^ Witness of Haskell type that mirrors ArrayFire type.
-> Int
-- ^ Size of ArrayFire type
getSizeOf proxy =
unsafePerformIO . mask_ . alloca $ \csize -> do
throwAFError =<< af_get_size_of csize (afType proxy)
fromIntegral <$> peek csize