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.

Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +40 -0
  3. data/README.md +2 -2
  4. data/carray-jit.gemspec +7 -3
  5. data/docs/02_KernelShapes.md +7 -7
  6. data/docs/03_SupportedFeatures.md +28 -28
  7. data/docs/04_Compiling.md +5 -5
  8. data/docs/05_DesignNotes.md +1 -3
  9. data/docs/06_Cheatsheet.md +12 -12
  10. data/docs/07_StepByStep.ja.md +45 -45
  11. data/docs/07_StepByStep.md +45 -45
  12. data/examples/applications/alarm.rb +2 -2
  13. data/examples/applications/cubic_spline.rb +17 -17
  14. data/examples/applications/dithering.rb +3 -3
  15. data/examples/applications/group_stats.rb +4 -4
  16. data/examples/applications/heat_equation.rb +5 -5
  17. data/examples/applications/kepler.rb +10 -10
  18. data/examples/applications/lookup.rb +3 -3
  19. data/examples/applications/median_filter.rb +7 -7
  20. data/examples/applications/moving_average.rb +6 -6
  21. data/examples/applications/parcel_ascent.rb +10 -10
  22. data/examples/applications/partial_sums.rb +3 -3
  23. data/examples/applications/point_cloud.rb +4 -4
  24. data/examples/applications/point_in_polygon.rb +4 -4
  25. data/examples/applications/quicksort.rb +1 -1
  26. data/examples/applications/relaxation.rb +1 -1
  27. data/examples/applications/sensor_gaps.rb +3 -3
  28. data/examples/applications/sobel_edges.rb +5 -5
  29. data/examples/applications/van_der_pol.rb +3 -3
  30. data/examples/applications/wet_bulb.rb +4 -4
  31. data/examples/features/01_element_wise.rb +8 -8
  32. data/examples/features/02_stencil.rb +4 -4
  33. data/examples/features/03_recurrence.rb +1 -1
  34. data/examples/features/04_thomas.rb +8 -8
  35. data/examples/features/05_reduction.rb +7 -7
  36. data/examples/features/06_jit_contract.rb +9 -9
  37. data/examples/features/07_masks.rb +4 -4
  38. data/examples/features/08_views.rb +5 -5
  39. data/examples/features/09_inspecting.rb +1 -1
  40. data/examples/features/10_complex.rb +4 -4
  41. data/examples/features/11_c_functions.rb +17 -17
  42. data/examples/features/12_sweep.rb +19 -24
  43. data/examples/features/13_cscalar.rb +12 -12
  44. data/examples/features/14_stencil_window.rb +6 -6
  45. data/examples/features/15_loops.rb +6 -6
  46. data/examples/features/16_raising.rb +4 -4
  47. data/lib/carray/jit/analyzer.rb +39 -34
  48. data/lib/carray/jit/c_function.rb +133 -139
  49. data/lib/carray/jit/c_generator.rb +20 -11
  50. data/lib/carray/jit/call.rb +1 -1
  51. data/lib/carray/jit/compiler.rb +78 -43
  52. data/lib/carray/jit/expression.rb +68 -28
  53. data/lib/carray/jit/kernel.rb +23 -47
  54. data/lib/carray/jit/native.rb +149 -0
  55. data/lib/carray/jit/node.rb +1 -1
  56. data/lib/carray/jit/sweep.rb +14 -64
  57. data/lib/carray/jit/type_assignment.rb +9 -4
  58. data/lib/carray/jit/version.rb +1 -1
  59. data/lib/carray/jit.rb +103 -28
  60. metadata +8 -7
@@ -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,11 +57,11 @@ 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
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.double() { 0.0 } }
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.double() { 0.0 }
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.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
@@ -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,8 +38,8 @@ 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
@@ -14,10 +14,10 @@ $LOAD_PATH.unshift(File.expand_path("../../lib", __dir__))
14
14
  require "carray/jit"
15
15
 
16
16
  n = 100_000
17
- a = CArray.double(n).seq!(1.0)
18
- b = CArray.double(n).seq!(0.5, 0.5)
19
- c = CArray.double(n).seq!(2.0, -0.25)
20
- out = CArray.double(n)
17
+ a = CArray.float64(n).seq!(1.0)
18
+ b = CArray.float64(n).seq!(0.5, 0.5)
19
+ c = CArray.float64(n).seq!(2.0, -0.25)
20
+ out = CArray.float64(n)
21
21
 
22
22
  CArray.jit_each { out = (a + b) * (c - a) + b * c - a }
23
23
 
@@ -32,9 +32,9 @@ puts " matches CArray #{out.to_a == expected.to_a}"
32
32
  # The rank comes from the arrays rather than from the block, so the same
33
33
  # expression serves whatever shape it is given, and broadcasting costs nothing
34
34
  # -- a stretched axis arrives as a stride of zero.
35
- matrix = CArray.double(3, 4).seq!(1.0)
36
- row = CArray.double(1, 4).seq!(10.0, 10.0)
37
- scaled = CArray.double(3, 4)
35
+ matrix = CArray.float64(3, 4).seq!(1.0)
36
+ row = CArray.float64(1, 4).seq!(10.0, 10.0)
37
+ scaled = CArray.float64(3, 4)
38
38
 
39
39
  CArray.jit_each { scaled = matrix * row }
40
40
 
@@ -64,6 +64,6 @@ puts " closing over a value #{blended[0..2].to_a.inspect}"
64
64
  # An assignment is a statement with a value -- Ruby's rule, not a special
65
65
  # case here -- so a block may write an array of yours and hand the same value
66
66
  # back. Which is jit_each's block, asked a different question.
67
- kept = CArray.double(n)
67
+ kept = CArray.float64(n)
68
68
  also = CArray.jit_map { kept = a - b }
69
69
  puts " written and returned #{kept[0..2].to_a == also[0..2].to_a}"
@@ -9,14 +9,14 @@ $LOAD_PATH.unshift(File.expand_path("../../lib", __dir__))
9
9
  require "carray/jit"
10
10
 
11
11
  rows, columns = 6, 8
12
- src = CArray.double(rows, columns).seq!(1.0)
13
- out = CArray.double(rows, columns)
12
+ src = CArray.float64(rows, columns).seq!(1.0)
13
+ out = CArray.float64(rows, columns)
14
14
 
15
15
  CArray.jit_for(1...(rows-1), 1...(columns-1)) { |i, j|
16
16
  out[i, j] = 0.25 * (src[i-1, j] + src[i+1, j] + src[i, j-1] + src[i, j+1])
17
17
  }
18
18
 
19
- reference = CArray.double(rows, columns)
19
+ reference = CArray.float64(rows, columns)
20
20
  (1...(rows-1)).each do |i|
21
21
  (1...(columns-1)).each do |j|
22
22
  reference[i, j] = 0.25 * (src[i-1, j] + src[i+1, j] + src[i, j-1] + src[i, j+1])
@@ -30,7 +30,7 @@ puts " border untouched #{out[0, nil].to_a.all?(0.0)}"
30
30
  # An extent may step by more than one, which changes which cells are written
31
31
  # and therefore which offsets are dependencies: with a step of two, a[i-1] is
32
32
  # a cell this loop never writes.
33
- values = CArray.double(10).seq!(1.0)
33
+ values = CArray.float64(10).seq!(1.0)
34
34
  CArray.jit_for((2...10).step(2)) { |i| values[i] = values[i-1] * 100.0 }
35
35
  puts " every other cell #{values.to_a.inspect}"
36
36
 
@@ -11,7 +11,7 @@ require "carray/jit"
11
11
  # Legendre polynomials at x, by their three-term recurrence.
12
12
  n = 24
13
13
  x = 0.5
14
- legendre = CArray.double(n)
14
+ legendre = CArray.float64(n)
15
15
  legendre[0] = 1.0
16
16
  legendre[1] = x
17
17
 
@@ -18,16 +18,16 @@ random = Random.new(20260902)
18
18
 
19
19
  # A diagonally dominant tridiagonal system: a is the subdiagonal, b the
20
20
  # diagonal, c the superdiagonal, d the right-hand side.
21
- a = CArray.double(n) { random.rand(-1.0..1.0) }
22
- c = CArray.double(n) { random.rand(-1.0..1.0) }
23
- b = CArray.double(n) { |i| 4.0 + random.rand }
24
- d = CArray.double(n) { random.rand(-1.0..1.0) }
21
+ a = CArray.float64(n) { random.rand(-1.0..1.0) }
22
+ c = CArray.float64(n) { random.rand(-1.0..1.0) }
23
+ b = CArray.float64(n) { |i| 4.0 + random.rand }
24
+ d = CArray.float64(n) { random.rand(-1.0..1.0) }
25
25
  a[0] = 0.0
26
26
  c[n-1] = 0.0
27
27
 
28
- cc = CArray.double(n)
29
- dd = CArray.double(n)
30
- x = CArray.double(n)
28
+ cc = CArray.float64(n)
29
+ dd = CArray.float64(n)
30
+ x = CArray.float64(n)
31
31
 
32
32
  cc[0] = c[0] / b[0]
33
33
  dd[0] = d[0] / b[0]
@@ -62,7 +62,7 @@ puts " max |A x - d| #{format('%.3e', residual)}"
62
62
  # not back substitution either: each cell reads an x[i+1] the sweep has not
63
63
  # reached yet. It is the answer the same Ruby loop gives, which is the only
64
64
  # thing this promises.
65
- wrong_way = CArray.double(n)
65
+ wrong_way = CArray.float64(n)
66
66
  wrong_way[n-1] = dd[n-1]
67
67
  CArray.jit_for(0...(n-1)) { |i| wrong_way[i] = dd[i] - cc[i] * wrong_way[i+1] }
68
68
 
@@ -10,9 +10,9 @@ $LOAD_PATH.unshift(File.expand_path("../../lib", __dir__))
10
10
  require "carray/jit"
11
11
 
12
12
  rows, columns = 5, 7
13
- source = CArray.double(rows, columns).seq!(1.0)
14
- total = CArray.double(rows)
15
- largest = CArray.double(rows)
13
+ source = CArray.float64(rows, columns).seq!(1.0)
14
+ total = CArray.float64(rows)
15
+ largest = CArray.float64(rows)
16
16
  positives = CArray.int32(rows)
17
17
 
18
18
  CArray.jit_for(rows) { |i|
@@ -49,9 +49,9 @@ puts " count #{positives.to_a.inspect}"
49
49
 
50
50
  # A matrix multiply is the same shape of thing: two output indices and one
51
51
  # inner loop.
52
- left = CArray.double(3, 4).seq!(1.0)
53
- right = CArray.double(4, 2).seq!(0.5, 0.5)
54
- product = CArray.double(3, 2)
52
+ left = CArray.float64(3, 4).seq!(1.0)
53
+ right = CArray.float64(4, 2).seq!(0.5, 0.5)
54
+ product = CArray.float64(3, 2)
55
55
 
56
56
  CArray.jit_for(3, 2) { |i, j|
57
57
  accumulator = 0.0
@@ -79,7 +79,7 @@ puts
79
79
  # Counting from zero is what an inner loop mostly does, and `n.times` says it
80
80
  # without naming an end. It compiles to the loop the range spells out --
81
81
  # `(0...n).each` -- so the choice is the reader's, not the compiler's.
82
- counted = CArray.double(rows)
82
+ counted = CArray.float64(rows)
83
83
  CArray.jit_for(rows) { |i|
84
84
  accumulator = 0.0
85
85
  columns.times { |j| accumulator = accumulator + source[i, j] }
@@ -8,12 +8,12 @@
8
8
  $LOAD_PATH.unshift(File.expand_path("../../lib", __dir__))
9
9
  require "carray/jit"
10
10
 
11
- a = CArray.double(3, 4).seq!(1.0)
12
- b = CArray.double(4, 2).seq!(0.5, 0.5)
13
- v = CArray.double(4).seq!(1.0)
14
- p = CArray.double(3).seq!(1.0)
15
- r = CArray.double(2).seq!(10.0, 10.0)
16
- q = CArray.double(3, 3).seq!(1.0)
11
+ a = CArray.float64(3, 4).seq!(1.0)
12
+ b = CArray.float64(4, 2).seq!(0.5, 0.5)
13
+ v = CArray.float64(4).seq!(1.0)
14
+ p = CArray.float64(3).seq!(1.0)
15
+ r = CArray.float64(2).seq!(10.0, 10.0)
16
+ q = CArray.float64(3, 3).seq!(1.0)
17
17
 
18
18
  matmul = CArray.jit_contract { |i, j, k| a[i,k] * b[k,j] } # k is summed
19
19
  matvec = CArray.jit_contract { |i, k| a[i,k] * v[k] } # k is summed
@@ -36,14 +36,14 @@ puts " swapped #{transposed.dim.inspect} vs #{matmul.dim.inspect}"
36
36
 
37
37
  # Assigning into an array of your own says where to put it instead. It must
38
38
  # name exactly the free indices, and still does not decide what is summed.
39
- destination = CArray.double(3, 2)
39
+ destination = CArray.float64(3, 2)
40
40
  CArray.jit_contract { |i, j, k| destination[i,j] = a[i,k] * b[k,j] }
41
41
  puts " into mine #{destination.to_a == matmul.to_a}"
42
42
 
43
43
  # A sum along an axis is not a contraction: there is nothing in a[i,k]
44
44
  # standing in for a sigma.
45
45
  begin
46
- total = CArray.double(3)
46
+ total = CArray.float64(3)
47
47
  CArray.jit_contract { |i, k| total[i] = a[i,k] }
48
48
  rescue CArray::JIT::Unsupported => error
49
49
  puts " refused: #{error.message.lines.first.strip}"
@@ -51,7 +51,7 @@ end
51
51
 
52
52
  # Nor is a shape mismatch let through.
53
53
  begin
54
- wrong = CArray.double(5, 2)
54
+ wrong = CArray.float64(5, 2)
55
55
  CArray.jit_contract { |i, j, k| a[i,k] * wrong[k,j] }
56
56
  rescue CArray::JIT::Unsupported => error
57
57
  puts " refused: #{error.message.lines.first.strip}"
@@ -9,12 +9,12 @@ $LOAD_PATH.unshift(File.expand_path("../../lib", __dir__))
9
9
  require "carray/jit"
10
10
 
11
11
  n = 7
12
- source = CArray.double(n).seq!(1.0)
12
+ source = CArray.float64(n).seq!(1.0)
13
13
  source[2] = UNDEF
14
14
 
15
15
  # Filling the holes. This is an ordinary Ruby loop as well, so it can be
16
16
  # checked against one.
17
- filled = CArray.double(n)
17
+ filled = CArray.float64(n)
18
18
  CArray.jit_for(n) { |i|
19
19
  if source[i] == UNDEF
20
20
  filled[i] = 0.0
@@ -36,14 +36,14 @@ puts " cells missing #{filled.count_masked} (a mask exists, but it is empty
36
36
  # any cell that fed a result masks that result, following the offsets, so each
37
37
  # output takes its mask from its own inputs rather than from everything the
38
38
  # body read.
39
- neighbours = CArray.double(n)
39
+ neighbours = CArray.float64(n)
40
40
  CArray.jit_for(1...(n-1)) { |i| neighbours[i] = source[i-1] + source[i+1] }
41
41
  puts " propagated #{neighbours.to_a.inspect}"
42
42
  puts " masked where #{(0...n).select { |i| neighbours[i] == UNDEF }.inspect} (the cells reading source[2])"
43
43
 
44
44
  # Writing UNDEF marks a cell missing. Mentioning UNDEF at all makes the kernel
45
45
  # a masked one, whatever its arrays happen to carry.
46
- clipped = CArray.double(n).seq!(1.0)
46
+ clipped = CArray.float64(n).seq!(1.0)
47
47
  CArray.jit_for(n) { |i|
48
48
  if clipped[i] > 4.0
49
49
  clipped[i] = UNDEF
@@ -10,7 +10,7 @@ $LOAD_PATH.unshift(File.expand_path("../../lib", __dir__))
10
10
  require "carray/jit"
11
11
 
12
12
  # A column: its stride is the row pitch, not the element size.
13
- matrix = CArray.double(8, 3)
13
+ matrix = CArray.float64(8, 3)
14
14
  column = matrix[nil, 1]
15
15
  column[0] = 1.0
16
16
 
@@ -22,15 +22,15 @@ puts " others untouched #{matrix[nil, 0].to_a.all?(0.0) && matrix[nil, 2].to_a
22
22
 
23
23
  # A reversed view has a negative stride, and a slice of a slice folds to one
24
24
  # offset and one stride however many times it was sliced.
25
- array = CArray.double(20).seq!
25
+ array = CArray.float64(20).seq!
26
26
  inner = array[4..15][2..9]
27
27
  CArray.jit_for(8) { |i| inner[i] = -inner[i] }
28
28
  puts " slice of a slice #{array[5..14].to_a.inspect}"
29
29
 
30
30
  # A transpose is read at its own indices; nothing is materialised.
31
- source = CArray.double(4, 3).seq!(1.0)
31
+ source = CArray.float64(4, 3).seq!(1.0)
32
32
  transposed = source.transpose
33
- result = CArray.double(3, 4)
33
+ result = CArray.float64(3, 4)
34
34
  CArray.jit_for(*transposed.dim) { |i, j| result[i, j] = transposed[i, j] * 2.0 }
35
35
  puts " transposed read #{result.to_a == (source.transpose * 2.0).to_a}"
36
36
 
@@ -39,7 +39,7 @@ puts " transposed read #{result.to_a == (source.transpose * 2.0).to_a}"
39
39
  # back. The cost is proportional to the region, not to the view: the kernel
40
40
  # knows its extents and its offsets before it runs, so it asks for the box it
41
41
  # will touch and no more.
42
- whole = CArray.double(64).seq!
42
+ whole = CArray.float64(64).seq!
43
43
  gathered = whole[whole >= 0.0]
44
44
  gathered[10] = 1.0
45
45
  CArray.jit_for(11...14) { |i| gathered[i] = gathered[i-1] * 2.0 }