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
@@ -48,6 +48,7 @@ typedef struct {
48
48
  #define CUMO_NDF_CUM (1<<8)
49
49
 
50
50
  #define CUMO_NDF_INDEXER_LOOP (1<<9) // Cumo custom. Use cumo own indexer.
51
+ #define CUMO_NDF_ANY_ORDER (1<<10) // Cumo custom. The elements may be visited in any order, so ndloop may reorder the axes.
51
52
 
52
53
  #define CUMO_FULL_LOOP (CUMO_NDF_HAS_LOOP|CUMO_NDF_STRIDE_LOOP|CUMO_NDF_INDEX_LOOP|CUMO_NDF_INPLACE)
53
54
  #define CUMO_FULL_LOOP_NIP (CUMO_NDF_HAS_LOOP|CUMO_NDF_STRIDE_LOOP|CUMO_NDF_INDEX_LOOP)
@@ -8,6 +8,18 @@
8
8
 
9
9
  #include "cumo/indexer.h"
10
10
 
11
+ // A kernel parameter that is read through a runtime index, the way the steps
12
+ // of an operand are, is copied to local memory by the compiler unless it is
13
+ // declared __grid_constant__, and the copy is paid by every thread: the second
14
+ // operand of a zip reduction cost a 104-byte stack frame that put mulsum along
15
+ // a middle axis at three times the sum of the same bytes. The qualifier needs
16
+ // CUDA 11.7 and sm_70; below those the parameter is merely const.
17
+ #if (defined(__CUDA_ARCH__) && __CUDA_ARCH__ < 700) || (defined(__CUDACC_VER_MAJOR__) && (__CUDACC_VER_MAJOR__ < 11 || (__CUDACC_VER_MAJOR__ == 11 && __CUDACC_VER_MINOR__ < 7)))
18
+ #define CUMO_GRID_CONSTANT const
19
+ #else
20
+ #define CUMO_GRID_CONSTANT const __grid_constant__
21
+ #endif
22
+
11
23
  namespace cumo_detail {
12
24
 
13
25
  static constexpr int64_t max_block_size = 512;
@@ -138,16 +150,27 @@ static inline void set_reduce_addr_out2(cumo_reduce_addr_t* ad, const cumo_na_re
138
150
  ad->out2_flat = axes_are_flat(out2, arg.out_indexer, 0, arg.out_indexer.ndim, &ad->out2_step);
139
151
  }
140
152
 
141
- // Threads of a block are consecutive in i_out below out_block_size and
142
- // consecutive in i_reduce above it, so whichever of the two runs along memory
143
- // decides whether a block's reads coalesce. Handing the reduce axis every
144
- // thread is right when it is the contiguous one and wrong when it is not: a
145
- // 2048x2048 sum along axis 0 otherwise puts each of a block's 512 threads on a
146
- // different row and reads one element per transaction.
153
+ // How a block's threads are shared between outputs. Whichever of the two axes
154
+ // runs along memory decides whether a block's reads coalesce, so the threads
155
+ // that sit next to each other in the block walk that axis: consecutive in
156
+ // i_out when the out axis is the contiguous one (a 2048x2048 sum along axis 0
157
+ // otherwise puts each of a block's 512 threads on a different row and reads
158
+ // one element per transaction), and consecutive in i_reduce when the reduce
159
+ // axis is. In that second, reduce-major form the threads of one output are a
160
+ // contiguous group of reduce_block_size, and the group is sized so that every
161
+ // thread has min_reduce_per_thread elements to read rather than one: a
162
+ // 4096x256 sum along the last axis with a 256-thread group per row reads one
163
+ // element per thread and then spends the time in the tree, at 92 GB/s, where
164
+ // a group of 16 reading 16 each gets 520.
165
+ static constexpr int64_t min_reduce_per_thread = 16;
166
+
147
167
  static inline void reduce_block_split(const cumo_reduce_addr_t& ad, int64_t reduce_total_size, int64_t* out_block_size, int64_t* reduce_block_size) {
148
- int64_t rbs = std::min(max_block_size, round_up_to_power_of_2(std::max(int64_t{1}, reduce_total_size)));
168
+ int64_t n = std::max(int64_t{1}, reduce_total_size);
169
+ int64_t rbs;
149
170
  if (ad.out_inner) {
150
- rbs = std::min(rbs, max_block_size / warp_size);
171
+ rbs = std::min(max_block_size / warp_size, round_up_to_power_of_2((n + min_reduce_per_thread - 1) / min_reduce_per_thread));
172
+ } else {
173
+ rbs = std::min(max_block_size, round_up_to_power_of_2((n + min_reduce_per_thread - 1) / min_reduce_per_thread));
151
174
  }
152
175
  *reduce_block_size = rbs;
153
176
  *out_block_size = max_block_size / rbs;
@@ -157,52 +180,112 @@ static inline void reduce_block_split(const cumo_reduce_addr_t& ad, int64_t redu
157
180
  // iarray's steps carry. Everything here is
158
181
  // read out of the kernel parameter and nothing is written back, which is what
159
182
  // keeps the indexer out of local memory.
183
+ //
184
+ // i is always below the product of the dims, so the outermost one takes what
185
+ // is left without a division.
160
186
  template <typename TIarray>
161
- __device__ static inline ssize_t axes_offset(const TIarray& iarray, const cumo_na_indexer_t& indexer, int begin, int end, int64_t i) {
187
+ __device__ static __forceinline__ ssize_t axes_offset(const TIarray& iarray, const cumo_na_indexer_t& indexer, int begin, int end, int64_t i) {
188
+ if (end <= begin) return 0;
162
189
  ssize_t off = 0;
163
- for (int j = end; --j >= begin;) {
190
+ for (int j = end; --j > begin;) {
164
191
  int64_t n = static_cast<int64_t>(indexer.shape[j]);
165
192
  off += static_cast<ssize_t>(i % n) * iarray.step[j];
166
193
  i /= n;
167
194
  }
168
- return off;
195
+ return off + static_cast<ssize_t>(i) * iarray.step[begin];
196
+ }
197
+
198
+ // The same split of i handed to two operands that share the indexer, which is
199
+ // what the two inputs of a zip reduction are.
200
+ template <typename TIarray>
201
+ __device__ static __forceinline__ void axes_offset_pair(const TIarray& a, const TIarray& b, const cumo_na_indexer_t& indexer, int begin, int end, int64_t i, ssize_t* off_a, ssize_t* off_b) {
202
+ *off_a = 0;
203
+ *off_b = 0;
204
+ if (end <= begin) return;
205
+ for (int j = end; --j > begin;) {
206
+ int64_t n = static_cast<int64_t>(indexer.shape[j]);
207
+ ssize_t r = static_cast<ssize_t>(i % n);
208
+ *off_a += r * a.step[j];
209
+ *off_b += r * b.step[j];
210
+ i /= n;
211
+ }
212
+ *off_a += static_cast<ssize_t>(i) * a.step[begin];
213
+ *off_b += static_cast<ssize_t>(i) * b.step[begin];
169
214
  }
170
215
 
171
216
  // The input side takes the iarray and the indexer rather than the whole
172
217
  // reduction arg, so that mulsum can address its second operand with the same
173
218
  // indexer and a step set of its own.
174
219
  template <bool FLAT>
175
- __device__ static inline ssize_t reduce_in_out_offset(const cumo_na_iarray_t& in, const cumo_na_indexer_t& in_indexer, const cumo_reduce_addr_t& ad, int64_t i_out) {
220
+ __device__ static __forceinline__ ssize_t reduce_in_out_offset(const cumo_na_iarray_t& in, const cumo_na_indexer_t& in_indexer, const cumo_reduce_addr_t& ad, int64_t i_out) {
176
221
  if (FLAT || ad.in_out_flat) return i_out * ad.in_out_step;
177
222
  if (ad.split < 0) return 0;
178
223
  return axes_offset(in, in_indexer, 0, ad.split, i_out);
179
224
  }
180
225
 
226
+ // reduce_in_out_offset for the two operands of a zip reduction at once.
181
227
  template <bool FLAT>
182
- __device__ static inline ssize_t reduce_in_offset(const cumo_na_iarray_t& in, const cumo_na_indexer_t& in_indexer, const cumo_reduce_addr_t& ad, ssize_t in_out_off, int64_t i_reduce, int64_t i_in) {
228
+ __device__ static __forceinline__ void reduce_in_out_offset_pair(const cumo_na_iarray_t& in, const cumo_na_iarray_t& in2, const cumo_na_indexer_t& in_indexer, const cumo_reduce_addr_t& ad, const cumo_reduce_addr_t& ad2, int64_t i_out, ssize_t* off, ssize_t* off2) {
229
+ if (!FLAT && !ad.in_out_flat && !ad2.in_out_flat && ad.split >= 0 && ad.split == ad2.split) {
230
+ axes_offset_pair(in, in2, in_indexer, 0, ad.split, i_out, off, off2);
231
+ return;
232
+ }
233
+ *off = reduce_in_out_offset<FLAT>(in, in_indexer, ad, i_out);
234
+ *off2 = reduce_in_out_offset<FLAT>(in2, in_indexer, ad2, i_out);
235
+ }
236
+
237
+ template <bool FLAT>
238
+ __device__ static __forceinline__ ssize_t reduce_in_offset(const cumo_na_iarray_t& in, const cumo_na_indexer_t& in_indexer, const cumo_reduce_addr_t& ad, ssize_t in_out_off, int64_t i_reduce, int64_t i_in) {
183
239
  if (FLAT || ad.in_reduce_flat) return in_out_off + i_reduce * ad.in_reduce_step;
184
240
  if (ad.split < 0) return axes_offset(in, in_indexer, 0, in_indexer.ndim, i_in);
185
241
  return in_out_off + axes_offset(in, in_indexer, ad.split, in_indexer.ndim, i_reduce);
186
242
  }
187
243
 
188
244
  template <bool FLAT>
189
- __device__ static inline ssize_t reduce_out_offset(const cumo_na_reduction_arg_t& arg, const cumo_reduce_addr_t& ad, int64_t i_out) {
245
+ __device__ static __forceinline__ ssize_t reduce_out_offset(const cumo_na_reduction_arg_t& arg, const cumo_reduce_addr_t& ad, int64_t i_out) {
190
246
  if (FLAT || ad.out_flat) return i_out * ad.out_step;
191
247
  return axes_offset(arg.out, arg.out_indexer, 0, arg.out_indexer.ndim, i_out);
192
248
  }
193
249
 
194
250
  template <bool FLAT>
195
- __device__ static inline ssize_t reduce_out2_offset(const cumo_na_reduction_arg_t& arg, const cumo_na_iarray_t& out2, const cumo_reduce_addr_t& ad, int64_t i_out) {
251
+ __device__ static __forceinline__ ssize_t reduce_out2_offset(const cumo_na_reduction_arg_t& arg, const cumo_na_iarray_t& out2, const cumo_reduce_addr_t& ad, int64_t i_out) {
196
252
  if (FLAT || ad.out2_flat) return i_out * ad.out2_step;
197
253
  return axes_offset(out2, arg.out_indexer, 0, arg.out_indexer.ndim, i_out);
198
254
  }
199
255
 
200
- // Combines the accumulators a block holds for one output element. Threads that
201
- // share out_offset sit reduce_block_size apart, so the tree only has to run
202
- // down to out_block_size.
256
+ // Which output and which slice of its reduce axis a thread takes, in the
257
+ // layout reduce_block_split describes.
258
+ __device__ static __forceinline__ void reduce_thread_split(const cumo_reduce_addr_t& ad, unsigned int tid, int out_block_size, int reduce_block_size, int64_t* reduce_offset, int64_t* out_offset) {
259
+ if (!ad.out_inner) {
260
+ *reduce_offset = tid % reduce_block_size;
261
+ *out_offset = tid / reduce_block_size;
262
+ } else {
263
+ *reduce_offset = tid / out_block_size;
264
+ *out_offset = tid % out_block_size;
265
+ }
266
+ }
267
+
268
+ // Combines the accumulators a block holds for one output element. In the
269
+ // out-major layout the threads that share out_offset sit out_block_size apart,
270
+ // so the tree only has to run down to out_block_size. In the reduce-major
271
+ // layout they are a contiguous, warp-aligned group of reduce_block_size, so a
272
+ // step whose partners are within a warp needs only __syncwarp.
203
273
  template <typename TypeReduce, typename ReductionImpl>
204
- __device__ static inline TypeReduce reduce_in_block(TypeReduce accum, TypeReduce* sdata, unsigned int tid, int out_block_size, ReductionImpl& impl) {
205
- if (out_block_size > max_block_size / 2) return accum;
274
+ __device__ static __forceinline__ TypeReduce reduce_in_block(TypeReduce accum, TypeReduce* sdata, unsigned int tid, int out_block_size, int reduce_block_size, bool reduce_major, ReductionImpl& impl) {
275
+ if (reduce_block_size == 1) return accum;
276
+ if (reduce_major) {
277
+ int r = tid % reduce_block_size;
278
+ sdata[tid] = accum;
279
+ for (int stride = reduce_block_size / 2; stride > 0; stride >>= 1) {
280
+ if (stride >= warp_size) __syncthreads(); else __syncwarp();
281
+ if (r < stride) {
282
+ impl.Reduce(sdata[tid + stride], sdata[tid]);
283
+ }
284
+ }
285
+ accum = sdata[tid];
286
+ if (reduce_block_size > warp_size) __syncthreads(); else __syncwarp();
287
+ return accum;
288
+ }
206
289
  sdata[tid] = accum;
207
290
  __syncthreads();
208
291
  // NOTE: Compiler optimizes to unroll this loop
@@ -230,7 +313,7 @@ __device__ static inline TypeReduce reduce_in_block(TypeReduce accum, TypeReduce
230
313
  // the offset of the element from the start of the input, which is the index
231
314
  // (min|max)_index answers with.
232
315
  template <bool FLAT, bool ARG_INDEX, typename TypeIn, typename ReductionImpl>
233
- __device__ static inline auto reduce_axis(const cumo_na_iarray_t& in, const cumo_na_indexer_t& in_indexer, const cumo_reduce_addr_t& ad, ReductionImpl& impl,
316
+ __device__ static __forceinline__ auto reduce_axis(const cumo_na_iarray_t& in, const cumo_na_indexer_t& in_indexer, const cumo_reduce_addr_t& ad, ReductionImpl& impl,
234
317
  ssize_t in_out_off, int64_t i_in, int64_t begin, int64_t end, int64_t reduce_offset, int64_t reduce_block_size) -> decltype(impl.Identity(0)) {
235
318
  using TypeReduce = decltype(impl.Identity(0));
236
319
 
@@ -258,7 +341,7 @@ __device__ static inline auto reduce_axis(const cumo_na_iarray_t& in, const cumo
258
341
  // an array. The pair comes out of one broadcast, so arg.in_indexer addresses
259
342
  // both and only the steps differ, which is what in2 and ad2 carry.
260
343
  template <bool FLAT, typename TypeIn, typename ReductionImpl>
261
- __device__ static inline auto reduce_axis_zip(const cumo_na_reduction_arg_t& arg, const cumo_na_iarray_t& in2,
344
+ __device__ static __forceinline__ auto reduce_axis_zip(const cumo_na_reduction_arg_t& arg, const cumo_na_iarray_t& in2,
262
345
  const cumo_reduce_addr_t& ad, const cumo_reduce_addr_t& ad2, ReductionImpl& impl,
263
346
  ssize_t in_out_off, ssize_t in_out_off2, int64_t i_in, int64_t begin, int64_t end,
264
347
  int64_t reduce_offset, int64_t reduce_block_size) -> decltype(impl.Identity(0)) {
@@ -288,7 +371,7 @@ __device__ static inline auto reduce_axis_zip(const cumo_na_reduction_arg_t& arg
288
371
  // Note that reduction and out axis are inverse with cupy. Former axes are out axes, latters are reduce axes.
289
372
 
290
373
  template <bool FLAT, typename TypeIn, typename TypeOut, typename ReductionImpl>
291
- __global__ static void reduction_kernel(cumo_na_reduction_arg_t arg, cumo_reduce_addr_t ad, int out_block_size, int reduce_block_size, ReductionImpl impl) {
374
+ __global__ static void reduction_kernel(CUMO_GRID_CONSTANT cumo_na_reduction_arg_t arg, CUMO_GRID_CONSTANT cumo_reduce_addr_t ad, int out_block_size, int reduce_block_size, ReductionImpl impl) {
292
375
  using TypeReduce = decltype(impl.Identity(0));
293
376
 
294
377
  extern __shared__ __align__(8) char sdata_raw[];
@@ -298,8 +381,8 @@ __global__ static void reduction_kernel(cumo_na_reduction_arg_t arg, cumo_reduce
298
381
  int64_t out_total_size = arg.out_indexer.total_size;
299
382
  int64_t reduce_total_size = arg.in_indexer.total_size / out_total_size;
300
383
 
301
- int64_t reduce_offset = tid / out_block_size; // # of cols == # of elems
302
- int64_t out_offset = tid % out_block_size; // # of rows
384
+ int64_t reduce_offset, out_offset;
385
+ reduce_thread_split(ad, tid, out_block_size, reduce_block_size, &reduce_offset, &out_offset);
303
386
  int64_t out_base = blockIdx.x * out_block_size;
304
387
  int64_t out_stride = gridDim.x * out_block_size;
305
388
 
@@ -309,7 +392,7 @@ __global__ static void reduction_kernel(cumo_na_reduction_arg_t arg, cumo_reduce
309
392
 
310
393
  TypeReduce accum = reduce_axis<FLAT,false,TypeIn>(arg.in, arg.in_indexer, ad, impl, in_out_off, i_in, 0, reduce_total_size, reduce_offset, reduce_block_size);
311
394
 
312
- accum = reduce_in_block(accum, sdata, tid, out_block_size, impl);
395
+ accum = reduce_in_block(accum, sdata, tid, out_block_size, reduce_block_size, !ad.out_inner, impl);
313
396
  if (reduce_offset == 0) {
314
397
  TypeOut* out_ptr = reinterpret_cast<TypeOut*>(arg.out.ptr + reduce_out_offset<FLAT>(arg, ad, i_out));
315
398
  *out_ptr = impl.MapOut(accum);
@@ -320,7 +403,7 @@ __global__ static void reduction_kernel(cumo_na_reduction_arg_t arg, cumo_reduce
320
403
  // Variant of reduction_kernel reading two inputs, for mulsum. See
321
404
  // reduce_axis_zip above.
322
405
  template <bool FLAT, typename TypeIn, typename TypeOut, typename ReductionImpl>
323
- __global__ static void reduction_zip_kernel(cumo_na_reduction_arg_t arg, cumo_na_iarray_t in2, cumo_reduce_addr_t ad, cumo_reduce_addr_t ad2, int out_block_size, int reduce_block_size, ReductionImpl impl) {
406
+ __global__ static void reduction_zip_kernel(CUMO_GRID_CONSTANT cumo_na_reduction_arg_t arg, CUMO_GRID_CONSTANT cumo_na_iarray_t in2, CUMO_GRID_CONSTANT cumo_reduce_addr_t ad, CUMO_GRID_CONSTANT cumo_reduce_addr_t ad2, int out_block_size, int reduce_block_size, ReductionImpl impl) {
324
407
  using TypeReduce = decltype(impl.Identity(0));
325
408
 
326
409
  extern __shared__ __align__(8) char sdata_raw[];
@@ -330,19 +413,19 @@ __global__ static void reduction_zip_kernel(cumo_na_reduction_arg_t arg, cumo_na
330
413
  int64_t out_total_size = arg.out_indexer.total_size;
331
414
  int64_t reduce_total_size = arg.in_indexer.total_size / out_total_size;
332
415
 
333
- int64_t reduce_offset = tid / out_block_size;
334
- int64_t out_offset = tid % out_block_size;
416
+ int64_t reduce_offset, out_offset;
417
+ reduce_thread_split(ad, tid, out_block_size, reduce_block_size, &reduce_offset, &out_offset);
335
418
  int64_t out_base = blockIdx.x * out_block_size;
336
419
  int64_t out_stride = gridDim.x * out_block_size;
337
420
 
338
421
  for (int64_t i_out = out_base + out_offset; i_out < out_total_size; i_out += out_stride) {
339
- ssize_t in_out_off = reduce_in_out_offset<FLAT>(arg.in, arg.in_indexer, ad, i_out);
340
- ssize_t in_out_off2 = reduce_in_out_offset<FLAT>(in2, arg.in_indexer, ad2, i_out);
422
+ ssize_t in_out_off, in_out_off2;
423
+ reduce_in_out_offset_pair<FLAT>(arg.in, in2, arg.in_indexer, ad, ad2, i_out, &in_out_off, &in_out_off2);
341
424
  int64_t i_in = i_out * reduce_total_size + reduce_offset;
342
425
 
343
426
  TypeReduce accum = reduce_axis_zip<FLAT,TypeIn>(arg, in2, ad, ad2, impl, in_out_off, in_out_off2, i_in, 0, reduce_total_size, reduce_offset, reduce_block_size);
344
427
 
345
- accum = reduce_in_block(accum, sdata, tid, out_block_size, impl);
428
+ accum = reduce_in_block(accum, sdata, tid, out_block_size, reduce_block_size, !ad.out_inner, impl);
346
429
  if (reduce_offset == 0) {
347
430
  TypeOut* out_ptr = reinterpret_cast<TypeOut*>(arg.out.ptr + reduce_out_offset<FLAT>(arg, ad, i_out));
348
431
  *out_ptr = impl.MapOut(accum);
@@ -353,7 +436,7 @@ __global__ static void reduction_zip_kernel(cumo_na_reduction_arg_t arg, cumo_na
353
436
  // Variant of reduction_kernel for arg-reductions (argmax/argmin), which report
354
437
  // the index along the reduction axis rather than the index of an element.
355
438
  template <bool FLAT, typename TypeIn, typename TypeOut, typename ReductionImpl>
356
- __global__ static void reduction_arg_kernel(cumo_na_reduction_arg_t arg, cumo_reduce_addr_t ad, int out_block_size, int reduce_block_size, ReductionImpl impl) {
439
+ __global__ static void reduction_arg_kernel(CUMO_GRID_CONSTANT cumo_na_reduction_arg_t arg, CUMO_GRID_CONSTANT cumo_reduce_addr_t ad, int out_block_size, int reduce_block_size, ReductionImpl impl) {
357
440
  using TypeReduce = decltype(impl.Identity(0));
358
441
 
359
442
  extern __shared__ __align__(8) char sdata_raw[];
@@ -363,8 +446,8 @@ __global__ static void reduction_arg_kernel(cumo_na_reduction_arg_t arg, cumo_re
363
446
  int64_t out_total_size = arg.out_indexer.total_size;
364
447
  int64_t reduce_total_size = arg.in_indexer.total_size / out_total_size;
365
448
 
366
- int64_t reduce_offset = tid / out_block_size;
367
- int64_t out_offset = tid % out_block_size;
449
+ int64_t reduce_offset, out_offset;
450
+ reduce_thread_split(ad, tid, out_block_size, reduce_block_size, &reduce_offset, &out_offset);
368
451
  int64_t out_base = blockIdx.x * out_block_size;
369
452
  int64_t out_stride = gridDim.x * out_block_size;
370
453
 
@@ -374,7 +457,7 @@ __global__ static void reduction_arg_kernel(cumo_na_reduction_arg_t arg, cumo_re
374
457
 
375
458
  TypeReduce accum = reduce_axis<FLAT,true,TypeIn>(arg.in, arg.in_indexer, ad, impl, in_out_off, i_in, 0, reduce_total_size, reduce_offset, reduce_block_size);
376
459
 
377
- accum = reduce_in_block(accum, sdata, tid, out_block_size, impl);
460
+ accum = reduce_in_block(accum, sdata, tid, out_block_size, reduce_block_size, !ad.out_inner, impl);
378
461
  if (reduce_offset == 0) {
379
462
  TypeOut* out_ptr = reinterpret_cast<TypeOut*>(arg.out.ptr + reduce_out_offset<FLAT>(arg, ad, i_out));
380
463
  *out_ptr = impl.MapOut(accum);
@@ -386,7 +469,7 @@ __global__ static void reduction_arg_kernel(cumo_na_reduction_arg_t arg, cumo_re
386
469
  // minmax reads the input once instead of reducing it twice. The impl stores
387
470
  // through the pointers rather than returning, since there are two of them.
388
471
  template <bool FLAT, typename TypeIn, typename TypeOut, typename ReductionImpl>
389
- __global__ static void reduction_pair_kernel(cumo_na_reduction_arg_t arg, cumo_na_iarray_t out2_iarray, cumo_reduce_addr_t ad, int out_block_size, int reduce_block_size, ReductionImpl impl) {
472
+ __global__ static void reduction_pair_kernel(CUMO_GRID_CONSTANT cumo_na_reduction_arg_t arg, CUMO_GRID_CONSTANT cumo_na_iarray_t out2_iarray, CUMO_GRID_CONSTANT cumo_reduce_addr_t ad, int out_block_size, int reduce_block_size, ReductionImpl impl) {
390
473
  using TypeReduce = decltype(impl.Identity(0));
391
474
 
392
475
  extern __shared__ __align__(8) char sdata_raw[];
@@ -396,8 +479,8 @@ __global__ static void reduction_pair_kernel(cumo_na_reduction_arg_t arg, cumo_n
396
479
  int64_t out_total_size = arg.out_indexer.total_size;
397
480
  int64_t reduce_total_size = arg.in_indexer.total_size / out_total_size;
398
481
 
399
- int64_t reduce_offset = tid / out_block_size;
400
- int64_t out_offset = tid % out_block_size;
482
+ int64_t reduce_offset, out_offset;
483
+ reduce_thread_split(ad, tid, out_block_size, reduce_block_size, &reduce_offset, &out_offset);
401
484
  int64_t out_base = blockIdx.x * out_block_size;
402
485
  int64_t out_stride = gridDim.x * out_block_size;
403
486
 
@@ -407,7 +490,7 @@ __global__ static void reduction_pair_kernel(cumo_na_reduction_arg_t arg, cumo_n
407
490
 
408
491
  TypeReduce accum = reduce_axis<FLAT,false,TypeIn>(arg.in, arg.in_indexer, ad, impl, in_out_off, i_in, 0, reduce_total_size, reduce_offset, reduce_block_size);
409
492
 
410
- accum = reduce_in_block(accum, sdata, tid, out_block_size, impl);
493
+ accum = reduce_in_block(accum, sdata, tid, out_block_size, reduce_block_size, !ad.out_inner, impl);
411
494
  if (reduce_offset == 0) {
412
495
  TypeOut* out_ptr = reinterpret_cast<TypeOut*>(arg.out.ptr + reduce_out_offset<FLAT>(arg, ad, i_out));
413
496
  TypeOut* out2_ptr = reinterpret_cast<TypeOut*>(out2_iarray.ptr + reduce_out2_offset<FLAT>(arg, out2_iarray, ad, i_out));
@@ -424,7 +507,7 @@ __global__ static void reduction_pair_kernel(cumo_na_reduction_arg_t arg, cumo_n
424
507
  // threads are handed out by output first, so a block still covers consecutive
425
508
  // outputs when those are the ones running along memory.
426
509
  template <bool FLAT, bool ARG, typename TypeIn, typename TypeReduce, typename ReductionImpl>
427
- __global__ static void reduction_partial_kernel(cumo_na_reduction_arg_t arg, cumo_reduce_addr_t ad, TypeReduce* partial, int64_t n_split, int64_t chunk, int out_block_size, int reduce_block_size, ReductionImpl impl) {
510
+ __global__ static void reduction_partial_kernel(CUMO_GRID_CONSTANT cumo_na_reduction_arg_t arg, CUMO_GRID_CONSTANT cumo_reduce_addr_t ad, TypeReduce* partial, int64_t n_split, int64_t chunk, int out_block_size, int reduce_block_size, ReductionImpl impl) {
428
511
  extern __shared__ __align__(8) char sdata_raw[];
429
512
  TypeReduce* sdata = reinterpret_cast<TypeReduce*>(sdata_raw);
430
513
  unsigned int tid = threadIdx.x;
@@ -433,8 +516,8 @@ __global__ static void reduction_partial_kernel(cumo_na_reduction_arg_t arg, cum
433
516
  int64_t reduce_total_size = arg.in_indexer.total_size / out_total_size;
434
517
  int64_t partial_total_size = out_total_size * n_split;
435
518
 
436
- int64_t reduce_offset = tid / out_block_size;
437
- int64_t out_offset = tid % out_block_size;
519
+ int64_t reduce_offset, out_offset;
520
+ reduce_thread_split(ad, tid, out_block_size, reduce_block_size, &reduce_offset, &out_offset);
438
521
  int64_t out_base = blockIdx.x * out_block_size;
439
522
  int64_t out_stride = gridDim.x * out_block_size;
440
523
 
@@ -449,7 +532,7 @@ __global__ static void reduction_partial_kernel(cumo_na_reduction_arg_t arg, cum
449
532
 
450
533
  TypeReduce accum = reduce_axis<FLAT,ARG,TypeIn>(arg.in, arg.in_indexer, ad, impl, in_out_off, i_in, begin, end, reduce_offset, reduce_block_size);
451
534
 
452
- accum = reduce_in_block(accum, sdata, tid, out_block_size, impl);
535
+ accum = reduce_in_block(accum, sdata, tid, out_block_size, reduce_block_size, !ad.out_inner, impl);
453
536
  if (reduce_offset == 0) {
454
537
  partial[i_out * n_split + i_split] = accum;
455
538
  }
@@ -458,7 +541,7 @@ __global__ static void reduction_partial_kernel(cumo_na_reduction_arg_t arg, cum
458
541
 
459
542
  // First pass of a split zip reduction. See reduction_partial_kernel above.
460
543
  template <bool FLAT, typename TypeIn, typename TypeReduce, typename ReductionImpl>
461
- __global__ static void reduction_zip_partial_kernel(cumo_na_reduction_arg_t arg, cumo_na_iarray_t in2, cumo_reduce_addr_t ad, cumo_reduce_addr_t ad2, TypeReduce* partial, int64_t n_split, int64_t chunk, int out_block_size, int reduce_block_size, ReductionImpl impl) {
544
+ __global__ static void reduction_zip_partial_kernel(CUMO_GRID_CONSTANT cumo_na_reduction_arg_t arg, CUMO_GRID_CONSTANT cumo_na_iarray_t in2, CUMO_GRID_CONSTANT cumo_reduce_addr_t ad, CUMO_GRID_CONSTANT cumo_reduce_addr_t ad2, TypeReduce* partial, int64_t n_split, int64_t chunk, int out_block_size, int reduce_block_size, ReductionImpl impl) {
462
545
  extern __shared__ __align__(8) char sdata_raw[];
463
546
  TypeReduce* sdata = reinterpret_cast<TypeReduce*>(sdata_raw);
464
547
  unsigned int tid = threadIdx.x;
@@ -467,8 +550,8 @@ __global__ static void reduction_zip_partial_kernel(cumo_na_reduction_arg_t arg,
467
550
  int64_t reduce_total_size = arg.in_indexer.total_size / out_total_size;
468
551
  int64_t partial_total_size = out_total_size * n_split;
469
552
 
470
- int64_t reduce_offset = tid / out_block_size;
471
- int64_t out_offset = tid % out_block_size;
553
+ int64_t reduce_offset, out_offset;
554
+ reduce_thread_split(ad, tid, out_block_size, reduce_block_size, &reduce_offset, &out_offset);
472
555
  int64_t out_base = blockIdx.x * out_block_size;
473
556
  int64_t out_stride = gridDim.x * out_block_size;
474
557
 
@@ -478,13 +561,13 @@ __global__ static void reduction_zip_partial_kernel(cumo_na_reduction_arg_t arg,
478
561
  int64_t begin = i_split * chunk;
479
562
  int64_t end = begin + chunk;
480
563
  if (end > reduce_total_size) end = reduce_total_size;
481
- ssize_t in_out_off = reduce_in_out_offset<FLAT>(arg.in, arg.in_indexer, ad, i_out);
482
- ssize_t in_out_off2 = reduce_in_out_offset<FLAT>(in2, arg.in_indexer, ad2, i_out);
564
+ ssize_t in_out_off, in_out_off2;
565
+ reduce_in_out_offset_pair<FLAT>(arg.in, in2, arg.in_indexer, ad, ad2, i_out, &in_out_off, &in_out_off2);
483
566
  int64_t i_in = i_out * reduce_total_size + begin + reduce_offset;
484
567
 
485
568
  TypeReduce accum = reduce_axis_zip<FLAT,TypeIn>(arg, in2, ad, ad2, impl, in_out_off, in_out_off2, i_in, begin, end, reduce_offset, reduce_block_size);
486
569
 
487
- accum = reduce_in_block(accum, sdata, tid, out_block_size, impl);
570
+ accum = reduce_in_block(accum, sdata, tid, out_block_size, reduce_block_size, !ad.out_inner, impl);
488
571
  if (reduce_offset == 0) {
489
572
  partial[i_out * n_split + i_split] = accum;
490
573
  }
@@ -36,17 +36,24 @@
36
36
 
37
37
  // pos is the first position in iteration order, so a step below zero walks down
38
38
  // from there and rebasing the pointer onto pos would underflow the position of
39
- // every element past the first word. A step of zero comes with an index, whose
40
- // entries only ever move forward.
39
+ // every element past the first word. An index holds positions relative to pos
40
+ // and one of them is below zero as soon as the view it came from walks an axis
41
+ // backwards, so pos + idx is right in unsigned arithmetic only while pos is
42
+ // whole. The test reads the iterator rather than the caller's copy of step,
43
+ // which is how a template declaring it as size_t used to lose the sign.
44
+ #define CUMO_REBASE_BIT_PTR( lp, i, ad, ps ) \
45
+ if (((lp)->args[i].iter[0]).step >= 0 && \
46
+ ((lp)->args[i].iter[0]).idx == NULL) { \
47
+ ad += ps/CUMO_NB; \
48
+ ps %= CUMO_NB; \
49
+ }
50
+
41
51
  #define CUMO_INIT_PTR_BIT( lp, i, ad, ps, st ) \
42
52
  { \
43
53
  ps = ((lp)->args[i].iter[0]).pos; \
44
54
  st = ((lp)->args[i].iter[0]).step; \
45
55
  ad = (CUMO_BIT_DIGIT*)(((lp)->args[i]).ptr); \
46
- if (st >= 0) { \
47
- ad += ps/CUMO_NB; \
48
- ps %= CUMO_NB; \
49
- } \
56
+ CUMO_REBASE_BIT_PTR(lp, i, ad, ps); \
50
57
  }
51
58
 
52
59
  #define CUMO_INIT_PTR_BIT_IDX( lp, i, ad, ps, st, id ) \
@@ -55,10 +62,7 @@
55
62
  st = ((lp)->args[i].iter[0]).step; \
56
63
  id = ((lp)->args[i].iter[0]).idx; \
57
64
  ad = (CUMO_BIT_DIGIT*)(((lp)->args[i]).ptr); \
58
- if (st >= 0) { \
59
- ad += ps/CUMO_NB; \
60
- ps %= CUMO_NB; \
61
- } \
65
+ CUMO_REBASE_BIT_PTR(lp, i, ad, ps); \
62
66
  }
63
67
 
64
68
  // A where compaction takes three launches, so below this many elements walking
@@ -179,4 +183,25 @@ cumo_na_store_rary_fetch(VALUE ary, size_t i, VALUE *x)
179
183
  return true;
180
184
  }
181
185
 
186
+ // A 0-dimensional walk still yields one index, so the depth floors at 0 rather
187
+ // than going negative and writing c[-1].
188
+ static inline void
189
+ cumo_na_with_index_dims(int ndim, int *nd, int *md)
190
+ {
191
+ *nd = (ndim > 0) ? ndim - 1 : ndim;
192
+ *md = *nd + 2;
193
+ }
194
+
195
+ static inline VALUE
196
+ cumo_na_yield_with_index(VALUE x, size_t *c, VALUE *a, int nd, int md)
197
+ {
198
+ int j;
199
+
200
+ a[0] = x;
201
+ for (j=0; j<=nd; j++) {
202
+ a[j+1] = SIZET2NUM(c[j]);
203
+ }
204
+ return rb_yield(rb_ary_new4(md,a));
205
+ }
206
+
182
207
  #endif /* ifndef CUMO_TEMPLATE_H */
@@ -62,6 +62,102 @@
62
62
  // limit of 2^31-1 is far above this.
63
63
  #define CUMO_MAX_GRID_DIM (4294967295ul / CUMO_MAX_BLOCK_DIM)
64
64
 
65
+ // A transpose has one side strided whichever way it is walked, so it stages a
66
+ // square tile in shared memory and reads and writes both sides in rows. The
67
+ // extra column spreads a tile's rows over the banks: for a 4-byte element that
68
+ // leaves no conflict at all, and it drops an 8-byte one to 2-way and a 16-byte
69
+ // one to 4-way. TILE must stay a multiple of ROWS.
70
+ #define CUMO_TRANSPOSE_TILE 32
71
+ #define CUMO_TRANSPOSE_ROWS 8
72
+
73
+ // gridDim.y and gridDim.z stop at 65535, unlike gridDim.x.
74
+ #define CUMO_MAX_GRID_DIM_Y 65535
75
+
76
+ // The tile a CUMO_TRANSPOSE_TILE_LOOP stages through. The extra column is what
77
+ // spreads its rows over the banks, so the loop reads it back along either axis
78
+ // without conflicting with itself.
79
+ #define CUMO_TRANSPOSE_TILE_DECL(type, name) \
80
+ __shared__ type name[CUMO_TRANSPOSE_TILE][CUMO_TRANSPOSE_TILE + 1]
81
+
82
+ // Tiles only pay off once both sides reach one. A shorter side leaves most of
83
+ // each warp idle, while the plain loop already reads or writes it in one go.
84
+ #define CUMO_TRANSPOSE_TILE_FITS(indexer) \
85
+ ((indexer)->ndim == 2 && \
86
+ (indexer)->shape[0] >= CUMO_TRANSPOSE_TILE && \
87
+ (indexer)->shape[1] >= CUMO_TRANSPOSE_TILE)
88
+
89
+ // Walks a rows-by-cols transpose one tile at a time.
90
+ //
91
+ // STAGE reads one element of the transposed side, which sits at cumo_tile_src
92
+ // of that side's own layout, and lands in cumo_tile_val. The body then runs
93
+ // once per output element, at cumo_tile_dst of the row-major layout the output
94
+ // and any other operand share.
95
+ //
96
+ // The body runs inside two nested loops, after a __syncthreads(). A continue
97
+ // there skips one output element, a break leaves the current tile but not the
98
+ // walk, and a return deadlocks the block on the next __syncthreads().
99
+ #define CUMO_TRANSPOSE_TILE_LOOP(tile, rows, cols, STAGE, ...) \
100
+ do { \
101
+ uint64_t cumo_tile_rows = (rows); \
102
+ uint64_t cumo_tile_cols = (cols); \
103
+ uint64_t cumo_tile_base; \
104
+ \
105
+ for (cumo_tile_base = (uint64_t)blockIdx.y * CUMO_TRANSPOSE_TILE; \
106
+ cumo_tile_base < cumo_tile_rows; \
107
+ cumo_tile_base += (uint64_t)gridDim.y * CUMO_TRANSPOSE_TILE) { \
108
+ uint64_t cumo_tile_x = cumo_tile_base + threadIdx.x; \
109
+ uint64_t cumo_tile_y = \
110
+ (uint64_t)blockIdx.x * CUMO_TRANSPOSE_TILE + threadIdx.y; \
111
+ int cumo_tile_j; \
112
+ \
113
+ __syncthreads(); \
114
+ for (cumo_tile_j = 0; \
115
+ threadIdx.y + cumo_tile_j < CUMO_TRANSPOSE_TILE; \
116
+ cumo_tile_j += CUMO_TRANSPOSE_ROWS) { \
117
+ if (cumo_tile_x < cumo_tile_rows && \
118
+ cumo_tile_y + cumo_tile_j < cumo_tile_cols) { \
119
+ uint64_t cumo_tile_src = \
120
+ (cumo_tile_y + cumo_tile_j) * cumo_tile_rows + cumo_tile_x; \
121
+ (tile)[threadIdx.y + cumo_tile_j][threadIdx.x] = STAGE; \
122
+ } \
123
+ } \
124
+ __syncthreads(); \
125
+ cumo_tile_x = (uint64_t)blockIdx.x * CUMO_TRANSPOSE_TILE + threadIdx.x; \
126
+ cumo_tile_y = cumo_tile_base + threadIdx.y; \
127
+ for (cumo_tile_j = 0; \
128
+ threadIdx.y + cumo_tile_j < CUMO_TRANSPOSE_TILE; \
129
+ cumo_tile_j += CUMO_TRANSPOSE_ROWS) { \
130
+ if (cumo_tile_x < cumo_tile_cols && \
131
+ cumo_tile_y + cumo_tile_j < cumo_tile_rows) { \
132
+ uint64_t cumo_tile_dst = \
133
+ (cumo_tile_y + cumo_tile_j) * cumo_tile_cols + cumo_tile_x; \
134
+ auto cumo_tile_val = \
135
+ (tile)[threadIdx.x][threadIdx.y + cumo_tile_j]; \
136
+ __VA_ARGS__ \
137
+ } \
138
+ } \
139
+ } \
140
+ } while (0)
141
+
142
+ // Launches such a kernel, which takes its own arguments and then rows and cols.
143
+ // gridDim.y walks tile rows on its own, but gridDim.x has to cover every tile
144
+ // column: a short grid there drops the columns past it without an error.
145
+ #define CUMO_TRANSPOSE_LAUNCH(kernel, rows, cols, ...) \
146
+ do { \
147
+ uint64_t cumo_launch_rows = (rows); \
148
+ uint64_t cumo_launch_cols = (cols); \
149
+ uint64_t cumo_launch_tiles_y = \
150
+ (cumo_launch_rows + CUMO_TRANSPOSE_TILE - 1) / CUMO_TRANSPOSE_TILE; \
151
+ dim3 cumo_launch_grid( \
152
+ (unsigned int)((cumo_launch_cols + CUMO_TRANSPOSE_TILE - 1) / CUMO_TRANSPOSE_TILE), \
153
+ (unsigned int)(cumo_launch_tiles_y > CUMO_MAX_GRID_DIM_Y \
154
+ ? CUMO_MAX_GRID_DIM_Y : cumo_launch_tiles_y)); \
155
+ dim3 cumo_launch_block(CUMO_TRANSPOSE_TILE, CUMO_TRANSPOSE_ROWS); \
156
+ \
157
+ kernel<<<cumo_launch_grid, cumo_launch_block>>>( \
158
+ __VA_ARGS__, cumo_launch_rows, cumo_launch_cols); \
159
+ } while (0)
160
+
65
161
  static inline size_t
66
162
  cumo_get_grid_dim(size_t n)
67
163
  {
@@ -1,3 +1,5 @@
1
+ #include "half_def.h"
2
+
1
3
  typedef CUMO_BIT_DIGIT dtype;
2
4
  typedef CUMO_BIT_DIGIT rtype;
3
5
  #define cT cumo_cBit
@@ -11,6 +13,7 @@ typedef CUMO_BIT_DIGIT rtype;
11
13
 
12
14
  #define m_from_double(x) (((x)==0) ? 0 : 1)
13
15
  #define m_from_real(x) (((x)==0) ? 0 : 1)
16
+ #define m_from_half(x) m_from_real(cumo_half2float(x))
14
17
  #define m_from_sint(x) (((x)==0) ? 0 : 1)
15
18
  #define m_from_int32(x) (((x)==0) ? 0 : 1)
16
19
  #define m_from_int64(x) (((x)==0) ? 0 : 1)
@@ -1,6 +1,8 @@
1
1
  #ifndef CUMO_BIT_KERNEL_H
2
2
  #define CUMO_BIT_KERNEL_H
3
3
 
4
+ #include "half_def_kernel.h"
5
+
4
6
  typedef CUMO_BIT_DIGIT dtype;
5
7
  typedef CUMO_BIT_DIGIT rtype;
6
8
 
@@ -12,6 +14,7 @@ typedef CUMO_BIT_DIGIT rtype;
12
14
 
13
15
  #define m_from_double(x) (((x)==0) ? 0 : 1)
14
16
  #define m_from_real(x) (((x)==0) ? 0 : 1)
17
+ #define m_from_half(x) m_from_real(cumo_half2float(x))
15
18
  #define m_from_sint(x) (((x)==0) ? 0 : 1)
16
19
  #define m_from_int32(x) (((x)==0) ? 0 : 1)
17
20
  #define m_from_int64(x) (((x)==0) ? 0 : 1)
@@ -1,4 +1,5 @@
1
1
  #include "float_def.h"
2
+ #include "half_def.h"
2
3
 
3
4
  extern double round(double);
4
5
  extern double log2(double);
@@ -43,6 +44,7 @@ static inline dtype c_from_dcomplex(cumo_dcomplex x) {
43
44
 
44
45
  #define m_from_double(x) c_new(x,0)
45
46
  #define m_from_real(x) c_new(x,0)
47
+ #define m_from_half(x) m_from_real(cumo_half2float(x))
46
48
  #define m_from_sint(x) c_new(x,0)
47
49
  #define m_from_int32(x) c_new(x,0)
48
50
  #define m_from_int64(x) c_new(x,0)
@@ -2,6 +2,7 @@
2
2
  #define CUMO_COMPLEX_MACRO_KERNEL_H
3
3
 
4
4
  #include "float_def_kernel.h"
5
+ #include "half_def_kernel.h"
5
6
 
6
7
  extern double round(double);
7
8
  extern double log2(double);
@@ -46,6 +47,7 @@ __host__ __device__ static inline dtype c_from_dcomplex(cumo_dcomplex x) {
46
47
 
47
48
  #define m_from_double(x) c_new(x,0)
48
49
  #define m_from_real(x) c_new(x,0)
50
+ #define m_from_half(x) m_from_real(cumo_half2float(x))
49
51
  #define m_from_sint(x) c_new(x,0)
50
52
  #define m_from_int32(x) c_new(x,0)
51
53
  #define m_from_int64(x) c_new(x,0)