carray-jit 0.1.7 → 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 (55) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +18 -0
  3. data/README.md +2 -2
  4. data/carray-jit.gemspec +4 -2
  5. data/docs/02_KernelShapes.md +5 -5
  6. data/docs/03_SupportedFeatures.md +27 -27
  7. data/docs/04_Compiling.md +1 -1
  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 +38 -33
  48. data/lib/carray/jit/c_generator.rb +1 -1
  49. data/lib/carray/jit/call.rb +1 -1
  50. data/lib/carray/jit/compiler.rb +34 -17
  51. data/lib/carray/jit/node.rb +1 -1
  52. data/lib/carray/jit/type_assignment.rb +9 -4
  53. data/lib/carray/jit/version.rb +1 -1
  54. data/lib/carray/jit.rb +7 -9
  55. metadata +6 -6
@@ -20,8 +20,8 @@ EDGES = 200
20
20
  POINTS = 1_000_000
21
21
 
22
22
  # A lobed blob, closed, going round once.
23
- corner_x = CArray.double(EDGES)
24
- corner_y = CArray.double(EDGES)
23
+ corner_x = CArray.float64(EDGES)
24
+ corner_y = CArray.float64(EDGES)
25
25
  CArray.jit_for(EDGES) { |k|
26
26
  angle = 2.0 * Math::PI * k / EDGES
27
27
  radius = 1.0 + 0.35 * Math.sin(7.0 * angle) + 0.15 * Math.cos(3.0 * angle)
@@ -30,10 +30,10 @@ CArray.jit_for(EDGES) { |k|
30
30
  }
31
31
 
32
32
  random = CArray::Rng.new(seed: 20260913)
33
- point_x = CArray.double(POINTS)
33
+ point_x = CArray.float64(POINTS)
34
34
  point_x.random!(rng: random)
35
35
  point_x = point_x * 3.2 - 1.6
36
- point_y = CArray.double(POINTS)
36
+ point_y = CArray.float64(POINTS)
37
37
  point_y.random!(rng: random)
38
38
  point_y = point_y * 3.2 - 1.6
39
39
 
@@ -48,7 +48,7 @@ quicksort = CArray.jit_function(
48
48
 
49
49
  n = 200_000
50
50
  random = Random.new(20260905)
51
- values = CArray.double(n) { |i| random.rand }
51
+ values = CArray.float64(n) { |i| random.rand }
52
52
 
53
53
  sorted = values.copy # `to_ca` would answer this array
54
54
  quicksort.call(sorted, 0, n - 1)
@@ -19,7 +19,7 @@ ROWS, COLUMNS = 120, 160
19
19
  SWEEPS = 1200
20
20
 
21
21
  def fresh_plate
22
- plate = CArray.double(ROWS, COLUMNS)
22
+ plate = CArray.float64(ROWS, COLUMNS)
23
23
  plate[nil, 0] = 25.0 # the sides, somewhere in between
24
24
  plate[nil, -1] = 25.0
25
25
  plate[0, nil] = 100.0 # the top edge is hot, corners included
@@ -18,7 +18,7 @@ days, hours = 30, 24
18
18
  random = Random.new(20260902)
19
19
 
20
20
  # Hourly temperature: a daily cycle, a seasonal drift, and noise.
21
- reading = CArray.double(days, hours) { |d, h|
21
+ reading = CArray.float64(days, hours) { |d, h|
22
22
  15.0 + 8.0 * Math.sin((h - 9) * Math::PI / 12) + d * 0.1 +
23
23
  random.rand(-1.0..1.0)
24
24
  }
@@ -57,7 +57,7 @@ puts " #{reading.count_masked - dropped} readings rejected as out of range"
57
57
  # 2. Fill a hole from its neighbours, but only where both neighbours are there.
58
58
  # Where they are not, the average reads a masked cell and the result comes
59
59
  # out masked by itself -- the rule does not have to be written twice.
60
- filled = CArray.double(days, hours)
60
+ filled = CArray.float64(days, hours)
61
61
  CArray.jit_for(days, 1...(hours-1)) { |d, h|
62
62
  if reading[d, h] == UNDEF
63
63
  filled[d, h] = 0.5 * (reading[d, h-1] + reading[d, h+1])
@@ -73,7 +73,7 @@ puts " #{recovered} holes interpolated, #{filled.count_masked} still missing"
73
73
 
74
74
  # 3. The daily mean, from the readings that survived -- and a day with fewer
75
75
  # than twenty valid hours does not get a mean at all.
76
- mean = CArray.double(days)
76
+ mean = CArray.float64(days)
77
77
  valid = CArray.int32(days)
78
78
  CArray.jit_for(days) { |d|
79
79
  total = 0.0
@@ -23,7 +23,7 @@ end
23
23
 
24
24
  # A synthetic picture: a disc, a bar, and a soft background gradient.
25
25
  rows, columns = 40, 96
26
- image = CArray.double(rows, columns)
26
+ image = CArray.float64(rows, columns)
27
27
  rows.times do |i|
28
28
  columns.times do |j|
29
29
  y = (i - 20.0) / 16.0
@@ -35,7 +35,7 @@ rows.times do |i|
35
35
  end
36
36
  end
37
37
 
38
- edges = CArray.double(rows, columns)
38
+ edges = CArray.float64(rows, columns)
39
39
  sobel(image, edges)
40
40
 
41
41
  def draw (field, title)
@@ -54,15 +54,15 @@ draw(edges, "edges")
54
54
  # On an image of a size you would actually filter -- the same filter, and the
55
55
  # same loop written in Ruby, both over the whole thing.
56
56
  size = 512
57
- large = CArray.double(size, size) { |i, j| Math.sin(i * 0.03) * Math.cos(j * 0.02) }
58
- result = CArray.double(size, size)
57
+ large = CArray.float64(size, size) { |i, j| Math.sin(i * 0.03) * Math.cos(j * 0.02) }
58
+ result = CArray.float64(size, size)
59
59
 
60
60
  sobel(large, result) # compile it before timing it
61
61
  started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
62
62
  5.times { sobel(large, result) }
63
63
  compiled = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - started) / 5
64
64
 
65
- reference = CArray.double(size, size)
65
+ reference = CArray.float64(size, size)
66
66
  started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
67
67
  (1...(size-1)).each do |i|
68
68
  (1...(size-1)).each do |j|
@@ -26,9 +26,9 @@ FINISH = 20.0
26
26
  TOLERANCE = 1.0e-8
27
27
  CAP = 200_000 # a `while` in a kernel carries a bound
28
28
 
29
- stiffness = CArray.double(TRAJECTORIES).seq!(0.1, 40.0 / TRAJECTORIES)
30
- position = CArray.double(TRAJECTORIES)
31
- velocity = CArray.double(TRAJECTORIES)
29
+ stiffness = CArray.float64(TRAJECTORIES).seq!(0.1, 40.0 / TRAJECTORIES)
30
+ position = CArray.float64(TRAJECTORIES)
31
+ velocity = CArray.float64(TRAJECTORIES)
32
32
  accepted = CArray.int32(TRAJECTORIES)
33
33
  rejected = CArray.int32(TRAJECTORIES)
34
34
 
@@ -29,8 +29,8 @@ CAP = 50 # a `while` in a kernel takes a cap
29
29
 
30
30
  # Stand-ins for the fields that would have been read: a pole-to-pole
31
31
  # temperature gradient with a wave in it, and humidity that varies across it.
32
- temperature = CArray.double(ROWS, COLUMNS)
33
- humidity = CArray.double(ROWS, COLUMNS)
32
+ temperature = CArray.float64(ROWS, COLUMNS)
33
+ humidity = CArray.float64(ROWS, COLUMNS)
34
34
  CArray.jit_for(ROWS, COLUMNS) { |j, i|
35
35
  latitude = 90.0 - 180.0 * j / (ROWS - 1)
36
36
  temperature[j, i] = 30.0 - 55.0 * (latitude / 90.0).abs +
@@ -38,8 +38,8 @@ CArray.jit_for(ROWS, COLUMNS) { |j, i|
38
38
  humidity[j, i] = 35.0 + 60.0 * (0.5 + 0.5 * Math.cos(i / 30.0 + j / 60.0))
39
39
  }
40
40
 
41
- dew_point = CArray.double(ROWS, COLUMNS)
42
- wet_bulb = CArray.double(ROWS, COLUMNS)
41
+ dew_point = CArray.float64(ROWS, COLUMNS)
42
+ wet_bulb = CArray.float64(ROWS, COLUMNS)
43
43
  rounds_taken = CArray.int8(ROWS, COLUMNS)
44
44
 
45
45
  # Everything in one walk. `vapour` and `gamma` are the cell's own values and
@@ -14,10 +14,10 @@ $LOAD_PATH.unshift(File.expand_path("../../lib", __dir__))
14
14
  require "carray/jit"
15
15
 
16
16
  n = 100_000
17
- a = CArray.double(n).seq!(1.0)
18
- b = CArray.double(n).seq!(0.5, 0.5)
19
- c = CArray.double(n).seq!(2.0, -0.25)
20
- out = CArray.double(n)
17
+ a = CArray.float64(n).seq!(1.0)
18
+ b = CArray.float64(n).seq!(0.5, 0.5)
19
+ c = CArray.float64(n).seq!(2.0, -0.25)
20
+ out = CArray.float64(n)
21
21
 
22
22
  CArray.jit_each { out = (a + b) * (c - a) + b * c - a }
23
23
 
@@ -32,9 +32,9 @@ puts " matches CArray #{out.to_a == expected.to_a}"
32
32
  # The rank comes from the arrays rather than from the block, so the same
33
33
  # expression serves whatever shape it is given, and broadcasting costs nothing
34
34
  # -- a stretched axis arrives as a stride of zero.
35
- matrix = CArray.double(3, 4).seq!(1.0)
36
- row = CArray.double(1, 4).seq!(10.0, 10.0)
37
- scaled = CArray.double(3, 4)
35
+ matrix = CArray.float64(3, 4).seq!(1.0)
36
+ row = CArray.float64(1, 4).seq!(10.0, 10.0)
37
+ scaled = CArray.float64(3, 4)
38
38
 
39
39
  CArray.jit_each { scaled = matrix * row }
40
40
 
@@ -64,6 +64,6 @@ puts " closing over a value #{blended[0..2].to_a.inspect}"
64
64
  # An assignment is a statement with a value -- Ruby's rule, not a special
65
65
  # case here -- so a block may write an array of yours and hand the same value
66
66
  # back. Which is jit_each's block, asked a different question.
67
- kept = CArray.double(n)
67
+ kept = CArray.float64(n)
68
68
  also = CArray.jit_map { kept = a - b }
69
69
  puts " written and returned #{kept[0..2].to_a == also[0..2].to_a}"
@@ -9,14 +9,14 @@ $LOAD_PATH.unshift(File.expand_path("../../lib", __dir__))
9
9
  require "carray/jit"
10
10
 
11
11
  rows, columns = 6, 8
12
- src = CArray.double(rows, columns).seq!(1.0)
13
- out = CArray.double(rows, columns)
12
+ src = CArray.float64(rows, columns).seq!(1.0)
13
+ out = CArray.float64(rows, columns)
14
14
 
15
15
  CArray.jit_for(1...(rows-1), 1...(columns-1)) { |i, j|
16
16
  out[i, j] = 0.25 * (src[i-1, j] + src[i+1, j] + src[i, j-1] + src[i, j+1])
17
17
  }
18
18
 
19
- reference = CArray.double(rows, columns)
19
+ reference = CArray.float64(rows, columns)
20
20
  (1...(rows-1)).each do |i|
21
21
  (1...(columns-1)).each do |j|
22
22
  reference[i, j] = 0.25 * (src[i-1, j] + src[i+1, j] + src[i, j-1] + src[i, j+1])
@@ -30,7 +30,7 @@ puts " border untouched #{out[0, nil].to_a.all?(0.0)}"
30
30
  # An extent may step by more than one, which changes which cells are written
31
31
  # and therefore which offsets are dependencies: with a step of two, a[i-1] is
32
32
  # a cell this loop never writes.
33
- values = CArray.double(10).seq!(1.0)
33
+ values = CArray.float64(10).seq!(1.0)
34
34
  CArray.jit_for((2...10).step(2)) { |i| values[i] = values[i-1] * 100.0 }
35
35
  puts " every other cell #{values.to_a.inspect}"
36
36
 
@@ -11,7 +11,7 @@ require "carray/jit"
11
11
  # Legendre polynomials at x, by their three-term recurrence.
12
12
  n = 24
13
13
  x = 0.5
14
- legendre = CArray.double(n)
14
+ legendre = CArray.float64(n)
15
15
  legendre[0] = 1.0
16
16
  legendre[1] = x
17
17
 
@@ -18,16 +18,16 @@ random = Random.new(20260902)
18
18
 
19
19
  # A diagonally dominant tridiagonal system: a is the subdiagonal, b the
20
20
  # diagonal, c the superdiagonal, d the right-hand side.
21
- a = CArray.double(n) { random.rand(-1.0..1.0) }
22
- c = CArray.double(n) { random.rand(-1.0..1.0) }
23
- b = CArray.double(n) { |i| 4.0 + random.rand }
24
- d = CArray.double(n) { random.rand(-1.0..1.0) }
21
+ a = CArray.float64(n) { random.rand(-1.0..1.0) }
22
+ c = CArray.float64(n) { random.rand(-1.0..1.0) }
23
+ b = CArray.float64(n) { |i| 4.0 + random.rand }
24
+ d = CArray.float64(n) { random.rand(-1.0..1.0) }
25
25
  a[0] = 0.0
26
26
  c[n-1] = 0.0
27
27
 
28
- cc = CArray.double(n)
29
- dd = CArray.double(n)
30
- x = CArray.double(n)
28
+ cc = CArray.float64(n)
29
+ dd = CArray.float64(n)
30
+ x = CArray.float64(n)
31
31
 
32
32
  cc[0] = c[0] / b[0]
33
33
  dd[0] = d[0] / b[0]
@@ -62,7 +62,7 @@ puts " max |A x - d| #{format('%.3e', residual)}"
62
62
  # not back substitution either: each cell reads an x[i+1] the sweep has not
63
63
  # reached yet. It is the answer the same Ruby loop gives, which is the only
64
64
  # thing this promises.
65
- wrong_way = CArray.double(n)
65
+ wrong_way = CArray.float64(n)
66
66
  wrong_way[n-1] = dd[n-1]
67
67
  CArray.jit_for(0...(n-1)) { |i| wrong_way[i] = dd[i] - cc[i] * wrong_way[i+1] }
68
68
 
@@ -10,9 +10,9 @@ $LOAD_PATH.unshift(File.expand_path("../../lib", __dir__))
10
10
  require "carray/jit"
11
11
 
12
12
  rows, columns = 5, 7
13
- source = CArray.double(rows, columns).seq!(1.0)
14
- total = CArray.double(rows)
15
- largest = CArray.double(rows)
13
+ source = CArray.float64(rows, columns).seq!(1.0)
14
+ total = CArray.float64(rows)
15
+ largest = CArray.float64(rows)
16
16
  positives = CArray.int32(rows)
17
17
 
18
18
  CArray.jit_for(rows) { |i|
@@ -49,9 +49,9 @@ puts " count #{positives.to_a.inspect}"
49
49
 
50
50
  # A matrix multiply is the same shape of thing: two output indices and one
51
51
  # inner loop.
52
- left = CArray.double(3, 4).seq!(1.0)
53
- right = CArray.double(4, 2).seq!(0.5, 0.5)
54
- product = CArray.double(3, 2)
52
+ left = CArray.float64(3, 4).seq!(1.0)
53
+ right = CArray.float64(4, 2).seq!(0.5, 0.5)
54
+ product = CArray.float64(3, 2)
55
55
 
56
56
  CArray.jit_for(3, 2) { |i, j|
57
57
  accumulator = 0.0
@@ -79,7 +79,7 @@ puts
79
79
  # Counting from zero is what an inner loop mostly does, and `n.times` says it
80
80
  # without naming an end. It compiles to the loop the range spells out --
81
81
  # `(0...n).each` -- so the choice is the reader's, not the compiler's.
82
- counted = CArray.double(rows)
82
+ counted = CArray.float64(rows)
83
83
  CArray.jit_for(rows) { |i|
84
84
  accumulator = 0.0
85
85
  columns.times { |j| accumulator = accumulator + source[i, j] }
@@ -8,12 +8,12 @@
8
8
  $LOAD_PATH.unshift(File.expand_path("../../lib", __dir__))
9
9
  require "carray/jit"
10
10
 
11
- a = CArray.double(3, 4).seq!(1.0)
12
- b = CArray.double(4, 2).seq!(0.5, 0.5)
13
- v = CArray.double(4).seq!(1.0)
14
- p = CArray.double(3).seq!(1.0)
15
- r = CArray.double(2).seq!(10.0, 10.0)
16
- q = CArray.double(3, 3).seq!(1.0)
11
+ a = CArray.float64(3, 4).seq!(1.0)
12
+ b = CArray.float64(4, 2).seq!(0.5, 0.5)
13
+ v = CArray.float64(4).seq!(1.0)
14
+ p = CArray.float64(3).seq!(1.0)
15
+ r = CArray.float64(2).seq!(10.0, 10.0)
16
+ q = CArray.float64(3, 3).seq!(1.0)
17
17
 
18
18
  matmul = CArray.jit_contract { |i, j, k| a[i,k] * b[k,j] } # k is summed
19
19
  matvec = CArray.jit_contract { |i, k| a[i,k] * v[k] } # k is summed
@@ -36,14 +36,14 @@ puts " swapped #{transposed.dim.inspect} vs #{matmul.dim.inspect}"
36
36
 
37
37
  # Assigning into an array of your own says where to put it instead. It must
38
38
  # name exactly the free indices, and still does not decide what is summed.
39
- destination = CArray.double(3, 2)
39
+ destination = CArray.float64(3, 2)
40
40
  CArray.jit_contract { |i, j, k| destination[i,j] = a[i,k] * b[k,j] }
41
41
  puts " into mine #{destination.to_a == matmul.to_a}"
42
42
 
43
43
  # A sum along an axis is not a contraction: there is nothing in a[i,k]
44
44
  # standing in for a sigma.
45
45
  begin
46
- total = CArray.double(3)
46
+ total = CArray.float64(3)
47
47
  CArray.jit_contract { |i, k| total[i] = a[i,k] }
48
48
  rescue CArray::JIT::Unsupported => error
49
49
  puts " refused: #{error.message.lines.first.strip}"
@@ -51,7 +51,7 @@ end
51
51
 
52
52
  # Nor is a shape mismatch let through.
53
53
  begin
54
- wrong = CArray.double(5, 2)
54
+ wrong = CArray.float64(5, 2)
55
55
  CArray.jit_contract { |i, j, k| a[i,k] * wrong[k,j] }
56
56
  rescue CArray::JIT::Unsupported => error
57
57
  puts " refused: #{error.message.lines.first.strip}"
@@ -9,12 +9,12 @@ $LOAD_PATH.unshift(File.expand_path("../../lib", __dir__))
9
9
  require "carray/jit"
10
10
 
11
11
  n = 7
12
- source = CArray.double(n).seq!(1.0)
12
+ source = CArray.float64(n).seq!(1.0)
13
13
  source[2] = UNDEF
14
14
 
15
15
  # Filling the holes. This is an ordinary Ruby loop as well, so it can be
16
16
  # checked against one.
17
- filled = CArray.double(n)
17
+ filled = CArray.float64(n)
18
18
  CArray.jit_for(n) { |i|
19
19
  if source[i] == UNDEF
20
20
  filled[i] = 0.0
@@ -36,14 +36,14 @@ puts " cells missing #{filled.count_masked} (a mask exists, but it is empty
36
36
  # any cell that fed a result masks that result, following the offsets, so each
37
37
  # output takes its mask from its own inputs rather than from everything the
38
38
  # body read.
39
- neighbours = CArray.double(n)
39
+ neighbours = CArray.float64(n)
40
40
  CArray.jit_for(1...(n-1)) { |i| neighbours[i] = source[i-1] + source[i+1] }
41
41
  puts " propagated #{neighbours.to_a.inspect}"
42
42
  puts " masked where #{(0...n).select { |i| neighbours[i] == UNDEF }.inspect} (the cells reading source[2])"
43
43
 
44
44
  # Writing UNDEF marks a cell missing. Mentioning UNDEF at all makes the kernel
45
45
  # a masked one, whatever its arrays happen to carry.
46
- clipped = CArray.double(n).seq!(1.0)
46
+ clipped = CArray.float64(n).seq!(1.0)
47
47
  CArray.jit_for(n) { |i|
48
48
  if clipped[i] > 4.0
49
49
  clipped[i] = UNDEF
@@ -10,7 +10,7 @@ $LOAD_PATH.unshift(File.expand_path("../../lib", __dir__))
10
10
  require "carray/jit"
11
11
 
12
12
  # A column: its stride is the row pitch, not the element size.
13
- matrix = CArray.double(8, 3)
13
+ matrix = CArray.float64(8, 3)
14
14
  column = matrix[nil, 1]
15
15
  column[0] = 1.0
16
16
 
@@ -22,15 +22,15 @@ puts " others untouched #{matrix[nil, 0].to_a.all?(0.0) && matrix[nil, 2].to_a
22
22
 
23
23
  # A reversed view has a negative stride, and a slice of a slice folds to one
24
24
  # offset and one stride however many times it was sliced.
25
- array = CArray.double(20).seq!
25
+ array = CArray.float64(20).seq!
26
26
  inner = array[4..15][2..9]
27
27
  CArray.jit_for(8) { |i| inner[i] = -inner[i] }
28
28
  puts " slice of a slice #{array[5..14].to_a.inspect}"
29
29
 
30
30
  # A transpose is read at its own indices; nothing is materialised.
31
- source = CArray.double(4, 3).seq!(1.0)
31
+ source = CArray.float64(4, 3).seq!(1.0)
32
32
  transposed = source.transpose
33
- result = CArray.double(3, 4)
33
+ result = CArray.float64(3, 4)
34
34
  CArray.jit_for(*transposed.dim) { |i, j| result[i, j] = transposed[i, j] * 2.0 }
35
35
  puts " transposed read #{result.to_a == (source.transpose * 2.0).to_a}"
36
36
 
@@ -39,7 +39,7 @@ puts " transposed read #{result.to_a == (source.transpose * 2.0).to_a}"
39
39
  # back. The cost is proportional to the region, not to the view: the kernel
40
40
  # knows its extents and its offsets before it runs, so it asks for the box it
41
41
  # will touch and no more.
42
- whole = CArray.double(64).seq!
42
+ whole = CArray.float64(64).seq!
43
43
  gathered = whole[whole >= 0.0]
44
44
  gathered[10] = 1.0
45
45
  CArray.jit_for(11...14) { |i| gathered[i] = gathered[i-1] * 2.0 }
@@ -8,7 +8,7 @@
8
8
  $LOAD_PATH.unshift(File.expand_path("../../lib", __dir__))
9
9
  require "carray/jit"
10
10
 
11
- values = CArray.double(16).seq!(1.0)
11
+ values = CArray.float64(16).seq!(1.0)
12
12
 
13
13
  def sweep (values)
14
14
  CArray.jit_for(1...values.dim[0]) { |i| values[i] = Math.sqrt(values[i-1]) + 1.0 }
@@ -26,16 +26,16 @@ puts " matches Ruby #{(0...n).all? { |i| rotated[i] == reference[i] }}"
26
26
  # The way out of the complex type. `abs`, `real`, `imag` and `arg` hand back
27
27
  # a Float, which is what lets a complex kernel write into a real array;
28
28
  # `conjugate` stays complex.
29
- power = CArray.double(n)
30
- phase = CArray.double(n)
29
+ power = CArray.float64(n)
30
+ phase = CArray.float64(n)
31
31
  CArray.jit_for(n) { |i| power[i] = signal[i].abs }
32
32
  CArray.jit_for(n) { |i| phase[i] = signal[i].arg }
33
33
  puts " power #{power.to_a.map { |e| e.round(4) }.inspect}"
34
34
  puts " phase #{phase.to_a.map { |e| e.round(4) }.inspect}"
35
35
 
36
36
  # And the way in, from two real arrays.
37
- real = CArray.double(n).seq!(1.0)
38
- imaginary = CArray.double(n).seq!(0.0, 0.25)
37
+ real = CArray.float64(n).seq!(1.0)
38
+ imaginary = CArray.float64(n).seq!(0.0, 0.25)
39
39
  built = CArray.cmplx128(n)
40
40
  CArray.jit_for(n) { |i| built[i] = Complex(real[i], imaginary[i]) }
41
41
  puts " built from parts #{built.to_a.first(3).inspect}"
@@ -27,8 +27,8 @@ require "benchmark"
27
27
  j0 = CArray.jit_extern("double j0(double)")
28
28
  puts "bound: #{j0}"
29
29
 
30
- x = CArray.double(6).seq!(1.0)
31
- out = CArray.double(6)
30
+ x = CArray.float64(6).seq!(1.0)
31
+ out = CArray.float64(6)
32
32
 
33
33
  CArray.jit_each { out = j0.call(x) }
34
34
  puts "j0(x) #{out.to_a.map { |v| v.round(6) }.inspect}"
@@ -41,7 +41,7 @@ puts
41
41
  # intermediate results to hold them, so this is not expressible as a map --
42
42
  # but it is an ordinary kernel.
43
43
 
44
- smoothed = CArray.double(6)
44
+ smoothed = CArray.float64(6)
45
45
  CArray.jit_for(1...6) { |i| smoothed[i] = 0.5 * (j0.call(x[i]) + j0.call(x[i-1])) }
46
46
  puts "half-sum of neighbouring j0"
47
47
  puts " #{smoothed[1..5].to_a.map { |v| v.round(6) }.inspect}"
@@ -50,8 +50,8 @@ puts
50
50
  # It mixes into an expression like anything else, and takes as many arguments
51
51
  # as the prototype says.
52
52
  atan2 = CArray.jit_extern("double atan2(double, double)")
53
- y = CArray.double(6).seq!(0.5, 0.5)
54
- mixed = CArray.double(6)
53
+ y = CArray.float64(6).seq!(0.5, 0.5)
54
+ mixed = CArray.float64(6)
55
55
  CArray.jit_each { mixed = atan2.call(x, y) * 2.0 - j0.call(x) }
56
56
  puts "atan2(x, y) * 2 - j0(x)"
57
57
  puts " #{mixed.to_a.map { |v| v.round(6) }.inspect}"
@@ -106,10 +106,10 @@ puts
106
106
 
107
107
  # And it is called from a kernel like any other, which is what gives kernels
108
108
  # something they did not have: a body you can factor and name.
109
- w = CArray.double(6).seq!(-0.2, 0.3)
110
- blended = CArray.double(6)
111
- low = CArray.double(6) { 10.0 }
112
- high = CArray.double(6) { 20.0 }
109
+ w = CArray.float64(6).seq!(-0.2, 0.3)
110
+ blended = CArray.float64(6)
111
+ low = CArray.float64(6) { 10.0 }
112
+ high = CArray.float64(6) { 20.0 }
113
113
  CArray.jit_each {
114
114
  blended = low + (high - low) * smoothstep.call(w)
115
115
  }
@@ -140,7 +140,7 @@ puts
140
140
  total = CArray.jit_function("double total(int n, const double v[8])") { |n, v|
141
141
  n == 0 ? 0.0 : v[n - 1] + total.call(n - 1, v)
142
142
  }
143
- values = CArray.double(8).seq!(1.0)
143
+ values = CArray.float64(8).seq!(1.0)
144
144
  puts "sum of #{values.to_a.map(&:to_i).inspect} by recursion #{total.call(8, values)}"
145
145
  puts " matches Ruby #{total.call(8, values) == total.block.call(8, values)}"
146
146
  puts
@@ -194,7 +194,7 @@ puts
194
194
  poly = CArray.jit_function("double (*)(double x, const double coef[3])") { |x, c|
195
195
  c[0] + c[1] * x + c[2] * x * x
196
196
  }
197
- coef = CArray.double(3) { |i| [1.0, 2.0, 3.0][i] }
197
+ coef = CArray.float64(3) { |i| [1.0, 2.0, 3.0][i] }
198
198
  puts "1 + 2x + 3x^2 at x = 2"
199
199
  puts " compiled C #{poly.call(2.0, coef)}"
200
200
  puts " same block in Ruby #{poly.block.call(2.0, coef)}"
@@ -210,8 +210,8 @@ ode = CArray.jit_function(
210
210
  dydt[1] = -y[0]
211
211
  0
212
212
  }
213
- state = CArray.double(2) { |i| [1.0, 0.0][i] }
214
- derivative = CArray.double(2)
213
+ state = CArray.float64(2) { |i| [1.0, 0.0][i] }
214
+ derivative = CArray.float64(2)
215
215
  ode.call(0.0, state, derivative, nil)
216
216
  puts "harmonic oscillator: y = #{state.to_a.inspect} -> dy/dt = #{derivative.to_a.inspect}"
217
217
  puts ode.c_source.lines.last(6).join
@@ -221,8 +221,8 @@ puts ode.c_source.lines.last(6).join
221
221
  # `x[i]` is a cell and `coef` is the whole array. Which is meant comes from
222
222
  # the declaration rather than from the spelling.
223
223
 
224
- ramp = CArray.double(6).seq!(0.0, 0.5)
225
- fitted = CArray.double(6)
224
+ ramp = CArray.float64(6).seq!(0.0, 0.5)
225
+ fitted = CArray.float64(6)
226
226
  CArray.jit_for(6) { |i| fitted[i] = poly.call(ramp[i], coef) }
227
227
  puts "1 + 2x + 3x^2 along a ramp"
228
228
  puts " #{fitted.to_a.map { |v| v.round(4) }.inspect}"
@@ -235,8 +235,8 @@ puts
235
235
  # cell at a time, or find a Math method that happens to exist.
236
236
 
237
237
  n = 200_000
238
- big = CArray.double(n).seq!(1.0, 1e-5)
239
- big_out = CArray.double(n)
238
+ big = CArray.float64(n).seq!(1.0, 1e-5)
239
+ big_out = CArray.float64(n)
240
240
  gamma = CArray.jit_extern("double tgamma(double)")
241
241
 
242
242
  def timed
@@ -1,10 +1,9 @@
1
1
  # Letting CArray drive the loop.
2
2
  #
3
3
  # An element-wise pass is the one shape that is a *sweep*: nothing reaches a
4
- # neighbour, nothing chooses an order. So where this CArray has
5
- # `ca_call_cslab` (3.0.1 and later), jit_each hands it the compiled body
6
- # and lets it acquire the operands -- broadcasting them, ORing and propagating
7
- # the masks, and calling back a chunk at a time.
4
+ # neighbour, nothing chooses an order. So jit_each hands the compiled body to
5
+ # CArray's `ca_call_cslab` and lets it acquire the operands -- broadcasting
6
+ # them, ORing and propagating the masks, and calling back a chunk at a time.
8
7
  #
9
8
  # Nothing about the expression changes, and nothing here is written
10
9
  # differently. What changes is who opens the arrays, and what that costs.
@@ -13,12 +12,8 @@ $LOAD_PATH.unshift(File.expand_path("../../lib", __dir__))
13
12
  require "carray/jit"
14
13
  require "benchmark"
15
14
 
16
- Sweep = CArray::JIT::Sweep
17
15
  Access = CArray::JIT::Access
18
16
 
19
- puts "this CArray has ca_call_cslab: #{Sweep.available?}"
20
- puts
21
-
22
17
  # The rank leaves the loop
23
18
  # ------------------------
24
19
  # A chunk is one flat run of cells -- and CArray's acquire reads an operand's
@@ -29,10 +24,10 @@ def doubled (source, out)
29
24
  CArray.jit_each { out = source * 2.0 }
30
25
  end
31
26
 
32
- flat_in = CArray.double(6).seq!
33
- flat_out = CArray.double(6)
34
- cube_in = CArray.double(2, 3, 4).seq!
35
- cube_out = CArray.double(2, 3, 4)
27
+ flat_in = CArray.float64(6).seq!
28
+ flat_out = CArray.float64(6)
29
+ cube_in = CArray.float64(2, 3, 4).seq!
30
+ cube_out = CArray.float64(2, 3, 4)
36
31
 
37
32
  flat = doubled(flat_in, flat_out)
38
33
  cube = doubled(cube_in, cube_out)
@@ -62,10 +57,10 @@ puts
62
57
  # whole array.
63
58
 
64
59
  n = 2_000_000
65
- src = CArray.double(n).seq!
66
- b = CArray.double(n).seq!(0.5, 0.5)
67
- out = CArray.double(n)
68
- gather = src[CArray.int32(n).seq.reverse]
60
+ src = CArray.float64(n).seq!
61
+ b = CArray.float64(n).seq!(0.5, 0.5)
62
+ out = CArray.float64(n)
63
+ gather = src[CArray.int32(n).seq.flip]
69
64
 
70
65
  def timed
71
66
  2.times { yield }
@@ -98,25 +93,25 @@ puts
98
93
  # --------------------------
99
94
  # Four things, and only the last is about this CArray being old.
100
95
 
101
- columns = CArray.double(64, 64).seq!
102
- into = CArray.double(64, 64)
96
+ columns = CArray.float64(64, 64).seq!
97
+ into = CArray.float64(64, 64)
103
98
  every_other = columns[nil, (0...64).step(2)]
104
99
  every_other_out = into[nil, (0...64).step(2)]
105
100
  kernel = CArray.jit_each { every_other_out = every_other * 2.0 }
106
101
  puts "a strided view operand loop axes #{kernel.rank}"
107
102
  puts " -- walked in place here, re-gathered there for nothing."
108
103
 
109
- matrix = CArray.double(3, 4).seq!
110
- row = CArray.double(1, 4).seq!(10.0)
111
- stretched = CArray.double(3, 4)
104
+ matrix = CArray.float64(3, 4).seq!
105
+ row = CArray.float64(1, 4).seq!(10.0)
106
+ stretched = CArray.float64(3, 4)
112
107
  kernel = CArray.jit_each { stretched = matrix * row }
113
108
  puts "a stretched operand loop axes #{kernel.rank}"
114
109
  puts " -- broadcasting arrives as a stride of zero on an axis, and a flat"
115
110
  puts " run has no axes, so cell k would stop lining up."
116
111
 
117
- masked = CArray.double(5).seq!(1.0)
112
+ masked = CArray.float64(5).seq!(1.0)
118
113
  masked[2] = UNDEF
119
- masked_out = CArray.double(5)
114
+ masked_out = CArray.float64(5)
120
115
  kernel = CArray.jit_each { masked_out = masked * 2.0 }
121
116
  puts "a mask anywhere in the pass sweepable? #{kernel.sweepable?}"
122
117
  puts " -- an operand that carries one, or a body that asks about one."
@@ -133,7 +128,7 @@ puts
133
128
  # names an index reaches neighbours, chooses an order and runs inner loops,
134
129
  # none of which a chunked walk can offer. Splitting the two methods by what
135
130
  # the block names drew the same line as what a sweep can drive.
136
- recurrence = CArray.double(8).seq!(1.0)
131
+ recurrence = CArray.float64(8).seq!(1.0)
137
132
  CArray.jit_for(1...8) { |i| recurrence[i] = recurrence[i-1] * 1.5 }
138
133
  puts "jit_for stays here by its nature"
139
134
  puts " #{recurrence.to_a.map { |v| v.round(4) }.inspect}"