[Numpy-discussion] (2012) Accessing LAPACK and BLAS from the numpy C API
Sturla Molden
sturla@molden...
Tue Mar 6 13:57:29 CST 2012
On 05.03.2012 14:26, "V. Armando Solé" wrote:
> In 2009 there was a thread in this mailing list concerning the access to
> BLAS from C extension modules.
>
> If I have properly understood the thread:
>
> http://mail.scipy.org/pipermail/numpy-discussion/2009-November/046567.html
>
> the answer by then was that those functions were not exposed (only f2py
> functions).
>
> I just wanted to know if the situation has changed since 2009 because it
> is not uncommon that to optimize some operations one has to sooner or
> later access BLAS functions that are already wrapped in numpy (either
> from ATLAS, from the Intel MKL, ...)
Why do you want to do this? It does not make your life easier to use
NumPy or SciPy's Python wrappers from C. Just use BLAS directly from C
instead.
BLAS is a Fortran 77 library (although it might be implemented in C or
assembly). Fortran 77 compilers do not use a predefined binary
interface. f2py has knowledge about all the major compilers, and will
generate different call statements depending on compiler. NumPy and
SciPy does not use the standard C BLAS interface, nor is all of BLAS and
LAPACK exposed.
You can, however, get a C function pointer to the part of BLAS and
LAPACK that SciPy does expose:
import scipy as sp
from scipy.linalg import get_blas_funcs
DGEMM = get_blas_funcs('gemm', dtype=np.float64)
Now DGEMM._cpointer is a PyCObject that wraps the C function pointer.
You can extract it by calling PyCObject_AsVoidPtr in C and cast the
return value to the correct function pointer type. But be aware that you
must know the binary interface of the underlying Fortran version.
Generally you can assume that all arguments to a Fortran 77 subroutine
are pointers (character strings can be problematic).
For MKL you can let f2py generate code for the Intel Fortran compiler
and use the same call statement in C. But then comes the question of
legality: If you don't have a developer's license for MKL you are
probably not allowed to use it like that. And if you do, you can just
use the header files and the C BLAS interface instead.
Generally, I will recommend that you build GotoBLAS2 (or OpenBLAS) if
you don't have a license for MKL -- or download ACML from AMD. You will
in any case get a set of C headers you can use from C. In either case it
is just extra work to hack into SciPy's BLAS functions using the
_cpointer attribute, nor do you gain anything from doing it.
Sturla
More information about the NumPy-Discussion
mailing list