Class PooledTensor<T>
A RAII wrapper that automatically returns a pooled tensor to its pool when disposed. This class ensures tensors are properly returned to the pool even if an exception occurs.
public sealed class PooledTensor<T> : IDisposable
Type Parameters
TThe element type of the tensor (e.g., float, double).
- Inheritance
-
PooledTensor<T>
- Implements
- Inherited Members
Remarks
PooledTensor provides a safe way to use pooled tensors with the using statement, ensuring the tensor is returned to the pool when it goes out of scope.
This is implemented as a class (not struct) to ensure reference semantics. Multiple references to the same PooledTensor share disposal state, preventing double-dispose issues that could corrupt the pool.
Example usage:
var pool = new TensorPool<float>();
using (var pooled = pool.RentPooled(new[] { 32, 32 }))
{
var tensor = pooled.Tensor;
// Use tensor for computations...
// Tensor is automatically returned to pool when block exits
}
Alternative explicit cast usage:
using var pooled = pool.RentPooled(new[] { 32, 32 });
Tensor<float> tensor = (Tensor<float>)pooled;
Thread Safety: The Dispose method is idempotent - calling it multiple times is safe and will only return the tensor to the pool once.
Constructors
PooledTensor(TensorPool<T>, Tensor<T>)
Initializes a new instance of the PooledTensor<T> class.
public PooledTensor(TensorPool<T> pool, Tensor<T> tensor)
Parameters
poolTensorPool<T>The tensor pool that owns this tensor and will receive it back on disposal.
tensorTensor<T>The tensor being wrapped.
Remarks
Typically you should not create PooledTensor instances directly. Instead, use RentPooled(int[]) to obtain a properly initialized wrapper.
Exceptions
- ArgumentNullException
Thrown when
poolortensoris null.
Properties
IsDisposed
Gets whether this wrapper has been disposed and the tensor returned to the pool.
public bool IsDisposed { get; }
Property Value
Tensor
Gets the underlying tensor managed by this wrapper.
public Tensor<T> Tensor { get; }
Property Value
- Tensor<T>
The pooled tensor instance.
Remarks
Access the tensor through this property for computations. Do not hold a reference to this tensor after the PooledTensor is disposed.
Methods
Dispose()
Returns the tensor to the pool. After disposal, the tensor should not be used as it may be rented by another operation.
public void Dispose()
Remarks
This method is idempotent - calling it multiple times is safe and will only return the tensor to the pool on the first call.
Thread-safe: Uses atomic compare-and-swap to ensure only one thread can successfully dispose and return the tensor, even under concurrent calls.
Operators
explicit operator Tensor<T>(PooledTensor<T>)
Explicitly converts a PooledTensor to its underlying Tensor.
public static explicit operator Tensor<T>(PooledTensor<T> pooled)
Parameters
pooledPooledTensor<T>The pooled tensor wrapper.
Returns
- Tensor<T>
The underlying tensor.
Remarks
This cast is explicit to remind users that they are responsible for the tensor's lifecycle. The returned tensor reference should not outlive the PooledTensor wrapper.
Exceptions
- ArgumentNullException
Thrown when
pooledis null.