do I still need to makeCluster if I'm already

2019-07-22 14:36发布

Reading the vignette for doparallel.

Are the following two code blocks one and the same?

library(doparallel)
  no_cores <- 8
  cl <- makeCluster(no_cores) 
  registerDoParallel(cl)
pieces <- foreach(i = seq_len(length(pieces))) %dopar% { # do stuff}

Is above just the same as this:

library(doparallel)
  registerDoParallel(cores = 8)
pieces <- foreach(i = seq_len(length(pieces))) %dopar% { # do stuff}

Must I makeCluster() when using doparallel if I want to use multiple cores? or is the single line enough registerDoParallel(cores = 8)

1条回答
Luminary・发光体
2楼-- · 2019-07-22 15:16

On a Windows machine, these two examples are basically equivalent. The only difference is that the first example uses an explicit cluster object and the second uses an implicit cluster object that is created when you execute registerDoParallel. The performance of the two examples should be the same.

On a Mac or Linux machine, the first example uses the snow derived backend (exactly the same as on a Windows machine), ultimately using clusterApplyLB to perform the parallel computations. The second example uses the multicore derived backend (which was never available on Windows), ultimately using mclapply to perform the parallel computations which will probably be somewhat more efficient than the first example.

查看更多
登录 后发表回答