This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

Convert to a scipy-friendly format

+1 vote

Problem: I have a sparse matrix A in petsc format (petsc4py.PETSc.Mat) and would like to create a sparse csr_matrix in scipy, without having to copy the data (if this is possible?)

Constraints: I'm stuck with version 1.4, using PETSc and uBLAS (and STL) as possible backends.

Solution so far:

ai, aj, av = A.getValuesCSR()
Asp = scipy.sparse.csr_matrix((av, aj, ai))

However this copies the data.

If I could somehow do Ablas = dolfin.uBLASMatrix(A) like I can do dolfin.PETScMatrix(A), then I could use the following answer
http://fenicsproject.org/qa/9463/how-to-extract-sparse-matrix-from-assembled-form

But I cannot. Any ideas?

asked Mar 23, 2016 by maartent FEniCS User (3,910 points)

I found uBLASSparseMatrix in the documentation for version 1.4, but it doesn't take a pets4py.PETSc.Mat as input, I get:

TypeError: in method 'new_uBLASSparseMatrix', argument 1 of type
'dolfin::uBLASMatrix< boost::numeric::ublas::compressed_matrix<
double,boost::numeric::ublas::row_major > > const &'

As I remember, the sparray method is rather straight forward in dolfin 1.4. Maybe I misunderstand you? Is this M_sp what you want?

 m = u*v*dx
 M = assemble(m)
 M_sp = M.sparray()

If I try that, I get:

*** -------------------------------------------------------------------------
*** Error: Unable to return pointers to underlying matrix data.
*** Reason: Not implemented by current linear algebra backend.
*** Where: This error was encountered inside GenericMatrix.h.
*** Process: unknown


*** DOLFIN version: 1.4.0
*** Git changeset:
*** -------------------------------------------------------------------------

I believe this is because I use the PETSc backend, instead of uBLAS.
Also, part of the traceback says data = self.data(deepcopy=True), which makes me wonder: is it possible to cast between a PETScMatrix, a petsc4py.PETSc.Mat matrix, and a scipy sparse matrix type without copying data in the first place? Or is the data stored differently somehow? I believe they are all stored in CSR-format, so I hope it is. It would speed up my code with 200x, so any help is appreciated :-)

1 Answer

0 votes
 
Best answer

It's not possible at the moment.

You could add your weight to the issue at https://bitbucket.org/fenics-project/dolfin/issues/519/. A reason that it's not so great to share data is that it breaks encapsulation since it gets into the details of the storage of the matrix format.

answered Mar 24, 2016 by Garth N. Wells FEniCS Expert (35,930 points)
selected Mar 24, 2016 by maartent

That is unfortunate, although the solution proposed to the issue looks good.

...