cumo 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,294 @@
1
+ require "erb"
2
+
3
+ class ErbPP
4
+ ATTRS = []
5
+
6
+ class ParamNotSetError < StandardError; end
7
+
8
+ def self.define_attrs(attrs)
9
+ attrs.each do |attr|
10
+ ivar = ("@"+attr).to_sym
11
+ define_method(attr){|*a| attr_method(ivar,*a)}
12
+ end
13
+ end
14
+
15
+ def attr_method(ivar,arg=nil)
16
+ if arg.nil?
17
+ instance_variable_get(ivar)
18
+ else
19
+ instance_variable_set(ivar,arg)
20
+ end
21
+ end
22
+
23
+ def initialize(parent,erb_path,opts={})
24
+ parents.push(parent) if parent
25
+ @erb_path = erb_path
26
+ @tmpl = @erb_path
27
+
28
+ @opts = opts
29
+ if @opts.class != Hash
30
+ raise ArgumentError, "option is not Hash"
31
+ end
32
+
33
+ @opts.each do |k,v|
34
+ ivar = ("@"+k.to_s).to_sym
35
+ instance_variable_set(ivar,v)
36
+ end
37
+ end
38
+
39
+ def load_erb
40
+ safe_level = nil
41
+ trim_mode = '%<>'
42
+ @erb = ERB.new(File.read(@erb_path),safe_level,trim_mode)
43
+ @erb.filename = @erb_path
44
+ end
45
+
46
+ def parents
47
+ @parents ||= []
48
+ end
49
+
50
+ def search_method_in_parents(_meth_id)
51
+ parents.each do |x|
52
+ if x.has_attr? _meth_id
53
+ return x
54
+ end
55
+ end
56
+ parents.each do |x|
57
+ if f = x.search_method_in_parents(_meth_id)
58
+ return f
59
+ end
60
+ end
61
+ nil
62
+ end
63
+
64
+ def attrs
65
+ self.class::ATTRS
66
+ end
67
+
68
+ def has_attr?(_meth_id)
69
+ respond_to?(_meth_id) or attrs.include?(_meth_id.to_s)
70
+ end
71
+
72
+ def check_params(*params)
73
+ params.each do |x|
74
+ val = send(x)
75
+ if !val # || val.empty?
76
+ raise ParamNotSetError,"parameter #{x.to_s} is not set"
77
+ end
78
+ end
79
+ end
80
+
81
+ alias method_missing_alias method_missing
82
+
83
+ def method_missing(_meth_id, *args, &block)
84
+ ivar = "@"+_meth_id.to_s
85
+ if args.empty? and instance_variable_defined?(ivar)
86
+ parm = instance_variable_get(ivar)
87
+ if parm.nil?
88
+ raise ParamNotSetError,"parameter #{_meth_id.to_s} is not set"
89
+ end
90
+ parm
91
+ elsif args.size == 1 and attrs.include?(_meth_id.to_s)
92
+ instance_variable_set(ivar,args.first)
93
+ elsif x = search_method_in_parents(_meth_id)
94
+ x.send(_meth_id, *args, &block)
95
+ else
96
+ method_missing_alias(_meth_id, *args)
97
+ end
98
+ end
99
+
100
+ def run
101
+ load_erb unless @erb
102
+ @erb.run(binding)
103
+ end
104
+
105
+ def result
106
+ load_erb unless @erb
107
+ @erb.result(binding)
108
+ end
109
+ end
110
+
111
+ # ----------------------------------------------------------------------
112
+
113
+ class IdVar
114
+ DEFS = []
115
+
116
+ def id_decl
117
+ "static ID #{@id_var};"
118
+ end
119
+
120
+ def id_assign
121
+ "#{@id_var} = rb_intern(\"#{@name}\");"
122
+ end
123
+
124
+ def initialize(parent,name,var=nil)
125
+ @name = name
126
+ var = name if var.nil?
127
+ @id_var = "id_"+var.gsub(/\?/,"_p").gsub(/\!/,"_bang")
128
+ DEFS.push(self)
129
+ end
130
+
131
+ def self.declaration
132
+ DEFS.map do |x|
133
+ x.id_decl
134
+ end
135
+ end
136
+
137
+ def self.assignment
138
+ DEFS.map do |x|
139
+ x.id_assign
140
+ end
141
+ end
142
+ end
143
+
144
+ # ----------------------------------------------------------------------
145
+
146
+ class Function < ErbPP
147
+ DEFS = []
148
+
149
+ attrs = %w[
150
+ singleton
151
+ meth
152
+ n_arg
153
+ ]
154
+ define_attrs attrs
155
+
156
+ def id_op
157
+ if op.size == 1
158
+ "'#{op}'"
159
+ else
160
+ "id_#{method}"
161
+ end
162
+ end
163
+
164
+ def method
165
+ meth.gsub(/\?/,"_p").gsub(/\!/,"_bang")
166
+ end
167
+
168
+ def initialize(parent,tmpl,**opts)
169
+ super
170
+ @aliases = opts[:aliases] || []
171
+ parent.tmpl_dirs.each do |d|
172
+ @erb_path = File.join(d, tmpl+".c")
173
+ break if File.exist?(@erb_path)
174
+ end
175
+ DEFS.push(self)
176
+ end
177
+
178
+ def c_method
179
+ "#{m_prefix}#{method}"
180
+ end
181
+
182
+ def c_iter
183
+ begin
184
+ t = "_"+type_name
185
+ rescue
186
+ t = ""
187
+ end
188
+ "iter#{t}_#{method}"
189
+ end
190
+ alias c_iterator c_iter
191
+
192
+ def c_func
193
+ s = singleton ? "_s" : ""
194
+ begin
195
+ t = "_"+type_name
196
+ rescue
197
+ t = ""
198
+ end
199
+ "cumo#{t}#{s}_#{method}"
200
+ end
201
+ alias c_function c_func
202
+ alias c_instance_method c_func
203
+
204
+ def op_map
205
+ @op || meth
206
+ end
207
+
208
+ def code
209
+ result + "\n\n"
210
+ end
211
+
212
+ def definition
213
+ return nil if n_arg <= -9
214
+ s = singleton ? "_singleton" : ""
215
+ check_params(:mod_var, :op_map, :c_func, :n_arg)
216
+ m = op_map
217
+ a = ["rb_define#{s}_method(#{mod_var}, \"#{m}\", #{c_func}, #{n_arg});"]
218
+ @aliases.map{|x| a << "rb_define_alias(#{mod_var}, \"#{x}\", \"#{m}\");"}
219
+ a
220
+ end
221
+
222
+ def self.codes
223
+ a = []
224
+ DEFS.each do |i|
225
+ x = i.code
226
+ a.push(x) if x
227
+ end
228
+ a
229
+ end
230
+
231
+ def self.definitions
232
+ a = []
233
+ DEFS.each do |i|
234
+ case x = i.definition
235
+ when Array
236
+ a.concat(x)
237
+ when String
238
+ a.push(x)
239
+ else
240
+ raise "unknown definition: #{x}" if x
241
+ end
242
+ end
243
+ a
244
+ end
245
+ end
246
+
247
+ class ModuleFunction < Function
248
+ def definition
249
+ m = op_map
250
+ "rb_define_module_function(#{mod_var}, \"#{m}\", #{c_func}, #{n_arg});"
251
+ end
252
+ end
253
+
254
+ class NodefFunction < Function
255
+ def definition
256
+ nil
257
+ end
258
+ end
259
+
260
+ class Alias < ErbPP
261
+ def initialize(parent, dst, src)
262
+ super(parent,nil)
263
+ @dst = dst
264
+ @src = src
265
+ Function::DEFS.push(self)
266
+ end
267
+
268
+ def code
269
+ nil
270
+ end
271
+
272
+ def definition
273
+ "rb_define_alias(#{mod_var}, \"#{dst}\", \"#{src}\");"
274
+ end
275
+ end
276
+
277
+ class Const < ErbPP
278
+ def initialize(parent,name,value,desc)
279
+ super(parent,nil)
280
+ @name = name
281
+ @value = value
282
+ @desc = desc
283
+ Function::DEFS.push(self)
284
+ end
285
+
286
+ def code
287
+ nil
288
+ end
289
+
290
+ def definition
291
+ "/*"+desc+"*/\n "+
292
+ "rb_define_const(#{mod_var},\"#{name}\",#{value});"
293
+ end
294
+ end
@@ -0,0 +1,137 @@
1
+ class CountLnString < String
2
+
3
+ def initialize(filename)
4
+ @filename = filename
5
+ @lnchar = "\n"
6
+ @buf = ""
7
+ @str = ""
8
+ @countln = 1
9
+ @current = 1
10
+ super("\n"+report_line)
11
+ end
12
+
13
+ def report_line
14
+ "#line #{@current} \"#{@filename}\"\n"
15
+ end
16
+
17
+ def concat0(s)
18
+ ln(caller[0])
19
+ @buf.concat(s)
20
+ @str.concat(s)
21
+ end
22
+
23
+ def concat1(s)
24
+ ln(caller[0])
25
+ @buf.concat(s)
26
+ end
27
+
28
+ def ln(status=nil)
29
+ case status
30
+ when /:(\d+):/
31
+ n = $1.to_i
32
+ else
33
+ n = status.to_i
34
+ end
35
+ return if n == @current
36
+ if @current != @countln || @postpone
37
+ if /\A\s*\z/ =~ @str || /\A#line / =~ @buf
38
+ @postpone = true
39
+ elsif @in_comment
40
+ @postpone = false
41
+ else
42
+ if self[-1] != "\n"
43
+ concat("\n")
44
+ end
45
+ concat(report_line)
46
+ @postpone = false
47
+ end
48
+ end
49
+ concat(@buf)
50
+
51
+ b = @buf.gsub(/".*?(?<!\\)"/,'""')
52
+ /^.*(\/\*)(.*?)$/ =~ b
53
+ x = $2
54
+ /^.*(\*\/)(.*?)$/ =~ b
55
+ y = $2
56
+ if x
57
+ if y
58
+ if x.size < y.size
59
+ #:in_comment
60
+ @in_comment = true
61
+ else
62
+ #:out_comment
63
+ @in_comment = false
64
+ end
65
+ else
66
+ #:in_comment
67
+ @in_comment = true
68
+ end
69
+ else
70
+ if y
71
+ #:out_comment
72
+ @in_comment = false
73
+ else
74
+ #:keep
75
+ end
76
+ end
77
+
78
+ @countln = @current + @buf.count(@lnchar)
79
+ @current = n
80
+ @buf = ""
81
+ @str = ""
82
+ end
83
+
84
+ def d(s)
85
+ p [s, [x,y], r]
86
+ r
87
+ end
88
+
89
+ def final
90
+ concat(@buf)
91
+ end
92
+
93
+ end
94
+
95
+ class ERB
96
+ alias result_orig result
97
+
98
+ def result(b=new_toplevel)
99
+ src = src_with_cpp_line
100
+ if @safe_level
101
+ proc {
102
+ $SAFE = @safe_level
103
+ eval(src, b, (@filename || '(erb)'), 0)
104
+ }.call
105
+ else
106
+ #open("tmpout","w"){|f| f.write src} if /dtype/=~@filename
107
+ eval(src, b, (@filename || '(erb)'), 0)
108
+ end
109
+ end
110
+
111
+ alias src_orig src
112
+
113
+ def src
114
+ src_with_cpp_line
115
+ end
116
+
117
+ def src_with_cpp_line
118
+ @src.each_line.with_index.map do |line, num|
119
+ # ruby ver 2.4 or earlier
120
+ line.gsub!(/_erbout.concat "/,'_erbout.concat0 "')
121
+ line.gsub!(/_erbout.concat\(/,'_erbout.concat1(')
122
+ # ruby ver 2.5
123
+ line.gsub!(/_erbout\.<<\(-/,'_erbout.concat0(-')
124
+ line.gsub!(/_erbout\.<<\(\(/, '_erbout.concat1((')
125
+ if num==0
126
+ # skip
127
+ elsif num==1
128
+ f = @filename.dump
129
+ line.sub!(/_erbout = (\+?''|String\.new);/, "_erbout = CountLnString.new(#{f});")
130
+ elsif /^; _erbout\.force_encoding/ =~ line
131
+ line.sub!(/^;/,";_erbout.ln(#{num});")
132
+ end
133
+ line
134
+ end.join+";_erbout.final;"
135
+ end
136
+
137
+ end
@@ -0,0 +1,381 @@
1
+ require 'erbpp'
2
+
3
+ module DefMethod
4
+
5
+ def def_id(meth,var=nil)
6
+ IdVar.new(self, meth, var)
7
+ end
8
+
9
+ def def_method(meth, n_arg, tmpl=nil, opts={})
10
+ h = {:meth => meth, :n_arg => n_arg}
11
+ h.merge!(opts)
12
+ tmpl ||= meth
13
+ Function.new(self, tmpl, h)
14
+ end
15
+
16
+ def def_singleton(meth, n_arg, tmpl=nil, opts={})
17
+ def_method(meth, n_arg, tmpl, :singleton => true)
18
+ end
19
+
20
+ def def_alias(dst, src)
21
+ Alias.new(self, dst, src)
22
+ end
23
+
24
+ def def_allocate(tmpl)
25
+ h = {:meth => "allocate", :singleton => true}
26
+ Allocate.new(self, tmpl, h)
27
+ end
28
+
29
+ def binary(meth, ope=nil)
30
+ ope = meth if ope.nil?
31
+ def_method(meth, 1, "binary", :op => ope)
32
+ end
33
+
34
+ def binary2(meth, ope=nil)
35
+ ope = meth if ope.nil?
36
+ def_method(meth, 1, "binary2", :op =>ope)
37
+ end
38
+
39
+ def unary(meth, ope=nil)
40
+ def_method(meth, 0, "unary", :op => ope)
41
+ end
42
+
43
+ def pow
44
+ def_method("pow", 1, "pow", :op => "**")
45
+ end
46
+
47
+ def unary2(meth, dtype, tpclass)
48
+ h = {:dtype => dtype, :tpclass => tpclass}
49
+ def_method(meth, 0, "unary2", h)
50
+ end
51
+
52
+ def set2(meth, dtype, tpclass)
53
+ h = {:dtype => dtype, :tpclass => tpclass}
54
+ def_method(meth, 1, "set2", h)
55
+ end
56
+
57
+ def cond_binary(meth,op=nil)
58
+ op = meth unless op
59
+ def_method(meth, 1, "cond_binary", :op => op)
60
+ end
61
+
62
+ def cond_unary(meth)
63
+ def_method(meth, 0, "cond_unary")
64
+ end
65
+
66
+ def bit_count(meth)
67
+ def_method(meth, -1, "bit_count")
68
+ end
69
+
70
+ def bit_reduce(meth, init_bit)
71
+ h = {:init_bit=>init_bit}
72
+ def_method(meth, -1, "bit_reduce", h)
73
+ end
74
+
75
+ def accum(meth, dtype, tpclass)
76
+ h = {:dtype => dtype, :tpclass => tpclass}
77
+ def_method(meth, -1, "accum", h)
78
+ end
79
+
80
+ def accum_index(meth)
81
+ def_method(meth, -1, "accum_index")
82
+ end
83
+
84
+ def cum(meth, cmacro)
85
+ def_method(meth, -1, "cum", cmacro:cmacro)
86
+ end
87
+
88
+ def accum_binary(meth, ope=nil)
89
+ ope = meth if ope.nil?
90
+ def_method(meth, -1, "accum_binary", :op => ope)
91
+ end
92
+
93
+ def qsort(tp, dtype, dcast, suffix="")
94
+ h = {:tp => tp, :dtype => dtype, :dcast => dcast, :suffix => suffix}
95
+ NodefFunction.new(self, "qsort", h)
96
+ end
97
+
98
+ def def_mod_func(meth, n_arg, tmpl=nil, opts={})
99
+ h = {:meth => meth, :n_arg => n_arg}
100
+ h.merge!(opts)
101
+ tmpl ||= meth
102
+ ModuleFunction.new(self, tmpl, h)
103
+ end
104
+
105
+ def math(meth, n=1, tmpl=nil)
106
+ h = {:mod_var => 'mTM'}
107
+ if tmpl.nil?
108
+ case n
109
+ when 1
110
+ tmpl = "unary_s"
111
+ when 2
112
+ tmpl = "binary_s"
113
+ when 3
114
+ tmpl = "ternary_s"
115
+ else
116
+ raise "invalid n=#{n}"
117
+ end
118
+ end
119
+ def_mod_func(meth, n, tmpl, h)
120
+ end
121
+
122
+ def store_numeric
123
+ StoreNum.new(self,"store_numeric")
124
+ end
125
+
126
+ def store_array
127
+ StoreArray.new(self,"store_array")
128
+ end
129
+
130
+ def cast_array
131
+ CastArray.new(self,"cast_array")
132
+ end
133
+
134
+ def store_from(cname,dtype,macro)
135
+ Store.new(self,"store_from",cname.downcase,dtype,"cumo_c"+cname,macro)
136
+ end
137
+
138
+ def store_bit(cname)
139
+ Store.new(self,"store_bit",cname.downcase,nil,"cumo_c"+cname,nil)
140
+ end
141
+
142
+ def store
143
+ Function.new(self,"store","store")
144
+ end
145
+
146
+ def find_method(meth)
147
+ Function::DEFS.find{|x| x.kind_of?(Function) and meth == x.meth }
148
+ end
149
+
150
+ def find_tmpl(meth)
151
+ Function::DEFS.find{|x| x.kind_of?(Function) and meth == x.tmpl }
152
+ end
153
+
154
+ def cast_func
155
+ "cumo_#{tp}_s_cast"
156
+ end
157
+ end
158
+
159
+ # ----------------------------------------------------------------------
160
+
161
+ class DataType < ErbPP
162
+ include DefMethod
163
+
164
+ def initialize(erb_path, type_file)
165
+ super(nil, erb_path)
166
+ @class_alias = []
167
+ @upcast = []
168
+ @mod_var = "cT"
169
+ load_type(type_file) if type_file
170
+ dirs = template_dir || ["tmpl"]
171
+ @tmpl_dirs = dirs.map{|d| File.join(File.dirname(erb_path),d)}
172
+ end
173
+
174
+ attr_reader :tmpl_dirs
175
+
176
+ def load_type(file)
177
+ eval File.read(file)
178
+ end
179
+
180
+ attrs = %w[
181
+ class_name
182
+ ctype
183
+ template_dir
184
+ blas_char
185
+ complex_class_name
186
+ complex_type
187
+ real_class_name
188
+ real_ctype
189
+ has_math
190
+ is_bit
191
+ is_int
192
+ is_unsigned
193
+ is_float
194
+ is_real
195
+ is_complex
196
+ is_object
197
+ is_comparable
198
+ is_double_precision
199
+ mod_var
200
+ ]
201
+ define_attrs attrs
202
+
203
+ def type_name
204
+ @type_name ||= class_name.downcase
205
+ end
206
+ alias tp type_name
207
+
208
+ def type_var
209
+ @type_var ||= "cumo_c"+class_name
210
+ end
211
+
212
+ def math_var
213
+ @math_var ||= "cumo_m"+class_name+"Math"
214
+ end
215
+
216
+ def real_class_name(arg=nil)
217
+ if arg.nil?
218
+ @real_class_name ||= class_name
219
+ else
220
+ @real_class_name = arg
221
+ end
222
+ end
223
+
224
+ def real_ctype(arg=nil)
225
+ if arg.nil?
226
+ @real_ctype ||= ctype
227
+ else
228
+ @real_ctype = arg
229
+ end
230
+ end
231
+
232
+ def real_type_var
233
+ @real_type_var ||= "cumo_c"+real_class_name
234
+ end
235
+
236
+ def real_type_name
237
+ @real_type_name ||= real_class_name.downcase
238
+ end
239
+
240
+ def class_alias(*args)
241
+ @class_alias.concat(args)
242
+ end
243
+
244
+ def upcast(c=nil,t=nil)
245
+ if c
246
+ if t
247
+ t = "cumo_c#{t}"
248
+ else
249
+ t = "cT"
250
+ end
251
+ @upcast << "rb_hash_aset(hCast, cumo_c#{c}, #{t});"
252
+ else
253
+ @upcast
254
+ end
255
+ end
256
+
257
+ def upcast_rb(c,t=nil)
258
+ if t
259
+ t = "cumo_c#{t}"
260
+ else
261
+ t = "cT"
262
+ end
263
+ if c=="Integer"
264
+ @upcast << "#ifdef RUBY_INTEGER_UNIFICATION"
265
+ @upcast << "rb_hash_aset(hCast, rb_cInteger, #{t});"
266
+ @upcast << "#else"
267
+ @upcast << "rb_hash_aset(hCast, rb_cFixnum, #{t});"
268
+ @upcast << "rb_hash_aset(hCast, rb_cBignum, #{t});"
269
+ @upcast << "#endif"
270
+ else
271
+ @upcast << "rb_hash_aset(hCast, rb_c#{c}, #{t});"
272
+ end
273
+ end
274
+ end
275
+
276
+
277
+ # ----------------------------------------------------------------------
278
+
279
+
280
+ class Allocate < Function
281
+ def definition
282
+ "rb_define_alloc_func(#{mod_var}, #{c_func});"
283
+ end
284
+ end
285
+
286
+ # ----------------------------------------------------------------------
287
+
288
+ class Store < Function
289
+ DEFS = []
290
+
291
+ def initialize(parent,tmpl,tpname,dtype,tpclass,macro)
292
+ super(parent,tmpl)
293
+ @tpname=tpname
294
+ @dtype=dtype
295
+ @tpclass=tpclass
296
+ @macro=macro
297
+ DEFS.push(self)
298
+ end
299
+ attr_reader :tmpl, :tpname, :dtype, :tpclass, :macro
300
+
301
+ def c_func
302
+ "cumo_#{tp}_store_#{tpname}"
303
+ end
304
+
305
+ def c_iter
306
+ "iter_#{tp}_store_#{tpname}"
307
+ end
308
+
309
+ def definition
310
+ nil
311
+ end
312
+
313
+ def condition(klass)
314
+ "#{klass}==#{tpclass}"
315
+ end
316
+
317
+ def extract_data(ptr,pos,x)
318
+ case tpname
319
+ when "bit"
320
+ "{BIT_DIGIT b; LOAD_BIT(#{ptr},#{pos},b); x = m_from_real(b);}"
321
+ when "robject"
322
+ "#{x} = m_num_to_data(*(#{dtype}*)(#{ptr}+#{pos}))"
323
+ when /complex/
324
+ "{#{dtype} *p = (#{dtype}*)(#{ptr}+#{pos}); #{x} = c_new(REAL(*p),IMAG(*p));}"
325
+ else
326
+ "#{x} = m_from_real(*(#{dtype}*)(#{ptr}+#{pos}))"
327
+ end
328
+ end
329
+
330
+ def self.definitions
331
+ a = []
332
+ DEFS.each do |x|
333
+ if x.condition("")
334
+ if x.tpname == x.parents[0].class_name.downcase
335
+ a.unshift(x)
336
+ else
337
+ a.push(x)
338
+ end
339
+ end
340
+ end
341
+ a
342
+ end
343
+ end
344
+
345
+ class StoreNum < Store
346
+ def initialize(parent,tmpl)
347
+ super(parent,tmpl,"numeric",nil,nil,nil)
348
+ end
349
+
350
+ def condition(klass)
351
+ "IS_INTEGER_CLASS(#{klass}) || #{klass}==rb_cFloat || #{klass}==rb_cComplex"
352
+ end
353
+ end
354
+
355
+ class StoreArray < Store
356
+ def initialize(parent,tmpl)
357
+ super(parent,tmpl,"array",nil,nil,nil)
358
+ end
359
+
360
+ def c_func
361
+ "cumo_#{tp}_#{tmpl}"
362
+ end
363
+
364
+ def condition(klass)
365
+ "#{klass}==rb_cArray"
366
+ end
367
+ end
368
+
369
+ class CastArray < StoreArray
370
+ def condition(klass)
371
+ nil
372
+ end
373
+
374
+ def c_func
375
+ "cumo_#{tp}_cast_#{tpname}"
376
+ end
377
+
378
+ def c_iter
379
+ "iter_#{tp}_cast_#{tpname}"
380
+ end
381
+ end