Skip to content

Commit 827385f

Browse files
authored
Fix linking error when exposing SUNDIALS/KINSOL functions or model methods (#977)
* Fix linking error with SUNDIALS * Add test * Add handling for Linux systems needing -fPIC * Update test-model-expose-functions.R
1 parent 12fe0f8 commit 827385f

4 files changed

Lines changed: 40 additions & 5 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
inst/doc
88
dev-helpers.R
99
release-prep.R
10+
*.DS_Store

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: cmdstanr
22
Title: R Interface to 'CmdStan'
3-
Version: 0.8.0
3+
Version: 0.8.0.9000
44
Date: 2024-05-18
55
Authors@R:
66
c(person(given = "Jonah", family = "Gabry", role = "aut",

R/utils.R

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -727,18 +727,38 @@ get_cmdstan_flags <- function(flag_name) {
727727
paste(flags, collapse = " ")
728728
}
729729

730+
check_sundials_fpic <- function(verbose) {
731+
if (!os_is_linux()){
732+
return(invisible(NULL))
733+
}
734+
sundials_flags <- get_cmdstan_flags("CPPFLAGS_SUNDIALS")
735+
local_flags <- cmdstan_make_local()
736+
if (any(grepl("-fPIC", c(sundials_flags, local_flags), fixed = TRUE))) {
737+
return(invisible(NULL))
738+
}
739+
if (interactive()) {
740+
message("SUNDIALS needs to be compiled with -fPIC when exposing functions or ",
741+
"model methods on Linux.\n",
742+
"Updating your make/local file to include -fPIC and rebuilding CmdStan now...")
743+
}
744+
cmdstan_make_local(cpp_options = list("CPPFLAGS_SUNDIALS += -fPIC"), append = TRUE)
745+
rebuild_cmdstan(quiet = !verbose)
746+
if (interactive()) {
747+
message("CmdStan has been rebuilt, continuing with model compilation...")
748+
}
749+
}
750+
730751
rcpp_source_stan <- function(code, env, verbose = FALSE, ...) {
752+
check_sundials_fpic(verbose)
731753
cxxflags <- get_cmdstan_flags("CXXFLAGS")
732754
cmdstanr_includes <- system.file("include", package = "cmdstanr", mustWork = TRUE)
733755
cmdstanr_includes <- paste0(" -I\"", cmdstanr_includes,"\"")
734-
libs <- c("LDLIBS", "LIBSUNDIALS", "TBB_TARGETS", "LDFLAGS_TBB")
756+
libs <- c("LDLIBS", "LIBSUNDIALS", "TBB_TARGETS", "LDFLAGS_TBB", "SUNDIALS_TARGETS")
735757
libs <- paste(sapply(libs, get_cmdstan_flags), collapse = " ")
736758
if (.Platform$OS.type == "windows") {
737759
libs <- paste(libs, "-fopenmp")
738760
}
739-
lib_paths <- c("/stan/lib/stan_math/lib/tbb/",
740-
"/stan/lib/stan_math/lib/sundials_6.1.1/lib/")
741-
withr::with_path(paste0(cmdstan_path(), lib_paths),
761+
withr::with_path(repair_path(file.path(cmdstan_path(),"stan/lib/stan_math/lib/tbb")),
742762
withr::with_makevars(
743763
c(
744764
USE_CXX14 = 1,

tests/testthat/test-model-expose-functions.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,3 +419,17 @@ test_that("Exposing functions with precompiled model gives meaningful error", {
419419
fixed = TRUE
420420
)
421421
})
422+
423+
test_that("Functions with SUNDIALS/KINSOL methods link correctly", {
424+
modcode <- "
425+
functions {
426+
vector dummy_functor(vector guess, vector theta, data array[] real tails, data array[] int x_i) {
427+
return [1, 1]';
428+
}
429+
vector call_solver(vector guess, vector theta, data array[] real tails, data array[] int x_i) {
430+
return algebra_solver_newton(dummy_functor, guess, theta, tails, x_i);
431+
}
432+
}"
433+
mod <- cmdstan_model(write_stan_file(modcode), force_recompile=TRUE)
434+
expect_no_error(mod$expose_functions())
435+
})

0 commit comments

Comments
 (0)