xref: /petsc/src/ksp/pc/impls/gasm/gasm.c (revision 4dfa11a44d5adf2389f1d3acbc8f3c1116dc6c3a)
1b1a0a8a3SJed Brown /*
2f746d493SDmitry Karpeev   This file defines an "generalized" additive Schwarz preconditioner for any Mat implementation.
3f1580f4eSBarry Smith   In this version each MPI rank may intersect multiple subdomains and any subdomain may
4f1580f4eSBarry Smith   intersect multiple MPI ranks.  Intersections of subdomains with MPI ranks are called *local
56a4f0f73SDmitry Karpeev   subdomains*.
6b1a0a8a3SJed Brown 
78f3b4b4dSDmitry Karpeev        N    - total number of distinct global subdomains          (set explicitly in PCGASMSetTotalSubdomains() or implicitly PCGASMSetSubdomains() and then calculated in PCSetUp_GASM())
8f1580f4eSBarry Smith        n    - actual number of local subdomains on this rank (set in PCGASMSetSubdomains() or calculated in PCGASMSetTotalSubdomains())
9f1580f4eSBarry Smith        nmax - maximum number of local subdomains per rank    (calculated in PCSetUp_GASM())
10b1a0a8a3SJed Brown */
11af0996ceSBarry Smith #include <petsc/private/pcimpl.h> /*I "petscpc.h" I*/
121e25c274SJed Brown #include <petscdm.h>
13b1a0a8a3SJed Brown 
14b1a0a8a3SJed Brown typedef struct {
15f746d493SDmitry Karpeev   PetscInt   N, n, nmax;
16b1a0a8a3SJed Brown   PetscInt   overlap;                /* overlap requested by user */
178f3b4b4dSDmitry Karpeev   PCGASMType type;                   /* use reduced interpolation, restriction or both */
188f3b4b4dSDmitry Karpeev   PetscBool  type_set;               /* if user set this value (so won't change it for symmetric problems) */
198f3b4b4dSDmitry Karpeev   PetscBool  same_subdomain_solvers; /* flag indicating whether all local solvers are same */
208f3b4b4dSDmitry Karpeev   PetscBool  sort_indices;           /* flag to sort subdomain indices */
218f3b4b4dSDmitry Karpeev   PetscBool  user_subdomains;        /* whether the user set explicit subdomain index sets -- keep them on PCReset() */
228f3b4b4dSDmitry Karpeev   PetscBool  dm_subdomains;          /* whether DM is allowed to define subdomains */
23ea91fabdSFande Kong   PetscBool  hierarchicalpartitioning;
248f3b4b4dSDmitry Karpeev   IS        *ois;           /* index sets that define the outer (conceptually, overlapping) subdomains */
258f3b4b4dSDmitry Karpeev   IS        *iis;           /* index sets that define the inner (conceptually, nonoverlapping) subdomains */
268f3b4b4dSDmitry Karpeev   KSP       *ksp;           /* linear solvers for each subdomain */
278f3b4b4dSDmitry Karpeev   Mat       *pmat;          /* subdomain block matrices */
28f746d493SDmitry Karpeev   Vec        gx, gy;        /* Merged work vectors */
29f746d493SDmitry Karpeev   Vec       *x, *y;         /* Split work vectors; storage aliases pieces of storage of the above merged vectors. */
306a4f0f73SDmitry Karpeev   VecScatter gorestriction; /* merged restriction to disjoint union of outer subdomains */
316a4f0f73SDmitry Karpeev   VecScatter girestriction; /* merged restriction to disjoint union of inner subdomains */
32ea91fabdSFande Kong   VecScatter pctoouter;
33ea91fabdSFande Kong   IS         permutationIS;
34ea91fabdSFande Kong   Mat        permutationP;
35ea91fabdSFande Kong   Mat        pcmat;
36ea91fabdSFande Kong   Vec        pcx, pcy;
37f746d493SDmitry Karpeev } PC_GASM;
38b1a0a8a3SJed Brown 
399371c9d4SSatish Balay static PetscErrorCode PCGASMComputeGlobalSubdomainNumbering_Private(PC pc, PetscInt **numbering, PetscInt **permutation) {
408f3b4b4dSDmitry Karpeev   PC_GASM *osm = (PC_GASM *)pc->data;
418f3b4b4dSDmitry Karpeev   PetscInt i;
428f3b4b4dSDmitry Karpeev 
438f3b4b4dSDmitry Karpeev   PetscFunctionBegin;
448f3b4b4dSDmitry Karpeev   /* Determine the number of globally-distinct subdomains and compute a global numbering for them. */
459566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(osm->n, numbering, osm->n, permutation));
469566063dSJacob Faibussowitsch   PetscCall(PetscObjectsListGetGlobalNumbering(PetscObjectComm((PetscObject)pc), osm->n, (PetscObject *)osm->iis, NULL, *numbering));
478f3b4b4dSDmitry Karpeev   for (i = 0; i < osm->n; ++i) (*permutation)[i] = i;
489566063dSJacob Faibussowitsch   PetscCall(PetscSortIntWithPermutation(osm->n, *numbering, *permutation));
498f3b4b4dSDmitry Karpeev   PetscFunctionReturn(0);
508f3b4b4dSDmitry Karpeev }
518f3b4b4dSDmitry Karpeev 
529371c9d4SSatish Balay static PetscErrorCode PCGASMSubdomainView_Private(PC pc, PetscInt i, PetscViewer viewer) {
53af538404SDmitry Karpeev   PC_GASM        *osm = (PC_GASM *)pc->data;
5406b43e7eSDmitry Karpeev   PetscInt        j, nidx;
55af538404SDmitry Karpeev   const PetscInt *idx;
5606b43e7eSDmitry Karpeev   PetscViewer     sviewer;
57af538404SDmitry Karpeev   char           *cidx;
58af538404SDmitry Karpeev 
59af538404SDmitry Karpeev   PetscFunctionBegin;
602472a847SBarry Smith   PetscCheck(i >= 0 && i < osm->n, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_WRONG, "Invalid subdomain %" PetscInt_FMT ": must nonnegative and less than %" PetscInt_FMT, i, osm->n);
614bde246dSDmitry Karpeev   /* Inner subdomains. */
629566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(osm->iis[i], &nidx));
634bde246dSDmitry Karpeev   /*
644bde246dSDmitry Karpeev    No more than 15 characters per index plus a space.
654bde246dSDmitry Karpeev    PetscViewerStringSPrintf requires a string of size at least 2, so use (nidx+1) instead of nidx,
664bde246dSDmitry Karpeev    in case nidx == 0. That will take care of the space for the trailing '\0' as well.
674bde246dSDmitry Karpeev    For nidx == 0, the whole string 16 '\0'.
684bde246dSDmitry Karpeev    */
6936a9e3b9SBarry Smith #define len 16 * (nidx + 1) + 1
709566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(len, &cidx));
719566063dSJacob Faibussowitsch   PetscCall(PetscViewerStringOpen(PETSC_COMM_SELF, cidx, len, &sviewer));
7236a9e3b9SBarry Smith #undef len
739566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(osm->iis[i], &idx));
7448a46eb9SPierre Jolivet   for (j = 0; j < nidx; ++j) PetscCall(PetscViewerStringSPrintf(sviewer, "%" PetscInt_FMT " ", idx[j]));
759566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(osm->iis[i], &idx));
769566063dSJacob Faibussowitsch   PetscCall(PetscViewerDestroy(&sviewer));
779566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "Inner subdomain:\n"));
789566063dSJacob Faibussowitsch   PetscCall(PetscViewerFlush(viewer));
799566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPushSynchronized(viewer));
809566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%s", cidx));
819566063dSJacob Faibussowitsch   PetscCall(PetscViewerFlush(viewer));
829566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPopSynchronized(viewer));
839566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
849566063dSJacob Faibussowitsch   PetscCall(PetscViewerFlush(viewer));
859566063dSJacob Faibussowitsch   PetscCall(PetscFree(cidx));
864bde246dSDmitry Karpeev   /* Outer subdomains. */
879566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(osm->ois[i], &nidx));
88eec7e3faSDmitry Karpeev   /*
89eec7e3faSDmitry Karpeev    No more than 15 characters per index plus a space.
90eec7e3faSDmitry Karpeev    PetscViewerStringSPrintf requires a string of size at least 2, so use (nidx+1) instead of nidx,
91eec7e3faSDmitry Karpeev    in case nidx == 0. That will take care of the space for the trailing '\0' as well.
92eec7e3faSDmitry Karpeev    For nidx == 0, the whole string 16 '\0'.
93eec7e3faSDmitry Karpeev    */
9436a9e3b9SBarry Smith #define len 16 * (nidx + 1) + 1
959566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(len, &cidx));
969566063dSJacob Faibussowitsch   PetscCall(PetscViewerStringOpen(PETSC_COMM_SELF, cidx, len, &sviewer));
9736a9e3b9SBarry Smith #undef len
989566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(osm->ois[i], &idx));
9948a46eb9SPierre Jolivet   for (j = 0; j < nidx; ++j) PetscCall(PetscViewerStringSPrintf(sviewer, "%" PetscInt_FMT " ", idx[j]));
1009566063dSJacob Faibussowitsch   PetscCall(PetscViewerDestroy(&sviewer));
1019566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(osm->ois[i], &idx));
1029566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "Outer subdomain:\n"));
1039566063dSJacob Faibussowitsch   PetscCall(PetscViewerFlush(viewer));
1049566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPushSynchronized(viewer));
1059566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%s", cidx));
1069566063dSJacob Faibussowitsch   PetscCall(PetscViewerFlush(viewer));
1079566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPopSynchronized(viewer));
1089566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
1099566063dSJacob Faibussowitsch   PetscCall(PetscViewerFlush(viewer));
1109566063dSJacob Faibussowitsch   PetscCall(PetscFree(cidx));
11106b43e7eSDmitry Karpeev   PetscFunctionReturn(0);
11206b43e7eSDmitry Karpeev }
11306b43e7eSDmitry Karpeev 
1149371c9d4SSatish Balay static PetscErrorCode PCGASMPrintSubdomains(PC pc) {
11506b43e7eSDmitry Karpeev   PC_GASM    *osm = (PC_GASM *)pc->data;
11606b43e7eSDmitry Karpeev   const char *prefix;
11706b43e7eSDmitry Karpeev   char        fname[PETSC_MAX_PATH_LEN + 1];
1188f3b4b4dSDmitry Karpeev   PetscInt    l, d, count;
1199140fbc5SPierre Jolivet   PetscBool   found;
1200298fd71SBarry Smith   PetscViewer viewer, sviewer = NULL;
1218f3b4b4dSDmitry Karpeev   PetscInt   *numbering, *permutation; /* global numbering of locally-supported subdomains and the permutation from the local ordering */
12206b43e7eSDmitry Karpeev 
12306b43e7eSDmitry Karpeev   PetscFunctionBegin;
1249566063dSJacob Faibussowitsch   PetscCall(PCGetOptionsPrefix(pc, &prefix));
1259566063dSJacob Faibussowitsch   PetscCall(PetscOptionsHasName(NULL, prefix, "-pc_gasm_print_subdomains", &found));
1269140fbc5SPierre Jolivet   if (!found) PetscFunctionReturn(0);
1279566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetString(NULL, prefix, "-pc_gasm_print_subdomains", fname, sizeof(fname), &found));
1289566063dSJacob Faibussowitsch   if (!found) PetscCall(PetscStrcpy(fname, "stdout"));
1299566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIOpen(PetscObjectComm((PetscObject)pc), fname, &viewer));
1304bde246dSDmitry Karpeev   /*
1314bde246dSDmitry Karpeev    Make sure the viewer has a name. Otherwise this may cause a deadlock or other weird errors when creating a subcomm viewer:
1324bde246dSDmitry Karpeev    the subcomm viewer will attempt to inherit the viewer's name, which, if not set, will be constructed collectively on the comm.
1334bde246dSDmitry Karpeev   */
1349566063dSJacob Faibussowitsch   PetscCall(PetscObjectName((PetscObject)viewer));
1354bde246dSDmitry Karpeev   l = 0;
1369566063dSJacob Faibussowitsch   PetscCall(PCGASMComputeGlobalSubdomainNumbering_Private(pc, &numbering, &permutation));
1378f3b4b4dSDmitry Karpeev   for (count = 0; count < osm->N; ++count) {
1384bde246dSDmitry Karpeev     /* Now let subdomains go one at a time in the global numbering order and print their subdomain/solver info. */
1394bde246dSDmitry Karpeev     if (l < osm->n) {
1404bde246dSDmitry Karpeev       d = permutation[l]; /* d is the local number of the l-th smallest (in the global ordering) among the locally supported subdomains */
1414bde246dSDmitry Karpeev       if (numbering[d] == count) {
1429566063dSJacob Faibussowitsch         PetscCall(PetscViewerGetSubViewer(viewer, ((PetscObject)osm->ois[d])->comm, &sviewer));
1439566063dSJacob Faibussowitsch         PetscCall(PCGASMSubdomainView_Private(pc, d, sviewer));
1449566063dSJacob Faibussowitsch         PetscCall(PetscViewerRestoreSubViewer(viewer, ((PetscObject)osm->ois[d])->comm, &sviewer));
1454bde246dSDmitry Karpeev         ++l;
146af538404SDmitry Karpeev       }
1474bde246dSDmitry Karpeev     }
1489566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Barrier(PetscObjectComm((PetscObject)pc)));
1494bde246dSDmitry Karpeev   }
1509566063dSJacob Faibussowitsch   PetscCall(PetscFree2(numbering, permutation));
1519566063dSJacob Faibussowitsch   PetscCall(PetscViewerDestroy(&viewer));
152af538404SDmitry Karpeev   PetscFunctionReturn(0);
153af538404SDmitry Karpeev }
154af538404SDmitry Karpeev 
1559371c9d4SSatish Balay static PetscErrorCode PCView_GASM(PC pc, PetscViewer viewer) {
156f746d493SDmitry Karpeev   PC_GASM    *osm = (PC_GASM *)pc->data;
157af538404SDmitry Karpeev   const char *prefix;
158af538404SDmitry Karpeev   PetscMPIInt rank, size;
1598f3b4b4dSDmitry Karpeev   PetscInt    bsz;
16006b43e7eSDmitry Karpeev   PetscBool   iascii, view_subdomains = PETSC_FALSE;
161b1a0a8a3SJed Brown   PetscViewer sviewer;
1628f3b4b4dSDmitry Karpeev   PetscInt    count, l;
1636a4f0f73SDmitry Karpeev   char        overlap[256]     = "user-defined overlap";
1646a4f0f73SDmitry Karpeev   char        gsubdomains[256] = "unknown total number of subdomains";
16506b43e7eSDmitry Karpeev   char        msubdomains[256] = "unknown max number of local subdomains";
1668f3b4b4dSDmitry Karpeev   PetscInt   *numbering, *permutation; /* global numbering of locally-supported subdomains and the permutation from the local ordering */
16711aeaf0aSBarry Smith 
168b1a0a8a3SJed Brown   PetscFunctionBegin;
1699566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size));
1709566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)pc), &rank));
17106b43e7eSDmitry Karpeev 
17248a46eb9SPierre Jolivet   if (osm->overlap >= 0) PetscCall(PetscSNPrintf(overlap, sizeof(overlap), "requested amount of overlap = %" PetscInt_FMT, osm->overlap));
17348a46eb9SPierre Jolivet   if (osm->N != PETSC_DETERMINE) PetscCall(PetscSNPrintf(gsubdomains, sizeof(gsubdomains), "total number of subdomains = %" PetscInt_FMT, osm->N));
17448a46eb9SPierre Jolivet   if (osm->nmax != PETSC_DETERMINE) PetscCall(PetscSNPrintf(msubdomains, sizeof(msubdomains), "max number of local subdomains = %" PetscInt_FMT, osm->nmax));
17506b43e7eSDmitry Karpeev 
1769566063dSJacob Faibussowitsch   PetscCall(PCGetOptionsPrefix(pc, &prefix));
1779566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(NULL, prefix, "-pc_gasm_view_subdomains", &view_subdomains, NULL));
17806b43e7eSDmitry Karpeev 
1799566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
180b1a0a8a3SJed Brown   if (iascii) {
18106b43e7eSDmitry Karpeev     /*
18206b43e7eSDmitry Karpeev      Make sure the viewer has a name. Otherwise this may cause a deadlock when creating a subcomm viewer:
18306b43e7eSDmitry Karpeev      the subcomm viewer will attempt to inherit the viewer's name, which, if not set, will be constructed
18406b43e7eSDmitry Karpeev      collectively on the comm.
18506b43e7eSDmitry Karpeev      */
1869566063dSJacob Faibussowitsch     PetscCall(PetscObjectName((PetscObject)viewer));
1879566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "  Restriction/interpolation type: %s\n", PCGASMTypes[osm->type]));
1889566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "  %s\n", overlap));
1899566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "  %s\n", gsubdomains));
1909566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "  %s\n", msubdomains));
1919566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPushSynchronized(viewer));
19263a3b9bcSJacob Faibussowitsch     PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "  [%d|%d] number of locally-supported subdomains = %" PetscInt_FMT "\n", rank, size, osm->n));
1939566063dSJacob Faibussowitsch     PetscCall(PetscViewerFlush(viewer));
1949566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPopSynchronized(viewer));
1956a4f0f73SDmitry Karpeev     /* Cannot take advantage of osm->same_subdomain_solvers without a global numbering of subdomains. */
1969566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "  Subdomain solver info is as follows:\n"));
1979566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPushTab(viewer));
1989566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "  - - - - - - - - - - - - - - - - - -\n"));
19906b43e7eSDmitry Karpeev     /* Make sure that everybody waits for the banner to be printed. */
2009566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Barrier(PetscObjectComm((PetscObject)viewer)));
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));
2151baa6e33SBarry Smith           if (view_subdomains) PetscCall(PCGASMSubdomainView_Private(pc, d, sviewer));
2166a4f0f73SDmitry Karpeev           if (!pc->setupcalled) {
2179566063dSJacob Faibussowitsch             PetscCall(PetscViewerASCIIPrintf(sviewer, "  Solver not set up yet: PCSetUp() not yet called\n"));
2182fa5cd67SKarl Rupp           } else {
2199566063dSJacob Faibussowitsch             PetscCall(KSPView(osm->ksp[d], sviewer));
2206a4f0f73SDmitry Karpeev           }
2219566063dSJacob Faibussowitsch           PetscCall(PetscViewerASCIIPrintf(sviewer, "  - - - - - - - - - - - - - - - - - -\n"));
2229566063dSJacob Faibussowitsch           PetscCall(PetscViewerFlush(sviewer));
2239566063dSJacob Faibussowitsch           PetscCall(PetscViewerRestoreSubViewer(viewer, ((PetscObject)osm->ois[d])->comm, &sviewer));
22406b43e7eSDmitry Karpeev           ++l;
225b1a0a8a3SJed Brown         }
22606b43e7eSDmitry Karpeev       }
2279566063dSJacob Faibussowitsch       PetscCallMPI(MPI_Barrier(PetscObjectComm((PetscObject)pc)));
228b1a0a8a3SJed Brown     }
2299566063dSJacob Faibussowitsch     PetscCall(PetscFree2(numbering, permutation));
2309566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPopTab(viewer));
2319566063dSJacob Faibussowitsch     PetscCall(PetscViewerFlush(viewer));
2329530cbd7SBarry Smith     /* this line is needed to match the extra PetscViewerASCIIPushSynchronized() in PetscViewerGetSubViewer() */
2339566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPopSynchronized(viewer));
2349530cbd7SBarry Smith   }
235b1a0a8a3SJed Brown   PetscFunctionReturn(0);
236b1a0a8a3SJed Brown }
237b1a0a8a3SJed Brown 
2388f3b4b4dSDmitry Karpeev PETSC_INTERN PetscErrorCode PCGASMCreateLocalSubdomains(Mat A, PetscInt nloc, IS *iis[]);
239af538404SDmitry Karpeev 
2409371c9d4SSatish Balay PetscErrorCode PCGASMSetHierarchicalPartitioning(PC pc) {
241ea91fabdSFande Kong   PC_GASM        *osm = (PC_GASM *)pc->data;
242ea91fabdSFande Kong   MatPartitioning part;
243ea91fabdSFande Kong   MPI_Comm        comm;
244ea91fabdSFande Kong   PetscMPIInt     size;
245ea91fabdSFande Kong   PetscInt        nlocalsubdomains, fromrows_localsize;
246ea91fabdSFande Kong   IS              partitioning, fromrows, isn;
247ea91fabdSFande Kong   Vec             outervec;
248ea91fabdSFande Kong 
249ea91fabdSFande Kong   PetscFunctionBegin;
2509566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)pc, &comm));
2519566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
252ea91fabdSFande Kong   /* we do not need a hierarchical partitioning when
253ea91fabdSFande Kong     * the total number of subdomains is consistent with
254ea91fabdSFande Kong     * the number of MPI tasks.
255ea91fabdSFande Kong     * For the following cases, we do not need to use HP
256ea91fabdSFande Kong     * */
257670417b2SFande Kong   if (osm->N == PETSC_DETERMINE || osm->N >= size || osm->N == 1) PetscFunctionReturn(0);
258f1580f4eSBarry 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);
259ea91fabdSFande Kong   nlocalsubdomains = size / osm->N;
260ea91fabdSFande Kong   osm->n           = 1;
2619566063dSJacob Faibussowitsch   PetscCall(MatPartitioningCreate(comm, &part));
2629566063dSJacob Faibussowitsch   PetscCall(MatPartitioningSetAdjacency(part, pc->pmat));
2639566063dSJacob Faibussowitsch   PetscCall(MatPartitioningSetType(part, MATPARTITIONINGHIERARCH));
2649566063dSJacob Faibussowitsch   PetscCall(MatPartitioningHierarchicalSetNcoarseparts(part, osm->N));
2659566063dSJacob Faibussowitsch   PetscCall(MatPartitioningHierarchicalSetNfineparts(part, nlocalsubdomains));
2669566063dSJacob Faibussowitsch   PetscCall(MatPartitioningSetFromOptions(part));
267f1580f4eSBarry Smith   /* get new rank owner number of each vertex */
2689566063dSJacob Faibussowitsch   PetscCall(MatPartitioningApply(part, &partitioning));
2699566063dSJacob Faibussowitsch   PetscCall(ISBuildTwoSided(partitioning, NULL, &fromrows));
2709566063dSJacob Faibussowitsch   PetscCall(ISPartitioningToNumbering(partitioning, &isn));
2719566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&isn));
2729566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(fromrows, &fromrows_localsize));
2739566063dSJacob Faibussowitsch   PetscCall(MatPartitioningDestroy(&part));
2749566063dSJacob Faibussowitsch   PetscCall(MatCreateVecs(pc->pmat, &outervec, NULL));
2759566063dSJacob Faibussowitsch   PetscCall(VecCreateMPI(comm, fromrows_localsize, PETSC_DETERMINE, &(osm->pcx)));
2769566063dSJacob Faibussowitsch   PetscCall(VecDuplicate(osm->pcx, &(osm->pcy)));
2779566063dSJacob Faibussowitsch   PetscCall(VecScatterCreate(osm->pcx, NULL, outervec, fromrows, &(osm->pctoouter)));
2789566063dSJacob Faibussowitsch   PetscCall(MatCreateSubMatrix(pc->pmat, fromrows, fromrows, MAT_INITIAL_MATRIX, &(osm->permutationP)));
2799566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fromrows));
280ea91fabdSFande Kong   osm->permutationIS = fromrows;
281ea91fabdSFande Kong   osm->pcmat         = pc->pmat;
2829566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)osm->permutationP));
283ea91fabdSFande Kong   pc->pmat = osm->permutationP;
2849566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&outervec));
2859566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&fromrows));
2869566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&partitioning));
287ea91fabdSFande Kong   osm->n = PETSC_DETERMINE;
288ea91fabdSFande Kong   PetscFunctionReturn(0);
289ea91fabdSFande Kong }
290ea91fabdSFande Kong 
2919371c9d4SSatish Balay static PetscErrorCode PCSetUp_GASM(PC pc) {
292f746d493SDmitry Karpeev   PC_GASM        *osm = (PC_GASM *)pc->data;
293ea91fabdSFande Kong   PetscInt        i, nInnerIndices, nTotalInnerIndices;
294c10bc1a1SDmitry Karpeev   PetscMPIInt     rank, size;
295b1a0a8a3SJed Brown   MatReuse        scall = MAT_REUSE_MATRIX;
296b1a0a8a3SJed Brown   KSP             ksp;
297b1a0a8a3SJed Brown   PC              subpc;
298b1a0a8a3SJed Brown   const char     *prefix, *pprefix;
299f746d493SDmitry Karpeev   Vec             x, y;
3006a4f0f73SDmitry Karpeev   PetscInt        oni;   /* Number of indices in the i-th local outer subdomain.               */
3016a4f0f73SDmitry Karpeev   const PetscInt *oidxi; /* Indices from the i-th subdomain local outer subdomain.             */
3026a4f0f73SDmitry Karpeev   PetscInt        on;    /* Number of indices in the disjoint union of local outer subdomains. */
3036a4f0f73SDmitry Karpeev   PetscInt       *oidx;  /* Indices in the disjoint union of local outer subdomains. */
3046a4f0f73SDmitry Karpeev   IS              gois;  /* Disjoint union the global indices of outer subdomains.             */
3056a4f0f73SDmitry Karpeev   IS              goid;  /* Identity IS of the size of the disjoint union of outer subdomains. */
306f746d493SDmitry Karpeev   PetscScalar    *gxarray, *gyarray;
307930d09c1SFande Kong   PetscInt        gostart; /* Start of locally-owned indices in the vectors -- osm->gx,osm->gy -- over the disjoint union of outer subdomains. */
3088f3b4b4dSDmitry Karpeev   PetscInt        num_subdomains  = 0;
3090298fd71SBarry Smith   DM             *subdomain_dm    = NULL;
3108f3b4b4dSDmitry Karpeev   char          **subdomain_names = NULL;
311f771a274SFande Kong   PetscInt       *numbering;
3128f3b4b4dSDmitry Karpeev 
313b1a0a8a3SJed Brown   PetscFunctionBegin;
3149566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size));
3159566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)pc), &rank));
316b1a0a8a3SJed Brown   if (!pc->setupcalled) {
317ea91fabdSFande Kong     /* use a hierarchical partitioning */
3181baa6e33SBarry Smith     if (osm->hierarchicalpartitioning) PetscCall(PCGASMSetHierarchicalPartitioning(pc));
3198f3b4b4dSDmitry Karpeev     if (osm->n == PETSC_DETERMINE) {
3208f3b4b4dSDmitry Karpeev       if (osm->N != PETSC_DETERMINE) {
3218f3b4b4dSDmitry Karpeev         /* No local subdomains given, but the desired number of total subdomains is known, so construct them accordingly. */
3229566063dSJacob Faibussowitsch         PetscCall(PCGASMCreateSubdomains(pc->pmat, osm->N, &osm->n, &osm->iis));
3238f3b4b4dSDmitry Karpeev       } else if (osm->dm_subdomains && pc->dm) {
3248f3b4b4dSDmitry Karpeev         /* try pc->dm next, if allowed */
3258f3b4b4dSDmitry Karpeev         PetscInt d;
326a35b7b57SDmitry Karpeev         IS      *inner_subdomain_is, *outer_subdomain_is;
3279566063dSJacob Faibussowitsch         PetscCall(DMCreateDomainDecomposition(pc->dm, &num_subdomains, &subdomain_names, &inner_subdomain_is, &outer_subdomain_is, &subdomain_dm));
3281baa6e33SBarry Smith         if (num_subdomains) PetscCall(PCGASMSetSubdomains(pc, num_subdomains, inner_subdomain_is, outer_subdomain_is));
329a35b7b57SDmitry Karpeev         for (d = 0; d < num_subdomains; ++d) {
3309566063dSJacob Faibussowitsch           if (inner_subdomain_is) PetscCall(ISDestroy(&inner_subdomain_is[d]));
3319566063dSJacob Faibussowitsch           if (outer_subdomain_is) PetscCall(ISDestroy(&outer_subdomain_is[d]));
332fdc48646SDmitry Karpeev         }
3339566063dSJacob Faibussowitsch         PetscCall(PetscFree(inner_subdomain_is));
3349566063dSJacob Faibussowitsch         PetscCall(PetscFree(outer_subdomain_is));
3358f3b4b4dSDmitry Karpeev       } else {
336f1580f4eSBarry Smith         /* still no subdomains; use one per rank */
33744fe18e5SDmitry Karpeev         osm->nmax = osm->n = 1;
3389566063dSJacob Faibussowitsch         PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size));
339f746d493SDmitry Karpeev         osm->N = size;
3409566063dSJacob Faibussowitsch         PetscCall(PCGASMCreateLocalSubdomains(pc->pmat, osm->n, &osm->iis));
341fdc48646SDmitry Karpeev       }
34206b43e7eSDmitry Karpeev     }
343a35b7b57SDmitry Karpeev     if (!osm->iis) {
344a35b7b57SDmitry Karpeev       /*
3458f3b4b4dSDmitry Karpeev        osm->n was set in PCGASMSetSubdomains(), but the actual subdomains have not been supplied.
3468f3b4b4dSDmitry Karpeev        We create the requisite number of local inner subdomains and then expand them into
3478f3b4b4dSDmitry Karpeev        out subdomains, if necessary.
348a35b7b57SDmitry Karpeev        */
3499566063dSJacob Faibussowitsch       PetscCall(PCGASMCreateLocalSubdomains(pc->pmat, osm->n, &osm->iis));
350a35b7b57SDmitry Karpeev     }
3518f3b4b4dSDmitry Karpeev     if (!osm->ois) {
3528f3b4b4dSDmitry Karpeev       /*
3538f3b4b4dSDmitry Karpeev             Initially make outer subdomains the same as inner subdomains. If nonzero additional overlap
3548f3b4b4dSDmitry Karpeev             has been requested, copy the inner subdomains over so they can be modified.
3558f3b4b4dSDmitry Karpeev       */
3569566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(osm->n, &osm->ois));
3578f3b4b4dSDmitry Karpeev       for (i = 0; i < osm->n; ++i) {
358ea91fabdSFande Kong         if (osm->overlap > 0 && osm->N > 1) { /* With positive overlap, osm->iis[i] will be modified */
3599566063dSJacob Faibussowitsch           PetscCall(ISDuplicate(osm->iis[i], (osm->ois) + i));
3609566063dSJacob Faibussowitsch           PetscCall(ISCopy(osm->iis[i], osm->ois[i]));
3618f3b4b4dSDmitry Karpeev         } else {
3629566063dSJacob Faibussowitsch           PetscCall(PetscObjectReference((PetscObject)((osm->iis)[i])));
3638f3b4b4dSDmitry Karpeev           osm->ois[i] = osm->iis[i];
3648f3b4b4dSDmitry Karpeev         }
3658f3b4b4dSDmitry Karpeev       }
366ea91fabdSFande Kong       if (osm->overlap > 0 && osm->N > 1) {
3678f3b4b4dSDmitry Karpeev         /* Extend the "overlapping" regions by a number of steps */
3689566063dSJacob Faibussowitsch         PetscCall(MatIncreaseOverlapSplit(pc->pmat, osm->n, osm->ois, osm->overlap));
3698f3b4b4dSDmitry Karpeev       }
370b1a0a8a3SJed Brown     }
3716a4f0f73SDmitry Karpeev 
3728f3b4b4dSDmitry Karpeev     /* Now the subdomains are defined.  Determine their global and max local numbers, if necessary. */
3738f3b4b4dSDmitry Karpeev     if (osm->nmax == PETSC_DETERMINE) {
3748f3b4b4dSDmitry Karpeev       PetscMPIInt inwork, outwork;
3758f3b4b4dSDmitry Karpeev       /* determine global number of subdomains and the max number of local subdomains */
3768f3b4b4dSDmitry Karpeev       inwork = osm->n;
3771c2dc1cbSBarry Smith       PetscCall(MPIU_Allreduce(&inwork, &outwork, 1, MPI_INT, MPI_MAX, PetscObjectComm((PetscObject)pc)));
3788f3b4b4dSDmitry Karpeev       osm->nmax = outwork;
3798f3b4b4dSDmitry Karpeev     }
3808f3b4b4dSDmitry Karpeev     if (osm->N == PETSC_DETERMINE) {
3818f3b4b4dSDmitry Karpeev       /* Determine the number of globally-distinct subdomains and compute a global numbering for them. */
3829566063dSJacob Faibussowitsch       PetscCall(PetscObjectsListGetGlobalNumbering(PetscObjectComm((PetscObject)pc), osm->n, (PetscObject *)osm->ois, &osm->N, NULL));
3838f3b4b4dSDmitry Karpeev     }
3848f3b4b4dSDmitry Karpeev 
385b1a0a8a3SJed Brown     if (osm->sort_indices) {
386f746d493SDmitry Karpeev       for (i = 0; i < osm->n; i++) {
3879566063dSJacob Faibussowitsch         PetscCall(ISSort(osm->ois[i]));
3889566063dSJacob Faibussowitsch         PetscCall(ISSort(osm->iis[i]));
389b1a0a8a3SJed Brown       }
390b1a0a8a3SJed Brown     }
3919566063dSJacob Faibussowitsch     PetscCall(PCGetOptionsPrefix(pc, &prefix));
3929566063dSJacob Faibussowitsch     PetscCall(PCGASMPrintSubdomains(pc));
3938f3b4b4dSDmitry Karpeev 
3946a4f0f73SDmitry Karpeev     /*
3956a4f0f73SDmitry Karpeev        Merge the ISs, create merged vectors and restrictions.
3966a4f0f73SDmitry Karpeev      */
3976a4f0f73SDmitry Karpeev     /* Merge outer subdomain ISs and construct a restriction onto the disjoint union of local outer subdomains. */
3986a4f0f73SDmitry Karpeev     on = 0;
399f746d493SDmitry Karpeev     for (i = 0; i < osm->n; i++) {
4009566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(osm->ois[i], &oni));
4016a4f0f73SDmitry Karpeev       on += oni;
402f746d493SDmitry Karpeev     }
4039566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(on, &oidx));
4046a4f0f73SDmitry Karpeev     on = 0;
405930d09c1SFande Kong     /* Merge local indices together */
406f746d493SDmitry Karpeev     for (i = 0; i < osm->n; i++) {
4079566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(osm->ois[i], &oni));
4089566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(osm->ois[i], &oidxi));
4099566063dSJacob Faibussowitsch       PetscCall(PetscArraycpy(oidx + on, oidxi, oni));
4109566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(osm->ois[i], &oidxi));
4116a4f0f73SDmitry Karpeev       on += oni;
412f746d493SDmitry Karpeev     }
4139566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(((PetscObject)(pc))->comm, on, oidx, PETSC_OWN_POINTER, &gois));
414ea91fabdSFande Kong     nTotalInnerIndices = 0;
415ea91fabdSFande Kong     for (i = 0; i < osm->n; i++) {
4169566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(osm->iis[i], &nInnerIndices));
417ea91fabdSFande Kong       nTotalInnerIndices += nInnerIndices;
418ea91fabdSFande Kong     }
4199566063dSJacob Faibussowitsch     PetscCall(VecCreateMPI(((PetscObject)(pc))->comm, nTotalInnerIndices, PETSC_DETERMINE, &x));
4209566063dSJacob Faibussowitsch     PetscCall(VecDuplicate(x, &y));
421ea91fabdSFande Kong 
4229566063dSJacob Faibussowitsch     PetscCall(VecCreateMPI(PetscObjectComm((PetscObject)pc), on, PETSC_DECIDE, &osm->gx));
4239566063dSJacob Faibussowitsch     PetscCall(VecDuplicate(osm->gx, &osm->gy));
4249566063dSJacob Faibussowitsch     PetscCall(VecGetOwnershipRange(osm->gx, &gostart, NULL));
4259566063dSJacob Faibussowitsch     PetscCall(ISCreateStride(PetscObjectComm((PetscObject)pc), on, gostart, 1, &goid));
426930d09c1SFande Kong     /* gois might indices not on local */
4279566063dSJacob Faibussowitsch     PetscCall(VecScatterCreate(x, gois, osm->gx, goid, &(osm->gorestriction)));
4289566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(osm->n, &numbering));
4299566063dSJacob Faibussowitsch     PetscCall(PetscObjectsListGetGlobalNumbering(PetscObjectComm((PetscObject)pc), osm->n, (PetscObject *)osm->ois, NULL, numbering));
4309566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&x));
4319566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&gois));
4322fa5cd67SKarl Rupp 
4336a4f0f73SDmitry Karpeev     /* Merge inner subdomain ISs and construct a restriction onto the disjoint union of local inner subdomains. */
4342fa5cd67SKarl Rupp     {
4352fa5cd67SKarl Rupp       PetscInt        ini;   /* Number of indices the i-th a local inner subdomain. */
4368966356dSPierre Jolivet       PetscInt        in;    /* Number of indices in the disjoint union of local inner subdomains. */
4376a4f0f73SDmitry Karpeev       PetscInt       *iidx;  /* Global indices in the merged local inner subdomain. */
4386a4f0f73SDmitry Karpeev       PetscInt       *ioidx; /* Global indices of the disjoint union of inner subdomains within the disjoint union of outer subdomains. */
4396a4f0f73SDmitry Karpeev       IS              giis;  /* IS for the disjoint union of inner subdomains. */
4406a4f0f73SDmitry Karpeev       IS              giois; /* IS for the disjoint union of inner subdomains within the disjoint union of outer subdomains. */
441f771a274SFande Kong       PetscScalar    *array;
442f771a274SFande Kong       const PetscInt *indices;
443f771a274SFande Kong       PetscInt        k;
4446a4f0f73SDmitry Karpeev       on = 0;
445f746d493SDmitry Karpeev       for (i = 0; i < osm->n; i++) {
4469566063dSJacob Faibussowitsch         PetscCall(ISGetLocalSize(osm->ois[i], &oni));
4476a4f0f73SDmitry Karpeev         on += oni;
448f746d493SDmitry Karpeev       }
4499566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(on, &iidx));
4509566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(on, &ioidx));
4519566063dSJacob Faibussowitsch       PetscCall(VecGetArray(y, &array));
452e12b4729SFande Kong       /* set communicator id to determine where overlap is */
453f771a274SFande Kong       in = 0;
454f771a274SFande Kong       for (i = 0; i < osm->n; i++) {
4559566063dSJacob Faibussowitsch         PetscCall(ISGetLocalSize(osm->iis[i], &ini));
456ad540459SPierre Jolivet         for (k = 0; k < ini; ++k) array[in + k] = numbering[i];
457f771a274SFande Kong         in += ini;
458f771a274SFande Kong       }
4599566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(y, &array));
4609566063dSJacob Faibussowitsch       PetscCall(VecScatterBegin(osm->gorestriction, y, osm->gy, INSERT_VALUES, SCATTER_FORWARD));
4619566063dSJacob Faibussowitsch       PetscCall(VecScatterEnd(osm->gorestriction, y, osm->gy, INSERT_VALUES, SCATTER_FORWARD));
4629566063dSJacob Faibussowitsch       PetscCall(VecGetOwnershipRange(osm->gy, &gostart, NULL));
4639566063dSJacob Faibussowitsch       PetscCall(VecGetArray(osm->gy, &array));
464f771a274SFande Kong       on = 0;
465f771a274SFande Kong       in = 0;
466f771a274SFande Kong       for (i = 0; i < osm->n; i++) {
4679566063dSJacob Faibussowitsch         PetscCall(ISGetLocalSize(osm->ois[i], &oni));
4689566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(osm->ois[i], &indices));
469f771a274SFande Kong         for (k = 0; k < oni; k++) {
470e12b4729SFande Kong           /*  skip overlapping indices to get inner domain */
47143081b6cSDmitry Karpeev           if (PetscRealPart(array[on + k]) != numbering[i]) continue;
472f771a274SFande Kong           iidx[in]    = indices[k];
473f771a274SFande Kong           ioidx[in++] = gostart + on + k;
474f771a274SFande Kong         }
4759566063dSJacob Faibussowitsch         PetscCall(ISRestoreIndices(osm->ois[i], &indices));
476f771a274SFande Kong         on += oni;
477f771a274SFande Kong       }
4789566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(osm->gy, &array));
4799566063dSJacob Faibussowitsch       PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)pc), in, iidx, PETSC_OWN_POINTER, &giis));
4809566063dSJacob Faibussowitsch       PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)pc), in, ioidx, PETSC_OWN_POINTER, &giois));
4819566063dSJacob Faibussowitsch       PetscCall(VecScatterCreate(y, giis, osm->gy, giois, &osm->girestriction));
4829566063dSJacob Faibussowitsch       PetscCall(VecDestroy(&y));
4839566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&giis));
4849566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&giois));
485b1a0a8a3SJed Brown     }
4869566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&goid));
4879566063dSJacob Faibussowitsch     PetscCall(PetscFree(numbering));
4882fa5cd67SKarl Rupp 
489f746d493SDmitry Karpeev     /* Create the subdomain work vectors. */
4909566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(osm->n, &osm->x));
4919566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(osm->n, &osm->y));
4929566063dSJacob Faibussowitsch     PetscCall(VecGetArray(osm->gx, &gxarray));
4939566063dSJacob Faibussowitsch     PetscCall(VecGetArray(osm->gy, &gyarray));
4946a4f0f73SDmitry Karpeev     for (i = 0, on = 0; i < osm->n; ++i, on += oni) {
4956a4f0f73SDmitry Karpeev       PetscInt oNi;
4969566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(osm->ois[i], &oni));
497930d09c1SFande Kong       /* on a sub communicator */
4989566063dSJacob Faibussowitsch       PetscCall(ISGetSize(osm->ois[i], &oNi));
4999566063dSJacob Faibussowitsch       PetscCall(VecCreateMPIWithArray(((PetscObject)(osm->ois[i]))->comm, 1, oni, oNi, gxarray + on, &osm->x[i]));
5009566063dSJacob Faibussowitsch       PetscCall(VecCreateMPIWithArray(((PetscObject)(osm->ois[i]))->comm, 1, oni, oNi, gyarray + on, &osm->y[i]));
501b1a0a8a3SJed Brown     }
5029566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(osm->gx, &gxarray));
5039566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(osm->gy, &gyarray));
5048f3b4b4dSDmitry Karpeev     /* Create the subdomain solvers */
5059566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(osm->n, &osm->ksp));
5068f3b4b4dSDmitry Karpeev     for (i = 0; i < osm->n; i++) {
5078f3b4b4dSDmitry Karpeev       char subprefix[PETSC_MAX_PATH_LEN + 1];
5089566063dSJacob Faibussowitsch       PetscCall(KSPCreate(((PetscObject)(osm->ois[i]))->comm, &ksp));
5099566063dSJacob Faibussowitsch       PetscCall(KSPSetErrorIfNotConverged(ksp, pc->erroriffailure));
5109566063dSJacob Faibussowitsch       PetscCall(PetscObjectIncrementTabLevel((PetscObject)ksp, (PetscObject)pc, 1));
5119566063dSJacob Faibussowitsch       PetscCall(KSPSetType(ksp, KSPPREONLY));
5129566063dSJacob Faibussowitsch       PetscCall(KSPGetPC(ksp, &subpc)); /* Why do we need this here? */
5138f3b4b4dSDmitry Karpeev       if (subdomain_dm) {
5149566063dSJacob Faibussowitsch         PetscCall(KSPSetDM(ksp, subdomain_dm[i]));
5159566063dSJacob Faibussowitsch         PetscCall(DMDestroy(subdomain_dm + i));
5168f3b4b4dSDmitry Karpeev       }
5179566063dSJacob Faibussowitsch       PetscCall(PCGetOptionsPrefix(pc, &prefix));
5189566063dSJacob Faibussowitsch       PetscCall(KSPSetOptionsPrefix(ksp, prefix));
5198f3b4b4dSDmitry Karpeev       if (subdomain_names && subdomain_names[i]) {
5209566063dSJacob Faibussowitsch         PetscCall(PetscSNPrintf(subprefix, PETSC_MAX_PATH_LEN, "sub_%s_", subdomain_names[i]));
5219566063dSJacob Faibussowitsch         PetscCall(KSPAppendOptionsPrefix(ksp, subprefix));
5229566063dSJacob Faibussowitsch         PetscCall(PetscFree(subdomain_names[i]));
5238f3b4b4dSDmitry Karpeev       }
5249566063dSJacob Faibussowitsch       PetscCall(KSPAppendOptionsPrefix(ksp, "sub_"));
525b1a0a8a3SJed Brown       osm->ksp[i] = ksp;
526b1a0a8a3SJed Brown     }
5279566063dSJacob Faibussowitsch     PetscCall(PetscFree(subdomain_dm));
5289566063dSJacob Faibussowitsch     PetscCall(PetscFree(subdomain_names));
529b1a0a8a3SJed Brown     scall = MAT_INITIAL_MATRIX;
5308f3b4b4dSDmitry Karpeev   } else { /* if (pc->setupcalled) */
531b1a0a8a3SJed Brown     /*
5328f3b4b4dSDmitry Karpeev        Destroy the submatrices from the previous iteration
533b1a0a8a3SJed Brown     */
534b1a0a8a3SJed Brown     if (pc->flag == DIFFERENT_NONZERO_PATTERN) {
5359566063dSJacob Faibussowitsch       PetscCall(MatDestroyMatrices(osm->n, &osm->pmat));
536b1a0a8a3SJed Brown       scall = MAT_INITIAL_MATRIX;
537b1a0a8a3SJed Brown     }
538ea91fabdSFande Kong     if (osm->permutationIS) {
5399566063dSJacob Faibussowitsch       PetscCall(MatCreateSubMatrix(pc->pmat, osm->permutationIS, osm->permutationIS, scall, &osm->permutationP));
5409566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject)osm->permutationP));
541ea91fabdSFande Kong       osm->pcmat = pc->pmat;
542ea91fabdSFande Kong       pc->pmat   = osm->permutationP;
543b1a0a8a3SJed Brown     }
544ea91fabdSFande Kong   }
545ea91fabdSFande Kong 
546b1a0a8a3SJed Brown   /*
5472da392ccSBarry Smith      Extract the submatrices.
548b1a0a8a3SJed Brown   */
54981a5c4aaSDmitry Karpeev   if (size > 1) {
5509566063dSJacob Faibussowitsch     PetscCall(MatCreateSubMatricesMPI(pc->pmat, osm->n, osm->ois, osm->ois, scall, &osm->pmat));
5512fa5cd67SKarl Rupp   } else {
5529566063dSJacob Faibussowitsch     PetscCall(MatCreateSubMatrices(pc->pmat, osm->n, osm->ois, osm->ois, scall, &osm->pmat));
55381a5c4aaSDmitry Karpeev   }
554b1a0a8a3SJed Brown   if (scall == MAT_INITIAL_MATRIX) {
5559566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetOptionsPrefix((PetscObject)pc->pmat, &pprefix));
556*4dfa11a4SJacob Faibussowitsch     for (i = 0; i < osm->n; i++) { PetscCall(PetscObjectSetOptionsPrefix((PetscObject)osm->pmat[i], pprefix)); }
557b1a0a8a3SJed Brown   }
558b1a0a8a3SJed Brown 
559b1a0a8a3SJed Brown   /* Return control to the user so that the submatrices can be modified (e.g., to apply
560b1a0a8a3SJed Brown      different boundary conditions for the submatrices than for the global problem) */
5619566063dSJacob Faibussowitsch   PetscCall(PCModifySubMatrices(pc, osm->n, osm->ois, osm->ois, osm->pmat, pc->modifysubmatricesP));
562b1a0a8a3SJed Brown 
563b1a0a8a3SJed Brown   /*
5646a4f0f73SDmitry Karpeev      Loop over submatrices putting them into local ksps
565b1a0a8a3SJed Brown   */
566f746d493SDmitry Karpeev   for (i = 0; i < osm->n; i++) {
5679566063dSJacob Faibussowitsch     PetscCall(KSPSetOperators(osm->ksp[i], osm->pmat[i], osm->pmat[i]));
5689566063dSJacob Faibussowitsch     PetscCall(KSPGetOptionsPrefix(osm->ksp[i], &prefix));
5699566063dSJacob Faibussowitsch     PetscCall(MatSetOptionsPrefix(osm->pmat[i], prefix));
57048a46eb9SPierre Jolivet     if (!pc->setupcalled) PetscCall(KSPSetFromOptions(osm->ksp[i]));
571b1a0a8a3SJed Brown   }
572ea91fabdSFande Kong   if (osm->pcmat) {
5739566063dSJacob Faibussowitsch     PetscCall(MatDestroy(&pc->pmat));
574ea91fabdSFande Kong     pc->pmat   = osm->pcmat;
5750a545947SLisandro Dalcin     osm->pcmat = NULL;
576ea91fabdSFande Kong   }
577b1a0a8a3SJed Brown   PetscFunctionReturn(0);
578b1a0a8a3SJed Brown }
579b1a0a8a3SJed Brown 
5809371c9d4SSatish Balay static PetscErrorCode PCSetUpOnBlocks_GASM(PC pc) {
581f746d493SDmitry Karpeev   PC_GASM *osm = (PC_GASM *)pc->data;
582b1a0a8a3SJed Brown   PetscInt i;
583b1a0a8a3SJed Brown 
584b1a0a8a3SJed Brown   PetscFunctionBegin;
58548a46eb9SPierre Jolivet   for (i = 0; i < osm->n; i++) PetscCall(KSPSetUp(osm->ksp[i]));
586b1a0a8a3SJed Brown   PetscFunctionReturn(0);
587b1a0a8a3SJed Brown }
588b1a0a8a3SJed Brown 
5899371c9d4SSatish Balay static PetscErrorCode PCApply_GASM(PC pc, Vec xin, Vec yout) {
590f746d493SDmitry Karpeev   PC_GASM    *osm = (PC_GASM *)pc->data;
591f746d493SDmitry Karpeev   PetscInt    i;
592ea91fabdSFande Kong   Vec         x, y;
593b1a0a8a3SJed Brown   ScatterMode forward = SCATTER_FORWARD, reverse = SCATTER_REVERSE;
594b1a0a8a3SJed Brown 
595b1a0a8a3SJed Brown   PetscFunctionBegin;
596ea91fabdSFande Kong   if (osm->pctoouter) {
5979566063dSJacob Faibussowitsch     PetscCall(VecScatterBegin(osm->pctoouter, xin, osm->pcx, INSERT_VALUES, SCATTER_REVERSE));
5989566063dSJacob Faibussowitsch     PetscCall(VecScatterEnd(osm->pctoouter, xin, osm->pcx, INSERT_VALUES, SCATTER_REVERSE));
599ea91fabdSFande Kong     x = osm->pcx;
600ea91fabdSFande Kong     y = osm->pcy;
601ea91fabdSFande Kong   } else {
602ea91fabdSFande Kong     x = xin;
603ea91fabdSFande Kong     y = yout;
604ea91fabdSFande Kong   }
605b1a0a8a3SJed Brown   /*
60648e38a8aSPierre Jolivet      support for limiting the restriction or interpolation only to the inner
607b1a0a8a3SJed Brown      subdomain values (leaving the other values 0).
608b1a0a8a3SJed Brown   */
609f746d493SDmitry Karpeev   if (!(osm->type & PC_GASM_RESTRICT)) {
610b1a0a8a3SJed Brown     /* have to zero the work RHS since scatter may leave some slots empty */
6119566063dSJacob Faibussowitsch     PetscCall(VecZeroEntries(osm->gx));
6129566063dSJacob Faibussowitsch     PetscCall(VecScatterBegin(osm->girestriction, x, osm->gx, INSERT_VALUES, forward));
6132fa5cd67SKarl Rupp   } else {
6149566063dSJacob Faibussowitsch     PetscCall(VecScatterBegin(osm->gorestriction, x, osm->gx, INSERT_VALUES, forward));
615b1a0a8a3SJed Brown   }
6169566063dSJacob Faibussowitsch   PetscCall(VecZeroEntries(osm->gy));
6176a4f0f73SDmitry Karpeev   if (!(osm->type & PC_GASM_RESTRICT)) {
6189566063dSJacob Faibussowitsch     PetscCall(VecScatterEnd(osm->girestriction, x, osm->gx, INSERT_VALUES, forward));
6192fa5cd67SKarl Rupp   } else {
6209566063dSJacob Faibussowitsch     PetscCall(VecScatterEnd(osm->gorestriction, x, osm->gx, INSERT_VALUES, forward));
6216a4f0f73SDmitry Karpeev   }
62286b96d36SDmitry Karpeev   /* do the subdomain solves */
62386b96d36SDmitry Karpeev   for (i = 0; i < osm->n; ++i) {
6249566063dSJacob Faibussowitsch     PetscCall(KSPSolve(osm->ksp[i], osm->x[i], osm->y[i]));
6259566063dSJacob Faibussowitsch     PetscCall(KSPCheckSolve(osm->ksp[i], pc, osm->y[i]));
626b1a0a8a3SJed Brown   }
62748e38a8aSPierre Jolivet   /* do we need to zero y? */
6289566063dSJacob Faibussowitsch   PetscCall(VecZeroEntries(y));
6296a4f0f73SDmitry Karpeev   if (!(osm->type & PC_GASM_INTERPOLATE)) {
6309566063dSJacob Faibussowitsch     PetscCall(VecScatterBegin(osm->girestriction, osm->gy, y, ADD_VALUES, reverse));
6319566063dSJacob Faibussowitsch     PetscCall(VecScatterEnd(osm->girestriction, osm->gy, y, ADD_VALUES, reverse));
6322fa5cd67SKarl Rupp   } else {
6339566063dSJacob Faibussowitsch     PetscCall(VecScatterBegin(osm->gorestriction, osm->gy, y, ADD_VALUES, reverse));
6349566063dSJacob Faibussowitsch     PetscCall(VecScatterEnd(osm->gorestriction, osm->gy, y, ADD_VALUES, reverse));
6356a4f0f73SDmitry Karpeev   }
636ea91fabdSFande Kong   if (osm->pctoouter) {
6379566063dSJacob Faibussowitsch     PetscCall(VecScatterBegin(osm->pctoouter, y, yout, INSERT_VALUES, SCATTER_FORWARD));
6389566063dSJacob Faibussowitsch     PetscCall(VecScatterEnd(osm->pctoouter, y, yout, INSERT_VALUES, SCATTER_FORWARD));
639ea91fabdSFande Kong   }
640ea91fabdSFande Kong   PetscFunctionReturn(0);
641b1a0a8a3SJed Brown }
642b1a0a8a3SJed Brown 
6439371c9d4SSatish Balay static PetscErrorCode PCMatApply_GASM(PC pc, Mat Xin, Mat Yout) {
64448e38a8aSPierre Jolivet   PC_GASM    *osm = (PC_GASM *)pc->data;
64548e38a8aSPierre Jolivet   Mat         X, Y, O = NULL, Z, W;
64648e38a8aSPierre Jolivet   Vec         x, y;
64748e38a8aSPierre Jolivet   PetscInt    i, m, M, N;
64848e38a8aSPierre Jolivet   ScatterMode forward = SCATTER_FORWARD, reverse = SCATTER_REVERSE;
64948e38a8aSPierre Jolivet 
65048e38a8aSPierre Jolivet   PetscFunctionBegin;
65108401ef6SPierre Jolivet   PetscCheck(osm->n == 1, PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "Not yet implemented");
6529566063dSJacob Faibussowitsch   PetscCall(MatGetSize(Xin, NULL, &N));
65348e38a8aSPierre Jolivet   if (osm->pctoouter) {
6549566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(osm->pcx, &m));
6559566063dSJacob Faibussowitsch     PetscCall(VecGetSize(osm->pcx, &M));
6569566063dSJacob Faibussowitsch     PetscCall(MatCreateDense(PetscObjectComm((PetscObject)osm->ois[0]), m, PETSC_DECIDE, M, N, NULL, &O));
65748e38a8aSPierre Jolivet     for (i = 0; i < N; ++i) {
6589566063dSJacob Faibussowitsch       PetscCall(MatDenseGetColumnVecRead(Xin, i, &x));
6599566063dSJacob Faibussowitsch       PetscCall(MatDenseGetColumnVecWrite(O, i, &y));
6609566063dSJacob Faibussowitsch       PetscCall(VecScatterBegin(osm->pctoouter, x, y, INSERT_VALUES, SCATTER_REVERSE));
6619566063dSJacob Faibussowitsch       PetscCall(VecScatterEnd(osm->pctoouter, x, y, INSERT_VALUES, SCATTER_REVERSE));
6629566063dSJacob Faibussowitsch       PetscCall(MatDenseRestoreColumnVecWrite(O, i, &y));
6639566063dSJacob Faibussowitsch       PetscCall(MatDenseRestoreColumnVecRead(Xin, i, &x));
66448e38a8aSPierre Jolivet     }
66548e38a8aSPierre Jolivet     X = Y = O;
66648e38a8aSPierre Jolivet   } else {
66748e38a8aSPierre Jolivet     X = Xin;
66848e38a8aSPierre Jolivet     Y = Yout;
66948e38a8aSPierre Jolivet   }
67048e38a8aSPierre Jolivet   /*
67148e38a8aSPierre Jolivet      support for limiting the restriction or interpolation only to the inner
67248e38a8aSPierre Jolivet      subdomain values (leaving the other values 0).
67348e38a8aSPierre Jolivet   */
6749566063dSJacob Faibussowitsch   PetscCall(VecGetLocalSize(osm->x[0], &m));
6759566063dSJacob Faibussowitsch   PetscCall(VecGetSize(osm->x[0], &M));
6769566063dSJacob Faibussowitsch   PetscCall(MatCreateDense(PetscObjectComm((PetscObject)osm->ois[0]), m, PETSC_DECIDE, M, N, NULL, &Z));
67748e38a8aSPierre Jolivet   for (i = 0; i < N; ++i) {
6789566063dSJacob Faibussowitsch     PetscCall(MatDenseGetColumnVecRead(X, i, &x));
6799566063dSJacob Faibussowitsch     PetscCall(MatDenseGetColumnVecWrite(Z, i, &y));
68048e38a8aSPierre Jolivet     if (!(osm->type & PC_GASM_RESTRICT)) {
68148e38a8aSPierre Jolivet       /* have to zero the work RHS since scatter may leave some slots empty */
6829566063dSJacob Faibussowitsch       PetscCall(VecZeroEntries(y));
6839566063dSJacob Faibussowitsch       PetscCall(VecScatterBegin(osm->girestriction, x, y, INSERT_VALUES, forward));
6849566063dSJacob Faibussowitsch       PetscCall(VecScatterEnd(osm->girestriction, x, y, INSERT_VALUES, forward));
68548e38a8aSPierre Jolivet     } else {
6869566063dSJacob Faibussowitsch       PetscCall(VecScatterBegin(osm->gorestriction, x, y, INSERT_VALUES, forward));
6879566063dSJacob Faibussowitsch       PetscCall(VecScatterEnd(osm->gorestriction, x, y, INSERT_VALUES, forward));
68848e38a8aSPierre Jolivet     }
6899566063dSJacob Faibussowitsch     PetscCall(MatDenseRestoreColumnVecWrite(Z, i, &y));
6909566063dSJacob Faibussowitsch     PetscCall(MatDenseRestoreColumnVecRead(X, i, &x));
69148e38a8aSPierre Jolivet   }
6929566063dSJacob Faibussowitsch   PetscCall(MatCreateDense(PetscObjectComm((PetscObject)osm->ois[0]), m, PETSC_DECIDE, M, N, NULL, &W));
6939566063dSJacob Faibussowitsch   PetscCall(MatSetOption(Z, MAT_NO_OFF_PROC_ENTRIES, PETSC_TRUE));
6949566063dSJacob Faibussowitsch   PetscCall(MatAssemblyBegin(Z, MAT_FINAL_ASSEMBLY));
6959566063dSJacob Faibussowitsch   PetscCall(MatAssemblyEnd(Z, MAT_FINAL_ASSEMBLY));
69648e38a8aSPierre Jolivet   /* do the subdomain solve */
6979566063dSJacob Faibussowitsch   PetscCall(KSPMatSolve(osm->ksp[0], Z, W));
6989566063dSJacob Faibussowitsch   PetscCall(KSPCheckSolve(osm->ksp[0], pc, NULL));
6999566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&Z));
70048e38a8aSPierre Jolivet   /* do we need to zero y? */
7019566063dSJacob Faibussowitsch   PetscCall(MatZeroEntries(Y));
70248e38a8aSPierre Jolivet   for (i = 0; i < N; ++i) {
7039566063dSJacob Faibussowitsch     PetscCall(MatDenseGetColumnVecWrite(Y, i, &y));
7049566063dSJacob Faibussowitsch     PetscCall(MatDenseGetColumnVecRead(W, i, &x));
70548e38a8aSPierre Jolivet     if (!(osm->type & PC_GASM_INTERPOLATE)) {
7069566063dSJacob Faibussowitsch       PetscCall(VecScatterBegin(osm->girestriction, x, y, ADD_VALUES, reverse));
7079566063dSJacob Faibussowitsch       PetscCall(VecScatterEnd(osm->girestriction, x, y, ADD_VALUES, reverse));
70848e38a8aSPierre Jolivet     } else {
7099566063dSJacob Faibussowitsch       PetscCall(VecScatterBegin(osm->gorestriction, x, y, ADD_VALUES, reverse));
7109566063dSJacob Faibussowitsch       PetscCall(VecScatterEnd(osm->gorestriction, x, y, ADD_VALUES, reverse));
71148e38a8aSPierre Jolivet     }
7129566063dSJacob Faibussowitsch     PetscCall(MatDenseRestoreColumnVecRead(W, i, &x));
71348e38a8aSPierre Jolivet     if (osm->pctoouter) {
7149566063dSJacob Faibussowitsch       PetscCall(MatDenseGetColumnVecWrite(Yout, i, &x));
7159566063dSJacob Faibussowitsch       PetscCall(VecScatterBegin(osm->pctoouter, y, x, INSERT_VALUES, SCATTER_FORWARD));
7169566063dSJacob Faibussowitsch       PetscCall(VecScatterEnd(osm->pctoouter, y, x, INSERT_VALUES, SCATTER_FORWARD));
7179566063dSJacob Faibussowitsch       PetscCall(MatDenseRestoreColumnVecRead(Yout, i, &x));
71848e38a8aSPierre Jolivet     }
7199566063dSJacob Faibussowitsch     PetscCall(MatDenseRestoreColumnVecWrite(Y, i, &y));
72048e38a8aSPierre Jolivet   }
7219566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&W));
7229566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&O));
72348e38a8aSPierre Jolivet   PetscFunctionReturn(0);
72448e38a8aSPierre Jolivet }
72548e38a8aSPierre Jolivet 
7269371c9d4SSatish Balay static PetscErrorCode PCApplyTranspose_GASM(PC pc, Vec xin, Vec yout) {
727f746d493SDmitry Karpeev   PC_GASM    *osm = (PC_GASM *)pc->data;
728f746d493SDmitry Karpeev   PetscInt    i;
729ea91fabdSFande Kong   Vec         x, y;
730b1a0a8a3SJed Brown   ScatterMode forward = SCATTER_FORWARD, reverse = SCATTER_REVERSE;
731b1a0a8a3SJed Brown 
732b1a0a8a3SJed Brown   PetscFunctionBegin;
733ea91fabdSFande Kong   if (osm->pctoouter) {
7349566063dSJacob Faibussowitsch     PetscCall(VecScatterBegin(osm->pctoouter, xin, osm->pcx, INSERT_VALUES, SCATTER_REVERSE));
7359566063dSJacob Faibussowitsch     PetscCall(VecScatterEnd(osm->pctoouter, xin, osm->pcx, INSERT_VALUES, SCATTER_REVERSE));
736ea91fabdSFande Kong     x = osm->pcx;
737ea91fabdSFande Kong     y = osm->pcy;
738ea91fabdSFande Kong   } else {
739ea91fabdSFande Kong     x = xin;
740ea91fabdSFande Kong     y = yout;
741ea91fabdSFande Kong   }
742b1a0a8a3SJed Brown   /*
743b1a0a8a3SJed Brown      Support for limiting the restriction or interpolation to only local
744b1a0a8a3SJed Brown      subdomain values (leaving the other values 0).
745b1a0a8a3SJed Brown 
746f746d493SDmitry Karpeev      Note: these are reversed from the PCApply_GASM() because we are applying the
747b1a0a8a3SJed Brown      transpose of the three terms
748b1a0a8a3SJed Brown   */
749f746d493SDmitry Karpeev   if (!(osm->type & PC_GASM_INTERPOLATE)) {
750b1a0a8a3SJed Brown     /* have to zero the work RHS since scatter may leave some slots empty */
7519566063dSJacob Faibussowitsch     PetscCall(VecZeroEntries(osm->gx));
7529566063dSJacob Faibussowitsch     PetscCall(VecScatterBegin(osm->girestriction, x, osm->gx, INSERT_VALUES, forward));
7532fa5cd67SKarl Rupp   } else {
7549566063dSJacob Faibussowitsch     PetscCall(VecScatterBegin(osm->gorestriction, x, osm->gx, INSERT_VALUES, forward));
755b1a0a8a3SJed Brown   }
7569566063dSJacob Faibussowitsch   PetscCall(VecZeroEntries(osm->gy));
7576a4f0f73SDmitry Karpeev   if (!(osm->type & PC_GASM_INTERPOLATE)) {
7589566063dSJacob Faibussowitsch     PetscCall(VecScatterEnd(osm->girestriction, x, osm->gx, INSERT_VALUES, forward));
7592fa5cd67SKarl Rupp   } else {
7609566063dSJacob Faibussowitsch     PetscCall(VecScatterEnd(osm->gorestriction, x, osm->gx, INSERT_VALUES, forward));
7616a4f0f73SDmitry Karpeev   }
762b1a0a8a3SJed Brown   /* do the local solves */
763ab3e923fSDmitry 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. */
7649566063dSJacob Faibussowitsch     PetscCall(KSPSolveTranspose(osm->ksp[i], osm->x[i], osm->y[i]));
7659566063dSJacob Faibussowitsch     PetscCall(KSPCheckSolve(osm->ksp[i], pc, osm->y[i]));
766b1a0a8a3SJed Brown   }
7679566063dSJacob Faibussowitsch   PetscCall(VecZeroEntries(y));
7686a4f0f73SDmitry Karpeev   if (!(osm->type & PC_GASM_RESTRICT)) {
7699566063dSJacob Faibussowitsch     PetscCall(VecScatterBegin(osm->girestriction, osm->gy, y, ADD_VALUES, reverse));
7709566063dSJacob Faibussowitsch     PetscCall(VecScatterEnd(osm->girestriction, osm->gy, y, ADD_VALUES, reverse));
7712fa5cd67SKarl Rupp   } else {
7729566063dSJacob Faibussowitsch     PetscCall(VecScatterBegin(osm->gorestriction, osm->gy, y, ADD_VALUES, reverse));
7739566063dSJacob Faibussowitsch     PetscCall(VecScatterEnd(osm->gorestriction, osm->gy, y, ADD_VALUES, reverse));
7746a4f0f73SDmitry Karpeev   }
775ea91fabdSFande Kong   if (osm->pctoouter) {
7769566063dSJacob Faibussowitsch     PetscCall(VecScatterBegin(osm->pctoouter, y, yout, INSERT_VALUES, SCATTER_FORWARD));
7779566063dSJacob Faibussowitsch     PetscCall(VecScatterEnd(osm->pctoouter, y, yout, INSERT_VALUES, SCATTER_FORWARD));
778ea91fabdSFande Kong   }
779b1a0a8a3SJed Brown   PetscFunctionReturn(0);
780b1a0a8a3SJed Brown }
781b1a0a8a3SJed Brown 
7829371c9d4SSatish Balay static PetscErrorCode PCReset_GASM(PC pc) {
783f746d493SDmitry Karpeev   PC_GASM *osm = (PC_GASM *)pc->data;
784b1a0a8a3SJed Brown   PetscInt i;
785b1a0a8a3SJed Brown 
786b1a0a8a3SJed Brown   PetscFunctionBegin;
787b1a0a8a3SJed Brown   if (osm->ksp) {
78848a46eb9SPierre Jolivet     for (i = 0; i < osm->n; i++) PetscCall(KSPReset(osm->ksp[i]));
789b1a0a8a3SJed Brown   }
790b1a0a8a3SJed Brown   if (osm->pmat) {
791f746d493SDmitry Karpeev     if (osm->n > 0) {
792df750dc8SHong Zhang       PetscMPIInt size;
7939566063dSJacob Faibussowitsch       PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size));
794df750dc8SHong Zhang       if (size > 1) {
7957dae84e0SHong Zhang         /* osm->pmat is created by MatCreateSubMatricesMPI(), cannot use MatDestroySubMatrices() */
7969566063dSJacob Faibussowitsch         PetscCall(MatDestroyMatrices(osm->n, &osm->pmat));
797df750dc8SHong Zhang       } else {
7989566063dSJacob Faibussowitsch         PetscCall(MatDestroySubMatrices(osm->n, &osm->pmat));
799df750dc8SHong Zhang       }
800b1a0a8a3SJed Brown     }
801b1a0a8a3SJed Brown   }
802d34fcf5fSBarry Smith   if (osm->x) {
803f746d493SDmitry Karpeev     for (i = 0; i < osm->n; i++) {
8049566063dSJacob Faibussowitsch       PetscCall(VecDestroy(&osm->x[i]));
8059566063dSJacob Faibussowitsch       PetscCall(VecDestroy(&osm->y[i]));
806b1a0a8a3SJed Brown     }
807d34fcf5fSBarry Smith   }
8089566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&osm->gx));
8099566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&osm->gy));
810ab3e923fSDmitry Karpeev 
8119566063dSJacob Faibussowitsch   PetscCall(VecScatterDestroy(&osm->gorestriction));
8129566063dSJacob Faibussowitsch   PetscCall(VecScatterDestroy(&osm->girestriction));
8138f3b4b4dSDmitry Karpeev   if (!osm->user_subdomains) {
8149566063dSJacob Faibussowitsch     PetscCall(PCGASMDestroySubdomains(osm->n, &osm->ois, &osm->iis));
8158f3b4b4dSDmitry Karpeev     osm->N    = PETSC_DETERMINE;
8168f3b4b4dSDmitry Karpeev     osm->nmax = PETSC_DETERMINE;
8178f3b4b4dSDmitry Karpeev   }
81848a46eb9SPierre Jolivet   if (osm->pctoouter) PetscCall(VecScatterDestroy(&(osm->pctoouter)));
81948a46eb9SPierre Jolivet   if (osm->permutationIS) PetscCall(ISDestroy(&(osm->permutationIS)));
82048a46eb9SPierre Jolivet   if (osm->pcx) PetscCall(VecDestroy(&(osm->pcx)));
82148a46eb9SPierre Jolivet   if (osm->pcy) PetscCall(VecDestroy(&(osm->pcy)));
82248a46eb9SPierre Jolivet   if (osm->permutationP) PetscCall(MatDestroy(&(osm->permutationP)));
82348a46eb9SPierre Jolivet   if (osm->pcmat) PetscCall(MatDestroy(&osm->pcmat));
824a06653b4SBarry Smith   PetscFunctionReturn(0);
825a06653b4SBarry Smith }
826a06653b4SBarry Smith 
8279371c9d4SSatish Balay static PetscErrorCode PCDestroy_GASM(PC pc) {
828a06653b4SBarry Smith   PC_GASM *osm = (PC_GASM *)pc->data;
829a06653b4SBarry Smith   PetscInt i;
830a06653b4SBarry Smith 
831a06653b4SBarry Smith   PetscFunctionBegin;
8329566063dSJacob Faibussowitsch   PetscCall(PCReset_GASM(pc));
833135757f6SDmitry Karpeev   /* PCReset will not destroy subdomains, if user_subdomains is true. */
8349566063dSJacob Faibussowitsch   PetscCall(PCGASMDestroySubdomains(osm->n, &osm->ois, &osm->iis));
835a06653b4SBarry Smith   if (osm->ksp) {
83648a46eb9SPierre Jolivet     for (i = 0; i < osm->n; i++) PetscCall(KSPDestroy(&osm->ksp[i]));
8379566063dSJacob Faibussowitsch     PetscCall(PetscFree(osm->ksp));
838a06653b4SBarry Smith   }
8399566063dSJacob Faibussowitsch   PetscCall(PetscFree(osm->x));
8409566063dSJacob Faibussowitsch   PetscCall(PetscFree(osm->y));
8412e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetSubdomains_C", NULL));
8422e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetOverlap_C", NULL));
8432e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetType_C", NULL));
8442e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetSortIndices_C", NULL));
8452e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMGetSubKSP_C", NULL));
8469566063dSJacob Faibussowitsch   PetscCall(PetscFree(pc->data));
847b1a0a8a3SJed Brown   PetscFunctionReturn(0);
848b1a0a8a3SJed Brown }
849b1a0a8a3SJed Brown 
8509371c9d4SSatish Balay static PetscErrorCode PCSetFromOptions_GASM(PC pc, PetscOptionItems *PetscOptionsObject) {
851f746d493SDmitry Karpeev   PC_GASM   *osm = (PC_GASM *)pc->data;
852b1a0a8a3SJed Brown   PetscInt   blocks, ovl;
85357501b6eSBarry Smith   PetscBool  flg;
854f746d493SDmitry Karpeev   PCGASMType gasmtype;
855b1a0a8a3SJed Brown 
856b1a0a8a3SJed Brown   PetscFunctionBegin;
857d0609cedSBarry Smith   PetscOptionsHeadBegin(PetscOptionsObject, "Generalized additive Schwarz options");
8589566063dSJacob 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));
8599566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt("-pc_gasm_total_subdomains", "Total number of subdomains across communicator", "PCGASMSetTotalSubdomains", osm->N, &blocks, &flg));
8601baa6e33SBarry Smith   if (flg) PetscCall(PCGASMSetTotalSubdomains(pc, blocks));
8619566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt("-pc_gasm_overlap", "Number of overlapping degrees of freedom", "PCGASMSetOverlap", osm->overlap, &ovl, &flg));
86265db9045SDmitry Karpeev   if (flg) {
8639566063dSJacob Faibussowitsch     PetscCall(PCGASMSetOverlap(pc, ovl));
864d709ea83SDmitry Karpeev     osm->dm_subdomains = PETSC_FALSE;
86565db9045SDmitry Karpeev   }
866b1a0a8a3SJed Brown   flg = PETSC_FALSE;
8679566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-pc_gasm_type", "Type of restriction/extension", "PCGASMSetType", PCGASMTypes, (PetscEnum)osm->type, (PetscEnum *)&gasmtype, &flg));
8689566063dSJacob Faibussowitsch   if (flg) PetscCall(PCGASMSetType(pc, gasmtype));
8699566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-pc_gasm_use_hierachical_partitioning", "use hierarchical partitioning", NULL, osm->hierarchicalpartitioning, &osm->hierarchicalpartitioning, &flg));
870d0609cedSBarry Smith   PetscOptionsHeadEnd();
871b1a0a8a3SJed Brown   PetscFunctionReturn(0);
872b1a0a8a3SJed Brown }
873b1a0a8a3SJed Brown 
874b1a0a8a3SJed Brown /*------------------------------------------------------------------------------------*/
875b1a0a8a3SJed Brown 
8768f3b4b4dSDmitry Karpeev /*@
877f1580f4eSBarry Smith     PCGASMSetTotalSubdomains - sets the total number of subdomains to use across the communicator for `PCGASM`
8788f3b4b4dSDmitry Karpeev     Logically collective on pc
8798f3b4b4dSDmitry Karpeev 
8808f3b4b4dSDmitry Karpeev     Input Parameters:
8818f3b4b4dSDmitry Karpeev +   pc  - the preconditioner
8828f3b4b4dSDmitry Karpeev -   N   - total number of subdomains
8838f3b4b4dSDmitry Karpeev 
8848f3b4b4dSDmitry Karpeev     Level: beginner
8858f3b4b4dSDmitry Karpeev 
886f1580f4eSBarry Smith .seealso: `PCGASM`, `PCGASMSetSubdomains()`, `PCGASMSetOverlap()`
887db781477SPatrick Sanan           `PCGASMCreateSubdomains2D()`
8888f3b4b4dSDmitry Karpeev @*/
8899371c9d4SSatish Balay PetscErrorCode PCGASMSetTotalSubdomains(PC pc, PetscInt N) {
8908f3b4b4dSDmitry Karpeev   PC_GASM    *osm = (PC_GASM *)pc->data;
8918f3b4b4dSDmitry Karpeev   PetscMPIInt size, rank;
8928f3b4b4dSDmitry Karpeev 
8938f3b4b4dSDmitry Karpeev   PetscFunctionBegin;
89463a3b9bcSJacob 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);
89528b400f6SJacob Faibussowitsch   PetscCheck(!pc->setupcalled, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "PCGASMSetTotalSubdomains() should be called before calling PCSetUp().");
8968f3b4b4dSDmitry Karpeev 
8979566063dSJacob Faibussowitsch   PetscCall(PCGASMDestroySubdomains(osm->n, &osm->iis, &osm->ois));
8988f3b4b4dSDmitry Karpeev   osm->ois = osm->iis = NULL;
8998f3b4b4dSDmitry Karpeev 
9009566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size));
9019566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)pc), &rank));
9028f3b4b4dSDmitry Karpeev   osm->N             = N;
9038f3b4b4dSDmitry Karpeev   osm->n             = PETSC_DETERMINE;
9048f3b4b4dSDmitry Karpeev   osm->nmax          = PETSC_DETERMINE;
9058f3b4b4dSDmitry Karpeev   osm->dm_subdomains = PETSC_FALSE;
9068f3b4b4dSDmitry Karpeev   PetscFunctionReturn(0);
9078f3b4b4dSDmitry Karpeev }
9088f3b4b4dSDmitry Karpeev 
9099371c9d4SSatish Balay static PetscErrorCode PCGASMSetSubdomains_GASM(PC pc, PetscInt n, IS iis[], IS ois[]) {
910f746d493SDmitry Karpeev   PC_GASM *osm = (PC_GASM *)pc->data;
911b1a0a8a3SJed Brown   PetscInt i;
912b1a0a8a3SJed Brown 
913b1a0a8a3SJed Brown   PetscFunctionBegin;
914f1580f4eSBarry 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);
91528b400f6SJacob Faibussowitsch   PetscCheck(!pc->setupcalled, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "PCGASMSetSubdomains() should be called before calling PCSetUp().");
916b1a0a8a3SJed Brown 
9179566063dSJacob Faibussowitsch   PetscCall(PCGASMDestroySubdomains(osm->n, &osm->iis, &osm->ois));
9188f3b4b4dSDmitry Karpeev   osm->iis = osm->ois = NULL;
9198f3b4b4dSDmitry Karpeev   osm->n              = n;
9208f3b4b4dSDmitry Karpeev   osm->N              = PETSC_DETERMINE;
9218f3b4b4dSDmitry Karpeev   osm->nmax           = PETSC_DETERMINE;
922a35b7b57SDmitry Karpeev   if (ois) {
9239566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(n, &osm->ois));
9248f3b4b4dSDmitry Karpeev     for (i = 0; i < n; i++) {
9259566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject)ois[i]));
9268f3b4b4dSDmitry Karpeev       osm->ois[i] = ois[i];
9278f3b4b4dSDmitry Karpeev     }
9288f3b4b4dSDmitry Karpeev     /*
9298f3b4b4dSDmitry Karpeev        Since the user set the outer subdomains, even if nontrivial overlap was requested via PCGASMSetOverlap(),
9308f3b4b4dSDmitry Karpeev        it will be ignored.  To avoid confusion later on (e.g., when viewing the PC), the overlap size is set to -1.
9318f3b4b4dSDmitry Karpeev     */
932b1a0a8a3SJed Brown     osm->overlap = -1;
933670417b2SFande Kong     /* inner subdomains must be provided  */
93428b400f6SJacob Faibussowitsch     PetscCheck(iis, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "inner indices have to be provided ");
935670417b2SFande Kong   } /* end if */
936a35b7b57SDmitry Karpeev   if (iis) {
9379566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(n, &osm->iis));
9388f3b4b4dSDmitry Karpeev     for (i = 0; i < n; i++) {
9399566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject)iis[i]));
9408f3b4b4dSDmitry Karpeev       osm->iis[i] = iis[i];
9418f3b4b4dSDmitry Karpeev     }
942a35b7b57SDmitry Karpeev     if (!ois) {
943390e1bf2SBarry Smith       osm->ois = NULL;
944670417b2SFande Kong       /* if user does not provide outer indices, we will create the corresponding outer indices using  osm->overlap =1 in PCSetUp_GASM */
945670417b2SFande Kong     }
946670417b2SFande Kong   }
94776bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
948670417b2SFande Kong     PetscInt        j, rstart, rend, *covered, lsize;
949670417b2SFande Kong     const PetscInt *indices;
950670417b2SFande Kong     /* check if the inner indices cover and only cover the local portion of the preconditioning matrix */
9519566063dSJacob Faibussowitsch     PetscCall(MatGetOwnershipRange(pc->pmat, &rstart, &rend));
9529566063dSJacob Faibussowitsch     PetscCall(PetscCalloc1(rend - rstart, &covered));
953f1580f4eSBarry Smith     /* check if the current MPI rank owns indices from others */
954a35b7b57SDmitry Karpeev     for (i = 0; i < n; i++) {
9559566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(osm->iis[i], &indices));
9569566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(osm->iis[i], &lsize));
957670417b2SFande Kong       for (j = 0; j < lsize; j++) {
958f1580f4eSBarry 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]);
9592472a847SBarry 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]);
96063a3b9bcSJacob Faibussowitsch         covered[indices[j] - rstart] = 1;
961a35b7b57SDmitry Karpeev       }
9629566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(osm->iis[i], &indices));
9638f3b4b4dSDmitry Karpeev     }
964670417b2SFande Kong     /* check if we miss any indices */
965ad540459SPierre 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);
9669566063dSJacob Faibussowitsch     PetscCall(PetscFree(covered));
967a35b7b57SDmitry Karpeev   }
9688f3b4b4dSDmitry Karpeev   if (iis) osm->user_subdomains = PETSC_TRUE;
969b1a0a8a3SJed Brown   PetscFunctionReturn(0);
970b1a0a8a3SJed Brown }
971b1a0a8a3SJed Brown 
9729371c9d4SSatish Balay static PetscErrorCode PCGASMSetOverlap_GASM(PC pc, PetscInt ovl) {
973f746d493SDmitry Karpeev   PC_GASM *osm = (PC_GASM *)pc->data;
974b1a0a8a3SJed Brown 
975b1a0a8a3SJed Brown   PetscFunctionBegin;
97608401ef6SPierre Jolivet   PetscCheck(ovl >= 0, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "Negative overlap value requested");
9772472a847SBarry Smith   PetscCheck(!pc->setupcalled || ovl == osm->overlap, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "PCGASMSetOverlap() should be called before PCSetUp().");
9782fa5cd67SKarl Rupp   if (!pc->setupcalled) osm->overlap = ovl;
979b1a0a8a3SJed Brown   PetscFunctionReturn(0);
980b1a0a8a3SJed Brown }
981b1a0a8a3SJed Brown 
9829371c9d4SSatish Balay static PetscErrorCode PCGASMSetType_GASM(PC pc, PCGASMType type) {
983f746d493SDmitry Karpeev   PC_GASM *osm = (PC_GASM *)pc->data;
984b1a0a8a3SJed Brown 
985b1a0a8a3SJed Brown   PetscFunctionBegin;
986b1a0a8a3SJed Brown   osm->type     = type;
987b1a0a8a3SJed Brown   osm->type_set = PETSC_TRUE;
988b1a0a8a3SJed Brown   PetscFunctionReturn(0);
989b1a0a8a3SJed Brown }
990b1a0a8a3SJed Brown 
9919371c9d4SSatish Balay static PetscErrorCode PCGASMSetSortIndices_GASM(PC pc, PetscBool doSort) {
992f746d493SDmitry Karpeev   PC_GASM *osm = (PC_GASM *)pc->data;
993b1a0a8a3SJed Brown 
994b1a0a8a3SJed Brown   PetscFunctionBegin;
995b1a0a8a3SJed Brown   osm->sort_indices = doSort;
996b1a0a8a3SJed Brown   PetscFunctionReturn(0);
997b1a0a8a3SJed Brown }
998b1a0a8a3SJed Brown 
99944fe18e5SDmitry Karpeev /*
10008f3b4b4dSDmitry Karpeev    FIXME: This routine might need to be modified now that multiple ranks per subdomain are allowed.
100144fe18e5SDmitry Karpeev         In particular, it would upset the global subdomain number calculation.
100244fe18e5SDmitry Karpeev */
10039371c9d4SSatish Balay static PetscErrorCode PCGASMGetSubKSP_GASM(PC pc, PetscInt *n, PetscInt *first, KSP **ksp) {
1004f746d493SDmitry Karpeev   PC_GASM *osm = (PC_GASM *)pc->data;
1005b1a0a8a3SJed Brown 
1006b1a0a8a3SJed Brown   PetscFunctionBegin;
100708401ef6SPierre 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");
1008b1a0a8a3SJed Brown 
10092fa5cd67SKarl Rupp   if (n) *n = osm->n;
1010ab3e923fSDmitry Karpeev   if (first) {
10119566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Scan(&osm->n, first, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject)pc)));
1012ab3e923fSDmitry Karpeev     *first -= osm->n;
1013b1a0a8a3SJed Brown   }
1014b1a0a8a3SJed Brown   if (ksp) {
1015b1a0a8a3SJed Brown     /* Assume that local solves are now different; not necessarily
101606b43e7eSDmitry Karpeev        true, though!  This flag is used only for PCView_GASM() */
1017b1a0a8a3SJed Brown     *ksp                        = osm->ksp;
10186a4f0f73SDmitry Karpeev     osm->same_subdomain_solvers = PETSC_FALSE;
1019b1a0a8a3SJed Brown   }
1020b1a0a8a3SJed Brown   PetscFunctionReturn(0);
1021ab3e923fSDmitry Karpeev } /* PCGASMGetSubKSP_GASM() */
1022b1a0a8a3SJed Brown 
1023b1a0a8a3SJed Brown /*@C
1024f1580f4eSBarry Smith     PCGASMSetSubdomains - Sets the subdomains for this MPI rank
1025f1580f4eSBarry Smith     for the additive Schwarz preconditioner with multiple MPI ranks per subdomain, `PCGASM`
1026b1a0a8a3SJed Brown 
10278f3b4b4dSDmitry Karpeev     Collective on pc
1028b1a0a8a3SJed Brown 
1029b1a0a8a3SJed Brown     Input Parameters:
10308f3b4b4dSDmitry Karpeev +   pc  - the preconditioner object
1031f1580f4eSBarry Smith .   n   - the number of subdomains for this MPI rank
10328f3b4b4dSDmitry Karpeev .   iis - the index sets that define the inner subdomains (or NULL for PETSc to determine subdomains)
10338f3b4b4dSDmitry Karpeev -   ois - the index sets that define the outer subdomains (or NULL to use the same as iis, or to construct by expanding iis by the requested overlap)
1034b1a0a8a3SJed Brown 
1035b1a0a8a3SJed Brown     Notes:
1036f1580f4eSBarry Smith     The `IS` indices use the parallel, global numbering of the vector entries.
10376a4f0f73SDmitry Karpeev     Inner subdomains are those where the correction is applied.
10386a4f0f73SDmitry Karpeev     Outer subdomains are those where the residual necessary to obtain the
1039f1580f4eSBarry Smith     corrections is obtained (see `PCGASMType` for the use of inner/outer subdomains).
1040f1580f4eSBarry Smith     Both inner and outer subdomains can extend over several MPI ranks.
1041f1580f4eSBarry Smith     This rank's portion of a subdomain is known as a local subdomain.
10426a4f0f73SDmitry Karpeev 
1043f1580f4eSBarry Smith     Inner subdomains can not overlap with each other, do not have any entities from remote ranks,
1044f1580f4eSBarry Smith     and  have to cover the entire local subdomain owned by the current rank. The index sets on each
1045f1580f4eSBarry Smith     rank should be ordered such that the ith local subdomain is connected to the ith remote subdomain
1046f1580f4eSBarry Smith     on another MPI rank.
1047670417b2SFande Kong 
1048f1580f4eSBarry Smith     By default the `PGASM` preconditioner uses 1 (local) subdomain per MPI rank.
10496a4f0f73SDmitry Karpeev 
1050b1a0a8a3SJed Brown     Level: advanced
1051b1a0a8a3SJed Brown 
1052f1580f4eSBarry Smith .seealso: `PCGASM`, `PCGASMSetOverlap()`, `PCGASMGetSubKSP()`,
1053db781477SPatrick Sanan           `PCGASMCreateSubdomains2D()`, `PCGASMGetSubdomains()`
1054b1a0a8a3SJed Brown @*/
10559371c9d4SSatish Balay PetscErrorCode PCGASMSetSubdomains(PC pc, PetscInt n, IS iis[], IS ois[]) {
10568f3b4b4dSDmitry Karpeev   PC_GASM *osm = (PC_GASM *)pc->data;
1057b1a0a8a3SJed Brown 
1058b1a0a8a3SJed Brown   PetscFunctionBegin;
1059b1a0a8a3SJed Brown   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1060cac4c232SBarry Smith   PetscTryMethod(pc, "PCGASMSetSubdomains_C", (PC, PetscInt, IS[], IS[]), (pc, n, iis, ois));
10618f3b4b4dSDmitry Karpeev   osm->dm_subdomains = PETSC_FALSE;
1062b1a0a8a3SJed Brown   PetscFunctionReturn(0);
1063b1a0a8a3SJed Brown }
1064b1a0a8a3SJed Brown 
1065b1a0a8a3SJed Brown /*@
1066f746d493SDmitry Karpeev     PCGASMSetOverlap - Sets the overlap between a pair of subdomains for the
1067f1580f4eSBarry Smith     additive Schwarz preconditioner `PCGASM`.  Either all or no MPI ranks in the
10688f3b4b4dSDmitry Karpeev     pc communicator must call this routine.
1069b1a0a8a3SJed Brown 
10708f3b4b4dSDmitry Karpeev     Logically Collective on pc
1071b1a0a8a3SJed Brown 
1072b1a0a8a3SJed Brown     Input Parameters:
1073b1a0a8a3SJed Brown +   pc  - the preconditioner context
10748f3b4b4dSDmitry Karpeev -   ovl - the amount of overlap between subdomains (ovl >= 0, default value = 0)
1075b1a0a8a3SJed Brown 
1076b1a0a8a3SJed Brown     Options Database Key:
107706b43e7eSDmitry Karpeev .   -pc_gasm_overlap <overlap> - Sets overlap
1078b1a0a8a3SJed Brown 
1079b1a0a8a3SJed Brown     Notes:
1080f1580f4eSBarry Smith     By default the `PCGASM` preconditioner uses 1 subdomain per rank.  To use
10818f3b4b4dSDmitry Karpeev     multiple subdomain per perocessor or "straddling" subdomains that intersect
1082f1580f4eSBarry Smith     multiple ranks use `PCGASMSetSubdomains()` (or option -pc_gasm_total_subdomains <n>).
1083b1a0a8a3SJed Brown 
10848f3b4b4dSDmitry Karpeev     The overlap defaults to 0, so if one desires that no additional
1085b1a0a8a3SJed Brown     overlap be computed beyond what may have been set with a call to
1086f1580f4eSBarry Smith     `PCGASMSetSubdomains()`, then ovl must be set to be 0.  In particular, if one does
10878f3b4b4dSDmitry Karpeev     not explicitly set the subdomains in application code, then all overlap would be computed
1088f1580f4eSBarry Smith     internally by PETSc, and using an overlap of 0 would result in an `PCGASM`
1089b1a0a8a3SJed Brown     variant that is equivalent to the block Jacobi preconditioner.
1090b1a0a8a3SJed Brown 
1091f1580f4eSBarry Smith     One can define initial index sets with any overlap via
1092f1580f4eSBarry Smith     `PCGASMSetSubdomains()`; the routine `PCGASMSetOverlap()` merely allows
109306b43e7eSDmitry Karpeev     PETSc to extend that overlap further, if desired.
1094b1a0a8a3SJed Brown 
1095b1a0a8a3SJed Brown     Level: intermediate
1096b1a0a8a3SJed Brown 
1097f1580f4eSBarry Smith .seealso: `PCGASM`, `PCGASMSetSubdomains()`, `PCGASMGetSubKSP()`,
1098db781477SPatrick Sanan           `PCGASMCreateSubdomains2D()`, `PCGASMGetSubdomains()`
1099b1a0a8a3SJed Brown @*/
11009371c9d4SSatish Balay PetscErrorCode PCGASMSetOverlap(PC pc, PetscInt ovl) {
11018f3b4b4dSDmitry Karpeev   PC_GASM *osm = (PC_GASM *)pc->data;
1102b1a0a8a3SJed Brown 
1103b1a0a8a3SJed Brown   PetscFunctionBegin;
1104b1a0a8a3SJed Brown   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1105b1a0a8a3SJed Brown   PetscValidLogicalCollectiveInt(pc, ovl, 2);
1106cac4c232SBarry Smith   PetscTryMethod(pc, "PCGASMSetOverlap_C", (PC, PetscInt), (pc, ovl));
11078f3b4b4dSDmitry Karpeev   osm->dm_subdomains = PETSC_FALSE;
1108b1a0a8a3SJed Brown   PetscFunctionReturn(0);
1109b1a0a8a3SJed Brown }
1110b1a0a8a3SJed Brown 
1111b1a0a8a3SJed Brown /*@
1112f746d493SDmitry Karpeev     PCGASMSetType - Sets the type of restriction and interpolation used
1113f1580f4eSBarry Smith     for local problems in the `PCGASM` additive Schwarz method.
1114b1a0a8a3SJed Brown 
1115f1580f4eSBarry Smith     Logically Collective on pc
1116b1a0a8a3SJed Brown 
1117b1a0a8a3SJed Brown     Input Parameters:
1118b1a0a8a3SJed Brown +   pc  - the preconditioner context
1119f1580f4eSBarry Smith -   type - variant of `PCGASM`, one of
1120b1a0a8a3SJed Brown .vb
1121f1580f4eSBarry Smith       `PC_GASM_BASIC`       - full interpolation and restriction
1122f1580f4eSBarry Smith       `PC_GASM_RESTRICT`    - full restriction, local MPI rank interpolation
1123f1580f4eSBarry Smith       `PC_GASM_INTERPOLATE` - full interpolation, local MPI rank restriction
1124f1580f4eSBarry Smith       `PC_GASM_NONE`        - local MPI rank restriction and interpolation
1125b1a0a8a3SJed Brown .ve
1126b1a0a8a3SJed Brown 
1127b1a0a8a3SJed Brown     Options Database Key:
1128f746d493SDmitry Karpeev .   -pc_gasm_type [basic,restrict,interpolate,none] - Sets GASM type
1129b1a0a8a3SJed Brown 
1130b1a0a8a3SJed Brown     Level: intermediate
1131b1a0a8a3SJed Brown 
1132f1580f4eSBarry Smith .seealso: `PCGASM`, `PCGASMSetSubdomains()`, `PCGASMGetSubKSP()`,
1133f1580f4eSBarry Smith           `PCGASMCreateSubdomains2D()`, `PCASM`, `PCASMSetType()`
1134b1a0a8a3SJed Brown @*/
11359371c9d4SSatish Balay PetscErrorCode PCGASMSetType(PC pc, PCGASMType type) {
1136b1a0a8a3SJed Brown   PetscFunctionBegin;
1137b1a0a8a3SJed Brown   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1138b1a0a8a3SJed Brown   PetscValidLogicalCollectiveEnum(pc, type, 2);
1139cac4c232SBarry Smith   PetscTryMethod(pc, "PCGASMSetType_C", (PC, PCGASMType), (pc, type));
1140b1a0a8a3SJed Brown   PetscFunctionReturn(0);
1141b1a0a8a3SJed Brown }
1142b1a0a8a3SJed Brown 
1143b1a0a8a3SJed Brown /*@
1144f746d493SDmitry Karpeev     PCGASMSetSortIndices - Determines whether subdomain indices are sorted.
1145b1a0a8a3SJed Brown 
1146f1580f4eSBarry Smith     Logically Collective on pc
1147b1a0a8a3SJed Brown 
1148b1a0a8a3SJed Brown     Input Parameters:
1149b1a0a8a3SJed Brown +   pc  - the preconditioner context
1150b1a0a8a3SJed Brown -   doSort - sort the subdomain indices
1151b1a0a8a3SJed Brown 
1152b1a0a8a3SJed Brown     Level: intermediate
1153b1a0a8a3SJed Brown 
1154f1580f4eSBarry Smith .seealso: `PCGASM`, `PCGASMSetSubdomains()`, `PCGASMGetSubKSP()`,
1155db781477SPatrick Sanan           `PCGASMCreateSubdomains2D()`
1156b1a0a8a3SJed Brown @*/
11579371c9d4SSatish Balay PetscErrorCode PCGASMSetSortIndices(PC pc, PetscBool doSort) {
1158b1a0a8a3SJed Brown   PetscFunctionBegin;
1159b1a0a8a3SJed Brown   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1160b1a0a8a3SJed Brown   PetscValidLogicalCollectiveBool(pc, doSort, 2);
1161cac4c232SBarry Smith   PetscTryMethod(pc, "PCGASMSetSortIndices_C", (PC, PetscBool), (pc, doSort));
1162b1a0a8a3SJed Brown   PetscFunctionReturn(0);
1163b1a0a8a3SJed Brown }
1164b1a0a8a3SJed Brown 
1165b1a0a8a3SJed Brown /*@C
1166f1580f4eSBarry Smith    PCGASMGetSubKSP - Gets the local `KSP` contexts for all subdomains on
1167f1580f4eSBarry Smith    this MPI rank.
1168b1a0a8a3SJed Brown 
1169f1580f4eSBarry Smith    Collective on pc iff first_local is requested
1170b1a0a8a3SJed Brown 
1171b1a0a8a3SJed Brown    Input Parameter:
1172b1a0a8a3SJed Brown .  pc - the preconditioner context
1173b1a0a8a3SJed Brown 
1174b1a0a8a3SJed Brown    Output Parameters:
1175f1580f4eSBarry Smith +  n_local - the number of blocks on this MPI rank or NULL
1176f1580f4eSBarry Smith .  first_local - the global number of the first block on this rank or NULL,
1177f1580f4eSBarry Smith                  all ranks must request or all must pass NULL
1178f1580f4eSBarry Smith -  ksp - the array of `KSP` contexts
1179b1a0a8a3SJed Brown 
1180b1a0a8a3SJed Brown    Note:
1181f1580f4eSBarry Smith    After `PCGASMGetSubKSP()` the array of `KSP`es is not to be freed
1182b1a0a8a3SJed Brown 
1183f1580f4eSBarry Smith    Currently for some matrix implementations only 1 block per MPI rank
1184b1a0a8a3SJed Brown    is supported.
1185b1a0a8a3SJed Brown 
1186f1580f4eSBarry Smith    You must call `KSPSetUp()` before calling `PCGASMGetSubKSP()`.
1187b1a0a8a3SJed Brown 
1188b1a0a8a3SJed Brown    Level: advanced
1189b1a0a8a3SJed Brown 
1190f1580f4eSBarry Smith .seealso: `PCGASM`, `PCGASMSetSubdomains()`, `PCGASMSetOverlap()`,
1191db781477SPatrick Sanan           `PCGASMCreateSubdomains2D()`,
1192b1a0a8a3SJed Brown @*/
11939371c9d4SSatish Balay PetscErrorCode PCGASMGetSubKSP(PC pc, PetscInt *n_local, PetscInt *first_local, KSP *ksp[]) {
1194b1a0a8a3SJed Brown   PetscFunctionBegin;
1195b1a0a8a3SJed Brown   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1196cac4c232SBarry Smith   PetscUseMethod(pc, "PCGASMGetSubKSP_C", (PC, PetscInt *, PetscInt *, KSP **), (pc, n_local, first_local, ksp));
1197b1a0a8a3SJed Brown   PetscFunctionReturn(0);
1198b1a0a8a3SJed Brown }
1199b1a0a8a3SJed Brown 
1200b1a0a8a3SJed Brown /*MC
1201f746d493SDmitry Karpeev    PCGASM - Use the (restricted) additive Schwarz method, each block is (approximately) solved with
1202f1580f4eSBarry Smith            its own `KSP` object on a subset of MPI ranks
1203b1a0a8a3SJed Brown 
1204b1a0a8a3SJed Brown    Options Database Keys:
1205f1580f4eSBarry Smith +  -pc_gasm_total_subdomains <n>  - Sets total number of local subdomains to be distributed among MPI ranks
1206f1580f4eSBarry Smith .  -pc_gasm_view_subdomains       - activates the printing of subdomain indices in `PCView()`, -ksp_view or -snes_view
1207f1580f4eSBarry Smith .  -pc_gasm_print_subdomains      - activates the printing of subdomain indices in `PCSetUp()`
120806b43e7eSDmitry Karpeev .  -pc_gasm_overlap <ovl>         - Sets overlap by which to (automatically) extend local subdomains
1209f1580f4eSBarry Smith -  -pc_gasm_type [basic,restrict,interpolate,none] - Sets `PCGASMType`
1210b1a0a8a3SJed Brown 
121195452b02SPatrick Sanan    Notes:
1212f1580f4eSBarry Smith    To set options on the solvers for each block append -sub_ to all the `KSP`, and `PC`
1213b1a0a8a3SJed Brown    options database keys. For example, -sub_pc_type ilu -sub_pc_factor_levels 1 -sub_ksp_type preonly
1214b1a0a8a3SJed Brown 
1215f1580f4eSBarry Smith    To set the options on the solvers separate for each block call `PCGASMGetSubKSP()`
1216f1580f4eSBarry Smith    and set the options directly on the resulting `KSP` object (you can access its `PC`
1217f1580f4eSBarry Smith    with `KSPGetPC())`
1218b1a0a8a3SJed Brown 
1219b1a0a8a3SJed Brown    Level: beginner
1220b1a0a8a3SJed Brown 
1221b1a0a8a3SJed Brown     References:
1222606c0280SSatish Balay +   * - M Dryja, OB Widlund, An additive variant of the Schwarz alternating method for the case of many subregions
122396a0c994SBarry Smith      Courant Institute, New York University Technical report
1224606c0280SSatish Balay -   * - Barry Smith, Petter Bjorstad, and William Gropp, Domain Decompositions: Parallel Multilevel Methods for Elliptic Partial Differential Equations,
122596a0c994SBarry Smith     Cambridge University Press.
1226b1a0a8a3SJed Brown 
1227f1580f4eSBarry Smith .seealso: `PCCreate()`, `PCSetType()`, `PCType`, `PC`, `PCASM`, `PCGASMType`, `PCGASMSetType()`,
1228db781477SPatrick Sanan           `PCBJACOBI`, `PCGASMGetSubKSP()`, `PCGASMSetSubdomains()`,
1229db781477SPatrick Sanan           `PCSetModifySubMatrices()`, `PCGASMSetOverlap()`, `PCGASMSetType()`
1230b1a0a8a3SJed Brown M*/
1231b1a0a8a3SJed Brown 
12329371c9d4SSatish Balay PETSC_EXTERN PetscErrorCode PCCreate_GASM(PC pc) {
1233f746d493SDmitry Karpeev   PC_GASM *osm;
1234b1a0a8a3SJed Brown 
1235b1a0a8a3SJed Brown   PetscFunctionBegin;
1236*4dfa11a4SJacob Faibussowitsch   PetscCall(PetscNew(&osm));
12372fa5cd67SKarl Rupp 
12388f3b4b4dSDmitry Karpeev   osm->N                        = PETSC_DETERMINE;
123906b43e7eSDmitry Karpeev   osm->n                        = PETSC_DECIDE;
12408f3b4b4dSDmitry Karpeev   osm->nmax                     = PETSC_DETERMINE;
12418f3b4b4dSDmitry Karpeev   osm->overlap                  = 0;
12420a545947SLisandro Dalcin   osm->ksp                      = NULL;
12430a545947SLisandro Dalcin   osm->gorestriction            = NULL;
12440a545947SLisandro Dalcin   osm->girestriction            = NULL;
12450a545947SLisandro Dalcin   osm->pctoouter                = NULL;
12460a545947SLisandro Dalcin   osm->gx                       = NULL;
12470a545947SLisandro Dalcin   osm->gy                       = NULL;
12480a545947SLisandro Dalcin   osm->x                        = NULL;
12490a545947SLisandro Dalcin   osm->y                        = NULL;
12500a545947SLisandro Dalcin   osm->pcx                      = NULL;
12510a545947SLisandro Dalcin   osm->pcy                      = NULL;
12520a545947SLisandro Dalcin   osm->permutationIS            = NULL;
12530a545947SLisandro Dalcin   osm->permutationP             = NULL;
12540a545947SLisandro Dalcin   osm->pcmat                    = NULL;
12550a545947SLisandro Dalcin   osm->ois                      = NULL;
12560a545947SLisandro Dalcin   osm->iis                      = NULL;
12570a545947SLisandro Dalcin   osm->pmat                     = NULL;
1258f746d493SDmitry Karpeev   osm->type                     = PC_GASM_RESTRICT;
12596a4f0f73SDmitry Karpeev   osm->same_subdomain_solvers   = PETSC_TRUE;
1260b1a0a8a3SJed Brown   osm->sort_indices             = PETSC_TRUE;
1261d709ea83SDmitry Karpeev   osm->dm_subdomains            = PETSC_FALSE;
1262ea91fabdSFande Kong   osm->hierarchicalpartitioning = PETSC_FALSE;
1263b1a0a8a3SJed Brown 
1264b1a0a8a3SJed Brown   pc->data                 = (void *)osm;
1265f746d493SDmitry Karpeev   pc->ops->apply           = PCApply_GASM;
126648e38a8aSPierre Jolivet   pc->ops->matapply        = PCMatApply_GASM;
1267f746d493SDmitry Karpeev   pc->ops->applytranspose  = PCApplyTranspose_GASM;
1268f746d493SDmitry Karpeev   pc->ops->setup           = PCSetUp_GASM;
1269a06653b4SBarry Smith   pc->ops->reset           = PCReset_GASM;
1270f746d493SDmitry Karpeev   pc->ops->destroy         = PCDestroy_GASM;
1271f746d493SDmitry Karpeev   pc->ops->setfromoptions  = PCSetFromOptions_GASM;
1272f746d493SDmitry Karpeev   pc->ops->setuponblocks   = PCSetUpOnBlocks_GASM;
1273f746d493SDmitry Karpeev   pc->ops->view            = PCView_GASM;
12740a545947SLisandro Dalcin   pc->ops->applyrichardson = NULL;
1275b1a0a8a3SJed Brown 
12769566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetSubdomains_C", PCGASMSetSubdomains_GASM));
12779566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetOverlap_C", PCGASMSetOverlap_GASM));
12789566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetType_C", PCGASMSetType_GASM));
12799566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetSortIndices_C", PCGASMSetSortIndices_GASM));
12809566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMGetSubKSP_C", PCGASMGetSubKSP_GASM));
1281b1a0a8a3SJed Brown   PetscFunctionReturn(0);
1282b1a0a8a3SJed Brown }
1283b1a0a8a3SJed Brown 
12849371c9d4SSatish Balay PetscErrorCode PCGASMCreateLocalSubdomains(Mat A, PetscInt nloc, IS *iis[]) {
1285b1a0a8a3SJed Brown   MatPartitioning mpart;
1286b1a0a8a3SJed Brown   const char     *prefix;
1287b1a0a8a3SJed Brown   PetscInt        i, j, rstart, rend, bs;
1288976e8c5aSLisandro Dalcin   PetscBool       hasop, isbaij = PETSC_FALSE, foundpart = PETSC_FALSE;
12890298fd71SBarry Smith   Mat             Ad = NULL, adj;
1290b1a0a8a3SJed Brown   IS              ispart, isnumb, *is;
1291b1a0a8a3SJed Brown 
1292b1a0a8a3SJed Brown   PetscFunctionBegin;
129363a3b9bcSJacob Faibussowitsch   PetscCheck(nloc >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "number of local subdomains must > 0, got nloc = %" PetscInt_FMT, nloc);
1294b1a0a8a3SJed Brown 
1295b1a0a8a3SJed Brown   /* Get prefix, row distribution, and block size */
12969566063dSJacob Faibussowitsch   PetscCall(MatGetOptionsPrefix(A, &prefix));
12979566063dSJacob Faibussowitsch   PetscCall(MatGetOwnershipRange(A, &rstart, &rend));
12989566063dSJacob Faibussowitsch   PetscCall(MatGetBlockSize(A, &bs));
12992472a847SBarry 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);
1300b1a0a8a3SJed Brown 
1301b1a0a8a3SJed Brown   /* Get diagonal block from matrix if possible */
13029566063dSJacob Faibussowitsch   PetscCall(MatHasOperation(A, MATOP_GET_DIAGONAL_BLOCK, &hasop));
130348a46eb9SPierre Jolivet   if (hasop) PetscCall(MatGetDiagonalBlock(A, &Ad));
1304b1a0a8a3SJed Brown   if (Ad) {
13059566063dSJacob Faibussowitsch     PetscCall(PetscObjectBaseTypeCompare((PetscObject)Ad, MATSEQBAIJ, &isbaij));
13069566063dSJacob Faibussowitsch     if (!isbaij) PetscCall(PetscObjectBaseTypeCompare((PetscObject)Ad, MATSEQSBAIJ, &isbaij));
1307b1a0a8a3SJed Brown   }
13088f3b4b4dSDmitry Karpeev   if (Ad && nloc > 1) {
1309b1a0a8a3SJed Brown     PetscBool match, done;
1310b1a0a8a3SJed Brown     /* Try to setup a good matrix partitioning if available */
13119566063dSJacob Faibussowitsch     PetscCall(MatPartitioningCreate(PETSC_COMM_SELF, &mpart));
13129566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)mpart, prefix));
13139566063dSJacob Faibussowitsch     PetscCall(MatPartitioningSetFromOptions(mpart));
13149566063dSJacob Faibussowitsch     PetscCall(PetscObjectTypeCompare((PetscObject)mpart, MATPARTITIONINGCURRENT, &match));
131548a46eb9SPierre Jolivet     if (!match) PetscCall(PetscObjectTypeCompare((PetscObject)mpart, MATPARTITIONINGSQUARE, &match));
1316b1a0a8a3SJed Brown     if (!match) { /* assume a "good" partitioner is available */
13171a83f524SJed Brown       PetscInt        na;
13181a83f524SJed Brown       const PetscInt *ia, *ja;
13199566063dSJacob Faibussowitsch       PetscCall(MatGetRowIJ(Ad, 0, PETSC_TRUE, isbaij, &na, &ia, &ja, &done));
1320b1a0a8a3SJed Brown       if (done) {
1321b1a0a8a3SJed Brown         /* Build adjacency matrix by hand. Unfortunately a call to
1322b1a0a8a3SJed Brown            MatConvert(Ad,MATMPIADJ,MAT_INITIAL_MATRIX,&adj) will
1323b1a0a8a3SJed Brown            remove the block-aij structure and we cannot expect
1324b1a0a8a3SJed Brown            MatPartitioning to split vertices as we need */
13250a545947SLisandro Dalcin         PetscInt        i, j, len, nnz, cnt, *iia = NULL, *jja = NULL;
13261a83f524SJed Brown         const PetscInt *row;
1327b1a0a8a3SJed Brown         nnz = 0;
1328b1a0a8a3SJed Brown         for (i = 0; i < na; i++) { /* count number of nonzeros */
1329b1a0a8a3SJed Brown           len = ia[i + 1] - ia[i];
1330b1a0a8a3SJed Brown           row = ja + ia[i];
1331b1a0a8a3SJed Brown           for (j = 0; j < len; j++) {
1332b1a0a8a3SJed Brown             if (row[j] == i) { /* don't count diagonal */
13339371c9d4SSatish Balay               len--;
13349371c9d4SSatish Balay               break;
1335b1a0a8a3SJed Brown             }
1336b1a0a8a3SJed Brown           }
1337b1a0a8a3SJed Brown           nnz += len;
1338b1a0a8a3SJed Brown         }
13399566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1(na + 1, &iia));
13409566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1(nnz, &jja));
1341b1a0a8a3SJed Brown         nnz    = 0;
1342b1a0a8a3SJed Brown         iia[0] = 0;
1343b1a0a8a3SJed Brown         for (i = 0; i < na; i++) { /* fill adjacency */
1344b1a0a8a3SJed Brown           cnt = 0;
1345b1a0a8a3SJed Brown           len = ia[i + 1] - ia[i];
1346b1a0a8a3SJed Brown           row = ja + ia[i];
1347b1a0a8a3SJed Brown           for (j = 0; j < len; j++) {
13482fa5cd67SKarl Rupp             if (row[j] != i) jja[nnz + cnt++] = row[j]; /* if not diagonal */
1349b1a0a8a3SJed Brown           }
1350b1a0a8a3SJed Brown           nnz += cnt;
1351b1a0a8a3SJed Brown           iia[i + 1] = nnz;
1352b1a0a8a3SJed Brown         }
1353b1a0a8a3SJed Brown         /* Partitioning of the adjacency matrix */
13549566063dSJacob Faibussowitsch         PetscCall(MatCreateMPIAdj(PETSC_COMM_SELF, na, na, iia, jja, NULL, &adj));
13559566063dSJacob Faibussowitsch         PetscCall(MatPartitioningSetAdjacency(mpart, adj));
13569566063dSJacob Faibussowitsch         PetscCall(MatPartitioningSetNParts(mpart, nloc));
13579566063dSJacob Faibussowitsch         PetscCall(MatPartitioningApply(mpart, &ispart));
13589566063dSJacob Faibussowitsch         PetscCall(ISPartitioningToNumbering(ispart, &isnumb));
13599566063dSJacob Faibussowitsch         PetscCall(MatDestroy(&adj));
1360b1a0a8a3SJed Brown         foundpart = PETSC_TRUE;
1361b1a0a8a3SJed Brown       }
13629566063dSJacob Faibussowitsch       PetscCall(MatRestoreRowIJ(Ad, 0, PETSC_TRUE, isbaij, &na, &ia, &ja, &done));
1363b1a0a8a3SJed Brown     }
13649566063dSJacob Faibussowitsch     PetscCall(MatPartitioningDestroy(&mpart));
1365b1a0a8a3SJed Brown   }
13669566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(nloc, &is));
1367b1a0a8a3SJed Brown   if (!foundpart) {
1368b1a0a8a3SJed Brown     /* Partitioning by contiguous chunks of rows */
1369b1a0a8a3SJed Brown 
1370b1a0a8a3SJed Brown     PetscInt mbs   = (rend - rstart) / bs;
1371b1a0a8a3SJed Brown     PetscInt start = rstart;
13728f3b4b4dSDmitry Karpeev     for (i = 0; i < nloc; i++) {
13738f3b4b4dSDmitry Karpeev       PetscInt count = (mbs / nloc + ((mbs % nloc) > i)) * bs;
13749566063dSJacob Faibussowitsch       PetscCall(ISCreateStride(PETSC_COMM_SELF, count, start, 1, &is[i]));
1375b1a0a8a3SJed Brown       start += count;
1376b1a0a8a3SJed Brown     }
1377b1a0a8a3SJed Brown 
1378b1a0a8a3SJed Brown   } else {
1379b1a0a8a3SJed Brown     /* Partitioning by adjacency of diagonal block  */
1380b1a0a8a3SJed Brown 
1381b1a0a8a3SJed Brown     const PetscInt *numbering;
1382b1a0a8a3SJed Brown     PetscInt       *count, nidx, *indices, *newidx, start = 0;
1383b1a0a8a3SJed Brown     /* Get node count in each partition */
13849566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nloc, &count));
13859566063dSJacob Faibussowitsch     PetscCall(ISPartitioningCount(ispart, nloc, count));
1386b1a0a8a3SJed Brown     if (isbaij && bs > 1) { /* adjust for the block-aij case */
13878f3b4b4dSDmitry Karpeev       for (i = 0; i < nloc; i++) count[i] *= bs;
1388b1a0a8a3SJed Brown     }
1389b1a0a8a3SJed Brown     /* Build indices from node numbering */
13909566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(isnumb, &nidx));
13919566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nidx, &indices));
1392b1a0a8a3SJed Brown     for (i = 0; i < nidx; i++) indices[i] = i; /* needs to be initialized */
13939566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(isnumb, &numbering));
13949566063dSJacob Faibussowitsch     PetscCall(PetscSortIntWithPermutation(nidx, numbering, indices));
13959566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(isnumb, &numbering));
1396b1a0a8a3SJed Brown     if (isbaij && bs > 1) { /* adjust for the block-aij case */
13979566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nidx * bs, &newidx));
13982fa5cd67SKarl Rupp       for (i = 0; i < nidx; i++) {
13992fa5cd67SKarl Rupp         for (j = 0; j < bs; j++) newidx[i * bs + j] = indices[i] * bs + j;
14002fa5cd67SKarl Rupp       }
14019566063dSJacob Faibussowitsch       PetscCall(PetscFree(indices));
1402b1a0a8a3SJed Brown       nidx *= bs;
1403b1a0a8a3SJed Brown       indices = newidx;
1404b1a0a8a3SJed Brown     }
1405b1a0a8a3SJed Brown     /* Shift to get global indices */
1406b1a0a8a3SJed Brown     for (i = 0; i < nidx; i++) indices[i] += rstart;
1407b1a0a8a3SJed Brown 
1408b1a0a8a3SJed Brown     /* Build the index sets for each block */
14098f3b4b4dSDmitry Karpeev     for (i = 0; i < nloc; i++) {
14109566063dSJacob Faibussowitsch       PetscCall(ISCreateGeneral(PETSC_COMM_SELF, count[i], &indices[start], PETSC_COPY_VALUES, &is[i]));
14119566063dSJacob Faibussowitsch       PetscCall(ISSort(is[i]));
1412b1a0a8a3SJed Brown       start += count[i];
1413b1a0a8a3SJed Brown     }
1414b1a0a8a3SJed Brown 
14159566063dSJacob Faibussowitsch     PetscCall(PetscFree(count));
14169566063dSJacob Faibussowitsch     PetscCall(PetscFree(indices));
14179566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&isnumb));
14189566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&ispart));
1419b1a0a8a3SJed Brown   }
14206a4f0f73SDmitry Karpeev   *iis = is;
14218f3b4b4dSDmitry Karpeev   PetscFunctionReturn(0);
14228f3b4b4dSDmitry Karpeev }
14238f3b4b4dSDmitry Karpeev 
14249371c9d4SSatish Balay PETSC_INTERN PetscErrorCode PCGASMCreateStraddlingSubdomains(Mat A, PetscInt N, PetscInt *n, IS *iis[]) {
14258f3b4b4dSDmitry Karpeev   PetscFunctionBegin;
14269566063dSJacob Faibussowitsch   PetscCall(MatSubdomainsCreateCoalesce(A, N, n, iis));
14278f3b4b4dSDmitry Karpeev   PetscFunctionReturn(0);
14288f3b4b4dSDmitry Karpeev }
14298f3b4b4dSDmitry Karpeev 
14308f3b4b4dSDmitry Karpeev /*@C
1431f1580f4eSBarry Smith    PCGASMCreateSubdomains - Creates n index sets defining n nonoverlapping subdomains for the `PCGASM` additive
14328f3b4b4dSDmitry Karpeev    Schwarz preconditioner for a any problem based on its matrix.
14338f3b4b4dSDmitry Karpeev 
14348f3b4b4dSDmitry Karpeev    Collective
14358f3b4b4dSDmitry Karpeev 
14368f3b4b4dSDmitry Karpeev    Input Parameters:
14378f3b4b4dSDmitry Karpeev +  A       - The global matrix operator
14388f3b4b4dSDmitry Karpeev -  N       - the number of global subdomains requested
14398f3b4b4dSDmitry Karpeev 
14408f3b4b4dSDmitry Karpeev    Output Parameters:
1441f1580f4eSBarry Smith +  n   - the number of subdomains created on this MPI rank
14428f3b4b4dSDmitry Karpeev -  iis - the array of index sets defining the local inner subdomains (on which the correction is applied)
14438f3b4b4dSDmitry Karpeev 
14448f3b4b4dSDmitry Karpeev    Level: advanced
14458f3b4b4dSDmitry Karpeev 
1446f1580f4eSBarry Smith    Note:
1447f1580f4eSBarry Smith    When N >= A's communicator size, each subdomain is local -- contained within a single MPI rank.
1448f1580f4eSBarry Smith          When N < size, the subdomains are 'straddling' (rank boundaries) and are no longer local.
1449f1580f4eSBarry Smith          The resulting subdomains can be use in `PCGASMSetSubdomains`(pc,n,iss,NULL).  The overlapping
14508f3b4b4dSDmitry Karpeev          outer subdomains will be automatically generated from these according to the requested amount of
14518f3b4b4dSDmitry Karpeev          overlap; this is currently supported only with local subdomains.
14528f3b4b4dSDmitry Karpeev 
1453f1580f4eSBarry Smith .seealso: `PCGASM`, `PCGASMSetSubdomains()`, `PCGASMDestroySubdomains()`
14548f3b4b4dSDmitry Karpeev @*/
14559371c9d4SSatish Balay PetscErrorCode PCGASMCreateSubdomains(Mat A, PetscInt N, PetscInt *n, IS *iis[]) {
14568f3b4b4dSDmitry Karpeev   PetscMPIInt size;
14578f3b4b4dSDmitry Karpeev 
14588f3b4b4dSDmitry Karpeev   PetscFunctionBegin;
14598f3b4b4dSDmitry Karpeev   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
14608f3b4b4dSDmitry Karpeev   PetscValidPointer(iis, 4);
14618f3b4b4dSDmitry Karpeev 
146263a3b9bcSJacob Faibussowitsch   PetscCheck(N >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of subdomains must be > 0, N = %" PetscInt_FMT, N);
14639566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)A), &size));
14648f3b4b4dSDmitry Karpeev   if (N >= size) {
14658f3b4b4dSDmitry Karpeev     *n = N / size + (N % size);
14669566063dSJacob Faibussowitsch     PetscCall(PCGASMCreateLocalSubdomains(A, *n, iis));
14676a4f0f73SDmitry Karpeev   } else {
14689566063dSJacob Faibussowitsch     PetscCall(PCGASMCreateStraddlingSubdomains(A, N, n, iis));
14696a4f0f73SDmitry Karpeev   }
1470b1a0a8a3SJed Brown   PetscFunctionReturn(0);
1471b1a0a8a3SJed Brown }
1472b1a0a8a3SJed Brown 
1473b1a0a8a3SJed Brown /*@C
1474f746d493SDmitry Karpeev    PCGASMDestroySubdomains - Destroys the index sets created with
1475f1580f4eSBarry Smith    `PCGASMCreateSubdomains()` or `PCGASMCreateSubdomains2D()`. Should be
1476f1580f4eSBarry Smith    called after setting subdomains with `PCGASMSetSubdomains()`.
1477b1a0a8a3SJed Brown 
1478b1a0a8a3SJed Brown    Collective
1479b1a0a8a3SJed Brown 
1480b1a0a8a3SJed Brown    Input Parameters:
1481b1a0a8a3SJed Brown +  n   - the number of index sets
14826a4f0f73SDmitry Karpeev .  iis - the array of inner subdomains,
14830298fd71SBarry Smith -  ois - the array of outer subdomains, can be NULL
1484b1a0a8a3SJed Brown 
14856a4f0f73SDmitry Karpeev    Level: intermediate
14866a4f0f73SDmitry Karpeev 
1487f1580f4eSBarry Smith    Note:
1488f1580f4eSBarry Smith    This is a convenience subroutine that walks each list,
1489f1580f4eSBarry Smith    destroys each `IS` on the list, and then frees the list. At the end the
14902c112581SDmitry Karpeev    list pointers are set to NULL.
1491b1a0a8a3SJed Brown 
1492f1580f4eSBarry Smith .seealso: `PCGASM`, `PCGASMCreateSubdomains()`, `PCGASMSetSubdomains()`
1493b1a0a8a3SJed Brown @*/
14949371c9d4SSatish Balay PetscErrorCode PCGASMDestroySubdomains(PetscInt n, IS **iis, IS **ois) {
1495b1a0a8a3SJed Brown   PetscInt i;
14965fd66863SKarl Rupp 
1497b1a0a8a3SJed Brown   PetscFunctionBegin;
1498a35b7b57SDmitry Karpeev   if (n <= 0) PetscFunctionReturn(0);
14996a4f0f73SDmitry Karpeev   if (ois) {
15002c112581SDmitry Karpeev     PetscValidPointer(ois, 3);
15012c112581SDmitry Karpeev     if (*ois) {
15022c112581SDmitry Karpeev       PetscValidPointer(*ois, 3);
150348a46eb9SPierre Jolivet       for (i = 0; i < n; i++) PetscCall(ISDestroy(&(*ois)[i]));
15049566063dSJacob Faibussowitsch       PetscCall(PetscFree((*ois)));
15052c112581SDmitry Karpeev     }
1506b1a0a8a3SJed Brown   }
1507b9d0fdaaSFande Kong   if (iis) {
1508b9d0fdaaSFande Kong     PetscValidPointer(iis, 2);
1509b9d0fdaaSFande Kong     if (*iis) {
1510b9d0fdaaSFande Kong       PetscValidPointer(*iis, 2);
151148a46eb9SPierre Jolivet       for (i = 0; i < n; i++) PetscCall(ISDestroy(&(*iis)[i]));
15129566063dSJacob Faibussowitsch       PetscCall(PetscFree((*iis)));
1513b9d0fdaaSFande Kong     }
1514b9d0fdaaSFande Kong   }
1515b1a0a8a3SJed Brown   PetscFunctionReturn(0);
1516b1a0a8a3SJed Brown }
1517b1a0a8a3SJed Brown 
1518af538404SDmitry Karpeev #define PCGASMLocalSubdomainBounds2D(M, N, xleft, ylow, xright, yhigh, first, last, xleft_loc, ylow_loc, xright_loc, yhigh_loc, n) \
1519af538404SDmitry Karpeev   { \
1520af538404SDmitry Karpeev     PetscInt first_row = first / M, last_row = last / M + 1; \
1521af538404SDmitry Karpeev     /*                                                                                                    \
1522af538404SDmitry Karpeev      Compute ylow_loc and yhigh_loc so that (ylow_loc,xleft) and (yhigh_loc,xright) are the corners       \
1523af538404SDmitry Karpeev      of the bounding box of the intersection of the subdomain with the local ownership range (local       \
1524af538404SDmitry Karpeev      subdomain).                                                                                          \
1525af538404SDmitry Karpeev      Also compute xleft_loc and xright_loc as the lower and upper bounds on the first and last rows       \
1526af538404SDmitry Karpeev      of the intersection.                                                                                 \
1527af538404SDmitry Karpeev     */ \
1528af538404SDmitry Karpeev     /* ylow_loc is the grid row containing the first element of the local sumbdomain */ \
1529eec7e3faSDmitry Karpeev     *ylow_loc   = PetscMax(first_row, ylow); \
1530af538404SDmitry Karpeev     /* xleft_loc is the offset of first element of the local subdomain within its grid row (might actually be outside the local subdomain) */ \
1531eec7e3faSDmitry Karpeev     *xleft_loc  = *ylow_loc == first_row ? PetscMax(first % M, xleft) : xleft; \
1532af538404SDmitry Karpeev     /* yhigh_loc is the grid row above the last local subdomain element */ \
1533eec7e3faSDmitry Karpeev     *yhigh_loc  = PetscMin(last_row, yhigh); \
1534af538404SDmitry Karpeev     /* xright is the offset of the end of the  local subdomain within its grid row (might actually be outside the local subdomain) */ \
1535eec7e3faSDmitry Karpeev     *xright_loc = *yhigh_loc == last_row ? PetscMin(xright, last % M) : xright; \
1536af538404SDmitry Karpeev     /* Now compute the size of the local subdomain n. */ \
1537c3518bceSDmitry Karpeev     *n          = 0; \
1538eec7e3faSDmitry Karpeev     if (*ylow_loc < *yhigh_loc) { \
1539af538404SDmitry Karpeev       PetscInt width = xright - xleft; \
1540eec7e3faSDmitry Karpeev       *n += width * (*yhigh_loc - *ylow_loc - 1); \
1541eec7e3faSDmitry Karpeev       *n += PetscMin(PetscMax(*xright_loc - xleft, 0), width); \
1542eec7e3faSDmitry Karpeev       *n -= PetscMin(PetscMax(*xleft_loc - xleft, 0), width); \
1543af538404SDmitry Karpeev     } \
1544af538404SDmitry Karpeev   }
1545af538404SDmitry Karpeev 
1546b1a0a8a3SJed Brown /*@
1547f1580f4eSBarry Smith    PCGASMCreateSubdomains2D - Creates the index sets for the `PCGASM` overlapping Schwarz
1548b1a0a8a3SJed Brown    preconditioner for a two-dimensional problem on a regular grid.
1549b1a0a8a3SJed Brown 
1550af538404SDmitry Karpeev    Collective
1551b1a0a8a3SJed Brown 
1552b1a0a8a3SJed Brown    Input Parameters:
15536b867d5aSJose E. Roman +  pc       - the preconditioner context
15546b867d5aSJose E. Roman .  M        - the global number of grid points in the x direction
15556b867d5aSJose E. Roman .  N        - the global number of grid points in the y direction
15566b867d5aSJose E. Roman .  Mdomains - the global number of subdomains in the x direction
15576b867d5aSJose E. Roman .  Ndomains - the global number of subdomains in the y direction
1558b1a0a8a3SJed Brown .  dof      - degrees of freedom per node
1559b1a0a8a3SJed Brown -  overlap  - overlap in mesh lines
1560b1a0a8a3SJed Brown 
1561b1a0a8a3SJed Brown    Output Parameters:
1562af538404SDmitry Karpeev +  Nsub - the number of local subdomains created
15636a4f0f73SDmitry Karpeev .  iis  - array of index sets defining inner (nonoverlapping) subdomains
15646a4f0f73SDmitry Karpeev -  ois  - array of index sets defining outer (overlapping, if overlap > 0) subdomains
1565b1a0a8a3SJed Brown 
1566b1a0a8a3SJed Brown    Level: advanced
1567b1a0a8a3SJed Brown 
1568f1580f4eSBarry Smith .seealso: `PCGASM`, `PCGASMSetSubdomains()`, `PCGASMGetSubKSP()`, `PCGASMSetOverlap()`
1569b1a0a8a3SJed Brown @*/
15709371c9d4SSatish Balay PetscErrorCode PCGASMCreateSubdomains2D(PC pc, PetscInt M, PetscInt N, PetscInt Mdomains, PetscInt Ndomains, PetscInt dof, PetscInt overlap, PetscInt *nsub, IS **iis, IS **ois) {
15712bbb417fSDmitry Karpeev   PetscMPIInt size, rank;
15722bbb417fSDmitry Karpeev   PetscInt    i, j;
15732bbb417fSDmitry Karpeev   PetscInt    maxheight, maxwidth;
15742bbb417fSDmitry Karpeev   PetscInt    xstart, xleft, xright, xleft_loc, xright_loc;
15752bbb417fSDmitry Karpeev   PetscInt    ystart, ylow, yhigh, ylow_loc, yhigh_loc;
1576eec7e3faSDmitry Karpeev   PetscInt    x[2][2], y[2][2], n[2];
15772bbb417fSDmitry Karpeev   PetscInt    first, last;
15782bbb417fSDmitry Karpeev   PetscInt    nidx, *idx;
15792bbb417fSDmitry Karpeev   PetscInt    ii, jj, s, q, d;
1580af538404SDmitry Karpeev   PetscInt    k, kk;
15812bbb417fSDmitry Karpeev   PetscMPIInt color;
15822bbb417fSDmitry Karpeev   MPI_Comm    comm, subcomm;
15830a545947SLisandro Dalcin   IS        **xis = NULL, **is = ois, **is_local = iis;
1584d34fcf5fSBarry Smith 
1585b1a0a8a3SJed Brown   PetscFunctionBegin;
15869566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)pc, &comm));
15879566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
15889566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
15899566063dSJacob Faibussowitsch   PetscCall(MatGetOwnershipRange(pc->pmat, &first, &last));
15909371c9d4SSatish Balay   PetscCheck((first % dof) == 0 && (last % dof) == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
15919371c9d4SSatish Balay              "Matrix row partitioning unsuitable for domain decomposition: local row range (%" PetscInt_FMT ",%" PetscInt_FMT ") "
15929371c9d4SSatish Balay              "does not respect the number of degrees of freedom per grid point %" PetscInt_FMT,
15939371c9d4SSatish Balay              first, last, dof);
1594d34fcf5fSBarry Smith 
1595af538404SDmitry Karpeev   /* Determine the number of domains with nonzero intersections with the local ownership range. */
15962bbb417fSDmitry Karpeev   s      = 0;
1597af538404SDmitry Karpeev   ystart = 0;
1598af538404SDmitry Karpeev   for (j = 0; j < Ndomains; ++j) {
1599af538404SDmitry Karpeev     maxheight = N / Ndomains + ((N % Ndomains) > j); /* Maximal height of subdomain */
160063a3b9bcSJacob Faibussowitsch     PetscCheck(maxheight >= 2, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many %" PetscInt_FMT " subdomains in the vertical directon for mesh height %" PetscInt_FMT, Ndomains, N);
1601eec7e3faSDmitry Karpeev     /* Vertical domain limits with an overlap. */
1602eec7e3faSDmitry Karpeev     ylow   = PetscMax(ystart - overlap, 0);
1603eec7e3faSDmitry Karpeev     yhigh  = PetscMin(ystart + maxheight + overlap, N);
1604b1a0a8a3SJed Brown     xstart = 0;
1605af538404SDmitry Karpeev     for (i = 0; i < Mdomains; ++i) {
1606af538404SDmitry Karpeev       maxwidth = M / Mdomains + ((M % Mdomains) > i); /* Maximal width of subdomain */
160763a3b9bcSJacob 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);
1608eec7e3faSDmitry Karpeev       /* Horizontal domain limits with an overlap. */
1609eec7e3faSDmitry Karpeev       xleft  = PetscMax(xstart - overlap, 0);
1610eec7e3faSDmitry Karpeev       xright = PetscMin(xstart + maxwidth + overlap, M);
1611af538404SDmitry Karpeev       /*
1612f1580f4eSBarry Smith          Determine whether this subdomain intersects this rank's ownership range of pc->pmat.
1613af538404SDmitry Karpeev       */
1614c3518bceSDmitry Karpeev       PCGASMLocalSubdomainBounds2D(M, N, xleft, ylow, xright, yhigh, first, last, (&xleft_loc), (&ylow_loc), (&xright_loc), (&yhigh_loc), (&nidx));
16152fa5cd67SKarl Rupp       if (nidx) ++s;
1616af538404SDmitry Karpeev       xstart += maxwidth;
1617af538404SDmitry Karpeev     } /* for (i = 0; i < Mdomains; ++i) */
1618af538404SDmitry Karpeev     ystart += maxheight;
1619af538404SDmitry Karpeev   } /* for (j = 0; j < Ndomains; ++j) */
16202fa5cd67SKarl Rupp 
1621af538404SDmitry Karpeev   /* Now we can allocate the necessary number of ISs. */
1622af538404SDmitry Karpeev   *nsub = s;
16239566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(*nsub, is));
16249566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(*nsub, is_local));
1625af538404SDmitry Karpeev   s      = 0;
1626af538404SDmitry Karpeev   ystart = 0;
1627af538404SDmitry Karpeev   for (j = 0; j < Ndomains; ++j) {
1628af538404SDmitry Karpeev     maxheight = N / Ndomains + ((N % Ndomains) > j); /* Maximal height of subdomain */
162963a3b9bcSJacob Faibussowitsch     PetscCheck(maxheight >= 2, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many %" PetscInt_FMT " subdomains in the vertical directon for mesh height %" PetscInt_FMT, Ndomains, N);
1630af538404SDmitry Karpeev     /* Vertical domain limits with an overlap. */
1631eec7e3faSDmitry Karpeev     y[0][0] = PetscMax(ystart - overlap, 0);
1632eec7e3faSDmitry Karpeev     y[0][1] = PetscMin(ystart + maxheight + overlap, N);
1633eec7e3faSDmitry Karpeev     /* Vertical domain limits without an overlap. */
1634eec7e3faSDmitry Karpeev     y[1][0] = ystart;
1635eec7e3faSDmitry Karpeev     y[1][1] = PetscMin(ystart + maxheight, N);
1636eec7e3faSDmitry Karpeev     xstart  = 0;
1637af538404SDmitry Karpeev     for (i = 0; i < Mdomains; ++i) {
1638af538404SDmitry Karpeev       maxwidth = M / Mdomains + ((M % Mdomains) > i); /* Maximal width of subdomain */
163963a3b9bcSJacob 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);
1640af538404SDmitry Karpeev       /* Horizontal domain limits with an overlap. */
1641eec7e3faSDmitry Karpeev       x[0][0] = PetscMax(xstart - overlap, 0);
1642eec7e3faSDmitry Karpeev       x[0][1] = PetscMin(xstart + maxwidth + overlap, M);
1643eec7e3faSDmitry Karpeev       /* Horizontal domain limits without an overlap. */
1644eec7e3faSDmitry Karpeev       x[1][0] = xstart;
1645eec7e3faSDmitry Karpeev       x[1][1] = PetscMin(xstart + maxwidth, M);
16462bbb417fSDmitry Karpeev       /*
1647f1580f4eSBarry Smith          Determine whether this domain intersects this rank's ownership range of pc->pmat.
16482bbb417fSDmitry Karpeev          Do this twice: first for the domains with overlaps, and once without.
16492bbb417fSDmitry Karpeev          During the first pass create the subcommunicators, and use them on the second pass as well.
16502bbb417fSDmitry Karpeev       */
16512bbb417fSDmitry Karpeev       for (q = 0; q < 2; ++q) {
1652cc96b2e5SBarry Smith         PetscBool split = PETSC_FALSE;
16532bbb417fSDmitry Karpeev         /*
16542bbb417fSDmitry Karpeev           domain limits, (xleft, xright) and (ylow, yheigh) are adjusted
16552bbb417fSDmitry Karpeev           according to whether the domain with an overlap or without is considered.
16562bbb417fSDmitry Karpeev         */
16579371c9d4SSatish Balay         xleft           = x[q][0];
16589371c9d4SSatish Balay         xright          = x[q][1];
16599371c9d4SSatish Balay         ylow            = y[q][0];
16609371c9d4SSatish Balay         yhigh           = y[q][1];
1661c3518bceSDmitry Karpeev         PCGASMLocalSubdomainBounds2D(M, N, xleft, ylow, xright, yhigh, first, last, (&xleft_loc), (&ylow_loc), (&xright_loc), (&yhigh_loc), (&nidx));
1662af538404SDmitry Karpeev         nidx *= dof;
1663eec7e3faSDmitry Karpeev         n[q] = nidx;
16642bbb417fSDmitry Karpeev         /*
1665eec7e3faSDmitry Karpeev          Based on the counted number of indices in the local domain *with an overlap*,
1666f1580f4eSBarry Smith          construct a subcommunicator of all the MPI ranks supporting this domain.
1667eec7e3faSDmitry Karpeev          Observe that a domain with an overlap might have nontrivial local support,
1668eec7e3faSDmitry Karpeev          while the domain without an overlap might not.  Hence, the decision to participate
1669eec7e3faSDmitry Karpeev          in the subcommunicator must be based on the domain with an overlap.
16702bbb417fSDmitry Karpeev          */
16712bbb417fSDmitry Karpeev         if (q == 0) {
16722fa5cd67SKarl Rupp           if (nidx) color = 1;
16732fa5cd67SKarl Rupp           else color = MPI_UNDEFINED;
16749566063dSJacob Faibussowitsch           PetscCallMPI(MPI_Comm_split(comm, color, rank, &subcomm));
1675cc96b2e5SBarry Smith           split = PETSC_TRUE;
16762bbb417fSDmitry Karpeev         }
1677af538404SDmitry Karpeev         /*
1678eec7e3faSDmitry Karpeev          Proceed only if the number of local indices *with an overlap* is nonzero.
1679af538404SDmitry Karpeev          */
1680eec7e3faSDmitry Karpeev         if (n[0]) {
16812fa5cd67SKarl Rupp           if (q == 0) xis = is;
1682af538404SDmitry Karpeev           if (q == 1) {
1683af538404SDmitry Karpeev             /*
1684eec7e3faSDmitry Karpeev              The IS for the no-overlap subdomain shares a communicator with the overlapping domain.
1685af538404SDmitry Karpeev              Moreover, if the overlap is zero, the two ISs are identical.
1686af538404SDmitry Karpeev              */
1687b1a0a8a3SJed Brown             if (overlap == 0) {
1688eec7e3faSDmitry Karpeev               (*is_local)[s] = (*is)[s];
16899566063dSJacob Faibussowitsch               PetscCall(PetscObjectReference((PetscObject)(*is)[s]));
16902bbb417fSDmitry Karpeev               continue;
1691d34fcf5fSBarry Smith             } else {
16926a4f0f73SDmitry Karpeev               xis     = is_local;
1693eec7e3faSDmitry Karpeev               subcomm = ((PetscObject)(*is)[s])->comm;
16942bbb417fSDmitry Karpeev             }
1695af538404SDmitry Karpeev           } /* if (q == 1) */
16960298fd71SBarry Smith           idx = NULL;
16979566063dSJacob Faibussowitsch           PetscCall(PetscMalloc1(nidx, &idx));
1698eec7e3faSDmitry Karpeev           if (nidx) {
16992bbb417fSDmitry Karpeev             k = 0;
17002bbb417fSDmitry Karpeev             for (jj = ylow_loc; jj < yhigh_loc; ++jj) {
1701af538404SDmitry Karpeev               PetscInt x0 = (jj == ylow_loc) ? xleft_loc : xleft;
1702af538404SDmitry Karpeev               PetscInt x1 = (jj == yhigh_loc - 1) ? xright_loc : xright;
1703af538404SDmitry Karpeev               kk          = dof * (M * jj + x0);
17042bbb417fSDmitry Karpeev               for (ii = x0; ii < x1; ++ii) {
1705ad540459SPierre Jolivet                 for (d = 0; d < dof; ++d) idx[k++] = kk++;
1706b1a0a8a3SJed Brown               }
1707b1a0a8a3SJed Brown             }
1708eec7e3faSDmitry Karpeev           }
17099566063dSJacob Faibussowitsch           PetscCall(ISCreateGeneral(subcomm, nidx, idx, PETSC_OWN_POINTER, (*xis) + s));
171048a46eb9SPierre Jolivet           if (split) PetscCallMPI(MPI_Comm_free(&subcomm));
1711eec7e3faSDmitry Karpeev         } /* if (n[0]) */
17122bbb417fSDmitry Karpeev       }   /* for (q = 0; q < 2; ++q) */
17132fa5cd67SKarl Rupp       if (n[0]) ++s;
1714af538404SDmitry Karpeev       xstart += maxwidth;
1715af538404SDmitry Karpeev     } /* for (i = 0; i < Mdomains; ++i) */
17162bbb417fSDmitry Karpeev     ystart += maxheight;
1717af538404SDmitry Karpeev   } /* for (j = 0; j < Ndomains; ++j) */
1718b1a0a8a3SJed Brown   PetscFunctionReturn(0);
1719b1a0a8a3SJed Brown }
1720b1a0a8a3SJed Brown 
1721b1a0a8a3SJed Brown /*@C
1722f1580f4eSBarry Smith     PCGASMGetSubdomains - Gets the subdomains supported on this MPI rank
1723f1580f4eSBarry Smith     for the `PCGASM` additive Schwarz preconditioner.
1724b1a0a8a3SJed Brown 
1725b1a0a8a3SJed Brown     Not Collective
1726b1a0a8a3SJed Brown 
1727b1a0a8a3SJed Brown     Input Parameter:
1728b1a0a8a3SJed Brown .   pc - the preconditioner context
1729b1a0a8a3SJed Brown 
1730b1a0a8a3SJed Brown     Output Parameters:
1731f1580f4eSBarry Smith +   n   - the number of subdomains for this MPI rank (default value = 1)
1732f1580f4eSBarry Smith .   iis - the index sets that define the inner subdomains (without overlap) supported on this rank (can be NULL)
1733f1580f4eSBarry Smith -   ois - the index sets that define the outer subdomains (with overlap) supported on this rank (can be NULL)
1734b1a0a8a3SJed Brown 
1735f1580f4eSBarry Smith     Note:
1736f1580f4eSBarry Smith     The user is responsible for destroying the `IS`s and freeing the returned arrays.
1737f1580f4eSBarry Smith     The `IS` numbering is in the parallel, global numbering of the vector.
1738b1a0a8a3SJed Brown 
1739b1a0a8a3SJed Brown     Level: advanced
1740b1a0a8a3SJed Brown 
1741f1580f4eSBarry Smith .seealso: `PCGASM`, `PCGASMSetOverlap()`, `PCGASMGetSubKSP()`, `PCGASMCreateSubdomains2D()`,
1742db781477SPatrick Sanan           `PCGASMSetSubdomains()`, `PCGASMGetSubmatrices()`
1743b1a0a8a3SJed Brown @*/
17449371c9d4SSatish Balay PetscErrorCode PCGASMGetSubdomains(PC pc, PetscInt *n, IS *iis[], IS *ois[]) {
1745f746d493SDmitry Karpeev   PC_GASM  *osm;
1746b1a0a8a3SJed Brown   PetscBool match;
17476a4f0f73SDmitry Karpeev   PetscInt  i;
17485fd66863SKarl Rupp 
1749b1a0a8a3SJed Brown   PetscFunctionBegin;
1750b1a0a8a3SJed Brown   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
17519566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCGASM, &match));
175228b400f6SJacob Faibussowitsch   PetscCheck(match, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONG, "Incorrect object type: expected %s, got %s instead", PCGASM, ((PetscObject)pc)->type_name);
1753f746d493SDmitry Karpeev   osm = (PC_GASM *)pc->data;
1754ab3e923fSDmitry Karpeev   if (n) *n = osm->n;
17551baa6e33SBarry Smith   if (iis) PetscCall(PetscMalloc1(osm->n, iis));
17561baa6e33SBarry Smith   if (ois) PetscCall(PetscMalloc1(osm->n, ois));
17576a4f0f73SDmitry Karpeev   if (iis || ois) {
17586a4f0f73SDmitry Karpeev     for (i = 0; i < osm->n; ++i) {
17596a4f0f73SDmitry Karpeev       if (iis) (*iis)[i] = osm->iis[i];
17606a4f0f73SDmitry Karpeev       if (ois) (*ois)[i] = osm->ois[i];
17616a4f0f73SDmitry Karpeev     }
1762b1a0a8a3SJed Brown   }
1763b1a0a8a3SJed Brown   PetscFunctionReturn(0);
1764b1a0a8a3SJed Brown }
1765b1a0a8a3SJed Brown 
1766b1a0a8a3SJed Brown /*@C
1767f1580f4eSBarry Smith     PCGASMGetSubmatrices - Gets the local submatrices (for this MPI rank
1768f1580f4eSBarry Smith     only) for the `PCGASM` additive Schwarz preconditioner.
1769b1a0a8a3SJed Brown 
1770b1a0a8a3SJed Brown     Not Collective
1771b1a0a8a3SJed Brown 
1772b1a0a8a3SJed Brown     Input Parameter:
1773b1a0a8a3SJed Brown .   pc - the preconditioner context
1774b1a0a8a3SJed Brown 
1775b1a0a8a3SJed Brown     Output Parameters:
1776f1580f4eSBarry Smith +   n   - the number of matrices for this MPI rank (default value = 1)
1777b1a0a8a3SJed Brown -   mat - the matrices
1778b1a0a8a3SJed Brown 
1779f1580f4eSBarry Smith     Note:
1780f1580f4eSBarry Smith     Matrices returned by this routine have the same communicators as the index sets (IS)
1781f1580f4eSBarry Smith            used to define subdomains in `PCGASMSetSubdomains()`
1782f1580f4eSBarry Smith 
1783b1a0a8a3SJed Brown     Level: advanced
1784b1a0a8a3SJed Brown 
1785f1580f4eSBarry Smith .seealso: `PCGASM`, `PCGASMSetOverlap()`, `PCGASMGetSubKSP()`,
1786db781477SPatrick Sanan           `PCGASMCreateSubdomains2D()`, `PCGASMSetSubdomains()`, `PCGASMGetSubdomains()`
1787b1a0a8a3SJed Brown @*/
17889371c9d4SSatish Balay PetscErrorCode PCGASMGetSubmatrices(PC pc, PetscInt *n, Mat *mat[]) {
1789f746d493SDmitry Karpeev   PC_GASM  *osm;
1790b1a0a8a3SJed Brown   PetscBool match;
1791b1a0a8a3SJed Brown 
1792b1a0a8a3SJed Brown   PetscFunctionBegin;
1793b1a0a8a3SJed Brown   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1794b1a0a8a3SJed Brown   PetscValidIntPointer(n, 2);
1795b1a0a8a3SJed Brown   if (mat) PetscValidPointer(mat, 3);
179628b400f6SJacob Faibussowitsch   PetscCheck(pc->setupcalled, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Must call after KSPSetUp() or PCSetUp().");
17979566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCGASM, &match));
179828b400f6SJacob Faibussowitsch   PetscCheck(match, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONG, "Expected %s, got %s instead", PCGASM, ((PetscObject)pc)->type_name);
1799f746d493SDmitry Karpeev   osm = (PC_GASM *)pc->data;
1800ab3e923fSDmitry Karpeev   if (n) *n = osm->n;
1801b1a0a8a3SJed Brown   if (mat) *mat = osm->pmat;
1802b1a0a8a3SJed Brown   PetscFunctionReturn(0);
1803b1a0a8a3SJed Brown }
1804d709ea83SDmitry Karpeev 
1805d709ea83SDmitry Karpeev /*@
1806f1580f4eSBarry Smith     PCGASMSetUseDMSubdomains - Indicates whether to use `DMCreateDomainDecomposition()` to define the subdomains, whenever possible for `PCGASM`
1807f1580f4eSBarry Smith 
1808d709ea83SDmitry Karpeev     Logically Collective
1809d709ea83SDmitry Karpeev 
1810d8d19677SJose E. Roman     Input Parameters:
1811d709ea83SDmitry Karpeev +   pc  - the preconditioner
1812f1580f4eSBarry Smith -   flg - boolean indicating whether to use subdomains defined by the `DM`
1813d709ea83SDmitry Karpeev 
1814d709ea83SDmitry Karpeev     Options Database Key:
18158f3b4b4dSDmitry Karpeev .   -pc_gasm_dm_subdomains -pc_gasm_overlap -pc_gasm_total_subdomains
1816d709ea83SDmitry Karpeev 
1817d709ea83SDmitry Karpeev     Level: intermediate
1818d709ea83SDmitry Karpeev 
1819f1580f4eSBarry Smith     Note:
1820f1580f4eSBarry Smith     `PCGASMSetSubdomains()`, `PCGASMSetTotalSubdomains()` or `PCGASMSetOverlap()` take precedence over `PCGASMSetUseDMSubdomains()`,
1821f1580f4eSBarry Smith     so setting `PCGASMSetSubdomains()` with nontrivial subdomain ISs or any of `PCGASMSetTotalSubdomains()` and `PCGASMSetOverlap()`
18228f3b4b4dSDmitry Karpeev     automatically turns the latter off.
1823d709ea83SDmitry Karpeev 
1824f1580f4eSBarry Smith .seealso: `PCGASM`, `PCGASMGetUseDMSubdomains()`, `PCGASMSetSubdomains()`, `PCGASMSetOverlap()`
1825db781477SPatrick Sanan           `PCGASMCreateSubdomains2D()`
1826d709ea83SDmitry Karpeev @*/
18279371c9d4SSatish Balay PetscErrorCode PCGASMSetUseDMSubdomains(PC pc, PetscBool flg) {
1828d709ea83SDmitry Karpeev   PC_GASM  *osm = (PC_GASM *)pc->data;
1829d709ea83SDmitry Karpeev   PetscBool match;
1830d709ea83SDmitry Karpeev 
1831d709ea83SDmitry Karpeev   PetscFunctionBegin;
1832d709ea83SDmitry Karpeev   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1833d709ea83SDmitry Karpeev   PetscValidLogicalCollectiveBool(pc, flg, 2);
183428b400f6SJacob Faibussowitsch   PetscCheck(!pc->setupcalled, ((PetscObject)pc)->comm, PETSC_ERR_ARG_WRONGSTATE, "Not for a setup PC.");
18359566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCGASM, &match));
1836d709ea83SDmitry Karpeev   if (match) {
1837ad540459SPierre Jolivet     if (!osm->user_subdomains && osm->N == PETSC_DETERMINE && osm->overlap < 0) osm->dm_subdomains = flg;
18388f3b4b4dSDmitry Karpeev   }
1839d709ea83SDmitry Karpeev   PetscFunctionReturn(0);
1840d709ea83SDmitry Karpeev }
1841d709ea83SDmitry Karpeev 
1842d709ea83SDmitry Karpeev /*@
1843f1580f4eSBarry Smith     PCGASMGetUseDMSubdomains - Returns flag indicating whether to use `DMCreateDomainDecomposition()` to define the subdomains, whenever possible with `PCGASM`
1844f1580f4eSBarry Smith 
1845d709ea83SDmitry Karpeev     Not Collective
1846d709ea83SDmitry Karpeev 
1847d709ea83SDmitry Karpeev     Input Parameter:
1848d709ea83SDmitry Karpeev .   pc  - the preconditioner
1849d709ea83SDmitry Karpeev 
1850d709ea83SDmitry Karpeev     Output Parameter:
1851f1580f4eSBarry Smith .   flg - boolean indicating whether to use subdomains defined by the `DM`
1852d709ea83SDmitry Karpeev 
1853d709ea83SDmitry Karpeev     Level: intermediate
1854d709ea83SDmitry Karpeev 
1855f1580f4eSBarry Smith .seealso: `PCGASM`, `PCGASMSetUseDMSubdomains()`, `PCGASMSetOverlap()`
1856db781477SPatrick Sanan           `PCGASMCreateSubdomains2D()`
1857d709ea83SDmitry Karpeev @*/
18589371c9d4SSatish Balay PetscErrorCode PCGASMGetUseDMSubdomains(PC pc, PetscBool *flg) {
1859d709ea83SDmitry Karpeev   PC_GASM  *osm = (PC_GASM *)pc->data;
1860d709ea83SDmitry Karpeev   PetscBool match;
1861d709ea83SDmitry Karpeev 
1862d709ea83SDmitry Karpeev   PetscFunctionBegin;
1863d709ea83SDmitry Karpeev   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1864534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
18659566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCGASM, &match));
1866d709ea83SDmitry Karpeev   if (match) {
1867d709ea83SDmitry Karpeev     if (flg) *flg = osm->dm_subdomains;
1868d709ea83SDmitry Karpeev   }
1869d709ea83SDmitry Karpeev   PetscFunctionReturn(0);
1870d709ea83SDmitry Karpeev }
1871