views:

37

answers:

1

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%?

A: 

It looks like a scoping issue.

Your func1 is 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.

Dirk Eddelbuettel
Thank you Dirk for your time, but I don't see how the vignette you mention covers the scoping issue (it may be me being somewhat dense). The code seems to work for now and let's hope it stays that way. I sometimes feel like R is a kind of magic (do you hear music in the background? :) ).
Roman Luštrik