Dag Sverre Seljebotn wrote:
> Zak Stone wrote:
>   
>> Hi everyone,
>>
>> I'm just getting started with Cython, and I'm wondering whether fast
>> matrix and vector operations are currently supported. The two threads
>> linked below ("access to numpy functions?" and "numpy calls without
>> Python overhead?") suggest that such operations cannot be accelerated,
>> but I'm curious whether that has changed.
>>
>> I work in computer vision with algorithms that frequently require
>> matrix multiplications, vector norm computations, manipulation of
>> individual rows of a matrix of feature vectors, and so forth. It would
>> be tremendously useful to be able to combine fast loops with these
>> operations! If Cython isn't going in this direction, could you kindly
>> suggest how I could interact with the relevant BLAS routines correctly
>> and efficiently from C?
>>   
>>     
> We might be going in that direction, but we won't be there before in a 
> year or two at least (depending on interest/available developer time).
>
> BLAS has a seperate API for calling from C. An example for calling a 
> BLAS function is given in 
> http://sage.math.washington.edu/home/dagss/numerical-cython-preprint.pdf. 
> The rest one should be able to figure out by reading the CBLAS API.
>   
BTW if you have matrices and vectors that are so small that the call 
overhead matter, you may want to consider using something else, like 
Fortran or TooN (for C++). In particular if you have really small fixed 
sizes then TooN is going to produce fast code as the computations are 
all inlined and even the function call to BLAS has some overhead.

For intermediate sizes, it's still hard not to get rather messy code in 
Cython (passing pointers around etc.) if you want to maintain speed. As 
soon as you create a reusable function like

cdef mul_matrix_vector(matrix, vector):
    ...

there is at least the overhead of buffer acquisition from the matrix and 
vector objects (although that should be faster than making a full call 
to np.dot, the gain might not be that much...at least benchmark it).

Dag Sverre
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to