xref: /petsc/src/ksp/pc/impls/gasm/gasm.c (revision eaa7cf455101e7391ead982166c6e99faf273403)
1b1a0a8a3SJed Brown /*
2f746d493SDmitry Karpeev   This file defines an "generalized" additive Schwarz preconditioner for any Mat implementation.
3f13dfd9eSBarry Smith   In this version, each MPI process may intersect multiple subdomains and any subdomain may
4f13dfd9eSBarry Smith   intersect multiple MPI processes.  Intersections of subdomains with MPI processes 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())
8f13dfd9eSBarry Smith        n    - actual number of local subdomains on this process (set in `PCGASMSetSubdomains()` or calculated in `PCGASMSetTotalSubdomains()`)
9f13dfd9eSBarry Smith        nmax - maximum number of local subdomains per process (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 
PCGASMComputeGlobalSubdomainNumbering_Private(PC pc,PetscInt ** numbering,PetscInt ** permutation)39d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGASMComputeGlobalSubdomainNumbering_Private(PC pc, PetscInt **numbering, PetscInt **permutation)
40d71ae5a4SJacob 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));
503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
518f3b4b4dSDmitry Karpeev }
528f3b4b4dSDmitry Karpeev 
PCGASMSubdomainView_Private(PC pc,PetscInt i,PetscViewer viewer)53d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGASMSubdomainView_Private(PC pc, PetscInt i, PetscViewer viewer)
54d71ae5a4SJacob 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;
62fe8fb074SBarry Smith   PetscCheck(i >= -1 && i < osm->n, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_WRONG, "Invalid subdomain %" PetscInt_FMT ": must nonnegative and less than %" PetscInt_FMT, i, osm->n);
63fe8fb074SBarry Smith 
644bde246dSDmitry Karpeev   /* Inner subdomains. */
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    */
71fe8fb074SBarry Smith   PetscCall(PetscViewerASCIIPrintf(viewer, "Inner subdomain:\n"));
72fe8fb074SBarry Smith   PetscCall(PetscViewerFlush(viewer));
73fe8fb074SBarry Smith   PetscCall(PetscViewerASCIIPushSynchronized(viewer));
74fe8fb074SBarry Smith   if (i > -1) {
75fe8fb074SBarry Smith     PetscCall(ISGetLocalSize(osm->iis[i], &nidx));
76fe8fb074SBarry Smith     PetscCall(PetscMalloc1(16 * (nidx + 1) + 1, &cidx));
77fe8fb074SBarry Smith     PetscCall(PetscViewerStringOpen(PETSC_COMM_SELF, cidx, 16 * (nidx + 1) + 1, &sviewer));
789566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(osm->iis[i], &idx));
7948a46eb9SPierre Jolivet     for (j = 0; j < nidx; ++j) PetscCall(PetscViewerStringSPrintf(sviewer, "%" PetscInt_FMT " ", idx[j]));
809566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(osm->iis[i], &idx));
819566063dSJacob Faibussowitsch     PetscCall(PetscViewerDestroy(&sviewer));
829566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%s", cidx));
83fe8fb074SBarry Smith     PetscCall(PetscFree(cidx));
84fe8fb074SBarry Smith   }
859566063dSJacob Faibussowitsch   PetscCall(PetscViewerFlush(viewer));
869566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPopSynchronized(viewer));
879566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
889566063dSJacob Faibussowitsch   PetscCall(PetscViewerFlush(viewer));
89fe8fb074SBarry Smith 
904bde246dSDmitry Karpeev   /* Outer subdomains. */
91eec7e3faSDmitry Karpeev   /*
92eec7e3faSDmitry Karpeev    No more than 15 characters per index plus a space.
93eec7e3faSDmitry Karpeev    PetscViewerStringSPrintf requires a string of size at least 2, so use (nidx+1) instead of nidx,
94eec7e3faSDmitry Karpeev    in case nidx == 0. That will take care of the space for the trailing '\0' as well.
95eec7e3faSDmitry Karpeev    For nidx == 0, the whole string 16 '\0'.
96eec7e3faSDmitry Karpeev    */
97fe8fb074SBarry Smith   PetscCall(PetscViewerASCIIPrintf(viewer, "Outer subdomain:\n"));
98fe8fb074SBarry Smith   PetscCall(PetscViewerFlush(viewer));
99fe8fb074SBarry Smith   PetscCall(PetscViewerASCIIPushSynchronized(viewer));
100fe8fb074SBarry Smith   if (i > -1) {
101fe8fb074SBarry Smith     PetscCall(ISGetLocalSize(osm->ois[i], &nidx));
102fe8fb074SBarry Smith     PetscCall(PetscMalloc1(16 * (nidx + 1) + 1, &cidx));
103fe8fb074SBarry Smith     PetscCall(PetscViewerStringOpen(PETSC_COMM_SELF, cidx, 16 * (nidx + 1) + 1, &sviewer));
1049566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(osm->ois[i], &idx));
10548a46eb9SPierre Jolivet     for (j = 0; j < nidx; ++j) PetscCall(PetscViewerStringSPrintf(sviewer, "%" PetscInt_FMT " ", idx[j]));
1069566063dSJacob Faibussowitsch     PetscCall(PetscViewerDestroy(&sviewer));
1079566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(osm->ois[i], &idx));
1089566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%s", cidx));
109fe8fb074SBarry Smith     PetscCall(PetscFree(cidx));
110fe8fb074SBarry Smith   }
1119566063dSJacob Faibussowitsch   PetscCall(PetscViewerFlush(viewer));
1129566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPopSynchronized(viewer));
1139566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
1149566063dSJacob Faibussowitsch   PetscCall(PetscViewerFlush(viewer));
1153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11606b43e7eSDmitry Karpeev }
11706b43e7eSDmitry Karpeev 
PCGASMPrintSubdomains(PC pc)118d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGASMPrintSubdomains(PC pc)
119d71ae5a4SJacob Faibussowitsch {
12006b43e7eSDmitry Karpeev   PC_GASM    *osm = (PC_GASM *)pc->data;
12106b43e7eSDmitry Karpeev   const char *prefix;
12206b43e7eSDmitry Karpeev   char        fname[PETSC_MAX_PATH_LEN + 1];
1238f3b4b4dSDmitry Karpeev   PetscInt    l, d, count;
1249140fbc5SPierre Jolivet   PetscBool   found;
125fe8fb074SBarry Smith   PetscViewer viewer;
1268f3b4b4dSDmitry Karpeev   PetscInt   *numbering, *permutation; /* global numbering of locally-supported subdomains and the permutation from the local ordering */
12706b43e7eSDmitry Karpeev 
12806b43e7eSDmitry Karpeev   PetscFunctionBegin;
1299566063dSJacob Faibussowitsch   PetscCall(PCGetOptionsPrefix(pc, &prefix));
1309566063dSJacob Faibussowitsch   PetscCall(PetscOptionsHasName(NULL, prefix, "-pc_gasm_print_subdomains", &found));
1313ba16761SJacob Faibussowitsch   if (!found) PetscFunctionReturn(PETSC_SUCCESS);
1329566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetString(NULL, prefix, "-pc_gasm_print_subdomains", fname, sizeof(fname), &found));
133c6a7a370SJeremy L Thompson   if (!found) PetscCall(PetscStrncpy(fname, "stdout", sizeof(fname)));
1349566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIOpen(PetscObjectComm((PetscObject)pc), fname, &viewer));
1354bde246dSDmitry Karpeev   /*
1364bde246dSDmitry Karpeev    Make sure the viewer has a name. Otherwise this may cause a deadlock or other weird errors when creating a subcomm viewer:
1374bde246dSDmitry Karpeev    the subcomm viewer will attempt to inherit the viewer's name, which, if not set, will be constructed collectively on the comm.
1384bde246dSDmitry Karpeev   */
1399566063dSJacob Faibussowitsch   PetscCall(PetscObjectName((PetscObject)viewer));
1404bde246dSDmitry Karpeev   l = 0;
1419566063dSJacob Faibussowitsch   PetscCall(PCGASMComputeGlobalSubdomainNumbering_Private(pc, &numbering, &permutation));
1428f3b4b4dSDmitry Karpeev   for (count = 0; count < osm->N; ++count) {
1434bde246dSDmitry Karpeev     /* Now let subdomains go one at a time in the global numbering order and print their subdomain/solver info. */
1444bde246dSDmitry Karpeev     if (l < osm->n) {
1454bde246dSDmitry Karpeev       d = permutation[l]; /* d is the local number of the l-th smallest (in the global ordering) among the locally supported subdomains */
146fe8fb074SBarry Smith       if (numbering[d] == count) l++;
147fe8fb074SBarry Smith       else d = -1;
148fe8fb074SBarry Smith     } else d = -1;
149fe8fb074SBarry Smith     PetscCall(PCGASMSubdomainView_Private(pc, d, viewer));
1504bde246dSDmitry Karpeev   }
1519566063dSJacob Faibussowitsch   PetscCall(PetscFree2(numbering, permutation));
1529566063dSJacob Faibussowitsch   PetscCall(PetscViewerDestroy(&viewer));
1533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
154af538404SDmitry Karpeev }
155af538404SDmitry Karpeev 
PCView_GASM(PC pc,PetscViewer viewer)156d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCView_GASM(PC pc, PetscViewer viewer)
157d71ae5a4SJacob Faibussowitsch {
158f746d493SDmitry Karpeev   PC_GASM    *osm = (PC_GASM *)pc->data;
159af538404SDmitry Karpeev   const char *prefix;
160af538404SDmitry Karpeev   PetscMPIInt rank, size;
1618f3b4b4dSDmitry Karpeev   PetscInt    bsz;
1629f196a02SMartin Diehl   PetscBool   isascii, view_subdomains = PETSC_FALSE;
163b1a0a8a3SJed Brown   PetscViewer sviewer;
1648f3b4b4dSDmitry Karpeev   PetscInt    count, l;
1656a4f0f73SDmitry Karpeev   char        overlap[256]     = "user-defined overlap";
1666a4f0f73SDmitry Karpeev   char        gsubdomains[256] = "unknown total number of subdomains";
16706b43e7eSDmitry Karpeev   char        msubdomains[256] = "unknown max number of local subdomains";
1688f3b4b4dSDmitry Karpeev   PetscInt   *numbering, *permutation; /* global numbering of locally-supported subdomains and the permutation from the local ordering */
16911aeaf0aSBarry Smith 
170b1a0a8a3SJed Brown   PetscFunctionBegin;
1719566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size));
1729566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)pc), &rank));
17306b43e7eSDmitry Karpeev 
17448a46eb9SPierre Jolivet   if (osm->overlap >= 0) PetscCall(PetscSNPrintf(overlap, sizeof(overlap), "requested amount of overlap = %" PetscInt_FMT, osm->overlap));
17548a46eb9SPierre Jolivet   if (osm->N != PETSC_DETERMINE) PetscCall(PetscSNPrintf(gsubdomains, sizeof(gsubdomains), "total number of subdomains = %" PetscInt_FMT, osm->N));
17648a46eb9SPierre Jolivet   if (osm->nmax != PETSC_DETERMINE) PetscCall(PetscSNPrintf(msubdomains, sizeof(msubdomains), "max number of local subdomains = %" PetscInt_FMT, osm->nmax));
17706b43e7eSDmitry Karpeev 
1789566063dSJacob Faibussowitsch   PetscCall(PCGetOptionsPrefix(pc, &prefix));
1799566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(NULL, prefix, "-pc_gasm_view_subdomains", &view_subdomains, NULL));
18006b43e7eSDmitry Karpeev 
1819f196a02SMartin Diehl   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
1829f196a02SMartin Diehl   if (isascii) {
18306b43e7eSDmitry Karpeev     /*
18406b43e7eSDmitry Karpeev      Make sure the viewer has a name. Otherwise this may cause a deadlock when creating a subcomm viewer:
18506b43e7eSDmitry Karpeev      the subcomm viewer will attempt to inherit the viewer's name, which, if not set, will be constructed
18606b43e7eSDmitry Karpeev      collectively on the comm.
18706b43e7eSDmitry Karpeev      */
1889566063dSJacob Faibussowitsch     PetscCall(PetscObjectName((PetscObject)viewer));
1899566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "  Restriction/interpolation type: %s\n", PCGASMTypes[osm->type]));
1909566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "  %s\n", overlap));
1919566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "  %s\n", gsubdomains));
1929566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "  %s\n", msubdomains));
1939566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPushSynchronized(viewer));
19463a3b9bcSJacob Faibussowitsch     PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "  [%d|%d] number of locally-supported subdomains = %" PetscInt_FMT "\n", rank, size, osm->n));
1959566063dSJacob Faibussowitsch     PetscCall(PetscViewerFlush(viewer));
1969566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPopSynchronized(viewer));
1976a4f0f73SDmitry Karpeev     /* Cannot take advantage of osm->same_subdomain_solvers without a global numbering of subdomains. */
198*ecf3d421SBarry Smith     PetscCall(PetscViewerASCIIPrintf(viewer, "  Subdomain solver info:\n"));
1999566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPushTab(viewer));
2009566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "  - - - - - - - - - - - - - - - - - -\n"));
2014bde246dSDmitry Karpeev     /* Now let subdomains go one at a time in the global numbering order and print their subdomain/solver info. */
2029566063dSJacob Faibussowitsch     PetscCall(PCGASMComputeGlobalSubdomainNumbering_Private(pc, &numbering, &permutation));
20306b43e7eSDmitry Karpeev     l = 0;
2048f3b4b4dSDmitry Karpeev     for (count = 0; count < osm->N; ++count) {
20506b43e7eSDmitry Karpeev       PetscMPIInt srank, ssize;
20606b43e7eSDmitry Karpeev       if (l < osm->n) {
20706b43e7eSDmitry Karpeev         PetscInt d = permutation[l]; /* d is the local number of the l-th smallest (in the global ordering) among the locally supported subdomains */
20806b43e7eSDmitry Karpeev         if (numbering[d] == count) {
2099566063dSJacob Faibussowitsch           PetscCallMPI(MPI_Comm_size(((PetscObject)osm->ois[d])->comm, &ssize));
2109566063dSJacob Faibussowitsch           PetscCallMPI(MPI_Comm_rank(((PetscObject)osm->ois[d])->comm, &srank));
2119566063dSJacob Faibussowitsch           PetscCall(PetscViewerGetSubViewer(viewer, ((PetscObject)osm->ois[d])->comm, &sviewer));
2129566063dSJacob Faibussowitsch           PetscCall(ISGetLocalSize(osm->ois[d], &bsz));
21363a3b9bcSJacob 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));
2149566063dSJacob Faibussowitsch           PetscCall(PetscViewerFlush(sviewer));
215b4025f61SBarry Smith           PetscCall(PetscViewerASCIIPushTab(sviewer));
2161baa6e33SBarry Smith           if (view_subdomains) PetscCall(PCGASMSubdomainView_Private(pc, d, sviewer));
2176a4f0f73SDmitry Karpeev           if (!pc->setupcalled) {
218b4025f61SBarry Smith             PetscCall(PetscViewerASCIISynchronizedPrintf(sviewer, "  Solver not set up yet: PCSetUp() not yet called\n"));
2192fa5cd67SKarl Rupp           } else {
2209566063dSJacob Faibussowitsch             PetscCall(KSPView(osm->ksp[d], sviewer));
2216a4f0f73SDmitry Karpeev           }
222b4025f61SBarry Smith           PetscCall(PetscViewerASCIIPopTab(sviewer));
2239566063dSJacob Faibussowitsch           PetscCall(PetscViewerASCIIPrintf(sviewer, "  - - - - - - - - - - - - - - - - - -\n"));
2249566063dSJacob Faibussowitsch           PetscCall(PetscViewerFlush(sviewer));
2259566063dSJacob Faibussowitsch           PetscCall(PetscViewerRestoreSubViewer(viewer, ((PetscObject)osm->ois[d])->comm, &sviewer));
22606b43e7eSDmitry Karpeev           ++l;
227b4025f61SBarry Smith         } else {
228b4025f61SBarry Smith           PetscCall(PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
229b4025f61SBarry Smith           PetscCall(PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
230b1a0a8a3SJed Brown         }
231b4025f61SBarry Smith       } else {
232b4025f61SBarry Smith         PetscCall(PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
233b4025f61SBarry Smith         PetscCall(PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
23406b43e7eSDmitry Karpeev       }
235b1a0a8a3SJed Brown     }
2369566063dSJacob Faibussowitsch     PetscCall(PetscFree2(numbering, permutation));
2379566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPopTab(viewer));
2389566063dSJacob Faibussowitsch     PetscCall(PetscViewerFlush(viewer));
2399530cbd7SBarry Smith     /* this line is needed to match the extra PetscViewerASCIIPushSynchronized() in PetscViewerGetSubViewer() */
2409566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPopSynchronized(viewer));
2419530cbd7SBarry Smith   }
2423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
243b1a0a8a3SJed Brown }
244b1a0a8a3SJed Brown 
2458f3b4b4dSDmitry Karpeev PETSC_INTERN PetscErrorCode PCGASMCreateLocalSubdomains(Mat A, PetscInt nloc, IS *iis[]);
246af538404SDmitry Karpeev 
PCGASMSetHierarchicalPartitioning(PC pc)24766976f2fSJacob Faibussowitsch static PetscErrorCode PCGASMSetHierarchicalPartitioning(PC pc)
248d71ae5a4SJacob Faibussowitsch {
249ea91fabdSFande Kong   PC_GASM        *osm = (PC_GASM *)pc->data;
250ea91fabdSFande Kong   MatPartitioning part;
251ea91fabdSFande Kong   MPI_Comm        comm;
252ea91fabdSFande Kong   PetscMPIInt     size;
253ea91fabdSFande Kong   PetscInt        nlocalsubdomains, fromrows_localsize;
254ea91fabdSFande Kong   IS              partitioning, fromrows, isn;
255ea91fabdSFande Kong   Vec             outervec;
256ea91fabdSFande Kong 
257ea91fabdSFande Kong   PetscFunctionBegin;
2589566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)pc, &comm));
2599566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
260ea91fabdSFande Kong   /* we do not need a hierarchical partitioning when
261ea91fabdSFande Kong     * the total number of subdomains is consistent with
262ea91fabdSFande Kong     * the number of MPI tasks.
263ea91fabdSFande Kong     * For the following cases, we do not need to use HP
264ea91fabdSFande Kong     * */
2653ba16761SJacob Faibussowitsch   if (osm->N == PETSC_DETERMINE || osm->N >= size || osm->N == 1) PetscFunctionReturn(PETSC_SUCCESS);
266f1580f4eSBarry 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);
267ea91fabdSFande Kong   nlocalsubdomains = size / osm->N;
268ea91fabdSFande Kong   osm->n           = 1;
2699566063dSJacob Faibussowitsch   PetscCall(MatPartitioningCreate(comm, &part));
2709566063dSJacob Faibussowitsch   PetscCall(MatPartitioningSetAdjacency(part, pc->pmat));
2719566063dSJacob Faibussowitsch   PetscCall(MatPartitioningSetType(part, MATPARTITIONINGHIERARCH));
2729566063dSJacob Faibussowitsch   PetscCall(MatPartitioningHierarchicalSetNcoarseparts(part, osm->N));
2739566063dSJacob Faibussowitsch   PetscCall(MatPartitioningHierarchicalSetNfineparts(part, nlocalsubdomains));
2749566063dSJacob Faibussowitsch   PetscCall(MatPartitioningSetFromOptions(part));
275f1580f4eSBarry Smith   /* get new rank owner number of each vertex */
2769566063dSJacob Faibussowitsch   PetscCall(MatPartitioningApply(part, &partitioning));
2779566063dSJacob Faibussowitsch   PetscCall(ISBuildTwoSided(partitioning, NULL, &fromrows));
2789566063dSJacob Faibussowitsch   PetscCall(ISPartitioningToNumbering(partitioning, &isn));
2799566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&isn));
2809566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(fromrows, &fromrows_localsize));
2819566063dSJacob Faibussowitsch   PetscCall(MatPartitioningDestroy(&part));
2829566063dSJacob Faibussowitsch   PetscCall(MatCreateVecs(pc->pmat, &outervec, NULL));
283f4f49eeaSPierre Jolivet   PetscCall(VecCreateMPI(comm, fromrows_localsize, PETSC_DETERMINE, &osm->pcx));
284f4f49eeaSPierre Jolivet   PetscCall(VecDuplicate(osm->pcx, &osm->pcy));
285f4f49eeaSPierre Jolivet   PetscCall(VecScatterCreate(osm->pcx, NULL, outervec, fromrows, &osm->pctoouter));
286f4f49eeaSPierre Jolivet   PetscCall(MatCreateSubMatrix(pc->pmat, fromrows, fromrows, MAT_INITIAL_MATRIX, &osm->permutationP));
2879566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fromrows));
288ea91fabdSFande Kong   osm->permutationIS = fromrows;
289ea91fabdSFande Kong   osm->pcmat         = pc->pmat;
2909566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)osm->permutationP));
291ea91fabdSFande Kong   pc->pmat = osm->permutationP;
2929566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&outervec));
2939566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&fromrows));
2949566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&partitioning));
295ea91fabdSFande Kong   osm->n = PETSC_DETERMINE;
2963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
297ea91fabdSFande Kong }
298ea91fabdSFande Kong 
PCSetUp_GASM(PC pc)299d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCSetUp_GASM(PC pc)
300d71ae5a4SJacob Faibussowitsch {
301f746d493SDmitry Karpeev   PC_GASM        *osm = (PC_GASM *)pc->data;
302ea91fabdSFande Kong   PetscInt        i, nInnerIndices, nTotalInnerIndices;
303c10bc1a1SDmitry Karpeev   PetscMPIInt     rank, size;
304b1a0a8a3SJed Brown   MatReuse        scall = MAT_REUSE_MATRIX;
305b1a0a8a3SJed Brown   KSP             ksp;
306b1a0a8a3SJed Brown   PC              subpc;
307b1a0a8a3SJed Brown   const char     *prefix, *pprefix;
308f746d493SDmitry Karpeev   Vec             x, y;
3096a4f0f73SDmitry Karpeev   PetscInt        oni;   /* Number of indices in the i-th local outer subdomain.               */
3106a4f0f73SDmitry Karpeev   const PetscInt *oidxi; /* Indices from the i-th subdomain local outer subdomain.             */
3116a4f0f73SDmitry Karpeev   PetscInt        on;    /* Number of indices in the disjoint union of local outer subdomains. */
3126a4f0f73SDmitry Karpeev   PetscInt       *oidx;  /* Indices in the disjoint union of local outer subdomains. */
3136a4f0f73SDmitry Karpeev   IS              gois;  /* Disjoint union the global indices of outer subdomains.             */
3146a4f0f73SDmitry Karpeev   IS              goid;  /* Identity IS of the size of the disjoint union of outer subdomains. */
315f746d493SDmitry Karpeev   PetscScalar    *gxarray, *gyarray;
316930d09c1SFande Kong   PetscInt        gostart; /* Start of locally-owned indices in the vectors -- osm->gx,osm->gy -- over the disjoint union of outer subdomains. */
3178f3b4b4dSDmitry Karpeev   PetscInt        num_subdomains  = 0;
3180298fd71SBarry Smith   DM             *subdomain_dm    = NULL;
3198f3b4b4dSDmitry Karpeev   char          **subdomain_names = NULL;
320f771a274SFande Kong   PetscInt       *numbering;
3218f3b4b4dSDmitry Karpeev 
322b1a0a8a3SJed Brown   PetscFunctionBegin;
3239566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size));
3249566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)pc), &rank));
325b1a0a8a3SJed Brown   if (!pc->setupcalled) {
326ea91fabdSFande Kong     /* use a hierarchical partitioning */
3271baa6e33SBarry Smith     if (osm->hierarchicalpartitioning) PetscCall(PCGASMSetHierarchicalPartitioning(pc));
3288f3b4b4dSDmitry Karpeev     if (osm->n == PETSC_DETERMINE) {
3298f3b4b4dSDmitry Karpeev       if (osm->N != PETSC_DETERMINE) {
3308f3b4b4dSDmitry Karpeev         /* No local subdomains given, but the desired number of total subdomains is known, so construct them accordingly. */
3319566063dSJacob Faibussowitsch         PetscCall(PCGASMCreateSubdomains(pc->pmat, osm->N, &osm->n, &osm->iis));
3328f3b4b4dSDmitry Karpeev       } else if (osm->dm_subdomains && pc->dm) {
3338f3b4b4dSDmitry Karpeev         /* try pc->dm next, if allowed */
3348f3b4b4dSDmitry Karpeev         PetscInt d;
335a35b7b57SDmitry Karpeev         IS      *inner_subdomain_is, *outer_subdomain_is;
3369566063dSJacob Faibussowitsch         PetscCall(DMCreateDomainDecomposition(pc->dm, &num_subdomains, &subdomain_names, &inner_subdomain_is, &outer_subdomain_is, &subdomain_dm));
3371baa6e33SBarry Smith         if (num_subdomains) PetscCall(PCGASMSetSubdomains(pc, num_subdomains, inner_subdomain_is, outer_subdomain_is));
338a35b7b57SDmitry Karpeev         for (d = 0; d < num_subdomains; ++d) {
3399566063dSJacob Faibussowitsch           if (inner_subdomain_is) PetscCall(ISDestroy(&inner_subdomain_is[d]));
3409566063dSJacob Faibussowitsch           if (outer_subdomain_is) PetscCall(ISDestroy(&outer_subdomain_is[d]));
341fdc48646SDmitry Karpeev         }
3429566063dSJacob Faibussowitsch         PetscCall(PetscFree(inner_subdomain_is));
3439566063dSJacob Faibussowitsch         PetscCall(PetscFree(outer_subdomain_is));
3448f3b4b4dSDmitry Karpeev       } else {
345f1580f4eSBarry Smith         /* still no subdomains; use one per rank */
34644fe18e5SDmitry Karpeev         osm->nmax = osm->n = 1;
3479566063dSJacob Faibussowitsch         PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size));
348f746d493SDmitry Karpeev         osm->N = size;
3499566063dSJacob Faibussowitsch         PetscCall(PCGASMCreateLocalSubdomains(pc->pmat, osm->n, &osm->iis));
350fdc48646SDmitry Karpeev       }
35106b43e7eSDmitry Karpeev     }
352a35b7b57SDmitry Karpeev     if (!osm->iis) {
353a35b7b57SDmitry Karpeev       /*
3548f3b4b4dSDmitry Karpeev        osm->n was set in PCGASMSetSubdomains(), but the actual subdomains have not been supplied.
3558f3b4b4dSDmitry Karpeev        We create the requisite number of local inner subdomains and then expand them into
3568f3b4b4dSDmitry Karpeev        out subdomains, if necessary.
357a35b7b57SDmitry Karpeev        */
3589566063dSJacob Faibussowitsch       PetscCall(PCGASMCreateLocalSubdomains(pc->pmat, osm->n, &osm->iis));
359a35b7b57SDmitry Karpeev     }
3608f3b4b4dSDmitry Karpeev     if (!osm->ois) {
3618f3b4b4dSDmitry Karpeev       /*
3628f3b4b4dSDmitry Karpeev             Initially make outer subdomains the same as inner subdomains. If nonzero additional overlap
3638f3b4b4dSDmitry Karpeev             has been requested, copy the inner subdomains over so they can be modified.
3648f3b4b4dSDmitry Karpeev       */
3659566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(osm->n, &osm->ois));
3668f3b4b4dSDmitry Karpeev       for (i = 0; i < osm->n; ++i) {
367ea91fabdSFande Kong         if (osm->overlap > 0 && osm->N > 1) { /* With positive overlap, osm->iis[i] will be modified */
3689566063dSJacob Faibussowitsch           PetscCall(ISDuplicate(osm->iis[i], (osm->ois) + i));
3699566063dSJacob Faibussowitsch           PetscCall(ISCopy(osm->iis[i], osm->ois[i]));
3708f3b4b4dSDmitry Karpeev         } else {
371f4f49eeaSPierre Jolivet           PetscCall(PetscObjectReference((PetscObject)osm->iis[i]));
3728f3b4b4dSDmitry Karpeev           osm->ois[i] = osm->iis[i];
3738f3b4b4dSDmitry Karpeev         }
3748f3b4b4dSDmitry Karpeev       }
375ea91fabdSFande Kong       if (osm->overlap > 0 && osm->N > 1) {
3768f3b4b4dSDmitry Karpeev         /* Extend the "overlapping" regions by a number of steps */
3779566063dSJacob Faibussowitsch         PetscCall(MatIncreaseOverlapSplit(pc->pmat, osm->n, osm->ois, osm->overlap));
3788f3b4b4dSDmitry Karpeev       }
379b1a0a8a3SJed Brown     }
3806a4f0f73SDmitry Karpeev 
3818f3b4b4dSDmitry Karpeev     /* Now the subdomains are defined.  Determine their global and max local numbers, if necessary. */
3828f3b4b4dSDmitry Karpeev     if (osm->nmax == PETSC_DETERMINE) {
3836497c311SBarry Smith       PetscInt inwork, outwork;
3848f3b4b4dSDmitry Karpeev       /* determine global number of subdomains and the max number of local subdomains */
3858f3b4b4dSDmitry Karpeev       inwork = osm->n;
386462c564dSBarry Smith       PetscCallMPI(MPIU_Allreduce(&inwork, &outwork, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)pc)));
3878f3b4b4dSDmitry Karpeev       osm->nmax = outwork;
3888f3b4b4dSDmitry Karpeev     }
3898f3b4b4dSDmitry Karpeev     if (osm->N == PETSC_DETERMINE) {
3908f3b4b4dSDmitry Karpeev       /* Determine the number of globally-distinct subdomains and compute a global numbering for them. */
3919566063dSJacob Faibussowitsch       PetscCall(PetscObjectsListGetGlobalNumbering(PetscObjectComm((PetscObject)pc), osm->n, (PetscObject *)osm->ois, &osm->N, NULL));
3928f3b4b4dSDmitry Karpeev     }
3938f3b4b4dSDmitry Karpeev 
394b1a0a8a3SJed Brown     if (osm->sort_indices) {
395f746d493SDmitry Karpeev       for (i = 0; i < osm->n; i++) {
3969566063dSJacob Faibussowitsch         PetscCall(ISSort(osm->ois[i]));
3979566063dSJacob Faibussowitsch         PetscCall(ISSort(osm->iis[i]));
398b1a0a8a3SJed Brown       }
399b1a0a8a3SJed Brown     }
4009566063dSJacob Faibussowitsch     PetscCall(PCGetOptionsPrefix(pc, &prefix));
4019566063dSJacob Faibussowitsch     PetscCall(PCGASMPrintSubdomains(pc));
4028f3b4b4dSDmitry Karpeev 
4036a4f0f73SDmitry Karpeev     /*
4046a4f0f73SDmitry Karpeev        Merge the ISs, create merged vectors and restrictions.
4056a4f0f73SDmitry Karpeev      */
4066a4f0f73SDmitry Karpeev     /* Merge outer subdomain ISs and construct a restriction onto the disjoint union of local outer subdomains. */
4076a4f0f73SDmitry Karpeev     on = 0;
408f746d493SDmitry Karpeev     for (i = 0; i < osm->n; i++) {
4099566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(osm->ois[i], &oni));
4106a4f0f73SDmitry Karpeev       on += oni;
411f746d493SDmitry Karpeev     }
4129566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(on, &oidx));
4136a4f0f73SDmitry Karpeev     on = 0;
414930d09c1SFande Kong     /* Merge local indices together */
415f746d493SDmitry Karpeev     for (i = 0; i < osm->n; i++) {
4169566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(osm->ois[i], &oni));
4179566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(osm->ois[i], &oidxi));
4189566063dSJacob Faibussowitsch       PetscCall(PetscArraycpy(oidx + on, oidxi, oni));
4199566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(osm->ois[i], &oidxi));
4206a4f0f73SDmitry Karpeev       on += oni;
421f746d493SDmitry Karpeev     }
42257508eceSPierre Jolivet     PetscCall(ISCreateGeneral(((PetscObject)pc)->comm, on, oidx, PETSC_OWN_POINTER, &gois));
423ea91fabdSFande Kong     nTotalInnerIndices = 0;
424ea91fabdSFande Kong     for (i = 0; i < osm->n; i++) {
4259566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(osm->iis[i], &nInnerIndices));
426ea91fabdSFande Kong       nTotalInnerIndices += nInnerIndices;
427ea91fabdSFande Kong     }
42857508eceSPierre Jolivet     PetscCall(VecCreateMPI(((PetscObject)pc)->comm, nTotalInnerIndices, PETSC_DETERMINE, &x));
4299566063dSJacob Faibussowitsch     PetscCall(VecDuplicate(x, &y));
430ea91fabdSFande Kong 
4319566063dSJacob Faibussowitsch     PetscCall(VecCreateMPI(PetscObjectComm((PetscObject)pc), on, PETSC_DECIDE, &osm->gx));
4329566063dSJacob Faibussowitsch     PetscCall(VecDuplicate(osm->gx, &osm->gy));
4339566063dSJacob Faibussowitsch     PetscCall(VecGetOwnershipRange(osm->gx, &gostart, NULL));
4349566063dSJacob Faibussowitsch     PetscCall(ISCreateStride(PetscObjectComm((PetscObject)pc), on, gostart, 1, &goid));
435930d09c1SFande Kong     /* gois might indices not on local */
436f4f49eeaSPierre Jolivet     PetscCall(VecScatterCreate(x, gois, osm->gx, goid, &osm->gorestriction));
4379566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(osm->n, &numbering));
4389566063dSJacob Faibussowitsch     PetscCall(PetscObjectsListGetGlobalNumbering(PetscObjectComm((PetscObject)pc), osm->n, (PetscObject *)osm->ois, NULL, numbering));
4399566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&x));
4409566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&gois));
4412fa5cd67SKarl Rupp 
4426a4f0f73SDmitry Karpeev     /* Merge inner subdomain ISs and construct a restriction onto the disjoint union of local inner subdomains. */
4432fa5cd67SKarl Rupp     {
4442fa5cd67SKarl Rupp       PetscInt        ini;   /* Number of indices the i-th a local inner subdomain. */
4458966356dSPierre Jolivet       PetscInt        in;    /* Number of indices in the disjoint union of local inner subdomains. */
4466a4f0f73SDmitry Karpeev       PetscInt       *iidx;  /* Global indices in the merged local inner subdomain. */
4476a4f0f73SDmitry Karpeev       PetscInt       *ioidx; /* Global indices of the disjoint union of inner subdomains within the disjoint union of outer subdomains. */
4486a4f0f73SDmitry Karpeev       IS              giis;  /* IS for the disjoint union of inner subdomains. */
4496a4f0f73SDmitry Karpeev       IS              giois; /* IS for the disjoint union of inner subdomains within the disjoint union of outer subdomains. */
450f771a274SFande Kong       PetscScalar    *array;
451f771a274SFande Kong       const PetscInt *indices;
452f771a274SFande Kong       PetscInt        k;
4536a4f0f73SDmitry Karpeev       on = 0;
454f746d493SDmitry Karpeev       for (i = 0; i < osm->n; i++) {
4559566063dSJacob Faibussowitsch         PetscCall(ISGetLocalSize(osm->ois[i], &oni));
4566a4f0f73SDmitry Karpeev         on += oni;
457f746d493SDmitry Karpeev       }
4589566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(on, &iidx));
4599566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(on, &ioidx));
4609566063dSJacob Faibussowitsch       PetscCall(VecGetArray(y, &array));
461e12b4729SFande Kong       /* set communicator id to determine where overlap is */
462f771a274SFande Kong       in = 0;
463f771a274SFande Kong       for (i = 0; i < osm->n; i++) {
4649566063dSJacob Faibussowitsch         PetscCall(ISGetLocalSize(osm->iis[i], &ini));
465ad540459SPierre Jolivet         for (k = 0; k < ini; ++k) array[in + k] = numbering[i];
466f771a274SFande Kong         in += ini;
467f771a274SFande Kong       }
4689566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(y, &array));
4699566063dSJacob Faibussowitsch       PetscCall(VecScatterBegin(osm->gorestriction, y, osm->gy, INSERT_VALUES, SCATTER_FORWARD));
4709566063dSJacob Faibussowitsch       PetscCall(VecScatterEnd(osm->gorestriction, y, osm->gy, INSERT_VALUES, SCATTER_FORWARD));
4719566063dSJacob Faibussowitsch       PetscCall(VecGetOwnershipRange(osm->gy, &gostart, NULL));
4729566063dSJacob Faibussowitsch       PetscCall(VecGetArray(osm->gy, &array));
473f771a274SFande Kong       on = 0;
474f771a274SFande Kong       in = 0;
475f771a274SFande Kong       for (i = 0; i < osm->n; i++) {
4769566063dSJacob Faibussowitsch         PetscCall(ISGetLocalSize(osm->ois[i], &oni));
4779566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(osm->ois[i], &indices));
478f771a274SFande Kong         for (k = 0; k < oni; k++) {
479e12b4729SFande Kong           /*  skip overlapping indices to get inner domain */
48043081b6cSDmitry Karpeev           if (PetscRealPart(array[on + k]) != numbering[i]) continue;
481f771a274SFande Kong           iidx[in]    = indices[k];
482f771a274SFande Kong           ioidx[in++] = gostart + on + k;
483f771a274SFande Kong         }
4849566063dSJacob Faibussowitsch         PetscCall(ISRestoreIndices(osm->ois[i], &indices));
485f771a274SFande Kong         on += oni;
486f771a274SFande Kong       }
4879566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(osm->gy, &array));
4889566063dSJacob Faibussowitsch       PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)pc), in, iidx, PETSC_OWN_POINTER, &giis));
4899566063dSJacob Faibussowitsch       PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)pc), in, ioidx, PETSC_OWN_POINTER, &giois));
4909566063dSJacob Faibussowitsch       PetscCall(VecScatterCreate(y, giis, osm->gy, giois, &osm->girestriction));
4919566063dSJacob Faibussowitsch       PetscCall(VecDestroy(&y));
4929566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&giis));
4939566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&giois));
494b1a0a8a3SJed Brown     }
4959566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&goid));
4969566063dSJacob Faibussowitsch     PetscCall(PetscFree(numbering));
4972fa5cd67SKarl Rupp 
498f746d493SDmitry Karpeev     /* Create the subdomain work vectors. */
4999566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(osm->n, &osm->x));
5009566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(osm->n, &osm->y));
5019566063dSJacob Faibussowitsch     PetscCall(VecGetArray(osm->gx, &gxarray));
5029566063dSJacob Faibussowitsch     PetscCall(VecGetArray(osm->gy, &gyarray));
5036a4f0f73SDmitry Karpeev     for (i = 0, on = 0; i < osm->n; ++i, on += oni) {
5046a4f0f73SDmitry Karpeev       PetscInt oNi;
5059566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(osm->ois[i], &oni));
506930d09c1SFande Kong       /* on a sub communicator */
5079566063dSJacob Faibussowitsch       PetscCall(ISGetSize(osm->ois[i], &oNi));
508f4f49eeaSPierre Jolivet       PetscCall(VecCreateMPIWithArray(((PetscObject)osm->ois[i])->comm, 1, oni, oNi, gxarray + on, &osm->x[i]));
509f4f49eeaSPierre Jolivet       PetscCall(VecCreateMPIWithArray(((PetscObject)osm->ois[i])->comm, 1, oni, oNi, gyarray + on, &osm->y[i]));
510b1a0a8a3SJed Brown     }
5119566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(osm->gx, &gxarray));
5129566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(osm->gy, &gyarray));
5138f3b4b4dSDmitry Karpeev     /* Create the subdomain solvers */
5149566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(osm->n, &osm->ksp));
5158f3b4b4dSDmitry Karpeev     for (i = 0; i < osm->n; i++) {
5168f3b4b4dSDmitry Karpeev       char subprefix[PETSC_MAX_PATH_LEN + 1];
517f4f49eeaSPierre Jolivet       PetscCall(KSPCreate(((PetscObject)osm->ois[i])->comm, &ksp));
5183821be0aSBarry Smith       PetscCall(KSPSetNestLevel(ksp, pc->kspnestlevel));
5199566063dSJacob Faibussowitsch       PetscCall(KSPSetErrorIfNotConverged(ksp, pc->erroriffailure));
5209566063dSJacob Faibussowitsch       PetscCall(PetscObjectIncrementTabLevel((PetscObject)ksp, (PetscObject)pc, 1));
5219566063dSJacob Faibussowitsch       PetscCall(KSPSetType(ksp, KSPPREONLY));
5229566063dSJacob Faibussowitsch       PetscCall(KSPGetPC(ksp, &subpc)); /* Why do we need this here? */
5238f3b4b4dSDmitry Karpeev       if (subdomain_dm) {
5249566063dSJacob Faibussowitsch         PetscCall(KSPSetDM(ksp, subdomain_dm[i]));
5259566063dSJacob Faibussowitsch         PetscCall(DMDestroy(subdomain_dm + i));
5268f3b4b4dSDmitry Karpeev       }
5279566063dSJacob Faibussowitsch       PetscCall(PCGetOptionsPrefix(pc, &prefix));
5289566063dSJacob Faibussowitsch       PetscCall(KSPSetOptionsPrefix(ksp, prefix));
5298f3b4b4dSDmitry Karpeev       if (subdomain_names && subdomain_names[i]) {
5309566063dSJacob Faibussowitsch         PetscCall(PetscSNPrintf(subprefix, PETSC_MAX_PATH_LEN, "sub_%s_", subdomain_names[i]));
5319566063dSJacob Faibussowitsch         PetscCall(KSPAppendOptionsPrefix(ksp, subprefix));
5329566063dSJacob Faibussowitsch         PetscCall(PetscFree(subdomain_names[i]));
5338f3b4b4dSDmitry Karpeev       }
5349566063dSJacob Faibussowitsch       PetscCall(KSPAppendOptionsPrefix(ksp, "sub_"));
535b1a0a8a3SJed Brown       osm->ksp[i] = ksp;
536b1a0a8a3SJed Brown     }
5379566063dSJacob Faibussowitsch     PetscCall(PetscFree(subdomain_dm));
5389566063dSJacob Faibussowitsch     PetscCall(PetscFree(subdomain_names));
539b1a0a8a3SJed Brown     scall = MAT_INITIAL_MATRIX;
5408f3b4b4dSDmitry Karpeev   } else { /* if (pc->setupcalled) */
541b1a0a8a3SJed Brown     /*
5428f3b4b4dSDmitry Karpeev        Destroy the submatrices from the previous iteration
543b1a0a8a3SJed Brown     */
544b1a0a8a3SJed Brown     if (pc->flag == DIFFERENT_NONZERO_PATTERN) {
5459566063dSJacob Faibussowitsch       PetscCall(MatDestroyMatrices(osm->n, &osm->pmat));
546b1a0a8a3SJed Brown       scall = MAT_INITIAL_MATRIX;
547b1a0a8a3SJed Brown     }
548ea91fabdSFande Kong     if (osm->permutationIS) {
5499566063dSJacob Faibussowitsch       PetscCall(MatCreateSubMatrix(pc->pmat, osm->permutationIS, osm->permutationIS, scall, &osm->permutationP));
5509566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject)osm->permutationP));
551ea91fabdSFande Kong       osm->pcmat = pc->pmat;
552ea91fabdSFande Kong       pc->pmat   = osm->permutationP;
553b1a0a8a3SJed Brown     }
554ea91fabdSFande Kong   }
555ea91fabdSFande Kong 
556b1a0a8a3SJed Brown   /*
5572da392ccSBarry Smith      Extract the submatrices.
558b1a0a8a3SJed Brown   */
55981a5c4aaSDmitry Karpeev   if (size > 1) {
5609566063dSJacob Faibussowitsch     PetscCall(MatCreateSubMatricesMPI(pc->pmat, osm->n, osm->ois, osm->ois, scall, &osm->pmat));
5612fa5cd67SKarl Rupp   } else {
5629566063dSJacob Faibussowitsch     PetscCall(MatCreateSubMatrices(pc->pmat, osm->n, osm->ois, osm->ois, scall, &osm->pmat));
56381a5c4aaSDmitry Karpeev   }
564b1a0a8a3SJed Brown   if (scall == MAT_INITIAL_MATRIX) {
5659566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetOptionsPrefix((PetscObject)pc->pmat, &pprefix));
566aa624791SPierre Jolivet     for (i = 0; i < osm->n; i++) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)osm->pmat[i], pprefix));
567b1a0a8a3SJed Brown   }
568b1a0a8a3SJed Brown 
569b1a0a8a3SJed Brown   /* Return control to the user so that the submatrices can be modified (e.g., to apply
570b1a0a8a3SJed Brown      different boundary conditions for the submatrices than for the global problem) */
5719566063dSJacob Faibussowitsch   PetscCall(PCModifySubMatrices(pc, osm->n, osm->ois, osm->ois, osm->pmat, pc->modifysubmatricesP));
572b1a0a8a3SJed Brown 
573b1a0a8a3SJed Brown   /*
5746a4f0f73SDmitry Karpeev      Loop over submatrices putting them into local ksps
575b1a0a8a3SJed Brown   */
576f746d493SDmitry Karpeev   for (i = 0; i < osm->n; i++) {
5779566063dSJacob Faibussowitsch     PetscCall(KSPSetOperators(osm->ksp[i], osm->pmat[i], osm->pmat[i]));
5789566063dSJacob Faibussowitsch     PetscCall(KSPGetOptionsPrefix(osm->ksp[i], &prefix));
5799566063dSJacob Faibussowitsch     PetscCall(MatSetOptionsPrefix(osm->pmat[i], prefix));
58048a46eb9SPierre Jolivet     if (!pc->setupcalled) PetscCall(KSPSetFromOptions(osm->ksp[i]));
581b1a0a8a3SJed Brown   }
582ea91fabdSFande Kong   if (osm->pcmat) {
5839566063dSJacob Faibussowitsch     PetscCall(MatDestroy(&pc->pmat));
584ea91fabdSFande Kong     pc->pmat   = osm->pcmat;
5850a545947SLisandro Dalcin     osm->pcmat = NULL;
586ea91fabdSFande Kong   }
5873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
588b1a0a8a3SJed Brown }
589b1a0a8a3SJed Brown 
PCSetUpOnBlocks_GASM(PC pc)590d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCSetUpOnBlocks_GASM(PC pc)
591d71ae5a4SJacob Faibussowitsch {
592f746d493SDmitry Karpeev   PC_GASM *osm = (PC_GASM *)pc->data;
593b1a0a8a3SJed Brown   PetscInt i;
594b1a0a8a3SJed Brown 
595b1a0a8a3SJed Brown   PetscFunctionBegin;
59648a46eb9SPierre Jolivet   for (i = 0; i < osm->n; i++) PetscCall(KSPSetUp(osm->ksp[i]));
5973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
598b1a0a8a3SJed Brown }
599b1a0a8a3SJed Brown 
PCApply_GASM(PC pc,Vec xin,Vec yout)600d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCApply_GASM(PC pc, Vec xin, Vec yout)
601d71ae5a4SJacob Faibussowitsch {
602f746d493SDmitry Karpeev   PC_GASM    *osm = (PC_GASM *)pc->data;
603f746d493SDmitry Karpeev   PetscInt    i;
604ea91fabdSFande Kong   Vec         x, y;
605b1a0a8a3SJed Brown   ScatterMode forward = SCATTER_FORWARD, reverse = SCATTER_REVERSE;
606b1a0a8a3SJed Brown 
607b1a0a8a3SJed Brown   PetscFunctionBegin;
608ea91fabdSFande Kong   if (osm->pctoouter) {
6099566063dSJacob Faibussowitsch     PetscCall(VecScatterBegin(osm->pctoouter, xin, osm->pcx, INSERT_VALUES, SCATTER_REVERSE));
6109566063dSJacob Faibussowitsch     PetscCall(VecScatterEnd(osm->pctoouter, xin, osm->pcx, INSERT_VALUES, SCATTER_REVERSE));
611ea91fabdSFande Kong     x = osm->pcx;
612ea91fabdSFande Kong     y = osm->pcy;
613ea91fabdSFande Kong   } else {
614ea91fabdSFande Kong     x = xin;
615ea91fabdSFande Kong     y = yout;
616ea91fabdSFande Kong   }
617b1a0a8a3SJed Brown   /*
61848e38a8aSPierre Jolivet      support for limiting the restriction or interpolation only to the inner
619b1a0a8a3SJed Brown      subdomain values (leaving the other values 0).
620b1a0a8a3SJed Brown   */
621f746d493SDmitry Karpeev   if (!(osm->type & PC_GASM_RESTRICT)) {
622b1a0a8a3SJed Brown     /* have to zero the work RHS since scatter may leave some slots empty */
6239566063dSJacob Faibussowitsch     PetscCall(VecZeroEntries(osm->gx));
6249566063dSJacob Faibussowitsch     PetscCall(VecScatterBegin(osm->girestriction, x, osm->gx, INSERT_VALUES, forward));
6252fa5cd67SKarl Rupp   } else {
6269566063dSJacob Faibussowitsch     PetscCall(VecScatterBegin(osm->gorestriction, x, osm->gx, INSERT_VALUES, forward));
627b1a0a8a3SJed Brown   }
6289566063dSJacob Faibussowitsch   PetscCall(VecZeroEntries(osm->gy));
6296a4f0f73SDmitry Karpeev   if (!(osm->type & PC_GASM_RESTRICT)) {
6309566063dSJacob Faibussowitsch     PetscCall(VecScatterEnd(osm->girestriction, x, osm->gx, INSERT_VALUES, forward));
6312fa5cd67SKarl Rupp   } else {
6329566063dSJacob Faibussowitsch     PetscCall(VecScatterEnd(osm->gorestriction, x, osm->gx, INSERT_VALUES, forward));
6336a4f0f73SDmitry Karpeev   }
63486b96d36SDmitry Karpeev   /* do the subdomain solves */
63586b96d36SDmitry Karpeev   for (i = 0; i < osm->n; ++i) {
6369566063dSJacob Faibussowitsch     PetscCall(KSPSolve(osm->ksp[i], osm->x[i], osm->y[i]));
6379566063dSJacob Faibussowitsch     PetscCall(KSPCheckSolve(osm->ksp[i], pc, osm->y[i]));
638b1a0a8a3SJed Brown   }
63948e38a8aSPierre Jolivet   /* do we need to zero y? */
6409566063dSJacob Faibussowitsch   PetscCall(VecZeroEntries(y));
6416a4f0f73SDmitry Karpeev   if (!(osm->type & PC_GASM_INTERPOLATE)) {
6429566063dSJacob Faibussowitsch     PetscCall(VecScatterBegin(osm->girestriction, osm->gy, y, ADD_VALUES, reverse));
6439566063dSJacob Faibussowitsch     PetscCall(VecScatterEnd(osm->girestriction, osm->gy, y, ADD_VALUES, reverse));
6442fa5cd67SKarl Rupp   } else {
6459566063dSJacob Faibussowitsch     PetscCall(VecScatterBegin(osm->gorestriction, osm->gy, y, ADD_VALUES, reverse));
6469566063dSJacob Faibussowitsch     PetscCall(VecScatterEnd(osm->gorestriction, osm->gy, y, ADD_VALUES, reverse));
6476a4f0f73SDmitry Karpeev   }
648ea91fabdSFande Kong   if (osm->pctoouter) {
6499566063dSJacob Faibussowitsch     PetscCall(VecScatterBegin(osm->pctoouter, y, yout, INSERT_VALUES, SCATTER_FORWARD));
6509566063dSJacob Faibussowitsch     PetscCall(VecScatterEnd(osm->pctoouter, y, yout, INSERT_VALUES, SCATTER_FORWARD));
651ea91fabdSFande Kong   }
6523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
653b1a0a8a3SJed Brown }
654b1a0a8a3SJed Brown 
PCMatApply_GASM(PC pc,Mat Xin,Mat Yout)655d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCMatApply_GASM(PC pc, Mat Xin, Mat Yout)
656d71ae5a4SJacob Faibussowitsch {
65748e38a8aSPierre Jolivet   PC_GASM    *osm = (PC_GASM *)pc->data;
65848e38a8aSPierre Jolivet   Mat         X, Y, O = NULL, Z, W;
65948e38a8aSPierre Jolivet   Vec         x, y;
66048e38a8aSPierre Jolivet   PetscInt    i, m, M, N;
66148e38a8aSPierre Jolivet   ScatterMode forward = SCATTER_FORWARD, reverse = SCATTER_REVERSE;
66248e38a8aSPierre Jolivet 
66348e38a8aSPierre Jolivet   PetscFunctionBegin;
66408401ef6SPierre Jolivet   PetscCheck(osm->n == 1, PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "Not yet implemented");
6659566063dSJacob Faibussowitsch   PetscCall(MatGetSize(Xin, NULL, &N));
66648e38a8aSPierre Jolivet   if (osm->pctoouter) {
6679566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(osm->pcx, &m));
6689566063dSJacob Faibussowitsch     PetscCall(VecGetSize(osm->pcx, &M));
6699566063dSJacob Faibussowitsch     PetscCall(MatCreateDense(PetscObjectComm((PetscObject)osm->ois[0]), m, PETSC_DECIDE, M, N, NULL, &O));
67048e38a8aSPierre Jolivet     for (i = 0; i < N; ++i) {
6719566063dSJacob Faibussowitsch       PetscCall(MatDenseGetColumnVecRead(Xin, i, &x));
6729566063dSJacob Faibussowitsch       PetscCall(MatDenseGetColumnVecWrite(O, i, &y));
6739566063dSJacob Faibussowitsch       PetscCall(VecScatterBegin(osm->pctoouter, x, y, INSERT_VALUES, SCATTER_REVERSE));
6749566063dSJacob Faibussowitsch       PetscCall(VecScatterEnd(osm->pctoouter, x, y, INSERT_VALUES, SCATTER_REVERSE));
6759566063dSJacob Faibussowitsch       PetscCall(MatDenseRestoreColumnVecWrite(O, i, &y));
6769566063dSJacob Faibussowitsch       PetscCall(MatDenseRestoreColumnVecRead(Xin, i, &x));
67748e38a8aSPierre Jolivet     }
67848e38a8aSPierre Jolivet     X = Y = O;
67948e38a8aSPierre Jolivet   } else {
68048e38a8aSPierre Jolivet     X = Xin;
68148e38a8aSPierre Jolivet     Y = Yout;
68248e38a8aSPierre Jolivet   }
68348e38a8aSPierre Jolivet   /*
68448e38a8aSPierre Jolivet      support for limiting the restriction or interpolation only to the inner
68548e38a8aSPierre Jolivet      subdomain values (leaving the other values 0).
68648e38a8aSPierre Jolivet   */
6879566063dSJacob Faibussowitsch   PetscCall(VecGetLocalSize(osm->x[0], &m));
6889566063dSJacob Faibussowitsch   PetscCall(VecGetSize(osm->x[0], &M));
6899566063dSJacob Faibussowitsch   PetscCall(MatCreateDense(PetscObjectComm((PetscObject)osm->ois[0]), m, PETSC_DECIDE, M, N, NULL, &Z));
69048e38a8aSPierre Jolivet   for (i = 0; i < N; ++i) {
6919566063dSJacob Faibussowitsch     PetscCall(MatDenseGetColumnVecRead(X, i, &x));
6929566063dSJacob Faibussowitsch     PetscCall(MatDenseGetColumnVecWrite(Z, i, &y));
69348e38a8aSPierre Jolivet     if (!(osm->type & PC_GASM_RESTRICT)) {
69448e38a8aSPierre Jolivet       /* have to zero the work RHS since scatter may leave some slots empty */
6959566063dSJacob Faibussowitsch       PetscCall(VecZeroEntries(y));
6969566063dSJacob Faibussowitsch       PetscCall(VecScatterBegin(osm->girestriction, x, y, INSERT_VALUES, forward));
6979566063dSJacob Faibussowitsch       PetscCall(VecScatterEnd(osm->girestriction, x, y, INSERT_VALUES, forward));
69848e38a8aSPierre Jolivet     } else {
6999566063dSJacob Faibussowitsch       PetscCall(VecScatterBegin(osm->gorestriction, x, y, INSERT_VALUES, forward));
7009566063dSJacob Faibussowitsch       PetscCall(VecScatterEnd(osm->gorestriction, x, y, INSERT_VALUES, forward));
70148e38a8aSPierre Jolivet     }
7029566063dSJacob Faibussowitsch     PetscCall(MatDenseRestoreColumnVecWrite(Z, i, &y));
7039566063dSJacob Faibussowitsch     PetscCall(MatDenseRestoreColumnVecRead(X, i, &x));
70448e38a8aSPierre Jolivet   }
7059566063dSJacob Faibussowitsch   PetscCall(MatCreateDense(PetscObjectComm((PetscObject)osm->ois[0]), m, PETSC_DECIDE, M, N, NULL, &W));
7069566063dSJacob Faibussowitsch   PetscCall(MatSetOption(Z, MAT_NO_OFF_PROC_ENTRIES, PETSC_TRUE));
7079566063dSJacob Faibussowitsch   PetscCall(MatAssemblyBegin(Z, MAT_FINAL_ASSEMBLY));
7089566063dSJacob Faibussowitsch   PetscCall(MatAssemblyEnd(Z, MAT_FINAL_ASSEMBLY));
70948e38a8aSPierre Jolivet   /* do the subdomain solve */
7109566063dSJacob Faibussowitsch   PetscCall(KSPMatSolve(osm->ksp[0], Z, W));
7119566063dSJacob Faibussowitsch   PetscCall(KSPCheckSolve(osm->ksp[0], pc, NULL));
7129566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&Z));
71348e38a8aSPierre Jolivet   /* do we need to zero y? */
7149566063dSJacob Faibussowitsch   PetscCall(MatZeroEntries(Y));
71548e38a8aSPierre Jolivet   for (i = 0; i < N; ++i) {
7169566063dSJacob Faibussowitsch     PetscCall(MatDenseGetColumnVecWrite(Y, i, &y));
7179566063dSJacob Faibussowitsch     PetscCall(MatDenseGetColumnVecRead(W, i, &x));
71848e38a8aSPierre Jolivet     if (!(osm->type & PC_GASM_INTERPOLATE)) {
7199566063dSJacob Faibussowitsch       PetscCall(VecScatterBegin(osm->girestriction, x, y, ADD_VALUES, reverse));
7209566063dSJacob Faibussowitsch       PetscCall(VecScatterEnd(osm->girestriction, x, y, ADD_VALUES, reverse));
72148e38a8aSPierre Jolivet     } else {
7229566063dSJacob Faibussowitsch       PetscCall(VecScatterBegin(osm->gorestriction, x, y, ADD_VALUES, reverse));
7239566063dSJacob Faibussowitsch       PetscCall(VecScatterEnd(osm->gorestriction, x, y, ADD_VALUES, reverse));
72448e38a8aSPierre Jolivet     }
7259566063dSJacob Faibussowitsch     PetscCall(MatDenseRestoreColumnVecRead(W, i, &x));
72648e38a8aSPierre Jolivet     if (osm->pctoouter) {
7279566063dSJacob Faibussowitsch       PetscCall(MatDenseGetColumnVecWrite(Yout, i, &x));
7289566063dSJacob Faibussowitsch       PetscCall(VecScatterBegin(osm->pctoouter, y, x, INSERT_VALUES, SCATTER_FORWARD));
7299566063dSJacob Faibussowitsch       PetscCall(VecScatterEnd(osm->pctoouter, y, x, INSERT_VALUES, SCATTER_FORWARD));
7309566063dSJacob Faibussowitsch       PetscCall(MatDenseRestoreColumnVecRead(Yout, i, &x));
73148e38a8aSPierre Jolivet     }
7329566063dSJacob Faibussowitsch     PetscCall(MatDenseRestoreColumnVecWrite(Y, i, &y));
73348e38a8aSPierre Jolivet   }
7349566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&W));
7359566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&O));
7363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
73748e38a8aSPierre Jolivet }
73848e38a8aSPierre Jolivet 
PCApplyTranspose_GASM(PC pc,Vec xin,Vec yout)739d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCApplyTranspose_GASM(PC pc, Vec xin, Vec yout)
740d71ae5a4SJacob Faibussowitsch {
741f746d493SDmitry Karpeev   PC_GASM    *osm = (PC_GASM *)pc->data;
742f746d493SDmitry Karpeev   PetscInt    i;
743ea91fabdSFande Kong   Vec         x, y;
744b1a0a8a3SJed Brown   ScatterMode forward = SCATTER_FORWARD, reverse = SCATTER_REVERSE;
745b1a0a8a3SJed Brown 
746b1a0a8a3SJed Brown   PetscFunctionBegin;
747ea91fabdSFande Kong   if (osm->pctoouter) {
7489566063dSJacob Faibussowitsch     PetscCall(VecScatterBegin(osm->pctoouter, xin, osm->pcx, INSERT_VALUES, SCATTER_REVERSE));
7499566063dSJacob Faibussowitsch     PetscCall(VecScatterEnd(osm->pctoouter, xin, osm->pcx, INSERT_VALUES, SCATTER_REVERSE));
750ea91fabdSFande Kong     x = osm->pcx;
751ea91fabdSFande Kong     y = osm->pcy;
752ea91fabdSFande Kong   } else {
753ea91fabdSFande Kong     x = xin;
754ea91fabdSFande Kong     y = yout;
755ea91fabdSFande Kong   }
756b1a0a8a3SJed Brown   /*
757b1a0a8a3SJed Brown      Support for limiting the restriction or interpolation to only local
758b1a0a8a3SJed Brown      subdomain values (leaving the other values 0).
759b1a0a8a3SJed Brown 
760f746d493SDmitry Karpeev      Note: these are reversed from the PCApply_GASM() because we are applying the
761b1a0a8a3SJed Brown      transpose of the three terms
762b1a0a8a3SJed Brown   */
763f746d493SDmitry Karpeev   if (!(osm->type & PC_GASM_INTERPOLATE)) {
764b1a0a8a3SJed Brown     /* have to zero the work RHS since scatter may leave some slots empty */
7659566063dSJacob Faibussowitsch     PetscCall(VecZeroEntries(osm->gx));
7669566063dSJacob Faibussowitsch     PetscCall(VecScatterBegin(osm->girestriction, x, osm->gx, INSERT_VALUES, forward));
7672fa5cd67SKarl Rupp   } else {
7689566063dSJacob Faibussowitsch     PetscCall(VecScatterBegin(osm->gorestriction, x, osm->gx, INSERT_VALUES, forward));
769b1a0a8a3SJed Brown   }
7709566063dSJacob Faibussowitsch   PetscCall(VecZeroEntries(osm->gy));
7716a4f0f73SDmitry Karpeev   if (!(osm->type & PC_GASM_INTERPOLATE)) {
7729566063dSJacob Faibussowitsch     PetscCall(VecScatterEnd(osm->girestriction, x, osm->gx, INSERT_VALUES, forward));
7732fa5cd67SKarl Rupp   } else {
7749566063dSJacob Faibussowitsch     PetscCall(VecScatterEnd(osm->gorestriction, x, osm->gx, INSERT_VALUES, forward));
7756a4f0f73SDmitry Karpeev   }
776b1a0a8a3SJed Brown   /* do the local solves */
777ab3e923fSDmitry 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. */
7789566063dSJacob Faibussowitsch     PetscCall(KSPSolveTranspose(osm->ksp[i], osm->x[i], osm->y[i]));
7799566063dSJacob Faibussowitsch     PetscCall(KSPCheckSolve(osm->ksp[i], pc, osm->y[i]));
780b1a0a8a3SJed Brown   }
7819566063dSJacob Faibussowitsch   PetscCall(VecZeroEntries(y));
7826a4f0f73SDmitry Karpeev   if (!(osm->type & PC_GASM_RESTRICT)) {
7839566063dSJacob Faibussowitsch     PetscCall(VecScatterBegin(osm->girestriction, osm->gy, y, ADD_VALUES, reverse));
7849566063dSJacob Faibussowitsch     PetscCall(VecScatterEnd(osm->girestriction, osm->gy, y, ADD_VALUES, reverse));
7852fa5cd67SKarl Rupp   } else {
7869566063dSJacob Faibussowitsch     PetscCall(VecScatterBegin(osm->gorestriction, osm->gy, y, ADD_VALUES, reverse));
7879566063dSJacob Faibussowitsch     PetscCall(VecScatterEnd(osm->gorestriction, osm->gy, y, ADD_VALUES, reverse));
7886a4f0f73SDmitry Karpeev   }
789ea91fabdSFande Kong   if (osm->pctoouter) {
7909566063dSJacob Faibussowitsch     PetscCall(VecScatterBegin(osm->pctoouter, y, yout, INSERT_VALUES, SCATTER_FORWARD));
7919566063dSJacob Faibussowitsch     PetscCall(VecScatterEnd(osm->pctoouter, y, yout, INSERT_VALUES, SCATTER_FORWARD));
792ea91fabdSFande Kong   }
7933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
794b1a0a8a3SJed Brown }
795b1a0a8a3SJed Brown 
PCReset_GASM(PC pc)796d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCReset_GASM(PC pc)
797d71ae5a4SJacob Faibussowitsch {
798f746d493SDmitry Karpeev   PC_GASM *osm = (PC_GASM *)pc->data;
799b1a0a8a3SJed Brown   PetscInt i;
800b1a0a8a3SJed Brown 
801b1a0a8a3SJed Brown   PetscFunctionBegin;
802b1a0a8a3SJed Brown   if (osm->ksp) {
80348a46eb9SPierre Jolivet     for (i = 0; i < osm->n; i++) PetscCall(KSPReset(osm->ksp[i]));
804b1a0a8a3SJed Brown   }
805b1a0a8a3SJed Brown   if (osm->pmat) {
806f746d493SDmitry Karpeev     if (osm->n > 0) {
807df750dc8SHong Zhang       PetscMPIInt size;
8089566063dSJacob Faibussowitsch       PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size));
809df750dc8SHong Zhang       if (size > 1) {
8107dae84e0SHong Zhang         /* osm->pmat is created by MatCreateSubMatricesMPI(), cannot use MatDestroySubMatrices() */
8119566063dSJacob Faibussowitsch         PetscCall(MatDestroyMatrices(osm->n, &osm->pmat));
812df750dc8SHong Zhang       } else {
8139566063dSJacob Faibussowitsch         PetscCall(MatDestroySubMatrices(osm->n, &osm->pmat));
814df750dc8SHong Zhang       }
815b1a0a8a3SJed Brown     }
816b1a0a8a3SJed Brown   }
817d34fcf5fSBarry Smith   if (osm->x) {
818f746d493SDmitry Karpeev     for (i = 0; i < osm->n; i++) {
8199566063dSJacob Faibussowitsch       PetscCall(VecDestroy(&osm->x[i]));
8209566063dSJacob Faibussowitsch       PetscCall(VecDestroy(&osm->y[i]));
821b1a0a8a3SJed Brown     }
822d34fcf5fSBarry Smith   }
8239566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&osm->gx));
8249566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&osm->gy));
825ab3e923fSDmitry Karpeev 
8269566063dSJacob Faibussowitsch   PetscCall(VecScatterDestroy(&osm->gorestriction));
8279566063dSJacob Faibussowitsch   PetscCall(VecScatterDestroy(&osm->girestriction));
8288f3b4b4dSDmitry Karpeev   if (!osm->user_subdomains) {
8299566063dSJacob Faibussowitsch     PetscCall(PCGASMDestroySubdomains(osm->n, &osm->ois, &osm->iis));
8308f3b4b4dSDmitry Karpeev     osm->N    = PETSC_DETERMINE;
8318f3b4b4dSDmitry Karpeev     osm->nmax = PETSC_DETERMINE;
8328f3b4b4dSDmitry Karpeev   }
833f4f49eeaSPierre Jolivet   if (osm->pctoouter) PetscCall(VecScatterDestroy(&osm->pctoouter));
834f4f49eeaSPierre Jolivet   if (osm->permutationIS) PetscCall(ISDestroy(&osm->permutationIS));
835f4f49eeaSPierre Jolivet   if (osm->pcx) PetscCall(VecDestroy(&osm->pcx));
836f4f49eeaSPierre Jolivet   if (osm->pcy) PetscCall(VecDestroy(&osm->pcy));
837f4f49eeaSPierre Jolivet   if (osm->permutationP) PetscCall(MatDestroy(&osm->permutationP));
83848a46eb9SPierre Jolivet   if (osm->pcmat) PetscCall(MatDestroy(&osm->pcmat));
8393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
840a06653b4SBarry Smith }
841a06653b4SBarry Smith 
PCDestroy_GASM(PC pc)842d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCDestroy_GASM(PC pc)
843d71ae5a4SJacob Faibussowitsch {
844a06653b4SBarry Smith   PC_GASM *osm = (PC_GASM *)pc->data;
845a06653b4SBarry Smith   PetscInt i;
846a06653b4SBarry Smith 
847a06653b4SBarry Smith   PetscFunctionBegin;
8489566063dSJacob Faibussowitsch   PetscCall(PCReset_GASM(pc));
849135757f6SDmitry Karpeev   /* PCReset will not destroy subdomains, if user_subdomains is true. */
8509566063dSJacob Faibussowitsch   PetscCall(PCGASMDestroySubdomains(osm->n, &osm->ois, &osm->iis));
851a06653b4SBarry Smith   if (osm->ksp) {
85248a46eb9SPierre Jolivet     for (i = 0; i < osm->n; i++) PetscCall(KSPDestroy(&osm->ksp[i]));
8539566063dSJacob Faibussowitsch     PetscCall(PetscFree(osm->ksp));
854a06653b4SBarry Smith   }
8559566063dSJacob Faibussowitsch   PetscCall(PetscFree(osm->x));
8569566063dSJacob Faibussowitsch   PetscCall(PetscFree(osm->y));
8572e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetSubdomains_C", NULL));
8582e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetOverlap_C", NULL));
8592e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetType_C", NULL));
8602e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetSortIndices_C", NULL));
8612e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMGetSubKSP_C", NULL));
8629566063dSJacob Faibussowitsch   PetscCall(PetscFree(pc->data));
8633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
864b1a0a8a3SJed Brown }
865b1a0a8a3SJed Brown 
PCSetFromOptions_GASM(PC pc,PetscOptionItems PetscOptionsObject)866ce78bad3SBarry Smith static PetscErrorCode PCSetFromOptions_GASM(PC pc, PetscOptionItems PetscOptionsObject)
867d71ae5a4SJacob Faibussowitsch {
868f746d493SDmitry Karpeev   PC_GASM   *osm = (PC_GASM *)pc->data;
869b1a0a8a3SJed Brown   PetscInt   blocks, ovl;
87057501b6eSBarry Smith   PetscBool  flg;
871f746d493SDmitry Karpeev   PCGASMType gasmtype;
872b1a0a8a3SJed Brown 
873b1a0a8a3SJed Brown   PetscFunctionBegin;
874d0609cedSBarry Smith   PetscOptionsHeadBegin(PetscOptionsObject, "Generalized additive Schwarz options");
8759566063dSJacob 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));
8769566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt("-pc_gasm_total_subdomains", "Total number of subdomains across communicator", "PCGASMSetTotalSubdomains", osm->N, &blocks, &flg));
8771baa6e33SBarry Smith   if (flg) PetscCall(PCGASMSetTotalSubdomains(pc, blocks));
8789566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt("-pc_gasm_overlap", "Number of overlapping degrees of freedom", "PCGASMSetOverlap", osm->overlap, &ovl, &flg));
87965db9045SDmitry Karpeev   if (flg) {
8809566063dSJacob Faibussowitsch     PetscCall(PCGASMSetOverlap(pc, ovl));
881d709ea83SDmitry Karpeev     osm->dm_subdomains = PETSC_FALSE;
88265db9045SDmitry Karpeev   }
883b1a0a8a3SJed Brown   flg = PETSC_FALSE;
8849566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-pc_gasm_type", "Type of restriction/extension", "PCGASMSetType", PCGASMTypes, (PetscEnum)osm->type, (PetscEnum *)&gasmtype, &flg));
8859566063dSJacob Faibussowitsch   if (flg) PetscCall(PCGASMSetType(pc, gasmtype));
8869566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-pc_gasm_use_hierachical_partitioning", "use hierarchical partitioning", NULL, osm->hierarchicalpartitioning, &osm->hierarchicalpartitioning, &flg));
887d0609cedSBarry Smith   PetscOptionsHeadEnd();
8883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
889b1a0a8a3SJed Brown }
890b1a0a8a3SJed Brown 
8918f3b4b4dSDmitry Karpeev /*@
892f1580f4eSBarry Smith   PCGASMSetTotalSubdomains - sets the total number of subdomains to use across the communicator for `PCGASM`
893c3339decSBarry Smith 
894c3339decSBarry Smith   Logically Collective
8958f3b4b4dSDmitry Karpeev 
8968f3b4b4dSDmitry Karpeev   Input Parameters:
8978f3b4b4dSDmitry Karpeev + pc - the preconditioner
8988f3b4b4dSDmitry Karpeev - N  - total number of subdomains
8998f3b4b4dSDmitry Karpeev 
9008f3b4b4dSDmitry Karpeev   Level: beginner
9018f3b4b4dSDmitry Karpeev 
902562efe2eSBarry Smith .seealso: [](ch_ksp), `PCGASM`, `PCGASMSetSubdomains()`, `PCGASMSetOverlap()`
903db781477SPatrick Sanan           `PCGASMCreateSubdomains2D()`
9048f3b4b4dSDmitry Karpeev @*/
PCGASMSetTotalSubdomains(PC pc,PetscInt N)905d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGASMSetTotalSubdomains(PC pc, PetscInt N)
906d71ae5a4SJacob Faibussowitsch {
9078f3b4b4dSDmitry Karpeev   PC_GASM    *osm = (PC_GASM *)pc->data;
9088f3b4b4dSDmitry Karpeev   PetscMPIInt size, rank;
9098f3b4b4dSDmitry Karpeev 
9108f3b4b4dSDmitry Karpeev   PetscFunctionBegin;
91163a3b9bcSJacob 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);
91228b400f6SJacob Faibussowitsch   PetscCheck(!pc->setupcalled, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "PCGASMSetTotalSubdomains() should be called before calling PCSetUp().");
9138f3b4b4dSDmitry Karpeev 
9149566063dSJacob Faibussowitsch   PetscCall(PCGASMDestroySubdomains(osm->n, &osm->iis, &osm->ois));
9158f3b4b4dSDmitry Karpeev   osm->ois = osm->iis = NULL;
9168f3b4b4dSDmitry Karpeev 
9179566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size));
9189566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)pc), &rank));
9198f3b4b4dSDmitry Karpeev   osm->N             = N;
9208f3b4b4dSDmitry Karpeev   osm->n             = PETSC_DETERMINE;
9218f3b4b4dSDmitry Karpeev   osm->nmax          = PETSC_DETERMINE;
9228f3b4b4dSDmitry Karpeev   osm->dm_subdomains = PETSC_FALSE;
9233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9248f3b4b4dSDmitry Karpeev }
9258f3b4b4dSDmitry Karpeev 
PCGASMSetSubdomains_GASM(PC pc,PetscInt n,IS iis[],IS ois[])926d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGASMSetSubdomains_GASM(PC pc, PetscInt n, IS iis[], IS ois[])
927d71ae5a4SJacob Faibussowitsch {
928f746d493SDmitry Karpeev   PC_GASM *osm = (PC_GASM *)pc->data;
929b1a0a8a3SJed Brown   PetscInt i;
930b1a0a8a3SJed Brown 
931b1a0a8a3SJed Brown   PetscFunctionBegin;
932f1580f4eSBarry 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);
93328b400f6SJacob Faibussowitsch   PetscCheck(!pc->setupcalled, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "PCGASMSetSubdomains() should be called before calling PCSetUp().");
934b1a0a8a3SJed Brown 
9359566063dSJacob Faibussowitsch   PetscCall(PCGASMDestroySubdomains(osm->n, &osm->iis, &osm->ois));
9368f3b4b4dSDmitry Karpeev   osm->iis = osm->ois = NULL;
9378f3b4b4dSDmitry Karpeev   osm->n              = n;
9388f3b4b4dSDmitry Karpeev   osm->N              = PETSC_DETERMINE;
9398f3b4b4dSDmitry Karpeev   osm->nmax           = PETSC_DETERMINE;
940a35b7b57SDmitry Karpeev   if (ois) {
9419566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(n, &osm->ois));
9428f3b4b4dSDmitry Karpeev     for (i = 0; i < n; i++) {
9439566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject)ois[i]));
9448f3b4b4dSDmitry Karpeev       osm->ois[i] = ois[i];
9458f3b4b4dSDmitry Karpeev     }
9468f3b4b4dSDmitry Karpeev     /*
9478f3b4b4dSDmitry Karpeev        Since the user set the outer subdomains, even if nontrivial overlap was requested via PCGASMSetOverlap(),
9488f3b4b4dSDmitry Karpeev        it will be ignored.  To avoid confusion later on (e.g., when viewing the PC), the overlap size is set to -1.
9498f3b4b4dSDmitry Karpeev     */
950b1a0a8a3SJed Brown     osm->overlap = -1;
951670417b2SFande Kong     /* inner subdomains must be provided  */
95228b400f6SJacob Faibussowitsch     PetscCheck(iis, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "inner indices have to be provided ");
953670417b2SFande Kong   } /* end if */
954a35b7b57SDmitry Karpeev   if (iis) {
9559566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(n, &osm->iis));
9568f3b4b4dSDmitry Karpeev     for (i = 0; i < n; i++) {
9579566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject)iis[i]));
9588f3b4b4dSDmitry Karpeev       osm->iis[i] = iis[i];
9598f3b4b4dSDmitry Karpeev     }
960a35b7b57SDmitry Karpeev     if (!ois) {
961390e1bf2SBarry Smith       osm->ois = NULL;
962670417b2SFande Kong       /* if user does not provide outer indices, we will create the corresponding outer indices using  osm->overlap =1 in PCSetUp_GASM */
963670417b2SFande Kong     }
964670417b2SFande Kong   }
96576bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
966670417b2SFande Kong     PetscInt        j, rstart, rend, *covered, lsize;
967670417b2SFande Kong     const PetscInt *indices;
968226e28e1SBarry Smith 
969226e28e1SBarry Smith     if (osm->iis) {
9707addb90fSBarry Smith       /* check if the inner indices cover and only cover the local portion of the matrix */
9719566063dSJacob Faibussowitsch       PetscCall(MatGetOwnershipRange(pc->pmat, &rstart, &rend));
9729566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(rend - rstart, &covered));
973f13dfd9eSBarry Smith       /* check if the current MPI process owns indices from others */
974a35b7b57SDmitry Karpeev       for (i = 0; i < n; i++) {
9759566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(osm->iis[i], &indices));
9769566063dSJacob Faibussowitsch         PetscCall(ISGetLocalSize(osm->iis[i], &lsize));
977670417b2SFande Kong         for (j = 0; j < lsize; j++) {
978f1580f4eSBarry 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]);
9792472a847SBarry 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]);
98063a3b9bcSJacob Faibussowitsch           covered[indices[j] - rstart] = 1;
981a35b7b57SDmitry Karpeev         }
9829566063dSJacob Faibussowitsch         PetscCall(ISRestoreIndices(osm->iis[i], &indices));
9838f3b4b4dSDmitry Karpeev       }
984670417b2SFande Kong       /* check if we miss any indices */
985ad540459SPierre 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);
9869566063dSJacob Faibussowitsch       PetscCall(PetscFree(covered));
987a35b7b57SDmitry Karpeev     }
988226e28e1SBarry Smith   }
9898f3b4b4dSDmitry Karpeev   if (iis) osm->user_subdomains = PETSC_TRUE;
9903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
991b1a0a8a3SJed Brown }
992b1a0a8a3SJed Brown 
PCGASMSetOverlap_GASM(PC pc,PetscInt ovl)993d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGASMSetOverlap_GASM(PC pc, PetscInt ovl)
994d71ae5a4SJacob Faibussowitsch {
995f746d493SDmitry Karpeev   PC_GASM *osm = (PC_GASM *)pc->data;
996b1a0a8a3SJed Brown 
997b1a0a8a3SJed Brown   PetscFunctionBegin;
99808401ef6SPierre Jolivet   PetscCheck(ovl >= 0, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "Negative overlap value requested");
9992472a847SBarry Smith   PetscCheck(!pc->setupcalled || ovl == osm->overlap, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "PCGASMSetOverlap() should be called before PCSetUp().");
10002fa5cd67SKarl Rupp   if (!pc->setupcalled) osm->overlap = ovl;
10013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1002b1a0a8a3SJed Brown }
1003b1a0a8a3SJed Brown 
PCGASMSetType_GASM(PC pc,PCGASMType type)1004d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGASMSetType_GASM(PC pc, PCGASMType type)
1005d71ae5a4SJacob Faibussowitsch {
1006f746d493SDmitry Karpeev   PC_GASM *osm = (PC_GASM *)pc->data;
1007b1a0a8a3SJed Brown 
1008b1a0a8a3SJed Brown   PetscFunctionBegin;
1009b1a0a8a3SJed Brown   osm->type     = type;
1010b1a0a8a3SJed Brown   osm->type_set = PETSC_TRUE;
10113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1012b1a0a8a3SJed Brown }
1013b1a0a8a3SJed Brown 
PCGASMSetSortIndices_GASM(PC pc,PetscBool doSort)1014d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGASMSetSortIndices_GASM(PC pc, PetscBool doSort)
1015d71ae5a4SJacob Faibussowitsch {
1016f746d493SDmitry Karpeev   PC_GASM *osm = (PC_GASM *)pc->data;
1017b1a0a8a3SJed Brown 
1018b1a0a8a3SJed Brown   PetscFunctionBegin;
1019b1a0a8a3SJed Brown   osm->sort_indices = doSort;
10203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1021b1a0a8a3SJed Brown }
1022b1a0a8a3SJed Brown 
102344fe18e5SDmitry Karpeev /*
1024f13dfd9eSBarry Smith    FIXME: This routine might need to be modified now that multiple processes per subdomain are allowed.
102544fe18e5SDmitry Karpeev         In particular, it would upset the global subdomain number calculation.
102644fe18e5SDmitry Karpeev */
PCGASMGetSubKSP_GASM(PC pc,PetscInt * n,PetscInt * first,KSP ** ksp)1027d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGASMGetSubKSP_GASM(PC pc, PetscInt *n, PetscInt *first, KSP **ksp)
1028d71ae5a4SJacob Faibussowitsch {
1029f746d493SDmitry Karpeev   PC_GASM *osm = (PC_GASM *)pc->data;
1030b1a0a8a3SJed Brown 
1031b1a0a8a3SJed Brown   PetscFunctionBegin;
103208401ef6SPierre 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");
1033b1a0a8a3SJed Brown 
10342fa5cd67SKarl Rupp   if (n) *n = osm->n;
1035ab3e923fSDmitry Karpeev   if (first) {
10369566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Scan(&osm->n, first, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject)pc)));
1037ab3e923fSDmitry Karpeev     *first -= osm->n;
1038b1a0a8a3SJed Brown   }
1039b1a0a8a3SJed Brown   if (ksp) {
1040b1a0a8a3SJed Brown     /* Assume that local solves are now different; not necessarily
104106b43e7eSDmitry Karpeev        true, though!  This flag is used only for PCView_GASM() */
1042b1a0a8a3SJed Brown     *ksp                        = osm->ksp;
10436a4f0f73SDmitry Karpeev     osm->same_subdomain_solvers = PETSC_FALSE;
1044b1a0a8a3SJed Brown   }
10453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1046ab3e923fSDmitry Karpeev } /* PCGASMGetSubKSP_GASM() */
1047b1a0a8a3SJed Brown 
10485d83a8b1SBarry Smith /*@
10495d83a8b1SBarry Smith   PCGASMSetSubdomains - Sets the subdomains for this MPI process
10505d83a8b1SBarry Smith   for the additive Schwarz preconditioner with multiple MPI processes per subdomain, `PCGASM`
1051b1a0a8a3SJed Brown 
1052c3339decSBarry Smith   Collective
1053b1a0a8a3SJed Brown 
1054b1a0a8a3SJed Brown   Input Parameters:
10558f3b4b4dSDmitry Karpeev + pc  - the preconditioner object
1056f13dfd9eSBarry Smith . n   - the number of subdomains for this MPI process
1057f13dfd9eSBarry Smith . iis - the index sets that define the inner subdomains (or `NULL` for PETSc to determine subdomains), the `iis` array is
1058f13dfd9eSBarry Smith         copied so may be freed after this call.
1059f36f9100SBarry Smith - ois - the index sets that define the outer subdomains (or `NULL` to use the same as `iis`, or to construct by expanding `iis` by
1060f13dfd9eSBarry Smith         the requested overlap), the `ois` array is copied so may be freed after this call.
106120f4b53cSBarry Smith 
106220f4b53cSBarry Smith   Level: advanced
1063b1a0a8a3SJed Brown 
1064b1a0a8a3SJed Brown   Notes:
1065f1580f4eSBarry Smith   The `IS` indices use the parallel, global numbering of the vector entries.
1066f36f9100SBarry Smith 
10676a4f0f73SDmitry Karpeev   Inner subdomains are those where the correction is applied.
1068f36f9100SBarry Smith 
10696a4f0f73SDmitry Karpeev   Outer subdomains are those where the residual necessary to obtain the
1070f1580f4eSBarry Smith   corrections is obtained (see `PCGASMType` for the use of inner/outer subdomains).
1071f36f9100SBarry Smith 
1072f13dfd9eSBarry Smith   Both inner and outer subdomains can extend over several MPI processes.
1073f13dfd9eSBarry Smith   This process' portion of a subdomain is known as a local subdomain.
10746a4f0f73SDmitry Karpeev 
1075f13dfd9eSBarry Smith   Inner subdomains can not overlap with each other, do not have any entities from remote processes,
1076f13dfd9eSBarry Smith   and  have to cover the entire local subdomain owned by the current process. The index sets on each
1077f13dfd9eSBarry Smith   process should be ordered such that the ith local subdomain is connected to the ith remote subdomain
1078f13dfd9eSBarry Smith   on another MPI process.
1079670417b2SFande Kong 
1080f13dfd9eSBarry Smith   By default the `PGASM` preconditioner uses 1 (local) subdomain per MPI process.
10816a4f0f73SDmitry Karpeev 
1082f36f9100SBarry Smith   The `iis` and `ois` arrays may be freed after this call using `PCGASMDestroySubdomains()`
1083f36f9100SBarry Smith 
1084562efe2eSBarry Smith .seealso: [](ch_ksp), `PCGASM`, `PCGASMSetOverlap()`, `PCGASMGetSubKSP()`, `PCGASMDestroySubdomains()`,
1085db781477SPatrick Sanan           `PCGASMCreateSubdomains2D()`, `PCGASMGetSubdomains()`
1086b1a0a8a3SJed Brown @*/
PCGASMSetSubdomains(PC pc,PetscInt n,IS iis[],IS ois[])1087d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGASMSetSubdomains(PC pc, PetscInt n, IS iis[], IS ois[])
1088d71ae5a4SJacob Faibussowitsch {
10898f3b4b4dSDmitry Karpeev   PC_GASM *osm = (PC_GASM *)pc->data;
1090b1a0a8a3SJed Brown 
1091b1a0a8a3SJed Brown   PetscFunctionBegin;
1092b1a0a8a3SJed Brown   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1093cac4c232SBarry Smith   PetscTryMethod(pc, "PCGASMSetSubdomains_C", (PC, PetscInt, IS[], IS[]), (pc, n, iis, ois));
10948f3b4b4dSDmitry Karpeev   osm->dm_subdomains = PETSC_FALSE;
10953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1096b1a0a8a3SJed Brown }
1097b1a0a8a3SJed Brown 
1098b1a0a8a3SJed Brown /*@
1099f746d493SDmitry Karpeev   PCGASMSetOverlap - Sets the overlap between a pair of subdomains for the
1100f13dfd9eSBarry Smith   additive Schwarz preconditioner `PCGASM`.  Either all or no MPI processes in the
11018f3b4b4dSDmitry Karpeev   pc communicator must call this routine.
1102b1a0a8a3SJed Brown 
1103c3339decSBarry Smith   Logically Collective
1104b1a0a8a3SJed Brown 
1105b1a0a8a3SJed Brown   Input Parameters:
1106b1a0a8a3SJed Brown + pc  - the preconditioner context
11078f3b4b4dSDmitry Karpeev - ovl - the amount of overlap between subdomains (ovl >= 0, default value = 0)
1108b1a0a8a3SJed Brown 
1109b1a0a8a3SJed Brown   Options Database Key:
111006b43e7eSDmitry Karpeev . -pc_gasm_overlap <overlap> - Sets overlap
1111b1a0a8a3SJed Brown 
111220f4b53cSBarry Smith   Level: intermediate
111320f4b53cSBarry Smith 
1114b1a0a8a3SJed Brown   Notes:
1115f13dfd9eSBarry Smith   By default the `PCGASM` preconditioner uses 1 subdomain per process.  To use
11168f3b4b4dSDmitry Karpeev   multiple subdomain per perocessor or "straddling" subdomains that intersect
1117f13dfd9eSBarry Smith   multiple processes use `PCGASMSetSubdomains()` (or option `-pc_gasm_total_subdomains` <n>).
1118b1a0a8a3SJed Brown 
11198f3b4b4dSDmitry Karpeev   The overlap defaults to 0, so if one desires that no additional
1120b1a0a8a3SJed Brown   overlap be computed beyond what may have been set with a call to
1121f36f9100SBarry Smith   `PCGASMSetSubdomains()`, then `ovl` must be set to be 0.  In particular, if one does
11228f3b4b4dSDmitry Karpeev   not explicitly set the subdomains in application code, then all overlap would be computed
1123f1580f4eSBarry Smith   internally by PETSc, and using an overlap of 0 would result in an `PCGASM`
1124b1a0a8a3SJed Brown   variant that is equivalent to the block Jacobi preconditioner.
1125b1a0a8a3SJed Brown 
1126f1580f4eSBarry Smith   One can define initial index sets with any overlap via
1127f1580f4eSBarry Smith   `PCGASMSetSubdomains()`; the routine `PCGASMSetOverlap()` merely allows
112806b43e7eSDmitry Karpeev   PETSc to extend that overlap further, if desired.
1129b1a0a8a3SJed Brown 
1130562efe2eSBarry Smith .seealso: [](ch_ksp), `PCGASM`, `PCGASMSetSubdomains()`, `PCGASMGetSubKSP()`,
1131db781477SPatrick Sanan           `PCGASMCreateSubdomains2D()`, `PCGASMGetSubdomains()`
1132b1a0a8a3SJed Brown @*/
PCGASMSetOverlap(PC pc,PetscInt ovl)1133d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGASMSetOverlap(PC pc, PetscInt ovl)
1134d71ae5a4SJacob Faibussowitsch {
11358f3b4b4dSDmitry Karpeev   PC_GASM *osm = (PC_GASM *)pc->data;
1136b1a0a8a3SJed Brown 
1137b1a0a8a3SJed Brown   PetscFunctionBegin;
1138b1a0a8a3SJed Brown   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1139b1a0a8a3SJed Brown   PetscValidLogicalCollectiveInt(pc, ovl, 2);
1140cac4c232SBarry Smith   PetscTryMethod(pc, "PCGASMSetOverlap_C", (PC, PetscInt), (pc, ovl));
11418f3b4b4dSDmitry Karpeev   osm->dm_subdomains = PETSC_FALSE;
11423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1143b1a0a8a3SJed Brown }
1144b1a0a8a3SJed Brown 
1145b1a0a8a3SJed Brown /*@
1146f746d493SDmitry Karpeev   PCGASMSetType - Sets the type of restriction and interpolation used
1147f1580f4eSBarry Smith   for local problems in the `PCGASM` additive Schwarz method.
1148b1a0a8a3SJed Brown 
1149c3339decSBarry Smith   Logically Collective
1150b1a0a8a3SJed Brown 
1151b1a0a8a3SJed Brown   Input Parameters:
1152b1a0a8a3SJed Brown + pc   - the preconditioner context
1153f1580f4eSBarry Smith - type - variant of `PCGASM`, one of
1154b1a0a8a3SJed Brown .vb
1155f1580f4eSBarry Smith       `PC_GASM_BASIC`       - full interpolation and restriction
1156f13dfd9eSBarry Smith       `PC_GASM_RESTRICT`    - full restriction, local MPI process interpolation
1157f13dfd9eSBarry Smith       `PC_GASM_INTERPOLATE` - full interpolation, local MPI process restriction
1158f13dfd9eSBarry Smith       `PC_GASM_NONE`        - local MPI process restriction and interpolation
1159b1a0a8a3SJed Brown .ve
1160b1a0a8a3SJed Brown 
1161b1a0a8a3SJed Brown   Options Database Key:
1162f36f9100SBarry Smith . -pc_gasm_type [basic,restrict,interpolate,none] - Sets `PCGASM` type
1163b1a0a8a3SJed Brown 
1164b1a0a8a3SJed Brown   Level: intermediate
1165b1a0a8a3SJed Brown 
1166562efe2eSBarry Smith .seealso: [](ch_ksp), `PCGASM`, `PCGASMSetSubdomains()`, `PCGASMGetSubKSP()`,
1167f1580f4eSBarry Smith           `PCGASMCreateSubdomains2D()`, `PCASM`, `PCASMSetType()`
1168b1a0a8a3SJed Brown @*/
PCGASMSetType(PC pc,PCGASMType type)1169d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGASMSetType(PC pc, PCGASMType type)
1170d71ae5a4SJacob Faibussowitsch {
1171b1a0a8a3SJed Brown   PetscFunctionBegin;
1172b1a0a8a3SJed Brown   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1173b1a0a8a3SJed Brown   PetscValidLogicalCollectiveEnum(pc, type, 2);
1174cac4c232SBarry Smith   PetscTryMethod(pc, "PCGASMSetType_C", (PC, PCGASMType), (pc, type));
11753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1176b1a0a8a3SJed Brown }
1177b1a0a8a3SJed Brown 
1178b1a0a8a3SJed Brown /*@
1179f746d493SDmitry Karpeev   PCGASMSetSortIndices - Determines whether subdomain indices are sorted.
1180b1a0a8a3SJed Brown 
1181c3339decSBarry Smith   Logically Collective
1182b1a0a8a3SJed Brown 
1183b1a0a8a3SJed Brown   Input Parameters:
1184b1a0a8a3SJed Brown + pc     - the preconditioner context
1185b1a0a8a3SJed Brown - doSort - sort the subdomain indices
1186b1a0a8a3SJed Brown 
1187b1a0a8a3SJed Brown   Level: intermediate
1188b1a0a8a3SJed Brown 
1189562efe2eSBarry Smith .seealso: [](ch_ksp), `PCGASM`, `PCGASMSetSubdomains()`, `PCGASMGetSubKSP()`,
1190db781477SPatrick Sanan           `PCGASMCreateSubdomains2D()`
1191b1a0a8a3SJed Brown @*/
PCGASMSetSortIndices(PC pc,PetscBool doSort)1192d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGASMSetSortIndices(PC pc, PetscBool doSort)
1193d71ae5a4SJacob Faibussowitsch {
1194b1a0a8a3SJed Brown   PetscFunctionBegin;
1195b1a0a8a3SJed Brown   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1196b1a0a8a3SJed Brown   PetscValidLogicalCollectiveBool(pc, doSort, 2);
1197cac4c232SBarry Smith   PetscTryMethod(pc, "PCGASMSetSortIndices_C", (PC, PetscBool), (pc, doSort));
11983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1199b1a0a8a3SJed Brown }
1200b1a0a8a3SJed Brown 
1201b1a0a8a3SJed Brown /*@C
1202f13dfd9eSBarry Smith   PCGASMGetSubKSP - Gets the local `KSP` contexts for all subdomains on this MPI process.
1203b1a0a8a3SJed Brown 
1204c3339decSBarry Smith   Collective iff first_local is requested
1205b1a0a8a3SJed Brown 
1206b1a0a8a3SJed Brown   Input Parameter:
1207b1a0a8a3SJed Brown . pc - the preconditioner context
1208b1a0a8a3SJed Brown 
1209b1a0a8a3SJed Brown   Output Parameters:
1210f13dfd9eSBarry Smith + n_local     - the number of blocks on this MPI process or `NULL`
1211f13dfd9eSBarry Smith . first_local - the global number of the first block on this process or `NULL`, all processes must request or all must pass `NULL`
1212f1580f4eSBarry Smith - ksp         - the array of `KSP` contexts
1213b1a0a8a3SJed Brown 
1214f36f9100SBarry Smith   Level: advanced
1215f36f9100SBarry Smith 
1216b1a0a8a3SJed Brown   Note:
1217f1580f4eSBarry Smith   After `PCGASMGetSubKSP()` the array of `KSP`es is not to be freed
1218b1a0a8a3SJed Brown 
1219f36f9100SBarry Smith   Currently for some matrix implementations only 1 block per MPI process
1220b1a0a8a3SJed Brown   is supported.
1221b1a0a8a3SJed Brown 
1222f1580f4eSBarry Smith   You must call `KSPSetUp()` before calling `PCGASMGetSubKSP()`.
1223b1a0a8a3SJed Brown 
1224226e28e1SBarry Smith   Fortran Note:
1225226e28e1SBarry Smith   Call `PCGASMRestoreSubKSP()` when the array of `KSP` is no longer needed
1226226e28e1SBarry Smith 
1227562efe2eSBarry Smith .seealso: [](ch_ksp), `PCGASM`, `PCGASMSetSubdomains()`, `PCGASMSetOverlap()`,
1228db781477SPatrick Sanan           `PCGASMCreateSubdomains2D()`,
1229b1a0a8a3SJed Brown @*/
PCGASMGetSubKSP(PC pc,PetscInt * n_local,PetscInt * first_local,KSP * ksp[])1230d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGASMGetSubKSP(PC pc, PetscInt *n_local, PetscInt *first_local, KSP *ksp[])
1231d71ae5a4SJacob Faibussowitsch {
1232b1a0a8a3SJed Brown   PetscFunctionBegin;
1233b1a0a8a3SJed Brown   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1234cac4c232SBarry Smith   PetscUseMethod(pc, "PCGASMGetSubKSP_C", (PC, PetscInt *, PetscInt *, KSP **), (pc, n_local, first_local, ksp));
12353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1236b1a0a8a3SJed Brown }
1237b1a0a8a3SJed Brown 
1238b1a0a8a3SJed Brown /*MC
1239f746d493SDmitry Karpeev    PCGASM - Use the (restricted) additive Schwarz method, each block is (approximately) solved with
12401d27aa22SBarry Smith            its own `KSP` object on a subset of MPI processes
1241b1a0a8a3SJed Brown 
1242b1a0a8a3SJed Brown    Options Database Keys:
12431d27aa22SBarry Smith +  -pc_gasm_total_subdomains <n>                   - Sets total number of local subdomains to be distributed among the MPI processes
1244f1580f4eSBarry Smith .  -pc_gasm_view_subdomains                        - activates the printing of subdomain indices in `PCView()`, -ksp_view or -snes_view
1245f1580f4eSBarry Smith .  -pc_gasm_print_subdomains                       - activates the printing of subdomain indices in `PCSetUp()`
124606b43e7eSDmitry Karpeev .  -pc_gasm_overlap <ovl>                          - Sets overlap by which to (automatically) extend local subdomains
1247f1580f4eSBarry Smith -  -pc_gasm_type [basic,restrict,interpolate,none] - Sets `PCGASMType`
1248b1a0a8a3SJed Brown 
1249f36f9100SBarry Smith    Level: beginner
1250f36f9100SBarry Smith 
125195452b02SPatrick Sanan    Notes:
1252f36f9100SBarry Smith    To set options on the solvers for each block append `-sub_` to all the `KSP`, and `PC`
1253f36f9100SBarry Smith    options database keys. For example, `-sub_pc_type ilu -sub_pc_factor_levels 1 -sub_ksp_type preonly`
1254b1a0a8a3SJed Brown 
1255f1580f4eSBarry Smith    To set the options on the solvers separate for each block call `PCGASMGetSubKSP()`
1256f1580f4eSBarry Smith    and set the options directly on the resulting `KSP` object (you can access its `PC`
12570462cc06SPierre Jolivet    with `KSPGetPC()`)
1258b1a0a8a3SJed Brown 
12591d27aa22SBarry Smith    See {cite}`dryja1987additive` and {cite}`1sbg` for details on additive Schwarz algorithms
1260b1a0a8a3SJed Brown 
1261562efe2eSBarry Smith .seealso: [](ch_ksp), `PCCreate()`, `PCSetType()`, `PCType`, `PC`, `PCASM`, `PCGASMType`, `PCGASMSetType()`,
1262db781477SPatrick Sanan           `PCBJACOBI`, `PCGASMGetSubKSP()`, `PCGASMSetSubdomains()`,
1263db781477SPatrick Sanan           `PCSetModifySubMatrices()`, `PCGASMSetOverlap()`, `PCGASMSetType()`
1264b1a0a8a3SJed Brown M*/
1265b1a0a8a3SJed Brown 
PCCreate_GASM(PC pc)1266d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PCCreate_GASM(PC pc)
1267d71ae5a4SJacob Faibussowitsch {
1268f746d493SDmitry Karpeev   PC_GASM *osm;
1269b1a0a8a3SJed Brown 
1270b1a0a8a3SJed Brown   PetscFunctionBegin;
12714dfa11a4SJacob Faibussowitsch   PetscCall(PetscNew(&osm));
12722fa5cd67SKarl Rupp 
12738f3b4b4dSDmitry Karpeev   osm->N                        = PETSC_DETERMINE;
127406b43e7eSDmitry Karpeev   osm->n                        = PETSC_DECIDE;
12758f3b4b4dSDmitry Karpeev   osm->nmax                     = PETSC_DETERMINE;
12768f3b4b4dSDmitry Karpeev   osm->overlap                  = 0;
12770a545947SLisandro Dalcin   osm->ksp                      = NULL;
12780a545947SLisandro Dalcin   osm->gorestriction            = NULL;
12790a545947SLisandro Dalcin   osm->girestriction            = NULL;
12800a545947SLisandro Dalcin   osm->pctoouter                = NULL;
12810a545947SLisandro Dalcin   osm->gx                       = NULL;
12820a545947SLisandro Dalcin   osm->gy                       = NULL;
12830a545947SLisandro Dalcin   osm->x                        = NULL;
12840a545947SLisandro Dalcin   osm->y                        = NULL;
12850a545947SLisandro Dalcin   osm->pcx                      = NULL;
12860a545947SLisandro Dalcin   osm->pcy                      = NULL;
12870a545947SLisandro Dalcin   osm->permutationIS            = NULL;
12880a545947SLisandro Dalcin   osm->permutationP             = NULL;
12890a545947SLisandro Dalcin   osm->pcmat                    = NULL;
12900a545947SLisandro Dalcin   osm->ois                      = NULL;
12910a545947SLisandro Dalcin   osm->iis                      = NULL;
12920a545947SLisandro Dalcin   osm->pmat                     = NULL;
1293f746d493SDmitry Karpeev   osm->type                     = PC_GASM_RESTRICT;
12946a4f0f73SDmitry Karpeev   osm->same_subdomain_solvers   = PETSC_TRUE;
1295b1a0a8a3SJed Brown   osm->sort_indices             = PETSC_TRUE;
1296d709ea83SDmitry Karpeev   osm->dm_subdomains            = PETSC_FALSE;
1297ea91fabdSFande Kong   osm->hierarchicalpartitioning = PETSC_FALSE;
1298b1a0a8a3SJed Brown 
1299b1a0a8a3SJed Brown   pc->data                 = (void *)osm;
1300f746d493SDmitry Karpeev   pc->ops->apply           = PCApply_GASM;
130148e38a8aSPierre Jolivet   pc->ops->matapply        = PCMatApply_GASM;
1302f746d493SDmitry Karpeev   pc->ops->applytranspose  = PCApplyTranspose_GASM;
1303f746d493SDmitry Karpeev   pc->ops->setup           = PCSetUp_GASM;
1304a06653b4SBarry Smith   pc->ops->reset           = PCReset_GASM;
1305f746d493SDmitry Karpeev   pc->ops->destroy         = PCDestroy_GASM;
1306f746d493SDmitry Karpeev   pc->ops->setfromoptions  = PCSetFromOptions_GASM;
1307f746d493SDmitry Karpeev   pc->ops->setuponblocks   = PCSetUpOnBlocks_GASM;
1308f746d493SDmitry Karpeev   pc->ops->view            = PCView_GASM;
13090a545947SLisandro Dalcin   pc->ops->applyrichardson = NULL;
1310b1a0a8a3SJed Brown 
13119566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetSubdomains_C", PCGASMSetSubdomains_GASM));
13129566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetOverlap_C", PCGASMSetOverlap_GASM));
13139566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetType_C", PCGASMSetType_GASM));
13149566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetSortIndices_C", PCGASMSetSortIndices_GASM));
13159566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMGetSubKSP_C", PCGASMGetSubKSP_GASM));
13163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1317b1a0a8a3SJed Brown }
1318b1a0a8a3SJed Brown 
PCGASMCreateLocalSubdomains(Mat A,PetscInt nloc,IS * iis[])1319d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGASMCreateLocalSubdomains(Mat A, PetscInt nloc, IS *iis[])
1320d71ae5a4SJacob Faibussowitsch {
1321b1a0a8a3SJed Brown   MatPartitioning mpart;
1322b1a0a8a3SJed Brown   const char     *prefix;
1323b1a0a8a3SJed Brown   PetscInt        i, j, rstart, rend, bs;
1324976e8c5aSLisandro Dalcin   PetscBool       hasop, isbaij = PETSC_FALSE, foundpart = PETSC_FALSE;
13250298fd71SBarry Smith   Mat             Ad = NULL, adj;
1326b1a0a8a3SJed Brown   IS              ispart, isnumb, *is;
1327b1a0a8a3SJed Brown 
1328b1a0a8a3SJed Brown   PetscFunctionBegin;
132963a3b9bcSJacob Faibussowitsch   PetscCheck(nloc >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "number of local subdomains must > 0, got nloc = %" PetscInt_FMT, nloc);
1330b1a0a8a3SJed Brown 
1331b1a0a8a3SJed Brown   /* Get prefix, row distribution, and block size */
13329566063dSJacob Faibussowitsch   PetscCall(MatGetOptionsPrefix(A, &prefix));
13339566063dSJacob Faibussowitsch   PetscCall(MatGetOwnershipRange(A, &rstart, &rend));
13349566063dSJacob Faibussowitsch   PetscCall(MatGetBlockSize(A, &bs));
13352472a847SBarry 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);
1336b1a0a8a3SJed Brown 
1337b1a0a8a3SJed Brown   /* Get diagonal block from matrix if possible */
13389566063dSJacob Faibussowitsch   PetscCall(MatHasOperation(A, MATOP_GET_DIAGONAL_BLOCK, &hasop));
133948a46eb9SPierre Jolivet   if (hasop) PetscCall(MatGetDiagonalBlock(A, &Ad));
1340b1a0a8a3SJed Brown   if (Ad) {
13419566063dSJacob Faibussowitsch     PetscCall(PetscObjectBaseTypeCompare((PetscObject)Ad, MATSEQBAIJ, &isbaij));
13429566063dSJacob Faibussowitsch     if (!isbaij) PetscCall(PetscObjectBaseTypeCompare((PetscObject)Ad, MATSEQSBAIJ, &isbaij));
1343b1a0a8a3SJed Brown   }
13448f3b4b4dSDmitry Karpeev   if (Ad && nloc > 1) {
1345b1a0a8a3SJed Brown     PetscBool match, done;
1346b1a0a8a3SJed Brown     /* Try to setup a good matrix partitioning if available */
13479566063dSJacob Faibussowitsch     PetscCall(MatPartitioningCreate(PETSC_COMM_SELF, &mpart));
13489566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)mpart, prefix));
13499566063dSJacob Faibussowitsch     PetscCall(MatPartitioningSetFromOptions(mpart));
13509566063dSJacob Faibussowitsch     PetscCall(PetscObjectTypeCompare((PetscObject)mpart, MATPARTITIONINGCURRENT, &match));
135148a46eb9SPierre Jolivet     if (!match) PetscCall(PetscObjectTypeCompare((PetscObject)mpart, MATPARTITIONINGSQUARE, &match));
1352b1a0a8a3SJed Brown     if (!match) { /* assume a "good" partitioner is available */
13531a83f524SJed Brown       PetscInt        na;
13541a83f524SJed Brown       const PetscInt *ia, *ja;
13559566063dSJacob Faibussowitsch       PetscCall(MatGetRowIJ(Ad, 0, PETSC_TRUE, isbaij, &na, &ia, &ja, &done));
1356b1a0a8a3SJed Brown       if (done) {
1357b1a0a8a3SJed Brown         /* Build adjacency matrix by hand. Unfortunately a call to
1358b1a0a8a3SJed Brown            MatConvert(Ad,MATMPIADJ,MAT_INITIAL_MATRIX,&adj) will
1359b1a0a8a3SJed Brown            remove the block-aij structure and we cannot expect
1360b1a0a8a3SJed Brown            MatPartitioning to split vertices as we need */
13610a545947SLisandro Dalcin         PetscInt        i, j, len, nnz, cnt, *iia = NULL, *jja = NULL;
13621a83f524SJed Brown         const PetscInt *row;
1363b1a0a8a3SJed Brown         nnz = 0;
1364b1a0a8a3SJed Brown         for (i = 0; i < na; i++) { /* count number of nonzeros */
1365b1a0a8a3SJed Brown           len = ia[i + 1] - ia[i];
1366b1a0a8a3SJed Brown           row = ja + ia[i];
1367b1a0a8a3SJed Brown           for (j = 0; j < len; j++) {
1368b1a0a8a3SJed Brown             if (row[j] == i) { /* don't count diagonal */
13699371c9d4SSatish Balay               len--;
13709371c9d4SSatish Balay               break;
1371b1a0a8a3SJed Brown             }
1372b1a0a8a3SJed Brown           }
1373b1a0a8a3SJed Brown           nnz += len;
1374b1a0a8a3SJed Brown         }
13759566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1(na + 1, &iia));
13769566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1(nnz, &jja));
1377b1a0a8a3SJed Brown         nnz    = 0;
1378b1a0a8a3SJed Brown         iia[0] = 0;
1379b1a0a8a3SJed Brown         for (i = 0; i < na; i++) { /* fill adjacency */
1380b1a0a8a3SJed Brown           cnt = 0;
1381b1a0a8a3SJed Brown           len = ia[i + 1] - ia[i];
1382b1a0a8a3SJed Brown           row = ja + ia[i];
1383b1a0a8a3SJed Brown           for (j = 0; j < len; j++) {
13842fa5cd67SKarl Rupp             if (row[j] != i) jja[nnz + cnt++] = row[j]; /* if not diagonal */
1385b1a0a8a3SJed Brown           }
1386b1a0a8a3SJed Brown           nnz += cnt;
1387b1a0a8a3SJed Brown           iia[i + 1] = nnz;
1388b1a0a8a3SJed Brown         }
1389b1a0a8a3SJed Brown         /* Partitioning of the adjacency matrix */
13909566063dSJacob Faibussowitsch         PetscCall(MatCreateMPIAdj(PETSC_COMM_SELF, na, na, iia, jja, NULL, &adj));
13919566063dSJacob Faibussowitsch         PetscCall(MatPartitioningSetAdjacency(mpart, adj));
13929566063dSJacob Faibussowitsch         PetscCall(MatPartitioningSetNParts(mpart, nloc));
13939566063dSJacob Faibussowitsch         PetscCall(MatPartitioningApply(mpart, &ispart));
13949566063dSJacob Faibussowitsch         PetscCall(ISPartitioningToNumbering(ispart, &isnumb));
13959566063dSJacob Faibussowitsch         PetscCall(MatDestroy(&adj));
1396b1a0a8a3SJed Brown         foundpart = PETSC_TRUE;
1397b1a0a8a3SJed Brown       }
13989566063dSJacob Faibussowitsch       PetscCall(MatRestoreRowIJ(Ad, 0, PETSC_TRUE, isbaij, &na, &ia, &ja, &done));
1399b1a0a8a3SJed Brown     }
14009566063dSJacob Faibussowitsch     PetscCall(MatPartitioningDestroy(&mpart));
1401b1a0a8a3SJed Brown   }
14029566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(nloc, &is));
1403b1a0a8a3SJed Brown   if (!foundpart) {
1404b1a0a8a3SJed Brown     /* Partitioning by contiguous chunks of rows */
1405b1a0a8a3SJed Brown 
1406b1a0a8a3SJed Brown     PetscInt mbs   = (rend - rstart) / bs;
1407b1a0a8a3SJed Brown     PetscInt start = rstart;
14088f3b4b4dSDmitry Karpeev     for (i = 0; i < nloc; i++) {
14098f3b4b4dSDmitry Karpeev       PetscInt count = (mbs / nloc + ((mbs % nloc) > i)) * bs;
14109566063dSJacob Faibussowitsch       PetscCall(ISCreateStride(PETSC_COMM_SELF, count, start, 1, &is[i]));
1411b1a0a8a3SJed Brown       start += count;
1412b1a0a8a3SJed Brown     }
1413b1a0a8a3SJed Brown 
1414b1a0a8a3SJed Brown   } else {
1415b1a0a8a3SJed Brown     /* Partitioning by adjacency of diagonal block  */
1416b1a0a8a3SJed Brown 
1417b1a0a8a3SJed Brown     const PetscInt *numbering;
1418b1a0a8a3SJed Brown     PetscInt       *count, nidx, *indices, *newidx, start = 0;
1419b1a0a8a3SJed Brown     /* Get node count in each partition */
14209566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nloc, &count));
14219566063dSJacob Faibussowitsch     PetscCall(ISPartitioningCount(ispart, nloc, count));
1422b1a0a8a3SJed Brown     if (isbaij && bs > 1) { /* adjust for the block-aij case */
14238f3b4b4dSDmitry Karpeev       for (i = 0; i < nloc; i++) count[i] *= bs;
1424b1a0a8a3SJed Brown     }
1425b1a0a8a3SJed Brown     /* Build indices from node numbering */
14269566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(isnumb, &nidx));
14279566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nidx, &indices));
1428b1a0a8a3SJed Brown     for (i = 0; i < nidx; i++) indices[i] = i; /* needs to be initialized */
14299566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(isnumb, &numbering));
14309566063dSJacob Faibussowitsch     PetscCall(PetscSortIntWithPermutation(nidx, numbering, indices));
14319566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(isnumb, &numbering));
1432b1a0a8a3SJed Brown     if (isbaij && bs > 1) { /* adjust for the block-aij case */
14339566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nidx * bs, &newidx));
14342fa5cd67SKarl Rupp       for (i = 0; i < nidx; i++) {
14352fa5cd67SKarl Rupp         for (j = 0; j < bs; j++) newidx[i * bs + j] = indices[i] * bs + j;
14362fa5cd67SKarl Rupp       }
14379566063dSJacob Faibussowitsch       PetscCall(PetscFree(indices));
1438b1a0a8a3SJed Brown       nidx *= bs;
1439b1a0a8a3SJed Brown       indices = newidx;
1440b1a0a8a3SJed Brown     }
1441b1a0a8a3SJed Brown     /* Shift to get global indices */
1442b1a0a8a3SJed Brown     for (i = 0; i < nidx; i++) indices[i] += rstart;
1443b1a0a8a3SJed Brown 
1444b1a0a8a3SJed Brown     /* Build the index sets for each block */
14458f3b4b4dSDmitry Karpeev     for (i = 0; i < nloc; i++) {
14469566063dSJacob Faibussowitsch       PetscCall(ISCreateGeneral(PETSC_COMM_SELF, count[i], &indices[start], PETSC_COPY_VALUES, &is[i]));
14479566063dSJacob Faibussowitsch       PetscCall(ISSort(is[i]));
1448b1a0a8a3SJed Brown       start += count[i];
1449b1a0a8a3SJed Brown     }
1450b1a0a8a3SJed Brown 
14519566063dSJacob Faibussowitsch     PetscCall(PetscFree(count));
14529566063dSJacob Faibussowitsch     PetscCall(PetscFree(indices));
14539566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&isnumb));
14549566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&ispart));
1455b1a0a8a3SJed Brown   }
14566a4f0f73SDmitry Karpeev   *iis = is;
14573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
14588f3b4b4dSDmitry Karpeev }
14598f3b4b4dSDmitry Karpeev 
PCGASMCreateStraddlingSubdomains(Mat A,PetscInt N,PetscInt * n,IS * iis[])1460d71ae5a4SJacob Faibussowitsch PETSC_INTERN PetscErrorCode PCGASMCreateStraddlingSubdomains(Mat A, PetscInt N, PetscInt *n, IS *iis[])
1461d71ae5a4SJacob Faibussowitsch {
14628f3b4b4dSDmitry Karpeev   PetscFunctionBegin;
14639566063dSJacob Faibussowitsch   PetscCall(MatSubdomainsCreateCoalesce(A, N, n, iis));
14643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
14658f3b4b4dSDmitry Karpeev }
14668f3b4b4dSDmitry Karpeev 
14678f3b4b4dSDmitry Karpeev /*@C
1468f36f9100SBarry Smith   PCGASMCreateSubdomains - Creates `n` index sets defining `n` nonoverlapping subdomains on this MPI process for the `PCGASM` additive
14698f3b4b4dSDmitry Karpeev   Schwarz preconditioner for a any problem based on its matrix.
14708f3b4b4dSDmitry Karpeev 
14718f3b4b4dSDmitry Karpeev   Collective
14728f3b4b4dSDmitry Karpeev 
14738f3b4b4dSDmitry Karpeev   Input Parameters:
14748f3b4b4dSDmitry Karpeev + A - The global matrix operator
14758f3b4b4dSDmitry Karpeev - N - the number of global subdomains requested
14768f3b4b4dSDmitry Karpeev 
14778f3b4b4dSDmitry Karpeev   Output Parameters:
1478f13dfd9eSBarry Smith + n   - the number of subdomains created on this MPI process
14798f3b4b4dSDmitry Karpeev - iis - the array of index sets defining the local inner subdomains (on which the correction is applied)
14808f3b4b4dSDmitry Karpeev 
14818f3b4b4dSDmitry Karpeev   Level: advanced
14828f3b4b4dSDmitry Karpeev 
1483f36f9100SBarry Smith   Notes:
1484f36f9100SBarry Smith   When `N` >= A's communicator size, each subdomain is local -- contained within a single MPI process.
1485f13dfd9eSBarry Smith   When `N` < size, the subdomains are 'straddling' (process boundaries) and are no longer local.
148620f4b53cSBarry Smith   The resulting subdomains can be use in `PCGASMSetSubdomains`(pc,n,iss,`NULL`).  The overlapping
14878f3b4b4dSDmitry Karpeev   outer subdomains will be automatically generated from these according to the requested amount of
14888f3b4b4dSDmitry Karpeev   overlap; this is currently supported only with local subdomains.
14898f3b4b4dSDmitry Karpeev 
1490f36f9100SBarry Smith   Use `PCGASMDestroySubdomains()` to free the array and the list of index sets.
1491f36f9100SBarry Smith 
1492562efe2eSBarry Smith .seealso: [](ch_ksp), `PCGASM`, `PCGASMSetSubdomains()`, `PCGASMDestroySubdomains()`
14938f3b4b4dSDmitry Karpeev @*/
PCGASMCreateSubdomains(Mat A,PetscInt N,PetscInt * n,IS * iis[])1494d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGASMCreateSubdomains(Mat A, PetscInt N, PetscInt *n, IS *iis[])
1495d71ae5a4SJacob Faibussowitsch {
14968f3b4b4dSDmitry Karpeev   PetscMPIInt size;
14978f3b4b4dSDmitry Karpeev 
14988f3b4b4dSDmitry Karpeev   PetscFunctionBegin;
14998f3b4b4dSDmitry Karpeev   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
15004f572ea9SToby Isaac   PetscAssertPointer(iis, 4);
15018f3b4b4dSDmitry Karpeev 
150263a3b9bcSJacob Faibussowitsch   PetscCheck(N >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of subdomains must be > 0, N = %" PetscInt_FMT, N);
15039566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)A), &size));
15048f3b4b4dSDmitry Karpeev   if (N >= size) {
15058f3b4b4dSDmitry Karpeev     *n = N / size + (N % size);
15069566063dSJacob Faibussowitsch     PetscCall(PCGASMCreateLocalSubdomains(A, *n, iis));
15076a4f0f73SDmitry Karpeev   } else {
15089566063dSJacob Faibussowitsch     PetscCall(PCGASMCreateStraddlingSubdomains(A, N, n, iis));
15096a4f0f73SDmitry Karpeev   }
15103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1511b1a0a8a3SJed Brown }
1512b1a0a8a3SJed Brown 
1513b1a0a8a3SJed Brown /*@C
1514f746d493SDmitry Karpeev   PCGASMDestroySubdomains - Destroys the index sets created with
1515f1580f4eSBarry Smith   `PCGASMCreateSubdomains()` or `PCGASMCreateSubdomains2D()`. Should be
1516f1580f4eSBarry Smith   called after setting subdomains with `PCGASMSetSubdomains()`.
1517b1a0a8a3SJed Brown 
1518b1a0a8a3SJed Brown   Collective
1519b1a0a8a3SJed Brown 
1520b1a0a8a3SJed Brown   Input Parameters:
1521b1a0a8a3SJed Brown + n   - the number of index sets
1522f36f9100SBarry Smith . iis - the array of inner subdomains
1523f36f9100SBarry Smith - ois - the array of outer subdomains, can be `NULL`
1524b1a0a8a3SJed Brown 
15256a4f0f73SDmitry Karpeev   Level: intermediate
15266a4f0f73SDmitry Karpeev 
1527f1580f4eSBarry Smith   Note:
1528f1580f4eSBarry Smith   This is a convenience subroutine that walks each list,
1529f1580f4eSBarry Smith   destroys each `IS` on the list, and then frees the list. At the end the
153020f4b53cSBarry Smith   list pointers are set to `NULL`.
1531b1a0a8a3SJed Brown 
1532562efe2eSBarry Smith .seealso: [](ch_ksp), `PCGASM`, `PCGASMCreateSubdomains()`, `PCGASMSetSubdomains()`
1533b1a0a8a3SJed Brown @*/
PCGASMDestroySubdomains(PetscInt n,IS * iis[],IS * ois[])1534a3b724e8SBarry Smith PetscErrorCode PCGASMDestroySubdomains(PetscInt n, IS *iis[], IS *ois[])
1535d71ae5a4SJacob Faibussowitsch {
1536b1a0a8a3SJed Brown   PetscInt i;
15375fd66863SKarl Rupp 
1538b1a0a8a3SJed Brown   PetscFunctionBegin;
15393ba16761SJacob Faibussowitsch   if (n <= 0) PetscFunctionReturn(PETSC_SUCCESS);
15406a4f0f73SDmitry Karpeev   if (ois) {
15414f572ea9SToby Isaac     PetscAssertPointer(ois, 3);
15422c112581SDmitry Karpeev     if (*ois) {
15434f572ea9SToby Isaac       PetscAssertPointer(*ois, 3);
154448a46eb9SPierre Jolivet       for (i = 0; i < n; i++) PetscCall(ISDestroy(&(*ois)[i]));
1545f4f49eeaSPierre Jolivet       PetscCall(PetscFree(*ois));
15462c112581SDmitry Karpeev     }
1547b1a0a8a3SJed Brown   }
1548b9d0fdaaSFande Kong   if (iis) {
15494f572ea9SToby Isaac     PetscAssertPointer(iis, 2);
1550b9d0fdaaSFande Kong     if (*iis) {
15514f572ea9SToby Isaac       PetscAssertPointer(*iis, 2);
155248a46eb9SPierre Jolivet       for (i = 0; i < n; i++) PetscCall(ISDestroy(&(*iis)[i]));
1553f4f49eeaSPierre Jolivet       PetscCall(PetscFree(*iis));
1554b9d0fdaaSFande Kong     }
1555b9d0fdaaSFande Kong   }
15563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1557b1a0a8a3SJed Brown }
1558b1a0a8a3SJed Brown 
1559af538404SDmitry Karpeev #define PCGASMLocalSubdomainBounds2D(M, N, xleft, ylow, xright, yhigh, first, last, xleft_loc, ylow_loc, xright_loc, yhigh_loc, n) \
1560a8f51744SPierre Jolivet   do { \
1561af538404SDmitry Karpeev     PetscInt first_row = first / M, last_row = last / M + 1; \
1562af538404SDmitry Karpeev     /*                                                                                                    \
1563af538404SDmitry Karpeev      Compute ylow_loc and yhigh_loc so that (ylow_loc,xleft) and (yhigh_loc,xright) are the corners       \
1564af538404SDmitry Karpeev      of the bounding box of the intersection of the subdomain with the local ownership range (local       \
1565af538404SDmitry Karpeev      subdomain).                                                                                          \
1566af538404SDmitry Karpeev      Also compute xleft_loc and xright_loc as the lower and upper bounds on the first and last rows       \
1567af538404SDmitry Karpeev      of the intersection.                                                                                 \
1568af538404SDmitry Karpeev     */ \
1569af538404SDmitry Karpeev     /* ylow_loc is the grid row containing the first element of the local sumbdomain */ \
1570eec7e3faSDmitry Karpeev     *ylow_loc = PetscMax(first_row, ylow); \
1571af538404SDmitry Karpeev     /* xleft_loc is the offset of first element of the local subdomain within its grid row (might actually be outside the local subdomain) */ \
1572eec7e3faSDmitry Karpeev     *xleft_loc = *ylow_loc == first_row ? PetscMax(first % M, xleft) : xleft; \
1573af538404SDmitry Karpeev     /* yhigh_loc is the grid row above the last local subdomain element */ \
1574eec7e3faSDmitry Karpeev     *yhigh_loc = PetscMin(last_row, yhigh); \
1575af538404SDmitry Karpeev     /* xright is the offset of the end of the  local subdomain within its grid row (might actually be outside the local subdomain) */ \
1576eec7e3faSDmitry Karpeev     *xright_loc = *yhigh_loc == last_row ? PetscMin(xright, last % M) : xright; \
1577af538404SDmitry Karpeev     /* Now compute the size of the local subdomain n. */ \
1578c3518bceSDmitry Karpeev     *n = 0; \
1579eec7e3faSDmitry Karpeev     if (*ylow_loc < *yhigh_loc) { \
1580af538404SDmitry Karpeev       PetscInt width = xright - xleft; \
1581eec7e3faSDmitry Karpeev       *n += width * (*yhigh_loc - *ylow_loc - 1); \
1582eec7e3faSDmitry Karpeev       *n += PetscMin(PetscMax(*xright_loc - xleft, 0), width); \
1583eec7e3faSDmitry Karpeev       *n -= PetscMin(PetscMax(*xleft_loc - xleft, 0), width); \
1584af538404SDmitry Karpeev     } \
1585a8f51744SPierre Jolivet   } while (0)
1586af538404SDmitry Karpeev 
1587f36f9100SBarry Smith /*@C
1588f1580f4eSBarry Smith   PCGASMCreateSubdomains2D - Creates the index sets for the `PCGASM` overlapping Schwarz
1589b1a0a8a3SJed Brown   preconditioner for a two-dimensional problem on a regular grid.
1590b1a0a8a3SJed Brown 
1591af538404SDmitry Karpeev   Collective
1592b1a0a8a3SJed Brown 
1593b1a0a8a3SJed Brown   Input Parameters:
15946b867d5aSJose E. Roman + pc       - the preconditioner context
15956b867d5aSJose E. Roman . M        - the global number of grid points in the x direction
15966b867d5aSJose E. Roman . N        - the global number of grid points in the y direction
15976b867d5aSJose E. Roman . Mdomains - the global number of subdomains in the x direction
15986b867d5aSJose E. Roman . Ndomains - the global number of subdomains in the y direction
1599b1a0a8a3SJed Brown . dof      - degrees of freedom per node
1600b1a0a8a3SJed Brown - overlap  - overlap in mesh lines
1601b1a0a8a3SJed Brown 
1602b1a0a8a3SJed Brown   Output Parameters:
1603feefa0e1SJacob Faibussowitsch + nsub - the number of local subdomains created
16046a4f0f73SDmitry Karpeev . iis  - array of index sets defining inner (nonoverlapping) subdomains
16056a4f0f73SDmitry Karpeev - ois  - array of index sets defining outer (overlapping, if overlap > 0) subdomains
1606b1a0a8a3SJed Brown 
1607b1a0a8a3SJed Brown   Level: advanced
1608b1a0a8a3SJed Brown 
1609f36f9100SBarry Smith   Note:
1610f36f9100SBarry Smith   Use `PCGASMDestroySubdomains()` to free the index sets and the arrays
1611f36f9100SBarry Smith 
1612562efe2eSBarry Smith .seealso: [](ch_ksp), `PCGASM`, `PCGASMSetSubdomains()`, `PCGASMGetSubKSP()`, `PCGASMSetOverlap()`, `PCASMCreateSubdomains2D()`,
1613f36f9100SBarry Smith           `PCGASMDestroySubdomains()`
1614b1a0a8a3SJed Brown @*/
PCGASMCreateSubdomains2D(PC pc,PetscInt M,PetscInt N,PetscInt Mdomains,PetscInt Ndomains,PetscInt dof,PetscInt overlap,PetscInt * nsub,IS * iis[],IS * ois[])1615a3b724e8SBarry Smith PetscErrorCode PCGASMCreateSubdomains2D(PC pc, PetscInt M, PetscInt N, PetscInt Mdomains, PetscInt Ndomains, PetscInt dof, PetscInt overlap, PetscInt *nsub, IS *iis[], IS *ois[])
1616d71ae5a4SJacob Faibussowitsch {
16172bbb417fSDmitry Karpeev   PetscMPIInt size, rank;
16182bbb417fSDmitry Karpeev   PetscInt    i, j;
16192bbb417fSDmitry Karpeev   PetscInt    maxheight, maxwidth;
16202bbb417fSDmitry Karpeev   PetscInt    xstart, xleft, xright, xleft_loc, xright_loc;
16212bbb417fSDmitry Karpeev   PetscInt    ystart, ylow, yhigh, ylow_loc, yhigh_loc;
1622eec7e3faSDmitry Karpeev   PetscInt    x[2][2], y[2][2], n[2];
16232bbb417fSDmitry Karpeev   PetscInt    first, last;
16242bbb417fSDmitry Karpeev   PetscInt    nidx, *idx;
16252bbb417fSDmitry Karpeev   PetscInt    ii, jj, s, q, d;
1626af538404SDmitry Karpeev   PetscInt    k, kk;
16272bbb417fSDmitry Karpeev   PetscMPIInt color;
16282bbb417fSDmitry Karpeev   MPI_Comm    comm, subcomm;
16290a545947SLisandro Dalcin   IS        **xis = NULL, **is = ois, **is_local = iis;
1630d34fcf5fSBarry Smith 
1631b1a0a8a3SJed Brown   PetscFunctionBegin;
16329566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)pc, &comm));
16339566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
16349566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
16359566063dSJacob Faibussowitsch   PetscCall(MatGetOwnershipRange(pc->pmat, &first, &last));
16369371c9d4SSatish Balay   PetscCheck((first % dof) == 0 && (last % dof) == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
16379371c9d4SSatish Balay              "Matrix row partitioning unsuitable for domain decomposition: local row range (%" PetscInt_FMT ",%" PetscInt_FMT ") "
16389371c9d4SSatish Balay              "does not respect the number of degrees of freedom per grid point %" PetscInt_FMT,
16399371c9d4SSatish Balay              first, last, dof);
1640d34fcf5fSBarry Smith 
1641af538404SDmitry Karpeev   /* Determine the number of domains with nonzero intersections with the local ownership range. */
16422bbb417fSDmitry Karpeev   s      = 0;
1643af538404SDmitry Karpeev   ystart = 0;
1644af538404SDmitry Karpeev   for (j = 0; j < Ndomains; ++j) {
1645af538404SDmitry Karpeev     maxheight = N / Ndomains + ((N % Ndomains) > j); /* Maximal height of subdomain */
1646da81f932SPierre Jolivet     PetscCheck(maxheight >= 2, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many %" PetscInt_FMT " subdomains in the vertical direction for mesh height %" PetscInt_FMT, Ndomains, N);
1647eec7e3faSDmitry Karpeev     /* Vertical domain limits with an overlap. */
1648eec7e3faSDmitry Karpeev     ylow   = PetscMax(ystart - overlap, 0);
1649eec7e3faSDmitry Karpeev     yhigh  = PetscMin(ystart + maxheight + overlap, N);
1650b1a0a8a3SJed Brown     xstart = 0;
1651af538404SDmitry Karpeev     for (i = 0; i < Mdomains; ++i) {
1652af538404SDmitry Karpeev       maxwidth = M / Mdomains + ((M % Mdomains) > i); /* Maximal width of subdomain */
165363a3b9bcSJacob 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);
1654eec7e3faSDmitry Karpeev       /* Horizontal domain limits with an overlap. */
1655eec7e3faSDmitry Karpeev       xleft  = PetscMax(xstart - overlap, 0);
1656eec7e3faSDmitry Karpeev       xright = PetscMin(xstart + maxwidth + overlap, M);
1657af538404SDmitry Karpeev       /*
1658f1580f4eSBarry Smith          Determine whether this subdomain intersects this rank's ownership range of pc->pmat.
1659af538404SDmitry Karpeev       */
1660c3518bceSDmitry Karpeev       PCGASMLocalSubdomainBounds2D(M, N, xleft, ylow, xright, yhigh, first, last, (&xleft_loc), (&ylow_loc), (&xright_loc), (&yhigh_loc), (&nidx));
16612fa5cd67SKarl Rupp       if (nidx) ++s;
1662af538404SDmitry Karpeev       xstart += maxwidth;
1663af538404SDmitry Karpeev     } /* for (i = 0; i < Mdomains; ++i) */
1664af538404SDmitry Karpeev     ystart += maxheight;
1665af538404SDmitry Karpeev   } /* for (j = 0; j < Ndomains; ++j) */
16662fa5cd67SKarl Rupp 
1667af538404SDmitry Karpeev   /* Now we can allocate the necessary number of ISs. */
1668af538404SDmitry Karpeev   *nsub = s;
16699566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(*nsub, is));
16709566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(*nsub, is_local));
1671af538404SDmitry Karpeev   s      = 0;
1672af538404SDmitry Karpeev   ystart = 0;
1673af538404SDmitry Karpeev   for (j = 0; j < Ndomains; ++j) {
1674af538404SDmitry Karpeev     maxheight = N / Ndomains + ((N % Ndomains) > j); /* Maximal height of subdomain */
1675da81f932SPierre Jolivet     PetscCheck(maxheight >= 2, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many %" PetscInt_FMT " subdomains in the vertical direction for mesh height %" PetscInt_FMT, Ndomains, N);
1676af538404SDmitry Karpeev     /* Vertical domain limits with an overlap. */
1677eec7e3faSDmitry Karpeev     y[0][0] = PetscMax(ystart - overlap, 0);
1678eec7e3faSDmitry Karpeev     y[0][1] = PetscMin(ystart + maxheight + overlap, N);
1679eec7e3faSDmitry Karpeev     /* Vertical domain limits without an overlap. */
1680eec7e3faSDmitry Karpeev     y[1][0] = ystart;
1681eec7e3faSDmitry Karpeev     y[1][1] = PetscMin(ystart + maxheight, N);
1682eec7e3faSDmitry Karpeev     xstart  = 0;
1683af538404SDmitry Karpeev     for (i = 0; i < Mdomains; ++i) {
1684af538404SDmitry Karpeev       maxwidth = M / Mdomains + ((M % Mdomains) > i); /* Maximal width of subdomain */
168563a3b9bcSJacob 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);
1686af538404SDmitry Karpeev       /* Horizontal domain limits with an overlap. */
1687eec7e3faSDmitry Karpeev       x[0][0] = PetscMax(xstart - overlap, 0);
1688eec7e3faSDmitry Karpeev       x[0][1] = PetscMin(xstart + maxwidth + overlap, M);
1689eec7e3faSDmitry Karpeev       /* Horizontal domain limits without an overlap. */
1690eec7e3faSDmitry Karpeev       x[1][0] = xstart;
1691eec7e3faSDmitry Karpeev       x[1][1] = PetscMin(xstart + maxwidth, M);
16922bbb417fSDmitry Karpeev       /*
1693f1580f4eSBarry Smith          Determine whether this domain intersects this rank's ownership range of pc->pmat.
16942bbb417fSDmitry Karpeev          Do this twice: first for the domains with overlaps, and once without.
16952bbb417fSDmitry Karpeev          During the first pass create the subcommunicators, and use them on the second pass as well.
16962bbb417fSDmitry Karpeev       */
16972bbb417fSDmitry Karpeev       for (q = 0; q < 2; ++q) {
1698cc96b2e5SBarry Smith         PetscBool split = PETSC_FALSE;
16992bbb417fSDmitry Karpeev         /*
17002bbb417fSDmitry Karpeev           domain limits, (xleft, xright) and (ylow, yheigh) are adjusted
17012bbb417fSDmitry Karpeev           according to whether the domain with an overlap or without is considered.
17022bbb417fSDmitry Karpeev         */
17039371c9d4SSatish Balay         xleft  = x[q][0];
17049371c9d4SSatish Balay         xright = x[q][1];
17059371c9d4SSatish Balay         ylow   = y[q][0];
17069371c9d4SSatish Balay         yhigh  = y[q][1];
1707c3518bceSDmitry Karpeev         PCGASMLocalSubdomainBounds2D(M, N, xleft, ylow, xright, yhigh, first, last, (&xleft_loc), (&ylow_loc), (&xright_loc), (&yhigh_loc), (&nidx));
1708af538404SDmitry Karpeev         nidx *= dof;
1709eec7e3faSDmitry Karpeev         n[q] = nidx;
17102bbb417fSDmitry Karpeev         /*
1711eec7e3faSDmitry Karpeev          Based on the counted number of indices in the local domain *with an overlap*,
1712f1580f4eSBarry Smith          construct a subcommunicator of all the MPI ranks supporting this domain.
1713eec7e3faSDmitry Karpeev          Observe that a domain with an overlap might have nontrivial local support,
1714eec7e3faSDmitry Karpeev          while the domain without an overlap might not.  Hence, the decision to participate
1715eec7e3faSDmitry Karpeev          in the subcommunicator must be based on the domain with an overlap.
17162bbb417fSDmitry Karpeev          */
17172bbb417fSDmitry Karpeev         if (q == 0) {
17182fa5cd67SKarl Rupp           if (nidx) color = 1;
17192fa5cd67SKarl Rupp           else color = MPI_UNDEFINED;
17209566063dSJacob Faibussowitsch           PetscCallMPI(MPI_Comm_split(comm, color, rank, &subcomm));
1721cc96b2e5SBarry Smith           split = PETSC_TRUE;
17222bbb417fSDmitry Karpeev         }
1723af538404SDmitry Karpeev         /*
1724eec7e3faSDmitry Karpeev          Proceed only if the number of local indices *with an overlap* is nonzero.
1725af538404SDmitry Karpeev          */
1726eec7e3faSDmitry Karpeev         if (n[0]) {
17272fa5cd67SKarl Rupp           if (q == 0) xis = is;
1728af538404SDmitry Karpeev           if (q == 1) {
1729af538404SDmitry Karpeev             /*
1730eec7e3faSDmitry Karpeev              The IS for the no-overlap subdomain shares a communicator with the overlapping domain.
1731af538404SDmitry Karpeev              Moreover, if the overlap is zero, the two ISs are identical.
1732af538404SDmitry Karpeev              */
1733b1a0a8a3SJed Brown             if (overlap == 0) {
1734eec7e3faSDmitry Karpeev               (*is_local)[s] = (*is)[s];
17359566063dSJacob Faibussowitsch               PetscCall(PetscObjectReference((PetscObject)(*is)[s]));
17362bbb417fSDmitry Karpeev               continue;
1737d34fcf5fSBarry Smith             } else {
17386a4f0f73SDmitry Karpeev               xis     = is_local;
1739eec7e3faSDmitry Karpeev               subcomm = ((PetscObject)(*is)[s])->comm;
17402bbb417fSDmitry Karpeev             }
1741af538404SDmitry Karpeev           } /* if (q == 1) */
17420298fd71SBarry Smith           idx = NULL;
17439566063dSJacob Faibussowitsch           PetscCall(PetscMalloc1(nidx, &idx));
1744eec7e3faSDmitry Karpeev           if (nidx) {
17452bbb417fSDmitry Karpeev             k = 0;
17462bbb417fSDmitry Karpeev             for (jj = ylow_loc; jj < yhigh_loc; ++jj) {
1747af538404SDmitry Karpeev               PetscInt x0 = (jj == ylow_loc) ? xleft_loc : xleft;
1748af538404SDmitry Karpeev               PetscInt x1 = (jj == yhigh_loc - 1) ? xright_loc : xright;
1749af538404SDmitry Karpeev               kk          = dof * (M * jj + x0);
17502bbb417fSDmitry Karpeev               for (ii = x0; ii < x1; ++ii) {
1751ad540459SPierre Jolivet                 for (d = 0; d < dof; ++d) idx[k++] = kk++;
1752b1a0a8a3SJed Brown               }
1753b1a0a8a3SJed Brown             }
1754eec7e3faSDmitry Karpeev           }
17559566063dSJacob Faibussowitsch           PetscCall(ISCreateGeneral(subcomm, nidx, idx, PETSC_OWN_POINTER, (*xis) + s));
175648a46eb9SPierre Jolivet           if (split) PetscCallMPI(MPI_Comm_free(&subcomm));
1757eec7e3faSDmitry Karpeev         } /* if (n[0]) */
17582bbb417fSDmitry Karpeev       } /* for (q = 0; q < 2; ++q) */
17592fa5cd67SKarl Rupp       if (n[0]) ++s;
1760af538404SDmitry Karpeev       xstart += maxwidth;
1761af538404SDmitry Karpeev     } /* for (i = 0; i < Mdomains; ++i) */
17622bbb417fSDmitry Karpeev     ystart += maxheight;
1763af538404SDmitry Karpeev   } /* for (j = 0; j < Ndomains; ++j) */
17643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1765b1a0a8a3SJed Brown }
1766b1a0a8a3SJed Brown 
1767b1a0a8a3SJed Brown /*@C
1768f13dfd9eSBarry Smith   PCGASMGetSubdomains - Gets the subdomains supported on this MPI process
1769f1580f4eSBarry Smith   for the `PCGASM` additive Schwarz preconditioner.
1770b1a0a8a3SJed Brown 
1771b1a0a8a3SJed Brown   Not Collective
1772b1a0a8a3SJed Brown 
1773b1a0a8a3SJed Brown   Input Parameter:
1774b1a0a8a3SJed Brown . pc - the preconditioner context
1775b1a0a8a3SJed Brown 
1776b1a0a8a3SJed Brown   Output Parameters:
1777f13dfd9eSBarry Smith + n   - the number of subdomains for this MPI process (default value = 1)
1778f13dfd9eSBarry Smith . iis - the index sets that define the inner subdomains (without overlap) supported on this process (can be `NULL`)
1779f13dfd9eSBarry Smith - ois - the index sets that define the outer subdomains (with overlap) supported on this process (can be `NULL`)
178020f4b53cSBarry Smith 
178120f4b53cSBarry Smith   Level: advanced
1782b1a0a8a3SJed Brown 
1783f36f9100SBarry Smith   Notes:
1784f36f9100SBarry Smith   The user is responsible for destroying the `IS`s and freeing the returned arrays, this can be done with
1785f36f9100SBarry Smith   `PCGASMDestroySubdomains()`
1786f36f9100SBarry Smith 
1787f1580f4eSBarry Smith   The `IS` numbering is in the parallel, global numbering of the vector.
1788b1a0a8a3SJed Brown 
1789562efe2eSBarry Smith .seealso: [](ch_ksp), `PCGASM`, `PCGASMSetOverlap()`, `PCGASMGetSubKSP()`, `PCGASMCreateSubdomains2D()`,
1790f36f9100SBarry Smith           `PCGASMSetSubdomains()`, `PCGASMGetSubmatrices()`, `PCGASMDestroySubdomains()`
1791b1a0a8a3SJed Brown @*/
PCGASMGetSubdomains(PC pc,PetscInt * n,IS * iis[],IS * ois[])1792d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGASMGetSubdomains(PC pc, PetscInt *n, IS *iis[], IS *ois[])
1793d71ae5a4SJacob Faibussowitsch {
1794f746d493SDmitry Karpeev   PC_GASM  *osm;
1795b1a0a8a3SJed Brown   PetscBool match;
17966a4f0f73SDmitry Karpeev   PetscInt  i;
17975fd66863SKarl Rupp 
1798b1a0a8a3SJed Brown   PetscFunctionBegin;
1799b1a0a8a3SJed Brown   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
18009566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCGASM, &match));
180128b400f6SJacob Faibussowitsch   PetscCheck(match, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONG, "Incorrect object type: expected %s, got %s instead", PCGASM, ((PetscObject)pc)->type_name);
1802f746d493SDmitry Karpeev   osm = (PC_GASM *)pc->data;
1803ab3e923fSDmitry Karpeev   if (n) *n = osm->n;
18041baa6e33SBarry Smith   if (iis) PetscCall(PetscMalloc1(osm->n, iis));
18051baa6e33SBarry Smith   if (ois) PetscCall(PetscMalloc1(osm->n, ois));
18066a4f0f73SDmitry Karpeev   if (iis || ois) {
18076a4f0f73SDmitry Karpeev     for (i = 0; i < osm->n; ++i) {
18086a4f0f73SDmitry Karpeev       if (iis) (*iis)[i] = osm->iis[i];
18096a4f0f73SDmitry Karpeev       if (ois) (*ois)[i] = osm->ois[i];
18106a4f0f73SDmitry Karpeev     }
1811b1a0a8a3SJed Brown   }
18123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1813b1a0a8a3SJed Brown }
1814b1a0a8a3SJed Brown 
1815b1a0a8a3SJed Brown /*@C
1816f13dfd9eSBarry Smith   PCGASMGetSubmatrices - Gets the local submatrices (for this MPI process
1817f1580f4eSBarry Smith   only) for the `PCGASM` additive Schwarz preconditioner.
1818b1a0a8a3SJed Brown 
1819b1a0a8a3SJed Brown   Not Collective
1820b1a0a8a3SJed Brown 
1821b1a0a8a3SJed Brown   Input Parameter:
1822b1a0a8a3SJed Brown . pc - the preconditioner context
1823b1a0a8a3SJed Brown 
1824b1a0a8a3SJed Brown   Output Parameters:
1825f13dfd9eSBarry Smith + n   - the number of matrices for this MPI process (default value = 1)
1826b1a0a8a3SJed Brown - mat - the matrices
1827b1a0a8a3SJed Brown 
1828b1a0a8a3SJed Brown   Level: advanced
1829b1a0a8a3SJed Brown 
1830f36f9100SBarry Smith   Note:
1831f36f9100SBarry Smith   Matrices returned by this routine have the same communicators as the index sets (`IS`)
1832f36f9100SBarry Smith   used to define subdomains in `PCGASMSetSubdomains()`
1833f36f9100SBarry Smith 
1834562efe2eSBarry Smith .seealso: [](ch_ksp), `PCGASM`, `PCGASMSetOverlap()`, `PCGASMGetSubKSP()`,
1835db781477SPatrick Sanan           `PCGASMCreateSubdomains2D()`, `PCGASMSetSubdomains()`, `PCGASMGetSubdomains()`
1836b1a0a8a3SJed Brown @*/
PCGASMGetSubmatrices(PC pc,PetscInt * n,Mat * mat[])1837d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGASMGetSubmatrices(PC pc, PetscInt *n, Mat *mat[])
1838d71ae5a4SJacob Faibussowitsch {
1839f746d493SDmitry Karpeev   PC_GASM  *osm;
1840b1a0a8a3SJed Brown   PetscBool match;
1841b1a0a8a3SJed Brown 
1842b1a0a8a3SJed Brown   PetscFunctionBegin;
1843b1a0a8a3SJed Brown   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
18444f572ea9SToby Isaac   PetscAssertPointer(n, 2);
18454f572ea9SToby Isaac   if (mat) PetscAssertPointer(mat, 3);
184628b400f6SJacob Faibussowitsch   PetscCheck(pc->setupcalled, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Must call after KSPSetUp() or PCSetUp().");
18479566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCGASM, &match));
184828b400f6SJacob Faibussowitsch   PetscCheck(match, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONG, "Expected %s, got %s instead", PCGASM, ((PetscObject)pc)->type_name);
1849f746d493SDmitry Karpeev   osm = (PC_GASM *)pc->data;
1850ab3e923fSDmitry Karpeev   if (n) *n = osm->n;
1851b1a0a8a3SJed Brown   if (mat) *mat = osm->pmat;
18523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1853b1a0a8a3SJed Brown }
1854d709ea83SDmitry Karpeev 
1855d709ea83SDmitry Karpeev /*@
1856f1580f4eSBarry Smith   PCGASMSetUseDMSubdomains - Indicates whether to use `DMCreateDomainDecomposition()` to define the subdomains, whenever possible for `PCGASM`
1857f1580f4eSBarry Smith 
1858d709ea83SDmitry Karpeev   Logically Collective
1859d709ea83SDmitry Karpeev 
1860d8d19677SJose E. Roman   Input Parameters:
1861d709ea83SDmitry Karpeev + pc  - the preconditioner
1862f1580f4eSBarry Smith - flg - boolean indicating whether to use subdomains defined by the `DM`
1863d709ea83SDmitry Karpeev 
1864d709ea83SDmitry Karpeev   Options Database Key:
1865feefa0e1SJacob Faibussowitsch + -pc_gasm_dm_subdomains    - configure subdomains
1866feefa0e1SJacob Faibussowitsch . -pc_gasm_overlap          - set overlap
1867feefa0e1SJacob Faibussowitsch - -pc_gasm_total_subdomains - set number of subdomains
1868d709ea83SDmitry Karpeev 
1869d709ea83SDmitry Karpeev   Level: intermediate
1870d709ea83SDmitry Karpeev 
1871f1580f4eSBarry Smith   Note:
1872f1580f4eSBarry Smith   `PCGASMSetSubdomains()`, `PCGASMSetTotalSubdomains()` or `PCGASMSetOverlap()` take precedence over `PCGASMSetUseDMSubdomains()`,
1873f1580f4eSBarry Smith   so setting `PCGASMSetSubdomains()` with nontrivial subdomain ISs or any of `PCGASMSetTotalSubdomains()` and `PCGASMSetOverlap()`
18748f3b4b4dSDmitry Karpeev   automatically turns the latter off.
1875d709ea83SDmitry Karpeev 
1876562efe2eSBarry Smith .seealso: [](ch_ksp), `PCGASM`, `PCGASMGetUseDMSubdomains()`, `PCGASMSetSubdomains()`, `PCGASMSetOverlap()`
1877db781477SPatrick Sanan           `PCGASMCreateSubdomains2D()`
1878d709ea83SDmitry Karpeev @*/
PCGASMSetUseDMSubdomains(PC pc,PetscBool flg)1879d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGASMSetUseDMSubdomains(PC pc, PetscBool flg)
1880d71ae5a4SJacob Faibussowitsch {
1881d709ea83SDmitry Karpeev   PC_GASM  *osm = (PC_GASM *)pc->data;
1882d709ea83SDmitry Karpeev   PetscBool match;
1883d709ea83SDmitry Karpeev 
1884d709ea83SDmitry Karpeev   PetscFunctionBegin;
1885d709ea83SDmitry Karpeev   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1886d709ea83SDmitry Karpeev   PetscValidLogicalCollectiveBool(pc, flg, 2);
188728b400f6SJacob Faibussowitsch   PetscCheck(!pc->setupcalled, ((PetscObject)pc)->comm, PETSC_ERR_ARG_WRONGSTATE, "Not for a setup PC.");
18889566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCGASM, &match));
1889d709ea83SDmitry Karpeev   if (match) {
1890ad540459SPierre Jolivet     if (!osm->user_subdomains && osm->N == PETSC_DETERMINE && osm->overlap < 0) osm->dm_subdomains = flg;
18918f3b4b4dSDmitry Karpeev   }
18923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1893d709ea83SDmitry Karpeev }
1894d709ea83SDmitry Karpeev 
1895d709ea83SDmitry Karpeev /*@
1896f1580f4eSBarry Smith   PCGASMGetUseDMSubdomains - Returns flag indicating whether to use `DMCreateDomainDecomposition()` to define the subdomains, whenever possible with `PCGASM`
1897f1580f4eSBarry Smith 
1898d709ea83SDmitry Karpeev   Not Collective
1899d709ea83SDmitry Karpeev 
1900d709ea83SDmitry Karpeev   Input Parameter:
1901d709ea83SDmitry Karpeev . pc - the preconditioner
1902d709ea83SDmitry Karpeev 
1903d709ea83SDmitry Karpeev   Output Parameter:
1904f1580f4eSBarry Smith . flg - boolean indicating whether to use subdomains defined by the `DM`
1905d709ea83SDmitry Karpeev 
1906d709ea83SDmitry Karpeev   Level: intermediate
1907d709ea83SDmitry Karpeev 
1908562efe2eSBarry Smith .seealso: [](ch_ksp), `PCGASM`, `PCGASMSetUseDMSubdomains()`, `PCGASMSetOverlap()`
1909db781477SPatrick Sanan           `PCGASMCreateSubdomains2D()`
1910d709ea83SDmitry Karpeev @*/
PCGASMGetUseDMSubdomains(PC pc,PetscBool * flg)1911d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGASMGetUseDMSubdomains(PC pc, PetscBool *flg)
1912d71ae5a4SJacob Faibussowitsch {
1913d709ea83SDmitry Karpeev   PC_GASM  *osm = (PC_GASM *)pc->data;
1914d709ea83SDmitry Karpeev   PetscBool match;
1915d709ea83SDmitry Karpeev 
1916d709ea83SDmitry Karpeev   PetscFunctionBegin;
1917d709ea83SDmitry Karpeev   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
19184f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
19199566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCGASM, &match));
1920d709ea83SDmitry Karpeev   if (match) {
1921d709ea83SDmitry Karpeev     if (flg) *flg = osm->dm_subdomains;
1922d709ea83SDmitry Karpeev   }
19233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1924d709ea83SDmitry Karpeev }
1925