[petsc-users] Mat.createNest in petsc4py

Jed Brown jed at jedbrown.org
Fri Apr 4 12:48:55 CDT 2014

Amit Itagi <gcfrai at gmail.com> writes:

> I tried the equivalent version with petsc in C++:
>  Mat subA[4];
>  Mat A;
>   ...
>  MatCreateNest(comm,2,NULL,2,NULL,subA,&A);
> I did not face any issues. I am trying to map it to petsc4py.

You should take Matt's suggestion.

Sometimes it is best to look at the source.  Lisandro his limited time
to work on petsc4py, and he spends it ensuring that everything works
correctly rather than carefully documenting the interface quirks.
Unfortunately, this means it can be tricky to guess how an interface
should be used.  It will probably stay this way until either
cross-language documentation becomes more automated or someone finds
enough time to spend on petsc4py documentation.

    def createNest(self, mats, isrows=None, iscols=None, comm=None):
        mats = [list(mat) for mat in mats]
        if isrows:
            isrows = list(isrows)
            assert len(isrows) == len(mats)
            isrows = None
        if iscols:
            iscols = list(iscols)
            assert len(iscols) == len(mats[0])
            iscols = None
        cdef MPI_Comm ccomm = def_Comm(comm, PETSC_COMM_DEFAULT)
        cdef Py_ssize_t i, mr = len(mats)
        cdef Py_ssize_t j, mc = len(mats[0])
        cdef PetscInt nr = <PetscInt>mr
        cdef PetscInt nc = <PetscInt>mc
        cdef PetscMat *cmats   = NULL
        cdef PetscIS  *cisrows = NULL
        cdef PetscIS  *ciscols = NULL
        cdef object tmp1, tmp2, tmp3
        tmp1 = oarray_p(empty_p(nr*nc), NULL, <void**>&cmats)
        for i from 0 <= i < mr:
            for j from 0 <= j < mc:
                cmats[i*mc+j] = (<Mat?>mats[i][j]).mat
        if isrows is not None:
            tmp2 = oarray_p(empty_p(nr), NULL, <void**>&cisrows)
            for i from 0 <= i < mr: cisrows[i] = (<IS?>isrows[i]).iset
        if iscols is not None:
            tmp3 = oarray_p(empty_p(nc), NULL, <void**>&ciscols)
            for j from 0 <= j < mc: ciscols[j] = (<IS?>iscols[j]).iset
        cdef PetscMat newmat = NULL
        CHKERR( MatCreateNest(ccomm, nr, cisrows, nc, ciscols, cmats, &newmat) )
        PetscCLEAR(self.obj); self.mat = newmat
        return self
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 835 bytes
Desc: not available
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20140404/80c870ab/attachment-0001.pgp>

More information about the petsc-users mailing list