parSapply在全局环境中找不到对象 [英] parSapply not finding objects in global environment

查看:170
本文介绍了parSapply在全局环境中找不到对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在多个内核上运行代码(我尝试了snowparallel软件包).我有

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

cl <- makeCluster(2)
y  <- 1:10
sapply(1:5, function(x) x + y)  # Works
parSapply(cl, 1:5, function(x) x + y)

最后一行返回错误:

Error in checkForRemoteErrors(val) : 
  2 nodes produced errors; first error: object 'y' not found

很显然,parSapply在全局环境中找不到y.有什么办法可以解决这个问题?谢谢.

Clearly parSapply isn't finding y in the global environment. Any ways to get around this? Thanks.

推荐答案

节点在主服务器上的全局环境中不了解y.您需要以某种方式告诉他们.

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)

这篇关于parSapply在全局环境中找不到对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆