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
@@ -107,7 +107,7 @@ end
107
107
  # query point has to find its interval; `passes` bounds the bisection, and a
108
108
  # kernel that cannot fail to stop is the better kind.
109
109
  def evaluate (x, y, moment, n, query, value, derivative)
110
- points = query.dim[0]
110
+ points = query.shape[0]
111
111
  passes = Math.log2(n).ceil + 1
112
112
 
113
113
  CArray.jit_for(points) { |k|
@@ -146,7 +146,7 @@ end
146
146
  # and the second kernel does every point the same way. Fusing the two into a
147
147
  # single kernel is worth about 30% more, at that price.
148
148
  def resample (x, y, moment, n, query, value, derivative, interval)
149
- points = query.dim[0]
149
+ points = query.shape[0]
150
150
 
151
151
  seed = 0
152
152
  seed += 1 while seed < n - 2 && x[seed+1] <= query[0]
@@ -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.double(n) { |i| 6.0 * i / (n - 1) }
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.double(n) { |i| curve(x[i]) }
182
+ y = CArray.float64(n) { |i| curve(x[i]) }
183
183
 
184
- work = Array.new(7) { CArray.double(n) }
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.double(points) { |k| x[0] + (x[n-1] - x[0]) * k / (points - 1) }
191
- truth = CArray.double(points) { |k| curve(query[k]) }
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.double(points)
194
- derivative = CArray.double(points)
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.double(n) { |i| x[i] }
226
- at_knots = CArray.double(n)
227
- at_knots_slope = CArray.double(n)
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.double(points)
237
- swept_slope = CArray.double(points)
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
 
@@ -293,7 +293,7 @@ def ruby_moments (x, y, n, work)
293
293
  end
294
294
 
295
295
  def ruby_evaluate (x, y, moment, n, query, value, derivative)
296
- query.dim[0].times do |k|
296
+ query.shape[0].times do |k|
297
297
  lo, hi = 0, n - 2
298
298
  while lo < hi
299
299
  middle = (lo + hi + 1) / 2
@@ -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.double(size) { |i| 6.0 * i / (size - 1) }
314
- values = CArray.double(size) { |i| curve(knots[i]) }
315
- scratch = Array.new(7) { CArray.double(size) }
316
- at = CArray.double(sampled) { |k| 6.0 * k / (sampled - 1) }
317
- out, slopes = CArray.double(sampled), CArray.double(sampled)
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.double(rows, columns)
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
@@ -36,9 +36,9 @@ end
36
36
  # against the branches that guard them, so the room is made in the array, not
37
37
  # in an `if`.
38
38
  def dither (image)
39
- rows, columns = image.dim
39
+ rows, columns = image.shape
40
40
  out = CArray.int8(rows, columns)
41
- work = CArray.double(rows + 1, columns + 2)
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|
@@ -61,8 +61,8 @@ ROWS, COLUMNS = 22, 72
61
61
 
62
62
  def show (bits, title)
63
63
  puts title
64
- bits.dim[0].times do |y|
65
- puts " " + (0...bits.dim[1]).map { |x| bits[y, x] == 1 ? "@" : " " }.join
64
+ bits.shape[0].times do |y|
65
+ puts " " + (0...bits.shape[1]).map { |x| bits[y, x] == 1 ? "@" : " " }.join
66
66
  end
67
67
  puts
68
68
  end
@@ -77,7 +77,7 @@ show(dither(image), "dithered, the error passed on")
77
77
 
78
78
  # The same walk in Ruby, at a size worth timing.
79
79
  def dither_in_ruby (image)
80
- rows, columns = image.dim
80
+ rows, columns = image.shape
81
81
  out = Array.new(rows) { Array.new(columns, 0) }
82
82
  work = Array.new(rows + 1) { Array.new(columns + 2, 0.0) }
83
83
  rows.times { |y| columns.times { |x| work[y][x + 1] = image[y, x] } }
@@ -117,9 +117,9 @@ puts format(" %.1f ms compiled, %.0f ms in Ruby (%.0fx)",
117
117
  # either -- the walk is part of what the algorithm says, which is why this is
118
118
  # not a pass an expression over arrays could have been rearranged into.
119
119
  def dither_backwards (image)
120
- rows, columns = image.dim
120
+ rows, columns = image.shape
121
121
  out = CArray.int8(rows, columns)
122
- work = CArray.double(rows + 1, columns + 2)
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|
@@ -20,7 +20,7 @@ require "carray/jit"
20
20
  ROWS, COLUMNS = 24, 48
21
21
 
22
22
  def step (current, following)
23
- rows, columns = current.dim
23
+ rows, columns = current.shape
24
24
  CArray.jit_for(1...(rows-1), 1...(columns-1)) { |i, j|
25
25
  neighbours = current[i-1, j-1] + current[i-1, j] + current[i-1, j+1] +
26
26
  current[i, j-1] + current[i, j+1] +
@@ -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.double(READINGS)
31
+ station = CArray.float64(READINGS)
32
32
  station.random!(rng: random)
33
33
  station = ((station ** 2) * STATIONS).int32
34
34
 
35
- value = CArray.double(READINGS)
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.double(STATIONS)
43
- peak = CArray.double(STATIONS).fill(-Float::INFINITY)
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.double(n)
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.double(n)
36
- dd = CArray.double(n)
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
@@ -55,7 +55,7 @@ def solve (temperature, cc, dd, lower, diagonal, upper, n)
55
55
  end
56
56
 
57
57
  def draw (temperature, label)
58
- n = temperature.dim[0]
58
+ n = temperature.shape[0]
59
59
  row = (0...72).map { |k|
60
60
  value = temperature[k * (n - 1) / 71]
61
61
  " .:-=+*#@"[[(value * 8).round, 8].min] || " "
@@ -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.double(size).seq! { |i| i.to_f / size }
107
- work = [CArray.double(size), CArray.double(size)]
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.double(n) { |i| 2.0 * Math::PI * i / n - Math::PI }
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.double(mean_anomaly.elements)
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.double(n)
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.double(n)
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.double(n) # one Newton step, in Ruby
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.double(n)
108
+ alone = CArray.float64(n)
109
109
  CArray.jit_for(n) { |i| alone[i] = Math.sin(iterate[i]) }
110
- together = CArray.double(n)
111
- unused = CArray.double(n)
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.double(n)
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.double(n)
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.double(N).seq!(0, 1.0 / (N - 1)) ** 2) * 100
26
+ knots = (CArray.float64(N).seq!(0, 1.0 / (N - 1)) ** 2) * 100
27
27
  table = knots.sin
28
- query = CArray.double(Q).seq!(0, 99.9 / Q)
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.double(query.elements)
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.double(SAMPLES)
28
+ noise = CArray.float64(SAMPLES)
29
29
  noise.random!(rng: random)
30
- chance = CArray.double(SAMPLES)
30
+ chance = CArray.float64(SAMPLES)
31
31
  chance.random!(rng: random)
32
- signal = CArray.double(SAMPLES)
33
- clean = CArray.double(SAMPLES)
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.double(SAMPLES)
41
- network_out = CArray.double(SAMPLES)
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.double(SAMPLES, WINDOW)
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.double(n)
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.double(n)
27
- peak = CArray.double(n)
28
- drawdown = CArray.double(n)
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.double(n)
62
- rolling = CArray.double(n)
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.double(LEVELS).seq!(1000.0, -10.0)
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.double(COLUMNS)
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.double(COLUMNS)
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.double(COLUMNS, LEVELS)
52
- height = CArray.double(LEVELS)
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,14 +57,14 @@ CArray.jit_for(COLUMNS, LEVELS) { |c, k|
57
57
  }
58
58
  dewpoint = environment[nil, 0] - spread
59
59
 
60
- cape = CArray.double(COLUMNS)
61
- cin = CArray.double(COLUMNS)
62
- lcl = CArray.double(COLUMNS)
63
- lfc = CArray.double(COLUMNS)
64
- el = CArray.double(COLUMNS)
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
- columns, levels = environment.dim
67
+ columns, levels = environment.shape
68
68
  CArray.jit_for(columns) { |c|
69
69
  base = pressure[0]
70
70
  start = environment[c, 0]
@@ -156,7 +156,7 @@ puts format(" strongest column: CAPE %.0f J/kg with CIN %.0f J/kg",
156
156
 
157
157
  # The same climb in Ruby.
158
158
  def climb_in_ruby (pressure, environment, dewpoint)
159
- columns, levels = environment.dim
159
+ columns, levels = environment.shape
160
160
  levels_a = pressure.to_a
161
161
  cape = Array.new(columns, 0.0)
162
162
  columns.times do |c|
@@ -21,7 +21,7 @@ require "carray/jit"
21
21
  N = 500_000
22
22
 
23
23
  def accumulators
24
- Array.new(9) { CScalar.double() { 0.0 } }
24
+ Array.new(9) { CScalar.float64() { 0.0 } }
25
25
  end
26
26
 
27
27
  def sum_series (n)
@@ -99,12 +99,12 @@ puts format(" every one of the nine matches the Ruby loop bit for bit: %s",
99
99
 
100
100
  # The harmonic sum again, with the loop written inside the cell instead of
101
101
  # being the kernel's own. That is a reduction, and a reduction's accumulator
102
- # may be split into partial sums: `reassociate: false` asks for the serial
102
+ # may be split into partial sums: `fastmath: false` asks for the serial
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
- def harmonic (n, reassociate)
106
- out = CScalar.double() { 0.0 }
107
- CArray.jit_for(1, reassociate: reassociate) { |c|
105
+ def harmonic (n, fastmath)
106
+ out = CScalar.float64() { 0.0 }
107
+ CArray.jit_for(1, fastmath: fastmath) { |c|
108
108
  total = 0.0
109
109
  (1..n).each { |k| total = total + 1.0 / (k * 1.0) }
110
110
  out[] = total
@@ -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.double(N).seq!(1.0)).sum
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.double(count, 3) { |p, k| random.rand(-1.0..1.0) }
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.double(3, 3)
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.double(count, 3)
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.double(4, 4).seq!(1.0)
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}"
@@ -20,8 +20,8 @@ EDGES = 200
20
20
  POINTS = 1_000_000
21
21
 
22
22
  # A lobed blob, closed, going round once.
23
- corner_x = CArray.double(EDGES)
24
- corner_y = CArray.double(EDGES)
23
+ corner_x = CArray.float64(EDGES)
24
+ corner_y = CArray.float64(EDGES)
25
25
  CArray.jit_for(EDGES) { |k|
26
26
  angle = 2.0 * Math::PI * k / EDGES
27
27
  radius = 1.0 + 0.35 * Math.sin(7.0 * angle) + 0.15 * Math.cos(3.0 * angle)
@@ -30,10 +30,10 @@ CArray.jit_for(EDGES) { |k|
30
30
  }
31
31
 
32
32
  random = CArray::Rng.new(seed: 20260913)
33
- point_x = CArray.double(POINTS)
33
+ point_x = CArray.float64(POINTS)
34
34
  point_x.random!(rng: random)
35
35
  point_x = point_x * 3.2 - 1.6
36
- point_y = CArray.double(POINTS)
36
+ point_y = CArray.float64(POINTS)
37
37
  point_y.random!(rng: random)
38
38
  point_y = point_y * 3.2 - 1.6
39
39
 
@@ -48,7 +48,7 @@ quicksort = CArray.jit_function(
48
48
 
49
49
  n = 200_000
50
50
  random = Random.new(20260905)
51
- values = CArray.double(n) { |i| random.rand }
51
+ values = CArray.float64(n) { |i| random.rand }
52
52
 
53
53
  sorted = values.copy # `to_ca` would answer this array
54
54
  quicksort.call(sorted, 0, n - 1)
@@ -19,7 +19,7 @@ ROWS, COLUMNS = 120, 160
19
19
  SWEEPS = 1200
20
20
 
21
21
  def fresh_plate
22
- plate = CArray.double(ROWS, COLUMNS)
22
+ plate = CArray.float64(ROWS, COLUMNS)
23
23
  plate[nil, 0] = 25.0 # the sides, somewhere in between
24
24
  plate[nil, -1] = 25.0
25
25
  plate[0, nil] = 100.0 # the top edge is hot, corners included
@@ -18,7 +18,7 @@ days, hours = 30, 24
18
18
  random = Random.new(20260902)
19
19
 
20
20
  # Hourly temperature: a daily cycle, a seasonal drift, and noise.
21
- reading = CArray.double(days, hours) { |d, h|
21
+ reading = CArray.float64(days, hours) { |d, h|
22
22
  15.0 + 8.0 * Math.sin((h - 9) * Math::PI / 12) + d * 0.1 +
23
23
  random.rand(-1.0..1.0)
24
24
  }
@@ -57,7 +57,7 @@ puts " #{reading.count_masked - dropped} readings rejected as out of range"
57
57
  # 2. Fill a hole from its neighbours, but only where both neighbours are there.
58
58
  # Where they are not, the average reads a masked cell and the result comes
59
59
  # out masked by itself -- the rule does not have to be written twice.
60
- filled = CArray.double(days, hours)
60
+ filled = CArray.float64(days, hours)
61
61
  CArray.jit_for(days, 1...(hours-1)) { |d, h|
62
62
  if reading[d, h] == UNDEF
63
63
  filled[d, h] = 0.5 * (reading[d, h-1] + reading[d, h+1])
@@ -73,7 +73,7 @@ puts " #{recovered} holes interpolated, #{filled.count_masked} still missing"
73
73
 
74
74
  # 3. The daily mean, from the readings that survived -- and a day with fewer
75
75
  # than twenty valid hours does not get a mean at all.
76
- mean = CArray.double(days)
76
+ mean = CArray.float64(days)
77
77
  valid = CArray.int32(days)
78
78
  CArray.jit_for(days) { |d|
79
79
  total = 0.0
@@ -11,7 +11,7 @@ $LOAD_PATH.unshift(File.expand_path("../../lib", __dir__))
11
11
  require "carray/jit"
12
12
 
13
13
  def sobel (image, edges)
14
- rows, columns = image.dim
14
+ rows, columns = image.shape
15
15
  CArray.jit_for(1...(rows-1), 1...(columns-1)) { |i, j|
16
16
  gx = (image[i-1, j+1] + 2.0 * image[i, j+1] + image[i+1, j+1]) -
17
17
  (image[i-1, j-1] + 2.0 * image[i, j-1] + image[i+1, j-1])
@@ -23,7 +23,7 @@ end
23
23
 
24
24
  # A synthetic picture: a disc, a bar, and a soft background gradient.
25
25
  rows, columns = 40, 96
26
- image = CArray.double(rows, columns)
26
+ image = CArray.float64(rows, columns)
27
27
  rows.times do |i|
28
28
  columns.times do |j|
29
29
  y = (i - 20.0) / 16.0
@@ -35,7 +35,7 @@ rows.times do |i|
35
35
  end
36
36
  end
37
37
 
38
- edges = CArray.double(rows, columns)
38
+ edges = CArray.float64(rows, columns)
39
39
  sobel(image, edges)
40
40
 
41
41
  def draw (field, title)
@@ -54,15 +54,15 @@ draw(edges, "edges")
54
54
  # On an image of a size you would actually filter -- the same filter, and the
55
55
  # same loop written in Ruby, both over the whole thing.
56
56
  size = 512
57
- large = CArray.double(size, size) { |i, j| Math.sin(i * 0.03) * Math.cos(j * 0.02) }
58
- result = CArray.double(size, size)
57
+ large = CArray.float64(size, size) { |i, j| Math.sin(i * 0.03) * Math.cos(j * 0.02) }
58
+ result = CArray.float64(size, size)
59
59
 
60
60
  sobel(large, result) # compile it before timing it
61
61
  started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
62
62
  5.times { sobel(large, result) }
63
63
  compiled = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - started) / 5
64
64
 
65
- reference = CArray.double(size, size)
65
+ reference = CArray.float64(size, size)
66
66
  started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
67
67
  (1...(size-1)).each do |i|
68
68
  (1...(size-1)).each do |j|
@@ -26,9 +26,9 @@ FINISH = 20.0
26
26
  TOLERANCE = 1.0e-8
27
27
  CAP = 200_000 # a `while` in a kernel carries a bound
28
28
 
29
- stiffness = CArray.double(TRAJECTORIES).seq!(0.1, 40.0 / TRAJECTORIES)
30
- position = CArray.double(TRAJECTORIES)
31
- velocity = CArray.double(TRAJECTORIES)
29
+ stiffness = CArray.float64(TRAJECTORIES).seq!(0.1, 40.0 / TRAJECTORIES)
30
+ position = CArray.float64(TRAJECTORIES)
31
+ velocity = CArray.float64(TRAJECTORIES)
32
32
  accepted = CArray.int32(TRAJECTORIES)
33
33
  rejected = CArray.int32(TRAJECTORIES)
34
34
 
@@ -29,8 +29,8 @@ CAP = 50 # a `while` in a kernel takes a cap
29
29
 
30
30
  # Stand-ins for the fields that would have been read: a pole-to-pole
31
31
  # temperature gradient with a wave in it, and humidity that varies across it.
32
- temperature = CArray.double(ROWS, COLUMNS)
33
- humidity = CArray.double(ROWS, COLUMNS)
32
+ temperature = CArray.float64(ROWS, COLUMNS)
33
+ humidity = CArray.float64(ROWS, COLUMNS)
34
34
  CArray.jit_for(ROWS, COLUMNS) { |j, i|
35
35
  latitude = 90.0 - 180.0 * j / (ROWS - 1)
36
36
  temperature[j, i] = 30.0 - 55.0 * (latitude / 90.0).abs +
@@ -38,15 +38,15 @@ CArray.jit_for(ROWS, COLUMNS) { |j, i|
38
38
  humidity[j, i] = 35.0 + 60.0 * (0.5 + 0.5 * Math.cos(i / 30.0 + j / 60.0))
39
39
  }
40
40
 
41
- dew_point = CArray.double(ROWS, COLUMNS)
42
- wet_bulb = CArray.double(ROWS, COLUMNS)
41
+ dew_point = CArray.float64(ROWS, COLUMNS)
42
+ wet_bulb = CArray.float64(ROWS, COLUMNS)
43
43
  rounds_taken = CArray.int8(ROWS, COLUMNS)
44
44
 
45
45
  # Everything in one walk. `vapour` and `gamma` are the cell's own values and
46
46
  # never become arrays; `low` and `high` are the cell's bracket, which is why
47
47
  # the loop can end when this cell is done rather than when the last one is.
48
48
  def derive (temperature, humidity, dew_point, wet_bulb, rounds_taken)
49
- rows, columns = temperature.dim
49
+ rows, columns = temperature.shape
50
50
  CArray.jit_for(rows, columns) { |j, i|
51
51
  air = temperature[j, i]
52
52
  saturation = 6.112 * Math.exp(17.67 * air / (air + 243.5))