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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export(ramUsageGB)
export(readTransferredObjects)
export(releaseCapacity)
export(releaseCores)
export(requestedWorkers)
export(reservationsPath)
export(reserveCapacity)
export(reserveCores)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# clusters (development version)

* New `requestedWorkers(cores, nCoresNeeded)`: the number of workers `clusterSetup()` asks the planner for (a number of `cores` is that many `"localhost"`; a host named more than once is its count; else `nCoresNeeded`; each host cut to `options(clusters.maxWorkersPerHost)` first). A caller that needs the population size before the cluster exists (fireSense_spreadFit's emulator and start population) calls it, so the rule is no longer copied. `clusterSetup()` calls it; nothing changes in what it builds.

* Inside a Slurm job (`.slurmAllocation()` non-NULL) the host headroom is not applied: no `options(clusters.memoryHeadroom)`, no `options(clusters.keepFreeCores)` and none of `freeCores()`'s `fraction` (all the job's CPUs are free, whether or not it is the whole node). A job whose memory limit cannot be read keeps the headroom, since its memory is then the node's. The job owns its allocation, so the headroom only wasted it (GPSC 2026-11-14: a 64-CPU, 454G whole-node job with 437 GB free capped 10 GB workers at 39, and a 40-worker fit waited over an hour). `.limitToJob()` flags the probe (`in_slurm_job`, not a ledger column), which `.fitCapacity()` reads, so `reserveCapacity()` follows too. Outside Slurm nothing changes.

* A fit no longer reads and rewrites the shared `hostSpeed.rds` and `fitMemory.rds` after every generation, which took 2.9 s with all workers idle at 367k rows (8 s at a full 30-day window; 0.03 s on an empty file). `DEoptimIterative()` keeps its records in memory and writes them at each rebalance (`options(clusters.rebalanceEvery)`, default 100 generations), which reads them, and when the fit ends, under the same lock; the files, columns and record semantics are unchanged, so other versions keep sharing them. A fit that is killed loses the records since its last write.
Expand Down
50 changes: 43 additions & 7 deletions R/clusterSetup.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,43 @@
#' How many workers `clusterSetup()` asks for
#'
#' The count `clusterSetup()` gives the planner ([plan_psock_min()]) as its `total`, which becomes the
#' population size `NP` when the cluster is built as asked. A caller that needs `NP` before the cluster
#' exists (a start population, an emulator) calls this with the `cores` and `nCoresNeeded` it will pass.
#' `clusterSetup()` calls it, so the rule is in one place.
#'
#' @details `cores` is first turned into one host name per worker: a single number is that many `"localhost"`
#' (`cores = 1L` is one host name), and each host is cut to `options(clusters.maxWorkersPerHost)` (with the
#' message `clusterSetup()` gives). Then, when a host is named more than once the count is the number of
#' names left, else it is `nCoresNeeded`. It is what is asked for: the planner can build fewer when a
#' host's free memory or cores, or the caps on other clusters' bookings, hold fewer.
#'
#' @param cores As in [clusterSetup()]: host names, or a number.
#' @param nCoresNeeded As in [clusterSetup()].
#' @return An integer: the number of workers requested; `nCoresNeeded` (as an integer) when `cores` is
#' `NULL`, as no cluster is built then.
#' @export
#' @examples
#' requestedWorkers(40L, 100) # 40
#' requestedWorkers(c("a", "b"), 100) # 100: distinct hosts, planner places nCoresNeeded
#' requestedWorkers(rep("a", 30), 100) # 30: a repeated host is that many workers
requestedWorkers <- function(cores, nCoresNeeded = 100) {
if (is.null(cores)) return(as.integer(nCoresNeeded))
.workerTotal(.requestedHosts(cores), nCoresNeeded)
}

## The count for hosts already through .requestedHosts(): a host named more than once is that many workers,
## else nCoresNeeded. clusterSetup() calls it on its own .requestedHosts() result, so the cap is applied once.
.workerTotal <- function(hosts, nCoresNeeded) {
as.integer(if (anyDuplicated(hosts)) length(hosts) else nCoresNeeded)
}

## `cores` as one host name per worker: a number repeats "localhost", each host is cut to its cap, and this
## machine's ssh names become "localhost".
.requestedHosts <- function(cores) {
if (is.numeric(cores)) cores <- rep("localhost", cores)
changeNodenameToLocalhost(.trimToHostCap(cores))
}

#' Setup a cluster for DEoptim
#'
#' This includes copying files over to unique(cores) machines, then loading all objects from disk in
Expand Down Expand Up @@ -27,7 +67,7 @@
#' @param libPath The project library, or several (a dev library, then a stable one): the master copy. Each, except R's own, is mirrored to every host in order, and workers load from them.
#' @param objsNeeded Character vector naming objects in `envir` to send to the workers.
#' @param pkgsNeeded Character vector of packages the workers must be able to load. Hosts that cannot are reported by [verifyClusterHosts()].
#' @param nCoresNeeded Integer; how many workers to aim for across all hosts.
#' @param nCoresNeeded Integer; how many workers to aim for across all hosts. See [requestedWorkers()] for the count asked for.
#' @param envir Environment holding `objsNeeded`; defaults to the caller's.
#' @param runName The `runName` the fit will be given in [DEoptimIterative()]. A host gets at most as many
#' workers as its free memory holds, at the memory per worker that fit recorded the last time it ran; see
Expand Down Expand Up @@ -64,10 +104,6 @@ clusterSetup <- function(messagePrefix = "DEoptim_",

if (!is.null(cores)) {


if (is.numeric(cores)) cores <- rep("localhost", cores)
cores <- .trimToHostCap(cores)

# Global range for ssh ports so there are not errors
# e.g., Warning: remote port forwarding failed for listen port 11173
# global_range <- 20000:40000
Expand All @@ -81,7 +117,7 @@ clusterSetup <- function(messagePrefix = "DEoptim_",
# This looks at .ssh/config, assumes that Host is used, with # comment character naming nodename
# e.g., "Host coco # A159576 n18 # Eliot Degradation"
# This should just skip and leave `cores` unchanged if that structure doesn't exist
cores <- changeNodenameToLocalhost(cores)
cores <- .requestedHosts(cores)

if (FALSE) { # this is for NRCan network; extracts names of all nodes in the .ssh/config file
sshLines <- readLines("~/.ssh/config")
Expand All @@ -99,7 +135,7 @@ clusterSetup <- function(messagePrefix = "DEoptim_",
# the project library -- and mirrored that to the hosts instead.
plan <- plan_psock_min(
hosts = coresUnique,
total = if (anyDuplicated(cores)) length(cores) else nCoresNeeded,
total = .workerTotal(cores, nCoresNeeded),
pkgsNeeded = pkgsNeeded,
libPath = libPath,
logPath = logPath,
Expand Down
2 changes: 1 addition & 1 deletion man/clusterSetup.Rd

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

35 changes: 35 additions & 0 deletions man/requestedWorkers.Rd

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

66 changes: 66 additions & 0 deletions tests/testthat/test-requestedWorkers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
## requestedWorkers() is the number of workers clusterSetup() asks the planner for (its `total`), so a caller
## that needs the population size before the cluster exists (fireSense_spreadFit) has it. These pin the rule
## clusterSetup() had inline: a number is that many "localhost", a repeated host is its count, else nCoresNeeded.

test_that("a number of cores is that many workers, whatever nCoresNeeded says; 1L is one host", {
withr::local_options(clusters.maxWorkersPerHost = NULL)
expect_identical(requestedWorkers(40L, 25), 40L)
expect_identical(requestedWorkers(12, 100), 12L)
expect_identical(requestedWorkers(1L, 25), 25L)
})

test_that("distinct host names give nCoresNeeded; a repeated host gives the number of names", {
withr::local_options(clusters.maxWorkersPerHost = NULL)
expect_identical(requestedWorkers(c("hostA", "hostB"), 25), 25L)
expect_identical(requestedWorkers("hostA", 25), 25L)
expect_identical(requestedWorkers(rep("hostA", 40), 25), 40L)
expect_identical(requestedWorkers(c(rep("hostA", 3), "hostB"), 25), 4L)
})

test_that("NULL cores is no cluster: nCoresNeeded", {
expect_identical(requestedWorkers(NULL, 30), 30L)
})

test_that("options(clusters.maxWorkersPerHost) cuts the count as clusterSetup() does", {
withr::local_options(clusters.maxWorkersPerHost = c(hostA = 10))
expect_message(n <- requestedWorkers(rep("hostA", 40), 25), "hostA cut by 30 to 10")
expect_identical(n, 10L)
expect_identical(requestedWorkers(c(rep("hostA", 40), rep("hostB", 5)), 25), 15L)
## cut to one worker per host: no repeat left, so nCoresNeeded
withr::local_options(clusters.maxWorkersPerHost = 1)
expect_identical(suppressMessages(requestedWorkers(rep("hostA", 40), 25)), 25L)
## an unnamed cap on a number of cores
withr::local_options(clusters.maxWorkersPerHost = 8)
expect_identical(suppressMessages(requestedWorkers(20L, 25)), 8L)
})

test_that("clusterSetup() builds the workers requestedWorkers() says (the planner's total)", {
withr::local_options(clusters.maxWorkersPerHost = c(hostA = 10))
seen <- NULL
testthat::local_mocked_bindings(
plan_psock_min = function(hosts, total, ...) { seen <<- list(hosts = hosts, total = total); stop("planned") },
.package = "clusters")
for (i in 1:3) {
cores <- list(rep("hostA", 40), 7L, c("hostA", "hostB"))[[i]]
seen <- NULL
suppressMessages(try(clusterSetup(cores = cores, nCoresNeeded = 25, logPath = tempdir(), libPath = .libPaths()[1],
objsNeeded = character(), pkgsNeeded = character()), silent = TRUE))
expect_identical(as.integer(seen$total), c(10L, 7L, 25L)[i])
expect_identical(suppressMessages(requestedWorkers(cores, 25)), c(10L, 7L, 25L)[i])
}
})

test_that("clusterSetup() applies the cap once: hosts renamed to localhost are not cut again", {
## 5 on this machine's ssh name + 5 "localhost", cap 6 on localhost: each is under the cap before the rename,
## so 10 are asked for, as before requestedWorkers(); cutting again after the rename would give 6
withr::local_options(clusters.maxWorkersPerHost = c(localhost = 6))
seen <- NULL
testthat::local_mocked_bindings(
changeNodenameToLocalhost = function(cores) sub("^thisHost$", "localhost", cores),
plan_psock_min = function(hosts, total, ...) { seen <<- list(hosts = hosts, total = total); stop("planned") },
.package = "clusters")
suppressMessages(try(clusterSetup(cores = c(rep("thisHost", 5), rep("localhost", 5)), nCoresNeeded = 25,
logPath = tempdir(), libPath = .libPaths()[1],
objsNeeded = character(), pkgsNeeded = character()), silent = TRUE))
expect_identical(as.integer(seen$total), 10L)
})
Loading