Calling a function that includes foreach %dopar% construct from optim causes an error:
> workers <- startWorkers(6) # 6 cores
>
> registerDoSMP(
[[Edited]]
Your pf
function and your "static table" x
must be distributed to all worker nodes. You must read the documentation for your parallel library on how that works.
It seems to be that when run through optim, the pf
function it finds is another one (probably stats::pf
, which does not have an isPrebuilt
argument).
Can you try renaming your pf
function (for example to mypf
)?
mypf <- pf # renaming the function
maxProb2 <- function(wp) {
r <- foreach (i=s0:s1, .combine=c) %dopar% { mypf(i,x[i,5],wp,isPrebuilt=TRUE) }
cat("w=",wp,"max=",sum(r),"\n")
sum(r)
}
Or, if your pf
function is part of a package with a namespace (say, mypackage
), you could reference it like this: mypackage::pf
maxProb2 <- function(wp) {
r <- foreach (i=s0:s1, .combine=c) %dopar% { mypackage::pf(i,x[i,5],wp,isPrebuilt=TRUE) }
cat("w=",wp,"max=",sum(r),"\n")
sum(r)
}