[Numpy-discussion] ndarray allocation question

David Doukhan david.doukhan@gmail....
Tue Apr 10 14:58:25 CDT 2007


2007/4/10, Travis Oliphant <oliphant.travis@ieee.org>:
> David Doukhan wrote:
> > Hi!
> > I'm writing you this mail because I would like to do "advanced" use of
> > ndarray memory allocation.
> >
> > So here is a short description of my problem:
> >
> > I'm interfacing multidimensionnal Numpy array to C using the ctypes
> > module. For now, there is no problem.
> > The C program that will use the pointer to the data of the array runs
> > on a CellBE architecture.
>
> > The consequences are:
> > * the adress of the beginning of the data of the array must be a
> > multiple a 16 bytes (that could be specified using the attribute
> > 'align' of dtype)
> > * the memory allocated MUST be a multiple of 16 bytes, and that's my
> > current problem, because I didn't find a way to specify it using dtypes...
>
> You can't specify the alignment for a data-type unless you write your
> own data-type (the alignment is a property of the data-type and is
> determined at compile time for built-in data-types).   I don't think you
> can set the alignment for a derived data-type at this point.
>
> > So, do the think there is a "clean" way to do what I want. For
> > example, asking for an array of 2 lines and 7 columns of float32, so
> > that the adress of the biginning of the data would be a multiple of 16
> > bytes (that's already possible)
> How are you doing that?
>

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 :-((

So I will ask you another question:
* What is the use of the argument align in :
numpy.dtype({'names' : ['x'], 'formats' : [N.float32]}, align=67) ?
* Is there other arguments than "names", "format", "offsets",
"titles", "align" that I could give to numpy.dtype ?

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.

-- 
David Doukhan


More information about the Numpy-discussion mailing list