[Numpy-discussion] Dot in C extension

Travis E. Oliphant oliphant@enthought....
Tue May 27 09:06:52 CDT 2008


Charles R Harris wrote:
>
>
> On Sun, May 25, 2008 at 12:12 AM, Charles R Harris 
> <charlesr.harris@gmail.com <mailto:charlesr.harris@gmail.com>> wrote:
>
>
>
>     On Sat, May 24, 2008 at 11:29 PM, <jordan@math.ucsb.edu
>     <mailto:jordan@math.ucsb.edu>> wrote:
>
>         Hi all,
>
>         I'm trying to write a Gauss-Seidel function in C++. The
>         function works
>         however it is too slow because I'm not using any acceleration
>         for the
>         vector multiplication. I'm not really sure how to access the
>         dot function
>         in my extension, nor what all the arguments are for.
>
>         Is this the right function to use (found in ndarrayobject.h):
>
>         typedef void (PyArray_DotFunc)(void *, npy_intp, void *,
>         npy_intp, void *,
>                                       npy_intp, void *);
>
>         I guess the voids are array objects, the two to be dotted and
>         the output.
>         What's the fourth?
>
>
>     It's ignored, so 0 (C++) should do.
>
>     static void
>     @name@_dot(char *ip1, intp is1, char *ip2, intp is2, char *op, intp n,
>                void *ignore)
>     {
>         register @out@ tmp=(@out@)0;
>         register intp i;
>         for(i=0;i<n;i++,ip1+=is1,ip2+=is2) {
>             tmp += (@out@)(*((@type@ *)ip1)) * \
>                    (@out@)(*((@type@ *)ip2));
>         }
>         *((@type@ *)op) = (@type@) tmp;
>     }
>      
>     Note that the function may call BLAS in practice, but you can
>     figure the use of the arguments from the above. Ignore the @type@
>     sort of stuff, it's replaced by real types by the code generator.
>
>
> I'm not sure how you get to these functions, which are type specific, 
> someone else will have to supply that answer.

They are attached to the .f member of the data-type object.  So, you get 
the data-type object, get the .f member and then the dotfunc.   
Something like

PyArray_DataType(a)->f->dotfunc  (but I would have to look up the name 
to be sure).

-Travis




More information about the Numpy-discussion mailing list