Appearance
cellpack::loader::Ring
Module contract · All interfaces
At a glance
pub struct Ring { /* private fields */ }A bounded ring over owned or caller-provided memory: the slot protocol and the participant bookkeeping that keep one producer and world consumer ranks apart.
Construction either allocates (allocate, with_buffer) or takes caller-provided memory (init, init_regions, attach, attach_regions); the latter take raw pointers unsafely, and the constructors that accept an owner keep it alive through an Arc. _owned holds memory this value allocated, _owner retains a caller’s; both drop after every lease, since a lease keeps the Arc<Ring> it came from.
Allocate from loader.ring_layout so schema extents and alignment agree. All readers must release their leases before a slot can be reused. An aborted ring needs replacement.
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(())
}
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 accessesMember index
28 of 28 members
allocateAllocates control and data together in huge pages.initInitializes contiguous storage.with_bufferAllocates control metadata and uses an external data array of at least RingLayout::data_min_bytes and at most RingLayout::data_bytes , aligned to at least 8 bytes. Regions start at REGION_ALIGN multiples within the array, so a 64-byte aligned array gives 64-byte aligned regions.init_regionsInitializes separate control and data (also suitable for IPC).attachAttaches to initialized contiguous shared storage.attach_regionsAttaches process-local pointers to initialized separate shared regions.layoutThe layout this ring was initialized or attached with.slot_cellsPositions per slot.schemasSchemas, one region each per slot.slot_strideBytes between consecutive slots.region_offsetOffset of schema s ’s region within a slot.cell_bytesBytes of one cell’s output for schema s .generationThe generation of the current or last run; 0 before the first run.abortAborts the ring: every wait fails with Error::Aborted and no later run can start on this ring.abortedWhether the ring was aborted; the flag is never cleared.waitAcquires one batch; dropping the guard returns its slot. None when the batch is past the end of the run or holds no position.acquireAcquires a batch whose storage and slot remain valid through an Arc; None as for Self::wait .acquire_generationAcquires a batch only from the specified run generation; None as for Self::wait .acquire_timeoutBounded wait for runtimes that need to poll cancellation or signals.join_consumerRegisters this process as rank rank ’s consumer of run generation (or of the next run). Fails for a rank outside world and when another live process consumes it.adopt_generationRecords the generation of the run a consumer’s first batch belongs to.leave_consumerThis process leaves rank rank , having taken all of its batches or not.join_producerRegisters this process as the producer of run generation ; rows that ended an earlier run describe nobody in this one.leave_producerThis process stops producing, successfully or not.producer_problemWhy run generation ’s producer can no longer finish it.consumer_problemWhy a consumer of run generation can no longer take its batches.consumed_elsewhereWhether a live process other than this one consumes rank rank of run generation .blockingBatches whose slots the producer of a run of batches batches still needs but that their consumers have not given back: ready and not taken ( false ), or still leased ( true ).
Methods and associated constants
allocate
pub fn allocate(layout: RingLayout) -> Result<Self>Allocates control and data together in huge pages.
init
Initializes contiguous storage.
Safety
Memory is exclusive during initialization, remains valid for this ring and its batches, and is accessed only through the slot protocol.
with_buffer
pub unsafe fn with_buffer(
data: *mut u8,
data_len: usize,
layout: RingLayout,
owner: Arc<dyn Send + Sync>,
) -> Result<Self>Allocates control metadata and uses an external data array of at least RingLayout::data_min_bytes and at most RingLayout::data_bytes, aligned to at least 8 bytes. Regions start at REGION_ALIGN multiples within the array, so a 64-byte aligned array gives 64-byte aligned regions.
Safety
The array remains allocated without reallocation while owner is alive. No aliases may access filling slots; ready slots follow the lease protocol.
init_regions
pub unsafe fn init_regions(
control: *mut u8,
control_len: usize,
data: *mut u8,
data_len: usize,
layout: RingLayout,
owner: Arc<dyn Send + Sync>,
) -> Result<Self>Initializes separate control and data (also suitable for IPC).
Safety
Regions are writable, exclusive during initialization, and retained by owner. All processes must follow this ring’s slot protocol.
attach
Attaches to initialized contiguous shared storage.
Safety
Storage remains valid and unmodified except through the slot protocol.
attach_regions
pub unsafe fn attach_regions(
control: *mut u8,
control_len: usize,
data: *mut u8,
data_len: usize,
owner: Arc<dyn Send + Sync>,
) -> Result<Self>Attaches process-local pointers to initialized separate shared regions.
Safety
Regions remain valid through owner and follow the slot protocol.
layout
pub fn layout(&self) -> RingLayoutThe layout this ring was initialized or attached with.
slot_cells
pub fn slot_cells(&self) -> u64Positions per slot.
schemas
Schemas, one region each per slot.
slot_stride
pub fn slot_stride(&self) -> usizeBytes between consecutive slots.
region_offset
pub fn region_offset(&self, s: usize) -> usizeOffset of schema s’s region within a slot.
cell_bytes
pub fn cell_bytes(&self, s: usize) -> usizeBytes of one cell’s output for schema s.
generation
pub fn generation(&self) -> u64The generation of the current or last run; 0 before the first run.
abort
pub fn abort(&self)Aborts the ring: every wait fails with Error::Aborted and no later run can start on this ring.
aborted
Whether the ring was aborted; the flag is never cleared.
wait
Acquires one batch; dropping the guard returns its slot. None when the batch is past the end of the run or holds no position.
acquire
Acquires a batch whose storage and slot remain valid through an Arc; None as for Self::wait.
acquire_generation
pub fn acquire_generation(
self: &Arc<Self>,
batch: u64,
generation: u64,
) -> Result<Option<BatchLease>>Acquires a batch only from the specified run generation; None as for Self::wait.
acquire_timeout
pub fn acquire_timeout(
self: &Arc<Self>,
batch: u64,
timeout: Duration,
) -> Result<Acquisition>Bounded wait for runtimes that need to poll cancellation or signals.
join_consumer
pub fn join_consumer(&self, rank: u32, generation: u64) -> Result<()>Registers this process as rank rank’s consumer of run generation (or of the next run). Fails for a rank outside world and when another live process consumes it.
adopt_generation
pub fn adopt_generation(&self, rank: u32, generation: u64)Records the generation of the run a consumer’s first batch belongs to.
leave_consumer
pub fn leave_consumer(&self, rank: u32, finished: bool)This process leaves rank rank, having taken all of its batches or not.
join_producer
pub fn join_producer(&self, generation: u64)Registers this process as the producer of run generation; rows that ended an earlier run describe nobody in this one.
leave_producer
pub fn leave_producer(&self, ok: bool)This process stops producing, successfully or not.
producer_problem
pub fn producer_problem(&self, generation: u64) -> Option<String>Why run generation’s producer can no longer finish it.
consumer_problem
pub fn consumer_problem(&self, generation: u64) -> Option<String>Why a consumer of run generation can no longer take its batches.
consumed_elsewhere
pub fn consumed_elsewhere(&self, rank: u32, generation: u64) -> boolWhether a live process other than this one consumes rank rank of run generation.
blocking
Batches whose slots the producer of a run of batches batches still needs but that their consumers have not given back: ready and not taken (false), or still leased (true).
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.