[Numpy-discussion] "aligned" matrix / ctypes
Zachary Pincus
zachary.pincus at yale.edu
Thu Apr 24 12:29:14 EDT 2008
Hi all,
> It's worth mentioning that there was some discussion of adding an
> allocator for aligned arrays. It got sidetracked into a discussion of
> SSE support for numpy, but there are all sorts of reasons to want
> aligned arrays in numpy, as this post demonstrates. Seeing as it's
> just a few lines worth of pure python, is anyone interested in writing
> an aligned_empty() for numpy?
Here's my attempt at aligned_empty(). It only accepts alignments that
are an integer multiple (or divisor) of the dtype's itemsize, because
of limitations in ndarray.view().
I've also attached aligned_empty_arbitrary(), which is similar, but
allows any bizarro alignment requested. Or at least it would, if
ndarray.view() were able to handle strided arrays.
It could probably be tightened a bit -- I tried to make the code err
on the "demonstrative" side rather than "terse".
Here are some basic tests:
import aligned_allocator as aa
a = aa.aligned_empty((9,7), dtype=numpy.uint8, byte_alignment=64,
order='C')
for i in range(a.shape[0]):
assert(a[i,:].ctypes.data % 64 == 0)
a = aa.aligned_empty((9,7), dtype=numpy.float32, byte_alignment=16,
order='C')
for i in range(a.shape[0]):
assert(a[i,:].ctypes.data % 32 == 0)
a = aa.aligned_empty((9,7), dtype=numpy.uint16, byte_alignment=32,
order='F')
for i in range(a.shape[1]):
assert(a[:,i].ctypes.data % 32 == 0)
Best,
Zach
-------------- next part --------------
A non-text attachment was scrubbed...
Name: aligned_allocator.py
Type: text/x-python-script
Size: 2554 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20080424/d0624fb7/attachment.bin>
More information about the NumPy-Discussion
mailing list