[Numpy-discussion] "aligned" matrix / ctypes
Stéfan van der Walt
stefan@sun.ac...
Fri Apr 25 12:07:25 CDT 2008
Robert,
Can we check this in somewhere under numpy.core? It seems very useful.
Stéfan
2008/4/25 Zachary Pincus <zachary.pincus@yale.edu>:
> Hello all,
>
> Attached is code (plus tests) for allocating aligned arrays -- I think this
> addresses all the requests in this thread, with regard to allowing for
> different kinds of alignment. Thanks Robert and Anne for your help and
> suggestions. Hopefully this will be useful.
>
> The core is a function for allocating arrays with totally arbitrary
> alignment along each dimension (e.g. you could allocate an 10x20 array of
> uint16's where each uint16 is aligned to 4-byte boundaries and each row of
> 20 uint16's is aligned to 32-byte boundaries, and the entire buffer is
> aligned to a 128-byte boundary.) I've also included helper functions for two
> common cases: when you want everything aligned to a particular multiple
> (every element, row, etc. as well as the whole buffer), and when you want an
> array where the rows (second-fastest moving index) are so aligned (this was
> my original use case, for fast image-blitting).
>
> Zach
>
>
> def aligned_empty(shape, dtype, dim_alignments, array_alignment):
> '''Allocate an empty array with the given shape and dtype, where the array
> buffer starts at a memory address evenly-divisible by array_alignment, and
> where items along each dimension are offset from the first item on that
> dimension by a byte offset that is an integer multiple of the
> corresponding
> value in the dim_alignments tuple.
>
> Example: To allocate a 20x30 array of floats32s, where individual data
> elements are aligned to 16-bute boundaries, each row is aligned to a
> 64-byte
> boundary, and the array's buffer starts on a 128-byte boundary, call:
> aligned_empty((20,30), numpy.float32, (64, 16), 128)
> '''
>
> def aligned_rows_empty(shape, dtype, alignment, order='C'):
> '''Return an array where the rows (second-fastest-moving index) are
> aligned
> to byte boundaries evenly-divisible by 'alignment'. If 'order' is 'C',
> then
> the indexing is such that the fastest-moving index is the last one; if the
> order is 'F', then the fastest-moving index is the first.'''
>
> def aligned_elements_empty(shape, dtype, alignment, order='C'):
> '''Return an array where each element is aligned to byte boundaries
> evenly-
> divisible by 'alignment'.'''
>
>
>
>
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
>
More information about the Numpy-discussion
mailing list