xref: /petsc/src/mat/utils/overlapsplit.c (revision b2566f29c2b6470df769aa9f7deb9e2726b0959e)
1 /*
2  * overlapsplit.c: increase the overlap of a 'big' subdomain across several processor cores
3  *
4  * Author: Fande Kong <fdkong.jd@gmail.com>
5  */
6 
7 #include <petscsf.h>
8 #include <petsc/private/matimpl.h>
9 
10 
11 #undef __FUNCT__
12 #define __FUNCT__ "MatIncreaseOverlapSplit_Single"
13 
14 /*
15  * Increase overlap for the sub-matrix across sub communicator
16  * sub-matrix could be a graph or numerical matrix
17  * */
18 PetscErrorCode  MatIncreaseOverlapSplit_Single(Mat mat,IS *is,PetscInt ov)
19 {
20   PetscInt         i,nindx,*indices_sc,*indices_ov,localsize,*localsizes_sc,localsize_tmp;
21   PetscInt         *indices_ov_rd,nroots,nleaves,*localoffsets,*indices_recv,*sources_sc,*sources_sc_rd;
22   const PetscInt   *indices;
23   PetscMPIInt      srank,ssize,issamecomm,k,grank;
24   IS               is_sc,allis_sc,partitioning;
25   MPI_Comm         gcomm,dcomm,scomm;
26   PetscSF          sf;
27   PetscSFNode      *remote;
28   Mat              *smat;
29   MatPartitioning  part;
30   PetscErrorCode   ierr;
31 
32   PetscFunctionBegin;
33   /* get a sub communicator before call individual MatIncreaseOverlap
34    * since the sub communicator may be changed.
35    * */
36   ierr = PetscObjectGetComm((PetscObject)(*is),&dcomm);CHKERRQ(ierr);
37   /* make a copy before the original one is deleted */
38   ierr = PetscCommDuplicate(dcomm,&scomm,NULL);CHKERRQ(ierr);
39   /* get a global communicator, where mat should be a global matrix  */
40   ierr = PetscObjectGetComm((PetscObject)mat,&gcomm);CHKERRQ(ierr);
41   ierr = (*mat->ops->increaseoverlap)(mat,1,is,ov);CHKERRQ(ierr);
42   ierr = MPI_Comm_compare(gcomm,scomm,&issamecomm);CHKERRQ(ierr);
43   /* if the sub-communicator is the same as the global communicator,
44    * user does not want to use a sub-communicator
45    * */
46   if(issamecomm == MPI_IDENT || issamecomm == MPI_CONGRUENT) PetscFunctionReturn(0);
47   /* if the sub-communicator is petsc_comm_self,
48    * user also does not care the sub-communicator
49    * */
50   ierr = MPI_Comm_compare(scomm,PETSC_COMM_SELF,&issamecomm);CHKERRQ(ierr);
51   if(issamecomm == MPI_IDENT || issamecomm == MPI_CONGRUENT){PetscFunctionReturn(0);}
52   ierr = MPI_Comm_rank(scomm,&srank);CHKERRQ(ierr);
53   ierr = MPI_Comm_size(scomm,&ssize);CHKERRQ(ierr);
54   ierr = MPI_Comm_rank(gcomm,&grank);CHKERRQ(ierr);
55   /* create a new IS based on sub-communicator
56    * since the old IS is often based on petsc_comm_self
57    * */
58   ierr = ISGetLocalSize(*is,&nindx);CHKERRQ(ierr);
59   ierr = PetscCalloc1(nindx,&indices_sc);CHKERRQ(ierr);
60   ierr = ISGetIndices(*is,&indices);CHKERRQ(ierr);
61   ierr = PetscMemcpy(indices_sc,indices,sizeof(PetscInt)*nindx);CHKERRQ(ierr);
62   ierr = ISRestoreIndices(*is,&indices);CHKERRQ(ierr);
63   /* we do not need any more */
64   ierr = ISDestroy(is);CHKERRQ(ierr);
65   /* create a index set based on the sub communicator  */
66   ierr = ISCreateGeneral(scomm,nindx,indices_sc,PETSC_OWN_POINTER,&is_sc);CHKERRQ(ierr);
67   /* gather all indices within  the sub communicator */
68   ierr = ISAllGather(is_sc,&allis_sc);CHKERRQ(ierr);
69   ierr = ISDestroy(&is_sc);CHKERRQ(ierr);
70   /* gather local sizes */
71   ierr = PetscMalloc1(ssize,&localsizes_sc);CHKERRQ(ierr);
72   /* get individual local sizes for all index sets */
73   ierr = MPI_Gather(&nindx,1,MPIU_INT,localsizes_sc,1,MPIU_INT,0,scomm);CHKERRQ(ierr);
74   /* only root does these computations */
75   if(!srank){
76    /* get local size for the big index set */
77    ierr = ISGetLocalSize(allis_sc,&localsize);CHKERRQ(ierr);
78    ierr = PetscCalloc2(localsize,&indices_ov,localsize,&sources_sc);CHKERRQ(ierr);
79    ierr = PetscCalloc2(localsize,&indices_ov_rd,localsize,&sources_sc_rd);CHKERRQ(ierr);
80    ierr = ISGetIndices(allis_sc,&indices);CHKERRQ(ierr);
81    ierr = PetscMemcpy(indices_ov,indices,sizeof(PetscInt)*localsize);CHKERRQ(ierr);
82    ierr = ISRestoreIndices(allis_sc,&indices);CHKERRQ(ierr);
83    ierr = ISDestroy(&allis_sc);CHKERRQ(ierr);
84    /* assign corresponding sources */
85    localsize_tmp = 0;
86    for(k=0; k<ssize; k++){
87      for(i=0; i<localsizes_sc[k]; i++){
88        sources_sc[localsize_tmp++] = k;
89      }
90    }
91    /* record where indices come from */
92    ierr = PetscSortIntWithArray(localsize,indices_ov,sources_sc);CHKERRQ(ierr);
93    /* count local sizes for reduced indices */
94    ierr = PetscMemzero(localsizes_sc,sizeof(PetscInt)*ssize);CHKERRQ(ierr);
95    /* initialize the first entity */
96    if(localsize){
97 	 indices_ov_rd[0] = indices_ov[0];
98 	 sources_sc_rd[0] = sources_sc[0];
99 	 localsizes_sc[sources_sc[0]]++;
100    }
101    localsize_tmp = 1;
102    /* remove duplicate integers */
103    for(i=1; i<localsize; i++){
104 	 if(indices_ov[i] != indices_ov[i-1]){
105 	   indices_ov_rd[localsize_tmp]   = indices_ov[i];
106 	   sources_sc_rd[localsize_tmp++] = sources_sc[i];
107 	   localsizes_sc[sources_sc[i]]++;
108 	 }
109    }
110    ierr = PetscFree2(indices_ov,sources_sc);CHKERRQ(ierr);
111    ierr = PetscCalloc1(ssize+1,&localoffsets);CHKERRQ(ierr);
112    for(k=0; k<ssize; k++){
113 	 localoffsets[k+1] = localoffsets[k] + localsizes_sc[k];
114    }
115    nleaves = localoffsets[ssize];
116    ierr = PetscMemzero(localoffsets,(ssize+1)*sizeof(PetscInt));CHKERRQ(ierr);
117    nroots  = localsizes_sc[srank];
118    ierr = PetscCalloc1(nleaves,&remote);CHKERRQ(ierr);
119    for(i=0; i<nleaves; i++){
120 	 remote[i].rank  = sources_sc_rd[i];
121 	 remote[i].index = localoffsets[sources_sc_rd[i]]++;
122    }
123    ierr = PetscFree(localoffsets);CHKERRQ(ierr);
124   }else{
125    ierr = ISDestroy(&allis_sc);CHKERRQ(ierr);
126    /* Allocate a 'zero' pointer to avoid using uninitialized variable  */
127    ierr = PetscCalloc1(0,&remote);CHKERRQ(ierr);
128    nleaves = 0;
129    indices_ov_rd = 0;
130    sources_sc_rd = 0;
131   }
132   /* scatter sizes to everybody */
133   ierr = MPI_Scatter(localsizes_sc,1, MPIU_INT,&nroots,1, MPIU_INT,0,scomm);CHKERRQ(ierr);
134   ierr = PetscFree(localsizes_sc);CHKERRQ(ierr);
135   ierr = PetscCalloc1(nroots,&indices_recv);CHKERRQ(ierr);
136   /* set data back to every body */
137   ierr = PetscSFCreate(scomm,&sf);CHKERRQ(ierr);
138   ierr = PetscSFSetType(sf,PETSCSFBASIC);CHKERRQ(ierr);
139   ierr = PetscSFSetFromOptions(sf);CHKERRQ(ierr);
140   ierr = PetscSFSetGraph(sf,nroots,nleaves,PETSC_NULL,PETSC_OWN_POINTER,remote,PETSC_OWN_POINTER);CHKERRQ(ierr);
141   ierr = PetscSFReduceBegin(sf,MPIU_INT,indices_ov_rd,indices_recv,MPIU_REPLACE);CHKERRQ(ierr);
142   ierr = PetscSFReduceEnd(sf,MPIU_INT,indices_ov_rd,indices_recv,MPIU_REPLACE);CHKERRQ(ierr);
143   ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
144   ierr = PetscFree2(indices_ov_rd,sources_sc_rd);CHKERRQ(ierr);
145   ierr = ISCreateGeneral(scomm,nroots,indices_recv,PETSC_OWN_POINTER,&is_sc);CHKERRQ(ierr);
146   ierr = MatGetSubMatricesMPI(mat,1,&is_sc,&is_sc,MAT_INITIAL_MATRIX,&smat);CHKERRQ(ierr);
147   ierr = ISDestroy(&allis_sc);CHKERRQ(ierr);
148   /* create a partitioner to repartition the sub-matrix */
149   ierr = MatPartitioningCreate(scomm,&part);CHKERRQ(ierr);
150   ierr = MatPartitioningSetAdjacency(part,smat[0]);CHKERRQ(ierr);
151 #if PETSC_HAVE_PARMETIS
152   /* if there exists a ParMETIS installation, we try to use ParMETIS
153    * because a repartition routine possibly work better
154    * */
155   ierr = MatPartitioningSetType(part,MATPARTITIONINGPARMETIS);CHKERRQ(ierr);
156   /* try to use reparition function, instead of partition function */
157   ierr = MatPartitioningParmetisSetRepartition(part);CHKERRQ(ierr);
158 #else
159   /* we at least provide a default partitioner to rebalance the computation  */
160   ierr = MatPartitioningSetType(part,MATPARTITIONINGAVERAGE);CHKERRQ(ierr);
161 #endif
162   /* user can pick up any partitioner by using an option */
163   ierr = MatPartitioningSetFromOptions(part);CHKERRQ(ierr);
164   ierr = MatPartitioningApply(part,&partitioning);CHKERRQ(ierr);
165   ierr = MatPartitioningDestroy(&part);CHKERRQ(ierr);
166   ierr = MatDestroy(&(smat[0]));CHKERRQ(ierr);
167   ierr = PetscFree(smat);CHKERRQ(ierr);
168   /* get local rows including  overlap */
169   ierr = ISBuildTwoSided(partitioning,is_sc,is);CHKERRQ(ierr);
170   ierr = ISDestroy(&is_sc);CHKERRQ(ierr);
171   ierr = ISDestroy(&partitioning);CHKERRQ(ierr);
172   ierr = PetscCommDestroy(&scomm);CHKERRQ(ierr);
173   PetscFunctionReturn(0);
174 }
175 
176 
177