Skip to content
Julia

Multi-threading & Distributed Compute

Parallelize loops with @threads and offload tasks with @spawn / pmap.

By EZ4Code Team
parallelthreadsdistributed

Code

# Multi-threading: launch with julia --threads=4
nthreads()

# @threads divides a loop across threads
function threaded_sum(v)
    s = zeros(Float64, nthreads())   # one accumulator per thread
    Threads.@threads for i in eachindex(v)
        s[Threads.threadid()] += v[i]
    end
    sum(s)
end

# @spawn schedules a task on any available thread
f = Threads.@spawn begin
    sleep(1)
    42
end
fetch(f)   # 42 (blocks until ready)

# Distributed: workers are separate processes
using Distributed
addprocs(4)          # add 4 worker processes
@everywhere using LinearAlgebra

# pmap parallelizes a function over a collection
results = pmap(1:100) do i
    eigvals(rand(i, i))
end

# @distributed reduces across workers
total = @distributed (+) for i in 1:1_000_000
    isqrt(i)
end

Explanation

Julia offers two parallelism tiers. Threads (@threads, @spawn) share memory and are best for fine-grained CPU-bound loops — start Julia with --threads=N. Distributed computing (addprocs, pmap, @distributed) uses separate processes that communicate via serialization, which is safer for large independent jobs. @threads requires manual per-thread accumulators to avoid races; @distributed's reduction form is race-free.

More Julia Snippets