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
@@ -8,7 +8,7 @@
8
8
  $LOAD_PATH.unshift(File.expand_path("../../lib", __dir__))
9
9
  require "carray/jit"
10
10
 
11
- values = CArray.double(16).seq!(1.0)
11
+ values = CArray.float64(16).seq!(1.0)
12
12
 
13
13
  def sweep (values)
14
14
  CArray.jit_for(1...values.dim[0]) { |i| values[i] = Math.sqrt(values[i-1]) + 1.0 }
@@ -26,16 +26,16 @@ puts " matches Ruby #{(0...n).all? { |i| rotated[i] == reference[i] }}"
26
26
  # The way out of the complex type. `abs`, `real`, `imag` and `arg` hand back
27
27
  # a Float, which is what lets a complex kernel write into a real array;
28
28
  # `conjugate` stays complex.
29
- power = CArray.double(n)
30
- phase = CArray.double(n)
29
+ power = CArray.float64(n)
30
+ phase = CArray.float64(n)
31
31
  CArray.jit_for(n) { |i| power[i] = signal[i].abs }
32
32
  CArray.jit_for(n) { |i| phase[i] = signal[i].arg }
33
33
  puts " power #{power.to_a.map { |e| e.round(4) }.inspect}"
34
34
  puts " phase #{phase.to_a.map { |e| e.round(4) }.inspect}"
35
35
 
36
36
  # And the way in, from two real arrays.
37
- real = CArray.double(n).seq!(1.0)
38
- imaginary = CArray.double(n).seq!(0.0, 0.25)
37
+ real = CArray.float64(n).seq!(1.0)
38
+ imaginary = CArray.float64(n).seq!(0.0, 0.25)
39
39
  built = CArray.cmplx128(n)
40
40
  CArray.jit_for(n) { |i| built[i] = Complex(real[i], imaginary[i]) }
41
41
  puts " built from parts #{built.to_a.first(3).inspect}"
@@ -27,8 +27,8 @@ require "benchmark"
27
27
  j0 = CArray.jit_extern("double j0(double)")
28
28
  puts "bound: #{j0}"
29
29
 
30
- x = CArray.double(6).seq!(1.0)
31
- out = CArray.double(6)
30
+ x = CArray.float64(6).seq!(1.0)
31
+ out = CArray.float64(6)
32
32
 
33
33
  CArray.jit_each { out = j0.call(x) }
34
34
  puts "j0(x) #{out.to_a.map { |v| v.round(6) }.inspect}"
@@ -41,7 +41,7 @@ puts
41
41
  # intermediate results to hold them, so this is not expressible as a map --
42
42
  # but it is an ordinary kernel.
43
43
 
44
- smoothed = CArray.double(6)
44
+ smoothed = CArray.float64(6)
45
45
  CArray.jit_for(1...6) { |i| smoothed[i] = 0.5 * (j0.call(x[i]) + j0.call(x[i-1])) }
46
46
  puts "half-sum of neighbouring j0"
47
47
  puts " #{smoothed[1..5].to_a.map { |v| v.round(6) }.inspect}"
@@ -50,8 +50,8 @@ puts
50
50
  # It mixes into an expression like anything else, and takes as many arguments
51
51
  # as the prototype says.
52
52
  atan2 = CArray.jit_extern("double atan2(double, double)")
53
- y = CArray.double(6).seq!(0.5, 0.5)
54
- mixed = CArray.double(6)
53
+ y = CArray.float64(6).seq!(0.5, 0.5)
54
+ mixed = CArray.float64(6)
55
55
  CArray.jit_each { mixed = atan2.call(x, y) * 2.0 - j0.call(x) }
56
56
  puts "atan2(x, y) * 2 - j0(x)"
57
57
  puts " #{mixed.to_a.map { |v| v.round(6) }.inspect}"
@@ -106,10 +106,10 @@ puts
106
106
 
107
107
  # And it is called from a kernel like any other, which is what gives kernels
108
108
  # something they did not have: a body you can factor and name.
109
- w = CArray.double(6).seq!(-0.2, 0.3)
110
- blended = CArray.double(6)
111
- low = CArray.double(6) { 10.0 }
112
- high = CArray.double(6) { 20.0 }
109
+ w = CArray.float64(6).seq!(-0.2, 0.3)
110
+ blended = CArray.float64(6)
111
+ low = CArray.float64(6) { 10.0 }
112
+ high = CArray.float64(6) { 20.0 }
113
113
  CArray.jit_each {
114
114
  blended = low + (high - low) * smoothstep.call(w)
115
115
  }
@@ -140,7 +140,7 @@ puts
140
140
  total = CArray.jit_function("double total(int n, const double v[8])") { |n, v|
141
141
  n == 0 ? 0.0 : v[n - 1] + total.call(n - 1, v)
142
142
  }
143
- values = CArray.double(8).seq!(1.0)
143
+ values = CArray.float64(8).seq!(1.0)
144
144
  puts "sum of #{values.to_a.map(&:to_i).inspect} by recursion #{total.call(8, values)}"
145
145
  puts " matches Ruby #{total.call(8, values) == total.block.call(8, values)}"
146
146
  puts
@@ -194,7 +194,7 @@ puts
194
194
  poly = CArray.jit_function("double (*)(double x, const double coef[3])") { |x, c|
195
195
  c[0] + c[1] * x + c[2] * x * x
196
196
  }
197
- coef = CArray.double(3) { |i| [1.0, 2.0, 3.0][i] }
197
+ coef = CArray.float64(3) { |i| [1.0, 2.0, 3.0][i] }
198
198
  puts "1 + 2x + 3x^2 at x = 2"
199
199
  puts " compiled C #{poly.call(2.0, coef)}"
200
200
  puts " same block in Ruby #{poly.block.call(2.0, coef)}"
@@ -210,8 +210,8 @@ ode = CArray.jit_function(
210
210
  dydt[1] = -y[0]
211
211
  0
212
212
  }
213
- state = CArray.double(2) { |i| [1.0, 0.0][i] }
214
- derivative = CArray.double(2)
213
+ state = CArray.float64(2) { |i| [1.0, 0.0][i] }
214
+ derivative = CArray.float64(2)
215
215
  ode.call(0.0, state, derivative, nil)
216
216
  puts "harmonic oscillator: y = #{state.to_a.inspect} -> dy/dt = #{derivative.to_a.inspect}"
217
217
  puts ode.c_source.lines.last(6).join
@@ -221,8 +221,8 @@ puts ode.c_source.lines.last(6).join
221
221
  # `x[i]` is a cell and `coef` is the whole array. Which is meant comes from
222
222
  # the declaration rather than from the spelling.
223
223
 
224
- ramp = CArray.double(6).seq!(0.0, 0.5)
225
- fitted = CArray.double(6)
224
+ ramp = CArray.float64(6).seq!(0.0, 0.5)
225
+ fitted = CArray.float64(6)
226
226
  CArray.jit_for(6) { |i| fitted[i] = poly.call(ramp[i], coef) }
227
227
  puts "1 + 2x + 3x^2 along a ramp"
228
228
  puts " #{fitted.to_a.map { |v| v.round(4) }.inspect}"
@@ -235,8 +235,8 @@ puts
235
235
  # cell at a time, or find a Math method that happens to exist.
236
236
 
237
237
  n = 200_000
238
- big = CArray.double(n).seq!(1.0, 1e-5)
239
- big_out = CArray.double(n)
238
+ big = CArray.float64(n).seq!(1.0, 1e-5)
239
+ big_out = CArray.float64(n)
240
240
  gamma = CArray.jit_extern("double tgamma(double)")
241
241
 
242
242
  def timed
@@ -1,10 +1,9 @@
1
1
  # Letting CArray drive the loop.
2
2
  #
3
3
  # An element-wise pass is the one shape that is a *sweep*: nothing reaches a
4
- # neighbour, nothing chooses an order. So where this CArray has
5
- # `ca_call_cslab` (3.0.1 and later), jit_each hands it the compiled body
6
- # and lets it acquire the operands -- broadcasting them, ORing and propagating
7
- # the masks, and calling back a chunk at a time.
4
+ # neighbour, nothing chooses an order. So jit_each hands the compiled body to
5
+ # CArray's `ca_call_cslab` and lets it acquire the operands -- broadcasting
6
+ # them, ORing and propagating the masks, and calling back a chunk at a time.
8
7
  #
9
8
  # Nothing about the expression changes, and nothing here is written
10
9
  # differently. What changes is who opens the arrays, and what that costs.
@@ -13,12 +12,8 @@ $LOAD_PATH.unshift(File.expand_path("../../lib", __dir__))
13
12
  require "carray/jit"
14
13
  require "benchmark"
15
14
 
16
- Sweep = CArray::JIT::Sweep
17
15
  Access = CArray::JIT::Access
18
16
 
19
- puts "this CArray has ca_call_cslab: #{Sweep.available?}"
20
- puts
21
-
22
17
  # The rank leaves the loop
23
18
  # ------------------------
24
19
  # A chunk is one flat run of cells -- and CArray's acquire reads an operand's
@@ -29,10 +24,10 @@ def doubled (source, out)
29
24
  CArray.jit_each { out = source * 2.0 }
30
25
  end
31
26
 
32
- flat_in = CArray.double(6).seq!
33
- flat_out = CArray.double(6)
34
- cube_in = CArray.double(2, 3, 4).seq!
35
- cube_out = CArray.double(2, 3, 4)
27
+ flat_in = CArray.float64(6).seq!
28
+ flat_out = CArray.float64(6)
29
+ cube_in = CArray.float64(2, 3, 4).seq!
30
+ cube_out = CArray.float64(2, 3, 4)
36
31
 
37
32
  flat = doubled(flat_in, flat_out)
38
33
  cube = doubled(cube_in, cube_out)
@@ -62,10 +57,10 @@ puts
62
57
  # whole array.
63
58
 
64
59
  n = 2_000_000
65
- src = CArray.double(n).seq!
66
- b = CArray.double(n).seq!(0.5, 0.5)
67
- out = CArray.double(n)
68
- gather = src[CArray.int32(n).seq.reverse]
60
+ src = CArray.float64(n).seq!
61
+ b = CArray.float64(n).seq!(0.5, 0.5)
62
+ out = CArray.float64(n)
63
+ gather = src[CArray.int32(n).seq.flip]
69
64
 
70
65
  def timed
71
66
  2.times { yield }
@@ -98,25 +93,25 @@ puts
98
93
  # --------------------------
99
94
  # Four things, and only the last is about this CArray being old.
100
95
 
101
- columns = CArray.double(64, 64).seq!
102
- into = CArray.double(64, 64)
96
+ columns = CArray.float64(64, 64).seq!
97
+ into = CArray.float64(64, 64)
103
98
  every_other = columns[nil, (0...64).step(2)]
104
99
  every_other_out = into[nil, (0...64).step(2)]
105
100
  kernel = CArray.jit_each { every_other_out = every_other * 2.0 }
106
101
  puts "a strided view operand loop axes #{kernel.rank}"
107
102
  puts " -- walked in place here, re-gathered there for nothing."
108
103
 
109
- matrix = CArray.double(3, 4).seq!
110
- row = CArray.double(1, 4).seq!(10.0)
111
- stretched = CArray.double(3, 4)
104
+ matrix = CArray.float64(3, 4).seq!
105
+ row = CArray.float64(1, 4).seq!(10.0)
106
+ stretched = CArray.float64(3, 4)
112
107
  kernel = CArray.jit_each { stretched = matrix * row }
113
108
  puts "a stretched operand loop axes #{kernel.rank}"
114
109
  puts " -- broadcasting arrives as a stride of zero on an axis, and a flat"
115
110
  puts " run has no axes, so cell k would stop lining up."
116
111
 
117
- masked = CArray.double(5).seq!(1.0)
112
+ masked = CArray.float64(5).seq!(1.0)
118
113
  masked[2] = UNDEF
119
- masked_out = CArray.double(5)
114
+ masked_out = CArray.float64(5)
120
115
  kernel = CArray.jit_each { masked_out = masked * 2.0 }
121
116
  puts "a mask anywhere in the pass sweepable? #{kernel.sweepable?}"
122
117
  puts " -- an operand that carries one, or a body that asks about one."
@@ -133,7 +128,7 @@ puts
133
128
  # names an index reaches neighbours, chooses an order and runs inner loops,
134
129
  # none of which a chunked walk can offer. Splitting the two methods by what
135
130
  # the block names drew the same line as what a sweep can drive.
136
- recurrence = CArray.double(8).seq!(1.0)
131
+ recurrence = CArray.float64(8).seq!(1.0)
137
132
  CArray.jit_for(1...8) { |i| recurrence[i] = recurrence[i-1] * 1.5 }
138
133
  puts "jit_for stays here by its nature"
139
134
  puts " #{recurrence.to_a.map { |v| v.round(4) }.inspect}"
@@ -8,10 +8,10 @@
8
8
  $LOAD_PATH.unshift(File.expand_path("../../lib", __dir__))
9
9
  require "carray/jit"
10
10
 
11
- gain = CScalar.double() { 2.0 }
12
- offset = CScalar.double() { 10.0 }
13
- signal = CArray.double(6).seq!(1.0)
14
- out = CArray.double(6)
11
+ gain = CScalar.float64() { 2.0 }
12
+ offset = CScalar.float64() { 10.0 }
13
+ signal = CArray.float64(6).seq!(1.0)
14
+ out = CArray.float64(6)
15
15
 
16
16
  # In an expression it is read at every cell, which is what CArray's own
17
17
  # operators do with it. Nothing here says it is a scalar; it says so itself.
@@ -29,18 +29,18 @@ puts
29
29
  # An indexed kernel reaches it too, and there the missing index is the point:
30
30
  # there is no axis to walk, so there is no index to write. A bare name says
31
31
  # the same thing.
32
- indexed = CArray.double(6)
32
+ indexed = CArray.float64(6)
33
33
  CArray.jit_for(6) { |i| indexed[i] = signal[i] * gain[] }
34
34
  puts "the same, with the loop written out"
35
35
  puts " #{indexed.to_a.inspect}"
36
36
 
37
- bare = CArray.double(6)
37
+ bare = CArray.float64(6)
38
38
  CArray.jit_for(6) { |i| bare[i] = signal[i] * gain }
39
39
  puts " a bare `gain` agrees #{bare.to_a == indexed.to_a}"
40
40
 
41
41
  # It is still the one-cell array it is, so the index spelling keeps working
42
42
  # and means the same cell.
43
- spelled = CArray.double(6)
43
+ spelled = CArray.float64(6)
44
44
  CArray.jit_for(6) { |i| spelled[i] = signal[i] * gain[0] }
45
45
  puts " and so does gain[0] #{spelled.to_a == indexed.to_a}"
46
46
  puts
@@ -48,8 +48,8 @@ puts
48
48
  # Every iteration writes the one cell it has, and what is left is what the
49
49
  # same Ruby loop leaves -- which makes it an accumulator without asking for
50
50
  # one. The inner loop of a reduction rests on exactly this.
51
- total = CScalar.int() { 0 }
52
- counts = CArray.int(4).seq!(1)
51
+ total = CScalar.int32() { 0 }
52
+ counts = CArray.int32(4).seq!(1)
53
53
  CArray.jit_for(4) { |i| total[] = total[] + counts[i] }
54
54
  puts "total after one pass over #{counts.to_a.inspect} #{total[0]}"
55
55
 
@@ -59,9 +59,9 @@ puts " what the same Ruby loop leaves #{in_ruby}"
59
59
  puts
60
60
 
61
61
  # A contraction takes one as it takes any other operand it does not index.
62
- left = CArray.double(3).seq!(1.0)
63
- right = CArray.double(3).seq!(1.0)
64
- weight = CScalar.double() { 2.0 }
62
+ left = CArray.float64(3).seq!(1.0)
63
+ right = CArray.float64(3).seq!(1.0)
64
+ weight = CScalar.float64() { 2.0 }
65
65
  puts "weighted dot product #{CArray.jit_contract { |k| left[k] * right[k] * weight[] }[0]}"
66
66
  puts
67
67
 
@@ -10,7 +10,7 @@ $LOAD_PATH.unshift(File.expand_path("../../lib", __dir__))
10
10
  require "carray/jit"
11
11
 
12
12
  rows, columns = 5, 6
13
- src = CArray.double(rows, columns).seq!(1.0)
13
+ src = CArray.float64(rows, columns).seq!(1.0)
14
14
 
15
15
  # The block's parameters are windows onto the arrays given, in that order:
16
16
  # a[0, 0] is the cell the loop is on, a[-1, 0] its neighbour. The block's
@@ -21,7 +21,7 @@ smoothed = CArray.jit_stencil(src) { |a|
21
21
  }
22
22
 
23
23
  # The same stencil with the indices named, over the interior alone.
24
- written = CArray.double(rows, columns)
24
+ written = CArray.float64(rows, columns)
25
25
  CArray.jit_for(1...(rows-1), 1...(columns-1)) { |i, j|
26
26
  written[i, j] = 0.25 * (src[i-1, j] + src[i+1, j] + src[i, j-1] + src[i, j+1])
27
27
  }
@@ -35,7 +35,7 @@ puts " border missing by default #{smoothed.count_masked} cells"
35
35
  # ---------------------------------------------------------------- the border
36
36
 
37
37
  # Five answers, on one row of a small array so they can be read side by side.
38
- line = CArray.double(1, 5).seq!(1.0) # 1 2 3 4 5
38
+ line = CArray.float64(1, 5).seq!(1.0) # 1 2 3 4 5
39
39
 
40
40
  def row (result)
41
41
  result.to_a[0].map { |e| e == UNDEF ? " --" : format("%4.1f", e) }.join(" ")
@@ -46,7 +46,7 @@ puts "border:, on [1 2 3 4 5] averaging each cell with its two neighbours"
46
46
  [:mask, :skip, :zero, :clamp, :wrap].each do |border|
47
47
  # :skip leaves the border as it was found, so what it was found as is worth
48
48
  # seeing: this one is filled with -1 first.
49
- into = CArray.double(1, 5).seq!(-1.0, 0.0)
49
+ into = CArray.float64(1, 5).seq!(-1.0, 0.0)
50
50
  result = CArray.jit_stencil(line, border: border, into: into) { |a|
51
51
  (a[0, -1] + a[0, 0] + a[0, 1]) / 3.0
52
52
  }
@@ -64,8 +64,8 @@ end
64
64
  # Each array given gets a window, in the order the block names them; the names
65
65
  # shadow whatever they hold outside, as CArray.fuse's do. A diffusion step
66
66
  # with a conductivity that varies from cell to cell:
67
- u = CArray.double(6, 6) { |i, j| (i - 2.5).abs + (j - 2.5).abs }
68
- k = CArray.double(6, 6).fill(0.2)
67
+ u = CArray.float64(6, 6) { |i, j| (i - 2.5).abs + (j - 2.5).abs }
68
+ k = CArray.float64(6, 6).fill(0.2)
69
69
 
70
70
  stepped = CArray.jit_stencil(u, k, border: :clamp) { |u, k|
71
71
  u[0,0] + k[0,0] * (u[-1,0] + u[1,0] + u[0,-1] + u[0,1] - 4.0 * u[0,0])
@@ -10,7 +10,7 @@ $LOAD_PATH.unshift(File.expand_path("../../lib", __dir__))
10
10
  require "carray/jit"
11
11
 
12
12
  rows, columns = 5, 8
13
- source = CArray.double(rows, columns) { |i, j| (j - i) * 1.0 }
13
+ source = CArray.float64(rows, columns) { |i, j| (j - i) * 1.0 }
14
14
  threshold = 1.5
15
15
 
16
16
  # A search: the first column past the threshold, or -1. `break` in an inner
@@ -41,11 +41,11 @@ puts " matches Ruby #{first.to_a == reference}"
41
41
  # the better spelling wherever a bound exists: the kernel cannot fail to stop,
42
42
  # and a cell that ran out of passes can be told from one that converged.
43
43
  n = 2000
44
- a = CArray.double(n).seq!(1.0)
44
+ a = CArray.float64(n).seq!(1.0)
45
45
  tolerance = 1e-12
46
46
  cap = 40
47
47
 
48
- root = CArray.double(n)
48
+ root = CArray.float64(n)
49
49
  passes = CArray.int32(n)
50
50
  CArray.jit_for(n) { |i|
51
51
  x = a[i]
@@ -63,7 +63,7 @@ puts
63
63
  puts "Newton, bounded by the extent"
64
64
  # Against Math.sqrt rather than against the loop's own residual: the residual
65
65
  # is what the break tested, so it can only come back at the tolerance.
66
- exact = CArray.double(n) { |i| Math.sqrt(a[i]) }
66
+ exact = CArray.float64(n) { |i| Math.sqrt(a[i]) }
67
67
  puts format(" worst relative error %.1e (vs Math.sqrt)",
68
68
  ((root - exact).abs / exact).max)
69
69
  puts format(" worst residual %.1e (what the break tested)",
@@ -76,7 +76,7 @@ puts " none ran out #{passes.max < cap}"
76
76
  # Where the bound is not knowable, `while` says so. The condition is read at
77
77
  # the top of every pass, as Ruby's is; a local the condition reads has to be a
78
78
  # local before the loop, since the condition is read before the body is.
79
- by_while = CArray.double(n)
79
+ by_while = CArray.float64(n)
80
80
  CArray.jit_for(n) { |i|
81
81
  guess = a[i]
82
82
  while (guess * guess - a[i]).abs > tolerance * a[i]
@@ -107,7 +107,7 @@ end
107
107
 
108
108
  # `next` in the kernel block skips the cell, the way it would end a block Ruby
109
109
  # was running: the cell keeps its value and its mask, and the loop moves on.
110
- kept = CArray.double(n).fill(-1.0)
110
+ kept = CArray.float64(n).fill(-1.0)
111
111
  CArray.jit_for(n) { |i|
112
112
  next if a[i] % 2.0 == 0.0
113
113
  kept[i] = a[i]
@@ -9,8 +9,8 @@ $LOAD_PATH.unshift(File.expand_path("../../lib", __dir__))
9
9
  require "carray/jit"
10
10
 
11
11
  n = 8
12
- depth = CArray.double(n).seq!(3.0, -1.0) # 3 2 1 0 -1 -2 ...
13
- out = CArray.double(n).fill(-99.0)
12
+ depth = CArray.float64(n).seq!(3.0, -1.0) # 3 2 1 0 -1 -2 ...
13
+ out = CArray.float64(n).fill(-99.0)
14
14
 
15
15
  begin
16
16
  CArray.jit_for(n) { |i|
@@ -38,10 +38,10 @@ puts " untouched after it #{out.to_a[4..].all?(-99.0)}"
38
38
  # A cell whose value is missing does not raise: the comparison was decided by
39
39
  # bytes that mean nothing, and this is the rule the division helper already
40
40
  # keeps and that `if` keeps for what it writes.
41
- holed = CArray.double(4).seq!(1.0)
41
+ holed = CArray.float64(4).seq!(1.0)
42
42
  holed[2] = 999.0 # a value that would raise below
43
43
  holed[2] = UNDEF # marked missing; the bytes are left alone
44
- squares = CArray.double(4)
44
+ squares = CArray.float64(4)
45
45
  CArray.jit_for(4) { |i|
46
46
  raise "out of range" if holed[i] > 100.0
47
47
  squares[i] = holed[i] * holed[i]
@@ -14,11 +14,8 @@ class CArray
14
14
 
15
15
  # ---- the vocabulary a local array is written in ----
16
16
 
17
- # The types a body may make an array of: CArray's own constructor names,
18
- # each naming the storage type CArray gives it. Taken from CArray
19
- # rather than invented here, which is why the aliases land where they
20
- # do -- `CArray.float` is a float32 and `CArray.complex` is a cmplx64,
21
- # the opposite of what Ruby's own Float and Complex would suggest.
17
+ # The types a body may make an array of, under the width names CArray
18
+ # gives them.
22
19
  LOCAL_ARRAY_TYPES = {
23
20
  :int8 => "int8", :int16 => "int16",
24
21
  :int32 => "int32", :int64 => "int64",
@@ -27,7 +24,13 @@ class CArray
27
24
  :float32 => "float32", :float64 => "float64",
28
25
  :cmplx64 => "cmplx64", :cmplx128 => "cmplx128",
29
26
  :boolean => "boolean",
30
- # The aliases, as CArray spells them.
27
+ }.freeze
28
+
29
+ # The C type names. A block is written in the width names, so these
30
+ # are refused with the width name each stands for -- which is worth
31
+ # saying, since `float` is a float32 and `complex` a cmplx64, the
32
+ # opposite of what Ruby's own Float and Complex would suggest.
33
+ LOCAL_ARRAY_C_TYPE_NAMES = {
31
34
  :float => "float32", :double => "float64",
32
35
  :complex => "cmplx64", :dcomplex => "cmplx128",
33
36
  :byte => "uint8", :short => "int16",
@@ -67,7 +70,7 @@ class CArray
67
70
  }.freeze
68
71
  LOCAL_ARRAY_REFUSED_TYPES = LOCAL_ARRAY_REFUSED_TYPES_HOLD.keys.freeze
69
72
 
70
- # CArray's compatibility layer, which `data_type_extension.rb` opens by
73
+ # CArray's compatibility layer, which `factories.rb` opens by
71
74
  # calling itself "Numo / NumPy-style" and "not the 'main' carray API".
72
75
  # The words inside a block are carray's own, so these are sent to the
73
76
  # spelling they stand for rather than taken.
@@ -76,19 +79,18 @@ class CArray
76
79
  # Every name a constructor may be written under, so that the receiver of
77
80
  # one is known for what it is before anything else has been read.
78
81
  LOCAL_ARRAY_CONSTRUCTOR_NAMES =
79
- (LOCAL_ARRAY_TYPES.keys + LOCAL_ARRAY_REFUSED_TYPES +
82
+ (LOCAL_ARRAY_TYPES.keys + LOCAL_ARRAY_C_TYPE_NAMES.keys +
83
+ LOCAL_ARRAY_REFUSED_TYPES +
80
84
  LOCAL_ARRAY_COMPATIBILITY_NAMES + [:new, :empty]).freeze
81
85
 
82
- # `CArray`, `::CArray`, or one of the type classes under either. A
83
- # constructor is written on one of these and on nothing else.
86
+ # `CArray` or `::CArray`. A constructor is written on one of these
87
+ # and on nothing else.
84
88
  def self.carray_receiver? (node)
85
89
  case node
86
90
  when Prism::ConstantReadNode
87
91
  node.name == :CArray
88
92
  when Prism::ConstantPathNode
89
- text = node.slice
90
- text == "::CArray" ||
91
- text.start_with?("CArray::") || text.start_with?("::CArray::")
93
+ node.slice == "::CArray"
92
94
  else
93
95
  false
94
96
  end
@@ -755,7 +757,7 @@ class CArray
755
757
  return [] unless node
756
758
  return [] if raise_call?(node)
757
759
  # The receiver of a constructor is part of a spelling rather than a
758
- # name the block reached outside for: `CArray.double(9)` says what to
760
+ # name the block reached outside for: `CArray.float64(9)` says what to
759
761
  # declare, and the class in front of it is not a value to look up.
760
762
  # Left in, it is looked up, found to be a Class, and refused for
761
763
  # being one -- which says nothing about the line that was written.
@@ -835,7 +837,7 @@ class CArray
835
837
  built = statements[0..-2].map { |node| build_statement(node) }
836
838
  if map_assignment
837
839
  built << build_statement(last)
838
- # `w = CArray.double(4)` as the last line: the block's value in Ruby
840
+ # `w = CArray.float64(4)` as the last line: the block's value in Ruby
839
841
  # is the array, and a cell of the result has nowhere to put one.
840
842
  if built.last.is_a?(LocalArrayDeclaration)
841
843
  raise Unsupported.new(
@@ -1880,7 +1882,7 @@ class CArray
1880
1882
  "`#{name}` to; declare what it points at",
1881
1883
  argument.location)
1882
1884
  end
1883
- wanted = CDeclaration::DATA_TYPES.fetch(parameter.element.fiddle).to_s
1885
+ wanted = CDeclaration::DATA_TYPES.fetch(parameter.element.abi).to_s
1884
1886
  unless storage == wanted
1885
1887
  raise Unsupported.new(
1886
1888
  "`#{c_function}` takes `#{parameter.text}` there, which is a " \
@@ -2031,7 +2033,7 @@ class CArray
2031
2033
  if passed && @array_names.include?(passed)
2032
2034
  raise Unsupported.new(
2033
2035
  "`#{node.name}` takes a local array -- one this block made with " \
2034
- "`CArray.double(n)` or its kin -- and `#{passed}` is an array " \
2036
+ "`CArray.float64(n)` or its kin -- and `#{passed}` is an array " \
2035
2037
  "the block closed over. Walking all of it at every cell is a " \
2036
2038
  "pass over the whole array per cell, which is not what the line " \
2037
2039
  "looks like it costs; `#{passed}.#{node.name}` outside the " \
@@ -2040,7 +2042,7 @@ class CArray
2040
2042
  end
2041
2043
  raise Unsupported.new(
2042
2044
  "`#{node.name}` takes a local array -- one this block made with " \
2043
- "`CArray.double(n)` or its kin -- named on its own, and " \
2045
+ "`CArray.float64(n)` or its kin -- named on its own, and " \
2044
2046
  "`#{argument.slice}` is not one",
2045
2047
  node.location)
2046
2048
  end
@@ -2085,10 +2087,6 @@ class CArray
2085
2087
  "`#{node.slice}` passes a keyword, and a local array is the " \
2086
2088
  "type and the shape only", node.location)
2087
2089
  end
2088
- if node.receiver.is_a?(Prism::ConstantPathNode) &&
2089
- node.receiver.slice != "::CArray"
2090
- return refuse_a_type_class_constructor(node, arguments)
2091
- end
2092
2090
  case node.name
2093
2091
  when :new then read_carray_new(node, arguments, true)
2094
2092
  when :empty then read_carray_empty(node, arguments)
@@ -2096,7 +2094,7 @@ class CArray
2096
2094
  end
2097
2095
  end
2098
2096
 
2099
- # `CArray.double(9)` and the rest of the singleton methods CArray's own
2097
+ # `CArray.float64(9)` and the rest of the singleton methods CArray's own
2100
2098
  # `Init` defines: the type is the name and the shape is the arguments.
2101
2099
  def read_type_constructor (node, arguments)
2102
2100
  if LOCAL_ARRAY_REFUSED_TYPES.include?(node.name)
@@ -2104,12 +2102,20 @@ class CArray
2104
2102
  "`#{node.name}` holds #{LOCAL_ARRAY_REFUSED_TYPES_HOLD.fetch(node.name)} " \
2105
2103
  "rather than numbers, and a kernel computes with numbers; a " \
2106
2104
  "local array is one of " \
2107
- "#{LOCAL_ARRAY_TYPES.keys.take(13).map { |name| "`#{name}`" }.join(', ')}",
2105
+ "#{LOCAL_ARRAY_TYPES.keys.map { |name| "`#{name}`" }.join(', ')}",
2108
2106
  node.location)
2109
2107
  end
2110
2108
  if LOCAL_ARRAY_COMPATIBILITY_NAMES.include?(node.name)
2111
2109
  return refuse_a_compatibility_constructor(node, arguments, nil)
2112
2110
  end
2111
+ if LOCAL_ARRAY_C_TYPE_NAMES.key?(node.name)
2112
+ width = LOCAL_ARRAY_C_TYPE_NAMES.fetch(node.name)
2113
+ raise Unsupported.new(
2114
+ "`CArray.#{node.name}` uses the C type name `#{node.name}`; a " \
2115
+ "kernel body is written in the width names, so write " \
2116
+ "`#{node.slice.sub(/\.#{node.name}\b/, ".#{width}")}`",
2117
+ node.location)
2118
+ end
2113
2119
  storage = LOCAL_ARRAY_TYPES.fetch(node.name)
2114
2120
  if arguments.empty?
2115
2121
  raise Unsupported.new(
@@ -2174,10 +2180,17 @@ class CArray
2174
2180
  "rather than numbers, and a kernel computes with numbers",
2175
2181
  node.location)
2176
2182
  end
2183
+ if LOCAL_ARRAY_C_TYPE_NAMES.key?(name)
2184
+ width = LOCAL_ARRAY_C_TYPE_NAMES.fetch(name)
2185
+ raise Unsupported.new(
2186
+ "`:#{name}` is the C type name `#{name}`; a kernel body is " \
2187
+ "written in the width names, so write `:#{width}`",
2188
+ node.location)
2189
+ end
2177
2190
  LOCAL_ARRAY_TYPES.fetch(name) do
2178
2191
  raise Unsupported.new(
2179
2192
  "`#{argument.slice}` is not a type a local array holds; they are " \
2180
- "#{LOCAL_ARRAY_TYPES.keys.take(13).map { |n| "`:#{n}`" }.join(', ')}",
2193
+ "#{LOCAL_ARRAY_TYPES.keys.map { |n| "`:#{n}`" }.join(', ')}",
2181
2194
  node.location)
2182
2195
  end
2183
2196
  end
@@ -2250,7 +2263,7 @@ class CArray
2250
2263
  end
2251
2264
 
2252
2265
  # `CArray.zeros(4)`, `CArray.empty(4)`, `CArray.ones(4)`: the
2253
- # compatibility layer, which `data_type_extension.rb` opens by calling
2266
+ # compatibility layer, which `factories.rb` opens by calling
2254
2267
  # itself "Numo / NumPy-style" and "not the 'main' carray API". The
2255
2268
  # words inside a block are carray's own.
2256
2269
  def refuse_a_compatibility_constructor (node, arguments, type)
@@ -2266,14 +2279,6 @@ class CArray
2266
2279
  node.location)
2267
2280
  end
2268
2281
 
2269
- # `CArray::Int64.empty(4)` and `CArray::Int64.zeros(4)`: the same layer
2270
- # reached through a type class, which is the Numo spelling of it.
2271
- def refuse_a_type_class_constructor (node, arguments)
2272
- tail = node.receiver.slice.split("::").last.to_s
2273
- type = LOCAL_ARRAY_TYPES[tail.downcase.to_sym]
2274
- refuse_a_compatibility_constructor(node, arguments, type)
2275
- end
2276
-
2277
2282
  # Which entry points take one, and which release takes the rest. The
2278
2283
  # shape of the check is the mode this analyzer was made in, because that
2279
2284
  # is what says how the block is being read.