xref: /petsc/src/ksp/pc/impls/gasm/gasm.c (revision d71ae5a4db6382e7f06317b8d368875286fe9008)
1b1a0a8a3SJed Brown /*
2f746d493SDmitry Karpeev   This file defines an "generalized" additive Schwarz preconditioner for any Mat implementation.
3f1580f4eSBarry Smith   In this version each MPI rank may intersect multiple subdomains and any subdomain may
4f1580f4eSBarry Smith   intersect multiple MPI ranks.  Intersections of subdomains with MPI ranks are called *local
56a4f0f73SDmitry Karpeev   subdomains*.
6b1a0a8a3SJed Brown 
78f3b4b4dSDmitry Karpeev        N    - total number of distinct global subdomains          (set explicitly in PCGASMSetTotalSubdomains() or implicitly PCGASMSetSubdomains() and then calculated in PCSetUp_GASM())
8f1580f4eSBarry Smith        n    - actual number of local subdomains on this rank (set in PCGASMSetSubdomains() or calculated in PCGASMSetTotalSubdomains())
9f1580f4eSBarry Smith        nmax - maximum number of local subdomains per rank    (calculated in PCSetUp_GASM())
10b1a0a8a3SJed Brown */
11af0996ceSBarry Smith #include <petsc/private/pcimpl.h> /*I "petscpc.h" I*/
121e25c274SJed Brown #include <petscdm.h>
13b1a0a8a3SJed Brown 
14b1a0a8a3SJed Brown typedef struct {
15f746d493SDmitry Karpeev   PetscInt   N, n, nmax;
16b1a0a8a3SJed Brown   PetscInt   overlap;                /* overlap requested by user */
178f3b4b4dSDmitry Karpeev   PCGASMType type;                   /* use reduced interpolation, restriction or both */
188f3b4b4dSDmitry Karpeev   PetscBool  type_set;               /* if user set this value (so won't change it for symmetric problems) */
198f3b4b4dSDmitry Karpeev   PetscBool  same_subdomain_solvers; /* flag indicating whether all local solvers are same */
208f3b4b4dSDmitry Karpeev   PetscBool  sort_indices;           /* flag to sort subdomain indices */
218f3b4b4dSDmitry Karpeev   PetscBool  user_subdomains;        /* whether the user set explicit subdomain index sets -- keep them on PCReset() */
228f3b4b4dSDmitry Karpeev   PetscBool  dm_subdomains;          /* whether DM is allowed to define subdomains */
23ea91fabdSFande Kong   PetscBool  hierarchicalpartitioning;
248f3b4b4dSDmitry Karpeev   IS        *ois;           /* index sets that define the outer (conceptually, overlapping) subdomains */
258f3b4b4dSDmitry Karpeev   IS        *iis;           /* index sets that define the inner (conceptually, nonoverlapping) subdomains */
268f3b4b4dSDmitry Karpeev   KSP       *ksp;           /* linear solvers for each subdomain */
278f3b4b4dSDmitry Karpeev   Mat       *pmat;          /* subdomain block matrices */
28f746d493SDmitry Karpeev   Vec        gx, gy;        /* Merged work vectors */
29f746d493SDmitry Karpeev   Vec       *x, *y;         /* Split work vectors; storage aliases pieces of storage of the above merged vectors. */
306a4f0f73SDmitry Karpeev   VecScatter gorestriction; /* merged restriction to disjoint union of outer subdomains */
316a4f0f73SDmitry Karpeev   VecScatter girestriction; /* merged restriction to disjoint union of inner subdomains */
32ea91fabdSFande Kong   VecScatter pctoouter;
33ea91fabdSFande Kong   IS         permutationIS;
34ea91fabdSFande Kong   Mat        permutationP;
35ea91fabdSFande Kong   Mat        pcmat;
36ea91fabdSFande Kong   Vec        pcx, pcy;
37f746d493SDmitry Karpeev } PC_GASM;
38b1a0a8a3SJed Brown 
39*d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGASMComputeGlobalSubdomainNumbering_Private(PC pc, PetscInt **numbering, PetscInt **permutation)
40*d71ae5a4SJacob Faibussowitsch {
418f3b4b4dSDmitry Karpeev   PC_GASM *osm = (PC_GASM *)pc->data;
428f3b4b4dSDmitry Karpeev   PetscInt i;
438f3b4b4dSDmitry Karpeev 
448f3b4b4dSDmitry Karpeev   PetscFunctionBegin;
458f3b4b4dSDmitry Karpeev   /* Determine the number of globally-distinct subdomains and compute a global numbering for them. */
469566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(osm->n, numbering, osm->n, permutation));
479566063dSJacob Faibussowitsch   PetscCall(PetscObjectsListGetGlobalNumbering(PetscObjectComm((PetscObject)pc), osm->n, (PetscObject *)osm->iis, NULL, *numbering));
488f3b4b4dSDmitry Karpeev   for (i = 0; i < osm->n; ++i) (*permutation)[i] = i;
499566063dSJacob Faibussowitsch   PetscCall(PetscSortIntWithPermutation(osm->n, *numbering, *permutation));
508f3b4b4dSDmitry Karpeev   PetscFunctionReturn(0);
518f3b4b4dSDmitry Karpeev }
528f3b4b4dSDmitry Karpeev 
53*d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGASMSubdomainView_Private(PC pc, PetscInt i, PetscViewer viewer)
54*d71ae5a4SJacob Faibussowitsch {
55af538404SDmitry Karpeev   PC_GASM        *osm = (PC_GASM *)pc->data;
5606b43e7eSDmitry Karpeev   PetscInt        j, nidx;
57af538404SDmitry Karpeev   const PetscInt *idx;
5806b43e7eSDmitry Karpeev   PetscViewer     sviewer;
59af538404SDmitry Karpeev   char           *cidx;
60af538404SDmitry Karpeev 
61af538404SDmitry Karpeev   PetscFunctionBegin;
622472a847SBarry Smith   PetscCheck(i >= 0 && i < osm->n, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_WRONG, "Invalid subdomain %" PetscInt_FMT ": must nonnegative and less than %" PetscInt_FMT, i, osm->n);
634bde246dSDmitry Karpeev   /* Inner subdomains. */
649566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(osm->iis[i], &nidx));
654bde246dSDmitry Karpeev   /*
664bde246dSDmitry Karpeev    No more than 15 characters per index plus a space.
674bde246dSDmitry Karpeev    PetscViewerStringSPrintf requires a string of size at least 2, so use (nidx+1) instead of nidx,
684bde246dSDmitry Karpeev    in case nidx == 0. That will take care of the space for the trailing '\0' as well.
694bde246dSDmitry Karpeev    For nidx == 0, the whole string 16 '\0'.
704bde246dSDmitry Karpeev    */
7136a9e3b9SBarry Smith #define len 16 * (nidx + 1) + 1
729566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(len, &cidx));
739566063dSJacob Faibussowitsch   PetscCall(PetscViewerStringOpen(PETSC_COMM_SELF, cidx, len, &sviewer));
7436a9e3b9SBarry Smith #undef len
759566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(osm->iis[i], &idx));
7648a46eb9SPierre Jolivet   for (j = 0; j < nidx; ++j) PetscCall(PetscViewerStringSPrintf(sviewer, "%" PetscInt_FMT " ", idx[j]));
779566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(osm->iis[i], &idx));
789566063dSJacob Faibussowitsch   PetscCall(PetscViewerDestroy(&sviewer));
799566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "Inner subdomain:\n"));
809566063dSJacob Faibussowitsch   PetscCall(PetscViewerFlush(viewer));
819566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPushSynchronized(viewer));
829566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%s", cidx));
839566063dSJacob Faibussowitsch   PetscCall(PetscViewerFlush(viewer));
849566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPopSynchronized(viewer));
859566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
869566063dSJacob Faibussowitsch   PetscCall(PetscViewerFlush(viewer));
879566063dSJacob Faibussowitsch   PetscCall(PetscFree(cidx));
884bde246dSDmitry Karpeev   /* Outer subdomains. */
899566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(osm->ois[i], &nidx));
90eec7e3faSDmitry Karpeev   /*
91eec7e3faSDmitry Karpeev    No more than 15 characters per index plus a space.
92eec7e3faSDmitry Karpeev    PetscViewerStringSPrintf requires a string of size at least 2, so use (nidx+1) instead of nidx,
93eec7e3faSDmitry Karpeev    in case nidx == 0. That will take care of the space for the trailing '\0' as well.
94eec7e3faSDmitry Karpeev    For nidx == 0, the whole string 16 '\0'.
95eec7e3faSDmitry Karpeev    */
9636a9e3b9SBarry Smith #define len 16 * (nidx + 1) + 1
979566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(len, &cidx));
989566063dSJacob Faibussowitsch   PetscCall(PetscViewerStringOpen(PETSC_COMM_SELF, cidx, len, &sviewer));
9936a9e3b9SBarry Smith #undef len
1009566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(osm->ois[i], &idx));
10148a46eb9SPierre Jolivet   for (j = 0; j < nidx; ++j) PetscCall(PetscViewerStringSPrintf(sviewer, "%" PetscInt_FMT " ", idx[j]));
1029566063dSJacob Faibussowitsch   PetscCall(PetscViewerDestroy(&sviewer));
1039566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(osm->ois[i], &idx));
1049566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "Outer subdomain:\n"));
1059566063dSJacob Faibussowitsch   PetscCall(PetscViewerFlush(viewer));
1069566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPushSynchronized(viewer));
1079566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%s", cidx));
1089566063dSJacob Faibussowitsch   PetscCall(PetscViewerFlush(viewer));
1099566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPopSynchronized(viewer));
1109566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
1119566063dSJacob Faibussowitsch   PetscCall(PetscViewerFlush(viewer));
1129566063dSJacob Faibussowitsch   PetscCall(PetscFree(cidx));
11306b43e7eSDmitry Karpeev   PetscFunctionReturn(0);
11406b43e7eSDmitry Karpeev }
11506b43e7eSDmitry Karpeev 
116*d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGASMPrintSubdomains(PC pc)
117*d71ae5a4SJacob Faibussowitsch {
11806b43e7eSDmitry Karpeev   PC_GASM    *osm = (PC_GASM *)pc->data;
11906b43e7eSDmitry Karpeev   const char *prefix;
12006b43e7eSDmitry Karpeev   char        fname[PETSC_MAX_PATH_LEN + 1];
1218f3b4b4dSDmitry Karpeev   PetscInt    l, d, count;
1229140fbc5SPierre Jolivet   PetscBool   found;
1230298fd71SBarry Smith   PetscViewer viewer, sviewer = NULL;
1248f3b4b4dSDmitry Karpeev   PetscInt   *numbering, *permutation; /* global numbering of locally-supported subdomains and the permutation from the local ordering */
12506b43e7eSDmitry Karpeev 
12606b43e7eSDmitry Karpeev   PetscFunctionBegin;
1279566063dSJacob Faibussowitsch   PetscCall(PCGetOptionsPrefix(pc, &prefix));
1289566063dSJacob Faibussowitsch   PetscCall(PetscOptionsHasName(NULL, prefix, "-pc_gasm_print_subdomains", &found));
1299140fbc5SPierre Jolivet   if (!found) PetscFunctionReturn(0);
1309566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetString(NULL, prefix, "-pc_gasm_print_subdomains", fname, sizeof(fname), &found));
1319566063dSJacob Faibussowitsch   if (!found) PetscCall(PetscStrcpy(fname, "stdout"));
1329566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIOpen(PetscObjectComm((PetscObject)pc), fname, &viewer));
1334bde246dSDmitry Karpeev   /*
1344bde246dSDmitry Karpeev    Make sure the viewer has a name. Otherwise this may cause a deadlock or other weird errors when creating a subcomm viewer:
1354bde246dSDmitry Karpeev    the subcomm viewer will attempt to inherit the viewer's name, which, if not set, will be constructed collectively on the comm.
1364bde246dSDmitry Karpeev   */
1379566063dSJacob Faibussowitsch   PetscCall(PetscObjectName((PetscObject)viewer));
1384bde246dSDmitry Karpeev   l = 0;
1399566063dSJacob Faibussowitsch   PetscCall(PCGASMComputeGlobalSubdomainNumbering_Private(pc, &numbering, &permutation));
1408f3b4b4dSDmitry Karpeev   for (count = 0; count < osm->N; ++count) {
1414bde246dSDmitry Karpeev     /* Now let subdomains go one at a time in the global numbering order and print their subdomain/solver info. */
1424bde246dSDmitry Karpeev     if (l < osm->n) {
1434bde246dSDmitry Karpeev       d = permutation[l]; /* d is the local number of the l-th smallest (in the global ordering) among the locally supported subdomains */
1444bde246dSDmitry Karpeev       if (numbering[d] == count) {
1459566063dSJacob Faibussowitsch         PetscCall(PetscViewerGetSubViewer(viewer, ((PetscObject)osm->ois[d])->comm, &sviewer));
1469566063dSJacob Faibussowitsch         PetscCall(PCGASMSubdomainView_Private(pc, d, sviewer));
1479566063dSJacob Faibussowitsch         PetscCall(PetscViewerRestoreSubViewer(viewer, ((PetscObject)osm->ois[d])->comm, &sviewer));
1484bde246dSDmitry Karpeev         ++l;
149af538404SDmitry Karpeev       }
1504bde246dSDmitry Karpeev     }
1519566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Barrier(PetscObjectComm((PetscObject)pc)));
1524bde246dSDmitry Karpeev   }
1539566063dSJacob Faibussowitsch   PetscCall(PetscFree2(numbering, permutation));
1549566063dSJacob Faibussowitsch   PetscCall(PetscViewerDestroy(&viewer));
155af538404SDmitry Karpeev   PetscFunctionReturn(0);
156af538404SDmitry Karpeev }
157af538404SDmitry Karpeev 
158*d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCView_GASM(PC pc, PetscViewer viewer)
159*d71ae5a4SJacob Faibussowitsch {
160f746d493SDmitry Karpeev   PC_GASM    *osm = (PC_GASM *)pc->data;
161af538404SDmitry Karpeev   const char *prefix;
162af538404SDmitry Karpeev   PetscMPIInt rank, size;
1638f3b4b4dSDmitry Karpeev   PetscInt    bsz;
16406b43e7eSDmitry Karpeev   PetscBool   iascii, view_subdomains = PETSC_FALSE;
165b1a0a8a3SJed Brown   PetscViewer sviewer;
1668f3b4b4dSDmitry Karpeev   PetscInt    count, l;
1676a4f0f73SDmitry Karpeev   char        overlap[256]     = "user-defined overlap";
1686a4f0f73SDmitry Karpeev   char        gsubdomains[256] = "unknown total number of subdomains";
16906b43e7eSDmitry Karpeev   char        msubdomains[256] = "unknown max number of local subdomains";
1708f3b4b4dSDmitry Karpeev   PetscInt   *numbering, *permutation; /* global numbering of locally-supported subdomains and the permutation from the local ordering */
17111aeaf0aSBarry Smith 
172b1a0a8a3SJed Brown   PetscFunctionBegin;
1739566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size));
1749566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)pc), &rank));
17506b43e7eSDmitry Karpeev 
17648a46eb9SPierre Jolivet   if (osm->overlap >= 0) PetscCall(PetscSNPrintf(overlap, sizeof(overlap), "requested amount of overlap = %" PetscInt_FMT, osm->overlap));
17748a46eb9SPierre Jolivet   if (osm->N != PETSC_DETERMINE) PetscCall(PetscSNPrintf(gsubdomains, sizeof(gsubdomains), "total number of subdomains = %" PetscInt_FMT, osm->N));
17848a46eb9SPierre Jolivet   if (osm->nmax != PETSC_DETERMINE) PetscCall(PetscSNPrintf(msubdomains, sizeof(msubdomains), "max number of local subdomains = %" PetscInt_FMT, osm->nmax));
17906b43e7eSDmitry Karpeev 
1809566063dSJacob Faibussowitsch   PetscCall(PCGetOptionsPrefix(pc, &prefix));
1819566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(NULL, prefix, "-pc_gasm_view_subdomains", &view_subdomains, NULL));
18206b43e7eSDmitry Karpeev 
1839566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
184b1a0a8a3SJed Brown   if (iascii) {
18506b43e7eSDmitry Karpeev     /*
18606b43e7eSDmitry Karpeev      Make sure the viewer has a name. Otherwise this may cause a deadlock when creating a subcomm viewer:
18706b43e7eSDmitry Karpeev      the subcomm viewer will attempt to inherit the viewer's name, which, if not set, will be constructed
18806b43e7eSDmitry Karpeev      collectively on the comm.
18906b43e7eSDmitry Karpeev      */
1909566063dSJacob Faibussowitsch     PetscCall(PetscObjectName((PetscObject)viewer));
1919566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "  Restriction/interpolation type: %s\n", PCGASMTypes[osm->type]));
1929566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "  %s\n", overlap));
1939566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "  %s\n", gsubdomains));
1949566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "  %s\n", msubdomains));
1959566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPushSynchronized(viewer));
19663a3b9bcSJacob Faibussowitsch     PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "  [%d|%d] number of locally-supported subdomains = %" PetscInt_FMT "\n", rank, size, osm->n));
1979566063dSJacob Faibussowitsch     PetscCall(PetscViewerFlush(viewer));
1989566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPopSynchronized(viewer));
1996a4f0f73SDmitry Karpeev     /* Cannot take advantage of osm->same_subdomain_solvers without a global numbering of subdomains. */
2009566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "  Subdomain solver info is as follows:\n"));
2019566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPushTab(viewer));
2029566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "  - - - - - - - - - - - - - - - - - -\n"));
20306b43e7eSDmitry Karpeev     /* Make sure that everybody waits for the banner to be printed. */
2049566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Barrier(PetscObjectComm((PetscObject)viewer)));
2054bde246dSDmitry Karpeev     /* Now let subdomains go one at a time in the global numbering order and print their subdomain/solver info. */
2069566063dSJacob Faibussowitsch     PetscCall(PCGASMComputeGlobalSubdomainNumbering_Private(pc, &numbering, &permutation));
20706b43e7eSDmitry Karpeev     l = 0;
2088f3b4b4dSDmitry Karpeev     for (count = 0; count < osm->N; ++count) {
20906b43e7eSDmitry Karpeev       PetscMPIInt srank, ssize;
21006b43e7eSDmitry Karpeev       if (l < osm->n) {
21106b43e7eSDmitry Karpeev         PetscInt d = permutation[l]; /* d is the local number of the l-th smallest (in the global ordering) among the locally supported subdomains */
21206b43e7eSDmitry Karpeev         if (numbering[d] == count) {
2139566063dSJacob Faibussowitsch           PetscCallMPI(MPI_Comm_size(((PetscObject)osm->ois[d])->comm, &ssize));
2149566063dSJacob Faibussowitsch           PetscCallMPI(MPI_Comm_rank(((PetscObject)osm->ois[d])->comm, &srank));
2159566063dSJacob Faibussowitsch           PetscCall(PetscViewerGetSubViewer(viewer, ((PetscObject)osm->ois[d])->comm, &sviewer));
2169566063dSJacob Faibussowitsch           PetscCall(ISGetLocalSize(osm->ois[d], &bsz));
21763a3b9bcSJacob Faibussowitsch           PetscCall(PetscViewerASCIISynchronizedPrintf(sviewer, "  [%d|%d] (subcomm [%d|%d]) local subdomain number %" PetscInt_FMT ", local size = %" PetscInt_FMT "\n", rank, size, srank, ssize, d, bsz));
2189566063dSJacob Faibussowitsch           PetscCall(PetscViewerFlush(sviewer));
2191baa6e33SBarry Smith           if (view_subdomains) PetscCall(PCGASMSubdomainView_Private(pc, d, sviewer));
2206a4f0f73SDmitry Karpeev           if (!pc->setupcalled) {
2219566063dSJacob Faibussowitsch             PetscCall(PetscViewerASCIIPrintf(sviewer, "  Solver not set up yet: PCSetUp() not yet called\n"));
2222fa5cd67SKarl Rupp           } else {
2239566063dSJacob Faibussowitsch             PetscCall(KSPView(osm->ksp[d], sviewer));
2246a4f0f73SDmitry Karpeev           }
2259566063dSJacob Faibussowitsch           PetscCall(PetscViewerASCIIPrintf(sviewer, "  - - - - - - - - - - - - - - - - - -\n"));
2269566063dSJacob Faibussowitsch           PetscCall(PetscViewerFlush(sviewer));
2279566063dSJacob Faibussowitsch           PetscCall(PetscViewerRestoreSubViewer(viewer, ((PetscObject)osm->ois[d])->comm, &sviewer));
22806b43e7eSDmitry Karpeev           ++l;
229b1a0a8a3SJed Brown         }
23006b43e7eSDmitry Karpeev       }
2319566063dSJacob Faibussowitsch       PetscCallMPI(MPI_Barrier(PetscObjectComm((PetscObject)pc)));
232b1a0a8a3SJed Brown     }
2339566063dSJacob Faibussowitsch     PetscCall(PetscFree2(numbering, permutation));
2349566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPopTab(viewer));
2359566063dSJacob Faibussowitsch     PetscCall(PetscViewerFlush(viewer));
2369530cbd7SBarry Smith     /* this line is needed to match the extra PetscViewerASCIIPushSynchronized() in PetscViewerGetSubViewer() */
2379566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPopSynchronized(viewer));
2389530cbd7SBarry Smith   }
239b1a0a8a3SJed Brown   PetscFunctionReturn(0);
240b1a0a8a3SJed Brown }
241b1a0a8a3SJed Brown 
2428f3b4b4dSDmitry Karpeev PETSC_INTERN PetscErrorCode PCGASMCreateLocalSubdomains(Mat A, PetscInt nloc, IS *iis[]);
243af538404SDmitry Karpeev 
244*d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGASMSetHierarchicalPartitioning(PC pc)
245*d71ae5a4SJacob Faibussowitsch {
246ea91fabdSFande Kong   PC_GASM        *osm = (PC_GASM *)pc->data;
247ea91fabdSFande Kong   MatPartitioning part;
248ea91fabdSFande Kong   MPI_Comm        comm;
249ea91fabdSFande Kong   PetscMPIInt     size;
250ea91fabdSFande Kong   PetscInt        nlocalsubdomains, fromrows_localsize;
251ea91fabdSFande Kong   IS              partitioning, fromrows, isn;
252ea91fabdSFande Kong   Vec             outervec;
253ea91fabdSFande Kong 
254ea91fabdSFande Kong   PetscFunctionBegin;
2559566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)pc, &comm));
2569566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
257ea91fabdSFande Kong   /* we do not need a hierarchical partitioning when
258ea91fabdSFande Kong     * the total number of subdomains is consistent with
259ea91fabdSFande Kong     * the number of MPI tasks.
260ea91fabdSFande Kong     * For the following cases, we do not need to use HP
261ea91fabdSFande Kong     * */
262670417b2SFande Kong   if (osm->N == PETSC_DETERMINE || osm->N >= size || osm->N == 1) PetscFunctionReturn(0);
263f1580f4eSBarry Smith   PetscCheck(size % osm->N == 0, PETSC_COMM_WORLD, PETSC_ERR_ARG_INCOMP, "have to specify the total number of subdomains %" PetscInt_FMT " to be a factor of the number of ranks %d ", osm->N, size);
264ea91fabdSFande Kong   nlocalsubdomains = size / osm->N;
265ea91fabdSFande Kong   osm->n           = 1;
2669566063dSJacob Faibussowitsch   PetscCall(MatPartitioningCreate(comm, &part));
2679566063dSJacob Faibussowitsch   PetscCall(MatPartitioningSetAdjacency(part, pc->pmat));
2689566063dSJacob Faibussowitsch   PetscCall(MatPartitioningSetType(part, MATPARTITIONINGHIERARCH));
2699566063dSJacob Faibussowitsch   PetscCall(MatPartitioningHierarchicalSetNcoarseparts(part, osm->N));
2709566063dSJacob Faibussowitsch   PetscCall(MatPartitioningHierarchicalSetNfineparts(part, nlocalsubdomains));
2719566063dSJacob Faibussowitsch   PetscCall(MatPartitioningSetFromOptions(part));
272f1580f4eSBarry Smith   /* get new rank owner number of each vertex */
2739566063dSJacob Faibussowitsch   PetscCall(MatPartitioningApply(part, &partitioning));
2749566063dSJacob Faibussowitsch   PetscCall(ISBuildTwoSided(partitioning, NULL, &fromrows));
2759566063dSJacob Faibussowitsch   PetscCall(ISPartitioningToNumbering(partitioning, &isn));
2769566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&isn));
2779566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(fromrows, &fromrows_localsize));
2789566063dSJacob Faibussowitsch   PetscCall(MatPartitioningDestroy(&part));
2799566063dSJacob Faibussowitsch   PetscCall(MatCreateVecs(pc->pmat, &outervec, NULL));
2809566063dSJacob Faibussowitsch   PetscCall(VecCreateMPI(comm, fromrows_localsize, PETSC_DETERMINE, &(osm->pcx)));
2819566063dSJacob Faibussowitsch   PetscCall(VecDuplicate(osm->pcx, &(osm->pcy)));
2829566063dSJacob Faibussowitsch   PetscCall(VecScatterCreate(osm->pcx, NULL, outervec, fromrows, &(osm->pctoouter)));
2839566063dSJacob Faibussowitsch   PetscCall(MatCreateSubMatrix(pc->pmat, fromrows, fromrows, MAT_INITIAL_MATRIX, &(osm->permutationP)));
2849566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fromrows));
285ea91fabdSFande Kong   osm->permutationIS = fromrows;
286ea91fabdSFande Kong   osm->pcmat         = pc->pmat;
2879566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)osm->permutationP));
288ea91fabdSFande Kong   pc->pmat = osm->permutationP;
2899566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&outervec));
2909566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&fromrows));
2919566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&partitioning));
292ea91fabdSFande Kong   osm->n = PETSC_DETERMINE;
293ea91fabdSFande Kong   PetscFunctionReturn(0);
294ea91fabdSFande Kong }
295ea91fabdSFande Kong 
296*d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCSetUp_GASM(PC pc)
297*d71ae5a4SJacob Faibussowitsch {
298f746d493SDmitry Karpeev   PC_GASM        *osm = (PC_GASM *)pc->data;
299ea91fabdSFande Kong   PetscInt        i, nInnerIndices, nTotalInnerIndices;
300c10bc1a1SDmitry Karpeev   PetscMPIInt     rank, size;
301b1a0a8a3SJed Brown   MatReuse        scall = MAT_REUSE_MATRIX;
302b1a0a8a3SJed Brown   KSP             ksp;
303b1a0a8a3SJed Brown   PC              subpc;
304b1a0a8a3SJed Brown   const char     *prefix, *pprefix;
305f746d493SDmitry Karpeev   Vec             x, y;
3066a4f0f73SDmitry Karpeev   PetscInt        oni;   /* Number of indices in the i-th local outer subdomain.               */
3076a4f0f73SDmitry Karpeev   const PetscInt *oidxi; /* Indices from the i-th subdomain local outer subdomain.             */
3086a4f0f73SDmitry Karpeev   PetscInt        on;    /* Number of indices in the disjoint union of local outer subdomains. */
3096a4f0f73SDmitry Karpeev   PetscInt       *oidx;  /* Indices in the disjoint union of local outer subdomains. */
3106a4f0f73SDmitry Karpeev   IS              gois;  /* Disjoint union the global indices of outer subdomains.             */
3116a4f0f73SDmitry Karpeev   IS              goid;  /* Identity IS of the size of the disjoint union of outer subdomains. */
312f746d493SDmitry Karpeev   PetscScalar    *gxarray, *gyarray;
313930d09c1SFande Kong   PetscInt        gostart; /* Start of locally-owned indices in the vectors -- osm->gx,osm->gy -- over the disjoint union of outer subdomains. */
3148f3b4b4dSDmitry Karpeev   PetscInt        num_subdomains  = 0;
3150298fd71SBarry Smith   DM             *subdomain_dm    = NULL;
3168f3b4b4dSDmitry Karpeev   char          **subdomain_names = NULL;
317f771a274SFande Kong   PetscInt       *numbering;
3188f3b4b4dSDmitry Karpeev 
319b1a0a8a3SJed Brown   PetscFunctionBegin;
3209566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size));
3219566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)pc), &rank));
322b1a0a8a3SJed Brown   if (!pc->setupcalled) {
323ea91fabdSFande Kong     /* use a hierarchical partitioning */
3241baa6e33SBarry Smith     if (osm->hierarchicalpartitioning) PetscCall(PCGASMSetHierarchicalPartitioning(pc));
3258f3b4b4dSDmitry Karpeev     if (osm->n == PETSC_DETERMINE) {
3268f3b4b4dSDmitry Karpeev       if (osm->N != PETSC_DETERMINE) {
3278f3b4b4dSDmitry Karpeev         /* No local subdomains given, but the desired number of total subdomains is known, so construct them accordingly. */
3289566063dSJacob Faibussowitsch         PetscCall(PCGASMCreateSubdomains(pc->pmat, osm->N, &osm->n, &osm->iis));
3298f3b4b4dSDmitry Karpeev       } else if (osm->dm_subdomains && pc->dm) {
3308f3b4b4dSDmitry Karpeev         /* try pc->dm next, if allowed */
3318f3b4b4dSDmitry Karpeev         PetscInt d;
332a35b7b57SDmitry Karpeev         IS      *inner_subdomain_is, *outer_subdomain_is;
3339566063dSJacob Faibussowitsch         PetscCall(DMCreateDomainDecomposition(pc->dm, &num_subdomains, &subdomain_names, &inner_subdomain_is, &outer_subdomain_is, &subdomain_dm));
3341baa6e33SBarry Smith         if (num_subdomains) PetscCall(PCGASMSetSubdomains(pc, num_subdomains, inner_subdomain_is, outer_subdomain_is));
335a35b7b57SDmitry Karpeev         for (d = 0; d < num_subdomains; ++d) {
3369566063dSJacob Faibussowitsch           if (inner_subdomain_is) PetscCall(ISDestroy(&inner_subdomain_is[d]));
3379566063dSJacob Faibussowitsch           if (outer_subdomain_is) PetscCall(ISDestroy(&outer_subdomain_is[d]));
338fdc48646SDmitry Karpeev         }
3399566063dSJacob Faibussowitsch         PetscCall(PetscFree(inner_subdomain_is));
3409566063dSJacob Faibussowitsch         PetscCall(PetscFree(outer_subdomain_is));
3418f3b4b4dSDmitry Karpeev       } else {
342f1580f4eSBarry Smith         /* still no subdomains; use one per rank */
34344fe18e5SDmitry Karpeev         osm->nmax = osm->n = 1;
3449566063dSJacob Faibussowitsch         PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size));
345f746d493SDmitry Karpeev         osm->N = size;
3469566063dSJacob Faibussowitsch         PetscCall(PCGASMCreateLocalSubdomains(pc->pmat, osm->n, &osm->iis));
347fdc48646SDmitry Karpeev       }
34806b43e7eSDmitry Karpeev     }
349a35b7b57SDmitry Karpeev     if (!osm->iis) {
350a35b7b57SDmitry Karpeev       /*
3518f3b4b4dSDmitry Karpeev        osm->n was set in PCGASMSetSubdomains(), but the actual subdomains have not been supplied.
3528f3b4b4dSDmitry Karpeev        We create the requisite number of local inner subdomains and then expand them into
3538f3b4b4dSDmitry Karpeev        out subdomains, if necessary.
354a35b7b57SDmitry Karpeev        */
3559566063dSJacob Faibussowitsch       PetscCall(PCGASMCreateLocalSubdomains(pc->pmat, osm->n, &osm->iis));
356a35b7b57SDmitry Karpeev     }
3578f3b4b4dSDmitry Karpeev     if (!osm->ois) {
3588f3b4b4dSDmitry Karpeev       /*
3598f3b4b4dSDmitry Karpeev             Initially make outer subdomains the same as inner subdomains. If nonzero additional overlap
3608f3b4b4dSDmitry Karpeev             has been requested, copy the inner subdomains over so they can be modified.
3618f3b4b4dSDmitry Karpeev       */
3629566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(osm->n, &osm->ois));
3638f3b4b4dSDmitry Karpeev       for (i = 0; i < osm->n; ++i) {
364ea91fabdSFande Kong         if (osm->overlap > 0 && osm->N > 1) { /* With positive overlap, osm->iis[i] will be modified */
3659566063dSJacob Faibussowitsch           PetscCall(ISDuplicate(osm->iis[i], (osm->ois) + i));
3669566063dSJacob Faibussowitsch           PetscCall(ISCopy(osm->iis[i], osm->ois[i]));
3678f3b4b4dSDmitry Karpeev         } else {
3689566063dSJacob Faibussowitsch           PetscCall(PetscObjectReference((PetscObject)((osm->iis)[i])));
3698f3b4b4dSDmitry Karpeev           osm->ois[i] = osm->iis[i];
3708f3b4b4dSDmitry Karpeev         }
3718f3b4b4dSDmitry Karpeev       }
372ea91fabdSFande Kong       if (osm->overlap > 0 && osm->N > 1) {
3738f3b4b4dSDmitry Karpeev         /* Extend the "overlapping" regions by a number of steps */
3749566063dSJacob Faibussowitsch         PetscCall(MatIncreaseOverlapSplit(pc->pmat, osm->n, osm->ois, osm->overlap));
3758f3b4b4dSDmitry Karpeev       }
376b1a0a8a3SJed Brown     }
3776a4f0f73SDmitry Karpeev 
3788f3b4b4dSDmitry Karpeev     /* Now the subdomains are defined.  Determine their global and max local numbers, if necessary. */
3798f3b4b4dSDmitry Karpeev     if (osm->nmax == PETSC_DETERMINE) {
3808f3b4b4dSDmitry Karpeev       PetscMPIInt inwork, outwork;
3818f3b4b4dSDmitry Karpeev       /* determine global number of subdomains and the max number of local subdomains */
3828f3b4b4dSDmitry Karpeev       inwork = osm->n;
3831c2dc1cbSBarry Smith       PetscCall(MPIU_Allreduce(&inwork, &outwork, 1, MPI_INT, MPI_MAX, PetscObjectComm((PetscObject)pc)));
3848f3b4b4dSDmitry Karpeev       osm->nmax = outwork;
3858f3b4b4dSDmitry Karpeev     }
3868f3b4b4dSDmitry Karpeev     if (osm->N == PETSC_DETERMINE) {
3878f3b4b4dSDmitry Karpeev       /* Determine the number of globally-distinct subdomains and compute a global numbering for them. */
3889566063dSJacob Faibussowitsch       PetscCall(PetscObjectsListGetGlobalNumbering(PetscObjectComm((PetscObject)pc), osm->n, (PetscObject *)osm->ois, &osm->N, NULL));
3898f3b4b4dSDmitry Karpeev     }
3908f3b4b4dSDmitry Karpeev 
391b1a0a8a3SJed Brown     if (osm->sort_indices) {
392f746d493SDmitry Karpeev       for (i = 0; i < osm->n; i++) {
3939566063dSJacob Faibussowitsch         PetscCall(ISSort(osm->ois[i]));
3949566063dSJacob Faibussowitsch         PetscCall(ISSort(osm->iis[i]));
395b1a0a8a3SJed Brown       }
396b1a0a8a3SJed Brown     }
3979566063dSJacob Faibussowitsch     PetscCall(PCGetOptionsPrefix(pc, &prefix));
3989566063dSJacob Faibussowitsch     PetscCall(PCGASMPrintSubdomains(pc));
3998f3b4b4dSDmitry Karpeev 
4006a4f0f73SDmitry Karpeev     /*
4016a4f0f73SDmitry Karpeev        Merge the ISs, create merged vectors and restrictions.
4026a4f0f73SDmitry Karpeev      */
4036a4f0f73SDmitry Karpeev     /* Merge outer subdomain ISs and construct a restriction onto the disjoint union of local outer subdomains. */
4046a4f0f73SDmitry Karpeev     on = 0;
405f746d493SDmitry Karpeev     for (i = 0; i < osm->n; i++) {
4069566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(osm->ois[i], &oni));
4076a4f0f73SDmitry Karpeev       on += oni;
408f746d493SDmitry Karpeev     }
4099566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(on, &oidx));
4106a4f0f73SDmitry Karpeev     on = 0;
411930d09c1SFande Kong     /* Merge local indices together */
412f746d493SDmitry Karpeev     for (i = 0; i < osm->n; i++) {
4139566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(osm->ois[i], &oni));
4149566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(osm->ois[i], &oidxi));
4159566063dSJacob Faibussowitsch       PetscCall(PetscArraycpy(oidx + on, oidxi, oni));
4169566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(osm->ois[i], &oidxi));
4176a4f0f73SDmitry Karpeev       on += oni;
418f746d493SDmitry Karpeev     }
4199566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(((PetscObject)(pc))->comm, on, oidx, PETSC_OWN_POINTER, &gois));
420ea91fabdSFande Kong     nTotalInnerIndices = 0;
421ea91fabdSFande Kong     for (i = 0; i < osm->n; i++) {
4229566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(osm->iis[i], &nInnerIndices));
423ea91fabdSFande Kong       nTotalInnerIndices += nInnerIndices;
424ea91fabdSFande Kong     }
4259566063dSJacob Faibussowitsch     PetscCall(VecCreateMPI(((PetscObject)(pc))->comm, nTotalInnerIndices, PETSC_DETERMINE, &x));
4269566063dSJacob Faibussowitsch     PetscCall(VecDuplicate(x, &y));
427ea91fabdSFande Kong 
4289566063dSJacob Faibussowitsch     PetscCall(VecCreateMPI(PetscObjectComm((PetscObject)pc), on, PETSC_DECIDE, &osm->gx));
4299566063dSJacob Faibussowitsch     PetscCall(VecDuplicate(osm->gx, &osm->gy));
4309566063dSJacob Faibussowitsch     PetscCall(VecGetOwnershipRange(osm->gx, &gostart, NULL));
4319566063dSJacob Faibussowitsch     PetscCall(ISCreateStride(PetscObjectComm((PetscObject)pc), on, gostart, 1, &goid));
432930d09c1SFande Kong     /* gois might indices not on local */
4339566063dSJacob Faibussowitsch     PetscCall(VecScatterCreate(x, gois, osm->gx, goid, &(osm->gorestriction)));
4349566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(osm->n, &numbering));
4359566063dSJacob Faibussowitsch     PetscCall(PetscObjectsListGetGlobalNumbering(PetscObjectComm((PetscObject)pc), osm->n, (PetscObject *)osm->ois, NULL, numbering));
4369566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&x));
4379566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&gois));
4382fa5cd67SKarl Rupp 
4396a4f0f73SDmitry Karpeev     /* Merge inner subdomain ISs and construct a restriction onto the disjoint union of local inner subdomains. */
4402fa5cd67SKarl Rupp     {
4412fa5cd67SKarl Rupp       PetscInt        ini;   /* Number of indices the i-th a local inner subdomain. */
4428966356dSPierre Jolivet       PetscInt        in;    /* Number of indices in the disjoint union of local inner subdomains. */
4436a4f0f73SDmitry Karpeev       PetscInt       *iidx;  /* Global indices in the merged local inner subdomain. */
4446a4f0f73SDmitry Karpeev       PetscInt       *ioidx; /* Global indices of the disjoint union of inner subdomains within the disjoint union of outer subdomains. */
4456a4f0f73SDmitry Karpeev       IS              giis;  /* IS for the disjoint union of inner subdomains. */
4466a4f0f73SDmitry Karpeev       IS              giois; /* IS for the disjoint union of inner subdomains within the disjoint union of outer subdomains. */
447f771a274SFande Kong       PetscScalar    *array;
448f771a274SFande Kong       const PetscInt *indices;
449f771a274SFande Kong       PetscInt        k;
4506a4f0f73SDmitry Karpeev       on = 0;
451f746d493SDmitry Karpeev       for (i = 0; i < osm->n; i++) {
4529566063dSJacob Faibussowitsch         PetscCall(ISGetLocalSize(osm->ois[i], &oni));
4536a4f0f73SDmitry Karpeev         on += oni;
454f746d493SDmitry Karpeev       }
4559566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(on, &iidx));
4569566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(on, &ioidx));
4579566063dSJacob Faibussowitsch       PetscCall(VecGetArray(y, &array));
458e12b4729SFande Kong       /* set communicator id to determine where overlap is */
459f771a274SFande Kong       in = 0;
460f771a274SFande Kong       for (i = 0; i < osm->n; i++) {
4619566063dSJacob Faibussowitsch         PetscCall(ISGetLocalSize(osm->iis[i], &ini));
462ad540459SPierre Jolivet         for (k = 0; k < ini; ++k) array[in + k] = numbering[i];
463f771a274SFande Kong         in += ini;
464f771a274SFande Kong       }
4659566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(y, &array));
4669566063dSJacob Faibussowitsch       PetscCall(VecScatterBegin(osm->gorestriction, y, osm->gy, INSERT_VALUES, SCATTER_FORWARD));
4679566063dSJacob Faibussowitsch       PetscCall(VecScatterEnd(osm->gorestriction, y, osm->gy, INSERT_VALUES, SCATTER_FORWARD));
4689566063dSJacob Faibussowitsch       PetscCall(VecGetOwnershipRange(osm->gy, &gostart, NULL));
4699566063dSJacob Faibussowitsch       PetscCall(VecGetArray(osm->gy, &array));
470f771a274SFande Kong       on = 0;
471f771a274SFande Kong       in = 0;
472f771a274SFande Kong       for (i = 0; i < osm->n; i++) {
4739566063dSJacob Faibussowitsch         PetscCall(ISGetLocalSize(osm->ois[i], &oni));
4749566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(osm->ois[i], &indices));
475f771a274SFande Kong         for (k = 0; k < oni; k++) {
476e12b4729SFande Kong           /*  skip overlapping indices to get inner domain */
47743081b6cSDmitry Karpeev           if (PetscRealPart(array[on + k]) != numbering[i]) continue;
478f771a274SFande Kong           iidx[in]    = indices[k];
479f771a274SFande Kong           ioidx[in++] = gostart + on + k;
480f771a274SFande Kong         }
4819566063dSJacob Faibussowitsch         PetscCall(ISRestoreIndices(osm->ois[i], &indices));
482f771a274SFande Kong         on += oni;
483f771a274SFande Kong       }
4849566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(osm->gy, &array));
4859566063dSJacob Faibussowitsch       PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)pc), in, iidx, PETSC_OWN_POINTER, &giis));
4869566063dSJacob Faibussowitsch       PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)pc), in, ioidx, PETSC_OWN_POINTER, &giois));
4879566063dSJacob Faibussowitsch       PetscCall(VecScatterCreate(y, giis, osm->gy, giois, &osm->girestriction));
4889566063dSJacob Faibussowitsch       PetscCall(VecDestroy(&y));
4899566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&giis));
4909566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&giois));
491b1a0a8a3SJed Brown     }
4929566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&goid));
4939566063dSJacob Faibussowitsch     PetscCall(PetscFree(numbering));
4942fa5cd67SKarl Rupp 
495f746d493SDmitry Karpeev     /* Create the subdomain work vectors. */
4969566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(osm->n, &osm->x));
4979566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(osm->n, &osm->y));
4989566063dSJacob Faibussowitsch     PetscCall(VecGetArray(osm->gx, &gxarray));
4999566063dSJacob Faibussowitsch     PetscCall(VecGetArray(osm->gy, &gyarray));
5006a4f0f73SDmitry Karpeev     for (i = 0, on = 0; i < osm->n; ++i, on += oni) {
5016a4f0f73SDmitry Karpeev       PetscInt oNi;
5029566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(osm->ois[i], &oni));
503930d09c1SFande Kong       /* on a sub communicator */
5049566063dSJacob Faibussowitsch       PetscCall(ISGetSize(osm->ois[i], &oNi));
5059566063dSJacob Faibussowitsch       PetscCall(VecCreateMPIWithArray(((PetscObject)(osm->ois[i]))->comm, 1, oni, oNi, gxarray + on, &osm->x[i]));
5069566063dSJacob Faibussowitsch       PetscCall(VecCreateMPIWithArray(((PetscObject)(osm->ois[i]))->comm, 1, oni, oNi, gyarray + on, &osm->y[i]));
507b1a0a8a3SJed Brown     }
5089566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(osm->gx, &gxarray));
5099566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(osm->gy, &gyarray));
5108f3b4b4dSDmitry Karpeev     /* Create the subdomain solvers */
5119566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(osm->n, &osm->ksp));
5128f3b4b4dSDmitry Karpeev     for (i = 0; i < osm->n; i++) {
5138f3b4b4dSDmitry Karpeev       char subprefix[PETSC_MAX_PATH_LEN + 1];
5149566063dSJacob Faibussowitsch       PetscCall(KSPCreate(((PetscObject)(osm->ois[i]))->comm, &ksp));
5159566063dSJacob Faibussowitsch       PetscCall(KSPSetErrorIfNotConverged(ksp, pc->erroriffailure));
5169566063dSJacob Faibussowitsch       PetscCall(PetscObjectIncrementTabLevel((PetscObject)ksp, (PetscObject)pc, 1));
5179566063dSJacob Faibussowitsch       PetscCall(KSPSetType(ksp, KSPPREONLY));
5189566063dSJacob Faibussowitsch       PetscCall(KSPGetPC(ksp, &subpc)); /* Why do we need this here? */
5198f3b4b4dSDmitry Karpeev       if (subdomain_dm) {
5209566063dSJacob Faibussowitsch         PetscCall(KSPSetDM(ksp, subdomain_dm[i]));
5219566063dSJacob Faibussowitsch         PetscCall(DMDestroy(subdomain_dm + i));
5228f3b4b4dSDmitry Karpeev       }
5239566063dSJacob Faibussowitsch       PetscCall(PCGetOptionsPrefix(pc, &prefix));
5249566063dSJacob Faibussowitsch       PetscCall(KSPSetOptionsPrefix(ksp, prefix));
5258f3b4b4dSDmitry Karpeev       if (subdomain_names && subdomain_names[i]) {
5269566063dSJacob Faibussowitsch         PetscCall(PetscSNPrintf(subprefix, PETSC_MAX_PATH_LEN, "sub_%s_", subdomain_names[i]));
5279566063dSJacob Faibussowitsch         PetscCall(KSPAppendOptionsPrefix(ksp, subprefix));
5289566063dSJacob Faibussowitsch         PetscCall(PetscFree(subdomain_names[i]));
5298f3b4b4dSDmitry Karpeev       }
5309566063dSJacob Faibussowitsch       PetscCall(KSPAppendOptionsPrefix(ksp, "sub_"));
531b1a0a8a3SJed Brown       osm->ksp[i] = ksp;
532b1a0a8a3SJed Brown     }
5339566063dSJacob Faibussowitsch     PetscCall(PetscFree(subdomain_dm));
5349566063dSJacob Faibussowitsch     PetscCall(PetscFree(subdomain_names));
535b1a0a8a3SJed Brown     scall = MAT_INITIAL_MATRIX;
5368f3b4b4dSDmitry Karpeev   } else { /* if (pc->setupcalled) */
537b1a0a8a3SJed Brown     /*
5388f3b4b4dSDmitry Karpeev        Destroy the submatrices from the previous iteration
539b1a0a8a3SJed Brown     */
540b1a0a8a3SJed Brown     if (pc->flag == DIFFERENT_NONZERO_PATTERN) {
5419566063dSJacob Faibussowitsch       PetscCall(MatDestroyMatrices(osm->n, &osm->pmat));
542b1a0a8a3SJed Brown       scall = MAT_INITIAL_MATRIX;
543b1a0a8a3SJed Brown     }
544ea91fabdSFande Kong     if (osm->permutationIS) {
5459566063dSJacob Faibussowitsch       PetscCall(MatCreateSubMatrix(pc->pmat, osm->permutationIS, osm->permutationIS, scall, &osm->permutationP));
5469566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject)osm->permutationP));
547ea91fabdSFande Kong       osm->pcmat = pc->pmat;
548ea91fabdSFande Kong       pc->pmat   = osm->permutationP;
549b1a0a8a3SJed Brown     }
550ea91fabdSFande Kong   }
551ea91fabdSFande Kong 
552b1a0a8a3SJed Brown   /*
5532da392ccSBarry Smith      Extract the submatrices.
554b1a0a8a3SJed Brown   */
55581a5c4aaSDmitry Karpeev   if (size > 1) {
5569566063dSJacob Faibussowitsch     PetscCall(MatCreateSubMatricesMPI(pc->pmat, osm->n, osm->ois, osm->ois, scall, &osm->pmat));
5572fa5cd67SKarl Rupp   } else {
5589566063dSJacob Faibussowitsch     PetscCall(MatCreateSubMatrices(pc->pmat, osm->n, osm->ois, osm->ois, scall, &osm->pmat));
55981a5c4aaSDmitry Karpeev   }
560b1a0a8a3SJed Brown   if (scall == MAT_INITIAL_MATRIX) {
5619566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetOptionsPrefix((PetscObject)pc->pmat, &pprefix));
5624dfa11a4SJacob Faibussowitsch     for (i = 0; i < osm->n; i++) { PetscCall(PetscObjectSetOptionsPrefix((PetscObject)osm->pmat[i], pprefix)); }
563b1a0a8a3SJed Brown   }
564b1a0a8a3SJed Brown 
565b1a0a8a3SJed Brown   /* Return control to the user so that the submatrices can be modified (e.g., to apply
566b1a0a8a3SJed Brown      different boundary conditions for the submatrices than for the global problem) */
5679566063dSJacob Faibussowitsch   PetscCall(PCModifySubMatrices(pc, osm->n, osm->ois, osm->ois, osm->pmat, pc->modifysubmatricesP));
568b1a0a8a3SJed Brown 
569b1a0a8a3SJed Brown   /*
5706a4f0f73SDmitry Karpeev      Loop over submatrices putting them into local ksps
571b1a0a8a3SJed Brown   */
572f746d493SDmitry Karpeev   for (i = 0; i < osm->n; i++) {
5739566063dSJacob Faibussowitsch     PetscCall(KSPSetOperators(osm->ksp[i], osm->pmat[i], osm->pmat[i]));
5749566063dSJacob Faibussowitsch     PetscCall(KSPGetOptionsPrefix(osm->ksp[i], &prefix));
5759566063dSJacob Faibussowitsch     PetscCall(MatSetOptionsPrefix(osm->pmat[i], prefix));
57648a46eb9SPierre Jolivet     if (!pc->setupcalled) PetscCall(KSPSetFromOptions(osm->ksp[i]));
577b1a0a8a3SJed Brown   }
578ea91fabdSFande Kong   if (osm->pcmat) {
5799566063dSJacob Faibussowitsch     PetscCall(MatDestroy(&pc->pmat));
580ea91fabdSFande Kong     pc->pmat   = osm->pcmat;
5810a545947SLisandro Dalcin     osm->pcmat = NULL;
582ea91fabdSFande Kong   }
583b1a0a8a3SJed Brown   PetscFunctionReturn(0);
584b1a0a8a3SJed Brown }
585b1a0a8a3SJed Brown 
586*d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCSetUpOnBlocks_GASM(PC pc)
587*d71ae5a4SJacob Faibussowitsch {
588f746d493SDmitry Karpeev   PC_GASM *osm = (PC_GASM *)pc->data;
589b1a0a8a3SJed Brown   PetscInt i;
590b1a0a8a3SJed Brown 
591b1a0a8a3SJed Brown   PetscFunctionBegin;
59248a46eb9SPierre Jolivet   for (i = 0; i < osm->n; i++) PetscCall(KSPSetUp(osm->ksp[i]));
593b1a0a8a3SJed Brown   PetscFunctionReturn(0);
594b1a0a8a3SJed Brown }
595b1a0a8a3SJed Brown 
596*d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCApply_GASM(PC pc, Vec xin, Vec yout)
597*d71ae5a4SJacob Faibussowitsch {
598f746d493SDmitry Karpeev   PC_GASM    *osm = (PC_GASM *)pc->data;
599f746d493SDmitry Karpeev   PetscInt    i;
600ea91fabdSFande Kong   Vec         x, y;
601b1a0a8a3SJed Brown   ScatterMode forward = SCATTER_FORWARD, reverse = SCATTER_REVERSE;
602b1a0a8a3SJed Brown 
603b1a0a8a3SJed Brown   PetscFunctionBegin;
604ea91fabdSFande Kong   if (osm->pctoouter) {
6059566063dSJacob Faibussowitsch     PetscCall(VecScatterBegin(osm->pctoouter, xin, osm->pcx, INSERT_VALUES, SCATTER_REVERSE));
6069566063dSJacob Faibussowitsch     PetscCall(VecScatterEnd(osm->pctoouter, xin, osm->pcx, INSERT_VALUES, SCATTER_REVERSE));
607ea91fabdSFande Kong     x = osm->pcx;
608ea91fabdSFande Kong     y = osm->pcy;
609ea91fabdSFande Kong   } else {
610ea91fabdSFande Kong     x = xin;
611ea91fabdSFande Kong     y = yout;
612ea91fabdSFande Kong   }
613b1a0a8a3SJed Brown   /*
61448e38a8aSPierre Jolivet      support for limiting the restriction or interpolation only to the inner
615b1a0a8a3SJed Brown      subdomain values (leaving the other values 0).
616b1a0a8a3SJed Brown   */
617f746d493SDmitry Karpeev   if (!(osm->type & PC_GASM_RESTRICT)) {
618b1a0a8a3SJed Brown     /* have to zero the work RHS since scatter may leave some slots empty */
6199566063dSJacob Faibussowitsch     PetscCall(VecZeroEntries(osm->gx));
6209566063dSJacob Faibussowitsch     PetscCall(VecScatterBegin(osm->girestriction, x, osm->gx, INSERT_VALUES, forward));
6212fa5cd67SKarl Rupp   } else {
6229566063dSJacob Faibussowitsch     PetscCall(VecScatterBegin(osm->gorestriction, x, osm->gx, INSERT_VALUES, forward));
623b1a0a8a3SJed Brown   }
6249566063dSJacob Faibussowitsch   PetscCall(VecZeroEntries(osm->gy));
6256a4f0f73SDmitry Karpeev   if (!(osm->type & PC_GASM_RESTRICT)) {
6269566063dSJacob Faibussowitsch     PetscCall(VecScatterEnd(osm->girestriction, x, osm->gx, INSERT_VALUES, forward));
6272fa5cd67SKarl Rupp   } else {
6289566063dSJacob Faibussowitsch     PetscCall(VecScatterEnd(osm->gorestriction, x, osm->gx, INSERT_VALUES, forward));
6296a4f0f73SDmitry Karpeev   }
63086b96d36SDmitry Karpeev   /* do the subdomain solves */
63186b96d36SDmitry Karpeev   for (i = 0; i < osm->n; ++i) {
6329566063dSJacob Faibussowitsch     PetscCall(KSPSolve(osm->ksp[i], osm->x[i], osm->y[i]));
6339566063dSJacob Faibussowitsch     PetscCall(KSPCheckSolve(osm->ksp[i], pc, osm->y[i]));
634b1a0a8a3SJed Brown   }
63548e38a8aSPierre Jolivet   /* do we need to zero y? */
6369566063dSJacob Faibussowitsch   PetscCall(VecZeroEntries(y));
6376a4f0f73SDmitry Karpeev   if (!(osm->type & PC_GASM_INTERPOLATE)) {
6389566063dSJacob Faibussowitsch     PetscCall(VecScatterBegin(osm->girestriction, osm->gy, y, ADD_VALUES, reverse));
6399566063dSJacob Faibussowitsch     PetscCall(VecScatterEnd(osm->girestriction, osm->gy, y, ADD_VALUES, reverse));
6402fa5cd67SKarl Rupp   } else {
6419566063dSJacob Faibussowitsch     PetscCall(VecScatterBegin(osm->gorestriction, osm->gy, y, ADD_VALUES, reverse));
6429566063dSJacob Faibussowitsch     PetscCall(VecScatterEnd(osm->gorestriction, osm->gy, y, ADD_VALUES, reverse));
6436a4f0f73SDmitry Karpeev   }
644ea91fabdSFande Kong   if (osm->pctoouter) {
6459566063dSJacob Faibussowitsch     PetscCall(VecScatterBegin(osm->pctoouter, y, yout, INSERT_VALUES, SCATTER_FORWARD));
6469566063dSJacob Faibussowitsch     PetscCall(VecScatterEnd(osm->pctoouter, y, yout, INSERT_VALUES, SCATTER_FORWARD));
647ea91fabdSFande Kong   }
648ea91fabdSFande Kong   PetscFunctionReturn(0);
649b1a0a8a3SJed Brown }
650b1a0a8a3SJed Brown 
651*d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCMatApply_GASM(PC pc, Mat Xin, Mat Yout)
652*d71ae5a4SJacob Faibussowitsch {
65348e38a8aSPierre Jolivet   PC_GASM    *osm = (PC_GASM *)pc->data;
65448e38a8aSPierre Jolivet   Mat         X, Y, O = NULL, Z, W;
65548e38a8aSPierre Jolivet   Vec         x, y;
65648e38a8aSPierre Jolivet   PetscInt    i, m, M, N;
65748e38a8aSPierre Jolivet   ScatterMode forward = SCATTER_FORWARD, reverse = SCATTER_REVERSE;
65848e38a8aSPierre Jolivet 
65948e38a8aSPierre Jolivet   PetscFunctionBegin;
66008401ef6SPierre Jolivet   PetscCheck(osm->n == 1, PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "Not yet implemented");
6619566063dSJacob Faibussowitsch   PetscCall(MatGetSize(Xin, NULL, &N));
66248e38a8aSPierre Jolivet   if (osm->pctoouter) {
6639566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(osm->pcx, &m));
6649566063dSJacob Faibussowitsch     PetscCall(VecGetSize(osm->pcx, &M));
6659566063dSJacob Faibussowitsch     PetscCall(MatCreateDense(PetscObjectComm((PetscObject)osm->ois[0]), m, PETSC_DECIDE, M, N, NULL, &O));
66648e38a8aSPierre Jolivet     for (i = 0; i < N; ++i) {
6679566063dSJacob Faibussowitsch       PetscCall(MatDenseGetColumnVecRead(Xin, i, &x));
6689566063dSJacob Faibussowitsch       PetscCall(MatDenseGetColumnVecWrite(O, i, &y));
6699566063dSJacob Faibussowitsch       PetscCall(VecScatterBegin(osm->pctoouter, x, y, INSERT_VALUES, SCATTER_REVERSE));
6709566063dSJacob Faibussowitsch       PetscCall(VecScatterEnd(osm->pctoouter, x, y, INSERT_VALUES, SCATTER_REVERSE));
6719566063dSJacob Faibussowitsch       PetscCall(MatDenseRestoreColumnVecWrite(O, i, &y));
6729566063dSJacob Faibussowitsch       PetscCall(MatDenseRestoreColumnVecRead(Xin, i, &x));
67348e38a8aSPierre Jolivet     }
67448e38a8aSPierre Jolivet     X = Y = O;
67548e38a8aSPierre Jolivet   } else {
67648e38a8aSPierre Jolivet     X = Xin;
67748e38a8aSPierre Jolivet     Y = Yout;
67848e38a8aSPierre Jolivet   }
67948e38a8aSPierre Jolivet   /*
68048e38a8aSPierre Jolivet      support for limiting the restriction or interpolation only to the inner
68148e38a8aSPierre Jolivet      subdomain values (leaving the other values 0).
68248e38a8aSPierre Jolivet   */
6839566063dSJacob Faibussowitsch   PetscCall(VecGetLocalSize(osm->x[0], &m));
6849566063dSJacob Faibussowitsch   PetscCall(VecGetSize(osm->x[0], &M));
6859566063dSJacob Faibussowitsch   PetscCall(MatCreateDense(PetscObjectComm((PetscObject)osm->ois[0]), m, PETSC_DECIDE, M, N, NULL, &Z));
68648e38a8aSPierre Jolivet   for (i = 0; i < N; ++i) {
6879566063dSJacob Faibussowitsch     PetscCall(MatDenseGetColumnVecRead(X, i, &x));
6889566063dSJacob Faibussowitsch     PetscCall(MatDenseGetColumnVecWrite(Z, i, &y));
68948e38a8aSPierre Jolivet     if (!(osm->type & PC_GASM_RESTRICT)) {
69048e38a8aSPierre Jolivet       /* have to zero the work RHS since scatter may leave some slots empty */
6919566063dSJacob Faibussowitsch       PetscCall(VecZeroEntries(y));
6929566063dSJacob Faibussowitsch       PetscCall(VecScatterBegin(osm->girestriction, x, y, INSERT_VALUES, forward));
6939566063dSJacob Faibussowitsch       PetscCall(VecScatterEnd(osm->girestriction, x, y, INSERT_VALUES, forward));
69448e38a8aSPierre Jolivet     } else {
6959566063dSJacob Faibussowitsch       PetscCall(VecScatterBegin(osm->gorestriction, x, y, INSERT_VALUES, forward));
6969566063dSJacob Faibussowitsch       PetscCall(VecScatterEnd(osm->gorestriction, x, y, INSERT_VALUES, forward));
69748e38a8aSPierre Jolivet     }
6989566063dSJacob Faibussowitsch     PetscCall(MatDenseRestoreColumnVecWrite(Z, i, &y));
6999566063dSJacob Faibussowitsch     PetscCall(MatDenseRestoreColumnVecRead(X, i, &x));
70048e38a8aSPierre Jolivet   }
7019566063dSJacob Faibussowitsch   PetscCall(MatCreateDense(PetscObjectComm((PetscObject)osm->ois[0]), m, PETSC_DECIDE, M, N, NULL, &W));
7029566063dSJacob Faibussowitsch   PetscCall(MatSetOption(Z, MAT_NO_OFF_PROC_ENTRIES, PETSC_TRUE));
7039566063dSJacob Faibussowitsch   PetscCall(MatAssemblyBegin(Z, MAT_FINAL_ASSEMBLY));
7049566063dSJacob Faibussowitsch   PetscCall(MatAssemblyEnd(Z, MAT_FINAL_ASSEMBLY));
70548e38a8aSPierre Jolivet   /* do the subdomain solve */
7069566063dSJacob Faibussowitsch   PetscCall(KSPMatSolve(osm->ksp[0], Z, W));
7079566063dSJacob Faibussowitsch   PetscCall(KSPCheckSolve(osm->ksp[0], pc, NULL));
7089566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&Z));
70948e38a8aSPierre Jolivet   /* do we need to zero y? */
7109566063dSJacob Faibussowitsch   PetscCall(MatZeroEntries(Y));
71148e38a8aSPierre Jolivet   for (i = 0; i < N; ++i) {
7129566063dSJacob Faibussowitsch     PetscCall(MatDenseGetColumnVecWrite(Y, i, &y));
7139566063dSJacob Faibussowitsch     PetscCall(MatDenseGetColumnVecRead(W, i, &x));
71448e38a8aSPierre Jolivet     if (!(osm->type & PC_GASM_INTERPOLATE)) {
7159566063dSJacob Faibussowitsch       PetscCall(VecScatterBegin(osm->girestriction, x, y, ADD_VALUES, reverse));
7169566063dSJacob Faibussowitsch       PetscCall(VecScatterEnd(osm->girestriction, x, y, ADD_VALUES, reverse));
71748e38a8aSPierre Jolivet     } else {
7189566063dSJacob Faibussowitsch       PetscCall(VecScatterBegin(osm->gorestriction, x, y, ADD_VALUES, reverse));
7199566063dSJacob Faibussowitsch       PetscCall(VecScatterEnd(osm->gorestriction, x, y, ADD_VALUES, reverse));
72048e38a8aSPierre Jolivet     }
7219566063dSJacob Faibussowitsch     PetscCall(MatDenseRestoreColumnVecRead(W, i, &x));
72248e38a8aSPierre Jolivet     if (osm->pctoouter) {
7239566063dSJacob Faibussowitsch       PetscCall(MatDenseGetColumnVecWrite(Yout, i, &x));
7249566063dSJacob Faibussowitsch       PetscCall(VecScatterBegin(osm->pctoouter, y, x, INSERT_VALUES, SCATTER_FORWARD));
7259566063dSJacob Faibussowitsch       PetscCall(VecScatterEnd(osm->pctoouter, y, x, INSERT_VALUES, SCATTER_FORWARD));
7269566063dSJacob Faibussowitsch       PetscCall(MatDenseRestoreColumnVecRead(Yout, i, &x));
72748e38a8aSPierre Jolivet     }
7289566063dSJacob Faibussowitsch     PetscCall(MatDenseRestoreColumnVecWrite(Y, i, &y));
72948e38a8aSPierre Jolivet   }
7309566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&W));
7319566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&O));
73248e38a8aSPierre Jolivet   PetscFunctionReturn(0);
73348e38a8aSPierre Jolivet }
73448e38a8aSPierre Jolivet 
735*d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCApplyTranspose_GASM(PC pc, Vec xin, Vec yout)
736*d71ae5a4SJacob Faibussowitsch {
737f746d493SDmitry Karpeev   PC_GASM    *osm = (PC_GASM *)pc->data;
738f746d493SDmitry Karpeev   PetscInt    i;
739ea91fabdSFande Kong   Vec         x, y;
740b1a0a8a3SJed Brown   ScatterMode forward = SCATTER_FORWARD, reverse = SCATTER_REVERSE;
741b1a0a8a3SJed Brown 
742b1a0a8a3SJed Brown   PetscFunctionBegin;
743ea91fabdSFande Kong   if (osm->pctoouter) {
7449566063dSJacob Faibussowitsch     PetscCall(VecScatterBegin(osm->pctoouter, xin, osm->pcx, INSERT_VALUES, SCATTER_REVERSE));
7459566063dSJacob Faibussowitsch     PetscCall(VecScatterEnd(osm->pctoouter, xin, osm->pcx, INSERT_VALUES, SCATTER_REVERSE));
746ea91fabdSFande Kong     x = osm->pcx;
747ea91fabdSFande Kong     y = osm->pcy;
748ea91fabdSFande Kong   } else {
749ea91fabdSFande Kong     x = xin;
750ea91fabdSFande Kong     y = yout;
751ea91fabdSFande Kong   }
752b1a0a8a3SJed Brown   /*
753b1a0a8a3SJed Brown      Support for limiting the restriction or interpolation to only local
754b1a0a8a3SJed Brown      subdomain values (leaving the other values 0).
755b1a0a8a3SJed Brown 
756f746d493SDmitry Karpeev      Note: these are reversed from the PCApply_GASM() because we are applying the
757b1a0a8a3SJed Brown      transpose of the three terms
758b1a0a8a3SJed Brown   */
759f746d493SDmitry Karpeev   if (!(osm->type & PC_GASM_INTERPOLATE)) {
760b1a0a8a3SJed Brown     /* have to zero the work RHS since scatter may leave some slots empty */
7619566063dSJacob Faibussowitsch     PetscCall(VecZeroEntries(osm->gx));
7629566063dSJacob Faibussowitsch     PetscCall(VecScatterBegin(osm->girestriction, x, osm->gx, INSERT_VALUES, forward));
7632fa5cd67SKarl Rupp   } else {
7649566063dSJacob Faibussowitsch     PetscCall(VecScatterBegin(osm->gorestriction, x, osm->gx, INSERT_VALUES, forward));
765b1a0a8a3SJed Brown   }
7669566063dSJacob Faibussowitsch   PetscCall(VecZeroEntries(osm->gy));
7676a4f0f73SDmitry Karpeev   if (!(osm->type & PC_GASM_INTERPOLATE)) {
7689566063dSJacob Faibussowitsch     PetscCall(VecScatterEnd(osm->girestriction, x, osm->gx, INSERT_VALUES, forward));
7692fa5cd67SKarl Rupp   } else {
7709566063dSJacob Faibussowitsch     PetscCall(VecScatterEnd(osm->gorestriction, x, osm->gx, INSERT_VALUES, forward));
7716a4f0f73SDmitry Karpeev   }
772b1a0a8a3SJed Brown   /* do the local solves */
773ab3e923fSDmitry Karpeev   for (i = 0; i < osm->n; ++i) { /* Note that the solves are local, so we can go to osm->n, rather than osm->nmax. */
7749566063dSJacob Faibussowitsch     PetscCall(KSPSolveTranspose(osm->ksp[i], osm->x[i], osm->y[i]));
7759566063dSJacob Faibussowitsch     PetscCall(KSPCheckSolve(osm->ksp[i], pc, osm->y[i]));
776b1a0a8a3SJed Brown   }
7779566063dSJacob Faibussowitsch   PetscCall(VecZeroEntries(y));
7786a4f0f73SDmitry Karpeev   if (!(osm->type & PC_GASM_RESTRICT)) {
7799566063dSJacob Faibussowitsch     PetscCall(VecScatterBegin(osm->girestriction, osm->gy, y, ADD_VALUES, reverse));
7809566063dSJacob Faibussowitsch     PetscCall(VecScatterEnd(osm->girestriction, osm->gy, y, ADD_VALUES, reverse));
7812fa5cd67SKarl Rupp   } else {
7829566063dSJacob Faibussowitsch     PetscCall(VecScatterBegin(osm->gorestriction, osm->gy, y, ADD_VALUES, reverse));
7839566063dSJacob Faibussowitsch     PetscCall(VecScatterEnd(osm->gorestriction, osm->gy, y, ADD_VALUES, reverse));
7846a4f0f73SDmitry Karpeev   }
785ea91fabdSFande Kong   if (osm->pctoouter) {
7869566063dSJacob Faibussowitsch     PetscCall(VecScatterBegin(osm->pctoouter, y, yout, INSERT_VALUES, SCATTER_FORWARD));
7879566063dSJacob Faibussowitsch     PetscCall(VecScatterEnd(osm->pctoouter, y, yout, INSERT_VALUES, SCATTER_FORWARD));
788ea91fabdSFande Kong   }
789b1a0a8a3SJed Brown   PetscFunctionReturn(0);
790b1a0a8a3SJed Brown }
791b1a0a8a3SJed Brown 
792*d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCReset_GASM(PC pc)
793*d71ae5a4SJacob Faibussowitsch {
794f746d493SDmitry Karpeev   PC_GASM *osm = (PC_GASM *)pc->data;
795b1a0a8a3SJed Brown   PetscInt i;
796b1a0a8a3SJed Brown 
797b1a0a8a3SJed Brown   PetscFunctionBegin;
798b1a0a8a3SJed Brown   if (osm->ksp) {
79948a46eb9SPierre Jolivet     for (i = 0; i < osm->n; i++) PetscCall(KSPReset(osm->ksp[i]));
800b1a0a8a3SJed Brown   }
801b1a0a8a3SJed Brown   if (osm->pmat) {
802f746d493SDmitry Karpeev     if (osm->n > 0) {
803df750dc8SHong Zhang       PetscMPIInt size;
8049566063dSJacob Faibussowitsch       PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size));
805df750dc8SHong Zhang       if (size > 1) {
8067dae84e0SHong Zhang         /* osm->pmat is created by MatCreateSubMatricesMPI(), cannot use MatDestroySubMatrices() */
8079566063dSJacob Faibussowitsch         PetscCall(MatDestroyMatrices(osm->n, &osm->pmat));
808df750dc8SHong Zhang       } else {
8099566063dSJacob Faibussowitsch         PetscCall(MatDestroySubMatrices(osm->n, &osm->pmat));
810df750dc8SHong Zhang       }
811b1a0a8a3SJed Brown     }
812b1a0a8a3SJed Brown   }
813d34fcf5fSBarry Smith   if (osm->x) {
814f746d493SDmitry Karpeev     for (i = 0; i < osm->n; i++) {
8159566063dSJacob Faibussowitsch       PetscCall(VecDestroy(&osm->x[i]));
8169566063dSJacob Faibussowitsch       PetscCall(VecDestroy(&osm->y[i]));
817b1a0a8a3SJed Brown     }
818d34fcf5fSBarry Smith   }
8199566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&osm->gx));
8209566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&osm->gy));
821ab3e923fSDmitry Karpeev 
8229566063dSJacob Faibussowitsch   PetscCall(VecScatterDestroy(&osm->gorestriction));
8239566063dSJacob Faibussowitsch   PetscCall(VecScatterDestroy(&osm->girestriction));
8248f3b4b4dSDmitry Karpeev   if (!osm->user_subdomains) {
8259566063dSJacob Faibussowitsch     PetscCall(PCGASMDestroySubdomains(osm->n, &osm->ois, &osm->iis));
8268f3b4b4dSDmitry Karpeev     osm->N    = PETSC_DETERMINE;
8278f3b4b4dSDmitry Karpeev     osm->nmax = PETSC_DETERMINE;
8288f3b4b4dSDmitry Karpeev   }
82948a46eb9SPierre Jolivet   if (osm->pctoouter) PetscCall(VecScatterDestroy(&(osm->pctoouter)));
83048a46eb9SPierre Jolivet   if (osm->permutationIS) PetscCall(ISDestroy(&(osm->permutationIS)));
83148a46eb9SPierre Jolivet   if (osm->pcx) PetscCall(VecDestroy(&(osm->pcx)));
83248a46eb9SPierre Jolivet   if (osm->pcy) PetscCall(VecDestroy(&(osm->pcy)));
83348a46eb9SPierre Jolivet   if (osm->permutationP) PetscCall(MatDestroy(&(osm->permutationP)));
83448a46eb9SPierre Jolivet   if (osm->pcmat) PetscCall(MatDestroy(&osm->pcmat));
835a06653b4SBarry Smith   PetscFunctionReturn(0);
836a06653b4SBarry Smith }
837a06653b4SBarry Smith 
838*d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCDestroy_GASM(PC pc)
839*d71ae5a4SJacob Faibussowitsch {
840a06653b4SBarry Smith   PC_GASM *osm = (PC_GASM *)pc->data;
841a06653b4SBarry Smith   PetscInt i;
842a06653b4SBarry Smith 
843a06653b4SBarry Smith   PetscFunctionBegin;
8449566063dSJacob Faibussowitsch   PetscCall(PCReset_GASM(pc));
845135757f6SDmitry Karpeev   /* PCReset will not destroy subdomains, if user_subdomains is true. */
8469566063dSJacob Faibussowitsch   PetscCall(PCGASMDestroySubdomains(osm->n, &osm->ois, &osm->iis));
847a06653b4SBarry Smith   if (osm->ksp) {
84848a46eb9SPierre Jolivet     for (i = 0; i < osm->n; i++) PetscCall(KSPDestroy(&osm->ksp[i]));
8499566063dSJacob Faibussowitsch     PetscCall(PetscFree(osm->ksp));
850a06653b4SBarry Smith   }
8519566063dSJacob Faibussowitsch   PetscCall(PetscFree(osm->x));
8529566063dSJacob Faibussowitsch   PetscCall(PetscFree(osm->y));
8532e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetSubdomains_C", NULL));
8542e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetOverlap_C", NULL));
8552e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetType_C", NULL));
8562e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetSortIndices_C", NULL));
8572e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMGetSubKSP_C", NULL));
8589566063dSJacob Faibussowitsch   PetscCall(PetscFree(pc->data));
859b1a0a8a3SJed Brown   PetscFunctionReturn(0);
860b1a0a8a3SJed Brown }
861b1a0a8a3SJed Brown 
862*d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCSetFromOptions_GASM(PC pc, PetscOptionItems *PetscOptionsObject)
863*d71ae5a4SJacob Faibussowitsch {
864f746d493SDmitry Karpeev   PC_GASM   *osm = (PC_GASM *)pc->data;
865b1a0a8a3SJed Brown   PetscInt   blocks, ovl;
86657501b6eSBarry Smith   PetscBool  flg;
867f746d493SDmitry Karpeev   PCGASMType gasmtype;
868b1a0a8a3SJed Brown 
869b1a0a8a3SJed Brown   PetscFunctionBegin;
870d0609cedSBarry Smith   PetscOptionsHeadBegin(PetscOptionsObject, "Generalized additive Schwarz options");
8719566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-pc_gasm_use_dm_subdomains", "If subdomains aren't set, use DMCreateDomainDecomposition() to define subdomains.", "PCGASMSetUseDMSubdomains", osm->dm_subdomains, &osm->dm_subdomains, &flg));
8729566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt("-pc_gasm_total_subdomains", "Total number of subdomains across communicator", "PCGASMSetTotalSubdomains", osm->N, &blocks, &flg));
8731baa6e33SBarry Smith   if (flg) PetscCall(PCGASMSetTotalSubdomains(pc, blocks));
8749566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt("-pc_gasm_overlap", "Number of overlapping degrees of freedom", "PCGASMSetOverlap", osm->overlap, &ovl, &flg));
87565db9045SDmitry Karpeev   if (flg) {
8769566063dSJacob Faibussowitsch     PetscCall(PCGASMSetOverlap(pc, ovl));
877d709ea83SDmitry Karpeev     osm->dm_subdomains = PETSC_FALSE;
87865db9045SDmitry Karpeev   }
879b1a0a8a3SJed Brown   flg = PETSC_FALSE;
8809566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-pc_gasm_type", "Type of restriction/extension", "PCGASMSetType", PCGASMTypes, (PetscEnum)osm->type, (PetscEnum *)&gasmtype, &flg));
8819566063dSJacob Faibussowitsch   if (flg) PetscCall(PCGASMSetType(pc, gasmtype));
8829566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-pc_gasm_use_hierachical_partitioning", "use hierarchical partitioning", NULL, osm->hierarchicalpartitioning, &osm->hierarchicalpartitioning, &flg));
883d0609cedSBarry Smith   PetscOptionsHeadEnd();
884b1a0a8a3SJed Brown   PetscFunctionReturn(0);
885b1a0a8a3SJed Brown }
886b1a0a8a3SJed Brown 
887b1a0a8a3SJed Brown /*------------------------------------------------------------------------------------*/
888b1a0a8a3SJed Brown 
8898f3b4b4dSDmitry Karpeev /*@
890f1580f4eSBarry Smith     PCGASMSetTotalSubdomains - sets the total number of subdomains to use across the communicator for `PCGASM`
8918f3b4b4dSDmitry Karpeev     Logically collective on pc
8928f3b4b4dSDmitry Karpeev 
8938f3b4b4dSDmitry Karpeev     Input Parameters:
8948f3b4b4dSDmitry Karpeev +   pc  - the preconditioner
8958f3b4b4dSDmitry Karpeev -   N   - total number of subdomains
8968f3b4b4dSDmitry Karpeev 
8978f3b4b4dSDmitry Karpeev     Level: beginner
8988f3b4b4dSDmitry Karpeev 
899f1580f4eSBarry Smith .seealso: `PCGASM`, `PCGASMSetSubdomains()`, `PCGASMSetOverlap()`
900db781477SPatrick Sanan           `PCGASMCreateSubdomains2D()`
9018f3b4b4dSDmitry Karpeev @*/
902*d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGASMSetTotalSubdomains(PC pc, PetscInt N)
903*d71ae5a4SJacob Faibussowitsch {
9048f3b4b4dSDmitry Karpeev   PC_GASM    *osm = (PC_GASM *)pc->data;
9058f3b4b4dSDmitry Karpeev   PetscMPIInt size, rank;
9068f3b4b4dSDmitry Karpeev 
9078f3b4b4dSDmitry Karpeev   PetscFunctionBegin;
90863a3b9bcSJacob Faibussowitsch   PetscCheck(N >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Total number of subdomains must be 1 or more, got N = %" PetscInt_FMT, N);
90928b400f6SJacob Faibussowitsch   PetscCheck(!pc->setupcalled, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "PCGASMSetTotalSubdomains() should be called before calling PCSetUp().");
9108f3b4b4dSDmitry Karpeev 
9119566063dSJacob Faibussowitsch   PetscCall(PCGASMDestroySubdomains(osm->n, &osm->iis, &osm->ois));
9128f3b4b4dSDmitry Karpeev   osm->ois = osm->iis = NULL;
9138f3b4b4dSDmitry Karpeev 
9149566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size));
9159566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)pc), &rank));
9168f3b4b4dSDmitry Karpeev   osm->N             = N;
9178f3b4b4dSDmitry Karpeev   osm->n             = PETSC_DETERMINE;
9188f3b4b4dSDmitry Karpeev   osm->nmax          = PETSC_DETERMINE;
9198f3b4b4dSDmitry Karpeev   osm->dm_subdomains = PETSC_FALSE;
9208f3b4b4dSDmitry Karpeev   PetscFunctionReturn(0);
9218f3b4b4dSDmitry Karpeev }
9228f3b4b4dSDmitry Karpeev 
923*d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGASMSetSubdomains_GASM(PC pc, PetscInt n, IS iis[], IS ois[])
924*d71ae5a4SJacob Faibussowitsch {
925f746d493SDmitry Karpeev   PC_GASM *osm = (PC_GASM *)pc->data;
926b1a0a8a3SJed Brown   PetscInt i;
927b1a0a8a3SJed Brown 
928b1a0a8a3SJed Brown   PetscFunctionBegin;
929f1580f4eSBarry Smith   PetscCheck(n >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Each MPI rank must have 1 or more subdomains, got n = %" PetscInt_FMT, n);
93028b400f6SJacob Faibussowitsch   PetscCheck(!pc->setupcalled, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "PCGASMSetSubdomains() should be called before calling PCSetUp().");
931b1a0a8a3SJed Brown 
9329566063dSJacob Faibussowitsch   PetscCall(PCGASMDestroySubdomains(osm->n, &osm->iis, &osm->ois));
9338f3b4b4dSDmitry Karpeev   osm->iis = osm->ois = NULL;
9348f3b4b4dSDmitry Karpeev   osm->n              = n;
9358f3b4b4dSDmitry Karpeev   osm->N              = PETSC_DETERMINE;
9368f3b4b4dSDmitry Karpeev   osm->nmax           = PETSC_DETERMINE;
937a35b7b57SDmitry Karpeev   if (ois) {
9389566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(n, &osm->ois));
9398f3b4b4dSDmitry Karpeev     for (i = 0; i < n; i++) {
9409566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject)ois[i]));
9418f3b4b4dSDmitry Karpeev       osm->ois[i] = ois[i];
9428f3b4b4dSDmitry Karpeev     }
9438f3b4b4dSDmitry Karpeev     /*
9448f3b4b4dSDmitry Karpeev        Since the user set the outer subdomains, even if nontrivial overlap was requested via PCGASMSetOverlap(),
9458f3b4b4dSDmitry Karpeev        it will be ignored.  To avoid confusion later on (e.g., when viewing the PC), the overlap size is set to -1.
9468f3b4b4dSDmitry Karpeev     */
947b1a0a8a3SJed Brown     osm->overlap = -1;
948670417b2SFande Kong     /* inner subdomains must be provided  */
94928b400f6SJacob Faibussowitsch     PetscCheck(iis, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "inner indices have to be provided ");
950670417b2SFande Kong   } /* end if */
951a35b7b57SDmitry Karpeev   if (iis) {
9529566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(n, &osm->iis));
9538f3b4b4dSDmitry Karpeev     for (i = 0; i < n; i++) {
9549566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject)iis[i]));
9558f3b4b4dSDmitry Karpeev       osm->iis[i] = iis[i];
9568f3b4b4dSDmitry Karpeev     }
957a35b7b57SDmitry Karpeev     if (!ois) {
958390e1bf2SBarry Smith       osm->ois = NULL;
959670417b2SFande Kong       /* if user does not provide outer indices, we will create the corresponding outer indices using  osm->overlap =1 in PCSetUp_GASM */
960670417b2SFande Kong     }
961670417b2SFande Kong   }
96276bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
963670417b2SFande Kong     PetscInt        j, rstart, rend, *covered, lsize;
964670417b2SFande Kong     const PetscInt *indices;
965670417b2SFande Kong     /* check if the inner indices cover and only cover the local portion of the preconditioning matrix */
9669566063dSJacob Faibussowitsch     PetscCall(MatGetOwnershipRange(pc->pmat, &rstart, &rend));
9679566063dSJacob Faibussowitsch     PetscCall(PetscCalloc1(rend - rstart, &covered));
968f1580f4eSBarry Smith     /* check if the current MPI rank owns indices from others */
969a35b7b57SDmitry Karpeev     for (i = 0; i < n; i++) {
9709566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(osm->iis[i], &indices));
9719566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(osm->iis[i], &lsize));
972670417b2SFande Kong       for (j = 0; j < lsize; j++) {
973f1580f4eSBarry Smith         PetscCheck(indices[j] >= rstart && indices[j] < rend, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "inner subdomains can not own an index %" PetscInt_FMT " from other ranks", indices[j]);
9742472a847SBarry Smith         PetscCheck(covered[indices[j] - rstart] != 1, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "inner subdomains can not have an overlapping index %" PetscInt_FMT " ", indices[j]);
97563a3b9bcSJacob Faibussowitsch         covered[indices[j] - rstart] = 1;
976a35b7b57SDmitry Karpeev       }
9779566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(osm->iis[i], &indices));
9788f3b4b4dSDmitry Karpeev     }
979670417b2SFande Kong     /* check if we miss any indices */
980ad540459SPierre Jolivet     for (i = rstart; i < rend; i++) PetscCheck(covered[i - rstart], PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "local entity %" PetscInt_FMT " was not covered by inner subdomains", i);
9819566063dSJacob Faibussowitsch     PetscCall(PetscFree(covered));
982a35b7b57SDmitry Karpeev   }
9838f3b4b4dSDmitry Karpeev   if (iis) osm->user_subdomains = PETSC_TRUE;
984b1a0a8a3SJed Brown   PetscFunctionReturn(0);
985b1a0a8a3SJed Brown }
986b1a0a8a3SJed Brown 
987*d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGASMSetOverlap_GASM(PC pc, PetscInt ovl)
988*d71ae5a4SJacob Faibussowitsch {
989f746d493SDmitry Karpeev   PC_GASM *osm = (PC_GASM *)pc->data;
990b1a0a8a3SJed Brown 
991b1a0a8a3SJed Brown   PetscFunctionBegin;
99208401ef6SPierre Jolivet   PetscCheck(ovl >= 0, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "Negative overlap value requested");
9932472a847SBarry Smith   PetscCheck(!pc->setupcalled || ovl == osm->overlap, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "PCGASMSetOverlap() should be called before PCSetUp().");
9942fa5cd67SKarl Rupp   if (!pc->setupcalled) osm->overlap = ovl;
995b1a0a8a3SJed Brown   PetscFunctionReturn(0);
996b1a0a8a3SJed Brown }
997b1a0a8a3SJed Brown 
998*d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGASMSetType_GASM(PC pc, PCGASMType type)
999*d71ae5a4SJacob Faibussowitsch {
1000f746d493SDmitry Karpeev   PC_GASM *osm = (PC_GASM *)pc->data;
1001b1a0a8a3SJed Brown 
1002b1a0a8a3SJed Brown   PetscFunctionBegin;
1003b1a0a8a3SJed Brown   osm->type     = type;
1004b1a0a8a3SJed Brown   osm->type_set = PETSC_TRUE;
1005b1a0a8a3SJed Brown   PetscFunctionReturn(0);
1006b1a0a8a3SJed Brown }
1007b1a0a8a3SJed Brown 
1008*d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGASMSetSortIndices_GASM(PC pc, PetscBool doSort)
1009*d71ae5a4SJacob Faibussowitsch {
1010f746d493SDmitry Karpeev   PC_GASM *osm = (PC_GASM *)pc->data;
1011b1a0a8a3SJed Brown 
1012b1a0a8a3SJed Brown   PetscFunctionBegin;
1013b1a0a8a3SJed Brown   osm->sort_indices = doSort;
1014b1a0a8a3SJed Brown   PetscFunctionReturn(0);
1015b1a0a8a3SJed Brown }
1016b1a0a8a3SJed Brown 
101744fe18e5SDmitry Karpeev /*
10188f3b4b4dSDmitry Karpeev    FIXME: This routine might need to be modified now that multiple ranks per subdomain are allowed.
101944fe18e5SDmitry Karpeev         In particular, it would upset the global subdomain number calculation.
102044fe18e5SDmitry Karpeev */
1021*d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGASMGetSubKSP_GASM(PC pc, PetscInt *n, PetscInt *first, KSP **ksp)
1022*d71ae5a4SJacob Faibussowitsch {
1023f746d493SDmitry Karpeev   PC_GASM *osm = (PC_GASM *)pc->data;
1024b1a0a8a3SJed Brown 
1025b1a0a8a3SJed Brown   PetscFunctionBegin;
102608401ef6SPierre Jolivet   PetscCheck(osm->n >= 1, PetscObjectComm((PetscObject)pc), PETSC_ERR_ORDER, "Need to call PCSetUp() on PC (or KSPSetUp() on the outer KSP object) before calling here");
1027b1a0a8a3SJed Brown 
10282fa5cd67SKarl Rupp   if (n) *n = osm->n;
1029ab3e923fSDmitry Karpeev   if (first) {
10309566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Scan(&osm->n, first, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject)pc)));
1031ab3e923fSDmitry Karpeev     *first -= osm->n;
1032b1a0a8a3SJed Brown   }
1033b1a0a8a3SJed Brown   if (ksp) {
1034b1a0a8a3SJed Brown     /* Assume that local solves are now different; not necessarily
103506b43e7eSDmitry Karpeev        true, though!  This flag is used only for PCView_GASM() */
1036b1a0a8a3SJed Brown     *ksp                        = osm->ksp;
10376a4f0f73SDmitry Karpeev     osm->same_subdomain_solvers = PETSC_FALSE;
1038b1a0a8a3SJed Brown   }
1039b1a0a8a3SJed Brown   PetscFunctionReturn(0);
1040ab3e923fSDmitry Karpeev } /* PCGASMGetSubKSP_GASM() */
1041b1a0a8a3SJed Brown 
1042b1a0a8a3SJed Brown /*@C
1043f1580f4eSBarry Smith     PCGASMSetSubdomains - Sets the subdomains for this MPI rank
1044f1580f4eSBarry Smith     for the additive Schwarz preconditioner with multiple MPI ranks per subdomain, `PCGASM`
1045b1a0a8a3SJed Brown 
10468f3b4b4dSDmitry Karpeev     Collective on pc
1047b1a0a8a3SJed Brown 
1048b1a0a8a3SJed Brown     Input Parameters:
10498f3b4b4dSDmitry Karpeev +   pc  - the preconditioner object
1050f1580f4eSBarry Smith .   n   - the number of subdomains for this MPI rank
10518f3b4b4dSDmitry Karpeev .   iis - the index sets that define the inner subdomains (or NULL for PETSc to determine subdomains)
10528f3b4b4dSDmitry Karpeev -   ois - the index sets that define the outer subdomains (or NULL to use the same as iis, or to construct by expanding iis by the requested overlap)
1053b1a0a8a3SJed Brown 
1054b1a0a8a3SJed Brown     Notes:
1055f1580f4eSBarry Smith     The `IS` indices use the parallel, global numbering of the vector entries.
10566a4f0f73SDmitry Karpeev     Inner subdomains are those where the correction is applied.
10576a4f0f73SDmitry Karpeev     Outer subdomains are those where the residual necessary to obtain the
1058f1580f4eSBarry Smith     corrections is obtained (see `PCGASMType` for the use of inner/outer subdomains).
1059f1580f4eSBarry Smith     Both inner and outer subdomains can extend over several MPI ranks.
1060f1580f4eSBarry Smith     This rank's portion of a subdomain is known as a local subdomain.
10616a4f0f73SDmitry Karpeev 
1062f1580f4eSBarry Smith     Inner subdomains can not overlap with each other, do not have any entities from remote ranks,
1063f1580f4eSBarry Smith     and  have to cover the entire local subdomain owned by the current rank. The index sets on each
1064f1580f4eSBarry Smith     rank should be ordered such that the ith local subdomain is connected to the ith remote subdomain
1065f1580f4eSBarry Smith     on another MPI rank.
1066670417b2SFande Kong 
1067f1580f4eSBarry Smith     By default the `PGASM` preconditioner uses 1 (local) subdomain per MPI rank.
10686a4f0f73SDmitry Karpeev 
1069b1a0a8a3SJed Brown     Level: advanced
1070b1a0a8a3SJed Brown 
1071f1580f4eSBarry Smith .seealso: `PCGASM`, `PCGASMSetOverlap()`, `PCGASMGetSubKSP()`,
1072db781477SPatrick Sanan           `PCGASMCreateSubdomains2D()`, `PCGASMGetSubdomains()`
1073b1a0a8a3SJed Brown @*/
1074*d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGASMSetSubdomains(PC pc, PetscInt n, IS iis[], IS ois[])
1075*d71ae5a4SJacob Faibussowitsch {
10768f3b4b4dSDmitry Karpeev   PC_GASM *osm = (PC_GASM *)pc->data;
1077b1a0a8a3SJed Brown 
1078b1a0a8a3SJed Brown   PetscFunctionBegin;
1079b1a0a8a3SJed Brown   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1080cac4c232SBarry Smith   PetscTryMethod(pc, "PCGASMSetSubdomains_C", (PC, PetscInt, IS[], IS[]), (pc, n, iis, ois));
10818f3b4b4dSDmitry Karpeev   osm->dm_subdomains = PETSC_FALSE;
1082b1a0a8a3SJed Brown   PetscFunctionReturn(0);
1083b1a0a8a3SJed Brown }
1084b1a0a8a3SJed Brown 
1085b1a0a8a3SJed Brown /*@
1086f746d493SDmitry Karpeev     PCGASMSetOverlap - Sets the overlap between a pair of subdomains for the
1087f1580f4eSBarry Smith     additive Schwarz preconditioner `PCGASM`.  Either all or no MPI ranks in the
10888f3b4b4dSDmitry Karpeev     pc communicator must call this routine.
1089b1a0a8a3SJed Brown 
10908f3b4b4dSDmitry Karpeev     Logically Collective on pc
1091b1a0a8a3SJed Brown 
1092b1a0a8a3SJed Brown     Input Parameters:
1093b1a0a8a3SJed Brown +   pc  - the preconditioner context
10948f3b4b4dSDmitry Karpeev -   ovl - the amount of overlap between subdomains (ovl >= 0, default value = 0)
1095b1a0a8a3SJed Brown 
1096b1a0a8a3SJed Brown     Options Database Key:
109706b43e7eSDmitry Karpeev .   -pc_gasm_overlap <overlap> - Sets overlap
1098b1a0a8a3SJed Brown 
1099b1a0a8a3SJed Brown     Notes:
1100f1580f4eSBarry Smith     By default the `PCGASM` preconditioner uses 1 subdomain per rank.  To use
11018f3b4b4dSDmitry Karpeev     multiple subdomain per perocessor or "straddling" subdomains that intersect
1102f1580f4eSBarry Smith     multiple ranks use `PCGASMSetSubdomains()` (or option -pc_gasm_total_subdomains <n>).
1103b1a0a8a3SJed Brown 
11048f3b4b4dSDmitry Karpeev     The overlap defaults to 0, so if one desires that no additional
1105b1a0a8a3SJed Brown     overlap be computed beyond what may have been set with a call to
1106f1580f4eSBarry Smith     `PCGASMSetSubdomains()`, then ovl must be set to be 0.  In particular, if one does
11078f3b4b4dSDmitry Karpeev     not explicitly set the subdomains in application code, then all overlap would be computed
1108f1580f4eSBarry Smith     internally by PETSc, and using an overlap of 0 would result in an `PCGASM`
1109b1a0a8a3SJed Brown     variant that is equivalent to the block Jacobi preconditioner.
1110b1a0a8a3SJed Brown 
1111f1580f4eSBarry Smith     One can define initial index sets with any overlap via
1112f1580f4eSBarry Smith     `PCGASMSetSubdomains()`; the routine `PCGASMSetOverlap()` merely allows
111306b43e7eSDmitry Karpeev     PETSc to extend that overlap further, if desired.
1114b1a0a8a3SJed Brown 
1115b1a0a8a3SJed Brown     Level: intermediate
1116b1a0a8a3SJed Brown 
1117f1580f4eSBarry Smith .seealso: `PCGASM`, `PCGASMSetSubdomains()`, `PCGASMGetSubKSP()`,
1118db781477SPatrick Sanan           `PCGASMCreateSubdomains2D()`, `PCGASMGetSubdomains()`
1119b1a0a8a3SJed Brown @*/
1120*d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGASMSetOverlap(PC pc, PetscInt ovl)
1121*d71ae5a4SJacob Faibussowitsch {
11228f3b4b4dSDmitry Karpeev   PC_GASM *osm = (PC_GASM *)pc->data;
1123b1a0a8a3SJed Brown 
1124b1a0a8a3SJed Brown   PetscFunctionBegin;
1125b1a0a8a3SJed Brown   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1126b1a0a8a3SJed Brown   PetscValidLogicalCollectiveInt(pc, ovl, 2);
1127cac4c232SBarry Smith   PetscTryMethod(pc, "PCGASMSetOverlap_C", (PC, PetscInt), (pc, ovl));
11288f3b4b4dSDmitry Karpeev   osm->dm_subdomains = PETSC_FALSE;
1129b1a0a8a3SJed Brown   PetscFunctionReturn(0);
1130b1a0a8a3SJed Brown }
1131b1a0a8a3SJed Brown 
1132b1a0a8a3SJed Brown /*@
1133f746d493SDmitry Karpeev     PCGASMSetType - Sets the type of restriction and interpolation used
1134f1580f4eSBarry Smith     for local problems in the `PCGASM` additive Schwarz method.
1135b1a0a8a3SJed Brown 
1136f1580f4eSBarry Smith     Logically Collective on pc
1137b1a0a8a3SJed Brown 
1138b1a0a8a3SJed Brown     Input Parameters:
1139b1a0a8a3SJed Brown +   pc  - the preconditioner context
1140f1580f4eSBarry Smith -   type - variant of `PCGASM`, one of
1141b1a0a8a3SJed Brown .vb
1142f1580f4eSBarry Smith       `PC_GASM_BASIC`       - full interpolation and restriction
1143f1580f4eSBarry Smith       `PC_GASM_RESTRICT`    - full restriction, local MPI rank interpolation
1144f1580f4eSBarry Smith       `PC_GASM_INTERPOLATE` - full interpolation, local MPI rank restriction
1145f1580f4eSBarry Smith       `PC_GASM_NONE`        - local MPI rank restriction and interpolation
1146b1a0a8a3SJed Brown .ve
1147b1a0a8a3SJed Brown 
1148b1a0a8a3SJed Brown     Options Database Key:
1149f746d493SDmitry Karpeev .   -pc_gasm_type [basic,restrict,interpolate,none] - Sets GASM type
1150b1a0a8a3SJed Brown 
1151b1a0a8a3SJed Brown     Level: intermediate
1152b1a0a8a3SJed Brown 
1153f1580f4eSBarry Smith .seealso: `PCGASM`, `PCGASMSetSubdomains()`, `PCGASMGetSubKSP()`,
1154f1580f4eSBarry Smith           `PCGASMCreateSubdomains2D()`, `PCASM`, `PCASMSetType()`
1155b1a0a8a3SJed Brown @*/
1156*d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGASMSetType(PC pc, PCGASMType type)
1157*d71ae5a4SJacob Faibussowitsch {
1158b1a0a8a3SJed Brown   PetscFunctionBegin;
1159b1a0a8a3SJed Brown   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1160b1a0a8a3SJed Brown   PetscValidLogicalCollectiveEnum(pc, type, 2);
1161cac4c232SBarry Smith   PetscTryMethod(pc, "PCGASMSetType_C", (PC, PCGASMType), (pc, type));
1162b1a0a8a3SJed Brown   PetscFunctionReturn(0);
1163b1a0a8a3SJed Brown }
1164b1a0a8a3SJed Brown 
1165b1a0a8a3SJed Brown /*@
1166f746d493SDmitry Karpeev     PCGASMSetSortIndices - Determines whether subdomain indices are sorted.
1167b1a0a8a3SJed Brown 
1168f1580f4eSBarry Smith     Logically Collective on pc
1169b1a0a8a3SJed Brown 
1170b1a0a8a3SJed Brown     Input Parameters:
1171b1a0a8a3SJed Brown +   pc  - the preconditioner context
1172b1a0a8a3SJed Brown -   doSort - sort the subdomain indices
1173b1a0a8a3SJed Brown 
1174b1a0a8a3SJed Brown     Level: intermediate
1175b1a0a8a3SJed Brown 
1176f1580f4eSBarry Smith .seealso: `PCGASM`, `PCGASMSetSubdomains()`, `PCGASMGetSubKSP()`,
1177db781477SPatrick Sanan           `PCGASMCreateSubdomains2D()`
1178b1a0a8a3SJed Brown @*/
1179*d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGASMSetSortIndices(PC pc, PetscBool doSort)
1180*d71ae5a4SJacob Faibussowitsch {
1181b1a0a8a3SJed Brown   PetscFunctionBegin;
1182b1a0a8a3SJed Brown   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1183b1a0a8a3SJed Brown   PetscValidLogicalCollectiveBool(pc, doSort, 2);
1184cac4c232SBarry Smith   PetscTryMethod(pc, "PCGASMSetSortIndices_C", (PC, PetscBool), (pc, doSort));
1185b1a0a8a3SJed Brown   PetscFunctionReturn(0);
1186b1a0a8a3SJed Brown }
1187b1a0a8a3SJed Brown 
1188b1a0a8a3SJed Brown /*@C
1189f1580f4eSBarry Smith    PCGASMGetSubKSP - Gets the local `KSP` contexts for all subdomains on
1190f1580f4eSBarry Smith    this MPI rank.
1191b1a0a8a3SJed Brown 
1192f1580f4eSBarry Smith    Collective on pc iff first_local is requested
1193b1a0a8a3SJed Brown 
1194b1a0a8a3SJed Brown    Input Parameter:
1195b1a0a8a3SJed Brown .  pc - the preconditioner context
1196b1a0a8a3SJed Brown 
1197b1a0a8a3SJed Brown    Output Parameters:
1198f1580f4eSBarry Smith +  n_local - the number of blocks on this MPI rank or NULL
1199f1580f4eSBarry Smith .  first_local - the global number of the first block on this rank or NULL,
1200f1580f4eSBarry Smith                  all ranks must request or all must pass NULL
1201f1580f4eSBarry Smith -  ksp - the array of `KSP` contexts
1202b1a0a8a3SJed Brown 
1203b1a0a8a3SJed Brown    Note:
1204f1580f4eSBarry Smith    After `PCGASMGetSubKSP()` the array of `KSP`es is not to be freed
1205b1a0a8a3SJed Brown 
1206f1580f4eSBarry Smith    Currently for some matrix implementations only 1 block per MPI rank
1207b1a0a8a3SJed Brown    is supported.
1208b1a0a8a3SJed Brown 
1209f1580f4eSBarry Smith    You must call `KSPSetUp()` before calling `PCGASMGetSubKSP()`.
1210b1a0a8a3SJed Brown 
1211b1a0a8a3SJed Brown    Level: advanced
1212b1a0a8a3SJed Brown 
1213f1580f4eSBarry Smith .seealso: `PCGASM`, `PCGASMSetSubdomains()`, `PCGASMSetOverlap()`,
1214db781477SPatrick Sanan           `PCGASMCreateSubdomains2D()`,
1215b1a0a8a3SJed Brown @*/
1216*d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGASMGetSubKSP(PC pc, PetscInt *n_local, PetscInt *first_local, KSP *ksp[])
1217*d71ae5a4SJacob Faibussowitsch {
1218b1a0a8a3SJed Brown   PetscFunctionBegin;
1219b1a0a8a3SJed Brown   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1220cac4c232SBarry Smith   PetscUseMethod(pc, "PCGASMGetSubKSP_C", (PC, PetscInt *, PetscInt *, KSP **), (pc, n_local, first_local, ksp));
1221b1a0a8a3SJed Brown   PetscFunctionReturn(0);
1222b1a0a8a3SJed Brown }
1223b1a0a8a3SJed Brown 
1224b1a0a8a3SJed Brown /*MC
1225f746d493SDmitry Karpeev    PCGASM - Use the (restricted) additive Schwarz method, each block is (approximately) solved with
1226f1580f4eSBarry Smith            its own `KSP` object on a subset of MPI ranks
1227b1a0a8a3SJed Brown 
1228b1a0a8a3SJed Brown    Options Database Keys:
1229f1580f4eSBarry Smith +  -pc_gasm_total_subdomains <n>  - Sets total number of local subdomains to be distributed among MPI ranks
1230f1580f4eSBarry Smith .  -pc_gasm_view_subdomains       - activates the printing of subdomain indices in `PCView()`, -ksp_view or -snes_view
1231f1580f4eSBarry Smith .  -pc_gasm_print_subdomains      - activates the printing of subdomain indices in `PCSetUp()`
123206b43e7eSDmitry Karpeev .  -pc_gasm_overlap <ovl>         - Sets overlap by which to (automatically) extend local subdomains
1233f1580f4eSBarry Smith -  -pc_gasm_type [basic,restrict,interpolate,none] - Sets `PCGASMType`
1234b1a0a8a3SJed Brown 
123595452b02SPatrick Sanan    Notes:
1236f1580f4eSBarry Smith    To set options on the solvers for each block append -sub_ to all the `KSP`, and `PC`
1237b1a0a8a3SJed Brown    options database keys. For example, -sub_pc_type ilu -sub_pc_factor_levels 1 -sub_ksp_type preonly
1238b1a0a8a3SJed Brown 
1239f1580f4eSBarry Smith    To set the options on the solvers separate for each block call `PCGASMGetSubKSP()`
1240f1580f4eSBarry Smith    and set the options directly on the resulting `KSP` object (you can access its `PC`
1241f1580f4eSBarry Smith    with `KSPGetPC())`
1242b1a0a8a3SJed Brown 
1243b1a0a8a3SJed Brown    Level: beginner
1244b1a0a8a3SJed Brown 
1245b1a0a8a3SJed Brown     References:
1246606c0280SSatish Balay +   * - M Dryja, OB Widlund, An additive variant of the Schwarz alternating method for the case of many subregions
124796a0c994SBarry Smith      Courant Institute, New York University Technical report
1248606c0280SSatish Balay -   * - Barry Smith, Petter Bjorstad, and William Gropp, Domain Decompositions: Parallel Multilevel Methods for Elliptic Partial Differential Equations,
124996a0c994SBarry Smith     Cambridge University Press.
1250b1a0a8a3SJed Brown 
1251f1580f4eSBarry Smith .seealso: `PCCreate()`, `PCSetType()`, `PCType`, `PC`, `PCASM`, `PCGASMType`, `PCGASMSetType()`,
1252db781477SPatrick Sanan           `PCBJACOBI`, `PCGASMGetSubKSP()`, `PCGASMSetSubdomains()`,
1253db781477SPatrick Sanan           `PCSetModifySubMatrices()`, `PCGASMSetOverlap()`, `PCGASMSetType()`
1254b1a0a8a3SJed Brown M*/
1255b1a0a8a3SJed Brown 
1256*d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PCCreate_GASM(PC pc)
1257*d71ae5a4SJacob Faibussowitsch {
1258f746d493SDmitry Karpeev   PC_GASM *osm;
1259b1a0a8a3SJed Brown 
1260b1a0a8a3SJed Brown   PetscFunctionBegin;
12614dfa11a4SJacob Faibussowitsch   PetscCall(PetscNew(&osm));
12622fa5cd67SKarl Rupp 
12638f3b4b4dSDmitry Karpeev   osm->N                        = PETSC_DETERMINE;
126406b43e7eSDmitry Karpeev   osm->n                        = PETSC_DECIDE;
12658f3b4b4dSDmitry Karpeev   osm->nmax                     = PETSC_DETERMINE;
12668f3b4b4dSDmitry Karpeev   osm->overlap                  = 0;
12670a545947SLisandro Dalcin   osm->ksp                      = NULL;
12680a545947SLisandro Dalcin   osm->gorestriction            = NULL;
12690a545947SLisandro Dalcin   osm->girestriction            = NULL;
12700a545947SLisandro Dalcin   osm->pctoouter                = NULL;
12710a545947SLisandro Dalcin   osm->gx                       = NULL;
12720a545947SLisandro Dalcin   osm->gy                       = NULL;
12730a545947SLisandro Dalcin   osm->x                        = NULL;
12740a545947SLisandro Dalcin   osm->y                        = NULL;
12750a545947SLisandro Dalcin   osm->pcx                      = NULL;
12760a545947SLisandro Dalcin   osm->pcy                      = NULL;
12770a545947SLisandro Dalcin   osm->permutationIS            = NULL;
12780a545947SLisandro Dalcin   osm->permutationP             = NULL;
12790a545947SLisandro Dalcin   osm->pcmat                    = NULL;
12800a545947SLisandro Dalcin   osm->ois                      = NULL;
12810a545947SLisandro Dalcin   osm->iis                      = NULL;
12820a545947SLisandro Dalcin   osm->pmat                     = NULL;
1283f746d493SDmitry Karpeev   osm->type                     = PC_GASM_RESTRICT;
12846a4f0f73SDmitry Karpeev   osm->same_subdomain_solvers   = PETSC_TRUE;
1285b1a0a8a3SJed Brown   osm->sort_indices             = PETSC_TRUE;
1286d709ea83SDmitry Karpeev   osm->dm_subdomains            = PETSC_FALSE;
1287ea91fabdSFande Kong   osm->hierarchicalpartitioning = PETSC_FALSE;
1288b1a0a8a3SJed Brown 
1289b1a0a8a3SJed Brown   pc->data                 = (void *)osm;
1290f746d493SDmitry Karpeev   pc->ops->apply           = PCApply_GASM;
129148e38a8aSPierre Jolivet   pc->ops->matapply        = PCMatApply_GASM;
1292f746d493SDmitry Karpeev   pc->ops->applytranspose  = PCApplyTranspose_GASM;
1293f746d493SDmitry Karpeev   pc->ops->setup           = PCSetUp_GASM;
1294a06653b4SBarry Smith   pc->ops->reset           = PCReset_GASM;
1295f746d493SDmitry Karpeev   pc->ops->destroy         = PCDestroy_GASM;
1296f746d493SDmitry Karpeev   pc->ops->setfromoptions  = PCSetFromOptions_GASM;
1297f746d493SDmitry Karpeev   pc->ops->setuponblocks   = PCSetUpOnBlocks_GASM;
1298f746d493SDmitry Karpeev   pc->ops->view            = PCView_GASM;
12990a545947SLisandro Dalcin   pc->ops->applyrichardson = NULL;
1300b1a0a8a3SJed Brown 
13019566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetSubdomains_C", PCGASMSetSubdomains_GASM));
13029566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetOverlap_C", PCGASMSetOverlap_GASM));
13039566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetType_C", PCGASMSetType_GASM));
13049566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetSortIndices_C", PCGASMSetSortIndices_GASM));
13059566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMGetSubKSP_C", PCGASMGetSubKSP_GASM));
1306b1a0a8a3SJed Brown   PetscFunctionReturn(0);
1307b1a0a8a3SJed Brown }
1308b1a0a8a3SJed Brown 
1309*d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGASMCreateLocalSubdomains(Mat A, PetscInt nloc, IS *iis[])
1310*d71ae5a4SJacob Faibussowitsch {
1311b1a0a8a3SJed Brown   MatPartitioning mpart;
1312b1a0a8a3SJed Brown   const char     *prefix;
1313b1a0a8a3SJed Brown   PetscInt        i, j, rstart, rend, bs;
1314976e8c5aSLisandro Dalcin   PetscBool       hasop, isbaij = PETSC_FALSE, foundpart = PETSC_FALSE;
13150298fd71SBarry Smith   Mat             Ad = NULL, adj;
1316b1a0a8a3SJed Brown   IS              ispart, isnumb, *is;
1317b1a0a8a3SJed Brown 
1318b1a0a8a3SJed Brown   PetscFunctionBegin;
131963a3b9bcSJacob Faibussowitsch   PetscCheck(nloc >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "number of local subdomains must > 0, got nloc = %" PetscInt_FMT, nloc);
1320b1a0a8a3SJed Brown 
1321b1a0a8a3SJed Brown   /* Get prefix, row distribution, and block size */
13229566063dSJacob Faibussowitsch   PetscCall(MatGetOptionsPrefix(A, &prefix));
13239566063dSJacob Faibussowitsch   PetscCall(MatGetOwnershipRange(A, &rstart, &rend));
13249566063dSJacob Faibussowitsch   PetscCall(MatGetBlockSize(A, &bs));
13252472a847SBarry Smith   PetscCheck(rstart / bs * bs == rstart && rend / bs * bs == rend, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "bad row distribution [%" PetscInt_FMT ",%" PetscInt_FMT ") for matrix block size %" PetscInt_FMT, rstart, rend, bs);
1326b1a0a8a3SJed Brown 
1327b1a0a8a3SJed Brown   /* Get diagonal block from matrix if possible */
13289566063dSJacob Faibussowitsch   PetscCall(MatHasOperation(A, MATOP_GET_DIAGONAL_BLOCK, &hasop));
132948a46eb9SPierre Jolivet   if (hasop) PetscCall(MatGetDiagonalBlock(A, &Ad));
1330b1a0a8a3SJed Brown   if (Ad) {
13319566063dSJacob Faibussowitsch     PetscCall(PetscObjectBaseTypeCompare((PetscObject)Ad, MATSEQBAIJ, &isbaij));
13329566063dSJacob Faibussowitsch     if (!isbaij) PetscCall(PetscObjectBaseTypeCompare((PetscObject)Ad, MATSEQSBAIJ, &isbaij));
1333b1a0a8a3SJed Brown   }
13348f3b4b4dSDmitry Karpeev   if (Ad && nloc > 1) {
1335b1a0a8a3SJed Brown     PetscBool match, done;
1336b1a0a8a3SJed Brown     /* Try to setup a good matrix partitioning if available */
13379566063dSJacob Faibussowitsch     PetscCall(MatPartitioningCreate(PETSC_COMM_SELF, &mpart));
13389566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)mpart, prefix));
13399566063dSJacob Faibussowitsch     PetscCall(MatPartitioningSetFromOptions(mpart));
13409566063dSJacob Faibussowitsch     PetscCall(PetscObjectTypeCompare((PetscObject)mpart, MATPARTITIONINGCURRENT, &match));
134148a46eb9SPierre Jolivet     if (!match) PetscCall(PetscObjectTypeCompare((PetscObject)mpart, MATPARTITIONINGSQUARE, &match));
1342b1a0a8a3SJed Brown     if (!match) { /* assume a "good" partitioner is available */
13431a83f524SJed Brown       PetscInt        na;
13441a83f524SJed Brown       const PetscInt *ia, *ja;
13459566063dSJacob Faibussowitsch       PetscCall(MatGetRowIJ(Ad, 0, PETSC_TRUE, isbaij, &na, &ia, &ja, &done));
1346b1a0a8a3SJed Brown       if (done) {
1347b1a0a8a3SJed Brown         /* Build adjacency matrix by hand. Unfortunately a call to
1348b1a0a8a3SJed Brown            MatConvert(Ad,MATMPIADJ,MAT_INITIAL_MATRIX,&adj) will
1349b1a0a8a3SJed Brown            remove the block-aij structure and we cannot expect
1350b1a0a8a3SJed Brown            MatPartitioning to split vertices as we need */
13510a545947SLisandro Dalcin         PetscInt        i, j, len, nnz, cnt, *iia = NULL, *jja = NULL;
13521a83f524SJed Brown         const PetscInt *row;
1353b1a0a8a3SJed Brown         nnz = 0;
1354b1a0a8a3SJed Brown         for (i = 0; i < na; i++) { /* count number of nonzeros */
1355b1a0a8a3SJed Brown           len = ia[i + 1] - ia[i];
1356b1a0a8a3SJed Brown           row = ja + ia[i];
1357b1a0a8a3SJed Brown           for (j = 0; j < len; j++) {
1358b1a0a8a3SJed Brown             if (row[j] == i) { /* don't count diagonal */
13599371c9d4SSatish Balay               len--;
13609371c9d4SSatish Balay               break;
1361b1a0a8a3SJed Brown             }
1362b1a0a8a3SJed Brown           }
1363b1a0a8a3SJed Brown           nnz += len;
1364b1a0a8a3SJed Brown         }
13659566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1(na + 1, &iia));
13669566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1(nnz, &jja));
1367b1a0a8a3SJed Brown         nnz    = 0;
1368b1a0a8a3SJed Brown         iia[0] = 0;
1369b1a0a8a3SJed Brown         for (i = 0; i < na; i++) { /* fill adjacency */
1370b1a0a8a3SJed Brown           cnt = 0;
1371b1a0a8a3SJed Brown           len = ia[i + 1] - ia[i];
1372b1a0a8a3SJed Brown           row = ja + ia[i];
1373b1a0a8a3SJed Brown           for (j = 0; j < len; j++) {
13742fa5cd67SKarl Rupp             if (row[j] != i) jja[nnz + cnt++] = row[j]; /* if not diagonal */
1375b1a0a8a3SJed Brown           }
1376b1a0a8a3SJed Brown           nnz += cnt;
1377b1a0a8a3SJed Brown           iia[i + 1] = nnz;
1378b1a0a8a3SJed Brown         }
1379b1a0a8a3SJed Brown         /* Partitioning of the adjacency matrix */
13809566063dSJacob Faibussowitsch         PetscCall(MatCreateMPIAdj(PETSC_COMM_SELF, na, na, iia, jja, NULL, &adj));
13819566063dSJacob Faibussowitsch         PetscCall(MatPartitioningSetAdjacency(mpart, adj));
13829566063dSJacob Faibussowitsch         PetscCall(MatPartitioningSetNParts(mpart, nloc));
13839566063dSJacob Faibussowitsch         PetscCall(MatPartitioningApply(mpart, &ispart));
13849566063dSJacob Faibussowitsch         PetscCall(ISPartitioningToNumbering(ispart, &isnumb));
13859566063dSJacob Faibussowitsch         PetscCall(MatDestroy(&adj));
1386b1a0a8a3SJed Brown         foundpart = PETSC_TRUE;
1387b1a0a8a3SJed Brown       }
13889566063dSJacob Faibussowitsch       PetscCall(MatRestoreRowIJ(Ad, 0, PETSC_TRUE, isbaij, &na, &ia, &ja, &done));
1389b1a0a8a3SJed Brown     }
13909566063dSJacob Faibussowitsch     PetscCall(MatPartitioningDestroy(&mpart));
1391b1a0a8a3SJed Brown   }
13929566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(nloc, &is));
1393b1a0a8a3SJed Brown   if (!foundpart) {
1394b1a0a8a3SJed Brown     /* Partitioning by contiguous chunks of rows */
1395b1a0a8a3SJed Brown 
1396b1a0a8a3SJed Brown     PetscInt mbs   = (rend - rstart) / bs;
1397b1a0a8a3SJed Brown     PetscInt start = rstart;
13988f3b4b4dSDmitry Karpeev     for (i = 0; i < nloc; i++) {
13998f3b4b4dSDmitry Karpeev       PetscInt count = (mbs / nloc + ((mbs % nloc) > i)) * bs;
14009566063dSJacob Faibussowitsch       PetscCall(ISCreateStride(PETSC_COMM_SELF, count, start, 1, &is[i]));
1401b1a0a8a3SJed Brown       start += count;
1402b1a0a8a3SJed Brown     }
1403b1a0a8a3SJed Brown 
1404b1a0a8a3SJed Brown   } else {
1405b1a0a8a3SJed Brown     /* Partitioning by adjacency of diagonal block  */
1406b1a0a8a3SJed Brown 
1407b1a0a8a3SJed Brown     const PetscInt *numbering;
1408b1a0a8a3SJed Brown     PetscInt       *count, nidx, *indices, *newidx, start = 0;
1409b1a0a8a3SJed Brown     /* Get node count in each partition */
14109566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nloc, &count));
14119566063dSJacob Faibussowitsch     PetscCall(ISPartitioningCount(ispart, nloc, count));
1412b1a0a8a3SJed Brown     if (isbaij && bs > 1) { /* adjust for the block-aij case */
14138f3b4b4dSDmitry Karpeev       for (i = 0; i < nloc; i++) count[i] *= bs;
1414b1a0a8a3SJed Brown     }
1415b1a0a8a3SJed Brown     /* Build indices from node numbering */
14169566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(isnumb, &nidx));
14179566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nidx, &indices));
1418b1a0a8a3SJed Brown     for (i = 0; i < nidx; i++) indices[i] = i; /* needs to be initialized */
14199566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(isnumb, &numbering));
14209566063dSJacob Faibussowitsch     PetscCall(PetscSortIntWithPermutation(nidx, numbering, indices));
14219566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(isnumb, &numbering));
1422b1a0a8a3SJed Brown     if (isbaij && bs > 1) { /* adjust for the block-aij case */
14239566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nidx * bs, &newidx));
14242fa5cd67SKarl Rupp       for (i = 0; i < nidx; i++) {
14252fa5cd67SKarl Rupp         for (j = 0; j < bs; j++) newidx[i * bs + j] = indices[i] * bs + j;
14262fa5cd67SKarl Rupp       }
14279566063dSJacob Faibussowitsch       PetscCall(PetscFree(indices));
1428b1a0a8a3SJed Brown       nidx *= bs;
1429b1a0a8a3SJed Brown       indices = newidx;
1430b1a0a8a3SJed Brown     }
1431b1a0a8a3SJed Brown     /* Shift to get global indices */
1432b1a0a8a3SJed Brown     for (i = 0; i < nidx; i++) indices[i] += rstart;
1433b1a0a8a3SJed Brown 
1434b1a0a8a3SJed Brown     /* Build the index sets for each block */
14358f3b4b4dSDmitry Karpeev     for (i = 0; i < nloc; i++) {
14369566063dSJacob Faibussowitsch       PetscCall(ISCreateGeneral(PETSC_COMM_SELF, count[i], &indices[start], PETSC_COPY_VALUES, &is[i]));
14379566063dSJacob Faibussowitsch       PetscCall(ISSort(is[i]));
1438b1a0a8a3SJed Brown       start += count[i];
1439b1a0a8a3SJed Brown     }
1440b1a0a8a3SJed Brown 
14419566063dSJacob Faibussowitsch     PetscCall(PetscFree(count));
14429566063dSJacob Faibussowitsch     PetscCall(PetscFree(indices));
14439566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&isnumb));
14449566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&ispart));
1445b1a0a8a3SJed Brown   }
14466a4f0f73SDmitry Karpeev   *iis = is;
14478f3b4b4dSDmitry Karpeev   PetscFunctionReturn(0);
14488f3b4b4dSDmitry Karpeev }
14498f3b4b4dSDmitry Karpeev 
1450*d71ae5a4SJacob Faibussowitsch PETSC_INTERN PetscErrorCode PCGASMCreateStraddlingSubdomains(Mat A, PetscInt N, PetscInt *n, IS *iis[])
1451*d71ae5a4SJacob Faibussowitsch {
14528f3b4b4dSDmitry Karpeev   PetscFunctionBegin;
14539566063dSJacob Faibussowitsch   PetscCall(MatSubdomainsCreateCoalesce(A, N, n, iis));
14548f3b4b4dSDmitry Karpeev   PetscFunctionReturn(0);
14558f3b4b4dSDmitry Karpeev }
14568f3b4b4dSDmitry Karpeev 
14578f3b4b4dSDmitry Karpeev /*@C
1458f1580f4eSBarry Smith    PCGASMCreateSubdomains - Creates n index sets defining n nonoverlapping subdomains for the `PCGASM` additive
14598f3b4b4dSDmitry Karpeev    Schwarz preconditioner for a any problem based on its matrix.
14608f3b4b4dSDmitry Karpeev 
14618f3b4b4dSDmitry Karpeev    Collective
14628f3b4b4dSDmitry Karpeev 
14638f3b4b4dSDmitry Karpeev    Input Parameters:
14648f3b4b4dSDmitry Karpeev +  A       - The global matrix operator
14658f3b4b4dSDmitry Karpeev -  N       - the number of global subdomains requested
14668f3b4b4dSDmitry Karpeev 
14678f3b4b4dSDmitry Karpeev    Output Parameters:
1468f1580f4eSBarry Smith +  n   - the number of subdomains created on this MPI rank
14698f3b4b4dSDmitry Karpeev -  iis - the array of index sets defining the local inner subdomains (on which the correction is applied)
14708f3b4b4dSDmitry Karpeev 
14718f3b4b4dSDmitry Karpeev    Level: advanced
14728f3b4b4dSDmitry Karpeev 
1473f1580f4eSBarry Smith    Note:
1474f1580f4eSBarry Smith    When N >= A's communicator size, each subdomain is local -- contained within a single MPI rank.
1475f1580f4eSBarry Smith          When N < size, the subdomains are 'straddling' (rank boundaries) and are no longer local.
1476f1580f4eSBarry Smith          The resulting subdomains can be use in `PCGASMSetSubdomains`(pc,n,iss,NULL).  The overlapping
14778f3b4b4dSDmitry Karpeev          outer subdomains will be automatically generated from these according to the requested amount of
14788f3b4b4dSDmitry Karpeev          overlap; this is currently supported only with local subdomains.
14798f3b4b4dSDmitry Karpeev 
1480f1580f4eSBarry Smith .seealso: `PCGASM`, `PCGASMSetSubdomains()`, `PCGASMDestroySubdomains()`
14818f3b4b4dSDmitry Karpeev @*/
1482*d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGASMCreateSubdomains(Mat A, PetscInt N, PetscInt *n, IS *iis[])
1483*d71ae5a4SJacob Faibussowitsch {
14848f3b4b4dSDmitry Karpeev   PetscMPIInt size;
14858f3b4b4dSDmitry Karpeev 
14868f3b4b4dSDmitry Karpeev   PetscFunctionBegin;
14878f3b4b4dSDmitry Karpeev   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
14888f3b4b4dSDmitry Karpeev   PetscValidPointer(iis, 4);
14898f3b4b4dSDmitry Karpeev 
149063a3b9bcSJacob Faibussowitsch   PetscCheck(N >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of subdomains must be > 0, N = %" PetscInt_FMT, N);
14919566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)A), &size));
14928f3b4b4dSDmitry Karpeev   if (N >= size) {
14938f3b4b4dSDmitry Karpeev     *n = N / size + (N % size);
14949566063dSJacob Faibussowitsch     PetscCall(PCGASMCreateLocalSubdomains(A, *n, iis));
14956a4f0f73SDmitry Karpeev   } else {
14969566063dSJacob Faibussowitsch     PetscCall(PCGASMCreateStraddlingSubdomains(A, N, n, iis));
14976a4f0f73SDmitry Karpeev   }
1498b1a0a8a3SJed Brown   PetscFunctionReturn(0);
1499b1a0a8a3SJed Brown }
1500b1a0a8a3SJed Brown 
1501b1a0a8a3SJed Brown /*@C
1502f746d493SDmitry Karpeev    PCGASMDestroySubdomains - Destroys the index sets created with
1503f1580f4eSBarry Smith    `PCGASMCreateSubdomains()` or `PCGASMCreateSubdomains2D()`. Should be
1504f1580f4eSBarry Smith    called after setting subdomains with `PCGASMSetSubdomains()`.
1505b1a0a8a3SJed Brown 
1506b1a0a8a3SJed Brown    Collective
1507b1a0a8a3SJed Brown 
1508b1a0a8a3SJed Brown    Input Parameters:
1509b1a0a8a3SJed Brown +  n   - the number of index sets
15106a4f0f73SDmitry Karpeev .  iis - the array of inner subdomains,
15110298fd71SBarry Smith -  ois - the array of outer subdomains, can be NULL
1512b1a0a8a3SJed Brown 
15136a4f0f73SDmitry Karpeev    Level: intermediate
15146a4f0f73SDmitry Karpeev 
1515f1580f4eSBarry Smith    Note:
1516f1580f4eSBarry Smith    This is a convenience subroutine that walks each list,
1517f1580f4eSBarry Smith    destroys each `IS` on the list, and then frees the list. At the end the
15182c112581SDmitry Karpeev    list pointers are set to NULL.
1519b1a0a8a3SJed Brown 
1520f1580f4eSBarry Smith .seealso: `PCGASM`, `PCGASMCreateSubdomains()`, `PCGASMSetSubdomains()`
1521b1a0a8a3SJed Brown @*/
1522*d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGASMDestroySubdomains(PetscInt n, IS **iis, IS **ois)
1523*d71ae5a4SJacob Faibussowitsch {
1524b1a0a8a3SJed Brown   PetscInt i;
15255fd66863SKarl Rupp 
1526b1a0a8a3SJed Brown   PetscFunctionBegin;
1527a35b7b57SDmitry Karpeev   if (n <= 0) PetscFunctionReturn(0);
15286a4f0f73SDmitry Karpeev   if (ois) {
15292c112581SDmitry Karpeev     PetscValidPointer(ois, 3);
15302c112581SDmitry Karpeev     if (*ois) {
15312c112581SDmitry Karpeev       PetscValidPointer(*ois, 3);
153248a46eb9SPierre Jolivet       for (i = 0; i < n; i++) PetscCall(ISDestroy(&(*ois)[i]));
15339566063dSJacob Faibussowitsch       PetscCall(PetscFree((*ois)));
15342c112581SDmitry Karpeev     }
1535b1a0a8a3SJed Brown   }
1536b9d0fdaaSFande Kong   if (iis) {
1537b9d0fdaaSFande Kong     PetscValidPointer(iis, 2);
1538b9d0fdaaSFande Kong     if (*iis) {
1539b9d0fdaaSFande Kong       PetscValidPointer(*iis, 2);
154048a46eb9SPierre Jolivet       for (i = 0; i < n; i++) PetscCall(ISDestroy(&(*iis)[i]));
15419566063dSJacob Faibussowitsch       PetscCall(PetscFree((*iis)));
1542b9d0fdaaSFande Kong     }
1543b9d0fdaaSFande Kong   }
1544b1a0a8a3SJed Brown   PetscFunctionReturn(0);
1545b1a0a8a3SJed Brown }
1546b1a0a8a3SJed Brown 
1547af538404SDmitry Karpeev #define PCGASMLocalSubdomainBounds2D(M, N, xleft, ylow, xright, yhigh, first, last, xleft_loc, ylow_loc, xright_loc, yhigh_loc, n) \
1548af538404SDmitry Karpeev   { \
1549af538404SDmitry Karpeev     PetscInt first_row = first / M, last_row = last / M + 1; \
1550af538404SDmitry Karpeev     /*                                                                                                    \
1551af538404SDmitry Karpeev      Compute ylow_loc and yhigh_loc so that (ylow_loc,xleft) and (yhigh_loc,xright) are the corners       \
1552af538404SDmitry Karpeev      of the bounding box of the intersection of the subdomain with the local ownership range (local       \
1553af538404SDmitry Karpeev      subdomain).                                                                                          \
1554af538404SDmitry Karpeev      Also compute xleft_loc and xright_loc as the lower and upper bounds on the first and last rows       \
1555af538404SDmitry Karpeev      of the intersection.                                                                                 \
1556af538404SDmitry Karpeev     */ \
1557af538404SDmitry Karpeev     /* ylow_loc is the grid row containing the first element of the local sumbdomain */ \
1558eec7e3faSDmitry Karpeev     *ylow_loc = PetscMax(first_row, ylow); \
1559af538404SDmitry Karpeev     /* xleft_loc is the offset of first element of the local subdomain within its grid row (might actually be outside the local subdomain) */ \
1560eec7e3faSDmitry Karpeev     *xleft_loc = *ylow_loc == first_row ? PetscMax(first % M, xleft) : xleft; \
1561af538404SDmitry Karpeev     /* yhigh_loc is the grid row above the last local subdomain element */ \
1562eec7e3faSDmitry Karpeev     *yhigh_loc = PetscMin(last_row, yhigh); \
1563af538404SDmitry Karpeev     /* xright is the offset of the end of the  local subdomain within its grid row (might actually be outside the local subdomain) */ \
1564eec7e3faSDmitry Karpeev     *xright_loc = *yhigh_loc == last_row ? PetscMin(xright, last % M) : xright; \
1565af538404SDmitry Karpeev     /* Now compute the size of the local subdomain n. */ \
1566c3518bceSDmitry Karpeev     *n = 0; \
1567eec7e3faSDmitry Karpeev     if (*ylow_loc < *yhigh_loc) { \
1568af538404SDmitry Karpeev       PetscInt width = xright - xleft; \
1569eec7e3faSDmitry Karpeev       *n += width * (*yhigh_loc - *ylow_loc - 1); \
1570eec7e3faSDmitry Karpeev       *n += PetscMin(PetscMax(*xright_loc - xleft, 0), width); \
1571eec7e3faSDmitry Karpeev       *n -= PetscMin(PetscMax(*xleft_loc - xleft, 0), width); \
1572af538404SDmitry Karpeev     } \
1573af538404SDmitry Karpeev   }
1574af538404SDmitry Karpeev 
1575b1a0a8a3SJed Brown /*@
1576f1580f4eSBarry Smith    PCGASMCreateSubdomains2D - Creates the index sets for the `PCGASM` overlapping Schwarz
1577b1a0a8a3SJed Brown    preconditioner for a two-dimensional problem on a regular grid.
1578b1a0a8a3SJed Brown 
1579af538404SDmitry Karpeev    Collective
1580b1a0a8a3SJed Brown 
1581b1a0a8a3SJed Brown    Input Parameters:
15826b867d5aSJose E. Roman +  pc       - the preconditioner context
15836b867d5aSJose E. Roman .  M        - the global number of grid points in the x direction
15846b867d5aSJose E. Roman .  N        - the global number of grid points in the y direction
15856b867d5aSJose E. Roman .  Mdomains - the global number of subdomains in the x direction
15866b867d5aSJose E. Roman .  Ndomains - the global number of subdomains in the y direction
1587b1a0a8a3SJed Brown .  dof      - degrees of freedom per node
1588b1a0a8a3SJed Brown -  overlap  - overlap in mesh lines
1589b1a0a8a3SJed Brown 
1590b1a0a8a3SJed Brown    Output Parameters:
1591af538404SDmitry Karpeev +  Nsub - the number of local subdomains created
15926a4f0f73SDmitry Karpeev .  iis  - array of index sets defining inner (nonoverlapping) subdomains
15936a4f0f73SDmitry Karpeev -  ois  - array of index sets defining outer (overlapping, if overlap > 0) subdomains
1594b1a0a8a3SJed Brown 
1595b1a0a8a3SJed Brown    Level: advanced
1596b1a0a8a3SJed Brown 
1597f1580f4eSBarry Smith .seealso: `PCGASM`, `PCGASMSetSubdomains()`, `PCGASMGetSubKSP()`, `PCGASMSetOverlap()`
1598b1a0a8a3SJed Brown @*/
1599*d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGASMCreateSubdomains2D(PC pc, PetscInt M, PetscInt N, PetscInt Mdomains, PetscInt Ndomains, PetscInt dof, PetscInt overlap, PetscInt *nsub, IS **iis, IS **ois)
1600*d71ae5a4SJacob Faibussowitsch {
16012bbb417fSDmitry Karpeev   PetscMPIInt size, rank;
16022bbb417fSDmitry Karpeev   PetscInt    i, j;
16032bbb417fSDmitry Karpeev   PetscInt    maxheight, maxwidth;
16042bbb417fSDmitry Karpeev   PetscInt    xstart, xleft, xright, xleft_loc, xright_loc;
16052bbb417fSDmitry Karpeev   PetscInt    ystart, ylow, yhigh, ylow_loc, yhigh_loc;
1606eec7e3faSDmitry Karpeev   PetscInt    x[2][2], y[2][2], n[2];
16072bbb417fSDmitry Karpeev   PetscInt    first, last;
16082bbb417fSDmitry Karpeev   PetscInt    nidx, *idx;
16092bbb417fSDmitry Karpeev   PetscInt    ii, jj, s, q, d;
1610af538404SDmitry Karpeev   PetscInt    k, kk;
16112bbb417fSDmitry Karpeev   PetscMPIInt color;
16122bbb417fSDmitry Karpeev   MPI_Comm    comm, subcomm;
16130a545947SLisandro Dalcin   IS        **xis = NULL, **is = ois, **is_local = iis;
1614d34fcf5fSBarry Smith 
1615b1a0a8a3SJed Brown   PetscFunctionBegin;
16169566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)pc, &comm));
16179566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
16189566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
16199566063dSJacob Faibussowitsch   PetscCall(MatGetOwnershipRange(pc->pmat, &first, &last));
16209371c9d4SSatish Balay   PetscCheck((first % dof) == 0 && (last % dof) == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
16219371c9d4SSatish Balay              "Matrix row partitioning unsuitable for domain decomposition: local row range (%" PetscInt_FMT ",%" PetscInt_FMT ") "
16229371c9d4SSatish Balay              "does not respect the number of degrees of freedom per grid point %" PetscInt_FMT,
16239371c9d4SSatish Balay              first, last, dof);
1624d34fcf5fSBarry Smith 
1625af538404SDmitry Karpeev   /* Determine the number of domains with nonzero intersections with the local ownership range. */
16262bbb417fSDmitry Karpeev   s      = 0;
1627af538404SDmitry Karpeev   ystart = 0;
1628af538404SDmitry Karpeev   for (j = 0; j < Ndomains; ++j) {
1629af538404SDmitry Karpeev     maxheight = N / Ndomains + ((N % Ndomains) > j); /* Maximal height of subdomain */
163063a3b9bcSJacob Faibussowitsch     PetscCheck(maxheight >= 2, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many %" PetscInt_FMT " subdomains in the vertical directon for mesh height %" PetscInt_FMT, Ndomains, N);
1631eec7e3faSDmitry Karpeev     /* Vertical domain limits with an overlap. */
1632eec7e3faSDmitry Karpeev     ylow   = PetscMax(ystart - overlap, 0);
1633eec7e3faSDmitry Karpeev     yhigh  = PetscMin(ystart + maxheight + overlap, N);
1634b1a0a8a3SJed Brown     xstart = 0;
1635af538404SDmitry Karpeev     for (i = 0; i < Mdomains; ++i) {
1636af538404SDmitry Karpeev       maxwidth = M / Mdomains + ((M % Mdomains) > i); /* Maximal width of subdomain */
163763a3b9bcSJacob Faibussowitsch       PetscCheck(maxwidth >= 2, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many %" PetscInt_FMT " subdomains in the horizontal direction for mesh width %" PetscInt_FMT, Mdomains, M);
1638eec7e3faSDmitry Karpeev       /* Horizontal domain limits with an overlap. */
1639eec7e3faSDmitry Karpeev       xleft  = PetscMax(xstart - overlap, 0);
1640eec7e3faSDmitry Karpeev       xright = PetscMin(xstart + maxwidth + overlap, M);
1641af538404SDmitry Karpeev       /*
1642f1580f4eSBarry Smith          Determine whether this subdomain intersects this rank's ownership range of pc->pmat.
1643af538404SDmitry Karpeev       */
1644c3518bceSDmitry Karpeev       PCGASMLocalSubdomainBounds2D(M, N, xleft, ylow, xright, yhigh, first, last, (&xleft_loc), (&ylow_loc), (&xright_loc), (&yhigh_loc), (&nidx));
16452fa5cd67SKarl Rupp       if (nidx) ++s;
1646af538404SDmitry Karpeev       xstart += maxwidth;
1647af538404SDmitry Karpeev     } /* for (i = 0; i < Mdomains; ++i) */
1648af538404SDmitry Karpeev     ystart += maxheight;
1649af538404SDmitry Karpeev   } /* for (j = 0; j < Ndomains; ++j) */
16502fa5cd67SKarl Rupp 
1651af538404SDmitry Karpeev   /* Now we can allocate the necessary number of ISs. */
1652af538404SDmitry Karpeev   *nsub = s;
16539566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(*nsub, is));
16549566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(*nsub, is_local));
1655af538404SDmitry Karpeev   s      = 0;
1656af538404SDmitry Karpeev   ystart = 0;
1657af538404SDmitry Karpeev   for (j = 0; j < Ndomains; ++j) {
1658af538404SDmitry Karpeev     maxheight = N / Ndomains + ((N % Ndomains) > j); /* Maximal height of subdomain */
165963a3b9bcSJacob Faibussowitsch     PetscCheck(maxheight >= 2, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many %" PetscInt_FMT " subdomains in the vertical directon for mesh height %" PetscInt_FMT, Ndomains, N);
1660af538404SDmitry Karpeev     /* Vertical domain limits with an overlap. */
1661eec7e3faSDmitry Karpeev     y[0][0] = PetscMax(ystart - overlap, 0);
1662eec7e3faSDmitry Karpeev     y[0][1] = PetscMin(ystart + maxheight + overlap, N);
1663eec7e3faSDmitry Karpeev     /* Vertical domain limits without an overlap. */
1664eec7e3faSDmitry Karpeev     y[1][0] = ystart;
1665eec7e3faSDmitry Karpeev     y[1][1] = PetscMin(ystart + maxheight, N);
1666eec7e3faSDmitry Karpeev     xstart  = 0;
1667af538404SDmitry Karpeev     for (i = 0; i < Mdomains; ++i) {
1668af538404SDmitry Karpeev       maxwidth = M / Mdomains + ((M % Mdomains) > i); /* Maximal width of subdomain */
166963a3b9bcSJacob Faibussowitsch       PetscCheck(maxwidth >= 2, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many %" PetscInt_FMT " subdomains in the horizontal direction for mesh width %" PetscInt_FMT, Mdomains, M);
1670af538404SDmitry Karpeev       /* Horizontal domain limits with an overlap. */
1671eec7e3faSDmitry Karpeev       x[0][0] = PetscMax(xstart - overlap, 0);
1672eec7e3faSDmitry Karpeev       x[0][1] = PetscMin(xstart + maxwidth + overlap, M);
1673eec7e3faSDmitry Karpeev       /* Horizontal domain limits without an overlap. */
1674eec7e3faSDmitry Karpeev       x[1][0] = xstart;
1675eec7e3faSDmitry Karpeev       x[1][1] = PetscMin(xstart + maxwidth, M);
16762bbb417fSDmitry Karpeev       /*
1677f1580f4eSBarry Smith          Determine whether this domain intersects this rank's ownership range of pc->pmat.
16782bbb417fSDmitry Karpeev          Do this twice: first for the domains with overlaps, and once without.
16792bbb417fSDmitry Karpeev          During the first pass create the subcommunicators, and use them on the second pass as well.
16802bbb417fSDmitry Karpeev       */
16812bbb417fSDmitry Karpeev       for (q = 0; q < 2; ++q) {
1682cc96b2e5SBarry Smith         PetscBool split = PETSC_FALSE;
16832bbb417fSDmitry Karpeev         /*
16842bbb417fSDmitry Karpeev           domain limits, (xleft, xright) and (ylow, yheigh) are adjusted
16852bbb417fSDmitry Karpeev           according to whether the domain with an overlap or without is considered.
16862bbb417fSDmitry Karpeev         */
16879371c9d4SSatish Balay         xleft  = x[q][0];
16889371c9d4SSatish Balay         xright = x[q][1];
16899371c9d4SSatish Balay         ylow   = y[q][0];
16909371c9d4SSatish Balay         yhigh  = y[q][1];
1691c3518bceSDmitry Karpeev         PCGASMLocalSubdomainBounds2D(M, N, xleft, ylow, xright, yhigh, first, last, (&xleft_loc), (&ylow_loc), (&xright_loc), (&yhigh_loc), (&nidx));
1692af538404SDmitry Karpeev         nidx *= dof;
1693eec7e3faSDmitry Karpeev         n[q] = nidx;
16942bbb417fSDmitry Karpeev         /*
1695eec7e3faSDmitry Karpeev          Based on the counted number of indices in the local domain *with an overlap*,
1696f1580f4eSBarry Smith          construct a subcommunicator of all the MPI ranks supporting this domain.
1697eec7e3faSDmitry Karpeev          Observe that a domain with an overlap might have nontrivial local support,
1698eec7e3faSDmitry Karpeev          while the domain without an overlap might not.  Hence, the decision to participate
1699eec7e3faSDmitry Karpeev          in the subcommunicator must be based on the domain with an overlap.
17002bbb417fSDmitry Karpeev          */
17012bbb417fSDmitry Karpeev         if (q == 0) {
17022fa5cd67SKarl Rupp           if (nidx) color = 1;
17032fa5cd67SKarl Rupp           else color = MPI_UNDEFINED;
17049566063dSJacob Faibussowitsch           PetscCallMPI(MPI_Comm_split(comm, color, rank, &subcomm));
1705cc96b2e5SBarry Smith           split = PETSC_TRUE;
17062bbb417fSDmitry Karpeev         }
1707af538404SDmitry Karpeev         /*
1708eec7e3faSDmitry Karpeev          Proceed only if the number of local indices *with an overlap* is nonzero.
1709af538404SDmitry Karpeev          */
1710eec7e3faSDmitry Karpeev         if (n[0]) {
17112fa5cd67SKarl Rupp           if (q == 0) xis = is;
1712af538404SDmitry Karpeev           if (q == 1) {
1713af538404SDmitry Karpeev             /*
1714eec7e3faSDmitry Karpeev              The IS for the no-overlap subdomain shares a communicator with the overlapping domain.
1715af538404SDmitry Karpeev              Moreover, if the overlap is zero, the two ISs are identical.
1716af538404SDmitry Karpeev              */
1717b1a0a8a3SJed Brown             if (overlap == 0) {
1718eec7e3faSDmitry Karpeev               (*is_local)[s] = (*is)[s];
17199566063dSJacob Faibussowitsch               PetscCall(PetscObjectReference((PetscObject)(*is)[s]));
17202bbb417fSDmitry Karpeev               continue;
1721d34fcf5fSBarry Smith             } else {
17226a4f0f73SDmitry Karpeev               xis     = is_local;
1723eec7e3faSDmitry Karpeev               subcomm = ((PetscObject)(*is)[s])->comm;
17242bbb417fSDmitry Karpeev             }
1725af538404SDmitry Karpeev           } /* if (q == 1) */
17260298fd71SBarry Smith           idx = NULL;
17279566063dSJacob Faibussowitsch           PetscCall(PetscMalloc1(nidx, &idx));
1728eec7e3faSDmitry Karpeev           if (nidx) {
17292bbb417fSDmitry Karpeev             k = 0;
17302bbb417fSDmitry Karpeev             for (jj = ylow_loc; jj < yhigh_loc; ++jj) {
1731af538404SDmitry Karpeev               PetscInt x0 = (jj == ylow_loc) ? xleft_loc : xleft;
1732af538404SDmitry Karpeev               PetscInt x1 = (jj == yhigh_loc - 1) ? xright_loc : xright;
1733af538404SDmitry Karpeev               kk          = dof * (M * jj + x0);
17342bbb417fSDmitry Karpeev               for (ii = x0; ii < x1; ++ii) {
1735ad540459SPierre Jolivet                 for (d = 0; d < dof; ++d) idx[k++] = kk++;
1736b1a0a8a3SJed Brown               }
1737b1a0a8a3SJed Brown             }
1738eec7e3faSDmitry Karpeev           }
17399566063dSJacob Faibussowitsch           PetscCall(ISCreateGeneral(subcomm, nidx, idx, PETSC_OWN_POINTER, (*xis) + s));
174048a46eb9SPierre Jolivet           if (split) PetscCallMPI(MPI_Comm_free(&subcomm));
1741eec7e3faSDmitry Karpeev         } /* if (n[0]) */
17422bbb417fSDmitry Karpeev       }   /* for (q = 0; q < 2; ++q) */
17432fa5cd67SKarl Rupp       if (n[0]) ++s;
1744af538404SDmitry Karpeev       xstart += maxwidth;
1745af538404SDmitry Karpeev     } /* for (i = 0; i < Mdomains; ++i) */
17462bbb417fSDmitry Karpeev     ystart += maxheight;
1747af538404SDmitry Karpeev   } /* for (j = 0; j < Ndomains; ++j) */
1748b1a0a8a3SJed Brown   PetscFunctionReturn(0);
1749b1a0a8a3SJed Brown }
1750b1a0a8a3SJed Brown 
1751b1a0a8a3SJed Brown /*@C
1752f1580f4eSBarry Smith     PCGASMGetSubdomains - Gets the subdomains supported on this MPI rank
1753f1580f4eSBarry Smith     for the `PCGASM` additive Schwarz preconditioner.
1754b1a0a8a3SJed Brown 
1755b1a0a8a3SJed Brown     Not Collective
1756b1a0a8a3SJed Brown 
1757b1a0a8a3SJed Brown     Input Parameter:
1758b1a0a8a3SJed Brown .   pc - the preconditioner context
1759b1a0a8a3SJed Brown 
1760b1a0a8a3SJed Brown     Output Parameters:
1761f1580f4eSBarry Smith +   n   - the number of subdomains for this MPI rank (default value = 1)
1762f1580f4eSBarry Smith .   iis - the index sets that define the inner subdomains (without overlap) supported on this rank (can be NULL)
1763f1580f4eSBarry Smith -   ois - the index sets that define the outer subdomains (with overlap) supported on this rank (can be NULL)
1764b1a0a8a3SJed Brown 
1765f1580f4eSBarry Smith     Note:
1766f1580f4eSBarry Smith     The user is responsible for destroying the `IS`s and freeing the returned arrays.
1767f1580f4eSBarry Smith     The `IS` numbering is in the parallel, global numbering of the vector.
1768b1a0a8a3SJed Brown 
1769b1a0a8a3SJed Brown     Level: advanced
1770b1a0a8a3SJed Brown 
1771f1580f4eSBarry Smith .seealso: `PCGASM`, `PCGASMSetOverlap()`, `PCGASMGetSubKSP()`, `PCGASMCreateSubdomains2D()`,
1772db781477SPatrick Sanan           `PCGASMSetSubdomains()`, `PCGASMGetSubmatrices()`
1773b1a0a8a3SJed Brown @*/
1774*d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGASMGetSubdomains(PC pc, PetscInt *n, IS *iis[], IS *ois[])
1775*d71ae5a4SJacob Faibussowitsch {
1776f746d493SDmitry Karpeev   PC_GASM  *osm;
1777b1a0a8a3SJed Brown   PetscBool match;
17786a4f0f73SDmitry Karpeev   PetscInt  i;
17795fd66863SKarl Rupp 
1780b1a0a8a3SJed Brown   PetscFunctionBegin;
1781b1a0a8a3SJed Brown   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
17829566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCGASM, &match));
178328b400f6SJacob Faibussowitsch   PetscCheck(match, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONG, "Incorrect object type: expected %s, got %s instead", PCGASM, ((PetscObject)pc)->type_name);
1784f746d493SDmitry Karpeev   osm = (PC_GASM *)pc->data;
1785ab3e923fSDmitry Karpeev   if (n) *n = osm->n;
17861baa6e33SBarry Smith   if (iis) PetscCall(PetscMalloc1(osm->n, iis));
17871baa6e33SBarry Smith   if (ois) PetscCall(PetscMalloc1(osm->n, ois));
17886a4f0f73SDmitry Karpeev   if (iis || ois) {
17896a4f0f73SDmitry Karpeev     for (i = 0; i < osm->n; ++i) {
17906a4f0f73SDmitry Karpeev       if (iis) (*iis)[i] = osm->iis[i];
17916a4f0f73SDmitry Karpeev       if (ois) (*ois)[i] = osm->ois[i];
17926a4f0f73SDmitry Karpeev     }
1793b1a0a8a3SJed Brown   }
1794b1a0a8a3SJed Brown   PetscFunctionReturn(0);
1795b1a0a8a3SJed Brown }
1796b1a0a8a3SJed Brown 
1797b1a0a8a3SJed Brown /*@C
1798f1580f4eSBarry Smith     PCGASMGetSubmatrices - Gets the local submatrices (for this MPI rank
1799f1580f4eSBarry Smith     only) for the `PCGASM` additive Schwarz preconditioner.
1800b1a0a8a3SJed Brown 
1801b1a0a8a3SJed Brown     Not Collective
1802b1a0a8a3SJed Brown 
1803b1a0a8a3SJed Brown     Input Parameter:
1804b1a0a8a3SJed Brown .   pc - the preconditioner context
1805b1a0a8a3SJed Brown 
1806b1a0a8a3SJed Brown     Output Parameters:
1807f1580f4eSBarry Smith +   n   - the number of matrices for this MPI rank (default value = 1)
1808b1a0a8a3SJed Brown -   mat - the matrices
1809b1a0a8a3SJed Brown 
1810f1580f4eSBarry Smith     Note:
1811f1580f4eSBarry Smith     Matrices returned by this routine have the same communicators as the index sets (IS)
1812f1580f4eSBarry Smith            used to define subdomains in `PCGASMSetSubdomains()`
1813f1580f4eSBarry Smith 
1814b1a0a8a3SJed Brown     Level: advanced
1815b1a0a8a3SJed Brown 
1816f1580f4eSBarry Smith .seealso: `PCGASM`, `PCGASMSetOverlap()`, `PCGASMGetSubKSP()`,
1817db781477SPatrick Sanan           `PCGASMCreateSubdomains2D()`, `PCGASMSetSubdomains()`, `PCGASMGetSubdomains()`
1818b1a0a8a3SJed Brown @*/
1819*d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGASMGetSubmatrices(PC pc, PetscInt *n, Mat *mat[])
1820*d71ae5a4SJacob Faibussowitsch {
1821f746d493SDmitry Karpeev   PC_GASM  *osm;
1822b1a0a8a3SJed Brown   PetscBool match;
1823b1a0a8a3SJed Brown 
1824b1a0a8a3SJed Brown   PetscFunctionBegin;
1825b1a0a8a3SJed Brown   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1826b1a0a8a3SJed Brown   PetscValidIntPointer(n, 2);
1827b1a0a8a3SJed Brown   if (mat) PetscValidPointer(mat, 3);
182828b400f6SJacob Faibussowitsch   PetscCheck(pc->setupcalled, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Must call after KSPSetUp() or PCSetUp().");
18299566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCGASM, &match));
183028b400f6SJacob Faibussowitsch   PetscCheck(match, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONG, "Expected %s, got %s instead", PCGASM, ((PetscObject)pc)->type_name);
1831f746d493SDmitry Karpeev   osm = (PC_GASM *)pc->data;
1832ab3e923fSDmitry Karpeev   if (n) *n = osm->n;
1833b1a0a8a3SJed Brown   if (mat) *mat = osm->pmat;
1834b1a0a8a3SJed Brown   PetscFunctionReturn(0);
1835b1a0a8a3SJed Brown }
1836d709ea83SDmitry Karpeev 
1837d709ea83SDmitry Karpeev /*@
1838f1580f4eSBarry Smith     PCGASMSetUseDMSubdomains - Indicates whether to use `DMCreateDomainDecomposition()` to define the subdomains, whenever possible for `PCGASM`
1839f1580f4eSBarry Smith 
1840d709ea83SDmitry Karpeev     Logically Collective
1841d709ea83SDmitry Karpeev 
1842d8d19677SJose E. Roman     Input Parameters:
1843d709ea83SDmitry Karpeev +   pc  - the preconditioner
1844f1580f4eSBarry Smith -   flg - boolean indicating whether to use subdomains defined by the `DM`
1845d709ea83SDmitry Karpeev 
1846d709ea83SDmitry Karpeev     Options Database Key:
18478f3b4b4dSDmitry Karpeev .   -pc_gasm_dm_subdomains -pc_gasm_overlap -pc_gasm_total_subdomains
1848d709ea83SDmitry Karpeev 
1849d709ea83SDmitry Karpeev     Level: intermediate
1850d709ea83SDmitry Karpeev 
1851f1580f4eSBarry Smith     Note:
1852f1580f4eSBarry Smith     `PCGASMSetSubdomains()`, `PCGASMSetTotalSubdomains()` or `PCGASMSetOverlap()` take precedence over `PCGASMSetUseDMSubdomains()`,
1853f1580f4eSBarry Smith     so setting `PCGASMSetSubdomains()` with nontrivial subdomain ISs or any of `PCGASMSetTotalSubdomains()` and `PCGASMSetOverlap()`
18548f3b4b4dSDmitry Karpeev     automatically turns the latter off.
1855d709ea83SDmitry Karpeev 
1856f1580f4eSBarry Smith .seealso: `PCGASM`, `PCGASMGetUseDMSubdomains()`, `PCGASMSetSubdomains()`, `PCGASMSetOverlap()`
1857db781477SPatrick Sanan           `PCGASMCreateSubdomains2D()`
1858d709ea83SDmitry Karpeev @*/
1859*d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGASMSetUseDMSubdomains(PC pc, PetscBool flg)
1860*d71ae5a4SJacob Faibussowitsch {
1861d709ea83SDmitry Karpeev   PC_GASM  *osm = (PC_GASM *)pc->data;
1862d709ea83SDmitry Karpeev   PetscBool match;
1863d709ea83SDmitry Karpeev 
1864d709ea83SDmitry Karpeev   PetscFunctionBegin;
1865d709ea83SDmitry Karpeev   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1866d709ea83SDmitry Karpeev   PetscValidLogicalCollectiveBool(pc, flg, 2);
186728b400f6SJacob Faibussowitsch   PetscCheck(!pc->setupcalled, ((PetscObject)pc)->comm, PETSC_ERR_ARG_WRONGSTATE, "Not for a setup PC.");
18689566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCGASM, &match));
1869d709ea83SDmitry Karpeev   if (match) {
1870ad540459SPierre Jolivet     if (!osm->user_subdomains && osm->N == PETSC_DETERMINE && osm->overlap < 0) osm->dm_subdomains = flg;
18718f3b4b4dSDmitry Karpeev   }
1872d709ea83SDmitry Karpeev   PetscFunctionReturn(0);
1873d709ea83SDmitry Karpeev }
1874d709ea83SDmitry Karpeev 
1875d709ea83SDmitry Karpeev /*@
1876f1580f4eSBarry Smith     PCGASMGetUseDMSubdomains - Returns flag indicating whether to use `DMCreateDomainDecomposition()` to define the subdomains, whenever possible with `PCGASM`
1877f1580f4eSBarry Smith 
1878d709ea83SDmitry Karpeev     Not Collective
1879d709ea83SDmitry Karpeev 
1880d709ea83SDmitry Karpeev     Input Parameter:
1881d709ea83SDmitry Karpeev .   pc  - the preconditioner
1882d709ea83SDmitry Karpeev 
1883d709ea83SDmitry Karpeev     Output Parameter:
1884f1580f4eSBarry Smith .   flg - boolean indicating whether to use subdomains defined by the `DM`
1885d709ea83SDmitry Karpeev 
1886d709ea83SDmitry Karpeev     Level: intermediate
1887d709ea83SDmitry Karpeev 
1888f1580f4eSBarry Smith .seealso: `PCGASM`, `PCGASMSetUseDMSubdomains()`, `PCGASMSetOverlap()`
1889db781477SPatrick Sanan           `PCGASMCreateSubdomains2D()`
1890d709ea83SDmitry Karpeev @*/
1891*d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGASMGetUseDMSubdomains(PC pc, PetscBool *flg)
1892*d71ae5a4SJacob Faibussowitsch {
1893d709ea83SDmitry Karpeev   PC_GASM  *osm = (PC_GASM *)pc->data;
1894d709ea83SDmitry Karpeev   PetscBool match;
1895d709ea83SDmitry Karpeev 
1896d709ea83SDmitry Karpeev   PetscFunctionBegin;
1897d709ea83SDmitry Karpeev   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1898534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
18999566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCGASM, &match));
1900d709ea83SDmitry Karpeev   if (match) {
1901d709ea83SDmitry Karpeev     if (flg) *flg = osm->dm_subdomains;
1902d709ea83SDmitry Karpeev   }
1903d709ea83SDmitry Karpeev   PetscFunctionReturn(0);
1904d709ea83SDmitry Karpeev }
1905