[Numpy-discussion] numpy ndarray questions
Tue Jan 27 14:26:48 CST 2009
> On Tue, 2009-01-27 at 14:16 +0100, Sturla Molden wrote:
>> def aligned_array(N, dtype):
>> d = dtype()
>> tmp = numpy.array(N * d.nbytes + 16, dtype=numpy.uint8)
>> address = tmp.__array_interface__['data']
>> offset = (16 - address % 16) % 16
>> return tmp[offset:offset+N].view(dtype=dtype)
> Ah, I didn't think about doing it in python, cool thanks.
Doing it from Python means you don't have to worry about manually
deallocating the array afterwards.
It seems the issue of 16-byte alignment has to do with efficient data
alignment for SIMD instructions (SSE, MMX, etc). So this is not just an
I would just put a check for 16-byte alignment in the wrapper, and raise
an exception (e.g. MemoryError) if the array is not aligned properly.
Raising an exception will inform the user of the problem. I would not
attempt to make a local copy if the array is errorneously aligned. That is
my personal preference.
More information about the Numpy-discussion