1
1
require (' arrayfire.lib' )
2
2
require (' arrayfire.defines' )
3
+ require (' arrayfire.dim4' )
3
4
local ffi = require ( " ffi" )
4
5
5
6
local funcs = {}
@@ -67,15 +68,22 @@ Array.__index = Array
67
68
68
69
local c_dim4_t = af .ffi .c_dim4_t
69
70
local c_uint_t = af .ffi .c_uint_t
70
- local c_array_p = af .ffi .c_array_p
71
+ local c_ptr_t = af .ffi .c_ptr_t
72
+ local Dim4 = af .Dim4
71
73
72
- local add_finalizer = function (arr_ptr )
73
- return ffi .gc (arr_ptr [0 ], af .clib .af_release_array )
74
+ local c_array_p = function (ptr )
75
+ local arr_ptr = ffi .new (' void *[1]' , ptr )
76
+ arr_ptr [0 ] = ffi .gc (arr_ptr [0 ], af .clib .af_release_array )
77
+ return arr_ptr
74
78
end
75
79
76
- Array . __init = function (data , dims , dtype , source )
80
+ local init = function (ptr )
77
81
local self = setmetatable ({}, Array )
82
+ self ._array = ptr
83
+ return self
84
+ end
78
85
86
+ Array .__init = function (data , dims , dtype , source )
79
87
if data then
80
88
assert (af .istable (data ))
81
89
end
@@ -87,32 +95,109 @@ Array.__init = function(data, dims, dtype, source)
87
95
c_dims = c_dim4_t (dims or (data and {# data } or {}))
88
96
c_ndims = c_uint_t (dims and # dims or (data and 1 or 0 ))
89
97
90
- nelement = 1
98
+ count = 1
91
99
for i = 1 ,tonumber (c_ndims ) do
92
- nelement = nelement * c_dims [i - 1 ]
100
+ count = count * c_dims [i - 1 ]
93
101
end
94
- nelement = tonumber (nelement )
102
+ count = tonumber (count )
95
103
96
104
local atype = dtype or af .dtype .f32
97
105
local res = c_array_p ()
98
106
if not data then
99
107
af .clib .af_create_handle (res , c_ndims , c_dims , atype )
100
108
else
101
- c_data = ffi . new (af .dtype_names [atype + 1 ] .. ' [?] ' , nelement , data )
109
+ c_data = c_ptr_t (af .dtype_names [atype + 1 ], count , data )
102
110
af .clib .af_create_array (res , c_data , c_ndims , c_dims , atype )
103
111
end
104
- self .__arr = add_finalizer (res )
105
- return self
112
+ return Array .init (res [0 ])
106
113
end
107
114
108
115
Array .__tostring = function (self )
109
116
return ' arrayfire.Array\n '
110
117
end
111
118
112
119
Array .get = function (self )
113
- return self .__arr
120
+ return self ._array
121
+ end
122
+
123
+ -- TODO: implement Array.write
124
+
125
+ Array .copy = function (self )
126
+ local res = c_array_p ()
127
+ af .clib .af_copy_array (res , self ._array )
128
+ return Array .init (res [0 ])
129
+ end
130
+
131
+ Array .softCopy = function (self )
132
+ local res = c_array_p ()
133
+ af .clib .af_copy_array (res , self ._array )
134
+ return Array .init (res [0 ])
135
+ end
136
+
137
+ Array .elements = function (self )
138
+ local res = c_ptr_t (' dim_t' )
139
+ af .clib .af_get_elements (res , self ._array )
140
+ return tonumber (res [0 ])
114
141
end
115
142
143
+ Array .type = function (self )
144
+ local res = c_ptr_t (' af_dtype' )
145
+ af .clib .af_get_type (res , self ._array )
146
+ return tonumber (res [0 ])
147
+ end
148
+
149
+ Array .typeName = function (self )
150
+ local res = c_ptr_t (' af_dtype' )
151
+ af .clib .af_get_type (res , self ._array )
152
+ return af .dtype_names [tonumber (res [0 ])]
153
+ end
154
+
155
+ Array .dims = function (self )
156
+ local res = c_dim4_t ()
157
+ af .clib .af_get_dims (res + 0 , res + 1 , res + 2 , res + 3 , self ._array )
158
+ return Dim4 (tonumber (res [0 ]), tonumber (res [1 ]),
159
+ tonumber (res [2 ]), tonumber (res [3 ]))
160
+ end
161
+
162
+ Array .numdims = function (self )
163
+ local res = c_ptr_t (' unsigned int' )
164
+ af .clib .af_get_numdims (res , self ._array )
165
+ return tonumber (res [0 ])
166
+ end
167
+
168
+ local funcs = {
169
+ isEmpty = ' is_empty' ,
170
+ isScalar = ' is_scalar' ,
171
+ isRow = ' is_row' ,
172
+ isColumn = ' is_column' ,
173
+ isVector = ' is_vector' ,
174
+ isComplex = ' is_complex' ,
175
+ isReal = ' is_real' ,
176
+ isDouble = ' is_double' ,
177
+ isSingle = ' is_single' ,
178
+ isRealFloating = ' is_realfloating' ,
179
+ isFloating = ' is_floating' ,
180
+ isInteger = ' is_integer' ,
181
+ isBool = ' is_bool' ,
182
+ }
183
+
184
+ for name , cname in pairs (funcs ) do
185
+ Array [name ] = function (self )
186
+ local res = c_ptr_t (' bool' )
187
+ af .clib [' af_' .. cname ](res , self ._array )
188
+ return res [0 ]
189
+ end
190
+ end
191
+
192
+ Array .eval = function (self )
193
+ af .clib .af_eval (self ._array )
194
+ end
195
+
196
+ -- Useful aliases
197
+ Array .ndims = Array .numdims
198
+ Array .nElement = Array .elements
199
+ Array .clone = Array .copy
200
+
116
201
setmetatable (
117
202
Array ,
118
203
{
@@ -124,3 +209,5 @@ setmetatable(
124
209
125
210
af .Array = Array
126
211
af .ffi .add_finalizer = add_finalizer
212
+ af .ffi .c_array_p = c_array_p
213
+ af .Array .init = init
0 commit comments