R-optim调用foreach%dopar%内部函数的问题 [英] R - problem with foreach %dopar% inside function called by optim
问题描述
从optim调用包含foreach%dopar%构造的函数会导致错误:
Calling a function that includes foreach %dopar% construct from optim causes an error:
> workers <- startWorkers(6) # 6 cores
>
> registerDoSMP(workers)
>
> t0 <- Sys.time()
>
> optim(w,maxProb2,control=list(fnscale=-1))
>
> Error in { : task 1 failed - "unused argument(s) (isPrebuilt = TRUE)"
>
> Sys.time()-t0
>
> Time difference of 2.032 secs
>
> stopWorkers(workers)
被调用函数如下:
> maxProb2 <- function(wp) {
>
> r <- foreach (i=s0:s1, .combine=c) %dopar% { pf(i,x[i,5],wp,isPrebuilt=TRUE) }
>
> cat("w=",wp,"max=",sum(r),"\n")
>
> sum(r)
>
> }
pf是其他函数,x是预先计算的元素的静态表.
pf is some other function, x is a static table of pre-computed elements.
也仅一次调用要优化的函数会导致相同的错误:
Also calling the function to be optimized just once causes the same error:
> workers <- startWorkers(6) # 6 cores
>
> Warning message:
> In startWorkers(6) : there is an existing doSMP session using doSMP1
>
> registerDoSMP(workers)
>
> maxProb2(w)
> Error in { : task 1 failed - "unused argument(s) (isPrebuilt = TRUE)"
>
> stopWorkers(workers)
奇怪的是,一次直接调用相同的代码就可以正常工作(optim多次调用同一函数):
What's strange, the identical code works fine when called directly a single time (optim calles the same function many times):
> workers <- startWorkers(6) # 6 - ilosc rdzeni
>
> Warning message:
> In startWorkers(6) : there is an existing doSMP session using doSMP1
>
> registerDoSMP(workers)
>
> r <- foreach (i=s0:s1, .combine=c) %dopar% { pf(i,x[i,5],w,isPrebuilt=TRUE) }
>
> sum(r)
> [1] 187.1781
>
> stopWorkers(workers)
使用%do%代替%dopar%时,调用的函数(maxProb2)可以正常工作.
The called function (maxProb2) works fine, when %do% is used instead of %dopar%.
如何正确调用包含foreach%dopar%构造的函数?
更新2011-07-17:
UPDATE 2011-07-17:
我已将pf函数重命名为probf,但问题仍然存在.
I have renamed the pf function into probf but the problem remains.
probf函数是在脚本中定义的,而不是在某些外部程序包中定义的.
probf functions is defined in the script, not in some external package.
两个注意事项:操作系统:Windows 7,IDE:Revolution Analytics Enterprise 4.3
Two notes: OS: Windows 7, IDE: Revolution Analytics Enterprise 4.3
> workers <- startWorkers(workerCount = 3)
>
> registerDoSMP(workers)
>
> maxProb2(w)
>
Error in { : task 1 failed - "could not find function "probf""
推荐答案
我遇到了同样的问题,即子线程中未包含环境.您的错误
I ran into the same problem an the issue is with the environment not being included in the sub-threads. Your error
{中的错误:任务1失败-找不到函数"simple_fn""
Error in { : task 1 failed - "could not find function "simple_fn""
可以通过以下非常简单的示例进行复制:
can be reproduced by this very simple example:
simple_fn <- function(x)
x+1
test_par <- function(){
library("parallel")
no_cores <- detectCores()
library("foreach")
cl<-makeCluster(no_cores)
library("doSNOW")
registerDoSNOW(cl)
out <- foreach(i=1:10) %dopar% {
simple_fn(i)
}
stopCluster(cl)
return(out)
}
test_par()
现在您要做的就是将foreach(i=1:10)
更改为foreach(i=1:10, .export=c("simple_fn"))
.如果要导出完整的全局环境,则只需编写.export=ls(envir=globalenv())
,无论情况好坏,我们都会使用它.
Now all you need to to is to change the foreach(i=1:10)
into foreach(i=1:10, .export=c("simple_fn"))
. If you want to export your complete global environment then just write .export=ls(envir=globalenv())
and you will have it for better or worse.
这篇关于R-optim调用foreach%dopar%内部函数的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!