【问题标题】:R CRAN Check fail when using parallel functions使用并行功能时 R CRAN 检查失败
【发布时间】:2018-05-28 17:45:00
【问题描述】:

我想向 CRAN 提交一个使用 parallel::makeCluster(parallel::detectCores()) 进行并行计算的包。

当我构建包时一切正常,但是当我检查包时 (devtools::check(document = FALSE)) 它返回错误:

Running examples in ‘TESTER-Ex.R’ failed
The error most likely occurred in:

> base::assign(".ptime", proc.time(), pos = "CheckExEnv")
> ### Name: hello_world
> ### Title: Prints hello world
> ### Aliases: hello_world
> 
> ### ** Examples
> 
> hello_world()
Error in .check_ncores(length(names)) : 8 simultaneous processes spawned
Calls: hello_world -> <Anonymous> -> makePSOCKcluster -> .check_ncores
Execution halted

我在 MWE 包 (TESTER) 中重新创建了错误,它只有一个函数 hello_world,我已将整个 TESTER-package 上传到 GitHub,但它应该可以从以下函数重现。

#' Prints hello world
#'
#' @return nothing
#' @export
#'
#' @examples
#' hello_world()
hello_world <- function() {

  # initiate cluster
  cl <- parallel::makeCluster(parallel::detectCores())

  # stop cluster
  parallel::stopCluster(cl)

  cat("Hello World\n")
  return(invisible(NULL))
}

我查看了Writing R Extensions,但找不到与此问题相关的任何内容,也找不到关于 SO 的问题。

任何想法导致此错误以及如何解决它?

【问题讨论】:

    标签: r cran


    【解决方案1】:

    CRAN 将包可用的内核数限制为 2 个, 出于性能原因。 邮件列表中有一个线程, 我相信, 但我现在找不到。

    我在测试中使用类似以下的内容:

    chk <- Sys.getenv("_R_CHECK_LIMIT_CORES_", "")
    
    if (nzchar(chk) && chk == "TRUE") {
        # use 2 cores in CRAN/Travis/AppVeyor
        num_workers <- 2L
    } else {
        # use all cores in devtools::test()
        num_workers <- parallel::detectCores()
    }
    

    【讨论】:

    【解决方案2】:

    我认为问题在于未加载并行库

    在您的 NAMESPACE 文件中,您应该加载包

    import(parallel)
    

    【讨论】:

    • 看来parallel是基本R的一部分,因为我们不需要直接导入。亚历克西斯的回答是关键并解决了问题
    猜你喜欢
    • 1970-01-01
    • 2014-11-05
    • 2013-02-08
    • 1970-01-01
    • 2021-06-02
    • 2021-10-02
    • 1970-01-01
    • 2013-02-26
    • 1970-01-01
    相关资源
    最近更新 更多