On 8/8/07, Anne Archibald <peridot.faceted@gmail.com> wrote:
On 08/08/2007, Stefan van der Walt <stefan@sun.ac.za> wrote:
> On Tue, Aug 07, 2007 at 01:33:24AM -0400, Anne Archibald wrote:
> > Well, it can be done in Python: just allocate a too-big ndarray and
> > take a slice that's the right shape and has the right alignment. But
> > this sucks.
>
> Could you explain to me why is this such a bad idea?

Oh. Well, it's not *terrible*; it gets you an aligned array. But you
have to allocate the original array as a 1D byte array (to allow for
arbitrary realignments) and then align it, reshape it, and reinterpret
it as a new type. Plus you're allocating an extra ndarray structure,
which will live as long as the new array does; this not only wastes
even more memory than the portable alignment solutions, it clogs up
python's garbage collector.

The ndarray structure doesn't take up much memory, it is the data that is large and the data is shared between the original array and the slice. Nor does the data type of the slice need changing, one simply uses the desired type to begin with, or at least a type of the right size so that a view will do the job without copies. Nor do I see how the garbage collector will get clogged up, slices are a common feature of using numpy. The slice method also has the advantage of being compiler and operating system independent, there is a reason Intel used that approach.

Aligning multidimensional arrays might indeed be complicated, but I suspect those complications will be easier to handle in Python than in C.

Chuck