An R package for running MCMC samplers on GPUs via OpenCL. Write your posterior function in OpenCL C, and rcocl handles compilation, multi-device dispatch, adaptive proposals, and sample collection.
devtools::install_github("bamonroe/rcocl")Requires an OpenCL-capable GPU and drivers.
- GPU-accelerated MCMC: Run thousands of chains in parallel on GPU hardware
- Adaptive proposals: Automatic tuning of proposal covariance matrices with configurable adaptation schedules
- Multi-device support: Distribute chains across multiple OpenCL devices
- Batched parameters: Group parameters into blocks with independent covariance structures
- Binary caching: Compiled OpenCL kernels are cached by MD5 hash for fast re-runs
- Built-in distributions: OpenCL implementations of common distributions (normal, beta, log-normal, etc.)
library(rcocl)
# Define your posterior in OpenCL C (inline or as a .c file)
kernel_code <- "
float my_posterior(float* pars, ...) {
// your log-posterior here
}
"
# Configure the sampler
config <- list(
draws = 10000,
skip = 1000,
fn = "my_posterior",
algo = "mhmc",
device_list = list(list(plat_num = 0, dev_num = 0, work_items = 256))
)
# Run
result <- gpu_mcmc(kernel_code, config,
init = c(a = 0, b = 0),
init_sd = c(0.1, 0.1),
dat = list(double = list(y), int = list()))
# result$samples is a data.frame with columns: iter, chain, posterior, a, bMIT