Skip to content

Commit 3efd323

Browse files
committed
Adding arrayfire.Array class and necessary helper functions
1 parent 1e96bcd commit 3efd323

File tree

3 files changed

+116
-6
lines changed

3 files changed

+116
-6
lines changed

arrayfire/array.lua

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
require('arrayfire.lib')
2-
require('class')
2+
require('arrayfire.defines')
33
local ffi = require( "ffi" )
44

55
local funcs = {}
@@ -61,3 +61,66 @@ funcs[34] = [[
6161
]]
6262

6363
af.lib.cdef(funcs)
64+
65+
local Array = {}
66+
Array.__index = Array
67+
68+
local c_dim4_t = af.ffi.c_dim4_t
69+
local c_uint_t = af.ffi.c_uint_t
70+
local c_array_p = af.ffi.c_array_p
71+
72+
local add_finalizer = function(arr_ptr)
73+
return ffi.gc(arr_ptr[0], af.clib.af_release_array)
74+
end
75+
76+
Array.__init = function(data, dims, dtype, source)
77+
local self = setmetatable({}, Array)
78+
79+
if data then
80+
assert(af.istable(data))
81+
end
82+
83+
if dims then
84+
assert(af.istable(dims))
85+
end
86+
87+
c_dims = c_dim4_t(dims or (data and {#data} or {}))
88+
c_ndims = c_uint_t(dims and #dims or (data and 1 or 0))
89+
90+
nelement = 1
91+
for i = 1,tonumber(c_ndims) do
92+
nelement = nelement * c_dims[i - 1]
93+
end
94+
nelement = tonumber(nelement)
95+
96+
local atype = dtype or af.dtype.f32
97+
local res = c_array_p()
98+
if not data then
99+
af.clib.af_create_handle(res, c_ndims, c_dims, atype)
100+
else
101+
c_data = ffi.new(af.dtype_names[atype + 1] .. '[?]', nelement, data)
102+
af.clib.af_create_array(res, c_data, c_ndims, c_dims, atype)
103+
end
104+
self.__arr = add_finalizer(res)
105+
return self
106+
end
107+
108+
Array.__tostring = function(self)
109+
return 'arrayfire.Array\n'
110+
end
111+
112+
Array.get = function(self)
113+
return self.__arr
114+
end
115+
116+
setmetatable(
117+
Array,
118+
{
119+
__call = function(cls, ...)
120+
return cls.__init(...)
121+
end
122+
}
123+
)
124+
125+
af.Array = Array
126+
af.ffi.add_finalizer = add_finalizer

arrayfire/lib.lua

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,24 @@ if ffi.abi('32bit') then
2424
else
2525
ffi.cdef[[typedef long long dim_t;]]
2626
end
27+
2728
ffi.cdef[[
28-
typedef void * af_array;
29-
typedef int af_dtype;
30-
typedef int af_err;
31-
typedef int af_source;
32-
af_err af_get_version (int *, int *, int *);
29+
typedef void * af_array;
30+
typedef int af_dtype;
31+
typedef int af_err;
32+
typedef int af_source;
33+
34+
typedef struct af_cfloat {
35+
float real;
36+
float imag;
37+
} af_cfloat;
38+
39+
typedef struct af_cdouble {
40+
double real;
41+
double imag;
42+
} af_cdouble;
43+
44+
af_err af_get_version (int *, int *, int *);
3345
]]
3446

3547
local major, minor, patch = ffi.new("int[1]"), ffi.new("int[1]"), ffi.new("int[1]")
@@ -46,3 +58,34 @@ af.lib.cdef = function(funcs)
4658
end
4759
end
4860
end
61+
62+
63+
af.isnumber = function(val)
64+
return type(val) == "number"
65+
end
66+
67+
af.istable = function(val)
68+
return type(val) == "table"
69+
end
70+
71+
af.ffi = {}
72+
73+
af.ffi.c_void_p = function()
74+
return ffi.new('void *')
75+
end
76+
77+
af.ffi.c_array_p = function(ptr)
78+
return ffi.new('void *[1]', ptr)
79+
end
80+
81+
af.ffi.c_dim_t = function(number)
82+
return ffi.new('dim_t', number)
83+
end
84+
85+
af.ffi.c_uint_t = function(number)
86+
return ffi.new('unsigned int', number)
87+
end
88+
89+
af.ffi.c_dim4_t = function(dims)
90+
return ffi.new('dim_t[4]', dims or {1, 1, 1, 1})
91+
end

arrayfire/util.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,7 @@ funcs[34] = [[
2727
]]
2828

2929
af.lib.cdef(funcs)
30+
31+
af.print = function(arr)
32+
af.clib.af_print_array(arr:get())
33+
end

0 commit comments

Comments
 (0)