Skip to main content

PersistentWorkerPool

Struct PersistentWorkerPool 

Source
pub struct PersistentWorkerPool<D, C>
where D: Dataset + Clone + Send + Sync + 'static, C: Collate<D::Item> + Clone + Send + Sync + 'static, D::Item: Send + 'static, C::Output: Send + 'static,
{ /* private fields */ }
Expand description

Persistent worker pool that keeps workers alive across epochs

Unlike the regular WorkerPool, this implementation keeps worker threads alive between epochs, reducing the overhead of thread creation and destruction. This is particularly useful for training scenarios with multiple epochs.

§Examples

use std::sync::Arc;
use torsh_data::dataloader::workers::PersistentWorkerPool;
use torsh_data::dataset::TensorDataset;
use torsh_data::collate::DefaultCollate;

let dataset = Arc::new(TensorDataset::new(vec![1, 2, 3, 4, 5]));
let collate_fn = Arc::new(DefaultCollate);
let worker_pool = PersistentWorkerPool::new(dataset, collate_fn, 4);

// Use across multiple epochs
for epoch in 0..10 {
    worker_pool.reset_for_epoch().unwrap();
    // Submit tasks for this epoch
}

// Shutdown when done
worker_pool.shutdown().unwrap();

Implementations§

Source§

impl<D, C> PersistentWorkerPool<D, C>
where D: Dataset + Clone + Send + Sync + 'static, C: Collate<D::Item> + Clone + Send + Sync + 'static, D::Item: Send + 'static, C::Output: Send + 'static,

Source

pub fn new(dataset: Arc<D>, collate_fn: Arc<C>, num_workers: usize) -> Self

Create a new persistent worker pool

§Arguments
  • dataset - The dataset to process
  • collate_fn - The collation function to apply to batches
  • num_workers - Number of worker threads to spawn
§Returns

A new PersistentWorkerPool ready to process tasks

Source

pub fn submit_task(&self, task_id: usize, indices: Vec<usize>) -> Result<()>

Submit a task to the persistent worker pool

§Arguments
  • task_id - Unique identifier for the task
  • indices - Indices of samples to process
§Returns

Result indicating success or failure of task submission

Source

pub fn get_result(&self) -> Result<WorkerResult<C::Output>>

Get a result from the persistent worker pool

This method blocks until a result is available from any worker.

§Returns

The next available worker result

Source

pub fn get_result_timeout( &self, timeout: Duration, ) -> Result<WorkerResult<C::Output>>

Get a result with timeout

§Arguments
  • timeout - Maximum time to wait for a result
§Returns

The next available worker result or a timeout error

Source

pub fn try_get_result(&self) -> Option<WorkerResult<C::Output>>

Try to get a result without blocking

§Returns

Some(result) if a result is available, None otherwise

Source

pub fn num_workers(&self) -> usize

Get the number of workers

§Returns

The number of worker threads in the pool

Source

pub fn is_shutdown(&self) -> bool

Check if the pool is shutdown

§Returns

True if the pool has been shutdown, false otherwise

Source

pub fn shutdown(&self) -> Result<()>

Shutdown the persistent worker pool

This method gracefully shuts down all worker threads and should be called when the pool is no longer needed.

§Returns

Result indicating success or failure of shutdown operation

Source

pub fn reset_for_epoch(&self) -> Result<()>

Reset the pool for a new epoch (clears any pending tasks)

This method can be called between epochs to reset the pool state. In the current implementation, this is a placeholder for future epoch-specific optimizations.

§Returns

Result indicating success or failure of reset operation

Trait Implementations§

Source§

impl<D, C> Drop for PersistentWorkerPool<D, C>
where D: Dataset + Clone + Send + Sync + 'static, C: Collate<D::Item> + Clone + Send + Sync + 'static, D::Item: Send + 'static, C::Output: Send + 'static,

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<D, C> !RefUnwindSafe for PersistentWorkerPool<D, C>

§

impl<D, C> !Sync for PersistentWorkerPool<D, C>

§

impl<D, C> !UnwindSafe for PersistentWorkerPool<D, C>

§

impl<D, C> Freeze for PersistentWorkerPool<D, C>

§

impl<D, C> Send for PersistentWorkerPool<D, C>

§

impl<D, C> Unpin for PersistentWorkerPool<D, C>

§

impl<D, C> UnsafeUnpin for PersistentWorkerPool<D, C>

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V