我正在本地并行运行任务,使用包中的%dopar%
包来创建集群(目前在 Windows 机器上运行)。我以前做过很多次,它工作正常,直到我在其中使用(即非并行)放置一个不相关的循环。然后 R 给了我错误(带有回溯):foreach
doSNOW
foreach
%do%
Error in { : task 1 failed - "could not find function "%do%"" 3 stop(simpleError(msg, call = expr)) 2 e$fun(obj, substitute(ex), parent.frame(), e$data) 1 foreach(rc = 1:5) %dopar% {
aRandomCounter = -1
if (1 > 0) {
for (batchi in 1:20) { ...
这是一些在我的机器上复制问题的代码:
require(foreach)
require(doSNOW)
cl<-makeCluster(5)
registerDoSNOW(cl)
for(stepi in 1:10) # normal outer for
{
foreach(rc=1:5) %dopar% # the time consuming stuff in parallel (not looking to actually retrieve any data)
{
aRandomCounter = -1
if(1 > 0)
{
for(batchi in 1:20)
{
anObjectIwantToCreate <- foreach( qrc = 1:100, .combine=c ) %do%
{
return(runif(1)) # I know this is not efficient, it is a placeholder to reproduce the issue
}
aRandomCounter = aRandomCounter + sum(anObjectIwantToCreate > 0.5)
}
}
return(aRandomCounter)
}
}
stopCluster(cl)
foreach
用简单的for
or替换内部(l/s)apply
是一种解决方案。但是有没有办法让它与内部一起工作foreach
,为什么首先会出现错误?