Search code examples
pythonnumpyshared-objectsf2py

Memory usage doubled when passing matrix to shared object


I have a linear set of equations, where A x = b and A is a large matrix and b is known as well. The matrix A is set up with python. Now I want to invert matrix A to get x.

A and b are passed to a Fortran 90 program via a shared object. I compiled the Fortran program using numpy.f2py:

import numpy.f2py.f2py2e as f2py2e
import sys, os
sys.argv += "-lmkl_rt -c -m MKL_MODULE MKL_WRAPPER.f90".split()
f2py2e.main() 

Finally, I call the f90 subroutine:

MKL_MODULE.mkl_wrapper.call_dgelsd(A, b, np.shape(A)[0], np.shape(A)[1])

When calling the fortran program, the memory usage doubles, apparently due to an internal copy of the matrix A and b. However, once I have the vector x I'm not interested in A or b anymore. Is there any way to avoid that internal copy and passing A to the fortran program?

I already had the idea of saving A and b to the HD and reading it from the Fortran program, but this takes very long time and is not really an option for matrices of the size I'm dealing with.


Solution

  • No internal copy will be made if the arrays are in F-order

    [How to force numpy array order to fortran style? to-fortran-style][1]