Julia SIMD Concepts
SIMD is one instruction that acts on several numbers at once. A loop marked for SIMD can process a whole group in one step.
A marked integer sum matches a plain sum
A plain loop and a marked loop give the same integer total.
function plain_total(xs)
total = 0
for x in xs
total += x
end
return total
end
function simd_total(xs)
total = 0
@simd for x in xs
total += x
end
return total
end
xs = collect(1:100)
println(plain_total(xs))
println(simd_total(xs))An index loop can use the same marking
An index loop can use the same marking. The sum stays exact.
function indexed_total(xs)
total = 0
@inbounds @simd for i in eachindex(xs)
total += xs[i]
end
return total
end
println(indexed_total(collect(1:100)))
println(indexed_total([3, 4, 5]))A marked loop can write into a new list
The marking also works when a loop writes into a new list. Each entry doubles.
function double_all(xs)
out = similar(xs)
@inbounds @simd for i in eachindex(xs)
out[i] = 2 * xs[i]
end
return out
end
println(double_all([1, 2, 3]))
println(double_all([10, 20]))Example
function plain_total(xs)
total = 0
for x in xs
total += x
end
return total
end
function simd_total(xs)
total = 0
@simd for x in xs
total += x
end
return total
end
big = collect(1:1000)
println(plain_total(big))
println(simd_total(big))
println(plain_total(big) == simd_total(big))Key points
- Vector work: SIMD applies one instruction to several numbers at once.
- Marking: @simd marks a loop so the compiler may use vector instructions for it.
- Bounds: pair @simd with @inbounds when every index in the loop is known to be safe.
- Integers: integer sums stay exact with @simd, so they give clean evidence for the idea.
- Floats: float sums with @simd may group numbers in a new order, so their totals may differ in tiny ways.
In this example, a plain total and a marked total are computed for 1 through 1000. Both totals are 500500. The equality check confirms the marked loop changes nothing about the result.
Note
@simd does not change what a loop computes for integers, it only changes how the loop runs.
Try it Yourself
Edit the code, then press Run.
Running is simulated · JuliaHub
Exercise
Why does this lesson use integer sums for @simd evidence?