🎓 A1Learner
🔍
0 of 47 lessons completed

Julia Distributed Patterns

Distributed patterns are repeatable ways to split work across workers and combine the results into one value. A reduction combines worker results with one operation, and integer addition keeps that combined value exact.

A distributed reduction adds one to ten

Workers add the numbers from 1 to 10 with one reduction, and the combined total is exact.

using Distributed
addprocs(2)
total = @distributed (+) for i in 1:10
    i
end
println(total)

A distributed reduction adds squares

Workers add the squares of the numbers from 1 to 20 with one reduction.

using Distributed
addprocs(2)
total = @distributed (+) for i in 1:20
    i * i
end
println(total)

A distributed reduction can count by twos

Each number from 1 to 50 contributes two to the reduction, and the combined total counts by twos.

using Distributed
addprocs(2)
total = @distributed (+) for i in 1:50
    2
end
println(total)

Example

using Distributed
addprocs(2)
total = @distributed (+) for i in 1:10
    i
end
squares = @distributed (+) for i in 1:10
    i * i
end
println(total)
println(squares)

Key points

  • Reduction: one operation combines results from many workers into a single value.
  • Use @distributed (+) for a loop to add integer results across workers.
  • Use addprocs(2) before a distributed loop so workers exist to run it.
  • Integer addition: adding integers in any order gives the same total.
  • Separate shares: each worker runs its own share of the loop range.

In this example, the script adds two workers, then it runs one reduction for the numbers from 1 to 10 and one reduction for their squares, and it prints both combined totals.

Note

a reduction result is one combined integer, not a list of worker pieces.

Try it Yourself

Edit the code, then press Run.

Running is simulated · JuliaHub

Exercise

What does a @distributed (+) reduction produce?