Skip to content

buffer

BufferKey = tuple[Callable, Iterable[int], npt.DTypeLike] module-attribute

BUFFER_CACHE = {} module-attribute

Buffer

A buffer cached by default.

_key: key into cache storage to allow easy re-caching array: ndarray allocated

array = array instance-attribute

__init__(key, array)

Init a cacheable buffer.

Parameters:

Name Type Description Default
key BufferKey

a cache key made out of tuple of Allocator, shape and dtype

required
array ndarray

ndarray of actual data

required

pop_from_cache(allocator, shape, dtype) classmethod

Retrieve or insert then retrieve of buffer from cache.

Parameters:

Name Type Description Default
allocator Allocator

used to allocate memory

required
shape Iterable[int]

shape of array

required
dtype DTypeLike

type of array elements

required

Return: a buffer wrapping an allocated array

push_to_cache(buffer) staticmethod

Push the buffer back into the cache.

Parameters:

Name Type Description Default
buffer Buffer

buffer to push back in cache, using internal key

required

finalize_memory_transfer()

Finalize any memory transfer

assign_to(destination_array, buffer_slice=np.index_exp[:], buffer_reshape=None)

Assign internal array to destination_array.

Parameters:

Name Type Description Default
destination_array ndarray

target ndarray

required

assign_from(source_array, buffer_slice=np.index_exp[:])

Assign source_array to internal array.

Parameters:

Name Type Description Default
source_array ndarray

source ndarray

required

array_buffer(allocator, shape, dtype)

A context manager providing a contiguous array, which may be re-used between calls.

Parameters:

Name Type Description Default
allocator Allocator

a function with the same signature as numpy.zeros which returns an ndarray

required
shape Iterable[int]

the shape of the desired array

required
dtype DTypeLike

the dtype of the desired array

required

Yields:

Name Type Description
buffer_array Buffer

an ndarray created according to the specification in the args. May be retained and re-used in subsequent calls.

send_buffer(allocator, array, timer=None)

A context manager ensuring that array is contiguous in a context where it is being sent as data, copying into a recycled buffer array if necessary.

Parameters:

Name Type Description Default
allocator Callable

used to allocate memory

required
array ndarray

a possibly non-contiguous array for which to provide a buffer

required
timer Timer | None

object to accumulate timings for "pack"

None

Yields:

Name Type Description
buffer_array Generator[ndarray]

if array is non-contiguous, a contiguous buffer array containing the data from array. Otherwise, yields array.

recv_buffer(allocator, array, timer=None)

A context manager ensuring that array is contiguous in a context where it is being used to receive data, using a recycled buffer array and then copying the result into array if necessary.

Parameters:

Name Type Description Default
allocator Callable

used to allocate memory

required
array ndarray

a possibly non-contiguous array for which to provide a buffer

required
timer Timer | None

object to accumulate timings for "unpack"

None

Yields:

Name Type Description
buffer_array Generator[ndarray]

if array is non-contiguous, a contiguous buffer array which is copied into array when the context is exited. Otherwise, yields array.