Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/R-CMD-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ jobs:
- {os: ubuntu-latest, r: 'oldrel-1'}
- {os: ubuntu-latest, r: 'oldrel-2'}
- {os: ubuntu-latest, r: 'oldrel-3'}

- {os: ubuntu-24.04-arm, r: 'release'}

env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
Expand Down
8 changes: 6 additions & 2 deletions R/Utility.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
stdThreadMax <- function() {
nThreads <- .Call(`_RcppAlgos_cpp11GetNumThreads`)
if (nThreads < 1L || is.na(nThreads) || is.null(nThreads)) nThreads = 1L
return(nThreads)

if (is.null(nThreads) || is.na(nThreads) || nThreads < 1L) {
nThreads <- 1L
}

as.integer(nThreads)
}

GetV <- function(v) {
Expand Down
66 changes: 17 additions & 49 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,21 @@ pkgEnv$nCores <- NULL
pkgEnv$maxThreads <- NULL

physicalCoreCount <- function() {
## Estimate physical cores using R's default detection path. This value is
## used for cache-size heuristics, not as the authoritative thread limit.
## If physical core detection is unavailable, fall back to logical cores
## and finally to 1.
n <- parallel::detectCores(logical = FALSE)

if (is.na(n) || n < 1L) {
n <- parallel::detectCores(logical = TRUE)
}

if (grepl("darwin|solaris", R.version$os) || .Platform$OS.type == "windows")
return(parallel::detectCores(logical = FALSE))

## According to R News as of # 3.4.4
## "parallel::detectCores(logical = FALSE) is ignored on Linux systems, since
## the information is not available with virtualized OSes."
##
## This implementation tries the old command available from version 3.3.0 until
## 3.4.3 (It was removed in 3.4.4), as I have had success with this command on
## several Linux flavors. If this fails, we also try the method outlined
## by user teambob in an answer to:
## "How to obtain the number of CPUs/cores in Linux from the command line?"
## https://stackoverflow.com/a/18051445/4408538
##
## Otherwise, the implementation below is nearly identical to parallel::detectCores
## with the argument logical set to FALSE. You will note that we have excluded
## freevsd, openbsd, & irix as they do not have this option available.

systems <- list(linux = c("cat /proc/cpuinfo | grep 'cpu cores'| uniq | cut -f2 -d:",
"grep '^core id' /proc/cpuinfo |sort -u|wc -l"),
darwin = "/usr/sbin/sysctl -n hw.physicalcpu 2>/dev/null",
solaris = "/bin/kstat -p -m cpu_info | grep :core_id | cut -f2 | uniq | wc -l")

for (i in seq(systems)) if (grepl(paste0("^", names(systems)[i]), R.version$os))
for (cmd in systems[i]) {
if (is.null(a <- tryCatch(suppressWarnings(
system(cmd, TRUE)), error = function(e) NULL))) {next}

a <- gsub("^ +", "", a[1])
if (grepl("^[1-9]", a))
return(as.integer(a))
}

## if the above fails, we try all of them
for (i in seq(systems))
for (cmd in systems[i]) {
if (is.null(a <- tryCatch(suppressWarnings(
system(cmd, TRUE)), error = function(e) NULL))) {next}

a <- gsub("^ +", "", a[1])
if (grepl("^[1-9]", a))
return(as.integer(a))
}

## If we get here, we assume 1 core
1L
if (is.na(n) || n < 1L) {
1L
} else {
as.integer(n)
}
}

## This will set the maximum number of cores
Expand All @@ -62,10 +30,10 @@ physicalCoreCount <- function() {
pkgEnv$nCores <- physicalCoreCount()
tempThreads <- stdThreadMax()

if (is.na(tempThreads)) {
pkgEnv$maxThreads <- 1L
pkgEnv$maxThreads <- if (is.na(tempThreads) || tempThreads < 1L) {
1L
} else {
pkgEnv$maxThreads <- tempThreads
as.integer(tempThreads)
}

invisible()
Expand Down
Loading