Skip to content

Commit

Permalink
Adding functions from array.h
Browse files Browse the repository at this point in the history
  • Loading branch information
pavanky committed Feb 26, 2017
1 parent 60e06eb commit 9cbe920
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 21 deletions.
1 change: 1 addition & 0 deletions arrayfire.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require('arrayfire.lib')
require('arrayfire.defines')
require('arrayfire.dim4')
require('arrayfire.util')
require('arrayfire.array')
require('arrayfire.device')
Expand Down
109 changes: 98 additions & 11 deletions arrayfire/array.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require('arrayfire.lib')
require('arrayfire.defines')
require('arrayfire.dim4')
local ffi = require( "ffi" )

local funcs = {}
Expand Down Expand Up @@ -67,15 +68,22 @@ Array.__index = Array

local c_dim4_t = af.ffi.c_dim4_t
local c_uint_t = af.ffi.c_uint_t
local c_array_p = af.ffi.c_array_p
local c_ptr_t = af.ffi.c_ptr_t
local Dim4 = af.Dim4

local add_finalizer = function(arr_ptr)
return ffi.gc(arr_ptr[0], af.clib.af_release_array)
local c_array_p = function(ptr)
local arr_ptr = ffi.new('void *[1]', ptr)
arr_ptr[0] = ffi.gc(arr_ptr[0], af.clib.af_release_array)
return arr_ptr
end

Array.__init = function(data, dims, dtype, source)
local init = function(ptr)
local self = setmetatable({}, Array)
self._array = ptr
return self
end

Array.__init = function(data, dims, dtype, source)
if data then
assert(af.istable(data))
end
Expand All @@ -87,32 +95,109 @@ Array.__init = function(data, dims, dtype, source)
c_dims = c_dim4_t(dims or (data and {#data} or {}))
c_ndims = c_uint_t(dims and #dims or (data and 1 or 0))

nelement = 1
count = 1
for i = 1,tonumber(c_ndims) do
nelement = nelement * c_dims[i - 1]
count = count * c_dims[i - 1]
end
nelement = tonumber(nelement)
count = tonumber(count)

local atype = dtype or af.dtype.f32
local res = c_array_p()
if not data then
af.clib.af_create_handle(res, c_ndims, c_dims, atype)
else
c_data = ffi.new(af.dtype_names[atype + 1] .. '[?]', nelement, data)
c_data = c_ptr_t(af.dtype_names[atype + 1], count, data)
af.clib.af_create_array(res, c_data, c_ndims, c_dims, atype)
end
self.__arr = add_finalizer(res)
return self
return Array.init(res[0])
end

Array.__tostring = function(self)
return 'arrayfire.Array\n'
end

Array.get = function(self)
return self.__arr
return self._array
end

-- TODO: implement Array.write

Array.copy = function(self)
local res = c_array_p()
af.clib.af_copy_array(res, self._array)
return Array.init(res[0])
end

Array.softCopy = function(self)
local res = c_array_p()
af.clib.af_copy_array(res, self._array)
return Array.init(res[0])
end

Array.elements = function(self)
local res = c_ptr_t('dim_t')
af.clib.af_get_elements(res, self._array)
return tonumber(res[0])
end

Array.type = function(self)
local res = c_ptr_t('af_dtype')
af.clib.af_get_type(res, self._array)
return tonumber(res[0])
end

Array.typeName = function(self)
local res = c_ptr_t('af_dtype')
af.clib.af_get_type(res, self._array)
return af.dtype_names[tonumber(res[0])]
end

Array.dims = function(self)
local res = c_dim4_t()
af.clib.af_get_dims(res + 0, res + 1, res + 2, res + 3, self._array)
return Dim4(tonumber(res[0]), tonumber(res[1]),
tonumber(res[2]), tonumber(res[3]))
end

Array.numdims = function(self)
local res = c_ptr_t('unsigned int')
af.clib.af_get_numdims(res, self._array)
return tonumber(res[0])
end

local funcs = {
isEmpty = 'is_empty',
isScalar = 'is_scalar',
isRow = 'is_row',
isColumn = 'is_column',
isVector = 'is_vector',
isComplex = 'is_complex',
isReal = 'is_real',
isDouble = 'is_double',
isSingle = 'is_single',
isRealFloating = 'is_realfloating',
isFloating = 'is_floating',
isInteger = 'is_integer',
isBool = 'is_bool',
}

for name, cname in pairs(funcs) do
Array[name] = function(self)
local res = c_ptr_t('bool')
af.clib['af_' .. cname](res, self._array)
return res[0]
end
end

Array.eval = function(self)
af.clib.af_eval(self._array)
end

-- Useful aliases
Array.ndims = Array.numdims
Array.nElement = Array.elements
Array.clone = Array.copy

setmetatable(
Array,
{
Expand All @@ -124,3 +209,5 @@ setmetatable(

af.Array = Array
af.ffi.add_finalizer = add_finalizer
af.ffi.c_array_p = c_array_p
af.Array.init = init
36 changes: 36 additions & 0 deletions arrayfire/dim4.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require('arrayfire.lib')
require('arrayfire.defines')
local ffi = require( "ffi" )
require 'string'

local Dim4 = {}
Dim4.__index = Dim4

setmetatable(
Dim4,
{
__call = function(cls, ...)
return cls.__init(...)
end
}
)

Dim4.__init = function(d1, d2, d3, d4)
local self = setmetatable({d1 or 1, d2 or 1, d3 or 1, d4 or 1}, Dim4)
return self
end

Dim4.__tostring = function(self)
return string.format('[%d, %d, %d, %d]', self[1], self[2], self[3], self[4])
end

Dim4.ndims = function(self)
for i = 4,1,-1 do
if self[i] ~= 1 then
return self[i] == 0 and 0 or i
end
end
return self[1] == 0 and 0 or 1
end

af.Dim4 =Dim4
9 changes: 4 additions & 5 deletions arrayfire/lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ af.lib.cdef = function(funcs)
end
end


af.isnumber = function(val)
return type(val) == "number"
end
Expand All @@ -74,14 +73,14 @@ af.ffi.c_void_p = function()
return ffi.new('void *')
end

af.ffi.c_array_p = function(ptr)
return ffi.new('void *[1]', ptr)
end

af.ffi.c_dim_t = function(number)
return ffi.new('dim_t', number)
end

af.ffi.c_ptr_t = function(ptr_type, count, values)
return ffi.new(ptr_type .. ' [?]', count or 1, values)
end

af.ffi.c_uint_t = function(number)
return ffi.new('unsigned int', number)
end
Expand Down
2 changes: 1 addition & 1 deletion arrayfire/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ funcs[34] = [[
af.lib.cdef(funcs)

af.print = function(arr)
af.clib.af_print_array_gen(ffi.cast("char *", "ArrayFire Array"), arr:get(), 4)
af.clib.af_print_array_gen(ffi.cast("char *", "ArrayFire Array"), arr._array, 4)
end
9 changes: 5 additions & 4 deletions rocks/arrayfire-scm-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ build = {
type = "builtin",
modules = {
arrayfire = "arrayfire.lua",
["arrayfire.lib"] = "arrayfire/lib.lua",
["arrayfire.util"] = "arrayfire/util.lua",
["arrayfire.array"] = "arrayfire/array.lua",
["arrayfire.lib"] = "arrayfire/lib.lua",
["arrayfire.util"] = "arrayfire/util.lua",
["arrayfire.array"] = "arrayfire/array.lua",
["arrayfire.defines"] = "arrayfire/defines.lua",
["arrayfire.device"] = "arrayfire/device.lua",
["arrayfire.device"] = "arrayfire/device.lua",
["arrayfire.dim4"] = "arrayfire/dim4.lua",
},
}

0 comments on commit 9cbe920

Please sign in to comment.