Skip to content

Commit c218b0d

Browse files
authored
Test and fix issues with 2.35 RC (#972)
* Try updating R actions to fix win failures * Format true/false to 1/0 in csv read * Add handling for new RNG type * Changes to RNG handling * Profile name ordering * Revert RC install in workflow prior to merge * Fix RNG compatibility with <2.35
1 parent 1f4efad commit c218b0d

File tree

13 files changed

+76
-32
lines changed

13 files changed

+76
-32
lines changed

.github/workflows/R-CMD-check-wsl.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ jobs:
3737

3838
- uses: actions/checkout@v4
3939

40-
- uses: r-lib/actions/setup-r@v2.8.7
41-
- uses: r-lib/actions/setup-pandoc@v2.8.7
40+
- uses: r-lib/actions/setup-r@v2.9.0
41+
- uses: r-lib/actions/setup-pandoc@v2.9.0
4242

43-
- uses: r-lib/actions/setup-r-dependencies@v2.8.7
43+
- uses: r-lib/actions/setup-r-dependencies@v2.9.0
4444
with:
4545
extra-packages: any::rcmdcheck, local::.
4646

@@ -71,7 +71,7 @@ jobs:
7171
sessioninfo::session_info(pkgs, include_base = TRUE)
7272
shell: Rscript {0}
7373

74-
- uses: r-lib/actions/check-r-package@v2.8.7
74+
- uses: r-lib/actions/check-r-package@v2.9.0
7575
env:
7676
_R_CHECK_CRAN_INCOMING_: false
7777

.github/workflows/R-CMD-check.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
config:
2626
- {os: macOS-latest, r: 'devel', rtools: ''}
2727
- {os: macOS-latest, r: 'release', rtools: ''}
28-
#- {os: windows-latest, r: 'devel', rtools: '44'}
28+
- {os: windows-latest, r: 'devel', rtools: '44'}
2929
- {os: windows-latest, r: 'release', rtools: '44'}
3030
- {os: windows-latest, r: 'oldrel', rtools: '43'}
3131
- {os: ubuntu-latest, r: 'devel', rtools: ''}
@@ -49,14 +49,14 @@ jobs:
4949

5050
- uses: actions/checkout@v4
5151

52-
- uses: r-lib/actions/setup-r@v2.8.7
52+
- uses: r-lib/actions/setup-r@v2.9.0
5353
with:
5454
r-version: ${{ matrix.config.r }}
5555
rtools-version: ${{ matrix.config.rtools }}
5656

57-
- uses: r-lib/actions/setup-pandoc@v2.8.7
57+
- uses: r-lib/actions/setup-pandoc@v2.9.0
5858

59-
- uses: r-lib/actions/setup-r-dependencies@v2.8.7
59+
- uses: r-lib/actions/setup-r-dependencies@v2.9.0
6060
with:
6161
extra-packages: any::rcmdcheck, local::.
6262

@@ -73,7 +73,7 @@ jobs:
7373
sessioninfo::session_info(pkgs, include_base = TRUE)
7474
shell: Rscript {0}
7575

76-
- uses: r-lib/actions/check-r-package@v2.8.7
76+
- uses: r-lib/actions/check-r-package@v2.9.0
7777
env:
7878
_R_CHECK_CRAN_INCOMING_: false
7979

.github/workflows/Test-coverage.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ jobs:
4242
if: "!startsWith(github.ref, 'refs/tags/') && github.ref != 'refs/heads/master'"
4343
- uses: actions/checkout@v4
4444

45-
- uses: r-lib/actions/setup-r@v2.8.7
46-
- uses: r-lib/actions/setup-pandoc@v2.8.7
45+
- uses: r-lib/actions/setup-r@v2.9.0
46+
- uses: r-lib/actions/setup-pandoc@v2.9.0
4747

48-
- uses: r-lib/actions/setup-r-dependencies@v2.8.7
48+
- uses: r-lib/actions/setup-r-dependencies@v2.9.0
4949
with:
5050
extra-packages: any::rcmdcheck, local::., any::covr, any::gridExtra
5151

.github/workflows/cmdstan-tarball-check.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ jobs:
4040
sudo apt-get install -y libcurl4-openssl-dev || true
4141
sudo apt-get install -y openmpi-bin openmpi-common libopenmpi-dev || true
4242
43-
- uses: r-lib/actions/setup-r@v2.8.7
43+
- uses: r-lib/actions/setup-r@v2.9.0
4444
with:
4545
r-version: ${{ matrix.config.r }}
4646
rtools-version: ${{ matrix.config.rtools }}
4747

48-
- uses: r-lib/actions/setup-pandoc@v2.8.7
48+
- uses: r-lib/actions/setup-pandoc@v2.9.0
4949

5050
- name: Query dependencies
5151
run: |

R/csv.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,14 @@ read_csv_metadata <- function(csv_file) {
840840
csv_file_info$fitted_params <- wsl_safe_path(csv_file_info$fitted_params,
841841
revert = TRUE)
842842
}
843+
csv_file_info <- lapply(csv_file_info, function(item) {
844+
if (is.character(item) && length(item) == 1) {
845+
if (item %in% c("true", "false")) {
846+
item <- as.integer(as.logical(item))
847+
}
848+
}
849+
item
850+
})
843851
csv_file_info
844852
}
845853

R/utils.R

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

730-
rcpp_source_stan <- function(code, env, verbose = FALSE) {
730+
rcpp_source_stan <- function(code, env, verbose = FALSE, ...) {
731731
cxxflags <- get_cmdstan_flags("CXXFLAGS")
732732
cmdstanr_includes <- system.file("include", package = "cmdstanr", mustWork = TRUE)
733733
cmdstanr_includes <- paste0(" -I\"", cmdstanr_includes,"\"")
@@ -746,7 +746,7 @@ rcpp_source_stan <- function(code, env, verbose = FALSE) {
746746
PKG_CXXFLAGS = paste0(cxxflags, cmdstanr_includes, collapse = " "),
747747
PKG_LIBS = libs
748748
),
749-
Rcpp::sourceCpp(code = code, env = env, verbose = verbose)
749+
Rcpp::sourceCpp(code = code, env = env, verbose = verbose, ...)
750750
)
751751
)
752752
invisible(NULL)
@@ -887,8 +887,12 @@ prep_fun_cpp <- function(fun_start, fun_end, model_lines) {
887887
}
888888
fun_body <- gsub("// [[stan::function]]", "// [[Rcpp::export]]\n", fun_body, fixed = TRUE)
889889
fun_body <- gsub("std::ostream\\*\\s*pstream__\\s*=\\s*nullptr", "", fun_body)
890-
fun_body <- gsub("boost::ecuyer1988&\\s*base_rng__", "SEXP base_rng_ptr", fun_body)
891-
fun_body <- gsub("base_rng__,", "*(Rcpp::XPtr<boost::ecuyer1988>(base_rng_ptr).get()),", fun_body, fixed = TRUE)
890+
if (cmdstan_version() < "2.35.0") {
891+
fun_body <- gsub("boost::ecuyer1988&\\s*base_rng__", "SEXP base_rng_ptr", fun_body)
892+
} else {
893+
fun_body <- gsub("stan::rng_t&\\s*base_rng__", "SEXP base_rng_ptr", fun_body)
894+
}
895+
fun_body <- gsub("base_rng__,", "*(Rcpp::XPtr<stan::rng_t>(base_rng_ptr).get()),", fun_body, fixed = TRUE)
892896
fun_body <- gsub("pstream__", "&Rcpp::Rcout", fun_body, fixed = TRUE)
893897
fun_body <- paste(fun_body, collapse = "\n")
894898
gsub(pattern = ",\\s*)", replacement = ")", fun_body)
@@ -921,6 +925,7 @@ compile_functions <- function(env, verbose = FALSE, global = FALSE) {
921925
env$hpp_code[1:(funs[1] - 1)],
922926
"#include <rcpp_tuple_interop.hpp>",
923927
"#include <rcpp_eigen_interop.hpp>",
928+
"#include <stan_rng.hpp>",
924929
stan_funs),
925930
collapse = "\n")
926931
if (global) {
@@ -935,7 +940,7 @@ compile_functions <- function(env, verbose = FALSE, global = FALSE) {
935940
if (length(rng_funs) > 0) {
936941
rng_cpp <- system.file("include", "base_rng.cpp", package = "cmdstanr", mustWork = TRUE)
937942
rcpp_source_stan(paste0(readLines(rng_cpp), collapse="\n"), env, verbose)
938-
env$rng_ptr <- env$base_rng(seed=0)
943+
env$rng_ptr <- env$base_rng(seed=1)
939944
}
940945

941946
# For all RNG functions, pass the initialised Boost RNG by default

inst/include/base_rng.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#include <Rcpp.h>
2-
#include <boost/random/additive_combine.hpp>
2+
#include <stan_rng.hpp>
33

44
// [[Rcpp::export]]
5-
SEXP base_rng(boost::uint32_t seed = 0) {
6-
Rcpp::XPtr<boost::ecuyer1988> rng_ptr(new boost::ecuyer1988(seed));
5+
SEXP base_rng(boost::uint32_t seed = 1) {
6+
Rcpp::XPtr<stan::rng_t> rng_ptr(new stan::rng_t(seed));
77
return rng_ptr;
88
}

inst/include/model_methods.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
#include <stan/model/model_base.hpp>
44
#include <stan/model/log_prob_grad.hpp>
55
#include <stan/model/log_prob_propto.hpp>
6-
#include <boost/random/additive_combine.hpp>
76
#ifdef CMDSTAN_JSON
87
#include <cmdstan/io/json/json_data.hpp>
98
#else
109
#include <stan/io/json/json_data.hpp>
1110
#include <stan/io/empty_var_context.hpp>
1211
#endif
12+
#include <stan_rng.hpp>
1313

1414
std::shared_ptr<stan::io::var_context> var_context(std::string file_path) {
1515
if (file_path == "") {
@@ -36,7 +36,7 @@ Rcpp::List model_ptr(std::string data_path, boost::uint32_t seed) {
3636
Rcpp::XPtr<stan::model::model_base> ptr(
3737
&new_model(*var_context(data_path), seed, &Rcpp::Rcout)
3838
);
39-
Rcpp::XPtr<boost::ecuyer1988> base_rng(new boost::ecuyer1988(seed));
39+
Rcpp::XPtr<stan::rng_t> base_rng(new stan::rng_t(seed));
4040
return Rcpp::List::create(
4141
Rcpp::Named("model_ptr") = ptr,
4242
Rcpp::Named("base_rng") = base_rng
@@ -144,7 +144,7 @@ std::vector<double> constrain_variables(SEXP ext_model_ptr, SEXP base_rng,
144144
bool return_trans_pars,
145145
bool return_gen_quants) {
146146
Rcpp::XPtr<stan::model::model_base> ptr(ext_model_ptr);
147-
Rcpp::XPtr<boost::ecuyer1988> rng(base_rng);
147+
Rcpp::XPtr<stan::rng_t> rng(base_rng);
148148
std::vector<int> params_i;
149149
std::vector<double> vars;
150150

inst/include/rcpp_tuple_interop.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#ifndef CMDSTANR_RCPP_TUPLE_INTEROP_HPP
2+
#define CMDSTANR_RCPP_TUPLE_INTEROP_HPP
3+
14
#include <stan/math/prim/functor/apply.hpp>
25
#include <Rcpp.h>
36

@@ -38,3 +41,5 @@ namespace Rcpp {
3841
}, x);
3942
}
4043
}
44+
45+
#endif

inst/include/stan_rng.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef CMDSTANR_STAN_RNG_HPP
2+
#define CMDSTANR_STAN_RNG_HPP
3+
4+
#include <boost/random/additive_combine.hpp>
5+
#include <stan/version.hpp>
6+
7+
// A consistent rng_t is defined from 2.35 onwards
8+
// so add a fallback for older versions
9+
#if STAN_MAJOR == 2 && STAN_MINOR >= 35
10+
#include <stan/services/util/create_rng.hpp>
11+
#else
12+
namespace stan {
13+
using rng_t = boost::ecuyer1988;
14+
}
15+
#endif
16+
17+
#endif

0 commit comments

Comments
 (0)