R - problem with foreach %dopar% inside function called by optim

后端 未结 3 2031
孤城傲影
孤城傲影 2020-12-28 17:22

Calling a function that includes foreach %dopar% construct from optim causes an error:

> workers <- startWorkers(6) # 6 cores
> 
> registerDoSMP(         


        
3条回答
  •  失恋的感觉
    2020-12-28 17:54

    [[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)
    }
    

提交回复
热议问题