Groups 39 of 99+ julia-users › threads and processes, @async vs. @spawn/@parallel? 2 posts by 2 authors John Brock 9/1/15 I've read through the parallel computing documentation and experimented with some toy examples, but I still have some questions about the fundamentals of parallel programming in Julia: It seems that @async performs work in a separate green thread, while @spawn performs work in a separate julia process. Is that right? Will code executed with several calls to @async actually run on separate cores? Getting true parallelism with @spawn or @parallel requires launching julia with the -p flag or using addprocs(...). Does the same hold for @async, i.e., will I get true parallelism with several calls to @async if I only have a single julia process? In what situations should I choose @spawn over @async, and vice versa? How does scope and serialization work with regards to @async? If the code being executed with @async references some Array, will each thread get a copy of that Array, like if I had called @spawn instead? Or will each thread have access to the same Array, obviating the need for SharedArray when using @async? A lot of this stuff is left ambiguous in the documentation, but I'd be happy to submit a pull request with updates if I can get some clear answers. Thanks! -John Steven G. Johnson 9/2/15 On Tuesday, September 1, 2015 at 10:02:30 PM UTC-4, John Brock wrote: I've read through the parallel computing documentation and experimented with some toy examples, but I still have some questions about the fundamentals of parallel programming in Julia: It seems that @async performs work in a separate green thread, while @spawn performs work in a separate julia process. Is that right? Yes. Will code executed with several calls to @async actually run on separate cores? No. Getting true parallelism with @spawn or @parallel requires launching julia with the -p flag or using addprocs(...). Does the same hold for @async, i.e., will I get true parallelism with several calls to @async if I only have a single julia process? @async will only ever give cooperative multitasking. In what situations should I choose @spawn over @async, and vice versa? @async is much cheaper and is especially useful for asynchronous I/O (it is built on libuv, and async I/O is libuv's raison-d'être). @spawn if you want multiple cores to be used simultaneously. Note, however, that @spawn is built on @async: asynchronous I/O is used to track the master process's communication with multiple workers "simultaneously". Basically, @async I/O is useful whenever you want to track communication with multiple I/O channels in a single process, with one I/O task waking up when read/write is available and sleeping when I/O is stalled, without the complexities (locking, race conditions, etc) or overhead of pre-emptive (kernel) threads. How does scope and serialization work with regards to @async? If the code being executed with @async references some Array, will each thread get a copy of that Array, like if I had called @spawn instead? Or will each thread have access to the same Array, obviating the need for SharedArray when using @async? @async uses green threads, which all share the same process address space (and hence will share the same array) and are all running in the same thread (just taking turns cooperatively). @spawn uses separate julia processes (not shared-memory threads!) with their own address spaces (think MPI or PVM, not OpenMP). (In the future, there is likely to be a third type of parallelism in Julia: shared-memory threads via @threads, ala openmp or cilk. See https://github.com/JuliaLang/julia/issues/1790 and the jn/threading branch of Julia for the current prototype). 1 0