From eb6d500a061486743a3356a6f608d72b31c14646 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Sat, 4 Jan 2025 16:49:31 +0800 Subject: [PATCH] Add opencl to CI, test fixes --- .github/workflows/R-CMD-check.yaml | 28 ++++++++++++++++++++++------ tests/testthat/test-opencl.R | 18 ++++++++++-------- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index 9e23f3e39..8e0e61ef2 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -7,7 +7,7 @@ name: Unit tests 'on': push: branches: - - master + - opencl-ci pull_request: branches: - master @@ -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 @@ -64,8 +65,23 @@ jobs: - uses: r-lib/actions/setup-r-dependencies@v2.11.1 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) diff --git a/tests/testthat/test-opencl.R b/tests/testthat/test-opencl.R index 55c59e2c8..d5774d998 100644 --- a/tests/testthat/test-opencl.R +++ b/tests/testthat/test-opencl.R @@ -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), @@ -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", { @@ -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 )