Matt
Matt

Reputation: 2832

Using Fortran NumPy operations in Cython with NOGIL, what's the Fortran library equivalent to NumPy multiply?

So I'm trying to help another SO user, and in the process I can't create a Cython program to do something simple outside of NumPy which forces me to use the GIL. So that makes using OpenMP (multicore) impossible. Then I came across an interesting post whereas you can import from SciPy the Fortran libraries directly into Cython code (BLAS, LAPACK) which are installed with NumPy, in my case Intel MKL equivalent functions. All I'm trying to do is a simple vector multiplication of 2 vectors 1000x1 dimensions by another which is transposed, resulting in a 1000x1000 matrix. But I can't find the relevant Fortran routine (equivalent to NumPy multiply) that will do the trick. All the routines seem to do matrix multiplication instead. So the cool feature now in SciPy is to add this to your Cython module: import scipy.linalg.cython_blas as blas and cimport scipy.linalg.cython_lapack as lapack then in theory I started with the Fortran library dgemm by calling blas.dgemm(options) but it does the matrix product rather than just element-wise multiplication. Does anyone know the Fortan module that will do the simple multiplication of 2 1000x1 vectors, 1 transposed, resulting in a 1000x1000 matrix? And if you can add the input syntax that would be great. I'm passing C contiguous memory views to the function i.e. [::1] Cython NumPy vectors.

Upvotes: 2

Views: 382

Answers (1)

MSeifert
MSeifert

Reputation: 152725

What you're describing is a pure NumPy feature called "broadcasting". These broadcasting operations are done using C (or Cython) code. You can always access these in Cython through the Python C API such as PyNumber_Multiply (although they probably won't release the GIL) but normal multiplication in Cython should delegate to that function anyway so you normally don't need to call (or import) it directly.

BLAS/LAPACK is mostly used in linear algebra stuff and even if you could "use" a function exposed there for this purpose it won't be the same NumPy uses (normally).

Upvotes: 3

Related Questions