更新时间:2023-12-02 19:28:40
这不是 foreach 的工作方式.你应该看看例子.如果要从并行化作业中输出某些内容,则需要使用 .combine.另外,而不是这个:
This is not how foreach works. You should look into examples. You need to use .combine, if you want to output something from your parallelized jobs. Also, instead of this:
sigmaEpsSqV<-as.matrix(sigmaEpsSqV)
SigEpsilonSq[[p]]<-sigmaEpsSqV
SigLSq[[p]]<-sigmaExtSq
RatioMat[[p]]<-ratioMatr
你必须像这样重写:
list(as.matrix(sigmaEpsSqV),sigmaEpsSqV,sigmaExtSq,ratioMatr)
您还可以使用 rbind、cbind、c、... 将结果聚合为一个最终输出.您甚至可以使用自己的组合功能,例如:
You can also use rbind, cbind, c,... to aggregate the results into one final output. You can even your own combine function, example:
.combine=function(x,y)rbindlist(list(x,y))
下面的解决方案应该有效.输出应该是一个列表列表.然而,检索结果并以正确的格式保存它们可能会很痛苦.如果是这样,您应该设计自己的 .combine 函数.
The solution below should work. The output should be a list of lists. However it might be painful to retreive results and save them in the correct format. If so, you should design your own .combine function.
cl<-makeCluster(core-1)
registerDoParallel(cl,cores=core)
SigEpsilonSq<-list()
SigLSq<-list()
RatioMat<-list()
results = foreach(p=1:100, .combine=list) %dopar%{
functions defining my variables{...}
for(i in 1:fMaxInd){
rhoSqjMatr[,i]<-1/(1+Bb[i])*(CbAdj+AbAdj*XjBarAdj+BbAdj[i]*XjSqBarAdj)/(dataZ*dataZ)
sigmaEpsSqV[i]<-mean(rhoSqjMatr[,i])
rhoSqjMatr[,i]<-rhoSqjMatr[,i]/sigmaEpsSqV[i]
biasCorrV[,i]<-sigmaEpsSqV[i]/L*gammaQl(rhoSqjMatr[,i])
Qcbar[,i]<-Qflbar-biasCorrV[,i]
sigmaExtSq[,i]<-sigmaSqExt(sigmaEpsSqV[i], rhoSqjMatr[,i])
ratioMatr[,i]<-sigmaExtSq[,i]/(sigmaL*sigmaL)#ratio (sigma_l^e)^2/(sigmaL)^2
}
list(as.matrix(sigmaEpsSqV),sigmaEpsSqV,sigmaExtSq,ratioMatr)
} #End of the dopar loop
stopCluster(cl)
#Then you extract and save results