简体   繁体   English

在Windows上使用doSMP并行计算功能

[英]parallel computing of a function with doSMP on Windows

I'm trying to multicore a function (in Windows), which, at one point, calls another workhorse function (function within function). 我正在尝试对一个函数进行多核(在Windows中),该函数有时会调用另一个主力函数(函数内的函数)。 Here is a minimal working example. 这是一个最小的工作示例。 You will need doSMP and revoIPC packages (to get them, see Tal's post here ). 您将需要doSMPrevoIPC软件包(要获取它们,请参阅此处的 Tal文章)。

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 上面的例程不起作用,但是如果我像这样将func1嵌套在func2

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% ? 如何使用%dopar%从外部调用函数?

It looks like a scoping issue . 看起来像是一个范围问题

Your func1 is known in the calling workspace but not on the compute nodes. 您的func1在调用工作区中是已知的,但在计算节点上却不是。 There are solutions for that, eg the foreach package has an entire vignettes entitled Nesting Foreach Loops . 有解决方案,例如, foreach包中有一个名为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 在foreach函数中,如果您有要执行功能fun2的包要加载,则有一个参数要传递

Typically in your example, if fun1 is part of the package PACKAGE1, execute 通常在您的示例中,如果fun1是软件包PACKAGE1的一部分,请执行

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

instead of 代替

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM