Skip to content

Commit

Permalink
Handle stan_threads cpp option consistent with cmdstan
Browse files Browse the repository at this point in the history
* mod$cpp_options() now shows options as false if unset
    (same as the `info` cli command). Previously, this
    was inconsistent: sometimes showing the user-provided arg
    and sometimes showing the output of `info`
* tests now match expected behavior of cmdstan
  • Loading branch information
katrinabrock committed Aug 21, 2024
1 parent 3740633 commit 3335166
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion R/model.R
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ compile <- function(quiet = TRUE,
} # End - if(!dry_run)

private$exe_file_ <- exe
private$cpp_options_ <- cpp_options
private$cpp_options_ <- model_compile_info(exe)
private$precompile_cpp_options_ <- NULL
private$precompile_stanc_options_ <- NULL
private$precompile_include_paths_ <- NULL
Expand Down
11 changes: 6 additions & 5 deletions tests/testthat/test-threads.R
Original file line number Diff line number Diff line change
Expand Up @@ -158,20 +158,21 @@ test_that("threading works with generate_quantities()", {
expect_equal(f_gq$metadata()$threads_per_chain, 4)
})

test_that("correct output when stan_threads not TRUE", {
mod <- cmdstan_model(stan_program, cpp_options = list(stan_threads = FALSE), force_recompile = TRUE)
test_that("correct output when stan_threads unset", {
mod <- cmdstan_model(stan_program, cpp_options = list(stan_threads = NULL), force_recompile = TRUE)
expect_output(
mod$sample(data = data_file_json),
"Running MCMC with 4 sequential chains",
fixed = TRUE
)
mod <- cmdstan_model(stan_program, cpp_options = list(stan_threads = "dummy string"), force_recompile = TRUE)
expect_output(
expect_error(
mod$sample(data = data_file_json),
"Running MCMC with 4 sequential chains",
"The model was compiled with 'cpp_options = list(stan_threads = TRUE)' but 'threads_per_chain' was not set!",
fixed = TRUE
)
mod <- cmdstan_model(stan_program, cpp_options = list(stan_threads = FALSE), force_recompile = TRUE)

mod <- cmdstan_model(stan_program, cpp_options = list(stan_threads = NULL), force_recompile = TRUE)
expect_warning(
mod$sample(data = data_file_json, threads_per_chain = 4),
"'threads_per_chain' is set but the model was not compiled with 'cpp_options = list(stan_threads = TRUE)' so 'threads_per_chain' will have no effect!",
Expand Down

0 comments on commit 3335166

Please sign in to comment.