carray-jit 0.1.7 → 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.
Potentially problematic release.
This version of carray-jit might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/CHANGELOG.md +18 -0
- data/README.md +2 -2
- data/carray-jit.gemspec +4 -2
- data/docs/02_KernelShapes.md +5 -5
- data/docs/03_SupportedFeatures.md +27 -27
- data/docs/04_Compiling.md +1 -1
- 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 +38 -33
- data/lib/carray/jit/c_generator.rb +1 -1
- data/lib/carray/jit/call.rb +1 -1
- data/lib/carray/jit/compiler.rb +34 -17
- data/lib/carray/jit/node.rb +1 -1
- data/lib/carray/jit/type_assignment.rb +9 -4
- data/lib/carray/jit/version.rb +1 -1
- data/lib/carray/jit.rb +7 -9
- metadata +6 -6
data/docs/07_StepByStep.md
CHANGED
|
@@ -23,10 +23,10 @@ refuses something, the refusal is the answer and is printed under the code.
|
|
|
23
23
|
`hint: the method for work that reaches no neighbour`
|
|
24
24
|
|
|
25
25
|
```ruby
|
|
26
|
-
a =
|
|
27
|
-
b =
|
|
28
|
-
c =
|
|
29
|
-
out = CArray.
|
|
26
|
+
a = CA_FLOAT64([1, 2, 3])
|
|
27
|
+
b = CA_FLOAT64([2, 3, 4])
|
|
28
|
+
c = CA_FLOAT64([3, 4, 5])
|
|
29
|
+
out = CArray.float64(3)
|
|
30
30
|
|
|
31
31
|
CArray.jit_each { out = a + b * c }
|
|
32
32
|
out.to_a #=> [7.0, 14.0, 23.0]
|
|
@@ -53,9 +53,9 @@ half.data_type_name #=> "float64"
|
|
|
53
53
|
`hint: CArray will broadcast an axis, but not invent a rank`
|
|
54
54
|
|
|
55
55
|
```ruby
|
|
56
|
-
table = CArray.
|
|
57
|
-
row =
|
|
58
|
-
out = CArray.
|
|
56
|
+
table = CArray.float64(2, 3).seq!(1.0)
|
|
57
|
+
row = CA_FLOAT64([10, 20, 30]).reshape(1, 3) # 1x3, not 3: else `ndim mismatch`
|
|
58
|
+
out = CArray.float64(2, 3)
|
|
59
59
|
|
|
60
60
|
CArray.jit_each { out = table + row }
|
|
61
61
|
out.to_a #=> [[11.0, 22.0, 33.0], [14.0, 25.0, 36.0]]
|
|
@@ -66,8 +66,8 @@ out.to_a #=> [[11.0, 22.0, 33.0], [14.0, 25.0, 36.0]]
|
|
|
66
66
|
`hint: nothing special is needed`
|
|
67
67
|
|
|
68
68
|
```ruby
|
|
69
|
-
a =
|
|
70
|
-
out = CArray.
|
|
69
|
+
a = CA_FLOAT64([1, 2])
|
|
70
|
+
out = CArray.float64(2)
|
|
71
71
|
|
|
72
72
|
[2.0, 5.0].map { |gain|
|
|
73
73
|
CArray.jit_each { out = a * gain } # a captured scalar is an argument,
|
|
@@ -80,9 +80,9 @@ out = CArray.double(2)
|
|
|
80
80
|
`hint: CScalar`
|
|
81
81
|
|
|
82
82
|
```ruby
|
|
83
|
-
a =
|
|
84
|
-
gain = CScalar.
|
|
85
|
-
out = CArray.
|
|
83
|
+
a = CA_FLOAT64([1, 2])
|
|
84
|
+
gain = CScalar.float64() { 2.5 }
|
|
85
|
+
out = CArray.float64(2)
|
|
86
86
|
|
|
87
87
|
CArray.jit_each { out = a * gain }
|
|
88
88
|
out.to_a #=> [2.5, 5.0]
|
|
@@ -93,8 +93,8 @@ out.to_a #=> [2.5, 5.0]
|
|
|
93
93
|
`hint: the branch is an expression`
|
|
94
94
|
|
|
95
95
|
```ruby
|
|
96
|
-
a =
|
|
97
|
-
out = CArray.
|
|
96
|
+
a = CA_FLOAT64([-2, 0, 1.5, 3])
|
|
97
|
+
out = CArray.float64(4)
|
|
98
98
|
|
|
99
99
|
CArray.jit_each { out = a < 0.0 ? 0.0 : a }
|
|
100
100
|
out.to_a #=> [0.0, 0.0, 1.5, 3.0]
|
|
@@ -105,8 +105,8 @@ out.to_a #=> [0.0, 0.0, 1.5, 3.0]
|
|
|
105
105
|
`hint: the cell has to say which cell it reads`
|
|
106
106
|
|
|
107
107
|
```ruby
|
|
108
|
-
a = CArray.
|
|
109
|
-
out = CArray.
|
|
108
|
+
a = CArray.float64(5).seq!(1.0)
|
|
109
|
+
out = CArray.float64(5)
|
|
110
110
|
|
|
111
111
|
CArray.jit_for(5) { |i| out[i] = a[4 - i] }
|
|
112
112
|
out.to_a #=> [5.0, 4.0, 3.0, 2.0, 1.0]
|
|
@@ -117,8 +117,8 @@ out.to_a #=> [5.0, 4.0, 3.0, 2.0, 1.0]
|
|
|
117
117
|
`hint: where can the range start?`
|
|
118
118
|
|
|
119
119
|
```ruby
|
|
120
|
-
a =
|
|
121
|
-
out = CArray.
|
|
120
|
+
a = CA_FLOAT64([1, 3, 6, 10])
|
|
121
|
+
out = CArray.float64(4)
|
|
122
122
|
|
|
123
123
|
CArray.jit_for(1...4) { |i| out[i] = a[i] - a[i - 1] }
|
|
124
124
|
out.to_a #=> [0.0, 2.0, 3.0, 4.0]
|
|
@@ -131,8 +131,8 @@ out.to_a #=> [0.0, 2.0, 3.0, 4.0]
|
|
|
131
131
|
`hint: read what this loop just wrote`
|
|
132
132
|
|
|
133
133
|
```ruby
|
|
134
|
-
a =
|
|
135
|
-
out = CArray.
|
|
134
|
+
a = CA_FLOAT64([4, 8, 8, 8])
|
|
135
|
+
out = CArray.float64(4)
|
|
136
136
|
out[0] = a[0]
|
|
137
137
|
|
|
138
138
|
CArray.jit_for(1...4) { |i| out[i] = 0.5 * a[i] + 0.5 * out[i - 1] }
|
|
@@ -144,8 +144,8 @@ out.to_a #=> [4.0, 6.0, 7.0, 7.5]
|
|
|
144
144
|
`hint: two parameters, two extents`
|
|
145
145
|
|
|
146
146
|
```ruby
|
|
147
|
-
a = CArray.
|
|
148
|
-
out = CArray.
|
|
147
|
+
a = CArray.float64(2, 3).seq!(1.0)
|
|
148
|
+
out = CArray.float64(3, 2)
|
|
149
149
|
|
|
150
150
|
CArray.jit_for(2, 3) { |i, j| out[j, i] = a[i, j] }
|
|
151
151
|
out.to_a #=> [[1.0, 4.0], [2.0, 5.0], [3.0, 6.0]]
|
|
@@ -156,8 +156,8 @@ out.to_a #=> [[1.0, 4.0], [2.0, 5.0], [3.0, 6.0]]
|
|
|
156
156
|
`hint: a loop inside the cell`
|
|
157
157
|
|
|
158
158
|
```ruby
|
|
159
|
-
a =
|
|
160
|
-
out = CArray.
|
|
159
|
+
a = CA_FLOAT64([[1, 3, 2], [9, 4, 5]])
|
|
160
|
+
out = CArray.float64(2)
|
|
161
161
|
|
|
162
162
|
CArray.jit_for(2) { |i|
|
|
163
163
|
best = a[i, 0]
|
|
@@ -196,9 +196,9 @@ counts.to_a #=> [2, 3, 1]
|
|
|
196
196
|
`hint: Math`
|
|
197
197
|
|
|
198
198
|
```ruby
|
|
199
|
-
x =
|
|
200
|
-
y =
|
|
201
|
-
out = CArray.
|
|
199
|
+
x = CA_FLOAT64([3, 5])
|
|
200
|
+
y = CA_FLOAT64([4, 12])
|
|
201
|
+
out = CArray.float64(2)
|
|
202
202
|
|
|
203
203
|
CArray.jit_each { out = Math.sqrt(x * x + y * y) }
|
|
204
204
|
out.to_a #=> [5.0, 13.0]
|
|
@@ -209,8 +209,8 @@ out.to_a #=> [5.0, 13.0]
|
|
|
209
209
|
`hint: neither guesses`
|
|
210
210
|
|
|
211
211
|
```ruby
|
|
212
|
-
a =
|
|
213
|
-
out = CArray.
|
|
212
|
+
a = CA_FLOAT64([1, 2, 3])
|
|
213
|
+
out = CArray.float64(3)
|
|
214
214
|
|
|
215
215
|
CArray.jit_each { |i| out[i] = a[i] + 1.0 }
|
|
216
216
|
#=> CArray::JIT::Unsupported: this block names the arrays it reaches, so it
|
|
@@ -247,7 +247,7 @@ digits.to_a #=> [1, 2, 4, 5]
|
|
|
247
247
|
`hint: stop looking`
|
|
248
248
|
|
|
249
249
|
```ruby
|
|
250
|
-
rows =
|
|
250
|
+
rows = CA_FLOAT64([[1, 2, 9, 3], [5, 1, 1, 8]])
|
|
251
251
|
first = CArray.int32(2)
|
|
252
252
|
|
|
253
253
|
CArray.jit_for(2) { |i|
|
|
@@ -268,8 +268,8 @@ first.to_a #=> [2, 0]
|
|
|
268
268
|
`hint: the index that repeats`
|
|
269
269
|
|
|
270
270
|
```ruby
|
|
271
|
-
a = CArray.
|
|
272
|
-
b = CArray.
|
|
271
|
+
a = CArray.float64(2, 3).seq!(1.0)
|
|
272
|
+
b = CArray.float64(3, 2).seq!(1.0)
|
|
273
273
|
|
|
274
274
|
CArray.jit_contract { |i, j, k| a[i, k] * b[k, j] }.to_a
|
|
275
275
|
#=> [[22.0, 28.0], [49.0, 64.0]]
|
|
@@ -280,8 +280,8 @@ CArray.jit_contract { |i, j, k| a[i, k] * b[k, j] }.to_a
|
|
|
280
280
|
`hint: the row index repeats too, and must not be summed`
|
|
281
281
|
|
|
282
282
|
```ruby
|
|
283
|
-
x =
|
|
284
|
-
y =
|
|
283
|
+
x = CA_FLOAT64([[1, 2, 3], [4, 5, 6]])
|
|
284
|
+
y = CA_FLOAT64([[1, 1, 1], [2, 2, 2]])
|
|
285
285
|
|
|
286
286
|
CArray.jit_contract(:b) { |k| x[b, k] * y[b, k] }.to_a
|
|
287
287
|
#=> [6.0, 30.0]
|
|
@@ -293,7 +293,7 @@ CArray.jit_contract(:b) { |k| x[b, k] * y[b, k] }.to_a
|
|
|
293
293
|
`hint: no index, no extent, no edge`
|
|
294
294
|
|
|
295
295
|
```ruby
|
|
296
|
-
a =
|
|
296
|
+
a = CA_FLOAT64([1, 2, 3, 4])
|
|
297
297
|
|
|
298
298
|
CArray.jit_stencil(a, border: :zero) { |w| w[-1] + w[0] + w[1] }.to_a
|
|
299
299
|
#=> [3.0, 6.0, 9.0, 7.0]
|
|
@@ -306,9 +306,9 @@ CArray.jit_stencil(a, border: :zero) { |w| w[-1] + w[0] + w[1] }.to_a
|
|
|
306
306
|
`hint: ask about one cell`
|
|
307
307
|
|
|
308
308
|
```ruby
|
|
309
|
-
a =
|
|
309
|
+
a = CA_FLOAT64([1, 2, 3])
|
|
310
310
|
a[1] = UNDEF
|
|
311
|
-
out = CArray.
|
|
311
|
+
out = CArray.float64(3)
|
|
312
312
|
|
|
313
313
|
CArray.jit_for(3) { |i| out[i] = a[i] == UNDEF ? 0.0 : a[i] }
|
|
314
314
|
out.to_a #=> [1.0, 0.0, 3.0]
|
|
@@ -321,7 +321,7 @@ out.to_a #=> [1.0, 0.0, 3.0]
|
|
|
321
321
|
`hint: hand the kernel the column`
|
|
322
322
|
|
|
323
323
|
```ruby
|
|
324
|
-
table = CArray.
|
|
324
|
+
table = CArray.float64(3, 3)
|
|
325
325
|
column = table[nil, 1]
|
|
326
326
|
|
|
327
327
|
CArray.jit_for(3) { |i| column[i] = i + 1.0 }
|
|
@@ -362,8 +362,8 @@ out.to_a #=> [3]
|
|
|
362
362
|
`hint: which way does the loop have to run, and where do you say so?`
|
|
363
363
|
|
|
364
364
|
```ruby
|
|
365
|
-
a =
|
|
366
|
-
out = CArray.
|
|
365
|
+
a = CA_FLOAT64([1, 2, 3, 4, 5])
|
|
366
|
+
out = CArray.float64(5)
|
|
367
367
|
out[4] = a[4]
|
|
368
368
|
|
|
369
369
|
CArray.jit_for(3.step(0, -1)) { |i| out[i] = a[i] + out[i + 1] }
|
|
@@ -371,7 +371,7 @@ out.to_a #=> [15.0, 14.0, 12.0, 9.0, 5.0]
|
|
|
371
371
|
```
|
|
372
372
|
|
|
373
373
|
```ruby
|
|
374
|
-
out = CArray.
|
|
374
|
+
out = CArray.float64(5)
|
|
375
375
|
out[4] = a[4]
|
|
376
376
|
|
|
377
377
|
CArray.jit_for(0...4) { |i| out[i] = a[i] + out[i + 1] }
|
|
@@ -423,8 +423,8 @@ best[0] #=> 3
|
|
|
423
423
|
`hint: the grid is sorted`
|
|
424
424
|
|
|
425
425
|
```ruby
|
|
426
|
-
grid =
|
|
427
|
-
query =
|
|
426
|
+
grid = CA_FLOAT64([0, 1, 4, 9, 16])
|
|
427
|
+
query = CA_FLOAT64([0.5, 5.0, 9.0, 12.0])
|
|
428
428
|
found = CArray.int32(4)
|
|
429
429
|
|
|
430
430
|
CArray.jit_for(4) { |i|
|
|
@@ -480,7 +480,7 @@ CArray.jit_for(2) { |i|
|
|
|
480
480
|
```ruby
|
|
481
481
|
z = CA_CMPLX128([Complex(0, 1), Complex(1, 1)])
|
|
482
482
|
squared = CArray.cmplx128(2)
|
|
483
|
-
size = CArray.
|
|
483
|
+
size = CArray.float64(2)
|
|
484
484
|
|
|
485
485
|
CArray.jit_each { squared = z * z }
|
|
486
486
|
CArray.jit_each { size = z.abs }
|
|
@@ -495,7 +495,7 @@ size.to_a #=> [1.0, 1.4142135623730951]
|
|
|
495
495
|
|
|
496
496
|
```ruby
|
|
497
497
|
random = CArray::Rng.new(seed: 3)
|
|
498
|
-
draws = CArray.
|
|
498
|
+
draws = CArray.float64(100_000)
|
|
499
499
|
|
|
500
500
|
CArray.jit_for(100_000) { |i| draws[i] = random.random }
|
|
501
501
|
draws.mean.round(4) #=> 0.5006
|
|
@@ -27,9 +27,9 @@ DWELL = 5 # after this many in a row
|
|
|
27
27
|
|
|
28
28
|
# A series that wanders across both lines, with noise sitting on them.
|
|
29
29
|
random = CArray::Rng.new(seed: 20260913)
|
|
30
|
-
noise = CArray.
|
|
30
|
+
noise = CArray.float64(SAMPLES)
|
|
31
31
|
noise.random!(rng: random)
|
|
32
|
-
reading = CArray.
|
|
32
|
+
reading = CArray.float64(SAMPLES)
|
|
33
33
|
CArray.jit_for(SAMPLES) { |i|
|
|
34
34
|
slow = 64.0 + 12.0 * Math.sin(i / 90000.0) + 4.0 * Math.sin(i / 700.0)
|
|
35
35
|
reading[i] = slow + 6.0 * (noise[i] - 0.5)
|
|
@@ -177,21 +177,21 @@ n = 15
|
|
|
177
177
|
random = Random.new(20260909)
|
|
178
178
|
# Knots that are not equally spaced -- measurements rarely are, and nothing
|
|
179
179
|
# above assumed they would be.
|
|
180
|
-
x = CArray.
|
|
180
|
+
x = CArray.float64(n) { |i| 6.0 * i / (n - 1) }
|
|
181
181
|
(1...(n-1)).each { |i| x[i] += random.rand(-0.12..0.12) }
|
|
182
|
-
y = CArray.
|
|
182
|
+
y = CArray.float64(n) { |i| curve(x[i]) }
|
|
183
183
|
|
|
184
|
-
work = Array.new(7) { CArray.
|
|
184
|
+
work = Array.new(7) { CArray.float64(n) }
|
|
185
185
|
natural = moments(x, y, n, work, nil).copy
|
|
186
186
|
clamped = moments(x, y, n, work, [slope(x[0]), slope(x[n-1])]).copy
|
|
187
187
|
not_a_knot = moments(x, y, n, work, :not_a_knot).copy
|
|
188
188
|
|
|
189
189
|
points = 601
|
|
190
|
-
query = CArray.
|
|
191
|
-
truth = CArray.
|
|
190
|
+
query = CArray.float64(points) { |k| x[0] + (x[n-1] - x[0]) * k / (points - 1) }
|
|
191
|
+
truth = CArray.float64(points) { |k| curve(query[k]) }
|
|
192
192
|
|
|
193
|
-
value = CArray.
|
|
194
|
-
derivative = CArray.
|
|
193
|
+
value = CArray.float64(points)
|
|
194
|
+
derivative = CArray.float64(points)
|
|
195
195
|
|
|
196
196
|
puts "cubic spline through #{n} unevenly spaced points, sampled at #{points}"
|
|
197
197
|
|
|
@@ -222,9 +222,9 @@ puts format(" not-knot S''' jumps by %.1e at x[1] and %.1e at x[n-2] -- neithe
|
|
|
222
222
|
|
|
223
223
|
# The interpolation itself: all of them pass through every knot, so all three
|
|
224
224
|
# are asked, not just the one whose name comes first.
|
|
225
|
-
knots = CArray.
|
|
226
|
-
at_knots = CArray.
|
|
227
|
-
at_knots_slope = CArray.
|
|
225
|
+
knots = CArray.float64(n) { |i| x[i] }
|
|
226
|
+
at_knots = CArray.float64(n)
|
|
227
|
+
at_knots_slope = CArray.float64(n)
|
|
228
228
|
{ "natural" => natural, "clamped" => clamped, "not-a-knot" => not_a_knot }.each do |name, moment|
|
|
229
229
|
evaluate(x, y, moment, n, knots, at_knots, at_knots_slope)
|
|
230
230
|
puts format(" %-10s max |S(x_i) - y_i| = %.2e", name, (at_knots - y).abs.max)
|
|
@@ -233,8 +233,8 @@ end
|
|
|
233
233
|
# The query grid above is sorted, so the sweep applies to it -- and gives back
|
|
234
234
|
# the same doubles, not merely close ones: the interval a point lands in is the
|
|
235
235
|
# same interval, and the polynomial evaluated in it is the same expression.
|
|
236
|
-
swept = CArray.
|
|
237
|
-
swept_slope = CArray.
|
|
236
|
+
swept = CArray.float64(points)
|
|
237
|
+
swept_slope = CArray.float64(points)
|
|
238
238
|
resample(x, y, clamped, n, query, swept, swept_slope, CArray.int32(points))
|
|
239
239
|
puts " the sorted sweep agrees bit for bit #{swept.to_a == results["clamped"][0].to_a}"
|
|
240
240
|
|
|
@@ -310,11 +310,11 @@ end
|
|
|
310
310
|
|
|
311
311
|
puts
|
|
312
312
|
[[200, 20_000], [2_000, 200_000]].each do |size, sampled|
|
|
313
|
-
knots = CArray.
|
|
314
|
-
values = CArray.
|
|
315
|
-
scratch = Array.new(7) { CArray.
|
|
316
|
-
at = CArray.
|
|
317
|
-
out, slopes = CArray.
|
|
313
|
+
knots = CArray.float64(size) { |i| 6.0 * i / (size - 1) }
|
|
314
|
+
values = CArray.float64(size) { |i| curve(knots[i]) }
|
|
315
|
+
scratch = Array.new(7) { CArray.float64(size) }
|
|
316
|
+
at = CArray.float64(sampled) { |k| 6.0 * k / (sampled - 1) }
|
|
317
|
+
out, slopes = CArray.float64(sampled), CArray.float64(sampled)
|
|
318
318
|
|
|
319
319
|
fit = time(20) { moments(knots, values, size, scratch, nil) }
|
|
320
320
|
fit_ruby = time(3) { ruby_moments(knots, values, size, scratch) }
|
|
@@ -20,7 +20,7 @@ require "carray/jit"
|
|
|
20
20
|
|
|
21
21
|
# A grey ramp, top to bottom, with a brighter disc sitting in it.
|
|
22
22
|
def picture (rows, columns)
|
|
23
|
-
image = CArray.
|
|
23
|
+
image = CArray.float64(rows, columns)
|
|
24
24
|
CArray.jit_for(rows, columns) { |y, x|
|
|
25
25
|
dy = (y - rows / 2.0) / (rows / 2.0)
|
|
26
26
|
dx = (x - columns / 2.0) / (columns / 2.0) * 0.42
|
|
@@ -38,7 +38,7 @@ end
|
|
|
38
38
|
def dither (image)
|
|
39
39
|
rows, columns = image.dim
|
|
40
40
|
out = CArray.int8(rows, columns)
|
|
41
|
-
work = CArray.
|
|
41
|
+
work = CArray.float64(rows + 1, columns + 2)
|
|
42
42
|
work[0..-2, 1..-2] = image
|
|
43
43
|
CArray.jit_for(1) { |z|
|
|
44
44
|
(0...rows).each { |y|
|
|
@@ -119,7 +119,7 @@ puts format(" %.1f ms compiled, %.0f ms in Ruby (%.0fx)",
|
|
|
119
119
|
def dither_backwards (image)
|
|
120
120
|
rows, columns = image.dim
|
|
121
121
|
out = CArray.int8(rows, columns)
|
|
122
|
-
work = CArray.
|
|
122
|
+
work = CArray.float64(rows + 1, columns + 2)
|
|
123
123
|
work[0..-2, 1..-2] = image
|
|
124
124
|
CArray.jit_for(1) { |z|
|
|
125
125
|
(0...rows).each { |y|
|
|
@@ -28,19 +28,19 @@ random = CArray::Rng.new(seed: 20260913)
|
|
|
28
28
|
|
|
29
29
|
# Stations of very different sizes: the square of a uniform draw lands most
|
|
30
30
|
# of the readings on the low-numbered ones.
|
|
31
|
-
station = CArray.
|
|
31
|
+
station = CArray.float64(READINGS)
|
|
32
32
|
station.random!(rng: random)
|
|
33
33
|
station = ((station ** 2) * STATIONS).int32
|
|
34
34
|
|
|
35
|
-
value = CArray.
|
|
35
|
+
value = CArray.float64(READINGS)
|
|
36
36
|
value.random!(rng: random)
|
|
37
37
|
value = value * 40.0
|
|
38
38
|
|
|
39
39
|
LIMIT = 35.0
|
|
40
40
|
|
|
41
41
|
count = CArray.int64(STATIONS)
|
|
42
|
-
total = CArray.
|
|
43
|
-
peak = CArray.
|
|
42
|
+
total = CArray.float64(STATIONS)
|
|
43
|
+
peak = CArray.float64(STATIONS).fill(-Float::INFINITY)
|
|
44
44
|
peak_at = CArray.int64(STATIONS).fill(-1)
|
|
45
45
|
exceed = CArray.int64(STATIONS)
|
|
46
46
|
|
|
@@ -19,7 +19,7 @@ dx = 1.0 / (n - 1)
|
|
|
19
19
|
dt = 2.5e-5 # eight times the step an explicit scheme would survive
|
|
20
20
|
r = dt / (dx * dx)
|
|
21
21
|
|
|
22
|
-
temperature = CArray.
|
|
22
|
+
temperature = CArray.float64(n)
|
|
23
23
|
# A hot band in the middle of a cold rod, ends held at zero.
|
|
24
24
|
CArray.jit_for(n) { |i| temperature[i] = 0.0 }
|
|
25
25
|
(n * 4 / 10...(n * 6 / 10)).each { |i| temperature[i] = 1.0 }
|
|
@@ -32,8 +32,8 @@ lower = -r
|
|
|
32
32
|
diagonal = 1.0 + 2.0 * r
|
|
33
33
|
upper = -r
|
|
34
34
|
|
|
35
|
-
cc = CArray.
|
|
36
|
-
dd = CArray.
|
|
35
|
+
cc = CArray.float64(n)
|
|
36
|
+
dd = CArray.float64(n)
|
|
37
37
|
|
|
38
38
|
def solve (temperature, cc, dd, lower, diagonal, upper, n)
|
|
39
39
|
cc[0] = 0.0 # boundary: T[0] fixed
|
|
@@ -103,8 +103,8 @@ def time (repeats)
|
|
|
103
103
|
end
|
|
104
104
|
|
|
105
105
|
[400, 4_000, 40_000].each do |size|
|
|
106
|
-
field = CArray.
|
|
107
|
-
work = [CArray.
|
|
106
|
+
field = CArray.float64(size).seq! { |i| i.to_f / size }
|
|
107
|
+
work = [CArray.float64(size), CArray.float64(size)]
|
|
108
108
|
compiled = time(20) { solve(field, *work, lower, diagonal, upper, size) }
|
|
109
109
|
interpreted = time(3) { ruby_solve(field, *work, lower, diagonal, upper, size) }
|
|
110
110
|
puts format(" n = %6d %7.1f us compiled %8.1f us Ruby %3.0fx",
|
|
@@ -21,10 +21,10 @@ require "carray/jit"
|
|
|
21
21
|
n = 200_000
|
|
22
22
|
tolerance = 1e-13
|
|
23
23
|
|
|
24
|
-
mean_anomaly = CArray.
|
|
24
|
+
mean_anomaly = CArray.float64(n) { |i| 2.0 * Math::PI * i / n - Math::PI }
|
|
25
25
|
|
|
26
26
|
def solve (mean_anomaly, eccentricity, tolerance)
|
|
27
|
-
eccentric = CArray.
|
|
27
|
+
eccentric = CArray.float64(mean_anomaly.elements)
|
|
28
28
|
CArray.jit_for(mean_anomaly.elements) { |i|
|
|
29
29
|
guess = mean_anomaly[i] # good enough for any e < 1
|
|
30
30
|
step = 1.0
|
|
@@ -53,7 +53,7 @@ end
|
|
|
53
53
|
# known. Counting is a second local; the loop is otherwise the same.
|
|
54
54
|
eccentricity = 0.9
|
|
55
55
|
passes = CArray.int32(n)
|
|
56
|
-
roots = CArray.
|
|
56
|
+
roots = CArray.float64(n)
|
|
57
57
|
CArray.jit_for(n) { |i|
|
|
58
58
|
guess = mean_anomaly[i]
|
|
59
59
|
step = 1.0
|
|
@@ -78,7 +78,7 @@ puts " a cap in an extent would have been #{passes.max} for every cell"
|
|
|
78
78
|
# the condition is read at the top of every pass, `step` is a local before the
|
|
79
79
|
# loop, and no accumulation is reassociated -- so the two take the same passes
|
|
80
80
|
# over the same arithmetic.
|
|
81
|
-
in_ruby = CArray.
|
|
81
|
+
in_ruby = CArray.float64(n)
|
|
82
82
|
(0...n).each do |i|
|
|
83
83
|
guess = mean_anomaly[i]
|
|
84
84
|
step = 1.0
|
|
@@ -98,17 +98,17 @@ end
|
|
|
98
98
|
# differently from `sin` on its own. Ruby calls `sin`.
|
|
99
99
|
differing = (0...n).count { |i| roots[i] != in_ruby[i] }
|
|
100
100
|
|
|
101
|
-
iterate = CArray.
|
|
101
|
+
iterate = CArray.float64(n) # one Newton step, in Ruby
|
|
102
102
|
(0...n).each do |i|
|
|
103
103
|
guess = mean_anomaly[i]
|
|
104
104
|
iterate[i] = guess - (guess - eccentricity * Math.sin(guess) - mean_anomaly[i]) /
|
|
105
105
|
(1.0 - eccentricity * Math.cos(guess))
|
|
106
106
|
end
|
|
107
107
|
|
|
108
|
-
alone = CArray.
|
|
108
|
+
alone = CArray.float64(n)
|
|
109
109
|
CArray.jit_for(n) { |i| alone[i] = Math.sin(iterate[i]) }
|
|
110
|
-
together = CArray.
|
|
111
|
-
unused = CArray.
|
|
110
|
+
together = CArray.float64(n)
|
|
111
|
+
unused = CArray.float64(n)
|
|
112
112
|
CArray.jit_for(n) { |i|
|
|
113
113
|
together[i] = Math.sin(iterate[i])
|
|
114
114
|
unused[i] = Math.cos(iterate[i])
|
|
@@ -123,7 +123,7 @@ puts format(" a sine beside a cosine differs for %d of the same arguments",
|
|
|
123
123
|
|
|
124
124
|
# With no transcendental in it, the same Newton iteration agrees bit for bit
|
|
125
125
|
# -- a cube root, written the same way, over the same anomalies:
|
|
126
|
-
cube = CArray.
|
|
126
|
+
cube = CArray.float64(n)
|
|
127
127
|
CArray.jit_for(n) { |i|
|
|
128
128
|
x = 1.0 + mean_anomaly[i].abs
|
|
129
129
|
step = 1.0
|
|
@@ -133,7 +133,7 @@ CArray.jit_for(n) { |i|
|
|
|
133
133
|
end
|
|
134
134
|
cube[i] = x
|
|
135
135
|
}
|
|
136
|
-
cube_in_ruby = CArray.
|
|
136
|
+
cube_in_ruby = CArray.float64(n)
|
|
137
137
|
(0...n).each do |i|
|
|
138
138
|
a = 1.0 + mean_anomaly[i].abs
|
|
139
139
|
x = a
|
|
@@ -23,16 +23,16 @@ N = 2_000 # samples in the table
|
|
|
23
23
|
Q = 500_000 # queries against it
|
|
24
24
|
|
|
25
25
|
# A grid that crowds towards zero, and a function sampled on it.
|
|
26
|
-
knots = (CArray.
|
|
26
|
+
knots = (CArray.float64(N).seq!(0, 1.0 / (N - 1)) ** 2) * 100
|
|
27
27
|
table = knots.sin
|
|
28
|
-
query = CArray.
|
|
28
|
+
query = CArray.float64(Q).seq!(0, 99.9 / Q)
|
|
29
29
|
|
|
30
30
|
# The search and the interpolation in one pass. `lo` and `hi` are the cell's
|
|
31
31
|
# own, so the kernel reads `knots[lo]` at an address no other cell shares --
|
|
32
32
|
# a gather, written as the subscript it is.
|
|
33
33
|
def interpolate (knots, table, query)
|
|
34
34
|
n = knots.elements
|
|
35
|
-
out = CArray.
|
|
35
|
+
out = CArray.float64(query.elements)
|
|
36
36
|
CArray.jit_for(query.elements) { |i|
|
|
37
37
|
t = query[i]
|
|
38
38
|
lo = 0
|
|
@@ -25,24 +25,24 @@ HALF = WINDOW / 2
|
|
|
25
25
|
|
|
26
26
|
# A smooth signal, some noise, and a spike every few hundred samples.
|
|
27
27
|
random = CArray::Rng.new(seed: 20260913)
|
|
28
|
-
noise = CArray.
|
|
28
|
+
noise = CArray.float64(SAMPLES)
|
|
29
29
|
noise.random!(rng: random)
|
|
30
|
-
chance = CArray.
|
|
30
|
+
chance = CArray.float64(SAMPLES)
|
|
31
31
|
chance.random!(rng: random)
|
|
32
|
-
signal = CArray.
|
|
33
|
-
clean = CArray.
|
|
32
|
+
signal = CArray.float64(SAMPLES)
|
|
33
|
+
clean = CArray.float64(SAMPLES)
|
|
34
34
|
CArray.jit_for(SAMPLES) { |i|
|
|
35
35
|
smooth = 10.0 * Math.sin(i / 5000.0) + 0.5 * (noise[i] - 0.5)
|
|
36
36
|
clean[i] = smooth
|
|
37
37
|
signal[i] = chance[i] < 0.002 ? smooth + 40.0 * (chance[i] * 500.0 - 0.5) : smooth
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
sorted_out = CArray.
|
|
41
|
-
network_out = CArray.
|
|
40
|
+
sorted_out = CArray.float64(SAMPLES)
|
|
41
|
+
network_out = CArray.float64(SAMPLES)
|
|
42
42
|
|
|
43
43
|
# One: the window copied into the cell's own row of a workspace and put in
|
|
44
44
|
# order there. Straightforward, and every comparison is a memory access.
|
|
45
|
-
workspace = CArray.
|
|
45
|
+
workspace = CArray.float64(SAMPLES, WINDOW)
|
|
46
46
|
|
|
47
47
|
def by_sorting (signal, out, workspace)
|
|
48
48
|
n = signal.elements
|
|
@@ -15,7 +15,7 @@ n = 500_000
|
|
|
15
15
|
random = Random.new(20260902)
|
|
16
16
|
|
|
17
17
|
# A price series: a multiplicative random walk with a slow drift.
|
|
18
|
-
price = CArray.
|
|
18
|
+
price = CArray.float64(n)
|
|
19
19
|
value = 100.0
|
|
20
20
|
n.times do |i|
|
|
21
21
|
value *= 1.0 + random.rand(-0.004..0.004) + 0.000002 * Math.sin(i / 5000.0)
|
|
@@ -23,9 +23,9 @@ n.times do |i|
|
|
|
23
23
|
end
|
|
24
24
|
|
|
25
25
|
alpha = 0.02
|
|
26
|
-
smoothed = CArray.
|
|
27
|
-
peak = CArray.
|
|
28
|
-
drawdown = CArray.
|
|
26
|
+
smoothed = CArray.float64(n)
|
|
27
|
+
peak = CArray.float64(n)
|
|
28
|
+
drawdown = CArray.float64(n)
|
|
29
29
|
|
|
30
30
|
smoothed[0] = price[0]
|
|
31
31
|
peak[0] = price[0]
|
|
@@ -58,8 +58,8 @@ puts format(" worst drawdown %.2f%% at sample %d", worst * 100, (drawdown.eq(
|
|
|
58
58
|
# block closed over, and it reaches the kernel as an argument, so changing the
|
|
59
59
|
# window does not compile anything again.
|
|
60
60
|
window = 200
|
|
61
|
-
cumulative = CArray.
|
|
62
|
-
rolling = CArray.
|
|
61
|
+
cumulative = CArray.float64(n)
|
|
62
|
+
rolling = CArray.float64(n)
|
|
63
63
|
cumulative[0] = price[0]
|
|
64
64
|
CArray.jit_for(1...n) { |i| cumulative[i] = cumulative[i-1] + price[i] }
|
|
65
65
|
CArray.jit_for(window...n) { |i|
|
|
@@ -36,20 +36,20 @@ LEVELS = 91 # 1000 hPa to 100 hPa, every 10
|
|
|
36
36
|
COLUMNS = 4_000 # soundings to climb
|
|
37
37
|
SUBSTEPS = 4 # moist steps between two levels
|
|
38
38
|
|
|
39
|
-
pressure = CArray.
|
|
39
|
+
pressure = CArray.float64(LEVELS).seq!(1000.0, -10.0)
|
|
40
40
|
|
|
41
41
|
# Synthetic soundings: a lapse rate of 6.5 K/km over a surface that varies
|
|
42
42
|
# from column to column, isothermal above the tropopause.
|
|
43
43
|
random = CArray::Rng.new(seed: 20260913)
|
|
44
|
-
surface = CArray.
|
|
44
|
+
surface = CArray.float64(COLUMNS)
|
|
45
45
|
surface.random!(rng: random)
|
|
46
46
|
surface = surface * 12.0 + 22.0 + 273.15 # 22 to 34 degC
|
|
47
|
-
spread = CArray.
|
|
47
|
+
spread = CArray.float64(COLUMNS)
|
|
48
48
|
spread.random!(rng: random)
|
|
49
49
|
spread = spread * 14.0 + 2.0 # dew point depression
|
|
50
50
|
|
|
51
|
-
environment = CArray.
|
|
52
|
-
height = CArray.
|
|
51
|
+
environment = CArray.float64(COLUMNS, LEVELS)
|
|
52
|
+
height = CArray.float64(LEVELS)
|
|
53
53
|
LEVELS.times { |k| height[k] = 44330.0 * (1.0 - (pressure[k] / 1013.25) ** 0.1903) }
|
|
54
54
|
CArray.jit_for(COLUMNS, LEVELS) { |c, k|
|
|
55
55
|
lapsed = surface[c] - 0.0065 * height[k]
|
|
@@ -57,11 +57,11 @@ CArray.jit_for(COLUMNS, LEVELS) { |c, k|
|
|
|
57
57
|
}
|
|
58
58
|
dewpoint = environment[nil, 0] - spread
|
|
59
59
|
|
|
60
|
-
cape = CArray.
|
|
61
|
-
cin = CArray.
|
|
62
|
-
lcl = CArray.
|
|
63
|
-
lfc = CArray.
|
|
64
|
-
el = CArray.
|
|
60
|
+
cape = CArray.float64(COLUMNS)
|
|
61
|
+
cin = CArray.float64(COLUMNS)
|
|
62
|
+
lcl = CArray.float64(COLUMNS)
|
|
63
|
+
lfc = CArray.float64(COLUMNS)
|
|
64
|
+
el = CArray.float64(COLUMNS)
|
|
65
65
|
|
|
66
66
|
def climb (pressure, environment, dewpoint, cape, cin, lcl, lfc, el)
|
|
67
67
|
columns, levels = environment.dim
|
|
@@ -21,7 +21,7 @@ require "carray/jit"
|
|
|
21
21
|
N = 500_000
|
|
22
22
|
|
|
23
23
|
def accumulators
|
|
24
|
-
Array.new(9) { CScalar.
|
|
24
|
+
Array.new(9) { CScalar.float64() { 0.0 } }
|
|
25
25
|
end
|
|
26
26
|
|
|
27
27
|
def sum_series (n)
|
|
@@ -103,7 +103,7 @@ puts format(" every one of the nine matches the Ruby loop bit for bit: %s",
|
|
|
103
103
|
# order, and the default asks for the faster one, which by splitting the
|
|
104
104
|
# accumulation is also what limits the cancellation.
|
|
105
105
|
def harmonic (n, reassociate)
|
|
106
|
-
out = CScalar.
|
|
106
|
+
out = CScalar.float64() { 0.0 }
|
|
107
107
|
CArray.jit_for(1, reassociate: reassociate) { |c|
|
|
108
108
|
total = 0.0
|
|
109
109
|
(1..n).each { |k| total = total + 1.0 / (k * 1.0) }
|
|
@@ -114,7 +114,7 @@ end
|
|
|
114
114
|
|
|
115
115
|
split = harmonic(N, true)
|
|
116
116
|
serial = harmonic(N, false)
|
|
117
|
-
by_carray = (1.0 / CArray.
|
|
117
|
+
by_carray = (1.0 / CArray.float64(N).seq!(1.0)).sum
|
|
118
118
|
|
|
119
119
|
puts
|
|
120
120
|
puts "the harmonic sum as an inner loop"
|
|
@@ -28,12 +28,12 @@ count = 200_000
|
|
|
28
28
|
random = Random.new(20260902)
|
|
29
29
|
|
|
30
30
|
# A flattened, tilted blob: wide in x, narrower in y, nearly flat in z.
|
|
31
|
-
points = CArray.
|
|
31
|
+
points = CArray.float64(count, 3) { |p, k| random.rand(-1.0..1.0) }
|
|
32
32
|
points[nil, 1] = points[nil, 1] * 0.4
|
|
33
33
|
points[nil, 2] = points[nil, 2] * 0.05
|
|
34
34
|
|
|
35
35
|
angle = Math::PI / 6
|
|
36
|
-
rotation = CArray.
|
|
36
|
+
rotation = CArray.float64(3, 3)
|
|
37
37
|
rotation[0, nil] = [Math.cos(angle), -Math.sin(angle), 0.0]
|
|
38
38
|
rotation[1, nil] = [Math.sin(angle), Math.cos(angle), 0.0]
|
|
39
39
|
rotation[2, nil] = [0.0, 0.0, 1.0]
|
|
@@ -90,7 +90,7 @@ puts format(" the cloud's total %.1f, and per point summed again %.1f",
|
|
|
90
90
|
whole[0], squared.sum)
|
|
91
91
|
|
|
92
92
|
# The same rotation written as a Ruby loop.
|
|
93
|
-
reference = CArray.
|
|
93
|
+
reference = CArray.float64(count, 3)
|
|
94
94
|
started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
95
95
|
count.times do |p|
|
|
96
96
|
3.times do |i|
|
|
@@ -115,7 +115,7 @@ puts " identical: #{rotated.to_a == reference.to_a}"
|
|
|
115
115
|
|
|
116
116
|
# An index whose axes disagree is the mistake this notation exists to catch.
|
|
117
117
|
begin
|
|
118
|
-
wrong = CArray.
|
|
118
|
+
wrong = CArray.float64(4, 4).seq!(1.0)
|
|
119
119
|
CArray.jit_contract { |p, i, j| points[p, j] * wrong[i, j] }
|
|
120
120
|
rescue CArray::JIT::Unsupported => error
|
|
121
121
|
puts " refused: #{error.message.lines.first.strip}"
|