1b1a0a8a3SJed Brown /* 2f746d493SDmitry Karpeev This file defines an "generalized" additive Schwarz preconditioner for any Mat implementation. 36a4f0f73SDmitry Karpeev In this version each processor may intersect multiple subdomains and any subdomain may 46a4f0f73SDmitry Karpeev intersect multiple processors. Intersections of subdomains with processors 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()) 86a4f0f73SDmitry Karpeev n - actual number of local subdomains on this processor (set in PCGASMSetSubdomains() or calculated in PCGASMSetTotalSubdomains()) 98f3b4b4dSDmitry Karpeev nmax - maximum number of local subdomains per processor (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)); 74*48a46eb9SPierre 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)); 99*48a46eb9SPierre 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 172*48a46eb9SPierre Jolivet if (osm->overlap >= 0) PetscCall(PetscSNPrintf(overlap, sizeof(overlap), "requested amount of overlap = %" PetscInt_FMT, osm->overlap)); 173*48a46eb9SPierre Jolivet if (osm->N != PETSC_DETERMINE) PetscCall(PetscSNPrintf(gsubdomains, sizeof(gsubdomains), "total number of subdomains = %" PetscInt_FMT, osm->N)); 174*48a46eb9SPierre 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); 25863a3b9bcSJacob Faibussowitsch 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 processors %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)); 267ea91fabdSFande Kong /* get new processor 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 { 3368f3b4b4dSDmitry Karpeev /* still no subdomains; use one per processor */ 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)); 4569371c9d4SSatish Balay 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)); 574*48a46eb9SPierre 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; 589*48a46eb9SPierre 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) { 792*48a46eb9SPierre 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 } 822*48a46eb9SPierre Jolivet if (osm->pctoouter) PetscCall(VecScatterDestroy(&(osm->pctoouter))); 823*48a46eb9SPierre Jolivet if (osm->permutationIS) PetscCall(ISDestroy(&(osm->permutationIS))); 824*48a46eb9SPierre Jolivet if (osm->pcx) PetscCall(VecDestroy(&(osm->pcx))); 825*48a46eb9SPierre Jolivet if (osm->pcy) PetscCall(VecDestroy(&(osm->pcy))); 826*48a46eb9SPierre Jolivet if (osm->permutationP) PetscCall(MatDestroy(&(osm->permutationP))); 827*48a46eb9SPierre 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) { 840*48a46eb9SPierre 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 /*@ 8818f3b4b4dSDmitry Karpeev PCGASMSetTotalSubdomains - sets the total number of subdomains to use across the 8828f3b4b4dSDmitry Karpeev communicator. 8838f3b4b4dSDmitry Karpeev Logically collective on pc 8848f3b4b4dSDmitry Karpeev 8858f3b4b4dSDmitry Karpeev Input Parameters: 8868f3b4b4dSDmitry Karpeev + pc - the preconditioner 8878f3b4b4dSDmitry Karpeev - N - total number of subdomains 8888f3b4b4dSDmitry Karpeev 8898f3b4b4dSDmitry Karpeev Level: beginner 8908f3b4b4dSDmitry Karpeev 891db781477SPatrick Sanan .seealso: `PCGASMSetSubdomains()`, `PCGASMSetOverlap()` 892db781477SPatrick Sanan `PCGASMCreateSubdomains2D()` 8938f3b4b4dSDmitry Karpeev @*/ 8949371c9d4SSatish Balay PetscErrorCode PCGASMSetTotalSubdomains(PC pc, PetscInt N) { 8958f3b4b4dSDmitry Karpeev PC_GASM *osm = (PC_GASM *)pc->data; 8968f3b4b4dSDmitry Karpeev PetscMPIInt size, rank; 8978f3b4b4dSDmitry Karpeev 8988f3b4b4dSDmitry Karpeev PetscFunctionBegin; 89963a3b9bcSJacob 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); 90028b400f6SJacob Faibussowitsch PetscCheck(!pc->setupcalled, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "PCGASMSetTotalSubdomains() should be called before calling PCSetUp()."); 9018f3b4b4dSDmitry Karpeev 9029566063dSJacob Faibussowitsch PetscCall(PCGASMDestroySubdomains(osm->n, &osm->iis, &osm->ois)); 9038f3b4b4dSDmitry Karpeev osm->ois = osm->iis = NULL; 9048f3b4b4dSDmitry Karpeev 9059566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size)); 9069566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)pc), &rank)); 9078f3b4b4dSDmitry Karpeev osm->N = N; 9088f3b4b4dSDmitry Karpeev osm->n = PETSC_DETERMINE; 9098f3b4b4dSDmitry Karpeev osm->nmax = PETSC_DETERMINE; 9108f3b4b4dSDmitry Karpeev osm->dm_subdomains = PETSC_FALSE; 9118f3b4b4dSDmitry Karpeev PetscFunctionReturn(0); 9128f3b4b4dSDmitry Karpeev } 9138f3b4b4dSDmitry Karpeev 9149371c9d4SSatish Balay static PetscErrorCode PCGASMSetSubdomains_GASM(PC pc, PetscInt n, IS iis[], IS ois[]) { 915f746d493SDmitry Karpeev PC_GASM *osm = (PC_GASM *)pc->data; 916b1a0a8a3SJed Brown PetscInt i; 917b1a0a8a3SJed Brown 918b1a0a8a3SJed Brown PetscFunctionBegin; 91963a3b9bcSJacob Faibussowitsch PetscCheck(n >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Each process must have 1 or more subdomains, got n = %" PetscInt_FMT, n); 92028b400f6SJacob Faibussowitsch PetscCheck(!pc->setupcalled, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "PCGASMSetSubdomains() should be called before calling PCSetUp()."); 921b1a0a8a3SJed Brown 9229566063dSJacob Faibussowitsch PetscCall(PCGASMDestroySubdomains(osm->n, &osm->iis, &osm->ois)); 9238f3b4b4dSDmitry Karpeev osm->iis = osm->ois = NULL; 9248f3b4b4dSDmitry Karpeev osm->n = n; 9258f3b4b4dSDmitry Karpeev osm->N = PETSC_DETERMINE; 9268f3b4b4dSDmitry Karpeev osm->nmax = PETSC_DETERMINE; 927a35b7b57SDmitry Karpeev if (ois) { 9289566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(n, &osm->ois)); 9298f3b4b4dSDmitry Karpeev for (i = 0; i < n; i++) { 9309566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)ois[i])); 9318f3b4b4dSDmitry Karpeev osm->ois[i] = ois[i]; 9328f3b4b4dSDmitry Karpeev } 9338f3b4b4dSDmitry Karpeev /* 9348f3b4b4dSDmitry Karpeev Since the user set the outer subdomains, even if nontrivial overlap was requested via PCGASMSetOverlap(), 9358f3b4b4dSDmitry Karpeev it will be ignored. To avoid confusion later on (e.g., when viewing the PC), the overlap size is set to -1. 9368f3b4b4dSDmitry Karpeev */ 937b1a0a8a3SJed Brown osm->overlap = -1; 938670417b2SFande Kong /* inner subdomains must be provided */ 93928b400f6SJacob Faibussowitsch PetscCheck(iis, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "inner indices have to be provided "); 940670417b2SFande Kong } /* end if */ 941a35b7b57SDmitry Karpeev if (iis) { 9429566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(n, &osm->iis)); 9438f3b4b4dSDmitry Karpeev for (i = 0; i < n; i++) { 9449566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)iis[i])); 9458f3b4b4dSDmitry Karpeev osm->iis[i] = iis[i]; 9468f3b4b4dSDmitry Karpeev } 947a35b7b57SDmitry Karpeev if (!ois) { 948390e1bf2SBarry Smith osm->ois = NULL; 949670417b2SFande Kong /* if user does not provide outer indices, we will create the corresponding outer indices using osm->overlap =1 in PCSetUp_GASM */ 950670417b2SFande Kong } 951670417b2SFande Kong } 95276bd3646SJed Brown if (PetscDefined(USE_DEBUG)) { 953670417b2SFande Kong PetscInt j, rstart, rend, *covered, lsize; 954670417b2SFande Kong const PetscInt *indices; 955670417b2SFande Kong /* check if the inner indices cover and only cover the local portion of the preconditioning matrix */ 9569566063dSJacob Faibussowitsch PetscCall(MatGetOwnershipRange(pc->pmat, &rstart, &rend)); 9579566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(rend - rstart, &covered)); 958670417b2SFande Kong /* check if the current processor owns indices from others */ 959a35b7b57SDmitry Karpeev for (i = 0; i < n; i++) { 9609566063dSJacob Faibussowitsch PetscCall(ISGetIndices(osm->iis[i], &indices)); 9619566063dSJacob Faibussowitsch PetscCall(ISGetLocalSize(osm->iis[i], &lsize)); 962670417b2SFande Kong for (j = 0; j < lsize; j++) { 9632472a847SBarry 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 processors", indices[j]); 9642472a847SBarry 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]); 96563a3b9bcSJacob Faibussowitsch covered[indices[j] - rstart] = 1; 966a35b7b57SDmitry Karpeev } 9679566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(osm->iis[i], &indices)); 9688f3b4b4dSDmitry Karpeev } 969670417b2SFande Kong /* check if we miss any indices */ 9709371c9d4SSatish Balay 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); } 9719566063dSJacob Faibussowitsch PetscCall(PetscFree(covered)); 972a35b7b57SDmitry Karpeev } 9738f3b4b4dSDmitry Karpeev if (iis) osm->user_subdomains = PETSC_TRUE; 974b1a0a8a3SJed Brown PetscFunctionReturn(0); 975b1a0a8a3SJed Brown } 976b1a0a8a3SJed Brown 9779371c9d4SSatish Balay static PetscErrorCode PCGASMSetOverlap_GASM(PC pc, PetscInt ovl) { 978f746d493SDmitry Karpeev PC_GASM *osm = (PC_GASM *)pc->data; 979b1a0a8a3SJed Brown 980b1a0a8a3SJed Brown PetscFunctionBegin; 98108401ef6SPierre Jolivet PetscCheck(ovl >= 0, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "Negative overlap value requested"); 9822472a847SBarry Smith PetscCheck(!pc->setupcalled || ovl == osm->overlap, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "PCGASMSetOverlap() should be called before PCSetUp()."); 9832fa5cd67SKarl Rupp if (!pc->setupcalled) osm->overlap = ovl; 984b1a0a8a3SJed Brown PetscFunctionReturn(0); 985b1a0a8a3SJed Brown } 986b1a0a8a3SJed Brown 9879371c9d4SSatish Balay static PetscErrorCode PCGASMSetType_GASM(PC pc, PCGASMType type) { 988f746d493SDmitry Karpeev PC_GASM *osm = (PC_GASM *)pc->data; 989b1a0a8a3SJed Brown 990b1a0a8a3SJed Brown PetscFunctionBegin; 991b1a0a8a3SJed Brown osm->type = type; 992b1a0a8a3SJed Brown osm->type_set = PETSC_TRUE; 993b1a0a8a3SJed Brown PetscFunctionReturn(0); 994b1a0a8a3SJed Brown } 995b1a0a8a3SJed Brown 9969371c9d4SSatish Balay static PetscErrorCode PCGASMSetSortIndices_GASM(PC pc, PetscBool doSort) { 997f746d493SDmitry Karpeev PC_GASM *osm = (PC_GASM *)pc->data; 998b1a0a8a3SJed Brown 999b1a0a8a3SJed Brown PetscFunctionBegin; 1000b1a0a8a3SJed Brown osm->sort_indices = doSort; 1001b1a0a8a3SJed Brown PetscFunctionReturn(0); 1002b1a0a8a3SJed Brown } 1003b1a0a8a3SJed Brown 100444fe18e5SDmitry Karpeev /* 10058f3b4b4dSDmitry Karpeev FIXME: This routine might need to be modified now that multiple ranks per subdomain are allowed. 100644fe18e5SDmitry Karpeev In particular, it would upset the global subdomain number calculation. 100744fe18e5SDmitry Karpeev */ 10089371c9d4SSatish Balay static PetscErrorCode PCGASMGetSubKSP_GASM(PC pc, PetscInt *n, PetscInt *first, KSP **ksp) { 1009f746d493SDmitry Karpeev PC_GASM *osm = (PC_GASM *)pc->data; 1010b1a0a8a3SJed Brown 1011b1a0a8a3SJed Brown PetscFunctionBegin; 101208401ef6SPierre 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"); 1013b1a0a8a3SJed Brown 10142fa5cd67SKarl Rupp if (n) *n = osm->n; 1015ab3e923fSDmitry Karpeev if (first) { 10169566063dSJacob Faibussowitsch PetscCallMPI(MPI_Scan(&osm->n, first, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject)pc))); 1017ab3e923fSDmitry Karpeev *first -= osm->n; 1018b1a0a8a3SJed Brown } 1019b1a0a8a3SJed Brown if (ksp) { 1020b1a0a8a3SJed Brown /* Assume that local solves are now different; not necessarily 102106b43e7eSDmitry Karpeev true, though! This flag is used only for PCView_GASM() */ 1022b1a0a8a3SJed Brown *ksp = osm->ksp; 10236a4f0f73SDmitry Karpeev osm->same_subdomain_solvers = PETSC_FALSE; 1024b1a0a8a3SJed Brown } 1025b1a0a8a3SJed Brown PetscFunctionReturn(0); 1026ab3e923fSDmitry Karpeev } /* PCGASMGetSubKSP_GASM() */ 1027b1a0a8a3SJed Brown 1028b1a0a8a3SJed Brown /*@C 102906b43e7eSDmitry Karpeev PCGASMSetSubdomains - Sets the subdomains for this processor 103006b43e7eSDmitry Karpeev for the additive Schwarz preconditioner. 1031b1a0a8a3SJed Brown 10328f3b4b4dSDmitry Karpeev Collective on pc 1033b1a0a8a3SJed Brown 1034b1a0a8a3SJed Brown Input Parameters: 10358f3b4b4dSDmitry Karpeev + pc - the preconditioner object 103606b43e7eSDmitry Karpeev . n - the number of subdomains for this processor 10378f3b4b4dSDmitry Karpeev . iis - the index sets that define the inner subdomains (or NULL for PETSc to determine subdomains) 10388f3b4b4dSDmitry 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) 1039b1a0a8a3SJed Brown 1040b1a0a8a3SJed Brown Notes: 104106b43e7eSDmitry Karpeev The IS indices use the parallel, global numbering of the vector entries. 10426a4f0f73SDmitry Karpeev Inner subdomains are those where the correction is applied. 10436a4f0f73SDmitry Karpeev Outer subdomains are those where the residual necessary to obtain the 10446a4f0f73SDmitry Karpeev corrections is obtained (see PCGASMType for the use of inner/outer subdomains). 10456a4f0f73SDmitry Karpeev Both inner and outer subdomains can extend over several processors. 10466a4f0f73SDmitry Karpeev This processor's portion of a subdomain is known as a local subdomain. 10476a4f0f73SDmitry Karpeev 1048670417b2SFande Kong Inner subdomains can not overlap with each other, do not have any entities from remote processors, 1049670417b2SFande Kong and have to cover the entire local subdomain owned by the current processor. The index sets on each 1050670417b2SFande Kong process should be ordered such that the ith local subdomain is connected to the ith remote subdomain 1051670417b2SFande Kong on another MPI process. 1052670417b2SFande Kong 10536a4f0f73SDmitry Karpeev By default the GASM preconditioner uses 1 (local) subdomain per processor. 10546a4f0f73SDmitry Karpeev 1055b1a0a8a3SJed Brown Level: advanced 1056b1a0a8a3SJed Brown 1057db781477SPatrick Sanan .seealso: `PCGASMSetOverlap()`, `PCGASMGetSubKSP()`, 1058db781477SPatrick Sanan `PCGASMCreateSubdomains2D()`, `PCGASMGetSubdomains()` 1059b1a0a8a3SJed Brown @*/ 10609371c9d4SSatish Balay PetscErrorCode PCGASMSetSubdomains(PC pc, PetscInt n, IS iis[], IS ois[]) { 10618f3b4b4dSDmitry Karpeev PC_GASM *osm = (PC_GASM *)pc->data; 1062b1a0a8a3SJed Brown 1063b1a0a8a3SJed Brown PetscFunctionBegin; 1064b1a0a8a3SJed Brown PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 1065cac4c232SBarry Smith PetscTryMethod(pc, "PCGASMSetSubdomains_C", (PC, PetscInt, IS[], IS[]), (pc, n, iis, ois)); 10668f3b4b4dSDmitry Karpeev osm->dm_subdomains = PETSC_FALSE; 1067b1a0a8a3SJed Brown PetscFunctionReturn(0); 1068b1a0a8a3SJed Brown } 1069b1a0a8a3SJed Brown 1070b1a0a8a3SJed Brown /*@ 1071f746d493SDmitry Karpeev PCGASMSetOverlap - Sets the overlap between a pair of subdomains for the 1072b1a0a8a3SJed Brown additive Schwarz preconditioner. Either all or no processors in the 10738f3b4b4dSDmitry Karpeev pc communicator must call this routine. 1074b1a0a8a3SJed Brown 10758f3b4b4dSDmitry Karpeev Logically Collective on pc 1076b1a0a8a3SJed Brown 1077b1a0a8a3SJed Brown Input Parameters: 1078b1a0a8a3SJed Brown + pc - the preconditioner context 10798f3b4b4dSDmitry Karpeev - ovl - the amount of overlap between subdomains (ovl >= 0, default value = 0) 1080b1a0a8a3SJed Brown 1081b1a0a8a3SJed Brown Options Database Key: 108206b43e7eSDmitry Karpeev . -pc_gasm_overlap <overlap> - Sets overlap 1083b1a0a8a3SJed Brown 1084b1a0a8a3SJed Brown Notes: 108506b43e7eSDmitry Karpeev By default the GASM preconditioner uses 1 subdomain per processor. To use 10868f3b4b4dSDmitry Karpeev multiple subdomain per perocessor or "straddling" subdomains that intersect 10878f3b4b4dSDmitry Karpeev multiple processors use PCGASMSetSubdomains() (or option -pc_gasm_total_subdomains <n>). 1088b1a0a8a3SJed Brown 10898f3b4b4dSDmitry Karpeev The overlap defaults to 0, so if one desires that no additional 1090b1a0a8a3SJed Brown overlap be computed beyond what may have been set with a call to 10918f3b4b4dSDmitry Karpeev PCGASMSetSubdomains(), then ovl must be set to be 0. In particular, if one does 10928f3b4b4dSDmitry Karpeev not explicitly set the subdomains in application code, then all overlap would be computed 1093f746d493SDmitry Karpeev internally by PETSc, and using an overlap of 0 would result in an GASM 1094b1a0a8a3SJed Brown variant that is equivalent to the block Jacobi preconditioner. 1095b1a0a8a3SJed Brown 1096b1a0a8a3SJed Brown Note that one can define initial index sets with any overlap via 109706b43e7eSDmitry Karpeev PCGASMSetSubdomains(); the routine PCGASMSetOverlap() merely allows 109806b43e7eSDmitry Karpeev PETSc to extend that overlap further, if desired. 1099b1a0a8a3SJed Brown 1100b1a0a8a3SJed Brown Level: intermediate 1101b1a0a8a3SJed Brown 1102db781477SPatrick Sanan .seealso: `PCGASMSetSubdomains()`, `PCGASMGetSubKSP()`, 1103db781477SPatrick Sanan `PCGASMCreateSubdomains2D()`, `PCGASMGetSubdomains()` 1104b1a0a8a3SJed Brown @*/ 11059371c9d4SSatish Balay PetscErrorCode PCGASMSetOverlap(PC pc, PetscInt ovl) { 11068f3b4b4dSDmitry Karpeev PC_GASM *osm = (PC_GASM *)pc->data; 1107b1a0a8a3SJed Brown 1108b1a0a8a3SJed Brown PetscFunctionBegin; 1109b1a0a8a3SJed Brown PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 1110b1a0a8a3SJed Brown PetscValidLogicalCollectiveInt(pc, ovl, 2); 1111cac4c232SBarry Smith PetscTryMethod(pc, "PCGASMSetOverlap_C", (PC, PetscInt), (pc, ovl)); 11128f3b4b4dSDmitry Karpeev osm->dm_subdomains = PETSC_FALSE; 1113b1a0a8a3SJed Brown PetscFunctionReturn(0); 1114b1a0a8a3SJed Brown } 1115b1a0a8a3SJed Brown 1116b1a0a8a3SJed Brown /*@ 1117f746d493SDmitry Karpeev PCGASMSetType - Sets the type of restriction and interpolation used 1118b1a0a8a3SJed Brown for local problems in the additive Schwarz method. 1119b1a0a8a3SJed Brown 1120b1a0a8a3SJed Brown Logically Collective on PC 1121b1a0a8a3SJed Brown 1122b1a0a8a3SJed Brown Input Parameters: 1123b1a0a8a3SJed Brown + pc - the preconditioner context 1124f746d493SDmitry Karpeev - type - variant of GASM, one of 1125b1a0a8a3SJed Brown .vb 1126f746d493SDmitry Karpeev PC_GASM_BASIC - full interpolation and restriction 1127f746d493SDmitry Karpeev PC_GASM_RESTRICT - full restriction, local processor interpolation 1128f746d493SDmitry Karpeev PC_GASM_INTERPOLATE - full interpolation, local processor restriction 1129f746d493SDmitry Karpeev PC_GASM_NONE - local processor restriction and interpolation 1130b1a0a8a3SJed Brown .ve 1131b1a0a8a3SJed Brown 1132b1a0a8a3SJed Brown Options Database Key: 1133f746d493SDmitry Karpeev . -pc_gasm_type [basic,restrict,interpolate,none] - Sets GASM type 1134b1a0a8a3SJed Brown 1135b1a0a8a3SJed Brown Level: intermediate 1136b1a0a8a3SJed Brown 1137db781477SPatrick Sanan .seealso: `PCGASMSetSubdomains()`, `PCGASMGetSubKSP()`, 1138db781477SPatrick Sanan `PCGASMCreateSubdomains2D()` 1139b1a0a8a3SJed Brown @*/ 11409371c9d4SSatish Balay PetscErrorCode PCGASMSetType(PC pc, PCGASMType type) { 1141b1a0a8a3SJed Brown PetscFunctionBegin; 1142b1a0a8a3SJed Brown PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 1143b1a0a8a3SJed Brown PetscValidLogicalCollectiveEnum(pc, type, 2); 1144cac4c232SBarry Smith PetscTryMethod(pc, "PCGASMSetType_C", (PC, PCGASMType), (pc, type)); 1145b1a0a8a3SJed Brown PetscFunctionReturn(0); 1146b1a0a8a3SJed Brown } 1147b1a0a8a3SJed Brown 1148b1a0a8a3SJed Brown /*@ 1149f746d493SDmitry Karpeev PCGASMSetSortIndices - Determines whether subdomain indices are sorted. 1150b1a0a8a3SJed Brown 1151b1a0a8a3SJed Brown Logically Collective on PC 1152b1a0a8a3SJed Brown 1153b1a0a8a3SJed Brown Input Parameters: 1154b1a0a8a3SJed Brown + pc - the preconditioner context 1155b1a0a8a3SJed Brown - doSort - sort the subdomain indices 1156b1a0a8a3SJed Brown 1157b1a0a8a3SJed Brown Level: intermediate 1158b1a0a8a3SJed Brown 1159db781477SPatrick Sanan .seealso: `PCGASMSetSubdomains()`, `PCGASMGetSubKSP()`, 1160db781477SPatrick Sanan `PCGASMCreateSubdomains2D()` 1161b1a0a8a3SJed Brown @*/ 11629371c9d4SSatish Balay PetscErrorCode PCGASMSetSortIndices(PC pc, PetscBool doSort) { 1163b1a0a8a3SJed Brown PetscFunctionBegin; 1164b1a0a8a3SJed Brown PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 1165b1a0a8a3SJed Brown PetscValidLogicalCollectiveBool(pc, doSort, 2); 1166cac4c232SBarry Smith PetscTryMethod(pc, "PCGASMSetSortIndices_C", (PC, PetscBool), (pc, doSort)); 1167b1a0a8a3SJed Brown PetscFunctionReturn(0); 1168b1a0a8a3SJed Brown } 1169b1a0a8a3SJed Brown 1170b1a0a8a3SJed Brown /*@C 1171f746d493SDmitry Karpeev PCGASMGetSubKSP - Gets the local KSP contexts for all blocks on 1172b1a0a8a3SJed Brown this processor. 1173b1a0a8a3SJed Brown 1174b1a0a8a3SJed Brown Collective on PC iff first_local is requested 1175b1a0a8a3SJed Brown 1176b1a0a8a3SJed Brown Input Parameter: 1177b1a0a8a3SJed Brown . pc - the preconditioner context 1178b1a0a8a3SJed Brown 1179b1a0a8a3SJed Brown Output Parameters: 11800298fd71SBarry Smith + n_local - the number of blocks on this processor or NULL 11810298fd71SBarry Smith . first_local - the global number of the first block on this processor or NULL, 11820298fd71SBarry Smith all processors must request or all must pass NULL 1183b1a0a8a3SJed Brown - ksp - the array of KSP contexts 1184b1a0a8a3SJed Brown 1185b1a0a8a3SJed Brown Note: 1186f746d493SDmitry Karpeev After PCGASMGetSubKSP() the array of KSPes is not to be freed 1187b1a0a8a3SJed Brown 1188b1a0a8a3SJed Brown Currently for some matrix implementations only 1 block per processor 1189b1a0a8a3SJed Brown is supported. 1190b1a0a8a3SJed Brown 1191f746d493SDmitry Karpeev You must call KSPSetUp() before calling PCGASMGetSubKSP(). 1192b1a0a8a3SJed Brown 1193b1a0a8a3SJed Brown Level: advanced 1194b1a0a8a3SJed Brown 1195db781477SPatrick Sanan .seealso: `PCGASMSetSubdomains()`, `PCGASMSetOverlap()`, 1196db781477SPatrick Sanan `PCGASMCreateSubdomains2D()`, 1197b1a0a8a3SJed Brown @*/ 11989371c9d4SSatish Balay PetscErrorCode PCGASMGetSubKSP(PC pc, PetscInt *n_local, PetscInt *first_local, KSP *ksp[]) { 1199b1a0a8a3SJed Brown PetscFunctionBegin; 1200b1a0a8a3SJed Brown PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 1201cac4c232SBarry Smith PetscUseMethod(pc, "PCGASMGetSubKSP_C", (PC, PetscInt *, PetscInt *, KSP **), (pc, n_local, first_local, ksp)); 1202b1a0a8a3SJed Brown PetscFunctionReturn(0); 1203b1a0a8a3SJed Brown } 1204b1a0a8a3SJed Brown 1205b1a0a8a3SJed Brown /* -------------------------------------------------------------------------------------*/ 1206b1a0a8a3SJed Brown /*MC 1207f746d493SDmitry Karpeev PCGASM - Use the (restricted) additive Schwarz method, each block is (approximately) solved with 1208b1a0a8a3SJed Brown its own KSP object. 1209b1a0a8a3SJed Brown 1210b1a0a8a3SJed Brown Options Database Keys: 12118f3b4b4dSDmitry Karpeev + -pc_gasm_total_subdomains <n> - Sets total number of local subdomains to be distributed among processors 121206b43e7eSDmitry Karpeev . -pc_gasm_view_subdomains - activates the printing of subdomain indices in PCView(), -ksp_view or -snes_view 121306b43e7eSDmitry Karpeev . -pc_gasm_print_subdomains - activates the printing of subdomain indices in PCSetUp() 121406b43e7eSDmitry Karpeev . -pc_gasm_overlap <ovl> - Sets overlap by which to (automatically) extend local subdomains 1215f746d493SDmitry Karpeev - -pc_gasm_type [basic,restrict,interpolate,none] - Sets GASM type 1216b1a0a8a3SJed Brown 1217b1a0a8a3SJed Brown IMPORTANT: If you run with, for example, 3 blocks on 1 processor or 3 blocks on 3 processors you 1218f746d493SDmitry Karpeev will get a different convergence rate due to the default option of -pc_gasm_type restrict. Use 1219f746d493SDmitry Karpeev -pc_gasm_type basic to use the standard GASM. 1220b1a0a8a3SJed Brown 122195452b02SPatrick Sanan Notes: 122295452b02SPatrick Sanan Blocks can be shared by multiple processes. 1223b1a0a8a3SJed Brown 1224b1a0a8a3SJed Brown To set options on the solvers for each block append -sub_ to all the KSP, and PC 1225b1a0a8a3SJed Brown options database keys. For example, -sub_pc_type ilu -sub_pc_factor_levels 1 -sub_ksp_type preonly 1226b1a0a8a3SJed Brown 1227f746d493SDmitry Karpeev To set the options on the solvers separate for each block call PCGASMGetSubKSP() 1228b1a0a8a3SJed Brown and set the options directly on the resulting KSP object (you can access its PC 1229b1a0a8a3SJed Brown with KSPGetPC()) 1230b1a0a8a3SJed Brown 1231b1a0a8a3SJed Brown Level: beginner 1232b1a0a8a3SJed Brown 1233b1a0a8a3SJed Brown References: 1234606c0280SSatish Balay + * - M Dryja, OB Widlund, An additive variant of the Schwarz alternating method for the case of many subregions 123596a0c994SBarry Smith Courant Institute, New York University Technical report 1236606c0280SSatish Balay - * - Barry Smith, Petter Bjorstad, and William Gropp, Domain Decompositions: Parallel Multilevel Methods for Elliptic Partial Differential Equations, 123796a0c994SBarry Smith Cambridge University Press. 1238b1a0a8a3SJed Brown 1239db781477SPatrick Sanan .seealso: `PCCreate()`, `PCSetType()`, `PCType`, `PC`, 1240db781477SPatrick Sanan `PCBJACOBI`, `PCGASMGetSubKSP()`, `PCGASMSetSubdomains()`, 1241db781477SPatrick Sanan `PCSetModifySubMatrices()`, `PCGASMSetOverlap()`, `PCGASMSetType()` 1242b1a0a8a3SJed Brown 1243b1a0a8a3SJed Brown M*/ 1244b1a0a8a3SJed Brown 12459371c9d4SSatish Balay PETSC_EXTERN PetscErrorCode PCCreate_GASM(PC pc) { 1246f746d493SDmitry Karpeev PC_GASM *osm; 1247b1a0a8a3SJed Brown 1248b1a0a8a3SJed Brown PetscFunctionBegin; 12499566063dSJacob Faibussowitsch PetscCall(PetscNewLog(pc, &osm)); 12502fa5cd67SKarl Rupp 12518f3b4b4dSDmitry Karpeev osm->N = PETSC_DETERMINE; 125206b43e7eSDmitry Karpeev osm->n = PETSC_DECIDE; 12538f3b4b4dSDmitry Karpeev osm->nmax = PETSC_DETERMINE; 12548f3b4b4dSDmitry Karpeev osm->overlap = 0; 12550a545947SLisandro Dalcin osm->ksp = NULL; 12560a545947SLisandro Dalcin osm->gorestriction = NULL; 12570a545947SLisandro Dalcin osm->girestriction = NULL; 12580a545947SLisandro Dalcin osm->pctoouter = NULL; 12590a545947SLisandro Dalcin osm->gx = NULL; 12600a545947SLisandro Dalcin osm->gy = NULL; 12610a545947SLisandro Dalcin osm->x = NULL; 12620a545947SLisandro Dalcin osm->y = NULL; 12630a545947SLisandro Dalcin osm->pcx = NULL; 12640a545947SLisandro Dalcin osm->pcy = NULL; 12650a545947SLisandro Dalcin osm->permutationIS = NULL; 12660a545947SLisandro Dalcin osm->permutationP = NULL; 12670a545947SLisandro Dalcin osm->pcmat = NULL; 12680a545947SLisandro Dalcin osm->ois = NULL; 12690a545947SLisandro Dalcin osm->iis = NULL; 12700a545947SLisandro Dalcin osm->pmat = NULL; 1271f746d493SDmitry Karpeev osm->type = PC_GASM_RESTRICT; 12726a4f0f73SDmitry Karpeev osm->same_subdomain_solvers = PETSC_TRUE; 1273b1a0a8a3SJed Brown osm->sort_indices = PETSC_TRUE; 1274d709ea83SDmitry Karpeev osm->dm_subdomains = PETSC_FALSE; 1275ea91fabdSFande Kong osm->hierarchicalpartitioning = PETSC_FALSE; 1276b1a0a8a3SJed Brown 1277b1a0a8a3SJed Brown pc->data = (void *)osm; 1278f746d493SDmitry Karpeev pc->ops->apply = PCApply_GASM; 127948e38a8aSPierre Jolivet pc->ops->matapply = PCMatApply_GASM; 1280f746d493SDmitry Karpeev pc->ops->applytranspose = PCApplyTranspose_GASM; 1281f746d493SDmitry Karpeev pc->ops->setup = PCSetUp_GASM; 1282a06653b4SBarry Smith pc->ops->reset = PCReset_GASM; 1283f746d493SDmitry Karpeev pc->ops->destroy = PCDestroy_GASM; 1284f746d493SDmitry Karpeev pc->ops->setfromoptions = PCSetFromOptions_GASM; 1285f746d493SDmitry Karpeev pc->ops->setuponblocks = PCSetUpOnBlocks_GASM; 1286f746d493SDmitry Karpeev pc->ops->view = PCView_GASM; 12870a545947SLisandro Dalcin pc->ops->applyrichardson = NULL; 1288b1a0a8a3SJed Brown 12899566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetSubdomains_C", PCGASMSetSubdomains_GASM)); 12909566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetOverlap_C", PCGASMSetOverlap_GASM)); 12919566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetType_C", PCGASMSetType_GASM)); 12929566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMSetSortIndices_C", PCGASMSetSortIndices_GASM)); 12939566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGASMGetSubKSP_C", PCGASMGetSubKSP_GASM)); 1294b1a0a8a3SJed Brown PetscFunctionReturn(0); 1295b1a0a8a3SJed Brown } 1296b1a0a8a3SJed Brown 12979371c9d4SSatish Balay PetscErrorCode PCGASMCreateLocalSubdomains(Mat A, PetscInt nloc, IS *iis[]) { 1298b1a0a8a3SJed Brown MatPartitioning mpart; 1299b1a0a8a3SJed Brown const char *prefix; 1300b1a0a8a3SJed Brown PetscInt i, j, rstart, rend, bs; 1301976e8c5aSLisandro Dalcin PetscBool hasop, isbaij = PETSC_FALSE, foundpart = PETSC_FALSE; 13020298fd71SBarry Smith Mat Ad = NULL, adj; 1303b1a0a8a3SJed Brown IS ispart, isnumb, *is; 1304b1a0a8a3SJed Brown 1305b1a0a8a3SJed Brown PetscFunctionBegin; 130663a3b9bcSJacob Faibussowitsch PetscCheck(nloc >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "number of local subdomains must > 0, got nloc = %" PetscInt_FMT, nloc); 1307b1a0a8a3SJed Brown 1308b1a0a8a3SJed Brown /* Get prefix, row distribution, and block size */ 13099566063dSJacob Faibussowitsch PetscCall(MatGetOptionsPrefix(A, &prefix)); 13109566063dSJacob Faibussowitsch PetscCall(MatGetOwnershipRange(A, &rstart, &rend)); 13119566063dSJacob Faibussowitsch PetscCall(MatGetBlockSize(A, &bs)); 13122472a847SBarry 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); 1313b1a0a8a3SJed Brown 1314b1a0a8a3SJed Brown /* Get diagonal block from matrix if possible */ 13159566063dSJacob Faibussowitsch PetscCall(MatHasOperation(A, MATOP_GET_DIAGONAL_BLOCK, &hasop)); 1316*48a46eb9SPierre Jolivet if (hasop) PetscCall(MatGetDiagonalBlock(A, &Ad)); 1317b1a0a8a3SJed Brown if (Ad) { 13189566063dSJacob Faibussowitsch PetscCall(PetscObjectBaseTypeCompare((PetscObject)Ad, MATSEQBAIJ, &isbaij)); 13199566063dSJacob Faibussowitsch if (!isbaij) PetscCall(PetscObjectBaseTypeCompare((PetscObject)Ad, MATSEQSBAIJ, &isbaij)); 1320b1a0a8a3SJed Brown } 13218f3b4b4dSDmitry Karpeev if (Ad && nloc > 1) { 1322b1a0a8a3SJed Brown PetscBool match, done; 1323b1a0a8a3SJed Brown /* Try to setup a good matrix partitioning if available */ 13249566063dSJacob Faibussowitsch PetscCall(MatPartitioningCreate(PETSC_COMM_SELF, &mpart)); 13259566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptionsPrefix((PetscObject)mpart, prefix)); 13269566063dSJacob Faibussowitsch PetscCall(MatPartitioningSetFromOptions(mpart)); 13279566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)mpart, MATPARTITIONINGCURRENT, &match)); 1328*48a46eb9SPierre Jolivet if (!match) PetscCall(PetscObjectTypeCompare((PetscObject)mpart, MATPARTITIONINGSQUARE, &match)); 1329b1a0a8a3SJed Brown if (!match) { /* assume a "good" partitioner is available */ 13301a83f524SJed Brown PetscInt na; 13311a83f524SJed Brown const PetscInt *ia, *ja; 13329566063dSJacob Faibussowitsch PetscCall(MatGetRowIJ(Ad, 0, PETSC_TRUE, isbaij, &na, &ia, &ja, &done)); 1333b1a0a8a3SJed Brown if (done) { 1334b1a0a8a3SJed Brown /* Build adjacency matrix by hand. Unfortunately a call to 1335b1a0a8a3SJed Brown MatConvert(Ad,MATMPIADJ,MAT_INITIAL_MATRIX,&adj) will 1336b1a0a8a3SJed Brown remove the block-aij structure and we cannot expect 1337b1a0a8a3SJed Brown MatPartitioning to split vertices as we need */ 13380a545947SLisandro Dalcin PetscInt i, j, len, nnz, cnt, *iia = NULL, *jja = NULL; 13391a83f524SJed Brown const PetscInt *row; 1340b1a0a8a3SJed Brown nnz = 0; 1341b1a0a8a3SJed Brown for (i = 0; i < na; i++) { /* count number of nonzeros */ 1342b1a0a8a3SJed Brown len = ia[i + 1] - ia[i]; 1343b1a0a8a3SJed Brown row = ja + ia[i]; 1344b1a0a8a3SJed Brown for (j = 0; j < len; j++) { 1345b1a0a8a3SJed Brown if (row[j] == i) { /* don't count diagonal */ 13469371c9d4SSatish Balay len--; 13479371c9d4SSatish Balay break; 1348b1a0a8a3SJed Brown } 1349b1a0a8a3SJed Brown } 1350b1a0a8a3SJed Brown nnz += len; 1351b1a0a8a3SJed Brown } 13529566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(na + 1, &iia)); 13539566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nnz, &jja)); 1354b1a0a8a3SJed Brown nnz = 0; 1355b1a0a8a3SJed Brown iia[0] = 0; 1356b1a0a8a3SJed Brown for (i = 0; i < na; i++) { /* fill adjacency */ 1357b1a0a8a3SJed Brown cnt = 0; 1358b1a0a8a3SJed Brown len = ia[i + 1] - ia[i]; 1359b1a0a8a3SJed Brown row = ja + ia[i]; 1360b1a0a8a3SJed Brown for (j = 0; j < len; j++) { 13612fa5cd67SKarl Rupp if (row[j] != i) jja[nnz + cnt++] = row[j]; /* if not diagonal */ 1362b1a0a8a3SJed Brown } 1363b1a0a8a3SJed Brown nnz += cnt; 1364b1a0a8a3SJed Brown iia[i + 1] = nnz; 1365b1a0a8a3SJed Brown } 1366b1a0a8a3SJed Brown /* Partitioning of the adjacency matrix */ 13679566063dSJacob Faibussowitsch PetscCall(MatCreateMPIAdj(PETSC_COMM_SELF, na, na, iia, jja, NULL, &adj)); 13689566063dSJacob Faibussowitsch PetscCall(MatPartitioningSetAdjacency(mpart, adj)); 13699566063dSJacob Faibussowitsch PetscCall(MatPartitioningSetNParts(mpart, nloc)); 13709566063dSJacob Faibussowitsch PetscCall(MatPartitioningApply(mpart, &ispart)); 13719566063dSJacob Faibussowitsch PetscCall(ISPartitioningToNumbering(ispart, &isnumb)); 13729566063dSJacob Faibussowitsch PetscCall(MatDestroy(&adj)); 1373b1a0a8a3SJed Brown foundpart = PETSC_TRUE; 1374b1a0a8a3SJed Brown } 13759566063dSJacob Faibussowitsch PetscCall(MatRestoreRowIJ(Ad, 0, PETSC_TRUE, isbaij, &na, &ia, &ja, &done)); 1376b1a0a8a3SJed Brown } 13779566063dSJacob Faibussowitsch PetscCall(MatPartitioningDestroy(&mpart)); 1378b1a0a8a3SJed Brown } 13799566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nloc, &is)); 1380b1a0a8a3SJed Brown if (!foundpart) { 1381b1a0a8a3SJed Brown /* Partitioning by contiguous chunks of rows */ 1382b1a0a8a3SJed Brown 1383b1a0a8a3SJed Brown PetscInt mbs = (rend - rstart) / bs; 1384b1a0a8a3SJed Brown PetscInt start = rstart; 13858f3b4b4dSDmitry Karpeev for (i = 0; i < nloc; i++) { 13868f3b4b4dSDmitry Karpeev PetscInt count = (mbs / nloc + ((mbs % nloc) > i)) * bs; 13879566063dSJacob Faibussowitsch PetscCall(ISCreateStride(PETSC_COMM_SELF, count, start, 1, &is[i])); 1388b1a0a8a3SJed Brown start += count; 1389b1a0a8a3SJed Brown } 1390b1a0a8a3SJed Brown 1391b1a0a8a3SJed Brown } else { 1392b1a0a8a3SJed Brown /* Partitioning by adjacency of diagonal block */ 1393b1a0a8a3SJed Brown 1394b1a0a8a3SJed Brown const PetscInt *numbering; 1395b1a0a8a3SJed Brown PetscInt *count, nidx, *indices, *newidx, start = 0; 1396b1a0a8a3SJed Brown /* Get node count in each partition */ 13979566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nloc, &count)); 13989566063dSJacob Faibussowitsch PetscCall(ISPartitioningCount(ispart, nloc, count)); 1399b1a0a8a3SJed Brown if (isbaij && bs > 1) { /* adjust for the block-aij case */ 14008f3b4b4dSDmitry Karpeev for (i = 0; i < nloc; i++) count[i] *= bs; 1401b1a0a8a3SJed Brown } 1402b1a0a8a3SJed Brown /* Build indices from node numbering */ 14039566063dSJacob Faibussowitsch PetscCall(ISGetLocalSize(isnumb, &nidx)); 14049566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nidx, &indices)); 1405b1a0a8a3SJed Brown for (i = 0; i < nidx; i++) indices[i] = i; /* needs to be initialized */ 14069566063dSJacob Faibussowitsch PetscCall(ISGetIndices(isnumb, &numbering)); 14079566063dSJacob Faibussowitsch PetscCall(PetscSortIntWithPermutation(nidx, numbering, indices)); 14089566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(isnumb, &numbering)); 1409b1a0a8a3SJed Brown if (isbaij && bs > 1) { /* adjust for the block-aij case */ 14109566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nidx * bs, &newidx)); 14112fa5cd67SKarl Rupp for (i = 0; i < nidx; i++) { 14122fa5cd67SKarl Rupp for (j = 0; j < bs; j++) newidx[i * bs + j] = indices[i] * bs + j; 14132fa5cd67SKarl Rupp } 14149566063dSJacob Faibussowitsch PetscCall(PetscFree(indices)); 1415b1a0a8a3SJed Brown nidx *= bs; 1416b1a0a8a3SJed Brown indices = newidx; 1417b1a0a8a3SJed Brown } 1418b1a0a8a3SJed Brown /* Shift to get global indices */ 1419b1a0a8a3SJed Brown for (i = 0; i < nidx; i++) indices[i] += rstart; 1420b1a0a8a3SJed Brown 1421b1a0a8a3SJed Brown /* Build the index sets for each block */ 14228f3b4b4dSDmitry Karpeev for (i = 0; i < nloc; i++) { 14239566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, count[i], &indices[start], PETSC_COPY_VALUES, &is[i])); 14249566063dSJacob Faibussowitsch PetscCall(ISSort(is[i])); 1425b1a0a8a3SJed Brown start += count[i]; 1426b1a0a8a3SJed Brown } 1427b1a0a8a3SJed Brown 14289566063dSJacob Faibussowitsch PetscCall(PetscFree(count)); 14299566063dSJacob Faibussowitsch PetscCall(PetscFree(indices)); 14309566063dSJacob Faibussowitsch PetscCall(ISDestroy(&isnumb)); 14319566063dSJacob Faibussowitsch PetscCall(ISDestroy(&ispart)); 1432b1a0a8a3SJed Brown } 14336a4f0f73SDmitry Karpeev *iis = is; 14348f3b4b4dSDmitry Karpeev PetscFunctionReturn(0); 14358f3b4b4dSDmitry Karpeev } 14368f3b4b4dSDmitry Karpeev 14379371c9d4SSatish Balay PETSC_INTERN PetscErrorCode PCGASMCreateStraddlingSubdomains(Mat A, PetscInt N, PetscInt *n, IS *iis[]) { 14388f3b4b4dSDmitry Karpeev PetscFunctionBegin; 14399566063dSJacob Faibussowitsch PetscCall(MatSubdomainsCreateCoalesce(A, N, n, iis)); 14408f3b4b4dSDmitry Karpeev PetscFunctionReturn(0); 14418f3b4b4dSDmitry Karpeev } 14428f3b4b4dSDmitry Karpeev 14438f3b4b4dSDmitry Karpeev /*@C 14448f3b4b4dSDmitry Karpeev PCGASMCreateSubdomains - Creates n index sets defining n nonoverlapping subdomains for the additive 14458f3b4b4dSDmitry Karpeev Schwarz preconditioner for a any problem based on its matrix. 14468f3b4b4dSDmitry Karpeev 14478f3b4b4dSDmitry Karpeev Collective 14488f3b4b4dSDmitry Karpeev 14498f3b4b4dSDmitry Karpeev Input Parameters: 14508f3b4b4dSDmitry Karpeev + A - The global matrix operator 14518f3b4b4dSDmitry Karpeev - N - the number of global subdomains requested 14528f3b4b4dSDmitry Karpeev 14538f3b4b4dSDmitry Karpeev Output Parameters: 14548f3b4b4dSDmitry Karpeev + n - the number of subdomains created on this processor 14558f3b4b4dSDmitry Karpeev - iis - the array of index sets defining the local inner subdomains (on which the correction is applied) 14568f3b4b4dSDmitry Karpeev 14578f3b4b4dSDmitry Karpeev Level: advanced 14588f3b4b4dSDmitry Karpeev 14598f3b4b4dSDmitry Karpeev Note: When N >= A's communicator size, each subdomain is local -- contained within a single processor. 14608f3b4b4dSDmitry Karpeev When N < size, the subdomains are 'straddling' (processor boundaries) and are no longer local. 14618f3b4b4dSDmitry Karpeev The resulting subdomains can be use in PCGASMSetSubdomains(pc,n,iss,NULL). The overlapping 14628f3b4b4dSDmitry Karpeev outer subdomains will be automatically generated from these according to the requested amount of 14638f3b4b4dSDmitry Karpeev overlap; this is currently supported only with local subdomains. 14648f3b4b4dSDmitry Karpeev 1465db781477SPatrick Sanan .seealso: `PCGASMSetSubdomains()`, `PCGASMDestroySubdomains()` 14668f3b4b4dSDmitry Karpeev @*/ 14679371c9d4SSatish Balay PetscErrorCode PCGASMCreateSubdomains(Mat A, PetscInt N, PetscInt *n, IS *iis[]) { 14688f3b4b4dSDmitry Karpeev PetscMPIInt size; 14698f3b4b4dSDmitry Karpeev 14708f3b4b4dSDmitry Karpeev PetscFunctionBegin; 14718f3b4b4dSDmitry Karpeev PetscValidHeaderSpecific(A, MAT_CLASSID, 1); 14728f3b4b4dSDmitry Karpeev PetscValidPointer(iis, 4); 14738f3b4b4dSDmitry Karpeev 147463a3b9bcSJacob Faibussowitsch PetscCheck(N >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of subdomains must be > 0, N = %" PetscInt_FMT, N); 14759566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)A), &size)); 14768f3b4b4dSDmitry Karpeev if (N >= size) { 14778f3b4b4dSDmitry Karpeev *n = N / size + (N % size); 14789566063dSJacob Faibussowitsch PetscCall(PCGASMCreateLocalSubdomains(A, *n, iis)); 14796a4f0f73SDmitry Karpeev } else { 14809566063dSJacob Faibussowitsch PetscCall(PCGASMCreateStraddlingSubdomains(A, N, n, iis)); 14816a4f0f73SDmitry Karpeev } 1482b1a0a8a3SJed Brown PetscFunctionReturn(0); 1483b1a0a8a3SJed Brown } 1484b1a0a8a3SJed Brown 1485b1a0a8a3SJed Brown /*@C 1486f746d493SDmitry Karpeev PCGASMDestroySubdomains - Destroys the index sets created with 14878f3b4b4dSDmitry Karpeev PCGASMCreateSubdomains() or PCGASMCreateSubdomains2D. Should be 148806b43e7eSDmitry Karpeev called after setting subdomains with PCGASMSetSubdomains(). 1489b1a0a8a3SJed Brown 1490b1a0a8a3SJed Brown Collective 1491b1a0a8a3SJed Brown 1492b1a0a8a3SJed Brown Input Parameters: 1493b1a0a8a3SJed Brown + n - the number of index sets 14946a4f0f73SDmitry Karpeev . iis - the array of inner subdomains, 14950298fd71SBarry Smith - ois - the array of outer subdomains, can be NULL 1496b1a0a8a3SJed Brown 14976a4f0f73SDmitry Karpeev Level: intermediate 14986a4f0f73SDmitry Karpeev 149995452b02SPatrick Sanan Notes: 150095452b02SPatrick Sanan this is merely a convenience subroutine that walks each list, 15012c112581SDmitry Karpeev destroys each IS on the list, and then frees the list. At the end the 15022c112581SDmitry Karpeev list pointers are set to NULL. 1503b1a0a8a3SJed Brown 1504db781477SPatrick Sanan .seealso: `PCGASMCreateSubdomains()`, `PCGASMSetSubdomains()` 1505b1a0a8a3SJed Brown @*/ 15069371c9d4SSatish Balay PetscErrorCode PCGASMDestroySubdomains(PetscInt n, IS **iis, IS **ois) { 1507b1a0a8a3SJed Brown PetscInt i; 15085fd66863SKarl Rupp 1509b1a0a8a3SJed Brown PetscFunctionBegin; 1510a35b7b57SDmitry Karpeev if (n <= 0) PetscFunctionReturn(0); 15116a4f0f73SDmitry Karpeev if (ois) { 15122c112581SDmitry Karpeev PetscValidPointer(ois, 3); 15132c112581SDmitry Karpeev if (*ois) { 15142c112581SDmitry Karpeev PetscValidPointer(*ois, 3); 1515*48a46eb9SPierre Jolivet for (i = 0; i < n; i++) PetscCall(ISDestroy(&(*ois)[i])); 15169566063dSJacob Faibussowitsch PetscCall(PetscFree((*ois))); 15172c112581SDmitry Karpeev } 1518b1a0a8a3SJed Brown } 1519b9d0fdaaSFande Kong if (iis) { 1520b9d0fdaaSFande Kong PetscValidPointer(iis, 2); 1521b9d0fdaaSFande Kong if (*iis) { 1522b9d0fdaaSFande Kong PetscValidPointer(*iis, 2); 1523*48a46eb9SPierre Jolivet for (i = 0; i < n; i++) PetscCall(ISDestroy(&(*iis)[i])); 15249566063dSJacob Faibussowitsch PetscCall(PetscFree((*iis))); 1525b9d0fdaaSFande Kong } 1526b9d0fdaaSFande Kong } 1527b1a0a8a3SJed Brown PetscFunctionReturn(0); 1528b1a0a8a3SJed Brown } 1529b1a0a8a3SJed Brown 1530af538404SDmitry Karpeev #define PCGASMLocalSubdomainBounds2D(M, N, xleft, ylow, xright, yhigh, first, last, xleft_loc, ylow_loc, xright_loc, yhigh_loc, n) \ 1531af538404SDmitry Karpeev { \ 1532af538404SDmitry Karpeev PetscInt first_row = first / M, last_row = last / M + 1; \ 1533af538404SDmitry Karpeev /* \ 1534af538404SDmitry Karpeev Compute ylow_loc and yhigh_loc so that (ylow_loc,xleft) and (yhigh_loc,xright) are the corners \ 1535af538404SDmitry Karpeev of the bounding box of the intersection of the subdomain with the local ownership range (local \ 1536af538404SDmitry Karpeev subdomain). \ 1537af538404SDmitry Karpeev Also compute xleft_loc and xright_loc as the lower and upper bounds on the first and last rows \ 1538af538404SDmitry Karpeev of the intersection. \ 1539af538404SDmitry Karpeev */ \ 1540af538404SDmitry Karpeev /* ylow_loc is the grid row containing the first element of the local sumbdomain */ \ 1541eec7e3faSDmitry Karpeev *ylow_loc = PetscMax(first_row, ylow); \ 1542af538404SDmitry Karpeev /* xleft_loc is the offset of first element of the local subdomain within its grid row (might actually be outside the local subdomain) */ \ 1543eec7e3faSDmitry Karpeev *xleft_loc = *ylow_loc == first_row ? PetscMax(first % M, xleft) : xleft; \ 1544af538404SDmitry Karpeev /* yhigh_loc is the grid row above the last local subdomain element */ \ 1545eec7e3faSDmitry Karpeev *yhigh_loc = PetscMin(last_row, yhigh); \ 1546af538404SDmitry Karpeev /* xright is the offset of the end of the local subdomain within its grid row (might actually be outside the local subdomain) */ \ 1547eec7e3faSDmitry Karpeev *xright_loc = *yhigh_loc == last_row ? PetscMin(xright, last % M) : xright; \ 1548af538404SDmitry Karpeev /* Now compute the size of the local subdomain n. */ \ 1549c3518bceSDmitry Karpeev *n = 0; \ 1550eec7e3faSDmitry Karpeev if (*ylow_loc < *yhigh_loc) { \ 1551af538404SDmitry Karpeev PetscInt width = xright - xleft; \ 1552eec7e3faSDmitry Karpeev *n += width * (*yhigh_loc - *ylow_loc - 1); \ 1553eec7e3faSDmitry Karpeev *n += PetscMin(PetscMax(*xright_loc - xleft, 0), width); \ 1554eec7e3faSDmitry Karpeev *n -= PetscMin(PetscMax(*xleft_loc - xleft, 0), width); \ 1555af538404SDmitry Karpeev } \ 1556af538404SDmitry Karpeev } 1557af538404SDmitry Karpeev 1558b1a0a8a3SJed Brown /*@ 1559f746d493SDmitry Karpeev PCGASMCreateSubdomains2D - Creates the index sets for the overlapping Schwarz 1560b1a0a8a3SJed Brown preconditioner for a two-dimensional problem on a regular grid. 1561b1a0a8a3SJed Brown 1562af538404SDmitry Karpeev Collective 1563b1a0a8a3SJed Brown 1564b1a0a8a3SJed Brown Input Parameters: 15656b867d5aSJose E. Roman + pc - the preconditioner context 15666b867d5aSJose E. Roman . M - the global number of grid points in the x direction 15676b867d5aSJose E. Roman . N - the global number of grid points in the y direction 15686b867d5aSJose E. Roman . Mdomains - the global number of subdomains in the x direction 15696b867d5aSJose E. Roman . Ndomains - the global number of subdomains in the y direction 1570b1a0a8a3SJed Brown . dof - degrees of freedom per node 1571b1a0a8a3SJed Brown - overlap - overlap in mesh lines 1572b1a0a8a3SJed Brown 1573b1a0a8a3SJed Brown Output Parameters: 1574af538404SDmitry Karpeev + Nsub - the number of local subdomains created 15756a4f0f73SDmitry Karpeev . iis - array of index sets defining inner (nonoverlapping) subdomains 15766a4f0f73SDmitry Karpeev - ois - array of index sets defining outer (overlapping, if overlap > 0) subdomains 1577b1a0a8a3SJed Brown 1578b1a0a8a3SJed Brown Level: advanced 1579b1a0a8a3SJed Brown 1580db781477SPatrick Sanan .seealso: `PCGASMSetSubdomains()`, `PCGASMGetSubKSP()`, `PCGASMSetOverlap()` 1581b1a0a8a3SJed Brown @*/ 15829371c9d4SSatish Balay PetscErrorCode PCGASMCreateSubdomains2D(PC pc, PetscInt M, PetscInt N, PetscInt Mdomains, PetscInt Ndomains, PetscInt dof, PetscInt overlap, PetscInt *nsub, IS **iis, IS **ois) { 15832bbb417fSDmitry Karpeev PetscMPIInt size, rank; 15842bbb417fSDmitry Karpeev PetscInt i, j; 15852bbb417fSDmitry Karpeev PetscInt maxheight, maxwidth; 15862bbb417fSDmitry Karpeev PetscInt xstart, xleft, xright, xleft_loc, xright_loc; 15872bbb417fSDmitry Karpeev PetscInt ystart, ylow, yhigh, ylow_loc, yhigh_loc; 1588eec7e3faSDmitry Karpeev PetscInt x[2][2], y[2][2], n[2]; 15892bbb417fSDmitry Karpeev PetscInt first, last; 15902bbb417fSDmitry Karpeev PetscInt nidx, *idx; 15912bbb417fSDmitry Karpeev PetscInt ii, jj, s, q, d; 1592af538404SDmitry Karpeev PetscInt k, kk; 15932bbb417fSDmitry Karpeev PetscMPIInt color; 15942bbb417fSDmitry Karpeev MPI_Comm comm, subcomm; 15950a545947SLisandro Dalcin IS **xis = NULL, **is = ois, **is_local = iis; 1596d34fcf5fSBarry Smith 1597b1a0a8a3SJed Brown PetscFunctionBegin; 15989566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)pc, &comm)); 15999566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(comm, &size)); 16009566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm, &rank)); 16019566063dSJacob Faibussowitsch PetscCall(MatGetOwnershipRange(pc->pmat, &first, &last)); 16029371c9d4SSatish Balay PetscCheck((first % dof) == 0 && (last % dof) == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, 16039371c9d4SSatish Balay "Matrix row partitioning unsuitable for domain decomposition: local row range (%" PetscInt_FMT ",%" PetscInt_FMT ") " 16049371c9d4SSatish Balay "does not respect the number of degrees of freedom per grid point %" PetscInt_FMT, 16059371c9d4SSatish Balay first, last, dof); 1606d34fcf5fSBarry Smith 1607af538404SDmitry Karpeev /* Determine the number of domains with nonzero intersections with the local ownership range. */ 16082bbb417fSDmitry Karpeev s = 0; 1609af538404SDmitry Karpeev ystart = 0; 1610af538404SDmitry Karpeev for (j = 0; j < Ndomains; ++j) { 1611af538404SDmitry Karpeev maxheight = N / Ndomains + ((N % Ndomains) > j); /* Maximal height of subdomain */ 161263a3b9bcSJacob 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); 1613eec7e3faSDmitry Karpeev /* Vertical domain limits with an overlap. */ 1614eec7e3faSDmitry Karpeev ylow = PetscMax(ystart - overlap, 0); 1615eec7e3faSDmitry Karpeev yhigh = PetscMin(ystart + maxheight + overlap, N); 1616b1a0a8a3SJed Brown xstart = 0; 1617af538404SDmitry Karpeev for (i = 0; i < Mdomains; ++i) { 1618af538404SDmitry Karpeev maxwidth = M / Mdomains + ((M % Mdomains) > i); /* Maximal width of subdomain */ 161963a3b9bcSJacob 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); 1620eec7e3faSDmitry Karpeev /* Horizontal domain limits with an overlap. */ 1621eec7e3faSDmitry Karpeev xleft = PetscMax(xstart - overlap, 0); 1622eec7e3faSDmitry Karpeev xright = PetscMin(xstart + maxwidth + overlap, M); 1623af538404SDmitry Karpeev /* 1624af538404SDmitry Karpeev Determine whether this subdomain intersects this processor's ownership range of pc->pmat. 1625af538404SDmitry Karpeev */ 1626c3518bceSDmitry Karpeev PCGASMLocalSubdomainBounds2D(M, N, xleft, ylow, xright, yhigh, first, last, (&xleft_loc), (&ylow_loc), (&xright_loc), (&yhigh_loc), (&nidx)); 16272fa5cd67SKarl Rupp if (nidx) ++s; 1628af538404SDmitry Karpeev xstart += maxwidth; 1629af538404SDmitry Karpeev } /* for (i = 0; i < Mdomains; ++i) */ 1630af538404SDmitry Karpeev ystart += maxheight; 1631af538404SDmitry Karpeev } /* for (j = 0; j < Ndomains; ++j) */ 16322fa5cd67SKarl Rupp 1633af538404SDmitry Karpeev /* Now we can allocate the necessary number of ISs. */ 1634af538404SDmitry Karpeev *nsub = s; 16359566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(*nsub, is)); 16369566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(*nsub, is_local)); 1637af538404SDmitry Karpeev s = 0; 1638af538404SDmitry Karpeev ystart = 0; 1639af538404SDmitry Karpeev for (j = 0; j < Ndomains; ++j) { 1640af538404SDmitry Karpeev maxheight = N / Ndomains + ((N % Ndomains) > j); /* Maximal height of subdomain */ 164163a3b9bcSJacob 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); 1642af538404SDmitry Karpeev /* Vertical domain limits with an overlap. */ 1643eec7e3faSDmitry Karpeev y[0][0] = PetscMax(ystart - overlap, 0); 1644eec7e3faSDmitry Karpeev y[0][1] = PetscMin(ystart + maxheight + overlap, N); 1645eec7e3faSDmitry Karpeev /* Vertical domain limits without an overlap. */ 1646eec7e3faSDmitry Karpeev y[1][0] = ystart; 1647eec7e3faSDmitry Karpeev y[1][1] = PetscMin(ystart + maxheight, N); 1648eec7e3faSDmitry Karpeev xstart = 0; 1649af538404SDmitry Karpeev for (i = 0; i < Mdomains; ++i) { 1650af538404SDmitry Karpeev maxwidth = M / Mdomains + ((M % Mdomains) > i); /* Maximal width of subdomain */ 165163a3b9bcSJacob 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); 1652af538404SDmitry Karpeev /* Horizontal domain limits with an overlap. */ 1653eec7e3faSDmitry Karpeev x[0][0] = PetscMax(xstart - overlap, 0); 1654eec7e3faSDmitry Karpeev x[0][1] = PetscMin(xstart + maxwidth + overlap, M); 1655eec7e3faSDmitry Karpeev /* Horizontal domain limits without an overlap. */ 1656eec7e3faSDmitry Karpeev x[1][0] = xstart; 1657eec7e3faSDmitry Karpeev x[1][1] = PetscMin(xstart + maxwidth, M); 16582bbb417fSDmitry Karpeev /* 16592bbb417fSDmitry Karpeev Determine whether this domain intersects this processor's ownership range of pc->pmat. 16602bbb417fSDmitry Karpeev Do this twice: first for the domains with overlaps, and once without. 16612bbb417fSDmitry Karpeev During the first pass create the subcommunicators, and use them on the second pass as well. 16622bbb417fSDmitry Karpeev */ 16632bbb417fSDmitry Karpeev for (q = 0; q < 2; ++q) { 1664cc96b2e5SBarry Smith PetscBool split = PETSC_FALSE; 16652bbb417fSDmitry Karpeev /* 16662bbb417fSDmitry Karpeev domain limits, (xleft, xright) and (ylow, yheigh) are adjusted 16672bbb417fSDmitry Karpeev according to whether the domain with an overlap or without is considered. 16682bbb417fSDmitry Karpeev */ 16699371c9d4SSatish Balay xleft = x[q][0]; 16709371c9d4SSatish Balay xright = x[q][1]; 16719371c9d4SSatish Balay ylow = y[q][0]; 16729371c9d4SSatish Balay yhigh = y[q][1]; 1673c3518bceSDmitry Karpeev PCGASMLocalSubdomainBounds2D(M, N, xleft, ylow, xright, yhigh, first, last, (&xleft_loc), (&ylow_loc), (&xright_loc), (&yhigh_loc), (&nidx)); 1674af538404SDmitry Karpeev nidx *= dof; 1675eec7e3faSDmitry Karpeev n[q] = nidx; 16762bbb417fSDmitry Karpeev /* 1677eec7e3faSDmitry Karpeev Based on the counted number of indices in the local domain *with an overlap*, 1678af538404SDmitry Karpeev construct a subcommunicator of all the processors supporting this domain. 1679eec7e3faSDmitry Karpeev Observe that a domain with an overlap might have nontrivial local support, 1680eec7e3faSDmitry Karpeev while the domain without an overlap might not. Hence, the decision to participate 1681eec7e3faSDmitry Karpeev in the subcommunicator must be based on the domain with an overlap. 16822bbb417fSDmitry Karpeev */ 16832bbb417fSDmitry Karpeev if (q == 0) { 16842fa5cd67SKarl Rupp if (nidx) color = 1; 16852fa5cd67SKarl Rupp else color = MPI_UNDEFINED; 16869566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_split(comm, color, rank, &subcomm)); 1687cc96b2e5SBarry Smith split = PETSC_TRUE; 16882bbb417fSDmitry Karpeev } 1689af538404SDmitry Karpeev /* 1690eec7e3faSDmitry Karpeev Proceed only if the number of local indices *with an overlap* is nonzero. 1691af538404SDmitry Karpeev */ 1692eec7e3faSDmitry Karpeev if (n[0]) { 16932fa5cd67SKarl Rupp if (q == 0) xis = is; 1694af538404SDmitry Karpeev if (q == 1) { 1695af538404SDmitry Karpeev /* 1696eec7e3faSDmitry Karpeev The IS for the no-overlap subdomain shares a communicator with the overlapping domain. 1697af538404SDmitry Karpeev Moreover, if the overlap is zero, the two ISs are identical. 1698af538404SDmitry Karpeev */ 1699b1a0a8a3SJed Brown if (overlap == 0) { 1700eec7e3faSDmitry Karpeev (*is_local)[s] = (*is)[s]; 17019566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)(*is)[s])); 17022bbb417fSDmitry Karpeev continue; 1703d34fcf5fSBarry Smith } else { 17046a4f0f73SDmitry Karpeev xis = is_local; 1705eec7e3faSDmitry Karpeev subcomm = ((PetscObject)(*is)[s])->comm; 17062bbb417fSDmitry Karpeev } 1707af538404SDmitry Karpeev } /* if (q == 1) */ 17080298fd71SBarry Smith idx = NULL; 17099566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nidx, &idx)); 1710eec7e3faSDmitry Karpeev if (nidx) { 17112bbb417fSDmitry Karpeev k = 0; 17122bbb417fSDmitry Karpeev for (jj = ylow_loc; jj < yhigh_loc; ++jj) { 1713af538404SDmitry Karpeev PetscInt x0 = (jj == ylow_loc) ? xleft_loc : xleft; 1714af538404SDmitry Karpeev PetscInt x1 = (jj == yhigh_loc - 1) ? xright_loc : xright; 1715af538404SDmitry Karpeev kk = dof * (M * jj + x0); 17162bbb417fSDmitry Karpeev for (ii = x0; ii < x1; ++ii) { 17179371c9d4SSatish Balay for (d = 0; d < dof; ++d) { idx[k++] = kk++; } 1718b1a0a8a3SJed Brown } 1719b1a0a8a3SJed Brown } 1720eec7e3faSDmitry Karpeev } 17219566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(subcomm, nidx, idx, PETSC_OWN_POINTER, (*xis) + s)); 1722*48a46eb9SPierre Jolivet if (split) PetscCallMPI(MPI_Comm_free(&subcomm)); 1723eec7e3faSDmitry Karpeev } /* if (n[0]) */ 17242bbb417fSDmitry Karpeev } /* for (q = 0; q < 2; ++q) */ 17252fa5cd67SKarl Rupp if (n[0]) ++s; 1726af538404SDmitry Karpeev xstart += maxwidth; 1727af538404SDmitry Karpeev } /* for (i = 0; i < Mdomains; ++i) */ 17282bbb417fSDmitry Karpeev ystart += maxheight; 1729af538404SDmitry Karpeev } /* for (j = 0; j < Ndomains; ++j) */ 1730b1a0a8a3SJed Brown PetscFunctionReturn(0); 1731b1a0a8a3SJed Brown } 1732b1a0a8a3SJed Brown 1733b1a0a8a3SJed Brown /*@C 173406b43e7eSDmitry Karpeev PCGASMGetSubdomains - Gets the subdomains supported on this processor 173506b43e7eSDmitry Karpeev for the additive Schwarz preconditioner. 1736b1a0a8a3SJed Brown 1737b1a0a8a3SJed Brown Not Collective 1738b1a0a8a3SJed Brown 1739b1a0a8a3SJed Brown Input Parameter: 1740b1a0a8a3SJed Brown . pc - the preconditioner context 1741b1a0a8a3SJed Brown 1742b1a0a8a3SJed Brown Output Parameters: 1743b1a0a8a3SJed Brown + n - the number of subdomains for this processor (default value = 1) 17440298fd71SBarry Smith . iis - the index sets that define the inner subdomains (without overlap) supported on this processor (can be NULL) 17450298fd71SBarry Smith - ois - the index sets that define the outer subdomains (with overlap) supported on this processor (can be NULL) 1746b1a0a8a3SJed Brown 1747b1a0a8a3SJed Brown Notes: 17486a4f0f73SDmitry Karpeev The user is responsible for destroying the ISs and freeing the returned arrays. 1749b1a0a8a3SJed Brown The IS numbering is in the parallel, global numbering of the vector. 1750b1a0a8a3SJed Brown 1751b1a0a8a3SJed Brown Level: advanced 1752b1a0a8a3SJed Brown 1753db781477SPatrick Sanan .seealso: `PCGASMSetOverlap()`, `PCGASMGetSubKSP()`, `PCGASMCreateSubdomains2D()`, 1754db781477SPatrick Sanan `PCGASMSetSubdomains()`, `PCGASMGetSubmatrices()` 1755b1a0a8a3SJed Brown @*/ 17569371c9d4SSatish Balay PetscErrorCode PCGASMGetSubdomains(PC pc, PetscInt *n, IS *iis[], IS *ois[]) { 1757f746d493SDmitry Karpeev PC_GASM *osm; 1758b1a0a8a3SJed Brown PetscBool match; 17596a4f0f73SDmitry Karpeev PetscInt i; 17605fd66863SKarl Rupp 1761b1a0a8a3SJed Brown PetscFunctionBegin; 1762b1a0a8a3SJed Brown PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 17639566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCGASM, &match)); 176428b400f6SJacob Faibussowitsch PetscCheck(match, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONG, "Incorrect object type: expected %s, got %s instead", PCGASM, ((PetscObject)pc)->type_name); 1765f746d493SDmitry Karpeev osm = (PC_GASM *)pc->data; 1766ab3e923fSDmitry Karpeev if (n) *n = osm->n; 17671baa6e33SBarry Smith if (iis) PetscCall(PetscMalloc1(osm->n, iis)); 17681baa6e33SBarry Smith if (ois) PetscCall(PetscMalloc1(osm->n, ois)); 17696a4f0f73SDmitry Karpeev if (iis || ois) { 17706a4f0f73SDmitry Karpeev for (i = 0; i < osm->n; ++i) { 17716a4f0f73SDmitry Karpeev if (iis) (*iis)[i] = osm->iis[i]; 17726a4f0f73SDmitry Karpeev if (ois) (*ois)[i] = osm->ois[i]; 17736a4f0f73SDmitry Karpeev } 1774b1a0a8a3SJed Brown } 1775b1a0a8a3SJed Brown PetscFunctionReturn(0); 1776b1a0a8a3SJed Brown } 1777b1a0a8a3SJed Brown 1778b1a0a8a3SJed Brown /*@C 177906b43e7eSDmitry Karpeev PCGASMGetSubmatrices - Gets the local submatrices (for this processor 1780b1a0a8a3SJed Brown only) for the additive Schwarz preconditioner. 1781b1a0a8a3SJed Brown 1782b1a0a8a3SJed Brown Not Collective 1783b1a0a8a3SJed Brown 1784b1a0a8a3SJed Brown Input Parameter: 1785b1a0a8a3SJed Brown . pc - the preconditioner context 1786b1a0a8a3SJed Brown 1787b1a0a8a3SJed Brown Output Parameters: 1788b1a0a8a3SJed Brown + n - the number of matrices for this processor (default value = 1) 1789b1a0a8a3SJed Brown - mat - the matrices 1790b1a0a8a3SJed Brown 179195452b02SPatrick Sanan Notes: 179295452b02SPatrick Sanan matrices returned by this routine have the same communicators as the index sets (IS) 17938f3b4b4dSDmitry Karpeev used to define subdomains in PCGASMSetSubdomains() 1794b1a0a8a3SJed Brown Level: advanced 1795b1a0a8a3SJed Brown 1796db781477SPatrick Sanan .seealso: `PCGASMSetOverlap()`, `PCGASMGetSubKSP()`, 1797db781477SPatrick Sanan `PCGASMCreateSubdomains2D()`, `PCGASMSetSubdomains()`, `PCGASMGetSubdomains()` 1798b1a0a8a3SJed Brown @*/ 17999371c9d4SSatish Balay PetscErrorCode PCGASMGetSubmatrices(PC pc, PetscInt *n, Mat *mat[]) { 1800f746d493SDmitry Karpeev PC_GASM *osm; 1801b1a0a8a3SJed Brown PetscBool match; 1802b1a0a8a3SJed Brown 1803b1a0a8a3SJed Brown PetscFunctionBegin; 1804b1a0a8a3SJed Brown PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 1805b1a0a8a3SJed Brown PetscValidIntPointer(n, 2); 1806b1a0a8a3SJed Brown if (mat) PetscValidPointer(mat, 3); 180728b400f6SJacob Faibussowitsch PetscCheck(pc->setupcalled, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Must call after KSPSetUp() or PCSetUp()."); 18089566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCGASM, &match)); 180928b400f6SJacob Faibussowitsch PetscCheck(match, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONG, "Expected %s, got %s instead", PCGASM, ((PetscObject)pc)->type_name); 1810f746d493SDmitry Karpeev osm = (PC_GASM *)pc->data; 1811ab3e923fSDmitry Karpeev if (n) *n = osm->n; 1812b1a0a8a3SJed Brown if (mat) *mat = osm->pmat; 1813b1a0a8a3SJed Brown PetscFunctionReturn(0); 1814b1a0a8a3SJed Brown } 1815d709ea83SDmitry Karpeev 1816d709ea83SDmitry Karpeev /*@ 18178f3b4b4dSDmitry Karpeev PCGASMSetUseDMSubdomains - Indicates whether to use DMCreateDomainDecomposition() to define the subdomains, whenever possible. 1818d709ea83SDmitry Karpeev Logically Collective 1819d709ea83SDmitry Karpeev 1820d8d19677SJose E. Roman Input Parameters: 1821d709ea83SDmitry Karpeev + pc - the preconditioner 1822d709ea83SDmitry Karpeev - flg - boolean indicating whether to use subdomains defined by the DM 1823d709ea83SDmitry Karpeev 1824d709ea83SDmitry Karpeev Options Database Key: 18258f3b4b4dSDmitry Karpeev . -pc_gasm_dm_subdomains -pc_gasm_overlap -pc_gasm_total_subdomains 1826d709ea83SDmitry Karpeev 1827d709ea83SDmitry Karpeev Level: intermediate 1828d709ea83SDmitry Karpeev 1829d709ea83SDmitry Karpeev Notes: 18308f3b4b4dSDmitry Karpeev PCGASMSetSubdomains(), PCGASMSetTotalSubdomains() or PCGASMSetOverlap() take precedence over PCGASMSetUseDMSubdomains(), 18318f3b4b4dSDmitry Karpeev so setting PCGASMSetSubdomains() with nontrivial subdomain ISs or any of PCGASMSetTotalSubdomains() and PCGASMSetOverlap() 18328f3b4b4dSDmitry Karpeev automatically turns the latter off. 1833d709ea83SDmitry Karpeev 1834db781477SPatrick Sanan .seealso: `PCGASMGetUseDMSubdomains()`, `PCGASMSetSubdomains()`, `PCGASMSetOverlap()` 1835db781477SPatrick Sanan `PCGASMCreateSubdomains2D()` 1836d709ea83SDmitry Karpeev @*/ 18379371c9d4SSatish Balay PetscErrorCode PCGASMSetUseDMSubdomains(PC pc, PetscBool flg) { 1838d709ea83SDmitry Karpeev PC_GASM *osm = (PC_GASM *)pc->data; 1839d709ea83SDmitry Karpeev PetscBool match; 1840d709ea83SDmitry Karpeev 1841d709ea83SDmitry Karpeev PetscFunctionBegin; 1842d709ea83SDmitry Karpeev PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 1843d709ea83SDmitry Karpeev PetscValidLogicalCollectiveBool(pc, flg, 2); 184428b400f6SJacob Faibussowitsch PetscCheck(!pc->setupcalled, ((PetscObject)pc)->comm, PETSC_ERR_ARG_WRONGSTATE, "Not for a setup PC."); 18459566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCGASM, &match)); 1846d709ea83SDmitry Karpeev if (match) { 18479371c9d4SSatish Balay if (!osm->user_subdomains && osm->N == PETSC_DETERMINE && osm->overlap < 0) { osm->dm_subdomains = flg; } 18488f3b4b4dSDmitry Karpeev } 1849d709ea83SDmitry Karpeev PetscFunctionReturn(0); 1850d709ea83SDmitry Karpeev } 1851d709ea83SDmitry Karpeev 1852d709ea83SDmitry Karpeev /*@ 18538f3b4b4dSDmitry Karpeev PCGASMGetUseDMSubdomains - Returns flag indicating whether to use DMCreateDomainDecomposition() to define the subdomains, whenever possible. 1854d709ea83SDmitry Karpeev Not Collective 1855d709ea83SDmitry Karpeev 1856d709ea83SDmitry Karpeev Input Parameter: 1857d709ea83SDmitry Karpeev . pc - the preconditioner 1858d709ea83SDmitry Karpeev 1859d709ea83SDmitry Karpeev Output Parameter: 1860d709ea83SDmitry Karpeev . flg - boolean indicating whether to use subdomains defined by the DM 1861d709ea83SDmitry Karpeev 1862d709ea83SDmitry Karpeev Level: intermediate 1863d709ea83SDmitry Karpeev 1864db781477SPatrick Sanan .seealso: `PCGASMSetUseDMSubdomains()`, `PCGASMSetOverlap()` 1865db781477SPatrick Sanan `PCGASMCreateSubdomains2D()` 1866d709ea83SDmitry Karpeev @*/ 18679371c9d4SSatish Balay PetscErrorCode PCGASMGetUseDMSubdomains(PC pc, PetscBool *flg) { 1868d709ea83SDmitry Karpeev PC_GASM *osm = (PC_GASM *)pc->data; 1869d709ea83SDmitry Karpeev PetscBool match; 1870d709ea83SDmitry Karpeev 1871d709ea83SDmitry Karpeev PetscFunctionBegin; 1872d709ea83SDmitry Karpeev PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 1873534a8f05SLisandro Dalcin PetscValidBoolPointer(flg, 2); 18749566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCGASM, &match)); 1875d709ea83SDmitry Karpeev if (match) { 1876d709ea83SDmitry Karpeev if (flg) *flg = osm->dm_subdomains; 1877d709ea83SDmitry Karpeev } 1878d709ea83SDmitry Karpeev PetscFunctionReturn(0); 1879d709ea83SDmitry Karpeev } 1880