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
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: dMod2
Type: Package
Title: Parameter Estimation in Dynamical Models
Version: 0.18.0
Version: 0.18.1
Date: 2026-11-25
Authors@R: c(
person("Simon", "Beyer", email = "simon.beyer@fdm.uni-freiburg.de",
Expand All @@ -27,7 +27,7 @@ Imports:
cOde,
methods,
tools,
cppDE (>= 0.13.16),
cppDE (>= 0.13.17),
ggplot2 (>= 3.5.0),
parallel,
parallelly,
Expand Down Expand Up @@ -73,6 +73,7 @@ SystemRequirements: C++17 compiler; Python (>= 3.10) with SymPy, NumPy and
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
LinkingTo:
cppDE,
Matrix,
Rcpp
Config/roxygen2/version: 8.1.1
Expand Down
12 changes: 12 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# dMod2 0.18.1

* Needs cppDE >= 0.13.17 and compiles against the cppDE headers (`LinkingTo: cppDE`). The OpenMP regions
of the native chain, of `Pimpl()`, of `normL2()` and of the alignment kernels set the BLAS
thread count to one before the team starts and restore it after the join.
* The forked workers of `mstrust()`, `profile()` and `msem()` run BLAS on one thread; PSOCK workers
set it when they start. The BLAS note on attach is removed.
* The tasks of `distributedComputing()` run with `MKL_CBWR=COMPATIBLE` unless the host sets
`MKL_CBWR`.
* `?residuals.em` writes the transpose as `G^T`, the article on sparsity priors the second derivative
in Leibniz notation.

# dMod2 0.18.0

* `symmetryDetection()` and `symmetryReduction()` need symident >= 0.18.2: under `equilibrate`
Expand Down
2 changes: 1 addition & 1 deletion R/emResults.R
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ predict.em <- function(object, times = NULL, type = c("all", "individual", "popu
#' error model), `"IRES"` (data minus individual prediction), `"CWRES"`
#' (conditional weighted residuals of the FOCE linearization at the modes:
#' per group the residual of the group data against \eqn{f(\eta^*) - G \eta^*},
#' whitened by \eqn{G \Omega G' + \Sigma}, \eqn{G} the sensitivities in the
#' whitened by \eqn{G \Omega G^\top + \Sigma}, \eqn{G} the sensitivities in the
#' normal random effects; without those equal to IWRES), `"PRES"` or
#' `"PWRES"` (against the population prediction), `"NPDE"` (normalized
#' prediction distribution errors from `nsim` simulated data sets,
Expand Down
31 changes: 23 additions & 8 deletions R/remoteComputing.R
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,9 @@ distributedComputing <- function(expr, tasks = NULL, on = localNode(), jobname =

unpack <- paste0("if [ -f workspace.RData.zst ]; then zstd -dqf workspace.RData.zst && ",
"rm -f workspace.RData.zst; fi && ")
setup <- paste0(.remoteLibs(libs), "export OMP_NUM_THREADS=1 MKL_NUM_THREADS=1; ")
# MKL on the compatible code path unless the host sets MKL_CBWR
setup <- paste0(.remoteLibs(libs), "export OMP_NUM_THREADS=1 MKL_NUM_THREADS=1 ",
"MKL_CBWR=\"${MKL_CBWR:-COMPATIBLE}\"; ")

if (slurm) {
filled <- .fillJobscript(jobscript, list(
Expand Down Expand Up @@ -1435,9 +1437,20 @@ slurmTemplate <- function(login = NULL, from = login, edit = interactive(),
}


# BLAS on one thread while `expr` runs, the count restored after; a BLAS without a getter is left
# as it is.
.withSerialBlas <- function(expr) {
n <- cppDE::blasThreads()
if (is.na(n) || n <= 1L) return(expr)
cppDE::blasThreads(1L)
on.exit(cppDE::blasThreads(n), add = TRUE)
expr
}

# Cross-platform parallel-apply. Unix forks via doParallel; Windows uses a
# PSOCK cluster with the library paths of the session. Driven through
# foreach::%dopar% so the caller body is the same on both platforms.
# foreach::%dopar% so the caller body is the same on both platforms. The workers run BLAS on one
# thread: forks inherit the setting of the parent, PSOCK workers set it at start.
.parallelLapply <- function(X, FUN, cores = 1L, coresConditions = NULL) {

cores <- max(1L, as.integer(cores))
Expand All @@ -1461,7 +1474,10 @@ slurmTemplate <- function(login = NULL, from = login, edit = interactive(),
}, add = TRUE)
doParallel::registerDoParallel(cl = cluster)
parallel::clusterCall(cl = cluster, function(x) .libPaths(x), .libPaths())
parallel::clusterEvalQ(cluster, suppressMessages(library(dMod2)))
parallel::clusterEvalQ(cluster, {
suppressMessages(library(dMod2))
cppDE::blasThreads(1L)
})
} else {
doParallel::registerDoParallel(cores = cores)
on.exit(doParallel::stopImplicitCluster(), add = TRUE)
Expand All @@ -1470,13 +1486,12 @@ slurmTemplate <- function(login = NULL, from = login, edit = interactive(),
i <- NULL # silence R CMD check NSE warning
loaded_packages <- .packages()
kcond <- if (is.null(coresConditions)) NULL else as.integer(coresConditions)
out <- foreach::foreach(i = seq_along(X),
.packages = loaded_packages,
.options.multicore = list(preschedule = FALSE)) %dopar% {
.withSerialBlas(foreach::foreach(i = seq_along(X),
.packages = loaded_packages,
.options.multicore = list(preschedule = FALSE)) %dopar% {
if (!is.null(kcond)) options(dMod2.cores = kcond)
FUN(X[[i]])
}
out
})
}


Expand Down
28 changes: 0 additions & 28 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,6 @@
else setHook(packageEvent("reticulate", "onLoad"), declare)
}

#' Package attach
#'
#' @keywords internal
#' @noRd
.onAttach <- function(libname, pkgname) {
.announceForkGuard()
}

# mstrust() and profile() fork, and a threaded BLAS deadlocks in the child. cppDE
# pins it for the width of each fork, which makes BLAS inside a worker serial, so
# say so here, and say when there is no lever and the hang is still reachable.
.announceForkGuard <- function() {
g <- tryCatch(cppDE::forkGuard(), error = function(e) NULL)
if (is.null(g) || !isTRUE(g$guard)) return(invisible(NULL))

if (is.na(g$api)) {
packageStartupMessage(
"dMod2: no BLAS thread-control entry point found. If this BLAS is threaded, ",
"mstrust() and profile() with cores > 1 can deadlock; start R with ",
"OMP_NUM_THREADS=1. See cppDE::forkGuard().")
} else if (!is.na(g$threads) && g$threads > 1L) {
packageStartupMessage(sprintf(
"dMod2: %s (%d threads) is pinned to 1 inside the forked workers of mstrust() and profile(). See cppDE::forkGuard().",
g$api, g$threads))
}
invisible(NULL)
}

# Guard for optionally-Suggested feature dependencies: fail with an actionable
# message if the package a feature needs is not installed. Lets heavy/optional
# deps (Python bridge, SBML/PEtab I/O, LP solver) live in Suggests, not Imports.
Expand Down
2 changes: 1 addition & 1 deletion man/residuals.em.Rd

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

4 changes: 4 additions & 0 deletions src/alignment_kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <Rcpp.h>
#include <vector>
#include <algorithm>
#include <cppde/cppde_blas_threads.hpp>

#if !defined(_WIN32)
#include <pthread.h>
Expand Down Expand Up @@ -113,6 +114,7 @@ NumericMatrix alignmentPredictors(List conds, List theta, int S, int threads = 1
th[c] = tc.size() ? &tc[0] : nullptr;
}
const int nt = nThreads(threads);
cppde::detail::serial_blas_scope blas(nt > 1);
#ifdef _OPENMP
#pragma omp parallel for num_threads(nt) schedule(dynamic, 16)
#endif
Expand Down Expand Up @@ -168,6 +170,7 @@ List alignmentForwardBlocks(NumericVector tangent, Nullable<NumericVector> hessi
}

const int nt = nThreads(threads);
cppde::detail::serial_blas_scope blas(nt > 1);
#ifdef _OPENMP
#pragma omp parallel for num_threads(nt) schedule(dynamic, 16)
#endif
Expand Down Expand Up @@ -249,6 +252,7 @@ List alignmentVjpBlocks(NumericVector tangent, Nullable<NumericVector> hessian,
for (int s = 0; s < nb; ++s) for (int d = 0; d < D; ++d) re[d + (std::size_t) D * s] = rest(d, s) - 1;

const int nt = nThreads(threads);
cppde::detail::serial_blas_scope blas(nt > 1);
#ifdef _OPENMP
#pragma omp parallel for num_threads(nt) schedule(dynamic, 16)
#endif
Expand Down
7 changes: 7 additions & 0 deletions src/chain_kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "pimpl_kernel.h"
#include "residual_kernel.h"
#include <cppde/cppde_blas_threads.hpp>

#ifdef _OPENMP
#include <omp.h>
Expand Down Expand Up @@ -331,6 +332,7 @@ SEXP chain_params(List plan, List values, bool deriv, bool deriv2, int threads,
std::vector<char> ok(K, 1);
std::vector<std::string> warnings;
const int nt = chainThreads(threads, K);
cppde::detail::serial_blas_scope blas(nt > 1);
for (;;) {
for (int c = 0; c < K; ++c) {
need[c] = -1;
Expand Down Expand Up @@ -680,6 +682,7 @@ SEXP chain_data(List plan, List pre, List raw, bool deriv, bool deriv2, bool bui
const bool seeds_on = want_seed || want_rows;
std::vector<std::vector<double>> rowJ(K), rowU(K);
const int nt = chainThreads(threads, K);
cppde::detail::serial_blas_scope blas(nt > 1);
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic) num_threads(nt) if (nt > 1)
#endif
Expand Down Expand Up @@ -878,6 +881,7 @@ SEXP chain_frames(List plan, List pre, List raw, bool deriv2, int threads) {
std::vector<char> ok(K, 1);
std::vector<Ad> Y(K), E(K);
const int nt = chainThreads(threads, K);
cppde::detail::serial_blas_scope blas(nt > 1);
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic) num_threads(nt) if (nt > 1)
#endif
Expand Down Expand Up @@ -1011,6 +1015,7 @@ SEXP chain_rows(List plan, List pre, List raw, int threads) {
int *Ce = LOGICAL(cens), *Mo = LOGICAL(model);
std::vector<char> ok(C, 1);
const int nt = chainThreads(threads, C);
cppde::detail::serial_blas_scope blas(nt > 1);
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic) num_threads(nt) if (nt > 1)
#endif
Expand Down Expand Up @@ -1082,6 +1087,7 @@ SEXP chain_back(List plan, List pre, List raw, NumericMatrix seedP, NumericMatri
std::vector<std::vector<double>> cot(C), curv(C), cp(C), dcp(C);
std::vector<char> ok(C, 1);
const int nt = chainThreads(threads, C);
cppde::detail::serial_blas_scope blas(nt > 1);
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic) num_threads(nt) if (nt > 1)
#endif
Expand Down Expand Up @@ -1336,6 +1342,7 @@ SEXP chain_pull(List plan, List values, NumericMatrix tangents, SEXP roots, List
}
std::vector<char> ok(C, 1);
const int nt = chainThreads(threads, C);
cppde::detail::serial_blas_scope blas(nt > 1);
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic) num_threads(nt) if (nt > 1)
#endif
Expand Down
2 changes: 2 additions & 0 deletions src/normL2_kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// that is merged in a critical region.

#include "residual_kernel.h"
#include <cppde/cppde_blas_threads.hpp>

#include <Rcpp.h>
#include <vector>
Expand Down Expand Up @@ -420,6 +421,7 @@ List normL2_kernel(
// the requested team, at most the processors, also for fewer conditions, so that OpenMP keeps the
// threads
if (n_cond <= 1) n_threads = 1;
cppde::detail::serial_blas_scope blas(n_threads > 1);

dmod::AccumOpts base_opts;
base_opts.use_deriv2_exact = deriv2_requested;
Expand Down
4 changes: 4 additions & 0 deletions src/pimpl_kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define USE_FC_LEN_T
#include <Rcpp.h>
#include <R_ext/Lapack.h>
#include <cppde/cppde_blas_threads.hpp>

#include <algorithm>
#include <cmath>
Expand Down Expand Up @@ -734,6 +735,9 @@ bool roots(const std::vector<Request>& req, std::vector<Root>& out, int threads,
#ifdef _OPENMP
nt = std::min(nt, omp_get_num_procs());
if (omp_in_parallel()) nt = 1;
#endif
cppde::detail::serial_blas_scope blas(nt > 1);
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic) num_threads(nt) if (nt > 1)
#endif
for (int t = 0; t < T; ++t) {
Expand Down
Loading
Loading