并行处理中的最佳内核数是多少?

Posted

技术标签:

【中文标题】并行处理中的最佳内核数是多少?【英文标题】:How many cores is optimal in parallel processing? 【发布时间】:2017-07-26 15:00:48 【问题描述】:

假设我有一个 8 核 CPU。在R中使用doParallel,当我注册makeCluster(x)时,理想的内核数量是多少,x,要使用?

是否尽可能多的核心?还是使用 7 核会比使用 6 核慢?这方面有什么规定吗?

【问题讨论】:

这取决于工作负载,以及是 CPU 绑定还是 IO 绑定。做一些基准测试并找出答案 不知道你是否使用library(caret),但这很好地说明了你的模型训练问题,topepo.github.io/caret/parallel-processing.html 这真的取决于你正在做的工作类型,但会有总是收益递减 【参考方案1】:

如 cmets 中所述,最佳内核数量取决于手头的任务,但您可以自行确定。初始化 7 个不同的集群并对结果进行基准测试。我不会使用所有 8 个内核,所以在你的情况下应该是 7 个。

这是一个小的“愚蠢”模板,其中并行化没有意义,因为简单的 sapply 版本要快得多,因为发送到内核的开销会大大降低性能。

不管怎样,插入你想要优化的代码,到处玩,找到完美的设置;)

require(parallel)
cl2 = makeCluster(2)
cl3 = makeCluster(3)
cl4 = makeCluster(4)
cl5 = makeCluster(5)
cl6 = makeCluster(6)
cl7 = makeCluster(7)

library(microbenchmark)
mc <- microbenchmark(times = 100,
                     noPa = 
                       res = sapply(mtcars, mean, na.rm = TRUE)
                     ,
                     cor2 = 
                       res = parSapply(cl2, mtcars, mean, na.rm = TRUE)
                     ,
                     cor3 = 
                       res = parSapply(cl3, mtcars, mean, na.rm = TRUE)
                     ,
                     cor4 = 
                       res = parSapply(cl4, mtcars, mean, na.rm = TRUE)
                     ,
                     cor5 = 
                       res = parSapply(cl5, mtcars, mean, na.rm = TRUE)
                     ,
                     cor6 = 
                       res = parSapply(cl6, mtcars, mean, na.rm = TRUE)
                     ,
                     cor7 = 
                       res = parSapply(cl7, mtcars, mean, na.rm = TRUE)
                     
); mc

stopCluster(cl2);stopCluster(cl3);stopCluster(cl4);
stopCluster(cl5);stopCluster(cl6);stopCluster(cl7)
Unit: microseconds
 expr      min        lq       mean   median        uq       max neval
 noPa   77.370   94.4365   97.52549   97.281  101.5475   131.983   100
 cor2  713.388  804.1260  947.56529  836.553  887.4680  7178.812   100
 cor3  840.250  941.2275 1071.55460  967.681 1027.4145  5343.576   100
 cor4  877.797 1046.7570 1194.51996 1077.761 1132.3745  7028.057   100
 cor5 1032.535 1139.2015 1303.64424 1190.686 1241.3170  8148.199   100
 cor6 1141.761 1222.5430 1438.18655 1261.797 1339.1655 10589.302   100
 cor7 1269.192 1345.4240 1586.03513 1399.468 1487.3615 10547.204   100

这是一个并行化有意义的示例。根据结果​​,7 核将是最快的解决方案。如果你在自己的机器上运行它并想在它旁边做其他事情,我会选择 4 个内核,因为时间是可比的,而且机器没有以最大容量工作。

library(lme4)
f <- function(i) 
  lmer(Petal.Width ~ . - Species + (1 | Species), data = iris)


library(microbenchmark)
mc <- microbenchmark(times = 3,
                     noPa = 
                       res = sapply(1:100, f)
                     ,
                     cor2 = 
                       res = parSapply(cl2, 1:100, f)
                     ,
                     cor3 = 
                       res = parSapply(cl3, 1:100, f)
                     ,
                     cor4 = 
                       res = parSapply(cl4, 1:100, f)
                     ,
                     cor5 = 
                       res = parSapply(cl5, 1:100, f)
                     ,
                     cor6 = 
                       res = parSapply(cl6, 1:100, f)
                     ,
                     cor7 = 
                       res = parSapply(cl7, 1:100, f)
                     
); mc
Unit: milliseconds
 expr       min        lq      mean    median       uq      max neval
 noPa 1925.2889 1964.9473 2169.9294 2004.6057 2292.250 2579.894     3
 cor2 1501.8176 1591.5596 1722.1834 1681.3015 1832.366 1983.431     3
 cor3 1097.4251 1188.6271 1345.1643 1279.8291 1469.034 1658.239     3
 cor4  956.9829 1007.6607 1302.2984 1058.3384 1474.956 1891.574     3
 cor5 1027.5877 1872.3501 2379.9384 2717.1125 3056.114 3395.115     3
 cor6 1001.2572 1048.8277 1217.5999 1096.3983 1325.771 1555.144     3
 cor7  815.2055  905.7948  945.7555  996.3841 1011.030 1025.677     3

【讨论】:

这很奇怪。我正在 12 核机器上训练 10 个类的分类器。如果我使用 X > 3 个内核,我将获得大约 25 秒的计算时间。增加内核和计算时间保持不变。用 htop 看内存,在 16GB 系统上内存低于 16GB。我认为这是因为矩阵计算无论如何都是并行完成的,看着 htop,我看到所有内核都有一些并行动作。至少我希望这就是原因

以上是关于并行处理中的最佳内核数是多少?的主要内容,如果未能解决你的问题,请参考以下文章

doMPI 和节点、处理器和内核

并行处理数组索引的最佳方法?

R doParallel foreach 中的并行处理

并行处理传入数据的最佳方法? (Java/Groovy)

南京大学《操作系统》笔记

多处理:仅使用物理内核?