Skip to main content

Pool

Struct Pool 

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

Owns a native pool allocator and releases it after its allocations.

A pool serves the intermediate blobs, workspace or attention KV cache that a network or an extractor allocates. Options and the per-extractor setters store the native handle without taking ownership, so the pool must outlive every network and extractor configured from it and must not be shared with one that runs concurrently. The type is thread-affine, like the objects that use it.

let mut pool = ncnn_rust::alloc::Pool::new()?;
pool.configure(0.5, 1024 * 1024)?;
let mut options = ncnn_rust::Options::new()?;
// SAFETY: `pool` outlives these options and every network configured from them.
unsafe { options.set_blob_allocator(&pool)? };
let net = ncnn_rust::NetBuilder::with_options(options).load_bytes(
    "7767517\n2 2\nInput input 0 1 input\nReLU relu 1 1 input output\n",
    &[],
)?;
assert_eq!(
    net.infer([("input", ncnn_rust::Mat::from_f32(&[-1.0, 2.0], &[2])?)], &["output"])?[0].values,
    [0.0, 2.0]
);

Implementations§

Source§

impl Pool

Source

pub fn new() -> Result<Self>

Creates a pool allocator with native defaults.

Source

pub fn configure(&mut self, ratio: f32, threshold: usize) -> Result<()>

Sets the native reuse ratio and size threshold.

Source

pub fn clear(&mut self)

Releases every cached block that has no outstanding allocation.

Source

pub fn as_raw(&self) -> ncnn_allocator_t

Borrows the allocator for advanced FFI allocation paths.

Trait Implementations§

Source§

impl Drop for Pool

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl !Send for Pool

§

impl !Sync for Pool

§

impl Freeze for Pool

§

impl RefUnwindSafe for Pool

§

impl Unpin for Pool

§

impl UnsafeUnpin for Pool

§

impl UnwindSafe for Pool

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 = !

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.