Skip to content

Fix linking error when exposing SUNDIALS/KINSOL functions or model methods #977

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 22, 2024
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
inst/doc
dev-helpers.R
release-prep.R
*.DS_Store
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: cmdstanr
Title: R Interface to 'CmdStan'
Version: 0.8.0
Version: 0.8.0.9000
Date: 2024-05-18
Authors@R:
c(person(given = "Jonah", family = "Gabry", role = "aut",
Expand Down
28 changes: 24 additions & 4 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -727,18 +727,38 @@ get_cmdstan_flags <- function(flag_name) {
paste(flags, collapse = " ")
}

check_sundials_fpic <- function(verbose) {
if (!os_is_linux()){
return(invisible(NULL))
}
sundials_flags <- get_cmdstan_flags("CPPFLAGS_SUNDIALS")
local_flags <- cmdstan_make_local()
if (any(grepl("-fPIC", c(sundials_flags, local_flags), fixed = TRUE))) {
return(invisible(NULL))
}
if (interactive()) {
message("SUNDIALS needs to be compiled with -fPIC when exposing functions or ",
"model methods on Linux.\n",
"Updating your make/local file to include -fPIC and rebuilding CmdStan now...")
}
cmdstan_make_local(cpp_options = list("CPPFLAGS_SUNDIALS += -fPIC"), append = TRUE)
rebuild_cmdstan(quiet = !verbose)
if (interactive()) {
message("CmdStan has been rebuilt, continuing with model compilation...")
}
}

rcpp_source_stan <- function(code, env, verbose = FALSE, ...) {
check_sundials_fpic(verbose)
cxxflags <- get_cmdstan_flags("CXXFLAGS")
cmdstanr_includes <- system.file("include", package = "cmdstanr", mustWork = TRUE)
cmdstanr_includes <- paste0(" -I\"", cmdstanr_includes,"\"")
libs <- c("LDLIBS", "LIBSUNDIALS", "TBB_TARGETS", "LDFLAGS_TBB")
libs <- c("LDLIBS", "LIBSUNDIALS", "TBB_TARGETS", "LDFLAGS_TBB", "SUNDIALS_TARGETS")
libs <- paste(sapply(libs, get_cmdstan_flags), collapse = " ")
if (.Platform$OS.type == "windows") {
libs <- paste(libs, "-fopenmp")
}
lib_paths <- c("/stan/lib/stan_math/lib/tbb/",
"/stan/lib/stan_math/lib/sundials_6.1.1/lib/")
withr::with_path(paste0(cmdstan_path(), lib_paths),
withr::with_path(repair_path(file.path(cmdstan_path(),"stan/lib/stan_math/lib/tbb")),
withr::with_makevars(
c(
USE_CXX14 = 1,
Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/test-model-expose-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -419,3 +419,17 @@ test_that("Exposing functions with precompiled model gives meaningful error", {
fixed = TRUE
)
})

test_that("Functions with SUNDIALS/KINSOL methods link correctly", {
modcode <- "
functions {
vector dummy_functor(vector guess, vector theta, data array[] real tails, data array[] int x_i) {
return [1, 1]';
}
vector call_solver(vector guess, vector theta, data array[] real tails, data array[] int x_i) {
return algebra_solver_newton(dummy_functor, guess, theta, tails, x_i);
}
}"
mod <- cmdstan_model(write_stan_file(modcode), force_recompile=TRUE)
expect_no_error(mod$expose_functions())
})
Loading