carray 3.0.6 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (277) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +1 -1
  3. data/CHANGELOG.md +7 -1830
  4. data/README.md +62 -35
  5. data/carray.gemspec +16 -4
  6. data/docs/changelogs/CHANGELOG-3.0.md +2464 -0
  7. data/ext/ca_array_pool.c +4 -5
  8. data/ext/ca_axis_descriptor.h +45 -30
  9. data/ext/ca_axis_dispatch.c +7 -4
  10. data/ext/ca_axis_group.c +12 -15
  11. data/ext/ca_bincmp_dispatch.h +16 -38
  12. data/ext/ca_binop_dispatch.c +35 -0
  13. data/ext/ca_binop_dispatch.h +22 -46
  14. data/ext/ca_categorical_iterator.c +36 -34
  15. data/ext/ca_compare.c +0 -1
  16. data/ext/ca_compare.h +8 -1
  17. data/ext/ca_composite_dispatch.h +6 -0
  18. data/ext/ca_for_buffer.h +6 -1
  19. data/ext/ca_group_iter.c +17 -18
  20. data/ext/ca_iter_state_fields.h +196 -0
  21. data/ext/ca_iter_substrate.h +84 -103
  22. data/ext/ca_kernel_iterator.c +125 -89
  23. data/ext/ca_kernel_iterator.h +229 -1504
  24. data/ext/ca_kernel_iterator_internal.h +1167 -0
  25. data/ext/ca_moncmp_dispatch.h +17 -24
  26. data/ext/ca_monop_dispatch.h +25 -74
  27. data/ext/ca_obj_array.c +92 -165
  28. data/ext/ca_obj_bincmp.c +65 -48
  29. data/ext/ca_obj_binop.c +74 -49
  30. data/ext/ca_obj_bitarray.c +7 -21
  31. data/ext/ca_obj_bitfield.c +93 -26
  32. data/ext/ca_obj_block.c +11 -12
  33. data/ext/ca_obj_byte_swap.c +34 -34
  34. data/ext/ca_obj_const_string.c +464 -304
  35. data/ext/ca_obj_face.c +167 -121
  36. data/ext/ca_obj_face.h +32 -170
  37. data/ext/ca_obj_fake.c +127 -34
  38. data/ext/ca_obj_farray.c +13 -9
  39. data/ext/ca_obj_field.c +17 -13
  40. data/ext/ca_obj_fixlen_string.c +57 -34
  41. data/ext/ca_obj_grid.c +149 -113
  42. data/ext/ca_obj_meld.c +28 -26
  43. data/ext/ca_obj_moncmp.c +10 -21
  44. data/ext/ca_obj_monop.c +88 -73
  45. data/ext/ca_obj_object.c +60 -46
  46. data/ext/ca_obj_record.c +74 -22
  47. data/ext/ca_obj_reduce.c +10 -14
  48. data/ext/ca_obj_refer.c +146 -39
  49. data/ext/ca_obj_remap.c +11 -29
  50. data/ext/ca_obj_repeat.c +41 -23
  51. data/ext/ca_obj_roll.c +20 -28
  52. data/ext/ca_obj_select.c +17 -27
  53. data/ext/ca_obj_select_axis.c +35 -42
  54. data/ext/ca_obj_shift.c +22 -42
  55. data/ext/ca_obj_source.c +1 -5
  56. data/ext/ca_obj_stack.c +33 -29
  57. data/ext/ca_obj_stride.c +99 -62
  58. data/ext/ca_obj_string.c +8 -20
  59. data/ext/ca_obj_tile.c +14 -16
  60. data/ext/ca_obj_time.c +12 -23
  61. data/ext/ca_obj_timedelta.c +12 -23
  62. data/ext/ca_obj_transpose.c +8 -10
  63. data/ext/ca_obj_triop.c +48 -49
  64. data/ext/ca_obj_window.c +87 -99
  65. data/ext/ca_op_cmplx64.h +4 -0
  66. data/ext/ca_op_ipower.c +97 -27
  67. data/ext/ca_op_powi.h +9 -28
  68. data/ext/ca_pow5_table.h +2 -1
  69. data/ext/ca_rng_normal.h +4 -0
  70. data/ext/ca_rng_xoshiro256pp.h +3 -0
  71. data/ext/ca_sort_kernels.h +23 -26
  72. data/ext/ca_sweep_engine.c +92 -26
  73. data/ext/ca_sweep_engine.h +22 -15
  74. data/ext/ca_transform_common.c +3 -8
  75. data/ext/ca_triop_dispatch.h +20 -32
  76. data/ext/caframe_csv_reader.c +12 -12
  77. data/ext/carray.h +300 -1009
  78. data/ext/carray_access.c +320 -207
  79. data/ext/carray_address_basis.c +128 -59
  80. data/ext/carray_attribute.c +81 -104
  81. data/ext/carray_bincount.c +12 -14
  82. data/ext/carray_broadcast.c +31 -27
  83. data/ext/carray_call_cfunc.c +174 -285
  84. data/ext/carray_call_cfunc.h +36 -28
  85. data/ext/carray_cast.c +1191 -351
  86. data/ext/carray_cast_func.rb +68 -8
  87. data/ext/carray_class.c +6 -11
  88. data/ext/carray_conversion.c +98 -39
  89. data/ext/carray_copy.c +52 -26
  90. data/ext/carray_core.c +286 -132
  91. data/ext/carray_count.c +53 -22
  92. data/ext/carray_element.c +49 -28
  93. data/ext/carray_factorize.c +64 -63
  94. data/ext/carray_generate.c +26 -19
  95. data/ext/carray_histogram.c +9 -11
  96. data/ext/carray_hold.c +7 -9
  97. data/ext/carray_index_classifier.c +127 -230
  98. data/ext/carray_index_classifier.h +11 -10
  99. data/ext/carray_integer_total.c +523 -0
  100. data/ext/carray_internal.h +1364 -156
  101. data/ext/carray_key_windows.c +3 -3
  102. data/ext/carray_lazy.c +37 -37
  103. data/ext/carray_loop.c +27 -23
  104. data/ext/carray_mask.c +235 -150
  105. data/ext/carray_math_kernel.h +23 -47
  106. data/ext/carray_mathfunc.c +10 -3
  107. data/ext/carray_median_percentile.c +106 -100
  108. data/ext/carray_memory_view.c +83 -69
  109. data/ext/carray_operator.c +490 -128
  110. data/ext/carray_order.c +163 -84
  111. data/ext/carray_partition.c +14 -16
  112. data/ext/carray_random.c +48 -42
  113. data/ext/carray_round.c +33 -5
  114. data/ext/carray_scatter.c +57 -39
  115. data/ext/carray_segment.c +42 -46
  116. data/ext/carray_slab.c +77 -51
  117. data/ext/carray_slab.h +44 -33
  118. data/ext/carray_sort.c +37 -31
  119. data/ext/carray_sort_kernel.c +36 -10
  120. data/ext/carray_struct.c +44 -34
  121. data/ext/carray_test.c +131 -81
  122. data/ext/carray_time_text.c +30 -31
  123. data/ext/carray_undef.c +50 -7
  124. data/ext/carray_utils.c +373 -139
  125. data/ext/extconf.rb +24 -17
  126. data/ext/mk_call_cfunc.rb +25 -31
  127. data/ext/mk_pow5_table.rb +2 -1
  128. data/ext/mkkernel.rb +819 -488
  129. data/ext/ruby_carray.c +47 -42
  130. data/ext/version.h +7 -5
  131. data/lib/carray/attribute.rb +19 -18
  132. data/lib/carray/autoload_carray.rb +20 -15
  133. data/lib/carray/autoload_method_extension.rb +64 -57
  134. data/lib/carray/axis_group.rb +272 -185
  135. data/lib/carray/basics.rb +298 -56
  136. data/lib/carray/bincount_nd.rb +41 -18
  137. data/lib/carray/block_iterator.rb +167 -119
  138. data/lib/carray/boolean_reduce.rb +7 -4
  139. data/lib/carray/categorical.rb +84 -59
  140. data/lib/carray/categorical_iterator.rb +90 -67
  141. data/lib/carray/complex.rb +61 -41
  142. data/lib/carray/conditional.rb +98 -23
  143. data/lib/carray/const_string.rb +112 -46
  144. data/lib/carray/core_extensions.rb +20 -69
  145. data/lib/carray/distance_limit.rb +7 -5
  146. data/lib/carray/factories.rb +269 -0
  147. data/lib/carray/fixlen_string.rb +95 -12
  148. data/lib/carray/frame/column_verbs.rb +76 -9
  149. data/lib/carray/frame/concat.rb +51 -7
  150. data/lib/carray/frame/convert.rb +2 -1
  151. data/lib/carray/frame/csv_parser.rb +20 -5
  152. data/lib/carray/frame/frame.rb +73 -31
  153. data/lib/carray/frame/group.rb +247 -187
  154. data/lib/carray/frame/io.rb +36 -12
  155. data/lib/carray/frame/join.rb +56 -17
  156. data/lib/carray/frame/records.rb +7 -5
  157. data/lib/carray/frame/reshape.rb +27 -7
  158. data/lib/carray/frame/sort.rb +10 -8
  159. data/lib/carray/frame/verbs.rb +217 -134
  160. data/lib/carray/frame/windows.rb +254 -19
  161. data/lib/carray/frame.rb +1 -2
  162. data/lib/carray/fuse_source.rb +28 -23
  163. data/lib/carray/fusion.rb +30 -30
  164. data/lib/carray/histogram.rb +35 -16
  165. data/lib/carray/inspect.rb +7 -7
  166. data/lib/carray/iterator.rb +104 -23
  167. data/lib/carray/key_windows.rb +93 -61
  168. data/lib/carray/lazy.rb +208 -111
  169. data/lib/carray/limits.rb +80 -0
  170. data/lib/carray/mask_gap_fill.rb +21 -16
  171. data/lib/carray/math.rb +87 -50
  172. data/lib/carray/meld_reduce.rb +30 -8
  173. data/lib/carray/methods/align_addr.rb +29 -17
  174. data/lib/carray/methods/bin.rb +8 -1
  175. data/lib/carray/methods/bincount.rb +4 -2
  176. data/lib/carray/methods/bit_string.rb +19 -31
  177. data/lib/carray/methods/broadcast.rb +8 -3
  178. data/lib/carray/methods/choose.rb +5 -5
  179. data/lib/carray/methods/composition.rb +73 -72
  180. data/lib/carray/methods/{index.rb → coordinate.rb} +13 -13
  181. data/lib/carray/methods/discovery_along.rb +3 -3
  182. data/lib/carray/methods/factorize.rb +2 -0
  183. data/lib/carray/methods/gather_nd.rb +106 -103
  184. data/lib/carray/methods/insert_block.rb +2 -2
  185. data/lib/carray/methods/is_between.rb +6 -4
  186. data/lib/carray/methods/is_in.rb +183 -143
  187. data/lib/carray/methods/join.rb +2 -1
  188. data/lib/carray/methods/lag.rb +6 -2
  189. data/lib/carray/methods/locate_addr.rb +157 -94
  190. data/lib/carray/methods/mask_duplicates.rb +1 -1
  191. data/lib/carray/methods/meshgrid.rb +28 -16
  192. data/lib/carray/methods/mode.rb +67 -67
  193. data/lib/carray/methods/nunique.rb +9 -7
  194. data/lib/carray/methods/repeat.rb +23 -13
  195. data/lib/carray/methods/resize.rb +1 -1
  196. data/lib/carray/methods/snap.rb +24 -20
  197. data/lib/carray/methods/string_format.rb +2 -2
  198. data/lib/carray/methods/unique.rb +4 -2
  199. data/lib/carray/methods/value_counts.rb +1 -1
  200. data/lib/carray/mkmf.rb +25 -28
  201. data/lib/carray/percentile_arguments.rb +82 -0
  202. data/lib/carray/record_order.rb +50 -20
  203. data/lib/carray/rng.rb +1 -0
  204. data/lib/carray/runtime.rb +195 -17
  205. data/lib/carray/segment_iterator.rb +262 -137
  206. data/lib/carray/serialize.rb +635 -566
  207. data/lib/carray/slab_iterator.rb +114 -114
  208. data/lib/carray/stack.rb +14 -15
  209. data/lib/carray/string.rb +16 -22
  210. data/lib/carray/string_operation_extension.rb +97 -52
  211. data/lib/carray/struct.rb +122 -164
  212. data/lib/carray/struct_builder.rb +247 -73
  213. data/lib/carray/time.rb +663 -365
  214. data/lib/carray/window_iterator.rb +232 -155
  215. data/lib/carray.rb +6 -11
  216. data/yard-stubs/ca_obj_array.rb +20 -135
  217. data/yard-stubs/ca_obj_bitarray.rb +1 -8
  218. data/yard-stubs/ca_obj_bitfield.rb +6 -2
  219. data/yard-stubs/ca_obj_byte_swap.rb +21 -7
  220. data/yard-stubs/ca_obj_fake.rb +3 -1
  221. data/yard-stubs/ca_obj_farray.rb +1 -1
  222. data/yard-stubs/ca_obj_field.rb +8 -6
  223. data/yard-stubs/ca_obj_refer.rb +10 -5
  224. data/yard-stubs/ca_obj_roll.rb +7 -6
  225. data/yard-stubs/ca_obj_shift.rb +2 -3
  226. data/yard-stubs/ca_obj_stride.rb +5 -7
  227. data/yard-stubs/ca_obj_tile.rb +1 -1
  228. data/yard-stubs/ca_obj_time.rb +43 -0
  229. data/yard-stubs/ca_obj_transpose.rb +1 -1
  230. data/yard-stubs/ca_obj_window.rb +28 -16
  231. data/yard-stubs/carray_access.rb +55 -6
  232. data/yard-stubs/carray_attribute.rb +26 -38
  233. data/yard-stubs/carray_cast.rb +121 -162
  234. data/yard-stubs/carray_class.rb +3 -4
  235. data/yard-stubs/carray_conversion.rb +6 -7
  236. data/yard-stubs/carray_copy.rb +2 -1
  237. data/yard-stubs/carray_count.rb +10 -5
  238. data/yard-stubs/carray_element.rb +4 -3
  239. data/yard-stubs/carray_limits.rb +100 -0
  240. data/yard-stubs/carray_loop.rb +0 -20
  241. data/yard-stubs/carray_mask.rb +59 -41
  242. data/yard-stubs/carray_math.rb +162 -100
  243. data/yard-stubs/carray_mathfunc.rb +415 -3
  244. data/yard-stubs/carray_median_percentile.rb +29 -11
  245. data/yard-stubs/carray_memory_view.rb +2 -2
  246. data/yard-stubs/carray_order.rb +72 -33
  247. data/yard-stubs/carray_random.rb +6 -6
  248. data/yard-stubs/carray_reduce.rb +353 -0
  249. data/yard-stubs/carray_rng.rb +92 -0
  250. data/yard-stubs/carray_scatter.rb +12 -3
  251. data/yard-stubs/carray_slab.rb +7 -4
  252. data/yard-stubs/carray_sort.rb +7 -6
  253. data/yard-stubs/carray_test.rb +18 -5
  254. data/yard-stubs/carray_undef.rb +44 -5
  255. data/yard-stubs/ruby_carray.rb +75 -40
  256. metadata +63 -25
  257. data/CHANGELOG.v1.md +0 -126
  258. data/ext/carray_build_flags.h +0 -3
  259. data/ext/carray_config.h +0 -28
  260. data/ext/carray_data_type.c +0 -90
  261. data/ext/carray_kernels_bincmp.c +0 -6457
  262. data/ext/carray_kernels_binop.c +0 -31534
  263. data/ext/carray_kernels_init.c +0 -1734
  264. data/ext/carray_kernels_moncmp.c +0 -2104
  265. data/ext/carray_kernels_monop.c +0 -25387
  266. data/ext/carray_kernels_reduce_aggregate.c +0 -27545
  267. data/ext/carray_kernels_reduce_boolean.c +0 -1924
  268. data/ext/carray_kernels_reduce_cumulative.c +0 -11646
  269. data/ext/carray_kernels_reduce_extreme.c +0 -21631
  270. data/ext/carray_kernels_reduce_variance.c +0 -5981
  271. data/ext/carray_kernels_scan.c +0 -4302
  272. data/ext/carray_kernels_search.c +0 -37776
  273. data/ext/carray_kernels_sort.c +0 -10814
  274. data/ext/carray_kernels_triop.c +0 -1864
  275. data/lib/carray/construct.rb +0 -360
  276. data/lib/carray/data_type_extension.rb +0 -261
  277. data/lib/carray/data_type_limits.rb +0 -91
data/ext/ca_array_pool.c CHANGED
@@ -38,12 +38,11 @@
38
38
  `_pool` is NULL. This kept the migration incremental and
39
39
  remains safe in mixed object-graph scenarios.
40
40
 
41
- See devel/PROPOSAL_CARRAY_POOL_STANDARDIZATION.md for the design.
41
+ guides/devel/03_memory_management.md describes the pool framework.
42
42
 
43
43
  ---------------------------------------------------------------------------- */
44
44
 
45
45
  #include "carray.h"
46
- #include "carray_internal.h"
47
46
  #include <string.h>
48
47
 
49
48
  /* Allocate struct + pool in one shot, then run pool_init.
@@ -51,7 +50,7 @@
51
50
  * Called by per-class C `*_new` constructors. */
52
51
  typedef struct {
53
52
  CArray *ca;
54
- int8_t obj_type;
53
+ int16_t obj_type;
55
54
  int8_t ndim;
56
55
  } ca_array_alloc_arg;
57
56
 
@@ -64,7 +63,7 @@ ca_array_alloc_pool (VALUE varg)
64
63
  }
65
64
 
66
65
  void *
67
- ca_array_alloc (int8_t obj_type, int8_t ndim)
66
+ ca_array_alloc (int16_t obj_type, int8_t ndim)
68
67
  {
69
68
  ca_operation_function_t *func = &ca_func[obj_type];
70
69
  CArray *ca = xmalloc(func->struct_size);
@@ -88,7 +87,7 @@ ca_array_alloc (int8_t obj_type, int8_t ndim)
88
87
  *
89
88
  * Called by per-class `*_initialize_copy` and by ca_array_alloc above. */
90
89
  void
91
- ca_array_pool_alloc (void *ap, int8_t obj_type, int8_t ndim)
90
+ ca_array_pool_alloc (void *ap, int16_t obj_type, int8_t ndim)
92
91
  {
93
92
  CArray *ca = (CArray *) ap;
94
93
  ca_operation_function_t *func = &ca_func[obj_type];
@@ -7,32 +7,14 @@
7
7
  per-axis-mixed views describe each axis the same way so a common
8
8
  attach dispatch engine (ca_axis_dispatch.c) can drive them uniformly.
9
9
 
10
- Status: TWO-TIER FREEZE CONTRACT (3.0 onward), the same split
11
- ca_kernel_iterator.h declares, for the same reason: this header is
12
- installed and reachable from `#include "carray.h"`, so what it declares
13
- is visible to downstream gems whether or not they were meant to use it.
14
-
15
- FROZEN (additions only; renaming or re-laying-out is a 3.x break):
16
- - ca_axis_kind_t and the layout of ca_axis_desc_t
17
-
18
- Not because view authors were invited to build descriptors, but
19
- because ca_iter_state -- whose slab-delivery fields ARE frozen
20
- author contract -- embeds ca_axis_desc_t descs[CA_RANK_MAX] by
21
- value. The type is already part of that struct's layout, so it
22
- cannot move or shrink without breaking kernels that were promised
23
- stability. It is frozen by consequence, not by invitation.
24
-
25
- INTERNAL (free to change across 3.x; no downstream contract):
26
- - every function below: ca_axis_dispatch_gather / _attach /
27
- _scatter / _fill_value / _fill_value_via_parent, and the
28
- ca_*_describe_axes producers
29
-
30
- An external obj_type cannot participate in this engine anyway:
31
- ca_iter_register_source_kind accepts CA_ITER_SRC_ATTACH only and
32
- raises on CA_ITER_SRC_DESCRIPTOR, precisely because the engine
33
- looks describe_axes up in its own table and an external type has no
34
- way to supply one. A contract nobody outside can invoke is a
35
- contract not worth keeping, so these stay free to move.
10
+ This header is internal to carray and carries no contract: anything in
11
+ it may change or go away in any release, 3.x patch releases included.
12
+ ca_kernel_iterator.h and carray_internal.h include it only when carray
13
+ itself is built, and it is not installed (see ext/extconf.rb
14
+ $INSTALLFILES). It ships in the gem's ext/ directory only because
15
+ carray is built from it there; an extension that puts that directory on
16
+ its include path must still not include it. ca_iter_state, which carray's engine fills with these
17
+ descriptors, shows an extension only its frozen fields.
36
18
 
37
19
  In-tree consumers (ca_obj_grid.c, ca_obj_select.c, ca_obj_select_axis.c,
38
20
  ca_obj_window.c, ca_iter_substrate.h, ca_kernel_iterator.c) pick this up
@@ -44,8 +26,9 @@
44
26
  #define CA_AXIS_DESCRIPTOR_H 1
45
27
 
46
28
  /* Depends on int8_t / uint8_t / ca_size_t typedefs from carray.h.
47
- When this header is included from carray.h mid-file, those typedefs
48
- are already in scope by the time we reach the include site. */
29
+ ca_kernel_iterator.h includes this header, after carray.h has declared
30
+ those typedefs, because the engine's fields of ca_iter_state hold
31
+ ca_axis_desc_t and ca_op_prefix_axis_t. */
49
32
 
50
33
  /* ---- per-axis descriptor types --------------------------------------------
51
34
 
@@ -75,14 +58,46 @@ typedef struct {
75
58
  ca_size_t step;
76
59
  /* INDEX-only field (unused / NULL when kind != INDEX).
77
60
  Ownership: borrowed from the producer view; lifetime tied to
78
- the view that produced the descriptor array. Consumer (D3
79
- dispatch) must not free or mutate. */
61
+ the view that produced the descriptor array. A consumer must not
62
+ free or mutate it. */
80
63
  ca_size_t *indices;
81
64
  /* SHIFT-only fields (unused / undefined when kind != SHIFT) */
82
65
  ca_size_t size0; /* parent dim along this axis (bounds check) */
83
66
  uint8_t policy; /* CA_BOUNDS_FILL / PERIODIC / REFLECT / ... */
84
67
  } ca_axis_desc_t;
85
68
 
69
+ /* A prefix axis, classified once before the walk.
70
+
71
+ Inner per-iteration offset contribution for prefix axis k:
72
+ STRIDE: poff += byte_start + idx[k] * byte_step
73
+ INDEX : poff += indices[idx[k]] * byte_pstride
74
+ SHIFT : (bound-checked, see ca_axis_dispatch_prefix_offset)
75
+
76
+ Quantities are pre-multiplied by pstrides[k] at setup time so the
77
+ inner loop spends one multiply per axis. */
78
+
79
+ typedef enum {
80
+ CA_OP_AXIS_STRIDE = 0,
81
+ CA_OP_AXIS_INDEX = 1,
82
+ CA_OP_AXIS_SHIFT = 2 /* bound-aware (CAWindow / CAShift) */
83
+ } ca_op_axis_kind_t;
84
+
85
+ typedef struct {
86
+ ca_op_axis_kind_t kind;
87
+ ca_size_t count;
88
+ /* STRIDE / SHIFT modes */
89
+ ca_size_t byte_start; /* = start * pstride */
90
+ ca_size_t byte_step; /* = step * pstride */
91
+ /* INDEX-mode (kind == CA_OP_AXIS_INDEX) */
92
+ const ca_size_t *indices;
93
+ ca_size_t byte_pstride; /* = pstride */
94
+ /* SHIFT-only fields */
95
+ ca_size_t shift_start;
96
+ ca_size_t shift_step;
97
+ ca_size_t size0; /* parent dim along this axis */
98
+ uint8_t policy; /* CA_BOUNDS_FILL / PERIODIC / ... */
99
+ } ca_op_prefix_axis_t;
100
+
86
101
  /* ---- producer interface --------------------------------------------------
87
102
 
88
103
  A producer writes one entry per descriptor axis into `out`
@@ -42,7 +42,6 @@
42
42
  #include "carray.h"
43
43
  #include "ca_iter_substrate.h"
44
44
  #include "ca_composite_dispatch.h"
45
- #include "carray_internal.h" /* ca_parent_lends_no_memory */
46
45
 
47
46
  #include <string.h>
48
47
 
@@ -1068,6 +1067,7 @@ ca_axis_dispatch_outer_has_shift (const ca_axis_desc_t *descs,
1068
1067
  ========================================================================== */
1069
1068
 
1070
1069
  /* The whole view as one region request to its own xfer_stride slot. */
1070
+ /* recursion: calls the slot of this array itself, not of its parent */
1071
1071
  static void
1072
1072
  ca_axis_view_xfer_region (CArray *ca, void *data, int dir)
1073
1073
  {
@@ -1099,15 +1099,17 @@ ca_axis_view_xfer_all (void *ap, ca_axis_view_fast_t fast, int region_ok,
1099
1099
  }
1100
1100
 
1101
1101
  if ( ca_attach_is_alias(parent) ) {
1102
- ca_attach(parent); /* window: an alias attach, cannot raise */
1102
+ ca_attach(parent); /* attach: an alias attach, cannot raise */
1103
1103
  fast(ca, (char *) data, dir);
1104
1104
  /* An entity that keeps no buffer at rest (the mask of a CAObject, a
1105
1105
  CASource) attaches a copy, which only its sync writes back; for
1106
1106
  memory it lends, the sync does nothing. */
1107
1107
  if ( dir == CA_XFER_PUT ) {
1108
- ca_sync(parent);
1108
+ ca_sync_detach(parent);
1109
+ }
1110
+ else {
1111
+ ca_detach(parent);
1109
1112
  }
1110
- ca_detach(parent);
1111
1113
  return;
1112
1114
  }
1113
1115
 
@@ -1164,6 +1166,7 @@ ca_axis_view_attach_owned (void *ap)
1164
1166
  ca->ptr = buf;
1165
1167
  }
1166
1168
 
1169
+ /* recursion: calls the slot of this array itself, not of its parent */
1167
1170
  void
1168
1171
  ca_axis_view_sync_owned (void *ap)
1169
1172
  {
data/ext/ca_axis_group.c CHANGED
@@ -4,8 +4,8 @@
4
4
  scan.
5
5
 
6
6
  INTERNAL kernels behind `__...__`-named methods. The user-facing surface
7
- (CACategorical / AxisGroup type gate in `[]`, CAGroupIterator, the :group
8
- reduce dispatch, GroupLabels) is wired in ca_group_iter.c (the `[]` gate and
7
+ (CACategorical / CAAxisGroup type gate in `[]`, CAGroupIterator, the :group
8
+ reduce dispatch, CArray::GroupLabels) is wired in ca_group_iter.c (the `[]` gate and
9
9
  the iterator) + `axis_group` (lib/carray/axis_group.rb). Both kernels are
10
10
  written in the general form: several group axes + rank-N categorical (an N-D
11
11
  codes map) via a per-slab-element composite code, native data type dispatch,
@@ -33,7 +33,6 @@
33
33
  --------------------------------------------------------------------------- */
34
34
 
35
35
  #include "carray.h"
36
- #include "carray_internal.h" /* CA_EXTREMUM_REPLACES, ca_obj_extremum_replaces */
37
36
  #include "ca_kernel_iterator.h"
38
37
  #include <math.h>
39
38
 
@@ -314,7 +313,7 @@ group_op_code (VALUE vop)
314
313
  n < 2 UNDEF, n >= 2 the formula.
315
314
  */
316
315
  static VALUE
317
- rb_ca_axis_group_reduce (VALUE self, VALUE vgaxes, VALUE vbundles, VALUE vop)
316
+ rb_ca_axis_group_reduce (volatile VALUE self, VALUE vgaxes, VALUE vbundles, VALUE vop)
318
317
  {
319
318
  CArray *src, *ca, *co;
320
319
  int op = group_op_code(vop);
@@ -377,8 +376,8 @@ rb_ca_axis_group_reduce (VALUE self, VALUE vgaxes, VALUE vbundles, VALUE vop)
377
376
  ca_size_t k = (ca_size_t) NUM2LONG(RARRAY_AREF(bundle, 1));
378
377
  VALUE vbaxes = RARRAY_AREF(bundle, 2);
379
378
  Check_Type(vbaxes, T_ARRAY);
380
- if ( k <= 0 ) {
381
- rb_raise(rb_eArgError, "axis_group_reduce: bundle k must be positive");
379
+ if ( k < 0 ) {
380
+ rb_raise(rb_eArgError, "axis_group_reduce: bundle k must not be negative");
382
381
  }
383
382
 
384
383
  /* The code table is copied out and the source let go of at once, rather
@@ -390,7 +389,7 @@ rb_ca_axis_group_reduce (VALUE self, VALUE vgaxes, VALUE vbundles, VALUE vop)
390
389
  that the unwind collects. Not a String: a String held only by `keep`
391
390
  can be moved by compaction, taking the bytes under bundle_codes with
392
391
  it. */
393
- VALUE v32 = rb_ca_wrap_readonly(vcodes, INT2NUM(CA_INT32));
392
+ volatile VALUE v32 = rb_ca_wrap_readonly(vcodes, INT2NUM(CA_INT32));
394
393
  rb_ary_push((VALUE) keep, v32);
395
394
  GetCArray(v32, bundle_ca[bi]);
396
395
  h_codes[bi] = 0;
@@ -488,7 +487,7 @@ rb_ca_axis_group_reduce (VALUE self, VALUE vgaxes, VALUE vbundles, VALUE vop)
488
487
  else if ( op == GR_ACCUM ) out_dt = src->data_type;
489
488
  else if ( op == GR_MIN || op == GR_MAX )
490
489
  out_dt = ( src->data_type == CA_BOOLEAN ) ? CA_UINT64 : src->data_type;
491
- VALUE vout = rb_carray_new(out_dt, ondim, odim, 0, NULL);
490
+ volatile VALUE vout = rb_carray_new(out_dt, ondim, odim, 0, NULL);
492
491
  GetCArray(vout, co);
493
492
 
494
493
  /* --- accumulator buffers (only those the op needs; all O(nout)) --- */
@@ -683,7 +682,6 @@ rb_ca_axis_group_reduce (VALUE self, VALUE vgaxes, VALUE vbundles, VALUE vop)
683
682
  ALLOCV_END(h_band);
684
683
  for ( int bi = 0; bi < n_bundles; bi++ ) ALLOCV_END(h_codes[bi]);
685
684
 
686
- RB_GC_GUARD(keep);
687
685
  return vout;
688
686
  }
689
687
 
@@ -1147,7 +1145,7 @@ group_scan_op_code (VALUE vop)
1147
1145
  UNDEF. A cell excluded from every group (composite code out of [0,k)) belongs
1148
1146
  to no group and stays UNDEF. */
1149
1147
  static VALUE
1150
- rb_ca_axis_group_scan (VALUE self, VALUE vgaxes, VALUE vbundles, VALUE vop)
1148
+ rb_ca_axis_group_scan (volatile VALUE self, VALUE vgaxes, VALUE vbundles, VALUE vop)
1151
1149
  {
1152
1150
  CArray *src, *ca, *co;
1153
1151
  int op = group_scan_op_code(vop);
@@ -1212,8 +1210,8 @@ rb_ca_axis_group_scan (VALUE self, VALUE vgaxes, VALUE vbundles, VALUE vop)
1212
1210
  ca_size_t k = (ca_size_t) NUM2LONG(RARRAY_AREF(bundle, 1));
1213
1211
  VALUE vbaxes = RARRAY_AREF(bundle, 2);
1214
1212
  Check_Type(vbaxes, T_ARRAY);
1215
- if ( k <= 0 ) {
1216
- rb_raise(rb_eArgError, "axis_group_scan: bundle k must be positive");
1213
+ if ( k < 0 ) {
1214
+ rb_raise(rb_eArgError, "axis_group_scan: bundle k must not be negative");
1217
1215
  }
1218
1216
 
1219
1217
  /* The code table is copied out and the source let go of at once, rather
@@ -1225,7 +1223,7 @@ rb_ca_axis_group_scan (VALUE self, VALUE vgaxes, VALUE vbundles, VALUE vop)
1225
1223
  that the unwind collects. Not a String: a String held only by `keep`
1226
1224
  can be moved by compaction, taking the bytes under bundle_codes with
1227
1225
  it. */
1228
- VALUE v32 = rb_ca_wrap_readonly(vcodes, INT2NUM(CA_INT32));
1226
+ volatile VALUE v32 = rb_ca_wrap_readonly(vcodes, INT2NUM(CA_INT32));
1229
1227
  rb_ary_push((VALUE) keep, v32);
1230
1228
  GetCArray(v32, bundle_ca[bi]);
1231
1229
  h_codes[bi] = 0;
@@ -1307,7 +1305,7 @@ rb_ca_axis_group_scan (VALUE self, VALUE vgaxes, VALUE vbundles, VALUE vop)
1307
1305
  /* --- source-shaped output --- */
1308
1306
  ca_size_t odim[CA_RANK_MAX];
1309
1307
  for ( int8_t i = 0; i < src->ndim; i++ ) odim[i] = src->dim[i];
1310
- VALUE vout = rb_carray_new(out_dt, src->ndim, odim, 0, NULL);
1308
+ volatile VALUE vout = rb_carray_new(out_dt, src->ndim, odim, 0, NULL);
1311
1309
  GetCArray(vout, co);
1312
1310
 
1313
1311
  /* --- band raveled base address per band flat index (same construction as
@@ -1414,7 +1412,6 @@ rb_ca_axis_group_scan (VALUE self, VALUE vgaxes, VALUE vbundles, VALUE vop)
1414
1412
  ALLOCV_END(h_accn); ALLOCV_END(h_seen); ALLOCV_END(h_acco);
1415
1413
  for ( int bi = 0; bi < n_bundles; bi++ ) ALLOCV_END(h_codes[bi]);
1416
1414
 
1417
- RB_GC_GUARD(keep);
1418
1415
  return vout;
1419
1416
  }
1420
1417
 
@@ -1,22 +1,22 @@
1
1
  /* ---------------------------------------------------------------------------
2
2
 
3
- ca_bincmp_dispatch.h
3
+ ca_bincmp_dispatch.h -- the comparison kernel tables and op ids
4
4
 
5
- PROPOSAL_LAZY_ELEMENTWISE_VIEW Phase 4 (P.4.1) — CABinCmp dispatch API.
5
+ carray.h includes this header through carray_math_kernel.h, which
6
+ declares the kernel type ca_bincmp_func_t.
6
7
 
7
- rev2 §0.5 #1 model: comparison output is always CA_BOOLEAN (boolean8_t,
8
- 1 byte/cell). Operands are cast to the common data_type via
9
- ca_promote_type, the kernel reads both operands and writes boolean
10
- output. The binop in-place trick (= pull left into output buffer)
11
- does NOT work for comparison because output data_type != operand data_type.
8
+ A comparison writes CA_BOOLEAN output (one byte per cell). Both
9
+ operands are cast to their common data type before the kernel runs.
12
10
 
13
- rev2 §0.5 #4: bitwise on boolean output is handled by Phase 2 CABinOp
14
- (= ca_binop_bit_and_i_boolean8_t exists). This file is comparison-
15
- only.
16
-
17
- P.4.1 scope: only :lt wired with a kernel; other 6 op constants
18
- reserved and lookup returns NULL (caller detects not-implemented).
19
- P.4.2 expands to 7 ops (= eq / ne / lt / gt / le / ge / feq).
11
+ For extensions, and fixed for the rest of 3.x: the op-id values
12
+ (CA_BINCMP_*) and the per-operation kernel tables (ca_bincmp_lt[] ...).
13
+ A table holds one kernel per data type, indexed by the data type code
14
+ (CA_FLOAT64 ...) below CA_NTYPE, which is fixed within 3.x. An extension
15
+ may assign its own ca_bincmp_func_t to a slot at load time, e.g.
16
+ `ca_bincmp_lt[CA_FLOAT64] = my_lt;`, and the operation then uses it for
17
+ arrays of that data type.
18
+ The lookup and promotion helpers are carray's own and are declared in
19
+ carray_internal.h, which is not installed.
20
20
 
21
21
  ---------------------------------------------------------------------------- */
22
22
 
@@ -37,7 +37,7 @@ enum {
37
37
  CA_BINCMP_EQ = 4,
38
38
  CA_BINCMP_NE = 5,
39
39
  CA_BINCMP_FEQ = 6, /* float ε-equal, eps stored in CABinCmp.tol tail */
40
- /* IC.2: tolerance-bearing predicates, tol stored in CABinCmp.tol tail */
40
+ /* Tolerance-bearing predicates, tol stored in CABinCmp.tol tail */
41
41
  CA_BINCMP_IS_CLOSE = 7, /* |a - b| <= tol (absolute) */
42
42
  CA_BINCMP_IS_EQUIV = 8, /* |a - b| / max(|a|, |b|) <= tol (relative) */
43
43
 
@@ -49,9 +49,7 @@ enum {
49
49
  /* ------------------------------------------------------------------- */
50
50
 
51
51
  /* The per-data_type bincmp kernel tables (mkkernel-generated, write
52
- boolean8_t output). Made public here (PROPOSAL_CARRAY_H_REORG H.2)
53
- so external math-backend gems can swap a slot through the carray.h
54
- umbrella with no hand-declared externs. */
52
+ boolean8_t output). */
55
53
  extern ca_bincmp_func_t ca_bincmp_lt [CA_NTYPE];
56
54
  extern ca_bincmp_func_t ca_bincmp_gt [CA_NTYPE];
57
55
  extern ca_bincmp_func_t ca_bincmp_le [CA_NTYPE];
@@ -62,24 +60,4 @@ extern ca_bincmp_func_t ca_bincmp_feq [CA_NTYPE];
62
60
  extern ca_bincmp_func_t ca_bincmp_is_close [CA_NTYPE];
63
61
  extern ca_bincmp_func_t ca_bincmp_is_equiv [CA_NTYPE];
64
62
 
65
- /* ------------------------------------------------------------------- */
66
- /* Dispatch API */
67
- /* ------------------------------------------------------------------- */
68
-
69
- /* Look up the bincmp kernel for (op_id, common_dt). Returns NULL if
70
- not implemented at the requested data_type. All bincmp kernels write
71
- boolean8_t to ptr3. */
72
- ca_bincmp_func_t ca_bincmp_kernel_lookup (uint16_t op_id, int8_t common_dt);
73
-
74
- /* Compute input common data_type via ca_promote_type. Output data_type is
75
- always CA_BOOLEAN (NOT returned here; CABinCmp.data_type is set
76
- directly). */
77
- int8_t ca_lazy_bincmp_promote (uint16_t op_id, int8_t l_dt, int8_t r_dt);
78
-
79
- /* What data_type does each operand need to be cast to before reaching the
80
- kernel? Returns common data_type for both (= cast-before route, same as
81
- Phase 2 CABinOp). */
82
- void ca_bincmp_kernel_input_data_types (uint16_t op_id, int8_t l_dt, int8_t r_dt,
83
- int8_t *out_l_in_dt, int8_t *out_r_in_dt);
84
-
85
63
  #endif /* CA_BINCMP_DISPATCH_H */
@@ -79,6 +79,41 @@ ca_binop_op_id_of_table (ca_binop_func_t table[])
79
79
  return -1;
80
80
  }
81
81
 
82
+ /* pmax, pmin, maximum and minimum pick one operand by order. */
83
+ int
84
+ ca_binop_picks_by_order (int op_id)
85
+ {
86
+ return op_id == CA_BINOP_PMAX || op_id == CA_BINOP_PMIN ||
87
+ op_id == CA_BINOP_MAXIMUM || op_id == CA_BINOP_MINIMUM;
88
+ }
89
+
90
+ /* + - * & | ^ give an integer array the same answer modulo 2 ** bits
91
+ whether a scalar operand is cut to the array's type first or not. */
92
+ int
93
+ ca_binop_is_ring (int op_id)
94
+ {
95
+ return op_id == CA_BINOP_ADD || op_id == CA_BINOP_SUB ||
96
+ op_id == CA_BINOP_MUL || op_id == CA_BINOP_BIT_AND ||
97
+ op_id == CA_BINOP_BIT_OR || op_id == CA_BINOP_BIT_XOR;
98
+ }
99
+
100
+ /* How a scalar operand beside an integer array is taken by op_id
101
+ (ca_scalar_operand_for_op). */
102
+ int
103
+ ca_binop_scalar_operand_mode (int op_id)
104
+ {
105
+ if ( ca_binop_is_ring(op_id) ) {
106
+ return CA_SCALAR_FOR_RING;
107
+ }
108
+ if ( op_id == CA_BINOP_PMIN || op_id == CA_BINOP_MINIMUM ) {
109
+ return CA_SCALAR_FOR_MIN;
110
+ }
111
+ if ( op_id == CA_BINOP_PMAX || op_id == CA_BINOP_MAXIMUM ) {
112
+ return CA_SCALAR_FOR_MAX;
113
+ }
114
+ return CA_SCALAR_FOR_ORDER;
115
+ }
116
+
82
117
  /* ------------------------------------------------------------------- */
83
118
  /* output data_type rule */
84
119
  /* ------------------------------------------------------------------- */
@@ -1,15 +1,23 @@
1
1
  /* ---------------------------------------------------------------------------
2
2
 
3
- ca_binop_dispatch.h
3
+ ca_binop_dispatch.h -- the binop kernel tables and op ids
4
4
 
5
- PROPOSAL_LAZY_ELEMENTWISE_VIEW Phase 2 (P.2.1) — CABinOp dispatch API.
5
+ carray.h includes this header through carray_math_kernel.h, which
6
+ declares the kernel type ca_binop_func_t.
6
7
 
7
- rev3 §3.2: 1-D same-type table + cast-before route. No 2-D mixed-type
8
- dispatch (= existing eager `ca_binop_<op>[CA_NTYPE]` is 1-D same-type,
9
- promotion handled by inserting CAMonOp(:cast_<common>) nodes before
10
- reaching CABinOp).
8
+ A table holds same-type kernels: operands of two data types are cast
9
+ to their common data type before the kernel runs (ca_binop_ipower, whose
10
+ right operand is always int64, is the exception).
11
11
 
12
- P.2.1 scope: ADD only, vv only. P.2.2 expands to 13 ops.
12
+ For extensions, and fixed for the rest of 3.x: the op-id values
13
+ (CA_BINOP_*) and the per-operation kernel tables (ca_binop_add[] ...). A
14
+ table holds one kernel per data type, indexed by the data type code
15
+ (CA_FLOAT64 ...) below CA_NTYPE, which is fixed within 3.x. An extension
16
+ may assign its own ca_binop_func_t to a slot at load time, e.g.
17
+ `ca_binop_add[CA_FLOAT64] = my_add;`, and the operation then uses it for
18
+ arrays of that data type.
19
+ The lookup and promotion helpers are carray's own and are declared in
20
+ carray_internal.h, which is not installed.
13
21
 
14
22
  ---------------------------------------------------------------------------- */
15
23
 
@@ -23,15 +31,14 @@
23
31
  /* ------------------------------------------------------------------- */
24
32
 
25
33
  enum {
26
- /* Arithmetic binop (5) — variants: vv/vs/sv (P.2.3 gate) */
34
+ /* Arithmetic binop (5) */
27
35
  CA_BINOP_ADD = 0,
28
36
  CA_BINOP_SUB = 1,
29
37
  CA_BINOP_MUL = 2,
30
38
  CA_BINOP_DIV = 3,
31
39
  CA_BINOP_POW = 4,
32
40
 
33
- /* Bitwise / logical (Q1 (b) verdict: 13 op total; full scope flesh
34
- out in P.2.2) */
41
+ /* Bitwise, mod, quo, rcp_mul */
35
42
  CA_BINOP_BIT_AND = 5,
36
43
  CA_BINOP_BIT_OR = 6,
37
44
  CA_BINOP_BIT_XOR = 7,
@@ -50,9 +57,7 @@ enum {
50
57
  hand-written kernel table. */
51
58
  CA_BINOP_IPOWER = 13,
52
59
 
53
- /* M.2 (PyTorch alignment): float-only binop family. Integer input
54
- unsupported at this layer — caller must cast operands explicitly.
55
- ATAN2 / HYPOT migrate from hand-written CAMath wrappers (M.3). */
60
+ /* copysign / logaddexp / nextafter / fmod / atan2 / hypot */
56
61
  CA_BINOP_COPYSIGN = 14,
57
62
  CA_BINOP_LOGADDEXP = 15,
58
63
  CA_BINOP_NEXTAFTER = 16,
@@ -62,7 +67,7 @@ enum {
62
67
 
63
68
  /* Pair-wise max / min (NaN-skip semantics via C99 fmax / fmin on the
64
69
  float branch; comparison form on integer / object). All numeric +
65
- object. Ruby aliases `fmax` / `fmin` share the same kernel. */
70
+ object. */
66
71
  CA_BINOP_PMAX = 20,
67
72
  CA_BINOP_PMIN = 21,
68
73
 
@@ -87,10 +92,8 @@ enum {
87
92
  /* Kernel dispatch tables (per-data_type) */
88
93
  /* ------------------------------------------------------------------- */
89
94
 
90
- /* The per-data_type binop kernel tables. Defined in carray_kernels.c
91
- (mkkernel-generated), ext/ca_op_ipower.c (ipower). Made public here
92
- (PROPOSAL_CARRAY_H_REORG H.2) so external math-backend gems can swap
93
- a slot through the carray.h umbrella with no hand-declared externs. */
95
+ /* The per-data_type binop kernel tables, generated by ext/mkkernel.rb
96
+ except ipower (ext/ca_op_ipower.c). */
94
97
  extern ca_binop_func_t ca_binop_add [CA_NTYPE];
95
98
  extern ca_binop_func_t ca_binop_sub [CA_NTYPE];
96
99
  extern ca_binop_func_t ca_binop_mul [CA_NTYPE];
@@ -107,7 +110,7 @@ extern ca_binop_func_t ca_binop_rcp_mul [CA_NTYPE];
107
110
  /* Hand-written heterogeneous binop (left: float/cmplx, right: int64,
108
111
  output: left data_type). Defined in ext/ca_op_ipower.c. */
109
112
  extern ca_binop_func_t ca_binop_ipower [CA_NTYPE];
110
- /* M.2 + M.3: PyTorch alignment additions (mkkernel-generated, float-only). */
113
+ /* copysign ... hypot, pmax / pmin / maximum / minimum, and / or / xor. */
111
114
  extern ca_binop_func_t ca_binop_copysign [CA_NTYPE];
112
115
  extern ca_binop_func_t ca_binop_logaddexp [CA_NTYPE];
113
116
  extern ca_binop_func_t ca_binop_nextafter [CA_NTYPE];
@@ -122,31 +125,4 @@ extern ca_binop_func_t ca_binop_and [CA_NTYPE];
122
125
  extern ca_binop_func_t ca_binop_or [CA_NTYPE];
123
126
  extern ca_binop_func_t ca_binop_xor [CA_NTYPE];
124
127
 
125
- /* ------------------------------------------------------------------- */
126
- /* Dispatch API */
127
- /* ------------------------------------------------------------------- */
128
-
129
- /* Look up the same-type vv kernel for (op_id, common_dt). Returns NULL
130
- if not implemented. P.2.1: only ADD wired up; others NULL until P.2.2. */
131
- ca_binop_func_t ca_binop_kernel_lookup_vv (uint16_t op_id, int8_t common_dt);
132
-
133
- /* Compute output (= common) data_type via the existing eager promotion rule
134
- (ca_promote_type). Phase 0 audit landed extern visibility. */
135
- int8_t ca_lazy_promote_binop (uint16_t op_id, int8_t l_dt, int8_t r_dt);
136
-
137
- /* What data_type does each operand need to be cast to before reaching the
138
- kernel? Returns common data_type for both (= cast-before route). When
139
- common != operand.data_type, caller inserts CAMonOp(:cast_<common>). */
140
- void ca_binop_kernel_input_data_types (uint16_t op_id, int8_t l_dt, int8_t r_dt,
141
- int8_t *out_l_in_dt, int8_t *out_r_in_dt);
142
-
143
- /* True iff op_id is a trapping op for the given common_dt (= integer
144
- DIV / MOD on integer data_types). Float DIV returns NaN/Inf and does
145
- not trap, so it is NOT classified trapping.
146
-
147
- When trapping AND either operand has a mask, xfer_stride must pass
148
- a slab mask to the kernel so masked cells are skipped (otherwise
149
- masked-zero divisors crash with SIGFPE). See §3.4 rev3. */
150
- int ca_binop_op_id_of_table (ca_binop_func_t table[]);
151
-
152
128
  #endif /* CA_BINOP_DISPATCH_H */