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_refer.c CHANGED
@@ -16,6 +16,7 @@
16
16
  #include "carray.h"
17
17
  #include "carray_internal.h" /* per-obj_type view constructors */
18
18
  #include "ca_obj_face.h" /* CA_FACE_LIFT_IF_FACE for CARecord parent */
19
+ #include "ruby/thread_native.h"
19
20
 
20
21
  extern ca_operation_function_t ca_stride_func;
21
22
 
@@ -145,9 +146,10 @@ ca_refer_setup (CARefer *ca, CArray *parent,
145
146
  ca_stride_setup_view((CAStride *) ca, CA_OBJ_REFER, parent,
146
147
  data_type, bytes, ndim, dim, strides, base_offset);
147
148
 
148
- /* A refer of a scalar stays one only while it has the one cell: read as
149
- narrower elements it is an array of several. */
150
- if (ca_is_scalar(parent) && ca->elements == 1) {
149
+ /* A refer of a scalar stays one only while it is one cell along one
150
+ axis, as a scalar is: read as narrower elements it is an array of
151
+ several, and read under another shape it is an array of that shape. */
152
+ if (ca_is_scalar(parent) && ca->elements == 1 && ca->ndim == 1) {
151
153
  ca_set_flag(ca, CA_FLAG_SCALAR);
152
154
  }
153
155
  return 0;
@@ -336,6 +338,139 @@ ca_keep_chain_flags (CArray *view, CArray *src)
336
338
  }
337
339
  }
338
340
 
341
+ /* The sources of folded views (CA_FLAG_FOLDED), keyed by the view. Views are
342
+ made and freed in any Ractor, so the table is taken under a lock.
343
+
344
+ st_insert can grow the table through ruby_xmalloc, which can run a GC, and
345
+ the GC frees folded views through ca_fold_forget. ca_fold_forget therefore
346
+ never waits for the lock: when it is held (by the inserting thread itself,
347
+ or by a Ractor stopped for this GC) the view is put on a list instead, and
348
+ whoever takes the lock next removes the listed views first. A freed view's
349
+ address can be reused by a new view only once that view is recorded, which
350
+ takes the lock and so empties the list first. */
351
+ static st_table *ca_fold_sources;
352
+ static rb_nativethread_lock_t ca_fold_lock;
353
+
354
+ typedef struct ca_fold_gone {
355
+ struct ca_fold_gone *next;
356
+ st_data_t key;
357
+ } ca_fold_gone_t;
358
+
359
+ static ca_fold_gone_t *ca_fold_gone_list = NULL;
360
+
361
+ /* With the lock held: remove the views freed while it was held elsewhere.
362
+ The list is allocated with malloc, not xmalloc: it is grown inside a GC. */
363
+ static void
364
+ ca_fold_forget_gone (void)
365
+ {
366
+ ca_fold_gone_t *g = __atomic_exchange_n(&ca_fold_gone_list, NULL, __ATOMIC_ACQ_REL);
367
+ while ( g ) {
368
+ ca_fold_gone_t *next = g->next;
369
+ st_data_t key = g->key;
370
+ st_delete(ca_fold_sources, &key, NULL);
371
+ free(g);
372
+ g = next;
373
+ }
374
+ }
375
+
376
+ static void
377
+ ca_fold_lock_table (void)
378
+ {
379
+ ca_native_lock_without_gvl(&ca_fold_lock);
380
+ ca_fold_forget_gone();
381
+ }
382
+
383
+ typedef struct {
384
+ CArray *view;
385
+ CArray *src;
386
+ } ca_fold_note_arg_t;
387
+
388
+ static VALUE
389
+ ca_fold_insert (VALUE varg)
390
+ {
391
+ ca_fold_note_arg_t *a = (ca_fold_note_arg_t *) varg;
392
+ st_insert(ca_fold_sources, (st_data_t) a->view, (st_data_t) a->src);
393
+ return Qnil;
394
+ }
395
+
396
+ /* The growth of the table can raise NoMemoryError; the lock is released
397
+ before that raise goes on. */
398
+ void
399
+ ca_fold_note_source (CArray *view, CArray *src)
400
+ {
401
+ ca_fold_note_arg_t arg = { view, src };
402
+ int state = 0;
403
+ /* lock: st_insert can run a GC; a view it frees is listed, not waited for */
404
+ ca_fold_lock_table();
405
+ /* protect: releases a native lock; no array is held */
406
+ rb_protect(ca_fold_insert, (VALUE) &arg, &state);
407
+ rb_native_mutex_unlock(&ca_fold_lock);
408
+ if ( state ) {
409
+ rb_jump_tag(state);
410
+ }
411
+ ca_set_flag(view, CA_FLAG_FOLDED);
412
+ }
413
+
414
+ CArray *
415
+ ca_fold_source (CArray *view)
416
+ {
417
+ st_data_t src = 0;
418
+ ca_fold_lock_table();
419
+ st_lookup(ca_fold_sources, (st_data_t) view, &src);
420
+ rb_native_mutex_unlock(&ca_fold_lock);
421
+ return (CArray *) src;
422
+ }
423
+
424
+ void
425
+ ca_fold_forget (CArray *view)
426
+ {
427
+ st_data_t key = (st_data_t) view;
428
+ ca_fold_gone_t *g;
429
+ if ( rb_native_mutex_trylock(&ca_fold_lock) == 0 ) {
430
+ ca_fold_forget_gone();
431
+ st_delete(ca_fold_sources, &key, NULL);
432
+ rb_native_mutex_unlock(&ca_fold_lock);
433
+ return;
434
+ }
435
+ g = (ca_fold_gone_t *) malloc(sizeof(*g));
436
+ if ( g == NULL ) {
437
+ /* No memory for the list: wait for the lock, as there is no other way
438
+ to drop the record. */
439
+ /* lock: only with no memory for the list; the wait may block the GC */
440
+ rb_native_mutex_lock(&ca_fold_lock);
441
+ ca_fold_forget_gone();
442
+ st_delete(ca_fold_sources, &key, NULL);
443
+ rb_native_mutex_unlock(&ca_fold_lock);
444
+ return;
445
+ }
446
+ g->key = key;
447
+ g->next = __atomic_load_n(&ca_fold_gone_list, __ATOMIC_ACQUIRE);
448
+ while ( ! __atomic_compare_exchange_n(&ca_fold_gone_list, &g->next, g, 0,
449
+ __ATOMIC_RELEASE, __ATOMIC_ACQUIRE) ) {
450
+ /* g->next now holds the head that won; try again on top of it */
451
+ }
452
+ }
453
+
454
+ /* Builds the folded view of `ca` for reshape / flatten: a CAStride over
455
+ `root`. When `ca` is not `root` itself, it is recorded as the view's
456
+ source. */
457
+ static VALUE
458
+ ca_reshape_folded (VALUE self, CArray *ca, CArray *root, int8_t ndim,
459
+ ca_size_t *dim, ca_size_t *strides, ca_size_t base)
460
+ {
461
+ volatile VALUE obj;
462
+ CAStride *cs = ca_stride_new(CA_OBJ_STRIDE, root, ca->data_type, ca->bytes,
463
+ ndim, dim, strides, base);
464
+ ca_keep_chain_flags((CArray *) cs, ca);
465
+ obj = ca_wrap_struct(cs);
466
+ rb_ca_set_parent(obj, self);
467
+ if ( root != ca ) {
468
+ ca_fold_note_source((CArray *) cs, ca);
469
+ }
470
+ CA_WRAPPER_LIFT(obj, self, ca);
471
+ return obj;
472
+ }
473
+
339
474
  static int
340
475
  ca_reshape_try_strides (CArray *parent,
341
476
  const ca_size_t *new_dim,
@@ -502,11 +637,25 @@ rb_ca_refer_kw (int argc, VALUE *argv, VALUE ropt, VALUE self)
502
637
  rdim = rb_ca_dim(self);
503
638
  }
504
639
  }
640
+ if ( ca_is_face(ca) && ! rb_obj_is_data_class(rtype)
641
+ && ( data_type == CA_OBJECT || ca->data_type == CA_FIXLEN ) ) {
642
+ /* A Face's cells do not mean their storage bytes, and the view below
643
+ would be lifted back to a Face reading cells of the wrong width. */
644
+ CArray *storage = ca_strip_face(ca);
645
+ if ( ! ( data_type == ca->data_type && bytes == ca->bytes )
646
+ && ! ( data_type == storage->data_type
647
+ && bytes == storage->bytes ) ) {
648
+ rb_raise(rb_eTypeError,
649
+ "%"PRIsVALUE" has no view of its values in another data_type: "
650
+ "#to_type gives the values, #parent.refer the raw storage",
651
+ rb_obj_class(self));
652
+ }
653
+ }
505
654
  Check_Type(rdim, T_ARRAY);
506
655
  CA_CHECK_RANK_GIVEN(RARRAY_LEN(rdim)); /* before dim[] is filled */
507
656
  ndim = RARRAY_LEN(rdim);
508
- for (i = 0; i < ndim; i++) dim[i] = NUM2SIZE(rb_ary_entry(rdim, i));
509
- if (! NIL_P(roffset)) offset = NUM2SIZE(roffset);
657
+ for (i = 0; i < ndim; i++) dim[i] = ca_integer_arg(rb_ary_entry(rdim, i), "a dimension", NULL);
658
+ if (! NIL_P(roffset)) offset = ca_integer_arg(roffset, "the offset", NULL);
510
659
  /* When rtype is a data_class the result is a record array. It is
511
660
  built on the storage, never on a Face, so that a Face stays on top
512
661
  of the chain: re-reading a record array as its own data_class lifts
@@ -668,7 +817,7 @@ rb_ca_reshape (int argc, VALUE *argv, VALUE self)
668
817
  new_dim[i] = ca->dim[src];
669
818
  }
670
819
  else {
671
- new_dim[i] = NUM2SIZE(argv[i]);
820
+ new_dim[i] = ca_integer_arg(argv[i], "a dimension", "reshape");
672
821
  }
673
822
  /* A product too large to count cannot equal the element count; an
674
823
  extent of zero anywhere makes it zero whatever it overflowed to. */
@@ -721,15 +870,8 @@ rb_ca_reshape (int argc, VALUE *argv, VALUE self)
721
870
  ca_size_t out_base;
722
871
  if (ca_reshape_try_strides(ca, new_dim, (int8_t) argc,
723
872
  &out_root, out_strides, &out_base)) {
724
- CAStride *cs = ca_stride_new(CA_OBJ_STRIDE, out_root,
725
- ca->data_type, ca->bytes,
726
- (int8_t) argc, new_dim,
727
- out_strides, out_base);
728
- ca_keep_chain_flags((CArray *) cs, ca);
729
- obj = ca_wrap_struct(cs);
730
- rb_ca_set_parent(obj, self);
731
- CA_WRAPPER_LIFT(obj, self, ca);
732
- return obj;
873
+ return ca_reshape_folded(self, ca, out_root, (int8_t) argc, new_dim,
874
+ out_strides, out_base);
733
875
  }
734
876
  }
735
877
 
@@ -760,14 +902,8 @@ rb_ca_flatten (VALUE self)
760
902
  ca_size_t out_strides[1];
761
903
  ca_size_t out_base;
762
904
  if (ca_reshape_try_strides(ca, dim, 1, &out_root, out_strides, &out_base)) {
763
- CAStride *cs = ca_stride_new(CA_OBJ_STRIDE, out_root,
764
- ca->data_type, ca->bytes,
765
- 1, dim, out_strides, out_base);
766
- ca_keep_chain_flags((CArray *) cs, ca);
767
- obj = ca_wrap_struct(cs);
768
- rb_ca_set_parent(obj, self);
769
- CA_WRAPPER_LIFT(obj, self, ca);
770
- return obj;
905
+ return ca_reshape_folded(self, ca, out_root, 1, dim,
906
+ out_strides, out_base);
771
907
  }
772
908
  }
773
909
 
@@ -847,6 +983,9 @@ Init_ca_obj_refer (void)
847
983
  /* rb_cCARefer, CA_OBJ_REFER are defined in ruby_carray.c /
848
984
  carray_core.c. Build the custom op table and override the
849
985
  baseline registration installed by Init_carray_core. */
986
+ ca_fold_sources = st_init_numtable();
987
+ rb_native_mutex_initialize(&ca_fold_lock);
988
+
850
989
  ca_refer_func = ca_stride_func;
851
990
  ca_refer_func.free_object = free_ca_refer;
852
991
  ca_refer_func.clone = ca_refer_func_clone;
data/ext/ca_obj_remap.c CHANGED
@@ -304,8 +304,9 @@ ca_remap_func_xfer_addrs (void *ap, ca_size_t n, ca_size_t *addrs,
304
304
 
305
305
  paddrs = ALLOCV_N(ca_size_t, holder, n);
306
306
  ca_xfer_addrs(ca->idx, n, addrs, paddrs, CA_XFER_GET);
307
- /* idx is read live, so its values are checked here, not at
308
- construction: negative counts from the end, out of range raises
307
+ /* idx is read-only once the view holds it, but its values are still
308
+ checked on every transfer (the private without_read_only_flag can
309
+ lift the flag): negative counts from the end, out of range raises
309
310
  before the parent is touched. */
310
311
  for ( i = 0; i < n; i++ ) {
311
312
  CA_CHECK_INDEX(paddrs[i], ca->parent->elements);
@@ -366,9 +367,19 @@ ca_remap_func_xfer_stride (void *ap, ca_size_t *starts, ca_size_t *counts,
366
367
  ALLOCV_END(holder);
367
368
  }
368
369
 
369
- /* xfer_all: whole-view delivery. Sequential view-flat addresses
370
- [0..elements) routed through xfer_addrs -- one ALLOCV for the addr
371
- list, then two batched ca_xfer_addrs calls inside xfer_addrs.
370
+ /* xfer_all: whole-view delivery. The whole view reads every cell of idx
371
+ in order, so when idx holds its cells in memory those cells are the
372
+ parent addresses: they are checked in one pass, a negative one counting
373
+ from the end of the parent.
374
+
375
+ idx itself is handed to the parent only when the parent holds its cells
376
+ in memory and shares no root with idx: that transfer is a plain copy, so
377
+ no Ruby runs while the parent reads the list and a scatter cannot
378
+ rewrite it. Any other parent (a view, a CAObject whose hooks run Ruby)
379
+ gets a copy of the addresses taken in the checking pass, so nothing it
380
+ does can change an address after it was checked, or change idx.
381
+ Without idx in memory, the sequential view-flat addresses [0..elements)
382
+ go through xfer_addrs.
372
383
 
373
384
  No ca_attach(parent) / ca_attach(idx). */
374
385
  static void
@@ -382,6 +393,46 @@ ca_remap_func_xfer_all (void *ap, void *data, int dir)
382
393
 
383
394
  if ( n == 0 ) return;
384
395
 
396
+ if ( ca->idx->ptr ) {
397
+ ca_size_t *iaddrs = (ca_size_t *) ca->idx->ptr;
398
+ ca_size_t pn = ca->parent->elements;
399
+ ca_size_t *paddrs;
400
+
401
+ if ( ca->parent->ptr && ! ca_shares_root(ca->parent, ca->idx) ) {
402
+ int lendable = 1;
403
+ for ( i = 0; i < n; i++ ) {
404
+ ca_size_t a = iaddrs[i];
405
+ if ( a < 0 ) {
406
+ lendable = 0;
407
+ a += pn;
408
+ }
409
+ if ( a < 0 || a >= pn ) {
410
+ ca_index_out_of_range(iaddrs[i], pn, "index");
411
+ }
412
+ }
413
+ if ( lendable ) {
414
+ ca_xfer_addrs(ca->parent, n, iaddrs, data, dir);
415
+ return;
416
+ }
417
+ }
418
+
419
+ paddrs = ALLOCV_N(ca_size_t, holder, n);
420
+ for ( i = 0; i < n; i++ ) {
421
+ ca_size_t a = iaddrs[i];
422
+ if ( a < 0 ) {
423
+ a += pn;
424
+ }
425
+ if ( a < 0 || a >= pn ) {
426
+ ALLOCV_END(holder);
427
+ ca_index_out_of_range(iaddrs[i], pn, "index");
428
+ }
429
+ paddrs[i] = a;
430
+ }
431
+ ca_xfer_addrs(ca->parent, n, paddrs, data, dir);
432
+ ALLOCV_END(holder);
433
+ return;
434
+ }
435
+
385
436
  vaddrs = ALLOCV_N(ca_size_t, holder, n);
386
437
  for ( i = 0; i < n; i++ ) vaddrs[i] = i;
387
438
  ca_remap_func_xfer_addrs(ca, n, vaddrs, data, dir);
@@ -417,7 +468,13 @@ ca_operation_function_t ca_remap_func = {
417
468
  carray_partition.c). Callers are expected to have already checked the
418
469
  routing preconditions (same shape, idx data_type == CA_SIZE);
419
470
  ca_remap_setup raises ArgumentError on mismatch as a defensive
420
- fallback. */
471
+ fallback.
472
+
473
+ The view takes rmapper over: every caller hands a fresh array no one
474
+ else holds, and it is marked read-only here, so the mapping a view was
475
+ made with cannot be rewritten through #mapper. CAGrid and CASelect
476
+ keep their indices where Ruby cannot reach them; this is the same
477
+ promise for an index array that stays visible. */
421
478
  VALUE
422
479
  rb_ca_remap_new (VALUE cary, VALUE rmapper)
423
480
  {
@@ -437,13 +494,14 @@ rb_ca_remap_new (VALUE cary, VALUE rmapper)
437
494
  covers the view's. ca->idx is a raw pointer into idx's struct;
438
495
  keeping rmapper reachable through the view keeps it alive. */
439
496
  rb_ivar_set(obj, rb_intern("remap_idx"), rmapper);
497
+ rb_ca_set_read_only_flag(rmapper);
440
498
  return obj;
441
499
  }
442
500
 
443
- /* Read-only accessor for the per-cell index CArray that defines this
444
- CARemap's mapping: the array the view reads. For ca[mapper] it is a
445
- copy of mapper taken at the call, so a later change to mapper does
446
- not reach the view. Companion to
501
+ /* Accessor for the per-cell index CArray that defines this CARemap's
502
+ mapping: the array the view reads, itself read-only. For ca[mapper]
503
+ it is a copy of mapper taken at the call, so a later change to mapper
504
+ does not reach the view. Companion to
447
505
  CABlock#start / CAStride#strides / CAWindow#count etc. — the view
448
506
  hierarchy is preserved so each named view class exposes its own
449
507
  defining semantic state. */
@@ -526,6 +584,7 @@ Init_ca_obj_remap (void)
526
584
  rb_cCARemapMask,
527
585
  &caremap_mask_data_type,
528
586
  &ca_remap_func, sizeof(ca_remap_func));
587
+ ca_register_cell_moving_view(CA_OBJ_REMAP);
529
588
 
530
589
  /* Defining semantic state accessor — companion to CABlock#start /
531
590
  CAStride#strides / CAWindow#count. The hierarchy-preserving
data/ext/ca_obj_repeat.c CHANGED
@@ -233,7 +233,7 @@ rb_ca_repeat (int argc, VALUE *argv, VALUE self)
233
233
  }
234
234
  }
235
235
  else {
236
- count[i] = NUM2SIZE(argv[i]);
236
+ count[i] = ca_integer_arg(argv[i], "a repeat count", "repeat");
237
237
  if ( count[i] == 0 ) {
238
238
  rb_raise(rb_eArgError,
239
239
  "zero repeat count specified in creating CARepeat object");
data/ext/ca_obj_roll.c CHANGED
@@ -525,16 +525,14 @@ static void
525
525
  ca_roll_func_allocate (void *ap)
526
526
  {
527
527
  CARoll *ca = (CARoll *) ap;
528
- ca_attach(ca->parent);
529
- ca->ptr = ca_alloc_cells((CArray *) ca);
528
+ ca->ptr = ca_alloc_cells_then_attach((CArray *) ca, 1, &ca->parent);
530
529
  }
531
530
 
532
531
  static void
533
532
  ca_roll_func_attach (void *ap)
534
533
  {
535
534
  CARoll *ca = (CARoll *) ap;
536
- ca_attach(ca->parent);
537
- ca->ptr = ca_alloc_cells((CArray *) ca);
535
+ ca->ptr = ca_alloc_cells_then_attach((CArray *) ca, 1, &ca->parent);
538
536
  ca_roll_iterate_regions(ca, ca->ptr, 0);
539
537
  }
540
538
 
@@ -733,6 +731,7 @@ Init_ca_obj_roll (void)
733
731
  &caroll_data_type,
734
732
  rb_cCARollMask,
735
733
  &caroll_mask_data_type, &ca_roll_func, sizeof(ca_roll_func));
734
+ ca_register_cell_moving_view(CA_OBJ_ROLL);
736
735
  rb_define_const(rb_cObject, "CA_OBJ_ROLL", INT2NUM(CA_OBJ_ROLL));
737
736
 
738
737
  rb_define_method(rb_cCArray, "roll", rb_ca_roll, -1);
data/ext/ca_obj_select.c CHANGED
@@ -205,7 +205,9 @@ ca_select_setup (CASelect *ca, CArray *parent, CArray *snapshot)
205
205
  ca->dim = &(ca->_dim);
206
206
  ca->dim[0] = ca->elements;
207
207
 
208
- if ( ca_is_scalar(snapshot) ) {
208
+ /* A selection of a scalar is one only while it picks the cell: a scalar
209
+ is one cell along one axis. */
210
+ if ( ca_is_scalar(snapshot) && ca->elements == 1 ) {
209
211
  ca_set_flag(ca, CA_FLAG_SCALAR);
210
212
  }
211
213
 
@@ -463,9 +465,11 @@ ca_select_func_allocate (void *ap)
463
465
  {
464
466
  CASelect *ca = (CASelect *) ap;
465
467
  if ( ! ca_parent_lends_no_memory(ca->parent) ) {
466
- ca_attach(ca->parent);
468
+ ca->ptr = ca_alloc_cells_then_attach((CArray *) ca, 1, &ca->parent);
469
+ }
470
+ else {
471
+ ca->ptr = ca_alloc_cells((CArray *) ca);
467
472
  }
468
- ca->ptr = ca_alloc_cells((CArray *) ca);
469
473
  }
470
474
 
471
475
  static void
@@ -480,10 +484,10 @@ ca_select_func_attach (void *ap)
480
484
  return;
481
485
  }
482
486
 
483
- ca_attach(ca->parent);
487
+ ca->ptr = ca_alloc_cells_then_attach((CArray *) ca, 1, &ca->parent);
484
488
  ca_select_describe_axes(ca, desc, pdims);
485
- ca->ptr = ca_axis_dispatch_attach(ca->parent, pdims, desc, ca->ndim,
486
- ca->bytes, ca->elements, NULL);
489
+ ca_axis_dispatch_gather(ca->parent, pdims, desc, ca->ndim,
490
+ ca->bytes, ca->elements, NULL, ca->ptr);
487
491
  }
488
492
 
489
493
  static void
@@ -598,7 +602,7 @@ ca_operation_function_t ca_select_func = {
598
602
  /* ------------------------------------------------------------------- */
599
603
 
600
604
  VALUE
601
- rb_ca_select_new (VALUE cary, VALUE select)
605
+ rb_ca_select_new (VALUE cary, volatile VALUE select)
602
606
  {
603
607
  volatile VALUE obj;
604
608
  CArray *parent, *cselect;
@@ -668,6 +672,7 @@ rb_cm_initialize_copy (VALUE self, VALUE other)
668
672
  void
669
673
  Init_ca_obj_select (void)
670
674
  {
675
+ ca_register_cell_moving_view(CA_OBJ_SELECT);
671
676
  /* rb_cCASelect and CA_OBJ_SELECT are defined in ruby_carray.c;
672
677
  this file only registers the constant + allocator. */
673
678
  rb_define_const(rb_cObject, "CA_OBJ_SELECT", INT2NUM(CA_OBJ_SELECT));
@@ -726,9 +726,11 @@ ca_select_axis_func_allocate (void *ap)
726
726
  {
727
727
  CASelectAxis *ca = (CASelectAxis *) ap;
728
728
  if ( ! ca_parent_lends_no_memory(ca->parent) ) {
729
- ca_attach(ca->parent);
729
+ ca->ptr = ca_alloc_cells_then_attach((CArray *) ca, 1, &ca->parent);
730
+ }
731
+ else {
732
+ ca->ptr = ca_alloc_cells((CArray *) ca);
730
733
  }
731
- ca->ptr = ca_alloc_cells((CArray *) ca);
732
734
  }
733
735
 
734
736
  static void
@@ -743,10 +745,10 @@ ca_select_axis_func_attach (void *ap)
743
745
  return;
744
746
  }
745
747
 
746
- ca_attach(ca->parent);
748
+ ca->ptr = ca_alloc_cells_then_attach((CArray *) ca, 1, &ca->parent);
747
749
  ca_select_axis_describe_axes(ca, desc, pdims);
748
- ca->ptr = ca_axis_dispatch_attach(ca->parent, pdims, desc, ca->ndim,
749
- ca->bytes, ca->elements, NULL);
750
+ ca_axis_dispatch_gather(ca->parent, pdims, desc, ca->ndim,
751
+ ca->bytes, ca->elements, NULL, ca->ptr);
750
752
  }
751
753
 
752
754
  static void
@@ -1008,7 +1010,7 @@ rb_ca_select_axis (int argc, VALUE *argv, VALUE self)
1008
1010
  }
1009
1011
 
1010
1012
  for ( i = 0; i < argc; i++ ) {
1011
- VALUE arg = argv[i];
1013
+ volatile VALUE arg = argv[i];
1012
1014
 
1013
1015
  if ( ! rb_obj_is_carray(arg) ) {
1014
1016
  ca_index_axis_key(arg, parent->dim[i], i,
@@ -1238,6 +1240,7 @@ Init_ca_obj_select_axis (void)
1238
1240
  rb_cCASelectAxisMask,
1239
1241
  &caselectaxis_mask_data_type,
1240
1242
  &ca_select_axis_func, sizeof(ca_select_axis_func));
1243
+ ca_register_cell_moving_view(CA_OBJ_SELECT_AXIS);
1241
1244
  rb_define_const(rb_cObject, "CA_OBJ_SELECT_AXIS", INT2NUM(CA_OBJ_SELECT_AXIS));
1242
1245
 
1243
1246
  rb_define_alloc_func(rb_cCASelectAxis, rb_ca_select_axis_s_allocate);
data/ext/ca_obj_shift.c CHANGED
@@ -363,6 +363,7 @@ Init_ca_obj_shift (void)
363
363
  &cashift_data_type,
364
364
  rb_cCAShiftMask,
365
365
  &cashift_mask_data_type, &ca_shift_func, sizeof(ca_shift_func));
366
+ ca_register_cell_moving_view(CA_OBJ_SHIFT);
366
367
  rb_define_const(rb_cObject, "CA_OBJ_SHIFT", INT2NUM(CA_OBJ_SHIFT));
367
368
 
368
369
  rb_define_method(rb_cCArray, "shift", rb_ca_shift, -1);