xref: /petsc/src/mat/impls/aij/mpi/fdmpiaij.c (revision b294de211100dc04b4749667b026c87656b60f6e)
1d4002b98SHong Zhang #include <../src/mat/impls/sell/mpi/mpisell.h>
2c6db04a5SJed Brown #include <../src/mat/impls/aij/mpi/mpiaij.h>
30d1c53f1SHong Zhang #include <../src/mat/impls/baij/mpi/mpibaij.h>
4af0996ceSBarry Smith #include <petsc/private/isimpl.h>
50d1c53f1SHong Zhang 
6d1e9a80fSBarry Smith PetscErrorCode MatFDColoringApply_BAIJ(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
70d1c53f1SHong Zhang {
80d1c53f1SHong Zhang   PetscErrorCode    (*f)(void*,Vec,Vec,void*)=(PetscErrorCode (*)(void*,Vec,Vec,void*))coloring->f;
90d1c53f1SHong Zhang   PetscErrorCode    ierr;
100d1c53f1SHong Zhang   PetscInt          k,cstart,cend,l,row,col,nz,spidx,i,j;
115edff71fSBarry Smith   PetscScalar       dx=0.0,*w3_array,*dy_i,*dy=coloring->dy;
120d1c53f1SHong Zhang   PetscScalar       *vscale_array;
135edff71fSBarry Smith   const PetscScalar *xx;
140d1c53f1SHong Zhang   PetscReal         epsilon=coloring->error_rel,umin=coloring->umin,unorm;
150d1c53f1SHong Zhang   Vec               w1=coloring->w1,w2=coloring->w2,w3,vscale=coloring->vscale;
160d1c53f1SHong Zhang   void              *fctx=coloring->fctx;
170d1c53f1SHong Zhang   PetscInt          ctype=coloring->ctype,nxloc,nrows_k;
180d1c53f1SHong Zhang   PetscScalar       *valaddr;
190d1c53f1SHong Zhang   MatEntry          *Jentry=coloring->matentry;
200df34763SHong Zhang   MatEntry2         *Jentry2=coloring->matentry2;
210d1c53f1SHong Zhang   const PetscInt    ncolors=coloring->ncolors,*ncolumns=coloring->ncolumns,*nrows=coloring->nrows;
220d1c53f1SHong Zhang   PetscInt          bs=J->rmap->bs;
230d1c53f1SHong Zhang 
240d1c53f1SHong Zhang   PetscFunctionBegin;
25b470e4b4SRichard Tran Mills   ierr = VecBindToCPU(x1,PETSC_TRUE);CHKERRQ(ierr);
260d1c53f1SHong Zhang   /* (1) Set w1 = F(x1) */
270d1c53f1SHong Zhang   if (!coloring->fset) {
28217044c2SLisandro Dalcin     ierr = PetscLogEventBegin(MAT_FDColoringFunction,coloring,0,0,0);CHKERRQ(ierr);
290d1c53f1SHong Zhang     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
30217044c2SLisandro Dalcin     ierr = PetscLogEventEnd(MAT_FDColoringFunction,coloring,0,0,0);CHKERRQ(ierr);
310d1c53f1SHong Zhang   } else {
320d1c53f1SHong Zhang     coloring->fset = PETSC_FALSE;
330d1c53f1SHong Zhang   }
340d1c53f1SHong Zhang 
350d1c53f1SHong Zhang   /* (2) Compute vscale = 1./dx - the local scale factors, including ghost points */
360d1c53f1SHong Zhang   ierr = VecGetLocalSize(x1,&nxloc);CHKERRQ(ierr);
370d1c53f1SHong Zhang   if (coloring->htype[0] == 'w') {
380d1c53f1SHong Zhang     /* vscale = dx is a constant scalar */
390d1c53f1SHong Zhang     ierr = VecNorm(x1,NORM_2,&unorm);CHKERRQ(ierr);
400d1c53f1SHong Zhang     dx = 1.0/(PetscSqrtReal(1.0 + unorm)*epsilon);
410d1c53f1SHong Zhang   } else {
425edff71fSBarry Smith     ierr = VecGetArrayRead(x1,&xx);CHKERRQ(ierr);
430d1c53f1SHong Zhang     ierr = VecGetArray(vscale,&vscale_array);CHKERRQ(ierr);
440d1c53f1SHong Zhang     for (col=0; col<nxloc; col++) {
450d1c53f1SHong Zhang       dx = xx[col];
460d1c53f1SHong Zhang       if (PetscAbsScalar(dx) < umin) {
470d1c53f1SHong Zhang         if (PetscRealPart(dx) >= 0.0)      dx = umin;
480d1c53f1SHong Zhang         else if (PetscRealPart(dx) < 0.0) dx = -umin;
490d1c53f1SHong Zhang       }
500d1c53f1SHong Zhang       dx               *= epsilon;
510d1c53f1SHong Zhang       vscale_array[col] = 1.0/dx;
520d1c53f1SHong Zhang     }
535edff71fSBarry Smith     ierr = VecRestoreArrayRead(x1,&xx);CHKERRQ(ierr);
540d1c53f1SHong Zhang     ierr = VecRestoreArray(vscale,&vscale_array);CHKERRQ(ierr);
550d1c53f1SHong Zhang   }
56684f2004SHong Zhang   if (ctype == IS_COLORING_GLOBAL && coloring->htype[0] == 'd') {
570d1c53f1SHong Zhang     ierr = VecGhostUpdateBegin(vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
580d1c53f1SHong Zhang     ierr = VecGhostUpdateEnd(vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
590d1c53f1SHong Zhang   }
600d1c53f1SHong Zhang 
610d1c53f1SHong Zhang   /* (3) Loop over each color */
620d1c53f1SHong Zhang   if (!coloring->w3) {
630d1c53f1SHong Zhang     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
64ce911d59SRichard Tran Mills     /* Vec is used intensively in particular piece of scalar CPU code; won't benefit from bouncing back and forth to the GPU */
65b470e4b4SRichard Tran Mills     ierr = VecBindToCPU(coloring->w3,PETSC_TRUE);CHKERRQ(ierr);
660d1c53f1SHong Zhang     ierr = PetscLogObjectParent((PetscObject)coloring,(PetscObject)coloring->w3);CHKERRQ(ierr);
670d1c53f1SHong Zhang   }
680d1c53f1SHong Zhang   w3 = coloring->w3;
690d1c53f1SHong Zhang 
700d1c53f1SHong Zhang   ierr = VecGetOwnershipRange(x1,&cstart,&cend);CHKERRQ(ierr); /* used by ghosted vscale */
710d1c53f1SHong Zhang   if (vscale) {
720d1c53f1SHong Zhang     ierr = VecGetArray(vscale,&vscale_array);CHKERRQ(ierr);
730d1c53f1SHong Zhang   }
740d1c53f1SHong Zhang   nz = 0;
750d1c53f1SHong Zhang   for (k=0; k<ncolors; k++) {
760d1c53f1SHong Zhang     coloring->currentcolor = k;
770d1c53f1SHong Zhang 
780d1c53f1SHong Zhang     /*
790d1c53f1SHong Zhang       (3-1) Loop over each column associated with color
800d1c53f1SHong Zhang       adding the perturbation to the vector w3 = x1 + dx.
810d1c53f1SHong Zhang     */
820d1c53f1SHong Zhang     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
830d1c53f1SHong Zhang     dy_i = dy;
840d1c53f1SHong Zhang     for (i=0; i<bs; i++) {     /* Loop over a block of columns */
850d1c53f1SHong Zhang       ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);
860d1c53f1SHong Zhang       if (ctype == IS_COLORING_GLOBAL) w3_array -= cstart; /* shift pointer so global index can be used */
870d1c53f1SHong Zhang       if (coloring->htype[0] == 'w') {
880d1c53f1SHong Zhang         for (l=0; l<ncolumns[k]; l++) {
890d1c53f1SHong Zhang           col            = i + bs*coloring->columns[k][l];  /* local column (in global index!) of the matrix we are probing for */
900d1c53f1SHong Zhang           w3_array[col] += 1.0/dx;
91684f2004SHong Zhang           if (i) w3_array[col-1] -= 1.0/dx; /* resume original w3[col-1] */
920d1c53f1SHong Zhang         }
930d1c53f1SHong Zhang       } else { /* htype == 'ds' */
940d1c53f1SHong Zhang         vscale_array -= cstart; /* shift pointer so global index can be used */
950d1c53f1SHong Zhang         for (l=0; l<ncolumns[k]; l++) {
96f8c2866eSHong Zhang           col = i + bs*coloring->columns[k][l]; /* local column (in global index!) of the matrix we are probing for */
970d1c53f1SHong Zhang           w3_array[col] += 1.0/vscale_array[col];
98684f2004SHong Zhang           if (i) w3_array[col-1] -=  1.0/vscale_array[col-1]; /* resume original w3[col-1] */
990d1c53f1SHong Zhang         }
1000d1c53f1SHong Zhang         vscale_array += cstart;
1010d1c53f1SHong Zhang       }
1020d1c53f1SHong Zhang       if (ctype == IS_COLORING_GLOBAL) w3_array += cstart;
1030d1c53f1SHong Zhang       ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
1040d1c53f1SHong Zhang 
1050d1c53f1SHong Zhang       /*
1060d1c53f1SHong Zhang        (3-2) Evaluate function at w3 = x1 + dx (here dx is a vector of perturbations)
1070d1c53f1SHong Zhang                            w2 = F(x1 + dx) - F(x1)
1080d1c53f1SHong Zhang        */
1090d1c53f1SHong Zhang       ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
1100d1c53f1SHong Zhang       ierr = VecPlaceArray(w2,dy_i);CHKERRQ(ierr); /* place w2 to the array dy_i */
1110d1c53f1SHong Zhang       ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
1120d1c53f1SHong Zhang       ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
1130d1c53f1SHong Zhang       ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr);
1140d1c53f1SHong Zhang       ierr = VecResetArray(w2);CHKERRQ(ierr);
1150d1c53f1SHong Zhang       dy_i += nxloc; /* points to dy+i*nxloc */
1160d1c53f1SHong Zhang     }
1170d1c53f1SHong Zhang 
1180d1c53f1SHong Zhang     /*
1190d1c53f1SHong Zhang      (3-3) Loop over rows of vector, putting results into Jacobian matrix
1200d1c53f1SHong Zhang     */
1210d1c53f1SHong Zhang     nrows_k = nrows[k];
1220d1c53f1SHong Zhang     if (coloring->htype[0] == 'w') {
1230d1c53f1SHong Zhang       for (l=0; l<nrows_k; l++) {
1240df34763SHong Zhang         row     = bs*Jentry2[nz].row;   /* local row index */
1250df34763SHong Zhang         valaddr = Jentry2[nz++].valaddr;
1260d1c53f1SHong Zhang         spidx   = 0;
1270d1c53f1SHong Zhang         dy_i    = dy;
1280d1c53f1SHong Zhang         for (i=0; i<bs; i++) {   /* column of the block */
1290d1c53f1SHong Zhang           for (j=0; j<bs; j++) { /* row of the block */
1300d1c53f1SHong Zhang             valaddr[spidx++] = dy_i[row+j]*dx;
1310d1c53f1SHong Zhang           }
132f8c2866eSHong Zhang           dy_i += nxloc; /* points to dy+i*nxloc */
1330d1c53f1SHong Zhang         }
1340d1c53f1SHong Zhang       }
1350d1c53f1SHong Zhang     } else { /* htype == 'ds' */
1360d1c53f1SHong Zhang       for (l=0; l<nrows_k; l++) {
1370d1c53f1SHong Zhang         row     = bs*Jentry[nz].row;   /* local row index */
1380d1c53f1SHong Zhang         col     = bs*Jentry[nz].col;   /* local column index */
139684f2004SHong Zhang         valaddr = Jentry[nz++].valaddr;
140f8c2866eSHong Zhang         spidx   = 0;
141f8c2866eSHong Zhang         dy_i    = dy;
142f8c2866eSHong Zhang         for (i=0; i<bs; i++) {   /* column of the block */
143f8c2866eSHong Zhang           for (j=0; j<bs; j++) { /* row of the block */
144f8c2866eSHong Zhang             valaddr[spidx++] = dy_i[row+j]*vscale_array[col+i];
145f8c2866eSHong Zhang           }
146f8c2866eSHong Zhang           dy_i += nxloc; /* points to dy+i*nxloc */
147f8c2866eSHong Zhang         }
1480d1c53f1SHong Zhang       }
1490d1c53f1SHong Zhang     }
1500d1c53f1SHong Zhang   }
1510d1c53f1SHong Zhang   ierr = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1520d1c53f1SHong Zhang   ierr = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1530d1c53f1SHong Zhang   if (vscale) {
1540d1c53f1SHong Zhang     ierr = VecRestoreArray(vscale,&vscale_array);CHKERRQ(ierr);
1550d1c53f1SHong Zhang   }
1560d1c53f1SHong Zhang 
1570d1c53f1SHong Zhang   coloring->currentcolor = -1;
158b470e4b4SRichard Tran Mills   ierr = VecBindToCPU(x1,PETSC_FALSE);CHKERRQ(ierr);
1590d1c53f1SHong Zhang   PetscFunctionReturn(0);
1600d1c53f1SHong Zhang }
161a64fbb32SBarry Smith 
162531c7667SBarry Smith /* this is declared PETSC_EXTERN because it is used by MatFDColoringUseDM() which is in the DM library */
163d1e9a80fSBarry Smith PetscErrorCode  MatFDColoringApply_AIJ(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
164fcd7ac73SHong Zhang {
165fcd7ac73SHong Zhang   PetscErrorCode    (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void*))coloring->f;
166fcd7ac73SHong Zhang   PetscErrorCode    ierr;
167a2f2d239SHong Zhang   PetscInt          k,cstart,cend,l,row,col,nz;
1685edff71fSBarry Smith   PetscScalar       dx=0.0,*y,*w3_array;
1695edff71fSBarry Smith   const PetscScalar *xx;
170fcd7ac73SHong Zhang   PetscScalar       *vscale_array;
171fcd7ac73SHong Zhang   PetscReal         epsilon=coloring->error_rel,umin=coloring->umin,unorm;
1728bc97078SHong Zhang   Vec               w1=coloring->w1,w2=coloring->w2,w3,vscale=coloring->vscale;
173fcd7ac73SHong Zhang   void              *fctx=coloring->fctx;
17491e7fa0fSBarry Smith   ISColoringType    ctype=coloring->ctype;
17591e7fa0fSBarry Smith   PetscInt          nxloc,nrows_k;
176573f477fSHong Zhang   MatEntry          *Jentry=coloring->matentry;
1770df34763SHong Zhang   MatEntry2         *Jentry2=coloring->matentry2;
1788bc97078SHong Zhang   const PetscInt    ncolors=coloring->ncolors,*ncolumns=coloring->ncolumns,*nrows=coloring->nrows;
179*b294de21SRichard Tran Mills   PetscBool         alreadyboundtocpu;
180fcd7ac73SHong Zhang 
181fcd7ac73SHong Zhang   PetscFunctionBegin;
182*b294de21SRichard Tran Mills   ierr = VecBoundToCPU(x1,&alreadyboundtocpu);CHKERRQ(ierr);
183b470e4b4SRichard Tran Mills   ierr = VecBindToCPU(x1,PETSC_TRUE);CHKERRQ(ierr);
184531c7667SBarry Smith   if ((ctype == IS_COLORING_LOCAL) && (J->ops->fdcoloringapply == MatFDColoringApply_AIJ)) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_SUP,"Must call MatColoringUseDM() with IS_COLORING_LOCAL");
1858bc97078SHong Zhang   /* (1) Set w1 = F(x1) */
186fcd7ac73SHong Zhang   if (!coloring->fset) {
187fcd7ac73SHong Zhang     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
188f6af9589SHong Zhang     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
189fcd7ac73SHong Zhang     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
190fcd7ac73SHong Zhang   } else {
191fcd7ac73SHong Zhang     coloring->fset = PETSC_FALSE;
192fcd7ac73SHong Zhang   }
193fcd7ac73SHong Zhang 
1948bc97078SHong Zhang   /* (2) Compute vscale = 1./dx - the local scale factors, including ghost points */
195f6af9589SHong Zhang   if (coloring->htype[0] == 'w') {
196684f2004SHong Zhang     /* vscale = 1./dx is a constant scalar */
197f6af9589SHong Zhang     ierr = VecNorm(x1,NORM_2,&unorm);CHKERRQ(ierr);
198c53567a0SHong Zhang     dx = 1.0/(PetscSqrtReal(1.0 + unorm)*epsilon);
19970e7395fSHong Zhang   } else {
20074d3cef9SHong Zhang     ierr = VecGetLocalSize(x1,&nxloc);CHKERRQ(ierr);
2015edff71fSBarry Smith     ierr = VecGetArrayRead(x1,&xx);CHKERRQ(ierr);
2028bc97078SHong Zhang     ierr = VecGetArray(vscale,&vscale_array);CHKERRQ(ierr);
20374d3cef9SHong Zhang     for (col=0; col<nxloc; col++) {
204fcd7ac73SHong Zhang       dx = xx[col];
20574d3cef9SHong Zhang       if (PetscAbsScalar(dx) < umin) {
20674d3cef9SHong Zhang         if (PetscRealPart(dx) >= 0.0)      dx = umin;
20774d3cef9SHong Zhang         else if (PetscRealPart(dx) < 0.0) dx = -umin;
20874d3cef9SHong Zhang       }
209fcd7ac73SHong Zhang       dx               *= epsilon;
21074d3cef9SHong Zhang       vscale_array[col] = 1.0/dx;
211f6af9589SHong Zhang     }
2125edff71fSBarry Smith     ierr = VecRestoreArrayRead(x1,&xx);CHKERRQ(ierr);
2138bc97078SHong Zhang     ierr = VecRestoreArray(vscale,&vscale_array);CHKERRQ(ierr);
21470e7395fSHong Zhang   }
215684f2004SHong Zhang   if (ctype == IS_COLORING_GLOBAL && coloring->htype[0] == 'd') {
2168bc97078SHong Zhang     ierr = VecGhostUpdateBegin(vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2178bc97078SHong Zhang     ierr = VecGhostUpdateEnd(vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
218fcd7ac73SHong Zhang   }
219fcd7ac73SHong Zhang 
2208bc97078SHong Zhang   /* (3) Loop over each color */
2218bc97078SHong Zhang   if (!coloring->w3) {
2228bc97078SHong Zhang     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
2238bc97078SHong Zhang     ierr = PetscLogObjectParent((PetscObject)coloring,(PetscObject)coloring->w3);CHKERRQ(ierr);
2248bc97078SHong Zhang   }
2258bc97078SHong Zhang   w3 = coloring->w3;
226fcd7ac73SHong Zhang 
2278bc97078SHong Zhang   ierr = VecGetOwnershipRange(x1,&cstart,&cend);CHKERRQ(ierr); /* used by ghosted vscale */
2289e917edbSHong Zhang   if (vscale) {
2298bc97078SHong Zhang     ierr = VecGetArray(vscale,&vscale_array);CHKERRQ(ierr);
2309e917edbSHong Zhang   }
2318bc97078SHong Zhang   nz = 0;
232e2c857f8SHong Zhang 
233b3e1f37bSHong Zhang   if (coloring->bcols > 1) { /* use blocked insertion of Jentry */
234b3e1f37bSHong Zhang     PetscInt    i,m=J->rmap->n,nbcols,bcols=coloring->bcols;
235e2c857f8SHong Zhang     PetscScalar *dy=coloring->dy,*dy_k;
236e2c857f8SHong Zhang 
237e2c857f8SHong Zhang     nbcols = 0;
238e2c857f8SHong Zhang     for (k=0; k<ncolors; k+=bcols) {
239e2c857f8SHong Zhang 
240e2c857f8SHong Zhang       /*
241e2c857f8SHong Zhang        (3-1) Loop over each column associated with color
242e2c857f8SHong Zhang        adding the perturbation to the vector w3 = x1 + dx.
243e2c857f8SHong Zhang        */
244e2c857f8SHong Zhang 
245e2c857f8SHong Zhang       dy_k = dy;
246e2c857f8SHong Zhang       if (k + bcols > ncolors) bcols = ncolors - k;
247e2c857f8SHong Zhang       for (i=0; i<bcols; i++) {
248ceef8a49SBarry Smith         coloring->currentcolor = k+i;
249e2c857f8SHong Zhang 
250e2c857f8SHong Zhang         ierr = VecCopy(x1,w3);CHKERRQ(ierr);
251e2c857f8SHong Zhang         ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);
252e2c857f8SHong Zhang         if (ctype == IS_COLORING_GLOBAL) w3_array -= cstart; /* shift pointer so global index can be used */
253e2c857f8SHong Zhang         if (coloring->htype[0] == 'w') {
254e2c857f8SHong Zhang           for (l=0; l<ncolumns[k+i]; l++) {
255e2c857f8SHong Zhang             col = coloring->columns[k+i][l]; /* local column (in global index!) of the matrix we are probing for */
256e2c857f8SHong Zhang             w3_array[col] += 1.0/dx;
257e2c857f8SHong Zhang           }
258e2c857f8SHong Zhang         } else { /* htype == 'ds' */
259e2c857f8SHong Zhang           vscale_array -= cstart; /* shift pointer so global index can be used */
260e2c857f8SHong Zhang           for (l=0; l<ncolumns[k+i]; l++) {
261e2c857f8SHong Zhang             col = coloring->columns[k+i][l]; /* local column (in global index!) of the matrix we are probing for */
262e2c857f8SHong Zhang             w3_array[col] += 1.0/vscale_array[col];
263e2c857f8SHong Zhang           }
264e2c857f8SHong Zhang           vscale_array += cstart;
265e2c857f8SHong Zhang         }
266e2c857f8SHong Zhang         if (ctype == IS_COLORING_GLOBAL) w3_array += cstart;
267e2c857f8SHong Zhang         ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
268e2c857f8SHong Zhang 
269e2c857f8SHong Zhang         /*
270e2c857f8SHong Zhang          (3-2) Evaluate function at w3 = x1 + dx (here dx is a vector of perturbations)
271e2c857f8SHong Zhang                            w2 = F(x1 + dx) - F(x1)
272e2c857f8SHong Zhang          */
273e2c857f8SHong Zhang         ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
274e2c857f8SHong Zhang         ierr = VecPlaceArray(w2,dy_k);CHKERRQ(ierr); /* place w2 to the array dy_i */
275e2c857f8SHong Zhang         ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
276e2c857f8SHong Zhang         ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
277e2c857f8SHong Zhang         ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr);
278e2c857f8SHong Zhang         ierr = VecResetArray(w2);CHKERRQ(ierr);
279e2c857f8SHong Zhang         dy_k += m; /* points to dy+i*nxloc */
280e2c857f8SHong Zhang       }
281e2c857f8SHong Zhang 
282e2c857f8SHong Zhang       /*
283e2c857f8SHong Zhang        (3-3) Loop over block rows of vector, putting results into Jacobian matrix
284e2c857f8SHong Zhang        */
285d880da65SHong Zhang       nrows_k = nrows[nbcols++];
286d880da65SHong Zhang 
287e2c857f8SHong Zhang       if (coloring->htype[0] == 'w') {
288e2c857f8SHong Zhang         for (l=0; l<nrows_k; l++) {
2890df34763SHong Zhang           row                      = Jentry2[nz].row;   /* local row index */
2900df34763SHong Zhang           *(Jentry2[nz++].valaddr) = dy[row]*dx;
291e2c857f8SHong Zhang         }
292e2c857f8SHong Zhang       } else { /* htype == 'ds' */
293e2c857f8SHong Zhang         for (l=0; l<nrows_k; l++) {
294e2c857f8SHong Zhang           row                   = Jentry[nz].row;   /* local row index */
295d880da65SHong Zhang           *(Jentry[nz].valaddr) = dy[row]*vscale_array[Jentry[nz].col];
296e2c857f8SHong Zhang           nz++;
297e2c857f8SHong Zhang         }
298e2c857f8SHong Zhang       }
299e2c857f8SHong Zhang     }
300b3e1f37bSHong Zhang   } else { /* bcols == 1 */
3018bc97078SHong Zhang     for (k=0; k<ncolors; k++) {
3028bc97078SHong Zhang       coloring->currentcolor = k;
3039e917edbSHong Zhang 
304fcd7ac73SHong Zhang       /*
3058bc97078SHong Zhang        (3-1) Loop over each column associated with color
306f6af9589SHong Zhang        adding the perturbation to the vector w3 = x1 + dx.
307fcd7ac73SHong Zhang        */
308f6af9589SHong Zhang       ierr = VecCopy(x1,w3);CHKERRQ(ierr);
309f6af9589SHong Zhang       ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);
310a2f2d239SHong Zhang       if (ctype == IS_COLORING_GLOBAL) w3_array -= cstart; /* shift pointer so global index can be used */
311b039d6c4SHong Zhang       if (coloring->htype[0] == 'w') {
3128bc97078SHong Zhang         for (l=0; l<ncolumns[k]; l++) {
313f6af9589SHong Zhang           col = coloring->columns[k][l]; /* local column (in global index!) of the matrix we are probing for */
3149e917edbSHong Zhang           w3_array[col] += 1.0/dx;
3159e917edbSHong Zhang         }
316b039d6c4SHong Zhang       } else { /* htype == 'ds' */
317a2f2d239SHong Zhang         vscale_array -= cstart; /* shift pointer so global index can be used */
318b039d6c4SHong Zhang         for (l=0; l<ncolumns[k]; l++) {
319b039d6c4SHong Zhang           col = coloring->columns[k][l]; /* local column (in global index!) of the matrix we are probing for */
320a2f2d239SHong Zhang           w3_array[col] += 1.0/vscale_array[col];
32170e7395fSHong Zhang         }
322a2f2d239SHong Zhang         vscale_array += cstart;
323fcd7ac73SHong Zhang       }
324a2f2d239SHong Zhang       if (ctype == IS_COLORING_GLOBAL) w3_array += cstart;
325fcd7ac73SHong Zhang       ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
3269e917edbSHong Zhang 
327fcd7ac73SHong Zhang       /*
3288bc97078SHong Zhang        (3-2) Evaluate function at w3 = x1 + dx (here dx is a vector of perturbations)
329fcd7ac73SHong Zhang                            w2 = F(x1 + dx) - F(x1)
330fcd7ac73SHong Zhang        */
331fcd7ac73SHong Zhang       ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
332fcd7ac73SHong Zhang       ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
333fcd7ac73SHong Zhang       ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
334fcd7ac73SHong Zhang       ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr);
3359e917edbSHong Zhang 
3368bc97078SHong Zhang       /*
3378bc97078SHong Zhang        (3-3) Loop over rows of vector, putting results into Jacobian matrix
3388bc97078SHong Zhang        */
339c53567a0SHong Zhang       nrows_k = nrows[k];
340fcd7ac73SHong Zhang       ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
341b039d6c4SHong Zhang       if (coloring->htype[0] == 'w') {
342c53567a0SHong Zhang         for (l=0; l<nrows_k; l++) {
3430df34763SHong Zhang           row                      = Jentry2[nz].row;   /* local row index */
3440df34763SHong Zhang           *(Jentry2[nz++].valaddr) = y[row]*dx;
3459e917edbSHong Zhang         }
346b039d6c4SHong Zhang       } else { /* htype == 'ds' */
347c53567a0SHong Zhang         for (l=0; l<nrows_k; l++) {
348b039d6c4SHong Zhang           row                   = Jentry[nz].row;   /* local row index */
349b039d6c4SHong Zhang           *(Jentry[nz].valaddr) = y[row]*vscale_array[Jentry[nz].col];
350573f477fSHong Zhang           nz++;
351fcd7ac73SHong Zhang         }
352b039d6c4SHong Zhang       }
353fcd7ac73SHong Zhang       ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
3548bc97078SHong Zhang     }
355b3e1f37bSHong Zhang   }
356b3e1f37bSHong Zhang 
357e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
358c70f7ee4SJunchao Zhang   if (J->offloadmask != PETSC_OFFLOAD_UNALLOCATED) J->offloadmask = PETSC_OFFLOAD_CPU;
359e2cf4d64SStefano Zampini #endif
360f5aae955SHong Zhang   ierr = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
361f5aae955SHong Zhang   ierr = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3629e917edbSHong Zhang   if (vscale) {
3638bc97078SHong Zhang     ierr = VecRestoreArray(vscale,&vscale_array);CHKERRQ(ierr);
3649e917edbSHong Zhang   }
365fcd7ac73SHong Zhang   coloring->currentcolor = -1;
366*b294de21SRichard Tran Mills   if (!alreadyboundtocpu) {ierr = VecBindToCPU(x1,PETSC_FALSE);CHKERRQ(ierr);}
367fcd7ac73SHong Zhang   PetscFunctionReturn(0);
368fcd7ac73SHong Zhang }
369fcd7ac73SHong Zhang 
370f86b9fbaSHong Zhang PetscErrorCode MatFDColoringSetUp_MPIXAIJ(Mat mat,ISColoring iscoloring,MatFDColoring c)
371fcd7ac73SHong Zhang {
372fcd7ac73SHong Zhang   PetscErrorCode         ierr;
373fcd7ac73SHong Zhang   PetscMPIInt            size,*ncolsonproc,*disp,nn;
374e3225e9dSHong Zhang   PetscInt               i,n,nrows,nrows_i,j,k,m,ncols,col,*rowhit,cstart,cend,colb;
37572c15787SHong Zhang   const PetscInt         *is,*A_ci,*A_cj,*B_ci,*B_cj,*row=NULL,*ltog=NULL;
376071fcb05SBarry Smith   PetscInt               nis=iscoloring->n,nctot,*cols,tmp = 0;
377fcd7ac73SHong Zhang   ISLocalToGlobalMapping map=mat->cmap->mapping;
378a8971b87SHong Zhang   PetscInt               ctype=c->ctype,*spidxA,*spidxB,nz,bs,bs2,spidx;
3790d1c53f1SHong Zhang   Mat                    A,B;
380e3225e9dSHong Zhang   PetscScalar            *A_val,*B_val,**valaddrhit;
381573f477fSHong Zhang   MatEntry               *Jentry;
3820df34763SHong Zhang   MatEntry2              *Jentry2;
383d4002b98SHong Zhang   PetscBool              isBAIJ,isSELL;
384531e53bdSHong Zhang   PetscInt               bcols=c->bcols;
3850d1c53f1SHong Zhang #if defined(PETSC_USE_CTABLE)
3860d1c53f1SHong Zhang   PetscTable             colmap=NULL;
3870d1c53f1SHong Zhang #else
3880d1c53f1SHong Zhang   PetscInt               *colmap=NULL;     /* local col number of off-diag col */
3890d1c53f1SHong Zhang #endif
390fcd7ac73SHong Zhang 
391fcd7ac73SHong Zhang   PetscFunctionBegin;
3925bdb020cSBarry Smith   if (ctype == IS_COLORING_LOCAL) {
39372c15787SHong Zhang     if (!map) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_INCOMP,"When using ghosted differencing matrix must have local to global mapping provided with MatSetLocalToGlobalMapping");
39472c15787SHong Zhang     ierr = ISLocalToGlobalMappingGetIndices(map,&ltog);CHKERRQ(ierr);
39572c15787SHong Zhang   }
396fcd7ac73SHong Zhang 
3970d1c53f1SHong Zhang   ierr = MatGetBlockSize(mat,&bs);CHKERRQ(ierr);
398b9e7e5c1SBarry Smith   ierr = PetscObjectBaseTypeCompare((PetscObject)mat,MATMPIBAIJ,&isBAIJ);CHKERRQ(ierr);
399d4002b98SHong Zhang   ierr = PetscObjectTypeCompare((PetscObject)mat,MATMPISELL,&isSELL);CHKERRQ(ierr);
400e3225e9dSHong Zhang   if (isBAIJ) {
401195687c5SHong Zhang     Mat_MPIBAIJ *baij=(Mat_MPIBAIJ*)mat->data;
4026c145f34SHong Zhang     Mat_SeqBAIJ *spA,*spB;
403195687c5SHong Zhang     A = baij->A;  spA = (Mat_SeqBAIJ*)A->data; A_val = spA->a;
404195687c5SHong Zhang     B = baij->B;  spB = (Mat_SeqBAIJ*)B->data; B_val = spB->a;
4050d1c53f1SHong Zhang     nz = spA->nz + spB->nz; /* total nonzero entries of mat */
406195687c5SHong Zhang     if (!baij->colmap) {
4070d1c53f1SHong Zhang       ierr = MatCreateColmap_MPIBAIJ_Private(mat);CHKERRQ(ierr);
4080d1c53f1SHong Zhang     }
409097e26fcSJed Brown     colmap = baij->colmap;
4100d1c53f1SHong Zhang     ierr = MatGetColumnIJ_SeqBAIJ_Color(A,0,PETSC_FALSE,PETSC_FALSE,&ncols,&A_ci,&A_cj,&spidxA,NULL);CHKERRQ(ierr);
4110d1c53f1SHong Zhang     ierr = MatGetColumnIJ_SeqBAIJ_Color(B,0,PETSC_FALSE,PETSC_FALSE,&ncols,&B_ci,&B_cj,&spidxB,NULL);CHKERRQ(ierr);
412195687c5SHong Zhang 
413195687c5SHong Zhang     if (ctype == IS_COLORING_GLOBAL && c->htype[0] == 'd') {  /* create vscale for storing dx */
414195687c5SHong Zhang       PetscInt    *garray;
415785e854fSJed Brown       ierr = PetscMalloc1(B->cmap->n,&garray);CHKERRQ(ierr);
416195687c5SHong Zhang       for (i=0; i<baij->B->cmap->n/bs; i++) {
417195687c5SHong Zhang         for (j=0; j<bs; j++) {
418195687c5SHong Zhang           garray[i*bs+j] = bs*baij->garray[i]+j;
419195687c5SHong Zhang         }
420195687c5SHong Zhang       }
421195687c5SHong Zhang       ierr = VecCreateGhost(PetscObjectComm((PetscObject)mat),mat->cmap->n,PETSC_DETERMINE,B->cmap->n,garray,&c->vscale);CHKERRQ(ierr);
422b470e4b4SRichard Tran Mills       ierr = VecBindToCPU(c->vscale,PETSC_TRUE);CHKERRQ(ierr);
423195687c5SHong Zhang       ierr = PetscFree(garray);CHKERRQ(ierr);
424195687c5SHong Zhang     }
425d4002b98SHong Zhang   } else if (isSELL) {
426d4002b98SHong Zhang     Mat_MPISELL *sell=(Mat_MPISELL*)mat->data;
427d4002b98SHong Zhang     Mat_SeqSELL *spA,*spB;
428d4002b98SHong Zhang     A = sell->A;  spA = (Mat_SeqSELL*)A->data; A_val = spA->val;
429d4002b98SHong Zhang     B = sell->B;  spB = (Mat_SeqSELL*)B->data; B_val = spB->val;
430f4b713efSHong Zhang     nz = spA->nz + spB->nz; /* total nonzero entries of mat */
431d4002b98SHong Zhang     if (!sell->colmap) {
432f4b713efSHong Zhang       /* Allow access to data structures of local part of matrix
433f4b713efSHong Zhang        - creates aij->colmap which maps global column number to local number in part B */
434d4002b98SHong Zhang       ierr = MatCreateColmap_MPISELL_Private(mat);CHKERRQ(ierr);
435f4b713efSHong Zhang     }
436d4002b98SHong Zhang     colmap = sell->colmap;
437d4002b98SHong Zhang     ierr = MatGetColumnIJ_SeqSELL_Color(A,0,PETSC_FALSE,PETSC_FALSE,&ncols,&A_ci,&A_cj,&spidxA,NULL);CHKERRQ(ierr);
438d4002b98SHong Zhang     ierr = MatGetColumnIJ_SeqSELL_Color(B,0,PETSC_FALSE,PETSC_FALSE,&ncols,&B_ci,&B_cj,&spidxB,NULL);CHKERRQ(ierr);
439f4b713efSHong Zhang 
440f4b713efSHong Zhang     bs = 1; /* only bs=1 is supported for non MPIBAIJ matrix */
441f4b713efSHong Zhang 
442f4b713efSHong Zhang     if (ctype == IS_COLORING_GLOBAL && c->htype[0] == 'd') { /* create vscale for storing dx */
443d4002b98SHong Zhang       ierr = VecCreateGhost(PetscObjectComm((PetscObject)mat),mat->cmap->n,PETSC_DETERMINE,B->cmap->n,sell->garray,&c->vscale);CHKERRQ(ierr);
444b470e4b4SRichard Tran Mills       ierr = VecBindToCPU(c->vscale,PETSC_TRUE);CHKERRQ(ierr);
445f4b713efSHong Zhang     }
446e3225e9dSHong Zhang   } else {
447e3225e9dSHong Zhang     Mat_MPIAIJ *aij=(Mat_MPIAIJ*)mat->data;
448e3225e9dSHong Zhang     Mat_SeqAIJ *spA,*spB;
449e3225e9dSHong Zhang     A = aij->A;  spA = (Mat_SeqAIJ*)A->data; A_val = spA->a;
450e3225e9dSHong Zhang     B = aij->B;  spB = (Mat_SeqAIJ*)B->data; B_val = spB->a;
451e3225e9dSHong Zhang     nz = spA->nz + spB->nz; /* total nonzero entries of mat */
452e3225e9dSHong Zhang     if (!aij->colmap) {
453e3225e9dSHong Zhang       /* Allow access to data structures of local part of matrix
454e3225e9dSHong Zhang        - creates aij->colmap which maps global column number to local number in part B */
455e3225e9dSHong Zhang       ierr = MatCreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
456e3225e9dSHong Zhang     }
457097e26fcSJed Brown     colmap = aij->colmap;
458e3225e9dSHong Zhang     ierr = MatGetColumnIJ_SeqAIJ_Color(A,0,PETSC_FALSE,PETSC_FALSE,&ncols,&A_ci,&A_cj,&spidxA,NULL);CHKERRQ(ierr);
459e3225e9dSHong Zhang     ierr = MatGetColumnIJ_SeqAIJ_Color(B,0,PETSC_FALSE,PETSC_FALSE,&ncols,&B_ci,&B_cj,&spidxB,NULL);CHKERRQ(ierr);
460e3225e9dSHong Zhang 
461e3225e9dSHong Zhang     bs = 1; /* only bs=1 is supported for non MPIBAIJ matrix */
462195687c5SHong Zhang 
463195687c5SHong Zhang     if (ctype == IS_COLORING_GLOBAL && c->htype[0] == 'd') { /* create vscale for storing dx */
464195687c5SHong Zhang       ierr = VecCreateGhost(PetscObjectComm((PetscObject)mat),mat->cmap->n,PETSC_DETERMINE,B->cmap->n,aij->garray,&c->vscale);CHKERRQ(ierr);
465b470e4b4SRichard Tran Mills       ierr = VecBindToCPU(c->vscale,PETSC_TRUE);CHKERRQ(ierr);
466195687c5SHong Zhang     }
4670d1c53f1SHong Zhang   }
4680d1c53f1SHong Zhang 
4690d1c53f1SHong Zhang   m      = mat->rmap->n/bs;
4700d1c53f1SHong Zhang   cstart = mat->cmap->rstart/bs;
4710d1c53f1SHong Zhang   cend   = mat->cmap->rend/bs;
472fcd7ac73SHong Zhang 
473071fcb05SBarry Smith   ierr = PetscMalloc2(nis,&c->ncolumns,nis,&c->columns);CHKERRQ(ierr);
474071fcb05SBarry Smith   ierr = PetscMalloc1(nis,&c->nrows);CHKERRQ(ierr);
4758bc97078SHong Zhang   ierr = PetscLogObjectMemory((PetscObject)c,3*nis*sizeof(PetscInt));CHKERRQ(ierr);
476fcd7ac73SHong Zhang 
4770df34763SHong Zhang   if (c->htype[0] == 'd') {
478785e854fSJed Brown     ierr        = PetscMalloc1(nz,&Jentry);CHKERRQ(ierr);
4798bc97078SHong Zhang     ierr        = PetscLogObjectMemory((PetscObject)c,nz*sizeof(MatEntry));CHKERRQ(ierr);
4808bc97078SHong Zhang     c->matentry = Jentry;
4810df34763SHong Zhang   } else if (c->htype[0] == 'w') {
482785e854fSJed Brown     ierr         = PetscMalloc1(nz,&Jentry2);CHKERRQ(ierr);
4830df34763SHong Zhang     ierr         = PetscLogObjectMemory((PetscObject)c,nz*sizeof(MatEntry2));CHKERRQ(ierr);
4840df34763SHong Zhang     c->matentry2 = Jentry2;
4850df34763SHong Zhang   } else SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"htype is not supported");
486a774a6f1SHong Zhang 
487dcca6d9dSJed Brown   ierr = PetscMalloc2(m+1,&rowhit,m+1,&valaddrhit);CHKERRQ(ierr);
488fcd7ac73SHong Zhang   nz   = 0;
489071fcb05SBarry Smith   ierr = ISColoringGetIS(iscoloring,PETSC_OWN_POINTER, PETSC_IGNORE,&c->isa);CHKERRQ(ierr);
490071fcb05SBarry Smith 
491071fcb05SBarry Smith   if (ctype == IS_COLORING_GLOBAL) {
492ffc4695bSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);CHKERRMPI(ierr);
493071fcb05SBarry Smith     ierr = PetscMalloc2(size,&ncolsonproc,size,&disp);CHKERRQ(ierr);
494071fcb05SBarry Smith   }
495071fcb05SBarry Smith 
496d3825b63SHong Zhang   for (i=0; i<nis; i++) { /* for each local color */
497071fcb05SBarry Smith     ierr = ISGetLocalSize(c->isa[i],&n);CHKERRQ(ierr);
498071fcb05SBarry Smith     ierr = ISGetIndices(c->isa[i],&is);CHKERRQ(ierr);
499fcd7ac73SHong Zhang 
500fcd7ac73SHong Zhang     c->ncolumns[i] = n; /* local number of columns of this color on this process */
501071fcb05SBarry Smith     c->columns[i]  = (PetscInt*)is;
502fcd7ac73SHong Zhang 
503fcd7ac73SHong Zhang     if (ctype == IS_COLORING_GLOBAL) {
504d3825b63SHong Zhang       /* Determine nctot, the total (parallel) number of columns of this color */
505d3825b63SHong Zhang       /* ncolsonproc[j]: local ncolumns on proc[j] of this color */
506fcd7ac73SHong Zhang       ierr  = PetscMPIIntCast(n,&nn);CHKERRQ(ierr);
50755b25c41SPierre Jolivet       ierr  = MPI_Allgather(&nn,1,MPI_INT,ncolsonproc,1,MPI_INT,PetscObjectComm((PetscObject)mat));CHKERRMPI(ierr);
508fcd7ac73SHong Zhang       nctot = 0; for (j=0; j<size; j++) nctot += ncolsonproc[j];
509fcd7ac73SHong Zhang       if (!nctot) {
510fcd7ac73SHong Zhang         ierr = PetscInfo(mat,"Coloring of matrix has some unneeded colors with no corresponding rows\n");CHKERRQ(ierr);
511fcd7ac73SHong Zhang       }
512fcd7ac73SHong Zhang 
513fcd7ac73SHong Zhang       disp[0] = 0;
514fcd7ac73SHong Zhang       for (j=1; j<size; j++) {
515fcd7ac73SHong Zhang         disp[j] = disp[j-1] + ncolsonproc[j-1];
516fcd7ac73SHong Zhang       }
517fcd7ac73SHong Zhang 
518d3825b63SHong Zhang       /* Get cols, the complete list of columns for this color on each process */
519854ce69bSBarry Smith       ierr = PetscMalloc1(nctot+1,&cols);CHKERRQ(ierr);
520ffc4695bSBarry Smith       ierr = MPI_Allgatherv((void*)is,n,MPIU_INT,cols,ncolsonproc,disp,MPIU_INT,PetscObjectComm((PetscObject)mat));CHKERRMPI(ierr);
5215bdb020cSBarry Smith     } else if (ctype == IS_COLORING_LOCAL) {
522fcd7ac73SHong Zhang       /* Determine local number of columns of this color on this process, including ghost points */
523fcd7ac73SHong Zhang       nctot = n;
524071fcb05SBarry Smith       cols  = (PetscInt*)is;
525fcd7ac73SHong Zhang     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not provided for this MatFDColoring type");
526fcd7ac73SHong Zhang 
5271b97d346SHong Zhang     /* Mark all rows affect by these columns */
528580bdb30SBarry Smith     ierr    = PetscArrayzero(rowhit,m);CHKERRQ(ierr);
5290d1c53f1SHong Zhang     bs2     = bs*bs;
5304b2e90caSHong Zhang     nrows_i = 0;
5311b97d346SHong Zhang     for (j=0; j<nctot; j++) { /* loop over columns*/
5325bdb020cSBarry Smith       if (ctype == IS_COLORING_LOCAL) {
533fcd7ac73SHong Zhang         col = ltog[cols[j]];
534fcd7ac73SHong Zhang       } else {
535fcd7ac73SHong Zhang         col = cols[j];
536fcd7ac73SHong Zhang       }
537e3225e9dSHong Zhang       if (col >= cstart && col < cend) { /* column is in A, diagonal block of mat */
538071fcb05SBarry Smith         tmp      = A_ci[col-cstart];
539071fcb05SBarry Smith         row      = A_cj + tmp;
540071fcb05SBarry Smith         nrows    = A_ci[col-cstart+1] - tmp;
5414b2e90caSHong Zhang         nrows_i += nrows;
542071fcb05SBarry Smith 
543fcd7ac73SHong Zhang         /* loop over columns of A marking them in rowhit */
544d3825b63SHong Zhang         for (k=0; k<nrows; k++) {
545d3825b63SHong Zhang           /* set valaddrhit for part A */
546071fcb05SBarry Smith           spidx            = bs2*spidxA[tmp + k];
547e3225e9dSHong Zhang           valaddrhit[*row] = &A_val[spidx];
548a774a6f1SHong Zhang           rowhit[*row++]   = col - cstart + 1; /* local column index */
549fcd7ac73SHong Zhang         }
550e3225e9dSHong Zhang       } else { /* column is in B, off-diagonal block of mat */
551fcd7ac73SHong Zhang #if defined(PETSC_USE_CTABLE)
5520d1c53f1SHong Zhang         ierr = PetscTableFind(colmap,col+1,&colb);CHKERRQ(ierr);
553fcd7ac73SHong Zhang         colb--;
554fcd7ac73SHong Zhang #else
5550d1c53f1SHong Zhang         colb = colmap[col] - 1; /* local column index */
556fcd7ac73SHong Zhang #endif
557fcd7ac73SHong Zhang         if (colb == -1) {
558d3825b63SHong Zhang           nrows = 0;
559fcd7ac73SHong Zhang         } else {
5600d1c53f1SHong Zhang           colb  = colb/bs;
561071fcb05SBarry Smith           tmp   = B_ci[colb];
562071fcb05SBarry Smith           row   = B_cj + tmp;
563071fcb05SBarry Smith           nrows = B_ci[colb+1] - tmp;
564fcd7ac73SHong Zhang         }
5654b2e90caSHong Zhang         nrows_i += nrows;
566fcd7ac73SHong Zhang         /* loop over columns of B marking them in rowhit */
567d3825b63SHong Zhang         for (k=0; k<nrows; k++) {
568d3825b63SHong Zhang           /* set valaddrhit for part B */
569071fcb05SBarry Smith           spidx            = bs2*spidxB[tmp + k];
570e3225e9dSHong Zhang           valaddrhit[*row] = &B_val[spidx];
57170e7395fSHong Zhang           rowhit[*row++]   = colb + 1 + cend - cstart; /* local column index */
572fcd7ac73SHong Zhang         }
573fcd7ac73SHong Zhang       }
574fcd7ac73SHong Zhang     }
5754b2e90caSHong Zhang     c->nrows[i] = nrows_i;
5768bc97078SHong Zhang 
5770df34763SHong Zhang     if (c->htype[0] == 'd') {
578d3825b63SHong Zhang       for (j=0; j<m; j++) {
579fcd7ac73SHong Zhang         if (rowhit[j]) {
580573f477fSHong Zhang           Jentry[nz].row     = j;              /* local row index */
581573f477fSHong Zhang           Jentry[nz].col     = rowhit[j] - 1;  /* local column index */
582573f477fSHong Zhang           Jentry[nz].valaddr = valaddrhit[j];  /* address of mat value for this entry */
583573f477fSHong Zhang           nz++;
584fcd7ac73SHong Zhang         }
585fcd7ac73SHong Zhang       }
5860df34763SHong Zhang     } else { /* c->htype == 'wp' */
5870df34763SHong Zhang       for (j=0; j<m; j++) {
5880df34763SHong Zhang         if (rowhit[j]) {
5890df34763SHong Zhang           Jentry2[nz].row     = j;              /* local row index */
5900df34763SHong Zhang           Jentry2[nz].valaddr = valaddrhit[j];  /* address of mat value for this entry */
5910df34763SHong Zhang           nz++;
5920df34763SHong Zhang         }
5930df34763SHong Zhang       }
5940df34763SHong Zhang     }
595071fcb05SBarry Smith     if (ctype == IS_COLORING_GLOBAL) {
596fcd7ac73SHong Zhang       ierr = PetscFree(cols);CHKERRQ(ierr);
597fcd7ac73SHong Zhang     }
598071fcb05SBarry Smith   }
599071fcb05SBarry Smith   if (ctype == IS_COLORING_GLOBAL) {
600071fcb05SBarry Smith     ierr = PetscFree2(ncolsonproc,disp);CHKERRQ(ierr);
601071fcb05SBarry Smith   }
602fcd7ac73SHong Zhang 
603a8971b87SHong Zhang   if (bcols > 1) { /* reorder Jentry for faster MatFDColoringApply() */
604a8971b87SHong Zhang     ierr = MatFDColoringSetUpBlocked_AIJ_Private(mat,c,nz);CHKERRQ(ierr);
605531e53bdSHong Zhang   }
606531e53bdSHong Zhang 
6070d1c53f1SHong Zhang   if (isBAIJ) {
6080d1c53f1SHong Zhang     ierr = MatRestoreColumnIJ_SeqBAIJ_Color(A,0,PETSC_FALSE,PETSC_FALSE,&ncols,&A_ci,&A_cj,&spidxA,NULL);CHKERRQ(ierr);
6090d1c53f1SHong Zhang     ierr = MatRestoreColumnIJ_SeqBAIJ_Color(B,0,PETSC_FALSE,PETSC_FALSE,&ncols,&B_ci,&B_cj,&spidxB,NULL);CHKERRQ(ierr);
610785e854fSJed Brown     ierr = PetscMalloc1(bs*mat->rmap->n,&c->dy);CHKERRQ(ierr);
611d4002b98SHong Zhang   } else if (isSELL) {
612d4002b98SHong Zhang     ierr = MatRestoreColumnIJ_SeqSELL_Color(A,0,PETSC_FALSE,PETSC_FALSE,&ncols,&A_ci,&A_cj,&spidxA,NULL);CHKERRQ(ierr);
613d4002b98SHong Zhang     ierr = MatRestoreColumnIJ_SeqSELL_Color(B,0,PETSC_FALSE,PETSC_FALSE,&ncols,&B_ci,&B_cj,&spidxB,NULL);CHKERRQ(ierr);
6140d1c53f1SHong Zhang   } else {
6150d1c53f1SHong Zhang     ierr = MatRestoreColumnIJ_SeqAIJ_Color(A,0,PETSC_FALSE,PETSC_FALSE,&ncols,&A_ci,&A_cj,&spidxA,NULL);CHKERRQ(ierr);
6160d1c53f1SHong Zhang     ierr = MatRestoreColumnIJ_SeqAIJ_Color(B,0,PETSC_FALSE,PETSC_FALSE,&ncols,&B_ci,&B_cj,&spidxB,NULL);CHKERRQ(ierr);
6170d1c53f1SHong Zhang   }
6180d1c53f1SHong Zhang 
619071fcb05SBarry Smith   ierr = ISColoringRestoreIS(iscoloring,PETSC_OWN_POINTER,&c->isa);CHKERRQ(ierr);
620a8971b87SHong Zhang   ierr = PetscFree2(rowhit,valaddrhit);CHKERRQ(ierr);
621a8971b87SHong Zhang 
6225bdb020cSBarry Smith   if (ctype == IS_COLORING_LOCAL) {
62372c15787SHong Zhang     ierr = ISLocalToGlobalMappingRestoreIndices(map,&ltog);CHKERRQ(ierr);
62472c15787SHong Zhang   }
625b3e1f37bSHong Zhang   ierr = PetscInfo3(c,"ncolors %D, brows %D and bcols %D are used.\n",c->ncolors,c->brows,c->bcols);CHKERRQ(ierr);
626fcd7ac73SHong Zhang   PetscFunctionReturn(0);
627fcd7ac73SHong Zhang }
62893dfae19SHong Zhang 
62993dfae19SHong Zhang PetscErrorCode MatFDColoringCreate_MPIXAIJ(Mat mat,ISColoring iscoloring,MatFDColoring c)
63093dfae19SHong Zhang {
631531e53bdSHong Zhang   PetscErrorCode ierr;
632a8971b87SHong Zhang   PetscInt       bs,nis=iscoloring->n,m=mat->rmap->n;
633d4002b98SHong Zhang   PetscBool      isBAIJ,isSELL;
634531e53bdSHong Zhang 
63593dfae19SHong Zhang   PetscFunctionBegin;
636531e53bdSHong Zhang   /* set default brows and bcols for speedup inserting the dense matrix into sparse Jacobian;
637531e53bdSHong Zhang    bcols is chosen s.t. dy-array takes 50% of memory space as mat */
638531e53bdSHong Zhang   ierr = MatGetBlockSize(mat,&bs);CHKERRQ(ierr);
639b9e7e5c1SBarry Smith   ierr = PetscObjectBaseTypeCompare((PetscObject)mat,MATMPIBAIJ,&isBAIJ);CHKERRQ(ierr);
640d4002b98SHong Zhang   ierr = PetscObjectTypeCompare((PetscObject)mat,MATMPISELL,&isSELL);CHKERRQ(ierr);
641b8dfa574SBarry Smith   if (isBAIJ || m == 0) {
642a8971b87SHong Zhang     c->brows = m;
643a8971b87SHong Zhang     c->bcols = 1;
644d4002b98SHong Zhang   } else if (isSELL) {
645f4b713efSHong Zhang     /* bcols is chosen s.t. dy-array takes 50% of local memory space as mat */
646d4002b98SHong Zhang     Mat_MPISELL *sell=(Mat_MPISELL*)mat->data;
647d4002b98SHong Zhang     Mat_SeqSELL *spA,*spB;
648f4b713efSHong Zhang     Mat        A,B;
649f4b713efSHong Zhang     PetscInt   nz,brows,bcols;
650f4b713efSHong Zhang     PetscReal  mem;
651f4b713efSHong Zhang 
652d4002b98SHong Zhang     bs    = 1; /* only bs=1 is supported for MPISELL matrix */
653f4b713efSHong Zhang 
654d4002b98SHong Zhang     A = sell->A;  spA = (Mat_SeqSELL*)A->data;
655d4002b98SHong Zhang     B = sell->B;  spB = (Mat_SeqSELL*)B->data;
656f4b713efSHong Zhang     nz = spA->nz + spB->nz; /* total local nonzero entries of mat */
657f4b713efSHong Zhang     mem = nz*(sizeof(PetscScalar) + sizeof(PetscInt)) + 3*m*sizeof(PetscInt);
658f4b713efSHong Zhang     bcols = (PetscInt)(0.5*mem /(m*sizeof(PetscScalar)));
659f4b713efSHong Zhang     brows = 1000/bcols;
660f4b713efSHong Zhang     if (bcols > nis) bcols = nis;
661f4b713efSHong Zhang     if (brows == 0 || brows > m) brows = m;
662f4b713efSHong Zhang     c->brows = brows;
663f4b713efSHong Zhang     c->bcols = bcols;
664531e53bdSHong Zhang   } else { /* mpiaij matrix */
665531e53bdSHong Zhang     /* bcols is chosen s.t. dy-array takes 50% of local memory space as mat */
666531e53bdSHong Zhang     Mat_MPIAIJ *aij=(Mat_MPIAIJ*)mat->data;
667531e53bdSHong Zhang     Mat_SeqAIJ *spA,*spB;
668531e53bdSHong Zhang     Mat        A,B;
669a8971b87SHong Zhang     PetscInt   nz,brows,bcols;
670531e53bdSHong Zhang     PetscReal  mem;
671531e53bdSHong Zhang 
672531e53bdSHong Zhang     bs    = 1; /* only bs=1 is supported for MPIAIJ matrix */
673531e53bdSHong Zhang 
674531e53bdSHong Zhang     A = aij->A;  spA = (Mat_SeqAIJ*)A->data;
675531e53bdSHong Zhang     B = aij->B;  spB = (Mat_SeqAIJ*)B->data;
676531e53bdSHong Zhang     nz = spA->nz + spB->nz; /* total local nonzero entries of mat */
677531e53bdSHong Zhang     mem = nz*(sizeof(PetscScalar) + sizeof(PetscInt)) + 3*m*sizeof(PetscInt);
678531e53bdSHong Zhang     bcols = (PetscInt)(0.5*mem /(m*sizeof(PetscScalar)));
679531e53bdSHong Zhang     brows = 1000/bcols;
680531e53bdSHong Zhang     if (bcols > nis) bcols = nis;
681531e53bdSHong Zhang     if (brows == 0 || brows > m) brows = m;
682531e53bdSHong Zhang     c->brows = brows;
683531e53bdSHong Zhang     c->bcols = bcols;
684531e53bdSHong Zhang   }
685a8971b87SHong Zhang 
686a8971b87SHong Zhang   c->M       = mat->rmap->N/bs;         /* set the global rows and columns and local rows */
687a8971b87SHong Zhang   c->N       = mat->cmap->N/bs;
688a8971b87SHong Zhang   c->m       = mat->rmap->n/bs;
689a8971b87SHong Zhang   c->rstart  = mat->rmap->rstart/bs;
690a8971b87SHong Zhang   c->ncolors = nis;
69193dfae19SHong Zhang   PetscFunctionReturn(0);
69293dfae19SHong Zhang }
693bdaf1daeSBarry Smith 
694bdaf1daeSBarry Smith /*@C
695bdaf1daeSBarry Smith 
696bdaf1daeSBarry Smith     MatFDColoringSetValues - takes a matrix in compressed color format and enters the matrix into a PETSc Mat
697bdaf1daeSBarry Smith 
698bdaf1daeSBarry Smith    Collective on J
699bdaf1daeSBarry Smith 
700bdaf1daeSBarry Smith    Input Parameters:
701bdaf1daeSBarry Smith +    J - the sparse matrix
702bdaf1daeSBarry Smith .    coloring - created with MatFDColoringCreate() and a local coloring
703bdaf1daeSBarry Smith -    y - column major storage of matrix values with one color of values per column, the number of rows of y should match
704bdaf1daeSBarry Smith          the number of local rows of J and the number of columns is the number of colors.
705bdaf1daeSBarry Smith 
706bdaf1daeSBarry Smith    Level: intermediate
707bdaf1daeSBarry Smith 
7086a9b8d82SBarry Smith    Notes: the matrix in compressed color format may come from an Automatic Differentiation code
709bdaf1daeSBarry Smith 
710bdaf1daeSBarry Smith    The code will be slightly faster if MatFDColoringSetBlockSize(coloring,PETSC_DEFAULT,nc); is called immediately after creating the coloring
711bdaf1daeSBarry Smith 
712bdaf1daeSBarry Smith .seealso: MatFDColoringCreate(), ISColoring, ISColoringCreate(), ISColoringSetType(), IS_COLORING_LOCAL, MatFDColoringSetBlockSize()
713bdaf1daeSBarry Smith 
714bdaf1daeSBarry Smith @*/
715bdaf1daeSBarry Smith PetscErrorCode  MatFDColoringSetValues(Mat J,MatFDColoring coloring,const PetscScalar *y)
716bdaf1daeSBarry Smith {
717bdaf1daeSBarry Smith   PetscErrorCode    ierr;
718bdaf1daeSBarry Smith   MatEntry2         *Jentry2;
719bdaf1daeSBarry Smith   PetscInt          row,i,nrows_k,l,ncolors,nz = 0,bcols,nbcols = 0;
720bdaf1daeSBarry Smith   const PetscInt    *nrows;
721bdaf1daeSBarry Smith   PetscBool         eq;
722bdaf1daeSBarry Smith 
723bdaf1daeSBarry Smith   PetscFunctionBegin;
724bdaf1daeSBarry Smith   PetscValidHeaderSpecific(J,MAT_CLASSID,1);
725bdaf1daeSBarry Smith   PetscValidHeaderSpecific(coloring,MAT_FDCOLORING_CLASSID,2);
726bdaf1daeSBarry Smith   ierr = PetscObjectCompareId((PetscObject)J,coloring->matid,&eq);CHKERRQ(ierr);
727bdaf1daeSBarry Smith   if (!eq) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_WRONG,"Matrix used with MatFDColoringSetValues() must be that used with MatFDColoringCreate()");
728bdaf1daeSBarry Smith   Jentry2 = coloring->matentry2;
729bdaf1daeSBarry Smith   nrows   = coloring->nrows;
730bdaf1daeSBarry Smith   ncolors = coloring->ncolors;
731bdaf1daeSBarry Smith   bcols   = coloring->bcols;
732bdaf1daeSBarry Smith 
733bdaf1daeSBarry Smith   for (i=0; i<ncolors; i+=bcols) {
734bdaf1daeSBarry Smith     nrows_k = nrows[nbcols++];
735bdaf1daeSBarry Smith     for (l=0; l<nrows_k; l++) {
736bdaf1daeSBarry Smith       row                      = Jentry2[nz].row;   /* local row index */
737bdaf1daeSBarry Smith       *(Jentry2[nz++].valaddr) = y[row];
738bdaf1daeSBarry Smith     }
739bdaf1daeSBarry Smith     y += bcols*coloring->m;
740bdaf1daeSBarry Smith   }
741bdaf1daeSBarry Smith   ierr = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
742bdaf1daeSBarry Smith   ierr = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
743bdaf1daeSBarry Smith   PetscFunctionReturn(0);
744bdaf1daeSBarry Smith }
745