Skip to main content

MemRanges

Struct MemRanges 

Source
pub struct MemRanges { /* private fields */ }
Expand description

Cumulative dataflow state for a sequence of concurrent dispatches.

Direct port of struct ggml_mem_ranges in ggml-metal-common.cpp:19-23. The state is reset every time a barrier is emitted; between barriers, all recorded dispatches are considered to run concurrently and their R/W ranges accumulate.

Implementations§

Source§

impl MemRanges

Source

pub fn new() -> Self

New empty state. Pre-allocates capacity matching llama.cpp’s reserve(256) (line 28).

Source

pub fn reset(&mut self)

Drop all recorded ranges (called after emitting a barrier). Mirrors ggml_mem_ranges_reset.

Source

pub fn len(&self) -> usize

Number of currently-recorded ranges (diagnostic).

Source

pub fn is_empty(&self) -> bool

Whether the cumulative state is empty.

Source

pub fn checks(&self) -> u64

Number of check() calls performed since construction (diagnostic, monotone).

Source

pub fn barriers_forced(&self) -> u64

Number of check() calls that returned false, forcing a barrier (diagnostic, monotone). When tracking is enabled at every dispatch, total_dispatches - barriers_forced == barriers elided versus the unconditional-barrier baseline.

Source

pub fn push(&mut self, range: BufferRange)

Push a single range onto the cumulative state without checking. Used internally by [Self::add] and Self::add_dispatch. Public so unit tests can construct adversarial states.

Source

pub fn add_dispatch(&mut self, reads: &[&MlxBuffer], writes: &[&MlxBuffer])

Record a dispatch’s read-buffer ranges + write-buffer ranges.

Mirrors ggml_mem_ranges_add(tensor) at ggml-metal-common.cpp:114-122: pushes one Src range per tensor->src[i] and one Dst range for tensor itself.

Caller is expected to have already invoked Self::check_dispatch and emitted a barrier on conflict; the barrier-emit + reset() is the responsibility of the integration site (typically CommandEncoder).

Source

pub fn check_dispatch( &mut self, reads: &[&MlxBuffer], writes: &[&MlxBuffer], ) -> bool

Check whether a candidate dispatch can run concurrently with the recorded state.

Returns true iff none of the candidate’s reads or writes conflict with any recorded range. Exactly mirrors ggml_mem_ranges_check(tensor) at ggml-metal-common.cpp:175-185: each src is checked against existing ranges, then the dst is checked against existing ranges.

Increments Self::checks. On false return, also increments Self::barriers_forced — so the diagnostic counter is accurate even when callers ignore the return value.

Source

pub fn check_and_record( &mut self, reads: &[&MlxBuffer], writes: &[&MlxBuffer], ) -> bool

Combined check + add. Returns true if the dispatch was added concurrent (no conflict, no barrier needed); returns false if the caller must emit a barrier and reset() before adding the dispatch’s ranges.

On false return, the caller’s responsibility is:

  1. Emit the underlying memoryBarrierWithScope: on the live encoder.
  2. Call Self::reset.
  3. Call Self::add_dispatch with the same reads/writes to seed the new concurrent group.

This mirrors the call pattern at ggml-metal-ops.cpp:220-225.

Trait Implementations§

Source§

impl Default for MemRanges

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.