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 On Tuesday, 16 June 2015 20:22:07 UTC+1, Daniel Carrera wrote: > > 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 > resultsend > > > > > Can someone help me understand how this function works? In particular, > what do @sync and @async do? > > Thanks for the help. > > Cheers, > Daniel >
