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