Learn R Programming

shard

Deterministic, zero-copy parallel execution for R.

shard is a parallel runtime for workloads that look like:

  • "run the same numeric kernel over many slices of big data"
  • "thousands of independent tasks over a shared dataset"
  • "parallel GLM / simulation / bootstrap / feature screening"

It focuses on three things that are often painful in R parallelism:

  1. Shared immutable inputs (avoid duplicating large objects across workers)
  2. Explicit output buffers (avoid huge result-gather lists)
  3. Deterministic cleanup (supervise workers and recycle on memory drift)

Installation

From CRAN (once released):

install.packages("shard")

Development version:

# install.packages("pak")
pak::pak("bbuchsbaum/shard")

Core concepts

1) Share large inputs (read-only by default)

X <- shard::share(X)   # matrix/array/vector
Y <- shard::share(Y)

Shared objects are designed for zero-copy parallel reads (where the OS allows) and are treated as immutable by default inside parallel tasks.

2) Allocate explicit output buffers

Instead of returning giant objects from each worker, write to a preallocated buffer:

out <- shard::buffer("double", dim = c(1e6))   # example: 1M outputs

3) Run shard_map() over shards

blocks <- shard::shards(1e6, block_size = "auto")

run <- shard::shard_map(
  blocks,
  borrow = list(X = X, Y = Y),
  out = list(out = out),
  workers = 8,
  fun = function(block, X, Y, out) {
    # block contains indices
    idx <- block$idx
    out[idx] <- colMeans(Y[, idx, drop = FALSE])
  }
)

shard::report(run)

Safety defaults (and why they matter)

Borrowed inputs are immutable

By default, trying to mutate borrowed/shared inputs is treated as a bug:

  • cow = "deny" (default): mutation triggers an error
  • cow = "audit": detect and flag (best-effort; platform dependent)
  • cow = "allow": allow copy-on-write, track it, and enforce budgets

Why default is deny:

  • Prevents silent memory blowups from accidental wide writes
  • Prevents subtle correctness bugs (changes are private to a worker)
  • Keeps behavior predictable across platforms

Deterministic cleanup via worker supervision

R's GC and allocator behavior can lead to memory drift in long-running workers.

shard monitors per-worker memory usage and can recycle workers when drift exceeds thresholds, keeping end-of-run memory close to baseline.


Diagnostics

After a run, shard can report:

  • total and per-worker peak RSS
  • end RSS vs baseline
  • materialized bytes (hidden copies)
  • recycling events, retries, timing
rep <- shard::report(run)
print(rep)

shard::mem_report(run)
shard::copy_report(run)

Convenience wrappers

If your workload is “apply a function over columns” or “lapply over a list”, shard provides convenience wrappers that handle sharing and buffering automatically while still running through the supervised runtime.

Column-wise apply (scalar return)

X <- matrix(rnorm(1e6), nrow = 1000)

scores <- shard::shard_apply_matrix(
  X,
  MARGIN = 2,
  FUN = function(v, y) cor(v, y),
  VARS = list(y = rnorm(nrow(X))),
  workers = 8
)

List lapply (guarded gather)

xs <- lapply(1:1000, function(i) rnorm(100))

out <- shard::shard_lapply_shared(
  xs,
  FUN = function(el) mean(el),
  workers = 8
)

For large outputs (big vectors/data.frames per element), prefer buffer(), table_sink(), or shard_reduce() instead of gathering everything to the master.



License

MIT

Copy Link

Version

Install

install.packages('shard')

Version

0.1.0

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Bradley Buchsbaum

Last Published

April 3rd, 2026

Functions in shard (0.1.0)

buffer_close

Close a Buffer
buffer_open

Open an Existing Buffer
attributes<-.shard_shared_vector

Set Attributes on a Shared Vector
buffer_info

Get Buffer Info
buffer_path

Get Buffer Path
buffer_diagnostics

Buffer Diagnostics
buffer

Shared Memory Buffers
buffer_advise

Advise access pattern for a buffer
close.shard_shared

Close a Shared Object
cow_report

Copy-on-Write Policy Report
dim<-.shard_shared_vector

Set dim on a Shared Vector
collect.shard_row_groups

Collect a row-groups handle into memory
available_backings

Get available shared memory backing types
collect

Collect a shard table into memory
coltypes

Column Types
copy_report

Data Copy Report
diagnostics

Diagnostics API
dim.shard_buffer

Dimensions of a Shared Memory Buffer
collect.shard_dataset

Collect a dataset handle into memory
idx_range

Contiguous index range
in_arena

Check if Currently Inside an Arena
collect.shard_table_handle

Collect a table handle into memory
dimnames<-.shard_shared_vector

Set dimnames on a Shared Vector
factor_col

Categorical column type
dispatch

Task Dispatch Engine
fetch

Fetch Data from a Shared Object
list_kernels

List registered kernels
dispatch_chunks

Dispatch Chunks to Worker Pool
ergonomics

Ergonomic Apply/Lapply Wrappers
length.shard_descriptor

Length of a shard_descriptor Object
length.shard_descriptor_lazy

Length of a shard_descriptor_lazy Object
materialize

Materialize Shared Object
iterate_row_groups

Iterate row groups
pool

Worker Pool Management
length.shard_buffer

Length of a Shared Memory Buffer
pin_workers

Pin shard workers to CPU cores
pool_stop

Stop the Worker Pool
pool_sapply

Parallel sapply with Supervision
print.shard_apply_policy

Print a shard_apply_policy Object
print.arena_result

Print an arena_result object
pool_status

Get Pool Status
pool_lapply

Parallel Dispatch with Async Workers
print.shard_deep_shared

Print a Deep-Shared Object
pool_dispatch

Dispatch Task to Worker
print.shard_buffer

Print a Shared Memory Buffer
materialize.shard_view_block

Materialize a block view into an R matrix
materialize.shard_view_gather

Materialize a gather view into an R matrix
pool_create

Create a Worker Pool
print.shard_idx_range

Print a shard_idx_range object
print.shard_pool

Print a shard_pool Object
print.shard_view_block

Print a shard_view_block object
print.shard_tiles

Print a shard_tiles object
pool_get

Get the Current Worker Pool
pool_health_check

Check Pool Health
print.shard_descriptor

Print a shard_descriptor Object
print.shard_shared

Print a Shared Object
print.shard_shared_vector

Print a Shared Vector
print.shard_view_gather

Print a shard_view_gather object
is_shared

Check if Object is Shared
rss

RSS Monitoring Utilities
print.shard_result

Print a shard_result Object
schema

Define a table schema
print.shard_segment

Print a Shared Memory Segment
is_shared_vector

Check if an object is a shared vector
is_view

View Predicates
is_windows

Check if running on Windows
print.shard_descriptor_lazy

Print a shard_descriptor_lazy Object
scratch_diagnostics

Scratch pool diagnostics
scratch_matrix

Get a scratch matrix
queue

Chunk Queue Management
recommendations

Performance Recommendations
segment_open

Open an existing shared memory segment
segment_path

Get the path or name of a segment
segment_write

Write data to a segment
print.shard_worker

Print a shard_worker Object
scratch_pool_config

Configure scratch pool limits
set_affinity

Set CPU affinity for the current process
results

Extract Results from shard_map
row_layout

Row layout for fixed-row table outputs
shard_apply_policy

Apply Wrapper Policy
segment_close

Close a shared memory segment
segment_advise

Advise OS about expected access pattern for a segment
mem_report

Memory Usage Report
shard_unregister_adapter

Unregister an Adapter
shard_share_hook

Deep Sharing Hook for Custom Classes
shards

Shard Descriptor Creation
shard_apply_matrix

Apply a Function Over Matrix Columns with Shared Inputs
shard-package

shard: Deterministic, Zero-Copy Parallel Execution for R
names<-.shard_shared_vector

Set Names on a Shared Vector
shards_list

Create Shards from an Explicit Index List
print.shard_dispatch_result

Print a shard_dispatch_result Object
shard_list_adapters

List Registered Adapters
shard_map

Parallel Execution with shard_map
shared_segment

Get the underlying segment from a shared vector
segment

Shared Memory Segment
segment_report

Shared Memory Segment Report
shard_lapply_shared

Apply a Function Over a List with Optional Auto-Sharing
shared_info

Get Information About a Shared Object
segment_size

Get the size of a segment
shard_get_adapter

Get Adapter for an Object
shared_advise

Advise access pattern for a shared input vector/matrix
shared_diagnostics

Get diagnostics for a shared vector
shard_crossprod

Parallel crossprod() using shard views + output buffers
stream_group_sum

Stream group-wise sum
shared_vector

Create a shared vector from a segment
stream_map

Stream over row-groups/datasets and map
print.shard_health_report

Print a shard_health_report Object
segment_info

Get segment information
shared_reset_diagnostics

Reset diagnostic counters for a shared vector
report

Generate Shard Runtime Report
segment_create

Create a new shared memory segment
segment_read

Read raw data from a segment
segment_protect

Make a segment read-only
stream_group_count

Stream group-wise count
stream_filter

Stream-filter a dataset/row-groups into a new partitioned dataset
[<-.shard_buffer

Assign to Buffer Elements
register_kernel

Register a shard kernel
print.shard_report

Print a shard_report Object
print.shard_reduce_result

Print a shard_reduce_result Object
utils

Utility Functions
[[<-.shard_shared_vector

Double-bracket Subset-assign a Shared Vector
shard_reduce

Streaming Reductions over Shards
stream_sum

Stream sum of a numeric column
table_write

Write tabular results into a table buffer or sink
table_finalize

Finalize a table buffer or sink
view_block

Create a contiguous block view
stream_reduce

Stream over row-groups/datasets and reduce
table_finalize.shard_table_buffer

Finalize a table buffer
shared_view

Create a view (subset) of a shared vector
[<-.shard_shared_vector

Subset-assign a Shared Vector
[.shard_descriptor

Subset Shard Descriptor
[.shard_descriptor_lazy

Subset a shard_descriptor_lazy Object
stream_count

Stream row count
succeeded

Check if shard_map Succeeded
table_sink

Create a table sink for row-group or partitioned outputs
table_finalize.shard_table_sink

Finalize a sink
views

Zero-copy Views
view_diagnostics

View diagnostics
worker

Individual Worker Control
shard_register_adapter

Register an Adapter for Class-Specific Traversal
view

Create a view over a shared matrix
task_report

Task Execution Report
table_write.shard_table_sink

Write a shard's row-group output
[[.shard_descriptor_lazy

Extract a Single Shard from a shard_descriptor_lazy Object
[[.shard_descriptor

Get Single Shard
table_write.shard_table_buffer

Write into a table buffer
share

Zero-Copy Shared Objects
stream_top_k

Stream top-k rows by a numeric column
share_open

Open an Existing Shared Object by Path
table_buffer

Allocate a fixed-row table buffer
view_gather

Create a gather (indexed) view over a shared matrix
[.shard_buffer

Extract Buffer Elements
table_diagnostics

Table Diagnostics
view_info

Introspection for a view
as.array.shard_buffer

Coerce a Shared Memory Buffer to Array
as.integer.shard_buffer

Coerce a Shared Memory Buffer to Integer
arena

Arena Semantic Scope
as.double.shard_buffer

Coerce a Shared Memory Buffer to Double
adapter

Adapter Registry for Class-Specific Deep Sharing
affinity_supported

Check whether CPU affinity is supported
altrep

ALTREP Shared Vectors
arena_depth

Get Current Arena Depth
affinity

CPU Affinity + mmap Advice (Advanced)
as.logical.shard_buffer

Coerce a Shared Memory Buffer to Logical
as_tibble

Materialize a shard table handle as a data.frame/tibble
as_tibble.shard_dataset

Materialize a dataset handle into a data.frame/tibble
as.raw.shard_buffer

Coerce a Shared Memory Buffer to Raw
as_tibble.shard_row_groups

Materialize a row-groups handle into a data.frame/tibble
as.matrix.shard_buffer

Coerce a Shared Memory Buffer to Matrix
as_tibble.shard_table_buffer

Materialize a fixed table handle or buffer
as_shared

Create a shared vector from an existing R vector
as.vector.shard_buffer

Coerce a Shared Memory Buffer to a Vector
as_tibble.shard_table_handle

Materialize a table handle into a data.frame/tibble
attr<-.shard_shared_vector

Set an Attribute on a Shared Vector