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): 
    N=A.getSize()[0]
    B=PETSc.Mat().create(comm=PETSc.COMM_SELF)
    B.setType(PETSc.Mat.Type.SEQAIJ)
    B.setSizes(N)
    B.setUp()
    rstart, rend = A.getOwnershipRange()
    for i in xrange(rstart,rend):
        cols,vals = A.getRow(i) #maybe restore later
        B.setValues(i,cols,vals,addv=PETSc.InsertMode.INSERT)
    B.assemble()
    return B

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 acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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