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_obj_stride.c CHANGED
@@ -183,6 +183,9 @@ free_ca_stride (void *ap)
183
183
  {
184
184
  CAStride *ca = (CAStride *) ap;
185
185
  if (ca == NULL) return;
186
+ if ( ca_test_flag(ca, CA_FLAG_FOLDED) ) {
187
+ ca_fold_forget((CArray *) ca);
188
+ }
186
189
  ca_free(ca->mask);
187
190
  if (ca->_pool) {
188
191
  /* Pool path: one xfree covers dim/strides via the pool buffer,
@@ -278,11 +281,15 @@ ca_stride_compose_through (CAStride *leaf, CAStride *parent,
278
281
 
279
282
  /* Compose each leaf dim's stride.
280
283
  Validity rule: leaf dim k must advance exactly one parent dim
281
- (not cross multiple parent dims) AND stay within that parent
282
- dim's bounds across leaf's full extent *given the base position*.
283
- Otherwise the leaf's traversal would wrap across parent dim
284
- boundaries, which is a non-strided access pattern that cannot
285
- be folded. */
284
+ (not cross multiple parent dims), and every parent dim must stay
285
+ within its bounds across the leaf's full extent *given the base
286
+ position*. Several leaf dims can advance the same parent dim (a block
287
+ of a reshape: rows step 4 parent columns, columns step 1), so the
288
+ bound is on the sum of their extents, kept in lo[] / hi[]. Otherwise
289
+ the leaf's traversal would wrap across parent dim boundaries, which
290
+ is a non-strided access pattern that cannot be folded. */
291
+ ca_size_t lo[CA_RANK_MAX], hi[CA_RANK_MAX];
292
+ for (m = 0; m < parent->ndim; m++) lo[m] = hi[m] = base_idx[m];
286
293
  for (k = 0; k < leaf->ndim; k++) {
287
294
  if (leaf->strides[k] % parent->bytes != 0) return 0;
288
295
  ca_size_t advance = leaf->strides[k] / parent->bytes;
@@ -302,20 +309,15 @@ ca_stride_compose_through (CAStride *leaf, CAStride *parent,
302
309
  }
303
310
  if (advance != 0) return 0;
304
311
  if (nonzero_count > 1) return 0; /* crosses parent dims */
305
- if (nonzero_count == 1) {
306
- /* Final position in parent dim after the full leaf extent.
307
- For a forward step it must stay strictly below dim; for a
308
- backward step it must stay at or above 0. */
309
- ca_size_t final_pos =
310
- base_idx[nonzero_dim] + (leaf->dim[k] - 1) * nonzero_step;
311
- if (nonzero_step > 0) {
312
- if (final_pos >= parent->dim[nonzero_dim]) return 0;
313
- } else {
314
- if (final_pos < 0) return 0;
315
- }
312
+ if (nonzero_count == 1 && leaf->dim[k] > 1) {
313
+ ca_size_t extent = (leaf->dim[k] - 1) * nonzero_step;
314
+ if (extent > 0) hi[nonzero_dim] += extent;
315
+ else lo[nonzero_dim] += extent;
316
316
  }
317
317
  out_strides[k] = composed;
318
318
  }
319
+ for (m = 0; m < parent->ndim; m++)
320
+ if (lo[m] < 0 || hi[m] >= parent->dim[m]) return 0;
319
321
 
320
322
  /* A leaf cell wider than the remainder of its first parent element
321
323
  (CARefer reinterpreting two float64 as one complex128) covers several
@@ -679,14 +681,20 @@ ca_stride_func_xfer_addrs (void *ap, ca_size_t n, ca_size_t *addrs,
679
681
  }
680
682
  }
681
683
  else {
684
+ /* Peel the axes from the innermost: a % dim[k] is the index on
685
+ axis k and a / dim[k] carries the rest outward. The outermost
686
+ axis takes whatever is left, so ndim-1 divisions per address. */
687
+ ca_size_t *dim = ca->dim;
688
+ int8_t ndim = ca->ndim;
682
689
  for ( i = 0; i < n; i++ ) {
683
- ca_size_t idx[CA_RANK_MAX];
690
+ ca_size_t a = addrs[i];
684
691
  ca_size_t off = ebase;
685
- ca_addr2index((CArray *) ca, addrs[i], idx);
686
- for ( k = 0; k < ca->ndim; k++ ) {
687
- off += idx[k] * esteps[k];
692
+ for ( k = ndim - 1; k > 0; k-- ) {
693
+ ca_size_t q = a / dim[k];
694
+ off += ( a - q * dim[k] ) * esteps[k];
695
+ a = q;
688
696
  }
689
- paddrs[i] = off;
697
+ paddrs[i] = off + a * esteps[0];
690
698
  }
691
699
  }
692
700
  ca_xfer_addrs(root, n, paddrs, data, dir);
@@ -821,9 +829,12 @@ ca_stride_func_xfer_stride (void *ap, ca_size_t *starts, ca_size_t *counts,
821
829
 
822
830
  /* A reshape over a 1-D cold root (ndim != root->ndim): the request has no
823
831
  box in the root's single axis, but the root cells it touches lie in one
824
- flat span. Ask the root for that span once and walk the request over
825
- it, as xfer_all does for the whole view. A PUT reads the span first so
826
- the cells between the requested ones go back unchanged. */
832
+ flat span. A GET asks the root for that span once and walks the
833
+ request over it, as xfer_all does for the whole view. A PUT hands the
834
+ root the list of the requested cells' addresses instead: sending the
835
+ span back would write the cells between them too, and a root that
836
+ repeats its cells (CATile) would have a stale copy land after the
837
+ written one. */
827
838
  if (aligned && !root->ptr && ca_func[root->obj_type].xfer_stride
828
839
  && ca->bytes == root->bytes && root->ndim == 1
829
840
  && root_base % root->bytes == 0) {
@@ -836,15 +847,33 @@ ca_stride_func_xfer_stride (void *ap, ca_size_t *starts, ca_size_t *counts,
836
847
  }
837
848
  if ( span_lo >= 0 && span_hi <= root->elements * root->bytes ) {
838
849
  volatile VALUE holder;
839
- ca_size_t rlo = span_lo / root->bytes;
840
- ca_size_t rcount = (span_hi - span_lo) / root->bytes;
841
- ca_size_t rstep = root->bytes;
842
- char *scratch = ALLOCV_N(char, holder, rcount * root->bytes);
843
- ca_xfer_stride(root, &rlo, &rcount, &rstep, scratch, CA_XFER_GET);
844
- ca_xfer_strided_walk(scratch + (root_base - span_lo), ca->bytes, ndim,
845
- counts, root_stride, d, dir);
846
850
  if ( dir == CA_XFER_PUT ) {
847
- ca_xfer_stride(root, &rlo, &rcount, &rstep, scratch, CA_XFER_PUT);
851
+ ca_size_t n = 1, m = 0, idx[CA_RANK_MAX];
852
+ ca_size_t *addrs;
853
+ for (k = 0; k < ndim; k++) {
854
+ n *= counts[k];
855
+ idx[k] = 0;
856
+ }
857
+ addrs = ALLOCV_N(ca_size_t, holder, n);
858
+ /* empty: the slot returns on an empty region at its entry */
859
+ while (1) {
860
+ ca_size_t off = root_base;
861
+ for (k = 0; k < ndim; k++) off += idx[k] * root_stride[k];
862
+ addrs[m++] = off / root->bytes;
863
+ k = ndim - 1;
864
+ while (k >= 0) { if (++idx[k] < counts[k]) break; idx[k] = 0; k--; }
865
+ if (k < 0) break;
866
+ }
867
+ ca_xfer_addrs(root, n, addrs, d, CA_XFER_PUT);
868
+ }
869
+ else {
870
+ ca_size_t rlo = span_lo / root->bytes;
871
+ ca_size_t rcount = (span_hi - span_lo) / root->bytes;
872
+ ca_size_t rstep = root->bytes;
873
+ char *scratch = ALLOCV_N(char, holder, rcount * root->bytes);
874
+ ca_xfer_stride(root, &rlo, &rcount, &rstep, scratch, CA_XFER_GET);
875
+ ca_xfer_strided_walk(scratch + (root_base - span_lo), ca->bytes, ndim,
876
+ counts, root_stride, d, dir);
848
877
  }
849
878
  ALLOCV_END(holder);
850
879
  return;
@@ -1116,12 +1145,50 @@ static int ca_layout_is_contiguous (int8_t ndim, const ca_size_t *dim,
1116
1145
  Entities hold their own buffer. A CAStride-family root is one the fold
1117
1146
  declined to walk through (a reshape over a transpose); its own attach
1118
1147
  folds onward from itself, so the answer is the one for the root it folds
1119
- to. What is left is the
1148
+ to -- unless two of its cells share memory (sliding windows). Its buffer
1149
+ then holds several copies of a cell, and sending the buffer back would
1150
+ let a stale copy overwrite one a view wrote, so it has nothing to lend
1151
+ either. What is left is the
1120
1152
  boundary views that compute or fetch their contents -- CAObject, the lazy
1121
- per-element transforms, whatever a companion gem installed -- and of those,
1122
- only the ones with an xfer_stride slot can answer a region request at all.
1123
- The rest have nothing better than materialising, so they keep doing it once
1124
- under attach rather than once per transfer. */
1153
+ per-element transforms, whatever a companion gem installed. All of them
1154
+ answer a region request: through their xfer_stride slot, or, when a type
1155
+ leaves it out, through the dispatcher's default, which hands the cells'
1156
+ addresses to xfer_addrs. Attaching one instead would produce all of it
1157
+ and write all of it back, so a cell nobody touched would go through the
1158
+ view's conversion both ways. */
1159
+ /* May two cells of this layout land on the same bytes? With the axes in
1160
+ order of stride, none can when each stride clears everything the smaller
1161
+ ones reach. The test is sufficient, not exact: a layout that interleaves
1162
+ its axes answers yes and takes the longer way. */
1163
+ static int
1164
+ ca_layout_cells_may_overlap (int8_t ndim, const ca_size_t *dim,
1165
+ const ca_size_t *strides, ca_size_t bytes)
1166
+ {
1167
+ ca_size_t s[CA_RANK_MAX], d[CA_RANK_MAX];
1168
+ ca_size_t reach = bytes;
1169
+ int n = 0;
1170
+ int8_t k;
1171
+ int i, j;
1172
+
1173
+ for (k = 0; k < ndim; k++) {
1174
+ if ( dim[k] > 1 ) {
1175
+ ca_size_t a = strides[k] < 0 ? -strides[k] : strides[k];
1176
+ for (i = n; i > 0 && s[i-1] > a; i--) {
1177
+ s[i] = s[i-1];
1178
+ d[i] = d[i-1];
1179
+ }
1180
+ s[i] = a;
1181
+ d[i] = dim[k];
1182
+ n++;
1183
+ }
1184
+ }
1185
+ for (j = 0; j < n; j++) {
1186
+ if ( s[j] < reach ) return 1;
1187
+ reach += s[j] * (d[j] - 1);
1188
+ }
1189
+ return 0;
1190
+ }
1191
+
1125
1192
  int
1126
1193
  ca_root_lends_no_memory (void *ap)
1127
1194
  {
@@ -1137,15 +1204,16 @@ ca_root_lends_no_memory (void *ap)
1137
1204
  ca_size_t cs[CA_RANK_MAX];
1138
1205
  ca_size_t base;
1139
1206
  ca_stride_compose_to_root((CAStride *) root, &below, cs, &base);
1140
- return ca_root_lends_no_memory(below);
1207
+ return ca_root_lends_no_memory(below)
1208
+ || ca_layout_cells_may_overlap(root->ndim, root->dim, cs, root->bytes);
1141
1209
  }
1142
- return ca_func[root->obj_type].xfer_stride != NULL;
1210
+ return 1;
1143
1211
  }
1144
1212
 
1145
1213
  /* The same question for the parent of a view that is not itself in the
1146
1214
  CAStride family (CAGrid, ...): whether the array its cells come from --
1147
1215
  the parent, or the root a CAStride parent folds to -- has no memory to
1148
- lend. Like ca_root_lends_no_memory it asks only what those arrays are,
1216
+ lend, or the parent has cells that share memory. Like ca_root_lends_no_memory it asks only what those arrays are,
1149
1217
  so the lifecycle slots can rely on getting the same answer from attach
1150
1218
  to detach. */
1151
1219
  int
@@ -1158,7 +1226,8 @@ ca_parent_lends_no_memory (void *ap)
1158
1226
  ca_size_t cs[CA_RANK_MAX];
1159
1227
  ca_size_t base;
1160
1228
  ca_stride_compose_to_root((CAStride *) parent, &root, cs, &base);
1161
- return ca_root_lends_no_memory(root);
1229
+ return ca_root_lends_no_memory(root)
1230
+ || ca_layout_cells_may_overlap(parent->ndim, parent->dim, cs, parent->bytes);
1162
1231
  }
1163
1232
  return ca_root_lends_no_memory(parent);
1164
1233
  }
@@ -1207,11 +1276,11 @@ ca_stride_func_allocate (void *ap)
1207
1276
  ca->ptr = ca_alloc_cells((CArray *) ca);
1208
1277
  return;
1209
1278
  }
1210
- ca_attach(root);
1211
1279
  if (ca_layout_is_contiguous(ca->ndim, ca->dim, composed_strides, ca->bytes)) {
1280
+ ca_attach(root);
1212
1281
  ca->ptr = root->ptr + composed_base;
1213
1282
  } else {
1214
- ca->ptr = ca_alloc_cells((CArray *) ca);
1283
+ ca->ptr = ca_alloc_cells_then_attach((CArray *) ca, 1, &root);
1215
1284
  }
1216
1285
  }
1217
1286
 
@@ -1246,11 +1315,11 @@ ca_stride_func_attach (void *ap)
1246
1315
  ca->ptr = buf;
1247
1316
  return;
1248
1317
  }
1249
- ca_attach(root);
1250
1318
  if (ca_layout_is_contiguous(ca->ndim, ca->dim, composed_strides, ca->bytes)) {
1319
+ ca_attach(root);
1251
1320
  ca->ptr = root->ptr + composed_base;
1252
1321
  } else {
1253
- ca->ptr = ca_alloc_cells((CArray *) ca);
1322
+ ca->ptr = ca_alloc_cells_then_attach((CArray *) ca, 1, &root);
1254
1323
  ca_stride_xfer_with_layout(ca, 0, root->ptr + composed_base, composed_strides);
1255
1324
  }
1256
1325
  }
@@ -1435,8 +1504,10 @@ ca_stride_func_xfer_all (void *ap, void *data, int dir)
1435
1504
  the request is exact) and ca->bytes == root->bytes (no byte
1436
1505
  reinterpret, so the span endpoints are whole root elements). A wider
1437
1506
  lazy root has no single strided box to ask for, so it takes the
1438
- whole-root fallback below -- correct, just not partial. */
1439
- if ( root->ndim == 1 && ca->bytes == root->bytes ) {
1507
+ whole-root fallback below -- correct, just not partial. Reads only:
1508
+ a write goes to the root as the list of its cells below, since
1509
+ sending the span back would write the cells between them too. */
1510
+ if ( dir == CA_XFER_GET && root->ndim == 1 && ca->bytes == root->bytes ) {
1440
1511
  ca_size_t span_lo = composed_base;
1441
1512
  ca_size_t span_hi = composed_base + ca->bytes;
1442
1513
  int8_t k;
@@ -1453,13 +1524,10 @@ ca_stride_func_xfer_all (void *ap, void *data, int dir)
1453
1524
  char *scratch = ALLOCV_N(char, holder, rcount * root->bytes);
1454
1525
  ca_xfer_stride(root, &rlo, &rcount, &rstep, scratch, CA_XFER_GET);
1455
1526
  ca->ptr = ptr;
1456
- ca_stride_xfer_with_layout(ca, (dir == CA_XFER_PUT) ? 1 : 0,
1527
+ ca_stride_xfer_with_layout(ca, 0,
1457
1528
  scratch + (composed_base - span_lo),
1458
1529
  composed_strides);
1459
1530
  ca->ptr = ptr0;
1460
- if ( dir == CA_XFER_PUT ) {
1461
- ca_xfer_stride(root, &rlo, &rcount, &rstep, scratch, CA_XFER_PUT);
1462
- }
1463
1531
  ALLOCV_END(holder);
1464
1532
  }
1465
1533
  return;
@@ -1585,6 +1653,51 @@ ca_stride_func_xfer_all (void *ap, void *data, int dir)
1585
1653
  }
1586
1654
  }
1587
1655
 
1656
+ /* Whether a layout of `ndim` axes (dims, byte strides, byte base) addresses
1657
+ each cell of a packed array of the same number of cells exactly once:
1658
+ ordered by the size of their strides, the axes longer than one must be
1659
+ packed (the smallest stride is `bytes`, each next one the previous times
1660
+ its axis length), and the lowest address must be 0. Equal counts alone
1661
+ do not say this: a zero or repeated stride covers fewer cells. */
1662
+ static int
1663
+ ca_stride_layout_is_permutation (int8_t ndim, const ca_size_t *dim,
1664
+ const ca_size_t *strides, ca_size_t base,
1665
+ ca_size_t bytes)
1666
+ {
1667
+ int8_t order[CA_RANK_MAX];
1668
+ int8_t n = 0, i, j;
1669
+ ca_size_t lowest = base, expect = bytes;
1670
+
1671
+ for (i=0; i<ndim; i++) {
1672
+ if ( dim[i] <= 1 ) {
1673
+ continue;
1674
+ }
1675
+ if ( strides[i] < 0 ) {
1676
+ lowest += (dim[i] - 1) * strides[i];
1677
+ }
1678
+ for (j=n; j>0; j--) {
1679
+ ca_size_t a = strides[order[j-1]], b = strides[i];
1680
+ if ( (a < 0 ? -a : a) <= (b < 0 ? -b : b) ) {
1681
+ break;
1682
+ }
1683
+ order[j] = order[j-1];
1684
+ }
1685
+ order[j] = i;
1686
+ n++;
1687
+ }
1688
+ if ( lowest != 0 ) {
1689
+ return 0;
1690
+ }
1691
+ for (i=0; i<n; i++) {
1692
+ ca_size_t s = strides[order[i]];
1693
+ if ( (s < 0 ? -s : s) != expect ) {
1694
+ return 0;
1695
+ }
1696
+ expect *= dim[order[i]];
1697
+ }
1698
+ return 1;
1699
+ }
1700
+
1588
1701
  static void
1589
1702
  ca_stride_func_fill_data (void *ap, void *ptr)
1590
1703
  {
@@ -1620,7 +1733,9 @@ ca_stride_func_fill_data (void *ap, void *ptr)
1620
1733
  into scratch only to overwrite every byte with *ptr -- wasted
1621
1734
  work. Delegate to root.fill_data instead.
1622
1735
  Safety gates:
1623
- (1) elements match -- our view covers every root cell;
1736
+ (1) elements match and the composed layout addresses each root cell
1737
+ exactly once -- our view covers every root cell (a zero or
1738
+ repeated stride has as many cells but covers fewer);
1624
1739
  (2) bytes match -- our cell width equals root's cell width.
1625
1740
  Without (2), a type-reinterpreting CAStride (CAField over a
1626
1741
  complex entity for `.real`/`.imag`) would corrupt root: root
@@ -1632,7 +1747,9 @@ ca_stride_func_fill_data (void *ap, void *ptr)
1632
1747
  if ( ca_func[root->obj_type].attach != ca_stride_func.attach &&
1633
1748
  ca_func[root->obj_type].attach != ca_window_func.attach &&
1634
1749
  ca->elements == root->elements &&
1635
- ca->bytes == root->bytes ) {
1750
+ ca->bytes == root->bytes &&
1751
+ ca_stride_layout_is_permutation(ca->ndim, ca->dim, composed_strides,
1752
+ composed_base, bytes) ) {
1636
1753
  ca_func[root->obj_type].fill_data(root, ptr);
1637
1754
  return;
1638
1755
  }
@@ -2094,6 +2211,13 @@ rb_cs_initialize_copy (VALUE self, VALUE other)
2094
2211
  ca_stride_setup(ca, cs->obj_type, cs->parent, cs->data_type, cs->bytes,
2095
2212
  cs->ndim, cs->dim, cs->strides, cs->base_offset);
2096
2213
  rb_ca_set_parent(self, rb_ca_parent(other));
2214
+ if ( ca_test_flag(cs, CA_FLAG_FOLDED) ) {
2215
+ /* the copy has the same @parent, so it answers to the same source */
2216
+ CArray *src = ca_fold_source((CArray *) cs);
2217
+ if ( src ) {
2218
+ ca_fold_note_source((CArray *) ca, src);
2219
+ }
2220
+ }
2097
2221
  return self;
2098
2222
  }
2099
2223
 
data/ext/ca_obj_string.c CHANGED
@@ -212,14 +212,15 @@ ca_string_refuse_cell (VALUE v, const char *where)
212
212
  "UNDEF masks a cell)", where);
213
213
  }
214
214
  rb_raise(rb_eTypeError,
215
- "%s is of class %s, not a String (a CAString holds Strings; "
216
- "UNDEF masks a cell)", where, rb_obj_classname(v));
215
+ "%s is of class %"PRIsVALUE", not a String (a CAString holds Strings; "
216
+ "UNDEF masks a cell)", where, rb_obj_class(v));
217
217
  }
218
218
 
219
219
  /* Every cell of `parent` that is not masked is a String. Under a masked
220
- cell of an entity that can be written, a value that is not a String (the
220
+ cell of a parent that can be written, a value that is not a String (the
221
221
  0 an object array starts with) becomes an empty String, so that the cell
222
- is still a String once it is unmasked. */
222
+ is still a String once it is unmasked. A view is written through its
223
+ value array, which leaves the mask as it is. */
223
224
  static void
224
225
  ca_string_check_cells (VALUE parent_val, CArray *parent)
225
226
  {
@@ -247,6 +248,7 @@ ca_string_check_cells (VALUE parent_val, CArray *parent)
247
248
  volatile VALUE list =
248
249
  rb_funcall(rb_funcall(parent_val, rb_intern("flatten"), 0),
249
250
  rb_intern("to_a"), 0);
251
+ volatile VALUE values = Qnil, raw = Qnil;
250
252
  long n = RARRAY_LEN(list), j;
251
253
  for (j = 0; j < n; j++) {
252
254
  VALUE v = rb_ary_entry(list, j);
@@ -256,6 +258,20 @@ ca_string_check_cells (VALUE parent_val, CArray *parent)
256
258
  ca_string_refuse_cell(v, where);
257
259
  }
258
260
  }
261
+ if ( ! ca_is_readonly(parent) ) {
262
+ for (j = 0; j < n; j++) {
263
+ if ( rb_ary_entry(list, j) != CA_UNDEF ) continue;
264
+ if ( NIL_P(values) ) {
265
+ values = rb_funcall(rb_funcall(parent_val, rb_intern("value"), 0),
266
+ rb_intern("flatten"), 0);
267
+ raw = rb_funcall(values, rb_intern("to_a"), 0);
268
+ }
269
+ if ( ! RB_TYPE_P(rb_ary_entry(raw, j), T_STRING) ) {
270
+ rb_funcall(values, rb_intern("[]="), 2, LONG2NUM(j),
271
+ rb_utf8_str_new("", 0));
272
+ }
273
+ }
274
+ }
259
275
  }
260
276
  }
261
277
 
data/ext/ca_obj_tile.c CHANGED
@@ -422,16 +422,14 @@ static void
422
422
  ca_tile_func_allocate (void *ap)
423
423
  {
424
424
  CATile *ca = (CATile *) ap;
425
- ca_attach(ca->parent);
426
- ca->ptr = ca_alloc_cells((CArray *) ca);
425
+ ca->ptr = ca_alloc_cells_then_attach((CArray *) ca, 1, &ca->parent);
427
426
  }
428
427
 
429
428
  static void
430
429
  ca_tile_func_attach (void *ap)
431
430
  {
432
431
  CATile *ca = (CATile *) ap;
433
- ca_attach(ca->parent);
434
- ca->ptr = ca_alloc_cells((CArray *) ca);
432
+ ca->ptr = ca_alloc_cells_then_attach((CArray *) ca, 1, &ca->parent);
435
433
  ca_tile_attach_into(ca, ca->ptr);
436
434
  }
437
435
 
@@ -643,6 +641,7 @@ Init_ca_obj_tile (void)
643
641
  &catile_data_type,
644
642
  rb_cCATileMask,
645
643
  &catile_mask_data_type, &ca_tile_func, sizeof(ca_tile_func));
644
+ ca_register_cell_moving_view(CA_OBJ_TILE);
646
645
  rb_define_const(rb_cObject, "CA_OBJ_TILE", INT2NUM(CA_OBJ_TILE));
647
646
 
648
647
  rb_define_method(rb_cCArray, "tile", rb_ca_tile, -1);
data/ext/ca_obj_triop.c CHANGED
@@ -139,8 +139,8 @@ ca_triop_setup (CATriOp *ca, CArray *op1, CArray *op2, CArray *op3,
139
139
  ca->op2 = op2;
140
140
  ca->op3 = op3;
141
141
  ca->op_id = op_id;
142
- ca->op2_is_scalar = ( op2->elements == 1 && op1->elements > 1 ) ? 1 : 0;
143
- ca->op3_is_scalar = ( op3->elements == 1 && op1->elements > 1 ) ? 1 : 0;
142
+ ca->op2_is_scalar = ( op2->elements == 1 ) ? 1 : 0;
143
+ ca->op3_is_scalar = ( op3->elements == 1 ) ? 1 : 0;
144
144
  ca->operands[0] = op1;
145
145
  ca->operands[1] = op2;
146
146
  ca->operands[2] = op3;
@@ -267,9 +267,11 @@ pull_operand (CArray *op, int is_scalar, const ca_lazy_req_t *req,
267
267
 
268
268
  /* Evaluate the cells a request names into `data` (packed, n cells). */
269
269
  static void
270
- ca_triop_eval (CATriOp *to, const ca_lazy_req_t *req, void *data)
270
+ ca_triop_eval_cells (void *node, const ca_lazy_req_t *req, void *data, int dir)
271
271
  {
272
+ CATriOp *to = (CATriOp *) node;
272
273
  ca_size_t slab_n = req->n;
274
+ (void) dir;
273
275
  void *scratch2, *scratch3;
274
276
  ca_size_t op2_step, op3_step;
275
277
 
@@ -316,6 +318,12 @@ ca_triop_eval (CATriOp *to, const ca_lazy_req_t *req, void *data)
316
318
  ca_lazy_arena_release(scratch2);
317
319
  }
318
320
 
321
+ static void
322
+ ca_triop_eval (CATriOp *to, const ca_lazy_req_t *req, void *data)
323
+ {
324
+ ca_lazy_eval_recorded(to, 3, to->operands, 0, NULL, ca_triop_eval_cells, req, data, CA_XFER_GET);
325
+ }
326
+
319
327
  static void
320
328
  ca_triop_func_xfer_stride (void *ap, ca_size_t *starts, ca_size_t *counts,
321
329
  ca_size_t *strides, void *data, int dir)
@@ -495,8 +503,9 @@ insert_cast (VALUE cary, int8_t target_dt, CArray **ca_out)
495
503
  data_type differs from the common data_type, then resolves
496
504
  same-ndim size-1 broadcast pairwise against op1 (the walk anchor).
497
505
 
498
- Broadcast: each of op2 / op3 is aligned against op1 via
499
- ca_broadcast_pair. After alignment, each must have either the
506
+ Broadcast: the three are aligned pairwise via ca_broadcast_pair
507
+ (op1 / op2 again last, as two of them can meet at a shape smaller than
508
+ the third's). After alignment, op2 / op3 must each have either the
500
509
  same elements as op1 or be a 1-element CScalar (walked with
501
510
  element_step = 0 by the kernel). If op1 itself is a 1-element
502
511
  CScalar and any of op2 / op3 is an array, op1 is lifted via
@@ -527,17 +536,16 @@ rb_ca_triop_build (VALUE cary1, VALUE cary2, VALUE cary3, uint16_t op_id)
527
536
  /* Scalar operands (clip's Numeric bounds, a CScalar) are wrapped and
528
537
  promoted pairwise in the order the eager triop uses. */
529
538
  else if ( ! rb_obj_is_carray(r1) || ! rb_obj_is_carray(r2) || ! rb_obj_is_carray(r3) ||
530
- rb_obj_is_cscalar(r2) || rb_obj_is_cscalar(r3) ) {
539
+ rb_obj_is_cscalar(r1) || rb_obj_is_cscalar(r2) || rb_obj_is_cscalar(r3) ) {
531
540
  if ( ! rb_obj_is_carray(r1) ) {
532
541
  rb_raise(rb_eArgError,
533
542
  "CATriOp: the first operand must be a CArray");
534
543
  }
544
+ int r1_scalar = rb_obj_is_cscalar(r1);
535
545
  int r2_scalar = ! rb_obj_is_carray(r2) || rb_obj_is_cscalar(r2);
536
546
  int r3_scalar = ! rb_obj_is_carray(r3) || rb_obj_is_cscalar(r3);
537
- rb_ca_cast_self_or_other(&r1, &r2);
538
- rb_ca_cast_self_or_other(&r1, &r3);
539
- rb_ca_cast_self_or_other(&r2, &r3);
540
- rb_ca_cast_self_or_other(&r1, &r2);
547
+ rb_ca_cast_three(&r1, &r2, &r3);
548
+ r1 = ca_lazy_settle_scalar(r1, r1_scalar);
541
549
  r2 = ca_lazy_settle_scalar(r2, r2_scalar);
542
550
  r3 = ca_lazy_settle_scalar(r3, r3_scalar);
543
551
  }
@@ -601,12 +609,15 @@ rb_ca_triop_build (VALUE cary1, VALUE cary2, VALUE cary3, uint16_t op_id)
601
609
  }
602
610
  }
603
611
 
604
- /* Now align op2 / op3 against op1 for size-1 broadcast. */
612
+ /* Now bring the three to one shape for size-1 broadcast. Every pair is
613
+ aligned, and the first again after the others: op1 and op2 may meet
614
+ at a shape smaller than op3's ([1,4] [1,4] [3,4]). */
605
615
  ca_broadcast_pair(&r1, &r2);
606
- TypedData_Get_Struct(r1, CArray, &carray_data_type, op1);
607
- TypedData_Get_Struct(r2, CArray, &carray_data_type, op2);
608
616
  ca_broadcast_pair(&r1, &r3);
617
+ ca_broadcast_pair(&r2, &r3);
618
+ ca_broadcast_pair(&r1, &r2);
609
619
  TypedData_Get_Struct(r1, CArray, &carray_data_type, op1);
620
+ TypedData_Get_Struct(r2, CArray, &carray_data_type, op2);
610
621
  TypedData_Get_Struct(r3, CArray, &carray_data_type, op3);
611
622
 
612
623
  /* Step 3: each of op2 / op3 must match op1's element count OR be a
data/ext/ca_obj_window.c CHANGED
@@ -275,10 +275,11 @@ static int
275
275
  ca_window_func_fold_stride (void *ap, ca_fold_t *f, void **next_parent)
276
276
  {
277
277
  CAWindow *w = (CAWindow *) ap;
278
- ca_window_mask_current(w);
279
- /* A write folded through to a stand-in would bypass the settle in this
280
- window's write slots. */
281
- if ( ca_window_mask_reads_standin(w) ) {
278
+ /* A mask window never folds. A write folded through to a stand-in would
279
+ bypass the settle in this window's write slots, and answering by what
280
+ the window reads now would change when a write settles: a view attached
281
+ over this window would detach a root it never attached. */
282
+ if ( w->mask_owner ) {
282
283
  return 0;
283
284
  }
284
285
  ca_size_t synth_strides[CA_RANK_MAX];
@@ -701,7 +702,8 @@ ca_window_setup (CAWindow *ca, CArray *parent,
701
702
  /* Under "ruby" or "strict" an index outside the parent raises. The
702
703
  parent's shape is fixed, so a window that reaches outside it would raise
703
704
  on every whole read or write, and a write would have changed the cells
704
- before the one that raised. It is refused here instead. */
705
+ before the one that raised. It is refused here instead. Under
706
+ "nearest" an axis with no cells has no edge to replicate. */
705
707
  for (i=0; i<ndim; i++) {
706
708
  ca_size_t lo, hi, size = dim[i];
707
709
  if ( count[i] == 0 ) {
@@ -717,6 +719,9 @@ ca_window_setup (CAWindow *ca, CArray *parent,
717
719
  if ( lo < -size ) ca_index_out_of_range(lo, size, "index");
718
720
  if ( hi >= size ) ca_index_out_of_range(hi, size, "index");
719
721
  }
722
+ else if ( bounds[i] == CA_BOUNDS_NEAREST && size == 0 ) {
723
+ ca_index_out_of_range(lo, size, "index");
724
+ }
720
725
  }
721
726
 
722
727
  /* ca == NULL asks for the checks above only, so that a constructor can
@@ -1114,12 +1119,12 @@ ca_window_func_allocate (void *ap)
1114
1119
  ca->ptr = ca_alloc_cells((CArray *) ca);
1115
1120
  return;
1116
1121
  }
1117
- ca_attach(ca->parent);
1118
1122
  if ( ca->embed_alias_eligible ) {
1123
+ ca_attach(ca->parent);
1119
1124
  ca->ptr = ca_window_alias_ptr(ca);
1120
1125
  return;
1121
1126
  }
1122
- ca->ptr = ca_alloc_cells((CArray *) ca);
1127
+ ca->ptr = ca_alloc_cells_then_attach((CArray *) ca, 1, &ca->parent);
1123
1128
  }
1124
1129
 
1125
1130
  /* The engine paths below emit a per-axis descriptor (ca_window_describe_axes)
@@ -1139,7 +1144,6 @@ ca_window_func_attach (void *ap)
1139
1144
  ca_axis_view_attach_owned(ca);
1140
1145
  return;
1141
1146
  }
1142
- ca_attach(ca->parent);
1143
1147
  if ( ca->embed_alias_eligible ) {
1144
1148
  /* Alias path: inner axes full + interior, so the embedded region is a
1145
1149
  contiguous run of parent storage. Skip malloc/memcpy and point
@@ -1147,10 +1151,13 @@ ca_window_func_attach (void *ap)
1147
1151
  CAREFUL: sync and detach must agree with this — sync has nothing to
1148
1152
  scatter back (the writes already landed in the parent) and detach must
1149
1153
  not xfree a pointer it does not own. */
1154
+ ca_attach(ca->parent);
1150
1155
  ca->ptr = ca_window_alias_ptr(ca);
1151
- } else if ( ca->embed_eligible ) {
1152
- /* Embed path: allocate, then 1 fill + 1 strided memcpy. */
1153
- ca->ptr = ca_alloc_cells((CArray *) ca);
1156
+ return;
1157
+ }
1158
+ ca->ptr = ca_alloc_cells_then_attach((CArray *) ca, 1, &ca->parent);
1159
+ if ( ca->embed_eligible ) {
1160
+ /* Embed path: 1 fill + 1 strided memcpy. */
1154
1161
  ca_window_attach_embed(ca, ca->ptr);
1155
1162
  } else {
1156
1163
  /* Fallback: PERIODIC / REFLECT / NEAREST / RUBY / STRICT go through
@@ -1158,8 +1165,8 @@ ca_window_func_attach (void *ap)
1158
1165
  ca_axis_desc_t desc[CA_RANK_MAX];
1159
1166
  ca_size_t pdims[CA_RANK_MAX];
1160
1167
  ca_window_describe_axes(ca, desc, pdims);
1161
- ca->ptr = ca_axis_dispatch_attach(ca->parent, pdims, desc, ca->ndim,
1162
- ca->bytes, ca->elements, ca->fill);
1168
+ ca_axis_dispatch_gather(ca->parent, pdims, desc, ca->ndim,
1169
+ ca->bytes, ca->elements, ca->fill, ca->ptr);
1163
1170
  }
1164
1171
  }
1165
1172
 
@@ -1700,8 +1707,8 @@ rb_ca_window (int argc, VALUE *argv, VALUE self)
1700
1707
  }
1701
1708
  if ( ! RB_TYPE_P(rbounds, T_STRING) ) {
1702
1709
  rb_raise(rb_eTypeError,
1703
- "window: bounds: must be a Symbol or a String (got %s)",
1704
- rb_obj_classname(rbounds));
1710
+ "window: bounds: must be a Symbol or a String (got %"PRIsVALUE")",
1711
+ rb_obj_class(rbounds));
1705
1712
  }
1706
1713
  cbounds = StringValueCStr(rbounds);
1707
1714
  if ( ! strcmp(cbounds, "ruby") ) {
@@ -1982,6 +1989,7 @@ Init_ca_obj_window (void)
1982
1989
  &cawindow_data_type,
1983
1990
  rb_cCAWindowMask,
1984
1991
  &cawindow_mask_data_type, &ca_window_func, sizeof(ca_window_func));
1992
+ ca_register_cell_moving_view(CA_OBJ_WINDOW);
1985
1993
  rb_define_const(rb_cObject, "CA_OBJ_WINDOW", INT2NUM(CA_OBJ_WINDOW));
1986
1994
 
1987
1995
  rb_define_method(rb_cCArray, "window", rb_ca_window, -1);