cumo 0.4.3 → 0.5.1

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 (137) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/.rubocop.yml +15 -0
  4. data/.rubocop_todo.yml +1252 -0
  5. data/3rd_party/mkmf-cu/Gemfile +2 -0
  6. data/3rd_party/mkmf-cu/Rakefile +2 -1
  7. data/3rd_party/mkmf-cu/bin/mkmf-cu-nvcc +2 -0
  8. data/3rd_party/mkmf-cu/lib/mkmf-cu/cli.rb +43 -7
  9. data/3rd_party/mkmf-cu/lib/mkmf-cu/nvcc.rb +51 -45
  10. data/3rd_party/mkmf-cu/lib/mkmf-cu.rb +2 -0
  11. data/3rd_party/mkmf-cu/mkmf-cu.gemspec +3 -1
  12. data/3rd_party/mkmf-cu/test/test_mkmf-cu.rb +5 -3
  13. data/CHANGELOG.md +85 -0
  14. data/Dockerfile +34 -0
  15. data/Gemfile +6 -1
  16. data/README.md +2 -10
  17. data/Rakefile +8 -11
  18. data/bench/broadcast_fp32.rb +28 -26
  19. data/bench/cumo_bench.rb +18 -16
  20. data/bench/numo_bench.rb +18 -16
  21. data/bench/reduction_fp32.rb +14 -12
  22. data/bin/console +1 -0
  23. data/cumo.gemspec +6 -9
  24. data/docker-build.sh +4 -0
  25. data/docker-launch.sh +4 -0
  26. data/docs/src-tree.md +1 -1
  27. data/ext/cumo/cuda/cudnn.c +2 -2
  28. data/ext/cumo/cuda/cudnn_impl.cpp +25 -3
  29. data/ext/cumo/cuda/driver.c +8 -0
  30. data/ext/cumo/cumo.c +7 -3
  31. data/ext/cumo/depend.erb +15 -13
  32. data/ext/cumo/extconf.rb +33 -47
  33. data/ext/cumo/include/cumo/cuda/cudnn.h +3 -1
  34. data/ext/cumo/include/cumo/cuda/cumo_thrust.hpp +13 -6
  35. data/ext/cumo/include/cumo/cuda/cumo_thrust_complex.hpp +3 -3
  36. data/ext/cumo/include/cumo/intern.h +1 -0
  37. data/ext/cumo/include/cumo/narray.h +13 -1
  38. data/ext/cumo/include/cumo/template.h +2 -4
  39. data/ext/cumo/include/cumo/types/complex_macro.h +1 -1
  40. data/ext/cumo/include/cumo/types/complex_macro_kernel.h +15 -4
  41. data/ext/cumo/include/cumo/types/float_macro.h +2 -2
  42. data/ext/cumo/include/cumo/types/real_accum_kernel.h +15 -4
  43. data/ext/cumo/include/cumo/types/xint_macro.h +3 -2
  44. data/ext/cumo/include/cumo/types/xint_macro_kernel.h +11 -3
  45. data/ext/cumo/include/cumo.h +2 -2
  46. data/ext/cumo/narray/array.c +8 -6
  47. data/ext/cumo/narray/data.c +48 -28
  48. data/ext/cumo/narray/gen/cogen.rb +8 -7
  49. data/ext/cumo/narray/gen/cogen_kernel.rb +8 -7
  50. data/ext/cumo/narray/gen/def/bit.rb +3 -1
  51. data/ext/cumo/narray/gen/def/dcomplex.rb +2 -0
  52. data/ext/cumo/narray/gen/def/dfloat.rb +2 -0
  53. data/ext/cumo/narray/gen/def/int16.rb +2 -0
  54. data/ext/cumo/narray/gen/def/int32.rb +2 -0
  55. data/ext/cumo/narray/gen/def/int64.rb +2 -0
  56. data/ext/cumo/narray/gen/def/int8.rb +2 -0
  57. data/ext/cumo/narray/gen/def/robject.rb +2 -0
  58. data/ext/cumo/narray/gen/def/scomplex.rb +2 -0
  59. data/ext/cumo/narray/gen/def/sfloat.rb +2 -0
  60. data/ext/cumo/narray/gen/def/uint16.rb +2 -0
  61. data/ext/cumo/narray/gen/def/uint32.rb +2 -0
  62. data/ext/cumo/narray/gen/def/uint64.rb +2 -0
  63. data/ext/cumo/narray/gen/def/uint8.rb +2 -0
  64. data/ext/cumo/narray/gen/erbln.rb +9 -7
  65. data/ext/cumo/narray/gen/erbpp2.rb +26 -24
  66. data/ext/cumo/narray/gen/narray_def.rb +13 -11
  67. data/ext/cumo/narray/gen/spec.rb +58 -55
  68. data/ext/cumo/narray/gen/tmpl/accum.c +2 -2
  69. data/ext/cumo/narray/gen/tmpl/accum_binary.c +1 -1
  70. data/ext/cumo/narray/gen/tmpl/alloc_func.c +1 -1
  71. data/ext/cumo/narray/gen/tmpl/aref.c +18 -18
  72. data/ext/cumo/narray/gen/tmpl/aset.c +16 -16
  73. data/ext/cumo/narray/gen/tmpl/at.c +34 -0
  74. data/ext/cumo/narray/gen/tmpl/batch_norm.c +5 -2
  75. data/ext/cumo/narray/gen/tmpl/batch_norm_backward.c +6 -3
  76. data/ext/cumo/narray/gen/tmpl/bincount.c +7 -7
  77. data/ext/cumo/narray/gen/tmpl/clip.c +11 -15
  78. data/ext/cumo/narray/gen/tmpl/conv.c +1 -1
  79. data/ext/cumo/narray/gen/tmpl/conv_grad_w.c +3 -1
  80. data/ext/cumo/narray/gen/tmpl/conv_transpose.c +1 -1
  81. data/ext/cumo/narray/gen/tmpl/cum.c +1 -1
  82. data/ext/cumo/narray/gen/tmpl/each.c +4 -2
  83. data/ext/cumo/narray/gen/tmpl/each_with_index.c +5 -2
  84. data/ext/cumo/narray/gen/tmpl/fixed_batch_norm.c +5 -2
  85. data/ext/cumo/narray/gen/tmpl/init_class.c +1 -0
  86. data/ext/cumo/narray/gen/tmpl/logseq.c +6 -5
  87. data/ext/cumo/narray/gen/tmpl/map_with_index.c +5 -6
  88. data/ext/cumo/narray/gen/tmpl/median.c +2 -2
  89. data/ext/cumo/narray/gen/tmpl/minmax.c +1 -1
  90. data/ext/cumo/narray/gen/tmpl/poly.c +4 -4
  91. data/ext/cumo/narray/gen/tmpl/pooling_backward.c +1 -1
  92. data/ext/cumo/narray/gen/tmpl/pooling_forward.c +1 -1
  93. data/ext/cumo/narray/gen/tmpl/qsort.c +1 -5
  94. data/ext/cumo/narray/gen/tmpl/rand.c +8 -6
  95. data/ext/cumo/narray/gen/tmpl/rand_norm.c +18 -16
  96. data/ext/cumo/narray/gen/tmpl/seq.c +5 -4
  97. data/ext/cumo/narray/gen/tmpl/sort.c +3 -3
  98. data/ext/cumo/narray/gen/tmpl/sort_index.c +2 -2
  99. data/ext/cumo/narray/gen/tmpl_bit/aref.c +26 -32
  100. data/ext/cumo/narray/gen/tmpl_bit/aset.c +18 -30
  101. data/ext/cumo/narray/gen/tmpl_bit/binary.c +42 -14
  102. data/ext/cumo/narray/gen/tmpl_bit/bit_count.c +5 -0
  103. data/ext/cumo/narray/gen/tmpl_bit/bit_reduce.c +5 -0
  104. data/ext/cumo/narray/gen/tmpl_bit/mask.c +27 -7
  105. data/ext/cumo/narray/gen/tmpl_bit/store_bit.c +21 -7
  106. data/ext/cumo/narray/gen/tmpl_bit/unary.c +21 -7
  107. data/ext/cumo/narray/index.c +244 -40
  108. data/ext/cumo/narray/index_kernel.cu +84 -0
  109. data/ext/cumo/narray/narray.c +57 -19
  110. data/ext/cumo/narray/ndloop.c +1 -1
  111. data/ext/cumo/narray/struct.c +1 -1
  112. data/lib/cumo/cuda/compile_error.rb +1 -1
  113. data/lib/cumo/cuda/compiler.rb +23 -22
  114. data/lib/cumo/cuda/cudnn.rb +1 -1
  115. data/lib/cumo/cuda/device.rb +1 -1
  116. data/lib/cumo/cuda/link_state.rb +2 -2
  117. data/lib/cumo/cuda/module.rb +1 -2
  118. data/lib/cumo/cuda/nvrtc_program.rb +3 -2
  119. data/lib/cumo/cuda.rb +2 -0
  120. data/lib/cumo/linalg.rb +2 -0
  121. data/lib/cumo/narray/extra.rb +297 -341
  122. data/lib/cumo/narray.rb +2 -0
  123. data/lib/cumo.rb +3 -1
  124. data/test/bit_test.rb +157 -0
  125. data/test/cuda/compiler_test.rb +69 -0
  126. data/test/cuda/device_test.rb +31 -0
  127. data/test/cuda/memory_pool_test.rb +45 -0
  128. data/test/cuda/nvrtc_test.rb +51 -0
  129. data/test/cuda/runtime_test.rb +28 -0
  130. data/test/cudnn_test.rb +498 -0
  131. data/test/cumo_test.rb +27 -0
  132. data/test/narray_test.rb +745 -0
  133. data/test/ractor_test.rb +52 -0
  134. data/test/test_helper.rb +31 -0
  135. metadata +34 -54
  136. data/.travis.yml +0 -5
  137. data/numo-narray-version +0 -1
@@ -62,7 +62,7 @@ static void
62
62
 
63
63
  /*
64
64
  Set logarithmic sequence of numbers to self. The sequence is obtained from
65
- base**(beg+i*step)
65
+ `base**(beg+i*step)`
66
66
  where i is 1-dimensional index.
67
67
  Applicable classes: DFloat, SFloat, DComplex, SCopmplex.
68
68
 
@@ -74,11 +74,12 @@ static void
74
74
 
75
75
  @example
76
76
  Cumo::DFloat.new(5).logseq(4,-1,2)
77
- => Cumo::DFloat#shape=[5]
78
- [16, 8, 4, 2, 1]
77
+ # => Cumo::DFloat#shape=[5]
78
+ # [16, 8, 4, 2, 1]
79
+
79
80
  Cumo::DComplex.new(5).logseq(0,1i*Math::PI/3,Math::E)
80
- => Cumo::DComplex#shape=[5]
81
- [1+7.26156e-310i, 0.5+0.866025i, -0.5+0.866025i, -1+1.22465e-16i, ...]
81
+ # => Cumo::DComplex#shape=[5]
82
+ # [1+7.26156e-310i, 0.5+0.866025i, -0.5+0.866025i, -1+1.22465e-16i, ...]
82
83
  */
83
84
  static VALUE
84
85
  <%=c_func(-1)%>(int argc, VALUE *args, VALUE self)
@@ -78,14 +78,13 @@ static void
78
78
  passing that element and indices along each axis as parameters.
79
79
  Creates a new NArray containing the values returned by the block.
80
80
  Inplace option is allowed, i.e., `nary.inplace.map` overwrites `nary`.
81
-
82
81
  @overload <%=name%>
83
-
84
- For a block {|x,i,j,...| ... }
85
- @yield [x,i,j,...] x is an element, i,j,... are multidimensional indices.
86
-
82
+ For a block `{|x,i,j,...| ... }`,
83
+ @yieldparam [Numeric] x an element
84
+ @yieldparam [Integer] i,j,... multitimensional indices
87
85
  @return [Cumo::NArray] mapped array
88
-
86
+ @see #map
87
+ @see #each_with_index
89
88
  */
90
89
  static VALUE
91
90
  <%=c_func(0)%>(VALUE self)
@@ -40,8 +40,8 @@ static void
40
40
  <% else %>
41
41
  @overload <%=name%>(axis:nil, keepdims:false)
42
42
  <% end %>
43
- @param [Numeric,Array,Range] axis (keyword) Affected dimensions.
44
- @param [TrueClass] keepdims (keyword) If true, the reduced axes are left in the result array as dimensions with size one.
43
+ @param [Numeric,Array,Range] axis Finds <%=name%> along the axis.
44
+ @param [TrueClass] keepdims If true, the reduced axes are left in the result array as dimensions with size one.
45
45
  @return [Cumo::<%=class_name%>] returns <%=name%> of self.
46
46
  */
47
47
 
@@ -26,7 +26,7 @@ static void
26
26
  <% else %>
27
27
  @overload <%=name%>(axis:nil, keepdims:false)
28
28
  <% end %>
29
- @param [Numeric,Array,Range] axis (keyword) Affected dimensions.
29
+ @param [Numeric,Array,Range] axis Finds min-max along the axis.
30
30
  @param [TrueClass] keepdims (keyword) If true, the reduced axes are left in the result array as dimensions with size one.
31
31
  @return [Cumo::<%=class_name%>,Cumo::<%=class_name%>] min and max of self.
32
32
  */
@@ -18,10 +18,10 @@ static void
18
18
  }
19
19
 
20
20
  /*
21
- Polynomial.: a0 + a1*x + a2*x**2 + a3*x**3 + ... + an*x**n
22
- @overload <%=name%> a0, a1, ...
23
- @param [Cumo::NArray,Numeric] a0
24
- @param [Cumo::NArray,Numeric] a1 , ...
21
+ Calculate polynomial.
22
+ `x.poly(a0,a1,a2,...,an) = a0 + a1*x + a2*x**2 + ... + an*x**n`
23
+ @overload <%=name%> a0, a1, ..., an
24
+ @param [Cumo::NArray,Numeric] a0,a1,...,an
25
25
  @return [Cumo::<%=class_name%>]
26
26
  */
27
27
  static VALUE
@@ -126,7 +126,7 @@ POOLING_BACKAWARD_ERROR:
126
126
  }
127
127
 
128
128
  #else // CUDNN_FOUND
129
- VALUE cumo_cuda_eCUDNNError;
129
+ #include "cumo/cuda/cudnn.h"
130
130
 
131
131
  static VALUE
132
132
  <%=c_func(-1)%>(int argc, VALUE argv[], VALUE self)
@@ -126,7 +126,7 @@ POLLING_FORWARD_ERROR:
126
126
  }
127
127
 
128
128
  #else // CUDNN_FOUND
129
- VALUE cumo_cuda_eCUDNNError;
129
+ #include "cumo/cuda/cudnn.h"
130
130
 
131
131
  static VALUE
132
132
  <%=c_func(-1)%>(int argc, VALUE argv[], VALUE self)
@@ -76,11 +76,7 @@
76
76
  (es) % sizeof(long) ? 2 : (es) == sizeof(long)? 0 : 1;
77
77
 
78
78
  static inline void
79
- swapfunc(a, b, n, swaptype)
80
- char *a,
81
- *b;
82
- size_t n;
83
- int swaptype;
79
+ swapfunc(char *a, char *b, size_t n, int swaptype)
84
80
  {
85
81
  if (swaptype <= 1)
86
82
  swapcode(long, a, b, n);
@@ -115,14 +115,16 @@ static void
115
115
  @return [Cumo::<%=class_name%>] self.
116
116
  @example
117
117
  Cumo::DFloat.new(6).rand
118
- => Cumo::DFloat#shape=[6]
119
- [0.0617545, 0.373067, 0.794815, 0.201042, 0.116041, 0.344032]
118
+ # => Cumo::DFloat#shape=[6]
119
+ # [0.0617545, 0.373067, 0.794815, 0.201042, 0.116041, 0.344032]
120
+
120
121
  Cumo::DComplex.new(6).rand(5+5i)
121
- => Cumo::DComplex#shape=[6]
122
- [2.69974+3.68908i, 0.825443+0.254414i, 0.540323+0.34354i, 4.52061+2.39322i, ...]
122
+ # => Cumo::DComplex#shape=[6]
123
+ # [2.69974+3.68908i, 0.825443+0.254414i, 0.540323+0.34354i, 4.52061+2.39322i, ...]
124
+
123
125
  Cumo::Int32.new(6).rand(2,5)
124
- => Cumo::Int32#shape=[6]
125
- [4, 3, 3, 2, 4, 2]
126
+ # => Cumo::Int32#shape=[6]
127
+ # [4, 3, 3, 2, 4, 2]
126
128
  */
127
129
  static VALUE
128
130
  <%=c_func(-1)%>(int argc, VALUE *args, VALUE self)
@@ -77,24 +77,26 @@ static void
77
77
  @return [Cumo::<%=class_name%>] self.
78
78
  @example
79
79
  Cumo::DFloat.new(5,5).rand_norm
80
- => Cumo::DFloat#shape=[5,5]
81
- [[-0.581255, -0.168354, 0.586895, -0.595142, -0.802802],
82
- [-0.326106, 0.282922, 1.68427, 0.918499, -0.0485384],
83
- [-0.464453, -0.992194, 0.413794, -0.60717, -0.699695],
84
- [-1.64168, 0.48676, -0.875871, -1.43275, 0.812172],
85
- [-0.209975, -0.103612, -0.878617, -1.42495, 1.0968]]
80
+ # => Cumo::DFloat#shape=[5,5]
81
+ # [[-0.581255, -0.168354, 0.586895, -0.595142, -0.802802],
82
+ # [-0.326106, 0.282922, 1.68427, 0.918499, -0.0485384],
83
+ # [-0.464453, -0.992194, 0.413794, -0.60717, -0.699695],
84
+ # [-1.64168, 0.48676, -0.875871, -1.43275, 0.812172],
85
+ # [-0.209975, -0.103612, -0.878617, -1.42495, 1.0968]]
86
+
86
87
  Cumo::DFloat.new(5,5).rand_norm(10,0.1)
87
- => Cumo::DFloat#shape=[5,5]
88
- [[9.9019, 9.90339, 10.0826, 9.98384, 9.72861],
89
- [9.81507, 10.0272, 9.91445, 10.0568, 9.88923],
90
- [10.0234, 9.97874, 9.96011, 9.9006, 9.99964],
91
- [10.0186, 9.94598, 9.92236, 9.99811, 9.97003],
92
- [9.79266, 9.95044, 9.95212, 9.93692, 10.2027]]
88
+ # => Cumo::DFloat#shape=[5,5]
89
+ # [[9.9019, 9.90339, 10.0826, 9.98384, 9.72861],
90
+ # [9.81507, 10.0272, 9.91445, 10.0568, 9.88923],
91
+ # [10.0234, 9.97874, 9.96011, 9.9006, 9.99964],
92
+ # [10.0186, 9.94598, 9.92236, 9.99811, 9.97003],
93
+ # [9.79266, 9.95044, 9.95212, 9.93692, 10.2027]]
94
+
93
95
  Cumo::DComplex.new(3,3).rand_norm(5+5i)
94
- => Cumo::DComplex#shape=[3,3]
95
- [[5.84303+4.40052i, 4.00984+6.08982i, 5.10979+5.13215i],
96
- [4.26477+3.99655i, 4.90052+5.00763i, 4.46607+2.3444i],
97
- [4.5528+7.11003i, 5.62117+6.69094i, 5.05443+5.35133i]]
96
+ # => Cumo::DComplex#shape=[3,3]
97
+ # [[5.84303+4.40052i, 4.00984+6.08982i, 5.10979+5.13215i],
98
+ # [4.26477+3.99655i, 4.90052+5.00763i, 4.46607+2.3444i],
99
+ # [4.5528+7.11003i, 5.62117+6.69094i, 5.05443+5.35133i]]
98
100
  */
99
101
  static VALUE
100
102
  <%=c_func(-1)%>(int argc, VALUE *args, VALUE self)
@@ -80,11 +80,12 @@ static void
80
80
  @return [Cumo::<%=class_name%>] self.
81
81
  @example
82
82
  Cumo::DFloat.new(6).seq(1,-0.2)
83
- => Cumo::DFloat#shape=[6]
84
- [1, 0.8, 0.6, 0.4, 0.2, 0]
83
+ # => Cumo::DFloat#shape=[6]
84
+ # [1, 0.8, 0.6, 0.4, 0.2, 0]
85
+
85
86
  Cumo::DComplex.new(6).seq(1,-0.2+0.2i)
86
- => Cumo::DComplex#shape=[6]
87
- [1+0i, 0.8+0.2i, 0.6+0.4i, 0.4+0.6i, 0.2+0.8i, 0+1i]
87
+ # => Cumo::DComplex#shape=[6]
88
+ # [1+0i, 0.8+0.2i, 0.6+0.4i, 0.4+0.6i, 0.2+0.8i, 0+1i]
88
89
  */
89
90
  static VALUE
90
91
  <%=c_func(-1)%>(int argc, VALUE *args, VALUE self)
@@ -22,17 +22,17 @@ static void
22
22
  <% else %>
23
23
  @overload <%=name%>(axis:nil)
24
24
  <% end %>
25
- @param [Numeric,Array,Range] axis Affected dimensions.
25
+ @param [Numeric,Array,Range] axis Performs <%=name%> along the axis.
26
26
  @return [Cumo::<%=class_name%>] returns result of <%=name%>.
27
27
  @example
28
- Cumo::DFloat[3,4,1,2].sort => Cumo::DFloat[1,2,3,4]
28
+ Cumo::DFloat[3,4,1,2].sort # => Cumo::DFloat[1,2,3,4]
29
29
  */
30
30
  static VALUE
31
31
  <%=c_func(-1)%>(int argc, VALUE *argv, VALUE self)
32
32
  {
33
33
  VALUE reduce;
34
34
  cumo_ndfunc_arg_in_t ain[2] = {{CUMO_OVERWRITE,0},{cumo_sym_reduce,0}};
35
- cumo_ndfunc_t ndf = {0, CUMO_STRIDE_LOOP|CUMO_NDF_FLAT_REDUCE, 2,0, ain,0};
35
+ cumo_ndfunc_t ndf = {0, CUMO_NDF_HAS_LOOP|CUMO_NDF_FLAT_REDUCE, 2,0, ain,0};
36
36
 
37
37
  if (!CUMO_TEST_INPLACE(self)) {
38
38
  self = cumo_na_copy(self);
@@ -52,10 +52,10 @@ static void
52
52
  <% else %>
53
53
  @overload <%=name%>(axis:nil)
54
54
  <% end %>
55
- @param [Numeric,Array,Range] axis Affected dimensions.
55
+ @param [Numeric,Array,Range] axis Performs <%=name%> along the axis.
56
56
  @return [Integer,Cumo::Int] returns result index of <%=name%>.
57
57
  @example
58
- Cumo::NArray[3,4,1,2].sort_index => Cumo::Int32[2,3,0,1]
58
+ Cumo::NArray[3,4,1,2].sort_index # => Cumo::Int32[2,3,0,1]
59
59
  */
60
60
  static VALUE
61
61
  <%=c_func(-1)%>(int argc, VALUE *argv, VALUE self)
@@ -2,42 +2,36 @@ static VALUE
2
2
  <%=c_func(-1)%>_cpu(int argc, VALUE *argv, VALUE self);
3
3
 
4
4
  /*
5
- Array element referenece or slice view.
5
+ Array indexing.
6
6
  @overload [](dim0,...,dimL)
7
- @param [Numeric,Range,etc] dim0,...,dimL Multi-dimensional Index.
8
- @return [Numeric,NArray::<%=class_name%>] Element object or NArray view.
7
+ @param [Numeric,Range,Array,Cumo::Bit,Cumo::Int32,Cumo::Int64] dim0,...,dimL Multi-dimensional Index.
8
+ @return [Cumo::Bit,Numeric] Element value or NArray view.
9
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.
10
+ --- Returns an element at `dim0`, `dim1`, ... are Numeric indices for each dimension, or returns a NArray View as a sliced array if `dim0`, `dim1`, ... includes other than Numeric index, e.g., Range or Array or true.
11
+
12
+ @see #[]=
14
13
 
15
14
  @example
16
- a = Cumo::DFloat.new(4,5).seq
17
- => Cumo::DFloat#shape=[4,5]
18
- [[0, 1, 2, 3, 4],
19
- [5, 6, 7, 8, 9],
20
- [10, 11, 12, 13, 14],
21
- [15, 16, 17, 18, 19]]
22
-
23
- a[1,1]
24
- => 6.0
25
-
26
- a[1..3,1]
27
- => Cumo::DFloat#shape=[3]
28
- [6, 11, 16]
29
-
30
- a[1,[1,3,4]]
31
- => Cumo::DFloat#shape=[3]
32
- [6, 8, 9]
33
-
34
- a[true,2].fill(99)
35
- a
36
- => Cumo::DFloat#shape=[4,5]
37
- [[0, 1, 99, 3, 4],
38
- [5, 6, 99, 8, 9],
39
- [10, 11, 99, 13, 14],
40
- [15, 16, 99, 18, 19]]
15
+ a = Cumo::Int32.new(3,4).seq
16
+ # => Cumo::Int32#shape=[3,4]
17
+ # [[0, 1, 2, 3],
18
+ # [4, 5, 6, 7],
19
+ # [8, 9, 10, 11]]
20
+
21
+ b = (a%2).eq(0)
22
+ # => Cumo::Bit#shape=[3,4]
23
+ # [[1, 0, 1, 0],
24
+ # [1, 0, 1, 0],
25
+ # [1, 0, 1, 0]]
26
+
27
+ b[true,(0..-1)%2]
28
+ # => Cumo::Bit(view)#shape=[3,2]
29
+ # [[1, 1],
30
+ # [1, 1],
31
+ # [1, 1]]
32
+
33
+ b[1,1]
34
+ # => 0
41
35
  */
42
36
  static VALUE
43
37
  <%=c_func(-1)%>(int argc, VALUE *argv, VALUE self)
@@ -1,41 +1,29 @@
1
1
  /*
2
- Array element(s) set.
2
+ Array element(s) assignment.
3
3
  @overload []=(dim0,..,dimL,val)
4
- @param [Numeric,Range,etc] dim0,..,dimL Multi-dimensional Index.
4
+ @param [Numeric,Range,Array,Cumo::Bit,Cumo::Int32,Cumo::Int64] dim0,..,dimL Multi-dimensional Index.
5
5
  @param [Numeric,Cumo::NArray,etc] val Value(s) to be set to self.
6
- @return [Numeric] returns val (last argument).
6
+ @return [Numeric] returns `val` (last argument).
7
7
 
8
- --- Replace element(s) at +dim0+, +dim1+, ... (index/range/array/true
9
- for each dimention). Broadcasting mechanism is applied.
8
+ Replaces element(s) at `dim0`, `dim1`, ... . Broadcasting mechanism is applied.
10
9
 
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]]
10
+ @see #[]
24
11
 
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]]
12
+ @example
13
+ a = Cumo::Bit.new(4,5).fill(0)
14
+ # => Cumo::Bit#shape=[4,5]
15
+ # [[0, 0, 0, 0, 0],
16
+ # [0, 0, 0, 0, 0],
17
+ # [0, 0, 0, 0, 0],
18
+ # [0, 0, 0, 0, 0]]
31
19
 
32
- a[1,true]=99
20
+ a[(0..-1)%2,(1..-1)%2] = 1
33
21
  a
34
- => Cumo::DFloat#shape=[3,4]
35
- [[0, 1, 2, 3],
36
- [99, 99, 99, 99],
37
- [8, 9, 10, 11]]
38
-
22
+ # => Cumo::Bit#shape=[4,5]
23
+ # [[0, 1, 0, 1, 0],
24
+ # [0, 0, 0, 0, 0],
25
+ # [0, 1, 0, 1, 0],
26
+ # [0, 0, 0, 0, 0]]
39
27
  */
40
28
  static VALUE
41
29
  <%=c_func(-1)%>(int argc, VALUE *argv, VALUE self)
@@ -25,10 +25,8 @@ static void
25
25
  CUMO_STORE_BIT_STEP(a3, p3, s3, idx3, x);
26
26
  }
27
27
  } else {
28
- o1 = p1 % CUMO_NB;
29
- o1 -= p3;
30
- o2 = p2 % CUMO_NB;
31
- o2 -= p3;
28
+ o1 = p1-p3;
29
+ o2 = p2-p3;
32
30
  l1 = CUMO_NB+o1;
33
31
  r1 = CUMO_NB-o1;
34
32
  l2 = CUMO_NB+o2;
@@ -58,23 +56,53 @@ static void
58
56
  }
59
57
  } else {
60
58
  for (; n>=CUMO_NB; n-=CUMO_NB) {
61
- x = *a1>>o1;
62
- if (o1<0) x |= *(a1-1)>>l1;
63
- if (o1>0) x |= *(a1+1)<<r1;
59
+ if (o1==0) {
60
+ x = *a1;
61
+ } else if (o1>0) {
62
+ x = *a1>>o1 | *(a1+1)<<r1;
63
+ } else {
64
+ x = *a1<<-o1 | *(a1-1)>>l1;
65
+ }
64
66
  a1++;
65
- y = *a2>>o2;
66
- if (o2<0) y |= *(a2-1)>>l2;
67
- if (o2>0) y |= *(a2+1)<<r2;
67
+ if (o2==0) {
68
+ y = *a2;
69
+ } else if (o2>0) {
70
+ y = *a2>>o2 | *(a2+1)<<r2;
71
+ } else {
72
+ y = *a2<<-o2 | *(a2-1)>>l2;
73
+ }
68
74
  a2++;
69
75
  x = m_<%=name%>(x,y);
70
76
  *(a3++) = x;
71
77
  }
72
78
  }
73
79
  if (n>0) {
74
- x = *a1>>o1;
75
- if (o1<0) x |= *(a1-1)>>l1;
76
- y = *a2>>o2;
77
- if (o2<0) y |= *(a2-1)>>l2;
80
+ if (o1==0) {
81
+ x = *a1;
82
+ } else if (o1>0) {
83
+ x = *a1>>o1;
84
+ if ((int)n>r1) {
85
+ x |= *(a1+1)<<r1;
86
+ }
87
+ } else {
88
+ x = *(a1-1)>>l1;
89
+ if ((int)n>-o1) {
90
+ x |= *a1<<-o1;
91
+ }
92
+ }
93
+ if (o2==0) {
94
+ y = *a2;
95
+ } else if (o2>0) {
96
+ y = *a2>>o2;
97
+ if ((int)n>r2) {
98
+ y |= *(a2+1)<<r2;
99
+ }
100
+ } else {
101
+ y = *(a2-1)>>l2;
102
+ if ((int)n>-o2) {
103
+ y |= *a2<<-o2;
104
+ }
105
+ }
78
106
  x = m_<%=name%>(x,y);
79
107
  *a3 = (x & CUMO_SLB(n)) | (*a3 & CUMO_BALL<<n);
80
108
  }
@@ -53,10 +53,15 @@ static VALUE
53
53
  return <%=c_func(-1)%>_cpu(argc, argv, self);
54
54
  } else {
55
55
  VALUE v, reduce;
56
+ cumo_narray_t *na;
56
57
  cumo_ndfunc_arg_in_t ain[3] = {{cT,0},{cumo_sym_reduce,0},{cumo_sym_init,0}};
57
58
  cumo_ndfunc_arg_out_t aout[1] = {{cumo_cUInt64,0}};
58
59
  cumo_ndfunc_t ndf = { <%=c_iter%>, CUMO_FULL_LOOP_NIP, 3, 1, ain, aout };
59
60
 
61
+ CumoGetNArray(self,na);
62
+ if (CUMO_NA_SIZE(na)==0) {
63
+ return INT2FIX(0);
64
+ }
60
65
  reduce = cumo_na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
61
66
  v = cumo_na_ndloop(&ndf, 3, self, reduce, INT2FIX(0));
62
67
  return v;
@@ -111,10 +111,15 @@ static VALUE
111
111
  <%=c_func(-1)%>(int argc, VALUE *argv, VALUE self)
112
112
  {
113
113
  VALUE v, reduce;
114
+ cumo_narray_t *na;
114
115
  cumo_ndfunc_arg_in_t ain[3] = {{cT,0},{cumo_sym_reduce,0},{cumo_sym_init,0}};
115
116
  cumo_ndfunc_arg_out_t aout[1] = {{cumo_cBit,0}};
116
117
  cumo_ndfunc_t ndf = {<%=c_iter%>, CUMO_FULL_LOOP_NIP, 3,1, ain,aout};
117
118
 
119
+ CumoGetNArray(self,na);
120
+ if (CUMO_NA_SIZE(na)==0) {
121
+ return INT2FIX(0);
122
+ }
118
123
  reduce = cumo_na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
119
124
  v = cumo_na_ndloop(&ndf, 3, self, reduce, INT2FIX(<%=init_bit%>));
120
125
  if (argc > 0) {
@@ -78,6 +78,10 @@ static void
78
78
  #define cIndex cumo_cInt32
79
79
  #endif
80
80
 
81
+ static void shape_error(void) {
82
+ rb_raise(cumo_na_eShapeError,"mask and masked arrays must have the same shape");
83
+ }
84
+
81
85
  /*
82
86
  Return subarray of argument masked with self bit array.
83
87
  @overload <%=op_map%>(array)
@@ -87,17 +91,33 @@ static void
87
91
  static VALUE
88
92
  <%=c_func(1)%>(VALUE mask, VALUE val)
89
93
  {
90
- volatile VALUE idx_1, view;
94
+ int i;
95
+ VALUE idx_1, view;
91
96
  cumo_narray_data_t *nidx;
92
- cumo_narray_view_t *nv;
93
- cumo_narray_t *na;
94
- cumo_narray_view_t *na1;
97
+ cumo_narray_view_t *nv, *nv_val;
98
+ cumo_narray_t *na, *na_mask;
95
99
  cumo_stridx_t stridx0;
96
100
  size_t n_1;
97
101
  where_opt_t g;
98
102
  cumo_ndfunc_arg_in_t ain[2] = {{cT,0},{Qnil,0}};
99
103
  cumo_ndfunc_t ndf = {<%=c_iter%>, CUMO_FULL_LOOP, 2, 0, ain, 0};
100
104
 
105
+ // cast val to NArray
106
+ if (!rb_obj_is_kind_of(val, cumo_cNArray)) {
107
+ val = rb_funcall(cumo_cNArray, cumo_id_cast, 1, val);
108
+ }
109
+ // shapes of mask and val must be same
110
+ CumoGetNArray(val, na);
111
+ CumoGetNArray(mask, na_mask);
112
+ if (na_mask->ndim != na->ndim) {
113
+ shape_error();
114
+ }
115
+ for (i=0; i<na->ndim; i++) {
116
+ if (na_mask->shape[i] != na->shape[i]) {
117
+ shape_error();
118
+ }
119
+ }
120
+
101
121
  // TODO(sonots): bit_count_true synchronizes with CPU. Avoid.
102
122
  n_1 = NUM2SIZET(<%=find_tmpl("count_true_cpu").c_func%>(0, NULL, mask));
103
123
  idx_1 = cumo_na_new(cIndex, 1, &n_1);
@@ -114,19 +134,19 @@ static VALUE
114
134
  CumoGetNArrayData(idx_1,nidx);
115
135
  CUMO_SDX_SET_INDEX(stridx0,(size_t*)nidx->ptr);
116
136
  nidx->ptr = NULL;
137
+ RB_GC_GUARD(idx_1);
117
138
 
118
139
  nv->stridx = ALLOC_N(cumo_stridx_t,1);
119
140
  nv->stridx[0] = stridx0;
120
141
  nv->offset = 0;
121
142
 
122
- CumoGetNArray(val, na);
123
143
  switch(CUMO_NA_TYPE(na)) {
124
144
  case CUMO_NARRAY_DATA_T:
125
145
  nv->data = val;
126
146
  break;
127
147
  case CUMO_NARRAY_VIEW_T:
128
- CumoGetNArrayView(val, na1);
129
- nv->data = na1->data;
148
+ CumoGetNArrayView(val, nv_val);
149
+ nv->data = nv_val->data;
130
150
  break;
131
151
  default:
132
152
  rb_raise(rb_eRuntimeError,"invalid CUMO_NA_TYPE: %d",CUMO_NA_TYPE(na));
@@ -22,8 +22,7 @@ static void
22
22
  CUMO_STORE_BIT_STEP(a3, p3, s3, idx3, x);
23
23
  }
24
24
  } else {
25
- o1 = p1 % CUMO_NB;
26
- o1 -= p3;
25
+ o1 = p1-p3;
27
26
  l1 = CUMO_NB+o1;
28
27
  r1 = CUMO_NB-o1;
29
28
  if (p3>0 || n<CUMO_NB) {
@@ -44,16 +43,31 @@ static void
44
43
  }
45
44
  } else {
46
45
  for (; n>=CUMO_NB; n-=CUMO_NB) {
47
- x = *a1>>o1;
48
- if (o1<0) x |= *(a1-1)>>l1;
49
- if (o1>0) x |= *(a1+1)<<r1;
46
+ if (o1==0) {
47
+ x = *a1;
48
+ } else if (o1>0) {
49
+ x = *a1>>o1 | *(a1+1)<<r1;
50
+ } else {
51
+ x = *a1<<-o1 | *(a1-1)>>l1;
52
+ }
50
53
  a1++;
51
54
  *(a3++) = x;
52
55
  }
53
56
  }
54
57
  if (n>0) {
55
- x = *a1>>o1;
56
- if (o1<0) x |= *(a1-1)>>l1;
58
+ if (o1==0) {
59
+ x = *a1;
60
+ } else if (o1>0) {
61
+ x = *a1>>o1;
62
+ if ((int)n>r1) {
63
+ x |= *(a1+1)<<r1;
64
+ }
65
+ } else {
66
+ x = *(a1-1)>>l1;
67
+ if ((int)n>-o1) {
68
+ x |= *a1<<-o1;
69
+ }
70
+ }
57
71
  *a3 = (x & CUMO_SLB(n)) | (*a3 & CUMO_BALL<<n);
58
72
  }
59
73
  }
@@ -24,8 +24,7 @@ static void
24
24
  CUMO_STORE_BIT_STEP(a3, p3, s3, idx3, y);
25
25
  }
26
26
  } else {
27
- o1 = p1 % CUMO_NB;
28
- o1 -= p3;
27
+ o1 = p1-p3;
29
28
  l1 = CUMO_NB+o1;
30
29
  r1 = CUMO_NB-o1;
31
30
  if (p3>0 || n<CUMO_NB) {
@@ -48,17 +47,32 @@ static void
48
47
  }
49
48
  } else {
50
49
  for (; n>=CUMO_NB; n-=CUMO_NB) {
51
- x = *a1>>o1;
52
- if (o1<0) x |= *(a1-1)>>l1;
53
- if (o1>0) x |= *(a1+1)<<r1;
50
+ if (o1==0) {
51
+ x = *a1;
52
+ } else if (o1>0) {
53
+ x = *a1>>o1 | *(a1+1)<<r1;
54
+ } else {
55
+ x = *a1<<-o1 | *(a1-1)>>l1;
56
+ }
54
57
  a1++;
55
58
  y = m_<%=name%>(x);
56
59
  *(a3++) = y;
57
60
  }
58
61
  }
59
62
  if (n>0) {
60
- x = *a1>>o1;
61
- if (o1<0) x |= *(a1-1)>>l1;
63
+ if (o1==0) {
64
+ x = *a1;
65
+ } else if (o1>0) {
66
+ x = *a1>>o1;
67
+ if ((int)n>r1) {
68
+ x |= *(a1+1)<<r1;
69
+ }
70
+ } else {
71
+ x = *(a1-1)>>l1;
72
+ if ((int)n>-o1) {
73
+ x |= *a1<<-o1;
74
+ }
75
+ }
62
76
  y = m_<%=name%>(x);
63
77
  *a3 = (y & CUMO_SLB(n)) | (*a3 & CUMO_BALL<<n);
64
78
  }