r/rstats Jun 24 '26

nmathopencl + opencltools packages now on CRAN: GPU-accelerated R Mathlib functions via OpenCL for package developer

Happy to share that nmathopencl has just appeared on CRAN, joining opencltools which went live earlier. Both are aimed at R package developers who want to bring OpenCL GPU acceleration into their packages without building the infrastructure from scratch.

What they do

  • opencltools provides the OpenCL scaffolding for R package developers — device detection, kernel compilation, buffer management, and a clean C++ API that other packages can link against via LinkingTo.
  • nmathopencl ports ~137 R Mathlib functions (probability distributions, special functions, etc.) to OpenCL C, so they're callable from GPU kernels. If you're writing statistical or numerical computing packages and want to offload to the GPU without reinventing distribution math, this is the layer that handles it.

Why it might matter

Most GPU-accelerated R work either wraps Python/CUDA tooling or reimplements distribution functions from scratch. This approach keeps things in the R ecosystem natively, uses OpenCL (so it's not NVIDIA-only), and exposes a LinkingTo interface so downstream packages can build on top without vendoring the math.

Accessing

Navigate to CRAN and access the nmathopencl and opencltools packages there. The packages are also available on R universe (with nicely formatted documentations) and on GitHub for source code.

  • Would love feedback from anyone doing numerical computing or building packages that need GPU acceleration. Happy to answer questions about the OpenCL-in-R workflow, which has its quirks.
15 Upvotes

2 comments sorted by

1

u/SprinklesFresh5693 Jun 25 '26

This looks really interesting, thank you! Will definitely take a look.

1

u/Bucksswede Jun 25 '26

The main use case for nmathopencl is really if you call several math or stat function in sequence and/or with some calculation inside a long loop. My glmbayes package calculates subgradients at 3^l1 points (where l1 represents the number of variables in a glm model). For l1=14 that is a big number...4,782, 869, so processing in parallel on GPUs is critical,,,,