Skip to content

foreach %do% and %dopar%

3 messages · ilai, Marcos Larios

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