我需要运行昂贵的算法320次。我可以轻松地在运行80次迭代的4个集群上并行化它。我可以使用具有32核的计算机,因此我想进一步并行化该问题。将80个迭代的组并行化比较棘手,但有可能。我的想法是在每个主群集上运行8个子群集,每个子群集处理10次迭代。
为了测试这个想法,我在R中使用并行包实现了以下伪代码。我已经在4个内核上对其进行了测试,并对结果感到惊讶。
library(parallel)
library(tictoc)
N <- 6*10^8 # total number of observations to generate
n.threads <- 2
sample.sizes <- rep(round(N/n.threads, 0), n.threads) # Each cluster generates half of the sample
tic()
cl <- makeCluster(n.threads)
list <- parLapply(cl, sample.sizes, rnorm)
stopCluster(cl)
v <- unlist(list)
toc()
rm(list, v); gc()
36秒执行时间; 50%的CPU使用率
library(parallel)
library(tictoc)
N <- 6*10^8 # total number of observations to generate
rnorm.parallel <- function(N.inside){
n.threads.inside <- 2
sample.sizes <- rep(round(N.inside/n.threads.inside, 0), n.threads.inside) # each sub thread generates 1*10^8 obs
cl.inside <- makeCluster(n.threads.inside)
list <- parLapply(cl.inside, sample.sizes, rnorm)
stopCluster(cl.inside)
v <- unlist(list)
return(v)
}
n.threads <- 2
sample.sizes <- rep(round(N/n.threads, 0), n.threads) # each main thread generates 2*10^8 obs
tic()
cl <- makeCluster(length(sample.sizes))
clusterEvalQ(cl, library(parallel))
list <- parLapply(cl, sample.sizes, rnorm.parallel)
stopCluster(cl)
v <- unlist(list)
toc()
rm(list, v); gc()
42秒执行时间; 100%CPU使用率
我的结论是,尽管在集群内运行集群的技术可行,但额外的开销却使其效率降低。还有其他可以帮助我的软件包/技术吗?