Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Parlappy-如何解决错误“;找不到函数“;bindToEnv"&引用;?_R_Parallel Processing_Lapply - Fatal编程技术网

Parlappy-如何解决错误“;找不到函数“;bindToEnv"&引用;?

Parlappy-如何解决错误“;找不到函数“;bindToEnv"&引用;?,r,parallel-processing,lapply,R,Parallel Processing,Lapply,我想使用Parlappy,我正在设置我的代码,就像这里介绍的那样: 最近几次,它运行良好。然而,通过我当前的parlappy调用,我得到了错误 checkForRemoteErrors(val)中的错误:3个节点产生错误;第一个错误:找不到函数“bindToEnv” 这里有一个简短的例子: #' Copy arguments into env and re-bind any function's lexical scope to bindTargetEnv . #' #' See http:/

我想使用Parlappy,我正在设置我的代码,就像这里介绍的那样:

最近几次,它运行良好。然而,通过我当前的
parlappy
调用,我得到了错误 checkForRemoteErrors(val)中的
错误:3个节点产生错误;第一个错误:找不到函数“bindToEnv”

这里有一个简短的例子:

#' Copy arguments into env and re-bind any function's lexical scope to bindTargetEnv .
#' 
#' See http://winvector.github.io/Parallel/PExample.html for example use.
#' 
#' 
#' Used to send data along with a function in situations such as parallel execution 
#' (when the global environment would not be available).  Typically called within 
#' a function that constructs the worker function to pass to the parallel processes
#' (so we have a nice lexical closure to work with).
#' 
#' @param bindTargetEnv environment to bind to
#' @param objNames additional names to lookup in parent environment and bind
#' @param names of functions to NOT rebind the lexical environments of
bindToEnv <- function(bindTargetEnv=parent.frame(),objNames,doNotRebind=c()) {
  # Bind the values into environment
  # and switch any functions to this environment!
  for(var in objNames) {
    val <- get(var,envir=parent.frame())
    if(is.function(val) && (!(var %in% doNotRebind))) {
      # replace function's lexical environment with our target (DANGEROUS)
      environment(val) <- bindTargetEnv
    }
    # assign object to target environment, only after any possible alteration
    assign(var,val,envir=bindTargetEnv)
  }
}

ccc <- 1

# Parallel
cl <- parallel::makeCluster(getOption("cl.cores", 3))
junk <- parallel::clusterEvalQ(cl, c(library(data.table)))

f <- function(x) {
  bindToEnv(objNames = 'ccc')

  return(x+x)  
}

b <- do.call(rbind, parallel::parLapply(cl, 1:10,  f))
将参数复制到env中,并将任何函数的词法作用域重新绑定到bindTargetEnv。 #' #”“看到了吗http://winvector.github.io/Parallel/PExample.html 例如使用。 #' #' #'用于在并行执行等情况下随函数发送数据 #"(当全球环境无法提供时)。通常在 #'构造传递给并行进程的辅助函数的函数 #(因此我们有一个很好的词汇闭包)。 #' #“@param bindTargetEnv要绑定到的环境 #“@param objNames是要在父环境中查找和绑定的其他名称 #“@param函数名,以不重新绑定
bindToEnv在创建集群之前,需要使用
clusterExport()
导出已使用的函数和定义的对象

library(parallel)
cl <- makeCluster(getOption("cl.cores", 3))
clusterEvalQ(cl, c(library(data.table)))
clusterExport(cl, c("bindToEnv", "ccc"), 
              envir=environment())
f <- function(x) {
  bindToEnv(objNames='ccc')
  return(x+x)  
}

b <- do.call(rbind, parallel::parLapply(cl, 1:10,  f))
b
#        ,1]
#  [1,]    2
#  [2,]    4
#  [3,]    6
#  [4,]    8
#  [5,]   10
#  [6,]   12
#  [7,]   14
#  [8,]   16
#  [9,]   18
# [10,]   20

stopCluster(cl)
库(并行)

cl可能是因为您直接尝试调用名称空间,当您将
库(并行)
包含到
集群evalq(.)
中时会发生什么?或者先在函数中尝试
parallel::bindToEnv
。不幸的是,错误仍然出现。我将
library(parallel)
添加到常规代码和
clusterEvalQ()
函数中。我无法添加
parallel::bindToEnv
,因为
bindToEnv
是一个自己编写的函数(或者更准确地说是从internet复制的),而不是来自并行程序包的函数。我添加到问题中的代码示例是否不可复制?它为我创建了错误,我不知道如何进一步减少代码量。感谢它与
clusterExport
一起工作!我仍然有点困惑为什么它一开始就不起作用,因为像以前那样设置
parlappy
。我认为逻辑就是启动集群之前创建的所有东西都必须导出。