cumo 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (266) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +27 -0
  3. data/.travis.yml +5 -0
  4. data/3rd_party/mkmf-cu/.gitignore +36 -0
  5. data/3rd_party/mkmf-cu/Gemfile +3 -0
  6. data/3rd_party/mkmf-cu/LICENSE +21 -0
  7. data/3rd_party/mkmf-cu/README.md +36 -0
  8. data/3rd_party/mkmf-cu/Rakefile +11 -0
  9. data/3rd_party/mkmf-cu/bin/mkmf-cu-nvcc +4 -0
  10. data/3rd_party/mkmf-cu/lib/mkmf-cu.rb +32 -0
  11. data/3rd_party/mkmf-cu/lib/mkmf-cu/cli.rb +80 -0
  12. data/3rd_party/mkmf-cu/lib/mkmf-cu/nvcc.rb +157 -0
  13. data/3rd_party/mkmf-cu/mkmf-cu.gemspec +16 -0
  14. data/3rd_party/mkmf-cu/test/test_mkmf-cu.rb +67 -0
  15. data/CODE_OF_CONDUCT.md +46 -0
  16. data/Gemfile +8 -0
  17. data/LICENSE.txt +82 -0
  18. data/README.md +252 -0
  19. data/Rakefile +43 -0
  20. data/bench/broadcast_fp32.rb +138 -0
  21. data/bench/cumo_bench.rb +193 -0
  22. data/bench/numo_bench.rb +138 -0
  23. data/bench/reduction_fp32.rb +117 -0
  24. data/bin/console +14 -0
  25. data/bin/setup +8 -0
  26. data/cumo.gemspec +32 -0
  27. data/ext/cumo/cuda/cublas.c +278 -0
  28. data/ext/cumo/cuda/driver.c +421 -0
  29. data/ext/cumo/cuda/memory_pool.cpp +185 -0
  30. data/ext/cumo/cuda/memory_pool_impl.cpp +308 -0
  31. data/ext/cumo/cuda/memory_pool_impl.hpp +370 -0
  32. data/ext/cumo/cuda/memory_pool_impl_test.cpp +554 -0
  33. data/ext/cumo/cuda/nvrtc.c +207 -0
  34. data/ext/cumo/cuda/runtime.c +167 -0
  35. data/ext/cumo/cumo.c +148 -0
  36. data/ext/cumo/depend.erb +58 -0
  37. data/ext/cumo/extconf.rb +179 -0
  38. data/ext/cumo/include/cumo.h +25 -0
  39. data/ext/cumo/include/cumo/compat.h +23 -0
  40. data/ext/cumo/include/cumo/cuda/cublas.h +153 -0
  41. data/ext/cumo/include/cumo/cuda/cumo_thrust.hpp +187 -0
  42. data/ext/cumo/include/cumo/cuda/cumo_thrust_complex.hpp +79 -0
  43. data/ext/cumo/include/cumo/cuda/driver.h +22 -0
  44. data/ext/cumo/include/cumo/cuda/memory_pool.h +28 -0
  45. data/ext/cumo/include/cumo/cuda/nvrtc.h +22 -0
  46. data/ext/cumo/include/cumo/cuda/runtime.h +40 -0
  47. data/ext/cumo/include/cumo/indexer.h +238 -0
  48. data/ext/cumo/include/cumo/intern.h +142 -0
  49. data/ext/cumo/include/cumo/intern_fwd.h +38 -0
  50. data/ext/cumo/include/cumo/intern_kernel.h +6 -0
  51. data/ext/cumo/include/cumo/narray.h +429 -0
  52. data/ext/cumo/include/cumo/narray_kernel.h +149 -0
  53. data/ext/cumo/include/cumo/ndloop.h +95 -0
  54. data/ext/cumo/include/cumo/reduce_kernel.h +126 -0
  55. data/ext/cumo/include/cumo/template.h +158 -0
  56. data/ext/cumo/include/cumo/template_kernel.h +77 -0
  57. data/ext/cumo/include/cumo/types/bit.h +40 -0
  58. data/ext/cumo/include/cumo/types/bit_kernel.h +34 -0
  59. data/ext/cumo/include/cumo/types/complex.h +402 -0
  60. data/ext/cumo/include/cumo/types/complex_kernel.h +414 -0
  61. data/ext/cumo/include/cumo/types/complex_macro.h +382 -0
  62. data/ext/cumo/include/cumo/types/complex_macro_kernel.h +186 -0
  63. data/ext/cumo/include/cumo/types/dcomplex.h +46 -0
  64. data/ext/cumo/include/cumo/types/dcomplex_kernel.h +13 -0
  65. data/ext/cumo/include/cumo/types/dfloat.h +47 -0
  66. data/ext/cumo/include/cumo/types/dfloat_kernel.h +14 -0
  67. data/ext/cumo/include/cumo/types/float_def.h +34 -0
  68. data/ext/cumo/include/cumo/types/float_def_kernel.h +39 -0
  69. data/ext/cumo/include/cumo/types/float_macro.h +191 -0
  70. data/ext/cumo/include/cumo/types/float_macro_kernel.h +158 -0
  71. data/ext/cumo/include/cumo/types/int16.h +24 -0
  72. data/ext/cumo/include/cumo/types/int16_kernel.h +23 -0
  73. data/ext/cumo/include/cumo/types/int32.h +24 -0
  74. data/ext/cumo/include/cumo/types/int32_kernel.h +19 -0
  75. data/ext/cumo/include/cumo/types/int64.h +24 -0
  76. data/ext/cumo/include/cumo/types/int64_kernel.h +19 -0
  77. data/ext/cumo/include/cumo/types/int8.h +24 -0
  78. data/ext/cumo/include/cumo/types/int8_kernel.h +19 -0
  79. data/ext/cumo/include/cumo/types/int_macro.h +67 -0
  80. data/ext/cumo/include/cumo/types/int_macro_kernel.h +48 -0
  81. data/ext/cumo/include/cumo/types/real_accum.h +486 -0
  82. data/ext/cumo/include/cumo/types/real_accum_kernel.h +101 -0
  83. data/ext/cumo/include/cumo/types/robj_macro.h +80 -0
  84. data/ext/cumo/include/cumo/types/robj_macro_kernel.h +0 -0
  85. data/ext/cumo/include/cumo/types/robject.h +27 -0
  86. data/ext/cumo/include/cumo/types/robject_kernel.h +7 -0
  87. data/ext/cumo/include/cumo/types/scomplex.h +46 -0
  88. data/ext/cumo/include/cumo/types/scomplex_kernel.h +13 -0
  89. data/ext/cumo/include/cumo/types/sfloat.h +48 -0
  90. data/ext/cumo/include/cumo/types/sfloat_kernel.h +14 -0
  91. data/ext/cumo/include/cumo/types/uint16.h +25 -0
  92. data/ext/cumo/include/cumo/types/uint16_kernel.h +20 -0
  93. data/ext/cumo/include/cumo/types/uint32.h +25 -0
  94. data/ext/cumo/include/cumo/types/uint32_kernel.h +20 -0
  95. data/ext/cumo/include/cumo/types/uint64.h +25 -0
  96. data/ext/cumo/include/cumo/types/uint64_kernel.h +20 -0
  97. data/ext/cumo/include/cumo/types/uint8.h +25 -0
  98. data/ext/cumo/include/cumo/types/uint8_kernel.h +20 -0
  99. data/ext/cumo/include/cumo/types/uint_macro.h +58 -0
  100. data/ext/cumo/include/cumo/types/uint_macro_kernel.h +38 -0
  101. data/ext/cumo/include/cumo/types/xint_macro.h +169 -0
  102. data/ext/cumo/include/cumo/types/xint_macro_kernel.h +88 -0
  103. data/ext/cumo/narray/SFMT-params.h +97 -0
  104. data/ext/cumo/narray/SFMT-params19937.h +46 -0
  105. data/ext/cumo/narray/SFMT.c +620 -0
  106. data/ext/cumo/narray/SFMT.h +167 -0
  107. data/ext/cumo/narray/array.c +638 -0
  108. data/ext/cumo/narray/data.c +961 -0
  109. data/ext/cumo/narray/gen/cogen.rb +56 -0
  110. data/ext/cumo/narray/gen/cogen_kernel.rb +58 -0
  111. data/ext/cumo/narray/gen/def/bit.rb +37 -0
  112. data/ext/cumo/narray/gen/def/dcomplex.rb +39 -0
  113. data/ext/cumo/narray/gen/def/dfloat.rb +37 -0
  114. data/ext/cumo/narray/gen/def/int16.rb +36 -0
  115. data/ext/cumo/narray/gen/def/int32.rb +36 -0
  116. data/ext/cumo/narray/gen/def/int64.rb +36 -0
  117. data/ext/cumo/narray/gen/def/int8.rb +36 -0
  118. data/ext/cumo/narray/gen/def/robject.rb +37 -0
  119. data/ext/cumo/narray/gen/def/scomplex.rb +39 -0
  120. data/ext/cumo/narray/gen/def/sfloat.rb +37 -0
  121. data/ext/cumo/narray/gen/def/uint16.rb +36 -0
  122. data/ext/cumo/narray/gen/def/uint32.rb +36 -0
  123. data/ext/cumo/narray/gen/def/uint64.rb +36 -0
  124. data/ext/cumo/narray/gen/def/uint8.rb +36 -0
  125. data/ext/cumo/narray/gen/erbpp2.rb +346 -0
  126. data/ext/cumo/narray/gen/narray_def.rb +268 -0
  127. data/ext/cumo/narray/gen/spec.rb +425 -0
  128. data/ext/cumo/narray/gen/tmpl/accum.c +86 -0
  129. data/ext/cumo/narray/gen/tmpl/accum_binary.c +121 -0
  130. data/ext/cumo/narray/gen/tmpl/accum_binary_kernel.cu +61 -0
  131. data/ext/cumo/narray/gen/tmpl/accum_index.c +119 -0
  132. data/ext/cumo/narray/gen/tmpl/accum_index_kernel.cu +66 -0
  133. data/ext/cumo/narray/gen/tmpl/accum_kernel.cu +12 -0
  134. data/ext/cumo/narray/gen/tmpl/alloc_func.c +107 -0
  135. data/ext/cumo/narray/gen/tmpl/allocate.c +37 -0
  136. data/ext/cumo/narray/gen/tmpl/aref.c +66 -0
  137. data/ext/cumo/narray/gen/tmpl/aref_cpu.c +50 -0
  138. data/ext/cumo/narray/gen/tmpl/aset.c +56 -0
  139. data/ext/cumo/narray/gen/tmpl/binary.c +162 -0
  140. data/ext/cumo/narray/gen/tmpl/binary2.c +70 -0
  141. data/ext/cumo/narray/gen/tmpl/binary2_kernel.cu +15 -0
  142. data/ext/cumo/narray/gen/tmpl/binary_kernel.cu +31 -0
  143. data/ext/cumo/narray/gen/tmpl/binary_s.c +45 -0
  144. data/ext/cumo/narray/gen/tmpl/binary_s_kernel.cu +15 -0
  145. data/ext/cumo/narray/gen/tmpl/bincount.c +181 -0
  146. data/ext/cumo/narray/gen/tmpl/cast.c +44 -0
  147. data/ext/cumo/narray/gen/tmpl/cast_array.c +13 -0
  148. data/ext/cumo/narray/gen/tmpl/class.c +9 -0
  149. data/ext/cumo/narray/gen/tmpl/class_kernel.cu +6 -0
  150. data/ext/cumo/narray/gen/tmpl/clip.c +121 -0
  151. data/ext/cumo/narray/gen/tmpl/coerce_cast.c +10 -0
  152. data/ext/cumo/narray/gen/tmpl/complex_accum_kernel.cu +129 -0
  153. data/ext/cumo/narray/gen/tmpl/cond_binary.c +68 -0
  154. data/ext/cumo/narray/gen/tmpl/cond_binary_kernel.cu +18 -0
  155. data/ext/cumo/narray/gen/tmpl/cond_unary.c +46 -0
  156. data/ext/cumo/narray/gen/tmpl/cum.c +50 -0
  157. data/ext/cumo/narray/gen/tmpl/each.c +47 -0
  158. data/ext/cumo/narray/gen/tmpl/each_with_index.c +70 -0
  159. data/ext/cumo/narray/gen/tmpl/ewcomp.c +79 -0
  160. data/ext/cumo/narray/gen/tmpl/ewcomp_kernel.cu +19 -0
  161. data/ext/cumo/narray/gen/tmpl/extract.c +22 -0
  162. data/ext/cumo/narray/gen/tmpl/extract_cpu.c +26 -0
  163. data/ext/cumo/narray/gen/tmpl/extract_data.c +53 -0
  164. data/ext/cumo/narray/gen/tmpl/eye.c +105 -0
  165. data/ext/cumo/narray/gen/tmpl/eye_kernel.cu +19 -0
  166. data/ext/cumo/narray/gen/tmpl/fill.c +52 -0
  167. data/ext/cumo/narray/gen/tmpl/fill_kernel.cu +29 -0
  168. data/ext/cumo/narray/gen/tmpl/float_accum_kernel.cu +106 -0
  169. data/ext/cumo/narray/gen/tmpl/format.c +62 -0
  170. data/ext/cumo/narray/gen/tmpl/format_to_a.c +49 -0
  171. data/ext/cumo/narray/gen/tmpl/frexp.c +38 -0
  172. data/ext/cumo/narray/gen/tmpl/gemm.c +203 -0
  173. data/ext/cumo/narray/gen/tmpl/init_class.c +20 -0
  174. data/ext/cumo/narray/gen/tmpl/init_module.c +12 -0
  175. data/ext/cumo/narray/gen/tmpl/inspect.c +21 -0
  176. data/ext/cumo/narray/gen/tmpl/lib.c +50 -0
  177. data/ext/cumo/narray/gen/tmpl/lib_kernel.cu +24 -0
  178. data/ext/cumo/narray/gen/tmpl/logseq.c +102 -0
  179. data/ext/cumo/narray/gen/tmpl/logseq_kernel.cu +31 -0
  180. data/ext/cumo/narray/gen/tmpl/map_with_index.c +98 -0
  181. data/ext/cumo/narray/gen/tmpl/median.c +66 -0
  182. data/ext/cumo/narray/gen/tmpl/minmax.c +47 -0
  183. data/ext/cumo/narray/gen/tmpl/module.c +9 -0
  184. data/ext/cumo/narray/gen/tmpl/module_kernel.cu +1 -0
  185. data/ext/cumo/narray/gen/tmpl/new_dim0.c +15 -0
  186. data/ext/cumo/narray/gen/tmpl/new_dim0_kernel.cu +8 -0
  187. data/ext/cumo/narray/gen/tmpl/poly.c +50 -0
  188. data/ext/cumo/narray/gen/tmpl/pow.c +97 -0
  189. data/ext/cumo/narray/gen/tmpl/pow_kernel.cu +29 -0
  190. data/ext/cumo/narray/gen/tmpl/powint.c +17 -0
  191. data/ext/cumo/narray/gen/tmpl/qsort.c +212 -0
  192. data/ext/cumo/narray/gen/tmpl/rand.c +168 -0
  193. data/ext/cumo/narray/gen/tmpl/rand_norm.c +121 -0
  194. data/ext/cumo/narray/gen/tmpl/real_accum_kernel.cu +75 -0
  195. data/ext/cumo/narray/gen/tmpl/seq.c +112 -0
  196. data/ext/cumo/narray/gen/tmpl/seq_kernel.cu +43 -0
  197. data/ext/cumo/narray/gen/tmpl/set2.c +57 -0
  198. data/ext/cumo/narray/gen/tmpl/sort.c +48 -0
  199. data/ext/cumo/narray/gen/tmpl/sort_index.c +111 -0
  200. data/ext/cumo/narray/gen/tmpl/store.c +41 -0
  201. data/ext/cumo/narray/gen/tmpl/store_array.c +187 -0
  202. data/ext/cumo/narray/gen/tmpl/store_array_kernel.cu +58 -0
  203. data/ext/cumo/narray/gen/tmpl/store_bit.c +86 -0
  204. data/ext/cumo/narray/gen/tmpl/store_bit_kernel.cu +66 -0
  205. data/ext/cumo/narray/gen/tmpl/store_from.c +81 -0
  206. data/ext/cumo/narray/gen/tmpl/store_from_kernel.cu +58 -0
  207. data/ext/cumo/narray/gen/tmpl/store_kernel.cu +3 -0
  208. data/ext/cumo/narray/gen/tmpl/store_numeric.c +9 -0
  209. data/ext/cumo/narray/gen/tmpl/to_a.c +43 -0
  210. data/ext/cumo/narray/gen/tmpl/unary.c +132 -0
  211. data/ext/cumo/narray/gen/tmpl/unary2.c +60 -0
  212. data/ext/cumo/narray/gen/tmpl/unary_kernel.cu +72 -0
  213. data/ext/cumo/narray/gen/tmpl/unary_ret2.c +34 -0
  214. data/ext/cumo/narray/gen/tmpl/unary_s.c +86 -0
  215. data/ext/cumo/narray/gen/tmpl/unary_s_kernel.cu +58 -0
  216. data/ext/cumo/narray/gen/tmpl_bit/allocate.c +24 -0
  217. data/ext/cumo/narray/gen/tmpl_bit/aref.c +54 -0
  218. data/ext/cumo/narray/gen/tmpl_bit/aref_cpu.c +57 -0
  219. data/ext/cumo/narray/gen/tmpl_bit/aset.c +56 -0
  220. data/ext/cumo/narray/gen/tmpl_bit/binary.c +98 -0
  221. data/ext/cumo/narray/gen/tmpl_bit/bit_count.c +64 -0
  222. data/ext/cumo/narray/gen/tmpl_bit/bit_count_cpu.c +88 -0
  223. data/ext/cumo/narray/gen/tmpl_bit/bit_count_kernel.cu +76 -0
  224. data/ext/cumo/narray/gen/tmpl_bit/bit_reduce.c +133 -0
  225. data/ext/cumo/narray/gen/tmpl_bit/each.c +48 -0
  226. data/ext/cumo/narray/gen/tmpl_bit/each_with_index.c +70 -0
  227. data/ext/cumo/narray/gen/tmpl_bit/extract.c +30 -0
  228. data/ext/cumo/narray/gen/tmpl_bit/extract_cpu.c +29 -0
  229. data/ext/cumo/narray/gen/tmpl_bit/fill.c +69 -0
  230. data/ext/cumo/narray/gen/tmpl_bit/format.c +64 -0
  231. data/ext/cumo/narray/gen/tmpl_bit/format_to_a.c +51 -0
  232. data/ext/cumo/narray/gen/tmpl_bit/inspect.c +21 -0
  233. data/ext/cumo/narray/gen/tmpl_bit/mask.c +136 -0
  234. data/ext/cumo/narray/gen/tmpl_bit/none_p.c +14 -0
  235. data/ext/cumo/narray/gen/tmpl_bit/store_array.c +108 -0
  236. data/ext/cumo/narray/gen/tmpl_bit/store_bit.c +70 -0
  237. data/ext/cumo/narray/gen/tmpl_bit/store_from.c +60 -0
  238. data/ext/cumo/narray/gen/tmpl_bit/to_a.c +47 -0
  239. data/ext/cumo/narray/gen/tmpl_bit/unary.c +81 -0
  240. data/ext/cumo/narray/gen/tmpl_bit/where.c +90 -0
  241. data/ext/cumo/narray/gen/tmpl_bit/where2.c +95 -0
  242. data/ext/cumo/narray/index.c +880 -0
  243. data/ext/cumo/narray/kwargs.c +153 -0
  244. data/ext/cumo/narray/math.c +142 -0
  245. data/ext/cumo/narray/narray.c +1948 -0
  246. data/ext/cumo/narray/ndloop.c +2105 -0
  247. data/ext/cumo/narray/rand.c +45 -0
  248. data/ext/cumo/narray/step.c +474 -0
  249. data/ext/cumo/narray/struct.c +886 -0
  250. data/lib/cumo.rb +3 -0
  251. data/lib/cumo/cuda.rb +11 -0
  252. data/lib/cumo/cuda/compile_error.rb +36 -0
  253. data/lib/cumo/cuda/compiler.rb +161 -0
  254. data/lib/cumo/cuda/device.rb +47 -0
  255. data/lib/cumo/cuda/link_state.rb +31 -0
  256. data/lib/cumo/cuda/module.rb +40 -0
  257. data/lib/cumo/cuda/nvrtc_program.rb +27 -0
  258. data/lib/cumo/linalg.rb +12 -0
  259. data/lib/cumo/narray.rb +2 -0
  260. data/lib/cumo/narray/extra.rb +1278 -0
  261. data/lib/erbpp.rb +294 -0
  262. data/lib/erbpp/line_number.rb +137 -0
  263. data/lib/erbpp/narray_def.rb +381 -0
  264. data/numo-narray-version +1 -0
  265. data/run.gdb +7 -0
  266. metadata +353 -0
@@ -0,0 +1,62 @@
1
+ static VALUE
2
+ format_<%=type_name%>(VALUE fmt, dtype* x)
3
+ {
4
+ // fix-me
5
+ char s[48];
6
+ int n;
7
+
8
+ if (NIL_P(fmt)) {
9
+ n = m_sprintf(s,*x);
10
+ return rb_str_new(s,n);
11
+ }
12
+ return rb_funcall(fmt, '%', 1, m_data_to_num(*x));
13
+ }
14
+
15
+ static void
16
+ <%=c_iter%>(na_loop_t *const lp)
17
+ {
18
+ size_t i;
19
+ char *p1, *p2;
20
+ ssize_t s1, s2;
21
+ size_t *idx1;
22
+ dtype *x;
23
+ VALUE y;
24
+ VALUE fmt = lp->option;
25
+ INIT_COUNTER(lp, i);
26
+ INIT_PTR_IDX(lp, 0, p1, s1, idx1);
27
+ INIT_PTR(lp, 1, p2, s2);
28
+ //SHOW_SYNCHRONIZE_FIXME_WARNING_ONCE("<%=name%>", "<%=type_name%>");
29
+ if (idx1) {
30
+ for (; i--;) {
31
+ x = (dtype*)(p1+*idx1); idx1++;
32
+ y = format_<%=type_name%>(fmt, x);
33
+ SET_DATA_STRIDE(p2, s2, VALUE, y);
34
+ }
35
+ } else {
36
+ for (; i--;) {
37
+ x = (dtype*)p1; p1+=s1;
38
+ y = format_<%=type_name%>(fmt, x);
39
+ SET_DATA_STRIDE(p2, s2, VALUE, y);
40
+ }
41
+ }
42
+ }
43
+
44
+ /*
45
+ Format elements into strings.
46
+ @overload <%=name%> format
47
+ @param [String] format
48
+ @return [Cumo::RObject] array of formated strings.
49
+ */
50
+ static VALUE
51
+ <%=c_func(-1)%>(int argc, VALUE *argv, VALUE self)
52
+ {
53
+ VALUE fmt=Qnil;
54
+
55
+ ndfunc_arg_in_t ain[2] = {{Qnil,0},{sym_option}};
56
+ ndfunc_arg_out_t aout[1] = {{cumo_cRObject,0}};
57
+ ndfunc_t ndf = { <%=c_iter%>, FULL_LOOP_NIP, 2, 1, ain, aout };
58
+
59
+ rb_scan_args(argc, argv, "01", &fmt);
60
+ cumo_cuda_runtime_check_status(cudaDeviceSynchronize());
61
+ return na_ndloop(&ndf, 2, self, fmt);
62
+ }
@@ -0,0 +1,49 @@
1
+ static void
2
+ <%=c_iter%>(na_loop_t *const lp)
3
+ {
4
+ size_t i;
5
+ char *p1;
6
+ ssize_t s1;
7
+ size_t *idx1;
8
+ dtype *x;
9
+ VALUE y;
10
+ volatile VALUE a;
11
+ VALUE fmt = lp->option;
12
+ INIT_COUNTER(lp, i);
13
+ INIT_PTR_IDX(lp, 0, p1, s1, idx1);
14
+ a = rb_ary_new2(i);
15
+ rb_ary_push(lp->args[1].value, a);
16
+ //SHOW_SYNCHRONIZE_FIXME_WARNING_ONCE("<%=name%>", "<%=type_name%>");
17
+ if (idx1) {
18
+ for (; i--;) {
19
+ x = (dtype*)(p1 + *idx1); idx1++;
20
+ y = format_<%=type_name%>(fmt, x);
21
+ rb_ary_push(a,y);
22
+ }
23
+ } else {
24
+ for (; i--;) {
25
+ x = (dtype*)p1; p1+=s1;
26
+ y = format_<%=type_name%>(fmt, x);
27
+ rb_ary_push(a,y);
28
+ }
29
+ }
30
+ }
31
+
32
+ /*
33
+ Format elements into strings.
34
+ @overload <%=name%> format
35
+ @param [String] format
36
+ @return [Array] array of formated strings.
37
+ */
38
+ static VALUE
39
+ <%=c_func(-1)%>(int argc, VALUE *argv, VALUE self)
40
+ {
41
+ VALUE fmt=Qnil;
42
+ ndfunc_arg_in_t ain[3] = {{Qnil,0},{sym_loop_opt},{sym_option}};
43
+ ndfunc_arg_out_t aout[1] = {{rb_cArray,0}}; // dummy?
44
+ ndfunc_t ndf = { <%=c_iter%>, FULL_LOOP_NIP, 3, 1, ain, aout };
45
+
46
+ rb_scan_args(argc, argv, "01", &fmt);
47
+ cumo_cuda_runtime_check_status(cudaDeviceSynchronize());
48
+ return na_ndloop_cast_narray_to_rarray(&ndf, self, fmt);
49
+ }
@@ -0,0 +1,38 @@
1
+ static void
2
+ <%=c_iter%>(na_loop_t *const lp)
3
+ {
4
+ size_t i;
5
+ char *p1, *p2, *p3;
6
+ ssize_t s1, s2, s3;
7
+ dtype x;
8
+ int y;
9
+ INIT_COUNTER(lp, i);
10
+ INIT_PTR(lp, 0, p1, s1);
11
+ INIT_PTR(lp, 1, p2, s2);
12
+ INIT_PTR(lp, 2, p3, s3);
13
+ SHOW_SYNCHRONIZE_FIXME_WARNING_ONCE("<%=name%>", "<%=type_name%>");
14
+ for (; i--;) {
15
+ GET_DATA_STRIDE(p1,s1,dtype,x);
16
+ x = m_<%=name%>(x,&y);
17
+ SET_DATA_STRIDE(p2,s2,dtype,x);
18
+ SET_DATA_STRIDE(p3,s3,int32_t,y);
19
+ }
20
+ }
21
+
22
+ /*
23
+ split the number x into a normalized fraction and an exponent.
24
+ Returns [mantissa, exponent], where x = mantissa * 2**exponent.
25
+
26
+ @overload <%=name%>(x)
27
+ @param [Cumo::NArray,Numeric] x
28
+ @return [Cumo::<%=class_name%>,Cumo::Int32] mantissa and exponent.
29
+
30
+ */
31
+ static VALUE
32
+ <%=c_func(1)%>(VALUE mod, VALUE a1)
33
+ {
34
+ ndfunc_arg_in_t ain[1] = {{cT,0}};
35
+ ndfunc_arg_out_t aout[2] = {{cT,0},{cumo_cInt32,0}};
36
+ ndfunc_t ndf = { <%=c_iter%>, STRIDE_LOOP, 1,2, ain,aout };
37
+ return na_ndloop(&ndf, 1, a1);
38
+ }
@@ -0,0 +1,203 @@
1
+ <%
2
+ func_prefix =
3
+ case type_name
4
+ when 'sfloat'
5
+ 'S'
6
+ when 'dfloat'
7
+ 'D'
8
+ when 'scomplex'
9
+ 'C'
10
+ when 'dcomplex'
11
+ 'Z'
12
+ end
13
+ cutype =
14
+ case type_name
15
+ when 'sfloat'
16
+ 'float'
17
+ when 'dfloat'
18
+ 'double'
19
+ when 'scomplex'
20
+ 'cuComplex'
21
+ when 'dcomplex'
22
+ 'cuDoubleComplex'
23
+ end
24
+ %>
25
+
26
+ #define args_t <%=name%>_args_t
27
+
28
+ typedef struct {
29
+ // enum CBLAS_ORDER order; // cuBLAS does not have order (row-major or column-major) option
30
+ cublasOperation_t transa, transb;
31
+ cublasSideMode_t side;
32
+ cublasFillMode_t uplo;
33
+ cublasDiagType_t diag;
34
+ dtype alpha, beta;
35
+ int m, n, k;
36
+ } args_t;
37
+
38
+ static void
39
+ <%=c_iter%>(na_loop_t *const lp)
40
+ {
41
+ dtype *a, *b;
42
+ int lda, ldb;
43
+ dtype *c;
44
+ int ldc;
45
+ args_t *g;
46
+ static cublasHandle_t handle = 0;
47
+
48
+ a = (dtype*)NDL_PTR(lp,0);
49
+ b = (dtype*)NDL_PTR(lp,1);
50
+ c = (dtype*)NDL_PTR(lp,2);
51
+ g = (args_t*)(lp->opt_ptr);
52
+
53
+ lda = NDL_STEP(lp,0) / sizeof(dtype);
54
+ ldb = NDL_STEP(lp,1) / sizeof(dtype);
55
+ ldc = NDL_STEP(lp,2) / sizeof(dtype);
56
+
57
+ //printf("transa=%d transb=%d m=%d n=%d k=%d lda=%d ldb=%d ldc=%d\n",g->transa,g->transb,g->m,g->n,g->k,lda,ldb,ldc);
58
+
59
+ // Note that cuBLAS uses the column major matrix representation.
60
+ // We use technic which following site describes:
61
+ // https://www.christophlassner.de/using-blas-from-c-with-row-major-data.html
62
+ //
63
+ // b^T = nxk matrix
64
+ // a^T = kxm matrix
65
+ // c^T = nxm matrix
66
+ // c^T = b^T * a^T
67
+ //
68
+ // cublasSgemm(handle,transb,transa,n,m,k,&alpha,b,n,a,k,&beta,c,n);
69
+
70
+ // TODO(sonots): Create another handle for another cuda device or cpu thread
71
+ if (!handle) {
72
+ cublasCreate(&handle);
73
+ }
74
+ cublas<%=func_prefix%>gemm(handle, g->transb, g->transa, g->n, g->m, g->k, (<%=cutype%>*)(&g->alpha), (<%=cutype%>*)b, ldb, (<%=cutype%>*)a, lda, (<%=cutype%>*)(&g->beta), (<%=cutype%>*)c, ldc);
75
+ // TODO(sonots): Destroy correctly
76
+ //cublasDestroy(handle);
77
+ }
78
+
79
+ /*
80
+ <%
81
+ # ext/numo/linalg/blas/gen/decl.rb
82
+
83
+ def mat(v,*a,**h)
84
+ tp = h[:type] || class_name
85
+ a.map!{|x| x==:inplace ? "inplace allowed" : x}
86
+ a.unshift ">=2-dimentional NArray"
87
+ "@param #{v} [#{tp}] matrix (#{a.join(', ')})."
88
+ end
89
+
90
+ def opt(v,tp=nil,*a)
91
+ tp ||= "String or Symbol"
92
+ case v
93
+ when /^order$/
94
+ "@param #{v} [#{tp}] if 'R': Row-major, if 'C': Column-major. (default='R')"
95
+ when /^uplo$/
96
+ "@param #{v} [#{tp}] if 'U': Upper triangle, if 'L': Lower triangle. (default='U')"
97
+ when /^side$/
98
+ "@param #{v} [#{tp}] if 'L': op(A)\\*B (left-side op), if 'R': B\\*op(A) (right-side op). (default='L')"
99
+ when /^diag$/
100
+ "@param #{v} [#{tp}] if 'U': assumed to be unit triangular, if 'N': not assumed to be unit triangular. (default='U')"
101
+ when /^trans(\w+)?$/
102
+ b = a[0] || $1
103
+ "@param #{v} [#{tp}] if 'N': Not transpose #{b}, if 'T': Transpose #{b}. (default='N')"
104
+ when "alpha"
105
+ "@param #{v} [Float] (default=1.0)"
106
+ when "beta"
107
+ "@param #{v} [Float] (default=0.0)"
108
+ else
109
+ "@param #{v} [#{tp}] #{a[0]}"
110
+ end
111
+ end
112
+ %>
113
+ <%
114
+ args_v = "a, b, [c, alpha:1, beta:0, transa:'N', transb:'N']"
115
+ params = [
116
+ mat("a"),
117
+ mat("b"),
118
+ mat("c","optional",:inplace),
119
+ opt("alpha"),
120
+ opt("beta"),
121
+ opt("transa"),
122
+ opt("transb"),
123
+ ].select{|x| x}.join("\n ")
124
+ %>
125
+ @overload <%=name%>(<%=args_v%>)
126
+ <%=params%>
127
+ @return [<%=class_name%>] returns c = alpha\*op( A )\*op( B ) + beta\*C.
128
+ <%=description%>
129
+ */
130
+ static VALUE
131
+ <%=c_func(-1)%>(int argc, VALUE argv[], VALUE self)
132
+ {
133
+ VALUE a=self, b, c=Qnil, alpha, beta;
134
+ narray_t *na1, *na2;
135
+ int ma, ka, kb, nb, tmp;
136
+ size_t shape[2];
137
+ ndfunc_arg_in_t ain[3] = {{cT,2},{cT,2},{OVERWRITE,2}};
138
+ ndfunc_arg_out_t aout[1] = {{cT,2,shape}};
139
+ ndfunc_t ndf = {<%=c_iter%>, NO_LOOP, 3, 0, ain, aout};
140
+
141
+ args_t g;
142
+ VALUE kw_hash = Qnil;
143
+ ID kw_table[4] = {rb_intern("alpha"),rb_intern("beta"),rb_intern("transa"),rb_intern("transb")};
144
+ VALUE opts[4] = {Qundef,Qundef,Qundef,Qundef};
145
+
146
+ rb_scan_args(argc, argv, "11:", &b, &c, &kw_hash);
147
+ rb_get_kwargs(kw_hash, kw_table, 0, 4, opts);
148
+ alpha = option_value(opts[0],Qnil);
149
+ g.alpha = RTEST(alpha) ? m_num_to_data(alpha) : m_one;
150
+ beta = option_value(opts[1],Qnil);
151
+ g.beta = RTEST(beta) ? m_num_to_data(beta) : m_zero;
152
+ g.transa = option_trans(opts[2]);
153
+ g.transb = option_trans(opts[3]);
154
+
155
+ GetNArray(a,na1);
156
+ GetNArray(b,na2);
157
+ CHECK_DIM_GE(na1,2);
158
+ CHECK_DIM_GE(na2,2);
159
+ ma = ROW_SIZE(na1); // m
160
+ ka = COL_SIZE(na1); // k
161
+ kb = ROW_SIZE(na2); // k
162
+ nb = COL_SIZE(na2); // n
163
+
164
+ SWAP_IFTR(g.transa, ma, ka, tmp);
165
+ SWAP_IFTR(g.transb, kb, nb, tmp);
166
+ CHECK_INT_EQ("ka",ka,"kb",kb);
167
+ g.m = ma;
168
+ g.n = nb;
169
+ g.k = ka;
170
+
171
+ SWAP_IFROW(ma, nb, tmp);
172
+
173
+ if (c == Qnil) { // c is not given.
174
+ ndfunc_arg_in_t ain_init = {sym_init,0};
175
+ ain[2] = ain_init;
176
+ ndf.nout = 1;
177
+ c = INT2FIX(0);
178
+ shape[0] = nb;
179
+ shape[1] = ma;
180
+ } else {
181
+ narray_t *na3;
182
+ int nc;
183
+ COPY_OR_CAST_TO(c,cT);
184
+ GetNArray(c,na3);
185
+ CHECK_DIM_GE(na3,2);
186
+ nc = ROW_SIZE(na3);
187
+ if (nc < nb) {
188
+ rb_raise(nary_eShapeError,"nc=%d must be >= nb=%d",nc,nb);
189
+ }
190
+ //CHECK_LEADING_GE("ldc",g.ldc,"m",ma);
191
+ }
192
+ {
193
+ VALUE ans = na_ndloop3(&ndf, &g, 3, a, b, c);
194
+
195
+ if (ndf.nout == 1) { // c is not given.
196
+ return ans;
197
+ } else {
198
+ return c;
199
+ }
200
+ }
201
+ }
202
+
203
+ #undef args_t
@@ -0,0 +1,20 @@
1
+ /*
2
+ Document-class: <%= full_class_name %>
3
+ <%= description %>
4
+ */
5
+ cT = rb_define_class_under(<%=ns_var%>, "<%=class_name%>", cNArray);
6
+
7
+ <% for x in class_alias %>
8
+ // alias of <%=class_name%>
9
+ rb_define_const(<%=ns_var%>, "<%=x%>", <%=type_var%>);
10
+ <% end %>
11
+
12
+ hCast = rb_hash_new();
13
+ rb_define_const(cT, "UPCAST", hCast);
14
+ rb_hash_aset(hCast, rb_cArray, cT);
15
+ <% for x in upcast %>
16
+ <%= x %><% end %>
17
+
18
+ <% @children.each do |m| %>
19
+ <%= m.init_def %><% end %>
20
+ rb_define_singleton_method(cT, "[]", <%=find("cast").c_func%>, -2);
@@ -0,0 +1,12 @@
1
+ /*
2
+ Document-module: <%= full_module_name %>
3
+ <%= description %>
4
+ */
5
+ <% if module_var != ns_var %>
6
+ <%=module_var%> = rb_define_module_under(<%=ns_var%>, "<%=module_name%>");
7
+ <% end %>
8
+ <% @children.each do |m| %>
9
+ <%= m.init_def %><% end %>
10
+
11
+ // how to do this?
12
+ //rb_extend_object(cT, mTM);
@@ -0,0 +1,21 @@
1
+ static VALUE
2
+ <%=c_iter%>(char *ptr, size_t pos, VALUE fmt)
3
+ {
4
+ <% if is_object %>
5
+ return rb_inspect(*(VALUE*)(ptr+pos));
6
+ <% else %>
7
+ return format_<%=type_name%>(fmt, (dtype*)(ptr+pos));
8
+ <% end %>
9
+ }
10
+
11
+ /*
12
+ Returns a string containing a human-readable representation of NArray.
13
+ @overload inspect
14
+ @return [String]
15
+ */
16
+ static VALUE
17
+ <%=c_func(0)%>(VALUE ary)
18
+ {
19
+ cumo_cuda_runtime_check_status(cudaDeviceSynchronize());
20
+ return na_ndloop_inspect(ary, <%=c_iter%>, Qnil);
21
+ }
@@ -0,0 +1,50 @@
1
+ /*
2
+ <%= file_name %>
3
+ Ruby/Cumo::GSL - GSL wrapper for Ruby/Cumo::NArray
4
+
5
+ created on: 2017-03-11
6
+ Copyright (C) 2017 Masahiro Tanaka
7
+ Copyright (C) 2018 Naotoshi Seo
8
+ */
9
+
10
+ #include <ruby.h>
11
+ #include <assert.h>
12
+ #include "cumo.h"
13
+ #include "cumo/narray.h"
14
+ #include "cumo/template.h"
15
+ #include "SFMT.h"
16
+ #include "cumo/cuda/memory_pool.h"
17
+ #include "cumo/cuda/runtime.h"
18
+ <% unless type_name == 'robject' %>
19
+ #include "cumo/indexer.h"
20
+ <% end %>
21
+
22
+ #define m_map(x) m_num_to_data(rb_yield(m_data_to_num(x)))
23
+
24
+ <% id_decl.each do |x| %>
25
+ <%= x %>
26
+ <% end %>
27
+
28
+ #include <<%="cumo/types/#{type_name}.h"%>>
29
+
30
+ VALUE cT;
31
+ extern VALUE cRT;
32
+
33
+ <% children.each do |c|%>
34
+ <%= c.result+"\n\n" %>
35
+ <% end %>
36
+
37
+ void
38
+ Init_<%=lib_name%>(void)
39
+ {
40
+ VALUE hCast, <%=ns_var%>;
41
+
42
+ <%=ns_var%> = rb_define_module("Cumo");
43
+
44
+ <% id_assign.each do |x| %>
45
+ <%= x %><% end %>
46
+
47
+ <% children.each do |c| %>
48
+ <%= c.init_def %>
49
+ <% end %>
50
+ }