[Numpy-discussion] ndarray allocation question
Travis Oliphant
oliphant at ee.byu.edu
Tue Apr 10 16:59:17 EDT 2007
David Doukhan wrote:
>Sorry, i thought doing the following lines:
>
>clib = ctypes.cdll.LoadLibrary("./libtest.so")
>dt = numpy.dtype({'names' : ['x'], 'formats' : [N.float32]}, align=67)
>myarray = numpy.empty((5,6,7),dtype=dt)
>
>would create an array such than when doing :
>
>my_pointer = myarray.ctypes.data_as(POINTER(c_float))
>clib.print_pointer(my_pointer)
>
>mypointer would be a multiple of 67.
>
>So after checking it, I realised that it is not the case :-((
>
>
The align argument is a Boolean argument that states whether or not you
would like the fields aligned as the compiler would align them if they
were in a C-struct.
>* Is there other arguments than "names", "format", "offsets",
>"titles", "align" that I could give to numpy.dtype ?
>
>
No. However, you can have "unused" space in a data-type (i.e. skipped
bytes).
>Actually, in a ideal world, I would like to be able to align the
>address of the data contained in the array, as if I was doing a
>posix_memalign in C, and to make the allocated memory size a multiple
>of a given size, even if the real data size is smaller.
>It seems that it won't be possible, unless I misunderstood something.
>
>
Sure it's possible, you just may have to do more work. The system is
not set up to make this kind of special case trivial.
There are lots of ways to do it. For example, you can allocate your
own memory (use posix_memalign called through c-types if you want). You
can then put an ndarray wrapper around it using a data-type that has the
floats where-ever you want them in each record.
It's all quite doable, just not "automatic"
Look at the ndarray constructor for how to build an ndarray from an
arbitrary "buffer"
-Travis
More information about the NumPy-Discussion
mailing list