ext/cumo/narray/narray.c in cumo-0.1.0 vs ext/cumo/narray/narray.c in cumo-0.1.1

- old
+ new

@@ -7,107 +7,107 @@ #include "cumo/cuda/runtime.h" /* global variables within this module */ VALUE cumo_cNArray; VALUE rb_mCumo; -VALUE nary_eCastError; -VALUE nary_eShapeError; -VALUE nary_eOperationError; -VALUE nary_eDimensionError; -VALUE nary_eValueError; +VALUE cumo_na_eCastError; +VALUE cumo_na_eShapeError; +VALUE cumo_na_eOperationError; +VALUE cumo_na_eDimensionError; +VALUE cumo_na_eValueError; -static ID id_contiguous_stride; -static ID id_allocate; -static ID id_element_byte_size; -static ID id_fill; -static ID id_seq; -static ID id_logseq; -static ID id_eye; -static ID id_UPCAST; -static ID id_cast; -static ID id_dup; -static ID id_to_host; -static ID id_bracket; -static ID id_shift_left; -static ID id_eq; -static ID id_count_false; -static ID id_count_false_cpu; -static ID id_axis; -static ID id_nan; -static ID id_keepdims; +static ID cumo_id_contiguous_stride; +static ID cumo_id_allocate; +static ID cumo_id_element_byte_size; +static ID cumo_id_fill; +static ID cumo_id_seq; +static ID cumo_id_logseq; +static ID cumo_id_eye; +static ID cumo_id_UPCAST; +static ID cumo_id_cast; +static ID cumo_id_dup; +static ID cumo_id_to_host; +static ID cumo_id_bracket; +static ID cumo_id_shift_left; +static ID cumo_id_eq; +static ID cumo_id_count_false; +static ID cumo_id_count_false_cpu; +static ID cumo_id_axis; +static ID cumo_id_nan; +static ID cumo_id_keepdims; -VALUE sym_reduce; -VALUE sym_option; -VALUE sym_loop_opt; -VALUE sym_init; +VALUE cumo_sym_reduce; +VALUE cumo_sym_option; +VALUE cumo_sym_loop_opt; +VALUE cumo_sym_init; -VALUE na_cStep; +VALUE cumo_na_cStep; #ifndef HAVE_RB_CCOMPLEX VALUE rb_cComplex; #endif -int cumo_na_inspect_rows=20; -int cumo_na_inspect_cols=80; +int cumo_na_inspect_rows_=20; +int cumo_na_inspect_cols_=80; -const rb_data_type_t na_data_type = { +const rb_data_type_t cumo_na_data_type = { "Cumo::NArray", {0, 0, 0,}, 0, 0, 0, }; static void -nary_debug_info_nadata(VALUE self) +cumo_na_debug_info_nadata(VALUE self) { - narray_data_t *na; - GetNArrayData(self,na); + cumo_narray_data_t *na; + CumoGetNArrayData(self,na); printf(" ptr = 0x%"SZF"x\n", (size_t)(na->ptr)); } static VALUE -nary_debug_info_naview(VALUE self) +cumo_na_debug_info_naview(VALUE self) { int i; - narray_view_t *na; + cumo_narray_view_t *na; size_t *idx; size_t j; - GetNArrayView(self,na); + CumoGetNArrayView(self,na); printf(" data = 0x%"SZF"x\n", (size_t)na->data); printf(" offset = %"SZF"d\n", (size_t)na->offset); printf(" stridx = 0x%"SZF"x\n", (size_t)na->stridx); if (na->stridx) { printf(" stridx = ["); for (i=0; i<na->base.ndim; i++) { - if (SDX_IS_INDEX(na->stridx[i])) { + if (CUMO_SDX_IS_INDEX(na->stridx[i])) { - idx = SDX_GET_INDEX(na->stridx[i]); + idx = CUMO_SDX_GET_INDEX(na->stridx[i]); printf(" index[%d]=[", i); for (j=0; j<na->base.shape[i]; j++) { printf(" %"SZF"d", idx[j]); } printf(" ] "); } else { - printf(" %"SZF"d", SDX_GET_STRIDE(na->stridx[i])); + printf(" %"SZF"d", CUMO_SDX_GET_STRIDE(na->stridx[i])); } } printf(" ]\n"); } return Qnil; } VALUE -nary_debug_info(VALUE self) +cumo_na_debug_info(VALUE self) { int i; - narray_t *na; - GetNArray(self,na); + cumo_narray_t *na; + CumoGetNArray(self,na); - printf("%s:\n",rb_class2name(CLASS_OF(self))); + printf("%s:\n",rb_class2name(rb_obj_class(self))); printf(" id = 0x%"PRI_VALUE_PREFIX"x\n", self); printf(" type = %d\n", na->type); printf(" flag = [%d,%d]\n", na->flag[0], na->flag[1]); printf(" size = %"SZF"d\n", na->size); printf(" ndim = %d\n", na->ndim); @@ -118,59 +118,59 @@ printf(" %"SZF"d", na->shape[i]); printf(" ]\n"); } switch(na->type) { - case NARRAY_DATA_T: - case NARRAY_FILEMAP_T: - nary_debug_info_nadata(self); + case CUMO_NARRAY_DATA_T: + case CUMO_NARRAY_FILEMAP_T: + cumo_na_debug_info_nadata(self); break; - case NARRAY_VIEW_T: - nary_debug_info_naview(self); + case CUMO_NARRAY_VIEW_T: + cumo_na_debug_info_naview(self); break; } return Qnil; } static size_t -na_view_memsize(const void* ptr) +cumo_na_view_memsize(const void* ptr) { int i; - size_t size = sizeof(narray_view_t); - const narray_view_t *na = ptr; + size_t size = sizeof(cumo_narray_view_t); + const cumo_narray_view_t *na = ptr; - assert(na->base.type == NARRAY_VIEW_T); + assert(na->base.type == CUMO_NARRAY_VIEW_T); if (na->stridx != NULL) { for (i=0; i<na->base.ndim; i++) { - if (SDX_IS_INDEX(na->stridx[i])) { + if (CUMO_SDX_IS_INDEX(na->stridx[i])) { size += sizeof(size_t) * na->base.shape[i]; } } - size += sizeof(stridx_t) * na->base.ndim; + size += sizeof(cumo_stridx_t) * na->base.ndim; } if (na->base.size > 0) { if (na->base.shape != NULL && na->base.shape != &(na->base.size)) { size += sizeof(size_t) * na->base.ndim; } } return size; } static void -na_view_free(void* ptr) +cumo_na_view_free(void* ptr) { int i; - narray_view_t *na = (narray_view_t*)ptr; + cumo_narray_view_t *na = (cumo_narray_view_t*)ptr; - assert(na->base.type == NARRAY_VIEW_T); + assert(na->base.type == CUMO_NARRAY_VIEW_T); if (na->stridx != NULL) { for (i=0; i<na->base.ndim; i++) { - if (SDX_IS_INDEX(na->stridx[i])) { - void *p = SDX_GET_INDEX(na->stridx[i]); + if (CUMO_SDX_IS_INDEX(na->stridx[i])) { + void *p = CUMO_SDX_GET_INDEX(na->stridx[i]); if (cumo_cuda_runtime_is_device_memory(p)) { cumo_cuda_runtime_free(p); } else { xfree(p); } @@ -187,56 +187,56 @@ } xfree(na); } static void -na_view_gc_mark(void* na) +cumo_na_view_gc_mark(void* na) { - if (((narray_t*)na)->type == NARRAY_VIEW_T) { - rb_gc_mark(((narray_view_t*)na)->data); + if (((cumo_narray_t*)na)->type == CUMO_NARRAY_VIEW_T) { + rb_gc_mark(((cumo_narray_view_t*)na)->data); } } -const rb_data_type_t na_data_type_view = { +const rb_data_type_t cumo_na_data_type_view = { "Cumo::NArrayView", - {na_view_gc_mark, na_view_free, na_view_memsize,}, - &na_data_type, 0, 0, + {cumo_na_view_gc_mark, cumo_na_view_free, cumo_na_view_memsize,}, + &cumo_na_data_type, 0, 0, }; VALUE -na_s_allocate_view(VALUE klass) +cumo_na_s_allocate_view(VALUE klass) { - narray_view_t *na = ALLOC(narray_view_t); + cumo_narray_view_t *na = ALLOC(cumo_narray_view_t); na->base.ndim = 0; - na->base.type = NARRAY_VIEW_T; - na->base.flag[0] = NA_FL0_INIT; - na->base.flag[1] = NA_FL1_INIT; + na->base.type = CUMO_NARRAY_VIEW_T; + na->base.flag[0] = CUMO_NA_FL0_INIT; + na->base.flag[1] = CUMO_NA_FL1_INIT; na->base.size = 0; na->base.shape = NULL; na->base.reduce = INT2FIX(0); na->data = Qnil; na->offset = 0; na->stridx = NULL; - return TypedData_Wrap_Struct(klass, &na_data_type_view, (void*)na); + return TypedData_Wrap_Struct(klass, &cumo_na_data_type_view, (void*)na); } //static const size_t zero=0; void -na_array_to_internal_shape(VALUE self, VALUE ary, size_t *shape) +cumo_na_array_to_internal_shape(VALUE self, VALUE ary, size_t *shape) { size_t i, n, c, s; ssize_t x; VALUE v; int flag = 0; n = RARRAY_LEN(ary); if (RTEST(self)) { - flag = TEST_COLUMN_MAJOR(self); + flag = CUMO_TEST_COLUMN_MAJOR(self); } if (flag) { c = n-1; s = -1; } else { @@ -255,37 +255,37 @@ } void -na_alloc_shape(narray_t *na, int ndim) +cumo_na_alloc_shape(cumo_narray_t *na, int ndim) { na->ndim = ndim; na->size = 0; switch(ndim) { case 0: case 1: na->shape = &(na->size); break; default: if (ndim < 0) { - rb_raise(nary_eDimensionError,"ndim=%d is negative", ndim); + rb_raise(cumo_na_eDimensionError,"ndim=%d is negative", ndim); } - if (ndim > NA_MAX_DIMENSION) { - rb_raise(nary_eDimensionError,"ndim=%d is too many", ndim); + if (ndim > CUMO_NA_MAX_DIMENSION) { + rb_raise(cumo_na_eDimensionError,"ndim=%d is too many", ndim); } na->shape = ALLOC_N(size_t, ndim); } } void -na_setup_shape(narray_t *na, int ndim, size_t *shape) +cumo_na_setup_shape(cumo_narray_t *na, int ndim, size_t *shape) { int i; size_t size; - na_alloc_shape(na, ndim); + cumo_na_alloc_shape(na, ndim); if (ndim==0) { na->size = 1; } else if (ndim==1) { @@ -299,15 +299,15 @@ na->size = size; } } static void -na_setup(VALUE self, int ndim, size_t *shape) +cumo_na_setup(VALUE self, int ndim, size_t *shape) { - narray_t *na; - GetNArray(self,na); - na_setup_shape(na, ndim, shape); + cumo_narray_t *na; + CumoGetNArray(self,na); + cumo_na_setup_shape(na, ndim, shape); } /* @overload initialize(shape) @@ -341,11 +341,11 @@ #=> in `new': allocator undefined for Cumo::NArray (TypeError) # from t.rb:9:in `<main>' */ static VALUE -na_initialize(VALUE self, VALUE args) +cumo_na_initialize(VALUE self, VALUE args) { VALUE v; size_t *shape=NULL; int ndim; @@ -356,40 +356,40 @@ } } else { v = args; } ndim = RARRAY_LEN(v); - if (ndim > NA_MAX_DIMENSION) { + if (ndim > CUMO_NA_MAX_DIMENSION) { rb_raise(rb_eArgError,"ndim=%d exceeds maximum dimension",ndim); } shape = ALLOCA_N(size_t, ndim); // setup size_t shape[] from VALUE shape argument - na_array_to_internal_shape(self, v, shape); - na_setup(self, ndim, shape); + cumo_na_array_to_internal_shape(self, v, shape); + cumo_na_setup(self, ndim, shape); return self; } VALUE -nary_new(VALUE klass, int ndim, size_t *shape) +cumo_na_new(VALUE klass, int ndim, size_t *shape) { volatile VALUE obj; - obj = rb_funcall(klass, id_allocate, 0); - na_setup(obj, ndim, shape); + obj = rb_funcall(klass, cumo_id_allocate, 0); + cumo_na_setup(obj, ndim, shape); return obj; } VALUE -nary_view_new(VALUE klass, int ndim, size_t *shape) +cumo_na_view_new(VALUE klass, int ndim, size_t *shape) { volatile VALUE obj; - obj = na_s_allocate_view(klass); - na_setup(obj, ndim, shape); + obj = cumo_na_s_allocate_view(klass); + cumo_na_setup(obj, ndim, shape); return obj; } /* @@ -398,18 +398,18 @@ @overload initialize_copy(other) @param [Cumo::NArray] other @return [Cumo::NArray] self */ static VALUE -na_initialize_copy(VALUE self, VALUE orig) +cumo_na_initialize_copy(VALUE self, VALUE orig) { - narray_t *na; - GetNArray(orig,na); + cumo_narray_t *na; + CumoGetNArray(orig,na); - na_setup(self,NA_NDIM(na),NA_SHAPE(na)); - na_store(self,orig); - na_copy_flags(orig,self); + cumo_na_setup(self,CUMO_NA_NDIM(na),CUMO_NA_SHAPE(na)); + cumo_na_store(self,orig); + cumo_na_copy_flags(orig,self); return self; } /* @@ -426,15 +426,15 @@ * [[0, 0, 0, 0, 0], * [0, 0, 0, 0, 0], * [0, 0, 0, 0, 0]] */ static VALUE -na_s_zeros(int argc, VALUE *argv, VALUE klass) +cumo_na_s_zeros(int argc, VALUE *argv, VALUE klass) { VALUE obj; obj = rb_class_new_instance(argc, argv, klass); - return rb_funcall(obj, id_fill, 1, INT2FIX(0)); + return rb_funcall(obj, cumo_id_fill, 1, INT2FIX(0)); } /* * call-seq: @@ -450,15 +450,15 @@ * [[1, 1, 1, 1, 1], * [1, 1, 1, 1, 1], * [1, 1, 1, 1, 1]] */ static VALUE -na_s_ones(int argc, VALUE *argv, VALUE klass) +cumo_na_s_ones(int argc, VALUE *argv, VALUE klass) { VALUE obj; obj = rb_class_new_instance(argc, argv, klass); - return rb_funcall(obj, id_fill, 1, INT2FIX(1)); + return rb_funcall(obj, cumo_id_fill, 1, INT2FIX(1)); } /* Returns an array of N linearly spaced points between x1 and x2. @@ -475,11 +475,11 @@ a = Cumo::DFloat.linspace(-5,5,7) => Cumo::DFloat#shape=[7] [-5, -3.33333, -1.66667, 0, 1.66667, 3.33333, 5] */ static VALUE -na_s_linspace(int argc, VALUE *argv, VALUE klass) +cumo_na_s_linspace(int argc, VALUE *argv, VALUE klass) { VALUE obj, vx1, vx2, vstep, vsize; double n; int narg; @@ -493,11 +493,11 @@ obj = rb_funcall(vx2, '-', 1, vx1); vstep = rb_funcall(obj, '/', 1, DBL2NUM(n-1)); obj = rb_class_new_instance(1, &vsize, klass); - return rb_funcall(obj, id_seq, 2, vx1, vstep); + return rb_funcall(obj, cumo_id_seq, 2, vx1, vstep); } /* Returns an array of N logarithmically spaced points between 10^a and 10^b. This singleton method is valid not for NArray having +logseq+ method, @@ -517,11 +517,11 @@ Cumo::DComplex.logspace(0,1i*Math::PI,5,Math::E) => Cumo::DComplex#shape=[5] [1+4.44659e-323i, 0.707107+0.707107i, 6.12323e-17+1i, -0.707107+0.707107i, ...] */ static VALUE -na_s_logspace(int argc, VALUE *argv, VALUE klass) +cumo_na_s_logspace(int argc, VALUE *argv, VALUE klass) { VALUE obj, vx1, vx2, vstep, vsize, vbase; double n; rb_scan_args(argc,argv,"22",&vx1,&vx2,&vsize,&vbase); @@ -537,11 +537,11 @@ obj = rb_funcall(vx2, '-', 1, vx1); vstep = rb_funcall(obj, '/', 1, DBL2NUM(n-1)); obj = rb_class_new_instance(1, &vsize, klass); - return rb_funcall(obj, id_logseq, 3, vx1, vstep, vbase); + return rb_funcall(obj, cumo_id_logseq, 3, vx1, vstep, vbase); } /* Returns a NArray with shape=(n,n) whose diagonal elements are 1, otherwise 0. @@ -554,11 +554,11 @@ [[1, 0, 0], [0, 1, 0], [0, 0, 1]] */ static VALUE -na_s_eye(int argc, VALUE *argv, VALUE klass) +cumo_na_s_eye(int argc, VALUE *argv, VALUE klass) { VALUE obj; VALUE tmp[2]; if (argc==0) { @@ -568,157 +568,157 @@ tmp[0] = tmp[1] = argv[0]; argv = tmp; argc = 2; } obj = rb_class_new_instance(argc, argv, klass); - return rb_funcall(obj, id_eye, 0); + return rb_funcall(obj, cumo_id_eye, 0); } #define READ 1 #define WRITE 2 static char * -na_get_pointer_for_rw(VALUE self, int flag) +cumo_na_get_pointer_for_rw(VALUE self, int flag) { char *ptr; VALUE obj; - narray_t *na; + cumo_narray_t *na; if ((flag & WRITE) && OBJ_FROZEN(self)) { rb_raise(rb_eRuntimeError, "cannot write to frozen NArray."); } - GetNArray(self,na); + CumoGetNArray(self,na); - switch(NA_TYPE(na)) { - case NARRAY_DATA_T: - ptr = NA_DATA_PTR(na); - if (NA_SIZE(na) > 0 && ptr == NULL) { + switch(CUMO_NA_TYPE(na)) { + case CUMO_NARRAY_DATA_T: + ptr = CUMO_NA_DATA_PTR(na); + if (CUMO_NA_SIZE(na) > 0 && ptr == NULL) { if (flag & READ) { rb_raise(rb_eRuntimeError,"cannot read unallocated NArray"); } if (flag & WRITE) { - rb_funcall(self, id_allocate, 0); - ptr = NA_DATA_PTR(na); + rb_funcall(self, cumo_id_allocate, 0); + ptr = CUMO_NA_DATA_PTR(na); } } return ptr; - case NARRAY_VIEW_T: - obj = NA_VIEW_DATA(na); + case CUMO_NARRAY_VIEW_T: + obj = CUMO_NA_VIEW_DATA(na); if ((flag & WRITE) && OBJ_FROZEN(obj)) { rb_raise(rb_eRuntimeError, "cannot write to frozen NArray."); } - GetNArray(obj,na); - switch(NA_TYPE(na)) { - case NARRAY_DATA_T: - ptr = NA_DATA_PTR(na); + CumoGetNArray(obj,na); + switch(CUMO_NA_TYPE(na)) { + case CUMO_NARRAY_DATA_T: + ptr = CUMO_NA_DATA_PTR(na); if (flag & (READ|WRITE)) { - if (NA_SIZE(na) > 0 && ptr == NULL) { + if (CUMO_NA_SIZE(na) > 0 && ptr == NULL) { rb_raise(rb_eRuntimeError,"cannot read/write unallocated NArray"); } } return ptr; default: - rb_raise(rb_eRuntimeError,"invalid NA_TYPE of view: %d",NA_TYPE(na)); + rb_raise(rb_eRuntimeError,"invalid CUMO_NA_TYPE of view: %d",CUMO_NA_TYPE(na)); } default: - rb_raise(rb_eRuntimeError,"invalid NA_TYPE: %d",NA_TYPE(na)); + rb_raise(rb_eRuntimeError,"invalid CUMO_NA_TYPE: %d",CUMO_NA_TYPE(na)); } return NULL; } char * -na_get_pointer_for_read(VALUE self) +cumo_na_get_pointer_for_read(VALUE self) { - return na_get_pointer_for_rw(self, READ); + return cumo_na_get_pointer_for_rw(self, READ); } char * -na_get_pointer_for_write(VALUE self) +cumo_na_get_pointer_for_write(VALUE self) { - return na_get_pointer_for_rw(self, WRITE); + return cumo_na_get_pointer_for_rw(self, WRITE); } char * -na_get_pointer_for_read_write(VALUE self) +cumo_na_get_pointer_for_read_write(VALUE self) { - return na_get_pointer_for_rw(self, READ|WRITE); + return cumo_na_get_pointer_for_rw(self, READ|WRITE); } char * -na_get_pointer(VALUE self) +cumo_na_get_pointer(VALUE self) { - return na_get_pointer_for_rw(self, 0); + return cumo_na_get_pointer_for_rw(self, 0); } void -na_release_lock(VALUE self) +cumo_na_release_lock(VALUE self) { - narray_t *na; + cumo_narray_t *na; - UNSET_LOCK(self); - GetNArray(self,na); + CUMO_UNCUMO_SET_LOCK(self); + CumoGetNArray(self,na); - switch(NA_TYPE(na)) { - case NARRAY_VIEW_T: - na_release_lock(NA_VIEW_DATA(na)); + switch(CUMO_NA_TYPE(na)) { + case CUMO_NARRAY_VIEW_T: + cumo_na_release_lock(CUMO_NA_VIEW_DATA(na)); break; } } /* method: size() -- returns the total number of typeents */ static VALUE -na_size(VALUE self) +cumo_na_size(VALUE self) { - narray_t *na; - GetNArray(self,na); + cumo_narray_t *na; + CumoGetNArray(self,na); return SIZET2NUM(na->size); } /* method: size() -- returns the total number of typeents */ static VALUE -na_ndim(VALUE self) +cumo_na_ndim(VALUE self) { - narray_t *na; - GetNArray(self,na); + cumo_narray_t *na; + CumoGetNArray(self,na); return INT2NUM(na->ndim); } /* Returns true if self.size == 0. @overload empty? */ static VALUE -na_empty_p(VALUE self) +cumo_na_empty_p(VALUE self) { - narray_t *na; - GetNArray(self,na); - if (NA_SIZE(na)==0) { + cumo_narray_t *na; + CumoGetNArray(self,na); + if (CUMO_NA_SIZE(na)==0) { return Qtrue; } return Qfalse; } /* method: shape() -- returns shape, array of the size of dimensions */ static VALUE - na_shape(VALUE self) + cumo_na_shape(VALUE self) { volatile VALUE v; - narray_t *na; + cumo_narray_t *na; size_t i, n, c, s; - GetNArray(self,na); - n = NA_NDIM(na); - if (TEST_COLUMN_MAJOR(self)) { + CumoGetNArray(self,na); + n = CUMO_NA_NDIM(na); + if (CUMO_TEST_COLUMN_MAJOR(self)) { c = n-1; s = -1; } else { c = 0; s = 1; @@ -731,138 +731,138 @@ return v; } unsigned int -nary_element_stride(VALUE v) +cumo_na_element_stride(VALUE v) { - narray_type_info_t *info; - narray_t *na; + cumo_narray_type_info_t *info; + cumo_narray_t *na; - GetNArray(v,na); - if (na->type == NARRAY_VIEW_T) { - v = NA_VIEW_DATA(na); - GetNArray(v,na); + CumoGetNArray(v,na); + if (na->type == CUMO_NARRAY_VIEW_T) { + v = CUMO_NA_VIEW_DATA(na); + CumoGetNArray(v,na); } - assert(na->type == NARRAY_DATA_T); + assert(na->type == CUMO_NARRAY_DATA_T); - info = (narray_type_info_t *)(RTYPEDDATA_TYPE(v)->data); + info = (cumo_narray_type_info_t *)(RTYPEDDATA_TYPE(v)->data); return info->element_stride; } size_t -na_dtype_elmsz(VALUE klass) +cumo_na_dtype_element_stride(VALUE klass) { - return NUM2SIZET(rb_const_get(klass, id_contiguous_stride)); + return NUM2SIZET(rb_const_get(klass, cumo_id_contiguous_stride)); } size_t -na_get_offset(VALUE self) +cumo_na_get_offset(VALUE self) { - narray_t *na; - GetNArray(self,na); + cumo_narray_t *na; + CumoGetNArray(self,na); switch(na->type) { - case NARRAY_DATA_T: - case NARRAY_FILEMAP_T: + case CUMO_NARRAY_DATA_T: + case CUMO_NARRAY_FILEMAP_T: return 0; - case NARRAY_VIEW_T: - return NA_VIEW_OFFSET(na); + case CUMO_NARRAY_VIEW_T: + return CUMO_NA_VIEW_OFFSET(na); } return 0; } void -na_index_arg_to_internal_order(int argc, VALUE *argv, VALUE self) +cumo_na_index_arg_to_internal_order(int argc, VALUE *argv, VALUE self) { int i,j; VALUE tmp; - if (TEST_COLUMN_MAJOR(self)) { + if (CUMO_TEST_COLUMN_MAJOR(self)) { for (i=0,j=argc-1; i<argc/2; i++,j--) { tmp = argv[i]; argv[i] = argv[j]; argv[j] = tmp; } } } void -na_copy_flags(VALUE src, VALUE dst) +cumo_na_copy_flags(VALUE src, VALUE dst) { - narray_t *na1, *na2; + cumo_narray_t *na1, *na2; - GetNArray(src,na1); - GetNArray(dst,na2); + CumoGetNArray(src,na1); + CumoGetNArray(dst,na2); na2->flag[0] = na1->flag[0]; - //na2->flag[1] = NA_FL1_INIT; + //na2->flag[1] = CUMO_NA_FL1_INIT; RBASIC(dst)->flags |= (RBASIC(src)->flags) & (FL_USER1|FL_USER2|FL_USER3|FL_USER4|FL_USER5|FL_USER6|FL_USER7); } // fix name, ex, allow_stride_for_flatten_view VALUE -na_check_ladder(VALUE self, int start_dim) +cumo_na_check_ladder(VALUE self, int start_dim) { int i; ssize_t st0, st1; - narray_t *na; - GetNArray(self,na); + cumo_narray_t *na; + CumoGetNArray(self,na); if (start_dim < -na->ndim || start_dim >= na->ndim) { rb_bug("start_dim (%d) out of range",start_dim); } switch(na->type) { - case NARRAY_DATA_T: - case NARRAY_FILEMAP_T: + case CUMO_NARRAY_DATA_T: + case CUMO_NARRAY_FILEMAP_T: return Qtrue; - case NARRAY_VIEW_T: + case CUMO_NARRAY_VIEW_T: // negative dim -> position from last dim if (start_dim < 0) { - start_dim += NA_NDIM(na); + start_dim += CUMO_NA_NDIM(na); } // not ladder if it has index - for (i=start_dim; i<NA_NDIM(na); i++) { - if (NA_IS_INDEX_AT(na,i)) + for (i=start_dim; i<CUMO_NA_NDIM(na); i++) { + if (CUMO_NA_IS_INDEX_AT(na,i)) return Qfalse; } // check stride - st0 = NA_STRIDE_AT(na,start_dim); - for (i=start_dim+1; i<NA_NDIM(na); i++) { - st1 = NA_STRIDE_AT(na,i); - if (st0 != (ssize_t)(st1 * NA_SHAPE(na)[i])) { + st0 = CUMO_NA_STRIDE_AT(na,start_dim); + for (i=start_dim+1; i<CUMO_NA_NDIM(na); i++) { + st1 = CUMO_NA_STRIDE_AT(na,i); + if (st0 != (ssize_t)(st1 * CUMO_NA_SHAPE(na)[i])) { return Qfalse; } st0 = st1; } } return Qtrue; } VALUE -na_check_contiguous(VALUE self) +cumo_na_check_contiguous(VALUE self) { ssize_t elmsz; - narray_t *na; - GetNArray(self,na); + cumo_narray_t *na; + CumoGetNArray(self,na); switch(na->type) { - case NARRAY_DATA_T: - case NARRAY_FILEMAP_T: + case CUMO_NARRAY_DATA_T: + case CUMO_NARRAY_FILEMAP_T: return Qtrue; - case NARRAY_VIEW_T: - if (NA_VIEW_STRIDX(na)==0) { + case CUMO_NARRAY_VIEW_T: + if (CUMO_NA_VIEW_STRIDX(na)==0) { return Qtrue; } - if (na_check_ladder(self,0)==Qtrue) { - elmsz = nary_element_stride(self); - if (elmsz == NA_STRIDE_AT(na,NA_NDIM(na)-1)) { + if (cumo_na_check_ladder(self,0)==Qtrue) { + elmsz = cumo_na_element_stride(self); + if (elmsz == CUMO_NA_STRIDE_AT(na,CUMO_NA_NDIM(na)-1)) { return Qtrue; } } } return Qfalse; @@ -875,52 +875,52 @@ * narray.view => narray * * Return view of NArray */ VALUE -na_make_view(VALUE self) +cumo_na_make_view(VALUE self) { int i, nd; size_t j; size_t *idx1, *idx2; ssize_t stride; - narray_t *na; - narray_view_t *na1, *na2; + cumo_narray_t *na; + cumo_narray_view_t *na1, *na2; volatile VALUE view; - GetNArray(self,na); + CumoGetNArray(self,na); nd = na->ndim; - view = na_s_allocate_view(CLASS_OF(self)); + view = cumo_na_s_allocate_view(rb_obj_class(self)); - na_copy_flags(self, view); - GetNArrayView(view, na2); + cumo_na_copy_flags(self, view); + CumoGetNArrayView(view, na2); - na_setup_shape((narray_t*)na2, nd, na->shape); - na2->stridx = ALLOC_N(stridx_t,nd); + cumo_na_setup_shape((cumo_narray_t*)na2, nd, na->shape); + na2->stridx = ALLOC_N(cumo_stridx_t,nd); switch(na->type) { - case NARRAY_DATA_T: - case NARRAY_FILEMAP_T: - stride = nary_element_stride(self); + case CUMO_NARRAY_DATA_T: + case CUMO_NARRAY_FILEMAP_T: + stride = cumo_na_element_stride(self); for (i=nd; i--;) { - SDX_SET_STRIDE(na2->stridx[i],stride); + CUMO_SDX_SET_STRIDE(na2->stridx[i],stride); stride *= na->shape[i]; } na2->offset = 0; na2->data = self; break; - case NARRAY_VIEW_T: - GetNArrayView(self, na1); + case CUMO_NARRAY_VIEW_T: + CumoGetNArrayView(self, na1); for (i=0; i<nd; i++) { - if (SDX_IS_INDEX(na1->stridx[i])) { - idx1 = SDX_GET_INDEX(na1->stridx[i]); + if (CUMO_SDX_IS_INDEX(na1->stridx[i])) { + idx1 = CUMO_SDX_GET_INDEX(na1->stridx[i]); idx2 = ALLOC_N(size_t,na1->base.shape[i]); for (j=0; j<na1->base.shape[i]; j++) { idx2[j] = idx1[j]; } - SDX_SET_INDEX(na2->stridx[i],idx2); + CUMO_SDX_SET_INDEX(na2->stridx[i],idx2); } else { na2->stridx[i] = na1->stridx[i]; } } na2->offset = na1->offset; @@ -942,55 +942,55 @@ * at a given dimension. * @param [Integer] dim dimension at which new axis is inserted. * @return [Cumo::NArray] result narray view. */ static VALUE -na_expand_dims(VALUE self, VALUE vdim) +cumo_na_expand_dims(VALUE self, VALUE vdim) { int i, j, nd, dim; - size_t *shape, *na_shape; - stridx_t *stridx, *na_stridx; - narray_t *na; - narray_view_t *na2; + size_t *shape, *cumo_na_shape; + cumo_stridx_t *stridx, *cumo_na_stridx; + cumo_narray_t *na; + cumo_narray_view_t *na2; VALUE view; - GetNArray(self,na); + CumoGetNArray(self,na); nd = na->ndim; dim = NUM2INT(vdim); if (dim < -nd-1 || dim > nd) { - rb_raise(nary_eDimensionError,"invalid axis (%d for %dD NArray)", + rb_raise(cumo_na_eDimensionError,"invalid axis (%d for %dD NArray)", dim,nd); } if (dim < 0) { dim += nd+1; } - view = na_make_view(self); - GetNArrayView(view, na2); + view = cumo_na_make_view(self); + CumoGetNArrayView(view, na2); shape = ALLOC_N(size_t,nd+1); - stridx = ALLOC_N(stridx_t,nd+1); - na_shape = na2->base.shape; - na_stridx = na2->stridx; + stridx = ALLOC_N(cumo_stridx_t,nd+1); + cumo_na_shape = na2->base.shape; + cumo_na_stridx = na2->stridx; for (i=j=0; i<=nd; i++) { if (i==dim) { shape[i] = 1; - SDX_SET_STRIDE(stridx[i],0); + CUMO_SDX_SET_STRIDE(stridx[i],0); } else { - shape[i] = na_shape[j]; - stridx[i] = na_stridx[j]; + shape[i] = cumo_na_shape[j]; + stridx[i] = cumo_na_stridx[j]; j++; } } na2->stridx = stridx; - xfree(na_stridx); + xfree(cumo_na_stridx); na2->base.shape = shape; - if (na_shape != &(na2->base.size)) { - xfree(na_shape); + if (cumo_na_shape != &(na2->base.size)) { + xfree(cumo_na_shape); } na2->base.ndim++; return view; } @@ -1001,77 +1001,77 @@ * narray.reverse([dim0,dim1,..]) => narray * * Return reversed view along specified dimeinsion */ static VALUE -nary_reverse(int argc, VALUE *argv, VALUE self) +cumo_na_reverse(int argc, VALUE *argv, VALUE self) { int i, nd; size_t j, n; size_t offset; size_t *idx1, *idx2; ssize_t stride; ssize_t sign; - narray_t *na; - narray_view_t *na1, *na2; + cumo_narray_t *na; + cumo_narray_view_t *na1, *na2; VALUE view; VALUE reduce; - reduce = na_reduce_dimension(argc, argv, 1, &self, 0, 0); + reduce = cumo_na_reduce_dimension(argc, argv, 1, &self, 0, 0); - GetNArray(self,na); + CumoGetNArray(self,na); nd = na->ndim; - view = na_s_allocate_view(CLASS_OF(self)); + view = cumo_na_s_allocate_view(rb_obj_class(self)); - na_copy_flags(self, view); - GetNArrayView(view, na2); + cumo_na_copy_flags(self, view); + CumoGetNArrayView(view, na2); - na_setup_shape((narray_t*)na2, nd, na->shape); - na2->stridx = ALLOC_N(stridx_t,nd); + cumo_na_setup_shape((cumo_narray_t*)na2, nd, na->shape); + na2->stridx = ALLOC_N(cumo_stridx_t,nd); switch(na->type) { - case NARRAY_DATA_T: - case NARRAY_FILEMAP_T: - stride = nary_element_stride(self); + case CUMO_NARRAY_DATA_T: + case CUMO_NARRAY_FILEMAP_T: + stride = cumo_na_element_stride(self); offset = 0; for (i=nd; i--;) { - if (na_test_reduce(reduce,i)) { + if (cumo_na_test_reduce(reduce,i)) { offset += (na->shape[i]-1)*stride; sign = -1; } else { sign = 1; } - SDX_SET_STRIDE(na2->stridx[i],stride*sign); + CUMO_SDX_SET_STRIDE(na2->stridx[i],stride*sign); stride *= na->shape[i]; } na2->offset = offset; na2->data = self; break; - case NARRAY_VIEW_T: - GetNArrayView(self, na1); + case CUMO_NARRAY_VIEW_T: + CumoGetNArrayView(self, na1); offset = na1->offset; for (i=0; i<nd; i++) { n = na1->base.shape[i]; - if (SDX_IS_INDEX(na1->stridx[i])) { - idx1 = SDX_GET_INDEX(na1->stridx[i]); + if (CUMO_SDX_IS_INDEX(na1->stridx[i])) { + idx1 = CUMO_SDX_GET_INDEX(na1->stridx[i]); idx2 = ALLOC_N(size_t,n); - if (na_test_reduce(reduce,i)) { + if (cumo_na_test_reduce(reduce,i)) { for (j=0; j<n; j++) { idx2[n-1-j] = idx1[j]; } } else { for (j=0; j<n; j++) { idx2[j] = idx1[j]; } } - SDX_SET_INDEX(na2->stridx[i],idx2); + CUMO_SDX_SET_INDEX(na2->stridx[i],idx2); } else { - stride = SDX_GET_STRIDE(na1->stridx[i]); - if (na_test_reduce(reduce,i)) { + stride = CUMO_SDX_GET_STRIDE(na1->stridx[i]); + if (cumo_na_test_reduce(reduce,i)) { offset += (n-1)*stride; - SDX_SET_STRIDE(na2->stridx[i],-stride); + CUMO_SDX_SET_STRIDE(na2->stridx[i],-stride); } else { na2->stridx[i] = na1->stridx[i]; } } } @@ -1092,16 +1092,16 @@ VALUE result_type; if (type1==type2) { return type1; } - upcast_hash = rb_const_get(type1, id_UPCAST); + upcast_hash = rb_const_get(type1, cumo_id_UPCAST); result_type = rb_hash_aref(upcast_hash, type2); if (NIL_P(result_type)) { if (TYPE(type2)==T_CLASS) { if (RTEST(rb_class_inherited_p(type2,cNArray))) { - upcast_hash = rb_const_get(type2, id_UPCAST); + upcast_hash = rb_const_get(type2, cumo_id_UPCAST); result_type = rb_hash_aref(upcast_hash, type1); } } } return result_type; @@ -1115,32 +1115,32 @@ @overload coerce(other) @param [Object] other numeric object. @return [Array] NArray-casted [other,self] */ static VALUE -nary_coerce(VALUE x, VALUE y) +cumo_na_coerce(VALUE x, VALUE y) { VALUE type; - type = cumo_na_upcast(CLASS_OF(x), CLASS_OF(y)); - y = rb_funcall(type,id_cast,1,y); + type = cumo_na_upcast(rb_obj_class(x), rb_obj_class(y)); + y = rb_funcall(type,cumo_id_cast,1,y); return rb_assoc_new(y , x); } /* Returns total byte size of NArray. @return [Integer] byte size. */ static VALUE -nary_byte_size(VALUE self) +cumo_na_byte_size(VALUE self) { VALUE velmsz; - narray_t *na; + cumo_narray_t *na; - GetNArray(self,na); - velmsz = rb_const_get(CLASS_OF(self), id_element_byte_size); + CumoGetNArray(self,na); + velmsz = rb_const_get(rb_obj_class(self), cumo_id_element_byte_size); if (FIXNUM_P(velmsz)) { return SIZET2NUM(NUM2SIZET(velmsz) * na->size); } return SIZET2NUM(ceil(NUM2DBL(velmsz) * na->size)); } @@ -1148,13 +1148,13 @@ /* Returns byte size of one element of NArray. @return [Numeric] byte size. */ static VALUE -nary_s_byte_size(VALUE type) +cumo_na_s_byte_size(VALUE type) { - return rb_const_get(type, id_element_byte_size); + return rb_const_get(type, cumo_id_element_byte_size); } /* Returns a new 1-D array initialized from binary raw data in a string. @@ -1162,11 +1162,11 @@ @param [String] string Binary raw data. @param [Array] shape array of integers representing array shape. @return [Cumo::NArray] NArray containing binary data. */ static VALUE -nary_s_from_binary(int argc, VALUE *argv, VALUE type) +cumo_na_s_from_binary(int argc, VALUE *argv, VALUE type) { size_t len, str_len, byte_size; size_t *shape; char *ptr; int i, nd, narg; @@ -1174,22 +1174,22 @@ VALUE velmsz; narg = rb_scan_args(argc,argv,"11",&vstr,&vshape); Check_Type(vstr,T_STRING); str_len = RSTRING_LEN(vstr); - velmsz = rb_const_get(type, id_element_byte_size); + velmsz = rb_const_get(type, cumo_id_element_byte_size); if (narg==2) { switch(TYPE(vshape)) { case T_FIXNUM: nd = 1; len = NUM2SIZET(vshape); shape = &len; break; case T_ARRAY: nd = RARRAY_LEN(vshape); - if (nd == 0 || nd > NA_MAX_DIMENSION) { - rb_raise(nary_eDimensionError,"too long or empty shape (%d)", nd); + if (nd == 0 || nd > CUMO_NA_MAX_DIMENSION) { + rb_raise(cumo_na_eDimensionError,"too long or empty shape (%d)", nd); } shape = ALLOCA_N(size_t,nd); len = 1; for (i=0; i<nd; ++i) { len *= shape[i] = NUM2SIZET(RARRAY_AREF(vshape,i)); @@ -1220,12 +1220,12 @@ } shape = ALLOCA_N(size_t,nd); shape[0] = len; } - vna = nary_new(type, nd, shape); - ptr = na_get_pointer_for_write(vna); + vna = cumo_na_new(type, nd, shape); + ptr = cumo_na_get_pointer_for_write(vna); memcpy(ptr, RSTRING_PTR(vstr), byte_size); return vna; } @@ -1236,18 +1236,18 @@ @param [String] string Binary raw data. @param [Integer] (optional) offset Byte offset in string. @return [Integer] stored length. */ static VALUE -nary_store_binary(int argc, VALUE *argv, VALUE self) +cumo_na_store_binary(int argc, VALUE *argv, VALUE self) { size_t size, str_len, byte_size, offset; char *ptr; int narg; VALUE vstr, voffset; VALUE velmsz; - narray_t *na; + cumo_narray_t *na; narg = rb_scan_args(argc,argv,"11",&vstr,&voffset); str_len = RSTRING_LEN(vstr); if (narg==2) { offset = NUM2SIZET(voffset); @@ -1257,23 +1257,23 @@ str_len -= offset; } else { offset = 0; } - GetNArray(self,na); - size = NA_SIZE(na); - velmsz = rb_const_get(CLASS_OF(self), id_element_byte_size); + CumoGetNArray(self,na); + size = CUMO_NA_SIZE(na); + velmsz = rb_const_get(rb_obj_class(self), cumo_id_element_byte_size); if (FIXNUM_P(velmsz)) { byte_size = size * NUM2SIZET(velmsz); } else { byte_size = ceil(size * NUM2DBL(velmsz)); } if (byte_size > str_len) { rb_raise(rb_eArgError, "string is too short to store"); } - ptr = na_get_pointer_for_write(self); + ptr = cumo_na_get_pointer_for_write(self); memcpy(ptr, RSTRING_PTR(vstr)+offset, byte_size); return SIZET2NUM(byte_size); } @@ -1281,30 +1281,30 @@ Returns string containing the raw data bytes in NArray. @overload to_binary() @return [String] String object containing binary raw data. */ static VALUE -nary_to_binary(VALUE self) +cumo_na_to_binary(VALUE self) { size_t len, offset=0; char *ptr; VALUE str; - narray_t *na; + cumo_narray_t *na; - SHOW_SYNCHRONIZE_WARNING_ONCE("nary_to_binary", "any"); + CUMO_SHOW_SYNCHRONIZE_WARNING_ONCE("cumo_na_to_binary", "any"); cumo_cuda_runtime_check_status(cudaDeviceSynchronize()); - GetNArray(self,na); - if (na->type == NARRAY_VIEW_T) { - if (na_check_contiguous(self)==Qtrue) { - offset = NA_VIEW_OFFSET(na); + CumoGetNArray(self,na); + if (na->type == CUMO_NARRAY_VIEW_T) { + if (cumo_na_check_contiguous(self)==Qtrue) { + offset = CUMO_NA_VIEW_OFFSET(na); } else { - self = rb_funcall(self,id_dup,0); + self = rb_funcall(self,cumo_id_dup,0); } } - len = NUM2SIZET(nary_byte_size(self)); - ptr = na_get_pointer_for_read(self); + len = NUM2SIZET(cumo_na_byte_size(self)); + ptr = cumo_na_get_pointer_for_read(self); str = rb_usascii_str_new(ptr+offset,len); RB_GC_GUARD(self); return str; } @@ -1312,51 +1312,51 @@ Dump marshal data. @overload marshal_dump() @return [Array] Array containing marshal data. */ static VALUE -nary_marshal_dump(VALUE self) +cumo_na_marshal_dump(VALUE self) { VALUE a; - SHOW_SYNCHRONIZE_WARNING_ONCE("nary_marshal_dump", "any"); + CUMO_SHOW_SYNCHRONIZE_WARNING_ONCE("cumo_na_marshal_dump", "any"); cumo_cuda_runtime_check_status(cudaDeviceSynchronize()); a = rb_ary_new(); rb_ary_push(a, INT2FIX(1)); // version - rb_ary_push(a, na_shape(self)); - rb_ary_push(a, INT2FIX(NA_FLAG0(self))); - if (CLASS_OF(self) == cumo_cRObject) { - narray_t *na; + rb_ary_push(a, cumo_na_shape(self)); + rb_ary_push(a, INT2FIX(CUMO_NA_FLAG0(self))); + if (rb_obj_class(self) == cumo_cRObject) { + cumo_narray_t *na; VALUE *ptr; size_t offset=0; - GetNArray(self,na); - if (na->type == NARRAY_VIEW_T) { - if (na_check_contiguous(self)==Qtrue) { - offset = NA_VIEW_OFFSET(na); + CumoGetNArray(self,na); + if (na->type == CUMO_NARRAY_VIEW_T) { + if (cumo_na_check_contiguous(self)==Qtrue) { + offset = CUMO_NA_VIEW_OFFSET(na); } else { - self = rb_funcall(self,id_dup,0); + self = rb_funcall(self,cumo_id_dup,0); } } - ptr = (VALUE*)na_get_pointer_for_read(self); - rb_ary_push(a, rb_ary_new4(NA_SIZE(na), ptr+offset)); + ptr = (VALUE*)cumo_na_get_pointer_for_read(self); + rb_ary_push(a, rb_ary_new4(CUMO_NA_SIZE(na), ptr+offset)); } else { - rb_ary_push(a, nary_to_binary(self)); + rb_ary_push(a, cumo_na_to_binary(self)); } RB_GC_GUARD(self); return a; } -static VALUE na_inplace( VALUE self ); +static VALUE cumo_na_inplace( VALUE self ); /* Load marshal data. @overload marshal_load(data) @params [Array] Array containing marshal data. @return [nil] */ static VALUE -nary_marshal_load(VALUE self, VALUE a) +cumo_na_marshal_load(VALUE self, VALUE a) { VALUE v; if (TYPE(a) != T_ARRAY) { rb_raise(rb_eArgError,"marshal argument should be array"); @@ -1366,30 +1366,30 @@ } if (RARRAY_AREF(a,0) != INT2FIX(1)) { rb_raise(rb_eArgError,"NArray marshal version %d is not supported " "(only version 1)", NUM2INT(RARRAY_AREF(a,0))); } - na_initialize(self,RARRAY_AREF(a,1)); - NA_FL0_SET(self,FIX2INT(RARRAY_AREF(a,2))); + cumo_na_initialize(self,RARRAY_AREF(a,1)); + CUMO_NA_FL0_SET(self,FIX2INT(RARRAY_AREF(a,2))); v = RARRAY_AREF(a,3); - if (CLASS_OF(self) == cumo_cRObject) { - narray_t *na; + if (rb_obj_class(self) == cumo_cRObject) { + cumo_narray_t *na; char *ptr; if (TYPE(v) != T_ARRAY) { rb_raise(rb_eArgError,"RObject content should be array"); } - GetNArray(self,na); - if (RARRAY_LEN(v) != (long)NA_SIZE(na)) { + CumoGetNArray(self,na); + if (RARRAY_LEN(v) != (long)CUMO_NA_SIZE(na)) { rb_raise(rb_eArgError,"RObject content size mismatch"); } - ptr = na_get_pointer_for_write(self); - memcpy(ptr, RARRAY_PTR(v), NA_SIZE(na)*sizeof(VALUE)); + ptr = cumo_na_get_pointer_for_write(self); + memcpy(ptr, RARRAY_PTR(v), CUMO_NA_SIZE(na)*sizeof(VALUE)); } else { - nary_store_binary(1,&v,self); - if (TEST_BYTE_SWAPPED(self)) { - rb_funcall(na_inplace(self),id_to_host,0); - REVERSE_ENDIAN(self); // correct behavior?? + cumo_na_store_binary(1,&v,self); + if (CUMO_TEST_BYTE_SWAPPED(self)) { + rb_funcall(cumo_na_inplace(self),cumo_id_to_host,0); + CUMO_REVERSE_ENDIAN(self); // correct behavior?? } } RB_GC_GUARD(a); return self; } @@ -1400,65 +1400,65 @@ @overload cast_to(datatype) @param [Class] datatype NArray datatype. @return [Cumo::NArray] */ static VALUE -nary_cast_to(VALUE obj, VALUE type) +cumo_na_cast_to(VALUE obj, VALUE type) { - return rb_funcall(type, id_cast, 1, obj); + return rb_funcall(type, cumo_id_cast, 1, obj); } // reduce is dimension indicies to reduce in reduction kernel (in bits), e.g., for an array of shape: // [2,3,4], 111b for sum(), 010b for sum(axis: 1), 110b for sum(axis: [1,2]) bool -na_test_reduce(VALUE reduce, int dim) +cumo_na_test_reduce(VALUE reduce, int dim) { size_t m; if (!RTEST(reduce)) return 0; if (FIXNUM_P(reduce)) { m = FIX2LONG(reduce); if (m==0) return 1; return (m & (1u<<dim)) ? 1 : 0; } else { - return (rb_funcall(reduce,id_bracket,1,INT2FIX(dim))==INT2FIX(1)) ? + return (rb_funcall(reduce,cumo_id_bracket,1,INT2FIX(dim))==INT2FIX(1)) ? 1 : 0 ; } } static VALUE -na_get_reduce_flag_from_narray(int naryc, VALUE *naryv, int *max_arg) +cumo_na_get_reduce_flag_from_narray(int naryc, VALUE *naryv, int *max_arg) { int ndim, ndim0; int rowmaj; int i; size_t j; - narray_t *na; + cumo_narray_t *na; VALUE reduce; if (naryc<1) { rb_raise(rb_eRuntimeError,"must be positive: naryc=%d", naryc); } - GetNArray(naryv[0],na); + CumoGetNArray(naryv[0],na); if (na->size==0) { - rb_raise(nary_eShapeError,"cannot reduce empty NArray"); + rb_raise(cumo_na_eShapeError,"cannot reduce empty NArray"); } reduce = na->reduce; ndim = ndim0 = na->ndim; if (max_arg) *max_arg = 0; - rowmaj = TEST_COLUMN_MAJOR(naryv[0]); + rowmaj = CUMO_TEST_COLUMN_MAJOR(naryv[0]); for (i=0; i<naryc; i++) { - GetNArray(naryv[i],na); + CumoGetNArray(naryv[i],na); if (na->size==0) { - rb_raise(nary_eShapeError,"cannot reduce empty NArray"); + rb_raise(cumo_na_eShapeError,"cannot reduce empty NArray"); } - if (TEST_COLUMN_MAJOR(naryv[i]) != rowmaj) { - rb_raise(nary_eDimensionError,"dimension order is different"); + if (CUMO_TEST_COLUMN_MAJOR(naryv[i]) != rowmaj) { + rb_raise(cumo_na_eDimensionError,"dimension order is different"); } if (na->ndim > ndim) { // maximum dimension ndim = na->ndim; if (max_arg) *max_arg = i; } @@ -1470,26 +1470,26 @@ return reduce; } static VALUE -na_get_reduce_flag_from_axes(VALUE na_obj, VALUE axes) +cumo_na_get_reduce_flag_from_axes(VALUE cumo_na_obj, VALUE axes) { int i, r; int ndim, rowmaj; long narg; size_t j; size_t len; ssize_t beg, step; VALUE v; size_t m; VALUE reduce; - narray_t *na; + cumo_narray_t *na; - GetNArray(na_obj,na); + CumoGetNArray(cumo_na_obj,na); ndim = na->ndim; - rowmaj = TEST_COLUMN_MAJOR(na_obj); + rowmaj = CUMO_TEST_COLUMN_MAJOR(cumo_na_obj); m = 0; reduce = Qnil; narg = RARRAY_LEN(axes); for (i=0; i<narg; i++) { @@ -1497,20 +1497,20 @@ //printf("argv[%d]=",i);rb_p(v); if (TYPE(v)==T_FIXNUM) { beg = FIX2INT(v); if (beg<0) beg+=ndim; if (beg>=ndim || beg<0) { - rb_raise(nary_eDimensionError,"dimension is out of range"); + rb_raise(cumo_na_eDimensionError,"dimension is out of range"); } len = 1; step = 0; //printf("beg=%d step=%d len=%d\n",beg,step,len); } else if (rb_obj_is_kind_of(v,rb_cRange) || - rb_obj_is_kind_of(v,na_cStep)) { - nary_step_array_index( v, ndim, &len, &beg, &step ); + rb_obj_is_kind_of(v,cumo_na_cStep)) { + cumo_na_step_array_index( v, ndim, &len, &beg, &step ); } else { - rb_raise(nary_eDimensionError, "invalid dimension argument %s", + rb_raise(cumo_na_eDimensionError, "invalid dimension argument %s", rb_obj_classname(v)); } for (j=0; j<len; j++) { r = beg + step*j; if (rowmaj) { @@ -1522,22 +1522,22 @@ continue; } else { reduce = SIZET2NUM(m); } } - v = rb_funcall( INT2FIX(1), id_shift_left, 1, INT2FIX(r) ); + v = rb_funcall( INT2FIX(1), cumo_id_shift_left, 1, INT2FIX(r) ); reduce = rb_funcall( reduce, '|', 1, v ); } } if (NIL_P(reduce)) reduce = SIZET2NUM(m); return reduce; } VALUE -nary_reduce_options(VALUE axes, VALUE *opts, int naryc, VALUE *naryv, - ndfunc_t *ndf) +cumo_na_reduce_options(VALUE axes, VALUE *opts, int naryc, VALUE *naryv, + cumo_ndfunc_t *ndf) { int max_arg; VALUE reduce; // option: axis @@ -1554,30 +1554,30 @@ } if (ndf) { // option: keepdims if (opts[1] != Qundef) { if (RTEST(opts[1])) - ndf->flag |= NDF_KEEP_DIM; + ndf->flag |= CUMO_NDF_KEEP_DIM; } } - reduce = na_get_reduce_flag_from_narray(naryc, naryv, &max_arg); + reduce = cumo_na_get_reduce_flag_from_narray(naryc, naryv, &max_arg); if (NIL_P(axes)) return reduce; - return na_get_reduce_flag_from_axes(naryv[max_arg], axes); + return cumo_na_get_reduce_flag_from_axes(naryv[max_arg], axes); } VALUE -nary_reduce_dimension(int argc, VALUE *argv, int naryc, VALUE *naryv, - ndfunc_t *ndf, na_iter_func_t iter_nan) +cumo_na_reduce_dimension(int argc, VALUE *argv, int naryc, VALUE *naryv, + cumo_ndfunc_t *ndf, cumo_na_iter_func_t iter_nan) { long narg; VALUE axes; VALUE kw_hash = Qnil; - ID kw_table[3] = {id_axis,id_keepdims,id_nan}; + ID kw_table[3] = {cumo_id_axis,cumo_id_keepdims,cumo_id_nan}; VALUE opts[3] = {Qundef,Qundef,Qundef}; narg = rb_scan_args(argc, argv, "*:", &axes, &kw_hash); rb_get_kwargs(kw_hash, kw_table, 0, 3, opts); @@ -1587,131 +1587,131 @@ if (RTEST(opts[2])) ndf->func = iter_nan; // replace to nan-aware iterator function } } - return na_reduce_options((narg)?axes:Qnil, opts, naryc, naryv, ndf); + return cumo_na_reduce_options((narg)?axes:Qnil, opts, naryc, naryv, ndf); } /* Return true if column major. */ -static VALUE na_column_major_p( VALUE self ) +static VALUE cumo_na_column_major_p( VALUE self ) { - if (TEST_COLUMN_MAJOR(self)) + if (CUMO_TEST_COLUMN_MAJOR(self)) return Qtrue; else return Qfalse; } /* Return true if row major. */ -static VALUE na_row_major_p( VALUE self ) +static VALUE cumo_na_row_major_p( VALUE self ) { - if (TEST_ROW_MAJOR(self)) + if (CUMO_TEST_ROW_MAJOR(self)) return Qtrue; else return Qfalse; } /* Return true if byte swapped. */ -static VALUE na_byte_swapped_p( VALUE self ) +static VALUE cumo_na_byte_swapped_p( VALUE self ) { - if (TEST_BYTE_SWAPPED(self)) + if (CUMO_TEST_BYTE_SWAPPED(self)) return Qtrue; return Qfalse; } /* Return true if not byte swapped. */ -static VALUE na_host_order_p( VALUE self ) +static VALUE cumo_na_host_order_p( VALUE self ) { - if (TEST_BYTE_SWAPPED(self)) + if (CUMO_TEST_BYTE_SWAPPED(self)) return Qfalse; return Qtrue; } /* Returns view of narray with inplace flagged. @return [Cumo::NArray] view of narray with inplace flag. */ -static VALUE na_inplace( VALUE self ) +static VALUE cumo_na_inplace( VALUE self ) { VALUE view = self; - view = na_make_view(self); - SET_INPLACE(view); + view = cumo_na_make_view(self); + CUMO_SET_INPLACE(view); return view; } /* Set inplace flag to self. @return [Cumo::NArray] self */ -static VALUE na_inplace_bang( VALUE self ) +static VALUE cumo_na_inplace_bang( VALUE self ) { - SET_INPLACE(self); + CUMO_SET_INPLACE(self); return self; } /* Return true if inplace flagged. */ -static VALUE na_inplace_p( VALUE self ) +static VALUE cumo_na_inplace_p( VALUE self ) { - if (TEST_INPLACE(self)) + if (CUMO_TEST_INPLACE(self)) return Qtrue; else return Qfalse; } /* Unset inplace flag to self. @return [Cumo::NArray] self */ -static VALUE na_out_of_place_bang( VALUE self ) +static VALUE cumo_na_out_of_place_bang( VALUE self ) { - UNSET_INPLACE(self); + CUMO_UNCUMO_SET_INPLACE(self); return self; } -int na_debug_flag=0; +int cumo_na_debug_flag=0; -static VALUE na_debug_set(VALUE mod, VALUE flag) +static VALUE cumo_na_debug_set(VALUE mod, VALUE flag) { - na_debug_flag = RTEST(flag); + cumo_na_debug_flag = RTEST(flag); return Qnil; } -static double na_profile_value=0; +static double cumo_na_profile_value=0; -static VALUE na_profile(VALUE mod) +static VALUE cumo_na_profile(VALUE mod) { - return rb_float_new(na_profile_value); + return rb_float_new(cumo_na_profile_value); } -static VALUE na_profile_set(VALUE mod, VALUE val) +static VALUE cumo_na_profile_set(VALUE mod, VALUE val) { - na_profile_value = NUM2DBL(val); + cumo_na_profile_value = NUM2DBL(val); return val; } /* Returns the number of rows used for NArray#inspect @overload inspect_rows @return [Integer or nil] the number of rows. */ -static VALUE na_inspect_rows(VALUE mod) +static VALUE cumo_na_inspect_rows(VALUE mod) { - if (cumo_na_inspect_rows > 0) { - return INT2NUM(cumo_na_inspect_rows); + if (cumo_na_inspect_rows_ > 0) { + return INT2NUM(cumo_na_inspect_rows_); } else { return Qnil; } } @@ -1719,29 +1719,29 @@ Set the number of rows used for NArray#inspect @overload inspect_rows=(rows) @param [Integer or nil] rows the number of rows @return [nil] */ -static VALUE na_inspect_rows_set(VALUE mod, VALUE num) +static VALUE cumo_na_inspect_rows_set(VALUE mod, VALUE num) { if (RTEST(num)) { - cumo_na_inspect_rows = NUM2INT(num); + cumo_na_inspect_rows_ = NUM2INT(num); } else { - cumo_na_inspect_rows = 0; + cumo_na_inspect_rows_ = 0; } return Qnil; } /* Returns the number of cols used for NArray#inspect @overload inspect_cols @return [Integer or nil] the number of cols. */ -static VALUE na_inspect_cols(VALUE mod) +static VALUE cumo_na_inspect_cols(VALUE mod) { - if (cumo_na_inspect_cols > 0) { - return INT2NUM(cumo_na_inspect_cols); + if (cumo_na_inspect_cols_ > 0) { + return INT2NUM(cumo_na_inspect_cols_); } else { return Qnil; } } @@ -1749,16 +1749,16 @@ Set the number of cols used for NArray#inspect @overload inspect_cols=(cols) @param [Integer or nil] cols the number of cols @return [nil] */ -static VALUE na_inspect_cols_set(VALUE mod, VALUE num) +static VALUE cumo_na_inspect_cols_set(VALUE mod, VALUE num) { if (RTEST(num)) { - cumo_na_inspect_cols = NUM2INT(num); + cumo_na_inspect_cols_ = NUM2INT(num); } else { - cumo_na_inspect_cols = 0; + cumo_na_inspect_cols_ = 0; } return Qnil; } @@ -1768,55 +1768,55 @@ @overload == other @param [Object] other @return [Boolean] true if self and other is equal. */ static VALUE -na_equal(VALUE self, volatile VALUE other) +cumo_na_equal(VALUE self, volatile VALUE other) { volatile VALUE vbool; - narray_t *na1, *na2; + cumo_narray_t *na1, *na2; int i; - GetNArray(self,na1); + CumoGetNArray(self,na1); if (!rb_obj_is_kind_of(other,cNArray)) { - other = rb_funcall(CLASS_OF(self), id_cast, 1, other); + other = rb_funcall(rb_obj_class(self), cumo_id_cast, 1, other); } - GetNArray(other,na2); + CumoGetNArray(other,na2); if (na1->ndim != na2->ndim) { return Qfalse; } for (i=0; i<na1->ndim; i++) { if (na1->shape[i] != na2->shape[i]) { return Qfalse; } } - vbool = rb_funcall(self, id_eq, 1, other); - return (rb_funcall(vbool, id_count_false_cpu, 0)==INT2FIX(0)) ? Qtrue : Qfalse; + vbool = rb_funcall(self, cumo_id_eq, 1, other); + return (rb_funcall(vbool, cumo_id_count_false_cpu, 0)==INT2FIX(0)) ? Qtrue : Qfalse; } /* Free data memory explicitly without waiting GC. @return [Boolean] true if free */ VALUE cumo_na_free_data(VALUE self) { - narray_t *na; - GetNArray(self, na); + cumo_narray_t *na; + CumoGetNArray(self, na); - if (na->type == NARRAY_DATA_T) { - void *ptr = NA_DATA_PTR(na); + if (na->type == CUMO_NARRAY_DATA_T) { + void *ptr = CUMO_NA_DATA_PTR(na); if (ptr != NULL) { if (cumo_cuda_runtime_is_device_memory(ptr)) { cumo_cuda_runtime_free(ptr); } else { xfree(ptr); } - NA_DATA_PTR(na) = NULL; + CUMO_NA_DATA_PTR(na) = NULL; return Qtrue; } } return Qfalse; @@ -1843,106 +1843,106 @@ rb_cComplex = rb_const_get(rb_cObject, rb_intern("Complex")); #endif rb_define_const(cNArray, "VERSION", rb_str_new2(CUMO_VERSION)); - nary_eCastError = rb_define_class_under(cNArray, "CastError", rb_eStandardError); - nary_eShapeError = rb_define_class_under(cNArray, "ShapeError", rb_eStandardError); - nary_eOperationError = rb_define_class_under(cNArray, "OperationError", rb_eStandardError); - nary_eDimensionError = rb_define_class_under(cNArray, "DimensionError", rb_eStandardError); - nary_eValueError = rb_define_class_under(cNArray, "ValueError", rb_eStandardError); + cumo_na_eCastError = rb_define_class_under(cNArray, "CastError", rb_eStandardError); + cumo_na_eShapeError = rb_define_class_under(cNArray, "ShapeError", rb_eStandardError); + cumo_na_eOperationError = rb_define_class_under(cNArray, "OperationError", rb_eStandardError); + cumo_na_eDimensionError = rb_define_class_under(cNArray, "DimensionError", rb_eStandardError); + cumo_na_eValueError = rb_define_class_under(cNArray, "ValueError", rb_eStandardError); - rb_define_singleton_method(cNArray, "debug=", na_debug_set, 1); - rb_define_singleton_method(cNArray, "profile", na_profile, 0); - rb_define_singleton_method(cNArray, "profile=", na_profile_set, 1); + rb_define_singleton_method(cNArray, "debug=", cumo_na_debug_set, 1); + rb_define_singleton_method(cNArray, "profile", cumo_na_profile, 0); + rb_define_singleton_method(cNArray, "profile=", cumo_na_profile_set, 1); - rb_define_singleton_method(cNArray, "inspect_rows", na_inspect_rows, 0); - rb_define_singleton_method(cNArray, "inspect_rows=", na_inspect_rows_set, 1); - rb_define_singleton_method(cNArray, "inspect_cols", na_inspect_cols, 0); - rb_define_singleton_method(cNArray, "inspect_cols=", na_inspect_cols_set, 1); + rb_define_singleton_method(cNArray, "inspect_rows", cumo_na_inspect_rows, 0); + rb_define_singleton_method(cNArray, "inspect_rows=", cumo_na_inspect_rows_set, 1); + rb_define_singleton_method(cNArray, "inspect_cols", cumo_na_inspect_cols, 0); + rb_define_singleton_method(cNArray, "inspect_cols=", cumo_na_inspect_cols_set, 1); /* Ruby allocation framework */ rb_undef_alloc_func(cNArray); - rb_define_method(cNArray, "initialize", na_initialize, -2); - rb_define_method(cNArray, "initialize_copy", na_initialize_copy, 1); + rb_define_method(cNArray, "initialize", cumo_na_initialize, -2); + rb_define_method(cNArray, "initialize_copy", cumo_na_initialize_copy, 1); rb_define_method(cNArray, "free", cumo_na_free_data, 0); - rb_define_singleton_method(cNArray, "zeros", na_s_zeros, -1); - rb_define_singleton_method(cNArray, "ones", na_s_ones, -1); - rb_define_singleton_method(cNArray, "linspace", na_s_linspace, -1); - rb_define_singleton_method(cNArray, "logspace", na_s_logspace, -1); - rb_define_singleton_method(cNArray, "eye", na_s_eye, -1); + rb_define_singleton_method(cNArray, "zeros", cumo_na_s_zeros, -1); + rb_define_singleton_method(cNArray, "ones", cumo_na_s_ones, -1); + rb_define_singleton_method(cNArray, "linspace", cumo_na_s_linspace, -1); + rb_define_singleton_method(cNArray, "logspace", cumo_na_s_logspace, -1); + rb_define_singleton_method(cNArray, "eye", cumo_na_s_eye, -1); - rb_define_method(cNArray, "size", na_size, 0); + rb_define_method(cNArray, "size", cumo_na_size, 0); rb_define_alias (cNArray, "length","size"); rb_define_alias (cNArray, "total","size"); - rb_define_method(cNArray, "shape", na_shape, 0); - rb_define_method(cNArray, "ndim", na_ndim,0); + rb_define_method(cNArray, "shape", cumo_na_shape, 0); + rb_define_method(cNArray, "ndim", cumo_na_ndim,0); rb_define_alias (cNArray, "rank","ndim"); - rb_define_method(cNArray, "empty?", na_empty_p, 0); + rb_define_method(cNArray, "empty?", cumo_na_empty_p, 0); - rb_define_method(cNArray, "debug_info", nary_debug_info, 0); + rb_define_method(cNArray, "debug_info", cumo_na_debug_info, 0); - rb_define_method(cNArray, "contiguous?", na_check_contiguous, 0); + rb_define_method(cNArray, "contiguous?", cumo_na_check_contiguous, 0); - rb_define_method(cNArray, "view", na_make_view, 0); - rb_define_method(cNArray, "expand_dims", na_expand_dims, 1); - rb_define_method(cNArray, "reverse", nary_reverse, -1); + rb_define_method(cNArray, "view", cumo_na_make_view, 0); + rb_define_method(cNArray, "expand_dims", cumo_na_expand_dims, 1); + rb_define_method(cNArray, "reverse", cumo_na_reverse, -1); rb_define_singleton_method(cNArray, "upcast", cumo_na_upcast, 1); - rb_define_singleton_method(cNArray, "byte_size", nary_s_byte_size, 0); + rb_define_singleton_method(cNArray, "byte_size", cumo_na_s_byte_size, 0); - rb_define_singleton_method(cNArray, "from_binary", nary_s_from_binary, -1); + rb_define_singleton_method(cNArray, "from_binary", cumo_na_s_from_binary, -1); rb_define_alias (rb_singleton_class(cNArray), "from_string", "from_binary"); - rb_define_method(cNArray, "store_binary", nary_store_binary, -1); - rb_define_method(cNArray, "to_binary", nary_to_binary, 0); + rb_define_method(cNArray, "store_binary", cumo_na_store_binary, -1); + rb_define_method(cNArray, "to_binary", cumo_na_to_binary, 0); rb_define_alias (cNArray, "to_string", "to_binary"); - rb_define_method(cNArray, "marshal_dump", nary_marshal_dump, 0); - rb_define_method(cNArray, "marshal_load", nary_marshal_load, 1); + rb_define_method(cNArray, "marshal_dump", cumo_na_marshal_dump, 0); + rb_define_method(cNArray, "marshal_load", cumo_na_marshal_load, 1); - rb_define_method(cNArray, "byte_size", nary_byte_size, 0); + rb_define_method(cNArray, "byte_size", cumo_na_byte_size, 0); - rb_define_method(cNArray, "cast_to", nary_cast_to, 1); + rb_define_method(cNArray, "cast_to", cumo_na_cast_to, 1); - rb_define_method(cNArray, "coerce", nary_coerce, 1); + rb_define_method(cNArray, "coerce", cumo_na_coerce, 1); - rb_define_method(cNArray, "column_major?", na_column_major_p, 0); - rb_define_method(cNArray, "row_major?", na_row_major_p, 0); - rb_define_method(cNArray, "byte_swapped?", na_byte_swapped_p, 0); - rb_define_method(cNArray, "host_order?", na_host_order_p, 0); + rb_define_method(cNArray, "column_major?", cumo_na_column_major_p, 0); + rb_define_method(cNArray, "row_major?", cumo_na_row_major_p, 0); + rb_define_method(cNArray, "byte_swapped?", cumo_na_byte_swapped_p, 0); + rb_define_method(cNArray, "host_order?", cumo_na_host_order_p, 0); - rb_define_method(cNArray, "inplace", na_inplace, 0); - rb_define_method(cNArray, "inplace?", na_inplace_p, 0); - rb_define_method(cNArray, "inplace!", na_inplace_bang, 0); - rb_define_method(cNArray, "out_of_place!", na_out_of_place_bang, 0); + rb_define_method(cNArray, "inplace", cumo_na_inplace, 0); + rb_define_method(cNArray, "inplace?", cumo_na_inplace_p, 0); + rb_define_method(cNArray, "inplace!", cumo_na_inplace_bang, 0); + rb_define_method(cNArray, "out_of_place!", cumo_na_out_of_place_bang, 0); rb_define_alias (cNArray, "not_inplace!", "out_of_place!"); - rb_define_method(cNArray, "==", na_equal, 1); + rb_define_method(cNArray, "==", cumo_na_equal, 1); - id_allocate = rb_intern("allocate"); - id_contiguous_stride = rb_intern(CONTIGUOUS_STRIDE); - //id_element_bit_size = rb_intern(ELEMENT_BIT_SIZE); - id_element_byte_size = rb_intern(ELEMENT_BYTE_SIZE); + cumo_id_allocate = rb_intern("allocate"); + cumo_id_contiguous_stride = rb_intern("CONTIGUOUS_STRIDE"); + //cumo_id_element_bit_size = rb_intern("ELEMENT_BIT_SIZE"); + cumo_id_element_byte_size = rb_intern("ELEMENT_BYTE_SIZE"); - id_fill = rb_intern("fill"); - id_seq = rb_intern("seq"); - id_logseq = rb_intern("logseq"); - id_eye = rb_intern("eye"); - id_UPCAST = rb_intern("UPCAST"); - id_cast = rb_intern("cast"); - id_dup = rb_intern("dup"); - id_to_host = rb_intern("to_host"); - id_bracket = rb_intern("[]"); - id_shift_left = rb_intern("<<"); - id_eq = rb_intern("eq"); - id_count_false = rb_intern("count_false"); - id_count_false_cpu = rb_intern("count_false_cpu"); - id_axis = rb_intern("axis"); - id_nan = rb_intern("nan"); - id_keepdims = rb_intern("keepdims"); + cumo_id_fill = rb_intern("fill"); + cumo_id_seq = rb_intern("seq"); + cumo_id_logseq = rb_intern("logseq"); + cumo_id_eye = rb_intern("eye"); + cumo_id_UPCAST = rb_intern("UPCAST"); + cumo_id_cast = rb_intern("cast"); + cumo_id_dup = rb_intern("dup"); + cumo_id_to_host = rb_intern("to_host"); + cumo_id_bracket = rb_intern("[]"); + cumo_id_shift_left = rb_intern("<<"); + cumo_id_eq = rb_intern("eq"); + cumo_id_count_false = rb_intern("count_false"); + cumo_id_count_false_cpu = rb_intern("count_false_cpu"); + cumo_id_axis = rb_intern("axis"); + cumo_id_nan = rb_intern("nan"); + cumo_id_keepdims = rb_intern("keepdims"); - sym_reduce = ID2SYM(rb_intern("reduce")); - sym_option = ID2SYM(rb_intern("option")); - sym_loop_opt = ID2SYM(rb_intern("loop_opt")); - sym_init = ID2SYM(rb_intern("init")); + cumo_sym_reduce = ID2SYM(rb_intern("reduce")); + cumo_sym_option = ID2SYM(rb_intern("option")); + cumo_sym_loop_opt = ID2SYM(rb_intern("loop_opt")); + cumo_sym_init = ID2SYM(rb_intern("init")); }