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 * */ 14d71ae5a4SJacob Faibussowitsch PetscErrorCode MatIncreaseOverlapSplit_Single(Mat mat, IS *is, PetscInt ov) 15d71ae5a4SJacob Faibussowitsch { 162452736bSFande Kong PetscInt i, nindx, *indices_sc, *indices_ov, localsize, *localsizes_sc, localsize_tmp; 172452736bSFande Kong PetscInt *indices_ov_rd, nroots, nleaves, *localoffsets, *indices_recv, *sources_sc, *sources_sc_rd; 182452736bSFande Kong const PetscInt *indices; 19a69400e0SFande Kong PetscMPIInt srank, ssize, issamecomm, k, grank; 20bac5b06fSFande Kong IS is_sc, allis_sc, partitioning; 21a69400e0SFande Kong MPI_Comm gcomm, dcomm, scomm; 222452736bSFande Kong PetscSF sf; 232452736bSFande Kong PetscSFNode *remote; 242452736bSFande Kong Mat *smat; 252452736bSFande Kong MatPartitioning part; 262452736bSFande Kong 272452736bSFande Kong PetscFunctionBegin; 282452736bSFande Kong /* get a sub communicator before call individual MatIncreaseOverlap 292452736bSFande Kong * since the sub communicator may be changed. 302452736bSFande Kong * */ 31*f4f49eeaSPierre Jolivet PetscCall(PetscObjectGetComm((PetscObject)*is, &dcomm)); 32a69400e0SFande Kong /* make a copy before the original one is deleted */ 339566063dSJacob Faibussowitsch PetscCall(PetscCommDuplicate(dcomm, &scomm, NULL)); 34ca2fc57aSFande Kong /* get a global communicator, where mat should be a global matrix */ 359566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)mat, &gcomm)); 36dbbe0bcdSBarry Smith PetscUseTypeMethod(mat, increaseoverlap, 1, is, ov); 379566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_compare(gcomm, scomm, &issamecomm)); 382452736bSFande Kong /* if the sub-communicator is the same as the global communicator, 392452736bSFande Kong * user does not want to use a sub-communicator 402452736bSFande Kong * */ 41ea91fabdSFande Kong if (issamecomm == MPI_IDENT || issamecomm == MPI_CONGRUENT) { 429566063dSJacob Faibussowitsch PetscCall(PetscCommDestroy(&scomm)); 433ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 44ea91fabdSFande Kong } 452452736bSFande Kong /* if the sub-communicator is petsc_comm_self, 462452736bSFande Kong * user also does not care the sub-communicator 472452736bSFande Kong * */ 489566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_compare(scomm, PETSC_COMM_SELF, &issamecomm)); 49ea91fabdSFande Kong if (issamecomm == MPI_IDENT || issamecomm == MPI_CONGRUENT) { 509566063dSJacob Faibussowitsch PetscCall(PetscCommDestroy(&scomm)); 513ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 52ea91fabdSFande Kong } 539566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(scomm, &srank)); 549566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(scomm, &ssize)); 559566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(gcomm, &grank)); 56ca2fc57aSFande Kong /* create a new IS based on sub-communicator 57ca2fc57aSFande Kong * since the old IS is often based on petsc_comm_self 582452736bSFande Kong * */ 599566063dSJacob Faibussowitsch PetscCall(ISGetLocalSize(*is, &nindx)); 609566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nindx, &indices_sc)); 619566063dSJacob Faibussowitsch PetscCall(ISGetIndices(*is, &indices)); 629566063dSJacob Faibussowitsch PetscCall(PetscArraycpy(indices_sc, indices, nindx)); 639566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(*is, &indices)); 642452736bSFande Kong /* we do not need any more */ 659566063dSJacob Faibussowitsch PetscCall(ISDestroy(is)); 662452736bSFande Kong /* create a index set based on the sub communicator */ 679566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(scomm, nindx, indices_sc, PETSC_OWN_POINTER, &is_sc)); 682452736bSFande Kong /* gather all indices within the sub communicator */ 699566063dSJacob Faibussowitsch PetscCall(ISAllGather(is_sc, &allis_sc)); 709566063dSJacob Faibussowitsch PetscCall(ISDestroy(&is_sc)); 712452736bSFande Kong /* gather local sizes */ 729566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(ssize, &localsizes_sc)); 73ca2fc57aSFande Kong /* get individual local sizes for all index sets */ 749566063dSJacob Faibussowitsch PetscCallMPI(MPI_Gather(&nindx, 1, MPIU_INT, localsizes_sc, 1, MPIU_INT, 0, scomm)); 75ca2fc57aSFande Kong /* only root does these computations */ 762452736bSFande Kong if (!srank) { 772452736bSFande Kong /* get local size for the big index set */ 789566063dSJacob Faibussowitsch PetscCall(ISGetLocalSize(allis_sc, &localsize)); 799566063dSJacob Faibussowitsch PetscCall(PetscCalloc2(localsize, &indices_ov, localsize, &sources_sc)); 809566063dSJacob Faibussowitsch PetscCall(PetscCalloc2(localsize, &indices_ov_rd, localsize, &sources_sc_rd)); 819566063dSJacob Faibussowitsch PetscCall(ISGetIndices(allis_sc, &indices)); 829566063dSJacob Faibussowitsch PetscCall(PetscArraycpy(indices_ov, indices, localsize)); 839566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(allis_sc, &indices)); 849566063dSJacob Faibussowitsch PetscCall(ISDestroy(&allis_sc)); 852452736bSFande Kong /* assign corresponding sources */ 862452736bSFande Kong localsize_tmp = 0; 872452736bSFande Kong for (k = 0; k < ssize; k++) { 88ad540459SPierre Jolivet for (i = 0; i < localsizes_sc[k]; i++) sources_sc[localsize_tmp++] = k; 892452736bSFande Kong } 902452736bSFande Kong /* record where indices come from */ 919566063dSJacob Faibussowitsch PetscCall(PetscSortIntWithArray(localsize, indices_ov, sources_sc)); 92ca2fc57aSFande Kong /* count local sizes for reduced indices */ 939566063dSJacob Faibussowitsch PetscCall(PetscArrayzero(localsizes_sc, ssize)); 94ca2fc57aSFande Kong /* initialize the first entity */ 952452736bSFande Kong if (localsize) { 962452736bSFande Kong indices_ov_rd[0] = indices_ov[0]; 972452736bSFande Kong sources_sc_rd[0] = sources_sc[0]; 982452736bSFande Kong localsizes_sc[sources_sc[0]]++; 992452736bSFande Kong } 100ca2fc57aSFande Kong localsize_tmp = 1; 1012452736bSFande Kong /* remove duplicate integers */ 1022452736bSFande Kong for (i = 1; i < localsize; i++) { 1032452736bSFande Kong if (indices_ov[i] != indices_ov[i - 1]) { 1042452736bSFande Kong indices_ov_rd[localsize_tmp] = indices_ov[i]; 1052452736bSFande Kong sources_sc_rd[localsize_tmp++] = sources_sc[i]; 1062452736bSFande Kong localsizes_sc[sources_sc[i]]++; 1072452736bSFande Kong } 1082452736bSFande Kong } 1099566063dSJacob Faibussowitsch PetscCall(PetscFree2(indices_ov, sources_sc)); 1109566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(ssize + 1, &localoffsets)); 111ad540459SPierre Jolivet for (k = 0; k < ssize; k++) localoffsets[k + 1] = localoffsets[k] + localsizes_sc[k]; 1122452736bSFande Kong nleaves = localoffsets[ssize]; 1139566063dSJacob Faibussowitsch PetscCall(PetscArrayzero(localoffsets, ssize + 1)); 1142452736bSFande Kong nroots = localsizes_sc[srank]; 1159566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nleaves, &remote)); 1162452736bSFande Kong for (i = 0; i < nleaves; i++) { 117ca2fc57aSFande Kong remote[i].rank = sources_sc_rd[i]; 118ca2fc57aSFande Kong remote[i].index = localoffsets[sources_sc_rd[i]]++; 1192452736bSFande Kong } 1209566063dSJacob Faibussowitsch PetscCall(PetscFree(localoffsets)); 1212452736bSFande Kong } else { 1229566063dSJacob Faibussowitsch PetscCall(ISDestroy(&allis_sc)); 123e12b4729SFande Kong /* Allocate a 'zero' pointer to avoid using uninitialized variable */ 1249566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(0, &remote)); 1252452736bSFande Kong nleaves = 0; 126f4259b30SLisandro Dalcin indices_ov_rd = NULL; 127f4259b30SLisandro Dalcin sources_sc_rd = NULL; 1282452736bSFande Kong } 1292452736bSFande Kong /* scatter sizes to everybody */ 1309566063dSJacob Faibussowitsch PetscCallMPI(MPI_Scatter(localsizes_sc, 1, MPIU_INT, &nroots, 1, MPIU_INT, 0, scomm)); 1319566063dSJacob Faibussowitsch PetscCall(PetscFree(localsizes_sc)); 1329566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(nroots, &indices_recv)); 1332452736bSFande Kong /* set data back to every body */ 1349566063dSJacob Faibussowitsch PetscCall(PetscSFCreate(scomm, &sf)); 1359566063dSJacob Faibussowitsch PetscCall(PetscSFSetType(sf, PETSCSFBASIC)); 1369566063dSJacob Faibussowitsch PetscCall(PetscSFSetFromOptions(sf)); 1379566063dSJacob Faibussowitsch PetscCall(PetscSFSetGraph(sf, nroots, nleaves, NULL, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER)); 1389566063dSJacob Faibussowitsch PetscCall(PetscSFReduceBegin(sf, MPIU_INT, indices_ov_rd, indices_recv, MPI_REPLACE)); 1399566063dSJacob Faibussowitsch PetscCall(PetscSFReduceEnd(sf, MPIU_INT, indices_ov_rd, indices_recv, MPI_REPLACE)); 1409566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&sf)); 1419566063dSJacob Faibussowitsch PetscCall(PetscFree2(indices_ov_rd, sources_sc_rd)); 1429566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(scomm, nroots, indices_recv, PETSC_OWN_POINTER, &is_sc)); 1439566063dSJacob Faibussowitsch PetscCall(MatCreateSubMatricesMPI(mat, 1, &is_sc, &is_sc, MAT_INITIAL_MATRIX, &smat)); 1449566063dSJacob Faibussowitsch PetscCall(ISDestroy(&allis_sc)); 1452452736bSFande Kong /* create a partitioner to repartition the sub-matrix */ 1469566063dSJacob Faibussowitsch PetscCall(MatPartitioningCreate(scomm, &part)); 1479566063dSJacob Faibussowitsch PetscCall(MatPartitioningSetAdjacency(part, smat[0])); 148dd63322aSSatish Balay #if defined(PETSC_HAVE_PARMETIS) 1492452736bSFande Kong /* if there exists a ParMETIS installation, we try to use ParMETIS 1502452736bSFande Kong * because a repartition routine possibly work better 1512452736bSFande Kong * */ 1529566063dSJacob Faibussowitsch PetscCall(MatPartitioningSetType(part, MATPARTITIONINGPARMETIS)); 1532452736bSFande Kong /* try to use reparition function, instead of partition function */ 1549566063dSJacob Faibussowitsch PetscCall(MatPartitioningParmetisSetRepartition(part)); 1552452736bSFande Kong #else 1562452736bSFande Kong /* we at least provide a default partitioner to rebalance the computation */ 1579566063dSJacob Faibussowitsch PetscCall(MatPartitioningSetType(part, MATPARTITIONINGAVERAGE)); 1582452736bSFande Kong #endif 1592452736bSFande Kong /* user can pick up any partitioner by using an option */ 1609566063dSJacob Faibussowitsch PetscCall(MatPartitioningSetFromOptions(part)); 1619566063dSJacob Faibussowitsch PetscCall(MatPartitioningApply(part, &partitioning)); 1629566063dSJacob Faibussowitsch PetscCall(MatPartitioningDestroy(&part)); 163*f4f49eeaSPierre Jolivet PetscCall(MatDestroy(&smat[0])); 1649566063dSJacob Faibussowitsch PetscCall(PetscFree(smat)); 1652452736bSFande Kong /* get local rows including overlap */ 1669566063dSJacob Faibussowitsch PetscCall(ISBuildTwoSided(partitioning, is_sc, is)); 1679566063dSJacob Faibussowitsch PetscCall(ISDestroy(&is_sc)); 1689566063dSJacob Faibussowitsch PetscCall(ISDestroy(&partitioning)); 1699566063dSJacob Faibussowitsch PetscCall(PetscCommDestroy(&scomm)); 1703ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1712452736bSFande Kong } 172