Skip to main content

ErasedScanPlan

Struct ErasedScanPlan 

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

Dtype-erased cumulative scan (cumsum / cumprod) along one axis.

The destination has the source dimensions; source and destination strides are independent, and negative strides and offsets are accepted. Supported dtypes are f32, f64, i32, i64, c32 and c64; bool is rejected.

§Evaluation order

Every output is the sequential left fold of its inputs in scan order (from the start of the axis, or from its end for a reverse scan), with no reassociation. The result is therefore bitwise identical for every layout, execution context and thread count. Integer scans wrap on overflow.

An empty axis or an empty set of lines writes nothing.

§Examples

use strided_basic::{
    ErasedRawStridedMut, ErasedRawStridedRef, ErasedScanPlan, ExecContext, KernelDType,
    ScanOp, ScanOptions,
};

// A column-major 3 x 2 matrix, scanned along axis 0.
let src = [1.0_f64, 2.0, 3.0, 10.0, 20.0, 30.0];
let mut out = [0.0_f64; 6];
let plan = ErasedScanPlan::compile(
    KernelDType::F64,
    ScanOp::Sum,
    &[3, 2],
    &[1, 3],
    &[1, 3],
    0,
    ScanOptions::new(),
)
.unwrap();
let src_ref = ErasedRawStridedRef::from_slice(&src, &[3, 2], &[1, 3], 0).unwrap();
let mut dest = ErasedRawStridedMut::from_slice_mut(&mut out, &[3, 2], &[1, 3], 0).unwrap();
plan.execute(&ExecContext::serial(), &mut dest, &src_ref).unwrap();
assert_eq!(out, [1.0, 3.0, 6.0, 10.0, 30.0, 60.0]);

Implementations§

Source§

impl ErasedScanPlan

Source

pub fn compile( dtype: KernelDType, op: ScanOp, dims: &[usize], src_strides: &[isize], dest_strides: &[isize], axis: usize, options: ScanOptions, ) -> Result<ErasedScanPlan, StridedError>

Validate and store a scan plan for one dtype and fixed layouts.

dims are the shared source and destination dimensions; axis is the scanned axis.

§Examples
use strided_basic::{ErasedScanPlan, KernelDType, ScanOp, ScanOptions};
let plan = ErasedScanPlan::compile(
    KernelDType::I64, ScanOp::Product, &[4], &[1], &[-1], 0, ScanOptions::new(),
)
.unwrap();
assert_eq!(plan.op(), ScanOp::Product);
assert!(ErasedScanPlan::compile(
    KernelDType::Bool, ScanOp::Sum, &[4], &[1], &[1], 0, ScanOptions::new(),
)
.is_err());
§Errors

Returns UnsupportedDType for bool, InvalidAxis for an axis out of range, StrideLengthMismatch for inconsistent ranks, NonInjectiveOutputLayout for an aliasing destination layout, and OffsetOverflow when a layout’s offsets are not representable.

Source

pub fn dtype(&self) -> KernelDType

Element dtype of the source and destination.

§Examples
use strided_basic::{ErasedScanPlan, KernelDType, ScanOp, ScanOptions};
let plan = ErasedScanPlan::compile(
    KernelDType::F32, ScanOp::Sum, &[2], &[1], &[1], 0, ScanOptions::new(),
)
.unwrap();
assert_eq!(plan.dtype(), KernelDType::F32);
Source

pub fn op(&self) -> ScanOp

Scan operation.

§Examples
use strided_basic::{ErasedScanPlan, KernelDType, ScanOp, ScanOptions};
let plan = ErasedScanPlan::compile(
    KernelDType::F32, ScanOp::Sum, &[2], &[1], &[1], 0, ScanOptions::new(),
)
.unwrap();
assert_eq!(plan.op(), ScanOp::Sum);
Source

pub fn options(&self) -> ScanOptions

Direction and inclusivity.

§Examples
use strided_basic::{ErasedScanPlan, KernelDType, ScanOp, ScanOptions};
let options = ScanOptions::new().reverse(true);
let plan =
    ErasedScanPlan::compile(KernelDType::F32, ScanOp::Sum, &[2], &[1], &[1], 0, options)
        .unwrap();
assert_eq!(plan.options(), options);
Source

pub fn execute( &self, ctx: &ExecContext, dest: &mut ErasedRawStridedMut<'_>, src: &ErasedRawStridedRef<'_>, ) -> Result<(), StridedError>

Execute the scan into an initialized destination.

§Examples
use strided_basic::{
    ErasedRawStridedMut, ErasedRawStridedRef, ErasedScanPlan, ExecContext, KernelDType,
    ScanOp, ScanOptions,
};
let src = [1_i32, 2, 3, 4];
let mut out = [0_i32; 4];
let options = ScanOptions::new().exclusive(true).reverse(true);
let plan =
    ErasedScanPlan::compile(KernelDType::I32, ScanOp::Sum, &[4], &[1], &[1], 0, options)
        .unwrap();
let src_ref = ErasedRawStridedRef::from_slice(&src, &[4], &[1], 0).unwrap();
let mut dest = ErasedRawStridedMut::from_slice_mut(&mut out, &[4], &[1], 0).unwrap();
plan.execute(&ExecContext::serial(), &mut dest, &src_ref).unwrap();
assert_eq!(out, [9, 7, 4, 0]);
§Errors

Returns DTypeMismatch or PlanLayoutMismatch when a descriptor does not match the plan, before any destination write.

Source

pub fn execute_uninit( &self, ctx: &ExecContext, dest: &mut ErasedRawStridedUninitMut<'_>, src: &ErasedRawStridedPtr<'_>, ) -> Result<(), StridedError>

Execute the scan into an uninitialized destination.

On success every reachable destination element is written; validation errors are returned before any write.

§Examples
use core::mem::MaybeUninit;
use strided_basic::{
    ErasedRawStridedPtr, ErasedRawStridedRef, ErasedRawStridedUninitMut, ErasedScanPlan,
    ExecContext, KernelDType, ScanOp, ScanOptions,
};
let src = [2.0_f32, 3.0, 4.0];
let mut out = [MaybeUninit::<f32>::uninit(); 3];
let plan = ErasedScanPlan::compile(
    KernelDType::F32, ScanOp::Product, &[3], &[1], &[1], 0, ScanOptions::new(),
)
.unwrap();
let src_ref = ErasedRawStridedRef::from_slice(&src, &[3], &[1], 0).unwrap();
let src_ptr = ErasedRawStridedPtr::from_ref(&src_ref);
let mut dest =
    ErasedRawStridedUninitMut::from_uninit_slice(&mut out, &[3], &[1], 0).unwrap();
plan.execute_uninit(&ExecContext::serial(), &mut dest, &src_ptr).unwrap();
let out: Vec<f32> = out.iter().map(|v| unsafe { v.assume_init() }).collect();
assert_eq!(out, [2.0, 6.0, 24.0]);
§Errors

As Self::execute, plus OverlappingInputOutput when the source overlaps the destination allocation.

Trait Implementations§

Source§

impl Clone for ErasedScanPlan

Source§

fn clone(&self) -> ErasedScanPlan

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ErasedScanPlan

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> MaybeSend for T

Source§

impl<T> MaybeSendSync for T

Source§

impl<T> MaybeSync for T

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.