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.