[Numpy-discussion] Byte aligned arrays

Sturla Molden sturla at molden.no
Thu Dec 20 11:26:28 EST 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?


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]\
                .reshape(shape, order=order)


More information about the NumPy-Discussion mailing list