Skip to content

Parallelization across threads does not appear to work correctly #548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wolthom opened this issue Jul 13, 2024 · 1 comment · Fixed by #549
Closed

Parallelization across threads does not appear to work correctly #548

wolthom opened this issue Jul 13, 2024 · 1 comment · Fixed by #549

Comments

@wolthom
Copy link
Contributor

wolthom commented Jul 13, 2024

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.

@wolthom wolthom changed the title Parallelization across threads does appear to work correctly Parallelization across threads does not appear to work correctly Jul 13, 2024
@jpsamaroo
Copy link
Member

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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants