1b1a0a8a3SJed Brown /* 2f746d493SDmitry Karpeev This file defines an "generalized" additive Schwarz preconditioner for any Mat implementation. 3*f1580f4eSBarry Smith In this version each MPI rank may intersect multiple subdomains and any subdomain may 4*f1580f4eSBarry 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()) 8*f1580f4eSBarry Smith n - actual number of local subdomains on this rank (set in PCGASMSetSubdomains() or calculated in PCGASMSetTotalSubdomains()) 9*f1580f4eSBarry 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); 258*f1580f4eSBarry 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)); 267*f1580f4eSBarry 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 { 336*f1580f4eSBarry 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(PetscLogObjectParent((PetscObject)pc, (PetscObject)ksp)); 5119566063dSJacob Faibussowitsch PetscCall(PetscObjectIncrementTabLevel((PetscObject)ksp, (PetscObject)pc, 1)); 5129566063dSJacob Faibussowitsch PetscCall(KSPSetType(ksp, KSPPREONLY)); 5139566063dSJacob Faibussowitsch PetscCall(KSPGetPC(ksp, &subpc)); /* Why do we need this here? */ 5148f3b4b4dSDmitry Karpeev if (subdomain_dm) { 5159566063dSJacob Faibussowitsch PetscCall(KSPSetDM(ksp, subdomain_dm[i])); 5169566063dSJacob Faibussowitsch PetscCall(DMDestroy(subdomain_dm + i)); 5178f3b4b4dSDmitry Karpeev } 5189566063dSJacob Faibussowitsch PetscCall(PCGetOptionsPrefix(pc, &prefix)); 5199566063dSJacob Faibussowitsch PetscCall(KSPSetOptionsPrefix(ksp, prefix)); 5208f3b4b4dSDmitry Karpeev if (subdomain_names && subdomain_names[i]) { 5219566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(subprefix, PETSC_MAX_PATH_LEN, "sub_%s_", subdomain_names[i])); 5229566063dSJacob Faibussowitsch PetscCall(KSPAppendOptionsPrefix(ksp, subprefix)); 5239566063dSJacob Faibussowitsch PetscCall(PetscFree(subdomain_names[i])); 5248f3b4b4dSDmitry Karpeev } 5259566063dSJacob Faibussowitsch PetscCall(KSPAppendOptionsPrefix(ksp, "sub_")); 526b1a0a8a3SJed Brown osm->ksp[i] = ksp; 527b1a0a8a3SJed Brown } 5289566063dSJacob Faibussowitsch PetscCall(PetscFree(subdomain_dm)); 5299566063dSJacob Faibussowitsch PetscCall(PetscFree(subdomain_names)); 530b1a0a8a3SJed Brown scall = MAT_INITIAL_MATRIX; 5318f3b4b4dSDmitry Karpeev } else { /* if (pc->setupcalled) */ 532b1a0a8a3SJed Brown /* 5338f3b4b4dSDmitry Karpeev Destroy the submatrices from the previous iteration 534b1a0a8a3SJed Brown */ 535b1a0a8a3SJed Brown if (pc->flag == DIFFERENT_NONZERO_PATTERN) { 5369566063dSJacob Faibussowitsch PetscCall(MatDestroyMatrices(osm->n, &osm->pmat)); 537b1a0a8a3SJed Brown scall = MAT_INITIAL_MATRIX; 538b1a0a8a3SJed Brown } 539ea91fabdSFande Kong if (osm->permutationIS) { 5409566063dSJacob Faibussowitsch PetscCall(MatCreateSubMatrix(pc->pmat, osm->permutationIS, osm->permutationIS, scall, &osm->permutationP)); 5419566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)osm->permutationP)); 542ea91fabdSFande Kong osm->pcmat = pc->pmat; 543ea91fabdSFande Kong pc->pmat = osm->permutationP; 544b1a0a8a3SJed Brown } 545ea91fabdSFande Kong } 546ea91fabdSFande Kong 547b1a0a8a3SJed Brown /* 5482da392ccSBarry Smith Extract the submatrices. 549b1a0a8a3SJed Brown */ 55081a5c4aaSDmitry Karpeev if (size > 1) { 5519566063dSJacob Faibussowitsch PetscCall(MatCreateSubMatricesMPI(pc->pmat, osm->n, osm->ois, osm->ois, scall, &osm->pmat)); 5522fa5cd67SKarl Rupp } else { 5539566063dSJacob Faibussowitsch PetscCall(MatCreateSubMatrices(pc->pmat, osm->n, osm->ois, osm->ois, scall, &osm->pmat)); 55481a5c4aaSDmitry Karpeev } 555b1a0a8a3SJed Brown if (scall == MAT_INITIAL_MATRIX) { 5569566063dSJacob Faibussowitsch PetscCall(PetscObjectGetOptionsPrefix((PetscObject)pc->pmat, &pprefix)); 557f746d493SDmitry Karpeev for (i = 0; i < osm->n; i++) { 5589566063dSJacob Faibussowitsch PetscCall(PetscLogObjectParent((PetscObject)pc, (PetscObject)osm->pmat[i])); 5599566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptionsPrefix((PetscObject)osm->pmat[i], pprefix)); 560b1a0a8a3SJed Brown } 561b1a0a8a3SJed Brown } 562b1a0a8a3SJed Brown 563b1a0a8a3SJed Brown /* Return control to the user so that the submatrices can be modified (e.g., to apply 564b1a0a8a3SJed Brown different boundary conditions for the submatrices than for the global problem) */ 5659566063dSJacob Faibussowitsch PetscCall(PCModifySubMatrices(pc, osm->n, osm->ois, osm->ois, osm->pmat, pc->modifysubmatricesP)); 566b1a0a8a3SJed Brown 567b1a0a8a3SJed Brown /* 5686a4f0f73SDmitry Karpeev Loop over submatrices putting them into local ksps 569b1a0a8a3SJed Brown */ 570f746d493SDmitry Karpeev for (i = 0; i < osm->n; i++) { 5719566063dSJacob Faibussowitsch PetscCall(KSPSetOperators(osm->ksp[i], osm->pmat[i], osm->pmat[i])); 5729566063dSJacob Faibussowitsch PetscCall(KSPGetOptionsPrefix(osm->ksp[i], &prefix)); 5739566063dSJacob Faibussowitsch PetscCall(MatSetOptionsPrefix(osm->pmat[i], prefix)); 57448a46eb9SPierre Jolivet if (!pc->setupcalled) PetscCall(KSPSetFromOptions(osm->ksp[i])); 575b1a0a8a3SJed Brown } 576ea91fabdSFande Kong if (osm->pcmat) { 5779566063dSJacob Faibussowitsch PetscCall(MatDestroy(&pc->pmat)); 578ea91fabdSFande Kong pc->pmat = osm->pcmat; 5790a545947SLisandro Dalcin osm->pcmat = NULL; 580ea91fabdSFande Kong } 581b1a0a8a3SJed Brown PetscFunctionReturn(0); 582b1a0a8a3SJed Brown } 583b1a0a8a3SJed Brown 5849371c9d4SSatish Balay static PetscErrorCode PCSetUpOnBlocks_GASM(PC pc) { 585f746d493SDmitry Karpeev PC_GASM *osm = (PC_GASM *)pc->data; 586b1a0a8a3SJed Brown PetscInt i; 587b1a0a8a3SJed Brown 588b1a0a8a3SJed Brown PetscFunctionBegin; 58948a46eb9SPierre Jolivet for (i = 0; i < osm->n; i++) PetscCall(KSPSetUp(osm->ksp[i])); 590b1a0a8a3SJed Brown PetscFunctionReturn(0); 591b1a0a8a3SJed Brown } 592b1a0a8a3SJed Brown 5939371c9d4SSatish Balay static PetscErrorCode PCApply_GASM(PC pc, Vec xin, Vec yout) { 594f746d493SDmitry Karpeev PC_GASM *osm = (PC_GASM *)pc->data; 595f746d493SDmitry Karpeev PetscInt i; 596ea91fabdSFande Kong Vec x, y; 597b1a0a8a3SJed Brown ScatterMode forward = SCATTER_FORWARD, reverse = SCATTER_REVERSE; 598b1a0a8a3SJed Brown 599b1a0a8a3SJed Brown PetscFunctionBegin; 600ea91fabdSFande Kong if (osm->pctoouter) { 6019566063dSJacob Faibussowitsch PetscCall(VecScatterBegin(osm->pctoouter, xin, osm->pcx, INSERT_VALUES, SCATTER_REVERSE)); 6029566063dSJacob Faibussowitsch PetscCall(VecScatterEnd(osm->pctoouter, xin, osm->pcx, INSERT_VALUES, SCATTER_REVERSE)); 603ea91fabdSFande Kong x = osm->pcx; 604ea91fabdSFande Kong y = osm->pcy; 605ea91fabdSFande Kong } else { 606ea91fabdSFande Kong x = xin; 607ea91fabdSFande Kong y = yout; 608ea91fabdSFande Kong } 609b1a0a8a3SJed Brown /* 61048e38a8aSPierre Jolivet support for limiting the restriction or interpolation only to the inner 611b1a0a8a3SJed Brown subdomain values (leaving the other values 0). 612b1a0a8a3SJed Brown */ 613f746d493SDmitry Karpeev if (!(osm->type & PC_GASM_RESTRICT)) { 614b1a0a8a3SJed Brown /* have to zero the work RHS since scatter may leave some slots empty */ 6159566063dSJacob Faibussowitsch PetscCall(VecZeroEntries(osm->gx)); 6169566063dSJacob Faibussowitsch PetscCall(VecScatterBegin(osm->girestriction, x, osm->gx, INSERT_VALUES, forward)); 6172fa5cd67SKarl Rupp } else { 6189566063dSJacob Faibussowitsch PetscCall(VecScatterBegin(osm->gorestriction, x, osm->gx, INSERT_VALUES, forward)); 619b1a0a8a3SJed Brown } 6209566063dSJacob Faibussowitsch PetscCall(VecZeroEntries(osm->gy)); 6216a4f0f73SDmitry Karpeev if (!(osm->type & PC_GASM_RESTRICT)) { 6229566063dSJacob Faibussowitsch PetscCall(VecScatterEnd(osm->girestriction, x, osm->gx, INSERT_VALUES, forward)); 6232fa5cd67SKarl Rupp } else { 6249566063dSJacob Faibussowitsch PetscCall(VecScatterEnd(osm->gorestriction, x, osm->gx, INSERT_VALUES, forward)); 6256a4f0f73SDmitry Karpeev } 62686b96d36SDmitry Karpeev /* do the subdomain solves */ 62786b96d36SDmitry Karpeev for (i = 0; i < osm->n; ++i) { 6289566063dSJacob Faibussowitsch PetscCall(KSPSolve(osm->ksp[i], osm->x[i], osm->y[i])); 6299566063dSJacob Faibussowitsch PetscCall(KSPCheckSolve(osm->ksp[i], pc, osm->y[i])); 630b1a0a8a3SJed Brown } 63148e38a8aSPierre Jolivet /* do we need to zero y? */ 6329566063dSJacob Faibussowitsch PetscCall(VecZeroEntries(y)); 6336a4f0f73SDmitry Karpeev if (!(osm->type & PC_GASM_INTERPOLATE)) { 6349566063dSJacob Faibussowitsch PetscCall(VecScatterBegin(osm->girestriction, osm->gy, y, ADD_VALUES, reverse)); 6359566063dSJacob Faibussowitsch PetscCall(VecScatterEnd(osm->girestriction, osm->gy, y, ADD_VALUES, reverse)); 6362fa5cd67SKarl Rupp } else { 6379566063dSJacob Faibussowitsch PetscCall(VecScatterBegin(osm->gorestriction, osm->gy, y, ADD_VALUES, reverse)); 6389566063dSJacob Faibussowitsch PetscCall(VecScatterEnd(osm->gorestriction, osm->gy, y, ADD_VALUES, reverse)); 6396a4f0f73SDmitry Karpeev } 640ea91fabdSFande Kong if (osm->pctoouter) { 6419566063dSJacob Faibussowitsch PetscCall(VecScatterBegin(osm->pctoouter, y, yout, INSERT_VALUES, SCATTER_FORWARD)); 6429566063dSJacob Faibussowitsch PetscCall(VecScatterEnd(osm->pctoouter, y, yout, INSERT_VALUES, SCATTER_FORWARD)); 643ea91fabdSFande Kong } 644ea91fabdSFande Kong PetscFunctionReturn(0); 645b1a0a8a3SJed Brown } 646b1a0a8a3SJed Brown 6479371c9d4SSatish Balay static PetscErrorCode PCMatApply_GASM(PC pc, Mat Xin, Mat Yout) { 64848e38a8aSPierre Jolivet PC_GASM *osm = (PC_GASM *)pc->data; 64948e38a8aSPierre Jolivet Mat X, Y, O = NULL, Z, W; 65048e38a8aSPierre Jolivet Vec x, y; 65148e38a8aSPierre Jolivet PetscInt i, m, M, N; 65248e38a8aSPierre Jolivet ScatterMode forward = SCATTER_FORWARD, reverse = SCATTER_REVERSE; 65348e38a8aSPierre Jolivet 65448e38a8aSPierre Jolivet PetscFunctionBegin; 65508401ef6SPierre Jolivet PetscCheck(osm->n == 1, PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "Not yet implemented"); 6569566063dSJacob Faibussowitsch PetscCall(MatGetSize(Xin, NULL, &N)); 65748e38a8aSPierre Jolivet if (osm->pctoouter) { 6589566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(osm->pcx, &m)); 6599566063dSJacob Faibussowitsch PetscCall(VecGetSize(osm->pcx, &M)); 6609566063dSJacob Faibussowitsch PetscCall(MatCreateDense(PetscObjectComm((PetscObject)osm->ois[0]), m, PETSC_DECIDE, M, N, NULL, &O)); 66148e38a8aSPierre Jolivet for (i = 0; i < N; ++i) { 6629566063dSJacob Faibussowitsch PetscCall(MatDenseGetColumnVecRead(Xin, i, &x)); 6639566063dSJacob Faibussowitsch PetscCall(MatDenseGetColumnVecWrite(O, i, &y)); 6649566063dSJacob Faibussowitsch PetscCall(VecScatterBegin(osm->pctoouter, x, y, INSERT_VALUES, SCATTER_REVERSE)); 6659566063dSJacob Faibussowitsch PetscCall(VecScatterEnd(osm->pctoouter, x, y, INSERT_VALUES, SCATTER_REVERSE)); 6669566063dSJacob Faibussowitsch PetscCall(MatDenseRestoreColumnVecWrite(O, i, &y)); 6679566063dSJacob Faibussowitsch PetscCall(MatDenseRestoreColumnVecRead(Xin, i, &x)); 66848e38a8aSPierre Jolivet } 66948e38a8aSPierre Jolivet X = Y = O; 67048e38a8aSPierre Jolivet } else { 67148e38a8aSPierre Jolivet X = Xin; 67248e38a8aSPierre Jolivet Y = Yout; 67348e38a8aSPierre Jolivet } 67448e38a8aSPierre Jolivet /* 67548e38a8aSPierre Jolivet support for limiting the restriction or interpolation only to the inner 67648e38a8aSPierre Jolivet subdomain values (leaving the other values 0). 67748e38a8aSPierre Jolivet */ 6789566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(osm->x[0], &m)); 6799566063dSJacob Faibussowitsch PetscCall(VecGetSize(osm->x[0], &M)); 6809566063dSJacob Faibussowitsch PetscCall(MatCreateDense(PetscObjectComm((PetscObject)osm->ois[0]), m, PETSC_DECIDE, M, N, NULL, &Z)); 68148e38a8aSPierre Jolivet for (i = 0; i < N; ++i) { 6829566063dSJacob Faibussowitsch PetscCall(MatDenseGetColumnVecRead(X, i, &x)); 6839566063dSJacob Faibussowitsch PetscCall(MatDenseGetColumnVecWrite(Z, i, &y)); 68448e38a8aSPierre Jolivet if (!(osm->type & PC_GASM_RESTRICT)) { 68548e38a8aSPierre Jolivet /* have to zero the work RHS since scatter may leave some slots empty */ 6869566063dSJacob Faibussowitsch PetscCall(VecZeroEntries(y)); 6879566063dSJacob Faibussowitsch PetscCall(VecScatterBegin(osm->girestriction, x, y, INSERT_VALUES, forward)); 6889566063dSJacob Faibussowitsch PetscCall(VecScatterEnd(osm->girestriction, x, y, INSERT_VALUES, forward)); 68948e38a8aSPierre Jolivet } else { 6909566063dSJacob Faibussowitsch PetscCall(VecScatterBegin(osm->gorestriction, x, y, INSERT_VALUES, forward)); 6919566063dSJacob Faibussowitsch PetscCall(VecScatterEnd(osm->gorestriction, x, y, INSERT_VALUES, forward)); 69248e38a8aSPierre Jolivet } 6939566063dSJacob Faibussowitsch PetscCall(MatDenseRestoreColumnVecWrite(Z, i, &y)); 6949566063dSJacob Faibussowitsch PetscCall(MatDenseRestoreColumnVecRead(X, i, &x)); 69548e38a8aSPierre Jolivet } 6969566063dSJacob Faibussowitsch PetscCall(MatCreateDense(PetscObjectComm((PetscObject)osm->ois[0]), m, PETSC_DECIDE, M, N, NULL, &W)); 6979566063dSJacob Faibussowitsch PetscCall(MatSetOption(Z, MAT_NO_OFF_PROC_ENTRIES, PETSC_TRUE)); 6989566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(Z, MAT_FINAL_ASSEMBLY)); 6999566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(Z, MAT_FINAL_ASSEMBLY)); 70048e38a8aSPierre Jolivet /* do the subdomain solve */ 7019566063dSJacob Faibussowitsch PetscCall(KSPMatSolve(osm->ksp[0], Z, W)); 7029566063dSJacob Faibussowitsch PetscCall(KSPCheckSolve(osm->ksp[0], pc, NULL)); 7039566063dSJacob Faibussowitsch PetscCall(MatDestroy(&Z)); 70448e38a8aSPierre Jolivet /* do we need to zero y? */ 7059566063dSJacob Faibussowitsch PetscCall(MatZeroEntries(Y)); 70648e38a8aSPierre Jolivet for (i = 0; i < N; ++i) { 7079566063dSJacob Faibussowitsch PetscCall(MatDenseGetColumnVecWrite(Y, i, &y)); 7089566063dSJacob Faibussowitsch PetscCall(MatDenseGetColumnVecRead(W, i, &x)); 70948e38a8aSPierre Jolivet if (!(osm->type & PC_GASM_INTERPOLATE)) { 7109566063dSJacob Faibussowitsch PetscCall(VecScatterBegin(osm->girestriction, x, y, ADD_VALUES, reverse)); 7119566063dSJacob Faibussowitsch PetscCall(VecScatterEnd(osm->girestriction, x, y, ADD_VALUES, reverse)); 71248e38a8aSPierre Jolivet } else { 7139566063dSJacob Faibussowitsch PetscCall(VecScatterBegin(osm->gorestriction, x, y, ADD_VALUES, reverse)); 7149566063dSJacob Faibussowitsch PetscCall(VecScatterEnd(osm->gorestriction, x, y, ADD_VALUES, reverse)); 71548e38a8aSPierre Jolivet } 7169566063dSJacob Faibussowitsch PetscCall(MatDenseRestoreColumnVecRead(W, i, &x)); 71748e38a8aSPierre Jolivet if (osm->pctoouter) { 7189566063dSJacob Faibussowitsch PetscCall(MatDenseGetColumnVecWrite(Yout, i, &x)); 7199566063dSJacob Faibussowitsch PetscCall(VecScatterBegin(osm->pctoouter, y, x, INSERT_VALUES, SCATTER_FORWARD)); 7209566063dSJacob Faibussowitsch PetscCall(VecScatterEnd(osm->pctoouter, y, x, INSERT_VALUES, SCATTER_FORWARD)); 7219566063dSJacob Faibussowitsch PetscCall(MatDenseRestoreColumnVecRead(Yout, i, &x)); 72248e38a8aSPierre Jolivet } 7239566063dSJacob Faibussowitsch PetscCall(MatDenseRestoreColumnVecWrite(Y, i, &y)); 72448e38a8aSPierre Jolivet } 7259566063dSJacob Faibussowitsch PetscCall(MatDestroy(&W)); 7269566063dSJacob Faibussowitsch PetscCall(MatDestroy(&O)); 72748e38a8aSPierre Jolivet PetscFunctionReturn(0); 72848e38a8aSPierre Jolivet } 72948e38a8aSPierre Jolivet 7309371c9d4SSatish Balay static PetscErrorCode PCApplyTranspose_GASM(PC pc, Vec xin, Vec yout) { 731f746d493SDmitry Karpeev PC_GASM *osm = (PC_GASM *)pc->data; 732f746d493SDmitry Karpeev PetscInt i; 733ea91fabdSFande Kong Vec x, y; 734b1a0a8a3SJed Brown ScatterMode forward = SCATTER_FORWARD, reverse = SCATTER_REVERSE; 735b1a0a8a3SJed Brown 736b1a0a8a3SJed Brown PetscFunctionBegin; 737ea91fabdSFande Kong if (osm->pctoouter) { 7389566063dSJacob Faibussowitsch PetscCall(VecScatterBegin(osm->pctoouter, xin, osm->pcx, INSERT_VALUES, SCATTER_REVERSE)); 7399566063dSJacob Faibussowitsch PetscCall(VecScatterEnd(osm->pctoouter, xin, osm->pcx, INSERT_VALUES, SCATTER_REVERSE)); 740ea91fabdSFande Kong x = osm->pcx; 741ea91fabdSFande Kong y = osm->pcy; 742ea91fabdSFande Kong } else { 743ea91fabdSFande Kong x = xin; 744ea91fabdSFande Kong y = yout; 745ea91fabdSFande Kong } 746b1a0a8a3SJed Brown /* 747b1a0a8a3SJed Brown Support for limiting the restriction or interpolation to only local 748b1a0a8a3SJed Brown subdomain values (leaving the other values 0). 749b1a0a8a3SJed Brown 750f746d493SDmitry Karpeev Note: these are reversed from the PCApply_GASM() because we are applying the 751b1a0a8a3SJed Brown transpose of the three terms 752b1a0a8a3SJed Brown */ 753f746d493SDmitry Karpeev if (!(osm->type & PC_GASM_INTERPOLATE)) { 754b1a0a8a3SJed Brown /* have to zero the work RHS since scatter may leave some slots empty */ 7559566063dSJacob Faibussowitsch PetscCall(VecZeroEntries(osm->gx)); 7569566063dSJacob Faibussowitsch PetscCall(VecScatterBegin(osm->girestriction, x, osm->gx, INSERT_VALUES, forward)); 7572fa5cd67SKarl Rupp } else { 7589566063dSJacob Faibussowitsch PetscCall(VecScatterBegin(osm->gorestriction, x, osm->gx, INSERT_VALUES, forward)); 759b1a0a8a3SJed Brown } 7609566063dSJacob Faibussowitsch PetscCall(VecZeroEntries(osm->gy)); 7616a4f0f73SDmitry Karpeev if (!(osm->type & PC_GASM_INTERPOLATE)) { 7629566063dSJacob Faibussowitsch PetscCall(VecScatterEnd(osm->girestriction, x, osm->gx, INSERT_VALUES, forward)); 7632fa5cd67SKarl Rupp } else { 7649566063dSJacob Faibussowitsch PetscCall(VecScatterEnd(osm->gorestriction, x, osm->gx, INSERT_VALUES, forward)); 7656a4f0f73SDmitry Karpeev } 766b1a0a8a3SJed Brown /* do the local solves */ 767ab3e923fSDmitry 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. */ 7689566063dSJacob Faibussowitsch PetscCall(KSPSolveTranspose(osm->ksp[i], osm->x[i], osm->y[i])); 7699566063dSJacob Faibussowitsch PetscCall(KSPCheckSolve(osm->ksp[i], pc, osm->y[i])); 770b1a0a8a3SJed Brown } 7719566063dSJacob Faibussowitsch PetscCall(VecZeroEntries(y)); 7726a4f0f73SDmitry Karpeev if (!(osm->type & PC_GASM_RESTRICT)) { 7739566063dSJacob Faibussowitsch PetscCall(VecScatterBegin(osm->girestriction, osm->gy, y, ADD_VALUES, reverse)); 7749566063dSJacob Faibussowitsch PetscCall(VecScatterEnd(osm->girestriction, osm->gy, y, ADD_VALUES, reverse)); 7752fa5cd67SKarl Rupp } else { 7769566063dSJacob Faibussowitsch PetscCall(VecScatterBegin(osm->gorestriction, osm->gy, y, ADD_VALUES, reverse)); 7779566063dSJacob Faibussowitsch PetscCall(VecScatterEnd(osm->gorestriction, osm->gy, y, ADD_VALUES, reverse)); 7786a4f0f73SDmitry Karpeev } 779ea91fabdSFande Kong if (osm->pctoouter) { 7809566063dSJacob Faibussowitsch PetscCall(VecScatterBegin(osm->pctoouter, y, yout, INSERT_VALUES, SCATTER_FORWARD)); 7819566063dSJacob Faibussowitsch PetscCall(VecScatterEnd(osm->pctoouter, y, yout, INSERT_VALUES, SCATTER_FORWARD)); 782ea91fabdSFande Kong } 783b1a0a8a3SJed Brown PetscFunctionReturn(0); 784b1a0a8a3SJed Brown } 785b1a0a8a3SJed Brown 7869371c9d4SSatish Balay static PetscErrorCode PCReset_GASM(PC pc) { 787f746d493SDmitry Karpeev PC_GASM *osm = (PC_GASM *)pc->data; 788b1a0a8a3SJed Brown PetscInt i; 789b1a0a8a3SJed Brown 790b1a0a8a3SJed Brown PetscFunctionBegin; 791b1a0a8a3SJed Brown if (osm->ksp) { 79248a46eb9SPierre Jolivet for (i = 0; i < osm->n; i++) PetscCall(KSPReset(osm->ksp[i])); 793b1a0a8a3SJed Brown } 794b1a0a8a3SJed Brown if (osm->pmat) { 795f746d493SDmitry Karpeev if (osm->n > 0) { 796df750dc8SHong Zhang PetscMPIInt size; 7979566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size)); 798df750dc8SHong Zhang if (size > 1) { 7997dae84e0SHong Zhang /* osm->pmat is created by MatCreateSubMatricesMPI(), cannot use MatDestroySubMatrices() */ 8009566063dSJacob Faibussowitsch PetscCall(MatDestroyMatrices(osm->n, &osm->pmat)); 801df750dc8SHong Zhang } else { 8029566063dSJacob Faibussowitsch PetscCall(MatDestroySubMatrices(osm->n, &osm->pmat)); 803df750dc8SHong Zhang } 804b1a0a8a3SJed Brown } 805b1a0a8a3SJed Brown } 806d34fcf5fSBarry Smith if (osm->x) { 807f746d493SDmitry Karpeev for (i = 0; i < osm->n; i++) { 8089566063dSJacob Faibussowitsch PetscCall(VecDestroy(&osm->x[i])); 8099566063dSJacob Faibussowitsch PetscCall(VecDestroy(&osm->y[i])); 810b1a0a8a3SJed Brown } 811d34fcf5fSBarry Smith } 8129566063dSJacob Faibussowitsch PetscCall(VecDestroy(&osm->gx)); 8139566063dSJacob Faibussowitsch PetscCall(VecDestroy(&osm->gy)); 814ab3e923fSDmitry Karpeev 8159566063dSJacob Faibussowitsch PetscCall(VecScatterDestroy(&osm->gorestriction)); 8169566063dSJacob Faibussowitsch PetscCall(VecScatterDestroy(&osm->girestriction)); 8178f3b4b4dSDmitry Karpeev if (!osm->user_subdomains) { 8189566063dSJacob Faibussowitsch PetscCall(PCGASMDestroySubdomains(osm->n, &osm->ois, &osm->iis)); 8198f3b4b4dSDmitry Karpeev osm->N = PETSC_DETERMINE; 8208f3b4b4dSDmitry Karpeev osm->nmax = PETSC_DETERMINE; 8218f3b4b4dSDmitry Karpeev } 82248a46eb9SPierre Jolivet if (osm->pctoouter) PetscCall(VecScatterDestroy(&(osm->pctoouter))); 82348a46eb9SPierre Jolivet if (osm->permutationIS) PetscCall(ISDestroy(&(osm->permutationIS))); 82448a46eb9SPierre Jolivet if (osm->pcx) PetscCall(VecDestroy(&(osm->pcx))); 82548a46eb9SPierre Jolivet if (osm->pcy) PetscCall(VecDestroy(&(osm->pcy))); 82648a46eb9SPierre Jolivet if (osm->permutationP) PetscCall(MatDestroy(&(osm->permutationP))); 82748a46eb9SPierre Jolivet if (osm->pcmat) PetscCall(MatDestroy(&osm->pcmat)); 828a06653b4SBarry Smith PetscFunctionReturn(0); 829a06653b4SBarry Smith } 830a06653b4SBarry Smith 8319371c9d4SSatish Balay static PetscErrorCode PCDestroy_GASM(PC pc) { 832a06653b4SBarry Smith PC_GASM *osm = (PC_GASM *)pc->data; 833a06653b4SBarry Smith PetscInt i; 834a06653b4SBarry Smith 835a06653b4SBarry Smith PetscFunctionBegin; 8369566063dSJacob Faibussowitsch PetscCall(PCReset_GASM(pc)); 837135757f6SDmitry Karpeev /* PCReset will not destroy subdomains, if user_subdomains is true. */ 8389566063dSJacob Faibussowitsch PetscCall(PCGASMDestroySubdomains(osm->n, &osm->ois, &osm->iis)); 839a06653b4SBarry Smith if (osm->ksp) { 84048a46eb9SPierre Jolivet for (i = 0; i < osm->n; i++) PetscCall(KSPDestroy(&osm->ksp[i])); 8419566063dSJacob Faibussowitsch PetscCall(PetscFree(osm->ksp)); 842a06653b4SBarry Smith } 8439566063dSJacob Faibussowitsch PetscCall(PetscFree(osm->x)); 8449566063dSJacob Faibussowitsch PetscCall(PetscFree(osm->y)); 8452e956fe4SStefano Zampini PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetSubdomains_C", NULL)); 8462e956fe4SStefano Zampini PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetOverlap_C", NULL)); 8472e956fe4SStefano Zampini PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetType_C", NULL)); 8482e956fe4SStefano Zampini PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetSortIndices_C", NULL)); 8492e956fe4SStefano Zampini PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMGetSubKSP_C", NULL)); 8509566063dSJacob Faibussowitsch PetscCall(PetscFree(pc->data)); 851b1a0a8a3SJed Brown PetscFunctionReturn(0); 852b1a0a8a3SJed Brown } 853b1a0a8a3SJed Brown 8549371c9d4SSatish Balay static PetscErrorCode PCSetFromOptions_GASM(PC pc, PetscOptionItems *PetscOptionsObject) { 855f746d493SDmitry Karpeev PC_GASM *osm = (PC_GASM *)pc->data; 856b1a0a8a3SJed Brown PetscInt blocks, ovl; 85757501b6eSBarry Smith PetscBool flg; 858f746d493SDmitry Karpeev PCGASMType gasmtype; 859b1a0a8a3SJed Brown 860b1a0a8a3SJed Brown PetscFunctionBegin; 861d0609cedSBarry Smith PetscOptionsHeadBegin(PetscOptionsObject, "Generalized additive Schwarz options"); 8629566063dSJacob 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)); 8639566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-pc_gasm_total_subdomains", "Total number of subdomains across communicator", "PCGASMSetTotalSubdomains", osm->N, &blocks, &flg)); 8641baa6e33SBarry Smith if (flg) PetscCall(PCGASMSetTotalSubdomains(pc, blocks)); 8659566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-pc_gasm_overlap", "Number of overlapping degrees of freedom", "PCGASMSetOverlap", osm->overlap, &ovl, &flg)); 86665db9045SDmitry Karpeev if (flg) { 8679566063dSJacob Faibussowitsch PetscCall(PCGASMSetOverlap(pc, ovl)); 868d709ea83SDmitry Karpeev osm->dm_subdomains = PETSC_FALSE; 86965db9045SDmitry Karpeev } 870b1a0a8a3SJed Brown flg = PETSC_FALSE; 8719566063dSJacob Faibussowitsch PetscCall(PetscOptionsEnum("-pc_gasm_type", "Type of restriction/extension", "PCGASMSetType", PCGASMTypes, (PetscEnum)osm->type, (PetscEnum *)&gasmtype, &flg)); 8729566063dSJacob Faibussowitsch if (flg) PetscCall(PCGASMSetType(pc, gasmtype)); 8739566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-pc_gasm_use_hierachical_partitioning", "use hierarchical partitioning", NULL, osm->hierarchicalpartitioning, &osm->hierarchicalpartitioning, &flg)); 874d0609cedSBarry Smith PetscOptionsHeadEnd(); 875b1a0a8a3SJed Brown PetscFunctionReturn(0); 876b1a0a8a3SJed Brown } 877b1a0a8a3SJed Brown 878b1a0a8a3SJed Brown /*------------------------------------------------------------------------------------*/ 879b1a0a8a3SJed Brown 8808f3b4b4dSDmitry Karpeev /*@ 881*f1580f4eSBarry Smith PCGASMSetTotalSubdomains - sets the total number of subdomains to use across the communicator for `PCGASM` 8828f3b4b4dSDmitry Karpeev Logically collective on pc 8838f3b4b4dSDmitry Karpeev 8848f3b4b4dSDmitry Karpeev Input Parameters: 8858f3b4b4dSDmitry Karpeev + pc - the preconditioner 8868f3b4b4dSDmitry Karpeev - N - total number of subdomains 8878f3b4b4dSDmitry Karpeev 8888f3b4b4dSDmitry Karpeev Level: beginner 8898f3b4b4dSDmitry Karpeev 890*f1580f4eSBarry Smith .seealso: `PCGASM`, `PCGASMSetSubdomains()`, `PCGASMSetOverlap()` 891db781477SPatrick Sanan `PCGASMCreateSubdomains2D()` 8928f3b4b4dSDmitry Karpeev @*/ 8939371c9d4SSatish Balay PetscErrorCode PCGASMSetTotalSubdomains(PC pc, PetscInt N) { 8948f3b4b4dSDmitry Karpeev PC_GASM *osm = (PC_GASM *)pc->data; 8958f3b4b4dSDmitry Karpeev PetscMPIInt size, rank; 8968f3b4b4dSDmitry Karpeev 8978f3b4b4dSDmitry Karpeev PetscFunctionBegin; 89863a3b9bcSJacob 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); 89928b400f6SJacob Faibussowitsch PetscCheck(!pc->setupcalled, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "PCGASMSetTotalSubdomains() should be called before calling PCSetUp()."); 9008f3b4b4dSDmitry Karpeev 9019566063dSJacob Faibussowitsch PetscCall(PCGASMDestroySubdomains(osm->n, &osm->iis, &osm->ois)); 9028f3b4b4dSDmitry Karpeev osm->ois = osm->iis = NULL; 9038f3b4b4dSDmitry Karpeev 9049566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size)); 9059566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)pc), &rank)); 9068f3b4b4dSDmitry Karpeev osm->N = N; 9078f3b4b4dSDmitry Karpeev osm->n = PETSC_DETERMINE; 9088f3b4b4dSDmitry Karpeev osm->nmax = PETSC_DETERMINE; 9098f3b4b4dSDmitry Karpeev osm->dm_subdomains = PETSC_FALSE; 9108f3b4b4dSDmitry Karpeev PetscFunctionReturn(0); 9118f3b4b4dSDmitry Karpeev } 9128f3b4b4dSDmitry Karpeev 9139371c9d4SSatish Balay static PetscErrorCode PCGASMSetSubdomains_GASM(PC pc, PetscInt n, IS iis[], IS ois[]) { 914f746d493SDmitry Karpeev PC_GASM *osm = (PC_GASM *)pc->data; 915b1a0a8a3SJed Brown PetscInt i; 916b1a0a8a3SJed Brown 917b1a0a8a3SJed Brown PetscFunctionBegin; 918*f1580f4eSBarry 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); 91928b400f6SJacob Faibussowitsch PetscCheck(!pc->setupcalled, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "PCGASMSetSubdomains() should be called before calling PCSetUp()."); 920b1a0a8a3SJed Brown 9219566063dSJacob Faibussowitsch PetscCall(PCGASMDestroySubdomains(osm->n, &osm->iis, &osm->ois)); 9228f3b4b4dSDmitry Karpeev osm->iis = osm->ois = NULL; 9238f3b4b4dSDmitry Karpeev osm->n = n; 9248f3b4b4dSDmitry Karpeev osm->N = PETSC_DETERMINE; 9258f3b4b4dSDmitry Karpeev osm->nmax = PETSC_DETERMINE; 926a35b7b57SDmitry Karpeev if (ois) { 9279566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(n, &osm->ois)); 9288f3b4b4dSDmitry Karpeev for (i = 0; i < n; i++) { 9299566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)ois[i])); 9308f3b4b4dSDmitry Karpeev osm->ois[i] = ois[i]; 9318f3b4b4dSDmitry Karpeev } 9328f3b4b4dSDmitry Karpeev /* 9338f3b4b4dSDmitry Karpeev Since the user set the outer subdomains, even if nontrivial overlap was requested via PCGASMSetOverlap(), 9348f3b4b4dSDmitry Karpeev it will be ignored. To avoid confusion later on (e.g., when viewing the PC), the overlap size is set to -1. 9358f3b4b4dSDmitry Karpeev */ 936b1a0a8a3SJed Brown osm->overlap = -1; 937670417b2SFande Kong /* inner subdomains must be provided */ 93828b400f6SJacob Faibussowitsch PetscCheck(iis, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "inner indices have to be provided "); 939670417b2SFande Kong } /* end if */ 940a35b7b57SDmitry Karpeev if (iis) { 9419566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(n, &osm->iis)); 9428f3b4b4dSDmitry Karpeev for (i = 0; i < n; i++) { 9439566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)iis[i])); 9448f3b4b4dSDmitry Karpeev osm->iis[i] = iis[i]; 9458f3b4b4dSDmitry Karpeev } 946a35b7b57SDmitry Karpeev if (!ois) { 947390e1bf2SBarry Smith osm->ois = NULL; 948670417b2SFande Kong /* if user does not provide outer indices, we will create the corresponding outer indices using osm->overlap =1 in PCSetUp_GASM */ 949670417b2SFande Kong } 950670417b2SFande Kong } 95176bd3646SJed Brown if (PetscDefined(USE_DEBUG)) { 952670417b2SFande Kong PetscInt j, rstart, rend, *covered, lsize; 953670417b2SFande Kong const PetscInt *indices; 954670417b2SFande Kong /* check if the inner indices cover and only cover the local portion of the preconditioning matrix */ 9559566063dSJacob Faibussowitsch PetscCall(MatGetOwnershipRange(pc->pmat, &rstart, &rend)); 9569566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(rend - rstart, &covered)); 957*f1580f4eSBarry Smith /* check if the current MPI rank owns indices from others */ 958a35b7b57SDmitry Karpeev for (i = 0; i < n; i++) { 9599566063dSJacob Faibussowitsch PetscCall(ISGetIndices(osm->iis[i], &indices)); 9609566063dSJacob Faibussowitsch PetscCall(ISGetLocalSize(osm->iis[i], &lsize)); 961670417b2SFande Kong for (j = 0; j < lsize; j++) { 962*f1580f4eSBarry 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]); 9632472a847SBarry 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]); 96463a3b9bcSJacob Faibussowitsch covered[indices[j] - rstart] = 1; 965a35b7b57SDmitry Karpeev } 9669566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(osm->iis[i], &indices)); 9678f3b4b4dSDmitry Karpeev } 968670417b2SFande Kong /* check if we miss any indices */ 969ad540459SPierre 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); 9709566063dSJacob Faibussowitsch PetscCall(PetscFree(covered)); 971a35b7b57SDmitry Karpeev } 9728f3b4b4dSDmitry Karpeev if (iis) osm->user_subdomains = PETSC_TRUE; 973b1a0a8a3SJed Brown PetscFunctionReturn(0); 974b1a0a8a3SJed Brown } 975b1a0a8a3SJed Brown 9769371c9d4SSatish Balay static PetscErrorCode PCGASMSetOverlap_GASM(PC pc, PetscInt ovl) { 977f746d493SDmitry Karpeev PC_GASM *osm = (PC_GASM *)pc->data; 978b1a0a8a3SJed Brown 979b1a0a8a3SJed Brown PetscFunctionBegin; 98008401ef6SPierre Jolivet PetscCheck(ovl >= 0, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "Negative overlap value requested"); 9812472a847SBarry Smith PetscCheck(!pc->setupcalled || ovl == osm->overlap, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "PCGASMSetOverlap() should be called before PCSetUp()."); 9822fa5cd67SKarl Rupp if (!pc->setupcalled) osm->overlap = ovl; 983b1a0a8a3SJed Brown PetscFunctionReturn(0); 984b1a0a8a3SJed Brown } 985b1a0a8a3SJed Brown 9869371c9d4SSatish Balay static PetscErrorCode PCGASMSetType_GASM(PC pc, PCGASMType type) { 987f746d493SDmitry Karpeev PC_GASM *osm = (PC_GASM *)pc->data; 988b1a0a8a3SJed Brown 989b1a0a8a3SJed Brown PetscFunctionBegin; 990b1a0a8a3SJed Brown osm->type = type; 991b1a0a8a3SJed Brown osm->type_set = PETSC_TRUE; 992b1a0a8a3SJed Brown PetscFunctionReturn(0); 993b1a0a8a3SJed Brown } 994b1a0a8a3SJed Brown 9959371c9d4SSatish Balay static PetscErrorCode PCGASMSetSortIndices_GASM(PC pc, PetscBool doSort) { 996f746d493SDmitry Karpeev PC_GASM *osm = (PC_GASM *)pc->data; 997b1a0a8a3SJed Brown 998b1a0a8a3SJed Brown PetscFunctionBegin; 999b1a0a8a3SJed Brown osm->sort_indices = doSort; 1000b1a0a8a3SJed Brown PetscFunctionReturn(0); 1001b1a0a8a3SJed Brown } 1002b1a0a8a3SJed Brown 100344fe18e5SDmitry Karpeev /* 10048f3b4b4dSDmitry Karpeev FIXME: This routine might need to be modified now that multiple ranks per subdomain are allowed. 100544fe18e5SDmitry Karpeev In particular, it would upset the global subdomain number calculation. 100644fe18e5SDmitry Karpeev */ 10079371c9d4SSatish Balay static PetscErrorCode PCGASMGetSubKSP_GASM(PC pc, PetscInt *n, PetscInt *first, KSP **ksp) { 1008f746d493SDmitry Karpeev PC_GASM *osm = (PC_GASM *)pc->data; 1009b1a0a8a3SJed Brown 1010b1a0a8a3SJed Brown PetscFunctionBegin; 101108401ef6SPierre 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"); 1012b1a0a8a3SJed Brown 10132fa5cd67SKarl Rupp if (n) *n = osm->n; 1014ab3e923fSDmitry Karpeev if (first) { 10159566063dSJacob Faibussowitsch PetscCallMPI(MPI_Scan(&osm->n, first, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject)pc))); 1016ab3e923fSDmitry Karpeev *first -= osm->n; 1017b1a0a8a3SJed Brown } 1018b1a0a8a3SJed Brown if (ksp) { 1019b1a0a8a3SJed Brown /* Assume that local solves are now different; not necessarily 102006b43e7eSDmitry Karpeev true, though! This flag is used only for PCView_GASM() */ 1021b1a0a8a3SJed Brown *ksp = osm->ksp; 10226a4f0f73SDmitry Karpeev osm->same_subdomain_solvers = PETSC_FALSE; 1023b1a0a8a3SJed Brown } 1024b1a0a8a3SJed Brown PetscFunctionReturn(0); 1025ab3e923fSDmitry Karpeev } /* PCGASMGetSubKSP_GASM() */ 1026b1a0a8a3SJed Brown 1027b1a0a8a3SJed Brown /*@C 1028*f1580f4eSBarry Smith PCGASMSetSubdomains - Sets the subdomains for this MPI rank 1029*f1580f4eSBarry Smith for the additive Schwarz preconditioner with multiple MPI ranks per subdomain, `PCGASM` 1030b1a0a8a3SJed Brown 10318f3b4b4dSDmitry Karpeev Collective on pc 1032b1a0a8a3SJed Brown 1033b1a0a8a3SJed Brown Input Parameters: 10348f3b4b4dSDmitry Karpeev + pc - the preconditioner object 1035*f1580f4eSBarry Smith . n - the number of subdomains for this MPI rank 10368f3b4b4dSDmitry Karpeev . iis - the index sets that define the inner subdomains (or NULL for PETSc to determine subdomains) 10378f3b4b4dSDmitry 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) 1038b1a0a8a3SJed Brown 1039b1a0a8a3SJed Brown Notes: 1040*f1580f4eSBarry Smith The `IS` indices use the parallel, global numbering of the vector entries. 10416a4f0f73SDmitry Karpeev Inner subdomains are those where the correction is applied. 10426a4f0f73SDmitry Karpeev Outer subdomains are those where the residual necessary to obtain the 1043*f1580f4eSBarry Smith corrections is obtained (see `PCGASMType` for the use of inner/outer subdomains). 1044*f1580f4eSBarry Smith Both inner and outer subdomains can extend over several MPI ranks. 1045*f1580f4eSBarry Smith This rank's portion of a subdomain is known as a local subdomain. 10466a4f0f73SDmitry Karpeev 1047*f1580f4eSBarry Smith Inner subdomains can not overlap with each other, do not have any entities from remote ranks, 1048*f1580f4eSBarry Smith and have to cover the entire local subdomain owned by the current rank. The index sets on each 1049*f1580f4eSBarry Smith rank should be ordered such that the ith local subdomain is connected to the ith remote subdomain 1050*f1580f4eSBarry Smith on another MPI rank. 1051670417b2SFande Kong 1052*f1580f4eSBarry Smith By default the `PGASM` preconditioner uses 1 (local) subdomain per MPI rank. 10536a4f0f73SDmitry Karpeev 1054b1a0a8a3SJed Brown Level: advanced 1055b1a0a8a3SJed Brown 1056*f1580f4eSBarry Smith .seealso: `PCGASM`, `PCGASMSetOverlap()`, `PCGASMGetSubKSP()`, 1057db781477SPatrick Sanan `PCGASMCreateSubdomains2D()`, `PCGASMGetSubdomains()` 1058b1a0a8a3SJed Brown @*/ 10599371c9d4SSatish Balay PetscErrorCode PCGASMSetSubdomains(PC pc, PetscInt n, IS iis[], IS ois[]) { 10608f3b4b4dSDmitry Karpeev PC_GASM *osm = (PC_GASM *)pc->data; 1061b1a0a8a3SJed Brown 1062b1a0a8a3SJed Brown PetscFunctionBegin; 1063b1a0a8a3SJed Brown PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 1064cac4c232SBarry Smith PetscTryMethod(pc, "PCGASMSetSubdomains_C", (PC, PetscInt, IS[], IS[]), (pc, n, iis, ois)); 10658f3b4b4dSDmitry Karpeev osm->dm_subdomains = PETSC_FALSE; 1066b1a0a8a3SJed Brown PetscFunctionReturn(0); 1067b1a0a8a3SJed Brown } 1068b1a0a8a3SJed Brown 1069b1a0a8a3SJed Brown /*@ 1070f746d493SDmitry Karpeev PCGASMSetOverlap - Sets the overlap between a pair of subdomains for the 1071*f1580f4eSBarry Smith additive Schwarz preconditioner `PCGASM`. Either all or no MPI ranks in the 10728f3b4b4dSDmitry Karpeev pc communicator must call this routine. 1073b1a0a8a3SJed Brown 10748f3b4b4dSDmitry Karpeev Logically Collective on pc 1075b1a0a8a3SJed Brown 1076b1a0a8a3SJed Brown Input Parameters: 1077b1a0a8a3SJed Brown + pc - the preconditioner context 10788f3b4b4dSDmitry Karpeev - ovl - the amount of overlap between subdomains (ovl >= 0, default value = 0) 1079b1a0a8a3SJed Brown 1080b1a0a8a3SJed Brown Options Database Key: 108106b43e7eSDmitry Karpeev . -pc_gasm_overlap <overlap> - Sets overlap 1082b1a0a8a3SJed Brown 1083b1a0a8a3SJed Brown Notes: 1084*f1580f4eSBarry Smith By default the `PCGASM` preconditioner uses 1 subdomain per rank. To use 10858f3b4b4dSDmitry Karpeev multiple subdomain per perocessor or "straddling" subdomains that intersect 1086*f1580f4eSBarry Smith multiple ranks use `PCGASMSetSubdomains()` (or option -pc_gasm_total_subdomains <n>). 1087b1a0a8a3SJed Brown 10888f3b4b4dSDmitry Karpeev The overlap defaults to 0, so if one desires that no additional 1089b1a0a8a3SJed Brown overlap be computed beyond what may have been set with a call to 1090*f1580f4eSBarry Smith `PCGASMSetSubdomains()`, then ovl must be set to be 0. In particular, if one does 10918f3b4b4dSDmitry Karpeev not explicitly set the subdomains in application code, then all overlap would be computed 1092*f1580f4eSBarry Smith internally by PETSc, and using an overlap of 0 would result in an `PCGASM` 1093b1a0a8a3SJed Brown variant that is equivalent to the block Jacobi preconditioner. 1094b1a0a8a3SJed Brown 1095*f1580f4eSBarry Smith One can define initial index sets with any overlap via 1096*f1580f4eSBarry Smith `PCGASMSetSubdomains()`; the routine `PCGASMSetOverlap()` merely allows 109706b43e7eSDmitry Karpeev PETSc to extend that overlap further, if desired. 1098b1a0a8a3SJed Brown 1099b1a0a8a3SJed Brown Level: intermediate 1100b1a0a8a3SJed Brown 1101*f1580f4eSBarry Smith .seealso: `PCGASM`, `PCGASMSetSubdomains()`, `PCGASMGetSubKSP()`, 1102db781477SPatrick Sanan `PCGASMCreateSubdomains2D()`, `PCGASMGetSubdomains()` 1103b1a0a8a3SJed Brown @*/ 11049371c9d4SSatish Balay PetscErrorCode PCGASMSetOverlap(PC pc, PetscInt ovl) { 11058f3b4b4dSDmitry Karpeev PC_GASM *osm = (PC_GASM *)pc->data; 1106b1a0a8a3SJed Brown 1107b1a0a8a3SJed Brown PetscFunctionBegin; 1108b1a0a8a3SJed Brown PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 1109b1a0a8a3SJed Brown PetscValidLogicalCollectiveInt(pc, ovl, 2); 1110cac4c232SBarry Smith PetscTryMethod(pc, "PCGASMSetOverlap_C", (PC, PetscInt), (pc, ovl)); 11118f3b4b4dSDmitry Karpeev osm->dm_subdomains = PETSC_FALSE; 1112b1a0a8a3SJed Brown PetscFunctionReturn(0); 1113b1a0a8a3SJed Brown } 1114b1a0a8a3SJed Brown 1115b1a0a8a3SJed Brown /*@ 1116f746d493SDmitry Karpeev PCGASMSetType - Sets the type of restriction and interpolation used 1117*f1580f4eSBarry Smith for local problems in the `PCGASM` additive Schwarz method. 1118b1a0a8a3SJed Brown 1119*f1580f4eSBarry Smith Logically Collective on pc 1120b1a0a8a3SJed Brown 1121b1a0a8a3SJed Brown Input Parameters: 1122b1a0a8a3SJed Brown + pc - the preconditioner context 1123*f1580f4eSBarry Smith - type - variant of `PCGASM`, one of 1124b1a0a8a3SJed Brown .vb 1125*f1580f4eSBarry Smith `PC_GASM_BASIC` - full interpolation and restriction 1126*f1580f4eSBarry Smith `PC_GASM_RESTRICT` - full restriction, local MPI rank interpolation 1127*f1580f4eSBarry Smith `PC_GASM_INTERPOLATE` - full interpolation, local MPI rank restriction 1128*f1580f4eSBarry Smith `PC_GASM_NONE` - local MPI rank restriction and interpolation 1129b1a0a8a3SJed Brown .ve 1130b1a0a8a3SJed Brown 1131b1a0a8a3SJed Brown Options Database Key: 1132f746d493SDmitry Karpeev . -pc_gasm_type [basic,restrict,interpolate,none] - Sets GASM type 1133b1a0a8a3SJed Brown 1134b1a0a8a3SJed Brown Level: intermediate 1135b1a0a8a3SJed Brown 1136*f1580f4eSBarry Smith .seealso: `PCGASM`, `PCGASMSetSubdomains()`, `PCGASMGetSubKSP()`, 1137*f1580f4eSBarry Smith `PCGASMCreateSubdomains2D()`, `PCASM`, `PCASMSetType()` 1138b1a0a8a3SJed Brown @*/ 11399371c9d4SSatish Balay PetscErrorCode PCGASMSetType(PC pc, PCGASMType type) { 1140b1a0a8a3SJed Brown PetscFunctionBegin; 1141b1a0a8a3SJed Brown PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 1142b1a0a8a3SJed Brown PetscValidLogicalCollectiveEnum(pc, type, 2); 1143cac4c232SBarry Smith PetscTryMethod(pc, "PCGASMSetType_C", (PC, PCGASMType), (pc, type)); 1144b1a0a8a3SJed Brown PetscFunctionReturn(0); 1145b1a0a8a3SJed Brown } 1146b1a0a8a3SJed Brown 1147b1a0a8a3SJed Brown /*@ 1148f746d493SDmitry Karpeev PCGASMSetSortIndices - Determines whether subdomain indices are sorted. 1149b1a0a8a3SJed Brown 1150*f1580f4eSBarry Smith Logically Collective on pc 1151b1a0a8a3SJed Brown 1152b1a0a8a3SJed Brown Input Parameters: 1153b1a0a8a3SJed Brown + pc - the preconditioner context 1154b1a0a8a3SJed Brown - doSort - sort the subdomain indices 1155b1a0a8a3SJed Brown 1156b1a0a8a3SJed Brown Level: intermediate 1157b1a0a8a3SJed Brown 1158*f1580f4eSBarry Smith .seealso: `PCGASM`, `PCGASMSetSubdomains()`, `PCGASMGetSubKSP()`, 1159db781477SPatrick Sanan `PCGASMCreateSubdomains2D()` 1160b1a0a8a3SJed Brown @*/ 11619371c9d4SSatish Balay PetscErrorCode PCGASMSetSortIndices(PC pc, PetscBool doSort) { 1162b1a0a8a3SJed Brown PetscFunctionBegin; 1163b1a0a8a3SJed Brown PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 1164b1a0a8a3SJed Brown PetscValidLogicalCollectiveBool(pc, doSort, 2); 1165cac4c232SBarry Smith PetscTryMethod(pc, "PCGASMSetSortIndices_C", (PC, PetscBool), (pc, doSort)); 1166b1a0a8a3SJed Brown PetscFunctionReturn(0); 1167b1a0a8a3SJed Brown } 1168b1a0a8a3SJed Brown 1169b1a0a8a3SJed Brown /*@C 1170*f1580f4eSBarry Smith PCGASMGetSubKSP - Gets the local `KSP` contexts for all subdomains on 1171*f1580f4eSBarry Smith this MPI rank. 1172b1a0a8a3SJed Brown 1173*f1580f4eSBarry Smith Collective on pc iff first_local is requested 1174b1a0a8a3SJed Brown 1175b1a0a8a3SJed Brown Input Parameter: 1176b1a0a8a3SJed Brown . pc - the preconditioner context 1177b1a0a8a3SJed Brown 1178b1a0a8a3SJed Brown Output Parameters: 1179*f1580f4eSBarry Smith + n_local - the number of blocks on this MPI rank or NULL 1180*f1580f4eSBarry Smith . first_local - the global number of the first block on this rank or NULL, 1181*f1580f4eSBarry Smith all ranks must request or all must pass NULL 1182*f1580f4eSBarry Smith - ksp - the array of `KSP` contexts 1183b1a0a8a3SJed Brown 1184b1a0a8a3SJed Brown Note: 1185*f1580f4eSBarry Smith After `PCGASMGetSubKSP()` the array of `KSP`es is not to be freed 1186b1a0a8a3SJed Brown 1187*f1580f4eSBarry Smith Currently for some matrix implementations only 1 block per MPI rank 1188b1a0a8a3SJed Brown is supported. 1189b1a0a8a3SJed Brown 1190*f1580f4eSBarry Smith You must call `KSPSetUp()` before calling `PCGASMGetSubKSP()`. 1191b1a0a8a3SJed Brown 1192b1a0a8a3SJed Brown Level: advanced 1193b1a0a8a3SJed Brown 1194*f1580f4eSBarry Smith .seealso: `PCGASM`, `PCGASMSetSubdomains()`, `PCGASMSetOverlap()`, 1195db781477SPatrick Sanan `PCGASMCreateSubdomains2D()`, 1196b1a0a8a3SJed Brown @*/ 11979371c9d4SSatish Balay PetscErrorCode PCGASMGetSubKSP(PC pc, PetscInt *n_local, PetscInt *first_local, KSP *ksp[]) { 1198b1a0a8a3SJed Brown PetscFunctionBegin; 1199b1a0a8a3SJed Brown PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 1200cac4c232SBarry Smith PetscUseMethod(pc, "PCGASMGetSubKSP_C", (PC, PetscInt *, PetscInt *, KSP **), (pc, n_local, first_local, ksp)); 1201b1a0a8a3SJed Brown PetscFunctionReturn(0); 1202b1a0a8a3SJed Brown } 1203b1a0a8a3SJed Brown 1204b1a0a8a3SJed Brown /*MC 1205f746d493SDmitry Karpeev PCGASM - Use the (restricted) additive Schwarz method, each block is (approximately) solved with 1206*f1580f4eSBarry Smith its own `KSP` object on a subset of MPI ranks 1207b1a0a8a3SJed Brown 1208b1a0a8a3SJed Brown Options Database Keys: 1209*f1580f4eSBarry Smith + -pc_gasm_total_subdomains <n> - Sets total number of local subdomains to be distributed among MPI ranks 1210*f1580f4eSBarry Smith . -pc_gasm_view_subdomains - activates the printing of subdomain indices in `PCView()`, -ksp_view or -snes_view 1211*f1580f4eSBarry Smith . -pc_gasm_print_subdomains - activates the printing of subdomain indices in `PCSetUp()` 121206b43e7eSDmitry Karpeev . -pc_gasm_overlap <ovl> - Sets overlap by which to (automatically) extend local subdomains 1213*f1580f4eSBarry Smith - -pc_gasm_type [basic,restrict,interpolate,none] - Sets `PCGASMType` 1214b1a0a8a3SJed Brown 121595452b02SPatrick Sanan Notes: 1216*f1580f4eSBarry Smith To set options on the solvers for each block append -sub_ to all the `KSP`, and `PC` 1217b1a0a8a3SJed Brown options database keys. For example, -sub_pc_type ilu -sub_pc_factor_levels 1 -sub_ksp_type preonly 1218b1a0a8a3SJed Brown 1219*f1580f4eSBarry Smith To set the options on the solvers separate for each block call `PCGASMGetSubKSP()` 1220*f1580f4eSBarry Smith and set the options directly on the resulting `KSP` object (you can access its `PC` 1221*f1580f4eSBarry Smith with `KSPGetPC())` 1222b1a0a8a3SJed Brown 1223b1a0a8a3SJed Brown Level: beginner 1224b1a0a8a3SJed Brown 1225b1a0a8a3SJed Brown References: 1226606c0280SSatish Balay + * - M Dryja, OB Widlund, An additive variant of the Schwarz alternating method for the case of many subregions 122796a0c994SBarry Smith Courant Institute, New York University Technical report 1228606c0280SSatish Balay - * - Barry Smith, Petter Bjorstad, and William Gropp, Domain Decompositions: Parallel Multilevel Methods for Elliptic Partial Differential Equations, 122996a0c994SBarry Smith Cambridge University Press. 1230b1a0a8a3SJed Brown 1231*f1580f4eSBarry Smith .seealso: `PCCreate()`, `PCSetType()`, `PCType`, `PC`, `PCASM`, `PCGASMType`, `PCGASMSetType()`, 1232db781477SPatrick Sanan `PCBJACOBI`, `PCGASMGetSubKSP()`, `PCGASMSetSubdomains()`, 1233db781477SPatrick Sanan `PCSetModifySubMatrices()`, `PCGASMSetOverlap()`, `PCGASMSetType()` 1234b1a0a8a3SJed Brown M*/ 1235b1a0a8a3SJed Brown 12369371c9d4SSatish Balay PETSC_EXTERN PetscErrorCode PCCreate_GASM(PC pc) { 1237f746d493SDmitry Karpeev PC_GASM *osm; 1238b1a0a8a3SJed Brown 1239b1a0a8a3SJed Brown PetscFunctionBegin; 12409566063dSJacob Faibussowitsch PetscCall(PetscNewLog(pc, &osm)); 12412fa5cd67SKarl Rupp 12428f3b4b4dSDmitry Karpeev osm->N = PETSC_DETERMINE; 124306b43e7eSDmitry Karpeev osm->n = PETSC_DECIDE; 12448f3b4b4dSDmitry Karpeev osm->nmax = PETSC_DETERMINE; 12458f3b4b4dSDmitry Karpeev osm->overlap = 0; 12460a545947SLisandro Dalcin osm->ksp = NULL; 12470a545947SLisandro Dalcin osm->gorestriction = NULL; 12480a545947SLisandro Dalcin osm->girestriction = NULL; 12490a545947SLisandro Dalcin osm->pctoouter = NULL; 12500a545947SLisandro Dalcin osm->gx = NULL; 12510a545947SLisandro Dalcin osm->gy = NULL; 12520a545947SLisandro Dalcin osm->x = NULL; 12530a545947SLisandro Dalcin osm->y = NULL; 12540a545947SLisandro Dalcin osm->pcx = NULL; 12550a545947SLisandro Dalcin osm->pcy = NULL; 12560a545947SLisandro Dalcin osm->permutationIS = NULL; 12570a545947SLisandro Dalcin osm->permutationP = NULL; 12580a545947SLisandro Dalcin osm->pcmat = NULL; 12590a545947SLisandro Dalcin osm->ois = NULL; 12600a545947SLisandro Dalcin osm->iis = NULL; 12610a545947SLisandro Dalcin osm->pmat = NULL; 1262f746d493SDmitry Karpeev osm->type = PC_GASM_RESTRICT; 12636a4f0f73SDmitry Karpeev osm->same_subdomain_solvers = PETSC_TRUE; 1264b1a0a8a3SJed Brown osm->sort_indices = PETSC_TRUE; 1265d709ea83SDmitry Karpeev osm->dm_subdomains = PETSC_FALSE; 1266ea91fabdSFande Kong osm->hierarchicalpartitioning = PETSC_FALSE; 1267b1a0a8a3SJed Brown 1268b1a0a8a3SJed Brown pc->data = (void *)osm; 1269f746d493SDmitry Karpeev pc->ops->apply = PCApply_GASM; 127048e38a8aSPierre Jolivet pc->ops->matapply = PCMatApply_GASM; 1271f746d493SDmitry Karpeev pc->ops->applytranspose = PCApplyTranspose_GASM; 1272f746d493SDmitry Karpeev pc->ops->setup = PCSetUp_GASM; 1273a06653b4SBarry Smith pc->ops->reset = PCReset_GASM; 1274f746d493SDmitry Karpeev pc->ops->destroy = PCDestroy_GASM; 1275f746d493SDmitry Karpeev pc->ops->setfromoptions = PCSetFromOptions_GASM; 1276f746d493SDmitry Karpeev pc->ops->setuponblocks = PCSetUpOnBlocks_GASM; 1277f746d493SDmitry Karpeev pc->ops->view = PCView_GASM; 12780a545947SLisandro Dalcin pc->ops->applyrichardson = NULL; 1279b1a0a8a3SJed Brown 12809566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetSubdomains_C", PCGASMSetSubdomains_GASM)); 12819566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetOverlap_C", PCGASMSetOverlap_GASM)); 12829566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetType_C", PCGASMSetType_GASM)); 12839566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetSortIndices_C", PCGASMSetSortIndices_GASM)); 12849566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMGetSubKSP_C", PCGASMGetSubKSP_GASM)); 1285b1a0a8a3SJed Brown PetscFunctionReturn(0); 1286b1a0a8a3SJed Brown } 1287b1a0a8a3SJed Brown 12889371c9d4SSatish Balay PetscErrorCode PCGASMCreateLocalSubdomains(Mat A, PetscInt nloc, IS *iis[]) { 1289b1a0a8a3SJed Brown MatPartitioning mpart; 1290b1a0a8a3SJed Brown const char *prefix; 1291b1a0a8a3SJed Brown PetscInt i, j, rstart, rend, bs; 1292976e8c5aSLisandro Dalcin PetscBool hasop, isbaij = PETSC_FALSE, foundpart = PETSC_FALSE; 12930298fd71SBarry Smith Mat Ad = NULL, adj; 1294b1a0a8a3SJed Brown IS ispart, isnumb, *is; 1295b1a0a8a3SJed Brown 1296b1a0a8a3SJed Brown PetscFunctionBegin; 129763a3b9bcSJacob Faibussowitsch PetscCheck(nloc >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "number of local subdomains must > 0, got nloc = %" PetscInt_FMT, nloc); 1298b1a0a8a3SJed Brown 1299b1a0a8a3SJed Brown /* Get prefix, row distribution, and block size */ 13009566063dSJacob Faibussowitsch PetscCall(MatGetOptionsPrefix(A, &prefix)); 13019566063dSJacob Faibussowitsch PetscCall(MatGetOwnershipRange(A, &rstart, &rend)); 13029566063dSJacob Faibussowitsch PetscCall(MatGetBlockSize(A, &bs)); 13032472a847SBarry 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); 1304b1a0a8a3SJed Brown 1305b1a0a8a3SJed Brown /* Get diagonal block from matrix if possible */ 13069566063dSJacob Faibussowitsch PetscCall(MatHasOperation(A, MATOP_GET_DIAGONAL_BLOCK, &hasop)); 130748a46eb9SPierre Jolivet if (hasop) PetscCall(MatGetDiagonalBlock(A, &Ad)); 1308b1a0a8a3SJed Brown if (Ad) { 13099566063dSJacob Faibussowitsch PetscCall(PetscObjectBaseTypeCompare((PetscObject)Ad, MATSEQBAIJ, &isbaij)); 13109566063dSJacob Faibussowitsch if (!isbaij) PetscCall(PetscObjectBaseTypeCompare((PetscObject)Ad, MATSEQSBAIJ, &isbaij)); 1311b1a0a8a3SJed Brown } 13128f3b4b4dSDmitry Karpeev if (Ad && nloc > 1) { 1313b1a0a8a3SJed Brown PetscBool match, done; 1314b1a0a8a3SJed Brown /* Try to setup a good matrix partitioning if available */ 13159566063dSJacob Faibussowitsch PetscCall(MatPartitioningCreate(PETSC_COMM_SELF, &mpart)); 13169566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptionsPrefix((PetscObject)mpart, prefix)); 13179566063dSJacob Faibussowitsch PetscCall(MatPartitioningSetFromOptions(mpart)); 13189566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)mpart, MATPARTITIONINGCURRENT, &match)); 131948a46eb9SPierre Jolivet if (!match) PetscCall(PetscObjectTypeCompare((PetscObject)mpart, MATPARTITIONINGSQUARE, &match)); 1320b1a0a8a3SJed Brown if (!match) { /* assume a "good" partitioner is available */ 13211a83f524SJed Brown PetscInt na; 13221a83f524SJed Brown const PetscInt *ia, *ja; 13239566063dSJacob Faibussowitsch PetscCall(MatGetRowIJ(Ad, 0, PETSC_TRUE, isbaij, &na, &ia, &ja, &done)); 1324b1a0a8a3SJed Brown if (done) { 1325b1a0a8a3SJed Brown /* Build adjacency matrix by hand. Unfortunately a call to 1326b1a0a8a3SJed Brown MatConvert(Ad,MATMPIADJ,MAT_INITIAL_MATRIX,&adj) will 1327b1a0a8a3SJed Brown remove the block-aij structure and we cannot expect 1328b1a0a8a3SJed Brown MatPartitioning to split vertices as we need */ 13290a545947SLisandro Dalcin PetscInt i, j, len, nnz, cnt, *iia = NULL, *jja = NULL; 13301a83f524SJed Brown const PetscInt *row; 1331b1a0a8a3SJed Brown nnz = 0; 1332b1a0a8a3SJed Brown for (i = 0; i < na; i++) { /* count number of nonzeros */ 1333b1a0a8a3SJed Brown len = ia[i + 1] - ia[i]; 1334b1a0a8a3SJed Brown row = ja + ia[i]; 1335b1a0a8a3SJed Brown for (j = 0; j < len; j++) { 1336b1a0a8a3SJed Brown if (row[j] == i) { /* don't count diagonal */ 13379371c9d4SSatish Balay len--; 13389371c9d4SSatish Balay break; 1339b1a0a8a3SJed Brown } 1340b1a0a8a3SJed Brown } 1341b1a0a8a3SJed Brown nnz += len; 1342b1a0a8a3SJed Brown } 13439566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(na + 1, &iia)); 13449566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nnz, &jja)); 1345b1a0a8a3SJed Brown nnz = 0; 1346b1a0a8a3SJed Brown iia[0] = 0; 1347b1a0a8a3SJed Brown for (i = 0; i < na; i++) { /* fill adjacency */ 1348b1a0a8a3SJed Brown cnt = 0; 1349b1a0a8a3SJed Brown len = ia[i + 1] - ia[i]; 1350b1a0a8a3SJed Brown row = ja + ia[i]; 1351b1a0a8a3SJed Brown for (j = 0; j < len; j++) { 13522fa5cd67SKarl Rupp if (row[j] != i) jja[nnz + cnt++] = row[j]; /* if not diagonal */ 1353b1a0a8a3SJed Brown } 1354b1a0a8a3SJed Brown nnz += cnt; 1355b1a0a8a3SJed Brown iia[i + 1] = nnz; 1356b1a0a8a3SJed Brown } 1357b1a0a8a3SJed Brown /* Partitioning of the adjacency matrix */ 13589566063dSJacob Faibussowitsch PetscCall(MatCreateMPIAdj(PETSC_COMM_SELF, na, na, iia, jja, NULL, &adj)); 13599566063dSJacob Faibussowitsch PetscCall(MatPartitioningSetAdjacency(mpart, adj)); 13609566063dSJacob Faibussowitsch PetscCall(MatPartitioningSetNParts(mpart, nloc)); 13619566063dSJacob Faibussowitsch PetscCall(MatPartitioningApply(mpart, &ispart)); 13629566063dSJacob Faibussowitsch PetscCall(ISPartitioningToNumbering(ispart, &isnumb)); 13639566063dSJacob Faibussowitsch PetscCall(MatDestroy(&adj)); 1364b1a0a8a3SJed Brown foundpart = PETSC_TRUE; 1365b1a0a8a3SJed Brown } 13669566063dSJacob Faibussowitsch PetscCall(MatRestoreRowIJ(Ad, 0, PETSC_TRUE, isbaij, &na, &ia, &ja, &done)); 1367b1a0a8a3SJed Brown } 13689566063dSJacob Faibussowitsch PetscCall(MatPartitioningDestroy(&mpart)); 1369b1a0a8a3SJed Brown } 13709566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nloc, &is)); 1371b1a0a8a3SJed Brown if (!foundpart) { 1372b1a0a8a3SJed Brown /* Partitioning by contiguous chunks of rows */ 1373b1a0a8a3SJed Brown 1374b1a0a8a3SJed Brown PetscInt mbs = (rend - rstart) / bs; 1375b1a0a8a3SJed Brown PetscInt start = rstart; 13768f3b4b4dSDmitry Karpeev for (i = 0; i < nloc; i++) { 13778f3b4b4dSDmitry Karpeev PetscInt count = (mbs / nloc + ((mbs % nloc) > i)) * bs; 13789566063dSJacob Faibussowitsch PetscCall(ISCreateStride(PETSC_COMM_SELF, count, start, 1, &is[i])); 1379b1a0a8a3SJed Brown start += count; 1380b1a0a8a3SJed Brown } 1381b1a0a8a3SJed Brown 1382b1a0a8a3SJed Brown } else { 1383b1a0a8a3SJed Brown /* Partitioning by adjacency of diagonal block */ 1384b1a0a8a3SJed Brown 1385b1a0a8a3SJed Brown const PetscInt *numbering; 1386b1a0a8a3SJed Brown PetscInt *count, nidx, *indices, *newidx, start = 0; 1387b1a0a8a3SJed Brown /* Get node count in each partition */ 13889566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nloc, &count)); 13899566063dSJacob Faibussowitsch PetscCall(ISPartitioningCount(ispart, nloc, count)); 1390b1a0a8a3SJed Brown if (isbaij && bs > 1) { /* adjust for the block-aij case */ 13918f3b4b4dSDmitry Karpeev for (i = 0; i < nloc; i++) count[i] *= bs; 1392b1a0a8a3SJed Brown } 1393b1a0a8a3SJed Brown /* Build indices from node numbering */ 13949566063dSJacob Faibussowitsch PetscCall(ISGetLocalSize(isnumb, &nidx)); 13959566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nidx, &indices)); 1396b1a0a8a3SJed Brown for (i = 0; i < nidx; i++) indices[i] = i; /* needs to be initialized */ 13979566063dSJacob Faibussowitsch PetscCall(ISGetIndices(isnumb, &numbering)); 13989566063dSJacob Faibussowitsch PetscCall(PetscSortIntWithPermutation(nidx, numbering, indices)); 13999566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(isnumb, &numbering)); 1400b1a0a8a3SJed Brown if (isbaij && bs > 1) { /* adjust for the block-aij case */ 14019566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nidx * bs, &newidx)); 14022fa5cd67SKarl Rupp for (i = 0; i < nidx; i++) { 14032fa5cd67SKarl Rupp for (j = 0; j < bs; j++) newidx[i * bs + j] = indices[i] * bs + j; 14042fa5cd67SKarl Rupp } 14059566063dSJacob Faibussowitsch PetscCall(PetscFree(indices)); 1406b1a0a8a3SJed Brown nidx *= bs; 1407b1a0a8a3SJed Brown indices = newidx; 1408b1a0a8a3SJed Brown } 1409b1a0a8a3SJed Brown /* Shift to get global indices */ 1410b1a0a8a3SJed Brown for (i = 0; i < nidx; i++) indices[i] += rstart; 1411b1a0a8a3SJed Brown 1412b1a0a8a3SJed Brown /* Build the index sets for each block */ 14138f3b4b4dSDmitry Karpeev for (i = 0; i < nloc; i++) { 14149566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, count[i], &indices[start], PETSC_COPY_VALUES, &is[i])); 14159566063dSJacob Faibussowitsch PetscCall(ISSort(is[i])); 1416b1a0a8a3SJed Brown start += count[i]; 1417b1a0a8a3SJed Brown } 1418b1a0a8a3SJed Brown 14199566063dSJacob Faibussowitsch PetscCall(PetscFree(count)); 14209566063dSJacob Faibussowitsch PetscCall(PetscFree(indices)); 14219566063dSJacob Faibussowitsch PetscCall(ISDestroy(&isnumb)); 14229566063dSJacob Faibussowitsch PetscCall(ISDestroy(&ispart)); 1423b1a0a8a3SJed Brown } 14246a4f0f73SDmitry Karpeev *iis = is; 14258f3b4b4dSDmitry Karpeev PetscFunctionReturn(0); 14268f3b4b4dSDmitry Karpeev } 14278f3b4b4dSDmitry Karpeev 14289371c9d4SSatish Balay PETSC_INTERN PetscErrorCode PCGASMCreateStraddlingSubdomains(Mat A, PetscInt N, PetscInt *n, IS *iis[]) { 14298f3b4b4dSDmitry Karpeev PetscFunctionBegin; 14309566063dSJacob Faibussowitsch PetscCall(MatSubdomainsCreateCoalesce(A, N, n, iis)); 14318f3b4b4dSDmitry Karpeev PetscFunctionReturn(0); 14328f3b4b4dSDmitry Karpeev } 14338f3b4b4dSDmitry Karpeev 14348f3b4b4dSDmitry Karpeev /*@C 1435*f1580f4eSBarry Smith PCGASMCreateSubdomains - Creates n index sets defining n nonoverlapping subdomains for the `PCGASM` additive 14368f3b4b4dSDmitry Karpeev Schwarz preconditioner for a any problem based on its matrix. 14378f3b4b4dSDmitry Karpeev 14388f3b4b4dSDmitry Karpeev Collective 14398f3b4b4dSDmitry Karpeev 14408f3b4b4dSDmitry Karpeev Input Parameters: 14418f3b4b4dSDmitry Karpeev + A - The global matrix operator 14428f3b4b4dSDmitry Karpeev - N - the number of global subdomains requested 14438f3b4b4dSDmitry Karpeev 14448f3b4b4dSDmitry Karpeev Output Parameters: 1445*f1580f4eSBarry Smith + n - the number of subdomains created on this MPI rank 14468f3b4b4dSDmitry Karpeev - iis - the array of index sets defining the local inner subdomains (on which the correction is applied) 14478f3b4b4dSDmitry Karpeev 14488f3b4b4dSDmitry Karpeev Level: advanced 14498f3b4b4dSDmitry Karpeev 1450*f1580f4eSBarry Smith Note: 1451*f1580f4eSBarry Smith When N >= A's communicator size, each subdomain is local -- contained within a single MPI rank. 1452*f1580f4eSBarry Smith When N < size, the subdomains are 'straddling' (rank boundaries) and are no longer local. 1453*f1580f4eSBarry Smith The resulting subdomains can be use in `PCGASMSetSubdomains`(pc,n,iss,NULL). The overlapping 14548f3b4b4dSDmitry Karpeev outer subdomains will be automatically generated from these according to the requested amount of 14558f3b4b4dSDmitry Karpeev overlap; this is currently supported only with local subdomains. 14568f3b4b4dSDmitry Karpeev 1457*f1580f4eSBarry Smith .seealso: `PCGASM`, `PCGASMSetSubdomains()`, `PCGASMDestroySubdomains()` 14588f3b4b4dSDmitry Karpeev @*/ 14599371c9d4SSatish Balay PetscErrorCode PCGASMCreateSubdomains(Mat A, PetscInt N, PetscInt *n, IS *iis[]) { 14608f3b4b4dSDmitry Karpeev PetscMPIInt size; 14618f3b4b4dSDmitry Karpeev 14628f3b4b4dSDmitry Karpeev PetscFunctionBegin; 14638f3b4b4dSDmitry Karpeev PetscValidHeaderSpecific(A, MAT_CLASSID, 1); 14648f3b4b4dSDmitry Karpeev PetscValidPointer(iis, 4); 14658f3b4b4dSDmitry Karpeev 146663a3b9bcSJacob Faibussowitsch PetscCheck(N >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of subdomains must be > 0, N = %" PetscInt_FMT, N); 14679566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)A), &size)); 14688f3b4b4dSDmitry Karpeev if (N >= size) { 14698f3b4b4dSDmitry Karpeev *n = N / size + (N % size); 14709566063dSJacob Faibussowitsch PetscCall(PCGASMCreateLocalSubdomains(A, *n, iis)); 14716a4f0f73SDmitry Karpeev } else { 14729566063dSJacob Faibussowitsch PetscCall(PCGASMCreateStraddlingSubdomains(A, N, n, iis)); 14736a4f0f73SDmitry Karpeev } 1474b1a0a8a3SJed Brown PetscFunctionReturn(0); 1475b1a0a8a3SJed Brown } 1476b1a0a8a3SJed Brown 1477b1a0a8a3SJed Brown /*@C 1478f746d493SDmitry Karpeev PCGASMDestroySubdomains - Destroys the index sets created with 1479*f1580f4eSBarry Smith `PCGASMCreateSubdomains()` or `PCGASMCreateSubdomains2D()`. Should be 1480*f1580f4eSBarry Smith called after setting subdomains with `PCGASMSetSubdomains()`. 1481b1a0a8a3SJed Brown 1482b1a0a8a3SJed Brown Collective 1483b1a0a8a3SJed Brown 1484b1a0a8a3SJed Brown Input Parameters: 1485b1a0a8a3SJed Brown + n - the number of index sets 14866a4f0f73SDmitry Karpeev . iis - the array of inner subdomains, 14870298fd71SBarry Smith - ois - the array of outer subdomains, can be NULL 1488b1a0a8a3SJed Brown 14896a4f0f73SDmitry Karpeev Level: intermediate 14906a4f0f73SDmitry Karpeev 1491*f1580f4eSBarry Smith Note: 1492*f1580f4eSBarry Smith This is a convenience subroutine that walks each list, 1493*f1580f4eSBarry Smith destroys each `IS` on the list, and then frees the list. At the end the 14942c112581SDmitry Karpeev list pointers are set to NULL. 1495b1a0a8a3SJed Brown 1496*f1580f4eSBarry Smith .seealso: `PCGASM`, `PCGASMCreateSubdomains()`, `PCGASMSetSubdomains()` 1497b1a0a8a3SJed Brown @*/ 14989371c9d4SSatish Balay PetscErrorCode PCGASMDestroySubdomains(PetscInt n, IS **iis, IS **ois) { 1499b1a0a8a3SJed Brown PetscInt i; 15005fd66863SKarl Rupp 1501b1a0a8a3SJed Brown PetscFunctionBegin; 1502a35b7b57SDmitry Karpeev if (n <= 0) PetscFunctionReturn(0); 15036a4f0f73SDmitry Karpeev if (ois) { 15042c112581SDmitry Karpeev PetscValidPointer(ois, 3); 15052c112581SDmitry Karpeev if (*ois) { 15062c112581SDmitry Karpeev PetscValidPointer(*ois, 3); 150748a46eb9SPierre Jolivet for (i = 0; i < n; i++) PetscCall(ISDestroy(&(*ois)[i])); 15089566063dSJacob Faibussowitsch PetscCall(PetscFree((*ois))); 15092c112581SDmitry Karpeev } 1510b1a0a8a3SJed Brown } 1511b9d0fdaaSFande Kong if (iis) { 1512b9d0fdaaSFande Kong PetscValidPointer(iis, 2); 1513b9d0fdaaSFande Kong if (*iis) { 1514b9d0fdaaSFande Kong PetscValidPointer(*iis, 2); 151548a46eb9SPierre Jolivet for (i = 0; i < n; i++) PetscCall(ISDestroy(&(*iis)[i])); 15169566063dSJacob Faibussowitsch PetscCall(PetscFree((*iis))); 1517b9d0fdaaSFande Kong } 1518b9d0fdaaSFande Kong } 1519b1a0a8a3SJed Brown PetscFunctionReturn(0); 1520b1a0a8a3SJed Brown } 1521b1a0a8a3SJed Brown 1522af538404SDmitry Karpeev #define PCGASMLocalSubdomainBounds2D(M, N, xleft, ylow, xright, yhigh, first, last, xleft_loc, ylow_loc, xright_loc, yhigh_loc, n) \ 1523af538404SDmitry Karpeev { \ 1524af538404SDmitry Karpeev PetscInt first_row = first / M, last_row = last / M + 1; \ 1525af538404SDmitry Karpeev /* \ 1526af538404SDmitry Karpeev Compute ylow_loc and yhigh_loc so that (ylow_loc,xleft) and (yhigh_loc,xright) are the corners \ 1527af538404SDmitry Karpeev of the bounding box of the intersection of the subdomain with the local ownership range (local \ 1528af538404SDmitry Karpeev subdomain). \ 1529af538404SDmitry Karpeev Also compute xleft_loc and xright_loc as the lower and upper bounds on the first and last rows \ 1530af538404SDmitry Karpeev of the intersection. \ 1531af538404SDmitry Karpeev */ \ 1532af538404SDmitry Karpeev /* ylow_loc is the grid row containing the first element of the local sumbdomain */ \ 1533eec7e3faSDmitry Karpeev *ylow_loc = PetscMax(first_row, ylow); \ 1534af538404SDmitry Karpeev /* xleft_loc is the offset of first element of the local subdomain within its grid row (might actually be outside the local subdomain) */ \ 1535eec7e3faSDmitry Karpeev *xleft_loc = *ylow_loc == first_row ? PetscMax(first % M, xleft) : xleft; \ 1536af538404SDmitry Karpeev /* yhigh_loc is the grid row above the last local subdomain element */ \ 1537eec7e3faSDmitry Karpeev *yhigh_loc = PetscMin(last_row, yhigh); \ 1538af538404SDmitry Karpeev /* xright is the offset of the end of the local subdomain within its grid row (might actually be outside the local subdomain) */ \ 1539eec7e3faSDmitry Karpeev *xright_loc = *yhigh_loc == last_row ? PetscMin(xright, last % M) : xright; \ 1540af538404SDmitry Karpeev /* Now compute the size of the local subdomain n. */ \ 1541c3518bceSDmitry Karpeev *n = 0; \ 1542eec7e3faSDmitry Karpeev if (*ylow_loc < *yhigh_loc) { \ 1543af538404SDmitry Karpeev PetscInt width = xright - xleft; \ 1544eec7e3faSDmitry Karpeev *n += width * (*yhigh_loc - *ylow_loc - 1); \ 1545eec7e3faSDmitry Karpeev *n += PetscMin(PetscMax(*xright_loc - xleft, 0), width); \ 1546eec7e3faSDmitry Karpeev *n -= PetscMin(PetscMax(*xleft_loc - xleft, 0), width); \ 1547af538404SDmitry Karpeev } \ 1548af538404SDmitry Karpeev } 1549af538404SDmitry Karpeev 1550b1a0a8a3SJed Brown /*@ 1551*f1580f4eSBarry Smith PCGASMCreateSubdomains2D - Creates the index sets for the `PCGASM` overlapping Schwarz 1552b1a0a8a3SJed Brown preconditioner for a two-dimensional problem on a regular grid. 1553b1a0a8a3SJed Brown 1554af538404SDmitry Karpeev Collective 1555b1a0a8a3SJed Brown 1556b1a0a8a3SJed Brown Input Parameters: 15576b867d5aSJose E. Roman + pc - the preconditioner context 15586b867d5aSJose E. Roman . M - the global number of grid points in the x direction 15596b867d5aSJose E. Roman . N - the global number of grid points in the y direction 15606b867d5aSJose E. Roman . Mdomains - the global number of subdomains in the x direction 15616b867d5aSJose E. Roman . Ndomains - the global number of subdomains in the y direction 1562b1a0a8a3SJed Brown . dof - degrees of freedom per node 1563b1a0a8a3SJed Brown - overlap - overlap in mesh lines 1564b1a0a8a3SJed Brown 1565b1a0a8a3SJed Brown Output Parameters: 1566af538404SDmitry Karpeev + Nsub - the number of local subdomains created 15676a4f0f73SDmitry Karpeev . iis - array of index sets defining inner (nonoverlapping) subdomains 15686a4f0f73SDmitry Karpeev - ois - array of index sets defining outer (overlapping, if overlap > 0) subdomains 1569b1a0a8a3SJed Brown 1570b1a0a8a3SJed Brown Level: advanced 1571b1a0a8a3SJed Brown 1572*f1580f4eSBarry Smith .seealso: `PCGASM`, `PCGASMSetSubdomains()`, `PCGASMGetSubKSP()`, `PCGASMSetOverlap()` 1573b1a0a8a3SJed Brown @*/ 15749371c9d4SSatish Balay PetscErrorCode PCGASMCreateSubdomains2D(PC pc, PetscInt M, PetscInt N, PetscInt Mdomains, PetscInt Ndomains, PetscInt dof, PetscInt overlap, PetscInt *nsub, IS **iis, IS **ois) { 15752bbb417fSDmitry Karpeev PetscMPIInt size, rank; 15762bbb417fSDmitry Karpeev PetscInt i, j; 15772bbb417fSDmitry Karpeev PetscInt maxheight, maxwidth; 15782bbb417fSDmitry Karpeev PetscInt xstart, xleft, xright, xleft_loc, xright_loc; 15792bbb417fSDmitry Karpeev PetscInt ystart, ylow, yhigh, ylow_loc, yhigh_loc; 1580eec7e3faSDmitry Karpeev PetscInt x[2][2], y[2][2], n[2]; 15812bbb417fSDmitry Karpeev PetscInt first, last; 15822bbb417fSDmitry Karpeev PetscInt nidx, *idx; 15832bbb417fSDmitry Karpeev PetscInt ii, jj, s, q, d; 1584af538404SDmitry Karpeev PetscInt k, kk; 15852bbb417fSDmitry Karpeev PetscMPIInt color; 15862bbb417fSDmitry Karpeev MPI_Comm comm, subcomm; 15870a545947SLisandro Dalcin IS **xis = NULL, **is = ois, **is_local = iis; 1588d34fcf5fSBarry Smith 1589b1a0a8a3SJed Brown PetscFunctionBegin; 15909566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)pc, &comm)); 15919566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(comm, &size)); 15929566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm, &rank)); 15939566063dSJacob Faibussowitsch PetscCall(MatGetOwnershipRange(pc->pmat, &first, &last)); 15949371c9d4SSatish Balay PetscCheck((first % dof) == 0 && (last % dof) == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, 15959371c9d4SSatish Balay "Matrix row partitioning unsuitable for domain decomposition: local row range (%" PetscInt_FMT ",%" PetscInt_FMT ") " 15969371c9d4SSatish Balay "does not respect the number of degrees of freedom per grid point %" PetscInt_FMT, 15979371c9d4SSatish Balay first, last, dof); 1598d34fcf5fSBarry Smith 1599af538404SDmitry Karpeev /* Determine the number of domains with nonzero intersections with the local ownership range. */ 16002bbb417fSDmitry Karpeev s = 0; 1601af538404SDmitry Karpeev ystart = 0; 1602af538404SDmitry Karpeev for (j = 0; j < Ndomains; ++j) { 1603af538404SDmitry Karpeev maxheight = N / Ndomains + ((N % Ndomains) > j); /* Maximal height of subdomain */ 160463a3b9bcSJacob 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); 1605eec7e3faSDmitry Karpeev /* Vertical domain limits with an overlap. */ 1606eec7e3faSDmitry Karpeev ylow = PetscMax(ystart - overlap, 0); 1607eec7e3faSDmitry Karpeev yhigh = PetscMin(ystart + maxheight + overlap, N); 1608b1a0a8a3SJed Brown xstart = 0; 1609af538404SDmitry Karpeev for (i = 0; i < Mdomains; ++i) { 1610af538404SDmitry Karpeev maxwidth = M / Mdomains + ((M % Mdomains) > i); /* Maximal width of subdomain */ 161163a3b9bcSJacob 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); 1612eec7e3faSDmitry Karpeev /* Horizontal domain limits with an overlap. */ 1613eec7e3faSDmitry Karpeev xleft = PetscMax(xstart - overlap, 0); 1614eec7e3faSDmitry Karpeev xright = PetscMin(xstart + maxwidth + overlap, M); 1615af538404SDmitry Karpeev /* 1616*f1580f4eSBarry Smith Determine whether this subdomain intersects this rank's ownership range of pc->pmat. 1617af538404SDmitry Karpeev */ 1618c3518bceSDmitry Karpeev PCGASMLocalSubdomainBounds2D(M, N, xleft, ylow, xright, yhigh, first, last, (&xleft_loc), (&ylow_loc), (&xright_loc), (&yhigh_loc), (&nidx)); 16192fa5cd67SKarl Rupp if (nidx) ++s; 1620af538404SDmitry Karpeev xstart += maxwidth; 1621af538404SDmitry Karpeev } /* for (i = 0; i < Mdomains; ++i) */ 1622af538404SDmitry Karpeev ystart += maxheight; 1623af538404SDmitry Karpeev } /* for (j = 0; j < Ndomains; ++j) */ 16242fa5cd67SKarl Rupp 1625af538404SDmitry Karpeev /* Now we can allocate the necessary number of ISs. */ 1626af538404SDmitry Karpeev *nsub = s; 16279566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(*nsub, is)); 16289566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(*nsub, is_local)); 1629af538404SDmitry Karpeev s = 0; 1630af538404SDmitry Karpeev ystart = 0; 1631af538404SDmitry Karpeev for (j = 0; j < Ndomains; ++j) { 1632af538404SDmitry Karpeev maxheight = N / Ndomains + ((N % Ndomains) > j); /* Maximal height of subdomain */ 163363a3b9bcSJacob 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); 1634af538404SDmitry Karpeev /* Vertical domain limits with an overlap. */ 1635eec7e3faSDmitry Karpeev y[0][0] = PetscMax(ystart - overlap, 0); 1636eec7e3faSDmitry Karpeev y[0][1] = PetscMin(ystart + maxheight + overlap, N); 1637eec7e3faSDmitry Karpeev /* Vertical domain limits without an overlap. */ 1638eec7e3faSDmitry Karpeev y[1][0] = ystart; 1639eec7e3faSDmitry Karpeev y[1][1] = PetscMin(ystart + maxheight, N); 1640eec7e3faSDmitry Karpeev xstart = 0; 1641af538404SDmitry Karpeev for (i = 0; i < Mdomains; ++i) { 1642af538404SDmitry Karpeev maxwidth = M / Mdomains + ((M % Mdomains) > i); /* Maximal width of subdomain */ 164363a3b9bcSJacob 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); 1644af538404SDmitry Karpeev /* Horizontal domain limits with an overlap. */ 1645eec7e3faSDmitry Karpeev x[0][0] = PetscMax(xstart - overlap, 0); 1646eec7e3faSDmitry Karpeev x[0][1] = PetscMin(xstart + maxwidth + overlap, M); 1647eec7e3faSDmitry Karpeev /* Horizontal domain limits without an overlap. */ 1648eec7e3faSDmitry Karpeev x[1][0] = xstart; 1649eec7e3faSDmitry Karpeev x[1][1] = PetscMin(xstart + maxwidth, M); 16502bbb417fSDmitry Karpeev /* 1651*f1580f4eSBarry Smith Determine whether this domain intersects this rank's ownership range of pc->pmat. 16522bbb417fSDmitry Karpeev Do this twice: first for the domains with overlaps, and once without. 16532bbb417fSDmitry Karpeev During the first pass create the subcommunicators, and use them on the second pass as well. 16542bbb417fSDmitry Karpeev */ 16552bbb417fSDmitry Karpeev for (q = 0; q < 2; ++q) { 1656cc96b2e5SBarry Smith PetscBool split = PETSC_FALSE; 16572bbb417fSDmitry Karpeev /* 16582bbb417fSDmitry Karpeev domain limits, (xleft, xright) and (ylow, yheigh) are adjusted 16592bbb417fSDmitry Karpeev according to whether the domain with an overlap or without is considered. 16602bbb417fSDmitry Karpeev */ 16619371c9d4SSatish Balay xleft = x[q][0]; 16629371c9d4SSatish Balay xright = x[q][1]; 16639371c9d4SSatish Balay ylow = y[q][0]; 16649371c9d4SSatish Balay yhigh = y[q][1]; 1665c3518bceSDmitry Karpeev PCGASMLocalSubdomainBounds2D(M, N, xleft, ylow, xright, yhigh, first, last, (&xleft_loc), (&ylow_loc), (&xright_loc), (&yhigh_loc), (&nidx)); 1666af538404SDmitry Karpeev nidx *= dof; 1667eec7e3faSDmitry Karpeev n[q] = nidx; 16682bbb417fSDmitry Karpeev /* 1669eec7e3faSDmitry Karpeev Based on the counted number of indices in the local domain *with an overlap*, 1670*f1580f4eSBarry Smith construct a subcommunicator of all the MPI ranks supporting this domain. 1671eec7e3faSDmitry Karpeev Observe that a domain with an overlap might have nontrivial local support, 1672eec7e3faSDmitry Karpeev while the domain without an overlap might not. Hence, the decision to participate 1673eec7e3faSDmitry Karpeev in the subcommunicator must be based on the domain with an overlap. 16742bbb417fSDmitry Karpeev */ 16752bbb417fSDmitry Karpeev if (q == 0) { 16762fa5cd67SKarl Rupp if (nidx) color = 1; 16772fa5cd67SKarl Rupp else color = MPI_UNDEFINED; 16789566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_split(comm, color, rank, &subcomm)); 1679cc96b2e5SBarry Smith split = PETSC_TRUE; 16802bbb417fSDmitry Karpeev } 1681af538404SDmitry Karpeev /* 1682eec7e3faSDmitry Karpeev Proceed only if the number of local indices *with an overlap* is nonzero. 1683af538404SDmitry Karpeev */ 1684eec7e3faSDmitry Karpeev if (n[0]) { 16852fa5cd67SKarl Rupp if (q == 0) xis = is; 1686af538404SDmitry Karpeev if (q == 1) { 1687af538404SDmitry Karpeev /* 1688eec7e3faSDmitry Karpeev The IS for the no-overlap subdomain shares a communicator with the overlapping domain. 1689af538404SDmitry Karpeev Moreover, if the overlap is zero, the two ISs are identical. 1690af538404SDmitry Karpeev */ 1691b1a0a8a3SJed Brown if (overlap == 0) { 1692eec7e3faSDmitry Karpeev (*is_local)[s] = (*is)[s]; 16939566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)(*is)[s])); 16942bbb417fSDmitry Karpeev continue; 1695d34fcf5fSBarry Smith } else { 16966a4f0f73SDmitry Karpeev xis = is_local; 1697eec7e3faSDmitry Karpeev subcomm = ((PetscObject)(*is)[s])->comm; 16982bbb417fSDmitry Karpeev } 1699af538404SDmitry Karpeev } /* if (q == 1) */ 17000298fd71SBarry Smith idx = NULL; 17019566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nidx, &idx)); 1702eec7e3faSDmitry Karpeev if (nidx) { 17032bbb417fSDmitry Karpeev k = 0; 17042bbb417fSDmitry Karpeev for (jj = ylow_loc; jj < yhigh_loc; ++jj) { 1705af538404SDmitry Karpeev PetscInt x0 = (jj == ylow_loc) ? xleft_loc : xleft; 1706af538404SDmitry Karpeev PetscInt x1 = (jj == yhigh_loc - 1) ? xright_loc : xright; 1707af538404SDmitry Karpeev kk = dof * (M * jj + x0); 17082bbb417fSDmitry Karpeev for (ii = x0; ii < x1; ++ii) { 1709ad540459SPierre Jolivet for (d = 0; d < dof; ++d) idx[k++] = kk++; 1710b1a0a8a3SJed Brown } 1711b1a0a8a3SJed Brown } 1712eec7e3faSDmitry Karpeev } 17139566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(subcomm, nidx, idx, PETSC_OWN_POINTER, (*xis) + s)); 171448a46eb9SPierre Jolivet if (split) PetscCallMPI(MPI_Comm_free(&subcomm)); 1715eec7e3faSDmitry Karpeev } /* if (n[0]) */ 17162bbb417fSDmitry Karpeev } /* for (q = 0; q < 2; ++q) */ 17172fa5cd67SKarl Rupp if (n[0]) ++s; 1718af538404SDmitry Karpeev xstart += maxwidth; 1719af538404SDmitry Karpeev } /* for (i = 0; i < Mdomains; ++i) */ 17202bbb417fSDmitry Karpeev ystart += maxheight; 1721af538404SDmitry Karpeev } /* for (j = 0; j < Ndomains; ++j) */ 1722b1a0a8a3SJed Brown PetscFunctionReturn(0); 1723b1a0a8a3SJed Brown } 1724b1a0a8a3SJed Brown 1725b1a0a8a3SJed Brown /*@C 1726*f1580f4eSBarry Smith PCGASMGetSubdomains - Gets the subdomains supported on this MPI rank 1727*f1580f4eSBarry Smith for the `PCGASM` additive Schwarz preconditioner. 1728b1a0a8a3SJed Brown 1729b1a0a8a3SJed Brown Not Collective 1730b1a0a8a3SJed Brown 1731b1a0a8a3SJed Brown Input Parameter: 1732b1a0a8a3SJed Brown . pc - the preconditioner context 1733b1a0a8a3SJed Brown 1734b1a0a8a3SJed Brown Output Parameters: 1735*f1580f4eSBarry Smith + n - the number of subdomains for this MPI rank (default value = 1) 1736*f1580f4eSBarry Smith . iis - the index sets that define the inner subdomains (without overlap) supported on this rank (can be NULL) 1737*f1580f4eSBarry Smith - ois - the index sets that define the outer subdomains (with overlap) supported on this rank (can be NULL) 1738b1a0a8a3SJed Brown 1739*f1580f4eSBarry Smith Note: 1740*f1580f4eSBarry Smith The user is responsible for destroying the `IS`s and freeing the returned arrays. 1741*f1580f4eSBarry Smith The `IS` numbering is in the parallel, global numbering of the vector. 1742b1a0a8a3SJed Brown 1743b1a0a8a3SJed Brown Level: advanced 1744b1a0a8a3SJed Brown 1745*f1580f4eSBarry Smith .seealso: `PCGASM`, `PCGASMSetOverlap()`, `PCGASMGetSubKSP()`, `PCGASMCreateSubdomains2D()`, 1746db781477SPatrick Sanan `PCGASMSetSubdomains()`, `PCGASMGetSubmatrices()` 1747b1a0a8a3SJed Brown @*/ 17489371c9d4SSatish Balay PetscErrorCode PCGASMGetSubdomains(PC pc, PetscInt *n, IS *iis[], IS *ois[]) { 1749f746d493SDmitry Karpeev PC_GASM *osm; 1750b1a0a8a3SJed Brown PetscBool match; 17516a4f0f73SDmitry Karpeev PetscInt i; 17525fd66863SKarl Rupp 1753b1a0a8a3SJed Brown PetscFunctionBegin; 1754b1a0a8a3SJed Brown PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 17559566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCGASM, &match)); 175628b400f6SJacob Faibussowitsch PetscCheck(match, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONG, "Incorrect object type: expected %s, got %s instead", PCGASM, ((PetscObject)pc)->type_name); 1757f746d493SDmitry Karpeev osm = (PC_GASM *)pc->data; 1758ab3e923fSDmitry Karpeev if (n) *n = osm->n; 17591baa6e33SBarry Smith if (iis) PetscCall(PetscMalloc1(osm->n, iis)); 17601baa6e33SBarry Smith if (ois) PetscCall(PetscMalloc1(osm->n, ois)); 17616a4f0f73SDmitry Karpeev if (iis || ois) { 17626a4f0f73SDmitry Karpeev for (i = 0; i < osm->n; ++i) { 17636a4f0f73SDmitry Karpeev if (iis) (*iis)[i] = osm->iis[i]; 17646a4f0f73SDmitry Karpeev if (ois) (*ois)[i] = osm->ois[i]; 17656a4f0f73SDmitry Karpeev } 1766b1a0a8a3SJed Brown } 1767b1a0a8a3SJed Brown PetscFunctionReturn(0); 1768b1a0a8a3SJed Brown } 1769b1a0a8a3SJed Brown 1770b1a0a8a3SJed Brown /*@C 1771*f1580f4eSBarry Smith PCGASMGetSubmatrices - Gets the local submatrices (for this MPI rank 1772*f1580f4eSBarry Smith only) for the `PCGASM` additive Schwarz preconditioner. 1773b1a0a8a3SJed Brown 1774b1a0a8a3SJed Brown Not Collective 1775b1a0a8a3SJed Brown 1776b1a0a8a3SJed Brown Input Parameter: 1777b1a0a8a3SJed Brown . pc - the preconditioner context 1778b1a0a8a3SJed Brown 1779b1a0a8a3SJed Brown Output Parameters: 1780*f1580f4eSBarry Smith + n - the number of matrices for this MPI rank (default value = 1) 1781b1a0a8a3SJed Brown - mat - the matrices 1782b1a0a8a3SJed Brown 1783*f1580f4eSBarry Smith Note: 1784*f1580f4eSBarry Smith Matrices returned by this routine have the same communicators as the index sets (IS) 1785*f1580f4eSBarry Smith used to define subdomains in `PCGASMSetSubdomains()` 1786*f1580f4eSBarry Smith 1787b1a0a8a3SJed Brown Level: advanced 1788b1a0a8a3SJed Brown 1789*f1580f4eSBarry Smith .seealso: `PCGASM`, `PCGASMSetOverlap()`, `PCGASMGetSubKSP()`, 1790db781477SPatrick Sanan `PCGASMCreateSubdomains2D()`, `PCGASMSetSubdomains()`, `PCGASMGetSubdomains()` 1791b1a0a8a3SJed Brown @*/ 17929371c9d4SSatish Balay PetscErrorCode PCGASMGetSubmatrices(PC pc, PetscInt *n, Mat *mat[]) { 1793f746d493SDmitry Karpeev PC_GASM *osm; 1794b1a0a8a3SJed Brown PetscBool match; 1795b1a0a8a3SJed Brown 1796b1a0a8a3SJed Brown PetscFunctionBegin; 1797b1a0a8a3SJed Brown PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 1798b1a0a8a3SJed Brown PetscValidIntPointer(n, 2); 1799b1a0a8a3SJed Brown if (mat) PetscValidPointer(mat, 3); 180028b400f6SJacob Faibussowitsch PetscCheck(pc->setupcalled, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Must call after KSPSetUp() or PCSetUp()."); 18019566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCGASM, &match)); 180228b400f6SJacob Faibussowitsch PetscCheck(match, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONG, "Expected %s, got %s instead", PCGASM, ((PetscObject)pc)->type_name); 1803f746d493SDmitry Karpeev osm = (PC_GASM *)pc->data; 1804ab3e923fSDmitry Karpeev if (n) *n = osm->n; 1805b1a0a8a3SJed Brown if (mat) *mat = osm->pmat; 1806b1a0a8a3SJed Brown PetscFunctionReturn(0); 1807b1a0a8a3SJed Brown } 1808d709ea83SDmitry Karpeev 1809d709ea83SDmitry Karpeev /*@ 1810*f1580f4eSBarry Smith PCGASMSetUseDMSubdomains - Indicates whether to use `DMCreateDomainDecomposition()` to define the subdomains, whenever possible for `PCGASM` 1811*f1580f4eSBarry Smith 1812d709ea83SDmitry Karpeev Logically Collective 1813d709ea83SDmitry Karpeev 1814d8d19677SJose E. Roman Input Parameters: 1815d709ea83SDmitry Karpeev + pc - the preconditioner 1816*f1580f4eSBarry Smith - flg - boolean indicating whether to use subdomains defined by the `DM` 1817d709ea83SDmitry Karpeev 1818d709ea83SDmitry Karpeev Options Database Key: 18198f3b4b4dSDmitry Karpeev . -pc_gasm_dm_subdomains -pc_gasm_overlap -pc_gasm_total_subdomains 1820d709ea83SDmitry Karpeev 1821d709ea83SDmitry Karpeev Level: intermediate 1822d709ea83SDmitry Karpeev 1823*f1580f4eSBarry Smith Note: 1824*f1580f4eSBarry Smith `PCGASMSetSubdomains()`, `PCGASMSetTotalSubdomains()` or `PCGASMSetOverlap()` take precedence over `PCGASMSetUseDMSubdomains()`, 1825*f1580f4eSBarry Smith so setting `PCGASMSetSubdomains()` with nontrivial subdomain ISs or any of `PCGASMSetTotalSubdomains()` and `PCGASMSetOverlap()` 18268f3b4b4dSDmitry Karpeev automatically turns the latter off. 1827d709ea83SDmitry Karpeev 1828*f1580f4eSBarry Smith .seealso: `PCGASM`, `PCGASMGetUseDMSubdomains()`, `PCGASMSetSubdomains()`, `PCGASMSetOverlap()` 1829db781477SPatrick Sanan `PCGASMCreateSubdomains2D()` 1830d709ea83SDmitry Karpeev @*/ 18319371c9d4SSatish Balay PetscErrorCode PCGASMSetUseDMSubdomains(PC pc, PetscBool flg) { 1832d709ea83SDmitry Karpeev PC_GASM *osm = (PC_GASM *)pc->data; 1833d709ea83SDmitry Karpeev PetscBool match; 1834d709ea83SDmitry Karpeev 1835d709ea83SDmitry Karpeev PetscFunctionBegin; 1836d709ea83SDmitry Karpeev PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 1837d709ea83SDmitry Karpeev PetscValidLogicalCollectiveBool(pc, flg, 2); 183828b400f6SJacob Faibussowitsch PetscCheck(!pc->setupcalled, ((PetscObject)pc)->comm, PETSC_ERR_ARG_WRONGSTATE, "Not for a setup PC."); 18399566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCGASM, &match)); 1840d709ea83SDmitry Karpeev if (match) { 1841ad540459SPierre Jolivet if (!osm->user_subdomains && osm->N == PETSC_DETERMINE && osm->overlap < 0) osm->dm_subdomains = flg; 18428f3b4b4dSDmitry Karpeev } 1843d709ea83SDmitry Karpeev PetscFunctionReturn(0); 1844d709ea83SDmitry Karpeev } 1845d709ea83SDmitry Karpeev 1846d709ea83SDmitry Karpeev /*@ 1847*f1580f4eSBarry Smith PCGASMGetUseDMSubdomains - Returns flag indicating whether to use `DMCreateDomainDecomposition()` to define the subdomains, whenever possible with `PCGASM` 1848*f1580f4eSBarry Smith 1849d709ea83SDmitry Karpeev Not Collective 1850d709ea83SDmitry Karpeev 1851d709ea83SDmitry Karpeev Input Parameter: 1852d709ea83SDmitry Karpeev . pc - the preconditioner 1853d709ea83SDmitry Karpeev 1854d709ea83SDmitry Karpeev Output Parameter: 1855*f1580f4eSBarry Smith . flg - boolean indicating whether to use subdomains defined by the `DM` 1856d709ea83SDmitry Karpeev 1857d709ea83SDmitry Karpeev Level: intermediate 1858d709ea83SDmitry Karpeev 1859*f1580f4eSBarry Smith .seealso: `PCGASM`, `PCGASMSetUseDMSubdomains()`, `PCGASMSetOverlap()` 1860db781477SPatrick Sanan `PCGASMCreateSubdomains2D()` 1861d709ea83SDmitry Karpeev @*/ 18629371c9d4SSatish Balay PetscErrorCode PCGASMGetUseDMSubdomains(PC pc, PetscBool *flg) { 1863d709ea83SDmitry Karpeev PC_GASM *osm = (PC_GASM *)pc->data; 1864d709ea83SDmitry Karpeev PetscBool match; 1865d709ea83SDmitry Karpeev 1866d709ea83SDmitry Karpeev PetscFunctionBegin; 1867d709ea83SDmitry Karpeev PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 1868534a8f05SLisandro Dalcin PetscValidBoolPointer(flg, 2); 18699566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCGASM, &match)); 1870d709ea83SDmitry Karpeev if (match) { 1871d709ea83SDmitry Karpeev if (flg) *flg = osm->dm_subdomains; 1872d709ea83SDmitry Karpeev } 1873d709ea83SDmitry Karpeev PetscFunctionReturn(0); 1874d709ea83SDmitry Karpeev } 1875