carray-jit 0.1.6 → 0.1.7

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.
@@ -1,4 +1,4 @@
1
- require "fiddle"
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
- def self.call (slab, fsync, arrays, context)
89
- function(arrays.size).call(slab, fsync,
90
- *arrays.map { |array| Fiddle.dlwrap(array) },
91
- context)
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
@@ -1,5 +1,5 @@
1
1
  class CArray
2
2
  module JIT
3
- VERSION = "0.1.6"
3
+ VERSION = "0.1.7"
4
4
  end
5
5
  end
data/lib/carray/jit.rb CHANGED
@@ -315,9 +315,9 @@ class CArray
315
315
  # CArray.jit_each { out = j0.call(x) }
316
316
  #
317
317
  # Nothing is compiled here: `extern` is C's word for a body that lives
318
- # elsewhere, and finding it is Fiddle's job. What this gem adds is that the
319
- # kernel calls the address directly instead of reaching it per cell through
320
- # Fiddle.
318
+ # elsewhere, and finding it is the dynamic loader's job. What this gem adds
319
+ # is that the kernel calls the address directly instead of reaching it per
320
+ # cell through a foreign-function call from Ruby.
321
321
  #
322
322
  # @param prototype [String] the function's C declaration, as C writes it.
323
323
  # @param from [String, nil] the library to open; `nil` searches the process.
@@ -905,7 +905,6 @@ class CArray
905
905
  # that every operand already has the shape the pass covers, which is
906
906
  # stricter than agreeing on the count of cells.
907
907
  def sweepable_pass? (arrays, shape, masked)
908
- return false unless Sweep.available?
909
908
  return false if masked
910
909
  return false if arrays.empty? || arrays.size > Sweep::MAX_ARITY
911
910
  return false unless arrays.each_value.all? { |array|
@@ -1526,6 +1525,16 @@ class CArray
1526
1525
  written = written_names.filter_map { |name| aligned[name] }
1527
1526
  return aligned if written.empty?
1528
1527
  roots = written.map { |array| root_of(array) }
1528
+ # Asked first without building anything, because the answer is nearly
1529
+ # always that nothing is shared, and this runs on every call. The
1530
+ # root is compared first since it is what nearly always says no.
1531
+ shared = aligned.any? { |name, array|
1532
+ next false if written_names.include?(name)
1533
+ root = root_of(array)
1534
+ roots.any? { |one| one.equal?(root) } &&
1535
+ written.none? { |one| one.equal?(array) }
1536
+ }
1537
+ return aligned unless shared
1529
1538
  aligned.to_h do |name, array|
1530
1539
  next [name, array] if written_names.include?(name)
1531
1540
  next [name, array] if written.any? { |other| other.equal?(array) }
@@ -1645,21 +1654,23 @@ class CArray
1645
1654
  end
1646
1655
  pairs = bounds(extents, extents.size)
1647
1656
  steps = pairs.map { |triple, _| triple[2] }
1657
+ reassociate = JIT.reassociate if reassociate.nil?
1658
+
1659
+ # Which kernel runs, and whether any operand would be transferred,
1660
+ # answer the same for two calls whose operands agree on name, type,
1661
+ # mask and tier -- so they are worked out once per such agreement,
1662
+ # as an element-wise pass does (#run_over_whole_arrays).
1663
+ key = indexed_run_key(arrays, scalars, c_functions, randoms, steps,
1664
+ into, reassociate)
1665
+ plan = key && found_run_plan(source, key)
1666
+ unless plan
1667
+ plan = plan_indexed_run(source, node, origin, arrays, scalars,
1668
+ c_functions, randoms, steps, into,
1669
+ reassociate)
1670
+ keep_run_plan(source, key, plan) if key
1671
+ end
1648
1672
 
1649
- kernel = compile(source,
1650
- node: node,
1651
- origin: origin,
1652
- array_names: arrays.keys,
1653
- storage_types: arrays.transform_values(&:data_type_name),
1654
- scalar_values: scalars,
1655
- c_functions: c_functions,
1656
- randoms: randoms,
1657
- masked: arrays.each_value.any? { |array| array.has_mask? },
1658
- steps: steps,
1659
- cell_names: cell_names(arrays),
1660
- map: into ? true : false,
1661
- result: into ? INIT_TARGET : nil,
1662
- reassociate: reassociate.nil? ? JIT.reassociate : reassociate)
1673
+ kernel = plan.kernel
1663
1674
  # The kernel decides, not the caller: mentioning UNDEF makes it a
1664
1675
  # masked kernel even when no array carries a mask yet.
1665
1676
  if kernel.masked
@@ -1675,11 +1686,77 @@ class CArray
1675
1686
  "#{pairs.size} #{pairs.size == 1 ? 'extent was' : 'extents were'} " \
1676
1687
  "given"
1677
1688
  end
1678
- refuse_a_transferred_alias(kernel, arrays)
1689
+ # Only an operand opened at the xfer tier can be refused, and the tier
1690
+ # is in the key; which arrays share a root is not, and is asked here.
1691
+ refuse_a_transferred_alias(kernel, arrays) if plan.transferred
1679
1692
  kernel.call(arrays, scalars, pairs.map(&:first), c_functions)
1680
1693
  into || kernel
1681
1694
  end
1682
1695
 
1696
+ # What an indexed pass works out from its operands without their data
1697
+ # or their shapes: the kernel, and whether any operand is opened at the
1698
+ # xfer tier. The shapes are the kernel's own plan to keep, since they
1699
+ # are what its bounds are checked against (CompiledKernel#call).
1700
+ #
1701
+ # @private
1702
+ IndexedRunPlan = Data.define(:kernel, :transferred)
1703
+
1704
+ # What two calls of one indexed block have to agree on for one to run
1705
+ # the other's kernel: what #compile is keyed on, read off the operands
1706
+ # rather than built. Each operand's name and type, whether it is a
1707
+ # CScalar (read at its one cell rather than walked) and whether it has
1708
+ # a mask; and its tier, because whether a transferred operand has to be
1709
+ # refused depends on it. Not its shape: the kernel is the same over
1710
+ # any shape, and a caller that grows its arrays would miss on every
1711
+ # call. Not a scalar's value, for the reason #run_key gives.
1712
+ #
1713
+ # The leading symbol keeps these apart from an element-wise pass's
1714
+ # keys, which share the store.
1715
+ #
1716
+ # nil where a scalar is not one a kernel takes, so that the call is
1717
+ # refused where and as it always was rather than here.
1718
+ def indexed_run_key (arrays, scalars, c_functions, randoms, steps, into,
1719
+ reassociate)
1720
+ key = [:indexed, into ? true : false, reassociate, steps, arrays.size]
1721
+ arrays.each do |name, array|
1722
+ key << name << array.data_type << array.is_a?(CScalar) <<
1723
+ array.has_mask? << Access.tier(array)
1724
+ end
1725
+ key << scalars.size
1726
+ scalars.each do |name, value|
1727
+ key << name << TypeAssignment.scalar_type(value)
1728
+ end
1729
+ key << c_functions.size
1730
+ c_functions.each { |name, function| key << name << function.kernel_key }
1731
+ randoms.each { |name, generator| key << name << generator.generator }
1732
+ key
1733
+ rescue Unsupported
1734
+ nil
1735
+ end
1736
+
1737
+ # @private
1738
+ def plan_indexed_run (source, node, origin, arrays, scalars, c_functions,
1739
+ randoms, steps, into, reassociate)
1740
+ kernel = compile(source,
1741
+ node: node,
1742
+ origin: origin,
1743
+ array_names: arrays.keys,
1744
+ storage_types: arrays.transform_values(&:data_type_name),
1745
+ scalar_values: scalars,
1746
+ c_functions: c_functions,
1747
+ randoms: randoms,
1748
+ masked: arrays.each_value.any? { |array| array.has_mask? },
1749
+ steps: steps,
1750
+ cell_names: cell_names(arrays),
1751
+ map: into ? true : false,
1752
+ result: into ? INIT_TARGET : nil,
1753
+ reassociate: reassociate)
1754
+ transferred = arrays.each_value.any? { |array|
1755
+ Access.tier(array) == Access::TIER_XFER
1756
+ }
1757
+ IndexedRunPlan.new(kernel: kernel, transferred: transferred)
1758
+ end
1759
+
1683
1760
  # Compiles for one set of array data types and one set of scalar types,
1684
1761
  # and returns the same CompiledKernel for every later call with the
1685
1762
  # same ones. Nothing on this path is cheap relative to running the
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.6
4
+ version: 0.1.7
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.5
18
+ version: 3.0.6
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.5
28
+ version: 3.0.6
29
29
  - - "<"
30
30
  - !ruby/object:Gem::Version
31
31
  version: '3.1'
@@ -35,14 +35,14 @@ dependencies:
35
35
  requirements:
36
36
  - - ">="
37
37
  - !ruby/object:Gem::Version
38
- version: '0'
38
+ version: 1.1.1
39
39
  type: :runtime
40
40
  prerelease: false
41
41
  version_requirements: !ruby/object:Gem::Requirement
42
42
  requirements:
43
43
  - - ">="
44
44
  - !ruby/object:Gem::Version
45
- version: '0'
45
+ version: 1.1.1
46
46
  description: |
47
47
  Write the loop in Ruby, and it runs at the speed of C -- one to two orders
48
48
  of magnitude faster than interpreted. It is for what a CArray expression
@@ -125,6 +125,7 @@ files:
125
125
  - lib/carray/jit/errors.rb
126
126
  - lib/carray/jit/expression.rb
127
127
  - lib/carray/jit/kernel.rb
128
+ - lib/carray/jit/native.rb
128
129
  - lib/carray/jit/node.rb
129
130
  - lib/carray/jit/sorting_networks.rb
130
131
  - lib/carray/jit/sweep.rb
@@ -148,7 +149,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
148
149
  - !ruby/object:Gem::Version
149
150
  version: '0'
150
151
  requirements: []
151
- rubygems_version: 4.0.17
152
+ rubygems_version: 4.0.8
152
153
  specification_version: 4
153
154
  summary: JIT compilation of CArray kernels written in Ruby
154
155
  test_files: []