切换到并行模式时,性能急剧下降

时间:2020-08-10 13:35:16

标签: r windows-10 doparallel

我正在Windows 10 / AMD Ryzen 9进程上尝试将并行模式与R一起使用。

不幸的是,我遇到奇怪的行为。

其中之一是:当计算尺寸增大的矩阵的平方时,当尺寸达到17时,性能会大大下降。仅在并行模式下

我知道使用并行模式与顺序模式会产生开销。 但是,为什么开销低于17,“正常”?为什么从17开始就变得如此戏剧性?

这是演示它的一段代码:

library("tidyverse")

library("parallel")
library("doParallel")
library("foreach")
library("rbenchmark")


f <- function(n) {
  M <- matrix(c(1:(n*n)),n,n)
  res <- M %*% M
  return(norm(res))
}

g <- function(n,m) {
  if (m=="PAR") {
    res <- sum(foreach(i=1:10,.combine="c",.export="f") %dopar% f(n))
  } else {
    res <- sum(foreach(i=1:10,.combine="c") %do% f(n))
  }
  cat("m=",m,", n=",n, " res=",res,"\n",sep="")
}

num_cores <- round(0.5*detectCores())
cat("Setting up the parallel execution cluster on",num_cores,"cores ... ")
my_cluster <- makeCluster(num_cores)
registerDoParallel(my_cluster)
cat("done.\n")

res <- foreach (i=1:20, .combine=rbind) %do%
  mutate(benchmark(g(i,"SEQ"),
            g(i,"PAR"),
            replications=10,
            columns=c("test","replications","elapsed")),
         i=i)

gg <- ggplot(res) + 
  scale_y_continuous(trans="log10") +
  geom_line(aes(x=i,y=elapsed,color=test),size=1.2)

print(gg)

这是此代码的输出,以对数标度。

performance result of running function f() in sequence or in parallel

为什么在并行模式下(尺寸为17)执行矩阵乘法会导致性能崩溃?

感谢您的帮助!

CF

1 个答案:

答案 0 :(得分:0)

感谢您的留言!它给了我一个尝试更新版本的R(4.0.2)的想法……这解决了问题!无论如何,这是有问题的配置的sessionInfo(),当我切换回R 3.5.3时仍然存在:

R version 3.5.3 (2019-03-11)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=French_France.1252  LC_CTYPE=French_France.1252    LC_MONETARY=French_France.1252
[4] LC_NUMERIC=C                   LC_TIME=French_France.1252    

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

other attached packages:
 [1] rbenchmark_1.0.0     doParallel_1.0.14    iterators_1.0.11     foreach_1.5.1       
 [5] forcats_0.4.0        stringr_1.4.0        dplyr_0.8.0.1        purrr_0.3.2         
 [9] readr_1.3.1          tidyr_0.8.3          tibble_2.1.1         ggplot2_3.1.1       
[13] tidyverse_1.2.1      RevoUtils_11.0.3     RevoUtilsMath_11.0.0

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.1       cellranger_1.1.0 pillar_1.3.1     compiler_3.5.3   plyr_1.8.4      
 [6] tools_3.5.3      digest_0.6.18    jsonlite_1.5     lubridate_1.7.4  gtable_0.3.0    
[11] nlme_3.1-137     lattice_0.20-38  pkgconfig_2.0.2  rlang_0.3.4      cli_1.1.0       
[16] rstudioapi_0.10  haven_2.1.0      xfun_0.6         withr_2.1.2      xml2_1.2.0      
[21] httr_1.4.0       generics_0.0.2   hms_0.4.2        grid_3.5.3       tidyselect_0.2.5
[26] glue_1.3.1       R6_2.3.0         readxl_1.3.1     modelr_0.1.4     magrittr_1.5    
[31] codetools_0.2-16 scales_1.0.0     backports_1.1.4  rvest_0.3.3      assertthat_0.2.1
[36] colorspace_1.4-1 labeling_0.3     tinytex_0.12     stringi_1.4.3    lazyeval_0.2.2  
[41] munsell_0.5.0    broom_0.5.2      crayon_1.3.4