cumo 0.5.11 → 0.7.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 (174) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +76 -1
  3. data/README.md +133 -0
  4. data/bench/cumo_shape_probe.rb +264 -0
  5. data/bench/cumo_sync_probe.rb +71 -8
  6. data/ext/cumo/cuda/cublas.c +0 -40
  7. data/ext/cumo/cuda/cudnn.c +48 -0
  8. data/ext/cumo/cuda/cudnn_impl.cpp +100 -63
  9. data/ext/cumo/cuda/memory_pool_impl_test.cpp +4 -1
  10. data/ext/cumo/cuda/nvrtc.c +1 -1
  11. data/ext/cumo/cuda/runtime.c +3 -4
  12. data/ext/cumo/cumo.c +34 -0
  13. data/ext/cumo/depend.erb +7 -0
  14. data/ext/cumo/extconf.rb +2 -0
  15. data/ext/cumo/include/cumo/bit_reduce_kernel.h +119 -13
  16. data/ext/cumo/include/cumo/check.h +51 -0
  17. data/ext/cumo/include/cumo/cuda/cudnn.h +47 -43
  18. data/ext/cumo/include/cumo/cuda/cumo_thrust.hpp +1 -0
  19. data/ext/cumo/include/cumo/cuda/runtime.h +28 -0
  20. data/ext/cumo/include/cumo/indexer.h +29 -8
  21. data/ext/cumo/include/cumo/intern.h +4 -1
  22. data/ext/cumo/include/cumo/narray.h +69 -3
  23. data/ext/cumo/include/cumo/ndloop.h +1 -0
  24. data/ext/cumo/include/cumo/reduce_kernel.h +133 -50
  25. data/ext/cumo/include/cumo/template.h +35 -10
  26. data/ext/cumo/include/cumo/template_kernel.h +96 -0
  27. data/ext/cumo/include/cumo/types/bit.h +3 -0
  28. data/ext/cumo/include/cumo/types/bit_kernel.h +3 -0
  29. data/ext/cumo/include/cumo/types/complex_macro.h +2 -0
  30. data/ext/cumo/include/cumo/types/complex_macro_kernel.h +2 -0
  31. data/ext/cumo/include/cumo/types/float_macro.h +17 -2
  32. data/ext/cumo/include/cumo/types/float_macro_kernel.h +17 -2
  33. data/ext/cumo/include/cumo/types/half_def.h +129 -0
  34. data/ext/cumo/include/cumo/types/half_def_kernel.h +28 -0
  35. data/ext/cumo/include/cumo/types/half_macro.h +188 -0
  36. data/ext/cumo/include/cumo/types/half_macro_kernel.h +198 -0
  37. data/ext/cumo/include/cumo/types/hfloat.h +22 -0
  38. data/ext/cumo/include/cumo/types/hfloat_kernel.h +16 -0
  39. data/ext/cumo/include/cumo/types/int_macro.h +20 -0
  40. data/ext/cumo/include/cumo/types/int_macro_kernel.h +20 -0
  41. data/ext/cumo/include/cumo/types/real_accum.h +2 -0
  42. data/ext/cumo/include/cumo/types/real_accum_kernel.h +2 -0
  43. data/ext/cumo/include/cumo/types/robj_macro.h +11 -3
  44. data/ext/cumo/include/cumo/types/xint_macro.h +3 -0
  45. data/ext/cumo/include/cumo/types/xint_macro_kernel.h +2 -0
  46. data/ext/cumo/include/cumo.h +2 -2
  47. data/ext/cumo/narray/data.c +70 -199
  48. data/ext/cumo/narray/data_kernel.cu +113 -0
  49. data/ext/cumo/narray/gen/def/bit.rb +2 -0
  50. data/ext/cumo/narray/gen/def/dcomplex.rb +2 -0
  51. data/ext/cumo/narray/gen/def/dfloat.rb +8 -0
  52. data/ext/cumo/narray/gen/def/hfloat.rb +47 -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 +8 -0
  60. data/ext/cumo/narray/gen/def/uint16.rb +3 -1
  61. data/ext/cumo/narray/gen/def/uint32.rb +3 -1
  62. data/ext/cumo/narray/gen/def/uint64.rb +2 -0
  63. data/ext/cumo/narray/gen/def/uint8.rb +3 -1
  64. data/ext/cumo/narray/gen/erbpp2.rb +6 -0
  65. data/ext/cumo/narray/gen/narray_def.rb +5 -0
  66. data/ext/cumo/narray/gen/spec.rb +11 -0
  67. data/ext/cumo/narray/gen/tmpl/accum.c +1 -1
  68. data/ext/cumo/narray/gen/tmpl/accum_arg.c +1 -1
  69. data/ext/cumo/narray/gen/tmpl/accum_arg_kernel.cu +5 -5
  70. data/ext/cumo/narray/gen/tmpl/accum_binary_kernel.cu +14 -1
  71. data/ext/cumo/narray/gen/tmpl/accum_index.c +1 -1
  72. data/ext/cumo/narray/gen/tmpl/accum_index_kernel.cu +5 -5
  73. data/ext/cumo/narray/gen/tmpl/alloc_func.c +4 -7
  74. data/ext/cumo/narray/gen/tmpl/allocate.c +1 -2
  75. data/ext/cumo/narray/gen/tmpl/aref_cpu.c +1 -1
  76. data/ext/cumo/narray/gen/tmpl/batch_norm.c +27 -36
  77. data/ext/cumo/narray/gen/tmpl/batch_norm_backward.c +26 -35
  78. data/ext/cumo/narray/gen/tmpl/binary.c +3 -3
  79. data/ext/cumo/narray/gen/tmpl/binary2.c +1 -1
  80. data/ext/cumo/narray/gen/tmpl/binary_kernel.cu +69 -2
  81. data/ext/cumo/narray/gen/tmpl/binary_s.c +3 -3
  82. data/ext/cumo/narray/gen/tmpl/clip.c +9 -9
  83. data/ext/cumo/narray/gen/tmpl/cond_binary.c +3 -3
  84. data/ext/cumo/narray/gen/tmpl/cond_unary.c +2 -2
  85. data/ext/cumo/narray/gen/tmpl/conv.c +19 -25
  86. data/ext/cumo/narray/gen/tmpl/conv_grad_w.c +21 -27
  87. data/ext/cumo/narray/gen/tmpl/conv_transpose.c +21 -27
  88. data/ext/cumo/narray/gen/tmpl/cum.c +24 -1
  89. data/ext/cumo/narray/gen/tmpl/cum_kernel.cu +29 -4
  90. data/ext/cumo/narray/gen/tmpl/each.c +2 -3
  91. data/ext/cumo/narray/gen/tmpl/each_with_index.c +5 -21
  92. data/ext/cumo/narray/gen/tmpl/ewcomp.c +1 -1
  93. data/ext/cumo/narray/gen/tmpl/extract_cpu.c +1 -1
  94. data/ext/cumo/narray/gen/tmpl/extract_data.c +1 -1
  95. data/ext/cumo/narray/gen/tmpl/fill.c +2 -2
  96. data/ext/cumo/narray/gen/tmpl/fixed_batch_norm.c +18 -27
  97. data/ext/cumo/narray/gen/tmpl/float_accum_kernel.cu +42 -37
  98. data/ext/cumo/narray/gen/tmpl/format.c +1 -1
  99. data/ext/cumo/narray/gen/tmpl/format_to_a.c +1 -1
  100. data/ext/cumo/narray/gen/tmpl/frexp.c +1 -1
  101. data/ext/cumo/narray/gen/tmpl/gemm.c +78 -27
  102. data/ext/cumo/narray/gen/tmpl/init_class.c +0 -7
  103. data/ext/cumo/narray/gen/tmpl/init_upcast.c +6 -0
  104. data/ext/cumo/narray/gen/tmpl/inspect.c +1 -1
  105. data/ext/cumo/narray/gen/tmpl/lib.c +11 -1
  106. data/ext/cumo/narray/gen/tmpl/map_with_index.c +7 -14
  107. data/ext/cumo/narray/gen/tmpl/median.c +9 -47
  108. data/ext/cumo/narray/gen/tmpl/minmax.c +1 -1
  109. data/ext/cumo/narray/gen/tmpl/poly.c +2 -2
  110. data/ext/cumo/narray/gen/tmpl/pooling_backward.c +6 -18
  111. data/ext/cumo/narray/gen/tmpl/pooling_forward.c +6 -18
  112. data/ext/cumo/narray/gen/tmpl/pow.c +6 -6
  113. data/ext/cumo/narray/gen/tmpl/qsort.c +0 -1
  114. data/ext/cumo/narray/gen/tmpl/rand.c +20 -2
  115. data/ext/cumo/narray/gen/tmpl/rand_kernel.cu +13 -0
  116. data/ext/cumo/narray/gen/tmpl/rand_norm.c +4 -4
  117. data/ext/cumo/narray/gen/tmpl/rand_norm_kernel.cu +7 -5
  118. data/ext/cumo/narray/gen/tmpl/real_accum_kernel.cu +155 -96
  119. data/ext/cumo/narray/gen/tmpl/set2.c +1 -1
  120. data/ext/cumo/narray/gen/tmpl/sort.c +1 -1
  121. data/ext/cumo/narray/gen/tmpl/sort_index.c +6 -3
  122. data/ext/cumo/narray/gen/tmpl/store_array.c +21 -22
  123. data/ext/cumo/narray/gen/tmpl/store_bit.c +2 -2
  124. data/ext/cumo/narray/gen/tmpl/store_from.c +2 -2
  125. data/ext/cumo/narray/gen/tmpl/store_from_kernel.cu +37 -2
  126. data/ext/cumo/narray/gen/tmpl/to_a.c +1 -1
  127. data/ext/cumo/narray/gen/tmpl/unary.c +21 -4
  128. data/ext/cumo/narray/gen/tmpl/unary2.c +2 -2
  129. data/ext/cumo/narray/gen/tmpl/unary_ret2.c +1 -1
  130. data/ext/cumo/narray/gen/tmpl/unary_s.c +2 -2
  131. data/ext/cumo/narray/gen/tmpl_bit/allocate.c +2 -3
  132. data/ext/cumo/narray/gen/tmpl_bit/aref_cpu.c +1 -1
  133. data/ext/cumo/narray/gen/tmpl_bit/binary.c +1 -1
  134. data/ext/cumo/narray/gen/tmpl_bit/bit_count_cpu.c +1 -1
  135. data/ext/cumo/narray/gen/tmpl_bit/bit_reduce.c +4 -1
  136. data/ext/cumo/narray/gen/tmpl_bit/bit_stat.c +36 -0
  137. data/ext/cumo/narray/gen/tmpl_bit/bit_stat_kernel.cu +4 -0
  138. data/ext/cumo/narray/gen/tmpl_bit/each.c +2 -2
  139. data/ext/cumo/narray/gen/tmpl_bit/each_with_index.c +5 -20
  140. data/ext/cumo/narray/gen/tmpl_bit/extract.c +13 -27
  141. data/ext/cumo/narray/gen/tmpl_bit/extract_cpu.c +1 -1
  142. data/ext/cumo/narray/gen/tmpl_bit/fill.c +1 -1
  143. data/ext/cumo/narray/gen/tmpl_bit/format.c +1 -1
  144. data/ext/cumo/narray/gen/tmpl_bit/format_to_a.c +1 -1
  145. data/ext/cumo/narray/gen/tmpl_bit/inspect.c +1 -1
  146. data/ext/cumo/narray/gen/tmpl_bit/mask.c +38 -17
  147. data/ext/cumo/narray/gen/tmpl_bit/mask_kernel.cu +4 -4
  148. data/ext/cumo/narray/gen/tmpl_bit/store_array.c +10 -11
  149. data/ext/cumo/narray/gen/tmpl_bit/store_bit.c +1 -1
  150. data/ext/cumo/narray/gen/tmpl_bit/store_from.c +2 -2
  151. data/ext/cumo/narray/gen/tmpl_bit/to_a.c +1 -1
  152. data/ext/cumo/narray/gen/tmpl_bit/unary.c +1 -1
  153. data/ext/cumo/narray/gen/tmpl_bit/where.c +66 -13
  154. data/ext/cumo/narray/gen/tmpl_bit/where2.c +57 -20
  155. data/ext/cumo/narray/gen/tmpl_bit/where_kernel.cu +6 -6
  156. data/ext/cumo/narray/index.c +83 -26
  157. data/ext/cumo/narray/index_kernel.cu +15 -0
  158. data/ext/cumo/narray/math.c +2 -0
  159. data/ext/cumo/narray/narray.c +421 -178
  160. data/ext/cumo/narray/ndloop.c +158 -26
  161. data/ext/cumo/narray/sort_kernel.cu +39 -18
  162. data/ext/cumo/narray/struct.c +14 -17
  163. data/lib/cumo/cuda/cudnn.rb +1 -1
  164. data/lib/cumo/narray/extra.rb +130 -26
  165. data/test/bit_test.rb +290 -0
  166. data/test/cudnn_test.rb +208 -34
  167. data/test/cumo_test.rb +1 -0
  168. data/test/extra_test.rb +75 -7
  169. data/test/hfloat_test.rb +717 -0
  170. data/test/math_test.rb +4 -1
  171. data/test/narray_alt_coverage_test.rb +7 -0
  172. data/test/narray_test.rb +1865 -10
  173. data/test/test_helper.rb +73 -0
  174. metadata +15 -1
@@ -7,6 +7,11 @@
7
7
  #include "cumo/indexer.h"
8
8
  #include "cumo/reduce_kernel.h"
9
9
 
10
+ #define CUMO_BIT_STAT_MEAN 0
11
+ #define CUMO_BIT_STAT_VAR 1
12
+ #define CUMO_BIT_STAT_STDDEV 2
13
+ #define CUMO_BIT_STAT_RMS 3
14
+
10
15
  namespace cumo_bit_detail {
11
16
 
12
17
  // A Bit reduction addresses its operands the way a numeric one does -- the
@@ -169,7 +174,7 @@ __device__ static inline uint64_t bit_count_axis(
169
174
  }
170
175
 
171
176
  __global__ static void bit_count_reduction_kernel(
172
- cumo_na_bit_reduction_arg_t arg, cumo_detail::cumo_reduce_addr_t ad, cumo_bit_word_addr_t wa, int invert,
177
+ CUMO_GRID_CONSTANT cumo_na_bit_reduction_arg_t arg, CUMO_GRID_CONSTANT cumo_detail::cumo_reduce_addr_t ad, CUMO_GRID_CONSTANT cumo_bit_word_addr_t wa, int invert,
173
178
  int out_block_size, int reduce_block_size, int64_t unit_total_size) {
174
179
  extern __shared__ __align__(8) char sdata_raw[];
175
180
  uint64_t* sdata = reinterpret_cast<uint64_t*>(sdata_raw);
@@ -179,8 +184,8 @@ __global__ static void bit_count_reduction_kernel(
179
184
  int64_t out_total_size = arg.out_indexer.total_size;
180
185
  int64_t reduce_total_size = arg.in_indexer.total_size / out_total_size;
181
186
 
182
- int64_t reduce_offset = tid / out_block_size;
183
- int64_t out_offset = tid % out_block_size;
187
+ int64_t reduce_offset, out_offset;
188
+ cumo_detail::reduce_thread_split(ad, tid, out_block_size, reduce_block_size, &reduce_offset, &out_offset);
184
189
  int64_t out_base = blockIdx.x * out_block_size;
185
190
  int64_t out_stride = gridDim.x * out_block_size;
186
191
 
@@ -191,7 +196,7 @@ __global__ static void bit_count_reduction_kernel(
191
196
  uint64_t accum = bit_count_axis(arg, ad, wa, invert, in_out_off, i_in, reduce_total_size,
192
197
  0, unit_total_size, reduce_offset, reduce_block_size);
193
198
 
194
- accum = cumo_detail::reduce_in_block(accum, sdata, tid, out_block_size, impl);
199
+ accum = cumo_detail::reduce_in_block(accum, sdata, tid, out_block_size, reduce_block_size, !ad.out_inner, impl);
195
200
  if (reduce_offset == 0) {
196
201
  *reinterpret_cast<uint64_t*>(arg.out.ptr + bit_out_offset(arg, ad, i_out)) = accum;
197
202
  }
@@ -202,7 +207,7 @@ __global__ static void bit_count_reduction_kernel(
202
207
  // grid a handful of blocks however long the reduce axis is.
203
208
  template <typename TArg>
204
209
  __global__ static void bit_count_partial_kernel(
205
- TArg arg, cumo_detail::cumo_reduce_addr_t ad, cumo_bit_word_addr_t wa, int invert,
210
+ CUMO_GRID_CONSTANT TArg arg, CUMO_GRID_CONSTANT cumo_detail::cumo_reduce_addr_t ad, CUMO_GRID_CONSTANT cumo_bit_word_addr_t wa, int invert,
206
211
  uint64_t* partial, int64_t n_split, int64_t chunk,
207
212
  int out_block_size, int reduce_block_size, int64_t unit_total_size) {
208
213
  extern __shared__ __align__(8) char sdata_raw[];
@@ -214,8 +219,8 @@ __global__ static void bit_count_partial_kernel(
214
219
  int64_t reduce_total_size = arg.in_indexer.total_size / out_total_size;
215
220
  int64_t partial_total_size = out_total_size * n_split;
216
221
 
217
- int64_t reduce_offset = tid / out_block_size;
218
- int64_t out_offset = tid % out_block_size;
222
+ int64_t reduce_offset, out_offset;
223
+ cumo_detail::reduce_thread_split(ad, tid, out_block_size, reduce_block_size, &reduce_offset, &out_offset);
219
224
  int64_t out_base = blockIdx.x * out_block_size;
220
225
  int64_t out_stride = gridDim.x * out_block_size;
221
226
 
@@ -231,7 +236,7 @@ __global__ static void bit_count_partial_kernel(
231
236
  uint64_t accum = bit_count_axis(arg, ad, wa, invert, in_out_off, i_in, reduce_total_size,
232
237
  begin, end, reduce_offset, reduce_block_size);
233
238
 
234
- accum = cumo_detail::reduce_in_block(accum, sdata, tid, out_block_size, impl);
239
+ accum = cumo_detail::reduce_in_block(accum, sdata, tid, out_block_size, reduce_block_size, !ad.out_inner, impl);
235
240
  if (reduce_offset == 0) {
236
241
  partial[i_out * n_split + i_split] = accum;
237
242
  }
@@ -287,6 +292,71 @@ static inline bit_reduce_plan make_bit_reduce_plan(const TArg& arg) {
287
292
  return p;
288
293
  }
289
294
 
295
+ // mean, var, stddev and rms of a bit array are functions of how many bits are
296
+ // set and how long the axis is, because every element is 0 or 1. They reduce
297
+ // the way count does and differ only in what the store computes.
298
+ __device__ static inline double bit_stat_of_count(uint64_t count, int64_t reduce_total_size, int stat) {
299
+ double k = (double)count;
300
+ double n = (double)reduce_total_size;
301
+ double mean = k / n;
302
+ switch (stat) {
303
+ case CUMO_BIT_STAT_MEAN: return mean;
304
+ case CUMO_BIT_STAT_RMS: return sqrt(mean);
305
+ // x squared is x for a bit, so the squared deviations sum to k - k * mean.
306
+ case CUMO_BIT_STAT_VAR: return (k - k * mean) / (n - 1);
307
+ default: return sqrt((k - k * mean) / (n - 1));
308
+ }
309
+ }
310
+
311
+ __global__ static void bit_stat_reduction_kernel(
312
+ CUMO_GRID_CONSTANT cumo_na_bit_reduction_arg_t arg, CUMO_GRID_CONSTANT cumo_detail::cumo_reduce_addr_t ad, CUMO_GRID_CONSTANT cumo_bit_word_addr_t wa, int stat,
313
+ int out_block_size, int reduce_block_size, int64_t unit_total_size) {
314
+ extern __shared__ __align__(8) char sdata_raw[];
315
+ uint64_t* sdata = reinterpret_cast<uint64_t*>(sdata_raw);
316
+ unsigned int tid = threadIdx.x;
317
+ BitCountImpl impl;
318
+
319
+ int64_t out_total_size = arg.out_indexer.total_size;
320
+ int64_t reduce_total_size = arg.in_indexer.total_size / out_total_size;
321
+
322
+ int64_t reduce_offset, out_offset;
323
+ cumo_detail::reduce_thread_split(ad, tid, out_block_size, reduce_block_size, &reduce_offset, &out_offset);
324
+ int64_t out_base = blockIdx.x * out_block_size;
325
+ int64_t out_stride = gridDim.x * out_block_size;
326
+
327
+ for (int64_t i_out = out_base + out_offset; i_out < out_total_size; i_out += out_stride) {
328
+ ssize_t in_out_off = bit_in_out_offset(arg, ad, i_out);
329
+ int64_t i_in = i_out * reduce_total_size + reduce_offset;
330
+
331
+ uint64_t accum = bit_count_axis(arg, ad, wa, 0, in_out_off, i_in, reduce_total_size,
332
+ 0, unit_total_size, reduce_offset, reduce_block_size);
333
+
334
+ accum = cumo_detail::reduce_in_block(accum, sdata, tid, out_block_size, reduce_block_size, !ad.out_inner, impl);
335
+ if (reduce_offset == 0) {
336
+ *reinterpret_cast<double*>(arg.out.ptr + bit_out_offset(arg, ad, i_out)) =
337
+ bit_stat_of_count(accum, reduce_total_size, stat);
338
+ }
339
+ }
340
+ }
341
+
342
+ // Second pass of a split statistic, laid out like bit_pred_combine_kernel.
343
+ __global__ static void bit_stat_combine_kernel(
344
+ CUMO_GRID_CONSTANT cumo_na_bit_reduction_arg_t arg, CUMO_GRID_CONSTANT cumo_detail::cumo_reduce_addr_t ad, int stat,
345
+ const uint64_t* partial, int64_t n_split) {
346
+ int64_t out_total_size = arg.out_indexer.total_size;
347
+ int64_t reduce_total_size = arg.in_indexer.total_size / out_total_size;
348
+
349
+ for (int64_t i_out = blockIdx.x * blockDim.x + threadIdx.x; i_out < out_total_size;
350
+ i_out += (int64_t)blockDim.x * gridDim.x) {
351
+ uint64_t count = 0;
352
+ for (int64_t i_split = 0; i_split < n_split; ++i_split) {
353
+ count += partial[i_out * n_split + i_split];
354
+ }
355
+ *reinterpret_cast<double*>(arg.out.ptr + bit_out_offset(arg, ad, i_out)) =
356
+ bit_stat_of_count(count, reduce_total_size, stat);
357
+ }
358
+ }
359
+
290
360
  // all? and any? are the count of set bits against the length of the axis and
291
361
  // against zero, so they reduce the same way and differ only here.
292
362
  __device__ static inline CUMO_BIT_DIGIT bit_pred_of_count(uint64_t count, int64_t reduce_total_size, int all) {
@@ -294,7 +364,7 @@ __device__ static inline CUMO_BIT_DIGIT bit_pred_of_count(uint64_t count, int64_
294
364
  }
295
365
 
296
366
  __global__ static void bit_pred_reduction_kernel(
297
- cumo_na_bit_pred_reduction_arg_t arg, cumo_detail::cumo_reduce_addr_t ad, cumo_bit_word_addr_t wa, int all,
367
+ CUMO_GRID_CONSTANT cumo_na_bit_pred_reduction_arg_t arg, CUMO_GRID_CONSTANT cumo_detail::cumo_reduce_addr_t ad, CUMO_GRID_CONSTANT cumo_bit_word_addr_t wa, int all,
298
368
  int out_block_size, int reduce_block_size, int64_t unit_total_size) {
299
369
  extern __shared__ __align__(8) char sdata_raw[];
300
370
  uint64_t* sdata = reinterpret_cast<uint64_t*>(sdata_raw);
@@ -304,8 +374,8 @@ __global__ static void bit_pred_reduction_kernel(
304
374
  int64_t out_total_size = arg.out_indexer.total_size;
305
375
  int64_t reduce_total_size = arg.in_indexer.total_size / out_total_size;
306
376
 
307
- int64_t reduce_offset = tid / out_block_size;
308
- int64_t out_offset = tid % out_block_size;
377
+ int64_t reduce_offset, out_offset;
378
+ cumo_detail::reduce_thread_split(ad, tid, out_block_size, reduce_block_size, &reduce_offset, &out_offset);
309
379
  int64_t out_base = blockIdx.x * out_block_size;
310
380
  int64_t out_stride = gridDim.x * out_block_size;
311
381
 
@@ -316,7 +386,7 @@ __global__ static void bit_pred_reduction_kernel(
316
386
  uint64_t accum = bit_count_axis(arg, ad, wa, 0, in_out_off, i_in, reduce_total_size,
317
387
  0, unit_total_size, reduce_offset, reduce_block_size);
318
388
 
319
- accum = cumo_detail::reduce_in_block(accum, sdata, tid, out_block_size, impl);
389
+ accum = cumo_detail::reduce_in_block(accum, sdata, tid, out_block_size, reduce_block_size, !ad.out_inner, impl);
320
390
  if (reduce_offset == 0) {
321
391
  size_t pos = (size_t)((ssize_t)arg.out.pos + bit_out_offset(arg, ad, i_out));
322
392
  CUMO_STORE_BIT(arg.out.ptr, pos, bit_pred_of_count(accum, reduce_total_size, all));
@@ -327,7 +397,7 @@ __global__ static void bit_pred_reduction_kernel(
327
397
  // Second pass of a split all? or any?. There are few outputs whenever the axis
328
398
  // was worth splitting, so one thread per output walks its chunks.
329
399
  __global__ static void bit_pred_combine_kernel(
330
- cumo_na_bit_pred_reduction_arg_t arg, cumo_detail::cumo_reduce_addr_t ad, int all,
400
+ CUMO_GRID_CONSTANT cumo_na_bit_pred_reduction_arg_t arg, CUMO_GRID_CONSTANT cumo_detail::cumo_reduce_addr_t ad, int all,
331
401
  const uint64_t* partial, int64_t n_split) {
332
402
  int64_t out_total_size = arg.out_indexer.total_size;
333
403
  int64_t reduce_total_size = arg.in_indexer.total_size / out_total_size;
@@ -387,6 +457,42 @@ static inline void cumo_bit_count_reduce(cumo_na_bit_reduction_arg_t arg, int in
387
457
  cumo_cuda_runtime_free(reinterpret_cast<char*>(partial));
388
458
  }
389
459
 
460
+ // The same for the four statistics, whose result is a DFloat of the same width
461
+ // as the count it is computed from.
462
+ static inline void cumo_bit_stat_reduce(cumo_na_bit_reduction_arg_t arg, int stat) {
463
+ if (arg.out_indexer.total_size == 0) {
464
+ return;
465
+ }
466
+
467
+ cumo_bit_detail::bit_reduce_plan p = cumo_bit_detail::make_bit_reduce_plan(arg);
468
+ int64_t block_size = cumo_detail::max_block_size;
469
+ int64_t shared_mem_size = sizeof(uint64_t) * block_size;
470
+
471
+ if (p.n_split < 2) {
472
+ int64_t grid_size = std::min(cumo_detail::max_grid_size, p.out_block_num);
473
+ cumo_bit_detail::bit_stat_reduction_kernel<<<grid_size, block_size, shared_mem_size>>>(
474
+ arg, p.ad, p.wa, stat, (int)p.out_block_size, (int)p.reduce_block_size, p.unit_total_size);
475
+ cumo_cuda_runtime_check_kernel_launch();
476
+ return;
477
+ }
478
+
479
+ int64_t partial_total_size = p.out_total_size * p.n_split;
480
+ uint64_t* partial = reinterpret_cast<uint64_t*>(cumo_cuda_runtime_malloc(sizeof(uint64_t) * partial_total_size));
481
+
482
+ int64_t grid_size = std::min(cumo_detail::max_grid_size, p.partial_block_num);
483
+ cumo_bit_detail::bit_count_partial_kernel<<<grid_size, block_size, shared_mem_size>>>(
484
+ arg, p.ad, p.wa, 0, partial, p.n_split, p.chunk,
485
+ (int)p.split_out_block_size, (int)p.split_reduce_block_size, p.unit_total_size);
486
+ cumo_cuda_runtime_check_kernel_launch();
487
+
488
+ int64_t combine_grid = (p.out_total_size + block_size - 1) / block_size;
489
+ if (combine_grid > cumo_detail::max_grid_size) combine_grid = cumo_detail::max_grid_size;
490
+ cumo_bit_detail::bit_stat_combine_kernel<<<combine_grid, block_size>>>(arg, p.ad, stat, partial, p.n_split);
491
+ cumo_cuda_runtime_check_kernel_launch();
492
+
493
+ cumo_cuda_runtime_free(reinterpret_cast<char*>(partial));
494
+ }
495
+
390
496
  // The same for all? (all non-zero) and any? (any non-zero), whose result is a
391
497
  // Bit rather than a count.
392
498
  static inline void cumo_bit_pred_reduce(cumo_na_bit_pred_reduction_arg_t arg, int all) {
@@ -0,0 +1,51 @@
1
+ #ifndef CUMO_CHECK_H
2
+ #define CUMO_CHECK_H
3
+
4
+ #include <ruby.h>
5
+ #include "cumo/narray.h"
6
+
7
+ #if defined(__cplusplus)
8
+ extern "C" {
9
+ #if 0
10
+ } /* satisfy cc-mode */
11
+ #endif
12
+ #endif
13
+
14
+ #define CUMO_CHECK_NARRAY_TYPE(x,t) \
15
+ if (rb_obj_class(x)!=(t)) { \
16
+ rb_raise(rb_eTypeError,"invalid NArray type (class)"); \
17
+ }
18
+
19
+ #define CUMO_CHECK_SIZE_EQ(sz1,sz2) \
20
+ if ((sz1) != (sz2)) { \
21
+ rb_raise(cumo_na_eShapeError, \
22
+ "size mismatch: %d != %d", \
23
+ (int)(sz1), (int)(sz2)); \
24
+ }
25
+
26
+ #define CUMO_CHECK_DIM_EQ(nd1,nd2) \
27
+ if ((nd1) != (nd2)) { \
28
+ rb_raise(cumo_na_eShapeError, \
29
+ "dimension mismatch: %d != %d", \
30
+ (int)(nd1), (int)(nd2)); \
31
+ }
32
+
33
+ static inline VALUE
34
+ cumo_option_value(VALUE value, VALUE default_value)
35
+ {
36
+ switch(TYPE(value)) {
37
+ case T_NIL:
38
+ case T_UNDEF:
39
+ return default_value;
40
+ }
41
+ return value;
42
+ }
43
+
44
+ #if defined(__cplusplus)
45
+ #if 0
46
+ { /* satisfy cc-mode */
47
+ #endif
48
+ } /* extern "C" { */
49
+ #endif
50
+
51
+ #endif /* CUMO_CHECK_H */
@@ -6,6 +6,7 @@
6
6
  #include <cudnn.h>
7
7
  #endif // CUDNN_FOUND
8
8
  #include "cumo/narray.h"
9
+ #include "cumo/check.h"
9
10
 
10
11
  #if defined(__cplusplus)
11
12
  extern "C" {
@@ -20,29 +21,14 @@ extern VALUE cumo_cuda_eCUDNNError;
20
21
 
21
22
  extern VALUE cumo_na_eShapeError;
22
23
 
23
- #define CUMO_CUDA_CUDNN_DEFAULT_MAX_WORKSPACE_SIZE 8 * 1024 * 1024
24
+ #define CUMO_CUDA_CUDNN_DEFAULT_MAX_WORKSPACE_SIZE (8 * 1024 * 1024)
24
25
 
25
- // TODO: Move to proper generic place
26
- #define CUMO_CUDA_CUDNN_CHECK_NARRAY_TYPE(x,t) \
27
- if (rb_obj_class(x)!=(t)) { \
28
- rb_raise(rb_eTypeError,"invalid NArray type (class)"); \
29
- }
30
-
31
- // TODO: Move to proper generic place
32
- #define CUMO_CUDA_CUDNN_CHECK_SIZE_EQ(sz1,sz2) \
33
- if ((sz1) != (sz2)) { \
34
- rb_raise(cumo_na_eShapeError, \
35
- "size mismatch: %d != %d", \
36
- (int)(sz1), (int)(sz2)); \
37
- }
38
-
39
- // TODO: Move to proper generic place
40
- #define CUMO_CUDA_CUDNN_CHECK_DIM_EQ(nd1,nd2) \
41
- if ((nd1) != (nd2)) { \
42
- rb_raise(cumo_na_eShapeError, \
43
- "dimension mismatch: %d != %d", \
44
- (int)(nd1), (int)(nd2)); \
45
- }
26
+ // How much scratch cuDNN may use to pick a convolution algorithm. The fastest
27
+ // half algorithms are the ones that need the most, so the default keeps every
28
+ // dtype on the algorithms it has always used and CUMO_CUDNN_MAX_WORKSPACE_SIZE
29
+ // raises the ceiling for whoever wants them.
30
+ size_t
31
+ cumo_cuda_cudnn_max_workspace_size();
46
32
 
47
33
  // An output array given by the caller is written through a descriptor built
48
34
  // from another operand, or as if it were contiguous, so cuDNN never learns how
@@ -52,11 +38,11 @@ cumo_cuda_cudnn_check_output(VALUE out, VALUE type, size_t ndim, size_t *shape)
52
38
  {
53
39
  cumo_narray_t *na;
54
40
 
55
- CUMO_CUDA_CUDNN_CHECK_NARRAY_TYPE(out, type);
41
+ CUMO_CHECK_NARRAY_TYPE(out, type);
56
42
  CumoGetNArray(out, na);
57
- CUMO_CUDA_CUDNN_CHECK_DIM_EQ((size_t)(na->ndim), ndim);
43
+ CUMO_CHECK_DIM_EQ((size_t)(na->ndim), ndim);
58
44
  for (size_t idim = 0; idim < ndim; ++idim) {
59
- CUMO_CUDA_CUDNN_CHECK_SIZE_EQ(na->shape[idim], shape[idim]);
45
+ CUMO_CHECK_SIZE_EQ(na->shape[idim], shape[idim]);
60
46
  }
61
47
  if (cumo_na_check_contiguous(out) != Qtrue) {
62
48
  rb_raise(cumo_na_eShapeError, "output NArray must be contiguous");
@@ -89,11 +75,22 @@ cumo_cuda_cudnn_check_input(VALUE in, VALUE type, size_t ndim, size_t *shape)
89
75
  {
90
76
  cumo_narray_t *na;
91
77
 
92
- CUMO_CUDA_CUDNN_CHECK_NARRAY_TYPE(in, type);
78
+ CUMO_CHECK_NARRAY_TYPE(in, type);
93
79
  CumoGetNArray(in, na);
94
- CUMO_CUDA_CUDNN_CHECK_DIM_EQ((size_t)(na->ndim), ndim);
80
+ CUMO_CHECK_DIM_EQ((size_t)(na->ndim), ndim);
95
81
  for (size_t idim = 0; idim < ndim; ++idim) {
96
- CUMO_CUDA_CUDNN_CHECK_SIZE_EQ(na->shape[idim], shape[idim]);
82
+ CUMO_CHECK_SIZE_EQ(na->shape[idim], shape[idim]);
83
+ }
84
+ }
85
+
86
+ // cuDNN derives the batch norm parameter descriptor from x, and widens it to
87
+ // float for a half x, so the parameters do not always take x's own class.
88
+ static inline void
89
+ cumo_cuda_cudnn_check_param_type(VALUE param, VALUE type, const char* name)
90
+ {
91
+ if (rb_obj_class(param) != type) {
92
+ rb_raise(rb_eTypeError, "%s must be %s, not %s",
93
+ name, rb_class2name(type), rb_obj_classname(param));
97
94
  }
98
95
  }
99
96
 
@@ -103,18 +100,6 @@ cumo_cuda_cudnn_check_status(cudnnStatus_t status);
103
100
  cudnnHandle_t
104
101
  cumo_cuda_cudnn_handle();
105
102
 
106
- // TODO: Move to more generic proper place
107
- static inline VALUE
108
- cumo_cuda_cudnn_option_value(VALUE value, VALUE default_value)
109
- {
110
- switch(TYPE(value)) {
111
- case T_NIL:
112
- case T_UNDEF:
113
- return default_value;
114
- }
115
- return value;
116
- }
117
-
118
103
  // VALUE is Ruby Array
119
104
  static inline void
120
105
  cumo_cuda_cudnn_get_int_ary(int* int_ary, VALUE ary, size_t ndim, int default_value)
@@ -130,7 +115,7 @@ cumo_cuda_cudnn_get_int_ary(int* int_ary, VALUE ary, size_t ndim, int default_va
130
115
  }
131
116
  } else {
132
117
  Check_Type(ary, T_ARRAY);
133
- CUMO_CUDA_CUDNN_CHECK_DIM_EQ((size_t)(RARRAY_LEN(ary)), ndim);
118
+ CUMO_CHECK_DIM_EQ((size_t)(RARRAY_LEN(ary)), ndim);
134
119
  for (size_t idim = 0; idim < ndim; ++idim) {
135
120
  int_ary[idim] = NUM2INT(rb_ary_entry(ary, idim));
136
121
  }
@@ -151,10 +136,28 @@ cumo_cuda_cudnn_get_int_axis(int* int_axis, VALUE axis)
151
136
  for (size_t idim = 0; idim < axis_ndim; ++idim) {
152
137
  int_axis[idim] = NUM2INT(rb_ary_entry(axis, (long)idim));
153
138
  }
154
- // TODO: check axis is sorted
155
139
  return axis_ndim;
156
140
  }
157
141
 
142
+ // The axis names dimensions of x, and the batch normalization mode is picked
143
+ // from it. One outside x, repeated, or out of order names neither: the mode
144
+ // comes out the same either way, so the call goes through and answers as
145
+ // though a different axis had been given.
146
+ static inline void
147
+ cumo_cuda_cudnn_check_axis(size_t axis_ndim, int* int_axis, size_t x_ndim)
148
+ {
149
+ for (size_t idim = 0; idim < axis_ndim; ++idim) {
150
+ if (int_axis[idim] < 0 || (size_t)int_axis[idim] >= x_ndim) {
151
+ rb_raise(rb_eArgError, "axis[%d]=%d is out of range for a %d-dimensional array",
152
+ (int)idim, int_axis[idim], (int)x_ndim);
153
+ }
154
+ if (idim > 0 && int_axis[idim] <= int_axis[idim - 1]) {
155
+ rb_raise(rb_eArgError, "axis must increase, but axis[%d]=%d follows axis[%d]=%d",
156
+ (int)idim, int_axis[idim], (int)(idim - 1), int_axis[idim - 1]);
157
+ }
158
+ }
159
+ }
160
+
158
161
  size_t
159
162
  cumo_cuda_cudnn_GetConvOutDim(
160
163
  size_t in_dim,
@@ -187,7 +190,8 @@ cumo_cuda_cudnn_CreateConvolutionDescriptor(
187
190
  size_t ndim,
188
191
  int* int_stride,
189
192
  int* int_pad,
190
- cudnnDataType_t cudnn_dtype);
193
+ cudnnDataType_t compute_dtype,
194
+ cudnnMathType_t math_type);
191
195
 
192
196
  cudnnStatus_t
193
197
  cumo_cuda_cudnn_CreatePoolingDescriptor(
@@ -8,6 +8,7 @@
8
8
  #include <thrust/inner_product.h>
9
9
  #include <thrust/iterator/counting_iterator.h>
10
10
  #include <thrust/iterator/transform_iterator.h>
11
+ #include <thrust/iterator/transform_output_iterator.h>
11
12
  #include <thrust/iterator/permutation_iterator.h>
12
13
  #include <thrust/reduce.h>
13
14
  #include <thrust/scan.h>
@@ -13,6 +13,13 @@ extern "C" {
13
13
 
14
14
  extern VALUE cumo_cuda_eRuntimeError;
15
15
 
16
+ // How many times the whole device has been seen to settle, which is what a host
17
+ // read of managed memory needs. One settling covers every kernel and copy issued
18
+ // before it, so code that recorded the count when it queued work can skip a wait
19
+ // the moment the count has moved on. Only cumo_cuda_runtime_device_synchronize
20
+ // advances it, and a count that is behind costs a wait rather than correctness.
21
+ extern uint64_t cumo_cuda_sync_epoch;
22
+
16
23
  static inline void
17
24
  cumo_cuda_runtime_check_status(cudaError_t status)
18
25
  {
@@ -21,6 +28,27 @@ cumo_cuda_runtime_check_status(cudaError_t status)
21
28
  }
22
29
  }
23
30
 
31
+ static inline void
32
+ cumo_cuda_runtime_device_synchronize(void)
33
+ {
34
+ cumo_cuda_runtime_check_status(cudaDeviceSynchronize());
35
+ cumo_cuda_sync_epoch++;
36
+ }
37
+
38
+ // Asking costs less than half of waiting, and there is nothing to wait for
39
+ // whenever the block stayed off the device. Neither answer advances the settle
40
+ // count: stream 0 going quiet is not the whole device settling, and a host read
41
+ // of managed memory needs the latter where concurrentManagedAccess is 0.
42
+ static inline int
43
+ cumo_cuda_runtime_sync_if_busy(void)
44
+ {
45
+ if (cudaStreamQuery(0) == cudaSuccess) {
46
+ return 0;
47
+ }
48
+ cumo_cuda_runtime_check_status(cudaStreamSynchronize(0));
49
+ return 1;
50
+ }
51
+
24
52
  static inline int
25
53
  cumo_cuda_runtime_get_device_count()
26
54
  {
@@ -289,14 +289,38 @@ cumo_na_make_bit_pred_reduction_arg(cumo_na_loop_t* lp_user, int out_arg)
289
289
 
290
290
  #ifdef __CUDACC__
291
291
 
292
+ // Splits the flat index i into the per-dimension indices. A 64-bit division
293
+ // on the device is a software routine of about a hundred instructions, and a
294
+ // non-contiguous or broadcast operand pays one per dimension per element, so
295
+ // when the whole array fits 32 bits the split runs in 32 bits. The outermost
296
+ // dimension takes what is left without dividing, since i is always below
297
+ // total_size. On an RTX 5070 Ti a [1024,3072] + [3072] runs at 321 GB/s in
298
+ // 64 bits and 609 with this.
299
+ #define CUMO_NA_INDEXER_DECOMPOSE(indexer, i, ndim) \
300
+ do { \
301
+ if ((indexer)->total_size <= 0xffffffffu) { \
302
+ uint32_t i32_ = (uint32_t)(i); \
303
+ for (int j_ = (ndim); --j_ >= 1;) { \
304
+ uint32_t n_ = (uint32_t)(indexer)->shape[j_]; \
305
+ (indexer)->index[j_] = i32_ % n_; \
306
+ i32_ /= n_; \
307
+ } \
308
+ if ((ndim) > 0) (indexer)->index[0] = i32_; \
309
+ } else { \
310
+ uint64_t i64_ = (i); \
311
+ for (int j_ = (ndim); --j_ >= 1;) { \
312
+ (indexer)->index[j_] = i64_ % (indexer)->shape[j_]; \
313
+ i64_ /= (indexer)->shape[j_]; \
314
+ } \
315
+ if ((ndim) > 0) (indexer)->index[0] = i64_; \
316
+ } \
317
+ } while (0)
318
+
292
319
  __host__ __device__
293
320
  static inline void
294
321
  cumo_na_indexer_set_dim(cumo_na_indexer_t* indexer, uint64_t i) {
295
322
  indexer->raw_index = i;
296
- for (int j = indexer->ndim; --j >= 0;) {
297
- indexer->index[j] = i % indexer->shape[j];
298
- i /= indexer->shape[j];
299
- }
323
+ CUMO_NA_INDEXER_DECOMPOSE(indexer, i, indexer->ndim);
300
324
  }
301
325
 
302
326
  // Let compiler optimize
@@ -305,10 +329,7 @@ __host__ __device__ \
305
329
  static inline void \
306
330
  cumo_na_indexer_set_dim##NDIM(cumo_na_indexer_t* indexer, uint64_t i) { \
307
331
  indexer->raw_index = i; \
308
- for (int j = NDIM; --j >= 0;) { \
309
- indexer->index[j] = i % indexer->shape[j]; \
310
- i /= indexer->shape[j]; \
311
- } \
332
+ CUMO_NA_INDEXER_DECOMPOSE(indexer, i, NDIM); \
312
333
  }
313
334
 
314
335
  CUMO_NA_INDEXER_SET(8)
@@ -33,7 +33,9 @@ char* cumo_na_get_offset_pointer_for_read_write(VALUE);
33
33
 
34
34
  void cumo_na_copy_flags(VALUE src, VALUE dst);
35
35
 
36
+ int cumo_na_last_dim_with_elements(const cumo_narray_t *na, int start_dim);
36
37
  VALUE cumo_na_check_ladder(VALUE self, int start_dim);
38
+ void cumo_na_set_newaxis_strides(cumo_narray_view_t *na2, const int *newaxis, int n_newaxis, int ndim, ssize_t elmsz);
37
39
  VALUE cumo_na_check_contiguous(VALUE self);
38
40
  VALUE cumo_na_as_contiguous_array(VALUE a);
39
41
 
@@ -78,8 +80,9 @@ void cumo_na_parse_enumerator_step(VALUE enum_obj, VALUE *pstep);
78
80
 
79
81
  // used in aref, aset
80
82
  int cumo_na_get_result_dimension(VALUE self, int argc, VALUE *argv, ssize_t stride, size_t *pos_idx);
83
+ void cumo_na_index_check_stray(cumo_narray_view_t *nv);
81
84
  void cumo_na_index_mark_filled(cumo_narray_view_t *nv);
82
- void cumo_na_index_wait_fill(const cumo_narray_view_t *nv);
85
+ void cumo_na_index_wait_fill(cumo_narray_view_t *nv);
83
86
  VALUE cumo_na_aref_main(int nidx, VALUE *idx, VALUE self, int keep_dim, int result_nd, size_t pos);
84
87
  VALUE cumo_na_at_main(int nidx, VALUE *idx, VALUE self, int keep_dim, int result_nd, size_t pos);
85
88
 
@@ -189,6 +189,7 @@ extern const rb_data_type_t cumo_na_data_type;
189
189
  extern VALUE cumo_cBit;
190
190
  extern VALUE cumo_cDFloat;
191
191
  extern VALUE cumo_cSFloat;
192
+ extern VALUE cumo_cHFloat;
192
193
  extern VALUE cumo_cDComplex;
193
194
  extern VALUE cumo_cSComplex;
194
195
  extern VALUE cumo_cInt64;
@@ -220,7 +221,6 @@ typedef struct {
220
221
  unsigned char ndim; // # of dimensions
221
222
  unsigned char type;
222
223
  unsigned char flag[2]; // flags
223
- unsigned short elmsz; // element size
224
224
  size_t size; // # of total elements
225
225
  size_t *shape; // # of elements for each dimension
226
226
  VALUE reduce;
@@ -230,7 +230,6 @@ typedef struct {
230
230
  typedef struct {
231
231
  cumo_narray_t base;
232
232
  char *ptr;
233
- bool owned;
234
233
  } cumo_narray_data_t;
235
234
 
236
235
 
@@ -248,8 +247,19 @@ typedef struct {
248
247
  // elm.step_unit = elm.bit_size / elm.access_unit
249
248
  // elm.step_unit = elm.size_bits / elm.unit_bits
250
249
  cumo_stridx_t *stridx; // stride or indices of data pointer for each dimension
250
+ // One past the furthest position this view can reach, in the units its
251
+ // offset and strides are in: bits for Cumo::Bit, bytes otherwise. Zero
252
+ // until it has been worked out. What a view reaches never changes, so it
253
+ // is worked out once, and it stays right even after the base has moved on.
254
+ size_t reach_end;
251
255
  uint64_t index_sync_epoch; // synchronizes counted when the index fills were
252
256
  // issued; UINT64_MAX when that is not known
257
+ // A view made from another one can point at the index arrays it already
258
+ // has rather than copy them. index_owner keeps that view alive for as long
259
+ // as this one borrows from it, and index_owned says which dimensions are
260
+ // this view's own to free.
261
+ VALUE index_owner;
262
+ uint64_t index_owned;
253
263
  } cumo_narray_view_t;
254
264
 
255
265
 
@@ -367,10 +377,10 @@ _cumo_na_get_narray_t(VALUE obj, unsigned char cumo_na_type)
367
377
  #define CUMO_NA_DATA(na) ((cumo_narray_data_t*)(na))
368
378
  #define CUMO_NA_VIEW(na) ((cumo_narray_view_t*)(na))
369
379
  #define CUMO_NA_DATA_PTR(na) (CUMO_NA_DATA(na)->ptr)
370
- #define CUMO_NA_DATA_OWNED(na) (CUMO_NA_DATA(na)->owned)
371
380
  #define CUMO_NA_VIEW_DATA(na) (CUMO_NA_VIEW(na)->data)
372
381
  #define CUMO_NA_VIEW_OFFSET(na) (CUMO_NA_VIEW(na)->offset)
373
382
  #define CUMO_NA_VIEW_STRIDX(na) (CUMO_NA_VIEW(na)->stridx)
383
+ #define CUMO_NA_VIEW_REACH_END(na) (CUMO_NA_VIEW(na)->reach_end)
374
384
 
375
385
  #define CUMO_NA_IS_INDEX_AT(na,i) (CUMO_SDX_IS_INDEX(CUMO_NA_VIEW_STRIDX(na)[i]))
376
386
  #define CUMO_NA_IS_STRIDE_AT(na,i) (CUMO_SDX_IS_STRIDE(CUMO_NA_VIEW_STRIDX(na)[i]))
@@ -405,6 +415,10 @@ _cumo_na_get_narray_t(VALUE obj, unsigned char cumo_na_type)
405
415
  #define CUMO_NA_FL0_COLUMN_MAJOR (0x1<<1)
406
416
  #define CUMO_NA_FL1_LOCK (0x1<<0)
407
417
  #define CUMO_NA_FL1_INPLACE (0x1<<1)
418
+ // Set once an array has been given a shape smaller than one it has had. Views
419
+ // made under the larger shape reach past what it holds now, so that is when
420
+ // they have to be measured; until then there is nothing to measure against.
421
+ #define CUMO_NA_FL1_SHRUNK (0x1<<2)
408
422
 
409
423
  #define CUMO_TEST_COLUMN_MAJOR(x) CUMO_NA_FL0_TEST(x,CUMO_NA_FL0_COLUMN_MAJOR)
410
424
  #define CUMO_SET_COLUMN_MAJOR(x) CUMO_NA_FL0_SET(x,CUMO_NA_FL0_COLUMN_MAJOR)
@@ -497,9 +511,61 @@ typedef unsigned int CUMO_BIT_DIGIT;
497
511
  #define CUMO_BALL (~(CUMO_BIT_DIGIT)0)
498
512
  #define CUMO_SLB(n) (((n)==CUMO_NB)?~(CUMO_BIT_DIGIT)0:(~(~(CUMO_BIT_DIGIT)0<<(n))))
499
513
 
514
+ // Bits are packed into digits, so a buffer holding n of them is as long as the
515
+ // digits they fill, rounded up. n has to be positive: an empty array has no
516
+ // buffer, and the subtraction below would wrap.
517
+ #define CUMO_BIT_BYTES(n) \
518
+ ((((n) - 1) / 8 / sizeof(CUMO_BIT_DIGIT) + 1) * sizeof(CUMO_BIT_DIGIT))
519
+
500
520
  #include "cumo/ndloop.h"
501
521
  #include "cumo/intern.h"
502
522
 
523
+ // An index array a view holds is freed only if it was set through here, so a
524
+ // view that puts one in stridx any other way leaks it. The bit names the slot,
525
+ // so code that permutes stridx has to permute index_owned with it.
526
+ static inline void
527
+ cumo_na_index_own(cumo_narray_view_t *nv, int i, size_t *idx)
528
+ {
529
+ CUMO_SDX_SET_INDEX(nv->stridx[i], idx);
530
+ nv->index_owned |= (uint64_t)1 << i;
531
+ }
532
+
533
+ // One dimension of nv now points at an index array that from reaches, so from
534
+ // has to outlive nv. A lender that owns nothing itself is skipped for its own
535
+ // lender, which keeps the common chain one link long. A lender that owns even
536
+ // one dimension is named directly, since only it keeps that one alive.
537
+ //
538
+ // A view borrows every index array or none. Borrowing some while building
539
+ // others holds the lender for arrays this view has already replaced, which
540
+ // costs more memory than the copy it saves. One lender is recorded, so a view
541
+ // may not borrow from two.
542
+ static inline void
543
+ cumo_na_index_borrow(cumo_narray_view_t *nv, VALUE from)
544
+ {
545
+ cumo_narray_view_t *nv1;
546
+
547
+ if (RTEST(nv->index_owner)) {
548
+ return;
549
+ }
550
+ CumoGetNArrayView(from, nv1);
551
+ nv->index_owner = (nv1->index_owned == 0 && RTEST(nv1->index_owner))
552
+ ? nv1->index_owner : from;
553
+ }
554
+
555
+ // Called once a view built from another one has all its dimensions. A view that
556
+ // owns index arrays built them here, so a fill is in flight and the first read
557
+ // waits. One that borrows them all is as settled as the view it borrowed from.
558
+ static inline void
559
+ cumo_na_index_mark_derived(cumo_narray_view_t *nv, cumo_narray_view_t *nv1)
560
+ {
561
+ if (nv->index_owned != 0) {
562
+ cumo_na_index_mark_filled(nv);
563
+ } else {
564
+ cumo_na_index_check_stray(nv);
565
+ nv->index_sync_epoch = nv1->index_sync_epoch;
566
+ }
567
+ }
568
+
503
569
  // for Ractor support code
504
570
  #ifndef HAVE_RB_EXT_RACTOR_SAFE
505
571
  # undef RUBY_TYPED_FROZEN_SHAREABLE