为什么并行foreach在解决时异常缓慢?

时间:2018-06-01 18:57:23

标签: r foreach parallel-processing

我尝试使用foreach库中的并行doParralel循环来提高代码的效率。但是,我发现并行代码完成需要相当长的时间。我已经设法将问题缩小到优化(nloptr)涉及矩阵求逆的函数的行。任何人都可以帮我找出为什么我得到以下结果?这是我用来重现问题的确切脚本

library(doParallel)
if (!exists('cluster')) {
  cl <<- makeCluster(2)
  registerDoParallel(cl)
  }

pt <- function(n,t,m,par=TRUE) {
  if (par)
    foreach(1:t) %dopar% {
      for(i in 1:m)
        solve(matrix(runif(n*n),n,n))
      NULL
    }
  else
    foreach(1:t) %do% {
      for(i in 1:m)
        solve(matrix(runif(n*n),n,n))
      NULL
    }
}

然后,

> system.time(pt(200,6,10,FALSE))
 user  system elapsed 
0.904   1.667   0.330 
> system.time(pt(200,6,10,TRUE))
 user  system elapsed 
0.133   0.012  33.918 

用户时间较短 - 很好。但为什么总耗时非常高?

> sessionInfo()
R version 3.2.3 (2015-12-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.4 LTS

locale:
 [1] LC_CTYPE=en_GB.UTF-8          LC_NUMERIC=C                 
 [3] LC_TIME=pl_PL.UTF-8           LC_COLLATE=en_GB.UTF-8       
 [5] LC_MONETARY=pl_PL.UTF-8       LC_MESSAGES=en_GB.UTF-8      
 [7] LC_PAPER=pl_PL.UTF-8          LC_NAME=pl_PL.UTF-8          
 [9] LC_ADDRESS=pl_PL.UTF-8        LC_TELEPHONE=pl_PL.UTF-8     
[11] LC_MEASUREMENT=pl_PL.UTF-8    LC_IDENTIFICATION=pl_PL.UTF-8

attached base packages:
[1] parallel  stats     graphics  grDevices utils     datasets  methods  
[8] base     

other attached packages:
[1] doParallel_1.0.11 iterators_1.0.9   foreach_1.4.4     rkward_0.6.3     

loaded via a namespace (and not attached):
[1] tools_3.2.3      codetools_0.2-14

0 个答案:

没有答案