2
$\begingroup$

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
$\endgroup$
  • $\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$ – JonasMu Feb 6 at 4:49
1
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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