[Numpy-discussion] numpy arrays, data allocation and SIMD alignement

Anne Archibald peridot.faceted@gmail....
Sat Aug 4 02:24:55 CDT 2007


On 04/08/07, David Cournapeau <david@ar.media.kyoto-u.ac.jp> wrote:

> >     Here's a hack that google turned up:

I'd avoid hacks in favour of posix_memalign (which allows arbitrary
degrees of alignment. For one thing, freeing becomes a headache (you
can't free a pointer you've jiggered!).

>  - Check whether a given a numpy array is simd aligned:
>
> /* Simple case: if aligned, use optimized func, use non optimized
> otherwise */
> int simd_func(double* in, size_t n);
> int nosimd_func(double* in, size_t n);
>
> if (PyArray_ISALIGNED_SIMD(a)) {
>     simd_func((double *)a->data, a->size);
> } else {
>     nosimd_func((double *)a->data, a->size);
> }
>  - Request explicitely an aligned arrays from any PyArray_* functions
> which create a ndarray, eg: ar = PyArray_FROM_OF(a, NPY_SIMD_ALIGNED);
>
> Allocating a buffer aligned to a given alignment is not the problem:
> there is a posix functions to do it, and we can implement easily a
> function for the OS who do not support it. This would be done in C, not
> in python.

I'd just like to point out that PyArray_ISALIGNED_SIMD(a) can be a
macro which aligns to something like "!((a->datapointer)&0xf)"; this
avoids any change to the array objects and allows checking for
arbitrary degrees of alignment - somebody mentioned the Intel
Performance Primitives need 32-byte aligned data? One might also want
page-aligned data or data aligned in some way with cache lines.

It seems to me two things are needed:

* A mechanism for requesting numpy arrays with buffers aligned to an
arbitrary power-of-two size (basically just using posix_memalign or
some horrible hack on platforms that don't have it).

* A macro (in C, and some way to get the same information from python,
perhaps just "a.ctypes.data % 16") to test for common alignment cases;
SIMD alignment and arbitrary power-of-two alignment are probably
sufficient.

Does this fail to cover any important cases?

Anne


More information about the Numpy-discussion mailing list