carray-jit 0.1.7 → 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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +69 -0
- data/README.md +3 -7
- data/carray-jit.gemspec +8 -2
- data/docs/01_GettingStarted.md +1 -1
- data/docs/02_KernelShapes.md +31 -15
- data/docs/03_SupportedFeatures.md +30 -29
- data/docs/04_Compiling.md +6 -3
- data/docs/05_DesignNotes.md +1 -3
- data/docs/06_Cheatsheet.md +31 -21
- data/docs/07_StepByStep.ja.md +45 -45
- data/docs/07_StepByStep.md +45 -45
- data/docs/08_LanguageReference.md +2 -1
- data/examples/applications/alarm.rb +2 -2
- data/examples/applications/cubic_spline.rb +20 -20
- data/examples/applications/dithering.rb +8 -8
- data/examples/applications/game_of_life.rb +1 -1
- data/examples/applications/group_stats.rb +4 -4
- data/examples/applications/heat_equation.rb +6 -6
- data/examples/applications/kepler.rb +10 -10
- data/examples/applications/lookup.rb +3 -3
- data/examples/applications/median_filter.rb +7 -7
- data/examples/applications/moving_average.rb +6 -6
- data/examples/applications/parcel_ascent.rb +12 -12
- data/examples/applications/partial_sums.rb +6 -6
- data/examples/applications/point_cloud.rb +4 -4
- data/examples/applications/point_in_polygon.rb +4 -4
- data/examples/applications/quicksort.rb +1 -1
- data/examples/applications/relaxation.rb +1 -1
- data/examples/applications/sensor_gaps.rb +3 -3
- data/examples/applications/sobel_edges.rb +6 -6
- data/examples/applications/van_der_pol.rb +3 -3
- data/examples/applications/wet_bulb.rb +5 -5
- data/examples/features/01_element_wise.rb +8 -8
- data/examples/features/02_stencil.rb +4 -4
- data/examples/features/03_recurrence.rb +1 -1
- data/examples/features/04_thomas.rb +8 -8
- data/examples/features/05_reduction.rb +8 -8
- data/examples/features/06_jit_contract.rb +10 -10
- data/examples/features/07_masks.rb +4 -4
- data/examples/features/08_views.rb +6 -6
- data/examples/features/09_inspecting.rb +2 -2
- data/examples/features/10_complex.rb +7 -7
- data/examples/features/11_c_functions.rb +17 -17
- data/examples/features/12_sweep.rb +20 -25
- data/examples/features/13_cscalar.rb +12 -12
- data/examples/features/14_stencil_window.rb +6 -6
- data/examples/features/15_loops.rb +6 -6
- data/examples/features/16_raising.rb +4 -4
- data/lib/carray/jit/analyzer.rb +47 -33
- data/lib/carray/jit/c_function.rb +80 -14
- data/lib/carray/jit/c_generator.rb +14 -2
- data/lib/carray/jit/call.rb +1 -1
- data/lib/carray/jit/compiler.rb +71 -21
- data/lib/carray/jit/errors.rb +22 -0
- data/lib/carray/jit/expression.rb +10 -4
- data/lib/carray/jit/kernel.rb +5 -6
- data/lib/carray/jit/native.rb +22 -0
- data/lib/carray/jit/node.rb +1 -1
- data/lib/carray/jit/type_assignment.rb +9 -4
- data/lib/carray/jit/vector_math.rb +111 -0
- data/lib/carray/jit/version.rb +1 -1
- data/lib/carray/jit.rb +168 -94
- metadata +148 -123
data/lib/carray/jit/compiler.rb
CHANGED
|
@@ -4,6 +4,7 @@ require "rbconfig"
|
|
|
4
4
|
require "securerandom"
|
|
5
5
|
require "tmpdir"
|
|
6
6
|
require "carray/jit/native"
|
|
7
|
+
require "carray/jit/vector_math"
|
|
7
8
|
require "monitor"
|
|
8
9
|
|
|
9
10
|
class CArray
|
|
@@ -71,7 +72,40 @@ class CArray
|
|
|
71
72
|
# as the documentation promises. Without it the compiler is free to
|
|
72
73
|
# assume a sum cannot overflow, so one kernel contradicts itself:
|
|
73
74
|
# `y = -a; y > 0 ? y : 0` stored a negative value for a = INT64_MIN.
|
|
74
|
-
|
|
75
|
+
#
|
|
76
|
+
# -fno-math-errno says that no one reads `errno` after a math function,
|
|
77
|
+
# which is true of every kernel: a kernel reports a failure through its
|
|
78
|
+
# error slot. gcc keeps a branch for `errno` in a loop that calls `sqrt`
|
|
79
|
+
# without it, and a loop with a branch is not vectorised; clang on macOS
|
|
80
|
+
# assumes this already. The values computed are the same either way.
|
|
81
|
+
#
|
|
82
|
+
# -march=native lets the compiler use every instruction set of the
|
|
83
|
+
# machine it runs on -- AVX2 or AVX-512 on x86_64 -- where the baseline
|
|
84
|
+
# is the oldest CPU of the architecture. A kernel is compiled on the
|
|
85
|
+
# machine that runs it, so the two CPUs are the same one. It follows
|
|
86
|
+
# CArray: a CArray built without the flag (`CARRAY_MARCH_NATIVE=0`, or
|
|
87
|
+
# `--disable-march-native`) is one whose owner chose not to depend on
|
|
88
|
+
# the build machine, and nothing here is compiled with it either.
|
|
89
|
+
#
|
|
90
|
+
# `CARRAY_MARCH_NATIVE=0` is the same variable CArray reads when it is
|
|
91
|
+
# built. This gem compiles when the program runs, so it reads the
|
|
92
|
+
# variable then: with `0`, a CArray that was built with the flag is
|
|
93
|
+
# still joined by objects compiled without it. Expression::FLAGS reads
|
|
94
|
+
# this constant too, so the kernels, the C functions and the expression
|
|
95
|
+
# evaluator all agree.
|
|
96
|
+
#
|
|
97
|
+
# -ffp-contract=off above still keeps a multiply and an add from being
|
|
98
|
+
# fused where the wider target has an instruction for it. What the flag
|
|
99
|
+
# selected is part of the cache key (see #target_identity), which costs
|
|
100
|
+
# one run of the compiler per process.
|
|
101
|
+
MARCH_NATIVE =
|
|
102
|
+
(ENV["CARRAY_MARCH_NATIVE"] != "0" &&
|
|
103
|
+
CArray.const_defined?(:BUILD_FLAGS) &&
|
|
104
|
+
CArray::BUILD_FLAGS.split.include?("-march=native") ?
|
|
105
|
+
["-march=native"] : []).freeze
|
|
106
|
+
|
|
107
|
+
FLAGS = ["-O3", "-fPIC", "-shared", "-ffp-contract=off", "-fwrapv",
|
|
108
|
+
"-fno-math-errno", *MARCH_NATIVE, *SYMBOLIC].freeze
|
|
75
109
|
|
|
76
110
|
# A compiled body calls a function borrowed with `jit_extern` by name,
|
|
77
111
|
# and the library it came from is one the process opened -- which is
|
|
@@ -153,26 +187,43 @@ class CArray
|
|
|
153
187
|
# CArray's own version is defined by its extension, and this may be
|
|
154
188
|
# running without it: the command that looks after the cache loads the
|
|
155
189
|
# compiler and nothing else, on purpose, so that a cache can be
|
|
156
|
-
# inspected or cleared when CArray itself will not load.
|
|
157
|
-
#
|
|
158
|
-
#
|
|
159
|
-
#
|
|
160
|
-
#
|
|
190
|
+
# inspected or cleared when CArray itself will not load. What it has
|
|
191
|
+
# to name is the version a `require "carray"` here would load, without
|
|
192
|
+
# doing the require.
|
|
193
|
+
#
|
|
194
|
+
# A checkout put on the load path with `-I` is found the way the
|
|
195
|
+
# require would find it, and its version read from the `version.h`
|
|
196
|
+
# beside the built extension. Otherwise RubyGems knows: the version
|
|
197
|
+
# already activated, as under Bundler, or else the one a `require`
|
|
198
|
+
# would activate.
|
|
161
199
|
def carray_version
|
|
162
200
|
return CArray::VERSION if defined?(CArray::VERSION)
|
|
163
201
|
return @carray_version if defined?(@carray_version)
|
|
164
|
-
@carray_version =
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
202
|
+
@carray_version = checkout_carray_version || installed_carray_version
|
|
203
|
+
return @carray_version
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
# An installed gem keeps `version.h` in `ext/` and the extension in
|
|
207
|
+
# `lib/`, so only a checkout has the two side by side.
|
|
208
|
+
def checkout_carray_version
|
|
209
|
+
extension = "carray_ext.#{RbConfig::CONFIG['DLEXT']}"
|
|
210
|
+
directory = $LOAD_PATH.map { |path| File.expand_path(path.to_s) }
|
|
211
|
+
.find { |path| File.file?(File.join(path, extension)) }
|
|
212
|
+
return nil unless directory
|
|
213
|
+
header = File.join(directory, "version.h")
|
|
214
|
+
return nil unless File.file?(header)
|
|
215
|
+
return File.read(header)[/^#define\s+CA_VERSION\s+"([^"]+)"/, 1]
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
def installed_carray_version
|
|
219
|
+
return "none" unless defined?(Gem::Specification)
|
|
220
|
+
loaded = Gem.loaded_specs["carray"]
|
|
221
|
+
return loaded.version.to_s if loaded
|
|
222
|
+
return Gem::Specification.find_by_name("carray").version.to_s
|
|
223
|
+
rescue StandardError, LoadError
|
|
224
|
+
# A gem that is not installed raises Gem::MissingSpecError, which
|
|
225
|
+
# is a LoadError rather than a StandardError.
|
|
226
|
+
return "none"
|
|
176
227
|
end
|
|
177
228
|
|
|
178
229
|
# Directories for versions and architectures no longer in use, so
|
|
@@ -345,8 +396,7 @@ class CArray
|
|
|
345
396
|
# sets together, so one probe answers all of it.
|
|
346
397
|
#
|
|
347
398
|
# Costs a compiler run, so it is only paid when a flag actually says
|
|
348
|
-
# `native
|
|
349
|
-
# and then once per process for each set of flags.
|
|
399
|
+
# `native`, and then once per process for each set of flags.
|
|
350
400
|
def target_identity (flags)
|
|
351
401
|
native = flags.select { |flag| flag =~ NATIVE_FLAGS }
|
|
352
402
|
return "" if native.empty?
|
|
@@ -579,7 +629,7 @@ class CArray
|
|
|
579
629
|
"the one Ruby was built with."
|
|
580
630
|
end
|
|
581
631
|
command = [compiler_command, *flags, source_path, "-o", object_path,
|
|
582
|
-
"-lm", *LINK]
|
|
632
|
+
"-lm", *VectorMath.link_flags, *LINK]
|
|
583
633
|
output = IO.popen(command, err: [:child, :out]) { |io| io.read }
|
|
584
634
|
unless $?.success?
|
|
585
635
|
raise CompilationError, "#{command.join(' ')}\n#{output}"
|
data/lib/carray/jit/errors.rb
CHANGED
|
@@ -33,5 +33,27 @@ class CArray
|
|
|
33
33
|
class CompilationError < Error
|
|
34
34
|
end
|
|
35
35
|
|
|
36
|
+
# Every entry that compiles or calls reaches C through the layer in
|
|
37
|
+
# Native, and that layer makes no call from a Ractor other than the main
|
|
38
|
+
# one. Without this the first thing to fail was a write to this module's
|
|
39
|
+
# own state, and the error named that instead of the reason. It stands
|
|
40
|
+
# here because `jit_call` is loaded without the rest of the compiler.
|
|
41
|
+
def self.refuse_outside_the_main_ractor
|
|
42
|
+
return if Ractor.current == Ractor.main
|
|
43
|
+
raise Unsupported,
|
|
44
|
+
"carray-jit compiles and calls a kernel in the main Ractor only; " \
|
|
45
|
+
"the layer it calls C through makes no call from another Ractor. " \
|
|
46
|
+
"`CArray.fuse` works in every Ractor, without the compiler"
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# What Ruby says when `clamp` is given its bounds the wrong way round. A
|
|
50
|
+
# kernel raises with Ruby's class and Ruby's words, and the words differ
|
|
51
|
+
# between versions of Ruby, so they are asked of the Ruby that is running.
|
|
52
|
+
CLAMP_BOUNDS_MESSAGE = begin
|
|
53
|
+
1.clamp(1, 0)
|
|
54
|
+
rescue ArgumentError => error
|
|
55
|
+
error.message.freeze
|
|
56
|
+
end
|
|
57
|
+
|
|
36
58
|
end
|
|
37
59
|
end
|
|
@@ -49,7 +49,13 @@ class CArray
|
|
|
49
49
|
# statements: on x86 with -march=native, `a * b + c` lost a rounding in
|
|
50
50
|
# 3296 of 30000 cells. -ffp-contract=on keeps a contraction inside a
|
|
51
51
|
# node's own body, as CArray's kernel has it, and none between nodes.
|
|
52
|
-
|
|
52
|
+
#
|
|
53
|
+
# -march=native is the one flag of CArray's that is not always taken:
|
|
54
|
+
# `CARRAY_MARCH_NATIVE=0` leaves it out of everything this gem compiles
|
|
55
|
+
# (see Compiler::MARCH_NATIVE).
|
|
56
|
+
FLAGS = ["-fPIC", "-shared",
|
|
57
|
+
*(CArray::BUILD_FLAGS.split - ["-march=native"]),
|
|
58
|
+
*Compiler::MARCH_NATIVE,
|
|
53
59
|
"-ffp-contract=on", *DYNAMIC_LOOKUP].freeze
|
|
54
60
|
|
|
55
61
|
C_TYPES = {
|
|
@@ -74,14 +80,14 @@ class CArray
|
|
|
74
80
|
# A shifted read reaches cells other than the one being written, so
|
|
75
81
|
# over its own output it would read what it has just written.
|
|
76
82
|
return false if aliased && shifted_leaves(plan).any? { |i| plan.leaves[i].equal?(out) }
|
|
77
|
-
return false unless out.
|
|
83
|
+
return false unless out.shape == plan.dim
|
|
78
84
|
arrays = [out, *plan.leaves]
|
|
79
85
|
writable = [true, *Array.new(plan.leaves.size, false)]
|
|
80
86
|
Access.open(arrays, writable) do |bases|
|
|
81
87
|
strided = strided_slots(arrays, bases) or return false
|
|
82
88
|
kernel = kernel_for(plan, aliased, strided) or return false
|
|
83
89
|
# Held here so that the buffers outlive the call.
|
|
84
|
-
dim = out.
|
|
90
|
+
dim = out.shape.pack("q*")
|
|
85
91
|
steps = strided.flat_map { |slot| slot_strides(bases, slot) }.pack("q*")
|
|
86
92
|
constants = packed_constants(plan) or return false
|
|
87
93
|
args = [out.elements]
|
|
@@ -240,7 +246,7 @@ class CArray
|
|
|
240
246
|
def strided_slots (arrays, bases)
|
|
241
247
|
slots = []
|
|
242
248
|
bases.each_with_index do |basis, i|
|
|
243
|
-
return nil unless basis[:dim] == arrays[0].
|
|
249
|
+
return nil unless basis[:dim] == arrays[0].shape
|
|
244
250
|
data, mask = i == 0 ? ["out", "out_mask"] : ["a#{i - 1}", "k#{i - 1}"]
|
|
245
251
|
if i == 0
|
|
246
252
|
return nil unless one_to_one?(basis[:strides], basis[:dim], basis[:bytes])
|
data/lib/carray/jit/kernel.rb
CHANGED
|
@@ -327,7 +327,7 @@ class CArray
|
|
|
327
327
|
shapes = []
|
|
328
328
|
arrays.each do |array|
|
|
329
329
|
return nil unless array.is_a?(CArray)
|
|
330
|
-
shapes << array.data_type << array.
|
|
330
|
+
shapes << array.data_type << array.shape
|
|
331
331
|
end
|
|
332
332
|
[shapes, bounds, border,
|
|
333
333
|
@shape_scalar_names.map { |name| scalars[name] }]
|
|
@@ -388,8 +388,7 @@ class CArray
|
|
|
388
388
|
when 1 then raise ZeroDivisionError, "divided by 0"
|
|
389
389
|
when 2 then raise IndexError, "index out of range"
|
|
390
390
|
when 3 then raise ArgumentError,
|
|
391
|
-
|
|
392
|
-
"max argument"
|
|
391
|
+
CLAMP_BOUNDS_MESSAGE
|
|
393
392
|
# Ruby's own wording names the value it could not compare, and the
|
|
394
393
|
# slot carries a code rather than a number, so the reason is named
|
|
395
394
|
# instead. The class and the failure are Ruby's.
|
|
@@ -518,7 +517,7 @@ class CArray
|
|
|
518
517
|
|
|
519
518
|
def packed_extents (array_values)
|
|
520
519
|
@extent_slots.map { |name, axis|
|
|
521
|
-
array_values.fetch(name).
|
|
520
|
+
array_values.fetch(name).shape[axis]
|
|
522
521
|
}.pack("q*")
|
|
523
522
|
end
|
|
524
523
|
|
|
@@ -577,7 +576,7 @@ class CArray
|
|
|
577
576
|
# would otherwise have written by hand.
|
|
578
577
|
if @dynamic_arrays.include?(name)
|
|
579
578
|
starts << Array.new(array_rank(name), 0)
|
|
580
|
-
counts << array_values.fetch(name).
|
|
579
|
+
counts << array_values.fetch(name).shape.dup
|
|
581
580
|
next
|
|
582
581
|
end
|
|
583
582
|
@axis_uses.fetch(name).each do |walkers, pinned|
|
|
@@ -799,7 +798,7 @@ class CArray
|
|
|
799
798
|
# before anything runs rather than read -- or written -- past its end.
|
|
800
799
|
def verify_bounds (name, array, ranges, scalars)
|
|
801
800
|
@axis_uses.fetch(name).each_with_index do |(walkers, pinned), axis|
|
|
802
|
-
extent = array.
|
|
801
|
+
extent = array.shape[axis]
|
|
803
802
|
|
|
804
803
|
pinned_positions(pinned, scalars).each do |position|
|
|
805
804
|
next if position >= 0 && position < extent
|
data/lib/carray/jit/native.rb
CHANGED
|
@@ -61,6 +61,28 @@ class CArray
|
|
|
61
61
|
|
|
62
62
|
POINTER_SIZE = Fiddle::SIZEOF_VOIDP
|
|
63
63
|
|
|
64
|
+
# The values each integer type holds on this machine, by the names above.
|
|
65
|
+
INTEGER_RANGES = {
|
|
66
|
+
char: [true, Fiddle::SIZEOF_CHAR],
|
|
67
|
+
uchar: [false, Fiddle::SIZEOF_CHAR],
|
|
68
|
+
short: [true, Fiddle::SIZEOF_SHORT],
|
|
69
|
+
ushort: [false, Fiddle::SIZEOF_SHORT],
|
|
70
|
+
int: [true, Fiddle::SIZEOF_INT],
|
|
71
|
+
uint: [false, Fiddle::SIZEOF_INT],
|
|
72
|
+
long: [true, Fiddle::SIZEOF_LONG],
|
|
73
|
+
ulong: [false, Fiddle::SIZEOF_LONG],
|
|
74
|
+
long_long: [true, Fiddle::SIZEOF_LONG_LONG],
|
|
75
|
+
ulong_long: [false, Fiddle::SIZEOF_LONG_LONG],
|
|
76
|
+
}.to_h { |name, (signed, bytes)|
|
|
77
|
+
bits = bytes * 8
|
|
78
|
+
[name, signed ? (-(1 << (bits - 1))..(1 << (bits - 1)) - 1) : (0..(1 << bits) - 1)]
|
|
79
|
+
}.freeze
|
|
80
|
+
|
|
81
|
+
# True for an object that is an address the caller made on purpose.
|
|
82
|
+
def self.address? (object)
|
|
83
|
+
object.is_a?(Fiddle::Pointer)
|
|
84
|
+
end
|
|
85
|
+
|
|
64
86
|
# What a library is, for a caller that was handed one. `jit_extern`'s
|
|
65
87
|
# `from:` takes one of these as it stands.
|
|
66
88
|
Library = Fiddle::Handle
|
data/lib/carray/jit/node.rb
CHANGED
|
@@ -691,7 +691,7 @@ class CArray
|
|
|
691
691
|
end
|
|
692
692
|
end
|
|
693
693
|
|
|
694
|
-
# `w = CArray.
|
|
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
|
|
873
|
-
#
|
|
874
|
-
#
|
|
875
|
-
#
|
|
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)
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
require "rbconfig"
|
|
2
|
+
require "carray/jit/native"
|
|
3
|
+
|
|
4
|
+
class CArray
|
|
5
|
+
module JIT
|
|
6
|
+
|
|
7
|
+
# The vector forms of the math.h functions, where the C library has them.
|
|
8
|
+
#
|
|
9
|
+
# glibc ships `libmvec`, which computes `exp`, `sin` and the like for
|
|
10
|
+
# several values in one call. gcc calls it from a loop when the function
|
|
11
|
+
# is declared with the `simd` attribute, and no other flag is needed: the
|
|
12
|
+
# declaration says a vector form exists, and the rounding rules, NaN and
|
|
13
|
+
# infinity are what they were. A loop that calls a math function is then
|
|
14
|
+
# vectorised like any other, where it stayed scalar before.
|
|
15
|
+
#
|
|
16
|
+
# The answers are not the scalar function's. A vector form is correct to
|
|
17
|
+
# within a few units in the last place (4 was the largest measured, on
|
|
18
|
+
# `cos`), and the cells a vector does not cover at the end of a loop are
|
|
19
|
+
# computed by the scalar function, so one input can give two last bits
|
|
20
|
+
# depending on where in the array it sits. That is why this belongs to
|
|
21
|
+
# `fastmath`, and why `fastmath: false` leaves it out.
|
|
22
|
+
#
|
|
23
|
+
# Only where it was measured: Linux on x86_64 with glibc. Everywhere
|
|
24
|
+
# else #functions is empty, a kernel is generated as it always was, and
|
|
25
|
+
# nothing here costs anything.
|
|
26
|
+
module VectorMath
|
|
27
|
+
|
|
28
|
+
LIBRARY = "libmvec.so.1"
|
|
29
|
+
|
|
30
|
+
# What a kernel can call, by its C name, with how many arguments it
|
|
31
|
+
# takes. `sqrt` is not here because it is an instruction, not a call.
|
|
32
|
+
CANDIDATES = {
|
|
33
|
+
"exp" => 1, "exp2" => 1, "log" => 1, "log2" => 1, "log10" => 1,
|
|
34
|
+
"sin" => 1, "cos" => 1, "tan" => 1,
|
|
35
|
+
"asin" => 1, "acos" => 1, "atan" => 1,
|
|
36
|
+
"sinh" => 1, "cosh" => 1, "tanh" => 1,
|
|
37
|
+
"asinh" => 1, "acosh" => 1, "atanh" => 1,
|
|
38
|
+
"cbrt" => 1, "erf" => 1, "erfc" => 1,
|
|
39
|
+
"pow" => 2, "atan2" => 2, "hypot" => 2,
|
|
40
|
+
}.freeze
|
|
41
|
+
|
|
42
|
+
# The names the x86_64 vector ABI gives one function: a letter for the
|
|
43
|
+
# instruction set, and how many values a vector of it holds, as doubles
|
|
44
|
+
# and as floats. gcc picks among them by the target, and a declaration
|
|
45
|
+
# promises all of them, so a function is declared only where the
|
|
46
|
+
# library has all four.
|
|
47
|
+
VARIANTS = { "b" => [2, 4], "c" => [4, 8], "d" => [4, 8], "e" => [8, 16] }.freeze
|
|
48
|
+
|
|
49
|
+
class << self
|
|
50
|
+
|
|
51
|
+
# The functions that have a vector form here: `[c_name, arity, type]`
|
|
52
|
+
# each, `type` being "double" or "float". Asked of the library once
|
|
53
|
+
# per process, and empty where there is no such library.
|
|
54
|
+
def functions
|
|
55
|
+
@functions ||= find_functions.freeze
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def available?
|
|
59
|
+
!functions.empty?
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# What a generated file declares, after `<math.h>`. A second
|
|
63
|
+
# declaration of a math.h function is allowed, and the attributes of
|
|
64
|
+
# the two are merged.
|
|
65
|
+
def declarations
|
|
66
|
+
@declarations ||= functions.map { |name, arity, type|
|
|
67
|
+
"__attribute__((simd(\"notinbranch\"), const)) " \
|
|
68
|
+
"#{type} #{name}(#{([type] * arity).join(', ')});\n"
|
|
69
|
+
}.join.freeze
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# The object has to name the library it calls into. Without this the
|
|
73
|
+
# object still loads, the loader resolving names when they are first
|
|
74
|
+
# called, and the process ends at the first vector call. A linker
|
|
75
|
+
# leaves the library out of an object that calls nothing in it, so
|
|
76
|
+
# the flag is given for every object.
|
|
77
|
+
def link_flags
|
|
78
|
+
available? ? ["-lmvec"] : []
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
private
|
|
82
|
+
|
|
83
|
+
def find_functions
|
|
84
|
+
return [] unless RbConfig::CONFIG["host_os"] =~ /linux/ &&
|
|
85
|
+
RbConfig::CONFIG["host_cpu"] == "x86_64"
|
|
86
|
+
library = begin
|
|
87
|
+
Native.open_library(LIBRARY, global: true)
|
|
88
|
+
rescue Native::LinkError
|
|
89
|
+
return []
|
|
90
|
+
end
|
|
91
|
+
CANDIDATES.flat_map { |name, arity|
|
|
92
|
+
[[name, arity, "double", 0], ["#{name}f", arity, "float", 1]]
|
|
93
|
+
}.select { |name, arity, _, width|
|
|
94
|
+
VARIANTS.all? { |letter, lanes|
|
|
95
|
+
symbol?(library, "_ZGV#{letter}N#{lanes[width]}#{'v' * arity}_#{name}")
|
|
96
|
+
}
|
|
97
|
+
}.map { |name, arity, type, _| [name, arity, type] }
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def symbol? (library, name)
|
|
101
|
+
Native.address(library, name) ? true : false
|
|
102
|
+
rescue Native::LinkError
|
|
103
|
+
false
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
end
|
|
111
|
+
end
|
data/lib/carray/jit/version.rb
CHANGED