无法重组 R 中嵌套 foreach 循环的结果
Unable to reorganize results of nested foreach loop in R
代码(我目前所用的更简单的版本)运行与串行后端完美结合:
regis <-list()
EOFvalues <-array(,c(5,6))
EOFvaluesM <-array(,c(5,6))
for(j in 1:6) {
for(k in 1:5){
loc_no <- (j-1)+k
regis[[j]]=c(j,k)
EOFvalues[k,j]=j+k
EOFvaluesM[k,j]=j*k
}}
结果为 1:regis(列表):
2:EOFvalues(一个数组):
3:EOFvaluesM(一个数组):
但是一旦我运行它使用并行后端
regis <-list()
EOFvalues <-array(,c(5,6))
EOFvaluesM <-array(,c(5,6))
library(doParallel)
cores0=detectCores()
cl<-makeCluster(cores0, type= "SOCK" ,outfile="")
registerDoParallel(cl)
oper <- foreach(j=1:6, .combine='c',.export = c("%dopar%"), .packages = c("doParallel")) %dopar% {
foreach(k=1:5,.export = c("%dopar%"), .packages = c("doParallel")) %do% {
regis[[j]]=c(j,k)
EOFvalues[k,j]=j+k
EOFvaluesM[k,j]=j*k
par_res <- list(regis,EOFvalues,EOFvaluesM)
}
}
stopImplicitCluster()`
所有的结果都很混乱:
(我的意思是我没有得到串行后端给出的结果,这可能是因为我对 R 中的并行性知之甚少)。
我需要获得类似的结果,以便在我的项目中进一步进行并节省内存和时间(因为实际上,EOFvalues 和 EOFvaluesM 的顺序为 (324,625))。所以,我不能离开并行后端。是否可以使用此代码重新生成相同的结果?如果是,那又如何?
幸运的是,我有一个看起来简单的解决方案来解决我的问题。
regis <-list()
EOFvalues <-array(,c(5,6))
EOFvaluesM <-array(,c(5,6))
library(doParallel)
cores0=detectCores()
cl<-makeCluster(cores0, type= "SOCK" ,outfile="")
registerDoParallel(cl)
oper_a <- foreach(j=1:6, .combine='rbind',.export = c("%dopar%"), .packages = c("doParallel")) %dopar% {
foreach(k=1:5,.export = c("%dopar%"), .packages = c("doParallel")) %do% {
res<-list()
res$regis <- c(j,k)
res$EOFvalues <- j+k
res$EOFvaluesM <- j*k
return(res)
# regis[[j]]=c(j,k)
# EOFvalues[k,j]=j+k
# EOFvaluesM[k,j]=j*k
# par_res <- list(regis,EOFvalues,EOFvaluesM)
}
}
################### The Solution ################
final_regis <- list() #made a list for all the three parameters
final_EOFvalues <- list() #...so that EOFvalues and EOFvaluesM can be converted from list to matrix
final_EOFvaluesM <- list()
for(i in 1:length(oper_a)){ #Here the above made lists are filled
final_regis <- c(final_regis,oper_a[[i]][["regis"]])
final_EOFvalues <- c(final_EOFvalues,oper_a[[i]][["EOFvalues"]])
final_EOFvaluesM <- c(final_EOFvaluesM,oper_a[[i]][["EOFvaluesM"]])
}
#Unlist to convert them into vectors.Also i don't know why but as.matrix doesn't give the correct dimensions.So I used simply matrix
mat_Afinal_EOFvalues <- matrix(unlist(final_EOFvalues),nrow=5,byrow=TRUE)
mat_Bfinal_EOFvaluesM <- matrix(unlist(final_EOFvaluesM),nrow=5,ncol=6,byrow=TRUE)
stopImplicitCluster()
#Hurray
代码(我目前所用的更简单的版本)运行与串行后端完美结合:
regis <-list()
EOFvalues <-array(,c(5,6))
EOFvaluesM <-array(,c(5,6))
for(j in 1:6) {
for(k in 1:5){
loc_no <- (j-1)+k
regis[[j]]=c(j,k)
EOFvalues[k,j]=j+k
EOFvaluesM[k,j]=j*k
}}
结果为 1:regis(列表):
2:EOFvalues(一个数组):
3:EOFvaluesM(一个数组):
但是一旦我运行它使用并行后端
regis <-list()
EOFvalues <-array(,c(5,6))
EOFvaluesM <-array(,c(5,6))
library(doParallel)
cores0=detectCores()
cl<-makeCluster(cores0, type= "SOCK" ,outfile="")
registerDoParallel(cl)
oper <- foreach(j=1:6, .combine='c',.export = c("%dopar%"), .packages = c("doParallel")) %dopar% {
foreach(k=1:5,.export = c("%dopar%"), .packages = c("doParallel")) %do% {
regis[[j]]=c(j,k)
EOFvalues[k,j]=j+k
EOFvaluesM[k,j]=j*k
par_res <- list(regis,EOFvalues,EOFvaluesM)
}
}
stopImplicitCluster()`
所有的结果都很混乱:
(我的意思是我没有得到串行后端给出的结果,这可能是因为我对 R 中的并行性知之甚少)。
我需要获得类似的结果,以便在我的项目中进一步进行并节省内存和时间(因为实际上,EOFvalues 和 EOFvaluesM 的顺序为 (324,625))。所以,我不能离开并行后端。是否可以使用此代码重新生成相同的结果?如果是,那又如何?
幸运的是,我有一个看起来简单的解决方案来解决我的问题。
regis <-list()
EOFvalues <-array(,c(5,6))
EOFvaluesM <-array(,c(5,6))
library(doParallel)
cores0=detectCores()
cl<-makeCluster(cores0, type= "SOCK" ,outfile="")
registerDoParallel(cl)
oper_a <- foreach(j=1:6, .combine='rbind',.export = c("%dopar%"), .packages = c("doParallel")) %dopar% {
foreach(k=1:5,.export = c("%dopar%"), .packages = c("doParallel")) %do% {
res<-list()
res$regis <- c(j,k)
res$EOFvalues <- j+k
res$EOFvaluesM <- j*k
return(res)
# regis[[j]]=c(j,k)
# EOFvalues[k,j]=j+k
# EOFvaluesM[k,j]=j*k
# par_res <- list(regis,EOFvalues,EOFvaluesM)
}
}
################### The Solution ################
final_regis <- list() #made a list for all the three parameters
final_EOFvalues <- list() #...so that EOFvalues and EOFvaluesM can be converted from list to matrix
final_EOFvaluesM <- list()
for(i in 1:length(oper_a)){ #Here the above made lists are filled
final_regis <- c(final_regis,oper_a[[i]][["regis"]])
final_EOFvalues <- c(final_EOFvalues,oper_a[[i]][["EOFvalues"]])
final_EOFvaluesM <- c(final_EOFvaluesM,oper_a[[i]][["EOFvaluesM"]])
}
#Unlist to convert them into vectors.Also i don't know why but as.matrix doesn't give the correct dimensions.So I used simply matrix
mat_Afinal_EOFvalues <- matrix(unlist(final_EOFvalues),nrow=5,byrow=TRUE)
mat_Bfinal_EOFvaluesM <- matrix(unlist(final_EOFvaluesM),nrow=5,ncol=6,byrow=TRUE)
stopImplicitCluster()
#Hurray