Multi-threading & Distributed Compute
Parallelize loops with @threads and offload tasks with @spawn / pmap.
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)
endExplanation
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
Broadcasting & Vectorization
Apply a function element-wise over arrays with dot syntax and @.
Multiple Dispatch
Select methods by the runtime types of all arguments, not just the receiver.
Parametric Types & Performance
Define generic, type-stable containers that compile to specialized code.
Macros & Expressions
Manipulate Julia syntax trees as first-class data via :expr and macro.
DataFrame Operations (DataFrames.jl)
Filter, transform, group, and join tabular data with DataFrames.jl.
Performance Tips: @inbounds, @fastmath, views
Write Julia that runs at C speed by removing bounds checks, avoiding allocations, and using views.