Help me understand @sync, @async and pmap()?

498 views
Skip to first unread message

Daniel Carrera

unread,
Jun 16, 2015, 3:22:07 PM6/16/15
to julia...@googlegroups.com
Hello,

I have been looking at the documentation for parallel programming, with special interest in SharedArrays. But no matter how hard I try, I cannot get a clear picture of what @sync and @async do. I think they are not really explained anywhere. Maybe they are explained somewhere and I just haven't found it. To make my question concrete, here is the implementation of pmap:

function pmap(f, lst)
    np = nprocs()  # determine the number of processes available
    n = length(lst)
    results = cell(n)
    i = 1
    # function to produce the next work item from the queue.
    # in this case it's just an index.
    nextidx() = (idx=i; i+=1; idx)
    @sync begin
        for p=1:np
            if p != myid() || np == 1
                @async begin
                    while true
                        idx = nextidx()
                        if idx > n
                            break
                        end
                        results[idx] = remotecall_fetch(p, f, lst[idx])
                    end
                end
            end
        end
    end
    results
end



Can someone help me understand how this function works? In particular, what do @sync and @async do?

Thanks for the help.

Cheers,
Daniel

Avik Sengupta

unread,
Jun 16, 2015, 4:28:22 PM6/16/15
to julia...@googlegroups.com
The following is an informal description, please don't take it as ground truth... 

So @async will start a job and return, without waiting for the result of that job being available. So, in the code above, the real work is being done in the remotecall_fetch, which runs a function "f" in the remote process. Due to the @async, the "while true" loop will continue as soon as the function is sent to the remote process, without waiting for its result to be passed back. 

When the "while true" loop is completed, you dont then want to return out of the "pmap" function, since the remote functions may not yet have finished. At that point, you want to wait till all the jobs that you have started do finish. That is what @sync does. It waits for all @async jobs to have finished before continuing. Hence, you will typically (though not always) see @sync/@async pairs. 

Hope that helps. 

Regards
-
Avik

Daniel Carrera

unread,
Jun 17, 2015, 3:06:54 AM6/17/15
to julia...@googlegroups.com
Thanks. That does help. I'm still having problems making it all work, but I think I need to prepare a minimalist example that illustrates what I want to do. I'll post a new thread with an example later.

Cheers,
Daniel.
Reply all
Reply to author
Forward
0 new messages