xref: /petsc/src/mat/utils/overlapsplit.c (revision 9371c9d470a9602b6d10a8bf50c9b2280a79e45a)
1bac5b06fSFande Kong /*
22da392ccSBarry Smith  * Increase the overlap of a 'big' subdomain across several processor cores
3bac5b06fSFande Kong  *
4bac5b06fSFande Kong  * Author: Fande Kong <fdkong.jd@gmail.com>
5bac5b06fSFande Kong  */
6bac5b06fSFande Kong 
72452736bSFande Kong #include <petscsf.h>
82452736bSFande Kong #include <petsc/private/matimpl.h>
92452736bSFande Kong 
102452736bSFande Kong /*
112452736bSFande Kong  * Increase overlap for the sub-matrix across sub communicator
122452736bSFande Kong  * sub-matrix could be a graph or numerical matrix
132452736bSFande Kong  * */
14*9371c9d4SSatish Balay PetscErrorCode MatIncreaseOverlapSplit_Single(Mat mat, IS *is, PetscInt ov) {
152452736bSFande Kong   PetscInt        i, nindx, *indices_sc, *indices_ov, localsize, *localsizes_sc, localsize_tmp;
162452736bSFande Kong   PetscInt       *indices_ov_rd, nroots, nleaves, *localoffsets, *indices_recv, *sources_sc, *sources_sc_rd;
172452736bSFande Kong   const PetscInt *indices;
18a69400e0SFande Kong   PetscMPIInt     srank, ssize, issamecomm, k, grank;
19bac5b06fSFande Kong   IS              is_sc, allis_sc, partitioning;
20a69400e0SFande Kong   MPI_Comm        gcomm, dcomm, scomm;
212452736bSFande Kong   PetscSF         sf;
222452736bSFande Kong   PetscSFNode    *remote;
232452736bSFande Kong   Mat            *smat;
242452736bSFande Kong   MatPartitioning part;
252452736bSFande Kong 
262452736bSFande Kong   PetscFunctionBegin;
272452736bSFande Kong   /* get a sub communicator before call individual MatIncreaseOverlap
282452736bSFande Kong    * since the sub communicator may be changed.
292452736bSFande Kong    * */
309566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)(*is), &dcomm));
31a69400e0SFande Kong   /* make a copy before the original one is deleted */
329566063dSJacob Faibussowitsch   PetscCall(PetscCommDuplicate(dcomm, &scomm, NULL));
33ca2fc57aSFande Kong   /* get a global communicator, where mat should be a global matrix  */
349566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)mat, &gcomm));
35dbbe0bcdSBarry Smith   PetscUseTypeMethod(mat, increaseoverlap, 1, is, ov);
369566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_compare(gcomm, scomm, &issamecomm));
372452736bSFande Kong   /* if the sub-communicator is the same as the global communicator,
382452736bSFande Kong    * user does not want to use a sub-communicator
392452736bSFande Kong    * */
40ea91fabdSFande Kong   if (issamecomm == MPI_IDENT || issamecomm == MPI_CONGRUENT) {
419566063dSJacob Faibussowitsch     PetscCall(PetscCommDestroy(&scomm));
42ea91fabdSFande Kong     PetscFunctionReturn(0);
43ea91fabdSFande Kong   }
442452736bSFande Kong   /* if the sub-communicator is petsc_comm_self,
452452736bSFande Kong    * user also does not care the sub-communicator
462452736bSFande Kong    * */
479566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_compare(scomm, PETSC_COMM_SELF, &issamecomm));
48ea91fabdSFande Kong   if (issamecomm == MPI_IDENT || issamecomm == MPI_CONGRUENT) {
499566063dSJacob Faibussowitsch     PetscCall(PetscCommDestroy(&scomm));
50ea91fabdSFande Kong     PetscFunctionReturn(0);
51ea91fabdSFande Kong   }
529566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(scomm, &srank));
539566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(scomm, &ssize));
549566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(gcomm, &grank));
55ca2fc57aSFande Kong   /* create a new IS based on sub-communicator
56ca2fc57aSFande Kong    * since the old IS is often based on petsc_comm_self
572452736bSFande Kong    * */
589566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(*is, &nindx));
599566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(nindx, &indices_sc));
609566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(*is, &indices));
619566063dSJacob Faibussowitsch   PetscCall(PetscArraycpy(indices_sc, indices, nindx));
629566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(*is, &indices));
632452736bSFande Kong   /* we do not need any more */
649566063dSJacob Faibussowitsch   PetscCall(ISDestroy(is));
652452736bSFande Kong   /* create a index set based on the sub communicator  */
669566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(scomm, nindx, indices_sc, PETSC_OWN_POINTER, &is_sc));
672452736bSFande Kong   /* gather all indices within  the sub communicator */
689566063dSJacob Faibussowitsch   PetscCall(ISAllGather(is_sc, &allis_sc));
699566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&is_sc));
702452736bSFande Kong   /* gather local sizes */
719566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(ssize, &localsizes_sc));
72ca2fc57aSFande Kong   /* get individual local sizes for all index sets */
739566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Gather(&nindx, 1, MPIU_INT, localsizes_sc, 1, MPIU_INT, 0, scomm));
74ca2fc57aSFande Kong   /* only root does these computations */
752452736bSFande Kong   if (!srank) {
762452736bSFande Kong     /* get local size for the big index set */
779566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(allis_sc, &localsize));
789566063dSJacob Faibussowitsch     PetscCall(PetscCalloc2(localsize, &indices_ov, localsize, &sources_sc));
799566063dSJacob Faibussowitsch     PetscCall(PetscCalloc2(localsize, &indices_ov_rd, localsize, &sources_sc_rd));
809566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(allis_sc, &indices));
819566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(indices_ov, indices, localsize));
829566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(allis_sc, &indices));
839566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&allis_sc));
842452736bSFande Kong     /* assign corresponding sources */
852452736bSFande Kong     localsize_tmp = 0;
862452736bSFande Kong     for (k = 0; k < ssize; k++) {
87*9371c9d4SSatish Balay       for (i = 0; i < localsizes_sc[k]; i++) { sources_sc[localsize_tmp++] = k; }
882452736bSFande Kong     }
892452736bSFande Kong     /* record where indices come from */
909566063dSJacob Faibussowitsch     PetscCall(PetscSortIntWithArray(localsize, indices_ov, sources_sc));
91ca2fc57aSFande Kong     /* count local sizes for reduced indices */
929566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(localsizes_sc, ssize));
93ca2fc57aSFande Kong     /* initialize the first entity */
942452736bSFande Kong     if (localsize) {
952452736bSFande Kong       indices_ov_rd[0] = indices_ov[0];
962452736bSFande Kong       sources_sc_rd[0] = sources_sc[0];
972452736bSFande Kong       localsizes_sc[sources_sc[0]]++;
982452736bSFande Kong     }
99ca2fc57aSFande Kong     localsize_tmp = 1;
1002452736bSFande Kong     /* remove duplicate integers */
1012452736bSFande Kong     for (i = 1; i < localsize; i++) {
1022452736bSFande Kong       if (indices_ov[i] != indices_ov[i - 1]) {
1032452736bSFande Kong         indices_ov_rd[localsize_tmp]   = indices_ov[i];
1042452736bSFande Kong         sources_sc_rd[localsize_tmp++] = sources_sc[i];
1052452736bSFande Kong         localsizes_sc[sources_sc[i]]++;
1062452736bSFande Kong       }
1072452736bSFande Kong     }
1089566063dSJacob Faibussowitsch     PetscCall(PetscFree2(indices_ov, sources_sc));
1099566063dSJacob Faibussowitsch     PetscCall(PetscCalloc1(ssize + 1, &localoffsets));
110*9371c9d4SSatish Balay     for (k = 0; k < ssize; k++) { localoffsets[k + 1] = localoffsets[k] + localsizes_sc[k]; }
1112452736bSFande Kong     nleaves = localoffsets[ssize];
1129566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(localoffsets, ssize + 1));
1132452736bSFande Kong     nroots = localsizes_sc[srank];
1149566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nleaves, &remote));
1152452736bSFande Kong     for (i = 0; i < nleaves; i++) {
116ca2fc57aSFande Kong       remote[i].rank  = sources_sc_rd[i];
117ca2fc57aSFande Kong       remote[i].index = localoffsets[sources_sc_rd[i]]++;
1182452736bSFande Kong     }
1199566063dSJacob Faibussowitsch     PetscCall(PetscFree(localoffsets));
1202452736bSFande Kong   } else {
1219566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&allis_sc));
122e12b4729SFande Kong     /* Allocate a 'zero' pointer to avoid using uninitialized variable  */
1239566063dSJacob Faibussowitsch     PetscCall(PetscCalloc1(0, &remote));
1242452736bSFande Kong     nleaves       = 0;
125f4259b30SLisandro Dalcin     indices_ov_rd = NULL;
126f4259b30SLisandro Dalcin     sources_sc_rd = NULL;
1272452736bSFande Kong   }
1282452736bSFande Kong   /* scatter sizes to everybody */
1299566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Scatter(localsizes_sc, 1, MPIU_INT, &nroots, 1, MPIU_INT, 0, scomm));
1309566063dSJacob Faibussowitsch   PetscCall(PetscFree(localsizes_sc));
1319566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(nroots, &indices_recv));
1322452736bSFande Kong   /* set data back to every body */
1339566063dSJacob Faibussowitsch   PetscCall(PetscSFCreate(scomm, &sf));
1349566063dSJacob Faibussowitsch   PetscCall(PetscSFSetType(sf, PETSCSFBASIC));
1359566063dSJacob Faibussowitsch   PetscCall(PetscSFSetFromOptions(sf));
1369566063dSJacob Faibussowitsch   PetscCall(PetscSFSetGraph(sf, nroots, nleaves, NULL, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER));
1379566063dSJacob Faibussowitsch   PetscCall(PetscSFReduceBegin(sf, MPIU_INT, indices_ov_rd, indices_recv, MPI_REPLACE));
1389566063dSJacob Faibussowitsch   PetscCall(PetscSFReduceEnd(sf, MPIU_INT, indices_ov_rd, indices_recv, MPI_REPLACE));
1399566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&sf));
1409566063dSJacob Faibussowitsch   PetscCall(PetscFree2(indices_ov_rd, sources_sc_rd));
1419566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(scomm, nroots, indices_recv, PETSC_OWN_POINTER, &is_sc));
1429566063dSJacob Faibussowitsch   PetscCall(MatCreateSubMatricesMPI(mat, 1, &is_sc, &is_sc, MAT_INITIAL_MATRIX, &smat));
1439566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&allis_sc));
1442452736bSFande Kong   /* create a partitioner to repartition the sub-matrix */
1459566063dSJacob Faibussowitsch   PetscCall(MatPartitioningCreate(scomm, &part));
1469566063dSJacob Faibussowitsch   PetscCall(MatPartitioningSetAdjacency(part, smat[0]));
147dd63322aSSatish Balay #if defined(PETSC_HAVE_PARMETIS)
1482452736bSFande Kong   /* if there exists a ParMETIS installation, we try to use ParMETIS
1492452736bSFande Kong    * because a repartition routine possibly work better
1502452736bSFande Kong    * */
1519566063dSJacob Faibussowitsch   PetscCall(MatPartitioningSetType(part, MATPARTITIONINGPARMETIS));
1522452736bSFande Kong   /* try to use reparition function, instead of partition function */
1539566063dSJacob Faibussowitsch   PetscCall(MatPartitioningParmetisSetRepartition(part));
1542452736bSFande Kong #else
1552452736bSFande Kong   /* we at least provide a default partitioner to rebalance the computation  */
1569566063dSJacob Faibussowitsch   PetscCall(MatPartitioningSetType(part, MATPARTITIONINGAVERAGE));
1572452736bSFande Kong #endif
1582452736bSFande Kong   /* user can pick up any partitioner by using an option */
1599566063dSJacob Faibussowitsch   PetscCall(MatPartitioningSetFromOptions(part));
1609566063dSJacob Faibussowitsch   PetscCall(MatPartitioningApply(part, &partitioning));
1619566063dSJacob Faibussowitsch   PetscCall(MatPartitioningDestroy(&part));
1629566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(smat[0])));
1639566063dSJacob Faibussowitsch   PetscCall(PetscFree(smat));
1642452736bSFande Kong   /* get local rows including  overlap */
1659566063dSJacob Faibussowitsch   PetscCall(ISBuildTwoSided(partitioning, is_sc, is));
1669566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&is_sc));
1679566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&partitioning));
1689566063dSJacob Faibussowitsch   PetscCall(PetscCommDestroy(&scomm));
1692452736bSFande Kong   PetscFunctionReturn(0);
1702452736bSFande Kong }
171