[Numpy-discussion] Byte aligned arrays
heng at cantab.net
Thu Dec 20 11:45:43 EST 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
> > 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']
> offset = (boundary - address % boundary) % boundary
> return tmp[offset:offset+N]\
> .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.
More information about the NumPy-Discussion