HOOMDGPUArray

class hoomd.data.HOOMDGPUArray(*args, **kwargs)

A __cuda_array_interface__ to internal HOOMD-blue data on the GPU.

The HOOMDGPUArray object exposes a GPU data buffer using __cuda_array_interface__. This class provides buffer access through a context manager to prevent invalid memory accesses (hoomd.State.gpu_local_snapshot). To avoid errors, use arrays only within the relevant context manager. For example:

with sim.state.gpu_local_snapshot as data:
    pos = cupy.array(data.particles.position, copy=False)
    pos[:, 2] += 1

Note

When CuPy can be imported, then this class wraps much of the cupy.ndarray class’s functionality. Otherwise, this class exposes only the buffer.

HOOMDGPUArray always supports getting (but not setting) the shape, strides, and ndim properties. HOOMDGPUArray never supports standard binary operators like (+, -, *). This is a current limitation on external classes hooking into CuPy.

When CuPy can be imported, slice/element assignment (e.g. array[0] = 1; array[:2] = 4) and compound assignment operators (e.g. +=, -=, *=) are available. In addition, most methods besides view, resize, flat, flatiter are available. The same is true for properties except the data and base properties. See CuPy’s documentation for a list of methods.

Tip

Use, cupy.add, cupy.multiply, etc. for binary operations on the GPU.

Note

Packages like Numba and PyTorch can use HOOMDGPUArray without CuPy installed. Any package that supports version 2 of the __cuda_array_interface__ should support the direct use of HOOMDGPUArray objects.