Skip to content

Commit

Permalink
Add opencl to CI, test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
andrjohns committed Jan 4, 2025
1 parent 8710b1f commit eb6d500
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
28 changes: 22 additions & 6 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ name: Unit tests
'on':
push:
branches:
- master
- opencl-ci
pull_request:
branches:
- master
Expand All @@ -23,18 +23,19 @@ jobs:
fail-fast: false
matrix:
config:
- {os: macOS-latest, r: 'devel', rtools: ''}
- {os: macOS-latest, r: 'release', rtools: ''}
- {os: macOS-latest, r: 'devel', rtools: 'true', opencl: true}
- {os: macOS-latest, r: 'release', rtools: 'true', opencl: true}
- {os: windows-latest, r: 'devel', rtools: '44'}
- {os: windows-latest, r: 'release', rtools: '44'}
- {os: windows-latest, r: 'oldrel', rtools: '43'}
- {os: ubuntu-latest, r: 'devel', rtools: ''}
- {os: ubuntu-latest, r: 'release', rtools: ''}
- {os: ubuntu-latest, r: 'oldrel', rtools: ''}
- {os: ubuntu-latest, r: 'devel', rtools: '', opencl: true}
- {os: ubuntu-latest, r: 'release', rtools: '', opencl: true}
- {os: ubuntu-latest, r: 'oldrel', rtools: '', opencl: true}
env:
R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
NOT_CRAN: true
CMDSTANR_OPENCL_TESTS: ${{ matrix.config.opencl }}

steps:
- name: cmdstan env vars
Expand Down Expand Up @@ -64,8 +65,23 @@ jobs:

- uses: r-lib/actions/[email protected]
with:
cache: "always"
extra-packages: any::rcmdcheck, local::.

- name: Install POCL on Ubuntu Runners
if: ${{ matrix.config.os == 'ubuntu-latest' }}
run: |
sudo apt-get update && sudo apt-get install -y ocl-icd-opencl-dev pocl-opencl-icd
shell: bash

- name: Install POCL on MacOS Runners
if: ${{ matrix.config.os == 'macOS-latest' }}
run: |
brew install pocl
# Set LDLIBS_OPENCL environment variable
echo "LDLIBS_OPENCL='-L/opt/homebrew/opt/opencl-icd-loader/lib -lOpenCL'" >> $GITHUB_ENV
shell: bash

- name: Install cmdstan
run: |
cmdstanr::check_cmdstan_toolchain(fix = TRUE)
Expand Down
18 changes: 10 additions & 8 deletions tests/testthat/test-opencl.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ test_that("all methods error when opencl_ids is used with non OpenCL model", {
test_that("all methods error on invalid opencl_ids", {
skip_if_not(Sys.getenv("CMDSTANR_OPENCL_TESTS") %in% c("1", "true"))
stan_file <- testing_stan_file("bernoulli")
mod <- cmdstan_model(stan_file = stan_file, cpp_options = list(stan_opencl = TRUE))
mod <- cmdstan_model(stan_file = stan_file, cpp_options = list(stan_opencl = TRUE),
force_recompile = TRUE)
utils::capture.output(
expect_warning(
mod$sample(data = testing_data("bernoulli"), opencl_ids = c(1000, 1000), chains = 1),
Expand Down Expand Up @@ -74,33 +75,34 @@ test_that("all methods run with valid opencl_ids", {
fit <- mod$sample(data = testing_data("bernoulli"), opencl_ids = c(0, 0), chains = 1)
)
expect_false(is.null(fit$metadata()$opencl_platform_name))
expect_false(is.null(fit$metadata()$opencl_ids_name))
expect_false(is.null(fit$metadata()$opencl_device_name))

stan_file_gq <- testing_stan_file("bernoulli_ppc")
mod_gq <- cmdstan_model(stan_file = stan_file_gq, cpp_options = list(stan_opencl = TRUE))
mod_gq <- cmdstan_model(stan_file = stan_file_gq, cpp_options = list(stan_opencl = TRUE),
force_recompile = TRUE)
expect_gq_output(
fit <- mod_gq$generate_quantities(fitted_params = fit, data = testing_data("bernoulli"), opencl_ids = c(0, 0)),
)
expect_false(is.null(fit$metadata()$opencl_platform_name))
expect_false(is.null(fit$metadata()$opencl_ids_name))
expect_false(is.null(fit$metadata()$opencl_device_name))

expect_sample_output(
fit <- mod$sample(data = testing_data("bernoulli"), opencl_ids = c(0, 0))
)
expect_false(is.null(fit$metadata()$opencl_platform_name))
expect_false(is.null(fit$metadata()$opencl_ids_name))
expect_false(is.null(fit$metadata()$opencl_device_name))

expect_optim_output(
fit <- mod$optimize(data = testing_data("bernoulli"), opencl_ids = c(0, 0))
)
expect_false(is.null(fit$metadata()$opencl_platform_name))
expect_false(is.null(fit$metadata()$opencl_ids_name))
expect_false(is.null(fit$metadata()$opencl_device_name))

expect_vb_output(
fit <- mod$variational(data = testing_data("bernoulli"), opencl_ids = c(0, 0))
)
expect_false(is.null(fit$metadata()$opencl_platform_name))
expect_false(is.null(fit$metadata()$opencl_ids_name))
expect_false(is.null(fit$metadata()$opencl_device_name))
})

test_that("error for runtime selection of OpenCL devices if version less than 2.26", {
Expand All @@ -111,7 +113,7 @@ test_that("error for runtime selection of OpenCL devices if version less than 2.
mod <- cmdstan_model(stan_file = stan_file, cpp_options = list(stan_opencl = TRUE),
force_recompile = TRUE)
expect_error(
mod$sample(data = data_list, chains = 1, refresh = 0, opencl_ids = c(1,1)),
mod$sample(data = testing_data("bernoulli"), chains = 1, refresh = 0, opencl_ids = c(1,1)),
"Runtime selection of OpenCL devices is only supported with CmdStan version 2.26 or newer",
fixed = TRUE
)
Expand Down

0 comments on commit eb6d500

Please sign in to comment.