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_grid.c CHANGED
@@ -15,6 +15,7 @@
15
15
 
16
16
  #include "carray.h"
17
17
  #include "carray_internal.h" /* ca_attach_window */
18
+ #include "ca_obj_face.h" /* CA_WRAPPER_LIFT */
18
19
 
19
20
  /* Per-axis tagged kind. Each axis is either STRIDE (start/step/count,
20
21
  no allocation) or INDEX (an owned ca_size_t index snapshot). A Range
@@ -490,11 +491,86 @@ cag_parent_addr (const CAGrid *ca, const ca_size_t *idx, ca_size_t *paddr)
490
491
  return hole;
491
492
  }
492
493
 
494
+ /* The parent addresses (paddrs, may be NULL) and the hole marks of every
495
+ view cell in address order: a walk over the outer axes with the
496
+ innermost axis a run, its indices and holes read off the axis. A 1-D
497
+ index axis is its own index and hole arrays. */
498
+ static void
499
+ cag_parent_addrs_whole (const CAGrid *ca, ca_size_t *paddrs, uint8_t *hole)
500
+ {
501
+ int8_t ndim = ca->ndim;
502
+ int8_t inner = ndim - 1;
503
+ const cag_axis_t *last = &ca->axes[inner];
504
+ ca_size_t count = last->count;
505
+ ca_size_t pdim = ca->parent->dim[inner];
506
+ ca_size_t idx[CA_RANK_MAX];
507
+ ca_size_t i = 0, j;
508
+ int8_t k;
509
+
510
+ if ( ndim == 1 && last->kind == CAG_AXIS_INDEX ) {
511
+ if ( paddrs ) {
512
+ memcpy(paddrs, last->indices, (size_t) count * sizeof(ca_size_t));
513
+ }
514
+ if ( last->holes ) {
515
+ memcpy(hole, last->holes, (size_t) count);
516
+ }
517
+ else {
518
+ memset(hole, 0, (size_t) count);
519
+ }
520
+ return;
521
+ }
522
+ if ( count == 0 || ca->elements == 0 ) {
523
+ return;
524
+ }
525
+ for (k = 0; k < ndim; k++) {
526
+ idx[k] = 0;
527
+ }
528
+ /* empty: returned above when the view has no cell */
529
+ while ( 1 ) {
530
+ ca_size_t base = 0;
531
+ uint8_t outer_hole = 0;
532
+ for (k = 0; k < inner; k++) {
533
+ const cag_axis_t *ax = &ca->axes[k];
534
+ if ( ax->kind == CAG_AXIS_INDEX && ax->holes && ax->holes[idx[k]] ) {
535
+ outer_hole = 1;
536
+ }
537
+ base = base * ca->parent->dim[k] + cag_parent_index_for(ax, idx[k]);
538
+ }
539
+ base *= pdim;
540
+ if ( last->kind == CAG_AXIS_INDEX ) {
541
+ const ca_size_t *ix = last->indices;
542
+ const uint8_t *hx = last->holes;
543
+ for (j = 0; j < count; j++) {
544
+ if ( paddrs ) {
545
+ paddrs[i + j] = base + ix[j];
546
+ }
547
+ hole[i + j] = (uint8_t) (outer_hole | (hx ? hx[j] : 0));
548
+ }
549
+ }
550
+ else {
551
+ for (j = 0; j < count; j++) {
552
+ if ( paddrs ) {
553
+ paddrs[i + j] = base + last->start + j * last->step;
554
+ }
555
+ hole[i + j] = outer_hole;
556
+ }
557
+ }
558
+ i += count;
559
+ for (k = inner - 1; k >= 0; k--) {
560
+ if ( ++idx[k] < ca->dim[k] ) break;
561
+ idx[k] = 0;
562
+ }
563
+ if ( k < 0 ) break;
564
+ }
565
+ }
566
+
493
567
  /* The transfer of a grid with holes, for the view cells at addrs (NULL:
494
- the whole view in order). The parent is asked for one batch of
495
- addresses. A GET reads every cell, a hole reading the cell at index 0
496
- of its holed axes (in a mask grid it is then set true); a PUT leaves
497
- the holes out. */
568
+ the whole view in order, walked axis by axis). The parent is asked
569
+ for one batch of addresses. A GET reads every cell, a hole reading the
570
+ cell at index 0 of its holed axes (in a mask grid it is then set true); a
571
+ PUT leaves the holes out. A mask grid reading its owner's stand-in reads
572
+ nothing: the stand-in is all false at rest (a write to it is settled
573
+ into the parent's mask at once), so the holes are the whole answer. */
498
574
  static void
499
575
  ca_grid_holed_xfer (CAGrid *ca, ca_size_t n, const ca_size_t *addrs,
500
576
  char *data, int dir)
@@ -511,11 +587,43 @@ ca_grid_holed_xfer (CAGrid *ca, ca_size_t n, const ca_size_t *addrs,
511
587
  }
512
588
  cag_mask_current(ca);
513
589
  parent = ca->parent;
514
- paddrs = ALLOCV_N(ca_size_t, h_addr, n);
515
- hole = ALLOCV_N(uint8_t, h_hole, n);
516
- for (i = 0; i < n; i++) {
517
- ca_addr2index((CArray *) ca, addrs ? addrs[i] : i, idx);
518
- hole[i] = (uint8_t) cag_parent_addr(ca, idx, &paddrs[i]);
590
+
591
+ if ( dir == CA_XFER_GET && ca->holes_read_masked && ca->mask_owner != NULL
592
+ && parent == ca->mask_owner->mask_standin ) {
593
+ if ( ! addrs ) {
594
+ cag_parent_addrs_whole(ca, NULL, (uint8_t *) data);
595
+ }
596
+ else {
597
+ for (i = 0; i < n; i++) {
598
+ ca_addr2index((CArray *) ca, addrs[i], idx);
599
+ ((boolean8_t *) data)[i] = (boolean8_t) cag_parent_addr(ca, idx, &m);
600
+ }
601
+ }
602
+ return;
603
+ }
604
+
605
+ if ( dir == CA_XFER_GET && ! addrs && ca->ndim == 1
606
+ && ( parent->obj_type == CA_OBJ_ARRAY || parent->obj_type == CA_OBJ_ARRAY_WRAP
607
+ || parent->obj_type == CA_OBJ_SCALAR ) ) {
608
+ /* A 1-D grid read whole from an array of its own: its index axis's
609
+ index and hole arrays are the batch as they are. Another parent's
610
+ transfer (a view, a CAObject's hook) takes a copy it may write on,
611
+ as a PUT does, which compacts the batch. */
612
+ paddrs = ca->axes[0].indices;
613
+ hole = ca->axes[0].holes;
614
+ }
615
+ else {
616
+ paddrs = ALLOCV_N(ca_size_t, h_addr, n);
617
+ hole = ALLOCV_N(uint8_t, h_hole, n);
618
+ if ( ! addrs ) {
619
+ cag_parent_addrs_whole(ca, paddrs, hole);
620
+ }
621
+ else {
622
+ for (i = 0; i < n; i++) {
623
+ ca_addr2index((CArray *) ca, addrs[i], idx);
624
+ hole[i] = (uint8_t) cag_parent_addr(ca, idx, &paddrs[i]);
625
+ }
626
+ }
519
627
  }
520
628
 
521
629
  if ( dir == CA_XFER_GET ) {
@@ -712,6 +820,50 @@ ca_grid_func_fold_stride (void *ap, ca_fold_t *f, void **next_parent)
712
820
  return 1;
713
821
  }
714
822
 
823
+ /* A region of a grid with holes: its view addresses as one batch to the
824
+ holed transfer, or none when the region is the whole view in order. */
825
+ static void
826
+ cag_holed_xfer_stride (CAGrid *g, const ca_size_t *starts, const ca_size_t *counts,
827
+ const ca_size_t *strides, char *data, int dir)
828
+ {
829
+ int8_t ndim = g->ndim;
830
+ ca_size_t gnative[CA_RANK_MAX], idx[CA_RANK_MAX];
831
+ ca_size_t n = 1, i, s, base = 0;
832
+ ca_size_t *addrs;
833
+ volatile VALUE holder = 0;
834
+ int whole = 1;
835
+ int8_t k;
836
+
837
+ s = g->bytes;
838
+ for (k = ndim - 1; k >= 0; k--) { gnative[k] = s; s *= g->dim[k]; }
839
+ for (k = 0; k < ndim; k++) {
840
+ n *= counts[k];
841
+ base += starts[k] * gnative[k];
842
+ if ( starts[k] != 0 || counts[k] != g->dim[k] || strides[k] != gnative[k] ) {
843
+ whole = 0;
844
+ }
845
+ idx[k] = 0;
846
+ }
847
+ if ( whole ) {
848
+ ca_grid_holed_xfer(g, n, NULL, data, dir);
849
+ return;
850
+ }
851
+ addrs = ALLOCV_N(ca_size_t, holder, n);
852
+ for (i = 0; i < n; i++) {
853
+ ca_size_t goff = base;
854
+ for (k = 0; k < ndim; k++) {
855
+ goff += idx[k] * strides[k];
856
+ }
857
+ addrs[i] = goff / g->bytes;
858
+ for (k = ndim - 1; k >= 0; k--) {
859
+ if ( ++idx[k] < counts[k] ) break;
860
+ idx[k] = 0;
861
+ }
862
+ }
863
+ ca_grid_holed_xfer(g, n, addrs, data, dir);
864
+ ALLOCV_END(holder);
865
+ }
866
+
715
867
  /* xfer_stride: structural region delivery when CAGrid is the (declining)
716
868
  fold boundary -- a multi-element INDEX axis. Instead of materialising
717
869
  the whole grid, deliver only the requested region using the per-axis
@@ -763,9 +915,12 @@ ca_grid_func_xfer_stride (void *ap, ca_size_t *starts, ca_size_t *counts,
763
915
  /* The request is over the view's addresses, so a transposed / flat request
764
916
  is legal and must not be composed axis-by-axis; see
765
917
  ca_xfer_stride_request_is_axis_box (carray.h). */
766
- if ( cag_has_holes(g)
767
- || ! ca_xfer_stride_request_is_axis_box(g, starts, counts, strides) ) {
768
- aligned = 0; /* a grid with holes goes cell by cell (xfer_index) */
918
+ if ( cag_has_holes(g) ) {
919
+ cag_holed_xfer_stride(g, starts, counts, strides, d, dir);
920
+ return;
921
+ }
922
+ if ( ! ca_xfer_stride_request_is_axis_box(g, starts, counts, strides) ) {
923
+ aligned = 0;
769
924
  }
770
925
  else {
771
926
  for (k = 0; k < ndim; k++) {
@@ -933,9 +1088,11 @@ ca_grid_func_allocate (void *ap)
933
1088
  {
934
1089
  CAGrid *ca = (CAGrid *) ap;
935
1090
  if ( ! cag_owns_buffer(ca) ) {
936
- ca_attach(ca->parent);
1091
+ ca->ptr = ca_alloc_cells_then_attach((CArray *) ca, 1, &ca->parent);
1092
+ }
1093
+ else {
1094
+ ca->ptr = ca_alloc_cells((CArray *) ca);
937
1095
  }
938
- ca->ptr = ca_alloc_cells((CArray *) ca);
939
1096
  }
940
1097
 
941
1098
  static void
@@ -950,10 +1107,10 @@ ca_grid_func_attach (void *ap)
950
1107
  return;
951
1108
  }
952
1109
 
953
- ca_attach(ca->parent);
1110
+ ca->ptr = ca_alloc_cells_then_attach((CArray *) ca, 1, &ca->parent);
954
1111
  ca_grid_describe_axes(ca, desc, pdims);
955
- ca->ptr = ca_axis_dispatch_attach(ca->parent, pdims, desc, ca->ndim,
956
- ca->bytes, ca->elements, NULL);
1112
+ ca_axis_dispatch_gather(ca->parent, pdims, desc, ca->ndim,
1113
+ ca->bytes, ca->elements, NULL, ca->ptr);
957
1114
  }
958
1115
 
959
1116
  static void
@@ -1268,8 +1425,9 @@ rb_ca_grid (int argc, VALUE *argv, VALUE self)
1268
1425
  extern int ca_csa_dispatch_bypass;
1269
1426
  if ( ! ca_csa_dispatch_bypass && argc > 0 && TYPE(argv[0]) == T_DATA ) {
1270
1427
  if ( rb_ca_select_axis_eligible_p(argc, argv, self) ) {
1271
- return ca_grid_drop_scalar_axes(rb_ca_select_axis(argc, argv, self),
1272
- argc, argv);
1428
+ obj = rb_ca_select_axis(argc, argv, self);
1429
+ CA_WRAPPER_LIFT(obj, self, ca);
1430
+ return ca_grid_drop_scalar_axes(obj, argc, argv);
1273
1431
  }
1274
1432
  }
1275
1433
 
@@ -1338,6 +1496,7 @@ rb_ca_grid (int argc, VALUE *argv, VALUE self)
1338
1496
  (VALUE) &c);
1339
1497
  }
1340
1498
 
1499
+ CA_WRAPPER_LIFT(obj, self, ca);
1341
1500
  return ca_grid_drop_scalar_axes(obj, argc, argv);
1342
1501
  }
1343
1502
 
@@ -1548,6 +1707,7 @@ Init_ca_obj_grid (void)
1548
1707
  &cagrid_data_type,
1549
1708
  rb_cCAGridMask,
1550
1709
  &cagrid_mask_data_type, &ca_grid_func, sizeof(ca_grid_func));
1710
+ ca_register_cell_moving_view(CA_OBJ_GRID);
1551
1711
  rb_define_const(rb_cObject, "CA_OBJ_GRID", INT2NUM(CA_OBJ_GRID));
1552
1712
 
1553
1713
  rb_define_method(rb_cCArray, "grid", rb_ca_grid, -1);
data/ext/ca_obj_meld.c CHANGED
@@ -686,7 +686,7 @@ ca_meld_func_xfer_stride (void *ap, ca_size_t *starts, ca_size_t *counts,
686
686
  structural = 0;
687
687
  }
688
688
 
689
- if ( ! structural || ! ca_fast_paths_enabled() ) {
689
+ if ( ! structural || ! ca_fast_path(CA_SC_MELD_STRIDE_BOX) ) {
690
690
  ca_meld_xfer_stride_per_cell(ca, starts, counts, strides, data, dir);
691
691
  return;
692
692
  }
@@ -702,16 +702,23 @@ ca_meld_func_xfer_stride (void *ap, ca_size_t *starts, ca_size_t *counts,
702
702
  /* xfer_all */
703
703
  /* ------------------------------------------------------------------- */
704
704
 
705
- /* A parent's cells, delivered (PUT). into_buffers writes them into the
706
- buffer the parent's attach handed out, rather than through the parent to
707
- what is behind it: the sync of an attached meld, which then syncs each
708
- parent. Writing past the buffer would leave it stale, and the parent's
709
- own sync would push the stale buffer back over the cells just written. */
705
+ /* A parent's cells, delivered (PUT). A non-NULL sync_error writes them
706
+ into the buffer the parent's attach handed out, rather than through the
707
+ parent to what is behind it, and syncs the parent: the sync of an
708
+ attached meld. Writing past the buffer would leave it stale, and the parent's
709
+ own sync would push the stale buffer back over the cells just written.
710
+ A parent with no buffer out (a stand-in the trial of a write puts in
711
+ place, which is never attached) has nothing to go stale: the cells go
712
+ through it. */
710
713
  static void
711
- ca_meld_put_parent (CArray *p, char *src, int into_buffers)
714
+ ca_meld_put_parent (CArray *p, char *src, volatile VALUE *sync_error)
712
715
  {
713
- if ( into_buffers ) {
716
+ if ( sync_error != NULL && p->ptr != NULL ) {
717
+ /* Synced at once, before the next part is written: parts that share
718
+ cells then take the write of the last of them, as a write through
719
+ the parts does. */
714
720
  memcpy(p->ptr, src, (size_t) (p->elements * p->bytes));
721
+ ca_sync_carry_on(p, sync_error);
715
722
  }
716
723
  else {
717
724
  ca_xfer_all(p, src, CA_XFER_PUT);
@@ -720,7 +727,7 @@ ca_meld_put_parent (CArray *p, char *src, int into_buffers)
720
727
 
721
728
  /* meld_axis == 0 best path: K contig xfer_all at prefix-sum offsets. */
722
729
  static void
723
- ca_meld_xfer_all_ma0 (CAMeld *ca, void *data, int dir, int into_buffers)
730
+ ca_meld_xfer_all_ma0 (CAMeld *ca, void *data, int dir, volatile VALUE *sync_error)
724
731
  {
725
732
  ca_size_t tail_bytes = ca_meld_tail_elements(ca) * ca->bytes;
726
733
  char *d = (char *) data;
@@ -728,7 +735,7 @@ ca_meld_xfer_all_ma0 (CAMeld *ca, void *data, int dir, int into_buffers)
728
735
  for ( k = 0; k < ca->n_parents; k++ ) {
729
736
  char *seg = d + ca->seg_offset[k] * tail_bytes;
730
737
  if ( dir == CA_XFER_PUT ) {
731
- ca_meld_put_parent(ca->parents[k], seg, into_buffers);
738
+ ca_meld_put_parent(ca->parents[k], seg, sync_error);
732
739
  }
733
740
  else {
734
741
  ca_xfer_all(ca->parents[k], seg, dir);
@@ -750,7 +757,7 @@ ca_meld_xfer_all_ma0 (CAMeld *ca, void *data, int dir, int into_buffers)
750
757
  chunked memcpy approaches memcpy bandwidth (eager parity target). */
751
758
  static void
752
759
  ca_meld_xfer_all_ma_internal (CAMeld *ca, void *data, int dir,
753
- int into_buffers)
760
+ volatile VALUE *sync_error)
754
761
  {
755
762
  int8_t ma = ca->meld_axis;
756
763
  int8_t ndim = ca->ndim;
@@ -780,6 +787,12 @@ ca_meld_xfer_all_ma_internal (CAMeld *ca, void *data, int dir,
780
787
  ca_size_t voff;
781
788
  int8_t i;
782
789
 
790
+ /* A parent with no cells moves nothing. The walks below copy one
791
+ chunk before they look at the outer extents, so a parent empty along
792
+ an outer axis would copy a chunk out of a 0-byte buffer. sync comes
793
+ here without the slot's own check on the view. */
794
+ if ( p->elements == 0 ) continue;
795
+
783
796
  buf = ALLOCV_N(char, holder, slab_bytes);
784
797
 
785
798
  if ( dir == CA_XFER_GET ) {
@@ -837,19 +850,19 @@ ca_meld_xfer_all_ma_internal (CAMeld *ca, void *data, int dir,
837
850
  }
838
851
 
839
852
  if ( dir == CA_XFER_PUT ) {
840
- ca_meld_put_parent(p, buf, into_buffers);
853
+ ca_meld_put_parent(p, buf, sync_error);
841
854
  }
842
855
  ALLOCV_END(holder);
843
856
  }
844
857
  }
845
858
 
846
859
  static void
847
- ca_meld_xfer_parents (CAMeld *ca, void *data, int dir, int into_buffers)
860
+ ca_meld_xfer_parents (CAMeld *ca, void *data, int dir, volatile VALUE *sync_error)
848
861
  {
849
- if ( ca->meld_axis == 0 && ca_fast_paths_enabled() ) {
850
- ca_meld_xfer_all_ma0(ca, data, dir, into_buffers);
862
+ if ( ca->meld_axis == 0 && ca_fast_path(CA_SC_MELD_PARENTS_AXIS0) ) {
863
+ ca_meld_xfer_all_ma0(ca, data, dir, sync_error);
851
864
  } else {
852
- ca_meld_xfer_all_ma_internal(ca, data, dir, into_buffers);
865
+ ca_meld_xfer_all_ma_internal(ca, data, dir, sync_error);
853
866
  }
854
867
  }
855
868
 
@@ -858,7 +871,7 @@ ca_meld_func_xfer_all (void *ap, void *data, int dir)
858
871
  {
859
872
  if ( ((CArray *) ap)->elements == 0 ) return;
860
873
  ca_meld_mask_refresh((CAMeld *) ap);
861
- ca_meld_xfer_parents((CAMeld *) ap, data, dir, 0);
874
+ ca_meld_xfer_parents((CAMeld *) ap, data, dir, NULL);
862
875
  }
863
876
 
864
877
  /* ------------------------------------------------------------------- */
@@ -870,8 +883,8 @@ ca_meld_func_allocate (void *ap)
870
883
  {
871
884
  CAMeld *ca = (CAMeld *) ap;
872
885
  ca_meld_mask_refresh(ca);
873
- ca_attach_all(ca->parents, ca->n_parents);
874
- ca->ptr = ca_alloc_cells((CArray *) ca);
886
+ ca->ptr = ca_alloc_cells_then_attach((CArray *) ca, ca->n_parents,
887
+ ca->parents);
875
888
  }
876
889
 
877
890
  static VALUE
@@ -890,15 +903,19 @@ ca_meld_func_attach (void *ap)
890
903
  ca_fill_or_detach(ca->n_parents, ca->parents, ca_meld_attach_owned, (VALUE) ca);
891
904
  }
892
905
 
893
- static void ca_meld_mask_refuse_sync (CAMeld *ca);
894
-
895
906
  static void
896
907
  ca_meld_func_sync (void *ap)
897
908
  {
898
909
  CAMeld *ca = (CAMeld *) ap;
899
- ca_meld_mask_refuse_sync(ca);
900
- ca_meld_xfer_parents(ca, ca->ptr, CA_XFER_PUT, 1);
901
- ca_sync_all(ca->parents, ca->n_parents);
910
+ CA_MULTI_PARENT_VALUE_TRIAL(ca, t,
911
+ ca_meld_xfer_parents((CAMeld *) t, ca->ptr, CA_XFER_PUT, NULL));
912
+ {
913
+ volatile VALUE error = Qnil;
914
+ ca_meld_xfer_parents(ca, ca->ptr, CA_XFER_PUT, &error);
915
+ if ( ! NIL_P(error) ) {
916
+ rb_exc_raise(error);
917
+ }
918
+ }
902
919
  ca_meld_mask_settle(ca);
903
920
  }
904
921
 
@@ -971,79 +988,21 @@ ca_meld_func_fold_stride (void *ap, ca_fold_t *f, void **next_parent)
971
988
  /* ------------------------------------------------------------------- */
972
989
 
973
990
  /* The table's write slots: after a write that may have put a true into a
974
- stand-in, the true goes on to the parent's mask, or is refused for a
975
- value array or a mask array (ca_mask_standin_settle). Only the mask of
976
- a CAMeld has stand-ins. */
977
- /* A write to the mask of a CAMeld, run once against blank masks first
978
- (ca_multi_parent_mask_refuse_first), so that a write refused for a value
979
- array or mask array part changes no other part. */
980
- enum { CA_MELD_PUT_INDEX, CA_MELD_PUT_ADDRS, CA_MELD_PUT_STRIDE, CA_MELD_PUT_ALL,
981
- CA_MELD_PUT_FILL, CA_MELD_PUT_SYNC, CA_MELD_PUT_FILL_ADDRS,
982
- CA_MELD_PUT_FILL_STRIDE };
983
-
984
- typedef struct {
985
- int slot;
986
- void *ap;
987
- ca_size_t *idx;
988
- ca_size_t n;
989
- ca_size_t *addrs;
990
- ca_size_t *starts;
991
- ca_size_t *counts;
992
- ca_size_t *strides;
993
- void *data;
994
- ca_size_t base;
995
- int8_t ndim;
996
- ca_size_t *steps;
997
- } ca_meld_put_t;
998
-
999
- static VALUE
1000
- ca_meld_put_body (VALUE arg)
1001
- {
1002
- ca_meld_put_t *a = (ca_meld_put_t *) arg;
1003
- switch ( a->slot ) {
1004
- case CA_MELD_PUT_INDEX:
1005
- ca_meld_func_xfer_index(a->ap, a->idx, a->data, CA_XFER_PUT); break;
1006
- case CA_MELD_PUT_ADDRS:
1007
- ca_meld_func_xfer_addrs(a->ap, a->n, a->addrs, a->data, CA_XFER_PUT); break;
1008
- case CA_MELD_PUT_STRIDE:
1009
- ca_meld_func_xfer_stride(a->ap, a->starts, a->counts, a->strides, a->data,
1010
- CA_XFER_PUT); break;
1011
- case CA_MELD_PUT_ALL:
1012
- ca_meld_func_xfer_all(a->ap, a->data, CA_XFER_PUT); break;
1013
- case CA_MELD_PUT_FILL:
1014
- ca_meld_func_fill_data(a->ap, a->data); break;
1015
- case CA_MELD_PUT_FILL_ADDRS:
1016
- ca_fill_addrs_default(a->ap, a->n, a->addrs, a->data); break;
1017
- case CA_MELD_PUT_FILL_STRIDE:
1018
- ca_fill_stride_default(a->ap, a->base, a->ndim, a->counts, a->steps,
1019
- a->data); break;
1020
- case CA_MELD_PUT_SYNC:
1021
- ca_meld_xfer_parents((CAMeld *) a->ap, ((CArray *) a->ap)->ptr, CA_XFER_PUT, 1);
1022
- break;
1023
- }
1024
- return Qnil;
1025
- }
1026
-
991
+ stand-in, the true goes on to the parent's mask (ca_mask_standin_settle).
992
+ Only the mask of a CAMeld has stand-ins. */
993
+ /* After a write of n cells of `data` to the mask of a CAMeld: a true
994
+ that went into a stand-in goes on to its parent's mask. */
1027
995
  static void
1028
- ca_meld_mask_refuse_first (ca_meld_put_t *a)
996
+ ca_meld_mask_adopt_before (void *ap, const void *data, ca_size_t n)
1029
997
  {
1030
- CAMeld *o = (CAMeld *) ((CAMeld *) a->ap)->mask_owner;
1031
- if ( o != NULL ) {
1032
- ca_multi_parent_mask_refuse_first((CArray *) a->ap, o->parents,
1033
- o->mask_standins, o->n_parents,
1034
- ca_meld_put_body, (VALUE) a);
998
+ CAMeld *ca = (CAMeld *) ap;
999
+ CAMeld *o = (CAMeld *) ca->mask_owner;
1000
+ if ( o != NULL && ca_mask_any_true(data, n) ) {
1001
+ ca_multi_parent_mask_adopt((CArray *) ca, o->parents, o->mask_standins,
1002
+ o->n_parents);
1035
1003
  }
1036
1004
  }
1037
1005
 
1038
- static void
1039
- ca_meld_mask_refuse_sync (CAMeld *ca)
1040
- {
1041
- ca_meld_put_t a = { CA_MELD_PUT_SYNC, ca, NULL, 0, NULL, NULL, NULL, NULL, NULL };
1042
- ca_meld_mask_refuse_first(&a);
1043
- }
1044
-
1045
- /* After a write of n cells of `data` to the mask of a CAMeld: a true
1046
- that went into a stand-in goes on to its parent's mask. */
1047
1006
  static void
1048
1007
  ca_meld_mask_settle_after (CAMeld *ca, const void *data, ca_size_t n)
1049
1008
  {
@@ -1055,10 +1014,7 @@ ca_meld_mask_settle_after (CAMeld *ca, const void *data, ca_size_t n)
1055
1014
  static void
1056
1015
  ca_meld_put_xfer_index (void *ap, ca_size_t *idx, void *data, int dir)
1057
1016
  {
1058
- if ( dir == CA_XFER_PUT ) {
1059
- ca_meld_put_t a = { CA_MELD_PUT_INDEX, ap, idx, 0, NULL, NULL, NULL, NULL, data };
1060
- ca_meld_mask_refuse_first(&a);
1061
- }
1017
+ if ( dir == CA_XFER_PUT ) ca_meld_mask_adopt_before(ap, data, 1);
1062
1018
  ca_meld_func_xfer_index(ap, idx, data, dir);
1063
1019
  if ( dir == CA_XFER_PUT ) ca_meld_mask_settle_after((CAMeld *) ap, data, 1);
1064
1020
  }
@@ -1068,8 +1024,9 @@ ca_meld_put_xfer_addrs (void *ap, ca_size_t n, ca_size_t *addrs, void *data,
1068
1024
  int dir)
1069
1025
  {
1070
1026
  if ( dir == CA_XFER_PUT ) {
1071
- ca_meld_put_t a = { CA_MELD_PUT_ADDRS, ap, NULL, n, addrs, NULL, NULL, NULL, data };
1072
- ca_meld_mask_refuse_first(&a);
1027
+ CA_MULTI_PARENT_VALUE_TRIAL(ap, t,
1028
+ ca_meld_func_xfer_addrs(t, n, addrs, data, dir));
1029
+ ca_meld_mask_adopt_before(ap, data, n);
1073
1030
  }
1074
1031
  ca_meld_func_xfer_addrs(ap, n, addrs, data, dir);
1075
1032
  if ( dir == CA_XFER_PUT ) ca_meld_mask_settle_after((CAMeld *) ap, data, n);
@@ -1080,8 +1037,10 @@ ca_meld_put_xfer_stride (void *ap, ca_size_t *starts, ca_size_t *counts,
1080
1037
  ca_size_t *strides, void *data, int dir)
1081
1038
  {
1082
1039
  if ( dir == CA_XFER_PUT ) {
1083
- ca_meld_put_t a = { CA_MELD_PUT_STRIDE, ap, NULL, 0, NULL, starts, counts, strides, data };
1084
- ca_meld_mask_refuse_first(&a);
1040
+ CA_MULTI_PARENT_VALUE_TRIAL(ap, t,
1041
+ ca_meld_func_xfer_stride(t, starts, counts, strides, data, dir));
1042
+ ca_meld_mask_adopt_before(ap, data,
1043
+ ca_shape_elements(((CArray *) ap)->ndim, counts, 1));
1085
1044
  }
1086
1045
  ca_meld_func_xfer_stride(ap, starts, counts, strides, data, dir);
1087
1046
  if ( dir == CA_XFER_PUT ) {
@@ -1094,8 +1053,8 @@ static void
1094
1053
  ca_meld_put_xfer_all (void *ap, void *data, int dir)
1095
1054
  {
1096
1055
  if ( dir == CA_XFER_PUT ) {
1097
- ca_meld_put_t a = { CA_MELD_PUT_ALL, ap, NULL, 0, NULL, NULL, NULL, NULL, data };
1098
- ca_meld_mask_refuse_first(&a);
1056
+ CA_MULTI_PARENT_VALUE_TRIAL(ap, t, ca_meld_func_xfer_all(t, data, dir));
1057
+ ca_meld_mask_adopt_before(ap, data, ((CArray *) ap)->elements);
1099
1058
  }
1100
1059
  ca_meld_func_xfer_all(ap, data, dir);
1101
1060
  if ( dir == CA_XFER_PUT ) {
@@ -1106,23 +1065,17 @@ ca_meld_put_xfer_all (void *ap, void *data, int dir)
1106
1065
  static void
1107
1066
  ca_meld_put_fill_data (void *ap, void *ptr)
1108
1067
  {
1109
- {
1110
- ca_meld_put_t a = { CA_MELD_PUT_FILL, ap, NULL, 0, NULL, NULL, NULL, NULL, ptr };
1111
- ca_meld_mask_refuse_first(&a);
1112
- }
1068
+ CA_MULTI_PARENT_VALUE_TRIAL(ap, t, ca_meld_func_fill_data(t, ptr));
1069
+ ca_meld_mask_adopt_before(ap, ptr, 1);
1113
1070
  ca_meld_func_fill_data(ap, ptr);
1114
1071
  ca_meld_mask_settle_after((CAMeld *) ap, ptr, 1);
1115
1072
  }
1116
1073
 
1117
- /* A fill of part of the mask goes cell by cell through xfer_index; run
1118
- the whole of it against blank masks first, so that a cell refused late
1119
- does not leave the cells before it written. */
1120
1074
  static void
1121
1075
  ca_meld_put_fill_addrs (void *ap, ca_size_t n, ca_size_t *addrs, void *ptr)
1122
1076
  {
1123
- ca_meld_put_t a = { CA_MELD_PUT_FILL_ADDRS, ap, NULL, n, addrs, NULL, NULL,
1124
- NULL, ptr };
1125
- ca_meld_mask_refuse_first(&a);
1077
+ CA_MULTI_PARENT_VALUE_TRIAL(ap, t, ca_fill_addrs_default(t, n, addrs, ptr));
1078
+ ca_meld_mask_adopt_before(ap, ptr, 1);
1126
1079
  ca_fill_addrs_default(ap, n, addrs, ptr);
1127
1080
  ca_meld_mask_settle_after((CAMeld *) ap, ptr, 1);
1128
1081
  }
@@ -1131,9 +1084,9 @@ static void
1131
1084
  ca_meld_put_fill_stride (void *ap, ca_size_t base, int8_t ndim,
1132
1085
  ca_size_t *counts, ca_size_t *steps, void *ptr)
1133
1086
  {
1134
- ca_meld_put_t a = { CA_MELD_PUT_FILL_STRIDE, ap, NULL, 0, NULL, NULL, counts,
1135
- NULL, ptr, base, ndim, steps };
1136
- ca_meld_mask_refuse_first(&a);
1087
+ CA_MULTI_PARENT_VALUE_TRIAL(ap, t,
1088
+ ca_fill_stride_default(t, base, ndim, counts, steps, ptr));
1089
+ ca_meld_mask_adopt_before(ap, ptr, 1);
1137
1090
  ca_fill_stride_default(ap, base, ndim, counts, steps, ptr);
1138
1091
  ca_meld_mask_settle_after((CAMeld *) ap, ptr, 1);
1139
1092
  }
@@ -1176,9 +1129,10 @@ rb_ca_meld_new (VALUE parents_ary, int8_t meld_axis)
1176
1129
  }
1177
1130
  parents = ALLOCV_N(CArray *, holder, n);
1178
1131
  for ( i = 0; i < n; i++ ) {
1179
- VALUE p = rb_ary_entry(parents_ary, i);
1132
+ volatile VALUE p = rb_ary_entry(parents_ary, i);
1180
1133
  rb_check_carray_object(p);
1181
1134
  TypedData_Get_Struct(p, CArray, &carray_data_type, parents[i]);
1135
+ ca_multi_parent_check_part(parents[i], "meld");
1182
1136
  }
1183
1137
  ca = ca_meld_new((int32_t) n, parents, meld_axis);
1184
1138
  obj = ca_wrap_struct(ca);
@@ -1231,7 +1185,7 @@ rb_ca_meld_initialize (int argc, VALUE *argv, VALUE self)
1231
1185
  }
1232
1186
  if ( ! NIL_P(axis_val) ) {
1233
1187
  CArray *ref;
1234
- VALUE first = rb_ary_entry(list, 0);
1188
+ volatile VALUE first = rb_ary_entry(list, 0);
1235
1189
  rb_check_carray_object(first);
1236
1190
  TypedData_Get_Struct(first, CArray, &carray_data_type, ref);
1237
1191
  meld_axis = (int8_t) ca_normalize_axis_arg(
@@ -1241,9 +1195,10 @@ rb_ca_meld_initialize (int argc, VALUE *argv, VALUE self)
1241
1195
  ca_check_uninitialized(ca);
1242
1196
  parents = ALLOCV_N(CArray *, holder, n);
1243
1197
  for ( i = 0; i < n; i++ ) {
1244
- VALUE p = rb_ary_entry(list, i);
1198
+ volatile VALUE p = rb_ary_entry(list, i);
1245
1199
  rb_check_carray_object(p);
1246
1200
  TypedData_Get_Struct(p, CArray, &carray_data_type, parents[i]);
1201
+ ca_multi_parent_check_part(parents[i], "meld");
1247
1202
  }
1248
1203
  ca_meld_setup(ca, (int32_t) n, parents, meld_axis);
1249
1204
  rb_ivar_set(self, id_parents, rb_ary_dup(list));
@@ -1339,6 +1294,7 @@ Init_ca_obj_meld (void)
1339
1294
  &cameld_data_type,
1340
1295
  rb_cCAMeldMask,
1341
1296
  &cameld_mask_data_type, &ca_meld_func, sizeof(ca_meld_func));
1297
+ ca_register_cell_moving_view(CA_OBJ_MELD);
1342
1298
  rb_define_const(rb_cObject, "CA_OBJ_MELD", INT2NUM(CA_OBJ_MELD));
1343
1299
 
1344
1300
  id_parents = rb_intern("parents");
data/ext/ca_obj_moncmp.c CHANGED
@@ -227,9 +227,11 @@ ca_moncmp_try_leaf_inplace (CArray *p,
227
227
 
228
228
  /* Evaluate the cells a request names into `data` (packed, n cells). */
229
229
  static void
230
- ca_moncmp_eval (CAMonCmp *mc, const ca_lazy_req_t *req, void *data)
230
+ ca_moncmp_eval_cells (void *node, const ca_lazy_req_t *req, void *data, int dir)
231
231
  {
232
+ CAMonCmp *mc = (CAMonCmp *) node;
232
233
  ca_size_t slab_n = req->n;
234
+ (void) dir;
233
235
  void *scratch;
234
236
  ca_size_t operand_bytes;
235
237
  int is_contig;
@@ -295,6 +297,12 @@ ca_moncmp_eval (CAMonCmp *mc, const ca_lazy_req_t *req, void *data)
295
297
  if ( ! scratch_is_inplace ) ca_lazy_arena_release(scratch);
296
298
  }
297
299
 
300
+ static void
301
+ ca_moncmp_eval (CAMonCmp *mc, const ca_lazy_req_t *req, void *data)
302
+ {
303
+ ca_lazy_eval_recorded(mc, 1, &mc->parent, 0, NULL, ca_moncmp_eval_cells, req, data, CA_XFER_GET);
304
+ }
305
+
298
306
  static void
299
307
  ca_moncmp_func_xfer_stride (void *ap, ca_size_t *starts, ca_size_t *counts,
300
308
  ca_size_t *strides, void *data, int dir)