carray 3.0.6 → 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 (277) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +1 -1
  3. data/CHANGELOG.md +7 -1830
  4. data/README.md +62 -35
  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 +12 -15
  11. data/ext/ca_bincmp_dispatch.h +16 -38
  12. data/ext/ca_binop_dispatch.c +35 -0
  13. data/ext/ca_binop_dispatch.h +22 -46
  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 -1504
  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 +92 -165
  28. data/ext/ca_obj_bincmp.c +65 -48
  29. data/ext/ca_obj_binop.c +74 -49
  30. data/ext/ca_obj_bitarray.c +7 -21
  31. data/ext/ca_obj_bitfield.c +93 -26
  32. data/ext/ca_obj_block.c +11 -12
  33. data/ext/ca_obj_byte_swap.c +34 -34
  34. data/ext/ca_obj_const_string.c +464 -304
  35. data/ext/ca_obj_face.c +167 -121
  36. data/ext/ca_obj_face.h +32 -170
  37. data/ext/ca_obj_fake.c +127 -34
  38. data/ext/ca_obj_farray.c +13 -9
  39. data/ext/ca_obj_field.c +17 -13
  40. data/ext/ca_obj_fixlen_string.c +57 -34
  41. data/ext/ca_obj_grid.c +149 -113
  42. data/ext/ca_obj_meld.c +28 -26
  43. data/ext/ca_obj_moncmp.c +10 -21
  44. data/ext/ca_obj_monop.c +88 -73
  45. data/ext/ca_obj_object.c +60 -46
  46. data/ext/ca_obj_record.c +74 -22
  47. data/ext/ca_obj_reduce.c +10 -14
  48. data/ext/ca_obj_refer.c +146 -39
  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 +20 -28
  52. data/ext/ca_obj_select.c +17 -27
  53. data/ext/ca_obj_select_axis.c +35 -42
  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 +33 -29
  57. data/ext/ca_obj_stride.c +99 -62
  58. data/ext/ca_obj_string.c +8 -20
  59. data/ext/ca_obj_tile.c +14 -16
  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 +48 -49
  64. data/ext/ca_obj_window.c +87 -99
  65. data/ext/ca_op_cmplx64.h +4 -0
  66. data/ext/ca_op_ipower.c +97 -27
  67. data/ext/ca_op_powi.h +9 -28
  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 +23 -26
  72. data/ext/ca_sweep_engine.c +92 -26
  73. data/ext/ca_sweep_engine.h +22 -15
  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 +12 -12
  77. data/ext/carray.h +300 -1009
  78. data/ext/carray_access.c +320 -207
  79. data/ext/carray_address_basis.c +128 -59
  80. data/ext/carray_attribute.c +81 -104
  81. data/ext/carray_bincount.c +12 -14
  82. data/ext/carray_broadcast.c +31 -27
  83. data/ext/carray_call_cfunc.c +174 -285
  84. data/ext/carray_call_cfunc.h +36 -28
  85. data/ext/carray_cast.c +1191 -351
  86. data/ext/carray_cast_func.rb +68 -8
  87. data/ext/carray_class.c +6 -11
  88. data/ext/carray_conversion.c +98 -39
  89. data/ext/carray_copy.c +52 -26
  90. data/ext/carray_core.c +286 -132
  91. data/ext/carray_count.c +53 -22
  92. data/ext/carray_element.c +49 -28
  93. data/ext/carray_factorize.c +64 -63
  94. data/ext/carray_generate.c +26 -19
  95. data/ext/carray_histogram.c +9 -11
  96. data/ext/carray_hold.c +7 -9
  97. data/ext/carray_index_classifier.c +127 -230
  98. data/ext/carray_index_classifier.h +11 -10
  99. data/ext/carray_integer_total.c +523 -0
  100. data/ext/carray_internal.h +1364 -156
  101. data/ext/carray_key_windows.c +3 -3
  102. data/ext/carray_lazy.c +37 -37
  103. data/ext/carray_loop.c +27 -23
  104. data/ext/carray_mask.c +235 -150
  105. data/ext/carray_math_kernel.h +23 -47
  106. data/ext/carray_mathfunc.c +10 -3
  107. data/ext/carray_median_percentile.c +106 -100
  108. data/ext/carray_memory_view.c +83 -69
  109. data/ext/carray_operator.c +490 -128
  110. data/ext/carray_order.c +163 -84
  111. data/ext/carray_partition.c +14 -16
  112. data/ext/carray_random.c +48 -42
  113. data/ext/carray_round.c +33 -5
  114. data/ext/carray_scatter.c +57 -39
  115. data/ext/carray_segment.c +42 -46
  116. data/ext/carray_slab.c +77 -51
  117. data/ext/carray_slab.h +44 -33
  118. data/ext/carray_sort.c +37 -31
  119. data/ext/carray_sort_kernel.c +36 -10
  120. data/ext/carray_struct.c +44 -34
  121. data/ext/carray_test.c +131 -81
  122. data/ext/carray_time_text.c +30 -31
  123. data/ext/carray_undef.c +50 -7
  124. data/ext/carray_utils.c +373 -139
  125. data/ext/extconf.rb +24 -17
  126. data/ext/mk_call_cfunc.rb +25 -31
  127. data/ext/mk_pow5_table.rb +2 -1
  128. data/ext/mkkernel.rb +819 -488
  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 +272 -185
  135. data/lib/carray/basics.rb +298 -56
  136. data/lib/carray/bincount_nd.rb +41 -18
  137. data/lib/carray/block_iterator.rb +167 -119
  138. data/lib/carray/boolean_reduce.rb +7 -4
  139. data/lib/carray/categorical.rb +84 -59
  140. data/lib/carray/categorical_iterator.rb +90 -67
  141. data/lib/carray/complex.rb +61 -41
  142. data/lib/carray/conditional.rb +98 -23
  143. data/lib/carray/const_string.rb +112 -46
  144. data/lib/carray/core_extensions.rb +20 -69
  145. data/lib/carray/distance_limit.rb +7 -5
  146. data/lib/carray/factories.rb +269 -0
  147. data/lib/carray/fixlen_string.rb +95 -12
  148. data/lib/carray/frame/column_verbs.rb +76 -9
  149. data/lib/carray/frame/concat.rb +51 -7
  150. data/lib/carray/frame/convert.rb +2 -1
  151. data/lib/carray/frame/csv_parser.rb +20 -5
  152. data/lib/carray/frame/frame.rb +73 -31
  153. data/lib/carray/frame/group.rb +247 -187
  154. data/lib/carray/frame/io.rb +36 -12
  155. data/lib/carray/frame/join.rb +56 -17
  156. data/lib/carray/frame/records.rb +7 -5
  157. data/lib/carray/frame/reshape.rb +27 -7
  158. data/lib/carray/frame/sort.rb +10 -8
  159. data/lib/carray/frame/verbs.rb +217 -134
  160. data/lib/carray/frame/windows.rb +254 -19
  161. data/lib/carray/frame.rb +1 -2
  162. data/lib/carray/fuse_source.rb +28 -23
  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 +104 -23
  167. data/lib/carray/key_windows.rb +93 -61
  168. data/lib/carray/lazy.rb +208 -111
  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 +8 -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 +73 -72
  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/factorize.rb +2 -0
  183. data/lib/carray/methods/gather_nd.rb +106 -103
  184. data/lib/carray/methods/insert_block.rb +2 -2
  185. data/lib/carray/methods/is_between.rb +6 -4
  186. data/lib/carray/methods/is_in.rb +183 -143
  187. data/lib/carray/methods/join.rb +2 -1
  188. data/lib/carray/methods/lag.rb +6 -2
  189. data/lib/carray/methods/locate_addr.rb +157 -94
  190. data/lib/carray/methods/mask_duplicates.rb +1 -1
  191. data/lib/carray/methods/meshgrid.rb +28 -16
  192. data/lib/carray/methods/mode.rb +67 -67
  193. data/lib/carray/methods/nunique.rb +9 -7
  194. data/lib/carray/methods/repeat.rb +23 -13
  195. data/lib/carray/methods/resize.rb +1 -1
  196. data/lib/carray/methods/snap.rb +24 -20
  197. data/lib/carray/methods/string_format.rb +2 -2
  198. data/lib/carray/methods/unique.rb +4 -2
  199. data/lib/carray/methods/value_counts.rb +1 -1
  200. data/lib/carray/mkmf.rb +25 -28
  201. data/lib/carray/percentile_arguments.rb +82 -0
  202. data/lib/carray/record_order.rb +50 -20
  203. data/lib/carray/rng.rb +1 -0
  204. data/lib/carray/runtime.rb +195 -17
  205. data/lib/carray/segment_iterator.rb +262 -137
  206. data/lib/carray/serialize.rb +635 -566
  207. data/lib/carray/slab_iterator.rb +114 -114
  208. data/lib/carray/stack.rb +14 -15
  209. data/lib/carray/string.rb +16 -22
  210. data/lib/carray/string_operation_extension.rb +97 -52
  211. data/lib/carray/struct.rb +122 -164
  212. data/lib/carray/struct_builder.rb +247 -73
  213. data/lib/carray/time.rb +663 -365
  214. data/lib/carray/window_iterator.rb +232 -155
  215. data/lib/carray.rb +6 -11
  216. data/yard-stubs/ca_obj_array.rb +20 -135
  217. data/yard-stubs/ca_obj_bitarray.rb +1 -8
  218. data/yard-stubs/ca_obj_bitfield.rb +6 -2
  219. data/yard-stubs/ca_obj_byte_swap.rb +21 -7
  220. data/yard-stubs/ca_obj_fake.rb +3 -1
  221. data/yard-stubs/ca_obj_farray.rb +1 -1
  222. data/yard-stubs/ca_obj_field.rb +8 -6
  223. data/yard-stubs/ca_obj_refer.rb +10 -5
  224. data/yard-stubs/ca_obj_roll.rb +7 -6
  225. data/yard-stubs/ca_obj_shift.rb +2 -3
  226. data/yard-stubs/ca_obj_stride.rb +5 -7
  227. data/yard-stubs/ca_obj_tile.rb +1 -1
  228. data/yard-stubs/ca_obj_time.rb +43 -0
  229. data/yard-stubs/ca_obj_transpose.rb +1 -1
  230. data/yard-stubs/ca_obj_window.rb +28 -16
  231. data/yard-stubs/carray_access.rb +55 -6
  232. data/yard-stubs/carray_attribute.rb +26 -38
  233. data/yard-stubs/carray_cast.rb +121 -162
  234. data/yard-stubs/carray_class.rb +3 -4
  235. data/yard-stubs/carray_conversion.rb +6 -7
  236. data/yard-stubs/carray_copy.rb +2 -1
  237. data/yard-stubs/carray_count.rb +10 -5
  238. data/yard-stubs/carray_element.rb +4 -3
  239. data/yard-stubs/carray_limits.rb +100 -0
  240. data/yard-stubs/carray_loop.rb +0 -20
  241. data/yard-stubs/carray_mask.rb +59 -41
  242. data/yard-stubs/carray_math.rb +162 -100
  243. data/yard-stubs/carray_mathfunc.rb +415 -3
  244. data/yard-stubs/carray_median_percentile.rb +29 -11
  245. data/yard-stubs/carray_memory_view.rb +2 -2
  246. data/yard-stubs/carray_order.rb +72 -33
  247. data/yard-stubs/carray_random.rb +6 -6
  248. data/yard-stubs/carray_reduce.rb +353 -0
  249. data/yard-stubs/carray_rng.rb +92 -0
  250. data/yard-stubs/carray_scatter.rb +12 -3
  251. data/yard-stubs/carray_slab.rb +7 -4
  252. data/yard-stubs/carray_sort.rb +7 -6
  253. data/yard-stubs/carray_test.rb +18 -5
  254. data/yard-stubs/carray_undef.rb +44 -5
  255. data/yard-stubs/ruby_carray.rb +75 -40
  256. metadata +63 -25
  257. data/CHANGELOG.v1.md +0 -126
  258. data/ext/carray_build_flags.h +0 -3
  259. data/ext/carray_config.h +0 -28
  260. data/ext/carray_data_type.c +0 -90
  261. data/ext/carray_kernels_bincmp.c +0 -6457
  262. data/ext/carray_kernels_binop.c +0 -31534
  263. data/ext/carray_kernels_init.c +0 -1734
  264. data/ext/carray_kernels_moncmp.c +0 -2104
  265. data/ext/carray_kernels_monop.c +0 -25387
  266. data/ext/carray_kernels_reduce_aggregate.c +0 -27545
  267. data/ext/carray_kernels_reduce_boolean.c +0 -1924
  268. data/ext/carray_kernels_reduce_cumulative.c +0 -11646
  269. data/ext/carray_kernels_reduce_extreme.c +0 -21631
  270. data/ext/carray_kernels_reduce_variance.c +0 -5981
  271. data/ext/carray_kernels_scan.c +0 -4302
  272. data/ext/carray_kernels_search.c +0 -37776
  273. data/ext/carray_kernels_sort.c +0 -10814
  274. data/ext/carray_kernels_triop.c +0 -1864
  275. data/lib/carray/construct.rb +0 -360
  276. data/lib/carray/data_type_extension.rb +0 -261
  277. data/lib/carray/data_type_limits.rb +0 -91
@@ -17,7 +17,6 @@
17
17
  ---------------------------------------------------------------------------- */
18
18
 
19
19
  #include "carray.h"
20
- #include "carray_internal.h" /* ca_lazy_arena_* */
21
20
  #include "ca_sweep_engine.h"
22
21
  #include "ca_for_buffer.h"
23
22
  #include <string.h>
@@ -36,6 +35,28 @@ ca_sweep_shape_str (CArray *ca)
36
35
  return s;
37
36
  }
38
37
 
38
+ /* fsync is the kernel author's string: one '0' (input) or '1' (output) per
39
+ * operand. Any other character would read as neither, and that operand
40
+ * would be neither written back nor given a mask. */
41
+ static void
42
+ ca_sweep_check_fsync (ca_sweep_state_t *st, const char *fallback_label)
43
+ {
44
+ int k_op;
45
+ if ((int) strlen(st->fsync) != st->n_ops) {
46
+ rb_raise(rb_eRuntimeError,
47
+ "[BUG] invalid length of fsync arg in %s (expected %d)",
48
+ st->src_label ? st->src_label : fallback_label, st->n_ops);
49
+ }
50
+ for (k_op = 0; k_op < st->n_ops; k_op++) {
51
+ if (st->fsync[k_op] != '0' && st->fsync[k_op] != '1') {
52
+ rb_raise(rb_eRuntimeError,
53
+ "[BUG] invalid character '%c' in fsync arg \"%s\" in %s "
54
+ "(expected '0' or '1')", st->fsync[k_op], st->fsync,
55
+ st->src_label ? st->src_label : fallback_label);
56
+ }
57
+ }
58
+ }
59
+
39
60
  void
40
61
  ca_sweep_refuse_shapes (CArray *a, CArray *b)
41
62
  {
@@ -60,8 +81,10 @@ ca_sweep_same_shape (CArray *a, CArray *b)
60
81
 
61
82
  /* Pair the operands: a scalar walks with stride 0, every other operand
62
83
  * must have the shape of the first non-scalar one, which is returned (NULL
63
- * when all operands are scalars). Sets stride[] and n_kernel. Allocates
64
- * nothing, so it runs before any operand is acquired. */
84
+ * when all operands are scalars). A scalar OUTPUT would have every cell of
85
+ * the walk written over one cell, so it is refused beside an array. Sets
86
+ * stride[] and n_kernel. Allocates nothing, so it runs before any operand
87
+ * is acquired. */
65
88
  static CArray *
66
89
  ca_sweep_pair_operands (ca_sweep_state_t *st)
67
90
  {
@@ -82,6 +105,14 @@ ca_sweep_pair_operands (ca_sweep_state_t *st)
82
105
  ca_sweep_refuse_shapes(donor, ca);
83
106
  }
84
107
  }
108
+ for (k_op = 0; donor && k_op < st->n_ops; k_op++) {
109
+ if (st->fsync[k_op] == '1' && ca_is_scalar(st->cx[k_op])) {
110
+ rb_raise(rb_eArgError,
111
+ "%s: a scalar output has one cell, and an array of %lld cells "
112
+ "was given as an operand; a scalar output pairs only with "
113
+ "scalars", ca_calling_method_name(), (long long) st->n_kernel);
114
+ }
115
+ }
85
116
  return donor;
86
117
  }
87
118
 
@@ -117,6 +148,29 @@ ca_sweep_any_input_mask (ca_sweep_state_t *st)
117
148
  return 0;
118
149
  }
119
150
 
151
+ /* The whole-buffer scratch for INPUT ca. Object cells read from a lazy
152
+ * operand or a CAObject exist nowhere else until the kernel has used them,
153
+ * and the kernel may call Ruby, so the scratch is held for GC as long as it
154
+ * lives. It is recorded in owned_buf before the hold, which can raise. */
155
+ static char *
156
+ ca_sweep_scratch_alloc (ca_sweep_state_t *st, int k_op)
157
+ {
158
+ CArray *ca = st->cx[k_op];
159
+ st->owned_buf[k_op] = xmalloc(ca->elements * ca->bytes);
160
+ if (ca->data_type == CA_OBJECT) {
161
+ ca_object_cells_clear(st->owned_buf[k_op], ca->elements);
162
+ ca_gc_hold_buffer(st->owned_buf[k_op], ca->elements);
163
+ }
164
+ return st->owned_buf[k_op];
165
+ }
166
+
167
+ static void
168
+ ca_sweep_scratch_free (void *buf)
169
+ {
170
+ ca_gc_release_buffer(buf);
171
+ xfree(buf);
172
+ }
173
+
120
174
  /* Give back everything acquire has taken so far, without syncing: detach
121
175
  * attached operands, free owned scratch (xmalloc on the whole-buffer path,
122
176
  * arena on the chunked one) and the mask buffers. Called when a read
@@ -132,7 +186,7 @@ ca_sweep_abort (ca_sweep_state_t *st, int chunked)
132
186
  ca_detach(st->cx[k_op]);
133
187
  } else if (st->owned_buf[k_op]) {
134
188
  if (chunked) ca_lazy_arena_release(st->owned_buf[k_op]);
135
- else xfree(st->owned_buf[k_op]);
189
+ else ca_sweep_scratch_free(st->owned_buf[k_op]);
136
190
  }
137
191
  st->owned_buf[k_op] = NULL;
138
192
  }
@@ -147,6 +201,21 @@ ca_sweep_abort (ca_sweep_state_t *st, int chunked)
147
201
  }
148
202
  }
149
203
 
204
+ /* The outputs' masks are overwritten by the OR of the input masks. When no
205
+ * input is masked that is all clear, and there is no m0 to carry it: clear
206
+ * them here, before an output is attached, so a view gathers the cleared
207
+ * mask. An output without a mask does not get one. */
208
+ static void
209
+ ca_sweep_clear_output_masks (ca_sweep_state_t *st)
210
+ {
211
+ int k_op;
212
+ for (k_op = 0; k_op < st->n_ops; k_op++) {
213
+ if (st->fsync[k_op] == '1' && ca_has_mask(st->cx[k_op])) {
214
+ ca_clear_mask(st->cx[k_op]);
215
+ }
216
+ }
217
+ }
218
+
150
219
  typedef struct {
151
220
  ca_sweep_state_t *st;
152
221
  int any_input_mask;
@@ -163,6 +232,8 @@ ca_sweep_acquire_body (VALUE arg)
163
232
  ca_sweep_state_t *st = ctx->st;
164
233
  int k_op;
165
234
 
235
+ if (!ctx->any_input_mask) ca_sweep_clear_output_masks(st);
236
+
166
237
  /* Per-operand acquire:
167
238
  * OUTPUT (fsync == '1') -> ca_attach + base = ca->ptr
168
239
  * INPUT alias -> ca_attach + base = ca->ptr
@@ -178,9 +249,7 @@ ca_sweep_acquire_body (VALUE arg)
178
249
  st->attached[k_op] = 1;
179
250
  st->base[k_op] = (char *) ca->ptr;
180
251
  } else {
181
- ca_size_t bytes_total = ca->elements * ca->bytes;
182
- st->owned_buf[k_op] = xmalloc(bytes_total);
183
- st->base[k_op] = st->owned_buf[k_op];
252
+ st->base[k_op] = ca_sweep_scratch_alloc(st, k_op);
184
253
  ca_xfer_all(ca, st->base[k_op], CA_XFER_GET);
185
254
  }
186
255
  }
@@ -209,7 +278,7 @@ ca_sweep_acquire_body (VALUE arg)
209
278
  }
210
279
 
211
280
  /* Overwrite each OUTPUT mask with the iter mask (creating the
212
- OUTPUT mask if it did not exist). No-op when m0 is NULL. */
281
+ OUTPUT mask if it did not exist). */
213
282
  if (st->m0) {
214
283
  for (k_op = 0; k_op < st->n_ops; k_op++) {
215
284
  CArray *ca = st->cx[k_op];
@@ -228,12 +297,7 @@ ca_sweep_acquire (ca_sweep_state_t *st)
228
297
  int k_op;
229
298
  int tag = 0;
230
299
 
231
- if ((int) strlen(st->fsync) != st->n_ops) {
232
- rb_raise(rb_eRuntimeError,
233
- "[BUG] invalid length of fsync arg in %s (expected %d)",
234
- st->src_label ? st->src_label : "ca_sweep_acquire",
235
- st->n_ops);
236
- }
300
+ ca_sweep_check_fsync(st, "ca_sweep_acquire");
237
301
 
238
302
  for (k_op = 0; k_op < st->n_ops; k_op++) {
239
303
  st->base[k_op] = NULL;
@@ -343,7 +407,7 @@ ca_sweep_release (ca_sweep_state_t *st)
343
407
  if (st->attached[k_op]) {
344
408
  ca_detach(st->cx[k_op]);
345
409
  } else if (st->owned_buf[k_op]) {
346
- xfree(st->owned_buf[k_op]);
410
+ ca_sweep_scratch_free(st->owned_buf[k_op]);
347
411
  st->owned_buf[k_op] = NULL;
348
412
  }
349
413
  }
@@ -363,6 +427,8 @@ ca_sweep_acquire_chunked_body (VALUE arg)
363
427
  ca_sweep_state_t *st = ctx->st;
364
428
  int k_op;
365
429
 
430
+ if (!ctx->any_input_mask) ca_sweep_clear_output_masks(st);
431
+
366
432
  /* per-operand acquire:
367
433
  * OUTPUT (fsync == '1') : ca_attach + base_orig = ca->ptr (legitimate)
368
434
  * INPUT alias : ca_attach + base_orig = ca->ptr (zero-copy)
@@ -378,9 +444,14 @@ ca_sweep_acquire_chunked_body (VALUE arg)
378
444
  st->attached[k_op] = 1;
379
445
  st->base_orig[k_op] = (char *) ca->ptr;
380
446
  } else {
381
- /* non-alias non-scalar INPUT: arena scratch sized for chunk_n_max */
382
- ca_size_t scratch_bytes = st->chunk_n_max * ca->bytes;
383
- st->owned_buf[k_op] = (char *) ca_lazy_arena_acquire(scratch_bytes);
447
+ /* non-alias non-scalar INPUT: arena scratch sized for chunk_n_max;
448
+ * the arena marks an object scratch while it is in use */
449
+ if (ca->data_type == CA_OBJECT) {
450
+ st->owned_buf[k_op] = (char *) ca_lazy_arena_acquire_object(st->chunk_n_max);
451
+ } else {
452
+ ca_size_t scratch_bytes = st->chunk_n_max * ca->bytes;
453
+ st->owned_buf[k_op] = (char *) ca_lazy_arena_acquire(scratch_bytes);
454
+ }
384
455
  /* base_orig stays NULL -- per-chunk gather lands the data in
385
456
  * owned_buf[k_op]; base[k_op] will be set to owned_buf[k_op] at
386
457
  * each chunk boundary. */
@@ -424,12 +495,7 @@ ca_sweep_acquire_chunked (ca_sweep_state_t *st)
424
495
  int tag = 0;
425
496
  CArray *shape_donor = NULL;
426
497
 
427
- if ((int) strlen(st->fsync) != st->n_ops) {
428
- rb_raise(rb_eRuntimeError,
429
- "[BUG] invalid length of fsync arg in %s (expected %d)",
430
- st->src_label ? st->src_label : "ca_sweep_acquire_chunked",
431
- st->n_ops);
432
- }
498
+ ca_sweep_check_fsync(st, "ca_sweep_acquire_chunked");
433
499
 
434
500
  for (k_op = 0; k_op < st->n_ops; k_op++) {
435
501
  st->base[k_op] = NULL;
@@ -647,11 +713,11 @@ ca_with_buffer_body_trampoline (VALUE ctx_val)
647
713
  }
648
714
 
649
715
  void
650
- rb_ca_call_with_buffer (VALUE r_ca, int writable,
716
+ rb_ca_call_with_buffer (volatile VALUE r_ca, int writable,
651
717
  ca_with_buffer_body_fn body, void *user_data)
652
718
  {
653
719
  ca_with_buffer_ctx_t ctx;
654
- TypedData_Get_Struct(r_ca, CArray, &carray_data_type, ctx.ca);
720
+ GetCArray(r_ca, ctx.ca);
655
721
  ctx.body_fn = body;
656
722
  ctx.user_data = user_data;
657
723
  ca_attach_window(1, &ctx.ca, writable ? "w" : "r",
@@ -1,11 +1,18 @@
1
1
  /* ---------------------------------------------------------------------------
2
2
  *
3
- * ca_sweep_engine.h -- sweep (xfer_all) author surface engine, shared helper
3
+ * ca_sweep_engine.h -- the engine under call_cfunc, cslab and
4
+ * rb_ca_call_with_buffer
4
5
  *
5
- * Established in PROPOSAL_L0_AUTHOR_SURFACE L0.1 (2026-06-11). Extracts
6
- * the xfer_all-aware lifecycle template previously duplicated 7-times in
7
- * ext/carray_call_cfunc.c (= one body per arity 1..7) into a single shared
8
- * helper, also used by the chunked slab family (ca_call_cslab_*).
6
+ * This header is internal to carray and carries no contract: anything in
7
+ * it may change or go away in any release, 3.x patch releases included.
8
+ * It is not installed (see ext/extconf.rb $INSTALLFILES); it ships in the
9
+ * gem's ext/ directory only because carray is built from it there. An
10
+ * extension uses the entry points above the engine instead: the per-cell
11
+ * ca_call_cfunc_* and per-chunk ca_call_cslab_* families
12
+ * (carray_call_cfunc.h) and rb_ca_call_with_buffer (ca_for_buffer.h).
13
+ *
14
+ * The xfer_all-aware lifecycle shared by ext/carray_call_cfunc.c (one body
15
+ * per arity 1..7) and the chunked slab family (ca_call_cslab_*).
9
16
  *
10
17
  * Lifecycle template:
11
18
  * (1) per-operand acquire
@@ -14,7 +21,8 @@
14
21
  * INPUT (fsync=='0') non-alias -> xmalloc + ca_xfer_all(GET)
15
22
  * (2) operand pairing (scalar collapse; arrays must agree in shape)
16
23
  * (3) mask OR across INPUT operands -> m0 (xmalloc, NULL if none)
17
- * (4) mask propagate to OUTPUT operands (overwrite output->mask)
24
+ * (4) mask propagate to OUTPUT operands (overwrite output->mask; cleared
25
+ * when no INPUT is masked)
18
26
  * (5) caller runs the per-cell loop using base[] + stride[]
19
27
  * (6) per-operand release (reverse order):
20
28
  * sync OUTPUTs, detach attached or xfree owned_buf, xfree m0
@@ -64,7 +72,7 @@ typedef struct ca_sweep_state {
64
72
  ca_size_t n_kernel;
65
73
  const char *src_label;
66
74
 
67
- /* === chunked path fields (L0.1 chunk, PROPOSAL_L0_AUTHOR_SURFACE) === */
75
+ /* === chunked path fields === */
68
76
  /* Used by ca_sweep_acquire_chunked / ca_sweep_next_chunk / ca_sweep_release_chunked
69
77
  * (= the chunked slab family, ca_call_cslab_*). Unused by the whole-materialise path
70
78
  * (ca_sweep_acquire / ca_sweep_release) — cfunc and other whole-buffer callers
@@ -118,9 +126,8 @@ NORETURN(void ca_sweep_refuse_shapes (CArray *a, CArray *b));
118
126
  *
119
127
  * Unlike ca_sweep_acquire / ca_sweep_release (= whole-buffer materialise used
120
128
  * by cfunc), the chunked path keeps INPUT memory peak bounded to chunk
121
- * size (~32KB at f64 by default). AC2 ancestor cascade prevention plus
122
- * memory peak guarantee: shrinking views materialise into chunk scratch
123
- * sized to inner-axis multiples, not full operand size.
129
+ * size (~32KB at f64 by default): shrinking views materialise into chunk
130
+ * scratch sized to inner-axis multiples, not full operand size.
124
131
  *
125
132
  * Usage shape:
126
133
  * ca_sweep_acquire_chunked(&st);
@@ -133,9 +140,9 @@ NORETURN(void ca_sweep_refuse_shapes (CArray *a, CArray *b));
133
140
  * ca_sweep_release_chunked(&st);
134
141
  *
135
142
  * OUTPUT operand semantics:
136
- * OUTPUT is ca_attach'd whole (= self IS write target, legitimate per
137
- * E.7 invariant), and base[k] is rewritten per chunk as base_orig[k] +
138
- * chunk_off * stride[k] so author writes land in the correct region.
143
+ * OUTPUT is ca_attach'd whole (it is the write target), and base[k]
144
+ * is rewritten per chunk as base_orig[k] + chunk_off * stride[k] so
145
+ * author writes land in the correct region.
139
146
  * Final ca_sync runs in release_chunked. Output memory peak = entity
140
147
  * size (= unavoidable; output buffer must exist).
141
148
  *
@@ -160,14 +167,14 @@ void ca_sweep_acquire_chunked (ca_sweep_state_t *st);
160
167
  int ca_sweep_next_chunk (ca_sweep_state_t *st); /* 1 = chunk ready, 0 = done */
161
168
  void ca_sweep_release_chunked (ca_sweep_state_t *st);
162
169
 
163
- /* ===== Chunking helpers (extern-ified from carray_operator.c) ===== */
170
+ /* ===== Chunking helpers ===== */
164
171
  ca_size_t ca_chunk_inner_size (CArray *ca);
165
172
  ca_size_t ca_chunk_compute_n (ca_size_t total, ca_size_t inner,
166
173
  ca_size_t bytes_per_cell);
167
174
  void ca_chunked_gather (CArray *ca, ca_size_t off, ca_size_t n,
168
175
  void *dest);
169
176
 
170
- /* Arena pool (extern-ified earlier by lazy substrate; reused here). */
177
+ /* Arena pool (shared with the lazy substrate). */
171
178
  extern void *ca_lazy_arena_acquire (ca_size_t bytes);
172
179
  extern void ca_lazy_arena_release (void *ptr);
173
180
 
@@ -5,8 +5,6 @@
5
5
  flight" fast path used by xfer_stride (and indirectly by xfer_all via
6
6
  thin-wrapper delegation).
7
7
 
8
- Design and rationale: devel/PROPOSAL_TRANSFORM_FUSED_XFER.md.
9
-
10
8
  ---------------------------------------------------------------------------- */
11
9
 
12
10
  #include "carray.h"
@@ -37,8 +35,7 @@ extern ca_operation_function_t ca_stride_func;
37
35
  - compose_to_root yields a root with live ptr (= entity / attached)
38
36
 
39
37
  Mask: not consulted. Post-xfer-reform data paths are mask-blind by
40
- design; mask is propagated on a separate channel. See
41
- PROPOSAL_TRANSFORM_FUSED_XFER.md for the audit.
38
+ design; mask is propagated on a separate channel.
42
39
 
43
40
  row scratch sizing: stack 16 KB first, ALLOCV (alloca-like) for larger.
44
41
 
@@ -164,15 +161,13 @@ ca_xfer_stride_transform_fused (CArray *view,
164
161
  write root->ptr+soff directly, saving the row_buf allocation and one
165
162
  of the two memory passes per outer row. Transposed access through
166
163
  CATranspose-of-CAFake has inner_stride != parent_bytes and falls
167
- through to the scratch+gather path. See
168
- PROPOSAL_XFER_STRIDE_PER_REGION_GAPS.md. */
164
+ through to the scratch+gather path. */
169
165
  int inner_contig = (inner_stride == parent_bytes);
170
166
 
171
167
  /* A 2D tile-cache + bulk-cast variant of this loop was explored and
172
168
  reverted: the existing per-row walker already streams source through
173
169
  L2/L3, and the tile path loses to per-call cast dispatch overhead
174
- and an extra untranspose pass. See
175
- PROPOSAL_TRANSFORM_FUSED_XFER.md for the rejected-direction record.
170
+ and an extra untranspose pass.
176
171
  A typed (src,dst)-specialised inner loop is the next candidate. */
177
172
 
178
173
  /* ---- Row scratch (parent data_type, inner_count cells) ----
@@ -1,18 +1,25 @@
1
1
  /* ---------------------------------------------------------------------------
2
2
 
3
- ca_triop_dispatch.h
4
-
5
- CATriOp dispatch API — sibling of ca_binop_dispatch.h. Routes a
6
- triop op id (`CA_TRIOP_*`) to the per-data_type kernel and computes
7
- the 3-way input promotion.
8
-
9
- Kernel signature: fn(n, mask, src1, i1, src2, i2, src3, i3, dst, i4)
10
- All 3 operands are cast to the common data_type before the kernel
11
- runs (cast-before invariant, mirrors CABinOp). Output data_type is
12
- always the common data_type (preserve).
13
-
14
- Scope: fma / fms / clip / select (see ext/mkkernel.rb).
15
- None of the currently-defined triops trap on integer zero divisor.
3
+ ca_triop_dispatch.h -- the triop kernel tables and op ids
4
+
5
+ carray.h includes this header through carray_math_kernel.h, which
6
+ declares the kernel type ca_triop_func_t.
7
+
8
+ Kernel signature: fn(n, mask, src1, i1, src2, i2, src3, i3, dst, i4).
9
+ All three operands are cast to their common data type before the kernel
10
+ runs, and the output has that data type. The operations are fma / fms
11
+ / clip / select (see ext/mkkernel.rb); none of them traps on an integer
12
+ zero divisor.
13
+
14
+ For extensions, and fixed for the rest of 3.x: the op-id values
15
+ (CA_TRIOP_*) and the per-operation kernel tables (ca_triop_fma[] ...). A
16
+ table holds one kernel per data type, indexed by the data type code
17
+ (CA_FLOAT64 ...) below CA_NTYPE, which is fixed within 3.x. An extension
18
+ may assign its own ca_triop_func_t to a slot at load time, e.g.
19
+ `ca_triop_fma[CA_FLOAT64] = my_fma;`, and the operation then uses it for
20
+ arrays of that data type.
21
+ The lookup and promotion helpers are carray's own and are declared in
22
+ carray_internal.h, which is not installed.
16
23
 
17
24
  ---------------------------------------------------------------------------- */
18
25
 
@@ -43,23 +50,4 @@ extern ca_triop_func_t ca_triop_fms [CA_NTYPE];
43
50
  extern ca_triop_func_t ca_triop_clip [CA_NTYPE];
44
51
  extern ca_triop_func_t ca_triop_select [CA_NTYPE];
45
52
 
46
- /* ------------------------------------------------------------------- */
47
- /* Dispatch API */
48
- /* ------------------------------------------------------------------- */
49
-
50
- /* Look up the triop kernel for (op_id, common_dt). Returns NULL if
51
- not implemented at the requested data_type. */
52
- ca_triop_func_t ca_triop_kernel_lookup_vvv (uint16_t op_id, int8_t common_dt);
53
-
54
- /* 3-way promotion: common data_type of (dt1, dt2, dt3). For select it is
55
- that of the two branches (dt2, dt3); the condition is converted to it. */
56
- int8_t ca_lazy_promote_triop (uint16_t op_id, int8_t dt1, int8_t dt2, int8_t dt3);
57
-
58
- /* Each operand's cast-target data_type (= common data_type for all three,
59
- the cast-before invariant). */
60
- void ca_triop_kernel_input_data_types (uint16_t op_id,
61
- int8_t dt1, int8_t dt2, int8_t dt3,
62
- int8_t *out_dt1, int8_t *out_dt2,
63
- int8_t *out_dt3);
64
-
65
53
  #endif /* CA_TRIOP_DISPATCH_H */
@@ -30,7 +30,6 @@
30
30
  keep names no column or one past the last. */
31
31
 
32
32
  #include "carray.h"
33
- #include "carray_internal.h"
34
33
  #include "ruby/encoding.h"
35
34
 
36
35
  static int
@@ -106,7 +105,7 @@ ca_csv_utf8_coderange (const unsigned char *s, long n)
106
105
  String as Ruby would cache it, so the Ruby reader's later String#count on
107
106
  the same chunk does not scan it again. */
108
107
  static int
109
- ca_csv_text_coderange (VALUE text, rb_encoding *enc)
108
+ ca_csv_text_coderange (volatile VALUE text, rb_encoding *enc)
110
109
  {
111
110
  int cr = ENC_CODERANGE(text);
112
111
  if ( cr != ENC_CODERANGE_UNKNOWN || enc != rb_utf8_encoding() ) {
@@ -413,6 +412,7 @@ ca_csv_read_body_walk_and_build (VALUE varg)
413
412
  &c->w.ncol) ) {
414
413
  return Qnil;
415
414
  }
415
+ /* gc: c lives in the frame of ca_csv_read_body, which pins its fields */
416
416
  if ( ! ca_csv_walk_records(RSTRING_PTR(c->text), RSTRING_LEN(c->text),
417
417
  RSTRING_PTR(c->vsep), RSTRING_LEN(c->vsep),
418
418
  RSTRING_PTR(c->vquote), RSTRING_LEN(c->vquote), &c->w) ) {
@@ -442,14 +442,12 @@ ca_csv_read_body (VALUE text, VALUE vsep, VALUE vquote, VALUE vncol, VALUE keep,
442
442
  text, vsep, vquote, vncol,
443
443
  { { NULL, 0, 0 }, { NULL, 0, 0 }, Qnil, keep, NULL, 0, 0, 0 }, build
444
444
  };
445
- VALUE out = rb_ensure(ca_csv_read_body_walk_and_build, (VALUE) &c,
446
- ca_csv_read_body_free_cell_ranges, (VALUE) &c);
447
- RB_GC_GUARD(c.w.tail);
448
- return out;
445
+ return rb_ensure(ca_csv_read_body_walk_and_build, (VALUE) &c,
446
+ ca_csv_read_body_free_cell_ranges, (VALUE) &c);
449
447
  }
450
448
 
451
449
  static VALUE
452
- ca_csv_build_string_cells (VALUE text, ca_csv_walk_t *w)
450
+ ca_csv_build_string_cells (volatile VALUE text, ca_csv_walk_t *w)
453
451
  {
454
452
  volatile VALUE out;
455
453
  rb_encoding *enc = rb_enc_get(text);
@@ -467,15 +465,16 @@ ca_csv_build_string_cells (VALUE text, ca_csv_walk_t *w)
467
465
  cell = rb_enc_str_new(s + a, b - a, enc);
468
466
  }
469
467
  else {
468
+ /* gc: w lives in the frame of ca_csv_read_body, which pins w->tail */
470
469
  cell = rb_enc_str_new(RSTRING_PTR(w->tail) + (a - n), b - a, enc);
471
470
  }
472
471
  rb_ary_push(out, cell);
473
472
  }
474
- return rb_ary_new3(3, LONG2NUM(w->ncol), out, LONG2NUM(w->records));
473
+ return rb_ary_new_from_args(3, LONG2NUM(w->ncol), out, LONG2NUM(w->records));
475
474
  }
476
475
 
477
476
  static VALUE
478
- ca_csv_build_const_string_columns (VALUE text, ca_csv_walk_t *w)
477
+ ca_csv_build_const_string_columns (volatile VALUE text, ca_csv_walk_t *w)
479
478
  {
480
479
  volatile VALUE buffer, cols;
481
480
  ca_size_t dim[1];
@@ -487,6 +486,7 @@ ca_csv_build_const_string_columns (VALUE text, ca_csv_walk_t *w)
487
486
  else {
488
487
  buffer = rb_str_buf_new(RSTRING_LEN(text) + RSTRING_LEN(w->tail));
489
488
  rb_str_cat(buffer, RSTRING_PTR(text), RSTRING_LEN(text));
489
+ /* gc: w lives in the frame of ca_csv_read_body, which pins w->tail */
490
490
  rb_str_cat(buffer, RSTRING_PTR(w->tail), RSTRING_LEN(w->tail));
491
491
  rb_enc_copy(buffer, text);
492
492
  }
@@ -498,9 +498,9 @@ ca_csv_build_const_string_columns (VALUE text, ca_csv_walk_t *w)
498
498
  CArray *pe;
499
499
  int64_t *range;
500
500
  boolean8_t *m = NULL;
501
- VALUE col = rb_carray_new(CA_FIXLEN, 1, dim, 2 * sizeof(int64_t), NULL);
501
+ volatile VALUE col = rb_carray_new(CA_FIXLEN, 1, dim, 2 * sizeof(int64_t), NULL);
502
502
  rb_ary_push(cols, col);
503
- TypedData_Get_Struct(col, CArray, &carray_data_type, pe);
503
+ GetCArray(col, pe);
504
504
  range = (int64_t *) pe->ptr;
505
505
  for (i = 0; i < nrow; i++) {
506
506
  int64_t a = w->cells.v[2 * (i * w->width + j)];
@@ -517,7 +517,7 @@ ca_csv_build_const_string_columns (VALUE text, ca_csv_walk_t *w)
517
517
  range[2 * i + 1] = b;
518
518
  }
519
519
  }
520
- return rb_ary_new3(4, LONG2NUM(w->ncol), buffer, cols, LONG2NUM(w->records));
520
+ return rb_ary_new_from_args(4, LONG2NUM(w->ncol), buffer, cols, LONG2NUM(w->records));
521
521
  }
522
522
 
523
523
  static VALUE