I’m trying to multicore a function (in Windows), which, at one point, calls another workhorse function (function within function). Here is a minimal working example. You will need doSMP and revoIPC packages (to get them, see Tal’s post here).
func1 <- function(x) {sqrt(x)}
func2 <- function(y) {
func1(y)
}
library(doSMP)
wrk <- startWorkers(workerCount = 4) #I have 4 cores, so adjust to your specs
registerDoSMP(wrk)
obj.result <- foreach(i = 1:10000) %dopar% func2(i)
The above routine doesn’t work, but if I nest func1 within func2 like so
func2 <- function(y) {
func1 <- function(x) {sqrt(x)}
func1(y)
}
the process goes through smoothly (as far as I can tell).
How can I call functions from outside with %dopar%?
It looks like a scoping issue.
Your
func1is known in the calling workspace but not on the compute nodes. There are solutions for that, e.g. the foreach package has an entire vignettes entitled Nesting Foreach Loops.