Anne,<br><br><div><span class="gmail_quote">On 8/8/07, <b class="gmail_sendername">Anne Archibald</b> <<a href="mailto:firstname.lastname@example.org">email@example.com</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
On 08/08/2007, Charles R Harris <<a href="mailto:firstname.lastname@example.org">email@example.com</a>> wrote:<br>><br>><br>> On 8/8/07, Anne Archibald <<a href="mailto:firstname.lastname@example.org">email@example.com
</a>> wrote:<br>> > Oh. Well, it's not *terrible*; it gets you an aligned array. But you<br>> > have to allocate the original array as a 1D byte array (to allow for<br>> > arbitrary realignments) and then align it, reshape it, and reinterpret
<br>> > it as a new type. Plus you're allocating an extra ndarray structure,<br>> > which will live as long as the new array does; this not only wastes<br>> > even more memory than the portable alignment solutions, it clogs up
<br>> > python's garbage collector.<br>><br>> The ndarray structure doesn't take up much memory, it is the data that is<br>> large and the data is shared between the original array and the slice. Nor
<br>> does the data type of the slice need changing, one simply uses the desired<br>> type to begin with, or at least a type of the right size so that a view will<br>> do the job without copies. Nor do I see how the garbage collector will get
<br>> clogged up, slices are a common feature of using numpy. The slice method<br>> also has the advantage of being compiler and operating system independent,<br>> there is a reason Intel used that approach.<br>>
<br>> Aligning multidimensional arrays might indeed be complicated, but I suspect<br>> those complications will be easier to handle in Python than in C.<br><br>Can we assume that numpy arrays allocated to contain (say) complex64s
<br>are aligned to a 16-byte boundary? I don't think they will<br>necessarily, so the shift we need may not be an integer number of<br>complex64s. float96s pose even more problems. So to ensure alignment,<br>we do need to do type conversion; if we're doing it anyway, byte
<br>arrays require the least trust in malloc().</blockquote><div><br>I think that is a safe assumption, it is probably almost as safe as assuming binary and two's complement, likely more safe than assuming ieee 784. I expect almost all 32 bit OS's to align on 4 byte boundaries at worst, 64 bit machines to align on 8 byte boundaries. Even C structures are typically filled out with blanks to preserve some sort of alignment. That is because of addressing efficiency, or even the impossibility of odd addressing -- depends on the architecture. Sometimes even byte addressing is easier to get by putting a larger integer on the bus and extracting the relevant part. In addition, I expect the heap implementation to make some alignment decisions for efficiency.
<br><br>My 64 bit linux on Intel aligns arrays, whatever the data type, on 16 byte boundaries. It might be interesting to see what happens with the Intel and MSVC comipilers, but I expect similar results. PPC's, Sun and SGI need to be checked, but I don't expect problems. I think that will cover almost all architectures numpy is likely to run on.
<br> <br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">The ndarray object isn't too big, probably some twenty or thirty<br>bytes, so I'm not talking about a huge waste. But it is a python
<br>object, and the garbage collector needs to walk the whole tree of<br>accessible python objects every time it runs, so this is one more<br>object on the list.<br><br>As an aside: numpy's handling of ndarray objects is actually not
<br>ideal; if you want to exhaust memory on your system, do:<br><br>a = arange(5)<br>while True:<br> a = a[::-1]</blockquote><div><br>Well, that's a pathological case present in numpy. Fixing it doesn't seem to be a high priority although there is a ticket somewhere.
<br></div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Each ndarray object keeps alive the ndarray object it is a slice of,<br>so this operation creates an ever-growing linked list of ndarray
<br>objects. Seems to me it would be better to keep a pointer only to the<br>original object that holds the address of the buffer (so it can be<br>freed).<br><br>Aligning multidimensional arrays is an interesting question. To first
<br>order, aligning the first element should be enough. If the dimensions<br>of the array are not divisible by the alignment, though, this means<br>that lower-dimensional complete slices may not be aligned:<br><br>A = aligned_empty((7,5),dtype=float,alignment=16)
<br><br>Then A is aligned, as is A[0,:], but A[1,:] is not.<br><br>So in this case we might want to actually allocate an 8-by-5 array and<br>take a slice. This does mean it won't be contiguous in memory, so that<br>flattening it requires a copy (which may not wind up aligned). This is
<br>something we might want to do - that is, make available as an option -<br>in python.</blockquote><div><br>I think that is better viewed as need based. I suspect that if you really need such alignment it is better to start with array dimensions that will naturally align the rows. It will be impossible to naturally align all the columnes unless the data type is the correct size.