xref: /petsc/src/dm/impls/da/fdda.c (revision 12b4a53753ecbae42c98ba33876a303b79054923)
1af0996ceSBarry Smith #include <petsc/private/dmdaimpl.h> /*I      "petscdmda.h"     I*/
207475bc1SBarry Smith #include <petscmat.h>
3e432b41dSStefano Zampini #include <petscbt.h>
447c6ae99SBarry Smith 
5e727c939SJed Brown extern PetscErrorCode DMCreateColoring_DA_1d_MPIAIJ(DM, ISColoringType, ISColoring *);
6e727c939SJed Brown extern PetscErrorCode DMCreateColoring_DA_2d_MPIAIJ(DM, ISColoringType, ISColoring *);
7e727c939SJed Brown extern PetscErrorCode DMCreateColoring_DA_2d_5pt_MPIAIJ(DM, ISColoringType, ISColoring *);
8e727c939SJed Brown extern PetscErrorCode DMCreateColoring_DA_3d_MPIAIJ(DM, ISColoringType, ISColoring *);
947c6ae99SBarry Smith 
1047c6ae99SBarry Smith /*
1147c6ae99SBarry Smith    For ghost i that may be negative or greater than the upper bound this
1247c6ae99SBarry Smith   maps it into the 0:m-1 range using periodicity
1347c6ae99SBarry Smith */
1447c6ae99SBarry Smith #define SetInRange(i, m) ((i < 0) ? m + i : ((i >= m) ? i - m : i))
1547c6ae99SBarry Smith 
16d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDASetBlockFills_Private(const PetscInt *dfill, PetscInt w, PetscInt **rfill)
17d71ae5a4SJacob Faibussowitsch {
1847c6ae99SBarry Smith   PetscInt i, j, nz, *fill;
1947c6ae99SBarry Smith 
2047c6ae99SBarry Smith   PetscFunctionBegin;
213ba16761SJacob Faibussowitsch   if (!dfill) PetscFunctionReturn(PETSC_SUCCESS);
2247c6ae99SBarry Smith 
2347c6ae99SBarry Smith   /* count number nonzeros */
2447c6ae99SBarry Smith   nz = 0;
2547c6ae99SBarry Smith   for (i = 0; i < w; i++) {
2647c6ae99SBarry Smith     for (j = 0; j < w; j++) {
2747c6ae99SBarry Smith       if (dfill[w * i + j]) nz++;
2847c6ae99SBarry Smith     }
2947c6ae99SBarry Smith   }
309566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(nz + w + 1, &fill));
3147c6ae99SBarry Smith   /* construct modified CSR storage of nonzero structure */
32ce308e1dSBarry Smith   /*  fill[0 -- w] marks starts of each row of column indices (and end of last row)
33ce308e1dSBarry Smith    so fill[1] - fill[0] gives number of nonzeros in first row etc */
3447c6ae99SBarry Smith   nz = w + 1;
3547c6ae99SBarry Smith   for (i = 0; i < w; i++) {
3647c6ae99SBarry Smith     fill[i] = nz;
3747c6ae99SBarry Smith     for (j = 0; j < w; j++) {
3847c6ae99SBarry Smith       if (dfill[w * i + j]) {
3947c6ae99SBarry Smith         fill[nz] = j;
4047c6ae99SBarry Smith         nz++;
4147c6ae99SBarry Smith       }
4247c6ae99SBarry Smith     }
4347c6ae99SBarry Smith   }
4447c6ae99SBarry Smith   fill[w] = nz;
4547c6ae99SBarry Smith 
4647c6ae99SBarry Smith   *rfill = fill;
473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4847c6ae99SBarry Smith }
4947c6ae99SBarry Smith 
50d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDASetBlockFillsSparse_Private(const PetscInt *dfillsparse, PetscInt w, PetscInt **rfill)
51d71ae5a4SJacob Faibussowitsch {
52767d920cSKarl Rupp   PetscInt nz;
5309e28618SBarry Smith 
5409e28618SBarry Smith   PetscFunctionBegin;
553ba16761SJacob Faibussowitsch   if (!dfillsparse) PetscFunctionReturn(PETSC_SUCCESS);
5609e28618SBarry Smith 
5709e28618SBarry Smith   /* Determine number of non-zeros */
5809e28618SBarry Smith   nz = (dfillsparse[w] - w - 1);
5909e28618SBarry Smith 
6009e28618SBarry Smith   /* Allocate space for our copy of the given sparse matrix representation. */
619566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(nz + w + 1, rfill));
629566063dSJacob Faibussowitsch   PetscCall(PetscArraycpy(*rfill, dfillsparse, nz + w + 1));
633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6409e28618SBarry Smith }
6509e28618SBarry Smith 
66d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDASetBlockFills_Private2(DM_DA *dd)
67d71ae5a4SJacob Faibussowitsch {
6809e28618SBarry Smith   PetscInt i, k, cnt = 1;
6909e28618SBarry Smith 
7009e28618SBarry Smith   PetscFunctionBegin;
7109e28618SBarry Smith 
7209e28618SBarry Smith   /* ofillcount tracks the columns of ofill that have any nonzero in thems; the value in each location is the number of
7309e28618SBarry Smith    columns to the left with any nonzeros in them plus 1 */
749566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(dd->w, &dd->ofillcols));
7509e28618SBarry Smith   for (i = 0; i < dd->w; i++) {
7609e28618SBarry Smith     for (k = dd->ofill[i]; k < dd->ofill[i + 1]; k++) dd->ofillcols[dd->ofill[k]] = 1;
7709e28618SBarry Smith   }
7809e28618SBarry Smith   for (i = 0; i < dd->w; i++) {
79ad540459SPierre Jolivet     if (dd->ofillcols[i]) dd->ofillcols[i] = cnt++;
8009e28618SBarry Smith   }
813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8209e28618SBarry Smith }
8309e28618SBarry Smith 
8447c6ae99SBarry Smith /*@
85aa219208SBarry Smith   DMDASetBlockFills - Sets the fill pattern in each block for a multi-component problem
86dce8aebaSBarry Smith   of the matrix returned by `DMCreateMatrix()`.
8747c6ae99SBarry Smith 
8820f4b53cSBarry Smith   Logically Collective
8947c6ae99SBarry Smith 
90d8d19677SJose E. Roman   Input Parameters:
9147c6ae99SBarry Smith + da    - the distributed array
92*12b4a537SBarry Smith . dfill - the fill pattern in the diagonal block (may be `NULL`, means use dense block)
9347c6ae99SBarry Smith - ofill - the fill pattern in the off-diagonal blocks
9447c6ae99SBarry Smith 
9547c6ae99SBarry Smith   Level: developer
9647c6ae99SBarry Smith 
9795452b02SPatrick Sanan   Notes:
9895452b02SPatrick Sanan   This only makes sense when you are doing multicomponent problems but using the
99dce8aebaSBarry Smith   `MATMPIAIJ` matrix format
10047c6ae99SBarry Smith 
101*12b4a537SBarry Smith   The format for `dfill` and `ofill` is a 2 dimensional dof by dof matrix with 1 entries
10247c6ae99SBarry Smith   representing coupling and 0 entries for missing coupling. For example
103dce8aebaSBarry Smith .vb
104dce8aebaSBarry Smith             dfill[9] = {1, 0, 0,
105dce8aebaSBarry Smith                         1, 1, 0,
106dce8aebaSBarry Smith                         0, 1, 1}
107dce8aebaSBarry Smith .ve
10847c6ae99SBarry Smith   means that row 0 is coupled with only itself in the diagonal block, row 1 is coupled with
10947c6ae99SBarry Smith   itself and row 0 (in the diagonal block) and row 2 is coupled with itself and row 1 (in the
11047c6ae99SBarry Smith   diagonal block).
11147c6ae99SBarry Smith 
112dce8aebaSBarry Smith   `DMDASetGetMatrix()` allows you to provide general code for those more complicated nonzero patterns then
113*12b4a537SBarry Smith   can be represented in the `dfill`, `ofill` format
11447c6ae99SBarry Smith 
1151d27aa22SBarry Smith   Contributed by\:
1161d27aa22SBarry Smith   Glenn Hammond
11747c6ae99SBarry Smith 
118*12b4a537SBarry Smith .seealso: [](sec_struct), `DM`, `DMDA`, `DMCreateMatrix()`, `DMDASetGetMatrix()`, `DMSetMatrixPreallocateOnly()`, `DMDASetBlockFillsSparse()`
11947c6ae99SBarry Smith @*/
120d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDASetBlockFills(DM da, const PetscInt *dfill, const PetscInt *ofill)
121d71ae5a4SJacob Faibussowitsch {
12247c6ae99SBarry Smith   DM_DA *dd = (DM_DA *)da->data;
12347c6ae99SBarry Smith 
12447c6ae99SBarry Smith   PetscFunctionBegin;
12509e28618SBarry Smith   /* save the given dfill and ofill information */
1269566063dSJacob Faibussowitsch   PetscCall(DMDASetBlockFills_Private(dfill, dd->w, &dd->dfill));
1279566063dSJacob Faibussowitsch   PetscCall(DMDASetBlockFills_Private(ofill, dd->w, &dd->ofill));
128ae4f298aSBarry Smith 
12909e28618SBarry Smith   /* count nonzeros in ofill columns */
1309566063dSJacob Faibussowitsch   PetscCall(DMDASetBlockFills_Private2(dd));
1313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
132ae4f298aSBarry Smith }
13309e28618SBarry Smith 
13409e28618SBarry Smith /*@
13509e28618SBarry Smith   DMDASetBlockFillsSparse - Sets the fill pattern in each block for a multi-component problem
136dce8aebaSBarry Smith   of the matrix returned by `DMCreateMatrix()`, using sparse representations
13709e28618SBarry Smith   of fill patterns.
13809e28618SBarry Smith 
13920f4b53cSBarry Smith   Logically Collective
14009e28618SBarry Smith 
141d8d19677SJose E. Roman   Input Parameters:
14209e28618SBarry Smith + da          - the distributed array
14360225df5SJacob Faibussowitsch . dfillsparse - the sparse fill pattern in the diagonal block (may be `NULL`, means use dense block)
14460225df5SJacob Faibussowitsch - ofillsparse - the sparse fill pattern in the off-diagonal blocks
14509e28618SBarry Smith 
14609e28618SBarry Smith   Level: developer
14709e28618SBarry Smith 
148dce8aebaSBarry Smith   Notes:
149dce8aebaSBarry Smith   This only makes sense when you are doing multicomponent problems but using the
150dce8aebaSBarry Smith   `MATMPIAIJ` matrix format
15109e28618SBarry Smith 
15220f4b53cSBarry Smith   The format for `dfill` and `ofill` is a sparse representation of a
15309e28618SBarry Smith   dof-by-dof matrix with 1 entries representing coupling and 0 entries
15409e28618SBarry Smith   for missing coupling.  The sparse representation is a 1 dimensional
15509e28618SBarry Smith   array of length nz + dof + 1, where nz is the number of non-zeros in
15609e28618SBarry Smith   the matrix.  The first dof entries in the array give the
15709e28618SBarry Smith   starting array indices of each row's items in the rest of the array,
15860942847SBarry Smith   the dof+1st item contains the value nz + dof + 1 (i.e. the entire length of the array)
15909e28618SBarry Smith   and the remaining nz items give the column indices of each of
16009e28618SBarry Smith   the 1s within the logical 2D matrix.  Each row's items within
16109e28618SBarry Smith   the array are the column indices of the 1s within that row
16209e28618SBarry Smith   of the 2D matrix.  PETSc developers may recognize that this is the
163dce8aebaSBarry Smith   same format as that computed by the `DMDASetBlockFills_Private()`
16409e28618SBarry Smith   function from a dense 2D matrix representation.
16509e28618SBarry Smith 
166dce8aebaSBarry Smith   `DMDASetGetMatrix()` allows you to provide general code for those more complicated nonzero patterns then
16720f4b53cSBarry Smith   can be represented in the `dfill`, `ofill` format
16809e28618SBarry Smith 
1691d27aa22SBarry Smith   Contributed by\:
1701d27aa22SBarry Smith   Philip C. Roth
17109e28618SBarry Smith 
172*12b4a537SBarry Smith .seealso: [](sec_struct), `DM`, `DMDA`, `DMDASetBlockFills()`, `DMCreateMatrix()`, `DMDASetGetMatrix()`, `DMSetMatrixPreallocateOnly()`
17309e28618SBarry Smith @*/
174d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDASetBlockFillsSparse(DM da, const PetscInt *dfillsparse, const PetscInt *ofillsparse)
175d71ae5a4SJacob Faibussowitsch {
17609e28618SBarry Smith   DM_DA *dd = (DM_DA *)da->data;
17709e28618SBarry Smith 
17809e28618SBarry Smith   PetscFunctionBegin;
17909e28618SBarry Smith   /* save the given dfill and ofill information */
1809566063dSJacob Faibussowitsch   PetscCall(DMDASetBlockFillsSparse_Private(dfillsparse, dd->w, &dd->dfill));
1819566063dSJacob Faibussowitsch   PetscCall(DMDASetBlockFillsSparse_Private(ofillsparse, dd->w, &dd->ofill));
18209e28618SBarry Smith 
18309e28618SBarry Smith   /* count nonzeros in ofill columns */
1849566063dSJacob Faibussowitsch   PetscCall(DMDASetBlockFills_Private2(dd));
1853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
18647c6ae99SBarry Smith }
18747c6ae99SBarry Smith 
188d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA(DM da, ISColoringType ctype, ISColoring *coloring)
189d71ae5a4SJacob Faibussowitsch {
19047c6ae99SBarry Smith   PetscInt       dim, m, n, p, nc;
191bff4a2f0SMatthew G. Knepley   DMBoundaryType bx, by, bz;
19247c6ae99SBarry Smith   MPI_Comm       comm;
19347c6ae99SBarry Smith   PetscMPIInt    size;
19447c6ae99SBarry Smith   PetscBool      isBAIJ;
19547c6ae99SBarry Smith   DM_DA         *dd = (DM_DA *)da->data;
19647c6ae99SBarry Smith 
19747c6ae99SBarry Smith   PetscFunctionBegin;
19847c6ae99SBarry Smith   /*
19947c6ae99SBarry Smith                                   m
20047c6ae99SBarry Smith           ------------------------------------------------------
20147c6ae99SBarry Smith          |                                                     |
20247c6ae99SBarry Smith          |                                                     |
20347c6ae99SBarry Smith          |               ----------------------                |
20447c6ae99SBarry Smith          |               |                    |                |
20547c6ae99SBarry Smith       n  |           yn  |                    |                |
20647c6ae99SBarry Smith          |               |                    |                |
20747c6ae99SBarry Smith          |               .---------------------                |
20847c6ae99SBarry Smith          |             (xs,ys)     xn                          |
20947c6ae99SBarry Smith          |            .                                        |
21047c6ae99SBarry Smith          |         (gxs,gys)                                   |
21147c6ae99SBarry Smith          |                                                     |
21247c6ae99SBarry Smith           -----------------------------------------------------
21347c6ae99SBarry Smith   */
21447c6ae99SBarry Smith 
21547c6ae99SBarry Smith   /*
21647c6ae99SBarry Smith          nc - number of components per grid point
21747c6ae99SBarry Smith          col - number of colors needed in one direction for single component problem
21847c6ae99SBarry Smith 
21947c6ae99SBarry Smith   */
2209566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, NULL, NULL, NULL, &m, &n, &p, &nc, NULL, &bx, &by, &bz, NULL));
22147c6ae99SBarry Smith 
2229566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
2239566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
2245bdb020cSBarry Smith   if (ctype == IS_COLORING_LOCAL) {
22547c6ae99SBarry Smith     if (size == 1) {
22647c6ae99SBarry Smith       ctype = IS_COLORING_GLOBAL;
2272ddab442SBarry Smith     } else {
2282ddab442SBarry Smith       PetscCheck((dim == 1) || !((m == 1 && bx == DM_BOUNDARY_PERIODIC) || (n == 1 && by == DM_BOUNDARY_PERIODIC) || (p == 1 && bz == DM_BOUNDARY_PERIODIC)), PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "IS_COLORING_LOCAL cannot be used for periodic boundary condition having both ends of the domain on the same process");
22947c6ae99SBarry Smith     }
23047c6ae99SBarry Smith   }
23147c6ae99SBarry Smith 
232aa219208SBarry Smith   /* Tell the DMDA it has 1 degree of freedom per grid point so that the coloring for BAIJ
23347c6ae99SBarry Smith      matrices is for the blocks, not the individual matrix elements  */
2349566063dSJacob Faibussowitsch   PetscCall(PetscStrbeginswith(da->mattype, MATBAIJ, &isBAIJ));
2359566063dSJacob Faibussowitsch   if (!isBAIJ) PetscCall(PetscStrbeginswith(da->mattype, MATMPIBAIJ, &isBAIJ));
2369566063dSJacob Faibussowitsch   if (!isBAIJ) PetscCall(PetscStrbeginswith(da->mattype, MATSEQBAIJ, &isBAIJ));
23747c6ae99SBarry Smith   if (isBAIJ) {
23847c6ae99SBarry Smith     dd->w  = 1;
23947c6ae99SBarry Smith     dd->xs = dd->xs / nc;
24047c6ae99SBarry Smith     dd->xe = dd->xe / nc;
24147c6ae99SBarry Smith     dd->Xs = dd->Xs / nc;
24247c6ae99SBarry Smith     dd->Xe = dd->Xe / nc;
24347c6ae99SBarry Smith   }
24447c6ae99SBarry Smith 
24547c6ae99SBarry Smith   /*
246aa219208SBarry Smith      We do not provide a getcoloring function in the DMDA operations because
2479a1b256bSStefano Zampini    the basic DMDA does not know about matrices. We think of DMDA as being
24847c6ae99SBarry Smith    more low-level then matrices.
24947c6ae99SBarry Smith   */
2501baa6e33SBarry Smith   if (dim == 1) PetscCall(DMCreateColoring_DA_1d_MPIAIJ(da, ctype, coloring));
2511baa6e33SBarry Smith   else if (dim == 2) PetscCall(DMCreateColoring_DA_2d_MPIAIJ(da, ctype, coloring));
2521baa6e33SBarry Smith   else if (dim == 3) PetscCall(DMCreateColoring_DA_3d_MPIAIJ(da, ctype, coloring));
2531baa6e33SBarry Smith   else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "Not done for %" PetscInt_FMT " dimension, send us mail petsc-maint@mcs.anl.gov for code", dim);
25447c6ae99SBarry Smith   if (isBAIJ) {
25547c6ae99SBarry Smith     dd->w  = nc;
25647c6ae99SBarry Smith     dd->xs = dd->xs * nc;
25747c6ae99SBarry Smith     dd->xe = dd->xe * nc;
25847c6ae99SBarry Smith     dd->Xs = dd->Xs * nc;
25947c6ae99SBarry Smith     dd->Xe = dd->Xe * nc;
26047c6ae99SBarry Smith   }
2613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
26247c6ae99SBarry Smith }
26347c6ae99SBarry Smith 
264d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA_2d_MPIAIJ(DM da, ISColoringType ctype, ISColoring *coloring)
265d71ae5a4SJacob Faibussowitsch {
26647c6ae99SBarry Smith   PetscInt         xs, ys, nx, ny, i, j, ii, gxs, gys, gnx, gny, m, n, M, N, dim, s, k, nc, col;
267dec0b466SHong Zhang   PetscInt         ncolors = 0;
26847c6ae99SBarry Smith   MPI_Comm         comm;
269bff4a2f0SMatthew G. Knepley   DMBoundaryType   bx, by;
270aa219208SBarry Smith   DMDAStencilType  st;
27147c6ae99SBarry Smith   ISColoringValue *colors;
27247c6ae99SBarry Smith   DM_DA           *dd = (DM_DA *)da->data;
27347c6ae99SBarry Smith 
27447c6ae99SBarry Smith   PetscFunctionBegin;
27547c6ae99SBarry Smith   /*
27647c6ae99SBarry Smith          nc - number of components per grid point
27747c6ae99SBarry Smith          col - number of colors needed in one direction for single component problem
27847c6ae99SBarry Smith 
27947c6ae99SBarry Smith   */
2809566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, &M, &N, NULL, &nc, &s, &bx, &by, NULL, &st));
28147c6ae99SBarry Smith   col = 2 * s + 1;
2829566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL));
2839566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL));
2849566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
28547c6ae99SBarry Smith 
28647c6ae99SBarry Smith   /* special case as taught to us by Paul Hovland */
287aa219208SBarry Smith   if (st == DMDA_STENCIL_STAR && s == 1) {
2889566063dSJacob Faibussowitsch     PetscCall(DMCreateColoring_DA_2d_5pt_MPIAIJ(da, ctype, coloring));
28947c6ae99SBarry Smith   } else {
29047c6ae99SBarry Smith     if (ctype == IS_COLORING_GLOBAL) {
29147c6ae99SBarry Smith       if (!dd->localcoloring) {
2929566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1(nc * nx * ny, &colors));
29347c6ae99SBarry Smith         ii = 0;
29447c6ae99SBarry Smith         for (j = ys; j < ys + ny; j++) {
29547c6ae99SBarry Smith           for (i = xs; i < xs + nx; i++) {
296ad540459SPierre Jolivet             for (k = 0; k < nc; k++) colors[ii++] = k + nc * ((i % col) + col * (j % col));
29747c6ae99SBarry Smith           }
29847c6ae99SBarry Smith         }
29947c6ae99SBarry Smith         ncolors = nc + nc * (col - 1 + col * (col - 1));
3009566063dSJacob Faibussowitsch         PetscCall(ISColoringCreate(comm, ncolors, nc * nx * ny, colors, PETSC_OWN_POINTER, &dd->localcoloring));
30147c6ae99SBarry Smith       }
30247c6ae99SBarry Smith       *coloring = dd->localcoloring;
3035bdb020cSBarry Smith     } else if (ctype == IS_COLORING_LOCAL) {
30447c6ae99SBarry Smith       if (!dd->ghostedcoloring) {
3059566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1(nc * gnx * gny, &colors));
30647c6ae99SBarry Smith         ii = 0;
30747c6ae99SBarry Smith         for (j = gys; j < gys + gny; j++) {
30847c6ae99SBarry Smith           for (i = gxs; i < gxs + gnx; i++) {
30947c6ae99SBarry Smith             for (k = 0; k < nc; k++) {
31047c6ae99SBarry Smith               /* the complicated stuff is to handle periodic boundaries */
31147c6ae99SBarry Smith               colors[ii++] = k + nc * ((SetInRange(i, m) % col) + col * (SetInRange(j, n) % col));
31247c6ae99SBarry Smith             }
31347c6ae99SBarry Smith           }
31447c6ae99SBarry Smith         }
31547c6ae99SBarry Smith         ncolors = nc + nc * (col - 1 + col * (col - 1));
3169566063dSJacob Faibussowitsch         PetscCall(ISColoringCreate(comm, ncolors, nc * gnx * gny, colors, PETSC_OWN_POINTER, &dd->ghostedcoloring));
31747c6ae99SBarry Smith         /* PetscIntView(ncolors,(PetscInt*)colors,0); */
31847c6ae99SBarry Smith 
3199566063dSJacob Faibussowitsch         PetscCall(ISColoringSetType(dd->ghostedcoloring, IS_COLORING_LOCAL));
32047c6ae99SBarry Smith       }
32147c6ae99SBarry Smith       *coloring = dd->ghostedcoloring;
32298921bdaSJacob Faibussowitsch     } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_ARG_WRONG, "Unknown ISColoringType %d", (int)ctype);
32347c6ae99SBarry Smith   }
3249566063dSJacob Faibussowitsch   PetscCall(ISColoringReference(*coloring));
3253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
32647c6ae99SBarry Smith }
32747c6ae99SBarry Smith 
328d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA_3d_MPIAIJ(DM da, ISColoringType ctype, ISColoring *coloring)
329d71ae5a4SJacob Faibussowitsch {
33047c6ae99SBarry Smith   PetscInt         xs, ys, nx, ny, i, j, gxs, gys, gnx, gny, m, n, p, dim, s, k, nc, col, zs, gzs, ii, l, nz, gnz, M, N, P;
33147c6ae99SBarry Smith   PetscInt         ncolors;
33247c6ae99SBarry Smith   MPI_Comm         comm;
333bff4a2f0SMatthew G. Knepley   DMBoundaryType   bx, by, bz;
334aa219208SBarry Smith   DMDAStencilType  st;
33547c6ae99SBarry Smith   ISColoringValue *colors;
33647c6ae99SBarry Smith   DM_DA           *dd = (DM_DA *)da->data;
33747c6ae99SBarry Smith 
33847c6ae99SBarry Smith   PetscFunctionBegin;
33947c6ae99SBarry Smith   /*
34047c6ae99SBarry Smith          nc - number of components per grid point
34147c6ae99SBarry Smith          col - number of colors needed in one direction for single component problem
34247c6ae99SBarry Smith   */
3439566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &nc, &s, &bx, &by, &bz, &st));
34447c6ae99SBarry Smith   col = 2 * s + 1;
345494b1ccaSBarry Smith   PetscCheck(bx != DM_BOUNDARY_PERIODIC || (m % col) == 0, PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "For coloring efficiency ensure number of grid points in X is divisible\n\
346494b1ccaSBarry Smith                  by 2*stencil_width + 1\n");
347494b1ccaSBarry Smith   PetscCheck(by != DM_BOUNDARY_PERIODIC || (n % col) == 0, PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "For coloring efficiency ensure number of grid points in Y is divisible\n\
348494b1ccaSBarry Smith                  by 2*stencil_width + 1\n");
349494b1ccaSBarry Smith   PetscCheck(bz != DM_BOUNDARY_PERIODIC || (p % col) == 0, PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "For coloring efficiency ensure number of grid points in Z is divisible\n\
350494b1ccaSBarry Smith                  by 2*stencil_width + 1\n");
351494b1ccaSBarry Smith 
3529566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz));
3539566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz));
3549566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
35547c6ae99SBarry Smith 
35647c6ae99SBarry Smith   /* create the coloring */
35747c6ae99SBarry Smith   if (ctype == IS_COLORING_GLOBAL) {
35847c6ae99SBarry Smith     if (!dd->localcoloring) {
3599566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nc * nx * ny * nz, &colors));
36047c6ae99SBarry Smith       ii = 0;
36147c6ae99SBarry Smith       for (k = zs; k < zs + nz; k++) {
36247c6ae99SBarry Smith         for (j = ys; j < ys + ny; j++) {
36347c6ae99SBarry Smith           for (i = xs; i < xs + nx; i++) {
364ad540459SPierre Jolivet             for (l = 0; l < nc; l++) colors[ii++] = l + nc * ((i % col) + col * (j % col) + col * col * (k % col));
36547c6ae99SBarry Smith           }
36647c6ae99SBarry Smith         }
36747c6ae99SBarry Smith       }
36847c6ae99SBarry Smith       ncolors = nc + nc * (col - 1 + col * (col - 1) + col * col * (col - 1));
3699566063dSJacob Faibussowitsch       PetscCall(ISColoringCreate(comm, ncolors, nc * nx * ny * nz, colors, PETSC_OWN_POINTER, &dd->localcoloring));
37047c6ae99SBarry Smith     }
37147c6ae99SBarry Smith     *coloring = dd->localcoloring;
3725bdb020cSBarry Smith   } else if (ctype == IS_COLORING_LOCAL) {
37347c6ae99SBarry Smith     if (!dd->ghostedcoloring) {
3749566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nc * gnx * gny * gnz, &colors));
37547c6ae99SBarry Smith       ii = 0;
37647c6ae99SBarry Smith       for (k = gzs; k < gzs + gnz; k++) {
37747c6ae99SBarry Smith         for (j = gys; j < gys + gny; j++) {
37847c6ae99SBarry Smith           for (i = gxs; i < gxs + gnx; i++) {
37947c6ae99SBarry Smith             for (l = 0; l < nc; l++) {
38047c6ae99SBarry Smith               /* the complicated stuff is to handle periodic boundaries */
38147c6ae99SBarry Smith               colors[ii++] = l + nc * ((SetInRange(i, m) % col) + col * (SetInRange(j, n) % col) + col * col * (SetInRange(k, p) % col));
38247c6ae99SBarry Smith             }
38347c6ae99SBarry Smith           }
38447c6ae99SBarry Smith         }
38547c6ae99SBarry Smith       }
38647c6ae99SBarry Smith       ncolors = nc + nc * (col - 1 + col * (col - 1) + col * col * (col - 1));
3879566063dSJacob Faibussowitsch       PetscCall(ISColoringCreate(comm, ncolors, nc * gnx * gny * gnz, colors, PETSC_OWN_POINTER, &dd->ghostedcoloring));
3889566063dSJacob Faibussowitsch       PetscCall(ISColoringSetType(dd->ghostedcoloring, IS_COLORING_LOCAL));
38947c6ae99SBarry Smith     }
39047c6ae99SBarry Smith     *coloring = dd->ghostedcoloring;
39198921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_ARG_WRONG, "Unknown ISColoringType %d", (int)ctype);
3929566063dSJacob Faibussowitsch   PetscCall(ISColoringReference(*coloring));
3933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
39447c6ae99SBarry Smith }
39547c6ae99SBarry Smith 
396d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA_1d_MPIAIJ(DM da, ISColoringType ctype, ISColoring *coloring)
397d71ae5a4SJacob Faibussowitsch {
39847c6ae99SBarry Smith   PetscInt         xs, nx, i, i1, gxs, gnx, l, m, M, dim, s, nc, col;
39947c6ae99SBarry Smith   PetscInt         ncolors;
40047c6ae99SBarry Smith   MPI_Comm         comm;
401bff4a2f0SMatthew G. Knepley   DMBoundaryType   bx;
40247c6ae99SBarry Smith   ISColoringValue *colors;
40347c6ae99SBarry Smith   DM_DA           *dd = (DM_DA *)da->data;
40447c6ae99SBarry Smith 
40547c6ae99SBarry Smith   PetscFunctionBegin;
40647c6ae99SBarry Smith   /*
40747c6ae99SBarry Smith          nc - number of components per grid point
40847c6ae99SBarry Smith          col - number of colors needed in one direction for single component problem
40947c6ae99SBarry Smith   */
4109566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, NULL, NULL, &M, NULL, NULL, &nc, &s, &bx, NULL, NULL, NULL));
41147c6ae99SBarry Smith   col = 2 * s + 1;
4129566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &nx, NULL, NULL));
4139566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, NULL, NULL, &gnx, NULL, NULL));
4149566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
41547c6ae99SBarry Smith 
41647c6ae99SBarry Smith   /* create the coloring */
41747c6ae99SBarry Smith   if (ctype == IS_COLORING_GLOBAL) {
41847c6ae99SBarry Smith     if (!dd->localcoloring) {
4199566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nc * nx, &colors));
420ae4f298aSBarry Smith       if (dd->ofillcols) {
421ae4f298aSBarry Smith         PetscInt tc = 0;
422ae4f298aSBarry Smith         for (i = 0; i < nc; i++) tc += (PetscInt)(dd->ofillcols[i] > 0);
423ae4f298aSBarry Smith         i1 = 0;
424ae4f298aSBarry Smith         for (i = xs; i < xs + nx; i++) {
425ae4f298aSBarry Smith           for (l = 0; l < nc; l++) {
426ae4f298aSBarry Smith             if (dd->ofillcols[l] && (i % col)) {
427ae4f298aSBarry Smith               colors[i1++] = nc - 1 + tc * ((i % col) - 1) + dd->ofillcols[l];
428ae4f298aSBarry Smith             } else {
429ae4f298aSBarry Smith               colors[i1++] = l;
430ae4f298aSBarry Smith             }
431ae4f298aSBarry Smith           }
432ae4f298aSBarry Smith         }
433ae4f298aSBarry Smith         ncolors = nc + 2 * s * tc;
434ae4f298aSBarry Smith       } else {
43547c6ae99SBarry Smith         i1 = 0;
43647c6ae99SBarry Smith         for (i = xs; i < xs + nx; i++) {
437ad540459SPierre Jolivet           for (l = 0; l < nc; l++) colors[i1++] = l + nc * (i % col);
43847c6ae99SBarry Smith         }
43947c6ae99SBarry Smith         ncolors = nc + nc * (col - 1);
440ae4f298aSBarry Smith       }
4419566063dSJacob Faibussowitsch       PetscCall(ISColoringCreate(comm, ncolors, nc * nx, colors, PETSC_OWN_POINTER, &dd->localcoloring));
44247c6ae99SBarry Smith     }
44347c6ae99SBarry Smith     *coloring = dd->localcoloring;
4445bdb020cSBarry Smith   } else if (ctype == IS_COLORING_LOCAL) {
44547c6ae99SBarry Smith     if (!dd->ghostedcoloring) {
4469566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nc * gnx, &colors));
44747c6ae99SBarry Smith       i1 = 0;
44847c6ae99SBarry Smith       for (i = gxs; i < gxs + gnx; i++) {
44947c6ae99SBarry Smith         for (l = 0; l < nc; l++) {
45047c6ae99SBarry Smith           /* the complicated stuff is to handle periodic boundaries */
45147c6ae99SBarry Smith           colors[i1++] = l + nc * (SetInRange(i, m) % col);
45247c6ae99SBarry Smith         }
45347c6ae99SBarry Smith       }
45447c6ae99SBarry Smith       ncolors = nc + nc * (col - 1);
4559566063dSJacob Faibussowitsch       PetscCall(ISColoringCreate(comm, ncolors, nc * gnx, colors, PETSC_OWN_POINTER, &dd->ghostedcoloring));
4569566063dSJacob Faibussowitsch       PetscCall(ISColoringSetType(dd->ghostedcoloring, IS_COLORING_LOCAL));
45747c6ae99SBarry Smith     }
45847c6ae99SBarry Smith     *coloring = dd->ghostedcoloring;
45998921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_ARG_WRONG, "Unknown ISColoringType %d", (int)ctype);
4609566063dSJacob Faibussowitsch   PetscCall(ISColoringReference(*coloring));
4613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
46247c6ae99SBarry Smith }
46347c6ae99SBarry Smith 
464d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA_2d_5pt_MPIAIJ(DM da, ISColoringType ctype, ISColoring *coloring)
465d71ae5a4SJacob Faibussowitsch {
46647c6ae99SBarry Smith   PetscInt         xs, ys, nx, ny, i, j, ii, gxs, gys, gnx, gny, m, n, dim, s, k, nc;
46747c6ae99SBarry Smith   PetscInt         ncolors;
46847c6ae99SBarry Smith   MPI_Comm         comm;
469bff4a2f0SMatthew G. Knepley   DMBoundaryType   bx, by;
47047c6ae99SBarry Smith   ISColoringValue *colors;
47147c6ae99SBarry Smith   DM_DA           *dd = (DM_DA *)da->data;
47247c6ae99SBarry Smith 
47347c6ae99SBarry Smith   PetscFunctionBegin;
47447c6ae99SBarry Smith   /*
47547c6ae99SBarry Smith          nc - number of components per grid point
47647c6ae99SBarry Smith          col - number of colors needed in one direction for single component problem
47747c6ae99SBarry Smith   */
4789566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, NULL, NULL, NULL, &nc, &s, &bx, &by, NULL, NULL));
4799566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL));
4809566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL));
4819566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
48247c6ae99SBarry Smith   /* create the coloring */
48347c6ae99SBarry Smith   if (ctype == IS_COLORING_GLOBAL) {
48447c6ae99SBarry Smith     if (!dd->localcoloring) {
4859566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nc * nx * ny, &colors));
48647c6ae99SBarry Smith       ii = 0;
48747c6ae99SBarry Smith       for (j = ys; j < ys + ny; j++) {
48847c6ae99SBarry Smith         for (i = xs; i < xs + nx; i++) {
489ad540459SPierre Jolivet           for (k = 0; k < nc; k++) colors[ii++] = k + nc * ((3 * j + i) % 5);
49047c6ae99SBarry Smith         }
49147c6ae99SBarry Smith       }
49247c6ae99SBarry Smith       ncolors = 5 * nc;
4939566063dSJacob Faibussowitsch       PetscCall(ISColoringCreate(comm, ncolors, nc * nx * ny, colors, PETSC_OWN_POINTER, &dd->localcoloring));
49447c6ae99SBarry Smith     }
49547c6ae99SBarry Smith     *coloring = dd->localcoloring;
4965bdb020cSBarry Smith   } else if (ctype == IS_COLORING_LOCAL) {
49747c6ae99SBarry Smith     if (!dd->ghostedcoloring) {
4989566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nc * gnx * gny, &colors));
49947c6ae99SBarry Smith       ii = 0;
50047c6ae99SBarry Smith       for (j = gys; j < gys + gny; j++) {
50147c6ae99SBarry Smith         for (i = gxs; i < gxs + gnx; i++) {
502ad540459SPierre Jolivet           for (k = 0; k < nc; k++) colors[ii++] = k + nc * ((3 * SetInRange(j, n) + SetInRange(i, m)) % 5);
50347c6ae99SBarry Smith         }
50447c6ae99SBarry Smith       }
50547c6ae99SBarry Smith       ncolors = 5 * nc;
5069566063dSJacob Faibussowitsch       PetscCall(ISColoringCreate(comm, ncolors, nc * gnx * gny, colors, PETSC_OWN_POINTER, &dd->ghostedcoloring));
5079566063dSJacob Faibussowitsch       PetscCall(ISColoringSetType(dd->ghostedcoloring, IS_COLORING_LOCAL));
50847c6ae99SBarry Smith     }
50947c6ae99SBarry Smith     *coloring = dd->ghostedcoloring;
51098921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_ARG_WRONG, "Unknown ISColoringType %d", (int)ctype);
5113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
51247c6ae99SBarry Smith }
51347c6ae99SBarry Smith 
51447c6ae99SBarry Smith /* =========================================================================== */
515e432b41dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_1d_MPIAIJ(DM, Mat);
516ce308e1dSBarry Smith extern PetscErrorCode DMCreateMatrix_DA_1d_MPIAIJ_Fill(DM, Mat);
517e432b41dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_1d_SeqAIJ_NoPreallocation(DM, Mat);
518e432b41dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_2d_MPIAIJ(DM, Mat);
519950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_2d_MPIAIJ_Fill(DM, Mat);
520e432b41dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_3d_MPIAIJ(DM, Mat);
521950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_3d_MPIAIJ_Fill(DM, Mat);
522950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_2d_MPIBAIJ(DM, Mat);
523950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_3d_MPIBAIJ(DM, Mat);
524950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_2d_MPISBAIJ(DM, Mat);
525950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_3d_MPISBAIJ(DM, Mat);
526d4002b98SHong Zhang extern PetscErrorCode DMCreateMatrix_DA_2d_MPISELL(DM, Mat);
527d4002b98SHong Zhang extern PetscErrorCode DMCreateMatrix_DA_3d_MPISELL(DM, Mat);
528e584696dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_IS(DM, Mat);
52947c6ae99SBarry Smith 
530a4e35b19SJacob Faibussowitsch static PetscErrorCode MatView_MPI_DA(Mat A, PetscViewer viewer)
531d71ae5a4SJacob Faibussowitsch {
5329a42bb27SBarry Smith   DM                da;
53347c6ae99SBarry Smith   const char       *prefix;
5342d1451d4SHong Zhang   Mat               AA, Anatural;
53547c6ae99SBarry Smith   AO                ao;
53647c6ae99SBarry Smith   PetscInt          rstart, rend, *petsc, i;
53747c6ae99SBarry Smith   IS                is;
53847c6ae99SBarry Smith   MPI_Comm          comm;
53974388724SJed Brown   PetscViewerFormat format;
5402d1451d4SHong Zhang   PetscBool         flag;
54147c6ae99SBarry Smith 
54247c6ae99SBarry Smith   PetscFunctionBegin;
54374388724SJed Brown   /* Check whether we are just printing info, in which case MatView() already viewed everything we wanted to view */
5449566063dSJacob Faibussowitsch   PetscCall(PetscViewerGetFormat(viewer, &format));
5453ba16761SJacob Faibussowitsch   if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) PetscFunctionReturn(PETSC_SUCCESS);
54674388724SJed Brown 
5479566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)A, &comm));
5489566063dSJacob Faibussowitsch   PetscCall(MatGetDM(A, &da));
5497a8be351SBarry Smith   PetscCheck(da, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Matrix not generated from a DMDA");
55047c6ae99SBarry Smith 
5519566063dSJacob Faibussowitsch   PetscCall(DMDAGetAO(da, &ao));
5529566063dSJacob Faibussowitsch   PetscCall(MatGetOwnershipRange(A, &rstart, &rend));
5539566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(rend - rstart, &petsc));
55447c6ae99SBarry Smith   for (i = rstart; i < rend; i++) petsc[i - rstart] = i;
5559566063dSJacob Faibussowitsch   PetscCall(AOApplicationToPetsc(ao, rend - rstart, petsc));
5569566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(comm, rend - rstart, petsc, PETSC_OWN_POINTER, &is));
55747c6ae99SBarry Smith 
55847c6ae99SBarry Smith   /* call viewer on natural ordering */
5592d1451d4SHong Zhang   PetscCall(PetscObjectBaseTypeCompare((PetscObject)A, MATMPISELL, &flag));
5602d1451d4SHong Zhang   if (flag) {
5612d1451d4SHong Zhang     PetscCall(MatConvert(A, MATAIJ, MAT_INITIAL_MATRIX, &AA));
5622d1451d4SHong Zhang     A = AA;
5632d1451d4SHong Zhang   }
5649566063dSJacob Faibussowitsch   PetscCall(MatCreateSubMatrix(A, is, is, MAT_INITIAL_MATRIX, &Anatural));
5659566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&is));
5669566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)A, &prefix));
5679566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)Anatural, prefix));
5689566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)Anatural, ((PetscObject)A)->name));
569f0ed2f47SStefano Zampini   ((PetscObject)Anatural)->donotPetscObjectPrintClassNamePrefixType = PETSC_TRUE;
5709566063dSJacob Faibussowitsch   PetscCall(MatView(Anatural, viewer));
571f0ed2f47SStefano Zampini   ((PetscObject)Anatural)->donotPetscObjectPrintClassNamePrefixType = PETSC_FALSE;
5729566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&Anatural));
5732d1451d4SHong Zhang   if (flag) PetscCall(MatDestroy(&AA));
5743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
57547c6ae99SBarry Smith }
57647c6ae99SBarry Smith 
577a4e35b19SJacob Faibussowitsch static PetscErrorCode MatLoad_MPI_DA(Mat A, PetscViewer viewer)
578d71ae5a4SJacob Faibussowitsch {
5799a42bb27SBarry Smith   DM       da;
58047c6ae99SBarry Smith   Mat      Anatural, Aapp;
58147c6ae99SBarry Smith   AO       ao;
582539c167fSBarry Smith   PetscInt rstart, rend, *app, i, m, n, M, N;
58347c6ae99SBarry Smith   IS       is;
58447c6ae99SBarry Smith   MPI_Comm comm;
58547c6ae99SBarry Smith 
58647c6ae99SBarry Smith   PetscFunctionBegin;
5879566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)A, &comm));
5889566063dSJacob Faibussowitsch   PetscCall(MatGetDM(A, &da));
5897a8be351SBarry Smith   PetscCheck(da, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Matrix not generated from a DMDA");
59047c6ae99SBarry Smith 
59147c6ae99SBarry Smith   /* Load the matrix in natural ordering */
5929566063dSJacob Faibussowitsch   PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &Anatural));
5939566063dSJacob Faibussowitsch   PetscCall(MatSetType(Anatural, ((PetscObject)A)->type_name));
5949566063dSJacob Faibussowitsch   PetscCall(MatGetSize(A, &M, &N));
5959566063dSJacob Faibussowitsch   PetscCall(MatGetLocalSize(A, &m, &n));
5969566063dSJacob Faibussowitsch   PetscCall(MatSetSizes(Anatural, m, n, M, N));
5979566063dSJacob Faibussowitsch   PetscCall(MatLoad(Anatural, viewer));
59847c6ae99SBarry Smith 
59947c6ae99SBarry Smith   /* Map natural ordering to application ordering and create IS */
6009566063dSJacob Faibussowitsch   PetscCall(DMDAGetAO(da, &ao));
6019566063dSJacob Faibussowitsch   PetscCall(MatGetOwnershipRange(Anatural, &rstart, &rend));
6029566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(rend - rstart, &app));
60347c6ae99SBarry Smith   for (i = rstart; i < rend; i++) app[i - rstart] = i;
6049566063dSJacob Faibussowitsch   PetscCall(AOPetscToApplication(ao, rend - rstart, app));
6059566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(comm, rend - rstart, app, PETSC_OWN_POINTER, &is));
60647c6ae99SBarry Smith 
60747c6ae99SBarry Smith   /* Do permutation and replace header */
6089566063dSJacob Faibussowitsch   PetscCall(MatCreateSubMatrix(Anatural, is, is, MAT_INITIAL_MATRIX, &Aapp));
6099566063dSJacob Faibussowitsch   PetscCall(MatHeaderReplace(A, &Aapp));
6109566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&is));
6119566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&Anatural));
6123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
61347c6ae99SBarry Smith }
61447c6ae99SBarry Smith 
615d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA(DM da, Mat *J)
616d71ae5a4SJacob Faibussowitsch {
61747c6ae99SBarry Smith   PetscInt    dim, dof, nx, ny, nz, dims[3], starts[3], M, N, P;
61847c6ae99SBarry Smith   Mat         A;
61947c6ae99SBarry Smith   MPI_Comm    comm;
62019fd82e9SBarry Smith   MatType     Atype;
621b412c318SBarry Smith   MatType     mtype;
62247c6ae99SBarry Smith   PetscMPIInt size;
62347c6ae99SBarry Smith   DM_DA      *dd    = (DM_DA *)da->data;
624ade515a3SBarry Smith   void (*aij)(void) = NULL, (*baij)(void) = NULL, (*sbaij)(void) = NULL, (*sell)(void) = NULL, (*is)(void) = NULL;
62547c6ae99SBarry Smith 
62647c6ae99SBarry Smith   PetscFunctionBegin;
6279566063dSJacob Faibussowitsch   PetscCall(MatInitializePackage());
628b412c318SBarry Smith   mtype = da->mattype;
62947c6ae99SBarry Smith 
63047c6ae99SBarry Smith   /*
63147c6ae99SBarry Smith                                   m
63247c6ae99SBarry Smith           ------------------------------------------------------
63347c6ae99SBarry Smith          |                                                     |
63447c6ae99SBarry Smith          |                                                     |
63547c6ae99SBarry Smith          |               ----------------------                |
63647c6ae99SBarry Smith          |               |                    |                |
63747c6ae99SBarry Smith       n  |           ny  |                    |                |
63847c6ae99SBarry Smith          |               |                    |                |
63947c6ae99SBarry Smith          |               .---------------------                |
64047c6ae99SBarry Smith          |             (xs,ys)     nx                          |
64147c6ae99SBarry Smith          |            .                                        |
64247c6ae99SBarry Smith          |         (gxs,gys)                                   |
64347c6ae99SBarry Smith          |                                                     |
64447c6ae99SBarry Smith           -----------------------------------------------------
64547c6ae99SBarry Smith   */
64647c6ae99SBarry Smith 
64747c6ae99SBarry Smith   /*
64847c6ae99SBarry Smith          nc - number of components per grid point
64947c6ae99SBarry Smith          col - number of colors needed in one direction for single component problem
65047c6ae99SBarry Smith   */
651e30e807fSPeter Brune   M   = dd->M;
652e30e807fSPeter Brune   N   = dd->N;
653e30e807fSPeter Brune   P   = dd->P;
654c73cfb54SMatthew G. Knepley   dim = da->dim;
655e30e807fSPeter Brune   dof = dd->w;
6569566063dSJacob Faibussowitsch   /* PetscCall(DMDAGetInfo(da,&dim,&M,&N,&P,NULL,NULL,NULL,&dof,NULL,NULL,NULL,NULL,NULL)); */
6579566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, NULL, NULL, NULL, &nx, &ny, &nz));
6589566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
6599566063dSJacob Faibussowitsch   PetscCall(MatCreate(comm, &A));
6609566063dSJacob Faibussowitsch   PetscCall(MatSetSizes(A, dof * nx * ny * nz, dof * nx * ny * nz, dof * M * N * P, dof * M * N * P));
6619566063dSJacob Faibussowitsch   PetscCall(MatSetType(A, mtype));
6629566063dSJacob Faibussowitsch   PetscCall(MatSetFromOptions(A));
66374427ab1SRichard Tran Mills   if (dof * nx * ny * nz < da->bind_below) {
6649566063dSJacob Faibussowitsch     PetscCall(MatSetBindingPropagates(A, PETSC_TRUE));
6659566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(A, PETSC_TRUE));
66674427ab1SRichard Tran Mills   }
6679566063dSJacob Faibussowitsch   PetscCall(MatSetDM(A, da));
6681baa6e33SBarry Smith   if (da->structure_only) PetscCall(MatSetOption(A, MAT_STRUCTURE_ONLY, PETSC_TRUE));
6699566063dSJacob Faibussowitsch   PetscCall(MatGetType(A, &Atype));
67047c6ae99SBarry Smith   /*
671aa219208SBarry Smith      We do not provide a getmatrix function in the DMDA operations because
672aa219208SBarry Smith    the basic DMDA does not know about matrices. We think of DMDA as being more
67347c6ae99SBarry Smith    more low-level than matrices. This is kind of cheating but, cause sometimes
674aa219208SBarry Smith    we think of DMDA has higher level than matrices.
67547c6ae99SBarry Smith 
67647c6ae99SBarry Smith      We could switch based on Atype (or mtype), but we do not since the
677844bd0d7SStefano Zampini    specialized setting routines depend only on the particular preallocation
67847c6ae99SBarry Smith    details of the matrix, not the type itself.
67947c6ae99SBarry Smith   */
680d7dabc60SJed Brown   if (!da->prealloc_skip) { // Flag is likely set when user intends to use MatSetPreallocationCOO()
6819566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatMPIAIJSetPreallocation_C", &aij));
68248a46eb9SPierre Jolivet     if (!aij) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatSeqAIJSetPreallocation_C", &aij));
68347c6ae99SBarry Smith     if (!aij) {
6849566063dSJacob Faibussowitsch       PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatMPIBAIJSetPreallocation_C", &baij));
68548a46eb9SPierre Jolivet       if (!baij) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatSeqBAIJSetPreallocation_C", &baij));
68647c6ae99SBarry Smith       if (!baij) {
6879566063dSJacob Faibussowitsch         PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatMPISBAIJSetPreallocation_C", &sbaij));
68848a46eb9SPierre Jolivet         if (!sbaij) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatSeqSBAIJSetPreallocation_C", &sbaij));
6895e26d47bSHong Zhang         if (!sbaij) {
6909566063dSJacob Faibussowitsch           PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatMPISELLSetPreallocation_C", &sell));
69148a46eb9SPierre Jolivet           if (!sell) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatSeqSELLSetPreallocation_C", &sell));
6925e26d47bSHong Zhang         }
69348a46eb9SPierre Jolivet         if (!sell) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatISSetPreallocation_C", &is));
69447c6ae99SBarry Smith       }
69547c6ae99SBarry Smith     }
696d7dabc60SJed Brown   }
69747c6ae99SBarry Smith   if (aij) {
69847c6ae99SBarry Smith     if (dim == 1) {
699ce308e1dSBarry Smith       if (dd->ofill) {
7009566063dSJacob Faibussowitsch         PetscCall(DMCreateMatrix_DA_1d_MPIAIJ_Fill(da, A));
701ce308e1dSBarry Smith       } else {
70219b08ed1SBarry Smith         DMBoundaryType bx;
70319b08ed1SBarry Smith         PetscMPIInt    size;
7049566063dSJacob Faibussowitsch         PetscCall(DMDAGetInfo(da, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &bx, NULL, NULL, NULL));
7059566063dSJacob Faibussowitsch         PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)da), &size));
70619b08ed1SBarry Smith         if (size == 1 && bx == DM_BOUNDARY_NONE) {
7079566063dSJacob Faibussowitsch           PetscCall(DMCreateMatrix_DA_1d_SeqAIJ_NoPreallocation(da, A));
70819b08ed1SBarry Smith         } else {
7099566063dSJacob Faibussowitsch           PetscCall(DMCreateMatrix_DA_1d_MPIAIJ(da, A));
710ce308e1dSBarry Smith         }
71119b08ed1SBarry Smith       }
71247c6ae99SBarry Smith     } else if (dim == 2) {
71347c6ae99SBarry Smith       if (dd->ofill) {
7149566063dSJacob Faibussowitsch         PetscCall(DMCreateMatrix_DA_2d_MPIAIJ_Fill(da, A));
71547c6ae99SBarry Smith       } else {
7169566063dSJacob Faibussowitsch         PetscCall(DMCreateMatrix_DA_2d_MPIAIJ(da, A));
71747c6ae99SBarry Smith       }
71847c6ae99SBarry Smith     } else if (dim == 3) {
71947c6ae99SBarry Smith       if (dd->ofill) {
7209566063dSJacob Faibussowitsch         PetscCall(DMCreateMatrix_DA_3d_MPIAIJ_Fill(da, A));
72147c6ae99SBarry Smith       } else {
7229566063dSJacob Faibussowitsch         PetscCall(DMCreateMatrix_DA_3d_MPIAIJ(da, A));
72347c6ae99SBarry Smith       }
72447c6ae99SBarry Smith     }
72547c6ae99SBarry Smith   } else if (baij) {
72647c6ae99SBarry Smith     if (dim == 2) {
7279566063dSJacob Faibussowitsch       PetscCall(DMCreateMatrix_DA_2d_MPIBAIJ(da, A));
72847c6ae99SBarry Smith     } else if (dim == 3) {
7299566063dSJacob Faibussowitsch       PetscCall(DMCreateMatrix_DA_3d_MPIBAIJ(da, A));
73063a3b9bcSJacob Faibussowitsch     } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "Not implemented for %" PetscInt_FMT " dimension and Matrix Type: %s in %" PetscInt_FMT " dimension! Send mail to petsc-maint@mcs.anl.gov for code", dim, Atype, dim);
73147c6ae99SBarry Smith   } else if (sbaij) {
73247c6ae99SBarry Smith     if (dim == 2) {
7339566063dSJacob Faibussowitsch       PetscCall(DMCreateMatrix_DA_2d_MPISBAIJ(da, A));
73447c6ae99SBarry Smith     } else if (dim == 3) {
7359566063dSJacob Faibussowitsch       PetscCall(DMCreateMatrix_DA_3d_MPISBAIJ(da, A));
73663a3b9bcSJacob Faibussowitsch     } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "Not implemented for %" PetscInt_FMT " dimension and Matrix Type: %s in %" PetscInt_FMT " dimension! Send mail to petsc-maint@mcs.anl.gov for code", dim, Atype, dim);
737d4002b98SHong Zhang   } else if (sell) {
7385e26d47bSHong Zhang     if (dim == 2) {
7399566063dSJacob Faibussowitsch       PetscCall(DMCreateMatrix_DA_2d_MPISELL(da, A));
740711261dbSHong Zhang     } else if (dim == 3) {
7419566063dSJacob Faibussowitsch       PetscCall(DMCreateMatrix_DA_3d_MPISELL(da, A));
74263a3b9bcSJacob Faibussowitsch     } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "Not implemented for %" PetscInt_FMT " dimension and Matrix Type: %s in %" PetscInt_FMT " dimension! Send mail to petsc-maint@mcs.anl.gov for code", dim, Atype, dim);
743e584696dSStefano Zampini   } else if (is) {
7449566063dSJacob Faibussowitsch     PetscCall(DMCreateMatrix_DA_IS(da, A));
745d7dabc60SJed Brown   } else { // unknown type or da->prealloc_skip so structural information may be needed, but no prealloc
74645b6f7e9SBarry Smith     ISLocalToGlobalMapping ltog;
747e584696dSStefano Zampini 
7489566063dSJacob Faibussowitsch     PetscCall(MatSetBlockSize(A, dof));
7499566063dSJacob Faibussowitsch     PetscCall(MatSetUp(A));
7509566063dSJacob Faibussowitsch     PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
7519566063dSJacob Faibussowitsch     PetscCall(MatSetLocalToGlobalMapping(A, ltog, ltog));
75247c6ae99SBarry Smith   }
7539566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &starts[0], &starts[1], &starts[2], &dims[0], &dims[1], &dims[2]));
7549566063dSJacob Faibussowitsch   PetscCall(MatSetStencil(A, dim, dims, starts, dof));
7559566063dSJacob Faibussowitsch   PetscCall(MatSetDM(A, da));
7569566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
75747c6ae99SBarry Smith   if (size > 1) {
75847c6ae99SBarry Smith     /* change viewer to display matrix in natural ordering */
7599566063dSJacob Faibussowitsch     PetscCall(MatSetOperation(A, MATOP_VIEW, (void (*)(void))MatView_MPI_DA));
7609566063dSJacob Faibussowitsch     PetscCall(MatSetOperation(A, MATOP_LOAD, (void (*)(void))MatLoad_MPI_DA));
76147c6ae99SBarry Smith   }
76247c6ae99SBarry Smith   *J = A;
7633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
76447c6ae99SBarry Smith }
76547c6ae99SBarry Smith 
766844bd0d7SStefano Zampini PETSC_EXTERN PetscErrorCode MatISSetPreallocation_IS(Mat, PetscInt, const PetscInt[], PetscInt, const PetscInt[]);
767844bd0d7SStefano Zampini 
768d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_IS(DM dm, Mat J)
769d71ae5a4SJacob Faibussowitsch {
770e584696dSStefano Zampini   DM_DA                 *da = (DM_DA *)dm->data;
771e432b41dSStefano Zampini   Mat                    lJ, P;
772e584696dSStefano Zampini   ISLocalToGlobalMapping ltog;
773e432b41dSStefano Zampini   IS                     is;
774e432b41dSStefano Zampini   PetscBT                bt;
77505339c03SStefano Zampini   const PetscInt        *e_loc, *idx;
776e432b41dSStefano Zampini   PetscInt               i, nel, nen, nv, dof, *gidx, n, N;
777e584696dSStefano Zampini 
778e584696dSStefano Zampini   /* The l2g map of DMDA has all ghosted nodes, and e_loc is a subset of all the local nodes (including the ghosted)
779e432b41dSStefano Zampini      We need to filter out the local indices that are not represented through the DMDAGetElements decomposition */
780e584696dSStefano Zampini   PetscFunctionBegin;
781e584696dSStefano Zampini   dof = da->w;
7829566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, dof));
7839566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(dm, &ltog));
784e432b41dSStefano Zampini 
785e432b41dSStefano Zampini   /* flag local elements indices in local DMDA numbering */
7869566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingGetSize(ltog, &nv));
7879566063dSJacob Faibussowitsch   PetscCall(PetscBTCreate(nv / dof, &bt));
7889566063dSJacob Faibussowitsch   PetscCall(DMDAGetElements(dm, &nel, &nen, &e_loc)); /* this will throw an error if the stencil type is not DMDA_STENCIL_BOX */
7899566063dSJacob Faibussowitsch   for (i = 0; i < nel * nen; i++) PetscCall(PetscBTSet(bt, e_loc[i]));
790e432b41dSStefano Zampini 
791e432b41dSStefano Zampini   /* filter out (set to -1) the global indices not used by the local elements */
7929566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(nv / dof, &gidx));
7939566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingGetBlockIndices(ltog, &idx));
7949566063dSJacob Faibussowitsch   PetscCall(PetscArraycpy(gidx, idx, nv / dof));
7959566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingRestoreBlockIndices(ltog, &idx));
7969371c9d4SSatish Balay   for (i = 0; i < nv / dof; i++)
7979371c9d4SSatish Balay     if (!PetscBTLookup(bt, i)) gidx[i] = -1;
7989566063dSJacob Faibussowitsch   PetscCall(PetscBTDestroy(&bt));
7999566063dSJacob Faibussowitsch   PetscCall(ISCreateBlock(PetscObjectComm((PetscObject)dm), dof, nv / dof, gidx, PETSC_OWN_POINTER, &is));
8009566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingCreateIS(is, &ltog));
8019566063dSJacob Faibussowitsch   PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
8029566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingDestroy(&ltog));
8039566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&is));
80405339c03SStefano Zampini 
805e432b41dSStefano Zampini   /* Preallocation */
806e306f867SJed Brown   if (dm->prealloc_skip) {
8079566063dSJacob Faibussowitsch     PetscCall(MatSetUp(J));
808e306f867SJed Brown   } else {
8099566063dSJacob Faibussowitsch     PetscCall(MatISGetLocalMat(J, &lJ));
8109566063dSJacob Faibussowitsch     PetscCall(MatGetLocalToGlobalMapping(lJ, &ltog, NULL));
8119566063dSJacob Faibussowitsch     PetscCall(MatCreate(PetscObjectComm((PetscObject)lJ), &P));
8129566063dSJacob Faibussowitsch     PetscCall(MatSetType(P, MATPREALLOCATOR));
8139566063dSJacob Faibussowitsch     PetscCall(MatSetLocalToGlobalMapping(P, ltog, ltog));
8149566063dSJacob Faibussowitsch     PetscCall(MatGetSize(lJ, &N, NULL));
8159566063dSJacob Faibussowitsch     PetscCall(MatGetLocalSize(lJ, &n, NULL));
8169566063dSJacob Faibussowitsch     PetscCall(MatSetSizes(P, n, n, N, N));
8179566063dSJacob Faibussowitsch     PetscCall(MatSetBlockSize(P, dof));
8189566063dSJacob Faibussowitsch     PetscCall(MatSetUp(P));
81948a46eb9SPierre Jolivet     for (i = 0; i < nel; i++) PetscCall(MatSetValuesBlockedLocal(P, nen, e_loc + i * nen, nen, e_loc + i * nen, NULL, INSERT_VALUES));
8209566063dSJacob Faibussowitsch     PetscCall(MatPreallocatorPreallocate(P, (PetscBool)!da->prealloc_only, lJ));
8219566063dSJacob Faibussowitsch     PetscCall(MatISRestoreLocalMat(J, &lJ));
8229566063dSJacob Faibussowitsch     PetscCall(DMDARestoreElements(dm, &nel, &nen, &e_loc));
8239566063dSJacob Faibussowitsch     PetscCall(MatDestroy(&P));
824e432b41dSStefano Zampini 
8259566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
8269566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
827e306f867SJed Brown   }
8283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
829e584696dSStefano Zampini }
830e584696dSStefano Zampini 
831d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPISELL(DM da, Mat J)
832d71ae5a4SJacob Faibussowitsch {
8335e26d47bSHong Zhang   PetscInt               xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny, m, n, dim, s, *cols = NULL, k, nc, *rows = NULL, col, cnt, l, p;
8345e26d47bSHong Zhang   PetscInt               lstart, lend, pstart, pend, *dnz, *onz;
8355e26d47bSHong Zhang   MPI_Comm               comm;
8365e26d47bSHong Zhang   PetscScalar           *values;
8375e26d47bSHong Zhang   DMBoundaryType         bx, by;
8385e26d47bSHong Zhang   ISLocalToGlobalMapping ltog;
8395e26d47bSHong Zhang   DMDAStencilType        st;
8405e26d47bSHong Zhang 
8415e26d47bSHong Zhang   PetscFunctionBegin;
8425e26d47bSHong Zhang   /*
8435e26d47bSHong Zhang          nc - number of components per grid point
8445e26d47bSHong Zhang          col - number of colors needed in one direction for single component problem
8455e26d47bSHong Zhang   */
8469566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, NULL, NULL, NULL, &nc, &s, &bx, &by, NULL, &st));
8475e26d47bSHong Zhang   col = 2 * s + 1;
8489566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL));
8499566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL));
8509566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
8515e26d47bSHong Zhang 
8529566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(nc, &rows, col * col * nc * nc, &cols));
8539566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
8545e26d47bSHong Zhang 
8559566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, nc));
8565e26d47bSHong Zhang   /* determine the matrix preallocation information */
857d0609cedSBarry Smith   MatPreallocateBegin(comm, nc * nx * ny, nc * nx * ny, dnz, onz);
8585e26d47bSHong Zhang   for (i = xs; i < xs + nx; i++) {
8595e26d47bSHong Zhang     pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
8605e26d47bSHong Zhang     pend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
8615e26d47bSHong Zhang 
8625e26d47bSHong Zhang     for (j = ys; j < ys + ny; j++) {
8635e26d47bSHong Zhang       slot = i - gxs + gnx * (j - gys);
8645e26d47bSHong Zhang 
8655e26d47bSHong Zhang       lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
8665e26d47bSHong Zhang       lend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
8675e26d47bSHong Zhang 
8685e26d47bSHong Zhang       cnt = 0;
8695e26d47bSHong Zhang       for (k = 0; k < nc; k++) {
8705e26d47bSHong Zhang         for (l = lstart; l < lend + 1; l++) {
8715e26d47bSHong Zhang           for (p = pstart; p < pend + 1; p++) {
8725e26d47bSHong Zhang             if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star have either l = 0 or p = 0 */
8735e26d47bSHong Zhang               cols[cnt++] = k + nc * (slot + gnx * l + p);
8745e26d47bSHong Zhang             }
8755e26d47bSHong Zhang           }
8765e26d47bSHong Zhang         }
8775e26d47bSHong Zhang         rows[k] = k + nc * (slot);
8785e26d47bSHong Zhang       }
8799566063dSJacob Faibussowitsch       PetscCall(MatPreallocateSetLocal(ltog, nc, rows, ltog, cnt, cols, dnz, onz));
8805e26d47bSHong Zhang     }
8815e26d47bSHong Zhang   }
8829566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, nc));
8839566063dSJacob Faibussowitsch   PetscCall(MatSeqSELLSetPreallocation(J, 0, dnz));
8849566063dSJacob Faibussowitsch   PetscCall(MatMPISELLSetPreallocation(J, 0, dnz, 0, onz));
885d0609cedSBarry Smith   MatPreallocateEnd(dnz, onz);
8865e26d47bSHong Zhang 
8879566063dSJacob Faibussowitsch   PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
8885e26d47bSHong Zhang 
8895e26d47bSHong Zhang   /*
8905e26d47bSHong Zhang     For each node in the grid: we get the neighbors in the local (on processor ordering
8915e26d47bSHong Zhang     that includes the ghost points) then MatSetValuesLocal() maps those indices to the global
8925e26d47bSHong Zhang     PETSc ordering.
8935e26d47bSHong Zhang   */
8945e26d47bSHong Zhang   if (!da->prealloc_only) {
8959566063dSJacob Faibussowitsch     PetscCall(PetscCalloc1(col * col * nc * nc, &values));
8965e26d47bSHong Zhang     for (i = xs; i < xs + nx; i++) {
8975e26d47bSHong Zhang       pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
8985e26d47bSHong Zhang       pend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
8995e26d47bSHong Zhang 
9005e26d47bSHong Zhang       for (j = ys; j < ys + ny; j++) {
9015e26d47bSHong Zhang         slot = i - gxs + gnx * (j - gys);
9025e26d47bSHong Zhang 
9035e26d47bSHong Zhang         lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
9045e26d47bSHong Zhang         lend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
9055e26d47bSHong Zhang 
9065e26d47bSHong Zhang         cnt = 0;
9075e26d47bSHong Zhang         for (k = 0; k < nc; k++) {
9085e26d47bSHong Zhang           for (l = lstart; l < lend + 1; l++) {
9095e26d47bSHong Zhang             for (p = pstart; p < pend + 1; p++) {
9105e26d47bSHong Zhang               if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star have either l = 0 or p = 0 */
9115e26d47bSHong Zhang                 cols[cnt++] = k + nc * (slot + gnx * l + p);
9125e26d47bSHong Zhang               }
9135e26d47bSHong Zhang             }
9145e26d47bSHong Zhang           }
9155e26d47bSHong Zhang           rows[k] = k + nc * (slot);
9165e26d47bSHong Zhang         }
9179566063dSJacob Faibussowitsch         PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, values, INSERT_VALUES));
9185e26d47bSHong Zhang       }
9195e26d47bSHong Zhang     }
9209566063dSJacob Faibussowitsch     PetscCall(PetscFree(values));
921e7e92044SBarry Smith     /* do not copy values to GPU since they are all zero and not yet needed there */
9229566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_TRUE));
9239566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
9249566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
9259566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_FALSE));
9269566063dSJacob Faibussowitsch     PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
9275e26d47bSHong Zhang   }
9289566063dSJacob Faibussowitsch   PetscCall(PetscFree2(rows, cols));
9293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9305e26d47bSHong Zhang }
9315e26d47bSHong Zhang 
932d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPISELL(DM da, Mat J)
933d71ae5a4SJacob Faibussowitsch {
934711261dbSHong Zhang   PetscInt               xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny;
935711261dbSHong Zhang   PetscInt               m, n, dim, s, *cols = NULL, k, nc, *rows = NULL, col, cnt, l, p, *dnz = NULL, *onz = NULL;
936711261dbSHong Zhang   PetscInt               istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk, M, N, P;
937711261dbSHong Zhang   MPI_Comm               comm;
938711261dbSHong Zhang   PetscScalar           *values;
939711261dbSHong Zhang   DMBoundaryType         bx, by, bz;
940711261dbSHong Zhang   ISLocalToGlobalMapping ltog;
941711261dbSHong Zhang   DMDAStencilType        st;
942711261dbSHong Zhang 
943711261dbSHong Zhang   PetscFunctionBegin;
944711261dbSHong Zhang   /*
945711261dbSHong Zhang          nc - number of components per grid point
946711261dbSHong Zhang          col - number of colors needed in one direction for single component problem
947711261dbSHong Zhang   */
9489566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &nc, &s, &bx, &by, &bz, &st));
949711261dbSHong Zhang   col = 2 * s + 1;
9509566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz));
9519566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz));
9529566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
953711261dbSHong Zhang 
9549566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(nc, &rows, col * col * col * nc * nc, &cols));
9559566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
956711261dbSHong Zhang 
9579566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, nc));
958711261dbSHong Zhang   /* determine the matrix preallocation information */
959d0609cedSBarry Smith   MatPreallocateBegin(comm, nc * nx * ny * nz, nc * nx * ny * nz, dnz, onz);
960711261dbSHong Zhang   for (i = xs; i < xs + nx; i++) {
961711261dbSHong Zhang     istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
962711261dbSHong Zhang     iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
963711261dbSHong Zhang     for (j = ys; j < ys + ny; j++) {
964711261dbSHong Zhang       jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
965711261dbSHong Zhang       jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
966711261dbSHong Zhang       for (k = zs; k < zs + nz; k++) {
967711261dbSHong Zhang         kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k));
968711261dbSHong Zhang         kend   = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1));
969711261dbSHong Zhang 
970711261dbSHong Zhang         slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs);
971711261dbSHong Zhang 
972711261dbSHong Zhang         cnt = 0;
973711261dbSHong Zhang         for (l = 0; l < nc; l++) {
974711261dbSHong Zhang           for (ii = istart; ii < iend + 1; ii++) {
975711261dbSHong Zhang             for (jj = jstart; jj < jend + 1; jj++) {
976711261dbSHong Zhang               for (kk = kstart; kk < kend + 1; kk++) {
977711261dbSHong Zhang                 if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/
978711261dbSHong Zhang                   cols[cnt++] = l + nc * (slot + ii + gnx * jj + gnx * gny * kk);
979711261dbSHong Zhang                 }
980711261dbSHong Zhang               }
981711261dbSHong Zhang             }
982711261dbSHong Zhang           }
983711261dbSHong Zhang           rows[l] = l + nc * (slot);
984711261dbSHong Zhang         }
9859566063dSJacob Faibussowitsch         PetscCall(MatPreallocateSetLocal(ltog, nc, rows, ltog, cnt, cols, dnz, onz));
986711261dbSHong Zhang       }
987711261dbSHong Zhang     }
988711261dbSHong Zhang   }
9899566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, nc));
9909566063dSJacob Faibussowitsch   PetscCall(MatSeqSELLSetPreallocation(J, 0, dnz));
9919566063dSJacob Faibussowitsch   PetscCall(MatMPISELLSetPreallocation(J, 0, dnz, 0, onz));
992d0609cedSBarry Smith   MatPreallocateEnd(dnz, onz);
9939566063dSJacob Faibussowitsch   PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
994711261dbSHong Zhang 
995711261dbSHong Zhang   /*
996711261dbSHong Zhang     For each node in the grid: we get the neighbors in the local (on processor ordering
997711261dbSHong Zhang     that includes the ghost points) then MatSetValuesLocal() maps those indices to the global
998711261dbSHong Zhang     PETSc ordering.
999711261dbSHong Zhang   */
1000711261dbSHong Zhang   if (!da->prealloc_only) {
10019566063dSJacob Faibussowitsch     PetscCall(PetscCalloc1(col * col * col * nc * nc * nc, &values));
1002711261dbSHong Zhang     for (i = xs; i < xs + nx; i++) {
1003711261dbSHong Zhang       istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
1004711261dbSHong Zhang       iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
1005711261dbSHong Zhang       for (j = ys; j < ys + ny; j++) {
1006711261dbSHong Zhang         jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
1007711261dbSHong Zhang         jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
1008711261dbSHong Zhang         for (k = zs; k < zs + nz; k++) {
1009711261dbSHong Zhang           kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k));
1010711261dbSHong Zhang           kend   = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1));
1011711261dbSHong Zhang 
1012711261dbSHong Zhang           slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs);
1013711261dbSHong Zhang 
1014711261dbSHong Zhang           cnt = 0;
1015711261dbSHong Zhang           for (l = 0; l < nc; l++) {
1016711261dbSHong Zhang             for (ii = istart; ii < iend + 1; ii++) {
1017711261dbSHong Zhang               for (jj = jstart; jj < jend + 1; jj++) {
1018711261dbSHong Zhang                 for (kk = kstart; kk < kend + 1; kk++) {
1019711261dbSHong Zhang                   if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/
1020711261dbSHong Zhang                     cols[cnt++] = l + nc * (slot + ii + gnx * jj + gnx * gny * kk);
1021711261dbSHong Zhang                   }
1022711261dbSHong Zhang                 }
1023711261dbSHong Zhang               }
1024711261dbSHong Zhang             }
1025711261dbSHong Zhang             rows[l] = l + nc * (slot);
1026711261dbSHong Zhang           }
10279566063dSJacob Faibussowitsch           PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, values, INSERT_VALUES));
1028711261dbSHong Zhang         }
1029711261dbSHong Zhang       }
1030711261dbSHong Zhang     }
10319566063dSJacob Faibussowitsch     PetscCall(PetscFree(values));
1032e7e92044SBarry Smith     /* do not copy values to GPU since they are all zero and not yet needed there */
10339566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_TRUE));
10349566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
10359566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
10369566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_FALSE));
10379566063dSJacob Faibussowitsch     PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
1038711261dbSHong Zhang   }
10399566063dSJacob Faibussowitsch   PetscCall(PetscFree2(rows, cols));
10403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1041711261dbSHong Zhang }
1042711261dbSHong Zhang 
1043d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPIAIJ(DM da, Mat J)
1044d71ae5a4SJacob Faibussowitsch {
1045c1154cd5SBarry Smith   PetscInt               xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny, m, n, dim, s, *cols = NULL, k, nc, *rows = NULL, col, cnt, l, p, M, N;
104647c6ae99SBarry Smith   PetscInt               lstart, lend, pstart, pend, *dnz, *onz;
104747c6ae99SBarry Smith   MPI_Comm               comm;
1048bff4a2f0SMatthew G. Knepley   DMBoundaryType         bx, by;
1049844bd0d7SStefano Zampini   ISLocalToGlobalMapping ltog, mltog;
1050aa219208SBarry Smith   DMDAStencilType        st;
1051b294de21SRichard Tran Mills   PetscBool              removedups = PETSC_FALSE, alreadyboundtocpu = PETSC_TRUE;
105247c6ae99SBarry Smith 
105347c6ae99SBarry Smith   PetscFunctionBegin;
105447c6ae99SBarry Smith   /*
105547c6ae99SBarry Smith          nc - number of components per grid point
105647c6ae99SBarry Smith          col - number of colors needed in one direction for single component problem
105747c6ae99SBarry Smith   */
1058924e958dSJunchao Zhang   PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, &M, &N, NULL, &nc, &s, &bx, &by, NULL, &st));
10591baa6e33SBarry Smith   if (bx == DM_BOUNDARY_NONE && by == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_TRUE));
106047c6ae99SBarry Smith   col = 2 * s + 1;
1061c1154cd5SBarry Smith   /*
1062c1154cd5SBarry Smith        With one processor in periodic domains in a skinny dimension the code will label nonzero columns multiple times
1063c1154cd5SBarry Smith        because of "wrapping" around the end of the domain hitting an entry already counted in the other direction.
1064c1154cd5SBarry Smith   */
1065c1154cd5SBarry Smith   if (M == 1 && 2 * s >= m) removedups = PETSC_TRUE;
1066c1154cd5SBarry Smith   if (N == 1 && 2 * s >= n) removedups = PETSC_TRUE;
10679566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL));
10689566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL));
10699566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
107047c6ae99SBarry Smith 
10719566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(nc, &rows, col * col * nc * nc, &cols));
10729566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
107347c6ae99SBarry Smith 
10749566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, nc));
107547c6ae99SBarry Smith   /* determine the matrix preallocation information */
1076d0609cedSBarry Smith   MatPreallocateBegin(comm, nc * nx * ny, nc * nx * ny, dnz, onz);
107747c6ae99SBarry Smith   for (i = xs; i < xs + nx; i++) {
1078bff4a2f0SMatthew G. Knepley     pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
1079bff4a2f0SMatthew G. Knepley     pend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
108047c6ae99SBarry Smith 
108147c6ae99SBarry Smith     for (j = ys; j < ys + ny; j++) {
108247c6ae99SBarry Smith       slot = i - gxs + gnx * (j - gys);
108347c6ae99SBarry Smith 
1084bff4a2f0SMatthew G. Knepley       lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
1085bff4a2f0SMatthew G. Knepley       lend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
108647c6ae99SBarry Smith 
108747c6ae99SBarry Smith       cnt = 0;
108847c6ae99SBarry Smith       for (k = 0; k < nc; k++) {
108947c6ae99SBarry Smith         for (l = lstart; l < lend + 1; l++) {
109047c6ae99SBarry Smith           for (p = pstart; p < pend + 1; p++) {
1091aa219208SBarry Smith             if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star have either l = 0 or p = 0 */
109247c6ae99SBarry Smith               cols[cnt++] = k + nc * (slot + gnx * l + p);
109347c6ae99SBarry Smith             }
109447c6ae99SBarry Smith           }
109547c6ae99SBarry Smith         }
109647c6ae99SBarry Smith         rows[k] = k + nc * (slot);
109747c6ae99SBarry Smith       }
10981baa6e33SBarry Smith       if (removedups) PetscCall(MatPreallocateSetLocalRemoveDups(ltog, nc, rows, ltog, cnt, cols, dnz, onz));
10991baa6e33SBarry Smith       else PetscCall(MatPreallocateSetLocal(ltog, nc, rows, ltog, cnt, cols, dnz, onz));
110047c6ae99SBarry Smith     }
1101c1154cd5SBarry Smith   }
11029566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, nc));
11039566063dSJacob Faibussowitsch   PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz));
11049566063dSJacob Faibussowitsch   PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz));
1105d0609cedSBarry Smith   MatPreallocateEnd(dnz, onz);
11069566063dSJacob Faibussowitsch   PetscCall(MatGetLocalToGlobalMapping(J, &mltog, NULL));
11071baa6e33SBarry Smith   if (!mltog) PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
110847c6ae99SBarry Smith 
110947c6ae99SBarry Smith   /*
111047c6ae99SBarry Smith     For each node in the grid: we get the neighbors in the local (on processor ordering
111147c6ae99SBarry Smith     that includes the ghost points) then MatSetValuesLocal() maps those indices to the global
111247c6ae99SBarry Smith     PETSc ordering.
111347c6ae99SBarry Smith   */
1114fcfd50ebSBarry Smith   if (!da->prealloc_only) {
111547c6ae99SBarry Smith     for (i = xs; i < xs + nx; i++) {
1116bff4a2f0SMatthew G. Knepley       pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
1117bff4a2f0SMatthew G. Knepley       pend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
111847c6ae99SBarry Smith 
111947c6ae99SBarry Smith       for (j = ys; j < ys + ny; j++) {
112047c6ae99SBarry Smith         slot = i - gxs + gnx * (j - gys);
112147c6ae99SBarry Smith 
1122bff4a2f0SMatthew G. Knepley         lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
1123bff4a2f0SMatthew G. Knepley         lend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
112447c6ae99SBarry Smith 
112547c6ae99SBarry Smith         cnt = 0;
112647c6ae99SBarry Smith         for (l = lstart; l < lend + 1; l++) {
112747c6ae99SBarry Smith           for (p = pstart; p < pend + 1; p++) {
1128aa219208SBarry Smith             if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star have either l = 0 or p = 0 */
1129071fcb05SBarry Smith               cols[cnt++] = nc * (slot + gnx * l + p);
1130071fcb05SBarry Smith               for (k = 1; k < nc; k++) {
11319371c9d4SSatish Balay                 cols[cnt] = 1 + cols[cnt - 1];
11329371c9d4SSatish Balay                 cnt++;
113347c6ae99SBarry Smith               }
113447c6ae99SBarry Smith             }
113547c6ae99SBarry Smith           }
113647c6ae99SBarry Smith         }
1137071fcb05SBarry Smith         for (k = 0; k < nc; k++) rows[k] = k + nc * (slot);
11389566063dSJacob Faibussowitsch         PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, NULL, INSERT_VALUES));
113947c6ae99SBarry Smith       }
114047c6ae99SBarry Smith     }
1141e7e92044SBarry Smith     /* do not copy values to GPU since they are all zero and not yet needed there */
11429566063dSJacob Faibussowitsch     PetscCall(MatBoundToCPU(J, &alreadyboundtocpu));
11439566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_TRUE));
11449566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
11459566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
11469566063dSJacob Faibussowitsch     if (!alreadyboundtocpu) PetscCall(MatBindToCPU(J, PETSC_FALSE));
11479566063dSJacob Faibussowitsch     PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
11481baa6e33SBarry Smith     if (bx == DM_BOUNDARY_NONE && by == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE));
114947c6ae99SBarry Smith   }
11509566063dSJacob Faibussowitsch   PetscCall(PetscFree2(rows, cols));
11513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
115247c6ae99SBarry Smith }
115347c6ae99SBarry Smith 
1154d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPIAIJ_Fill(DM da, Mat J)
1155d71ae5a4SJacob Faibussowitsch {
115647c6ae99SBarry Smith   PetscInt               xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny;
1157c1154cd5SBarry Smith   PetscInt               m, n, dim, s, *cols, k, nc, row, col, cnt, maxcnt = 0, l, p, M, N;
115847c6ae99SBarry Smith   PetscInt               lstart, lend, pstart, pend, *dnz, *onz;
115947c6ae99SBarry Smith   DM_DA                 *dd = (DM_DA *)da->data;
116047c6ae99SBarry Smith   PetscInt               ifill_col, *ofill = dd->ofill, *dfill = dd->dfill;
116147c6ae99SBarry Smith   MPI_Comm               comm;
1162bff4a2f0SMatthew G. Knepley   DMBoundaryType         bx, by;
116345b6f7e9SBarry Smith   ISLocalToGlobalMapping ltog;
1164aa219208SBarry Smith   DMDAStencilType        st;
1165c1154cd5SBarry Smith   PetscBool              removedups = PETSC_FALSE;
116647c6ae99SBarry Smith 
116747c6ae99SBarry Smith   PetscFunctionBegin;
116847c6ae99SBarry Smith   /*
116947c6ae99SBarry Smith          nc - number of components per grid point
117047c6ae99SBarry Smith          col - number of colors needed in one direction for single component problem
117147c6ae99SBarry Smith   */
1172924e958dSJunchao Zhang   PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, &M, &N, NULL, &nc, &s, &bx, &by, NULL, &st));
117347c6ae99SBarry Smith   col = 2 * s + 1;
1174c1154cd5SBarry Smith   /*
1175c1154cd5SBarry Smith        With one processor in periodic domains in a skinny dimension the code will label nonzero columns multiple times
1176c1154cd5SBarry Smith        because of "wrapping" around the end of the domain hitting an entry already counted in the other direction.
1177c1154cd5SBarry Smith   */
1178c1154cd5SBarry Smith   if (M == 1 && 2 * s >= m) removedups = PETSC_TRUE;
1179c1154cd5SBarry Smith   if (N == 1 && 2 * s >= n) removedups = PETSC_TRUE;
11809566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL));
11819566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL));
11829566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
118347c6ae99SBarry Smith 
11849566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(col * col * nc, &cols));
11859566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
118647c6ae99SBarry Smith 
11879566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, nc));
118847c6ae99SBarry Smith   /* determine the matrix preallocation information */
1189d0609cedSBarry Smith   MatPreallocateBegin(comm, nc * nx * ny, nc * nx * ny, dnz, onz);
119047c6ae99SBarry Smith   for (i = xs; i < xs + nx; i++) {
1191bff4a2f0SMatthew G. Knepley     pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
1192bff4a2f0SMatthew G. Knepley     pend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
119347c6ae99SBarry Smith 
119447c6ae99SBarry Smith     for (j = ys; j < ys + ny; j++) {
119547c6ae99SBarry Smith       slot = i - gxs + gnx * (j - gys);
119647c6ae99SBarry Smith 
1197bff4a2f0SMatthew G. Knepley       lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
1198bff4a2f0SMatthew G. Knepley       lend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
119947c6ae99SBarry Smith 
120047c6ae99SBarry Smith       for (k = 0; k < nc; k++) {
120147c6ae99SBarry Smith         cnt = 0;
120247c6ae99SBarry Smith         for (l = lstart; l < lend + 1; l++) {
120347c6ae99SBarry Smith           for (p = pstart; p < pend + 1; p++) {
120447c6ae99SBarry Smith             if (l || p) {
1205aa219208SBarry Smith               if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star */
12068865f1eaSKarl Rupp                 for (ifill_col = ofill[k]; ifill_col < ofill[k + 1]; ifill_col++) cols[cnt++] = ofill[ifill_col] + nc * (slot + gnx * l + p);
120747c6ae99SBarry Smith               }
120847c6ae99SBarry Smith             } else {
120947c6ae99SBarry Smith               if (dfill) {
12108865f1eaSKarl Rupp                 for (ifill_col = dfill[k]; ifill_col < dfill[k + 1]; ifill_col++) cols[cnt++] = dfill[ifill_col] + nc * (slot + gnx * l + p);
121147c6ae99SBarry Smith               } else {
12128865f1eaSKarl Rupp                 for (ifill_col = 0; ifill_col < nc; ifill_col++) cols[cnt++] = ifill_col + nc * (slot + gnx * l + p);
121347c6ae99SBarry Smith               }
121447c6ae99SBarry Smith             }
121547c6ae99SBarry Smith           }
121647c6ae99SBarry Smith         }
121747c6ae99SBarry Smith         row    = k + nc * (slot);
1218c0ab637bSBarry Smith         maxcnt = PetscMax(maxcnt, cnt);
12191baa6e33SBarry Smith         if (removedups) PetscCall(MatPreallocateSetLocalRemoveDups(ltog, 1, &row, ltog, cnt, cols, dnz, onz));
12201baa6e33SBarry Smith         else PetscCall(MatPreallocateSetLocal(ltog, 1, &row, ltog, cnt, cols, dnz, onz));
122147c6ae99SBarry Smith       }
122247c6ae99SBarry Smith     }
1223c1154cd5SBarry Smith   }
12249566063dSJacob Faibussowitsch   PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz));
12259566063dSJacob Faibussowitsch   PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz));
1226d0609cedSBarry Smith   MatPreallocateEnd(dnz, onz);
12279566063dSJacob Faibussowitsch   PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
122847c6ae99SBarry Smith 
122947c6ae99SBarry Smith   /*
123047c6ae99SBarry Smith     For each node in the grid: we get the neighbors in the local (on processor ordering
123147c6ae99SBarry Smith     that includes the ghost points) then MatSetValuesLocal() maps those indices to the global
123247c6ae99SBarry Smith     PETSc ordering.
123347c6ae99SBarry Smith   */
1234fcfd50ebSBarry Smith   if (!da->prealloc_only) {
123547c6ae99SBarry Smith     for (i = xs; i < xs + nx; i++) {
1236bff4a2f0SMatthew G. Knepley       pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
1237bff4a2f0SMatthew G. Knepley       pend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
123847c6ae99SBarry Smith 
123947c6ae99SBarry Smith       for (j = ys; j < ys + ny; j++) {
124047c6ae99SBarry Smith         slot = i - gxs + gnx * (j - gys);
124147c6ae99SBarry Smith 
1242bff4a2f0SMatthew G. Knepley         lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
1243bff4a2f0SMatthew G. Knepley         lend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
124447c6ae99SBarry Smith 
124547c6ae99SBarry Smith         for (k = 0; k < nc; k++) {
124647c6ae99SBarry Smith           cnt = 0;
124747c6ae99SBarry Smith           for (l = lstart; l < lend + 1; l++) {
124847c6ae99SBarry Smith             for (p = pstart; p < pend + 1; p++) {
124947c6ae99SBarry Smith               if (l || p) {
1250aa219208SBarry Smith                 if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star */
12518865f1eaSKarl Rupp                   for (ifill_col = ofill[k]; ifill_col < ofill[k + 1]; ifill_col++) cols[cnt++] = ofill[ifill_col] + nc * (slot + gnx * l + p);
125247c6ae99SBarry Smith                 }
125347c6ae99SBarry Smith               } else {
125447c6ae99SBarry Smith                 if (dfill) {
12558865f1eaSKarl Rupp                   for (ifill_col = dfill[k]; ifill_col < dfill[k + 1]; ifill_col++) cols[cnt++] = dfill[ifill_col] + nc * (slot + gnx * l + p);
125647c6ae99SBarry Smith                 } else {
12578865f1eaSKarl Rupp                   for (ifill_col = 0; ifill_col < nc; ifill_col++) cols[cnt++] = ifill_col + nc * (slot + gnx * l + p);
125847c6ae99SBarry Smith                 }
125947c6ae99SBarry Smith               }
126047c6ae99SBarry Smith             }
126147c6ae99SBarry Smith           }
126247c6ae99SBarry Smith           row = k + nc * (slot);
12639566063dSJacob Faibussowitsch           PetscCall(MatSetValuesLocal(J, 1, &row, cnt, cols, NULL, INSERT_VALUES));
126447c6ae99SBarry Smith         }
126547c6ae99SBarry Smith       }
126647c6ae99SBarry Smith     }
1267e7e92044SBarry Smith     /* do not copy values to GPU since they are all zero and not yet needed there */
12689566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_TRUE));
12699566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
12709566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
12719566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_FALSE));
12729566063dSJacob Faibussowitsch     PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
127347c6ae99SBarry Smith   }
12749566063dSJacob Faibussowitsch   PetscCall(PetscFree(cols));
12753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
127647c6ae99SBarry Smith }
127747c6ae99SBarry Smith 
1278d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPIAIJ(DM da, Mat J)
1279d71ae5a4SJacob Faibussowitsch {
128047c6ae99SBarry Smith   PetscInt               xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny;
12810298fd71SBarry Smith   PetscInt               m, n, dim, s, *cols = NULL, k, nc, *rows = NULL, col, cnt, l, p, *dnz = NULL, *onz = NULL;
1282c1154cd5SBarry Smith   PetscInt               istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk, M, N, P;
128347c6ae99SBarry Smith   MPI_Comm               comm;
1284bff4a2f0SMatthew G. Knepley   DMBoundaryType         bx, by, bz;
1285844bd0d7SStefano Zampini   ISLocalToGlobalMapping ltog, mltog;
1286aa219208SBarry Smith   DMDAStencilType        st;
1287c1154cd5SBarry Smith   PetscBool              removedups = PETSC_FALSE;
128847c6ae99SBarry Smith 
128947c6ae99SBarry Smith   PetscFunctionBegin;
129047c6ae99SBarry Smith   /*
129147c6ae99SBarry Smith          nc - number of components per grid point
129247c6ae99SBarry Smith          col - number of colors needed in one direction for single component problem
129347c6ae99SBarry Smith   */
12949566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &nc, &s, &bx, &by, &bz, &st));
129548a46eb9SPierre Jolivet   if (bx == DM_BOUNDARY_NONE && by == DM_BOUNDARY_NONE && bz == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_TRUE));
129647c6ae99SBarry Smith   col = 2 * s + 1;
129747c6ae99SBarry Smith 
1298c1154cd5SBarry Smith   /*
1299c1154cd5SBarry Smith        With one processor in periodic domains in a skinny dimension the code will label nonzero columns multiple times
1300c1154cd5SBarry Smith        because of "wrapping" around the end of the domain hitting an entry already counted in the other direction.
1301c1154cd5SBarry Smith   */
1302c1154cd5SBarry Smith   if (M == 1 && 2 * s >= m) removedups = PETSC_TRUE;
1303c1154cd5SBarry Smith   if (N == 1 && 2 * s >= n) removedups = PETSC_TRUE;
1304c1154cd5SBarry Smith   if (P == 1 && 2 * s >= p) removedups = PETSC_TRUE;
1305c1154cd5SBarry Smith 
13069566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz));
13079566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz));
13089566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
130947c6ae99SBarry Smith 
13109566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(nc, &rows, col * col * col * nc * nc, &cols));
13119566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
131247c6ae99SBarry Smith 
13139566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, nc));
131447c6ae99SBarry Smith   /* determine the matrix preallocation information */
1315d0609cedSBarry Smith   MatPreallocateBegin(comm, nc * nx * ny * nz, nc * nx * ny * nz, dnz, onz);
131647c6ae99SBarry Smith   for (i = xs; i < xs + nx; i++) {
1317bff4a2f0SMatthew G. Knepley     istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
1318bff4a2f0SMatthew G. Knepley     iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
131947c6ae99SBarry Smith     for (j = ys; j < ys + ny; j++) {
1320bff4a2f0SMatthew G. Knepley       jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
1321bff4a2f0SMatthew G. Knepley       jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
132247c6ae99SBarry Smith       for (k = zs; k < zs + nz; k++) {
1323bff4a2f0SMatthew G. Knepley         kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k));
1324bff4a2f0SMatthew G. Knepley         kend   = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1));
132547c6ae99SBarry Smith 
132647c6ae99SBarry Smith         slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs);
132747c6ae99SBarry Smith 
132847c6ae99SBarry Smith         cnt = 0;
132947c6ae99SBarry Smith         for (l = 0; l < nc; l++) {
133047c6ae99SBarry Smith           for (ii = istart; ii < iend + 1; ii++) {
133147c6ae99SBarry Smith             for (jj = jstart; jj < jend + 1; jj++) {
133247c6ae99SBarry Smith               for (kk = kstart; kk < kend + 1; kk++) {
1333aa219208SBarry Smith                 if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/
133447c6ae99SBarry Smith                   cols[cnt++] = l + nc * (slot + ii + gnx * jj + gnx * gny * kk);
133547c6ae99SBarry Smith                 }
133647c6ae99SBarry Smith               }
133747c6ae99SBarry Smith             }
133847c6ae99SBarry Smith           }
133947c6ae99SBarry Smith           rows[l] = l + nc * (slot);
134047c6ae99SBarry Smith         }
13411baa6e33SBarry Smith         if (removedups) PetscCall(MatPreallocateSetLocalRemoveDups(ltog, nc, rows, ltog, cnt, cols, dnz, onz));
13421baa6e33SBarry Smith         else PetscCall(MatPreallocateSetLocal(ltog, nc, rows, ltog, cnt, cols, dnz, onz));
134347c6ae99SBarry Smith       }
134447c6ae99SBarry Smith     }
1345c1154cd5SBarry Smith   }
13469566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, nc));
13479566063dSJacob Faibussowitsch   PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz));
13489566063dSJacob Faibussowitsch   PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz));
1349d0609cedSBarry Smith   MatPreallocateEnd(dnz, onz);
13509566063dSJacob Faibussowitsch   PetscCall(MatGetLocalToGlobalMapping(J, &mltog, NULL));
135148a46eb9SPierre Jolivet   if (!mltog) PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
135247c6ae99SBarry Smith 
135347c6ae99SBarry Smith   /*
135447c6ae99SBarry Smith     For each node in the grid: we get the neighbors in the local (on processor ordering
135547c6ae99SBarry Smith     that includes the ghost points) then MatSetValuesLocal() maps those indices to the global
135647c6ae99SBarry Smith     PETSc ordering.
135747c6ae99SBarry Smith   */
1358fcfd50ebSBarry Smith   if (!da->prealloc_only) {
135947c6ae99SBarry Smith     for (i = xs; i < xs + nx; i++) {
1360bff4a2f0SMatthew G. Knepley       istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
1361bff4a2f0SMatthew G. Knepley       iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
136247c6ae99SBarry Smith       for (j = ys; j < ys + ny; j++) {
1363bff4a2f0SMatthew G. Knepley         jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
1364bff4a2f0SMatthew G. Knepley         jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
136547c6ae99SBarry Smith         for (k = zs; k < zs + nz; k++) {
1366bff4a2f0SMatthew G. Knepley           kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k));
1367bff4a2f0SMatthew G. Knepley           kend   = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1));
136847c6ae99SBarry Smith 
136947c6ae99SBarry Smith           slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs);
137047c6ae99SBarry Smith 
137147c6ae99SBarry Smith           cnt = 0;
137247c6ae99SBarry Smith           for (kk = kstart; kk < kend + 1; kk++) {
1373071fcb05SBarry Smith             for (jj = jstart; jj < jend + 1; jj++) {
1374071fcb05SBarry Smith               for (ii = istart; ii < iend + 1; ii++) {
1375aa219208SBarry Smith                 if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/
1376071fcb05SBarry Smith                   cols[cnt++] = nc * (slot + ii + gnx * jj + gnx * gny * kk);
1377071fcb05SBarry Smith                   for (l = 1; l < nc; l++) {
13789371c9d4SSatish Balay                     cols[cnt] = 1 + cols[cnt - 1];
13799371c9d4SSatish Balay                     cnt++;
138047c6ae99SBarry Smith                   }
138147c6ae99SBarry Smith                 }
138247c6ae99SBarry Smith               }
138347c6ae99SBarry Smith             }
138447c6ae99SBarry Smith           }
13859371c9d4SSatish Balay           rows[0] = nc * (slot);
13869371c9d4SSatish Balay           for (l = 1; l < nc; l++) rows[l] = 1 + rows[l - 1];
13879566063dSJacob Faibussowitsch           PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, NULL, INSERT_VALUES));
138847c6ae99SBarry Smith         }
138947c6ae99SBarry Smith       }
139047c6ae99SBarry Smith     }
1391e7e92044SBarry Smith     /* do not copy values to GPU since they are all zero and not yet needed there */
13929566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_TRUE));
13939566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
13949566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
139548a46eb9SPierre Jolivet     if (bx == DM_BOUNDARY_NONE && by == DM_BOUNDARY_NONE && bz == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE));
13969566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_FALSE));
13979566063dSJacob Faibussowitsch     PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
139847c6ae99SBarry Smith   }
13999566063dSJacob Faibussowitsch   PetscCall(PetscFree2(rows, cols));
14003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
140147c6ae99SBarry Smith }
140247c6ae99SBarry Smith 
1403d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_1d_MPIAIJ_Fill(DM da, Mat J)
1404d71ae5a4SJacob Faibussowitsch {
1405ce308e1dSBarry Smith   DM_DA                 *dd = (DM_DA *)da->data;
1406ce308e1dSBarry Smith   PetscInt               xs, nx, i, j, gxs, gnx, row, k, l;
14078d4c968fSBarry Smith   PetscInt               m, dim, s, *cols = NULL, nc, cnt, maxcnt = 0, *ocols;
14080acb5bebSBarry Smith   PetscInt              *ofill = dd->ofill, *dfill = dd->dfill;
1409bff4a2f0SMatthew G. Knepley   DMBoundaryType         bx;
141045b6f7e9SBarry Smith   ISLocalToGlobalMapping ltog;
1411ce308e1dSBarry Smith   PetscMPIInt            rank, size;
1412ce308e1dSBarry Smith 
1413ce308e1dSBarry Smith   PetscFunctionBegin;
14149566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)da), &rank));
14159566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)da), &size));
1416ce308e1dSBarry Smith 
1417ce308e1dSBarry Smith   /*
1418ce308e1dSBarry Smith          nc - number of components per grid point
1419ce308e1dSBarry Smith   */
14209566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, NULL, NULL, NULL, NULL, NULL, &nc, &s, &bx, NULL, NULL, NULL));
142108401ef6SPierre Jolivet   PetscCheck(s <= 1, PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "Matrix creation for 1d not implemented correctly for stencil width larger than 1");
14229566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &nx, NULL, NULL));
14239566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, NULL, NULL, &gnx, NULL, NULL));
1424ce308e1dSBarry Smith 
14259566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, nc));
14269566063dSJacob Faibussowitsch   PetscCall(PetscCalloc2(nx * nc, &cols, nx * nc, &ocols));
1427ce308e1dSBarry Smith 
1428ce308e1dSBarry Smith   /*
1429ce308e1dSBarry Smith         note should be smaller for first and last process with no periodic
1430ce308e1dSBarry Smith         does not handle dfill
1431ce308e1dSBarry Smith   */
1432ce308e1dSBarry Smith   cnt = 0;
1433ce308e1dSBarry Smith   /* coupling with process to the left */
1434ce308e1dSBarry Smith   for (i = 0; i < s; i++) {
1435ce308e1dSBarry Smith     for (j = 0; j < nc; j++) {
1436dd400576SPatrick Sanan       ocols[cnt] = ((rank == 0) ? 0 : (s - i) * (ofill[j + 1] - ofill[j]));
14370acb5bebSBarry Smith       cols[cnt]  = dfill[j + 1] - dfill[j] + (s + i) * (ofill[j + 1] - ofill[j]);
1438dd400576SPatrick Sanan       if (rank == 0 && (dd->bx == DM_BOUNDARY_PERIODIC)) {
1439831644c1SBarry Smith         if (size > 1) ocols[cnt] += (s - i) * (ofill[j + 1] - ofill[j]);
1440831644c1SBarry Smith         else cols[cnt] += (s - i) * (ofill[j + 1] - ofill[j]);
1441831644c1SBarry Smith       }
1442c0ab637bSBarry Smith       maxcnt = PetscMax(maxcnt, ocols[cnt] + cols[cnt]);
1443ce308e1dSBarry Smith       cnt++;
1444ce308e1dSBarry Smith     }
1445ce308e1dSBarry Smith   }
1446ce308e1dSBarry Smith   for (i = s; i < nx - s; i++) {
1447ce308e1dSBarry Smith     for (j = 0; j < nc; j++) {
14480acb5bebSBarry Smith       cols[cnt] = dfill[j + 1] - dfill[j] + 2 * s * (ofill[j + 1] - ofill[j]);
1449c0ab637bSBarry Smith       maxcnt    = PetscMax(maxcnt, ocols[cnt] + cols[cnt]);
1450ce308e1dSBarry Smith       cnt++;
1451ce308e1dSBarry Smith     }
1452ce308e1dSBarry Smith   }
1453ce308e1dSBarry Smith   /* coupling with process to the right */
1454ce308e1dSBarry Smith   for (i = nx - s; i < nx; i++) {
1455ce308e1dSBarry Smith     for (j = 0; j < nc; j++) {
1456ce308e1dSBarry Smith       ocols[cnt] = ((rank == (size - 1)) ? 0 : (i - nx + s + 1) * (ofill[j + 1] - ofill[j]));
14570acb5bebSBarry Smith       cols[cnt]  = dfill[j + 1] - dfill[j] + (s + nx - i - 1) * (ofill[j + 1] - ofill[j]);
1458831644c1SBarry Smith       if ((rank == size - 1) && (dd->bx == DM_BOUNDARY_PERIODIC)) {
1459831644c1SBarry Smith         if (size > 1) ocols[cnt] += (i - nx + s + 1) * (ofill[j + 1] - ofill[j]);
1460831644c1SBarry Smith         else cols[cnt] += (i - nx + s + 1) * (ofill[j + 1] - ofill[j]);
1461831644c1SBarry Smith       }
1462c0ab637bSBarry Smith       maxcnt = PetscMax(maxcnt, ocols[cnt] + cols[cnt]);
1463ce308e1dSBarry Smith       cnt++;
1464ce308e1dSBarry Smith     }
1465ce308e1dSBarry Smith   }
1466ce308e1dSBarry Smith 
14679566063dSJacob Faibussowitsch   PetscCall(MatSeqAIJSetPreallocation(J, 0, cols));
14689566063dSJacob Faibussowitsch   PetscCall(MatMPIAIJSetPreallocation(J, 0, cols, 0, ocols));
14699566063dSJacob Faibussowitsch   PetscCall(PetscFree2(cols, ocols));
1470ce308e1dSBarry Smith 
14719566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
14729566063dSJacob Faibussowitsch   PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
1473ce308e1dSBarry Smith 
1474ce308e1dSBarry Smith   /*
1475ce308e1dSBarry Smith     For each node in the grid: we get the neighbors in the local (on processor ordering
1476ce308e1dSBarry Smith     that includes the ghost points) then MatSetValuesLocal() maps those indices to the global
1477ce308e1dSBarry Smith     PETSc ordering.
1478ce308e1dSBarry Smith   */
1479ce308e1dSBarry Smith   if (!da->prealloc_only) {
14809566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(maxcnt, &cols));
1481ce308e1dSBarry Smith     row = xs * nc;
1482ce308e1dSBarry Smith     /* coupling with process to the left */
1483ce308e1dSBarry Smith     for (i = xs; i < xs + s; i++) {
1484ce308e1dSBarry Smith       for (j = 0; j < nc; j++) {
1485ce308e1dSBarry Smith         cnt = 0;
1486ce308e1dSBarry Smith         if (rank) {
1487ce308e1dSBarry Smith           for (l = 0; l < s; l++) {
1488ce308e1dSBarry Smith             for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i - s + l) * nc + ofill[k];
1489ce308e1dSBarry Smith           }
1490ce308e1dSBarry Smith         }
1491dd400576SPatrick Sanan         if (rank == 0 && (dd->bx == DM_BOUNDARY_PERIODIC)) {
1492831644c1SBarry Smith           for (l = 0; l < s; l++) {
1493831644c1SBarry Smith             for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (m + i - s - l) * nc + ofill[k];
1494831644c1SBarry Smith           }
1495831644c1SBarry Smith         }
14960acb5bebSBarry Smith         if (dfill) {
1497ad540459SPierre Jolivet           for (k = dfill[j]; k < dfill[j + 1]; k++) cols[cnt++] = i * nc + dfill[k];
14980acb5bebSBarry Smith         } else {
1499ad540459SPierre Jolivet           for (k = 0; k < nc; k++) cols[cnt++] = i * nc + k;
15000acb5bebSBarry Smith         }
1501ce308e1dSBarry Smith         for (l = 0; l < s; l++) {
1502ce308e1dSBarry Smith           for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i + s - l) * nc + ofill[k];
1503ce308e1dSBarry Smith         }
15049566063dSJacob Faibussowitsch         PetscCall(MatSetValues(J, 1, &row, cnt, cols, NULL, INSERT_VALUES));
1505ce308e1dSBarry Smith         row++;
1506ce308e1dSBarry Smith       }
1507ce308e1dSBarry Smith     }
1508ce308e1dSBarry Smith     for (i = xs + s; i < xs + nx - s; i++) {
1509ce308e1dSBarry Smith       for (j = 0; j < nc; j++) {
1510ce308e1dSBarry Smith         cnt = 0;
1511ce308e1dSBarry Smith         for (l = 0; l < s; l++) {
1512ce308e1dSBarry Smith           for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i - s + l) * nc + ofill[k];
1513ce308e1dSBarry Smith         }
15140acb5bebSBarry Smith         if (dfill) {
1515ad540459SPierre Jolivet           for (k = dfill[j]; k < dfill[j + 1]; k++) cols[cnt++] = i * nc + dfill[k];
15160acb5bebSBarry Smith         } else {
1517ad540459SPierre Jolivet           for (k = 0; k < nc; k++) cols[cnt++] = i * nc + k;
15180acb5bebSBarry Smith         }
1519ce308e1dSBarry Smith         for (l = 0; l < s; l++) {
1520ce308e1dSBarry Smith           for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i + s - l) * nc + ofill[k];
1521ce308e1dSBarry Smith         }
15229566063dSJacob Faibussowitsch         PetscCall(MatSetValues(J, 1, &row, cnt, cols, NULL, INSERT_VALUES));
1523ce308e1dSBarry Smith         row++;
1524ce308e1dSBarry Smith       }
1525ce308e1dSBarry Smith     }
1526ce308e1dSBarry Smith     /* coupling with process to the right */
1527ce308e1dSBarry Smith     for (i = xs + nx - s; i < xs + nx; i++) {
1528ce308e1dSBarry Smith       for (j = 0; j < nc; j++) {
1529ce308e1dSBarry Smith         cnt = 0;
1530ce308e1dSBarry Smith         for (l = 0; l < s; l++) {
1531ce308e1dSBarry Smith           for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i - s + l) * nc + ofill[k];
1532ce308e1dSBarry Smith         }
15330acb5bebSBarry Smith         if (dfill) {
1534ad540459SPierre Jolivet           for (k = dfill[j]; k < dfill[j + 1]; k++) cols[cnt++] = i * nc + dfill[k];
15350acb5bebSBarry Smith         } else {
1536ad540459SPierre Jolivet           for (k = 0; k < nc; k++) cols[cnt++] = i * nc + k;
15370acb5bebSBarry Smith         }
1538ce308e1dSBarry Smith         if (rank < size - 1) {
1539ce308e1dSBarry Smith           for (l = 0; l < s; l++) {
1540ce308e1dSBarry Smith             for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i + s - l) * nc + ofill[k];
1541ce308e1dSBarry Smith           }
1542ce308e1dSBarry Smith         }
1543831644c1SBarry Smith         if ((rank == size - 1) && (dd->bx == DM_BOUNDARY_PERIODIC)) {
1544831644c1SBarry Smith           for (l = 0; l < s; l++) {
1545831644c1SBarry Smith             for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i - s - l - m + 2) * nc + ofill[k];
1546831644c1SBarry Smith           }
1547831644c1SBarry Smith         }
15489566063dSJacob Faibussowitsch         PetscCall(MatSetValues(J, 1, &row, cnt, cols, NULL, INSERT_VALUES));
1549ce308e1dSBarry Smith         row++;
1550ce308e1dSBarry Smith       }
1551ce308e1dSBarry Smith     }
15529566063dSJacob Faibussowitsch     PetscCall(PetscFree(cols));
1553e7e92044SBarry Smith     /* do not copy values to GPU since they are all zero and not yet needed there */
15549566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_TRUE));
15559566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
15569566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
15579566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_FALSE));
15589566063dSJacob Faibussowitsch     PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
1559ce308e1dSBarry Smith   }
15603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1561ce308e1dSBarry Smith }
1562ce308e1dSBarry Smith 
1563d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_1d_MPIAIJ(DM da, Mat J)
1564d71ae5a4SJacob Faibussowitsch {
156547c6ae99SBarry Smith   PetscInt               xs, nx, i, i1, slot, gxs, gnx;
15660298fd71SBarry Smith   PetscInt               m, dim, s, *cols = NULL, nc, *rows = NULL, col, cnt, l;
156747c6ae99SBarry Smith   PetscInt               istart, iend;
1568bff4a2f0SMatthew G. Knepley   DMBoundaryType         bx;
1569844bd0d7SStefano Zampini   ISLocalToGlobalMapping ltog, mltog;
157047c6ae99SBarry Smith 
157147c6ae99SBarry Smith   PetscFunctionBegin;
157247c6ae99SBarry Smith   /*
157347c6ae99SBarry Smith          nc - number of components per grid point
157447c6ae99SBarry Smith          col - number of colors needed in one direction for single component problem
157547c6ae99SBarry Smith   */
15769566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, NULL, NULL, NULL, NULL, NULL, &nc, &s, &bx, NULL, NULL, NULL));
157748a46eb9SPierre Jolivet   if (bx == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_TRUE));
157847c6ae99SBarry Smith   col = 2 * s + 1;
157947c6ae99SBarry Smith 
15809566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &nx, NULL, NULL));
15819566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, NULL, NULL, &gnx, NULL, NULL));
158247c6ae99SBarry Smith 
15839566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, nc));
15849566063dSJacob Faibussowitsch   PetscCall(MatSeqAIJSetPreallocation(J, col * nc, NULL));
15859566063dSJacob Faibussowitsch   PetscCall(MatMPIAIJSetPreallocation(J, col * nc, NULL, col * nc, NULL));
158647c6ae99SBarry Smith 
15879566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
15889566063dSJacob Faibussowitsch   PetscCall(MatGetLocalToGlobalMapping(J, &mltog, NULL));
158948a46eb9SPierre Jolivet   if (!mltog) PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
159047c6ae99SBarry Smith 
159147c6ae99SBarry Smith   /*
159247c6ae99SBarry Smith     For each node in the grid: we get the neighbors in the local (on processor ordering
159347c6ae99SBarry Smith     that includes the ghost points) then MatSetValuesLocal() maps those indices to the global
159447c6ae99SBarry Smith     PETSc ordering.
159547c6ae99SBarry Smith   */
1596fcfd50ebSBarry Smith   if (!da->prealloc_only) {
15979566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(nc, &rows, col * nc * nc, &cols));
159847c6ae99SBarry Smith     for (i = xs; i < xs + nx; i++) {
159947c6ae99SBarry Smith       istart = PetscMax(-s, gxs - i);
160047c6ae99SBarry Smith       iend   = PetscMin(s, gxs + gnx - i - 1);
160147c6ae99SBarry Smith       slot   = i - gxs;
160247c6ae99SBarry Smith 
160347c6ae99SBarry Smith       cnt = 0;
160447c6ae99SBarry Smith       for (i1 = istart; i1 < iend + 1; i1++) {
1605071fcb05SBarry Smith         cols[cnt++] = nc * (slot + i1);
1606071fcb05SBarry Smith         for (l = 1; l < nc; l++) {
16079371c9d4SSatish Balay           cols[cnt] = 1 + cols[cnt - 1];
16089371c9d4SSatish Balay           cnt++;
160947c6ae99SBarry Smith         }
161047c6ae99SBarry Smith       }
16119371c9d4SSatish Balay       rows[0] = nc * (slot);
16129371c9d4SSatish Balay       for (l = 1; l < nc; l++) rows[l] = 1 + rows[l - 1];
16139566063dSJacob Faibussowitsch       PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, NULL, INSERT_VALUES));
161447c6ae99SBarry Smith     }
1615e7e92044SBarry Smith     /* do not copy values to GPU since they are all zero and not yet needed there */
16169566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_TRUE));
16179566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
16189566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
161948a46eb9SPierre Jolivet     if (bx == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE));
16209566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_FALSE));
16219566063dSJacob Faibussowitsch     PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
16229566063dSJacob Faibussowitsch     PetscCall(PetscFree2(rows, cols));
1623ce308e1dSBarry Smith   }
16243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
162547c6ae99SBarry Smith }
162647c6ae99SBarry Smith 
1627d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_1d_SeqAIJ_NoPreallocation(DM da, Mat J)
1628d71ae5a4SJacob Faibussowitsch {
162919b08ed1SBarry Smith   PetscInt               xs, nx, i, i1, slot, gxs, gnx;
163019b08ed1SBarry Smith   PetscInt               m, dim, s, *cols = NULL, nc, *rows = NULL, col, cnt, l;
163119b08ed1SBarry Smith   PetscInt               istart, iend;
163219b08ed1SBarry Smith   DMBoundaryType         bx;
163319b08ed1SBarry Smith   ISLocalToGlobalMapping ltog, mltog;
163419b08ed1SBarry Smith 
163519b08ed1SBarry Smith   PetscFunctionBegin;
163619b08ed1SBarry Smith   /*
163719b08ed1SBarry Smith          nc - number of components per grid point
163819b08ed1SBarry Smith          col - number of colors needed in one direction for single component problem
163919b08ed1SBarry Smith   */
16409566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, NULL, NULL, NULL, NULL, NULL, &nc, &s, &bx, NULL, NULL, NULL));
164119b08ed1SBarry Smith   col = 2 * s + 1;
164219b08ed1SBarry Smith 
16439566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &nx, NULL, NULL));
16449566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, NULL, NULL, &gnx, NULL, NULL));
164519b08ed1SBarry Smith 
16469566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, nc));
16479566063dSJacob Faibussowitsch   PetscCall(MatSeqAIJSetTotalPreallocation(J, nx * nc * col * nc));
164819b08ed1SBarry Smith 
16499566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
16509566063dSJacob Faibussowitsch   PetscCall(MatGetLocalToGlobalMapping(J, &mltog, NULL));
165148a46eb9SPierre Jolivet   if (!mltog) PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
165219b08ed1SBarry Smith 
165319b08ed1SBarry Smith   /*
165419b08ed1SBarry Smith     For each node in the grid: we get the neighbors in the local (on processor ordering
165519b08ed1SBarry Smith     that includes the ghost points) then MatSetValuesLocal() maps those indices to the global
165619b08ed1SBarry Smith     PETSc ordering.
165719b08ed1SBarry Smith   */
165819b08ed1SBarry Smith   if (!da->prealloc_only) {
16599566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(nc, &rows, col * nc * nc, &cols));
166019b08ed1SBarry Smith     for (i = xs; i < xs + nx; i++) {
166119b08ed1SBarry Smith       istart = PetscMax(-s, gxs - i);
166219b08ed1SBarry Smith       iend   = PetscMin(s, gxs + gnx - i - 1);
166319b08ed1SBarry Smith       slot   = i - gxs;
166419b08ed1SBarry Smith 
166519b08ed1SBarry Smith       cnt = 0;
166619b08ed1SBarry Smith       for (i1 = istart; i1 < iend + 1; i1++) {
166719b08ed1SBarry Smith         cols[cnt++] = nc * (slot + i1);
166819b08ed1SBarry Smith         for (l = 1; l < nc; l++) {
16699371c9d4SSatish Balay           cols[cnt] = 1 + cols[cnt - 1];
16709371c9d4SSatish Balay           cnt++;
167119b08ed1SBarry Smith         }
167219b08ed1SBarry Smith       }
16739371c9d4SSatish Balay       rows[0] = nc * (slot);
16749371c9d4SSatish Balay       for (l = 1; l < nc; l++) rows[l] = 1 + rows[l - 1];
16759566063dSJacob Faibussowitsch       PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, NULL, INSERT_VALUES));
167619b08ed1SBarry Smith     }
167719b08ed1SBarry Smith     /* do not copy values to GPU since they are all zero and not yet needed there */
16789566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_TRUE));
16799566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
16809566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
168148a46eb9SPierre Jolivet     if (bx == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE));
16829566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_FALSE));
16839566063dSJacob Faibussowitsch     PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
16849566063dSJacob Faibussowitsch     PetscCall(PetscFree2(rows, cols));
168519b08ed1SBarry Smith   }
16869566063dSJacob Faibussowitsch   PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE));
16873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
168819b08ed1SBarry Smith }
168919b08ed1SBarry Smith 
1690d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPIBAIJ(DM da, Mat J)
1691d71ae5a4SJacob Faibussowitsch {
169247c6ae99SBarry Smith   PetscInt               xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny;
169347c6ae99SBarry Smith   PetscInt               m, n, dim, s, *cols, nc, col, cnt, *dnz, *onz;
169447c6ae99SBarry Smith   PetscInt               istart, iend, jstart, jend, ii, jj;
169547c6ae99SBarry Smith   MPI_Comm               comm;
169647c6ae99SBarry Smith   PetscScalar           *values;
1697bff4a2f0SMatthew G. Knepley   DMBoundaryType         bx, by;
1698aa219208SBarry Smith   DMDAStencilType        st;
169945b6f7e9SBarry Smith   ISLocalToGlobalMapping ltog;
170047c6ae99SBarry Smith 
170147c6ae99SBarry Smith   PetscFunctionBegin;
170247c6ae99SBarry Smith   /*
170347c6ae99SBarry Smith      nc - number of components per grid point
170447c6ae99SBarry Smith      col - number of colors needed in one direction for single component problem
170547c6ae99SBarry Smith   */
17069566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, NULL, NULL, NULL, &nc, &s, &bx, &by, NULL, &st));
170747c6ae99SBarry Smith   col = 2 * s + 1;
170847c6ae99SBarry Smith 
17099566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL));
17109566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL));
17119566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
171247c6ae99SBarry Smith 
17139566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(col * col * nc * nc, &cols));
171447c6ae99SBarry Smith 
17159566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
171647c6ae99SBarry Smith 
171747c6ae99SBarry Smith   /* determine the matrix preallocation information */
1718d0609cedSBarry Smith   MatPreallocateBegin(comm, nx * ny, nx * ny, dnz, onz);
171947c6ae99SBarry Smith   for (i = xs; i < xs + nx; i++) {
1720bff4a2f0SMatthew G. Knepley     istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
1721bff4a2f0SMatthew G. Knepley     iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
172247c6ae99SBarry Smith     for (j = ys; j < ys + ny; j++) {
1723bff4a2f0SMatthew G. Knepley       jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
1724bff4a2f0SMatthew G. Knepley       jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
172547c6ae99SBarry Smith       slot   = i - gxs + gnx * (j - gys);
172647c6ae99SBarry Smith 
172747c6ae99SBarry Smith       /* Find block columns in block row */
172847c6ae99SBarry Smith       cnt = 0;
172947c6ae99SBarry Smith       for (ii = istart; ii < iend + 1; ii++) {
173047c6ae99SBarry Smith         for (jj = jstart; jj < jend + 1; jj++) {
1731aa219208SBarry Smith           if (st == DMDA_STENCIL_BOX || !ii || !jj) { /* BOX or on the STAR */
173247c6ae99SBarry Smith             cols[cnt++] = slot + ii + gnx * jj;
173347c6ae99SBarry Smith           }
173447c6ae99SBarry Smith         }
173547c6ae99SBarry Smith       }
17369566063dSJacob Faibussowitsch       PetscCall(MatPreallocateSetLocalBlock(ltog, 1, &slot, ltog, cnt, cols, dnz, onz));
173747c6ae99SBarry Smith     }
173847c6ae99SBarry Smith   }
17399566063dSJacob Faibussowitsch   PetscCall(MatSeqBAIJSetPreallocation(J, nc, 0, dnz));
17409566063dSJacob Faibussowitsch   PetscCall(MatMPIBAIJSetPreallocation(J, nc, 0, dnz, 0, onz));
1741d0609cedSBarry Smith   MatPreallocateEnd(dnz, onz);
174247c6ae99SBarry Smith 
17439566063dSJacob Faibussowitsch   PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
174447c6ae99SBarry Smith 
174547c6ae99SBarry Smith   /*
174647c6ae99SBarry Smith     For each node in the grid: we get the neighbors in the local (on processor ordering
174747c6ae99SBarry Smith     that includes the ghost points) then MatSetValuesLocal() maps those indices to the global
174847c6ae99SBarry Smith     PETSc ordering.
174947c6ae99SBarry Smith   */
1750fcfd50ebSBarry Smith   if (!da->prealloc_only) {
17519566063dSJacob Faibussowitsch     PetscCall(PetscCalloc1(col * col * nc * nc, &values));
175247c6ae99SBarry Smith     for (i = xs; i < xs + nx; i++) {
1753bff4a2f0SMatthew G. Knepley       istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
1754bff4a2f0SMatthew G. Knepley       iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
175547c6ae99SBarry Smith       for (j = ys; j < ys + ny; j++) {
1756bff4a2f0SMatthew G. Knepley         jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
1757bff4a2f0SMatthew G. Knepley         jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
175847c6ae99SBarry Smith         slot   = i - gxs + gnx * (j - gys);
175947c6ae99SBarry Smith         cnt    = 0;
176047c6ae99SBarry Smith         for (ii = istart; ii < iend + 1; ii++) {
176147c6ae99SBarry Smith           for (jj = jstart; jj < jend + 1; jj++) {
1762aa219208SBarry Smith             if (st == DMDA_STENCIL_BOX || !ii || !jj) { /* BOX or on the STAR */
176347c6ae99SBarry Smith               cols[cnt++] = slot + ii + gnx * jj;
176447c6ae99SBarry Smith             }
176547c6ae99SBarry Smith           }
176647c6ae99SBarry Smith         }
17679566063dSJacob Faibussowitsch         PetscCall(MatSetValuesBlockedLocal(J, 1, &slot, cnt, cols, values, INSERT_VALUES));
176847c6ae99SBarry Smith       }
176947c6ae99SBarry Smith     }
17709566063dSJacob Faibussowitsch     PetscCall(PetscFree(values));
1771e7e92044SBarry Smith     /* do not copy values to GPU since they are all zero and not yet needed there */
17729566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_TRUE));
17739566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
17749566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
17759566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_FALSE));
17769566063dSJacob Faibussowitsch     PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
177747c6ae99SBarry Smith   }
17789566063dSJacob Faibussowitsch   PetscCall(PetscFree(cols));
17793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
178047c6ae99SBarry Smith }
178147c6ae99SBarry Smith 
1782d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPIBAIJ(DM da, Mat J)
1783d71ae5a4SJacob Faibussowitsch {
178447c6ae99SBarry Smith   PetscInt               xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny;
178547c6ae99SBarry Smith   PetscInt               m, n, dim, s, *cols, k, nc, col, cnt, p, *dnz, *onz;
178647c6ae99SBarry Smith   PetscInt               istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk;
178747c6ae99SBarry Smith   MPI_Comm               comm;
178847c6ae99SBarry Smith   PetscScalar           *values;
1789bff4a2f0SMatthew G. Knepley   DMBoundaryType         bx, by, bz;
1790aa219208SBarry Smith   DMDAStencilType        st;
179145b6f7e9SBarry Smith   ISLocalToGlobalMapping ltog;
179247c6ae99SBarry Smith 
179347c6ae99SBarry Smith   PetscFunctionBegin;
179447c6ae99SBarry Smith   /*
179547c6ae99SBarry Smith          nc - number of components per grid point
179647c6ae99SBarry Smith          col - number of colors needed in one direction for single component problem
179747c6ae99SBarry Smith   */
17989566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, NULL, NULL, NULL, &nc, &s, &bx, &by, &bz, &st));
179947c6ae99SBarry Smith   col = 2 * s + 1;
180047c6ae99SBarry Smith 
18019566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz));
18029566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz));
18039566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
180447c6ae99SBarry Smith 
18059566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(col * col * col, &cols));
180647c6ae99SBarry Smith 
18079566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
180847c6ae99SBarry Smith 
180947c6ae99SBarry Smith   /* determine the matrix preallocation information */
1810d0609cedSBarry Smith   MatPreallocateBegin(comm, nx * ny * nz, nx * ny * nz, dnz, onz);
181147c6ae99SBarry Smith   for (i = xs; i < xs + nx; i++) {
1812bff4a2f0SMatthew G. Knepley     istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
1813bff4a2f0SMatthew G. Knepley     iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
181447c6ae99SBarry Smith     for (j = ys; j < ys + ny; j++) {
1815bff4a2f0SMatthew G. Knepley       jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
1816bff4a2f0SMatthew G. Knepley       jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
181747c6ae99SBarry Smith       for (k = zs; k < zs + nz; k++) {
1818bff4a2f0SMatthew G. Knepley         kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k));
1819bff4a2f0SMatthew G. Knepley         kend   = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1));
182047c6ae99SBarry Smith 
182147c6ae99SBarry Smith         slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs);
182247c6ae99SBarry Smith 
182347c6ae99SBarry Smith         /* Find block columns in block row */
182447c6ae99SBarry Smith         cnt = 0;
182547c6ae99SBarry Smith         for (ii = istart; ii < iend + 1; ii++) {
182647c6ae99SBarry Smith           for (jj = jstart; jj < jend + 1; jj++) {
182747c6ae99SBarry Smith             for (kk = kstart; kk < kend + 1; kk++) {
1828aa219208SBarry Smith               if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/
182947c6ae99SBarry Smith                 cols[cnt++] = slot + ii + gnx * jj + gnx * gny * kk;
183047c6ae99SBarry Smith               }
183147c6ae99SBarry Smith             }
183247c6ae99SBarry Smith           }
183347c6ae99SBarry Smith         }
18349566063dSJacob Faibussowitsch         PetscCall(MatPreallocateSetLocalBlock(ltog, 1, &slot, ltog, cnt, cols, dnz, onz));
183547c6ae99SBarry Smith       }
183647c6ae99SBarry Smith     }
183747c6ae99SBarry Smith   }
18389566063dSJacob Faibussowitsch   PetscCall(MatSeqBAIJSetPreallocation(J, nc, 0, dnz));
18399566063dSJacob Faibussowitsch   PetscCall(MatMPIBAIJSetPreallocation(J, nc, 0, dnz, 0, onz));
1840d0609cedSBarry Smith   MatPreallocateEnd(dnz, onz);
184147c6ae99SBarry Smith 
18429566063dSJacob Faibussowitsch   PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
184347c6ae99SBarry Smith 
184447c6ae99SBarry Smith   /*
184547c6ae99SBarry Smith     For each node in the grid: we get the neighbors in the local (on processor ordering
184647c6ae99SBarry Smith     that includes the ghost points) then MatSetValuesLocal() maps those indices to the global
184747c6ae99SBarry Smith     PETSc ordering.
184847c6ae99SBarry Smith   */
1849fcfd50ebSBarry Smith   if (!da->prealloc_only) {
18509566063dSJacob Faibussowitsch     PetscCall(PetscCalloc1(col * col * col * nc * nc, &values));
185147c6ae99SBarry Smith     for (i = xs; i < xs + nx; i++) {
1852bff4a2f0SMatthew G. Knepley       istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
1853bff4a2f0SMatthew G. Knepley       iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
185447c6ae99SBarry Smith       for (j = ys; j < ys + ny; j++) {
1855bff4a2f0SMatthew G. Knepley         jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
1856bff4a2f0SMatthew G. Knepley         jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
185747c6ae99SBarry Smith         for (k = zs; k < zs + nz; k++) {
1858bff4a2f0SMatthew G. Knepley           kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k));
1859bff4a2f0SMatthew G. Knepley           kend   = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1));
186047c6ae99SBarry Smith 
186147c6ae99SBarry Smith           slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs);
186247c6ae99SBarry Smith 
186347c6ae99SBarry Smith           cnt = 0;
186447c6ae99SBarry Smith           for (ii = istart; ii < iend + 1; ii++) {
186547c6ae99SBarry Smith             for (jj = jstart; jj < jend + 1; jj++) {
186647c6ae99SBarry Smith               for (kk = kstart; kk < kend + 1; kk++) {
1867aa219208SBarry Smith                 if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/
186847c6ae99SBarry Smith                   cols[cnt++] = slot + ii + gnx * jj + gnx * gny * kk;
186947c6ae99SBarry Smith                 }
187047c6ae99SBarry Smith               }
187147c6ae99SBarry Smith             }
187247c6ae99SBarry Smith           }
18739566063dSJacob Faibussowitsch           PetscCall(MatSetValuesBlockedLocal(J, 1, &slot, cnt, cols, values, INSERT_VALUES));
187447c6ae99SBarry Smith         }
187547c6ae99SBarry Smith       }
187647c6ae99SBarry Smith     }
18779566063dSJacob Faibussowitsch     PetscCall(PetscFree(values));
1878e7e92044SBarry Smith     /* do not copy values to GPU since they are all zero and not yet needed there */
18799566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_TRUE));
18809566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
18819566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
18829566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_FALSE));
18839566063dSJacob Faibussowitsch     PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
188447c6ae99SBarry Smith   }
18859566063dSJacob Faibussowitsch   PetscCall(PetscFree(cols));
18863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
188747c6ae99SBarry Smith }
188847c6ae99SBarry Smith 
188947c6ae99SBarry Smith /*
189047c6ae99SBarry Smith   This helper is for of SBAIJ preallocation, to discard the lower-triangular values which are difficult to
189147c6ae99SBarry Smith   identify in the local ordering with periodic domain.
189247c6ae99SBarry Smith */
1893d71ae5a4SJacob Faibussowitsch static PetscErrorCode L2GFilterUpperTriangular(ISLocalToGlobalMapping ltog, PetscInt *row, PetscInt *cnt, PetscInt col[])
1894d71ae5a4SJacob Faibussowitsch {
189547c6ae99SBarry Smith   PetscInt i, n;
189647c6ae99SBarry Smith 
189747c6ae99SBarry Smith   PetscFunctionBegin;
18989566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingApplyBlock(ltog, 1, row, row));
18999566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingApplyBlock(ltog, *cnt, col, col));
190047c6ae99SBarry Smith   for (i = 0, n = 0; i < *cnt; i++) {
190147c6ae99SBarry Smith     if (col[i] >= *row) col[n++] = col[i];
190247c6ae99SBarry Smith   }
190347c6ae99SBarry Smith   *cnt = n;
19043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
190547c6ae99SBarry Smith }
190647c6ae99SBarry Smith 
1907d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPISBAIJ(DM da, Mat J)
1908d71ae5a4SJacob Faibussowitsch {
190947c6ae99SBarry Smith   PetscInt               xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny;
191047c6ae99SBarry Smith   PetscInt               m, n, dim, s, *cols, nc, col, cnt, *dnz, *onz;
191147c6ae99SBarry Smith   PetscInt               istart, iend, jstart, jend, ii, jj;
191247c6ae99SBarry Smith   MPI_Comm               comm;
191347c6ae99SBarry Smith   PetscScalar           *values;
1914bff4a2f0SMatthew G. Knepley   DMBoundaryType         bx, by;
1915aa219208SBarry Smith   DMDAStencilType        st;
191645b6f7e9SBarry Smith   ISLocalToGlobalMapping ltog;
191747c6ae99SBarry Smith 
191847c6ae99SBarry Smith   PetscFunctionBegin;
191947c6ae99SBarry Smith   /*
192047c6ae99SBarry Smith      nc - number of components per grid point
192147c6ae99SBarry Smith      col - number of colors needed in one direction for single component problem
192247c6ae99SBarry Smith   */
19239566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, NULL, NULL, NULL, &nc, &s, &bx, &by, NULL, &st));
192447c6ae99SBarry Smith   col = 2 * s + 1;
192547c6ae99SBarry Smith 
19269566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL));
19279566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL));
19289566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
192947c6ae99SBarry Smith 
19309566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(col * col * nc * nc, &cols));
193147c6ae99SBarry Smith 
19329566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
193347c6ae99SBarry Smith 
193447c6ae99SBarry Smith   /* determine the matrix preallocation information */
1935d0609cedSBarry Smith   MatPreallocateBegin(comm, nx * ny, nx * ny, dnz, onz);
193647c6ae99SBarry Smith   for (i = xs; i < xs + nx; i++) {
1937bff4a2f0SMatthew G. Knepley     istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
1938bff4a2f0SMatthew G. Knepley     iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
193947c6ae99SBarry Smith     for (j = ys; j < ys + ny; j++) {
1940bff4a2f0SMatthew G. Knepley       jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
1941bff4a2f0SMatthew G. Knepley       jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
194247c6ae99SBarry Smith       slot   = i - gxs + gnx * (j - gys);
194347c6ae99SBarry Smith 
194447c6ae99SBarry Smith       /* Find block columns in block row */
194547c6ae99SBarry Smith       cnt = 0;
194647c6ae99SBarry Smith       for (ii = istart; ii < iend + 1; ii++) {
194747c6ae99SBarry Smith         for (jj = jstart; jj < jend + 1; jj++) {
1948ad540459SPierre Jolivet           if (st == DMDA_STENCIL_BOX || !ii || !jj) cols[cnt++] = slot + ii + gnx * jj;
194947c6ae99SBarry Smith         }
195047c6ae99SBarry Smith       }
19519566063dSJacob Faibussowitsch       PetscCall(L2GFilterUpperTriangular(ltog, &slot, &cnt, cols));
19529566063dSJacob Faibussowitsch       PetscCall(MatPreallocateSymmetricSetBlock(slot, cnt, cols, dnz, onz));
195347c6ae99SBarry Smith     }
195447c6ae99SBarry Smith   }
19559566063dSJacob Faibussowitsch   PetscCall(MatSeqSBAIJSetPreallocation(J, nc, 0, dnz));
19569566063dSJacob Faibussowitsch   PetscCall(MatMPISBAIJSetPreallocation(J, nc, 0, dnz, 0, onz));
1957d0609cedSBarry Smith   MatPreallocateEnd(dnz, onz);
195847c6ae99SBarry Smith 
19599566063dSJacob Faibussowitsch   PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
196047c6ae99SBarry Smith 
196147c6ae99SBarry Smith   /*
196247c6ae99SBarry Smith     For each node in the grid: we get the neighbors in the local (on processor ordering
196347c6ae99SBarry Smith     that includes the ghost points) then MatSetValuesLocal() maps those indices to the global
196447c6ae99SBarry Smith     PETSc ordering.
196547c6ae99SBarry Smith   */
1966fcfd50ebSBarry Smith   if (!da->prealloc_only) {
19679566063dSJacob Faibussowitsch     PetscCall(PetscCalloc1(col * col * nc * nc, &values));
196847c6ae99SBarry Smith     for (i = xs; i < xs + nx; i++) {
1969bff4a2f0SMatthew G. Knepley       istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
1970bff4a2f0SMatthew G. Knepley       iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
197147c6ae99SBarry Smith       for (j = ys; j < ys + ny; j++) {
1972bff4a2f0SMatthew G. Knepley         jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
1973bff4a2f0SMatthew G. Knepley         jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
197447c6ae99SBarry Smith         slot   = i - gxs + gnx * (j - gys);
197547c6ae99SBarry Smith 
197647c6ae99SBarry Smith         /* Find block columns in block row */
197747c6ae99SBarry Smith         cnt = 0;
197847c6ae99SBarry Smith         for (ii = istart; ii < iend + 1; ii++) {
197947c6ae99SBarry Smith           for (jj = jstart; jj < jend + 1; jj++) {
1980ad540459SPierre Jolivet             if (st == DMDA_STENCIL_BOX || !ii || !jj) cols[cnt++] = slot + ii + gnx * jj;
198147c6ae99SBarry Smith           }
198247c6ae99SBarry Smith         }
19839566063dSJacob Faibussowitsch         PetscCall(L2GFilterUpperTriangular(ltog, &slot, &cnt, cols));
19849566063dSJacob Faibussowitsch         PetscCall(MatSetValuesBlocked(J, 1, &slot, cnt, cols, values, INSERT_VALUES));
198547c6ae99SBarry Smith       }
198647c6ae99SBarry Smith     }
19879566063dSJacob Faibussowitsch     PetscCall(PetscFree(values));
1988e7e92044SBarry Smith     /* do not copy values to GPU since they are all zero and not yet needed there */
19899566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_TRUE));
19909566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
19919566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
19929566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_FALSE));
19939566063dSJacob Faibussowitsch     PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
199447c6ae99SBarry Smith   }
19959566063dSJacob Faibussowitsch   PetscCall(PetscFree(cols));
19963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
199747c6ae99SBarry Smith }
199847c6ae99SBarry Smith 
1999d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPISBAIJ(DM da, Mat J)
2000d71ae5a4SJacob Faibussowitsch {
200147c6ae99SBarry Smith   PetscInt               xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny;
200247c6ae99SBarry Smith   PetscInt               m, n, dim, s, *cols, k, nc, col, cnt, p, *dnz, *onz;
200347c6ae99SBarry Smith   PetscInt               istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk;
200447c6ae99SBarry Smith   MPI_Comm               comm;
200547c6ae99SBarry Smith   PetscScalar           *values;
2006bff4a2f0SMatthew G. Knepley   DMBoundaryType         bx, by, bz;
2007aa219208SBarry Smith   DMDAStencilType        st;
200845b6f7e9SBarry Smith   ISLocalToGlobalMapping ltog;
200947c6ae99SBarry Smith 
201047c6ae99SBarry Smith   PetscFunctionBegin;
201147c6ae99SBarry Smith   /*
201247c6ae99SBarry Smith      nc - number of components per grid point
201347c6ae99SBarry Smith      col - number of colors needed in one direction for single component problem
201447c6ae99SBarry Smith   */
20159566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, NULL, NULL, NULL, &nc, &s, &bx, &by, &bz, &st));
201647c6ae99SBarry Smith   col = 2 * s + 1;
201747c6ae99SBarry Smith 
20189566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz));
20199566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz));
20209566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
202147c6ae99SBarry Smith 
202247c6ae99SBarry Smith   /* create the matrix */
20239566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(col * col * col, &cols));
202447c6ae99SBarry Smith 
20259566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
202647c6ae99SBarry Smith 
202747c6ae99SBarry Smith   /* determine the matrix preallocation information */
2028d0609cedSBarry Smith   MatPreallocateBegin(comm, nx * ny * nz, nx * ny * nz, dnz, onz);
202947c6ae99SBarry Smith   for (i = xs; i < xs + nx; i++) {
2030bff4a2f0SMatthew G. Knepley     istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
2031bff4a2f0SMatthew G. Knepley     iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
203247c6ae99SBarry Smith     for (j = ys; j < ys + ny; j++) {
2033bff4a2f0SMatthew G. Knepley       jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
2034bff4a2f0SMatthew G. Knepley       jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
203547c6ae99SBarry Smith       for (k = zs; k < zs + nz; k++) {
2036bff4a2f0SMatthew G. Knepley         kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k));
2037bff4a2f0SMatthew G. Knepley         kend   = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1));
203847c6ae99SBarry Smith 
203947c6ae99SBarry Smith         slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs);
204047c6ae99SBarry Smith 
204147c6ae99SBarry Smith         /* Find block columns in block row */
204247c6ae99SBarry Smith         cnt = 0;
204347c6ae99SBarry Smith         for (ii = istart; ii < iend + 1; ii++) {
204447c6ae99SBarry Smith           for (jj = jstart; jj < jend + 1; jj++) {
204547c6ae99SBarry Smith             for (kk = kstart; kk < kend + 1; kk++) {
2046ad540459SPierre Jolivet               if ((st == DMDA_STENCIL_BOX) || (!ii && !jj) || (!jj && !kk) || (!ii && !kk)) cols[cnt++] = slot + ii + gnx * jj + gnx * gny * kk;
204747c6ae99SBarry Smith             }
204847c6ae99SBarry Smith           }
204947c6ae99SBarry Smith         }
20509566063dSJacob Faibussowitsch         PetscCall(L2GFilterUpperTriangular(ltog, &slot, &cnt, cols));
20519566063dSJacob Faibussowitsch         PetscCall(MatPreallocateSymmetricSetBlock(slot, cnt, cols, dnz, onz));
205247c6ae99SBarry Smith       }
205347c6ae99SBarry Smith     }
205447c6ae99SBarry Smith   }
20559566063dSJacob Faibussowitsch   PetscCall(MatSeqSBAIJSetPreallocation(J, nc, 0, dnz));
20569566063dSJacob Faibussowitsch   PetscCall(MatMPISBAIJSetPreallocation(J, nc, 0, dnz, 0, onz));
2057d0609cedSBarry Smith   MatPreallocateEnd(dnz, onz);
205847c6ae99SBarry Smith 
20599566063dSJacob Faibussowitsch   PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
206047c6ae99SBarry Smith 
206147c6ae99SBarry Smith   /*
206247c6ae99SBarry Smith     For each node in the grid: we get the neighbors in the local (on processor ordering
206347c6ae99SBarry Smith     that includes the ghost points) then MatSetValuesLocal() maps those indices to the global
206447c6ae99SBarry Smith     PETSc ordering.
206547c6ae99SBarry Smith   */
2066fcfd50ebSBarry Smith   if (!da->prealloc_only) {
20679566063dSJacob Faibussowitsch     PetscCall(PetscCalloc1(col * col * col * nc * nc, &values));
206847c6ae99SBarry Smith     for (i = xs; i < xs + nx; i++) {
2069bff4a2f0SMatthew G. Knepley       istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
2070bff4a2f0SMatthew G. Knepley       iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
207147c6ae99SBarry Smith       for (j = ys; j < ys + ny; j++) {
2072bff4a2f0SMatthew G. Knepley         jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
2073bff4a2f0SMatthew G. Knepley         jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
207447c6ae99SBarry Smith         for (k = zs; k < zs + nz; k++) {
2075bff4a2f0SMatthew G. Knepley           kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k));
2076bff4a2f0SMatthew G. Knepley           kend   = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1));
207747c6ae99SBarry Smith 
207847c6ae99SBarry Smith           slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs);
207947c6ae99SBarry Smith 
208047c6ae99SBarry Smith           cnt = 0;
208147c6ae99SBarry Smith           for (ii = istart; ii < iend + 1; ii++) {
208247c6ae99SBarry Smith             for (jj = jstart; jj < jend + 1; jj++) {
208347c6ae99SBarry Smith               for (kk = kstart; kk < kend + 1; kk++) {
2084ad540459SPierre Jolivet                 if ((st == DMDA_STENCIL_BOX) || (!ii && !jj) || (!jj && !kk) || (!ii && !kk)) cols[cnt++] = slot + ii + gnx * jj + gnx * gny * kk;
208547c6ae99SBarry Smith               }
208647c6ae99SBarry Smith             }
208747c6ae99SBarry Smith           }
20889566063dSJacob Faibussowitsch           PetscCall(L2GFilterUpperTriangular(ltog, &slot, &cnt, cols));
20899566063dSJacob Faibussowitsch           PetscCall(MatSetValuesBlocked(J, 1, &slot, cnt, cols, values, INSERT_VALUES));
209047c6ae99SBarry Smith         }
209147c6ae99SBarry Smith       }
209247c6ae99SBarry Smith     }
20939566063dSJacob Faibussowitsch     PetscCall(PetscFree(values));
2094e7e92044SBarry Smith     /* do not copy values to GPU since they are all zero and not yet needed there */
20959566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_TRUE));
20969566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
20979566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
20989566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_FALSE));
20999566063dSJacob Faibussowitsch     PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
210047c6ae99SBarry Smith   }
21019566063dSJacob Faibussowitsch   PetscCall(PetscFree(cols));
21023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
210347c6ae99SBarry Smith }
210447c6ae99SBarry Smith 
2105d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPIAIJ_Fill(DM da, Mat J)
2106d71ae5a4SJacob Faibussowitsch {
210747c6ae99SBarry Smith   PetscInt               xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny;
2108c0ab637bSBarry Smith   PetscInt               m, n, dim, s, *cols, k, nc, row, col, cnt, maxcnt = 0, l, p, *dnz, *onz;
2109c1154cd5SBarry Smith   PetscInt               istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk, M, N, P;
211047c6ae99SBarry Smith   DM_DA                 *dd = (DM_DA *)da->data;
211147c6ae99SBarry Smith   PetscInt               ifill_col, *dfill = dd->dfill, *ofill = dd->ofill;
211247c6ae99SBarry Smith   MPI_Comm               comm;
211347c6ae99SBarry Smith   PetscScalar           *values;
2114bff4a2f0SMatthew G. Knepley   DMBoundaryType         bx, by, bz;
211545b6f7e9SBarry Smith   ISLocalToGlobalMapping ltog;
2116aa219208SBarry Smith   DMDAStencilType        st;
2117c1154cd5SBarry Smith   PetscBool              removedups = PETSC_FALSE;
211847c6ae99SBarry Smith 
211947c6ae99SBarry Smith   PetscFunctionBegin;
212047c6ae99SBarry Smith   /*
212147c6ae99SBarry Smith          nc - number of components per grid point
212247c6ae99SBarry Smith          col - number of colors needed in one direction for single component problem
212347c6ae99SBarry Smith   */
21249566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &nc, &s, &bx, &by, &bz, &st));
212547c6ae99SBarry Smith   col = 2 * s + 1;
212647c6ae99SBarry Smith 
2127c1154cd5SBarry Smith   /*
2128c1154cd5SBarry Smith        With one processor in periodic domains in a skinny dimension the code will label nonzero columns multiple times
2129c1154cd5SBarry Smith        because of "wrapping" around the end of the domain hitting an entry already counted in the other direction.
2130c1154cd5SBarry Smith   */
2131c1154cd5SBarry Smith   if (M == 1 && 2 * s >= m) removedups = PETSC_TRUE;
2132c1154cd5SBarry Smith   if (N == 1 && 2 * s >= n) removedups = PETSC_TRUE;
2133c1154cd5SBarry Smith   if (P == 1 && 2 * s >= p) removedups = PETSC_TRUE;
2134c1154cd5SBarry Smith 
21359566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz));
21369566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz));
21379566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
213847c6ae99SBarry Smith 
21399566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(col * col * col * nc, &cols));
21409566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
214147c6ae99SBarry Smith 
214247c6ae99SBarry Smith   /* determine the matrix preallocation information */
2143d0609cedSBarry Smith   MatPreallocateBegin(comm, nc * nx * ny * nz, nc * nx * ny * nz, dnz, onz);
214447c6ae99SBarry Smith 
21459566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, nc));
214647c6ae99SBarry Smith   for (i = xs; i < xs + nx; i++) {
2147bff4a2f0SMatthew G. Knepley     istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
2148bff4a2f0SMatthew G. Knepley     iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
214947c6ae99SBarry Smith     for (j = ys; j < ys + ny; j++) {
2150bff4a2f0SMatthew G. Knepley       jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
2151bff4a2f0SMatthew G. Knepley       jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
215247c6ae99SBarry Smith       for (k = zs; k < zs + nz; k++) {
2153bff4a2f0SMatthew G. Knepley         kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k));
2154bff4a2f0SMatthew G. Knepley         kend   = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1));
215547c6ae99SBarry Smith 
215647c6ae99SBarry Smith         slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs);
215747c6ae99SBarry Smith 
215847c6ae99SBarry Smith         for (l = 0; l < nc; l++) {
215947c6ae99SBarry Smith           cnt = 0;
216047c6ae99SBarry Smith           for (ii = istart; ii < iend + 1; ii++) {
216147c6ae99SBarry Smith             for (jj = jstart; jj < jend + 1; jj++) {
216247c6ae99SBarry Smith               for (kk = kstart; kk < kend + 1; kk++) {
216347c6ae99SBarry Smith                 if (ii || jj || kk) {
2164aa219208SBarry Smith                   if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/
21658865f1eaSKarl Rupp                     for (ifill_col = ofill[l]; ifill_col < ofill[l + 1]; ifill_col++) cols[cnt++] = ofill[ifill_col] + nc * (slot + ii + gnx * jj + gnx * gny * kk);
216647c6ae99SBarry Smith                   }
216747c6ae99SBarry Smith                 } else {
216847c6ae99SBarry Smith                   if (dfill) {
21698865f1eaSKarl Rupp                     for (ifill_col = dfill[l]; ifill_col < dfill[l + 1]; ifill_col++) cols[cnt++] = dfill[ifill_col] + nc * (slot + ii + gnx * jj + gnx * gny * kk);
217047c6ae99SBarry Smith                   } else {
21718865f1eaSKarl Rupp                     for (ifill_col = 0; ifill_col < nc; ifill_col++) cols[cnt++] = ifill_col + nc * (slot + ii + gnx * jj + gnx * gny * kk);
217247c6ae99SBarry Smith                   }
217347c6ae99SBarry Smith                 }
217447c6ae99SBarry Smith               }
217547c6ae99SBarry Smith             }
217647c6ae99SBarry Smith           }
217747c6ae99SBarry Smith           row    = l + nc * (slot);
2178c0ab637bSBarry Smith           maxcnt = PetscMax(maxcnt, cnt);
21791baa6e33SBarry Smith           if (removedups) PetscCall(MatPreallocateSetLocalRemoveDups(ltog, 1, &row, ltog, cnt, cols, dnz, onz));
21801baa6e33SBarry Smith           else PetscCall(MatPreallocateSetLocal(ltog, 1, &row, ltog, cnt, cols, dnz, onz));
218147c6ae99SBarry Smith         }
218247c6ae99SBarry Smith       }
218347c6ae99SBarry Smith     }
2184c1154cd5SBarry Smith   }
21859566063dSJacob Faibussowitsch   PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz));
21869566063dSJacob Faibussowitsch   PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz));
2187d0609cedSBarry Smith   MatPreallocateEnd(dnz, onz);
21889566063dSJacob Faibussowitsch   PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
218947c6ae99SBarry Smith 
219047c6ae99SBarry Smith   /*
219147c6ae99SBarry Smith     For each node in the grid: we get the neighbors in the local (on processor ordering
219247c6ae99SBarry Smith     that includes the ghost points) then MatSetValuesLocal() maps those indices to the global
219347c6ae99SBarry Smith     PETSc ordering.
219447c6ae99SBarry Smith   */
2195fcfd50ebSBarry Smith   if (!da->prealloc_only) {
21969566063dSJacob Faibussowitsch     PetscCall(PetscCalloc1(maxcnt, &values));
219747c6ae99SBarry Smith     for (i = xs; i < xs + nx; i++) {
2198bff4a2f0SMatthew G. Knepley       istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
2199bff4a2f0SMatthew G. Knepley       iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
220047c6ae99SBarry Smith       for (j = ys; j < ys + ny; j++) {
2201bff4a2f0SMatthew G. Knepley         jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
2202bff4a2f0SMatthew G. Knepley         jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
220347c6ae99SBarry Smith         for (k = zs; k < zs + nz; k++) {
2204bff4a2f0SMatthew G. Knepley           kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k));
2205bff4a2f0SMatthew G. Knepley           kend   = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1));
220647c6ae99SBarry Smith 
220747c6ae99SBarry Smith           slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs);
220847c6ae99SBarry Smith 
220947c6ae99SBarry Smith           for (l = 0; l < nc; l++) {
221047c6ae99SBarry Smith             cnt = 0;
221147c6ae99SBarry Smith             for (ii = istart; ii < iend + 1; ii++) {
221247c6ae99SBarry Smith               for (jj = jstart; jj < jend + 1; jj++) {
221347c6ae99SBarry Smith                 for (kk = kstart; kk < kend + 1; kk++) {
221447c6ae99SBarry Smith                   if (ii || jj || kk) {
2215aa219208SBarry Smith                     if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/
22168865f1eaSKarl Rupp                       for (ifill_col = ofill[l]; ifill_col < ofill[l + 1]; ifill_col++) cols[cnt++] = ofill[ifill_col] + nc * (slot + ii + gnx * jj + gnx * gny * kk);
221747c6ae99SBarry Smith                     }
221847c6ae99SBarry Smith                   } else {
221947c6ae99SBarry Smith                     if (dfill) {
22208865f1eaSKarl Rupp                       for (ifill_col = dfill[l]; ifill_col < dfill[l + 1]; ifill_col++) cols[cnt++] = dfill[ifill_col] + nc * (slot + ii + gnx * jj + gnx * gny * kk);
222147c6ae99SBarry Smith                     } else {
22228865f1eaSKarl Rupp                       for (ifill_col = 0; ifill_col < nc; ifill_col++) cols[cnt++] = ifill_col + nc * (slot + ii + gnx * jj + gnx * gny * kk);
222347c6ae99SBarry Smith                     }
222447c6ae99SBarry Smith                   }
222547c6ae99SBarry Smith                 }
222647c6ae99SBarry Smith               }
222747c6ae99SBarry Smith             }
222847c6ae99SBarry Smith             row = l + nc * (slot);
22299566063dSJacob Faibussowitsch             PetscCall(MatSetValuesLocal(J, 1, &row, cnt, cols, values, INSERT_VALUES));
223047c6ae99SBarry Smith           }
223147c6ae99SBarry Smith         }
223247c6ae99SBarry Smith       }
223347c6ae99SBarry Smith     }
22349566063dSJacob Faibussowitsch     PetscCall(PetscFree(values));
2235e7e92044SBarry Smith     /* do not copy values to GPU since they are all zero and not yet needed there */
22369566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_TRUE));
22379566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
22389566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
22399566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_FALSE));
22409566063dSJacob Faibussowitsch     PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
224147c6ae99SBarry Smith   }
22429566063dSJacob Faibussowitsch   PetscCall(PetscFree(cols));
22433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
224447c6ae99SBarry Smith }
2245