pygsti.tools.sharedmemtools

Utility functions for working with shared memory

Module Contents

Classes

LocalNumpyArray

Numpy array with metadata for referencing how this "local" array is part

Functions

shared_mem_is_enabled()

Whether shared memory functionality is available (Python 3.8+)

create_shared_ndarray(resource_alloc, shape, dtype, zero_out=False, memory_tracker=None)

Creates a numpy.ndarray that is potentially shared between processors.

cleanup_shared_ndarray(shm)

De-allocates a (potentially) shared numpy array, created by :function:`create_shared_ndarray`.

Attributes

_shared_memory

pygsti.tools.sharedmemtools._shared_memory
class pygsti.tools.sharedmemtools.LocalNumpyArray(shape, dtype=float, buffer=None, offset=0, strides=None, order=None)

Bases: numpy.ndarray

Numpy array with metadata for referencing how this “local” array is part of a larger shared memory array.

__pygsti_reduce__(self)
__pygsti_setstate__(self, state_dict)
__array_finalize__(self, obj)
pygsti.tools.sharedmemtools.shared_mem_is_enabled()

Whether shared memory functionality is available (Python 3.8+)

Returns

bool

pygsti.tools.sharedmemtools.create_shared_ndarray(resource_alloc, shape, dtype, zero_out=False, memory_tracker=None)

Creates a numpy.ndarray that is potentially shared between processors.

A shared memory array is created when resource_alloc.host_comm is not None, in which case it indicates which processors belong to the same host and have access to the same shared memory.

Parameters
  • resource_alloc (ResourceAllocation) – The resource allocation object containing information about whether or not to create shared memory arrays and how to do so (see above).

  • shape (tuple) – The shape of the returned array

  • dtype (numpy.dtype) – The numpy data type of the returned array.

  • zero_out (bool, optional) – Whether to initialize the array to all zeros. When True, this function behaves as numpy.zeros; when False as numpy.empty.

  • memory_tracker (ResourceAllocation, optional) – If not none, callc memory_tracker.add_tracked_memory to track the size of the allocated array.

Returns

  • ar (numpy.ndarray) – The potentially shared-memory array.

  • shm (multiprocessing.shared_memory.SharedMemory) – A shared memory object needed to cleanup the shared memory. If a normal array is created, this is None. Provide this to :function:`cleanup_shared_ndarray` to ensure ar is deallocated properly.

pygsti.tools.sharedmemtools.cleanup_shared_ndarray(shm)

De-allocates a (potentially) shared numpy array, created by :function:`create_shared_ndarray`.

Parameters

shm (multiprocessing.shared_memory.SharedMemory or None) – The shared memory object to deallocate. If None, no deallocation is is performed.

Returns

None