| Title: | 'OpenCL'-Ported R 'Mathlib' for GPU-Accelerated Packages |
|---|---|
| Description: | Ships statistical and mathematical routines from R internal 'nmath' ('Mathlib') as 'OpenCL' C sources under directory 'inst/cl/', with R wrappers that use the GPU when 'OpenCL' is available at compile time and fall back to 'stats' equivalents otherwise. Aimed at package developers building custom kernels (for example Bayesian GLMs via suggested package 'glmbayes') using 'opencltools' kernel loaders and related helpers. Contains translated shims, an illustrative GLM-related kernel subsystem, vignettes, and optional GPU acceleration. |
| Authors: | Kjell Nygren [aut, cre], The R Core Team [ctb, cph] (R 'Mathlib' sources, R stats modeling code, and derived/adapted routines), The R Foundation [cph] (Portions of R 'Mathlib' and R source code), Ross Ihaka [ctb, cph] (R 'Mathlib' and original R modeling infrastructure), Robert Gentleman [ctb, cph] (Portions of R 'Mathlib'), Simon Davies [ctb] (Original R glm implementation), Morten Welinder [ctb, cph] (Portions of R 'Mathlib'), Martin Maechler [ctb] (Portions of R 'Mathlib') |
| Maintainer: | Kjell Nygren <[email protected]> |
| License: | GPL-2 |
| Version: | 0.8.1 |
| Built: | 2026-06-05 19:20:12 UTC |
| Source: | https://github.com/knygren/nmathopencl |
nmathopencl provides OpenCL-ported versions of R's internal nmath and
R_ext math routines, enabling downstream R packages to build custom
GPU-accelerated kernels that call the same statistical distribution functions
available in base R. The package is intended as a developer library: users
install it to gain access to the ported .cl source files, then write their
own OpenCL kernels that #include those sources as needed.
The core deliverable is a collection of .cl files installed under
inst/cl/nmath/ that mirror the R nmath library (density, distribution,
quantile, and random-variate functions). Downstream packages locate these
files at runtime with system.file("cl", package = "nmathopencl") and
assemble them into an OpenCL program using
opencltools::load_kernel_library(..., package = "nmathopencl").
The package also ships Ex_EnvelopeEval and its supporting
functions (Ex_glmbfamfunc, Ex_glmb_Standardize_Model, Ex_EnvelopeSize) as a
worked example of how a downstream package—here the glmbayes Bayesian GLM
sampler—builds a custom kernel on top of the ported nmath routines. See
system.file("examples", "Ex_EnvelopeEval.R", package = "nmathopencl")
and vignette("Chapter-10") for a complete walkthrough.
Optional GPU acceleration is available wherever an OpenCL runtime is
installed. Use nmathopencl_has_opencl to query compile-time OpenCL support,
nmathopencl_opencl_fp64_available / nmathopencl_opencl_device_info for
double-precision device selection used by kernels. Host/runtime diagnostics use
opencltools::diagnose_glmbayes().
The simulation theory underlying the envelope construction is described in (Nygren and Nygren 2006), with implementation details in (Nygren 2025, 2025). OpenCL GPU execution follows (Stone et al. 2010); package vignettes also discuss GPU workflows ((Nygren 2025, 2025)).
GPU *_opencl routines are registered for R_GetCCallable on load.
Downstream packages should LinkingTo: nmathopencl, Imports: nmathopencl, opencltools,
and #include <nmathopencl/nmathopencl_capi.h> (see
system.file("include/nmathopencl/README.md", package = "nmathopencl")).
Call nmathopencl_api_version() for ABI compatibility. Kernel loading
remains on opencltools (opencltools_capi.h).
In interactive sessions, attaching the package with library(nmathopencl)
may emit a packageStartupMessage
comparing compile-time OpenCL support in nmathopencl and
opencltools, noting that CPU fallbacks remain available, and
summarizing whether an OpenCL runtime appears available on the host.
Messages point to ?gpu_diagnostics, vignette("Chapter-01")
(OpenCL enablement), vignette("Chapter-12") (packaged *_opencl
API), and this help page. Host-side OpenCL diagnostics use opencltools.
Set options(nmathopencl.quiet_opencl_startup = TRUE) to suppress
these notes (recommended for CI and R CMD check).
Kjell Nygren
Nygren K (2025).
“Chapter A05: Simulation Methods – Likelihood Subgradient Densities.”
Vignette in the glmbayes R package.
R vignette name: Chapter-A05.
Nygren K (2025).
“Chapter A08: Overview of Envelope Related Functions.”
Vignette in the glmbayes R package.
R vignette name: Chapter-A08.
Nygren K (2025).
“Chapter 12: Large Models: GPU Acceleration using OpenCL.”
Vignette in the glmbayes R package.
R vignette name: Chapter-12.
Nygren K (2025).
“Chapter A10: Accelerated EnvelopeBuild Implementation using OpenCL.”
Vignette in the glmbayes R package.
R vignette name: Chapter-A10.
Nygren K~N, Nygren L~M (2006).
“Likelihood Subgradient Densities.”
Journal of the American Statistical Association, 101(475), 1144–1156.
doi:10.1198/016214506000000357.
Stone JE, Gohara D, Shi G (2010).
“OpenCL: A Parallel Programming Standard for Heterogeneous Computing Systems.”
Computing in Science & Engineering, 12(3), 66–72.
doi:10.1109/MCSE.2010.69.
Key developer entry points:
opencltools::load_kernel_library
— assemble the nmath .cl sources (package = "nmathopencl").
nmathopencl_has_opencl — check whether an OpenCL runtime is present.
nmathopencl_opencl_device_info — which OpenCL device is used for fp64 kernels.
Ex_EnvelopeEval — worked example of a custom kernel built
on the ported nmath routines.
Useful links:
R-Universe: https://knygren.r-universe.dev/nmathopencl
Related sampler package (Suggests): glmbayes
Given a set of user-facing kernel .cl files and a library directory,
computes the full transitive dependency list for each kernel (using the
library's pre-built dependency index) and writes the results back into the
kernel files as annotation tags.
attach_cross_library_tags( kernel_paths, library_dir, depends_tag = "depends_nmath", index = NULL, dry_run = FALSE )attach_cross_library_tags( kernel_paths, library_dir, depends_tag = "depends_nmath", index = NULL, dry_run = FALSE )
kernel_paths |
Character vector of paths to kernel |
library_dir |
Path to the library directory containing
|
depends_tag |
Name of the annotation tag in the kernel files that lists
the direct library entry-point stems (e.g. |
index |
Optional dependency index (write_kernel_dependency_index, load via
|
dry_run |
Logical; if |
Cross-library analogue of attach_kernel_dependency_tags: it targets
kernels that depend on a library through a @{depends_tag} tag (entry-point stems),
instead of expanding @depends purely inside one library tree.
Typical usage for kernels that call nmath functions:
nmath_dir <- system.file(
"cl", "nmath", package = "opencltools")
idx <- readRDS(file.path(nmath_dir, "kernel_dependency_index.rds"))
attach_cross_library_tags(
kernel_paths = list.files("inst/cl/src", "\\\\.cl$", full.names = TRUE),
library_dir = nmath_dir,
depends_tag = "depends_nmath",
index = idx
)
This writes @all_depends_nmath_count and @all_depends_nmath into each
kernel file that carries a @depends_nmath annotation.
A data frame (returned invisibly) with one row per kernel file and columns:
fileBasename of the kernel file.
direct_stemsComma-separated direct entry-point stems read from
@{depends_tag}.
all_depends_countNumber of library files in the full transitive closure.
all_dependsComma-separated full transitive dependency list in load order.
changedTRUE if the file was (or would be, under dry_run)
modified.
attach_kernel_dependency_tags write_kernel_dependency_index load_library_for_kernel
############################ Start of kernel_tagging_workflow example ######################## lib_dir <- system.file("cl/ex_glmbayes_nmath", package = "opencltools") kernels <- list.files( system.file("cl/ex_glmbayes_draft_src", package = "opencltools"), pattern = "\\.cl$", full.names = TRUE ) # Step 1: scan draft kernels for library calls (read-only dry run) step1 <- attach_kernel_call_tags( kernel_paths = kernels, library_dir = lib_dir, library_tag = "nmath", dry_run = TRUE ) step1 # Step 2: expand transitive closure (small nmath library; runs on CRAN check) nmath_small <- system.file("cl/nmath_small", package = "opencltools") tagged <- system.file("cl/src/dnorm_kernel.cl", package = "opencltools") idx_small <- write_kernel_dependency_index(library_dir = nmath_small, write = FALSE) step2_small <- attach_cross_library_tags( kernel_paths = tagged, library_dir = nmath_small, depends_tag = "depends_nmath", index = idx_small, dry_run = TRUE ) nrow(step2_small) # Step 2: full nmath (slow) nmath_dir <- system.file("cl/nmath", package = "opencltools") idx <- write_kernel_dependency_index(library_dir = nmath_dir, write = FALSE) step2 <- attach_cross_library_tags( kernel_paths = tagged, library_dir = nmath_dir, depends_tag = "depends_nmath", index = idx, dry_run = TRUE ) step2 ############################################################################### ## End of kernel_tagging_workflow example ########################################################################################################### Start of kernel_tagging_workflow example ######################## lib_dir <- system.file("cl/ex_glmbayes_nmath", package = "opencltools") kernels <- list.files( system.file("cl/ex_glmbayes_draft_src", package = "opencltools"), pattern = "\\.cl$", full.names = TRUE ) # Step 1: scan draft kernels for library calls (read-only dry run) step1 <- attach_kernel_call_tags( kernel_paths = kernels, library_dir = lib_dir, library_tag = "nmath", dry_run = TRUE ) step1 # Step 2: expand transitive closure (small nmath library; runs on CRAN check) nmath_small <- system.file("cl/nmath_small", package = "opencltools") tagged <- system.file("cl/src/dnorm_kernel.cl", package = "opencltools") idx_small <- write_kernel_dependency_index(library_dir = nmath_small, write = FALSE) step2_small <- attach_cross_library_tags( kernel_paths = tagged, library_dir = nmath_small, depends_tag = "depends_nmath", index = idx_small, dry_run = TRUE ) nrow(step2_small) # Step 2: full nmath (slow) nmath_dir <- system.file("cl/nmath", package = "opencltools") idx <- write_kernel_dependency_index(library_dir = nmath_dir, write = FALSE) step2 <- attach_cross_library_tags( kernel_paths = tagged, library_dir = nmath_dir, depends_tag = "depends_nmath", index = idx, dry_run = TRUE ) step2 ############################################################################### ## End of kernel_tagging_workflow example ###############################################################################
Scans kernel .cl source files for calls to functions provided by a
pre-annotated library, then writes the discovered dependencies as annotation
tags directly into the kernel files.
attach_kernel_call_tags( kernel_paths, library_dir, library_tag, overwrite_existing = FALSE, dry_run = FALSE )attach_kernel_call_tags( kernel_paths, library_dir, library_tag, overwrite_existing = FALSE, dry_run = FALSE )
kernel_paths |
Character vector of paths to kernel |
library_dir |
Path to the pre-annotated library directory. Each
|
library_tag |
String tag suffix used for annotation names, e.g.
|
overwrite_existing |
Logical; if |
dry_run |
Logical; if |
For a library such as nmathopencl, each .cl shard carries a @provides
annotation listing the symbols it defines. This function builds a
provides map from those annotations (symbol shard stem), scans
each kernel's source (with comments and string literals stripped) for
matching function calls, and writes four annotation tags at the top of each
kernel file:
@library_deps: <library_tag>Library name (written if absent).
@calls_<library_tag>: sym1, sym2Symbols from the library actually called in this kernel.
@depends_<library_tag>: stem1, stem2Library shard stems that define the called symbols.
@calls_opencl_builtin: sym | (none)Detected OpenCL work-item and
synchronization builtins (standard math builtins excluded).
Two-step tagging workflow:
# Step 1 — this function: infer direct library calls from source
nmath_dir <- system.file("cl/nmath", package = "nmathopencl")
attach_kernel_call_tags(
kernel_paths = list.files("inst/cl/src", "\\.cl$", full.names = TRUE),
library_dir = nmath_dir,
library_tag = "nmath"
)
# Step 2 — expand to full transitive closure
attach_cross_library_tags(
kernel_paths = list.files("inst/cl/src", "\\.cl$", full.names = TRUE),
library_dir = nmath_dir,
depends_tag = "depends_nmath"
)
A data frame (returned invisibly) with one row per kernel file and columns:
fileBasename of the kernel file.
callsComma-separated library symbols detected in source.
dependsComma-separated shard stems for the detected symbols.
opencl_builtinsComma-separated OpenCL builtins detected, or empty string if none.
changedTRUE if the file was (or would be) modified.
NA means the file was skipped (already tagged).
attach_cross_library_tags, attach_kernel_dependency_tags
############################ Start of kernel_tagging_workflow example ######################## lib_dir <- system.file("cl/ex_glmbayes_nmath", package = "opencltools") kernels <- list.files( system.file("cl/ex_glmbayes_draft_src", package = "opencltools"), pattern = "\\.cl$", full.names = TRUE ) # Step 1: scan draft kernels for library calls (read-only dry run) step1 <- attach_kernel_call_tags( kernel_paths = kernels, library_dir = lib_dir, library_tag = "nmath", dry_run = TRUE ) step1 # Step 2: expand transitive closure (small nmath library; runs on CRAN check) nmath_small <- system.file("cl/nmath_small", package = "opencltools") tagged <- system.file("cl/src/dnorm_kernel.cl", package = "opencltools") idx_small <- write_kernel_dependency_index(library_dir = nmath_small, write = FALSE) step2_small <- attach_cross_library_tags( kernel_paths = tagged, library_dir = nmath_small, depends_tag = "depends_nmath", index = idx_small, dry_run = TRUE ) nrow(step2_small) # Step 2: full nmath (slow) nmath_dir <- system.file("cl/nmath", package = "opencltools") idx <- write_kernel_dependency_index(library_dir = nmath_dir, write = FALSE) step2 <- attach_cross_library_tags( kernel_paths = tagged, library_dir = nmath_dir, depends_tag = "depends_nmath", index = idx, dry_run = TRUE ) step2 ############################################################################### ## End of kernel_tagging_workflow example ########################################################################################################### Start of kernel_tagging_workflow example ######################## lib_dir <- system.file("cl/ex_glmbayes_nmath", package = "opencltools") kernels <- list.files( system.file("cl/ex_glmbayes_draft_src", package = "opencltools"), pattern = "\\.cl$", full.names = TRUE ) # Step 1: scan draft kernels for library calls (read-only dry run) step1 <- attach_kernel_call_tags( kernel_paths = kernels, library_dir = lib_dir, library_tag = "nmath", dry_run = TRUE ) step1 # Step 2: expand transitive closure (small nmath library; runs on CRAN check) nmath_small <- system.file("cl/nmath_small", package = "opencltools") tagged <- system.file("cl/src/dnorm_kernel.cl", package = "opencltools") idx_small <- write_kernel_dependency_index(library_dir = nmath_small, write = FALSE) step2_small <- attach_cross_library_tags( kernel_paths = tagged, library_dir = nmath_small, depends_tag = "depends_nmath", index = idx_small, dry_run = TRUE ) nrow(step2_small) # Step 2: full nmath (slow) nmath_dir <- system.file("cl/nmath", package = "opencltools") idx <- write_kernel_dependency_index(library_dir = nmath_dir, write = FALSE) step2 <- attach_cross_library_tags( kernel_paths = tagged, library_dir = nmath_dir, depends_tag = "depends_nmath", index = idx, dry_run = TRUE ) step2 ############################################################################### ## End of kernel_tagging_workflow example ###############################################################################
Compute and attach derived tags to each .cl file in a library:
@load_order, @all_depends, and @all_depends_count.
attach_kernel_dependency_tags(library_dir, dry_run = FALSE)attach_kernel_dependency_tags(library_dir, dry_run = FALSE)
library_dir |
Directory containing |
dry_run |
Logical; if |
Tags are written only if dependency sorting fully succeeds. If unresolved files remain, no files are modified and a cycle report is returned so source refactoring can be prioritized.
A list with:
Logical success flag.
Human-readable summary.
Sorted records data frame.
Unresolved records data frame (empty on success).
Cycle report data frame (empty on success).
Per-file tag data frame with source_origin, source_type,
includes, depends, provides, load_order, all_depends, and
all_depends_count (present on success).
Functions declared in header files without
attribute_hidden, including declaring header, inferred definition file,
declaration signature, define_alias, and all_depends for the
definition file.
An S3 class "opencl_dependency_tags" is attached for use with
print().
############################ Start of attach_kernel_dependency_tags example ######################## lib_dir <- system.file("cl/ex_glmbayes_nmath", package = "opencltools") res <- attach_kernel_dependency_tags(lib_dir, dry_run = TRUE) res$ok print(res) ############################################################################### ## End of attach_kernel_dependency_tags example ########################################################################################################### Start of attach_kernel_dependency_tags example ######################## lib_dir <- system.file("cl/ex_glmbayes_nmath", package = "opencltools") res <- attach_kernel_dependency_tags(lib_dir, dry_run = TRUE) res$ok print(res) ############################################################################### ## End of attach_kernel_dependency_tags example ###############################################################################
OpenCL-backed wrappers for Bessel functions.
besselI_opencl(x, nu, expon.scaled = FALSE, fallback = FALSE, verbose = FALSE) besselJ_opencl(x, nu, fallback = FALSE, verbose = FALSE) besselK_opencl(x, nu, expon.scaled = FALSE, fallback = FALSE, verbose = FALSE) besselY_opencl(x, nu, fallback = FALSE, verbose = FALSE)besselI_opencl(x, nu, expon.scaled = FALSE, fallback = FALSE, verbose = FALSE) besselJ_opencl(x, nu, fallback = FALSE, verbose = FALSE) besselK_opencl(x, nu, expon.scaled = FALSE, fallback = FALSE, verbose = FALSE) besselY_opencl(x, nu, fallback = FALSE, verbose = FALSE)
x, nu
|
Numeric vectors recycled together. |
expon.scaled |
Logical vector recycled with |
fallback |
When |
verbose |
Logical; print fallback/error diagnostics. |
Numeric vector of recycled common length.
Current Bessel OpenCL paths may require temporary-workspace allocation
semantics equivalent to host R_alloc/vmax* behavior. On some
GPU stacks this can fail at runtime; use fallback = TRUE if you must
tolerate failures until device-side workspace handling is implemented.
if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { # Bessel OpenCL paths currently depend on temporary-workspace allocation # behavior (R_alloc/vmax* semantics) not yet fully implemented for device # execution. Keep these commented to avoid flaky CI/check failures: # n <- 1L # besselI_opencl(x = 2.0, nu = 1.5, expon.scaled = FALSE, fallback = FALSE, verbose = TRUE) # besselJ_opencl(x = 2.0, nu = 1.5, fallback = FALSE, verbose = TRUE) # besselK_opencl(x = 2.0, nu = 1.5, expon.scaled = FALSE, fallback = FALSE, verbose = TRUE) # besselY_opencl(x = 2.0, nu = 1.5, fallback = FALSE, verbose = TRUE) } else { besselI(2.0, nu = 1.5) besselJ(2.0, nu = 1.5) besselK(2.0, nu = 1.5) besselY(2.0, nu = 1.5) }if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { # Bessel OpenCL paths currently depend on temporary-workspace allocation # behavior (R_alloc/vmax* semantics) not yet fully implemented for device # execution. Keep these commented to avoid flaky CI/check failures: # n <- 1L # besselI_opencl(x = 2.0, nu = 1.5, expon.scaled = FALSE, fallback = FALSE, verbose = TRUE) # besselJ_opencl(x = 2.0, nu = 1.5, fallback = FALSE, verbose = TRUE) # besselK_opencl(x = 2.0, nu = 1.5, expon.scaled = FALSE, fallback = FALSE, verbose = TRUE) # besselY_opencl(x = 2.0, nu = 1.5, fallback = FALSE, verbose = TRUE) } else { besselI(2.0, nu = 1.5) besselJ(2.0, nu = 1.5) besselK(2.0, nu = 1.5) besselY(2.0, nu = 1.5) }
OpenCL-backed density, distribution, quantile, and random generation wrappers
for the beta distribution. These mirror the base stats beta family
while adding OpenCL dispatch and optional CPU fallback behavior.
dbeta_opencl( x, shape1, shape2, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) dnbeta_opencl( x, shape1, shape2, ncp, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) pbeta_opencl( q, shape1, shape2, ncp = 0, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qbeta_opencl( p, shape1, shape2, ncp = 0, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) rbeta_opencl(n, shape1, shape2, fallback = FALSE, verbose = FALSE)dbeta_opencl( x, shape1, shape2, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) dnbeta_opencl( x, shape1, shape2, ncp, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) pbeta_opencl( q, shape1, shape2, ncp = 0, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qbeta_opencl( p, shape1, shape2, ncp = 0, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) rbeta_opencl(n, shape1, shape2, fallback = FALSE, verbose = FALSE)
x |
|
shape1 |
First shape parameter (must be > 0). |
shape2 |
Second shape parameter (must be > 0). |
log |
|
opencl_parallel |
Dispatch hint |
fallback |
When |
verbose |
Logical; print fallback/error diagnostics. |
ncp |
Non-centrality parameter (must be >= 0). Used by
|
q |
Numeric vector of quantiles for |
lower.tail, log.p
|
Tail/log-p inputs ( |
p |
Numeric vector of probabilities for |
n |
Number of observations (non-negative integer scalar). Used only by |
Numeric vector of length n.
qbeta_opencl with ncp > 0 may stress devices.
Rf_lbeta linkage breaks on GPUs; skip GPU demos.
Tracker: ‘inst/OPENCL_KERNEL_KNOWN_FAILURES.md’
n <- 5L if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { dbeta_opencl(rep(0.6, n), shape1 = 2.5, shape2 = 4, fallback = FALSE, verbose = TRUE) dnbeta_opencl(rep(0.6, n), shape1 = 2.5, shape2 = 4, ncp = 0.8, fallback = FALSE, verbose = TRUE) pbeta_opencl(q = 0.6, shape1 = 2.5, shape2 = 4, ncp = 0, fallback = FALSE, verbose = TRUE) ## qbeta_opencl: disabled — see inst/OPENCL_KERNEL_KNOWN_FAILURES.md # qbeta_opencl(rep(0.8, n), shape1 = 2.5, shape2 = 4, ncp = 0, fallback = FALSE, verbose = TRUE) rbeta_opencl(n, shape1 = 2.5, shape2 = 4, fallback = FALSE, verbose = TRUE) } else { stats::dbeta(rep(0.6, n), shape1 = 2.5, shape2 = 4) stats::pbeta(0.6, shape1 = 2.5, shape2 = 4, ncp = 0) stats::rbeta(n, shape1 = 2.5, shape2 = 4) }n <- 5L if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { dbeta_opencl(rep(0.6, n), shape1 = 2.5, shape2 = 4, fallback = FALSE, verbose = TRUE) dnbeta_opencl(rep(0.6, n), shape1 = 2.5, shape2 = 4, ncp = 0.8, fallback = FALSE, verbose = TRUE) pbeta_opencl(q = 0.6, shape1 = 2.5, shape2 = 4, ncp = 0, fallback = FALSE, verbose = TRUE) ## qbeta_opencl: disabled — see inst/OPENCL_KERNEL_KNOWN_FAILURES.md # qbeta_opencl(rep(0.8, n), shape1 = 2.5, shape2 = 4, ncp = 0, fallback = FALSE, verbose = TRUE) rbeta_opencl(n, shape1 = 2.5, shape2 = 4, fallback = FALSE, verbose = TRUE) } else { stats::dbeta(rep(0.6, n), shape1 = 2.5, shape2 = 4) stats::pbeta(0.6, shape1 = 2.5, shape2 = 4, ncp = 0) stats::rbeta(n, shape1 = 2.5, shape2 = 4) }
OpenCL-backed density, distribution, quantile, and random generation wrappers for the binomial distribution.
dbinom_raw_opencl( x, size, prob, qprob = NULL, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) dbinom_opencl( x, size, prob, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) pbinom_opencl( q, size, prob, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qbinom_opencl( p, size, prob, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) rbinom_opencl(n, size, prob, fallback = FALSE, verbose = FALSE)dbinom_raw_opencl( x, size, prob, qprob = NULL, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) dbinom_opencl( x, size, prob, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) pbinom_opencl( q, size, prob, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qbinom_opencl( p, size, prob, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) rbinom_opencl(n, size, prob, fallback = FALSE, verbose = FALSE)
x |
Numeric scalar quantile. |
size |
Number of trials (must be >= 0). |
prob |
Probability of success in |
qprob |
Complementary probability. If |
log |
|
opencl_parallel |
Dispatch hint |
fallback |
When |
verbose |
Logical; print fallback/error diagnostics. |
q |
Numeric vector of quantiles for |
lower.tail, log.p
|
Tail/log-p inputs ( |
p |
Probabilities for |
n |
Number of observations (non-negative integer scalar). Used only by |
Numeric vector result from the corresponding binomial-family operation.
n <- 5L if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { dbinom_raw_opencl(rep(6, n), size = 10, prob = 0.3, fallback = FALSE, verbose = TRUE) dbinom_opencl(rep(6, n), size = 10, prob = 0.3, fallback = FALSE, verbose = TRUE) pbinom_opencl(q = 6, size = 10, prob = 0.3, fallback = FALSE, verbose = TRUE) qbinom_opencl(rep(0.8, n), size = 10, prob = 0.3, fallback = FALSE, verbose = TRUE) rbinom_opencl(n, size = 10, prob = 0.3, fallback = FALSE, verbose = TRUE) } else { stats::dbinom(rep(6, n), size = 10, prob = 0.3) stats::dbinom(rep(6, n), size = 10, prob = 0.3) stats::pbinom(6, size = 10, prob = 0.3) stats::qbinom(rep(0.8, n), size = 10, prob = 0.3) stats::rbinom(n, size = 10, prob = 0.3) }n <- 5L if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { dbinom_raw_opencl(rep(6, n), size = 10, prob = 0.3, fallback = FALSE, verbose = TRUE) dbinom_opencl(rep(6, n), size = 10, prob = 0.3, fallback = FALSE, verbose = TRUE) pbinom_opencl(q = 6, size = 10, prob = 0.3, fallback = FALSE, verbose = TRUE) qbinom_opencl(rep(0.8, n), size = 10, prob = 0.3, fallback = FALSE, verbose = TRUE) rbinom_opencl(n, size = 10, prob = 0.3, fallback = FALSE, verbose = TRUE) } else { stats::dbinom(rep(6, n), size = 10, prob = 0.3) stats::dbinom(rep(6, n), size = 10, prob = 0.3) stats::pbinom(6, size = 10, prob = 0.3) stats::qbinom(rep(0.8, n), size = 10, prob = 0.3) stats::rbinom(n, size = 10, prob = 0.3) }
OpenCL-backed density, distribution, quantile, and random generation wrappers for the Cauchy distribution.
dcauchy_opencl( x, location = 0, scale = 1, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) pcauchy_opencl( q, location = 0, scale = 1, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qcauchy_opencl( p, location = 0, scale = 1, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) rcauchy_opencl(n, location = 0, scale = 1, fallback = FALSE, verbose = FALSE)dcauchy_opencl( x, location = 0, scale = 1, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) pcauchy_opencl( q, location = 0, scale = 1, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qcauchy_opencl( p, location = 0, scale = 1, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) rcauchy_opencl(n, location = 0, scale = 1, fallback = FALSE, verbose = FALSE)
x |
Numeric scalar quantile. |
location |
Location parameter. |
scale |
Scale parameter (must be > 0). |
log |
|
opencl_parallel |
Dispatch hint |
fallback |
When |
verbose |
Logical; print fallback/error diagnostics. |
q |
Quantiles ( |
lower.tail, log.p
|
Tail/log-p inputs ( |
p |
Numeric vector of probabilities for |
n |
Number of observations (non-negative integer scalar). Used only by |
Numeric vector result from the corresponding Cauchy-family operation.
n <- 5L if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { dcauchy_opencl(rep(0.2, n), location = 0, scale = 1, fallback = FALSE, verbose = TRUE) pcauchy_opencl(q = 0.2, location = 0, scale = 1, fallback = FALSE, verbose = TRUE) qcauchy_opencl(rep(0.8, n), location = 0, scale = 1, fallback = FALSE, verbose = TRUE) rcauchy_opencl(n, location = 0, scale = 1, fallback = FALSE, verbose = TRUE) } else { stats::dcauchy(rep(0.2, n), location = 0, scale = 1) stats::pcauchy(0.2, location = 0, scale = 1) stats::qcauchy(rep(0.8, n), location = 0, scale = 1) stats::rcauchy(n, location = 0, scale = 1) }n <- 5L if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { dcauchy_opencl(rep(0.2, n), location = 0, scale = 1, fallback = FALSE, verbose = TRUE) pcauchy_opencl(q = 0.2, location = 0, scale = 1, fallback = FALSE, verbose = TRUE) qcauchy_opencl(rep(0.8, n), location = 0, scale = 1, fallback = FALSE, verbose = TRUE) rcauchy_opencl(n, location = 0, scale = 1, fallback = FALSE, verbose = TRUE) } else { stats::dcauchy(rep(0.2, n), location = 0, scale = 1) stats::pcauchy(0.2, location = 0, scale = 1) stats::qcauchy(rep(0.8, n), location = 0, scale = 1) stats::rcauchy(n, location = 0, scale = 1) }
OpenCL-backed density, distribution, quantile, and random generation wrappers
for the chi-squared distribution, including non-central paths via ncp.
dchisq_opencl( x, df, ncp = 0, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) pchisq_opencl( q, df, ncp = 0, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qchisq_opencl( p, df, ncp = 0, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) rchisq_opencl(n, df, ncp = 0, fallback = FALSE, verbose = FALSE)dchisq_opencl( x, df, ncp = 0, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) pchisq_opencl( q, df, ncp = 0, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qchisq_opencl( p, df, ncp = 0, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) rchisq_opencl(n, df, ncp = 0, fallback = FALSE, verbose = FALSE)
x |
Numeric vector of quantiles for |
df |
Degrees of freedom (must be > 0). |
ncp |
Non-centrality parameter (must be >= 0). |
log |
|
opencl_parallel |
Dispatch hint |
fallback |
When |
verbose |
Logical; print fallback/error diagnostics. |
q |
Numeric vector of quantiles for |
lower.tail, log.p
|
Tail/log-p inputs ( |
p |
Numeric vector of probabilities for |
n |
Number of observations (non-negative integer scalar). Used only by |
Numeric vector of length n.
n <- 5L if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { dchisq_opencl(rep(4.5, n), df = 6, ncp = 0, fallback = FALSE, verbose = TRUE) pchisq_opencl(q = 4.5, df = 6, ncp = 0, fallback = FALSE, verbose = TRUE) qchisq_opencl(rep(0.8, n), df = 6, ncp = 0, fallback = FALSE, verbose = TRUE) rchisq_opencl(n, df = 6, ncp = 0, fallback = FALSE, verbose = TRUE) } else { stats::dchisq(rep(4.5, n), df = 6, ncp = 0) stats::pchisq(4.5, df = 6, ncp = 0) stats::qchisq(rep(0.8, n), df = 6, ncp = 0) stats::rchisq(n, df = 6, ncp = 0) }n <- 5L if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { dchisq_opencl(rep(4.5, n), df = 6, ncp = 0, fallback = FALSE, verbose = TRUE) pchisq_opencl(q = 4.5, df = 6, ncp = 0, fallback = FALSE, verbose = TRUE) qchisq_opencl(rep(0.8, n), df = 6, ncp = 0, fallback = FALSE, verbose = TRUE) rchisq_opencl(n, df = 6, ncp = 0, fallback = FALSE, verbose = TRUE) } else { stats::dchisq(rep(4.5, n), df = 6, ncp = 0) stats::pchisq(4.5, df = 6, ncp = 0) stats::qchisq(rep(0.8, n), df = 6, ncp = 0) stats::rchisq(n, df = 6, ncp = 0) }
OpenCL-backed density, distribution, quantile, and random generation wrappers for the exponential distribution.
dexp_opencl( x, rate = 1, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) pexp_opencl( q, rate = 1, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qexp_opencl( p, rate = 1, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) rexp_opencl(n, rate = 1, fallback = FALSE, verbose = FALSE)dexp_opencl( x, rate = 1, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) pexp_opencl( q, rate = 1, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qexp_opencl( p, rate = 1, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) rexp_opencl(n, rate = 1, fallback = FALSE, verbose = FALSE)
x |
Numeric scalar quantile. |
rate |
Rate parameter (must be > 0). |
log |
|
opencl_parallel |
Dispatch hint |
fallback |
When |
verbose |
Logical; print fallback/error diagnostics. |
q |
Numeric vector of quantiles for |
lower.tail, log.p
|
Tail/log-p inputs ( |
p |
Numeric vector of probabilities for |
n |
Number of observations (non-negative integer scalar). Used only by |
Numeric vector result from the corresponding exponential-family operation.
n <- 5L if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { dexp_opencl(rep(1.2, n), rate = 1, fallback = FALSE, verbose = TRUE) pexp_opencl(q = 1.2, rate = 1, fallback = FALSE, verbose = TRUE) qexp_opencl(rep(0.8, n), rate = 1, fallback = FALSE, verbose = TRUE) rexp_opencl(n, rate = 1, fallback = FALSE, verbose = TRUE) } else { stats::dexp(rep(1.2, n), rate = 1) stats::pexp(1.2, rate = 1) stats::qexp(rep(0.8, n), rate = 1) stats::rexp(n, rate = 1) }n <- 5L if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { dexp_opencl(rep(1.2, n), rate = 1, fallback = FALSE, verbose = TRUE) pexp_opencl(q = 1.2, rate = 1, fallback = FALSE, verbose = TRUE) qexp_opencl(rep(0.8, n), rate = 1, fallback = FALSE, verbose = TRUE) rexp_opencl(n, rate = 1, fallback = FALSE, verbose = TRUE) } else { stats::dexp(rep(1.2, n), rate = 1) stats::pexp(1.2, rate = 1) stats::qexp(rep(0.8, n), rate = 1) stats::rexp(n, rate = 1) }
OpenCL-backed density, distribution, quantile, and random generation wrappers for the F distribution.
df_opencl( x, df1, df2, ncp = 0, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) pf_opencl( q, df1, df2, ncp = 0, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qf_opencl( p, df1, df2, ncp = 0, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) rf_opencl(n, df1, df2, fallback = FALSE, verbose = FALSE)df_opencl( x, df1, df2, ncp = 0, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) pf_opencl( q, df1, df2, ncp = 0, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qf_opencl( p, df1, df2, ncp = 0, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) rf_opencl(n, df1, df2, fallback = FALSE, verbose = FALSE)
x |
Numeric vector of quantiles ( |
df1 |
Numerator degrees of freedom (must be > 0). |
df2 |
Denominator degrees of freedom (must be > 0). |
ncp |
Non-centrality parameter (must be >= 0). Used by
|
log |
|
opencl_parallel |
Dispatch hint |
fallback |
When |
verbose |
Logical; print fallback/error diagnostics. |
q |
Numeric vector of quantiles ( |
lower.tail, log.p
|
Tail/log-p inputs ( |
p |
Numeric vector of probabilities for |
n |
Number of observations (non-negative integer scalar). Used only by |
For df_opencl, qf_opencl, rf_opencl: numeric vector result.
For pf_opencl: numeric vector of recycled length (see stats::pf).
qf_opencl() can fail on some GPU/driver combinations with
CL_OUT_OF_RESOURCES. This has been observed in both central and
non-central settings, with non-central paths typically more fragile.
n <- 5L if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { df_opencl(rep(2, n), df1 = 5, df2 = 9, ncp = 0, fallback = FALSE, verbose = TRUE) df_opencl(rep(2, n), df1 = 5, df2 = 9, ncp = 1.1, fallback = FALSE, verbose = TRUE) pf_opencl(q = 2, df1 = 5, df2 = 9, ncp = 0, fallback = FALSE, verbose = TRUE) pf_opencl(q = 2, df1 = 5, df2 = 9, ncp = 1.1, fallback = FALSE, verbose = TRUE) rf_opencl(n, df1 = 5, df2 = 9, fallback = FALSE, verbose = TRUE) ## qf_opencl: disabled — see inst/OPENCL_KERNEL_KNOWN_FAILURES.md } else { stats::df(rep(2, n), df1 = 5, df2 = 9, ncp = 0) stats::df(rep(2, n), df1 = 5, df2 = 9, ncp = 1.1) stats::pf(2, df1 = 5, df2 = 9, ncp = 0) stats::pf(2, df1 = 5, df2 = 9, ncp = 1.1) stats::rf(n, df1 = 5, df2 = 9) }n <- 5L if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { df_opencl(rep(2, n), df1 = 5, df2 = 9, ncp = 0, fallback = FALSE, verbose = TRUE) df_opencl(rep(2, n), df1 = 5, df2 = 9, ncp = 1.1, fallback = FALSE, verbose = TRUE) pf_opencl(q = 2, df1 = 5, df2 = 9, ncp = 0, fallback = FALSE, verbose = TRUE) pf_opencl(q = 2, df1 = 5, df2 = 9, ncp = 1.1, fallback = FALSE, verbose = TRUE) rf_opencl(n, df1 = 5, df2 = 9, fallback = FALSE, verbose = TRUE) ## qf_opencl: disabled — see inst/OPENCL_KERNEL_KNOWN_FAILURES.md } else { stats::df(rep(2, n), df1 = 5, df2 = 9, ncp = 0) stats::df(rep(2, n), df1 = 5, df2 = 9, ncp = 1.1) stats::pf(2, df1 = 5, df2 = 9, ncp = 0) stats::pf(2, df1 = 5, df2 = 9, ncp = 1.1) stats::rf(n, df1 = 5, df2 = 9) }
OpenCL-backed density, distribution, quantile, and random generation wrappers
for the gamma distribution. These mirror the base stats gamma family
while adding OpenCL dispatch and optional CPU fallback behavior.
dgamma_opencl( x, shape, scale = 1, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) pgamma_opencl( q, shape, rate = 1, scale = 1/rate, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qgamma_opencl( p, shape, scale = 1, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) rgamma_opencl(n, shape, scale = 1, fallback = FALSE, verbose = FALSE)dgamma_opencl( x, shape, scale = 1, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) pgamma_opencl( q, shape, rate = 1, scale = 1/rate, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qgamma_opencl( p, shape, scale = 1, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) rgamma_opencl(n, shape, scale = 1, fallback = FALSE, verbose = FALSE)
x |
Numeric vector of quantiles for |
shape |
Shape parameter (must be > 0). |
scale |
Scale parameter (must be > 0). For |
log |
|
opencl_parallel |
Dispatch hint |
fallback |
When |
verbose |
Logical; print informational fallback messages. |
q |
Numeric vector of quantiles for |
rate |
Optional rate for |
lower.tail, log.p
|
Tail/log-p inputs ( |
p |
Numeric vector of probabilities for |
n |
Number of observations (non-negative integer scalar). Used only by |
pgamma_opencl follows pgamma argument names and
rate/scale handling (including the error when both are supplied
inconsistently). Recycling of q, shape, and scale follows
stats::pgamma. Vector lower.tail and log.p are recycled
row-wise with those arguments (like pnorm_opencl); a single-vector
stats::pgamma() call does not apply tail flags element-wise.
There is no leading n argument for pgamma_opencl.
On the GPU path each recycled row runs pgamma_kernel
once with n_out = 1. Missing or non-finite values after recycling, or non-positive
shape/scale, use row-wise stats::pgamma.
Numeric vector result from the corresponding gamma-family operation.
Compilation of qgamma_kernel can fail (ptxas: unresolved stirlerr_cycle_free).
Runnable examples omit GPU qgamma_opencl until resolved.
See ‘inst/OPENCL_KERNEL_KNOWN_FAILURES.md’.
n <- 5L if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { dgamma_opencl(rep(1.2, n), shape = 2, scale = 1, fallback = FALSE, verbose = TRUE) pgamma_opencl(q = 1.2, shape = 2, scale = 1, fallback = FALSE, verbose = TRUE) ## qgamma_opencl: disabled — see inst/OPENCL_KERNEL_KNOWN_FAILURES.md # qgamma_opencl(rep(0.8, n), shape = 2, scale = 1, fallback = FALSE, verbose = TRUE) rgamma_opencl(n, shape = 2, scale = 1, fallback = FALSE, verbose = TRUE) } else { stats::dgamma(rep(1.2, n), shape = 2, scale = 1) stats::pgamma(1.2, shape = 2, scale = 1) stats::rgamma(n, shape = 2, scale = 1) }n <- 5L if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { dgamma_opencl(rep(1.2, n), shape = 2, scale = 1, fallback = FALSE, verbose = TRUE) pgamma_opencl(q = 1.2, shape = 2, scale = 1, fallback = FALSE, verbose = TRUE) ## qgamma_opencl: disabled — see inst/OPENCL_KERNEL_KNOWN_FAILURES.md # qgamma_opencl(rep(0.8, n), shape = 2, scale = 1, fallback = FALSE, verbose = TRUE) rgamma_opencl(n, shape = 2, scale = 1, fallback = FALSE, verbose = TRUE) } else { stats::dgamma(rep(1.2, n), shape = 2, scale = 1) stats::pgamma(1.2, shape = 2, scale = 1) stats::rgamma(n, shape = 2, scale = 1) }
OpenCL-backed density, distribution, quantile, and random generation wrappers for the geometric distribution.
dgeom_opencl( x, prob, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) pgeom_opencl( q, prob, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qgeom_opencl( p, prob, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) rgeom_opencl(n, prob, fallback = FALSE, verbose = FALSE)dgeom_opencl( x, prob, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) pgeom_opencl( q, prob, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qgeom_opencl( p, prob, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) rgeom_opencl(n, prob, fallback = FALSE, verbose = FALSE)
x |
Numeric scalar quantile. |
prob |
Probability of success in |
log |
|
opencl_parallel |
Dispatch hint |
fallback |
When |
verbose |
Logical; print fallback/error diagnostics. |
q |
Numeric vector of quantiles for |
lower.tail, log.p
|
Tail/log-p inputs ( |
p |
Numeric vector of probabilities for |
n |
Number of observations (non-negative integer scalar). Used only by |
Numeric vector result from the corresponding geometric-family operation.
n <- 5L if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { dgeom_opencl(rep(4, n), prob = 0.3, fallback = FALSE, verbose = TRUE) pgeom_opencl(q = 4, prob = 0.3, fallback = FALSE, verbose = TRUE) qgeom_opencl(rep(0.8, n), prob = 0.3, fallback = FALSE, verbose = TRUE) rgeom_opencl(n, prob = 0.3, fallback = FALSE, verbose = TRUE) } else { stats::dgeom(rep(4, n), prob = 0.3) stats::pgeom(4, prob = 0.3) stats::qgeom(rep(0.8, n), prob = 0.3) stats::rgeom(n, prob = 0.3) }n <- 5L if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { dgeom_opencl(rep(4, n), prob = 0.3, fallback = FALSE, verbose = TRUE) pgeom_opencl(q = 4, prob = 0.3, fallback = FALSE, verbose = TRUE) qgeom_opencl(rep(0.8, n), prob = 0.3, fallback = FALSE, verbose = TRUE) rgeom_opencl(n, prob = 0.3, fallback = FALSE, verbose = TRUE) } else { stats::dgeom(rep(4, n), prob = 0.3) stats::pgeom(4, prob = 0.3) stats::qgeom(rep(0.8, n), prob = 0.3) stats::rgeom(n, prob = 0.3) }
OpenCL-backed density, distribution, quantile, and random generation wrappers for the hypergeometric distribution.
dhyper_opencl( x, m, n_black, k, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) phyper_opencl( q, m, n_black, k, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qhyper_opencl( p, m, n_black, k, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) rhyper_opencl(n, m, n_black, k, fallback = FALSE, verbose = FALSE)dhyper_opencl( x, m, n_black, k, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) phyper_opencl( q, m, n_black, k, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qhyper_opencl( p, m, n_black, k, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) rhyper_opencl(n, m, n_black, k, fallback = FALSE, verbose = FALSE)
x |
Numeric scalar quantile. |
m |
Number of white balls in the urn (must be >= 0). |
n_black |
Number of black balls in the urn (must be >= 0). |
k |
Number of draws (must be >= 0). |
log |
|
opencl_parallel |
Dispatch hint |
fallback |
When |
verbose |
Logical; print fallback/error diagnostics. |
q |
Numeric vector of quantiles for |
lower.tail, log.p
|
Tail/log-p inputs ( |
p |
Numeric vector of probabilities for |
n |
Number of observations (non-negative integer scalar). Used only by |
Numeric vector result from the corresponding hypergeometric-family operation.
n <- 5L if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { dhyper_opencl(rep(3, n), m = 10, n_black = 12, k = 8, fallback = FALSE, verbose = TRUE) phyper_opencl(q = 3, m = 10, n_black = 12, k = 8, fallback = FALSE, verbose = TRUE) qhyper_opencl(rep(0.8, n), m = 10, n_black = 12, k = 8, fallback = FALSE, verbose = TRUE) rhyper_opencl(n, m = 10, n_black = 12, k = 8, fallback = FALSE, verbose = TRUE) } else { stats::dhyper(rep(3, n), m = 10, n = 12, k = 8) stats::phyper(3, m = 10, n = 12, k = 8) stats::qhyper(rep(0.8, n), m = 10, n = 12, k = 8) stats::rhyper(n, m = 10, n = 12, k = 8) }n <- 5L if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { dhyper_opencl(rep(3, n), m = 10, n_black = 12, k = 8, fallback = FALSE, verbose = TRUE) phyper_opencl(q = 3, m = 10, n_black = 12, k = 8, fallback = FALSE, verbose = TRUE) qhyper_opencl(rep(0.8, n), m = 10, n_black = 12, k = 8, fallback = FALSE, verbose = TRUE) rhyper_opencl(n, m = 10, n_black = 12, k = 8, fallback = FALSE, verbose = TRUE) } else { stats::dhyper(rep(3, n), m = 10, n = 12, k = 8) stats::phyper(3, m = 10, n = 12, k = 8) stats::qhyper(rep(0.8, n), m = 10, n = 12, k = 8) stats::rhyper(n, m = 10, n = 12, k = 8) }
OpenCL-backed density, distribution, quantile, and random generation wrappers for the lognormal distribution.
dlnorm_opencl( x, meanlog = 0, sdlog = 1, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) plnorm_opencl( q, meanlog = 0, sdlog = 1, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qlnorm_opencl( p, meanlog = 0, sdlog = 1, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) rlnorm_opencl(n, meanlog = 0, sdlog = 1, fallback = FALSE, verbose = FALSE)dlnorm_opencl( x, meanlog = 0, sdlog = 1, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) plnorm_opencl( q, meanlog = 0, sdlog = 1, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qlnorm_opencl( p, meanlog = 0, sdlog = 1, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) rlnorm_opencl(n, meanlog = 0, sdlog = 1, fallback = FALSE, verbose = FALSE)
x |
Numeric scalar quantile (must be >= 0). |
meanlog |
Mean of the distribution on the log scale. |
sdlog |
Standard deviation on the log scale (must be > 0). |
log |
|
opencl_parallel |
Dispatch hint |
fallback |
When |
verbose |
Logical; print fallback/error diagnostics. |
q |
Numeric vector of quantiles for |
lower.tail, log.p
|
Tail/log-p inputs ( |
p |
Numeric vector of probabilities for |
n |
Number of observations (non-negative integer scalar). Used only by |
Numeric vector of length n.
n <- 5L if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { dlnorm_opencl(rep(1.2, n), meanlog = 0.1, sdlog = 0.8, fallback = FALSE, verbose = TRUE) plnorm_opencl(q = 1.2, meanlog = 0.1, sdlog = 0.8, fallback = FALSE, verbose = TRUE) qlnorm_opencl(rep(0.8, n), meanlog = 0.1, sdlog = 0.8, fallback = FALSE, verbose = TRUE) rlnorm_opencl(n, meanlog = 0.1, sdlog = 0.8, fallback = FALSE, verbose = TRUE) } else { stats::dlnorm(rep(1.2, n), meanlog = 0.1, sdlog = 0.8) stats::plnorm(1.2, meanlog = 0.1, sdlog = 0.8) stats::qlnorm(rep(0.8, n), meanlog = 0.1, sdlog = 0.8) stats::rlnorm(n, meanlog = 0.1, sdlog = 0.8) }n <- 5L if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { dlnorm_opencl(rep(1.2, n), meanlog = 0.1, sdlog = 0.8, fallback = FALSE, verbose = TRUE) plnorm_opencl(q = 1.2, meanlog = 0.1, sdlog = 0.8, fallback = FALSE, verbose = TRUE) qlnorm_opencl(rep(0.8, n), meanlog = 0.1, sdlog = 0.8, fallback = FALSE, verbose = TRUE) rlnorm_opencl(n, meanlog = 0.1, sdlog = 0.8, fallback = FALSE, verbose = TRUE) } else { stats::dlnorm(rep(1.2, n), meanlog = 0.1, sdlog = 0.8) stats::plnorm(1.2, meanlog = 0.1, sdlog = 0.8) stats::qlnorm(rep(0.8, n), meanlog = 0.1, sdlog = 0.8) stats::rlnorm(n, meanlog = 0.1, sdlog = 0.8) }
OpenCL-backed density, distribution, quantile, and random generation wrappers for the logistic distribution.
dlogis_opencl( x, location = 0, scale = 1, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) plogis_opencl( q, location = 0, scale = 1, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qlogis_opencl( p, location = 0, scale = 1, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) rlogis_opencl(n, location = 0, scale = 1, fallback = FALSE, verbose = FALSE)dlogis_opencl( x, location = 0, scale = 1, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) plogis_opencl( q, location = 0, scale = 1, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qlogis_opencl( p, location = 0, scale = 1, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) rlogis_opencl(n, location = 0, scale = 1, fallback = FALSE, verbose = FALSE)
x |
Numeric scalar quantile. |
location |
Location parameter. |
scale |
Scale parameter (must be > 0). |
log |
|
opencl_parallel |
Dispatch hint ( |
fallback |
When |
verbose |
Logical; print fallback/error diagnostics. |
q |
Numeric vector of quantiles for |
lower.tail, log.p
|
Tail/log-p inputs ( |
p |
Numeric vector of probabilities for |
n |
Number of observations (non-negative integer scalar). Used only by |
Numeric vector result from the corresponding logistic-family operation.
Some platforms fail to link qlogis_kernel (ptxas unresolved Rf_qlogis).
Runnable examples omit GPU qlogis_opencl until resolved.
See ‘inst/OPENCL_KERNEL_KNOWN_FAILURES.md’.
n <- 5L if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { dlogis_opencl(rep(0.2, n), location = 0, scale = 1, fallback = FALSE, verbose = TRUE) plogis_opencl(q = 0.2, location = 0, scale = 1, fallback = FALSE, verbose = TRUE) ## qlogis_opencl: disabled — see inst/OPENCL_KERNEL_KNOWN_FAILURES.md rlogis_opencl(n, location = 0, scale = 1, fallback = FALSE, verbose = TRUE) } else { stats::dlogis(rep(0.2, n), location = 0, scale = 1) stats::plogis(0.2, location = 0, scale = 1) stats::rlogis(n, location = 0, scale = 1) }n <- 5L if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { dlogis_opencl(rep(0.2, n), location = 0, scale = 1, fallback = FALSE, verbose = TRUE) plogis_opencl(q = 0.2, location = 0, scale = 1, fallback = FALSE, verbose = TRUE) ## qlogis_opencl: disabled — see inst/OPENCL_KERNEL_KNOWN_FAILURES.md rlogis_opencl(n, location = 0, scale = 1, fallback = FALSE, verbose = TRUE) } else { stats::dlogis(rep(0.2, n), location = 0, scale = 1) stats::plogis(0.2, location = 0, scale = 1) stats::rlogis(n, location = 0, scale = 1) }
OpenCL-backed density, distribution, quantile, and random generation wrappers
for the negative binomial distribution, with variants parameterized by
prob and by mu.
dnbinom_opencl( x, size, prob, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) pnbinom_opencl( q, size, prob, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qnbinom_opencl( p, size, prob, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) rnbinom_opencl(n, size, prob, fallback = FALSE, verbose = FALSE) dnbinom_mu_opencl( x, size, mu, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) pnbinom_mu_opencl( q, size, mu, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qnbinom_mu_opencl( p, size, mu, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) rnbinom_mu_opencl(n, size, mu, fallback = FALSE, verbose = FALSE)dnbinom_opencl( x, size, prob, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) pnbinom_opencl( q, size, prob, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qnbinom_opencl( p, size, prob, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) rnbinom_opencl(n, size, prob, fallback = FALSE, verbose = FALSE) dnbinom_mu_opencl( x, size, mu, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) pnbinom_mu_opencl( q, size, mu, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qnbinom_mu_opencl( p, size, mu, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) rnbinom_mu_opencl(n, size, mu, fallback = FALSE, verbose = FALSE)
x |
Numeric scalar quantile (must be >= 0). |
size |
Dispersion/size parameter (must be >= 0). |
prob |
Probability of success in |
log |
|
opencl_parallel |
Dispatch hint |
fallback |
When |
verbose |
Logical; print fallback/error diagnostics. |
q |
Quantiles for |
lower.tail, log.p
|
Tail/log-p inputs ( |
p |
Probabilities for |
n |
Observations scalar; used by negative-binomial |
mu |
Mean parameter (must be >= 0). |
Numeric vector result from the corresponding negative-binomial operation.
n <- 5L if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { dnbinom_opencl(rep(4, n), size = 7, prob = 0.4, fallback = FALSE, verbose = TRUE) pnbinom_opencl(q = 4, size = 7, prob = 0.4, fallback = FALSE, verbose = TRUE) qnbinom_opencl(rep(0.8, n), size = 7, prob = 0.4, fallback = FALSE, verbose = TRUE) rnbinom_opencl(n, size = 7, prob = 0.4, fallback = FALSE, verbose = TRUE) dnbinom_mu_opencl(rep(4, n), size = 7, mu = 5, fallback = FALSE, verbose = TRUE) pnbinom_mu_opencl(q = 4, size = 7, mu = 5, fallback = FALSE, verbose = TRUE) qnbinom_mu_opencl(rep(0.8, n), size = 7, mu = 5, fallback = FALSE, verbose = TRUE) rnbinom_mu_opencl(n, size = 7, mu = 5, fallback = FALSE, verbose = TRUE) } else { stats::dnbinom(rep(4, n), size = 7, prob = 0.4) stats::pnbinom(4, size = 7, prob = 0.4) stats::qnbinom(rep(0.8, n), size = 7, prob = 0.4) stats::rnbinom(n, size = 7, prob = 0.4) stats::dnbinom(rep(4, n), size = 7, mu = 5) stats::pnbinom(4, size = 7, mu = 5) stats::qnbinom(rep(0.8, n), size = 7, mu = 5) stats::rnbinom(n, size = 7, mu = 5) }n <- 5L if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { dnbinom_opencl(rep(4, n), size = 7, prob = 0.4, fallback = FALSE, verbose = TRUE) pnbinom_opencl(q = 4, size = 7, prob = 0.4, fallback = FALSE, verbose = TRUE) qnbinom_opencl(rep(0.8, n), size = 7, prob = 0.4, fallback = FALSE, verbose = TRUE) rnbinom_opencl(n, size = 7, prob = 0.4, fallback = FALSE, verbose = TRUE) dnbinom_mu_opencl(rep(4, n), size = 7, mu = 5, fallback = FALSE, verbose = TRUE) pnbinom_mu_opencl(q = 4, size = 7, mu = 5, fallback = FALSE, verbose = TRUE) qnbinom_mu_opencl(rep(0.8, n), size = 7, mu = 5, fallback = FALSE, verbose = TRUE) rnbinom_mu_opencl(n, size = 7, mu = 5, fallback = FALSE, verbose = TRUE) } else { stats::dnbinom(rep(4, n), size = 7, prob = 0.4) stats::pnbinom(4, size = 7, prob = 0.4) stats::qnbinom(rep(0.8, n), size = 7, prob = 0.4) stats::rnbinom(n, size = 7, prob = 0.4) stats::dnbinom(rep(4, n), size = 7, mu = 5) stats::pnbinom(4, size = 7, mu = 5) stats::qnbinom(rep(0.8, n), size = 7, mu = 5) stats::rnbinom(n, size = 7, mu = 5) }
OpenCL-backed density, distribution, quantile, and random generation wrappers
for the normal distribution. These mirror the base stats normal family
while adding OpenCL dispatch and optional CPU fallback behavior.
dnorm_opencl( x, mean = 0, sd = 1, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) pnorm_opencl( q, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qnorm_opencl( p, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) rnorm_opencl(n, mean = 0, sd = 1, fallback = FALSE, verbose = FALSE)dnorm_opencl( x, mean = 0, sd = 1, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) pnorm_opencl( q, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qnorm_opencl( p, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) rnorm_opencl(n, mean = 0, sd = 1, fallback = FALSE, verbose = FALSE)
x |
Numeric vector of quantiles. |
mean |
Location parameter ( |
sd |
Scale parameter ( |
log |
|
opencl_parallel |
Dispatch hint |
fallback |
When |
verbose |
Logical; print informational fallback messages. |
q |
Numeric vector of quantiles for |
lower.tail, log.p
|
Recycling for |
p |
Numeric vector of probabilities for |
n |
Number of observations (non-negative integer scalar). Used only by |
pnorm_opencl matches pnorm on the first five statistical
arguments (q, mean, sd, lower.tail, log.p) and the
usual defaults. It also accepts opencl_parallel, fallback, and
verbose. Only rnorm_opencl uses a leading n.
Recycling follows pnorm once arguments are aligned. On the GPU path,
each recycled row calls the scalar pnorm_kernel once (len submissions).
Vector q, mean, sd, lower.tail, and log.p yield one
OpenCL evaluation per output index.
Length-zero q returns numeric(0) as in pnorm.
Missing or non-finite values (after recycling), or any sd == 0, use CPU
pnorm. Zero-length recycling errors and negative sd still error.
Numeric vector result from the corresponding normal-family operation.
n <- 5L x <- c(-1, 0, 1) if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { dnorm_opencl(x, mean = 0, sd = 1, fallback = FALSE, verbose = TRUE) pnorm_opencl(q = 0.2, mean = 0, sd = 1, fallback = FALSE, verbose = TRUE) qnorm_opencl(rep(0.8, n), mean = 0, sd = 1, fallback = FALSE, verbose = TRUE) rnorm_opencl(n, mean = 0, sd = 1, fallback = FALSE, verbose = TRUE) } else { stats::dnorm(x, mean = 0, sd = 1) stats::pnorm(0.2, mean = 0, sd = 1) stats::qnorm(rep(0.8, n), mean = 0, sd = 1) stats::rnorm(n, mean = 0, sd = 1) }n <- 5L x <- c(-1, 0, 1) if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { dnorm_opencl(x, mean = 0, sd = 1, fallback = FALSE, verbose = TRUE) pnorm_opencl(q = 0.2, mean = 0, sd = 1, fallback = FALSE, verbose = TRUE) qnorm_opencl(rep(0.8, n), mean = 0, sd = 1, fallback = FALSE, verbose = TRUE) rnorm_opencl(n, mean = 0, sd = 1, fallback = FALSE, verbose = TRUE) } else { stats::dnorm(x, mean = 0, sd = 1) stats::pnorm(0.2, mean = 0, sd = 1) stats::qnorm(rep(0.8, n), mean = 0, sd = 1) stats::rnorm(n, mean = 0, sd = 1) }
OpenCL-backed density, distribution, quantile, and random generation wrappers for the Poisson distribution.
dpois_raw_opencl( x, lambda, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) dpois_opencl( x, lambda, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) ppois_opencl( q, lambda, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qpois_opencl( p, lambda, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) rpois_opencl(n, lambda, fallback = FALSE, verbose = FALSE)dpois_raw_opencl( x, lambda, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) dpois_opencl( x, lambda, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) ppois_opencl( q, lambda, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qpois_opencl( p, lambda, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) rpois_opencl(n, lambda, fallback = FALSE, verbose = FALSE)
x |
Numeric scalar quantile (must be >= 0). |
lambda |
Mean/rate parameter (must be >= 0). |
log |
|
opencl_parallel |
Dispatch hint |
fallback |
When |
verbose |
Logical; print fallback/error diagnostics. |
q |
Numeric vector of quantiles for |
lower.tail, log.p
|
Tail/log-p inputs ( |
p |
Numeric vector of probabilities for |
n |
Number of observations (non-negative integer scalar). Used only by |
Numeric vector result from the corresponding Poisson-family operation.
n <- 5L if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { dpois_raw_opencl(rep(4, n), lambda = 4, fallback = FALSE, verbose = TRUE) dpois_opencl(rep(4, n), lambda = 4, fallback = FALSE, verbose = TRUE) ppois_opencl(q = 4, lambda = 4, fallback = FALSE, verbose = TRUE) qpois_opencl(rep(0.8, n), lambda = 4, fallback = FALSE, verbose = TRUE) rpois_opencl(n, lambda = 4, fallback = FALSE, verbose = TRUE) } else { stats::dpois(rep(4, n), lambda = 4) stats::dpois(rep(4, n), lambda = 4) stats::ppois(4, lambda = 4) stats::qpois(rep(0.8, n), lambda = 4) stats::rpois(n, lambda = 4) }n <- 5L if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { dpois_raw_opencl(rep(4, n), lambda = 4, fallback = FALSE, verbose = TRUE) dpois_opencl(rep(4, n), lambda = 4, fallback = FALSE, verbose = TRUE) ppois_opencl(q = 4, lambda = 4, fallback = FALSE, verbose = TRUE) qpois_opencl(rep(0.8, n), lambda = 4, fallback = FALSE, verbose = TRUE) rpois_opencl(n, lambda = 4, fallback = FALSE, verbose = TRUE) } else { stats::dpois(rep(4, n), lambda = 4) stats::dpois(rep(4, n), lambda = 4) stats::ppois(4, lambda = 4) stats::qpois(rep(0.8, n), lambda = 4) stats::rpois(n, lambda = 4) }
OpenCL-backed density, distribution, quantile, and random generation wrappers for the Wilcoxon signed rank distribution.
dsignrank_opencl( x, nsize, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) psignrank_opencl( q, nsize, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qsignrank_opencl( p, nsize, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) rsignrank_opencl(n, nsize, fallback = FALSE, verbose = FALSE)dsignrank_opencl( x, nsize, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) psignrank_opencl( q, nsize, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qsignrank_opencl( p, nsize, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) rsignrank_opencl(n, nsize, fallback = FALSE, verbose = FALSE)
x |
Numeric scalar quantile (must be >= 0). |
nsize |
Number of observations used by signed-rank routines (must be > 0). |
log |
|
opencl_parallel |
Dispatch hint |
fallback |
When |
verbose |
Logical; print fallback/error diagnostics. |
q |
|
lower.tail, log.p
|
Tail/log-p inputs ( |
p |
|
n |
Draw-count scalar ( |
Numeric vector result from the corresponding signed-rank operation.
Signed-rank kernels can fail to build on some GPU toolchains due to unresolved
runtime allocation symbols (for example R_chk_calloc). Use
fallback = TRUE only if you need to tolerate those failures until
device-safe shims are complete.
if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { # Signed-rank OpenCL kernels are currently known to fail on some GPU stacks # due to unresolved runtime allocation symbols (e.g., R_chk_calloc). # Keeping these commented avoids flaky check failures: # n <- 5L # dsignrank_opencl(n, x = 6, nsize = 8, fallback = FALSE, verbose = TRUE) # psignrank_opencl(q = 6, nsize = 8, fallback = FALSE, verbose = TRUE) # qsignrank_opencl(rep(0.8, n), nsize = 8, fallback = FALSE, verbose = TRUE) # rsignrank_opencl(n, nsize = 8, fallback = FALSE, verbose = TRUE) } else { n <- 5L stats::dsignrank(rep(6, n), n = 8) stats::psignrank(6, n = 8) stats::qsignrank(rep(0.8, n), n = 8) stats::rsignrank(n, n = 8) }if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { # Signed-rank OpenCL kernels are currently known to fail on some GPU stacks # due to unresolved runtime allocation symbols (e.g., R_chk_calloc). # Keeping these commented avoids flaky check failures: # n <- 5L # dsignrank_opencl(n, x = 6, nsize = 8, fallback = FALSE, verbose = TRUE) # psignrank_opencl(q = 6, nsize = 8, fallback = FALSE, verbose = TRUE) # qsignrank_opencl(rep(0.8, n), nsize = 8, fallback = FALSE, verbose = TRUE) # rsignrank_opencl(n, nsize = 8, fallback = FALSE, verbose = TRUE) } else { n <- 5L stats::dsignrank(rep(6, n), n = 8) stats::psignrank(6, n = 8) stats::qsignrank(rep(0.8, n), n = 8) stats::rsignrank(n, n = 8) }
OpenCL-backed density, distribution, quantile, and random generation wrappers for the Student t distribution.
dt_opencl( x, df, ncp = 0, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) pt_opencl( q, df, ncp = 0, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qt_opencl( p, df, ncp = 0, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) rt_opencl(n, df, fallback = FALSE, verbose = FALSE)dt_opencl( x, df, ncp = 0, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) pt_opencl( q, df, ncp = 0, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qt_opencl( p, df, ncp = 0, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) rt_opencl(n, df, fallback = FALSE, verbose = FALSE)
x |
Numeric vector of quantiles ( |
df |
Degrees of freedom (must be > 0). |
ncp |
Non-centrality parameter. |
log |
|
opencl_parallel |
Dispatch hint |
fallback |
When |
verbose |
Logical; print fallback/error diagnostics. |
q |
Numeric vector of quantiles ( |
lower.tail, log.p
|
Tail/log-p inputs ( |
p |
Numeric vector of probabilities for |
n |
Number of observations (non-negative integer scalar). Used only by |
For dt_opencl, qt_opencl, rt_opencl: numeric vector result.
For pt_opencl: numeric vector of recycled length (see stats::pt).
Some platforms fail to link qnt_kernel (ptxas unresolved Rf_qnt).
Runnable examples omit GPU qt_opencl until resolved.
See ‘inst/OPENCL_KERNEL_KNOWN_FAILURES.md’.
n <- 5L if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { dt_opencl(rep(1.5, n), df = 8, ncp = 0, fallback = FALSE, verbose = TRUE) dt_opencl(rep(1.5, n), df = 8, ncp = 1.2, fallback = FALSE, verbose = TRUE) pt_opencl(q = 1.5, df = 8, ncp = 0, fallback = FALSE, verbose = TRUE) pt_opencl(q = 1.5, df = 8, ncp = 1.2, fallback = FALSE, verbose = TRUE) ## qt_opencl: disabled — see inst/OPENCL_KERNEL_KNOWN_FAILURES.md rt_opencl(n, df = 8, fallback = FALSE, verbose = TRUE) } else { stats::dt(rep(1.5, n), df = 8, ncp = 0) stats::dt(rep(1.5, n), df = 8, ncp = 1.2) stats::pt(1.5, df = 8, ncp = 0) stats::pt(1.5, df = 8, ncp = 1.2) stats::rt(n, df = 8) }n <- 5L if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { dt_opencl(rep(1.5, n), df = 8, ncp = 0, fallback = FALSE, verbose = TRUE) dt_opencl(rep(1.5, n), df = 8, ncp = 1.2, fallback = FALSE, verbose = TRUE) pt_opencl(q = 1.5, df = 8, ncp = 0, fallback = FALSE, verbose = TRUE) pt_opencl(q = 1.5, df = 8, ncp = 1.2, fallback = FALSE, verbose = TRUE) ## qt_opencl: disabled — see inst/OPENCL_KERNEL_KNOWN_FAILURES.md rt_opencl(n, df = 8, fallback = FALSE, verbose = TRUE) } else { stats::dt(rep(1.5, n), df = 8, ncp = 0) stats::dt(rep(1.5, n), df = 8, ncp = 1.2) stats::pt(1.5, df = 8, ncp = 0) stats::pt(1.5, df = 8, ncp = 1.2) stats::rt(n, df = 8) }
OpenCL-backed density, distribution, quantile, and random generation wrappers
for the uniform distribution. These mirror the base stats uniform
family while adding OpenCL dispatch and optional CPU fallback behavior.
dunif_opencl( x, min = 0, max = 1, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) punif_opencl( q, min = 0, max = 1, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qunif_opencl( p, min = 0, max = 1, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) runif_opencl(n, min = 0, max = 1, fallback = FALSE, verbose = FALSE)dunif_opencl( x, min = 0, max = 1, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) punif_opencl( q, min = 0, max = 1, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qunif_opencl( p, min = 0, max = 1, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) runif_opencl(n, min = 0, max = 1, fallback = FALSE, verbose = FALSE)
x |
Numeric scalar quantile for |
min |
Lower limit of the distribution. |
max |
Upper limit of the distribution. |
log |
|
opencl_parallel |
Dispatch hint |
fallback |
When |
verbose |
Logical; print informational fallback messages. |
q |
Numeric quantiles ( |
lower.tail, log.p
|
Tail/log-p inputs ( |
p |
Probabilities for |
n |
Draw count ( |
Numeric vector result from the corresponding uniform-family operation.
Some platforms fail to link qunif_kernel (ptxas unresolved Rf_qunif).
Runnable examples omit GPU qunif_opencl until resolved.
See ‘inst/OPENCL_KERNEL_KNOWN_FAILURES.md’.
n <- 5L if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { dunif_opencl(rep(0.4, n), min = 0, max = 1, fallback = FALSE, verbose = TRUE) punif_opencl(q = 0.4, min = 0, max = 1, fallback = FALSE, verbose = TRUE) ## qunif_opencl: disabled — see inst/OPENCL_KERNEL_KNOWN_FAILURES.md runif_opencl(n, min = 0, max = 1, fallback = FALSE, verbose = TRUE) } else { stats::dunif(rep(0.4, n), min = 0, max = 1) stats::punif(0.4, min = 0, max = 1) stats::runif(n, min = 0, max = 1) }n <- 5L if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { dunif_opencl(rep(0.4, n), min = 0, max = 1, fallback = FALSE, verbose = TRUE) punif_opencl(q = 0.4, min = 0, max = 1, fallback = FALSE, verbose = TRUE) ## qunif_opencl: disabled — see inst/OPENCL_KERNEL_KNOWN_FAILURES.md runif_opencl(n, min = 0, max = 1, fallback = FALSE, verbose = TRUE) } else { stats::dunif(rep(0.4, n), min = 0, max = 1) stats::punif(0.4, min = 0, max = 1) stats::runif(n, min = 0, max = 1) }
OpenCL-backed density, distribution, quantile, and random generation wrappers for the Weibull distribution.
dweibull_opencl( x, shape, scale = 1, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) pweibull_opencl( q, shape, scale = 1, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qweibull_opencl( p, shape, scale = 1, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) rweibull_opencl(n, shape, scale = 1, fallback = FALSE, verbose = FALSE)dweibull_opencl( x, shape, scale = 1, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) pweibull_opencl( q, shape, scale = 1, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qweibull_opencl( p, shape, scale = 1, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) rweibull_opencl(n, shape, scale = 1, fallback = FALSE, verbose = FALSE)
x |
Numeric scalar quantile (must be >= 0). |
shape |
Shape parameter (must be > 0). |
scale |
Scale parameter (must be > 0). |
log |
|
opencl_parallel |
Dispatch hint |
fallback |
When |
verbose |
Logical; print fallback/error diagnostics. |
q |
|
lower.tail, log.p
|
Tail/log-p inputs ( |
p |
Numeric vector of probabilities for |
n |
Number of observations (non-negative integer scalar). Used only by |
Numeric vector result from the corresponding Weibull-family operation.
n <- 5L if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { dweibull_opencl(rep(1.2, n), shape = 2, scale = 1.5, fallback = FALSE, verbose = TRUE) pweibull_opencl(q = 1.2, shape = 2, scale = 1.5, fallback = FALSE, verbose = TRUE) qweibull_opencl(rep(0.8, n), shape = 2, scale = 1.5, fallback = FALSE, verbose = TRUE) rweibull_opencl(n, shape = 2, scale = 1.5, fallback = FALSE, verbose = TRUE) } else { stats::dweibull(rep(1.2, n), shape = 2, scale = 1.5) stats::pweibull(1.2, shape = 2, scale = 1.5) stats::qweibull(rep(0.8, n), shape = 2, scale = 1.5) stats::rweibull(n, shape = 2, scale = 1.5) }n <- 5L if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { dweibull_opencl(rep(1.2, n), shape = 2, scale = 1.5, fallback = FALSE, verbose = TRUE) pweibull_opencl(q = 1.2, shape = 2, scale = 1.5, fallback = FALSE, verbose = TRUE) qweibull_opencl(rep(0.8, n), shape = 2, scale = 1.5, fallback = FALSE, verbose = TRUE) rweibull_opencl(n, shape = 2, scale = 1.5, fallback = FALSE, verbose = TRUE) } else { stats::dweibull(rep(1.2, n), shape = 2, scale = 1.5) stats::pweibull(1.2, shape = 2, scale = 1.5) stats::qweibull(rep(0.8, n), shape = 2, scale = 1.5) stats::rweibull(n, shape = 2, scale = 1.5) }
OpenCL-backed density, distribution, quantile, and random generation wrappers for the Wilcoxon rank sum distribution.
dwilcox_opencl( x, m, nn, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) pwilcox_opencl( q, m, nn, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qwilcox_opencl( p, m, nn, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) rwilcox_opencl(n, m, nn, fallback = FALSE, verbose = FALSE)dwilcox_opencl( x, m, nn, log = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) pwilcox_opencl( q, m, nn, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qwilcox_opencl( p, m, nn, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) rwilcox_opencl(n, m, nn, fallback = FALSE, verbose = FALSE)
x |
Numeric scalar quantile (must be >= 0). |
m |
Number of observations in one sample (must be > 0). |
nn |
Number of observations in the other sample (must be > 0). |
log |
|
opencl_parallel |
Dispatch hint |
fallback |
When |
verbose |
Logical; print fallback/error diagnostics. |
q |
|
lower.tail, log.p
|
Tail/log-p inputs ( |
p |
|
n |
Draw-count scalar ( |
Numeric vector result from the corresponding Wilcoxon-family operation.
Wilcoxon kernels can still hit runtime-shim gaps depending on device and
driver stack (for example unresolved runtime symbols in some builds).
Defaults follow fallback = FALSE: OpenCL failures surface unless you pass fallback = TRUE.
if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { # Wilcoxon OpenCL kernels are currently known to fail on some GPU stacks due # to unresolved runtime symbols. Keeping these commented avoids flaky checks: # n <- 1L # dwilcox_opencl(n, x = 5, m = 5, nn = 7, fallback = FALSE, verbose = TRUE) # pwilcox_opencl(q = 5, m = 5, nn = 7, fallback = FALSE, verbose = TRUE) # qwilcox_opencl(rep(0.8, n), m = 5, nn = 7, fallback = FALSE, verbose = TRUE) # # Known allocator/runtime fragility on some stacks: # rwilcox_opencl(n, m = 5, nn = 7, fallback = FALSE, verbose = TRUE) } else { n <- 1L stats::dwilcox(5, m = 5, n = 7) stats::pwilcox(5, m = 5, n = 7) stats::qwilcox(rep(0.8, n), m = 5, n = 7) stats::rwilcox(n, m = 5, n = 7) }if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { # Wilcoxon OpenCL kernels are currently known to fail on some GPU stacks due # to unresolved runtime symbols. Keeping these commented avoids flaky checks: # n <- 1L # dwilcox_opencl(n, x = 5, m = 5, nn = 7, fallback = FALSE, verbose = TRUE) # pwilcox_opencl(q = 5, m = 5, nn = 7, fallback = FALSE, verbose = TRUE) # qwilcox_opencl(rep(0.8, n), m = 5, nn = 7, fallback = FALSE, verbose = TRUE) # # Known allocator/runtime fragility on some stacks: # rwilcox_opencl(n, m = 5, nn = 7, fallback = FALSE, verbose = TRUE) } else { n <- 1L stats::dwilcox(5, m = 5, n = 7) stats::pwilcox(5, m = 5, n = 7) stats::qwilcox(rep(0.8, n), m = 5, n = 7) stats::rwilcox(n, m = 5, n = 7) }
These functions implement the grid evaluation logic used in envelope construction for rejection sampling. They make use of the theory described in (Nygren and Nygren 2006) and the general implementation outlined in (Nygren 2025).
Ex_EnvelopeEval(G4, y, x, mu, P, alpha, wt, family, link, use_opencl = FALSE, verbose = FALSE)Ex_EnvelopeEval(G4, y, x, mu, P, alpha, wt, family, link, use_opencl = FALSE, verbose = FALSE)
G4 |
Numeric matrix of parameter values (parameters * grid points). |
y |
Numeric response vector. |
x |
Numeric design matrix. |
mu |
Numeric matrix of offsets or prior means. |
P |
Numeric matrix representing the portion of the prior precision shifted into the likelihood. |
alpha |
Numeric offset vector of length |
wt |
Numeric vector of weights. |
family |
Character string; model family (e.g. |
link |
Character string; link function (e.g. |
use_opencl |
Logical; if |
verbose |
Logical; if |
Compact overview.
Derivations:\cr vignettes/equations ((Nygren and Nygren 2006)).
Dispatcher fans out to CPU and OpenCL kernel runners.
NegLL vectors plus cbars matrices feed envelopes in
rNormal_reg internals.
Acceptance inequalities mirror the vignette “Simulation execution” chapter.
NegLL: negatives logLik; cbars: tangent gradients.
qf/grad from CPU kernels.
qf/grad from OpenCL.
Pilot runtime estimate (seconds).
Nygren K (2025).
“Chapter A05: Simulation Methods – Likelihood Subgradient Densities.”
Vignette in the glmbayes R package.
R vignette name: Chapter-A05.
Nygren K~N, Nygren L~M (2006).
“Likelihood Subgradient Densities.”
Journal of the American Statistical Association, 101(475), 1144–1156.
doi:10.1198/016214506000000357.
Ex_EnvelopeSize
(Nygren 2025)
(Nygren 2025)
(Nygren 2025)
(Nygren 2025)
############################### Start of Ex_EnvelopeEval example #################### # This example demonstrates Ex_EnvelopeEval in isolation. Ex_EnvelopeEval evaluates # the negative log-likelihood and gradients at a grid of parameter values. # It is called internally by EnvelopeBuild. Here we build the same inputs # (grid G4, standardized model) using Ex_EnvelopeSize and expand.grid, then # call Ex_EnvelopeEval directly. The setup mirrors Ex_EnvelopeBuild through # the standardization step. data(menarche, package = "MASS") Age2 <- menarche$Age - 13 x <- matrix(as.numeric(1.0), nrow = length(Age2), ncol = 2) x[, 2] <- Age2 y <- menarche$Menarche / menarche$Total wt <- menarche$Total mu <- matrix(as.numeric(0.0), nrow = 2, ncol = 1) mu[2, 1] <- (log(0.9 / 0.1) - log(0.5 / 0.5)) / 3 V1 <- 1 * diag(as.numeric(2.0)) V1[1, 1] <- ((log(0.9 / 0.1) - log(0.5 / 0.5)) / 2)^2 V1[2, 2] <- (3 * mu[2, 1] / 2)^2 famfunc <- Ex_glmbfamfunc(binomial(logit)) f2 <- famfunc$f2 f3 <- famfunc$f3 dispersion2 <- as.numeric(1.0) start <- mu offset2 <- rep(as.numeric(0.0), length(y)) P <- solve(V1) n <- 1000 wt2 <- wt / dispersion2 alpha <- x %*% as.vector(mu) + offset2 mu2 <- 0 * as.vector(mu) P2 <- P x2 <- x parin <- start - mu opt_out <- optim(parin, f2, f3, y = as.vector(y), x = as.matrix(x), mu = as.vector(mu2), P = as.matrix(P), alpha = as.vector(alpha), wt = as.vector(wt2), method = "BFGS", hessian = TRUE ) bstar <- opt_out$par A1 <- opt_out$hessian Standard_Mod <- Ex_glmb_Standardize_Model( y = as.vector(y), x = as.matrix(x), P = as.matrix(P), bstar = as.matrix(bstar, ncol = 1), A1 = as.matrix(A1) ) bstar2 <- Standard_Mod$bstar2 A <- Standard_Mod$A x2 <- Standard_Mod$x2 mu2 <- Standard_Mod$mu2 P2 <- Standard_Mod$P2 ############################################################################### # Build grid G4 via Ex_EnvelopeSize and expand.grid (as EnvelopeBuild does) ############################################################################### a <- diag(A) omega <- (sqrt(2) - exp(-1.20491 - 0.7321 * sqrt(0.5 + a))) / sqrt(1 + a) b2 <- as.vector(bstar2) G1 <- rbind(b2 - omega, b2, b2 + omega) size_info <- Ex_EnvelopeSize(a, G1, Gridtype = 3L, n = n) G2 <- size_info$G2 G3 <- as.matrix(do.call(expand.grid, G2)) G4 <- t(G3) ############################################################################### # Ex_EnvelopeEval: negative log-likelihood and gradients at grid points ############################################################################### eval_out <- Ex_EnvelopeEval( G4 = G4, y = y, x = as.matrix(x2), mu = as.matrix(mu2, ncol = 1), P = as.matrix(P2), alpha = as.vector(alpha), wt = as.vector(wt2), family = "binomial", link = "logit", use_opencl = FALSE, verbose = FALSE ) eval_out$NegLL eval_out$cbars ############################################################################### # End of Ex_EnvelopeEval example ############################################################################################################## Start of Ex_EnvelopeEval example #################### # This example demonstrates Ex_EnvelopeEval in isolation. Ex_EnvelopeEval evaluates # the negative log-likelihood and gradients at a grid of parameter values. # It is called internally by EnvelopeBuild. Here we build the same inputs # (grid G4, standardized model) using Ex_EnvelopeSize and expand.grid, then # call Ex_EnvelopeEval directly. The setup mirrors Ex_EnvelopeBuild through # the standardization step. data(menarche, package = "MASS") Age2 <- menarche$Age - 13 x <- matrix(as.numeric(1.0), nrow = length(Age2), ncol = 2) x[, 2] <- Age2 y <- menarche$Menarche / menarche$Total wt <- menarche$Total mu <- matrix(as.numeric(0.0), nrow = 2, ncol = 1) mu[2, 1] <- (log(0.9 / 0.1) - log(0.5 / 0.5)) / 3 V1 <- 1 * diag(as.numeric(2.0)) V1[1, 1] <- ((log(0.9 / 0.1) - log(0.5 / 0.5)) / 2)^2 V1[2, 2] <- (3 * mu[2, 1] / 2)^2 famfunc <- Ex_glmbfamfunc(binomial(logit)) f2 <- famfunc$f2 f3 <- famfunc$f3 dispersion2 <- as.numeric(1.0) start <- mu offset2 <- rep(as.numeric(0.0), length(y)) P <- solve(V1) n <- 1000 wt2 <- wt / dispersion2 alpha <- x %*% as.vector(mu) + offset2 mu2 <- 0 * as.vector(mu) P2 <- P x2 <- x parin <- start - mu opt_out <- optim(parin, f2, f3, y = as.vector(y), x = as.matrix(x), mu = as.vector(mu2), P = as.matrix(P), alpha = as.vector(alpha), wt = as.vector(wt2), method = "BFGS", hessian = TRUE ) bstar <- opt_out$par A1 <- opt_out$hessian Standard_Mod <- Ex_glmb_Standardize_Model( y = as.vector(y), x = as.matrix(x), P = as.matrix(P), bstar = as.matrix(bstar, ncol = 1), A1 = as.matrix(A1) ) bstar2 <- Standard_Mod$bstar2 A <- Standard_Mod$A x2 <- Standard_Mod$x2 mu2 <- Standard_Mod$mu2 P2 <- Standard_Mod$P2 ############################################################################### # Build grid G4 via Ex_EnvelopeSize and expand.grid (as EnvelopeBuild does) ############################################################################### a <- diag(A) omega <- (sqrt(2) - exp(-1.20491 - 0.7321 * sqrt(0.5 + a))) / sqrt(1 + a) b2 <- as.vector(bstar2) G1 <- rbind(b2 - omega, b2, b2 + omega) size_info <- Ex_EnvelopeSize(a, G1, Gridtype = 3L, n = n) G2 <- size_info$G2 G3 <- as.matrix(do.call(expand.grid, G2)) G4 <- t(G3) ############################################################################### # Ex_EnvelopeEval: negative log-likelihood and gradients at grid points ############################################################################### eval_out <- Ex_EnvelopeEval( G4 = G4, y = y, x = as.matrix(x2), mu = as.matrix(mu2, ncol = 1), P = as.matrix(P2), alpha = as.vector(alpha), wt = as.vector(wt2), family = "binomial", link = "logit", use_opencl = FALSE, verbose = FALSE ) eval_out$NegLL eval_out$cbars ############################################################################### # End of Ex_EnvelopeEval example ###############################################################################
Ex_EnvelopeSize() is the high-level entry point that constructs
per-dimension grids and expected draw counts, while Ex_EnvelopeOpt()
performs the adaptive optimization used when Gridtype = 2.
Ex_EnvelopeSize(a, G1, Gridtype = 2L, n = 1000L, n_envopt = -1, use_opencl = FALSE, verbose = FALSE)Ex_EnvelopeSize(a, G1, Gridtype = 2L, n = 1000L, n_envopt = -1, use_opencl = FALSE, verbose = FALSE)
a |
Numeric vector of diagonal precisions for the log-likelihood
(posterior precision is |
G1 |
Numeric matrix of candidate grid points (3 * l1). |
Gridtype |
Integer code controlling grid sizing logic:
|
n |
Integer; number of posterior draws to generate (used for grid sizing). |
n_envopt |
Integer; effective sample size passed to |
use_opencl |
Logical; if |
verbose |
Logical; if |
These functions implement the grid sizing logic used in envelope construction for rejection sampling. They make use of the theory described in (Nygren and Nygren 2006) and the general implementation outlined in (Nygren 2025).
Ex_EnvelopeSize() returns the constructed grid (G2),
index vectors (GIndex1), expected draw count (E_draws),
and the per-dimension grid index.
Ex_EnvelopeOpt() implements the adaptive optimization used in
Gridtype = 2, ranking dimensions by posterior variance and
promoting them to three-point tangents when the tradeoff is favorable.
Ex_EnvelopeSize()A list with components G2, GIndex1,
E_draws, and gridindex.
Ex_EnvelopeOpt()An integer vector of length with entries
1 (single-point) or 3 (three-point).
The envelope sizing logic follows the analysis of (Nygren and Nygren 2006).
For each dimension , if
,
then a single tangent at the posterior mode suffices.
Expected candidates per draw in that dimension:
. Otherwise, a symmetric three-point envelope is used at
,
with expected candidates per draw bounded above by
.
Each dimension is assigned either a single-point or three-point envelope by minimizing
The optimizer balances build cost (grows with number of tangents) against
sampling cost (decreases as acceptance improves).
Expected candidates per draw:
, where each factor is either
(single-point) or (three-point),
depending on the optimization outcome.
Every dimension uses a symmetric three-point envelope. Expected candidates per draw:
for dimensions, as shown in Theorem 3 of
(Nygren and Nygren 2006).
Every dimension uses a single tangent at the posterior mode. Expected candidates per draw:
(Example 1 in (Nygren and Nygren 2006)).
Nygren K (2025).
“Chapter A05: Simulation Methods – Likelihood Subgradient Densities.”
Vignette in the glmbayes R package.
R vignette name: Chapter-A05.
Nygren K~N, Nygren L~M (2006).
“Likelihood Subgradient Densities.”
Journal of the American Statistical Association, 101(475), 1144–1156.
doi:10.1198/016214506000000357.
Ex_EnvelopeEval for evaluating these grids.
(Nygren 2025)
(Nygren 2025)
Standardizes a Non-Gaussian Model prior to Envelope Creation
Ex_glmb_Standardize_Model(y, x, P, bstar, A1)Ex_glmb_Standardize_Model(y, x, P, bstar, A1)
y |
a vector of observations of length m |
x |
a design matrix of dimension m*p |
P |
Positive-definite prior precision ( |
bstar |
a matrix containing the posterior mode from an optimization step |
A1 |
a matrix containing the posterior precision matrix at the posterior mode |
Starts from the posterior mode quantities and proceeds in three transformations.
Step 1: posterior-precision eigendecomp.
Interim model gets identity posterior precision.
Step 2: isolate diagonal epsilon; remainder behaves like likelihood information.
Step 3: another eigendecomp diagonalizes data precision A.
The
prior tied to epsilon becomes identity.
(Nygren and Nygren 2006)
A list with the following components
bstar2 |
Standardized Posterior Mode |
A |
Standardized Data Precision Matrix |
x2 |
Standardized Design Matrix |
mu2 |
Standardized Prior Mean vector |
P2 |
Standardized Precision Matrix Added to log-likelihood |
L2Inv |
A matrix used when undoing the first step in standardization described below |
L3Inv |
A matrix used when undoing the second step in standardization described below |
Nygren K~N, Nygren L~M (2006). “Likelihood Subgradient Densities.” Journal of the American Statistical Association, 101(475), 1144–1156. doi:10.1198/016214506000000357.
This function takes as input a family object and returns a
set of functions that are used during simulation and summarization of models
using the simulation functions in this package.
Ex_glmbfamfunc(family) ## S3 method for class 'Ex_glmbfamfunc' print(x, ...)Ex_glmbfamfunc(family) ## S3 method for class 'Ex_glmbfamfunc' print(x, ...)
family |
an object of class |
x |
an object of class |
... |
additional optional arguments |
Compiled paths dominate;
retain Ex_glmbfamfunc closures for scripted workflows.
A list (class "Ex_glmbfamfunc") whose first four components are always
present for every supported family and link. The names f1–f4
are stable: they mean the same roles across families (only the internal formulas change).
f1Neg log-likelihood in coefficients b (usual data args).
f2Neg log-posterior: likelihood plus Normal(mu, P) quadratic penalty.
f3Gradient of f2 w.r.t.\ b (argument pattern mirrors f2).
f4Deviance gap vs saturation; honors dispersion;
quasi / DIC helper.
f7Weighted curvature / Hessian proxy at b.
Slots f5 and f6 are not returned: they were reserved for alternate or
C++-aligned likelihood/posterior routines and remain commented out in the implementation
(only f1, f2, f3, f4, and f7 are assigned in the returned list).
Given one or more kernel .cl files and a library directory, determines the
minimal set of library files required (union of all kernels' dependency
annotations) and copies them — in dependency order — to a destination
directory.
extract_library_subset( kernel_paths, library_dir, dest_dir, depends_tag = "all_depends", index = NULL, overwrite = FALSE )extract_library_subset( kernel_paths, library_dir, dest_dir, depends_tag = "all_depends", index = NULL, overwrite = FALSE )
kernel_paths |
Character vector of paths to kernel |
library_dir |
Path to the source library directory. |
dest_dir |
Path where files would be written. Must already exist for any
copying to occur. If absent, a warning is issued, no directories are created,
and nothing is copied; the returned |
depends_tag |
Name of the annotation tag listing library file stems.
Defaults to |
index |
Pre-loaded dependency index. If |
overwrite |
Logical; if |
Use this to populate a project-local copy containing only the library files actually needed by your kernels. The result can then be committed alongside your kernel files, removing a runtime dependency on the full library.
Bundled libraries such as inst/cl/nmath ship
kernel_dependency_index.rds next to their .cl shards. Pass
index = when you pass a pre-loaded index object to avoid redundant reads; regenerate
the files with write_kernel_dependency_index, for example after porting via
nmathtools/port_inst_cl_nmath_from_src.R in the openclport
package source tree.
lib_dir <- system.file("cl/nmath", package = "opencltools")
kernel_paths <- system.file(
c("cl/src/dnorm_kernel.cl", "cl/src/pnorm_kernel.cl"),
package = "opencltools"
)
dest_dir <- tempfile("ex_subset"); dir.create(dest_dir)
## on.exit(unlink(dest_dir, recursive = TRUE), add = TRUE)
result <- extract_library_subset(
kernel_paths, lib_dir, dest_dir,
depends_tag = "all_depends_nmath")
extract_library_subset() evaluates
inst/extdata/opencl_known_failures.json against the union of
depends_tag annotations and launcher paths.
A nmathopencl_lib_extract_df subclass of data.frame
with one row per library shard (.cl files in dependency order, followed by
companion index files when planned or copied) and columns:
stemStem name (filename without .cl), or index filenames.
sourceFull path to the source file under library_dir.
destIntended destination path under dest_dir.
copiedTRUE if copied; otherwise FALSE including when
dest_dir is missing (copied = FALSE for every row),
a source path is missing, or an existing destination was skipped.
Other OpenCL kernel library subsets:
load_library_for_kernel(),
write_kernel_dependency_index()
############################ Start of extract_library_subset example ######################### ## Small library (fast; runs on CRAN check) lib_small <- system.file("cl/nmath_small", package = "opencltools") kpath <- system.file("cl/src/dnorm_kernel.cl", package = "opencltools") idx_small <- write_kernel_dependency_index(library_dir = lib_small, write = FALSE) dest_small <- file.path(tempdir(), "opencltools_extract_small") if (dir.exists(dest_small)) unlink(dest_small, recursive = TRUE) dir.create(dest_small, recursive = TRUE) on.exit(unlink(dest_small, recursive = TRUE), add = TRUE) df_small <- extract_library_subset( kpath, lib_small, dest_small, depends_tag = "all_depends_nmath", index = idx_small ) sum(df_small$copied) ## Full nmath (slow) lib_dir <- system.file("cl/nmath", package = "opencltools") kernel_paths <- system.file( c("cl/src/dnorm_kernel.cl", "cl/src/pnorm_kernel.cl"), package = "opencltools" ) idx <- write_kernel_dependency_index(library_dir = lib_dir, write = FALSE) dest_dir <- file.path(tempdir(), "opencltools_extract_example") if (dir.exists(dest_dir)) unlink(dest_dir, recursive = TRUE) dir.create(dest_dir, recursive = TRUE) on.exit(unlink(dest_dir, recursive = TRUE), add = TRUE) df <- extract_library_subset( kernel_paths, lib_dir, dest_dir, depends_tag = "all_depends_nmath", index = idx ) print(df) sum(df$copied) ############################################################################### ## End of extract_library_subset example ########################################################################################################### Start of extract_library_subset example ######################### ## Small library (fast; runs on CRAN check) lib_small <- system.file("cl/nmath_small", package = "opencltools") kpath <- system.file("cl/src/dnorm_kernel.cl", package = "opencltools") idx_small <- write_kernel_dependency_index(library_dir = lib_small, write = FALSE) dest_small <- file.path(tempdir(), "opencltools_extract_small") if (dir.exists(dest_small)) unlink(dest_small, recursive = TRUE) dir.create(dest_small, recursive = TRUE) on.exit(unlink(dest_small, recursive = TRUE), add = TRUE) df_small <- extract_library_subset( kpath, lib_small, dest_small, depends_tag = "all_depends_nmath", index = idx_small ) sum(df_small$copied) ## Full nmath (slow) lib_dir <- system.file("cl/nmath", package = "opencltools") kernel_paths <- system.file( c("cl/src/dnorm_kernel.cl", "cl/src/pnorm_kernel.cl"), package = "opencltools" ) idx <- write_kernel_dependency_index(library_dir = lib_dir, write = FALSE) dest_dir <- file.path(tempdir(), "opencltools_extract_example") if (dir.exists(dest_dir)) unlink(dest_dir, recursive = TRUE) dir.create(dest_dir, recursive = TRUE) on.exit(unlink(dest_dir, recursive = TRUE), add = TRUE) df <- extract_library_subset( kernel_paths, lib_dir, dest_dir, depends_tag = "all_depends_nmath", index = idx ) print(df) sum(df$copied) ############################################################################### ## End of extract_library_subset example ###############################################################################
OpenCL-backed wrappers for selected special functions from R Mathlib.
gammafn_opencl(x, fallback = FALSE, verbose = FALSE) lgammafn_opencl(x, fallback = FALSE, verbose = FALSE) digamma_opencl(x, fallback = FALSE, verbose = FALSE) trigamma_opencl(x, fallback = FALSE, verbose = FALSE) tetragamma_opencl(x, fallback = FALSE, verbose = FALSE) pentagamma_opencl(x, fallback = FALSE, verbose = FALSE) psigamma_opencl(x, deriv, fallback = FALSE, verbose = FALSE) beta_opencl(a, b, fallback = FALSE, verbose = FALSE) lbeta_opencl(a, b, fallback = FALSE, verbose = FALSE) choose_opencl(n, k, fallback = FALSE, verbose = FALSE) lchoose_opencl(n, k, fallback = FALSE, verbose = FALSE)gammafn_opencl(x, fallback = FALSE, verbose = FALSE) lgammafn_opencl(x, fallback = FALSE, verbose = FALSE) digamma_opencl(x, fallback = FALSE, verbose = FALSE) trigamma_opencl(x, fallback = FALSE, verbose = FALSE) tetragamma_opencl(x, fallback = FALSE, verbose = FALSE) pentagamma_opencl(x, fallback = FALSE, verbose = FALSE) psigamma_opencl(x, deriv, fallback = FALSE, verbose = FALSE) beta_opencl(a, b, fallback = FALSE, verbose = FALSE) lbeta_opencl(a, b, fallback = FALSE, verbose = FALSE) choose_opencl(n, k, fallback = FALSE, verbose = FALSE) lchoose_opencl(n, k, fallback = FALSE, verbose = FALSE)
x |
Numeric vector (and additional vectors where listed); arguments are recycled to a common length like the corresponding base functions. |
fallback |
When |
verbose |
Logical; print fallback/error diagnostics. |
deriv |
Derivative order for |
a, b
|
Parameters for |
n, k
|
Arguments for |
Numeric vector of recycled common length.
if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { gammafn_opencl(x = 2.5, fallback = FALSE, verbose = TRUE) lgammafn_opencl(x = 2.5, fallback = FALSE, verbose = TRUE) digamma_opencl(x = 2.5, fallback = FALSE, verbose = TRUE) trigamma_opencl(x = 2.5, fallback = FALSE, verbose = TRUE) tetragamma_opencl(x = 2.5, fallback = FALSE, verbose = TRUE) pentagamma_opencl(x = 2.5, fallback = FALSE, verbose = TRUE) psigamma_opencl(x = 2.5, deriv = 1, fallback = FALSE, verbose = TRUE) beta_opencl(a = 2.5, b = 3.0, fallback = FALSE, verbose = TRUE) lbeta_opencl(a = 2.5, b = 3.0, fallback = FALSE, verbose = TRUE) choose_opencl(n = 10, k = 4, fallback = FALSE, verbose = TRUE) lchoose_opencl(n = 10, k = 4, fallback = FALSE, verbose = TRUE) } else { base::gamma(2.5) base::lgamma(2.5) base::digamma(2.5) base::trigamma(2.5) base::psigamma(2.5, deriv = 2L) base::psigamma(2.5, deriv = 3L) base::psigamma(2.5, deriv = 1L) base::beta(2.5, 3.0) base::lbeta(2.5, 3.0) base::choose(10, 4) base::lchoose(10, 4) }if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { gammafn_opencl(x = 2.5, fallback = FALSE, verbose = TRUE) lgammafn_opencl(x = 2.5, fallback = FALSE, verbose = TRUE) digamma_opencl(x = 2.5, fallback = FALSE, verbose = TRUE) trigamma_opencl(x = 2.5, fallback = FALSE, verbose = TRUE) tetragamma_opencl(x = 2.5, fallback = FALSE, verbose = TRUE) pentagamma_opencl(x = 2.5, fallback = FALSE, verbose = TRUE) psigamma_opencl(x = 2.5, deriv = 1, fallback = FALSE, verbose = TRUE) beta_opencl(a = 2.5, b = 3.0, fallback = FALSE, verbose = TRUE) lbeta_opencl(a = 2.5, b = 3.0, fallback = FALSE, verbose = TRUE) choose_opencl(n = 10, k = 4, fallback = FALSE, verbose = TRUE) lchoose_opencl(n = 10, k = 4, fallback = FALSE, verbose = TRUE) } else { base::gamma(2.5) base::lgamma(2.5) base::digamma(2.5) base::trigamma(2.5) base::psigamma(2.5, deriv = 2L) base::psigamma(2.5, deriv = 3L) base::psigamma(2.5, deriv = 1L) base::beta(2.5, 3.0) base::lbeta(2.5, 3.0) base::choose(10, 4) base::lchoose(10, 4) }
Core utilities for envelope-based posterior simulation using GPU-accelerated OpenCL kernels. These functions support the construction and evaluation of log-likelihood envelopes used in rejection sampling, and serve as an example of how downstream packages can build custom OpenCL kernels on top of the ported nmath routines in nmathopencl.
There are no references for Rd macro \insertAllCites on this help page.
OpenCL-backed wrappers for miscellaneous scalar support functions.
imax2_opencl(x, y, fallback = FALSE, verbose = FALSE) imin2_opencl(x, y, fallback = FALSE, verbose = FALSE) fmax2_opencl(x, y, fallback = FALSE, verbose = FALSE) fmin2_opencl(x, y, fallback = FALSE, verbose = FALSE) sign_opencl(x, fallback = FALSE, verbose = FALSE) fprec_opencl(x, digits, fallback = FALSE, verbose = FALSE) fround_opencl(x, digits, fallback = FALSE, verbose = FALSE) fsign_opencl(x, y, fallback = FALSE, verbose = FALSE) ftrunc_opencl(x, fallback = FALSE, verbose = FALSE)imax2_opencl(x, y, fallback = FALSE, verbose = FALSE) imin2_opencl(x, y, fallback = FALSE, verbose = FALSE) fmax2_opencl(x, y, fallback = FALSE, verbose = FALSE) fmin2_opencl(x, y, fallback = FALSE, verbose = FALSE) sign_opencl(x, fallback = FALSE, verbose = FALSE) fprec_opencl(x, digits, fallback = FALSE, verbose = FALSE) fround_opencl(x, digits, fallback = FALSE, verbose = FALSE) fsign_opencl(x, y, fallback = FALSE, verbose = FALSE) ftrunc_opencl(x, fallback = FALSE, verbose = FALSE)
x, y
|
Numeric vectors recycled together (where both appear). |
fallback |
When |
verbose |
Logical; print fallback/error diagnostics. |
digits |
Numeric vector recycled with |
Numeric vector of recycled common length.
if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { imax2_opencl(x = 7, y = 3, fallback = FALSE, verbose = TRUE) imin2_opencl(x = 7, y = 3, fallback = FALSE, verbose = TRUE) fmax2_opencl(x = 7.2, y = 3.1, fallback = FALSE, verbose = TRUE) fmin2_opencl(x = 7.2, y = 3.1, fallback = FALSE, verbose = TRUE) sign_opencl(x = -2.5, fallback = FALSE, verbose = TRUE) fprec_opencl(x = 123.456, digits = 4, fallback = FALSE, verbose = TRUE) fround_opencl(x = 123.456, digits = 2, fallback = FALSE, verbose = TRUE) fsign_opencl(x = -2.5, y = 4.0, fallback = FALSE, verbose = TRUE) ftrunc_opencl(x = 123.456, fallback = FALSE, verbose = TRUE) } else { as.double(pmax(7L, 3L)) as.double(pmin(7L, 3L)) pmax(7.2, 3.1) pmin(7.2, 3.1) base::sign(-2.5) signif(123.456, digits = 4) base::round(123.456, digits = 2) base::sign(-2.5) * abs(4.0) base::trunc(123.456) }if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { imax2_opencl(x = 7, y = 3, fallback = FALSE, verbose = TRUE) imin2_opencl(x = 7, y = 3, fallback = FALSE, verbose = TRUE) fmax2_opencl(x = 7.2, y = 3.1, fallback = FALSE, verbose = TRUE) fmin2_opencl(x = 7.2, y = 3.1, fallback = FALSE, verbose = TRUE) sign_opencl(x = -2.5, fallback = FALSE, verbose = TRUE) fprec_opencl(x = 123.456, digits = 4, fallback = FALSE, verbose = TRUE) fround_opencl(x = 123.456, digits = 2, fallback = FALSE, verbose = TRUE) fsign_opencl(x = -2.5, y = 4.0, fallback = FALSE, verbose = TRUE) ftrunc_opencl(x = 123.456, fallback = FALSE, verbose = TRUE) } else { as.double(pmax(7L, 3L)) as.double(pmin(7L, 3L)) pmax(7.2, 3.1) pmin(7.2, 3.1) base::sign(-2.5) signif(123.456, digits = 4) base::round(123.456, digits = 2) base::sign(-2.5) * abs(4.0) base::trunc(123.456) }
Given a single kernel .cl file and a library directory (with an associated
dependency index), reads the annotation tag that lists needed library files
and returns their source code concatenated in the correct dependency order.
load_library_for_kernel( kernel_path, library_dir, depends_tag = "all_depends", index = NULL )load_library_for_kernel( kernel_path, library_dir, depends_tag = "all_depends", index = NULL )
kernel_path |
Path to a single |
library_dir |
Path to the library directory containing the |
depends_tag |
Name of the annotation tag in the kernel file that lists
the required library file stems. Defaults to |
index |
Optional RDS list; |
For repeated calls in R code, loading kernel_dependency_index.rds once
and passing index = avoids redundant disk reads. The bundled
cl/nmath directory ships kernel_dependency_index.rds beside the
.cl files; use write_kernel_dependency_index to regenerate it after
porting (for example via nmathtools/port_inst_cl_nmath_from_src.R in the
openclport package source tree).
lib_dir <- system.file("cl/nmath", package = "opencltools")
kpath <- system.file("cl/src/dnorm_kernel.cl", package = "opencltools")
src <- load_library_for_kernel(
kpath, lib_dir,
depends_tag = "all_depends_nmath")
Subsets come only from each launcher file's transitive
@all_depends_nmath-style annotations (use attach_cross_library_tags
where dependency graphs span libraries). Deliberately loading every .cl under
cl/nmath remains available via load_kernel_library(..., "nmath")
where appropriate.
When inst/extdata/opencl_known_failures.json matches the launcher path or
the declared / loaded stems, warning(...) points to fragile ports.
A character vector subclass nmathopencl_concatenated_lib holding
concatenated sources (often length 1; blank annotations yield length-zero
concatenation). Attachments describe requested and loaded library stems,
paths, and byte size; see print methods.
Other OpenCL kernel library subsets:
extract_library_subset(),
write_kernel_dependency_index()
############################ Start of load_library_for_kernel example ######################## ## Small library (fast; runs on CRAN check) lib_small <- system.file("cl/nmath_small", package = "opencltools") kpath <- system.file("cl/src/dnorm_kernel.cl", package = "opencltools") idx_small <- write_kernel_dependency_index(library_dir = lib_small, write = FALSE) src_small <- load_library_for_kernel( kpath, lib_small, depends_tag = "all_depends_nmath", index = idx_small ) nzchar(src_small) ## Full nmath (slow; rebuilds index over all shards) lib_dir <- system.file("cl/nmath", package = "opencltools") idx <- write_kernel_dependency_index(library_dir = lib_dir, write = FALSE) src <- load_library_for_kernel( kpath, lib_dir, depends_tag = "all_depends_nmath", index = idx ) print(src) nzchar(src) ############################################################################### ## End of load_library_for_kernel example ########################################################################################################### Start of load_library_for_kernel example ######################## ## Small library (fast; runs on CRAN check) lib_small <- system.file("cl/nmath_small", package = "opencltools") kpath <- system.file("cl/src/dnorm_kernel.cl", package = "opencltools") idx_small <- write_kernel_dependency_index(library_dir = lib_small, write = FALSE) src_small <- load_library_for_kernel( kpath, lib_small, depends_tag = "all_depends_nmath", index = idx_small ) nzchar(src_small) ## Full nmath (slow; rebuilds index over all shards) lib_dir <- system.file("cl/nmath", package = "opencltools") idx <- write_kernel_dependency_index(library_dir = lib_dir, write = FALSE) src <- load_library_for_kernel( kpath, lib_dir, depends_tag = "all_depends_nmath", index = idx ) print(src) nzchar(src) ############################################################################### ## End of load_library_for_kernel example ###############################################################################
Compile-time and device-selection probes for nmathopencl.
Low-level workstation probes (GPU vendor detection, driver and ICD checks,
PATH validation, verify_opencl_runtime(), PATH helpers, combined
diagnostic reports, etc.) live in opencltools; call them as
opencltools::detect_environment_and_gpus(),
opencltools::diagnose_glmbayes(), and related topics documented under
?opencltools.
nmathopencl_has_opencl() nmathopencl_opencl_device_info(force = FALSE, details = FALSE) nmathopencl_opencl_fp64_available(force = FALSE) nmathopencl_opencl_reset_device_selection()nmathopencl_has_opencl() nmathopencl_opencl_device_info(force = FALSE, details = FALSE) nmathopencl_opencl_fp64_available(force = FALSE) nmathopencl_opencl_reset_device_selection()
force |
If |
details |
If |
GPU acceleration uses OpenCL kernels and *_opencl wrappers when
nmathopencl_has_opencl() is TRUE and a suitable device is available
((Stone et al. 2010)). CPU fallbacks apply for many routines
when OpenCL is absent at compile time or runtime.
For a readable host/runtime report, use opencltools::diagnose_glmbayes();
use nmathopencl_has_opencl() for this package's compile-time flag.
Setup: vignette("Chapter-01", package = "nmathopencl"); packaged GPU API:
vignette("Chapter-12", package = "nmathopencl").
A list with ok (logical),
reason (character), indices, vendor/name strings, device_type,
extension_cl_khr_fp64, probe_fp64_ok, selection_policy,
and optionally candidates.
nmathopencl_opencl_device_info(): Cached OpenCL device selection for double-precision
(cl_khr_fp64) kernels in nmathopencl: enumerates platforms and devices, prefers GPU,
checks the extension token, then verifies with a tiny clBuildProgram
probe. Override with environment variables NMATHOPENCL_PLATFORM_INDEX
and/or NMATHOPENCL_DEVICE_INDEX (0-based; device index is within the
platform's device list). Use nmathopencl_opencl_reset_device_selection()
to clear the cache (e.g. after driver changes).
nmathopencl_opencl_fp64_available(): Returns TRUE if a cached OpenCL device passes
the cl_khr_fp64 extension check and build probe used for double kernels
in nmathopencl (uses this package's compile-time OpenCL build and device
cache, not opencltools).
nmathopencl_opencl_reset_device_selection(): Clears the process-local OpenCL device selection cache
so the next kernel or nmathopencl_opencl_device_info() run re-enumerates devices.
nmathopencl_has_opencl() — TRUE if this build was compiled
with OpenCL support.
nmathopencl_opencl_device_info(),
nmathopencl_opencl_fp64_available() —
cached double-precision device selection for kernels.
nmathopencl_opencl_reset_device_selection() — clear device cache.
opencltools::get_opencl_core_count() —
compute units on the opencltools default device.
add_to_path_windows() and related PATH helpers
Stone JE, Gohara D, Shi G (2010). “OpenCL: A Parallel Programming Standard for Heterogeneous Computing Systems.” Computing in Science & Engineering, 12(3), 66–72. doi:10.1109/MCSE.2010.69.
nmathopencl_has_opencl,
nmathopencl_opencl_device_info, opencltools.
Linkage wrappers for core RNG primitives used by translated Mathlib paths.
norm_rand_opencl(n, fallback = FALSE, verbose = FALSE) unif_rand_opencl(n, fallback = FALSE, verbose = FALSE) r_unif_index_opencl(n, dn, fallback = FALSE, verbose = FALSE) exp_rand_opencl(n, fallback = FALSE, verbose = FALSE)norm_rand_opencl(n, fallback = FALSE, verbose = FALSE) unif_rand_opencl(n, fallback = FALSE, verbose = FALSE) r_unif_index_opencl(n, dn, fallback = FALSE, verbose = FALSE) exp_rand_opencl(n, fallback = FALSE, verbose = FALSE)
n |
Number of observations. Non-negative integer scalar. |
fallback |
When |
verbose |
Logical; print fallback/error diagnostics. |
dn |
Positive upper bound used by |
Numeric vector of length n.
n <- 5L if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { norm_rand_opencl(n, fallback = FALSE, verbose = TRUE) unif_rand_opencl(n, fallback = FALSE, verbose = TRUE) r_unif_index_opencl(n, dn = 10, fallback = FALSE, verbose = TRUE) exp_rand_opencl(n, fallback = FALSE, verbose = TRUE) } else { stats::rnorm(n) stats::runif(n) floor(stats::runif(n, min = 0, max = 10)) stats::rexp(n) }n <- 5L if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { norm_rand_opencl(n, fallback = FALSE, verbose = TRUE) unif_rand_opencl(n, fallback = FALSE, verbose = TRUE) r_unif_index_opencl(n, dn = 10, fallback = FALSE, verbose = TRUE) exp_rand_opencl(n, fallback = FALSE, verbose = TRUE) } else { stats::rnorm(n) stats::runif(n) floor(stats::runif(n, min = 0, max = 10)) stats::rexp(n) }
Migrates a package that already has a static committed src/Makevars
(and optionally src/Makevars.win) to the configure-script pattern
required for CRAN-safe OpenCL support.
The function renames the existing src/Makevars to
src/Makevars.in (the maintained source template) and generates
configure (Linux/macOS) and configure.win (Windows) scripts
that read src/Makevars.in at R CMD INSTALL time, run OpenCL
detection, and write the final src/Makevars with OpenCL flags merged
in – or copied verbatim from src/Makevars.in for CPU-only builds.
The generated scripts always succeed: if no OpenCL SDK is found the package installs cleanly as CPU-only. This is the property that makes packages safe for CRAN submission on build machines without a GPU SDK.
For packages with no existing src/Makevars, use
use_opencl_configure instead. This function is for
migrating an existing static Makevars.
port_to_opencl_configure(path = ".", backup = TRUE, overwrite = FALSE)port_to_opencl_configure(path = ".", backup = TRUE, overwrite = FALSE)
path |
Character. Root directory of the target package. Defaults
to the current working directory ( |
backup |
Logical. If |
overwrite |
Logical. If |
Invisibly returns a character vector of the file paths written.
After porting, maintain src/Makevars.in for your base build
flags (OpenMP, RcppParallel, LAPACK, etc.). The configure script reads it
at install time and appends (or omits) the OpenCL flags. The generated
src/Makevars is a build artifact – add it to .gitignore and
never commit it. To update base flags, edit src/Makevars.in and
reinstall.
configure / configure.win
-> reads src/Makevars.in for base flags
-> detects CL/cl.h + libOpenCL (+ runtime probe on Linux)
-> writes -DUSE_OPENCL into Makevars (or copies .in verbatim)
#ifdef USE_OPENCL in C++ source
-> guards all GPU code; package compiles cleanly either way
has_opencl() in R
-> mirrors the compile-time flag; TRUE only if USE_OPENCL was set
+= (append) assignments in src/Makevars are detected
and trigger a warning; review the generated configure carefully.
If src/Makevars looks like a generated file (contains
absolute paths, -lOpenCL, or -DUSE_OPENCL), the function
warns. Run on the static committed file, not a build artifact.
Packages that already have configure or configure.win
are refused unless overwrite = TRUE. Users with existing
configure scripts should integrate the OpenCL block manually; see
system.file("configure-templates", "README.md",
package = "opencltools").
use_opencl_configure for packages without an existing
src/Makevars.
vignette("Chapter-02", package = "nmathopencl") in nmathopencl for a
full guide when building on the ported nmath kernel library.
############################ Start of port_to_opencl_configure example ######################## tmp <- tempfile("port_opencl_pkg") dir.create(tmp) dir.create(file.path(tmp, "src")) on.exit(unlink(tmp, recursive = TRUE), add = TRUE) writeLines( c( "PKG_CXXFLAGS = $(SHLIB_OPENMP_CXXFLAGS)", "PKG_LIBS = $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)" ), file.path(tmp, "src", "Makevars") ) written <- port_to_opencl_configure(path = tmp, backup = TRUE) written file.exists(file.path(tmp, "configure")) file.exists(file.path(tmp, "src", "Makevars.in")) ## Regenerate configure scripts after editing Makevars.in (same temp tree) written2 <- port_to_opencl_configure(path = tmp, backup = FALSE, overwrite = TRUE) length(written2) ############################################################################### ## End of port_to_opencl_configure example ########################################################################################################### Start of port_to_opencl_configure example ######################## tmp <- tempfile("port_opencl_pkg") dir.create(tmp) dir.create(file.path(tmp, "src")) on.exit(unlink(tmp, recursive = TRUE), add = TRUE) writeLines( c( "PKG_CXXFLAGS = $(SHLIB_OPENMP_CXXFLAGS)", "PKG_LIBS = $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)" ), file.path(tmp, "src", "Makevars") ) written <- port_to_opencl_configure(path = tmp, backup = TRUE) written file.exists(file.path(tmp, "configure")) file.exists(file.path(tmp, "src", "Makevars.in")) ## Regenerate configure scripts after editing Makevars.in (same temp tree) written2 <- port_to_opencl_configure(path = tmp, backup = FALSE, overwrite = TRUE) length(written2) ############################################################################### ## End of port_to_opencl_configure example ###############################################################################
OpenCL-backed distribution and quantile wrappers for the studentized range (Tukey) distribution.
ptukey_opencl( q, nmeans, df, nranges = 1, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qtukey_opencl( p, nmeans, df, nranges = 1, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE )ptukey_opencl( q, nmeans, df, nranges = 1, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE ) qtukey_opencl( p, nmeans, df, nranges = 1, lower.tail = TRUE, log.p = FALSE, opencl_parallel = NA, fallback = FALSE, verbose = FALSE )
q |
Numeric vector of quantiles for |
nmeans |
Number of means in each range (must be >= 2). |
df |
Degrees of freedom (must be > 0). |
nranges |
Number of groups whose maxima/minima define the range (must be >= 1). |
lower.tail, log.p
|
Tail/log-p inputs ( |
opencl_parallel |
Dispatch hint |
fallback |
When |
verbose |
Logical; print fallback/error diagnostics. |
p |
Numeric vector of probabilities for |
Numeric vector result from ptukey_opencl or qtukey_opencl.
n <- 1L if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { ptukey_opencl(q = 3.4, nmeans = 5, df = 10, nranges = 1, fallback = FALSE, verbose = TRUE) qtukey_opencl(rep(0.8, n), nmeans = 5, df = 10, nranges = 1, fallback = FALSE, verbose = TRUE) } else { stats::ptukey(3.4, nmeans = 5, df = 10, nranges = 1) stats::qtukey(rep(0.8, n), nmeans = 5, df = 10, nranges = 1) }n <- 1L if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { ptukey_opencl(q = 3.4, nmeans = 5, df = 10, nranges = 1, fallback = FALSE, verbose = TRUE) qtukey_opencl(rep(0.8, n), nmeans = 5, df = 10, nranges = 1, fallback = FALSE, verbose = TRUE) } else { stats::ptukey(3.4, nmeans = 5, df = 10, nranges = 1) stats::qtukey(rep(0.8, n), nmeans = 5, df = 10, nranges = 1) }
Wrappers for utility hooks used by translated R_ext-dependent kernels.
r_check_user_interrupt_opencl(n, fallback = FALSE, verbose = FALSE) r_check_stack_opencl(n, fallback = FALSE, verbose = FALSE)r_check_user_interrupt_opencl(n, fallback = FALSE, verbose = FALSE) r_check_stack_opencl(n, fallback = FALSE, verbose = FALSE)
n |
Number of observations. Non-negative integer scalar. |
fallback |
When |
verbose |
Logical; print fallback/error diagnostics. |
Numeric vector of length n.
On some builds, r_check_stack_opencl() can fail in device compilation or
runtime due to missing host/runtime stack symbols. Use as linkage smoke only,
and keep CPU fallback enabled unless explicitly debugging OpenCL failures.
n <- 5L if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { r_check_user_interrupt_opencl(n, fallback = FALSE, verbose = TRUE) # Known linkage/runtime gap on some setups (stack hook symbol availability): # r_check_stack_opencl(n, fallback = FALSE, verbose = TRUE) } else { as.numeric(seq_len(n)) }n <- 5L if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { r_check_user_interrupt_opencl(n, fallback = FALSE, verbose = TRUE) # Known linkage/runtime gap on some setups (stack hook symbol availability): # r_check_stack_opencl(n, fallback = FALSE, verbose = TRUE) } else { as.numeric(seq_len(n)) }
Wrappers for low-level Mathlib runtime helpers used by translated kernels.
r_pow_opencl(x, y, fallback = FALSE, verbose = FALSE) r_pow_di_opencl(x, n_exp, fallback = FALSE, verbose = FALSE) log1pmx_opencl(x, fallback = FALSE, verbose = FALSE) log1pexp_opencl(x, fallback = FALSE, verbose = FALSE) log1mexp_opencl(x, fallback = FALSE, verbose = FALSE) lgamma1p_opencl(x, fallback = FALSE, verbose = FALSE) pow1p_opencl(x, y, fallback = FALSE, verbose = FALSE) logspace_add_opencl(logx, logy, fallback = FALSE, verbose = FALSE) logspace_sub_opencl(logx, logy, fallback = FALSE, verbose = FALSE) logspace_sum_opencl(logx, logy, fallback = FALSE, verbose = FALSE)r_pow_opencl(x, y, fallback = FALSE, verbose = FALSE) r_pow_di_opencl(x, n_exp, fallback = FALSE, verbose = FALSE) log1pmx_opencl(x, fallback = FALSE, verbose = FALSE) log1pexp_opencl(x, fallback = FALSE, verbose = FALSE) log1mexp_opencl(x, fallback = FALSE, verbose = FALSE) lgamma1p_opencl(x, fallback = FALSE, verbose = FALSE) pow1p_opencl(x, y, fallback = FALSE, verbose = FALSE) logspace_add_opencl(logx, logy, fallback = FALSE, verbose = FALSE) logspace_sub_opencl(logx, logy, fallback = FALSE, verbose = FALSE) logspace_sum_opencl(logx, logy, fallback = FALSE, verbose = FALSE)
x |
Numeric vector(s): primary input, recycled together like |
y |
Numeric vector for |
fallback |
When |
verbose |
Logical; print fallback/error diagnostics. |
n_exp |
Integer vector for |
logx, logy
|
Numeric vectors for log-space combination helpers (recycled together). |
On the GPU path, arguments are recycled to a common length len (maximum
argument length, R recycling rules). Each output index runs one scalar kernel launch.
Numeric vector of the recycled common length (see Details).
if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { r_pow_opencl(x = 1.2, y = 2, fallback = FALSE, verbose = TRUE) r_pow_di_opencl(x = 1.2, n_exp = 3L, fallback = FALSE, verbose = TRUE) log1pmx_opencl(x = 0.2, fallback = FALSE, verbose = TRUE) log1pexp_opencl(x = 0.2, fallback = FALSE, verbose = TRUE) log1mexp_opencl(x = 0.5, fallback = FALSE, verbose = TRUE) lgamma1p_opencl(x = 0.2, fallback = FALSE, verbose = TRUE) pow1p_opencl(x = 0.2, y = 3, fallback = FALSE, verbose = TRUE) logspace_add_opencl(logx = -2, logy = -3, fallback = FALSE, verbose = TRUE) logspace_sub_opencl(logx = -2, logy = -3, fallback = FALSE, verbose = TRUE) logspace_sum_opencl(logx = -2, logy = -3, fallback = FALSE, verbose = TRUE) log1pmx_opencl(x = seq(-0.5, 0.5, by = 0.25), fallback = FALSE, verbose = TRUE) } else { (1.2 + seq_len(1L) * 1e-3)^2 1.2^3 log1p(0.2) - 0.2 ifelse(0.2 > 0, 0.2 + log1p(exp(-0.2)), log1p(exp(0.2))) ifelse(0.5 <= log(2), log(-expm1(-0.5)), log1p(-exp(-0.5))) lgamma(1.2) exp(3 * log1p(0.2)) { m <- max(-2, -3) m + log1p(exp(min(-2, -3) - m)) } -2 + log1p(-exp(-3 - (-2))) { m <- max(-2, -3) m + log1p(exp(min(-2, -3) - m)) } { xv <- seq(-0.5, 0.5, by = 0.25) log1p(xv) - xv } }if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { r_pow_opencl(x = 1.2, y = 2, fallback = FALSE, verbose = TRUE) r_pow_di_opencl(x = 1.2, n_exp = 3L, fallback = FALSE, verbose = TRUE) log1pmx_opencl(x = 0.2, fallback = FALSE, verbose = TRUE) log1pexp_opencl(x = 0.2, fallback = FALSE, verbose = TRUE) log1mexp_opencl(x = 0.5, fallback = FALSE, verbose = TRUE) lgamma1p_opencl(x = 0.2, fallback = FALSE, verbose = TRUE) pow1p_opencl(x = 0.2, y = 3, fallback = FALSE, verbose = TRUE) logspace_add_opencl(logx = -2, logy = -3, fallback = FALSE, verbose = TRUE) logspace_sub_opencl(logx = -2, logy = -3, fallback = FALSE, verbose = TRUE) logspace_sum_opencl(logx = -2, logy = -3, fallback = FALSE, verbose = TRUE) log1pmx_opencl(x = seq(-0.5, 0.5, by = 0.25), fallback = FALSE, verbose = TRUE) } else { (1.2 + seq_len(1L) * 1e-3)^2 1.2^3 log1p(0.2) - 0.2 ifelse(0.2 > 0, 0.2 + log1p(exp(-0.2)), log1p(exp(0.2))) ifelse(0.5 <= log(2), log(-expm1(-0.5)), log1p(-exp(-0.5))) lgamma(1.2) exp(3 * log1p(0.2)) { m <- max(-2, -3) m + log1p(exp(min(-2, -3) - m)) } -2 + log1p(-exp(-3 - (-2))) { m <- max(-2, -3) m + log1p(exp(min(-2, -3) - m)) } { xv <- seq(-0.5, 0.5, by = 0.25) log1p(xv) - xv } }
OpenCL-backed linkage wrapper for multinomial sampling. The current OpenCL
kernel supports a 2-category multinomial parameterization via scalar
prob, and returns a 2 x n count matrix.
rmultinom_opencl(n, size, prob, fallback = FALSE, verbose = FALSE)rmultinom_opencl(n, size, prob, fallback = FALSE, verbose = FALSE)
n |
Number of observations. Non-negative integer scalar. |
size |
Number of trials per draw (non-negative integer scalar). |
prob |
Probability of the first category in |
fallback |
When |
verbose |
Logical; print fallback/error diagnostics. |
Integer matrix with 2 rows and n columns.
n <- 5L if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { rmultinom_opencl(n, size = 12L, prob = 0.4, fallback = FALSE, verbose = TRUE) } else { stats::rmultinom(n, size = 12L, prob = c(0.4, 0.6)) }n <- 5L if (!nmathopencl_has_opencl() || identical(Sys.getenv("NOT_CRAN"), "true")) { rmultinom_opencl(n, size = 12L, prob = 0.4, fallback = FALSE, verbose = TRUE) } else { stats::rmultinom(n, size = 12L, prob = c(0.4, 0.6)) }
Run the file-level @depends sort without requiring full success. Files
that can be sorted are copied in order, and files blocked by unresolved
dependencies are copied into a separate folder with CSV reports.
stage_kernel_dependency_sort(library_dir, output_dir, overwrite = FALSE)stage_kernel_dependency_sort(library_dir, output_dir, overwrite = FALSE)
library_dir |
Directory containing |
output_dir |
Directory where sorted and unresolved files/reports should be written. |
overwrite |
Logical; remove and recreate |
A named list with:
sortedData frame of files placed in dependency order
(order, pass, file, source_type, depends).
unresolvedData frame of files blocked by missing dependencies.
sorted_dir, unresolved_dir
Output directory paths; CSV
reports ‘sorted_files.csv’ and ‘unresolved_files.csv’ are written
under output_dir.
############################ Start of stage_kernel_dependency_sort example ######################## lib_dir <- system.file("cl/ex_glmbayes_nmath", package = "opencltools") output_dir <- tempfile("stage_sort") on.exit(unlink(output_dir, recursive = TRUE), add = TRUE) res <- stage_kernel_dependency_sort(lib_dir, output_dir, overwrite = TRUE) nrow(res$sorted) length(list.files(output_dir, recursive = TRUE)) ############################################################################### ## End of stage_kernel_dependency_sort example ########################################################################################################### Start of stage_kernel_dependency_sort example ######################## lib_dir <- system.file("cl/ex_glmbayes_nmath", package = "opencltools") output_dir <- tempfile("stage_sort") on.exit(unlink(output_dir, recursive = TRUE), add = TRUE) res <- stage_kernel_dependency_sort(lib_dir, output_dir, overwrite = TRUE) nrow(res$sorted) length(list.files(output_dir, recursive = TRUE)) ############################################################################### ## End of stage_kernel_dependency_sort example ###############################################################################
Copies generic OpenCL configure and configure.win scripts to
the root directory of a package. The scripts detect CL/cl.h and
libOpenCL at compile time and generate src/Makevars (Linux /
macOS) or src/Makevars.win (Windows) with or without
-DUSE_OPENCL, depending on what is found.
The scripts always succeed. When no OpenCL SDK is present they
produce a CPU-only Makevars with no -lOpenCL. This is the key
property that makes packages safe for CRAN submission without requiring a
GPU SDK on the build machine.
use_opencl_configure(path = ".", overwrite = FALSE)use_opencl_configure(path = ".", overwrite = FALSE)
path |
Character. Root directory of the target package. Defaults to
the current working directory ( |
overwrite |
Logical. If |
Invisibly returns a character vector of the file paths that were written (empty if all files were skipped).
A package that references -lOpenCL or CL/cl.h in a static
src/Makevars will fail to compile on 'CRAN' build machines
(which have no GPU SDK installed), and no binary will be produced. The
configure scripts here avoid this by probing for the SDK at install time and
falling back to a CPU-only build when it is absent. The relationship is:
configure / configure.win
-> detects CL/cl.h + libOpenCL
-> writes -DUSE_OPENCL into Makevars (or omits it)
#ifdef USE_OPENCL in C++ source
-> guards all GPU code; package compiles cleanly either way
has_opencl() in R
-> mirrors the compile-time flag; returns TRUE only if USE_OPENCL was set
port_to_opencl_configure for packages with an existing static
src/Makevars.
opencltoolsLdFlags for linking against this package from C++.
vignette("Chapter-02", package = "nmathopencl") in nmathopencl for a
full downstream package guide when using the ported nmath kernel library.
Template source: system.file("configure-templates", package = "opencltools").
############################ Start of use_opencl_configure example ######################## tmp <- tempfile("use_opencl_pkg") dir.create(tmp) on.exit(unlink(tmp, recursive = TRUE), add = TRUE) written <- use_opencl_configure(path = tmp) written file.exists(file.path(tmp, "configure")) file.exists(file.path(tmp, "configure.win")) ## Overwrite path (same temp tree; safe when run.dontest = TRUE) written2 <- use_opencl_configure(path = tmp, overwrite = TRUE) length(written2) ############################################################################### ## End of use_opencl_configure example ########################################################################################################### Start of use_opencl_configure example ######################## tmp <- tempfile("use_opencl_pkg") dir.create(tmp) on.exit(unlink(tmp, recursive = TRUE), add = TRUE) written <- use_opencl_configure(path = tmp) written file.exists(file.path(tmp, "configure")) file.exists(file.path(tmp, "configure.win")) ## Overwrite path (same temp tree; safe when run.dontest = TRUE) written2 <- use_opencl_configure(path = tmp, overwrite = TRUE) length(written2) ############################################################################### ## End of use_opencl_configure example ###############################################################################
Writes two companion index files next to the .cl files in the kernel
library directory:
write_kernel_dependency_index( library_dir = NULL, tags = NULL, output_path = NULL, write = TRUE, verbose = FALSE )write_kernel_dependency_index( library_dir = NULL, tags = NULL, output_path = NULL, write = TRUE, verbose = FALSE )
library_dir |
Library root with annotated |
tags |
Optional attachment result from attach_kernel_dependency_tags. |
output_path |
Path for the RDS file. Defaults to
|
write |
If |
verbose |
If |
RDS helper shard for loaders such as load_library_for_kernel.
Tab-separated (.tsv) stem map for C++ (rows stem<TAB>dependencies, pre-sorted).
Both files encode the same information and are always written together so they remain in sync.
Invisibly, the index list() (version schema):
version: integer schema version.
generated_at: timestamp from Sys.time().
library_dir, library_name: resolved library path / basename.
stems_ordered: stems in global load order.
load_order: named integer vector stem -> rank.
depends: named list stem -> character() (direct @depends).
all_depends: named list stem -> character() (transitive dependencies, order
consistent with global load order).
n_files: file count.
Other OpenCL kernel library subsets:
extract_library_subset(),
load_library_for_kernel()
############################ Start of write_kernel_dependency_index example ######################## lib_dir <- system.file("cl/ex_glmbayes_nmath", package = "opencltools") idx <- write_kernel_dependency_index(library_dir = lib_dir, write = FALSE) names(idx) length(idx$stems_ordered) idx$n_files ############################################################################### ## End of write_kernel_dependency_index example ########################################################################################################### Start of write_kernel_dependency_index example ######################## lib_dir <- system.file("cl/ex_glmbayes_nmath", package = "opencltools") idx <- write_kernel_dependency_index(library_dir = lib_dir, write = FALSE) names(idx) length(idx$stems_ordered) idx$n_files ############################################################################### ## End of write_kernel_dependency_index example ###############################################################################