carray 3.0.7 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (275) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +1 -1
  3. data/CHANGELOG.md +7 -1940
  4. data/README.md +38 -32
  5. data/carray.gemspec +16 -4
  6. data/docs/changelogs/CHANGELOG-3.0.md +2464 -0
  7. data/ext/ca_array_pool.c +4 -5
  8. data/ext/ca_axis_descriptor.h +45 -30
  9. data/ext/ca_axis_dispatch.c +7 -4
  10. data/ext/ca_axis_group.c +6 -9
  11. data/ext/ca_bincmp_dispatch.h +16 -38
  12. data/ext/ca_binop_dispatch.c +27 -0
  13. data/ext/ca_binop_dispatch.h +22 -47
  14. data/ext/ca_categorical_iterator.c +36 -34
  15. data/ext/ca_compare.c +0 -1
  16. data/ext/ca_compare.h +8 -1
  17. data/ext/ca_composite_dispatch.h +6 -0
  18. data/ext/ca_for_buffer.h +6 -1
  19. data/ext/ca_group_iter.c +17 -18
  20. data/ext/ca_iter_state_fields.h +196 -0
  21. data/ext/ca_iter_substrate.h +84 -103
  22. data/ext/ca_kernel_iterator.c +125 -89
  23. data/ext/ca_kernel_iterator.h +229 -1506
  24. data/ext/ca_kernel_iterator_internal.h +1167 -0
  25. data/ext/ca_moncmp_dispatch.h +17 -24
  26. data/ext/ca_monop_dispatch.h +25 -74
  27. data/ext/ca_obj_array.c +90 -163
  28. data/ext/ca_obj_bincmp.c +47 -46
  29. data/ext/ca_obj_binop.c +38 -38
  30. data/ext/ca_obj_bitarray.c +5 -17
  31. data/ext/ca_obj_bitfield.c +15 -30
  32. data/ext/ca_obj_block.c +11 -12
  33. data/ext/ca_obj_byte_swap.c +32 -30
  34. data/ext/ca_obj_const_string.c +57 -62
  35. data/ext/ca_obj_face.c +99 -91
  36. data/ext/ca_obj_face.h +32 -170
  37. data/ext/ca_obj_fake.c +15 -21
  38. data/ext/ca_obj_farray.c +7 -9
  39. data/ext/ca_obj_field.c +17 -13
  40. data/ext/ca_obj_fixlen_string.c +8 -19
  41. data/ext/ca_obj_grid.c +137 -106
  42. data/ext/ca_obj_meld.c +23 -21
  43. data/ext/ca_obj_moncmp.c +10 -21
  44. data/ext/ca_obj_monop.c +24 -35
  45. data/ext/ca_obj_object.c +51 -37
  46. data/ext/ca_obj_record.c +74 -22
  47. data/ext/ca_obj_reduce.c +8 -10
  48. data/ext/ca_obj_refer.c +42 -29
  49. data/ext/ca_obj_remap.c +11 -29
  50. data/ext/ca_obj_repeat.c +41 -23
  51. data/ext/ca_obj_roll.c +18 -24
  52. data/ext/ca_obj_select.c +10 -22
  53. data/ext/ca_obj_select_axis.c +27 -36
  54. data/ext/ca_obj_shift.c +22 -42
  55. data/ext/ca_obj_source.c +1 -5
  56. data/ext/ca_obj_stack.c +28 -24
  57. data/ext/ca_obj_stride.c +53 -54
  58. data/ext/ca_obj_string.c +6 -18
  59. data/ext/ca_obj_tile.c +12 -12
  60. data/ext/ca_obj_time.c +12 -23
  61. data/ext/ca_obj_timedelta.c +12 -23
  62. data/ext/ca_obj_transpose.c +8 -10
  63. data/ext/ca_obj_triop.c +35 -39
  64. data/ext/ca_obj_window.c +67 -86
  65. data/ext/ca_op_cmplx64.h +4 -0
  66. data/ext/ca_op_ipower.c +34 -12
  67. data/ext/ca_op_powi.h +4 -0
  68. data/ext/ca_pow5_table.h +2 -1
  69. data/ext/ca_rng_normal.h +4 -0
  70. data/ext/ca_rng_xoshiro256pp.h +3 -0
  71. data/ext/ca_sort_kernels.h +20 -26
  72. data/ext/ca_sweep_engine.c +2 -3
  73. data/ext/ca_sweep_engine.h +17 -9
  74. data/ext/ca_transform_common.c +3 -8
  75. data/ext/ca_triop_dispatch.h +20 -32
  76. data/ext/caframe_csv_reader.c +11 -11
  77. data/ext/carray.h +300 -1011
  78. data/ext/carray_access.c +234 -180
  79. data/ext/carray_address_basis.c +33 -38
  80. data/ext/carray_attribute.c +81 -104
  81. data/ext/carray_bincount.c +12 -14
  82. data/ext/carray_broadcast.c +30 -26
  83. data/ext/carray_call_cfunc.c +169 -277
  84. data/ext/carray_call_cfunc.h +36 -28
  85. data/ext/carray_cast.c +912 -346
  86. data/ext/carray_cast_func.rb +31 -3
  87. data/ext/carray_class.c +6 -11
  88. data/ext/carray_conversion.c +43 -38
  89. data/ext/carray_copy.c +28 -26
  90. data/ext/carray_core.c +217 -126
  91. data/ext/carray_count.c +27 -15
  92. data/ext/carray_element.c +40 -25
  93. data/ext/carray_factorize.c +53 -52
  94. data/ext/carray_generate.c +26 -19
  95. data/ext/carray_histogram.c +8 -10
  96. data/ext/carray_hold.c +7 -9
  97. data/ext/carray_index_classifier.c +123 -228
  98. data/ext/carray_index_classifier.h +11 -10
  99. data/ext/carray_integer_total.c +523 -0
  100. data/ext/carray_internal.h +1274 -160
  101. data/ext/carray_key_windows.c +3 -3
  102. data/ext/carray_lazy.c +21 -36
  103. data/ext/carray_loop.c +19 -22
  104. data/ext/carray_mask.c +123 -145
  105. data/ext/carray_math_kernel.h +23 -47
  106. data/ext/carray_mathfunc.c +10 -3
  107. data/ext/carray_median_percentile.c +95 -94
  108. data/ext/carray_memory_view.c +42 -47
  109. data/ext/carray_operator.c +423 -122
  110. data/ext/carray_order.c +121 -85
  111. data/ext/carray_partition.c +13 -15
  112. data/ext/carray_random.c +47 -41
  113. data/ext/carray_round.c +9 -4
  114. data/ext/carray_scatter.c +54 -40
  115. data/ext/carray_segment.c +41 -45
  116. data/ext/carray_slab.c +64 -47
  117. data/ext/carray_slab.h +44 -33
  118. data/ext/carray_sort.c +32 -29
  119. data/ext/carray_sort_kernel.c +12 -10
  120. data/ext/carray_struct.c +38 -43
  121. data/ext/carray_test.c +47 -43
  122. data/ext/carray_time_text.c +24 -31
  123. data/ext/carray_undef.c +50 -7
  124. data/ext/carray_utils.c +258 -94
  125. data/ext/extconf.rb +24 -17
  126. data/ext/mk_call_cfunc.rb +19 -20
  127. data/ext/mk_pow5_table.rb +2 -1
  128. data/ext/mkkernel.rb +782 -461
  129. data/ext/ruby_carray.c +47 -42
  130. data/ext/version.h +7 -5
  131. data/lib/carray/attribute.rb +19 -18
  132. data/lib/carray/autoload_carray.rb +20 -15
  133. data/lib/carray/autoload_method_extension.rb +64 -57
  134. data/lib/carray/axis_group.rb +264 -180
  135. data/lib/carray/basics.rb +272 -48
  136. data/lib/carray/bincount_nd.rb +33 -16
  137. data/lib/carray/block_iterator.rb +123 -102
  138. data/lib/carray/boolean_reduce.rb +7 -4
  139. data/lib/carray/categorical.rb +63 -51
  140. data/lib/carray/categorical_iterator.rb +89 -67
  141. data/lib/carray/complex.rb +61 -41
  142. data/lib/carray/conditional.rb +49 -22
  143. data/lib/carray/const_string.rb +54 -62
  144. data/lib/carray/core_extensions.rb +1 -1
  145. data/lib/carray/distance_limit.rb +7 -5
  146. data/lib/carray/factories.rb +269 -0
  147. data/lib/carray/fixlen_string.rb +25 -24
  148. data/lib/carray/frame/column_verbs.rb +20 -8
  149. data/lib/carray/frame/concat.rb +3 -3
  150. data/lib/carray/frame/convert.rb +2 -1
  151. data/lib/carray/frame/csv_parser.rb +7 -1
  152. data/lib/carray/frame/frame.rb +35 -25
  153. data/lib/carray/frame/group.rb +183 -150
  154. data/lib/carray/frame/io.rb +26 -7
  155. data/lib/carray/frame/join.rb +37 -12
  156. data/lib/carray/frame/records.rb +1 -1
  157. data/lib/carray/frame/reshape.rb +3 -3
  158. data/lib/carray/frame/sort.rb +10 -8
  159. data/lib/carray/frame/verbs.rb +148 -118
  160. data/lib/carray/frame/windows.rb +232 -21
  161. data/lib/carray/frame.rb +1 -2
  162. data/lib/carray/fuse_source.rb +24 -21
  163. data/lib/carray/fusion.rb +30 -30
  164. data/lib/carray/histogram.rb +35 -16
  165. data/lib/carray/inspect.rb +7 -7
  166. data/lib/carray/iterator.rb +97 -24
  167. data/lib/carray/key_windows.rb +83 -57
  168. data/lib/carray/lazy.rb +196 -94
  169. data/lib/carray/limits.rb +80 -0
  170. data/lib/carray/mask_gap_fill.rb +21 -16
  171. data/lib/carray/math.rb +87 -50
  172. data/lib/carray/meld_reduce.rb +30 -8
  173. data/lib/carray/methods/align_addr.rb +29 -17
  174. data/lib/carray/methods/bin.rb +5 -1
  175. data/lib/carray/methods/bincount.rb +4 -2
  176. data/lib/carray/methods/bit_string.rb +19 -31
  177. data/lib/carray/methods/broadcast.rb +8 -3
  178. data/lib/carray/methods/choose.rb +5 -5
  179. data/lib/carray/methods/composition.rb +72 -71
  180. data/lib/carray/methods/{index.rb → coordinate.rb} +13 -13
  181. data/lib/carray/methods/discovery_along.rb +3 -3
  182. data/lib/carray/methods/gather_nd.rb +106 -106
  183. data/lib/carray/methods/insert_block.rb +2 -2
  184. data/lib/carray/methods/is_between.rb +6 -4
  185. data/lib/carray/methods/is_in.rb +165 -145
  186. data/lib/carray/methods/join.rb +2 -1
  187. data/lib/carray/methods/lag.rb +6 -2
  188. data/lib/carray/methods/locate_addr.rb +150 -138
  189. data/lib/carray/methods/mask_duplicates.rb +1 -1
  190. data/lib/carray/methods/meshgrid.rb +28 -16
  191. data/lib/carray/methods/mode.rb +67 -67
  192. data/lib/carray/methods/nunique.rb +9 -7
  193. data/lib/carray/methods/repeat.rb +12 -12
  194. data/lib/carray/methods/resize.rb +1 -1
  195. data/lib/carray/methods/snap.rb +24 -20
  196. data/lib/carray/methods/string_format.rb +2 -2
  197. data/lib/carray/methods/unique.rb +4 -2
  198. data/lib/carray/methods/value_counts.rb +1 -1
  199. data/lib/carray/mkmf.rb +25 -28
  200. data/lib/carray/percentile_arguments.rb +82 -0
  201. data/lib/carray/record_order.rb +18 -17
  202. data/lib/carray/rng.rb +1 -0
  203. data/lib/carray/runtime.rb +190 -22
  204. data/lib/carray/segment_iterator.rb +251 -129
  205. data/lib/carray/serialize.rb +618 -571
  206. data/lib/carray/slab_iterator.rb +114 -114
  207. data/lib/carray/stack.rb +11 -12
  208. data/lib/carray/string.rb +16 -22
  209. data/lib/carray/string_operation_extension.rb +76 -52
  210. data/lib/carray/struct.rb +122 -164
  211. data/lib/carray/struct_builder.rb +243 -72
  212. data/lib/carray/time.rb +496 -342
  213. data/lib/carray/window_iterator.rb +191 -150
  214. data/lib/carray.rb +6 -11
  215. data/yard-stubs/ca_obj_array.rb +20 -135
  216. data/yard-stubs/ca_obj_bitarray.rb +1 -8
  217. data/yard-stubs/ca_obj_bitfield.rb +4 -3
  218. data/yard-stubs/ca_obj_byte_swap.rb +21 -7
  219. data/yard-stubs/ca_obj_fake.rb +3 -1
  220. data/yard-stubs/ca_obj_farray.rb +1 -1
  221. data/yard-stubs/ca_obj_field.rb +8 -6
  222. data/yard-stubs/ca_obj_refer.rb +7 -5
  223. data/yard-stubs/ca_obj_roll.rb +7 -6
  224. data/yard-stubs/ca_obj_shift.rb +2 -3
  225. data/yard-stubs/ca_obj_stride.rb +5 -7
  226. data/yard-stubs/ca_obj_tile.rb +1 -1
  227. data/yard-stubs/ca_obj_time.rb +43 -0
  228. data/yard-stubs/ca_obj_transpose.rb +1 -1
  229. data/yard-stubs/ca_obj_window.rb +26 -15
  230. data/yard-stubs/carray_access.rb +51 -4
  231. data/yard-stubs/carray_attribute.rb +26 -38
  232. data/yard-stubs/carray_cast.rb +118 -165
  233. data/yard-stubs/carray_class.rb +3 -4
  234. data/yard-stubs/carray_conversion.rb +6 -7
  235. data/yard-stubs/carray_copy.rb +2 -1
  236. data/yard-stubs/carray_count.rb +5 -3
  237. data/yard-stubs/carray_element.rb +4 -3
  238. data/yard-stubs/carray_limits.rb +100 -0
  239. data/yard-stubs/carray_loop.rb +0 -20
  240. data/yard-stubs/carray_mask.rb +59 -41
  241. data/yard-stubs/carray_math.rb +157 -100
  242. data/yard-stubs/carray_mathfunc.rb +415 -3
  243. data/yard-stubs/carray_median_percentile.rb +29 -11
  244. data/yard-stubs/carray_memory_view.rb +2 -2
  245. data/yard-stubs/carray_order.rb +67 -34
  246. data/yard-stubs/carray_random.rb +6 -6
  247. data/yard-stubs/carray_reduce.rb +353 -0
  248. data/yard-stubs/carray_rng.rb +92 -0
  249. data/yard-stubs/carray_scatter.rb +10 -2
  250. data/yard-stubs/carray_slab.rb +7 -4
  251. data/yard-stubs/carray_sort.rb +7 -6
  252. data/yard-stubs/carray_undef.rb +44 -5
  253. data/yard-stubs/ruby_carray.rb +75 -40
  254. metadata +62 -24
  255. data/CHANGELOG.v1.md +0 -126
  256. data/ext/carray_build_flags.h +0 -3
  257. data/ext/carray_config.h +0 -28
  258. data/ext/carray_data_type.c +0 -90
  259. data/ext/carray_kernels_bincmp.c +0 -6457
  260. data/ext/carray_kernels_binop.c +0 -31534
  261. data/ext/carray_kernels_init.c +0 -1734
  262. data/ext/carray_kernels_moncmp.c +0 -2104
  263. data/ext/carray_kernels_monop.c +0 -25337
  264. data/ext/carray_kernels_reduce_aggregate.c +0 -27545
  265. data/ext/carray_kernels_reduce_boolean.c +0 -1924
  266. data/ext/carray_kernels_reduce_cumulative.c +0 -11646
  267. data/ext/carray_kernels_reduce_extreme.c +0 -21631
  268. data/ext/carray_kernels_reduce_variance.c +0 -5981
  269. data/ext/carray_kernels_scan.c +0 -4302
  270. data/ext/carray_kernels_search.c +0 -37776
  271. data/ext/carray_kernels_sort.c +0 -10814
  272. data/ext/carray_kernels_triop.c +0 -1864
  273. data/lib/carray/construct.rb +0 -360
  274. data/lib/carray/data_type_extension.rb +0 -262
  275. data/lib/carray/data_type_limits.rb +0 -91
@@ -25,7 +25,6 @@
25
25
  --------------------------------------------------------------------------- */
26
26
 
27
27
  #include "carray.h"
28
- #include "carray_internal.h" /* CA_EXTREMUM_REPLACES */
29
28
  #include "ca_sort_kernels.h" /* ca_partition_nan_f64 */
30
29
  #include <stdlib.h> /* qsort */
31
30
  #include <math.h> /* floor */
@@ -46,10 +45,10 @@
46
45
  } while (0)
47
46
 
48
47
  static VALUE
49
- rb_ca_addresses_by_category (VALUE self, volatile VALUE rcursor, VALUE rk, VALUE rnvalid)
48
+ rb_ca_addresses_by_category (volatile VALUE self, volatile VALUE rcursor, VALUE rk, VALUE rnvalid)
50
49
  {
51
50
  CArray *codes, *cursor, *result;
52
- VALUE rresult;
51
+ volatile VALUE rresult;
53
52
  int64_t k, c, *cur, *out, tail, nvalid;
54
53
  ca_size_t n, j, dim;
55
54
  int overrun = 0;
@@ -86,7 +85,7 @@ rb_ca_addresses_by_category (VALUE self, volatile VALUE rcursor, VALUE rk, VALUE
86
85
  cur = (int64_t *) cursor->ptr;
87
86
  tail = nvalid;
88
87
 
89
- ca_attach(codes); /* window: nothing below raises */
88
+ ca_attach(codes); /* attach: nothing below raises */
90
89
  cmask = ca_mask_ptr(codes);
91
90
  switch ( codes->data_type ) {
92
91
  case CA_INT8: ADDRESSES_BY_CATEGORY_BODY(int8_t); break;
@@ -263,8 +262,8 @@ rb_ca_addresses_by_category (VALUE self, volatile VALUE rcursor, VALUE rk, VALUE
263
262
  } while (0)
264
263
 
265
264
  static VALUE
266
- rb_ca_reduceat_moments (VALUE self, VALUE roffsets, VALUE rcounts,
267
- VALUE rsums, volatile VALUE rmins, volatile VALUE rmaxs)
265
+ rb_ca_reduceat_moments (volatile VALUE self, volatile VALUE roffsets, volatile VALUE rcounts,
266
+ volatile VALUE rsums, volatile VALUE rmins, volatile VALUE rmaxs)
268
267
  {
269
268
  CArray *grouped, *offsets, *counts, *sums, *mins, *maxs;
270
269
  int64_t k, *offs, *countp;
@@ -397,12 +396,13 @@ ca_nth_element_double (double *a, ca_size_t n, ca_size_t kth)
397
396
  } while (0)
398
397
 
399
398
  static VALUE
400
- rb_ca_reduceat_percentile (VALUE self, volatile VALUE roffsets, VALUE rp, volatile VALUE rout)
399
+ rb_ca_reduceat_percentile (volatile VALUE self, volatile VALUE roffsets, VALUE rp, volatile VALUE rout)
401
400
  {
402
401
  CArray *grouped, *offsets, *out;
403
402
  int64_t k, *offs;
404
403
  ca_size_t n, c, maxseg = 0;
405
404
  double p, *outp, *scratch = NULL;
405
+ volatile VALUE hscratch = 0;
406
406
  boolean8_t *gm, *outm;
407
407
 
408
408
  GetCArray(self, grouped);
@@ -432,7 +432,7 @@ rb_ca_reduceat_percentile (VALUE self, volatile VALUE roffsets, VALUE rp, volati
432
432
  ca_size_t hi = (c + 1 < (ca_size_t) k) ? (ca_size_t) offs[c+1] : n;
433
433
  if ( hi - lo > maxseg ) maxseg = hi - lo;
434
434
  }
435
- if ( maxseg > 0 ) scratch = (double *) xmalloc((size_t) maxseg * sizeof(double));
435
+ if ( maxseg > 0 ) scratch = ALLOCV_N(double, hscratch, maxseg);
436
436
 
437
437
  switch ( grouped->data_type ) {
438
438
  case CA_INT8: REDUCEAT_PCT_BODY(int8_t); break;
@@ -446,12 +446,12 @@ rb_ca_reduceat_percentile (VALUE self, volatile VALUE roffsets, VALUE rp, volati
446
446
  case CA_FLOAT32: REDUCEAT_PCT_BODY(float32_t); break;
447
447
  case CA_FLOAT64: REDUCEAT_PCT_BODY(float64_t); break;
448
448
  default:
449
- if ( scratch ) xfree(scratch);
449
+ ALLOCV_END(hscratch);
450
450
  rb_raise(rb_eCADataTypeError,
451
451
  "__reduceat_percentile__: numeric value required (got data_type %d)",
452
452
  grouped->data_type);
453
453
  }
454
- if ( scratch ) xfree(scratch);
454
+ ALLOCV_END(hscratch);
455
455
  return Qnil;
456
456
  }
457
457
 
@@ -502,8 +502,8 @@ rb_ca_reduceat_percentile (VALUE self, volatile VALUE roffsets, VALUE rp, volati
502
502
  } while (0)
503
503
 
504
504
  static VALUE
505
- rb_ca_reduceat_variance (VALUE self, VALUE roffsets, VALUE rmeans,
506
- VALUE rcounts, volatile VALUE rout, VALUE rddof)
505
+ rb_ca_reduceat_variance (volatile VALUE self, volatile VALUE roffsets, volatile VALUE rmeans,
506
+ volatile VALUE rcounts, volatile VALUE rout, VALUE rddof)
507
507
  {
508
508
  int64_t ddof = NUM2LL(rddof);
509
509
  CArray *grouped, *offsets, *means, *counts, *out;
@@ -583,7 +583,7 @@ rb_ca_reduceat_variance (VALUE self, VALUE roffsets, VALUE rmeans,
583
583
  } while (0)
584
584
 
585
585
  static VALUE
586
- rb_ca_reduceat_prod (VALUE self, VALUE roffsets, VALUE rout)
586
+ rb_ca_reduceat_prod (volatile VALUE self, volatile VALUE roffsets, volatile VALUE rout)
587
587
  {
588
588
  CArray *grouped, *offsets, *out;
589
589
  int64_t k, *offs;
@@ -649,7 +649,7 @@ rb_ca_reduceat_prod (VALUE self, VALUE roffsets, VALUE rout)
649
649
  } while (0)
650
650
 
651
651
  static VALUE
652
- rb_ca_reduceat_argminmax (VALUE self, VALUE roffsets, volatile VALUE rminidx, volatile VALUE rmaxidx)
652
+ rb_ca_reduceat_argminmax (volatile VALUE self, volatile VALUE roffsets, volatile VALUE rminidx, volatile VALUE rmaxidx)
653
653
  {
654
654
  CArray *grouped, *offsets, *minidx, *maxidx;
655
655
  int64_t k, *offs, *minp, *maxp;
@@ -699,7 +699,7 @@ rb_ca_reduceat_argminmax (VALUE self, VALUE roffsets, volatile VALUE rminidx, vo
699
699
  over present cells. Value data type must be boolean. Empty segment: all -> true,
700
700
  any -> false. */
701
701
  static VALUE
702
- rb_ca_reduceat_all_any (VALUE self, VALUE roffsets, VALUE rall, VALUE rany)
702
+ rb_ca_reduceat_all_any (volatile VALUE self, volatile VALUE roffsets, volatile VALUE rall, volatile VALUE rany)
703
703
  {
704
704
  CArray *grouped, *offsets, *all, *any;
705
705
  int64_t k, *offs;
@@ -786,14 +786,15 @@ cmp_double (const void *a, const void *b)
786
786
  } while (0)
787
787
 
788
788
  static VALUE
789
- rb_ca_reduceat_quantile (VALUE self, VALUE roffsets, VALUE rp0, VALUE rp25,
789
+ rb_ca_reduceat_quantile (volatile VALUE self, volatile VALUE roffsets, VALUE rp0, VALUE rp25,
790
790
  VALUE rp50, VALUE rp75, VALUE rp100)
791
791
  {
792
792
  CArray *grouped, *offsets, *outs[5];
793
- VALUE routs[5];
793
+ volatile VALUE routs[5];
794
794
  int64_t k, *offs;
795
795
  ca_size_t n, c, maxseg = 0;
796
796
  double *outp[5], *scratch = NULL;
797
+ volatile VALUE hscratch = 0;
797
798
  boolean8_t *gm, *outm[5];
798
799
  int t;
799
800
 
@@ -822,7 +823,7 @@ rb_ca_reduceat_quantile (VALUE self, VALUE roffsets, VALUE rp0, VALUE rp25,
822
823
  ca_size_t hi = (c + 1 < (ca_size_t) k) ? (ca_size_t) offs[c+1] : n;
823
824
  if ( hi - lo > maxseg ) maxseg = hi - lo;
824
825
  }
825
- if ( maxseg > 0 ) scratch = (double *) xmalloc((size_t) maxseg * sizeof(double));
826
+ if ( maxseg > 0 ) scratch = ALLOCV_N(double, hscratch, maxseg);
826
827
 
827
828
  switch ( grouped->data_type ) {
828
829
  case CA_INT8: REDUCEAT_QUANTILE_BODY(int8_t); break;
@@ -836,12 +837,12 @@ rb_ca_reduceat_quantile (VALUE self, VALUE roffsets, VALUE rp0, VALUE rp25,
836
837
  case CA_FLOAT32: REDUCEAT_QUANTILE_BODY(float32_t); break;
837
838
  case CA_FLOAT64: REDUCEAT_QUANTILE_BODY(float64_t); break;
838
839
  default:
839
- if ( scratch ) xfree(scratch);
840
+ ALLOCV_END(hscratch);
840
841
  rb_raise(rb_eCADataTypeError,
841
842
  "__reduceat_quantile__: numeric value required (got data_type %d)",
842
843
  grouped->data_type);
843
844
  }
844
- if ( scratch ) xfree(scratch);
845
+ ALLOCV_END(hscratch);
845
846
  return Qnil;
846
847
  }
847
848
 
@@ -871,8 +872,8 @@ rb_ca_reduceat_quantile (VALUE self, VALUE roffsets, VALUE rp0, VALUE rp25,
871
872
  } while (0)
872
873
 
873
874
  static VALUE
874
- rb_ca_reduceat_wsum_wmean (VALUE self, VALUE roffsets, volatile VALUE rwg,
875
- VALUE rwsum, volatile VALUE rwmean)
875
+ rb_ca_reduceat_wsum_wmean (volatile VALUE self, volatile VALUE roffsets, volatile VALUE rwg,
876
+ volatile VALUE rwsum, volatile VALUE rwmean)
876
877
  {
877
878
  CArray *grouped, *offsets, *wg, *wsum, *wmean;
878
879
  int64_t k, *offs;
@@ -927,8 +928,9 @@ rb_ca_reduceat_wsum_wmean (VALUE self, VALUE roffsets, volatile VALUE rwg,
927
928
 
928
929
  The band-preserving sibling of __reduceat_moments__: reduces `h` along `axis`
929
930
  per (band-coord) fiber, dispatched by `codes`. Ruby side broadcasts codes to
930
- h.shape before this call so all three shape cases (A / B / band-only,
931
- PROPOSAL_CATEGORICAL_REDUCE_AXIS §2.2) collapse to one kernel signature.
931
+ h.shape before this call, so all three shape cases (1-D codes along the
932
+ axis, codes of the source rank, or band-only codes one rank less) collapse
933
+ to one kernel signature.
932
934
 
933
935
  Not aligned kernel_iterator: output position depends on the codes value
934
936
  (data-dependent scatter), so ca_attach materialises the inputs into
@@ -1001,9 +1003,9 @@ rb_ca_reduceat_wsum_wmean (VALUE self, VALUE roffsets, volatile VALUE rwg,
1001
1003
  }
1002
1004
 
1003
1005
  static VALUE
1004
- rb_ca_fiber_scatter_moments (VALUE self, VALUE rcodes, VALUE raxis, VALUE rk,
1005
- VALUE rcounts, VALUE rsums,
1006
- VALUE rmins, VALUE rmaxs)
1006
+ rb_ca_fiber_scatter_moments (volatile VALUE self, volatile VALUE rcodes, VALUE raxis, VALUE rk,
1007
+ volatile VALUE rcounts, volatile VALUE rsums,
1008
+ volatile VALUE rmins, volatile VALUE rmaxs)
1007
1009
  {
1008
1010
  CArray *h, *codes, *counts, *sums, *mins, *maxs;
1009
1011
  int64_t K, *countp;
@@ -1077,7 +1079,7 @@ rb_ca_fiber_scatter_moments (VALUE self, VALUE rcodes, VALUE raxis, VALUE rk,
1077
1079
  band_size = outer_prod * inner_prod;
1078
1080
  total = (ca_size_t)(K * band_size);
1079
1081
 
1080
- ca_attach_n(2, h, codes); /* window: detached before each raise inside */
1082
+ ca_attach_n(2, h, codes); /* attach: detached before each raise */
1081
1083
  hm = ca_mask_ptr(h);
1082
1084
  cm = ca_mask_ptr(codes);
1083
1085
  countp = (int64_t *) counts->ptr;
@@ -1184,8 +1186,8 @@ rb_ca_fiber_scatter_moments (VALUE self, VALUE rcodes, VALUE raxis, VALUE rk,
1184
1186
  }
1185
1187
 
1186
1188
  static VALUE
1187
- rb_ca_fiber_scatter_prod (VALUE self, VALUE rcodes, VALUE raxis,
1188
- VALUE rk, VALUE rout)
1189
+ rb_ca_fiber_scatter_prod (volatile VALUE self, volatile VALUE rcodes, VALUE raxis,
1190
+ VALUE rk, volatile VALUE rout)
1189
1191
  {
1190
1192
  CArray *h, *codes, *out;
1191
1193
  int64_t K;
@@ -1245,7 +1247,7 @@ rb_ca_fiber_scatter_prod (VALUE self, VALUE rcodes, VALUE raxis,
1245
1247
  band_size = outer_prod * inner_prod;
1246
1248
  total = (ca_size_t)(K * band_size);
1247
1249
 
1248
- ca_attach_n(2, h, codes); /* window: detached before each raise inside */
1250
+ ca_attach_n(2, h, codes); /* attach: detached before each raise */
1249
1251
  hm = ca_mask_ptr(h);
1250
1252
  cm = ca_mask_ptr(codes);
1251
1253
  outp = (double *) out->ptr;
@@ -1343,9 +1345,9 @@ rb_ca_fiber_scatter_prod (VALUE self, VALUE rcodes, VALUE raxis,
1343
1345
  }
1344
1346
 
1345
1347
  static VALUE
1346
- rb_ca_fiber_scatter_wsum_wmean (VALUE self, VALUE rcodes, VALUE rweights,
1348
+ rb_ca_fiber_scatter_wsum_wmean (volatile VALUE self, volatile VALUE rcodes, volatile VALUE rweights,
1347
1349
  VALUE raxis, VALUE rk,
1348
- VALUE rwsum, VALUE rwmean)
1350
+ volatile VALUE rwsum, volatile VALUE rwmean)
1349
1351
  {
1350
1352
  CArray *h, *codes, *weights, *wsum, *wmean;
1351
1353
  int64_t K, *cntp;
@@ -1414,7 +1416,7 @@ rb_ca_fiber_scatter_wsum_wmean (VALUE self, VALUE rcodes, VALUE rweights,
1414
1416
  band_size = outer_prod * inner_prod;
1415
1417
  total = (ca_size_t)(K * band_size);
1416
1418
 
1417
- ca_attach_n(3, h, codes, weights); /* window: detached before each raise inside */
1419
+ ca_attach_n(3, h, codes, weights); /* attach: detached before each raise */
1418
1420
  hm = ca_mask_ptr(h);
1419
1421
  cm = ca_mask_ptr(codes);
1420
1422
  wm = ca_mask_ptr(weights);
data/ext/ca_compare.c CHANGED
@@ -14,7 +14,6 @@
14
14
  #include "ruby.h"
15
15
  #include "carray.h"
16
16
  #include "ca_compare.h"
17
- #include "carray_internal.h" /* CA_FLOAT_ORDER_CMP, ca_obj_order_cmp */
18
17
  #include <math.h>
19
18
 
20
19
  #define DEFINE_ELEM_CMP(type) \
data/ext/ca_compare.h CHANGED
@@ -1,5 +1,11 @@
1
1
  /* ---------------------------------------------------------------------------
2
2
 
3
+ ca_compare.h
4
+
5
+ This header is internal to carray and carries no contract. It is not
6
+ installed; it ships in the gem's ext/ directory only because carray is
7
+ built from it there.
8
+
3
9
  Element comparators by data_type: a 3-way compare (-1 / 0 / +1) of two
4
10
  same-typed cells, with the (const void *, const void *) prototype that
5
11
  libc qsort / bsearch expect. This is a shared *ordering primitive*, not
@@ -20,7 +26,8 @@
20
26
  typedef int (*ca_cmp_func)(const void *, const void *);
21
27
 
22
28
  /* Per-data_type element comparators, indexed by data_type (CA_NTYPE wide).
23
- FIXLEN / retired / unused slots hold an unsupported stub that raises. */
29
+ The CA_FIXLEN slot, the complex slots and the reserved CA_FLOAT128 /
30
+ CA_CMPLX256 slots hold an unsupported stub that raises. */
24
31
  extern ca_cmp_func ca_elem_cmp[CA_NTYPE];
25
32
 
26
33
  #endif /* CA_COMPARE_H */
@@ -1,5 +1,11 @@
1
1
  /* ---------------------------------------------------------------------------
2
2
 
3
+ ca_composite_dispatch.h
4
+
5
+ This header is internal to carray and carries no contract. It is not
6
+ installed; it ships in the gem's ext/ directory only because carray is
7
+ built from it there.
8
+
3
9
  Geometric primitives for region-mapped views (CAWindow / CATile /
4
10
  CARoll). These helpers are the "per-region routine" called by each
5
11
  region-mapped view's attach/sync path.
data/ext/ca_for_buffer.h CHANGED
@@ -3,6 +3,12 @@
3
3
  * ca_for_buffer.h -- whole contig buffer handed to author / third-party
4
4
  * library
5
5
  *
6
+ * Everything this header declares is for extensions, and stays as it is
7
+ * for the rest of 3.x: rb_ca_call_with_buffer and its body type
8
+ * ca_with_buffer_body_fn. An extension includes this header itself,
9
+ * after carray.h; carray.h does not include it. The engine that delivers
10
+ * the buffer is carray's own and is not declared to an extension.
11
+ *
6
12
  * C-side counterpart of Ruby's `ca.attach! { |a| ... }` block: the
7
13
  * attach / sync / detach lifecycle is scoped to a body function, which
8
14
  * receives a contig buffer ptr and element count and may do whatever
@@ -35,7 +41,6 @@
35
41
  #define CA_FOR_BUFFER_H
36
42
 
37
43
  #include "carray.h"
38
- #include "ca_sweep_engine.h"
39
44
 
40
45
  /* body_fn(user_data, ptr, n_elements) -> may raise
41
46
  *
data/ext/ca_group_iter.c CHANGED
@@ -3,12 +3,12 @@
3
3
  Axis-group reduction surface: the `[]` type gate and CAGroupIterator.
4
4
 
5
5
  The apply path is C-level: the `[]` type gate that routes a CACategorical /
6
- AxisGroup index to the group path (vs ordinary selection), the
6
+ CAAxisGroup index to the group path (vs ordinary selection), the
7
7
  CAGroupIterator construction and the :group reduce dispatch all live here.
8
8
  The classifier is CACategorical (`categorical` (lib/carray/categorical.rb)):
9
9
  a READONLY Face over narrow-uint codes; this surface consumes its codes / k
10
- / labels. The value-independent metadata (AxisGroup spec derivation,
11
- GroupLabels) is Ruby (`axis_group` (lib/carray/axis_group.rb)) — it is not on
10
+ / labels. The value-independent metadata (CAAxisGroup spec derivation,
11
+ CArray::GroupLabels) is Ruby (`axis_group` (lib/carray/axis_group.rb)) — it is not on
12
12
  the `[]` hot path and is plain O(ndim) metadata.
13
13
 
14
14
  Split summary:
@@ -21,7 +21,6 @@
21
21
  --------------------------------------------------------------------------- */
22
22
 
23
23
  #include "carray.h"
24
- #include "carray_internal.h" /* ca_check_rank_fits */
25
24
 
26
25
  /* lib/carray/axis_group.rb registers these classes here on first load (and
27
26
  loading is wired so that load happens as soon as any categorical exists —
@@ -33,9 +32,9 @@ static VALUE rb_cAxisGroup = Qnil;
33
32
  static VALUE rb_cCAGroupIterator = Qnil;
34
33
 
35
34
  static ID id_value, id_spec, id_axis_group, id_reduce_plan, id_parse_axis;
36
- static ID id_axis_group_reduce, id_reshape, id_transpose, id_labels;
35
+ static ID id_axis_group_reduce, id_transpose, id_labels;
37
36
 
38
- /* CArray.register_axis_group_classes(CACategorical, AxisGroup) — called
37
+ /* CArray.register_axis_group_classes(CACategorical, CAAxisGroup) — called
39
38
  from the top of lib/carray/axis_group.rb so the C type gate can recognise
40
39
  the classifier/spec types without triggering autoload on every [] call. */
41
40
  static VALUE
@@ -53,13 +52,13 @@ rb_ca_register_axis_group_classes (VALUE klass, VALUE cat, VALUE ag)
53
52
  }
54
53
 
55
54
  /* Cheap per-arg pre-filter + kind_of check. Returns 1 if any arg is a
56
- CACategorical (one-shot slot) or an AxisGroup (pre-built spec) — the group
55
+ CACategorical (one-shot slot) or an CAAxisGroup (pre-built spec) — the group
57
56
  apply path — and 0 otherwise.
58
57
 
59
58
  The two group-arg forms have different Ruby representations, so each gets a
60
59
  pre-filter that keeps ordinary indexing off the kind_of walk:
61
60
 
62
- - AxisGroup is a plain Ruby object (T_OBJECT). The scalar index kinds
61
+ - CAAxisGroup is a plain Ruby object (T_OBJECT). The scalar index kinds
63
62
  (Integer / nil / Symbol / Range) are not T_OBJECT, so they cost only the
64
63
  RB_TYPE_P bit test.
65
64
 
@@ -81,7 +80,7 @@ ca_argv_has_group (int argc, VALUE *argv)
81
80
  {
82
81
  int i;
83
82
  for ( i = 0; i < argc; i++ ) {
84
- VALUE a = argv[i];
83
+ volatile VALUE a = argv[i];
85
84
  if ( RB_TYPE_P(a, T_OBJECT) ) {
86
85
  if ( ! NIL_P(rb_cAxisGroup) && rb_obj_is_kind_of(a, rb_cAxisGroup) ) {
87
86
  return 1;
@@ -89,7 +88,8 @@ ca_argv_has_group (int argc, VALUE *argv)
89
88
  }
90
89
  else if ( RB_TYPE_P(a, T_DATA)
91
90
  && rb_typeddata_is_kind_of(a, &carray_data_type) ) {
92
- CArray *ca = (CArray *) DATA_PTR(a);
91
+ CArray *ca;
92
+ GetCArray(a, ca);
93
93
  if ( ca->data_type == CA_FIXLEN ) {
94
94
  if ( NIL_P(rb_cCategorical) ) {
95
95
  rb_require("carray/axis_group"); /* lazily arm (registers) */
@@ -105,7 +105,7 @@ ca_argv_has_group (int argc, VALUE *argv)
105
105
 
106
106
  /* Build a CAGroupIterator for `self[*argv]`. Two entry forms:
107
107
  (A) one-shot self[idx, nil, ...] -> build the spec via axis_group(*argv)
108
- (B) pre-built self[g] -> g is already an AxisGroup spec
108
+ (B) pre-built self[g] -> g is already an CAAxisGroup spec
109
109
  Pre-condition: ca_argv_has_group(argc, argv) is true. */
110
110
  VALUE
111
111
  rb_ca_fetch_group (int argc, VALUE *argv, VALUE self)
@@ -127,7 +127,7 @@ rb_ca_fetch_group (int argc, VALUE *argv, VALUE self)
127
127
  /* --- CAGroupIterator -------------------------------------------------------
128
128
 
129
129
  A reduction dispatcher, not an array: it holds the source value + the
130
- AxisGroup spec and exposes the tier-1 reductions. There is no to_ca / copy
130
+ CAAxisGroup spec and exposes the tier-1 reductions. There is no to_ca / copy
131
131
  / materialise path — the grouping only exists as the reduction's argument. */
132
132
 
133
133
  static VALUE
@@ -163,7 +163,7 @@ rb_ca_group_iter_labels (int argc, VALUE *argv, VALUE self)
163
163
  -> transpose to slot order (perm)
164
164
  -> squeeze the length-1 fused-band axes. */
165
165
  static VALUE
166
- group_iter_shape_output (VALUE result, VALUE group_dims, VALUE perm, VALUE squeeze)
166
+ group_iter_shape_output (volatile VALUE result, VALUE group_dims, VALUE perm, VALUE squeeze)
167
167
  {
168
168
  CArray *cr;
169
169
  VALUE dims[CA_RANK_MAX];
@@ -183,7 +183,7 @@ group_iter_shape_output (VALUE result, VALUE group_dims, VALUE perm, VALUE squee
183
183
  for ( i = 0; i < nband; i++ ) {
184
184
  dims[ng + i] = SIZE2NUM(cr->dim[i + 1]);
185
185
  }
186
- result = rb_funcall2(result, id_reshape, (int) nd, dims);
186
+ result = rb_ca_reshape((int) nd, dims, result);
187
187
 
188
188
  /* transpose to slot order */
189
189
  for ( i = 0; i < nd; i++ ) {
@@ -205,7 +205,7 @@ group_iter_shape_output (VALUE result, VALUE group_dims, VALUE perm, VALUE squee
205
205
  for ( i = 0; i < cr->ndim; i++ ) {
206
206
  if ( ! drop[i] ) keep[nkeep++] = SIZE2NUM(cr->dim[i]);
207
207
  }
208
- result = rb_funcall2(result, id_reshape, (int) nkeep, keep);
208
+ result = rb_ca_reshape((int) nkeep, keep, result);
209
209
  }
210
210
  return result;
211
211
  }
@@ -282,7 +282,6 @@ Init_ca_group_iter (void)
282
282
  id_reduce_plan = rb_intern("reduce_plan");
283
283
  id_parse_axis = rb_intern("parse_axis");
284
284
  id_axis_group_reduce = rb_intern("__reduce_by_axis_groups__");
285
- id_reshape = rb_intern("reshape");
286
285
  id_transpose = rb_intern("transpose");
287
286
  id_labels = rb_intern("labels");
288
287
 
@@ -296,8 +295,8 @@ Init_ca_group_iter (void)
296
295
 
297
296
  rb_define_private_method(rb_singleton_class(rb_cCAGroupIterator), "build_for",
298
297
  rb_ca_group_iter_build, 2);
299
- rb_define_method(rb_cCAGroupIterator, "value", rb_ca_group_iter_value, 0);
300
- rb_define_method(rb_cCAGroupIterator, "spec", rb_ca_group_iter_spec, 0);
298
+ rb_define_method(rb_cCAGroupIterator, "source", rb_ca_group_iter_value, 0);
299
+ rb_define_private_method(rb_cCAGroupIterator, "spec", rb_ca_group_iter_spec, 0);
301
300
  rb_define_method(rb_cCAGroupIterator, "labels", rb_ca_group_iter_labels, -1);
302
301
 
303
302
  /* reduction white-list. All bind to one driver; the op is recovered from
@@ -0,0 +1,196 @@
1
+ /* ---------------------------------------------------------------------------
2
+
3
+ ca_iter_state_fields.h -- the engine's fields of ca_iter_state
4
+
5
+ This header is internal to carray and carries no contract: anything in
6
+ it may change or go away in any release, 3.x patch releases included.
7
+ It is not installed (see ext/extconf.rb $INSTALLFILES).
8
+
9
+ It contains only field declarations. ca_kernel_iterator.h includes it
10
+ once, inside the definition of ca_iter_state, and only when carray
11
+ itself is built; there the fields share the bytes after the frozen
12
+ fields with _carray_private. It has no include guard and is not
13
+ included anywhere else. The types of the fields are declared before
14
+ the struct (ca_axis_desc_t and ca_op_prefix_axis_t in
15
+ ca_axis_descriptor.h).
16
+
17
+ --------------------------------------------------------------------------- */
18
+
19
+ /* --- inputs (fixed at init) --- */
20
+ struct _CArray *src;
21
+ uint8_t src_kind; /* CA_ITER_SRC_* */
22
+ int8_t level; /* 1=L1 contig, 2=L2 strided */
23
+ ca_slab_policy_t policy;
24
+ int8_t ndim;
25
+ int8_t naxes; /* AXES policy axis count */
26
+ void *pair; /* the other state of a two-state
27
+ walk (ca_iter_state_init_l2_paired),
28
+ else NULL; finished with this one
29
+ when the walk raises */
30
+ uint32_t flags;
31
+ ca_size_t bytes; /* element size */
32
+
33
+ /* --- CAStride compose-fold cache (L2 alias path) ---
34
+ For CAStride-family sources at L2, init_l2 runs
35
+ ca_stride_compose_to_root once to get the root + per-axis
36
+ byte strides + base offset, recorded here. These are engine
37
+ bookkeeping, not part of the author contract (a kernel reads the
38
+ slab_* / outer_* fields). The walk itself steps from composed_base
39
+ by outer_strides (below), which init builds from the composed
40
+ strides. Inline CA_RANK_MAX array (no heap alloc).
41
+
42
+ root is the array init attaches and finish detaches:
43
+ - the compose-fold root of an L2 CAStride walk (an entity, or a
44
+ view that lends memory), whose memory the slabs are cut from
45
+ - the parent of a descriptor view, on every descriptor route. On
46
+ the strided route (DESC_STRIDED) the slabs are cut from its
47
+ memory; on the L1 descriptor walk, PER_SLAB / PER_SLAB_HOIST
48
+ and DESC_WHOLE (SHIFT_WHOLE_VIEW included) the descriptor
49
+ engine gathers from it into a scratch of the walk's own (once,
50
+ or per slab for PER_SLAB) and the kernel reads the scratch, but
51
+ root stays attached until finish.
52
+ NULL when the walk reads src itself, a scratch filled through the
53
+ view's own transfer, or a CAStack's parents. */
54
+ struct _CArray *root; /* attached at init, detached (or
55
+ synced and detached, on WRITE)
56
+ by finish */
57
+ ca_size_t composed_strides[CA_RANK_MAX]; /* byte units */
58
+ ca_size_t composed_base; /* byte offset from root->ptr */
59
+
60
+ /* --- descriptor framework cache (descriptor sources) ---
61
+ For CASelectAxis / CAGrid / CASelect / CAWindow / CAShift, init runs
62
+ the view's *_describe_axes once then
63
+ reuses the descriptor engine's substrate (ca_axis_dispatch_prepare / _layout /
64
+ _classify_prefix) to derive the slab layout. Cached inline so
65
+ next_slab walks the prefix axes without re-doing the analysis. */
66
+ ca_axis_desc_t descs[CA_RANK_MAX]; /* post-merge axes */
67
+ ca_size_t pstrides[CA_RANK_MAX]; /* parent byte strides */
68
+ ca_size_t mdim[CA_RANK_MAX]; /* effective parent dims */
69
+ ca_op_prefix_axis_t prefix[CA_RANK_MAX]; /* pre-classified prefix */
70
+ ca_size_t parent_axis_dims[CA_RANK_MAX]; /* from describe_axes */
71
+ int8_t desc_ndim;
72
+ int8_t slab_start; /* prefix axes [0..slab_start) */
73
+ ca_size_t slab_base; /* slab base byte offset */
74
+ ca_size_t slab_bytes_desc; /* descriptor slab span */
75
+ ca_size_t total_elements; /* view.elements snapshot */
76
+
77
+ /* --- iteration cursor --- */
78
+ ca_size_t *outer_idx; /* [outer_ndim] */
79
+ ca_size_t slab_n; /* current slab element count */
80
+ ca_size_t total_slabs;
81
+ ca_size_t slabs_emitted;
82
+
83
+ /* --- chunk position --- */
84
+ ca_size_t chunk_size;
85
+
86
+ /* --- scratch buffer --- */
87
+ char *scratch_ptr;
88
+ boolean8_t *scratch_mask;
89
+ ca_size_t scratch_cap;
90
+
91
+ /* --- alias path --- */
92
+ uint8_t alias_mode; /* CA_ITER_ALIAS_* */
93
+ char *alias_ptr; /* base the slabs are yielded from:
94
+ the source or root memory, or the
95
+ scratch under ALIAS_NONE; NULL for
96
+ STACK / STACK_OUTER_K /
97
+ PER_FIBER_FUSED */
98
+ boolean8_t *alias_mask;
99
+
100
+ /* --- WRITE-path sync timing --- */
101
+ uint8_t write_dirty;
102
+
103
+ /* --- CA_SLAB_AXES policy fields ---
104
+ Populated when init_l2 is called with policy = CA_SLAB_AXES, for
105
+ every kind of source. The kernel reads slab metadata directly
106
+ from these fields (= per-walk metadata, unchanged across slabs).
107
+ outer_axes / outer_dims / outer_strides drive the prefix walk;
108
+ slab_axes_buf / slab_dims / slab_strides describe the K-D slab
109
+ handed to the kernel. All strides are byte units. Zero-init for
110
+ other policies (CA_SLAB_WHOLE / FREE).
111
+
112
+ The slab axis list is "slab_axes_buf" (an inline array); the axes
113
+ the caller passes to init are not kept in the state. */
114
+ int8_t slab_axes_buf[CA_RANK_MAX]; /* user axes (copied, sort-ascending) */
115
+ ca_size_t outer_strides[CA_RANK_MAX]; /* per-outer-axis data byte stride */
116
+ ca_size_t outer_mask_strides[CA_RANK_MAX]; /* per-outer-axis mask element stride */
117
+
118
+ /* --- Per-fiber contig scratch ---
119
+ For naxes==1 (= per-axis fiber, the catalog CA_FOR_EACH_FIBER target)
120
+ on the paths of next_slab_axes that yield a fiber in place (the
121
+ default fall-through and STACK_OUTER_K; ki_fiber_contig_data), when
122
+ slab_strides[0] != bytes the fiber
123
+ is yielded strided. To honor the catalog contract "data contig
124
+ delivery", next_slab_axes lazily allocates this scratch on first
125
+ gather and gathers each fiber here before yielding. Reused across
126
+ fibers; size grows to max slab_dims[0] * bytes.
127
+
128
+ last_data_off is the byte offset of the current slab from alias_ptr,
129
+ set by next_slab_axes on its default path BEFORE the outer_idx
130
+ advance. sync_slab reads it for the fiber it scatters back and for
131
+ the cells of a whole-view scratch it compares; the slab iterator's
132
+ map_slab reads it to mark the output's mask.
133
+
134
+ The per-slab paths (CA_ITER_ALIAS_PER_SLAB / _HOIST) and the stack
135
+ paths do not set it. */
136
+ char *fiber_data_scratch;
137
+ ca_size_t fiber_data_scratch_cap;
138
+ ca_size_t last_data_off;
139
+
140
+ /* Per-fiber contig mask scratch.
141
+ Symmetric to fiber_data_scratch. When the source carries a mask
142
+ (= alias_mask != NULL) and slab_mask_strides[0] != 1, the engine
143
+ gathers the fiber's mask into contig boolean8_t order here so the
144
+ author can do `m[i]` without indirection. Read-only from kernel
145
+ POV (= L2 WRITE semantic does not propagate to mask state), so no
146
+ scatter is needed. See header field doc for sibling field
147
+ fiber_data_scratch. */
148
+ boolean8_t *fiber_mask_scratch;
149
+ ca_size_t fiber_mask_scratch_cap;
150
+
151
+ /* --- PER_FIBER_FUSED fields ---
152
+ When alias_mode == CA_ITER_ALIAS_PER_FIBER_FUSED, next_slab_axes
153
+ builds a fiber region from outer_idx + fiber_axis and calls
154
+ ca_xfer_stride(src, ..., GET) into fiber_data_scratch instead of
155
+ reading from a whole-view scratch buffer. fiber_axis is the
156
+ source-axis index (= same axis-space as src->dim[]) of the user-
157
+ passed slab axis. fiber_native_strides are row-major byte strides
158
+ over src->dim used in ca_xfer_stride strides[] argument. */
159
+ int8_t fiber_axis;
160
+ ca_size_t fiber_native_strides[CA_RANK_MAX];
161
+
162
+ /* --- CAStack read from its parents (STACK / STACK_OUTER_K) --- */
163
+ /* When alias_mode is CA_ITER_ALIAS_STACK or STACK_OUTER_K, init_l2
164
+ attaches all K parents (= O(1) per entity parent) and caches their
165
+ ptr aliases here so next_slab_axes reads them without going through
166
+ ca_xfer_stride. Owned by iter (xfree in finish).
167
+ stack_parent_strides[] are the uniform parent-native byte strides
168
+ (all the parents of a CAStack have one shape). */
169
+ char **stack_parent_ptrs; /* [n_parents] */
170
+ int32_t stack_n_parents; /* = ((CAStack *)src)->n_parents */
171
+ ca_size_t stack_parent_strides[CA_RANK_MAX]; /* parent-space byte strides */
172
+ /* Parent-space element strides (= 1 byte per cell) for parent mask
173
+ addressing; filled by the STACK_OUTER_K init whether or not the
174
+ stack has a mask. Caching
175
+ here lets next_slab_axes compute mask_off without reading the
176
+ CAStack struct. */
177
+ ca_size_t stack_parent_mask_strides[CA_RANK_MAX];
178
+ /* When alias_mode == CA_ITER_ALIAS_STACK_OUTER_K and the CAStack
179
+ source carries a mask, init_l2 copies the K parent masks here, read
180
+ alongside parents[k]->ptr. NULL when the source has no mask. Owned
181
+ by the iterator (xfree in finish). */
182
+ boolean8_t **stack_parent_mask_ptrs; /* [n_parents] or NULL */
183
+ /* K axis position within the outer iter axis list. Set by init_l2
184
+ when alias_mode == CA_ITER_ALIAS_STACK_OUTER_K; next_slab_axes uses
185
+ outer_idx[stack_k_outer_pos] to pick the active parent. */
186
+ int8_t stack_k_outer_pos;
187
+ /* CA_ITER_ALIAS_STACK tile cache. Refills TILE fibers (= K cells each)
188
+ at once via K contig parent reads, then serves the next TILE next_slab
189
+ calls from L1d-resident buffer. Layout: cache[t][k] so a fiber at
190
+ tile_pos = cache + tile_pos * K * bytes (matches slab_strides[0] =
191
+ bytes). Tile capacity sized to fit ~32 KB L1d budget; current refill
192
+ length clamped to remaining slabs. Owned by iter (xfree in finish). */
193
+ char *stack_tile_cache; /* K * stack_tile_cap * bytes */
194
+ ca_size_t stack_tile_cap; /* TILE = fibers per refill (0 = disabled) */
195
+ ca_size_t stack_tile_pos; /* 0..stack_tile_have-1 (= ready); ==have triggers refill */
196
+ ca_size_t stack_tile_have; /* fibers actually present in current tile */