R中的并行R执行问题

时间:2023-01-21 16:15:21

I am using doSMP as a parallel backend in Windows 7, with R 2.12.2. I incur in an error, and would like to understand the likely cause. Here is some sample code to reproduce the error.

我在Windows 7中使用doSMP作为并行后端,使用R 2.12.2。我发生错误,并想了解可能的原因。以下是一些重现错误的示例代码。

require(foreach)
require(doSMP)
require(data.table)
wrk <- startWorkers(workerCount = 2)
registerDoSMP(wrk)
DF = data.table(x=c("b","b","b","a","a"),v=rnorm(5))
setkey(DF,x)
foreach( i=1:2)  %dopar% {
    DF[J("a"),]
}

The error message is

错误消息是

Error in { : task 1 failed - "could not find function "J""

2 个解决方案

#1


8  

I've not used doSMP, but I did some digging around and it looks like this post gets at a similar issue.

我没有使用过doSMP,但是我做了一些挖掘,看起来这个帖子遇到了类似的问题。

so it looks like you should be able to do:

所以看起来你应该能做到:

foreach( i=1:2, .packages="data.table")  %dopar% {
    DF[J("a"),]
}

I can't test as I don't have a Windows machine handy.

我无法测试,因为我没有方便的Windows机器。

#2


7  

OK, I asked Revolution computing, and Steve Weller (of RC) replied:

好吧,我问革命计算,史蒂夫韦勒(RC)回答说:

The problem is a R scoping issue. By default, foreach() will look for variables defined in it's own 'environment'. Any objects defined outside of it's scope need to be explicitly passed to it via the '.export' argument.

问题是R范围问题。默认情况下,foreach()将查找在其自己的“环境”中定义的变量。在其范围之外定义的任何对象都需要通过'.export'参数显式传递给它。

In your case, you will need to modify your 'foreach()' call to pass in the objects 'DF' and 'J':

在您的情况下,您需要修改'foreach()'调用以传入对象'DF'和'J':

...

foreach(i=1:2, .export=c("DF","J")) %dopar% {
...

I haven't tried either solution yet, but I trust both JD and RC...

我还没有尝试过任何解决方案,但我相信JD和RC ......

#1


8  

I've not used doSMP, but I did some digging around and it looks like this post gets at a similar issue.

我没有使用过doSMP,但是我做了一些挖掘,看起来这个帖子遇到了类似的问题。

so it looks like you should be able to do:

所以看起来你应该能做到:

foreach( i=1:2, .packages="data.table")  %dopar% {
    DF[J("a"),]
}

I can't test as I don't have a Windows machine handy.

我无法测试,因为我没有方便的Windows机器。

#2


7  

OK, I asked Revolution computing, and Steve Weller (of RC) replied:

好吧,我问革命计算,史蒂夫韦勒(RC)回答说:

The problem is a R scoping issue. By default, foreach() will look for variables defined in it's own 'environment'. Any objects defined outside of it's scope need to be explicitly passed to it via the '.export' argument.

问题是R范围问题。默认情况下,foreach()将查找在其自己的“环境”中定义的变量。在其范围之外定义的任何对象都需要通过'.export'参数显式传递给它。

In your case, you will need to modify your 'foreach()' call to pass in the objects 'DF' and 'J':

在您的情况下,您需要修改'foreach()'调用以传入对象'DF'和'J':

...

foreach(i=1:2, .export=c("DF","J")) %dopar% {
...

I haven't tried either solution yet, but I trust both JD and RC...

我还没有尝试过任何解决方案,但我相信JD和RC ......