Marcos, This worked for me. Can't say why it didn't for you. Did you make sure to source AFTER you define the variables ? Not doing so is the only reason I can think of why they would be "empty". Here I'm running your example (I called the script that needs to be sourced "src.R" and put it in the same working directory):
library(doMC) registerDoMC() NsimT <- iter(1:200) #Number of iterations Nsim = 200 #Number of random scenarios should be equal to NsimT #This are sells per month in units MCVAMPA <- round(rnorm(Nsim, mean=350, sd=35),0) #Product A MCVAMPB <- round(rnorm(Nsim, mean=390, sd=39),0) #Product B MCVAMPC <- round(rnorm(Nsim, mean=460, sd=46),0) #Product C MCVAMPD <- round(rnorm(Nsim, mean=410, sd=41),0) #Product D #this are prices per month for each product. MCVAPPA <- rnorm(Nsim, mean=1750, sd=155) #Product A MCVAPPB <- rnorm(Nsim, mean=1700, sd=150) #Product B MCVAPPC <- rnorm(Nsim, mean=1570, sd=137) #Product C MCVAPPD <- rnorm(Nsim, mean=1635, sd=143.5) #Product D ## Don't need a bunch of 0 arrays out <- foreach (simx = NsimT) %dopar% { MCPVMPA <<- MCVAMPA[simx] MCPVMPB <<- MCVAMPB[simx] MCPVMPC <<- MCVAMPC[simx] MCPVMPD <<- MCVAMPD[simx] #With this I pass from the vector just one value to do the calculations MCPVPPA <<- MCVAPPA[simx] MCPVPPB <<- MCVAPPB[simx] MCPVPPC <<- MCVAPPC[simx] MCPVPPD <<- MCVAPPD[simx] source('src.R') c(CIFERRN,WKMTWKZ,FNMSDE,OARMTFE) } out <- do.call('rbind',out) colnames(out) <- c('CIFERRN','WKMTWKZ','FNMSDE','OARMTFE') summary(out) ### DID IT WORK ? #### The src.R file ##### #We need the value of the sells per month for each variable. PVMA <- MCPVMPA PVMB <- MCPVMPB PVMC <- MCPVMPC PVMD <- MCPVMPD #We make a nice gompertz growth curve for our products. GOMPEXP <- as.double(c(1,1.05,1.1,1.15,1.2,1.3,1.5,1.75,1.9,2,2.25,2.5)) #This is the factor for each month #Growth Product A. PVMPA = as.double(SSgompertz(GOMPEXP, PVMA, PVMA*0.25, 0.025)) #This is the gompertz curve. PVMPA = as.integer(PVMPA) #We need it to be integer, we can't sell 0.25 units of product. #One big note: if I put as.integer(SSgompertz(GOMPEXP, PVMA, PVMA*0.25, 0.025)) #The growth of the curve is very abrupt, in the way I do it, you get a smoother curve. #I don't know why, it just does. #Growth Product B. PVMPB = as.double(SSgompertz(GOMPEXP, PVMB, PVMB*0.25, 0.025)) PVMPB = as.integer(PVMPB) #Growth Product C. PVMPC = as.double(SSgompertz(GOMPEXP, PVMC, PVMC*0.25, 0.025)) PVMPC = as.integer(PVMPC) #Growth Product D. PVMPD = as.double(SSgompertz(GOMPEXP, PVMD, PVMD*0.25, 0.025)) PVMPD = as.integer(PVMPD) #Then we need the price list of each product. PVPPA <- MCPVPPA PVPPB <- MCPVPPB PVPPC <- MCPVPPC PVPPD <- MCPVPPD #Calculate the value of the sells per month PVVVA <- as.double(PVPPA*PVMPA) PVVVB <- as.double(PVPPB*PVMPB) PVVVC <- as.double(PVPPC*PVMPC) PVVVD <- as.double(PVPPD*PVMPD) #Then my response variables, which are the total value of my sells in the year.# CIFERRN = sum(PVVVA) WKMTWKZ = sum(PVVVB) FNMSDE = sum(PVVVC) OARMTFE = sum(PVVVD) #### END ##### On , Marcos Larios <mlario...@gmail.com> wrote: > Elai, > The > Lets call this script MC: > library(foreach) > library(doMC) > registerDoMC() > library(iterators) > pathglobal NsimT Nsim = 200 #Number of random scenarios should be equal > to NsimT > #This are sells per month in units > MCVAMPA MCVAMPB MCVAMPC > MCVAMPD > #this are prices per month for each product. > MCVAPPA MCVAPPB > MCVAPPC MCVAPPD > #I've to set the variables first or the loop wont even start > #I don't know if this is my fault or not. > MCPVMPA MCPVMPB MCPVMPC MCPVMPD MCPVPPA MCPVPPB MCPVPPC MCPVPPD > #I also have to set the response variables before the loop. > MCVRERM > MCVRWKM MCVRFNM MCVRFEM > #Here comes the loop > foreach (simx = NsimT, .inorder=FALSE, .verbose=TRUE, .options.smp= > source(paste(pathglobal, "Valuation.R", sep=""))) %dopar% { > #With this I pass from the vector just one value to do the calculations > MCPVMPA MCPVMPB MCPVMPC MCPVMPD > #With this I pass from the vector just one value to do the calculations > MCPVPPA MCPVPPB MCPVPPC MCPVPPD > #Then I set my "answers" variables > MCVRERM[simx] = CIFERRN > MCVRWKM[simx] = WKMTWKZ > MCVRFNM[simx] = FNMSDE > MCVRFEM[simx] = OARMTFE > } > #This creates a histogram with all possible results for each variable > hist(x=MCVRERM) > hist(x=MCVRWKM) > hist(x=MCVRFNM) > hist(x=MCVRFEM) > This is the end of the script, now we go to another script, the one that > is supposed to get sourced from the script above, lets call the script > Valuation.R > #We need the value of the sells per month for each variable. > PVMA PVMB PVMC PVMD > #We make a nice gompertz growth curve for our products. > GOMPEXP 2,1.3,1.5,1.75,1.9,2,2.25,2.5)) #This is the factor for each month > #Growth Product A. > PVMPA = as.double(SSgompertz(GOMPEXP, PVMA, PVMA*0.25, 0.025)) #This is > the gompertz curve. > PVMPA = as.integer(PVMPA) #We need it to be integer, we can't sell 0.25 > units of product. > #One big note: if I put as.integer(SSgompertz(GOMPEXP, PVMA, PVMA*0.25, > 0.025)) > #The growth of the curve is very abrupt, in the way I do it, you get a > smoother curve. > #I don't know why, it just does. > #Growth Product B. > PVMPB = as.double(SSgompertz(GOMPEXP, PVMB, PVMB*0.25, 0.025)) > PVMPB = as.integer(PVMPB) > #Growth Product C. > PVMPC = as.double(SSgompertz(GOMPEXP, PVMC, PVMC*0.25, 0.025)) > PVMPC = as.integer(PVMPC) > #Growth Product D. > PVMPD = as.double(SSgompertz(GOMPEXP, PVMD, PVMD*0.25, 0.025)) > PVMPD = as.integer(PVMPD) > #Then we need the price list of each product. > PVPPA PVPPB PVPPC PVPPD > #Calculate the value of the sells per month > PVVVA PVVVB PVVVC PVVVD > #Then my response variables, which are the total value of my sells in the > year. > CIFERRN = sum(PVVVA) > WKMTWKZ = sum(PVVVB) > FNMSDE = sum(PVVVC) > OARMTFE = sum(PVVVD) > This is the end of the script. I really hope someone can help me. > On Sat, Feb 18, 2012 at 1:20 PM, ilai ke...@math.montana.edu> wrote: > Marcos, > Untested because you didn't provide a reproducible example but my > guess, the problem is in the local assignment of MCPVMP*. The %do% > worked just because it operates in the same (local) environment for > all threads. I'll try to clarify with aa self contained example of > sourcing a script with "calculations" one some variables (take the > sqrt): > cat('sqrt(somevar)',file='myscript.R') > library(doMC) > registerDoMC() > foreach(i = 1:2) %dopar% {somevar > # local assignment fails > foreach(i = 1:2) %dopar% {somevar > # assign to the global environment works > Bottom line try to change all the equal signs to " > script in the loop (I don't think passing it to .options.smp will work > in this context). > Two unrelated minor points > 1) is NsimT a vector 1:2000 or of class 'iter' ? just a number like > simx=2000 is not right > 2) With intel's hyperthreading you may really have only 4 real cores not 8 > Hope this helps > Elai > On Fri, Feb 17, 2012 at 11:41 PM, Marcos Larios mlario...@gmail.com> > wrote: > > 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] > > MCVRWKM[simx] > > MCVRFNM[simx] > > #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] > > MCVRWKM[simx] > > MCVRFNM[simx] > > } > > > > 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? > > > > > > -- > > LE 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. > -- > LE 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.