중첩 루프를 foreach를 사용하여 병렬 코드로 변환하려고합니다. 나는 이것을하는 방법을 이해하지 못한다. 내 코드를 따르십시오 : 미리 감사드립니다 !!R : foreach와 병렬로 변경하기 위해 중첩 된 루프
setwd("simul")
IntervalTime <- c(10,15,20)
ThinIntesity <- c(10,15,20,25)
MINSTOCKGROW <- c(200,250,300)
meteoScen <- c("rcp4p5", "rcp8p5")
Co2mod <- c("CO2_rcp4p5", "CO2_rcp8p5")
fileSettings <- c("set_co2_on_man_off.txt", "set_co2_on_man_on.txt")
mapUnc <- data.frame()
uncerOut <- data.frame()
mapUncNoMan <- data.frame()
validation <- list()
Sample <- getSample(resDE,thin=700,start=0.3*niter,coda=F)
nSample <- nrow(Sample)
ValidationOutput <- "./output/"
#FOR MANAGEMENT CASES
다음
병렬화 코드,하지만 난 다른 방법하지만 하나의 코어에 작업을 시도
for (i in 1:length(IntervalTime)){
for(p in 1:length(MINSTOCKGROW)){
for (j in 1:length(ThinIntesity)){
for(z in 1:length(meteoScen)){
for (k in 1:nSample){
# MAP BAYESIAN REFERENCE
ps <- createMixWithDefaults(pars = Sample[k, ],defaults = paramDefault, parind)
if (meteoScen[z]=="M45"){
#running the model
unlink(list.files("./output/Validation",full.names = TRUE),recursive=TRUE)
validation <- run3DCMCC(modelrun)
validation$annual$MINSTOCKGROW <- MINSTOCKGROW[p]
validation$annual$Management <- paste0(meteoScen[z],"_","IV_",IntervalTime[i],"_","IT_",ThinIntesity[j])
validation$annual$CODE <- paste0(meteoScen[z],"_","IV_",IntervalTime[i],"_","IT_",ThinIntesity[j],"_",MINSTOCKGROW[p],"_",validation$annual$YEAR)
validation$annual$Thinintensity <- ThinIntesity[j]
validation$annual$intervTime <- IntervalTime[i]
validation$annual$Meteo <- meteoScen[z]
validation$annual$Manage <- "MANAGEMENT"
validation$annual$Harv_Wood <- cumsum(validation$annual$C_HW)
validation$annual$StockCarbon <- validation$annual$Harv_Wood+validation$annual$STANDING_WOOD
# validation$monthly$Date <- as.yearmon(paste0(validation$monthly$YEAR, "-", validation$monthly$MONTH))
# validation$monthly$Management <- paste0("METEO_",meteoScenario[z],"INTERVAL_",IntervalTime[i],"INTENSITY_",ThinIntesity[j])
validationDF <- data.frame(validation$annual)
write.table(validationDF, paste0("./dataOut/",meteoScen[z],"_IV_",IntervalTime[i],"_IT_",ThinIntesity[j],"_MINSTCK_",MINSTOCKGROW[p],"_MAN_ON",".txt"),quote = F, sep = "\t",row.names = F,col.names = T)
mapUnc <- rbind(mapUnc, validation$annual)
} else {
중첩 루프의 내부 상태
ps <- createMixWithDefaults(pars = Sample[k, ],defaults = paramDefault,parind)
#running the model
unlink(list.files("./output/",full.names = TRUE),recursive=TRUE)
validation <- run3DCMCC(modelrun)
validation$annual$MINSTOCKGROW <- MINSTOCKGROW[p]
validation$annual$Management <- paste0(meteoScen[z],"_","IV_",IntervalTime[i],"_","IT_",ThinIntesity[j])
validation$annual$CODE <- paste0(meteoScen[z],"_","IV_",IntervalTime[i],"_","IT_",ThinIntesity[j],"_",validation$annual$YEAR)
validation$annual$Thinintensity <- ThinIntesity[j]
validation$annual$intervTime <- IntervalTime[i]
validation$annual$Meteo <- meteoScen[z]
validation$annual$Manage <- "MANAGEMENT"
validation$annual$Harv_Wood <- cumsum(validation$annual$C_HW)
validation$annual$StockCarbon <- validation$annual$Harv_Wood+validation$annual$STANDING_WOOD
validationDF <- data.frame(validation$annual)
write.table(validationDF, paste0("./dataOut/",meteoScen[z],"_","IV_",IntervalTime[i],"_","IT_",ThinIntesity[j],"_MAN_ON",".txt"),quote = F, sep = "\t",row.names = F,col.names = T)
모든 실행 모델에 대한 rbind
mapUnc <- rbind(mapUnc, validation$annual)
}
}
}
}
}
}