HOOMDArray¶
- class hoomd.data.HOOMDArray(buffer, callback, read_only=None)¶
A numpy.ndarray-like interface to internal HOOMD-blue data.
HOOMD-blue’s zero copy local snapshot API (
hoomd.State.cpu_local_snapshot
) returnsHOOMDArray
objects.HOOMDArray
acts likenumpy.ndarray
through NumPy’s provided interface. Some exceptions are theview
,resize
,flat
andflatiter
methods and thedata
andbase
properties.To ensure memory safety, a
HOOMDArray
object cannot be accessed outside of the context manager in which it was created. Make an explicit copy to use the array elsewhere (e.g.numpy.array(obj, copy=True)
).In general this class should be nearly as fast as a standard NumPy array, but there is some overhead. This is mitigated by returning a
numpy.ndarray
whenever possible.Performance Tips
Let
a
represent aHOOMDArray
.Place the
HOOMDArray
to the left of the expression (e.g.a + b + c
is faster thanb + a + c
). This has to do with the mechanismsHOOMDArray
has to do to hook into NumPy’s functionality.Make copies as early as possible. In other words, if you will need access outside the context manager, use
numpy.array(a, copy=True)
before doing any calculations.If you know that your access of the internal buffer is safe and we cannot detect this (i.e. we return a
HOOMDArray
), usingHOOMDArray._coerce_to_ndarray
should help. Note that for large arrays this only gives minimal performance improvements at greater risk of breaking your program.