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.rb
CHANGED
|
@@ -51,13 +51,23 @@ class CArray
|
|
|
51
51
|
# subset is. An expression over whole arrays that needs no compiler is
|
|
52
52
|
# CArray.fuse's.
|
|
53
53
|
#
|
|
54
|
-
# `
|
|
55
|
-
#
|
|
56
|
-
#
|
|
57
|
-
#
|
|
58
|
-
#
|
|
59
|
-
#
|
|
60
|
-
#
|
|
54
|
+
# `fastmath:` says whether the kernel may compute an answer that differs
|
|
55
|
+
# in its last bits from the one a serial Ruby loop reaches. It defaults to
|
|
56
|
+
# CArray::JIT.fastmath, which is true, and it allows two things:
|
|
57
|
+
#
|
|
58
|
+
# - A reduction's accumulator is split into partial sums. That is usually
|
|
59
|
+
# the more accurate answer as well as the faster one, because splitting
|
|
60
|
+
# the accumulation is what limits the cancellation.
|
|
61
|
+
# - A math function (`Math.exp`, `Math.sin`, `**`, ...) is computed by its
|
|
62
|
+
# vector form where the C library has one, which is glibc on x86_64.
|
|
63
|
+
# That answer is within a few units in the last place of the scalar
|
|
64
|
+
# function's, and one input can give two last bits depending on where in
|
|
65
|
+
# the array it sits.
|
|
66
|
+
#
|
|
67
|
+
# It allows nothing else: NaN and infinity are computed as they were, and
|
|
68
|
+
# a multiply and an add are not fused. Pass false for the answer of the
|
|
69
|
+
# Ruby loop -- for a compensated summation, whose algorithm *is* the order,
|
|
70
|
+
# or to check a kernel against the loop it replaces.
|
|
61
71
|
#
|
|
62
72
|
# Returns the compiled kernel, whose {CArray::JIT::CompiledKernel#c_source} is the
|
|
63
73
|
# C that ran.
|
|
@@ -66,12 +76,13 @@ class CArray
|
|
|
66
76
|
#
|
|
67
77
|
# @param extents [Array<Range, Integer>] one per loop index, an Integer
|
|
68
78
|
# standing for `0...n`.
|
|
69
|
-
# @param
|
|
70
|
-
#
|
|
79
|
+
# @param fastmath [Boolean, nil] whether a reduction may be split into
|
|
80
|
+
# partial sums and a math function computed by its vector form; `nil`
|
|
81
|
+
# defers to {CArray::JIT.fastmath}.
|
|
71
82
|
# @return [CArray::JIT::CompiledKernel]
|
|
72
83
|
# @raise [CArray::JIT::Unsupported] when the block falls outside the
|
|
73
84
|
# recognized subset, or the extents do not match its parameters.
|
|
74
|
-
def self.jit_for (*extents,
|
|
85
|
+
def self.jit_for (*extents, fastmath: nil, &block)
|
|
75
86
|
unless block
|
|
76
87
|
raise JIT::Unsupported, "jit_for needs a block"
|
|
77
88
|
end
|
|
@@ -81,7 +92,7 @@ class CArray
|
|
|
81
92
|
"indices as its parameters; a block that names none is " \
|
|
82
93
|
"element-wise and belongs to jit_each"
|
|
83
94
|
end
|
|
84
|
-
JIT.run(extents, block,
|
|
95
|
+
JIT.run(extents, block, fastmath)
|
|
85
96
|
end
|
|
86
97
|
|
|
87
98
|
# Returns the kernel compiled from `block` and run at every cell.
|
|
@@ -119,10 +130,13 @@ class CArray
|
|
|
119
130
|
#
|
|
120
131
|
# The block is read and compiled, never called, so it is not yielded to.
|
|
121
132
|
#
|
|
133
|
+
# @param fastmath [Boolean, nil] whether the math functions may be the
|
|
134
|
+
# vector forms, as {CArray.jit_for} says; `nil` defers to
|
|
135
|
+
# {CArray::JIT.fastmath}. There is no reduction here to split.
|
|
122
136
|
# @return [CArray::JIT::CompiledKernel]
|
|
123
137
|
# @raise [CArray::JIT::Unsupported] when the block falls outside the
|
|
124
138
|
# recognized subset, or names any parameter.
|
|
125
|
-
def self.jit_each (&block)
|
|
139
|
+
def self.jit_each (fastmath: nil, &block)
|
|
126
140
|
unless block
|
|
127
141
|
raise JIT::Unsupported, "jit_each needs a block"
|
|
128
142
|
end
|
|
@@ -131,7 +145,7 @@ class CArray
|
|
|
131
145
|
"this block names the arrays it reaches, so it takes no " \
|
|
132
146
|
"parameters; for a loop that names its indices, see `jit_for`"
|
|
133
147
|
end
|
|
134
|
-
JIT.run_over_whole_arrays(block)
|
|
148
|
+
JIT.run_over_whole_arrays(block, fastmath: fastmath)
|
|
135
149
|
end
|
|
136
150
|
|
|
137
151
|
# Returns a new array holding `block`'s value at every cell.
|
|
@@ -148,10 +162,11 @@ class CArray
|
|
|
148
162
|
#
|
|
149
163
|
# The block is read and compiled, never called, so it is not yielded to.
|
|
150
164
|
#
|
|
165
|
+
# @param fastmath [Boolean, nil] as {CArray.jit_each}.
|
|
151
166
|
# @return [CArray] a new array, typed from the block's value.
|
|
152
167
|
# @raise [CArray::JIT::Unsupported] when the block falls outside the
|
|
153
168
|
# recognized subset, or names any parameter.
|
|
154
|
-
def self.jit_map (&block)
|
|
169
|
+
def self.jit_map (fastmath: nil, &block)
|
|
155
170
|
unless block
|
|
156
171
|
raise JIT::Unsupported, "jit_map needs a block"
|
|
157
172
|
end
|
|
@@ -160,7 +175,7 @@ class CArray
|
|
|
160
175
|
"this block names the arrays it reaches, so it takes no " \
|
|
161
176
|
"parameters; for a loop that names its indices, see `jit_for`"
|
|
162
177
|
end
|
|
163
|
-
JIT.run_over_whole_arrays(block, map: true)
|
|
178
|
+
JIT.run_over_whole_arrays(block, map: true, fastmath: fastmath)
|
|
164
179
|
end
|
|
165
180
|
|
|
166
181
|
# Returns a new array holding `block`'s value at every cell, computed from
|
|
@@ -202,11 +217,14 @@ class CArray
|
|
|
202
217
|
# block's value's.
|
|
203
218
|
# @param into [CArray, nil] an array of yours to write into, which then
|
|
204
219
|
# decides the type.
|
|
220
|
+
# @param fastmath [Boolean, nil] as {CArray.jit_each}. A sum over a
|
|
221
|
+
# window stays in the order it was written.
|
|
205
222
|
# @return [CArray] `into` when it is given, otherwise a new array.
|
|
206
223
|
# @raise [CArray::JIT::Unsupported] when the block falls outside the
|
|
207
224
|
# recognized subset, when no array is given, or when both `type` and
|
|
208
225
|
# `into` are.
|
|
209
|
-
def self.jit_stencil (*arrays, border: :mask, type: nil, into: nil,
|
|
226
|
+
def self.jit_stencil (*arrays, border: :mask, type: nil, into: nil,
|
|
227
|
+
fastmath: nil, &block)
|
|
210
228
|
unless block
|
|
211
229
|
raise JIT::Unsupported, "jit_stencil needs a block"
|
|
212
230
|
end
|
|
@@ -215,7 +233,7 @@ class CArray
|
|
|
215
233
|
"jit_stencil takes the arrays its block has windows onto, as in " \
|
|
216
234
|
"`CArray.jit_stencil(image) { |a| ... }`"
|
|
217
235
|
end
|
|
218
|
-
JIT.run_stencil(arrays, block, border, type, into)
|
|
236
|
+
JIT.run_stencil(arrays, block, border, type, into, fastmath)
|
|
219
237
|
end
|
|
220
238
|
|
|
221
239
|
# Returns the contraction the block writes: a repeated index is summed.
|
|
@@ -271,18 +289,22 @@ class CArray
|
|
|
271
289
|
#
|
|
272
290
|
# The sum is split into partial ones, as `jit_for`'s reduction is: a
|
|
273
291
|
# contraction says which indices are summed and nothing about the order, so
|
|
274
|
-
# there is no order here to override. `
|
|
275
|
-
#
|
|
292
|
+
# there is no order here to override. `fastmath: false` asks for the
|
|
293
|
+
# serial one, which is what a Ruby loop would take, and for the scalar
|
|
294
|
+
# math functions; without the keyword the call follows
|
|
295
|
+
# `CArray::JIT.fastmath`.
|
|
276
296
|
#
|
|
277
297
|
# The block is read and compiled, never called, so it is not yielded to.
|
|
278
298
|
#
|
|
279
299
|
# @param free_indices [Array<Symbol>] the result's axes, in order; empty to
|
|
280
300
|
# let the convention decide, which is an index appearing once.
|
|
301
|
+
# @param fastmath [Boolean, nil] as {CArray.jit_for}; `nil` defers to
|
|
302
|
+
# {CArray::JIT.fastmath}.
|
|
281
303
|
# @return [CArray, CArray::JIT::CompiledKernel] the allocated result when the block
|
|
282
304
|
# assigns into nothing, otherwise the compiled kernel.
|
|
283
305
|
# @raise [CArray::JIT::Unsupported] when the block falls outside the
|
|
284
306
|
# recognized subset, or an index's axes disagree.
|
|
285
|
-
def self.jit_contract (*free_indices, &block)
|
|
307
|
+
def self.jit_contract (*free_indices, fastmath: nil, &block)
|
|
286
308
|
unless block
|
|
287
309
|
raise JIT::Unsupported, "jit_contract needs a block"
|
|
288
310
|
end
|
|
@@ -299,7 +321,8 @@ class CArray
|
|
|
299
321
|
end
|
|
300
322
|
# Nothing named is the convention; naming none of them is a contraction
|
|
301
323
|
# to a single number, and the two are different statements.
|
|
302
|
-
JIT.run_contraction(block, free_indices.empty? ? nil : free_indices
|
|
324
|
+
JIT.run_contraction(block, free_indices.empty? ? nil : free_indices,
|
|
325
|
+
fastmath)
|
|
303
326
|
end
|
|
304
327
|
|
|
305
328
|
# @!endgroup
|
|
@@ -335,14 +358,23 @@ class CArray
|
|
|
335
358
|
# can be factored and named, and its address may be handed to a C library
|
|
336
359
|
# that knows nothing about Ruby.
|
|
337
360
|
#
|
|
361
|
+
# `fastmath:` says whether a math function in a loop of the body may be
|
|
362
|
+
# computed by its vector form, as {CArray.jit_for} says. The answer is
|
|
363
|
+
# taken when the function is compiled, and it stays with the function. It
|
|
364
|
+
# is about the function called on its own, from Ruby or from a C library.
|
|
365
|
+
# A kernel that calls the function compiles the body into itself, and
|
|
366
|
+
# there the kernel's own `fastmath` decides.
|
|
367
|
+
#
|
|
338
368
|
# The block is read and compiled, never called, so it is not yielded to.
|
|
339
369
|
#
|
|
340
370
|
# @param prototype [String] the function's C declaration, as C writes it.
|
|
371
|
+
# @param fastmath [Boolean, nil] whether the math functions may be the
|
|
372
|
+
# vector forms; `nil` defers to {CArray::JIT.fastmath}.
|
|
341
373
|
# @return [CArray::JIT::CFunction]
|
|
342
374
|
# @raise [CArray::JIT::Unsupported] when the block falls outside the
|
|
343
375
|
# recognized subset.
|
|
344
|
-
def self.jit_function (prototype, &block)
|
|
345
|
-
JIT.function(prototype, &block)
|
|
376
|
+
def self.jit_function (prototype, fastmath: nil, &block)
|
|
377
|
+
JIT.function(prototype, fastmath: fastmath, &block)
|
|
346
378
|
end
|
|
347
379
|
|
|
348
380
|
# Gives every cell of `self` the block's value at its indices, and returns
|
|
@@ -381,12 +413,12 @@ class CArray
|
|
|
381
413
|
#
|
|
382
414
|
# The block is read and compiled, never called, so it is not yielded to.
|
|
383
415
|
#
|
|
384
|
-
# @param
|
|
385
|
-
# {CArray::JIT.
|
|
416
|
+
# @param fastmath [Boolean, nil] as {CArray.jit_for}; `nil` defers to
|
|
417
|
+
# {CArray::JIT.fastmath}.
|
|
386
418
|
# @return [CArray] `self`
|
|
387
419
|
# @raise [CArray::JIT::Unsupported] when the block falls outside the
|
|
388
420
|
# recognized subset, or names a number of indices other than `ndim`.
|
|
389
|
-
def jit_init (
|
|
421
|
+
def jit_init (fastmath: nil, &block)
|
|
390
422
|
unless block
|
|
391
423
|
raise JIT::Unsupported, "jit_init needs a block"
|
|
392
424
|
end
|
|
@@ -413,46 +445,50 @@ class CArray
|
|
|
413
445
|
"#{block.arity == 1 ? 'index' : 'indices'}; jit_init fills every " \
|
|
414
446
|
"cell, so there is one index per axis"
|
|
415
447
|
end
|
|
416
|
-
JIT.run(
|
|
448
|
+
JIT.run(shape, block, fastmath, into: self)
|
|
417
449
|
end
|
|
418
450
|
|
|
419
451
|
# @!endgroup
|
|
420
452
|
|
|
421
453
|
# The compiler behind `CArray.jit_*`: it reads a block, generates C for it,
|
|
422
|
-
# builds it and calls it. What is public here is the
|
|
454
|
+
# builds it and calls it. What is public here is the `fastmath` default
|
|
423
455
|
# and the kernel cache; the rest is the machinery.
|
|
424
456
|
module JIT
|
|
425
457
|
|
|
426
458
|
class << self
|
|
427
459
|
|
|
428
|
-
# Whether a
|
|
429
|
-
# the call site does not say. True: a
|
|
430
|
-
#
|
|
431
|
-
#
|
|
460
|
+
# Whether a kernel may compute an answer that differs in its last bits
|
|
461
|
+
# from a serial Ruby loop's, when the call site does not say. True: a
|
|
462
|
+
# kernel is here to be fast.
|
|
463
|
+
#
|
|
464
|
+
# It is two things. A reduction's accumulator is split into partial
|
|
465
|
+
# sums, in `jit_for`, `jit_init` and a contraction. And in every
|
|
466
|
+
# kernel, a math function is computed by its vector form where the C
|
|
467
|
+
# library has one (see VectorMath). Every `jit_` method that
|
|
468
|
+
# compiles a kernel, and `jit_function`, takes `fastmath:` per call;
|
|
469
|
+
# this is the answer for a call that does not say.
|
|
432
470
|
#
|
|
433
|
-
#
|
|
434
|
-
# false that says "compute this exactly as the loop would" -- for a
|
|
471
|
+
# False says "compute this exactly as the loop would" -- for a
|
|
435
472
|
# compensated summation, or to check one against the other.
|
|
436
|
-
#
|
|
437
|
-
#
|
|
438
|
-
#
|
|
439
|
-
#
|
|
440
|
-
# sums when the call site does not say.
|
|
473
|
+
# CARRAY_JIT_FASTMATH=0 sets it false for a whole process.
|
|
474
|
+
# @!attribute [w] fastmath
|
|
475
|
+
# Sets whether a kernel may split a reduction and use the vector forms
|
|
476
|
+
# of the math functions when the call site does not say.
|
|
441
477
|
# @return [Boolean]
|
|
442
|
-
attr_writer :
|
|
478
|
+
attr_writer :fastmath
|
|
443
479
|
|
|
444
|
-
# @return [Boolean] whether a
|
|
445
|
-
# the call site does not say.
|
|
446
|
-
def
|
|
447
|
-
return @
|
|
448
|
-
@
|
|
480
|
+
# @return [Boolean] whether a kernel may split a reduction and use the
|
|
481
|
+
# vector forms of the math functions when the call site does not say.
|
|
482
|
+
def fastmath
|
|
483
|
+
return @fastmath unless @fastmath.nil?
|
|
484
|
+
@fastmath = ENV["CARRAY_JIT_FASTMATH"] != "0"
|
|
449
485
|
end
|
|
450
486
|
|
|
451
487
|
# @private
|
|
452
488
|
RESULT = :__contraction_result
|
|
453
489
|
|
|
454
490
|
# @private
|
|
455
|
-
def run_contraction (block, free_indices = nil)
|
|
491
|
+
def run_contraction (block, free_indices = nil, fastmath = nil)
|
|
456
492
|
node, source, origin = read_block(block)
|
|
457
493
|
# An index named at the call site is not a parameter of the block, so
|
|
458
494
|
# the block reaches for it the way it reaches for a captured value.
|
|
@@ -464,7 +500,8 @@ class CArray
|
|
|
464
500
|
"and runs once per term, so a draw in it would be one per term " \
|
|
465
501
|
"rather than one per cell of the result")
|
|
466
502
|
contract(source, arrays, free_indices, node: node, origin: origin,
|
|
467
|
-
scalars: scalars, c_functions: c_functions
|
|
503
|
+
scalars: scalars, c_functions: c_functions,
|
|
504
|
+
fastmath: fastmath)
|
|
468
505
|
end
|
|
469
506
|
|
|
470
507
|
# Reads a block as a contraction and returns the terms it is a product
|
|
@@ -706,7 +743,8 @@ class CArray
|
|
|
706
743
|
# is the same whatever recovered them, which is what lets
|
|
707
744
|
# `contract_terms` reach it with a source it wrote itself.
|
|
708
745
|
def contract (source, arrays, free_indices,
|
|
709
|
-
node: nil, origin: nil, scalars: {}, c_functions: {}
|
|
746
|
+
node: nil, origin: nil, scalars: {}, c_functions: {},
|
|
747
|
+
fastmath: nil)
|
|
710
748
|
result = allocate_result(source, node, arrays, scalars, free_indices,
|
|
711
749
|
c_functions)
|
|
712
750
|
arrays = arrays.merge(RESULT => result) if result
|
|
@@ -725,7 +763,7 @@ class CArray
|
|
|
725
763
|
# in is not something the caller wrote, so splitting
|
|
726
764
|
# the sum into partial ones does not change what the
|
|
727
765
|
# contraction means. Same licence `jit_for` takes.
|
|
728
|
-
|
|
766
|
+
fastmath: fastmath.nil? ? JIT.fastmath : fastmath,
|
|
729
767
|
free_indices: free_indices,
|
|
730
768
|
cell_names: cell_names(arrays))
|
|
731
769
|
|
|
@@ -785,7 +823,7 @@ class CArray
|
|
|
785
823
|
free, index_axes, type = probe
|
|
786
824
|
shape = free.map do |index|
|
|
787
825
|
extents = index_axes.fetch(index).map { |array, axis|
|
|
788
|
-
[arrays.fetch(array).
|
|
826
|
+
[arrays.fetch(array).shape[axis], array, axis]
|
|
789
827
|
}
|
|
790
828
|
distinct = extents.map(&:first).uniq
|
|
791
829
|
if distinct.size > 1
|
|
@@ -865,7 +903,7 @@ class CArray
|
|
|
865
903
|
order = kernel.index_names + kernel.contracted_names
|
|
866
904
|
order.map do |index|
|
|
867
905
|
places = kernel.index_axes[index]
|
|
868
|
-
extents = places.map { |array, axis| [arrays.fetch(array).
|
|
906
|
+
extents = places.map { |array, axis| [arrays.fetch(array).shape[axis], array, axis] }
|
|
869
907
|
distinct = extents.map(&:first).uniq
|
|
870
908
|
if distinct.size > 1
|
|
871
909
|
described = extents.map { |extent, array, axis|
|
|
@@ -908,7 +946,7 @@ class CArray
|
|
|
908
946
|
return false if masked
|
|
909
947
|
return false if arrays.empty? || arrays.size > Sweep::MAX_ARITY
|
|
910
948
|
return false unless arrays.each_value.all? { |array|
|
|
911
|
-
array.ndim.zero? || array.
|
|
949
|
+
array.ndim.zero? || array.shape == shape
|
|
912
950
|
}
|
|
913
951
|
walkable_in_place?(arrays.each_value)
|
|
914
952
|
end
|
|
@@ -975,7 +1013,7 @@ class CArray
|
|
|
975
1013
|
COMPUTED_BORDERS = [:zero, :clamp, :wrap].freeze
|
|
976
1014
|
|
|
977
1015
|
# @private
|
|
978
|
-
def run_stencil (arrays, block, border, type, into)
|
|
1016
|
+
def run_stencil (arrays, block, border, type, into, fastmath = nil)
|
|
979
1017
|
unless FRAME_BORDERS.include?(border) || COMPUTED_BORDERS.include?(border)
|
|
980
1018
|
raise Unsupported,
|
|
981
1019
|
"`border:` is " \
|
|
@@ -1004,15 +1042,15 @@ class CArray
|
|
|
1004
1042
|
"#{array.class}"
|
|
1005
1043
|
end
|
|
1006
1044
|
end
|
|
1007
|
-
shape = arrays.first.
|
|
1045
|
+
shape = arrays.first.shape
|
|
1008
1046
|
arrays.each_with_index do |array, position|
|
|
1009
|
-
next if array.
|
|
1047
|
+
next if array.shape == shape
|
|
1010
1048
|
# Shapes are required to agree rather than broadcast: a stretched
|
|
1011
1049
|
# axis has no neighbour to reach, so what a window would mean on one
|
|
1012
1050
|
# is a question this does not have to answer yet.
|
|
1013
1051
|
raise Unsupported,
|
|
1014
1052
|
"`#{windows.first}` has shape #{shape.inspect} and " \
|
|
1015
|
-
"`#{windows[position]}` #{array.
|
|
1053
|
+
"`#{windows[position]}` #{array.shape.inspect}; a stencil's " \
|
|
1016
1054
|
"arrays have the same shape"
|
|
1017
1055
|
end
|
|
1018
1056
|
|
|
@@ -1051,6 +1089,7 @@ class CArray
|
|
|
1051
1089
|
rank: shape.size,
|
|
1052
1090
|
windows: windows,
|
|
1053
1091
|
border: COMPUTED_BORDERS.include?(border) ? border : nil,
|
|
1092
|
+
vector_math: fastmath,
|
|
1054
1093
|
map: true,
|
|
1055
1094
|
result: MAP_RESULT)
|
|
1056
1095
|
|
|
@@ -1061,11 +1100,18 @@ class CArray
|
|
|
1061
1100
|
low, high = reach[axis]
|
|
1062
1101
|
[-low, extent - high, 1]
|
|
1063
1102
|
}
|
|
1064
|
-
|
|
1103
|
+
# An array narrower than the window along some axis has no interior.
|
|
1104
|
+
# Under a rule that computes the frame every cell is then a frame
|
|
1105
|
+
# cell, and the frame's body answers for a read however far outside
|
|
1106
|
+
# it falls. Under `:mask` and `:skip` no cell would be computed at
|
|
1107
|
+
# all, which is refused.
|
|
1108
|
+
no_interior = bounds.any? { |from, to, _| from >= to }
|
|
1109
|
+
if no_interior && !COMPUTED_BORDERS.include?(border)
|
|
1065
1110
|
raise Unsupported,
|
|
1066
1111
|
"the window reaches #{reach.inspect} and the arrays are " \
|
|
1067
1112
|
"#{shape.inspect}, so there is no cell where the window is " \
|
|
1068
|
-
"inside the array"
|
|
1113
|
+
"inside the array, and `border: #{border.inspect}` computes " \
|
|
1114
|
+
"only those; `:zero`, `:clamp` and `:wrap` compute every cell"
|
|
1069
1115
|
end
|
|
1070
1116
|
# Writing into an array a window reads is not a pass over the array:
|
|
1071
1117
|
# a cell written here is a neighbour a later cell reads, so what comes
|
|
@@ -1094,9 +1140,12 @@ class CArray
|
|
|
1094
1140
|
result.mask = 0 if (kernel.masked || border == :mask) && !result.has_mask?
|
|
1095
1141
|
mark_frame(result, bounds, shape) if border == :mask
|
|
1096
1142
|
operands = given.merge(MAP_RESULT => result)
|
|
1097
|
-
kernel.call(operands, scalars, bounds, c_functions)
|
|
1143
|
+
kernel.call(operands, scalars, bounds, c_functions) unless no_interior
|
|
1098
1144
|
if COMPUTED_BORDERS.include?(border)
|
|
1099
|
-
|
|
1145
|
+
boxes = if no_interior then [shape.map { |extent| [0, extent, 1] }]
|
|
1146
|
+
else frame_boxes(bounds, shape)
|
|
1147
|
+
end
|
|
1148
|
+
boxes.each do |box|
|
|
1100
1149
|
kernel.call(operands, scalars, box, c_functions, border: true)
|
|
1101
1150
|
end
|
|
1102
1151
|
end
|
|
@@ -1154,7 +1203,7 @@ class CArray
|
|
|
1154
1203
|
def stencil_result (source, node, arrays, scalars, c_functions, windows,
|
|
1155
1204
|
shape, type, into)
|
|
1156
1205
|
if into
|
|
1157
|
-
unless into.is_a?(CArray) && into.
|
|
1206
|
+
unless into.is_a?(CArray) && into.shape == shape
|
|
1158
1207
|
raise Unsupported,
|
|
1159
1208
|
"`into:` takes an array of the stencil's own shape " \
|
|
1160
1209
|
"#{shape.inspect}"
|
|
@@ -1231,8 +1280,12 @@ class CArray
|
|
|
1231
1280
|
RUN_PLAN_LIMIT = 8
|
|
1232
1281
|
|
|
1233
1282
|
# @private
|
|
1234
|
-
def run_over_whole_arrays (block, map: false)
|
|
1283
|
+
def run_over_whole_arrays (block, map: false, fastmath: nil)
|
|
1235
1284
|
node, source, origin = read_block(block)
|
|
1285
|
+
# After the block is read, which is where another Ractor is refused:
|
|
1286
|
+
# asking for the default writes this module's state.
|
|
1287
|
+
fastmath = JIT.fastmath if fastmath.nil?
|
|
1288
|
+
fastmath = fastmath ? true : false
|
|
1236
1289
|
free, assigned = free_and_assigned_names(source, node)
|
|
1237
1290
|
# One Binding for the three questions asked of it. `Proc#binding`
|
|
1238
1291
|
# allocates one every time it is asked, and this method is re-entered
|
|
@@ -1267,11 +1320,11 @@ class CArray
|
|
|
1267
1320
|
# plan leaves to the call is what it has to: the operands themselves,
|
|
1268
1321
|
# the views the broadcast makes of them, a generator's state, the
|
|
1269
1322
|
# map's result, and the masks.
|
|
1270
|
-
key = run_key(arrays, scalars, c_functions, randoms, map)
|
|
1323
|
+
key = run_key(arrays, scalars, c_functions, randoms, map, fastmath)
|
|
1271
1324
|
plan = key && found_run_plan(source, key)
|
|
1272
1325
|
unless plan
|
|
1273
1326
|
plan = plan_run(source, node, origin, arrays, scalars, c_functions,
|
|
1274
|
-
randoms, map)
|
|
1327
|
+
randoms, map, fastmath)
|
|
1275
1328
|
keep_run_plan(source, key, plan) if key
|
|
1276
1329
|
end
|
|
1277
1330
|
|
|
@@ -1352,27 +1405,26 @@ class CArray
|
|
|
1352
1405
|
#
|
|
1353
1406
|
# Then the type of each scalar, each C function's signature and each
|
|
1354
1407
|
# generator's kind, which is what the kernel is compiled for of those.
|
|
1408
|
+
# And the call's answer about `fastmath`, which decides the C.
|
|
1355
1409
|
# Not a scalar's value: a clock or a coefficient changes every step,
|
|
1356
1410
|
# and keying on it would miss on every call of the loop this is for.
|
|
1357
1411
|
#
|
|
1358
1412
|
# 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.
|
|
1360
|
-
#
|
|
1361
|
-
# an Array on every call.
|
|
1413
|
+
# process, and holding an array would hold its memory as long. A 1-D
|
|
1414
|
+
# shape is its `elements`, which builds no Array.
|
|
1362
1415
|
#
|
|
1363
1416
|
# nil where a scalar is not one a kernel takes, so that the call is
|
|
1364
1417
|
# refused where and as it always was rather than here.
|
|
1365
|
-
def run_key (arrays, scalars, c_functions, randoms, map)
|
|
1366
|
-
key = [map, arrays.size]
|
|
1418
|
+
def run_key (arrays, scalars, c_functions, randoms, map, fastmath)
|
|
1419
|
+
key = [map, fastmath, arrays.size]
|
|
1367
1420
|
arrays.each do |name, array|
|
|
1368
1421
|
rank = array.ndim
|
|
1369
1422
|
key << name << array.data_type << rank << array.is_a?(CScalar) <<
|
|
1370
1423
|
array.has_mask? << Access.tier(array)
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
else key.concat(array.dim)
|
|
1424
|
+
if rank == 1
|
|
1425
|
+
key << array.elements
|
|
1426
|
+
else
|
|
1427
|
+
key.concat(array.shape)
|
|
1376
1428
|
end
|
|
1377
1429
|
end
|
|
1378
1430
|
key << scalars.size
|
|
@@ -1388,8 +1440,12 @@ class CArray
|
|
|
1388
1440
|
end
|
|
1389
1441
|
|
|
1390
1442
|
# @private
|
|
1443
|
+
# Two stores, one for each answer of `fastmath`: a plan holds the
|
|
1444
|
+
# kernel that was compiled under one of them, and the process default
|
|
1445
|
+
# can be changed between two calls of the same block.
|
|
1391
1446
|
def run_plans
|
|
1392
|
-
@run_plans ||= {}
|
|
1447
|
+
@run_plans ||= { true => {}, false => {} }
|
|
1448
|
+
@run_plans[JIT.fastmath ? true : false]
|
|
1393
1449
|
end
|
|
1394
1450
|
|
|
1395
1451
|
# The block's plan for this key, if it has one. The list a plan sits
|
|
@@ -1432,7 +1488,7 @@ class CArray
|
|
|
1432
1488
|
# met here, before a plan exists, so a plan is only ever of a call that
|
|
1433
1489
|
# ran.
|
|
1434
1490
|
def plan_run (source, node, origin, arrays, scalars, c_functions,
|
|
1435
|
-
randoms, map)
|
|
1491
|
+
randoms, map, fastmath = nil)
|
|
1436
1492
|
# An array the block only hands to a C function whole is not walked,
|
|
1437
1493
|
# so the expression's shape has nothing to say about it -- and it has
|
|
1438
1494
|
# a shape of its own that need not line up with anything. Left in,
|
|
@@ -1485,15 +1541,16 @@ class CArray
|
|
|
1485
1541
|
randoms: randoms,
|
|
1486
1542
|
masked: masked,
|
|
1487
1543
|
rank: sweeping ? 1 : shape.size,
|
|
1544
|
+
vector_math: fastmath,
|
|
1488
1545
|
map: map,
|
|
1489
1546
|
result: map ? MAP_RESULT : nil)
|
|
1490
1547
|
|
|
1491
1548
|
kernel.written_arrays.each do |name|
|
|
1492
1549
|
next if name == MAP_RESULT
|
|
1493
1550
|
written = arrays.fetch(name)
|
|
1494
|
-
unless written.
|
|
1551
|
+
unless written.shape == shape
|
|
1495
1552
|
raise Unsupported,
|
|
1496
|
-
"`#{name}` has shape #{written.
|
|
1553
|
+
"`#{name}` has shape #{written.shape.inspect}, but the " \
|
|
1497
1554
|
"expression covers #{shape.inspect}; a stretched array " \
|
|
1498
1555
|
"cannot be written to"
|
|
1499
1556
|
end
|
|
@@ -1607,7 +1664,7 @@ class CArray
|
|
|
1607
1664
|
value.is_a?(CScalar) ? value.refer(value.data_type, [1] * rank) : value
|
|
1608
1665
|
}
|
|
1609
1666
|
aligned = values.size == 1 ? values : CArray.broadcast(*values)
|
|
1610
|
-
[arrays.keys.zip(aligned).to_h, aligned.first.
|
|
1667
|
+
[arrays.keys.zip(aligned).to_h, aligned.first.shape]
|
|
1611
1668
|
end
|
|
1612
1669
|
|
|
1613
1670
|
# An indexed kernel addresses what it is handed, so a CScalar among the
|
|
@@ -1628,7 +1685,7 @@ class CArray
|
|
|
1628
1685
|
# statement is what the cell of `into` gets, as jit_map's is what the
|
|
1629
1686
|
# cell of its own result gets. Without it the block writes the arrays
|
|
1630
1687
|
# it names, which is jit_for.
|
|
1631
|
-
def run (extents, block,
|
|
1688
|
+
def run (extents, block, fastmath = nil, into: nil)
|
|
1632
1689
|
node, source, origin = read_block(block)
|
|
1633
1690
|
names = capture_names(source, node)
|
|
1634
1691
|
arrays, scalars, c_functions, randoms =
|
|
@@ -1654,19 +1711,19 @@ class CArray
|
|
|
1654
1711
|
end
|
|
1655
1712
|
pairs = bounds(extents, extents.size)
|
|
1656
1713
|
steps = pairs.map { |triple, _| triple[2] }
|
|
1657
|
-
|
|
1714
|
+
fastmath = JIT.fastmath if fastmath.nil?
|
|
1658
1715
|
|
|
1659
1716
|
# Which kernel runs, and whether any operand would be transferred,
|
|
1660
1717
|
# answer the same for two calls whose operands agree on name, type,
|
|
1661
1718
|
# mask and tier -- so they are worked out once per such agreement,
|
|
1662
1719
|
# as an element-wise pass does (#run_over_whole_arrays).
|
|
1663
1720
|
key = indexed_run_key(arrays, scalars, c_functions, randoms, steps,
|
|
1664
|
-
into,
|
|
1721
|
+
into, fastmath)
|
|
1665
1722
|
plan = key && found_run_plan(source, key)
|
|
1666
1723
|
unless plan
|
|
1667
1724
|
plan = plan_indexed_run(source, node, origin, arrays, scalars,
|
|
1668
1725
|
c_functions, randoms, steps, into,
|
|
1669
|
-
|
|
1726
|
+
fastmath)
|
|
1670
1727
|
keep_run_plan(source, key, plan) if key
|
|
1671
1728
|
end
|
|
1672
1729
|
|
|
@@ -1716,8 +1773,8 @@ class CArray
|
|
|
1716
1773
|
# nil where a scalar is not one a kernel takes, so that the call is
|
|
1717
1774
|
# refused where and as it always was rather than here.
|
|
1718
1775
|
def indexed_run_key (arrays, scalars, c_functions, randoms, steps, into,
|
|
1719
|
-
|
|
1720
|
-
key = [:indexed, into ? true : false,
|
|
1776
|
+
fastmath)
|
|
1777
|
+
key = [:indexed, into ? true : false, fastmath, steps, arrays.size]
|
|
1721
1778
|
arrays.each do |name, array|
|
|
1722
1779
|
key << name << array.data_type << array.is_a?(CScalar) <<
|
|
1723
1780
|
array.has_mask? << Access.tier(array)
|
|
@@ -1736,7 +1793,7 @@ class CArray
|
|
|
1736
1793
|
|
|
1737
1794
|
# @private
|
|
1738
1795
|
def plan_indexed_run (source, node, origin, arrays, scalars, c_functions,
|
|
1739
|
-
randoms, steps, into,
|
|
1796
|
+
randoms, steps, into, fastmath)
|
|
1740
1797
|
kernel = compile(source,
|
|
1741
1798
|
node: node,
|
|
1742
1799
|
origin: origin,
|
|
@@ -1750,7 +1807,7 @@ class CArray
|
|
|
1750
1807
|
cell_names: cell_names(arrays),
|
|
1751
1808
|
map: into ? true : false,
|
|
1752
1809
|
result: into ? INIT_TARGET : nil,
|
|
1753
|
-
|
|
1810
|
+
fastmath: fastmath)
|
|
1754
1811
|
transferred = arrays.each_value.any? { |array|
|
|
1755
1812
|
Access.tier(array) == Access::TIER_XFER
|
|
1756
1813
|
}
|
|
@@ -1764,11 +1821,24 @@ class CArray
|
|
|
1764
1821
|
def compile (source, node: nil, origin: nil, array_names:, storage_types:,
|
|
1765
1822
|
scalar_values:, c_functions: {}, masked: false, rank: nil,
|
|
1766
1823
|
steps: nil, contract: false, result: nil, map: false,
|
|
1767
|
-
|
|
1768
|
-
free_indices: nil, randoms: {})
|
|
1824
|
+
fastmath: nil, cell_names: [], windows: [], border: nil,
|
|
1825
|
+
free_indices: nil, randoms: {}, vector_math: nil)
|
|
1769
1826
|
# A kernel that mentions UNDEF is a masked one whatever its arrays
|
|
1770
1827
|
# carry, and deciding that here means no caller has to remember it.
|
|
1771
1828
|
masked ||= mentions_undef(source, node)
|
|
1829
|
+
# `fastmath` is two things here. A reduction is split into partial
|
|
1830
|
+
# sums only where the caller said true: `jit_for`, `jit_init` and a
|
|
1831
|
+
# contraction pass their answer as `fastmath`, and the other kernels
|
|
1832
|
+
# leave it nil and keep the serial accumulator. The math.h functions
|
|
1833
|
+
# in their vector forms are for every kernel: those other kernels
|
|
1834
|
+
# pass their answer as `vector_math`, and a caller that says nothing
|
|
1835
|
+
# follows the process. The second is only true where there are such
|
|
1836
|
+
# forms, so that the key and the C are the same with and without it
|
|
1837
|
+
# everywhere else.
|
|
1838
|
+
reassociate = fastmath ? true : false
|
|
1839
|
+
vector_math = fastmath if vector_math.nil?
|
|
1840
|
+
vector_math = JIT.fastmath if vector_math.nil?
|
|
1841
|
+
vector_math &&= VectorMath.available?
|
|
1772
1842
|
key = [source, storage_types,
|
|
1773
1843
|
scalar_values.transform_values { |value|
|
|
1774
1844
|
TypeAssignment.scalar_type(value)
|
|
@@ -1802,14 +1872,16 @@ class CArray
|
|
|
1802
1872
|
# The licence is part of the kernel, not of the call: it
|
|
1803
1873
|
# decides what C is emitted, so the two spellings are two
|
|
1804
1874
|
# kernels and the cache keeps them apart.
|
|
1805
|
-
reassociate
|
|
1875
|
+
reassociate,
|
|
1876
|
+
# And so is its other half.
|
|
1877
|
+
vector_math]
|
|
1806
1878
|
found = registry[key]
|
|
1807
1879
|
return found if found
|
|
1808
1880
|
registry[key] = build(source, node, array_names, storage_types,
|
|
1809
1881
|
scalar_values, c_functions, masked, rank, steps,
|
|
1810
1882
|
contract, result, origin, map, reassociate,
|
|
1811
1883
|
cell_names, windows, border, free_indices,
|
|
1812
|
-
randoms)
|
|
1884
|
+
randoms, vector_math)
|
|
1813
1885
|
end
|
|
1814
1886
|
|
|
1815
1887
|
# A kernel that mentions UNDEF is a masked kernel whatever its arrays
|
|
@@ -1933,7 +2005,7 @@ class CArray
|
|
|
1933
2005
|
@local_array_name_cache = {}
|
|
1934
2006
|
# A plan holds the kernel it runs, so one left behind would hand the
|
|
1935
2007
|
# next call a kernel the registry no longer has.
|
|
1936
|
-
@run_plans =
|
|
2008
|
+
@run_plans = nil
|
|
1937
2009
|
end
|
|
1938
2010
|
|
|
1939
2011
|
# @return [String] the directory this environment's kernels are kept in,
|
|
@@ -2001,7 +2073,7 @@ class CArray
|
|
|
2001
2073
|
masked, rank = nil, steps = nil, contract = false, result = nil,
|
|
2002
2074
|
origin = nil, map = false, reassociate = false,
|
|
2003
2075
|
cell_names = [], windows = [], border = nil, free_indices = nil,
|
|
2004
|
-
randoms = {})
|
|
2076
|
+
randoms = {}, vector_math = false)
|
|
2005
2077
|
analyzer = Analyzer.new(source, node: node, array_names: array_names,
|
|
2006
2078
|
c_functions: c_functions,
|
|
2007
2079
|
rank: rank, steps: steps, contract: contract,
|
|
@@ -2015,6 +2087,7 @@ class CArray
|
|
|
2015
2087
|
c_functions: c_functions,
|
|
2016
2088
|
randoms: randoms,
|
|
2017
2089
|
masked: masked, reassociate: reassociate,
|
|
2090
|
+
vector_math: vector_math,
|
|
2018
2091
|
steps: steps, border: border,
|
|
2019
2092
|
origin: origin, block_source: source)
|
|
2020
2093
|
CompiledKernel.new(source: source, generator: generator,
|
|
@@ -2024,6 +2097,7 @@ class CArray
|
|
|
2024
2097
|
# Recovering a block's source means parsing the whole file it lives in,
|
|
2025
2098
|
# which is far too expensive to repeat per call.
|
|
2026
2099
|
def read_block (block)
|
|
2100
|
+
refuse_outside_the_main_ractor
|
|
2027
2101
|
sequence = RubyVM::InstructionSequence.of(block) if
|
|
2028
2102
|
defined?(RubyVM::InstructionSequence)
|
|
2029
2103
|
return BlockReader.read(block) unless sequence
|
|
@@ -2051,7 +2125,7 @@ class CArray
|
|
|
2051
2125
|
}
|
|
2052
2126
|
return unless shadowed
|
|
2053
2127
|
raise Unsupported,
|
|
2054
|
-
"`#{shadowed}` is made in this block with `CArray.
|
|
2128
|
+
"`#{shadowed}` is made in this block with `CArray.float64` or " \
|
|
2055
2129
|
"its kin, and the block also closes over an array named " \
|
|
2056
2130
|
"`#{shadowed}`; in this spelling an assignment writes that " \
|
|
2057
2131
|
"array's cell, so the line means one thing to a reader and " \
|