[Numpy-discussion] ndarray allocation question

David Doukhan david.doukhan@gmail....
Tue Apr 10 17:05:15 CDT 2007


2007/4/10, Travis Oliphant <oliphant@ee.byu.edu>:
> 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.
>

Oops!
Sorry for that misunderstanding.
Actually, I did that error because I've seen the following line :

dt = N.dtype({'names' : ['i', 'j'], 'formats' : [N.intc, N.float64]}, align=1)

on this website:  http://www.scipy.org/Cookbook/Ctypes

So I thought the align argument was numeric....
Now, with your explanations, it will be ok.

> >* 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"
>

I was already doing this kind of stuffs, but I was wondering if there
was a way to automatize it, in order to depend as less as possible of
a C code at this step. My misunderstaing of the use of the align
argument made me think this kind of things could be automatized by
Numpy...

>
> -Travis
>
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>

So thanks for your help, for the speed at wich you managed to answer
my mails, and for your patience.


-- 
David Doukhan


More information about the Numpy-discussion mailing list