shard_apply_matrix: Apply a Function Over Matrix Columns with Shared Inputs

View source: R/ergonomics.R

shard_apply_matrixR Documentation

Apply a Function Over Matrix Columns with Shared Inputs

Description

A convenience wrapper for the common "per-column apply" pattern. The matrix is shared once and each worker receives a zero-copy column view when possible.

Usage

shard_apply_matrix(
  X,
  MARGIN = 2,
  FUN,
  VARS = NULL,
  workers = NULL,
  ...,
  policy = shard_apply_policy()
)

Arguments

X

A numeric/integer/logical matrix (or a shared matrix created by share()).

MARGIN

Must be 2 (columns).

FUN

Function of the form ⁠function(v, ...)⁠ returning a scalar atomic.

VARS

Optional named list of extra variables. Large atomic VARS are auto-shared based on policy$auto_share_min_bytes.

workers

Number of workers (passed to shard_map()).

...

Additional arguments forwarded to FUN.

policy

A shard_apply_policy() object.

Details

Current limitation: MARGIN must be 2 (columns). Row-wise apply would require strided/gather slicing and is intentionally explicit in shard via views/kernels.

Value

An atomic vector of length ncol(X) with the results.

Examples


X <- matrix(rnorm(400), 20, 20)
shard_apply_matrix(X, MARGIN = 2, FUN = mean)
pool_stop()


shard documentation built on April 3, 2026, 9:08 a.m.