Skip to content
Julia

Multihilo y computación distribuida

Paraleliza bucles con @threads y descarga tareas con @spawn / pmap.

#parallel#threads#distributed

Code

julia
# 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