R/np.udist.process.cache.R

Defines functions .np_udist_cache_draws .np_udist_cache_chunk .np_udist_cache_prepare .np_udist_cache_plan

# Fit-local reuse of exact existing block innovations. The cap is independent
# of target count; larger jobs retain the incumbent bounded replay owner.
.np_udist_cache_plan <- function(n, B, m, tile, blocklen, budget, max.bytes,
                                seed.length) {
  payload <- 8 * as.double(n) * B
  # Tasks are disjoint across ranks: this bounds all retained/serialized
  # innovation copies across the pool, and also any single process.
  extra <- 64 * as.double(n) * B +
    128 * ceiling(B / 32) * (seed.length + 16)
  if (m <= tile || blocklen <= 1L || !is.finite(payload) ||
      payload > 16 * 1024^2 || !is.finite(extra) ||
      budget$bytes + extra > max.bytes)
    return(NULL)
  list(payload.bytes = payload, extra.bytes = extra,
       bytes = budget$bytes + extra,
       cap.bytes = 16 * 1024^2, draw.chunk = 32L)
}

.np_udist_cache_prepare <- function(n, B, law, blocklen) {
  starts <- seq.int(1L, B, by = 32L)
  tasks <- vector("list", length(starts))
  for (i in seq_along(starts)) {
    first <- starts[i]; size <- min(32L, B - first + 1L)
    entry <- get(".Random.seed", .GlobalEnv, inherits = FALSE)
    weights <- .np_bias_process_innovations(n, first, first + size - 1L,
                                           B, law, blocklen)
    tasks[[i]] <- list(start = first, bsz = size, seed = NA_integer_,
      rng_state = entry, weights = weights,
      rng_end = get(".Random.seed", .GlobalEnv, inherits = FALSE))
  }
  attr(tasks, "rng_final_state") <- get(".Random.seed", .GlobalEnv, inherits = FALSE)
  tasks
}

.np_udist_cache_chunk <- function(F, task) {
  value <- F %*% task$weights
  assign(".Random.seed", task$rng_end, .GlobalEnv)
  value
}

.np_udist_cache_draws <- function(F, tasks, B, law, blocklen) {
  out <- matrix(0, nrow(F), B)
  for (task in tasks) {
    assign(".Random.seed", task$rng_state, .GlobalEnv)
    jj <- seq.int(task$start, task$start + task$bsz - 1L)
    out[, jj] <- .np_udist_cache_chunk(F, task)
  }
  out
}

Try the np package in your browser

Any scripts or data that you put into this service are public.

np documentation built on Nov. 25, 2026, 1:06 a.m.