From 5db1da2320f270ddbabb1872755fa9b9ca5af348 Mon Sep 17 00:00:00 2001 From: Eliot McIntire Date: Fri, 4 Dec 2026 09:24:42 -0700 Subject: [PATCH] Pass code arguments to the DEoptim objective unevaluated .DEoptimChunk() and .rescoreFn() built the objective's call with do.call() and no quote = TRUE, so a language argument (mpbRedTopSpread's quotedSpread) was run as code in the main process. Co-Authored-By: Claude Opus 5.5 --- DESCRIPTION | 2 +- NEWS.md | 2 ++ R/DEoptim.R | 6 +++-- tests/testthat/test-DEoptimLanguageArgs.R | 27 +++++++++++++++++++++++ 4 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 tests/testthat/test-DEoptimLanguageArgs.R diff --git a/DESCRIPTION b/DESCRIPTION index 5ffce07..bcab523 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -6,7 +6,7 @@ URL: http://clusters.predictiveecology.org, https://github.com/PredictiveEcology/clusters Date: 2026-10-16 -Version: 0.0.95 +Version: 0.0.96 Authors@R: c( person("Eliot J B", "McIntire", email = "eliot.mcintire@nrcan-rncan.gc.ca", role = c("aut", "cre"), comment=c(ORCID = "0000-0002-6914-8316")), diff --git a/NEWS.md b/NEWS.md index 6058b39..8b76edd 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # clusters (development version) +* An objective's argument that is code, such as mpbRedTopSpread's `quotedSpread` (a `quote({...})`), now reaches the objective as is. `.DEoptimChunk()` and `.rescoreFn()` passed it through `do.call()` without `quote = TRUE`, so it was run in the main process before the objective was called (MPB 2026-12-04: "object 'massAttackRasStart' not found"). + * 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. diff --git a/R/DEoptim.R b/R/DEoptim.R index 0d16025..37f6803 100644 --- a/R/DEoptim.R +++ b/R/DEoptim.R @@ -64,8 +64,10 @@ if (!is.null(seed)) assign(".Random.seed", seed, envir = globalenv()) invisible(.deoptimRecordTakeAll(cl)) # nothing left over from an interrupted chunk if (!is.null(cl)) control$cluster <- cl + ## quote = TRUE: an argument that is code (e.g. a quote({...})) reaches fn as is, not run here out <- do.call(DEoptim::DEoptim, - c(list(fn = memoFn, lower = lower, upper = upper, control = control), dotsList)) + c(list(fn = memoFn, lower = lower, upper = upper, control = control), dotsList), + quote = TRUE) list(out = out, recs = .deoptimRecordTakeAll(cl)) } ran <- .runWithRebuild(control$cluster, attempt) @@ -185,7 +187,7 @@ ## .memoObjFun is, because it is sent to the workers. .rescoreFn <- function(fn, args, parNames) { force(fn); force(args); force(parNames) - function(par) do.call(fn, c(list(stats::setNames(par, parNames)), args)) + function(par) do.call(fn, c(list(stats::setNames(par, parNames)), args), quote = TRUE) } .rescorePopulation <- function(fn, pop, args, parNames, cl) { ## apply() and parApply() on a matrix with no rows call `f` ONCE, in this process, on a vector of diff --git a/tests/testthat/test-DEoptimLanguageArgs.R b/tests/testthat/test-DEoptimLanguageArgs.R new file mode 100644 index 0000000..40738d7 --- /dev/null +++ b/tests/testthat/test-DEoptimLanguageArgs.R @@ -0,0 +1,27 @@ +## An objective's arguments can be code, e.g. mpbRedTopSpread's `quotedSpread`, a quote({...}) that +## the objective evaluates in its own frame. The chunk and the re-scoring must pass it as is, not run it. +## +## MPB, 2026-12-04: .DEoptimChunk's do.call(DEoptim, c(..., dotsList)) put the quoted spread into the +## call as code, so DEoptim ran it in the main process: "object 'massAttackRasStart' not found". + +skip_if_not_installed("DEoptim") + +objFn <- function(par, quotedSpread) { + stopifnot(is.language(quotedSpread)) + sum((par - 0.3)^2) +} +code <- quote({ stop("the quoted argument was evaluated") }) + +test_that(".DEoptimChunk() passes a language argument to the objective unevaluated", { + out <- clusters:::.DEoptimChunk( + objFn, lower = c(a = 0, b = 0), upper = c(a = 1, b = 1), + control = DEoptim::DEoptim.control(NP = 20L, itermax = 2L, trace = FALSE), + known = list(keys = character(0), vals = numeric(0)), + dotsList = list(quotedSpread = code)) + expect_true(is.finite(out$optim$bestval)) +}) + +test_that(".rescoreFn() passes a language argument to the objective unevaluated", { + f <- clusters:::.rescoreFn(objFn, args = list(quotedSpread = code), parNames = c("a", "b")) + expect_equal(f(c(0.3, 0.3)), 0) +})