whats the difference between @spawn fetch and @sync @sync in julia

Viewed 418

to do multiple work asyncronously (sorting vectors, and function calculation mainly (where the calculations is compute or memory bound,at the moment, i can write those operations in the following ways:

  1. using Threads.@spawn
_f1 = Threads.@spawn f1(x)
_f2 = Threads.@spawn f2(x)
_f3 = Threads.@spawn f3(x)
y1 = fetch(_f1)
y2 = fetch(_f2)
y3 = fetch(_f3)

There is also this pattern (seems cleaner):

@sync begin
  @async y1 = f1(x)
  @async y2 = f2(x)
  @async y3 = f3(x)
end

for this particular use case, which one is preferred?

1 Answers

Julia has the following support for parallelization (also see https://docs.julialang.org/en/v1/manual/parallel-computing/)

  1. generating SIMD assembly code using @simd and @inbounds macros (read https://docs.julialang.org/en/v1/base/simd-types/ for more details)

  2. Green threading (co-routines) - these are not actual threads but allow one task to wait for another, typically where the other tasks do not consume CPU on the current thread. Good examples include waiting for intensive I/O operations or orchestration of distributed processes. Green threads are very light and there can be thousands of them but they all are executed within a single (calling) system thread.

  3. Threads via the Threads module. This allows to spawn actual system threads. The advantage (compared to the next scenario in this list) is that all threads share the same process memory.

  4. Multiprocessing/distributed computing via Distributed module. The nice thing here is that you can use the same API moving from a single machine to a cluster. In any HPC computing scenario this is the first choice to consider

  5. GPU computing

In your post you are considering green threads vs real threads. The rule is simple:

  • if your functions f1, f2, f3 do mostly I/O and are not CPU intensive (e.g. download files from internet) or wait for other processes to complete - use green threading (@sync/@async)
  • otherwise use Threads (or Distributed computing). This is particularly important in case of CPU-intensive jobs where green threads will not give you any performance boost as they utilize a single system thread.
Related