开发者

parallel computing of a function with doSMP on Windows

开发者 https://www.devze.com 2023-01-10 13:13 出处:网络
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 p

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 开发者_JAVA技巧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 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.


In the foreach function, there is an argument to pass if you have packages to load in order to execute the function fun2

Typically in your example, if fun1 is part of the package PACKAGE1, execute

obj.result <- foreach(i = 1:10000, .packages="PACKAGE1") %dopar% func2(i)

instead of

obj.result <- foreach(i = 1:10000) %dopar% func2(i)
0

精彩评论

暂无评论...
验证码 换一张
取 消