Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cython: when using typed memoryviews, are Cython users supposed to implement their own library of "vector" functions?

If I am using typed memoryviews, and I want to add two vectors I am representing using such a memoryview, or take their dot-product, or other such vector things, does Cython expect me to to implement these functions on my own?

I don't have a problem with that, but I wonder if I am doing something wrong if I am busy writing add vectors/multiply by a scalar/dot products/etc. on my own. It feels like an antipattern, but I am not sure.

What is the right pattern?

like image 715
bzm3r Avatar asked Dec 15 '22 16:12

bzm3r


2 Answers

If your code spends a lot of time in linear algebra functions or vectorized operations, it's probably not a very good candidate for Cythonizing.

Dot products between numpy arrays are usually performed using BLAS library calls which are already very highly optimized, and you are pretty much guaranteed to do worse if you try and reinvent the wheel in Cython*. Similarly, basic vectorized operations such as adding two vectors are already pretty efficient for numpy arrays, although in certain circumstances it might be possible to do a bit better in Cython (e.g. by exploiting parallelization, or by avoiding intermediate array allocation).

Cython is most useful for speeding up operations that can't be easily vectorized, where you would otherwise have to resort to Python for loops etc. The best approach is usually to identify and Cythonize just these bottlenecks, rather than attempting to re-write everything in Cython.


*Having said that, it is possible to call a BLAS or LAPACK function directly on a typed memoryview by passing a pointer to the first element in the array (see here and here for some examples).

like image 64
ali_m Avatar answered Dec 17 '22 04:12

ali_m


Note that you can actually pass memoryviews into most numpy functions. Of course you still pay the Python call overhead, but if the memoryview is large, this may be insignificant. Example:

cdef double[:,:] A
# ... initialize A ...
x = np.sum(A)
like image 32
cfh Avatar answered Dec 17 '22 04:12

cfh