Skip to content

Commit

Permalink
Merge branch 'devel'
Browse files Browse the repository at this point in the history
  • Loading branch information
pavanky committed Feb 23, 2017
2 parents 5cd1e77 + dbdaab1 commit eb84175
Show file tree
Hide file tree
Showing 130 changed files with 546 additions and 14,095 deletions.
21 changes: 0 additions & 21 deletions CMakeLists.txt

This file was deleted.

76 changes: 0 additions & 76 deletions CMakeModules/build_lua.cmake

This file was deleted.

21 changes: 0 additions & 21 deletions CMakeModules/lua_MSVC.patch

This file was deleted.

87 changes: 25 additions & 62 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,32 @@

[ArrayFire](https://github.com/arrayfire/arrayfire) is a high performance library for parallel computing with an easy-to-use API. It enables users to write scientific computing code that is portable across CUDA, OpenCL and CPU devices. This project provides Lua bindings for the ArrayFire library.

The wrapper is currently compliant with ArrayFire 3.2 API (and higher). If you find any bugs, please report them [here](https://github.com/arrayfire/arrayfire-lua/issues).
The wrapper is currently compliant with ArrayFire 3.4 API (and higher). If you find any bugs, please report them [here](https://github.com/arrayfire/arrayfire-lua/issues).

## Example

```lua
local AF = require("arrayfire")
Sample code:

AF.main(function()
local x = AF.randu(5, "f32")
AF.print("x", x)
AF.print("min of x", AF.min(x))
AF.print("max of x", AF.max(x))
end)
```
local af = require('arrayfire')
af.info()
a = af.Array{1, 2, 3, 4}
af.print(a)
```

Sample output:

```
$ lua examples/lua/helloworld/intro.lua
ArrayFire v3.2.1 (CUDA, 64-bit Linux, build f263db0)
Platform: CUDA Toolkit 7.5, Driver: 358.16
[0] GeForce GTX 690, 2047 MB, CUDA Compute 3.0
-1- GeForce GTX 690, 2048 MB, CUDA Compute 3.0
x
[5 1 1 1]
0.7402
0.9210
0.0390
0.9690
0.9251
min of x
[1 1 1 1]
0.0390
max of x
[1 1 1 1]
0.9690
ArrayFire v3.5.0 (CUDA, 64-bit Linux, build 06e605b0)
Platform: CUDA Toolkit 8, Driver: 378.13
[0] GeForce GTX 950, 1996 MB, CUDA Compute 5.2
ArrayFire Array
[4 1 1 1]
1.0000
2.0000
3.0000
4.0000
```

Expand All @@ -49,8 +39,8 @@ TODO

### Requirements

- `cmake`
- `Visual Studio` on Windows, `clang` / `gcc` on Linux / OSX.
- [luarocks](https://github.com/keplerproject/luarocks)
- [luaffi](https://github.com/facebook/luaffifb) (Not required for LuaJIT)

### Get ArrayFire libraries

Expand All @@ -65,37 +55,10 @@ You can install ArrayFire using one of the following two ways.

### Building the Lua module

**Windows**

1. Launch `cmake-gui`. Set source and build directories.
2. Press configure.
3. Select `generator` as `Visual Studio 12 2013 Win64`.
- You can choose a different generator as long as it is Win64.
4. Set `CMAKE_INSTALL_PREFIX` to a location of choice.
5. Press generate. The generated visual studio solution file will be present in the build directory.
6. Open the VS solution file and build the `INSTALL` project.

**Linux / OSX**

1. Make sure the environment variable `ArrayFire_DIR` is set to `/path/to/arrayfire/share/ArrayFire/cmake`.
2. Create a build directory and `cd` into it.
3. Run `cmake /path/to/arrayfire-lua/ -DCMAKE_INSTALL_PREFIX=package`.
4. Run `make`

### Setting up Lua paths

**Windows**

> SET LUA_PATH=C:\path\to\install\location\arrayfire\?.lua;;
> SET LUA_CPATH=C:\path\to\install\location\?.dll;;
> lua.exe helloworld/helloworld.lua

**Linux**

$ export LUA_PATH="/path/to/install/location/arrayfire/?.lua;;"
$ export LUA_CPATH="/path/to/install/location/?.so;;"
$ lua helloworld/helloworld.lua

```
cd /path/to/arrayfire-lua/
luarocks make
```

You should now be good to go!

Expand Down
7 changes: 7 additions & 0 deletions arrayfire.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require('arrayfire.lib')
require('arrayfire.defines')
require('arrayfire.util')
require('arrayfire.array')
require('arrayfire.device')

return af
126 changes: 126 additions & 0 deletions arrayfire/array.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
require('arrayfire.lib')
require('arrayfire.defines')
local ffi = require( "ffi" )

local funcs = {}
funcs[30] = [[
af_err af_create_array(af_array *arr, const void * const data,
const unsigned ndims, const dim_t * const dims, const af_dtype type);
af_err af_create_handle(af_array *arr, const unsigned ndims,
const dim_t * const dims, const af_dtype type);
af_err af_copy_array(af_array *arr, const af_array in);
af_err af_write_array(af_array arr, const void *data, const size_t bytes, af_source src);
af_err af_get_data_ptr(void *data, const af_array arr);
af_err af_release_array(af_array arr);
af_err af_retain_array(af_array *out, const af_array in);
af_err af_get_elements(dim_t *elems, const af_array arr);
af_err af_get_type(af_dtype *type, const af_array arr);
af_err af_get_dims(dim_t *d0, dim_t *d1, dim_t *d2, dim_t *d3,
const af_array arr);
af_err af_get_numdims(unsigned *result, const af_array arr);
af_err af_is_empty (bool *result, const af_array arr);
af_err af_is_scalar (bool *result, const af_array arr);
af_err af_is_row (bool *result, const af_array arr);
af_err af_is_column (bool *result, const af_array arr);
af_err af_is_vector (bool *result, const af_array arr);
af_err af_is_complex (bool *result, const af_array arr);
af_err af_is_real (bool *result, const af_array arr);
af_err af_is_double (bool *result, const af_array arr);
af_err af_is_single (bool *result, const af_array arr);
af_err af_is_realfloating (bool *result, const af_array arr);
af_err af_is_floating (bool *result, const af_array arr);
af_err af_is_integer (bool *result, const af_array arr);
af_err af_is_bool (bool *result, const af_array arr);
af_err af_eval(af_array in);
]]

funcs[31] = [[
af_err af_get_data_ref_count(int *use_count, const af_array in);
]]


funcs[34] = [[
af_err af_eval_multiple(const int num, af_array *arrays);
af_err af_set_manual_eval_flag(bool flag);
af_err af_get_manual_eval_flag(bool *flag);
af_err af_is_sparse (bool *result, const af_array arr);
]]

af.lib.cdef(funcs)

local Array = {}
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 add_finalizer = function(arr_ptr)
return ffi.gc(arr_ptr[0], af.clib.af_release_array)
end

Array.__init = function(data, dims, dtype, source)
local self = setmetatable({}, Array)

if data then
assert(af.istable(data))
end

if dims then
assert(af.istable(dims))
end

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
for i = 1,tonumber(c_ndims) do
nelement = nelement * c_dims[i - 1]
end
nelement = tonumber(nelement)

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)
af.clib.af_create_array(res, c_data, c_ndims, c_dims, atype)
end
self.__arr = add_finalizer(res)
return self
end

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

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

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

af.Array = Array
af.ffi.add_finalizer = add_finalizer
Loading

0 comments on commit eb84175

Please sign in to comment.