From b5a92d0fab3dbfc2342d36947bb921bdacabaf0d Mon Sep 17 00:00:00 2001 From: Eliot McIntire Date: Wed, 18 Nov 2026 08:49:00 -0700 Subject: [PATCH 1/2] requestedWorkers(): the worker count clusterSetup() asks for, in one place Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014G18Mik3fH4z5M5CyMSUS6 --- NAMESPACE | 1 + NEWS.md | 2 + R/clusterSetup.R | 45 +++++++++++++++++++---- man/clusterSetup.Rd | 2 +- man/requestedWorkers.Rd | 35 ++++++++++++++++++ tests/testthat/test-requestedWorkers.R | 51 ++++++++++++++++++++++++++ 6 files changed, 128 insertions(+), 8 deletions(-) create mode 100644 man/requestedWorkers.Rd create mode 100644 tests/testthat/test-requestedWorkers.R diff --git a/NAMESPACE b/NAMESPACE index c9c5b37..fcbca38 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -25,6 +25,7 @@ export(ramUsageGB) export(readTransferredObjects) export(releaseCapacity) export(releaseCores) +export(requestedWorkers) export(reservationsPath) export(reserveCapacity) export(reserveCores) diff --git a/NEWS.md b/NEWS.md index 09a11ff..6058b39 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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. diff --git a/R/clusterSetup.R b/R/clusterSetup.R index a85b641..4a7e9b4 100644 --- a/R/clusterSetup.R +++ b/R/clusterSetup.R @@ -1,3 +1,38 @@ +#' 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)) + cores <- .requestedHosts(cores) + as.integer(if (anyDuplicated(cores)) length(cores) 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". Idempotent, so requestedWorkers() may be given its result. +.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 @@ -27,7 +62,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 @@ -64,10 +99,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 @@ -81,7 +112,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") @@ -99,7 +130,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 = requestedWorkers(cores, nCoresNeeded), pkgsNeeded = pkgsNeeded, libPath = libPath, logPath = logPath, diff --git a/man/clusterSetup.Rd b/man/clusterSetup.Rd index f19e7bb..ec49c17 100644 --- a/man/clusterSetup.Rd +++ b/man/clusterSetup.Rd @@ -47,7 +47,7 @@ cluster and no \code{NP}, \code{NP} is its number of rows.} \item{pkgsNeeded}{Character vector of packages the workers must be able to load. Hosts that cannot are reported by \code{\link[=verifyClusterHosts]{verifyClusterHosts()}}.} -\item{nCoresNeeded}{Integer; how many workers to aim for across all hosts.} +\item{nCoresNeeded}{Integer; how many workers to aim for across all hosts. See \code{\link[=requestedWorkers]{requestedWorkers()}} for the count asked for.} \item{envir}{Environment holding \code{objsNeeded}; defaults to the caller's.} diff --git a/man/requestedWorkers.Rd b/man/requestedWorkers.Rd new file mode 100644 index 0000000..b1e25f4 --- /dev/null +++ b/man/requestedWorkers.Rd @@ -0,0 +1,35 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/clusterSetup.R +\name{requestedWorkers} +\alias{requestedWorkers} +\title{How many workers \code{clusterSetup()} asks for} +\usage{ +requestedWorkers(cores, nCoresNeeded = 100) +} +\arguments{ +\item{cores}{As in \code{\link[=clusterSetup]{clusterSetup()}}: host names, or a number.} + +\item{nCoresNeeded}{As in \code{\link[=clusterSetup]{clusterSetup()}}.} +} +\value{ +An integer: the number of workers requested; \code{nCoresNeeded} (as an integer) when \code{cores} is +\code{NULL}, as no cluster is built then. +} +\description{ +The count \code{clusterSetup()} gives the planner (\code{\link[=plan_psock_min]{plan_psock_min()}}) as its \code{total}, which becomes the +population size \code{NP} when the cluster is built as asked. A caller that needs \code{NP} before the cluster +exists (a start population, an emulator) calls this with the \code{cores} and \code{nCoresNeeded} it will pass. +\code{clusterSetup()} calls it, so the rule is in one place. +} +\details{ +\code{cores} is first turned into one host name per worker: a single number is that many \code{"localhost"} +(\code{cores = 1L} is one host name), and each host is cut to \code{options(clusters.maxWorkersPerHost)} (with the +message \code{clusterSetup()} gives). Then, when a host is named more than once the count is the number of +names left, else it is \code{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. +} +\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 +} diff --git a/tests/testthat/test-requestedWorkers.R b/tests/testthat/test-requestedWorkers.R new file mode 100644 index 0000000..a6d899b --- /dev/null +++ b/tests/testthat/test-requestedWorkers.R @@ -0,0 +1,51 @@ +## 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]) + } +}) From c6f7f2f43904a375408ee2318881b36b93b2937b Mon Sep 17 00:00:00 2001 From: Eliot McIntire Date: Wed, 18 Nov 2026 09:15:27 -0700 Subject: [PATCH 2/2] clusterSetup(): apply the host cap once (.workerTotal on the hosts it already trimmed and renamed) requestedWorkers() on clusterSetup()'s own .requestedHosts() result cut again after the rename to localhost, so 5 thisHost + 5 localhost under a localhost cap of 6 asked for 6, not 10. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014G18Mik3fH4z5M5CyMSUS6 --- R/clusterSetup.R | 13 +++++++++---- tests/testthat/test-requestedWorkers.R | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/R/clusterSetup.R b/R/clusterSetup.R index 4a7e9b4..083627e 100644 --- a/R/clusterSetup.R +++ b/R/clusterSetup.R @@ -22,12 +22,17 @@ #' 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)) - cores <- .requestedHosts(cores) - as.integer(if (anyDuplicated(cores)) length(cores) else 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". Idempotent, so requestedWorkers() may be given its result. +## machine's ssh names become "localhost". .requestedHosts <- function(cores) { if (is.numeric(cores)) cores <- rep("localhost", cores) changeNodenameToLocalhost(.trimToHostCap(cores)) @@ -130,7 +135,7 @@ clusterSetup <- function(messagePrefix = "DEoptim_", # the project library -- and mirrored that to the hosts instead. plan <- plan_psock_min( hosts = coresUnique, - total = requestedWorkers(cores, nCoresNeeded), + total = .workerTotal(cores, nCoresNeeded), pkgsNeeded = pkgsNeeded, libPath = libPath, logPath = logPath, diff --git a/tests/testthat/test-requestedWorkers.R b/tests/testthat/test-requestedWorkers.R index a6d899b..bb71db3 100644 --- a/tests/testthat/test-requestedWorkers.R +++ b/tests/testthat/test-requestedWorkers.R @@ -49,3 +49,18 @@ test_that("clusterSetup() builds the workers requestedWorkers() says (the planne 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) +})