R CRAN Check fail when using parallel functions
Asked Answered
S

2

13

I want to submit a package to CRAN that uses parallel computation using parallel::makeCluster(parallel::detectCores()).

When I build the package everything works fine, but when I check the package (devtools::check(document = FALSE)) it returns the error:

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

I recreated the error in an MWE-package (TESTER), which only has one function hello_world, I have uploaded the whole TESTER-package to GitHub but the it should be reproducible from the following function.

#' 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))
}

I have looked through Writing R Extensions but couldn't find anything related to this issue, neither could I find a question on SO.

Any ideas what causes this error and how to resolve it?

Stockstill answered 28/5, 2018 at 17:45 Comment(0)
C
36

CRAN limits the number of cores available to packages to 2, for performance reasons. There was a thread in the mailing list, I believe, but I can't find it right now.

I use something like the following in my tests:

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()
}
Collaboration answered 28/5, 2018 at 18:2 Comment(2)
I believe you mean this document: cran.r-project.org/web/packages/policies.htmlStockstill
2 cores... maybe someone should tell CRAN we are in 2020.Foothold
B
-4

I think the issue is that the library parallel is not loaded

in your NAMESPACE file you should load the package

import(parallel)
Bub answered 28/5, 2018 at 18:13 Comment(1)
It seems that parallel is part of basic R, as in, we don't need to import it directly. Alexis answer was the key and resolves the issueStockstill

© 2022 - 2024 — McMap. All rights reserved.