Is mclapply guaranteed to return its results in order?

好久不见. 提交于 2019-12-04 00:17:31

Short answer: it does return the results in the correct order.

But of course, you should read the code yourself (mclapply is an R function...)

The man page for collect gives some more hints:

Note: If expr uses low-level multicore functions such as sendMaster a single job can deliver results multiple times and it is the responsibility of the user to interpret them correctly.

However, if you don't mess with low-level,

collect returns any results that are available in a list. The results will have the same order as the specified jobs. If there are multiple jobs and a job has a name it will be used to name the result, otherwise its process ID will be used.

(my emphasis)

Now for mclapply. A quick glanc over the source code yields:

  • if !mc.preschedule and there are no more jobs than cores (length (X) <= cores) parallel and collect are used, see above.
  • if mc.preschedule or more jobs than cores, mclapply itself takes care of the order - see the code.

However, here's a slightly modified version of your experiment:

> unlist (mclapply(1:10, function(x){
    Sys.sleep(sample(1:5, size = 1)); 
    cat (x, " ");    
    identity(x)}, 
  mc.cores = 2, mc.preschedule = FALSE))
1  2  4  3  6  5  7  8  9  10   [1]  1  2  3  4  5  6  7  8  9 10
> unlist (mclapply(1:10, function(x){
    Sys.sleep(sample(1:5, size = 1)); 
    cat (x, " ");    
    identity(x)}, 
  mc.cores = 2, mc.preschedule = TRUE))
1  3  2  5  4  6  7  8  10  9   [1]  1  2  3  4  5  6  7  8  9 10

Which shows that the results are returned in different order by the child jobs (more precisely: child jobs are about to finish in different order), but the result is assembled in the original order.

(works on the console, but not in RStudio - the cats do not show up there)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!