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:
>
>    1. It seems that @async performs work in a separate green thread, 
>    while @spawn performs work in a separate julia process. Is that right?
>    
> Yes. 

>
>    1. Will code executed with several calls to @async actually run on 
>    separate cores?
>    
> No. 

>
>    1. 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.

>
>    1. 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.
 

>
>    1. 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).

Reply via email to