[Numpy-discussion] Byte aligned arrays

Sturla Molden sturla@molden...
Thu Dec 20 10:26:28 CST 2012


On 19.12.2012 09:40, Henry Gomersall wrote:
> I've written a few simple cython routines for assisting in creating
> byte-aligned numpy arrays. The point being for the arrays to work with
> SSE/AVX code.
>
> https://github.com/hgomersall/pyFFTW/blob/master/pyfftw/utils.pxi

Why use Cython?

http://mail.scipy.org/pipermail/scipy-user/2009-March/020289.html


def aligned_zeros(shape, boundary=16, dtype=float, order='C'):
      N = np.prod(shape)
      d = np.dtype(dtype)
      tmp = np.zeros(N * d.itemsize + boundary, dtype=np.uint8)
      address = tmp.__array_interface__['data'][0]
      offset = (boundary - address % boundary) % boundary
      return tmp[offset:offset+N]\
                .view(dtype=d)\
                .reshape(shape, order=order)


Sturla


More information about the NumPy-Discussion mailing list