Multithreading for nested loops

Julia can manage huge numbers of tasks — it’s fine (and good for load-balancing) if the number of tasks is far more than the number of processors. But spawning a task has a significant overhead, so you only want to spawn a task for a relatively expensive calculation, not in a tight loop that does a tiny amount of computation per task.

To have an idea, what’s the overhead (approximately)? Memory, time, both?

Technically both, but the one that typically matters more is time.

To get a feeling for the overhead:

julia> f() = @sync Threads.@spawn nothing;

julia> @btime f();
  1.753 μs (17 allocations: 960 bytes)

julia> f() = Threads.@spawn nothing;

julia> @btime f();
  377.878 ns (4 allocations: 434 bytes)
2 Likes