xref: /petsc/src/mat/impls/aij/mpi/fdmpiaij.c (revision 5059b3035c851581e12b227c8f14dc1a4347a63f)
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;
179fcd7ac73SHong Zhang 
180fcd7ac73SHong Zhang   PetscFunctionBegin;
181b470e4b4SRichard Tran Mills   ierr = VecBindToCPU(x1,PETSC_TRUE);CHKERRQ(ierr);
182531c7667SBarry 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");
1838bc97078SHong Zhang   /* (1) Set w1 = F(x1) */
184fcd7ac73SHong Zhang   if (!coloring->fset) {
185fcd7ac73SHong Zhang     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
186f6af9589SHong Zhang     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
187fcd7ac73SHong Zhang     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
188fcd7ac73SHong Zhang   } else {
189fcd7ac73SHong Zhang     coloring->fset = PETSC_FALSE;
190fcd7ac73SHong Zhang   }
191fcd7ac73SHong Zhang 
1928bc97078SHong Zhang   /* (2) Compute vscale = 1./dx - the local scale factors, including ghost points */
193f6af9589SHong Zhang   if (coloring->htype[0] == 'w') {
194684f2004SHong Zhang     /* vscale = 1./dx is a constant scalar */
195f6af9589SHong Zhang     ierr = VecNorm(x1,NORM_2,&unorm);CHKERRQ(ierr);
196c53567a0SHong Zhang     dx = 1.0/(PetscSqrtReal(1.0 + unorm)*epsilon);
19770e7395fSHong Zhang   } else {
19874d3cef9SHong Zhang     ierr = VecGetLocalSize(x1,&nxloc);CHKERRQ(ierr);
1995edff71fSBarry Smith     ierr = VecGetArrayRead(x1,&xx);CHKERRQ(ierr);
2008bc97078SHong Zhang     ierr = VecGetArray(vscale,&vscale_array);CHKERRQ(ierr);
20174d3cef9SHong Zhang     for (col=0; col<nxloc; col++) {
202fcd7ac73SHong Zhang       dx = xx[col];
20374d3cef9SHong Zhang       if (PetscAbsScalar(dx) < umin) {
20474d3cef9SHong Zhang         if (PetscRealPart(dx) >= 0.0)      dx = umin;
20574d3cef9SHong Zhang         else if (PetscRealPart(dx) < 0.0) dx = -umin;
20674d3cef9SHong Zhang       }
207fcd7ac73SHong Zhang       dx               *= epsilon;
20874d3cef9SHong Zhang       vscale_array[col] = 1.0/dx;
209f6af9589SHong Zhang     }
2105edff71fSBarry Smith     ierr = VecRestoreArrayRead(x1,&xx);CHKERRQ(ierr);
2118bc97078SHong Zhang     ierr = VecRestoreArray(vscale,&vscale_array);CHKERRQ(ierr);
21270e7395fSHong Zhang   }
213684f2004SHong Zhang   if (ctype == IS_COLORING_GLOBAL && coloring->htype[0] == 'd') {
2148bc97078SHong Zhang     ierr = VecGhostUpdateBegin(vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2158bc97078SHong Zhang     ierr = VecGhostUpdateEnd(vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
216fcd7ac73SHong Zhang   }
217fcd7ac73SHong Zhang 
2188bc97078SHong Zhang   /* (3) Loop over each color */
2198bc97078SHong Zhang   if (!coloring->w3) {
2208bc97078SHong Zhang     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
2218bc97078SHong Zhang     ierr = PetscLogObjectParent((PetscObject)coloring,(PetscObject)coloring->w3);CHKERRQ(ierr);
2228bc97078SHong Zhang   }
2238bc97078SHong Zhang   w3 = coloring->w3;
224fcd7ac73SHong Zhang 
2258bc97078SHong Zhang   ierr = VecGetOwnershipRange(x1,&cstart,&cend);CHKERRQ(ierr); /* used by ghosted vscale */
2269e917edbSHong Zhang   if (vscale) {
2278bc97078SHong Zhang     ierr = VecGetArray(vscale,&vscale_array);CHKERRQ(ierr);
2289e917edbSHong Zhang   }
2298bc97078SHong Zhang   nz = 0;
230e2c857f8SHong Zhang 
231b3e1f37bSHong Zhang   if (coloring->bcols > 1) { /* use blocked insertion of Jentry */
232b3e1f37bSHong Zhang     PetscInt    i,m=J->rmap->n,nbcols,bcols=coloring->bcols;
233e2c857f8SHong Zhang     PetscScalar *dy=coloring->dy,*dy_k;
234e2c857f8SHong Zhang 
235e2c857f8SHong Zhang     nbcols = 0;
236e2c857f8SHong Zhang     for (k=0; k<ncolors; k+=bcols) {
237e2c857f8SHong Zhang 
238e2c857f8SHong Zhang       /*
239e2c857f8SHong Zhang        (3-1) Loop over each column associated with color
240e2c857f8SHong Zhang        adding the perturbation to the vector w3 = x1 + dx.
241e2c857f8SHong Zhang        */
242e2c857f8SHong Zhang 
243e2c857f8SHong Zhang       dy_k = dy;
244e2c857f8SHong Zhang       if (k + bcols > ncolors) bcols = ncolors - k;
245e2c857f8SHong Zhang       for (i=0; i<bcols; i++) {
246ceef8a49SBarry Smith         coloring->currentcolor = k+i;
247e2c857f8SHong Zhang 
248e2c857f8SHong Zhang         ierr = VecCopy(x1,w3);CHKERRQ(ierr);
249e2c857f8SHong Zhang         ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);
250e2c857f8SHong Zhang         if (ctype == IS_COLORING_GLOBAL) w3_array -= cstart; /* shift pointer so global index can be used */
251e2c857f8SHong Zhang         if (coloring->htype[0] == 'w') {
252e2c857f8SHong Zhang           for (l=0; l<ncolumns[k+i]; l++) {
253e2c857f8SHong Zhang             col = coloring->columns[k+i][l]; /* local column (in global index!) of the matrix we are probing for */
254e2c857f8SHong Zhang             w3_array[col] += 1.0/dx;
255e2c857f8SHong Zhang           }
256e2c857f8SHong Zhang         } else { /* htype == 'ds' */
257e2c857f8SHong Zhang           vscale_array -= cstart; /* shift pointer so global index can be used */
258e2c857f8SHong Zhang           for (l=0; l<ncolumns[k+i]; l++) {
259e2c857f8SHong Zhang             col = coloring->columns[k+i][l]; /* local column (in global index!) of the matrix we are probing for */
260e2c857f8SHong Zhang             w3_array[col] += 1.0/vscale_array[col];
261e2c857f8SHong Zhang           }
262e2c857f8SHong Zhang           vscale_array += cstart;
263e2c857f8SHong Zhang         }
264e2c857f8SHong Zhang         if (ctype == IS_COLORING_GLOBAL) w3_array += cstart;
265e2c857f8SHong Zhang         ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
266e2c857f8SHong Zhang 
267e2c857f8SHong Zhang         /*
268e2c857f8SHong Zhang          (3-2) Evaluate function at w3 = x1 + dx (here dx is a vector of perturbations)
269e2c857f8SHong Zhang                            w2 = F(x1 + dx) - F(x1)
270e2c857f8SHong Zhang          */
271e2c857f8SHong Zhang         ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
272e2c857f8SHong Zhang         ierr = VecPlaceArray(w2,dy_k);CHKERRQ(ierr); /* place w2 to the array dy_i */
273e2c857f8SHong Zhang         ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
274e2c857f8SHong Zhang         ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
275e2c857f8SHong Zhang         ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr);
276e2c857f8SHong Zhang         ierr = VecResetArray(w2);CHKERRQ(ierr);
277e2c857f8SHong Zhang         dy_k += m; /* points to dy+i*nxloc */
278e2c857f8SHong Zhang       }
279e2c857f8SHong Zhang 
280e2c857f8SHong Zhang       /*
281e2c857f8SHong Zhang        (3-3) Loop over block rows of vector, putting results into Jacobian matrix
282e2c857f8SHong Zhang        */
283d880da65SHong Zhang       nrows_k = nrows[nbcols++];
284d880da65SHong Zhang 
285e2c857f8SHong Zhang       if (coloring->htype[0] == 'w') {
286e2c857f8SHong Zhang         for (l=0; l<nrows_k; l++) {
2870df34763SHong Zhang           row  = Jentry2[nz].row;   /* local row index */
288*5059b303SJunchao Zhang           /* The 'useless' ifdef is due to a bug in NVIDIA nvc 21.11, which triggers a segfault on this line. We write it in
289*5059b303SJunchao Zhang              another way, and it seems work. See https://lists.mcs.anl.gov/pipermail/petsc-users/2021-December/045158.html
290*5059b303SJunchao Zhang            */
291*5059b303SJunchao Zhang          #if defined(PETSC_USE_COMPLEX)
292*5059b303SJunchao Zhang           PetscScalar *tmp = Jentry2[nz].valaddr;
293*5059b303SJunchao Zhang           *tmp = dy[row]*dx;
294*5059b303SJunchao Zhang          #else
295*5059b303SJunchao Zhang           *(Jentry2[nz].valaddr) = dy[row]*dx;
296*5059b303SJunchao Zhang          #endif
297*5059b303SJunchao Zhang           nz++;
298e2c857f8SHong Zhang         }
299e2c857f8SHong Zhang       } else { /* htype == 'ds' */
300e2c857f8SHong Zhang         for (l=0; l<nrows_k; l++) {
301e2c857f8SHong Zhang           row = Jentry[nz].row;   /* local row index */
302*5059b303SJunchao Zhang          #if defined(PETSC_USE_COMPLEX) /* See https://lists.mcs.anl.gov/pipermail/petsc-users/2021-December/045158.html */
303*5059b303SJunchao Zhang           PetscScalar *tmp = Jentry[nz].valaddr;
304*5059b303SJunchao Zhang           *tmp = dy[row]*vscale_array[Jentry[nz].col];
305*5059b303SJunchao Zhang          #else
306d880da65SHong Zhang           *(Jentry[nz].valaddr) = dy[row]*vscale_array[Jentry[nz].col];
307*5059b303SJunchao Zhang          #endif
308e2c857f8SHong Zhang           nz++;
309e2c857f8SHong Zhang         }
310e2c857f8SHong Zhang       }
311e2c857f8SHong Zhang     }
312b3e1f37bSHong Zhang   } else { /* bcols == 1 */
3138bc97078SHong Zhang     for (k=0; k<ncolors; k++) {
3148bc97078SHong Zhang       coloring->currentcolor = k;
3159e917edbSHong Zhang 
316fcd7ac73SHong Zhang       /*
3178bc97078SHong Zhang        (3-1) Loop over each column associated with color
318f6af9589SHong Zhang        adding the perturbation to the vector w3 = x1 + dx.
319fcd7ac73SHong Zhang        */
320f6af9589SHong Zhang       ierr = VecCopy(x1,w3);CHKERRQ(ierr);
321f6af9589SHong Zhang       ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);
322a2f2d239SHong Zhang       if (ctype == IS_COLORING_GLOBAL) w3_array -= cstart; /* shift pointer so global index can be used */
323b039d6c4SHong Zhang       if (coloring->htype[0] == 'w') {
3248bc97078SHong Zhang         for (l=0; l<ncolumns[k]; l++) {
325f6af9589SHong Zhang           col = coloring->columns[k][l]; /* local column (in global index!) of the matrix we are probing for */
3269e917edbSHong Zhang           w3_array[col] += 1.0/dx;
3279e917edbSHong Zhang         }
328b039d6c4SHong Zhang       } else { /* htype == 'ds' */
329a2f2d239SHong Zhang         vscale_array -= cstart; /* shift pointer so global index can be used */
330b039d6c4SHong Zhang         for (l=0; l<ncolumns[k]; l++) {
331b039d6c4SHong Zhang           col = coloring->columns[k][l]; /* local column (in global index!) of the matrix we are probing for */
332a2f2d239SHong Zhang           w3_array[col] += 1.0/vscale_array[col];
33370e7395fSHong Zhang         }
334a2f2d239SHong Zhang         vscale_array += cstart;
335fcd7ac73SHong Zhang       }
336a2f2d239SHong Zhang       if (ctype == IS_COLORING_GLOBAL) w3_array += cstart;
337fcd7ac73SHong Zhang       ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
3389e917edbSHong Zhang 
339fcd7ac73SHong Zhang       /*
3408bc97078SHong Zhang        (3-2) Evaluate function at w3 = x1 + dx (here dx is a vector of perturbations)
341fcd7ac73SHong Zhang                            w2 = F(x1 + dx) - F(x1)
342fcd7ac73SHong Zhang        */
343fcd7ac73SHong Zhang       ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
344fcd7ac73SHong Zhang       ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
345fcd7ac73SHong Zhang       ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
346fcd7ac73SHong Zhang       ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr);
3479e917edbSHong Zhang 
3488bc97078SHong Zhang       /*
3498bc97078SHong Zhang        (3-3) Loop over rows of vector, putting results into Jacobian matrix
3508bc97078SHong Zhang        */
351c53567a0SHong Zhang       nrows_k = nrows[k];
352fcd7ac73SHong Zhang       ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
353b039d6c4SHong Zhang       if (coloring->htype[0] == 'w') {
354c53567a0SHong Zhang         for (l=0; l<nrows_k; l++) {
3550df34763SHong Zhang           row  = Jentry2[nz].row;   /* local row index */
356*5059b303SJunchao Zhang          #if defined(PETSC_USE_COMPLEX) /* See https://lists.mcs.anl.gov/pipermail/petsc-users/2021-December/045158.html */
357*5059b303SJunchao Zhang           PetscScalar *tmp = Jentry2[nz].valaddr;
358*5059b303SJunchao Zhang           *tmp = y[row]*dx;
359*5059b303SJunchao Zhang          #else
360*5059b303SJunchao Zhang           *(Jentry2[nz].valaddr) = y[row]*dx;
361*5059b303SJunchao Zhang          #endif
362*5059b303SJunchao Zhang           nz++;
3639e917edbSHong Zhang         }
364b039d6c4SHong Zhang       } else { /* htype == 'ds' */
365c53567a0SHong Zhang         for (l=0; l<nrows_k; l++) {
366b039d6c4SHong Zhang           row  = Jentry[nz].row;   /* local row index */
367*5059b303SJunchao Zhang          #if defined(PETSC_USE_COMPLEX) /* See https://lists.mcs.anl.gov/pipermail/petsc-users/2021-December/045158.html */
368*5059b303SJunchao Zhang           PetscScalar *tmp = Jentry[nz].valaddr;
369*5059b303SJunchao Zhang           *tmp = y[row]*vscale_array[Jentry[nz].col];
370*5059b303SJunchao Zhang          #else
371b039d6c4SHong Zhang           *(Jentry[nz].valaddr) = y[row]*vscale_array[Jentry[nz].col];
372*5059b303SJunchao Zhang          #endif
373573f477fSHong Zhang           nz++;
374fcd7ac73SHong Zhang         }
375b039d6c4SHong Zhang       }
376fcd7ac73SHong Zhang       ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
3778bc97078SHong Zhang     }
378b3e1f37bSHong Zhang   }
379b3e1f37bSHong Zhang 
380e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
381c70f7ee4SJunchao Zhang   if (J->offloadmask != PETSC_OFFLOAD_UNALLOCATED) J->offloadmask = PETSC_OFFLOAD_CPU;
382e2cf4d64SStefano Zampini #endif
383f5aae955SHong Zhang   ierr = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
384f5aae955SHong Zhang   ierr = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3859e917edbSHong Zhang   if (vscale) {
3868bc97078SHong Zhang     ierr = VecRestoreArray(vscale,&vscale_array);CHKERRQ(ierr);
3879e917edbSHong Zhang   }
388fcd7ac73SHong Zhang   coloring->currentcolor = -1;
389b470e4b4SRichard Tran Mills   ierr = VecBindToCPU(x1,PETSC_FALSE);CHKERRQ(ierr);
390fcd7ac73SHong Zhang   PetscFunctionReturn(0);
391fcd7ac73SHong Zhang }
392fcd7ac73SHong Zhang 
393f86b9fbaSHong Zhang PetscErrorCode MatFDColoringSetUp_MPIXAIJ(Mat mat,ISColoring iscoloring,MatFDColoring c)
394fcd7ac73SHong Zhang {
395fcd7ac73SHong Zhang   PetscErrorCode         ierr;
396fcd7ac73SHong Zhang   PetscMPIInt            size,*ncolsonproc,*disp,nn;
397e3225e9dSHong Zhang   PetscInt               i,n,nrows,nrows_i,j,k,m,ncols,col,*rowhit,cstart,cend,colb;
39872c15787SHong Zhang   const PetscInt         *is,*A_ci,*A_cj,*B_ci,*B_cj,*row=NULL,*ltog=NULL;
399071fcb05SBarry Smith   PetscInt               nis=iscoloring->n,nctot,*cols,tmp = 0;
400fcd7ac73SHong Zhang   ISLocalToGlobalMapping map=mat->cmap->mapping;
401a8971b87SHong Zhang   PetscInt               ctype=c->ctype,*spidxA,*spidxB,nz,bs,bs2,spidx;
4020d1c53f1SHong Zhang   Mat                    A,B;
403e3225e9dSHong Zhang   PetscScalar            *A_val,*B_val,**valaddrhit;
404573f477fSHong Zhang   MatEntry               *Jentry;
4050df34763SHong Zhang   MatEntry2              *Jentry2;
406d4002b98SHong Zhang   PetscBool              isBAIJ,isSELL;
407531e53bdSHong Zhang   PetscInt               bcols=c->bcols;
4080d1c53f1SHong Zhang #if defined(PETSC_USE_CTABLE)
4090d1c53f1SHong Zhang   PetscTable             colmap=NULL;
4100d1c53f1SHong Zhang #else
4110d1c53f1SHong Zhang   PetscInt               *colmap=NULL;     /* local col number of off-diag col */
4120d1c53f1SHong Zhang #endif
413fcd7ac73SHong Zhang 
414fcd7ac73SHong Zhang   PetscFunctionBegin;
4155bdb020cSBarry Smith   if (ctype == IS_COLORING_LOCAL) {
41672c15787SHong 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");
41772c15787SHong Zhang     ierr = ISLocalToGlobalMappingGetIndices(map,&ltog);CHKERRQ(ierr);
41872c15787SHong Zhang   }
419fcd7ac73SHong Zhang 
4200d1c53f1SHong Zhang   ierr = MatGetBlockSize(mat,&bs);CHKERRQ(ierr);
421b9e7e5c1SBarry Smith   ierr = PetscObjectBaseTypeCompare((PetscObject)mat,MATMPIBAIJ,&isBAIJ);CHKERRQ(ierr);
422d4002b98SHong Zhang   ierr = PetscObjectTypeCompare((PetscObject)mat,MATMPISELL,&isSELL);CHKERRQ(ierr);
423e3225e9dSHong Zhang   if (isBAIJ) {
424195687c5SHong Zhang     Mat_MPIBAIJ *baij=(Mat_MPIBAIJ*)mat->data;
4256c145f34SHong Zhang     Mat_SeqBAIJ *spA,*spB;
426195687c5SHong Zhang     A = baij->A;  spA = (Mat_SeqBAIJ*)A->data; A_val = spA->a;
427195687c5SHong Zhang     B = baij->B;  spB = (Mat_SeqBAIJ*)B->data; B_val = spB->a;
4280d1c53f1SHong Zhang     nz = spA->nz + spB->nz; /* total nonzero entries of mat */
429195687c5SHong Zhang     if (!baij->colmap) {
4300d1c53f1SHong Zhang       ierr = MatCreateColmap_MPIBAIJ_Private(mat);CHKERRQ(ierr);
4310d1c53f1SHong Zhang     }
432097e26fcSJed Brown     colmap = baij->colmap;
4330d1c53f1SHong Zhang     ierr = MatGetColumnIJ_SeqBAIJ_Color(A,0,PETSC_FALSE,PETSC_FALSE,&ncols,&A_ci,&A_cj,&spidxA,NULL);CHKERRQ(ierr);
4340d1c53f1SHong Zhang     ierr = MatGetColumnIJ_SeqBAIJ_Color(B,0,PETSC_FALSE,PETSC_FALSE,&ncols,&B_ci,&B_cj,&spidxB,NULL);CHKERRQ(ierr);
435195687c5SHong Zhang 
436195687c5SHong Zhang     if (ctype == IS_COLORING_GLOBAL && c->htype[0] == 'd') {  /* create vscale for storing dx */
437195687c5SHong Zhang       PetscInt    *garray;
438785e854fSJed Brown       ierr = PetscMalloc1(B->cmap->n,&garray);CHKERRQ(ierr);
439195687c5SHong Zhang       for (i=0; i<baij->B->cmap->n/bs; i++) {
440195687c5SHong Zhang         for (j=0; j<bs; j++) {
441195687c5SHong Zhang           garray[i*bs+j] = bs*baij->garray[i]+j;
442195687c5SHong Zhang         }
443195687c5SHong Zhang       }
444195687c5SHong Zhang       ierr = VecCreateGhost(PetscObjectComm((PetscObject)mat),mat->cmap->n,PETSC_DETERMINE,B->cmap->n,garray,&c->vscale);CHKERRQ(ierr);
445b470e4b4SRichard Tran Mills       ierr = VecBindToCPU(c->vscale,PETSC_TRUE);CHKERRQ(ierr);
446195687c5SHong Zhang       ierr = PetscFree(garray);CHKERRQ(ierr);
447195687c5SHong Zhang     }
448d4002b98SHong Zhang   } else if (isSELL) {
449d4002b98SHong Zhang     Mat_MPISELL *sell=(Mat_MPISELL*)mat->data;
450d4002b98SHong Zhang     Mat_SeqSELL *spA,*spB;
451d4002b98SHong Zhang     A = sell->A;  spA = (Mat_SeqSELL*)A->data; A_val = spA->val;
452d4002b98SHong Zhang     B = sell->B;  spB = (Mat_SeqSELL*)B->data; B_val = spB->val;
453f4b713efSHong Zhang     nz = spA->nz + spB->nz; /* total nonzero entries of mat */
454d4002b98SHong Zhang     if (!sell->colmap) {
455f4b713efSHong Zhang       /* Allow access to data structures of local part of matrix
456f4b713efSHong Zhang        - creates aij->colmap which maps global column number to local number in part B */
457d4002b98SHong Zhang       ierr = MatCreateColmap_MPISELL_Private(mat);CHKERRQ(ierr);
458f4b713efSHong Zhang     }
459d4002b98SHong Zhang     colmap = sell->colmap;
460d4002b98SHong Zhang     ierr = MatGetColumnIJ_SeqSELL_Color(A,0,PETSC_FALSE,PETSC_FALSE,&ncols,&A_ci,&A_cj,&spidxA,NULL);CHKERRQ(ierr);
461d4002b98SHong Zhang     ierr = MatGetColumnIJ_SeqSELL_Color(B,0,PETSC_FALSE,PETSC_FALSE,&ncols,&B_ci,&B_cj,&spidxB,NULL);CHKERRQ(ierr);
462f4b713efSHong Zhang 
463f4b713efSHong Zhang     bs = 1; /* only bs=1 is supported for non MPIBAIJ matrix */
464f4b713efSHong Zhang 
465f4b713efSHong Zhang     if (ctype == IS_COLORING_GLOBAL && c->htype[0] == 'd') { /* create vscale for storing dx */
466d4002b98SHong Zhang       ierr = VecCreateGhost(PetscObjectComm((PetscObject)mat),mat->cmap->n,PETSC_DETERMINE,B->cmap->n,sell->garray,&c->vscale);CHKERRQ(ierr);
467b470e4b4SRichard Tran Mills       ierr = VecBindToCPU(c->vscale,PETSC_TRUE);CHKERRQ(ierr);
468f4b713efSHong Zhang     }
469e3225e9dSHong Zhang   } else {
470e3225e9dSHong Zhang     Mat_MPIAIJ *aij=(Mat_MPIAIJ*)mat->data;
471e3225e9dSHong Zhang     Mat_SeqAIJ *spA,*spB;
472e3225e9dSHong Zhang     A = aij->A;  spA = (Mat_SeqAIJ*)A->data; A_val = spA->a;
473e3225e9dSHong Zhang     B = aij->B;  spB = (Mat_SeqAIJ*)B->data; B_val = spB->a;
474e3225e9dSHong Zhang     nz = spA->nz + spB->nz; /* total nonzero entries of mat */
475e3225e9dSHong Zhang     if (!aij->colmap) {
476e3225e9dSHong Zhang       /* Allow access to data structures of local part of matrix
477e3225e9dSHong Zhang        - creates aij->colmap which maps global column number to local number in part B */
478e3225e9dSHong Zhang       ierr = MatCreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr);
479e3225e9dSHong Zhang     }
480097e26fcSJed Brown     colmap = aij->colmap;
481e3225e9dSHong Zhang     ierr = MatGetColumnIJ_SeqAIJ_Color(A,0,PETSC_FALSE,PETSC_FALSE,&ncols,&A_ci,&A_cj,&spidxA,NULL);CHKERRQ(ierr);
482e3225e9dSHong Zhang     ierr = MatGetColumnIJ_SeqAIJ_Color(B,0,PETSC_FALSE,PETSC_FALSE,&ncols,&B_ci,&B_cj,&spidxB,NULL);CHKERRQ(ierr);
483e3225e9dSHong Zhang 
484e3225e9dSHong Zhang     bs = 1; /* only bs=1 is supported for non MPIBAIJ matrix */
485195687c5SHong Zhang 
486195687c5SHong Zhang     if (ctype == IS_COLORING_GLOBAL && c->htype[0] == 'd') { /* create vscale for storing dx */
487195687c5SHong Zhang       ierr = VecCreateGhost(PetscObjectComm((PetscObject)mat),mat->cmap->n,PETSC_DETERMINE,B->cmap->n,aij->garray,&c->vscale);CHKERRQ(ierr);
488b470e4b4SRichard Tran Mills       ierr = VecBindToCPU(c->vscale,PETSC_TRUE);CHKERRQ(ierr);
489195687c5SHong Zhang     }
4900d1c53f1SHong Zhang   }
4910d1c53f1SHong Zhang 
4920d1c53f1SHong Zhang   m      = mat->rmap->n/bs;
4930d1c53f1SHong Zhang   cstart = mat->cmap->rstart/bs;
4940d1c53f1SHong Zhang   cend   = mat->cmap->rend/bs;
495fcd7ac73SHong Zhang 
496071fcb05SBarry Smith   ierr = PetscMalloc2(nis,&c->ncolumns,nis,&c->columns);CHKERRQ(ierr);
497071fcb05SBarry Smith   ierr = PetscMalloc1(nis,&c->nrows);CHKERRQ(ierr);
4988bc97078SHong Zhang   ierr = PetscLogObjectMemory((PetscObject)c,3*nis*sizeof(PetscInt));CHKERRQ(ierr);
499fcd7ac73SHong Zhang 
5000df34763SHong Zhang   if (c->htype[0] == 'd') {
501785e854fSJed Brown     ierr        = PetscMalloc1(nz,&Jentry);CHKERRQ(ierr);
5028bc97078SHong Zhang     ierr        = PetscLogObjectMemory((PetscObject)c,nz*sizeof(MatEntry));CHKERRQ(ierr);
5038bc97078SHong Zhang     c->matentry = Jentry;
5040df34763SHong Zhang   } else if (c->htype[0] == 'w') {
505785e854fSJed Brown     ierr         = PetscMalloc1(nz,&Jentry2);CHKERRQ(ierr);
5060df34763SHong Zhang     ierr         = PetscLogObjectMemory((PetscObject)c,nz*sizeof(MatEntry2));CHKERRQ(ierr);
5070df34763SHong Zhang     c->matentry2 = Jentry2;
5080df34763SHong Zhang   } else SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"htype is not supported");
509a774a6f1SHong Zhang 
510dcca6d9dSJed Brown   ierr = PetscMalloc2(m+1,&rowhit,m+1,&valaddrhit);CHKERRQ(ierr);
511fcd7ac73SHong Zhang   nz   = 0;
512071fcb05SBarry Smith   ierr = ISColoringGetIS(iscoloring,PETSC_OWN_POINTER, PETSC_IGNORE,&c->isa);CHKERRQ(ierr);
513071fcb05SBarry Smith 
514071fcb05SBarry Smith   if (ctype == IS_COLORING_GLOBAL) {
515ffc4695bSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);CHKERRMPI(ierr);
516071fcb05SBarry Smith     ierr = PetscMalloc2(size,&ncolsonproc,size,&disp);CHKERRQ(ierr);
517071fcb05SBarry Smith   }
518071fcb05SBarry Smith 
519d3825b63SHong Zhang   for (i=0; i<nis; i++) { /* for each local color */
520071fcb05SBarry Smith     ierr = ISGetLocalSize(c->isa[i],&n);CHKERRQ(ierr);
521071fcb05SBarry Smith     ierr = ISGetIndices(c->isa[i],&is);CHKERRQ(ierr);
522fcd7ac73SHong Zhang 
523fcd7ac73SHong Zhang     c->ncolumns[i] = n; /* local number of columns of this color on this process */
524071fcb05SBarry Smith     c->columns[i]  = (PetscInt*)is;
525fcd7ac73SHong Zhang 
526fcd7ac73SHong Zhang     if (ctype == IS_COLORING_GLOBAL) {
527d3825b63SHong Zhang       /* Determine nctot, the total (parallel) number of columns of this color */
528d3825b63SHong Zhang       /* ncolsonproc[j]: local ncolumns on proc[j] of this color */
529fcd7ac73SHong Zhang       ierr  = PetscMPIIntCast(n,&nn);CHKERRQ(ierr);
53055b25c41SPierre Jolivet       ierr  = MPI_Allgather(&nn,1,MPI_INT,ncolsonproc,1,MPI_INT,PetscObjectComm((PetscObject)mat));CHKERRMPI(ierr);
531fcd7ac73SHong Zhang       nctot = 0; for (j=0; j<size; j++) nctot += ncolsonproc[j];
532fcd7ac73SHong Zhang       if (!nctot) {
533fcd7ac73SHong Zhang         ierr = PetscInfo(mat,"Coloring of matrix has some unneeded colors with no corresponding rows\n");CHKERRQ(ierr);
534fcd7ac73SHong Zhang       }
535fcd7ac73SHong Zhang 
536fcd7ac73SHong Zhang       disp[0] = 0;
537fcd7ac73SHong Zhang       for (j=1; j<size; j++) {
538fcd7ac73SHong Zhang         disp[j] = disp[j-1] + ncolsonproc[j-1];
539fcd7ac73SHong Zhang       }
540fcd7ac73SHong Zhang 
541d3825b63SHong Zhang       /* Get cols, the complete list of columns for this color on each process */
542854ce69bSBarry Smith       ierr = PetscMalloc1(nctot+1,&cols);CHKERRQ(ierr);
543ffc4695bSBarry Smith       ierr = MPI_Allgatherv((void*)is,n,MPIU_INT,cols,ncolsonproc,disp,MPIU_INT,PetscObjectComm((PetscObject)mat));CHKERRMPI(ierr);
5445bdb020cSBarry Smith     } else if (ctype == IS_COLORING_LOCAL) {
545fcd7ac73SHong Zhang       /* Determine local number of columns of this color on this process, including ghost points */
546fcd7ac73SHong Zhang       nctot = n;
547071fcb05SBarry Smith       cols  = (PetscInt*)is;
548fcd7ac73SHong Zhang     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not provided for this MatFDColoring type");
549fcd7ac73SHong Zhang 
5501b97d346SHong Zhang     /* Mark all rows affect by these columns */
551580bdb30SBarry Smith     ierr    = PetscArrayzero(rowhit,m);CHKERRQ(ierr);
5520d1c53f1SHong Zhang     bs2     = bs*bs;
5534b2e90caSHong Zhang     nrows_i = 0;
5541b97d346SHong Zhang     for (j=0; j<nctot; j++) { /* loop over columns*/
5555bdb020cSBarry Smith       if (ctype == IS_COLORING_LOCAL) {
556fcd7ac73SHong Zhang         col = ltog[cols[j]];
557fcd7ac73SHong Zhang       } else {
558fcd7ac73SHong Zhang         col = cols[j];
559fcd7ac73SHong Zhang       }
560e3225e9dSHong Zhang       if (col >= cstart && col < cend) { /* column is in A, diagonal block of mat */
561071fcb05SBarry Smith         tmp      = A_ci[col-cstart];
562071fcb05SBarry Smith         row      = A_cj + tmp;
563071fcb05SBarry Smith         nrows    = A_ci[col-cstart+1] - tmp;
5644b2e90caSHong Zhang         nrows_i += nrows;
565071fcb05SBarry Smith 
566fcd7ac73SHong Zhang         /* loop over columns of A marking them in rowhit */
567d3825b63SHong Zhang         for (k=0; k<nrows; k++) {
568d3825b63SHong Zhang           /* set valaddrhit for part A */
569071fcb05SBarry Smith           spidx            = bs2*spidxA[tmp + k];
570e3225e9dSHong Zhang           valaddrhit[*row] = &A_val[spidx];
571a774a6f1SHong Zhang           rowhit[*row++]   = col - cstart + 1; /* local column index */
572fcd7ac73SHong Zhang         }
573e3225e9dSHong Zhang       } else { /* column is in B, off-diagonal block of mat */
574fcd7ac73SHong Zhang #if defined(PETSC_USE_CTABLE)
5750d1c53f1SHong Zhang         ierr = PetscTableFind(colmap,col+1,&colb);CHKERRQ(ierr);
576fcd7ac73SHong Zhang         colb--;
577fcd7ac73SHong Zhang #else
5780d1c53f1SHong Zhang         colb = colmap[col] - 1; /* local column index */
579fcd7ac73SHong Zhang #endif
580fcd7ac73SHong Zhang         if (colb == -1) {
581d3825b63SHong Zhang           nrows = 0;
582fcd7ac73SHong Zhang         } else {
5830d1c53f1SHong Zhang           colb  = colb/bs;
584071fcb05SBarry Smith           tmp   = B_ci[colb];
585071fcb05SBarry Smith           row   = B_cj + tmp;
586071fcb05SBarry Smith           nrows = B_ci[colb+1] - tmp;
587fcd7ac73SHong Zhang         }
5884b2e90caSHong Zhang         nrows_i += nrows;
589fcd7ac73SHong Zhang         /* loop over columns of B marking them in rowhit */
590d3825b63SHong Zhang         for (k=0; k<nrows; k++) {
591d3825b63SHong Zhang           /* set valaddrhit for part B */
592071fcb05SBarry Smith           spidx            = bs2*spidxB[tmp + k];
593e3225e9dSHong Zhang           valaddrhit[*row] = &B_val[spidx];
59470e7395fSHong Zhang           rowhit[*row++]   = colb + 1 + cend - cstart; /* local column index */
595fcd7ac73SHong Zhang         }
596fcd7ac73SHong Zhang       }
597fcd7ac73SHong Zhang     }
5984b2e90caSHong Zhang     c->nrows[i] = nrows_i;
5998bc97078SHong Zhang 
6000df34763SHong Zhang     if (c->htype[0] == 'd') {
601d3825b63SHong Zhang       for (j=0; j<m; j++) {
602fcd7ac73SHong Zhang         if (rowhit[j]) {
603573f477fSHong Zhang           Jentry[nz].row     = j;              /* local row index */
604573f477fSHong Zhang           Jentry[nz].col     = rowhit[j] - 1;  /* local column index */
605573f477fSHong Zhang           Jentry[nz].valaddr = valaddrhit[j];  /* address of mat value for this entry */
606573f477fSHong Zhang           nz++;
607fcd7ac73SHong Zhang         }
608fcd7ac73SHong Zhang       }
6090df34763SHong Zhang     } else { /* c->htype == 'wp' */
6100df34763SHong Zhang       for (j=0; j<m; j++) {
6110df34763SHong Zhang         if (rowhit[j]) {
6120df34763SHong Zhang           Jentry2[nz].row     = j;              /* local row index */
6130df34763SHong Zhang           Jentry2[nz].valaddr = valaddrhit[j];  /* address of mat value for this entry */
6140df34763SHong Zhang           nz++;
6150df34763SHong Zhang         }
6160df34763SHong Zhang       }
6170df34763SHong Zhang     }
618071fcb05SBarry Smith     if (ctype == IS_COLORING_GLOBAL) {
619fcd7ac73SHong Zhang       ierr = PetscFree(cols);CHKERRQ(ierr);
620fcd7ac73SHong Zhang     }
621071fcb05SBarry Smith   }
622071fcb05SBarry Smith   if (ctype == IS_COLORING_GLOBAL) {
623071fcb05SBarry Smith     ierr = PetscFree2(ncolsonproc,disp);CHKERRQ(ierr);
624071fcb05SBarry Smith   }
625fcd7ac73SHong Zhang 
626a8971b87SHong Zhang   if (bcols > 1) { /* reorder Jentry for faster MatFDColoringApply() */
627a8971b87SHong Zhang     ierr = MatFDColoringSetUpBlocked_AIJ_Private(mat,c,nz);CHKERRQ(ierr);
628531e53bdSHong Zhang   }
629531e53bdSHong Zhang 
6300d1c53f1SHong Zhang   if (isBAIJ) {
6310d1c53f1SHong Zhang     ierr = MatRestoreColumnIJ_SeqBAIJ_Color(A,0,PETSC_FALSE,PETSC_FALSE,&ncols,&A_ci,&A_cj,&spidxA,NULL);CHKERRQ(ierr);
6320d1c53f1SHong Zhang     ierr = MatRestoreColumnIJ_SeqBAIJ_Color(B,0,PETSC_FALSE,PETSC_FALSE,&ncols,&B_ci,&B_cj,&spidxB,NULL);CHKERRQ(ierr);
633785e854fSJed Brown     ierr = PetscMalloc1(bs*mat->rmap->n,&c->dy);CHKERRQ(ierr);
634d4002b98SHong Zhang   } else if (isSELL) {
635d4002b98SHong Zhang     ierr = MatRestoreColumnIJ_SeqSELL_Color(A,0,PETSC_FALSE,PETSC_FALSE,&ncols,&A_ci,&A_cj,&spidxA,NULL);CHKERRQ(ierr);
636d4002b98SHong Zhang     ierr = MatRestoreColumnIJ_SeqSELL_Color(B,0,PETSC_FALSE,PETSC_FALSE,&ncols,&B_ci,&B_cj,&spidxB,NULL);CHKERRQ(ierr);
6370d1c53f1SHong Zhang   } else {
6380d1c53f1SHong Zhang     ierr = MatRestoreColumnIJ_SeqAIJ_Color(A,0,PETSC_FALSE,PETSC_FALSE,&ncols,&A_ci,&A_cj,&spidxA,NULL);CHKERRQ(ierr);
6390d1c53f1SHong Zhang     ierr = MatRestoreColumnIJ_SeqAIJ_Color(B,0,PETSC_FALSE,PETSC_FALSE,&ncols,&B_ci,&B_cj,&spidxB,NULL);CHKERRQ(ierr);
6400d1c53f1SHong Zhang   }
6410d1c53f1SHong Zhang 
642071fcb05SBarry Smith   ierr = ISColoringRestoreIS(iscoloring,PETSC_OWN_POINTER,&c->isa);CHKERRQ(ierr);
643a8971b87SHong Zhang   ierr = PetscFree2(rowhit,valaddrhit);CHKERRQ(ierr);
644a8971b87SHong Zhang 
6455bdb020cSBarry Smith   if (ctype == IS_COLORING_LOCAL) {
64672c15787SHong Zhang     ierr = ISLocalToGlobalMappingRestoreIndices(map,&ltog);CHKERRQ(ierr);
64772c15787SHong Zhang   }
648b3e1f37bSHong Zhang   ierr = PetscInfo3(c,"ncolors %D, brows %D and bcols %D are used.\n",c->ncolors,c->brows,c->bcols);CHKERRQ(ierr);
649fcd7ac73SHong Zhang   PetscFunctionReturn(0);
650fcd7ac73SHong Zhang }
65193dfae19SHong Zhang 
65293dfae19SHong Zhang PetscErrorCode MatFDColoringCreate_MPIXAIJ(Mat mat,ISColoring iscoloring,MatFDColoring c)
65393dfae19SHong Zhang {
654531e53bdSHong Zhang   PetscErrorCode ierr;
655a8971b87SHong Zhang   PetscInt       bs,nis=iscoloring->n,m=mat->rmap->n;
656d4002b98SHong Zhang   PetscBool      isBAIJ,isSELL;
657531e53bdSHong Zhang 
65893dfae19SHong Zhang   PetscFunctionBegin;
659531e53bdSHong Zhang   /* set default brows and bcols for speedup inserting the dense matrix into sparse Jacobian;
660531e53bdSHong Zhang    bcols is chosen s.t. dy-array takes 50% of memory space as mat */
661531e53bdSHong Zhang   ierr = MatGetBlockSize(mat,&bs);CHKERRQ(ierr);
662b9e7e5c1SBarry Smith   ierr = PetscObjectBaseTypeCompare((PetscObject)mat,MATMPIBAIJ,&isBAIJ);CHKERRQ(ierr);
663d4002b98SHong Zhang   ierr = PetscObjectTypeCompare((PetscObject)mat,MATMPISELL,&isSELL);CHKERRQ(ierr);
664b8dfa574SBarry Smith   if (isBAIJ || m == 0) {
665a8971b87SHong Zhang     c->brows = m;
666a8971b87SHong Zhang     c->bcols = 1;
667d4002b98SHong Zhang   } else if (isSELL) {
668f4b713efSHong Zhang     /* bcols is chosen s.t. dy-array takes 50% of local memory space as mat */
669d4002b98SHong Zhang     Mat_MPISELL *sell=(Mat_MPISELL*)mat->data;
670d4002b98SHong Zhang     Mat_SeqSELL *spA,*spB;
671f4b713efSHong Zhang     Mat        A,B;
672f4b713efSHong Zhang     PetscInt   nz,brows,bcols;
673f4b713efSHong Zhang     PetscReal  mem;
674f4b713efSHong Zhang 
675d4002b98SHong Zhang     bs    = 1; /* only bs=1 is supported for MPISELL matrix */
676f4b713efSHong Zhang 
677d4002b98SHong Zhang     A = sell->A;  spA = (Mat_SeqSELL*)A->data;
678d4002b98SHong Zhang     B = sell->B;  spB = (Mat_SeqSELL*)B->data;
679f4b713efSHong Zhang     nz = spA->nz + spB->nz; /* total local nonzero entries of mat */
680f4b713efSHong Zhang     mem = nz*(sizeof(PetscScalar) + sizeof(PetscInt)) + 3*m*sizeof(PetscInt);
681f4b713efSHong Zhang     bcols = (PetscInt)(0.5*mem /(m*sizeof(PetscScalar)));
682f4b713efSHong Zhang     brows = 1000/bcols;
683f4b713efSHong Zhang     if (bcols > nis) bcols = nis;
684f4b713efSHong Zhang     if (brows == 0 || brows > m) brows = m;
685f4b713efSHong Zhang     c->brows = brows;
686f4b713efSHong Zhang     c->bcols = bcols;
687531e53bdSHong Zhang   } else { /* mpiaij matrix */
688531e53bdSHong Zhang     /* bcols is chosen s.t. dy-array takes 50% of local memory space as mat */
689531e53bdSHong Zhang     Mat_MPIAIJ *aij=(Mat_MPIAIJ*)mat->data;
690531e53bdSHong Zhang     Mat_SeqAIJ *spA,*spB;
691531e53bdSHong Zhang     Mat        A,B;
692a8971b87SHong Zhang     PetscInt   nz,brows,bcols;
693531e53bdSHong Zhang     PetscReal  mem;
694531e53bdSHong Zhang 
695531e53bdSHong Zhang     bs    = 1; /* only bs=1 is supported for MPIAIJ matrix */
696531e53bdSHong Zhang 
697531e53bdSHong Zhang     A = aij->A;  spA = (Mat_SeqAIJ*)A->data;
698531e53bdSHong Zhang     B = aij->B;  spB = (Mat_SeqAIJ*)B->data;
699531e53bdSHong Zhang     nz = spA->nz + spB->nz; /* total local nonzero entries of mat */
700531e53bdSHong Zhang     mem = nz*(sizeof(PetscScalar) + sizeof(PetscInt)) + 3*m*sizeof(PetscInt);
701531e53bdSHong Zhang     bcols = (PetscInt)(0.5*mem /(m*sizeof(PetscScalar)));
702531e53bdSHong Zhang     brows = 1000/bcols;
703531e53bdSHong Zhang     if (bcols > nis) bcols = nis;
704531e53bdSHong Zhang     if (brows == 0 || brows > m) brows = m;
705531e53bdSHong Zhang     c->brows = brows;
706531e53bdSHong Zhang     c->bcols = bcols;
707531e53bdSHong Zhang   }
708a8971b87SHong Zhang 
709a8971b87SHong Zhang   c->M       = mat->rmap->N/bs;         /* set the global rows and columns and local rows */
710a8971b87SHong Zhang   c->N       = mat->cmap->N/bs;
711a8971b87SHong Zhang   c->m       = mat->rmap->n/bs;
712a8971b87SHong Zhang   c->rstart  = mat->rmap->rstart/bs;
713a8971b87SHong Zhang   c->ncolors = nis;
71493dfae19SHong Zhang   PetscFunctionReturn(0);
71593dfae19SHong Zhang }
716bdaf1daeSBarry Smith 
717bdaf1daeSBarry Smith /*@C
718bdaf1daeSBarry Smith 
719bdaf1daeSBarry Smith     MatFDColoringSetValues - takes a matrix in compressed color format and enters the matrix into a PETSc Mat
720bdaf1daeSBarry Smith 
721bdaf1daeSBarry Smith    Collective on J
722bdaf1daeSBarry Smith 
723bdaf1daeSBarry Smith    Input Parameters:
724bdaf1daeSBarry Smith +    J - the sparse matrix
725bdaf1daeSBarry Smith .    coloring - created with MatFDColoringCreate() and a local coloring
726bdaf1daeSBarry Smith -    y - column major storage of matrix values with one color of values per column, the number of rows of y should match
727bdaf1daeSBarry Smith          the number of local rows of J and the number of columns is the number of colors.
728bdaf1daeSBarry Smith 
729bdaf1daeSBarry Smith    Level: intermediate
730bdaf1daeSBarry Smith 
7316a9b8d82SBarry Smith    Notes: the matrix in compressed color format may come from an Automatic Differentiation code
732bdaf1daeSBarry Smith 
733bdaf1daeSBarry Smith    The code will be slightly faster if MatFDColoringSetBlockSize(coloring,PETSC_DEFAULT,nc); is called immediately after creating the coloring
734bdaf1daeSBarry Smith 
735bdaf1daeSBarry Smith .seealso: MatFDColoringCreate(), ISColoring, ISColoringCreate(), ISColoringSetType(), IS_COLORING_LOCAL, MatFDColoringSetBlockSize()
736bdaf1daeSBarry Smith 
737bdaf1daeSBarry Smith @*/
738bdaf1daeSBarry Smith PetscErrorCode  MatFDColoringSetValues(Mat J,MatFDColoring coloring,const PetscScalar *y)
739bdaf1daeSBarry Smith {
740bdaf1daeSBarry Smith   PetscErrorCode    ierr;
741bdaf1daeSBarry Smith   MatEntry2         *Jentry2;
742bdaf1daeSBarry Smith   PetscInt          row,i,nrows_k,l,ncolors,nz = 0,bcols,nbcols = 0;
743bdaf1daeSBarry Smith   const PetscInt    *nrows;
744bdaf1daeSBarry Smith   PetscBool         eq;
745bdaf1daeSBarry Smith 
746bdaf1daeSBarry Smith   PetscFunctionBegin;
747bdaf1daeSBarry Smith   PetscValidHeaderSpecific(J,MAT_CLASSID,1);
748bdaf1daeSBarry Smith   PetscValidHeaderSpecific(coloring,MAT_FDCOLORING_CLASSID,2);
749bdaf1daeSBarry Smith   ierr = PetscObjectCompareId((PetscObject)J,coloring->matid,&eq);CHKERRQ(ierr);
750bdaf1daeSBarry Smith   if (!eq) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_WRONG,"Matrix used with MatFDColoringSetValues() must be that used with MatFDColoringCreate()");
751bdaf1daeSBarry Smith   Jentry2 = coloring->matentry2;
752bdaf1daeSBarry Smith   nrows   = coloring->nrows;
753bdaf1daeSBarry Smith   ncolors = coloring->ncolors;
754bdaf1daeSBarry Smith   bcols   = coloring->bcols;
755bdaf1daeSBarry Smith 
756bdaf1daeSBarry Smith   for (i=0; i<ncolors; i+=bcols) {
757bdaf1daeSBarry Smith     nrows_k = nrows[nbcols++];
758bdaf1daeSBarry Smith     for (l=0; l<nrows_k; l++) {
759bdaf1daeSBarry Smith       row                      = Jentry2[nz].row;   /* local row index */
760bdaf1daeSBarry Smith       *(Jentry2[nz++].valaddr) = y[row];
761bdaf1daeSBarry Smith     }
762bdaf1daeSBarry Smith     y += bcols*coloring->m;
763bdaf1daeSBarry Smith   }
764bdaf1daeSBarry Smith   ierr = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
765bdaf1daeSBarry Smith   ierr = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
766bdaf1daeSBarry Smith   PetscFunctionReturn(0);
767bdaf1daeSBarry Smith }
768