I am new to the PETSc package.

I have a ~4000x4000 matrix A in matrix-market format and I want to get PETSc to solve this using multiple processors.

I know how to solve the system on a single processor, but I don't know how to distribute the matrix and vectors among different processors.

Is there a simple set of instructions to do this?


1 Answer 1


Matrix Market is a terrible format for reading in parallel, therefore it is better to preprocess to a better parallel format. Your matrix size is extremely small so performance is not an issue, but the easiest and most general thing is to use Python or Matlab/Octave to write the Matrix Market file in PETSc binary format, which can be read efficiently in parallel using MatLoad(). For example, you can use this Python code for the preprocessing (add $PETSC_DIR/bin/pythonscripts to your PYTHONPATH)

import scipy.io, PetscBinaryIO
A = scipy.io.mmread('thematrix.mtx')
PetscBinaryIO.PetscBinaryIO().writeMatSciPy(open('petscmatrix','w'), A)

You can also write a vector to the file at this time. If you just want to read and solve the system, you can use src/ksp/ksp/examples/tutorials/ex10.c (with the option -f petscmatrix to read the binary file you just wrote).

In a real application, you should avoid a workflow that involves writing files to disk in any format. It is much better to assemble the matrix in parallel using a domain-decomposed representation of the problem. Most examples in PETSc are written this way.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.