Skip to content

Commit

Permalink
Add support for availableCores(which ="/proc/self/status") [#122]
Browse files Browse the repository at this point in the history
  • Loading branch information
HenrikBengtsson committed Dec 14, 2024
1 parent ad7d6ba commit 16c31f5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: parallelly
Version: 1.40.1-9003
Version: 1.40.1-9004
Title: Enhancing the 'parallel' Package
Imports:
parallel,
Expand Down
18 changes: 17 additions & 1 deletion R/availableCores.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
#' \item `"system"` -
#' Query \code{\link[parallel]{detectCores}(logical = logical)}.
#'
#' \item `"/proc/self/status"` -
#' Query \code{Cpus_allowed_list} of `/proc/self/status`.
#'
#' \item `"cgroups.cpuset"` -
#' On Unix, query control group (cgroup v1) value \code{cpuset.set}.
#'
Expand Down Expand Up @@ -216,7 +219,7 @@
#'
#' @importFrom parallel detectCores
#' @export
availableCores <- function(constraints = NULL, methods = getOption2("parallelly.availableCores.methods", c("system", "cgroups.cpuset", "cgroups.cpuquota", "cgroups2.cpu.max", "nproc", "mc.cores", "BiocParallel", "_R_CHECK_LIMIT_CORES_", "Bioconductor", "LSF", "PJM", "PBS", "SGE", "Slurm", "fallback", "custom")), na.rm = TRUE, logical = getOption2("parallelly.availableCores.logical", TRUE), default = c(current = 1L), which = c("min", "max", "all"), omit = getOption2("parallelly.availableCores.omit", 0L)) {
availableCores <- function(constraints = NULL, methods = getOption2("parallelly.availableCores.methods", c("system", "/proc/self/status", "cgroups.cpuset", "cgroups.cpuquota", "cgroups2.cpu.max", "nproc", "mc.cores", "BiocParallel", "_R_CHECK_LIMIT_CORES_", "Bioconductor", "LSF", "PJM", "PBS", "SGE", "Slurm", "fallback", "custom")), na.rm = TRUE, logical = getOption2("parallelly.availableCores.logical", TRUE), default = c(current = 1L), which = c("min", "max", "all"), omit = getOption2("parallelly.availableCores.omit", 0L)) {
## Local functions
getenv <- function(name, mode = "integer") {
value <- trim(getEnvVar2(name, default = NA_character_))
Expand Down Expand Up @@ -368,6 +371,19 @@ availableCores <- function(constraints = NULL, methods = getOption2("parallelly.
} else if (method == "system") {
## Number of cores available according to parallel::detectCores()
n <- detectCores(logical = logical)
} else if (method == "/proc/self/status") {
pathname <- "/proc/self/status"
if (file_test("-f", pathname)) {
bfr <- readLines(pathname, warn = FALSE)
bfr <- grep("^Cpus_allowed_list:", bfr, value = TRUE)
if (length(bfr) == 1) {
bfr <- sub("^Cpus_allowed_list:\t", "", bfr)
if (nzchar(bfr)) {
bfr <- slurm_expand_nodelist(sprintf("[%s]", bfr))
n <- length(bfr)
}
}
}
} else if (method == "cgroups.cpuset") {
## Number of cores according to Unix cgroups v1 CPU set
n <- length(getCGroups1CpuSet())
Expand Down
11 changes: 7 additions & 4 deletions man/availableCores.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 16c31f5

Please sign in to comment.