Skip to content
API REFERENCEUsage guides →

cellpack::loader::Loader ​

struct

Module contract · All interfaces

At a glance ​

pub struct Loader { /* private fields */ }

One data source and the schemas that output its cells, ready to serve runs.

The tables of a loader are immutable once built: the per-chunk output maps and the conversion kernels are composed then, so Loader::prepare only binds an access sequence and a ring. It serves one run at a time (see Error::Busy) and the run borrows it, so the run must drop before the loader can prepare the next run.

Use when repeated requests should be delivered in order by concurrent workers. Build a source, define schemas, then prepare a run with accesses and a ring.

Try it ​

FIXED EXAMPLE RUST / CPU

Deliver ordered batches

Two output schemas share each requested position. Missing dimensions use the schema default; present sparse zeros stay zero.

mod common;

use std::error::Error;
use std::sync::Arc;
use cellpack::loader::{
    DataSourceBuilder, Dtype, IoBackend, Loader, LoaderConfig,
    OutputSchema, Planning, Replicate, Ring,
};

fn main() -> Result<(), Box<dyn Error>> {
    let mut builder = DataSourceBuilder::new(5, 4096)?.direct_io(false);
    builder.add_cpk(common::fixture()?, &[0, 1, 2, 3])?;
    let source = Arc::new(builder.build()?);
    let schemas = vec![
        OutputSchema::dense(1, vec![3, 1, 4], Dtype::F32)?.with_default(f64::NAN),
        OutputSchema::dense(2, vec![0, 2], Dtype::F64)?,
    ];
    let loader = Loader::new(source, schemas, LoaderConfig {
        workers: 2, backend: IoBackend::Pread, slot_cells: 2,
        ..LoaderConfig::default()
    })?;
    let cells = [3u32, 0, 2, 3, 1];
    let accesses = Replicate::new(cells, 2)?;
    let ring = Arc::new(Ring::allocate(loader.ring_layout(2, 1))?);
    let run = loader.prepare(&accesses, ring, Planning::None)?;
    let stats = run.for_each_batch(|batch| {
        let panel: Vec<f32> = batch.region(0).chunks_exact(4)
            .map(|v| f32::from_ne_bytes(v.try_into().unwrap())).collect();
        println!("positions {}..{}: panel {panel:?}",
            batch.first_position(), batch.first_position() + batch.rows() as u64);
        Ok(())
    })?;
    println!("delivered: {} positions, {} accesses", cells.len(), stats.counters.accesses);
    Ok(())
}
OUTPUTExpected result
positions 0..2: panel [0.0, 8.0, NaN, 1.0, 2.0, NaN]
positions 2..4: panel [6.0, 0.0, NaN, 0.0, 8.0, NaN]
positions 4..5: panel [0.0, 0.0, NaN]
delivered: 5 positions, 10 accesses

Member index ​

17 of 17 members

  • newBinds schemas to source . Rejects a zero worker count, in-flight depth or claim batch, a window of zero, an in-flight depth past u32::MAX / 4 and CPU indices at or beyond CPU_SETSIZE ; the claim batch is clamped to one slot’s accesses ( slot_cells × schemas ), and a claim never crosses into the next slot anyway, since the open slot’s gate ends it. Graph::new also rejects an empty schema list, duplicate schema ids, output dims outside the vocabulary, a built-in cell size that differs from its dense row, slot_cells outside 1..2^32 , and an input that changed since registration.
  • sourceThe inputs the loader reads: every matrix, its cell ids and page map.
  • schemasThe output schemas, in loader order: schema k receives access position × S + k and writes region k of the slot.
  • accesses_per_slotAccesses per slot: every schema of every position.
  • windowThe execution window in slots: the configured one, or enough slots for every access in flight. It is the default for runs without a plan and for the reuse gap of plans built by Self::plan_options .
  • ring_layoutThe ring layout of this loader’s outputs.
  • coresPhysical cores the workers run on: the cores among the pinned CPUs (or the process’s allowed CPUs), at most one per worker. A cost model’s CPU capacity.
  • plan_optionsPlan options with this loader’s window as the reuse gap and the default cost model ( CostModel::defaults with crate::opt::defaults::Tier::Nvme ) on its cores, to be replaced by measurements on the target machine (pycellpack’s costs ). The per-schema builtin flags follow schema order.
  • compileCompiles a cache plan for a stored sequence of accesses (see crate::opt ).
  • replayReplays a plan serially against this loader’s inputs and schemas.
  • replay_with_costPrices the same actions with measured costs, without changing the plan or recompiling its cache decisions.
  • compile_onlineCompiles a stored sequence with the online compiler (a finite horizon, chunked freezing) into a plan verified like an offline one: to inspect or compare what online runs do.
  • plan_onlineCompiles a sequence online without loading it, for the statistics of the plan an online run would follow (its modeled time among them).
  • onlineChecks plan options for this loader — including that the configured window does not exceed their reuse gap — and returns them as the run’s planning. The compiler’s threads run beside the workers ( options.threads is used as given).
  • prepareValidates the input files, the ring’s layout, the access count and the plan binding, then opens the ring’s slots under the next run generation and returns the run. Fails with Error::Busy while another run holds the loader or the ring has a producer, and rejects a ring that was aborted or still holds ready or leased slots.
  • memory_estimateEstimates a page-only run with default planning options and this cache budget (0: no plan). Use memory_estimate_for for a specific plan, including zero-budget plans.
  • memory_estimate_forKnown reservations for one run under this planning, with the window and chunk size the loader’s config and the options resolve to. This does not bind a fixed plan to an access sequence.

Methods and associated constants ​

new ​

pub fn new(
    source: Arc<DataSource>,
    schemas: Vec<OutputSchema>,
    config: LoaderConfig,
) -> Result<Self>

Binds schemas to source. Rejects a zero worker count, in-flight depth or claim batch, a window of zero, an in-flight depth past u32::MAX / 4 and CPU indices at or beyond CPU_SETSIZE; the claim batch is clamped to one slot’s accesses (slot_cells × schemas), and a claim never crosses into the next slot anyway, since the open slot’s gate ends it. Graph::new also rejects an empty schema list, duplicate schema ids, output dims outside the vocabulary, a built-in cell size that differs from its dense row, slot_cells outside 1..2^32, and an input that changed since registration.

Parameters ​

ParameterMeaning and constraints
sourceShared immutable registered matrices and their metadata.
schemasOne or more output declarations with distinct schema IDs, in delivery order.
configWorker count, I/O backend, slot size, and execution window.

source ​

pub fn source(&self) -> &Arc<DataSource>

The inputs the loader reads: every matrix, its cell ids and page map.

schemas ​

pub fn schemas(&self) -> &[OutputSchema]

The output schemas, in loader order: schema k receives access position × S + k and writes region k of the slot.

accesses_per_slot ​

pub fn accesses_per_slot(&self) -> u64

Accesses per slot: every schema of every position.

window ​

pub fn window(&self) -> u64

The execution window in slots: the configured one, or enough slots for every access in flight. It is the default for runs without a plan and for the reuse gap of plans built by Self::plan_options.

ring_layout ​

pub fn ring_layout(&self, slots: u32, world: u32) -> RingLayout

The ring layout of this loader’s outputs.

cores ​

pub fn cores(&self) -> f64

Physical cores the workers run on: the cores among the pinned CPUs (or the process’s allowed CPUs), at most one per worker. A cost model’s CPU capacity.

plan_options ​

pub fn plan_options(&self, cache_bytes: usize) -> PlanOptions

Plan options with this loader’s window as the reuse gap and the default cost model (CostModel::defaults with crate::opt::defaults::Tier::Nvme) on its cores, to be replaced by measurements on the target machine (pycellpack’s costs). The per-schema builtin flags follow schema order.

Parameters ​

ParameterMeaning and constraints
cache_bytesFrame capacity in bytes. This is not a bound on total process memory.

compile ​

pub fn compile(
    &self,
    accesses: &[CellId],
    options: &PlanOptions,
) -> Result<Plan>

Compiles a cache plan for a stored sequence of accesses (see crate::opt).

replay ​

pub fn replay(&self, plan: &Plan, accesses: &[CellId]) -> Result<PlanSummary>

Replays a plan serially against this loader’s inputs and schemas.

replay_with_cost ​

pub fn replay_with_cost(
    &self,
    plan: &Plan,
    accesses: &[CellId],
    cost: &CostModel,
) -> Result<PlanSummary>

Prices the same actions with measured costs, without changing the plan or recompiling its cache decisions.

compile_online ​

pub fn compile_online(
    &self,
    accesses: &[CellId],
    options: &PlanOptions,
) -> Result<(Plan, OnlineStats)>

Compiles a stored sequence with the online compiler (a finite horizon, chunked freezing) into a plan verified like an offline one: to inspect or compare what online runs do.

plan_online ​

pub fn plan_online(
    &self,
    accesses: &dyn Accesses,
    options: &PlanOptions,
) -> Result<OnlineStats>

Compiles a sequence online without loading it, for the statistics of the plan an online run would follow (its modeled time among them).

online ​

pub fn online(&self, options: PlanOptions) -> Result<Planning>

Checks plan options for this loader — including that the configured window does not exceed their reuse gap — and returns them as the run’s planning. The compiler’s threads run beside the workers (options.threads is used as given).

prepare ​

pub fn prepare<'a>(
    &'a self,
    accesses: &'a dyn Accesses,
    ring: Arc<Ring>,
    planning: Planning,
) -> Result<Run<'a>>

Validates the input files, the ring’s layout, the access count and the plan binding, then opens the ring’s slots under the next run generation and returns the run. Fails with Error::Busy while another run holds the loader or the ring has a producer, and rejects a ring that was aborted or still holds ready or leased slots.

Parameters ​

ParameterMeaning and constraints
accessesDeterministic global cell IDs. Length must be divisible by the schema count. The run borrows this sequence.
ringShared output storage with a layout matching this loader and no outstanding earlier leases.
planningNo cache plan, an exact compatible offline plan, or online compiler options.

Result ​

A prepared run borrowing this loader and sequence. Use for_each_batch to overlap production and consumption.

memory_estimate ​

pub fn memory_estimate(
    &self,
    cache_bytes: usize,
    slots: u32,
    world: u32,
) -> Result<MemoryEstimate>

Estimates a page-only run with default planning options and this cache budget (0: no plan). Use memory_estimate_for for a specific plan, including zero-budget plans.

memory_estimate_for ​

pub fn memory_estimate_for(
    &self,
    planning: &Planning,
    slots: u32,
    world: u32,
) -> Result<MemoryEstimate>

Known reservations for one run under this planning, with the window and chunk size the loader’s config and the options resolve to. This does not bind a fixed plan to an access sequence.

Contract reference ​

See the module reference for semantic input/output rules, units, allocation, mutation, failure behavior, and lifetime requirements. Declarations and documented members are synchronized from the crate source.

Definition