cumo 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +27 -0
- data/.travis.yml +5 -0
- data/3rd_party/mkmf-cu/.gitignore +36 -0
- data/3rd_party/mkmf-cu/Gemfile +3 -0
- data/3rd_party/mkmf-cu/LICENSE +21 -0
- data/3rd_party/mkmf-cu/README.md +36 -0
- data/3rd_party/mkmf-cu/Rakefile +11 -0
- data/3rd_party/mkmf-cu/bin/mkmf-cu-nvcc +4 -0
- data/3rd_party/mkmf-cu/lib/mkmf-cu.rb +32 -0
- data/3rd_party/mkmf-cu/lib/mkmf-cu/cli.rb +80 -0
- data/3rd_party/mkmf-cu/lib/mkmf-cu/nvcc.rb +157 -0
- data/3rd_party/mkmf-cu/mkmf-cu.gemspec +16 -0
- data/3rd_party/mkmf-cu/test/test_mkmf-cu.rb +67 -0
- data/CODE_OF_CONDUCT.md +46 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +82 -0
- data/README.md +252 -0
- data/Rakefile +43 -0
- data/bench/broadcast_fp32.rb +138 -0
- data/bench/cumo_bench.rb +193 -0
- data/bench/numo_bench.rb +138 -0
- data/bench/reduction_fp32.rb +117 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/cumo.gemspec +32 -0
- data/ext/cumo/cuda/cublas.c +278 -0
- data/ext/cumo/cuda/driver.c +421 -0
- data/ext/cumo/cuda/memory_pool.cpp +185 -0
- data/ext/cumo/cuda/memory_pool_impl.cpp +308 -0
- data/ext/cumo/cuda/memory_pool_impl.hpp +370 -0
- data/ext/cumo/cuda/memory_pool_impl_test.cpp +554 -0
- data/ext/cumo/cuda/nvrtc.c +207 -0
- data/ext/cumo/cuda/runtime.c +167 -0
- data/ext/cumo/cumo.c +148 -0
- data/ext/cumo/depend.erb +58 -0
- data/ext/cumo/extconf.rb +179 -0
- data/ext/cumo/include/cumo.h +25 -0
- data/ext/cumo/include/cumo/compat.h +23 -0
- data/ext/cumo/include/cumo/cuda/cublas.h +153 -0
- data/ext/cumo/include/cumo/cuda/cumo_thrust.hpp +187 -0
- data/ext/cumo/include/cumo/cuda/cumo_thrust_complex.hpp +79 -0
- data/ext/cumo/include/cumo/cuda/driver.h +22 -0
- data/ext/cumo/include/cumo/cuda/memory_pool.h +28 -0
- data/ext/cumo/include/cumo/cuda/nvrtc.h +22 -0
- data/ext/cumo/include/cumo/cuda/runtime.h +40 -0
- data/ext/cumo/include/cumo/indexer.h +238 -0
- data/ext/cumo/include/cumo/intern.h +142 -0
- data/ext/cumo/include/cumo/intern_fwd.h +38 -0
- data/ext/cumo/include/cumo/intern_kernel.h +6 -0
- data/ext/cumo/include/cumo/narray.h +429 -0
- data/ext/cumo/include/cumo/narray_kernel.h +149 -0
- data/ext/cumo/include/cumo/ndloop.h +95 -0
- data/ext/cumo/include/cumo/reduce_kernel.h +126 -0
- data/ext/cumo/include/cumo/template.h +158 -0
- data/ext/cumo/include/cumo/template_kernel.h +77 -0
- data/ext/cumo/include/cumo/types/bit.h +40 -0
- data/ext/cumo/include/cumo/types/bit_kernel.h +34 -0
- data/ext/cumo/include/cumo/types/complex.h +402 -0
- data/ext/cumo/include/cumo/types/complex_kernel.h +414 -0
- data/ext/cumo/include/cumo/types/complex_macro.h +382 -0
- data/ext/cumo/include/cumo/types/complex_macro_kernel.h +186 -0
- data/ext/cumo/include/cumo/types/dcomplex.h +46 -0
- data/ext/cumo/include/cumo/types/dcomplex_kernel.h +13 -0
- data/ext/cumo/include/cumo/types/dfloat.h +47 -0
- data/ext/cumo/include/cumo/types/dfloat_kernel.h +14 -0
- data/ext/cumo/include/cumo/types/float_def.h +34 -0
- data/ext/cumo/include/cumo/types/float_def_kernel.h +39 -0
- data/ext/cumo/include/cumo/types/float_macro.h +191 -0
- data/ext/cumo/include/cumo/types/float_macro_kernel.h +158 -0
- data/ext/cumo/include/cumo/types/int16.h +24 -0
- data/ext/cumo/include/cumo/types/int16_kernel.h +23 -0
- data/ext/cumo/include/cumo/types/int32.h +24 -0
- data/ext/cumo/include/cumo/types/int32_kernel.h +19 -0
- data/ext/cumo/include/cumo/types/int64.h +24 -0
- data/ext/cumo/include/cumo/types/int64_kernel.h +19 -0
- data/ext/cumo/include/cumo/types/int8.h +24 -0
- data/ext/cumo/include/cumo/types/int8_kernel.h +19 -0
- data/ext/cumo/include/cumo/types/int_macro.h +67 -0
- data/ext/cumo/include/cumo/types/int_macro_kernel.h +48 -0
- data/ext/cumo/include/cumo/types/real_accum.h +486 -0
- data/ext/cumo/include/cumo/types/real_accum_kernel.h +101 -0
- data/ext/cumo/include/cumo/types/robj_macro.h +80 -0
- data/ext/cumo/include/cumo/types/robj_macro_kernel.h +0 -0
- data/ext/cumo/include/cumo/types/robject.h +27 -0
- data/ext/cumo/include/cumo/types/robject_kernel.h +7 -0
- data/ext/cumo/include/cumo/types/scomplex.h +46 -0
- data/ext/cumo/include/cumo/types/scomplex_kernel.h +13 -0
- data/ext/cumo/include/cumo/types/sfloat.h +48 -0
- data/ext/cumo/include/cumo/types/sfloat_kernel.h +14 -0
- data/ext/cumo/include/cumo/types/uint16.h +25 -0
- data/ext/cumo/include/cumo/types/uint16_kernel.h +20 -0
- data/ext/cumo/include/cumo/types/uint32.h +25 -0
- data/ext/cumo/include/cumo/types/uint32_kernel.h +20 -0
- data/ext/cumo/include/cumo/types/uint64.h +25 -0
- data/ext/cumo/include/cumo/types/uint64_kernel.h +20 -0
- data/ext/cumo/include/cumo/types/uint8.h +25 -0
- data/ext/cumo/include/cumo/types/uint8_kernel.h +20 -0
- data/ext/cumo/include/cumo/types/uint_macro.h +58 -0
- data/ext/cumo/include/cumo/types/uint_macro_kernel.h +38 -0
- data/ext/cumo/include/cumo/types/xint_macro.h +169 -0
- data/ext/cumo/include/cumo/types/xint_macro_kernel.h +88 -0
- data/ext/cumo/narray/SFMT-params.h +97 -0
- data/ext/cumo/narray/SFMT-params19937.h +46 -0
- data/ext/cumo/narray/SFMT.c +620 -0
- data/ext/cumo/narray/SFMT.h +167 -0
- data/ext/cumo/narray/array.c +638 -0
- data/ext/cumo/narray/data.c +961 -0
- data/ext/cumo/narray/gen/cogen.rb +56 -0
- data/ext/cumo/narray/gen/cogen_kernel.rb +58 -0
- data/ext/cumo/narray/gen/def/bit.rb +37 -0
- data/ext/cumo/narray/gen/def/dcomplex.rb +39 -0
- data/ext/cumo/narray/gen/def/dfloat.rb +37 -0
- data/ext/cumo/narray/gen/def/int16.rb +36 -0
- data/ext/cumo/narray/gen/def/int32.rb +36 -0
- data/ext/cumo/narray/gen/def/int64.rb +36 -0
- data/ext/cumo/narray/gen/def/int8.rb +36 -0
- data/ext/cumo/narray/gen/def/robject.rb +37 -0
- data/ext/cumo/narray/gen/def/scomplex.rb +39 -0
- data/ext/cumo/narray/gen/def/sfloat.rb +37 -0
- data/ext/cumo/narray/gen/def/uint16.rb +36 -0
- data/ext/cumo/narray/gen/def/uint32.rb +36 -0
- data/ext/cumo/narray/gen/def/uint64.rb +36 -0
- data/ext/cumo/narray/gen/def/uint8.rb +36 -0
- data/ext/cumo/narray/gen/erbpp2.rb +346 -0
- data/ext/cumo/narray/gen/narray_def.rb +268 -0
- data/ext/cumo/narray/gen/spec.rb +425 -0
- data/ext/cumo/narray/gen/tmpl/accum.c +86 -0
- data/ext/cumo/narray/gen/tmpl/accum_binary.c +121 -0
- data/ext/cumo/narray/gen/tmpl/accum_binary_kernel.cu +61 -0
- data/ext/cumo/narray/gen/tmpl/accum_index.c +119 -0
- data/ext/cumo/narray/gen/tmpl/accum_index_kernel.cu +66 -0
- data/ext/cumo/narray/gen/tmpl/accum_kernel.cu +12 -0
- data/ext/cumo/narray/gen/tmpl/alloc_func.c +107 -0
- data/ext/cumo/narray/gen/tmpl/allocate.c +37 -0
- data/ext/cumo/narray/gen/tmpl/aref.c +66 -0
- data/ext/cumo/narray/gen/tmpl/aref_cpu.c +50 -0
- data/ext/cumo/narray/gen/tmpl/aset.c +56 -0
- data/ext/cumo/narray/gen/tmpl/binary.c +162 -0
- data/ext/cumo/narray/gen/tmpl/binary2.c +70 -0
- data/ext/cumo/narray/gen/tmpl/binary2_kernel.cu +15 -0
- data/ext/cumo/narray/gen/tmpl/binary_kernel.cu +31 -0
- data/ext/cumo/narray/gen/tmpl/binary_s.c +45 -0
- data/ext/cumo/narray/gen/tmpl/binary_s_kernel.cu +15 -0
- data/ext/cumo/narray/gen/tmpl/bincount.c +181 -0
- data/ext/cumo/narray/gen/tmpl/cast.c +44 -0
- data/ext/cumo/narray/gen/tmpl/cast_array.c +13 -0
- data/ext/cumo/narray/gen/tmpl/class.c +9 -0
- data/ext/cumo/narray/gen/tmpl/class_kernel.cu +6 -0
- data/ext/cumo/narray/gen/tmpl/clip.c +121 -0
- data/ext/cumo/narray/gen/tmpl/coerce_cast.c +10 -0
- data/ext/cumo/narray/gen/tmpl/complex_accum_kernel.cu +129 -0
- data/ext/cumo/narray/gen/tmpl/cond_binary.c +68 -0
- data/ext/cumo/narray/gen/tmpl/cond_binary_kernel.cu +18 -0
- data/ext/cumo/narray/gen/tmpl/cond_unary.c +46 -0
- data/ext/cumo/narray/gen/tmpl/cum.c +50 -0
- data/ext/cumo/narray/gen/tmpl/each.c +47 -0
- data/ext/cumo/narray/gen/tmpl/each_with_index.c +70 -0
- data/ext/cumo/narray/gen/tmpl/ewcomp.c +79 -0
- data/ext/cumo/narray/gen/tmpl/ewcomp_kernel.cu +19 -0
- data/ext/cumo/narray/gen/tmpl/extract.c +22 -0
- data/ext/cumo/narray/gen/tmpl/extract_cpu.c +26 -0
- data/ext/cumo/narray/gen/tmpl/extract_data.c +53 -0
- data/ext/cumo/narray/gen/tmpl/eye.c +105 -0
- data/ext/cumo/narray/gen/tmpl/eye_kernel.cu +19 -0
- data/ext/cumo/narray/gen/tmpl/fill.c +52 -0
- data/ext/cumo/narray/gen/tmpl/fill_kernel.cu +29 -0
- data/ext/cumo/narray/gen/tmpl/float_accum_kernel.cu +106 -0
- data/ext/cumo/narray/gen/tmpl/format.c +62 -0
- data/ext/cumo/narray/gen/tmpl/format_to_a.c +49 -0
- data/ext/cumo/narray/gen/tmpl/frexp.c +38 -0
- data/ext/cumo/narray/gen/tmpl/gemm.c +203 -0
- data/ext/cumo/narray/gen/tmpl/init_class.c +20 -0
- data/ext/cumo/narray/gen/tmpl/init_module.c +12 -0
- data/ext/cumo/narray/gen/tmpl/inspect.c +21 -0
- data/ext/cumo/narray/gen/tmpl/lib.c +50 -0
- data/ext/cumo/narray/gen/tmpl/lib_kernel.cu +24 -0
- data/ext/cumo/narray/gen/tmpl/logseq.c +102 -0
- data/ext/cumo/narray/gen/tmpl/logseq_kernel.cu +31 -0
- data/ext/cumo/narray/gen/tmpl/map_with_index.c +98 -0
- data/ext/cumo/narray/gen/tmpl/median.c +66 -0
- data/ext/cumo/narray/gen/tmpl/minmax.c +47 -0
- data/ext/cumo/narray/gen/tmpl/module.c +9 -0
- data/ext/cumo/narray/gen/tmpl/module_kernel.cu +1 -0
- data/ext/cumo/narray/gen/tmpl/new_dim0.c +15 -0
- data/ext/cumo/narray/gen/tmpl/new_dim0_kernel.cu +8 -0
- data/ext/cumo/narray/gen/tmpl/poly.c +50 -0
- data/ext/cumo/narray/gen/tmpl/pow.c +97 -0
- data/ext/cumo/narray/gen/tmpl/pow_kernel.cu +29 -0
- data/ext/cumo/narray/gen/tmpl/powint.c +17 -0
- data/ext/cumo/narray/gen/tmpl/qsort.c +212 -0
- data/ext/cumo/narray/gen/tmpl/rand.c +168 -0
- data/ext/cumo/narray/gen/tmpl/rand_norm.c +121 -0
- data/ext/cumo/narray/gen/tmpl/real_accum_kernel.cu +75 -0
- data/ext/cumo/narray/gen/tmpl/seq.c +112 -0
- data/ext/cumo/narray/gen/tmpl/seq_kernel.cu +43 -0
- data/ext/cumo/narray/gen/tmpl/set2.c +57 -0
- data/ext/cumo/narray/gen/tmpl/sort.c +48 -0
- data/ext/cumo/narray/gen/tmpl/sort_index.c +111 -0
- data/ext/cumo/narray/gen/tmpl/store.c +41 -0
- data/ext/cumo/narray/gen/tmpl/store_array.c +187 -0
- data/ext/cumo/narray/gen/tmpl/store_array_kernel.cu +58 -0
- data/ext/cumo/narray/gen/tmpl/store_bit.c +86 -0
- data/ext/cumo/narray/gen/tmpl/store_bit_kernel.cu +66 -0
- data/ext/cumo/narray/gen/tmpl/store_from.c +81 -0
- data/ext/cumo/narray/gen/tmpl/store_from_kernel.cu +58 -0
- data/ext/cumo/narray/gen/tmpl/store_kernel.cu +3 -0
- data/ext/cumo/narray/gen/tmpl/store_numeric.c +9 -0
- data/ext/cumo/narray/gen/tmpl/to_a.c +43 -0
- data/ext/cumo/narray/gen/tmpl/unary.c +132 -0
- data/ext/cumo/narray/gen/tmpl/unary2.c +60 -0
- data/ext/cumo/narray/gen/tmpl/unary_kernel.cu +72 -0
- data/ext/cumo/narray/gen/tmpl/unary_ret2.c +34 -0
- data/ext/cumo/narray/gen/tmpl/unary_s.c +86 -0
- data/ext/cumo/narray/gen/tmpl/unary_s_kernel.cu +58 -0
- data/ext/cumo/narray/gen/tmpl_bit/allocate.c +24 -0
- data/ext/cumo/narray/gen/tmpl_bit/aref.c +54 -0
- data/ext/cumo/narray/gen/tmpl_bit/aref_cpu.c +57 -0
- data/ext/cumo/narray/gen/tmpl_bit/aset.c +56 -0
- data/ext/cumo/narray/gen/tmpl_bit/binary.c +98 -0
- data/ext/cumo/narray/gen/tmpl_bit/bit_count.c +64 -0
- data/ext/cumo/narray/gen/tmpl_bit/bit_count_cpu.c +88 -0
- data/ext/cumo/narray/gen/tmpl_bit/bit_count_kernel.cu +76 -0
- data/ext/cumo/narray/gen/tmpl_bit/bit_reduce.c +133 -0
- data/ext/cumo/narray/gen/tmpl_bit/each.c +48 -0
- data/ext/cumo/narray/gen/tmpl_bit/each_with_index.c +70 -0
- data/ext/cumo/narray/gen/tmpl_bit/extract.c +30 -0
- data/ext/cumo/narray/gen/tmpl_bit/extract_cpu.c +29 -0
- data/ext/cumo/narray/gen/tmpl_bit/fill.c +69 -0
- data/ext/cumo/narray/gen/tmpl_bit/format.c +64 -0
- data/ext/cumo/narray/gen/tmpl_bit/format_to_a.c +51 -0
- data/ext/cumo/narray/gen/tmpl_bit/inspect.c +21 -0
- data/ext/cumo/narray/gen/tmpl_bit/mask.c +136 -0
- data/ext/cumo/narray/gen/tmpl_bit/none_p.c +14 -0
- data/ext/cumo/narray/gen/tmpl_bit/store_array.c +108 -0
- data/ext/cumo/narray/gen/tmpl_bit/store_bit.c +70 -0
- data/ext/cumo/narray/gen/tmpl_bit/store_from.c +60 -0
- data/ext/cumo/narray/gen/tmpl_bit/to_a.c +47 -0
- data/ext/cumo/narray/gen/tmpl_bit/unary.c +81 -0
- data/ext/cumo/narray/gen/tmpl_bit/where.c +90 -0
- data/ext/cumo/narray/gen/tmpl_bit/where2.c +95 -0
- data/ext/cumo/narray/index.c +880 -0
- data/ext/cumo/narray/kwargs.c +153 -0
- data/ext/cumo/narray/math.c +142 -0
- data/ext/cumo/narray/narray.c +1948 -0
- data/ext/cumo/narray/ndloop.c +2105 -0
- data/ext/cumo/narray/rand.c +45 -0
- data/ext/cumo/narray/step.c +474 -0
- data/ext/cumo/narray/struct.c +886 -0
- data/lib/cumo.rb +3 -0
- data/lib/cumo/cuda.rb +11 -0
- data/lib/cumo/cuda/compile_error.rb +36 -0
- data/lib/cumo/cuda/compiler.rb +161 -0
- data/lib/cumo/cuda/device.rb +47 -0
- data/lib/cumo/cuda/link_state.rb +31 -0
- data/lib/cumo/cuda/module.rb +40 -0
- data/lib/cumo/cuda/nvrtc_program.rb +27 -0
- data/lib/cumo/linalg.rb +12 -0
- data/lib/cumo/narray.rb +2 -0
- data/lib/cumo/narray/extra.rb +1278 -0
- data/lib/erbpp.rb +294 -0
- data/lib/erbpp/line_number.rb +137 -0
- data/lib/erbpp/narray_def.rb +381 -0
- data/numo-narray-version +1 -0
- data/run.gdb +7 -0
- metadata +353 -0
@@ -0,0 +1,886 @@
|
|
1
|
+
#include <ruby.h>
|
2
|
+
#include "cumo/narray.h"
|
3
|
+
#include "cumo/template.h"
|
4
|
+
#include "cumo/cuda/memory_pool.h"
|
5
|
+
#include "cumo/cuda/runtime.h"
|
6
|
+
|
7
|
+
#define cT cumo_cStruct
|
8
|
+
VALUE cT;
|
9
|
+
|
10
|
+
static VALUE
|
11
|
+
nst_allocate(VALUE self)
|
12
|
+
{
|
13
|
+
narray_t *na;
|
14
|
+
void *ptr;
|
15
|
+
VALUE velmsz;
|
16
|
+
|
17
|
+
GetNArray(self,na);
|
18
|
+
|
19
|
+
switch(NA_TYPE(na)) {
|
20
|
+
case NARRAY_DATA_T:
|
21
|
+
ptr = NA_DATA_PTR(na);
|
22
|
+
if (na->size > 0 && ptr == NULL) {
|
23
|
+
velmsz = rb_const_get(CLASS_OF(self), rb_intern("element_byte_size"));
|
24
|
+
ptr = cumo_cuda_runtime_malloc(NUM2SIZET(velmsz) * na->size);
|
25
|
+
NA_DATA_PTR(na) = ptr;
|
26
|
+
}
|
27
|
+
break;
|
28
|
+
case NARRAY_VIEW_T:
|
29
|
+
rb_funcall(NA_VIEW_DATA(na), rb_intern("allocate"), 0);
|
30
|
+
break;
|
31
|
+
case NARRAY_FILEMAP_T:
|
32
|
+
//ptr = ((narray_filemap_t*)na)->ptr;
|
33
|
+
// to be implemented
|
34
|
+
default:
|
35
|
+
rb_bug("invalid narray type : %d",NA_TYPE(na));
|
36
|
+
}
|
37
|
+
return self;
|
38
|
+
}
|
39
|
+
|
40
|
+
|
41
|
+
static inline VALUE
|
42
|
+
nst_definitions(VALUE nst)
|
43
|
+
{
|
44
|
+
return rb_const_get(nst, rb_intern("DEFINITIONS"));
|
45
|
+
}
|
46
|
+
|
47
|
+
static VALUE
|
48
|
+
nst_definition(VALUE nst, VALUE idx)
|
49
|
+
{
|
50
|
+
long i;
|
51
|
+
VALUE def = nst_definitions(CLASS_OF(nst));
|
52
|
+
long len = RARRAY_LEN(def);
|
53
|
+
|
54
|
+
if (TYPE(idx) == T_STRING || TYPE(idx) == T_SYMBOL) {
|
55
|
+
ID id = rb_to_id(idx);
|
56
|
+
for (i=0; i<len; i++) {
|
57
|
+
VALUE key = RARRAY_AREF(RARRAY_AREF(def,i),0);
|
58
|
+
if (SYM2ID(key) == id) {
|
59
|
+
return RARRAY_AREF(def,i);
|
60
|
+
}
|
61
|
+
}
|
62
|
+
} else if (rb_obj_is_kind_of(idx,rb_cNumeric)) {
|
63
|
+
i = NUM2LONG(idx);
|
64
|
+
if (i<-len || i>=len) {
|
65
|
+
rb_raise(rb_eIndexError,"offset %ld out of range of struct(size:%ld)", i, len);
|
66
|
+
}
|
67
|
+
return RARRAY_AREF(def,i);
|
68
|
+
}
|
69
|
+
return Qnil;
|
70
|
+
}
|
71
|
+
|
72
|
+
|
73
|
+
|
74
|
+
void na_copy_array_structure(VALUE self, VALUE view);
|
75
|
+
|
76
|
+
static VALUE
|
77
|
+
na_make_view_struct(VALUE self, VALUE dtype, VALUE offset)
|
78
|
+
{
|
79
|
+
size_t i, n;
|
80
|
+
int j, k, ndim;
|
81
|
+
size_t *shape;
|
82
|
+
size_t *idx1, *idx2;
|
83
|
+
ssize_t stride;
|
84
|
+
stridx_t *stridx;
|
85
|
+
narray_t *na, *nt;
|
86
|
+
narray_view_t *na1, *na2;
|
87
|
+
VALUE klass;
|
88
|
+
volatile VALUE view;
|
89
|
+
|
90
|
+
GetNArray(self,na);
|
91
|
+
|
92
|
+
// build from Cumo::Struct
|
93
|
+
if (rb_obj_is_kind_of(dtype,cNArray)) {
|
94
|
+
GetNArray(dtype,nt);
|
95
|
+
ndim = na->ndim + nt->ndim;
|
96
|
+
shape = ALLOCA_N(size_t,ndim);
|
97
|
+
// struct dimensions
|
98
|
+
for (j=0; j<na->ndim; j++) {
|
99
|
+
shape[j] = na->shape[j];
|
100
|
+
}
|
101
|
+
// member dimension
|
102
|
+
for (j=na->ndim,k=0; j<ndim; j++,k++) {
|
103
|
+
shape[j] = nt->shape[k];
|
104
|
+
}
|
105
|
+
klass = CLASS_OF(dtype);
|
106
|
+
stridx = ALLOC_N(stridx_t, ndim);
|
107
|
+
stride = na_dtype_elmsz(klass);
|
108
|
+
for (j=ndim,k=nt->ndim; k; ) {
|
109
|
+
SDX_SET_STRIDE(stridx[--j],stride);
|
110
|
+
stride *= nt->shape[--k];
|
111
|
+
}
|
112
|
+
} else {
|
113
|
+
ndim = na->ndim;
|
114
|
+
shape = ALLOCA_N(size_t,ndim);
|
115
|
+
for (j=0; j<ndim; j++) {
|
116
|
+
shape[j] = na->shape[j];
|
117
|
+
}
|
118
|
+
klass = CLASS_OF(self);
|
119
|
+
if (TYPE(dtype)==T_CLASS) {
|
120
|
+
if (RTEST(rb_class_inherited_p(dtype,cNArray))) {
|
121
|
+
klass = dtype;
|
122
|
+
}
|
123
|
+
}
|
124
|
+
stridx = ALLOC_N(stridx_t, ndim);
|
125
|
+
}
|
126
|
+
|
127
|
+
view = na_s_allocate_view(klass);
|
128
|
+
na_copy_flags(self, view);
|
129
|
+
GetNArrayView(view, na2);
|
130
|
+
na_setup_shape((narray_t*)na2, ndim, shape);
|
131
|
+
na2->stridx = stridx;
|
132
|
+
|
133
|
+
switch(na->type) {
|
134
|
+
case NARRAY_DATA_T:
|
135
|
+
case NARRAY_FILEMAP_T:
|
136
|
+
stride = nary_element_stride(self);
|
137
|
+
for (j=na->ndim; j--;) {
|
138
|
+
SDX_SET_STRIDE(na2->stridx[j], stride);
|
139
|
+
stride *= na->shape[j];
|
140
|
+
}
|
141
|
+
na2->offset = 0;
|
142
|
+
na2->data = self;
|
143
|
+
break;
|
144
|
+
case NARRAY_VIEW_T:
|
145
|
+
GetNArrayView(self, na1);
|
146
|
+
for (j=na1->base.ndim; j--; ) {
|
147
|
+
if (SDX_IS_INDEX(na1->stridx[j])) {
|
148
|
+
n = na1->base.shape[j];
|
149
|
+
idx1 = SDX_GET_INDEX(na1->stridx[j]);
|
150
|
+
idx2 = ALLOC_N(size_t, na1->base.shape[j]);
|
151
|
+
for (i=0; i<n; i++) {
|
152
|
+
idx2[i] = idx1[i];
|
153
|
+
}
|
154
|
+
SDX_SET_INDEX(na2->stridx[j],idx2);
|
155
|
+
} else {
|
156
|
+
na2->stridx[j] = na1->stridx[j];
|
157
|
+
}
|
158
|
+
}
|
159
|
+
na2->offset = na1->offset;
|
160
|
+
na2->data = na1->data;
|
161
|
+
break;
|
162
|
+
}
|
163
|
+
|
164
|
+
if (RTEST(offset)) {
|
165
|
+
na2->offset += NUM2SIZET(offset);
|
166
|
+
}
|
167
|
+
|
168
|
+
return view;
|
169
|
+
}
|
170
|
+
|
171
|
+
|
172
|
+
static VALUE
|
173
|
+
nst_field_view(VALUE self, VALUE idx)
|
174
|
+
{
|
175
|
+
VALUE def, type, ofs;
|
176
|
+
|
177
|
+
def = nst_definition(self, idx);
|
178
|
+
if (!RTEST(def)) {
|
179
|
+
idx = rb_funcall(idx, rb_intern("to_s"), 0);
|
180
|
+
rb_raise(rb_eTypeError, "Invalid field: '%s' for struct %s",
|
181
|
+
StringValuePtr(idx), rb_class2name(CLASS_OF(self)));
|
182
|
+
}
|
183
|
+
type = RARRAY_AREF(def,1);
|
184
|
+
ofs = RARRAY_AREF(def,2);
|
185
|
+
return na_make_view_struct(self, type, ofs);
|
186
|
+
}
|
187
|
+
|
188
|
+
static VALUE
|
189
|
+
nst_field(VALUE self, VALUE idx)
|
190
|
+
{
|
191
|
+
VALUE obj;
|
192
|
+
narray_view_t *nv;
|
193
|
+
|
194
|
+
obj = nst_field_view(self,idx);
|
195
|
+
GetNArrayView(obj,nv);
|
196
|
+
if (nv->base.ndim==0) {
|
197
|
+
obj = rb_funcall(obj,rb_intern("extract_cpu"),0);
|
198
|
+
}
|
199
|
+
return obj;
|
200
|
+
}
|
201
|
+
|
202
|
+
static VALUE
|
203
|
+
nst_field_set(VALUE self, VALUE idx, VALUE other)
|
204
|
+
{
|
205
|
+
VALUE obj;
|
206
|
+
|
207
|
+
obj = nst_field_view(self,idx);
|
208
|
+
rb_funcall(obj,rb_intern("store"),1,other);
|
209
|
+
return other;
|
210
|
+
}
|
211
|
+
|
212
|
+
|
213
|
+
static VALUE
|
214
|
+
nst_method_missing(int argc, VALUE *argv, VALUE self)
|
215
|
+
{
|
216
|
+
VALUE s, tag, obj;
|
217
|
+
|
218
|
+
if (argc == 2) {
|
219
|
+
s = rb_sym_to_s(argv[0]);
|
220
|
+
if (RSTRING_PTR(s)[RSTRING_LEN(s)-1] == '=') {
|
221
|
+
tag = rb_str_intern(rb_str_new(RSTRING_PTR(s), RSTRING_LEN(s)-1));
|
222
|
+
obj = nst_field(self, tag);
|
223
|
+
if (RTEST(obj)) {
|
224
|
+
rb_funcall(obj, rb_intern("store"), 1, argv[1]);
|
225
|
+
return argv[1];
|
226
|
+
}
|
227
|
+
}
|
228
|
+
return rb_call_super(argc,argv);
|
229
|
+
}
|
230
|
+
if (argc == 1) {
|
231
|
+
obj = nst_field(self,argv[0]);
|
232
|
+
if (RTEST(obj)) {
|
233
|
+
return obj;
|
234
|
+
}
|
235
|
+
}
|
236
|
+
return rb_call_super(argc,argv);
|
237
|
+
}
|
238
|
+
|
239
|
+
|
240
|
+
/*
|
241
|
+
Foo = Cumo::Struct.new {
|
242
|
+
int8 :byte
|
243
|
+
float64 :float, [2,2]
|
244
|
+
dcomplex :compl
|
245
|
+
}
|
246
|
+
*/
|
247
|
+
static VALUE
|
248
|
+
nst_s_new(int argc, VALUE *argv, VALUE klass)
|
249
|
+
{
|
250
|
+
VALUE name=Qnil, rest, size;
|
251
|
+
VALUE st, members;
|
252
|
+
ID id;
|
253
|
+
|
254
|
+
rb_scan_args(argc, argv, "0*", &rest);
|
255
|
+
if (RARRAY_LEN(rest)>0) {
|
256
|
+
name = RARRAY_AREF(rest,0);
|
257
|
+
if (!NIL_P(name)) {
|
258
|
+
VALUE tmp = rb_check_string_type(name);
|
259
|
+
if (!NIL_P(tmp)) {
|
260
|
+
rb_ary_shift(rest);
|
261
|
+
} else {
|
262
|
+
name = Qnil;
|
263
|
+
}
|
264
|
+
}
|
265
|
+
}
|
266
|
+
|
267
|
+
if (NIL_P(name)) {
|
268
|
+
st = rb_define_class_id(name, klass);
|
269
|
+
rb_funcall(klass, rb_intern("inherited"), 1, st);
|
270
|
+
}
|
271
|
+
else {
|
272
|
+
char *cname = StringValuePtr(name);
|
273
|
+
id = rb_intern(cname);
|
274
|
+
if (!rb_is_const_id(id)) {
|
275
|
+
rb_name_error(id, "identifier %s needs to be constant", cname);
|
276
|
+
}
|
277
|
+
if (rb_const_defined_at(klass, id)) {
|
278
|
+
rb_warn("redefining constant Struct::%s", cname);
|
279
|
+
rb_mod_remove_const(klass, ID2SYM(id));
|
280
|
+
}
|
281
|
+
st = rb_define_class_under(klass, rb_id2name(id), klass);
|
282
|
+
}
|
283
|
+
|
284
|
+
rb_iv_set(st, "__members__", rb_ary_new());
|
285
|
+
rb_iv_set(st, "__offset__", INT2FIX(0));
|
286
|
+
|
287
|
+
if (rb_block_given_p()) {
|
288
|
+
rb_mod_module_eval(0, 0, st);
|
289
|
+
}
|
290
|
+
|
291
|
+
size = rb_iv_get(st, "__offset__");
|
292
|
+
members = rb_iv_get(st, "__members__");
|
293
|
+
//printf("size=%d\n",NUM2INT(size));
|
294
|
+
rb_define_const(st, CONTIGUOUS_STRIDE, size);
|
295
|
+
rb_define_const(st, ELEMENT_BYTE_SIZE, size);
|
296
|
+
rb_define_const(st, ELEMENT_BIT_SIZE, rb_funcall(size,'*',1,INT2FIX(8)));
|
297
|
+
|
298
|
+
OBJ_FREEZE(members);
|
299
|
+
rb_define_const(st, "DEFINITIONS", members);
|
300
|
+
|
301
|
+
rb_define_singleton_method(st, "new", rb_class_new_instance, -1);
|
302
|
+
//rb_define_singleton_method(st, "[]", rb_class_new_instance, -1);
|
303
|
+
rb_define_method(st, "allocate", nst_allocate, 0);
|
304
|
+
|
305
|
+
return st;
|
306
|
+
}
|
307
|
+
|
308
|
+
|
309
|
+
static VALUE
|
310
|
+
nstruct_add_type(VALUE type, int argc, VALUE *argv, VALUE nst)
|
311
|
+
{
|
312
|
+
VALUE ofs, size;
|
313
|
+
ID id;
|
314
|
+
int i;
|
315
|
+
VALUE name=Qnil;
|
316
|
+
size_t *shape=NULL;
|
317
|
+
int ndim=0;
|
318
|
+
ssize_t stride;
|
319
|
+
narray_view_t *nt;
|
320
|
+
int j;
|
321
|
+
|
322
|
+
for (i=0; i<argc; i++) {
|
323
|
+
switch(TYPE(argv[i])) {
|
324
|
+
case T_STRING:
|
325
|
+
case T_SYMBOL:
|
326
|
+
if (NIL_P(name)) {
|
327
|
+
name = argv[i];
|
328
|
+
break;
|
329
|
+
}
|
330
|
+
rb_raise(rb_eArgError,"multiple name in struct definition");
|
331
|
+
case T_ARRAY:
|
332
|
+
if (shape) {
|
333
|
+
rb_raise(rb_eArgError,"multiple shape in struct definition");
|
334
|
+
}
|
335
|
+
ndim = RARRAY_LEN(argv[i]);
|
336
|
+
if (ndim > NA_MAX_DIMENSION) {
|
337
|
+
rb_raise(rb_eArgError,"too large number of dimensions");
|
338
|
+
}
|
339
|
+
if (ndim == 0) {
|
340
|
+
rb_raise(rb_eArgError,"array is empty");
|
341
|
+
}
|
342
|
+
shape = ALLOCA_N(size_t, ndim);
|
343
|
+
na_array_to_internal_shape(Qnil, argv[i], shape);
|
344
|
+
break;
|
345
|
+
}
|
346
|
+
}
|
347
|
+
|
348
|
+
id = rb_to_id(name);
|
349
|
+
name = ID2SYM(id);
|
350
|
+
if (rb_obj_is_kind_of(type,cNArray)) {
|
351
|
+
narray_t *na;
|
352
|
+
GetNArray(type,na);
|
353
|
+
type = CLASS_OF(type);
|
354
|
+
ndim = na->ndim;
|
355
|
+
shape = na->shape;
|
356
|
+
}
|
357
|
+
type = nary_view_new(type,ndim,shape);
|
358
|
+
GetNArrayView(type,nt);
|
359
|
+
|
360
|
+
nt->stridx = ALLOC_N(stridx_t,ndim);
|
361
|
+
stride = na_dtype_elmsz(CLASS_OF(type));
|
362
|
+
for (j=ndim; j--; ) {
|
363
|
+
SDX_SET_STRIDE(nt->stridx[j], stride);
|
364
|
+
stride *= shape[j];
|
365
|
+
}
|
366
|
+
|
367
|
+
ofs = rb_iv_get(nst, "__offset__");
|
368
|
+
nt->offset = NUM2SIZET(ofs);
|
369
|
+
|
370
|
+
size = rb_funcall(type, rb_intern("byte_size"), 0);
|
371
|
+
rb_iv_set(nst, "__offset__", rb_funcall(ofs,'+',1,size));
|
372
|
+
rb_ary_push(rb_iv_get(nst,"__members__"),
|
373
|
+
rb_ary_new3(4,name,type,ofs,size)); // <- field definition
|
374
|
+
return Qnil;
|
375
|
+
}
|
376
|
+
|
377
|
+
|
378
|
+
static VALUE
|
379
|
+
nst_extract(VALUE self)
|
380
|
+
{
|
381
|
+
return self;
|
382
|
+
}
|
383
|
+
|
384
|
+
|
385
|
+
static void
|
386
|
+
iter_nstruct_to_a(na_loop_t *const lp)
|
387
|
+
{
|
388
|
+
long i, len;
|
389
|
+
VALUE opt, types, defs, def;
|
390
|
+
VALUE elmt, velm, vary;
|
391
|
+
size_t ofs, pos;
|
392
|
+
narray_view_t *ne;
|
393
|
+
|
394
|
+
opt = lp->option;
|
395
|
+
types = RARRAY_AREF(opt,0);
|
396
|
+
defs = RARRAY_AREF(opt,1);
|
397
|
+
pos = lp->args[0].iter[0].pos;
|
398
|
+
|
399
|
+
len = RARRAY_LEN(types);
|
400
|
+
vary = rb_ary_new2(len);
|
401
|
+
|
402
|
+
for (i=0; i<len; i++) {
|
403
|
+
def = RARRAY_AREF(defs,i);
|
404
|
+
ofs = NUM2SIZET(RARRAY_AREF(def,2));
|
405
|
+
//ofs = NUM2SIZET(RARRAY_AREF(ofsts,i));
|
406
|
+
elmt = RARRAY_AREF(types,i);
|
407
|
+
GetNArrayView(elmt,ne);
|
408
|
+
ne->offset = pos + ofs;
|
409
|
+
if (ne->base.ndim==0) {
|
410
|
+
velm = rb_funcall(elmt,rb_intern("extract_cpu"),0);
|
411
|
+
} else {
|
412
|
+
velm = rb_funcall(elmt,rb_intern("to_a"),0);
|
413
|
+
}
|
414
|
+
rb_ary_push(vary, velm);
|
415
|
+
}
|
416
|
+
rb_ary_push(lp->args[1].value, vary);
|
417
|
+
}
|
418
|
+
|
419
|
+
static VALUE
|
420
|
+
na_original_data(VALUE self)
|
421
|
+
{
|
422
|
+
narray_t *na;
|
423
|
+
narray_view_t *nv;
|
424
|
+
|
425
|
+
GetNArray(self,na);
|
426
|
+
switch(na->type) {
|
427
|
+
case NARRAY_VIEW_T:
|
428
|
+
GetNArrayView(self, nv);
|
429
|
+
return nv->data;
|
430
|
+
}
|
431
|
+
return self;
|
432
|
+
}
|
433
|
+
|
434
|
+
static VALUE
|
435
|
+
nst_create_member_views(VALUE self)
|
436
|
+
{
|
437
|
+
VALUE defs, def, types, type, elmt;
|
438
|
+
long i, len;
|
439
|
+
narray_view_t *ne;
|
440
|
+
|
441
|
+
defs = nst_definitions(CLASS_OF(self));
|
442
|
+
len = RARRAY_LEN(defs);
|
443
|
+
types = rb_ary_new2(len);
|
444
|
+
//ofsts = rb_ary_new2(len);
|
445
|
+
for (i=0; i<len; i++) {
|
446
|
+
def = RARRAY_AREF(defs,i);
|
447
|
+
type = RARRAY_AREF(def,1);
|
448
|
+
//ofst = RARRAY_AREF(def,2);
|
449
|
+
elmt = na_make_view(type);
|
450
|
+
rb_ary_push(types, elmt);
|
451
|
+
//rb_ary_push(ofsts, ofst);
|
452
|
+
GetNArrayView(elmt,ne);
|
453
|
+
ne->data = na_original_data(self);
|
454
|
+
}
|
455
|
+
return rb_assoc_new(types,defs);
|
456
|
+
}
|
457
|
+
|
458
|
+
static VALUE
|
459
|
+
nary_struct_to_a(VALUE self)
|
460
|
+
{
|
461
|
+
volatile VALUE opt;
|
462
|
+
ndfunc_arg_in_t ain[3] = {{Qnil,0},{sym_loop_opt},{sym_option}};
|
463
|
+
ndfunc_arg_out_t aout[1] = {{rb_cArray,0}}; // dummy?
|
464
|
+
ndfunc_t ndf = {iter_nstruct_to_a, NO_LOOP, 3, 1, ain, aout};
|
465
|
+
|
466
|
+
opt = nst_create_member_views(self);
|
467
|
+
return na_ndloop_cast_narray_to_rarray(&ndf, self, opt);
|
468
|
+
}
|
469
|
+
|
470
|
+
|
471
|
+
|
472
|
+
/*
|
473
|
+
static size_t
|
474
|
+
check_array(VALUE item) {
|
475
|
+
narray_t *na;
|
476
|
+
|
477
|
+
if (TYPE(item) == T_ARRAY) {
|
478
|
+
return 1;
|
479
|
+
}
|
480
|
+
if (RTEST(rb_obj_is_kind_of(item, cNArray))) {
|
481
|
+
GetNArray(item,na);
|
482
|
+
if (na->ndim == 1) {
|
483
|
+
return 1;
|
484
|
+
} else {
|
485
|
+
return 0;
|
486
|
+
}
|
487
|
+
}
|
488
|
+
return 0;
|
489
|
+
}
|
490
|
+
*/
|
491
|
+
|
492
|
+
/*
|
493
|
+
static size_t
|
494
|
+
check_array_1d(VALUE item, size_t size) {
|
495
|
+
narray_t *na;
|
496
|
+
size_t i, len;
|
497
|
+
|
498
|
+
if (TYPE(item) == T_ARRAY) {
|
499
|
+
len = RARRAY_LEN(item);
|
500
|
+
if (size != len) {
|
501
|
+
return 0;
|
502
|
+
}
|
503
|
+
for (i=0; i<len; i++) {
|
504
|
+
if (!check_array(RARRAY_AREF(item,i))) {
|
505
|
+
return 0;
|
506
|
+
}
|
507
|
+
}
|
508
|
+
return 1;
|
509
|
+
}
|
510
|
+
if (RTEST(rb_obj_is_kind_of(item, cNArray))) {
|
511
|
+
GetNArray(item,na);
|
512
|
+
if (na->ndim == 1 && na->size == size) {
|
513
|
+
return 1;
|
514
|
+
} else {
|
515
|
+
return 0;
|
516
|
+
}
|
517
|
+
}
|
518
|
+
return 0;
|
519
|
+
}
|
520
|
+
*/
|
521
|
+
|
522
|
+
/*
|
523
|
+
VALUE
|
524
|
+
nst_check_compatibility(VALUE nst, VALUE ary)
|
525
|
+
{
|
526
|
+
VALUE defs, def, type, item;
|
527
|
+
long len, i;
|
528
|
+
narray_t *nt;
|
529
|
+
|
530
|
+
if (TYPE(ary) != T_ARRAY) {
|
531
|
+
if (nst==CLASS_OF(ary)) { // same Struct
|
532
|
+
return Qtrue;
|
533
|
+
}
|
534
|
+
return Qfalse;
|
535
|
+
}
|
536
|
+
defs = nst_definitions(nst);
|
537
|
+
len = RARRAY_LEN(defs);
|
538
|
+
|
539
|
+
if (len != RARRAY_LEN(ary)) {
|
540
|
+
//puts("pass2");
|
541
|
+
return Qfalse;
|
542
|
+
}
|
543
|
+
for (i=0; i<len; i++) {
|
544
|
+
def = RARRAY_AREF(defs,i);
|
545
|
+
type = RARRAY_AREF(def,1);
|
546
|
+
GetNArray(type,nt);
|
547
|
+
item = RARRAY_AREF(ary,i);
|
548
|
+
if (nt->ndim == 0) {
|
549
|
+
if (check_array(item)) {
|
550
|
+
//puts("pass3");
|
551
|
+
return Qfalse;
|
552
|
+
}
|
553
|
+
} else if (nt->ndim == 1) {
|
554
|
+
if (!check_array_1d(item, nt->size)) {
|
555
|
+
//puts("pass4");
|
556
|
+
return Qfalse;
|
557
|
+
}
|
558
|
+
} else {
|
559
|
+
// multi-dimension member
|
560
|
+
//volatile VALUE vnc;
|
561
|
+
//na_compose_t *nc;
|
562
|
+
VALUE vnc;
|
563
|
+
narray_t *nc;
|
564
|
+
int j;
|
565
|
+
|
566
|
+
//vnc = na_ary_composition(item);
|
567
|
+
//Data_Get_Struct(vnc, na_compose_t, nc);
|
568
|
+
vnc = na_s_new_like(cNArray, item);
|
569
|
+
GetNArray(vnc,nc);
|
570
|
+
if (nt->ndim != nc->ndim) {
|
571
|
+
return Qfalse;
|
572
|
+
}
|
573
|
+
for (j=0; j<nc->ndim; j++) {
|
574
|
+
if (nc->shape[j] != nt->shape[j]) {
|
575
|
+
return Qfalse;
|
576
|
+
}
|
577
|
+
}
|
578
|
+
return Qtrue;
|
579
|
+
}
|
580
|
+
}
|
581
|
+
return Qtrue;
|
582
|
+
}
|
583
|
+
*/
|
584
|
+
|
585
|
+
|
586
|
+
VALUE na_ary_composition_for_struct(VALUE nstruct, VALUE ary);
|
587
|
+
|
588
|
+
// ------
|
589
|
+
static void
|
590
|
+
iter_nstruct_from_a(na_loop_t *const lp)
|
591
|
+
{
|
592
|
+
long i, len;
|
593
|
+
VALUE ary;
|
594
|
+
VALUE types, defs, def;
|
595
|
+
VALUE elmt, item;
|
596
|
+
size_t ofs;
|
597
|
+
narray_view_t *ne;
|
598
|
+
|
599
|
+
types = RARRAY_AREF(lp->option,0);
|
600
|
+
defs = RARRAY_AREF(lp->option,1);
|
601
|
+
|
602
|
+
len = RARRAY_LEN(types);
|
603
|
+
ary = lp->args[1].value;
|
604
|
+
//rb_p(CLASS_OF(ary));rb_p(ary);
|
605
|
+
|
606
|
+
for (i=0; i<len; i++) {
|
607
|
+
def = RARRAY_AREF(defs,i);
|
608
|
+
ofs = NUM2SIZET(RARRAY_AREF(def,2));
|
609
|
+
elmt = RARRAY_AREF(types,i);
|
610
|
+
GetNArrayView(elmt,ne);
|
611
|
+
ne->offset = lp->args[0].iter[0].pos + ofs;
|
612
|
+
item = RARRAY_AREF(ary,i);
|
613
|
+
//rb_p(ary);
|
614
|
+
//rb_p(item);
|
615
|
+
//rb_p(elmt);
|
616
|
+
//abort();
|
617
|
+
rb_funcall(elmt, rb_intern("store"), 1, item);
|
618
|
+
}
|
619
|
+
}
|
620
|
+
|
621
|
+
static VALUE
|
622
|
+
nary_struct_cast_array(VALUE klass, VALUE rary)
|
623
|
+
{
|
624
|
+
//volatile VALUE vnc, nary;
|
625
|
+
VALUE nary;
|
626
|
+
narray_t *na;
|
627
|
+
//na_compose_t *nc;
|
628
|
+
VALUE opt;
|
629
|
+
ndfunc_arg_in_t ain[3] = {{OVERWRITE,0},{rb_cArray,0},{sym_option}};
|
630
|
+
ndfunc_t ndf = {iter_nstruct_from_a, NO_LOOP, 3, 0, ain, 0};
|
631
|
+
|
632
|
+
//fprintf(stderr,"rary:");rb_p(rary);
|
633
|
+
//fprintf(stderr,"class_of(rary):");rb_p(CLASS_OF(rary));
|
634
|
+
|
635
|
+
//vnc = na_ary_composition_for_struct(klass, rary);
|
636
|
+
//Data_Get_Struct(vnc, na_compose_t, nc);
|
637
|
+
//nary = nary_new(klass, nc->ndim, nc->shape);
|
638
|
+
nary = na_s_new_like(klass, rary);
|
639
|
+
GetNArray(nary,na);
|
640
|
+
//fprintf(stderr,"na->size=%lu\n",na->size);
|
641
|
+
//fprintf(stderr,"na->ndim=%d\n",na->ndim);
|
642
|
+
if (na->size>0) {
|
643
|
+
opt = nst_create_member_views(nary);
|
644
|
+
rb_funcall(nary, rb_intern("allocate"), 0);
|
645
|
+
na_ndloop_store_rarray2(&ndf, nary, rary, opt);
|
646
|
+
}
|
647
|
+
return nary;
|
648
|
+
}
|
649
|
+
|
650
|
+
static inline VALUE
|
651
|
+
nary_struct_s_cast(VALUE klass, VALUE rary)
|
652
|
+
{
|
653
|
+
return nary_struct_cast_array(klass, rary);
|
654
|
+
}
|
655
|
+
|
656
|
+
|
657
|
+
|
658
|
+
static void
|
659
|
+
iter_struct_store_struct(na_loop_t *const lp)
|
660
|
+
{
|
661
|
+
size_t i, s1, s2;
|
662
|
+
char *p1, *p2;
|
663
|
+
size_t *idx1, *idx2;
|
664
|
+
size_t elmsz;
|
665
|
+
char *x, *y;
|
666
|
+
|
667
|
+
INIT_COUNTER(lp, i);
|
668
|
+
INIT_PTR_IDX(lp, 0, p1, s1, idx1);
|
669
|
+
INIT_PTR_IDX(lp, 1, p2, s2, idx2);
|
670
|
+
INIT_ELMSIZE(lp, 0, elmsz);
|
671
|
+
if (idx2) {
|
672
|
+
if (idx1) {
|
673
|
+
for (; i--;) {
|
674
|
+
x = (char*)(p1+*idx1); idx1++;
|
675
|
+
y = (char*)(p2+*idx2); idx2++;
|
676
|
+
memcpy(x,y,elmsz);
|
677
|
+
}
|
678
|
+
} else {
|
679
|
+
for (; i--;) {
|
680
|
+
x = (char*)p1; p1+=s1;
|
681
|
+
y = (char*)(p2+*idx2); idx2++;
|
682
|
+
memcpy(x,y,elmsz);
|
683
|
+
}
|
684
|
+
}
|
685
|
+
} else {
|
686
|
+
if (idx1) {
|
687
|
+
for (; i--;) {
|
688
|
+
x = (char*)(p1+*idx1); idx1++;
|
689
|
+
y = (char*)p2; p2+=s2;
|
690
|
+
memcpy(x,y,elmsz);
|
691
|
+
}
|
692
|
+
} else {
|
693
|
+
for (; i--;) {
|
694
|
+
x = (char*)p1; p1+=s1;
|
695
|
+
y = (char*)p2; p2+=s2;
|
696
|
+
memcpy(x,y,elmsz);
|
697
|
+
}
|
698
|
+
}
|
699
|
+
}
|
700
|
+
}
|
701
|
+
|
702
|
+
|
703
|
+
static VALUE
|
704
|
+
nary_struct_store_struct(VALUE self, VALUE obj)
|
705
|
+
{
|
706
|
+
ndfunc_arg_in_t ain[2] = {{OVERWRITE,0},{Qnil,0}};
|
707
|
+
ndfunc_t ndf = {iter_struct_store_struct, FULL_LOOP, 2, 0, ain, 0};
|
708
|
+
|
709
|
+
na_ndloop(&ndf, 2, self, obj);
|
710
|
+
return self;
|
711
|
+
}
|
712
|
+
|
713
|
+
|
714
|
+
|
715
|
+
|
716
|
+
static inline VALUE
|
717
|
+
nary_struct_store_array(VALUE self, VALUE obj)
|
718
|
+
{
|
719
|
+
return nary_struct_store_struct(self, nary_struct_cast_array(CLASS_OF(self),obj));
|
720
|
+
}
|
721
|
+
|
722
|
+
/*
|
723
|
+
Store elements to Cumo::Struct from other.
|
724
|
+
@overload store(other)
|
725
|
+
@param [Object] other
|
726
|
+
@return [Cumo::Struct] self
|
727
|
+
*/
|
728
|
+
static VALUE
|
729
|
+
nary_struct_store(VALUE self, VALUE obj)
|
730
|
+
{
|
731
|
+
if (TYPE(obj)==T_ARRAY) {
|
732
|
+
nary_struct_store_array(self,obj);
|
733
|
+
return self;
|
734
|
+
}
|
735
|
+
if (CLASS_OF(self) == CLASS_OF(obj)) {
|
736
|
+
nary_struct_store_struct(self,obj);
|
737
|
+
return self;
|
738
|
+
}
|
739
|
+
rb_raise(nary_eCastError, "unknown conversion from %s to %s",
|
740
|
+
rb_class2name(CLASS_OF(obj)),
|
741
|
+
rb_class2name(CLASS_OF(self)));
|
742
|
+
return self;
|
743
|
+
}
|
744
|
+
|
745
|
+
|
746
|
+
|
747
|
+
static VALUE
|
748
|
+
//iter_struct_inspect(na_loop_t *const lp)
|
749
|
+
iter_struct_inspect(char *ptr, size_t pos, VALUE opt)
|
750
|
+
{
|
751
|
+
VALUE types, defs, def, name, elmt, vary, v, x;
|
752
|
+
size_t ofs;
|
753
|
+
long i, len;
|
754
|
+
narray_view_t *ne;
|
755
|
+
|
756
|
+
types = RARRAY_AREF(opt,0);
|
757
|
+
defs = RARRAY_AREF(opt,1);
|
758
|
+
|
759
|
+
len = RARRAY_LEN(types);
|
760
|
+
vary = rb_ary_new2(len);
|
761
|
+
|
762
|
+
for (i=0; i<len; i++) {
|
763
|
+
def = RARRAY_AREF(defs,i);
|
764
|
+
name = RARRAY_AREF(def,0);
|
765
|
+
ofs = NUM2SIZET(RARRAY_AREF(def,2));
|
766
|
+
elmt = RARRAY_AREF(types,i);
|
767
|
+
GetNArrayView(elmt,ne);
|
768
|
+
ne->offset = pos + ofs;
|
769
|
+
v = rb_str_concat(rb_sym_to_s(name), rb_str_new2(": "));
|
770
|
+
x = rb_funcall(elmt, rb_intern("format_to_a"), 0); // <-- fix me
|
771
|
+
if (ne->base.ndim==0) {
|
772
|
+
x = rb_funcall(x, rb_intern("first"), 0);
|
773
|
+
}
|
774
|
+
x = rb_funcall(x, rb_intern("to_s"), 0);
|
775
|
+
v = rb_str_concat(v, x);
|
776
|
+
rb_ary_push(vary, v);
|
777
|
+
}
|
778
|
+
v = rb_ary_join(vary, rb_str_new2(", "));
|
779
|
+
v = rb_str_concat(rb_str_new2("["), v);
|
780
|
+
v = rb_str_concat(v, rb_str_new2("]"));
|
781
|
+
return v;
|
782
|
+
}
|
783
|
+
|
784
|
+
/*
|
785
|
+
Returns a string containing a human-readable representation of NArray.
|
786
|
+
@overload inspect
|
787
|
+
@return [String]
|
788
|
+
*/
|
789
|
+
static VALUE
|
790
|
+
nary_struct_inspect(VALUE ary)
|
791
|
+
{
|
792
|
+
VALUE opt;
|
793
|
+
opt = nst_create_member_views(ary);
|
794
|
+
return na_ndloop_inspect(ary, iter_struct_inspect, opt);
|
795
|
+
}
|
796
|
+
|
797
|
+
|
798
|
+
static VALUE
|
799
|
+
nst_s_add_type(int argc, VALUE *argv, VALUE mod)
|
800
|
+
{
|
801
|
+
if (argc==0)
|
802
|
+
rb_raise(rb_eArgError,
|
803
|
+
"wrong number of arguments (%d for 1)", argc);
|
804
|
+
nstruct_add_type(argv[0],argc-1,argv+1,mod);
|
805
|
+
return Qnil;
|
806
|
+
}
|
807
|
+
|
808
|
+
|
809
|
+
|
810
|
+
|
811
|
+
#define NST_TYPEDEF(tpname,tpclass) \
|
812
|
+
static VALUE \
|
813
|
+
nst_s_##tpname(VALUE argc, VALUE *argv, VALUE mod) \
|
814
|
+
{ nstruct_add_type(tpclass,argc,argv,mod); \
|
815
|
+
return Qnil; \
|
816
|
+
}
|
817
|
+
|
818
|
+
NST_TYPEDEF(int8,cumo_cInt8)
|
819
|
+
NST_TYPEDEF(int16,cumo_cInt16)
|
820
|
+
NST_TYPEDEF(int32,cumo_cInt32)
|
821
|
+
NST_TYPEDEF(int64,cumo_cInt64)
|
822
|
+
NST_TYPEDEF(uint8,cumo_cUInt8)
|
823
|
+
NST_TYPEDEF(uint16,cumo_cUInt16)
|
824
|
+
NST_TYPEDEF(uint32,cumo_cUInt32)
|
825
|
+
NST_TYPEDEF(uint64,cumo_cUInt64)
|
826
|
+
NST_TYPEDEF(dfloat,cumo_cDFloat)
|
827
|
+
NST_TYPEDEF(dcomplex,cumo_cDComplex)
|
828
|
+
NST_TYPEDEF(sfloat,cumo_cSFloat)
|
829
|
+
NST_TYPEDEF(scomplex,cumo_cSComplex)
|
830
|
+
|
831
|
+
|
832
|
+
#define rb_define_singleton_alias(klass,name1,name2) \
|
833
|
+
rb_define_alias(rb_singleton_class(klass),name1,name2)
|
834
|
+
|
835
|
+
void
|
836
|
+
Init_cumo_nary_struct()
|
837
|
+
{
|
838
|
+
cT = rb_define_class_under(mCumo, "Struct", cumo_cNArray);
|
839
|
+
//cNStMember = rb_define_class_under(cT, "Member", rb_cObject);
|
840
|
+
|
841
|
+
//rb_define_alloc_func(cNStMember, nst_member_s_allocate);
|
842
|
+
//rb_define_method(cNStMember, "initialize", nst_member_initialize, -1);
|
843
|
+
|
844
|
+
//rb_undef_alloc_func(cT);
|
845
|
+
rb_define_singleton_method(cT, "new", nst_s_new, -1);
|
846
|
+
rb_define_singleton_method(cT, "add_type", nst_s_add_type, -1);
|
847
|
+
rb_define_singleton_method(cT, "int8", nst_s_int8, -1);
|
848
|
+
rb_define_singleton_method(cT, "int16", nst_s_int16, -1);
|
849
|
+
rb_define_singleton_method(cT, "int32", nst_s_int32, -1);
|
850
|
+
rb_define_singleton_method(cT, "int64", nst_s_int64, -1);
|
851
|
+
rb_define_singleton_method(cT, "uint8", nst_s_uint8, -1);
|
852
|
+
rb_define_singleton_method(cT, "uint16", nst_s_uint16, -1);
|
853
|
+
rb_define_singleton_method(cT, "uint32", nst_s_uint32, -1);
|
854
|
+
rb_define_singleton_method(cT, "uint64", nst_s_uint64, -1);
|
855
|
+
rb_define_singleton_method(cT, "sfloat", nst_s_sfloat, -1);
|
856
|
+
rb_define_singleton_alias (cT, "float32", "sfloat");
|
857
|
+
rb_define_singleton_method(cT, "scomplex", nst_s_scomplex, -1);
|
858
|
+
rb_define_singleton_alias (cT, "complex64", "scomplex");
|
859
|
+
rb_define_singleton_method(cT, "dfloat", nst_s_dfloat, -1);
|
860
|
+
rb_define_singleton_alias (cT, "float64", "dfloat");
|
861
|
+
rb_define_singleton_method(cT, "dcomplex", nst_s_dcomplex, -1);
|
862
|
+
rb_define_singleton_alias (cT, "complex128", "dcomplex");
|
863
|
+
|
864
|
+
rb_define_method(cT, "definition", nst_definition, 1);
|
865
|
+
rb_define_method(cT, "definitions", nst_definitions, 0);
|
866
|
+
rb_define_method(cT, "field", nst_field, 1);
|
867
|
+
rb_define_method(cT, "field_set", nst_field_set, 2);
|
868
|
+
rb_define_method(cT, "extract", nst_extract, 0);
|
869
|
+
rb_define_method(cT, "method_missing", nst_method_missing, -1);
|
870
|
+
|
871
|
+
//rb_define_method(cT, "fill", nary_nstruct_fill, 1);
|
872
|
+
|
873
|
+
//rb_define_method(cT, "debug_print", nary_nstruct_debug_print, 0);
|
874
|
+
|
875
|
+
rb_define_method(cT, "to_a", nary_struct_to_a, 0);
|
876
|
+
|
877
|
+
rb_define_method(cT, "store", nary_struct_store, 1);
|
878
|
+
|
879
|
+
rb_define_method(cT, "inspect", nary_struct_inspect, 0);
|
880
|
+
|
881
|
+
rb_define_singleton_method(cT, "cast", nary_struct_s_cast, 1);
|
882
|
+
rb_define_singleton_method(cT, "[]", nary_struct_s_cast, -2);
|
883
|
+
|
884
|
+
//rb_define_method(cT, "initialize", rb_struct_initialize, -2);
|
885
|
+
//rb_define_method(cT, "initialize_copy", rb_struct_init_copy, 1);
|
886
|
+
}
|