This chapter describes how to enable GPU acceleration in
glmbayes using OpenCL. GPU acceleration can dramatically
reduce computation time for large envelope models, especially when
working with high‑dimensional predictors or repeated model fits. OpenCL
is used because it is vendor‑neutral and works across NVIDIA, AMD, and
Intel hardware. CPU‑only execution remains fully supported, but may be
significantly slower for large models.
Follow these steps to install and verify glmbayes with
OpenCL:
Ubuntu/Debian: sudo apt-get install build-essential r-base-dev
(If you need the latest R, add the CRAN repo first — see “Installing the latest R on Ubuntu/Debian” below.)
Fedora: sudo dnf groupinstall “Development Tools” sudo dnf install R-devel
Arch Linux: sudo pacman -S base-devel r
Installing the latest R on Ubuntu/Debian (optional but recommended) Ubuntu’s default repositories often contain older R versions. To install the current CRAN release:
sudo apt-get install –no-install-recommends dirmngr gnupg ca-certificates software-properties-common sudo gpg –keyserver keyserver.ubuntu.com –recv-key E298A3A825C0D65DFD57CBB651716619E084DAB9 sudo gpg -a –export E298A3A825C0D65DFD57CBB651716619E084DAB9 | sudo tee /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc sudo add-apt-repository “deb https://CRAN.R-project.org/bin/linux/ubuntu/ jammy-cran40/” sudo apt-get update sudo apt-get install build-essential r-base-dev
macOS requires both the Xcode Command Line Tools and GCC when installing glmbayes from source. This is because the package’s configure script uses GCC to detect system include and library paths for OpenCL.
Install Xcode Command Line Tools:
Install GCC via Homebrew:
Binary installs (e.g., CRAN or R‑Universe macOS binaries) do not require GCC.
glmbayes must currently be installed from
source because neither CRAN nor R‑universe build packages with
OpenCL GPU support. Their build systems do not provide OpenCL headers or
development libraries, so any precompiled binary from those repositories
will have OpenCL disabled. To enable GPU acceleration, you must install
glmbayes from source on a system where a complete OpenCL
development environment is available.
A full OpenCL development environment includes:
libOpenCL.so (needed for
linking)Most GPU drivers provide only the vendor‑specific OpenCL runtime, not the headers or the development symlink. The following subsections describe how to install the required OpenCL components on Windows, Linux, and macOS.
Choose one of:
CUDA Toolkit (recommended)
Includes full OpenCL headers.
https://developer.nvidia.com/cuda-downloads
Intel OpenCL runtimes / Intel oneAPI CPU
runtime
https://www.intel.com/content/www/us/en/developer/articles/tool/opencl-drivers.html
Khronos OpenCL Headers (header‑only
download)
https://github.com/KhronosGroup/OpenCL-Headers
Note: When installing the CUDA Toolkit, Intel OpenCL
SDK, or Khronos OpenCL headers,
you can accept all default installation options. The default settings
include the
OpenCL header files (such as CL/cl.h) required to compile
glmbayes from source.
On Windows, installing the CUDA Toolkit or Intel OpenCL SDK is sufficient; no additional OpenCL runtime or development packages are required.
AMD’s Windows driver package includes the OpenCL runtime and ICD
components automatically. No additional installation or PATH
configuration is required. If OpenCL is not detected, updating to the
latest AMD Software (Adrenalin Edition) typically resolves the issue.
The diagnose_glmbayes() function will report whether the
AMD OpenCL components are correctly installed.
To compile and run glmbayes with OpenCL support on
Linux, you must install:
libOpenCL.so
(linking)The correct OpenCL implementation depends on your GPU vendor.
For NVIDIA and Intel, the OpenCL runtime is installed automatically with
the GPU driver.
For AMD, you must choose the correct OpenCL stack — see Appendix A.
An OpenCL implementation must be present for your GPU vendor:
The remaining subsections describe the generic OpenCL development
components required to compile glmbayes from source.
These provide CL/cl.h, CL/cl_platform.h,
and related headers.
Install:
Ubuntu / Debian
sudo apt-get install opencl-headersFedora
sudo dnf install opencl-headersArch Linux
sudo pacman -S opencl-headersIf these headers are missing, glmbayes cannot compile
GPU support.
This provides the shared library:
libOpenCL.so.1
which is required for OpenCL to function at runtime.
Install:
Ubuntu / Debian
sudo apt-get install ocl-icd-libopencl1Fedora
sudo dnf install ocl-icdArch Linux
sudo pacman -S opencl-icd-loaderWithout the runtime, OpenCL calls (e.g.,
clGetPlatformIDs()) will fail.
To compile against OpenCL, the linker requires the unversioned symlink:
libOpenCL.so
This is not provided by the runtime package.
It is provided by the development package.
Install:
Ubuntu / Debian
sudo apt-get install ocl-icd-opencl-devFedora
sudo dnf install ocl-icd-develArch Linux
(already included with the ICD loader)
Verify installation:
ls -l /usr/lib/x86_64-linux-gnu/libOpenCL*
You should see both:
libOpenCL.solibOpenCL.so.1If only libOpenCL.so.1 is present, install the
development package.
clinfo
(Linux, recommended)Having headers and libOpenCL installed does
not guarantee that a vendor OpenCL
implementation registers a platform. The
glmbayes configure script and
diagnose_glmbayes() both probe for a usable platform
(e.g. clGetPlatformIDs()). On Linux—especially
remote GPU VMs—it helps to check this outside
R first.
Install the clinfo utility:
Ubuntu / Debian
sudo apt-get install clinfoFedora
sudo dnf install clinfoArch Linux
sudo pacman -S clinfoThen run:
You should see Number of platforms >=
1 and at least one GPU device (for NVIDIA,
typically under the NVIDIA platform). If
Number of platforms is 0, the ICD loader
is present but no vendor runtime is visible—install
your vendor’s OpenCL ICD (on Ubuntu/Debian with NVIDIA GPUs,
sudo apt-get install nvidia-opencl-icd is often required in
minimal images) and run clinfo again.
Note: clinfo is a system diagnostic,
not part of R. It complements diagnose_glmbayes() when
debugging cloud or container setups where the NVIDIA
driver looks fine (nvidia-smi works) but OpenCL still
reports no platform.
macOS includes the OpenCL headers and the system OpenCL runtime as
part of the Xcode Command Line Tools, so no additional installation is
required for compilation. However, Apple has deprecated OpenCL, and
modern Apple Silicon systems do not provide hardware OpenCL support. As
a result, glmbayes will compile successfully on macOS, but
GPU acceleration is not guaranteed and may fall back to CPU
execution.
Because CRAN and R‑universe do not build packages with OpenCL support, installing from source is required to enable GPU acceleration.
On Linux, if you have shell access (including
Jupyter Terminal on a cloud instance), running
clinfo before opening R is recommended:
confirm Number of platforms >= 1 (see
§2.2.5). That catches missing vendor ICDs early, independent of the R
session.
If has_opencl() returns FALSE, run:
This function performs a full OpenCL environment check and reports whether your system is correctly configured for GPU acceleration.
A clean diagnostic on Linux looks like:
=== glmbayes Diagnostic Report ===
Environment: linux
GPU: NVIDIA
[OK] Driver installed
[OK] OpenCL headers found (CL/cl.h)
[OK] OpenCL runtime found (OpenCL.dll / ICD)
[OK] OpenCL fully available (headers + runtime)
[OK] Required PATH and library dirs present
[OK] OpenCL runtime probe succeeded (platform available)
[OK] glmbayes was compiled with OpenCL support.
Explanation of each line:
Environment: linux
Confirms the operating system detected by
glmbayes.
GPU: NVIDIA
Identifies the GPU vendor detected by the OpenCL runtime.
(May show Intel, AMD, or “CPU” depending on the system.)
[OK] Driver installed
The system GPU driver is present and exposes an OpenCL
platform.
[OK] OpenCL headers found (CL/cl.h)
The OpenCL development headers required for compilation are
installed.
[OK] OpenCL runtime found (OpenCL.dll /
ICD)
The OpenCL ICD loader (libOpenCL.so.1 on Linux) is
available at runtime.
[OK] OpenCL fully available (headers +
runtime)
Confirms that both compile‑time and run‑time OpenCL components are
present.
[OK] Required PATH and library dirs
present
The library search paths include the directories where OpenCL is
installed.
[OK] OpenCL runtime probe succeeded (platform
available)
A live OpenCL call (clGetPlatformIDs()) succeeded, meaning
the system can enumerate OpenCL devices.
[OK] glmbayes was compiled with OpenCL
support.
Confirms that the package was built with OpenCL enabled (i.e., the
linker found libOpenCL.so).
If any line shows a warning or error, the diagnostic output will indicate which component is missing and how to correct it.
The chunks below are not executed during vignette
builds (runtime). They mirror a large glmb CPU vs OpenCL
comparison; for an interactive GPU check use
example(Cleveland) (§6 above).
# Cleveland GPU-accelerated example (same pattern as example(Cleveland))
# (Not executed in the vignette build.)
library(glmbayes)
# Load the dataset
data("Cleveland")
# ------------------------------------------------------------------
# OpenCL-accelerated Bayesian logistic regression example
# This example only runs if OpenCL is available.
# ------------------------------------------------------------------
# Prior setup for the full model
ps <- Prior_Setup(
hd ~ age + sex + cp + trestbps + chol +
fbs + restecg + thalach + exang + oldpeak + slope + ca + thal,
family = binomial(logit),
data = Cleveland
)
t_non_opencl <- system.time({
fit_non_opencl <- 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_non_openclt_opencl <- system.time({
fit_opencl <- 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_openclAMD provides multiple OpenCL implementations on Linux, but only
ROCm OpenCL is fully supported and stable.
If you are using an AMD GPU, we recommend installing ROCm OpenCL
on Ubuntu 22.04 or 24.04 LTS.
sudo apt-get install rocm-opencl-runtime
This installs:
amdocl64.icd)ROCm OpenCL supports:
Older GPUs (Polaris, Vega, Navi 1x/2x) are not supported by ROCm.