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

后端 未结 3 2033
孤城傲影
孤城傲影 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 18:16

    I ran into the same problem an the issue is with the environment not being included in the sub-threads. Your error

    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()
    

    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.

提交回复
热议问题