Hi everyone, I'm working on a script trying to use foreach %dopar% but without success, so I manage to run the code with foreach %do% and looks like this:
The code is part of a MCMC model for projects valuation, returning the most important results (VPN, TIR, EVA, etc.) of the simulation. foreach (simx = NsimT, .combine=cbind, .inorder=FALSE, .verbose=TRUE) %do% { MCPVMPA = MCVAMPA[simx] #The *[simx] variables are vectors containing 100,000 simulations of each variable. MCPVMPB = MCVAMPB[simx] #Wich then I want to parse to the script below MCPVMPC = MCVAMPC[simx] #In order for the model to take the values of each variable. MCPVMPD = MCVAMPD[simx] source(paste(pathglobal, "Valuation.R", sep="")) #This script does everyting #Before you suggest making it a function, let me say I CAN'T. #This script is about 3000 lines long, and connects to several other scripts (8), each about 300-500 lines long. #pathglobal is a string variable pointing to the working directory, ej. ~/Documents/Valuation/ MCVRERM[simx] <- sum(CIFERRN) #This is my response variable 1 MCVRWKM[simx] <- WKMTWKZ[12] #This is my response variable 2 MCVRFNM[simx] <- sum(FNMSDE) #This is my response variable 3 #The response variables come from another script which is call within Valuation.R } As I said, the above code works flawlessly with %do%, but it takes about 1 hour just to run 2,000 simulations, so I want to make use of all the 8 cores of my Intel i7 machine by using %dopar%, so I changed the code to something like this: foreach (simx = NsimT, .combine=cbind, .inorder=FALSE, .verbose=TRUE, .options.smp= source(paste(pathglobal, "Valuation.R", sep=""))) %dopar% { MCPVMPA = MCVAMPA[simx] MCPVMPB = MCVAMPB[simx] MCPVMPC = MCVAMPC[simx] MCPVMPD = MCVAMPD[simx] MCVRERM[simx] <- sum(CIFERRN) MCVRWKM[simx] <- WKMTWKZ[12] MCVRFNM[simx] <- sum(FNMSDE) } The changes makes the script fail, saying somtehing like: abscent value where TRUE/FALSE is necessary, the variables that are supposed to pass the information to the Valuation.R script are empty, and error comes because it does something like this at some point: VarX = A/B #But because the variables values are not getting into the Valuation.R script then #A = MCPVMPA = 0 and B = MCMKMPA = 0 So VarX = NaN Then it does something like: if (VarZ>VarX) VarY = 0 else VarY = VarX-VarZ #This line generates the error I have all the libraries installed: library(foreach) library(doMC) registerDoMC() It's not the installation because all the example code with %dopar% I've tested work fine. So can you help me please to make my code work? Any ideas? -- L.E. Marcos Larios Sata Rosa [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.