xref: /petsc/src/mat/impls/aij/mpi/fdmpiaij.c (revision 4b2e90ca22e5d236f72fef601ed2fa3dbdcf4eae)
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__ "MatFDColoringApply_MPIAIJ"
8 PetscErrorCode  MatFDColoringApply_MPIAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
9 {
10   PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void*))coloring->f;
11   PetscErrorCode ierr;
12   PetscInt       k,cstart,cend,l,row,col,nz;
13   PetscScalar    dx=0.0,*y,*xx,*w3_array;
14   PetscScalar    *vscale_array;
15   PetscReal      epsilon=coloring->error_rel,umin=coloring->umin,unorm;
16   Vec            w1=coloring->w1,w2=coloring->w2,w3,vscale=coloring->vscale;
17   void           *fctx=coloring->fctx;
18   PetscBool      flg=PETSC_FALSE;
19   PetscInt       ctype=coloring->ctype,nxloc;
20   Mat_MPIAIJ     *aij=(Mat_MPIAIJ*)J->data;
21   MatEntry       *Jentry=coloring->matentry;
22   const PetscInt ncolors=coloring->ncolors,*ncolumns=coloring->ncolumns,*nrows=coloring->nrows;
23 
24   PetscFunctionBegin;
25   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
26   ierr = PetscOptionsGetBool(NULL,"-mat_fd_coloring_dont_rezero",&flg,NULL);CHKERRQ(ierr);
27   if (flg) {
28     ierr = PetscInfo(coloring,"Not calling MatZeroEntries()\n");CHKERRQ(ierr);
29   } else {
30     PetscBool assembled;
31     ierr = MatAssembled(J,&assembled);CHKERRQ(ierr);
32     if (assembled) {
33       ierr = MatZeroEntries(J);CHKERRQ(ierr);
34     }
35   }
36 
37   /* create vscale for storing dx */
38   if (!vscale) {
39     if (ctype == IS_COLORING_GLOBAL && coloring->htype[0] == 'd') {
40       ierr = VecCreateGhost(PetscObjectComm((PetscObject)J),J->cmap->n,PETSC_DETERMINE,aij->B->cmap->n,aij->garray,&vscale);CHKERRQ(ierr);
41     } else if (ctype == IS_COLORING_GHOSTED) {
42       ierr = VecDuplicate(x1,&vscale);CHKERRQ(ierr);
43     }
44     coloring->vscale = vscale;
45   }
46 
47   /* (1) Set w1 = F(x1) */
48   if (!coloring->fset) {
49     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
50     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
51     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
52   } else {
53     coloring->fset = PETSC_FALSE;
54   }
55 
56   /* (2) Compute vscale = 1./dx - the local scale factors, including ghost points */
57   if (coloring->htype[0] == 'w') {
58     /* vscale = dx is a constant scalar */
59     ierr = VecNorm(x1,NORM_2,&unorm);CHKERRQ(ierr);
60     dx = 1.0/((1.0 + unorm)*epsilon);
61   } else {
62     ierr = VecGetLocalSize(x1,&nxloc);CHKERRQ(ierr);
63     ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);
64     ierr = VecGetArray(vscale,&vscale_array);CHKERRQ(ierr);
65     for (col=0; col<nxloc; col++) {
66       dx = xx[col];
67       if (PetscAbsScalar(dx) < umin) {
68         if (PetscRealPart(dx) >= 0.0)      dx = umin;
69         else if (PetscRealPart(dx) < 0.0 ) dx = -umin;
70       }
71       dx               *= epsilon;
72       vscale_array[col] = 1.0/dx;
73     }
74     ierr = VecRestoreArray(x1,&xx);CHKERRQ(ierr);
75     ierr = VecRestoreArray(vscale,&vscale_array);CHKERRQ(ierr);
76   }
77   if (ctype == IS_COLORING_GLOBAL && coloring->htype[0] != 'w') {
78     ierr = VecGhostUpdateBegin(vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
79     ierr = VecGhostUpdateEnd(vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
80   }
81 
82   /* (3) Loop over each color */
83   if (!coloring->w3) {
84     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
85     ierr = PetscLogObjectParent((PetscObject)coloring,(PetscObject)coloring->w3);CHKERRQ(ierr);
86   }
87   w3 = coloring->w3;
88 
89   ierr = VecGetOwnershipRange(x1,&cstart,&cend);CHKERRQ(ierr); /* used by ghosted vscale */
90   if (vscale) {
91     ierr = VecGetArray(vscale,&vscale_array);CHKERRQ(ierr);
92   }
93   nz   = 0;
94   for (k=0; k<ncolors; k++) {
95     coloring->currentcolor = k;
96 
97     /*
98       (3-1) Loop over each column associated with color
99       adding the perturbation to the vector w3 = x1 + dx.
100     */
101     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
102     ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);
103     if (ctype == IS_COLORING_GLOBAL) w3_array -= cstart; /* shift pointer so global index can be used */
104     if (coloring->htype[0] == 'w') {
105       for (l=0; l<ncolumns[k]; l++) {
106         col = coloring->columns[k][l]; /* local column (in global index!) of the matrix we are probing for */
107         w3_array[col] += 1.0/dx;
108       }
109     } else { /* htype == 'ds' */
110       vscale_array -= cstart; /* shift pointer so global index can be used */
111       for (l=0; l<ncolumns[k]; l++) {
112         col = coloring->columns[k][l]; /* local column (in global index!) of the matrix we are probing for */
113         w3_array[col] += 1.0/vscale_array[col];
114       }
115       vscale_array += cstart;
116     }
117     if (ctype == IS_COLORING_GLOBAL) w3_array += cstart;
118     ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
119 
120     /*
121       (3-2) Evaluate function at w3 = x1 + dx (here dx is a vector of perturbations)
122                            w2 = F(x1 + dx) - F(x1)
123     */
124     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
125     ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
126     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
127     ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr);
128 
129     /*
130      (3-3) Loop over rows of vector, putting results into Jacobian matrix
131     */
132     ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
133     if (coloring->htype[0] == 'w') {
134       for (l=0; l<nrows[k]; l++) {
135         row                     = Jentry[nz].row;   /* local row index */
136         *(Jentry[nz++].valaddr) = y[row]*dx;
137       }
138     } else { /* htype == 'ds' */
139       for (l=0; l<nrows[k]; l++) {
140         row                   = Jentry[nz].row;   /* local row index */
141         *(Jentry[nz].valaddr) = y[row]*vscale_array[Jentry[nz].col];
142         nz++;
143       }
144     }
145     ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
146   }
147   ierr = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
148   ierr = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
149   if (vscale) {
150     ierr = VecRestoreArray(vscale,&vscale_array);CHKERRQ(ierr);
151   }
152 
153   coloring->currentcolor = -1;
154   PetscFunctionReturn(0);
155 }
156 
157 #undef __FUNCT__
158 #define __FUNCT__ "MatFDColoringCreate_MPIAIJ_new"
159 PetscErrorCode MatFDColoringCreate_MPIAIJ_new(Mat mat,ISColoring iscoloring,MatFDColoring c)
160 {
161   Mat_MPIAIJ             *aij=(Mat_MPIAIJ*)mat->data;
162   PetscErrorCode         ierr;
163   PetscMPIInt            size,*ncolsonproc,*disp,nn;
164   PetscInt               i,n,nrows,nrows_i,j,k,m,ncols,col;
165   const PetscInt         *is,*A_ci,*A_cj,*B_ci,*B_cj,*row = NULL,*ltog=NULL;
166   PetscInt               nis=iscoloring->n,nctot,*cols;
167   PetscInt               *rowhit,cstart,cend,colb;
168   IS                     *isa;
169   ISLocalToGlobalMapping map=mat->cmap->mapping;
170   PetscInt               ctype=c->ctype;
171   Mat                    A=aij->A,B=aij->B;
172   Mat_SeqAIJ             *spA=(Mat_SeqAIJ*)A->data,*spB=(Mat_SeqAIJ*)B->data;
173   PetscScalar            *A_val=spA->a,*B_val=spB->a;
174   PetscInt               spidx;
175   PetscInt               *spidxA,*spidxB,nz;
176   PetscScalar            **valaddrhit;
177   MatEntry               *Jentry;
178 
179   PetscFunctionBegin;
180   if (ctype == IS_COLORING_GHOSTED) {
181     if (!map) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_INCOMP,"When using ghosted differencing matrix must have local to global mapping provided with MatSetLocalToGlobalMapping");
182     ierr = ISLocalToGlobalMappingGetIndices(map,&ltog);CHKERRQ(ierr);
183   }
184 
185   m         = mat->rmap->n;
186   cstart    = mat->cmap->rstart;
187   cend      = mat->cmap->rend;
188   c->M      = mat->rmap->N;         /* set the global rows and columns and local rows */
189   c->N      = mat->cmap->N;
190   c->m      = m;
191   c->rstart = mat->rmap->rstart;
192 
193   c->ncolors = nis;
194   ierr       = PetscMalloc(nis*sizeof(PetscInt),&c->ncolumns);CHKERRQ(ierr);
195   ierr       = PetscMalloc(nis*sizeof(PetscInt*),&c->columns);CHKERRQ(ierr);
196   ierr       = PetscMalloc(nis*sizeof(PetscInt),&c->nrows);CHKERRQ(ierr);
197   ierr       = PetscLogObjectMemory((PetscObject)c,3*nis*sizeof(PetscInt));CHKERRQ(ierr);
198 
199   nz         = spA->nz + spB->nz; /* total nonzero entries of mat */
200   ierr       = PetscMalloc(nz*sizeof(MatEntry),&Jentry);CHKERRQ(ierr);
201   ierr       = PetscLogObjectMemory((PetscObject)c,nz*sizeof(MatEntry));CHKERRQ(ierr);
202   c->matentry = Jentry;
203 
204   /* Allow access to data structures of local part of matrix
205    - creates aij->colmap which maps global column number to local number in part B */
206   if (!aij->colmap) {
207     ierr = MatCreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
208   }
209   ierr = MatGetColumnIJ_SeqAIJ_Color(aij->A,0,PETSC_FALSE,PETSC_FALSE,&ncols,&A_ci,&A_cj,&spidxA,NULL);CHKERRQ(ierr);
210   ierr = MatGetColumnIJ_SeqAIJ_Color(aij->B,0,PETSC_FALSE,PETSC_FALSE,&ncols,&B_ci,&B_cj,&spidxB,NULL);CHKERRQ(ierr);
211 
212   ierr = PetscMalloc2(m+1,PetscInt,&rowhit,m+1,PetscScalar*,&valaddrhit);CHKERRQ(ierr);
213   nz = 0;
214   ierr = ISColoringGetIS(iscoloring,PETSC_IGNORE,&isa);CHKERRQ(ierr);
215   for (i=0; i<nis; i++) { /* for each local color */
216     ierr = ISGetLocalSize(isa[i],&n);CHKERRQ(ierr);
217     ierr = ISGetIndices(isa[i],&is);CHKERRQ(ierr);
218 
219     c->ncolumns[i] = n; /* local number of columns of this color on this process */
220     if (n) {
221       ierr = PetscMalloc(n*sizeof(PetscInt),&c->columns[i]);CHKERRQ(ierr);
222       ierr = PetscLogObjectMemory((PetscObject)c,n*sizeof(PetscInt));CHKERRQ(ierr);
223       ierr = PetscMemcpy(c->columns[i],is,n*sizeof(PetscInt));CHKERRQ(ierr);
224       /* convert global column indices to local ones! */
225 
226     } else {
227       c->columns[i] = 0;
228     }
229 
230     if (ctype == IS_COLORING_GLOBAL) {
231       /* Determine nctot, the total (parallel) number of columns of this color */
232       ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);CHKERRQ(ierr);
233       ierr = PetscMalloc2(size,PetscMPIInt,&ncolsonproc,size,PetscMPIInt,&disp);CHKERRQ(ierr);
234 
235       /* ncolsonproc[j]: local ncolumns on proc[j] of this color */
236       ierr  = PetscMPIIntCast(n,&nn);CHKERRQ(ierr);
237       ierr  = MPI_Allgather(&nn,1,MPI_INT,ncolsonproc,1,MPI_INT,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
238       nctot = 0; for (j=0; j<size; j++) nctot += ncolsonproc[j];
239       if (!nctot) {
240         ierr = PetscInfo(mat,"Coloring of matrix has some unneeded colors with no corresponding rows\n");CHKERRQ(ierr);
241       }
242 
243       disp[0] = 0;
244       for (j=1; j<size; j++) {
245         disp[j] = disp[j-1] + ncolsonproc[j-1];
246       }
247 
248       /* Get cols, the complete list of columns for this color on each process */
249       ierr = PetscMalloc((nctot+1)*sizeof(PetscInt),&cols);CHKERRQ(ierr);
250       ierr = MPI_Allgatherv((void*)is,n,MPIU_INT,cols,ncolsonproc,disp,MPIU_INT,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
251       ierr = PetscFree2(ncolsonproc,disp);CHKERRQ(ierr);
252     } else if (ctype == IS_COLORING_GHOSTED) {
253       /* Determine local number of columns of this color on this process, including ghost points */
254       nctot = n;
255       ierr  = PetscMalloc((nctot+1)*sizeof(PetscInt),&cols);CHKERRQ(ierr);
256       ierr  = PetscMemcpy(cols,is,n*sizeof(PetscInt));CHKERRQ(ierr);
257     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not provided for this MatFDColoring type");
258 
259     /* Mark all rows affect by these columns */
260     ierr = PetscMemzero(rowhit,m*sizeof(PetscInt));CHKERRQ(ierr);
261     nrows_i = 0;
262     for (j=0; j<nctot; j++) { /* loop over columns*/
263       if (ctype == IS_COLORING_GHOSTED) {
264         col = ltog[cols[j]];
265       } else {
266         col = cols[j];
267       }
268       if (col >= cstart && col < cend) { /* column is in diagonal block of matrix A */
269         row      = A_cj + A_ci[col-cstart];
270         nrows    = A_ci[col-cstart+1] - A_ci[col-cstart];
271         nrows_i += nrows;
272         /* loop over columns of A marking them in rowhit */
273         for (k=0; k<nrows; k++) {
274           /* set valaddrhit for part A */
275           spidx            = spidxA[A_ci[col-cstart] + k];
276           valaddrhit[*row] = &A_val[spidx];
277           rowhit[*row++]   = col - cstart + 1; /* local column index */
278         }
279       } else { /* column is in off-diagonal block of matrix B */
280 #if defined(PETSC_USE_CTABLE)
281         ierr = PetscTableFind(aij->colmap,col+1,&colb);CHKERRQ(ierr);
282         colb--;
283 #else
284         colb = aij->colmap[col] - 1; /* local column index */
285 #endif
286         if (colb == -1) {
287           nrows = 0;
288         } else {
289           row   = B_cj + B_ci[colb];
290           nrows = B_ci[colb+1] - B_ci[colb];
291         }
292         nrows_i += nrows;
293         /* loop over columns of B marking them in rowhit */
294         for (k=0; k<nrows; k++) {
295           /* set valaddrhit for part B */
296           spidx            = spidxB[B_ci[colb] + k];
297           valaddrhit[*row] = &B_val[spidx];
298           rowhit[*row++]   = colb + 1 + cend - cstart; /* local column index */
299         }
300       }
301     }
302     c->nrows[i] = nrows_i;
303 
304     for (j=0; j<m; j++) {
305       if (rowhit[j]) {
306         Jentry[nz].row     = j;              /* local row index */
307         Jentry[nz].col     = rowhit[j] - 1;  /* local column index */
308         Jentry[nz].valaddr = valaddrhit[j];  /* address of mat value for this entry */
309         nz++;
310       }
311     }
312     ierr = PetscFree(cols);CHKERRQ(ierr);
313   }
314   ierr = ISColoringRestoreIS(iscoloring,&isa);CHKERRQ(ierr);
315   if (nz != spA->nz + spB->nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"nz %d != mat->nz %d\n",nz,spA->nz+spB->nz);
316 
317   ierr = PetscFree2(rowhit,valaddrhit);CHKERRQ(ierr);
318   ierr = MatRestoreColumnIJ_SeqAIJ_Color(aij->A,0,PETSC_FALSE,PETSC_FALSE,&ncols,&A_ci,&A_cj,&spidxA,NULL);CHKERRQ(ierr);
319   ierr = MatRestoreColumnIJ_SeqAIJ_Color(aij->B,0,PETSC_FALSE,PETSC_FALSE,&ncols,&B_ci,&B_cj,&spidxB,NULL);CHKERRQ(ierr);
320   if (ctype == IS_COLORING_GHOSTED) {
321     ierr = ISLocalToGlobalMappingRestoreIndices(map,&ltog);CHKERRQ(ierr);
322   }
323 
324   mat->ops->fdcoloringapply = MatFDColoringApply_MPIAIJ;
325   PetscFunctionReturn(0);
326 }
327 
328 /*------------------------------------------------------*/
329 #undef __FUNCT__
330 #define __FUNCT__ "MatFDColoringCreate_MPIAIJ"
331 PetscErrorCode MatFDColoringCreate_MPIAIJ(Mat mat,ISColoring iscoloring,MatFDColoring c)
332 {
333   Mat_MPIAIJ             *aij = (Mat_MPIAIJ*)mat->data;
334   PetscErrorCode         ierr;
335   PetscMPIInt            size,*ncolsonproc,*disp,nn;
336   PetscInt               i,n,nrows,j,k,m,ncols,col;
337   const PetscInt         *is,*A_ci,*A_cj,*B_ci,*B_cj,*rows = 0,*ltog;
338   PetscInt               nis = iscoloring->n,nctot,*cols;
339   PetscInt               *rowhit,M,cstart,cend,colb;
340   PetscInt               *columnsforrow,l;
341   IS                     *isa;
342   PetscBool              done,flg;
343   ISLocalToGlobalMapping map   = mat->cmap->mapping;
344   PetscInt               ctype=c->ctype;
345   PetscBool              new_impl=PETSC_FALSE;
346 
347   PetscFunctionBegin;
348   ierr = PetscOptionsName("-new","using new impls","",&new_impl);CHKERRQ(ierr);
349   if (new_impl){
350     ierr =  MatFDColoringCreate_MPIAIJ_new(mat,iscoloring,c);CHKERRQ(ierr);
351     PetscFunctionReturn(0);
352   }
353   if (ctype == IS_COLORING_GHOSTED && !map) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_INCOMP,"When using ghosted differencing matrix must have local to global mapping provided with MatSetLocalToGlobalMapping");
354 
355   if (map) {ierr = ISLocalToGlobalMappingGetIndices(map,&ltog);CHKERRQ(ierr);}
356   else     ltog = NULL;
357   ierr = ISColoringGetIS(iscoloring,PETSC_IGNORE,&isa);CHKERRQ(ierr);
358 
359   M         = mat->rmap->n;
360   cstart    = mat->cmap->rstart;
361   cend      = mat->cmap->rend;
362   c->M      = mat->rmap->N;         /* set the global rows and columns and local rows */
363   c->N      = mat->cmap->N;
364   c->m      = mat->rmap->n;
365   c->rstart = mat->rmap->rstart;
366 
367   c->ncolors = nis;
368   ierr       = PetscMalloc(nis*sizeof(PetscInt),&c->ncolumns);CHKERRQ(ierr);
369   ierr       = PetscMalloc(nis*sizeof(PetscInt*),&c->columns);CHKERRQ(ierr);
370   ierr       = PetscMalloc(nis*sizeof(PetscInt),&c->nrows);CHKERRQ(ierr);
371   ierr       = PetscMalloc(nis*sizeof(PetscInt*),&c->rows);CHKERRQ(ierr);
372   ierr       = PetscMalloc(nis*sizeof(PetscInt*),&c->columnsforrow);CHKERRQ(ierr);
373   ierr       = PetscLogObjectMemory((PetscObject)c,5*nis*sizeof(PetscInt));CHKERRQ(ierr);
374 
375   /* Allow access to data structures of local part of matrix */
376   if (!aij->colmap) {
377     ierr = MatCreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
378   }
379   ierr = MatGetColumnIJ(aij->A,0,PETSC_FALSE,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done);CHKERRQ(ierr);
380   ierr = MatGetColumnIJ(aij->B,0,PETSC_FALSE,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done);CHKERRQ(ierr);
381 
382   ierr = PetscMalloc((M+1)*sizeof(PetscInt),&rowhit);CHKERRQ(ierr);
383   ierr = PetscMalloc((M+1)*sizeof(PetscInt),&columnsforrow);CHKERRQ(ierr);
384 
385   for (i=0; i<nis; i++) {
386     ierr = ISGetLocalSize(isa[i],&n);CHKERRQ(ierr);
387     ierr = ISGetIndices(isa[i],&is);CHKERRQ(ierr);
388 
389     c->ncolumns[i] = n; /* local number of columns of this color on this process */
390     if (n) {
391       ierr = PetscMalloc(n*sizeof(PetscInt),&c->columns[i]);CHKERRQ(ierr);
392       ierr = PetscLogObjectMemory((PetscObject)c,n*sizeof(PetscInt));CHKERRQ(ierr);
393       ierr = PetscMemcpy(c->columns[i],is,n*sizeof(PetscInt));CHKERRQ(ierr);
394     } else {
395       c->columns[i] = 0;
396     }
397 
398     if (ctype == IS_COLORING_GLOBAL) {
399       /* Determine the total (parallel) number of columns of this color */
400       ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);CHKERRQ(ierr);
401       ierr = PetscMalloc2(size,PetscMPIInt,&ncolsonproc,size,PetscMPIInt,&disp);CHKERRQ(ierr);
402 
403       ierr  = PetscMPIIntCast(n,&nn);CHKERRQ(ierr);
404       ierr  = MPI_Allgather(&nn,1,MPI_INT,ncolsonproc,1,MPI_INT,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
405       nctot = 0; for (j=0; j<size; j++) nctot += ncolsonproc[j];
406       if (!nctot) {
407         ierr = PetscInfo(mat,"Coloring of matrix has some unneeded colors with no corresponding rows\n");CHKERRQ(ierr);
408       }
409 
410       disp[0] = 0;
411       for (j=1; j<size; j++) {
412         disp[j] = disp[j-1] + ncolsonproc[j-1];
413       }
414 
415       /* Get complete list of columns for color on each processor */
416       ierr = PetscMalloc((nctot+1)*sizeof(PetscInt),&cols);CHKERRQ(ierr);
417       ierr = MPI_Allgatherv((void*)is,n,MPIU_INT,cols,ncolsonproc,disp,MPIU_INT,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
418       ierr = PetscFree2(ncolsonproc,disp);CHKERRQ(ierr);
419     } else if (ctype == IS_COLORING_GHOSTED) {
420       /* Determine local number of columns of this color on this process, including ghost points */
421       nctot = n;
422       ierr  = PetscMalloc((nctot+1)*sizeof(PetscInt),&cols);CHKERRQ(ierr);
423       ierr  = PetscMemcpy(cols,is,n*sizeof(PetscInt));CHKERRQ(ierr);
424     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not provided for this MatFDColoring type");
425 
426     /*
427        Mark all rows affect by these columns
428     */
429     /* Temporary option to allow for debugging/testing */
430     flg  = PETSC_FALSE;
431     ierr = PetscOptionsGetBool(NULL,"-matfdcoloring_slow",&flg,NULL);CHKERRQ(ierr);
432     if (!flg) { /*-----------------------------------------------------------------------------*/
433       /* crude, fast version */
434       ierr = PetscMemzero(rowhit,M*sizeof(PetscInt));CHKERRQ(ierr);
435       /* loop over columns*/
436       for (j=0; j<nctot; j++) {
437         if (ctype == IS_COLORING_GHOSTED) {
438           col = ltog[cols[j]];
439         } else {
440           col = cols[j];
441         }
442         if (col >= cstart && col < cend) {
443           /* column is in diagonal block of matrix */
444           rows = A_cj + A_ci[col-cstart];
445           m    = A_ci[col-cstart+1] - A_ci[col-cstart];
446         } else {
447 #if defined(PETSC_USE_CTABLE)
448           ierr = PetscTableFind(aij->colmap,col+1,&colb);CHKERRQ(ierr);
449           colb--;
450 #else
451           colb = aij->colmap[col] - 1;
452 #endif
453           if (colb == -1) {
454             m = 0;
455           } else {
456             rows = B_cj + B_ci[colb];
457             m    = B_ci[colb+1] - B_ci[colb];
458           }
459         }
460         /* loop over columns marking them in rowhit */
461         for (k=0; k<m; k++) {
462           rowhit[*rows++] = col + 1;
463         }
464       }
465 
466       /* count the number of hits */
467       nrows = 0;
468       for (j=0; j<M; j++) {
469         if (rowhit[j]) nrows++;
470       }
471       c->nrows[i] = nrows;
472       ierr        = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->rows[i]);CHKERRQ(ierr);
473       ierr        = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->columnsforrow[i]);CHKERRQ(ierr);
474       ierr        = PetscLogObjectMemory((PetscObject)c,2*(nrows+1)*sizeof(PetscInt));CHKERRQ(ierr);
475       nrows       = 0;
476       for (j=0; j<M; j++) {
477         if (rowhit[j]) {
478           c->rows[i][nrows]          = j;              /* local row index */
479           c->columnsforrow[i][nrows] = rowhit[j] - 1;  /* global column index */
480           nrows++;
481         }
482       }
483     } else { /*-------------------------------------------------------------------------------*/
484       /* slow version, using rowhit as a linked list */
485       PetscInt currentcol,fm,mfm;
486       rowhit[M] = M;
487       nrows     = 0;
488       /* loop over columns*/
489       for (j=0; j<nctot; j++) {
490         if (ctype == IS_COLORING_GHOSTED) {
491           col = ltog[cols[j]];
492         } else {
493           col = cols[j];
494         }
495         if (col >= cstart && col < cend) {
496           /* column is in diagonal block of matrix */
497           rows = A_cj + A_ci[col-cstart];
498           m    = A_ci[col-cstart+1] - A_ci[col-cstart];
499         } else {
500 #if defined(PETSC_USE_CTABLE)
501           ierr = PetscTableFind(aij->colmap,col+1,&colb);CHKERRQ(ierr);
502           colb--;
503 #else
504           colb = aij->colmap[col] - 1;
505 #endif
506           if (colb == -1) {
507             m = 0;
508           } else {
509             rows = B_cj + B_ci[colb];
510             m    = B_ci[colb+1] - B_ci[colb];
511           }
512         }
513 
514         /* loop over columns marking them in rowhit */
515         fm = M;    /* fm points to first entry in linked list */
516         for (k=0; k<m; k++) {
517           currentcol = *rows++;
518           /* is it already in the list? */
519           do {
520             mfm = fm;
521             fm  = rowhit[fm];
522           } while (fm < currentcol);
523           /* not in list so add it */
524           if (fm != currentcol) {
525             nrows++;
526             columnsforrow[currentcol] = col;
527             /* next three lines insert new entry into linked list */
528             rowhit[mfm]        = currentcol;
529             rowhit[currentcol] = fm;
530             fm                 = currentcol;
531             /* fm points to present position in list since we know the columns are sorted */
532           } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Invalid coloring of matrix detected");
533         }
534       }
535       c->nrows[i] = nrows;
536 
537       ierr = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->rows[i]);CHKERRQ(ierr);
538       ierr = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->columnsforrow[i]);CHKERRQ(ierr);
539       ierr = PetscLogObjectMemory((PetscObject)c,(nrows+1)*sizeof(PetscInt));CHKERRQ(ierr);
540       /* now store the linked list of rows into c->rows[i] */
541       nrows = 0;
542       fm    = rowhit[M];
543       do {
544         c->rows[i][nrows]            = fm;
545         c->columnsforrow[i][nrows++] = columnsforrow[fm];
546         fm                           = rowhit[fm];
547       } while (fm < M);
548     } /* ---------------------------------------------------------------------------------------*/
549     ierr = PetscFree(cols);CHKERRQ(ierr);
550   }
551 
552   /* Optimize by adding the vscale, and scaleforrow[][] fields */
553   /*
554        vscale will contain the "diagonal" on processor scalings followed by the off processor
555   */
556   if (ctype == IS_COLORING_GLOBAL) {
557     ierr = VecCreateGhost(PetscObjectComm((PetscObject)mat),aij->A->rmap->n,PETSC_DETERMINE,aij->B->cmap->n,aij->garray,&c->vscale);CHKERRQ(ierr);
558     ierr = PetscMalloc(c->ncolors*sizeof(PetscInt*),&c->vscaleforrow);CHKERRQ(ierr);
559     for (k=0; k<c->ncolors; k++) {
560       ierr = PetscMalloc((c->nrows[k]+1)*sizeof(PetscInt),&c->vscaleforrow[k]);CHKERRQ(ierr);
561       for (l=0; l<c->nrows[k]; l++) {
562         col = c->columnsforrow[k][l];
563         if (col >= cstart && col < cend) {
564           /* column is in diagonal block of matrix */
565           colb = col - cstart;
566         } else {
567           /* column  is in "off-processor" part */
568 #if defined(PETSC_USE_CTABLE)
569           ierr = PetscTableFind(aij->colmap,col+1,&colb);CHKERRQ(ierr);
570           colb--;
571 #else
572           colb = aij->colmap[col] - 1;
573 #endif
574           colb += cend - cstart;
575         }
576         c->vscaleforrow[k][l] = colb;
577       }
578     }
579   } else if (ctype == IS_COLORING_GHOSTED) {
580     /* Get gtol mapping */
581     PetscInt N = mat->cmap->N,nlocal,*gtol;
582     ierr = PetscMalloc((N+1)*sizeof(PetscInt),&gtol);CHKERRQ(ierr);
583     for (i=0; i<N; i++) gtol[i] = -1;
584     ierr = ISLocalToGlobalMappingGetSize(map,&nlocal);CHKERRQ(ierr);
585     for (i=0; i<nlocal; i++) gtol[ltog[i]] = i;
586 
587     c->vscale = 0; /* will be created in MatFDColoringApply() */
588     ierr      = PetscMalloc(c->ncolors*sizeof(PetscInt*),&c->vscaleforrow);CHKERRQ(ierr);
589     for (k=0; k<c->ncolors; k++) {
590       ierr = PetscMalloc((c->nrows[k]+1)*sizeof(PetscInt),&c->vscaleforrow[k]);CHKERRQ(ierr);
591       for (l=0; l<c->nrows[k]; l++) {
592         col = c->columnsforrow[k][l];      /* global column index */
593         c->vscaleforrow[k][l] = gtol[col]; /* local column index */
594       }
595     }
596     ierr = PetscFree(gtol);CHKERRQ(ierr);
597   }
598   ierr = ISColoringRestoreIS(iscoloring,&isa);CHKERRQ(ierr);
599 
600   ierr = PetscFree(rowhit);CHKERRQ(ierr);
601   ierr = PetscFree(columnsforrow);CHKERRQ(ierr);
602   ierr = MatRestoreColumnIJ(aij->A,0,PETSC_FALSE,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done);CHKERRQ(ierr);
603   ierr = MatRestoreColumnIJ(aij->B,0,PETSC_FALSE,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done);CHKERRQ(ierr);
604   if (map) {ierr = ISLocalToGlobalMappingRestoreIndices(map,&ltog);CHKERRQ(ierr);}
605   PetscFunctionReturn(0);
606 }
607 
608 
609 
610 
611 
612 
613