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.
Potentially problematic release.
This version of carray-jit might be problematic. Click here for more details.
- 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
data/docs/07_StepByStep.ja.md
CHANGED
|
@@ -23,10 +23,10 @@ require "carray/jit"
|
|
|
23
23
|
`ヒント: 隣に手を伸ばさない仕事のためのメソッド`
|
|
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
|
`ヒント: CArray は軸を伸ばすが、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) # 3 ではなく 1x3。でないと 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
|
`ヒント: 特別なことは要らない`
|
|
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 } # 捕捉したスカラーは引数であって、
|
|
@@ -80,9 +80,9 @@ out = CArray.double(2)
|
|
|
80
80
|
`ヒント: 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
|
`ヒント: 分岐は式`
|
|
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
|
`ヒント: どのセルを読むかをセルが言わねばならない`
|
|
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
|
`ヒント: 範囲はどこから始められるか`
|
|
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
|
`ヒント: このループが今書いたものを読む`
|
|
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
|
`ヒント: 引数二つ、extent 二つ`
|
|
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
|
`ヒント: セルの中のループ`
|
|
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
|
`ヒント: 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
|
`ヒント: どちらも推測しない`
|
|
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
|
`ヒント: 見るのをやめる`
|
|
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
|
`ヒント: 繰り返される添字`
|
|
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
|
`ヒント: 行の添字も繰り返されるが、和にしてはいけない`
|
|
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
|
`ヒント: 添字も extent も端も出てこない`
|
|
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
|
`ヒント: 一セルについて尋ねる`
|
|
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
|
`ヒント: カーネルに列を渡す`
|
|
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
|
`ヒント: ループはどちら向きに走るべきか。そしてそれをどこで言うか`
|
|
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
|
`ヒント: 格子は整列している`
|
|
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
|
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",
|