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: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: cppDE
Title: 'C++' Code Generation for ODEs with Sensitivity Propagation
Version: 0.13.16
Version: 0.13.17
Authors@R: c(
person("Simon", "Beyer", , "simon.beyer@fdm.uni-freiburg.de", role = c("aut", "cre")),
person("Lawrence Livermore National Security", role = "cph",
Expand Down
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ S3method(diagnostics,default)
S3method(print,cppDEadjoint)
export(adjointControl)
export(batchAvailable)
export(blasThreads)
export(clearNativeSymbols)
export(codegenAvailable)
export(compile)
Expand All @@ -13,7 +14,6 @@ export(cvode)
export(diagnostics)
export(evaluateMany)
export(forcingValues)
export(forkGuard)
export(installLibs)
export(prepareBatch)
export(ptc)
Expand Down
14 changes: 14 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# cppDE 0.13.17

* A batch with more than one thread sets the BLAS thread count to one before the team starts and
restores it after the join, in `solveBatch()`, `solveODEBatch()` and the `evaluateBatch` entry of
`cppFUN()`. Models compiled with an earlier cppDE keep the previous behavior until they are
compiled again.
* A single solve runs BLAS with the thread count of the caller, on one thread where the BLAS loads
an OpenMP runtime other than the libgomp of the models.
* `configure` finds SUNDIALS and KLU where `TMPDIR` names a directory that does not exist.
* `blasThreads()` reads and sets the BLAS thread count. `forkGuard()`, the `fork()` handler installed
when the DLL loads and the startup message are removed.
* `?solveODE` and the methods articles write the derivatives of a reparameterization in Leibniz
notation.

# cppDE 0.13.16

* A product with a constant and a quotient by a constant have finite first and second derivatives
Expand Down
29 changes: 0 additions & 29 deletions R/blas.R

This file was deleted.

29 changes: 29 additions & 0 deletions R/blasThreads.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#' BLAS Thread Count
#'
#' @description
#' Reads or sets the thread count of the BLAS that R uses, through the runtime
#' entry point of the BLAS (MKL, OpenBLAS, FlexiBLAS or BLIS).
#'
#' @details
#' A batch of [solveBatch()], [solveODEBatch()] or the `evaluateBatch` entry of
#' a [cppFUN()] object with more than one thread runs BLAS on one thread and
#' restores the previous count after the batch. Outside a batch BLAS runs with
#' the count set here or by the BLAS itself, on one thread where the BLAS loads
#' an OpenMP runtime other than the libgomp of the models.
#'
#' @param n Thread count to set, or `NULL` to only read it.
#' @return The thread count before the call, `NA` when the BLAS offers no
#' getter, invisibly when `n` is given. Attribute `api`: the BLAS whose count
#' is read and set, `NA` when no entry point resolved.
#' @seealso [batchAvailable()]
#' @example inst/examples/blasThreads.R
#' @export
blasThreads <- function(n = NULL) {
if (!is.null(n)) {
n <- as.integer(n)
if (length(n) != 1L || is.na(n) || n < 1L)
stop("blasThreads: 'n' must be a single positive integer.", call. = FALSE)
return(invisible(.Call(C_cppde_blas_threads, n)))
}
.Call(C_cppde_blas_threads, NULL)
}
3 changes: 1 addition & 2 deletions R/install_libs.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@
#' SUNDIALS is built against R's BLAS and LAPACK; there is no option to choose
#' another. When R reports a BLAS, the build enables `SUNLinSol_LapackDense`,
#' which [cvode()] then uses for dense Jacobians; otherwise `cvode()` uses
#' `SUNLinSol_Dense`. During a solve and across `fork()` cppDE runs BLAS on one
#' thread, see [forkGuard()].
#' `SUNLinSol_Dense`.
#'
#' @param which Which libraries to build. Default `"sundials"`, which
#' installs SUNDIALS and SuiteSparse; `"suitesparse"` installs
Expand Down
8 changes: 4 additions & 4 deletions R/solveODE.R
Original file line number Diff line number Diff line change
Expand Up @@ -578,8 +578,8 @@
#'
#' ## Shapes of `tangent` and `hessian`
#'
#' `tangent` is the Jacobian \eqn{\Phi'(\theta)} and `hessian` the second
#' derivative \eqn{\Phi''(\theta)} of a reparameterization
#' `tangent` is the Jacobian \eqn{\partial\Phi/\partial\theta} and `hessian` the second
#' derivative \eqn{\partial^2\Phi/\partial\theta^2} of a reparameterization
#' \eqn{p = \Phi(\theta)} of the initial states and parameters; the results are
#' then derivatives in \eqn{\theta}. `tangent` takes one of three shapes, told
#' apart by the row count and row names. `hessian` has the rows of `tangent` and
Expand All @@ -589,7 +589,7 @@
#' parameter rows are the identity on the active sensitivities, the model's
#' sensitivity names minus `fixed`. `result$tangent[t, , ]` has this shape and
#' can seed a following solve.
#' - Full, `[n_x + n_p, n_s]`, states first: \eqn{\Phi'(\theta)} itself. `n_s`
#' - Full, `[n_x + n_p, n_s]`, states first: \eqn{\partial\Phi/\partial\theta} itself. `n_s`
#' may change from call to call.
#' - Partial, `[k, n_s]` with `k < n_x + n_p`. The row names, a subset of
#' `c(variables, parameters)`, place the rows; missing rows are zero, so
Expand Down Expand Up @@ -1160,7 +1160,7 @@ solveBatch <- function(handle, parms = NULL, tangent = NULL, hessian = NULL,

# Thread count for a batch of K conditions: the cores asked for even when K is smaller, so that
# OpenMP keeps the thread team (batch_threads() in cppde_r_batch.hpp), one for a single condition. Not read
# from OMP_NUM_THREADS, which nothing keeps in step with the BLAS thread count cppDE pins.
# from OMP_NUM_THREADS, which sets the threads of a BLAS that runs on OpenMP.
.batchCores <- function(cores, K) {
if (!is.null(cores)) {
cores <- as.integer(cores)
Expand Down
29 changes: 0 additions & 29 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,35 +70,6 @@ cvodeConfig <- new.env(parent = emptyenv())
}
}

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

# One-line report of the BLAS fork guard, or a warning when no entry point
# resolved. Silent when there is nothing to pin. Called from .onAttach, not
# .onLoad, since tooling loads namespaces without attaching them.
.announceForkGuard <- function() {
if (isTRUE(getOption("cppDE.quiet"))) return(invisible(NULL))
g <- tryCatch(forkGuard(), error = function(e) NULL)
if (is.null(g) || !isTRUE(g$guard)) return(invisible(NULL))

if (is.na(g$api)) {
packageStartupMessage(
"cppDE: no BLAS thread-control entry point found. If this BLAS is ",
"threaded, a forked worker can deadlock; start R with OMP_NUM_THREADS=1. ",
"See ?forkGuard.")
} else if (!is.na(g$threads) && g$threads > 1L) {
packageStartupMessage(sprintf(
"cppDE: %s runs %d threads; each fork() pins it to 1 and restores it afterwards (?forkGuard).",
g$api, g$threads))
}
invisible(NULL)
}

#' Lazy import of internal Python modules
#'
#' @keywords internal
Expand Down
2 changes: 1 addition & 1 deletion _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,6 @@ reference:
contents:
- codegenAvailable
- batchAvailable
- forkGuard
- blasThreads
- installLibs
- clearNativeSymbols
4 changes: 2 additions & 2 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ probe_compile() {
# final diagnostic: the install temp dir doesn't survive a
# successful R CMD INSTALL, so we can't rely on a config.log file.
snippet="$1"; extra_cflags="$2"; extra_libs="$3"
tmpdir=$(mktemp -d 2>/dev/null || mktemp -d -t conftest)
tmpdir=$(mktemp -d 2>/dev/null || mktemp -d /tmp/cppde_conftest.XXXXXX)
printf '%s\n' "$snippet" > "$tmpdir/conftest.cpp"
PROBE_LAST_CMD="$CXX $CPPFLAGS $CXXFLAGS $extra_cflags -o conftest conftest.cpp $extra_libs"
PROBE_LAST_ERR=$($CXX $CPPFLAGS $CXXFLAGS $extra_cflags -o "$tmpdir/conftest" \
Expand Down Expand Up @@ -588,7 +588,7 @@ fi
# fails is reported but leaves the backend enabled.
# ---------------------------------------------------------------------
if [ "$sundials_available" = "1" ] && [ "${CPPDE_SKIP_RUN_CHECK:-0}" != "1" ]; then
rc_dir=$(mktemp -d 2>/dev/null || mktemp -d -t conftest)
rc_dir=$(mktemp -d 2>/dev/null || mktemp -d /tmp/cppde_conftest.XXXXXX)
printf '%s\n' "$snippet" > "$rc_dir/conftest.cpp"
if $CXX $CPPFLAGS $CXXFLAGS $sundials_cflags -o "$rc_dir/conftest" \
"$rc_dir/conftest.cpp" $sundials_libs >/dev/null 2>&1; then
Expand Down
2 changes: 1 addition & 1 deletion configure.win
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ probe_compile() {
# caller can surface failures via append_probe_err / dump_probe_errs.
[ -z "$CXX" ] && { PROBE_LAST_ERR="(no C++ compiler: R CMD config CXX returned empty)"; return 1; }
snippet="$1"; extra_cflags="$2"; extra_libs="$3"
tmpdir=$(mktemp -d 2>/dev/null || mktemp -d -t conftest)
tmpdir=$(mktemp -d 2>/dev/null || mktemp -d /tmp/cppde_conftest.XXXXXX)
printf '%s\n' "$snippet" > "$tmpdir/conftest.cpp"
PROBE_LAST_CMD="$CXX $CPPFLAGS $CXXFLAGS $extra_cflags -o conftest.exe conftest.cpp $extra_libs"
PROBE_LAST_ERR=$($CXX $CPPFLAGS $CXXFLAGS $extra_cflags -o "$tmpdir/conftest.exe" \
Expand Down
5 changes: 5 additions & 0 deletions inst/examples/blasThreads.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
n <- blasThreads()
n
old <- blasThreads(1)
blasThreads()
if (!is.na(old)) blasThreads(old)
1 change: 0 additions & 1 deletion inst/examples/forkGuard.R

This file was deleted.

Loading
Loading