From f64db840e140483358e71f9f1fce09c3d248a41e Mon Sep 17 00:00:00 2001 From: prasun Date: Wed, 31 May 2017 13:52:04 +0530 Subject: [PATCH 001/125] added initialization tests --- spec/creation_spec.rb | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 spec/creation_spec.rb diff --git a/spec/creation_spec.rb b/spec/creation_spec.rb new file mode 100644 index 0000000..d93194b --- /dev/null +++ b/spec/creation_spec.rb @@ -0,0 +1,34 @@ +require 'spec_helper' + +describe ArrayFire::Af_Array do + context '#initialize' do + let(:i) { ArrayFire::Af_Array.new 2, [2,2],[1,2,3,4] } + subject { i } + it { expect(i.ndims).to eq 2 } + it { expect(i.dimension).to eq [2,2] } + it { expect(i.array).to eq [1,2,3,4] } + it{ + pending("only float supported currently") + expect(i.dtype).to eq :float64 + } + end + + context '#addition' do + let(:a) { ArrayFire::Af_Array.new 2, [2,2],[1,2,3,4] } + let(:b) { ArrayFire::Af_Array.new 2, [2,2],[1,2,3,4] } + let(:c) { ArrayFire::Af_Array.new 2, [2,2],[2,4,6,8] } + subject {c} + it {expect(a+b).to eq c} + it {expect(c.ndims).to eq a.ndims} + it {expect(c.dimension).to eq a.dimension} + end + + context '#Equality' do + let(:a) { ArrayFire::Af_Array.new 2, [2,2],[1,2,3,4] } + let(:b) { ArrayFire::Af_Array.new 2, [2,2],[1,2,3,4] } + subject {c} + it {expect(a).to eq b} + it {expect(b.ndims).to eq a.ndims} + it {expect(b.dimension).to eq a.dimension} + end +end From c30155af55cbf60fdbbf3612e24274d3e7d13f36 Mon Sep 17 00:00:00 2001 From: prasun Date: Wed, 31 May 2017 14:00:42 +0530 Subject: [PATCH 002/125] blas spec --- spec/blas_spec.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 spec/blas_spec.rb diff --git a/spec/blas_spec.rb b/spec/blas_spec.rb new file mode 100644 index 0000000..7435e69 --- /dev/null +++ b/spec/blas_spec.rb @@ -0,0 +1,11 @@ +require 'spec_helper' + +describe ArrayFire::BLAS do + context '#matmul' do + let(:a) { ArrayFire::Af_Array.new 2, [2,2],[1,2,3,4] } + let(:b) { ArrayFire::Af_Array.new 2, [2,2],[1,2,3,4] } + let(:c) { ArrayFire::Af_Array.new 2, [2,2],[7,10,15,22] } + subject {c} + it {expect(ArrayFire::BLAS.matmul(a,b)).to eq c} + end +end From 110e3e1b337a7fd9df4d78eef7c1c10c7edf38a7 Mon Sep 17 00:00:00 2001 From: prasun Date: Wed, 31 May 2017 14:18:59 +0530 Subject: [PATCH 003/125] add BLAS and LAPACK routines --- ext/mri/arrayfire.c | 235 +++++++++++++++++++++++++++++++++---- ext/mri/ruby_arrayfire.cpp | 92 +++++++++++---- ext/mri/ruby_arrayfire.h | 42 ++++++- 3 files changed, 318 insertions(+), 51 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 159102e..4014e16 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -1,36 +1,53 @@ -// #include "ruby.h" - #include "arrayfire.h" #include #include -#include "ruby_arrayfire.h" - VALUE ArrayFire = Qnil; VALUE Af_Array = Qnil; VALUE Device = Qnil; VALUE Blas = Qnil; VALUE Lapack = Qnil; -typedef struct AF_STRUCT -{ - size_t dimension; // Method of storage (csc, dense, etc). - size_t array; -}afstruct; - - // prototypes -// void Init_arrayfire(); +void Init_arrayfire(); static VALUE test1(VALUE self); -// VALUE method_arf_init(VALUE self, VALUE val); static VALUE arf_init(int argc, VALUE* argv, VALUE self); static VALUE arf_alloc(VALUE klass); static void arf_free(afstruct* af); +static VALUE ndims(VALUE self); static VALUE dimension(VALUE self); static VALUE array(VALUE self); +static void array2(VALUE self); static VALUE get_info(VALUE self); +static size_t* interpret_shape(VALUE arg, size_t* dim); + +#define DEF_ELEMENTWISE_RUBY_ACCESSOR(oper, name) \ +static VALUE arf_ew_##name(VALUE left_val, VALUE right_val) { \ + return elementwise_op(arf::EW_##oper, left_val, right_val); \ +} + +#define DECL_ELEMENTWISE_RUBY_ACCESSOR(name) static VALUE arf_ew_##name(VALUE left_val, VALUE right_val); +DECL_ELEMENTWISE_RUBY_ACCESSOR(add) + + +static VALUE elementwise_op(arf::ewop_t op, VALUE left_val, VALUE right_val); +/* + * Macro defines an element-wise accessor function for some operation. + * + * This is only responsible for the Ruby accessor! You still have to write the actual functions, obviously. + */ + +static VALUE arf_eqeq(VALUE left_val, VALUE right_val); + + +static VALUE arf_matmul(VALUE self, VALUE left_val, VALUE right_val); +static VALUE arf_cholesky(VALUE self, VALUE val); +static VALUE arf_det(VALUE self); +static VALUE arf_inverse(VALUE self); +static VALUE arf_norm(VALUE self); + void Init_arrayfire() { ArrayFire = rb_define_module("ArrayFire"); rb_define_method(ArrayFire, "test1", (METHOD)test1, 0); @@ -38,15 +55,24 @@ void Init_arrayfire() { Af_Array = rb_define_class_under(ArrayFire, "Af_Array", rb_cObject); rb_define_alloc_func(Af_Array, arf_alloc); rb_define_method(Af_Array, "initialize", (METHOD)arf_init, -1); + rb_define_method(Af_Array, "ndims", (METHOD)ndims, 0); rb_define_method(Af_Array, "dimension", (METHOD)dimension, 0); rb_define_method(Af_Array, "array", (METHOD)array, 0); + rb_define_method(Af_Array, "array2", (METHOD)array2, 0); + rb_define_method(Af_Array, "+",(METHOD)arf_ew_add,1); + rb_define_method(Af_Array, "==",(METHOD)arf_eqeq,1); + rb_define_method(Af_Array, "det",(METHOD)arf_det,0); + rb_define_method(Af_Array, "inverse",(METHOD)arf_inverse,0); + rb_define_method(Af_Array, "norm",(METHOD)arf_norm,0); Device = rb_define_class_under(ArrayFire, "Device", rb_cObject); rb_define_method(Device, "getInfo", (METHOD)get_info, 0); Blas = rb_define_class_under(ArrayFire, "BLAS", rb_cObject); + rb_define_singleton_method(Blas, "matmul", (METHOD)arf_matmul, 2); Lapack = rb_define_class_under(ArrayFire, "LAPACK", rb_cObject); + rb_define_singleton_method(Lapack, "cholesky", (METHOD)arf_cholesky, 1); } VALUE test1(VALUE self) { @@ -57,16 +83,29 @@ VALUE test1(VALUE self) { VALUE arf_init(int argc, VALUE* argv, VALUE self) { - afstruct* afarray; Data_Get_Struct(self, afstruct, afarray); - afarray->dimension = argv[0]; - afarray->array = argv[1]; - + afarray->ndims = FIX2LONG(argv[0]); + afarray->dimension = ALLOC_N(VALUE, argv[0]); + size_t count = 1; + for (size_t index = 0; index < FIX2LONG(argv[0]); index++) { + afarray->dimension[index] = FIX2LONG(RARRAY_AREF(argv[1], index)); + count *= afarray->dimension[index]; + } + afarray->count = count; + afarray->array = ALLOC_N(double, count); + for (size_t index = 0; index < count; index++) { + afarray->array[index] = NUM2DBL(RARRAY_AREF(argv[2], index)); + } + dim_t dims[afarray->ndims] ; + for (size_t index = 0; index < afarray->ndims; ++index){ + dims[index] = (dim_t)afarray->dimension[index]; + } return self; } + static VALUE arf_alloc(VALUE klass) { /* allocate */ @@ -81,13 +120,28 @@ static void arf_free(afstruct* af) free(af); } +static VALUE ndims(VALUE self) +{ + afstruct * af; + + Data_Get_Struct(self, afstruct, af); + + return INT2NUM(af->ndims); +} + static VALUE dimension(VALUE self) { afstruct * af; Data_Get_Struct(self, afstruct, af); - return af->dimension; + VALUE* dimension = ALLOC_N(VALUE, af->ndims); + + for (size_t index = 0; index < af->ndims; ++index){ + dimension[index] = INT2FIX(af->dimension[index]); + } + + return rb_ary_new4(af->ndims, dimension); } static VALUE array(VALUE self) @@ -96,14 +150,155 @@ static VALUE array(VALUE self) Data_Get_Struct(self, afstruct, af); - return af->array; + VALUE* array = ALLOC_N(VALUE, af->count); + + for (size_t index = 0; index < af->count; ++index){ + array[index] = DBL2NUM(af->array[index]); + } + + return rb_ary_new4(af->count, array); +} + +static void array2(VALUE self){ + afstruct * af; + + Data_Get_Struct(self, afstruct, af); } static VALUE get_info(VALUE self) { VALUE x; af_info(); - arf::matmul_benchmark(); return x; } + +DEF_ELEMENTWISE_RUBY_ACCESSOR(ADD, add) + +static VALUE elementwise_op(arf::ewop_t op, VALUE left_val, VALUE right_val) { + + afstruct* left; + afstruct* right; + afstruct* result = ALLOC(afstruct); + + Data_Get_Struct(left_val, afstruct, left); + Data_Get_Struct(right_val, afstruct, right); + + + result->ndims = left->ndims; + result->dimension = left->dimension; + result->count = left->count; + arf::add(result, left, right); + + return Data_Wrap_Struct(CLASS_OF(left_val), NULL, arf_free, result); +} + +static VALUE arf_eqeq(VALUE left_val, VALUE right_val) { + afstruct* left; + afstruct* right; + + bool result = true; + + size_t i; + size_t count = 1; + + Data_Get_Struct(left_val, afstruct, left); + Data_Get_Struct(right_val, afstruct, right); + + printf("%d\n", left->ndims); + + for(size_t i = 0; i < left->ndims; i++){ + if(left->dimension[i]!= right->dimension[i]){ + return Qfalse; + } + } + + for(size_t i = 0; i < left->count; i++){ + if(left->array[i]!= right->array[i]){ + return Qfalse; + } + } + + return Qtrue; +} + +static VALUE arf_matmul(VALUE self, VALUE left_val, VALUE right_val){ + + afstruct* left; + afstruct* right; + afstruct* result = ALLOC(afstruct); + + Data_Get_Struct(left_val, afstruct, left); + Data_Get_Struct(right_val, afstruct, right); + + + result->ndims = left->ndims; + size_t dimension[2]; + dimension[0] = left->dimension[0]; + dimension[1] = right->dimension[1]; + size_t count = dimension[0]*dimension[1]; + result->dimension = dimension; + result->count = count; + + arf::matmul(result, left, right); + + return Data_Wrap_Struct(CLASS_OF(left_val), NULL, arf_free, result); +} + +static VALUE arf_cholesky(VALUE self, VALUE val){ + + afstruct* matrix; + afstruct* result = ALLOC(afstruct); + + Data_Get_Struct(val, afstruct, matrix); + + + result->ndims = matrix->ndims; + result->dimension = matrix->dimension; + result->count = matrix->count; + arf::cholesky_(result, matrix); + + return Data_Wrap_Struct(CLASS_OF(val), NULL, arf_free, result); +} + +static VALUE arf_det(VALUE self){ + + afstruct* matrix; + Data_Get_Struct(self, afstruct, matrix); + + af_array m; + dim_t dims[matrix->ndims] ; + for (size_t index = 0; index < matrix->ndims; ++index){ + dims[index] = (dim_t)matrix->dimension[index]; + } + af_create_array( &m, matrix->array, matrix->ndims, dims, f64 ); + double real, imaginary; + af_det(&real,&imaginary,m); + return DBL2NUM(real); +} + +static VALUE arf_inverse(VALUE self){ + + afstruct* matrix; + afstruct* result = ALLOC(afstruct); + + Data_Get_Struct(self, afstruct, matrix); + + result->ndims = matrix->ndims; + result->dimension = matrix->dimension; + result->count = matrix->count; + arf::inverse_(result, matrix); + + return Data_Wrap_Struct(CLASS_OF(self), NULL, arf_free, result); +} + +static VALUE arf_norm(VALUE self){ + afstruct* matrix; + + Data_Get_Struct(self, afstruct, matrix); + + double norm = arf::norm_(matrix); + + + return DBL2NUM(norm); +} diff --git a/ext/mri/ruby_arrayfire.cpp b/ext/mri/ruby_arrayfire.cpp index a94666c..62c0fd1 100644 --- a/ext/mri/ruby_arrayfire.cpp +++ b/ext/mri/ruby_arrayfire.cpp @@ -13,40 +13,80 @@ namespace arf { - // #include + enum ewop_t { + EW_ADD, + EW_SUB, + EW_MUL, + EW_DIV, + EW_POW, + EW_MOD, + EW_EQEQ, + EW_NEQ, + EW_LT, + EW_GT, + EW_LEQ, + EW_GEQ, + }; using namespace af; -// create a small wrapper to benchmark static array A; // populated before each timing static void fn() { array B = matmul(A, A); // matrix multiply B.eval(); // ensure evaluated } - static void matmul_benchmark() - { - double peak = 0; - try { - int device = 0; - setDevice(device); - info(); - printf("Benchmark N-by-N matrix multiply\n"); - for (int n = 128; n <= 2048; n += 128) { - printf("%4d x %4d: ", n, n); - A = constant(1,n,n); - double time = timeit(fn); // time in seconds - double gflops = 2.0 * powf(n,3) / (time * 1e9); - if (gflops > peak) - peak = gflops; - printf(" %4.0f Gflops\n", gflops); - fflush(stdout); - } - } catch (af::exception& e) { - fprintf(stderr, "%s\n", e.what()); - throw; - } - printf(" ### peak %g GFLOPS\n", peak); + + static void createArray(afstruct *afarray) + { + dim_t dims[afarray->ndims] ; + + for (size_t index = 0; index < afarray->ndims; ++index){ + dims[index] = (dim_t)afarray->dimension[index]; + } + } + + static void hostArray(afstruct *afarray) + { + + } + + static void add(afstruct *result, afstruct *left, afstruct *right) + { + array l = array(left->dimension[0], left->dimension[1], left->array); + array r = array(right->dimension[0], right->dimension[1], right->array); + array res = operator+(l,r); + result->array = res.host(); + } + + static void matmul(afstruct *result, afstruct *left, afstruct *right) + { + array l = array(left->dimension[0], left->dimension[1], left->array); + array r = array(right->dimension[0], right->dimension[1], right->array); + array res = matmul(l,r); + result->array = res.host(); + } + + static void cholesky_(afstruct *result, afstruct *matrix) + { + array m = array(matrix->dimension[0], matrix->dimension[1], matrix->array); + bool is_upper = true; + array res; + cholesky(res, m, is_upper); + result->array = res.host(); + } + + static void inverse_(afstruct *result, afstruct *matrix) + { + array m = array(matrix->dimension[0], matrix->dimension[1], matrix->array); + array res = inverse(m); + result->array = res.host(); + } + + static double norm_(afstruct *matrix) + { + array m = array(matrix->dimension[0], matrix->dimension[1], matrix->array); + return norm(m, AF_NORM_EUCLID, 1, 1); } } extern "C" { #include "arrayfire.c" -} \ No newline at end of file +} diff --git a/ext/mri/ruby_arrayfire.h b/ext/mri/ruby_arrayfire.h index 42b9e69..f6305f5 100644 --- a/ext/mri/ruby_arrayfire.h +++ b/ext/mri/ruby_arrayfire.h @@ -2,6 +2,34 @@ #define RUBY_ARRAYFIRE_H #endif +#include + +typedef struct AF_STRUCT +{ + // af_array arr; + size_t ndims; + size_t count; + size_t* dimension; + double* array; +}afstruct; + +#ifndef HAVE_RB_ARRAY_CONST_PTR +static inline const VALUE * +rb_array_const_ptr(VALUE a) +{ + return FIX_CONST_VALUE_PTR((RBASIC(a)->flags & RARRAY_EMBED_FLAG) ? + RARRAY(a)->as.ary : RARRAY(a)->as.heap.ptr); +} +#endif + +#ifndef RARRAY_CONST_PTR +# define RARRAY_CONST_PTR(a) rb_array_const_ptr(a) +#endif + +#ifndef RARRAY_AREF +# define RARRAY_AREF(a, i) (RARRAY_CONST_PTR(a)[i]) +#endif + /* * Functions */ @@ -11,18 +39,22 @@ typedef VALUE (*METHOD)(...); //}; // end of namespace nm #endif -#include -// #include + #ifdef __cplusplus extern "C" { #endif void Init_arrayfire(); - static void matmul_benchmark(); + static void test(); // External API - + static void createArray(afstruct *afarray); + static void hostArray(afstruct *afarray); + static void add(afstruct *result, afstruct *left, afstruct *right); + static void matmul(afstruct *result, afstruct *left, afstruct *right); + static void cholesky_(afstruct *result, afstruct *matrix); + static void inverse_(afstruct *result, afstruct *matrix); + static double norm_(afstruct *matrix); #ifdef __cplusplus } #endif - From 1344b33c04c1e433b910b1999d50b684c715f3c8 Mon Sep 17 00:00:00 2001 From: prasun Date: Wed, 31 May 2017 14:29:12 +0530 Subject: [PATCH 004/125] README --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index 1b19ddf..1e2beb4 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,9 @@ + + +ArrayFire is a general-purpose library that simplifies the process of developing +software that targets parallel and massively-parallel architectures including +CPUs, GPUs, and other hardware acceleration devices. + # Ruby wrapper for ArrayFire [![Gitter chat](https://badges.gitter.im/gitterHQ/gitter.png)](https://gitter.im/arrayfire/arrayfire-ruby) @@ -10,3 +16,8 @@ git clone https://github.com/arrayfire/arrayfire-rb bundle install rake compile ``` +# LICENSE + +This software is distributed under the [BSD 3-Clause License](LICENSE). + +Copyright © 2017, Prasun Anand and ArrayFire From daa476a5abde25bad8f2e263071576d8dcdfd35f Mon Sep 17 00:00:00 2001 From: prasun Date: Wed, 31 May 2017 14:31:31 +0530 Subject: [PATCH 005/125] LICENSE --- LICENSE | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/LICENSE b/LICENSE index bbc9e5a..7c98723 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,6 @@ -Copyright (c) 2016, ArrayFire +BSD 3-Clause License + +Copyright (c) 2016, Prasun Anand and ArrayFire All rights reserved. Redistribution and use in source and binary forms, with or without @@ -11,7 +13,7 @@ modification, are permitted provided that the following conditions are met: this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. -* Neither the name of arrayfire-rb nor the names of its +* Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. @@ -25,4 +27,3 @@ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - From 0666396bdc740ed4b873b5d91bd0fb5b109c7b81 Mon Sep 17 00:00:00 2001 From: prasun Date: Wed, 31 May 2017 14:47:02 +0530 Subject: [PATCH 006/125] install instructions --- LICENSE | 2 +- README.md | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/LICENSE b/LICENSE index 7c98723..b7b3701 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ BSD 3-Clause License -Copyright (c) 2016, Prasun Anand and ArrayFire +Copyright (c) 2016-2017, Prasun Anand and ArrayFire All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/README.md b/README.md index 1e2beb4..b9d65a3 100644 --- a/README.md +++ b/README.md @@ -10,12 +10,29 @@ CPUs, GPUs, and other hardware acceleration devices. Ruby bindings are a work in progress and are not production ready yet. -## Build from source +## Installation + ```sh git clone https://github.com/arrayfire/arrayfire-rb +cd arrayfire/ +gem install bundler bundle install rake compile +rspec +``` + +If you want to try out the code without installing: + +```sh +rake pry ``` + +To install: + +```sh +rake install +``` + # LICENSE This software is distributed under the [BSD 3-Clause License](LICENSE). From 8128615ee824b54e91f7b1cf78eb186109bb3d2b Mon Sep 17 00:00:00 2001 From: prasun Date: Thu, 1 Jun 2017 19:18:55 +0530 Subject: [PATCH 007/125] blas test modified --- spec/blas_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/blas_spec.rb b/spec/blas_spec.rb index 7435e69..f901127 100644 --- a/spec/blas_spec.rb +++ b/spec/blas_spec.rb @@ -6,6 +6,6 @@ let(:b) { ArrayFire::Af_Array.new 2, [2,2],[1,2,3,4] } let(:c) { ArrayFire::Af_Array.new 2, [2,2],[7,10,15,22] } subject {c} - it {expect(ArrayFire::BLAS.matmul(a,b)).to eq c} + it {expect(ArrayFire::BLAS.matmul(a,b)).array to eq c.array} end end From 58e0dc8db743f287d1d0c6429f238574aa3e6d5c Mon Sep 17 00:00:00 2001 From: prasun Date: Thu, 1 Jun 2017 19:19:23 +0530 Subject: [PATCH 008/125] move blas routines to blas.cpp --- ext/mri/blas.cpp | 7 +++++++ ext/mri/ruby_arrayfire.cpp | 10 +++------- 2 files changed, 10 insertions(+), 7 deletions(-) create mode 100644 ext/mri/blas.cpp diff --git a/ext/mri/blas.cpp b/ext/mri/blas.cpp new file mode 100644 index 0000000..fbaa6d4 --- /dev/null +++ b/ext/mri/blas.cpp @@ -0,0 +1,7 @@ +static void matmul(afstruct *result, afstruct *left, afstruct *right) +{ + array l = array(left->dimension[0], left->dimension[1], left->array); + array r = array(right->dimension[0], right->dimension[1], right->array); + array res = matmul(l,r); + result->array = res.host(); +} diff --git a/ext/mri/ruby_arrayfire.cpp b/ext/mri/ruby_arrayfire.cpp index 62c0fd1..7daf4af 100644 --- a/ext/mri/ruby_arrayfire.cpp +++ b/ext/mri/ruby_arrayfire.cpp @@ -57,13 +57,7 @@ namespace arf { result->array = res.host(); } - static void matmul(afstruct *result, afstruct *left, afstruct *right) - { - array l = array(left->dimension[0], left->dimension[1], left->array); - array r = array(right->dimension[0], right->dimension[1], right->array); - array res = matmul(l,r); - result->array = res.host(); - } + static void cholesky_(afstruct *result, afstruct *matrix) { @@ -86,6 +80,8 @@ namespace arf { array m = array(matrix->dimension[0], matrix->dimension[1], matrix->array); return norm(m, AF_NORM_EUCLID, 1, 1); } + + #include "blas.cpp" } extern "C" { #include "arrayfire.c" From 32bbd4323c0a6521b1a0f656a351d825c95f870d Mon Sep 17 00:00:00 2001 From: prasun Date: Thu, 1 Jun 2017 19:28:41 +0530 Subject: [PATCH 009/125] move lapack routines to lapack.cpp --- ext/mri/lapack.cpp | 21 +++++++++++++++++++++ ext/mri/ruby_arrayfire.cpp | 25 +------------------------ 2 files changed, 22 insertions(+), 24 deletions(-) create mode 100644 ext/mri/lapack.cpp diff --git a/ext/mri/lapack.cpp b/ext/mri/lapack.cpp new file mode 100644 index 0000000..8821316 --- /dev/null +++ b/ext/mri/lapack.cpp @@ -0,0 +1,21 @@ +static void cholesky_(afstruct *result, afstruct *matrix) +{ + array m = array(matrix->dimension[0], matrix->dimension[1], matrix->array); + bool is_upper = true; + array res; + cholesky(res, m, is_upper); + result->array = res.host(); +} + +static void inverse_(afstruct *result, afstruct *matrix) +{ + array m = array(matrix->dimension[0], matrix->dimension[1], matrix->array); + array res = inverse(m); + result->array = res.host(); +} + +static double norm_(afstruct *matrix) +{ + array m = array(matrix->dimension[0], matrix->dimension[1], matrix->array); + return norm(m, AF_NORM_EUCLID, 1, 1); +} \ No newline at end of file diff --git a/ext/mri/ruby_arrayfire.cpp b/ext/mri/ruby_arrayfire.cpp index 7daf4af..a7da91c 100644 --- a/ext/mri/ruby_arrayfire.cpp +++ b/ext/mri/ruby_arrayfire.cpp @@ -57,31 +57,8 @@ namespace arf { result->array = res.host(); } - - - static void cholesky_(afstruct *result, afstruct *matrix) - { - array m = array(matrix->dimension[0], matrix->dimension[1], matrix->array); - bool is_upper = true; - array res; - cholesky(res, m, is_upper); - result->array = res.host(); - } - - static void inverse_(afstruct *result, afstruct *matrix) - { - array m = array(matrix->dimension[0], matrix->dimension[1], matrix->array); - array res = inverse(m); - result->array = res.host(); - } - - static double norm_(afstruct *matrix) - { - array m = array(matrix->dimension[0], matrix->dimension[1], matrix->array); - return norm(m, AF_NORM_EUCLID, 1, 1); - } - #include "blas.cpp" + #include "lapack.cpp" } extern "C" { #include "arrayfire.c" From 46e017c93210f39502d7121e5af299da45cd5548 Mon Sep 17 00:00:00 2001 From: prasun Date: Thu, 1 Jun 2017 19:35:17 +0530 Subject: [PATCH 010/125] move arith routines to arith.cpp --- ext/mri/arith.cpp | 7 +++++++ ext/mri/ruby_arrayfire.cpp | 14 +------------- 2 files changed, 8 insertions(+), 13 deletions(-) create mode 100644 ext/mri/arith.cpp diff --git a/ext/mri/arith.cpp b/ext/mri/arith.cpp new file mode 100644 index 0000000..2f44be4 --- /dev/null +++ b/ext/mri/arith.cpp @@ -0,0 +1,7 @@ +static void add(afstruct *result, afstruct *left, afstruct *right) +{ + array l = array(left->dimension[0], left->dimension[1], left->array); + array r = array(right->dimension[0], right->dimension[1], right->array); + array res = operator+(l,r); + result->array = res.host(); +} diff --git a/ext/mri/ruby_arrayfire.cpp b/ext/mri/ruby_arrayfire.cpp index a7da91c..4580da3 100644 --- a/ext/mri/ruby_arrayfire.cpp +++ b/ext/mri/ruby_arrayfire.cpp @@ -44,19 +44,7 @@ namespace arf { } } - static void hostArray(afstruct *afarray) - { - - } - - static void add(afstruct *result, afstruct *left, afstruct *right) - { - array l = array(left->dimension[0], left->dimension[1], left->array); - array r = array(right->dimension[0], right->dimension[1], right->array); - array res = operator+(l,r); - result->array = res.host(); - } - + #include "arith.cpp" #include "blas.cpp" #include "lapack.cpp" } From a36d63836c00a61bfbfd1a4134214c7d4833b3af Mon Sep 17 00:00:00 2001 From: prasun Date: Thu, 1 Jun 2017 19:39:36 +0530 Subject: [PATCH 011/125] init routines for CUDA and OpenCL --- ext/mri/arrayfire.c | 85 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 84 insertions(+), 1 deletion(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 4014e16..19c22fd 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -4,9 +4,11 @@ VALUE ArrayFire = Qnil; VALUE Af_Array = Qnil; -VALUE Device = Qnil; VALUE Blas = Qnil; +VALUE Cuda = Qnil; +VALUE Device = Qnil; VALUE Lapack = Qnil; +VALUE OpenCL = Qnil; // prototypes @@ -21,6 +23,22 @@ static VALUE array(VALUE self); static void array2(VALUE self); static VALUE get_info(VALUE self); + +static VALUE arf_get_stream(VALUE self); +static VALUE arf_get_native_id(VALUE self); +static VALUE arf_set_native_id(VALUE self); + +static VALUE arf_get_context(VALUE self); +static VALUE arf_get_queue(VALUE self); +static VALUE arf_get_device_id(VALUE self); +static VALUE arf_set_device_id(VALUE self); +static VALUE arf_add_device_context(VALUE self); +static VALUE arf_set_device_context(VALUE self); +static VALUE arf_delete_device_context(VALUE self); +static VALUE arf_get_device_type(VALUE self); +static VALUE arf_get_platform(VALUE self); + + static size_t* interpret_shape(VALUE arg, size_t* dim); #define DEF_ELEMENTWISE_RUBY_ACCESSOR(oper, name) \ @@ -71,8 +89,25 @@ void Init_arrayfire() { Blas = rb_define_class_under(ArrayFire, "BLAS", rb_cObject); rb_define_singleton_method(Blas, "matmul", (METHOD)arf_matmul, 2); + Cuda = rb_define_class_under(ArrayFire, "CUDA", rb_cObject); + rb_define_singleton_method(Cuda, "get_stream", (METHOD)arf_get_stream, 0); + rb_define_singleton_method(Cuda, "get_native_id", (METHOD)arf_get_native_id, 0); + rb_define_singleton_method(Cuda, "set_native_id", (METHOD)arf_set_native_id, 0); + Lapack = rb_define_class_under(ArrayFire, "LAPACK", rb_cObject); rb_define_singleton_method(Lapack, "cholesky", (METHOD)arf_cholesky, 1); + + OpenCL = rb_define_class_under(ArrayFire, "OpenCL", rb_cObject); + rb_define_singleton_method(OpenCL, "get_context", (METHOD)arf_get_context, 0); + rb_define_singleton_method(OpenCL, "get_queue", (METHOD)arf_get_queue, 0); + rb_define_singleton_method(OpenCL, "get_device_id", (METHOD)arf_get_device_id, 0); + rb_define_singleton_method(OpenCL, "set_device_id", (METHOD)arf_set_device_id, 0); + rb_define_singleton_method(OpenCL, "add_device_context", (METHOD)arf_add_device_context, 0); + rb_define_singleton_method(OpenCL, "set_device_context", (METHOD)arf_set_device_context, 0); + rb_define_singleton_method(OpenCL, "delete_device_context", (METHOD)arf_delete_device_context, 0); + rb_define_singleton_method(OpenCL, "get_device_type", (METHOD)arf_get_device_type, 0); + rb_define_singleton_method(OpenCL, "get_platform", (METHOD)arf_get_platform, 0); + } VALUE test1(VALUE self) { @@ -302,3 +337,51 @@ static VALUE arf_norm(VALUE self){ return DBL2NUM(norm); } + +static VALUE arf_get_stream(VALUE self){ + return Qnil; +} + +static VALUE arf_get_native_id(VALUE self){ + return Qnil; +} + +static VALUE arf_set_native_id(VALUE self){ + return Qnil; +} + +static VALUE arf_get_context(VALUE self){ + return Qnil; +} + +static VALUE arf_get_queue(VALUE self){ + return Qnil; +} + +static VALUE arf_get_device_id(VALUE self){ + return Qnil; +} + +static VALUE arf_set_device_id(VALUE self){ + return Qnil; +} + +static VALUE arf_add_device_context(VALUE self){ + return Qnil; +} + +static VALUE arf_set_device_context(VALUE self){ + return Qnil; +} + +static VALUE arf_delete_device_context(VALUE self){ + return Qnil; +} + +static VALUE arf_get_device_type(VALUE self){ + return Qnil; +} + +static VALUE arf_get_platform(VALUE self){ + return Qnil; +} From 0e7db4ea7703ec8313559bf87d963b0a5f685374 Mon Sep 17 00:00:00 2001 From: prasun Date: Thu, 1 Jun 2017 19:56:33 +0530 Subject: [PATCH 012/125] init methods for Data --- ext/mri/arrayfire.c | 150 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 19c22fd..73cb1f0 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -6,6 +6,7 @@ VALUE ArrayFire = Qnil; VALUE Af_Array = Qnil; VALUE Blas = Qnil; VALUE Cuda = Qnil; +VALUE Data = Qnil; VALUE Device = Qnil; VALUE Lapack = Qnil; VALUE OpenCL = Qnil; @@ -38,6 +39,31 @@ static VALUE arf_delete_device_context(VALUE self); static VALUE arf_get_device_type(VALUE self); static VALUE arf_get_platform(VALUE self); +static VALUE arf_constant(VALUE self); +static VALUE arf_constant_complex(VALUE self); +static VALUE arf_constant_long(VALUE self); +static VALUE arf_constant_ulong(VALUE self); +static VALUE arf_range(VALUE self); +static VALUE arf_iota(VALUE self); +static VALUE arf_identity(VALUE self); +static VALUE arf_diag_create(VALUE self); +static VALUE arf_diag_extract(VALUE self); +static VALUE arf_join(VALUE self); +static VALUE arf_join_many(VALUE self); +static VALUE arf_tile(VALUE self); +static VALUE arf_reorder(VALUE self); +static VALUE arf_shift(VALUE self); +static VALUE arf_moddims(VALUE self); +static VALUE arf_flat(VALUE self); +static VALUE arf_flip(VALUE self); +static VALUE arf_lower(VALUE self); +static VALUE arf_upper(VALUE self); +static VALUE arf_select(VALUE self); +static VALUE arf_select_scalar_r(VALUE self); +static VALUE arf_select_scalar_l(VALUE self); +static VALUE arf_replace(VALUE self); +static VALUE arf_replace_scalar(VALUE self); + static size_t* interpret_shape(VALUE arg, size_t* dim); @@ -108,6 +134,31 @@ void Init_arrayfire() { rb_define_singleton_method(OpenCL, "get_device_type", (METHOD)arf_get_device_type, 0); rb_define_singleton_method(OpenCL, "get_platform", (METHOD)arf_get_platform, 0); + Data = rb_define_class_under(ArrayFire, "Data", rb_cObject); + rb_define_singleton_method(Data, "constant", (METHOD)arf_constant, 0); + rb_define_singleton_method(Data, "constant_complex", (METHOD)arf_constant_complex, 0); + rb_define_singleton_method(Data, "constant_long", (METHOD)arf_constant_long, 0); + rb_define_singleton_method(Data, "constant_ulong", (METHOD)arf_constant_ulong, 0); + rb_define_singleton_method(Data, "range", (METHOD)arf_range, 0); + rb_define_singleton_method(Data, "iota", (METHOD)arf_iota, 0); + rb_define_singleton_method(Data, "identity", (METHOD)arf_identity, 0); + rb_define_singleton_method(Data, "diag_create", (METHOD)arf_diag_create, 0); + rb_define_singleton_method(Data, "diag_extract", (METHOD)arf_diag_extract, 0); + rb_define_singleton_method(Data, "join", (METHOD)arf_join, 0); + rb_define_singleton_method(Data, "join_many", (METHOD)arf_join_many, 0); + rb_define_singleton_method(Data, "tile", (METHOD)arf_tile, 0); + rb_define_singleton_method(Data, "reorder", (METHOD)arf_reorder, 0); + rb_define_singleton_method(Data, "shift", (METHOD)arf_shift, 0); + rb_define_singleton_method(Data, "moddims", (METHOD)arf_moddims, 0); + rb_define_singleton_method(Data, "flat", (METHOD)arf_flat, 0); + rb_define_singleton_method(Data, "flip", (METHOD)arf_flip, 0); + rb_define_singleton_method(Data, "lower", (METHOD)arf_lower, 0); + rb_define_singleton_method(Data, "upper", (METHOD)arf_upper, 0); + rb_define_singleton_method(Data, "select", (METHOD)arf_select, 0); + rb_define_singleton_method(Data, "select_scalar_r", (METHOD)arf_select_scalar_r, 0); + rb_define_singleton_method(Data, "select_scalar_l", (METHOD)arf_select_scalar_l, 0); + rb_define_singleton_method(Data, "replace", (METHOD)arf_replace, 0); + rb_define_singleton_method(Data, "replace_scalar", (METHOD)arf_replace_scalar, 0); } VALUE test1(VALUE self) { @@ -385,3 +436,102 @@ static VALUE arf_get_device_type(VALUE self){ static VALUE arf_get_platform(VALUE self){ return Qnil; } + +//Data + + +static VALUE arf_constant(VALUE self){ + return Qnil; +} + +static VALUE arf_constant_complex(VALUE self){ + return Qnil; +} + +static VALUE arf_constant_long(VALUE self){ + return Qnil; +} + +static VALUE arf_constant_ulong(VALUE self){ + return Qnil; +} + +static VALUE arf_range(VALUE self){ + return Qnil; +} + +static VALUE arf_iota(VALUE self){ + return Qnil; +} + +static VALUE arf_identity(VALUE self){ + return Qnil; +} + +static VALUE arf_diag_create(VALUE self){ + return Qnil; +} + +static VALUE arf_diag_extract(VALUE self){ + return Qnil; +} + +static VALUE arf_join(VALUE self){ + return Qnil; +} + +static VALUE arf_join_many(VALUE self){ + return Qnil; +} + +static VALUE arf_tile(VALUE self){ + return Qnil; +} + +static VALUE arf_reorder(VALUE self){ + return Qnil; +} + +static VALUE arf_shift(VALUE self){ + return Qnil; +} + +static VALUE arf_moddims(VALUE self){ + return Qnil; +} + +static VALUE arf_flat(VALUE self){ + return Qnil; +} + +static VALUE arf_flip(VALUE self){ + return Qnil; +} + +static VALUE arf_lower(VALUE self){ + return Qnil; +} + +static VALUE arf_upper(VALUE self){ + return Qnil; +} + +static VALUE arf_select(VALUE self){ + return Qnil; +} + +static VALUE arf_select_scalar_r(VALUE self){ + return Qnil; +} + +static VALUE arf_select_scalar_l(VALUE self){ + return Qnil; +} + +static VALUE arf_replace(VALUE self){ + return Qnil; +} + +static VALUE arf_replace_scalar(VALUE self){ + return Qnil; +} From 9bd6a6cba392f974707428737897d628b2337f16 Mon Sep 17 00:00:00 2001 From: prasun Date: Thu, 1 Jun 2017 21:09:23 +0530 Subject: [PATCH 013/125] init methods for Lapack --- ext/mri/arrayfire.c | 197 +++++++++++++++++++++++++++++--------------- 1 file changed, 132 insertions(+), 65 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 73cb1f0..9456a51 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -65,6 +65,22 @@ static VALUE arf_replace(VALUE self); static VALUE arf_replace_scalar(VALUE self); +static VALUE arf_svd(VALUE self); +static VALUE arf_svd_inplace(VALUE self); +static VALUE arf_lu(VALUE self); +static VALUE arf_lu_inplace(VALUE self); +static VALUE arf_qr(VALUE self); +static VALUE arf_qr_inplace(VALUE self); +static VALUE arf_cholesky(VALUE self, VALUE val); +static VALUE arf_cholesky_inplace(VALUE self); +static VALUE arf_solve(VALUE self); +static VALUE arf_solve_lu(VALUE self); +static VALUE arf_inverse(VALUE self); +static VALUE arf_rank(VALUE self); +static VALUE arf_det(VALUE self, VALUE val); +static VALUE arf_norm(VALUE self, VALUE val); +static VALUE arf_is_lapack_available(VALUE self); + static size_t* interpret_shape(VALUE arg, size_t* dim); #define DEF_ELEMENTWISE_RUBY_ACCESSOR(oper, name) \ @@ -87,10 +103,6 @@ static VALUE arf_eqeq(VALUE left_val, VALUE right_val); static VALUE arf_matmul(VALUE self, VALUE left_val, VALUE right_val); -static VALUE arf_cholesky(VALUE self, VALUE val); -static VALUE arf_det(VALUE self); -static VALUE arf_inverse(VALUE self); -static VALUE arf_norm(VALUE self); void Init_arrayfire() { ArrayFire = rb_define_module("ArrayFire"); @@ -105,9 +117,7 @@ void Init_arrayfire() { rb_define_method(Af_Array, "array2", (METHOD)array2, 0); rb_define_method(Af_Array, "+",(METHOD)arf_ew_add,1); rb_define_method(Af_Array, "==",(METHOD)arf_eqeq,1); - rb_define_method(Af_Array, "det",(METHOD)arf_det,0); rb_define_method(Af_Array, "inverse",(METHOD)arf_inverse,0); - rb_define_method(Af_Array, "norm",(METHOD)arf_norm,0); Device = rb_define_class_under(ArrayFire, "Device", rb_cObject); rb_define_method(Device, "getInfo", (METHOD)get_info, 0); @@ -120,9 +130,6 @@ void Init_arrayfire() { rb_define_singleton_method(Cuda, "get_native_id", (METHOD)arf_get_native_id, 0); rb_define_singleton_method(Cuda, "set_native_id", (METHOD)arf_set_native_id, 0); - Lapack = rb_define_class_under(ArrayFire, "LAPACK", rb_cObject); - rb_define_singleton_method(Lapack, "cholesky", (METHOD)arf_cholesky, 1); - OpenCL = rb_define_class_under(ArrayFire, "OpenCL", rb_cObject); rb_define_singleton_method(OpenCL, "get_context", (METHOD)arf_get_context, 0); rb_define_singleton_method(OpenCL, "get_queue", (METHOD)arf_get_queue, 0); @@ -159,6 +166,23 @@ void Init_arrayfire() { rb_define_singleton_method(Data, "select_scalar_l", (METHOD)arf_select_scalar_l, 0); rb_define_singleton_method(Data, "replace", (METHOD)arf_replace, 0); rb_define_singleton_method(Data, "replace_scalar", (METHOD)arf_replace_scalar, 0); + + Lapack = rb_define_class_under(ArrayFire, "LAPACK", rb_cObject); + rb_define_singleton_method(Lapack, "svd", (METHOD)arf_svd, 0); + rb_define_singleton_method(Lapack, "svd_inplace", (METHOD)arf_svd_inplace, 0); + rb_define_singleton_method(Lapack, "lu", (METHOD)arf_lu, 0); + rb_define_singleton_method(Lapack, "lu_inplace", (METHOD)arf_lu_inplace, 0); + rb_define_singleton_method(Lapack, "qr", (METHOD)arf_qr, 0); + rb_define_singleton_method(Lapack, "qr_inplace", (METHOD)arf_qr_inplace, 0); + rb_define_singleton_method(Lapack, "cholesky", (METHOD)arf_cholesky, 1); + rb_define_singleton_method(Lapack, "cholesky_inplace", (METHOD)arf_cholesky_inplace, 0); + rb_define_singleton_method(Lapack, "solve", (METHOD)arf_solve, 0); + rb_define_singleton_method(Lapack, "solve_lu", (METHOD)arf_solve_lu, 0); + rb_define_singleton_method(Lapack, "inverse", (METHOD)arf_inverse, 0); + rb_define_singleton_method(Lapack, "rank", (METHOD)arf_rank, 0); + rb_define_singleton_method(Lapack, "det", (METHOD)arf_det, 1); + rb_define_singleton_method(Lapack, "norm", (METHOD)arf_norm, 1); + rb_define_singleton_method(Lapack, "is_lapack_available", (METHOD)arf_is_lapack_available, 0); } VALUE test1(VALUE self) { @@ -331,63 +355,7 @@ static VALUE arf_matmul(VALUE self, VALUE left_val, VALUE right_val){ return Data_Wrap_Struct(CLASS_OF(left_val), NULL, arf_free, result); } -static VALUE arf_cholesky(VALUE self, VALUE val){ - - afstruct* matrix; - afstruct* result = ALLOC(afstruct); - - Data_Get_Struct(val, afstruct, matrix); - - - result->ndims = matrix->ndims; - result->dimension = matrix->dimension; - result->count = matrix->count; - arf::cholesky_(result, matrix); - - return Data_Wrap_Struct(CLASS_OF(val), NULL, arf_free, result); -} - -static VALUE arf_det(VALUE self){ - afstruct* matrix; - Data_Get_Struct(self, afstruct, matrix); - - af_array m; - dim_t dims[matrix->ndims] ; - for (size_t index = 0; index < matrix->ndims; ++index){ - dims[index] = (dim_t)matrix->dimension[index]; - } - af_create_array( &m, matrix->array, matrix->ndims, dims, f64 ); - double real, imaginary; - af_det(&real,&imaginary,m); - return DBL2NUM(real); -} - -static VALUE arf_inverse(VALUE self){ - - afstruct* matrix; - afstruct* result = ALLOC(afstruct); - - Data_Get_Struct(self, afstruct, matrix); - - result->ndims = matrix->ndims; - result->dimension = matrix->dimension; - result->count = matrix->count; - arf::inverse_(result, matrix); - - return Data_Wrap_Struct(CLASS_OF(self), NULL, arf_free, result); -} - -static VALUE arf_norm(VALUE self){ - afstruct* matrix; - - Data_Get_Struct(self, afstruct, matrix); - - double norm = arf::norm_(matrix); - - - return DBL2NUM(norm); -} static VALUE arf_get_stream(VALUE self){ return Qnil; @@ -535,3 +503,102 @@ static VALUE arf_replace(VALUE self){ static VALUE arf_replace_scalar(VALUE self){ return Qnil; } + +// Lapack + +static VALUE arf_svd(VALUE self){ + return Qnil; +} + +static VALUE arf_svd_inplace(VALUE self){ + return Qnil; +} + +static VALUE arf_lu(VALUE self){ + return Qnil; +} + +static VALUE arf_lu_inplace(VALUE self){ + return Qnil; +} + +static VALUE arf_qr(VALUE self){ + return Qnil; +} + +static VALUE arf_qr_inplace(VALUE self){ + return Qnil; +} + +static VALUE arf_cholesky(VALUE self, VALUE val){ + + afstruct* matrix; + afstruct* result = ALLOC(afstruct); + + Data_Get_Struct(val, afstruct, matrix); + + + result->ndims = matrix->ndims; + result->dimension = matrix->dimension; + result->count = matrix->count; + arf::cholesky_(result, matrix); + + return Data_Wrap_Struct(CLASS_OF(val), NULL, arf_free, result); +} + +static VALUE arf_cholesky_inplace(VALUE self){ + return Qnil; +} + +static VALUE arf_solve(VALUE self){ + return Qnil; +} + +static VALUE arf_solve_lu(VALUE self){ + return Qnil; +} + +static VALUE arf_inverse(VALUE self){ + + afstruct* matrix; + afstruct* result = ALLOC(afstruct); + + Data_Get_Struct(self, afstruct, matrix); + + result->ndims = matrix->ndims; + result->dimension = matrix->dimension; + result->count = matrix->count; + arf::inverse_(result, matrix); + + return Data_Wrap_Struct(CLASS_OF(self), NULL, arf_free, result); +} +static VALUE arf_rank(VALUE self){ + return Qnil; +} + +static VALUE arf_det(VALUE self, VALUE val){ + + afstruct* matrix; + Data_Get_Struct(val, afstruct, matrix); + + af_array m; + dim_t dims[matrix->ndims] ; + for (size_t index = 0; index < matrix->ndims; ++index){ + dims[index] = (dim_t)matrix->dimension[index]; + } + af_create_array( &m, matrix->array, matrix->ndims, dims, f64 ); + double real, imaginary; + af_det(&real,&imaginary,m); + return DBL2NUM(real); +} + +static VALUE arf_norm(VALUE self, VALUE val){ + afstruct* matrix + Data_Get_Struct(val, afstruct, matrix); + double norm = arf::norm_(matrix); + return DBL2NUM(norm); +} + +static VALUE arf_is_lapack_available(VALUE self){ + return Qnil; +} From 0b1a6d19c6b0af53fb246ad40ad683cfe57ca466 Mon Sep 17 00:00:00 2001 From: prasun Date: Thu, 1 Jun 2017 22:23:35 +0530 Subject: [PATCH 014/125] init methods for Blas --- ext/mri/arrayfire.c | 26 ++++++++++++++++++++++++-- spec/blas_spec.rb | 4 ++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 9456a51..75e5ee2 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -24,6 +24,10 @@ static VALUE array(VALUE self); static void array2(VALUE self); static VALUE get_info(VALUE self); +static VALUE arf_matmul(VALUE self, VALUE left_val, VALUE right_val); +static VALUE arf_dot(VALUE self, VALUE left_val, VALUE right_val); +static VALUE arf_transpose(VALUE self, VALUE left_val, VALUE right_val); +static VALUE arf_transpose_inplace(VALUE self, VALUE left_val, VALUE right_val); static VALUE arf_get_stream(VALUE self); static VALUE arf_get_native_id(VALUE self); @@ -93,6 +97,7 @@ DECL_ELEMENTWISE_RUBY_ACCESSOR(add) static VALUE elementwise_op(arf::ewop_t op, VALUE left_val, VALUE right_val); + /* * Macro defines an element-wise accessor function for some operation. * @@ -124,6 +129,9 @@ void Init_arrayfire() { Blas = rb_define_class_under(ArrayFire, "BLAS", rb_cObject); rb_define_singleton_method(Blas, "matmul", (METHOD)arf_matmul, 2); + rb_define_singleton_method(Blas, "dot", (METHOD)arf_dot, 2); + rb_define_singleton_method(Blas, "transpose", (METHOD)arf_transpose, 2); + rb_define_singleton_method(Blas, "transpose_inplace", (METHOD)arf_transpose_inplace, 2); Cuda = rb_define_class_under(ArrayFire, "CUDA", rb_cObject); rb_define_singleton_method(Cuda, "get_stream", (METHOD)arf_get_stream, 0); @@ -315,8 +323,6 @@ static VALUE arf_eqeq(VALUE left_val, VALUE right_val) { Data_Get_Struct(left_val, afstruct, left); Data_Get_Struct(right_val, afstruct, right); - printf("%d\n", left->ndims); - for(size_t i = 0; i < left->ndims; i++){ if(left->dimension[i]!= right->dimension[i]){ return Qfalse; @@ -332,6 +338,8 @@ static VALUE arf_eqeq(VALUE left_val, VALUE right_val) { return Qtrue; } +// BLAS + static VALUE arf_matmul(VALUE self, VALUE left_val, VALUE right_val){ afstruct* left; @@ -355,7 +363,19 @@ static VALUE arf_matmul(VALUE self, VALUE left_val, VALUE right_val){ return Data_Wrap_Struct(CLASS_OF(left_val), NULL, arf_free, result); } +static VALUE arf_dot(VALUE self, VALUE left_val, VALUE right_val){ + return Qnil; +} +static VALUE arf_transpose(VALUE self, VALUE left_val, VALUE right_val){ + return Qnil; +} + +static VALUE arf_transpose_inplace(VALUE self, VALUE left_val, VALUE right_val){ + return Qnil; +} + +// CUDA static VALUE arf_get_stream(VALUE self){ return Qnil; @@ -369,6 +389,8 @@ static VALUE arf_set_native_id(VALUE self){ return Qnil; } +// OpenCL + static VALUE arf_get_context(VALUE self){ return Qnil; } diff --git a/spec/blas_spec.rb b/spec/blas_spec.rb index f901127..67d3f49 100644 --- a/spec/blas_spec.rb +++ b/spec/blas_spec.rb @@ -4,8 +4,8 @@ context '#matmul' do let(:a) { ArrayFire::Af_Array.new 2, [2,2],[1,2,3,4] } let(:b) { ArrayFire::Af_Array.new 2, [2,2],[1,2,3,4] } - let(:c) { ArrayFire::Af_Array.new 2, [2,2],[7,10,15,22] } + let(:c) { ArrayFire::Af_Array.new 2, [2,2],[7.0,10.0,15.0,22.0] } subject {c} - it {expect(ArrayFire::BLAS.matmul(a,b)).array to eq c.array} + it {expect(ArrayFire::BLAS.matmul(a,b).array).to eq c.array} end end From b9c7e56b2b2aeb5ddb59fd9d1306fdecf8ee175d Mon Sep 17 00:00:00 2001 From: prasun Date: Thu, 1 Jun 2017 22:38:09 +0530 Subject: [PATCH 015/125] init methods for Backend --- ext/mri/arrayfire.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 75e5ee2..1ff5565 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -4,6 +4,7 @@ VALUE ArrayFire = Qnil; VALUE Af_Array = Qnil; +VALUE Backend = Qnil; VALUE Blas = Qnil; VALUE Cuda = Qnil; VALUE Data = Qnil; @@ -24,6 +25,12 @@ static VALUE array(VALUE self); static void array2(VALUE self); static VALUE get_info(VALUE self); +static VALUE arf_get_backend_count(VALUE self); +static VALUE arf_get_available_backends(VALUE self); +static VALUE arf_get_backend_id(VALUE self); +static VALUE arf_get_active_backend(VALUE self); +static VALUE arf_get_backend_device_id(VALUE self); + static VALUE arf_matmul(VALUE self, VALUE left_val, VALUE right_val); static VALUE arf_dot(VALUE self, VALUE left_val, VALUE right_val); static VALUE arf_transpose(VALUE self, VALUE left_val, VALUE right_val); @@ -124,6 +131,13 @@ void Init_arrayfire() { rb_define_method(Af_Array, "==",(METHOD)arf_eqeq,1); rb_define_method(Af_Array, "inverse",(METHOD)arf_inverse,0); + Backend = rb_define_class_under(ArrayFire, "Backend", rb_cObject); + rb_define_method(Backend, "get_backend_count", (METHOD)arf_get_backend_count, 0); + rb_define_method(Backend, "get_available_backends", (METHOD)arf_get_available_backends, 0); + rb_define_method(Backend, "get_backend_id", (METHOD)arf_get_backend_id, 0); + rb_define_method(Backend, "get_active_backend", (METHOD)arf_get_active_backend, 0); + rb_define_method(Backend, "get_device_id", (METHOD)arf_get_backend_device_id, 0); + Device = rb_define_class_under(ArrayFire, "Device", rb_cObject); rb_define_method(Device, "getInfo", (METHOD)get_info, 0); @@ -338,6 +352,28 @@ static VALUE arf_eqeq(VALUE left_val, VALUE right_val) { return Qtrue; } +// Backend + +static VALUE arf_get_backend_count(VALUE self){ + return Qnil; +} + +static VALUE arf_get_available_backends(VALUE self){ + return Qnil; +} + +static VALUE arf_get_backend_id(VALUE self){ + return Qnil; +} + +static VALUE arf_get_active_backend(VALUE self){ + return Qnil; +} + +static VALUE arf_get_backend_device_id(VALUE self){ + return Qnil; +} + // BLAS static VALUE arf_matmul(VALUE self, VALUE left_val, VALUE right_val){ From 66173b1e82792dfad03e97a01c1a30eb22c9bebc Mon Sep 17 00:00:00 2001 From: prasun Date: Fri, 2 Jun 2017 00:19:42 +0530 Subject: [PATCH 016/125] init Device functions --- ext/mri/arrayfire.c | 177 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 167 insertions(+), 10 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 1ff5565..7fc2d0b 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -23,7 +23,6 @@ static VALUE ndims(VALUE self); static VALUE dimension(VALUE self); static VALUE array(VALUE self); static void array2(VALUE self); -static VALUE get_info(VALUE self); static VALUE arf_get_backend_count(VALUE self); static VALUE arf_get_available_backends(VALUE self); @@ -40,6 +39,34 @@ static VALUE arf_get_stream(VALUE self); static VALUE arf_get_native_id(VALUE self); static VALUE arf_set_native_id(VALUE self); +static VALUE arf_info(VALUE self); +static VALUE arf_init2(VALUE self); +static VALUE arf_info_string(VALUE self); +static VALUE arf_device_info(VALUE self); +static VALUE arf_get_device_count(VALUE self); +static VALUE arf_get_dbl_support(VALUE self); +static VALUE arf_set_device(VALUE self); +static VALUE arf_get_device(VALUE self); +static VALUE arf_sync(VALUE self); +static VALUE arf_alloc_device(VALUE self); +static VALUE arf_free_device(VALUE self); +static VALUE arf_alloc_pinned(VALUE self); +static VALUE arf_free_pinned(VALUE self); +static VALUE arf_alloc_host(VALUE self); +static VALUE arf_free_host(VALUE self); +static VALUE arf_device_array(VALUE self); +static VALUE arf_device_mem_info(VALUE self); +static VALUE arf_print_mem_info(VALUE self); +static VALUE arf_device_gc(VALUE self); +static VALUE arf_set_mem_step_size(VALUE self); +static VALUE arf_get_mem_step_size(VALUE self); +static VALUE arf_lock_device_ptr(VALUE self); +static VALUE arf_unlock_device_ptr(VALUE self); +static VALUE arf_lock_array(VALUE self); +static VALUE arf_unlock_array(VALUE self); +static VALUE arf_is_locked_array(VALUE self); +static VALUE arf_get_device_ptr(VALUE self); + static VALUE arf_get_context(VALUE self); static VALUE arf_get_queue(VALUE self); static VALUE arf_get_device_id(VALUE self); @@ -139,7 +166,33 @@ void Init_arrayfire() { rb_define_method(Backend, "get_device_id", (METHOD)arf_get_backend_device_id, 0); Device = rb_define_class_under(ArrayFire, "Device", rb_cObject); - rb_define_method(Device, "getInfo", (METHOD)get_info, 0); + rb_define_method(Device, "info", (METHOD)arf_info, 0); + rb_define_method(Device, "init", (METHOD)arf_init, 0); + rb_define_method(Device, "info_string", (METHOD)arf_info_string, 0); + rb_define_method(Device, "device_info", (METHOD)arf_device_info, 0); + rb_define_method(Device, "get_device_count", (METHOD)arf_get_device_count, 0); + rb_define_method(Device, "get_dbl_support", (METHOD)arf_get_dbl_support, 0); + rb_define_method(Device, "set_device", (METHOD)arf_set_device, 0); + rb_define_method(Device, "get_device", (METHOD)arf_get_device, 0); + rb_define_method(Device, "sync", (METHOD)arf_sync, 0); + rb_define_method(Device, "alloc_device", (METHOD)arf_alloc_device, 0); + rb_define_method(Device, "free_device", (METHOD)arf_free_device, 0); + rb_define_method(Device, "alloc_pinned", (METHOD)arf_alloc_pinned, 0); + rb_define_method(Device, "free_pinned", (METHOD)arf_free_pinned, 0); + rb_define_method(Device, "alloc_host", (METHOD)arf_alloc_host, 0); + rb_define_method(Device, "free_host", (METHOD)arf_free_host, 0); + rb_define_method(Device, "device_array", (METHOD)arf_device_array, 0); + rb_define_method(Device, "device_mem_info", (METHOD)arf_device_mem_info, 0); + rb_define_method(Device, "print_mem_info", (METHOD)arf_print_mem_info, 0); + rb_define_method(Device, "device_gc", (METHOD)arf_device_gc, 0); + rb_define_method(Device, "set_mem_step_size", (METHOD)arf_set_mem_step_size, 0); + rb_define_method(Device, "get_mem_step_size", (METHOD)arf_get_mem_step_size, 0); + rb_define_method(Device, "lock_device_ptr", (METHOD)arf_lock_device_ptr, 0); + rb_define_method(Device, "unlock_device_ptr", (METHOD)arf_unlock_device_ptr, 0); + rb_define_method(Device, "lock_array", (METHOD)arf_lock_array, 0); + rb_define_method(Device, "unlock_array", (METHOD)arf_unlock_array, 0); + rb_define_method(Device, "is_locked_array", (METHOD)arf_is_locked_array, 0); + rb_define_method(Device, "get_device_ptr", (METHOD)arf_get_device_ptr, 0); Blas = rb_define_class_under(ArrayFire, "BLAS", rb_cObject); rb_define_singleton_method(Blas, "matmul", (METHOD)arf_matmul, 2); @@ -297,14 +350,6 @@ static void array2(VALUE self){ Data_Get_Struct(self, afstruct, af); } -static VALUE get_info(VALUE self) -{ - VALUE x; - af_info(); - return x; -} - - DEF_ELEMENTWISE_RUBY_ACCESSOR(ADD, add) static VALUE elementwise_op(arf::ewop_t op, VALUE left_val, VALUE right_val) { @@ -425,8 +470,120 @@ static VALUE arf_set_native_id(VALUE self){ return Qnil; } +// Device + +static VALUE arf_info(VALUE self){ + af_info(); + return Qnil; +} + +static VALUE arf_init2(VALUE self){ + return Qnil; +} + +static VALUE arf_info_string(VALUE self){ + return Qnil; +} + +static VALUE arf_device_info(VALUE self){ + return Qnil; +} + +static VALUE arf_get_device_count(VALUE self){ + return Qnil; +} + +static VALUE arf_get_dbl_support(VALUE self){ + return Qnil; +} + +static VALUE arf_set_device(VALUE self){ + return Qnil; +} + +static VALUE arf_get_device(VALUE self){ + return Qnil; +} + +static VALUE arf_sync(VALUE self){ + return Qnil; +} + +static VALUE arf_alloc_device(VALUE self){ + return Qnil; +} + +static VALUE arf_free_device(VALUE self){ + return Qnil; +} + +static VALUE arf_alloc_pinned(VALUE self){ + return Qnil; +} + +static VALUE arf_free_pinned(VALUE self){ + return Qnil; +} + +static VALUE arf_alloc_host(VALUE self){ + return Qnil; +} + +static VALUE arf_free_host(VALUE self){ + return Qnil; +} + +static VALUE arf_device_array(VALUE self){ + return Qnil; +} + +static VALUE arf_device_mem_info(VALUE self){ + return Qnil; +} + +static VALUE arf_print_mem_info(VALUE self){ + return Qnil; +} + +static VALUE arf_device_gc(VALUE self){ + return Qnil; +} + +static VALUE arf_set_mem_step_size(VALUE self){ + return Qnil; +} + +static VALUE arf_get_mem_step_size(VALUE self){ + return Qnil; +} + +static VALUE arf_lock_device_ptr(VALUE self){ + return Qnil; +} + +static VALUE arf_unlock_device_ptr(VALUE self){ + return Qnil; +} + +static VALUE arf_lock_array(VALUE self){ + return Qnil; +} + +static VALUE arf_unlock_array(VALUE self){ + return Qnil; +} + +static VALUE arf_is_locked_array(VALUE self){ + return Qnil; +} + +static VALUE arf_get_device_ptr(VALUE self){ + return Qnil; +} + // OpenCL + static VALUE arf_get_context(VALUE self){ return Qnil; } From 026966fa2a0ce193c7ef768317e34e2d5c8e6554 Mon Sep 17 00:00:00 2001 From: prasun Date: Fri, 2 Jun 2017 00:26:56 +0530 Subject: [PATCH 017/125] init Algorithm functions --- ext/mri/arrayfire.c | 177 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 177 insertions(+) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 7fc2d0b..c223490 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -4,6 +4,7 @@ VALUE ArrayFire = Qnil; VALUE Af_Array = Qnil; +VALUE Algorithm = Qnil; VALUE Backend = Qnil; VALUE Blas = Qnil; VALUE Cuda = Qnil; @@ -24,6 +25,41 @@ static VALUE dimension(VALUE self); static VALUE array(VALUE self); static void array2(VALUE self); +static VALUE arf_sum(VALUE self); +static VALUE arf_sum_nan(VALUE self); +static VALUE arf_product(VALUE self); +static VALUE arf_product_nan(VALUE self); +static VALUE arf_min(VALUE self); +static VALUE arf_max(VALUE self); +static VALUE arf_all_true(VALUE self); +static VALUE arf_any_true(VALUE self); +static VALUE arf_count(VALUE self); +static VALUE arf_sum_all(VALUE self); +static VALUE arf_sum_nan_all(VALUE self); +static VALUE arf_product_all(VALUE self); +static VALUE arf_product_nan_all(VALUE self); +static VALUE arf_min_all(VALUE self); +static VALUE arf_max_all(VALUE self); +static VALUE arf_all_true_all(VALUE self); +static VALUE arf_any_true_all(VALUE self); +static VALUE arf_count_all(VALUE self); +static VALUE arf_imin(VALUE self); +static VALUE arf_imax(VALUE self); +static VALUE arf_imin_all(VALUE self); +static VALUE arf_imax_all(VALUE self); +static VALUE arf_accum(VALUE self); +static VALUE arf_scan(VALUE self); +static VALUE arf_scan_by_key(VALUE self); +static VALUE arf_where(VALUE self); +static VALUE arf_diff1(VALUE self); +static VALUE arf_diff2(VALUE self); +static VALUE arf_sort(VALUE self); +static VALUE arf_sort_index(VALUE self); +static VALUE arf_sort_by_key(VALUE self); +static VALUE arf_set_unique(VALUE self); +static VALUE arf_set_union(VALUE self); +static VALUE arf_set_intersect(VALUE self); + static VALUE arf_get_backend_count(VALUE self); static VALUE arf_get_available_backends(VALUE self); static VALUE arf_get_backend_id(VALUE self); @@ -158,6 +194,42 @@ void Init_arrayfire() { rb_define_method(Af_Array, "==",(METHOD)arf_eqeq,1); rb_define_method(Af_Array, "inverse",(METHOD)arf_inverse,0); + Algorithm = rb_define_class_under(ArrayFire, "Algorithm", rb_cObject); + rb_define_method(Algorithm, "sum", (METHOD)arf_sum, 0); + rb_define_method(Algorithm, "sum_nan", (METHOD)arf_sum_nan, 0); + rb_define_method(Algorithm, "product", (METHOD)arf_product, 0); + rb_define_method(Algorithm, "product_nan", (METHOD)arf_product_nan, 0); + rb_define_method(Algorithm, "min", (METHOD)arf_min, 0); + rb_define_method(Algorithm, "max", (METHOD)arf_max, 0); + rb_define_method(Algorithm, "all_true", (METHOD)arf_all_true, 0); + rb_define_method(Algorithm, "any_true", (METHOD)arf_any_true, 0); + rb_define_method(Algorithm, "count", (METHOD)arf_count, 0); + rb_define_method(Algorithm, "sum_all", (METHOD)arf_sum_all, 0); + rb_define_method(Algorithm, "sum_nan_all", (METHOD)arf_sum_nan_all, 0); + rb_define_method(Algorithm, "product_all", (METHOD)arf_product_all, 0); + rb_define_method(Algorithm, "product_nan_all", (METHOD)arf_product_nan_all, 0); + rb_define_method(Algorithm, "min_all", (METHOD)arf_min_all, 0); + rb_define_method(Algorithm, "max_all", (METHOD)arf_max_all, 0); + rb_define_method(Algorithm, "all_true_all", (METHOD)arf_all_true_all, 0); + rb_define_method(Algorithm, "any_true_all", (METHOD)arf_any_true_all, 0); + rb_define_method(Algorithm, "count_all", (METHOD)arf_count_all, 0); + rb_define_method(Algorithm, "imin", (METHOD)arf_imin, 0); + rb_define_method(Algorithm, "imax", (METHOD)arf_imax, 0); + rb_define_method(Algorithm, "imin_all", (METHOD)arf_imin_all, 0); + rb_define_method(Algorithm, "imax_all", (METHOD)arf_imax_all, 0); + rb_define_method(Algorithm, "accum", (METHOD)arf_accum, 0); + rb_define_method(Algorithm, "scan", (METHOD)arf_scan, 0); + rb_define_method(Algorithm, "scan_by_key", (METHOD)arf_scan_by_key, 0); + rb_define_method(Algorithm, "where", (METHOD)arf_where, 0); + rb_define_method(Algorithm, "diff1", (METHOD)arf_diff1, 0); + rb_define_method(Algorithm, "diff2", (METHOD)arf_diff2, 0); + rb_define_method(Algorithm, "sort", (METHOD)arf_sort, 0); + rb_define_method(Algorithm, "sort_index", (METHOD)arf_sort_index, 0); + rb_define_method(Algorithm, "sort_by_key", (METHOD)arf_sort_by_key, 0); + rb_define_method(Algorithm, "set_unique", (METHOD)arf_set_unique, 0); + rb_define_method(Algorithm, "set_union", (METHOD)arf_set_union, 0); + rb_define_method(Algorithm, "set_intersect", (METHOD)arf_set_intersect, 0); + Backend = rb_define_class_under(ArrayFire, "Backend", rb_cObject); rb_define_method(Backend, "get_backend_count", (METHOD)arf_get_backend_count, 0); rb_define_method(Backend, "get_available_backends", (METHOD)arf_get_available_backends, 0); @@ -397,6 +469,111 @@ static VALUE arf_eqeq(VALUE left_val, VALUE right_val) { return Qtrue; } +// Algorithm + +static VALUE arf_sum(VALUE self){ + return Qnil; +} +static VALUE arf_sum_nan(VALUE self){ + return Qnil; +} +static VALUE arf_product(VALUE self){ + return Qnil; +} +static VALUE arf_product_nan(VALUE self){ + return Qnil; +} +static VALUE arf_min(VALUE self){ + return Qnil; +} +static VALUE arf_max(VALUE self){ + return Qnil; +} +static VALUE arf_all_true(VALUE self){ + return Qnil; +} +static VALUE arf_any_true(VALUE self){ + return Qnil; +} +static VALUE arf_count(VALUE self){ + return Qnil; +} +static VALUE arf_sum_all(VALUE self){ + return Qnil; +} +static VALUE arf_sum_nan_all(VALUE self){ + return Qnil; +} +static VALUE arf_product_all(VALUE self){ + return Qnil; +} +static VALUE arf_product_nan_all(VALUE self){ + return Qnil; +} +static VALUE arf_min_all(VALUE self){ + return Qnil; +} +static VALUE arf_max_all(VALUE self){ + return Qnil; +} +static VALUE arf_all_true_all(VALUE self){ + return Qnil; +} +static VALUE arf_any_true_all(VALUE self){ + return Qnil; +} +static VALUE arf_count_all(VALUE self){ + return Qnil; +} +static VALUE arf_imin(VALUE self){ + return Qnil; +} +static VALUE arf_imax(VALUE self){ + return Qnil; +} +static VALUE arf_imin_all(VALUE self){ + return Qnil; +} +static VALUE arf_imax_all(VALUE self){ + return Qnil; +} +static VALUE arf_accum(VALUE self){ + return Qnil; +} +static VALUE arf_scan(VALUE self){ + return Qnil; +} +static VALUE arf_scan_by_key(VALUE self){ + return Qnil; +} +static VALUE arf_where(VALUE self){ + return Qnil; +} +static VALUE arf_diff1(VALUE self){ + return Qnil; +} +static VALUE arf_diff2(VALUE self){ + return Qnil; +} +static VALUE arf_sort(VALUE self){ + return Qnil; +} +static VALUE arf_sort_index(VALUE self){ + return Qnil; +} +static VALUE arf_sort_by_key(VALUE self){ + return Qnil; +} +static VALUE arf_set_unique(VALUE self){ + return Qnil; +} +static VALUE arf_set_union(VALUE self){ + return Qnil; +} +static VALUE arf_set_intersect(VALUE self){ + return Qnil; +} + // Backend static VALUE arf_get_backend_count(VALUE self){ From c59de6a8c8606bcbf2136e04f3f671a26b891aa0 Mon Sep 17 00:00:00 2001 From: prasun Date: Fri, 2 Jun 2017 00:38:40 +0530 Subject: [PATCH 018/125] init Random functions --- ext/mri/arrayfire.c | 83 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 82 insertions(+), 1 deletion(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index c223490..604ee23 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -12,7 +12,7 @@ VALUE Data = Qnil; VALUE Device = Qnil; VALUE Lapack = Qnil; VALUE OpenCL = Qnil; - +VALUE Random = Qnil; // prototypes void Init_arrayfire(); @@ -155,6 +155,22 @@ static VALUE arf_det(VALUE self, VALUE val); static VALUE arf_norm(VALUE self, VALUE val); static VALUE arf_is_lapack_available(VALUE self); +static VALUE arf_create_random_engine(VALUE self); +static VALUE arf_retain_random_engine(VALUE self); +static VALUE arf_random_engine_set_type(VALUE self); +static VALUE arf_random_engine_get_type(VALUE self); +static VALUE arf_random_uniform(VALUE self); +static VALUE arf_random_normal(VALUE self); +static VALUE arf_random_engine_set_seed(VALUE self); +static VALUE arf_get_default_random_engine(VALUE self); +static VALUE arf_set_default_random_engine_type(VALUE self); +static VALUE arf_random_engine_get_seed(VALUE self); +static VALUE arf_release_random_engine(VALUE self); +static VALUE arf_randu(VALUE self); +static VALUE arf_randn(VALUE self); +static VALUE arf_set_seed(VALUE self); +static VALUE arf_get_seed(VALUE self); + static size_t* interpret_shape(VALUE arg, size_t* dim); #define DEF_ELEMENTWISE_RUBY_ACCESSOR(oper, name) \ @@ -330,6 +346,23 @@ void Init_arrayfire() { rb_define_singleton_method(Lapack, "det", (METHOD)arf_det, 1); rb_define_singleton_method(Lapack, "norm", (METHOD)arf_norm, 1); rb_define_singleton_method(Lapack, "is_lapack_available", (METHOD)arf_is_lapack_available, 0); + + Random = rb_define_class_under(ArrayFire, "Random", rb_cObject); + rb_define_method(Random, "create_random_engine", (METHOD)arf_create_random_engine, 0); + rb_define_method(Random, "retain_random_engine", (METHOD)arf_retain_random_engine, 0); + rb_define_method(Random, "random_engine_set_type", (METHOD)arf_random_engine_set_type, 0); + rb_define_method(Random, "random_engine_get_type", (METHOD)arf_random_engine_get_type, 0); + rb_define_method(Random, "random_uniform", (METHOD)arf_random_uniform, 0); + rb_define_method(Random, "random_normal", (METHOD)arf_random_normal, 0); + rb_define_method(Random, "random_engine_set_seed", (METHOD)arf_random_engine_set_seed, 0); + rb_define_method(Random, "get_default_random_engine", (METHOD)arf_get_default_random_engine, 0); + rb_define_method(Random, "set_default_random_engine_type", (METHOD)arf_set_default_random_engine_type, 0); + rb_define_method(Random, "random_engine_get_seed", (METHOD)arf_random_engine_get_seed, 0); + rb_define_method(Random, "release_random_engine", (METHOD)arf_release_random_engine, 0); + rb_define_method(Random, "randu", (METHOD)arf_randu, 0); + rb_define_method(Random, "randn", (METHOD)arf_randn, 0); + rb_define_method(Random, "set_seed", (METHOD)arf_set_seed, 0); + rb_define_method(Random, "get_seed", (METHOD)arf_get_seed, 0); } VALUE test1(VALUE self) { @@ -994,3 +1027,51 @@ static VALUE arf_norm(VALUE self, VALUE val){ static VALUE arf_is_lapack_available(VALUE self){ return Qnil; } + +// Random + +static VALUE arf_create_random_engine(VALUE self){ + return Qnil; +} +static VALUE arf_retain_random_engine(VALUE self){ + return Qnil; +} +static VALUE arf_random_engine_set_type(VALUE self){ + return Qnil; +} +static VALUE arf_random_engine_get_type(VALUE self){ + return Qnil; +} +static VALUE arf_random_uniform(VALUE self){ + return Qnil; +} +static VALUE arf_random_normal(VALUE self){ + return Qnil; +} +static VALUE arf_random_engine_set_seed(VALUE self){ + return Qnil; +} +static VALUE arf_get_default_random_engine(VALUE self){ + return Qnil; +} +static VALUE arf_set_default_random_engine_type(VALUE self){ + return Qnil; +} +static VALUE arf_random_engine_get_seed(VALUE self){ + return Qnil; +} +static VALUE arf_release_random_engine(VALUE self){ + return Qnil; +} +static VALUE arf_randu(VALUE self){ + return Qnil; +} +static VALUE arf_randn(VALUE self){ + return Qnil; +} +static VALUE arf_set_seed(VALUE self){ + return Qnil; +} +static VALUE arf_get_seed(VALUE self){ + return Qnil; +} From 73cc062409743d8ce0bfada7cb91d10ef8e2d74c Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Thu, 22 Jun 2017 15:59:46 +0530 Subject: [PATCH 019/125] add paths to gemspec and modify gem name --- arrayfire.gemspec | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/arrayfire.gemspec b/arrayfire.gemspec index 2f0ec5f..6f38d57 100644 --- a/arrayfire.gemspec +++ b/arrayfire.gemspec @@ -1,5 +1,5 @@ Gem::Specification.new do |gem| - gem.name = 'ArrayFire' + gem.name = 'arrayfire' gem.version = '0.0.0' gem.date = '2016-09-29' gem.summary = 'Ruby wrapper for ArrayFire.' @@ -9,6 +9,10 @@ Gem::Specification.new do |gem| gem.files = ['lib/arrayfire.rb'] gem.homepage = 'http://rubygems.org/gems/arrayfire' gem.license = 'BSD-3-Clause' + gem.extensions = ['ext/mri/extconf.rb'] + gem.files = `git ls-files -- ext/mri`.split("\n") + gem.files += `git ls-files -- lib`.split("\n") + gem.require_paths = ["lib"] gem.add_development_dependency 'rubocop' gem.add_development_dependency 'bundler', '~>1.6' gem.add_development_dependency 'json' From 9edf6b9e1eff6001d538319c584eb70d569f1e34 Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Thu, 22 Jun 2017 16:01:49 +0530 Subject: [PATCH 020/125] convert info method to singleton method --- ext/mri/arrayfire.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 604ee23..a239124 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -254,7 +254,7 @@ void Init_arrayfire() { rb_define_method(Backend, "get_device_id", (METHOD)arf_get_backend_device_id, 0); Device = rb_define_class_under(ArrayFire, "Device", rb_cObject); - rb_define_method(Device, "info", (METHOD)arf_info, 0); + rb_define_singleton_method(Device, "info", (METHOD)arf_info, 0); rb_define_method(Device, "init", (METHOD)arf_init, 0); rb_define_method(Device, "info_string", (METHOD)arf_info_string, 0); rb_define_method(Device, "device_info", (METHOD)arf_device_info, 0); From d6c54cc6ff8070ee4aca32b5e7a67122edcad91d Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Thu, 22 Jun 2017 16:03:05 +0530 Subject: [PATCH 021/125] concentrating on opencl --- ext/mri/extconf.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/mri/extconf.rb b/ext/mri/extconf.rb index 5f2937b..6c980f3 100644 --- a/ext/mri/extconf.rb +++ b/ext/mri/extconf.rb @@ -23,7 +23,7 @@ HEADER_DIRS = [INCLUDEDIR, '/usr/local/include/', '/usr/local/include/af/'] -have_library('afcuda') +have_library('afopencl') have_library('cusolver') have_library('cudart') have_library('cufft') From fb29b2a9ce221c1f62999afacef269823520dd9a Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Wed, 28 Jun 2017 23:39:21 +0530 Subject: [PATCH 022/125] cleanup: remove ndims, array, array2, dimension --- ext/mri/arrayfire.c | 60 --------------------------------------------- 1 file changed, 60 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index a239124..1aee443 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -16,14 +16,9 @@ VALUE Random = Qnil; // prototypes void Init_arrayfire(); -static VALUE test1(VALUE self); static VALUE arf_init(int argc, VALUE* argv, VALUE self); static VALUE arf_alloc(VALUE klass); static void arf_free(afstruct* af); -static VALUE ndims(VALUE self); -static VALUE dimension(VALUE self); -static VALUE array(VALUE self); -static void array2(VALUE self); static VALUE arf_sum(VALUE self); static VALUE arf_sum_nan(VALUE self); @@ -202,10 +197,6 @@ void Init_arrayfire() { Af_Array = rb_define_class_under(ArrayFire, "Af_Array", rb_cObject); rb_define_alloc_func(Af_Array, arf_alloc); rb_define_method(Af_Array, "initialize", (METHOD)arf_init, -1); - rb_define_method(Af_Array, "ndims", (METHOD)ndims, 0); - rb_define_method(Af_Array, "dimension", (METHOD)dimension, 0); - rb_define_method(Af_Array, "array", (METHOD)array, 0); - rb_define_method(Af_Array, "array2", (METHOD)array2, 0); rb_define_method(Af_Array, "+",(METHOD)arf_ew_add,1); rb_define_method(Af_Array, "==",(METHOD)arf_eqeq,1); rb_define_method(Af_Array, "inverse",(METHOD)arf_inverse,0); @@ -365,12 +356,6 @@ void Init_arrayfire() { rb_define_method(Random, "get_seed", (METHOD)arf_get_seed, 0); } -VALUE test1(VALUE self) { - VALUE x; - x = rb_str_new_cstr("Hello, world!"); - return x; -} - VALUE arf_init(int argc, VALUE* argv, VALUE self) { afstruct* afarray; @@ -410,51 +395,6 @@ static void arf_free(afstruct* af) free(af); } -static VALUE ndims(VALUE self) -{ - afstruct * af; - - Data_Get_Struct(self, afstruct, af); - - return INT2NUM(af->ndims); -} - -static VALUE dimension(VALUE self) -{ - afstruct * af; - - Data_Get_Struct(self, afstruct, af); - - VALUE* dimension = ALLOC_N(VALUE, af->ndims); - - for (size_t index = 0; index < af->ndims; ++index){ - dimension[index] = INT2FIX(af->dimension[index]); - } - - return rb_ary_new4(af->ndims, dimension); -} - -static VALUE array(VALUE self) -{ - afstruct * af; - - Data_Get_Struct(self, afstruct, af); - - VALUE* array = ALLOC_N(VALUE, af->count); - - for (size_t index = 0; index < af->count; ++index){ - array[index] = DBL2NUM(af->array[index]); - } - - return rb_ary_new4(af->count, array); -} - -static void array2(VALUE self){ - afstruct * af; - - Data_Get_Struct(self, afstruct, af); -} - DEF_ELEMENTWISE_RUBY_ACCESSOR(ADD, add) static VALUE elementwise_op(arf::ewop_t op, VALUE left_val, VALUE right_val) { From 359472dc6dd60faa3a42bdf468e17afddba13544 Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Wed, 28 Jun 2017 23:41:45 +0530 Subject: [PATCH 023/125] use af_array to store array pointer --- ext/mri/ruby_arrayfire.h | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/ext/mri/ruby_arrayfire.h b/ext/mri/ruby_arrayfire.h index f6305f5..77c94d8 100644 --- a/ext/mri/ruby_arrayfire.h +++ b/ext/mri/ruby_arrayfire.h @@ -6,11 +6,7 @@ typedef struct AF_STRUCT { - // af_array arr; - size_t ndims; - size_t count; - size_t* dimension; - double* array; + af_array carray; }afstruct; #ifndef HAVE_RB_ARRAY_CONST_PTR From aed9306cddeb531f199e91505c02a503c7983150 Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Wed, 28 Jun 2017 23:43:57 +0530 Subject: [PATCH 024/125] modify initializer function to store af_array pointer --- ext/mri/arrayfire.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 1aee443..40c642c 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -360,23 +360,22 @@ VALUE arf_init(int argc, VALUE* argv, VALUE self) { afstruct* afarray; Data_Get_Struct(self, afstruct, afarray); - afarray->ndims = FIX2LONG(argv[0]); - afarray->dimension = ALLOC_N(VALUE, argv[0]); - size_t count = 1; - for (size_t index = 0; index < FIX2LONG(argv[0]); index++) { - afarray->dimension[index] = FIX2LONG(RARRAY_AREF(argv[1], index)); - count *= afarray->dimension[index]; + dim_t ndims = (dim_t)FIX2LONG(argv[0]); + dim_t* dimensions = (dim_t*)malloc(ndims * sizeof(dim_t)); + dim_t count = 1; + for (size_t index = 0; index < ndims; index++) { + dimensions[index] = (dim_t)FIX2LONG(RARRAY_AREF(argv[1], index)); + count *= dimensions[index]; } - afarray->count = count; - afarray->array = ALLOC_N(double, count); + float* host_array = (float*)malloc(count * sizeof(float)); for (size_t index = 0; index < count; index++) { - afarray->array[index] = NUM2DBL(RARRAY_AREF(argv[2], index)); + host_array[index] = (float)NUM2DBL(RARRAY_AREF(argv[2], index)); } - dim_t dims[afarray->ndims] ; - for (size_t index = 0; index < afarray->ndims; ++index){ - dims[index] = (dim_t)afarray->dimension[index]; - } + af_create_array(&afarray->carray, host_array, 2, dimensions, f32); + + af_print_array(afarray->carray); + return self; } From 084fbb7b1e9664804dfd364868870f4c1b8167ad Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Wed, 28 Jun 2017 23:54:27 +0530 Subject: [PATCH 025/125] move around files --- ext/mri/{arith.cpp => cmodules/arith.c} | 0 ext/mri/{blas.cpp => cmodules/blas.c} | 0 ext/mri/{lapack.cpp => cmodules/lapack.c} | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename ext/mri/{arith.cpp => cmodules/arith.c} (100%) rename ext/mri/{blas.cpp => cmodules/blas.c} (100%) rename ext/mri/{lapack.cpp => cmodules/lapack.c} (100%) diff --git a/ext/mri/arith.cpp b/ext/mri/cmodules/arith.c similarity index 100% rename from ext/mri/arith.cpp rename to ext/mri/cmodules/arith.c diff --git a/ext/mri/blas.cpp b/ext/mri/cmodules/blas.c similarity index 100% rename from ext/mri/blas.cpp rename to ext/mri/cmodules/blas.c diff --git a/ext/mri/lapack.cpp b/ext/mri/cmodules/lapack.c similarity index 100% rename from ext/mri/lapack.cpp rename to ext/mri/cmodules/lapack.c From babc63c659d8a3f86cdb5844e4d974a9c0847f13 Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Wed, 28 Jun 2017 23:58:36 +0530 Subject: [PATCH 026/125] arith: add elementwise operator declarations --- ext/mri/arrayfire.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 40c642c..f622776 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -174,7 +174,11 @@ static VALUE arf_ew_##name(VALUE left_val, VALUE right_val) { \ } #define DECL_ELEMENTWISE_RUBY_ACCESSOR(name) static VALUE arf_ew_##name(VALUE left_val, VALUE right_val); + DECL_ELEMENTWISE_RUBY_ACCESSOR(add) +DECL_ELEMENTWISE_RUBY_ACCESSOR(subtract) +DECL_ELEMENTWISE_RUBY_ACCESSOR(multiply) +DECL_ELEMENTWISE_RUBY_ACCESSOR(divide) static VALUE elementwise_op(arf::ewop_t op, VALUE left_val, VALUE right_val); From 388f66243884c7dccd72fc94b9207c09c1e6d855 Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Thu, 29 Jun 2017 00:02:36 +0530 Subject: [PATCH 027/125] arith: add elementwise operator definitions --- ext/mri/arrayfire.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index f622776..0356609 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -168,9 +168,16 @@ static VALUE arf_get_seed(VALUE self); static size_t* interpret_shape(VALUE arg, size_t* dim); -#define DEF_ELEMENTWISE_RUBY_ACCESSOR(oper, name) \ -static VALUE arf_ew_##name(VALUE left_val, VALUE right_val) { \ - return elementwise_op(arf::EW_##oper, left_val, right_val); \ +#define DEF_ELEMENTWISE_RUBY_ACCESSOR(name, oper) \ +static VALUE arf_ew_##name(VALUE left_val, VALUE right_val) { \ + afstruct* left; \ + afstruct* right; \ + afstruct* result = ALLOC(afstruct); \ + Data_Get_Struct(left_val, afstruct, left); \ + Data_Get_Struct(right_val, afstruct, right); \ + af_##oper(&result->carray, left->carray, right->carray, true); \ + af_print_array(result->carray); \ + return Data_Wrap_Struct(CLASS_OF(left_val), NULL, arf_free, result); \ } #define DECL_ELEMENTWISE_RUBY_ACCESSOR(name) static VALUE arf_ew_##name(VALUE left_val, VALUE right_val); @@ -445,6 +452,11 @@ static VALUE arf_eqeq(VALUE left_val, VALUE right_val) { return Qtrue; } +DEF_ELEMENTWISE_RUBY_ACCESSOR(add, add) +DEF_ELEMENTWISE_RUBY_ACCESSOR(subtract, sub) +DEF_ELEMENTWISE_RUBY_ACCESSOR(multiply, mul) +DEF_ELEMENTWISE_RUBY_ACCESSOR(divide, div) + // Algorithm static VALUE arf_sum(VALUE self){ From 173ccc38c17a0caded14c7dbbaf76e31bc950e51 Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Thu, 29 Jun 2017 00:08:23 +0530 Subject: [PATCH 028/125] arith: add unary operator declarations --- ext/mri/arrayfire.c | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 0356609..65a35b6 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -181,14 +181,37 @@ static VALUE arf_ew_##name(VALUE left_val, VALUE right_val) { \ } #define DECL_ELEMENTWISE_RUBY_ACCESSOR(name) static VALUE arf_ew_##name(VALUE left_val, VALUE right_val); +#define DECL_UNARY_RUBY_ACCESSOR(name) static VALUE arf_unary_##name(VALUE self); DECL_ELEMENTWISE_RUBY_ACCESSOR(add) DECL_ELEMENTWISE_RUBY_ACCESSOR(subtract) DECL_ELEMENTWISE_RUBY_ACCESSOR(multiply) DECL_ELEMENTWISE_RUBY_ACCESSOR(divide) - -static VALUE elementwise_op(arf::ewop_t op, VALUE left_val, VALUE right_val); +DECL_UNARY_RUBY_ACCESSOR(sin) +DECL_UNARY_RUBY_ACCESSOR(cos) +DECL_UNARY_RUBY_ACCESSOR(tan) +DECL_UNARY_RUBY_ACCESSOR(asin) +DECL_UNARY_RUBY_ACCESSOR(acos) +DECL_UNARY_RUBY_ACCESSOR(atan) +DECL_UNARY_RUBY_ACCESSOR(sinh) +DECL_UNARY_RUBY_ACCESSOR(cosh) +DECL_UNARY_RUBY_ACCESSOR(tanh) +DECL_UNARY_RUBY_ACCESSOR(asinh) +DECL_UNARY_RUBY_ACCESSOR(acosh) +DECL_UNARY_RUBY_ACCESSOR(atanh) +DECL_UNARY_RUBY_ACCESSOR(exp) +DECL_UNARY_RUBY_ACCESSOR(log2) +DECL_UNARY_RUBY_ACCESSOR(log1p) +DECL_UNARY_RUBY_ACCESSOR(log10) +DECL_UNARY_RUBY_ACCESSOR(sqrt) +DECL_UNARY_RUBY_ACCESSOR(erf) +DECL_UNARY_RUBY_ACCESSOR(erfc) +DECL_UNARY_RUBY_ACCESSOR(cbrt) +DECL_UNARY_RUBY_ACCESSOR(lgamma) +DECL_UNARY_RUBY_ACCESSOR(tgamma) +DECL_UNARY_RUBY_ACCESSOR(floor) +DECL_UNARY_RUBY_ACCESSOR(ceil) /* * Macro defines an element-wise accessor function for some operation. From 0e28e9e99df4d035a9c55b912ca8b76bfd937a13 Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Thu, 29 Jun 2017 00:12:17 +0530 Subject: [PATCH 029/125] arith: add unary operator definitions --- ext/mri/arrayfire.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 65a35b6..5196d85 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -180,6 +180,16 @@ static VALUE arf_ew_##name(VALUE left_val, VALUE right_val) { \ return Data_Wrap_Struct(CLASS_OF(left_val), NULL, arf_free, result); \ } +#define DEF_UNARY_RUBY_ACCESSOR(oper, name) \ +static VALUE arf_unary_##name(VALUE self) { \ + afstruct* obj; \ + afstruct* result = ALLOC(afstruct); \ + Data_Get_Struct(self, afstruct, obj); \ + af_##oper(&result->carray, obj->carray); \ + af_print_array(result->carray); \ + return Data_Wrap_Struct(CLASS_OF(self), NULL, arf_free, result); \ +} + #define DECL_ELEMENTWISE_RUBY_ACCESSOR(name) static VALUE arf_ew_##name(VALUE left_val, VALUE right_val); #define DECL_UNARY_RUBY_ACCESSOR(name) static VALUE arf_unary_##name(VALUE self); @@ -480,6 +490,31 @@ DEF_ELEMENTWISE_RUBY_ACCESSOR(subtract, sub) DEF_ELEMENTWISE_RUBY_ACCESSOR(multiply, mul) DEF_ELEMENTWISE_RUBY_ACCESSOR(divide, div) +DEF_UNARY_RUBY_ACCESSOR(sin, sin) +DEF_UNARY_RUBY_ACCESSOR(cos, cos) +DEF_UNARY_RUBY_ACCESSOR(tan, tan) +DEF_UNARY_RUBY_ACCESSOR(asin, asin) +DEF_UNARY_RUBY_ACCESSOR(acos, acos) +DEF_UNARY_RUBY_ACCESSOR(atan, atan) +DEF_UNARY_RUBY_ACCESSOR(sinh, sinh) +DEF_UNARY_RUBY_ACCESSOR(cosh, cosh) +DEF_UNARY_RUBY_ACCESSOR(tanh, tanh) +DEF_UNARY_RUBY_ACCESSOR(asinh, asinh) +DEF_UNARY_RUBY_ACCESSOR(acosh, acosh) +DEF_UNARY_RUBY_ACCESSOR(atanh, atanh) +DEF_UNARY_RUBY_ACCESSOR(exp, exp) +DEF_UNARY_RUBY_ACCESSOR(log2, log2) +DEF_UNARY_RUBY_ACCESSOR(log1p, log1p) +DEF_UNARY_RUBY_ACCESSOR(log10, log10) +DEF_UNARY_RUBY_ACCESSOR(sqrt, sqrt) +DEF_UNARY_RUBY_ACCESSOR(erf, erf) +DEF_UNARY_RUBY_ACCESSOR(erfc, erfc) +DEF_UNARY_RUBY_ACCESSOR(cbrt, cbrt) +DEF_UNARY_RUBY_ACCESSOR(lgamma, lgamma) +DEF_UNARY_RUBY_ACCESSOR(tgamma, tgamma) +DEF_UNARY_RUBY_ACCESSOR(floor, floor) +DEF_UNARY_RUBY_ACCESSOR(ceil, ceil) + // Algorithm static VALUE arf_sum(VALUE self){ From 5177d90a599edc3dce906ccdf28718c56d493834 Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Thu, 29 Jun 2017 00:15:45 +0530 Subject: [PATCH 030/125] arith: define arith methods for array class --- ext/mri/arrayfire.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 5196d85..cc29093 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -242,8 +242,35 @@ void Init_arrayfire() { rb_define_alloc_func(Af_Array, arf_alloc); rb_define_method(Af_Array, "initialize", (METHOD)arf_init, -1); rb_define_method(Af_Array, "+",(METHOD)arf_ew_add,1); + rb_define_method(Af_Array, "-",(METHOD)arf_ew_subtract,1); + rb_define_method(Af_Array, "*",(METHOD)arf_ew_multiply,1); + rb_define_method(Af_Array, "/",(METHOD)arf_ew_divide,1); rb_define_method(Af_Array, "==",(METHOD)arf_eqeq,1); - rb_define_method(Af_Array, "inverse",(METHOD)arf_inverse,0); + + rb_define_method(Af_Array, "sin", (METHOD)arf_unary_sin, 0); + rb_define_method(Af_Array, "cos", (METHOD)arf_unary_cos, 0); + rb_define_method(Af_Array, "tan", (METHOD)arf_unary_tan, 0); + rb_define_method(Af_Array, "asin", (METHOD)arf_unary_asin, 0); + rb_define_method(Af_Array, "acos", (METHOD)arf_unary_acos, 0); + rb_define_method(Af_Array, "atan", (METHOD)arf_unary_atan, 0); + rb_define_method(Af_Array, "sinh", (METHOD)arf_unary_sinh, 0); + rb_define_method(Af_Array, "cosh", (METHOD)arf_unary_cosh, 0); + rb_define_method(Af_Array, "tanh", (METHOD)arf_unary_tanh, 0); + rb_define_method(Af_Array, "asinh", (METHOD)arf_unary_asinh, 0); + rb_define_method(Af_Array, "acosh", (METHOD)arf_unary_acosh, 0); + rb_define_method(Af_Array, "atanh", (METHOD)arf_unary_atanh, 0); + rb_define_method(Af_Array, "exp", (METHOD)arf_unary_exp, 0); + rb_define_method(Af_Array, "log2", (METHOD)arf_unary_log2, 0); + rb_define_method(Af_Array, "log1p", (METHOD)arf_unary_log1p, 0); + rb_define_method(Af_Array, "log10", (METHOD)arf_unary_log10, 0); + rb_define_method(Af_Array, "sqrt", (METHOD)arf_unary_sqrt, 0); + rb_define_method(Af_Array, "erf", (METHOD)arf_unary_erf, 0); + rb_define_method(Af_Array, "erfc", (METHOD)arf_unary_erfc, 0); + rb_define_method(Af_Array, "cbrt", (METHOD)arf_unary_cbrt, 0); + rb_define_method(Af_Array, "lgamma", (METHOD)arf_unary_lgamma, 0); + rb_define_method(Af_Array, "tgamma", (METHOD)arf_unary_tgamma, 0); + rb_define_method(Af_Array, "floor", (METHOD)arf_unary_floor, 0); + rb_define_method(Af_Array, "ceil", (METHOD)arf_unary_ceil, 0); Algorithm = rb_define_class_under(ArrayFire, "Algorithm", rb_cObject); rb_define_method(Algorithm, "sum", (METHOD)arf_sum, 0); From de148e17501f8cbe6d4c56a9e86898c192c93d76 Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Thu, 29 Jun 2017 00:42:03 +0530 Subject: [PATCH 031/125] blas: implement blas routines --- ext/mri/arrayfire.c | 41 +++----------------------------- ext/mri/cmodules/blas.c | 52 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 49 insertions(+), 44 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index cc29093..b72c23f 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -63,8 +63,8 @@ static VALUE arf_get_backend_device_id(VALUE self); static VALUE arf_matmul(VALUE self, VALUE left_val, VALUE right_val); static VALUE arf_dot(VALUE self, VALUE left_val, VALUE right_val); -static VALUE arf_transpose(VALUE self, VALUE left_val, VALUE right_val); -static VALUE arf_transpose_inplace(VALUE self, VALUE left_val, VALUE right_val); +static VALUE arf_transpose(VALUE self, VALUE input); +static VALUE arf_transpose_inplace(VALUE self, VALUE input); static VALUE arf_get_stream(VALUE self); static VALUE arf_get_native_id(VALUE self); @@ -669,42 +669,7 @@ static VALUE arf_get_backend_device_id(VALUE self){ return Qnil; } -// BLAS - -static VALUE arf_matmul(VALUE self, VALUE left_val, VALUE right_val){ - - afstruct* left; - afstruct* right; - afstruct* result = ALLOC(afstruct); - - Data_Get_Struct(left_val, afstruct, left); - Data_Get_Struct(right_val, afstruct, right); - - - result->ndims = left->ndims; - size_t dimension[2]; - dimension[0] = left->dimension[0]; - dimension[1] = right->dimension[1]; - size_t count = dimension[0]*dimension[1]; - result->dimension = dimension; - result->count = count; - - arf::matmul(result, left, right); - - return Data_Wrap_Struct(CLASS_OF(left_val), NULL, arf_free, result); -} - -static VALUE arf_dot(VALUE self, VALUE left_val, VALUE right_val){ - return Qnil; -} - -static VALUE arf_transpose(VALUE self, VALUE left_val, VALUE right_val){ - return Qnil; -} - -static VALUE arf_transpose_inplace(VALUE self, VALUE left_val, VALUE right_val){ - return Qnil; -} +#include "cmodules/blas.c" // CUDA diff --git a/ext/mri/cmodules/blas.c b/ext/mri/cmodules/blas.c index fbaa6d4..91cf56d 100644 --- a/ext/mri/cmodules/blas.c +++ b/ext/mri/cmodules/blas.c @@ -1,7 +1,47 @@ -static void matmul(afstruct *result, afstruct *left, afstruct *right) -{ - array l = array(left->dimension[0], left->dimension[1], left->array); - array r = array(right->dimension[0], right->dimension[1], right->array); - array res = matmul(l,r); - result->array = res.host(); +static VALUE arf_matmul(VALUE self, VALUE left_val, VALUE right_val){ + + afstruct* left; + afstruct* right; + afstruct* result = ALLOC(afstruct); + + Data_Get_Struct(left_val, afstruct, left); + Data_Get_Struct(right_val, afstruct, right); + + af_matmul(&result->carray, left->carray, right->carray, AF_MAT_NONE, AF_MAT_NONE); + + return Data_Wrap_Struct(CLASS_OF(left_val), NULL, arf_free, result); +} + +static VALUE arf_dot(VALUE self, VALUE left_val, VALUE right_val){ + afstruct* left; + afstruct* right; + afstruct* result = ALLOC(afstruct); + + Data_Get_Struct(left_val, afstruct, left); + Data_Get_Struct(right_val, afstruct, right); + + af_dot(&result->carray, left->carray, right->carray, AF_MAT_NONE, AF_MAT_NONE); + + return Data_Wrap_Struct(CLASS_OF(left_val), NULL, arf_free, result); +} + +static VALUE arf_transpose(VALUE self, VALUE input){ + afstruct* obj; + afstruct* result = ALLOC(afstruct); + + Data_Get_Struct(input, afstruct, obj); + + af_transpose(&result->carray, obj->carray, false); + + return Data_Wrap_Struct(CLASS_OF(self), NULL, arf_free, result); +} + +static VALUE arf_transpose_inplace(VALUE self, VALUE input){ + afstruct* obj; + + Data_Get_Struct(input, afstruct, obj); + + af_transpose_inplace(obj->carray, false); + + return Data_Wrap_Struct(CLASS_OF(self), NULL, arf_free, obj); } From c006d863ec86a1cc8832528eb164e011fa4141c5 Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Thu, 29 Jun 2017 00:43:11 +0530 Subject: [PATCH 032/125] cleanup --- ext/mri/arrayfire.c | 90 +++------------------------------------------ 1 file changed, 5 insertions(+), 85 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index b72c23f..5b13245 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -236,7 +236,6 @@ static VALUE arf_matmul(VALUE self, VALUE left_val, VALUE right_val); void Init_arrayfire() { ArrayFire = rb_define_module("ArrayFire"); - rb_define_method(ArrayFire, "test1", (METHOD)test1, 0); Af_Array = rb_define_class_under(ArrayFire, "Af_Array", rb_cObject); rb_define_alloc_func(Af_Array, arf_alloc); @@ -467,49 +466,8 @@ static void arf_free(afstruct* af) DEF_ELEMENTWISE_RUBY_ACCESSOR(ADD, add) -static VALUE elementwise_op(arf::ewop_t op, VALUE left_val, VALUE right_val) { - - afstruct* left; - afstruct* right; - afstruct* result = ALLOC(afstruct); - - Data_Get_Struct(left_val, afstruct, left); - Data_Get_Struct(right_val, afstruct, right); - - - result->ndims = left->ndims; - result->dimension = left->dimension; - result->count = left->count; - arf::add(result, left, right); - - return Data_Wrap_Struct(CLASS_OF(left_val), NULL, arf_free, result); -} - static VALUE arf_eqeq(VALUE left_val, VALUE right_val) { - afstruct* left; - afstruct* right; - - bool result = true; - - size_t i; - size_t count = 1; - - Data_Get_Struct(left_val, afstruct, left); - Data_Get_Struct(right_val, afstruct, right); - - for(size_t i = 0; i < left->ndims; i++){ - if(left->dimension[i]!= right->dimension[i]){ - return Qfalse; - } - } - - for(size_t i = 0; i < left->count; i++){ - if(left->array[i]!= right->array[i]){ - return Qfalse; - } - } - - return Qtrue; + return Qnil; } DEF_ELEMENTWISE_RUBY_ACCESSOR(add, add) @@ -961,19 +919,7 @@ static VALUE arf_qr_inplace(VALUE self){ } static VALUE arf_cholesky(VALUE self, VALUE val){ - - afstruct* matrix; - afstruct* result = ALLOC(afstruct); - - Data_Get_Struct(val, afstruct, matrix); - - - result->ndims = matrix->ndims; - result->dimension = matrix->dimension; - result->count = matrix->count; - arf::cholesky_(result, matrix); - - return Data_Wrap_Struct(CLASS_OF(val), NULL, arf_free, result); + return Qnil; } static VALUE arf_cholesky_inplace(VALUE self){ @@ -989,44 +935,18 @@ static VALUE arf_solve_lu(VALUE self){ } static VALUE arf_inverse(VALUE self){ - - afstruct* matrix; - afstruct* result = ALLOC(afstruct); - - Data_Get_Struct(self, afstruct, matrix); - - result->ndims = matrix->ndims; - result->dimension = matrix->dimension; - result->count = matrix->count; - arf::inverse_(result, matrix); - - return Data_Wrap_Struct(CLASS_OF(self), NULL, arf_free, result); + return Qnil; } static VALUE arf_rank(VALUE self){ return Qnil; } static VALUE arf_det(VALUE self, VALUE val){ - - afstruct* matrix; - Data_Get_Struct(val, afstruct, matrix); - - af_array m; - dim_t dims[matrix->ndims] ; - for (size_t index = 0; index < matrix->ndims; ++index){ - dims[index] = (dim_t)matrix->dimension[index]; - } - af_create_array( &m, matrix->array, matrix->ndims, dims, f64 ); - double real, imaginary; - af_det(&real,&imaginary,m); - return DBL2NUM(real); + return Qnil; } static VALUE arf_norm(VALUE self, VALUE val){ - afstruct* matrix - Data_Get_Struct(val, afstruct, matrix); - double norm = arf::norm_(matrix); - return DBL2NUM(norm); + return Qnil; } static VALUE arf_is_lapack_available(VALUE self){ From 16fd9d06700d3b7611ccea505cb0747725bf0b86 Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Thu, 29 Jun 2017 00:43:26 +0530 Subject: [PATCH 033/125] more cleanup --- ext/mri/ruby_arrayfire.cpp | 34 ---------------------------------- 1 file changed, 34 deletions(-) diff --git a/ext/mri/ruby_arrayfire.cpp b/ext/mri/ruby_arrayfire.cpp index 4580da3..0070143 100644 --- a/ext/mri/ruby_arrayfire.cpp +++ b/ext/mri/ruby_arrayfire.cpp @@ -13,40 +13,6 @@ namespace arf { - enum ewop_t { - EW_ADD, - EW_SUB, - EW_MUL, - EW_DIV, - EW_POW, - EW_MOD, - EW_EQEQ, - EW_NEQ, - EW_LT, - EW_GT, - EW_LEQ, - EW_GEQ, - }; - using namespace af; - static array A; // populated before each timing - static void fn() - { - array B = matmul(A, A); // matrix multiply - B.eval(); // ensure evaluated - } - - static void createArray(afstruct *afarray) - { - dim_t dims[afarray->ndims] ; - - for (size_t index = 0; index < afarray->ndims; ++index){ - dims[index] = (dim_t)afarray->dimension[index]; - } - } - - #include "arith.cpp" - #include "blas.cpp" - #include "lapack.cpp" } extern "C" { #include "arrayfire.c" From 8ac0ce7f8319e3319ccf2c49d2e42a59f0a12fb8 Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Thu, 29 Jun 2017 00:48:40 +0530 Subject: [PATCH 034/125] modify lapack declarations --- ext/mri/arrayfire.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 5b13245..d8b11b6 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -134,18 +134,18 @@ static VALUE arf_replace(VALUE self); static VALUE arf_replace_scalar(VALUE self); -static VALUE arf_svd(VALUE self); -static VALUE arf_svd_inplace(VALUE self); -static VALUE arf_lu(VALUE self); +static VALUE arf_svd(VALUE self, VALUE val); +static VALUE arf_svd_inplace(VALUE self, VALUE val); +static VALUE arf_lu(VALUE self, VALUE val); static VALUE arf_lu_inplace(VALUE self); -static VALUE arf_qr(VALUE self); +static VALUE arf_qr(VALUE self, VALUE val); static VALUE arf_qr_inplace(VALUE self); static VALUE arf_cholesky(VALUE self, VALUE val); static VALUE arf_cholesky_inplace(VALUE self); -static VALUE arf_solve(VALUE self); -static VALUE arf_solve_lu(VALUE self); -static VALUE arf_inverse(VALUE self); -static VALUE arf_rank(VALUE self); +static VALUE arf_solve(VALUE self, VALUE lhs_val, VALUE rhs_val); +static VALUE arf_solve_lu(VALUE self, VALUE lhs_val, VALUE rhs_val, VALUE piv_val); +static VALUE arf_inverse(VALUE self, VALUE val); +static VALUE arf_rank(VALUE self, VALUE val); static VALUE arf_det(VALUE self, VALUE val); static VALUE arf_norm(VALUE self, VALUE val); static VALUE arf_is_lapack_available(VALUE self); From 23593d34bcac78f6fdd1ce17a9938747db090ca5 Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Thu, 29 Jun 2017 00:52:16 +0530 Subject: [PATCH 035/125] add and load lapack functions --- ext/mri/arrayfire.c | 61 +------------- ext/mri/cmodules/lapack.c | 170 +++++++++++++++++++++++++++++++++----- 2 files changed, 150 insertions(+), 81 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index d8b11b6..5dc2d99 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -892,66 +892,7 @@ static VALUE arf_replace_scalar(VALUE self){ return Qnil; } -// Lapack - -static VALUE arf_svd(VALUE self){ - return Qnil; -} - -static VALUE arf_svd_inplace(VALUE self){ - return Qnil; -} - -static VALUE arf_lu(VALUE self){ - return Qnil; -} - -static VALUE arf_lu_inplace(VALUE self){ - return Qnil; -} - -static VALUE arf_qr(VALUE self){ - return Qnil; -} - -static VALUE arf_qr_inplace(VALUE self){ - return Qnil; -} - -static VALUE arf_cholesky(VALUE self, VALUE val){ - return Qnil; -} - -static VALUE arf_cholesky_inplace(VALUE self){ - return Qnil; -} - -static VALUE arf_solve(VALUE self){ - return Qnil; -} - -static VALUE arf_solve_lu(VALUE self){ - return Qnil; -} - -static VALUE arf_inverse(VALUE self){ - return Qnil; -} -static VALUE arf_rank(VALUE self){ - return Qnil; -} - -static VALUE arf_det(VALUE self, VALUE val){ - return Qnil; -} - -static VALUE arf_norm(VALUE self, VALUE val){ - return Qnil; -} - -static VALUE arf_is_lapack_available(VALUE self){ - return Qnil; -} +#include "cmodules/lapack.c" // Random diff --git a/ext/mri/cmodules/lapack.c b/ext/mri/cmodules/lapack.c index 8821316..b5e117f 100644 --- a/ext/mri/cmodules/lapack.c +++ b/ext/mri/cmodules/lapack.c @@ -1,21 +1,149 @@ -static void cholesky_(afstruct *result, afstruct *matrix) -{ - array m = array(matrix->dimension[0], matrix->dimension[1], matrix->array); - bool is_upper = true; - array res; - cholesky(res, m, is_upper); - result->array = res.host(); -} - -static void inverse_(afstruct *result, afstruct *matrix) -{ - array m = array(matrix->dimension[0], matrix->dimension[1], matrix->array); - array res = inverse(m); - result->array = res.host(); -} - -static double norm_(afstruct *matrix) -{ - array m = array(matrix->dimension[0], matrix->dimension[1], matrix->array); - return norm(m, AF_NORM_EUCLID, 1, 1); -} \ No newline at end of file +static VALUE arf_svd(VALUE self, VALUE val){ + afstruct* input; + afstruct* u = ALLOC(afstruct); + afstruct* s = ALLOC(afstruct); + afstruct* vt = ALLOC(afstruct); + + Data_Get_Struct(val, afstruct, input); + + af_svd(&u->carray, &s->carray, &vt->carray, input->carray); + + return Data_Wrap_Struct(CLASS_OF(val), NULL, arf_free, u); +} + +static VALUE arf_svd_inplace(VALUE self, VALUE val){ + afstruct* input; + afstruct* u = ALLOC(afstruct); + afstruct* s = ALLOC(afstruct); + afstruct* vt = ALLOC(afstruct); + + Data_Get_Struct(val, afstruct, input); + + af_svd_inplace(&u->carray, &s->carray, &vt->carray, input->carray); + + return Data_Wrap_Struct(CLASS_OF(val), NULL, arf_free, u); +} + +static VALUE arf_lu(VALUE self, VALUE val){ + afstruct* input; + afstruct* lower = ALLOC(afstruct); + afstruct* upper = ALLOC(afstruct); + afstruct* pivot = ALLOC(afstruct); + + Data_Get_Struct(val, afstruct, input); + + af_lu(&lower->carray, &upper->carray, &pivot->carray, input->carray); + return Data_Wrap_Struct(CLASS_OF(val), NULL, arf_free, lower); +} + +static VALUE arf_lu_inplace(VALUE self){ + return Qnil; +} + +static VALUE arf_qr(VALUE self, VALUE val){ + afstruct* input; + afstruct* q = ALLOC(afstruct); + afstruct* r = ALLOC(afstruct); + afstruct* tau = ALLOC(afstruct); + + Data_Get_Struct(val, afstruct, input); + + af_qr(&q->carray, &r->carray, &tau->carray, input->carray); + return Data_Wrap_Struct(CLASS_OF(val), NULL, arf_free, q); +} + +static VALUE arf_qr_inplace(VALUE self){ + return Qnil; +} + +static VALUE arf_cholesky(VALUE self, VALUE val){ + afstruct* input; + afstruct* output = ALLOC(afstruct); + int* info; + + Data_Get_Struct(val, afstruct, input); + + af_cholesky(&output->carray, info, input->carray, true); + return Data_Wrap_Struct(CLASS_OF(val), NULL, arf_free, output); +} + +static VALUE arf_cholesky_inplace(VALUE self){ + return Qnil; +} + +static VALUE arf_solve(VALUE self, VALUE lhs_val, VALUE rhs_val){ + + afstruct* lhs; + afstruct* rhs; + afstruct* result = ALLOC(afstruct); + + Data_Get_Struct(lhs_val, afstruct, lhs); + Data_Get_Struct(rhs_val, afstruct, rhs); + + af_solve(&result->carray, lhs->carray, rhs->carray, AF_MAT_NONE); + return Data_Wrap_Struct(CLASS_OF(lhs_val), NULL, arf_free, result); + +} + +static VALUE arf_solve_lu(VALUE self, VALUE lhs_val, VALUE rhs_val, VALUE piv_val){ + afstruct* lhs; + afstruct* rhs; + afstruct* piv; + afstruct* result = ALLOC(afstruct); + + Data_Get_Struct(lhs_val, afstruct, lhs); + Data_Get_Struct(rhs_val, afstruct, rhs); + + af_solve_lu(&result->carray, lhs->carray, piv->carray, rhs->carray, AF_MAT_NONE); + return Data_Wrap_Struct(CLASS_OF(lhs_val), NULL, arf_free, result); +} + +static VALUE arf_inverse(VALUE self, VALUE val){ + + afstruct* matrix; + afstruct* result = ALLOC(afstruct); + + Data_Get_Struct(val, afstruct, matrix); + + af_inverse(&result->carray, matrix->carray, AF_MAT_NONE); + + return Data_Wrap_Struct(CLASS_OF(val), NULL, arf_free, result); +} + +static VALUE arf_rank(VALUE self, VALUE val){ + afstruct* matrix; + uint rank; + + Data_Get_Struct(val, afstruct, matrix); + + af_rank(&rank, matrix->carray, 0.001); + return UINT2NUM(rank); +} + +static VALUE arf_det(VALUE self, VALUE val){ + afstruct* matrix; + uint a; + double det_real; + // double det_image; + + Data_Get_Struct(val, afstruct, matrix); + + af_rank(&a, matrix->carray, det_real); + return DBL2NUM(det_real); +} + +static VALUE arf_norm(VALUE self, VALUE val){ + afstruct* matrix; + double norm; + double p = 0; + double q = 0; + Data_Get_Struct(val, afstruct, matrix); + af_norm(&norm, matrix->carray, AF_NORM_EUCLID, p, q); + return DBL2NUM(norm); +} + +static VALUE arf_is_lapack_available(VALUE self){ + bool* output; + // af_is_lapack_available(output); + return Qfalse; +} From ab2be5f7fb54d0b17e3a0abbc8d3b048d55e0215 Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Thu, 29 Jun 2017 00:54:23 +0530 Subject: [PATCH 036/125] convert Algorithm methods to class methods --- ext/mri/arrayfire.c | 68 ++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 5dc2d99..2babddd 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -272,40 +272,40 @@ void Init_arrayfire() { rb_define_method(Af_Array, "ceil", (METHOD)arf_unary_ceil, 0); Algorithm = rb_define_class_under(ArrayFire, "Algorithm", rb_cObject); - rb_define_method(Algorithm, "sum", (METHOD)arf_sum, 0); - rb_define_method(Algorithm, "sum_nan", (METHOD)arf_sum_nan, 0); - rb_define_method(Algorithm, "product", (METHOD)arf_product, 0); - rb_define_method(Algorithm, "product_nan", (METHOD)arf_product_nan, 0); - rb_define_method(Algorithm, "min", (METHOD)arf_min, 0); - rb_define_method(Algorithm, "max", (METHOD)arf_max, 0); - rb_define_method(Algorithm, "all_true", (METHOD)arf_all_true, 0); - rb_define_method(Algorithm, "any_true", (METHOD)arf_any_true, 0); - rb_define_method(Algorithm, "count", (METHOD)arf_count, 0); - rb_define_method(Algorithm, "sum_all", (METHOD)arf_sum_all, 0); - rb_define_method(Algorithm, "sum_nan_all", (METHOD)arf_sum_nan_all, 0); - rb_define_method(Algorithm, "product_all", (METHOD)arf_product_all, 0); - rb_define_method(Algorithm, "product_nan_all", (METHOD)arf_product_nan_all, 0); - rb_define_method(Algorithm, "min_all", (METHOD)arf_min_all, 0); - rb_define_method(Algorithm, "max_all", (METHOD)arf_max_all, 0); - rb_define_method(Algorithm, "all_true_all", (METHOD)arf_all_true_all, 0); - rb_define_method(Algorithm, "any_true_all", (METHOD)arf_any_true_all, 0); - rb_define_method(Algorithm, "count_all", (METHOD)arf_count_all, 0); - rb_define_method(Algorithm, "imin", (METHOD)arf_imin, 0); - rb_define_method(Algorithm, "imax", (METHOD)arf_imax, 0); - rb_define_method(Algorithm, "imin_all", (METHOD)arf_imin_all, 0); - rb_define_method(Algorithm, "imax_all", (METHOD)arf_imax_all, 0); - rb_define_method(Algorithm, "accum", (METHOD)arf_accum, 0); - rb_define_method(Algorithm, "scan", (METHOD)arf_scan, 0); - rb_define_method(Algorithm, "scan_by_key", (METHOD)arf_scan_by_key, 0); - rb_define_method(Algorithm, "where", (METHOD)arf_where, 0); - rb_define_method(Algorithm, "diff1", (METHOD)arf_diff1, 0); - rb_define_method(Algorithm, "diff2", (METHOD)arf_diff2, 0); - rb_define_method(Algorithm, "sort", (METHOD)arf_sort, 0); - rb_define_method(Algorithm, "sort_index", (METHOD)arf_sort_index, 0); - rb_define_method(Algorithm, "sort_by_key", (METHOD)arf_sort_by_key, 0); - rb_define_method(Algorithm, "set_unique", (METHOD)arf_set_unique, 0); - rb_define_method(Algorithm, "set_union", (METHOD)arf_set_union, 0); - rb_define_method(Algorithm, "set_intersect", (METHOD)arf_set_intersect, 0); + rb_define_singleton_method(Algorithm, "sum", (METHOD)arf_sum, 0); + rb_define_singleton_method(Algorithm, "sum_nan", (METHOD)arf_sum_nan, 0); + rb_define_singleton_method(Algorithm, "product", (METHOD)arf_product, 0); + rb_define_singleton_method(Algorithm, "product_nan", (METHOD)arf_product_nan, 0); + rb_define_singleton_method(Algorithm, "min", (METHOD)arf_min, 0); + rb_define_singleton_method(Algorithm, "max", (METHOD)arf_max, 0); + rb_define_singleton_method(Algorithm, "all_true", (METHOD)arf_all_true, 0); + rb_define_singleton_method(Algorithm, "any_true", (METHOD)arf_any_true, 0); + rb_define_singleton_method(Algorithm, "count", (METHOD)arf_count, 0); + rb_define_singleton_method(Algorithm, "sum_all", (METHOD)arf_sum_all, 0); + rb_define_singleton_method(Algorithm, "sum_nan_all", (METHOD)arf_sum_nan_all, 0); + rb_define_singleton_method(Algorithm, "product_all", (METHOD)arf_product_all, 0); + rb_define_singleton_method(Algorithm, "product_nan_all", (METHOD)arf_product_nan_all, 0); + rb_define_singleton_method(Algorithm, "min_all", (METHOD)arf_min_all, 0); + rb_define_singleton_method(Algorithm, "max_all", (METHOD)arf_max_all, 0); + rb_define_singleton_method(Algorithm, "all_true_all", (METHOD)arf_all_true_all, 0); + rb_define_singleton_method(Algorithm, "any_true_all", (METHOD)arf_any_true_all, 0); + rb_define_singleton_method(Algorithm, "count_all", (METHOD)arf_count_all, 0); + rb_define_singleton_method(Algorithm, "imin", (METHOD)arf_imin, 0); + rb_define_singleton_method(Algorithm, "imax", (METHOD)arf_imax, 0); + rb_define_singleton_method(Algorithm, "imin_all", (METHOD)arf_imin_all, 0); + rb_define_singleton_method(Algorithm, "imax_all", (METHOD)arf_imax_all, 0); + rb_define_singleton_method(Algorithm, "accum", (METHOD)arf_accum, 0); + rb_define_singleton_method(Algorithm, "scan", (METHOD)arf_scan, 0); + rb_define_singleton_method(Algorithm, "scan_by_key", (METHOD)arf_scan_by_key, 0); + rb_define_singleton_method(Algorithm, "where", (METHOD)arf_where, 0); + rb_define_singleton_method(Algorithm, "diff1", (METHOD)arf_diff1, 0); + rb_define_singleton_method(Algorithm, "diff2", (METHOD)arf_diff2, 0); + rb_define_singleton_method(Algorithm, "sort", (METHOD)arf_sort, 0); + rb_define_singleton_method(Algorithm, "sort_index", (METHOD)arf_sort_index, 0); + rb_define_singleton_method(Algorithm, "sort_by_key", (METHOD)arf_sort_by_key, 0); + rb_define_singleton_method(Algorithm, "set_unique", (METHOD)arf_set_unique, 0); + rb_define_singleton_method(Algorithm, "set_union", (METHOD)arf_set_union, 0); + rb_define_singleton_method(Algorithm, "set_intersect", (METHOD)arf_set_intersect, 0); Backend = rb_define_class_under(ArrayFire, "Backend", rb_cObject); rb_define_method(Backend, "get_backend_count", (METHOD)arf_get_backend_count, 0); From 6c7696e6280105328d672fc6bc2deab834cd3be5 Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Thu, 29 Jun 2017 00:59:55 +0530 Subject: [PATCH 037/125] modify declarations for Algorithm methods --- ext/mri/arrayfire.c | 46 ++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 2babddd..97779e0 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -20,29 +20,29 @@ static VALUE arf_init(int argc, VALUE* argv, VALUE self); static VALUE arf_alloc(VALUE klass); static void arf_free(afstruct* af); -static VALUE arf_sum(VALUE self); -static VALUE arf_sum_nan(VALUE self); -static VALUE arf_product(VALUE self); -static VALUE arf_product_nan(VALUE self); -static VALUE arf_min(VALUE self); -static VALUE arf_max(VALUE self); -static VALUE arf_all_true(VALUE self); -static VALUE arf_any_true(VALUE self); -static VALUE arf_count(VALUE self); -static VALUE arf_sum_all(VALUE self); -static VALUE arf_sum_nan_all(VALUE self); -static VALUE arf_product_all(VALUE self); -static VALUE arf_product_nan_all(VALUE self); -static VALUE arf_min_all(VALUE self); -static VALUE arf_max_all(VALUE self); -static VALUE arf_all_true_all(VALUE self); -static VALUE arf_any_true_all(VALUE self); -static VALUE arf_count_all(VALUE self); -static VALUE arf_imin(VALUE self); -static VALUE arf_imax(VALUE self); -static VALUE arf_imin_all(VALUE self); -static VALUE arf_imax_all(VALUE self); -static VALUE arf_accum(VALUE self); +static VALUE arf_sum(VALUE self, VALUE array_val, VALUE dim_val); +static VALUE arf_sum_nan(VALUE self, VALUE array_val, VALUE dim_val, VALUE nan_val); +static VALUE arf_product(VALUE self, VALUE array_val, VALUE dim_val); +static VALUE arf_product_nan(VALUE self, VALUE array_val, VALUE dim_val, VALUE nan_val); +static VALUE arf_min(VALUE self, VALUE array_val, VALUE dim_val); +static VALUE arf_max(VALUE self, VALUE array_val, VALUE dim_val); +static VALUE arf_all_true(VALUE self, VALUE array_val, VALUE dim_val); +static VALUE arf_any_true(VALUE self, VALUE array_val, VALUE dim_val); +static VALUE arf_count(VALUE self, VALUE array_val, VALUE dim_val); +static VALUE arf_sum_all(VALUE self, VALUE array_val); +static VALUE arf_sum_nan_all(VALUE self, VALUE array_val, VALUE nan_val); +static VALUE arf_product_all(VALUE self, VALUE array_val); +static VALUE arf_product_nan_all(VALUE self, VALUE array_val, VALUE nan_val); +static VALUE arf_min_all(VALUE self, VALUE array_val); +static VALUE arf_max_all(VALUE self, VALUE array_val); +static VALUE arf_all_true_all(VALUE self, VALUE array_val); +static VALUE arf_any_true_all(VALUE self, VALUE array_val); +static VALUE arf_count_all(VALUE self, VALUE array_val); +static VALUE arf_imin(VALUE self, VALUE array_val, VALUE dim_val); +static VALUE arf_imax(VALUE self, VALUE array_val, VALUE dim_val); +static VALUE arf_imin_all(VALUE self, VALUE array_val); +static VALUE arf_imax_all(VALUE self, VALUE array_val); +static VALUE arf_accum(VALUE self, VALUE array_val, VALUE dim_val); static VALUE arf_scan(VALUE self); static VALUE arf_scan_by_key(VALUE self); static VALUE arf_where(VALUE self); From a684faeaf0ae7ec758e246d2ca3661f1d6d7c244 Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Thu, 29 Jun 2017 01:00:34 +0530 Subject: [PATCH 038/125] add and load lapack functions --- ext/mri/arrayfire.c | 103 +------------ ext/mri/cmodules/algorithm.c | 286 +++++++++++++++++++++++++++++++++++ 2 files changed, 287 insertions(+), 102 deletions(-) create mode 100644 ext/mri/cmodules/algorithm.c diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 97779e0..9369750 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -502,108 +502,7 @@ DEF_UNARY_RUBY_ACCESSOR(ceil, ceil) // Algorithm -static VALUE arf_sum(VALUE self){ - return Qnil; -} -static VALUE arf_sum_nan(VALUE self){ - return Qnil; -} -static VALUE arf_product(VALUE self){ - return Qnil; -} -static VALUE arf_product_nan(VALUE self){ - return Qnil; -} -static VALUE arf_min(VALUE self){ - return Qnil; -} -static VALUE arf_max(VALUE self){ - return Qnil; -} -static VALUE arf_all_true(VALUE self){ - return Qnil; -} -static VALUE arf_any_true(VALUE self){ - return Qnil; -} -static VALUE arf_count(VALUE self){ - return Qnil; -} -static VALUE arf_sum_all(VALUE self){ - return Qnil; -} -static VALUE arf_sum_nan_all(VALUE self){ - return Qnil; -} -static VALUE arf_product_all(VALUE self){ - return Qnil; -} -static VALUE arf_product_nan_all(VALUE self){ - return Qnil; -} -static VALUE arf_min_all(VALUE self){ - return Qnil; -} -static VALUE arf_max_all(VALUE self){ - return Qnil; -} -static VALUE arf_all_true_all(VALUE self){ - return Qnil; -} -static VALUE arf_any_true_all(VALUE self){ - return Qnil; -} -static VALUE arf_count_all(VALUE self){ - return Qnil; -} -static VALUE arf_imin(VALUE self){ - return Qnil; -} -static VALUE arf_imax(VALUE self){ - return Qnil; -} -static VALUE arf_imin_all(VALUE self){ - return Qnil; -} -static VALUE arf_imax_all(VALUE self){ - return Qnil; -} -static VALUE arf_accum(VALUE self){ - return Qnil; -} -static VALUE arf_scan(VALUE self){ - return Qnil; -} -static VALUE arf_scan_by_key(VALUE self){ - return Qnil; -} -static VALUE arf_where(VALUE self){ - return Qnil; -} -static VALUE arf_diff1(VALUE self){ - return Qnil; -} -static VALUE arf_diff2(VALUE self){ - return Qnil; -} -static VALUE arf_sort(VALUE self){ - return Qnil; -} -static VALUE arf_sort_index(VALUE self){ - return Qnil; -} -static VALUE arf_sort_by_key(VALUE self){ - return Qnil; -} -static VALUE arf_set_unique(VALUE self){ - return Qnil; -} -static VALUE arf_set_union(VALUE self){ - return Qnil; -} -static VALUE arf_set_intersect(VALUE self){ - return Qnil; -} +#include "cmodules/algorithm.c" // Backend diff --git a/ext/mri/cmodules/algorithm.c b/ext/mri/cmodules/algorithm.c new file mode 100644 index 0000000..b493e5b --- /dev/null +++ b/ext/mri/cmodules/algorithm.c @@ -0,0 +1,286 @@ +static VALUE arf_sum(VALUE self, VALUE array_val, VALUE dim_val){ + afstruct* input; + afstruct* output = ALLOC(afstruct); + + Data_Get_Struct(array_val, afstruct, input); + + af_sum(&output->carray, input->carray, FIX2INT(dim_val)); + + return Data_Wrap_Struct(CLASS_OF(array_val), NULL, arf_free, output); +} + +static VALUE arf_sum_nan(VALUE self, VALUE array_val, VALUE dim_val, VALUE nan_val){ + afstruct* input; + afstruct* output = ALLOC(afstruct); + + Data_Get_Struct(array_val, afstruct, input); + + af_sum_nan(&output->carray, input->carray, FIX2INT(dim_val), NUM2DBL(nan_val)); + + return Data_Wrap_Struct(CLASS_OF(array_val), NULL, arf_free, output); +} + +static VALUE arf_product(VALUE self, VALUE array_val, VALUE dim_val){ + afstruct* input; + afstruct* output = ALLOC(afstruct); + + Data_Get_Struct(array_val, afstruct, input); + + af_product(&output->carray, input->carray, FIX2INT(dim_val)); + + return Data_Wrap_Struct(CLASS_OF(array_val), NULL, arf_free, output); +} + +static VALUE arf_product_nan(VALUE self, VALUE array_val, VALUE dim_val, VALUE nan_val){ + afstruct* input; + afstruct* output = ALLOC(afstruct); + + Data_Get_Struct(array_val, afstruct, input); + + af_product_nan(&output->carray, input->carray, FIX2INT(dim_val), NUM2DBL(nan_val)); + + return Data_Wrap_Struct(CLASS_OF(array_val), NULL, arf_free, output); +} + +static VALUE arf_min(VALUE self, VALUE array_val, VALUE dim_val){ + afstruct* input; + afstruct* output = ALLOC(afstruct); + + Data_Get_Struct(array_val, afstruct, input); + + af_min(&output->carray, input->carray, FIX2INT(dim_val)); + + return Data_Wrap_Struct(CLASS_OF(array_val), NULL, arf_free, output); +} + +static VALUE arf_max(VALUE self, VALUE array_val, VALUE dim_val){ + afstruct* input; + afstruct* output = ALLOC(afstruct); + + Data_Get_Struct(array_val, afstruct, input); + + af_max(&output->carray, input->carray, FIX2INT(dim_val)); + + return Data_Wrap_Struct(CLASS_OF(array_val), NULL, arf_free, output); +} + +static VALUE arf_all_true(VALUE self, VALUE array_val, VALUE dim_val){ + afstruct* input; + afstruct* output = ALLOC(afstruct); + + Data_Get_Struct(array_val, afstruct, input); + + af_sum(&output->carray, input->carray, FIX2INT(dim_val)); + + return Data_Wrap_Struct(CLASS_OF(array_val), NULL, arf_free, output); +} + +static VALUE arf_any_true(VALUE self, VALUE array_val, VALUE dim_val){ + afstruct* input; + afstruct* output = ALLOC(afstruct); + + Data_Get_Struct(array_val, afstruct, input); + + af_sum(&output->carray, input->carray, FIX2INT(dim_val)); + + return Data_Wrap_Struct(CLASS_OF(array_val), NULL, arf_free, output); +} + +static VALUE arf_count(VALUE self, VALUE array_val, VALUE dim_val){ + afstruct* input; + afstruct* output = ALLOC(afstruct); + + Data_Get_Struct(array_val, afstruct, input); + + af_sum(&output->carray, input->carray, FIX2INT(dim_val)); + + return Data_Wrap_Struct(CLASS_OF(array_val), NULL, arf_free, output); +} + +static VALUE arf_sum_all(VALUE self, VALUE array_val){ + afstruct* input; + double real_part, imag_part; + + Data_Get_Struct(array_val, afstruct, input); + + af_sum_all(&real_part, &imag_part, input->carray); + + return NUM2DBL(real_part); +} + +static VALUE arf_sum_nan_all(VALUE self, VALUE array_val, VALUE nan_val){ + afstruct* input; + double real_part, imag_part; + + Data_Get_Struct(array_val, afstruct, input); + + af_sum_nan_all(&real_part, &imag_part, input->carray, NUM2DBL(nan_val)); + + return NUM2DBL(real_part); +} + +static VALUE arf_product_all(VALUE self, VALUE array_val){ + afstruct* input; + double real_part, imag_part; + + Data_Get_Struct(array_val, afstruct, input); + + af_product_all(&real_part, &imag_part, input->carray); + + return NUM2DBL(real_part); +} + +static VALUE arf_product_nan_all(VALUE self, VALUE array_val, VALUE nan_val){ + afstruct* input; + double real_part, imag_part; + + Data_Get_Struct(array_val, afstruct, input); + + af_product_nan_all(&real_part, &imag_part, input->carray, NUM2DBL(nan_val)); + + return NUM2DBL(real_part); +} + +static VALUE arf_min_all(VALUE self, VALUE array_val){ + afstruct* input; + double real_part, imag_part; + + Data_Get_Struct(array_val, afstruct, input); + + af_min_all(&real_part, &imag_part, input->carray); + + return NUM2DBL(real_part); +} + +static VALUE arf_max_all(VALUE self, VALUE array_val){ + afstruct* input; + double real_part, imag_part; + + Data_Get_Struct(array_val, afstruct, input); + + af_max_all(&real_part, &imag_part, input->carray); + + return NUM2DBL(real_part); +} + +static VALUE arf_all_true_all(VALUE self, VALUE array_val){ + afstruct* input; + double real_part, imag_part; + + Data_Get_Struct(array_val, afstruct, input); + + af_all_true_all(&real_part, &imag_part, input->carray); + + return NUM2DBL(real_part); +} + +static VALUE arf_any_true_all(VALUE self, VALUE array_val){ + afstruct* input; + double real_part, imag_part; + + Data_Get_Struct(array_val, afstruct, input); + + af_any_true_all(&real_part, &imag_part, input->carray); + + return NUM2DBL(real_part); +} + +static VALUE arf_count_all(VALUE self, VALUE array_val){ + afstruct* input; + double real_part, imag_part; + + Data_Get_Struct(array_val, afstruct, input); + + af_count_all(&real_part, &imag_part, input->carray); + + return NUM2DBL(real_part); +} + +static VALUE arf_imin(VALUE self, VALUE array_val, VALUE dim_val){ + afstruct* input; + afstruct* output = ALLOC(afstruct); + afstruct* idx = ALLOC(afstruct); + + Data_Get_Struct(array_val, afstruct, input); + + af_imin(&output->carray, &idx->carray, input->carray, FIX2INT(dim_val)); + return Data_Wrap_Struct(CLASS_OF(array_val), NULL, arf_free, output); +} + +static VALUE arf_imax(VALUE self, VALUE array_val, VALUE dim_val){ + afstruct* input; + afstruct* output = ALLOC(afstruct); + afstruct* idx = ALLOC(afstruct); + + Data_Get_Struct(array_val, afstruct, input); + + af_imax(&output->carray, &idx->carray, input->carray, FIX2INT(dim_val)); + return Data_Wrap_Struct(CLASS_OF(array_val), NULL, arf_free, output); +} + +static VALUE arf_imin_all(VALUE self, VALUE array_val){ + afstruct* input; + double real_part, imag_part; + uint idx; + + Data_Get_Struct(array_val, afstruct, input); + + af_imin_all(&real_part, &imag_part, &idx, input->carray); + return NUM2DBL(real_part); +} + +static VALUE arf_imax_all(VALUE self, VALUE array_val){ + afstruct* input; + double real_part, imag_part; + uint idx; + + Data_Get_Struct(array_val, afstruct, input); + + af_imax_all(&real_part, &imag_part, &idx, input->carray); + return NUM2DBL(real_part); +} + +static VALUE arf_accum(VALUE self, VALUE array_val, VALUE dim_val){ + afstruct* input; + afstruct* output = ALLOC(afstruct); + + Data_Get_Struct(array_val, afstruct, input); + + af_accum(&output->carray, input->carray, FIX2INT(dim_val)); + + return Data_Wrap_Struct(CLASS_OF(array_val), NULL, arf_free, output); +} + +static VALUE arf_scan(VALUE self){ + return Qnil; +} +static VALUE arf_scan_by_key(VALUE self){ + return Qnil; +} +static VALUE arf_where(VALUE self){ + return Qnil; +} +static VALUE arf_diff1(VALUE self){ + return Qnil; +} +static VALUE arf_diff2(VALUE self){ + return Qnil; +} +static VALUE arf_sort(VALUE self){ + return Qnil; +} +static VALUE arf_sort_index(VALUE self){ + return Qnil; +} +static VALUE arf_sort_by_key(VALUE self){ + return Qnil; +} +static VALUE arf_set_unique(VALUE self){ + return Qnil; +} +static VALUE arf_set_union(VALUE self){ + return Qnil; +} +static VALUE arf_set_intersect(VALUE self){ + return Qnil; +} \ No newline at end of file From 69dc11518e9c30a07adb5dfc087054c1a9fff08c Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Thu, 29 Jun 2017 10:36:44 +0530 Subject: [PATCH 039/125] move data functions to data.c --- ext/mri/arrayfire.c | 100 +--------------------------------------- ext/mri/cmodules/data.c | 95 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 99 deletions(-) create mode 100644 ext/mri/cmodules/data.c diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 9369750..09834dd 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -692,105 +692,7 @@ static VALUE arf_get_platform(VALUE self){ return Qnil; } -//Data - - -static VALUE arf_constant(VALUE self){ - return Qnil; -} - -static VALUE arf_constant_complex(VALUE self){ - return Qnil; -} - -static VALUE arf_constant_long(VALUE self){ - return Qnil; -} - -static VALUE arf_constant_ulong(VALUE self){ - return Qnil; -} - -static VALUE arf_range(VALUE self){ - return Qnil; -} - -static VALUE arf_iota(VALUE self){ - return Qnil; -} - -static VALUE arf_identity(VALUE self){ - return Qnil; -} - -static VALUE arf_diag_create(VALUE self){ - return Qnil; -} - -static VALUE arf_diag_extract(VALUE self){ - return Qnil; -} - -static VALUE arf_join(VALUE self){ - return Qnil; -} - -static VALUE arf_join_many(VALUE self){ - return Qnil; -} - -static VALUE arf_tile(VALUE self){ - return Qnil; -} - -static VALUE arf_reorder(VALUE self){ - return Qnil; -} - -static VALUE arf_shift(VALUE self){ - return Qnil; -} - -static VALUE arf_moddims(VALUE self){ - return Qnil; -} - -static VALUE arf_flat(VALUE self){ - return Qnil; -} - -static VALUE arf_flip(VALUE self){ - return Qnil; -} - -static VALUE arf_lower(VALUE self){ - return Qnil; -} - -static VALUE arf_upper(VALUE self){ - return Qnil; -} - -static VALUE arf_select(VALUE self){ - return Qnil; -} - -static VALUE arf_select_scalar_r(VALUE self){ - return Qnil; -} - -static VALUE arf_select_scalar_l(VALUE self){ - return Qnil; -} - -static VALUE arf_replace(VALUE self){ - return Qnil; -} - -static VALUE arf_replace_scalar(VALUE self){ - return Qnil; -} - +#include "cmodules/data.c" #include "cmodules/lapack.c" // Random diff --git a/ext/mri/cmodules/data.c b/ext/mri/cmodules/data.c new file mode 100644 index 0000000..217a300 --- /dev/null +++ b/ext/mri/cmodules/data.c @@ -0,0 +1,95 @@ +static VALUE arf_constant(VALUE self){ + return Qnil; +} + +static VALUE arf_constant_complex(VALUE self){ + return Qnil; +} + +static VALUE arf_constant_long(VALUE self){ + return Qnil; +} + +static VALUE arf_constant_ulong(VALUE self){ + return Qnil; +} + +static VALUE arf_range(VALUE self){ + return Qnil; +} + +static VALUE arf_iota(VALUE self){ + return Qnil; +} + +static VALUE arf_identity(VALUE self){ + return Qnil; +} + +static VALUE arf_diag_create(VALUE self){ + return Qnil; +} + +static VALUE arf_diag_extract(VALUE self){ + return Qnil; +} + +static VALUE arf_join(VALUE self){ + return Qnil; +} + +static VALUE arf_join_many(VALUE self){ + return Qnil; +} + +static VALUE arf_tile(VALUE self){ + return Qnil; +} + +static VALUE arf_reorder(VALUE self){ + return Qnil; +} + +static VALUE arf_shift(VALUE self){ + return Qnil; +} + +static VALUE arf_moddims(VALUE self){ + return Qnil; +} + +static VALUE arf_flat(VALUE self){ + return Qnil; +} + +static VALUE arf_flip(VALUE self){ + return Qnil; +} + +static VALUE arf_lower(VALUE self){ + return Qnil; +} + +static VALUE arf_upper(VALUE self){ + return Qnil; +} + +static VALUE arf_select(VALUE self){ + return Qnil; +} + +static VALUE arf_select_scalar_r(VALUE self){ + return Qnil; +} + +static VALUE arf_select_scalar_l(VALUE self){ + return Qnil; +} + +static VALUE arf_replace(VALUE self){ + return Qnil; +} + +static VALUE arf_replace_scalar(VALUE self){ + return Qnil; +} \ No newline at end of file From a35f2f469d8f957af8193b3cd8b0b92c3c0d3fec Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Thu, 29 Jun 2017 10:37:56 +0530 Subject: [PATCH 040/125] move random functions to random.c --- ext/mri/arrayfire.c | 49 +-------------------------------------- ext/mri/cmodules/random.c | 45 +++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 48 deletions(-) create mode 100644 ext/mri/cmodules/random.c diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 09834dd..b602f94 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -694,51 +694,4 @@ static VALUE arf_get_platform(VALUE self){ #include "cmodules/data.c" #include "cmodules/lapack.c" - -// Random - -static VALUE arf_create_random_engine(VALUE self){ - return Qnil; -} -static VALUE arf_retain_random_engine(VALUE self){ - return Qnil; -} -static VALUE arf_random_engine_set_type(VALUE self){ - return Qnil; -} -static VALUE arf_random_engine_get_type(VALUE self){ - return Qnil; -} -static VALUE arf_random_uniform(VALUE self){ - return Qnil; -} -static VALUE arf_random_normal(VALUE self){ - return Qnil; -} -static VALUE arf_random_engine_set_seed(VALUE self){ - return Qnil; -} -static VALUE arf_get_default_random_engine(VALUE self){ - return Qnil; -} -static VALUE arf_set_default_random_engine_type(VALUE self){ - return Qnil; -} -static VALUE arf_random_engine_get_seed(VALUE self){ - return Qnil; -} -static VALUE arf_release_random_engine(VALUE self){ - return Qnil; -} -static VALUE arf_randu(VALUE self){ - return Qnil; -} -static VALUE arf_randn(VALUE self){ - return Qnil; -} -static VALUE arf_set_seed(VALUE self){ - return Qnil; -} -static VALUE arf_get_seed(VALUE self){ - return Qnil; -} +#include "cmodules/random.c" diff --git a/ext/mri/cmodules/random.c b/ext/mri/cmodules/random.c new file mode 100644 index 0000000..0da4249 --- /dev/null +++ b/ext/mri/cmodules/random.c @@ -0,0 +1,45 @@ +static VALUE arf_create_random_engine(VALUE self){ + return Qnil; +} +static VALUE arf_retain_random_engine(VALUE self){ + return Qnil; +} +static VALUE arf_random_engine_set_type(VALUE self){ + return Qnil; +} +static VALUE arf_random_engine_get_type(VALUE self){ + return Qnil; +} +static VALUE arf_random_uniform(VALUE self){ + return Qnil; +} +static VALUE arf_random_normal(VALUE self){ + return Qnil; +} +static VALUE arf_random_engine_set_seed(VALUE self){ + return Qnil; +} +static VALUE arf_get_default_random_engine(VALUE self){ + return Qnil; +} +static VALUE arf_set_default_random_engine_type(VALUE self){ + return Qnil; +} +static VALUE arf_random_engine_get_seed(VALUE self){ + return Qnil; +} +static VALUE arf_release_random_engine(VALUE self){ + return Qnil; +} +static VALUE arf_randu(VALUE self){ + return Qnil; +} +static VALUE arf_randn(VALUE self){ + return Qnil; +} +static VALUE arf_set_seed(VALUE self){ + return Qnil; +} +static VALUE arf_get_seed(VALUE self){ + return Qnil; +} From 1b49497eaa22991350599d68dd186db77583808e Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Thu, 29 Jun 2017 10:40:05 +0530 Subject: [PATCH 041/125] move opencl functions to opencl.c --- ext/mri/arrayfire.c | 40 +-------------------------------------- ext/mri/cmodules/opencl.c | 35 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 39 deletions(-) create mode 100644 ext/mri/cmodules/opencl.c diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index b602f94..9a2a15d 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -653,45 +653,7 @@ static VALUE arf_get_device_ptr(VALUE self){ return Qnil; } -// OpenCL - - -static VALUE arf_get_context(VALUE self){ - return Qnil; -} - -static VALUE arf_get_queue(VALUE self){ - return Qnil; -} - -static VALUE arf_get_device_id(VALUE self){ - return Qnil; -} - -static VALUE arf_set_device_id(VALUE self){ - return Qnil; -} - -static VALUE arf_add_device_context(VALUE self){ - return Qnil; -} - -static VALUE arf_set_device_context(VALUE self){ - return Qnil; -} - -static VALUE arf_delete_device_context(VALUE self){ - return Qnil; -} - -static VALUE arf_get_device_type(VALUE self){ - return Qnil; -} - -static VALUE arf_get_platform(VALUE self){ - return Qnil; -} - +#include "cmodules/opencl.c" #include "cmodules/data.c" #include "cmodules/lapack.c" #include "cmodules/random.c" diff --git a/ext/mri/cmodules/opencl.c b/ext/mri/cmodules/opencl.c new file mode 100644 index 0000000..fc73c02 --- /dev/null +++ b/ext/mri/cmodules/opencl.c @@ -0,0 +1,35 @@ +static VALUE arf_get_context(VALUE self){ + return Qnil; +} + +static VALUE arf_get_queue(VALUE self){ + return Qnil; +} + +static VALUE arf_get_device_id(VALUE self){ + return Qnil; +} + +static VALUE arf_set_device_id(VALUE self){ + return Qnil; +} + +static VALUE arf_add_device_context(VALUE self){ + return Qnil; +} + +static VALUE arf_set_device_context(VALUE self){ + return Qnil; +} + +static VALUE arf_delete_device_context(VALUE self){ + return Qnil; +} + +static VALUE arf_get_device_type(VALUE self){ + return Qnil; +} + +static VALUE arf_get_platform(VALUE self){ + return Qnil; +} From 711f46cdc4b59ac3d08d268d54787ae0c7d81ebe Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Thu, 29 Jun 2017 10:42:13 +0530 Subject: [PATCH 042/125] move device functions to device.c --- ext/mri/arrayfire.c | 110 +------------------------------------- ext/mri/cmodules/device.c | 108 +++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 109 deletions(-) create mode 100644 ext/mri/cmodules/device.c diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 9a2a15d..e825061 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -544,115 +544,7 @@ static VALUE arf_set_native_id(VALUE self){ // Device -static VALUE arf_info(VALUE self){ - af_info(); - return Qnil; -} - -static VALUE arf_init2(VALUE self){ - return Qnil; -} - -static VALUE arf_info_string(VALUE self){ - return Qnil; -} - -static VALUE arf_device_info(VALUE self){ - return Qnil; -} - -static VALUE arf_get_device_count(VALUE self){ - return Qnil; -} - -static VALUE arf_get_dbl_support(VALUE self){ - return Qnil; -} - -static VALUE arf_set_device(VALUE self){ - return Qnil; -} - -static VALUE arf_get_device(VALUE self){ - return Qnil; -} - -static VALUE arf_sync(VALUE self){ - return Qnil; -} - -static VALUE arf_alloc_device(VALUE self){ - return Qnil; -} - -static VALUE arf_free_device(VALUE self){ - return Qnil; -} - -static VALUE arf_alloc_pinned(VALUE self){ - return Qnil; -} - -static VALUE arf_free_pinned(VALUE self){ - return Qnil; -} - -static VALUE arf_alloc_host(VALUE self){ - return Qnil; -} - -static VALUE arf_free_host(VALUE self){ - return Qnil; -} - -static VALUE arf_device_array(VALUE self){ - return Qnil; -} - -static VALUE arf_device_mem_info(VALUE self){ - return Qnil; -} - -static VALUE arf_print_mem_info(VALUE self){ - return Qnil; -} - -static VALUE arf_device_gc(VALUE self){ - return Qnil; -} - -static VALUE arf_set_mem_step_size(VALUE self){ - return Qnil; -} - -static VALUE arf_get_mem_step_size(VALUE self){ - return Qnil; -} - -static VALUE arf_lock_device_ptr(VALUE self){ - return Qnil; -} - -static VALUE arf_unlock_device_ptr(VALUE self){ - return Qnil; -} - -static VALUE arf_lock_array(VALUE self){ - return Qnil; -} - -static VALUE arf_unlock_array(VALUE self){ - return Qnil; -} - -static VALUE arf_is_locked_array(VALUE self){ - return Qnil; -} - -static VALUE arf_get_device_ptr(VALUE self){ - return Qnil; -} - +#include "cmodules/device.c" #include "cmodules/opencl.c" #include "cmodules/data.c" #include "cmodules/lapack.c" diff --git a/ext/mri/cmodules/device.c b/ext/mri/cmodules/device.c new file mode 100644 index 0000000..bc4b4b3 --- /dev/null +++ b/ext/mri/cmodules/device.c @@ -0,0 +1,108 @@ +static VALUE arf_info(VALUE self){ + af_info(); + return Qnil; +} + +static VALUE arf_init2(VALUE self){ + return Qnil; +} + +static VALUE arf_info_string(VALUE self){ + return Qnil; +} + +static VALUE arf_device_info(VALUE self){ + return Qnil; +} + +static VALUE arf_get_device_count(VALUE self){ + return Qnil; +} + +static VALUE arf_get_dbl_support(VALUE self){ + return Qnil; +} + +static VALUE arf_set_device(VALUE self){ + return Qnil; +} + +static VALUE arf_get_device(VALUE self){ + return Qnil; +} + +static VALUE arf_sync(VALUE self){ + return Qnil; +} + +static VALUE arf_alloc_device(VALUE self){ + return Qnil; +} + +static VALUE arf_free_device(VALUE self){ + return Qnil; +} + +static VALUE arf_alloc_pinned(VALUE self){ + return Qnil; +} + +static VALUE arf_free_pinned(VALUE self){ + return Qnil; +} + +static VALUE arf_alloc_host(VALUE self){ + return Qnil; +} + +static VALUE arf_free_host(VALUE self){ + return Qnil; +} + +static VALUE arf_device_array(VALUE self){ + return Qnil; +} + +static VALUE arf_device_mem_info(VALUE self){ + return Qnil; +} + +static VALUE arf_print_mem_info(VALUE self){ + return Qnil; +} + +static VALUE arf_device_gc(VALUE self){ + return Qnil; +} + +static VALUE arf_set_mem_step_size(VALUE self){ + return Qnil; +} + +static VALUE arf_get_mem_step_size(VALUE self){ + return Qnil; +} + +static VALUE arf_lock_device_ptr(VALUE self){ + return Qnil; +} + +static VALUE arf_unlock_device_ptr(VALUE self){ + return Qnil; +} + +static VALUE arf_lock_array(VALUE self){ + return Qnil; +} + +static VALUE arf_unlock_array(VALUE self){ + return Qnil; +} + +static VALUE arf_is_locked_array(VALUE self){ + return Qnil; +} + +static VALUE arf_get_device_ptr(VALUE self){ + return Qnil; +} From 69505240cfd124eec647c3cc2fb84f812f7ff4d4 Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Thu, 29 Jun 2017 10:43:41 +0530 Subject: [PATCH 043/125] move cuda functions to cuda.c --- ext/mri/arrayfire.c | 18 +----------------- ext/mri/cmodules/cuda.c | 11 +++++++++++ 2 files changed, 12 insertions(+), 17 deletions(-) create mode 100644 ext/mri/cmodules/cuda.c diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index e825061..73a67a9 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -527,23 +527,7 @@ static VALUE arf_get_backend_device_id(VALUE self){ } #include "cmodules/blas.c" - -// CUDA - -static VALUE arf_get_stream(VALUE self){ - return Qnil; -} - -static VALUE arf_get_native_id(VALUE self){ - return Qnil; -} - -static VALUE arf_set_native_id(VALUE self){ - return Qnil; -} - -// Device - +#include "cmodules/cuda.c" #include "cmodules/device.c" #include "cmodules/opencl.c" #include "cmodules/data.c" diff --git a/ext/mri/cmodules/cuda.c b/ext/mri/cmodules/cuda.c new file mode 100644 index 0000000..9a3265a --- /dev/null +++ b/ext/mri/cmodules/cuda.c @@ -0,0 +1,11 @@ +static VALUE arf_get_stream(VALUE self){ + return Qnil; +} + +static VALUE arf_get_native_id(VALUE self){ + return Qnil; +} + +static VALUE arf_set_native_id(VALUE self){ + return Qnil; +} From d941e7db33649f27e132ab96a77ae0e560d5a786 Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Thu, 29 Jun 2017 10:45:51 +0530 Subject: [PATCH 044/125] move backend functions to backend.c --- ext/mri/arrayfire.c | 26 +------------------------- ext/mri/cmodules/backend.c | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 25 deletions(-) create mode 100644 ext/mri/cmodules/backend.c diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 73a67a9..503ee56 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -500,32 +500,8 @@ DEF_UNARY_RUBY_ACCESSOR(tgamma, tgamma) DEF_UNARY_RUBY_ACCESSOR(floor, floor) DEF_UNARY_RUBY_ACCESSOR(ceil, ceil) -// Algorithm - #include "cmodules/algorithm.c" - -// Backend - -static VALUE arf_get_backend_count(VALUE self){ - return Qnil; -} - -static VALUE arf_get_available_backends(VALUE self){ - return Qnil; -} - -static VALUE arf_get_backend_id(VALUE self){ - return Qnil; -} - -static VALUE arf_get_active_backend(VALUE self){ - return Qnil; -} - -static VALUE arf_get_backend_device_id(VALUE self){ - return Qnil; -} - +#include "cmodules/backend.c" #include "cmodules/blas.c" #include "cmodules/cuda.c" #include "cmodules/device.c" diff --git a/ext/mri/cmodules/backend.c b/ext/mri/cmodules/backend.c new file mode 100644 index 0000000..f2840f2 --- /dev/null +++ b/ext/mri/cmodules/backend.c @@ -0,0 +1,19 @@ +static VALUE arf_get_backend_count(VALUE self){ + return Qnil; +} + +static VALUE arf_get_available_backends(VALUE self){ + return Qnil; +} + +static VALUE arf_get_backend_id(VALUE self){ + return Qnil; +} + +static VALUE arf_get_active_backend(VALUE self){ + return Qnil; +} + +static VALUE arf_get_backend_device_id(VALUE self){ + return Qnil; +} From 7de74924fc0401589e49f6e91919ddfce623e689 Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Thu, 29 Jun 2017 11:34:58 +0530 Subject: [PATCH 045/125] constant: implement Data#constant method --- ext/mri/arrayfire.c | 4 ++-- ext/mri/cmodules/data.c | 18 ++++++++++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 503ee56..b7e2afb 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -108,7 +108,7 @@ static VALUE arf_delete_device_context(VALUE self); static VALUE arf_get_device_type(VALUE self); static VALUE arf_get_platform(VALUE self); -static VALUE arf_constant(VALUE self); +static VALUE arf_constant(int argc, VALUE* argv); static VALUE arf_constant_complex(VALUE self); static VALUE arf_constant_long(VALUE self); static VALUE arf_constant_ulong(VALUE self); @@ -366,7 +366,7 @@ void Init_arrayfire() { rb_define_singleton_method(OpenCL, "get_platform", (METHOD)arf_get_platform, 0); Data = rb_define_class_under(ArrayFire, "Data", rb_cObject); - rb_define_singleton_method(Data, "constant", (METHOD)arf_constant, 0); + rb_define_singleton_method(Data, "constant", (METHOD)arf_constant, -1); rb_define_singleton_method(Data, "constant_complex", (METHOD)arf_constant_complex, 0); rb_define_singleton_method(Data, "constant_long", (METHOD)arf_constant_long, 0); rb_define_singleton_method(Data, "constant_ulong", (METHOD)arf_constant_ulong, 0); diff --git a/ext/mri/cmodules/data.c b/ext/mri/cmodules/data.c index 217a300..8953dc1 100644 --- a/ext/mri/cmodules/data.c +++ b/ext/mri/cmodules/data.c @@ -1,5 +1,19 @@ -static VALUE arf_constant(VALUE self){ - return Qnil; +static VALUE arf_constant(int argc, VALUE* argv) +{ + afstruct* output = ALLOC(afstruct); + + dim_t ndims = (dim_t)FIX2LONG(argv[0]); + dim_t* dimensions = (dim_t*)malloc(ndims * sizeof(dim_t)); + dim_t count = 1; + for (size_t index = 0; index < ndims; index++) { + dimensions[index] = (dim_t)FIX2LONG(RARRAY_AREF(argv[1], index)); + count *= dimensions[index]; + } + float data = NUM2DBL(argv[2]); + af_constant(&output->carray, data, 2, dimensions, f32); + af_print_array(output->carray); + + return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } static VALUE arf_constant_complex(VALUE self){ From c9402580a32b983ef6db54a5083443635fc081fa Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Thu, 29 Jun 2017 12:03:39 +0530 Subject: [PATCH 046/125] constant: implement Data#constant_long and Data#constant_ulong method --- ext/mri/arrayfire.c | 8 ++++---- ext/mri/cmodules/data.c | 37 +++++++++++++++++++++++++++++++------ 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index b7e2afb..d905850 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -110,8 +110,8 @@ static VALUE arf_get_platform(VALUE self); static VALUE arf_constant(int argc, VALUE* argv); static VALUE arf_constant_complex(VALUE self); -static VALUE arf_constant_long(VALUE self); -static VALUE arf_constant_ulong(VALUE self); +static VALUE arf_constant_long(int argc, VALUE* argv); +static VALUE arf_constant_ulong(int argc, VALUE* argv); static VALUE arf_range(VALUE self); static VALUE arf_iota(VALUE self); static VALUE arf_identity(VALUE self); @@ -368,8 +368,8 @@ void Init_arrayfire() { Data = rb_define_class_under(ArrayFire, "Data", rb_cObject); rb_define_singleton_method(Data, "constant", (METHOD)arf_constant, -1); rb_define_singleton_method(Data, "constant_complex", (METHOD)arf_constant_complex, 0); - rb_define_singleton_method(Data, "constant_long", (METHOD)arf_constant_long, 0); - rb_define_singleton_method(Data, "constant_ulong", (METHOD)arf_constant_ulong, 0); + rb_define_singleton_method(Data, "constant_long", (METHOD)arf_constant_long, -1); + rb_define_singleton_method(Data, "constant_ulong", (METHOD)arf_constant_ulong, -1); rb_define_singleton_method(Data, "range", (METHOD)arf_range, 0); rb_define_singleton_method(Data, "iota", (METHOD)arf_iota, 0); rb_define_singleton_method(Data, "identity", (METHOD)arf_identity, 0); diff --git a/ext/mri/cmodules/data.c b/ext/mri/cmodules/data.c index 8953dc1..0c6601d 100644 --- a/ext/mri/cmodules/data.c +++ b/ext/mri/cmodules/data.c @@ -1,5 +1,4 @@ -static VALUE arf_constant(int argc, VALUE* argv) -{ +static VALUE arf_constant(int argc, VALUE* argv){ afstruct* output = ALLOC(afstruct); dim_t ndims = (dim_t)FIX2LONG(argv[0]); @@ -20,12 +19,38 @@ static VALUE arf_constant_complex(VALUE self){ return Qnil; } -static VALUE arf_constant_long(VALUE self){ - return Qnil; +static VALUE arf_constant_long(int argc, VALUE* argv){ + afstruct* output = ALLOC(afstruct); + + dim_t ndims = (dim_t)FIX2LONG(argv[0]); + dim_t* dimensions = (dim_t*)malloc(ndims * sizeof(dim_t)); + dim_t count = 1; + for (size_t index = 0; index < ndims; index++) { + dimensions[index] = (dim_t)FIX2LONG(RARRAY_AREF(argv[1], index)); + count *= dimensions[index]; + } + float data = NUM2LONG(argv[2]); + af_constant_long(&output->carray, data, 2, dimensions); + af_print_array(output->carray); + + return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } -static VALUE arf_constant_ulong(VALUE self){ - return Qnil; +static VALUE arf_constant_ulong(int argc, VALUE* argv){ + afstruct* output = ALLOC(afstruct); + + dim_t ndims = (dim_t)FIX2LONG(argv[0]); + dim_t* dimensions = (dim_t*)malloc(ndims * sizeof(dim_t)); + dim_t count = 1; + for (size_t index = 0; index < ndims; index++) { + dimensions[index] = (dim_t)FIX2LONG(RARRAY_AREF(argv[1], index)); + count *= dimensions[index]; + } + float data = NUM2LONG(argv[2]); + af_constant_ulong(&output->carray, data, 2, dimensions); + af_print_array(output->carray); + + return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } static VALUE arf_range(VALUE self){ From 7161297187e1c0791d5879a697c2e9e22216aef5 Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Thu, 29 Jun 2017 16:10:31 +0530 Subject: [PATCH 047/125] constant: implement Data#range and Data#identity method --- ext/mri/arrayfire.c | 8 ++++---- ext/mri/cmodules/data.c | 33 +++++++++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index d905850..30dbc8b 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -112,9 +112,9 @@ static VALUE arf_constant(int argc, VALUE* argv); static VALUE arf_constant_complex(VALUE self); static VALUE arf_constant_long(int argc, VALUE* argv); static VALUE arf_constant_ulong(int argc, VALUE* argv); -static VALUE arf_range(VALUE self); +static VALUE arf_range(int argc, VALUE* argv); static VALUE arf_iota(VALUE self); -static VALUE arf_identity(VALUE self); +static VALUE arf_identity(int argc, VALUE* argv); static VALUE arf_diag_create(VALUE self); static VALUE arf_diag_extract(VALUE self); static VALUE arf_join(VALUE self); @@ -370,9 +370,9 @@ void Init_arrayfire() { rb_define_singleton_method(Data, "constant_complex", (METHOD)arf_constant_complex, 0); rb_define_singleton_method(Data, "constant_long", (METHOD)arf_constant_long, -1); rb_define_singleton_method(Data, "constant_ulong", (METHOD)arf_constant_ulong, -1); - rb_define_singleton_method(Data, "range", (METHOD)arf_range, 0); + rb_define_singleton_method(Data, "range", (METHOD)arf_range, -1); rb_define_singleton_method(Data, "iota", (METHOD)arf_iota, 0); - rb_define_singleton_method(Data, "identity", (METHOD)arf_identity, 0); + rb_define_singleton_method(Data, "identity", (METHOD)arf_identity, -1); rb_define_singleton_method(Data, "diag_create", (METHOD)arf_diag_create, 0); rb_define_singleton_method(Data, "diag_extract", (METHOD)arf_diag_extract, 0); rb_define_singleton_method(Data, "join", (METHOD)arf_join, 0); diff --git a/ext/mri/cmodules/data.c b/ext/mri/cmodules/data.c index 0c6601d..07cdcc4 100644 --- a/ext/mri/cmodules/data.c +++ b/ext/mri/cmodules/data.c @@ -53,16 +53,41 @@ static VALUE arf_constant_ulong(int argc, VALUE* argv){ return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } -static VALUE arf_range(VALUE self){ - return Qnil; +static VALUE arf_range(int argc, VALUE* argv){ + afstruct* output = ALLOC(afstruct); + + dim_t ndims = (dim_t)FIX2LONG(argv[0]); + dim_t* dimensions = (dim_t*)malloc(ndims * sizeof(dim_t)); + dim_t count = 1; + for (size_t index = 0; index < ndims; index++) { + dimensions[index] = (dim_t)FIX2LONG(RARRAY_AREF(argv[1], index)); + count *= dimensions[index]; + } + int seq_dim = NUM2INT(argv[2]); + af_range(&output->carray, ndims, dimensions, seq_dim, f32); + af_print_array(output->carray); + + return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } static VALUE arf_iota(VALUE self){ return Qnil; } -static VALUE arf_identity(VALUE self){ - return Qnil; +static VALUE arf_identity(int argc, VALUE* argv){ + afstruct* output = ALLOC(afstruct); + + dim_t ndims = (dim_t)FIX2LONG(argv[0]); + dim_t* dimensions = (dim_t*)malloc(ndims * sizeof(dim_t)); + dim_t count = 1; + for (size_t index = 0; index < ndims; index++) { + dimensions[index] = (dim_t)FIX2LONG(RARRAY_AREF(argv[1], index)); + count *= dimensions[index]; + } + af_identity(&output->carray, ndims, dimensions, f32); + af_print_array(output->carray); + + return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } static VALUE arf_diag_create(VALUE self){ From afd245df408f07b285efcbca39a50f4541e8833c Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Thu, 29 Jun 2017 16:19:24 +0530 Subject: [PATCH 048/125] constant: implement Data#diag_create and Data#diag_extract --- ext/mri/arrayfire.c | 8 ++++---- ext/mri/cmodules/data.c | 24 ++++++++++++++++++++---- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 30dbc8b..559657b 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -115,8 +115,8 @@ static VALUE arf_constant_ulong(int argc, VALUE* argv); static VALUE arf_range(int argc, VALUE* argv); static VALUE arf_iota(VALUE self); static VALUE arf_identity(int argc, VALUE* argv); -static VALUE arf_diag_create(VALUE self); -static VALUE arf_diag_extract(VALUE self); +static VALUE arf_diag_create(VALUE self, VALUE array_val, VALUE num_val) +static VALUE arf_diag_extract(VALUE self, VALUE array_val, VALUE num_val) static VALUE arf_join(VALUE self); static VALUE arf_join_many(VALUE self); static VALUE arf_tile(VALUE self); @@ -373,8 +373,8 @@ void Init_arrayfire() { rb_define_singleton_method(Data, "range", (METHOD)arf_range, -1); rb_define_singleton_method(Data, "iota", (METHOD)arf_iota, 0); rb_define_singleton_method(Data, "identity", (METHOD)arf_identity, -1); - rb_define_singleton_method(Data, "diag_create", (METHOD)arf_diag_create, 0); - rb_define_singleton_method(Data, "diag_extract", (METHOD)arf_diag_extract, 0); + rb_define_singleton_method(Data, "diag_create", (METHOD)arf_diag_create, 2); + rb_define_singleton_method(Data, "diag_extract", (METHOD)arf_diag_extract, 2); rb_define_singleton_method(Data, "join", (METHOD)arf_join, 0); rb_define_singleton_method(Data, "join_many", (METHOD)arf_join_many, 0); rb_define_singleton_method(Data, "tile", (METHOD)arf_tile, 0); diff --git a/ext/mri/cmodules/data.c b/ext/mri/cmodules/data.c index 07cdcc4..415268b 100644 --- a/ext/mri/cmodules/data.c +++ b/ext/mri/cmodules/data.c @@ -90,12 +90,28 @@ static VALUE arf_identity(int argc, VALUE* argv){ return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } -static VALUE arf_diag_create(VALUE self){ - return Qnil; +static VALUE arf_diag_create(VALUE self, VALUE array_val, VALUE num_val){ + afstruct* input; + afstruct* output = ALLOC(afstruct); + + Data_Get_Struct(array_val, afstruct, input); + + af_diag_create(&output->carray, input->carray, NUM2INT(num)); + af_print_array(output->carray); + + return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } -static VALUE arf_diag_extract(VALUE self){ - return Qnil; +static VALUE arf_diag_extract(VALUE self, VALUE array_val, VALUE num_val){ + afstruct* input; + afstruct* output = ALLOC(afstruct); + + Data_Get_Struct(array_val, afstruct, input); + + af_diag_extract(&output->carray, input->carray, NUM2INT(num)); + af_print_array(output->carray); + + return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } static VALUE arf_join(VALUE self){ From b3a478fdb5a58b9a93d675154afb491edb3bc9ef Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Thu, 29 Jun 2017 16:42:17 +0530 Subject: [PATCH 049/125] constant: implement Data methods join, tile, reorder, shift --- ext/mri/arrayfire.c | 20 +++++++-------- ext/mri/cmodules/data.c | 56 +++++++++++++++++++++++++++++++++-------- 2 files changed, 55 insertions(+), 21 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 559657b..2e07e1a 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -115,13 +115,13 @@ static VALUE arf_constant_ulong(int argc, VALUE* argv); static VALUE arf_range(int argc, VALUE* argv); static VALUE arf_iota(VALUE self); static VALUE arf_identity(int argc, VALUE* argv); -static VALUE arf_diag_create(VALUE self, VALUE array_val, VALUE num_val) -static VALUE arf_diag_extract(VALUE self, VALUE array_val, VALUE num_val) -static VALUE arf_join(VALUE self); +static VALUE arf_diag_create(VALUE self, VALUE array_val, VALUE num_val); +static VALUE arf_diag_extract(VALUE self, VALUE array_val, VALUE num_val); +static VALUE arf_join(VALUE self, VALUE dim_val, VALUE first_array_val, VALUE second_array_val); static VALUE arf_join_many(VALUE self); -static VALUE arf_tile(VALUE self); -static VALUE arf_reorder(VALUE self); -static VALUE arf_shift(VALUE self); +static VALUE arf_tile(VALUE self, VALUE array_val, VALUE x_val, VALUE y_val, VALUE z_val, VALUE w_val); +static VALUE arf_reorder(VALUE self, VALUE array_val, VALUE x_val, VALUE y_val, VALUE z_val, VALUE w_val); +static VALUE arf_shift(VALUE self, VALUE array_val, VALUE x_val, VALUE y_val, VALUE z_val, VALUE w_val); static VALUE arf_moddims(VALUE self); static VALUE arf_flat(VALUE self); static VALUE arf_flip(VALUE self); @@ -375,11 +375,11 @@ void Init_arrayfire() { rb_define_singleton_method(Data, "identity", (METHOD)arf_identity, -1); rb_define_singleton_method(Data, "diag_create", (METHOD)arf_diag_create, 2); rb_define_singleton_method(Data, "diag_extract", (METHOD)arf_diag_extract, 2); - rb_define_singleton_method(Data, "join", (METHOD)arf_join, 0); + rb_define_singleton_method(Data, "join", (METHOD)arf_join, 3); rb_define_singleton_method(Data, "join_many", (METHOD)arf_join_many, 0); - rb_define_singleton_method(Data, "tile", (METHOD)arf_tile, 0); - rb_define_singleton_method(Data, "reorder", (METHOD)arf_reorder, 0); - rb_define_singleton_method(Data, "shift", (METHOD)arf_shift, 0); + rb_define_singleton_method(Data, "tile", (METHOD)arf_tile, 5); + rb_define_singleton_method(Data, "reorder", (METHOD)arf_reorder, 5); + rb_define_singleton_method(Data, "shift", (METHOD)arf_shift, 5); rb_define_singleton_method(Data, "moddims", (METHOD)arf_moddims, 0); rb_define_singleton_method(Data, "flat", (METHOD)arf_flat, 0); rb_define_singleton_method(Data, "flip", (METHOD)arf_flip, 0); diff --git a/ext/mri/cmodules/data.c b/ext/mri/cmodules/data.c index 415268b..353d105 100644 --- a/ext/mri/cmodules/data.c +++ b/ext/mri/cmodules/data.c @@ -96,7 +96,7 @@ static VALUE arf_diag_create(VALUE self, VALUE array_val, VALUE num_val){ Data_Get_Struct(array_val, afstruct, input); - af_diag_create(&output->carray, input->carray, NUM2INT(num)); + af_diag_create(&output->carray, input->carray, NUM2INT(num_val)); af_print_array(output->carray); return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); @@ -108,30 +108,64 @@ static VALUE arf_diag_extract(VALUE self, VALUE array_val, VALUE num_val){ Data_Get_Struct(array_val, afstruct, input); - af_diag_extract(&output->carray, input->carray, NUM2INT(num)); + af_diag_extract(&output->carray, input->carray, NUM2INT(num_val)); af_print_array(output->carray); return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } -static VALUE arf_join(VALUE self){ - return Qnil; +static VALUE arf_join(VALUE self, VALUE dim_val, VALUE first_array_val, VALUE second_array_val){ + afstruct* first_array; + afstruct* second_array; + afstruct* output = ALLOC(afstruct); + + Data_Get_Struct(first_array_val, afstruct, first_array); + Data_Get_Struct(second_array_val, afstruct, second_array); + + af_join(&output->carray, NUM2INT(dim_val), first_array->carray, second_array->carray); + af_print_array(output->carray); + + return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } static VALUE arf_join_many(VALUE self){ return Qnil; } -static VALUE arf_tile(VALUE self){ - return Qnil; +static VALUE arf_tile(VALUE self, VALUE array_val, VALUE x_val, VALUE y_val, VALUE z_val, VALUE w_val){ + afstruct* input; + afstruct* output = ALLOC(afstruct); + + Data_Get_Struct(array_val, afstruct, input); + + af_tile(&output->carray, input->carray, NUM2UINT(x_val), NUM2UINT(y_val), NUM2UINT(z_val), NUM2UINT(w_val)); + af_print_array(output->carray); + + return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } -static VALUE arf_reorder(VALUE self){ - return Qnil; +static VALUE arf_reorder(VALUE self, VALUE array_val, VALUE x_val, VALUE y_val, VALUE z_val, VALUE w_val){ + afstruct* input; + afstruct* output = ALLOC(afstruct); + + Data_Get_Struct(array_val, afstruct, input); + + af_reorder(&output->carray, input->carray, NUM2UINT(x_val), NUM2UINT(y_val), NUM2UINT(z_val), NUM2UINT(w_val)); + af_print_array(output->carray); + + return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } -static VALUE arf_shift(VALUE self){ - return Qnil; +static VALUE arf_shift(VALUE self, VALUE array_val, VALUE x_val, VALUE y_val, VALUE z_val, VALUE w_val){ + afstruct* input; + afstruct* output = ALLOC(afstruct); + + Data_Get_Struct(array_val, afstruct, input); + + af_shift(&output->carray, input->carray, NUM2INT(x_val), NUM2INT(y_val), NUM2INT(z_val), NUM2INT(w_val)); + af_print_array(output->carray); + + return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } static VALUE arf_moddims(VALUE self){ @@ -172,4 +206,4 @@ static VALUE arf_replace(VALUE self){ static VALUE arf_replace_scalar(VALUE self){ return Qnil; -} \ No newline at end of file +} From a2cd98cf7eb2f9a1349104cd0d7fd88036b6e928 Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Thu, 29 Jun 2017 17:01:39 +0530 Subject: [PATCH 050/125] constant: implement Data methods flat and flip --- ext/mri/arrayfire.c | 8 ++++---- ext/mri/cmodules/data.c | 24 ++++++++++++++++++++---- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 2e07e1a..41b6f79 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -123,8 +123,8 @@ static VALUE arf_tile(VALUE self, VALUE array_val, VALUE x_val, VALUE y_val, VAL static VALUE arf_reorder(VALUE self, VALUE array_val, VALUE x_val, VALUE y_val, VALUE z_val, VALUE w_val); static VALUE arf_shift(VALUE self, VALUE array_val, VALUE x_val, VALUE y_val, VALUE z_val, VALUE w_val); static VALUE arf_moddims(VALUE self); -static VALUE arf_flat(VALUE self); -static VALUE arf_flip(VALUE self); +static VALUE arf_flat(VALUE self, VALUE array_val); +static VALUE arf_flip(VALUE self, VALUE array_val, VALUE dim_val); static VALUE arf_lower(VALUE self); static VALUE arf_upper(VALUE self); static VALUE arf_select(VALUE self); @@ -381,8 +381,8 @@ void Init_arrayfire() { rb_define_singleton_method(Data, "reorder", (METHOD)arf_reorder, 5); rb_define_singleton_method(Data, "shift", (METHOD)arf_shift, 5); rb_define_singleton_method(Data, "moddims", (METHOD)arf_moddims, 0); - rb_define_singleton_method(Data, "flat", (METHOD)arf_flat, 0); - rb_define_singleton_method(Data, "flip", (METHOD)arf_flip, 0); + rb_define_singleton_method(Data, "flat", (METHOD)arf_flat, 1); + rb_define_singleton_method(Data, "flip", (METHOD)arf_flip, 2); rb_define_singleton_method(Data, "lower", (METHOD)arf_lower, 0); rb_define_singleton_method(Data, "upper", (METHOD)arf_upper, 0); rb_define_singleton_method(Data, "select", (METHOD)arf_select, 0); diff --git a/ext/mri/cmodules/data.c b/ext/mri/cmodules/data.c index 353d105..3b21a54 100644 --- a/ext/mri/cmodules/data.c +++ b/ext/mri/cmodules/data.c @@ -172,12 +172,28 @@ static VALUE arf_moddims(VALUE self){ return Qnil; } -static VALUE arf_flat(VALUE self){ - return Qnil; +static VALUE arf_flat(VALUE self, VALUE array_val){ + afstruct* input; + afstruct* output = ALLOC(afstruct); + + Data_Get_Struct(array_val, afstruct, input); + + af_flat(&output->carray, input->carray); + af_print_array(output->carray); + + return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } -static VALUE arf_flip(VALUE self){ - return Qnil; +static VALUE arf_flip(VALUE self, VALUE array_val, VALUE dim_val){ + afstruct* input; + afstruct* output = ALLOC(afstruct); + + Data_Get_Struct(array_val, afstruct, input); + + af_flip(&output->carray, input->carray, NUM2UINT(dim_val)); + af_print_array(output->carray); + + return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } static VALUE arf_lower(VALUE self){ From 31670dac08d5a0c0f46160c4b522e6d809b7c065 Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Thu, 29 Jun 2017 21:29:03 +0530 Subject: [PATCH 051/125] constant: implement Data select methods --- ext/mri/arrayfire.c | 12 +++++------ ext/mri/cmodules/data.c | 44 +++++++++++++++++++++++++++++++++++------ 2 files changed, 44 insertions(+), 12 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 41b6f79..8f3524a 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -127,9 +127,9 @@ static VALUE arf_flat(VALUE self, VALUE array_val); static VALUE arf_flip(VALUE self, VALUE array_val, VALUE dim_val); static VALUE arf_lower(VALUE self); static VALUE arf_upper(VALUE self); -static VALUE arf_select(VALUE self); -static VALUE arf_select_scalar_r(VALUE self); -static VALUE arf_select_scalar_l(VALUE self); +static VALUE arf_select(VALUE self, VALUE array_cond_val, VALUE array_a_val, VALUE array_b_val); +static VALUE arf_select_scalar_r(VALUE self, VALUE array_cond_val, VALUE array_a_val, VALUE b_val); +static VALUE arf_select_scalar_l(VALUE self, VALUE array_cond_val, VALUE a_val, VALUE array_b_val); static VALUE arf_replace(VALUE self); static VALUE arf_replace_scalar(VALUE self); @@ -385,9 +385,9 @@ void Init_arrayfire() { rb_define_singleton_method(Data, "flip", (METHOD)arf_flip, 2); rb_define_singleton_method(Data, "lower", (METHOD)arf_lower, 0); rb_define_singleton_method(Data, "upper", (METHOD)arf_upper, 0); - rb_define_singleton_method(Data, "select", (METHOD)arf_select, 0); - rb_define_singleton_method(Data, "select_scalar_r", (METHOD)arf_select_scalar_r, 0); - rb_define_singleton_method(Data, "select_scalar_l", (METHOD)arf_select_scalar_l, 0); + rb_define_singleton_method(Data, "select", (METHOD)arf_select, 3); + rb_define_singleton_method(Data, "select_scalar_r", (METHOD)arf_select_scalar_r, 3); + rb_define_singleton_method(Data, "select_scalar_l", (METHOD)arf_select_scalar_l, 3); rb_define_singleton_method(Data, "replace", (METHOD)arf_replace, 0); rb_define_singleton_method(Data, "replace_scalar", (METHOD)arf_replace_scalar, 0); diff --git a/ext/mri/cmodules/data.c b/ext/mri/cmodules/data.c index 3b21a54..398a8c2 100644 --- a/ext/mri/cmodules/data.c +++ b/ext/mri/cmodules/data.c @@ -204,16 +204,48 @@ static VALUE arf_upper(VALUE self){ return Qnil; } -static VALUE arf_select(VALUE self){ - return Qnil; +static VALUE arf_select(VALUE self, VALUE array_cond_val, VALUE array_a_val, VALUE array_b_val){ + afstruct* array_cond; + afstruct* array_a; + afstruct* array_b; + afstruct* output = ALLOC(afstruct); + + Data_Get_Struct(array_cond_val, afstruct, array_cond); + Data_Get_Struct(array_a_val, afstruct, array_a); + Data_Get_Struct(array_b_val, afstruct, array_b); + + af_select(&output->carray, array_cond->carray, array_a->carray, array_b->carray); + af_print_array(output->carray); + + return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } -static VALUE arf_select_scalar_r(VALUE self){ - return Qnil; +static VALUE arf_select_scalar_r(VALUE self, VALUE array_cond_val, VALUE array_a_val, VALUE b_val){ + afstruct* array_cond; + afstruct* array_a; + afstruct* output = ALLOC(afstruct); + + Data_Get_Struct(array_cond_val, afstruct, array_cond); + Data_Get_Struct(array_a_val, afstruct, array_a); + + af_select_scalar_r(&output->carray, array_cond->carray, array_a->carray, NUM2DBL(b_val)); + af_print_array(output->carray); + + return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } -static VALUE arf_select_scalar_l(VALUE self){ - return Qnil; +static VALUE arf_select_scalar_l(VALUE self, VALUE array_cond_val, VALUE a_val, VALUE array_b_val){ + afstruct* array_cond; + afstruct* array_b; + afstruct* output = ALLOC(afstruct); + + Data_Get_Struct(array_cond_val, afstruct, array_cond); + Data_Get_Struct(array_b_val, afstruct, array_b); + + af_select_scalar_l(&output->carray, array_cond->carray, NUM2DBL(a_val), array_b->carray); + af_print_array(output->carray); + + return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } static VALUE arf_replace(VALUE self){ From e2faf8830f6fc25782051b5e450588ed0563bfc2 Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Thu, 29 Jun 2017 21:36:20 +0530 Subject: [PATCH 052/125] constant: implement Data replace methods --- ext/mri/arrayfire.c | 8 ++++---- ext/mri/cmodules/data.c | 24 ++++++++++++++++++++---- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 8f3524a..8d8254a 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -130,8 +130,8 @@ static VALUE arf_upper(VALUE self); static VALUE arf_select(VALUE self, VALUE array_cond_val, VALUE array_a_val, VALUE array_b_val); static VALUE arf_select_scalar_r(VALUE self, VALUE array_cond_val, VALUE array_a_val, VALUE b_val); static VALUE arf_select_scalar_l(VALUE self, VALUE array_cond_val, VALUE a_val, VALUE array_b_val); -static VALUE arf_replace(VALUE self); -static VALUE arf_replace_scalar(VALUE self); +static void arf_replace(VALUE self, VALUE array_input_val, VALUE array_cond_val, VALUE array_b_val); +static void arf_replace_scalar(VALUE self, VALUE array_input_val, VALUE array_cond_val, VALUE b_val); static VALUE arf_svd(VALUE self, VALUE val); @@ -388,8 +388,8 @@ void Init_arrayfire() { rb_define_singleton_method(Data, "select", (METHOD)arf_select, 3); rb_define_singleton_method(Data, "select_scalar_r", (METHOD)arf_select_scalar_r, 3); rb_define_singleton_method(Data, "select_scalar_l", (METHOD)arf_select_scalar_l, 3); - rb_define_singleton_method(Data, "replace", (METHOD)arf_replace, 0); - rb_define_singleton_method(Data, "replace_scalar", (METHOD)arf_replace_scalar, 0); + rb_define_singleton_method(Data, "replace", (METHOD)arf_replace, 3); + rb_define_singleton_method(Data, "replace_scalar", (METHOD)arf_replace_scalar, 3); Lapack = rb_define_class_under(ArrayFire, "LAPACK", rb_cObject); rb_define_singleton_method(Lapack, "svd", (METHOD)arf_svd, 0); diff --git a/ext/mri/cmodules/data.c b/ext/mri/cmodules/data.c index 398a8c2..76fea26 100644 --- a/ext/mri/cmodules/data.c +++ b/ext/mri/cmodules/data.c @@ -248,10 +248,26 @@ static VALUE arf_select_scalar_l(VALUE self, VALUE array_cond_val, VALUE a_val, return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } -static VALUE arf_replace(VALUE self){ - return Qnil; +static void arf_replace(VALUE self, VALUE array_input_val, VALUE array_cond_val, VALUE array_b_val){ + afstruct* input; + afstruct* array_cond; + afstruct* array_b; + + Data_Get_Struct(array_input_val, afstruct, input); + Data_Get_Struct(array_cond_val, afstruct, array_cond); + Data_Get_Struct(array_b_val, afstruct, array_b); + + af_replace(&input->carray, array_cond->carray, array_b->carray); + } -static VALUE arf_replace_scalar(VALUE self){ - return Qnil; +static void arf_replace_scalar(VALUE self, VALUE array_input_val, VALUE array_cond_val, VALUE b_val){ + afstruct* input; + afstruct* array_cond; + + Data_Get_Struct(array_input_val, afstruct, input); + Data_Get_Struct(array_cond_val, afstruct, array_cond); + + af_replace_scalar(&input->carray, array_cond->carray, NUM2DBL(b_val)); + } From 9392985ec0d3a57c19f8f2f76ec92bca810b517d Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Thu, 29 Jun 2017 22:37:01 +0530 Subject: [PATCH 053/125] constant: implement Data#lower Data#upper methods --- ext/mri/arrayfire.c | 8 ++++---- ext/mri/cmodules/data.c | 24 ++++++++++++++++++++---- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 8d8254a..bb3564c 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -125,8 +125,8 @@ static VALUE arf_shift(VALUE self, VALUE array_val, VALUE x_val, VALUE y_val, VA static VALUE arf_moddims(VALUE self); static VALUE arf_flat(VALUE self, VALUE array_val); static VALUE arf_flip(VALUE self, VALUE array_val, VALUE dim_val); -static VALUE arf_lower(VALUE self); -static VALUE arf_upper(VALUE self); +static VALUE arf_lower(VALUE self, VALUE array_val, VALUE dim_val); +static VALUE arf_upper(VALUE self, VALUE array_val, VALUE dim_val); static VALUE arf_select(VALUE self, VALUE array_cond_val, VALUE array_a_val, VALUE array_b_val); static VALUE arf_select_scalar_r(VALUE self, VALUE array_cond_val, VALUE array_a_val, VALUE b_val); static VALUE arf_select_scalar_l(VALUE self, VALUE array_cond_val, VALUE a_val, VALUE array_b_val); @@ -383,8 +383,8 @@ void Init_arrayfire() { rb_define_singleton_method(Data, "moddims", (METHOD)arf_moddims, 0); rb_define_singleton_method(Data, "flat", (METHOD)arf_flat, 1); rb_define_singleton_method(Data, "flip", (METHOD)arf_flip, 2); - rb_define_singleton_method(Data, "lower", (METHOD)arf_lower, 0); - rb_define_singleton_method(Data, "upper", (METHOD)arf_upper, 0); + rb_define_singleton_method(Data, "lower", (METHOD)arf_lower, 2); + rb_define_singleton_method(Data, "upper", (METHOD)arf_upper, 2); rb_define_singleton_method(Data, "select", (METHOD)arf_select, 3); rb_define_singleton_method(Data, "select_scalar_r", (METHOD)arf_select_scalar_r, 3); rb_define_singleton_method(Data, "select_scalar_l", (METHOD)arf_select_scalar_l, 3); diff --git a/ext/mri/cmodules/data.c b/ext/mri/cmodules/data.c index 76fea26..ed3a09e 100644 --- a/ext/mri/cmodules/data.c +++ b/ext/mri/cmodules/data.c @@ -196,12 +196,28 @@ static VALUE arf_flip(VALUE self, VALUE array_val, VALUE dim_val){ return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } -static VALUE arf_lower(VALUE self){ - return Qnil; +static VALUE arf_lower(VALUE self, VALUE array_val, VALUE is_unit_diag){ + afstruct* input; + afstruct* output = ALLOC(afstruct); + + Data_Get_Struct(array_val, afstruct, input); + + af_lower(&output->carray, input->carray, RTEST(is_unit_diag)); + af_print_array(output->carray); + + return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } -static VALUE arf_upper(VALUE self){ - return Qnil; +static VALUE arf_upper(VALUE self, VALUE array_val, VALUE is_unit_diag){ + afstruct* input; + afstruct* output = ALLOC(afstruct); + + Data_Get_Struct(array_val, afstruct, input); + + af_upper(&output->carray, input->carray, RTEST(is_unit_diag)); + af_print_array(output->carray); + + return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } static VALUE arf_select(VALUE self, VALUE array_cond_val, VALUE array_a_val, VALUE array_b_val){ From 5949de1aabe77a5068811e1dc4a16c09956bf8f9 Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Fri, 30 Jun 2017 10:51:37 +0530 Subject: [PATCH 054/125] adding methods to Af_Array class: initialization --- ext/mri/arrayfire.c | 64 +++++++++++++++++++++ ext/mri/cmodules/array.c | 119 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 183 insertions(+) create mode 100644 ext/mri/cmodules/array.c diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index bb3564c..0530774 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -20,6 +20,37 @@ static VALUE arf_init(int argc, VALUE* argv, VALUE self); static VALUE arf_alloc(VALUE klass); static void arf_free(afstruct* af); +static VALUE arf_create_array(VALUE self); +static VALUE arf_create_handle(VALUE self); +static VALUE arf_copy_array(VALUE self); +static VALUE arf_write_array(VALUE self); +static VALUE arf_get_data_ptr(VALUE self); +static VALUE arf_release_array(VALUE self); +static VALUE arf_retain_array(VALUE self); +static VALUE arf_get_data_ref_count(VALUE self); +static VALUE arf_eval(VALUE self); +static VALUE arf_eval_multiple(VALUE self); +static VALUE arf_set_manual_eval_flag(VALUE self); +static VALUE arf_get_manual_eval_flag(VALUE self); +static VALUE arf_get_elements(VALUE self); +static VALUE arf_get_type(VALUE self); +static VALUE arf_get_dims(VALUE self); +static VALUE arf_get_numdims(VALUE self); +static VALUE arf_is_empty(VALUE self); +static VALUE arf_is_scalar(VALUE self); +static VALUE arf_is_row(VALUE self); +static VALUE arf_is_column(VALUE self); +static VALUE arf_is_vector(VALUE self); +static VALUE arf_is_complex(VALUE self); +static VALUE arf_is_real(VALUE self); +static VALUE arf_is_double(VALUE self); +static VALUE arf_is_single(VALUE self); +static VALUE arf_is_realfloating(VALUE self); +static VALUE arf_is_floating(VALUE self); +static VALUE arf_is_integer(VALUE self); +static VALUE arf_is_bool(VALUE self); +static VALUE arf_is_sparse(VALUE self); + static VALUE arf_sum(VALUE self, VALUE array_val, VALUE dim_val); static VALUE arf_sum_nan(VALUE self, VALUE array_val, VALUE dim_val, VALUE nan_val); static VALUE arf_product(VALUE self, VALUE array_val, VALUE dim_val); @@ -240,6 +271,38 @@ void Init_arrayfire() { Af_Array = rb_define_class_under(ArrayFire, "Af_Array", rb_cObject); rb_define_alloc_func(Af_Array, arf_alloc); rb_define_method(Af_Array, "initialize", (METHOD)arf_init, -1); + rb_define_method(Af_Array, "arf_create_array", (METHOD)arf_create_array, 0); + rb_define_method(Af_Array, "arf_create_handle", (METHOD)arf_create_handle, 0); + rb_define_method(Af_Array, "arf_copy_array", (METHOD)arf_copy_array, 0); + rb_define_method(Af_Array, "arf_write_array", (METHOD)arf_write_array, 0); + rb_define_method(Af_Array, "arf_get_data_ptr", (METHOD)arf_get_data_ptr, 0); + rb_define_method(Af_Array, "arf_release_array", (METHOD)arf_release_array, 0); + rb_define_method(Af_Array, "arf_retain_array", (METHOD)arf_retain_array, 0); + rb_define_method(Af_Array, "arf_get_data_ref_count", (METHOD)arf_get_data_ref_count, 0); + rb_define_method(Af_Array, "arf_eval", (METHOD)arf_eval, 0); + rb_define_method(Af_Array, "arf_eval_multiple", (METHOD)arf_eval_multiple, 0); + rb_define_method(Af_Array, "arf_set_manual_eval_flag", (METHOD)arf_set_manual_eval_flag, 0); + rb_define_method(Af_Array, "arf_get_manual_eval_flag", (METHOD)arf_get_manual_eval_flag, 0); + rb_define_method(Af_Array, "arf_get_elements", (METHOD)arf_get_elements, 0); + rb_define_method(Af_Array, "arf_get_type", (METHOD)arf_get_type, 0); + rb_define_method(Af_Array, "arf_get_dims", (METHOD)arf_get_dims, 0); + rb_define_method(Af_Array, "arf_get_numdims", (METHOD)arf_get_numdims, 0); + rb_define_method(Af_Array, "arf_is_empty", (METHOD)arf_is_empty, 0); + rb_define_method(Af_Array, "arf_is_scalar", (METHOD)arf_is_scalar, 0); + rb_define_method(Af_Array, "arf_is_row", (METHOD)arf_is_row, 0); + rb_define_method(Af_Array, "arf_is_column", (METHOD)arf_is_column, 0); + rb_define_method(Af_Array, "arf_is_vector", (METHOD)arf_is_vector, 0); + rb_define_method(Af_Array, "arf_is_complex", (METHOD)arf_is_complex, 0); + rb_define_method(Af_Array, "arf_is_real", (METHOD)arf_is_real, 0); + rb_define_method(Af_Array, "arf_is_double", (METHOD)arf_is_double, 0); + rb_define_method(Af_Array, "arf_is_single", (METHOD)arf_is_single, 0); + rb_define_method(Af_Array, "arf_is_realfloating", (METHOD)arf_is_realfloating, 0); + rb_define_method(Af_Array, "arf_is_floating", (METHOD)arf_is_floating, 0); + rb_define_method(Af_Array, "arf_is_integer", (METHOD)arf_is_integer, 0); + rb_define_method(Af_Array, "arf_is_bool", (METHOD)arf_is_bool, 0); + rb_define_method(Af_Array, "arf_is_sparse", (METHOD)arf_is_sparse, 0); + + rb_define_method(Af_Array, "+",(METHOD)arf_ew_add,1); rb_define_method(Af_Array, "-",(METHOD)arf_ew_subtract,1); rb_define_method(Af_Array, "*",(METHOD)arf_ew_multiply,1); @@ -500,6 +563,7 @@ DEF_UNARY_RUBY_ACCESSOR(tgamma, tgamma) DEF_UNARY_RUBY_ACCESSOR(floor, floor) DEF_UNARY_RUBY_ACCESSOR(ceil, ceil) +#include "cmodules/array.c" #include "cmodules/algorithm.c" #include "cmodules/backend.c" #include "cmodules/blas.c" diff --git a/ext/mri/cmodules/array.c b/ext/mri/cmodules/array.c new file mode 100644 index 0000000..938b39f --- /dev/null +++ b/ext/mri/cmodules/array.c @@ -0,0 +1,119 @@ +static VALUE arf_create_array(VALUE self){ + return Qnil; +} + +static VALUE arf_create_handle(VALUE self){ + return Qnil; +} + +static VALUE arf_copy_array(VALUE self){ + return Qnil; +} + +static VALUE arf_write_array(VALUE self){ + return Qnil; +} + +static VALUE arf_get_data_ptr(VALUE self){ + return Qnil; +} + +static VALUE arf_release_array(VALUE self){ + return Qnil; +} + +static VALUE arf_retain_array(VALUE self){ + return Qnil; +} + +static VALUE arf_get_data_ref_count(VALUE self){ + return Qnil; +} + +static VALUE arf_eval(VALUE self){ + return Qnil; +} + +static VALUE arf_eval_multiple(VALUE self){ + return Qnil; +} + +static VALUE arf_set_manual_eval_flag(VALUE self){ + return Qnil; +} + +static VALUE arf_get_manual_eval_flag(VALUE self){ + return Qnil; +} + +static VALUE arf_get_elements(VALUE self){ + return Qnil; +} + +static VALUE arf_get_type(VALUE self){ + return Qnil; +} + +static VALUE arf_get_dims(VALUE self){ + return Qnil; +} + +static VALUE arf_get_numdims(VALUE self){ + return Qnil; +} + +static VALUE arf_is_empty(VALUE self){ + return Qnil; +} + +static VALUE arf_is_scalar(VALUE self){ + return Qnil; +} + +static VALUE arf_is_row(VALUE self){ + return Qnil; +} + +static VALUE arf_is_column(VALUE self){ + return Qnil; +} + +static VALUE arf_is_vector(VALUE self){ + return Qnil; +} + +static VALUE arf_is_complex(VALUE self){ + return Qnil; +} + +static VALUE arf_is_real(VALUE self){ + return Qnil; +} + +static VALUE arf_is_double(VALUE self){ + return Qnil; +} + +static VALUE arf_is_single(VALUE self){ + return Qnil; +} + +static VALUE arf_is_realfloating(VALUE self){ + return Qnil; +} + +static VALUE arf_is_floating(VALUE self){ + return Qnil; +} + +static VALUE arf_is_integer(VALUE self){ + return Qnil; +} + +static VALUE arf_is_bool(VALUE self){ + return Qnil; +} + +static VALUE arf_is_sparse(VALUE self){ + return Qnil; +} From b8bd1365a8dae8547d16e1b0e03ba12f84a69d36 Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Fri, 30 Jun 2017 12:56:09 +0530 Subject: [PATCH 055/125] modify names of Af_Array methods --- ext/mri/arrayfire.c | 60 ++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 0530774..b8549e5 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -271,36 +271,36 @@ void Init_arrayfire() { Af_Array = rb_define_class_under(ArrayFire, "Af_Array", rb_cObject); rb_define_alloc_func(Af_Array, arf_alloc); rb_define_method(Af_Array, "initialize", (METHOD)arf_init, -1); - rb_define_method(Af_Array, "arf_create_array", (METHOD)arf_create_array, 0); - rb_define_method(Af_Array, "arf_create_handle", (METHOD)arf_create_handle, 0); - rb_define_method(Af_Array, "arf_copy_array", (METHOD)arf_copy_array, 0); - rb_define_method(Af_Array, "arf_write_array", (METHOD)arf_write_array, 0); - rb_define_method(Af_Array, "arf_get_data_ptr", (METHOD)arf_get_data_ptr, 0); - rb_define_method(Af_Array, "arf_release_array", (METHOD)arf_release_array, 0); - rb_define_method(Af_Array, "arf_retain_array", (METHOD)arf_retain_array, 0); - rb_define_method(Af_Array, "arf_get_data_ref_count", (METHOD)arf_get_data_ref_count, 0); - rb_define_method(Af_Array, "arf_eval", (METHOD)arf_eval, 0); - rb_define_method(Af_Array, "arf_eval_multiple", (METHOD)arf_eval_multiple, 0); - rb_define_method(Af_Array, "arf_set_manual_eval_flag", (METHOD)arf_set_manual_eval_flag, 0); - rb_define_method(Af_Array, "arf_get_manual_eval_flag", (METHOD)arf_get_manual_eval_flag, 0); - rb_define_method(Af_Array, "arf_get_elements", (METHOD)arf_get_elements, 0); - rb_define_method(Af_Array, "arf_get_type", (METHOD)arf_get_type, 0); - rb_define_method(Af_Array, "arf_get_dims", (METHOD)arf_get_dims, 0); - rb_define_method(Af_Array, "arf_get_numdims", (METHOD)arf_get_numdims, 0); - rb_define_method(Af_Array, "arf_is_empty", (METHOD)arf_is_empty, 0); - rb_define_method(Af_Array, "arf_is_scalar", (METHOD)arf_is_scalar, 0); - rb_define_method(Af_Array, "arf_is_row", (METHOD)arf_is_row, 0); - rb_define_method(Af_Array, "arf_is_column", (METHOD)arf_is_column, 0); - rb_define_method(Af_Array, "arf_is_vector", (METHOD)arf_is_vector, 0); - rb_define_method(Af_Array, "arf_is_complex", (METHOD)arf_is_complex, 0); - rb_define_method(Af_Array, "arf_is_real", (METHOD)arf_is_real, 0); - rb_define_method(Af_Array, "arf_is_double", (METHOD)arf_is_double, 0); - rb_define_method(Af_Array, "arf_is_single", (METHOD)arf_is_single, 0); - rb_define_method(Af_Array, "arf_is_realfloating", (METHOD)arf_is_realfloating, 0); - rb_define_method(Af_Array, "arf_is_floating", (METHOD)arf_is_floating, 0); - rb_define_method(Af_Array, "arf_is_integer", (METHOD)arf_is_integer, 0); - rb_define_method(Af_Array, "arf_is_bool", (METHOD)arf_is_bool, 0); - rb_define_method(Af_Array, "arf_is_sparse", (METHOD)arf_is_sparse, 0); + rb_define_method(Af_Array, "create_array", (METHOD)arf_create_array, 0); + rb_define_method(Af_Array, "create_handle", (METHOD)arf_create_handle, 0); + rb_define_method(Af_Array, "copy_array", (METHOD)arf_copy_array, 0); + rb_define_method(Af_Array, "write_array", (METHOD)arf_write_array, 0); + rb_define_method(Af_Array, "get_data_ptr", (METHOD)arf_get_data_ptr, 0); + rb_define_method(Af_Array, "release_array", (METHOD)arf_release_array, 0); + rb_define_method(Af_Array, "retain_array", (METHOD)arf_retain_array, 0); + rb_define_method(Af_Array, "get_data_ref_count", (METHOD)arf_get_data_ref_count, 0); + rb_define_method(Af_Array, "eval", (METHOD)arf_eval, 0); + rb_define_method(Af_Array, "eval_multiple", (METHOD)arf_eval_multiple, 0); + rb_define_method(Af_Array, "set_manual_eval_flag", (METHOD)arf_set_manual_eval_flag, 0); + rb_define_method(Af_Array, "get_manual_eval_flag", (METHOD)arf_get_manual_eval_flag, 0); + rb_define_method(Af_Array, "get_elements", (METHOD)arf_get_elements, 0); + rb_define_method(Af_Array, "get_type", (METHOD)arf_get_type, 0); + rb_define_method(Af_Array, "get_dims", (METHOD)arf_get_dims, 0); + rb_define_method(Af_Array, "get_numdims", (METHOD)arf_get_numdims, 0); + rb_define_method(Af_Array, "is_empty", (METHOD)arf_is_empty, 0); + rb_define_method(Af_Array, "is_scalar", (METHOD)arf_is_scalar, 0); + rb_define_method(Af_Array, "is_row", (METHOD)arf_is_row, 0); + rb_define_method(Af_Array, "is_column", (METHOD)arf_is_column, 0); + rb_define_method(Af_Array, "is_vector", (METHOD)arf_is_vector, 0); + rb_define_method(Af_Array, "is_complex", (METHOD)arf_is_complex, 0); + rb_define_method(Af_Array, "is_real", (METHOD)arf_is_real, 0); + rb_define_method(Af_Array, "is_double", (METHOD)arf_is_double, 0); + rb_define_method(Af_Array, "is_single", (METHOD)arf_is_single, 0); + rb_define_method(Af_Array, "is_realfloating", (METHOD)arf_is_realfloating, 0); + rb_define_method(Af_Array, "is_floating", (METHOD)arf_is_floating, 0); + rb_define_method(Af_Array, "is_integer", (METHOD)arf_is_integer, 0); + rb_define_method(Af_Array, "is_bool", (METHOD)arf_is_bool, 0); + rb_define_method(Af_Array, "is_sparse", (METHOD)arf_is_sparse, 0); rb_define_method(Af_Array, "+",(METHOD)arf_ew_add,1); From 8dbda7498ec5bfb6e9c29f6fde64ba59d3db1a19 Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Fri, 30 Jun 2017 12:57:36 +0530 Subject: [PATCH 056/125] Impement af_array bindings --- ext/mri/cmodules/array.c | 142 ++++++++++++++++++++++++++++++++++----- 1 file changed, 126 insertions(+), 16 deletions(-) diff --git a/ext/mri/cmodules/array.c b/ext/mri/cmodules/array.c index 938b39f..981fabd 100644 --- a/ext/mri/cmodules/array.c +++ b/ext/mri/cmodules/array.c @@ -47,73 +47,183 @@ static VALUE arf_get_manual_eval_flag(VALUE self){ } static VALUE arf_get_elements(VALUE self){ - return Qnil; + afstruct* input; + + dim_t elems; + + Data_Get_Struct(self, afstruct, input); + + af_get_elements(&elems, input->carray); + return ULONG2NUM(elems); } static VALUE arf_get_type(VALUE self){ + afstruct* input; + af_dtype type; + + Data_Get_Struct(self, afstruct, input); + + af_get_type(&type, input->carray); return Qnil; } static VALUE arf_get_dims(VALUE self){ + afstruct* input; + dim_t d0, d1, d2, d3; + + Data_Get_Struct(self, afstruct, input); + + af_get_dims(&d0, &d1, &d2, &d3, input->carray); return Qnil; } static VALUE arf_get_numdims(VALUE self){ - return Qnil; + afstruct* input; + uint result; + + Data_Get_Struct(self, afstruct, input); + + af_get_numdims(&result, input->carray); + return UINT2NUM(result); } static VALUE arf_is_empty(VALUE self){ - return Qnil; + afstruct* input; + bool result; + + Data_Get_Struct(self, afstruct, input); + + af_is_empty(&result, input->carray); + return result ? Qtrue : Qfalse; } static VALUE arf_is_scalar(VALUE self){ - return Qnil; + afstruct* input; + bool result; + + Data_Get_Struct(self, afstruct, input); + + af_is_scalar(&result, input->carray); + return result ? Qtrue : Qfalse; } static VALUE arf_is_row(VALUE self){ - return Qnil; + afstruct* input; + bool result; + + Data_Get_Struct(self, afstruct, input); + + af_is_row(&result, input->carray); + return result ? Qtrue : Qfalse; } static VALUE arf_is_column(VALUE self){ - return Qnil; + afstruct* input; + bool result; + + Data_Get_Struct(self, afstruct, input); + + af_is_column(&result, input->carray); + return result ? Qtrue : Qfalse; } static VALUE arf_is_vector(VALUE self){ - return Qnil; + afstruct* input; + bool result; + + Data_Get_Struct(self, afstruct, input); + + af_is_vector(&result, input->carray); + return result ? Qtrue : Qfalse; } static VALUE arf_is_complex(VALUE self){ - return Qnil; + afstruct* input; + bool result; + + Data_Get_Struct(self, afstruct, input); + + af_is_complex(&result, input->carray); + return result ? Qtrue : Qfalse; } static VALUE arf_is_real(VALUE self){ - return Qnil; + afstruct* input; + bool result; + + Data_Get_Struct(self, afstruct, input); + + af_is_real(&result, input->carray); + return result ? Qtrue : Qfalse; } static VALUE arf_is_double(VALUE self){ - return Qnil; + afstruct* input; + bool result; + + Data_Get_Struct(self, afstruct, input); + + af_is_double(&result, input->carray); + return result ? Qtrue : Qfalse; } static VALUE arf_is_single(VALUE self){ - return Qnil; + afstruct* input; + bool result; + + Data_Get_Struct(self, afstruct, input); + + af_is_single(&result, input->carray); + return result ? Qtrue : Qfalse; } static VALUE arf_is_realfloating(VALUE self){ - return Qnil; + afstruct* input; + bool result; + + Data_Get_Struct(self, afstruct, input); + + af_is_realfloating(&result, input->carray); + return result ? Qtrue : Qfalse; } static VALUE arf_is_floating(VALUE self){ - return Qnil; + afstruct* input; + bool result; + + Data_Get_Struct(self, afstruct, input); + + af_is_floating(&result, input->carray); + return result ? Qtrue : Qfalse; } static VALUE arf_is_integer(VALUE self){ - return Qnil; + afstruct* input; + bool result; + + Data_Get_Struct(self, afstruct, input); + + af_is_integer(&result, input->carray); + return result ? Qtrue : Qfalse; } static VALUE arf_is_bool(VALUE self){ - return Qnil; + afstruct* input; + bool result; + + Data_Get_Struct(self, afstruct, input); + + af_is_bool(&result, input->carray); + return result ? Qtrue : Qfalse; } static VALUE arf_is_sparse(VALUE self){ - return Qnil; + afstruct* input; + bool result = false; + + Data_Get_Struct(self, afstruct, input); + + //FIXME + // af_is_bool(&result, input->carray); + return result ? Qtrue : Qfalse; } From b64343939b841328245486306e5c74982d10fa62 Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Fri, 30 Jun 2017 14:45:07 +0530 Subject: [PATCH 057/125] add bindings for Index module --- ext/mri/arrayfire.c | 24 ++++++++++++++++++++++++ ext/mri/cmodules/index.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 ext/mri/cmodules/index.c diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index b8549e5..8851315 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -10,6 +10,7 @@ VALUE Blas = Qnil; VALUE Cuda = Qnil; VALUE Data = Qnil; VALUE Device = Qnil; +VALUE Index = Qnil; VALUE Lapack = Qnil; VALUE OpenCL = Qnil; VALUE Random = Qnil; @@ -164,6 +165,16 @@ static VALUE arf_select_scalar_l(VALUE self, VALUE array_cond_val, VALUE a_val, static void arf_replace(VALUE self, VALUE array_input_val, VALUE array_cond_val, VALUE array_b_val); static void arf_replace_scalar(VALUE self, VALUE array_input_val, VALUE array_cond_val, VALUE b_val); +static VALUE arf_index(VALUE self); +static VALUE arf_lookup(VALUE self); +static VALUE arf_assign_seq(VALUE self); +static VALUE arf_index_gen(VALUE self); +static VALUE arf_assign_gen(VALUE self); +static VALUE arf_create_indexers(VALUE self); +static VALUE arf_set_array_indexer(VALUE self); +static VALUE arf_set_seq_indexer(VALUE self); +static VALUE arf_set_seq_param_indexer(VALUE self); +static VALUE arf_release_indexers(VALUE self); static VALUE arf_svd(VALUE self, VALUE val); static VALUE arf_svd_inplace(VALUE self, VALUE val); @@ -417,6 +428,18 @@ void Init_arrayfire() { rb_define_singleton_method(Cuda, "get_native_id", (METHOD)arf_get_native_id, 0); rb_define_singleton_method(Cuda, "set_native_id", (METHOD)arf_set_native_id, 0); + Index = rb_define_class_under(ArrayFire, "Index", rb_cObject); + rb_define_singleton_method(Index, "index", (METHOD)arf_index, 0); + rb_define_singleton_method(Index, "lookup", (METHOD)arf_lookup, 0); + rb_define_singleton_method(Index, "assign_seq", (METHOD)arf_assign_seq, 0); + rb_define_singleton_method(Index, "index_gen", (METHOD)arf_index_gen, 0); + rb_define_singleton_method(Index, "assign_gen", (METHOD)arf_assign_gen, 0); + rb_define_singleton_method(Index, "create_indexers", (METHOD)arf_create_indexers, 0); + rb_define_singleton_method(Index, "set_array_indexer", (METHOD)arf_set_array_indexer, 0); + rb_define_singleton_method(Index, "set_seq_indexer", (METHOD)arf_set_seq_indexer, 0); + rb_define_singleton_method(Index, "set_seq_param_indexer", (METHOD)arf_set_seq_param_indexer, 0); + rb_define_singleton_method(Index, "release_indexers", (METHOD)arf_release_indexers, 0); + OpenCL = rb_define_class_under(ArrayFire, "OpenCL", rb_cObject); rb_define_singleton_method(OpenCL, "get_context", (METHOD)arf_get_context, 0); rb_define_singleton_method(OpenCL, "get_queue", (METHOD)arf_get_queue, 0); @@ -569,6 +592,7 @@ DEF_UNARY_RUBY_ACCESSOR(ceil, ceil) #include "cmodules/blas.c" #include "cmodules/cuda.c" #include "cmodules/device.c" +#include "cmodules/index.c" #include "cmodules/opencl.c" #include "cmodules/data.c" #include "cmodules/lapack.c" diff --git a/ext/mri/cmodules/index.c b/ext/mri/cmodules/index.c new file mode 100644 index 0000000..6c7896b --- /dev/null +++ b/ext/mri/cmodules/index.c @@ -0,0 +1,39 @@ +static VALUE arf_index(VALUE self){ + return Qnil; +} + +static VALUE arf_lookup(VALUE self){ + return Qnil; +} + +static VALUE arf_assign_seq(VALUE self){ + return Qnil; +} + +static VALUE arf_index_gen(VALUE self){ + return Qnil; +} + +static VALUE arf_assign_gen(VALUE self){ + return Qnil; +} + +static VALUE arf_create_indexers(VALUE self){ + return Qnil; +} + +static VALUE arf_set_array_indexer(VALUE self){ + return Qnil; +} + +static VALUE arf_set_seq_indexer(VALUE self){ + return Qnil; +} + +static VALUE arf_set_seq_param_indexer(VALUE self){ + return Qnil; +} + +static VALUE arf_release_indexers(VALUE self){ + return Qnil; +} From 84417333984d19c3a49b70f14e3173d316e87d4d Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Fri, 30 Jun 2017 16:20:57 +0530 Subject: [PATCH 058/125] implement to_cpu and ndims method to Af_Array --- ext/mri/arrayfire.c | 2 ++ ext/mri/cmodules/array.c | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 8851315..41313d1 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -313,6 +313,8 @@ void Init_arrayfire() { rb_define_method(Af_Array, "is_bool", (METHOD)arf_is_bool, 0); rb_define_method(Af_Array, "is_sparse", (METHOD)arf_is_sparse, 0); + rb_define_alias(Af_Array, "to_cpu", "get_data_ptr"); + rb_define_alias(Af_Array, "ndims", "get_numdims"); rb_define_method(Af_Array, "+",(METHOD)arf_ew_add,1); rb_define_method(Af_Array, "-",(METHOD)arf_ew_subtract,1); diff --git a/ext/mri/cmodules/array.c b/ext/mri/cmodules/array.c index 981fabd..ad0a3f5 100644 --- a/ext/mri/cmodules/array.c +++ b/ext/mri/cmodules/array.c @@ -15,7 +15,21 @@ static VALUE arf_write_array(VALUE self){ } static VALUE arf_get_data_ptr(VALUE self){ - return Qnil; + afstruct* input; + dim_t count; + + Data_Get_Struct(self, afstruct, input); + + af_get_elements(&count, input->carray); + float* data = (float*)malloc(count * sizeof(float)); + af_get_data_ptr(data, input->carray); + + VALUE* array = ALLOC_N(VALUE, count); + for (size_t index = 0; index < (size_t)count; index++){ + array[index] = DBL2NUM(data[index]); + } + + return rb_ary_new4(count, array); } static VALUE arf_release_array(VALUE self){ From d158eab306a2c3f7744eccf9433171fc97155048 Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Fri, 30 Jun 2017 16:41:17 +0530 Subject: [PATCH 059/125] add retain and get_reference bindings --- ext/mri/arrayfire.c | 2 +- ext/mri/cmodules/array.c | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 41313d1..fc36827 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -26,7 +26,7 @@ static VALUE arf_create_handle(VALUE self); static VALUE arf_copy_array(VALUE self); static VALUE arf_write_array(VALUE self); static VALUE arf_get_data_ptr(VALUE self); -static VALUE arf_release_array(VALUE self); +static void arf_release_array(VALUE self); static VALUE arf_retain_array(VALUE self); static VALUE arf_get_data_ref_count(VALUE self); static VALUE arf_eval(VALUE self); diff --git a/ext/mri/cmodules/array.c b/ext/mri/cmodules/array.c index ad0a3f5..3eb9163 100644 --- a/ext/mri/cmodules/array.c +++ b/ext/mri/cmodules/array.c @@ -32,16 +32,26 @@ static VALUE arf_get_data_ptr(VALUE self){ return rb_ary_new4(count, array); } -static VALUE arf_release_array(VALUE self){ - return Qnil; +static void arf_release_array(VALUE self){ + afstruct* input; + Data_Get_Struct(self, afstruct, input); + af_release_array(input->carray); } static VALUE arf_retain_array(VALUE self){ - return Qnil; + afstruct* input; + afstruct* output = ALLOC(afstruct); + Data_Get_Struct(self, afstruct, input); + af_retain_array(&output->carray, input->carray); + return Data_Wrap_Struct(CLASS_OF(self), NULL, arf_free, output); } static VALUE arf_get_data_ref_count(VALUE self){ - return Qnil; + afstruct* input; + int use_count; + Data_Get_Struct(self, afstruct, input); + af_get_data_ref_count(&use_count, input->carray); + return INT2NUM(use_count); } static VALUE arf_eval(VALUE self){ From b79ba12e4ec1355b85840800fbcff899405d1b3a Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Fri, 30 Jun 2017 17:25:56 +0530 Subject: [PATCH 060/125] generalize Af_Array#new to accept ndims upto 4 --- ext/mri/arrayfire.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index fc36827..1da1f99 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -392,7 +392,7 @@ void Init_arrayfire() { Device = rb_define_class_under(ArrayFire, "Device", rb_cObject); rb_define_singleton_method(Device, "info", (METHOD)arf_info, 0); - rb_define_method(Device, "init", (METHOD)arf_init, 0); + rb_define_method(Device, "init", (METHOD)arf_init2, 0); rb_define_method(Device, "info_string", (METHOD)arf_info_string, 0); rb_define_method(Device, "device_info", (METHOD)arf_device_info, 0); rb_define_method(Device, "get_device_count", (METHOD)arf_get_device_count, 0); @@ -530,7 +530,7 @@ VALUE arf_init(int argc, VALUE* argv, VALUE self) host_array[index] = (float)NUM2DBL(RARRAY_AREF(argv[2], index)); } - af_create_array(&afarray->carray, host_array, 2, dimensions, f32); + af_create_array(&afarray->carray, host_array, ndims, dimensions, f32); af_print_array(afarray->carray); From c1fe2dbb7d915b4f1f1e9e30e05d87bbb1ffce5f Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Fri, 30 Jun 2017 17:28:56 +0530 Subject: [PATCH 061/125] implement Af_Array singleton methods to crate_array and create_handle --- ext/mri/arrayfire.c | 8 ++++---- ext/mri/cmodules/array.c | 42 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 1da1f99..ef8d0ba 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -21,8 +21,8 @@ static VALUE arf_init(int argc, VALUE* argv, VALUE self); static VALUE arf_alloc(VALUE klass); static void arf_free(afstruct* af); -static VALUE arf_create_array(VALUE self); -static VALUE arf_create_handle(VALUE self); +static VALUE arf_create_array(int argc, VALUE* argv); +static VALUE arf_create_handle(int argc, VALUE* argv); static VALUE arf_copy_array(VALUE self); static VALUE arf_write_array(VALUE self); static VALUE arf_get_data_ptr(VALUE self); @@ -282,8 +282,8 @@ void Init_arrayfire() { Af_Array = rb_define_class_under(ArrayFire, "Af_Array", rb_cObject); rb_define_alloc_func(Af_Array, arf_alloc); rb_define_method(Af_Array, "initialize", (METHOD)arf_init, -1); - rb_define_method(Af_Array, "create_array", (METHOD)arf_create_array, 0); - rb_define_method(Af_Array, "create_handle", (METHOD)arf_create_handle, 0); + rb_define_singleton_method(Af_Array, "create_array", (METHOD)arf_create_array, -1); + rb_define_singleton_method(Af_Array, "create_handle", (METHOD)arf_create_handle, -1); rb_define_method(Af_Array, "copy_array", (METHOD)arf_copy_array, 0); rb_define_method(Af_Array, "write_array", (METHOD)arf_write_array, 0); rb_define_method(Af_Array, "get_data_ptr", (METHOD)arf_get_data_ptr, 0); diff --git a/ext/mri/cmodules/array.c b/ext/mri/cmodules/array.c index 3eb9163..852c4a9 100644 --- a/ext/mri/cmodules/array.c +++ b/ext/mri/cmodules/array.c @@ -1,9 +1,43 @@ -static VALUE arf_create_array(VALUE self){ - return Qnil; +static VALUE arf_create_array(int argc, VALUE* argv){ + afstruct* afarray = ALLOC(afstruct); + dim_t ndims = (dim_t)FIX2LONG(argv[0]); + dim_t* dimensions = (dim_t*)malloc(ndims * sizeof(dim_t)); + dim_t count = 1; + for (size_t index = 0; index < ndims; index++) { + dimensions[index] = (dim_t)FIX2LONG(RARRAY_AREF(argv[1], index)); + count *= dimensions[index]; + } + float* host_array = (float*)malloc(count * sizeof(float)); + for (size_t index = 0; index < count; index++) { + host_array[index] = (float)NUM2DBL(RARRAY_AREF(argv[2], index)); + } + + af_create_array(&afarray->carray, host_array, ndims, dimensions, f32); + + af_print_array(afarray->carray); + + return Data_Wrap_Struct(Af_Array, NULL, arf_free, afarray); } -static VALUE arf_create_handle(VALUE self){ - return Qnil; +static VALUE arf_create_handle(int argc, VALUE* argv){ + afstruct* afarray = ALLOC(afstruct); + dim_t ndims = (dim_t)FIX2LONG(argv[0]); + dim_t* dimensions = (dim_t*)malloc(ndims * sizeof(dim_t)); + dim_t count = 1; + for (size_t index = 0; index < ndims; index++) { + dimensions[index] = (dim_t)FIX2LONG(RARRAY_AREF(argv[1], index)); + count *= dimensions[index]; + } + float* host_array = (float*)malloc(count * sizeof(float)); + for (size_t index = 0; index < count; index++) { + host_array[index] = (float)NUM2DBL(RARRAY_AREF(argv[2], index)); + } + + af_create_handle(&afarray->carray, ndims, dimensions, f32); + + af_print_array(afarray->carray); + + return Data_Wrap_Struct(Af_Array, NULL, arf_free, afarray); } static VALUE arf_copy_array(VALUE self){ From 98b8f8a1125bed2448f6d1b06940e815e4f3ca40 Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Fri, 30 Jun 2017 17:52:28 +0530 Subject: [PATCH 062/125] implemented deep array coppying --- ext/mri/cmodules/array.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/ext/mri/cmodules/array.c b/ext/mri/cmodules/array.c index 852c4a9..a357e3a 100644 --- a/ext/mri/cmodules/array.c +++ b/ext/mri/cmodules/array.c @@ -41,7 +41,14 @@ static VALUE arf_create_handle(int argc, VALUE* argv){ } static VALUE arf_copy_array(VALUE self){ - return Qnil; + afstruct* array_val; + afstruct* result = ALLOC(afstruct); + + Data_Get_Struct(self, afstruct, result); + + af_copy_array(&result->carray, result->carray); + + return Data_Wrap_Struct(CLASS_OF(self), NULL, arf_free, result); } static VALUE arf_write_array(VALUE self){ From f4bf35ebf33bef71414de786c94457ff5250c755 Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Fri, 7 Jul 2017 11:00:49 +0530 Subject: [PATCH 063/125] add elementwise comparision operators --- ext/mri/arrayfire.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index ef8d0ba..2b1418a 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -240,6 +240,13 @@ DECL_ELEMENTWISE_RUBY_ACCESSOR(subtract) DECL_ELEMENTWISE_RUBY_ACCESSOR(multiply) DECL_ELEMENTWISE_RUBY_ACCESSOR(divide) +DECL_ELEMENTWISE_RUBY_ACCESSOR(eqeq) +DECL_ELEMENTWISE_RUBY_ACCESSOR(neq) +DECL_ELEMENTWISE_RUBY_ACCESSOR(leq) +DECL_ELEMENTWISE_RUBY_ACCESSOR(geq) +DECL_ELEMENTWISE_RUBY_ACCESSOR(lt) +DECL_ELEMENTWISE_RUBY_ACCESSOR(gt) + DECL_UNARY_RUBY_ACCESSOR(sin) DECL_UNARY_RUBY_ACCESSOR(cos) DECL_UNARY_RUBY_ACCESSOR(tan) @@ -322,6 +329,13 @@ void Init_arrayfire() { rb_define_method(Af_Array, "/",(METHOD)arf_ew_divide,1); rb_define_method(Af_Array, "==",(METHOD)arf_eqeq,1); + rb_define_method(Af_Array, "=~",(METHOD)arf_ew_eqeq,1); + rb_define_method(Af_Array, "!~", (METHOD)arf_ew_neq, 1); + rb_define_method(Af_Array, "<=", (METHOD)arf_ew_leq, 1); + rb_define_method(Af_Array, ">=", (METHOD)arf_ew_geq, 1); + rb_define_method(Af_Array, "<", (METHOD)arf_ew_lt, 1); + rb_define_method(Af_Array, ">", (METHOD)arf_ew_gt, 1); + rb_define_method(Af_Array, "sin", (METHOD)arf_unary_sin, 0); rb_define_method(Af_Array, "cos", (METHOD)arf_unary_cos, 0); rb_define_method(Af_Array, "tan", (METHOD)arf_unary_tan, 0); @@ -563,6 +577,13 @@ DEF_ELEMENTWISE_RUBY_ACCESSOR(subtract, sub) DEF_ELEMENTWISE_RUBY_ACCESSOR(multiply, mul) DEF_ELEMENTWISE_RUBY_ACCESSOR(divide, div) +DEF_ELEMENTWISE_RUBY_ACCESSOR(eqeq, eq) +DEF_ELEMENTWISE_RUBY_ACCESSOR(neq, neq) +DEF_ELEMENTWISE_RUBY_ACCESSOR(leq, le) +DEF_ELEMENTWISE_RUBY_ACCESSOR(geq, ge) +DEF_ELEMENTWISE_RUBY_ACCESSOR(lt, lt) +DEF_ELEMENTWISE_RUBY_ACCESSOR(gt, gt) + DEF_UNARY_RUBY_ACCESSOR(sin, sin) DEF_UNARY_RUBY_ACCESSOR(cos, cos) DEF_UNARY_RUBY_ACCESSOR(tan, tan) From a23cd42e857bd495091b970642f98dc83e126c2e Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Fri, 7 Jul 2017 11:30:18 +0530 Subject: [PATCH 064/125] added equality operator --- ext/mri/arrayfire.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 2b1418a..3f53c86 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -569,7 +569,25 @@ static void arf_free(afstruct* af) DEF_ELEMENTWISE_RUBY_ACCESSOR(ADD, add) static VALUE arf_eqeq(VALUE left_val, VALUE right_val) { - return Qnil; + afstruct* left; + afstruct* right; + afstruct* result = ALLOC(afstruct); + Data_Get_Struct(left_val, afstruct, left); + Data_Get_Struct(right_val, afstruct, right); + af_eq(&result->carray, left->carray, right->carray, true); + + dim_t count; + af_get_elements(&count, result->carray); + float* data = (float*)malloc(count * sizeof(float)); + af_get_data_ptr(data, result->carray); + + for (size_t index = 0; index < (size_t)count; index++){ + if(data[index] == 0){ + return Qfalse; + } + } + + return Qtrue; } DEF_ELEMENTWISE_RUBY_ACCESSOR(add, add) From 4be52d60fe12caf04d61a15aef57581040834b0d Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Fri, 7 Jul 2017 12:29:28 +0530 Subject: [PATCH 065/125] use bool array to store result in eqeq --- ext/mri/arrayfire.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 3f53c86..a6ea721 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -578,15 +578,14 @@ static VALUE arf_eqeq(VALUE left_val, VALUE right_val) { dim_t count; af_get_elements(&count, result->carray); - float* data = (float*)malloc(count * sizeof(float)); + bool* data = (bool*)malloc(count * sizeof(bool)); af_get_data_ptr(data, result->carray); for (size_t index = 0; index < (size_t)count; index++){ - if(data[index] == 0){ + if(!data[index]){ return Qfalse; } } - return Qtrue; } From 73a87873851fb33a3983eb544b24179145ad7b81 Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Fri, 7 Jul 2017 12:53:55 +0530 Subject: [PATCH 066/125] modify blas spec --- spec/blas_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/blas_spec.rb b/spec/blas_spec.rb index 67d3f49..4e20d29 100644 --- a/spec/blas_spec.rb +++ b/spec/blas_spec.rb @@ -5,7 +5,7 @@ let(:a) { ArrayFire::Af_Array.new 2, [2,2],[1,2,3,4] } let(:b) { ArrayFire::Af_Array.new 2, [2,2],[1,2,3,4] } let(:c) { ArrayFire::Af_Array.new 2, [2,2],[7.0,10.0,15.0,22.0] } - subject {c} - it {expect(ArrayFire::BLAS.matmul(a,b).array).to eq c.array} + subject {ArrayFire::BLAS.matmul(a,b)} + it {is_expected.to eq c} end end From db68dc2e685a41942a67def5c90ebc88add804c5 Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Fri, 7 Jul 2017 15:56:42 +0530 Subject: [PATCH 067/125] added rspec-its as a dependency --- arrayfire.gemspec | 1 + 1 file changed, 1 insertion(+) diff --git a/arrayfire.gemspec b/arrayfire.gemspec index 6f38d57..f328160 100644 --- a/arrayfire.gemspec +++ b/arrayfire.gemspec @@ -21,4 +21,5 @@ 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 'rspec', '~>2.14' + gem.add_development_dependency 'rspec-its' end From 3a0bb7bd2c39db2a01a902e96b78d0c935edee8f Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Fri, 7 Jul 2017 15:57:33 +0530 Subject: [PATCH 068/125] modify tests in creation_spec and reauire rspec-its --- spec/creation_spec.rb | 24 +++++------------------- spec/spec_helper.rb | 2 ++ 2 files changed, 7 insertions(+), 19 deletions(-) diff --git a/spec/creation_spec.rb b/spec/creation_spec.rb index d93194b..5d03d25 100644 --- a/spec/creation_spec.rb +++ b/spec/creation_spec.rb @@ -4,31 +4,17 @@ context '#initialize' do let(:i) { ArrayFire::Af_Array.new 2, [2,2],[1,2,3,4] } subject { i } - it { expect(i.ndims).to eq 2 } - it { expect(i.dimension).to eq [2,2] } - it { expect(i.array).to eq [1,2,3,4] } - it{ - pending("only float supported currently") - expect(i.dtype).to eq :float64 - } + its(:ndims) { is_expected.to eq 2 } + its(:dims) { is_expected.to eq [2,2] } + its(:elements) {is_expected.to eq [1,2,3,4]} end context '#addition' do let(:a) { ArrayFire::Af_Array.new 2, [2,2],[1,2,3,4] } let(:b) { ArrayFire::Af_Array.new 2, [2,2],[1,2,3,4] } let(:c) { ArrayFire::Af_Array.new 2, [2,2],[2,4,6,8] } - subject {c} - it {expect(a+b).to eq c} - it {expect(c.ndims).to eq a.ndims} - it {expect(c.dimension).to eq a.dimension} + subject {a+b} + it {is_expected.to eq c} end - context '#Equality' do - let(:a) { ArrayFire::Af_Array.new 2, [2,2],[1,2,3,4] } - let(:b) { ArrayFire::Af_Array.new 2, [2,2],[1,2,3,4] } - subject {c} - it {expect(a).to eq b} - it {expect(b.ndims).to eq a.ndims} - it {expect(b.dimension).to eq a.dimension} - end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 273228a..48220fd 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1 +1,3 @@ require './lib/arrayfire' + +require 'rspec/its' From 12ea9bcb7b2e15a8bbc8d52f7f363e5a98a287f6 Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Fri, 7 Jul 2017 16:14:14 +0530 Subject: [PATCH 069/125] dims and elements attributes --- ext/mri/arrayfire.c | 4 +++- ext/mri/cmodules/array.c | 15 ++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index a6ea721..9a3019e 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -320,8 +320,10 @@ void Init_arrayfire() { rb_define_method(Af_Array, "is_bool", (METHOD)arf_is_bool, 0); rb_define_method(Af_Array, "is_sparse", (METHOD)arf_is_sparse, 0); - rb_define_alias(Af_Array, "to_cpu", "get_data_ptr"); rb_define_alias(Af_Array, "ndims", "get_numdims"); + rb_define_alias(Af_Array, "dims", "get_dims"); + rb_define_alias(Af_Array, "to_cpu", "get_data_ptr"); + rb_define_alias(Af_Array, "elements", "get_data_ptr"); rb_define_method(Af_Array, "+",(METHOD)arf_ew_add,1); rb_define_method(Af_Array, "-",(METHOD)arf_ew_subtract,1); diff --git a/ext/mri/cmodules/array.c b/ext/mri/cmodules/array.c index a357e3a..38fad07 100644 --- a/ext/mri/cmodules/array.c +++ b/ext/mri/cmodules/array.c @@ -134,12 +134,21 @@ static VALUE arf_get_type(VALUE self){ static VALUE arf_get_dims(VALUE self){ afstruct* input; - dim_t d0, d1, d2, d3; + uint ndims; + dim_t* dims = (dim_t*)malloc(ndims * sizeof(dim_t));; Data_Get_Struct(self, afstruct, input); - af_get_dims(&d0, &d1, &d2, &d3, input->carray); - return Qnil; + af_get_numdims(&ndims, input->carray); + + af_get_dims(&dims[0], &dims[1], &dims[2], &dims[3], input->carray); + + VALUE* array = ALLOC_N(VALUE, ndims); + for (size_t index = 0; index < ndims; index++){ + array[index] = UINT2NUM(dims[index]); + } + + return rb_ary_new4(ndims, array); } static VALUE arf_get_numdims(VALUE self){ From e8606c9f13fd8b3be811271a34a8be8b93a8d4cd Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Wed, 12 Jul 2017 21:59:14 +0530 Subject: [PATCH 070/125] added contributing guideline --- CONTRIBUTING.md | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 CONTRIBUTING.md diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..57a50e6 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,40 @@ +# Contributing to ArrayFire + +## Reporting Issues + +We both value and encourage the community to provide feedback about using ArrayFire and the issues they are facing. +The more detailed the information, the easier it is for the developers to resolve the issue. + +Issues can span a variety of topics including: +- Feature requests +- Bug reports +- Build Issues +- Performance improvements +- New hardware / backend support + +We use the github [issue tracker](https://github.com/arrayfire/arrayfire-rb/issues?state=open) to track our issues. Technical issues can also be discussed on our [user forum](https://groups.google.com/forum/#!forum/arrayfire-users). + +## Contributing Code + +If you want to contribute code, we suggest you use the one of the following methods. + +- [Using Github](https://github.com/arrayfire/arrayfire-rb/wiki/Contribute-code-using-github) +- [Using Email](https://github.com/arrayfire/arrayfire-rb/wiki/Contribute-code-using-email) + +Key areas of code contributions include: +- [New features](https://github.com/arrayfire/arrayfire-rb/issues?q=is%3Aopen+is%3Aissue+label%3Afeature) +- [Bug fixes](https://github.com/arrayfire/arrayfire-rb/labels/bug) +- [Style changes](https://github.com/arrayfire/arrayfire-rb/labels/style) +- [Performance improvements](https://github.com/arrayfire/arrayfire-rb/labels/style) +- [New tests](https://github.com/arrayfire/arrayfire-rb/labels/test) +- New examples! + +## ArrayFire Based Projects + +You can also contribute to ArrayFire by helping out projects that use ArrayFire! For our part, in addition to the ArrayFire library we are also in the process of adding native bindings for numerous language. We currently support + +- [Java](https://github.com/arrayfire/arrayfire_java) +- [R](https://github.com/arrayfire/arrayfire_r) +- [Fortran](https://github.com/arrayfire/arrayfire_fortran) + +If you are experienced in any of these languages, you can help us improve these language bindings. If you prefer a different language that is not in the list, dive in and create a new repo! From 5efc291fe85df256c2d0cd9641f071032ecec4ab Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Wed, 12 Jul 2017 22:01:03 +0530 Subject: [PATCH 071/125] added rdoc task --- Rakefile | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Rakefile b/Rakefile index 08c84f8..84d8a15 100644 --- a/Rakefile +++ b/Rakefile @@ -21,3 +21,9 @@ end def run(*cmd) sh(cmd.join(' ')) end + +require "rdoc/task" +RDoc::Task.new do |rdoc| + rdoc.main = "README.md" + rdoc.rdoc_files.include(%w{README.md LICENSE CONTRIBUTING.md lib ext}) +end From 9e56cb54d76d401879de0978eb6b52348470b46c Mon Sep 17 00:00:00 2001 From: prasun Date: Fri, 21 Jul 2017 20:31:58 +0530 Subject: [PATCH 072/125] switch to minitest --- Rakefile | 12 +++++++++++- arrayfire.gemspec | 5 ++--- spec/spec_helper.rb | 3 --- spec/blas_spec.rb => test/blas_test.rb | 0 spec/creation_spec.rb => test/creation_test.rb | 0 test/test_helper.rb | 4 ++++ 6 files changed, 17 insertions(+), 7 deletions(-) delete mode 100644 spec/spec_helper.rb rename spec/blas_spec.rb => test/blas_test.rb (100%) rename spec/creation_spec.rb => test/creation_test.rb (100%) create mode 100644 test/test_helper.rb diff --git a/Rakefile b/Rakefile index 84d8a15..a210642 100644 --- a/Rakefile +++ b/Rakefile @@ -1,5 +1,8 @@ +require "bundler/gem_tasks" require 'rake' require 'rake/extensiontask' +require "rake/testtask" +require "rdoc/task" Rake::ExtensionTask.new do |ext| ext.name = 'arrayfire' @@ -22,8 +25,15 @@ def run(*cmd) sh(cmd.join(' ')) end -require "rdoc/task" RDoc::Task.new do |rdoc| rdoc.main = "README.md" rdoc.rdoc_files.include(%w{README.md LICENSE CONTRIBUTING.md lib ext}) end + +Rake::TestTask.new(:test) do |t| + t.libs << "test" + t.libs << "lib" + t.test_files = FileList['test/**/*_test.rb'] +end + +task :default => :test diff --git a/arrayfire.gemspec b/arrayfire.gemspec index f328160..b2c2ece 100644 --- a/arrayfire.gemspec +++ b/arrayfire.gemspec @@ -20,6 +20,5 @@ Gem::Specification.new do |gem| gem.add_development_dependency 'rake', '~>10.3' gem.add_development_dependency 'rake-compiler', '~>0.8' gem.add_development_dependency 'rdoc', '~>4.0', '>=4.0.1' - gem.add_development_dependency 'rspec', '~>2.14' - gem.add_development_dependency 'rspec-its' -end + gem.add_development_dependency "minitest", "~> 5.0" +end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb deleted file mode 100644 index 48220fd..0000000 --- a/spec/spec_helper.rb +++ /dev/null @@ -1,3 +0,0 @@ -require './lib/arrayfire' - -require 'rspec/its' diff --git a/spec/blas_spec.rb b/test/blas_test.rb similarity index 100% rename from spec/blas_spec.rb rename to test/blas_test.rb diff --git a/spec/creation_spec.rb b/test/creation_test.rb similarity index 100% rename from spec/creation_spec.rb rename to test/creation_test.rb diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 0000000..8eae370 --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,4 @@ +$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) +require 'arrayfire' + +require 'minitest/autorun' From 2f30174b0eedf7fb6aa5a511da1b0610cddf6118 Mon Sep 17 00:00:00 2001 From: prasun Date: Fri, 21 Jul 2017 20:35:07 +0530 Subject: [PATCH 073/125] modify tests according to minitest --- test/blas_test.rb | 22 +++++++++++++--------- test/creation_test.rb | 29 +++++++++++++++-------------- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/test/blas_test.rb b/test/blas_test.rb index 4e20d29..8fb8993 100644 --- a/test/blas_test.rb +++ b/test/blas_test.rb @@ -1,11 +1,15 @@ -require 'spec_helper' - -describe ArrayFire::BLAS do - context '#matmul' do - let(:a) { ArrayFire::Af_Array.new 2, [2,2],[1,2,3,4] } - let(:b) { ArrayFire::Af_Array.new 2, [2,2],[1,2,3,4] } - let(:c) { ArrayFire::Af_Array.new 2, [2,2],[7.0,10.0,15.0,22.0] } - subject {ArrayFire::BLAS.matmul(a,b)} - it {is_expected.to eq c} +require 'test_helper' + +class ArrayFire::BLASTest < Minitest::Test + + def setup + @a = ArrayFire::Af_Array.new 2, [2,2],[1,2,3,4] + @b = ArrayFire::Af_Array.new 2, [2,2],[1,2,3,4] end + + def test_matmul + c = ArrayFire::Af_Array.new 2, [2,2],[7.0,10.0,15.0,22.0] + assert_equal c, ArrayFire::BLAS.matmul(@a,@b) + end + end diff --git a/test/creation_test.rb b/test/creation_test.rb index 5d03d25..4162c2e 100644 --- a/test/creation_test.rb +++ b/test/creation_test.rb @@ -1,20 +1,21 @@ -require 'spec_helper' +require 'test_helper' -describe ArrayFire::Af_Array do - context '#initialize' do - let(:i) { ArrayFire::Af_Array.new 2, [2,2],[1,2,3,4] } - subject { i } - its(:ndims) { is_expected.to eq 2 } - its(:dims) { is_expected.to eq [2,2] } - its(:elements) {is_expected.to eq [1,2,3,4]} +class ArrayFire::Af_ArrayTest < Minitest::Test + + def setup + @i = ArrayFire::Af_Array.new 2, [2,2],[1,2,3,4] + end + + def test_ndims + assert_equal 2, @i.ndims + end + + def test_dims + assert_equal [2,2], @i.dims end - context '#addition' do - let(:a) { ArrayFire::Af_Array.new 2, [2,2],[1,2,3,4] } - let(:b) { ArrayFire::Af_Array.new 2, [2,2],[1,2,3,4] } - let(:c) { ArrayFire::Af_Array.new 2, [2,2],[2,4,6,8] } - subject {a+b} - it {is_expected.to eq c} + def test_elements + assert_equal [1, 2, 3, 4], @i.elements end end From d08b71de51641717b5d26d70f704a47f090b6508 Mon Sep 17 00:00:00 2001 From: prasun Date: Fri, 21 Jul 2017 20:36:50 +0530 Subject: [PATCH 074/125] run test after compile --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b9d65a3..a3ee453 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,12 @@ cd arrayfire/ gem install bundler bundle install rake compile -rspec +``` + +Run the tests using + +```sh +rake test ``` If you want to try out the code without installing: From 170101d3a81c69da230e628c4c893a72d393e7ec Mon Sep 17 00:00:00 2001 From: prasun Date: Fri, 21 Jul 2017 20:38:32 +0530 Subject: [PATCH 075/125] added arith test --- test/arith_test.rb | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 test/arith_test.rb diff --git a/test/arith_test.rb b/test/arith_test.rb new file mode 100644 index 0000000..af53d5c --- /dev/null +++ b/test/arith_test.rb @@ -0,0 +1,29 @@ +require 'test_helper' + +class ArrayFire::ArithTest < Minitest::Test + + def setup + @a = ArrayFire::Af_Array.new 2, [2,2],[1,2,3,4] + @b = ArrayFire::Af_Array.new 2, [2,2],[2,4,6,8] + end + + def test_addition + c = ArrayFire::Af_Array.new 2, [2,2],[3,6,9,12] + assert_equal c, @a + @b + end + + def test_subtraction + assert_equal @a, @b - @a + end + + def test_multiplication + c = ArrayFire::Af_Array.new 2, [2,2],[2,8,18,32] + assert_equal @a, @b - @a + end + + def test_division + c = ArrayFire::Af_Array.new 2, [2,2],[2,2,2,2] + assert_equal c, @b / @a + end + +end From dcf1c5407e511eb18c03abf7c3a1502e3f647edb Mon Sep 17 00:00:00 2001 From: prasun Date: Sun, 23 Jul 2017 19:52:25 +0530 Subject: [PATCH 076/125] added algorithm tests --- ext/mri/arrayfire.c | 26 +++++++++++------------ test/algorithm_test.rb | 48 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 13 deletions(-) create mode 100644 test/algorithm_test.rb diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 9a3019e..e4d1cf4 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -364,12 +364,12 @@ void Init_arrayfire() { rb_define_method(Af_Array, "ceil", (METHOD)arf_unary_ceil, 0); Algorithm = rb_define_class_under(ArrayFire, "Algorithm", rb_cObject); - rb_define_singleton_method(Algorithm, "sum", (METHOD)arf_sum, 0); - rb_define_singleton_method(Algorithm, "sum_nan", (METHOD)arf_sum_nan, 0); - rb_define_singleton_method(Algorithm, "product", (METHOD)arf_product, 0); - rb_define_singleton_method(Algorithm, "product_nan", (METHOD)arf_product_nan, 0); - rb_define_singleton_method(Algorithm, "min", (METHOD)arf_min, 0); - rb_define_singleton_method(Algorithm, "max", (METHOD)arf_max, 0); + rb_define_singleton_method(Algorithm, "sum", (METHOD)arf_sum, 2); + rb_define_singleton_method(Algorithm, "sum_nan", (METHOD)arf_sum_nan, 3); + rb_define_singleton_method(Algorithm, "product", (METHOD)arf_product, 2); + rb_define_singleton_method(Algorithm, "product_nan", (METHOD)arf_product_nan, 3); + rb_define_singleton_method(Algorithm, "min", (METHOD)arf_min, 2); + rb_define_singleton_method(Algorithm, "max", (METHOD)arf_max, 2); rb_define_singleton_method(Algorithm, "all_true", (METHOD)arf_all_true, 0); rb_define_singleton_method(Algorithm, "any_true", (METHOD)arf_any_true, 0); rb_define_singleton_method(Algorithm, "count", (METHOD)arf_count, 0); @@ -496,14 +496,14 @@ void Init_arrayfire() { rb_define_singleton_method(Data, "replace_scalar", (METHOD)arf_replace_scalar, 3); Lapack = rb_define_class_under(ArrayFire, "LAPACK", rb_cObject); - rb_define_singleton_method(Lapack, "svd", (METHOD)arf_svd, 0); - rb_define_singleton_method(Lapack, "svd_inplace", (METHOD)arf_svd_inplace, 0); - rb_define_singleton_method(Lapack, "lu", (METHOD)arf_lu, 0); - rb_define_singleton_method(Lapack, "lu_inplace", (METHOD)arf_lu_inplace, 0); - rb_define_singleton_method(Lapack, "qr", (METHOD)arf_qr, 0); - rb_define_singleton_method(Lapack, "qr_inplace", (METHOD)arf_qr_inplace, 0); + rb_define_singleton_method(Lapack, "svd", (METHOD)arf_svd, 1); + rb_define_singleton_method(Lapack, "svd_inplace", (METHOD)arf_svd_inplace, 1); + rb_define_singleton_method(Lapack, "lu", (METHOD)arf_lu, 1); + rb_define_singleton_method(Lapack, "lu_inplace", (METHOD)arf_lu_inplace, 1); + rb_define_singleton_method(Lapack, "qr", (METHOD)arf_qr, 1); + rb_define_singleton_method(Lapack, "qr_inplace", (METHOD)arf_qr_inplace, 1); rb_define_singleton_method(Lapack, "cholesky", (METHOD)arf_cholesky, 1); - rb_define_singleton_method(Lapack, "cholesky_inplace", (METHOD)arf_cholesky_inplace, 0); + rb_define_singleton_method(Lapack, "cholesky_inplace", (METHOD)arf_cholesky_inplace, 1); rb_define_singleton_method(Lapack, "solve", (METHOD)arf_solve, 0); rb_define_singleton_method(Lapack, "solve_lu", (METHOD)arf_solve_lu, 0); rb_define_singleton_method(Lapack, "inverse", (METHOD)arf_inverse, 0); diff --git a/test/algorithm_test.rb b/test/algorithm_test.rb new file mode 100644 index 0000000..9fd9b89 --- /dev/null +++ b/test/algorithm_test.rb @@ -0,0 +1,48 @@ +require 'test_helper' + +class ArrayFire::AlgorithmTest < Minitest::Test + + def setup + @input = ArrayFire::Af_Array.new 2, [3,3], [4, 1, 5, 6, -11, 9 , -22, 11, 1] + @input2 = ArrayFire::Af_Array.new 2, [3,3], [4, 1, 5, 6, -11, 9 , -22, 11, 0] + @inf_array = @input / @input2 + @nan_array = @inf_array / @inf_array + end + + def test_sum + sum_arr = ArrayFire::Af_Array.new 1, [3], [-12.0, 1.0, 15.0] + result = ArrayFire::Algorithm.sum(@input, 1) + assert_equal(sum_arr, result) + end + + def test_sum_nan + sum_nan_arr = ArrayFire::Af_Array.new 1, [3], [3.0, 3.0, 8.0] + result = ArrayFire::Algorithm.sum_nan(@nan_array, 1, 6) + assert_equal(sum_nan_arr, result) + end + + def test_product + product_arr = ArrayFire::Af_Array.new 1, [3], [-528.0, -121.0, 45.0] + result = ArrayFire::Algorithm.product(@input, 1) + assert_equal(product_arr, result) + end + + def test_product_nan + product_nan_arr = ArrayFire::Af_Array.new 1, [3], [1.0, 1.0, 6.0] + result = ArrayFire::Algorithm.product_nan(@nan_array, 1, 6) + assert_equal(product_nan_arr, result) + end + + def test_min + min_arr = ArrayFire::Af_Array.new 1, [3], [-22.0, -11.0, 1] + result = ArrayFire::Algorithm.min(@input, 1) + assert_equal(min_arr, result) + end + + def test_max + max_arr = ArrayFire::Af_Array.new 1, [3], [6.0, 11.0, 9.0] + result = ArrayFire::Algorithm.max(@input, 1) + assert_equal(max_arr, result) + end + +end From a4b22e470fc4e2943bc730ffe66dbfb4b5f427dd Mon Sep 17 00:00:00 2001 From: prasun Date: Sun, 23 Jul 2017 20:08:26 +0530 Subject: [PATCH 077/125] added more tests --- ext/mri/arrayfire.c | 6 +++--- ext/mri/cmodules/algorithm.c | 6 +++--- test/algorithm_test.rb | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index e4d1cf4..a9e7411 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -370,9 +370,9 @@ void Init_arrayfire() { rb_define_singleton_method(Algorithm, "product_nan", (METHOD)arf_product_nan, 3); rb_define_singleton_method(Algorithm, "min", (METHOD)arf_min, 2); rb_define_singleton_method(Algorithm, "max", (METHOD)arf_max, 2); - rb_define_singleton_method(Algorithm, "all_true", (METHOD)arf_all_true, 0); - rb_define_singleton_method(Algorithm, "any_true", (METHOD)arf_any_true, 0); - rb_define_singleton_method(Algorithm, "count", (METHOD)arf_count, 0); + rb_define_singleton_method(Algorithm, "all_true", (METHOD)arf_all_true, 2); + rb_define_singleton_method(Algorithm, "any_true", (METHOD)arf_any_true, 2); + rb_define_singleton_method(Algorithm, "count", (METHOD)arf_count, 2); rb_define_singleton_method(Algorithm, "sum_all", (METHOD)arf_sum_all, 0); rb_define_singleton_method(Algorithm, "sum_nan_all", (METHOD)arf_sum_nan_all, 0); rb_define_singleton_method(Algorithm, "product_all", (METHOD)arf_product_all, 0); diff --git a/ext/mri/cmodules/algorithm.c b/ext/mri/cmodules/algorithm.c index b493e5b..b4fbeba 100644 --- a/ext/mri/cmodules/algorithm.c +++ b/ext/mri/cmodules/algorithm.c @@ -70,7 +70,7 @@ static VALUE arf_all_true(VALUE self, VALUE array_val, VALUE dim_val){ Data_Get_Struct(array_val, afstruct, input); - af_sum(&output->carray, input->carray, FIX2INT(dim_val)); + af_all_true(&output->carray, input->carray, FIX2INT(dim_val)); return Data_Wrap_Struct(CLASS_OF(array_val), NULL, arf_free, output); } @@ -81,7 +81,7 @@ static VALUE arf_any_true(VALUE self, VALUE array_val, VALUE dim_val){ Data_Get_Struct(array_val, afstruct, input); - af_sum(&output->carray, input->carray, FIX2INT(dim_val)); + af_any_true(&output->carray, input->carray, FIX2INT(dim_val)); return Data_Wrap_Struct(CLASS_OF(array_val), NULL, arf_free, output); } @@ -92,7 +92,7 @@ static VALUE arf_count(VALUE self, VALUE array_val, VALUE dim_val){ Data_Get_Struct(array_val, afstruct, input); - af_sum(&output->carray, input->carray, FIX2INT(dim_val)); + af_count(&output->carray, input->carray, FIX2INT(dim_val)); return Data_Wrap_Struct(CLASS_OF(array_val), NULL, arf_free, output); } diff --git a/test/algorithm_test.rb b/test/algorithm_test.rb index 9fd9b89..a0537a1 100644 --- a/test/algorithm_test.rb +++ b/test/algorithm_test.rb @@ -45,4 +45,22 @@ def test_max assert_equal(max_arr, result) end + def test_all_true + sum_arr = ArrayFire::Af_Array.new 1, [3], [1.0, 1.0, 1.0] + result = ArrayFire::Algorithm.all_true(@input, 1) + assert_equal(sum_arr, result) + end + + def test_any_true + sum_arr = ArrayFire::Af_Array.new 1, [3], [1.0, 1.0, 1.0] + result = ArrayFire::Algorithm.any_true(@input, 1) + assert_equal(sum_arr, result) + end + + def test_count + sum_arr = ArrayFire::Af_Array.new 1, [3], [3.0, 3.0, 3.0] + result = ArrayFire::Algorithm.count(@input, 1) + assert_equal(sum_arr, result) + end + end From abba793ea9ece342a1a6ac1e222627f51d01a4a7 Mon Sep 17 00:00:00 2001 From: prasun Date: Sun, 23 Jul 2017 20:27:14 +0530 Subject: [PATCH 078/125] modify test parameters and add more tests --- ext/mri/arrayfire.c | 18 +++++++-------- ext/mri/cmodules/algorithm.c | 22 +++++++++--------- test/algorithm_test.rb | 44 +++++++++++++++++++++++++++++++----- 3 files changed, 58 insertions(+), 26 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index a9e7411..0ba8448 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -373,15 +373,15 @@ void Init_arrayfire() { rb_define_singleton_method(Algorithm, "all_true", (METHOD)arf_all_true, 2); rb_define_singleton_method(Algorithm, "any_true", (METHOD)arf_any_true, 2); rb_define_singleton_method(Algorithm, "count", (METHOD)arf_count, 2); - rb_define_singleton_method(Algorithm, "sum_all", (METHOD)arf_sum_all, 0); - rb_define_singleton_method(Algorithm, "sum_nan_all", (METHOD)arf_sum_nan_all, 0); - rb_define_singleton_method(Algorithm, "product_all", (METHOD)arf_product_all, 0); - rb_define_singleton_method(Algorithm, "product_nan_all", (METHOD)arf_product_nan_all, 0); - rb_define_singleton_method(Algorithm, "min_all", (METHOD)arf_min_all, 0); - rb_define_singleton_method(Algorithm, "max_all", (METHOD)arf_max_all, 0); - rb_define_singleton_method(Algorithm, "all_true_all", (METHOD)arf_all_true_all, 0); - rb_define_singleton_method(Algorithm, "any_true_all", (METHOD)arf_any_true_all, 0); - rb_define_singleton_method(Algorithm, "count_all", (METHOD)arf_count_all, 0); + rb_define_singleton_method(Algorithm, "sum_all", (METHOD)arf_sum_all, 1); + rb_define_singleton_method(Algorithm, "sum_nan_all", (METHOD)arf_sum_nan_all, 2); + rb_define_singleton_method(Algorithm, "product_all", (METHOD)arf_product_all, 1); + rb_define_singleton_method(Algorithm, "product_nan_all", (METHOD)arf_product_nan_all, 2); + rb_define_singleton_method(Algorithm, "min_all", (METHOD)arf_min_all, 1); + rb_define_singleton_method(Algorithm, "max_all", (METHOD)arf_max_all, 1); + rb_define_singleton_method(Algorithm, "all_true_all", (METHOD)arf_all_true_all, 1); + rb_define_singleton_method(Algorithm, "any_true_all", (METHOD)arf_any_true_all, 1); + rb_define_singleton_method(Algorithm, "count_all", (METHOD)arf_count_all, 1); rb_define_singleton_method(Algorithm, "imin", (METHOD)arf_imin, 0); rb_define_singleton_method(Algorithm, "imax", (METHOD)arf_imax, 0); rb_define_singleton_method(Algorithm, "imin_all", (METHOD)arf_imin_all, 0); diff --git a/ext/mri/cmodules/algorithm.c b/ext/mri/cmodules/algorithm.c index b4fbeba..73b783d 100644 --- a/ext/mri/cmodules/algorithm.c +++ b/ext/mri/cmodules/algorithm.c @@ -105,7 +105,7 @@ static VALUE arf_sum_all(VALUE self, VALUE array_val){ af_sum_all(&real_part, &imag_part, input->carray); - return NUM2DBL(real_part); + return DBL2NUM(real_part); } static VALUE arf_sum_nan_all(VALUE self, VALUE array_val, VALUE nan_val){ @@ -116,7 +116,7 @@ static VALUE arf_sum_nan_all(VALUE self, VALUE array_val, VALUE nan_val){ af_sum_nan_all(&real_part, &imag_part, input->carray, NUM2DBL(nan_val)); - return NUM2DBL(real_part); + return DBL2NUM(real_part); } static VALUE arf_product_all(VALUE self, VALUE array_val){ @@ -127,7 +127,7 @@ static VALUE arf_product_all(VALUE self, VALUE array_val){ af_product_all(&real_part, &imag_part, input->carray); - return NUM2DBL(real_part); + return DBL2NUM(real_part); } static VALUE arf_product_nan_all(VALUE self, VALUE array_val, VALUE nan_val){ @@ -138,7 +138,7 @@ static VALUE arf_product_nan_all(VALUE self, VALUE array_val, VALUE nan_val){ af_product_nan_all(&real_part, &imag_part, input->carray, NUM2DBL(nan_val)); - return NUM2DBL(real_part); + return DBL2NUM(real_part); } static VALUE arf_min_all(VALUE self, VALUE array_val){ @@ -149,7 +149,7 @@ static VALUE arf_min_all(VALUE self, VALUE array_val){ af_min_all(&real_part, &imag_part, input->carray); - return NUM2DBL(real_part); + return DBL2NUM(real_part); } static VALUE arf_max_all(VALUE self, VALUE array_val){ @@ -160,7 +160,7 @@ static VALUE arf_max_all(VALUE self, VALUE array_val){ af_max_all(&real_part, &imag_part, input->carray); - return NUM2DBL(real_part); + return DBL2NUM(real_part); } static VALUE arf_all_true_all(VALUE self, VALUE array_val){ @@ -171,7 +171,7 @@ static VALUE arf_all_true_all(VALUE self, VALUE array_val){ af_all_true_all(&real_part, &imag_part, input->carray); - return NUM2DBL(real_part); + return DBL2NUM(real_part); } static VALUE arf_any_true_all(VALUE self, VALUE array_val){ @@ -182,7 +182,7 @@ static VALUE arf_any_true_all(VALUE self, VALUE array_val){ af_any_true_all(&real_part, &imag_part, input->carray); - return NUM2DBL(real_part); + return DBL2NUM(real_part); } static VALUE arf_count_all(VALUE self, VALUE array_val){ @@ -193,7 +193,7 @@ static VALUE arf_count_all(VALUE self, VALUE array_val){ af_count_all(&real_part, &imag_part, input->carray); - return NUM2DBL(real_part); + return DBL2NUM(real_part); } static VALUE arf_imin(VALUE self, VALUE array_val, VALUE dim_val){ @@ -226,7 +226,7 @@ static VALUE arf_imin_all(VALUE self, VALUE array_val){ Data_Get_Struct(array_val, afstruct, input); af_imin_all(&real_part, &imag_part, &idx, input->carray); - return NUM2DBL(real_part); + return DBL2NUM(real_part); } static VALUE arf_imax_all(VALUE self, VALUE array_val){ @@ -237,7 +237,7 @@ static VALUE arf_imax_all(VALUE self, VALUE array_val){ Data_Get_Struct(array_val, afstruct, input); af_imax_all(&real_part, &imag_part, &idx, input->carray); - return NUM2DBL(real_part); + return DBL2NUM(real_part); } static VALUE arf_accum(VALUE self, VALUE array_val, VALUE dim_val){ diff --git a/test/algorithm_test.rb b/test/algorithm_test.rb index a0537a1..64d54a1 100644 --- a/test/algorithm_test.rb +++ b/test/algorithm_test.rb @@ -46,21 +46,53 @@ def test_max end def test_all_true - sum_arr = ArrayFire::Af_Array.new 1, [3], [1.0, 1.0, 1.0] + all_true_arr = ArrayFire::Af_Array.new 1, [3], [1.0, 1.0, 1.0] result = ArrayFire::Algorithm.all_true(@input, 1) - assert_equal(sum_arr, result) + assert_equal(all_true_arr, result) end def test_any_true - sum_arr = ArrayFire::Af_Array.new 1, [3], [1.0, 1.0, 1.0] + any_true_arr = ArrayFire::Af_Array.new 1, [3], [1.0, 1.0, 1.0] result = ArrayFire::Algorithm.any_true(@input, 1) - assert_equal(sum_arr, result) + assert_equal(any_true_arr, result) end def test_count - sum_arr = ArrayFire::Af_Array.new 1, [3], [3.0, 3.0, 3.0] + count_arr = ArrayFire::Af_Array.new 1, [3], [3.0, 3.0, 3.0] result = ArrayFire::Algorithm.count(@input, 1) - assert_equal(sum_arr, result) + assert_equal(count_arr, result) + end + + def test_sum_all + assert_equal 4, ArrayFire::Algorithm.sum_all(@input) + end + + def test_sum_nan_all + assert_equal 18, ArrayFire::Algorithm.sum_nan_all(@nan_array, 10) + end + + def test_product_all + assert_equal 2874960.0, ArrayFire::Algorithm.product_all(@input) + end + + def test_product_nan_all + assert_equal 10, ArrayFire::Algorithm.product_nan_all(@nan_array, 10) + end + + def test_min_all + assert_equal -22, ArrayFire::Algorithm.min_all(@input) + end + + def test_max_all + assert_equal 11, ArrayFire::Algorithm.max_all(@input) + end + + def test_all_true_all + assert_equal 1, ArrayFire::Algorithm.all_true_all(@input) + end + + def test_any_true_all + assert_equal 1, ArrayFire::Algorithm.any_true_all(@input) end end From 397f40db18963b35ef7ff9158d758b614b8a49e6 Mon Sep 17 00:00:00 2001 From: prasun Date: Sun, 23 Jul 2017 20:30:47 +0530 Subject: [PATCH 079/125] remove unused declarations --- ext/mri/ruby_arrayfire.h | 9 --------- 1 file changed, 9 deletions(-) diff --git a/ext/mri/ruby_arrayfire.h b/ext/mri/ruby_arrayfire.h index 77c94d8..883339d 100644 --- a/ext/mri/ruby_arrayfire.h +++ b/ext/mri/ruby_arrayfire.h @@ -41,15 +41,6 @@ extern "C" { #endif void Init_arrayfire(); - static void test(); - // External API - static void createArray(afstruct *afarray); - static void hostArray(afstruct *afarray); - static void add(afstruct *result, afstruct *left, afstruct *right); - static void matmul(afstruct *result, afstruct *left, afstruct *right); - static void cholesky_(afstruct *result, afstruct *matrix); - static void inverse_(afstruct *result, afstruct *matrix); - static double norm_(afstruct *matrix); #ifdef __cplusplus } From a2463648373428bef7023c075f3171335d0b9778 Mon Sep 17 00:00:00 2001 From: prasun Date: Sun, 23 Jul 2017 22:00:09 +0530 Subject: [PATCH 080/125] get rid of warnings --- ext/mri/arrayfire.c | 10 +++------- ext/mri/cmodules/array.c | 24 ++++++++++++------------ ext/mri/cmodules/data.c | 10 +++++----- 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 0ba8448..8a1cb66 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -208,8 +208,6 @@ static VALUE arf_randn(VALUE self); static VALUE arf_set_seed(VALUE self); static VALUE arf_get_seed(VALUE self); -static size_t* interpret_shape(VALUE arg, size_t* dim); - #define DEF_ELEMENTWISE_RUBY_ACCESSOR(name, oper) \ static VALUE arf_ew_##name(VALUE left_val, VALUE right_val) { \ afstruct* left; \ @@ -537,12 +535,12 @@ VALUE arf_init(int argc, VALUE* argv, VALUE self) dim_t ndims = (dim_t)FIX2LONG(argv[0]); dim_t* dimensions = (dim_t*)malloc(ndims * sizeof(dim_t)); dim_t count = 1; - for (size_t index = 0; index < ndims; index++) { + for (dim_t index = 0; index < ndims; index++) { dimensions[index] = (dim_t)FIX2LONG(RARRAY_AREF(argv[1], index)); count *= dimensions[index]; } float* host_array = (float*)malloc(count * sizeof(float)); - for (size_t index = 0; index < count; index++) { + for (dim_t index = 0; index < count; index++) { host_array[index] = (float)NUM2DBL(RARRAY_AREF(argv[2], index)); } @@ -568,8 +566,6 @@ static void arf_free(afstruct* af) free(af); } -DEF_ELEMENTWISE_RUBY_ACCESSOR(ADD, add) - static VALUE arf_eqeq(VALUE left_val, VALUE right_val) { afstruct* left; afstruct* right; @@ -583,7 +579,7 @@ static VALUE arf_eqeq(VALUE left_val, VALUE right_val) { bool* data = (bool*)malloc(count * sizeof(bool)); af_get_data_ptr(data, result->carray); - for (size_t index = 0; index < (size_t)count; index++){ + for (dim_t index = 0; index < count; index++){ if(!data[index]){ return Qfalse; } diff --git a/ext/mri/cmodules/array.c b/ext/mri/cmodules/array.c index 38fad07..e368fb1 100644 --- a/ext/mri/cmodules/array.c +++ b/ext/mri/cmodules/array.c @@ -3,12 +3,12 @@ static VALUE arf_create_array(int argc, VALUE* argv){ dim_t ndims = (dim_t)FIX2LONG(argv[0]); dim_t* dimensions = (dim_t*)malloc(ndims * sizeof(dim_t)); dim_t count = 1; - for (size_t index = 0; index < ndims; index++) { + for (dim_t index = 0; index < ndims; index++) { dimensions[index] = (dim_t)FIX2LONG(RARRAY_AREF(argv[1], index)); count *= dimensions[index]; } float* host_array = (float*)malloc(count * sizeof(float)); - for (size_t index = 0; index < count; index++) { + for (dim_t index = 0; index < count; index++) { host_array[index] = (float)NUM2DBL(RARRAY_AREF(argv[2], index)); } @@ -24,12 +24,12 @@ static VALUE arf_create_handle(int argc, VALUE* argv){ dim_t ndims = (dim_t)FIX2LONG(argv[0]); dim_t* dimensions = (dim_t*)malloc(ndims * sizeof(dim_t)); dim_t count = 1; - for (size_t index = 0; index < ndims; index++) { + for (dim_t index = 0; index < ndims; index++) { dimensions[index] = (dim_t)FIX2LONG(RARRAY_AREF(argv[1], index)); count *= dimensions[index]; } float* host_array = (float*)malloc(count * sizeof(float)); - for (size_t index = 0; index < count; index++) { + for (dim_t index = 0; index < count; index++) { host_array[index] = (float)NUM2DBL(RARRAY_AREF(argv[2], index)); } @@ -44,9 +44,9 @@ static VALUE arf_copy_array(VALUE self){ afstruct* array_val; afstruct* result = ALLOC(afstruct); - Data_Get_Struct(self, afstruct, result); + Data_Get_Struct(self, afstruct, array_val); - af_copy_array(&result->carray, result->carray); + af_copy_array(&result->carray, array_val->carray); return Data_Wrap_Struct(CLASS_OF(self), NULL, arf_free, result); } @@ -66,7 +66,7 @@ static VALUE arf_get_data_ptr(VALUE self){ af_get_data_ptr(data, input->carray); VALUE* array = ALLOC_N(VALUE, count); - for (size_t index = 0; index < (size_t)count; index++){ + for (dim_t index = 0; index < count; index++){ array[index] = DBL2NUM(data[index]); } @@ -134,17 +134,17 @@ static VALUE arf_get_type(VALUE self){ static VALUE arf_get_dims(VALUE self){ afstruct* input; - uint ndims; - dim_t* dims = (dim_t*)malloc(ndims * sizeof(dim_t));; - Data_Get_Struct(self, afstruct, input); + uint ndims; af_get_numdims(&ndims, input->carray); + dim_t* dims = (dim_t*)malloc(ndims * sizeof(dim_t)); + af_get_dims(&dims[0], &dims[1], &dims[2], &dims[3], input->carray); VALUE* array = ALLOC_N(VALUE, ndims); - for (size_t index = 0; index < ndims; index++){ + for (dim_t index = 0; index < ndims; index++){ array[index] = UINT2NUM(dims[index]); } @@ -298,6 +298,6 @@ static VALUE arf_is_sparse(VALUE self){ Data_Get_Struct(self, afstruct, input); //FIXME - // af_is_bool(&result, input->carray); + af_is_bool(&result, input->carray); return result ? Qtrue : Qfalse; } diff --git a/ext/mri/cmodules/data.c b/ext/mri/cmodules/data.c index ed3a09e..d27c0c9 100644 --- a/ext/mri/cmodules/data.c +++ b/ext/mri/cmodules/data.c @@ -4,7 +4,7 @@ static VALUE arf_constant(int argc, VALUE* argv){ dim_t ndims = (dim_t)FIX2LONG(argv[0]); dim_t* dimensions = (dim_t*)malloc(ndims * sizeof(dim_t)); dim_t count = 1; - for (size_t index = 0; index < ndims; index++) { + for (dim_t index = 0; index < ndims; index++) { dimensions[index] = (dim_t)FIX2LONG(RARRAY_AREF(argv[1], index)); count *= dimensions[index]; } @@ -25,7 +25,7 @@ static VALUE arf_constant_long(int argc, VALUE* argv){ dim_t ndims = (dim_t)FIX2LONG(argv[0]); dim_t* dimensions = (dim_t*)malloc(ndims * sizeof(dim_t)); dim_t count = 1; - for (size_t index = 0; index < ndims; index++) { + for (dim_t index = 0; index < ndims; index++) { dimensions[index] = (dim_t)FIX2LONG(RARRAY_AREF(argv[1], index)); count *= dimensions[index]; } @@ -42,7 +42,7 @@ static VALUE arf_constant_ulong(int argc, VALUE* argv){ dim_t ndims = (dim_t)FIX2LONG(argv[0]); dim_t* dimensions = (dim_t*)malloc(ndims * sizeof(dim_t)); dim_t count = 1; - for (size_t index = 0; index < ndims; index++) { + for (dim_t index = 0; index < ndims; index++) { dimensions[index] = (dim_t)FIX2LONG(RARRAY_AREF(argv[1], index)); count *= dimensions[index]; } @@ -59,7 +59,7 @@ static VALUE arf_range(int argc, VALUE* argv){ dim_t ndims = (dim_t)FIX2LONG(argv[0]); dim_t* dimensions = (dim_t*)malloc(ndims * sizeof(dim_t)); dim_t count = 1; - for (size_t index = 0; index < ndims; index++) { + for (dim_t index = 0; index < ndims; index++) { dimensions[index] = (dim_t)FIX2LONG(RARRAY_AREF(argv[1], index)); count *= dimensions[index]; } @@ -80,7 +80,7 @@ static VALUE arf_identity(int argc, VALUE* argv){ dim_t ndims = (dim_t)FIX2LONG(argv[0]); dim_t* dimensions = (dim_t*)malloc(ndims * sizeof(dim_t)); dim_t count = 1; - for (size_t index = 0; index < ndims; index++) { + for (dim_t index = 0; index < ndims; index++) { dimensions[index] = (dim_t)FIX2LONG(RARRAY_AREF(argv[1], index)); count *= dimensions[index]; } From 8e8ee7081d95ae40fd02281499a48741cae9c236 Mon Sep 17 00:00:00 2001 From: prasun Date: Sun, 23 Jul 2017 22:41:30 +0530 Subject: [PATCH 081/125] minor modifications --- ext/mri/cmodules/lapack.c | 6 +++--- test/arith_test.rb | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ext/mri/cmodules/lapack.c b/ext/mri/cmodules/lapack.c index b5e117f..8b50af2 100644 --- a/ext/mri/cmodules/lapack.c +++ b/ext/mri/cmodules/lapack.c @@ -143,7 +143,7 @@ static VALUE arf_norm(VALUE self, VALUE val){ } static VALUE arf_is_lapack_available(VALUE self){ - bool* output; - // af_is_lapack_available(output); - return Qfalse; + bool output; + af_is_lapack_available(&output); + return output ? Qtrue : Qfalse; } diff --git a/test/arith_test.rb b/test/arith_test.rb index af53d5c..6645ae6 100644 --- a/test/arith_test.rb +++ b/test/arith_test.rb @@ -18,7 +18,7 @@ def test_subtraction def test_multiplication c = ArrayFire::Af_Array.new 2, [2,2],[2,8,18,32] - assert_equal @a, @b - @a + assert_equal c, @b * @a end def test_division From e9576cb8528ac4d97611b736792c42fe209343f5 Mon Sep 17 00:00:00 2001 From: prasun Date: Sun, 23 Jul 2017 23:54:52 +0530 Subject: [PATCH 082/125] added unary method tests --- ext/mri/arrayfire.c | 35 ++++++++++++++++++++++++++++++++++- test/arith_test.rb | 10 ++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 8a1cb66..0251588 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -277,7 +277,7 @@ 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); static VALUE arf_matmul(VALUE self, VALUE left_val, VALUE right_val); @@ -328,6 +328,7 @@ void Init_arrayfire() { rb_define_method(Af_Array, "*",(METHOD)arf_ew_multiply,1); rb_define_method(Af_Array, "/",(METHOD)arf_ew_divide,1); rb_define_method(Af_Array, "==",(METHOD)arf_eqeq,1); + rb_define_method(Af_Array, "approx_equal",(METHOD)arf_eqeq_approx,1); rb_define_method(Af_Array, "=~",(METHOD)arf_ew_eqeq,1); rb_define_method(Af_Array, "!~", (METHOD)arf_ew_neq, 1); @@ -587,6 +588,38 @@ static VALUE arf_eqeq(VALUE left_val, VALUE right_val) { return Qtrue; } +static VALUE arf_eqeq_approx(VALUE left_val, VALUE right_val) { + + afstruct* left; + afstruct* right; + + dim_t left_count; + dim_t right_count; + + Data_Get_Struct(left_val, afstruct, left); + Data_Get_Struct(right_val, afstruct, right); + + af_get_elements(&left_count, left->carray); + af_get_elements(&right_count, right->carray); + + if(left_count != right_count){return Qfalse;} + + float* left_arr = (float*)malloc(left_count * sizeof(float)); + af_get_data_ptr(left_arr, left->carray); + + float* right_arr = (float*)malloc(left_count * sizeof(float)); + af_get_data_ptr(right_arr, right->carray); + + for (dim_t index = 0; index < left_count; index++){ + float diff = left_arr[index] - right_arr[index]; + if(diff < 0){diff *= -1;} + if(diff > 1e-3){ + return Qfalse; + } + } + return Qtrue; +} + DEF_ELEMENTWISE_RUBY_ACCESSOR(add, add) DEF_ELEMENTWISE_RUBY_ACCESSOR(subtract, sub) DEF_ELEMENTWISE_RUBY_ACCESSOR(multiply, mul) diff --git a/test/arith_test.rb b/test/arith_test.rb index 6645ae6..4e512f0 100644 --- a/test/arith_test.rb +++ b/test/arith_test.rb @@ -5,6 +5,8 @@ class ArrayFire::ArithTest < Minitest::Test def setup @a = ArrayFire::Af_Array.new 2, [2,2],[1,2,3,4] @b = ArrayFire::Af_Array.new 2, [2,2],[2,4,6,8] + @elements = [10, -11, 48, 21, 65, 0, 1, -7, 112] + @af_array = ArrayFire::Af_Array.new 2, [3,3], @elements end def test_addition @@ -26,4 +28,12 @@ def test_division assert_equal c, @b / @a end + [:sin, :cos, :tan, :sinh, :cosh, :tanh].each do |method| + define_method("test_#{method}") do + x = @elements.map{ |e| Math.send(method, e) } + res_arr = ArrayFire::Af_Array.new 2, [3,3], x + assert res_arr.approx_equal @af_array.send method + end + end + end From 7d0e5b36486d380d91df25ba3eb173d24dac965b Mon Sep 17 00:00:00 2001 From: prasun Date: Mon, 24 Jul 2017 00:28:17 +0530 Subject: [PATCH 083/125] added blas tests --- ext/mri/arrayfire.c | 4 ++-- ext/mri/cmodules/blas.c | 4 ++-- test/blas_test.rb | 20 ++++++++++++++++---- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 0251588..407514c 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -437,8 +437,8 @@ void Init_arrayfire() { Blas = rb_define_class_under(ArrayFire, "BLAS", rb_cObject); rb_define_singleton_method(Blas, "matmul", (METHOD)arf_matmul, 2); rb_define_singleton_method(Blas, "dot", (METHOD)arf_dot, 2); - rb_define_singleton_method(Blas, "transpose", (METHOD)arf_transpose, 2); - rb_define_singleton_method(Blas, "transpose_inplace", (METHOD)arf_transpose_inplace, 2); + rb_define_singleton_method(Blas, "transpose", (METHOD)arf_transpose, 1); + rb_define_singleton_method(Blas, "transpose_inplace", (METHOD)arf_transpose_inplace, 1); Cuda = rb_define_class_under(ArrayFire, "CUDA", rb_cObject); rb_define_singleton_method(Cuda, "get_stream", (METHOD)arf_get_stream, 0); diff --git a/ext/mri/cmodules/blas.c b/ext/mri/cmodules/blas.c index 91cf56d..fbc9498 100644 --- a/ext/mri/cmodules/blas.c +++ b/ext/mri/cmodules/blas.c @@ -33,7 +33,7 @@ static VALUE arf_transpose(VALUE self, VALUE input){ af_transpose(&result->carray, obj->carray, false); - return Data_Wrap_Struct(CLASS_OF(self), NULL, arf_free, result); + return Data_Wrap_Struct(CLASS_OF(input), NULL, arf_free, result); } static VALUE arf_transpose_inplace(VALUE self, VALUE input){ @@ -43,5 +43,5 @@ static VALUE arf_transpose_inplace(VALUE self, VALUE input){ af_transpose_inplace(obj->carray, false); - return Data_Wrap_Struct(CLASS_OF(self), NULL, arf_free, obj); + return Data_Wrap_Struct(CLASS_OF(input), NULL, arf_free, obj); } diff --git a/test/blas_test.rb b/test/blas_test.rb index 8fb8993..622749d 100644 --- a/test/blas_test.rb +++ b/test/blas_test.rb @@ -3,13 +3,25 @@ class ArrayFire::BLASTest < Minitest::Test def setup - @a = ArrayFire::Af_Array.new 2, [2,2],[1,2,3,4] - @b = ArrayFire::Af_Array.new 2, [2,2],[1,2,3,4] + @matrix_left = ArrayFire::Af_Array.new 2, [2,2],[ 12, 21,-61, 48] + @matrix_right = ArrayFire::Af_Array.new 2, [2,2],[-15, 41, 30 , 7 ] + @vector_left = ArrayFire::Af_Array.new 2, [4,1],[-15, 41, 30 , 7 ] + @vector_right = ArrayFire::Af_Array.new 2, [4,1],[ 25, 141, 17 , 7 ] end def test_matmul - c = ArrayFire::Af_Array.new 2, [2,2],[7.0,10.0,15.0,22.0] - assert_equal c, ArrayFire::BLAS.matmul(@a,@b) + result = ArrayFire::Af_Array.new 2, [2,2],[-2681.0, 1653.0, -67.0, 966.0] + assert_equal result, ArrayFire::BLAS.matmul(@matrix_left, @matrix_right) + end + + def test_dot + result = ArrayFire::Af_Array.new 1,[1],[5965.0] + assert_equal result, ArrayFire::BLAS.dot(@vector_left, @vector_right) + end + + def test_transpose + result = ArrayFire::Af_Array.new 2, [2,2], [12.0, -61.0, 21.0, 48.0] + assert_equal result, ArrayFire::BLAS.transpose(@matrix_left) end end From c76214a3ed85c771d90f5076df8d785bd5d65404 Mon Sep 17 00:00:00 2001 From: prasun Date: Mon, 24 Jul 2017 00:52:36 +0530 Subject: [PATCH 084/125] added lapack tests --- ext/mri/arrayfire.c | 4 ++-- ext/mri/cmodules/lapack.c | 7 ++----- test/lapack_test.rb | 30 ++++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 test/lapack_test.rb diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 407514c..058537d 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -505,8 +505,8 @@ void Init_arrayfire() { rb_define_singleton_method(Lapack, "cholesky_inplace", (METHOD)arf_cholesky_inplace, 1); rb_define_singleton_method(Lapack, "solve", (METHOD)arf_solve, 0); rb_define_singleton_method(Lapack, "solve_lu", (METHOD)arf_solve_lu, 0); - rb_define_singleton_method(Lapack, "inverse", (METHOD)arf_inverse, 0); - rb_define_singleton_method(Lapack, "rank", (METHOD)arf_rank, 0); + rb_define_singleton_method(Lapack, "inverse", (METHOD)arf_inverse, 1); + rb_define_singleton_method(Lapack, "rank", (METHOD)arf_rank, 1); rb_define_singleton_method(Lapack, "det", (METHOD)arf_det, 1); rb_define_singleton_method(Lapack, "norm", (METHOD)arf_norm, 1); rb_define_singleton_method(Lapack, "is_lapack_available", (METHOD)arf_is_lapack_available, 0); diff --git a/ext/mri/cmodules/lapack.c b/ext/mri/cmodules/lapack.c index 8b50af2..a017593 100644 --- a/ext/mri/cmodules/lapack.c +++ b/ext/mri/cmodules/lapack.c @@ -99,7 +99,6 @@ static VALUE arf_solve_lu(VALUE self, VALUE lhs_val, VALUE rhs_val, VALUE piv_va } static VALUE arf_inverse(VALUE self, VALUE val){ - afstruct* matrix; afstruct* result = ALLOC(afstruct); @@ -122,13 +121,11 @@ static VALUE arf_rank(VALUE self, VALUE val){ static VALUE arf_det(VALUE self, VALUE val){ afstruct* matrix; - uint a; - double det_real; - // double det_image; + double det_real, det_imag; Data_Get_Struct(val, afstruct, matrix); - af_rank(&a, matrix->carray, det_real); + af_det(&det_real, &det_imag, matrix->carray); return DBL2NUM(det_real); } diff --git a/test/lapack_test.rb b/test/lapack_test.rb new file mode 100644 index 0000000..57a469f --- /dev/null +++ b/test/lapack_test.rb @@ -0,0 +1,30 @@ +require 'test_helper' + +class ArrayFire::LAPACKTest < Minitest::Test + + def setup + @array = ArrayFire::Af_Array.new 2, [2,2],[ 12, 21,-61, 48 ] + end + + def test_inverse + result = ArrayFire::Af_Array.new 2, [2,2], [0.0258, -0.0113, 0.0328, 0.0064] + assert(result.approx_equal ArrayFire::LAPACK.inverse(@array)); + end + + def test_rank + assert_equal ArrayFire::LAPACK.rank(@array), 2; + end + + def test_det + assert_equal ArrayFire::LAPACK.det(@array), 1857.0; + end + + def test_norm + assert((ArrayFire::LAPACK.norm(@array) - 81.3019).abs < 0.001); + end + + def test_is_lapack_available + assert(ArrayFire::LAPACK.is_lapack_available); + end + +end From ef6a83f61617423b8043eb88353e4e3086ef48ca Mon Sep 17 00:00:00 2001 From: prasun Date: Mon, 24 Jul 2017 21:25:56 +0530 Subject: [PATCH 085/125] implemented lapack factorization methods --- ext/mri/arrayfire.c | 69 ++++++++++++++++++++------------------ ext/mri/cmodules/lapack.c | 63 ++++++++++++++++++++-------------- lib/arrayfire/arrayfire.rb | 1 + lib/arrayfire/lapack.rb | 37 ++++++++++++++++++++ 4 files changed, 111 insertions(+), 59 deletions(-) create mode 100644 lib/arrayfire/lapack.rb diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 058537d..d3b3b05 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -176,16 +176,16 @@ static VALUE arf_set_seq_indexer(VALUE self); static VALUE arf_set_seq_param_indexer(VALUE self); static VALUE arf_release_indexers(VALUE self); -static VALUE arf_svd(VALUE self, VALUE val); -static VALUE arf_svd_inplace(VALUE self, VALUE val); -static VALUE arf_lu(VALUE self, VALUE val); -static VALUE arf_lu_inplace(VALUE self); -static VALUE arf_qr(VALUE self, VALUE val); -static VALUE arf_qr_inplace(VALUE self); -static VALUE arf_cholesky(VALUE self, VALUE val); -static VALUE arf_cholesky_inplace(VALUE self); -static VALUE arf_solve(VALUE self, VALUE lhs_val, VALUE rhs_val); -static VALUE arf_solve_lu(VALUE self, VALUE lhs_val, VALUE rhs_val, VALUE piv_val); +static VALUE arf_svd_func(VALUE self, VALUE u_val, VALUE s_val, VALUE vt_val, VALUE val); +static VALUE arf_svd_inplace_func(VALUE self, VALUE val); +static VALUE arf_lu_func(VALUE self, VALUE lower_val, VALUE upper_val, VALUE pivot_val, VALUE val); +static VALUE arf_lu_inplace_func(VALUE self); +static VALUE arf_qr_func(VALUE self, VALUE q_val, VALUE r_val, VALUE tau_val, VALUE val); +static VALUE arf_qr_inplace_func(VALUE self); +static VALUE arf_cholesky_func(VALUE self, VALUE output_val, VALUE val, VALUE is_upper_val); +static VALUE arf_cholesky_inplace_func(VALUE self); +static VALUE arf_solve_func(VALUE self, VALUE lhs_val, VALUE rhs_val); +static VALUE arf_solve_lu_func(VALUE self, VALUE lhs_val, VALUE rhs_val, VALUE piv_val); static VALUE arf_inverse(VALUE self, VALUE val); static VALUE arf_rank(VALUE self, VALUE val); static VALUE arf_det(VALUE self, VALUE val); @@ -495,16 +495,16 @@ void Init_arrayfire() { rb_define_singleton_method(Data, "replace_scalar", (METHOD)arf_replace_scalar, 3); Lapack = rb_define_class_under(ArrayFire, "LAPACK", rb_cObject); - rb_define_singleton_method(Lapack, "svd", (METHOD)arf_svd, 1); - rb_define_singleton_method(Lapack, "svd_inplace", (METHOD)arf_svd_inplace, 1); - rb_define_singleton_method(Lapack, "lu", (METHOD)arf_lu, 1); - rb_define_singleton_method(Lapack, "lu_inplace", (METHOD)arf_lu_inplace, 1); - rb_define_singleton_method(Lapack, "qr", (METHOD)arf_qr, 1); - rb_define_singleton_method(Lapack, "qr_inplace", (METHOD)arf_qr_inplace, 1); - rb_define_singleton_method(Lapack, "cholesky", (METHOD)arf_cholesky, 1); - rb_define_singleton_method(Lapack, "cholesky_inplace", (METHOD)arf_cholesky_inplace, 1); - rb_define_singleton_method(Lapack, "solve", (METHOD)arf_solve, 0); - rb_define_singleton_method(Lapack, "solve_lu", (METHOD)arf_solve_lu, 0); + rb_define_singleton_method(Lapack, "svd_func", (METHOD)arf_svd_func, 4); + rb_define_singleton_method(Lapack, "svd_inplace_func", (METHOD)arf_svd_inplace_func, 1); + rb_define_singleton_method(Lapack, "lu_func", (METHOD)arf_lu_func, 4); + rb_define_singleton_method(Lapack, "lu_inplace_func", (METHOD)arf_lu_inplace_func, 1); + rb_define_singleton_method(Lapack, "qr_func", (METHOD)arf_qr_func, 4); + rb_define_singleton_method(Lapack, "qr_inplace_func", (METHOD)arf_qr_inplace_func, 1); + rb_define_singleton_method(Lapack, "cholesky_func", (METHOD)arf_cholesky_func, 3); + rb_define_singleton_method(Lapack, "cholesky_inplace_func", (METHOD)arf_cholesky_inplace_func, 1); + rb_define_singleton_method(Lapack, "solve_func", (METHOD)arf_solve_func, 0); + rb_define_singleton_method(Lapack, "solve_lu_func", (METHOD)arf_solve_lu_func, 0); rb_define_singleton_method(Lapack, "inverse", (METHOD)arf_inverse, 1); rb_define_singleton_method(Lapack, "rank", (METHOD)arf_rank, 1); rb_define_singleton_method(Lapack, "det", (METHOD)arf_det, 1); @@ -533,21 +533,24 @@ VALUE arf_init(int argc, VALUE* argv, VALUE self) { afstruct* afarray; Data_Get_Struct(self, afstruct, afarray); - dim_t ndims = (dim_t)FIX2LONG(argv[0]); - dim_t* dimensions = (dim_t*)malloc(ndims * sizeof(dim_t)); - dim_t count = 1; - for (dim_t index = 0; index < ndims; index++) { - dimensions[index] = (dim_t)FIX2LONG(RARRAY_AREF(argv[1], index)); - count *= dimensions[index]; - } - float* host_array = (float*)malloc(count * sizeof(float)); - for (dim_t index = 0; index < count; index++) { - host_array[index] = (float)NUM2DBL(RARRAY_AREF(argv[2], index)); - } + if(argc > 0){ + + dim_t ndims = (dim_t)FIX2LONG(argv[0]); + dim_t* dimensions = (dim_t*)malloc(ndims * sizeof(dim_t)); + dim_t count = 1; + for (dim_t index = 0; index < ndims; index++) { + dimensions[index] = (dim_t)FIX2LONG(RARRAY_AREF(argv[1], index)); + count *= dimensions[index]; + } + float* host_array = (float*)malloc(count * sizeof(float)); + for (dim_t index = 0; index < count; index++) { + host_array[index] = (float)NUM2DBL(RARRAY_AREF(argv[2], index)); + } - af_create_array(&afarray->carray, host_array, ndims, dimensions, f32); + af_create_array(&afarray->carray, host_array, ndims, dimensions, f32); - af_print_array(afarray->carray); + af_print_array(afarray->carray); + } return self; } diff --git a/ext/mri/cmodules/lapack.c b/ext/mri/cmodules/lapack.c index a017593..952a1bf 100644 --- a/ext/mri/cmodules/lapack.c +++ b/ext/mri/cmodules/lapack.c @@ -1,17 +1,20 @@ -static VALUE arf_svd(VALUE self, VALUE val){ +static VALUE arf_svd_func(VALUE self, VALUE u_val, VALUE s_val, VALUE vt_val, VALUE val){ afstruct* input; - afstruct* u = ALLOC(afstruct); - afstruct* s = ALLOC(afstruct); - afstruct* vt = ALLOC(afstruct); + afstruct* u; + afstruct* s; + afstruct* vt; Data_Get_Struct(val, afstruct, input); + Data_Get_Struct(u_val, afstruct, u); + Data_Get_Struct(s_val, afstruct, s); + Data_Get_Struct(vt_val, afstruct, vt); af_svd(&u->carray, &s->carray, &vt->carray, input->carray); - return Data_Wrap_Struct(CLASS_OF(val), NULL, arf_free, u); + return Qtrue; } -static VALUE arf_svd_inplace(VALUE self, VALUE val){ +static VALUE arf_svd_inplace_func(VALUE self, VALUE val){ afstruct* input; afstruct* u = ALLOC(afstruct); afstruct* s = ALLOC(afstruct); @@ -24,54 +27,62 @@ static VALUE arf_svd_inplace(VALUE self, VALUE val){ return Data_Wrap_Struct(CLASS_OF(val), NULL, arf_free, u); } -static VALUE arf_lu(VALUE self, VALUE val){ +static VALUE arf_lu_func(VALUE self, VALUE lower_val, VALUE upper_val, VALUE pivot_val, VALUE val){ afstruct* input; - afstruct* lower = ALLOC(afstruct); - afstruct* upper = ALLOC(afstruct); - afstruct* pivot = ALLOC(afstruct); + afstruct* lower; + afstruct* upper; + afstruct* pivot; Data_Get_Struct(val, afstruct, input); + Data_Get_Struct(lower_val, afstruct, lower); + Data_Get_Struct(upper_val, afstruct, upper); + Data_Get_Struct(pivot_val, afstruct, pivot); af_lu(&lower->carray, &upper->carray, &pivot->carray, input->carray); - return Data_Wrap_Struct(CLASS_OF(val), NULL, arf_free, lower); + return Qtrue; } -static VALUE arf_lu_inplace(VALUE self){ +static VALUE arf_lu_inplace_func(VALUE self){ return Qnil; } -static VALUE arf_qr(VALUE self, VALUE val){ +static VALUE arf_qr_func(VALUE self, VALUE q_val, VALUE r_val, VALUE tau_val, VALUE val){ afstruct* input; - afstruct* q = ALLOC(afstruct); - afstruct* r = ALLOC(afstruct); - afstruct* tau = ALLOC(afstruct); + afstruct* q; + afstruct* r; + afstruct* tau; Data_Get_Struct(val, afstruct, input); + Data_Get_Struct(q_val, afstruct, q); + Data_Get_Struct(r_val, afstruct, r); + Data_Get_Struct(tau_val, afstruct, tau); af_qr(&q->carray, &r->carray, &tau->carray, input->carray); - return Data_Wrap_Struct(CLASS_OF(val), NULL, arf_free, q); + return Qfalse; } -static VALUE arf_qr_inplace(VALUE self){ +static VALUE arf_qr_inplace_func(VALUE self){ return Qnil; } -static VALUE arf_cholesky(VALUE self, VALUE val){ +static VALUE arf_cholesky_func(VALUE self, VALUE output_val, VALUE val, VALUE is_upper_val){ afstruct* input; - afstruct* output = ALLOC(afstruct); - int* info; + afstruct* output; + int info; Data_Get_Struct(val, afstruct, input); + Data_Get_Struct(output_val, afstruct, output); + + af_cholesky(&output->carray, &info, input->carray, is_upper_val); - af_cholesky(&output->carray, info, input->carray, true); - return Data_Wrap_Struct(CLASS_OF(val), NULL, arf_free, output); + return INT2NUM(info); } -static VALUE arf_cholesky_inplace(VALUE self){ +static VALUE arf_cholesky_inplace_func(VALUE self){ return Qnil; } -static VALUE arf_solve(VALUE self, VALUE lhs_val, VALUE rhs_val){ +static VALUE arf_solve_func(VALUE self, VALUE lhs_val, VALUE rhs_val){ afstruct* lhs; afstruct* rhs; @@ -85,7 +96,7 @@ static VALUE arf_solve(VALUE self, VALUE lhs_val, VALUE rhs_val){ } -static VALUE arf_solve_lu(VALUE self, VALUE lhs_val, VALUE rhs_val, VALUE piv_val){ +static VALUE arf_solve_lu_func(VALUE self, VALUE lhs_val, VALUE rhs_val, VALUE piv_val){ afstruct* lhs; afstruct* rhs; afstruct* piv; diff --git a/lib/arrayfire/arrayfire.rb b/lib/arrayfire/arrayfire.rb index 967d23a..a593644 100644 --- a/lib/arrayfire/arrayfire.rb +++ b/lib/arrayfire/arrayfire.rb @@ -1 +1,2 @@ require_relative '../arrayfire.so' +require_relative 'lapack.rb' \ No newline at end of file diff --git a/lib/arrayfire/lapack.rb b/lib/arrayfire/lapack.rb new file mode 100644 index 0000000..b3777a2 --- /dev/null +++ b/lib/arrayfire/lapack.rb @@ -0,0 +1,37 @@ +module ArrayFire + class LAPACK + + def self.svd(af_array) + u = ArrayFire::Af_Array.new + s = ArrayFire::Af_Array.new + vt = ArrayFire::Af_Array.new + ArrayFire::LAPACK.svd_func(u, s, vt, af_array) + return u, s, vt + end + + def self.lu(af_array) + lower = ArrayFire::Af_Array.new + upper = ArrayFire::Af_Array.new + pivot = ArrayFire::Af_Array.new + ArrayFire::LAPACK.lu_func(lower, upper, pivot, af_array) + return lower, upper, pivot + end + + def self.qr(af_array) + q = ArrayFire::Af_Array.new + r = ArrayFire::Af_Array.new + tau = ArrayFire::Af_Array.new + ArrayFire::LAPACK.qr_func(q, r, tau, af_array) + return q, r, tau + end + + def self.cholesky(af_array, is_upper=true) + out = ArrayFire::Af_Array.new + info = 0 + info = ArrayFire::LAPACK.cholesky_func(out, af_array, is_upper) + return out, info + end + + end +end + From 062b5acaa8a7aa210fb38cf4ba513f1a62510ecb Mon Sep 17 00:00:00 2001 From: prasun Date: Tue, 1 Aug 2017 14:38:15 +0530 Subject: [PATCH 086/125] bind ArrayFire::Backend apis --- ext/mri/arrayfire.c | 16 +++++++++------- ext/mri/cmodules/backend.c | 29 ++++++++++++++++++++++------- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index d3b3b05..727beaf 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -89,9 +89,10 @@ static VALUE arf_set_intersect(VALUE self); static VALUE arf_get_backend_count(VALUE self); static VALUE arf_get_available_backends(VALUE self); -static VALUE arf_get_backend_id(VALUE self); +static VALUE arf_get_backend_id(VALUE self, VALUE array_val); static VALUE arf_get_active_backend(VALUE self); -static VALUE arf_get_backend_device_id(VALUE self); +static VALUE arf_get_backend_device_id(VALUE self, VALUE array_val); +static VALUE arf_set_backend(VALUE self); static VALUE arf_matmul(VALUE self, VALUE left_val, VALUE right_val); static VALUE arf_dot(VALUE self, VALUE left_val, VALUE right_val); @@ -399,11 +400,12 @@ void Init_arrayfire() { rb_define_singleton_method(Algorithm, "set_intersect", (METHOD)arf_set_intersect, 0); Backend = rb_define_class_under(ArrayFire, "Backend", rb_cObject); - rb_define_method(Backend, "get_backend_count", (METHOD)arf_get_backend_count, 0); - rb_define_method(Backend, "get_available_backends", (METHOD)arf_get_available_backends, 0); - rb_define_method(Backend, "get_backend_id", (METHOD)arf_get_backend_id, 0); - rb_define_method(Backend, "get_active_backend", (METHOD)arf_get_active_backend, 0); - rb_define_method(Backend, "get_device_id", (METHOD)arf_get_backend_device_id, 0); + rb_define_singleton_method(Backend, "get_backend_count", (METHOD)arf_get_backend_count, 0); + rb_define_singleton_method(Backend, "get_available_backends", (METHOD)arf_get_available_backends, 0); + rb_define_singleton_method(Backend, "get_backend_id", (METHOD)arf_get_backend_id, 1); + rb_define_singleton_method(Backend, "get_active_backend", (METHOD)arf_get_active_backend, 0); + rb_define_singleton_method(Backend, "get_device_id", (METHOD)arf_get_backend_device_id, 1); + rb_define_singleton_method(Backend, "set_backend", (METHOD)arf_set_backend, 0); Device = rb_define_class_under(ArrayFire, "Device", rb_cObject); rb_define_singleton_method(Device, "info", (METHOD)arf_info, 0); diff --git a/ext/mri/cmodules/backend.c b/ext/mri/cmodules/backend.c index f2840f2..09b1479 100644 --- a/ext/mri/cmodules/backend.c +++ b/ext/mri/cmodules/backend.c @@ -1,19 +1,34 @@ static VALUE arf_get_backend_count(VALUE self){ - return Qnil; + uint num_backends; + af_get_backend_count(&num_backends); + return UINT2NUM(num_backends); } static VALUE arf_get_available_backends(VALUE self){ - return Qnil; + int backends; + af_get_available_backends (&backends); + return INT2NUM(backends); } -static VALUE arf_get_backend_id(VALUE self){ - return Qnil; +static VALUE arf_get_backend_id(VALUE self, VALUE array_val){ + return Qnil; } static VALUE arf_get_active_backend(VALUE self){ - return Qnil; + return Qnil; } -static VALUE arf_get_backend_device_id(VALUE self){ - return Qnil; +static VALUE arf_get_backend_device_id(VALUE self, VALUE array_val){ + afstruct* input; + int device_id; + + Data_Get_Struct(array_val, afstruct, input); + + af_get_device_id(&device_id, input->carray); + + return INT2NUM(device_id); +} + +static VALUE arf_set_backend(VALUE self){ + return Qnil; } From c368e6889c4267901c16efe34bc59255abfeeda3 Mon Sep 17 00:00:00 2001 From: prasun Date: Tue, 1 Aug 2017 16:59:15 +0530 Subject: [PATCH 087/125] added tests for ArrayFire::Data --- test/data_test.rb | 145 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 test/data_test.rb diff --git a/test/data_test.rb b/test/data_test.rb new file mode 100644 index 0000000..a144b11 --- /dev/null +++ b/test/data_test.rb @@ -0,0 +1,145 @@ +require 'test_helper' + +class ArrayFire::DataTest < Minitest::Test + + def setup + @first = ArrayFire::Af_Array.new 2, [3,3], [4, 1, 5, 6, -11, 9 , -22, 11, 1] + @second = ArrayFire::Af_Array.new 2, [3,3], [-14, 27, 15, 6, -11, 9 , 22, 0, 61] + @diagonal = ArrayFire::Af_Array.new 1, [4,1], [1, 2, 3, 4] + @order_array = ArrayFire::Af_Array.new 2, [3,3], [1, 2, 3, 4, 5, 6 , 7, 8 , 9] + end + + def test_constant + result = ArrayFire::Af_Array.new 2, [3,3], [4, 4, 4, 4, 4, 4, 4, 4, 4] + assert_equal result, ArrayFire::Data.constant( 2, [2,2], 4 ); + end + + def test_constant_complex + # ArrayFire::Data.constant_complex + end + + def test_constant_long + result = ArrayFire::Af_Array.new 2, [3,3], [4, 4, 4, 4, 4, 4, 4, 4, 4] + assert_equal result, ArrayFire::Data.constant_long( 2, [3,3],4 ) + end + + def test_constant_ulong + result = ArrayFire::Af_Array.new 2, [3,3], [4, 4, 4, 4, 4, 4, 4, 4, 4] + assert_equal result, ArrayFire::Data.constant_ulong( 2, [3,3], 4 ) + end + + def test_range + result = ArrayFire::Af_Array.new 2, [4,4], [0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0, 3.0] + assert_equal result, ArrayFire::Data.range( 2, [4,4], 1 ) + end + + def test_iota + # ArrayFire::Data.iota + end + + def test_identity + result = ArrayFire::Af_Array.new 2, [4,5], [1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0] + assert_equal result, ArrayFire::Data.identity( 2, [4,5] ) + end + + def test_diag_create + result = ArrayFire::Af_Array.new 2, [4,4], [1.0, 0.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 0.0, 3.0, 0.0, 0.0, 0.0, 0.0, 4.0] + assert_equal result, ArrayFire::Data.diag_create( @diagonal, 0 ) + end + + def test_diag_extract + diag_matrix = ArrayFire::Data.diag_create @diagonal, 0 + assert_equal @diagonal, ArrayFire::Data.diag_extract( diag_matrix, 0 ) + end + + def test_join + result = ArrayFire::Af_Array.new 2, [3, 6], [4.0, 1.0, 5.0, 6.0, -11.0, 9.0, -22.0, 11.0, 1.0, + -14.0, 27.0, 15.0, 6.0, -11.0, 9.0, 22.0,0.0, 61.0] + + assert_equal result, ArrayFire::Data.join(1, @first, @second) + end + + def test_join_many + # result = ArrayFire::Af_Array.new 2, [3, 6], [4.0, 1.0, 5.0, 6.0, -11.0, 9.0, -22.0, 11.0, 1.0, + # -14.0, 27.0, 15.0, 6.0, -11.0, 9.0, 22.0,0.0, 61.0] + + # assert_equal result, ArrayFire::Data.join_many(1, @first, @second) + end + + def test_tile + result = ArrayFire::Af_Array.new 2, [6,9], [1.0,2.0,3.0,1.0,2.0,3.0,4.0,5.0,6.0, + 4.0,5.0,6.0,7.0,8.0,9.0,7.0,8.0,9.0, + 1.0,2.0,3.0,1.0,2.0,3.0,4.0,5.0,6.0, + 4.0,5.0,6.0,7.0,8.0,9.0,7.0,8.0,9.0, + 1.0,2.0,3.0,1.0,2.0,3.0,4.0,5.0,6.0, + 4.0,5.0,6.0,7.0,8.0,9.0,7.0,8.0,9.0] + + assert_equal result.elements, ArrayFire::Data.tile( @order_array, 2, 3, 1, 1 ).elements + end + + def test_reorder + result = ArrayFire:: Af_Array.new 2, [3,3], [1.0, 4.0, 7.0, 2.0, 5.0, 8.0, 3.0, 6.0, 9.0] + assert_equal result, ArrayFire::Data.reorder( @order_array, 1, 0, 2, 3 ) + end + + def test_shift + first_shift = ArrayFire::Af_Array.new 2, [3,3], [3.0, 1.0, 2.0, 6.0, 4.0, 5.0, 9.0, 7.0, 8.0] + second_shift = ArrayFire::Af_Array.new 2, [3,3], [6.0, 4.0, 5.0, 9.0, 7.0, 8.0, 3.0, 1.0, 2.0] + assert_equal first_shift, ArrayFire::Data.shift( @order_array, 1, 0, 0, 0 ) + assert_equal second_shift, ArrayFire::Data.shift( @order_array, 1, 2, 0, 0 ) + end + + def test_moddims + # result = ArrayFire::Af_Array.new 2, [9,1], [4, 1, 5, 6, -11, 9 , -22, 11, 1] + # assert_equal result, ArrayFire::Data.moddims( @input, 2, [9,1] ) + end + + def test_flat + result = ArrayFire::Af_Array.new 1, [9], [4.0, 1.0, 5.0, 6.0, -11.0, 9.0, -22.0, 11.0, 1.0] + assert_equal result, ArrayFire::Data.flat(@first) + end + + def test_flip + result = ArrayFire::Af_Array.new 2, [3,3], [-22.0, 11.0, 1.0, 6.0, -11.0, 9.0, 4.0, 1.0, 5.0] + assert_equal result, ArrayFire::Data.flip(@first, 1) + end + + def test_lower + result = ArrayFire::Af_Array.new 2, [3,3], [4.0, 1.0, 5.0, 0.0, -11.0, 9.0, 0.0, 0.0, 1.0] + assert_equal result, ArrayFire::Data.lower(@first, false) + + #unit_diagonal + result = ArrayFire::Af_Array.new 2, [3,3], [1.0, 1.0, 5.0, 0.0, 1.0, 9.0, 0.0, 0.0, 1.0] + assert_equal result, ArrayFire::Data.lower(@first, true) + end + + def test_upper + result = ArrayFire::Af_Array.new 2, [3,3], [4.0, 0.0, 0.0, 6.0, -11.0, 0.0, -22.0, 11.0, 1.0] + assert_equal result, ArrayFire::Data.upper(@first, false) + + #unit_diagonal + result = ArrayFire::Af_Array.new 2, [3,3], [1.0, 0.0, 0.0, 6.0, 1.0, 0.0, -22.0, 11.0, 1.0] + assert_equal result, ArrayFire::Data.upper(@first, true) + end + + def test_select + # ArrayFire::Data.select + end + + def test_select_scalar_r + # ArrayFire::Data.select_scalar_r + end + + def test_select_scalar_l + # ArrayFire::Data.select_scalar_l + end + + def test_replace + # ArrayFire::Data.replace + end + + def test_replace_scalar + # ArrayFire::Data.replace_scalar + end + +end From b33bf944edd9f3b4a24ccf206786e0e346cd8f36 Mon Sep 17 00:00:00 2001 From: prasun Date: Wed, 2 Aug 2017 10:39:42 +0530 Subject: [PATCH 088/125] init ArrayFire::Statistics class --- ext/mri/arrayfire.c | 34 ++++++++++++++++++++++ ext/mri/cmodules/statistics.c | 55 +++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 ext/mri/cmodules/statistics.c diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 727beaf..633a3b2 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -14,6 +14,7 @@ VALUE Index = Qnil; VALUE Lapack = Qnil; VALUE OpenCL = Qnil; VALUE Random = Qnil; +VALUE Statistics = Qnil; // prototypes void Init_arrayfire(); @@ -209,6 +210,21 @@ static VALUE arf_randn(VALUE self); static VALUE arf_set_seed(VALUE self); static VALUE arf_get_seed(VALUE self); +static VALUE arf_mean(VALUE self); +static VALUE arf_mean_weighted(VALUE self); +static VALUE arf_var(VALUE self); +static VALUE arf_var_weighted(VALUE self); +static VALUE arf_stdev(VALUE self); +static VALUE arf_cov(VALUE self); +static VALUE arf_median(VALUE self); +static VALUE arf_mean_all(VALUE self); +static VALUE arf_mean_all_weighted(VALUE self); +static VALUE arf_var_all(VALUE self); +static VALUE arf_var_all_weighted(VALUE self); +static VALUE arf_stdev_all(VALUE self); +static VALUE arf_median_all(VALUE self); +static VALUE arf_corrcoef(VALUE self); + #define DEF_ELEMENTWISE_RUBY_ACCESSOR(name, oper) \ static VALUE arf_ew_##name(VALUE left_val, VALUE right_val) { \ afstruct* left; \ @@ -529,6 +545,23 @@ void Init_arrayfire() { rb_define_method(Random, "randn", (METHOD)arf_randn, 0); rb_define_method(Random, "set_seed", (METHOD)arf_set_seed, 0); rb_define_method(Random, "get_seed", (METHOD)arf_get_seed, 0); + + Statistics = rb_define_class_under(ArrayFire, "Statistics", rb_cObject); + rb_define_singleton_method(Statistics, "mean", (METHOD)arf_mean, 0); + rb_define_singleton_method(Statistics, "mean_weighted", (METHOD)arf_mean_weighted, 0); + rb_define_singleton_method(Statistics, "var", (METHOD)arf_var, 0); + rb_define_singleton_method(Statistics, "var_weighted", (METHOD)arf_var_weighted, 0); + rb_define_singleton_method(Statistics, "stdev", (METHOD)arf_stdev, 0); + rb_define_singleton_method(Statistics, "cov", (METHOD)arf_cov, 0); + rb_define_singleton_method(Statistics, "median", (METHOD)arf_median, 0); + rb_define_singleton_method(Statistics, "mean_all", (METHOD)arf_mean_all, 0); + rb_define_singleton_method(Statistics, "mean_all_weighted", (METHOD)arf_mean_all_weighted, 0); + rb_define_singleton_method(Statistics, "var_all", (METHOD)arf_var_all, 0); + rb_define_singleton_method(Statistics, "var_all_weighted", (METHOD)arf_var_all_weighted, 0); + rb_define_singleton_method(Statistics, "stdev_all", (METHOD)arf_stdev_all, 0); + rb_define_singleton_method(Statistics, "median_all", (METHOD)arf_median_all, 0); + rb_define_singleton_method(Statistics, "corrcoef", (METHOD)arf_corrcoef, 0); + } VALUE arf_init(int argc, VALUE* argv, VALUE self) @@ -673,3 +706,4 @@ DEF_UNARY_RUBY_ACCESSOR(ceil, ceil) #include "cmodules/data.c" #include "cmodules/lapack.c" #include "cmodules/random.c" +#include "cmodules/statistics.c" diff --git a/ext/mri/cmodules/statistics.c b/ext/mri/cmodules/statistics.c new file mode 100644 index 0000000..afff4f1 --- /dev/null +++ b/ext/mri/cmodules/statistics.c @@ -0,0 +1,55 @@ +static VALUE arf_mean(VALUE self){ + return Qnil; +} + +static VALUE arf_mean_weighted(VALUE self){ + return Qnil; +} + +static VALUE arf_var(VALUE self){ + return Qnil; +} + +static VALUE arf_var_weighted(VALUE self){ + return Qnil; +} + +static VALUE arf_stdev(VALUE self){ + return Qnil; +} + +static VALUE arf_cov(VALUE self){ + return Qnil; +} + +static VALUE arf_median(VALUE self){ + return Qnil; +} + +static VALUE arf_mean_all(VALUE self){ + return Qnil; +} + +static VALUE arf_mean_all_weighted(VALUE self){ + return Qnil; +} + +static VALUE arf_var_all(VALUE self){ + return Qnil; +} + +static VALUE arf_var_all_weighted(VALUE self){ + return Qnil; +} + +static VALUE arf_stdev_all(VALUE self){ + return Qnil; +} + +static VALUE arf_median_all(VALUE self){ + return Qnil; +} + +static VALUE arf_corrcoef(VALUE self){ + return Qnil; +} From 261bfc4933937f837705c7e3651d36f4479591ba Mon Sep 17 00:00:00 2001 From: prasun Date: Wed, 2 Aug 2017 11:23:51 +0530 Subject: [PATCH 089/125] add statistics methods --- ext/mri/arrayfire.c | 56 +++++------ ext/mri/cmodules/statistics.c | 173 ++++++++++++++++++++++++++++------ 2 files changed, 173 insertions(+), 56 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 633a3b2..2f1164f 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -210,20 +210,20 @@ static VALUE arf_randn(VALUE self); static VALUE arf_set_seed(VALUE self); static VALUE arf_get_seed(VALUE self); -static VALUE arf_mean(VALUE self); -static VALUE arf_mean_weighted(VALUE self); -static VALUE arf_var(VALUE self); -static VALUE arf_var_weighted(VALUE self); -static VALUE arf_stdev(VALUE self); -static VALUE arf_cov(VALUE self); -static VALUE arf_median(VALUE self); -static VALUE arf_mean_all(VALUE self); -static VALUE arf_mean_all_weighted(VALUE self); -static VALUE arf_var_all(VALUE self); -static VALUE arf_var_all_weighted(VALUE self); -static VALUE arf_stdev_all(VALUE self); -static VALUE arf_median_all(VALUE self); -static VALUE arf_corrcoef(VALUE self); +static VALUE arf_mean(VALUE self, VALUE array_val, VALUE dim_val); +static VALUE arf_mean_weighted(VALUE self, VALUE array_val, VALUE weighted_array_val, VALUE dim_val); +static VALUE arf_var(VALUE self, VALUE array_val, VALUE is_biased, VALUE dim_val); +static VALUE arf_var_weighted(VALUE self, VALUE array_val, VALUE weighted_array_val, VALUE dim_val); +static VALUE arf_stdev(VALUE self, VALUE array_val, VALUE dim_val); +static VALUE arf_cov(VALUE self, VALUE first_array_val, VALUE second_array_val, VALUE is_biased); +static VALUE arf_median(VALUE self, VALUE array_val, VALUE dim_val); +static VALUE arf_mean_all(VALUE self, VALUE array_val); +static VALUE arf_mean_all_weighted(VALUE self, VALUE array_val, VALUE weighted_array_val); +static VALUE arf_var_all(VALUE self, VALUE array_val, VALUE is_biased); +static VALUE arf_var_all_weighted(VALUE self, VALUE array_val, VALUE weighted_array_val); +static VALUE arf_stdev_all(VALUE self, VALUE array_val); +static VALUE arf_median_all(VALUE self, VALUE array_val); +static VALUE arf_corrcoef(VALUE self, VALUE first_array_val, VALUE second_array_val); #define DEF_ELEMENTWISE_RUBY_ACCESSOR(name, oper) \ static VALUE arf_ew_##name(VALUE left_val, VALUE right_val) { \ @@ -547,20 +547,20 @@ void Init_arrayfire() { rb_define_method(Random, "get_seed", (METHOD)arf_get_seed, 0); Statistics = rb_define_class_under(ArrayFire, "Statistics", rb_cObject); - rb_define_singleton_method(Statistics, "mean", (METHOD)arf_mean, 0); - rb_define_singleton_method(Statistics, "mean_weighted", (METHOD)arf_mean_weighted, 0); - rb_define_singleton_method(Statistics, "var", (METHOD)arf_var, 0); - rb_define_singleton_method(Statistics, "var_weighted", (METHOD)arf_var_weighted, 0); - rb_define_singleton_method(Statistics, "stdev", (METHOD)arf_stdev, 0); - rb_define_singleton_method(Statistics, "cov", (METHOD)arf_cov, 0); - rb_define_singleton_method(Statistics, "median", (METHOD)arf_median, 0); - rb_define_singleton_method(Statistics, "mean_all", (METHOD)arf_mean_all, 0); - rb_define_singleton_method(Statistics, "mean_all_weighted", (METHOD)arf_mean_all_weighted, 0); - rb_define_singleton_method(Statistics, "var_all", (METHOD)arf_var_all, 0); - rb_define_singleton_method(Statistics, "var_all_weighted", (METHOD)arf_var_all_weighted, 0); - rb_define_singleton_method(Statistics, "stdev_all", (METHOD)arf_stdev_all, 0); - rb_define_singleton_method(Statistics, "median_all", (METHOD)arf_median_all, 0); - rb_define_singleton_method(Statistics, "corrcoef", (METHOD)arf_corrcoef, 0); + rb_define_singleton_method(Statistics, "mean", (METHOD)arf_mean, 2); + rb_define_singleton_method(Statistics, "mean_weighted", (METHOD)arf_mean_weighted, 3); + rb_define_singleton_method(Statistics, "var", (METHOD)arf_var, 3); + rb_define_singleton_method(Statistics, "var_weighted", (METHOD)arf_var_weighted, 3); + rb_define_singleton_method(Statistics, "stdev", (METHOD)arf_stdev, 2); + rb_define_singleton_method(Statistics, "cov", (METHOD)arf_cov, 3); + rb_define_singleton_method(Statistics, "median", (METHOD)arf_median, 2); + rb_define_singleton_method(Statistics, "mean_all", (METHOD)arf_mean_all, 1); + rb_define_singleton_method(Statistics, "mean_all_weighted", (METHOD)arf_mean_all_weighted, 2); + rb_define_singleton_method(Statistics, "var_all", (METHOD)arf_var_all, 2); + rb_define_singleton_method(Statistics, "var_all_weighted", (METHOD)arf_var_all_weighted, 2); + rb_define_singleton_method(Statistics, "stdev_all", (METHOD)arf_stdev_all, 1); + rb_define_singleton_method(Statistics, "median_all", (METHOD)arf_median_all, 1); + rb_define_singleton_method(Statistics, "corrcoef", (METHOD)arf_corrcoef, 2); } diff --git a/ext/mri/cmodules/statistics.c b/ext/mri/cmodules/statistics.c index afff4f1..5842df1 100644 --- a/ext/mri/cmodules/statistics.c +++ b/ext/mri/cmodules/statistics.c @@ -1,55 +1,172 @@ -static VALUE arf_mean(VALUE self){ - return Qnil; +static VALUE arf_mean(VALUE self, VALUE array_val, VALUE dim_val){ + afstruct* input; + afstruct* output = ALLOC(afstruct); + + Data_Get_Struct(array_val, afstruct, input); + + af_mean(&output->carray, input->carray, NUM2UINT(dim_val)); + af_print_array(output->carray); + + return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } -static VALUE arf_mean_weighted(VALUE self){ - return Qnil; +static VALUE arf_mean_weighted(VALUE self, VALUE array_val, VALUE weighted_array_val, VALUE dim_val){ + afstruct* input; + afstruct* weighted_array; + afstruct* output = ALLOC(afstruct); + + Data_Get_Struct(array_val, afstruct, input); + Data_Get_Struct(weighted_array_val, afstruct, weighted_array); + + af_mean_weighted(&output->carray, input->carray, weighted_array->carray, NUM2UINT(dim_val)); + af_print_array(output->carray); + + return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } -static VALUE arf_var(VALUE self){ - return Qnil; +static VALUE arf_var(VALUE self, VALUE array_val, VALUE is_biased, VALUE dim_val){ + afstruct* input; + afstruct* output = ALLOC(afstruct); + + Data_Get_Struct(array_val, afstruct, input); + + af_var(&output->carray, input->carray, RTEST(is_biased), NUM2UINT(dim_val)); + af_print_array(output->carray); + + return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } -static VALUE arf_var_weighted(VALUE self){ - return Qnil; +static VALUE arf_var_weighted(VALUE self, VALUE array_val, VALUE weighted_array_val, VALUE dim_val){ + afstruct* input; + afstruct* weighted_array; + afstruct* output = ALLOC(afstruct); + + Data_Get_Struct(array_val, afstruct, input); + Data_Get_Struct(weighted_array_val, afstruct, weighted_array); + + af_var_weighted(&output->carray, input->carray, weighted_array->carray, NUM2UINT(dim_val)); + af_print_array(output->carray); + + return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } -static VALUE arf_stdev(VALUE self){ - return Qnil; +static VALUE arf_stdev(VALUE self, VALUE array_val, VALUE dim_val){ + afstruct* input; + afstruct* output = ALLOC(afstruct); + + Data_Get_Struct(array_val, afstruct, input); + + af_stdev(&output->carray, input->carray, NUM2UINT(dim_val)); + af_print_array(output->carray); + + return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } -static VALUE arf_cov(VALUE self){ - return Qnil; +static VALUE arf_cov(VALUE self, VALUE first_array_val, VALUE second_array_val, VALUE is_biased){ + afstruct* output = ALLOC(afstruct); + + afstruct* first_array; + afstruct* second_array; + + Data_Get_Struct(first_array_val, afstruct, first_array); + Data_Get_Struct(second_array_val, afstruct, second_array); + + af_cov(&output->carray, first_array->carray, second_array->carray, RTEST(is_biased)); + + return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } -static VALUE arf_median(VALUE self){ - return Qnil; +static VALUE arf_median(VALUE self, VALUE array_val, VALUE dim_val){ + afstruct* input; + afstruct* output = ALLOC(afstruct); + + Data_Get_Struct(array_val, afstruct, input); + + af_median(&output->carray, input->carray, NUM2UINT(dim_val)); + af_print_array(output->carray); + + return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } -static VALUE arf_mean_all(VALUE self){ - return Qnil; +static VALUE arf_mean_all(VALUE self, VALUE array_val){ + afstruct* input; + double real_part, imag_part; + + Data_Get_Struct(array_val, afstruct, input); + + af_mean_all(&real_part, &imag_part, input->carray); + + return DBL2NUM(real_part); } -static VALUE arf_mean_all_weighted(VALUE self){ - return Qnil; +static VALUE arf_mean_all_weighted(VALUE self, VALUE array_val, VALUE weighted_array_val){ + afstruct* input; + afstruct* weighted_array; + double real_part, imag_part; + + Data_Get_Struct(array_val, afstruct, input); + Data_Get_Struct(weighted_array_val, afstruct, weighted_array); + + // af_mean_all_weigheted(&real_part, &imag_part, input->carray, weighted_array->carray); + + return DBL2NUM(real_part); } -static VALUE arf_var_all(VALUE self){ - return Qnil; +static VALUE arf_var_all(VALUE self, VALUE array_val, VALUE is_biased){ + afstruct* input; + double real_part, imag_part; + + Data_Get_Struct(array_val, afstruct, input); + + af_var_all(&real_part, &imag_part, input->carray, RTEST(is_biased)); + + return DBL2NUM(real_part); } -static VALUE arf_var_all_weighted(VALUE self){ - return Qnil; +static VALUE arf_var_all_weighted(VALUE self, VALUE array_val, VALUE weighted_array_val){ + afstruct* input; + afstruct* weighted_array; + double real_part, imag_part; + + Data_Get_Struct(array_val, afstruct, input); + Data_Get_Struct(weighted_array_val, afstruct, weighted_array); + + // af_var_all_weigheted(&real_part, &imag_part, input->carray, weighted_array->carray); + + return DBL2NUM(real_part); } -static VALUE arf_stdev_all(VALUE self){ - return Qnil; +static VALUE arf_stdev_all(VALUE self, VALUE array_val){ + afstruct* input; + double real_part, imag_part; + + Data_Get_Struct(array_val, afstruct, input); + + // af_stddev_all(&real_part, &imag_part, input->carray); + + return DBL2NUM(real_part); } -static VALUE arf_median_all(VALUE self){ - return Qnil; +static VALUE arf_median_all(VALUE self, VALUE array_val){ + afstruct* input; + double real_part, imag_part; + + Data_Get_Struct(array_val, afstruct, input); + + af_median_all(&real_part, &imag_part, input->carray); + + return DBL2NUM(real_part); } -static VALUE arf_corrcoef(VALUE self){ - return Qnil; +static VALUE arf_corrcoef(VALUE self, VALUE first_array_val, VALUE second_array_val){ + afstruct* first_array; + afstruct* second_array; + double real_part, imag_part; + + Data_Get_Struct(first_array_val, afstruct, first_array); + Data_Get_Struct(first_array_val, afstruct, first_array); + + af_corrcoef(&real_part, &imag_part, first_array->carray, second_array->carray); + + return DBL2NUM(real_part); } From 66ec7d57912e91e3b94305dfb97c0d85b0d9cb76 Mon Sep 17 00:00:00 2001 From: prasun Date: Wed, 2 Aug 2017 11:36:09 +0530 Subject: [PATCH 090/125] init ArrayFire::Util class --- ext/mri/arrayfire.c | 27 +++++++++++++++++++++++++++ ext/mri/cmodules/util.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 ext/mri/cmodules/util.c diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 2f1164f..7abcfa6 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -15,6 +15,7 @@ VALUE Lapack = Qnil; VALUE OpenCL = Qnil; VALUE Random = Qnil; VALUE Statistics = Qnil; +VALUE Util = Qnil; // prototypes void Init_arrayfire(); @@ -225,6 +226,18 @@ static VALUE arf_stdev_all(VALUE self, VALUE array_val); static VALUE arf_median_all(VALUE self, VALUE array_val); static VALUE arf_corrcoef(VALUE self, VALUE first_array_val, VALUE second_array_val); +static VALUE arf_print_array(VALUE self); +static VALUE arf_print_array_gen(VALUE self); +static VALUE arf_save_array(VALUE self); +static VALUE arf_read_array_index(VALUE self); +static VALUE arf_read_array_key(VALUE self); +static VALUE arf_read_array_key_check(VALUE self); +static VALUE arf_array_to_string(VALUE self); +static VALUE arf_example_function(VALUE self); +static VALUE arf_get_version(VALUE self); +static VALUE arf_get_revision(VALUE self); +static VALUE arf_get_size_of(VALUE self); + #define DEF_ELEMENTWISE_RUBY_ACCESSOR(name, oper) \ static VALUE arf_ew_##name(VALUE left_val, VALUE right_val) { \ afstruct* left; \ @@ -562,6 +575,19 @@ void Init_arrayfire() { rb_define_singleton_method(Statistics, "median_all", (METHOD)arf_median_all, 1); rb_define_singleton_method(Statistics, "corrcoef", (METHOD)arf_corrcoef, 2); + Util = rb_define_class_under(ArrayFire, "Util", rb_cObject); + rb_define_singleton_method(Util, "print_array", (METHOD)arf_print_array, 0); + rb_define_singleton_method(Util, "print_array_gen", (METHOD)arf_print_array_gen, 0); + rb_define_singleton_method(Util, "save_array", (METHOD)arf_save_array, 0); + rb_define_singleton_method(Util, "read_array_index", (METHOD)arf_read_array_index, 0); + rb_define_singleton_method(Util, "read_array_key", (METHOD)arf_read_array_key, 0); + rb_define_singleton_method(Util, "read_array_key_check", (METHOD)arf_read_array_key_check, 0); + rb_define_singleton_method(Util, "array_to_string", (METHOD)arf_array_to_string, 0); + rb_define_singleton_method(Util, "example_function", (METHOD)arf_example_function, 0); + rb_define_singleton_method(Util, "get_version", (METHOD)arf_get_version, 0); + rb_define_singleton_method(Util, "get_revision", (METHOD)arf_get_revision, 0); + rb_define_singleton_method(Util, "get_size_of", (METHOD)arf_get_size_of, 0); + } VALUE arf_init(int argc, VALUE* argv, VALUE self) @@ -707,3 +733,4 @@ DEF_UNARY_RUBY_ACCESSOR(ceil, ceil) #include "cmodules/lapack.c" #include "cmodules/random.c" #include "cmodules/statistics.c" +#include "cmodules/util.c" diff --git a/ext/mri/cmodules/util.c b/ext/mri/cmodules/util.c new file mode 100644 index 0000000..4db5371 --- /dev/null +++ b/ext/mri/cmodules/util.c @@ -0,0 +1,33 @@ +static VALUE arf_print_array(VALUE self){ + return Qnil; +} +static VALUE arf_print_array_gen(VALUE self){ + return Qnil; +} +static VALUE arf_save_array(VALUE self){ + return Qnil; +} +static VALUE arf_read_array_index(VALUE self){ + return Qnil; +} +static VALUE arf_read_array_key(VALUE self){ + return Qnil; +} +static VALUE arf_read_array_key_check(VALUE self){ + return Qnil; +} +static VALUE arf_array_to_string(VALUE self){ + return Qnil; +} +static VALUE arf_example_function(VALUE self){ + return Qnil; +} +static VALUE arf_get_version(VALUE self){ + return Qnil; +} +static VALUE arf_get_revision(VALUE self){ + return Qnil; +} +static VALUE arf_get_size_of(VALUE self){ + return Qnil; +} From a6d4b0b8807ae47990807384ed5a3f7d47dc2dbc Mon Sep 17 00:00:00 2001 From: prasun Date: Thu, 3 Aug 2017 00:51:32 +0530 Subject: [PATCH 091/125] Added tests for ArrayFire::Statistics#methods --- ext/mri/cmodules/statistics.c | 8 ++-- test/statistics_test.rb | 72 +++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 test/statistics_test.rb diff --git a/ext/mri/cmodules/statistics.c b/ext/mri/cmodules/statistics.c index 5842df1..efd29b7 100644 --- a/ext/mri/cmodules/statistics.c +++ b/ext/mri/cmodules/statistics.c @@ -107,7 +107,7 @@ static VALUE arf_mean_all_weighted(VALUE self, VALUE array_val, VALUE weighted_a Data_Get_Struct(array_val, afstruct, input); Data_Get_Struct(weighted_array_val, afstruct, weighted_array); - // af_mean_all_weigheted(&real_part, &imag_part, input->carray, weighted_array->carray); + af_mean_all_weighted(&real_part, &imag_part, input->carray, weighted_array->carray); return DBL2NUM(real_part); } @@ -131,7 +131,7 @@ static VALUE arf_var_all_weighted(VALUE self, VALUE array_val, VALUE weighted_ar Data_Get_Struct(array_val, afstruct, input); Data_Get_Struct(weighted_array_val, afstruct, weighted_array); - // af_var_all_weigheted(&real_part, &imag_part, input->carray, weighted_array->carray); + af_var_all_weighted(&real_part, &imag_part, input->carray, weighted_array->carray); return DBL2NUM(real_part); } @@ -142,7 +142,7 @@ static VALUE arf_stdev_all(VALUE self, VALUE array_val){ Data_Get_Struct(array_val, afstruct, input); - // af_stddev_all(&real_part, &imag_part, input->carray); + af_stdev_all(&real_part, &imag_part, input->carray); return DBL2NUM(real_part); } @@ -164,7 +164,7 @@ static VALUE arf_corrcoef(VALUE self, VALUE first_array_val, VALUE second_array_ double real_part, imag_part; Data_Get_Struct(first_array_val, afstruct, first_array); - Data_Get_Struct(first_array_val, afstruct, first_array); + Data_Get_Struct(first_array_val, afstruct, second_array); af_corrcoef(&real_part, &imag_part, first_array->carray, second_array->carray); diff --git a/test/statistics_test.rb b/test/statistics_test.rb new file mode 100644 index 0000000..42a8733 --- /dev/null +++ b/test/statistics_test.rb @@ -0,0 +1,72 @@ +require 'test_helper' + +class ArrayFire::StatisticsTest < Minitest::Test + + def setup + @array = ArrayFire::Af_Array.new 2, [3,3], [4, 3, 5, 6, -11, 9 , -22, 11, 1] + @weighted_array = ArrayFire::Af_Array.new 2, [3,3], [1, 2, 2, 0, -2, 2 , 1, 3, 1] + end + + def test_mean + result = ArrayFire::Af_Array.new 2, [3,1], [-4, 1, 5] + assert_equal result, ArrayFire::Statistics.mean(@array, 1) + end + + def test_mean_weighted + result = ArrayFire::Af_Array.new 2, [3,1], [-9.0, 20.333, 5.8] + assert result.approx_equal ArrayFire::Statistics.mean_weighted(@array, @weighted_array, 1) + end + + def test_var + result = ArrayFire::Af_Array.new 2, [3,1], [162.6667, 82.6667, 10.6667] + assert result.approx_equal ArrayFire::Statistics.var(@array, true, 1) + result = ArrayFire::Af_Array.new 2, [3,1], [244.0, 124.0, 16.0] + assert result.approx_equal ArrayFire::Statistics.var(@array, false, 1) + end + + def test_var_weighted + result = ArrayFire::Af_Array.new 2, [3,1], [169.0, -367.111, 8.960] + assert result.approx_equal ArrayFire::Statistics.var_weighted(@array, @weighted_array, 1) + end + + def test_stdev + result = ArrayFire::Af_Array.new 2, [3,1], [12.7540, 9.0921, 3.2659] + assert result.approx_equal ArrayFire::Statistics.stdev(@array, 1) + end + + def test_median + result = ArrayFire::Af_Array.new 2, [3,1], [4, 3, 5] + assert_equal result, ArrayFire::Statistics.median(@array, 1) + end + + def test_mean_all + assert_in_delta 0.667, ArrayFire::Statistics.mean_all(@array) + end + + def mean_all_weighted + assert_in_delta 7.20, ArrayFire::Statistics.mean_all_weighted(@array, @weighted_array) + end + + def test_var_all + assert_in_delta 98.8888, ArrayFire::Statistics.var_all(@array, true) + assert_in_delta 111.250, ArrayFire::Statistics.var_all(@array, false) + end + + def var_all_weighted + assert_in_delta 33.360, ArrayFire::Statistics.var_all_weighted(@array, @weighted_array) + end + + def stdev_all_weighted + assert_in_delta 9.9442, ArrayFire::Statistics.stdev_all(@array) + end + + def test_median_all + assert 4.0, ArrayFire::Statistics.median_all(@array) + end + + def test_corrcoef + array_second = ArrayFire::Af_Array.new 2, [3,3], [42, 1, -15, 52, -121, 9 , -22, 1, 7] + assert_equal 1.0, ArrayFire::Statistics.corrcoef(@array, array_second) + end + +end \ No newline at end of file From fe49c826373ee1b74a3a4e1020e6e7c49fe077e7 Mon Sep 17 00:00:00 2001 From: prasun Date: Thu, 3 Aug 2017 10:51:14 +0530 Subject: [PATCH 092/125] Implement Random Engine --- ext/mri/arrayfire.c | 59 +++++++++++++-------- ext/mri/cmodules/random.c | 104 +++++++++++++++++++++++++++++++++----- ext/mri/ruby_arrayfire.h | 5 ++ 3 files changed, 132 insertions(+), 36 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 7abcfa6..f4cbc8a 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -22,6 +22,8 @@ void Init_arrayfire(); static VALUE arf_init(int argc, VALUE* argv, VALUE self); static VALUE arf_alloc(VALUE klass); static void arf_free(afstruct* af); +static VALUE arf_engine_alloc(VALUE klass); +static void arf_engine_free(afrandomenginestruct* afrandomengine); static VALUE arf_create_array(int argc, VALUE* argv); static VALUE arf_create_handle(int argc, VALUE* argv); @@ -195,19 +197,19 @@ static VALUE arf_det(VALUE self, VALUE val); static VALUE arf_norm(VALUE self, VALUE val); static VALUE arf_is_lapack_available(VALUE self); -static VALUE arf_create_random_engine(VALUE self); -static VALUE arf_retain_random_engine(VALUE self); +static VALUE arf_create_random_engine(VALUE self, VALUE seed_val); +static VALUE arf_retain_random_engine(VALUE self, VALUE engine_val); static VALUE arf_random_engine_set_type(VALUE self); static VALUE arf_random_engine_get_type(VALUE self); -static VALUE arf_random_uniform(VALUE self); -static VALUE arf_random_normal(VALUE self); -static VALUE arf_random_engine_set_seed(VALUE self); +static VALUE arf_random_uniform(VALUE self, VALUE ndims_val, VALUE dim_val, VALUE engine_val); +static VALUE arf_random_normal(VALUE self, VALUE ndims_val, VALUE dim_val, VALUE engine_val); +static VALUE arf_random_engine_set_seed(VALUE self, VALUE engine_val ,VALUE seed_val); static VALUE arf_get_default_random_engine(VALUE self); static VALUE arf_set_default_random_engine_type(VALUE self); static VALUE arf_random_engine_get_seed(VALUE self); static VALUE arf_release_random_engine(VALUE self); -static VALUE arf_randu(VALUE self); -static VALUE arf_randn(VALUE self); +static VALUE arf_randu(VALUE self, VALUE ndims_val, VALUE dim_val); +static VALUE arf_randn(VALUE self, VALUE ndims_val, VALUE dim_val); static VALUE arf_set_seed(VALUE self); static VALUE arf_get_seed(VALUE self); @@ -543,21 +545,22 @@ void Init_arrayfire() { rb_define_singleton_method(Lapack, "is_lapack_available", (METHOD)arf_is_lapack_available, 0); Random = rb_define_class_under(ArrayFire, "Random", rb_cObject); - rb_define_method(Random, "create_random_engine", (METHOD)arf_create_random_engine, 0); - rb_define_method(Random, "retain_random_engine", (METHOD)arf_retain_random_engine, 0); - rb_define_method(Random, "random_engine_set_type", (METHOD)arf_random_engine_set_type, 0); - rb_define_method(Random, "random_engine_get_type", (METHOD)arf_random_engine_get_type, 0); - rb_define_method(Random, "random_uniform", (METHOD)arf_random_uniform, 0); - rb_define_method(Random, "random_normal", (METHOD)arf_random_normal, 0); - rb_define_method(Random, "random_engine_set_seed", (METHOD)arf_random_engine_set_seed, 0); - rb_define_method(Random, "get_default_random_engine", (METHOD)arf_get_default_random_engine, 0); - rb_define_method(Random, "set_default_random_engine_type", (METHOD)arf_set_default_random_engine_type, 0); - rb_define_method(Random, "random_engine_get_seed", (METHOD)arf_random_engine_get_seed, 0); - rb_define_method(Random, "release_random_engine", (METHOD)arf_release_random_engine, 0); - rb_define_method(Random, "randu", (METHOD)arf_randu, 0); - rb_define_method(Random, "randn", (METHOD)arf_randn, 0); - rb_define_method(Random, "set_seed", (METHOD)arf_set_seed, 0); - rb_define_method(Random, "get_seed", (METHOD)arf_get_seed, 0); + rb_define_alloc_func(Random, arf_engine_alloc); + rb_define_singleton_method(Random, "create_random_engine", (METHOD)arf_create_random_engine, 1); + rb_define_singleton_method(Random, "retain_random_engine", (METHOD)arf_retain_random_engine, 1); + rb_define_singleton_method(Random, "random_engine_set_type", (METHOD)arf_random_engine_set_type, 0); + rb_define_singleton_method(Random, "random_engine_get_type", (METHOD)arf_random_engine_get_type, 0); + rb_define_singleton_method(Random, "random_uniform", (METHOD)arf_random_uniform, 3); + rb_define_singleton_method(Random, "random_normal", (METHOD)arf_random_normal, 3); + rb_define_singleton_method(Random, "random_engine_set_seed", (METHOD)arf_random_engine_set_seed, 2); + rb_define_singleton_method(Random, "get_default_random_engine", (METHOD)arf_get_default_random_engine, 0); + rb_define_singleton_method(Random, "set_default_random_engine_type", (METHOD)arf_set_default_random_engine_type, 0); + rb_define_singleton_method(Random, "random_engine_get_seed", (METHOD)arf_random_engine_get_seed, 0); + rb_define_singleton_method(Random, "release_random_engine", (METHOD)arf_release_random_engine, 0); + rb_define_singleton_method(Random, "randu", (METHOD)arf_randu, 2); + rb_define_singleton_method(Random, "randn", (METHOD)arf_randn, 2); + rb_define_singleton_method(Random, "set_seed", (METHOD)arf_set_seed, 0); + rb_define_singleton_method(Random, "get_seed", (METHOD)arf_get_seed, 0); Statistics = rb_define_class_under(ArrayFire, "Statistics", rb_cObject); rb_define_singleton_method(Statistics, "mean", (METHOD)arf_mean, 2); @@ -625,12 +628,24 @@ static VALUE arf_alloc(VALUE klass) return Data_Wrap_Struct(klass, NULL, arf_free, af); } +static VALUE arf_engine_alloc(VALUE klass) +{ + /* allocate */ + afrandomenginestruct* afrandomengine = ALLOC(afrandomenginestruct); + /* wrap */ + return Data_Wrap_Struct(klass, NULL, arf_engine_free, afrandomengine); +} static void arf_free(afstruct* af) { free(af); } +static void arf_engine_free(afrandomenginestruct* afrandomengine) +{ + free(afrandomengine); +} + static VALUE arf_eqeq(VALUE left_val, VALUE right_val) { afstruct* left; afstruct* right; diff --git a/ext/mri/cmodules/random.c b/ext/mri/cmodules/random.c index 0da4249..61abb01 100644 --- a/ext/mri/cmodules/random.c +++ b/ext/mri/cmodules/random.c @@ -1,45 +1,121 @@ -static VALUE arf_create_random_engine(VALUE self){ - return Qnil; +static VALUE arf_create_random_engine(VALUE self, VALUE seed_val){ + afrandomenginestruct* output = ALLOC(afrandomenginestruct); + af_create_random_engine(&output->cengine, AF_RANDOM_ENGINE_DEFAULT, NUM2ULL(seed_val) ) ; + + return Data_Wrap_Struct(Random, NULL, arf_engine_free, output); } -static VALUE arf_retain_random_engine(VALUE self){ - return Qnil; + +static VALUE arf_retain_random_engine(VALUE self, VALUE engine_val){ + afrandomenginestruct* output = ALLOC(afrandomenginestruct); + afrandomenginestruct* engine; + + Data_Get_Struct(engine_val, afrandomenginestruct, engine); + af_retain_random_engine ( &output->cengine, engine->cengine); + + return Data_Wrap_Struct(Random, NULL, arf_engine_free, output); } + static VALUE arf_random_engine_set_type(VALUE self){ return Qnil; } + static VALUE arf_random_engine_get_type(VALUE self){ return Qnil; } -static VALUE arf_random_uniform(VALUE self){ - return Qnil; + +static VALUE arf_random_uniform(VALUE self, VALUE ndims_val, VALUE dim_val, VALUE engine_val){ + afstruct* out_array = ALLOC(afstruct); + afrandomenginestruct* engine; + + Data_Get_Struct(engine_val, afrandomenginestruct, engine); + + dim_t ndims = (dim_t)FIX2LONG(ndims_val); + dim_t* dimensions = (dim_t*)malloc(ndims * sizeof(dim_t)); + dim_t count = 1; + for (dim_t index = 0; index < ndims; index++) { + dimensions[index] = (dim_t)FIX2LONG(RARRAY_AREF(dim_val, index)); + count *= dimensions[index]; + } + + af_random_uniform(&out_array->carray, ndims, dimensions, f32, engine->cengine); + return Data_Wrap_Struct(Af_Array, NULL, arf_free, out_array); } -static VALUE arf_random_normal(VALUE self){ - return Qnil; + +static VALUE arf_random_normal(VALUE self, VALUE ndims_val, VALUE dim_val, VALUE engine_val){ + afstruct* out_array = ALLOC(afstruct); + afrandomenginestruct* engine; + + Data_Get_Struct(engine_val, afrandomenginestruct, engine); + + dim_t ndims = (dim_t)FIX2LONG(ndims_val); + dim_t* dimensions = (dim_t*)malloc(ndims * sizeof(dim_t)); + dim_t count = 1; + for (dim_t index = 0; index < ndims; index++) { + dimensions[index] = (dim_t)FIX2LONG(RARRAY_AREF(dim_val, index)); + count *= dimensions[index]; + } + + af_random_uniform(&out_array->carray, ndims, dimensions, f32, engine->cengine); + return Data_Wrap_Struct(Af_Array, NULL, arf_free, out_array); } -static VALUE arf_random_engine_set_seed(VALUE self){ - return Qnil; + +static VALUE arf_random_engine_set_seed(VALUE self, VALUE engine_val ,VALUE seed_val){ + afrandomenginestruct* engine; + + Data_Get_Struct(engine_val, afrandomenginestruct, engine); + af_random_engine_set_seed (&engine->cengine, NUM2ULL(seed_val)); + + return Data_Wrap_Struct(Random, NULL, arf_engine_free, engine); } + static VALUE arf_get_default_random_engine(VALUE self){ return Qnil; } + static VALUE arf_set_default_random_engine_type(VALUE self){ return Qnil; } + static VALUE arf_random_engine_get_seed(VALUE self){ return Qnil; } + static VALUE arf_release_random_engine(VALUE self){ return Qnil; } -static VALUE arf_randu(VALUE self){ - return Qnil; + +static VALUE arf_randu(VALUE self, VALUE ndims_val, VALUE dim_val){ + afstruct* out_array = ALLOC(afstruct); + + dim_t ndims = (dim_t)FIX2LONG(ndims_val); + dim_t* dimensions = (dim_t*)malloc(ndims * sizeof(dim_t)); + dim_t count = 1; + for (dim_t index = 0; index < ndims; index++) { + dimensions[index] = (dim_t)FIX2LONG(RARRAY_AREF(dim_val, index)); + count *= dimensions[index]; + } + af_randu(&out_array->carray, ndims, dimensions,f32); + return Data_Wrap_Struct(Af_Array, NULL, arf_free, out_array); } -static VALUE arf_randn(VALUE self){ - return Qnil; + +static VALUE arf_randn(VALUE self, VALUE ndims_val, VALUE dim_val){ + afstruct* out_array = ALLOC(afstruct); + + dim_t ndims = (dim_t)FIX2LONG(ndims_val); + dim_t* dimensions = (dim_t*)malloc(ndims * sizeof(dim_t)); + dim_t count = 1; + for (dim_t index = 0; index < ndims; index++) { + dimensions[index] = (dim_t)FIX2LONG(RARRAY_AREF(dim_val, index)); + count *= dimensions[index]; + } + af_randn(&out_array->carray, ndims, dimensions,f32); + return Data_Wrap_Struct(Af_Array, NULL, arf_free, out_array); } + static VALUE arf_set_seed(VALUE self){ return Qnil; } + static VALUE arf_get_seed(VALUE self){ return Qnil; } diff --git a/ext/mri/ruby_arrayfire.h b/ext/mri/ruby_arrayfire.h index 883339d..f58884b 100644 --- a/ext/mri/ruby_arrayfire.h +++ b/ext/mri/ruby_arrayfire.h @@ -9,6 +9,11 @@ typedef struct AF_STRUCT af_array carray; }afstruct; +typedef struct RANDOM_ENGINE_STRUCT +{ + af_random_engine cengine; +}afrandomenginestruct; + #ifndef HAVE_RB_ARRAY_CONST_PTR static inline const VALUE * rb_array_const_ptr(VALUE a) From eb2bcd940efb73921edd57dd1f6efea2b6e667a7 Mon Sep 17 00:00:00 2001 From: prasun Date: Sat, 5 Aug 2017 23:05:07 +0530 Subject: [PATCH 093/125] init multiple dtypes --- ext/mri/arrayfire.c | 12 +++++++----- ext/mri/cmodules/defines.c | 27 +++++++++++++++++++++++++++ ext/mri/ruby_arrayfire.h | 2 ++ 3 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 ext/mri/cmodules/defines.c diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index f4cbc8a..82bc115 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -19,6 +19,8 @@ VALUE Util = Qnil; // prototypes void Init_arrayfire(); + +af_dtype arf_dtype_from_rbsymbol(VALUE sym); static VALUE arf_init(int argc, VALUE* argv, VALUE self); static VALUE arf_alloc(VALUE klass); static void arf_free(afstruct* af); @@ -598,6 +600,7 @@ VALUE arf_init(int argc, VALUE* argv, VALUE self) afstruct* afarray; Data_Get_Struct(self, afstruct, afarray); if(argc > 0){ + af_dtype dtype = (argc == 4) ? arf_dtype_from_rbsymbol(argv[3]) : f32; dim_t ndims = (dim_t)FIX2LONG(argv[0]); dim_t* dimensions = (dim_t*)malloc(ndims * sizeof(dim_t)); @@ -611,15 +614,13 @@ VALUE arf_init(int argc, VALUE* argv, VALUE self) host_array[index] = (float)NUM2DBL(RARRAY_AREF(argv[2], index)); } - af_create_array(&afarray->carray, host_array, ndims, dimensions, f32); - + af_create_array(&afarray->carray, host_array, ndims, dimensions, dtype); af_print_array(afarray->carray); } return self; } - static VALUE arf_alloc(VALUE klass) { /* allocate */ @@ -741,11 +742,12 @@ DEF_UNARY_RUBY_ACCESSOR(ceil, ceil) #include "cmodules/backend.c" #include "cmodules/blas.c" #include "cmodules/cuda.c" +#include "cmodules/data.c" +#include "cmodules/defines.c" #include "cmodules/device.c" #include "cmodules/index.c" -#include "cmodules/opencl.c" -#include "cmodules/data.c" #include "cmodules/lapack.c" +#include "cmodules/opencl.c" #include "cmodules/random.c" #include "cmodules/statistics.c" #include "cmodules/util.c" diff --git a/ext/mri/cmodules/defines.c b/ext/mri/cmodules/defines.c new file mode 100644 index 0000000..3fb78bd --- /dev/null +++ b/ext/mri/cmodules/defines.c @@ -0,0 +1,27 @@ +const char* const DTYPE_NAMES[ARF_NUM_DTYPES] = { + "f32", + "c32", + "f64", + "c64", + "b8", + "s32", + "u32", + "u8", + "s64", + "u64", + "s16", + "u16" +}; + +af_dtype arf_dtype_from_rbsymbol(VALUE sym) { + ID sym_id = SYM2ID(sym); + + for (size_t index = 0; index < ARF_NUM_DTYPES; ++index) { + if (sym_id == rb_intern(DTYPE_NAMES[index])) { + return static_cast(index); + } + } + + VALUE str = rb_any_to_s(sym); + rb_raise(rb_eArgError, "invalid data type symbol (:%s) specified", RSTRING_PTR(str)); +} diff --git a/ext/mri/ruby_arrayfire.h b/ext/mri/ruby_arrayfire.h index f58884b..1f8f5c1 100644 --- a/ext/mri/ruby_arrayfire.h +++ b/ext/mri/ruby_arrayfire.h @@ -14,6 +14,8 @@ typedef struct RANDOM_ENGINE_STRUCT af_random_engine cengine; }afrandomenginestruct; +#define ARF_NUM_DTYPES 12 + #ifndef HAVE_RB_ARRAY_CONST_PTR static inline const VALUE * rb_array_const_ptr(VALUE a) From 4fc9e5ab0f75a35567dcf0e166852b300d9d3a16 Mon Sep 17 00:00:00 2001 From: prasun Date: Sun, 6 Aug 2017 01:04:06 +0530 Subject: [PATCH 094/125] pass matrix property as an argument to #matmul --- ext/mri/arrayfire.c | 8 ++++---- ext/mri/cmodules/blas.c | 6 ++++-- ext/mri/cmodules/defines.c | 29 +++++++++++++++++++++++++++++ test/blas_test.rb | 2 +- 4 files changed, 38 insertions(+), 7 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 82bc115..ae80e67 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -21,6 +21,8 @@ VALUE Util = Qnil; void Init_arrayfire(); af_dtype arf_dtype_from_rbsymbol(VALUE sym); +af_mat_prop arf_mat_type_from_rbsymbol(VALUE sym); + static VALUE arf_init(int argc, VALUE* argv, VALUE self); static VALUE arf_alloc(VALUE klass); static void arf_free(afstruct* af); @@ -100,7 +102,7 @@ static VALUE arf_get_active_backend(VALUE self); static VALUE arf_get_backend_device_id(VALUE self, VALUE array_val); static VALUE arf_set_backend(VALUE self); -static VALUE arf_matmul(VALUE self, VALUE left_val, VALUE right_val); +static VALUE arf_matmul(VALUE self, VALUE left_val, VALUE right_val, VALUE prop_val); static VALUE arf_dot(VALUE self, VALUE left_val, VALUE right_val); static VALUE arf_transpose(VALUE self, VALUE input); static VALUE arf_transpose_inplace(VALUE self, VALUE input); @@ -313,8 +315,6 @@ 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); -static VALUE arf_matmul(VALUE self, VALUE left_val, VALUE right_val); - void Init_arrayfire() { ArrayFire = rb_define_module("ArrayFire"); @@ -470,7 +470,7 @@ void Init_arrayfire() { rb_define_method(Device, "get_device_ptr", (METHOD)arf_get_device_ptr, 0); Blas = rb_define_class_under(ArrayFire, "BLAS", rb_cObject); - rb_define_singleton_method(Blas, "matmul", (METHOD)arf_matmul, 2); + rb_define_singleton_method(Blas, "matmul", (METHOD)arf_matmul, 3); rb_define_singleton_method(Blas, "dot", (METHOD)arf_dot, 2); rb_define_singleton_method(Blas, "transpose", (METHOD)arf_transpose, 1); rb_define_singleton_method(Blas, "transpose_inplace", (METHOD)arf_transpose_inplace, 1); diff --git a/ext/mri/cmodules/blas.c b/ext/mri/cmodules/blas.c index fbc9498..5f2da50 100644 --- a/ext/mri/cmodules/blas.c +++ b/ext/mri/cmodules/blas.c @@ -1,4 +1,4 @@ -static VALUE arf_matmul(VALUE self, VALUE left_val, VALUE right_val){ +static VALUE arf_matmul(VALUE self, VALUE left_val, VALUE right_val, VALUE prop_val){ afstruct* left; afstruct* right; @@ -7,7 +7,9 @@ static VALUE arf_matmul(VALUE self, VALUE left_val, VALUE right_val){ Data_Get_Struct(left_val, afstruct, left); Data_Get_Struct(right_val, afstruct, right); - af_matmul(&result->carray, left->carray, right->carray, AF_MAT_NONE, AF_MAT_NONE); + af_mat_prop mat_prop = arf_mat_type_from_rbsymbol(prop_val); + + af_matmul(&result->carray, left->carray, right->carray, AF_MAT_NONE, mat_prop); return Data_Wrap_Struct(CLASS_OF(left_val), NULL, arf_free, result); } diff --git a/ext/mri/cmodules/defines.c b/ext/mri/cmodules/defines.c index 3fb78bd..21f44c4 100644 --- a/ext/mri/cmodules/defines.c +++ b/ext/mri/cmodules/defines.c @@ -13,6 +13,22 @@ const char* const DTYPE_NAMES[ARF_NUM_DTYPES] = { "u16" }; +std::map MAT_PROPERTIES = { + {"AF_MAT_NONE", 0}, + {"AF_MAT_TRANS", 1}, + {"AF_MAT_CTRANS", 2}, + {"AF_MAT_CONJ", 4}, + {"AF_MAT_UPPER", 32}, + {"AF_MAT_LOWER", 64}, + {"AF_MAT_DIAG_UNIT", 128}, + {"AF_MAT_SYM", 512}, + {"AF_MAT_POSDEF", 1024}, + {"AF_MAT_ORTHOG", 2048}, + {"AF_MAT_TRI_DIAG", 4096}, + {"AF_MAT_BLOCK_DIAG", 8192} +}; + + af_dtype arf_dtype_from_rbsymbol(VALUE sym) { ID sym_id = SYM2ID(sym); @@ -25,3 +41,16 @@ af_dtype arf_dtype_from_rbsymbol(VALUE sym) { VALUE str = rb_any_to_s(sym); rb_raise(rb_eArgError, "invalid data type symbol (:%s) specified", RSTRING_PTR(str)); } + +af_mat_prop arf_mat_type_from_rbsymbol(VALUE sym) { + ID sym_id = SYM2ID(sym); + + for(std::map::value_type& entry : MAT_PROPERTIES) { + if (sym_id == rb_intern(entry.first)) { + return static_cast(entry.second); + } + } + + VALUE str = rb_any_to_s(sym); + rb_raise(rb_eArgError, "invalid matrix type symbol (:%s) specified", RSTRING_PTR(str)); +} diff --git a/test/blas_test.rb b/test/blas_test.rb index 622749d..84f563f 100644 --- a/test/blas_test.rb +++ b/test/blas_test.rb @@ -11,7 +11,7 @@ def setup def test_matmul result = ArrayFire::Af_Array.new 2, [2,2],[-2681.0, 1653.0, -67.0, 966.0] - assert_equal result, ArrayFire::BLAS.matmul(@matrix_left, @matrix_right) + assert_equal result, ArrayFire::BLAS.matmul(@matrix_left, @matrix_right, :AF_MAT_NONE) end def test_dot From 266c1548809b3670f7a264da37117400927656bb Mon Sep 17 00:00:00 2001 From: prasun Date: Sun, 6 Aug 2017 01:39:29 +0530 Subject: [PATCH 095/125] add more definitions --- ext/mri/arrayfire.c | 3 ++ ext/mri/cmodules/defines.c | 63 ++++++++++++++++++++++++++++++++++++++ ext/mri/ruby_arrayfire.cpp | 2 ++ ext/mri/ruby_arrayfire.h | 2 ++ 4 files changed, 70 insertions(+) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index ae80e67..776cb3d 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -21,7 +21,10 @@ VALUE Util = Qnil; void Init_arrayfire(); af_dtype arf_dtype_from_rbsymbol(VALUE sym); +af_source arf_source_from_rbsymbol(VALUE sym); af_mat_prop arf_mat_type_from_rbsymbol(VALUE sym); +af_norm_type arf_norm_type_from_rbsymbol(VALUE sym); +af_moment_type arf_moment_type_from_rbsymbol(VALUE sym); static VALUE arf_init(int argc, VALUE* argv, VALUE self); static VALUE arf_alloc(VALUE klass); diff --git a/ext/mri/cmodules/defines.c b/ext/mri/cmodules/defines.c index 21f44c4..d969ec3 100644 --- a/ext/mri/cmodules/defines.c +++ b/ext/mri/cmodules/defines.c @@ -13,6 +13,11 @@ const char* const DTYPE_NAMES[ARF_NUM_DTYPES] = { "u16" }; +const char* const SOURCE_NAMES[ARF_NUM_SOURCES] = { + "afDevice", ///< Device pointer + "afHost" ///< Host pointer +}; + std::map MAT_PROPERTIES = { {"AF_MAT_NONE", 0}, {"AF_MAT_TRANS", 1}, @@ -28,6 +33,25 @@ std::map MAT_PROPERTIES = { {"AF_MAT_BLOCK_DIAG", 8192} }; +const char* const NORM_TYPES[ARF_NUM_NORM_TYPES] = { + "AF_NORM_VECTOR_1", ///< treats the input as a vector and returns the sum of absolute values + "AF_NORM_VECTOR_INF", ///< treats the input as a vector and returns the max of absolute values + "AF_NORM_VECTOR_2", ///< treats the input as a vector and returns euclidean norm + "AF_NORM_VECTOR_P", ///< treats the input as a vector and returns the p-norm + "AF_NORM_MATRIX_1", ///< return the max of column sums + "AF_NORM_MATRIX_INF", ///< return the max of row sums + "AF_NORM_MATRIX_2", ///< returns the max singular value). Currently NOT SUPPORTED + "AF_NORM_MATRIX_L_PQ", ///< returns Lpq-norm + "AF_NORM_EUCLID" ///< The default. Same as AF_NORM_VECTOR_2 +}; + +std::map MOMENT_TYPES = { + {"AF_MOMENT_M00", 1}, + {"AF_MOMENT_M01", 2}, + {"AF_MOMENT_M10", 4}, + {"AF_MOMENT_M11", 8} + //AF_MOMENT_FIRST_ORDER = AF_MOMENT_M00 | AF_MOMENT_M01 | AF_MOMENT_M10 | AF_MOMENT_M11 +}; af_dtype arf_dtype_from_rbsymbol(VALUE sym) { ID sym_id = SYM2ID(sym); @@ -42,6 +66,19 @@ af_dtype arf_dtype_from_rbsymbol(VALUE sym) { rb_raise(rb_eArgError, "invalid data type symbol (:%s) specified", RSTRING_PTR(str)); } +af_source arf_source_from_rbsymbol(VALUE sym) { + ID sym_id = SYM2ID(sym); + + for (size_t index = 0; index < ARF_NUM_SOURCES; ++index) { + if (sym_id == rb_intern(SOURCE_NAMES[index])) { + return static_cast(index); + } + } + + VALUE str = rb_any_to_s(sym); + rb_raise(rb_eArgError, "invalid data type symbol (:%s) specified", RSTRING_PTR(str)); +} + af_mat_prop arf_mat_type_from_rbsymbol(VALUE sym) { ID sym_id = SYM2ID(sym); @@ -54,3 +91,29 @@ af_mat_prop arf_mat_type_from_rbsymbol(VALUE sym) { VALUE str = rb_any_to_s(sym); rb_raise(rb_eArgError, "invalid matrix type symbol (:%s) specified", RSTRING_PTR(str)); } + +af_norm_type arf_norm_type_from_rbsymbol(VALUE sym) { + ID sym_id = SYM2ID(sym); + + for (size_t index = 0; index < ARF_NUM_NORM_TYPES; ++index) { + if (sym_id == rb_intern(NORM_TYPES[index])) { + return static_cast(index); + } + } + + VALUE str = rb_any_to_s(sym); + rb_raise(rb_eArgError, "invalid norm type symbol (:%s) specified", RSTRING_PTR(str)); +} + +af_moment_type arf_moment_type_from_rbsymbol(VALUE sym) { + ID sym_id = SYM2ID(sym); + + for(std::map::value_type& entry : MOMENT_TYPES) { + if (sym_id == rb_intern(entry.first)) { + return static_cast(entry.second); + } + } + + VALUE str = rb_any_to_s(sym); + rb_raise(rb_eArgError, "invalid moment type symbol (:%s) specified", RSTRING_PTR(str)); +} diff --git a/ext/mri/ruby_arrayfire.cpp b/ext/mri/ruby_arrayfire.cpp index 0070143..e3f6326 100644 --- a/ext/mri/ruby_arrayfire.cpp +++ b/ext/mri/ruby_arrayfire.cpp @@ -4,6 +4,8 @@ #include #include #include +#include + /* * Project Includes */ diff --git a/ext/mri/ruby_arrayfire.h b/ext/mri/ruby_arrayfire.h index 1f8f5c1..ef2205d 100644 --- a/ext/mri/ruby_arrayfire.h +++ b/ext/mri/ruby_arrayfire.h @@ -15,6 +15,8 @@ typedef struct RANDOM_ENGINE_STRUCT }afrandomenginestruct; #define ARF_NUM_DTYPES 12 +#define ARF_NUM_SOURCES 2 +#define ARF_NUM_NORM_TYPES 9 #ifndef HAVE_RB_ARRAY_CONST_PTR static inline const VALUE * From 5dcddf7e8ad2ded777d4f910db8a76550ac0ed4f Mon Sep 17 00:00:00 2001 From: prasun Date: Sun, 6 Aug 2017 02:39:11 +0530 Subject: [PATCH 096/125] pass mat_properties to #matmul and #dot --- ext/mri/arrayfire.c | 8 ++++---- ext/mri/cmodules/blas.c | 14 +++++++++----- test/blas_test.rb | 4 ++-- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 776cb3d..6a5c185 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -105,8 +105,8 @@ static VALUE arf_get_active_backend(VALUE self); static VALUE arf_get_backend_device_id(VALUE self, VALUE array_val); static VALUE arf_set_backend(VALUE self); -static VALUE arf_matmul(VALUE self, VALUE left_val, VALUE right_val, VALUE prop_val); -static VALUE arf_dot(VALUE self, VALUE left_val, VALUE right_val); +static VALUE arf_matmul(VALUE self, VALUE left_val, VALUE right_val, VALUE left_prop_val, VALUE right_prop_val); +static VALUE arf_dot(VALUE self, VALUE left_val, VALUE right_val, VALUE left_prop_val, VALUE right_prop_val); static VALUE arf_transpose(VALUE self, VALUE input); static VALUE arf_transpose_inplace(VALUE self, VALUE input); @@ -473,8 +473,8 @@ void Init_arrayfire() { rb_define_method(Device, "get_device_ptr", (METHOD)arf_get_device_ptr, 0); Blas = rb_define_class_under(ArrayFire, "BLAS", rb_cObject); - rb_define_singleton_method(Blas, "matmul", (METHOD)arf_matmul, 3); - rb_define_singleton_method(Blas, "dot", (METHOD)arf_dot, 2); + rb_define_singleton_method(Blas, "matmul", (METHOD)arf_matmul, 4); + rb_define_singleton_method(Blas, "dot", (METHOD)arf_dot, 4); rb_define_singleton_method(Blas, "transpose", (METHOD)arf_transpose, 1); rb_define_singleton_method(Blas, "transpose_inplace", (METHOD)arf_transpose_inplace, 1); diff --git a/ext/mri/cmodules/blas.c b/ext/mri/cmodules/blas.c index 5f2da50..5e023a8 100644 --- a/ext/mri/cmodules/blas.c +++ b/ext/mri/cmodules/blas.c @@ -1,4 +1,4 @@ -static VALUE arf_matmul(VALUE self, VALUE left_val, VALUE right_val, VALUE prop_val){ +static VALUE arf_matmul(VALUE self, VALUE left_val, VALUE right_val, VALUE left_prop_val, VALUE right_prop_val){ afstruct* left; afstruct* right; @@ -7,14 +7,15 @@ static VALUE arf_matmul(VALUE self, VALUE left_val, VALUE right_val, VALUE prop_ Data_Get_Struct(left_val, afstruct, left); Data_Get_Struct(right_val, afstruct, right); - af_mat_prop mat_prop = arf_mat_type_from_rbsymbol(prop_val); + af_mat_prop left_mat_prop = arf_mat_type_from_rbsymbol(left_prop_val); + af_mat_prop right_mat_prop = arf_mat_type_from_rbsymbol(right_prop_val); - af_matmul(&result->carray, left->carray, right->carray, AF_MAT_NONE, mat_prop); + af_matmul(&result->carray, left->carray, right->carray, left_mat_prop, right_mat_prop); return Data_Wrap_Struct(CLASS_OF(left_val), NULL, arf_free, result); } -static VALUE arf_dot(VALUE self, VALUE left_val, VALUE right_val){ +static VALUE arf_dot(VALUE self, VALUE left_val, VALUE right_val, VALUE left_prop_val, VALUE right_prop_val){ afstruct* left; afstruct* right; afstruct* result = ALLOC(afstruct); @@ -22,7 +23,10 @@ static VALUE arf_dot(VALUE self, VALUE left_val, VALUE right_val){ Data_Get_Struct(left_val, afstruct, left); Data_Get_Struct(right_val, afstruct, right); - af_dot(&result->carray, left->carray, right->carray, AF_MAT_NONE, AF_MAT_NONE); + af_mat_prop left_mat_prop = arf_mat_type_from_rbsymbol(left_prop_val); + af_mat_prop right_mat_prop = arf_mat_type_from_rbsymbol(right_prop_val); + + af_dot(&result->carray, left->carray, right->carray, left_mat_prop, right_mat_prop); return Data_Wrap_Struct(CLASS_OF(left_val), NULL, arf_free, result); } diff --git a/test/blas_test.rb b/test/blas_test.rb index 84f563f..dfa7b91 100644 --- a/test/blas_test.rb +++ b/test/blas_test.rb @@ -11,12 +11,12 @@ def setup def test_matmul result = ArrayFire::Af_Array.new 2, [2,2],[-2681.0, 1653.0, -67.0, 966.0] - assert_equal result, ArrayFire::BLAS.matmul(@matrix_left, @matrix_right, :AF_MAT_NONE) + assert_equal result, ArrayFire::BLAS.matmul(@matrix_left, @matrix_right, :AF_MAT_NONE, :AF_MAT_NONE) end def test_dot result = ArrayFire::Af_Array.new 1,[1],[5965.0] - assert_equal result, ArrayFire::BLAS.dot(@vector_left, @vector_right) + assert_equal result, ArrayFire::BLAS.dot(@vector_left, @vector_right, :AF_MAT_NONE, :AF_MAT_NONE) end def test_transpose From 8514495035229258d166169df00f9d46ead60b8d Mon Sep 17 00:00:00 2001 From: prasun Date: Mon, 7 Aug 2017 13:31:40 +0530 Subject: [PATCH 097/125] arrays and lists for passing symbols --- ext/mri/arrayfire.c | 6 ++- ext/mri/cmodules/defines.c | 78 +++++++++++++++++++++++++++++++++++++- ext/mri/ruby_arrayfire.h | 3 +- 3 files changed, 82 insertions(+), 5 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 6a5c185..2d0122b 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -20,11 +20,13 @@ VALUE Util = Qnil; // prototypes void Init_arrayfire(); +af_backend arf_backend_type_from_rbsymbol(VALUE sym); af_dtype arf_dtype_from_rbsymbol(VALUE sym); -af_source arf_source_from_rbsymbol(VALUE sym); af_mat_prop arf_mat_type_from_rbsymbol(VALUE sym); -af_norm_type arf_norm_type_from_rbsymbol(VALUE sym); af_moment_type arf_moment_type_from_rbsymbol(VALUE sym); +af_norm_type arf_norm_type_from_rbsymbol(VALUE sym); +af_random_engine_type arf_randome_engine_type_from_rbsymbol(VALUE sym); +af_source arf_source_from_rbsymbol(VALUE sym); static VALUE arf_init(int argc, VALUE* argv, VALUE self); static VALUE arf_alloc(VALUE klass); diff --git a/ext/mri/cmodules/defines.c b/ext/mri/cmodules/defines.c index d969ec3..7687195 100644 --- a/ext/mri/cmodules/defines.c +++ b/ext/mri/cmodules/defines.c @@ -14,8 +14,8 @@ const char* const DTYPE_NAMES[ARF_NUM_DTYPES] = { }; const char* const SOURCE_NAMES[ARF_NUM_SOURCES] = { - "afDevice", ///< Device pointer - "afHost" ///< Host pointer + "afDevice", ///< Device pointer + "afHost" ///< Host pointer }; std::map MAT_PROPERTIES = { @@ -53,6 +53,54 @@ std::map MOMENT_TYPES = { //AF_MOMENT_FIRST_ORDER = AF_MOMENT_M00 | AF_MOMENT_M01 | AF_MOMENT_M10 | AF_MOMENT_M11 }; +std::map BACKEND_TYPES = { + {"AF_BACKEND_DEFAULT" , 0}, ///< Default backend order: OpenCL -> CUDA -> CPU + {"AF_BACKEND_CPU" , 1}, ///< CPU a.k.a sequential algorithms + {"AF_BACKEND_CUDA" , 2}, ///< CUDA Compute Backend + {"AF_BACKEND_OPENCL" , 4} ///< OpenCL Compute Backend +}; + +std::map RANDOM_ENGINE_TYPES = { + {"AF_RANDOM_ENGINE_PHILOX_4X32_10" , 100}, //Philox variant with N = 4, W = 32 and Rounds = 10 + {"AF_RANDOM_ENGINE_THREEFRY_2X32_16" , 200}, //Threefry variant with N = 2, W = 32 and Rounds = 16 + {"AF_RANDOM_ENGINE_MERSENNE_GP11213" , 300}, //Mersenne variant with MEXP = 11213 + {"AF_RANDOM_ENGINE_PHILOX" , 100}, //Resolves to Philox 4x32_10 + {"AF_RANDOM_ENGINE_THREEFRY" , 200}, //Resolves to Threefry 2X32_16 + {"AF_RANDOM_ENGINE_MERSENNE" , 300}, //Resolves to Mersenne GP 11213 + {"AF_RANDOM_ENGINE_DEFAULT" , 100}, //Resolves to Philox +}; + +std::map ERROR_TYPES = { + {"AF_SUCCESS" , 0}, + {"AF_ERR_NO_MEM" , 101}, + {"AF_ERR_DRIVER" , 102}, + {"AF_ERR_RUNTIME" , 103}, + {"AF_ERR_INVALID_ARRAY" , 201}, + {"AF_ERR_ARG" , 202}, + {"AF_ERR_SIZE" , 203}, + {"AF_ERR_TYPE" , 204}, + {"AF_ERR_DIFF_TYPE" , 205}, + {"AF_ERR_BATCH" , 207}, + {"AF_ERR_DEVICE" , 208}, + {"AF_ERR_NOT_SUPPORTED" , 301}, + {"AF_ERR_NOT_CONFIGURED" , 302}, + {"AF_ERR_NONFREE" , 303}, + {"AF_ERR_NO_DBL" , 401}, + {"AF_ERR_NO_GFX" , 402}, + {"AF_ERR_LOAD_LIB" , 501}, + {"AF_ERR_LOAD_SYM" , 502}, + {"AF_ERR_ARR_BKND_MISMATCH" , 503}, + {"AF_ERR_INTERNAL" , 998}, + {"AF_ERR_UNKNOWN" , 999} +}; + +const char* const STORAGE_TYPES[ARF_NUM_STORAGE_TYPES] = { + "AF_STORAGE_DENSE", ///< Storage type is dense + "AF_STORAGE_CSR", ///< Storage type is CSR + "AF_STORAGE_CSC", ///< Storage type is CSC + "AF_STORAGE_COO", ///< Storage type is COO +}; + af_dtype arf_dtype_from_rbsymbol(VALUE sym) { ID sym_id = SYM2ID(sym); @@ -117,3 +165,29 @@ af_moment_type arf_moment_type_from_rbsymbol(VALUE sym) { VALUE str = rb_any_to_s(sym); rb_raise(rb_eArgError, "invalid moment type symbol (:%s) specified", RSTRING_PTR(str)); } + +af_backend arf_backend_type_from_rbsymbol(VALUE sym) { + ID sym_id = SYM2ID(sym); + + for(std::map::value_type& entry : BACKEND_TYPES) { + if (sym_id == rb_intern(entry.first)) { + return static_cast(entry.second); + } + } + + VALUE str = rb_any_to_s(sym); + rb_raise(rb_eArgError, "invalid backend type symbol (:%s) specified", RSTRING_PTR(str)); +} + +af_random_engine_type arf_randome_engine_type_from_rbsymbol(VALUE sym) { + ID sym_id = SYM2ID(sym); + + for(std::map::value_type& entry : RANDOM_ENGINE_TYPES) { + if (sym_id == rb_intern(entry.first)) { + return static_cast(entry.second); + } + } + + VALUE str = rb_any_to_s(sym); + rb_raise(rb_eArgError, "invalid backend type symbol (:%s) specified", RSTRING_PTR(str)); +} diff --git a/ext/mri/ruby_arrayfire.h b/ext/mri/ruby_arrayfire.h index ef2205d..47f1309 100644 --- a/ext/mri/ruby_arrayfire.h +++ b/ext/mri/ruby_arrayfire.h @@ -15,8 +15,9 @@ typedef struct RANDOM_ENGINE_STRUCT }afrandomenginestruct; #define ARF_NUM_DTYPES 12 -#define ARF_NUM_SOURCES 2 #define ARF_NUM_NORM_TYPES 9 +#define ARF_NUM_SOURCES 2 +#define ARF_NUM_STORAGE_TYPES 4 #ifndef HAVE_RB_ARRAY_CONST_PTR static inline const VALUE * From f0a4d3d850a51645a236283fa62a9f501db8a089 Mon Sep 17 00:00:00 2001 From: prasun Date: Mon, 7 Aug 2017 22:21:39 +0530 Subject: [PATCH 098/125] add bindings for device module --- ext/mri/arrayfire.c | 84 ++++++++++++++++++++++----------------- ext/mri/cmodules/device.c | 75 +++++++++++++++++++++++++--------- 2 files changed, 103 insertions(+), 56 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 2d0122b..898454d 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -116,13 +116,14 @@ static VALUE arf_get_stream(VALUE self); static VALUE arf_get_native_id(VALUE self); static VALUE arf_set_native_id(VALUE self); +// device static VALUE arf_info(VALUE self); static VALUE arf_init2(VALUE self); -static VALUE arf_info_string(VALUE self); +static VALUE arf_info_string(VALUE self, VALUE bool_val); static VALUE arf_device_info(VALUE self); static VALUE arf_get_device_count(VALUE self); -static VALUE arf_get_dbl_support(VALUE self); -static VALUE arf_set_device(VALUE self); +static VALUE arf_get_dbl_support(VALUE self, VALUE device); +static VALUE arf_set_device(VALUE self, VALUE device); static VALUE arf_get_device(VALUE self); static VALUE arf_sync(VALUE self); static VALUE arf_alloc_device(VALUE self); @@ -133,15 +134,15 @@ static VALUE arf_alloc_host(VALUE self); static VALUE arf_free_host(VALUE self); static VALUE arf_device_array(VALUE self); static VALUE arf_device_mem_info(VALUE self); -static VALUE arf_print_mem_info(VALUE self); -static VALUE arf_device_gc(VALUE self); -static VALUE arf_set_mem_step_size(VALUE self); +static VALUE arf_print_mem_info(VALUE self, VALUE msg_val, VALUE device_id_val); +static void arf_device_gc(VALUE self); +static VALUE arf_set_mem_step_size(VALUE self, VALUE step_bytes); static VALUE arf_get_mem_step_size(VALUE self); -static VALUE arf_lock_device_ptr(VALUE self); -static VALUE arf_unlock_device_ptr(VALUE self); -static VALUE arf_lock_array(VALUE self); -static VALUE arf_unlock_array(VALUE self); -static VALUE arf_is_locked_array(VALUE self); +static VALUE arf_lock_device_ptr(VALUE self, VALUE array_val); +static VALUE arf_unlock_device_ptr(VALUE self, VALUE array_val); +static VALUE arf_lock_array(VALUE self, VALUE array_val); +static VALUE arf_unlock_array(VALUE self, VALUE array_val); +static VALUE arf_is_locked_array(VALUE self, VALUE array_val); static VALUE arf_get_device_ptr(VALUE self); static VALUE arf_get_context(VALUE self); @@ -447,32 +448,32 @@ void Init_arrayfire() { Device = rb_define_class_under(ArrayFire, "Device", rb_cObject); rb_define_singleton_method(Device, "info", (METHOD)arf_info, 0); - rb_define_method(Device, "init", (METHOD)arf_init2, 0); - rb_define_method(Device, "info_string", (METHOD)arf_info_string, 0); - rb_define_method(Device, "device_info", (METHOD)arf_device_info, 0); - rb_define_method(Device, "get_device_count", (METHOD)arf_get_device_count, 0); - rb_define_method(Device, "get_dbl_support", (METHOD)arf_get_dbl_support, 0); - rb_define_method(Device, "set_device", (METHOD)arf_set_device, 0); - rb_define_method(Device, "get_device", (METHOD)arf_get_device, 0); - rb_define_method(Device, "sync", (METHOD)arf_sync, 0); - rb_define_method(Device, "alloc_device", (METHOD)arf_alloc_device, 0); - rb_define_method(Device, "free_device", (METHOD)arf_free_device, 0); - rb_define_method(Device, "alloc_pinned", (METHOD)arf_alloc_pinned, 0); - rb_define_method(Device, "free_pinned", (METHOD)arf_free_pinned, 0); - rb_define_method(Device, "alloc_host", (METHOD)arf_alloc_host, 0); - rb_define_method(Device, "free_host", (METHOD)arf_free_host, 0); - rb_define_method(Device, "device_array", (METHOD)arf_device_array, 0); - rb_define_method(Device, "device_mem_info", (METHOD)arf_device_mem_info, 0); - rb_define_method(Device, "print_mem_info", (METHOD)arf_print_mem_info, 0); - rb_define_method(Device, "device_gc", (METHOD)arf_device_gc, 0); - rb_define_method(Device, "set_mem_step_size", (METHOD)arf_set_mem_step_size, 0); - rb_define_method(Device, "get_mem_step_size", (METHOD)arf_get_mem_step_size, 0); - rb_define_method(Device, "lock_device_ptr", (METHOD)arf_lock_device_ptr, 0); - rb_define_method(Device, "unlock_device_ptr", (METHOD)arf_unlock_device_ptr, 0); - rb_define_method(Device, "lock_array", (METHOD)arf_lock_array, 0); - rb_define_method(Device, "unlock_array", (METHOD)arf_unlock_array, 0); - rb_define_method(Device, "is_locked_array", (METHOD)arf_is_locked_array, 0); - rb_define_method(Device, "get_device_ptr", (METHOD)arf_get_device_ptr, 0); + rb_define_singleton_method(Device, "init", (METHOD)arf_init2, 0); + rb_define_singleton_method(Device, "info_string", (METHOD)arf_info_string, 0); + rb_define_singleton_method(Device, "device_info", (METHOD)arf_device_info, 0); + rb_define_singleton_method(Device, "get_device_count", (METHOD)arf_get_device_count, 0); + rb_define_singleton_method(Device, "get_dbl_support", (METHOD)arf_get_dbl_support, 1); + rb_define_singleton_method(Device, "set_device", (METHOD)arf_set_device, 1); + rb_define_singleton_method(Device, "get_device", (METHOD)arf_get_device, 0); + rb_define_singleton_method(Device, "sync", (METHOD)arf_sync, 0); + rb_define_singleton_method(Device, "alloc_device", (METHOD)arf_alloc_device, 0); + rb_define_singleton_method(Device, "free_device", (METHOD)arf_free_device, 0); + rb_define_singleton_method(Device, "alloc_pinned", (METHOD)arf_alloc_pinned, 0); + rb_define_singleton_method(Device, "free_pinned", (METHOD)arf_free_pinned, 0); + rb_define_singleton_method(Device, "alloc_host", (METHOD)arf_alloc_host, 0); + rb_define_singleton_method(Device, "free_host", (METHOD)arf_free_host, 0); + rb_define_singleton_method(Device, "device_array", (METHOD)arf_device_array, 0); + rb_define_singleton_method(Device, "device_mem_info", (METHOD)arf_device_mem_info, 0); + rb_define_singleton_method(Device, "print_mem_info", (METHOD)arf_print_mem_info, 2); + rb_define_singleton_method(Device, "device_gc", (METHOD)arf_device_gc, 0); + rb_define_singleton_method(Device, "set_mem_step_size", (METHOD)arf_set_mem_step_size, 1); + rb_define_singleton_method(Device, "get_mem_step_size", (METHOD)arf_get_mem_step_size, 0); + rb_define_singleton_method(Device, "lock_device_ptr", (METHOD)arf_lock_device_ptr, 1); + rb_define_singleton_method(Device, "unlock_device_ptr", (METHOD)arf_unlock_device_ptr, 1); + rb_define_singleton_method(Device, "lock_array", (METHOD)arf_lock_array, 1); + rb_define_singleton_method(Device, "unlock_array", (METHOD)arf_unlock_array, 1); + rb_define_singleton_method(Device, "is_locked_array", (METHOD)arf_is_locked_array, 1); + rb_define_singleton_method(Device, "get_device_ptr", (METHOD)arf_get_device_ptr, 0); Blas = rb_define_class_under(ArrayFire, "BLAS", rb_cObject); rb_define_singleton_method(Blas, "matmul", (METHOD)arf_matmul, 4); @@ -600,6 +601,15 @@ void Init_arrayfire() { } +/* + * call-seq: + * new(dimesnsion) -> Af_Array + * new(dims, dimesnsion, elements, data_type) -> NMatrix + * + * Create a new Af_Array. + * + */ + VALUE arf_init(int argc, VALUE* argv, VALUE self) { afstruct* afarray; diff --git a/ext/mri/cmodules/device.c b/ext/mri/cmodules/device.c index bc4b4b3..4d5f1fd 100644 --- a/ext/mri/cmodules/device.c +++ b/ext/mri/cmodules/device.c @@ -4,31 +4,43 @@ static VALUE arf_info(VALUE self){ } static VALUE arf_init2(VALUE self){ + af_init(); return Qnil; } -static VALUE arf_info_string(VALUE self){ - return Qnil; +static VALUE arf_info_string(VALUE self, VALUE bool_val){ + char* str; + af_info_string(&str, RTEST(bool_val)); + return rb_str_new_cstr(str); } static VALUE arf_device_info(VALUE self){ + char d_name, d_platform, d_toolkit, d_compute; + af_device_info(&d_name, &d_platform, &d_toolkit, &d_compute); return Qnil; } static VALUE arf_get_device_count(VALUE self){ - return Qnil; + int num_of_devices; + af_get_device_count(&num_of_devices); + return INT2NUM(num_of_devices); } -static VALUE arf_get_dbl_support(VALUE self){ - return Qnil; +static VALUE arf_get_dbl_support(VALUE self, VALUE device){ + bool available; + af_get_dbl_support(&available, NUM2INT(device)); + return available ? Qtrue : Qfalse; } -static VALUE arf_set_device(VALUE self){ +static VALUE arf_set_device(VALUE self, VALUE device){ + af_set_device(NUM2INT(device)); return Qnil; } static VALUE arf_get_device(VALUE self){ - return Qnil; + int device; + af_get_device(&device); + return INT2NUM(device); } static VALUE arf_sync(VALUE self){ @@ -64,43 +76,68 @@ static VALUE arf_device_array(VALUE self){ } static VALUE arf_device_mem_info(VALUE self){ + size_t alloc_bytes, alloc_buffers, lock_bytes, lock_buffers; + af_device_mem_info( &alloc_bytes, &alloc_buffers, &lock_bytes, &lock_buffers); + printf("Allocated Bytes: %d\nAllocated buffers: %d\nLock Bytes: %d\nLock Buffers: %d\n", + alloc_bytes, alloc_buffers, lock_bytes, lock_buffers); return Qnil; } -static VALUE arf_print_mem_info(VALUE self){ +static VALUE arf_print_mem_info(VALUE self, VALUE msg_val, VALUE device_id_val){ + const char* msg = StringValuePtr(msg_val); + af_print_mem_info( msg, NUM2INT(device_id_val)); return Qnil; } -static VALUE arf_device_gc(VALUE self){ - return Qnil; +static void arf_device_gc(VALUE self){ + af_device_gc(); } -static VALUE arf_set_mem_step_size(VALUE self){ - return Qnil; +static VALUE arf_set_mem_step_size(VALUE self, VALUE step_bytes){ + af_set_mem_step_size(NUM2UINT(step_bytes)); + return Qtrue; } static VALUE arf_get_mem_step_size(VALUE self){ - return Qnil; + size_t step_bytes; + af_get_mem_step_size(&step_bytes); + return UINT2NUM(step_bytes); } -static VALUE arf_lock_device_ptr(VALUE self){ +static VALUE arf_lock_device_ptr(VALUE self, VALUE array_val){ + afstruct* input; + Data_Get_Struct(array_val, afstruct, input); + af_lock_device_ptr(input->carray); return Qnil; } -static VALUE arf_unlock_device_ptr(VALUE self){ +static VALUE arf_unlock_device_ptr(VALUE self, VALUE array_val){ + afstruct* input; + Data_Get_Struct(array_val, afstruct, input); + af_unlock_device_ptr(input->carray); return Qnil; } -static VALUE arf_lock_array(VALUE self){ +static VALUE arf_lock_array(VALUE self, VALUE array_val){ + afstruct* input; + Data_Get_Struct(array_val, afstruct, input); + af_lock_array(input->carray); return Qnil; } -static VALUE arf_unlock_array(VALUE self){ +static VALUE arf_unlock_array(VALUE self, VALUE array_val){ + afstruct* input; + Data_Get_Struct(array_val, afstruct, input); + af_unlock_array(input->carray); return Qnil; } -static VALUE arf_is_locked_array(VALUE self){ - return Qnil; +static VALUE arf_is_locked_array(VALUE self, VALUE array_val){ + bool res; + afstruct* input; + Data_Get_Struct(array_val, afstruct, input); + af_is_locked_array(&res, input->carray); + return res ? Qtrue : Qfalse; } static VALUE arf_get_device_ptr(VALUE self){ From 00f68e5081b722f45424762f50525cdc948a498b Mon Sep 17 00:00:00 2001 From: prasun Date: Tue, 8 Aug 2017 12:29:10 +0530 Subject: [PATCH 099/125] ArrayFire::Backend bindings --- ext/mri/arrayfire.c | 5 +++-- ext/mri/cmodules/backend.c | 24 ++++++++++++++++++++---- ext/mri/cmodules/defines.c | 9 +++++++++ 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 898454d..e80d539 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -20,6 +20,7 @@ VALUE Util = Qnil; // prototypes void Init_arrayfire(); +const char* get_backend_name(af_backend backend); af_backend arf_backend_type_from_rbsymbol(VALUE sym); af_dtype arf_dtype_from_rbsymbol(VALUE sym); af_mat_prop arf_mat_type_from_rbsymbol(VALUE sym); @@ -105,7 +106,7 @@ static VALUE arf_get_available_backends(VALUE self); static VALUE arf_get_backend_id(VALUE self, VALUE array_val); static VALUE arf_get_active_backend(VALUE self); static VALUE arf_get_backend_device_id(VALUE self, VALUE array_val); -static VALUE arf_set_backend(VALUE self); +static VALUE arf_set_backend(VALUE self, VALUE backend_val); static VALUE arf_matmul(VALUE self, VALUE left_val, VALUE right_val, VALUE left_prop_val, VALUE right_prop_val); static VALUE arf_dot(VALUE self, VALUE left_val, VALUE right_val, VALUE left_prop_val, VALUE right_prop_val); @@ -444,7 +445,7 @@ void Init_arrayfire() { rb_define_singleton_method(Backend, "get_backend_id", (METHOD)arf_get_backend_id, 1); rb_define_singleton_method(Backend, "get_active_backend", (METHOD)arf_get_active_backend, 0); rb_define_singleton_method(Backend, "get_device_id", (METHOD)arf_get_backend_device_id, 1); - rb_define_singleton_method(Backend, "set_backend", (METHOD)arf_set_backend, 0); + rb_define_singleton_method(Backend, "set_backend", (METHOD)arf_set_backend, 1); Device = rb_define_class_under(ArrayFire, "Device", rb_cObject); rb_define_singleton_method(Device, "info", (METHOD)arf_info, 0); diff --git a/ext/mri/cmodules/backend.c b/ext/mri/cmodules/backend.c index 09b1479..c1a0425 100644 --- a/ext/mri/cmodules/backend.c +++ b/ext/mri/cmodules/backend.c @@ -4,18 +4,32 @@ static VALUE arf_get_backend_count(VALUE self){ return UINT2NUM(num_backends); } +// int backends = af::getAvailableBackends(); +// bool cpu = backends & AF_BACKEND_CPU; +// bool cuda = backends & AF_BACKEND_CUDA; +// bool opencl = backends & AF_BACKEND_OPENCL; + static VALUE arf_get_available_backends(VALUE self){ int backends; - af_get_available_backends (&backends); + af_get_available_backends(&backends); return INT2NUM(backends); } static VALUE arf_get_backend_id(VALUE self, VALUE array_val){ - return Qnil; + afstruct* input; + Data_Get_Struct(array_val, afstruct, input); + af_backend backend; + af_get_backend_id (&backend, input->carray); + + const char* backend_name = get_backend_name(backend); + return rb_str_new_cstr(backend_name); } static VALUE arf_get_active_backend(VALUE self){ - return Qnil; + af_backend backend; + af_get_active_backend(&backend); + const char* backend_name = get_backend_name(backend); + return rb_str_new_cstr(backend_name); } static VALUE arf_get_backend_device_id(VALUE self, VALUE array_val){ @@ -29,6 +43,8 @@ static VALUE arf_get_backend_device_id(VALUE self, VALUE array_val){ return INT2NUM(device_id); } -static VALUE arf_set_backend(VALUE self){ +static VALUE arf_set_backend(VALUE self, VALUE backend_val){ + af_backend backend = arf_backend_type_from_rbsymbol(backend_val); + af_set_backend(backend); return Qnil; } diff --git a/ext/mri/cmodules/defines.c b/ext/mri/cmodules/defines.c index 7687195..fe5be57 100644 --- a/ext/mri/cmodules/defines.c +++ b/ext/mri/cmodules/defines.c @@ -191,3 +191,12 @@ af_random_engine_type arf_randome_engine_type_from_rbsymbol(VALUE sym) { VALUE str = rb_any_to_s(sym); rb_raise(rb_eArgError, "invalid backend type symbol (:%s) specified", RSTRING_PTR(str)); } + +const char* get_backend_name(af_backend backend){ + for(std::map::value_type& entry : BACKEND_TYPES) { + if (backend == entry.second) { + return entry.first; + } + } + rb_raise(rb_eArgError, "Something went wrong!"); +} From db253e9fa9e619c99e510d5f9d6f6c61b2e202b3 Mon Sep 17 00:00:00 2001 From: prasun Date: Tue, 8 Aug 2017 12:58:46 +0530 Subject: [PATCH 100/125] added CUDA specific bindings to handle CUDA code --- ext/mri/arrayfire.c | 13 +++++++------ ext/mri/cmodules/cuda.c | 13 +++++++++---- ext/mri/ruby_arrayfire.cpp | 1 + 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index e80d539..f9bc18a 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -113,9 +113,10 @@ static VALUE arf_dot(VALUE self, VALUE left_val, VALUE right_val, VALUE left_pro static VALUE arf_transpose(VALUE self, VALUE input); static VALUE arf_transpose_inplace(VALUE self, VALUE input); -static VALUE arf_get_stream(VALUE self); -static VALUE arf_get_native_id(VALUE self); -static VALUE arf_set_native_id(VALUE self); +// CUDA +static VALUE arf_get_stream(VALUE self, VALUE id); +static VALUE arf_get_native_id(VALUE self, VALUE cuda_device_id); +static VALUE arf_set_native_id(VALUE self, VALUE native_id); // device static VALUE arf_info(VALUE self); @@ -483,9 +484,9 @@ void Init_arrayfire() { rb_define_singleton_method(Blas, "transpose_inplace", (METHOD)arf_transpose_inplace, 1); Cuda = rb_define_class_under(ArrayFire, "CUDA", rb_cObject); - rb_define_singleton_method(Cuda, "get_stream", (METHOD)arf_get_stream, 0); - rb_define_singleton_method(Cuda, "get_native_id", (METHOD)arf_get_native_id, 0); - rb_define_singleton_method(Cuda, "set_native_id", (METHOD)arf_set_native_id, 0); + rb_define_singleton_method(Cuda, "get_stream", (METHOD)arf_get_stream, 1); + rb_define_singleton_method(Cuda, "get_native_id", (METHOD)arf_get_native_id, 1); + rb_define_singleton_method(Cuda, "set_native_id", (METHOD)arf_set_native_id, 1); Index = rb_define_class_under(ArrayFire, "Index", rb_cObject); rb_define_singleton_method(Index, "index", (METHOD)arf_index, 0); diff --git a/ext/mri/cmodules/cuda.c b/ext/mri/cmodules/cuda.c index 9a3265a..5a061bc 100644 --- a/ext/mri/cmodules/cuda.c +++ b/ext/mri/cmodules/cuda.c @@ -1,11 +1,16 @@ -static VALUE arf_get_stream(VALUE self){ +static VALUE arf_get_stream(VALUE self, VALUE id){ + // cudaStream_t* stream; + // afcu_get_stream (stream, NUM2INT(id)); return Qnil; } -static VALUE arf_get_native_id(VALUE self){ - return Qnil; +static VALUE arf_get_native_id(VALUE self, VALUE cuda_device_id){ + int native_id; + afcu_get_native_id(&native_id, NUM2INT(cuda_device_id)); + return INT2NUM(native_id); } -static VALUE arf_set_native_id(VALUE self){ +static VALUE arf_set_native_id(VALUE self, VALUE native_id){ + afcu_set_native_id(native_id); return Qnil; } diff --git a/ext/mri/ruby_arrayfire.cpp b/ext/mri/ruby_arrayfire.cpp index e3f6326..8ee1f1b 100644 --- a/ext/mri/ruby_arrayfire.cpp +++ b/ext/mri/ruby_arrayfire.cpp @@ -2,6 +2,7 @@ #include // std::min #include #include +#include #include #include #include From 296e61e93c1117a806873016c2146b7964384e03 Mon Sep 17 00:00:00 2001 From: prasun Date: Tue, 8 Aug 2017 14:24:50 +0530 Subject: [PATCH 101/125] added opencl bindings --- ext/mri/arrayfire.c | 3 +++ ext/mri/cmodules/opencl.c | 49 ++++++++++++++++++++++++++++++++++++-- ext/mri/ruby_arrayfire.cpp | 1 + 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index f9bc18a..db60546 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -21,6 +21,9 @@ VALUE Util = Qnil; void Init_arrayfire(); const char* get_backend_name(af_backend backend); +const char* get_cl_device_name(afcl_device_type device); +const char* get_cl_platform_name(afcl_platform platform); + af_backend arf_backend_type_from_rbsymbol(VALUE sym); af_dtype arf_dtype_from_rbsymbol(VALUE sym); af_mat_prop arf_mat_type_from_rbsymbol(VALUE sym); diff --git a/ext/mri/cmodules/opencl.c b/ext/mri/cmodules/opencl.c index fc73c02..66b78b9 100644 --- a/ext/mri/cmodules/opencl.c +++ b/ext/mri/cmodules/opencl.c @@ -1,3 +1,24 @@ +std::map AFCL_DEVICE_TYPES = { + {"AFCL_DEVICE_TYPE_CPU", 0}, + {"CL_DEVICE_TYPE_CPU", 1}, + {"AFCL_DEVICE_TYPE_GPU", 2}, + {"CL_DEVICE_TYPE_GPU", 3}, + {"AFCL_DEVICE_TYPE_ACC", 4}, + {"CL_DEVICE_TYPE_ACCELERATOR", 5}, + {"AFCL_DEVICE_TYPE_UNKNOWN", -1} +}; + +std::map AFCL_PLATFORM_TYPES = { + {"AFCL_PLATFORM_AMD", 0}, + {"AFCL_PLATFORM_APPLE", 1}, + {"AFCL_PLATFORM_INTEL", 2}, + {"AFCL_PLATFORM_NVIDIA", 3}, + {"AFCL_PLATFORM_BEIGNET", 4}, + {"AFCL_PLATFORM_POCL", 5}, + {"AFCL_PLATFORM_UNKNOWN", -1} +}; + + static VALUE arf_get_context(VALUE self){ return Qnil; } @@ -7,6 +28,8 @@ static VALUE arf_get_queue(VALUE self){ } static VALUE arf_get_device_id(VALUE self){ + cl_device_id id; + afcl_get_device_id(&id); return Qnil; } @@ -27,9 +50,31 @@ static VALUE arf_delete_device_context(VALUE self){ } static VALUE arf_get_device_type(VALUE self){ - return Qnil; + afcl_device_type device; + afcl_get_device_type(&device); + return rb_str_new_cstr(get_cl_device_name(device)); } static VALUE arf_get_platform(VALUE self){ - return Qnil; + afcl_platform platform; + afcl_get_platform(&platform); + return rb_str_new_cstr(get_cl_platform_name(platform)); +} + +const char* get_cl_device_name(afcl_device_type device){ + for(std::map::value_type& entry : AFCL_DEVICE_TYPES) { + if (device == entry.second) { + return entry.first; + } + } + rb_raise(rb_eArgError, "Couldn't detect device!"); +} + +const char* get_cl_platform_name(afcl_platform platform){ + for(std::map::value_type& entry : AFCL_PLATFORM_TYPES) { + if (platform == entry.second) { + return entry.first; + } + } + rb_raise(rb_eArgError, "Couldn't detect platform!"); } diff --git a/ext/mri/ruby_arrayfire.cpp b/ext/mri/ruby_arrayfire.cpp index 8ee1f1b..af301fa 100644 --- a/ext/mri/ruby_arrayfire.cpp +++ b/ext/mri/ruby_arrayfire.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include From 7e2297e7f4a25c74eeeadae96e8744af74d9e27c Mon Sep 17 00:00:00 2001 From: prasun Date: Tue, 8 Aug 2017 16:48:15 +0530 Subject: [PATCH 102/125] add comments and headers for Sparse --- ext/mri/arrayfire.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index db60546..205e4d2 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -38,6 +38,7 @@ static void arf_free(afstruct* af); static VALUE arf_engine_alloc(VALUE klass); static void arf_engine_free(afrandomenginestruct* afrandomengine); +// Af_Array static VALUE arf_create_array(int argc, VALUE* argv); static VALUE arf_create_handle(int argc, VALUE* argv); static VALUE arf_copy_array(VALUE self); @@ -69,6 +70,7 @@ static VALUE arf_is_integer(VALUE self); static VALUE arf_is_bool(VALUE self); static VALUE arf_is_sparse(VALUE self); +// Algorithm static VALUE arf_sum(VALUE self, VALUE array_val, VALUE dim_val); static VALUE arf_sum_nan(VALUE self, VALUE array_val, VALUE dim_val, VALUE nan_val); static VALUE arf_product(VALUE self, VALUE array_val, VALUE dim_val); @@ -104,6 +106,7 @@ static VALUE arf_set_unique(VALUE self); static VALUE arf_set_union(VALUE self); static VALUE arf_set_intersect(VALUE self); +// Backend static VALUE arf_get_backend_count(VALUE self); static VALUE arf_get_available_backends(VALUE self); static VALUE arf_get_backend_id(VALUE self, VALUE array_val); @@ -111,6 +114,7 @@ static VALUE arf_get_active_backend(VALUE self); static VALUE arf_get_backend_device_id(VALUE self, VALUE array_val); static VALUE arf_set_backend(VALUE self, VALUE backend_val); +// BLAS static VALUE arf_matmul(VALUE self, VALUE left_val, VALUE right_val, VALUE left_prop_val, VALUE right_prop_val); static VALUE arf_dot(VALUE self, VALUE left_val, VALUE right_val, VALUE left_prop_val, VALUE right_prop_val); static VALUE arf_transpose(VALUE self, VALUE input); @@ -121,7 +125,7 @@ static VALUE arf_get_stream(VALUE self, VALUE id); static VALUE arf_get_native_id(VALUE self, VALUE cuda_device_id); static VALUE arf_set_native_id(VALUE self, VALUE native_id); -// device +// Device static VALUE arf_info(VALUE self); static VALUE arf_init2(VALUE self); static VALUE arf_info_string(VALUE self, VALUE bool_val); @@ -150,6 +154,7 @@ static VALUE arf_unlock_array(VALUE self, VALUE array_val); static VALUE arf_is_locked_array(VALUE self, VALUE array_val); static VALUE arf_get_device_ptr(VALUE self); +// OpenCL static VALUE arf_get_context(VALUE self); static VALUE arf_get_queue(VALUE self); static VALUE arf_get_device_id(VALUE self); @@ -160,6 +165,7 @@ static VALUE arf_delete_device_context(VALUE self); static VALUE arf_get_device_type(VALUE self); static VALUE arf_get_platform(VALUE self); +// Data static VALUE arf_constant(int argc, VALUE* argv); static VALUE arf_constant_complex(VALUE self); static VALUE arf_constant_long(int argc, VALUE* argv); @@ -185,6 +191,7 @@ static VALUE arf_select_scalar_l(VALUE self, VALUE array_cond_val, VALUE a_val, static void arf_replace(VALUE self, VALUE array_input_val, VALUE array_cond_val, VALUE array_b_val); static void arf_replace_scalar(VALUE self, VALUE array_input_val, VALUE array_cond_val, VALUE b_val); +// Index static VALUE arf_index(VALUE self); static VALUE arf_lookup(VALUE self); static VALUE arf_assign_seq(VALUE self); @@ -196,6 +203,7 @@ static VALUE arf_set_seq_indexer(VALUE self); static VALUE arf_set_seq_param_indexer(VALUE self); static VALUE arf_release_indexers(VALUE self); +// LAPACK static VALUE arf_svd_func(VALUE self, VALUE u_val, VALUE s_val, VALUE vt_val, VALUE val); static VALUE arf_svd_inplace_func(VALUE self, VALUE val); static VALUE arf_lu_func(VALUE self, VALUE lower_val, VALUE upper_val, VALUE pivot_val, VALUE val); @@ -212,6 +220,7 @@ static VALUE arf_det(VALUE self, VALUE val); static VALUE arf_norm(VALUE self, VALUE val); static VALUE arf_is_lapack_available(VALUE self); +// Random static VALUE arf_create_random_engine(VALUE self, VALUE seed_val); static VALUE arf_retain_random_engine(VALUE self, VALUE engine_val); static VALUE arf_random_engine_set_type(VALUE self); @@ -228,6 +237,20 @@ static VALUE arf_randn(VALUE self, VALUE ndims_val, VALUE dim_val); static VALUE arf_set_seed(VALUE self); static VALUE arf_get_seed(VALUE self); +// Sparse +static VALUE arf_create_sparse_array(VALUE self); +static VALUE arf_create_sparse_array_from_ptr(VALUE self); +static VALUE arf_create_sparse_array_from_dense(VALUE self); +static VALUE arf_sparse_convert_to(VALUE self); +static VALUE arf_sparse_to_dense(VALUE self); +static VALUE arf_sparse_get_info(VALUE self); +static VALUE arf_sparse_get_values(VALUE self); +static VALUE arf_sparse_get_row_idx(VALUE self); +static VALUE arf_sparse_get_col_idx(VALUE self); +static VALUE arf_sparse_get_nnz(VALUE self); +static VALUE arf_sparse_get_storage(VALUE self); + +// Statistics static VALUE arf_mean(VALUE self, VALUE array_val, VALUE dim_val); static VALUE arf_mean_weighted(VALUE self, VALUE array_val, VALUE weighted_array_val, VALUE dim_val); static VALUE arf_var(VALUE self, VALUE array_val, VALUE is_biased, VALUE dim_val); @@ -243,6 +266,7 @@ static VALUE arf_stdev_all(VALUE self, VALUE array_val); static VALUE arf_median_all(VALUE self, VALUE array_val); static VALUE arf_corrcoef(VALUE self, VALUE first_array_val, VALUE second_array_val); +// Util static VALUE arf_print_array(VALUE self); static VALUE arf_print_array_gen(VALUE self); static VALUE arf_save_array(VALUE self); From e080fdd433ce83faaecf18b7aace941b184216c4 Mon Sep 17 00:00:00 2001 From: prasun Date: Tue, 8 Aug 2017 17:08:27 +0530 Subject: [PATCH 103/125] ArrayFire::Sparse class init --- ext/mri/arrayfire.c | 17 +++++++++++++++- ext/mri/cmodules/sparse.c | 43 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 ext/mri/cmodules/sparse.c diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 205e4d2..27119c2 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -14,6 +14,7 @@ VALUE Index = Qnil; VALUE Lapack = Qnil; VALUE OpenCL = Qnil; VALUE Random = Qnil; +VALUE Sparse = Qnil; VALUE Statistics = Qnil; VALUE Util = Qnil; @@ -599,6 +600,19 @@ void Init_arrayfire() { rb_define_singleton_method(Random, "set_seed", (METHOD)arf_set_seed, 0); rb_define_singleton_method(Random, "get_seed", (METHOD)arf_get_seed, 0); + Sparse = rb_define_class_under(ArrayFire, "Sparse", rb_cObject); + rb_define_singleton_method(Sparse, "create_sparse_array", (METHOD)arf_create_sparse_array, 0); + rb_define_singleton_method(Sparse, "create_sparse_array_from_ptr", (METHOD)arf_create_sparse_array_from_ptr, 0); + rb_define_singleton_method(Sparse, "create_sparse_array_from_dense", (METHOD)arf_create_sparse_array_from_dense, 0); + rb_define_singleton_method(Sparse, "sparse_convert_to", (METHOD)arf_sparse_convert_to, 0); + rb_define_singleton_method(Sparse, "sparse_to_dense", (METHOD)arf_sparse_to_dense, 0); + rb_define_singleton_method(Sparse, "sparse_get_info", (METHOD)arf_sparse_get_info, 0); + rb_define_singleton_method(Sparse, "sparse_get_values", (METHOD)arf_sparse_get_values, 0); + rb_define_singleton_method(Sparse, "sparse_get_row_idx", (METHOD)arf_sparse_get_row_idx, 0); + rb_define_singleton_method(Sparse, "sparse_get_col_idx", (METHOD)arf_sparse_get_col_idx, 0); + rb_define_singleton_method(Sparse, "sparse_get_nnz", (METHOD)arf_sparse_get_nnz, 0); + rb_define_singleton_method(Sparse, "sparse_get_storage", (METHOD)arf_sparse_get_storage, 0); + Statistics = rb_define_class_under(ArrayFire, "Statistics", rb_cObject); rb_define_singleton_method(Statistics, "mean", (METHOD)arf_mean, 2); rb_define_singleton_method(Statistics, "mean_weighted", (METHOD)arf_mean_weighted, 3); @@ -633,7 +647,7 @@ void Init_arrayfire() { /* * call-seq: * new(dimesnsion) -> Af_Array - * new(dims, dimesnsion, elements, data_type) -> NMatrix + * new(dims, dimesnsion, elements, data_type) -> Af_Array * * Create a new Af_Array. * @@ -793,5 +807,6 @@ DEF_UNARY_RUBY_ACCESSOR(ceil, ceil) #include "cmodules/lapack.c" #include "cmodules/opencl.c" #include "cmodules/random.c" +#include "cmodules/sparse.c" #include "cmodules/statistics.c" #include "cmodules/util.c" diff --git a/ext/mri/cmodules/sparse.c b/ext/mri/cmodules/sparse.c new file mode 100644 index 0000000..b853fc2 --- /dev/null +++ b/ext/mri/cmodules/sparse.c @@ -0,0 +1,43 @@ +static VALUE arf_create_sparse_array(VALUE self){ + return Qnil; +} + +static VALUE arf_create_sparse_array_from_ptr(VALUE self){ + return Qnil; +} + +static VALUE arf_create_sparse_array_from_dense(VALUE self){ + return Qnil; +} + +static VALUE arf_sparse_convert_to(VALUE self){ + return Qnil; +} + +static VALUE arf_sparse_to_dense(VALUE self){ + return Qnil; +} + +static VALUE arf_sparse_get_info(VALUE self){ + return Qnil; +} + +static VALUE arf_sparse_get_values(VALUE self){ + return Qnil; +} + +static VALUE arf_sparse_get_row_idx(VALUE self){ + return Qnil; +} + +static VALUE arf_sparse_get_col_idx(VALUE self){ + return Qnil; +} + +static VALUE arf_sparse_get_nnz(VALUE self){ + return Qnil; +} + +static VALUE arf_sparse_get_storage(VALUE self){ + return Qnil; +} From f52d75304cec58a088823a4ab0a04a4c27f01ef7 Mon Sep 17 00:00:00 2001 From: prasun Date: Tue, 8 Aug 2017 17:14:18 +0530 Subject: [PATCH 104/125] move arith functions to arith.c --- ext/mri/arrayfire.c | 60 +------------------------------------- ext/mri/cmodules/arith.c | 63 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 58 insertions(+), 65 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 27119c2..032a677 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -280,28 +280,6 @@ static VALUE arf_get_version(VALUE self); static VALUE arf_get_revision(VALUE self); static VALUE arf_get_size_of(VALUE self); -#define DEF_ELEMENTWISE_RUBY_ACCESSOR(name, oper) \ -static VALUE arf_ew_##name(VALUE left_val, VALUE right_val) { \ - afstruct* left; \ - afstruct* right; \ - afstruct* result = ALLOC(afstruct); \ - Data_Get_Struct(left_val, afstruct, left); \ - Data_Get_Struct(right_val, afstruct, right); \ - af_##oper(&result->carray, left->carray, right->carray, true); \ - af_print_array(result->carray); \ - return Data_Wrap_Struct(CLASS_OF(left_val), NULL, arf_free, result); \ -} - -#define DEF_UNARY_RUBY_ACCESSOR(oper, name) \ -static VALUE arf_unary_##name(VALUE self) { \ - afstruct* obj; \ - afstruct* result = ALLOC(afstruct); \ - Data_Get_Struct(self, afstruct, obj); \ - af_##oper(&result->carray, obj->carray); \ - af_print_array(result->carray); \ - return Data_Wrap_Struct(CLASS_OF(self), NULL, arf_free, result); \ -} - #define DECL_ELEMENTWISE_RUBY_ACCESSOR(name) static VALUE arf_ew_##name(VALUE left_val, VALUE right_val); #define DECL_UNARY_RUBY_ACCESSOR(name) static VALUE arf_unary_##name(VALUE self); @@ -758,45 +736,9 @@ static VALUE arf_eqeq_approx(VALUE left_val, VALUE right_val) { return Qtrue; } -DEF_ELEMENTWISE_RUBY_ACCESSOR(add, add) -DEF_ELEMENTWISE_RUBY_ACCESSOR(subtract, sub) -DEF_ELEMENTWISE_RUBY_ACCESSOR(multiply, mul) -DEF_ELEMENTWISE_RUBY_ACCESSOR(divide, div) - -DEF_ELEMENTWISE_RUBY_ACCESSOR(eqeq, eq) -DEF_ELEMENTWISE_RUBY_ACCESSOR(neq, neq) -DEF_ELEMENTWISE_RUBY_ACCESSOR(leq, le) -DEF_ELEMENTWISE_RUBY_ACCESSOR(geq, ge) -DEF_ELEMENTWISE_RUBY_ACCESSOR(lt, lt) -DEF_ELEMENTWISE_RUBY_ACCESSOR(gt, gt) - -DEF_UNARY_RUBY_ACCESSOR(sin, sin) -DEF_UNARY_RUBY_ACCESSOR(cos, cos) -DEF_UNARY_RUBY_ACCESSOR(tan, tan) -DEF_UNARY_RUBY_ACCESSOR(asin, asin) -DEF_UNARY_RUBY_ACCESSOR(acos, acos) -DEF_UNARY_RUBY_ACCESSOR(atan, atan) -DEF_UNARY_RUBY_ACCESSOR(sinh, sinh) -DEF_UNARY_RUBY_ACCESSOR(cosh, cosh) -DEF_UNARY_RUBY_ACCESSOR(tanh, tanh) -DEF_UNARY_RUBY_ACCESSOR(asinh, asinh) -DEF_UNARY_RUBY_ACCESSOR(acosh, acosh) -DEF_UNARY_RUBY_ACCESSOR(atanh, atanh) -DEF_UNARY_RUBY_ACCESSOR(exp, exp) -DEF_UNARY_RUBY_ACCESSOR(log2, log2) -DEF_UNARY_RUBY_ACCESSOR(log1p, log1p) -DEF_UNARY_RUBY_ACCESSOR(log10, log10) -DEF_UNARY_RUBY_ACCESSOR(sqrt, sqrt) -DEF_UNARY_RUBY_ACCESSOR(erf, erf) -DEF_UNARY_RUBY_ACCESSOR(erfc, erfc) -DEF_UNARY_RUBY_ACCESSOR(cbrt, cbrt) -DEF_UNARY_RUBY_ACCESSOR(lgamma, lgamma) -DEF_UNARY_RUBY_ACCESSOR(tgamma, tgamma) -DEF_UNARY_RUBY_ACCESSOR(floor, floor) -DEF_UNARY_RUBY_ACCESSOR(ceil, ceil) - #include "cmodules/array.c" #include "cmodules/algorithm.c" +#include "cmodules/arith.c" #include "cmodules/backend.c" #include "cmodules/blas.c" #include "cmodules/cuda.c" diff --git a/ext/mri/cmodules/arith.c b/ext/mri/cmodules/arith.c index 2f44be4..f343d66 100644 --- a/ext/mri/cmodules/arith.c +++ b/ext/mri/cmodules/arith.c @@ -1,7 +1,58 @@ -static void add(afstruct *result, afstruct *left, afstruct *right) -{ - array l = array(left->dimension[0], left->dimension[1], left->array); - array r = array(right->dimension[0], right->dimension[1], right->array); - array res = operator+(l,r); - result->array = res.host(); +#define DEF_ELEMENTWISE_RUBY_ACCESSOR(name, oper) \ +static VALUE arf_ew_##name(VALUE left_val, VALUE right_val) { \ + afstruct* left; \ + afstruct* right; \ + afstruct* result = ALLOC(afstruct); \ + Data_Get_Struct(left_val, afstruct, left); \ + Data_Get_Struct(right_val, afstruct, right); \ + af_##oper(&result->carray, left->carray, right->carray, true); \ + af_print_array(result->carray); \ + return Data_Wrap_Struct(CLASS_OF(left_val), NULL, arf_free, result); \ } + +#define DEF_UNARY_RUBY_ACCESSOR(oper, name) \ +static VALUE arf_unary_##name(VALUE self) { \ + afstruct* obj; \ + afstruct* result = ALLOC(afstruct); \ + Data_Get_Struct(self, afstruct, obj); \ + af_##oper(&result->carray, obj->carray); \ + af_print_array(result->carray); \ + return Data_Wrap_Struct(CLASS_OF(self), NULL, arf_free, result); \ +} + +DEF_ELEMENTWISE_RUBY_ACCESSOR(add, add) +DEF_ELEMENTWISE_RUBY_ACCESSOR(subtract, sub) +DEF_ELEMENTWISE_RUBY_ACCESSOR(multiply, mul) +DEF_ELEMENTWISE_RUBY_ACCESSOR(divide, div) + +DEF_ELEMENTWISE_RUBY_ACCESSOR(eqeq, eq) +DEF_ELEMENTWISE_RUBY_ACCESSOR(neq, neq) +DEF_ELEMENTWISE_RUBY_ACCESSOR(leq, le) +DEF_ELEMENTWISE_RUBY_ACCESSOR(geq, ge) +DEF_ELEMENTWISE_RUBY_ACCESSOR(lt, lt) +DEF_ELEMENTWISE_RUBY_ACCESSOR(gt, gt) + +DEF_UNARY_RUBY_ACCESSOR(sin, sin) +DEF_UNARY_RUBY_ACCESSOR(cos, cos) +DEF_UNARY_RUBY_ACCESSOR(tan, tan) +DEF_UNARY_RUBY_ACCESSOR(asin, asin) +DEF_UNARY_RUBY_ACCESSOR(acos, acos) +DEF_UNARY_RUBY_ACCESSOR(atan, atan) +DEF_UNARY_RUBY_ACCESSOR(sinh, sinh) +DEF_UNARY_RUBY_ACCESSOR(cosh, cosh) +DEF_UNARY_RUBY_ACCESSOR(tanh, tanh) +DEF_UNARY_RUBY_ACCESSOR(asinh, asinh) +DEF_UNARY_RUBY_ACCESSOR(acosh, acosh) +DEF_UNARY_RUBY_ACCESSOR(atanh, atanh) +DEF_UNARY_RUBY_ACCESSOR(exp, exp) +DEF_UNARY_RUBY_ACCESSOR(log2, log2) +DEF_UNARY_RUBY_ACCESSOR(log1p, log1p) +DEF_UNARY_RUBY_ACCESSOR(log10, log10) +DEF_UNARY_RUBY_ACCESSOR(sqrt, sqrt) +DEF_UNARY_RUBY_ACCESSOR(erf, erf) +DEF_UNARY_RUBY_ACCESSOR(erfc, erfc) +DEF_UNARY_RUBY_ACCESSOR(cbrt, cbrt) +DEF_UNARY_RUBY_ACCESSOR(lgamma, lgamma) +DEF_UNARY_RUBY_ACCESSOR(tgamma, tgamma) +DEF_UNARY_RUBY_ACCESSOR(floor, floor) +DEF_UNARY_RUBY_ACCESSOR(ceil, ceil) From 0a42d5f5c9d155bd68b6a2a938f0bd3338ef16c2 Mon Sep 17 00:00:00 2001 From: prasun Date: Tue, 8 Aug 2017 18:53:54 +0530 Subject: [PATCH 105/125] sparse array bindings --- ext/mri/arrayfire.c | 24 ++++++------ ext/mri/cmodules/defines.c | 13 +++++++ ext/mri/cmodules/sparse.c | 75 ++++++++++++++++++++++++++++++++------ 3 files changed, 88 insertions(+), 24 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 032a677..f454a9c 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -239,17 +239,17 @@ static VALUE arf_set_seed(VALUE self); static VALUE arf_get_seed(VALUE self); // Sparse -static VALUE arf_create_sparse_array(VALUE self); +static VALUE arf_create_sparse_array(VALUE self, VALUE shape_array, VALUE values_array, VALUE rowIdx_val, VALUE colIdx_val, VALUE stype_val); static VALUE arf_create_sparse_array_from_ptr(VALUE self); -static VALUE arf_create_sparse_array_from_dense(VALUE self); +static VALUE arf_create_sparse_array_from_dense(VALUE self, VALUE dense_val, VALUE stype_val); static VALUE arf_sparse_convert_to(VALUE self); static VALUE arf_sparse_to_dense(VALUE self); static VALUE arf_sparse_get_info(VALUE self); -static VALUE arf_sparse_get_values(VALUE self); -static VALUE arf_sparse_get_row_idx(VALUE self); -static VALUE arf_sparse_get_col_idx(VALUE self); +static VALUE arf_sparse_get_values(VALUE self, VALUE input_val); +static VALUE arf_sparse_get_row_idx(VALUE self, VALUE input_val); +static VALUE arf_sparse_get_col_idx(VALUE self, VALUE input_val); static VALUE arf_sparse_get_nnz(VALUE self); -static VALUE arf_sparse_get_storage(VALUE self); +static VALUE arf_sparse_get_storage(VALUE self, VALUE input_val); // Statistics static VALUE arf_mean(VALUE self, VALUE array_val, VALUE dim_val); @@ -579,17 +579,17 @@ void Init_arrayfire() { rb_define_singleton_method(Random, "get_seed", (METHOD)arf_get_seed, 0); Sparse = rb_define_class_under(ArrayFire, "Sparse", rb_cObject); - rb_define_singleton_method(Sparse, "create_sparse_array", (METHOD)arf_create_sparse_array, 0); + rb_define_singleton_method(Sparse, "create_sparse_array", (METHOD)arf_create_sparse_array, 5); rb_define_singleton_method(Sparse, "create_sparse_array_from_ptr", (METHOD)arf_create_sparse_array_from_ptr, 0); - rb_define_singleton_method(Sparse, "create_sparse_array_from_dense", (METHOD)arf_create_sparse_array_from_dense, 0); + rb_define_singleton_method(Sparse, "create_sparse_array_from_dense", (METHOD)arf_create_sparse_array_from_dense, 2); rb_define_singleton_method(Sparse, "sparse_convert_to", (METHOD)arf_sparse_convert_to, 0); rb_define_singleton_method(Sparse, "sparse_to_dense", (METHOD)arf_sparse_to_dense, 0); rb_define_singleton_method(Sparse, "sparse_get_info", (METHOD)arf_sparse_get_info, 0); - rb_define_singleton_method(Sparse, "sparse_get_values", (METHOD)arf_sparse_get_values, 0); - rb_define_singleton_method(Sparse, "sparse_get_row_idx", (METHOD)arf_sparse_get_row_idx, 0); - rb_define_singleton_method(Sparse, "sparse_get_col_idx", (METHOD)arf_sparse_get_col_idx, 0); + rb_define_singleton_method(Sparse, "sparse_get_values", (METHOD)arf_sparse_get_values, 1); + rb_define_singleton_method(Sparse, "sparse_get_row_idx", (METHOD)arf_sparse_get_row_idx, 1); + rb_define_singleton_method(Sparse, "sparse_get_col_idx", (METHOD)arf_sparse_get_col_idx, 1); rb_define_singleton_method(Sparse, "sparse_get_nnz", (METHOD)arf_sparse_get_nnz, 0); - rb_define_singleton_method(Sparse, "sparse_get_storage", (METHOD)arf_sparse_get_storage, 0); + rb_define_singleton_method(Sparse, "sparse_get_storage", (METHOD)arf_sparse_get_storage, 1); Statistics = rb_define_class_under(ArrayFire, "Statistics", rb_cObject); rb_define_singleton_method(Statistics, "mean", (METHOD)arf_mean, 2); diff --git a/ext/mri/cmodules/defines.c b/ext/mri/cmodules/defines.c index fe5be57..607d319 100644 --- a/ext/mri/cmodules/defines.c +++ b/ext/mri/cmodules/defines.c @@ -192,6 +192,19 @@ af_random_engine_type arf_randome_engine_type_from_rbsymbol(VALUE sym) { rb_raise(rb_eArgError, "invalid backend type symbol (:%s) specified", RSTRING_PTR(str)); } +af_storage arf_storage_type_from_rbsymbol(VALUE sym){ + ID sym_id = SYM2ID(sym); + + for (size_t index = 0; index < ARF_NUM_STORAGE_TYPES; ++index) { + if (sym_id == rb_intern(STORAGE_TYPES[index])) { + return static_cast(index); + } + } + + VALUE str = rb_any_to_s(sym); + rb_raise(rb_eArgError, "invalid storage type symbol (:%s) specified", RSTRING_PTR(str)); +} + const char* get_backend_name(af_backend backend){ for(std::map::value_type& entry : BACKEND_TYPES) { if (backend == entry.second) { diff --git a/ext/mri/cmodules/sparse.c b/ext/mri/cmodules/sparse.c index b853fc2..8d158ac 100644 --- a/ext/mri/cmodules/sparse.c +++ b/ext/mri/cmodules/sparse.c @@ -1,13 +1,40 @@ -static VALUE arf_create_sparse_array(VALUE self){ - return Qnil; +static VALUE arf_create_sparse_array(VALUE self, VALUE shape_array, VALUE values_array, VALUE rowIdx_val, VALUE colIdx_val, VALUE stype_val){ + afstruct* output = ALLOC(afstruct); + afstruct* values; + afstruct* rowIdx; + afstruct* colIdx; + + Data_Get_Struct(values_array, afstruct, values); + Data_Get_Struct(rowIdx_val, afstruct, rowIdx); + Data_Get_Struct(colIdx_val, afstruct, colIdx); + + af_storage stype = arf_storage_type_from_rbsymbol(stype_val); + + dim_t nRows = (dim_t)FIX2LONG(RARRAY_AREF(shape_array, 0)); + dim_t nCols = (dim_t)FIX2LONG(RARRAY_AREF(shape_array, 1)); + + af_create_sparse_array(&output->carray, nRows, nCols, values->carray, rowIdx->carray, colIdx->carray, stype); + + af_print_array(output->carray); + + return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } static VALUE arf_create_sparse_array_from_ptr(VALUE self){ return Qnil; } -static VALUE arf_create_sparse_array_from_dense(VALUE self){ - return Qnil; +static VALUE arf_create_sparse_array_from_dense(VALUE self, VALUE dense_val, VALUE stype_val){ + afstruct* output = ALLOC(afstruct); + afstruct* dense; + + Data_Get_Struct(dense_val, afstruct, dense); + + af_storage stype = arf_storage_type_from_rbsymbol(stype_val); + + af_create_sparse_array_from_dense(&output->carray, dense->carray, stype); + + return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } static VALUE arf_sparse_convert_to(VALUE self){ @@ -22,22 +49,46 @@ static VALUE arf_sparse_get_info(VALUE self){ return Qnil; } -static VALUE arf_sparse_get_values(VALUE self){ - return Qnil; +static VALUE arf_sparse_get_values(VALUE self, VALUE input_val){ + afstruct* output = ALLOC(afstruct); + afstruct* input; + + Data_Get_Struct(input_val, afstruct, input); + af_sparse_get_values(&output->carray, input->carray); + + return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } -static VALUE arf_sparse_get_row_idx(VALUE self){ - return Qnil; +static VALUE arf_sparse_get_row_idx(VALUE self, VALUE input_val){ + afstruct* output = ALLOC(afstruct); + afstruct* input; + + Data_Get_Struct(input_val, afstruct, input); + af_sparse_get_row_idx(&output->carray, input->carray); + + return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } -static VALUE arf_sparse_get_col_idx(VALUE self){ - return Qnil; +static VALUE arf_sparse_get_col_idx(VALUE self, VALUE input_val){ + afstruct* output = ALLOC(afstruct); + afstruct* input; + + Data_Get_Struct(input_val, afstruct, input); + af_sparse_get_col_idx(&output->carray, input->carray); + + return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } static VALUE arf_sparse_get_nnz(VALUE self){ return Qnil; } -static VALUE arf_sparse_get_storage(VALUE self){ - return Qnil; +static VALUE arf_sparse_get_storage(VALUE self, VALUE input_val){ + afstruct* input; + Data_Get_Struct(input_val, afstruct, input); + + af_storage storage; + af_sparse_get_storage(&storage , input->carray); + + return rb_str_new_cstr(STORAGE_TYPES[storage]); } From 8a0bc75337db32a229681f809f938bd1949352ab Mon Sep 17 00:00:00 2001 From: prasun Date: Wed, 9 Aug 2017 21:03:20 +0530 Subject: [PATCH 106/125] init examples --- examples/benchmarks/blas.rb | 0 examples/benchmarks/cg.rb | 0 examples/benchmarks/fft.rb | 0 examples/benchmarks/pi.rb | 0 examples/computer_vision/fast.rb | 0 examples/computer_vision/harris.rb | 0 examples/computer_vision/matching.rb | 0 examples/computer_vision/susan.rb | 0 examples/financial/black_scholes_options.rb | 0 examples/financial/heston_model.rb | 0 examples/financial/monte_carlo_options.rb | 0 examples/getting_started/convolve.rb | 0 examples/getting_started/integer.rb | 0 examples/getting_started/rainfall.rb | 0 examples/getting_started/vectorize.rb | 0 examples/helloworld/helloworld.rb | 0 examples/image_processing/adaptive_thresholding.rb | 0 examples/image_processing/binary_thresholding.rb | 0 examples/image_processing/brain_segmentation.rb | 0 examples/image_processing/edge.rb | 0 examples/image_processing/filters.rb | 0 examples/image_processing/image_demo.rb | 0 examples/image_processing/image_editing.rb | 0 examples/image_processing/morphing.rb | 0 examples/image_processing/optical_flow.rb | 0 examples/image_processing/pyramids.rb | 0 examples/lin_algebra/cholesky.rb | 0 examples/lin_algebra/lu.rb | 0 examples/lin_algebra/qr.rb | 0 examples/lin_algebra/svd.rb | 0 examples/machine_learning/bagging.rb | 0 examples/machine_learning/deep_belief_net.rb | 0 examples/machine_learning/geneticalgorithm.rb | 0 examples/machine_learning/kmeans.rb | 0 examples/machine_learning/knn.rb | 0 examples/machine_learning/logistic_regression.rb | 0 examples/machine_learning/naive_bayes.rb | 0 examples/machine_learning/neural_network.rb | 0 examples/machine_learning/perceptron.rb | 0 examples/machine_learning/rbm.rb | 0 examples/machine_learning/softmax_regression.rb | 0 examples/pde/swe.rb | 0 examples/unified/basic.rb | 0 43 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 examples/benchmarks/blas.rb create mode 100644 examples/benchmarks/cg.rb create mode 100644 examples/benchmarks/fft.rb create mode 100644 examples/benchmarks/pi.rb create mode 100644 examples/computer_vision/fast.rb create mode 100644 examples/computer_vision/harris.rb create mode 100644 examples/computer_vision/matching.rb create mode 100644 examples/computer_vision/susan.rb create mode 100644 examples/financial/black_scholes_options.rb create mode 100644 examples/financial/heston_model.rb create mode 100644 examples/financial/monte_carlo_options.rb create mode 100644 examples/getting_started/convolve.rb create mode 100644 examples/getting_started/integer.rb create mode 100644 examples/getting_started/rainfall.rb create mode 100644 examples/getting_started/vectorize.rb create mode 100644 examples/helloworld/helloworld.rb create mode 100644 examples/image_processing/adaptive_thresholding.rb create mode 100644 examples/image_processing/binary_thresholding.rb create mode 100644 examples/image_processing/brain_segmentation.rb create mode 100644 examples/image_processing/edge.rb create mode 100644 examples/image_processing/filters.rb create mode 100644 examples/image_processing/image_demo.rb create mode 100644 examples/image_processing/image_editing.rb create mode 100644 examples/image_processing/morphing.rb create mode 100644 examples/image_processing/optical_flow.rb create mode 100644 examples/image_processing/pyramids.rb create mode 100644 examples/lin_algebra/cholesky.rb create mode 100644 examples/lin_algebra/lu.rb create mode 100644 examples/lin_algebra/qr.rb create mode 100644 examples/lin_algebra/svd.rb create mode 100644 examples/machine_learning/bagging.rb create mode 100644 examples/machine_learning/deep_belief_net.rb create mode 100644 examples/machine_learning/geneticalgorithm.rb create mode 100644 examples/machine_learning/kmeans.rb create mode 100644 examples/machine_learning/knn.rb create mode 100644 examples/machine_learning/logistic_regression.rb create mode 100644 examples/machine_learning/naive_bayes.rb create mode 100644 examples/machine_learning/neural_network.rb create mode 100644 examples/machine_learning/perceptron.rb create mode 100644 examples/machine_learning/rbm.rb create mode 100644 examples/machine_learning/softmax_regression.rb create mode 100644 examples/pde/swe.rb create mode 100644 examples/unified/basic.rb diff --git a/examples/benchmarks/blas.rb b/examples/benchmarks/blas.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/benchmarks/cg.rb b/examples/benchmarks/cg.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/benchmarks/fft.rb b/examples/benchmarks/fft.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/benchmarks/pi.rb b/examples/benchmarks/pi.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/computer_vision/fast.rb b/examples/computer_vision/fast.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/computer_vision/harris.rb b/examples/computer_vision/harris.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/computer_vision/matching.rb b/examples/computer_vision/matching.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/computer_vision/susan.rb b/examples/computer_vision/susan.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/financial/black_scholes_options.rb b/examples/financial/black_scholes_options.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/financial/heston_model.rb b/examples/financial/heston_model.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/financial/monte_carlo_options.rb b/examples/financial/monte_carlo_options.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/getting_started/convolve.rb b/examples/getting_started/convolve.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/getting_started/integer.rb b/examples/getting_started/integer.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/getting_started/rainfall.rb b/examples/getting_started/rainfall.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/getting_started/vectorize.rb b/examples/getting_started/vectorize.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/helloworld/helloworld.rb b/examples/helloworld/helloworld.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/image_processing/adaptive_thresholding.rb b/examples/image_processing/adaptive_thresholding.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/image_processing/binary_thresholding.rb b/examples/image_processing/binary_thresholding.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/image_processing/brain_segmentation.rb b/examples/image_processing/brain_segmentation.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/image_processing/edge.rb b/examples/image_processing/edge.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/image_processing/filters.rb b/examples/image_processing/filters.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/image_processing/image_demo.rb b/examples/image_processing/image_demo.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/image_processing/image_editing.rb b/examples/image_processing/image_editing.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/image_processing/morphing.rb b/examples/image_processing/morphing.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/image_processing/optical_flow.rb b/examples/image_processing/optical_flow.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/image_processing/pyramids.rb b/examples/image_processing/pyramids.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/lin_algebra/cholesky.rb b/examples/lin_algebra/cholesky.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/lin_algebra/lu.rb b/examples/lin_algebra/lu.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/lin_algebra/qr.rb b/examples/lin_algebra/qr.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/lin_algebra/svd.rb b/examples/lin_algebra/svd.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/machine_learning/bagging.rb b/examples/machine_learning/bagging.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/machine_learning/deep_belief_net.rb b/examples/machine_learning/deep_belief_net.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/machine_learning/geneticalgorithm.rb b/examples/machine_learning/geneticalgorithm.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/machine_learning/kmeans.rb b/examples/machine_learning/kmeans.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/machine_learning/knn.rb b/examples/machine_learning/knn.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/machine_learning/logistic_regression.rb b/examples/machine_learning/logistic_regression.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/machine_learning/naive_bayes.rb b/examples/machine_learning/naive_bayes.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/machine_learning/neural_network.rb b/examples/machine_learning/neural_network.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/machine_learning/perceptron.rb b/examples/machine_learning/perceptron.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/machine_learning/rbm.rb b/examples/machine_learning/rbm.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/machine_learning/softmax_regression.rb b/examples/machine_learning/softmax_regression.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/pde/swe.rb b/examples/pde/swe.rb new file mode 100644 index 0000000..e69de29 diff --git a/examples/unified/basic.rb b/examples/unified/basic.rb new file mode 100644 index 0000000..e69de29 From f9e1c6d8cbec6f414b26ec2c9da68a5932f97d6c Mon Sep 17 00:00:00 2001 From: prasun Date: Mon, 14 Aug 2017 15:59:52 +0530 Subject: [PATCH 107/125] added factorization tests for lapack --- test/lapack_test.rb | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/test/lapack_test.rb b/test/lapack_test.rb index 57a469f..314fb57 100644 --- a/test/lapack_test.rb +++ b/test/lapack_test.rb @@ -6,6 +6,51 @@ def setup @array = ArrayFire::Af_Array.new 2, [2,2],[ 12, 21,-61, 48 ] end + def test_svd + matrix = ArrayFire::Af_Array.new 2, [4,2], [1,3,5,7,2,4,6,8] + u = ArrayFire::Af_Array.new 2, [4,4], [-0.1524, -0.3499, -0.5473, -0.7447, -0.8226, -0.4213, -0.0201, 0.3811, -0.3945, 0.2427, 0.6979, -0.5462, -0.3799, 0.8006, -0.4614, 0.0407] + s = ArrayFire::Af_Array.new 2, [2,1], [14.269095420837402, 0.6268] + v = ArrayFire::Af_Array.new 2, [2,2], [-0.6414, 0.7671, -0.7671, -0.6414] + svd_fac = ArrayFire::LAPACK.svd(matrix); + assert(u.approx_equal svd_fac[0]) + assert(s.approx_equal svd_fac[1]) + assert(v.approx_equal svd_fac[2]) + end + + def test_lu + matrix = ArrayFire::Af_Array.new 2, [3,3], [1,4,7,2,5,8,3,6,0] + l = ArrayFire::Af_Array.new 2, [3,3], [1.0, 0.1428, 0.5714, 0.0, 1.0, 0.4999, 0.0, 0.0, 1.0] + u = ArrayFire::Af_Array.new 2, [3,3], [7.0, 0.0, 0.0, 8.0, 0.8571, 0.0, 0.0, 3.0, 4.5000] + p = ArrayFire::Af_Array.new 2, [3,1], [0, 0, 0] + lu_fac = ArrayFire::LAPACK.lu(matrix); + assert(l.approx_equal lu_fac[0]) + assert(u.approx_equal lu_fac[1]) + assert(p.approx_equal lu_fac[2]) + end + + def test_qr + matrix = ArrayFire::Af_Array.new 2, [3,3], [12.0, -51.0, 4.0, 6.0, 167.0, -68.0, -4.0, 24.0, -41.0] + q = ArrayFire::Af_Array.new 2, [3,3], [-0.2283, 0.9705, -0.0761, -0.6189, -0.0843, 0.7809, 0.7515, 0.2254, 0.6199] + r = ArrayFire::Af_Array.new 2, [3,3], [-52.5452, 0.0, 0.0, 165.8952, -70.9068, 0.0, 27.3288, -31.5664, -23.015] + p = ArrayFire::Af_Array.new 2, [3,1], [1.2283, 1.5734, 0.0] + qr_fac = ArrayFire::LAPACK.qr matrix + assert(q.approx_equal qr_fac[0]) + assert(r.approx_equal qr_fac[1]) + assert(p.approx_equal qr_fac[2]) + end + + def test_cholesky + mat_upper = ArrayFire::Af_Array.new 2, [3,3], [1,0,0, 1,2,0, 1,2,6] + res_upper = ArrayFire::Af_Array.new 2, [3,3], [1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 2.0] + upper = ArrayFire::LAPACK.cholesky(mat_upper)[0] + assert_equal upper, res_upper + + mat_lower = ArrayFire::Af_Array.new 2, [3,3], [1,1,1, 0,2,2, 0,0,6] + res_lower = ArrayFire::Af_Array.new 2, [3,3], [1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 2.0] + lower = ArrayFire::LAPACK.cholesky(mat_lower, false)[0] + assert_equal lower, res_lower + end + def test_inverse result = ArrayFire::Af_Array.new 2, [2,2], [0.0258, -0.0113, 0.0328, 0.0064] assert(result.approx_equal ArrayFire::LAPACK.inverse(@array)); From 0a352ecd13f966174ee66835ad5cc736c9980776 Mon Sep 17 00:00:00 2001 From: prasun Date: Mon, 14 Aug 2017 16:30:58 +0530 Subject: [PATCH 108/125] add solve test --- ext/mri/arrayfire.c | 8 ++++---- ext/mri/cmodules/lapack.c | 5 +++-- test/lapack_test.rb | 11 +++++++++++ 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index f454a9c..9c08df6 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -213,8 +213,8 @@ static VALUE arf_qr_func(VALUE self, VALUE q_val, VALUE r_val, VALUE tau_val, VA static VALUE arf_qr_inplace_func(VALUE self); static VALUE arf_cholesky_func(VALUE self, VALUE output_val, VALUE val, VALUE is_upper_val); static VALUE arf_cholesky_inplace_func(VALUE self); -static VALUE arf_solve_func(VALUE self, VALUE lhs_val, VALUE rhs_val); -static VALUE arf_solve_lu_func(VALUE self, VALUE lhs_val, VALUE rhs_val, VALUE piv_val); +static VALUE arf_solve(VALUE self, VALUE lhs_val, VALUE rhs_val); +static VALUE arf_solve_lu(VALUE self, VALUE lhs_val, VALUE rhs_val, VALUE piv_val); static VALUE arf_inverse(VALUE self, VALUE val); static VALUE arf_rank(VALUE self, VALUE val); static VALUE arf_det(VALUE self, VALUE val); @@ -552,8 +552,8 @@ void Init_arrayfire() { rb_define_singleton_method(Lapack, "qr_inplace_func", (METHOD)arf_qr_inplace_func, 1); rb_define_singleton_method(Lapack, "cholesky_func", (METHOD)arf_cholesky_func, 3); rb_define_singleton_method(Lapack, "cholesky_inplace_func", (METHOD)arf_cholesky_inplace_func, 1); - rb_define_singleton_method(Lapack, "solve_func", (METHOD)arf_solve_func, 0); - rb_define_singleton_method(Lapack, "solve_lu_func", (METHOD)arf_solve_lu_func, 0); + rb_define_singleton_method(Lapack, "solve", (METHOD)arf_solve, 2); + rb_define_singleton_method(Lapack, "solve_lu", (METHOD)arf_solve_lu, 3); rb_define_singleton_method(Lapack, "inverse", (METHOD)arf_inverse, 1); rb_define_singleton_method(Lapack, "rank", (METHOD)arf_rank, 1); rb_define_singleton_method(Lapack, "det", (METHOD)arf_det, 1); diff --git a/ext/mri/cmodules/lapack.c b/ext/mri/cmodules/lapack.c index 952a1bf..270ab78 100644 --- a/ext/mri/cmodules/lapack.c +++ b/ext/mri/cmodules/lapack.c @@ -82,7 +82,7 @@ static VALUE arf_cholesky_inplace_func(VALUE self){ return Qnil; } -static VALUE arf_solve_func(VALUE self, VALUE lhs_val, VALUE rhs_val){ +static VALUE arf_solve(VALUE self, VALUE lhs_val, VALUE rhs_val){ afstruct* lhs; afstruct* rhs; @@ -96,7 +96,7 @@ static VALUE arf_solve_func(VALUE self, VALUE lhs_val, VALUE rhs_val){ } -static VALUE arf_solve_lu_func(VALUE self, VALUE lhs_val, VALUE rhs_val, VALUE piv_val){ +static VALUE arf_solve_lu(VALUE self, VALUE lhs_val, VALUE rhs_val, VALUE piv_val){ afstruct* lhs; afstruct* rhs; afstruct* piv; @@ -104,6 +104,7 @@ static VALUE arf_solve_lu_func(VALUE self, VALUE lhs_val, VALUE rhs_val, VALUE p Data_Get_Struct(lhs_val, afstruct, lhs); Data_Get_Struct(rhs_val, afstruct, rhs); + Data_Get_Struct(piv_val, afstruct, piv); af_solve_lu(&result->carray, lhs->carray, piv->carray, rhs->carray, AF_MAT_NONE); return Data_Wrap_Struct(CLASS_OF(lhs_val), NULL, arf_free, result); diff --git a/test/lapack_test.rb b/test/lapack_test.rb index 314fb57..42c0e05 100644 --- a/test/lapack_test.rb +++ b/test/lapack_test.rb @@ -51,6 +51,17 @@ def test_cholesky assert_equal lower, res_lower end + def test_solve + lhs = ArrayFire::Af_Array.new 2, [2,2], [3,1,1,2] + rhs = ArrayFire::Af_Array.new 2, [2,1], [9,8] + solution = ArrayFire::Af_Array.new 2, [2,1], [2,3] + assert_equal solution, ArrayFire::LAPACK.solve(lhs, rhs) + end + + def test_solve_lu + # TODO + end + def test_inverse result = ArrayFire::Af_Array.new 2, [2,2], [0.0258, -0.0113, 0.0328, 0.0064] assert(result.approx_equal ArrayFire::LAPACK.inverse(@array)); From 7201e8f498774d07d6a9eaabe66ff62c71d0ba13 Mon Sep 17 00:00:00 2001 From: prasun Date: Wed, 16 Aug 2017 17:54:15 +0530 Subject: [PATCH 109/125] add Util.print_array --- ext/mri/arrayfire.c | 4 ++-- ext/mri/cmodules/util.c | 12 +++++++++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 9c08df6..38b8c6e 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -268,7 +268,7 @@ static VALUE arf_median_all(VALUE self, VALUE array_val); static VALUE arf_corrcoef(VALUE self, VALUE first_array_val, VALUE second_array_val); // Util -static VALUE arf_print_array(VALUE self); +static VALUE arf_print_array(VALUE self, VALUE input_val); static VALUE arf_print_array_gen(VALUE self); static VALUE arf_save_array(VALUE self); static VALUE arf_read_array_index(VALUE self); @@ -608,7 +608,7 @@ void Init_arrayfire() { rb_define_singleton_method(Statistics, "corrcoef", (METHOD)arf_corrcoef, 2); Util = rb_define_class_under(ArrayFire, "Util", rb_cObject); - rb_define_singleton_method(Util, "print_array", (METHOD)arf_print_array, 0); + rb_define_singleton_method(Util, "print_array", (METHOD)arf_print_array, 1); rb_define_singleton_method(Util, "print_array_gen", (METHOD)arf_print_array_gen, 0); rb_define_singleton_method(Util, "save_array", (METHOD)arf_save_array, 0); rb_define_singleton_method(Util, "read_array_index", (METHOD)arf_read_array_index, 0); diff --git a/ext/mri/cmodules/util.c b/ext/mri/cmodules/util.c index 4db5371..9ddb33e 100644 --- a/ext/mri/cmodules/util.c +++ b/ext/mri/cmodules/util.c @@ -1,6 +1,12 @@ -static VALUE arf_print_array(VALUE self){ - return Qnil; -} +static VALUE arf_print_array(VALUE self, VALUE input_val){ + afstruct* input; + + Data_Get_Struct(input_val, afstruct, input); + + af_print_array(input->carray); + return Qtrue; +} + static VALUE arf_print_array_gen(VALUE self){ return Qnil; } From 2f8d12f939332d855a43ceab6e4ee6ec6ee6292d Mon Sep 17 00:00:00 2001 From: prasun Date: Wed, 16 Aug 2017 22:15:47 +0530 Subject: [PATCH 110/125] added sync method --- ext/mri/arrayfire.c | 4 ++-- ext/mri/cmodules/device.c | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 38b8c6e..be4811c 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -135,7 +135,7 @@ static VALUE arf_get_device_count(VALUE self); static VALUE arf_get_dbl_support(VALUE self, VALUE device); static VALUE arf_set_device(VALUE self, VALUE device); static VALUE arf_get_device(VALUE self); -static VALUE arf_sync(VALUE self); +static VALUE arf_sync(VALUE self, VALUE device_val); static VALUE arf_alloc_device(VALUE self); static VALUE arf_free_device(VALUE self); static VALUE arf_alloc_pinned(VALUE self); @@ -463,7 +463,7 @@ void Init_arrayfire() { rb_define_singleton_method(Device, "get_dbl_support", (METHOD)arf_get_dbl_support, 1); rb_define_singleton_method(Device, "set_device", (METHOD)arf_set_device, 1); rb_define_singleton_method(Device, "get_device", (METHOD)arf_get_device, 0); - rb_define_singleton_method(Device, "sync", (METHOD)arf_sync, 0); + rb_define_singleton_method(Device, "sync", (METHOD)arf_sync, 1); rb_define_singleton_method(Device, "alloc_device", (METHOD)arf_alloc_device, 0); rb_define_singleton_method(Device, "free_device", (METHOD)arf_free_device, 0); rb_define_singleton_method(Device, "alloc_pinned", (METHOD)arf_alloc_pinned, 0); diff --git a/ext/mri/cmodules/device.c b/ext/mri/cmodules/device.c index 4d5f1fd..79b723f 100644 --- a/ext/mri/cmodules/device.c +++ b/ext/mri/cmodules/device.c @@ -43,7 +43,8 @@ static VALUE arf_get_device(VALUE self){ return INT2NUM(device); } -static VALUE arf_sync(VALUE self){ +static VALUE arf_sync(VALUE self, VALUE device_val){ + af_sync(NUM2INT(device_val)); return Qnil; } From 3db88645ccab062b880c1bcb1247c04004bc1a30 Mon Sep 17 00:00:00 2001 From: prasun Date: Wed, 16 Aug 2017 22:29:41 +0530 Subject: [PATCH 111/125] use doubles as default --- ext/mri/arrayfire.c | 12 ++++++------ ext/mri/cmodules/array.c | 10 +++++----- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index be4811c..ee693f5 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -636,7 +636,7 @@ VALUE arf_init(int argc, VALUE* argv, VALUE self) afstruct* afarray; Data_Get_Struct(self, afstruct, afarray); if(argc > 0){ - af_dtype dtype = (argc == 4) ? arf_dtype_from_rbsymbol(argv[3]) : f32; + af_dtype dtype = (argc == 4) ? arf_dtype_from_rbsymbol(argv[3]) : f64; dim_t ndims = (dim_t)FIX2LONG(argv[0]); dim_t* dimensions = (dim_t*)malloc(ndims * sizeof(dim_t)); @@ -645,9 +645,9 @@ VALUE arf_init(int argc, VALUE* argv, VALUE self) dimensions[index] = (dim_t)FIX2LONG(RARRAY_AREF(argv[1], index)); count *= dimensions[index]; } - float* host_array = (float*)malloc(count * sizeof(float)); + double* host_array = (double*)malloc(count * sizeof(double)); for (dim_t index = 0; index < count; index++) { - host_array[index] = (float)NUM2DBL(RARRAY_AREF(argv[2], index)); + host_array[index] = (double)NUM2DBL(RARRAY_AREF(argv[2], index)); } af_create_array(&afarray->carray, host_array, ndims, dimensions, dtype); @@ -720,14 +720,14 @@ static VALUE arf_eqeq_approx(VALUE left_val, VALUE right_val) { if(left_count != right_count){return Qfalse;} - float* left_arr = (float*)malloc(left_count * sizeof(float)); + double* left_arr = (double*)malloc(left_count * sizeof(double)); af_get_data_ptr(left_arr, left->carray); - float* right_arr = (float*)malloc(left_count * sizeof(float)); + double* right_arr = (double*)malloc(left_count * sizeof(double)); af_get_data_ptr(right_arr, right->carray); for (dim_t index = 0; index < left_count; index++){ - float diff = left_arr[index] - right_arr[index]; + double diff = left_arr[index] - right_arr[index]; if(diff < 0){diff *= -1;} if(diff > 1e-3){ return Qfalse; diff --git a/ext/mri/cmodules/array.c b/ext/mri/cmodules/array.c index e368fb1..1715ca7 100644 --- a/ext/mri/cmodules/array.c +++ b/ext/mri/cmodules/array.c @@ -7,9 +7,9 @@ static VALUE arf_create_array(int argc, VALUE* argv){ dimensions[index] = (dim_t)FIX2LONG(RARRAY_AREF(argv[1], index)); count *= dimensions[index]; } - float* host_array = (float*)malloc(count * sizeof(float)); + double* host_array = (double*)malloc(count * sizeof(double)); for (dim_t index = 0; index < count; index++) { - host_array[index] = (float)NUM2DBL(RARRAY_AREF(argv[2], index)); + host_array[index] = (double)NUM2DBL(RARRAY_AREF(argv[2], index)); } af_create_array(&afarray->carray, host_array, ndims, dimensions, f32); @@ -28,9 +28,9 @@ static VALUE arf_create_handle(int argc, VALUE* argv){ dimensions[index] = (dim_t)FIX2LONG(RARRAY_AREF(argv[1], index)); count *= dimensions[index]; } - float* host_array = (float*)malloc(count * sizeof(float)); + double* host_array = (double*)malloc(count * sizeof(double)); for (dim_t index = 0; index < count; index++) { - host_array[index] = (float)NUM2DBL(RARRAY_AREF(argv[2], index)); + host_array[index] = (double)NUM2DBL(RARRAY_AREF(argv[2], index)); } af_create_handle(&afarray->carray, ndims, dimensions, f32); @@ -62,7 +62,7 @@ static VALUE arf_get_data_ptr(VALUE self){ Data_Get_Struct(self, afstruct, input); af_get_elements(&count, input->carray); - float* data = (float*)malloc(count * sizeof(float)); + double* data = (double*)malloc(count * sizeof(double)); af_get_data_ptr(data, input->carray); VALUE* array = ALLOC_N(VALUE, count); From fa5c7f1b0817b1f57f0a6e582b219f37d793c762 Mon Sep 17 00:00:00 2001 From: prasun Date: Wed, 16 Aug 2017 22:30:59 +0530 Subject: [PATCH 112/125] added eval bindings --- ext/mri/arrayfire.c | 6 +++--- ext/mri/cmodules/array.c | 12 +++++++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index ee693f5..d455057 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -50,7 +50,7 @@ static VALUE arf_retain_array(VALUE self); static VALUE arf_get_data_ref_count(VALUE self); static VALUE arf_eval(VALUE self); static VALUE arf_eval_multiple(VALUE self); -static VALUE arf_set_manual_eval_flag(VALUE self); +static VALUE arf_set_manual_eval_flag(VALUE self, VALUE flag); static VALUE arf_get_manual_eval_flag(VALUE self); static VALUE arf_get_elements(VALUE self); static VALUE arf_get_type(VALUE self); @@ -345,8 +345,8 @@ void Init_arrayfire() { rb_define_method(Af_Array, "get_data_ref_count", (METHOD)arf_get_data_ref_count, 0); rb_define_method(Af_Array, "eval", (METHOD)arf_eval, 0); rb_define_method(Af_Array, "eval_multiple", (METHOD)arf_eval_multiple, 0); - rb_define_method(Af_Array, "set_manual_eval_flag", (METHOD)arf_set_manual_eval_flag, 0); - rb_define_method(Af_Array, "get_manual_eval_flag", (METHOD)arf_get_manual_eval_flag, 0); + rb_define_singleton_method(Af_Array, "set_manual_eval_flag", (METHOD)arf_set_manual_eval_flag, 1); + rb_define_singleton_method(Af_Array, "get_manual_eval_flag", (METHOD)arf_get_manual_eval_flag, 0); rb_define_method(Af_Array, "get_elements", (METHOD)arf_get_elements, 0); rb_define_method(Af_Array, "get_type", (METHOD)arf_get_type, 0); rb_define_method(Af_Array, "get_dims", (METHOD)arf_get_dims, 0); diff --git a/ext/mri/cmodules/array.c b/ext/mri/cmodules/array.c index 1715ca7..c93ba6e 100644 --- a/ext/mri/cmodules/array.c +++ b/ext/mri/cmodules/array.c @@ -96,19 +96,25 @@ static VALUE arf_get_data_ref_count(VALUE self){ } static VALUE arf_eval(VALUE self){ - return Qnil; + afstruct* input; + Data_Get_Struct(self, afstruct, input); + af_eval(input->carray); + return Qtrue; } static VALUE arf_eval_multiple(VALUE self){ return Qnil; } -static VALUE arf_set_manual_eval_flag(VALUE self){ +static VALUE arf_set_manual_eval_flag(VALUE self, VALUE flag){ + af_set_manual_eval_flag(RTEST(flag)); return Qnil; } static VALUE arf_get_manual_eval_flag(VALUE self){ - return Qnil; + bool flag; + af_get_manual_eval_flag(&flag); + return flag ? Qtrue : Qfalse; } static VALUE arf_get_elements(VALUE self){ From d311d00516ce6ccef3e2fd9b2420ce1a961ca226 Mon Sep 17 00:00:00 2001 From: prasun Date: Thu, 17 Aug 2017 17:32:55 +0530 Subject: [PATCH 113/125] Random#methods added --- ext/mri/arrayfire.c | 25 ++++++++-------- ext/mri/cmodules/defines.c | 11 ++++++- ext/mri/cmodules/random.c | 60 +++++++++++++++++++++++++++++--------- 3 files changed, 69 insertions(+), 27 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index d455057..e31e3a4 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -24,6 +24,7 @@ void Init_arrayfire(); const char* get_backend_name(af_backend backend); const char* get_cl_device_name(afcl_device_type device); const char* get_cl_platform_name(afcl_platform platform); +const char* get_random_engine_name(af_random_engine_type engine); af_backend arf_backend_type_from_rbsymbol(VALUE sym); af_dtype arf_dtype_from_rbsymbol(VALUE sym); @@ -224,18 +225,18 @@ static VALUE arf_is_lapack_available(VALUE self); // Random static VALUE arf_create_random_engine(VALUE self, VALUE seed_val); static VALUE arf_retain_random_engine(VALUE self, VALUE engine_val); -static VALUE arf_random_engine_set_type(VALUE self); -static VALUE arf_random_engine_get_type(VALUE self); +static VALUE arf_random_engine_set_type(VALUE self, VALUE engine_val, VALUE type_val); +static VALUE arf_random_engine_get_type(VALUE self, VALUE engine_val); static VALUE arf_random_uniform(VALUE self, VALUE ndims_val, VALUE dim_val, VALUE engine_val); static VALUE arf_random_normal(VALUE self, VALUE ndims_val, VALUE dim_val, VALUE engine_val); static VALUE arf_random_engine_set_seed(VALUE self, VALUE engine_val ,VALUE seed_val); static VALUE arf_get_default_random_engine(VALUE self); -static VALUE arf_set_default_random_engine_type(VALUE self); -static VALUE arf_random_engine_get_seed(VALUE self); -static VALUE arf_release_random_engine(VALUE self); +static VALUE arf_set_default_random_engine_type(VALUE self, VALUE type_val); +static VALUE arf_random_engine_get_seed(VALUE self, VALUE engine_val); +static VALUE arf_release_random_engine(VALUE self, VALUE engine_val); static VALUE arf_randu(VALUE self, VALUE ndims_val, VALUE dim_val); static VALUE arf_randn(VALUE self, VALUE ndims_val, VALUE dim_val); -static VALUE arf_set_seed(VALUE self); +static VALUE arf_set_seed(VALUE self, VALUE seed); static VALUE arf_get_seed(VALUE self); // Sparse @@ -564,18 +565,18 @@ void Init_arrayfire() { rb_define_alloc_func(Random, arf_engine_alloc); rb_define_singleton_method(Random, "create_random_engine", (METHOD)arf_create_random_engine, 1); rb_define_singleton_method(Random, "retain_random_engine", (METHOD)arf_retain_random_engine, 1); - rb_define_singleton_method(Random, "random_engine_set_type", (METHOD)arf_random_engine_set_type, 0); - rb_define_singleton_method(Random, "random_engine_get_type", (METHOD)arf_random_engine_get_type, 0); + rb_define_singleton_method(Random, "random_engine_set_type", (METHOD)arf_random_engine_set_type, 2); + rb_define_singleton_method(Random, "random_engine_get_type", (METHOD)arf_random_engine_get_type, 1); rb_define_singleton_method(Random, "random_uniform", (METHOD)arf_random_uniform, 3); rb_define_singleton_method(Random, "random_normal", (METHOD)arf_random_normal, 3); rb_define_singleton_method(Random, "random_engine_set_seed", (METHOD)arf_random_engine_set_seed, 2); rb_define_singleton_method(Random, "get_default_random_engine", (METHOD)arf_get_default_random_engine, 0); - rb_define_singleton_method(Random, "set_default_random_engine_type", (METHOD)arf_set_default_random_engine_type, 0); - rb_define_singleton_method(Random, "random_engine_get_seed", (METHOD)arf_random_engine_get_seed, 0); - rb_define_singleton_method(Random, "release_random_engine", (METHOD)arf_release_random_engine, 0); + rb_define_singleton_method(Random, "set_default_random_engine_type", (METHOD)arf_set_default_random_engine_type, 1); + rb_define_singleton_method(Random, "random_engine_get_seed", (METHOD)arf_random_engine_get_seed, 1); + rb_define_singleton_method(Random, "release_random_engine", (METHOD)arf_release_random_engine, 1); rb_define_singleton_method(Random, "randu", (METHOD)arf_randu, 2); rb_define_singleton_method(Random, "randn", (METHOD)arf_randn, 2); - rb_define_singleton_method(Random, "set_seed", (METHOD)arf_set_seed, 0); + rb_define_singleton_method(Random, "set_seed", (METHOD)arf_set_seed, 1); rb_define_singleton_method(Random, "get_seed", (METHOD)arf_get_seed, 0); Sparse = rb_define_class_under(ArrayFire, "Sparse", rb_cObject); diff --git a/ext/mri/cmodules/defines.c b/ext/mri/cmodules/defines.c index 607d319..ad1e2b6 100644 --- a/ext/mri/cmodules/defines.c +++ b/ext/mri/cmodules/defines.c @@ -189,7 +189,7 @@ af_random_engine_type arf_randome_engine_type_from_rbsymbol(VALUE sym) { } VALUE str = rb_any_to_s(sym); - rb_raise(rb_eArgError, "invalid backend type symbol (:%s) specified", RSTRING_PTR(str)); + rb_raise(rb_eArgError, "invalid engine type symbol (:%s) specified", RSTRING_PTR(str)); } af_storage arf_storage_type_from_rbsymbol(VALUE sym){ @@ -213,3 +213,12 @@ const char* get_backend_name(af_backend backend){ } rb_raise(rb_eArgError, "Something went wrong!"); } + +const char* get_random_engine_name(af_random_engine_type engine){ + for(std::map::value_type& entry : RANDOM_ENGINE_TYPES) { + if (engine == entry.second) { + return entry.first; + } + } + rb_raise(rb_eArgError, "Something went wrong!"); +} diff --git a/ext/mri/cmodules/random.c b/ext/mri/cmodules/random.c index 61abb01..b9c3259 100644 --- a/ext/mri/cmodules/random.c +++ b/ext/mri/cmodules/random.c @@ -15,12 +15,25 @@ static VALUE arf_retain_random_engine(VALUE self, VALUE engine_val){ return Data_Wrap_Struct(Random, NULL, arf_engine_free, output); } -static VALUE arf_random_engine_set_type(VALUE self){ - return Qnil; +static VALUE arf_random_engine_set_type(VALUE self, VALUE engine_val, VALUE type_val){ + af_random_engine_type rtype = arf_randome_engine_type_from_rbsymbol(type_val); + afrandomenginestruct* engine; + + Data_Get_Struct(engine_val, afrandomenginestruct, engine); + af_random_engine_set_type(&engine->cengine, rtype); + + return Qtrue; } -static VALUE arf_random_engine_get_type(VALUE self){ - return Qnil; +static VALUE arf_random_engine_get_type(VALUE self, VALUE engine_val){ + af_random_engine_type rtype; + afrandomenginestruct* engine; + + Data_Get_Struct(engine_val, afrandomenginestruct, engine); + af_random_engine_get_type(&rtype, engine->cengine); + + const char* rengine = get_random_engine_name(rtype); + return rb_str_new_cstr(rengine); } static VALUE arf_random_uniform(VALUE self, VALUE ndims_val, VALUE dim_val, VALUE engine_val){ @@ -69,19 +82,35 @@ static VALUE arf_random_engine_set_seed(VALUE self, VALUE engine_val ,VALUE seed } static VALUE arf_get_default_random_engine(VALUE self){ - return Qnil; + afrandomenginestruct* output = ALLOC(afrandomenginestruct); + af_get_default_random_engine(&output->cengine) ; + + return Data_Wrap_Struct(Random, NULL, arf_engine_free, output); } -static VALUE arf_set_default_random_engine_type(VALUE self){ - return Qnil; +static VALUE arf_set_default_random_engine_type(VALUE self, VALUE type_val){ + af_random_engine_type rtype = arf_randome_engine_type_from_rbsymbol(type_val); + af_set_default_random_engine_type(rtype); + return Qtrue; } -static VALUE arf_random_engine_get_seed(VALUE self){ - return Qnil; +static VALUE arf_random_engine_get_seed(VALUE self, VALUE engine_val){ + afrandomenginestruct* engine; + uintl seed; + + Data_Get_Struct(engine_val, afrandomenginestruct, engine); + + af_random_engine_get_seed(&seed, engine->cengine); + return ULL2NUM(seed); } -static VALUE arf_release_random_engine(VALUE self){ - return Qnil; +static VALUE arf_release_random_engine(VALUE self, VALUE engine_val){ + afrandomenginestruct* engine; + + Data_Get_Struct(engine_val, afrandomenginestruct, engine); + + af_release_random_engine(engine); + return Qtrue; } static VALUE arf_randu(VALUE self, VALUE ndims_val, VALUE dim_val){ @@ -112,10 +141,13 @@ static VALUE arf_randn(VALUE self, VALUE ndims_val, VALUE dim_val){ return Data_Wrap_Struct(Af_Array, NULL, arf_free, out_array); } -static VALUE arf_set_seed(VALUE self){ - return Qnil; +static VALUE arf_set_seed(VALUE self, VALUE seed){ + af_set_seed(NUM2ULL(seed)); + return Qtrue; } static VALUE arf_get_seed(VALUE self){ - return Qnil; + uintl seed; + af_get_seed(&seed); + return ULL2NUM(seed); } From b904f0b41816b2f1fe5c62f83f738b4cb2a85c45 Mon Sep 17 00:00:00 2001 From: prasun Date: Thu, 17 Aug 2017 18:57:42 +0530 Subject: [PATCH 114/125] added util methods --- ext/mri/arrayfire.c | 16 +++++++-------- ext/mri/cmodules/util.c | 44 +++++++++++++++++++++++++++++++++-------- 2 files changed, 44 insertions(+), 16 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index e31e3a4..0f29462 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -271,15 +271,15 @@ static VALUE arf_corrcoef(VALUE self, VALUE first_array_val, VALUE second_array_ // Util static VALUE arf_print_array(VALUE self, VALUE input_val); static VALUE arf_print_array_gen(VALUE self); -static VALUE arf_save_array(VALUE self); +static VALUE arf_save_array(VALUE self, VALUE key_val, VALUE array_val, VALUE fn_val, VALUE append); static VALUE arf_read_array_index(VALUE self); static VALUE arf_read_array_key(VALUE self); static VALUE arf_read_array_key_check(VALUE self); -static VALUE arf_array_to_string(VALUE self); +static VALUE arf_array_to_string(VALUE self, VALUE exp_val, VALUE array_val, VALUE precision, VALUE transpose); static VALUE arf_example_function(VALUE self); -static VALUE arf_get_version(VALUE self); +static VALUE arf_get_version(VALUE self, VALUE major, VALUE minor, VALUE patch); static VALUE arf_get_revision(VALUE self); -static VALUE arf_get_size_of(VALUE self); +static VALUE arf_get_size_of(VALUE self, VALUE dtype_val); #define DECL_ELEMENTWISE_RUBY_ACCESSOR(name) static VALUE arf_ew_##name(VALUE left_val, VALUE right_val); #define DECL_UNARY_RUBY_ACCESSOR(name) static VALUE arf_unary_##name(VALUE self); @@ -611,15 +611,15 @@ void Init_arrayfire() { Util = rb_define_class_under(ArrayFire, "Util", rb_cObject); rb_define_singleton_method(Util, "print_array", (METHOD)arf_print_array, 1); rb_define_singleton_method(Util, "print_array_gen", (METHOD)arf_print_array_gen, 0); - rb_define_singleton_method(Util, "save_array", (METHOD)arf_save_array, 0); + rb_define_singleton_method(Util, "save_array", (METHOD)arf_save_array, 4); rb_define_singleton_method(Util, "read_array_index", (METHOD)arf_read_array_index, 0); rb_define_singleton_method(Util, "read_array_key", (METHOD)arf_read_array_key, 0); rb_define_singleton_method(Util, "read_array_key_check", (METHOD)arf_read_array_key_check, 0); - rb_define_singleton_method(Util, "array_to_string", (METHOD)arf_array_to_string, 0); + rb_define_singleton_method(Util, "array_to_string", (METHOD)arf_array_to_string, 4); rb_define_singleton_method(Util, "example_function", (METHOD)arf_example_function, 0); - rb_define_singleton_method(Util, "get_version", (METHOD)arf_get_version, 0); + rb_define_singleton_method(Util, "get_version", (METHOD)arf_get_version, 3); rb_define_singleton_method(Util, "get_revision", (METHOD)arf_get_revision, 0); - rb_define_singleton_method(Util, "get_size_of", (METHOD)arf_get_size_of, 0); + rb_define_singleton_method(Util, "get_size_of", (METHOD)arf_get_size_of, 1); } diff --git a/ext/mri/cmodules/util.c b/ext/mri/cmodules/util.c index 9ddb33e..18a5482 100644 --- a/ext/mri/cmodules/util.c +++ b/ext/mri/cmodules/util.c @@ -10,30 +10,58 @@ static VALUE arf_print_array(VALUE self, VALUE input_val){ static VALUE arf_print_array_gen(VALUE self){ return Qnil; } -static VALUE arf_save_array(VALUE self){ - return Qnil; + +static VALUE arf_save_array(VALUE self, VALUE key_val, VALUE array_val, VALUE fn_val, VALUE append){ + afstruct* input; + + Data_Get_Struct(array_val, afstruct, input); + const char* key = StringValueCStr(key_val); + const char* filename = StringValueCStr(fn_val); + int index; + af_save_array (&index, key, input->carray, filename, RTEST(append)); + return INT2NUM(index); } + static VALUE arf_read_array_index(VALUE self){ return Qnil; } + static VALUE arf_read_array_key(VALUE self){ return Qnil; } + static VALUE arf_read_array_key_check(VALUE self){ return Qnil; } -static VALUE arf_array_to_string(VALUE self){ - return Qnil; + +static VALUE arf_array_to_string(VALUE self, VALUE exp_val, VALUE array_val, VALUE precision, VALUE transpose){ + char* output; + afstruct* input; + + Data_Get_Struct(array_val, afstruct, input); + const char* exp = StringValueCStr(exp_val); + af_array_to_string(&output, exp, input->carray, NUM2INT(precision), RTEST(transpose)); + + return rb_str_new_cstr(output); } + static VALUE arf_example_function(VALUE self){ return Qnil; } -static VALUE arf_get_version(VALUE self){ + +static VALUE arf_get_version(VALUE self, VALUE major_val, VALUE minor_val, VALUE patch_val){ + int major, minor, patch; + af_get_version(&major, &minor, &patch); return Qnil; } + static VALUE arf_get_revision(VALUE self){ - return Qnil; + const char* revision = af_get_revision(); + return rb_str_new_cstr(revision); } -static VALUE arf_get_size_of(VALUE self){ - return Qnil; + +static VALUE arf_get_size_of(VALUE self, VALUE dtype_val){ + size_t size; + af_get_size_of(&size, arf_dtype_from_rbsymbol(dtype_val)); + return ULL2NUM(size); } From e4156378c5b19c1f57144d1478a2d066832c0087 Mon Sep 17 00:00:00 2001 From: prasun Date: Fri, 18 Aug 2017 10:52:39 +0530 Subject: [PATCH 115/125] complete Sparse bindings --- ext/mri/arrayfire.c | 16 ++++++------ ext/mri/cmodules/sparse.c | 51 +++++++++++++++++++++++++++++++++------ lib/arrayfire/sparse.rb | 13 ++++++++++ 3 files changed, 64 insertions(+), 16 deletions(-) create mode 100644 lib/arrayfire/sparse.rb diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 0f29462..efe757d 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -243,13 +243,13 @@ static VALUE arf_get_seed(VALUE self); static VALUE arf_create_sparse_array(VALUE self, VALUE shape_array, VALUE values_array, VALUE rowIdx_val, VALUE colIdx_val, VALUE stype_val); static VALUE arf_create_sparse_array_from_ptr(VALUE self); static VALUE arf_create_sparse_array_from_dense(VALUE self, VALUE dense_val, VALUE stype_val); -static VALUE arf_sparse_convert_to(VALUE self); -static VALUE arf_sparse_to_dense(VALUE self); -static VALUE arf_sparse_get_info(VALUE self); +static VALUE arf_sparse_convert_to(VALUE self, VALUE input_val, VALUE dest_storage_val); +static VALUE arf_sparse_to_dense(VALUE self, VALUE sparse_array); +static VALUE arf_sparse_get_info_func(VALUE self, VALUE values_val, VALUE rowIdx_val, VALUE colIdx_val, VALUE input_val); static VALUE arf_sparse_get_values(VALUE self, VALUE input_val); static VALUE arf_sparse_get_row_idx(VALUE self, VALUE input_val); static VALUE arf_sparse_get_col_idx(VALUE self, VALUE input_val); -static VALUE arf_sparse_get_nnz(VALUE self); +static VALUE arf_sparse_get_nnz(VALUE self, VALUE input_val); static VALUE arf_sparse_get_storage(VALUE self, VALUE input_val); // Statistics @@ -583,13 +583,13 @@ void Init_arrayfire() { rb_define_singleton_method(Sparse, "create_sparse_array", (METHOD)arf_create_sparse_array, 5); rb_define_singleton_method(Sparse, "create_sparse_array_from_ptr", (METHOD)arf_create_sparse_array_from_ptr, 0); rb_define_singleton_method(Sparse, "create_sparse_array_from_dense", (METHOD)arf_create_sparse_array_from_dense, 2); - rb_define_singleton_method(Sparse, "sparse_convert_to", (METHOD)arf_sparse_convert_to, 0); - rb_define_singleton_method(Sparse, "sparse_to_dense", (METHOD)arf_sparse_to_dense, 0); - rb_define_singleton_method(Sparse, "sparse_get_info", (METHOD)arf_sparse_get_info, 0); + rb_define_singleton_method(Sparse, "sparse_convert_to", (METHOD)arf_sparse_convert_to, 2); + rb_define_singleton_method(Sparse, "sparse_to_dense", (METHOD)arf_sparse_to_dense, 1); + rb_define_singleton_method(Sparse, "sparse_get_info_func", (METHOD)arf_sparse_get_info_func, 4); rb_define_singleton_method(Sparse, "sparse_get_values", (METHOD)arf_sparse_get_values, 1); rb_define_singleton_method(Sparse, "sparse_get_row_idx", (METHOD)arf_sparse_get_row_idx, 1); rb_define_singleton_method(Sparse, "sparse_get_col_idx", (METHOD)arf_sparse_get_col_idx, 1); - rb_define_singleton_method(Sparse, "sparse_get_nnz", (METHOD)arf_sparse_get_nnz, 0); + rb_define_singleton_method(Sparse, "sparse_get_nnz", (METHOD)arf_sparse_get_nnz, 1); rb_define_singleton_method(Sparse, "sparse_get_storage", (METHOD)arf_sparse_get_storage, 1); Statistics = rb_define_class_under(ArrayFire, "Statistics", rb_cObject); diff --git a/ext/mri/cmodules/sparse.c b/ext/mri/cmodules/sparse.c index 8d158ac..55f6eea 100644 --- a/ext/mri/cmodules/sparse.c +++ b/ext/mri/cmodules/sparse.c @@ -37,16 +37,45 @@ static VALUE arf_create_sparse_array_from_dense(VALUE self, VALUE dense_val, VAL return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } -static VALUE arf_sparse_convert_to(VALUE self){ - return Qnil; +static VALUE arf_sparse_convert_to(VALUE self, VALUE input_val, VALUE dest_storage_val){ + afstruct* output = ALLOC(afstruct); + afstruct* input; + + Data_Get_Struct(input_val, afstruct, input); + + af_storage dest_storage = arf_storage_type_from_rbsymbol(dest_storage_val); + + af_sparse_convert_to(&output->carray, input->carray, dest_storage); + + return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } -static VALUE arf_sparse_to_dense(VALUE self){ - return Qnil; +static VALUE arf_sparse_to_dense(VALUE self, VALUE sparse_array){ + afstruct* dense = ALLOC(afstruct); + afstruct* sparse; + + Data_Get_Struct(sparse_array, afstruct, sparse); + + af_sparse_to_dense(&dense->carray, sparse->carray); + + return Data_Wrap_Struct(Af_Array, NULL, arf_free, dense); } -static VALUE arf_sparse_get_info(VALUE self){ - return Qnil; +static VALUE arf_sparse_get_info_func(VALUE self, VALUE values_val, VALUE rowIdx_val, VALUE colIdx_val, VALUE input_val){ + afstruct* input; + afstruct* values; + afstruct* rowIdx; + afstruct* colIdx; + af_storage stype; + + Data_Get_Struct(input_val, afstruct, input); + Data_Get_Struct(values_val, afstruct, values); + Data_Get_Struct(rowIdx_val, afstruct, rowIdx); + Data_Get_Struct(colIdx_val, afstruct, colIdx); + + af_sparse_get_info( &values->carray, &rowIdx->carray, &colIdx->carray, &stype, input->carray ); + + return rb_str_new_cstr(STORAGE_TYPES[stype]); } static VALUE arf_sparse_get_values(VALUE self, VALUE input_val){ @@ -79,8 +108,14 @@ static VALUE arf_sparse_get_col_idx(VALUE self, VALUE input_val){ return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } -static VALUE arf_sparse_get_nnz(VALUE self){ - return Qnil; +static VALUE arf_sparse_get_nnz(VALUE self, VALUE input_val){ + dim_t out; + afstruct* input; + + Data_Get_Struct(input_val, afstruct, input); + af_sparse_get_nnz( &out, input); + + return ULL2NUM(out); } static VALUE arf_sparse_get_storage(VALUE self, VALUE input_val){ diff --git a/lib/arrayfire/sparse.rb b/lib/arrayfire/sparse.rb new file mode 100644 index 0000000..439fe34 --- /dev/null +++ b/lib/arrayfire/sparse.rb @@ -0,0 +1,13 @@ +module ArrayFire + class Sparse + + def self.sparse_get_info(af_array) + values = ArrayFire::Af_Array.new + rowIdx = ArrayFire::Af_Array.new + colIdx = ArrayFire::Af_Array.new + stype = ArrayFire::Sparse.sparse_get_info_func(values, rowIdx, colIdx, af_array) + return values, rowIdx, colIdx, stype + end + + end +end From 1a370f5b13b8971a6163e2b738a2d1dfebdc1d2a Mon Sep 17 00:00:00 2001 From: prasun Date: Fri, 18 Aug 2017 12:45:14 +0530 Subject: [PATCH 116/125] specify type for random engine --- ext/mri/arrayfire.c | 4 ++-- ext/mri/cmodules/random.c | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index efe757d..0099e2c 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -223,7 +223,7 @@ static VALUE arf_norm(VALUE self, VALUE val); static VALUE arf_is_lapack_available(VALUE self); // Random -static VALUE arf_create_random_engine(VALUE self, VALUE seed_val); +static VALUE arf_create_random_engine(VALUE self, VALUE type_val, VALUE seed_val); static VALUE arf_retain_random_engine(VALUE self, VALUE engine_val); static VALUE arf_random_engine_set_type(VALUE self, VALUE engine_val, VALUE type_val); static VALUE arf_random_engine_get_type(VALUE self, VALUE engine_val); @@ -563,7 +563,7 @@ void Init_arrayfire() { Random = rb_define_class_under(ArrayFire, "Random", rb_cObject); rb_define_alloc_func(Random, arf_engine_alloc); - rb_define_singleton_method(Random, "create_random_engine", (METHOD)arf_create_random_engine, 1); + rb_define_singleton_method(Random, "create_random_engine", (METHOD)arf_create_random_engine, 2); rb_define_singleton_method(Random, "retain_random_engine", (METHOD)arf_retain_random_engine, 1); rb_define_singleton_method(Random, "random_engine_set_type", (METHOD)arf_random_engine_set_type, 2); rb_define_singleton_method(Random, "random_engine_get_type", (METHOD)arf_random_engine_get_type, 1); diff --git a/ext/mri/cmodules/random.c b/ext/mri/cmodules/random.c index b9c3259..779941f 100644 --- a/ext/mri/cmodules/random.c +++ b/ext/mri/cmodules/random.c @@ -1,5 +1,7 @@ -static VALUE arf_create_random_engine(VALUE self, VALUE seed_val){ +static VALUE arf_create_random_engine(VALUE self, VALUE type_val, VALUE seed_val){ afrandomenginestruct* output = ALLOC(afrandomenginestruct); + af_random_engine_type rtype = arf_randome_engine_type_from_rbsymbol(type_val); + af_create_random_engine(&output->cengine, AF_RANDOM_ENGINE_DEFAULT, NUM2ULL(seed_val) ) ; return Data_Wrap_Struct(Random, NULL, arf_engine_free, output); From e250a8441c5f44d50c0e048af4eb65bb22c06179 Mon Sep 17 00:00:00 2001 From: prasun Date: Mon, 21 Aug 2017 19:24:09 +0530 Subject: [PATCH 117/125] added NMatrix - ArrayFire interface --- arrayfire.gemspec | 2 ++ ext/mri/arrayfire.c | 17 ++++++++++- ext/mri/extconf.rb | 29 ++++++++++++++---- ext/mri/interfaces/nmatrix.c | 58 ++++++++++++++++++++++++++++++++++++ ext/mri/ruby_arrayfire.cpp | 2 ++ ext/mri/ruby_arrayfire.h | 5 ++-- 6 files changed, 105 insertions(+), 8 deletions(-) create mode 100644 ext/mri/interfaces/nmatrix.c diff --git a/arrayfire.gemspec b/arrayfire.gemspec index b2c2ece..98b5db6 100644 --- a/arrayfire.gemspec +++ b/arrayfire.gemspec @@ -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 \ No newline at end of file diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 0099e2c..657e34c 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -1,4 +1,3 @@ -#include "arrayfire.h" #include #include @@ -17,6 +16,9 @@ VALUE Random = Qnil; VALUE Sparse = Qnil; VALUE Statistics = Qnil; VALUE Util = Qnil; +extern "C++" { + VALUE cNMatrix; +} // prototypes void Init_arrayfire(); @@ -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"); @@ -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); @@ -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" diff --git a/ext/mri/extconf.rb b/ext/mri/extconf.rb index 6c980f3..672d5d1 100644 --- a/ext/mri/extconf.rb +++ b/ext/mri/extconf.rb @@ -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)'], @@ -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" } diff --git a/ext/mri/interfaces/nmatrix.c b/ext/mri/interfaces/nmatrix.c new file mode 100644 index 0000000..aee7afb --- /dev/null +++ b/ext/mri/interfaces/nmatrix.c @@ -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)); + + af_get_dims(&dims[0], &dims[1], &dims[2], &dims[3], input->carray); + + size_t* shape = (size_t*)malloc(ndims * sizeof(size_t));; + 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; +} diff --git a/ext/mri/ruby_arrayfire.cpp b/ext/mri/ruby_arrayfire.cpp index af301fa..4b16065 100644 --- a/ext/mri/ruby_arrayfire.cpp +++ b/ext/mri/ruby_arrayfire.cpp @@ -7,6 +7,8 @@ #include #include #include +#include "nmatrix.h" + /* * Project Includes diff --git a/ext/mri/ruby_arrayfire.h b/ext/mri/ruby_arrayfire.h index 47f1309..dc51ed5 100644 --- a/ext/mri/ruby_arrayfire.h +++ b/ext/mri/ruby_arrayfire.h @@ -2,8 +2,6 @@ #define RUBY_ARRAYFIRE_H #endif -#include - typedef struct AF_STRUCT { af_array carray; @@ -43,6 +41,9 @@ rb_array_const_ptr(VALUE a) #ifdef __cplusplus typedef VALUE (*METHOD)(...); //}; // end of namespace nm + +// Interfaces + #endif From faae5dc3d294fe877655db1bc4b0ec8b77be8996 Mon Sep 17 00:00:00 2001 From: prasun Date: Wed, 23 Aug 2017 13:21:47 +0530 Subject: [PATCH 118/125] device_info --- ext/mri/arrayfire.c | 6 +++--- ext/mri/cmodules/device.c | 16 +++++++++++++--- lib/arrayfire/arrayfire.rb | 4 +++- lib/arrayfire/lapack.rb | 1 - 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 657e34c..b35672c 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -133,7 +133,7 @@ static VALUE arf_set_native_id(VALUE self, VALUE native_id); static VALUE arf_info(VALUE self); static VALUE arf_init2(VALUE self); static VALUE arf_info_string(VALUE self, VALUE bool_val); -static VALUE arf_device_info(VALUE self); +static VALUE arf_device_info(VALUE self, VALUE name_val, VALUE platform_val, VALUE toolkit_val, VALUE compute_val); static VALUE arf_get_device_count(VALUE self); static VALUE arf_get_dbl_support(VALUE self, VALUE device); static VALUE arf_set_device(VALUE self, VALUE device); @@ -471,8 +471,8 @@ void Init_arrayfire() { Device = rb_define_class_under(ArrayFire, "Device", rb_cObject); rb_define_singleton_method(Device, "info", (METHOD)arf_info, 0); rb_define_singleton_method(Device, "init", (METHOD)arf_init2, 0); - rb_define_singleton_method(Device, "info_string", (METHOD)arf_info_string, 0); - rb_define_singleton_method(Device, "device_info", (METHOD)arf_device_info, 0); + rb_define_singleton_method(Device, "info_string", (METHOD)arf_info_string, 1); + rb_define_singleton_method(Device, "device_info_func", (METHOD)arf_device_info, 4); rb_define_singleton_method(Device, "get_device_count", (METHOD)arf_get_device_count, 0); rb_define_singleton_method(Device, "get_dbl_support", (METHOD)arf_get_dbl_support, 1); rb_define_singleton_method(Device, "set_device", (METHOD)arf_set_device, 1); diff --git a/ext/mri/cmodules/device.c b/ext/mri/cmodules/device.c index 79b723f..68c9c33 100644 --- a/ext/mri/cmodules/device.c +++ b/ext/mri/cmodules/device.c @@ -14,9 +14,19 @@ static VALUE arf_info_string(VALUE self, VALUE bool_val){ return rb_str_new_cstr(str); } -static VALUE arf_device_info(VALUE self){ - char d_name, d_platform, d_toolkit, d_compute; - af_device_info(&d_name, &d_platform, &d_toolkit, &d_compute); +static VALUE arf_device_info(VALUE self, VALUE name_val, VALUE platform_val, VALUE toolkit_val, VALUE compute_val){ + char* d_name = (char*)malloc(sizeof(char) * 64); + char* d_platform = (char*)malloc(sizeof(char) * 10); + char* d_toolkit = (char*)malloc(sizeof(char) * 64); + char* d_compute = (char*)malloc(sizeof(char) * 10); + + af_device_info(d_name, d_platform, d_toolkit, d_compute); + + rb_str_cat2(name_val, d_name); + rb_str_cat2(platform_val, d_platform); + rb_str_cat2(toolkit_val, d_toolkit); + rb_str_cat2(compute_val, d_compute); + return Qnil; } diff --git a/lib/arrayfire/arrayfire.rb b/lib/arrayfire/arrayfire.rb index a593644..b1fd09a 100644 --- a/lib/arrayfire/arrayfire.rb +++ b/lib/arrayfire/arrayfire.rb @@ -1,2 +1,4 @@ require_relative '../arrayfire.so' -require_relative 'lapack.rb' \ No newline at end of file +require_relative 'lapack.rb' +require_relative 'device.rb' +require_relative 'sparse.rb' \ No newline at end of file diff --git a/lib/arrayfire/lapack.rb b/lib/arrayfire/lapack.rb index b3777a2..f1ad88a 100644 --- a/lib/arrayfire/lapack.rb +++ b/lib/arrayfire/lapack.rb @@ -27,7 +27,6 @@ def self.qr(af_array) def self.cholesky(af_array, is_upper=true) out = ArrayFire::Af_Array.new - info = 0 info = ArrayFire::LAPACK.cholesky_func(out, af_array, is_upper) return out, info end From c3b143e0f809ab35eb0e0c75ce7e98123aa9a4b6 Mon Sep 17 00:00:00 2001 From: prasun Date: Wed, 23 Aug 2017 16:59:01 +0530 Subject: [PATCH 119/125] Util.get_version --- ext/mri/arrayfire.c | 4 ++-- ext/mri/cmodules/util.c | 9 +++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index b35672c..19f5617 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -279,7 +279,7 @@ static VALUE arf_read_array_key(VALUE self); static VALUE arf_read_array_key_check(VALUE self); static VALUE arf_array_to_string(VALUE self, VALUE exp_val, VALUE array_val, VALUE precision, VALUE transpose); static VALUE arf_example_function(VALUE self); -static VALUE arf_get_version(VALUE self, VALUE major, VALUE minor, VALUE patch); +static VALUE arf_get_version(VALUE self); static VALUE arf_get_revision(VALUE self); static VALUE arf_get_size_of(VALUE self, VALUE dtype_val); @@ -630,7 +630,7 @@ void Init_arrayfire() { rb_define_singleton_method(Util, "read_array_key_check", (METHOD)arf_read_array_key_check, 0); rb_define_singleton_method(Util, "array_to_string", (METHOD)arf_array_to_string, 4); rb_define_singleton_method(Util, "example_function", (METHOD)arf_example_function, 0); - rb_define_singleton_method(Util, "get_version", (METHOD)arf_get_version, 3); + rb_define_singleton_method(Util, "get_version", (METHOD)arf_get_version, 0); rb_define_singleton_method(Util, "get_revision", (METHOD)arf_get_revision, 0); rb_define_singleton_method(Util, "get_size_of", (METHOD)arf_get_size_of, 1); diff --git a/ext/mri/cmodules/util.c b/ext/mri/cmodules/util.c index 18a5482..6c83b7b 100644 --- a/ext/mri/cmodules/util.c +++ b/ext/mri/cmodules/util.c @@ -49,10 +49,15 @@ static VALUE arf_example_function(VALUE self){ return Qnil; } -static VALUE arf_get_version(VALUE self, VALUE major_val, VALUE minor_val, VALUE patch_val){ +static VALUE arf_get_version(VALUE self){ int major, minor, patch; af_get_version(&major, &minor, &patch); - return Qnil; + + VALUE hash = rb_hash_new(); + rb_hash_aset(hash, rb_str_new_cstr("major"), INT2NUM(major)); + rb_hash_aset(hash, rb_str_new_cstr("minor"), INT2NUM(minor)); + rb_hash_aset(hash, rb_str_new_cstr("patch"), INT2NUM(patch)); + return hash; } static VALUE arf_get_revision(VALUE self){ From ae121f5526e0b836a026998d73cae41f9217d02e Mon Sep 17 00:00:00 2001 From: prasun Date: Wed, 23 Aug 2017 17:37:20 +0530 Subject: [PATCH 120/125] added Af_Array#to_s method --- ext/mri/arrayfire.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 19f5617..71a446f 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -74,6 +74,8 @@ static VALUE arf_is_integer(VALUE self); static VALUE arf_is_bool(VALUE self); static VALUE arf_is_sparse(VALUE self); +static VALUE arf_to_string(VALUE self); + // Algorithm static VALUE arf_sum(VALUE self, VALUE array_val, VALUE dim_val); static VALUE arf_sum_nan(VALUE self, VALUE array_val, VALUE dim_val, VALUE nan_val); @@ -375,6 +377,8 @@ void Init_arrayfire() { rb_define_method(Af_Array, "is_bool", (METHOD)arf_is_bool, 0); rb_define_method(Af_Array, "is_sparse", (METHOD)arf_is_sparse, 0); + rb_define_method(Af_Array, "to_s", (METHOD)arf_to_string, 0); + rb_define_alias(Af_Array, "ndims", "get_numdims"); rb_define_alias(Af_Array, "dims", "get_dims"); rb_define_alias(Af_Array, "to_cpu", "get_data_ptr"); @@ -697,6 +701,18 @@ static void arf_engine_free(afrandomenginestruct* afrandomengine) free(afrandomengine); } + +static VALUE arf_to_string(VALUE self){ + char* output; + afstruct* input; + + Data_Get_Struct(self, afstruct, input); + const char* exp = ""; + af_array_to_string(&output, exp, input->carray, 3, false); + + return rb_str_new_cstr(output); +} + static VALUE arf_eqeq(VALUE left_val, VALUE right_val) { afstruct* left; afstruct* right; From 4bafb2d910649b0375432d9d6e992943848530e3 Mon Sep 17 00:00:00 2001 From: prasun Date: Sat, 26 Aug 2017 10:54:06 +0530 Subject: [PATCH 121/125] gem version and unified backend --- arrayfire.gemspec | 21 ++++++++++++++++----- ext/mri/extconf.rb | 2 +- lib/arrayfire/version.rb | 3 +++ 3 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 lib/arrayfire/version.rb diff --git a/arrayfire.gemspec b/arrayfire.gemspec index 98b5db6..21de1da 100644 --- a/arrayfire.gemspec +++ b/arrayfire.gemspec @@ -1,9 +1,20 @@ +# coding: utf-8 +$:.unshift File.expand_path("../lib", __FILE__) + +require 'arrayfire/version.rb' + +ArrayFire::DESCRIPTION = < 0' gem.add_development_dependency 'bundler', '~>1.6' - gem.add_development_dependency 'json' + gem.add_development_dependency 'json', '~> 0' gem.add_development_dependency 'pry', '~>0.10' gem.add_development_dependency 'rake', '~>10.3' 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' + gem.add_development_dependency 'nmatrix', '~> 0.2.1' end \ No newline at end of file diff --git a/ext/mri/extconf.rb b/ext/mri/extconf.rb index 672d5d1..ff26e5c 100644 --- a/ext/mri/extconf.rb +++ b/ext/mri/extconf.rb @@ -39,7 +39,7 @@ dir_config(extension_name, HEADER_DIRS, LIB_DIRS) -have_library('afcuda') +have_library('af') have_library('cusolver') have_library('cudart') have_library('cufft') diff --git a/lib/arrayfire/version.rb b/lib/arrayfire/version.rb new file mode 100644 index 0000000..3a6457a --- /dev/null +++ b/lib/arrayfire/version.rb @@ -0,0 +1,3 @@ +module ArrayFire + VERSION = '3.5.0' +end From 9f1f2aa1e00fc22cc3422be1f0bbfab9d575a331 Mon Sep 17 00:00:00 2001 From: prasun Date: Sat, 26 Aug 2017 11:46:29 +0530 Subject: [PATCH 122/125] device multiple returns --- lib/arrayfire/device.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 lib/arrayfire/device.rb diff --git a/lib/arrayfire/device.rb b/lib/arrayfire/device.rb new file mode 100644 index 0000000..d2fea0c --- /dev/null +++ b/lib/arrayfire/device.rb @@ -0,0 +1,12 @@ +module ArrayFire + class Device + def self.device_info + d_name = "" + d_platform = "" + d_toolkit = "" + d_compute = "" + ArrayFire::Device.device_info_func(d_name, d_platform, d_toolkit, d_compute); + return d_name, d_platform, d_toolkit, d_compute + end + end +end From 70fd885f1f1515d4a0de1393dc96c2f9388fad21 Mon Sep 17 00:00:00 2001 From: prasun Date: Thu, 31 Aug 2017 21:10:39 +0530 Subject: [PATCH 123/125] replace malloc and free with Ruby GC aware mallocs and xfree respectively --- ext/mri/arrayfire.c | 20 ++++++++++++-------- ext/mri/cmodules/array.c | 12 ++++++------ ext/mri/cmodules/data.c | 16 ++++++++-------- ext/mri/cmodules/device.c | 8 ++++---- ext/mri/cmodules/random.c | 8 ++++---- ext/mri/interfaces/nmatrix.c | 8 ++++---- lib/arrayfire/arrayfire.rb | 2 +- test/data_test.rb | 2 +- 8 files changed, 40 insertions(+), 36 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 71a446f..44012ac 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -657,13 +657,13 @@ VALUE arf_init(int argc, VALUE* argv, VALUE self) af_dtype dtype = (argc == 4) ? arf_dtype_from_rbsymbol(argv[3]) : f64; dim_t ndims = (dim_t)FIX2LONG(argv[0]); - dim_t* dimensions = (dim_t*)malloc(ndims * sizeof(dim_t)); + dim_t* dimensions = ALLOC_N(dim_t, ndims); dim_t count = 1; for (dim_t index = 0; index < ndims; index++) { dimensions[index] = (dim_t)FIX2LONG(RARRAY_AREF(argv[1], index)); count *= dimensions[index]; } - double* host_array = (double*)malloc(count * sizeof(double)); + double* host_array = ALLOC_N(double, count); for (dim_t index = 0; index < count; index++) { host_array[index] = (double)NUM2DBL(RARRAY_AREF(argv[2], index)); } @@ -693,12 +693,12 @@ static VALUE arf_engine_alloc(VALUE klass) static void arf_free(afstruct* af) { - free(af); + xfree(af); } static void arf_engine_free(afrandomenginestruct* afrandomengine) { - free(afrandomengine); + xfree(afrandomengine); } @@ -723,8 +723,12 @@ static VALUE arf_eqeq(VALUE left_val, VALUE right_val) { dim_t count; af_get_elements(&count, result->carray); - bool* data = (bool*)malloc(count * sizeof(bool)); - af_get_data_ptr(data, result->carray); + bool* data = ALLOC_N(bool, count); + af_err flag = af_get_data_ptr(data, result->carray); + + if(flag != AF_SUCCESS){ + rb_raise(rb_eArgError, "Something went wrong!"); + } for (dim_t index = 0; index < count; index++){ if(!data[index]){ @@ -750,10 +754,10 @@ static VALUE arf_eqeq_approx(VALUE left_val, VALUE right_val) { if(left_count != right_count){return Qfalse;} - double* left_arr = (double*)malloc(left_count * sizeof(double)); + double* left_arr = ALLOC_N(double, left_count); af_get_data_ptr(left_arr, left->carray); - double* right_arr = (double*)malloc(left_count * sizeof(double)); + double* right_arr = ALLOC_N(double, left_count); af_get_data_ptr(right_arr, right->carray); for (dim_t index = 0; index < left_count; index++){ diff --git a/ext/mri/cmodules/array.c b/ext/mri/cmodules/array.c index c93ba6e..6b2c970 100644 --- a/ext/mri/cmodules/array.c +++ b/ext/mri/cmodules/array.c @@ -1,13 +1,13 @@ static VALUE arf_create_array(int argc, VALUE* argv){ afstruct* afarray = ALLOC(afstruct); dim_t ndims = (dim_t)FIX2LONG(argv[0]); - dim_t* dimensions = (dim_t*)malloc(ndims * sizeof(dim_t)); + dim_t* dimensions = ALLOC_N(dim_t, ndims); dim_t count = 1; for (dim_t index = 0; index < ndims; index++) { dimensions[index] = (dim_t)FIX2LONG(RARRAY_AREF(argv[1], index)); count *= dimensions[index]; } - double* host_array = (double*)malloc(count * sizeof(double)); + double* host_array = ALLOC_N(double, count);; for (dim_t index = 0; index < count; index++) { host_array[index] = (double)NUM2DBL(RARRAY_AREF(argv[2], index)); } @@ -22,13 +22,13 @@ static VALUE arf_create_array(int argc, VALUE* argv){ static VALUE arf_create_handle(int argc, VALUE* argv){ afstruct* afarray = ALLOC(afstruct); dim_t ndims = (dim_t)FIX2LONG(argv[0]); - dim_t* dimensions = (dim_t*)malloc(ndims * sizeof(dim_t)); + dim_t* dimensions = ALLOC_N(dim_t, ndims); dim_t count = 1; for (dim_t index = 0; index < ndims; index++) { dimensions[index] = (dim_t)FIX2LONG(RARRAY_AREF(argv[1], index)); count *= dimensions[index]; } - double* host_array = (double*)malloc(count * sizeof(double)); + double* host_array = ALLOC_N(double, count); for (dim_t index = 0; index < count; index++) { host_array[index] = (double)NUM2DBL(RARRAY_AREF(argv[2], index)); } @@ -62,7 +62,7 @@ static VALUE arf_get_data_ptr(VALUE self){ Data_Get_Struct(self, afstruct, input); af_get_elements(&count, input->carray); - double* data = (double*)malloc(count * sizeof(double)); + double* data = ALLOC_N(double, count); af_get_data_ptr(data, input->carray); VALUE* array = ALLOC_N(VALUE, count); @@ -145,7 +145,7 @@ static VALUE arf_get_dims(VALUE self){ uint ndims; af_get_numdims(&ndims, input->carray); - dim_t* dims = (dim_t*)malloc(ndims * sizeof(dim_t)); + dim_t* dims = ALLOC_N(dim_t, ndims); af_get_dims(&dims[0], &dims[1], &dims[2], &dims[3], input->carray); diff --git a/ext/mri/cmodules/data.c b/ext/mri/cmodules/data.c index d27c0c9..ebcd2cd 100644 --- a/ext/mri/cmodules/data.c +++ b/ext/mri/cmodules/data.c @@ -2,13 +2,13 @@ static VALUE arf_constant(int argc, VALUE* argv){ afstruct* output = ALLOC(afstruct); dim_t ndims = (dim_t)FIX2LONG(argv[0]); - dim_t* dimensions = (dim_t*)malloc(ndims * sizeof(dim_t)); + dim_t* dimensions = ALLOC_N(dim_t, ndims); dim_t count = 1; for (dim_t index = 0; index < ndims; index++) { dimensions[index] = (dim_t)FIX2LONG(RARRAY_AREF(argv[1], index)); count *= dimensions[index]; } - float data = NUM2DBL(argv[2]); + double data = NUM2DBL(argv[2]); af_constant(&output->carray, data, 2, dimensions, f32); af_print_array(output->carray); @@ -23,13 +23,13 @@ static VALUE arf_constant_long(int argc, VALUE* argv){ afstruct* output = ALLOC(afstruct); dim_t ndims = (dim_t)FIX2LONG(argv[0]); - dim_t* dimensions = (dim_t*)malloc(ndims * sizeof(dim_t)); + dim_t* dimensions = ALLOC_N(dim_t, ndims); dim_t count = 1; for (dim_t index = 0; index < ndims; index++) { dimensions[index] = (dim_t)FIX2LONG(RARRAY_AREF(argv[1], index)); count *= dimensions[index]; } - float data = NUM2LONG(argv[2]); + long data = NUM2LONG(argv[2]); af_constant_long(&output->carray, data, 2, dimensions); af_print_array(output->carray); @@ -40,13 +40,13 @@ static VALUE arf_constant_ulong(int argc, VALUE* argv){ afstruct* output = ALLOC(afstruct); dim_t ndims = (dim_t)FIX2LONG(argv[0]); - dim_t* dimensions = (dim_t*)malloc(ndims * sizeof(dim_t)); + dim_t* dimensions = ALLOC_N(dim_t, ndims); dim_t count = 1; for (dim_t index = 0; index < ndims; index++) { dimensions[index] = (dim_t)FIX2LONG(RARRAY_AREF(argv[1], index)); count *= dimensions[index]; } - float data = NUM2LONG(argv[2]); + unsigned long data = NUM2LONG(argv[2]); af_constant_ulong(&output->carray, data, 2, dimensions); af_print_array(output->carray); @@ -57,7 +57,7 @@ static VALUE arf_range(int argc, VALUE* argv){ afstruct* output = ALLOC(afstruct); dim_t ndims = (dim_t)FIX2LONG(argv[0]); - dim_t* dimensions = (dim_t*)malloc(ndims * sizeof(dim_t)); + dim_t* dimensions = ALLOC_N(dim_t, ndims); dim_t count = 1; for (dim_t index = 0; index < ndims; index++) { dimensions[index] = (dim_t)FIX2LONG(RARRAY_AREF(argv[1], index)); @@ -78,7 +78,7 @@ static VALUE arf_identity(int argc, VALUE* argv){ afstruct* output = ALLOC(afstruct); dim_t ndims = (dim_t)FIX2LONG(argv[0]); - dim_t* dimensions = (dim_t*)malloc(ndims * sizeof(dim_t)); + dim_t* dimensions = ALLOC_N(dim_t, ndims); dim_t count = 1; for (dim_t index = 0; index < ndims; index++) { dimensions[index] = (dim_t)FIX2LONG(RARRAY_AREF(argv[1], index)); diff --git a/ext/mri/cmodules/device.c b/ext/mri/cmodules/device.c index 68c9c33..c247a88 100644 --- a/ext/mri/cmodules/device.c +++ b/ext/mri/cmodules/device.c @@ -15,10 +15,10 @@ static VALUE arf_info_string(VALUE self, VALUE bool_val){ } static VALUE arf_device_info(VALUE self, VALUE name_val, VALUE platform_val, VALUE toolkit_val, VALUE compute_val){ - char* d_name = (char*)malloc(sizeof(char) * 64); - char* d_platform = (char*)malloc(sizeof(char) * 10); - char* d_toolkit = (char*)malloc(sizeof(char) * 64); - char* d_compute = (char*)malloc(sizeof(char) * 10); + char* d_name = ALLOC_N(char, sizeof(char) * 64); + char* d_platform = ALLOC_N(char, sizeof(char) * 10); + char* d_toolkit = ALLOC_N(char, sizeof(char) * 64); + char* d_compute = ALLOC_N(char, sizeof(char) * 10); af_device_info(d_name, d_platform, d_toolkit, d_compute); diff --git a/ext/mri/cmodules/random.c b/ext/mri/cmodules/random.c index 779941f..ef96c26 100644 --- a/ext/mri/cmodules/random.c +++ b/ext/mri/cmodules/random.c @@ -45,7 +45,7 @@ static VALUE arf_random_uniform(VALUE self, VALUE ndims_val, VALUE dim_val, VALU Data_Get_Struct(engine_val, afrandomenginestruct, engine); dim_t ndims = (dim_t)FIX2LONG(ndims_val); - dim_t* dimensions = (dim_t*)malloc(ndims * sizeof(dim_t)); + dim_t* dimensions = ALLOC_N(dim_t, ndims); dim_t count = 1; for (dim_t index = 0; index < ndims; index++) { dimensions[index] = (dim_t)FIX2LONG(RARRAY_AREF(dim_val, index)); @@ -63,7 +63,7 @@ static VALUE arf_random_normal(VALUE self, VALUE ndims_val, VALUE dim_val, VALUE Data_Get_Struct(engine_val, afrandomenginestruct, engine); dim_t ndims = (dim_t)FIX2LONG(ndims_val); - dim_t* dimensions = (dim_t*)malloc(ndims * sizeof(dim_t)); + dim_t* dimensions = ALLOC_N(dim_t, ndims); dim_t count = 1; for (dim_t index = 0; index < ndims; index++) { dimensions[index] = (dim_t)FIX2LONG(RARRAY_AREF(dim_val, index)); @@ -119,7 +119,7 @@ static VALUE arf_randu(VALUE self, VALUE ndims_val, VALUE dim_val){ afstruct* out_array = ALLOC(afstruct); dim_t ndims = (dim_t)FIX2LONG(ndims_val); - dim_t* dimensions = (dim_t*)malloc(ndims * sizeof(dim_t)); + dim_t* dimensions = ALLOC_N(dim_t, ndims); dim_t count = 1; for (dim_t index = 0; index < ndims; index++) { dimensions[index] = (dim_t)FIX2LONG(RARRAY_AREF(dim_val, index)); @@ -133,7 +133,7 @@ static VALUE arf_randn(VALUE self, VALUE ndims_val, VALUE dim_val){ afstruct* out_array = ALLOC(afstruct); dim_t ndims = (dim_t)FIX2LONG(ndims_val); - dim_t* dimensions = (dim_t*)malloc(ndims * sizeof(dim_t)); + dim_t* dimensions = ALLOC_N(dim_t, ndims); dim_t count = 1; for (dim_t index = 0; index < ndims; index++) { dimensions[index] = (dim_t)FIX2LONG(RARRAY_AREF(dim_val, index)); diff --git a/ext/mri/interfaces/nmatrix.c b/ext/mri/interfaces/nmatrix.c index aee7afb..40df632 100644 --- a/ext/mri/interfaces/nmatrix.c +++ b/ext/mri/interfaces/nmatrix.c @@ -5,18 +5,18 @@ static VALUE arf_af_array_to_nmatrix(VALUE self) { uint ndims; af_get_numdims(&ndims, input->carray); - dim_t* dims = (dim_t*)malloc(ndims * sizeof(dim_t)); + dim_t* dims = ALLOC_N(dim_t, ndims); af_get_dims(&dims[0], &dims[1], &dims[2], &dims[3], input->carray); - size_t* shape = (size_t*)malloc(ndims * sizeof(size_t));; + size_t* shape = ALLOC_N(size_t, ndims); 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)); + double* elements = ALLOC_N(double, count); af_get_data_ptr(elements, input->carray); return rb_nmatrix_dense_create(nm::FLOAT64, shape, ndims, elements, (int)count); @@ -47,7 +47,7 @@ afstruct* arf_nmatrix_to_af_array(VALUE nm) { 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));; + dim_t* shape = ALLOC_N(dim_t, nmat->dim); for (size_t index = 0; index < nmat->dim; index++){ shape[index] = (size_t)(nmat->shape[index]); } diff --git a/lib/arrayfire/arrayfire.rb b/lib/arrayfire/arrayfire.rb index b1fd09a..5e220d6 100644 --- a/lib/arrayfire/arrayfire.rb +++ b/lib/arrayfire/arrayfire.rb @@ -1,4 +1,4 @@ require_relative '../arrayfire.so' require_relative 'lapack.rb' require_relative 'device.rb' -require_relative 'sparse.rb' \ No newline at end of file +require_relative 'sparse.rb' diff --git a/test/data_test.rb b/test/data_test.rb index a144b11..cce8270 100644 --- a/test/data_test.rb +++ b/test/data_test.rb @@ -11,7 +11,7 @@ def setup def test_constant result = ArrayFire::Af_Array.new 2, [3,3], [4, 4, 4, 4, 4, 4, 4, 4, 4] - assert_equal result, ArrayFire::Data.constant( 2, [2,2], 4 ); + assert_equal result, ArrayFire::Data.constant( 2, [3,3], 4 ); end def test_constant_complex From edec5676d726f7eba4c4bf883a934127a6a73817 Mon Sep 17 00:00:00 2001 From: prasun Date: Thu, 31 Aug 2017 22:30:57 +0530 Subject: [PATCH 124/125] added exception handling --- ext/mri/arrayfire.c | 12 ++-- ext/mri/cmodules/arith.c | 24 ++++--- ext/mri/cmodules/array.c | 132 +++++++++++++++++++++++++--------- ext/mri/cmodules/backend.c | 29 ++++++-- ext/mri/cmodules/blas.c | 16 +++-- ext/mri/cmodules/cuda.c | 8 ++- ext/mri/cmodules/data.c | 90 +++++++++++++++++------ ext/mri/cmodules/defines.c | 9 +++ ext/mri/cmodules/device.c | 78 ++++++++++++-------- ext/mri/cmodules/lapack.c | 52 ++++++++++---- ext/mri/cmodules/opencl.c | 9 ++- ext/mri/cmodules/random.c | 78 ++++++++++++++++---- ext/mri/cmodules/sparse.c | 45 +++++++++--- ext/mri/cmodules/statistics.c | 58 +++++++++++---- ext/mri/cmodules/util.c | 28 ++++++-- ext/mri/interfaces/nmatrix.c | 18 +++-- 16 files changed, 508 insertions(+), 178 deletions(-) diff --git a/ext/mri/arrayfire.c b/ext/mri/arrayfire.c index 44012ac..cc6508a 100644 --- a/ext/mri/arrayfire.c +++ b/ext/mri/arrayfire.c @@ -23,6 +23,8 @@ extern "C++" { // prototypes void Init_arrayfire(); +void arf_handle_exception(af_err error_code); + const char* get_backend_name(af_backend backend); const char* get_cl_device_name(afcl_device_type device); const char* get_cl_platform_name(afcl_platform platform); @@ -48,7 +50,7 @@ static VALUE arf_create_handle(int argc, VALUE* argv); static VALUE arf_copy_array(VALUE self); static VALUE arf_write_array(VALUE self); static VALUE arf_get_data_ptr(VALUE self); -static void arf_release_array(VALUE self); +static VALUE arf_release_array(VALUE self); static VALUE arf_retain_array(VALUE self); static VALUE arf_get_data_ref_count(VALUE self); static VALUE arf_eval(VALUE self); @@ -194,8 +196,8 @@ static VALUE arf_upper(VALUE self, VALUE array_val, VALUE dim_val); static VALUE arf_select(VALUE self, VALUE array_cond_val, VALUE array_a_val, VALUE array_b_val); static VALUE arf_select_scalar_r(VALUE self, VALUE array_cond_val, VALUE array_a_val, VALUE b_val); static VALUE arf_select_scalar_l(VALUE self, VALUE array_cond_val, VALUE a_val, VALUE array_b_val); -static void arf_replace(VALUE self, VALUE array_input_val, VALUE array_cond_val, VALUE array_b_val); -static void arf_replace_scalar(VALUE self, VALUE array_input_val, VALUE array_cond_val, VALUE b_val); +static VALUE arf_replace(VALUE self, VALUE array_input_val, VALUE array_cond_val, VALUE array_b_val); +static VALUE arf_replace_scalar(VALUE self, VALUE array_input_val, VALUE array_cond_val, VALUE b_val); // Index static VALUE arf_index(VALUE self); @@ -668,7 +670,9 @@ VALUE arf_init(int argc, VALUE* argv, VALUE self) host_array[index] = (double)NUM2DBL(RARRAY_AREF(argv[2], index)); } - af_create_array(&afarray->carray, host_array, ndims, dimensions, dtype); + af_err flag = af_create_array(&afarray->carray, host_array, ndims, dimensions, dtype); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); af_print_array(afarray->carray); } diff --git a/ext/mri/cmodules/arith.c b/ext/mri/cmodules/arith.c index f343d66..fdbea23 100644 --- a/ext/mri/cmodules/arith.c +++ b/ext/mri/cmodules/arith.c @@ -1,13 +1,14 @@ -#define DEF_ELEMENTWISE_RUBY_ACCESSOR(name, oper) \ -static VALUE arf_ew_##name(VALUE left_val, VALUE right_val) { \ - afstruct* left; \ - afstruct* right; \ - afstruct* result = ALLOC(afstruct); \ - Data_Get_Struct(left_val, afstruct, left); \ - Data_Get_Struct(right_val, afstruct, right); \ - af_##oper(&result->carray, left->carray, right->carray, true); \ - af_print_array(result->carray); \ - return Data_Wrap_Struct(CLASS_OF(left_val), NULL, arf_free, result); \ +#define DEF_ELEMENTWISE_RUBY_ACCESSOR(name, oper) \ +static VALUE arf_ew_##name(VALUE left_val, VALUE right_val) { \ + afstruct* left; \ + afstruct* right; \ + afstruct* result = ALLOC(afstruct); \ + Data_Get_Struct(left_val, afstruct, left); \ + Data_Get_Struct(right_val, afstruct, right); \ + af_err flag = af_##oper(&result->carray, left->carray, right->carray, true); \ + if (flag != AF_SUCCESS) arf_handle_exception(flag); \ + af_print_array(result->carray); \ + return Data_Wrap_Struct(CLASS_OF(left_val), NULL, arf_free, result); \ } #define DEF_UNARY_RUBY_ACCESSOR(oper, name) \ @@ -15,7 +16,8 @@ static VALUE arf_unary_##name(VALUE self) { \ afstruct* obj; \ afstruct* result = ALLOC(afstruct); \ Data_Get_Struct(self, afstruct, obj); \ - af_##oper(&result->carray, obj->carray); \ + af_err flag = af_##oper(&result->carray, obj->carray); \ + if (flag != AF_SUCCESS) arf_handle_exception(flag); \ af_print_array(result->carray); \ return Data_Wrap_Struct(CLASS_OF(self), NULL, arf_free, result); \ } diff --git a/ext/mri/cmodules/array.c b/ext/mri/cmodules/array.c index 6b2c970..cd5e4d9 100644 --- a/ext/mri/cmodules/array.c +++ b/ext/mri/cmodules/array.c @@ -12,8 +12,9 @@ static VALUE arf_create_array(int argc, VALUE* argv){ host_array[index] = (double)NUM2DBL(RARRAY_AREF(argv[2], index)); } - af_create_array(&afarray->carray, host_array, ndims, dimensions, f32); + af_err flag = af_create_array(&afarray->carray, host_array, ndims, dimensions, f32); + if (flag != AF_SUCCESS) arf_handle_exception(flag); af_print_array(afarray->carray); return Data_Wrap_Struct(Af_Array, NULL, arf_free, afarray); @@ -33,8 +34,9 @@ static VALUE arf_create_handle(int argc, VALUE* argv){ host_array[index] = (double)NUM2DBL(RARRAY_AREF(argv[2], index)); } - af_create_handle(&afarray->carray, ndims, dimensions, f32); + af_err flag = af_create_handle(&afarray->carray, ndims, dimensions, f32); + if (flag != AF_SUCCESS) arf_handle_exception(flag); af_print_array(afarray->carray); return Data_Wrap_Struct(Af_Array, NULL, arf_free, afarray); @@ -46,7 +48,9 @@ static VALUE arf_copy_array(VALUE self){ Data_Get_Struct(self, afstruct, array_val); - af_copy_array(&result->carray, array_val->carray); + af_err flag = af_copy_array(&result->carray, array_val->carray); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); return Data_Wrap_Struct(CLASS_OF(self), NULL, arf_free, result); } @@ -61,9 +65,13 @@ static VALUE arf_get_data_ptr(VALUE self){ Data_Get_Struct(self, afstruct, input); - af_get_elements(&count, input->carray); + af_err flag = af_get_elements(&count, input->carray); + if (flag != AF_SUCCESS) arf_handle_exception(flag); + double* data = ALLOC_N(double, count); - af_get_data_ptr(data, input->carray); + + flag = af_get_data_ptr(data, input->carray); + if (flag != AF_SUCCESS) arf_handle_exception(flag); VALUE* array = ALLOC_N(VALUE, count); for (dim_t index = 0; index < count; index++){ @@ -73,17 +81,20 @@ static VALUE arf_get_data_ptr(VALUE self){ return rb_ary_new4(count, array); } -static void arf_release_array(VALUE self){ +static VALUE arf_release_array(VALUE self){ afstruct* input; Data_Get_Struct(self, afstruct, input); - af_release_array(input->carray); + af_err flag = af_release_array(input->carray); + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return Qtrue; } static VALUE arf_retain_array(VALUE self){ afstruct* input; afstruct* output = ALLOC(afstruct); Data_Get_Struct(self, afstruct, input); - af_retain_array(&output->carray, input->carray); + af_err flag = af_retain_array(&output->carray, input->carray); + if (flag != AF_SUCCESS) arf_handle_exception(flag); return Data_Wrap_Struct(CLASS_OF(self), NULL, arf_free, output); } @@ -91,14 +102,16 @@ static VALUE arf_get_data_ref_count(VALUE self){ afstruct* input; int use_count; Data_Get_Struct(self, afstruct, input); - af_get_data_ref_count(&use_count, input->carray); + af_err flag = af_get_data_ref_count(&use_count, input->carray); + if (flag != AF_SUCCESS) arf_handle_exception(flag); return INT2NUM(use_count); } static VALUE arf_eval(VALUE self){ afstruct* input; Data_Get_Struct(self, afstruct, input); - af_eval(input->carray); + af_err flag = af_eval(input->carray); + if (flag != AF_SUCCESS) arf_handle_exception(flag); return Qtrue; } @@ -107,13 +120,15 @@ static VALUE arf_eval_multiple(VALUE self){ } static VALUE arf_set_manual_eval_flag(VALUE self, VALUE flag){ - af_set_manual_eval_flag(RTEST(flag)); - return Qnil; + af_err code = af_set_manual_eval_flag(RTEST(flag)); + if (code != AF_SUCCESS) arf_handle_exception(code); + return Qtrue; } static VALUE arf_get_manual_eval_flag(VALUE self){ bool flag; - af_get_manual_eval_flag(&flag); + af_err code = af_get_manual_eval_flag(&flag); + if (code != AF_SUCCESS) arf_handle_exception(code); return flag ? Qtrue : Qfalse; } @@ -124,7 +139,10 @@ static VALUE arf_get_elements(VALUE self){ Data_Get_Struct(self, afstruct, input); - af_get_elements(&elems, input->carray); + af_err flag = af_get_elements(&elems, input->carray); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return ULONG2NUM(elems); } @@ -134,7 +152,10 @@ static VALUE arf_get_type(VALUE self){ Data_Get_Struct(self, afstruct, input); - af_get_type(&type, input->carray); + af_err flag = af_get_type(&type, input->carray); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return Qnil; } @@ -143,11 +164,14 @@ static VALUE arf_get_dims(VALUE self){ Data_Get_Struct(self, afstruct, input); uint ndims; - af_get_numdims(&ndims, input->carray); + + af_err flag = af_get_numdims(&ndims, input->carray); + if (flag != AF_SUCCESS) arf_handle_exception(flag); dim_t* dims = ALLOC_N(dim_t, ndims); - af_get_dims(&dims[0], &dims[1], &dims[2], &dims[3], input->carray); + flag = af_get_dims(&dims[0], &dims[1], &dims[2], &dims[3], input->carray); + if (flag != AF_SUCCESS) arf_handle_exception(flag); VALUE* array = ALLOC_N(VALUE, ndims); for (dim_t index = 0; index < ndims; index++){ @@ -163,7 +187,10 @@ static VALUE arf_get_numdims(VALUE self){ Data_Get_Struct(self, afstruct, input); - af_get_numdims(&result, input->carray); + af_err flag = af_get_numdims(&result, input->carray); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return UINT2NUM(result); } @@ -173,7 +200,10 @@ static VALUE arf_is_empty(VALUE self){ Data_Get_Struct(self, afstruct, input); - af_is_empty(&result, input->carray); + af_err flag = af_is_empty(&result, input->carray); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return result ? Qtrue : Qfalse; } @@ -183,7 +213,10 @@ static VALUE arf_is_scalar(VALUE self){ Data_Get_Struct(self, afstruct, input); - af_is_scalar(&result, input->carray); + af_err flag = af_is_scalar(&result, input->carray); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return result ? Qtrue : Qfalse; } @@ -193,7 +226,10 @@ static VALUE arf_is_row(VALUE self){ Data_Get_Struct(self, afstruct, input); - af_is_row(&result, input->carray); + af_err flag = af_is_row(&result, input->carray); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return result ? Qtrue : Qfalse; } @@ -203,7 +239,10 @@ static VALUE arf_is_column(VALUE self){ Data_Get_Struct(self, afstruct, input); - af_is_column(&result, input->carray); + af_err flag = af_is_column(&result, input->carray); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return result ? Qtrue : Qfalse; } @@ -213,7 +252,10 @@ static VALUE arf_is_vector(VALUE self){ Data_Get_Struct(self, afstruct, input); - af_is_vector(&result, input->carray); + af_err flag = af_is_vector(&result, input->carray); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return result ? Qtrue : Qfalse; } @@ -223,7 +265,10 @@ static VALUE arf_is_complex(VALUE self){ Data_Get_Struct(self, afstruct, input); - af_is_complex(&result, input->carray); + af_err flag = af_is_complex(&result, input->carray); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return result ? Qtrue : Qfalse; } @@ -233,7 +278,10 @@ static VALUE arf_is_real(VALUE self){ Data_Get_Struct(self, afstruct, input); - af_is_real(&result, input->carray); + af_err flag = af_is_real(&result, input->carray); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return result ? Qtrue : Qfalse; } @@ -243,7 +291,10 @@ static VALUE arf_is_double(VALUE self){ Data_Get_Struct(self, afstruct, input); - af_is_double(&result, input->carray); + af_err flag = af_is_double(&result, input->carray); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return result ? Qtrue : Qfalse; } @@ -253,7 +304,10 @@ static VALUE arf_is_single(VALUE self){ Data_Get_Struct(self, afstruct, input); - af_is_single(&result, input->carray); + af_err flag = af_is_single(&result, input->carray); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return result ? Qtrue : Qfalse; } @@ -263,7 +317,10 @@ static VALUE arf_is_realfloating(VALUE self){ Data_Get_Struct(self, afstruct, input); - af_is_realfloating(&result, input->carray); + af_err flag = af_is_realfloating(&result, input->carray); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return result ? Qtrue : Qfalse; } @@ -273,7 +330,10 @@ static VALUE arf_is_floating(VALUE self){ Data_Get_Struct(self, afstruct, input); - af_is_floating(&result, input->carray); + af_err flag = af_is_floating(&result, input->carray); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return result ? Qtrue : Qfalse; } @@ -283,7 +343,10 @@ static VALUE arf_is_integer(VALUE self){ Data_Get_Struct(self, afstruct, input); - af_is_integer(&result, input->carray); + af_err flag = af_is_integer(&result, input->carray); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return result ? Qtrue : Qfalse; } @@ -293,7 +356,10 @@ static VALUE arf_is_bool(VALUE self){ Data_Get_Struct(self, afstruct, input); - af_is_bool(&result, input->carray); + af_err flag = af_is_bool(&result, input->carray); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return result ? Qtrue : Qfalse; } @@ -304,6 +370,8 @@ static VALUE arf_is_sparse(VALUE self){ Data_Get_Struct(self, afstruct, input); //FIXME - af_is_bool(&result, input->carray); + af_err flag = af_is_bool(&result, input->carray); + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return result ? Qtrue : Qfalse; } diff --git a/ext/mri/cmodules/backend.c b/ext/mri/cmodules/backend.c index c1a0425..443f371 100644 --- a/ext/mri/cmodules/backend.c +++ b/ext/mri/cmodules/backend.c @@ -1,6 +1,9 @@ static VALUE arf_get_backend_count(VALUE self){ uint num_backends; - af_get_backend_count(&num_backends); + + af_err flag = af_get_backend_count(&num_backends); + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return UINT2NUM(num_backends); } @@ -11,7 +14,10 @@ static VALUE arf_get_backend_count(VALUE self){ static VALUE arf_get_available_backends(VALUE self){ int backends; - af_get_available_backends(&backends); + + af_err flag = af_get_available_backends(&backends); + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return INT2NUM(backends); } @@ -19,7 +25,9 @@ static VALUE arf_get_backend_id(VALUE self, VALUE array_val){ afstruct* input; Data_Get_Struct(array_val, afstruct, input); af_backend backend; - af_get_backend_id (&backend, input->carray); + + af_err flag = af_get_backend_id (&backend, input->carray); + if (flag != AF_SUCCESS) arf_handle_exception(flag); const char* backend_name = get_backend_name(backend); return rb_str_new_cstr(backend_name); @@ -27,7 +35,10 @@ static VALUE arf_get_backend_id(VALUE self, VALUE array_val){ static VALUE arf_get_active_backend(VALUE self){ af_backend backend; - af_get_active_backend(&backend); + + af_err flag = af_get_active_backend(&backend); + if (flag != AF_SUCCESS) arf_handle_exception(flag); + const char* backend_name = get_backend_name(backend); return rb_str_new_cstr(backend_name); } @@ -38,13 +49,17 @@ static VALUE arf_get_backend_device_id(VALUE self, VALUE array_val){ Data_Get_Struct(array_val, afstruct, input); - af_get_device_id(&device_id, input->carray); + af_err flag = af_get_device_id(&device_id, input->carray); + if (flag != AF_SUCCESS) arf_handle_exception(flag); return INT2NUM(device_id); } static VALUE arf_set_backend(VALUE self, VALUE backend_val){ af_backend backend = arf_backend_type_from_rbsymbol(backend_val); - af_set_backend(backend); - return Qnil; + + af_err flag = af_set_backend(backend); + if (flag != AF_SUCCESS) arf_handle_exception(flag); + + return Qtrue; } diff --git a/ext/mri/cmodules/blas.c b/ext/mri/cmodules/blas.c index 5e023a8..19ec7d2 100644 --- a/ext/mri/cmodules/blas.c +++ b/ext/mri/cmodules/blas.c @@ -10,7 +10,9 @@ static VALUE arf_matmul(VALUE self, VALUE left_val, VALUE right_val, VALUE left_ af_mat_prop left_mat_prop = arf_mat_type_from_rbsymbol(left_prop_val); af_mat_prop right_mat_prop = arf_mat_type_from_rbsymbol(right_prop_val); - af_matmul(&result->carray, left->carray, right->carray, left_mat_prop, right_mat_prop); + af_err flag = af_matmul(&result->carray, left->carray, right->carray, left_mat_prop, right_mat_prop); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); return Data_Wrap_Struct(CLASS_OF(left_val), NULL, arf_free, result); } @@ -26,7 +28,9 @@ static VALUE arf_dot(VALUE self, VALUE left_val, VALUE right_val, VALUE left_pro af_mat_prop left_mat_prop = arf_mat_type_from_rbsymbol(left_prop_val); af_mat_prop right_mat_prop = arf_mat_type_from_rbsymbol(right_prop_val); - af_dot(&result->carray, left->carray, right->carray, left_mat_prop, right_mat_prop); + af_err flag = af_dot(&result->carray, left->carray, right->carray, left_mat_prop, right_mat_prop); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); return Data_Wrap_Struct(CLASS_OF(left_val), NULL, arf_free, result); } @@ -37,7 +41,9 @@ static VALUE arf_transpose(VALUE self, VALUE input){ Data_Get_Struct(input, afstruct, obj); - af_transpose(&result->carray, obj->carray, false); + af_err flag = af_transpose(&result->carray, obj->carray, false); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); return Data_Wrap_Struct(CLASS_OF(input), NULL, arf_free, result); } @@ -47,7 +53,9 @@ static VALUE arf_transpose_inplace(VALUE self, VALUE input){ Data_Get_Struct(input, afstruct, obj); - af_transpose_inplace(obj->carray, false); + af_err flag = af_transpose_inplace(obj->carray, false); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); return Data_Wrap_Struct(CLASS_OF(input), NULL, arf_free, obj); } diff --git a/ext/mri/cmodules/cuda.c b/ext/mri/cmodules/cuda.c index 5a061bc..0d6cf1b 100644 --- a/ext/mri/cmodules/cuda.c +++ b/ext/mri/cmodules/cuda.c @@ -6,11 +6,13 @@ static VALUE arf_get_stream(VALUE self, VALUE id){ static VALUE arf_get_native_id(VALUE self, VALUE cuda_device_id){ int native_id; - afcu_get_native_id(&native_id, NUM2INT(cuda_device_id)); + af_err flag = afcu_get_native_id(&native_id, NUM2INT(cuda_device_id)); + if (flag != AF_SUCCESS) arf_handle_exception(flag); return INT2NUM(native_id); } static VALUE arf_set_native_id(VALUE self, VALUE native_id){ - afcu_set_native_id(native_id); - return Qnil; + af_err flag = afcu_set_native_id(native_id); + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return Qtrue; } diff --git a/ext/mri/cmodules/data.c b/ext/mri/cmodules/data.c index ebcd2cd..9fafa37 100644 --- a/ext/mri/cmodules/data.c +++ b/ext/mri/cmodules/data.c @@ -9,7 +9,10 @@ static VALUE arf_constant(int argc, VALUE* argv){ count *= dimensions[index]; } double data = NUM2DBL(argv[2]); - af_constant(&output->carray, data, 2, dimensions, f32); + + af_err flag = af_constant(&output->carray, data, 2, dimensions, f32); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); af_print_array(output->carray); return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); @@ -30,7 +33,10 @@ static VALUE arf_constant_long(int argc, VALUE* argv){ count *= dimensions[index]; } long data = NUM2LONG(argv[2]); - af_constant_long(&output->carray, data, 2, dimensions); + + af_err flag = af_constant_long(&output->carray, data, 2, dimensions); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); af_print_array(output->carray); return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); @@ -47,7 +53,10 @@ static VALUE arf_constant_ulong(int argc, VALUE* argv){ count *= dimensions[index]; } unsigned long data = NUM2LONG(argv[2]); - af_constant_ulong(&output->carray, data, 2, dimensions); + + af_err flag = af_constant_ulong(&output->carray, data, 2, dimensions); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); af_print_array(output->carray); return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); @@ -64,7 +73,9 @@ static VALUE arf_range(int argc, VALUE* argv){ count *= dimensions[index]; } int seq_dim = NUM2INT(argv[2]); - af_range(&output->carray, ndims, dimensions, seq_dim, f32); + af_err flag = af_range(&output->carray, ndims, dimensions, seq_dim, f32); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); af_print_array(output->carray); return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); @@ -84,7 +95,10 @@ static VALUE arf_identity(int argc, VALUE* argv){ dimensions[index] = (dim_t)FIX2LONG(RARRAY_AREF(argv[1], index)); count *= dimensions[index]; } - af_identity(&output->carray, ndims, dimensions, f32); + + af_err flag = af_identity(&output->carray, ndims, dimensions, f32); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); af_print_array(output->carray); return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); @@ -96,7 +110,9 @@ static VALUE arf_diag_create(VALUE self, VALUE array_val, VALUE num_val){ Data_Get_Struct(array_val, afstruct, input); - af_diag_create(&output->carray, input->carray, NUM2INT(num_val)); + af_err flag = af_diag_create(&output->carray, input->carray, NUM2INT(num_val)); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); af_print_array(output->carray); return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); @@ -108,7 +124,9 @@ static VALUE arf_diag_extract(VALUE self, VALUE array_val, VALUE num_val){ Data_Get_Struct(array_val, afstruct, input); - af_diag_extract(&output->carray, input->carray, NUM2INT(num_val)); + af_err flag = af_diag_extract(&output->carray, input->carray, NUM2INT(num_val)); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); af_print_array(output->carray); return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); @@ -122,7 +140,9 @@ static VALUE arf_join(VALUE self, VALUE dim_val, VALUE first_array_val, VALUE se Data_Get_Struct(first_array_val, afstruct, first_array); Data_Get_Struct(second_array_val, afstruct, second_array); - af_join(&output->carray, NUM2INT(dim_val), first_array->carray, second_array->carray); + af_err flag = af_join(&output->carray, NUM2INT(dim_val), first_array->carray, second_array->carray); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); af_print_array(output->carray); return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); @@ -138,7 +158,9 @@ static VALUE arf_tile(VALUE self, VALUE array_val, VALUE x_val, VALUE y_val, VAL Data_Get_Struct(array_val, afstruct, input); - af_tile(&output->carray, input->carray, NUM2UINT(x_val), NUM2UINT(y_val), NUM2UINT(z_val), NUM2UINT(w_val)); + af_err flag = af_tile(&output->carray, input->carray, NUM2UINT(x_val), NUM2UINT(y_val), NUM2UINT(z_val), NUM2UINT(w_val)); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); af_print_array(output->carray); return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); @@ -150,7 +172,9 @@ static VALUE arf_reorder(VALUE self, VALUE array_val, VALUE x_val, VALUE y_val, Data_Get_Struct(array_val, afstruct, input); - af_reorder(&output->carray, input->carray, NUM2UINT(x_val), NUM2UINT(y_val), NUM2UINT(z_val), NUM2UINT(w_val)); + af_err flag = af_reorder(&output->carray, input->carray, NUM2UINT(x_val), NUM2UINT(y_val), NUM2UINT(z_val), NUM2UINT(w_val)); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); af_print_array(output->carray); return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); @@ -162,7 +186,9 @@ static VALUE arf_shift(VALUE self, VALUE array_val, VALUE x_val, VALUE y_val, VA Data_Get_Struct(array_val, afstruct, input); - af_shift(&output->carray, input->carray, NUM2INT(x_val), NUM2INT(y_val), NUM2INT(z_val), NUM2INT(w_val)); + af_err flag = af_shift(&output->carray, input->carray, NUM2INT(x_val), NUM2INT(y_val), NUM2INT(z_val), NUM2INT(w_val)); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); af_print_array(output->carray); return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); @@ -178,7 +204,9 @@ static VALUE arf_flat(VALUE self, VALUE array_val){ Data_Get_Struct(array_val, afstruct, input); - af_flat(&output->carray, input->carray); + af_err flag = af_flat(&output->carray, input->carray); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); af_print_array(output->carray); return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); @@ -190,7 +218,9 @@ static VALUE arf_flip(VALUE self, VALUE array_val, VALUE dim_val){ Data_Get_Struct(array_val, afstruct, input); - af_flip(&output->carray, input->carray, NUM2UINT(dim_val)); + af_err flag = af_flip(&output->carray, input->carray, NUM2UINT(dim_val)); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); af_print_array(output->carray); return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); @@ -202,7 +232,9 @@ static VALUE arf_lower(VALUE self, VALUE array_val, VALUE is_unit_diag){ Data_Get_Struct(array_val, afstruct, input); - af_lower(&output->carray, input->carray, RTEST(is_unit_diag)); + af_err flag = af_lower(&output->carray, input->carray, RTEST(is_unit_diag)); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); af_print_array(output->carray); return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); @@ -214,7 +246,9 @@ static VALUE arf_upper(VALUE self, VALUE array_val, VALUE is_unit_diag){ Data_Get_Struct(array_val, afstruct, input); - af_upper(&output->carray, input->carray, RTEST(is_unit_diag)); + af_err flag = af_upper(&output->carray, input->carray, RTEST(is_unit_diag)); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); af_print_array(output->carray); return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); @@ -230,7 +264,9 @@ static VALUE arf_select(VALUE self, VALUE array_cond_val, VALUE array_a_val, VAL Data_Get_Struct(array_a_val, afstruct, array_a); Data_Get_Struct(array_b_val, afstruct, array_b); - af_select(&output->carray, array_cond->carray, array_a->carray, array_b->carray); + af_err flag = af_select(&output->carray, array_cond->carray, array_a->carray, array_b->carray); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); af_print_array(output->carray); return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); @@ -244,7 +280,9 @@ static VALUE arf_select_scalar_r(VALUE self, VALUE array_cond_val, VALUE array_a Data_Get_Struct(array_cond_val, afstruct, array_cond); Data_Get_Struct(array_a_val, afstruct, array_a); - af_select_scalar_r(&output->carray, array_cond->carray, array_a->carray, NUM2DBL(b_val)); + af_err flag = af_select_scalar_r(&output->carray, array_cond->carray, array_a->carray, NUM2DBL(b_val)); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); af_print_array(output->carray); return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); @@ -258,13 +296,15 @@ static VALUE arf_select_scalar_l(VALUE self, VALUE array_cond_val, VALUE a_val, Data_Get_Struct(array_cond_val, afstruct, array_cond); Data_Get_Struct(array_b_val, afstruct, array_b); - af_select_scalar_l(&output->carray, array_cond->carray, NUM2DBL(a_val), array_b->carray); + af_err flag = af_select_scalar_l(&output->carray, array_cond->carray, NUM2DBL(a_val), array_b->carray); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); af_print_array(output->carray); return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } -static void arf_replace(VALUE self, VALUE array_input_val, VALUE array_cond_val, VALUE array_b_val){ +static VALUE arf_replace(VALUE self, VALUE array_input_val, VALUE array_cond_val, VALUE array_b_val){ afstruct* input; afstruct* array_cond; afstruct* array_b; @@ -273,17 +313,23 @@ static void arf_replace(VALUE self, VALUE array_input_val, VALUE array_cond_val, Data_Get_Struct(array_cond_val, afstruct, array_cond); Data_Get_Struct(array_b_val, afstruct, array_b); - af_replace(&input->carray, array_cond->carray, array_b->carray); + af_err flag = af_replace(&input->carray, array_cond->carray, array_b->carray); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return Qtrue; } -static void arf_replace_scalar(VALUE self, VALUE array_input_val, VALUE array_cond_val, VALUE b_val){ +static VALUE arf_replace_scalar(VALUE self, VALUE array_input_val, VALUE array_cond_val, VALUE b_val){ afstruct* input; afstruct* array_cond; Data_Get_Struct(array_input_val, afstruct, input); Data_Get_Struct(array_cond_val, afstruct, array_cond); - af_replace_scalar(&input->carray, array_cond->carray, NUM2DBL(b_val)); + af_err flag = af_replace_scalar(&input->carray, array_cond->carray, NUM2DBL(b_val)); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return Qtrue; } diff --git a/ext/mri/cmodules/defines.c b/ext/mri/cmodules/defines.c index ad1e2b6..08674c3 100644 --- a/ext/mri/cmodules/defines.c +++ b/ext/mri/cmodules/defines.c @@ -101,6 +101,15 @@ const char* const STORAGE_TYPES[ARF_NUM_STORAGE_TYPES] = { "AF_STORAGE_COO", ///< Storage type is COO }; +void arf_handle_exception(af_err error_code){ + for(std::map::value_type& entry : ERROR_TYPES) { + if (error_code == entry.second) { + rb_raise(rb_eArgError, entry.first); + break; + } + } +} + af_dtype arf_dtype_from_rbsymbol(VALUE sym) { ID sym_id = SYM2ID(sym); diff --git a/ext/mri/cmodules/device.c b/ext/mri/cmodules/device.c index c247a88..cad5699 100644 --- a/ext/mri/cmodules/device.c +++ b/ext/mri/cmodules/device.c @@ -1,16 +1,19 @@ static VALUE arf_info(VALUE self){ - af_info(); - return Qnil; + af_err flag = af_info(); + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return Qtrue; } static VALUE arf_init2(VALUE self){ - af_init(); - return Qnil; + af_err flag = af_init(); + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return Qtrue; } static VALUE arf_info_string(VALUE self, VALUE bool_val){ char* str; - af_info_string(&str, RTEST(bool_val)); + af_err flag = af_info_string(&str, RTEST(bool_val)); + if (flag != AF_SUCCESS) arf_handle_exception(flag); return rb_str_new_cstr(str); } @@ -20,42 +23,47 @@ static VALUE arf_device_info(VALUE self, VALUE name_val, VALUE platform_val, VAL char* d_toolkit = ALLOC_N(char, sizeof(char) * 64); char* d_compute = ALLOC_N(char, sizeof(char) * 10); - af_device_info(d_name, d_platform, d_toolkit, d_compute); + af_err flag = af_device_info(d_name, d_platform, d_toolkit, d_compute); + if (flag != AF_SUCCESS) arf_handle_exception(flag); rb_str_cat2(name_val, d_name); rb_str_cat2(platform_val, d_platform); rb_str_cat2(toolkit_val, d_toolkit); rb_str_cat2(compute_val, d_compute); - return Qnil; + return Qtrue; } static VALUE arf_get_device_count(VALUE self){ int num_of_devices; - af_get_device_count(&num_of_devices); + af_err flag = af_get_device_count(&num_of_devices); + if (flag != AF_SUCCESS) arf_handle_exception(flag); return INT2NUM(num_of_devices); } static VALUE arf_get_dbl_support(VALUE self, VALUE device){ bool available; - af_get_dbl_support(&available, NUM2INT(device)); + af_err flag = af_get_dbl_support(&available, NUM2INT(device)); + if (flag != AF_SUCCESS) arf_handle_exception(flag); return available ? Qtrue : Qfalse; } static VALUE arf_set_device(VALUE self, VALUE device){ - af_set_device(NUM2INT(device)); - return Qnil; + af_err flag = af_set_device(NUM2INT(device)); + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return Qtrue; } static VALUE arf_get_device(VALUE self){ int device; - af_get_device(&device); + af_err flag = af_get_device(&device); return INT2NUM(device); } static VALUE arf_sync(VALUE self, VALUE device_val){ - af_sync(NUM2INT(device_val)); - return Qnil; + af_err flag = af_sync(NUM2INT(device_val)); + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return Qtrue; } static VALUE arf_alloc_device(VALUE self){ @@ -88,66 +96,76 @@ static VALUE arf_device_array(VALUE self){ static VALUE arf_device_mem_info(VALUE self){ size_t alloc_bytes, alloc_buffers, lock_bytes, lock_buffers; - af_device_mem_info( &alloc_bytes, &alloc_buffers, &lock_bytes, &lock_buffers); + af_err flag = af_device_mem_info( &alloc_bytes, &alloc_buffers, &lock_bytes, &lock_buffers); + if (flag != AF_SUCCESS) arf_handle_exception(flag); printf("Allocated Bytes: %d\nAllocated buffers: %d\nLock Bytes: %d\nLock Buffers: %d\n", alloc_bytes, alloc_buffers, lock_bytes, lock_buffers); - return Qnil; + return Qtrue; } static VALUE arf_print_mem_info(VALUE self, VALUE msg_val, VALUE device_id_val){ const char* msg = StringValuePtr(msg_val); - af_print_mem_info( msg, NUM2INT(device_id_val)); - return Qnil; + af_err flag = af_print_mem_info( msg, NUM2INT(device_id_val)); + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return Qtrue; } static void arf_device_gc(VALUE self){ - af_device_gc(); + af_err flag = af_device_gc(); + if (flag != AF_SUCCESS) arf_handle_exception(flag); } static VALUE arf_set_mem_step_size(VALUE self, VALUE step_bytes){ - af_set_mem_step_size(NUM2UINT(step_bytes)); + af_err flag = af_set_mem_step_size(NUM2UINT(step_bytes)); + if (flag != AF_SUCCESS) arf_handle_exception(flag); return Qtrue; } static VALUE arf_get_mem_step_size(VALUE self){ size_t step_bytes; - af_get_mem_step_size(&step_bytes); + af_err flag = af_get_mem_step_size(&step_bytes); + if (flag != AF_SUCCESS) arf_handle_exception(flag); return UINT2NUM(step_bytes); } static VALUE arf_lock_device_ptr(VALUE self, VALUE array_val){ afstruct* input; Data_Get_Struct(array_val, afstruct, input); - af_lock_device_ptr(input->carray); - return Qnil; + af_err flag = af_lock_device_ptr(input->carray); + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return Qtrue; } static VALUE arf_unlock_device_ptr(VALUE self, VALUE array_val){ afstruct* input; Data_Get_Struct(array_val, afstruct, input); - af_unlock_device_ptr(input->carray); - return Qnil; + af_err flag = af_unlock_device_ptr(input->carray); + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return Qtrue; } static VALUE arf_lock_array(VALUE self, VALUE array_val){ afstruct* input; Data_Get_Struct(array_val, afstruct, input); - af_lock_array(input->carray); - return Qnil; + af_err flag = af_lock_array(input->carray); + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return Qtrue; } static VALUE arf_unlock_array(VALUE self, VALUE array_val){ afstruct* input; Data_Get_Struct(array_val, afstruct, input); - af_unlock_array(input->carray); - return Qnil; + af_err flag = af_unlock_array(input->carray); + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return Qtrue; } static VALUE arf_is_locked_array(VALUE self, VALUE array_val){ bool res; afstruct* input; Data_Get_Struct(array_val, afstruct, input); - af_is_locked_array(&res, input->carray); + af_err flag = af_is_locked_array(&res, input->carray); + if (flag != AF_SUCCESS) arf_handle_exception(flag); return res ? Qtrue : Qfalse; } diff --git a/ext/mri/cmodules/lapack.c b/ext/mri/cmodules/lapack.c index 270ab78..55f100f 100644 --- a/ext/mri/cmodules/lapack.c +++ b/ext/mri/cmodules/lapack.c @@ -9,7 +9,9 @@ static VALUE arf_svd_func(VALUE self, VALUE u_val, VALUE s_val, VALUE vt_val, VA Data_Get_Struct(s_val, afstruct, s); Data_Get_Struct(vt_val, afstruct, vt); - af_svd(&u->carray, &s->carray, &vt->carray, input->carray); + af_err flag = af_svd(&u->carray, &s->carray, &vt->carray, input->carray); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); return Qtrue; } @@ -22,7 +24,9 @@ static VALUE arf_svd_inplace_func(VALUE self, VALUE val){ Data_Get_Struct(val, afstruct, input); - af_svd_inplace(&u->carray, &s->carray, &vt->carray, input->carray); + af_err flag = af_svd_inplace(&u->carray, &s->carray, &vt->carray, input->carray); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); return Data_Wrap_Struct(CLASS_OF(val), NULL, arf_free, u); } @@ -57,7 +61,10 @@ static VALUE arf_qr_func(VALUE self, VALUE q_val, VALUE r_val, VALUE tau_val, VA Data_Get_Struct(r_val, afstruct, r); Data_Get_Struct(tau_val, afstruct, tau); - af_qr(&q->carray, &r->carray, &tau->carray, input->carray); + af_err flag = af_qr(&q->carray, &r->carray, &tau->carray, input->carray); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return Qfalse; } @@ -73,7 +80,9 @@ static VALUE arf_cholesky_func(VALUE self, VALUE output_val, VALUE val, VALUE is Data_Get_Struct(val, afstruct, input); Data_Get_Struct(output_val, afstruct, output); - af_cholesky(&output->carray, &info, input->carray, is_upper_val); + af_err flag = af_cholesky(&output->carray, &info, input->carray, is_upper_val); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); return INT2NUM(info); } @@ -91,9 +100,11 @@ static VALUE arf_solve(VALUE self, VALUE lhs_val, VALUE rhs_val){ Data_Get_Struct(lhs_val, afstruct, lhs); Data_Get_Struct(rhs_val, afstruct, rhs); - af_solve(&result->carray, lhs->carray, rhs->carray, AF_MAT_NONE); - return Data_Wrap_Struct(CLASS_OF(lhs_val), NULL, arf_free, result); + af_err flag = af_solve(&result->carray, lhs->carray, rhs->carray, AF_MAT_NONE); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return Data_Wrap_Struct(CLASS_OF(lhs_val), NULL, arf_free, result); } static VALUE arf_solve_lu(VALUE self, VALUE lhs_val, VALUE rhs_val, VALUE piv_val){ @@ -106,7 +117,10 @@ static VALUE arf_solve_lu(VALUE self, VALUE lhs_val, VALUE rhs_val, VALUE piv_va Data_Get_Struct(rhs_val, afstruct, rhs); Data_Get_Struct(piv_val, afstruct, piv); - af_solve_lu(&result->carray, lhs->carray, piv->carray, rhs->carray, AF_MAT_NONE); + af_err flag = af_solve_lu(&result->carray, lhs->carray, piv->carray, rhs->carray, AF_MAT_NONE); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return Data_Wrap_Struct(CLASS_OF(lhs_val), NULL, arf_free, result); } @@ -116,7 +130,9 @@ static VALUE arf_inverse(VALUE self, VALUE val){ Data_Get_Struct(val, afstruct, matrix); - af_inverse(&result->carray, matrix->carray, AF_MAT_NONE); + af_err flag = af_inverse(&result->carray, matrix->carray, AF_MAT_NONE); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); return Data_Wrap_Struct(CLASS_OF(val), NULL, arf_free, result); } @@ -127,7 +143,10 @@ static VALUE arf_rank(VALUE self, VALUE val){ Data_Get_Struct(val, afstruct, matrix); - af_rank(&rank, matrix->carray, 0.001); + af_err flag = af_rank(&rank, matrix->carray, 0.001); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return UINT2NUM(rank); } @@ -137,7 +156,10 @@ static VALUE arf_det(VALUE self, VALUE val){ Data_Get_Struct(val, afstruct, matrix); - af_det(&det_real, &det_imag, matrix->carray); + af_err flag = af_det(&det_real, &det_imag, matrix->carray); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return DBL2NUM(det_real); } @@ -147,12 +169,18 @@ static VALUE arf_norm(VALUE self, VALUE val){ double p = 0; double q = 0; Data_Get_Struct(val, afstruct, matrix); - af_norm(&norm, matrix->carray, AF_NORM_EUCLID, p, q); + af_err flag = af_norm(&norm, matrix->carray, AF_NORM_EUCLID, p, q); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return DBL2NUM(norm); } static VALUE arf_is_lapack_available(VALUE self){ bool output; - af_is_lapack_available(&output); + af_err flag = af_is_lapack_available(&output); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return output ? Qtrue : Qfalse; } diff --git a/ext/mri/cmodules/opencl.c b/ext/mri/cmodules/opencl.c index 66b78b9..6b3e103 100644 --- a/ext/mri/cmodules/opencl.c +++ b/ext/mri/cmodules/opencl.c @@ -29,7 +29,8 @@ static VALUE arf_get_queue(VALUE self){ static VALUE arf_get_device_id(VALUE self){ cl_device_id id; - afcl_get_device_id(&id); + af_err flag = afcl_get_device_id(&id); + if (flag != AF_SUCCESS) arf_handle_exception(flag); return Qnil; } @@ -51,13 +52,15 @@ static VALUE arf_delete_device_context(VALUE self){ static VALUE arf_get_device_type(VALUE self){ afcl_device_type device; - afcl_get_device_type(&device); + af_err flag = afcl_get_device_type(&device); + if (flag != AF_SUCCESS) arf_handle_exception(flag); return rb_str_new_cstr(get_cl_device_name(device)); } static VALUE arf_get_platform(VALUE self){ afcl_platform platform; - afcl_get_platform(&platform); + af_err flag = afcl_get_platform(&platform); + if (flag != AF_SUCCESS) arf_handle_exception(flag); return rb_str_new_cstr(get_cl_platform_name(platform)); } diff --git a/ext/mri/cmodules/random.c b/ext/mri/cmodules/random.c index ef96c26..033a7e8 100644 --- a/ext/mri/cmodules/random.c +++ b/ext/mri/cmodules/random.c @@ -2,7 +2,9 @@ static VALUE arf_create_random_engine(VALUE self, VALUE type_val, VALUE seed_val afrandomenginestruct* output = ALLOC(afrandomenginestruct); af_random_engine_type rtype = arf_randome_engine_type_from_rbsymbol(type_val); - af_create_random_engine(&output->cengine, AF_RANDOM_ENGINE_DEFAULT, NUM2ULL(seed_val) ) ; + af_err flag = af_create_random_engine(&output->cengine, AF_RANDOM_ENGINE_DEFAULT, NUM2ULL(seed_val) ) ; + + if (flag != AF_SUCCESS) arf_handle_exception(flag); return Data_Wrap_Struct(Random, NULL, arf_engine_free, output); } @@ -12,7 +14,10 @@ static VALUE arf_retain_random_engine(VALUE self, VALUE engine_val){ afrandomenginestruct* engine; Data_Get_Struct(engine_val, afrandomenginestruct, engine); - af_retain_random_engine ( &output->cengine, engine->cengine); + + af_err flag = af_retain_random_engine ( &output->cengine, engine->cengine); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); return Data_Wrap_Struct(Random, NULL, arf_engine_free, output); } @@ -22,7 +27,10 @@ static VALUE arf_random_engine_set_type(VALUE self, VALUE engine_val, VALUE type afrandomenginestruct* engine; Data_Get_Struct(engine_val, afrandomenginestruct, engine); - af_random_engine_set_type(&engine->cengine, rtype); + + af_err flag = af_random_engine_set_type(&engine->cengine, rtype); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); return Qtrue; } @@ -32,7 +40,10 @@ static VALUE arf_random_engine_get_type(VALUE self, VALUE engine_val){ afrandomenginestruct* engine; Data_Get_Struct(engine_val, afrandomenginestruct, engine); - af_random_engine_get_type(&rtype, engine->cengine); + + af_err flag = af_random_engine_get_type(&rtype, engine->cengine); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); const char* rengine = get_random_engine_name(rtype); return rb_str_new_cstr(rengine); @@ -52,7 +63,10 @@ static VALUE arf_random_uniform(VALUE self, VALUE ndims_val, VALUE dim_val, VALU count *= dimensions[index]; } - af_random_uniform(&out_array->carray, ndims, dimensions, f32, engine->cengine); + af_err flag = af_random_uniform(&out_array->carray, ndims, dimensions, f32, engine->cengine); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return Data_Wrap_Struct(Af_Array, NULL, arf_free, out_array); } @@ -70,7 +84,10 @@ static VALUE arf_random_normal(VALUE self, VALUE ndims_val, VALUE dim_val, VALUE count *= dimensions[index]; } - af_random_uniform(&out_array->carray, ndims, dimensions, f32, engine->cengine); + af_err flag = af_random_uniform(&out_array->carray, ndims, dimensions, f32, engine->cengine); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return Data_Wrap_Struct(Af_Array, NULL, arf_free, out_array); } @@ -78,21 +95,31 @@ static VALUE arf_random_engine_set_seed(VALUE self, VALUE engine_val ,VALUE seed afrandomenginestruct* engine; Data_Get_Struct(engine_val, afrandomenginestruct, engine); - af_random_engine_set_seed (&engine->cengine, NUM2ULL(seed_val)); + + af_err flag = af_random_engine_set_seed (&engine->cengine, NUM2ULL(seed_val)); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); return Data_Wrap_Struct(Random, NULL, arf_engine_free, engine); } static VALUE arf_get_default_random_engine(VALUE self){ afrandomenginestruct* output = ALLOC(afrandomenginestruct); - af_get_default_random_engine(&output->cengine) ; + + af_err flag = af_get_default_random_engine(&output->cengine) ; + + if (flag != AF_SUCCESS) arf_handle_exception(flag); return Data_Wrap_Struct(Random, NULL, arf_engine_free, output); } static VALUE arf_set_default_random_engine_type(VALUE self, VALUE type_val){ af_random_engine_type rtype = arf_randome_engine_type_from_rbsymbol(type_val); - af_set_default_random_engine_type(rtype); + + af_err flag = af_set_default_random_engine_type(rtype); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return Qtrue; } @@ -102,7 +129,10 @@ static VALUE arf_random_engine_get_seed(VALUE self, VALUE engine_val){ Data_Get_Struct(engine_val, afrandomenginestruct, engine); - af_random_engine_get_seed(&seed, engine->cengine); + af_err flag = af_random_engine_get_seed(&seed, engine->cengine); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return ULL2NUM(seed); } @@ -111,7 +141,10 @@ static VALUE arf_release_random_engine(VALUE self, VALUE engine_val){ Data_Get_Struct(engine_val, afrandomenginestruct, engine); - af_release_random_engine(engine); + af_err flag = af_release_random_engine(engine); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return Qtrue; } @@ -125,7 +158,11 @@ static VALUE arf_randu(VALUE self, VALUE ndims_val, VALUE dim_val){ dimensions[index] = (dim_t)FIX2LONG(RARRAY_AREF(dim_val, index)); count *= dimensions[index]; } - af_randu(&out_array->carray, ndims, dimensions,f32); + + af_err flag = af_randu(&out_array->carray, ndims, dimensions,f32); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return Data_Wrap_Struct(Af_Array, NULL, arf_free, out_array); } @@ -139,17 +176,28 @@ static VALUE arf_randn(VALUE self, VALUE ndims_val, VALUE dim_val){ dimensions[index] = (dim_t)FIX2LONG(RARRAY_AREF(dim_val, index)); count *= dimensions[index]; } - af_randn(&out_array->carray, ndims, dimensions,f32); + + af_err flag = af_randn(&out_array->carray, ndims, dimensions,f32); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return Data_Wrap_Struct(Af_Array, NULL, arf_free, out_array); } static VALUE arf_set_seed(VALUE self, VALUE seed){ - af_set_seed(NUM2ULL(seed)); + af_err flag = af_set_seed(NUM2ULL(seed)); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return Qtrue; } static VALUE arf_get_seed(VALUE self){ uintl seed; - af_get_seed(&seed); + + af_err flag = af_get_seed(&seed); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return ULL2NUM(seed); } diff --git a/ext/mri/cmodules/sparse.c b/ext/mri/cmodules/sparse.c index 55f6eea..fcb80ea 100644 --- a/ext/mri/cmodules/sparse.c +++ b/ext/mri/cmodules/sparse.c @@ -13,7 +13,9 @@ static VALUE arf_create_sparse_array(VALUE self, VALUE shape_array, VALUE values dim_t nRows = (dim_t)FIX2LONG(RARRAY_AREF(shape_array, 0)); dim_t nCols = (dim_t)FIX2LONG(RARRAY_AREF(shape_array, 1)); - af_create_sparse_array(&output->carray, nRows, nCols, values->carray, rowIdx->carray, colIdx->carray, stype); + af_err flag = af_create_sparse_array(&output->carray, nRows, nCols, values->carray, rowIdx->carray, colIdx->carray, stype); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); af_print_array(output->carray); @@ -32,7 +34,9 @@ static VALUE arf_create_sparse_array_from_dense(VALUE self, VALUE dense_val, VAL af_storage stype = arf_storage_type_from_rbsymbol(stype_val); - af_create_sparse_array_from_dense(&output->carray, dense->carray, stype); + af_err flag = af_create_sparse_array_from_dense(&output->carray, dense->carray, stype); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } @@ -45,7 +49,9 @@ static VALUE arf_sparse_convert_to(VALUE self, VALUE input_val, VALUE dest_stora af_storage dest_storage = arf_storage_type_from_rbsymbol(dest_storage_val); - af_sparse_convert_to(&output->carray, input->carray, dest_storage); + af_err flag = af_sparse_convert_to(&output->carray, input->carray, dest_storage); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } @@ -56,7 +62,9 @@ static VALUE arf_sparse_to_dense(VALUE self, VALUE sparse_array){ Data_Get_Struct(sparse_array, afstruct, sparse); - af_sparse_to_dense(&dense->carray, sparse->carray); + af_err flag = af_sparse_to_dense(&dense->carray, sparse->carray); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); return Data_Wrap_Struct(Af_Array, NULL, arf_free, dense); } @@ -73,7 +81,9 @@ static VALUE arf_sparse_get_info_func(VALUE self, VALUE values_val, VALUE rowIdx Data_Get_Struct(rowIdx_val, afstruct, rowIdx); Data_Get_Struct(colIdx_val, afstruct, colIdx); - af_sparse_get_info( &values->carray, &rowIdx->carray, &colIdx->carray, &stype, input->carray ); + af_err flag = af_sparse_get_info( &values->carray, &rowIdx->carray, &colIdx->carray, &stype, input->carray ); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); return rb_str_new_cstr(STORAGE_TYPES[stype]); } @@ -83,7 +93,10 @@ static VALUE arf_sparse_get_values(VALUE self, VALUE input_val){ afstruct* input; Data_Get_Struct(input_val, afstruct, input); - af_sparse_get_values(&output->carray, input->carray); + + af_err flag = af_sparse_get_values(&output->carray, input->carray); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } @@ -93,7 +106,10 @@ static VALUE arf_sparse_get_row_idx(VALUE self, VALUE input_val){ afstruct* input; Data_Get_Struct(input_val, afstruct, input); - af_sparse_get_row_idx(&output->carray, input->carray); + + af_err flag = af_sparse_get_row_idx(&output->carray, input->carray); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } @@ -103,7 +119,10 @@ static VALUE arf_sparse_get_col_idx(VALUE self, VALUE input_val){ afstruct* input; Data_Get_Struct(input_val, afstruct, input); - af_sparse_get_col_idx(&output->carray, input->carray); + + af_err flag = af_sparse_get_col_idx(&output->carray, input->carray); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } @@ -113,7 +132,10 @@ static VALUE arf_sparse_get_nnz(VALUE self, VALUE input_val){ afstruct* input; Data_Get_Struct(input_val, afstruct, input); - af_sparse_get_nnz( &out, input); + + af_err flag = af_sparse_get_nnz( &out, input); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); return ULL2NUM(out); } @@ -123,7 +145,10 @@ static VALUE arf_sparse_get_storage(VALUE self, VALUE input_val){ Data_Get_Struct(input_val, afstruct, input); af_storage storage; - af_sparse_get_storage(&storage , input->carray); + + af_err flag = af_sparse_get_storage(&storage , input->carray); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); return rb_str_new_cstr(STORAGE_TYPES[storage]); } diff --git a/ext/mri/cmodules/statistics.c b/ext/mri/cmodules/statistics.c index efd29b7..937c3d0 100644 --- a/ext/mri/cmodules/statistics.c +++ b/ext/mri/cmodules/statistics.c @@ -4,7 +4,9 @@ static VALUE arf_mean(VALUE self, VALUE array_val, VALUE dim_val){ Data_Get_Struct(array_val, afstruct, input); - af_mean(&output->carray, input->carray, NUM2UINT(dim_val)); + af_err flag = af_mean(&output->carray, input->carray, NUM2UINT(dim_val)); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); af_print_array(output->carray); return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); @@ -18,7 +20,9 @@ static VALUE arf_mean_weighted(VALUE self, VALUE array_val, VALUE weighted_array Data_Get_Struct(array_val, afstruct, input); Data_Get_Struct(weighted_array_val, afstruct, weighted_array); - af_mean_weighted(&output->carray, input->carray, weighted_array->carray, NUM2UINT(dim_val)); + af_err flag = af_mean_weighted(&output->carray, input->carray, weighted_array->carray, NUM2UINT(dim_val)); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); af_print_array(output->carray); return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); @@ -30,7 +34,9 @@ static VALUE arf_var(VALUE self, VALUE array_val, VALUE is_biased, VALUE dim_val Data_Get_Struct(array_val, afstruct, input); - af_var(&output->carray, input->carray, RTEST(is_biased), NUM2UINT(dim_val)); + af_err flag = af_var(&output->carray, input->carray, RTEST(is_biased), NUM2UINT(dim_val)); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); af_print_array(output->carray); return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); @@ -44,7 +50,9 @@ static VALUE arf_var_weighted(VALUE self, VALUE array_val, VALUE weighted_array_ Data_Get_Struct(array_val, afstruct, input); Data_Get_Struct(weighted_array_val, afstruct, weighted_array); - af_var_weighted(&output->carray, input->carray, weighted_array->carray, NUM2UINT(dim_val)); + af_err flag = af_var_weighted(&output->carray, input->carray, weighted_array->carray, NUM2UINT(dim_val)); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); af_print_array(output->carray); return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); @@ -56,7 +64,9 @@ static VALUE arf_stdev(VALUE self, VALUE array_val, VALUE dim_val){ Data_Get_Struct(array_val, afstruct, input); - af_stdev(&output->carray, input->carray, NUM2UINT(dim_val)); + af_err flag = af_stdev(&output->carray, input->carray, NUM2UINT(dim_val)); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); af_print_array(output->carray); return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); @@ -71,7 +81,9 @@ static VALUE arf_cov(VALUE self, VALUE first_array_val, VALUE second_array_val, Data_Get_Struct(first_array_val, afstruct, first_array); Data_Get_Struct(second_array_val, afstruct, second_array); - af_cov(&output->carray, first_array->carray, second_array->carray, RTEST(is_biased)); + af_err flag = af_cov(&output->carray, first_array->carray, second_array->carray, RTEST(is_biased)); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } @@ -82,8 +94,10 @@ static VALUE arf_median(VALUE self, VALUE array_val, VALUE dim_val){ Data_Get_Struct(array_val, afstruct, input); - af_median(&output->carray, input->carray, NUM2UINT(dim_val)); - af_print_array(output->carray); + af_err flag = af_median(&output->carray, input->carray, NUM2UINT(dim_val)); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); + af_print_array(output->carray); return Data_Wrap_Struct(Af_Array, NULL, arf_free, output); } @@ -94,7 +108,9 @@ static VALUE arf_mean_all(VALUE self, VALUE array_val){ Data_Get_Struct(array_val, afstruct, input); - af_mean_all(&real_part, &imag_part, input->carray); + af_err flag = af_mean_all(&real_part, &imag_part, input->carray); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); return DBL2NUM(real_part); } @@ -107,7 +123,9 @@ static VALUE arf_mean_all_weighted(VALUE self, VALUE array_val, VALUE weighted_a Data_Get_Struct(array_val, afstruct, input); Data_Get_Struct(weighted_array_val, afstruct, weighted_array); - af_mean_all_weighted(&real_part, &imag_part, input->carray, weighted_array->carray); + af_err flag = af_mean_all_weighted(&real_part, &imag_part, input->carray, weighted_array->carray); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); return DBL2NUM(real_part); } @@ -118,7 +136,9 @@ static VALUE arf_var_all(VALUE self, VALUE array_val, VALUE is_biased){ Data_Get_Struct(array_val, afstruct, input); - af_var_all(&real_part, &imag_part, input->carray, RTEST(is_biased)); + af_err flag = af_var_all(&real_part, &imag_part, input->carray, RTEST(is_biased)); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); return DBL2NUM(real_part); } @@ -131,7 +151,9 @@ static VALUE arf_var_all_weighted(VALUE self, VALUE array_val, VALUE weighted_ar Data_Get_Struct(array_val, afstruct, input); Data_Get_Struct(weighted_array_val, afstruct, weighted_array); - af_var_all_weighted(&real_part, &imag_part, input->carray, weighted_array->carray); + af_err flag = af_var_all_weighted(&real_part, &imag_part, input->carray, weighted_array->carray); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); return DBL2NUM(real_part); } @@ -142,7 +164,9 @@ static VALUE arf_stdev_all(VALUE self, VALUE array_val){ Data_Get_Struct(array_val, afstruct, input); - af_stdev_all(&real_part, &imag_part, input->carray); + af_err flag = af_stdev_all(&real_part, &imag_part, input->carray); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); return DBL2NUM(real_part); } @@ -153,7 +177,9 @@ static VALUE arf_median_all(VALUE self, VALUE array_val){ Data_Get_Struct(array_val, afstruct, input); - af_median_all(&real_part, &imag_part, input->carray); + af_err flag = af_median_all(&real_part, &imag_part, input->carray); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); return DBL2NUM(real_part); } @@ -166,7 +192,9 @@ static VALUE arf_corrcoef(VALUE self, VALUE first_array_val, VALUE second_array_ Data_Get_Struct(first_array_val, afstruct, first_array); Data_Get_Struct(first_array_val, afstruct, second_array); - af_corrcoef(&real_part, &imag_part, first_array->carray, second_array->carray); + af_err flag = af_corrcoef(&real_part, &imag_part, first_array->carray, second_array->carray); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); return DBL2NUM(real_part); } diff --git a/ext/mri/cmodules/util.c b/ext/mri/cmodules/util.c index 6c83b7b..5e4743d 100644 --- a/ext/mri/cmodules/util.c +++ b/ext/mri/cmodules/util.c @@ -3,7 +3,10 @@ static VALUE arf_print_array(VALUE self, VALUE input_val){ Data_Get_Struct(input_val, afstruct, input); - af_print_array(input->carray); + af_err flag = af_print_array(input->carray); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return Qtrue; } @@ -18,7 +21,11 @@ static VALUE arf_save_array(VALUE self, VALUE key_val, VALUE array_val, VALUE fn const char* key = StringValueCStr(key_val); const char* filename = StringValueCStr(fn_val); int index; - af_save_array (&index, key, input->carray, filename, RTEST(append)); + + af_err flag = af_save_array (&index, key, input->carray, filename, RTEST(append)); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return INT2NUM(index); } @@ -40,7 +47,10 @@ static VALUE arf_array_to_string(VALUE self, VALUE exp_val, VALUE array_val, VAL Data_Get_Struct(array_val, afstruct, input); const char* exp = StringValueCStr(exp_val); - af_array_to_string(&output, exp, input->carray, NUM2INT(precision), RTEST(transpose)); + + af_err flag = af_array_to_string(&output, exp, input->carray, NUM2INT(precision), RTEST(transpose)); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); return rb_str_new_cstr(output); } @@ -51,12 +61,16 @@ static VALUE arf_example_function(VALUE self){ static VALUE arf_get_version(VALUE self){ int major, minor, patch; - af_get_version(&major, &minor, &patch); + + af_err flag = af_get_version(&major, &minor, &patch); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); VALUE hash = rb_hash_new(); rb_hash_aset(hash, rb_str_new_cstr("major"), INT2NUM(major)); rb_hash_aset(hash, rb_str_new_cstr("minor"), INT2NUM(minor)); rb_hash_aset(hash, rb_str_new_cstr("patch"), INT2NUM(patch)); + return hash; } @@ -67,6 +81,10 @@ static VALUE arf_get_revision(VALUE self){ static VALUE arf_get_size_of(VALUE self, VALUE dtype_val){ size_t size; - af_get_size_of(&size, arf_dtype_from_rbsymbol(dtype_val)); + + af_err flag = af_get_size_of(&size, arf_dtype_from_rbsymbol(dtype_val)); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); + return ULL2NUM(size); } diff --git a/ext/mri/interfaces/nmatrix.c b/ext/mri/interfaces/nmatrix.c index 40df632..0b016dd 100644 --- a/ext/mri/interfaces/nmatrix.c +++ b/ext/mri/interfaces/nmatrix.c @@ -3,21 +3,27 @@ static VALUE arf_af_array_to_nmatrix(VALUE self) { Data_Get_Struct(self, afstruct, input); dim_t count; uint ndims; - af_get_numdims(&ndims, input->carray); + + af_err flag = af_get_numdims(&ndims, input->carray); + if (flag != AF_SUCCESS) arf_handle_exception(flag); dim_t* dims = ALLOC_N(dim_t, ndims); - af_get_dims(&dims[0], &dims[1], &dims[2], &dims[3], input->carray); + flag = af_get_dims(&dims[0], &dims[1], &dims[2], &dims[3], input->carray); + if (flag != AF_SUCCESS) arf_handle_exception(flag); size_t* shape = ALLOC_N(size_t, ndims); for (dim_t index = 0; index < ndims; index++){ shape[index] = (size_t)(dims[index]); } - af_get_elements(&count, input->carray); + flag = af_get_elements(&count, input->carray); + if (flag != AF_SUCCESS) arf_handle_exception(flag); double* elements = ALLOC_N(double, count); - af_get_data_ptr(elements, input->carray); + + flag = af_get_data_ptr(elements, input->carray); + if (flag != AF_SUCCESS) arf_handle_exception(flag); return rb_nmatrix_dense_create(nm::FLOAT64, shape, ndims, elements, (int)count); } @@ -52,7 +58,9 @@ afstruct* arf_nmatrix_to_af_array(VALUE nm) { shape[index] = (size_t)(nmat->shape[index]); } - af_create_array(&output->carray, nmat->elements, nmat->dim, shape, f64); + af_err flag = af_create_array(&output->carray, nmat->elements, nmat->dim, shape, f64); + + if (flag != AF_SUCCESS) arf_handle_exception(flag); return output; } From 18fcd082b704e5d4b4ae119bbbb5033e9872ccc2 Mon Sep 17 00:00:00 2001 From: prasun Date: Mon, 4 Sep 2017 14:50:27 +0530 Subject: [PATCH 125/125] correct test for sinh --- test/arith_test.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/test/arith_test.rb b/test/arith_test.rb index 4e512f0..e511262 100644 --- a/test/arith_test.rb +++ b/test/arith_test.rb @@ -6,7 +6,9 @@ def setup @a = ArrayFire::Af_Array.new 2, [2,2],[1,2,3,4] @b = ArrayFire::Af_Array.new 2, [2,2],[2,4,6,8] @elements = [10, -11, 48, 21, 65, 0, 1, -7, 112] + @elements_h = [1, -1, 0, 1, 1, 0.4, -0.7, 1, 0.9] @af_array = ArrayFire::Af_Array.new 2, [3,3], @elements + @af_array_h = ArrayFire::Af_Array.new 2, [3,3], @elements_h end def test_addition @@ -28,7 +30,7 @@ def test_division assert_equal c, @b / @a end - [:sin, :cos, :tan, :sinh, :cosh, :tanh].each do |method| + [:sin, :cos, :tan].each do |method| define_method("test_#{method}") do x = @elements.map{ |e| Math.send(method, e) } res_arr = ArrayFire::Af_Array.new 2, [3,3], x @@ -36,4 +38,12 @@ def test_division end end + [:sinh, :cosh, :tanh].each do |method| + define_method("test_#{method}") do + x = @elements_h.map{ |e| Math.send(method, e) } + res_arr = ArrayFire::Af_Array.new 2, [3,3], x + assert res_arr.approx_equal @af_array_h.send method + end + end + end