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:
所有的结果都很混乱:
(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).
(我的意思是我没有得到串行后端给出的结果,也许这是因为我对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?
我需要获得类似的结果才能在我的项目中继续进行并节省内存和时间(因为实际上,EOFvalues和EOFvaluesM的顺序是(324,625))。所以,我不能留下并行后端。是否可以使用此代码重新生成相同的结果?如果是这样,那怎么样?
1 个解决方案
#1
0
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
#1
0
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