From 3335166b6f36c38691704b2e2d804d7567b7f9fa Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 21 Aug 2024 10:45:58 +0200 Subject: [PATCH] Handle stan_threads cpp option consistent with cmdstan * 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 --- R/model.R | 2 +- tests/testthat/test-threads.R | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/R/model.R b/R/model.R index c7391b33..04fc000e 100644 --- a/R/model.R +++ b/R/model.R @@ -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 diff --git a/tests/testthat/test-threads.R b/tests/testthat/test-threads.R index 7bf8338f..e3104fe5 100644 --- a/tests/testthat/test-threads.R +++ b/tests/testthat/test-threads.R @@ -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!",