carray-jit 0.1.6 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +91 -0
  3. data/README.md +3 -7
  4. data/carray-jit.gemspec +11 -3
  5. data/docs/01_GettingStarted.md +1 -1
  6. data/docs/02_KernelShapes.md +33 -17
  7. data/docs/03_SupportedFeatures.md +31 -30
  8. data/docs/04_Compiling.md +10 -7
  9. data/docs/05_DesignNotes.md +1 -3
  10. data/docs/06_Cheatsheet.md +31 -21
  11. data/docs/07_StepByStep.ja.md +45 -45
  12. data/docs/07_StepByStep.md +45 -45
  13. data/docs/08_LanguageReference.md +2 -1
  14. data/examples/applications/alarm.rb +2 -2
  15. data/examples/applications/cubic_spline.rb +20 -20
  16. data/examples/applications/dithering.rb +8 -8
  17. data/examples/applications/game_of_life.rb +1 -1
  18. data/examples/applications/group_stats.rb +4 -4
  19. data/examples/applications/heat_equation.rb +6 -6
  20. data/examples/applications/kepler.rb +10 -10
  21. data/examples/applications/lookup.rb +3 -3
  22. data/examples/applications/median_filter.rb +7 -7
  23. data/examples/applications/moving_average.rb +6 -6
  24. data/examples/applications/parcel_ascent.rb +12 -12
  25. data/examples/applications/partial_sums.rb +6 -6
  26. data/examples/applications/point_cloud.rb +4 -4
  27. data/examples/applications/point_in_polygon.rb +4 -4
  28. data/examples/applications/quicksort.rb +1 -1
  29. data/examples/applications/relaxation.rb +1 -1
  30. data/examples/applications/sensor_gaps.rb +3 -3
  31. data/examples/applications/sobel_edges.rb +6 -6
  32. data/examples/applications/van_der_pol.rb +3 -3
  33. data/examples/applications/wet_bulb.rb +5 -5
  34. data/examples/features/01_element_wise.rb +8 -8
  35. data/examples/features/02_stencil.rb +4 -4
  36. data/examples/features/03_recurrence.rb +1 -1
  37. data/examples/features/04_thomas.rb +8 -8
  38. data/examples/features/05_reduction.rb +8 -8
  39. data/examples/features/06_jit_contract.rb +10 -10
  40. data/examples/features/07_masks.rb +4 -4
  41. data/examples/features/08_views.rb +6 -6
  42. data/examples/features/09_inspecting.rb +2 -2
  43. data/examples/features/10_complex.rb +7 -7
  44. data/examples/features/11_c_functions.rb +17 -17
  45. data/examples/features/12_sweep.rb +20 -25
  46. data/examples/features/13_cscalar.rb +12 -12
  47. data/examples/features/14_stencil_window.rb +6 -6
  48. data/examples/features/15_loops.rb +6 -6
  49. data/examples/features/16_raising.rb +4 -4
  50. data/lib/carray/jit/analyzer.rb +48 -34
  51. data/lib/carray/jit/c_function.rb +213 -153
  52. data/lib/carray/jit/c_generator.rb +33 -12
  53. data/lib/carray/jit/call.rb +1 -1
  54. data/lib/carray/jit/compiler.rb +115 -47
  55. data/lib/carray/jit/errors.rb +22 -0
  56. data/lib/carray/jit/expression.rb +78 -32
  57. data/lib/carray/jit/kernel.rb +28 -53
  58. data/lib/carray/jit/native.rb +171 -0
  59. data/lib/carray/jit/node.rb +1 -1
  60. data/lib/carray/jit/sweep.rb +14 -64
  61. data/lib/carray/jit/type_assignment.rb +9 -4
  62. data/lib/carray/jit/vector_math.rb +111 -0
  63. data/lib/carray/jit/version.rb +1 -1
  64. data/lib/carray/jit.rb +257 -106
  65. metadata +148 -122
@@ -2,7 +2,9 @@
2
2
 
3
3
  Ten entry points: the nine `jit_` methods this gem puts on `CArray`, and
4
4
  `CArray.fuse`, which is CArray's own and gets the compiler from this gem being
5
- installed. Every example here runs as written.
5
+ installed. The examples are fragments: the arrays and numbers they name (`a`,
6
+ `b`, `out`, `rows`, ...) are defined around them, and `...` stands for code
7
+ left out.
6
8
 
7
9
  ## Which one
8
10
 
@@ -89,16 +91,19 @@ how a recurrence advances: `a, b = b, a + b`, `a[i], a[j] = a[j], a[i]`. One
89
91
  value per name, written out -- the readings that take a single value apart
90
92
  (`a, b = f(x)`, `a, *rest =`, `a, (b, c) =`) are each refused by name.
91
93
 
92
- `reassociate:` says whether a reduction's accumulator may be split into partial
93
- sums. Default is `CArray::JIT.reassociate` (`true`). Pass `false` for the
94
- serial order -- a compensated summation, or checking against the loop.
94
+ `fastmath:` says whether the kernel may differ in its last bits from the Ruby
95
+ loop. Default is `CArray::JIT.fastmath` (`true`). It allows two things: a
96
+ reduction's accumulator is split into partial sums, and a math function is
97
+ computed by its vector form where the C library has one (glibc on x86_64).
98
+ Pass `false` for the answer of the loop -- a compensated summation, or
99
+ checking against the loop.
95
100
 
96
101
  A `CArray` the block makes is a C array of the block's own -- in any block
97
102
  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.float` is float32 and
99
- `CArray.complex` is cmplx64. Up to 4 KiB it stands in the frame; past that,
103
+ unless it is `CArray.empty(:type, [n])`. Note `CArray.float32` is float32 and
104
+ `CArray.cmplx64` is cmplx64. Up to 4 KiB it stands in the frame; past that,
100
105
  or where the shape is written over an integer the block captured
101
- (`CArray.double(n)`), the kernel allocates it at its entry and frees it at
106
+ (`CArray.float64(n)`), the kernel allocates it at its entry and frees it at
102
107
  its exit -- and then every subscript is checked where the cell is reached,
103
108
  and a C function takes it only through a pointer with no length. Where the kernel carries masks
104
109
  each cell gets a mask byte beside it, so `w[k] = UNDEF` and `w[k] == UNDEF`
@@ -124,8 +129,8 @@ calls, not methods on the array -- `w.sum` is refused.
124
129
  | `sort(w)` | a **statement** | ascending, in place, NaN last. A network up to 16 cells, an insertion sort above |
125
130
 
126
131
  ```ruby
127
- median = CArray.jit_for(rows) { |i|
128
- w = CArray.double(9)
132
+ CArray.jit_for(rows) { |i| # the median of each row
133
+ w = CArray.float64(9)
129
134
  (0...9).each { |k| w[k] = a[i, k] }
130
135
  sort(w)
131
136
  out[i] = w[4]
@@ -135,8 +140,8 @@ median = CArray.jit_for(rows) { |i|
135
140
  ## Filling from the indices
136
141
 
137
142
  ```ruby
138
- CArray.double(3, 4).jit_init { |i, j| i * 10.0 + j }
139
- grid = CArray.double(n)
143
+ CArray.float64(3, 4).jit_init { |i, j| i * 10.0 + j }
144
+ grid = CArray.float64(n)
140
145
  grid.jit_init { |i| Math.sin(i * step) } # fills in place
141
146
  ```
142
147
 
@@ -149,8 +154,8 @@ no index is `jit_each`'s work, and is refused here saying so.
149
154
  ## A scalar from outside
150
155
 
151
156
  ```ruby
152
- gain = CScalar.double() { 2.0 }
153
- total = CScalar.int()
157
+ gain = CScalar.float64() { 2.0 }
158
+ total = CScalar.int32()
154
159
 
155
160
  CArray.jit_each { out = signal * gain } # read at every cell
156
161
  CArray.jit_for(a.size) { |i| total[] += a[i] } # and written like any cell
@@ -164,8 +169,8 @@ axis to walk; written to, a bare name is a local of the block's own, so an
164
169
  accumulator is `s[] = s[] + ...` and not `s = s + ...`.
165
170
 
166
171
  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.int()` is that one
168
- cell zero-filled, as `CArray.int(1)` is; write `CScalar.int() { 0 }` where the
172
+ and what is left is what the same Ruby loop leaves. `CScalar.int32()` is that one
173
+ cell zero-filled, as `CArray.int32(1)` is; write `CScalar.int32() { 0 }` where the
169
174
  starting value is part of what the code says.
170
175
 
171
176
  See [CScalar](02_KernelShapes.md#a-cscalar-is-a-value-with-a-home) for how the
@@ -196,6 +201,10 @@ is what the cell gets.
196
201
 
197
202
  `type:` and `into:` together are refused.
198
203
 
204
+ An array narrower than the window along some axis is computed whole under
205
+ `:zero`, `:clamp` and `:wrap`, and refused under `:mask` and `:skip`, where no
206
+ cell would be computed.
207
+
199
208
  ## Contraction over a repeated index
200
209
 
201
210
  ```ruby
@@ -215,8 +224,8 @@ Naming one axis names them all: the argument list is the result's axes and
215
224
  their order, so a parameter left at a single position is refused there as it is
216
225
  under the convention. No extent is given: each index's extent comes from the
217
226
  axes it addresses. The sum is split into partial sums, as `jit_for`'s reduction
218
- is -- `CArray::JIT.reassociate = false` is the serial order, and a contraction
219
- has no per-call licence of its own.
227
+ is -- `fastmath: false` on the call, or `CArray::JIT.fastmath = false`, is
228
+ the serial order.
220
229
 
221
230
  Assigning into an array of yours says where to put it and in what order its
222
231
  axes lie. It does **not** decide what is summed -- so `total[i] = a[i,k]` is
@@ -240,8 +249,8 @@ borrowed with `jit_extern`, which the generated file declares and calls by
240
249
  name, are the two things such a body may reach outside its parameters.
241
250
 
242
251
  ```ruby
243
- values = CArray.double(8) { |i| i }
244
- out = CArray.double(8)
252
+ values = CArray.float64(8) { |i| i }
253
+ out = CArray.float64(8)
245
254
  n = values.elements
246
255
 
247
256
  CArray.jit_call("void (*)(double *out, const double *values, size_t n)") {
@@ -367,8 +376,9 @@ that was not asked.
367
376
  ## Knobs
368
377
 
369
378
  ```ruby
370
- CArray::JIT.reassociate #=> true, for jit_for reductions and every contraction
371
- CArray::JIT.reassociate = false # serial accumulation everywhere
379
+ CArray::JIT.fastmath #=> true: split reductions, vector math functions
380
+ CArray::JIT.fastmath = false # the answer of the Ruby loop, in every kernel
381
+ # whose call does not pass `fastmath:`
372
382
 
373
383
  CArray::JIT.cache_directory #=> ~/.cache/carray-jit/<version>
374
384
  CArray::JIT.cache_root = "tmp/jit" # this application keeps its own
@@ -23,10 +23,10 @@ require "carray/jit"
23
23
  `ヒント: 隣に手を伸ばさない仕事のためのメソッド`
24
24
 
25
25
  ```ruby
26
- a = CA_DOUBLE([1, 2, 3])
27
- b = CA_DOUBLE([2, 3, 4])
28
- c = CA_DOUBLE([3, 4, 5])
29
- out = CArray.double(3)
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.double(2, 3).seq!(1.0)
57
- row = CA_DOUBLE([10, 20, 30]).reshape(1, 3) # 3 ではなく 1x3。でないと ndim mismatch
58
- out = CArray.double(2, 3)
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 = CA_DOUBLE([1, 2])
70
- out = CArray.double(2)
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 = CA_DOUBLE([1, 2])
84
- gain = CScalar.double() { 2.5 }
85
- out = CArray.double(2)
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 = CA_DOUBLE([-2, 0, 1.5, 3])
97
- out = CArray.double(4)
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.double(5).seq!(1.0)
109
- out = CArray.double(5)
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 = CA_DOUBLE([1, 3, 6, 10])
121
- out = CArray.double(4)
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 = CA_DOUBLE([4, 8, 8, 8])
135
- out = CArray.double(4)
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.double(2, 3).seq!(1.0)
148
- out = CArray.double(3, 2)
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 = CA_DOUBLE([[1, 3, 2], [9, 4, 5]])
160
- out = CArray.double(2)
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 = CA_DOUBLE([3, 5])
200
- y = CA_DOUBLE([4, 12])
201
- out = CArray.double(2)
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 = CA_DOUBLE([1, 2, 3])
213
- out = CArray.double(3)
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 = CA_DOUBLE([[1, 2, 9, 3], [5, 1, 1, 8]])
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.double(2, 3).seq!(1.0)
272
- b = CArray.double(3, 2).seq!(1.0)
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 = CA_DOUBLE([[1, 2, 3], [4, 5, 6]])
284
- y = CA_DOUBLE([[1, 1, 1], [2, 2, 2]])
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 = CA_DOUBLE([1, 2, 3, 4])
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 = CA_DOUBLE([1, 2, 3])
309
+ a = CA_FLOAT64([1, 2, 3])
310
310
  a[1] = UNDEF
311
- out = CArray.double(3)
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.double(3, 3)
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 = CA_DOUBLE([1, 2, 3, 4, 5])
366
- out = CArray.double(5)
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.double(5)
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 = CA_DOUBLE([0, 1, 4, 9, 16])
427
- query = CA_DOUBLE([0.5, 5.0, 9.0, 12.0])
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.double(2)
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.double(100_000)
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
@@ -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 = CA_DOUBLE([1, 2, 3])
27
- b = CA_DOUBLE([2, 3, 4])
28
- c = CA_DOUBLE([3, 4, 5])
29
- out = CArray.double(3)
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.double(2, 3).seq!(1.0)
57
- row = CA_DOUBLE([10, 20, 30]).reshape(1, 3) # 1x3, not 3: else `ndim mismatch`
58
- out = CArray.double(2, 3)
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 = CA_DOUBLE([1, 2])
70
- out = CArray.double(2)
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 = CA_DOUBLE([1, 2])
84
- gain = CScalar.double() { 2.5 }
85
- out = CArray.double(2)
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 = CA_DOUBLE([-2, 0, 1.5, 3])
97
- out = CArray.double(4)
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.double(5).seq!(1.0)
109
- out = CArray.double(5)
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 = CA_DOUBLE([1, 3, 6, 10])
121
- out = CArray.double(4)
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 = CA_DOUBLE([4, 8, 8, 8])
135
- out = CArray.double(4)
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.double(2, 3).seq!(1.0)
148
- out = CArray.double(3, 2)
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 = CA_DOUBLE([[1, 3, 2], [9, 4, 5]])
160
- out = CArray.double(2)
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 = CA_DOUBLE([3, 5])
200
- y = CA_DOUBLE([4, 12])
201
- out = CArray.double(2)
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 = CA_DOUBLE([1, 2, 3])
213
- out = CArray.double(3)
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 = CA_DOUBLE([[1, 2, 9, 3], [5, 1, 1, 8]])
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.double(2, 3).seq!(1.0)
272
- b = CArray.double(3, 2).seq!(1.0)
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 = CA_DOUBLE([[1, 2, 3], [4, 5, 6]])
284
- y = CA_DOUBLE([[1, 1, 1], [2, 2, 2]])
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 = CA_DOUBLE([1, 2, 3, 4])
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 = CA_DOUBLE([1, 2, 3])
309
+ a = CA_FLOAT64([1, 2, 3])
310
310
  a[1] = UNDEF
311
- out = CArray.double(3)
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.double(3, 3)
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 = CA_DOUBLE([1, 2, 3, 4, 5])
366
- out = CArray.double(5)
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.double(5)
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 = CA_DOUBLE([0, 1, 4, 9, 16])
427
- query = CA_DOUBLE([0.5, 5.0, 9.0, 12.0])
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.double(2)
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.double(100_000)
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
@@ -394,7 +394,8 @@ These are deliberate. Everything not listed here gives Ruby's answer, to the las
394
394
  - **A negative real raised to a fractional power** is NaN where the result is read as a real. Stored into a complex cell, or joined with a complex value, it is Ruby's Complex.
395
395
  - **A complex raised to a power** uses C's `cpow`, which can differ from Ruby in the last bit.
396
396
  - **A sine beside a cosine of the same argument** may be computed together by the C compiler, which can change the last bit.
397
- - **A reduction is reassociated** into partial sums unless `reassociate: false` is given, so its last bits can differ from adding in order.
397
+ - **A reduction is split** into partial sums unless `fastmath: false` is given, so its last bits can differ from adding in order.
398
+ - **A math function is computed by its vector form** on x86_64 Linux with glibc unless `fastmath` is false, so its last few bits can differ from `Math`'s.
398
399
  - **A real stored into an integer array** keeps its integer part, as CArray's store does, and one past the type's range raises `RangeError` where Ruby's `to_i` would grow. A NaN or an infinity raises `FloatDomainError`, as `to_i` does.
399
400
 
400
401
  **The language**
@@ -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.double(SAMPLES)
30
+ noise = CArray.float64(SAMPLES)
31
31
  noise.random!(rng: random)
32
- reading = CArray.double(SAMPLES)
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)