[SciPy-user] numpy aligned memory

Sturla Molden sturla@molden...
Mon Mar 9 10:50:51 CDT 2009


On 3/8/2009 6:03 PM, Rohit Garg wrote:

> http://www.mail-archive.com/numpy-discussion@scipy.org/msg04005.html
> 
> while googling for numpy memory alignment. I wish to know if anything
> on that account has come to pass yet? On linux 64 bit platform, can I
> assume anything beyond the glibc alignment as of now?

If you are willing to waste a few bytes, there is nothing that prevents 
you from ensuring arbitrary alignment manually. You just allocate more 
space than you need (16 bytes for 16 bytes alignment), and return a view 
to a properly aligned segment. Something like this:

import numpy as np

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)

We had questions regarding this for an FFTW interface as well (how to 
use fftw_malloc instead of malloc). It also affect all coding using SIMD 
extensions on x86 (MMX, SSE, SSE2). I don't use PPC so I don't know what 
altivec needs. In any case, should this be in the cookbook? Or even in 
numpy? It seems a bit redundant to answer this question over and over again.


Sturla Molden







More information about the SciPy-user mailing list