Skip to content

help with a scoping issue with foreach

3 messages · Roman Luštrik, Stephen Weston

#
Roman,

When %dopar% is executed with certain parallel backends, it analyzes
the right hand expression to determine if there are any variables that need
to be exported.  If a symbol is referenced in the expression that is defined
in the enclosing environment, it will export that variable to the parallel
workers.  If it is defined in the global environment, or in some package,
it will not automatically export it.

You don't show where "effect.distance" is defined.  If it is defined
in the global environment, you could explicitly export it via the foreach
".export" argument.  See the foreach man page for details.  If it is
defined in a package, you could use the ".packages" argument to
load that package on each of the parallel workers.

Note that this process of automatically exporting variables is performed
in doSNOW and doMPI, but not in doMC.  That is because the multicore
package uses the "fork" system call to create the worker processes.
Processes that are forked get a copy of the parent process's execution
environment, and so variables don't generally need to be exported,
either implicitly or explicitly.  Thus, you could run into problems with
undefined variables with doSNOW, even though your program runs fine
with doMC.

- Steve
On Tue, Aug 24, 2010 at 8:13 AM, Roman Lu?trik <roman.lustrik at gmail.com> wrote: