carray-jit 0.1.7 → 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 (55) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +18 -0
  3. data/README.md +2 -2
  4. data/carray-jit.gemspec +4 -2
  5. data/docs/02_KernelShapes.md +5 -5
  6. data/docs/03_SupportedFeatures.md +27 -27
  7. data/docs/04_Compiling.md +1 -1
  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 +38 -33
  48. data/lib/carray/jit/c_generator.rb +1 -1
  49. data/lib/carray/jit/call.rb +1 -1
  50. data/lib/carray/jit/compiler.rb +34 -17
  51. data/lib/carray/jit/node.rb +1 -1
  52. data/lib/carray/jit/type_assignment.rb +9 -4
  53. data/lib/carray/jit/version.rb +1 -1
  54. data/lib/carray/jit.rb +7 -9
  55. metadata +6 -6
@@ -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(
@@ -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.
@@ -2606,7 +2606,7 @@ class CArray
2606
2606
  @intrinsic_needs << entry unless @intrinsic_needs.include?(entry)
2607
2607
  end
2608
2608
 
2609
- # `w = CArray.double(9)` where the statement stands. The declaration is
2609
+ # `w = CArray.float64(9)` where the statement stands. The declaration is
2610
2610
  # at the head of the block; what is left here is what Ruby does at this
2611
2611
  # line, which for the zeroed spellings is a fresh array of zeros.
2612
2612
  #
@@ -25,7 +25,7 @@ class CArray
25
25
  #
26
26
  # def moving_average (values, window)
27
27
  # n = values.elements
28
- # out = CArray.double(n)
28
+ # out = CArray.float64(n)
29
29
  # CArray.jit_call("void (*)(double *out, const double *values, " \
30
30
  # "size_t n, size_t window)") {
31
31
  # n.times { |i| ... }
@@ -153,26 +153,43 @@ class CArray
153
153
  # CArray's own version is defined by its extension, and this may be
154
154
  # running without it: the command that looks after the cache loads the
155
155
  # compiler and nothing else, on purpose, so that a cache can be
156
- # inspected or cleared when CArray itself will not load. RubyGems
157
- # knows which version a `require` would activate without activating
158
- # it, and that is the same one -- except where CArray is on the load
159
- # path from a checkout, which nothing outside the process could have
160
- # known either way.
156
+ # inspected or cleared when CArray itself will not load. What it has
157
+ # to name is the version a `require "carray"` here would load, without
158
+ # doing the require.
159
+ #
160
+ # A checkout put on the load path with `-I` is found the way the
161
+ # require would find it, and its version read from the `version.h`
162
+ # beside the built extension. Otherwise RubyGems knows: the version
163
+ # already activated, as under Bundler, or else the one a `require`
164
+ # would activate.
161
165
  def carray_version
162
166
  return CArray::VERSION if defined?(CArray::VERSION)
163
167
  return @carray_version if defined?(@carray_version)
164
- @carray_version =
165
- if defined?(Gem::Specification)
166
- begin
167
- Gem::Specification.find_by_name("carray").version.to_s
168
- rescue StandardError, LoadError
169
- # A gem that is not installed raises Gem::MissingSpecError,
170
- # which is a LoadError rather than a StandardError.
171
- "none"
172
- end
173
- else
174
- "none"
175
- end
168
+ @carray_version = checkout_carray_version || installed_carray_version
169
+ return @carray_version
170
+ end
171
+
172
+ # An installed gem keeps `version.h` in `ext/` and the extension in
173
+ # `lib/`, so only a checkout has the two side by side.
174
+ def checkout_carray_version
175
+ extension = "carray_ext.#{RbConfig::CONFIG['DLEXT']}"
176
+ directory = $LOAD_PATH.map { |path| File.expand_path(path.to_s) }
177
+ .find { |path| File.file?(File.join(path, extension)) }
178
+ return nil unless directory
179
+ header = File.join(directory, "version.h")
180
+ return nil unless File.file?(header)
181
+ return File.read(header)[/^#define\s+CA_VERSION\s+"([^"]+)"/, 1]
182
+ end
183
+
184
+ def installed_carray_version
185
+ return "none" unless defined?(Gem::Specification)
186
+ loaded = Gem.loaded_specs["carray"]
187
+ return loaded.version.to_s if loaded
188
+ return Gem::Specification.find_by_name("carray").version.to_s
189
+ rescue StandardError, LoadError
190
+ # A gem that is not installed raises Gem::MissingSpecError, which
191
+ # is a LoadError rather than a StandardError.
192
+ return "none"
176
193
  end
177
194
 
178
195
  # Directories for versions and architectures no longer in use, so
@@ -691,7 +691,7 @@ class CArray
691
691
  end
692
692
  end
693
693
 
694
- # `w = CArray.double(9)`, `CArray.new(:int64, [256])`, or
694
+ # `w = CArray.float64(9)`, `CArray.new(:int64, [256])`, or
695
695
  # `CArray.empty(:float64, [9])`: an array the body owns.
696
696
  #
697
697
  # It is declared at the head of the C block its Ruby scope stands for, as
@@ -869,10 +869,11 @@ class CArray
869
869
  # scalar takes the array's dtype rather than the array widening to meet
870
870
  # it, so `f32 * 2.0` is float32 where the order alone would say double.
871
871
  #
872
- # What the array gives the scalar is a width, never a kind, so the two
873
- # have to be the same kind for anything to be given. `i32 * 2.0` is a
874
- # float64 and `f32 * 1i` a cmplx128 -- the scalar\'s own Ruby type, since
875
- # the array has no narrower one of that kind to offer. And a real
872
+ # What the array gives the scalar is a width. `i32 * 2.0` is a float64,
873
+ # the scalar\'s own Ruby type, since an integer array has no width of
874
+ # a float to offer. A float array has one for a Complex: `f32 * 1i`
875
+ # is a cmplx64, the complex type made of the array\'s float, as it is
876
+ # in CArray from 3.0.9 (it was a cmplx128 before). And a real
876
877
  # scalar against a complex array stays real, which is not a width
877
878
  # question at all: Ruby\'s `z + x` adds to the real part and leaves the
878
879
  # imaginary one alone, sign included, and that is a different operation
@@ -887,6 +888,10 @@ class CArray
887
888
  # The wider kind wins, as it does between two arrays. What absorption
888
889
  # settles is the tie: at the same kind the scalar takes the other
889
890
  # side's width rather than the other side widening to Ruby's.
891
+ if scalar_kind == :complex && typed.type == :float
892
+ scalar.type = :float_complex
893
+ return scalar.type
894
+ end
890
895
  return scalar.type if KIND_RANK[scalar_kind] > KIND_RANK[typed_kind]
891
896
  if scalar_kind == typed_kind
892
897
  refuse_wide_capture(scalar, typed)
@@ -1,5 +1,5 @@
1
1
  class CArray
2
2
  module JIT
3
- VERSION = "0.1.7"
3
+ VERSION = "0.1.8"
4
4
  end
5
5
  end
data/lib/carray/jit.rb CHANGED
@@ -1356,9 +1356,8 @@ class CArray
1356
1356
  # and keying on it would miss on every call of the loop this is for.
1357
1357
  #
1358
1358
  # Values only, never an operand: a block's plans live as long as the
1359
- # process, and holding an array would hold its memory as long. Shapes
1360
- # from `ndim`, `elements` and `dim0`.. rather than `dim`, which builds
1361
- # an Array on every call.
1359
+ # process, and holding an array would hold its memory as long. A 1-D
1360
+ # shape is its `elements`, which builds no Array.
1362
1361
  #
1363
1362
  # nil where a scalar is not one a kernel takes, so that the call is
1364
1363
  # refused where and as it always was rather than here.
@@ -1368,11 +1367,10 @@ class CArray
1368
1367
  rank = array.ndim
1369
1368
  key << name << array.data_type << rank << array.is_a?(CScalar) <<
1370
1369
  array.has_mask? << Access.tier(array)
1371
- case rank
1372
- when 1 then key << array.elements
1373
- when 2 then key << array.dim0 << array.dim1
1374
- when 3 then key << array.dim0 << array.dim1 << array.dim2
1375
- else key.concat(array.dim)
1370
+ if rank == 1
1371
+ key << array.elements
1372
+ else
1373
+ key.concat(array.shape)
1376
1374
  end
1377
1375
  end
1378
1376
  key << scalars.size
@@ -2051,7 +2049,7 @@ class CArray
2051
2049
  }
2052
2050
  return unless shadowed
2053
2051
  raise Unsupported,
2054
- "`#{shadowed}` is made in this block with `CArray.double` or " \
2052
+ "`#{shadowed}` is made in this block with `CArray.float64` or " \
2055
2053
  "its kin, and the block also closes over an array named " \
2056
2054
  "`#{shadowed}`; in this spelling an assignment writes that " \
2057
2055
  "array's cell, so the line means one thing to a reader and " \
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carray-jit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - himotoyoshi
@@ -15,20 +15,20 @@ dependencies:
15
15
  requirements:
16
16
  - - ">="
17
17
  - !ruby/object:Gem::Version
18
- version: 3.0.6
18
+ version: 3.1.0
19
19
  - - "<"
20
20
  - !ruby/object:Gem::Version
21
- version: '3.1'
21
+ version: '3.2'
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  requirements:
26
26
  - - ">="
27
27
  - !ruby/object:Gem::Version
28
- version: 3.0.6
28
+ version: 3.1.0
29
29
  - - "<"
30
30
  - !ruby/object:Gem::Version
31
- version: '3.1'
31
+ version: '3.2'
32
32
  - !ruby/object:Gem::Dependency
33
33
  name: fiddle
34
34
  requirement: !ruby/object:Gem::Requirement
@@ -149,7 +149,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
149
149
  - !ruby/object:Gem::Version
150
150
  version: '0'
151
151
  requirements: []
152
- rubygems_version: 4.0.8
152
+ rubygems_version: 4.0.17
153
153
  specification_version: 4
154
154
  summary: JIT compilation of CArray kernels written in Ruby
155
155
  test_files: []