Skip to main content

Context

Trait Context 

Source
pub trait Context: Clone + Default {
    // Required method
    fn clone_with_nbatch(&self, nbatch: usize) -> Result<Self, LaError>;

    // Provided methods
    fn nbatch(&self) -> usize { ... }
    fn synchronize(&self) { ... }
    fn assert_broadcastable_into(&self, src_nbatch: usize, op: &str) { ... }
    fn vector_from_element<V: Vector<C = Self>>(
        &self,
        len: usize,
        value: V::T,
    ) -> V { ... }
    fn vector_from_vec<V: Vector<C = Self>>(&self, vec: Vec<V::T>) -> V { ... }
    fn vector_zeros<V: Vector<C = Self>>(&self, len: usize) -> V { ... }
    fn dense_mat_zeros<V: Vector<C = Self> + DefaultDenseMatrix>(
        &self,
        rows: usize,
        cols: usize,
    ) -> <V as DefaultDenseMatrix>::M { ... }
}
Expand description

defines the current execution and allocation context of an operator / vector / matrix for example:

  • threading model (e.g. single-threaded, multi-threaded, GPU)
  • custom allocators, host/device memory
  • etc.

It will generally be the case that all the operators / vectors / matrices for the current ode problem share the same context

Required Methods§

Source

fn clone_with_nbatch(&self, nbatch: usize) -> Result<Self, LaError>

Creates a new context with the given batch count.

Other properties of the context (e.g. CUDA stream, faer parallelism) are preserved.

Returns an error when a backend cannot create a requested batch context.

Provided Methods§

Source

fn nbatch(&self) -> usize

Returns the batch count for this context.

When nbatch > 1, vectors and matrices store data for nbatch independent ODE systems simultaneously. Operations between operands with different batch counts use broadcast semantics: an operand with nbatch == B is applied to B * P batches of the other operand, repeating each of its batches over P contiguous batches (nbatch == 1 is the B == 1 case, applied to every batch of the other operand).

Source

fn synchronize(&self)

Waits for any queued work to finish.

A no-op on the CPU backends, where every operation has already run by the time it returns. On a device backend the operations only enqueue, so anything timing them has to call this before reading the clock.

Source

fn assert_broadcastable_into(&self, src_nbatch: usize, op: &str)

Panics unless an operand holding src_nbatch batches broadcasts into self’s batches, i.e. unless self.nbatch() is an exact multiple of src_nbatch.

Every batched operation picks one operand whose batch count governs the result, and self is always that operand:

  • an owned operand has priority, because the result can be written into it – A + &B and A + B are governed by A, &A + B by B;
  • otherwise the left-hand side governs, and the result is a fresh allocation at its batch count – &A + &B is governed by A;
  • operations with a fixed destination are governed by it: assignments, copy_from, axpy, gemv into y, a linear solve in place.

The other operand is then broadcast into the governing count: each of its batches is repeated across dest_nbatch / src_nbatch contiguous destination batches.

Source

fn vector_from_element<V: Vector<C = Self>>(&self, len: usize, value: V::T) -> V

Source

fn vector_from_vec<V: Vector<C = Self>>(&self, vec: Vec<V::T>) -> V

Source

fn vector_zeros<V: Vector<C = Self>>(&self, len: usize) -> V

Source

fn dense_mat_zeros<V: Vector<C = Self> + DefaultDenseMatrix>( &self, rows: usize, cols: usize, ) -> <V as DefaultDenseMatrix>::M

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§