xref: /petsc/src/mat/impls/aij/mpi/fdmpiaij.c (revision 5c8f6a953e7ed1c81f507d64aebddb11080b60e9)
1 
2 #include <../src/mat/impls/aij/mpi/mpiaij.h>
3 
4 extern PetscErrorCode MatCreateColmap_MPIAIJ_Private(Mat);
5 
6 #undef __FUNCT__
7 #define __FUNCT__ "MatFDColoringCreate_MPIAIJ"
8 PetscErrorCode MatFDColoringCreate_MPIAIJ(Mat mat,ISColoring iscoloring,MatFDColoring c)
9 {
10   Mat_MPIAIJ            *aij = (Mat_MPIAIJ*)mat->data;
11   PetscErrorCode        ierr;
12   PetscMPIInt           size,*ncolsonproc,*disp,nn;
13   PetscInt              i,n,nrows,j,k,m,ncols,col;
14   const PetscInt        *is,*A_ci,*A_cj,*B_ci,*B_cj,*rows = 0;
15   PetscInt              nis = iscoloring->n,nctot,*cols;
16   PetscInt              *rowhit,M,cstart,cend,colb;
17   PetscInt              *columnsforrow,l;
18   IS                    *isa;
19   PetscBool              done,flg;
20   ISLocalToGlobalMapping map = mat->cmap->mapping;
21   PetscInt               *ltog = (map ? map->indices : (PetscInt*) PETSC_NULL) ,ctype=c->ctype;
22 
23   PetscFunctionBegin;
24   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Matrix must be assembled first; MatAssemblyBegin/End();");
25   if (ctype == IS_COLORING_GHOSTED && !map) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_INCOMP,"When using ghosted differencing matrix must have local to global mapping provided with MatSetLocalToGlobalMapping");
26 
27   ierr = ISColoringGetIS(iscoloring,PETSC_IGNORE,&isa);CHKERRQ(ierr);
28 
29   M                = mat->rmap->n;
30   cstart           = mat->cmap->rstart;
31   cend             = mat->cmap->rend;
32   c->M             = mat->rmap->N;  /* set the global rows and columns and local rows */
33   c->N             = mat->cmap->N;
34   c->m             = mat->rmap->n;
35   c->rstart        = mat->rmap->rstart;
36 
37   c->ncolors       = nis;
38   ierr             = PetscMalloc(nis*sizeof(PetscInt),&c->ncolumns);CHKERRQ(ierr);
39   ierr             = PetscMalloc(nis*sizeof(PetscInt*),&c->columns);CHKERRQ(ierr);
40   ierr             = PetscMalloc(nis*sizeof(PetscInt),&c->nrows);CHKERRQ(ierr);
41   ierr             = PetscMalloc(nis*sizeof(PetscInt*),&c->rows);CHKERRQ(ierr);
42   ierr             = PetscMalloc(nis*sizeof(PetscInt*),&c->columnsforrow);CHKERRQ(ierr);
43   ierr = PetscLogObjectMemory(c,5*nis*sizeof(PetscInt));CHKERRQ(ierr);
44 
45   /* Allow access to data structures of local part of matrix */
46   if (!aij->colmap) {
47     ierr = MatCreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
48   }
49   ierr = MatGetColumnIJ(aij->A,0,PETSC_FALSE,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done);CHKERRQ(ierr);
50   ierr = MatGetColumnIJ(aij->B,0,PETSC_FALSE,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done);CHKERRQ(ierr);
51 
52   ierr = PetscMalloc((M+1)*sizeof(PetscInt),&rowhit);CHKERRQ(ierr);
53   ierr = PetscMalloc((M+1)*sizeof(PetscInt),&columnsforrow);CHKERRQ(ierr);
54 
55   for (i=0; i<nis; i++) {
56     ierr = ISGetLocalSize(isa[i],&n);CHKERRQ(ierr);
57     ierr = ISGetIndices(isa[i],&is);CHKERRQ(ierr);
58     c->ncolumns[i] = n;
59     if (n) {
60       ierr = PetscMalloc(n*sizeof(PetscInt),&c->columns[i]);CHKERRQ(ierr);
61       ierr = PetscLogObjectMemory(c,n*sizeof(PetscInt));CHKERRQ(ierr);
62       ierr = PetscMemcpy(c->columns[i],is,n*sizeof(PetscInt));CHKERRQ(ierr);
63     } else {
64       c->columns[i]  = 0;
65     }
66 
67     if (ctype == IS_COLORING_GLOBAL) {
68       /* Determine the total (parallel) number of columns of this color */
69       ierr = MPI_Comm_size(((PetscObject)mat)->comm,&size);CHKERRQ(ierr);
70       ierr = PetscMalloc2(size,PetscMPIInt,&ncolsonproc,size,PetscMPIInt,&disp);CHKERRQ(ierr);
71 
72       nn   = PetscMPIIntCast(n);
73       ierr = MPI_Allgather(&nn,1,MPI_INT,ncolsonproc,1,MPI_INT,((PetscObject)mat)->comm);CHKERRQ(ierr);
74       nctot = 0; for (j=0; j<size; j++) {nctot += ncolsonproc[j];}
75       if (!nctot) {
76         ierr = PetscInfo(mat,"Coloring of matrix has some unneeded colors with no corresponding rows\n");CHKERRQ(ierr);
77       }
78 
79       disp[0] = 0;
80       for (j=1; j<size; j++) {
81         disp[j] = disp[j-1] + ncolsonproc[j-1];
82       }
83 
84       /* Get complete list of columns for color on each processor */
85       ierr = PetscMalloc((nctot+1)*sizeof(PetscInt),&cols);CHKERRQ(ierr);
86       ierr = MPI_Allgatherv((void*)is,n,MPIU_INT,cols,ncolsonproc,disp,MPIU_INT,((PetscObject)mat)->comm);CHKERRQ(ierr);
87       ierr = PetscFree2(ncolsonproc,disp);CHKERRQ(ierr);
88     } else if (ctype == IS_COLORING_GHOSTED) {
89       /* Determine local number of columns of this color on this process, including ghost points */
90       nctot = n;
91       ierr = PetscMalloc((nctot+1)*sizeof(PetscInt),&cols);CHKERRQ(ierr);
92       ierr = PetscMemcpy(cols,is,n*sizeof(PetscInt));CHKERRQ(ierr);
93     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not provided for this MatFDColoring type");
94 
95     /*
96        Mark all rows affect by these columns
97     */
98     /* Temporary option to allow for debugging/testing */
99     flg  = PETSC_FALSE;
100     ierr = PetscOptionsGetBool(PETSC_NULL,"-matfdcoloring_slow",&flg,PETSC_NULL);CHKERRQ(ierr);
101     if (!flg) {/*-----------------------------------------------------------------------------*/
102       /* crude, fast version */
103       ierr = PetscMemzero(rowhit,M*sizeof(PetscInt));CHKERRQ(ierr);
104       /* loop over columns*/
105       for (j=0; j<nctot; j++) {
106         if (ctype == IS_COLORING_GHOSTED) {
107           col = ltog[cols[j]];
108         } else {
109           col  = cols[j];
110         }
111         if (col >= cstart && col < cend) {
112           /* column is in diagonal block of matrix */
113           rows = A_cj + A_ci[col-cstart];
114           m    = A_ci[col-cstart+1] - A_ci[col-cstart];
115         } else {
116 #if defined (PETSC_USE_CTABLE)
117           ierr = PetscTableFind(aij->colmap,col+1,&colb);CHKERRQ(ierr);
118           colb --;
119 #else
120           colb = aij->colmap[col] - 1;
121 #endif
122           if (colb == -1) {
123             m = 0;
124           } else {
125             rows = B_cj + B_ci[colb];
126             m    = B_ci[colb+1] - B_ci[colb];
127           }
128         }
129         /* loop over columns marking them in rowhit */
130         for (k=0; k<m; k++) {
131           rowhit[*rows++] = col + 1;
132         }
133       }
134 
135       /* count the number of hits */
136       nrows = 0;
137       for (j=0; j<M; j++) {
138         if (rowhit[j]) nrows++;
139       }
140       c->nrows[i]         = nrows;
141       ierr                = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->rows[i]);CHKERRQ(ierr);
142       ierr                = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->columnsforrow[i]);CHKERRQ(ierr);
143       ierr = PetscLogObjectMemory(c,2*(nrows+1)*sizeof(PetscInt));CHKERRQ(ierr);
144       nrows = 0;
145       for (j=0; j<M; j++) {
146         if (rowhit[j]) {
147           c->rows[i][nrows]           = j;
148           c->columnsforrow[i][nrows] = rowhit[j] - 1;
149           nrows++;
150         }
151       }
152     } else {/*-------------------------------------------------------------------------------*/
153       /* slow version, using rowhit as a linked list */
154       PetscInt currentcol,fm,mfm;
155       rowhit[M] = M;
156       nrows     = 0;
157       /* loop over columns*/
158       for (j=0; j<nctot; j++) {
159         if (ctype == IS_COLORING_GHOSTED) {
160           col = ltog[cols[j]];
161         } else {
162           col  = cols[j];
163         }
164         if (col >= cstart && col < cend) {
165           /* column is in diagonal block of matrix */
166           rows = A_cj + A_ci[col-cstart];
167           m    = A_ci[col-cstart+1] - A_ci[col-cstart];
168         } else {
169 #if defined (PETSC_USE_CTABLE)
170           ierr = PetscTableFind(aij->colmap,col+1,&colb);CHKERRQ(ierr);
171           colb --;
172 #else
173           colb = aij->colmap[col] - 1;
174 #endif
175           if (colb == -1) {
176             m = 0;
177           } else {
178             rows = B_cj + B_ci[colb];
179             m    = B_ci[colb+1] - B_ci[colb];
180           }
181         }
182 
183         /* loop over columns marking them in rowhit */
184         fm    = M; /* fm points to first entry in linked list */
185         for (k=0; k<m; k++) {
186           currentcol = *rows++;
187           /* is it already in the list? */
188           do {
189             mfm  = fm;
190             fm   = rowhit[fm];
191           } while (fm < currentcol);
192           /* not in list so add it */
193           if (fm != currentcol) {
194             nrows++;
195             columnsforrow[currentcol] = col;
196             /* next three lines insert new entry into linked list */
197             rowhit[mfm]               = currentcol;
198             rowhit[currentcol]        = fm;
199             fm                        = currentcol;
200             /* fm points to present position in list since we know the columns are sorted */
201           } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Invalid coloring of matrix detected");
202         }
203       }
204       c->nrows[i]         = nrows;
205       ierr = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->rows[i]);CHKERRQ(ierr);
206       ierr = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->columnsforrow[i]);CHKERRQ(ierr);
207       ierr = PetscLogObjectMemory(c,(nrows+1)*sizeof(PetscInt));CHKERRQ(ierr);
208       /* now store the linked list of rows into c->rows[i] */
209       nrows = 0;
210       fm    = rowhit[M];
211       do {
212         c->rows[i][nrows]            = fm;
213         c->columnsforrow[i][nrows++] = columnsforrow[fm];
214         fm                           = rowhit[fm];
215       } while (fm < M);
216     } /* ---------------------------------------------------------------------------------------*/
217     ierr = PetscFree(cols);CHKERRQ(ierr);
218   }
219 
220   /* Optimize by adding the vscale, and scaleforrow[][] fields */
221   /*
222        vscale will contain the "diagonal" on processor scalings followed by the off processor
223   */
224   if (ctype == IS_COLORING_GLOBAL) {
225     ierr = VecCreateGhost(((PetscObject)mat)->comm,aij->A->rmap->n,PETSC_DETERMINE,aij->B->cmap->n,aij->garray,&c->vscale);CHKERRQ(ierr);
226     ierr = PetscMalloc(c->ncolors*sizeof(PetscInt*),&c->vscaleforrow);CHKERRQ(ierr);
227     for (k=0; k<c->ncolors; k++) {
228       ierr = PetscMalloc((c->nrows[k]+1)*sizeof(PetscInt),&c->vscaleforrow[k]);CHKERRQ(ierr);
229       for (l=0; l<c->nrows[k]; l++) {
230         col = c->columnsforrow[k][l];
231         if (col >= cstart && col < cend) {
232           /* column is in diagonal block of matrix */
233           colb = col - cstart;
234         } else {
235           /* column  is in "off-processor" part */
236 #if defined (PETSC_USE_CTABLE)
237           ierr = PetscTableFind(aij->colmap,col+1,&colb);CHKERRQ(ierr);
238           colb --;
239 #else
240           colb = aij->colmap[col] - 1;
241 #endif
242           colb += cend - cstart;
243         }
244         c->vscaleforrow[k][l] = colb;
245       }
246     }
247   } else if (ctype == IS_COLORING_GHOSTED) {
248     /* Get gtol mapping */
249     PetscInt N = mat->cmap->N, *gtol;
250     ierr = PetscMalloc((N+1)*sizeof(PetscInt),&gtol);CHKERRQ(ierr);
251     for (i=0; i<N; i++) gtol[i] = -1;
252     for (i=0; i<map->n; i++) gtol[ltog[i]] = i;
253 
254     c->vscale = 0; /* will be created in MatFDColoringApply() */
255     ierr = PetscMalloc(c->ncolors*sizeof(PetscInt*),&c->vscaleforrow);CHKERRQ(ierr);
256     for (k=0; k<c->ncolors; k++) {
257       ierr = PetscMalloc((c->nrows[k]+1)*sizeof(PetscInt),&c->vscaleforrow[k]);CHKERRQ(ierr);
258       for (l=0; l<c->nrows[k]; l++) {
259         col = c->columnsforrow[k][l];      /* global column index */
260         c->vscaleforrow[k][l] = gtol[col]; /* local column index */
261       }
262     }
263     ierr = PetscFree(gtol);CHKERRQ(ierr);
264   }
265   ierr = ISColoringRestoreIS(iscoloring,&isa);CHKERRQ(ierr);
266 
267   ierr = PetscFree(rowhit);CHKERRQ(ierr);
268   ierr = PetscFree(columnsforrow);CHKERRQ(ierr);
269   ierr = MatRestoreColumnIJ(aij->A,0,PETSC_FALSE,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done);CHKERRQ(ierr);
270   ierr = MatRestoreColumnIJ(aij->B,0,PETSC_FALSE,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done);CHKERRQ(ierr);
271   PetscFunctionReturn(0);
272 }
273 
274 
275 
276 
277 
278 
279