Elai,

The <<- trick is of no use, and sourcing the script within the loop gives
me nothing, if i put it in .option all variables are loaded to my
workspace; though, they're empty and generates some errors. I'll put a
reproducible example:

Lets call this script MC:

library(foreach)
library(doMC)
registerDoMC()
library(iterators)

pathglobal <- "~/Documents/RModel/"
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

#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 <- 0
MCPVMPB <- 0
MCPVMPC <- 0
MCPVMPD <- 0
MCPVPPA <- 0
MCPVPPB <- 0
MCPVPPC <- 0
MCPVPPD <- 0

#I also have to set the response variables before the loop.
MCVRERM <- array(0,dim=c(0,Nsim))
MCVRWKM <- array(0,dim=c(0,Nsim))
MCVRFNM <- array(0,dim=c(0,Nsim))
MCVRFEM <- array(0,dim=c(0,Nsim))

#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 <<- 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]

#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 <- 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)

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 a a 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 <- c(1,9)[i] ;
> source('myscript.R')$value }
> # local assignment fails
> foreach(i = 1:2) %dopar% {somevar <<- c(1,9)[i] ;
> source('myscript.R')$value }
> # assign to the global environment works
>
> Bottom line try to change all the equal signs to "<<-" and source the
> 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] <- 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.
>



-- 
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.

Reply via email to