carray-jit 0.1.6 → 0.1.8

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.

Potentially problematic release.


This version of carray-jit might be problematic. Click here for more details.

Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +40 -0
  3. data/README.md +2 -2
  4. data/carray-jit.gemspec +7 -3
  5. data/docs/02_KernelShapes.md +7 -7
  6. data/docs/03_SupportedFeatures.md +28 -28
  7. data/docs/04_Compiling.md +5 -5
  8. data/docs/05_DesignNotes.md +1 -3
  9. data/docs/06_Cheatsheet.md +12 -12
  10. data/docs/07_StepByStep.ja.md +45 -45
  11. data/docs/07_StepByStep.md +45 -45
  12. data/examples/applications/alarm.rb +2 -2
  13. data/examples/applications/cubic_spline.rb +17 -17
  14. data/examples/applications/dithering.rb +3 -3
  15. data/examples/applications/group_stats.rb +4 -4
  16. data/examples/applications/heat_equation.rb +5 -5
  17. data/examples/applications/kepler.rb +10 -10
  18. data/examples/applications/lookup.rb +3 -3
  19. data/examples/applications/median_filter.rb +7 -7
  20. data/examples/applications/moving_average.rb +6 -6
  21. data/examples/applications/parcel_ascent.rb +10 -10
  22. data/examples/applications/partial_sums.rb +3 -3
  23. data/examples/applications/point_cloud.rb +4 -4
  24. data/examples/applications/point_in_polygon.rb +4 -4
  25. data/examples/applications/quicksort.rb +1 -1
  26. data/examples/applications/relaxation.rb +1 -1
  27. data/examples/applications/sensor_gaps.rb +3 -3
  28. data/examples/applications/sobel_edges.rb +5 -5
  29. data/examples/applications/van_der_pol.rb +3 -3
  30. data/examples/applications/wet_bulb.rb +4 -4
  31. data/examples/features/01_element_wise.rb +8 -8
  32. data/examples/features/02_stencil.rb +4 -4
  33. data/examples/features/03_recurrence.rb +1 -1
  34. data/examples/features/04_thomas.rb +8 -8
  35. data/examples/features/05_reduction.rb +7 -7
  36. data/examples/features/06_jit_contract.rb +9 -9
  37. data/examples/features/07_masks.rb +4 -4
  38. data/examples/features/08_views.rb +5 -5
  39. data/examples/features/09_inspecting.rb +1 -1
  40. data/examples/features/10_complex.rb +4 -4
  41. data/examples/features/11_c_functions.rb +17 -17
  42. data/examples/features/12_sweep.rb +19 -24
  43. data/examples/features/13_cscalar.rb +12 -12
  44. data/examples/features/14_stencil_window.rb +6 -6
  45. data/examples/features/15_loops.rb +6 -6
  46. data/examples/features/16_raising.rb +4 -4
  47. data/lib/carray/jit/analyzer.rb +39 -34
  48. data/lib/carray/jit/c_function.rb +133 -139
  49. data/lib/carray/jit/c_generator.rb +20 -11
  50. data/lib/carray/jit/call.rb +1 -1
  51. data/lib/carray/jit/compiler.rb +78 -43
  52. data/lib/carray/jit/expression.rb +68 -28
  53. data/lib/carray/jit/kernel.rb +23 -47
  54. data/lib/carray/jit/native.rb +149 -0
  55. data/lib/carray/jit/node.rb +1 -1
  56. data/lib/carray/jit/sweep.rb +14 -64
  57. data/lib/carray/jit/type_assignment.rb +9 -4
  58. data/lib/carray/jit/version.rb +1 -1
  59. data/lib/carray/jit.rb +103 -28
  60. metadata +8 -7
@@ -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
@@ -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, never a kind, so the two
873
- # have to be the same kind for anything to be given. `i32 * 2.0` is a
874
- # float64 and `f32 * 1i` a cmplx128 -- the scalar\'s own Ruby type, since
875
- # the array has no narrower one of that kind to offer. And a real
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)
@@ -1,5 +1,5 @@
1
1
  class CArray
2
2
  module JIT
3
- VERSION = "0.1.6"
3
+ VERSION = "0.1.8"
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|
@@ -1357,9 +1356,8 @@ class CArray
1357
1356
  # and keying on it would miss on every call of the loop this is for.
1358
1357
  #
1359
1358
  # 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.
1359
+ # process, and holding an array would hold its memory as long. A 1-D
1360
+ # shape is its `elements`, which builds no Array.
1363
1361
  #
1364
1362
  # nil where a scalar is not one a kernel takes, so that the call is
1365
1363
  # refused where and as it always was rather than here.
@@ -1369,11 +1367,10 @@ class CArray
1369
1367
  rank = array.ndim
1370
1368
  key << name << array.data_type << rank << array.is_a?(CScalar) <<
1371
1369
  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)
1370
+ if rank == 1
1371
+ key << array.elements
1372
+ else
1373
+ key.concat(array.shape)
1377
1374
  end
1378
1375
  end
1379
1376
  key << scalars.size
@@ -1526,6 +1523,16 @@ class CArray
1526
1523
  written = written_names.filter_map { |name| aligned[name] }
1527
1524
  return aligned if written.empty?
1528
1525
  roots = written.map { |array| root_of(array) }
1526
+ # Asked first without building anything, because the answer is nearly
1527
+ # always that nothing is shared, and this runs on every call. The
1528
+ # root is compared first since it is what nearly always says no.
1529
+ shared = aligned.any? { |name, array|
1530
+ next false if written_names.include?(name)
1531
+ root = root_of(array)
1532
+ roots.any? { |one| one.equal?(root) } &&
1533
+ written.none? { |one| one.equal?(array) }
1534
+ }
1535
+ return aligned unless shared
1529
1536
  aligned.to_h do |name, array|
1530
1537
  next [name, array] if written_names.include?(name)
1531
1538
  next [name, array] if written.any? { |other| other.equal?(array) }
@@ -1645,21 +1652,23 @@ class CArray
1645
1652
  end
1646
1653
  pairs = bounds(extents, extents.size)
1647
1654
  steps = pairs.map { |triple, _| triple[2] }
1655
+ reassociate = JIT.reassociate if reassociate.nil?
1656
+
1657
+ # Which kernel runs, and whether any operand would be transferred,
1658
+ # answer the same for two calls whose operands agree on name, type,
1659
+ # mask and tier -- so they are worked out once per such agreement,
1660
+ # as an element-wise pass does (#run_over_whole_arrays).
1661
+ key = indexed_run_key(arrays, scalars, c_functions, randoms, steps,
1662
+ into, reassociate)
1663
+ plan = key && found_run_plan(source, key)
1664
+ unless plan
1665
+ plan = plan_indexed_run(source, node, origin, arrays, scalars,
1666
+ c_functions, randoms, steps, into,
1667
+ reassociate)
1668
+ keep_run_plan(source, key, plan) if key
1669
+ end
1648
1670
 
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)
1671
+ kernel = plan.kernel
1663
1672
  # The kernel decides, not the caller: mentioning UNDEF makes it a
1664
1673
  # masked kernel even when no array carries a mask yet.
1665
1674
  if kernel.masked
@@ -1675,11 +1684,77 @@ class CArray
1675
1684
  "#{pairs.size} #{pairs.size == 1 ? 'extent was' : 'extents were'} " \
1676
1685
  "given"
1677
1686
  end
1678
- refuse_a_transferred_alias(kernel, arrays)
1687
+ # Only an operand opened at the xfer tier can be refused, and the tier
1688
+ # is in the key; which arrays share a root is not, and is asked here.
1689
+ refuse_a_transferred_alias(kernel, arrays) if plan.transferred
1679
1690
  kernel.call(arrays, scalars, pairs.map(&:first), c_functions)
1680
1691
  into || kernel
1681
1692
  end
1682
1693
 
1694
+ # What an indexed pass works out from its operands without their data
1695
+ # or their shapes: the kernel, and whether any operand is opened at the
1696
+ # xfer tier. The shapes are the kernel's own plan to keep, since they
1697
+ # are what its bounds are checked against (CompiledKernel#call).
1698
+ #
1699
+ # @private
1700
+ IndexedRunPlan = Data.define(:kernel, :transferred)
1701
+
1702
+ # What two calls of one indexed block have to agree on for one to run
1703
+ # the other's kernel: what #compile is keyed on, read off the operands
1704
+ # rather than built. Each operand's name and type, whether it is a
1705
+ # CScalar (read at its one cell rather than walked) and whether it has
1706
+ # a mask; and its tier, because whether a transferred operand has to be
1707
+ # refused depends on it. Not its shape: the kernel is the same over
1708
+ # any shape, and a caller that grows its arrays would miss on every
1709
+ # call. Not a scalar's value, for the reason #run_key gives.
1710
+ #
1711
+ # The leading symbol keeps these apart from an element-wise pass's
1712
+ # keys, which share the store.
1713
+ #
1714
+ # nil where a scalar is not one a kernel takes, so that the call is
1715
+ # refused where and as it always was rather than here.
1716
+ def indexed_run_key (arrays, scalars, c_functions, randoms, steps, into,
1717
+ reassociate)
1718
+ key = [:indexed, into ? true : false, reassociate, steps, arrays.size]
1719
+ arrays.each do |name, array|
1720
+ key << name << array.data_type << array.is_a?(CScalar) <<
1721
+ array.has_mask? << Access.tier(array)
1722
+ end
1723
+ key << scalars.size
1724
+ scalars.each do |name, value|
1725
+ key << name << TypeAssignment.scalar_type(value)
1726
+ end
1727
+ key << c_functions.size
1728
+ c_functions.each { |name, function| key << name << function.kernel_key }
1729
+ randoms.each { |name, generator| key << name << generator.generator }
1730
+ key
1731
+ rescue Unsupported
1732
+ nil
1733
+ end
1734
+
1735
+ # @private
1736
+ def plan_indexed_run (source, node, origin, arrays, scalars, c_functions,
1737
+ randoms, steps, into, reassociate)
1738
+ kernel = compile(source,
1739
+ node: node,
1740
+ origin: origin,
1741
+ array_names: arrays.keys,
1742
+ storage_types: arrays.transform_values(&:data_type_name),
1743
+ scalar_values: scalars,
1744
+ c_functions: c_functions,
1745
+ randoms: randoms,
1746
+ masked: arrays.each_value.any? { |array| array.has_mask? },
1747
+ steps: steps,
1748
+ cell_names: cell_names(arrays),
1749
+ map: into ? true : false,
1750
+ result: into ? INIT_TARGET : nil,
1751
+ reassociate: reassociate)
1752
+ transferred = arrays.each_value.any? { |array|
1753
+ Access.tier(array) == Access::TIER_XFER
1754
+ }
1755
+ IndexedRunPlan.new(kernel: kernel, transferred: transferred)
1756
+ end
1757
+
1683
1758
  # Compiles for one set of array data types and one set of scalar types,
1684
1759
  # and returns the same CompiledKernel for every later call with the
1685
1760
  # same ones. Nothing on this path is cheap relative to running the
@@ -1974,7 +2049,7 @@ class CArray
1974
2049
  }
1975
2050
  return unless shadowed
1976
2051
  raise Unsupported,
1977
- "`#{shadowed}` is made in this block with `CArray.double` or " \
2052
+ "`#{shadowed}` is made in this block with `CArray.float64` or " \
1978
2053
  "its kin, and the block also closes over an array named " \
1979
2054
  "`#{shadowed}`; in this spelling an assignment writes that " \
1980
2055
  "array's cell, so the line means one thing to a reader and " \
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.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - himotoyoshi
@@ -15,34 +15,34 @@ dependencies:
15
15
  requirements:
16
16
  - - ">="
17
17
  - !ruby/object:Gem::Version
18
- version: 3.0.5
18
+ version: 3.1.0
19
19
  - - "<"
20
20
  - !ruby/object:Gem::Version
21
- version: '3.1'
21
+ version: '3.2'
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  requirements:
26
26
  - - ">="
27
27
  - !ruby/object:Gem::Version
28
- version: 3.0.5
28
+ version: 3.1.0
29
29
  - - "<"
30
30
  - !ruby/object:Gem::Version
31
- version: '3.1'
31
+ version: '3.2'
32
32
  - !ruby/object:Gem::Dependency
33
33
  name: fiddle
34
34
  requirement: !ruby/object:Gem::Requirement
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