[Numpy-discussion] Byte aligned arrays

Henry Gomersall heng@cantab....
Thu Dec 20 10:45:43 CST 2012


On Thu, 2012-12-20 at 17:26 +0100, Sturla Molden wrote:
> 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)


Initially because it kept my module in a single file. That's legacy now,
but since I'm already in the Cython domain, it makes sense to get the
advantages (like speed - creating a 1000 length array with
n_byte_align_empty is about 7 times faster than with the code above).

The alignment functions is just a utility function for the FFTW wrapper.

Cheers,

Henry



More information about the NumPy-Discussion mailing list