ras_memory: Estimate Memory Requirements and Check System Readiness

View source: R/utils.R

ras_memoryR Documentation

Estimate Memory Requirements and Check System Readiness

Description

Detects the current machine's RAM and CPU configuration, computes per-stage peak memory estimates for the RAS pipeline, and issues a go / no-go verdict.

Usage

ras_memory(
  n_total,
  n_train,
  n_holdout,
  n_snps,
  bytes_per_element = 8,
  abort = FALSE
)

Arguments

n_total

Integer. Total number of samples (rows of geno).

n_train

Integer. Number of training-split samples (typically n_total / 2).

n_holdout

Integer. Number of hold-out samples (rows of pgs.mat; typically n_total - n_train).

n_snps

Integer. Number of variants (columns of geno).

bytes_per_element

Numeric. Bytes per matrix element. Default 8 (R double).

abort

Logical. If TRUE and estimated peak memory exceeds available RAM, calls stop so the pipeline cannot proceed. Default FALSE.

Details

Peak memory is estimated for three pipeline stages:

Stage 1 — compute_gwas_weights

Holds the full geno matrix plus a small N \times 4 coefficient matrix. Peak \approx geno_mb + coefmat_mb.

Stage 2 — compute_pgs_matrix

Worst-case holds two copies of geno (R copy-on-modify triggered by geno[is.na(geno)] <- 0) plus the output pgs.mat. Peak \approx 2 * geno_mb + pgsmat_mb.

Stage 3 — screen_forward_max_region

Holds geno and pgs.mat simultaneously. Peak \approx geno_mb + pgsmat_mb.

The overall estimated peak is the maximum across the three stages.

Available RAM is queried via wmic on Windows and /proc/meminfo on Linux; the function degrades gracefully (prints a caution message and returns can_proceed = TRUE) if the query fails.

The function prints a formatted report to the console and invisibly returns the numeric estimates for programmatic use.

Value

Invisibly returns a named list with two elements:

memory

Named numeric vector with elements geno_mb, pgsmat_mb, coefmat_mb, stage1_peak_mb, stage2_peak_mb, stage3_peak_mb, overall_peak_mb, available_mb, total_mb.

system

Named list with elements cores_physical, cores_logical, r_version, platform, and can_proceed (logical: TRUE if RAM is sufficient or cannot be measured).

See Also

ras_scan, ras for the functions whose memory use is being estimated. release_memory to reclaim heap memory after each repetition.

Examples

## Check feasibility for 5,000 samples and 500,000 SNPs
ras_memory(
  n_total   = 5000,
  n_train   = 2500,
  n_holdout = 2500,
  n_snps    = 500000
)

## Abort if memory is insufficient (wrapped in try() so the example runs)
try(ras_memory(
  n_total   = 100000,
  n_train   = 50000,
  n_holdout = 50000,
  n_snps    = 1000000,
  abort     = TRUE
))

RAS documentation built on July 24, 2026, 5:06 p.m.