carray-jit 0.1.6 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +91 -0
- data/README.md +3 -7
- data/carray-jit.gemspec +11 -3
- data/docs/01_GettingStarted.md +1 -1
- data/docs/02_KernelShapes.md +33 -17
- data/docs/03_SupportedFeatures.md +31 -30
- data/docs/04_Compiling.md +10 -7
- 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 +48 -34
- data/lib/carray/jit/c_function.rb +213 -153
- data/lib/carray/jit/c_generator.rb +33 -12
- data/lib/carray/jit/call.rb +1 -1
- data/lib/carray/jit/compiler.rb +115 -47
- data/lib/carray/jit/errors.rb +22 -0
- data/lib/carray/jit/expression.rb +78 -32
- data/lib/carray/jit/kernel.rb +28 -53
- data/lib/carray/jit/native.rb +171 -0
- data/lib/carray/jit/node.rb +1 -1
- data/lib/carray/jit/sweep.rb +14 -64
- 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 +257 -106
- metadata +148 -122
data/lib/carray/jit/kernel.rb
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
require "
|
|
1
|
+
require "carray/jit/native"
|
|
2
2
|
require "carray/jit/sweep"
|
|
3
3
|
|
|
4
4
|
class CArray
|
|
@@ -7,8 +7,9 @@ class CArray
|
|
|
7
7
|
# A compiled kernel, bound to one set of array data types and one set of
|
|
8
8
|
# scalar types.
|
|
9
9
|
#
|
|
10
|
-
# Every kernel has the same C signature, so the
|
|
11
|
-
# fixed and the per-kernel detail travels in the six
|
|
10
|
+
# Every kernel has the same C signature, so the shape of the function
|
|
11
|
+
# Native makes is fixed and the per-kernel detail travels in the six
|
|
12
|
+
# buffers.
|
|
12
13
|
class CompiledKernel
|
|
13
14
|
|
|
14
15
|
# @!attribute [r] source
|
|
@@ -54,6 +55,12 @@ class CArray
|
|
|
54
55
|
@rank = analyzer.rank
|
|
55
56
|
@index_names = analyzer.index_names
|
|
56
57
|
@written_arrays = analyzer.written_arrays
|
|
58
|
+
# Which operands the sweep writes, in CArray's spelling: one character
|
|
59
|
+
# per operand. Decided by the body, so spelled once rather than on
|
|
60
|
+
# every call.
|
|
61
|
+
@fsync = @arrays.map { |name|
|
|
62
|
+
@written_arrays.include?(name) ? "1" : "0"
|
|
63
|
+
}.join.freeze
|
|
57
64
|
@array_ranks = analyzer.array_ranks
|
|
58
65
|
@inner_ranges = analyzer.inner_ranges
|
|
59
66
|
@unbounded_indices = analyzer.unbounded_indices
|
|
@@ -126,9 +133,8 @@ class CArray
|
|
|
126
133
|
@handle, @compiled = Compiler.build(generator.generate_slab(SLAB_NAME),
|
|
127
134
|
CGenerator::FUNCTION_NAME,
|
|
128
135
|
header: generator.provenance)
|
|
129
|
-
@function =
|
|
130
|
-
|
|
131
|
-
Fiddle::TYPE_VOID)
|
|
136
|
+
@function = Native.function(Native.address(@handle, CGenerator::FUNCTION_NAME),
|
|
137
|
+
[:pointer] * 10, :void, release_gvl: true)
|
|
132
138
|
end
|
|
133
139
|
|
|
134
140
|
# `bounds` is one [start, limit, step] per axis.
|
|
@@ -209,9 +215,6 @@ class CArray
|
|
|
209
215
|
# arguments the kernel's own loop takes are the ones a chunk arrives as.
|
|
210
216
|
def sweep (array_values, scalar_values, c_function_values = {})
|
|
211
217
|
arrays = @arrays.map { |name| array_values.fetch(name) }
|
|
212
|
-
fsync = @arrays.map { |name|
|
|
213
|
-
@written_arrays.include?(name) ? "1" : "0"
|
|
214
|
-
}.join
|
|
215
218
|
|
|
216
219
|
error = [0].pack("l")
|
|
217
220
|
reals = packed_reals(scalar_values)
|
|
@@ -233,12 +236,10 @@ class CArray
|
|
|
233
236
|
# signature has the slots.
|
|
234
237
|
masks = ("\0" * 8)
|
|
235
238
|
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
slots.last[0, error.bytesize]
|
|
241
|
-
}
|
|
239
|
+
# The slab writes the error slot in `error` itself.
|
|
240
|
+
Sweep.run(slab_pointer, @fsync, arrays,
|
|
241
|
+
buffer(reals), buffer(integers), buffer(functions),
|
|
242
|
+
buffer(data), masks, masks, error)
|
|
242
243
|
|
|
243
244
|
packed.each_with_index do |(array, item), index|
|
|
244
245
|
next if array.equal?(item)
|
|
@@ -326,13 +327,13 @@ class CArray
|
|
|
326
327
|
shapes = []
|
|
327
328
|
arrays.each do |array|
|
|
328
329
|
return nil unless array.is_a?(CArray)
|
|
329
|
-
shapes << array.data_type << array.
|
|
330
|
+
shapes << array.data_type << array.shape
|
|
330
331
|
end
|
|
331
332
|
[shapes, bounds, border,
|
|
332
333
|
@shape_scalar_names.map { |name| scalars[name] }]
|
|
333
334
|
end
|
|
334
335
|
|
|
335
|
-
# A kernel is shared, and
|
|
336
|
+
# A kernel is shared, and its call lets go of the GVL, so two
|
|
336
337
|
# threads can be reading one plan while a third prepares another. That
|
|
337
338
|
# is sound only while nothing in a plan is written after it is published
|
|
338
339
|
# -- and a box is arrays inside an array, so freezing the top of it
|
|
@@ -387,8 +388,7 @@ class CArray
|
|
|
387
388
|
when 1 then raise ZeroDivisionError, "divided by 0"
|
|
388
389
|
when 2 then raise IndexError, "index out of range"
|
|
389
390
|
when 3 then raise ArgumentError,
|
|
390
|
-
|
|
391
|
-
"max argument"
|
|
391
|
+
CLAMP_BOUNDS_MESSAGE
|
|
392
392
|
# Ruby's own wording names the value it could not compare, and the
|
|
393
393
|
# slot carries a code rather than a number, so the reason is named
|
|
394
394
|
# instead. The class and the failure are Ruby's.
|
|
@@ -456,7 +456,7 @@ class CArray
|
|
|
456
456
|
end
|
|
457
457
|
|
|
458
458
|
def slab_pointer
|
|
459
|
-
@slab_pointer ||= @handle
|
|
459
|
+
@slab_pointer ||= Native.address(@handle, SLAB_NAME)
|
|
460
460
|
end
|
|
461
461
|
|
|
462
462
|
# Resolved when the frame is first walked rather than at build time: a
|
|
@@ -464,8 +464,8 @@ class CArray
|
|
|
464
464
|
# runs its interior first.
|
|
465
465
|
def border_function
|
|
466
466
|
@border_function ||=
|
|
467
|
-
|
|
468
|
-
|
|
467
|
+
Native.function(Native.address(@handle, CGenerator::BORDER_NAME),
|
|
468
|
+
[:pointer] * 10, :void, release_gvl: true)
|
|
469
469
|
end
|
|
470
470
|
|
|
471
471
|
# What a C function may be handed as a pointer, and what has to be true
|
|
@@ -517,13 +517,13 @@ class CArray
|
|
|
517
517
|
|
|
518
518
|
def packed_extents (array_values)
|
|
519
519
|
@extent_slots.map { |name, axis|
|
|
520
|
-
array_values.fetch(name).
|
|
520
|
+
array_values.fetch(name).shape[axis]
|
|
521
521
|
}.pack("q*")
|
|
522
522
|
end
|
|
523
523
|
|
|
524
524
|
def address_buffer (name, array)
|
|
525
525
|
@address_parameters.fetch(name).each do |parameter|
|
|
526
|
-
wanted = CDeclaration::DATA_TYPES.fetch(parameter.element.
|
|
526
|
+
wanted = CDeclaration::DATA_TYPES.fetch(parameter.element.abi)
|
|
527
527
|
unless array.data_type_name == wanted.to_s
|
|
528
528
|
raise Unsupported,
|
|
529
529
|
"`#{name}` is handed to a C function as `#{parameter.text}`, " \
|
|
@@ -553,37 +553,12 @@ class CArray
|
|
|
553
553
|
Access.classify(array)[:entity] ? array : array.copy
|
|
554
554
|
end
|
|
555
555
|
|
|
556
|
-
# A pointer into an empty string is not something
|
|
557
|
-
# and a kernel with no scalars has an empty buffer.
|
|
556
|
+
# A pointer into an empty string is not something Native is asked to
|
|
557
|
+
# hand over, and a kernel with no scalars has an empty buffer.
|
|
558
558
|
def buffer (packed)
|
|
559
559
|
packed.empty? ? "\0" * 8 : packed
|
|
560
560
|
end
|
|
561
561
|
|
|
562
|
-
# Copies `parts` into one block of C memory, each part 8-byte aligned,
|
|
563
|
-
# after a table of their addresses, and yields a pointer to the table
|
|
564
|
-
# and a pointer to each part; the block is freed when this returns.
|
|
565
|
-
#
|
|
566
|
-
# For a call that is handed addresses inside other memory rather than
|
|
567
|
-
# arguments: a Ruby String's bytes may be moved by GC compaction, and
|
|
568
|
-
# an address taken from one and packed elsewhere is not one Ruby knows
|
|
569
|
-
# to keep in place. Memory from malloc is never moved.
|
|
570
|
-
def in_one_block (parts)
|
|
571
|
-
table = parts.size * Fiddle::SIZEOF_VOIDP
|
|
572
|
-
offsets = parts.each_with_object([]) { |part, list|
|
|
573
|
-
last = list.empty? ? table : list.last + parts[list.size - 1].bytesize
|
|
574
|
-
list << ((last + 7) & ~7)
|
|
575
|
-
}
|
|
576
|
-
size = offsets.last + parts.last.bytesize
|
|
577
|
-
Fiddle::Pointer.malloc(size, Fiddle::RUBY_FREE) do |block|
|
|
578
|
-
slots = parts.zip(offsets).map { |part, offset|
|
|
579
|
-
block[offset, part.bytesize] = part
|
|
580
|
-
block + offset
|
|
581
|
-
}
|
|
582
|
-
block[0, table] = slots.map(&:to_i).pack("Q*")
|
|
583
|
-
yield block, slots
|
|
584
|
-
end
|
|
585
|
-
end
|
|
586
|
-
|
|
587
562
|
# The xfer tier transfers a box rather than the whole view. The box
|
|
588
563
|
# is the loop range grown by how far the kernel reaches from the cell it
|
|
589
564
|
# is on -- per array, because two arrays in one kernel need not be read
|
|
@@ -601,7 +576,7 @@ class CArray
|
|
|
601
576
|
# would otherwise have written by hand.
|
|
602
577
|
if @dynamic_arrays.include?(name)
|
|
603
578
|
starts << Array.new(array_rank(name), 0)
|
|
604
|
-
counts << array_values.fetch(name).
|
|
579
|
+
counts << array_values.fetch(name).shape.dup
|
|
605
580
|
next
|
|
606
581
|
end
|
|
607
582
|
@axis_uses.fetch(name).each do |walkers, pinned|
|
|
@@ -823,7 +798,7 @@ class CArray
|
|
|
823
798
|
# before anything runs rather than read -- or written -- past its end.
|
|
824
799
|
def verify_bounds (name, array, ranges, scalars)
|
|
825
800
|
@axis_uses.fetch(name).each_with_index do |(walkers, pinned), axis|
|
|
826
|
-
extent = array.
|
|
801
|
+
extent = array.shape[axis]
|
|
827
802
|
|
|
828
803
|
pinned_positions(pinned, scalars).each do |position|
|
|
829
804
|
next if position >= 0 && position < extent
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
require "fiddle"
|
|
2
|
+
require "fiddle/import"
|
|
3
|
+
require "carray/jit/errors"
|
|
4
|
+
|
|
5
|
+
class CArray
|
|
6
|
+
module JIT
|
|
7
|
+
|
|
8
|
+
# Everything this library asks of the layer that reaches C from Ruby, and
|
|
9
|
+
# the one place that layer is named. The rest of the library says what
|
|
10
|
+
# it wants -- open this object, find this symbol, make a function of
|
|
11
|
+
# these types, read these bytes -- in the words below, and only this
|
|
12
|
+
# file knows that Fiddle is what answers.
|
|
13
|
+
#
|
|
14
|
+
# That is so the layer can be replaced by changing one file. Fiddle does
|
|
15
|
+
# not let a call be made from a Ractor other than the main one, and a
|
|
16
|
+
# layer that does would be a second version of this module rather than a
|
|
17
|
+
# second version of the library (devel/PROPOSAL_FFI_SEAM.md).
|
|
18
|
+
#
|
|
19
|
+
# What is asked:
|
|
20
|
+
#
|
|
21
|
+
# - a shared object opened, and the libraries already loaded searched
|
|
22
|
+
# - a symbol's address found in one
|
|
23
|
+
# - a function made from an address and its types, and called
|
|
24
|
+
# - bytes read and written at an address
|
|
25
|
+
# - a Ruby object handed to C as the VALUE it is
|
|
26
|
+
#
|
|
27
|
+
# A function is called by calling what #function returns: its #call is
|
|
28
|
+
# the layer's own, so nothing stands between a kernel and the C beyond
|
|
29
|
+
# what the layer itself costs. The arguments it takes for a `:pointer`
|
|
30
|
+
# are an Integer address, a String -- whose bytes are what C is handed --
|
|
31
|
+
# or nil.
|
|
32
|
+
# @private
|
|
33
|
+
module Native
|
|
34
|
+
|
|
35
|
+
# The C types a function is made with, by this library's names for
|
|
36
|
+
# them. `:pointer` is any pointer; a function's arguments say what it
|
|
37
|
+
# points at only to the C, which is all that needs to know.
|
|
38
|
+
TYPES = {
|
|
39
|
+
void: Fiddle::TYPE_VOID,
|
|
40
|
+
pointer: Fiddle::TYPE_VOIDP,
|
|
41
|
+
char: Fiddle::TYPE_CHAR,
|
|
42
|
+
uchar: Fiddle::TYPE_UCHAR,
|
|
43
|
+
short: Fiddle::TYPE_SHORT,
|
|
44
|
+
ushort: Fiddle::TYPE_USHORT,
|
|
45
|
+
int: Fiddle::TYPE_INT,
|
|
46
|
+
uint: Fiddle::TYPE_UINT,
|
|
47
|
+
long: Fiddle::TYPE_LONG,
|
|
48
|
+
ulong: Fiddle::TYPE_ULONG,
|
|
49
|
+
long_long: Fiddle::TYPE_LONG_LONG,
|
|
50
|
+
ulong_long: Fiddle::TYPE_ULONG_LONG,
|
|
51
|
+
float: Fiddle::TYPE_FLOAT,
|
|
52
|
+
double: Fiddle::TYPE_DOUBLE,
|
|
53
|
+
}.freeze
|
|
54
|
+
|
|
55
|
+
# The other way round, for what Fiddle's parser answers. Every code it
|
|
56
|
+
# gives for a spelling CDeclaration hands it is one of the fourteen
|
|
57
|
+
# above: `size_t` and the other platform words arrive as the code of
|
|
58
|
+
# whichever of these they are on this machine.
|
|
59
|
+
NAMES = TYPES.invert.freeze
|
|
60
|
+
private_constant :NAMES
|
|
61
|
+
|
|
62
|
+
POINTER_SIZE = Fiddle::SIZEOF_VOIDP
|
|
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
|
+
|
|
86
|
+
# What a library is, for a caller that was handed one. `jit_extern`'s
|
|
87
|
+
# `from:` takes one of these as it stands.
|
|
88
|
+
Library = Fiddle::Handle
|
|
89
|
+
|
|
90
|
+
# Raised when a library will not open or a symbol is not in it.
|
|
91
|
+
LinkError = Fiddle::DLError
|
|
92
|
+
|
|
93
|
+
PARSER = ::Object.new.extend(Fiddle::CParser)
|
|
94
|
+
private_constant :PARSER
|
|
95
|
+
|
|
96
|
+
# Opens a shared object. `global:` is whether its symbols join the
|
|
97
|
+
# process-wide namespace -- RTLD_GLOBAL -- and it is a required word
|
|
98
|
+
# because layers differ on what they do unasked: Fiddle opens globally,
|
|
99
|
+
# and a layer that opened locally would change which definition a name
|
|
100
|
+
# in one generated object resolves to (see Compiler's link flags).
|
|
101
|
+
# Fiddle has no word for the other way, and nothing here asks for it.
|
|
102
|
+
def self.open_library (path, global:)
|
|
103
|
+
raise ArgumentError, "Fiddle opens a library only globally" unless global
|
|
104
|
+
Fiddle::Handle.new(path, Fiddle::RTLD_LAZY | Fiddle::RTLD_GLOBAL)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# What the process has already loaded, searched as one library.
|
|
108
|
+
def self.loaded_libraries
|
|
109
|
+
Fiddle::Handle::DEFAULT
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# The address of `name` in `library`, as an Integer.
|
|
113
|
+
def self.address (library, name)
|
|
114
|
+
library[name]
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
# A function at `address` taking `arguments` and returning `result`,
|
|
118
|
+
# each one of the names in TYPES.
|
|
119
|
+
#
|
|
120
|
+
# `release_gvl:` is whether the GVL is let go for the length of a call,
|
|
121
|
+
# and it is a required word because layers differ on what they do
|
|
122
|
+
# unasked -- Fiddle lets it go, FFI keeps it. Letting it go is right
|
|
123
|
+
# for C that touches no Ruby value, which is every generated kernel; it
|
|
124
|
+
# is wrong for C that does, which is CArray's own sweep.
|
|
125
|
+
def self.function (address, arguments, result, release_gvl:, name: nil)
|
|
126
|
+
types = arguments.map { |type| TYPES.fetch(type) }
|
|
127
|
+
options = name ? { name: name } : {}
|
|
128
|
+
return Fiddle::Function.new(address, types, TYPES.fetch(result), **options) if release_gvl
|
|
129
|
+
begin
|
|
130
|
+
Fiddle::Function.new(address, types, TYPES.fetch(result),
|
|
131
|
+
need_gvl: true, **options)
|
|
132
|
+
rescue ArgumentError
|
|
133
|
+
# A Fiddle too old to be told. It is also too old to have let go
|
|
134
|
+
# of the GVL in the first place -- the keyword and the releasing
|
|
135
|
+
# arrived together -- so what is left here is what that Fiddle
|
|
136
|
+
# would have done anyway.
|
|
137
|
+
Fiddle::Function.new(address, types, TYPES.fetch(result), **options)
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
# The type a C spelling is on this machine, as a name in TYPES, or nil
|
|
142
|
+
# where it is not one. The spelling is one CDeclaration has already
|
|
143
|
+
# put in order: `unsigned long` rather than `long unsigned`, `void *`
|
|
144
|
+
# for any pointer.
|
|
145
|
+
def self.c_type (spelling)
|
|
146
|
+
NAMES.fetch(PARSER.parse_ctype(spelling))
|
|
147
|
+
rescue StandardError
|
|
148
|
+
nil
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
# `size` bytes at `address`, as a String. Fiddle::Pointer.read and
|
|
152
|
+
# .write are not in the fiddle Ruby 3.2 ships (1.1.1).
|
|
153
|
+
def self.read (address, size)
|
|
154
|
+
Fiddle::Pointer.new(address)[0, size]
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
# `bytes` written at `address`.
|
|
158
|
+
def self.write (address, bytes)
|
|
159
|
+
Fiddle::Pointer.new(address)[0, bytes.bytesize] = bytes
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
# `object` as the VALUE C sees -- for CArray's own C, which takes the
|
|
163
|
+
# array itself rather than its memory.
|
|
164
|
+
def self.value (object)
|
|
165
|
+
Fiddle.dlwrap(object)
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
end
|
|
171
|
+
end
|
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
|
data/lib/carray/jit/sweep.rb
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
require "
|
|
1
|
+
require "carray"
|
|
2
2
|
|
|
3
3
|
class CArray
|
|
4
4
|
module JIT
|
|
@@ -19,64 +19,6 @@ class CArray
|
|
|
19
19
|
# as saying it is the form a sweep can drive.
|
|
20
20
|
module Sweep
|
|
21
21
|
|
|
22
|
-
# The sweep is reached by address, the way this library reaches every
|
|
23
|
-
# other C function: CArray's extension is already loaded, so the symbol
|
|
24
|
-
# is there to be found rather than linked against.
|
|
25
|
-
def self.handle
|
|
26
|
-
return @handle if defined?(@handle)
|
|
27
|
-
path = $LOADED_FEATURES.find { |feature|
|
|
28
|
-
File.basename(feature) =~ /\Acarray_ext\.(so|bundle|dylib)\z/
|
|
29
|
-
}
|
|
30
|
-
@handle = path && begin
|
|
31
|
-
Fiddle::Handle.new(path)
|
|
32
|
-
rescue Fiddle::DLError
|
|
33
|
-
nil
|
|
34
|
-
end
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
# True when this CArray is new enough to have the chunked slab family.
|
|
38
|
-
def self.available?
|
|
39
|
-
return @available if defined?(@available)
|
|
40
|
-
@available = !handle.nil? && begin
|
|
41
|
-
handle["ca_call_cslab_1_r"]
|
|
42
|
-
true
|
|
43
|
-
rescue Fiddle::DLError
|
|
44
|
-
false
|
|
45
|
-
end
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
# ca_call_cslab_N_r(func, fsync, rcx0..rcxN-1, userdata)
|
|
49
|
-
#
|
|
50
|
-
# `need_gvl: true` is not a detail. Fiddle releases the GVL for a call
|
|
51
|
-
# unless it is told otherwise, which is right for every other function
|
|
52
|
-
# this library reaches -- a generated kernel is pure C, touches no Ruby
|
|
53
|
-
# value and is the better for running while other threads do. This one
|
|
54
|
-
# is not that: it is CArray's own C, and acquiring the operands attaches
|
|
55
|
-
# them, allocates with `xmalloc`, creates a mask where an output needs
|
|
56
|
-
# one and raises on a shape that will not broadcast. All of that is the
|
|
57
|
-
# Ruby runtime's, and none of it may be done without the GVL. The
|
|
58
|
-
# kernel it calls back into is still pure C, but the GVL is held for the
|
|
59
|
-
# whole call rather than the callback, because Fiddle's choice is made
|
|
60
|
-
# once at the boundary and there is nowhere inside to make it again.
|
|
61
|
-
def self.function (arity)
|
|
62
|
-
@functions ||= {}
|
|
63
|
-
@functions[arity] ||=
|
|
64
|
-
begin
|
|
65
|
-
types = [Fiddle::TYPE_VOIDP] * (arity + 3)
|
|
66
|
-
begin
|
|
67
|
-
Fiddle::Function.new(handle["ca_call_cslab_#{arity}_r"], types,
|
|
68
|
-
Fiddle::TYPE_VOIDP, need_gvl: true)
|
|
69
|
-
rescue ArgumentError
|
|
70
|
-
# A Fiddle too old to be told. It is also too old to have let
|
|
71
|
-
# go of the GVL in the first place -- the keyword and the
|
|
72
|
-
# releasing arrived together -- so what is left here is what
|
|
73
|
-
# that Fiddle would have done anyway.
|
|
74
|
-
Fiddle::Function.new(handle["ca_call_cslab_#{arity}_r"], types,
|
|
75
|
-
Fiddle::TYPE_VOIDP)
|
|
76
|
-
end
|
|
77
|
-
end
|
|
78
|
-
end
|
|
79
|
-
|
|
80
22
|
# The largest arity the family was generated for. Whether a pass can
|
|
81
23
|
# go this way at all is decided in CArray::JIT.sweepable_pass?, before
|
|
82
24
|
# there is a kernel -- because the answer changes what is compiled.
|
|
@@ -84,11 +26,19 @@ class CArray
|
|
|
84
26
|
|
|
85
27
|
# `arrays` are the operands in the order the kernel addresses them, and
|
|
86
28
|
# `fsync` says which of them it writes -- CArray's own spelling, one
|
|
87
|
-
# character per operand.
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
29
|
+
# character per operand. `parts` become the table the slab is handed
|
|
30
|
+
# as `userdata`, in order; one that is not frozen is the slab's to
|
|
31
|
+
# write.
|
|
32
|
+
#
|
|
33
|
+
# CArray makes the call, and points the slab at the Strings' own bytes:
|
|
34
|
+
# they arrive as arguments, and an argument is pinned against
|
|
35
|
+
# compaction for the length of the call. It holds the GVL throughout,
|
|
36
|
+
# unlike every other call this library makes into C. Acquiring the
|
|
37
|
+
# operands attaches them, allocates and raises on a shape that will not
|
|
38
|
+
# broadcast, none of which may be done without it, and the kernel is
|
|
39
|
+
# called back from inside, where there is nowhere to let go of it.
|
|
40
|
+
def self.run (slab, fsync, arrays, *parts)
|
|
41
|
+
CArray::AddressBasis.sweep(slab, fsync, arrays, *parts)
|
|
92
42
|
end
|
|
93
43
|
|
|
94
44
|
end
|
|
@@ -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