carray-jit 0.1.7 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +69 -0
- data/README.md +3 -7
- data/carray-jit.gemspec +8 -2
- data/docs/01_GettingStarted.md +1 -1
- data/docs/02_KernelShapes.md +31 -15
- data/docs/03_SupportedFeatures.md +30 -29
- data/docs/04_Compiling.md +6 -3
- data/docs/05_DesignNotes.md +1 -3
- data/docs/06_Cheatsheet.md +31 -21
- data/docs/07_StepByStep.ja.md +45 -45
- data/docs/07_StepByStep.md +45 -45
- data/docs/08_LanguageReference.md +2 -1
- data/examples/applications/alarm.rb +2 -2
- data/examples/applications/cubic_spline.rb +20 -20
- data/examples/applications/dithering.rb +8 -8
- data/examples/applications/game_of_life.rb +1 -1
- data/examples/applications/group_stats.rb +4 -4
- data/examples/applications/heat_equation.rb +6 -6
- data/examples/applications/kepler.rb +10 -10
- data/examples/applications/lookup.rb +3 -3
- data/examples/applications/median_filter.rb +7 -7
- data/examples/applications/moving_average.rb +6 -6
- data/examples/applications/parcel_ascent.rb +12 -12
- data/examples/applications/partial_sums.rb +6 -6
- data/examples/applications/point_cloud.rb +4 -4
- data/examples/applications/point_in_polygon.rb +4 -4
- data/examples/applications/quicksort.rb +1 -1
- data/examples/applications/relaxation.rb +1 -1
- data/examples/applications/sensor_gaps.rb +3 -3
- data/examples/applications/sobel_edges.rb +6 -6
- data/examples/applications/van_der_pol.rb +3 -3
- data/examples/applications/wet_bulb.rb +5 -5
- data/examples/features/01_element_wise.rb +8 -8
- data/examples/features/02_stencil.rb +4 -4
- data/examples/features/03_recurrence.rb +1 -1
- data/examples/features/04_thomas.rb +8 -8
- data/examples/features/05_reduction.rb +8 -8
- data/examples/features/06_jit_contract.rb +10 -10
- data/examples/features/07_masks.rb +4 -4
- data/examples/features/08_views.rb +6 -6
- data/examples/features/09_inspecting.rb +2 -2
- data/examples/features/10_complex.rb +7 -7
- data/examples/features/11_c_functions.rb +17 -17
- data/examples/features/12_sweep.rb +20 -25
- data/examples/features/13_cscalar.rb +12 -12
- data/examples/features/14_stencil_window.rb +6 -6
- data/examples/features/15_loops.rb +6 -6
- data/examples/features/16_raising.rb +4 -4
- data/lib/carray/jit/analyzer.rb +47 -33
- data/lib/carray/jit/c_function.rb +80 -14
- data/lib/carray/jit/c_generator.rb +14 -2
- data/lib/carray/jit/call.rb +1 -1
- data/lib/carray/jit/compiler.rb +71 -21
- data/lib/carray/jit/errors.rb +22 -0
- data/lib/carray/jit/expression.rb +10 -4
- data/lib/carray/jit/kernel.rb +5 -6
- data/lib/carray/jit/native.rb +22 -0
- data/lib/carray/jit/node.rb +1 -1
- data/lib/carray/jit/type_assignment.rb +9 -4
- data/lib/carray/jit/vector_math.rb +111 -0
- data/lib/carray/jit/version.rb +1 -1
- data/lib/carray/jit.rb +168 -94
- metadata +148 -123
|
@@ -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.
|
|
18
|
-
b = CArray.
|
|
19
|
-
c = CArray.
|
|
20
|
-
out = CArray.
|
|
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.
|
|
36
|
-
row = CArray.
|
|
37
|
-
scaled = CArray.
|
|
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.
|
|
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.
|
|
13
|
-
out = CArray.
|
|
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.
|
|
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.
|
|
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
|
|
|
@@ -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.
|
|
22
|
-
c = CArray.
|
|
23
|
-
b = CArray.
|
|
24
|
-
d = CArray.
|
|
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.
|
|
29
|
-
dd = CArray.
|
|
30
|
-
x = CArray.
|
|
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.
|
|
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.
|
|
14
|
-
total = CArray.
|
|
15
|
-
largest = CArray.
|
|
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.
|
|
53
|
-
right = CArray.
|
|
54
|
-
product = CArray.
|
|
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
|
|
@@ -68,7 +68,7 @@ puts " matches #{product.to_a == reference}"
|
|
|
68
68
|
# The accumulator is split into partial sums, as sum(axis:) splits its own:
|
|
69
69
|
# a serial chain waits out the latency of each addition, and the split answer
|
|
70
70
|
# is usually the more accurate one as well as the faster. It is not the order
|
|
71
|
-
# the same loop takes in Ruby, and `
|
|
71
|
+
# the same loop takes in Ruby, and `fastmath: false` is what asks for that.
|
|
72
72
|
#
|
|
73
73
|
# CArray's own reductions are faster than this still, their kernels being
|
|
74
74
|
# written for the shape. What this is for is the reductions that have no such
|
|
@@ -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.
|
|
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.
|
|
12
|
-
b = CArray.
|
|
13
|
-
v = CArray.
|
|
14
|
-
p = CArray.
|
|
15
|
-
r = CArray.
|
|
16
|
-
q = CArray.
|
|
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
|
|
@@ -32,18 +32,18 @@ puts " p (x) r #{outer.to_a.inspect}"
|
|
|
32
32
|
# the parameter list is where the axis order is stated -- and swapping two
|
|
33
33
|
# parameters transposes.
|
|
34
34
|
transposed = CArray.jit_contract { |j, i, k| a[i,k] * b[k,j] }
|
|
35
|
-
puts " swapped #{transposed.
|
|
35
|
+
puts " swapped #{transposed.shape.inspect} vs #{matmul.shape.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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
13
|
+
matrix = CArray.float64(8, 3)
|
|
14
14
|
column = matrix[nil, 1]
|
|
15
15
|
column[0] = 1.0
|
|
16
16
|
|
|
@@ -22,16 +22,16 @@ 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.
|
|
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.
|
|
31
|
+
source = CArray.float64(4, 3).seq!(1.0)
|
|
32
32
|
transposed = source.transpose
|
|
33
|
-
result = CArray.
|
|
34
|
-
CArray.jit_for(*transposed.
|
|
33
|
+
result = CArray.float64(3, 4)
|
|
34
|
+
CArray.jit_for(*transposed.shape) { |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
|
|
|
37
37
|
# A view with no stride expression -- a gather, say -- cannot be addressed
|
|
@@ -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.
|
|
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,10 +8,10 @@
|
|
|
8
8
|
$LOAD_PATH.unshift(File.expand_path("../../lib", __dir__))
|
|
9
9
|
require "carray/jit"
|
|
10
10
|
|
|
11
|
-
values = CArray.
|
|
11
|
+
values = CArray.float64(16).seq!(1.0)
|
|
12
12
|
|
|
13
13
|
def sweep (values)
|
|
14
|
-
CArray.jit_for(1...values.
|
|
14
|
+
CArray.jit_for(1...values.shape[0]) { |i| values[i] = Math.sqrt(values[i-1]) + 1.0 }
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
kernel = sweep(values)
|
|
@@ -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.
|
|
30
|
-
phase = CArray.
|
|
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.
|
|
38
|
-
imaginary = CArray.
|
|
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}"
|
|
@@ -49,7 +49,7 @@ puts " tanh matches the array operator #{transformed.to_a == signal.tanh.to_a}"
|
|
|
49
49
|
|
|
50
50
|
# A reduction over complex cells: the accumulator starts from a complex zero.
|
|
51
51
|
# The sum is split into partial ones, as every reduction here is, so the last
|
|
52
|
-
# bits are not the serial loop's -- `
|
|
52
|
+
# bits are not the serial loop's -- `fastmath: false` asks for that order
|
|
53
53
|
# and gets it exactly.
|
|
54
54
|
total = CArray.cmplx128(1)
|
|
55
55
|
CArray.jit_for(1) { |i|
|
|
@@ -58,7 +58,7 @@ CArray.jit_for(1) { |i|
|
|
|
58
58
|
total[i] = running
|
|
59
59
|
}
|
|
60
60
|
in_order = CArray.cmplx128(1)
|
|
61
|
-
CArray.jit_for(1,
|
|
61
|
+
CArray.jit_for(1, fastmath: false) { |i|
|
|
62
62
|
running = Complex(0.0, 0.0)
|
|
63
63
|
(0...n).each { |j| running = running + signal[j] }
|
|
64
64
|
in_order[i] = running
|
|
@@ -66,7 +66,7 @@ CArray.jit_for(1, reassociate: false) { |i|
|
|
|
66
66
|
serial = (0...n).inject(Complex(0.0, 0.0)) { |sum, j| sum + signal[j] }
|
|
67
67
|
puts " sum #{total[0].rectangular.map { |e| e.round(6) }.inspect}"
|
|
68
68
|
puts " split sum matches Ruby #{total[0] == serial} <- the partial sums"
|
|
69
|
-
puts " serial sum matches Ruby #{in_order[0] == serial} <-
|
|
69
|
+
puts " serial sum matches Ruby #{in_order[0] == serial} <- fastmath: false"
|
|
70
70
|
|
|
71
71
|
# Where Ruby and C part company. A real operand carries an exact Integer zero
|
|
72
72
|
# as its imaginary part, and Ruby's own arithmetic returns the other operand
|
|
@@ -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.
|
|
31
|
-
out = CArray.
|
|
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.
|
|
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.
|
|
54
|
-
mixed = CArray.
|
|
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.
|
|
110
|
-
blended = CArray.
|
|
111
|
-
low = CArray.
|
|
112
|
-
high = CArray.
|
|
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.
|
|
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.
|
|
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.
|
|
214
|
-
derivative = CArray.
|
|
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.
|
|
225
|
-
fitted = CArray.
|
|
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.
|
|
239
|
-
big_out = CArray.
|
|
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
|
|
5
|
-
# `ca_call_cslab`
|
|
6
|
-
#
|
|
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,17 +24,17 @@ def doubled (source, out)
|
|
|
29
24
|
CArray.jit_each { out = source * 2.0 }
|
|
30
25
|
end
|
|
31
26
|
|
|
32
|
-
flat_in = CArray.
|
|
33
|
-
flat_out = CArray.
|
|
34
|
-
cube_in = CArray.
|
|
35
|
-
cube_out = CArray.
|
|
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)
|
|
39
34
|
|
|
40
35
|
puts "one block, a run of cells and a cube of them"
|
|
41
36
|
puts " loop axes #{flat.rank} and #{cube.rank}"
|
|
42
|
-
puts " arrays keep shape #{flat_out.
|
|
37
|
+
puts " arrays keep shape #{flat_out.shape.inspect} and #{cube_out.shape.inspect}"
|
|
43
38
|
puts " one compiled kernel #{flat.equal?(cube)}"
|
|
44
39
|
puts " answers #{flat_out.to_a == (flat_in * 2.0).to_a} and " \
|
|
45
40
|
"#{cube_out.to_a == (cube_in * 2.0).to_a}"
|
|
@@ -62,10 +57,10 @@ puts
|
|
|
62
57
|
# whole array.
|
|
63
58
|
|
|
64
59
|
n = 2_000_000
|
|
65
|
-
src = CArray.
|
|
66
|
-
b = CArray.
|
|
67
|
-
out = CArray.
|
|
68
|
-
gather = src[CArray.int32(n).seq.
|
|
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.
|
|
102
|
-
into = CArray.
|
|
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.
|
|
110
|
-
row = CArray.
|
|
111
|
-
stretched = CArray.
|
|
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.
|
|
112
|
+
masked = CArray.float64(5).seq!(1.0)
|
|
118
113
|
masked[2] = UNDEF
|
|
119
|
-
masked_out = CArray.
|
|
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.
|
|
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}"
|
|
@@ -8,10 +8,10 @@
|
|
|
8
8
|
$LOAD_PATH.unshift(File.expand_path("../../lib", __dir__))
|
|
9
9
|
require "carray/jit"
|
|
10
10
|
|
|
11
|
-
gain = CScalar.
|
|
12
|
-
offset = CScalar.
|
|
13
|
-
signal = CArray.
|
|
14
|
-
out = CArray.
|
|
11
|
+
gain = CScalar.float64() { 2.0 }
|
|
12
|
+
offset = CScalar.float64() { 10.0 }
|
|
13
|
+
signal = CArray.float64(6).seq!(1.0)
|
|
14
|
+
out = CArray.float64(6)
|
|
15
15
|
|
|
16
16
|
# In an expression it is read at every cell, which is what CArray's own
|
|
17
17
|
# operators do with it. Nothing here says it is a scalar; it says so itself.
|
|
@@ -29,18 +29,18 @@ puts
|
|
|
29
29
|
# An indexed kernel reaches it too, and there the missing index is the point:
|
|
30
30
|
# there is no axis to walk, so there is no index to write. A bare name says
|
|
31
31
|
# the same thing.
|
|
32
|
-
indexed = CArray.
|
|
32
|
+
indexed = CArray.float64(6)
|
|
33
33
|
CArray.jit_for(6) { |i| indexed[i] = signal[i] * gain[] }
|
|
34
34
|
puts "the same, with the loop written out"
|
|
35
35
|
puts " #{indexed.to_a.inspect}"
|
|
36
36
|
|
|
37
|
-
bare = CArray.
|
|
37
|
+
bare = CArray.float64(6)
|
|
38
38
|
CArray.jit_for(6) { |i| bare[i] = signal[i] * gain }
|
|
39
39
|
puts " a bare `gain` agrees #{bare.to_a == indexed.to_a}"
|
|
40
40
|
|
|
41
41
|
# It is still the one-cell array it is, so the index spelling keeps working
|
|
42
42
|
# and means the same cell.
|
|
43
|
-
spelled = CArray.
|
|
43
|
+
spelled = CArray.float64(6)
|
|
44
44
|
CArray.jit_for(6) { |i| spelled[i] = signal[i] * gain[0] }
|
|
45
45
|
puts " and so does gain[0] #{spelled.to_a == indexed.to_a}"
|
|
46
46
|
puts
|
|
@@ -48,8 +48,8 @@ puts
|
|
|
48
48
|
# Every iteration writes the one cell it has, and what is left is what the
|
|
49
49
|
# same Ruby loop leaves -- which makes it an accumulator without asking for
|
|
50
50
|
# one. The inner loop of a reduction rests on exactly this.
|
|
51
|
-
total = CScalar.
|
|
52
|
-
counts = CArray.
|
|
51
|
+
total = CScalar.int32() { 0 }
|
|
52
|
+
counts = CArray.int32(4).seq!(1)
|
|
53
53
|
CArray.jit_for(4) { |i| total[] = total[] + counts[i] }
|
|
54
54
|
puts "total after one pass over #{counts.to_a.inspect} #{total[0]}"
|
|
55
55
|
|
|
@@ -59,9 +59,9 @@ puts " what the same Ruby loop leaves #{in_ruby}"
|
|
|
59
59
|
puts
|
|
60
60
|
|
|
61
61
|
# A contraction takes one as it takes any other operand it does not index.
|
|
62
|
-
left = CArray.
|
|
63
|
-
right = CArray.
|
|
64
|
-
weight = CScalar.
|
|
62
|
+
left = CArray.float64(3).seq!(1.0)
|
|
63
|
+
right = CArray.float64(3).seq!(1.0)
|
|
64
|
+
weight = CScalar.float64() { 2.0 }
|
|
65
65
|
puts "weighted dot product #{CArray.jit_contract { |k| left[k] * right[k] * weight[] }[0]}"
|
|
66
66
|
puts
|
|
67
67
|
|
|
@@ -10,7 +10,7 @@ $LOAD_PATH.unshift(File.expand_path("../../lib", __dir__))
|
|
|
10
10
|
require "carray/jit"
|
|
11
11
|
|
|
12
12
|
rows, columns = 5, 6
|
|
13
|
-
src = CArray.
|
|
13
|
+
src = CArray.float64(rows, columns).seq!(1.0)
|
|
14
14
|
|
|
15
15
|
# The block's parameters are windows onto the arrays given, in that order:
|
|
16
16
|
# a[0, 0] is the cell the loop is on, a[-1, 0] its neighbour. The block's
|
|
@@ -21,7 +21,7 @@ smoothed = CArray.jit_stencil(src) { |a|
|
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
# The same stencil with the indices named, over the interior alone.
|
|
24
|
-
written = CArray.
|
|
24
|
+
written = CArray.float64(rows, columns)
|
|
25
25
|
CArray.jit_for(1...(rows-1), 1...(columns-1)) { |i, j|
|
|
26
26
|
written[i, j] = 0.25 * (src[i-1, j] + src[i+1, j] + src[i, j-1] + src[i, j+1])
|
|
27
27
|
}
|
|
@@ -35,7 +35,7 @@ puts " border missing by default #{smoothed.count_masked} cells"
|
|
|
35
35
|
# ---------------------------------------------------------------- the border
|
|
36
36
|
|
|
37
37
|
# Five answers, on one row of a small array so they can be read side by side.
|
|
38
|
-
line = CArray.
|
|
38
|
+
line = CArray.float64(1, 5).seq!(1.0) # 1 2 3 4 5
|
|
39
39
|
|
|
40
40
|
def row (result)
|
|
41
41
|
result.to_a[0].map { |e| e == UNDEF ? " --" : format("%4.1f", e) }.join(" ")
|
|
@@ -46,7 +46,7 @@ puts "border:, on [1 2 3 4 5] averaging each cell with its two neighbours"
|
|
|
46
46
|
[:mask, :skip, :zero, :clamp, :wrap].each do |border|
|
|
47
47
|
# :skip leaves the border as it was found, so what it was found as is worth
|
|
48
48
|
# seeing: this one is filled with -1 first.
|
|
49
|
-
into = CArray.
|
|
49
|
+
into = CArray.float64(1, 5).seq!(-1.0, 0.0)
|
|
50
50
|
result = CArray.jit_stencil(line, border: border, into: into) { |a|
|
|
51
51
|
(a[0, -1] + a[0, 0] + a[0, 1]) / 3.0
|
|
52
52
|
}
|
|
@@ -64,8 +64,8 @@ end
|
|
|
64
64
|
# Each array given gets a window, in the order the block names them; the names
|
|
65
65
|
# shadow whatever they hold outside, as CArray.fuse's do. A diffusion step
|
|
66
66
|
# with a conductivity that varies from cell to cell:
|
|
67
|
-
u = CArray.
|
|
68
|
-
k = CArray.
|
|
67
|
+
u = CArray.float64(6, 6) { |i, j| (i - 2.5).abs + (j - 2.5).abs }
|
|
68
|
+
k = CArray.float64(6, 6).fill(0.2)
|
|
69
69
|
|
|
70
70
|
stepped = CArray.jit_stencil(u, k, border: :clamp) { |u, k|
|
|
71
71
|
u[0,0] + k[0,0] * (u[-1,0] + u[1,0] + u[0,-1] + u[0,1] - 4.0 * u[0,0])
|