[R] foreach %do% and %dopar%

ilai keren at math.montana.edu
Sat Feb 18 20:20:43 CET 2012


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



More information about the R-help mailing list