carray-jit 0.1.4 → 0.1.5
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 +19 -0
- data/README.md +2 -2
- data/carray-jit.gemspec +1 -1
- data/docs/04_Compiling.md +1 -1
- data/docs/05_DesignNotes.md +1 -1
- data/docs/08_LanguageReference.md +1 -0
- data/lib/carray/jit/kernel.rb +31 -5
- data/lib/carray/jit/version.rb +1 -1
- data/lib/carray/jit.rb +216 -54
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a4d8ccd59d268ba31816c5339867d7231b7051f7879d67faecdc4eb4d924b375
|
|
4
|
+
data.tar.gz: d73b3600e59633ac69986019597626b42a27e5e18b0143e00d39406d3f1c562f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 96e9c9585b2e93ed1a2f655ee92e3ef446a11d35f95895acf08dda59ed841c7570e5bc0361995c303eda8aa81b3461068fb93f0495814b6a973309d8f0f18bb0
|
|
7
|
+
data.tar.gz: 4b6fd2250447527deca38a65b19555833cd5702e2f27a253aa9bcba7359e3862e3f3b95f921004911de2ff502422c522a37d3507efb5b69449d94a3b40f4ecad
|
data/CHANGELOG.md
CHANGED
|
@@ -37,6 +37,25 @@ version you have and a newer one.
|
|
|
37
37
|
a release needs is said in the gemspec, and an entry says so only
|
|
38
38
|
when the answer changes. -->
|
|
39
39
|
|
|
40
|
+
## 0.1.5
|
|
41
|
+
|
|
42
|
+
- Change: needs CArray 3.0.4 or later in the 3.0 series; install it
|
|
43
|
+
alongside this release. 0.1.4 accepts CArray 3.0.4 but does not run
|
|
44
|
+
under it, so the two are upgraded together. A store in a block into a
|
|
45
|
+
cell that cannot hold the value still wraps as C does, where CArray
|
|
46
|
+
3.0.4's own `int8[0] = 300` raises RangeError.
|
|
47
|
+
|
|
48
|
+
- Change: `jit_each` and `jit_map` called again with operands of the same
|
|
49
|
+
shapes, types and masking cost less per call -- about 9 us rather than 11
|
|
50
|
+
for a small two-array block -- which a time-stepping loop calling one
|
|
51
|
+
block thousands of times will notice. Nothing changes in what they
|
|
52
|
+
compute; a scalar's value may differ from call to call as before.
|
|
53
|
+
|
|
54
|
+
- Fix: an element-wise `jit_each` or `jit_map` run through CArray's sweep
|
|
55
|
+
could read its scalars and C functions from where they had been before
|
|
56
|
+
GC compaction moved them, and answer wrongly or crash. Only a process
|
|
57
|
+
that compacts (`GC.compact`, `GC.auto_compact = true`) was exposed.
|
|
58
|
+
|
|
40
59
|
## 0.1.4
|
|
41
60
|
|
|
42
61
|
- New: a lazy `then_else` (a lazy condition or branch) is compiled with
|
data/README.md
CHANGED
|
@@ -8,7 +8,7 @@ The block is read with Prism, translated to C if it falls inside that subset, co
|
|
|
8
8
|
|
|
9
9
|
## Status
|
|
10
10
|
|
|
11
|
-
0.1.
|
|
11
|
+
0.1.5 is the current release, and it still moves: behaviour can change between releases.
|
|
12
12
|
|
|
13
13
|
## Features
|
|
14
14
|
|
|
@@ -35,7 +35,7 @@ gem "carray-jit"
|
|
|
35
35
|
Requires:
|
|
36
36
|
|
|
37
37
|
- Ruby >= 3.2
|
|
38
|
-
- CArray >= 3.0.
|
|
38
|
+
- CArray >= 3.0.4, < 3.1
|
|
39
39
|
- A C compiler
|
|
40
40
|
- Prism and Fiddle (both ship with Ruby; Fiddle is a bundled gem)
|
|
41
41
|
|
data/carray-jit.gemspec
CHANGED
|
@@ -33,7 +33,7 @@ Gem::Specification.new do |spec|
|
|
|
33
33
|
spec.executables = ["carray-jit"]
|
|
34
34
|
|
|
35
35
|
# A kernel reaches CArray's C by address, so the ceiling is the next minor.
|
|
36
|
-
spec.add_dependency "carray", ">= 3.0.
|
|
36
|
+
spec.add_dependency "carray", ">= 3.0.4", "< 3.1"
|
|
37
37
|
# fiddle ships as a bundled gem; depend on it explicitly so Ruby 3.5+ resolves it.
|
|
38
38
|
spec.add_dependency "fiddle"
|
|
39
39
|
end
|
data/docs/04_Compiling.md
CHANGED
|
@@ -67,7 +67,7 @@ In `~/.cache/carray-jit` (honouring `XDG_CACHE_HOME`), one directory per version
|
|
|
67
67
|
|
|
68
68
|
A kernel is only good for the version that generated it and the architecture it was built for, so those get their own directories. CArray's version is there for the same reason and one of its own: a kernel is handed CArray's memory, on layouts CArray decides, so one compiled against one version and run against the next would answer wrongly rather than fail to load.
|
|
69
69
|
|
|
70
|
-
The two numbers say different things and move on their own clocks: the first is the version that generated the kernel, the second the version it was generated against. This gem is not versioned with CArray, and one release of it meets more than one -- the dependency is `>= 3.0.
|
|
70
|
+
The two numbers say different things and move on their own clocks: the first is the version that generated the kernel, the second the version it was generated against. This gem is not versioned with CArray, and one release of it meets more than one -- the dependency is `>= 3.0.4, < 3.1`, so 3.0.4 and 3.0.5 both satisfy it -- and a layout the kernel reaches into can move between them. A new release does not spend its cache budget on entries nothing can reach any more, and a home directory shared between machines -- over NFS, or between Rosetta and native -- does not have one architecture evicting the other's kernels. A directory whose newest entry has not been touched in 30 days (`CARRAY_JIT_CACHE_MAX_AGE_DAYS`) is removed.
|
|
71
71
|
|
|
72
72
|
```ruby
|
|
73
73
|
CArray::JIT.cache_root #=> "/home/you/.cache/carray-jit"
|
data/docs/05_DesignNotes.md
CHANGED
|
@@ -4,7 +4,7 @@ Decisions that were not obvious, and why.
|
|
|
4
4
|
|
|
5
5
|
## The type is C's, and so is the arithmetic where the width is real
|
|
6
6
|
|
|
7
|
-
The storage type is CArray's, mapped to C exactly -- `int64_t`, `uint8_t`, `float` -- and everything the type itself decides follows from that: the width, the wrap on store, the bit patterns. There a kernel agrees with CArray, because both are the same C. A shift agrees too, though the two get there differently: a kernel shifts in the width it computes in, `int64_t`, and narrows on store, where CArray shifts in the cell's own width -- and since both take a shift to mean what Ruby's Integer means by it, the low bits that survive the narrowing are the same.
|
|
7
|
+
The storage type is CArray's, mapped to C exactly -- `int64_t`, `uint8_t`, `float` -- and everything the type itself decides follows from that: the width, the wrap on store, the bit patterns. There a kernel agrees with CArray's conversion between arrays (`to_type`), because both are the same C; CArray's store of a single Ruby Integer refuses one the cell cannot hold, where a kernel narrows it. A shift agrees too, though the two get there differently: a kernel shifts in the width it computes in, `int64_t`, and narrows on store, where CArray shifts in the cell's own width -- and since both take a shift to mean what Ruby's Integer means by it, the low bits that survive the narrowing are the same.
|
|
8
8
|
|
|
9
9
|
The arithmetic follows the same rule where the width makes a difference to the answer, and Ruby's where it does not. That splits the types in two.
|
|
10
10
|
|
|
@@ -417,6 +417,7 @@ A block reads like Ruby run over one cell at a time, and it answers as that Ruby
|
|
|
417
417
|
| `int8` cells: `a * 2 / 4` with `a = 100` | `-14`: each operation wraps at 8 bits | `50`: computed in `int64`, wrapped only when stored |
|
|
418
418
|
| `int8` + `uint8` | computed and returned in `uint8` | computed in `int64` |
|
|
419
419
|
| a uint8 cell `+ 300` | `RangeError`: 300 does not fit uint8 | `301`, computed in `int64` |
|
|
420
|
+
| `int8` cell `= 300` | `RangeError`: 300 does not fit int8 (`to_type` gives `44`) | `44`: narrowed as C converts |
|
|
420
421
|
| `x.round`, `floor`, `ceil`, `truncate` | a float array | an Integer, as Ruby |
|
|
421
422
|
| boolean arithmetic, `b + b` | `2`: a boolean is 0/1 | refused, as Ruby refuses `true + true` |
|
|
422
423
|
| a boolean compared with a number, `b == 1` | compares as 0/1 | refused: in Ruby `true == 1` is false |
|
data/lib/carray/jit/kernel.rb
CHANGED
|
@@ -235,9 +235,10 @@ class CArray
|
|
|
235
235
|
|
|
236
236
|
held = [buffer(reals), buffer(integers), buffer(functions),
|
|
237
237
|
buffer(data), masks, masks, error]
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
238
|
+
error = in_one_block(held) { |context, slots|
|
|
239
|
+
Sweep.call(slab_pointer, fsync, arrays, context)
|
|
240
|
+
slots.last[0, error.bytesize]
|
|
241
|
+
}
|
|
241
242
|
|
|
242
243
|
packed.each_with_index do |(array, item), index|
|
|
243
244
|
next if array.equal?(item)
|
|
@@ -558,6 +559,31 @@ class CArray
|
|
|
558
559
|
packed.empty? ? "\0" * 8 : packed
|
|
559
560
|
end
|
|
560
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
|
+
|
|
561
587
|
# The xfer tier transfers a box rather than the whole view. The box
|
|
562
588
|
# is the loop range grown by how far the kernel reaches from the cell it
|
|
563
589
|
# is on -- per array, because two arrays in one kernel need not be read
|
|
@@ -778,9 +804,9 @@ class CArray
|
|
|
778
804
|
unless array.is_a?(CArray)
|
|
779
805
|
raise Unsupported, "`#{name}` is not a CArray"
|
|
780
806
|
end
|
|
781
|
-
unless array.
|
|
807
|
+
unless array.ndim == array_rank(name)
|
|
782
808
|
raise Unsupported,
|
|
783
|
-
"`#{name}` has rank #{array.
|
|
809
|
+
"`#{name}` has rank #{array.ndim}, but is indexed with " \
|
|
784
810
|
"#{array_rank(name)} #{array_rank(name) == 1 ? 'index' : 'indices'}"
|
|
785
811
|
end
|
|
786
812
|
unless array.data_type_name == @storage_types.fetch(name)
|
data/lib/carray/jit/version.rb
CHANGED
data/lib/carray/jit.rb
CHANGED
|
@@ -638,10 +638,10 @@ class CArray
|
|
|
638
638
|
"the indices of a term are symbols naming its axes, as in " \
|
|
639
639
|
"`[a, [:i, :k]]`; got #{subscripts.inspect}"
|
|
640
640
|
end
|
|
641
|
-
unless subscripts.size == array.
|
|
641
|
+
unless subscripts.size == array.ndim
|
|
642
642
|
raise Unsupported,
|
|
643
643
|
"a term names one index per axis: this array has rank " \
|
|
644
|
-
"#{array.
|
|
644
|
+
"#{array.ndim} and #{subscripts.size} " \
|
|
645
645
|
"#{subscripts.size == 1 ? 'index' : 'indices'} were given"
|
|
646
646
|
end
|
|
647
647
|
end
|
|
@@ -680,9 +680,9 @@ class CArray
|
|
|
680
680
|
raise Unsupported, "`into:` is an array to write into"
|
|
681
681
|
end
|
|
682
682
|
expected = free.empty? ? 1 : free.size
|
|
683
|
-
unless into.
|
|
683
|
+
unless into.ndim == expected
|
|
684
684
|
raise Unsupported,
|
|
685
|
-
"`into:` has rank #{into.
|
|
685
|
+
"`into:` has rank #{into.ndim}, and the result's axes are " \
|
|
686
686
|
"#{free.empty? ? 'none, which is one cell' : free.map { |name| "`#{name}`" }.join(', ')}"
|
|
687
687
|
end
|
|
688
688
|
end
|
|
@@ -909,7 +909,7 @@ class CArray
|
|
|
909
909
|
return false if masked
|
|
910
910
|
return false if arrays.empty? || arrays.size > Sweep::MAX_ARITY
|
|
911
911
|
return false unless arrays.each_value.all? { |array|
|
|
912
|
-
array.
|
|
912
|
+
array.ndim.zero? || array.dim == shape
|
|
913
913
|
}
|
|
914
914
|
walkable_in_place?(arrays.each_value)
|
|
915
915
|
end
|
|
@@ -933,7 +933,7 @@ class CArray
|
|
|
933
933
|
# 0.9 ns/cell with the driver here against 6.1 ns/cell swept, and no
|
|
934
934
|
# scratch either way -- there was nothing the re-gather was buying.
|
|
935
935
|
def walkable_in_place? (arrays)
|
|
936
|
-
tiers = arrays.map { |array| Access.
|
|
936
|
+
tiers = arrays.map { |array| Access.tier(array) }
|
|
937
937
|
return true if tiers.include?(Access::TIER_XFER)
|
|
938
938
|
tiers.none? { |tier| tier == Access::TIER_STRIDE }
|
|
939
939
|
end
|
|
@@ -1214,6 +1214,23 @@ class CArray
|
|
|
1214
1214
|
# common case allocates nothing.
|
|
1215
1215
|
EMPTY_ARRAYS = {}.freeze
|
|
1216
1216
|
|
|
1217
|
+
# What an element-wise pass works out from its operands' shapes and
|
|
1218
|
+
# types, and not from their data: which of them are walked and which
|
|
1219
|
+
# are handed to a C function whole, the shape the pass covers, whether
|
|
1220
|
+
# lining the walked ones up to it changes any of them, which loop runs
|
|
1221
|
+
# it, the kernel, and what type a map's result is made as.
|
|
1222
|
+
#
|
|
1223
|
+
# @private
|
|
1224
|
+
RunPlan = Data.define(:walked, :addressed, :stretched, :shape,
|
|
1225
|
+
:sweeping, :kernel, :result_type)
|
|
1226
|
+
|
|
1227
|
+
# How many plans one block keeps. Not one: a caller alternating
|
|
1228
|
+
# between two model states would miss on every call, and pay for the
|
|
1229
|
+
# asking on top of what every call paid before. Eight, as a kernel
|
|
1230
|
+
# keeps of its own calls (CompiledKernel::PLAN_LIMIT), and for no
|
|
1231
|
+
# better-measured reason.
|
|
1232
|
+
RUN_PLAN_LIMIT = 8
|
|
1233
|
+
|
|
1217
1234
|
# @private
|
|
1218
1235
|
def run_over_whole_arrays (block, map: false)
|
|
1219
1236
|
node, source, origin = read_block(block)
|
|
@@ -1238,11 +1255,185 @@ class CArray
|
|
|
1238
1255
|
# is before the broadcast.
|
|
1239
1256
|
made = local_array_names(source, node)
|
|
1240
1257
|
refuse_a_shadowed_array(made, outside)
|
|
1241
|
-
|
|
1258
|
+
written = assigned_arrays(made.empty? ? assigned : assigned - made,
|
|
1259
|
+
outside)
|
|
1260
|
+
arrays = arrays.merge(written) unless written.empty?
|
|
1242
1261
|
if arrays.empty?
|
|
1243
1262
|
raise Unsupported, "the block reaches no array"
|
|
1244
1263
|
end
|
|
1245
1264
|
|
|
1265
|
+
# Everything from here to the kernel answers the same for two calls
|
|
1266
|
+
# whose operands agree on shape and type, so it is worked out once
|
|
1267
|
+
# per such agreement rather than once per call (#plan_run). What the
|
|
1268
|
+
# plan leaves to the call is what it has to: the operands themselves,
|
|
1269
|
+
# the views the broadcast makes of them, a generator's state, the
|
|
1270
|
+
# map's result, and the masks.
|
|
1271
|
+
key = run_key(arrays, scalars, c_functions, randoms, map)
|
|
1272
|
+
plan = key && found_run_plan(source, key)
|
|
1273
|
+
unless plan
|
|
1274
|
+
plan = plan_run(source, node, origin, arrays, scalars, c_functions,
|
|
1275
|
+
randoms, map)
|
|
1276
|
+
keep_run_plan(source, key, plan) if key
|
|
1277
|
+
end
|
|
1278
|
+
|
|
1279
|
+
walked = plan.addressed.empty? ? arrays : arrays.slice(*plan.walked)
|
|
1280
|
+
# Where no operand is stretched the broadcast hands back the operands
|
|
1281
|
+
# it was given, so it is not asked. Where one is, the views it makes
|
|
1282
|
+
# are views of these arrays and are made again: a view is exactly
|
|
1283
|
+
# what a plan must not keep.
|
|
1284
|
+
#
|
|
1285
|
+
# Unstretched, `aligned` can be `arrays` itself. That is sound only
|
|
1286
|
+
# because every write into it below puts back the array already
|
|
1287
|
+
# there.
|
|
1288
|
+
aligned = plan.stretched ? broadcast(walked).first : walked
|
|
1289
|
+
plan.addressed.each { |name| aligned[name] = arrays.fetch(name) }
|
|
1290
|
+
# A generator's state joins after the broadcast, never before it.
|
|
1291
|
+
# It is passed whole rather than walked, so the expression's shape
|
|
1292
|
+
# has nothing to say about it -- and it has a shape of its own that
|
|
1293
|
+
# would not line up anyway: four cells, against however many the
|
|
1294
|
+
# expression covers. Lining it up first is what `jit_each` did to a
|
|
1295
|
+
# one-cell state before 314d6cb, and a four-cell one does not even
|
|
1296
|
+
# stretch.
|
|
1297
|
+
unless randoms.empty?
|
|
1298
|
+
states = random_states(randoms)
|
|
1299
|
+
arrays = arrays.merge(states)
|
|
1300
|
+
aligned = aligned.merge(states)
|
|
1301
|
+
end
|
|
1302
|
+
shape = plan.shape
|
|
1303
|
+
if map
|
|
1304
|
+
result = CArray.send(plan.result_type, *(shape.empty? ? [1] : shape))
|
|
1305
|
+
arrays = arrays.merge(MAP_RESULT => result)
|
|
1306
|
+
aligned = aligned.merge(MAP_RESULT => result)
|
|
1307
|
+
end
|
|
1308
|
+
|
|
1309
|
+
kernel = plan.kernel
|
|
1310
|
+
# The shape of each of these was checked against the pass's when the
|
|
1311
|
+
# plan was made, and the key holds the shapes.
|
|
1312
|
+
kernel.written_arrays.each do |name|
|
|
1313
|
+
next if name == MAP_RESULT
|
|
1314
|
+
aligned[name] = arrays.fetch(name)
|
|
1315
|
+
end
|
|
1316
|
+
|
|
1317
|
+
aligned = read_once_where_the_pass_writes(aligned, kernel.written_arrays)
|
|
1318
|
+
|
|
1319
|
+
# An array handed to a C function by address is passed whole rather
|
|
1320
|
+
# than walked, so the expression's shape has nothing to say about it.
|
|
1321
|
+
# Broadcasting it would stretch a one-cell state array into a
|
|
1322
|
+
# read-only `CARepeat`, which the copy-back after the call cannot
|
|
1323
|
+
# write through -- and a state array is exactly the shape a caller
|
|
1324
|
+
# reaches for.
|
|
1325
|
+
kernel.address_arrays.each do |name|
|
|
1326
|
+
aligned[name] = arrays.fetch(name)
|
|
1327
|
+
end
|
|
1328
|
+
|
|
1329
|
+
if kernel.masked
|
|
1330
|
+
kernel.written_arrays.each do |name|
|
|
1331
|
+
array = arrays.fetch(name)
|
|
1332
|
+
array.mask = 0 unless array.has_mask?
|
|
1333
|
+
end
|
|
1334
|
+
end
|
|
1335
|
+
|
|
1336
|
+
result.mask = 0 if map && kernel.masked && !result.has_mask?
|
|
1337
|
+
drive(kernel, aligned, scalars, c_functions, shape, plan.sweeping)
|
|
1338
|
+
map ? result : kernel
|
|
1339
|
+
end
|
|
1340
|
+
|
|
1341
|
+
# What two calls of one block have to agree on for one to be run as
|
|
1342
|
+
# the other was.
|
|
1343
|
+
#
|
|
1344
|
+
# Each operand's name, type and shape, because that is what the kernel
|
|
1345
|
+
# is compiled for and the broadcast lines up; whether it is a CScalar,
|
|
1346
|
+
# because the broadcast stretches one where it leaves a one-cell array
|
|
1347
|
+
# standing. Whether it has a mask, because a mask is runtime state that
|
|
1348
|
+
# leaves the shape and the type alone -- one appearing makes a masked
|
|
1349
|
+
# kernel, and nothing else here would notice it. And the tier it would
|
|
1350
|
+
# be opened at, because that decides whether CArray's sweep or the
|
|
1351
|
+
# tiers here run the pass (#walkable_in_place?). All of it read off
|
|
1352
|
+
# the operand on this call; none of it kept from the last.
|
|
1353
|
+
#
|
|
1354
|
+
# Then the type of each scalar, each C function's signature and each
|
|
1355
|
+
# generator's kind, which is what the kernel is compiled for of those.
|
|
1356
|
+
# Not a scalar's value: a clock or a coefficient changes every step,
|
|
1357
|
+
# and keying on it would miss on every call of the loop this is for.
|
|
1358
|
+
#
|
|
1359
|
+
# Values only, never an operand: a block's plans live as long as the
|
|
1360
|
+
# process, and holding an array would hold its memory as long. Shapes
|
|
1361
|
+
# from `ndim`, `elements` and `dim0`.. rather than `dim`, which builds
|
|
1362
|
+
# an Array on every call.
|
|
1363
|
+
#
|
|
1364
|
+
# nil where a scalar is not one a kernel takes, so that the call is
|
|
1365
|
+
# refused where and as it always was rather than here.
|
|
1366
|
+
def run_key (arrays, scalars, c_functions, randoms, map)
|
|
1367
|
+
key = [map, arrays.size]
|
|
1368
|
+
arrays.each do |name, array|
|
|
1369
|
+
rank = array.ndim
|
|
1370
|
+
key << name << array.data_type << rank << array.is_a?(CScalar) <<
|
|
1371
|
+
array.has_mask? << Access.tier(array)
|
|
1372
|
+
case rank
|
|
1373
|
+
when 1 then key << array.elements
|
|
1374
|
+
when 2 then key << array.dim0 << array.dim1
|
|
1375
|
+
when 3 then key << array.dim0 << array.dim1 << array.dim2
|
|
1376
|
+
else key.concat(array.dim)
|
|
1377
|
+
end
|
|
1378
|
+
end
|
|
1379
|
+
key << scalars.size
|
|
1380
|
+
scalars.each do |name, value|
|
|
1381
|
+
key << name << TypeAssignment.scalar_type(value)
|
|
1382
|
+
end
|
|
1383
|
+
key << c_functions.size
|
|
1384
|
+
c_functions.each { |name, function| key << name << function.kernel_key }
|
|
1385
|
+
randoms.each { |name, generator| key << name << generator.generator }
|
|
1386
|
+
key
|
|
1387
|
+
rescue Unsupported
|
|
1388
|
+
nil
|
|
1389
|
+
end
|
|
1390
|
+
|
|
1391
|
+
# @private
|
|
1392
|
+
def run_plans
|
|
1393
|
+
@run_plans ||= {}
|
|
1394
|
+
end
|
|
1395
|
+
|
|
1396
|
+
# The block's plan for this key, if it has one. The list a plan sits
|
|
1397
|
+
# in is never changed once it is published -- a kernel's call lets go
|
|
1398
|
+
# of the GVL, so another thread may be reading it -- and bringing a
|
|
1399
|
+
# plan to the front replaces the list rather than editing it.
|
|
1400
|
+
def found_run_plan (source, key)
|
|
1401
|
+
plans = run_plans[source]
|
|
1402
|
+
return nil unless plans
|
|
1403
|
+
found = plans.assoc(key)
|
|
1404
|
+
return nil unless found
|
|
1405
|
+
unless plans.first.equal?(found)
|
|
1406
|
+
run_plans[source] =
|
|
1407
|
+
[found, *plans.reject { |entry| entry.equal?(found) }].freeze
|
|
1408
|
+
end
|
|
1409
|
+
found[1]
|
|
1410
|
+
end
|
|
1411
|
+
|
|
1412
|
+
# @private
|
|
1413
|
+
def keep_run_plan (source, key, plan)
|
|
1414
|
+
entry = [frozen_throughout(key), plan].freeze
|
|
1415
|
+
run_plans[source] =
|
|
1416
|
+
[entry, *run_plans[source]].first(RUN_PLAN_LIMIT).freeze
|
|
1417
|
+
end
|
|
1418
|
+
|
|
1419
|
+
# A copy that nothing outside can change under it, and that changes
|
|
1420
|
+
# nothing outside by being frozen: a C function's signature is its own
|
|
1421
|
+
# strings, and freezing those in place would freeze them in the
|
|
1422
|
+
# function.
|
|
1423
|
+
def frozen_throughout (value)
|
|
1424
|
+
case value
|
|
1425
|
+
when Array then value.map { |item| frozen_throughout(item) }.freeze
|
|
1426
|
+
when String then value.frozen? ? value : value.dup.freeze
|
|
1427
|
+
else value
|
|
1428
|
+
end
|
|
1429
|
+
end
|
|
1430
|
+
|
|
1431
|
+
# Works out what #run_over_whole_arrays keeps between calls, the way it
|
|
1432
|
+
# always worked it out on every call. Every refusal a pass can meet is
|
|
1433
|
+
# met here, before a plan exists, so a plan is only ever of a call that
|
|
1434
|
+
# ran.
|
|
1435
|
+
def plan_run (source, node, origin, arrays, scalars, c_functions,
|
|
1436
|
+
randoms, map)
|
|
1246
1437
|
# An array the block only hands to a C function whole is not walked,
|
|
1247
1438
|
# so the expression's shape has nothing to say about it -- and it has
|
|
1248
1439
|
# a shape of its own that need not line up with anything. Left in,
|
|
@@ -1250,15 +1441,10 @@ class CArray
|
|
|
1250
1441
|
# axis rather than the call it was written for.
|
|
1251
1442
|
#
|
|
1252
1443
|
# So it joins after the broadcast, which is what a generator's state
|
|
1253
|
-
# already does and for the same reason. The re-substitution
|
|
1254
|
-
#
|
|
1255
|
-
# the final one.
|
|
1444
|
+
# already does and for the same reason. The re-substitution in the
|
|
1445
|
+
# caller stays: this is a static reading, and the analyzer's answer
|
|
1446
|
+
# is the final one.
|
|
1256
1447
|
handed_over = address_array_names(source, node, c_functions)
|
|
1257
|
-
# The common case is that there are none, and it pays nothing: no
|
|
1258
|
-
# hash is built and the broadcast is handed the operands it always
|
|
1259
|
-
# was. Every call comes through here -- `jit_each` is re-entered per
|
|
1260
|
-
# call -- so the work that is only needed sometimes is done only
|
|
1261
|
-
# then.
|
|
1262
1448
|
addressed = handed_over.empty? ? EMPTY_ARRAYS
|
|
1263
1449
|
: arrays.slice(*handed_over)
|
|
1264
1450
|
walked = addressed.empty? ? arrays
|
|
@@ -1274,22 +1460,13 @@ class CArray
|
|
|
1274
1460
|
end
|
|
1275
1461
|
|
|
1276
1462
|
aligned, shape = broadcast(walked)
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
# It is passed whole rather than walked, so the expression's shape
|
|
1280
|
-
# has nothing to say about it -- and it has a shape of its own that
|
|
1281
|
-
# would not line up anyway: four cells, against however many the
|
|
1282
|
-
# expression covers. Lining it up first is what `jit_each` did to a
|
|
1283
|
-
# one-cell state before 314d6cb, and a four-cell one does not even
|
|
1284
|
-
# stretch.
|
|
1285
|
-
states = random_states(randoms)
|
|
1286
|
-
arrays = arrays.merge(states)
|
|
1287
|
-
aligned = aligned.merge(states)
|
|
1463
|
+
stretched = aligned.any? { |name, array| !array.equal?(walked[name]) }
|
|
1464
|
+
arrays = arrays.merge(random_states(randoms))
|
|
1288
1465
|
if map
|
|
1289
|
-
|
|
1290
|
-
|
|
1466
|
+
result_type = probe_map(source, node, arrays, scalars, c_functions,
|
|
1467
|
+
randoms)
|
|
1468
|
+
result = CArray.send(result_type, *(shape.empty? ? [1] : shape))
|
|
1291
1469
|
arrays = arrays.merge(MAP_RESULT => result)
|
|
1292
|
-
aligned = aligned.merge(MAP_RESULT => result)
|
|
1293
1470
|
end
|
|
1294
1471
|
# Both questions are about the arrays the loop walks. An array
|
|
1295
1472
|
# handed over whole is not one of them: its mask is not something the
|
|
@@ -1321,31 +1498,13 @@ class CArray
|
|
|
1321
1498
|
"expression covers #{shape.inspect}; a stretched array " \
|
|
1322
1499
|
"cannot be written to"
|
|
1323
1500
|
end
|
|
1324
|
-
aligned[name] = written
|
|
1325
|
-
end
|
|
1326
|
-
|
|
1327
|
-
aligned = read_once_where_the_pass_writes(aligned, kernel.written_arrays)
|
|
1328
|
-
|
|
1329
|
-
# An array handed to a C function by address is passed whole rather
|
|
1330
|
-
# than walked, so the expression's shape has nothing to say about it.
|
|
1331
|
-
# Broadcasting it would stretch a one-cell state array into a
|
|
1332
|
-
# read-only `CARepeat`, which the copy-back after the call cannot
|
|
1333
|
-
# write through -- and a state array is exactly the shape a caller
|
|
1334
|
-
# reaches for.
|
|
1335
|
-
kernel.address_arrays.each do |name|
|
|
1336
|
-
aligned[name] = arrays.fetch(name)
|
|
1337
|
-
end
|
|
1338
|
-
|
|
1339
|
-
if kernel.masked
|
|
1340
|
-
kernel.written_arrays.each do |name|
|
|
1341
|
-
array = arrays.fetch(name)
|
|
1342
|
-
array.mask = 0 unless array.has_mask?
|
|
1343
|
-
end
|
|
1344
1501
|
end
|
|
1345
1502
|
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1503
|
+
RunPlan.new(walked: walked.keys.freeze,
|
|
1504
|
+
addressed: addressed.keys.freeze,
|
|
1505
|
+
stretched: stretched, shape: shape.freeze,
|
|
1506
|
+
sweeping: sweeping, kernel: kernel,
|
|
1507
|
+
result_type: result_type)
|
|
1349
1508
|
end
|
|
1350
1509
|
|
|
1351
1510
|
# `a = b * 2.0` reads the whole of the right-hand side and then assigns,
|
|
@@ -1394,7 +1553,7 @@ class CArray
|
|
|
1394
1553
|
return if written.empty?
|
|
1395
1554
|
roots = written.map { |array| root_of(array) }
|
|
1396
1555
|
arrays.each do |name, array|
|
|
1397
|
-
next unless Access.
|
|
1556
|
+
next unless Access.tier(array) == Access::TIER_XFER
|
|
1398
1557
|
root = root_of(array)
|
|
1399
1558
|
# Another operand on the same storage, and one of the two written:
|
|
1400
1559
|
# a copy taken before the loop cannot answer for either of them.
|
|
@@ -1434,7 +1593,7 @@ class CArray
|
|
|
1434
1593
|
def broadcast (arrays)
|
|
1435
1594
|
values = arrays.values
|
|
1436
1595
|
rank = values.reject { |value| value.is_a?(CScalar) }
|
|
1437
|
-
.map(&:
|
|
1596
|
+
.map(&:ndim).max || 1
|
|
1438
1597
|
values = values.map { |value|
|
|
1439
1598
|
value.is_a?(CScalar) ? value.refer(value.data_type, [1] * rank) : value
|
|
1440
1599
|
}
|
|
@@ -1695,6 +1854,9 @@ class CArray
|
|
|
1695
1854
|
@undef_cache = {}
|
|
1696
1855
|
@probe_cache = {}
|
|
1697
1856
|
@local_array_name_cache = {}
|
|
1857
|
+
# A plan holds the kernel it runs, so one left behind would hand the
|
|
1858
|
+
# next call a kernel the registry no longer has.
|
|
1859
|
+
@run_plans = {}
|
|
1698
1860
|
end
|
|
1699
1861
|
|
|
1700
1862
|
# @return [String] the directory this environment's kernels are kept in,
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: carray-jit
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.5
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- himotoyoshi
|
|
@@ -15,7 +15,7 @@ dependencies:
|
|
|
15
15
|
requirements:
|
|
16
16
|
- - ">="
|
|
17
17
|
- !ruby/object:Gem::Version
|
|
18
|
-
version: 3.0.
|
|
18
|
+
version: 3.0.4
|
|
19
19
|
- - "<"
|
|
20
20
|
- !ruby/object:Gem::Version
|
|
21
21
|
version: '3.1'
|
|
@@ -25,7 +25,7 @@ dependencies:
|
|
|
25
25
|
requirements:
|
|
26
26
|
- - ">="
|
|
27
27
|
- !ruby/object:Gem::Version
|
|
28
|
-
version: 3.0.
|
|
28
|
+
version: 3.0.4
|
|
29
29
|
- - "<"
|
|
30
30
|
- !ruby/object:Gem::Version
|
|
31
31
|
version: '3.1'
|