【发布时间】:2019-06-10 12:30:34
【问题描述】:
在 Windows 中(尚未在 Linux 中进行测试),我正在尝试(令人尴尬地)对大量人口进行贝叶斯采样并行化。我正在运行一些测试,并在处理列表对象长度不同的列表中发现了一些非常令人担忧的行为。使用的库是 parallel、snow、doSNOW、foreach 和 rlecuyer。
## Set parameters
cores<-4; N<-10004; Mean<-rnorm(N,sd=0.7); SD<-rnorm(N,mean=1,sd=0.1)
## Split the population
lst<-suppressWarnings(split(1:N,f=1:cores))
## Initialize cluster
cl <<- parallel::makePSOCKcluster(cores)
parallel::clusterSetRNGStream(cl, iseed = round(runif(cores)*1001))
## Export and run test
clusterExport(cl,c("lst"))
system.time(
theta<-as.vector(parSapply(cl,1:cores,function(x) rnorm(length(lst[[x]]),mean=Mean[lst[[x]]],sd=SD)))
)
## validate length
system.time(
n.lst<-as.vector(parSapply(cl,1:cores,function(x) lst[[x]]))
)
## Stop the cluster and check data
parallel::stopCluster(cl)
length(theta) # 10004
length(n.lst) # 10004
现在我将人口更改为不能被 4 整除的数字
## Set parameters
cores<-4; N<-10001; Mean<-rnorm(N,sd=0.7); SD<-rnorm(N,mean=1,sd=0.1)
## Run the same code above... And check the output arrays:
length(theta) # 25010000
length(n.lst) # 25010000
所以,是的,列表呈指数增长...到数组的长度不是 2500+2500+2500+2501,而是 2500*2501*4...这对我来说毫无意义。
【问题讨论】:
标签: r parallel-processing snow