JuliaParallel / Dagger.jl

A framework for out-of-core and parallel execution
Other
629 stars 67 forks source link

Parallelization across threads does not appear to work correctly #548

Closed wolthom closed 1 month ago

wolthom commented 2 months ago

Context

In the following minimal example, an outer function executes the inner function many times in parallel and is itself executed multiple times. The problem is trivially parallelizable, i.e. there are no dependencies across tasks.

I'm running Julia with 22 threads:

julia> Base.Threads.nthreads()
22

Copy-pasteable example

using Dagger

# Inner function
function inner_sleep()
    sleep(2)
end

# Outer function
function outer_sleep()
    @sync for _ in 1:10
        Dagger.@spawn inner_sleep()
    end
    Base.Threads.threadid()
end

# Run outer task N times in parallel
@time fetch.([Dagger.@spawn outer_sleep() for _ in 1:2])

Results

If my understanding of the task-based parallelism approach is correct, this should take roughly 2 seconds regardless of how many outer tasks I'm spawning.

This actually takes ~4 seconds instead, on my machine.

jpsamaroo commented 2 months ago

You'll want to let Dagger execute multiple of inner_sleep on each thread at a time with this for each call:

Dagger.@spawn occupancy=Dict(Dagger.ThreadProc=>0) inner_sleep()