parSapply not finding objects in global environment

前端 未结 2 1918
旧时难觅i
旧时难觅i 2020-12-29 04:23

I am trying to run code on several cores (I tried both the snow and parallel packages). I have

cl <- makeCluster(2)
y  <- 1:1         


        
2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-29 04:44

    The nodes don't know about the y in the global environment on the master. You need to tell them somehow.

    library(parallel)
    cl <- makeCluster(2)
    y  <- 1:10
    # add y to function definition and parSapply call
    parSapply(cl, 1:5, function(x,y) x + y, y)
    # export y to the global environment of each node
    # then call your original code
    clusterExport(cl, "y")
    parSapply(cl, 1:5, function(x) x + y)
    

提交回复
热议问题