简体   繁体   中英

Unable to reorganize results of nested foreach loop in R

The code (a simpler version of what I'm currently stuck in) runs flawlessly with the serial backend:

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
}}

with results as 1:regis(a list):

结果1:又名regis列表

2:EOFvalues (an array):

结果2:又名EOFvalues数组

3:EOFvaluesM (an array):

结果3:一个数组又名EOFvaluesM


but as soon as I run it using the parallel backend

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()`

All the results are quite mixed up:

在此处查看并行后端结果,存储在列表“ oper”中

(I mean I'm not getting the results as the serial backend gives, maybe that's because of my scarce knowledge of the parallelism in R).

I need to obtain the similar result in order to proceed further in my project and save memory and time(beacause actually, EOFvalues and EOFvaluesM are of the order (324,625)). So, I cannot leave parallel backend. Is it possible to regenerate the same result using this code? If so, then how?

Luckily I have a easy-looking solution to my problem.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM