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 * */ 142452736bSFande Kong PetscErrorCode MatIncreaseOverlapSplit_Single(Mat mat,IS *is,PetscInt ov) 152452736bSFande Kong { 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 * */ 319566063dSJacob Faibussowitsch 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)); 36*dbbe0bcdSBarry 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)); 43ea91fabdSFande Kong PetscFunctionReturn(0); 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)); 51ea91fabdSFande Kong PetscFunctionReturn(0); 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++) { 882452736bSFande Kong for (i=0; i<localsizes_sc[k]; i++) { 892452736bSFande Kong sources_sc[localsize_tmp++] = k; 902452736bSFande Kong } 912452736bSFande Kong } 922452736bSFande Kong /* record where indices come from */ 939566063dSJacob Faibussowitsch PetscCall(PetscSortIntWithArray(localsize,indices_ov,sources_sc)); 94ca2fc57aSFande Kong /* count local sizes for reduced indices */ 959566063dSJacob Faibussowitsch PetscCall(PetscArrayzero(localsizes_sc,ssize)); 96ca2fc57aSFande Kong /* initialize the first entity */ 972452736bSFande Kong if (localsize) { 982452736bSFande Kong indices_ov_rd[0] = indices_ov[0]; 992452736bSFande Kong sources_sc_rd[0] = sources_sc[0]; 1002452736bSFande Kong localsizes_sc[sources_sc[0]]++; 1012452736bSFande Kong } 102ca2fc57aSFande Kong localsize_tmp = 1; 1032452736bSFande Kong /* remove duplicate integers */ 1042452736bSFande Kong for (i=1; i<localsize; i++) { 1052452736bSFande Kong if (indices_ov[i] != indices_ov[i-1]) { 1062452736bSFande Kong indices_ov_rd[localsize_tmp] = indices_ov[i]; 1072452736bSFande Kong sources_sc_rd[localsize_tmp++] = sources_sc[i]; 1082452736bSFande Kong localsizes_sc[sources_sc[i]]++; 1092452736bSFande Kong } 1102452736bSFande Kong } 1119566063dSJacob Faibussowitsch PetscCall(PetscFree2(indices_ov,sources_sc)); 1129566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(ssize+1,&localoffsets)); 1132452736bSFande Kong for (k=0; k<ssize; k++) { 114a69400e0SFande Kong localoffsets[k+1] = localoffsets[k] + localsizes_sc[k]; 1152452736bSFande Kong } 1162452736bSFande Kong nleaves = localoffsets[ssize]; 1179566063dSJacob Faibussowitsch PetscCall(PetscArrayzero(localoffsets,ssize+1)); 1182452736bSFande Kong nroots = localsizes_sc[srank]; 1199566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nleaves,&remote)); 1202452736bSFande Kong for (i=0; i<nleaves; i++) { 121ca2fc57aSFande Kong remote[i].rank = sources_sc_rd[i]; 122ca2fc57aSFande Kong remote[i].index = localoffsets[sources_sc_rd[i]]++; 1232452736bSFande Kong } 1249566063dSJacob Faibussowitsch PetscCall(PetscFree(localoffsets)); 1252452736bSFande Kong } else { 1269566063dSJacob Faibussowitsch PetscCall(ISDestroy(&allis_sc)); 127e12b4729SFande Kong /* Allocate a 'zero' pointer to avoid using uninitialized variable */ 1289566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(0,&remote)); 1292452736bSFande Kong nleaves = 0; 130f4259b30SLisandro Dalcin indices_ov_rd = NULL; 131f4259b30SLisandro Dalcin sources_sc_rd = NULL; 1322452736bSFande Kong } 1332452736bSFande Kong /* scatter sizes to everybody */ 1349566063dSJacob Faibussowitsch PetscCallMPI(MPI_Scatter(localsizes_sc,1, MPIU_INT,&nroots,1, MPIU_INT,0,scomm)); 1359566063dSJacob Faibussowitsch PetscCall(PetscFree(localsizes_sc)); 1369566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(nroots,&indices_recv)); 1372452736bSFande Kong /* set data back to every body */ 1389566063dSJacob Faibussowitsch PetscCall(PetscSFCreate(scomm,&sf)); 1399566063dSJacob Faibussowitsch PetscCall(PetscSFSetType(sf,PETSCSFBASIC)); 1409566063dSJacob Faibussowitsch PetscCall(PetscSFSetFromOptions(sf)); 1419566063dSJacob Faibussowitsch PetscCall(PetscSFSetGraph(sf,nroots,nleaves,NULL,PETSC_OWN_POINTER,remote,PETSC_OWN_POINTER)); 1429566063dSJacob Faibussowitsch PetscCall(PetscSFReduceBegin(sf,MPIU_INT,indices_ov_rd,indices_recv,MPI_REPLACE)); 1439566063dSJacob Faibussowitsch PetscCall(PetscSFReduceEnd(sf,MPIU_INT,indices_ov_rd,indices_recv,MPI_REPLACE)); 1449566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&sf)); 1459566063dSJacob Faibussowitsch PetscCall(PetscFree2(indices_ov_rd,sources_sc_rd)); 1469566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(scomm,nroots,indices_recv,PETSC_OWN_POINTER,&is_sc)); 1479566063dSJacob Faibussowitsch PetscCall(MatCreateSubMatricesMPI(mat,1,&is_sc,&is_sc,MAT_INITIAL_MATRIX,&smat)); 1489566063dSJacob Faibussowitsch PetscCall(ISDestroy(&allis_sc)); 1492452736bSFande Kong /* create a partitioner to repartition the sub-matrix */ 1509566063dSJacob Faibussowitsch PetscCall(MatPartitioningCreate(scomm,&part)); 1519566063dSJacob Faibussowitsch PetscCall(MatPartitioningSetAdjacency(part,smat[0])); 152dd63322aSSatish Balay #if defined(PETSC_HAVE_PARMETIS) 1532452736bSFande Kong /* if there exists a ParMETIS installation, we try to use ParMETIS 1542452736bSFande Kong * because a repartition routine possibly work better 1552452736bSFande Kong * */ 1569566063dSJacob Faibussowitsch PetscCall(MatPartitioningSetType(part,MATPARTITIONINGPARMETIS)); 1572452736bSFande Kong /* try to use reparition function, instead of partition function */ 1589566063dSJacob Faibussowitsch PetscCall(MatPartitioningParmetisSetRepartition(part)); 1592452736bSFande Kong #else 1602452736bSFande Kong /* we at least provide a default partitioner to rebalance the computation */ 1619566063dSJacob Faibussowitsch PetscCall(MatPartitioningSetType(part,MATPARTITIONINGAVERAGE)); 1622452736bSFande Kong #endif 1632452736bSFande Kong /* user can pick up any partitioner by using an option */ 1649566063dSJacob Faibussowitsch PetscCall(MatPartitioningSetFromOptions(part)); 1659566063dSJacob Faibussowitsch PetscCall(MatPartitioningApply(part,&partitioning)); 1669566063dSJacob Faibussowitsch PetscCall(MatPartitioningDestroy(&part)); 1679566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(smat[0]))); 1689566063dSJacob Faibussowitsch PetscCall(PetscFree(smat)); 1692452736bSFande Kong /* get local rows including overlap */ 1709566063dSJacob Faibussowitsch PetscCall(ISBuildTwoSided(partitioning,is_sc,is)); 1719566063dSJacob Faibussowitsch PetscCall(ISDestroy(&is_sc)); 1729566063dSJacob Faibussowitsch PetscCall(ISDestroy(&partitioning)); 1739566063dSJacob Faibussowitsch PetscCall(PetscCommDestroy(&scomm)); 1742452736bSFande Kong PetscFunctionReturn(0); 1752452736bSFande Kong } 176