carray 3.0.5 → 3.0.7

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 (173) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +343 -0
  3. data/README.md +25 -4
  4. data/carray.gemspec +3 -2
  5. data/ext/ca_axis_dispatch.c +6 -0
  6. data/ext/ca_axis_group.c +7 -7
  7. data/ext/ca_binop_dispatch.c +8 -0
  8. data/ext/ca_binop_dispatch.h +1 -0
  9. data/ext/ca_categorical_iterator.c +14 -14
  10. data/ext/ca_kernel_iterator.c +116 -231
  11. data/ext/ca_kernel_iterator.h +416 -258
  12. data/ext/ca_obj_array.c +4 -4
  13. data/ext/ca_obj_bincmp.c +28 -4
  14. data/ext/ca_obj_binop.c +46 -12
  15. data/ext/ca_obj_bitarray.c +2 -4
  16. data/ext/ca_obj_bitfield.c +88 -4
  17. data/ext/ca_obj_byte_swap.c +4 -4
  18. data/ext/ca_obj_const_string.c +422 -257
  19. data/ext/ca_obj_face.c +73 -33
  20. data/ext/ca_obj_fake.c +114 -13
  21. data/ext/ca_obj_farray.c +7 -1
  22. data/ext/ca_obj_fixlen_string.c +51 -17
  23. data/ext/ca_obj_grid.c +179 -19
  24. data/ext/ca_obj_meld.c +74 -118
  25. data/ext/ca_obj_moncmp.c +9 -1
  26. data/ext/ca_obj_monop.c +100 -36
  27. data/ext/ca_obj_object.c +63 -29
  28. data/ext/ca_obj_reduce.c +17 -5
  29. data/ext/ca_obj_refer.c +162 -23
  30. data/ext/ca_obj_remap.c +69 -10
  31. data/ext/ca_obj_repeat.c +1 -1
  32. data/ext/ca_obj_roll.c +3 -4
  33. data/ext/ca_obj_select.c +12 -7
  34. data/ext/ca_obj_select_axis.c +9 -6
  35. data/ext/ca_obj_shift.c +1 -0
  36. data/ext/ca_obj_stack.c +315 -116
  37. data/ext/ca_obj_stride.c +177 -53
  38. data/ext/ca_obj_string.c +20 -4
  39. data/ext/ca_obj_tile.c +3 -4
  40. data/ext/ca_obj_triop.c +24 -13
  41. data/ext/ca_obj_window.c +23 -15
  42. data/ext/ca_op_ipower.c +71 -23
  43. data/ext/ca_op_powi.h +6 -29
  44. data/ext/ca_sort_kernels.h +3 -0
  45. data/ext/ca_sweep_engine.c +90 -23
  46. data/ext/ca_sweep_engine.h +5 -6
  47. data/ext/caframe_csv_reader.c +2 -2
  48. data/ext/carray.h +5 -2
  49. data/ext/carray_access.c +125 -103
  50. data/ext/carray_address_basis.c +209 -1
  51. data/ext/carray_attribute.c +8 -0
  52. data/ext/carray_broadcast.c +5 -6
  53. data/ext/carray_build_flags.h +1 -1
  54. data/ext/carray_call_cfunc.c +5 -8
  55. data/ext/carray_cast.c +319 -48
  56. data/ext/carray_cast_func.rb +37 -5
  57. data/ext/carray_conversion.c +65 -7
  58. data/ext/carray_copy.c +24 -0
  59. data/ext/carray_core.c +874 -72
  60. data/ext/carray_count.c +28 -9
  61. data/ext/carray_element.c +22 -12
  62. data/ext/carray_factorize.c +11 -11
  63. data/ext/carray_histogram.c +27 -5
  64. data/ext/carray_index_classifier.c +7 -5
  65. data/ext/carray_internal.h +241 -12
  66. data/ext/carray_kernels_bincmp.c +2007 -3
  67. data/ext/carray_kernels_binop.c +25502 -5336
  68. data/ext/carray_kernels_init.c +22 -22
  69. data/ext/carray_kernels_monop.c +10739 -3771
  70. data/ext/carray_kernels_reduce_aggregate.c +4429 -702
  71. data/ext/carray_kernels_reduce_boolean.c +1533 -15
  72. data/ext/carray_kernels_reduce_cumulative.c +1808 -248
  73. data/ext/carray_kernels_reduce_extreme.c +4508 -511
  74. data/ext/carray_kernels_reduce_variance.c +2284 -152
  75. data/ext/carray_kernels_scan.c +240 -240
  76. data/ext/carray_kernels_search.c +4211 -264
  77. data/ext/carray_kernels_sort.c +367 -450
  78. data/ext/carray_lazy.c +63 -1
  79. data/ext/carray_loop.c +10 -3
  80. data/ext/carray_mask.c +247 -149
  81. data/ext/carray_median_percentile.c +17 -12
  82. data/ext/carray_memory_view.c +54 -29
  83. data/ext/carray_operator.c +219 -55
  84. data/ext/carray_order.c +55 -11
  85. data/ext/carray_partition.c +1 -1
  86. data/ext/carray_random.c +5 -2
  87. data/ext/carray_round.c +24 -1
  88. data/ext/carray_scatter.c +4 -0
  89. data/ext/carray_segment.c +1 -1
  90. data/ext/carray_slab.c +110 -25
  91. data/ext/carray_sort.c +7 -4
  92. data/ext/carray_sort_kernel.c +24 -0
  93. data/ext/carray_struct.c +21 -6
  94. data/ext/carray_test.c +154 -53
  95. data/ext/carray_time_text.c +8 -2
  96. data/ext/carray_utils.c +124 -54
  97. data/ext/extconf.rb +11 -0
  98. data/ext/mk_call_cfunc.rb +6 -11
  99. data/ext/mkkernel.rb +383 -121
  100. data/ext/ruby_carray.c +4 -1
  101. data/ext/version.h +4 -4
  102. data/lib/carray/attribute.rb +25 -1
  103. data/lib/carray/autoload_carray.rb +3 -3
  104. data/lib/carray/autoload_method_extension.rb +5 -3
  105. data/lib/carray/axis_group.rb +22 -13
  106. data/lib/carray/basics.rb +27 -9
  107. data/lib/carray/bincount_nd.rb +8 -2
  108. data/lib/carray/block_iterator.rb +65 -24
  109. data/lib/carray/categorical.rb +27 -16
  110. data/lib/carray/categorical_iterator.rb +4 -3
  111. data/lib/carray/complex.rb +2 -2
  112. data/lib/carray/conditional.rb +55 -6
  113. data/lib/carray/const_string.rb +74 -0
  114. data/lib/carray/construct.rb +2 -2
  115. data/lib/carray/core_extensions.rb +19 -68
  116. data/lib/carray/data_type_extension.rb +1 -0
  117. data/lib/carray/fixlen_string.rb +83 -1
  118. data/lib/carray/frame/column_verbs.rb +102 -16
  119. data/lib/carray/frame/concat.rb +50 -6
  120. data/lib/carray/frame/convert.rb +19 -0
  121. data/lib/carray/frame/csv_parser.rb +45 -36
  122. data/lib/carray/frame/describe.rb +4 -2
  123. data/lib/carray/frame/frame.rb +436 -83
  124. data/lib/carray/frame/group.rb +731 -78
  125. data/lib/carray/frame/io.rb +216 -73
  126. data/lib/carray/frame/join.rb +166 -43
  127. data/lib/carray/frame/records.rb +9 -7
  128. data/lib/carray/frame/reshape.rb +109 -11
  129. data/lib/carray/frame/sort.rb +57 -9
  130. data/lib/carray/frame/verbs.rb +448 -60
  131. data/lib/carray/frame/windows.rb +71 -9
  132. data/lib/carray/fuse_source.rb +4 -2
  133. data/lib/carray/inspect.rb +7 -2
  134. data/lib/carray/iterator.rb +40 -0
  135. data/lib/carray/key_windows.rb +10 -4
  136. data/lib/carray/lazy.rb +28 -24
  137. data/lib/carray/mask_gap_fill.rb +37 -17
  138. data/lib/carray/meld_reduce.rb +1 -1
  139. data/lib/carray/methods/bin.rb +3 -0
  140. data/lib/carray/methods/choose.rb +3 -2
  141. data/lib/carray/methods/composition.rb +1 -1
  142. data/lib/carray/methods/factorize.rb +2 -0
  143. data/lib/carray/methods/gather_nd.rb +4 -1
  144. data/lib/carray/methods/is_in.rb +23 -3
  145. data/lib/carray/methods/locate_addr.rb +63 -12
  146. data/lib/carray/methods/meshgrid.rb +3 -1
  147. data/lib/carray/methods/repeat.rb +11 -1
  148. data/lib/carray/record_order.rb +35 -6
  149. data/lib/carray/runtime.rb +57 -8
  150. data/lib/carray/segment_iterator.rb +42 -24
  151. data/lib/carray/serialize.rb +56 -17
  152. data/lib/carray/slab_iterator.rb +1 -1
  153. data/lib/carray/stack.rb +21 -9
  154. data/lib/carray/string.rb +6 -6
  155. data/lib/carray/string_operation_extension.rb +25 -4
  156. data/lib/carray/struct_builder.rb +4 -1
  157. data/lib/carray/time.rb +195 -46
  158. data/lib/carray/window_iterator.rb +64 -16
  159. data/yard-stubs/ca_obj_array.rb +26 -1
  160. data/yard-stubs/ca_obj_bitfield.rb +4 -1
  161. data/yard-stubs/ca_obj_refer.rb +3 -0
  162. data/yard-stubs/ca_obj_window.rb +2 -1
  163. data/yard-stubs/carray_access.rb +4 -2
  164. data/yard-stubs/carray_cast.rb +14 -5
  165. data/yard-stubs/carray_conversion.rb +8 -2
  166. data/yard-stubs/carray_count.rb +6 -3
  167. data/yard-stubs/carray_mask.rb +20 -1
  168. data/yard-stubs/carray_math.rb +36 -16
  169. data/yard-stubs/carray_memory_view.rb +7 -0
  170. data/yard-stubs/carray_order.rb +6 -0
  171. data/yard-stubs/carray_scatter.rb +2 -1
  172. data/yard-stubs/carray_test.rb +23 -7
  173. metadata +17 -3
data/ext/ca_op_ipower.c CHANGED
@@ -1,7 +1,10 @@
1
1
  /* ---------------------------------------------------------------------------
2
2
 
3
- Float/Complex ** Integer integer-exponent fast path via
4
- binary exponentiation (O(log p)).
3
+ Float/Complex ** Integer: the exponent stays an int64 (no cast of the
4
+ array to a wider type), and each cell is computed exactly as `a ** b`
5
+ computes it when b is an array of that integer: the exponents -2 .. 4
6
+ by at most two multiplications or divisions, the rest by pow / powf /
7
+ cpow.
5
8
 
6
9
  Contents:
7
10
  (1) LAZY kernel table `ca_binop_ipower[CA_NTYPE]` — CABinOp dispatch
@@ -11,14 +14,59 @@
11
14
  `rb_ca_pow{,_bang}` dispatch between ipower (Float/Complex **
12
15
  Integer) and mkkernel-generated `rb_ca_power` (general pow).
13
16
 
14
- Both paths share `op_powi_<type>` from ca_op_powi.h (= repeated
15
- squaring, ~1-3 mul/cell for typical p).
17
+ Both paths share `ca_ipow_<type>` below. The exponent is converted
18
+ the way an int64 cell is converted to the base's type (through double
19
+ for float32 / cmplx64), so the answer is the one the general power
20
+ kernel gives for the same exponent held in an array.
16
21
 
17
22
  ---------------------------------------------------------------------------- */
18
23
 
19
24
  #include "carray.h"
20
25
  #include "ca_binop_dispatch.h"
21
- #include "ca_op_powi.h"
26
+ #include <math.h>
27
+ #include <complex.h>
28
+
29
+ /* The exponents -2 .. 4 take at most two roundings, as in the power
30
+ kernel (MkKernel.pow_short). */
31
+ #define CA_IPOW_SHORT(x, p) do { \
32
+ switch ( p ) { \
33
+ case 2: return (x) * (x); \
34
+ case 3: return (x) * (x) * (x); \
35
+ case 4: return ((x) * (x)) * ((x) * (x)); \
36
+ case 1: return (x); \
37
+ case 0: return 1; \
38
+ case -1: return 1 / (x); \
39
+ case -2: return 1 / ((x) * (x)); \
40
+ } \
41
+ } while (0)
42
+
43
+ static inline float32_t
44
+ ca_ipow_float32_t (float32_t x, int64_t p)
45
+ {
46
+ CA_IPOW_SHORT(x, p);
47
+ return powf(x, (float) (double) p);
48
+ }
49
+
50
+ static inline float64_t
51
+ ca_ipow_float64_t (float64_t x, int64_t p)
52
+ {
53
+ CA_IPOW_SHORT(x, p);
54
+ return pow(x, (double) p);
55
+ }
56
+
57
+ static inline cmplx64_t
58
+ ca_ipow_cmplx64_t (cmplx64_t x, int64_t p)
59
+ {
60
+ CA_IPOW_SHORT(x, p);
61
+ return cpow(x, (float) (double) p);
62
+ }
63
+
64
+ static inline cmplx128_t
65
+ ca_ipow_cmplx128_t (cmplx128_t x, int64_t p)
66
+ {
67
+ CA_IPOW_SHORT(x, p);
68
+ return cpow(x, (double) p);
69
+ }
22
70
 
23
71
  /* Defined in ext/carray_kernels.c (mkkernel-generated general pow /
24
72
  cpow binop). Called by rb_ca_pow{,_bang} when the fast path does
@@ -71,7 +119,7 @@ ca_binop_ipower_##T (ca_size_t n, boolean8_t *m, \
71
119
  p1 = q1 + k*i1; \
72
120
  p2 = q2 + k*i2; \
73
121
  p3 = q3 + k*i3; \
74
- *p3 = op_powi_##T(*p1, *p2); \
122
+ *p3 = ca_ipow_##T(*p1, *p2); \
75
123
  } \
76
124
  } \
77
125
  } else { \
@@ -79,7 +127,7 @@ ca_binop_ipower_##T (ca_size_t n, boolean8_t *m, \
79
127
  p1 = q1 + k*i1; \
80
128
  p2 = q2 + k*i2; \
81
129
  p3 = q3 + k*i3; \
82
- *p3 = op_powi_##T(*p1, *p2); \
130
+ *p3 = ca_ipow_##T(*p1, *p2); \
83
131
  } \
84
132
  } \
85
133
  }
@@ -110,62 +158,62 @@ ca_binop_func_t ca_binop_ipower[CA_NTYPE] = {
110
158
 
111
159
  static void
112
160
  ca_ipower_float32_t (ca_size_t n, boolean8_t *m,
113
- char *ptr1, int32_t ipow, char *ptr2)
161
+ char *ptr1, int64_t ipow, char *ptr2)
114
162
  {
115
163
  ca_u_float32_t *p1 = (ca_u_float32_t *) ptr1, *p2 = (ca_u_float32_t *) ptr2;
116
164
  if ( m ) {
117
165
  while (n--) {
118
- if ( ! *m++ ) { *p2 = op_powi_float32_t(*p1, ipow); }
166
+ if ( ! *m++ ) { *p2 = ca_ipow_float32_t(*p1, ipow); }
119
167
  p1++; p2++;
120
168
  }
121
169
  }
122
170
  else
123
- while (n--) { (*p2) = op_powi_float32_t(*p1, ipow); p1++; p2++; }
171
+ while (n--) { (*p2) = ca_ipow_float32_t(*p1, ipow); p1++; p2++; }
124
172
  }
125
173
 
126
174
  static void
127
175
  ca_ipower_float64_t (ca_size_t n, boolean8_t *m,
128
- char *ptr1, int32_t ipow, char *ptr2)
176
+ char *ptr1, int64_t ipow, char *ptr2)
129
177
  {
130
178
  ca_u_float64_t *p1 = (ca_u_float64_t *) ptr1, *p2 = (ca_u_float64_t *) ptr2;
131
179
  if ( m ) {
132
180
  while (n--) {
133
- if ( ! *m++ ) { *p2 = op_powi_float64_t(*p1, ipow); }
181
+ if ( ! *m++ ) { *p2 = ca_ipow_float64_t(*p1, ipow); }
134
182
  p1++; p2++;
135
183
  }
136
184
  }
137
185
  else
138
- while (n--) { (*p2) = op_powi_float64_t(*p1, ipow); p1++; p2++; }
186
+ while (n--) { (*p2) = ca_ipow_float64_t(*p1, ipow); p1++; p2++; }
139
187
  }
140
188
 
141
189
  static void
142
190
  ca_ipower_cmplx64_t (ca_size_t n, boolean8_t *m,
143
- char *ptr1, int32_t ipow, char *ptr2)
191
+ char *ptr1, int64_t ipow, char *ptr2)
144
192
  {
145
193
  ca_u_cmplx64_t *p1 = (ca_u_cmplx64_t *) ptr1, *p2 = (ca_u_cmplx64_t *) ptr2;
146
194
  if ( m ) {
147
195
  while (n--) {
148
- if ( ! *m++ ) { *p2 = op_powi_cmplx64_t(*p1, ipow); }
196
+ if ( ! *m++ ) { *p2 = ca_ipow_cmplx64_t(*p1, ipow); }
149
197
  p1++; p2++;
150
198
  }
151
199
  }
152
200
  else
153
- while (n--) { (*p2) = op_powi_cmplx64_t(*p1, ipow); p1++; p2++; }
201
+ while (n--) { (*p2) = ca_ipow_cmplx64_t(*p1, ipow); p1++; p2++; }
154
202
  }
155
203
 
156
204
  static void
157
205
  ca_ipower_cmplx128_t (ca_size_t n, boolean8_t *m,
158
- char *ptr1, int32_t ipow, char *ptr2)
206
+ char *ptr1, int64_t ipow, char *ptr2)
159
207
  {
160
208
  ca_u_cmplx128_t *p1 = (ca_u_cmplx128_t *) ptr1, *p2 = (ca_u_cmplx128_t *) ptr2;
161
209
  if ( m ) {
162
210
  while (n--) {
163
- if ( ! *m++ ) { *p2 = op_powi_cmplx128_t(*p1, ipow); }
211
+ if ( ! *m++ ) { *p2 = ca_ipow_cmplx128_t(*p1, ipow); }
164
212
  p1++; p2++;
165
213
  }
166
214
  }
167
215
  else
168
- while (n--) { (*p2) = op_powi_cmplx128_t(*p1, ipow); p1++; p2++; }
216
+ while (n--) { (*p2) = ca_ipow_cmplx128_t(*p1, ipow); p1++; p2++; }
169
217
  }
170
218
 
171
219
  /* The data types the ipower kernels cover; checked before any window
@@ -195,10 +243,10 @@ rb_ca_ipower (VALUE self, VALUE other)
195
243
  {
196
244
  volatile VALUE obj;
197
245
  CArray *ca, *co;
198
- int32_t ipow;
246
+ int64_t ipow;
199
247
  boolean8_t *m;
200
248
 
201
- ipow = NUM2INT(other);
249
+ ipow = NUM2LL(other);
202
250
 
203
251
  TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
204
252
 
@@ -252,10 +300,10 @@ static VALUE
252
300
  rb_ca_ipower_bang (VALUE self, VALUE other)
253
301
  {
254
302
  CArray *ca;
255
- int32_t ipow;
303
+ int64_t ipow;
256
304
  boolean8_t *m;
257
305
 
258
- ipow = NUM2INT(other);
306
+ ipow = NUM2LL(other);
259
307
 
260
308
  TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
261
309
 
data/ext/ca_op_powi.h CHANGED
@@ -1,19 +1,11 @@
1
1
  /* ---------------------------------------------------------------------------
2
2
 
3
- ca_op_powi.h -- shared `op_powi_<type>` integer-power helpers
3
+ ca_op_powi.h -- `op_powi_<type>`, an integer to an integer power
4
4
 
5
- Used by:
6
- - ext/carray_kernels.c (generated from ext/mkkernel.rb)
7
- the `power` binop kernel's integer variants call
8
- `op_powi_<type>` to compute integer exponentiation by binary
9
- exponentiation (O(log p) vs pow's general path).
10
- - ext/carray_math.c (hand-written, P.5b.5)
11
- the `ipower` family (rb_ca_ipower, rb_ca_ipower_bang) uses
12
- `op_powi_<type>` for the Float**Integer / Complex**Integer
13
- fast path when invoked via CArray#pow with an Integer rhs.
14
-
15
- Both files include this header independently; each TU gets its own
16
- `static inline` copy with no linker conflicts.
5
+ The `power` binop kernel's integer variants (generated from
6
+ ext/mkkernel.rb) call `op_powi_<type>`, binary exponentiation in the
7
+ integer type. A float or complex base goes through pow / cpow
8
+ instead (ext/ca_op_ipower.c for an Integer exponent).
17
9
 
18
10
  --------------------------------------------------------------------------- */
19
11
 
@@ -27,7 +19,7 @@
27
19
  an evaluator that compiles a kernel body calling op_powi_<type>
28
20
  (CArray.__kernel_helpers__, written into the build by mkkernel.rb). It
29
21
  may use only <stdint.h>, float32_t / float64_t and ca_zerodiv: what such
30
- an evaluator's C declares. The complex instantiations stay outside. */
22
+ an evaluator's C declares. */
31
23
 
32
24
  /* BEGIN kernel helpers */
33
25
 
@@ -85,17 +77,6 @@ op_powi_## type (type x, int64_t p) \
85
77
  return op_powi_mag_## type(x, (uint64_t) p); \
86
78
  }
87
79
 
88
- #define op_powi_fc(type) \
89
- op_powi_magnitude(type) \
90
- static inline type \
91
- op_powi_## type (type x, int64_t p) \
92
- { \
93
- if ( p < 0 ) { \
94
- return 1 / op_powi_mag_## type(x, (uint64_t) 0 - (uint64_t) p); \
95
- } \
96
- return op_powi_mag_## type(x, (uint64_t) p); \
97
- }
98
-
99
80
  op_powi(int8_t)
100
81
  op_powi_u(uint8_t)
101
82
  op_powi(int16_t)
@@ -104,12 +85,8 @@ op_powi(int32_t)
104
85
  op_powi_u(uint32_t)
105
86
  op_powi(int64_t)
106
87
  op_powi_u(uint64_t)
107
- op_powi_fc(float32_t)
108
- op_powi_fc(float64_t)
109
88
 
110
89
  /* END kernel helpers */
111
90
 
112
- op_powi_fc(cmplx64_t)
113
- op_powi_fc(cmplx128_t)
114
91
 
115
92
  #endif /* CA_OP_POWI_H */
@@ -61,6 +61,9 @@ void ca_sort_merge_f64 (double *a, double *aux, ca_size_t n);
61
61
  /* P.3: NaN pre-partition for float data types (Hoare 1-pass, returns finite count). */
62
62
  ca_size_t ca_partition_nan_f32 (float32_t *a, ca_size_t n);
63
63
  ca_size_t ca_partition_nan_f64 (double *a, ca_size_t n);
64
+ /* The same, keeping the order within both regions; scratch holds n values. */
65
+ ca_size_t ca_partition_nan_stable_f32 (float32_t *a, ca_size_t n, float32_t *scratch);
66
+ ca_size_t ca_partition_nan_stable_f64 (double *a, ca_size_t n, double *scratch);
64
67
 
65
68
  /* Pair variant of NaN pre-partition for float argsort kernels. scratch
66
69
  holds n pairs; its contents on return are unspecified. */
@@ -36,6 +36,28 @@ ca_sweep_shape_str (CArray *ca)
36
36
  return s;
37
37
  }
38
38
 
39
+ /* fsync is the kernel author's string: one '0' (input) or '1' (output) per
40
+ * operand. Any other character would read as neither, and that operand
41
+ * would be neither written back nor given a mask. */
42
+ static void
43
+ ca_sweep_check_fsync (ca_sweep_state_t *st, const char *fallback_label)
44
+ {
45
+ int k_op;
46
+ if ((int) strlen(st->fsync) != st->n_ops) {
47
+ rb_raise(rb_eRuntimeError,
48
+ "[BUG] invalid length of fsync arg in %s (expected %d)",
49
+ st->src_label ? st->src_label : fallback_label, st->n_ops);
50
+ }
51
+ for (k_op = 0; k_op < st->n_ops; k_op++) {
52
+ if (st->fsync[k_op] != '0' && st->fsync[k_op] != '1') {
53
+ rb_raise(rb_eRuntimeError,
54
+ "[BUG] invalid character '%c' in fsync arg \"%s\" in %s "
55
+ "(expected '0' or '1')", st->fsync[k_op], st->fsync,
56
+ st->src_label ? st->src_label : fallback_label);
57
+ }
58
+ }
59
+ }
60
+
39
61
  void
40
62
  ca_sweep_refuse_shapes (CArray *a, CArray *b)
41
63
  {
@@ -60,8 +82,10 @@ ca_sweep_same_shape (CArray *a, CArray *b)
60
82
 
61
83
  /* Pair the operands: a scalar walks with stride 0, every other operand
62
84
  * 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. */
85
+ * when all operands are scalars). A scalar OUTPUT would have every cell of
86
+ * the walk written over one cell, so it is refused beside an array. Sets
87
+ * stride[] and n_kernel. Allocates nothing, so it runs before any operand
88
+ * is acquired. */
65
89
  static CArray *
66
90
  ca_sweep_pair_operands (ca_sweep_state_t *st)
67
91
  {
@@ -82,6 +106,14 @@ ca_sweep_pair_operands (ca_sweep_state_t *st)
82
106
  ca_sweep_refuse_shapes(donor, ca);
83
107
  }
84
108
  }
109
+ for (k_op = 0; donor && k_op < st->n_ops; k_op++) {
110
+ if (st->fsync[k_op] == '1' && ca_is_scalar(st->cx[k_op])) {
111
+ rb_raise(rb_eArgError,
112
+ "%s: a scalar output has one cell, and an array of %lld cells "
113
+ "was given as an operand; a scalar output pairs only with "
114
+ "scalars", ca_calling_method_name(), (long long) st->n_kernel);
115
+ }
116
+ }
85
117
  return donor;
86
118
  }
87
119
 
@@ -117,6 +149,29 @@ ca_sweep_any_input_mask (ca_sweep_state_t *st)
117
149
  return 0;
118
150
  }
119
151
 
152
+ /* The whole-buffer scratch for INPUT ca. Object cells read from a lazy
153
+ * operand or a CAObject exist nowhere else until the kernel has used them,
154
+ * and the kernel may call Ruby, so the scratch is held for GC as long as it
155
+ * lives. It is recorded in owned_buf before the hold, which can raise. */
156
+ static char *
157
+ ca_sweep_scratch_alloc (ca_sweep_state_t *st, int k_op)
158
+ {
159
+ CArray *ca = st->cx[k_op];
160
+ st->owned_buf[k_op] = xmalloc(ca->elements * ca->bytes);
161
+ if (ca->data_type == CA_OBJECT) {
162
+ ca_object_cells_clear(st->owned_buf[k_op], ca->elements);
163
+ ca_gc_hold_buffer(st->owned_buf[k_op], ca->elements);
164
+ }
165
+ return st->owned_buf[k_op];
166
+ }
167
+
168
+ static void
169
+ ca_sweep_scratch_free (void *buf)
170
+ {
171
+ ca_gc_release_buffer(buf);
172
+ xfree(buf);
173
+ }
174
+
120
175
  /* Give back everything acquire has taken so far, without syncing: detach
121
176
  * attached operands, free owned scratch (xmalloc on the whole-buffer path,
122
177
  * arena on the chunked one) and the mask buffers. Called when a read
@@ -132,7 +187,7 @@ ca_sweep_abort (ca_sweep_state_t *st, int chunked)
132
187
  ca_detach(st->cx[k_op]);
133
188
  } else if (st->owned_buf[k_op]) {
134
189
  if (chunked) ca_lazy_arena_release(st->owned_buf[k_op]);
135
- else xfree(st->owned_buf[k_op]);
190
+ else ca_sweep_scratch_free(st->owned_buf[k_op]);
136
191
  }
137
192
  st->owned_buf[k_op] = NULL;
138
193
  }
@@ -147,6 +202,21 @@ ca_sweep_abort (ca_sweep_state_t *st, int chunked)
147
202
  }
148
203
  }
149
204
 
205
+ /* The outputs' masks are overwritten by the OR of the input masks. When no
206
+ * input is masked that is all clear, and there is no m0 to carry it: clear
207
+ * them here, before an output is attached, so a view gathers the cleared
208
+ * mask. An output without a mask does not get one. */
209
+ static void
210
+ ca_sweep_clear_output_masks (ca_sweep_state_t *st)
211
+ {
212
+ int k_op;
213
+ for (k_op = 0; k_op < st->n_ops; k_op++) {
214
+ if (st->fsync[k_op] == '1' && ca_has_mask(st->cx[k_op])) {
215
+ ca_clear_mask(st->cx[k_op]);
216
+ }
217
+ }
218
+ }
219
+
150
220
  typedef struct {
151
221
  ca_sweep_state_t *st;
152
222
  int any_input_mask;
@@ -163,6 +233,8 @@ ca_sweep_acquire_body (VALUE arg)
163
233
  ca_sweep_state_t *st = ctx->st;
164
234
  int k_op;
165
235
 
236
+ if (!ctx->any_input_mask) ca_sweep_clear_output_masks(st);
237
+
166
238
  /* Per-operand acquire:
167
239
  * OUTPUT (fsync == '1') -> ca_attach + base = ca->ptr
168
240
  * INPUT alias -> ca_attach + base = ca->ptr
@@ -178,9 +250,7 @@ ca_sweep_acquire_body (VALUE arg)
178
250
  st->attached[k_op] = 1;
179
251
  st->base[k_op] = (char *) ca->ptr;
180
252
  } 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];
253
+ st->base[k_op] = ca_sweep_scratch_alloc(st, k_op);
184
254
  ca_xfer_all(ca, st->base[k_op], CA_XFER_GET);
185
255
  }
186
256
  }
@@ -209,7 +279,7 @@ ca_sweep_acquire_body (VALUE arg)
209
279
  }
210
280
 
211
281
  /* Overwrite each OUTPUT mask with the iter mask (creating the
212
- OUTPUT mask if it did not exist). No-op when m0 is NULL. */
282
+ OUTPUT mask if it did not exist). */
213
283
  if (st->m0) {
214
284
  for (k_op = 0; k_op < st->n_ops; k_op++) {
215
285
  CArray *ca = st->cx[k_op];
@@ -228,12 +298,7 @@ ca_sweep_acquire (ca_sweep_state_t *st)
228
298
  int k_op;
229
299
  int tag = 0;
230
300
 
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
- }
301
+ ca_sweep_check_fsync(st, "ca_sweep_acquire");
237
302
 
238
303
  for (k_op = 0; k_op < st->n_ops; k_op++) {
239
304
  st->base[k_op] = NULL;
@@ -343,7 +408,7 @@ ca_sweep_release (ca_sweep_state_t *st)
343
408
  if (st->attached[k_op]) {
344
409
  ca_detach(st->cx[k_op]);
345
410
  } else if (st->owned_buf[k_op]) {
346
- xfree(st->owned_buf[k_op]);
411
+ ca_sweep_scratch_free(st->owned_buf[k_op]);
347
412
  st->owned_buf[k_op] = NULL;
348
413
  }
349
414
  }
@@ -363,6 +428,8 @@ ca_sweep_acquire_chunked_body (VALUE arg)
363
428
  ca_sweep_state_t *st = ctx->st;
364
429
  int k_op;
365
430
 
431
+ if (!ctx->any_input_mask) ca_sweep_clear_output_masks(st);
432
+
366
433
  /* per-operand acquire:
367
434
  * OUTPUT (fsync == '1') : ca_attach + base_orig = ca->ptr (legitimate)
368
435
  * INPUT alias : ca_attach + base_orig = ca->ptr (zero-copy)
@@ -378,9 +445,14 @@ ca_sweep_acquire_chunked_body (VALUE arg)
378
445
  st->attached[k_op] = 1;
379
446
  st->base_orig[k_op] = (char *) ca->ptr;
380
447
  } 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);
448
+ /* non-alias non-scalar INPUT: arena scratch sized for chunk_n_max;
449
+ * the arena marks an object scratch while it is in use */
450
+ if (ca->data_type == CA_OBJECT) {
451
+ st->owned_buf[k_op] = (char *) ca_lazy_arena_acquire_object(st->chunk_n_max);
452
+ } else {
453
+ ca_size_t scratch_bytes = st->chunk_n_max * ca->bytes;
454
+ st->owned_buf[k_op] = (char *) ca_lazy_arena_acquire(scratch_bytes);
455
+ }
384
456
  /* base_orig stays NULL -- per-chunk gather lands the data in
385
457
  * owned_buf[k_op]; base[k_op] will be set to owned_buf[k_op] at
386
458
  * each chunk boundary. */
@@ -424,12 +496,7 @@ ca_sweep_acquire_chunked (ca_sweep_state_t *st)
424
496
  int tag = 0;
425
497
  CArray *shape_donor = NULL;
426
498
 
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
- }
499
+ ca_sweep_check_fsync(st, "ca_sweep_acquire_chunked");
433
500
 
434
501
  for (k_op = 0; k_op < st->n_ops; k_op++) {
435
502
  st->base[k_op] = NULL;
@@ -2,10 +2,8 @@
2
2
  *
3
3
  * ca_sweep_engine.h -- sweep (xfer_all) author surface engine, shared helper
4
4
  *
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_*).
5
+ * The xfer_all-aware lifecycle shared by ext/carray_call_cfunc.c (one body
6
+ * per arity 1..7) and the chunked slab family (ca_call_cslab_*).
9
7
  *
10
8
  * Lifecycle template:
11
9
  * (1) per-operand acquire
@@ -14,7 +12,8 @@
14
12
  * INPUT (fsync=='0') non-alias -> xmalloc + ca_xfer_all(GET)
15
13
  * (2) operand pairing (scalar collapse; arrays must agree in shape)
16
14
  * (3) mask OR across INPUT operands -> m0 (xmalloc, NULL if none)
17
- * (4) mask propagate to OUTPUT operands (overwrite output->mask)
15
+ * (4) mask propagate to OUTPUT operands (overwrite output->mask; cleared
16
+ * when no INPUT is masked)
18
17
  * (5) caller runs the per-cell loop using base[] + stride[]
19
18
  * (6) per-operand release (reverse order):
20
19
  * sync OUTPUTs, detach attached or xfree owned_buf, xfree m0
@@ -64,7 +63,7 @@ typedef struct ca_sweep_state {
64
63
  ca_size_t n_kernel;
65
64
  const char *src_label;
66
65
 
67
- /* === chunked path fields (L0.1 chunk, PROPOSAL_L0_AUTHOR_SURFACE) === */
66
+ /* === chunked path fields === */
68
67
  /* Used by ca_sweep_acquire_chunked / ca_sweep_next_chunk / ca_sweep_release_chunked
69
68
  * (= the chunked slab family, ca_call_cslab_*). Unused by the whole-materialise path
70
69
  * (ca_sweep_acquire / ca_sweep_release) — cfunc and other whole-buffer callers
@@ -1,4 +1,4 @@
1
- /* CSV body reader for CAFrame.from_csv.
1
+ /* CSV body reader for CAFrame.read_csv.
2
2
 
3
3
  CArray.__csv_read_body_as_string_cells__(text, sep, quote, ncol, keep)
4
4
  -> [ncol, cells, records]
@@ -498,7 +498,7 @@ 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
503
  TypedData_Get_Struct(col, CArray, &carray_data_type, pe);
504
504
  range = (int64_t *) pe->ptr;
data/ext/carray.h CHANGED
@@ -237,7 +237,8 @@ typedef ca_u_float64_t ca_u_double;
237
237
  routines that walk a single ->parent test this flag and instead fold over
238
238
  the parents[] exposed by the CAMultiParent layout convention. */
239
239
  #define CA_FLAG_MULTI_PARENTS 32
240
- #define CA_FLAG_CYCLE_CHECK 64
240
+ /* bit 64 (formerly CA_FLAG_CYCLE_CHECK) is free: a cyclic read is found
241
+ without writing to the array (ca_test_cyclic_check). */
241
242
  #define CA_FLAG_IS_FACE 128
242
243
  /* Face opt-in ordering/search flags.
243
244
  A Face declares that its numeric storage may be descended to for kernel
@@ -1200,7 +1201,9 @@ double OBJ2DBL (VALUE v);
1200
1201
  /* Mask-aware object->numeric parsers: return 1 with *out set on success, or
1201
1202
  0 (no value; caller masks the cell) on parse failure. Used by the
1202
1203
  object->int/float cast loop so an unparseable cell becomes UNDEF instead
1203
- of a silent 0.0 (float) or a raise (int). See ext/carray_cast.c. */
1204
+ of a silent 0.0 (float) or a raise (int). An Integer wider than the C
1205
+ type gives its low bytes (two's complement), as a narrowing C cast does;
1206
+ these parsers do not raise. See ext/carray_cast.c. */
1204
1207
  int ca_obj2dbl_ok (VALUE v, double *out);
1205
1208
  int ca_obj2cmplx_ok (VALUE v, double complex *out);
1206
1209
  int rb_obj2long_ok (VALUE v, long *out);