Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArrayFire bindings for Linear Algebra #3

Merged
merged 125 commits into from
Sep 4, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
125 commits
Select commit Hold shift + click to select a range
f64db84
added initialization tests
prasunanand May 31, 2017
c30155a
blas spec
prasunanand May 31, 2017
110e3e1
add BLAS and LAPACK routines
prasunanand May 31, 2017
1344b33
README
prasunanand May 31, 2017
daa476a
LICENSE
prasunanand May 31, 2017
0666396
install instructions
prasunanand May 31, 2017
8128615
blas test modified
prasunanand Jun 1, 2017
58e0dc8
move blas routines to blas.cpp
prasunanand Jun 1, 2017
32bbd43
move lapack routines to lapack.cpp
prasunanand Jun 1, 2017
46e017c
move arith routines to arith.cpp
prasunanand Jun 1, 2017
a36d638
init routines for CUDA and OpenCL
prasunanand Jun 1, 2017
0e7db4e
init methods for Data
prasunanand Jun 1, 2017
9bd6a6c
init methods for Lapack
prasunanand Jun 1, 2017
0b1a6d1
init methods for Blas
prasunanand Jun 1, 2017
b9c7e56
init methods for Backend
prasunanand Jun 1, 2017
66173b1
init Device functions
prasunanand Jun 1, 2017
026966f
init Algorithm functions
prasunanand Jun 1, 2017
c59de6a
init Random functions
prasunanand Jun 1, 2017
73cc062
add paths to gemspec and modify gem name
prasunanand Jun 22, 2017
9edf6b9
convert info method to singleton method
prasunanand Jun 22, 2017
d6c54cc
concentrating on opencl
prasunanand Jun 22, 2017
fb29b2a
cleanup: remove ndims, array, array2, dimension
prasunanand Jun 28, 2017
359472d
use af_array to store array pointer
prasunanand Jun 28, 2017
aed9306
modify initializer function to store af_array pointer
prasunanand Jun 28, 2017
084fbb7
move around files
prasunanand Jun 28, 2017
babc63c
arith: add elementwise operator declarations
prasunanand Jun 28, 2017
388f662
arith: add elementwise operator definitions
prasunanand Jun 28, 2017
173ccc3
arith: add unary operator declarations
prasunanand Jun 28, 2017
0e28e9e
arith: add unary operator definitions
prasunanand Jun 28, 2017
5177d90
arith: define arith methods for array class
prasunanand Jun 28, 2017
de148e1
blas: implement blas routines
prasunanand Jun 28, 2017
c006d86
cleanup
prasunanand Jun 28, 2017
16fd9d0
more cleanup
prasunanand Jun 28, 2017
8ac0ce7
modify lapack declarations
prasunanand Jun 28, 2017
23593d3
add and load lapack functions
prasunanand Jun 28, 2017
ab2be5f
convert Algorithm methods to class methods
prasunanand Jun 28, 2017
6c7696e
modify declarations for Algorithm methods
prasunanand Jun 28, 2017
a684fae
add and load lapack functions
prasunanand Jun 28, 2017
69dc115
move data functions to data.c
prasunanand Jun 29, 2017
a35f2f4
move random functions to random.c
prasunanand Jun 29, 2017
1b49497
move opencl functions to opencl.c
prasunanand Jun 29, 2017
711f46c
move device functions to device.c
prasunanand Jun 29, 2017
6950524
move cuda functions to cuda.c
prasunanand Jun 29, 2017
d941e7d
move backend functions to backend.c
prasunanand Jun 29, 2017
7de7492
constant: implement Data#constant method
prasunanand Jun 29, 2017
c940258
constant: implement Data#constant_long and Data#constant_ulong method
prasunanand Jun 29, 2017
7161297
constant: implement Data#range and Data#identity method
prasunanand Jun 29, 2017
afd245d
constant: implement Data#diag_create and Data#diag_extract
prasunanand Jun 29, 2017
b3a478f
constant: implement Data methods join, tile, reorder, shift
prasunanand Jun 29, 2017
a2cd98c
constant: implement Data methods flat and flip
prasunanand Jun 29, 2017
31670da
constant: implement Data select methods
prasunanand Jun 29, 2017
e2faf88
constant: implement Data replace methods
prasunanand Jun 29, 2017
9392985
constant: implement Data#lower Data#upper methods
prasunanand Jun 29, 2017
5949de1
adding methods to Af_Array class: initialization
prasunanand Jun 30, 2017
b8bd136
modify names of Af_Array methods
prasunanand Jun 30, 2017
8dbda74
Impement af_array bindings
prasunanand Jun 30, 2017
b643439
add bindings for Index module
prasunanand Jun 30, 2017
8441733
implement to_cpu and ndims method to Af_Array
prasunanand Jun 30, 2017
d158eab
add retain and get_reference bindings
prasunanand Jun 30, 2017
b79ba12
generalize Af_Array#new to accept ndims upto 4
prasunanand Jun 30, 2017
c1fe2db
implement Af_Array singleton methods to crate_array and create_handle
prasunanand Jun 30, 2017
98b8f8a
implemented deep array coppying
prasunanand Jun 30, 2017
f4bf35e
add elementwise comparision operators
prasunanand Jul 7, 2017
a23cd42
added equality operator
prasunanand Jul 7, 2017
4be52d6
use bool array to store result in eqeq
prasunanand Jul 7, 2017
73a8787
modify blas spec
prasunanand Jul 7, 2017
db68dc2
added rspec-its as a dependency
prasunanand Jul 7, 2017
3a0bb7b
modify tests in creation_spec and reauire rspec-its
prasunanand Jul 7, 2017
12ea9bc
dims and elements attributes
prasunanand Jul 7, 2017
e8606c9
added contributing guideline
prasunanand Jul 12, 2017
5efc291
added rdoc task
prasunanand Jul 12, 2017
9e56cb5
switch to minitest
prasunanand Jul 21, 2017
2f30174
modify tests according to minitest
prasunanand Jul 21, 2017
d08b71d
run test after compile
prasunanand Jul 21, 2017
170101d
added arith test
prasunanand Jul 21, 2017
dcf1c54
added algorithm tests
prasunanand Jul 23, 2017
a4b22e4
added more tests
prasunanand Jul 23, 2017
abba793
modify test parameters and add more tests
prasunanand Jul 23, 2017
397f40d
remove unused declarations
prasunanand Jul 23, 2017
a246364
get rid of warnings
prasunanand Jul 23, 2017
8e8ee70
minor modifications
prasunanand Jul 23, 2017
e9576cb
added unary method tests
prasunanand Jul 23, 2017
7d0e5b3
added blas tests
prasunanand Jul 23, 2017
c76214a
added lapack tests
prasunanand Jul 23, 2017
ef6a83f
implemented lapack factorization methods
prasunanand Jul 24, 2017
062b5ac
bind ArrayFire::Backend apis
prasunanand Aug 1, 2017
c368e68
added tests for ArrayFire::Data
prasunanand Aug 1, 2017
b33bf94
init ArrayFire::Statistics class
prasunanand Aug 2, 2017
261bfc4
add statistics methods
prasunanand Aug 2, 2017
66ec7d5
init ArrayFire::Util class
prasunanand Aug 2, 2017
a6d4b0b
Added tests for ArrayFire::Statistics#methods
prasunanand Aug 2, 2017
fe49c82
Implement Random Engine
prasunanand Aug 3, 2017
eb2bcd9
init multiple dtypes
prasunanand Aug 5, 2017
4fc9e5a
pass matrix property as an argument to #matmul
prasunanand Aug 5, 2017
266c154
add more definitions
prasunanand Aug 5, 2017
5dcddf7
pass mat_properties to #matmul and #dot
prasunanand Aug 5, 2017
8514495
arrays and lists for passing symbols
prasunanand Aug 7, 2017
f0a4d3d
add bindings for device module
prasunanand Aug 7, 2017
00f68e5
ArrayFire::Backend bindings
prasunanand Aug 8, 2017
db253e9
added CUDA specific bindings to handle CUDA code
prasunanand Aug 8, 2017
296e61e
added opencl bindings
prasunanand Aug 8, 2017
7e2297e
add comments and headers for Sparse
prasunanand Aug 8, 2017
e080fdd
ArrayFire::Sparse class init
prasunanand Aug 8, 2017
f52d753
move arith functions to arith.c
prasunanand Aug 8, 2017
0a42d5f
sparse array bindings
prasunanand Aug 8, 2017
8a0bc75
init examples
prasunanand Aug 9, 2017
f9e1c6d
added factorization tests for lapack
prasunanand Aug 14, 2017
0a352ec
add solve test
prasunanand Aug 14, 2017
7201e8f
add Util.print_array
prasunanand Aug 16, 2017
2f8d12f
added sync method
prasunanand Aug 16, 2017
3db8864
use doubles as default
prasunanand Aug 16, 2017
fa5c7f1
added eval bindings
prasunanand Aug 16, 2017
d311d00
Random#methods added
prasunanand Aug 17, 2017
b904f0b
added util methods
prasunanand Aug 17, 2017
e415637
complete Sparse bindings
prasunanand Aug 18, 2017
1a370f5
specify type for random engine
prasunanand Aug 18, 2017
e250a84
added NMatrix - ArrayFire interface
prasunanand Aug 21, 2017
faae5dc
device_info
prasunanand Aug 23, 2017
c3b143e
Util.get_version
prasunanand Aug 23, 2017
ae121f5
added Af_Array#to_s method
prasunanand Aug 23, 2017
4bafb2d
gem version and unified backend
prasunanand Aug 26, 2017
9f1f2aa
device multiple returns
prasunanand Aug 26, 2017
70fd885
replace malloc and free with Ruby GC aware mallocs and xfree respecti…
prasunanand Aug 31, 2017
edec567
added exception handling
prasunanand Aug 31, 2017
18fcd08
correct test for sinh
prasunanand Sep 4, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions arrayfire.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ Gem::Specification.new do |gem|
gem.add_development_dependency 'rake-compiler', '~>0.8'
gem.add_development_dependency 'rdoc', '~>4.0', '>=4.0.1'
gem.add_development_dependency "minitest", "~> 5.0"

gem.add_development_dependency 'nmatrix', '>= 0.2.1'
end
17 changes: 16 additions & 1 deletion ext/mri/arrayfire.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include "arrayfire.h"
#include <stdio.h>
#include <math.h>

Expand All @@ -17,6 +16,9 @@ VALUE Random = Qnil;
VALUE Sparse = Qnil;
VALUE Statistics = Qnil;
VALUE Util = Qnil;
extern "C++" {
VALUE cNMatrix;
}

// prototypes
void Init_arrayfire();
Expand Down Expand Up @@ -330,6 +332,12 @@ DECL_UNARY_RUBY_ACCESSOR(ceil)
static VALUE arf_eqeq(VALUE left_val, VALUE right_val);
static VALUE arf_eqeq_approx(VALUE left_val, VALUE right_val);

// Interfaces

static VALUE arf_af_array_to_nmatrix(VALUE self);
extern VALUE arf_nmatrix_to_af_array_method(VALUE nmatrix);
afstruct* arf_nmatrix_to_af_array(VALUE nm);

void Init_arrayfire() {
ArrayFire = rb_define_module("ArrayFire");

Expand Down Expand Up @@ -411,6 +419,11 @@ void Init_arrayfire() {
rb_define_method(Af_Array, "floor", (METHOD)arf_unary_floor, 0);
rb_define_method(Af_Array, "ceil", (METHOD)arf_unary_ceil, 0);

rb_define_method(Af_Array, "to_nmatrix", (METHOD)arf_af_array_to_nmatrix, 0);

cNMatrix = rb_define_class("NMatrix", rb_cObject);
rb_define_method(cNMatrix, "to_af_array", (METHOD)arf_nmatrix_to_af_array_method, 0);

Algorithm = rb_define_class_under(ArrayFire, "Algorithm", rb_cObject);
rb_define_singleton_method(Algorithm, "sum", (METHOD)arf_sum, 2);
rb_define_singleton_method(Algorithm, "sum_nan", (METHOD)arf_sum_nan, 3);
Expand Down Expand Up @@ -753,3 +766,5 @@ static VALUE arf_eqeq_approx(VALUE left_val, VALUE right_val) {
#include "cmodules/sparse.c"
#include "cmodules/statistics.c"
#include "cmodules/util.c"

#include "interfaces/nmatrix.c"
29 changes: 24 additions & 5 deletions ext/mri/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

extension_name = 'arrayfire'

dir_config(extension_name)
nmatrix_path = Gem::Specification.find_all_by_name('nmatrix').compact
abort "Cannot locate NMatrix installation" unless nmatrix_path
nmatrix_header_dir = File.join(nmatrix_path[0].require_path)

$INSTALLFILES = [
['ruby_arrayfire.h' , '$(archdir)'],
Expand All @@ -15,19 +17,36 @@
$CXXFLAGS = ["-Wall -Werror=return-type",$CXXFLAGS].join(" ")
$CPPFLAGS = ["-Wall -Werror=return-type",$CPPFLAGS].join(" ")

dir_config('arrayfire', '/usr/local/include/', '/usr/local/lib/')

# $LOCAL_LIBS =
LIBDIR = RbConfig::CONFIG['libdir']
INCLUDEDIR = RbConfig::CONFIG['includedir']

HEADER_DIRS = [INCLUDEDIR, '/usr/local/include/', '/usr/local/include/af/']
HEADER_DIRS = [
'/opt/local/include',
'/usr/local/include',
INCLUDEDIR,
'/usr/include',
nmatrix_header_dir
]

LIB_DIRS = [
'/opt/local/lib',
'/usr/local/lib',
LIBDIR,
'/usr/lib',
nmatrix_header_dir
]

dir_config(extension_name, HEADER_DIRS, LIB_DIRS)

have_library('afopencl')
have_library('afcuda')
have_library('cusolver')
have_library('cudart')
have_library('cufft')
have_library('cublas')
have_library('nmatrix')
have_header("nmatrix_config.h")
abort "Cannot locate NMatrix header files : nmatrix.h" unless find_header("nmatrix.h")

basenames = %w{ruby_arrayfire}
$objs = basenames.map { |b| "#{b}.o" }
Expand Down
58 changes: 58 additions & 0 deletions ext/mri/interfaces/nmatrix.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
static VALUE arf_af_array_to_nmatrix(VALUE self) {
afstruct* input;
Data_Get_Struct(self, afstruct, input);
dim_t count;
uint ndims;
af_get_numdims(&ndims, input->carray);

dim_t* dims = (dim_t*)malloc(ndims * sizeof(dim_t));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its good practice in C to declare all the variables at the top of the function.


af_get_dims(&dims[0], &dims[1], &dims[2], &dims[3], input->carray);

size_t* shape = (size_t*)malloc(ndims * sizeof(size_t));;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double colon at the end.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its outdated commit.

for (dim_t index = 0; index < ndims; index++){
shape[index] = (size_t)(dims[index]);
}

af_get_elements(&count, input->carray);

double* elements = (double*)malloc(count * sizeof(double));
af_get_data_ptr(elements, input->carray);

return rb_nmatrix_dense_create(nm::FLOAT64, shape, ndims, elements, (int)count);
}

extern VALUE arf_nmatrix_to_af_array_method(VALUE nmatrix) {
if (NM_DIM(nmatrix) > 4) {
rb_raise(rb_eStandardError,
"NMatrix must not have greater than 4 dimensions.");
}

if (NM_DTYPE(nmatrix) == nm::FLOAT64) {
return Data_Wrap_Struct(Af_Array, NULL, arf_free, arf_nmatrix_to_af_array(nmatrix));
}
else {
rb_raise(rb_eStandardError,
"NMatrix should be either :complex64, :complex128, :int32 or :float64 type.");
}
return Qnil;
}


afstruct* arf_nmatrix_to_af_array(VALUE nm) {
DENSE_STORAGE* nmat = NM_STORAGE_DENSE(nm);
afstruct* output = ALLOC(afstruct);

if (nmat->dtype != nm::FLOAT64) {
rb_raise(rb_eStandardError, "requires dtype of :float64 to convert to an Af_Array");
}

dim_t* shape = (dim_t*)malloc(nmat->dim * sizeof(dim_t));;
for (size_t index = 0; index < nmat->dim; index++){
shape[index] = (size_t)(nmat->shape[index]);
}

af_create_array(&output->carray, nmat->elements, nmat->dim, shape, f64);

return output;
}
2 changes: 2 additions & 0 deletions ext/mri/ruby_arrayfire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <stdio.h>
#include <math.h>
#include <map>
#include "nmatrix.h"


/*
* Project Includes
Expand Down
5 changes: 3 additions & 2 deletions ext/mri/ruby_arrayfire.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
#define RUBY_ARRAYFIRE_H
#endif

#include <ruby.h>

typedef struct AF_STRUCT
{
af_array carray;
Expand Down Expand Up @@ -43,6 +41,9 @@ rb_array_const_ptr(VALUE a)
#ifdef __cplusplus
typedef VALUE (*METHOD)(...);
//}; // end of namespace nm

// Interfaces

#endif


Expand Down