carray-jit 0.1.6 → 0.1.8
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +40 -0
- data/README.md +2 -2
- data/carray-jit.gemspec +7 -3
- data/docs/02_KernelShapes.md +7 -7
- data/docs/03_SupportedFeatures.md +28 -28
- data/docs/04_Compiling.md +5 -5
- data/docs/05_DesignNotes.md +1 -3
- data/docs/06_Cheatsheet.md +12 -12
- data/docs/07_StepByStep.ja.md +45 -45
- data/docs/07_StepByStep.md +45 -45
- data/examples/applications/alarm.rb +2 -2
- data/examples/applications/cubic_spline.rb +17 -17
- data/examples/applications/dithering.rb +3 -3
- data/examples/applications/group_stats.rb +4 -4
- data/examples/applications/heat_equation.rb +5 -5
- data/examples/applications/kepler.rb +10 -10
- data/examples/applications/lookup.rb +3 -3
- data/examples/applications/median_filter.rb +7 -7
- data/examples/applications/moving_average.rb +6 -6
- data/examples/applications/parcel_ascent.rb +10 -10
- data/examples/applications/partial_sums.rb +3 -3
- data/examples/applications/point_cloud.rb +4 -4
- data/examples/applications/point_in_polygon.rb +4 -4
- data/examples/applications/quicksort.rb +1 -1
- data/examples/applications/relaxation.rb +1 -1
- data/examples/applications/sensor_gaps.rb +3 -3
- data/examples/applications/sobel_edges.rb +5 -5
- data/examples/applications/van_der_pol.rb +3 -3
- data/examples/applications/wet_bulb.rb +4 -4
- data/examples/features/01_element_wise.rb +8 -8
- data/examples/features/02_stencil.rb +4 -4
- data/examples/features/03_recurrence.rb +1 -1
- data/examples/features/04_thomas.rb +8 -8
- data/examples/features/05_reduction.rb +7 -7
- data/examples/features/06_jit_contract.rb +9 -9
- data/examples/features/07_masks.rb +4 -4
- data/examples/features/08_views.rb +5 -5
- data/examples/features/09_inspecting.rb +1 -1
- data/examples/features/10_complex.rb +4 -4
- data/examples/features/11_c_functions.rb +17 -17
- data/examples/features/12_sweep.rb +19 -24
- data/examples/features/13_cscalar.rb +12 -12
- data/examples/features/14_stencil_window.rb +6 -6
- data/examples/features/15_loops.rb +6 -6
- data/examples/features/16_raising.rb +4 -4
- data/lib/carray/jit/analyzer.rb +39 -34
- data/lib/carray/jit/c_function.rb +133 -139
- data/lib/carray/jit/c_generator.rb +20 -11
- data/lib/carray/jit/call.rb +1 -1
- data/lib/carray/jit/compiler.rb +78 -43
- data/lib/carray/jit/expression.rb +68 -28
- data/lib/carray/jit/kernel.rb +23 -47
- data/lib/carray/jit/native.rb +149 -0
- data/lib/carray/jit/node.rb +1 -1
- data/lib/carray/jit/sweep.rb +14 -64
- data/lib/carray/jit/type_assignment.rb +9 -4
- data/lib/carray/jit/version.rb +1 -1
- data/lib/carray/jit.rb +103 -28
- metadata +8 -7
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 16d51f931daae550963d691be75c85af0816d5a2b0b355dcfbc78ae0fd422a94
|
|
4
|
+
data.tar.gz: d36bf117c2fe829f4b5804690819f393d17199f7a2692bd59f9dcb77477dad41
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 179ac2913af0b8d47a634a3971a155a4a6be771f4b83b99f974bbca00ff25f4da8e77a11224b288d357a43772bdbb9a58d7fb03076dd6b27f2fc63c70a937b84
|
|
7
|
+
data.tar.gz: e2cf86d8914f39909fc2755c078cbd8ef7703df3b9d8cc78008b03eaf1c2250b1111f3a5ca2706af5205807163323d199ed7876879435e58f8f2dffe66c247d1
|
data/CHANGELOG.md
CHANGED
|
@@ -37,6 +37,46 @@ version you have and a newer one.
|
|
|
37
37
|
a release needs is said in the gemspec, and an entry says so only
|
|
38
38
|
when the answer changes. -->
|
|
39
39
|
|
|
40
|
+
## 0.1.8
|
|
41
|
+
|
|
42
|
+
- Change: a local array in a block is made under the width names,
|
|
43
|
+
`CArray.float64(n)`, `CArray.int32(n)` and the rest. The C type names
|
|
44
|
+
(`CArray.double`, `CArray.float`, `CArray.int`, `CArray.complex` and
|
|
45
|
+
their kin) raise `Unsupported`, naming the width name to write instead.
|
|
46
|
+
Note that `CArray.float` is a float32 and `CArray.complex` a cmplx64.
|
|
47
|
+
|
|
48
|
+
- Change: needs CArray 3.1.0 or later in the 3.1 series, where 0.1.7
|
|
49
|
+
needed the 3.0 series.
|
|
50
|
+
|
|
51
|
+
- Change: a Complex scalar beside a float32 array gives a cmplx64, as
|
|
52
|
+
CArray from 3.0.9 does; it was a cmplx128.
|
|
53
|
+
|
|
54
|
+
- Fix: the `carray-jit` cache command, run with a CArray checkout on the
|
|
55
|
+
load path through `-I`, names that checkout's version and looks after
|
|
56
|
+
its kernels. It took the version of the installed CArray gem instead.
|
|
57
|
+
|
|
58
|
+
## 0.1.7
|
|
59
|
+
|
|
60
|
+
- Change: needs CArray 3.0.6 or later in the 3.0 series; install it
|
|
61
|
+
alongside this release. An element-wise pass is handed to
|
|
62
|
+
`CArray::AddressBasis.sweep`, which 3.0.6 is the first to have.
|
|
63
|
+
|
|
64
|
+
- Change: the kernel cache keeps 2000 kernels per environment by
|
|
65
|
+
default, up from 512, which is about 60 MB on disk. A program that
|
|
66
|
+
compiled more than 512 rebuilt the ones evicted each time it ran. Set
|
|
67
|
+
`CARRAY_JIT_CACHE_LIMIT` for a different bound.
|
|
68
|
+
|
|
69
|
+
- Change: `jit_for` and `jit_init` cost less per call when called again
|
|
70
|
+
with operands that agree in name, type and mask, and a kernel writing
|
|
71
|
+
an `int64` array is vectorised as a `float64` one is.
|
|
72
|
+
|
|
73
|
+
- Fix: a lazy expression or `CArray.fuse` computed here keeps the sign of
|
|
74
|
+
a NaN as CArray does: `(a * -1).signbit` and a NaN fill of `shift`
|
|
75
|
+
gave other bits with the evaluator registered.
|
|
76
|
+
|
|
77
|
+
- Fix: kernels built at once by several threads, or in Ractors, no
|
|
78
|
+
longer raise `Errno::ENOENT`.
|
|
79
|
+
|
|
40
80
|
## 0.1.6
|
|
41
81
|
|
|
42
82
|
- Change: needs CArray 3.0.5 or later in the 3.0 series; install it
|
data/README.md
CHANGED
|
@@ -35,7 +35,7 @@ gem "carray-jit"
|
|
|
35
35
|
Requires:
|
|
36
36
|
|
|
37
37
|
- Ruby >= 3.2
|
|
38
|
-
- CArray >= 3.0
|
|
38
|
+
- CArray >= 3.1.0, < 3.2
|
|
39
39
|
- A C compiler
|
|
40
40
|
- Prism and Fiddle (both ship with Ruby; Fiddle is a bundled gem)
|
|
41
41
|
|
|
@@ -47,7 +47,7 @@ Requires:
|
|
|
47
47
|
# loop has just written.
|
|
48
48
|
|
|
49
49
|
x = 0.5
|
|
50
|
-
legendre = CArray.
|
|
50
|
+
legendre = CArray.float64(24)
|
|
51
51
|
legendre[0] = 1.0 # P_0(x) = 1
|
|
52
52
|
legendre[1] = x # P_1(x) = x
|
|
53
53
|
|
data/carray-jit.gemspec
CHANGED
|
@@ -32,8 +32,12 @@ Gem::Specification.new do |spec|
|
|
|
32
32
|
spec.bindir = "bin"
|
|
33
33
|
spec.executables = ["carray-jit"]
|
|
34
34
|
|
|
35
|
-
#
|
|
36
|
-
|
|
35
|
+
# The floor is the first CArray whose surface this release uses. There is
|
|
36
|
+
# no ceiling: a kernel is compiled against the CArray it runs with, and the
|
|
37
|
+
# cache keeps the kernels of each CArray version apart.
|
|
38
|
+
spec.add_dependency "carray", ">= 3.1.0", "< 3.2"
|
|
37
39
|
# fiddle ships as a bundled gem; depend on it explicitly so Ruby 3.5+ resolves it.
|
|
38
|
-
|
|
40
|
+
# 1.1.1, which Ruby 3.2 ships, is the first with TYPE_UCHAR and the other
|
|
41
|
+
# unsigned types.
|
|
42
|
+
spec.add_dependency "fiddle", ">= 1.1.1"
|
|
39
43
|
end
|
data/docs/02_KernelShapes.md
CHANGED
|
@@ -17,7 +17,7 @@ The assignment is Ruby's own. Every name in the block is a cell -- the loop is t
|
|
|
17
17
|
`CScalar` is the one-cell `CArray` it subclasses, minus the index: `s[]` is the value, and `s[] = ...` puts one back. CArray's own operators read that one cell for every cell of everything else, and so does this:
|
|
18
18
|
|
|
19
19
|
```ruby
|
|
20
|
-
gain = CScalar.
|
|
20
|
+
gain = CScalar.float64() { 2.0 }
|
|
21
21
|
CArray.jit_each { out = signal * gain } # gain read at every cell
|
|
22
22
|
CArray.jit_each { gain = gain + 0.5 } # and written like any cell
|
|
23
23
|
```
|
|
@@ -28,7 +28,7 @@ An indexed kernel reaches it too, and there the missing index is the whole point
|
|
|
28
28
|
CArray.jit_for(n) { |i| out[i] = signal[i] * gain[] }
|
|
29
29
|
```
|
|
30
30
|
|
|
31
|
-
`CScalar.
|
|
31
|
+
`CScalar.int32()` with no block is that one cell zero-filled, as `CArray.int32(1)` is, which is enough for an accumulator that starts at zero; the block form says the starting value out loud, and is what to write where the value is part of what the code means.
|
|
32
32
|
|
|
33
33
|
The two routes differ in how they get there. `jit_each` stretches it as it stretches a one-cell `CArray` -- a stride of zero -- while `jit_for` reads it where it lies. They compute the same thing, and `s[0]` keeps working in both, since it is still the one-cell array it is. Writing a wider expression into one is refused, with the shapes named, exactly as CArray refuses it.
|
|
34
34
|
|
|
@@ -46,7 +46,7 @@ It is the same shape of block `CArray.jit_function` takes, and not the same thin
|
|
|
46
46
|
|
|
47
47
|
#### Who drives the loop
|
|
48
48
|
|
|
49
|
-
An element-wise pass is the one shape that is a *sweep* -- nothing reaches a neighbour, nothing chooses an order -- so CArray can drive it instead
|
|
49
|
+
An element-wise pass is the one shape that is a *sweep* -- nothing reaches a neighbour, nothing chooses an order -- so CArray can drive it instead: `jit_each` hands its chunked sweep (`ca_call_cslab`) the compiled body and lets it acquire the operands.
|
|
50
50
|
|
|
51
51
|
The body is the same either way. The sweep entry point is a wrapper that calls the same kernel with the chunk as its bounds, so what changes is who opens the arrays, not what they compute.
|
|
52
52
|
|
|
@@ -56,7 +56,7 @@ A **strided view** is the case where there is. A column, a transpose, every othe
|
|
|
56
56
|
|
|
57
57
|
The rank does not stand in the way, and the arrays are not reshaped to get it out of the way. CArray's acquire reads an operand's element count and element size and never looks at its shape, so a chunk is already a flat run of cells; what has to be flat is the *kernel*, and it is compiled at rank one over the same cells rather than as a nest over the axes. One such kernel then serves whatever rank it is given, because the rank has left the loop.
|
|
58
58
|
|
|
59
|
-
Four things keep the driver here. An operand that is a **strided view**, for the reason just measured: it is walked in place here, and re-gathered there for nothing. A **stretched operand**, which is the one thing that cannot be flattened: broadcasting arrives as a stride of zero on an axis, and a flat run has no axes, so cell k of the output would stop lining up with cell k of the operand. A body that **asks about a mask**, because a chunk carries no per-cell mask to ask it of -- CArray ORs and propagates the masks, but `a[i] == UNDEF` is a question about one cell.
|
|
59
|
+
Four things keep the driver here. An operand that is a **strided view**, for the reason just measured: it is walked in place here, and re-gathered there for nothing. A **stretched operand**, which is the one thing that cannot be flattened: broadcasting arrives as a stride of zero on an axis, and a flat run has no axes, so cell k of the output would stop lining up with cell k of the operand. A body that **asks about a mask**, because a chunk carries no per-cell mask to ask it of -- CArray ORs and propagates the masks, but `a[i] == UNDEF` is a question about one cell. And **more than seven operands**, because seven is the widest the sweep family is generated for.
|
|
60
60
|
|
|
61
61
|
Whichever drives it, an operand that is **read while the pass writes the array it is a view of** is copied once before the loop. `a = b * 2.0` reads the whole right-hand side and then assigns; a kernel walks cell by cell, which is the same thing until a cell it reads is one it has written -- `hi = lo * 2.0` over two blocks that share six cells, a transpose written over itself. The copy is what makes those read the values the expression started with, as CArray's own operators do, and it is what lets the chunked sweep take them: what it re-gathers per chunk is then a copy nothing is writing to. Sharing a root is the test rather than overlapping cells, a view knowing what it is a view of; `a = a + 1.0` is not that case, the cell read being the cell written.
|
|
62
62
|
|
|
@@ -180,7 +180,7 @@ Written with the indices named, the same thing is [`jit_for`](#extents-steps-and
|
|
|
180
180
|
|
|
181
181
|
```ruby
|
|
182
182
|
rows, columns = image.dim
|
|
183
|
-
smoothed = CArray.
|
|
183
|
+
smoothed = CArray.float64(rows, columns)
|
|
184
184
|
CArray.jit_for(1...(rows-1), 1...(columns-1)) { |i, j|
|
|
185
185
|
smoothed[i, j] = 0.25 * (image[i-1, j] + image[i+1, j] +
|
|
186
186
|
image[i, j-1] + image[i, j+1])
|
|
@@ -385,7 +385,7 @@ A row of a captured array is one place to put it, and the body makes one itself
|
|
|
385
385
|
|
|
386
386
|
```ruby
|
|
387
387
|
median = CArray.jit_stencil(image, border: :clamp) { |a|
|
|
388
|
-
w = CArray.
|
|
388
|
+
w = CArray.float64(9) # nine doubles on this cell's stack
|
|
389
389
|
w[0] = a[-1,-1]; w[1] = a[-1, 0]; w[2] = a[-1, 1]
|
|
390
390
|
w[3] = a[ 0,-1]; w[4] = a[ 0, 0]; w[5] = a[ 0, 1]
|
|
391
391
|
w[6] = a[ 1,-1]; w[7] = a[ 1, 0]; w[8] = a[ 1, 1]
|
|
@@ -396,7 +396,7 @@ median = CArray.jit_stencil(image, border: :clamp) { |a|
|
|
|
396
396
|
|
|
397
397
|
`sort(w)` is one of the functions the compiler brings with it (see [Intrinsics](03_SupportedFeatures.md#intrinsics)); at nine cells it is a comparator network with no branch in it. Written against a row of captured workspace instead, the same filter needs `work[rows, cols, 9]` -- 288 MB over a 2000x2000 image to hold 72 bytes at a time -- and `jit_stencil` cannot use one at all, its block having no index.
|
|
398
398
|
|
|
399
|
-
**What the clearing costs.** `CArray.
|
|
399
|
+
**What the clearing costs.** `CArray.float64(9)` starts its cells at zero every time the line runs, which for a body that writes all nine before reading any is work nobody asked for. Measured over the filter above on 2000x2000: **41.3 ns a cell, against `CArray.empty(:float64, [9])`'s 42.2** -- no difference, because the compiler sees the nine stores that follow and drops the clearing as dead. So the zeroed spelling is the one to write here, and it is the one that reads correctly.
|
|
400
400
|
|
|
401
401
|
Where the clearing is *not* dead it costs about what clearing that many bytes costs, and nothing else. Over 1000x1000, with a histogram built per cell and a body that reads back a cell it did not write:
|
|
402
402
|
|
|
@@ -126,17 +126,17 @@ Three spellings are taken, and they are CArray's own:
|
|
|
126
126
|
|
|
127
127
|
The types are the ones CArray has, minus the two a kernel computes with neither of: `int8` `int16` `int32` `int64`, `uint8` `uint16` `uint32` `uint64`, `float32` `float64`, `cmplx64` `cmplx128`, `boolean`. `object` and `fixlen` are refused.
|
|
128
128
|
|
|
129
|
-
The
|
|
129
|
+
The C type names -- `float`, `double`, `complex`, `dcomplex`, `byte`, `short` and `int` -- are refused, in `CArray.double(n)` and in `CArray.new(:double, [n])` alike: a block is written in the width names, and the message names the one to write. Two of them are worth reading twice: `float` is a float32 and `complex` is a cmplx64, the opposite of what Ruby's own `Float` and `Complex` would suggest.
|
|
130
130
|
|
|
131
131
|
`CArray.empty(data_type, dim)` is carray 3.0.2's spelling. A block is read rather than run, so a kernel takes it whatever version of CArray is loaded -- but calling it outside a kernel needs 3.0.2.
|
|
132
132
|
|
|
133
|
-
The shape is **written out**: an integer, or integers joined by `+`, `-` and `*`, so `CArray.
|
|
133
|
+
The shape is **written out**: an integer, or integers joined by `+`, `-` and `*`, so `CArray.float64(2 * 4 + 1)` is a nine-cell array. A length worked out from the integers the block captured is computed when the kernel starts, and that array is allocated on the heap (see below). A length from an index, a local or a cell is refused -- there would be no C array to declare and no bound to check against -- and so is a length of zero or less.
|
|
134
134
|
|
|
135
135
|
**More than one axis** is written the way CArray writes it, and each axis is checked against its own extent:
|
|
136
136
|
|
|
137
137
|
```ruby
|
|
138
138
|
CArray.jit_for(ny, nx) { |i, j|
|
|
139
|
-
m = CArray.
|
|
139
|
+
m = CArray.float64(3, 4) # double m[12]; packed row-major
|
|
140
140
|
3.times { |r| 4.times { |c| m[r, c] = coef[i, j, r, c] } }
|
|
141
141
|
...
|
|
142
142
|
}
|
|
@@ -147,7 +147,7 @@ The shape is literal, so the strides are constants the compiler can see: `m[r, c
|
|
|
147
147
|
Per-axis checking is the thing this buys, and the reason to write `m[r, c]` rather than flattening by hand. Write `m[r * 4 + c]` yourself and a `c` of 4 is a cell of the next row -- a real read, of the wrong number, saying nothing. Write `m[r, c]` and the column is held to four:
|
|
148
148
|
|
|
149
149
|
```ruby
|
|
150
|
-
m = CArray.
|
|
150
|
+
m = CArray.float64(3, 4)
|
|
151
151
|
3.times { |r| 5.times { |c| m[r, c] = 1.0 } }
|
|
152
152
|
#=> the subscript `c` on axis 1 of `m`, which is 3 x 4, reaches cell 4 of that
|
|
153
153
|
# axis's 4 cells, whose cells are 0 to 3: `c` runs 0 to 4 here.
|
|
@@ -155,16 +155,16 @@ m = CArray.double(3, 4)
|
|
|
155
155
|
|
|
156
156
|
A column the kernel works out is checked where it is reached, per axis as well, so a write past one axis raises rather than landing in the next row.
|
|
157
157
|
|
|
158
|
-
The size counts cells rather than axes: `CArray.
|
|
158
|
+
The size counts cells rather than axes: `CArray.float64(32, 32)` is 8 KiB, past what a frame holds, so it is allocated at the kernel's entry instead (see "Larger, and lengths the kernel works out").
|
|
159
159
|
|
|
160
160
|
To a C function a local array goes as what it is -- one flat run of cells, row after row -- so a `double[3][4]` is handed to `const double a[12]`, and the length is matched over every cell.
|
|
161
161
|
|
|
162
|
-
The compatibility layer is not taken. `CArray.zeros(4)`, `CArray.ones(4)`, `CArray.full(4, 1.0)`,
|
|
162
|
+
The compatibility layer is not taken. `CArray.zeros(4)`, `CArray.ones(4)`, `CArray.full(4, 1.0)`, and `CArray.empty(4)` are all refused, each naming the carray spelling it stands for. `factories.rb` opens by calling itself "Numo / NumPy-style" and "not the 'main' carray API"; the words inside a block are carray's own.
|
|
163
163
|
|
|
164
164
|
**A subscript is checked, and where depends on what it is made of.** Where the position is an inner loop's index at a literal offset and that loop states its range in literals, the reach is known as the block is read, and the C carries no test:
|
|
165
165
|
|
|
166
166
|
```ruby
|
|
167
|
-
t = CArray.
|
|
167
|
+
t = CArray.float64(4)
|
|
168
168
|
(0...4).each { |k| t[k + 1] = 0.0 }
|
|
169
169
|
#=> `t[k + 1]` reaches cell 4 of a local array of 4 cells, where the cells are
|
|
170
170
|
# 0 to 3: `k` runs 0 to 3 here.
|
|
@@ -178,7 +178,7 @@ Every other position -- a plain local, a value read out of an array, an index wh
|
|
|
178
178
|
out = CArray.int64(1)
|
|
179
179
|
|
|
180
180
|
CArray.jit_for(1) { |i|
|
|
181
|
-
w = CArray.
|
|
181
|
+
w = CArray.float64(4)
|
|
182
182
|
j = 0
|
|
183
183
|
while w[j] == 0.0 # reads off the end on the fifth pass
|
|
184
184
|
j += 1
|
|
@@ -191,7 +191,7 @@ CArray.jit_for(1) { |i|
|
|
|
191
191
|
**A local array is a workspace and not a value.** It has no methods and is not read bare:
|
|
192
192
|
|
|
193
193
|
```ruby
|
|
194
|
-
w = CArray.
|
|
194
|
+
w = CArray.float64(4)
|
|
195
195
|
x = w #=> `w` is a local array; index it, as in `w[0]`
|
|
196
196
|
x = w.sum #=> `sum` is a method CArray answers outside a kernel
|
|
197
197
|
```
|
|
@@ -215,8 +215,8 @@ Two kinds of local array do not stand in the frame: one whose cells come to more
|
|
|
215
215
|
```ruby
|
|
216
216
|
# n is an Integer the block closed over; 4096 doubles is 32 KiB
|
|
217
217
|
CArray.jit_for(rows) { |i|
|
|
218
|
-
w = CArray.
|
|
219
|
-
big = CArray.
|
|
218
|
+
w = CArray.float64(n) # allocated at the entry, freed at the exit
|
|
219
|
+
big = CArray.float64(4096) # the same, for its size alone
|
|
220
220
|
...
|
|
221
221
|
}
|
|
222
222
|
```
|
|
@@ -240,12 +240,12 @@ A `jit_function` body allocates nothing. It is called once per cell, so an alloc
|
|
|
240
240
|
A kernel that carries masks gives every local array a **shadow** of one byte a cell, declared beside the cells, and a cell of the array carries a mask the way a plain local carries one beside its value: what the expression written into it carried, and the masks of the branches the write stands in. So a window copied into a workspace keeps its holes, and reading a cell back is reading what it was made of.
|
|
241
241
|
|
|
242
242
|
```ruby
|
|
243
|
-
field = CArray.
|
|
243
|
+
field = CArray.float64(8).seq!(1.0)
|
|
244
244
|
field[2] = UNDEF
|
|
245
|
-
out = CArray.
|
|
245
|
+
out = CArray.float64(8)
|
|
246
246
|
|
|
247
247
|
CArray.jit_for(1...7) { |i|
|
|
248
|
-
w = CArray.
|
|
248
|
+
w = CArray.float64(3)
|
|
249
249
|
w[0] = field[i-1]; w[1] = field[i]; w[2] = field[i+1]
|
|
250
250
|
out[i] = w[0] + w[1] + w[2]
|
|
251
251
|
}
|
|
@@ -290,7 +290,7 @@ The name does not collide with anything the block writes. `sum = 0.0` beside `su
|
|
|
290
290
|
**`min` and `max`** answer what CArray's own `min` and `max` answer. A NaN never displaces the accumulator, so it is skipped wherever it stands -- first, middle or last:
|
|
291
291
|
|
|
292
292
|
```ruby
|
|
293
|
-
CArray.
|
|
293
|
+
CArray.float64(3) { [Float::NAN, 1.0, 2.0] }.min #=> 1.0
|
|
294
294
|
```
|
|
295
295
|
|
|
296
296
|
and an array of nothing but NaN comes back `NaN`, there being no number left to win, which is again CArray's answer. Of two cells that compare equal the first is kept, so `0.0` and `-0.0` come back in whichever order they stood, as they do from CArray -- which is why the fold is a comparison rather than C99 `fmin` / `fmax`, whose NaN rule is the same but which leave the choice between two zeros to the library. Over an integer array, which has no NaN, it is a comparison against the limit of the type. `sort` does not skip a NaN, for the reason the fold does: skipping drops a cell -- right for a fold, wrong for a sort. A Complex array is refused, Ruby not ordering Complex numbers either, and so is a boolean one.
|
|
@@ -538,9 +538,9 @@ A masked cell means "no value here". A plain CArray has no mask at all -- one co
|
|
|
538
538
|
When some array does carry one, the mask propagates the way CArray's own operators propagate it: any cell that fed a result masks that result, and an array that is written gets a mask if it did not have one.
|
|
539
539
|
|
|
540
540
|
```ruby
|
|
541
|
-
source = CArray.
|
|
541
|
+
source = CArray.float64(7).seq!(1.0)
|
|
542
542
|
source[2] = UNDEF
|
|
543
|
-
result = CArray.
|
|
543
|
+
result = CArray.float64(7)
|
|
544
544
|
|
|
545
545
|
CArray.jit_for(1...6) { |i| result[i] = source[i-1] + source[i+1] }
|
|
546
546
|
#=> result[1] and result[3] are UNDEF; they are the cells that read source[2]
|
|
@@ -559,7 +559,7 @@ Where a kernel leaves the masking implicit, the reference is CArray's operators
|
|
|
559
559
|
**A compare-exchange spreads a mask further than anything else does.** A branch decided by a missing cell masks what it writes, which is the rule everywhere; a swap writes two cells rather than one, and a sort runs compare-exchange over every cell again and again. So one missing cell does not stay one:
|
|
560
560
|
|
|
561
561
|
```ruby
|
|
562
|
-
row =
|
|
562
|
+
row = CA_FLOAT64([[5, 1, 4, 2, 3]])
|
|
563
563
|
row[0, 2] = UNDEF
|
|
564
564
|
|
|
565
565
|
CArray.jit_for(1) { |i| # a bubble sort over the row
|
|
@@ -738,7 +738,7 @@ Neville's interpolation is the shape that wants it. It walks a workspace of the
|
|
|
738
738
|
NEVILLE = CArray.jit_function(
|
|
739
739
|
"double neville(double x, const double xs[4], const double ys[4])"
|
|
740
740
|
) { |x, xs, ys|
|
|
741
|
-
t = CArray.
|
|
741
|
+
t = CArray.float64(4)
|
|
742
742
|
4.times { |k| t[k] = ys[k] }
|
|
743
743
|
(1...4).each { |m|
|
|
744
744
|
(0...4-m).each { |k|
|
|
@@ -777,13 +777,13 @@ DOT4 = CArray.jit_function("double dot4(const double x[4], const double y[4])")
|
|
|
777
777
|
}
|
|
778
778
|
|
|
779
779
|
CArray.jit_for(0...(n - 3)) { |i|
|
|
780
|
-
w = CArray.
|
|
780
|
+
w = CArray.float64(4)
|
|
781
781
|
w[0] = signal[i]; w[1] = signal[i+1]; w[2] = signal[i+2]; w[3] = signal[i+3]
|
|
782
782
|
smoothed[i] = DOT4.call(w, weights)
|
|
783
783
|
}
|
|
784
784
|
```
|
|
785
785
|
|
|
786
|
-
**What the declaration says is matched as the block is read**, not at the call. Both halves are written in the block -- the element type is the constructor and the length is a literal -- so `CArray.float32(4)` against `const double x[4]`, or `CArray.
|
|
786
|
+
**What the declaration says is matched as the block is read**, not at the call. Both halves are written in the block -- the element type is the constructor and the length is a literal -- so `CArray.float32(4)` against `const double x[4]`, or `CArray.float64(3)` against it, is refused before anything is compiled. A captured array is matched at the call instead (there is no array to look at until then), and the rules are the same ones: the element type exactly, and at least as many cells as a sized declarator asks for. More cells than it asks for is fine, as it is for a captured array -- the callee reads the four it was promised.
|
|
787
787
|
|
|
788
788
|
The length check is doing real work here rather than tidying up. A subscript on a pointer parameter is unchecked by design, and a local array is on the stack: a declaration that read more cells than the array holds would walk over the frame the return address is in.
|
|
789
789
|
|
|
@@ -793,8 +793,8 @@ A parameter that is not `const` may be written through, and the line after the c
|
|
|
793
793
|
SOLVE3 = CArray.jit_function("void solve3(const double a[12], double x[3])") { |a, x| ... }
|
|
794
794
|
|
|
795
795
|
CArray.jit_for(ny, nx) { |i, j|
|
|
796
|
-
rhs = CArray.
|
|
797
|
-
sol = CArray.
|
|
796
|
+
rhs = CArray.float64(12)
|
|
797
|
+
sol = CArray.float64(3)
|
|
798
798
|
...
|
|
799
799
|
SOLVE3.call(rhs, sol)
|
|
800
800
|
out[i, j] = sol[0]
|
|
@@ -810,8 +810,8 @@ Passing the same local array to two parameters -- `DOT4.call(w, w)` -- is well-f
|
|
|
810
810
|
In `jit_each`, `jit_map` and `jit_stencil` the block names no index, so a bare array name is the cell the loop is on and every operand has to line up with every other. An array handed to a C function is not one of those: it goes over whole, so its own length is nobody else's business.
|
|
811
811
|
|
|
812
812
|
```ruby
|
|
813
|
-
weights = CArray.
|
|
814
|
-
a = CArray.
|
|
813
|
+
weights = CArray.float64(4).seq # four coefficients
|
|
814
|
+
a = CArray.float64(1000).seq # a thousand cells to compute
|
|
815
815
|
|
|
816
816
|
CArray.jit_each { out = a + DOT4.call(weights, weights) }
|
|
817
817
|
```
|
|
@@ -854,7 +854,7 @@ A window may be opened inside a window, and `#call` may be made inside one: both
|
|
|
854
854
|
|
|
855
855
|
One flag per compiled object and one per thread, so two functions never share a window and neither do two threads. The second half matters more than it sounds: the object is shared whether or not anybody meant to share it, because two blocks with the same text compile to one function. So a program that never mentions threads can open two windows on one body, and one flag between them is a window neither of them can read -- the second to open puts down what the first had already recorded. The flag and the hook are `_Thread_local`, which costs about half a nanosecond a call, and only on a body that has a flag at all.
|
|
856
856
|
|
|
857
|
-
The thread is the native one, which for Ruby is the thread that opened the window -- except under the M:N scheduler
|
|
857
|
+
The thread is the native one, which for Ruby is the thread that opened the window -- except under the M:N scheduler, where a Ruby thread is not tied to one native thread and the window cannot be relied on to be the same copy throughout. Which threads that is depends on `RUBY_MN_THREADS`: unset or `0`, a non-main Ractor's threads; `1`, the main Ractor's threads other than the main thread as well; and from Ruby 4.1, `2`, the main thread too, so that no thread is outside it. `-1`, also from Ruby 4.1, takes every thread out of it.
|
|
858
858
|
|
|
859
859
|
What this leaves outside the window is a library that calls the body from threads of its own. The flag such a call raises belongs to the thread that raised it: the body stops answering there, as it should, and the window watching from Ruby sees nothing and raises nothing. A library that calls back on the thread it was called from -- which is what GSL and NLopt do -- is inside the window.
|
|
860
860
|
|
|
@@ -1046,7 +1046,7 @@ A pointer that points at numbers is indexed, and takes a CArray:
|
|
|
1046
1046
|
poly = CArray.jit_function("double (*)(double x, const double coef[3])") { |x, c|
|
|
1047
1047
|
c[0] + c[1] * x + c[2] * x * x
|
|
1048
1048
|
}
|
|
1049
|
-
poly.call(2.0, CArray.
|
|
1049
|
+
poly.call(2.0, CArray.float64(3) { |i| [1.0, 2.0, 3.0][i] }) # => 17.0
|
|
1050
1050
|
```
|
|
1051
1051
|
|
|
1052
1052
|
Nothing about the spelling had to be settled: C settled it long ago. `const` says a parameter may only be read, so `dydt[0] = ...` compiles and `coef[0] = ...` is refused; a declarator carries a length where there is one, so `coef[3]` is checked against the array it is given and `coef[]` is the caller's business, as it is in C. The whole ODE signature is therefore sayable, with no half of it invented here:
|
|
@@ -1139,7 +1139,7 @@ The other side of that: the address has to arrive with the call rather than with
|
|
|
1139
1139
|
```ruby
|
|
1140
1140
|
def moving_average (values, window)
|
|
1141
1141
|
n = values.elements
|
|
1142
|
-
out = CArray.
|
|
1142
|
+
out = CArray.float64(n)
|
|
1143
1143
|
CArray.jit_call("void (*)(double *out, const double *values, " \
|
|
1144
1144
|
"size_t n, size_t window)") {
|
|
1145
1145
|
n.times { |i|
|
data/docs/04_Compiling.md
CHANGED
|
@@ -67,7 +67,7 @@ In `~/.cache/carray-jit` (honouring `XDG_CACHE_HOME`), one directory per version
|
|
|
67
67
|
|
|
68
68
|
A kernel is only good for the version that generated it and the architecture it was built for, so those get their own directories. CArray's version is there for the same reason and one of its own: a kernel is handed CArray's memory, on layouts CArray decides, so one compiled against one version and run against the next would answer wrongly rather than fail to load.
|
|
69
69
|
|
|
70
|
-
The two numbers say different things and move on their own clocks: the first is the version that generated the kernel, the second the version it was generated against. This gem is not versioned with CArray, and one release of it meets more than one -- the dependency is `>= 3.0
|
|
70
|
+
The two numbers say different things and move on their own clocks: the first is the version that generated the kernel, the second the version it was generated against. This gem is not versioned with CArray, and one release of it meets more than one -- the dependency is `>= 3.1.0, < 3.2`, so every version in the 3.1 series satisfies it -- and a layout the kernel reaches into can move between them. A new release does not spend its cache budget on entries nothing can reach any more, and a home directory shared between machines -- over NFS, or between Rosetta and native -- does not have one architecture evicting the other's kernels. A directory whose newest entry has not been touched in 30 days (`CARRAY_JIT_CACHE_MAX_AGE_DAYS`) is removed.
|
|
71
71
|
|
|
72
72
|
```ruby
|
|
73
73
|
CArray::JIT.cache_root #=> "/home/you/.cache/carray-jit"
|
|
@@ -99,7 +99,7 @@ The path is expanded where it is given, so a relative one does not move when the
|
|
|
99
99
|
|
|
100
100
|
`CARRAY_JIT_CACHE` and `CARRAY_JIT_NO_CACHE` still come first, so whoever runs the program has the last word on where a cache may be written and whether there is one at all. `CArray::JIT.cache_root = nil` restores the default.
|
|
101
101
|
|
|
102
|
-
The cache is **bounded**: past `CARRAY_JIT_CACHE_LIMIT` kernels per environment (
|
|
102
|
+
The cache is **bounded**: past `CARRAY_JIT_CACHE_LIMIT` kernels per environment (2000 by default, so about 60 MB) the least recently used are evicted, source and object together. Reuse updates an entry's timestamp, so what a program actually runs stays. This is the one place carray-jit deliberately parts with RubyInline, whose `~/.ruby_inline` has no eviction at all and grows for the life of the account.
|
|
103
103
|
|
|
104
104
|
Removing a cached object never breaks a kernel already in use: unlinking a loaded shared object leaves its mapping intact.
|
|
105
105
|
|
|
@@ -143,7 +143,7 @@ carray_jit_contiguous (char **pointers, int64_t *strides, int64_t *bounds, ...)
|
|
|
143
143
|
const int64_t legendre_s0 = strides[0];
|
|
144
144
|
const double x = reals[0];
|
|
145
145
|
|
|
146
|
-
for (int64_t i = bounds[0]
|
|
146
|
+
for (int64_t i = bounds[0], i__limit = bounds[1]; i < i__limit; i++) {
|
|
147
147
|
double w;
|
|
148
148
|
double wy;
|
|
149
149
|
w = x * ((double *)(p_legendre))[i - 1];
|
|
@@ -165,7 +165,7 @@ root /home/you/.cache/carray-jit
|
|
|
165
165
|
environment 0.1.0-carray3.0.1-arm64-darwin24
|
|
166
166
|
kernels 3
|
|
167
167
|
size 50.1 KB
|
|
168
|
-
limit
|
|
168
|
+
limit 2000 kernels
|
|
169
169
|
|
|
170
170
|
other environments:
|
|
171
171
|
0.0.9-carray3.0.1-arm64-darwin24 1.2 MB last used 2026-01-01
|
|
@@ -217,7 +217,7 @@ carray_jit_contiguous (char **pointers, int64_t *strides, int64_t *bounds, ...)
|
|
|
217
217
|
| `CARRAY_JIT_DUMP` | Print generated C to stderr before compiling |
|
|
218
218
|
| `CARRAY_JIT_CACHE` | Cache directory (default `~/.cache/carray-jit`, or what `CArray::JIT.cache_root=` named), or `none` |
|
|
219
219
|
| `CARRAY_JIT_NO_CACHE` | Keep the cache in a temporary directory, removed on exit |
|
|
220
|
-
| `CARRAY_JIT_CACHE_LIMIT` | Kernels retained on disk, per environment (default
|
|
220
|
+
| `CARRAY_JIT_CACHE_LIMIT` | Kernels retained on disk, per environment (default 2000) |
|
|
221
221
|
| `CARRAY_JIT_CACHE_MAX_AGE_DAYS` | Days an unused environment's directory is kept (default 30) |
|
|
222
222
|
| `CARRAY_JIT_CC` | C compiler (default `RbConfig::CONFIG["CC"]`) |
|
|
223
223
|
| `CARRAY_JIT_REASSOCIATE` | `0` makes the serial accumulator the default for the process |
|
data/docs/05_DesignNotes.md
CHANGED
|
@@ -27,9 +27,7 @@ Narrowing them was tried and dropped, because it would change the answer and buy
|
|
|
27
27
|
|
|
28
28
|
`uint64` is neither: it is the width that cannot fold into `int64_t` at all, since the values it holds above 2^63 are the ones `int64_t` cannot carry, so it computes in a `uint64_t` of its own. See [Types](03_SupportedFeatures.md#unsigned-64-bit).
|
|
29
29
|
|
|
30
|
-
**A value with no data type follows Ruby.** A literal and a captured Numeric have no width of their own, so `2.0` is a double and `2` an Integer -- until they meet an array of their own kind, which lends them its width: `f32 * 2.0` is float32. What an array can lend is a width
|
|
31
|
-
|
|
32
|
-
`CArray.float` is float32; `CArray.double` is float64.
|
|
30
|
+
**A value with no data type follows Ruby.** A literal and a captured Numeric have no width of their own, so `2.0` is a double and `2` an Integer -- until they meet an array of their own kind, which lends them its width: `f32 * 2.0` is float32. What an array can lend is a width: an integer array has no width of a float to lend, so `i32 * 2.0` is a float64, and a float32 array lends its width to a Complex, so `f32 * 1i` is a cmplx64, as in CArray. A local that wants a particular type is seeded from a `CScalar`, which is a value with a data type. See [Locals](03_SupportedFeatures.md#locals-types-and-postfix-math).
|
|
33
31
|
|
|
34
32
|
## `-ffp-contract=off` is mandatory
|
|
35
33
|
|
data/docs/06_Cheatsheet.md
CHANGED
|
@@ -95,10 +95,10 @@ serial order -- a compensated summation, or checking against the loop.
|
|
|
95
95
|
|
|
96
96
|
A `CArray` the block makes is a C array of the block's own -- in any block
|
|
97
97
|
but a contraction's, of any number of axes, cleared at the line each pass
|
|
98
|
-
unless it is `CArray.empty(:type, [n])`. Note `CArray.
|
|
99
|
-
`CArray.
|
|
98
|
+
unless it is `CArray.empty(:type, [n])`. Note `CArray.float32` is float32 and
|
|
99
|
+
`CArray.cmplx64` is cmplx64. Up to 4 KiB it stands in the frame; past that,
|
|
100
100
|
or where the shape is written over an integer the block captured
|
|
101
|
-
(`CArray.
|
|
101
|
+
(`CArray.float64(n)`), the kernel allocates it at its entry and frees it at
|
|
102
102
|
its exit -- and then every subscript is checked where the cell is reached,
|
|
103
103
|
and a C function takes it only through a pointer with no length. Where the kernel carries masks
|
|
104
104
|
each cell gets a mask byte beside it, so `w[k] = UNDEF` and `w[k] == UNDEF`
|
|
@@ -125,7 +125,7 @@ calls, not methods on the array -- `w.sum` is refused.
|
|
|
125
125
|
|
|
126
126
|
```ruby
|
|
127
127
|
median = CArray.jit_for(rows) { |i|
|
|
128
|
-
w = CArray.
|
|
128
|
+
w = CArray.float64(9)
|
|
129
129
|
(0...9).each { |k| w[k] = a[i, k] }
|
|
130
130
|
sort(w)
|
|
131
131
|
out[i] = w[4]
|
|
@@ -135,8 +135,8 @@ median = CArray.jit_for(rows) { |i|
|
|
|
135
135
|
## Filling from the indices
|
|
136
136
|
|
|
137
137
|
```ruby
|
|
138
|
-
CArray.
|
|
139
|
-
grid = CArray.
|
|
138
|
+
CArray.float64(3, 4).jit_init { |i, j| i * 10.0 + j }
|
|
139
|
+
grid = CArray.float64(n)
|
|
140
140
|
grid.jit_init { |i| Math.sin(i * step) } # fills in place
|
|
141
141
|
```
|
|
142
142
|
|
|
@@ -149,8 +149,8 @@ no index is `jit_each`'s work, and is refused here saying so.
|
|
|
149
149
|
## A scalar from outside
|
|
150
150
|
|
|
151
151
|
```ruby
|
|
152
|
-
gain = CScalar.
|
|
153
|
-
total = CScalar.
|
|
152
|
+
gain = CScalar.float64() { 2.0 }
|
|
153
|
+
total = CScalar.int32()
|
|
154
154
|
|
|
155
155
|
CArray.jit_each { out = signal * gain } # read at every cell
|
|
156
156
|
CArray.jit_for(a.size) { |i| total[] += a[i] } # and written like any cell
|
|
@@ -164,8 +164,8 @@ axis to walk; written to, a bare name is a local of the block's own, so an
|
|
|
164
164
|
accumulator is `s[] = s[] + ...` and not `s = s + ...`.
|
|
165
165
|
|
|
166
166
|
Every iteration writing the one cell it has is what makes it an accumulator,
|
|
167
|
-
and what is left is what the same Ruby loop leaves. `CScalar.
|
|
168
|
-
cell zero-filled, as `CArray.
|
|
167
|
+
and what is left is what the same Ruby loop leaves. `CScalar.int32()` is that one
|
|
168
|
+
cell zero-filled, as `CArray.int32(1)` is; write `CScalar.int32() { 0 }` where the
|
|
169
169
|
starting value is part of what the code says.
|
|
170
170
|
|
|
171
171
|
See [CScalar](02_KernelShapes.md#a-cscalar-is-a-value-with-a-home) for how the
|
|
@@ -240,8 +240,8 @@ borrowed with `jit_extern`, which the generated file declares and calls by
|
|
|
240
240
|
name, are the two things such a body may reach outside its parameters.
|
|
241
241
|
|
|
242
242
|
```ruby
|
|
243
|
-
values = CArray.
|
|
244
|
-
out = CArray.
|
|
243
|
+
values = CArray.float64(8) { |i| i }
|
|
244
|
+
out = CArray.float64(8)
|
|
245
245
|
n = values.elements
|
|
246
246
|
|
|
247
247
|
CArray.jit_call("void (*)(double *out, const double *values, size_t n)") {
|