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

Read and write matrix in dolfin

+4 votes

Hi

I want to use dolfin to solve the linear system of equations using Petsc backend. Does Dolfin support to read and write matrix market file format or any other kind of matrix format?

Thanks in advance
Giang Bui

asked Jul 8, 2013 by kstn FEniCS Novice (280 points)

2 Answers

+2 votes
 
Best answer

Output

M = PETScMatrix()
M.binary_dump(filename)

Input is not supported. But you can call PETSc I/O routines directly. In python interface of DOLFIN you need to use compile_extension_module functionality to do this.

answered Jul 8, 2013 by Jan Blechta FEniCS Expert (51,420 points)
selected Jul 19, 2013 by Jan Blechta

Hi

I created new Mat and reset the input matrix

Mat U;
ierr = MatCreate(PETSC_COMM_WORLD,&U);CHKERRV(ierr);
ierr = MatSetType(U,MATMPIAIJ);CHKERRV(ierr);
ierr = MatLoad(U,view);CHKERRV(ierr);
mat.reset(new dolfin::PETScMatrix(U));

But the error throwed:
RuntimeError:

*** -------------------------------------------------------------------------
*** DOLFIN encountered an error. If you are not able to resolve this issue
*** using the information listed below, you can ask for help at


*** fenics@fenicsproject.org


*** Remember to include the error message listed below and, if possible,
*** include a minimal running example to reproduce the error.


*** -------------------------------------------------------------------------
*** Error: Unable to create GPU matrix.
*** Reason: PETSc not compiled with Cusp support.
*** Where: This error was encountered inside PETScMatrix.cpp.
*** Process: 0
*** -------------------------------------------------------------------------

I thought gpu invocation is disabled by default ?

You want to call constructor with signature

PETScMatrix(Mat A);

but no such exists. Your call gets probably casted into

PETScMatrix(true);

which is call to this constructor

PETScMatrix(bool use_gpu=false);

You need to use constructor

explicit PETScMatrix(boost::shared_ptr<Mat> A, bool use_gpu=false);

i.e. provide pointer, not value.

Ohh. I'm stupid.

Hi

Sorry for question again and again. Now I'm successfully read from matrixmarket file and plays a bit with various solver in dolfin. Next I want to improve on performance by letting dolfin read the matrix & vector in my code and try to solve. In my code, the matrix format is boost::ublas::compressed_matrix and is exported to python as CompressedMatrix. I tried to read the CompressedMatrix to dolfin by creating a function:

void dump_ublas_compressed_matrix_vector(
std::string fileout,
boost::numeric::ublas::compressed_matrix<double>& ublasA,
boost::numeric::ublas::vector<double>& ublasb
)

The purpose of this function is to write the matrix in ublas format to petsc binary file and loaded again to parallel matrix by dolfin to solve parallelly. In the code, I called

def Solve(self, A, x, b): //A and b are CompressedMatrix and Vector
sample_modules.dump_ublas_compressed_matrix_vector('temp_Ab', A, b)
....

However, the error throwed:
return _dolfin_compile_code_73d36380eae28ddfe74ebc2ea2813f72.dump_ublas_compressed_matrix_vector(*args)
TypeError: in method 'dump_ublas_compressed_matrix_vector', argument 2 of type 'boost::numeric::ublas::compressed_matrix& '

Is there a way to resolve type conflict of my format and the code compiled by dolfin? / How can I interface my matrix format to compiled module in dolfin?

P/S: I also tried to use void * as parameters and cast later in the code but since python pass object as reference this also doesn't work

Thanks
Bui

I don't understand it. Please, provide runnable code (and indent code by four spaces).

+3 votes

Just for the record, output to matrix-market format with the uBLAS backend is probably best done by

rows, cols, values = A.data()
A = scipy.sparse.csr_matrix((values, cols, rows))
scipy.io.mmwrite(filename, A)
answered Feb 6, 2014 by nschloe FEniCS User (7,120 points)
...