I am curious, if there is a function to convert MPIAIJ (distributed matrices in AIJ format) to a SEQAIJ matrix that lie on a single processor. It is possible to do such an operation for PETSc vectors with VecScatterCreateToAll or VecScatterCreateToZero, but I couldn't find a similar function for matrices. It is not a scalable operation obviously, but can be helpful for debugging easily.

Naively, I thought the following will work, but the problem with this code is that every process generate a different SEQAIJ matrix. Interestingly, petsc4py doesn't generate an error, but simply leaves the matrix with all zeros.

#Input A is an MPIAIJ matrix
def getSEQAIJ(A): 
    rstart, rend = A.getOwnershipRange()
    for i in xrange(rstart,rend):
        cols,vals = A.getRow(i) #maybe restore later
    return B
  • $\begingroup$ Ist there an answer in 2019? Im also interested in this topic. The solution has not to be scalable at all. It's just for debugging reasons. $\endgroup$
    – BeiHerta
    Commented Feb 6, 2019 at 4:49

1 Answer 1


I haven't used PETSc via Python, but I suppose you can use all the functions provided by PETSc. Then you can use MatGetSubmatrices() on your processor 0 to obtain all rows and columns, thus obtaining a local copy.

The following discussion on PETSc-maillist has a very similar question answered and some sample code provided, though, without python involved.


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.