Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

RubyGrant 2020 related work #57

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions ext/accessors.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
VALUE nm_accessor_get(int argc, VALUE* argv, VALUE self){
nmatrix* nmat;
Data_Get_Struct(self, nmatrix, nmat);
TypedData_Get_Struct(self, nmatrix, &nm_data_type, nmat);

size_t* lower_indices = ALLOC_N(size_t, nmat->ndims);
size_t* upper_indices = ALLOC_N(size_t, nmat->ndims);
Expand All @@ -24,7 +24,7 @@ VALUE nm_accessor_get(int argc, VALUE* argv, VALUE self){

get_slice(nmat, lower_indices, upper_indices, slice);

return Data_Wrap_Struct(NMatrix, NULL, nm_free, slice);
return TypedData_Wrap_Struct(NMatrix, &nm_data_type, slice);

//return a slice
}
Expand All @@ -48,7 +48,7 @@ VALUE nm_accessor_get(int argc, VALUE* argv, VALUE self){

get_slice(nmat, lower_indices, upper_indices, slice);

return Data_Wrap_Struct(NMatrix, NULL, nm_free, slice);
return TypedData_Wrap_Struct(NMatrix, &nm_data_type, slice);

//return a slice
}
Expand All @@ -66,13 +66,13 @@ VALUE nm_accessor_get(int argc, VALUE* argv, VALUE self){
{
if(is_slice(nmat, argv)){

nmatrix* slice = ALLOC(nmatrix);
nmatrix_buffer* slice = ALLOC(nmatrix_buffer);
slice->dtype = nmat->dtype;
slice->stype = nmat->stype;
slice->mat = nmat;

get_slice(nmat, lower_indices, upper_indices, slice);

return Data_Wrap_Struct(NMatrix, NULL, nm_free, slice);
return TypedData_Wrap_Struct(NMatrix, &nm_buffer_data_type, slice);

//return a slice
}
Expand All @@ -96,7 +96,7 @@ VALUE nm_accessor_get(int argc, VALUE* argv, VALUE self){

get_slice(nmat, lower_indices, upper_indices, slice);

return Data_Wrap_Struct(NMatrix, NULL, nm_free, slice);
return TypedData_Wrap_Struct(NMatrix, &nm_data_type, slice);

//return a slice
}
Expand All @@ -120,7 +120,7 @@ VALUE nm_accessor_get(int argc, VALUE* argv, VALUE self){

get_slice(nmat, lower_indices, upper_indices, slice);

return Data_Wrap_Struct(NMatrix, NULL, nm_free, slice);
return TypedData_Wrap_Struct(NMatrix, &nm_data_type, slice);

//return a slice
}
Expand All @@ -144,7 +144,7 @@ VALUE nm_accessor_get(int argc, VALUE* argv, VALUE self){

get_slice(nmat, lower_indices, upper_indices, slice);

return Data_Wrap_Struct(NMatrix, NULL, nm_free, slice);
return TypedData_Wrap_Struct(NMatrix, &nm_data_type, slice);

//return a slice
}
Expand Down Expand Up @@ -193,7 +193,7 @@ VALUE nm_accessor_get(int argc, VALUE* argv, VALUE self){
*/
VALUE nm_accessor_set(int argc, VALUE* argv, VALUE self){
nmatrix* nmat;
Data_Get_Struct(self, nmatrix, nmat);
TypedData_Get_Struct(self, nmatrix, &nm_data_type, nmat);

size_t index = get_index(nmat, argv);

Expand Down
8 changes: 4 additions & 4 deletions ext/blas.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
VALUE nm_dot(VALUE self, VALUE another){
nmatrix* left;
nmatrix* right;
Data_Get_Struct(self, nmatrix, left);
Data_Get_Struct(another, nmatrix, right);
TypedData_Get_Struct(self, nmatrix, &nm_data_type, left);
TypedData_Get_Struct(another, nmatrix, &nm_data_type, right);

nmatrix* result = ALLOC(nmatrix);
result->dtype = left->dtype;
Expand Down Expand Up @@ -67,7 +67,7 @@ VALUE nm_dot(VALUE self, VALUE another){
}
}

return Data_Wrap_Struct(NMatrix, NULL, nm_free, result);
return TypedData_Wrap_Struct(NMatrix, &nm_data_type, result);
}

/*
Expand All @@ -79,7 +79,7 @@ VALUE nm_dot(VALUE self, VALUE another){
*/
VALUE nm_norm2(VALUE self){
nmatrix* matrix;
Data_Get_Struct(self, nmatrix, matrix);
TypedData_Get_Struct(self, nmatrix, &nm_data_type, matrix);
//check mat is vector
VALUE val = Qnil;

Expand Down
4 changes: 2 additions & 2 deletions ext/broadcasting.c
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ void broadcast_matrices(nmatrix* nmat1, nmatrix* nmat2) {
*/
VALUE nm_broadcast_to(int argc, VALUE* argv) {
nmatrix* nmat;
Data_Get_Struct(argv[0], nmatrix, nmat);
TypedData_Get_Struct(argv[0], nmatrix, &nm_data_type, nmat);

size_t new_ndims = (size_t)RARRAY_LEN(argv[1]);

Expand All @@ -342,7 +342,7 @@ VALUE nm_broadcast_to(int argc, VALUE* argv) {

broadcast_matrix(nmat, new_shape, new_ndims);

return Data_Wrap_Struct(NMatrix, NULL, nm_free, nmat);
return TypedData_Wrap_Struct(NMatrix, &nm_data_type, nmat);
}

/*
Expand Down
20 changes: 10 additions & 10 deletions ext/comparison.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
VALUE nm_eqeq(VALUE self, VALUE another){
nmatrix* left;
nmatrix* right;
Data_Get_Struct(self, nmatrix, left);
Data_Get_Struct(another, nmatrix, right);
TypedData_Get_Struct(self, nmatrix, &nm_data_type, left);
TypedData_Get_Struct(another, nmatrix, &nm_data_type, right);

if(left->count != right->count){
return Qfalse;
Expand Down Expand Up @@ -99,7 +99,7 @@ VALUE nm_eqeq(VALUE self, VALUE another){
*/
VALUE nm_gt(VALUE self, VALUE another){
nmatrix* left;
Data_Get_Struct(self, nmatrix, left);
TypedData_Get_Struct(self, nmatrix, &nm_data_type, left);

nmatrix* result = ALLOC(nmatrix);
result->dtype = nm_bool;
Expand Down Expand Up @@ -172,7 +172,7 @@ VALUE nm_gt(VALUE self, VALUE another){
}
}
result->elements = result_elements;
return Data_Wrap_Struct(NMatrix, NULL, nm_free, result);
return TypedData_Wrap_Struct(NMatrix, &nm_data_type, result);
}

/*
Expand All @@ -183,7 +183,7 @@ VALUE nm_gt(VALUE self, VALUE another){
*/
VALUE nm_gteq(VALUE self, VALUE another){
nmatrix* left;
Data_Get_Struct(self, nmatrix, left);
TypedData_Get_Struct(self, nmatrix, &nm_data_type, left);

nmatrix* result = ALLOC(nmatrix);
result->dtype = nm_bool;
Expand Down Expand Up @@ -256,7 +256,7 @@ VALUE nm_gteq(VALUE self, VALUE another){
}
}
result->elements = result_elements;
return Data_Wrap_Struct(NMatrix, NULL, nm_free, result);
return TypedData_Wrap_Struct(NMatrix, &nm_data_type, result);
}

/*
Expand All @@ -267,7 +267,7 @@ VALUE nm_gteq(VALUE self, VALUE another){
*/
VALUE nm_lt(VALUE self, VALUE another){
nmatrix* left;
Data_Get_Struct(self, nmatrix, left);
TypedData_Get_Struct(self, nmatrix, &nm_data_type, left);

nmatrix* result = ALLOC(nmatrix);
result->dtype = nm_bool;
Expand Down Expand Up @@ -340,7 +340,7 @@ VALUE nm_lt(VALUE self, VALUE another){
}
}
result->elements = result_elements;
return Data_Wrap_Struct(NMatrix, NULL, nm_free, result);
return TypedData_Wrap_Struct(NMatrix, &nm_data_type, result);
}


Expand All @@ -352,7 +352,7 @@ VALUE nm_lt(VALUE self, VALUE another){
*/
VALUE nm_lteq(VALUE self, VALUE another){
nmatrix* left;
Data_Get_Struct(self, nmatrix, left);
TypedData_Get_Struct(self, nmatrix, &nm_data_type, left);

nmatrix* result = ALLOC(nmatrix);
result->dtype = nm_bool;
Expand Down Expand Up @@ -425,5 +425,5 @@ VALUE nm_lteq(VALUE self, VALUE another){
}
}
result->elements = result_elements;
return Data_Wrap_Struct(NMatrix, NULL, nm_free, result);
return TypedData_Wrap_Struct(NMatrix, &nm_data_type, result);
}
50 changes: 35 additions & 15 deletions ext/elementwise.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
#define DEF_ELEMENTWISE_RUBY_ACCESSOR(name, oper) \
VALUE nm_##name(VALUE self, VALUE another){ \
nmatrix* left; \
Data_Get_Struct(self, nmatrix, left); \
TypedData_Get_Struct(self, nmatrix, &nm_data_type, left); \
\
nmatrix* right; \
nmatrix* result = ALLOC(nmatrix); \
\
nmatrix* left_copy; \
nmatrix* right_copy; \
if(rb_obj_is_kind_of(another, NMatrix) == Qtrue) {\
Data_Get_Struct(another, nmatrix, right); \
TypedData_Get_Struct(another, nmatrix, &nm_data_type, right); \
left_copy = matrix_copy(left); \
right_copy = matrix_copy(right); \
broadcast_matrices(left_copy, right_copy); \
Expand Down Expand Up @@ -44,7 +44,7 @@ VALUE nm_##name(VALUE self, VALUE another){ \
case nm_bool: \
{ \
bool* result_elements = ALLOC_N(bool, result->count); \
if(RB_TYPE_P(another, T_TRUE) || RB_TYPE_P(another, T_FALSE)){ \
if(rb_obj_is_kind_of(another, NMatrix) == Qfalse){ \
bool* left_elements = (bool*)left->elements; \
for(size_t index = 0; index < left->count; index++){ \
result_elements[index] = (left_elements[index]) oper (another ? Qtrue : Qfalse); \
Expand All @@ -64,7 +64,7 @@ VALUE nm_##name(VALUE self, VALUE another){ \
case nm_int: \
{ \
int* result_elements = ALLOC_N(int, result->count); \
if(RB_TYPE_P(another, T_FLOAT) || RB_TYPE_P(another, T_FIXNUM)){ \
if(rb_obj_is_kind_of(another, NMatrix) == Qfalse){ \
int* left_elements = (int*)left->elements; \
for(size_t index = 0; index < left->count; index++){ \
result_elements[index] = (left_elements[index]) oper (NUM2DBL(another)); \
Expand All @@ -84,7 +84,7 @@ VALUE nm_##name(VALUE self, VALUE another){ \
case nm_float32: \
{ \
float* result_elements = ALLOC_N(float, result->count); \
if(RB_TYPE_P(another, T_FLOAT) || RB_TYPE_P(another, T_FIXNUM)){ \
if(rb_obj_is_kind_of(another, NMatrix) == Qfalse){ \
float* left_elements = (float*)left->elements; \
for(size_t index = 0; index < left->count; index++){ \
result_elements[index] = (left_elements[index]) oper (NUM2DBL(another)); \
Expand All @@ -104,7 +104,7 @@ VALUE nm_##name(VALUE self, VALUE another){ \
case nm_float64: \
{ \
double* result_elements = ALLOC_N(double, result->count); \
if(RB_TYPE_P(another, T_FLOAT) || RB_TYPE_P(another, T_FIXNUM)){ \
if(rb_obj_is_kind_of(another, NMatrix) == Qfalse){ \
double* left_elements = (double*)left->elements; \
for(size_t index = 0; index < left->count; index++){ \
result_elements[index] = (left_elements[index]) oper (NUM2DBL(another)); \
Expand All @@ -124,7 +124,7 @@ VALUE nm_##name(VALUE self, VALUE another){ \
case nm_complex32: \
{ \
complex float* result_elements = ALLOC_N(complex float, result->count); \
if(RB_TYPE_P(another, T_FLOAT) || RB_TYPE_P(another, T_FIXNUM)){ \
if(rb_obj_is_kind_of(another, NMatrix) == Qfalse){ \
complex float* left_elements = (complex float*)left->elements; \
for(size_t index = 0; index < left->count; index++){ \
result_elements[index] = (left_elements[index]) oper (NUM2DBL(another)); \
Expand All @@ -144,7 +144,7 @@ VALUE nm_##name(VALUE self, VALUE another){ \
case nm_complex64: \
{ \
complex double* result_elements = ALLOC_N(complex double, result->count); \
if(RB_TYPE_P(another, T_FLOAT) || RB_TYPE_P(another, T_FIXNUM)){ \
if(rb_obj_is_kind_of(another, NMatrix) == Qfalse){ \
complex double* left_elements = (complex double*)left->elements; \
for(size_t index = 0; index < left->count; index++){ \
result_elements[index] = (left_elements[index]) oper (NUM2DBL(another)); \
Expand All @@ -161,8 +161,28 @@ VALUE nm_##name(VALUE self, VALUE another){ \
result->elements = result_elements; \
break; \
} \
default: \
{ \
double* result_elements = ALLOC_N(double, result->count); \
if(rb_obj_is_kind_of(another, NMatrix) == Qfalse){ \
double* left_elements = (double*)left->elements; \
for(size_t index = 0; index < left->count; index++){ \
result_elements[index] = (left_elements[index]) oper (NUM2DBL(another)); \
} \
} \
else{ \
double* left_elements = (double*)left_copy->elements; \
double* right_elements = (double*)right_copy->elements; \
\
for(size_t index = 0; index < left_copy->count; index++){ \
result_elements[index] = (left_elements[index]) oper (right_elements[index]); \
} \
} \
result->elements = result_elements; \
break; \
} \
} \
return Data_Wrap_Struct(NMatrix, NULL, nm_free, result); \
return TypedData_Wrap_Struct(NMatrix, &nm_data_type, result); \
}

DEF_ELEMENTWISE_RUBY_ACCESSOR(add, +)
Expand All @@ -180,7 +200,7 @@ DEF_ELEMENTWISE_RUBY_ACCESSOR(divide, /)

VALUE nm_sin(VALUE self){
nmatrix* input;
Data_Get_Struct(self, nmatrix, input);
TypedData_Get_Struct(self, nmatrix, &nm_data_type, input);

nmatrix* result = ALLOC(nmatrix);
result->dtype = input->dtype;
Expand Down Expand Up @@ -256,13 +276,13 @@ VALUE nm_sin(VALUE self){
}
}

return Data_Wrap_Struct(NMatrix, NULL, nm_free, result);
return TypedData_Wrap_Struct(NMatrix, &nm_data_type, result);
}

#define DEF_UNARY_RUBY_ACCESSOR(oper, name) \
static VALUE nm_##name(VALUE self) { \
nmatrix* input; \
Data_Get_Struct(self, nmatrix, input); \
TypedData_Get_Struct(self, nmatrix, &nm_data_type, input); \
\
nmatrix* result = ALLOC(nmatrix); \
result->dtype = input->dtype; \
Expand Down Expand Up @@ -336,7 +356,7 @@ static VALUE nm_##name(VALUE self) { \
break; \
} \
} \
return Data_Wrap_Struct(NMatrix, NULL, nm_free, result); \
return TypedData_Wrap_Struct(NMatrix, &nm_data_type, result); \
}

DEF_UNARY_RUBY_ACCESSOR(cos, cos)
Expand All @@ -357,7 +377,7 @@ DEF_UNARY_RUBY_ACCESSOR(sqrt, sqrt)
#define DEF_UNARY_RUBY_ACCESSOR_NON_COMPLEX(oper, name) \
static VALUE nm_##name(VALUE self) { \
nmatrix* input; \
Data_Get_Struct(self, nmatrix, input); \
TypedData_Get_Struct(self, nmatrix, &nm_data_type, input); \
\
nmatrix* result = ALLOC(nmatrix); \
result->dtype = input->dtype; \
Expand Down Expand Up @@ -419,7 +439,7 @@ static VALUE nm_##name(VALUE self) { \
/* Not supported message */ \
} \
} \
return Data_Wrap_Struct(NMatrix, NULL, nm_free, result); \
return TypedData_Wrap_Struct(NMatrix, &nm_data_type, result); \
}

DEF_UNARY_RUBY_ACCESSOR_NON_COMPLEX(log2, log2)
Expand Down
8 changes: 4 additions & 4 deletions ext/iteration.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
VALUE nm_each(VALUE self) {
nmatrix* input;
Data_Get_Struct(self, nmatrix, input);
TypedData_Get_Struct(self, nmatrix, &nm_data_type, input);

switch(input->stype){
case nm_dense:
Expand Down Expand Up @@ -78,7 +78,7 @@ VALUE nm_each(VALUE self) {

VALUE nm_each_with_indices(VALUE self) {
nmatrix* input;
Data_Get_Struct(self, nmatrix, input);
TypedData_Get_Struct(self, nmatrix, &nm_data_type, input);

VALUE* shape_array = ALLOC_N(VALUE, input->ndims);
for (size_t index = 0; index < input->ndims; index++){
Expand Down Expand Up @@ -217,7 +217,7 @@ VALUE nm_map_stored(VALUE self) {

VALUE nm_each_rank(VALUE self, VALUE dimension_idx) {
nmatrix* input;
Data_Get_Struct(self, nmatrix, input);
TypedData_Get_Struct(self, nmatrix, &nm_data_type, input);

size_t dim_idx = NUM2SIZET(dimension_idx);

Expand Down Expand Up @@ -249,7 +249,7 @@ VALUE nm_each_rank(VALUE self, VALUE dimension_idx) {

get_slice(input, lower_indices, upper_indices, result);

rb_yield(Data_Wrap_Struct(NMatrix, NULL, nm_free, result));
rb_yield(TypedData_Wrap_Struct(NMatrix, &nm_data_type, result));
}

return self;
Expand Down
Loading