Skip to content

Commit

Permalink
Merge pull request #1148 from stan-dev/experimental
Browse files Browse the repository at this point in the history
Synchronise `experimental` branch
  • Loading branch information
andrjohns authored Jan 15, 2025
2 parents 9b8d8fe + 7ffa7fc commit 33c31a7
Show file tree
Hide file tree
Showing 39 changed files with 39,943 additions and 52,196 deletions.
161 changes: 31 additions & 130 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,162 +27,63 @@ jobs:
fail-fast: true
matrix:
config:
- {os: macOS-latest, r: 'devel'}
- {os: macOS-latest, r: 'release'}
- {os: macOS-latest, r: 'oldrel'}
- {os: windows-latest, r: 'devel'}
- {os: windows-latest, r: 'release'}
- {os: windows-latest, r: 'oldrel'}
- {os: ubuntu-20.04, r: 'devel'}
- {os: ubuntu-20.04, r: 'release'}
- {os: ubuntu-20.04, r: 'oldrel'}
- {os: ubuntu-latest, r: 'devel'}
- {os: ubuntu-latest, r: 'release'}
- {os: ubuntu-latest, r: 'oldrel'}

env:
R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
with:
submodules: recursive

- uses: r-lib/actions/setup-r@v2
with:
r-version: ${{ matrix.config.r }}

- uses: r-lib/actions/setup-pandoc@v2

- name: Query dependencies
# Use a shorter temp directory for pak installations, due to filename
# length issues on Windows. https://github.com/r-lib/pak/issues/252
- name: Windows temp dir
if: runner.os == 'Windows'
run: |
options(install.packages.check.source = "no")
options(pkgType = ifelse(grepl("linux", R.version$os), "source", "binary"))
install.packages('remotes')
install.packages('V8')
saveRDS(remotes::dev_package_deps(pkg = "./rstan/rstan", dependencies = TRUE), ".github/depends.Rds", version = 2)
writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version")
shell: Rscript {0}
New-Item -Path "C:\" -Name "tmp" -ItemType Directory
echo "TMPDIR=c:\tmp" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
- name: Cache R packages
if: runner.os != 'Windows'
uses: actions/cache@v2
- uses: r-lib/actions/setup-r-dependencies@v2
with:
path: ${{ env.R_LIBS_USER }}
key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }}
restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-
working-directory: rstan/rstan
extra-packages: any::rcmdcheck, local::../../StanHeaders

- name: Install system dependencies
if: runner.os == 'Linux'
env:
RHUB_PLATFORM: linux-x86_64-ubuntu-gcc
- name: Remove symlinks on Windows
if: runner.os == 'Windows'
run: |
Rscript -e "remotes::install_github('r-hub/sysreqs')"
sysreqs=$(Rscript -e "cat(sysreqs::sysreq_commands('./rstan/rstan/DESCRIPTION'))")
sudo -s eval "$sysreqs"
rm -Rf inst/include/src \
inst/include/mathlib \
inst/include/stan \
inst/include/libsundials || true
shell: bash

- name: Install dependencies
run: |
remotes::install_deps(pkg = "./rstan/rstan", dependencies = TRUE)
remotes::install_cran("rcmdcheck")
remotes::install_cran("rstantools", type = "source")
remotes::install_cran("sessioninfo")
if (utils::packageVersion("sessioninfo") >= "1.2.1") {
sessioninfo::session_info(pkgs = "installed", include_base = TRUE)
} else {
options(width = 200)
sessioninfo::session_info(rownames(installed.packages()), include_base = TRUE)
}
shell: Rscript {0}

- name: Install StanHeaders
run: |
try(system("sh sh_b.sh --no-build-vignettes --no-manual"))
shell: Rscript {0}

- name: Session info
run: |
options(width = 100)
pkgs <- installed.packages()[, "Package"]
sessioninfo::session_info(pkgs, include_base = TRUE)
shell: Rscript {0}

- name: Verify Math Version
run: |
readLines(system.file("include","stan","math","version.hpp", package="StanHeaders"))
shell: Rscript {0}

- name: Check
- uses: r-lib/actions/check-r-package@v2
with:
working-directory: StanHeaders
error-on: '"error"'
env:
_R_CHECK_CRAN_INCOMING_: false
run: |
if (R.version$major < 4 && isTRUE(.Platform$OS.type == "windows")) {
dotR <- file.path(Sys.getenv("HOME"), ".R")
if (!file.exists(dotR)) dir.create(dotR)
path_makevars <- ifelse(.Platform$OS.type == "windows", "Makevars.win", "Makevars")
M <- file.path(dotR, path_makevars)
if (!file.exists(M)) file.create(M)
cat("\nCXX14FLAGS=-O3",
"CXX14 = $(BINPREF)g++ -m$(WIN) -std=c++1y",
"CXX11FLAGS=-O3",
file = M, sep = "\n", append = TRUE)
}
rcmdcheck::rcmdcheck(path = "./rstan/rstan", args = c("--no-manual", "--as-cran", "--ignore-vignettes"), build_args = c("--no-build-vignettes"), error_on = "warning", check_dir = "check")
shell: Rscript {0}

- name: Show testthat output
if: always()
run: |
rm -rf check/rstan.Rcheck/rstan check/rstan.Rcheck/*pkg_src
find check -name 'testthat.Rout*' -exec cat '{}' \; || true
shell: bash

- name: Upload check results
if: failure()
uses: actions/upload-artifact@v2
- uses: r-lib/actions/check-r-package@v2
with:
name: ${{ runner.os }}-r${{ matrix.config.r }}-results
path: check

- name: Extract version and check StanHeaders source package
run: |
set -x
echo "STANHEADERS_VERSION=$(grep '^Version' StanHeaders/DESCRIPTION | sed 's/.*: *//')" >> $GITHUB_ENV
echo "RSTAN_VERSION=$(grep '^Version' rstan/rstan/DESCRIPTION | sed 's/.*: *//')" >> $GITHUB_ENV
if [[ ! -f StanHeaders_*.tar.gz ]]; then
sh sh_b.sh --no-build-vignettes --no-manual || true
fi
mkdir -p build || true
cp -v StanHeaders_*.tar.gz build/ || true
ls -R build/
shell: bash

- name: Build RStan source package
if: matrix.config.os == 'ubuntu-20.04' && matrix.config.r == 'devel'
run: |
src_rstan <- pkgbuild::build("./rstan/rstan", dest_path = tempdir(), vignettes = FALSE, manual = FALSE)
file.copy(src_rstan, "build")
shell: Rscript {0}

- name: Build binary packages
run: |
bin1 <- pkgbuild::build("./build/StanHeaders_${{ env.STANHEADERS_VERSION }}.tar.gz", dest_path = tempdir(), vignettes = FALSE, manual = FALSE, binary = TRUE)
bin2 <- pkgbuild::build("./rstan/rstan", dest_path = tempdir(), vignettes = FALSE, manual = FALSE, binary = TRUE)
dir.create("build_bin")
file.copy(c(bin1, bin2), "build_bin")
shell: Rscript {0}

- name: Upload StanHeaders source package
if: matrix.config.os == 'ubuntu-20.04' && matrix.config.r == 'devel'
uses: actions/upload-artifact@v2
with:
name: StanHeaders_${{ env.STANHEADERS_VERSION }}.tar.gz
path: build/StanHeaders_${{ env.STANHEADERS_VERSION }}.tar.gz

- name: Upload RStan source package
if: matrix.config.os == 'ubuntu-20.04' && matrix.config.r == 'devel'
uses: actions/upload-artifact@v2
with:
name: rstan_${{ env.RSTAN_VERSION }}.tar.gz
path: build/rstan_${{ env.RSTAN_VERSION }}.tar.gz
working-directory: rstan/rstan
env:
_R_CHECK_CRAN_INCOMING_: false

- name: Upload binary packages
uses: actions/upload-artifact@v2
with:
name: R-${{ matrix.config.r }}_${{ matrix.config.os }}
path: build_bin
5 changes: 5 additions & 0 deletions StanHeaders/.prepare
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

git checkout experimental
cd inst/include/upstream && git checkout develop && git pull && cd ../../..
cd inst/include/upstream/lib/stan_math && git checkout develop && git pull && cd ../../../../..
2 changes: 1 addition & 1 deletion StanHeaders/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: StanHeaders
Title: C++ Header Files for Stan
Version: 2.33.1.9000
Version: 2.36.0.9000
Authors@R: c(person("Ben",family="Goodrich", email="benjamin.goodrich@columbia.edu", role=c('cre','aut')),
person("Joshua", "Pritikin", role = "ctb"),
person("Andrew", "Gelman", role = "aut"),
Expand Down
12 changes: 11 additions & 1 deletion StanHeaders/R/Flags.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ CxxFlags <- function(as_character = FALSE) {
CXXFLAGS <- paste0("-I", shQuote(TBB_INC), " -D_REENTRANT -DSTAN_THREADS -DTBB_INTERFACE_NEW")
} else {
CXXFLAGS <- paste0("-I", shQuote(TBB_INC), " -D_REENTRANT -DSTAN_THREADS")
# RcppParallel TBB does not have assembly code for Windows ARM64
# so we need to use compiler builtins
if (.Platform$OS.type == "windows" && R.version$arch == "aarch64") {
CXXFLAGS <- paste(CXXFLAGS, "-DTBB_USE_GCC_BUILTINS")
}
}

if (isTRUE(as_character)) return(CXXFLAGS)
Expand All @@ -24,7 +29,12 @@ LdFlags <- function(as_character = FALSE) {
TBB_LIB <- system.file("lib", .Platform$r_arch, package = "RcppParallel", mustWork = TRUE)
}

PKG_LIBS <- paste0("-L", shQuote(TBB_LIB), " -Wl,-rpath,", shQuote(TBB_LIB), " -ltbb -ltbbmalloc")
PKG_LIBS <- paste0("-L", shQuote(TBB_LIB))
# RTools aarch64 does not support rpath, but it is not used on Windows anyway
if (!(.Platform$OS.type == "windows" && R.version$arch == "aarch64")) {
PKG_LIBS <- paste0(PKG_LIBS, " -Wl,-rpath,", shQuote(TBB_LIB))
}
PKG_LIBS <- paste0(PKG_LIBS, " -ltbb -ltbbmalloc")

if (isTRUE(as_character)) return(PKG_LIBS)
cat(PKG_LIBS, " ")
Expand Down
38 changes: 28 additions & 10 deletions StanHeaders/R/stanFunction.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,30 @@ stanFunction <- function(function_name, ..., env = parent.frame(), rebuild = FAL
Eigen <- FALSE
if (is.matrix(x)) {
Eigen <- TRUE
if (nrow(x) == 1L) type <- "stan::math::row_vector_d"
else type <- "stan::math::matrix_d"
if (is.complex(x)) {
if (nrow(x) == 1L) {
type <- "Eigen::Matrix<std::complex<double>, 1, -1>"
} else type <- "Eigen::MatrixXcd"
} else if (nrow(x) == 1L) {
type <- "stan::math::row_vector_d"
} else type <- "stan::math::matrix_d"
} else if (length(x) > 1L) {
if (is.integer(x)) {
type <- "std::vector<int>"
} else {
Eigen <- TRUE
type <- "stan::math::vector_d"
if (is.complex(x)) {
type <- "Eigen::VectorXcd"
} else type <- "stan::math::vector_d"
}
} else if (is.integer(x)) {
type <- "int"
} else if (is.numeric(x)) {
type <- "double"
} else if (is.complex(x)) {
type <- "std::complex<double>"
} else stop(paste("all arguments to", function_name, "must be matrices,",
"vectors, integers, doubles or lists thereof"))
"vectors, integers, doubles, complexes, or lists thereof"))
if (Eigen) type <- paste0("const ", type, "&")
else type <- paste0("const ", type)
return(type)
Expand All @@ -43,6 +52,8 @@ stanFunction <- function(function_name, ..., env = parent.frame(), rebuild = FAL
if (any(double_lists)) types[double_lists] <- "const List&"
int_lists <- types == "const std::vector<int >&"
if (any(int_lists)) types[int_lists] <- "const List&"
complex_lists <- types == "const std::vector<std::complex<double> >&"
if (any(complex_lists)) types[complex_lists] <- "const List&"
code <- paste0("auto ", function_name, "(",
paste(types, names(types), collapse = ", "),
") { return stan::math::", function_name, "(",
Expand All @@ -52,7 +63,11 @@ stanFunction <- function(function_name, ..., env = parent.frame(), rebuild = FAL
ifelse(int_lists,
paste0("std::vector<int>(", names(types), ".begin(), ",
names(types), ".end())"),
names(types))), collapse = ", "), "); }")
ifelse(complex_lists,
paste0("std::vector<complex<double>(",
names(types), ".begin(), ",
names(types), ".end())"),
names(types)))), collapse = ", "), "); }")
incl <- dir(system.file("include", "stan", "math", "prim",
package = "StanHeaders", mustWork = TRUE),
pattern = "hpp$")
Expand All @@ -67,18 +82,21 @@ stanFunction <- function(function_name, ..., env = parent.frame(), rebuild = FAL
incl <- c(incl, paste0('#include ', dQuote(create_rng)))
code <- sub(") {", ", const int random_seed = 0) {", code, fixed = TRUE)
code <- sub(" return ",
"boost::ecuyer1988 base_rng__ = stan::services::util::create_rng(random_seed, 0); return ",
"boost::random::mixmax base_rng__ = stan::services::util::create_rng(random_seed, 0); return ",
code)
code <- sub("); }", ", base_rng__); }", code, fixed = TRUE)
}
withr::with_makevars(
c(
PKG_CXXFLAGS = CxxFlags(as_character = TRUE),
PKG_LIBS = LdFlags(as_character = TRUE)
PKG_LIBS = LdFlags(as_character = TRUE),
USE_CXX17 = 1
),
Rcpp::cppFunction(code, depends = c("StanHeaders", "RcppEigen", "BH"),
includes = incl, env = env, rebuild = rebuild,
cacheDir = cacheDir,
Rcpp::cppFunction(code,
depends = c("StanHeaders", "RcppEigen", "BH"),
plugins = "cpp17",
includes = incl,
env = env, rebuild = rebuild, cacheDir = cacheDir,
showOutput = showOutput, verbose = verbose)
)
if (grepl("_rng$", function_name)) {
Expand Down
1 change: 0 additions & 1 deletion StanHeaders/inst/include/libsundials

This file was deleted.

1 change: 0 additions & 1 deletion StanHeaders/inst/include/mathlib

This file was deleted.

1 change: 0 additions & 1 deletion StanHeaders/inst/include/src

This file was deleted.

1 change: 0 additions & 1 deletion StanHeaders/inst/include/stan

This file was deleted.

2 changes: 1 addition & 1 deletion StanHeaders/inst/include/upstream
Submodule upstream updated 188 files
Loading

0 comments on commit 33c31a7

Please sign in to comment.