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,12 @@
|
|
1
|
+
<% unless defined?($cumo_narray_gen_tmpl_accum_kernel_included) %>
|
2
|
+
<% $cumo_narray_gen_tmpl_accum_kernel_included = 1 %>
|
3
|
+
|
4
|
+
<% if type_name.include?('int') %>
|
5
|
+
<% f = File.join(File.dirname(__FILE__), 'real_accum_kernel.cu'); ERB.new(File.read(f)).tap {|erb| erb.filename = f }.result(binding) %>
|
6
|
+
<% elsif type_name.include?('float') %>
|
7
|
+
<%= f = File.join(File.dirname(__FILE__), 'float_accum_kernel.cu'); ERB.new(File.read(f)).tap {|erb| erb.filename = f }.result(binding) %>
|
8
|
+
<% elsif type_name.include?('complex') %>
|
9
|
+
<%= f = File.join(File.dirname(__FILE__), 'complex_accum_kernel.cu'); ERB.new(File.read(f)).tap {|erb| erb.filename = f }.result(binding) %>
|
10
|
+
<% end %>
|
11
|
+
|
12
|
+
<% end %>
|
@@ -0,0 +1,107 @@
|
|
1
|
+
static size_t
|
2
|
+
<%=type_name%>_memsize(const void* ptr)
|
3
|
+
{
|
4
|
+
size_t size = sizeof(narray_data_t);
|
5
|
+
const narray_data_t *na = (const narray_data_t*)ptr;
|
6
|
+
|
7
|
+
assert(na->base.type == NARRAY_DATA_T);
|
8
|
+
|
9
|
+
if (na->ptr != NULL) {
|
10
|
+
<% if is_bit %>
|
11
|
+
size += ((na->base.size-1)/8/sizeof(BIT_DIGIT)+1)*sizeof(BIT_DIGIT);
|
12
|
+
<% else %>
|
13
|
+
size += na->base.size * sizeof(dtype);
|
14
|
+
<% end %>
|
15
|
+
}
|
16
|
+
if (na->base.size > 0) {
|
17
|
+
if (na->base.shape != NULL && na->base.shape != &(na->base.size)) {
|
18
|
+
size += sizeof(size_t) * na->base.ndim;
|
19
|
+
}
|
20
|
+
}
|
21
|
+
return size;
|
22
|
+
}
|
23
|
+
|
24
|
+
static void
|
25
|
+
<%=type_name%>_free(void* ptr)
|
26
|
+
{
|
27
|
+
narray_data_t *na = (narray_data_t*)ptr;
|
28
|
+
|
29
|
+
assert(na->base.type == NARRAY_DATA_T);
|
30
|
+
|
31
|
+
if (na->ptr != NULL) {
|
32
|
+
cumo_cuda_runtime_free(na->ptr);
|
33
|
+
na->ptr = NULL;
|
34
|
+
}
|
35
|
+
if (na->base.size > 0) {
|
36
|
+
if (na->base.shape != NULL && na->base.shape != &(na->base.size)) {
|
37
|
+
xfree(na->base.shape);
|
38
|
+
na->base.shape = NULL;
|
39
|
+
}
|
40
|
+
}
|
41
|
+
xfree(na);
|
42
|
+
}
|
43
|
+
|
44
|
+
static narray_type_info_t <%=type_name%>_info = {
|
45
|
+
<% if is_bit %>
|
46
|
+
1, // element_bits
|
47
|
+
0, // element_bytes
|
48
|
+
1, // element_stride (in bits)
|
49
|
+
<% else %>
|
50
|
+
0, // element_bits
|
51
|
+
sizeof(dtype), // element_bytes
|
52
|
+
sizeof(dtype), // element_stride (in bytes)
|
53
|
+
<% end %>
|
54
|
+
};
|
55
|
+
|
56
|
+
<% if is_object %>
|
57
|
+
static void
|
58
|
+
<%=type_name%>_gc_mark(void *ptr)
|
59
|
+
{
|
60
|
+
size_t n, i;
|
61
|
+
VALUE *a;
|
62
|
+
narray_data_t *na = ptr;
|
63
|
+
|
64
|
+
if (na->ptr) {
|
65
|
+
a = (VALUE*)(na->ptr);
|
66
|
+
n = na->base.size;
|
67
|
+
for (i=0; i<n; i++) {
|
68
|
+
rb_gc_mark(a[i]);
|
69
|
+
}
|
70
|
+
}
|
71
|
+
}
|
72
|
+
|
73
|
+
static const rb_data_type_t <%=type_name%>_data_type = {
|
74
|
+
"<%=full_class_name%>",
|
75
|
+
{<%=type_name%>_gc_mark, <%=type_name%>_free, <%=type_name%>_memsize,},
|
76
|
+
&na_data_type,
|
77
|
+
&<%=type_name%>_info,
|
78
|
+
0, // flags
|
79
|
+
};
|
80
|
+
|
81
|
+
<% else %>
|
82
|
+
|
83
|
+
static const rb_data_type_t <%=type_name%>_data_type = {
|
84
|
+
"<%=full_class_name%>",
|
85
|
+
{0, <%=type_name%>_free, <%=type_name%>_memsize,},
|
86
|
+
&na_data_type,
|
87
|
+
&<%=type_name%>_info,
|
88
|
+
0, // flags
|
89
|
+
};
|
90
|
+
|
91
|
+
<% end %>
|
92
|
+
|
93
|
+
static VALUE
|
94
|
+
<%=c_func(0)%>(VALUE klass)
|
95
|
+
{
|
96
|
+
narray_data_t *na = ALLOC(narray_data_t);
|
97
|
+
|
98
|
+
na->base.ndim = 0;
|
99
|
+
na->base.type = NARRAY_DATA_T;
|
100
|
+
na->base.flag[0] = NA_FL0_INIT;
|
101
|
+
na->base.flag[1] = NA_FL1_INIT;
|
102
|
+
na->base.size = 0;
|
103
|
+
na->base.shape = NULL;
|
104
|
+
na->base.reduce = INT2FIX(0);
|
105
|
+
na->ptr = NULL;
|
106
|
+
return TypedData_Wrap_Struct(klass, &<%=type_name%>_data_type, (void*)na);
|
107
|
+
}
|
@@ -0,0 +1,37 @@
|
|
1
|
+
static VALUE
|
2
|
+
<%=c_func(0)%>(VALUE self)
|
3
|
+
{
|
4
|
+
narray_t *na;
|
5
|
+
void *ptr;
|
6
|
+
|
7
|
+
GetNArray(self,na);
|
8
|
+
|
9
|
+
switch(NA_TYPE(na)) {
|
10
|
+
case NARRAY_DATA_T:
|
11
|
+
ptr = NA_DATA_PTR(na);
|
12
|
+
if (na->size > 0 && ptr == NULL) {
|
13
|
+
<% if is_object %>
|
14
|
+
ptr = xmalloc(sizeof(dtype) * na->size);
|
15
|
+
{ size_t i;
|
16
|
+
VALUE *a = (VALUE*)ptr;
|
17
|
+
for (i=na->size; i--;) {
|
18
|
+
*a++ = Qnil;
|
19
|
+
}
|
20
|
+
}
|
21
|
+
<% else %>
|
22
|
+
ptr = cumo_cuda_runtime_malloc(sizeof(dtype) * na->size);
|
23
|
+
<% end %>
|
24
|
+
NA_DATA_PTR(na) = ptr;
|
25
|
+
}
|
26
|
+
break;
|
27
|
+
case NARRAY_VIEW_T:
|
28
|
+
rb_funcall(NA_VIEW_DATA(na), rb_intern("allocate"), 0);
|
29
|
+
break;
|
30
|
+
case NARRAY_FILEMAP_T:
|
31
|
+
//ptr = ((narray_filemap_t*)na)->ptr;
|
32
|
+
// to be implemented
|
33
|
+
default:
|
34
|
+
rb_bug("invalid narray type : %d",NA_TYPE(na));
|
35
|
+
}
|
36
|
+
return self;
|
37
|
+
}
|
@@ -0,0 +1,66 @@
|
|
1
|
+
static VALUE
|
2
|
+
<%=c_func(-1)%>_cpu(int argc, VALUE *argv, VALUE self);
|
3
|
+
|
4
|
+
/*
|
5
|
+
Array element referenece or slice view.
|
6
|
+
@overload [](dim0,...,dimL)
|
7
|
+
@param [Numeric,Range,etc] dim0,...,dimL Multi-dimensional Index.
|
8
|
+
@return [NArray::<%=class_name%>] NArray view.
|
9
|
+
|
10
|
+
--- Returns the element at +dim0+, +dim1+, ... are Numeric indices
|
11
|
+
for each dimension, or returns a NArray View as a sliced subarray if
|
12
|
+
+dim0+, +dim1+, ... includes other than Numeric index, e.g., Range
|
13
|
+
or Array or true.
|
14
|
+
|
15
|
+
Note that Cumo::NArray always returns NArray and does not return a
|
16
|
+
Ruby numeric object as Numo::NArray does to avoid synchronization
|
17
|
+
between GPU and CPU for performance.
|
18
|
+
|
19
|
+
Call Cumo.enable_compatible_mode to make ths method behave compatibly
|
20
|
+
with Numo, or you can use `aref_cpu(*idx)` method instead.
|
21
|
+
|
22
|
+
@example
|
23
|
+
a = Cumo::DFloat.new(4,5).seq
|
24
|
+
=> Cumo::DFloat#shape=[4,5]
|
25
|
+
[[0, 1, 2, 3, 4],
|
26
|
+
[5, 6, 7, 8, 9],
|
27
|
+
[10, 11, 12, 13, 14],
|
28
|
+
[15, 16, 17, 18, 19]]
|
29
|
+
|
30
|
+
a[7]
|
31
|
+
=> Cumo::DFloat#shape=[]
|
32
|
+
6.0
|
33
|
+
|
34
|
+
a[1,1]
|
35
|
+
=> Cumo::DFloat#shape=[]
|
36
|
+
6.0
|
37
|
+
|
38
|
+
a[1..3,1]
|
39
|
+
=> Cumo::DFloat#shape=[3]
|
40
|
+
[6, 11, 16]
|
41
|
+
|
42
|
+
a[1,[1,3,4]]
|
43
|
+
=> Cumo::DFloat#shape=[3]
|
44
|
+
[6, 8, 9]
|
45
|
+
|
46
|
+
a[true,2].fill(99)
|
47
|
+
a
|
48
|
+
=> Cumo::DFloat#shape=[4,5]
|
49
|
+
[[0, 1, 99, 3, 4],
|
50
|
+
[5, 6, 99, 8, 9],
|
51
|
+
[10, 11, 99, 13, 14],
|
52
|
+
[15, 16, 99, 18, 19]]
|
53
|
+
*/
|
54
|
+
static VALUE
|
55
|
+
<%=c_func(-1)%>(int argc, VALUE *argv, VALUE self)
|
56
|
+
{
|
57
|
+
if (cumo_compatible_mode_enabled_p()) {
|
58
|
+
return <%=c_func(-1)%>_cpu(argc, argv, self);
|
59
|
+
} else {
|
60
|
+
int result_nd;
|
61
|
+
size_t pos;
|
62
|
+
|
63
|
+
result_nd = na_get_result_dimension(self, argc, argv, sizeof(dtype), &pos);
|
64
|
+
return na_aref_main(argc, argv, self, 0, result_nd, pos);
|
65
|
+
}
|
66
|
+
}
|
@@ -0,0 +1,50 @@
|
|
1
|
+
/*
|
2
|
+
Array element referenece or slice view.
|
3
|
+
@overload [](dim0,...,dimL)
|
4
|
+
@param [Numeric,Range,etc] dim0,...,dimL Multi-dimensional Index.
|
5
|
+
@return [Numeric,NArray::<%=class_name%>] Element object or NArray view.
|
6
|
+
--- Returns the element at +dim0+, +dim1+, ... are Numeric indices
|
7
|
+
for each dimension, or returns a NArray View as a sliced subarray if
|
8
|
+
+dim0+, +dim1+, ... includes other than Numeric index, e.g., Range
|
9
|
+
or Array or true.
|
10
|
+
This method is compatible with Numo NArray's [] method.
|
11
|
+
@example
|
12
|
+
a = Cumo::DFloat.new(4,5).seq
|
13
|
+
=> Cumo::DFloat#shape=[4,5]
|
14
|
+
[[0, 1, 2, 3, 4],
|
15
|
+
[5, 6, 7, 8, 9],
|
16
|
+
[10, 11, 12, 13, 14],
|
17
|
+
[15, 16, 17, 18, 19]]
|
18
|
+
a[1,1]
|
19
|
+
=> 6.0
|
20
|
+
a[1..3,1]
|
21
|
+
=> Cumo::DFloat#shape=[3]
|
22
|
+
[6, 11, 16]
|
23
|
+
a[1,[1,3,4]]
|
24
|
+
=> Cumo::DFloat#shape=[3]
|
25
|
+
[6, 8, 9]
|
26
|
+
a[true,2].fill(99)
|
27
|
+
a
|
28
|
+
=> Cumo::DFloat#shape=[4,5]
|
29
|
+
[[0, 1, 99, 3, 4],
|
30
|
+
[5, 6, 99, 8, 9],
|
31
|
+
[10, 11, 99, 13, 14],
|
32
|
+
[15, 16, 99, 18, 19]]
|
33
|
+
*/
|
34
|
+
static VALUE
|
35
|
+
<%=c_func(-1)%>(int argc, VALUE *argv, VALUE self)
|
36
|
+
{
|
37
|
+
int result_nd;
|
38
|
+
size_t pos;
|
39
|
+
char *ptr;
|
40
|
+
|
41
|
+
result_nd = na_get_result_dimension(self, argc, argv, sizeof(dtype), &pos);
|
42
|
+
if (result_nd) {
|
43
|
+
return na_aref_main(argc, argv, self, 0, result_nd, pos);
|
44
|
+
} else {
|
45
|
+
ptr = na_get_pointer_for_read(self) + pos;
|
46
|
+
SHOW_SYNCHRONIZE_WARNING_ONCE("<%=name%>", "<%=type_name%>");
|
47
|
+
cumo_cuda_runtime_check_status(cudaDeviceSynchronize());
|
48
|
+
return m_extract(ptr);
|
49
|
+
}
|
50
|
+
}
|
@@ -0,0 +1,56 @@
|
|
1
|
+
/*
|
2
|
+
Array element(s) set.
|
3
|
+
@overload []=(dim0,..,dimL,val)
|
4
|
+
@param [Numeric,Range,etc] dim0,..,dimL Multi-dimensional Index.
|
5
|
+
@param [Numeric,Cumo::NArray,etc] val Value(s) to be set to self.
|
6
|
+
@return [Numeric] returns val (last argument).
|
7
|
+
|
8
|
+
--- Replace element(s) at +dim0+, +dim1+, ... (index/range/array/true
|
9
|
+
for each dimention). Broadcasting mechanism is applied.
|
10
|
+
|
11
|
+
@example
|
12
|
+
a = Cumo::DFloat.new(3,4).seq
|
13
|
+
=> Cumo::DFloat#shape=[3,4]
|
14
|
+
[[0, 1, 2, 3],
|
15
|
+
[4, 5, 6, 7],
|
16
|
+
[8, 9, 10, 11]]
|
17
|
+
|
18
|
+
a[1,2]=99
|
19
|
+
a
|
20
|
+
=> Cumo::DFloat#shape=[3,4]
|
21
|
+
[[0, 1, 2, 3],
|
22
|
+
[4, 5, 99, 7],
|
23
|
+
[8, 9, 10, 11]]
|
24
|
+
|
25
|
+
a[1,[0,2]] = [101,102]
|
26
|
+
a
|
27
|
+
=> Cumo::DFloat#shape=[3,4]
|
28
|
+
[[0, 1, 2, 3],
|
29
|
+
[101, 5, 102, 7],
|
30
|
+
[8, 9, 10, 11]]
|
31
|
+
|
32
|
+
a[1,true]=99
|
33
|
+
a
|
34
|
+
=> Cumo::DFloat#shape=[3,4]
|
35
|
+
[[0, 1, 2, 3],
|
36
|
+
[99, 99, 99, 99],
|
37
|
+
[8, 9, 10, 11]]
|
38
|
+
|
39
|
+
*/
|
40
|
+
static VALUE
|
41
|
+
<%=c_func(-1)%>(int argc, VALUE *argv, VALUE self)
|
42
|
+
{
|
43
|
+
int nd;
|
44
|
+
size_t pos;
|
45
|
+
VALUE a;
|
46
|
+
|
47
|
+
argc--;
|
48
|
+
if (argc==0) {
|
49
|
+
<%=c_func.sub(/_aset/,"_store")%>(self, argv[argc]);
|
50
|
+
} else {
|
51
|
+
nd = na_get_result_dimension(self, argc, argv, sizeof(dtype), &pos);
|
52
|
+
a = na_aref_main(argc, argv, self, 0, nd, pos);
|
53
|
+
<%=c_func.sub(/_aset/,"_store")%>(a, argv[argc]);
|
54
|
+
}
|
55
|
+
return argv[argc];
|
56
|
+
}
|
@@ -0,0 +1,162 @@
|
|
1
|
+
// TODO(sonots): handle zero division error in CUDA kernel?
|
2
|
+
// ref. https://devtalk.nvidia.com/default/topic/415951/divide-by-zero-handling/
|
3
|
+
|
4
|
+
//<% if is_int and %w[div mod divmod].include? name %>
|
5
|
+
#define check_intdivzero(y) \
|
6
|
+
if ((y)==0) { \
|
7
|
+
lp->err_type = rb_eZeroDivError; \
|
8
|
+
return; \
|
9
|
+
}
|
10
|
+
//<% else %>
|
11
|
+
#define check_intdivzero(y) {}
|
12
|
+
//<% end %>
|
13
|
+
|
14
|
+
<% unless type_name == 'robject' %>
|
15
|
+
void <%="cumo_#{c_iter}_kernel_launch"%>(na_iarray_t* a1, na_iarray_t* a2, na_iarray_t* a3, na_indexer_t* indexer);
|
16
|
+
<% end %>
|
17
|
+
|
18
|
+
static void
|
19
|
+
<%=c_iter%>(na_loop_t *const lp)
|
20
|
+
{
|
21
|
+
<% if type_name == 'robject' %>
|
22
|
+
{
|
23
|
+
size_t i, n;
|
24
|
+
char *p1, *p2, *p3;
|
25
|
+
ssize_t s1, s2, s3;
|
26
|
+
|
27
|
+
INIT_COUNTER(lp, n);
|
28
|
+
INIT_PTR(lp, 0, p1, s1);
|
29
|
+
INIT_PTR(lp, 1, p2, s2);
|
30
|
+
INIT_PTR(lp, 2, p3, s3);
|
31
|
+
|
32
|
+
SHOW_SYNCHRONIZE_FIXME_WARNING_ONCE("<%=name%>", "<%=type_name%>");
|
33
|
+
//<% if need_align %>
|
34
|
+
if (is_aligned(p1,sizeof(dtype)) &&
|
35
|
+
is_aligned(p2,sizeof(dtype)) &&
|
36
|
+
is_aligned(p3,sizeof(dtype)) ) {
|
37
|
+
|
38
|
+
if (s1 == sizeof(dtype) &&
|
39
|
+
s2 == sizeof(dtype) &&
|
40
|
+
s3 == sizeof(dtype) ) {
|
41
|
+
|
42
|
+
if (p1 == p3) { // inplace case
|
43
|
+
for (i=0; i<n; i++) {
|
44
|
+
check_intdivzero(*(dtype*)p2);
|
45
|
+
((dtype*)p1)[i] = m_<%=name%>(((dtype*)p1)[i],((dtype*)p2)[i]);
|
46
|
+
}
|
47
|
+
} else {
|
48
|
+
for (i=0; i<n; i++) {
|
49
|
+
check_intdivzero(*(dtype*)p2);
|
50
|
+
((dtype*)p3)[i] = m_<%=name%>(((dtype*)p1)[i],((dtype*)p2)[i]);
|
51
|
+
}
|
52
|
+
}
|
53
|
+
return;
|
54
|
+
}
|
55
|
+
if (is_aligned_step(s1,sizeof(dtype)) &&
|
56
|
+
is_aligned_step(s2,sizeof(dtype)) &&
|
57
|
+
is_aligned_step(s3,sizeof(dtype)) ) {
|
58
|
+
//<% end %>
|
59
|
+
|
60
|
+
if (s2 == 0){ // Broadcasting from scalar value.
|
61
|
+
check_intdivzero(*(dtype*)p2);
|
62
|
+
if (s1 == sizeof(dtype) &&
|
63
|
+
s3 == sizeof(dtype) ) {
|
64
|
+
if (p1 == p3) { // inplace case
|
65
|
+
for (i=0; i<n; i++) {
|
66
|
+
((dtype*)p1)[i] = m_<%=name%>(((dtype*)p1)[i],*(dtype*)p2);
|
67
|
+
}
|
68
|
+
} else {
|
69
|
+
for (i=0; i<n; i++) {
|
70
|
+
((dtype*)p3)[i] = m_<%=name%>(((dtype*)p1)[i],*(dtype*)p2);
|
71
|
+
}
|
72
|
+
}
|
73
|
+
} else {
|
74
|
+
for (i=0; i<n; i++) {
|
75
|
+
*(dtype*)p3 = m_<%=name%>(*(dtype*)p1,*(dtype*)p2);
|
76
|
+
p1 += s1;
|
77
|
+
p3 += s3;
|
78
|
+
}
|
79
|
+
}
|
80
|
+
} else { // Broadcasting from Numo::NArray
|
81
|
+
if (p1 == p3) { // inplace case
|
82
|
+
for (i=0; i<n; i++) {
|
83
|
+
check_intdivzero(*(dtype*)p2);
|
84
|
+
*(dtype*)p1 = m_<%=name%>(*(dtype*)p1,*(dtype*)p2);
|
85
|
+
p1 += s1;
|
86
|
+
p2 += s2;
|
87
|
+
}
|
88
|
+
} else {
|
89
|
+
for (i=0; i<n; i++) {
|
90
|
+
check_intdivzero(*(dtype*)p2);
|
91
|
+
*(dtype*)p3 = m_<%=name%>(*(dtype*)p1,*(dtype*)p2);
|
92
|
+
p1 += s1;
|
93
|
+
p2 += s2;
|
94
|
+
p3 += s3;
|
95
|
+
}
|
96
|
+
}
|
97
|
+
}
|
98
|
+
|
99
|
+
return;
|
100
|
+
//<% if need_align %>
|
101
|
+
}
|
102
|
+
}
|
103
|
+
for (i=0; i<n; i++) {
|
104
|
+
dtype x, y, z;
|
105
|
+
GET_DATA_STRIDE(p1,s1,dtype,x);
|
106
|
+
GET_DATA_STRIDE(p2,s2,dtype,y);
|
107
|
+
check_intdivzero(y);
|
108
|
+
z = m_<%=name%>(x,y);
|
109
|
+
SET_DATA_STRIDE(p3,s3,dtype,z);
|
110
|
+
}
|
111
|
+
//<% end %>
|
112
|
+
}
|
113
|
+
<% else %>
|
114
|
+
{
|
115
|
+
na_iarray_t a1 = na_make_iarray(&lp->args[0]);
|
116
|
+
na_iarray_t a2 = na_make_iarray(&lp->args[1]);
|
117
|
+
na_iarray_t a3 = na_make_iarray(&lp->args[2]);
|
118
|
+
na_indexer_t indexer = na_make_indexer(&lp->args[0]);
|
119
|
+
|
120
|
+
<%="cumo_#{c_iter}_kernel_launch"%>(&a1,&a2,&a3,&indexer);
|
121
|
+
}
|
122
|
+
<% end %>
|
123
|
+
}
|
124
|
+
#undef check_intdivzero
|
125
|
+
|
126
|
+
static VALUE
|
127
|
+
<%=c_func%>_self(VALUE self, VALUE other)
|
128
|
+
{
|
129
|
+
ndfunc_arg_in_t ain[2] = {{cT,0},{cT,0}};
|
130
|
+
ndfunc_arg_out_t aout[1] = {{cT,0}};
|
131
|
+
<% if type_name == 'robject' %>
|
132
|
+
ndfunc_t ndf = { <%=c_iter%>, STRIDE_LOOP, 2, 1, ain, aout };
|
133
|
+
<% else %>
|
134
|
+
ndfunc_t ndf = { <%=c_iter%>, STRIDE_LOOP|NDF_INDEXER_LOOP, 2, 1, ain, aout };
|
135
|
+
<% end %>
|
136
|
+
|
137
|
+
return na_ndloop(&ndf, 2, self, other);
|
138
|
+
}
|
139
|
+
|
140
|
+
/*
|
141
|
+
Binary <%=name%>.
|
142
|
+
@overload <%=op_map%> other
|
143
|
+
@param [Cumo::NArray,Numeric] other
|
144
|
+
@return [Cumo::NArray] self <%=op_map%> other
|
145
|
+
*/
|
146
|
+
static VALUE
|
147
|
+
<%=c_func(1)%>(VALUE self, VALUE other)
|
148
|
+
{
|
149
|
+
<% if is_object %>
|
150
|
+
return <%=c_func%>_self(self, other);
|
151
|
+
<% else %>
|
152
|
+
VALUE klass, v;
|
153
|
+
|
154
|
+
klass = na_upcast(CLASS_OF(self),CLASS_OF(other));
|
155
|
+
if (klass==cT) {
|
156
|
+
return <%=c_func%>_self(self, other);
|
157
|
+
} else {
|
158
|
+
v = rb_funcall(klass, id_cast, 1, self);
|
159
|
+
return rb_funcall(v, <%=id_op%>, 1, other);
|
160
|
+
}
|
161
|
+
<% end %>
|
162
|
+
}
|