Julia Parallel Patterns
Parallel patterns are reusable shapes for splitting work and combining results. A pattern splits work into parts, then combines the parts into one result.
An atomic counter adds values from many rounds
Ten rounds add their numbers to one counter. The counter holds the full total.
function atomic_total()
counter = Threads.Atomic{Int}(0)
Threads.@threads for i in 1:10
Threads.atomic_add!(counter, i)
end
return counter[]
end
println(atomic_total())Partial results add in order to one total
Two chunks are added separately. Their partial totals add in order to the full total.
function chunk_total()
ranges = [(1, 10), (11, 20)]
partials = [0, 0]
Threads.@threads for c in 1:2
partials[c] = sum(ranges[c][1]:ranges[c][2])
end
return partials
end
println(chunk_total())
println(sum(chunk_total()))An atomic counter counts matching rounds
Twenty rounds run, and matching rounds add one to the counter. The counter holds the count of even numbers.
function count_evens()
counter = Threads.Atomic{Int}(0)
Threads.@threads for i in 1:20
if i % 2 == 0
Threads.atomic_add!(counter, 1)
end
end
return counter[]
end
println(count_evens())Example
function parallel_sum(values)
counter = Threads.Atomic{Int}(0)
Threads.@threads for v in values
Threads.atomic_add!(counter, v)
end
return counter[]
end
values = [15, 25, 35, 45]
println(parallel_sum(values))
println(sum(values))Key points
- Use Threads.Atomic{Int}(0) to hold one integer that many rounds can update.
- Use Threads.atomic_add! to add a value to an atomic counter.
- Use counter[] to read the value in an atomic counter.
- Use partial results summed in order as another way to combine integer work.
- Use integer addition for a parallel reduction, because it gives the same total in any order.
In this example, each value is added to an atomic counter by a shared loop, the counter holds 120, and the serial sum confirms the same total.
Note
An atomic counter starts at zero before any round adds to it.
Try it Yourself
Edit the code, then press Run.
Running is simulated · JuliaHub
Exercise
Which kind of reduction does an atomic counter safely combine in a parallel loop?