Chapter 16: Large models — GPU acceleration using OpenCL

Introduction

GPU acceleration is an optional feature of the glmbayes modelling stack. All modeling functions — glmb(), lmb(), rglmb(), and related tools — run fully on the CPU regardless of whether OpenCL is available. No setup is needed for standard use.

OpenCL is implemented in the compiled backend glmbayesCore (imported by glmbayes). A CPU-only glmbayes install is normal when glmbayesCore was built without OpenCL; enabling GPU acceleration means reinstalling glmbayesCore from source with OpenCL available at compile time — not rebuilding glmbayes itself ( glmbayes has no C++ configure step).

Where GPU acceleration pays off is with large models: high-dimensional predictor sets or large posterior sample sizes. Envelope construction and evaluation in glmbayesCore — gradient and log-posterior work at each tangency grid point — is embarrassingly parallel. Passing use_opencl = TRUE to glmb() or lmb() can substantially reduce wall time. See vignette("Chapter-A10", package = "glmbayesCore") for technical detail.

This chapter is the glmbayes user checklist. Backend-only installs should also read vignette("Chapter-16", package = "glmbayesCore").

What you see when you load glmbayes

When glmbayes is attached interactively it checks whether GPU acceleration appears feasible. If has_opencl() is TRUE — meaning glmbayesCore was compiled with OpenCL — attach is silent.

If has_opencl() is FALSE and a GPU or OpenCL stack is detected, you may see:

Note: glmbayes provides full CPU capability in this session
(e.g. glmb(), lmb(), Prior_Setup()). GPU acceleration is recommended
for bigger models and appears available. Reinstall glmbayesCore from
source with OpenCL at compile time to enable it; see vignette("Chapter-16",
"glmbayes") for install instructions.

On a machine with no GPU and no OpenCL stack, attach is silent.

To suppress the message:

options(glmbayes.quiet_opencl_startup = TRUE)

Enabling GPU acceleration: three steps

Work through these steps in order.

Step 1: Check whether OpenCL is already enabled

library(glmbayes)
has_opencl()

has_opencl() reports whether glmbayesCore in this session was built with OpenCL. If TRUE, pass use_opencl = TRUE to glmb() or lmb() and you are done. Otherwise continue.

Step 2: Ensure backend dependencies are OpenCL-ready

glmbayes depends on glmbayesCore, which in turn depends on opencltools and nmathopencl. For a GPU-enabled backend, opencltools must be built with OpenCL support and the system must provide drivers, headers, and an ICD loader.

Check opencltools:

opencltools::has_opencl()

If FALSE, follow vignette("Chapter-01", package = "opencltools"), then reinstall opencltools (and nmathopencl) from source:

install.packages(
  c("opencltools", "nmathopencl"),
  repos = c("https://cloud.r-project.org", "https://knygren.r-universe.dev"),
  type = "source"
)

What you need on your system (summary; details in opencltools Chapter 01):

Component What it provides Needed for
GPU driver Exposes hardware to the OS Runtime
OpenCL headers (CL/cl.h) Required at compile time Source build
OpenCL ICD loader Dispatches to vendor runtime Runtime

Step 3: Reinstall glmbayesCore from source

Reinstall glmbayesCore from source so its configure / configure.win can enable OpenCL. Reinstalling glmbayes alone does not compile OpenCL into the backend.

Prebuilt CRAN / R-Universe glmbayesCore binaries are often CPU-only. Use type = "source" from R-Universe (or GitHub) after Step 2.

Windows

Install Rtools and, if needed, devtools:

install.packages("devtools")
install.packages(
  "glmbayesCore",
  repos = c("https://cloud.r-project.org", "https://knygren.r-universe.dev"),
  type = "source"
)

Development versions:

devtools::install_github("knygren/glmbayesCore")
devtools::install_github("knygren/glmbayes")

Linux / macOS

install.packages(
  "glmbayesCore",
  repos = c("https://cloud.r-project.org", "https://knygren.r-universe.dev"),
  type = "source"
)

Then update glmbayes if needed (binary install is fine once glmbayesCore has OpenCL):

install.packages("glmbayes")

After the install

library(glmbayes)
has_opencl()
#> [1] TRUE

Verifying the setup

diagnose_glmbayes()

This combines opencltools host checks with compile-time status for glmbayesCore. Example output:

=== glmbayes OpenCL Diagnostic Report ===
...
[OK] glmbayes was compiled with OpenCL support.

The final line refers to the glmbayesCore backend (wording is historical).

Common resolutions:

  • Driver / headers / runtime → see opencltools Chapter 01.
  • Runtime probe failed → clinfo outside R; ICD vendors on Linux.
  • Not compiled with OpenCL → repeat Step 2, reinstall glmbayesCore from source (not glmbayes alone).

Running a GPU-accelerated model

Pass use_opencl = TRUE to glmb() or lmb():

example(Cleveland)

Cleveland CPU vs OpenCL illustration (not executed during vignette build):

library(glmbayes)
data("Cleveland")

ps <- Prior_Setup(
  hd ~ age + sex + cp + trestbps + chol +
    fbs + restecg + thalach + exang + oldpeak + slope + ca + thal,
  family = binomial(logit),
  data = Cleveland
)

t_cpu <- system.time({
  fit_cpu <- glmb(
    hd ~ age + sex + cp + trestbps + chol +
      fbs + restecg + thalach + exang + oldpeak + slope + ca + thal,
    family = binomial(link = "logit"),
    pfamily = dNormal(mu = ps$mu, Sigma = ps$Sigma),
    data = Cleveland,
    n = 20000,
    Gridtype = 2,
    use_parallel = TRUE,
    use_opencl = FALSE,
    verbose = FALSE
  )
})

t_gpu <- system.time({
  fit_gpu <- glmb(
    hd ~ age + sex + cp + trestbps + chol +
      fbs + restecg + thalach + exang + oldpeak + slope + ca + thal,
    family = binomial(link = "logit"),
    pfamily = dNormal(mu = ps$mu, Sigma = ps$Sigma),
    data = Cleveland,
    n = 20000,
    Gridtype = 2,
    use_parallel = TRUE,
    use_opencl = TRUE,
    verbose = FALSE
  )
})

t_cpu
t_gpu

summary(fit_gpu)

Appendix A: AMD GPUs on Linux (ROCm OpenCL)

On Ubuntu 22.04 or 24.04 LTS:

sudo apt-get install rocm-opencl-runtime

Supported ROCm GPUs include RX 7900 / 7800 / 7700 and Instinct MI200 / MI300. See vignette("Chapter-01", package = "opencltools").