carray 3.0.3 → 3.0.4

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 (212) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +264 -0
  3. data/README.md +3 -2
  4. data/carray.gemspec +1 -1
  5. data/ext/ca_axis_descriptor.h +27 -24
  6. data/ext/ca_axis_dispatch.c +1 -6
  7. data/ext/ca_axis_group.c +27 -27
  8. data/ext/ca_binop_dispatch.c +1 -29
  9. data/ext/ca_binop_dispatch.h +0 -1
  10. data/ext/ca_categorical_iterator.c +205 -134
  11. data/ext/ca_compare.c +7 -18
  12. data/ext/ca_group_iter.c +16 -17
  13. data/ext/ca_kernel_iterator.c +1234 -1994
  14. data/ext/ca_kernel_iterator.h +391 -437
  15. data/ext/ca_monop_dispatch.c +82 -59
  16. data/ext/ca_monop_dispatch.h +1 -0
  17. data/ext/ca_obj_array.c +96 -39
  18. data/ext/ca_obj_bincmp.c +24 -21
  19. data/ext/ca_obj_binop.c +15 -26
  20. data/ext/ca_obj_bitarray.c +3 -2
  21. data/ext/ca_obj_bitfield.c +5 -6
  22. data/ext/ca_obj_block.c +18 -9
  23. data/ext/ca_obj_byte_swap.c +5 -6
  24. data/ext/ca_obj_const_string.c +173 -19
  25. data/ext/ca_obj_face.c +238 -57
  26. data/ext/ca_obj_face.h +19 -0
  27. data/ext/ca_obj_fake.c +15 -29
  28. data/ext/ca_obj_farray.c +6 -3
  29. data/ext/ca_obj_field.c +17 -5
  30. data/ext/ca_obj_fixlen_string.c +2 -3
  31. data/ext/ca_obj_grid.c +406 -132
  32. data/ext/ca_obj_meld.c +172 -34
  33. data/ext/ca_obj_moncmp.c +23 -8
  34. data/ext/ca_obj_monop.c +67 -57
  35. data/ext/ca_obj_object.c +157 -51
  36. data/ext/ca_obj_record.c +12 -7
  37. data/ext/ca_obj_reduce.c +13 -8
  38. data/ext/ca_obj_refer.c +25 -27
  39. data/ext/ca_obj_remap.c +30 -9
  40. data/ext/ca_obj_repeat.c +9 -10
  41. data/ext/ca_obj_roll.c +6 -5
  42. data/ext/ca_obj_select.c +14 -9
  43. data/ext/ca_obj_select_axis.c +15 -47
  44. data/ext/ca_obj_shift.c +11 -1
  45. data/ext/ca_obj_stack.c +179 -34
  46. data/ext/ca_obj_stride.c +162 -115
  47. data/ext/ca_obj_string.c +71 -4
  48. data/ext/ca_obj_tile.c +6 -5
  49. data/ext/ca_obj_time.c +22 -6
  50. data/ext/ca_obj_timedelta.c +22 -6
  51. data/ext/ca_obj_transpose.c +5 -3
  52. data/ext/ca_obj_triop.c +23 -32
  53. data/ext/ca_obj_window.c +190 -26
  54. data/ext/ca_sweep_engine.c +2 -2
  55. data/ext/caframe_csv_reader.c +80 -1
  56. data/ext/carray.h +181 -75
  57. data/ext/carray_access.c +184 -96
  58. data/ext/carray_address_basis.c +343 -87
  59. data/ext/carray_attribute.c +89 -6
  60. data/ext/carray_bincount.c +10 -8
  61. data/ext/carray_broadcast.c +13 -8
  62. data/ext/carray_build_flags.h +3 -0
  63. data/ext/carray_call_cfunc.c +108 -0
  64. data/ext/carray_cast.c +142 -24
  65. data/ext/carray_cast_func.rb +46 -6
  66. data/ext/carray_class.c +4 -3
  67. data/ext/carray_config.h +28 -0
  68. data/ext/carray_conversion.c +16 -7
  69. data/ext/carray_copy.c +51 -14
  70. data/ext/carray_core.c +147 -55
  71. data/ext/carray_count.c +50 -20
  72. data/ext/carray_element.c +76 -33
  73. data/ext/carray_factorize.c +118 -64
  74. data/ext/carray_generate.c +191 -50
  75. data/ext/carray_histogram.c +62 -17
  76. data/ext/carray_hold.c +12 -15
  77. data/ext/carray_index_classifier.c +388 -176
  78. data/ext/carray_internal.h +336 -29
  79. data/ext/carray_kernels_bincmp.c +4453 -0
  80. data/ext/carray_kernels_binop.c +11368 -0
  81. data/ext/carray_kernels_init.c +1734 -0
  82. data/ext/carray_kernels_moncmp.c +2104 -0
  83. data/ext/carray_kernels_monop.c +18369 -0
  84. data/ext/carray_kernels_reduce_aggregate.c +23818 -0
  85. data/ext/carray_kernels_reduce_boolean.c +406 -0
  86. data/ext/carray_kernels_reduce_cumulative.c +10086 -0
  87. data/ext/carray_kernels_reduce_extreme.c +17634 -0
  88. data/ext/carray_kernels_reduce_variance.c +3849 -0
  89. data/ext/carray_kernels_scan.c +4302 -0
  90. data/ext/carray_kernels_search.c +33829 -0
  91. data/ext/carray_kernels_sort.c +10906 -0
  92. data/ext/carray_kernels_triop.c +1864 -0
  93. data/ext/carray_key_windows.c +216 -0
  94. data/ext/carray_lazy.c +170 -111
  95. data/ext/carray_mask.c +292 -72
  96. data/ext/carray_median_percentile.c +82 -28
  97. data/ext/carray_memory_view.c +669 -561
  98. data/ext/carray_operator.c +124 -47
  99. data/ext/carray_order.c +384 -76
  100. data/ext/carray_partition.c +28 -27
  101. data/ext/carray_random.c +67 -13
  102. data/ext/carray_round.c +273 -0
  103. data/ext/carray_scatter.c +63 -38
  104. data/ext/carray_segment.c +3 -3
  105. data/ext/carray_slab.c +91 -28
  106. data/ext/carray_sort.c +35 -35
  107. data/ext/carray_struct.c +19 -18
  108. data/ext/carray_test.c +26 -5
  109. data/ext/carray_undef.c +4 -0
  110. data/ext/carray_utils.c +246 -45
  111. data/ext/mk_call_cfunc.rb +19 -2
  112. data/ext/mkkernel.rb +198 -240
  113. data/ext/ruby_carray.c +10 -4
  114. data/ext/version.h +4 -4
  115. data/lib/carray/attribute.rb +126 -22
  116. data/lib/carray/autoload_carray.rb +8 -31
  117. data/lib/carray/autoload_method_extension.rb +14 -5
  118. data/lib/carray/axis_group.rb +255 -106
  119. data/lib/carray/basics.rb +78 -10
  120. data/lib/carray/bincount_nd.rb +12 -2
  121. data/lib/carray/block_iterator.rb +126 -23
  122. data/lib/carray/boolean_reduce.rb +17 -17
  123. data/lib/carray/categorical.rb +165 -43
  124. data/lib/carray/categorical_iterator.rb +130 -27
  125. data/lib/carray/complex.rb +5 -5
  126. data/lib/carray/conditional.rb +67 -18
  127. data/lib/carray/const_string.rb +14 -5
  128. data/lib/carray/construct.rb +15 -11
  129. data/lib/carray/data_type_extension.rb +1 -0
  130. data/lib/carray/distance_limit.rb +77 -0
  131. data/lib/carray/fixlen_string.rb +7 -1
  132. data/lib/carray/frame/column_verbs.rb +70 -0
  133. data/lib/carray/frame/convert.rb +69 -0
  134. data/lib/carray/frame/csv_parser.rb +111 -17
  135. data/lib/carray/frame/describe.rb +3 -2
  136. data/lib/carray/frame/frame.rb +246 -21
  137. data/lib/carray/frame/group.rb +510 -95
  138. data/lib/carray/frame/io.rb +115 -28
  139. data/lib/carray/frame/join.rb +312 -51
  140. data/lib/carray/frame/records.rb +83 -7
  141. data/lib/carray/frame/reshape.rb +42 -35
  142. data/lib/carray/frame/sort.rb +7 -19
  143. data/lib/carray/frame/verbs.rb +188 -48
  144. data/lib/carray/frame/windows.rb +117 -0
  145. data/lib/carray/frame.rb +2 -0
  146. data/lib/carray/fuse_source.rb +11 -4
  147. data/lib/carray/fusion.rb +40 -16
  148. data/lib/carray/histogram.rb +6 -1
  149. data/lib/carray/inspect.rb +31 -8
  150. data/lib/carray/iterator.rb +85 -7
  151. data/lib/carray/key_windows.rb +620 -0
  152. data/lib/carray/lazy.rb +182 -183
  153. data/lib/carray/mask_gap_fill.rb +63 -57
  154. data/lib/carray/meld_reduce.rb +12 -14
  155. data/lib/carray/methods/bin.rb +26 -18
  156. data/lib/carray/methods/bincount.rb +16 -0
  157. data/lib/carray/methods/bit_string.rb +1 -1
  158. data/lib/carray/methods/choose.rb +16 -3
  159. data/lib/carray/methods/composition.rb +3 -1
  160. data/lib/carray/methods/gather_nd.rb +14 -5
  161. data/lib/carray/methods/insert_block.rb +2 -2
  162. data/lib/carray/methods/is_between.rb +59 -0
  163. data/lib/carray/methods/is_in.rb +53 -36
  164. data/lib/carray/methods/join.rb +1 -6
  165. data/lib/carray/methods/lag.rb +49 -0
  166. data/lib/carray/methods/locate_addr.rb +47 -51
  167. data/lib/carray/methods/mode.rb +1 -1
  168. data/lib/carray/methods/nunique.rb +11 -6
  169. data/lib/carray/methods/resize.rb +3 -3
  170. data/lib/carray/methods/value_counts.rb +4 -11
  171. data/lib/carray/record_order.rb +3 -3
  172. data/lib/carray/rng.rb +3 -0
  173. data/lib/carray/runtime.rb +53 -8
  174. data/lib/carray/segment_iterator.rb +313 -82
  175. data/lib/carray/serialize.rb +24 -10
  176. data/lib/carray/slab_iterator.rb +50 -0
  177. data/lib/carray/string.rb +105 -5
  178. data/lib/carray/string_operation_extension.rb +8 -1
  179. data/lib/carray/struct.rb +9 -21
  180. data/lib/carray/struct_builder.rb +19 -8
  181. data/lib/carray/time.rb +726 -104
  182. data/lib/carray/window_iterator.rb +122 -16
  183. data/lib/carray.rb +7 -0
  184. data/yard-stubs/ca_obj_bitfield.rb +1 -1
  185. data/yard-stubs/ca_obj_field.rb +4 -0
  186. data/yard-stubs/ca_obj_grid.rb +21 -10
  187. data/yard-stubs/ca_obj_refer.rb +2 -1
  188. data/yard-stubs/ca_obj_roll.rb +2 -2
  189. data/yard-stubs/ca_obj_shift.rb +5 -1
  190. data/yard-stubs/ca_obj_tile.rb +1 -1
  191. data/yard-stubs/ca_obj_window.rb +8 -3
  192. data/yard-stubs/carray_access.rb +6 -1
  193. data/yard-stubs/carray_attribute.rb +0 -5
  194. data/yard-stubs/carray_cast.rb +10 -0
  195. data/yard-stubs/carray_conversion.rb +2 -1
  196. data/yard-stubs/carray_copy.rb +4 -2
  197. data/yard-stubs/carray_count.rb +4 -4
  198. data/yard-stubs/carray_element.rb +10 -4
  199. data/yard-stubs/carray_generate.rb +21 -1
  200. data/yard-stubs/carray_mask.rb +26 -2
  201. data/yard-stubs/carray_math.rb +27 -1
  202. data/yard-stubs/carray_median_percentile.rb +5 -2
  203. data/yard-stubs/carray_memory_view.rb +40 -23
  204. data/yard-stubs/carray_order.rb +29 -18
  205. data/yard-stubs/carray_random.rb +13 -4
  206. data/yard-stubs/carray_scatter.rb +14 -9
  207. data/yard-stubs/carray_slab.rb +9 -2
  208. data/yard-stubs/carray_sort.rb +6 -5
  209. data/yard-stubs/carray_test.rb +5 -3
  210. metadata +25 -3
  211. data/lib/carray/arrow_tensor.rb +0 -401
  212. data/lib/carray/table.rb +0 -108
@@ -1,24 +1,46 @@
1
1
  /* ---------------------------------------------------------------------------
2
2
 
3
- ca_kernel_iterator.h
4
-
5
- T1 kernel_iterator — C extension author surface.
6
-
7
- Status: TWO-TIER FREEZE CONTRACT (3.0 onward). The surface is split
3
+ ca_kernel_iterator.h -- the kernel iterator, for C extension authors
4
+
5
+ A kernel written against this header receives the cells of any CArray
6
+ -- an entity, a view of a view, a selection, a lazy expression -- one
7
+ slab at a time, with the mask beside them, and on a write the cells it
8
+ changes reach the array. How the cells are delivered is the engine's
9
+ business (ca_kernel_iterator.c); the kernel only names the axes it
10
+ wants and walks what it is given.
11
+
12
+ Where to start:
13
+ - guides/devel/11_kernel_iterator.md: the surface and its contract,
14
+ with worked kernels
15
+ - docs/authoring/HOW_TO_WRITE_KERNEL.md: the reference, and which
16
+ macro to pick for a reduction, a map, a scan or a whole-array walk
17
+ - the block macros below (CA_FOR_EACH_SLAB / _FIBER / _WHOLE) are
18
+ the usual way in; the raw entry points (ca_iter_state_init_l1 /
19
+ init_l2 ...) are for kernels that handle the return codes
20
+ themselves
21
+
22
+ Freeze contract (3.0 onward). The surface is split
8
23
  so the engine can be re-implemented across 3.x (the surface stays fixed)
9
- without breaking ext-gem kernels. See docs/authoring/HOW_TO_WRITE_KERNEL.md
10
- §0/§13 for the prose contract; utils/check_kernel_surface_freeze.rb
24
+ without breaking ext-gem kernels. docs/authoring/HOW_TO_WRITE_KERNEL.md
25
+ states the contract in prose; utils/check_kernel_surface_freeze.rb
11
26
  (rake kernel_surface_check) is the mechanical pin.
12
27
 
13
28
  FROZEN (do NOT rename / re-arity / change semantics; additions only):
14
- - the author macros (CA_FOR_EACH_SLAB / _FIBER families,
15
- CA_SLAB_REDUCE_* / _MAP_* / _SCAN_* suites, CA_L2_FOR_EACH,
16
- CA_*_UNMASKED helpers)
29
+ - the author macros: the CA_FOR_EACH_SLAB / _FIBER / _WHOLE
30
+ families (including _FIBER_PAIR / _FIBER_PAIR_MASKED), the
31
+ CA_SLAB_REDUCE_* / _MAP_* / _SCAN_* suites except the generator-
32
+ only macros listed under INTERNAL, CA_L2_FOR_EACH, and the mask
33
+ helper CA_FOR_EACH_UNMASKED
17
34
  - the raw-API entry points the macros expand to: ca_iter_state_
18
- init_l2 / init_l2_paired / next_slab_axes / sync_slab / finish /
19
- close, ca_iter_check_init, ca_iter_check_same_shape
35
+ init_l1 / next_slab / init_l2 / init_l2_paired / next_slab_axes /
36
+ sync_slab / finish / close, ca_iter_check_init,
37
+ ca_iter_check_same_shape
38
+ - the author entry points beside them: ca_iter_ensure (a body that
39
+ can raise) and ca_iter_register_source_kind (an externally
40
+ installed view class)
20
41
  - the enum/status tokens authors write literally: CA_SLAB_AXES,
21
- CA_KERNEL_WRITE, CA_KERNEL_NO_MASK, CA_ITER_OK, CA_ITER_ERR_*
42
+ CA_SLAB_WHOLE, CA_KERNEL_WRITE, CA_KERNEL_NO_MASK, CA_ITER_OK,
43
+ CA_ITER_ERR_*
22
44
  - the slab-delivery representation = the ca_iter_state fields a
23
45
  kernel reads (marked "FROZEN author contract" at the struct):
24
46
  slab_ndim / slab_dims / slab_strides / slab_mask_strides /
@@ -28,9 +50,15 @@
28
50
 
29
51
  INTERNAL (free to refactor across 3.x — engine implementation):
30
52
  - the state-machine functions' bodies + init_l1 / next_slab /
31
- next_slab_strided / ca_iter_can_alias
53
+ ca_iter_can_alias
54
+ - the macros only the kernel generator (ext/mkkernel.rb) emits:
55
+ CA_SLAB_REDUCE_SUM8_EX / MIN8_EX / MAX8_EX / STAR8_EX (8-way
56
+ accumulator split; they take EXPR(x) rather than exposing v and
57
+ idx), CA_SLAB_REDUCE_T_MIN_EX / _MAX_EX / _STAR_EX (what it emits
58
+ when a :min / :max / :prod kernel does not take the 8-way form),
59
+ CA_SLAB_SCAN_T_GATED, and CA_SLAB_REDUCE_T_VAR_EX (no caller)
32
60
  - alias_mode / src_kind routing (CA_ITER_ALIAS_* / CA_ITER_SRC_* /
33
- CA_KERNEL_FIBER_CONTIG / CA_SLAB_FREE / CA_SLAB_WHOLE)
61
+ CA_KERNEL_FIBER_CONTIG / CA_SLAB_FREE)
34
62
  - every ca_iter_state field NOT marked frozen (scratch / stack /
35
63
  fiber / composed / descriptor bookkeeping) + physical layout
36
64
 
@@ -38,9 +66,9 @@
38
66
  _EX macro variant + thin wrapper, new catalog macro) — that is how
39
67
  the surface evolves within 3.x. Changing or removing a FROZEN name
40
68
  is a 3.x breaking change: update the doc contract AND the guard in
41
- the same commit. (PROPOSAL_T1_KERNEL_ITERATOR.md;
42
- devel/MEMO_KERNEL_ITERATOR_OOP_PATH.vev4.md for the planned engine
43
- overhaul this contract insulates against.)
69
+ the same commit. The engine behind these names may be rewritten
70
+ within 3.x; this contract is what keeps such a rewrite invisible to
71
+ kernel authors.
44
72
 
45
73
  --------------------------------------------------------------------------- */
46
74
 
@@ -50,7 +78,7 @@
50
78
  #include "carray.h"
51
79
  #include "ca_iter_substrate.h" /* ca_axis_desc_t, ca_op_prefix_axis_t */
52
80
 
53
- /* ---- slab policy (proposal §2.2) ------------------------------------- */
81
+ /* ---- slab policy ------------------------------------------------------ */
54
82
  typedef enum {
55
83
  CA_SLAB_FREE = 0, /* engine-chosen chunk. Not implemented:
56
84
  init returns CA_ITER_ERR_POLICY. */
@@ -58,57 +86,77 @@ typedef enum {
58
86
  CA_SLAB_WHOLE = 2 /* whole array in one slab */
59
87
  } ca_slab_policy_t;
60
88
 
61
- /* ---- kernel flags (proposal §2.2) ------------------------------------ */
89
+ /* ---- kernel flags ------------------------------------------------------ */
62
90
  #define CA_KERNEL_READ 0x0
63
- #define CA_KERNEL_WRITE 0x1 /* step 4+ */
64
- #define CA_KERNEL_NO_MASK 0x2 /* step 7 */
65
- #define CA_KERNEL_CHUNK_HINT 0x4 /* T2 reserve, never set in MVP */
66
- #define CA_KERNEL_FIBER_CONTIG 0x8 /* PROPOSAL_FIBER_DELIVERY F.1a:
67
- when set with policy=CA_SLAB_AXES
68
- and naxes==1, next_slab_axes
69
- guarantees contig data delivery
70
- (= gathers strided fibers into
91
+ #define CA_KERNEL_WRITE 0x1 /* the kernel writes the slab back */
92
+ #define CA_KERNEL_NO_MASK 0x2 /* the kernel cannot take a mask:
93
+ a masked source is refused */
94
+ #define CA_KERNEL_CHUNK_HINT 0x4 /* reserved: init refuses it
95
+ with CA_ITER_ERR_FLAGS */
96
+ #define CA_KERNEL_FIBER_CONTIG 0x8 /* with policy=CA_SLAB_AXES and
97
+ naxes==1, next_slab_axes hands
98
+ out every fiber contiguous
99
+ (gathering a strided one into
71
100
  fiber_data_scratch). Set by the
72
- CA_FOR_EACH_FIBER macro family
73
- (F.2). Direct callers that want
74
- the original L2 strided semantic
75
- (= *out_ptr is alias_ptr + offset,
76
- caller walks via slab_strides[0])
77
- leave this bit clear. */
78
-
79
- /* ---- alias mode (proposal §3.1) -------------------------------------- */
80
- #define CA_ITER_ALIAS_NONE 0 /* scratch materialize. step 2+. */
81
- #define CA_ITER_ALIAS_CONTIG 1 /* parent.ptr+offset direct slab. */
82
- #define CA_ITER_ALIAS_STRIDED 2 /* L2 stride-aware. step 3+. */
101
+ CA_FOR_EACH_FIBER macro family.
102
+ Direct callers that walk the
103
+ slab with slab_strides[0]
104
+ themselves leave it clear. */
105
+
106
+ /* ---- alias mode: where the slabs are cut from (internal) --------------- */
107
+ #define CA_ITER_ALIAS_NONE 0 /* a scratch the walk gathered */
108
+ #define CA_ITER_ALIAS_CONTIG 1 /* the source's own memory */
109
+ #define CA_ITER_ALIAS_STRIDED 2 /* the root's or parent's memory,
110
+ through composed strides */
83
111
  #define CA_ITER_ALIAS_ATTACH 3 /* Not set by the engine. A
84
112
  SRC_ATTACH walk runs with
85
113
  CA_ITER_ALIAS_NONE over an
86
- iterator-owned scratch filled and
87
- written back with ca_xfer_all. */
88
- #define CA_ITER_ALIAS_PER_SLAB 4 /* Phase C T3 fallback: per-slab
89
- materialise via ca_axis_dispatch_gather
90
- with subset descriptor (= caller-built
91
- outer-pinned). scratch_ptr reused
92
- across outer iters (D1.1 (B): max slab
93
- size, refilled in next_slab_axes).
94
- READ-only in C.1 (WRITE = future). */
95
- #define CA_ITER_ALIAS_PER_FIBER_FUSED 6 /* PROPOSAL_FIBER_PER_SOURCE_PATH
96
- F.6.1: per-fiber fused xfer
97
- dispatch. Engine skips
98
- whole-view materialise and
99
- calls ca_xfer_stride GET/PUT
100
- per fiber, routing into the
101
- view's fused fast path (X.1
102
- OOB-fused, X.4 transform-
103
- fused). Selected by
104
- ca_iter_should_per_fiber_fused
105
- (= hybrid src_kind + view func
106
- probe + fiber axis effective
107
- stride predicate). Yields
108
- from fiber_data_scratch with
109
- mask in fiber_mask_scratch
110
- (= rev2 §3.3, (data,mask)
111
- pair). */
114
+ iterator-owned scratch, filled with
115
+ ca_xfer_all and written back with
116
+ ki_scratch_push_changed (only the
117
+ changed cells). */
118
+ #define CA_ITER_ALIAS_PER_SLAB 4 /* a descriptor view read one slab at
119
+ a time: ca_axis_dispatch_gather
120
+ over a subset of the descriptors
121
+ with the outer axes pinned, into
122
+ one slab-sized scratch refilled
123
+ in next_slab_axes. Read walks
124
+ only; a write walk takes the
125
+ transfer walk instead. */
126
+ #define CA_ITER_ALIAS_PER_SLAB_HOIST 5 /* PER_SLAB when the innermost slab
127
+ axis is STRIDE and no axis is
128
+ SHIFT: the outer and
129
+ non-innermost slab offsets are
130
+ computed once per slab row and
131
+ the inner run is a memcpy. */
132
+ #define CA_ITER_ALIAS_PER_FIBER_FUSED 6 /* each fiber is fetched (and on a
133
+ write put back) with the view's
134
+ ca_xfer_stride, without
135
+ gathering the whole view: a
136
+ CAWindow / CAShift fiber, or a
137
+ read of the innermost fiber of
138
+ a conversion view. Chosen by
139
+ ca_iter_should_per_fiber_fused.
140
+ The fiber is handed out from
141
+ fiber_data_scratch, its mask
142
+ from fiber_mask_scratch. */
143
+ #define CA_ITER_ALIAS_STACK 7 /* a CAStack read with slab_axes ==
144
+ [k_axis] (the stack axis alone,
145
+ e.g. view.mean(axis: k_axis)).
146
+ init_l2 attaches the K parents
147
+ and caches their pointers and
148
+ their common native strides;
149
+ next_slab_axes hands out each
150
+ fiber's K cells from a tile cache
151
+ (K * tile * bytes, tile 8..64
152
+ fibers) refilled from the parents'
153
+ memory, without ca_xfer_stride.
154
+ Only for slab_axes == [k_axis]
155
+ and an unmasked stack; any other
156
+ stack walk takes STACK_OUTER_K or
157
+ the transfer walk. Delivery
158
+ through ca_xfer_stride was tried
159
+ and measured ~16x slower. */
112
160
  #define CA_ITER_ALIAS_STACK_OUTER_K 8 /* CAStack source + CA_SLAB_AXES
113
161
  whose slab leaves out the stack
114
162
  axis (k_axis), which is then an
@@ -125,61 +173,18 @@ typedef enum {
125
173
  that is not contiguous in its
126
174
  parent is gathered (FIBER_CONTIG).
127
175
  READ only. */
128
- #define CA_ITER_ALIAS_STACK 7 /* PROPOSAL_CASTACK_LOOP_INTERCHANGE
129
- Vector A rev2 (direct per-parent
130
- ptr access): CAStack source +
131
- CA_SLAB_AXES with slab_axes ==
132
- [k_axis] (= the stack axis alone,
133
- e.g. view.mean(axis: k_axis)).
134
- init_l2 attaches K parents up
135
- front (= O(1) per entity parent),
136
- caches parent->ptr aliases +
137
- uniform parent-native byte
138
- strides. next_slab_axes does
139
- K-fold direct memcpy gather from
140
- parents[k]->ptr + parent_off
141
- into a slab-sized scratch,
142
- bypassing ca_xfer_stride
143
- entirely (= no per-call dispatch
144
- / cyclic_check / strided_walk
145
- function-boundary overhead).
146
- Peak buffer = K * bytes
147
- (= slab footprint), not
148
- K * parent.elements * bytes.
149
- Scope-narrow to slab_axes == [0]:
150
- arbitrary slab shapes require
151
- per-cell index decode which
152
- loses the inner contig fast
153
- path; covers the demand-driving
154
- case (= reduce along the stacked
155
- K-axis). Mask-bearing CAStack
156
- falls back to SRC_ATTACH whole-
157
- view path (per-slab mask gather
158
- = future extension). Rev1
159
- explored xfer_stride-based
160
- delivery (see PROPOSAL
161
- rev2 §11) and was rejected for
162
- ~16x wall-clock regression. */
163
- #define CA_ITER_ALIAS_PER_SLAB_HOIST 5 /* Phase C T3 specialised (B-1b,
164
- C.1b): innermost slab axis is
165
- STRIDE and no SHIFT axes
166
- anywhere. Manual gather:
167
- outer + non-innermost-slab axes
168
- hoisted (computed once per slab
169
- row), inner = pure STRIDE
170
- linear memcpy (= no engine
171
- per-cell switch, SIMD-friendly
172
- contig run). Target: 1.5-1.8x
173
- win vs (A) fallback for INDEX
174
- slab with innermost STRIDE. */
175
176
 
176
177
  /* ---- error codes ----------------------------------------------------- */
177
178
  #define CA_ITER_OK 0
178
179
  #define CA_ITER_ERR_NOT_CHEAP 1 /* the classifier does not recognise
179
180
  the source (a view with no attach
180
181
  slot; any other external obj_type
181
- is delivered by attaching it) */
182
- #define CA_ITER_ERR_POLICY 2 /* policy not implemented yet */
182
+ is delivered by the transfer walk,
183
+ through its xfer slots) */
184
+ #define CA_ITER_ERR_POLICY 2 /* policy or axes refused: a policy
185
+ not implemented, axes NULL / out
186
+ of range / duplicated, AXES at
187
+ L1, descriptor rank != view rank */
183
188
  #define CA_ITER_ERR_FLAGS 3 /* flag combination unsupported;
184
189
  also returned for a NULL state
185
190
  or source */
@@ -187,9 +192,9 @@ typedef enum {
187
192
  #define CA_ITER_ERR_MASK 5 /* reserved: no entry point returns
188
193
  it (masked sources are accepted) */
189
194
  #define CA_ITER_ERR_MASK_NOT_ALLOWED 6 /* NO_MASK flag set on a masked source */
190
- /* ---- source kind (step 5+, internal routing) ------------------------- */
195
+ /* ---- source kind (internal routing) ------------------------------------ */
191
196
  #define CA_ITER_SRC_NONE 0
192
- #define CA_ITER_SRC_CASTRIDE 1 /* entity / CAStride family (step 1-4) */
197
+ #define CA_ITER_SRC_CASTRIDE 1 /* entity / CAStride family */
193
198
  #define CA_ITER_SRC_DESCRIPTOR 2 /* CASelectAxis / CAGrid / CASelect /
194
199
  CAWindow / CAShift: per-axis
195
200
  descriptors (describe_axes) */
@@ -200,32 +205,31 @@ typedef enum {
200
205
  registered external obj_type: the
201
206
  view's own transfer gathers it */
202
207
  #define CA_ITER_SRC_DESCRIPTOR_L2_ALIASABLE 4
203
- /* F-2 (PROPOSAL_F2_KERNEL_ITERATOR_ALIAS
204
- rev6): descriptor view whose innermost
205
- axis is STRIDE kind. init_l2 takes the
206
- alias path (= no scratch alloc, parent.ptr
207
- + outer-prefix-offset + inner-byte-start
208
- is yielded with inner_byte_stride). Only
209
- emitted by ca_iter_route_source after
210
- describe_axes inspection. */
208
+ /* a descriptor view whose innermost axis
209
+ is STRIDE kind, so an L2 walk can read
210
+ the parent's memory in place. Only
211
+ returned by ca_iter_route_source,
212
+ after describe_axes. */
211
213
 
212
214
  /* ---- source-kind registration for externally installed obj_types -----
213
215
 
214
216
  The classifier recognises the core's own view classes by comparing
215
217
  their operation table against a list compiled into the engine. A view
216
218
  class installed by a companion gem through ca_install_obj_type matches
217
- nothing on that list; it is delivered as CA_ITER_SRC_ATTACH (the view's
218
- own attach gathers it), and so is one that registers here, once, in the
219
- class's Init:
219
+ nothing on that list; it is delivered as CA_ITER_SRC_ATTACH (gathered
220
+ through the view's own transfer), and so is one that registers here,
221
+ once, in the class's Init:
220
222
 
221
223
  ca_iter_register_source_kind(CA_OBJ_MY_VIEW, CA_ITER_SRC_ATTACH);
222
224
 
223
225
  Registering states the routing rather than changing it.
224
226
  CA_ITER_SRC_ATTACH is the only kind that may be registered. It is the
225
- one whose contract an external class can meet on its own: func_attach
226
- materialises (or aliases) src->ptr and func_sync scatters back — the
227
- CAFake contract, which every view already implements to be attachable
228
- at all. The other kinds are not open to registration: SRC_CASTRIDE
227
+ one whose contract an external class can meet on its own: the engine
228
+ reads the cells with ca_xfer_all GET and, on a WRITE walk, sends back
229
+ the changed cells with ca_xfer_addrs (or ca_xfer_all PUT when every
230
+ cell changed), so the view's transfer slots are all it needs -- the
231
+ slots every view already implements. The view's attach / sync slots
232
+ are not called. The other kinds are not open to registration: SRC_CASTRIDE
229
233
  asserts the struct *is* a CAStride (the engine reads its strides
230
234
  directly, and a class that really is one is already classified by its
231
235
  inherited operation table), and SRC_DESCRIPTOR requires a
@@ -239,18 +243,18 @@ typedef enum {
239
243
  materialising path), and consults this table before the built-in list. */
240
244
  void ca_iter_register_source_kind (int obj_type, uint8_t kind);
241
245
 
242
- /* ---- iter state (proposal §3.1, step 1 subset) ----------------------- */
243
- /* Fields marked "[step N+]" are present for forward layout compat but
244
- are zero-initialised and unused in step 1. Adding them now avoids a
245
- struct-layout churn when later steps fill them in. */
246
+ /* ---- iter state ------------------------------------------------------- */
247
+ /* The state lives on the ext author's stack, so its layout is fixed: a
248
+ field is never removed, and the ones no path uses any more (chunk_pos,
249
+ alias_stride) stay as zero-initialised slots. */
246
250
  typedef struct {
247
251
  /* --- inputs (fixed at init) --- */
248
252
  struct _CArray *src;
249
- uint8_t src_kind; /* CA_ITER_SRC_* (step 5+) */
250
- int8_t level; /* 1=L1 contig, 2=L2 strided (step 3+) */
253
+ uint8_t src_kind; /* CA_ITER_SRC_* */
254
+ int8_t level; /* 1=L1 contig, 2=L2 strided */
251
255
  ca_slab_policy_t policy;
252
256
  int8_t ndim;
253
- int8_t naxes; /* [step 5+] AXES policy axis count */
257
+ int8_t naxes; /* AXES policy axis count */
254
258
  void *pair; /* the other state of a two-state
255
259
  walk (ca_iter_state_init_l2_paired),
256
260
  else NULL; finished with this one
@@ -258,15 +262,20 @@ typedef struct {
258
262
  uint32_t flags;
259
263
  ca_size_t bytes; /* element size */
260
264
 
261
- /* --- CAStride compose-fold cache (step 3+, L2 alias path) ---
265
+ /* --- CAStride compose-fold cache (L2 alias path) ---
262
266
  For CAStride-family sources at L2, init_l2 runs
263
- ca_stride_compose_to_root once to get the root entity + per-axis
264
- byte strides + base offset. Cached here so next_slab_strided's
265
- per-iter offset calc is just Σ outer_idx[k] * composed_strides[k].
266
- Inline CA_RANK_MAX array (no heap alloc) since CA_DIM_MAX=16 keeps
267
- the footprint at 128 bytes per state. root is NULL on L1 paths
268
- and on L2 with an entity / contig source (use src as the base). */
269
- struct _CArray *root; /* root entity, attached at init */
267
+ ca_stride_compose_to_root once to get the root + per-axis
268
+ byte strides + base offset, kept here for the author to read. The
269
+ walk itself steps from composed_base by outer_strides (below), which
270
+ init builds from the composed strides. Inline CA_RANK_MAX array (no
271
+ heap alloc). root is the array attached at
272
+ init whose memory the walk reads: the compose-fold root of an L2
273
+ CAStride walk (an entity, or a view that lends memory) or the parent
274
+ of a descriptor view. NULL when the walk reads src itself or a
275
+ scratch of its own. */
276
+ struct _CArray *root; /* attached at init, detached (or
277
+ synced and detached, on WRITE)
278
+ by finish */
270
279
  ca_size_t composed_strides[CA_RANK_MAX]; /* byte units */
271
280
  ca_size_t composed_base; /* byte offset from root->ptr */
272
281
 
@@ -288,30 +297,34 @@ typedef struct {
288
297
  ca_size_t total_elements; /* view.elements snapshot */
289
298
 
290
299
  /* --- iteration cursor --- */
291
- ca_size_t *outer_idx; /* [step 3+] [outer_axes_n] */
300
+ ca_size_t *outer_idx; /* [outer_ndim] */
292
301
  ca_size_t slab_n; /* current slab element count */
293
302
  ca_size_t total_slabs;
294
303
  ca_size_t slabs_emitted;
295
304
 
296
- /* --- chunk position (T2 forward compat, proposal §8) --- */
305
+ /* --- chunk position (chunk_pos is unused, kept for layout) --- */
297
306
  ca_size_t chunk_pos;
298
307
  ca_size_t chunk_size;
299
308
 
300
- /* --- scratch buffer [step 2+] --- */
309
+ /* --- scratch buffer --- */
301
310
  char *scratch_ptr;
302
311
  boolean8_t *scratch_mask;
303
312
  ca_size_t scratch_cap;
304
313
 
305
314
  /* --- alias path --- */
306
315
  uint8_t alias_mode; /* CA_ITER_ALIAS_* */
307
- char *alias_ptr; /* slab ptr when alias_mode != NONE */
308
- boolean8_t *alias_mask; /* [step 6+] */
309
- ca_size_t alias_stride; /* [step 3+] */
310
-
311
- /* --- WRITE-path sync timing [step 4+] --- */
316
+ char *alias_ptr; /* base the slabs are yielded from:
317
+ the source or root memory, or the
318
+ scratch under ALIAS_NONE; NULL for
319
+ STACK / STACK_OUTER_K /
320
+ PER_FIBER_FUSED */
321
+ boolean8_t *alias_mask;
322
+ ca_size_t alias_stride; /* unused, kept for layout */
323
+
324
+ /* --- WRITE-path sync timing --- */
312
325
  uint8_t write_dirty;
313
326
 
314
- /* --- CA_SLAB_AXES policy fields (Phase A capstone, T1) ---
327
+ /* --- CA_SLAB_AXES policy fields ---
315
328
  Populated when init_l2 is called with policy = CA_SLAB_AXES, for
316
329
  every kind of source. The kernel reads slab metadata directly
317
330
  from these fields (= per-walk metadata, unchanged across slabs).
@@ -320,8 +333,8 @@ typedef struct {
320
333
  handed to the kernel. All strides are byte units. Zero-init for
321
334
  other policies (CA_SLAB_WHOLE / FREE).
322
335
 
323
- "slab_axes_buf" rather than "slab_axes" to avoid colliding with
324
- the existing `int8_t *axes` user-input pointer field above.
336
+ The slab axis list is "slab_axes_buf" (an inline array); the axes
337
+ the caller passes to init are not kept in the state.
325
338
 
326
339
  >>> FROZEN author contract (see banner, two-tier freeze): the
327
340
  slab-delivery representation a kernel reads is exactly
@@ -343,10 +356,11 @@ typedef struct {
343
356
  ca_size_t outer_strides[CA_RANK_MAX]; /* per-outer-axis data byte stride */
344
357
  ca_size_t outer_mask_strides[CA_RANK_MAX]; /* per-outer-axis mask element stride */
345
358
 
346
- /* --- Per-fiber contig scratch (PROPOSAL_FIBER_DELIVERY F.1a) ---
359
+ /* --- Per-fiber contig scratch ---
347
360
  For naxes==1 (= per-axis fiber, the catalog CA_FOR_EACH_FIBER target)
348
- in the default fall-through path of next_slab_axes (= Phase A/B alias
349
- + SRC_ATTACH + Phase B.1.5), when slab_strides[0] != bytes the fiber
361
+ on the paths of next_slab_axes that yield a fiber in place (the
362
+ default fall-through and STACK_OUTER_K; ki_fiber_contig_data), when
363
+ slab_strides[0] != bytes the fiber
350
364
  is yielded strided. To honor the catalog contract "data contig
351
365
  delivery", next_slab_axes lazily allocates this scratch on first
352
366
  gather and gathers each fiber here before yielding. Reused across
@@ -364,7 +378,7 @@ typedef struct {
364
378
  ca_size_t fiber_data_scratch_cap;
365
379
  ca_size_t last_data_off;
366
380
 
367
- /* PROPOSAL_FIBER_DELIVERY F.1b: per-fiber contig mask scratch.
381
+ /* Per-fiber contig mask scratch.
368
382
  Symmetric to fiber_data_scratch. When the source carries a mask
369
383
  (= alias_mask != NULL) and slab_mask_strides[0] != 1, the engine
370
384
  gathers the fiber's mask into contig boolean8_t order here so the
@@ -375,7 +389,7 @@ typedef struct {
375
389
  boolean8_t *fiber_mask_scratch;
376
390
  ca_size_t fiber_mask_scratch_cap;
377
391
 
378
- /* --- PROPOSAL_FIBER_PER_SOURCE_PATH F.6.1 substrate ---
392
+ /* --- PER_FIBER_FUSED fields ---
379
393
  When alias_mode == CA_ITER_ALIAS_PER_FIBER_FUSED, next_slab_axes
380
394
  builds a fiber region from outer_idx + fiber_axis and calls
381
395
  ca_xfer_stride(src, ..., GET) into fiber_data_scratch instead of
@@ -387,23 +401,22 @@ typedef struct {
387
401
  ca_size_t fiber_native_strides[CA_RANK_MAX];
388
402
  /* fiber_region_starts[] cached by next_slab_axes BEFORE outer_idx
389
403
  advance so sync_slab can reconstruct the same ca_xfer_stride
390
- region for WRITE PUT. Same hazard pattern as last_data_off
391
- (= F.1a). */
404
+ region for WRITE PUT. The same hazard as last_data_off. */
392
405
  ca_size_t fiber_region_starts[CA_RANK_MAX];
393
406
 
394
- /* --- PROPOSAL_CASTACK_LOOP_INTERCHANGE Vector A rev2 (direct per-
395
- parent ptr access) --- */
396
- /* When alias_mode == CA_ITER_ALIAS_STACK, init_l2 attaches all K
397
- parents (= O(1) per entity parent) and caches their ptr aliases
398
- here so next_slab_axes can do K-fold direct memcpy gather without
399
- going through ca_xfer_stride. Owned by iter (xfree in finish).
407
+ /* --- CAStack read from its parents (STACK / STACK_OUTER_K) --- */
408
+ /* When alias_mode is CA_ITER_ALIAS_STACK or STACK_OUTER_K, init_l2
409
+ attaches all K parents (= O(1) per entity parent) and caches their
410
+ ptr aliases here so next_slab_axes reads them without going through
411
+ ca_xfer_stride. Owned by iter (xfree in finish).
400
412
  stack_parent_strides[] are the uniform parent-native byte strides
401
- (= all CAStack parents are uniform shape per MEMO §3.2). */
413
+ (all the parents of a CAStack have one shape). */
402
414
  char **stack_parent_ptrs; /* [n_parents] */
403
415
  int32_t stack_n_parents; /* = ((CAStack *)src)->n_parents */
404
416
  ca_size_t stack_parent_strides[CA_RANK_MAX]; /* parent-space byte strides */
405
417
  /* Parent-space element strides (= 1 byte per cell) for parent mask
406
- addressing; only filled when stack_parent_mask_ptrs != NULL. Caching
418
+ addressing; filled by the STACK_OUTER_K init whether or not the
419
+ stack has a mask. Caching
407
420
  here lets next_slab_axes compute mask_off without downcasting to
408
421
  CAStack (= AC3 layering goal). */
409
422
  ca_size_t stack_parent_mask_strides[CA_RANK_MAX];
@@ -416,7 +429,6 @@ typedef struct {
416
429
  when alias_mode == CA_ITER_ALIAS_STACK_OUTER_K; next_slab_axes uses
417
430
  outer_idx[stack_k_outer_pos] to pick the active parent. */
418
431
  int8_t stack_k_outer_pos;
419
- /* --- pilot/castack-axis0-loop-interchange (2026-06-19) --- */
420
432
  /* CA_ITER_ALIAS_STACK tile cache. Refills TILE fibers (= K cells each)
421
433
  at once via K contig parent reads, then serves the next TILE next_slab
422
434
  calls from L1d-resident buffer. Layout: cache[t][k] so a fiber at
@@ -429,10 +441,9 @@ typedef struct {
429
441
  ca_size_t stack_tile_have; /* fibers actually present in current tile */
430
442
  } ca_iter_state;
431
443
 
432
- /* ---- alias eligibility predicate (proposal §11.3) ------------------- */
444
+ /* ---- alias eligibility predicate ---------------------------------------- */
433
445
 
434
- /* Generalises ca_attach_is_alias (carray_core.c:410, renamed from
435
- ca_attach_is_cheap in T1 step 9.4a) so the alias decision can be
446
+ /* Generalises ca_attach_is_alias (carray_core.c) so the alias decision can be
436
447
  made level-aware. Level is the dispatch level the caller intends
437
448
  to use:
438
449
 
@@ -446,15 +457,17 @@ typedef struct {
446
457
  Alias iff the engine can yield per-outer-axis slabs as
447
458
  parent->ptr + offset with a native stride_bytes argument, with
448
459
  no scratch allocation. Broader than L1: any CAStride-family
449
- view qualifies (the innermost run, even a stride-of-1 of count 1,
460
+ view qualifies by its strides (init_l2 still gathers into scratch
461
+ when the root it folds to lends no memory, which this predicate
462
+ does not look at) (the innermost run, even a stride-of-1 of count 1,
450
463
  defines a valid strided slab). Entity arrays also qualify
451
464
  trivially. Pathological all-strided-no-contig sources still
452
465
  qualify here — the engine yields slab_n=1 with the native step;
453
466
  L1 fallback is an engine-policy choice, not an eligibility one.
454
467
 
455
468
  level == 3 (L3, multi-d kernel)
456
- Not implemented in Phase 1 — falls back to L1 semantics so the predicate
457
- stays well-defined for callers that probe ahead.
469
+ Not implemented — answered as L1 so the predicate stays
470
+ well-defined for callers that probe ahead.
458
471
 
459
472
  Descriptor views (CASelectAxis / CAGrid / CASelect / CAWindow /
460
473
  CAShift) and conversion views (CAFake / CAByteSwap / CABitfield /
@@ -463,13 +476,12 @@ typedef struct {
463
476
  (CA_ITER_SRC_DESCRIPTOR_L2_ALIASABLE); this predicate does not
464
477
  report that case.
465
478
 
466
- ca_attach_is_alias is retained as the level=1 oracle for the Tier A
467
- (PROPOSAL_DELEGATE_COPY_DATA) defer site; callers that already use
468
- it keep working unchanged. New code in the kernel_iterator path
469
- should call ca_iter_can_alias with an explicit level. */
479
+ ca_attach_is_alias stays the level=1 answer for its existing callers;
480
+ new code in the kernel iterator path should call ca_iter_can_alias
481
+ with an explicit level. */
470
482
  int ca_iter_can_alias (void *ap, int level);
471
483
 
472
- /* ---- state machine (proposal §3.2) ---------------------------------- */
484
+ /* ---- state machine ------------------------------------------------------ */
473
485
 
474
486
  /* Initialise `st` for an **L1 (contig kernel)** walk of `src`.
475
487
 
@@ -478,11 +490,12 @@ int ca_iter_can_alias (void *ap, int level);
478
490
  src->ptr, stride implicit = bytes)
479
491
  - CAStride family non-contig: scratch path (ca_copy_data
480
492
  compose-fold gather into a malloc'd buffer)
481
- - descriptor views (CAGrid / CASelect / CAMapping / CAWindow /
482
- CAShift / CASelectAxis): gathered into a scratch buffer
483
- - SRC_ATTACH views (overlays, lazy views, CAObject, CAStack, ...):
484
- iterator-owned scratch filled through ca_xfer_all
485
- - sources the classifier does not recognise: CA_ITER_ERR_NOT_CHEAP
493
+ - descriptor views (CAGrid / CASelect / CAWindow / CAShift /
494
+ CASelectAxis): gathered into a scratch buffer
495
+ - SRC_ATTACH views (overlays, lazy views, CAObject, CAStack, ...,
496
+ and any other view with an attach slot): iterator-owned scratch
497
+ filled through ca_xfer_all
498
+ - a source with no attach slot: CA_ITER_ERR_NOT_CHEAP
486
499
 
487
500
  policy: CA_SLAB_WHOLE only (CA_SLAB_AXES and CA_SLAB_FREE return
488
501
  CA_ITER_ERR_POLICY).
@@ -503,28 +516,31 @@ int ca_iter_state_init_l1 (ca_iter_state *st,
503
516
  /* Initialise `st` for an **L2 (strided kernel)** walk of `src`.
504
517
 
505
518
  Source routing:
506
- - entity / CAStride contig: alias_mode = CONTIG, single slab,
507
- stride = bytes (kernel still receives the explicit stride arg)
508
- - CAStride non-contig: alias_mode = STRIDED, multi-slab walk over
509
- prefix axes, each yield carries native inner stride_bytes. No
510
- scratch.
519
+ - entity / CAStride contig: alias_mode = CONTIG, the slab reads
520
+ the source's own memory
521
+ - CAStride non-contig: alias_mode = STRIDED, the slab reads the
522
+ root through the composed strides. No scratch.
511
523
  - descriptor views: aliased when the innermost axis is a STRIDE
512
- axis, otherwise gathered into scratch (per slab under
513
- CA_SLAB_AXES when a slab axis is an INDEX or SHIFT axis)
524
+ axis, otherwise gathered into scratch (per slab when a slab axis
525
+ is an INDEX or SHIFT axis)
514
526
  - SRC_ATTACH views: iterator-owned scratch filled through
515
- ca_xfer_all and written back the same way
516
- - sources the classifier does not recognise: CA_ITER_ERR_NOT_CHEAP
517
-
518
- policy: CA_SLAB_WHOLE or CA_SLAB_AXES (CA_SLAB_FREE returns
519
- CA_ITER_ERR_POLICY, as do out-of-range or duplicate axes, and
520
- CA_SLAB_AXES over a descriptor view whose descriptor rank
521
- differs from the view's rank).
522
- flags: as init_l1. CA_KERNEL_WRITE is refused with
523
- CA_ITER_ERR_FLAGS when a CA_SLAB_AXES slab axis is an INDEX
524
- or SHIFT axis of a descriptor view.
525
-
526
- Pair with ca_iter_state_next_slab_strided (CA_SLAB_WHOLE) or
527
- ca_iter_state_next_slab_axes (CA_SLAB_AXES). */
527
+ ca_xfer_all, written back with ki_scratch_push_changed (only the
528
+ changed cells). A CAStack may instead be walked through its
529
+ parents, and a single FIBER_CONTIG fiber of a cast / byte-swap
530
+ view through ca_xfer_stride (PER_FIBER_FUSED)
531
+ - a source with no attach slot: CA_ITER_ERR_NOT_CHEAP
532
+
533
+ policy: CA_SLAB_AXES only. CA_SLAB_WHOLE and CA_SLAB_FREE return
534
+ CA_ITER_ERR_POLICY, as do out-of-range or duplicate axes,
535
+ and a descriptor view whose descriptor rank differs from the
536
+ view's rank. The whole array as one contiguous run is L1
537
+ (init_l1 with CA_SLAB_WHOLE); one strided run per innermost
538
+ row is CA_SLAB_AXES with the last axis.
539
+ flags: as init_l1. A CA_KERNEL_WRITE walk over a descriptor view
540
+ whose slab has an INDEX or SHIFT axis goes by the transfer
541
+ walk.
542
+
543
+ Pair with ca_iter_state_next_slab_axes. */
528
544
  int ca_iter_state_init_l2 (ca_iter_state *st,
529
545
  struct _CArray *src,
530
546
  ca_slab_policy_t policy,
@@ -540,11 +556,12 @@ int ca_iter_state_init_l2 (ca_iter_state *st,
540
556
  *out_mask is set to the per-slab boolean8_t mask pointer when the
541
557
  source carries a mask (= ca_has_mask(src)), or NULL otherwise.
542
558
  The mask layout matches the value layout (= same iteration order
543
- and same n). Step 6+: kernels use the CA_FOR_EACH_UNMASKED macro
544
- family (carray.h) to skip masked cells.
559
+ and same n). Kernels use the CA_FOR_EACH_UNMASKED macro family
560
+ (below, in this header) to skip masked cells.
545
561
 
546
- The mask is an input. What it points at is a copy taken when the
547
- walk began, so writing through it changes nothing -- not even under
562
+ The mask is an input. What it points at is a copy -- taken when the
563
+ walk began, or, on the per-fiber fused path, gathered for each fiber
564
+ -- so writing through it changes nothing -- not even under
548
565
  CA_KERNEL_WRITE, where the value half of the same yield is often a
549
566
  live alias. It also cannot express the thing an author would most
550
567
  want it for: an unmasked source yields NULL, so there is nowhere to
@@ -564,19 +581,6 @@ int ca_iter_state_next_slab (ca_iter_state *st,
564
581
  boolean8_t **out_mask,
565
582
  ca_size_t *out_n);
566
583
 
567
- /* Pull the next L2 strided slab. Returns 1 with *out_ptr /
568
- *out_mask / *out_n / *out_stride_bytes when a slab is yielded;
569
- returns 0 when the walk is complete. The kernel walks `*out_n`
570
- elements by stepping `*out_stride_bytes` between consecutive
571
- elements starting at `*out_ptr`. The mask, when non-NULL, is
572
- contiguous whatever the value stride: mask[i] pairs with the value
573
- at ptr + i * stride_bytes. Only valid when init_l2 was used. */
574
- int ca_iter_state_next_slab_strided (ca_iter_state *st,
575
- char **out_ptr,
576
- boolean8_t **out_mask,
577
- ca_size_t *out_n,
578
- ca_size_t *out_stride_bytes);
579
-
580
584
  /* Pull the next CA_SLAB_AXES slab (K-D block). Returns 1 with *out_ptr
581
585
  / *out_mask set to the slab base when a slab is yielded; returns 0
582
586
  when the walk is complete. Only valid when init_l2 was called with
@@ -613,21 +617,39 @@ int ca_iter_state_next_slab_axes (ca_iter_state *st,
613
617
  /* Sync the just-yielded slab back to parent (WRITE path; READ no-op).
614
618
 
615
619
  Caller calls this **unconditionally** after each next_slab /
616
- next_slab_strided + kernel invocation; the alias_mode branch lives
620
+ next_slab_axes + kernel invocation; the alias_mode branch lives
617
621
  inside the state machine so the caller never has to inspect it.
618
622
 
619
623
  Behaviour:
620
624
  - !(flags & CA_KERNEL_WRITE): no-op (READ walk)
621
625
  - per-fiber gather (CA_KERNEL_FIBER_CONTIG on a strided fiber):
622
626
  the fiber is scattered back from its scratch first
623
- - alias path (scratch_ptr == NULL): no-op — kernel wrote to
624
- parent directly through alias_ptr
625
- - scratch path, at L1 or L2: written back through the source —
626
- ca_xfer_all for SRC_ATTACH, ca_sync_data for CAStride, the
627
- descriptor scatter for descriptor views */
627
+ - CAStack walked through its parents: READ only, no-op
628
+ - per-fiber fused (CA_ITER_ALIAS_PER_FIBER_FUSED): the fiber is put
629
+ back through the view with ca_xfer_stride
630
+ - alias path (scratch_ptr == NULL): no-op — kernel wrote through
631
+ alias_ptr. When the alias is into a view root's own buffer, that
632
+ buffer is written back once, by finish (ca_sync_detach)
633
+ - scratch path, at L1 or L2: the cells the kernel changed are sent
634
+ back through the source (ki_scratch_push_changed: ca_xfer_addrs,
635
+ or ca_xfer_all PUT when every cell changed); a write over a
636
+ descriptor view takes this path, opened as a transfer walk
637
+
638
+ A view that shows one root cell more than once (a repeated index, a
639
+ tile, a stack or meld holding the same parent twice): which of the
640
+ values written to its positions the root cell ends with is not
641
+ specified. It depends on the route, and a value equal to the one
642
+ delivered is not sent back. The same holds for a view that reads
643
+ several root cells as one (the mask of a refer to a wider type, which
644
+ is the OR of the cells under it): a cell written with the value it
645
+ was delivered is not sent back, so what its root cells end with is
646
+ not specified. */
628
647
  void ca_iter_state_sync_slab (ca_iter_state *st);
629
648
 
630
- /* Release any resources held by `st` and detach parent. Idempotent:
649
+ /* Release any resources held by `st` and detach parent. On a WRITE
650
+ walk with a root, the root is synced before it is detached
651
+ (ca_sync_detach); a slab still handed out from scratch is dropped, not
652
+ written back (ca_iter_state_close writes it back first). Idempotent:
631
653
  a second call is a no-op. Also safe after a failed init, which holds
632
654
  nothing to release (the call is then not required). */
633
655
  void ca_iter_state_finish (ca_iter_state *st);
@@ -659,11 +681,12 @@ int ca_iter_state_init_l2_paired (ca_iter_state *st,
659
681
 
660
682
  /* Finish a walk as the block macros leave it: when the body left a write
661
683
  walk with `break`, the slab it was in is written back before the walk is
662
- finished. ca_iter_state_finish itself abandons such a slab. */
684
+ finished. ca_iter_state_finish alone drops such a slab when it sits in
685
+ scratch (writes through an alias are already in place). */
663
686
  void ca_iter_state_close (ca_iter_state *st);
664
687
 
665
688
  /* Raise unless rc is CA_ITER_OK. The block macros call this on the value
666
- ca_iter_state_init_l1 / _l2 returned, so an author who never looks at a
689
+ ca_iter_state_init_l2 / _l2_paired returned, so an author who never looks at a
667
690
  return code still hears about a request the iterator would not serve.
668
691
  Returns rc when there is nothing to report, so it composes inside the
669
692
  macros' comma expressions. */
@@ -695,12 +718,14 @@ int ca_iter_check_same_shape (struct _CArray *a, struct _CArray *b);
695
718
  VALUE ca_iter_ensure (int32_t n, ca_iter_state *states,
696
719
  VALUE (*body)(VALUE), VALUE arg);
697
720
 
698
- /* ---- Phase C C.3: kernel author block macros ------------------------
699
- (PROPOSAL_CAPSTONE_PHASE_C.md D3.1 (A) do/while/for + D3.2 (C) 2 kinds)
721
+ /* ---- kernel author block macros -----------------------------------------
700
722
 
701
723
  Wrap init_l2 / next_slab_axes / [sync_slab] / finish in a single
702
724
  block scope so kernel authors don't write lifecycle plumbing.
703
725
 
726
+ For a function in carray that uses each macro, see the catalog in
727
+ docs/authoring/HOW_TO_WRITE_KERNEL.md §0.1.
728
+
704
729
  --- Constraints ---
705
730
 
706
731
  - Author must pre-declare `char *p` and `boolean8_t *m` (or names of
@@ -721,7 +746,8 @@ VALUE ca_iter_ensure (int32_t n, ca_iter_state *states,
721
746
  - `break;` from inside the body exits the loop AND triggers finish
722
747
  correctly (= outer for's "increment" clause runs once on natural
723
748
  exit; `break` from the inner while breaks both). In a write walk
724
- finish writes back the slab the body was in. `return` inside
749
+ the macros close with ca_iter_state_close, which writes back the
750
+ slab the body was in. `return` inside
725
751
  the body LEAKS resources (scratch_ptr, parent attach) — drop to
726
752
  raw API if early return is needed. A body that raises leaks the
727
753
  same way: the engine releases what it holds when the walk itself
@@ -733,14 +759,11 @@ VALUE ca_iter_ensure (int32_t n, ca_iter_state *states,
733
759
  - Macros are not statement-equivalent (= they expand to nested for
734
760
  constructs). Don't follow them with `else` etc. */
735
761
 
736
- /* ---- T1 kernel_iterator mask helper macros (step 6+) ---------------------
737
- These macros let kernel_iterator kernels handle masked sources
762
+ /* ---- mask helper macro ---------------------------------------------------
763
+ This macro lets a kernel handle masked sources
738
764
  uniformly: pass NULL for `mask` if the source has no mask (= treat
739
765
  all cells as unmasked), or a `boolean8_t *` of length n where
740
766
  non-zero entries indicate masked (= "skip this cell").
741
-
742
- PROPOSAL_T1_KERNEL_ITERATOR.md §2.4. GCC statement-expression for
743
- CA_COUNT_UNMASKED is GCC/Clang only (MSVC not in scope).
744
767
  --------------------------------------------------------------------- */
745
768
 
746
769
  #define CA_FOR_EACH_UNMASKED(p, mask, n, body) do { \
@@ -752,31 +775,9 @@ VALUE ca_iter_ensure (int32_t n, ca_iter_state *states,
752
775
  } \
753
776
  } while (0)
754
777
 
755
- #define CA_FOR_EACH_INDEX_UNMASKED(p, mask, n, i, body) do { \
756
- if (mask) { \
757
- for (ca_size_t i = 0; i < (n); i++) if (!(mask)[i]) { body } \
758
- } else { \
759
- for (ca_size_t i = 0; i < (n); i++) { body } \
760
- } \
761
- } while (0)
762
-
763
- #define CA_COUNT_UNMASKED(mask, n) ({ \
764
- ca_size_t _ca_cnt = 0; \
765
- ca_size_t _ca_n = (n); \
766
- if (mask) { \
767
- for (ca_size_t _ca_i = 0; _ca_i < _ca_n; _ca_i++) \
768
- if (!(mask)[_ca_i]) _ca_cnt++; \
769
- } else { \
770
- _ca_cnt = _ca_n; \
771
- } \
772
- _ca_cnt; \
773
- })
774
-
775
- #define CA_MASK_GET(mask, i) ((mask) ? (mask)[i] : 0)
776
-
777
- /* ---- L2 inner-loop macros (step 8+) -----------------------------------
778
- `CA_L2_FOR_EACH(T, ptr, n, stride, p, body)` and its unmasked sibling
779
- wrap the strided callback signature `(ptr, n, stride_bytes)` with a
778
+ /* ---- L2 inner-loop macro ------------------------------------------------
779
+ `CA_L2_FOR_EACH(T, ptr, n, stride, p, body)` wraps the strided
780
+ callback signature `(ptr, n, stride_bytes)` with a
780
781
  `stride == sizeof(T)` fast-path branch. When the runtime stride
781
782
  matches the element size, `body` runs against a `T *p` that the
782
783
  compiler can autovectorise; when it doesn't, `p` is recomputed per
@@ -785,24 +786,21 @@ VALUE ca_iter_ensure (int32_t n, ca_iter_state *states,
785
786
  The split is intentional: L2 strided callbacks are deliberately
786
787
  universal — they accept arbitrary stride and so the compiler cannot
787
788
  prove contig on the kernel-side loop. When the iterator hands a
788
- contig scratch (= descriptor materialise path, stride == bytes), the
789
- universal-dispatch cost (step 5.5 §10.4.5) shows up as an 18-22%
790
- SIMD inhibition on the kernel. These macros let kernel authors recover
791
- the contig autovectorisation without giving up the L2 surface.
789
+ contig scratch (a gathered view, stride == bytes), a loop written for
790
+ any stride was measured 18-22% slower than a contiguous one. This
791
+ macro lets kernel authors recover the contig autovectorisation
792
+ without giving up the L2 surface.
792
793
 
793
794
  This is the standard pattern for L2 kernels: write the body once,
794
- the macro picks the right loop shape. Step 8 verifies the speed-up
795
- empirically; the framing (universal dispatch is the abstraction,
796
- kernel-side macros are the speed knob) is `PROPOSAL_T1_KERNEL_
797
- ITERATOR.md` §13.1.
795
+ the macro picks the right loop shape. The walk stays the same for
796
+ every source; the loop shape is the kernel's knob for speed.
798
797
 
799
798
  Usage:
800
799
  CA_L2_FOR_EACH(double, ptr, n, stride, p, {
801
800
  *p = value; // p is `double *`
802
801
  });
803
802
 
804
- GCC / Clang only (block expressions and per-iteration variable
805
- declarations inside a macro). */
803
+ Plain C99 (a do/while block with for-loop declarations). */
806
804
 
807
805
  #define CA_L2_FOR_EACH(T, ptr, n, stride, p, body) do { \
808
806
  ca_size_t _l2_n = (n); \
@@ -822,39 +820,6 @@ VALUE ca_iter_ensure (int32_t n, ca_iter_state *states,
822
820
  } \
823
821
  } while (0)
824
822
 
825
- #define CA_L2_FOR_EACH_UNMASKED(T, ptr, mask, n, stride, p, body) do { \
826
- ca_size_t _l2_n = (n); \
827
- ca_size_t _l2_s = (stride); \
828
- char *_l2_b = (char *)(ptr); \
829
- boolean8_t *_l2_m = (mask); \
830
- if (_l2_s == sizeof(T)) { \
831
- T *p = (T *)_l2_b; \
832
- if (_l2_m) { \
833
- for (ca_size_t _l2_i = 0; _l2_i < _l2_n; _l2_i++) { \
834
- if (!_l2_m[_l2_i]) { body } \
835
- p++; \
836
- } \
837
- } else { \
838
- for (ca_size_t _l2_i = 0; _l2_i < _l2_n; _l2_i++) { \
839
- body \
840
- p++; \
841
- } \
842
- } \
843
- } else { \
844
- if (_l2_m) { \
845
- for (ca_size_t _l2_i = 0; _l2_i < _l2_n; _l2_i++) { \
846
- T *p = (T *)(_l2_b + _l2_i * _l2_s); \
847
- if (!_l2_m[_l2_i]) { body } \
848
- } \
849
- } else { \
850
- for (ca_size_t _l2_i = 0; _l2_i < _l2_n; _l2_i++) { \
851
- T *p = (T *)(_l2_b + _l2_i * _l2_s); \
852
- body \
853
- } \
854
- } \
855
- } \
856
- } while (0)
857
-
858
823
  /* The slab policy is fixed to CA_SLAB_AXES (the only policy compatible
859
824
  with next_slab_axes); it is hardcoded inside the macro rather than
860
825
  taken as an argument, so block-macro authors never type the policy
@@ -877,15 +842,16 @@ VALUE ca_iter_ensure (int32_t n, ca_iter_state *states,
877
842
  output iter runs WRITE; sync_slab is called on output after each
878
843
  body iteration.
879
844
 
880
- Shape mismatch between ca_in / ca_out is NOT validated by the macro
881
- — caller responsibility (= typically output is `rb_ca_template_with_type`
882
- of input, guaranteeing same shape). Init failure on either iter
883
- raises — see CA_FOR_EACH_SLAB above. */
845
+ ca_in and ca_out must have the same shape (ndim and every dim[k]);
846
+ ca_iter_check_same_shape raises ArgumentError before either walk is
847
+ opened when they do not. Init failure on either iter raises — see
848
+ CA_FOR_EACH_SLAB above. */
884
849
  /* Policy fixed to CA_SLAB_AXES internally — see CA_FOR_EACH_SLAB above. */
885
850
  #define CA_FOR_EACH_SLAB_INOUT(st_in, st_out, ca_in, ca_out, \
886
851
  axes, naxes, \
887
852
  p_in, p_out, m_in, m_out) \
888
853
  for ( int __cafi_init = ( \
854
+ ca_iter_check_same_shape((ca_in), (ca_out)), \
889
855
  ca_iter_check_init( \
890
856
  ca_iter_state_init_l2(&(st_in), (ca_in), CA_SLAB_AXES, \
891
857
  (axes), (naxes), 0)), \
@@ -902,8 +868,7 @@ VALUE ca_iter_ensure (int32_t n, ca_iter_state *states,
902
868
  ca_iter_state_next_slab_axes(&(st_out), &(p_out), &(m_out)); \
903
869
  ca_iter_state_sync_slab(&(st_out)) )
904
870
 
905
- /* ---- PROPOSAL_FIBER_DELIVERY F.2: per-axis fiber catalog macros ----
906
- (rev4 §3 catalog contract)
871
+ /* ---- per-axis fiber macros ----------------------------------------------
907
872
 
908
873
  Author-facing surface for "deliver one contig fiber along `axis` to
909
874
  the kernel". Contig delivery is contract:
@@ -913,7 +878,7 @@ VALUE ca_iter_ensure (int32_t n, ca_iter_state *states,
913
878
  - output (INOUT forms): contig same as data; CA_KERNEL_WRITE auto-set
914
879
 
915
880
  The CA_KERNEL_FIBER_CONTIG flag is auto-set; engine gathers strided
916
- fibers into per-state scratch when slab_strides[0] != bytes (= F.1a/b).
881
+ fibers into per-state scratch when slab_strides[0] != bytes.
917
882
 
918
883
  `axis`, `ca`, `ca_in` and `ca_out` may be evaluated more than once:
919
884
  pass variables, not expressions with side effects. `n` is set to the
@@ -1080,7 +1045,67 @@ VALUE ca_iter_ensure (int32_t n, ca_iter_state *states,
1080
1045
  ca_iter_state_sync_slab(&(st_a)), \
1081
1046
  ca_iter_state_sync_slab(&(st_b)) )
1082
1047
 
1083
- /* ---- Phase D: per-data_type reduction macro suite ----------------------- */
1048
+ /* ---- whole-array block macros -------------------------------------------
1049
+
1050
+ Hand the kernel the whole array as one contiguous run of cells in
1051
+ row-major order: `p` is the first cell and `n` is the number of cells
1052
+ (src->elements). The body runs once.
1053
+
1054
+ CA_FOR_EACH_WHOLE (st, ca, flags, p, n) data only
1055
+ CA_FOR_EACH_WHOLE_MASKED(st, ca, flags, p, n, m) + mask m[0..n-1],
1056
+ NULL when the
1057
+ source has none
1058
+
1059
+ Use them for a kernel that wants every cell in order and no axes: a
1060
+ whole-array scan, a hash of the values, a copy into a library buffer.
1061
+ A reduction that also runs per axis is better written once with
1062
+ CA_FOR_EACH_SLAB over the chosen axes (every axis for the whole array).
1063
+
1064
+ Contiguity is the promise, and it is kept by copying when it has to be:
1065
+ a contiguous array lends its memory, any other source is gathered into
1066
+ the walk's scratch first (a transpose, a strided block, a selection, a
1067
+ converting view). A kernel that can take strides and wants to avoid
1068
+ that copy uses CA_FOR_EACH_SLAB over every axis instead.
1069
+
1070
+ Same rules as CA_FOR_EACH_SLAB and the CA_FOR_EACH_FIBER family:
1071
+ - `p` and the mask cursor are declared by the caller; `n` receives the
1072
+ cell count. `ca` may be evaluated more than once.
1073
+ - Init failure raises (ca_iter_check_init); the body does not run.
1074
+ - The form without MASKED hands out no mask cursor: masked cells are
1075
+ read as values. Pass CA_KERNEL_NO_MASK to refuse a masked source.
1076
+ The mask cursor of the MASKED form is a copy; writing it reaches
1077
+ nothing.
1078
+ - With CA_KERNEL_WRITE the cells the kernel changed are written back
1079
+ when the body ends, including by `break`.
1080
+ - `return` from the body leaks what the walk holds. So does a body
1081
+ that raises: one that can raise -- one that calls back into Ruby --
1082
+ opens the macro inside ca_iter_ensure, with the state in the
1083
+ caller's frame. A write that raises keeps the cells already
1084
+ written where the walk lends the array's memory, and none of them
1085
+ where it gathered a copy.
1086
+ - Not statement-equivalent (nested `for`); no trailing `else`. */
1087
+
1088
+ #define CA_FOR_EACH_WHOLE(st, ca, flags, p, n) \
1089
+ for ( int __cfw_init = (ca_iter_check_init( \
1090
+ ca_iter_state_init_l1(&(st), (ca), CA_SLAB_WHOLE, \
1091
+ NULL, 0, (flags))), \
1092
+ 1); \
1093
+ __cfw_init; \
1094
+ __cfw_init = 0, ca_iter_state_close(&(st)) ) \
1095
+ for ( ; ca_iter_state_next_slab(&(st), &(p), NULL, &(n)); \
1096
+ ca_iter_state_sync_slab(&(st)) )
1097
+
1098
+ #define CA_FOR_EACH_WHOLE_MASKED(st, ca, flags, p, n, m) \
1099
+ for ( int __cfwm_init = (ca_iter_check_init( \
1100
+ ca_iter_state_init_l1(&(st), (ca), CA_SLAB_WHOLE,\
1101
+ NULL, 0, (flags))), \
1102
+ 1); \
1103
+ __cfwm_init; \
1104
+ __cfwm_init = 0, ca_iter_state_close(&(st)) ) \
1105
+ for ( ; ca_iter_state_next_slab(&(st), &(p), &(m), &(n)); \
1106
+ ca_iter_state_sync_slab(&(st)) )
1107
+
1108
+ /* ---- per-data_type reduction macro suite ------------------------------- */
1084
1109
 
1085
1110
  /* CA_SLAB_REDUCE_T(T, ...): generic per-data_type slab reduction. T is the
1086
1111
  element load type (`double`, `float`, `int32_t`, `int64_t`, ...).
@@ -1107,10 +1132,8 @@ VALUE ca_iter_ensure (int32_t n, ca_iter_state *states,
1107
1132
  - p: slab data pointer (char *).
1108
1133
  - m: slab mask pointer (boolean8_t *, may be NULL).
1109
1134
 
1110
- Convenience aliases (defined below): CA_SLAB_REDUCE_F64, _F32, _I32,
1111
- _I64. Use those when the load type is one of the standard four
1112
- numerics; use CA_SLAB_REDUCE_T directly for less common types
1113
- (boolean8_t, int8_t, uint16_t, ...).
1135
+ T is the load type: double, float, int32_t, int64_t, or any other
1136
+ element type (boolean8_t, int8_t, uint16_t, ...).
1114
1137
 
1115
1138
  Mask semantics: when m != NULL, masked cells are skipped (REDUCE is
1116
1139
  not invoked). When m == NULL, every cell contributes.
@@ -1218,9 +1241,9 @@ VALUE ca_iter_ensure (int32_t n, ca_iter_state *states,
1218
1241
  } while (0)
1219
1242
 
1220
1243
  /* Backward-compatible wrapper that hides masked_cnt from kernels that
1221
- don't need it. Existing kernels (sum / mean / min / argmin / ...)
1222
- continue to use CA_SLAB_REDUCE_T unchanged; only mask-policy-aware
1223
- kernels reach for CA_SLAB_REDUCE_T_EX. */
1244
+ don't need it. The generated reductions use the _EX forms (or their
1245
+ SIMD / 8-way variants); CA_SLAB_REDUCE_T is for hand-written kernels
1246
+ that do not need the masked count. */
1224
1247
  #define CA_SLAB_REDUCE_T(T, st, p, m, acc, INIT, REDUCE) do { \
1225
1248
  ca_size_t __sr_throwaway_mc = 0; \
1226
1249
  CA_SLAB_REDUCE_T_EX(T, st, p, m, acc, INIT, REDUCE, __sr_throwaway_mc); \
@@ -1228,24 +1251,18 @@ VALUE ca_iter_ensure (int32_t n, ca_iter_state *states,
1228
1251
  } while (0)
1229
1252
 
1230
1253
  /* ----------------------------------------------------------------------
1231
- * SIMD-licensed reduction variants (SL.1.0 stubs)
1254
+ * SIMD-licensed reduction variants
1232
1255
  *
1233
- * The PLUS/MIN/MAX/STAR/VAR _EX variants below are the SIMD-license
1234
- * vehicle introduced by PROPOSAL_REDUCTION_SIMD_LICENSE. In Phase
1235
- * SL.1.0 they are wired-but-inert: each variant currently forwards to
1236
- * CA_SLAB_REDUCE_T_EX so DSL plumbing (mkkernel reduction_kind:) can
1237
- * be exercised without any kernel behavior change.
1238
- *
1239
- * Subsequent sub-steps (SL.1.1+) will replace each forward with a
1240
- * contig-branch body carrying `#pragma omp simd reduction(<kind>:acc)`
1241
- * (and per-acc pragmas for VAR). The pragma is emitted via the
1256
+ * The PLUS/MIN/MAX/STAR _EX variants below carry, in their contiguous
1257
+ * no-mask branch, `#pragma omp simd reduction(<kind>:acc)`, which
1258
+ * licenses the compiler to reassociate the reduction (results are
1259
+ * epsilon-close, not bit-exact). VAR_EX forwards to PLUS_EX and has no
1260
+ * caller. The pragma is emitted via the
1242
1261
  * _Pragma + _CA_XSTR substitution helpers below so the acc lvalue
1243
1262
  * can be parameterised.
1244
1263
  *
1245
1264
  * The pragma is a no-op on compilers that don't support `-fopenmp-simd`
1246
1265
  * (extconf.rb probe — graceful degradation, code stays correct).
1247
- *
1248
- * Q1-Q6 closure (sparring round 1 2026-06-12): see proposal §5.
1249
1266
  * --------------------------------------------------------------------*/
1250
1267
 
1251
1268
  #define _CA_STR(x) #x
@@ -1255,15 +1272,15 @@ VALUE ca_iter_ensure (int32_t n, ca_iter_state *states,
1255
1272
  #define _CA_SIMD_MAX(var) _Pragma(_CA_XSTR(omp simd reduction(max:var)))
1256
1273
  #define _CA_SIMD_STAR(var) _Pragma(_CA_XSTR(omp simd reduction(*:var)))
1257
1274
 
1258
- /* CA_SLAB_REDUCE_T_PLUS_EX (SL.1.1):
1275
+ /* CA_SLAB_REDUCE_T_PLUS_EX:
1259
1276
  * Same structure as CA_SLAB_REDUCE_T_EX, but the **no-mask + contig**
1260
1277
  * inner loop carries `#pragma omp simd reduction(+:acc)` so clang/gcc
1261
1278
  * are licensed to reassoc the accumulator and emit SIMD reduction
1262
1279
  * sequences. All other branches (masked-contig, non-contig, masked-
1263
- * non-contig) are byte-identical to _EX — proposal §2.2 defers
1264
- * strided/masked SIMD to Phase 2 (output-buffered loop interchange).
1280
+ * non-contig) are byte-identical to _EX: a SIMD reduction there would
1281
+ * need an output-buffered loop interchange.
1265
1282
  *
1266
- * PoC (2026-06-12): N=1M f64 sum 906 us -> 116 us (= 7.8x, 68.8 GB/s).
1283
+ * Measured: N=1M f64 sum 906 us -> 116 us (= 7.8x, 68.8 GB/s).
1267
1284
  * Parity: ε-close (relative error < 2e-16 for f64 sum), bit-exact
1268
1285
  * not guaranteed (= the SIMD license; see guides/devel/11_kernel_iterator.md).
1269
1286
  *
@@ -1363,7 +1380,7 @@ VALUE ca_iter_ensure (int32_t n, ca_iter_state *states,
1363
1380
  (void) idx; \
1364
1381
  } while (0)
1365
1382
 
1366
- /* CA_SLAB_REDUCE_SUM8_EX (variance/stddev regression fix, 2026-07-18):
1383
+ /* CA_SLAB_REDUCE_SUM8_EX:
1367
1384
  * 8-way manual accumulator split for the no-mask + contig inner loop.
1368
1385
  *
1369
1386
  * Why: GCC 11.5 with -march=native (FMA) does NOT split the pragma-simd
@@ -1387,8 +1404,9 @@ VALUE ca_iter_ensure (int32_t n, ca_iter_state *states,
1387
1404
  * branches stay single-accumulator (not the hot path). Position
1388
1405
  * counter `idx` is not tracked (variance REDUCE never uses it). ACC_T
1389
1406
  * is the accumulator C type (double for Pass 1 sum on numeric/bool and
1390
- * Pass 2 M2; complex Pass 1 passes its complex type). Used only by the
1391
- * two_pass_centred emitter in mkkernel.rb.
1407
+ * Pass 2 M2; complex Pass 1 passes its complex type). mkkernel.rb
1408
+ * emits it for every 8-way-eligible _PLUS reduction (sum, mean, and
1409
+ * both passes of variance / stddev).
1392
1410
  */
1393
1411
  #define CA_SLAB_REDUCE_SUM8_EX(T, ACC_T, st, p, m, acc, INIT, EXPR, masked_cnt) do { \
1394
1412
  (acc) = (INIT); \
@@ -1474,9 +1492,8 @@ VALUE ca_iter_ensure (int32_t n, ca_iter_state *states,
1474
1492
  } \
1475
1493
  } while (0)
1476
1494
 
1477
- /* CA_SLAB_REDUCE_MIN8_EX / _MAX8_EX / _STAR8_EX
1478
- * (horizontal extension of the SUM8_EX pattern to standard reducers,
1479
- * 2026-07-19):
1495
+ /* CA_SLAB_REDUCE_MIN8_EX / _MAX8_EX / _STAR8_EX (the SUM8_EX pattern for
1496
+ * the other standard reducers):
1480
1497
  *
1481
1498
  * Same rationale as SUM8_EX — GCC does not split single-accumulator pragma
1482
1499
  * reductions into multiple independent chains on FMA/FADD-latency-bound
@@ -1767,10 +1784,10 @@ VALUE ca_iter_ensure (int32_t n, ca_iter_state *states,
1767
1784
  } \
1768
1785
  } while (0)
1769
1786
 
1770
- /* CA_SLAB_REDUCE_T_MIN_EX (SL.1.2):
1787
+ /* CA_SLAB_REDUCE_T_MIN_EX (emitted by the kernel generator; not author surface):
1771
1788
  * Same structure as PLUS_EX; no-mask + contig inner loop carries
1772
- * `#pragma omp simd reduction(min:acc)`. Author REDUCE expression
1773
- * is `if (v < acc) acc = v` (see :min kernel in mkkernel.rb) which
1789
+ * `#pragma omp simd reduction(min:acc)`. The :min kernel in
1790
+ * mkkernel.rb writes REDUCE as `acc = (v < acc) ? v : acc`, which
1774
1791
  * matches OpenMP `min:` reduction semantics exactly — bit-exact
1775
1792
  * parity preserved (no reassoc license needed; min/max are
1776
1793
  * associative + commutative under `<` / `>`).
@@ -1871,7 +1888,7 @@ VALUE ca_iter_ensure (int32_t n, ca_iter_state *states,
1871
1888
  (void) idx; \
1872
1889
  } while (0)
1873
1890
 
1874
- /* CA_SLAB_REDUCE_T_MAX_EX (SL.1.2): same as MIN_EX with `_CA_SIMD_MAX`. */
1891
+ /* CA_SLAB_REDUCE_T_MAX_EX (generator-only): same as MIN_EX with `_CA_SIMD_MAX`. */
1875
1892
  #define CA_SLAB_REDUCE_T_MAX_EX(T, st, p, m, acc, INIT, REDUCE, masked_cnt) do { \
1876
1893
  (acc) = (INIT); \
1877
1894
  int8_t __srK = (st).slab_ndim; \
@@ -1964,7 +1981,7 @@ VALUE ca_iter_ensure (int32_t n, ca_iter_state *states,
1964
1981
  (void) idx; \
1965
1982
  } while (0)
1966
1983
 
1967
- /* CA_SLAB_REDUCE_T_STAR_EX (SL.1.4):
1984
+ /* CA_SLAB_REDUCE_T_STAR_EX (emitted by the kernel generator; not author surface):
1968
1985
  * Same structure as PLUS_EX; no-mask + contig inner loop carries
1969
1986
  * `#pragma omp simd reduction(*:acc)`. Used by :prod kernel
1970
1987
  * (acc *= v). f64 / f32 multiplication parity is ε-close (same
@@ -2064,51 +2081,16 @@ VALUE ca_iter_ensure (int32_t n, ca_iter_state *states,
2064
2081
  (void) idx; \
2065
2082
  } while (0)
2066
2083
 
2067
- /* No-mask wrappers (hide masked_cnt) — parallel to CA_SLAB_REDUCE_T. */
2068
- #define CA_SLAB_REDUCE_T_PLUS(T, st, p, m, acc, INIT, REDUCE) do { \
2069
- ca_size_t __sr_throwaway_mc = 0; \
2070
- CA_SLAB_REDUCE_T_PLUS_EX(T, st, p, m, acc, INIT, REDUCE, \
2071
- __sr_throwaway_mc); \
2072
- (void) __sr_throwaway_mc; \
2073
- } while (0)
2074
- #define CA_SLAB_REDUCE_T_MIN(T, st, p, m, acc, INIT, REDUCE) do { \
2075
- ca_size_t __sr_throwaway_mc = 0; \
2076
- CA_SLAB_REDUCE_T_MIN_EX(T, st, p, m, acc, INIT, REDUCE, \
2077
- __sr_throwaway_mc); \
2078
- (void) __sr_throwaway_mc; \
2079
- } while (0)
2080
- #define CA_SLAB_REDUCE_T_MAX(T, st, p, m, acc, INIT, REDUCE) do { \
2081
- ca_size_t __sr_throwaway_mc = 0; \
2082
- CA_SLAB_REDUCE_T_MAX_EX(T, st, p, m, acc, INIT, REDUCE, \
2083
- __sr_throwaway_mc); \
2084
- (void) __sr_throwaway_mc; \
2085
- } while (0)
2086
- #define CA_SLAB_REDUCE_T_STAR(T, st, p, m, acc, INIT, REDUCE) do { \
2087
- ca_size_t __sr_throwaway_mc = 0; \
2088
- CA_SLAB_REDUCE_T_STAR_EX(T, st, p, m, acc, INIT, REDUCE, \
2089
- __sr_throwaway_mc); \
2090
- (void) __sr_throwaway_mc; \
2091
- } while (0)
2092
-
2093
- /* Multi-acc variant for variance/stddev (sum + sumsq + cnt).
2094
- *
2095
- * SL.1.3 measurement (2026-06-12) showed VAR_EX is **NOT NEEDED** for
2096
- * variance/stddev: a single `#pragma omp simd reduction(+:acc)` on
2097
- * the primary accumulator (via PLUS_EX dispatch) is sufficient — clang
2098
- * extends SIMD treatment to the dependent `sumsq` updates via idiom
2099
- * recognition (the secondary accumulator is recognised as a derived
2100
- * reduction over `v*v`). variance entity 1-D f64 reached 53.4 GB/s
2101
- * (gate > 40 PASS, 8.2x speedup vs 6.5 baseline) using PLUS_EX.
2102
- *
2103
- * VAR_EX is **retained as a stub** here for future SL.2.x output-
2104
- * buffered work or other multi-acc reductions that don't share the
2105
- * variance pattern (= primary + derived sumsq). The stub forwards
2106
- * to PLUS_EX on acc1; acc2 is zero-initialised but otherwise left
2107
- * to the enclosing REDUCE expression. This is safe because no
2108
- * kernel currently dispatches to VAR_EX (= reduce_macro_suffix never
2109
- * returns "_VAR"; SL.1.0/1.1 reduce DSL has no :var value).
2084
+ /* Multi-acc variant (two accumulators). No kernel uses it: variance /
2085
+ * stddev are computed in two passes (Σx for the mean, then Σ(x-mean)^2),
2086
+ * each a single-accumulator reduction through SUM8_EX / PLUS_EX.
2110
2087
  *
2111
- * Removal candidate if no caller materialises by SL.2 close.
2088
+ * VAR_EX is kept as a stub for a future multi-accumulator reduction
2089
+ * that does not share the variance pattern (primary + derived sumsq).
2090
+ * The stub forwards to PLUS_EX on acc1; acc2 is zero-initialised but
2091
+ * otherwise left to the enclosing REDUCE expression. This is safe
2092
+ * because no kernel dispatches to VAR_EX (reduce_macro_suffix never
2093
+ * returns "_VAR"; the reduce DSL has no :var value).
2112
2094
  */
2113
2095
  #define CA_SLAB_REDUCE_T_VAR_EX(T, st, p, m, acc1, acc2, \
2114
2096
  INIT1, INIT2, REDUCE, masked_cnt) do { \
@@ -2130,9 +2112,10 @@ VALUE ca_iter_ensure (int32_t n, ca_iter_state *states,
2130
2112
  enforced by ca_check_same_shape at dispatcher).
2131
2113
 
2132
2114
  Author-supplied (additional to CA_SLAB_REDUCE_T_EX):
2133
- - T_W: second-operand element C type (load type). Must match
2134
- T in the W.1 framework (mkkernel array_arg: data_type:
2135
- :match_source enforces this). Kept separate in the
2115
+ - T_W: second-operand element C type (load type). The
2116
+ production kernels read the weight as double
2117
+ (mkkernel array_arg: data_type: :promote), so T_W need
2118
+ not match T. Kept separate in the
2136
2119
  macro signature so future heterogeneous-data_type variants
2137
2120
  can plug in without macro change.
2138
2121
  - st_w: second-operand ca_iter_state, positioned on the
@@ -2144,7 +2127,7 @@ VALUE ca_iter_ensure (int32_t n, ca_iter_state *states,
2144
2127
  REDUCE expression binds BOTH `v` (source cell, type T) AND `w` (weights
2145
2128
  cell, type T_W), in addition to `acc` and `idx` from the base macro.
2146
2129
  The mask `m` applies to the SOURCE only (weights mask is overlaid onto
2147
- source mask at dispatcher time, per Q3 (A) legacy parity). */
2130
+ source mask by the dispatcher). */
2148
2131
  #define CA_SLAB_REDUCE_ARRAY_T_EX(T, T_W, st, p, m, st_w, p_w, acc, INIT, REDUCE, masked_cnt) do { \
2149
2132
  (acc) = (INIT); \
2150
2133
  int8_t __sraK = (st).slab_ndim; \
@@ -2251,14 +2234,7 @@ VALUE ca_iter_ensure (int32_t n, ca_iter_state *states,
2251
2234
  (void) idx; \
2252
2235
  } while (0)
2253
2236
 
2254
- #define CA_SLAB_REDUCE_ARRAY_T(T, T_W, st, p, m, st_w, p_w, acc, INIT, REDUCE) do { \
2255
- ca_size_t __sra_throwaway_mc = 0; \
2256
- CA_SLAB_REDUCE_ARRAY_T_EX(T, T_W, st, p, m, st_w, p_w, acc, INIT, REDUCE, \
2257
- __sra_throwaway_mc); \
2258
- (void) __sra_throwaway_mc; \
2259
- } while (0)
2260
-
2261
- /* CA_SLAB_REDUCE_ARRAY_T_PLUS_EX (SL.1.4b):
2237
+ /* CA_SLAB_REDUCE_ARRAY_T_PLUS_EX:
2262
2238
  * Parallel-array PLUS variant of CA_SLAB_REDUCE_ARRAY_T_EX.
2263
2239
  * no-mask + both-contig inner loop carries `#pragma omp simd
2264
2240
  * reduction(+:acc)`. Used by :wsum / :wmean — author REDUCE
@@ -2267,7 +2243,7 @@ VALUE ca_iter_ensure (int32_t n, ca_iter_state *states,
2267
2243
  * reduction-tree merge. Other branches (masked, non-contig)
2268
2244
  * stay identical to _EX.
2269
2245
  *
2270
- * PoC bench (SL.1.4b, 2026-06-12):
2246
+ * Measured:
2271
2247
  * wsum f64 N=1M: 908 us -> ~150 us (= the expected 50-60 GB/s)
2272
2248
  * wmean f64 N=1M: 1212 us -> ~150 us (same path + 1 extra acc)
2273
2249
  */
@@ -2379,23 +2355,6 @@ VALUE ca_iter_ensure (int32_t n, ca_iter_state *states,
2379
2355
  (void) idx; \
2380
2356
  } while (0)
2381
2357
 
2382
- #define CA_SLAB_REDUCE_ARRAY_T_PLUS(T, T_W, st, p, m, st_w, p_w, acc, \
2383
- INIT, REDUCE) do { \
2384
- ca_size_t __sra_throwaway_mc = 0; \
2385
- CA_SLAB_REDUCE_ARRAY_T_PLUS_EX(T, T_W, st, p, m, st_w, p_w, acc, INIT, \
2386
- REDUCE, __sra_throwaway_mc); \
2387
- (void) __sra_throwaway_mc; \
2388
- } while (0)
2389
-
2390
- #define CA_SLAB_REDUCE_F64(st, p, m, acc, INIT, REDUCE) \
2391
- CA_SLAB_REDUCE_T(double, st, p, m, acc, INIT, REDUCE)
2392
- #define CA_SLAB_REDUCE_F32(st, p, m, acc, INIT, REDUCE) \
2393
- CA_SLAB_REDUCE_T(float, st, p, m, acc, INIT, REDUCE)
2394
- #define CA_SLAB_REDUCE_I32(st, p, m, acc, INIT, REDUCE) \
2395
- CA_SLAB_REDUCE_T(int32_t, st, p, m, acc, INIT, REDUCE)
2396
- #define CA_SLAB_REDUCE_I64(st, p, m, acc, INIT, REDUCE) \
2397
- CA_SLAB_REDUCE_T(int64_t, st, p, m, acc, INIT, REDUCE)
2398
-
2399
2358
  /* CA_SLAB_MAP_T(T_IN, T_OUT, ...): generic per-cell transform from
2400
2359
  input slab to output slab, walking both in lockstep. T_IN is the
2401
2360
  input element load type, T_OUT is the output element store type.
@@ -2417,9 +2376,7 @@ VALUE ca_iter_ensure (int32_t n, ca_iter_state *states,
2417
2376
  shape-equal CArrays. Mask handling is **not** done by this macro —
2418
2377
  if your input is masked, see §8 Mask handling in
2419
2378
  docs/authoring/HOW_TO_WRITE_KERNEL.md. Caller is responsible for invoking
2420
- ca_iter_state_sync_slab on the output state after each slab.
2421
-
2422
- Convenience aliases below: CA_SLAB_MAP_F64 (T_IN = T_OUT = double). */
2379
+ ca_iter_state_sync_slab on the output state after each slab. */
2423
2380
  #define CA_SLAB_MAP_T(T_IN, T_OUT, st_in, p_in, st_out, p_out, MAP_EXPR) do { \
2424
2381
  int8_t __mK = (st_in).slab_ndim; \
2425
2382
  int8_t __mOuterK = __mK - 1; \
@@ -2464,9 +2421,6 @@ VALUE ca_iter_ensure (int32_t n, ca_iter_state *states,
2464
2421
  } \
2465
2422
  } while (0)
2466
2423
 
2467
- #define CA_SLAB_MAP_F64(st_in, p_in, st_out, p_out, MAP_EXPR) \
2468
- CA_SLAB_MAP_T(double, double, st_in, p_in, st_out, p_out, MAP_EXPR)
2469
-
2470
2424
  /* CA_SLAB_SCAN_T(T_LOAD, T_OUT, ...): cumulative / prefix-scan walk
2471
2425
  that combines a reduction (running accumulator) with a map (per-cell
2472
2426
  output write). Walks input and output slabs in lockstep; the
@@ -2758,10 +2712,10 @@ VALUE ca_iter_ensure (int32_t n, ca_iter_state *states,
2758
2712
  } while (0)
2759
2713
 
2760
2714
  /* ---- Ruby surface ---------------------------------------------------- */
2761
- /* Called from Init_carray_ext (ruby_carray.c). Registers the smoke
2762
- test stub (CArray.t1_step1_smoke) used by
2763
- spec_ai/test_t1_kernel_iterator_step1.rb. This is step-1 scaffolding
2764
- only; later steps may replace or remove it. */
2715
+ /* Called from Init_carray_ext (ruby_carray.c). Creates the
2716
+ Ractor-local key for the kept scratch buffer, and in a development
2717
+ build (CARRAY_DEV_BUILD) registers the CArray.t1_* smoke entry points
2718
+ the spec_ai kernel iterator tests use. */
2765
2719
  void Init_ca_kernel_iterator (void);
2766
2720
 
2767
2721
  #endif /* CA_KERNEL_ITERATOR_H */