xref: /petsc/src/dm/impls/da/fdda.c (revision a4e35b1925eceef64945ea472b84f2bf06a67b5e)
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
920298fd71SBarry 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 
10147c6ae99SBarry 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
11347c6ae99SBarry Smith   can be represented in the dfill, ofill format
11447c6ae99SBarry Smith 
11547c6ae99SBarry Smith   Contributed by Glenn Hammond
11647c6ae99SBarry Smith 
117dce8aebaSBarry Smith .seealso: `DM`, `DMDA`, `DMCreateMatrix()`, `DMDASetGetMatrix()`, `DMSetMatrixPreallocateOnly()`
11847c6ae99SBarry Smith @*/
119d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDASetBlockFills(DM da, const PetscInt *dfill, const PetscInt *ofill)
120d71ae5a4SJacob Faibussowitsch {
12147c6ae99SBarry Smith   DM_DA *dd = (DM_DA *)da->data;
12247c6ae99SBarry Smith 
12347c6ae99SBarry Smith   PetscFunctionBegin;
12409e28618SBarry Smith   /* save the given dfill and ofill information */
1259566063dSJacob Faibussowitsch   PetscCall(DMDASetBlockFills_Private(dfill, dd->w, &dd->dfill));
1269566063dSJacob Faibussowitsch   PetscCall(DMDASetBlockFills_Private(ofill, dd->w, &dd->ofill));
127ae4f298aSBarry Smith 
12809e28618SBarry Smith   /* count nonzeros in ofill columns */
1299566063dSJacob Faibussowitsch   PetscCall(DMDASetBlockFills_Private2(dd));
1303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
131ae4f298aSBarry Smith }
13209e28618SBarry Smith 
13309e28618SBarry Smith /*@
13409e28618SBarry Smith   DMDASetBlockFillsSparse - Sets the fill pattern in each block for a multi-component problem
135dce8aebaSBarry Smith   of the matrix returned by `DMCreateMatrix()`, using sparse representations
13609e28618SBarry Smith   of fill patterns.
13709e28618SBarry Smith 
13820f4b53cSBarry Smith   Logically Collective
13909e28618SBarry Smith 
140d8d19677SJose E. Roman   Input Parameters:
14109e28618SBarry Smith + da          - the distributed array
14260225df5SJacob Faibussowitsch . dfillsparse - the sparse fill pattern in the diagonal block (may be `NULL`, means use dense block)
14360225df5SJacob Faibussowitsch - ofillsparse - the sparse fill pattern in the off-diagonal blocks
14409e28618SBarry Smith 
14509e28618SBarry Smith   Level: developer
14609e28618SBarry Smith 
147dce8aebaSBarry Smith   Notes:
148dce8aebaSBarry Smith   This only makes sense when you are doing multicomponent problems but using the
149dce8aebaSBarry Smith   `MATMPIAIJ` matrix format
15009e28618SBarry Smith 
15120f4b53cSBarry Smith   The format for `dfill` and `ofill` is a sparse representation of a
15209e28618SBarry Smith   dof-by-dof matrix with 1 entries representing coupling and 0 entries
15309e28618SBarry Smith   for missing coupling.  The sparse representation is a 1 dimensional
15409e28618SBarry Smith   array of length nz + dof + 1, where nz is the number of non-zeros in
15509e28618SBarry Smith   the matrix.  The first dof entries in the array give the
15609e28618SBarry Smith   starting array indices of each row's items in the rest of the array,
15760942847SBarry Smith   the dof+1st item contains the value nz + dof + 1 (i.e. the entire length of the array)
15809e28618SBarry Smith   and the remaining nz items give the column indices of each of
15909e28618SBarry Smith   the 1s within the logical 2D matrix.  Each row's items within
16009e28618SBarry Smith   the array are the column indices of the 1s within that row
16109e28618SBarry Smith   of the 2D matrix.  PETSc developers may recognize that this is the
162dce8aebaSBarry Smith   same format as that computed by the `DMDASetBlockFills_Private()`
16309e28618SBarry Smith   function from a dense 2D matrix representation.
16409e28618SBarry Smith 
165dce8aebaSBarry Smith   `DMDASetGetMatrix()` allows you to provide general code for those more complicated nonzero patterns then
16620f4b53cSBarry Smith   can be represented in the `dfill`, `ofill` format
16709e28618SBarry Smith 
16809e28618SBarry Smith   Contributed by Philip C. Roth
16909e28618SBarry Smith 
170dce8aebaSBarry Smith .seealso: `DM`, `DMDA`, `DMDASetBlockFills()`, `DMCreateMatrix()`, `DMDASetGetMatrix()`, `DMSetMatrixPreallocateOnly()`
17109e28618SBarry Smith @*/
172d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDASetBlockFillsSparse(DM da, const PetscInt *dfillsparse, const PetscInt *ofillsparse)
173d71ae5a4SJacob Faibussowitsch {
17409e28618SBarry Smith   DM_DA *dd = (DM_DA *)da->data;
17509e28618SBarry Smith 
17609e28618SBarry Smith   PetscFunctionBegin;
17709e28618SBarry Smith   /* save the given dfill and ofill information */
1789566063dSJacob Faibussowitsch   PetscCall(DMDASetBlockFillsSparse_Private(dfillsparse, dd->w, &dd->dfill));
1799566063dSJacob Faibussowitsch   PetscCall(DMDASetBlockFillsSparse_Private(ofillsparse, dd->w, &dd->ofill));
18009e28618SBarry Smith 
18109e28618SBarry Smith   /* count nonzeros in ofill columns */
1829566063dSJacob Faibussowitsch   PetscCall(DMDASetBlockFills_Private2(dd));
1833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
18447c6ae99SBarry Smith }
18547c6ae99SBarry Smith 
186d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA(DM da, ISColoringType ctype, ISColoring *coloring)
187d71ae5a4SJacob Faibussowitsch {
18847c6ae99SBarry Smith   PetscInt       dim, m, n, p, nc;
189bff4a2f0SMatthew G. Knepley   DMBoundaryType bx, by, bz;
19047c6ae99SBarry Smith   MPI_Comm       comm;
19147c6ae99SBarry Smith   PetscMPIInt    size;
19247c6ae99SBarry Smith   PetscBool      isBAIJ;
19347c6ae99SBarry Smith   DM_DA         *dd = (DM_DA *)da->data;
19447c6ae99SBarry Smith 
19547c6ae99SBarry Smith   PetscFunctionBegin;
19647c6ae99SBarry Smith   /*
19747c6ae99SBarry Smith                                   m
19847c6ae99SBarry Smith           ------------------------------------------------------
19947c6ae99SBarry Smith          |                                                     |
20047c6ae99SBarry Smith          |                                                     |
20147c6ae99SBarry Smith          |               ----------------------                |
20247c6ae99SBarry Smith          |               |                    |                |
20347c6ae99SBarry Smith       n  |           yn  |                    |                |
20447c6ae99SBarry Smith          |               |                    |                |
20547c6ae99SBarry Smith          |               .---------------------                |
20647c6ae99SBarry Smith          |             (xs,ys)     xn                          |
20747c6ae99SBarry Smith          |            .                                        |
20847c6ae99SBarry Smith          |         (gxs,gys)                                   |
20947c6ae99SBarry Smith          |                                                     |
21047c6ae99SBarry Smith           -----------------------------------------------------
21147c6ae99SBarry Smith   */
21247c6ae99SBarry Smith 
21347c6ae99SBarry Smith   /*
21447c6ae99SBarry Smith          nc - number of components per grid point
21547c6ae99SBarry Smith          col - number of colors needed in one direction for single component problem
21647c6ae99SBarry Smith 
21747c6ae99SBarry Smith   */
2189566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, NULL, NULL, NULL, &m, &n, &p, &nc, NULL, &bx, &by, &bz, NULL));
21947c6ae99SBarry Smith 
2209566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
2219566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
2225bdb020cSBarry Smith   if (ctype == IS_COLORING_LOCAL) {
22347c6ae99SBarry Smith     if (size == 1) {
22447c6ae99SBarry Smith       ctype = IS_COLORING_GLOBAL;
2252ddab442SBarry Smith     } else {
2262ddab442SBarry 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");
22747c6ae99SBarry Smith     }
22847c6ae99SBarry Smith   }
22947c6ae99SBarry Smith 
230aa219208SBarry Smith   /* Tell the DMDA it has 1 degree of freedom per grid point so that the coloring for BAIJ
23147c6ae99SBarry Smith      matrices is for the blocks, not the individual matrix elements  */
2329566063dSJacob Faibussowitsch   PetscCall(PetscStrbeginswith(da->mattype, MATBAIJ, &isBAIJ));
2339566063dSJacob Faibussowitsch   if (!isBAIJ) PetscCall(PetscStrbeginswith(da->mattype, MATMPIBAIJ, &isBAIJ));
2349566063dSJacob Faibussowitsch   if (!isBAIJ) PetscCall(PetscStrbeginswith(da->mattype, MATSEQBAIJ, &isBAIJ));
23547c6ae99SBarry Smith   if (isBAIJ) {
23647c6ae99SBarry Smith     dd->w  = 1;
23747c6ae99SBarry Smith     dd->xs = dd->xs / nc;
23847c6ae99SBarry Smith     dd->xe = dd->xe / nc;
23947c6ae99SBarry Smith     dd->Xs = dd->Xs / nc;
24047c6ae99SBarry Smith     dd->Xe = dd->Xe / nc;
24147c6ae99SBarry Smith   }
24247c6ae99SBarry Smith 
24347c6ae99SBarry Smith   /*
244aa219208SBarry Smith      We do not provide a getcoloring function in the DMDA operations because
2459a1b256bSStefano Zampini    the basic DMDA does not know about matrices. We think of DMDA as being
24647c6ae99SBarry Smith    more low-level then matrices.
24747c6ae99SBarry Smith   */
2481baa6e33SBarry Smith   if (dim == 1) PetscCall(DMCreateColoring_DA_1d_MPIAIJ(da, ctype, coloring));
2491baa6e33SBarry Smith   else if (dim == 2) PetscCall(DMCreateColoring_DA_2d_MPIAIJ(da, ctype, coloring));
2501baa6e33SBarry Smith   else if (dim == 3) PetscCall(DMCreateColoring_DA_3d_MPIAIJ(da, ctype, coloring));
2511baa6e33SBarry 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);
25247c6ae99SBarry Smith   if (isBAIJ) {
25347c6ae99SBarry Smith     dd->w  = nc;
25447c6ae99SBarry Smith     dd->xs = dd->xs * nc;
25547c6ae99SBarry Smith     dd->xe = dd->xe * nc;
25647c6ae99SBarry Smith     dd->Xs = dd->Xs * nc;
25747c6ae99SBarry Smith     dd->Xe = dd->Xe * nc;
25847c6ae99SBarry Smith   }
2593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
26047c6ae99SBarry Smith }
26147c6ae99SBarry Smith 
262d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA_2d_MPIAIJ(DM da, ISColoringType ctype, ISColoring *coloring)
263d71ae5a4SJacob Faibussowitsch {
26447c6ae99SBarry Smith   PetscInt         xs, ys, nx, ny, i, j, ii, gxs, gys, gnx, gny, m, n, M, N, dim, s, k, nc, col;
265dec0b466SHong Zhang   PetscInt         ncolors = 0;
26647c6ae99SBarry Smith   MPI_Comm         comm;
267bff4a2f0SMatthew G. Knepley   DMBoundaryType   bx, by;
268aa219208SBarry Smith   DMDAStencilType  st;
26947c6ae99SBarry Smith   ISColoringValue *colors;
27047c6ae99SBarry Smith   DM_DA           *dd = (DM_DA *)da->data;
27147c6ae99SBarry Smith 
27247c6ae99SBarry Smith   PetscFunctionBegin;
27347c6ae99SBarry Smith   /*
27447c6ae99SBarry Smith          nc - number of components per grid point
27547c6ae99SBarry Smith          col - number of colors needed in one direction for single component problem
27647c6ae99SBarry Smith 
27747c6ae99SBarry Smith   */
2789566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, &M, &N, NULL, &nc, &s, &bx, &by, NULL, &st));
27947c6ae99SBarry Smith   col = 2 * s + 1;
2809566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL));
2819566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL));
2829566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
28347c6ae99SBarry Smith 
28447c6ae99SBarry Smith   /* special case as taught to us by Paul Hovland */
285aa219208SBarry Smith   if (st == DMDA_STENCIL_STAR && s == 1) {
2869566063dSJacob Faibussowitsch     PetscCall(DMCreateColoring_DA_2d_5pt_MPIAIJ(da, ctype, coloring));
28747c6ae99SBarry Smith   } else {
28847c6ae99SBarry Smith     if (ctype == IS_COLORING_GLOBAL) {
28947c6ae99SBarry Smith       if (!dd->localcoloring) {
2909566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1(nc * nx * ny, &colors));
29147c6ae99SBarry Smith         ii = 0;
29247c6ae99SBarry Smith         for (j = ys; j < ys + ny; j++) {
29347c6ae99SBarry Smith           for (i = xs; i < xs + nx; i++) {
294ad540459SPierre Jolivet             for (k = 0; k < nc; k++) colors[ii++] = k + nc * ((i % col) + col * (j % col));
29547c6ae99SBarry Smith           }
29647c6ae99SBarry Smith         }
29747c6ae99SBarry Smith         ncolors = nc + nc * (col - 1 + col * (col - 1));
2989566063dSJacob Faibussowitsch         PetscCall(ISColoringCreate(comm, ncolors, nc * nx * ny, colors, PETSC_OWN_POINTER, &dd->localcoloring));
29947c6ae99SBarry Smith       }
30047c6ae99SBarry Smith       *coloring = dd->localcoloring;
3015bdb020cSBarry Smith     } else if (ctype == IS_COLORING_LOCAL) {
30247c6ae99SBarry Smith       if (!dd->ghostedcoloring) {
3039566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1(nc * gnx * gny, &colors));
30447c6ae99SBarry Smith         ii = 0;
30547c6ae99SBarry Smith         for (j = gys; j < gys + gny; j++) {
30647c6ae99SBarry Smith           for (i = gxs; i < gxs + gnx; i++) {
30747c6ae99SBarry Smith             for (k = 0; k < nc; k++) {
30847c6ae99SBarry Smith               /* the complicated stuff is to handle periodic boundaries */
30947c6ae99SBarry Smith               colors[ii++] = k + nc * ((SetInRange(i, m) % col) + col * (SetInRange(j, n) % col));
31047c6ae99SBarry Smith             }
31147c6ae99SBarry Smith           }
31247c6ae99SBarry Smith         }
31347c6ae99SBarry Smith         ncolors = nc + nc * (col - 1 + col * (col - 1));
3149566063dSJacob Faibussowitsch         PetscCall(ISColoringCreate(comm, ncolors, nc * gnx * gny, colors, PETSC_OWN_POINTER, &dd->ghostedcoloring));
31547c6ae99SBarry Smith         /* PetscIntView(ncolors,(PetscInt*)colors,0); */
31647c6ae99SBarry Smith 
3179566063dSJacob Faibussowitsch         PetscCall(ISColoringSetType(dd->ghostedcoloring, IS_COLORING_LOCAL));
31847c6ae99SBarry Smith       }
31947c6ae99SBarry Smith       *coloring = dd->ghostedcoloring;
32098921bdaSJacob Faibussowitsch     } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_ARG_WRONG, "Unknown ISColoringType %d", (int)ctype);
32147c6ae99SBarry Smith   }
3229566063dSJacob Faibussowitsch   PetscCall(ISColoringReference(*coloring));
3233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
32447c6ae99SBarry Smith }
32547c6ae99SBarry Smith 
326d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA_3d_MPIAIJ(DM da, ISColoringType ctype, ISColoring *coloring)
327d71ae5a4SJacob Faibussowitsch {
32847c6ae99SBarry 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;
32947c6ae99SBarry Smith   PetscInt         ncolors;
33047c6ae99SBarry Smith   MPI_Comm         comm;
331bff4a2f0SMatthew G. Knepley   DMBoundaryType   bx, by, bz;
332aa219208SBarry Smith   DMDAStencilType  st;
33347c6ae99SBarry Smith   ISColoringValue *colors;
33447c6ae99SBarry Smith   DM_DA           *dd = (DM_DA *)da->data;
33547c6ae99SBarry Smith 
33647c6ae99SBarry Smith   PetscFunctionBegin;
33747c6ae99SBarry Smith   /*
33847c6ae99SBarry Smith          nc - number of components per grid point
33947c6ae99SBarry Smith          col - number of colors needed in one direction for single component problem
34047c6ae99SBarry Smith   */
3419566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &nc, &s, &bx, &by, &bz, &st));
34247c6ae99SBarry Smith   col = 2 * s + 1;
343494b1ccaSBarry 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\
344494b1ccaSBarry Smith                  by 2*stencil_width + 1\n");
345494b1ccaSBarry 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\
346494b1ccaSBarry Smith                  by 2*stencil_width + 1\n");
347494b1ccaSBarry 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\
348494b1ccaSBarry Smith                  by 2*stencil_width + 1\n");
349494b1ccaSBarry Smith 
3509566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz));
3519566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz));
3529566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
35347c6ae99SBarry Smith 
35447c6ae99SBarry Smith   /* create the coloring */
35547c6ae99SBarry Smith   if (ctype == IS_COLORING_GLOBAL) {
35647c6ae99SBarry Smith     if (!dd->localcoloring) {
3579566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nc * nx * ny * nz, &colors));
35847c6ae99SBarry Smith       ii = 0;
35947c6ae99SBarry Smith       for (k = zs; k < zs + nz; k++) {
36047c6ae99SBarry Smith         for (j = ys; j < ys + ny; j++) {
36147c6ae99SBarry Smith           for (i = xs; i < xs + nx; i++) {
362ad540459SPierre Jolivet             for (l = 0; l < nc; l++) colors[ii++] = l + nc * ((i % col) + col * (j % col) + col * col * (k % col));
36347c6ae99SBarry Smith           }
36447c6ae99SBarry Smith         }
36547c6ae99SBarry Smith       }
36647c6ae99SBarry Smith       ncolors = nc + nc * (col - 1 + col * (col - 1) + col * col * (col - 1));
3679566063dSJacob Faibussowitsch       PetscCall(ISColoringCreate(comm, ncolors, nc * nx * ny * nz, colors, PETSC_OWN_POINTER, &dd->localcoloring));
36847c6ae99SBarry Smith     }
36947c6ae99SBarry Smith     *coloring = dd->localcoloring;
3705bdb020cSBarry Smith   } else if (ctype == IS_COLORING_LOCAL) {
37147c6ae99SBarry Smith     if (!dd->ghostedcoloring) {
3729566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nc * gnx * gny * gnz, &colors));
37347c6ae99SBarry Smith       ii = 0;
37447c6ae99SBarry Smith       for (k = gzs; k < gzs + gnz; k++) {
37547c6ae99SBarry Smith         for (j = gys; j < gys + gny; j++) {
37647c6ae99SBarry Smith           for (i = gxs; i < gxs + gnx; i++) {
37747c6ae99SBarry Smith             for (l = 0; l < nc; l++) {
37847c6ae99SBarry Smith               /* the complicated stuff is to handle periodic boundaries */
37947c6ae99SBarry Smith               colors[ii++] = l + nc * ((SetInRange(i, m) % col) + col * (SetInRange(j, n) % col) + col * col * (SetInRange(k, p) % col));
38047c6ae99SBarry Smith             }
38147c6ae99SBarry Smith           }
38247c6ae99SBarry Smith         }
38347c6ae99SBarry Smith       }
38447c6ae99SBarry Smith       ncolors = nc + nc * (col - 1 + col * (col - 1) + col * col * (col - 1));
3859566063dSJacob Faibussowitsch       PetscCall(ISColoringCreate(comm, ncolors, nc * gnx * gny * gnz, colors, PETSC_OWN_POINTER, &dd->ghostedcoloring));
3869566063dSJacob Faibussowitsch       PetscCall(ISColoringSetType(dd->ghostedcoloring, IS_COLORING_LOCAL));
38747c6ae99SBarry Smith     }
38847c6ae99SBarry Smith     *coloring = dd->ghostedcoloring;
38998921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_ARG_WRONG, "Unknown ISColoringType %d", (int)ctype);
3909566063dSJacob Faibussowitsch   PetscCall(ISColoringReference(*coloring));
3913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
39247c6ae99SBarry Smith }
39347c6ae99SBarry Smith 
394d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA_1d_MPIAIJ(DM da, ISColoringType ctype, ISColoring *coloring)
395d71ae5a4SJacob Faibussowitsch {
39647c6ae99SBarry Smith   PetscInt         xs, nx, i, i1, gxs, gnx, l, m, M, dim, s, nc, col;
39747c6ae99SBarry Smith   PetscInt         ncolors;
39847c6ae99SBarry Smith   MPI_Comm         comm;
399bff4a2f0SMatthew G. Knepley   DMBoundaryType   bx;
40047c6ae99SBarry Smith   ISColoringValue *colors;
40147c6ae99SBarry Smith   DM_DA           *dd = (DM_DA *)da->data;
40247c6ae99SBarry Smith 
40347c6ae99SBarry Smith   PetscFunctionBegin;
40447c6ae99SBarry Smith   /*
40547c6ae99SBarry Smith          nc - number of components per grid point
40647c6ae99SBarry Smith          col - number of colors needed in one direction for single component problem
40747c6ae99SBarry Smith   */
4089566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, NULL, NULL, &M, NULL, NULL, &nc, &s, &bx, NULL, NULL, NULL));
40947c6ae99SBarry Smith   col = 2 * s + 1;
4109566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &nx, NULL, NULL));
4119566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, NULL, NULL, &gnx, NULL, NULL));
4129566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
41347c6ae99SBarry Smith 
41447c6ae99SBarry Smith   /* create the coloring */
41547c6ae99SBarry Smith   if (ctype == IS_COLORING_GLOBAL) {
41647c6ae99SBarry Smith     if (!dd->localcoloring) {
4179566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nc * nx, &colors));
418ae4f298aSBarry Smith       if (dd->ofillcols) {
419ae4f298aSBarry Smith         PetscInt tc = 0;
420ae4f298aSBarry Smith         for (i = 0; i < nc; i++) tc += (PetscInt)(dd->ofillcols[i] > 0);
421ae4f298aSBarry Smith         i1 = 0;
422ae4f298aSBarry Smith         for (i = xs; i < xs + nx; i++) {
423ae4f298aSBarry Smith           for (l = 0; l < nc; l++) {
424ae4f298aSBarry Smith             if (dd->ofillcols[l] && (i % col)) {
425ae4f298aSBarry Smith               colors[i1++] = nc - 1 + tc * ((i % col) - 1) + dd->ofillcols[l];
426ae4f298aSBarry Smith             } else {
427ae4f298aSBarry Smith               colors[i1++] = l;
428ae4f298aSBarry Smith             }
429ae4f298aSBarry Smith           }
430ae4f298aSBarry Smith         }
431ae4f298aSBarry Smith         ncolors = nc + 2 * s * tc;
432ae4f298aSBarry Smith       } else {
43347c6ae99SBarry Smith         i1 = 0;
43447c6ae99SBarry Smith         for (i = xs; i < xs + nx; i++) {
435ad540459SPierre Jolivet           for (l = 0; l < nc; l++) colors[i1++] = l + nc * (i % col);
43647c6ae99SBarry Smith         }
43747c6ae99SBarry Smith         ncolors = nc + nc * (col - 1);
438ae4f298aSBarry Smith       }
4399566063dSJacob Faibussowitsch       PetscCall(ISColoringCreate(comm, ncolors, nc * nx, colors, PETSC_OWN_POINTER, &dd->localcoloring));
44047c6ae99SBarry Smith     }
44147c6ae99SBarry Smith     *coloring = dd->localcoloring;
4425bdb020cSBarry Smith   } else if (ctype == IS_COLORING_LOCAL) {
44347c6ae99SBarry Smith     if (!dd->ghostedcoloring) {
4449566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nc * gnx, &colors));
44547c6ae99SBarry Smith       i1 = 0;
44647c6ae99SBarry Smith       for (i = gxs; i < gxs + gnx; i++) {
44747c6ae99SBarry Smith         for (l = 0; l < nc; l++) {
44847c6ae99SBarry Smith           /* the complicated stuff is to handle periodic boundaries */
44947c6ae99SBarry Smith           colors[i1++] = l + nc * (SetInRange(i, m) % col);
45047c6ae99SBarry Smith         }
45147c6ae99SBarry Smith       }
45247c6ae99SBarry Smith       ncolors = nc + nc * (col - 1);
4539566063dSJacob Faibussowitsch       PetscCall(ISColoringCreate(comm, ncolors, nc * gnx, colors, PETSC_OWN_POINTER, &dd->ghostedcoloring));
4549566063dSJacob Faibussowitsch       PetscCall(ISColoringSetType(dd->ghostedcoloring, IS_COLORING_LOCAL));
45547c6ae99SBarry Smith     }
45647c6ae99SBarry Smith     *coloring = dd->ghostedcoloring;
45798921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_ARG_WRONG, "Unknown ISColoringType %d", (int)ctype);
4589566063dSJacob Faibussowitsch   PetscCall(ISColoringReference(*coloring));
4593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
46047c6ae99SBarry Smith }
46147c6ae99SBarry Smith 
462d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA_2d_5pt_MPIAIJ(DM da, ISColoringType ctype, ISColoring *coloring)
463d71ae5a4SJacob Faibussowitsch {
46447c6ae99SBarry Smith   PetscInt         xs, ys, nx, ny, i, j, ii, gxs, gys, gnx, gny, m, n, dim, s, k, nc;
46547c6ae99SBarry Smith   PetscInt         ncolors;
46647c6ae99SBarry Smith   MPI_Comm         comm;
467bff4a2f0SMatthew G. Knepley   DMBoundaryType   bx, by;
46847c6ae99SBarry Smith   ISColoringValue *colors;
46947c6ae99SBarry Smith   DM_DA           *dd = (DM_DA *)da->data;
47047c6ae99SBarry Smith 
47147c6ae99SBarry Smith   PetscFunctionBegin;
47247c6ae99SBarry Smith   /*
47347c6ae99SBarry Smith          nc - number of components per grid point
47447c6ae99SBarry Smith          col - number of colors needed in one direction for single component problem
47547c6ae99SBarry Smith   */
4769566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, NULL, NULL, NULL, &nc, &s, &bx, &by, NULL, NULL));
4779566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL));
4789566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL));
4799566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
48047c6ae99SBarry Smith   /* create the coloring */
48147c6ae99SBarry Smith   if (ctype == IS_COLORING_GLOBAL) {
48247c6ae99SBarry Smith     if (!dd->localcoloring) {
4839566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nc * nx * ny, &colors));
48447c6ae99SBarry Smith       ii = 0;
48547c6ae99SBarry Smith       for (j = ys; j < ys + ny; j++) {
48647c6ae99SBarry Smith         for (i = xs; i < xs + nx; i++) {
487ad540459SPierre Jolivet           for (k = 0; k < nc; k++) colors[ii++] = k + nc * ((3 * j + i) % 5);
48847c6ae99SBarry Smith         }
48947c6ae99SBarry Smith       }
49047c6ae99SBarry Smith       ncolors = 5 * nc;
4919566063dSJacob Faibussowitsch       PetscCall(ISColoringCreate(comm, ncolors, nc * nx * ny, colors, PETSC_OWN_POINTER, &dd->localcoloring));
49247c6ae99SBarry Smith     }
49347c6ae99SBarry Smith     *coloring = dd->localcoloring;
4945bdb020cSBarry Smith   } else if (ctype == IS_COLORING_LOCAL) {
49547c6ae99SBarry Smith     if (!dd->ghostedcoloring) {
4969566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nc * gnx * gny, &colors));
49747c6ae99SBarry Smith       ii = 0;
49847c6ae99SBarry Smith       for (j = gys; j < gys + gny; j++) {
49947c6ae99SBarry Smith         for (i = gxs; i < gxs + gnx; i++) {
500ad540459SPierre Jolivet           for (k = 0; k < nc; k++) colors[ii++] = k + nc * ((3 * SetInRange(j, n) + SetInRange(i, m)) % 5);
50147c6ae99SBarry Smith         }
50247c6ae99SBarry Smith       }
50347c6ae99SBarry Smith       ncolors = 5 * nc;
5049566063dSJacob Faibussowitsch       PetscCall(ISColoringCreate(comm, ncolors, nc * gnx * gny, colors, PETSC_OWN_POINTER, &dd->ghostedcoloring));
5059566063dSJacob Faibussowitsch       PetscCall(ISColoringSetType(dd->ghostedcoloring, IS_COLORING_LOCAL));
50647c6ae99SBarry Smith     }
50747c6ae99SBarry Smith     *coloring = dd->ghostedcoloring;
50898921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_ARG_WRONG, "Unknown ISColoringType %d", (int)ctype);
5093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
51047c6ae99SBarry Smith }
51147c6ae99SBarry Smith 
51247c6ae99SBarry Smith /* =========================================================================== */
513e432b41dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_1d_MPIAIJ(DM, Mat);
514ce308e1dSBarry Smith extern PetscErrorCode DMCreateMatrix_DA_1d_MPIAIJ_Fill(DM, Mat);
515e432b41dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_1d_SeqAIJ_NoPreallocation(DM, Mat);
516e432b41dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_2d_MPIAIJ(DM, Mat);
517950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_2d_MPIAIJ_Fill(DM, Mat);
518e432b41dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_3d_MPIAIJ(DM, Mat);
519950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_3d_MPIAIJ_Fill(DM, Mat);
520950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_2d_MPIBAIJ(DM, Mat);
521950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_3d_MPIBAIJ(DM, Mat);
522950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_2d_MPISBAIJ(DM, Mat);
523950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_3d_MPISBAIJ(DM, Mat);
524d4002b98SHong Zhang extern PetscErrorCode DMCreateMatrix_DA_2d_MPISELL(DM, Mat);
525d4002b98SHong Zhang extern PetscErrorCode DMCreateMatrix_DA_3d_MPISELL(DM, Mat);
526e584696dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_IS(DM, Mat);
52747c6ae99SBarry Smith 
5288bbdbebaSMatthew G Knepley /*@C
529dce8aebaSBarry Smith   MatSetupDM - Sets the `DMDA` that is to be used by the HYPRE_StructMatrix PETSc matrix
53047c6ae99SBarry Smith 
53120f4b53cSBarry Smith   Logically Collective
53247c6ae99SBarry Smith 
53347c6ae99SBarry Smith   Input Parameters:
53447c6ae99SBarry Smith + mat - the matrix
53547c6ae99SBarry Smith - da  - the da
53647c6ae99SBarry Smith 
53747c6ae99SBarry Smith   Level: intermediate
53847c6ae99SBarry Smith 
53920f4b53cSBarry Smith .seealso: `DMDA`, `Mat`, `MatSetUp()`
54047c6ae99SBarry Smith @*/
541d71ae5a4SJacob Faibussowitsch PetscErrorCode MatSetupDM(Mat mat, DM da)
542d71ae5a4SJacob Faibussowitsch {
54347c6ae99SBarry Smith   PetscFunctionBegin;
54447c6ae99SBarry Smith   PetscValidHeaderSpecific(mat, MAT_CLASSID, 1);
545064a246eSJacob Faibussowitsch   PetscValidHeaderSpecificType(da, DM_CLASSID, 2, DMDA);
546cac4c232SBarry Smith   PetscTryMethod(mat, "MatSetupDM_C", (Mat, DM), (mat, da));
5473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
54847c6ae99SBarry Smith }
54947c6ae99SBarry Smith 
550*a4e35b19SJacob Faibussowitsch static PetscErrorCode MatView_MPI_DA(Mat A, PetscViewer viewer)
551d71ae5a4SJacob Faibussowitsch {
5529a42bb27SBarry Smith   DM                da;
55347c6ae99SBarry Smith   const char       *prefix;
5542d1451d4SHong Zhang   Mat               AA, Anatural;
55547c6ae99SBarry Smith   AO                ao;
55647c6ae99SBarry Smith   PetscInt          rstart, rend, *petsc, i;
55747c6ae99SBarry Smith   IS                is;
55847c6ae99SBarry Smith   MPI_Comm          comm;
55974388724SJed Brown   PetscViewerFormat format;
5602d1451d4SHong Zhang   PetscBool         flag;
56147c6ae99SBarry Smith 
56247c6ae99SBarry Smith   PetscFunctionBegin;
56374388724SJed Brown   /* Check whether we are just printing info, in which case MatView() already viewed everything we wanted to view */
5649566063dSJacob Faibussowitsch   PetscCall(PetscViewerGetFormat(viewer, &format));
5653ba16761SJacob Faibussowitsch   if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) PetscFunctionReturn(PETSC_SUCCESS);
56674388724SJed Brown 
5679566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)A, &comm));
5689566063dSJacob Faibussowitsch   PetscCall(MatGetDM(A, &da));
5697a8be351SBarry Smith   PetscCheck(da, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Matrix not generated from a DMDA");
57047c6ae99SBarry Smith 
5719566063dSJacob Faibussowitsch   PetscCall(DMDAGetAO(da, &ao));
5729566063dSJacob Faibussowitsch   PetscCall(MatGetOwnershipRange(A, &rstart, &rend));
5739566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(rend - rstart, &petsc));
57447c6ae99SBarry Smith   for (i = rstart; i < rend; i++) petsc[i - rstart] = i;
5759566063dSJacob Faibussowitsch   PetscCall(AOApplicationToPetsc(ao, rend - rstart, petsc));
5769566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(comm, rend - rstart, petsc, PETSC_OWN_POINTER, &is));
57747c6ae99SBarry Smith 
57847c6ae99SBarry Smith   /* call viewer on natural ordering */
5792d1451d4SHong Zhang   PetscCall(PetscObjectBaseTypeCompare((PetscObject)A, MATMPISELL, &flag));
5802d1451d4SHong Zhang   if (flag) {
5812d1451d4SHong Zhang     PetscCall(MatConvert(A, MATAIJ, MAT_INITIAL_MATRIX, &AA));
5822d1451d4SHong Zhang     A = AA;
5832d1451d4SHong Zhang   }
5849566063dSJacob Faibussowitsch   PetscCall(MatCreateSubMatrix(A, is, is, MAT_INITIAL_MATRIX, &Anatural));
5859566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&is));
5869566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)A, &prefix));
5879566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)Anatural, prefix));
5889566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)Anatural, ((PetscObject)A)->name));
589f0ed2f47SStefano Zampini   ((PetscObject)Anatural)->donotPetscObjectPrintClassNamePrefixType = PETSC_TRUE;
5909566063dSJacob Faibussowitsch   PetscCall(MatView(Anatural, viewer));
591f0ed2f47SStefano Zampini   ((PetscObject)Anatural)->donotPetscObjectPrintClassNamePrefixType = PETSC_FALSE;
5929566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&Anatural));
5932d1451d4SHong Zhang   if (flag) PetscCall(MatDestroy(&AA));
5943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
59547c6ae99SBarry Smith }
59647c6ae99SBarry Smith 
597*a4e35b19SJacob Faibussowitsch static PetscErrorCode MatLoad_MPI_DA(Mat A, PetscViewer viewer)
598d71ae5a4SJacob Faibussowitsch {
5999a42bb27SBarry Smith   DM       da;
60047c6ae99SBarry Smith   Mat      Anatural, Aapp;
60147c6ae99SBarry Smith   AO       ao;
602539c167fSBarry Smith   PetscInt rstart, rend, *app, i, m, n, M, N;
60347c6ae99SBarry Smith   IS       is;
60447c6ae99SBarry Smith   MPI_Comm comm;
60547c6ae99SBarry Smith 
60647c6ae99SBarry Smith   PetscFunctionBegin;
6079566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)A, &comm));
6089566063dSJacob Faibussowitsch   PetscCall(MatGetDM(A, &da));
6097a8be351SBarry Smith   PetscCheck(da, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Matrix not generated from a DMDA");
61047c6ae99SBarry Smith 
61147c6ae99SBarry Smith   /* Load the matrix in natural ordering */
6129566063dSJacob Faibussowitsch   PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &Anatural));
6139566063dSJacob Faibussowitsch   PetscCall(MatSetType(Anatural, ((PetscObject)A)->type_name));
6149566063dSJacob Faibussowitsch   PetscCall(MatGetSize(A, &M, &N));
6159566063dSJacob Faibussowitsch   PetscCall(MatGetLocalSize(A, &m, &n));
6169566063dSJacob Faibussowitsch   PetscCall(MatSetSizes(Anatural, m, n, M, N));
6179566063dSJacob Faibussowitsch   PetscCall(MatLoad(Anatural, viewer));
61847c6ae99SBarry Smith 
61947c6ae99SBarry Smith   /* Map natural ordering to application ordering and create IS */
6209566063dSJacob Faibussowitsch   PetscCall(DMDAGetAO(da, &ao));
6219566063dSJacob Faibussowitsch   PetscCall(MatGetOwnershipRange(Anatural, &rstart, &rend));
6229566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(rend - rstart, &app));
62347c6ae99SBarry Smith   for (i = rstart; i < rend; i++) app[i - rstart] = i;
6249566063dSJacob Faibussowitsch   PetscCall(AOPetscToApplication(ao, rend - rstart, app));
6259566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(comm, rend - rstart, app, PETSC_OWN_POINTER, &is));
62647c6ae99SBarry Smith 
62747c6ae99SBarry Smith   /* Do permutation and replace header */
6289566063dSJacob Faibussowitsch   PetscCall(MatCreateSubMatrix(Anatural, is, is, MAT_INITIAL_MATRIX, &Aapp));
6299566063dSJacob Faibussowitsch   PetscCall(MatHeaderReplace(A, &Aapp));
6309566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&is));
6319566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&Anatural));
6323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
63347c6ae99SBarry Smith }
63447c6ae99SBarry Smith 
635d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA(DM da, Mat *J)
636d71ae5a4SJacob Faibussowitsch {
63747c6ae99SBarry Smith   PetscInt    dim, dof, nx, ny, nz, dims[3], starts[3], M, N, P;
63847c6ae99SBarry Smith   Mat         A;
63947c6ae99SBarry Smith   MPI_Comm    comm;
64019fd82e9SBarry Smith   MatType     Atype;
641b412c318SBarry Smith   MatType     mtype;
64247c6ae99SBarry Smith   PetscMPIInt size;
64347c6ae99SBarry Smith   DM_DA      *dd    = (DM_DA *)da->data;
644ade515a3SBarry Smith   void (*aij)(void) = NULL, (*baij)(void) = NULL, (*sbaij)(void) = NULL, (*sell)(void) = NULL, (*is)(void) = NULL;
64547c6ae99SBarry Smith 
64647c6ae99SBarry Smith   PetscFunctionBegin;
6479566063dSJacob Faibussowitsch   PetscCall(MatInitializePackage());
648b412c318SBarry Smith   mtype = da->mattype;
64947c6ae99SBarry Smith 
65047c6ae99SBarry Smith   /*
65147c6ae99SBarry Smith                                   m
65247c6ae99SBarry Smith           ------------------------------------------------------
65347c6ae99SBarry Smith          |                                                     |
65447c6ae99SBarry Smith          |                                                     |
65547c6ae99SBarry Smith          |               ----------------------                |
65647c6ae99SBarry Smith          |               |                    |                |
65747c6ae99SBarry Smith       n  |           ny  |                    |                |
65847c6ae99SBarry Smith          |               |                    |                |
65947c6ae99SBarry Smith          |               .---------------------                |
66047c6ae99SBarry Smith          |             (xs,ys)     nx                          |
66147c6ae99SBarry Smith          |            .                                        |
66247c6ae99SBarry Smith          |         (gxs,gys)                                   |
66347c6ae99SBarry Smith          |                                                     |
66447c6ae99SBarry Smith           -----------------------------------------------------
66547c6ae99SBarry Smith   */
66647c6ae99SBarry Smith 
66747c6ae99SBarry Smith   /*
66847c6ae99SBarry Smith          nc - number of components per grid point
66947c6ae99SBarry Smith          col - number of colors needed in one direction for single component problem
67047c6ae99SBarry Smith   */
671e30e807fSPeter Brune   M   = dd->M;
672e30e807fSPeter Brune   N   = dd->N;
673e30e807fSPeter Brune   P   = dd->P;
674c73cfb54SMatthew G. Knepley   dim = da->dim;
675e30e807fSPeter Brune   dof = dd->w;
6769566063dSJacob Faibussowitsch   /* PetscCall(DMDAGetInfo(da,&dim,&M,&N,&P,NULL,NULL,NULL,&dof,NULL,NULL,NULL,NULL,NULL)); */
6779566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, NULL, NULL, NULL, &nx, &ny, &nz));
6789566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
6799566063dSJacob Faibussowitsch   PetscCall(MatCreate(comm, &A));
6809566063dSJacob Faibussowitsch   PetscCall(MatSetSizes(A, dof * nx * ny * nz, dof * nx * ny * nz, dof * M * N * P, dof * M * N * P));
6819566063dSJacob Faibussowitsch   PetscCall(MatSetType(A, mtype));
6829566063dSJacob Faibussowitsch   PetscCall(MatSetFromOptions(A));
68374427ab1SRichard Tran Mills   if (dof * nx * ny * nz < da->bind_below) {
6849566063dSJacob Faibussowitsch     PetscCall(MatSetBindingPropagates(A, PETSC_TRUE));
6859566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(A, PETSC_TRUE));
68674427ab1SRichard Tran Mills   }
6879566063dSJacob Faibussowitsch   PetscCall(MatSetDM(A, da));
6881baa6e33SBarry Smith   if (da->structure_only) PetscCall(MatSetOption(A, MAT_STRUCTURE_ONLY, PETSC_TRUE));
6899566063dSJacob Faibussowitsch   PetscCall(MatGetType(A, &Atype));
69047c6ae99SBarry Smith   /*
691aa219208SBarry Smith      We do not provide a getmatrix function in the DMDA operations because
692aa219208SBarry Smith    the basic DMDA does not know about matrices. We think of DMDA as being more
69347c6ae99SBarry Smith    more low-level than matrices. This is kind of cheating but, cause sometimes
694aa219208SBarry Smith    we think of DMDA has higher level than matrices.
69547c6ae99SBarry Smith 
69647c6ae99SBarry Smith      We could switch based on Atype (or mtype), but we do not since the
697844bd0d7SStefano Zampini    specialized setting routines depend only on the particular preallocation
69847c6ae99SBarry Smith    details of the matrix, not the type itself.
69947c6ae99SBarry Smith   */
7009566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatMPIAIJSetPreallocation_C", &aij));
70148a46eb9SPierre Jolivet   if (!aij) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatSeqAIJSetPreallocation_C", &aij));
70247c6ae99SBarry Smith   if (!aij) {
7039566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatMPIBAIJSetPreallocation_C", &baij));
70448a46eb9SPierre Jolivet     if (!baij) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatSeqBAIJSetPreallocation_C", &baij));
70547c6ae99SBarry Smith     if (!baij) {
7069566063dSJacob Faibussowitsch       PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatMPISBAIJSetPreallocation_C", &sbaij));
70748a46eb9SPierre Jolivet       if (!sbaij) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatSeqSBAIJSetPreallocation_C", &sbaij));
7085e26d47bSHong Zhang       if (!sbaij) {
7099566063dSJacob Faibussowitsch         PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatMPISELLSetPreallocation_C", &sell));
71048a46eb9SPierre Jolivet         if (!sell) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatSeqSELLSetPreallocation_C", &sell));
7115e26d47bSHong Zhang       }
71248a46eb9SPierre Jolivet       if (!sell) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatISSetPreallocation_C", &is));
71347c6ae99SBarry Smith     }
71447c6ae99SBarry Smith   }
71547c6ae99SBarry Smith   if (aij) {
71647c6ae99SBarry Smith     if (dim == 1) {
717ce308e1dSBarry Smith       if (dd->ofill) {
7189566063dSJacob Faibussowitsch         PetscCall(DMCreateMatrix_DA_1d_MPIAIJ_Fill(da, A));
719ce308e1dSBarry Smith       } else {
72019b08ed1SBarry Smith         DMBoundaryType bx;
72119b08ed1SBarry Smith         PetscMPIInt    size;
7229566063dSJacob Faibussowitsch         PetscCall(DMDAGetInfo(da, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &bx, NULL, NULL, NULL));
7239566063dSJacob Faibussowitsch         PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)da), &size));
72419b08ed1SBarry Smith         if (size == 1 && bx == DM_BOUNDARY_NONE) {
7259566063dSJacob Faibussowitsch           PetscCall(DMCreateMatrix_DA_1d_SeqAIJ_NoPreallocation(da, A));
72619b08ed1SBarry Smith         } else {
7279566063dSJacob Faibussowitsch           PetscCall(DMCreateMatrix_DA_1d_MPIAIJ(da, A));
728ce308e1dSBarry Smith         }
72919b08ed1SBarry Smith       }
73047c6ae99SBarry Smith     } else if (dim == 2) {
73147c6ae99SBarry Smith       if (dd->ofill) {
7329566063dSJacob Faibussowitsch         PetscCall(DMCreateMatrix_DA_2d_MPIAIJ_Fill(da, A));
73347c6ae99SBarry Smith       } else {
7349566063dSJacob Faibussowitsch         PetscCall(DMCreateMatrix_DA_2d_MPIAIJ(da, A));
73547c6ae99SBarry Smith       }
73647c6ae99SBarry Smith     } else if (dim == 3) {
73747c6ae99SBarry Smith       if (dd->ofill) {
7389566063dSJacob Faibussowitsch         PetscCall(DMCreateMatrix_DA_3d_MPIAIJ_Fill(da, A));
73947c6ae99SBarry Smith       } else {
7409566063dSJacob Faibussowitsch         PetscCall(DMCreateMatrix_DA_3d_MPIAIJ(da, A));
74147c6ae99SBarry Smith       }
74247c6ae99SBarry Smith     }
74347c6ae99SBarry Smith   } else if (baij) {
74447c6ae99SBarry Smith     if (dim == 2) {
7459566063dSJacob Faibussowitsch       PetscCall(DMCreateMatrix_DA_2d_MPIBAIJ(da, A));
74647c6ae99SBarry Smith     } else if (dim == 3) {
7479566063dSJacob Faibussowitsch       PetscCall(DMCreateMatrix_DA_3d_MPIBAIJ(da, A));
74863a3b9bcSJacob 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);
74947c6ae99SBarry Smith   } else if (sbaij) {
75047c6ae99SBarry Smith     if (dim == 2) {
7519566063dSJacob Faibussowitsch       PetscCall(DMCreateMatrix_DA_2d_MPISBAIJ(da, A));
75247c6ae99SBarry Smith     } else if (dim == 3) {
7539566063dSJacob Faibussowitsch       PetscCall(DMCreateMatrix_DA_3d_MPISBAIJ(da, A));
75463a3b9bcSJacob 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);
755d4002b98SHong Zhang   } else if (sell) {
7565e26d47bSHong Zhang     if (dim == 2) {
7579566063dSJacob Faibussowitsch       PetscCall(DMCreateMatrix_DA_2d_MPISELL(da, A));
758711261dbSHong Zhang     } else if (dim == 3) {
7599566063dSJacob Faibussowitsch       PetscCall(DMCreateMatrix_DA_3d_MPISELL(da, A));
76063a3b9bcSJacob 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);
761e584696dSStefano Zampini   } else if (is) {
7629566063dSJacob Faibussowitsch     PetscCall(DMCreateMatrix_DA_IS(da, A));
763869776cdSLisandro Dalcin   } else {
76445b6f7e9SBarry Smith     ISLocalToGlobalMapping ltog;
765e584696dSStefano Zampini 
7669566063dSJacob Faibussowitsch     PetscCall(MatSetBlockSize(A, dof));
7679566063dSJacob Faibussowitsch     PetscCall(MatSetUp(A));
7689566063dSJacob Faibussowitsch     PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
7699566063dSJacob Faibussowitsch     PetscCall(MatSetLocalToGlobalMapping(A, ltog, ltog));
77047c6ae99SBarry Smith   }
7719566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &starts[0], &starts[1], &starts[2], &dims[0], &dims[1], &dims[2]));
7729566063dSJacob Faibussowitsch   PetscCall(MatSetStencil(A, dim, dims, starts, dof));
7739566063dSJacob Faibussowitsch   PetscCall(MatSetDM(A, da));
7749566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
77547c6ae99SBarry Smith   if (size > 1) {
77647c6ae99SBarry Smith     /* change viewer to display matrix in natural ordering */
7779566063dSJacob Faibussowitsch     PetscCall(MatSetOperation(A, MATOP_VIEW, (void (*)(void))MatView_MPI_DA));
7789566063dSJacob Faibussowitsch     PetscCall(MatSetOperation(A, MATOP_LOAD, (void (*)(void))MatLoad_MPI_DA));
77947c6ae99SBarry Smith   }
78047c6ae99SBarry Smith   *J = A;
7813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
78247c6ae99SBarry Smith }
78347c6ae99SBarry Smith 
784844bd0d7SStefano Zampini PETSC_EXTERN PetscErrorCode MatISSetPreallocation_IS(Mat, PetscInt, const PetscInt[], PetscInt, const PetscInt[]);
785844bd0d7SStefano Zampini 
786d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_IS(DM dm, Mat J)
787d71ae5a4SJacob Faibussowitsch {
788e584696dSStefano Zampini   DM_DA                 *da = (DM_DA *)dm->data;
789e432b41dSStefano Zampini   Mat                    lJ, P;
790e584696dSStefano Zampini   ISLocalToGlobalMapping ltog;
791e432b41dSStefano Zampini   IS                     is;
792e432b41dSStefano Zampini   PetscBT                bt;
79305339c03SStefano Zampini   const PetscInt        *e_loc, *idx;
794e432b41dSStefano Zampini   PetscInt               i, nel, nen, nv, dof, *gidx, n, N;
795e584696dSStefano Zampini 
796e584696dSStefano Zampini   /* The l2g map of DMDA has all ghosted nodes, and e_loc is a subset of all the local nodes (including the ghosted)
797e432b41dSStefano Zampini      We need to filter out the local indices that are not represented through the DMDAGetElements decomposition */
798e584696dSStefano Zampini   PetscFunctionBegin;
799e584696dSStefano Zampini   dof = da->w;
8009566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, dof));
8019566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(dm, &ltog));
802e432b41dSStefano Zampini 
803e432b41dSStefano Zampini   /* flag local elements indices in local DMDA numbering */
8049566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingGetSize(ltog, &nv));
8059566063dSJacob Faibussowitsch   PetscCall(PetscBTCreate(nv / dof, &bt));
8069566063dSJacob Faibussowitsch   PetscCall(DMDAGetElements(dm, &nel, &nen, &e_loc)); /* this will throw an error if the stencil type is not DMDA_STENCIL_BOX */
8079566063dSJacob Faibussowitsch   for (i = 0; i < nel * nen; i++) PetscCall(PetscBTSet(bt, e_loc[i]));
808e432b41dSStefano Zampini 
809e432b41dSStefano Zampini   /* filter out (set to -1) the global indices not used by the local elements */
8109566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(nv / dof, &gidx));
8119566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingGetBlockIndices(ltog, &idx));
8129566063dSJacob Faibussowitsch   PetscCall(PetscArraycpy(gidx, idx, nv / dof));
8139566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingRestoreBlockIndices(ltog, &idx));
8149371c9d4SSatish Balay   for (i = 0; i < nv / dof; i++)
8159371c9d4SSatish Balay     if (!PetscBTLookup(bt, i)) gidx[i] = -1;
8169566063dSJacob Faibussowitsch   PetscCall(PetscBTDestroy(&bt));
8179566063dSJacob Faibussowitsch   PetscCall(ISCreateBlock(PetscObjectComm((PetscObject)dm), dof, nv / dof, gidx, PETSC_OWN_POINTER, &is));
8189566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingCreateIS(is, &ltog));
8199566063dSJacob Faibussowitsch   PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
8209566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingDestroy(&ltog));
8219566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&is));
82205339c03SStefano Zampini 
823e432b41dSStefano Zampini   /* Preallocation */
824e306f867SJed Brown   if (dm->prealloc_skip) {
8259566063dSJacob Faibussowitsch     PetscCall(MatSetUp(J));
826e306f867SJed Brown   } else {
8279566063dSJacob Faibussowitsch     PetscCall(MatISGetLocalMat(J, &lJ));
8289566063dSJacob Faibussowitsch     PetscCall(MatGetLocalToGlobalMapping(lJ, &ltog, NULL));
8299566063dSJacob Faibussowitsch     PetscCall(MatCreate(PetscObjectComm((PetscObject)lJ), &P));
8309566063dSJacob Faibussowitsch     PetscCall(MatSetType(P, MATPREALLOCATOR));
8319566063dSJacob Faibussowitsch     PetscCall(MatSetLocalToGlobalMapping(P, ltog, ltog));
8329566063dSJacob Faibussowitsch     PetscCall(MatGetSize(lJ, &N, NULL));
8339566063dSJacob Faibussowitsch     PetscCall(MatGetLocalSize(lJ, &n, NULL));
8349566063dSJacob Faibussowitsch     PetscCall(MatSetSizes(P, n, n, N, N));
8359566063dSJacob Faibussowitsch     PetscCall(MatSetBlockSize(P, dof));
8369566063dSJacob Faibussowitsch     PetscCall(MatSetUp(P));
83748a46eb9SPierre Jolivet     for (i = 0; i < nel; i++) PetscCall(MatSetValuesBlockedLocal(P, nen, e_loc + i * nen, nen, e_loc + i * nen, NULL, INSERT_VALUES));
8389566063dSJacob Faibussowitsch     PetscCall(MatPreallocatorPreallocate(P, (PetscBool)!da->prealloc_only, lJ));
8399566063dSJacob Faibussowitsch     PetscCall(MatISRestoreLocalMat(J, &lJ));
8409566063dSJacob Faibussowitsch     PetscCall(DMDARestoreElements(dm, &nel, &nen, &e_loc));
8419566063dSJacob Faibussowitsch     PetscCall(MatDestroy(&P));
842e432b41dSStefano Zampini 
8439566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
8449566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
845e306f867SJed Brown   }
8463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
847e584696dSStefano Zampini }
848e584696dSStefano Zampini 
849d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPISELL(DM da, Mat J)
850d71ae5a4SJacob Faibussowitsch {
8515e26d47bSHong 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;
8525e26d47bSHong Zhang   PetscInt               lstart, lend, pstart, pend, *dnz, *onz;
8535e26d47bSHong Zhang   MPI_Comm               comm;
8545e26d47bSHong Zhang   PetscScalar           *values;
8555e26d47bSHong Zhang   DMBoundaryType         bx, by;
8565e26d47bSHong Zhang   ISLocalToGlobalMapping ltog;
8575e26d47bSHong Zhang   DMDAStencilType        st;
8585e26d47bSHong Zhang 
8595e26d47bSHong Zhang   PetscFunctionBegin;
8605e26d47bSHong Zhang   /*
8615e26d47bSHong Zhang          nc - number of components per grid point
8625e26d47bSHong Zhang          col - number of colors needed in one direction for single component problem
8635e26d47bSHong Zhang   */
8649566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, NULL, NULL, NULL, &nc, &s, &bx, &by, NULL, &st));
8655e26d47bSHong Zhang   col = 2 * s + 1;
8669566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL));
8679566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL));
8689566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
8695e26d47bSHong Zhang 
8709566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(nc, &rows, col * col * nc * nc, &cols));
8719566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
8725e26d47bSHong Zhang 
8739566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, nc));
8745e26d47bSHong Zhang   /* determine the matrix preallocation information */
875d0609cedSBarry Smith   MatPreallocateBegin(comm, nc * nx * ny, nc * nx * ny, dnz, onz);
8765e26d47bSHong Zhang   for (i = xs; i < xs + nx; i++) {
8775e26d47bSHong Zhang     pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
8785e26d47bSHong Zhang     pend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
8795e26d47bSHong Zhang 
8805e26d47bSHong Zhang     for (j = ys; j < ys + ny; j++) {
8815e26d47bSHong Zhang       slot = i - gxs + gnx * (j - gys);
8825e26d47bSHong Zhang 
8835e26d47bSHong Zhang       lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
8845e26d47bSHong Zhang       lend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
8855e26d47bSHong Zhang 
8865e26d47bSHong Zhang       cnt = 0;
8875e26d47bSHong Zhang       for (k = 0; k < nc; k++) {
8885e26d47bSHong Zhang         for (l = lstart; l < lend + 1; l++) {
8895e26d47bSHong Zhang           for (p = pstart; p < pend + 1; p++) {
8905e26d47bSHong Zhang             if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star have either l = 0 or p = 0 */
8915e26d47bSHong Zhang               cols[cnt++] = k + nc * (slot + gnx * l + p);
8925e26d47bSHong Zhang             }
8935e26d47bSHong Zhang           }
8945e26d47bSHong Zhang         }
8955e26d47bSHong Zhang         rows[k] = k + nc * (slot);
8965e26d47bSHong Zhang       }
8979566063dSJacob Faibussowitsch       PetscCall(MatPreallocateSetLocal(ltog, nc, rows, ltog, cnt, cols, dnz, onz));
8985e26d47bSHong Zhang     }
8995e26d47bSHong Zhang   }
9009566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, nc));
9019566063dSJacob Faibussowitsch   PetscCall(MatSeqSELLSetPreallocation(J, 0, dnz));
9029566063dSJacob Faibussowitsch   PetscCall(MatMPISELLSetPreallocation(J, 0, dnz, 0, onz));
903d0609cedSBarry Smith   MatPreallocateEnd(dnz, onz);
9045e26d47bSHong Zhang 
9059566063dSJacob Faibussowitsch   PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
9065e26d47bSHong Zhang 
9075e26d47bSHong Zhang   /*
9085e26d47bSHong Zhang     For each node in the grid: we get the neighbors in the local (on processor ordering
9095e26d47bSHong Zhang     that includes the ghost points) then MatSetValuesLocal() maps those indices to the global
9105e26d47bSHong Zhang     PETSc ordering.
9115e26d47bSHong Zhang   */
9125e26d47bSHong Zhang   if (!da->prealloc_only) {
9139566063dSJacob Faibussowitsch     PetscCall(PetscCalloc1(col * col * nc * nc, &values));
9145e26d47bSHong Zhang     for (i = xs; i < xs + nx; i++) {
9155e26d47bSHong Zhang       pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
9165e26d47bSHong Zhang       pend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
9175e26d47bSHong Zhang 
9185e26d47bSHong Zhang       for (j = ys; j < ys + ny; j++) {
9195e26d47bSHong Zhang         slot = i - gxs + gnx * (j - gys);
9205e26d47bSHong Zhang 
9215e26d47bSHong Zhang         lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
9225e26d47bSHong Zhang         lend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
9235e26d47bSHong Zhang 
9245e26d47bSHong Zhang         cnt = 0;
9255e26d47bSHong Zhang         for (k = 0; k < nc; k++) {
9265e26d47bSHong Zhang           for (l = lstart; l < lend + 1; l++) {
9275e26d47bSHong Zhang             for (p = pstart; p < pend + 1; p++) {
9285e26d47bSHong Zhang               if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star have either l = 0 or p = 0 */
9295e26d47bSHong Zhang                 cols[cnt++] = k + nc * (slot + gnx * l + p);
9305e26d47bSHong Zhang               }
9315e26d47bSHong Zhang             }
9325e26d47bSHong Zhang           }
9335e26d47bSHong Zhang           rows[k] = k + nc * (slot);
9345e26d47bSHong Zhang         }
9359566063dSJacob Faibussowitsch         PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, values, INSERT_VALUES));
9365e26d47bSHong Zhang       }
9375e26d47bSHong Zhang     }
9389566063dSJacob Faibussowitsch     PetscCall(PetscFree(values));
939e7e92044SBarry Smith     /* do not copy values to GPU since they are all zero and not yet needed there */
9409566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_TRUE));
9419566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
9429566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
9439566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_FALSE));
9449566063dSJacob Faibussowitsch     PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
9455e26d47bSHong Zhang   }
9469566063dSJacob Faibussowitsch   PetscCall(PetscFree2(rows, cols));
9473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9485e26d47bSHong Zhang }
9495e26d47bSHong Zhang 
950d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPISELL(DM da, Mat J)
951d71ae5a4SJacob Faibussowitsch {
952711261dbSHong Zhang   PetscInt               xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny;
953711261dbSHong Zhang   PetscInt               m, n, dim, s, *cols = NULL, k, nc, *rows = NULL, col, cnt, l, p, *dnz = NULL, *onz = NULL;
954711261dbSHong Zhang   PetscInt               istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk, M, N, P;
955711261dbSHong Zhang   MPI_Comm               comm;
956711261dbSHong Zhang   PetscScalar           *values;
957711261dbSHong Zhang   DMBoundaryType         bx, by, bz;
958711261dbSHong Zhang   ISLocalToGlobalMapping ltog;
959711261dbSHong Zhang   DMDAStencilType        st;
960711261dbSHong Zhang 
961711261dbSHong Zhang   PetscFunctionBegin;
962711261dbSHong Zhang   /*
963711261dbSHong Zhang          nc - number of components per grid point
964711261dbSHong Zhang          col - number of colors needed in one direction for single component problem
965711261dbSHong Zhang   */
9669566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &nc, &s, &bx, &by, &bz, &st));
967711261dbSHong Zhang   col = 2 * s + 1;
9689566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz));
9699566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz));
9709566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
971711261dbSHong Zhang 
9729566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(nc, &rows, col * col * col * nc * nc, &cols));
9739566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
974711261dbSHong Zhang 
9759566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, nc));
976711261dbSHong Zhang   /* determine the matrix preallocation information */
977d0609cedSBarry Smith   MatPreallocateBegin(comm, nc * nx * ny * nz, nc * nx * ny * nz, dnz, onz);
978711261dbSHong Zhang   for (i = xs; i < xs + nx; i++) {
979711261dbSHong Zhang     istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
980711261dbSHong Zhang     iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
981711261dbSHong Zhang     for (j = ys; j < ys + ny; j++) {
982711261dbSHong Zhang       jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
983711261dbSHong Zhang       jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
984711261dbSHong Zhang       for (k = zs; k < zs + nz; k++) {
985711261dbSHong Zhang         kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k));
986711261dbSHong Zhang         kend   = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1));
987711261dbSHong Zhang 
988711261dbSHong Zhang         slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs);
989711261dbSHong Zhang 
990711261dbSHong Zhang         cnt = 0;
991711261dbSHong Zhang         for (l = 0; l < nc; l++) {
992711261dbSHong Zhang           for (ii = istart; ii < iend + 1; ii++) {
993711261dbSHong Zhang             for (jj = jstart; jj < jend + 1; jj++) {
994711261dbSHong Zhang               for (kk = kstart; kk < kend + 1; kk++) {
995711261dbSHong Zhang                 if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/
996711261dbSHong Zhang                   cols[cnt++] = l + nc * (slot + ii + gnx * jj + gnx * gny * kk);
997711261dbSHong Zhang                 }
998711261dbSHong Zhang               }
999711261dbSHong Zhang             }
1000711261dbSHong Zhang           }
1001711261dbSHong Zhang           rows[l] = l + nc * (slot);
1002711261dbSHong Zhang         }
10039566063dSJacob Faibussowitsch         PetscCall(MatPreallocateSetLocal(ltog, nc, rows, ltog, cnt, cols, dnz, onz));
1004711261dbSHong Zhang       }
1005711261dbSHong Zhang     }
1006711261dbSHong Zhang   }
10079566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, nc));
10089566063dSJacob Faibussowitsch   PetscCall(MatSeqSELLSetPreallocation(J, 0, dnz));
10099566063dSJacob Faibussowitsch   PetscCall(MatMPISELLSetPreallocation(J, 0, dnz, 0, onz));
1010d0609cedSBarry Smith   MatPreallocateEnd(dnz, onz);
10119566063dSJacob Faibussowitsch   PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
1012711261dbSHong Zhang 
1013711261dbSHong Zhang   /*
1014711261dbSHong Zhang     For each node in the grid: we get the neighbors in the local (on processor ordering
1015711261dbSHong Zhang     that includes the ghost points) then MatSetValuesLocal() maps those indices to the global
1016711261dbSHong Zhang     PETSc ordering.
1017711261dbSHong Zhang   */
1018711261dbSHong Zhang   if (!da->prealloc_only) {
10199566063dSJacob Faibussowitsch     PetscCall(PetscCalloc1(col * col * col * nc * nc * nc, &values));
1020711261dbSHong Zhang     for (i = xs; i < xs + nx; i++) {
1021711261dbSHong Zhang       istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
1022711261dbSHong Zhang       iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
1023711261dbSHong Zhang       for (j = ys; j < ys + ny; j++) {
1024711261dbSHong Zhang         jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
1025711261dbSHong Zhang         jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
1026711261dbSHong Zhang         for (k = zs; k < zs + nz; k++) {
1027711261dbSHong Zhang           kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k));
1028711261dbSHong Zhang           kend   = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1));
1029711261dbSHong Zhang 
1030711261dbSHong Zhang           slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs);
1031711261dbSHong Zhang 
1032711261dbSHong Zhang           cnt = 0;
1033711261dbSHong Zhang           for (l = 0; l < nc; l++) {
1034711261dbSHong Zhang             for (ii = istart; ii < iend + 1; ii++) {
1035711261dbSHong Zhang               for (jj = jstart; jj < jend + 1; jj++) {
1036711261dbSHong Zhang                 for (kk = kstart; kk < kend + 1; kk++) {
1037711261dbSHong Zhang                   if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/
1038711261dbSHong Zhang                     cols[cnt++] = l + nc * (slot + ii + gnx * jj + gnx * gny * kk);
1039711261dbSHong Zhang                   }
1040711261dbSHong Zhang                 }
1041711261dbSHong Zhang               }
1042711261dbSHong Zhang             }
1043711261dbSHong Zhang             rows[l] = l + nc * (slot);
1044711261dbSHong Zhang           }
10459566063dSJacob Faibussowitsch           PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, values, INSERT_VALUES));
1046711261dbSHong Zhang         }
1047711261dbSHong Zhang       }
1048711261dbSHong Zhang     }
10499566063dSJacob Faibussowitsch     PetscCall(PetscFree(values));
1050e7e92044SBarry Smith     /* do not copy values to GPU since they are all zero and not yet needed there */
10519566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_TRUE));
10529566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
10539566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
10549566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_FALSE));
10559566063dSJacob Faibussowitsch     PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
1056711261dbSHong Zhang   }
10579566063dSJacob Faibussowitsch   PetscCall(PetscFree2(rows, cols));
10583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1059711261dbSHong Zhang }
1060711261dbSHong Zhang 
1061d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPIAIJ(DM da, Mat J)
1062d71ae5a4SJacob Faibussowitsch {
1063c1154cd5SBarry 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;
106447c6ae99SBarry Smith   PetscInt               lstart, lend, pstart, pend, *dnz, *onz;
106547c6ae99SBarry Smith   MPI_Comm               comm;
1066bff4a2f0SMatthew G. Knepley   DMBoundaryType         bx, by;
1067844bd0d7SStefano Zampini   ISLocalToGlobalMapping ltog, mltog;
1068aa219208SBarry Smith   DMDAStencilType        st;
1069b294de21SRichard Tran Mills   PetscBool              removedups = PETSC_FALSE, alreadyboundtocpu = PETSC_TRUE;
107047c6ae99SBarry Smith 
107147c6ae99SBarry Smith   PetscFunctionBegin;
107247c6ae99SBarry Smith   /*
107347c6ae99SBarry Smith          nc - number of components per grid point
107447c6ae99SBarry Smith          col - number of colors needed in one direction for single component problem
107547c6ae99SBarry Smith   */
1076924e958dSJunchao Zhang   PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, &M, &N, NULL, &nc, &s, &bx, &by, NULL, &st));
10771baa6e33SBarry Smith   if (bx == DM_BOUNDARY_NONE && by == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_TRUE));
107847c6ae99SBarry Smith   col = 2 * s + 1;
1079c1154cd5SBarry Smith   /*
1080c1154cd5SBarry Smith        With one processor in periodic domains in a skinny dimension the code will label nonzero columns multiple times
1081c1154cd5SBarry Smith        because of "wrapping" around the end of the domain hitting an entry already counted in the other direction.
1082c1154cd5SBarry Smith   */
1083c1154cd5SBarry Smith   if (M == 1 && 2 * s >= m) removedups = PETSC_TRUE;
1084c1154cd5SBarry Smith   if (N == 1 && 2 * s >= n) removedups = PETSC_TRUE;
10859566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL));
10869566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL));
10879566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
108847c6ae99SBarry Smith 
10899566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(nc, &rows, col * col * nc * nc, &cols));
10909566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
109147c6ae99SBarry Smith 
10929566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, nc));
109347c6ae99SBarry Smith   /* determine the matrix preallocation information */
1094d0609cedSBarry Smith   MatPreallocateBegin(comm, nc * nx * ny, nc * nx * ny, dnz, onz);
109547c6ae99SBarry Smith   for (i = xs; i < xs + nx; i++) {
1096bff4a2f0SMatthew G. Knepley     pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
1097bff4a2f0SMatthew G. Knepley     pend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
109847c6ae99SBarry Smith 
109947c6ae99SBarry Smith     for (j = ys; j < ys + ny; j++) {
110047c6ae99SBarry Smith       slot = i - gxs + gnx * (j - gys);
110147c6ae99SBarry Smith 
1102bff4a2f0SMatthew G. Knepley       lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
1103bff4a2f0SMatthew G. Knepley       lend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
110447c6ae99SBarry Smith 
110547c6ae99SBarry Smith       cnt = 0;
110647c6ae99SBarry Smith       for (k = 0; k < nc; k++) {
110747c6ae99SBarry Smith         for (l = lstart; l < lend + 1; l++) {
110847c6ae99SBarry Smith           for (p = pstart; p < pend + 1; p++) {
1109aa219208SBarry Smith             if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star have either l = 0 or p = 0 */
111047c6ae99SBarry Smith               cols[cnt++] = k + nc * (slot + gnx * l + p);
111147c6ae99SBarry Smith             }
111247c6ae99SBarry Smith           }
111347c6ae99SBarry Smith         }
111447c6ae99SBarry Smith         rows[k] = k + nc * (slot);
111547c6ae99SBarry Smith       }
11161baa6e33SBarry Smith       if (removedups) PetscCall(MatPreallocateSetLocalRemoveDups(ltog, nc, rows, ltog, cnt, cols, dnz, onz));
11171baa6e33SBarry Smith       else PetscCall(MatPreallocateSetLocal(ltog, nc, rows, ltog, cnt, cols, dnz, onz));
111847c6ae99SBarry Smith     }
1119c1154cd5SBarry Smith   }
11209566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, nc));
11219566063dSJacob Faibussowitsch   PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz));
11229566063dSJacob Faibussowitsch   PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz));
1123d0609cedSBarry Smith   MatPreallocateEnd(dnz, onz);
11249566063dSJacob Faibussowitsch   PetscCall(MatGetLocalToGlobalMapping(J, &mltog, NULL));
11251baa6e33SBarry Smith   if (!mltog) PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
112647c6ae99SBarry Smith 
112747c6ae99SBarry Smith   /*
112847c6ae99SBarry Smith     For each node in the grid: we get the neighbors in the local (on processor ordering
112947c6ae99SBarry Smith     that includes the ghost points) then MatSetValuesLocal() maps those indices to the global
113047c6ae99SBarry Smith     PETSc ordering.
113147c6ae99SBarry Smith   */
1132fcfd50ebSBarry Smith   if (!da->prealloc_only) {
113347c6ae99SBarry Smith     for (i = xs; i < xs + nx; i++) {
1134bff4a2f0SMatthew G. Knepley       pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
1135bff4a2f0SMatthew G. Knepley       pend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
113647c6ae99SBarry Smith 
113747c6ae99SBarry Smith       for (j = ys; j < ys + ny; j++) {
113847c6ae99SBarry Smith         slot = i - gxs + gnx * (j - gys);
113947c6ae99SBarry Smith 
1140bff4a2f0SMatthew G. Knepley         lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
1141bff4a2f0SMatthew G. Knepley         lend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
114247c6ae99SBarry Smith 
114347c6ae99SBarry Smith         cnt = 0;
114447c6ae99SBarry Smith         for (l = lstart; l < lend + 1; l++) {
114547c6ae99SBarry Smith           for (p = pstart; p < pend + 1; p++) {
1146aa219208SBarry Smith             if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star have either l = 0 or p = 0 */
1147071fcb05SBarry Smith               cols[cnt++] = nc * (slot + gnx * l + p);
1148071fcb05SBarry Smith               for (k = 1; k < nc; k++) {
11499371c9d4SSatish Balay                 cols[cnt] = 1 + cols[cnt - 1];
11509371c9d4SSatish Balay                 cnt++;
115147c6ae99SBarry Smith               }
115247c6ae99SBarry Smith             }
115347c6ae99SBarry Smith           }
115447c6ae99SBarry Smith         }
1155071fcb05SBarry Smith         for (k = 0; k < nc; k++) rows[k] = k + nc * (slot);
11569566063dSJacob Faibussowitsch         PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, NULL, INSERT_VALUES));
115747c6ae99SBarry Smith       }
115847c6ae99SBarry Smith     }
1159e7e92044SBarry Smith     /* do not copy values to GPU since they are all zero and not yet needed there */
11609566063dSJacob Faibussowitsch     PetscCall(MatBoundToCPU(J, &alreadyboundtocpu));
11619566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_TRUE));
11629566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
11639566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
11649566063dSJacob Faibussowitsch     if (!alreadyboundtocpu) PetscCall(MatBindToCPU(J, PETSC_FALSE));
11659566063dSJacob Faibussowitsch     PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
11661baa6e33SBarry Smith     if (bx == DM_BOUNDARY_NONE && by == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE));
116747c6ae99SBarry Smith   }
11689566063dSJacob Faibussowitsch   PetscCall(PetscFree2(rows, cols));
11693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
117047c6ae99SBarry Smith }
117147c6ae99SBarry Smith 
1172d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPIAIJ_Fill(DM da, Mat J)
1173d71ae5a4SJacob Faibussowitsch {
117447c6ae99SBarry Smith   PetscInt               xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny;
1175c1154cd5SBarry Smith   PetscInt               m, n, dim, s, *cols, k, nc, row, col, cnt, maxcnt = 0, l, p, M, N;
117647c6ae99SBarry Smith   PetscInt               lstart, lend, pstart, pend, *dnz, *onz;
117747c6ae99SBarry Smith   DM_DA                 *dd = (DM_DA *)da->data;
117847c6ae99SBarry Smith   PetscInt               ifill_col, *ofill = dd->ofill, *dfill = dd->dfill;
117947c6ae99SBarry Smith   MPI_Comm               comm;
1180bff4a2f0SMatthew G. Knepley   DMBoundaryType         bx, by;
118145b6f7e9SBarry Smith   ISLocalToGlobalMapping ltog;
1182aa219208SBarry Smith   DMDAStencilType        st;
1183c1154cd5SBarry Smith   PetscBool              removedups = PETSC_FALSE;
118447c6ae99SBarry Smith 
118547c6ae99SBarry Smith   PetscFunctionBegin;
118647c6ae99SBarry Smith   /*
118747c6ae99SBarry Smith          nc - number of components per grid point
118847c6ae99SBarry Smith          col - number of colors needed in one direction for single component problem
118947c6ae99SBarry Smith   */
1190924e958dSJunchao Zhang   PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, &M, &N, NULL, &nc, &s, &bx, &by, NULL, &st));
119147c6ae99SBarry Smith   col = 2 * s + 1;
1192c1154cd5SBarry Smith   /*
1193c1154cd5SBarry Smith        With one processor in periodic domains in a skinny dimension the code will label nonzero columns multiple times
1194c1154cd5SBarry Smith        because of "wrapping" around the end of the domain hitting an entry already counted in the other direction.
1195c1154cd5SBarry Smith   */
1196c1154cd5SBarry Smith   if (M == 1 && 2 * s >= m) removedups = PETSC_TRUE;
1197c1154cd5SBarry Smith   if (N == 1 && 2 * s >= n) removedups = PETSC_TRUE;
11989566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL));
11999566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL));
12009566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
120147c6ae99SBarry Smith 
12029566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(col * col * nc, &cols));
12039566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
120447c6ae99SBarry Smith 
12059566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, nc));
120647c6ae99SBarry Smith   /* determine the matrix preallocation information */
1207d0609cedSBarry Smith   MatPreallocateBegin(comm, nc * nx * ny, nc * nx * ny, dnz, onz);
120847c6ae99SBarry Smith   for (i = xs; i < xs + nx; i++) {
1209bff4a2f0SMatthew G. Knepley     pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
1210bff4a2f0SMatthew G. Knepley     pend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
121147c6ae99SBarry Smith 
121247c6ae99SBarry Smith     for (j = ys; j < ys + ny; j++) {
121347c6ae99SBarry Smith       slot = i - gxs + gnx * (j - gys);
121447c6ae99SBarry Smith 
1215bff4a2f0SMatthew G. Knepley       lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
1216bff4a2f0SMatthew G. Knepley       lend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
121747c6ae99SBarry Smith 
121847c6ae99SBarry Smith       for (k = 0; k < nc; k++) {
121947c6ae99SBarry Smith         cnt = 0;
122047c6ae99SBarry Smith         for (l = lstart; l < lend + 1; l++) {
122147c6ae99SBarry Smith           for (p = pstart; p < pend + 1; p++) {
122247c6ae99SBarry Smith             if (l || p) {
1223aa219208SBarry Smith               if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star */
12248865f1eaSKarl Rupp                 for (ifill_col = ofill[k]; ifill_col < ofill[k + 1]; ifill_col++) cols[cnt++] = ofill[ifill_col] + nc * (slot + gnx * l + p);
122547c6ae99SBarry Smith               }
122647c6ae99SBarry Smith             } else {
122747c6ae99SBarry Smith               if (dfill) {
12288865f1eaSKarl Rupp                 for (ifill_col = dfill[k]; ifill_col < dfill[k + 1]; ifill_col++) cols[cnt++] = dfill[ifill_col] + nc * (slot + gnx * l + p);
122947c6ae99SBarry Smith               } else {
12308865f1eaSKarl Rupp                 for (ifill_col = 0; ifill_col < nc; ifill_col++) cols[cnt++] = ifill_col + nc * (slot + gnx * l + p);
123147c6ae99SBarry Smith               }
123247c6ae99SBarry Smith             }
123347c6ae99SBarry Smith           }
123447c6ae99SBarry Smith         }
123547c6ae99SBarry Smith         row    = k + nc * (slot);
1236c0ab637bSBarry Smith         maxcnt = PetscMax(maxcnt, cnt);
12371baa6e33SBarry Smith         if (removedups) PetscCall(MatPreallocateSetLocalRemoveDups(ltog, 1, &row, ltog, cnt, cols, dnz, onz));
12381baa6e33SBarry Smith         else PetscCall(MatPreallocateSetLocal(ltog, 1, &row, ltog, cnt, cols, dnz, onz));
123947c6ae99SBarry Smith       }
124047c6ae99SBarry Smith     }
1241c1154cd5SBarry Smith   }
12429566063dSJacob Faibussowitsch   PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz));
12439566063dSJacob Faibussowitsch   PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz));
1244d0609cedSBarry Smith   MatPreallocateEnd(dnz, onz);
12459566063dSJacob Faibussowitsch   PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
124647c6ae99SBarry Smith 
124747c6ae99SBarry Smith   /*
124847c6ae99SBarry Smith     For each node in the grid: we get the neighbors in the local (on processor ordering
124947c6ae99SBarry Smith     that includes the ghost points) then MatSetValuesLocal() maps those indices to the global
125047c6ae99SBarry Smith     PETSc ordering.
125147c6ae99SBarry Smith   */
1252fcfd50ebSBarry Smith   if (!da->prealloc_only) {
125347c6ae99SBarry Smith     for (i = xs; i < xs + nx; i++) {
1254bff4a2f0SMatthew G. Knepley       pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
1255bff4a2f0SMatthew G. Knepley       pend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
125647c6ae99SBarry Smith 
125747c6ae99SBarry Smith       for (j = ys; j < ys + ny; j++) {
125847c6ae99SBarry Smith         slot = i - gxs + gnx * (j - gys);
125947c6ae99SBarry Smith 
1260bff4a2f0SMatthew G. Knepley         lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
1261bff4a2f0SMatthew G. Knepley         lend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
126247c6ae99SBarry Smith 
126347c6ae99SBarry Smith         for (k = 0; k < nc; k++) {
126447c6ae99SBarry Smith           cnt = 0;
126547c6ae99SBarry Smith           for (l = lstart; l < lend + 1; l++) {
126647c6ae99SBarry Smith             for (p = pstart; p < pend + 1; p++) {
126747c6ae99SBarry Smith               if (l || p) {
1268aa219208SBarry Smith                 if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star */
12698865f1eaSKarl Rupp                   for (ifill_col = ofill[k]; ifill_col < ofill[k + 1]; ifill_col++) cols[cnt++] = ofill[ifill_col] + nc * (slot + gnx * l + p);
127047c6ae99SBarry Smith                 }
127147c6ae99SBarry Smith               } else {
127247c6ae99SBarry Smith                 if (dfill) {
12738865f1eaSKarl Rupp                   for (ifill_col = dfill[k]; ifill_col < dfill[k + 1]; ifill_col++) cols[cnt++] = dfill[ifill_col] + nc * (slot + gnx * l + p);
127447c6ae99SBarry Smith                 } else {
12758865f1eaSKarl Rupp                   for (ifill_col = 0; ifill_col < nc; ifill_col++) cols[cnt++] = ifill_col + nc * (slot + gnx * l + p);
127647c6ae99SBarry Smith                 }
127747c6ae99SBarry Smith               }
127847c6ae99SBarry Smith             }
127947c6ae99SBarry Smith           }
128047c6ae99SBarry Smith           row = k + nc * (slot);
12819566063dSJacob Faibussowitsch           PetscCall(MatSetValuesLocal(J, 1, &row, cnt, cols, NULL, INSERT_VALUES));
128247c6ae99SBarry Smith         }
128347c6ae99SBarry Smith       }
128447c6ae99SBarry Smith     }
1285e7e92044SBarry Smith     /* do not copy values to GPU since they are all zero and not yet needed there */
12869566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_TRUE));
12879566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
12889566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
12899566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_FALSE));
12909566063dSJacob Faibussowitsch     PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
129147c6ae99SBarry Smith   }
12929566063dSJacob Faibussowitsch   PetscCall(PetscFree(cols));
12933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
129447c6ae99SBarry Smith }
129547c6ae99SBarry Smith 
1296d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPIAIJ(DM da, Mat J)
1297d71ae5a4SJacob Faibussowitsch {
129847c6ae99SBarry Smith   PetscInt               xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny;
12990298fd71SBarry Smith   PetscInt               m, n, dim, s, *cols = NULL, k, nc, *rows = NULL, col, cnt, l, p, *dnz = NULL, *onz = NULL;
1300c1154cd5SBarry Smith   PetscInt               istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk, M, N, P;
130147c6ae99SBarry Smith   MPI_Comm               comm;
1302bff4a2f0SMatthew G. Knepley   DMBoundaryType         bx, by, bz;
1303844bd0d7SStefano Zampini   ISLocalToGlobalMapping ltog, mltog;
1304aa219208SBarry Smith   DMDAStencilType        st;
1305c1154cd5SBarry Smith   PetscBool              removedups = PETSC_FALSE;
130647c6ae99SBarry Smith 
130747c6ae99SBarry Smith   PetscFunctionBegin;
130847c6ae99SBarry Smith   /*
130947c6ae99SBarry Smith          nc - number of components per grid point
131047c6ae99SBarry Smith          col - number of colors needed in one direction for single component problem
131147c6ae99SBarry Smith   */
13129566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &nc, &s, &bx, &by, &bz, &st));
131348a46eb9SPierre Jolivet   if (bx == DM_BOUNDARY_NONE && by == DM_BOUNDARY_NONE && bz == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_TRUE));
131447c6ae99SBarry Smith   col = 2 * s + 1;
131547c6ae99SBarry Smith 
1316c1154cd5SBarry Smith   /*
1317c1154cd5SBarry Smith        With one processor in periodic domains in a skinny dimension the code will label nonzero columns multiple times
1318c1154cd5SBarry Smith        because of "wrapping" around the end of the domain hitting an entry already counted in the other direction.
1319c1154cd5SBarry Smith   */
1320c1154cd5SBarry Smith   if (M == 1 && 2 * s >= m) removedups = PETSC_TRUE;
1321c1154cd5SBarry Smith   if (N == 1 && 2 * s >= n) removedups = PETSC_TRUE;
1322c1154cd5SBarry Smith   if (P == 1 && 2 * s >= p) removedups = PETSC_TRUE;
1323c1154cd5SBarry Smith 
13249566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz));
13259566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz));
13269566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
132747c6ae99SBarry Smith 
13289566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(nc, &rows, col * col * col * nc * nc, &cols));
13299566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
133047c6ae99SBarry Smith 
13319566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, nc));
133247c6ae99SBarry Smith   /* determine the matrix preallocation information */
1333d0609cedSBarry Smith   MatPreallocateBegin(comm, nc * nx * ny * nz, nc * nx * ny * nz, dnz, onz);
133447c6ae99SBarry Smith   for (i = xs; i < xs + nx; i++) {
1335bff4a2f0SMatthew G. Knepley     istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
1336bff4a2f0SMatthew G. Knepley     iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
133747c6ae99SBarry Smith     for (j = ys; j < ys + ny; j++) {
1338bff4a2f0SMatthew G. Knepley       jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
1339bff4a2f0SMatthew G. Knepley       jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
134047c6ae99SBarry Smith       for (k = zs; k < zs + nz; k++) {
1341bff4a2f0SMatthew G. Knepley         kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k));
1342bff4a2f0SMatthew G. Knepley         kend   = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1));
134347c6ae99SBarry Smith 
134447c6ae99SBarry Smith         slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs);
134547c6ae99SBarry Smith 
134647c6ae99SBarry Smith         cnt = 0;
134747c6ae99SBarry Smith         for (l = 0; l < nc; l++) {
134847c6ae99SBarry Smith           for (ii = istart; ii < iend + 1; ii++) {
134947c6ae99SBarry Smith             for (jj = jstart; jj < jend + 1; jj++) {
135047c6ae99SBarry Smith               for (kk = kstart; kk < kend + 1; kk++) {
1351aa219208SBarry Smith                 if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/
135247c6ae99SBarry Smith                   cols[cnt++] = l + nc * (slot + ii + gnx * jj + gnx * gny * kk);
135347c6ae99SBarry Smith                 }
135447c6ae99SBarry Smith               }
135547c6ae99SBarry Smith             }
135647c6ae99SBarry Smith           }
135747c6ae99SBarry Smith           rows[l] = l + nc * (slot);
135847c6ae99SBarry Smith         }
13591baa6e33SBarry Smith         if (removedups) PetscCall(MatPreallocateSetLocalRemoveDups(ltog, nc, rows, ltog, cnt, cols, dnz, onz));
13601baa6e33SBarry Smith         else PetscCall(MatPreallocateSetLocal(ltog, nc, rows, ltog, cnt, cols, dnz, onz));
136147c6ae99SBarry Smith       }
136247c6ae99SBarry Smith     }
1363c1154cd5SBarry Smith   }
13649566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, nc));
13659566063dSJacob Faibussowitsch   PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz));
13669566063dSJacob Faibussowitsch   PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz));
1367d0609cedSBarry Smith   MatPreallocateEnd(dnz, onz);
13689566063dSJacob Faibussowitsch   PetscCall(MatGetLocalToGlobalMapping(J, &mltog, NULL));
136948a46eb9SPierre Jolivet   if (!mltog) PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
137047c6ae99SBarry Smith 
137147c6ae99SBarry Smith   /*
137247c6ae99SBarry Smith     For each node in the grid: we get the neighbors in the local (on processor ordering
137347c6ae99SBarry Smith     that includes the ghost points) then MatSetValuesLocal() maps those indices to the global
137447c6ae99SBarry Smith     PETSc ordering.
137547c6ae99SBarry Smith   */
1376fcfd50ebSBarry Smith   if (!da->prealloc_only) {
137747c6ae99SBarry Smith     for (i = xs; i < xs + nx; i++) {
1378bff4a2f0SMatthew G. Knepley       istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
1379bff4a2f0SMatthew G. Knepley       iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
138047c6ae99SBarry Smith       for (j = ys; j < ys + ny; j++) {
1381bff4a2f0SMatthew G. Knepley         jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
1382bff4a2f0SMatthew G. Knepley         jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
138347c6ae99SBarry Smith         for (k = zs; k < zs + nz; k++) {
1384bff4a2f0SMatthew G. Knepley           kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k));
1385bff4a2f0SMatthew G. Knepley           kend   = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1));
138647c6ae99SBarry Smith 
138747c6ae99SBarry Smith           slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs);
138847c6ae99SBarry Smith 
138947c6ae99SBarry Smith           cnt = 0;
139047c6ae99SBarry Smith           for (kk = kstart; kk < kend + 1; kk++) {
1391071fcb05SBarry Smith             for (jj = jstart; jj < jend + 1; jj++) {
1392071fcb05SBarry Smith               for (ii = istart; ii < iend + 1; ii++) {
1393aa219208SBarry Smith                 if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/
1394071fcb05SBarry Smith                   cols[cnt++] = nc * (slot + ii + gnx * jj + gnx * gny * kk);
1395071fcb05SBarry Smith                   for (l = 1; l < nc; l++) {
13969371c9d4SSatish Balay                     cols[cnt] = 1 + cols[cnt - 1];
13979371c9d4SSatish Balay                     cnt++;
139847c6ae99SBarry Smith                   }
139947c6ae99SBarry Smith                 }
140047c6ae99SBarry Smith               }
140147c6ae99SBarry Smith             }
140247c6ae99SBarry Smith           }
14039371c9d4SSatish Balay           rows[0] = nc * (slot);
14049371c9d4SSatish Balay           for (l = 1; l < nc; l++) rows[l] = 1 + rows[l - 1];
14059566063dSJacob Faibussowitsch           PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, NULL, INSERT_VALUES));
140647c6ae99SBarry Smith         }
140747c6ae99SBarry Smith       }
140847c6ae99SBarry Smith     }
1409e7e92044SBarry Smith     /* do not copy values to GPU since they are all zero and not yet needed there */
14109566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_TRUE));
14119566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
14129566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
141348a46eb9SPierre Jolivet     if (bx == DM_BOUNDARY_NONE && by == DM_BOUNDARY_NONE && bz == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE));
14149566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_FALSE));
14159566063dSJacob Faibussowitsch     PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
141647c6ae99SBarry Smith   }
14179566063dSJacob Faibussowitsch   PetscCall(PetscFree2(rows, cols));
14183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
141947c6ae99SBarry Smith }
142047c6ae99SBarry Smith 
1421d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_1d_MPIAIJ_Fill(DM da, Mat J)
1422d71ae5a4SJacob Faibussowitsch {
1423ce308e1dSBarry Smith   DM_DA                 *dd = (DM_DA *)da->data;
1424ce308e1dSBarry Smith   PetscInt               xs, nx, i, j, gxs, gnx, row, k, l;
14258d4c968fSBarry Smith   PetscInt               m, dim, s, *cols = NULL, nc, cnt, maxcnt = 0, *ocols;
14260acb5bebSBarry Smith   PetscInt              *ofill = dd->ofill, *dfill = dd->dfill;
1427bff4a2f0SMatthew G. Knepley   DMBoundaryType         bx;
142845b6f7e9SBarry Smith   ISLocalToGlobalMapping ltog;
1429ce308e1dSBarry Smith   PetscMPIInt            rank, size;
1430ce308e1dSBarry Smith 
1431ce308e1dSBarry Smith   PetscFunctionBegin;
14329566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)da), &rank));
14339566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)da), &size));
1434ce308e1dSBarry Smith 
1435ce308e1dSBarry Smith   /*
1436ce308e1dSBarry Smith          nc - number of components per grid point
1437ce308e1dSBarry Smith   */
14389566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, NULL, NULL, NULL, NULL, NULL, &nc, &s, &bx, NULL, NULL, NULL));
143908401ef6SPierre Jolivet   PetscCheck(s <= 1, PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "Matrix creation for 1d not implemented correctly for stencil width larger than 1");
14409566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &nx, NULL, NULL));
14419566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, NULL, NULL, &gnx, NULL, NULL));
1442ce308e1dSBarry Smith 
14439566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, nc));
14449566063dSJacob Faibussowitsch   PetscCall(PetscCalloc2(nx * nc, &cols, nx * nc, &ocols));
1445ce308e1dSBarry Smith 
1446ce308e1dSBarry Smith   /*
1447ce308e1dSBarry Smith         note should be smaller for first and last process with no periodic
1448ce308e1dSBarry Smith         does not handle dfill
1449ce308e1dSBarry Smith   */
1450ce308e1dSBarry Smith   cnt = 0;
1451ce308e1dSBarry Smith   /* coupling with process to the left */
1452ce308e1dSBarry Smith   for (i = 0; i < s; i++) {
1453ce308e1dSBarry Smith     for (j = 0; j < nc; j++) {
1454dd400576SPatrick Sanan       ocols[cnt] = ((rank == 0) ? 0 : (s - i) * (ofill[j + 1] - ofill[j]));
14550acb5bebSBarry Smith       cols[cnt]  = dfill[j + 1] - dfill[j] + (s + i) * (ofill[j + 1] - ofill[j]);
1456dd400576SPatrick Sanan       if (rank == 0 && (dd->bx == DM_BOUNDARY_PERIODIC)) {
1457831644c1SBarry Smith         if (size > 1) ocols[cnt] += (s - i) * (ofill[j + 1] - ofill[j]);
1458831644c1SBarry Smith         else cols[cnt] += (s - i) * (ofill[j + 1] - ofill[j]);
1459831644c1SBarry Smith       }
1460c0ab637bSBarry Smith       maxcnt = PetscMax(maxcnt, ocols[cnt] + cols[cnt]);
1461ce308e1dSBarry Smith       cnt++;
1462ce308e1dSBarry Smith     }
1463ce308e1dSBarry Smith   }
1464ce308e1dSBarry Smith   for (i = s; i < nx - s; i++) {
1465ce308e1dSBarry Smith     for (j = 0; j < nc; j++) {
14660acb5bebSBarry Smith       cols[cnt] = dfill[j + 1] - dfill[j] + 2 * s * (ofill[j + 1] - ofill[j]);
1467c0ab637bSBarry Smith       maxcnt    = PetscMax(maxcnt, ocols[cnt] + cols[cnt]);
1468ce308e1dSBarry Smith       cnt++;
1469ce308e1dSBarry Smith     }
1470ce308e1dSBarry Smith   }
1471ce308e1dSBarry Smith   /* coupling with process to the right */
1472ce308e1dSBarry Smith   for (i = nx - s; i < nx; i++) {
1473ce308e1dSBarry Smith     for (j = 0; j < nc; j++) {
1474ce308e1dSBarry Smith       ocols[cnt] = ((rank == (size - 1)) ? 0 : (i - nx + s + 1) * (ofill[j + 1] - ofill[j]));
14750acb5bebSBarry Smith       cols[cnt]  = dfill[j + 1] - dfill[j] + (s + nx - i - 1) * (ofill[j + 1] - ofill[j]);
1476831644c1SBarry Smith       if ((rank == size - 1) && (dd->bx == DM_BOUNDARY_PERIODIC)) {
1477831644c1SBarry Smith         if (size > 1) ocols[cnt] += (i - nx + s + 1) * (ofill[j + 1] - ofill[j]);
1478831644c1SBarry Smith         else cols[cnt] += (i - nx + s + 1) * (ofill[j + 1] - ofill[j]);
1479831644c1SBarry Smith       }
1480c0ab637bSBarry Smith       maxcnt = PetscMax(maxcnt, ocols[cnt] + cols[cnt]);
1481ce308e1dSBarry Smith       cnt++;
1482ce308e1dSBarry Smith     }
1483ce308e1dSBarry Smith   }
1484ce308e1dSBarry Smith 
14859566063dSJacob Faibussowitsch   PetscCall(MatSeqAIJSetPreallocation(J, 0, cols));
14869566063dSJacob Faibussowitsch   PetscCall(MatMPIAIJSetPreallocation(J, 0, cols, 0, ocols));
14879566063dSJacob Faibussowitsch   PetscCall(PetscFree2(cols, ocols));
1488ce308e1dSBarry Smith 
14899566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
14909566063dSJacob Faibussowitsch   PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
1491ce308e1dSBarry Smith 
1492ce308e1dSBarry Smith   /*
1493ce308e1dSBarry Smith     For each node in the grid: we get the neighbors in the local (on processor ordering
1494ce308e1dSBarry Smith     that includes the ghost points) then MatSetValuesLocal() maps those indices to the global
1495ce308e1dSBarry Smith     PETSc ordering.
1496ce308e1dSBarry Smith   */
1497ce308e1dSBarry Smith   if (!da->prealloc_only) {
14989566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(maxcnt, &cols));
1499ce308e1dSBarry Smith     row = xs * nc;
1500ce308e1dSBarry Smith     /* coupling with process to the left */
1501ce308e1dSBarry Smith     for (i = xs; i < xs + s; i++) {
1502ce308e1dSBarry Smith       for (j = 0; j < nc; j++) {
1503ce308e1dSBarry Smith         cnt = 0;
1504ce308e1dSBarry Smith         if (rank) {
1505ce308e1dSBarry Smith           for (l = 0; l < s; l++) {
1506ce308e1dSBarry Smith             for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i - s + l) * nc + ofill[k];
1507ce308e1dSBarry Smith           }
1508ce308e1dSBarry Smith         }
1509dd400576SPatrick Sanan         if (rank == 0 && (dd->bx == DM_BOUNDARY_PERIODIC)) {
1510831644c1SBarry Smith           for (l = 0; l < s; l++) {
1511831644c1SBarry Smith             for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (m + i - s - l) * nc + ofill[k];
1512831644c1SBarry Smith           }
1513831644c1SBarry 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     for (i = xs + s; i < xs + nx - s; i++) {
1527ce308e1dSBarry Smith       for (j = 0; j < nc; j++) {
1528ce308e1dSBarry Smith         cnt = 0;
1529ce308e1dSBarry Smith         for (l = 0; l < s; l++) {
1530ce308e1dSBarry Smith           for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i - s + l) * nc + ofill[k];
1531ce308e1dSBarry Smith         }
15320acb5bebSBarry Smith         if (dfill) {
1533ad540459SPierre Jolivet           for (k = dfill[j]; k < dfill[j + 1]; k++) cols[cnt++] = i * nc + dfill[k];
15340acb5bebSBarry Smith         } else {
1535ad540459SPierre Jolivet           for (k = 0; k < nc; k++) cols[cnt++] = i * nc + k;
15360acb5bebSBarry Smith         }
1537ce308e1dSBarry Smith         for (l = 0; l < s; l++) {
1538ce308e1dSBarry Smith           for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i + s - l) * nc + ofill[k];
1539ce308e1dSBarry Smith         }
15409566063dSJacob Faibussowitsch         PetscCall(MatSetValues(J, 1, &row, cnt, cols, NULL, INSERT_VALUES));
1541ce308e1dSBarry Smith         row++;
1542ce308e1dSBarry Smith       }
1543ce308e1dSBarry Smith     }
1544ce308e1dSBarry Smith     /* coupling with process to the right */
1545ce308e1dSBarry Smith     for (i = xs + nx - s; i < xs + nx; i++) {
1546ce308e1dSBarry Smith       for (j = 0; j < nc; j++) {
1547ce308e1dSBarry Smith         cnt = 0;
1548ce308e1dSBarry Smith         for (l = 0; l < s; l++) {
1549ce308e1dSBarry Smith           for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i - s + l) * nc + ofill[k];
1550ce308e1dSBarry Smith         }
15510acb5bebSBarry Smith         if (dfill) {
1552ad540459SPierre Jolivet           for (k = dfill[j]; k < dfill[j + 1]; k++) cols[cnt++] = i * nc + dfill[k];
15530acb5bebSBarry Smith         } else {
1554ad540459SPierre Jolivet           for (k = 0; k < nc; k++) cols[cnt++] = i * nc + k;
15550acb5bebSBarry Smith         }
1556ce308e1dSBarry Smith         if (rank < size - 1) {
1557ce308e1dSBarry Smith           for (l = 0; l < s; l++) {
1558ce308e1dSBarry Smith             for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i + s - l) * nc + ofill[k];
1559ce308e1dSBarry Smith           }
1560ce308e1dSBarry Smith         }
1561831644c1SBarry Smith         if ((rank == size - 1) && (dd->bx == DM_BOUNDARY_PERIODIC)) {
1562831644c1SBarry Smith           for (l = 0; l < s; l++) {
1563831644c1SBarry Smith             for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i - s - l - m + 2) * nc + ofill[k];
1564831644c1SBarry Smith           }
1565831644c1SBarry Smith         }
15669566063dSJacob Faibussowitsch         PetscCall(MatSetValues(J, 1, &row, cnt, cols, NULL, INSERT_VALUES));
1567ce308e1dSBarry Smith         row++;
1568ce308e1dSBarry Smith       }
1569ce308e1dSBarry Smith     }
15709566063dSJacob Faibussowitsch     PetscCall(PetscFree(cols));
1571e7e92044SBarry Smith     /* do not copy values to GPU since they are all zero and not yet needed there */
15729566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_TRUE));
15739566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
15749566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
15759566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_FALSE));
15769566063dSJacob Faibussowitsch     PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
1577ce308e1dSBarry Smith   }
15783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1579ce308e1dSBarry Smith }
1580ce308e1dSBarry Smith 
1581d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_1d_MPIAIJ(DM da, Mat J)
1582d71ae5a4SJacob Faibussowitsch {
158347c6ae99SBarry Smith   PetscInt               xs, nx, i, i1, slot, gxs, gnx;
15840298fd71SBarry Smith   PetscInt               m, dim, s, *cols = NULL, nc, *rows = NULL, col, cnt, l;
158547c6ae99SBarry Smith   PetscInt               istart, iend;
1586bff4a2f0SMatthew G. Knepley   DMBoundaryType         bx;
1587844bd0d7SStefano Zampini   ISLocalToGlobalMapping ltog, mltog;
158847c6ae99SBarry Smith 
158947c6ae99SBarry Smith   PetscFunctionBegin;
159047c6ae99SBarry Smith   /*
159147c6ae99SBarry Smith          nc - number of components per grid point
159247c6ae99SBarry Smith          col - number of colors needed in one direction for single component problem
159347c6ae99SBarry Smith   */
15949566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, NULL, NULL, NULL, NULL, NULL, &nc, &s, &bx, NULL, NULL, NULL));
159548a46eb9SPierre Jolivet   if (bx == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_TRUE));
159647c6ae99SBarry Smith   col = 2 * s + 1;
159747c6ae99SBarry Smith 
15989566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &nx, NULL, NULL));
15999566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, NULL, NULL, &gnx, NULL, NULL));
160047c6ae99SBarry Smith 
16019566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, nc));
16029566063dSJacob Faibussowitsch   PetscCall(MatSeqAIJSetPreallocation(J, col * nc, NULL));
16039566063dSJacob Faibussowitsch   PetscCall(MatMPIAIJSetPreallocation(J, col * nc, NULL, col * nc, NULL));
160447c6ae99SBarry Smith 
16059566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
16069566063dSJacob Faibussowitsch   PetscCall(MatGetLocalToGlobalMapping(J, &mltog, NULL));
160748a46eb9SPierre Jolivet   if (!mltog) PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
160847c6ae99SBarry Smith 
160947c6ae99SBarry Smith   /*
161047c6ae99SBarry Smith     For each node in the grid: we get the neighbors in the local (on processor ordering
161147c6ae99SBarry Smith     that includes the ghost points) then MatSetValuesLocal() maps those indices to the global
161247c6ae99SBarry Smith     PETSc ordering.
161347c6ae99SBarry Smith   */
1614fcfd50ebSBarry Smith   if (!da->prealloc_only) {
16159566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(nc, &rows, col * nc * nc, &cols));
161647c6ae99SBarry Smith     for (i = xs; i < xs + nx; i++) {
161747c6ae99SBarry Smith       istart = PetscMax(-s, gxs - i);
161847c6ae99SBarry Smith       iend   = PetscMin(s, gxs + gnx - i - 1);
161947c6ae99SBarry Smith       slot   = i - gxs;
162047c6ae99SBarry Smith 
162147c6ae99SBarry Smith       cnt = 0;
162247c6ae99SBarry Smith       for (i1 = istart; i1 < iend + 1; i1++) {
1623071fcb05SBarry Smith         cols[cnt++] = nc * (slot + i1);
1624071fcb05SBarry Smith         for (l = 1; l < nc; l++) {
16259371c9d4SSatish Balay           cols[cnt] = 1 + cols[cnt - 1];
16269371c9d4SSatish Balay           cnt++;
162747c6ae99SBarry Smith         }
162847c6ae99SBarry Smith       }
16299371c9d4SSatish Balay       rows[0] = nc * (slot);
16309371c9d4SSatish Balay       for (l = 1; l < nc; l++) rows[l] = 1 + rows[l - 1];
16319566063dSJacob Faibussowitsch       PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, NULL, INSERT_VALUES));
163247c6ae99SBarry Smith     }
1633e7e92044SBarry Smith     /* do not copy values to GPU since they are all zero and not yet needed there */
16349566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_TRUE));
16359566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
16369566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
163748a46eb9SPierre Jolivet     if (bx == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE));
16389566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_FALSE));
16399566063dSJacob Faibussowitsch     PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
16409566063dSJacob Faibussowitsch     PetscCall(PetscFree2(rows, cols));
1641ce308e1dSBarry Smith   }
16423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
164347c6ae99SBarry Smith }
164447c6ae99SBarry Smith 
1645d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_1d_SeqAIJ_NoPreallocation(DM da, Mat J)
1646d71ae5a4SJacob Faibussowitsch {
164719b08ed1SBarry Smith   PetscInt               xs, nx, i, i1, slot, gxs, gnx;
164819b08ed1SBarry Smith   PetscInt               m, dim, s, *cols = NULL, nc, *rows = NULL, col, cnt, l;
164919b08ed1SBarry Smith   PetscInt               istart, iend;
165019b08ed1SBarry Smith   DMBoundaryType         bx;
165119b08ed1SBarry Smith   ISLocalToGlobalMapping ltog, mltog;
165219b08ed1SBarry Smith 
165319b08ed1SBarry Smith   PetscFunctionBegin;
165419b08ed1SBarry Smith   /*
165519b08ed1SBarry Smith          nc - number of components per grid point
165619b08ed1SBarry Smith          col - number of colors needed in one direction for single component problem
165719b08ed1SBarry Smith   */
16589566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, NULL, NULL, NULL, NULL, NULL, &nc, &s, &bx, NULL, NULL, NULL));
165919b08ed1SBarry Smith   col = 2 * s + 1;
166019b08ed1SBarry Smith 
16619566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &nx, NULL, NULL));
16629566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, NULL, NULL, &gnx, NULL, NULL));
166319b08ed1SBarry Smith 
16649566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, nc));
16659566063dSJacob Faibussowitsch   PetscCall(MatSeqAIJSetTotalPreallocation(J, nx * nc * col * nc));
166619b08ed1SBarry Smith 
16679566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
16689566063dSJacob Faibussowitsch   PetscCall(MatGetLocalToGlobalMapping(J, &mltog, NULL));
166948a46eb9SPierre Jolivet   if (!mltog) PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
167019b08ed1SBarry Smith 
167119b08ed1SBarry Smith   /*
167219b08ed1SBarry Smith     For each node in the grid: we get the neighbors in the local (on processor ordering
167319b08ed1SBarry Smith     that includes the ghost points) then MatSetValuesLocal() maps those indices to the global
167419b08ed1SBarry Smith     PETSc ordering.
167519b08ed1SBarry Smith   */
167619b08ed1SBarry Smith   if (!da->prealloc_only) {
16779566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(nc, &rows, col * nc * nc, &cols));
167819b08ed1SBarry Smith     for (i = xs; i < xs + nx; i++) {
167919b08ed1SBarry Smith       istart = PetscMax(-s, gxs - i);
168019b08ed1SBarry Smith       iend   = PetscMin(s, gxs + gnx - i - 1);
168119b08ed1SBarry Smith       slot   = i - gxs;
168219b08ed1SBarry Smith 
168319b08ed1SBarry Smith       cnt = 0;
168419b08ed1SBarry Smith       for (i1 = istart; i1 < iend + 1; i1++) {
168519b08ed1SBarry Smith         cols[cnt++] = nc * (slot + i1);
168619b08ed1SBarry Smith         for (l = 1; l < nc; l++) {
16879371c9d4SSatish Balay           cols[cnt] = 1 + cols[cnt - 1];
16889371c9d4SSatish Balay           cnt++;
168919b08ed1SBarry Smith         }
169019b08ed1SBarry Smith       }
16919371c9d4SSatish Balay       rows[0] = nc * (slot);
16929371c9d4SSatish Balay       for (l = 1; l < nc; l++) rows[l] = 1 + rows[l - 1];
16939566063dSJacob Faibussowitsch       PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, NULL, INSERT_VALUES));
169419b08ed1SBarry Smith     }
169519b08ed1SBarry Smith     /* do not copy values to GPU since they are all zero and not yet needed there */
16969566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_TRUE));
16979566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
16989566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
169948a46eb9SPierre Jolivet     if (bx == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE));
17009566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_FALSE));
17019566063dSJacob Faibussowitsch     PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
17029566063dSJacob Faibussowitsch     PetscCall(PetscFree2(rows, cols));
170319b08ed1SBarry Smith   }
17049566063dSJacob Faibussowitsch   PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE));
17053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
170619b08ed1SBarry Smith }
170719b08ed1SBarry Smith 
1708d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPIBAIJ(DM da, Mat J)
1709d71ae5a4SJacob Faibussowitsch {
171047c6ae99SBarry Smith   PetscInt               xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny;
171147c6ae99SBarry Smith   PetscInt               m, n, dim, s, *cols, nc, col, cnt, *dnz, *onz;
171247c6ae99SBarry Smith   PetscInt               istart, iend, jstart, jend, ii, jj;
171347c6ae99SBarry Smith   MPI_Comm               comm;
171447c6ae99SBarry Smith   PetscScalar           *values;
1715bff4a2f0SMatthew G. Knepley   DMBoundaryType         bx, by;
1716aa219208SBarry Smith   DMDAStencilType        st;
171745b6f7e9SBarry Smith   ISLocalToGlobalMapping ltog;
171847c6ae99SBarry Smith 
171947c6ae99SBarry Smith   PetscFunctionBegin;
172047c6ae99SBarry Smith   /*
172147c6ae99SBarry Smith      nc - number of components per grid point
172247c6ae99SBarry Smith      col - number of colors needed in one direction for single component problem
172347c6ae99SBarry Smith   */
17249566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, NULL, NULL, NULL, &nc, &s, &bx, &by, NULL, &st));
172547c6ae99SBarry Smith   col = 2 * s + 1;
172647c6ae99SBarry Smith 
17279566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL));
17289566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL));
17299566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
173047c6ae99SBarry Smith 
17319566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(col * col * nc * nc, &cols));
173247c6ae99SBarry Smith 
17339566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
173447c6ae99SBarry Smith 
173547c6ae99SBarry Smith   /* determine the matrix preallocation information */
1736d0609cedSBarry Smith   MatPreallocateBegin(comm, nx * ny, nx * ny, dnz, onz);
173747c6ae99SBarry Smith   for (i = xs; i < xs + nx; i++) {
1738bff4a2f0SMatthew G. Knepley     istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
1739bff4a2f0SMatthew G. Knepley     iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
174047c6ae99SBarry Smith     for (j = ys; j < ys + ny; j++) {
1741bff4a2f0SMatthew G. Knepley       jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
1742bff4a2f0SMatthew G. Knepley       jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
174347c6ae99SBarry Smith       slot   = i - gxs + gnx * (j - gys);
174447c6ae99SBarry Smith 
174547c6ae99SBarry Smith       /* Find block columns in block row */
174647c6ae99SBarry Smith       cnt = 0;
174747c6ae99SBarry Smith       for (ii = istart; ii < iend + 1; ii++) {
174847c6ae99SBarry Smith         for (jj = jstart; jj < jend + 1; jj++) {
1749aa219208SBarry Smith           if (st == DMDA_STENCIL_BOX || !ii || !jj) { /* BOX or on the STAR */
175047c6ae99SBarry Smith             cols[cnt++] = slot + ii + gnx * jj;
175147c6ae99SBarry Smith           }
175247c6ae99SBarry Smith         }
175347c6ae99SBarry Smith       }
17549566063dSJacob Faibussowitsch       PetscCall(MatPreallocateSetLocalBlock(ltog, 1, &slot, ltog, cnt, cols, dnz, onz));
175547c6ae99SBarry Smith     }
175647c6ae99SBarry Smith   }
17579566063dSJacob Faibussowitsch   PetscCall(MatSeqBAIJSetPreallocation(J, nc, 0, dnz));
17589566063dSJacob Faibussowitsch   PetscCall(MatMPIBAIJSetPreallocation(J, nc, 0, dnz, 0, onz));
1759d0609cedSBarry Smith   MatPreallocateEnd(dnz, onz);
176047c6ae99SBarry Smith 
17619566063dSJacob Faibussowitsch   PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
176247c6ae99SBarry Smith 
176347c6ae99SBarry Smith   /*
176447c6ae99SBarry Smith     For each node in the grid: we get the neighbors in the local (on processor ordering
176547c6ae99SBarry Smith     that includes the ghost points) then MatSetValuesLocal() maps those indices to the global
176647c6ae99SBarry Smith     PETSc ordering.
176747c6ae99SBarry Smith   */
1768fcfd50ebSBarry Smith   if (!da->prealloc_only) {
17699566063dSJacob Faibussowitsch     PetscCall(PetscCalloc1(col * col * nc * nc, &values));
177047c6ae99SBarry Smith     for (i = xs; i < xs + nx; i++) {
1771bff4a2f0SMatthew G. Knepley       istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
1772bff4a2f0SMatthew G. Knepley       iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
177347c6ae99SBarry Smith       for (j = ys; j < ys + ny; j++) {
1774bff4a2f0SMatthew G. Knepley         jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
1775bff4a2f0SMatthew G. Knepley         jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
177647c6ae99SBarry Smith         slot   = i - gxs + gnx * (j - gys);
177747c6ae99SBarry Smith         cnt    = 0;
177847c6ae99SBarry Smith         for (ii = istart; ii < iend + 1; ii++) {
177947c6ae99SBarry Smith           for (jj = jstart; jj < jend + 1; jj++) {
1780aa219208SBarry Smith             if (st == DMDA_STENCIL_BOX || !ii || !jj) { /* BOX or on the STAR */
178147c6ae99SBarry Smith               cols[cnt++] = slot + ii + gnx * jj;
178247c6ae99SBarry Smith             }
178347c6ae99SBarry Smith           }
178447c6ae99SBarry Smith         }
17859566063dSJacob Faibussowitsch         PetscCall(MatSetValuesBlockedLocal(J, 1, &slot, cnt, cols, values, INSERT_VALUES));
178647c6ae99SBarry Smith       }
178747c6ae99SBarry Smith     }
17889566063dSJacob Faibussowitsch     PetscCall(PetscFree(values));
1789e7e92044SBarry Smith     /* do not copy values to GPU since they are all zero and not yet needed there */
17909566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_TRUE));
17919566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
17929566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
17939566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_FALSE));
17949566063dSJacob Faibussowitsch     PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
179547c6ae99SBarry Smith   }
17969566063dSJacob Faibussowitsch   PetscCall(PetscFree(cols));
17973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
179847c6ae99SBarry Smith }
179947c6ae99SBarry Smith 
1800d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPIBAIJ(DM da, Mat J)
1801d71ae5a4SJacob Faibussowitsch {
180247c6ae99SBarry Smith   PetscInt               xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny;
180347c6ae99SBarry Smith   PetscInt               m, n, dim, s, *cols, k, nc, col, cnt, p, *dnz, *onz;
180447c6ae99SBarry Smith   PetscInt               istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk;
180547c6ae99SBarry Smith   MPI_Comm               comm;
180647c6ae99SBarry Smith   PetscScalar           *values;
1807bff4a2f0SMatthew G. Knepley   DMBoundaryType         bx, by, bz;
1808aa219208SBarry Smith   DMDAStencilType        st;
180945b6f7e9SBarry Smith   ISLocalToGlobalMapping ltog;
181047c6ae99SBarry Smith 
181147c6ae99SBarry Smith   PetscFunctionBegin;
181247c6ae99SBarry Smith   /*
181347c6ae99SBarry Smith          nc - number of components per grid point
181447c6ae99SBarry Smith          col - number of colors needed in one direction for single component problem
181547c6ae99SBarry Smith   */
18169566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, NULL, NULL, NULL, &nc, &s, &bx, &by, &bz, &st));
181747c6ae99SBarry Smith   col = 2 * s + 1;
181847c6ae99SBarry Smith 
18199566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz));
18209566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz));
18219566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
182247c6ae99SBarry Smith 
18239566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(col * col * col, &cols));
182447c6ae99SBarry Smith 
18259566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
182647c6ae99SBarry Smith 
182747c6ae99SBarry Smith   /* determine the matrix preallocation information */
1828d0609cedSBarry Smith   MatPreallocateBegin(comm, nx * ny * nz, nx * ny * nz, dnz, onz);
182947c6ae99SBarry Smith   for (i = xs; i < xs + nx; i++) {
1830bff4a2f0SMatthew G. Knepley     istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
1831bff4a2f0SMatthew G. Knepley     iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
183247c6ae99SBarry Smith     for (j = ys; j < ys + ny; j++) {
1833bff4a2f0SMatthew G. Knepley       jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
1834bff4a2f0SMatthew G. Knepley       jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
183547c6ae99SBarry Smith       for (k = zs; k < zs + nz; k++) {
1836bff4a2f0SMatthew G. Knepley         kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k));
1837bff4a2f0SMatthew G. Knepley         kend   = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1));
183847c6ae99SBarry Smith 
183947c6ae99SBarry Smith         slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs);
184047c6ae99SBarry Smith 
184147c6ae99SBarry Smith         /* Find block columns in block row */
184247c6ae99SBarry Smith         cnt = 0;
184347c6ae99SBarry Smith         for (ii = istart; ii < iend + 1; ii++) {
184447c6ae99SBarry Smith           for (jj = jstart; jj < jend + 1; jj++) {
184547c6ae99SBarry Smith             for (kk = kstart; kk < kend + 1; kk++) {
1846aa219208SBarry Smith               if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/
184747c6ae99SBarry Smith                 cols[cnt++] = slot + ii + gnx * jj + gnx * gny * kk;
184847c6ae99SBarry Smith               }
184947c6ae99SBarry Smith             }
185047c6ae99SBarry Smith           }
185147c6ae99SBarry Smith         }
18529566063dSJacob Faibussowitsch         PetscCall(MatPreallocateSetLocalBlock(ltog, 1, &slot, ltog, cnt, cols, dnz, onz));
185347c6ae99SBarry Smith       }
185447c6ae99SBarry Smith     }
185547c6ae99SBarry Smith   }
18569566063dSJacob Faibussowitsch   PetscCall(MatSeqBAIJSetPreallocation(J, nc, 0, dnz));
18579566063dSJacob Faibussowitsch   PetscCall(MatMPIBAIJSetPreallocation(J, nc, 0, dnz, 0, onz));
1858d0609cedSBarry Smith   MatPreallocateEnd(dnz, onz);
185947c6ae99SBarry Smith 
18609566063dSJacob Faibussowitsch   PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
186147c6ae99SBarry Smith 
186247c6ae99SBarry Smith   /*
186347c6ae99SBarry Smith     For each node in the grid: we get the neighbors in the local (on processor ordering
186447c6ae99SBarry Smith     that includes the ghost points) then MatSetValuesLocal() maps those indices to the global
186547c6ae99SBarry Smith     PETSc ordering.
186647c6ae99SBarry Smith   */
1867fcfd50ebSBarry Smith   if (!da->prealloc_only) {
18689566063dSJacob Faibussowitsch     PetscCall(PetscCalloc1(col * col * col * nc * nc, &values));
186947c6ae99SBarry Smith     for (i = xs; i < xs + nx; i++) {
1870bff4a2f0SMatthew G. Knepley       istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
1871bff4a2f0SMatthew G. Knepley       iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
187247c6ae99SBarry Smith       for (j = ys; j < ys + ny; j++) {
1873bff4a2f0SMatthew G. Knepley         jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
1874bff4a2f0SMatthew G. Knepley         jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
187547c6ae99SBarry Smith         for (k = zs; k < zs + nz; k++) {
1876bff4a2f0SMatthew G. Knepley           kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k));
1877bff4a2f0SMatthew G. Knepley           kend   = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1));
187847c6ae99SBarry Smith 
187947c6ae99SBarry Smith           slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs);
188047c6ae99SBarry Smith 
188147c6ae99SBarry Smith           cnt = 0;
188247c6ae99SBarry Smith           for (ii = istart; ii < iend + 1; ii++) {
188347c6ae99SBarry Smith             for (jj = jstart; jj < jend + 1; jj++) {
188447c6ae99SBarry Smith               for (kk = kstart; kk < kend + 1; kk++) {
1885aa219208SBarry Smith                 if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/
188647c6ae99SBarry Smith                   cols[cnt++] = slot + ii + gnx * jj + gnx * gny * kk;
188747c6ae99SBarry Smith                 }
188847c6ae99SBarry Smith               }
188947c6ae99SBarry Smith             }
189047c6ae99SBarry Smith           }
18919566063dSJacob Faibussowitsch           PetscCall(MatSetValuesBlockedLocal(J, 1, &slot, cnt, cols, values, INSERT_VALUES));
189247c6ae99SBarry Smith         }
189347c6ae99SBarry Smith       }
189447c6ae99SBarry Smith     }
18959566063dSJacob Faibussowitsch     PetscCall(PetscFree(values));
1896e7e92044SBarry Smith     /* do not copy values to GPU since they are all zero and not yet needed there */
18979566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_TRUE));
18989566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
18999566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
19009566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_FALSE));
19019566063dSJacob Faibussowitsch     PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
190247c6ae99SBarry Smith   }
19039566063dSJacob Faibussowitsch   PetscCall(PetscFree(cols));
19043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
190547c6ae99SBarry Smith }
190647c6ae99SBarry Smith 
190747c6ae99SBarry Smith /*
190847c6ae99SBarry Smith   This helper is for of SBAIJ preallocation, to discard the lower-triangular values which are difficult to
190947c6ae99SBarry Smith   identify in the local ordering with periodic domain.
191047c6ae99SBarry Smith */
1911d71ae5a4SJacob Faibussowitsch static PetscErrorCode L2GFilterUpperTriangular(ISLocalToGlobalMapping ltog, PetscInt *row, PetscInt *cnt, PetscInt col[])
1912d71ae5a4SJacob Faibussowitsch {
191347c6ae99SBarry Smith   PetscInt i, n;
191447c6ae99SBarry Smith 
191547c6ae99SBarry Smith   PetscFunctionBegin;
19169566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingApplyBlock(ltog, 1, row, row));
19179566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingApplyBlock(ltog, *cnt, col, col));
191847c6ae99SBarry Smith   for (i = 0, n = 0; i < *cnt; i++) {
191947c6ae99SBarry Smith     if (col[i] >= *row) col[n++] = col[i];
192047c6ae99SBarry Smith   }
192147c6ae99SBarry Smith   *cnt = n;
19223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
192347c6ae99SBarry Smith }
192447c6ae99SBarry Smith 
1925d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPISBAIJ(DM da, Mat J)
1926d71ae5a4SJacob Faibussowitsch {
192747c6ae99SBarry Smith   PetscInt               xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny;
192847c6ae99SBarry Smith   PetscInt               m, n, dim, s, *cols, nc, col, cnt, *dnz, *onz;
192947c6ae99SBarry Smith   PetscInt               istart, iend, jstart, jend, ii, jj;
193047c6ae99SBarry Smith   MPI_Comm               comm;
193147c6ae99SBarry Smith   PetscScalar           *values;
1932bff4a2f0SMatthew G. Knepley   DMBoundaryType         bx, by;
1933aa219208SBarry Smith   DMDAStencilType        st;
193445b6f7e9SBarry Smith   ISLocalToGlobalMapping ltog;
193547c6ae99SBarry Smith 
193647c6ae99SBarry Smith   PetscFunctionBegin;
193747c6ae99SBarry Smith   /*
193847c6ae99SBarry Smith      nc - number of components per grid point
193947c6ae99SBarry Smith      col - number of colors needed in one direction for single component problem
194047c6ae99SBarry Smith   */
19419566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, NULL, NULL, NULL, &nc, &s, &bx, &by, NULL, &st));
194247c6ae99SBarry Smith   col = 2 * s + 1;
194347c6ae99SBarry Smith 
19449566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL));
19459566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL));
19469566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
194747c6ae99SBarry Smith 
19489566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(col * col * nc * nc, &cols));
194947c6ae99SBarry Smith 
19509566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
195147c6ae99SBarry Smith 
195247c6ae99SBarry Smith   /* determine the matrix preallocation information */
1953d0609cedSBarry Smith   MatPreallocateBegin(comm, nx * ny, nx * ny, dnz, onz);
195447c6ae99SBarry Smith   for (i = xs; i < xs + nx; i++) {
1955bff4a2f0SMatthew G. Knepley     istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
1956bff4a2f0SMatthew G. Knepley     iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
195747c6ae99SBarry Smith     for (j = ys; j < ys + ny; j++) {
1958bff4a2f0SMatthew G. Knepley       jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
1959bff4a2f0SMatthew G. Knepley       jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
196047c6ae99SBarry Smith       slot   = i - gxs + gnx * (j - gys);
196147c6ae99SBarry Smith 
196247c6ae99SBarry Smith       /* Find block columns in block row */
196347c6ae99SBarry Smith       cnt = 0;
196447c6ae99SBarry Smith       for (ii = istart; ii < iend + 1; ii++) {
196547c6ae99SBarry Smith         for (jj = jstart; jj < jend + 1; jj++) {
1966ad540459SPierre Jolivet           if (st == DMDA_STENCIL_BOX || !ii || !jj) cols[cnt++] = slot + ii + gnx * jj;
196747c6ae99SBarry Smith         }
196847c6ae99SBarry Smith       }
19699566063dSJacob Faibussowitsch       PetscCall(L2GFilterUpperTriangular(ltog, &slot, &cnt, cols));
19709566063dSJacob Faibussowitsch       PetscCall(MatPreallocateSymmetricSetBlock(slot, cnt, cols, dnz, onz));
197147c6ae99SBarry Smith     }
197247c6ae99SBarry Smith   }
19739566063dSJacob Faibussowitsch   PetscCall(MatSeqSBAIJSetPreallocation(J, nc, 0, dnz));
19749566063dSJacob Faibussowitsch   PetscCall(MatMPISBAIJSetPreallocation(J, nc, 0, dnz, 0, onz));
1975d0609cedSBarry Smith   MatPreallocateEnd(dnz, onz);
197647c6ae99SBarry Smith 
19779566063dSJacob Faibussowitsch   PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
197847c6ae99SBarry Smith 
197947c6ae99SBarry Smith   /*
198047c6ae99SBarry Smith     For each node in the grid: we get the neighbors in the local (on processor ordering
198147c6ae99SBarry Smith     that includes the ghost points) then MatSetValuesLocal() maps those indices to the global
198247c6ae99SBarry Smith     PETSc ordering.
198347c6ae99SBarry Smith   */
1984fcfd50ebSBarry Smith   if (!da->prealloc_only) {
19859566063dSJacob Faibussowitsch     PetscCall(PetscCalloc1(col * col * nc * nc, &values));
198647c6ae99SBarry Smith     for (i = xs; i < xs + nx; i++) {
1987bff4a2f0SMatthew G. Knepley       istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
1988bff4a2f0SMatthew G. Knepley       iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
198947c6ae99SBarry Smith       for (j = ys; j < ys + ny; j++) {
1990bff4a2f0SMatthew G. Knepley         jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
1991bff4a2f0SMatthew G. Knepley         jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
199247c6ae99SBarry Smith         slot   = i - gxs + gnx * (j - gys);
199347c6ae99SBarry Smith 
199447c6ae99SBarry Smith         /* Find block columns in block row */
199547c6ae99SBarry Smith         cnt = 0;
199647c6ae99SBarry Smith         for (ii = istart; ii < iend + 1; ii++) {
199747c6ae99SBarry Smith           for (jj = jstart; jj < jend + 1; jj++) {
1998ad540459SPierre Jolivet             if (st == DMDA_STENCIL_BOX || !ii || !jj) cols[cnt++] = slot + ii + gnx * jj;
199947c6ae99SBarry Smith           }
200047c6ae99SBarry Smith         }
20019566063dSJacob Faibussowitsch         PetscCall(L2GFilterUpperTriangular(ltog, &slot, &cnt, cols));
20029566063dSJacob Faibussowitsch         PetscCall(MatSetValuesBlocked(J, 1, &slot, cnt, cols, values, INSERT_VALUES));
200347c6ae99SBarry Smith       }
200447c6ae99SBarry Smith     }
20059566063dSJacob Faibussowitsch     PetscCall(PetscFree(values));
2006e7e92044SBarry Smith     /* do not copy values to GPU since they are all zero and not yet needed there */
20079566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_TRUE));
20089566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
20099566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
20109566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_FALSE));
20119566063dSJacob Faibussowitsch     PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
201247c6ae99SBarry Smith   }
20139566063dSJacob Faibussowitsch   PetscCall(PetscFree(cols));
20143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
201547c6ae99SBarry Smith }
201647c6ae99SBarry Smith 
2017d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPISBAIJ(DM da, Mat J)
2018d71ae5a4SJacob Faibussowitsch {
201947c6ae99SBarry Smith   PetscInt               xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny;
202047c6ae99SBarry Smith   PetscInt               m, n, dim, s, *cols, k, nc, col, cnt, p, *dnz, *onz;
202147c6ae99SBarry Smith   PetscInt               istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk;
202247c6ae99SBarry Smith   MPI_Comm               comm;
202347c6ae99SBarry Smith   PetscScalar           *values;
2024bff4a2f0SMatthew G. Knepley   DMBoundaryType         bx, by, bz;
2025aa219208SBarry Smith   DMDAStencilType        st;
202645b6f7e9SBarry Smith   ISLocalToGlobalMapping ltog;
202747c6ae99SBarry Smith 
202847c6ae99SBarry Smith   PetscFunctionBegin;
202947c6ae99SBarry Smith   /*
203047c6ae99SBarry Smith      nc - number of components per grid point
203147c6ae99SBarry Smith      col - number of colors needed in one direction for single component problem
203247c6ae99SBarry Smith   */
20339566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, NULL, NULL, NULL, &nc, &s, &bx, &by, &bz, &st));
203447c6ae99SBarry Smith   col = 2 * s + 1;
203547c6ae99SBarry Smith 
20369566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz));
20379566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz));
20389566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
203947c6ae99SBarry Smith 
204047c6ae99SBarry Smith   /* create the matrix */
20419566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(col * col * col, &cols));
204247c6ae99SBarry Smith 
20439566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
204447c6ae99SBarry Smith 
204547c6ae99SBarry Smith   /* determine the matrix preallocation information */
2046d0609cedSBarry Smith   MatPreallocateBegin(comm, nx * ny * nz, nx * ny * nz, dnz, onz);
204747c6ae99SBarry Smith   for (i = xs; i < xs + nx; i++) {
2048bff4a2f0SMatthew G. Knepley     istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
2049bff4a2f0SMatthew G. Knepley     iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
205047c6ae99SBarry Smith     for (j = ys; j < ys + ny; j++) {
2051bff4a2f0SMatthew G. Knepley       jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
2052bff4a2f0SMatthew G. Knepley       jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
205347c6ae99SBarry Smith       for (k = zs; k < zs + nz; k++) {
2054bff4a2f0SMatthew G. Knepley         kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k));
2055bff4a2f0SMatthew G. Knepley         kend   = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1));
205647c6ae99SBarry Smith 
205747c6ae99SBarry Smith         slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs);
205847c6ae99SBarry Smith 
205947c6ae99SBarry Smith         /* Find block columns in block row */
206047c6ae99SBarry Smith         cnt = 0;
206147c6ae99SBarry Smith         for (ii = istart; ii < iend + 1; ii++) {
206247c6ae99SBarry Smith           for (jj = jstart; jj < jend + 1; jj++) {
206347c6ae99SBarry Smith             for (kk = kstart; kk < kend + 1; kk++) {
2064ad540459SPierre Jolivet               if ((st == DMDA_STENCIL_BOX) || (!ii && !jj) || (!jj && !kk) || (!ii && !kk)) cols[cnt++] = slot + ii + gnx * jj + gnx * gny * kk;
206547c6ae99SBarry Smith             }
206647c6ae99SBarry Smith           }
206747c6ae99SBarry Smith         }
20689566063dSJacob Faibussowitsch         PetscCall(L2GFilterUpperTriangular(ltog, &slot, &cnt, cols));
20699566063dSJacob Faibussowitsch         PetscCall(MatPreallocateSymmetricSetBlock(slot, cnt, cols, dnz, onz));
207047c6ae99SBarry Smith       }
207147c6ae99SBarry Smith     }
207247c6ae99SBarry Smith   }
20739566063dSJacob Faibussowitsch   PetscCall(MatSeqSBAIJSetPreallocation(J, nc, 0, dnz));
20749566063dSJacob Faibussowitsch   PetscCall(MatMPISBAIJSetPreallocation(J, nc, 0, dnz, 0, onz));
2075d0609cedSBarry Smith   MatPreallocateEnd(dnz, onz);
207647c6ae99SBarry Smith 
20779566063dSJacob Faibussowitsch   PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
207847c6ae99SBarry Smith 
207947c6ae99SBarry Smith   /*
208047c6ae99SBarry Smith     For each node in the grid: we get the neighbors in the local (on processor ordering
208147c6ae99SBarry Smith     that includes the ghost points) then MatSetValuesLocal() maps those indices to the global
208247c6ae99SBarry Smith     PETSc ordering.
208347c6ae99SBarry Smith   */
2084fcfd50ebSBarry Smith   if (!da->prealloc_only) {
20859566063dSJacob Faibussowitsch     PetscCall(PetscCalloc1(col * col * col * nc * nc, &values));
208647c6ae99SBarry Smith     for (i = xs; i < xs + nx; i++) {
2087bff4a2f0SMatthew G. Knepley       istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
2088bff4a2f0SMatthew G. Knepley       iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
208947c6ae99SBarry Smith       for (j = ys; j < ys + ny; j++) {
2090bff4a2f0SMatthew G. Knepley         jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
2091bff4a2f0SMatthew G. Knepley         jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
209247c6ae99SBarry Smith         for (k = zs; k < zs + nz; k++) {
2093bff4a2f0SMatthew G. Knepley           kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k));
2094bff4a2f0SMatthew G. Knepley           kend   = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1));
209547c6ae99SBarry Smith 
209647c6ae99SBarry Smith           slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs);
209747c6ae99SBarry Smith 
209847c6ae99SBarry Smith           cnt = 0;
209947c6ae99SBarry Smith           for (ii = istart; ii < iend + 1; ii++) {
210047c6ae99SBarry Smith             for (jj = jstart; jj < jend + 1; jj++) {
210147c6ae99SBarry Smith               for (kk = kstart; kk < kend + 1; kk++) {
2102ad540459SPierre Jolivet                 if ((st == DMDA_STENCIL_BOX) || (!ii && !jj) || (!jj && !kk) || (!ii && !kk)) cols[cnt++] = slot + ii + gnx * jj + gnx * gny * kk;
210347c6ae99SBarry Smith               }
210447c6ae99SBarry Smith             }
210547c6ae99SBarry Smith           }
21069566063dSJacob Faibussowitsch           PetscCall(L2GFilterUpperTriangular(ltog, &slot, &cnt, cols));
21079566063dSJacob Faibussowitsch           PetscCall(MatSetValuesBlocked(J, 1, &slot, cnt, cols, values, INSERT_VALUES));
210847c6ae99SBarry Smith         }
210947c6ae99SBarry Smith       }
211047c6ae99SBarry Smith     }
21119566063dSJacob Faibussowitsch     PetscCall(PetscFree(values));
2112e7e92044SBarry Smith     /* do not copy values to GPU since they are all zero and not yet needed there */
21139566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_TRUE));
21149566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
21159566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
21169566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_FALSE));
21179566063dSJacob Faibussowitsch     PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
211847c6ae99SBarry Smith   }
21199566063dSJacob Faibussowitsch   PetscCall(PetscFree(cols));
21203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
212147c6ae99SBarry Smith }
212247c6ae99SBarry Smith 
2123d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPIAIJ_Fill(DM da, Mat J)
2124d71ae5a4SJacob Faibussowitsch {
212547c6ae99SBarry Smith   PetscInt               xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny;
2126c0ab637bSBarry Smith   PetscInt               m, n, dim, s, *cols, k, nc, row, col, cnt, maxcnt = 0, l, p, *dnz, *onz;
2127c1154cd5SBarry Smith   PetscInt               istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk, M, N, P;
212847c6ae99SBarry Smith   DM_DA                 *dd = (DM_DA *)da->data;
212947c6ae99SBarry Smith   PetscInt               ifill_col, *dfill = dd->dfill, *ofill = dd->ofill;
213047c6ae99SBarry Smith   MPI_Comm               comm;
213147c6ae99SBarry Smith   PetscScalar           *values;
2132bff4a2f0SMatthew G. Knepley   DMBoundaryType         bx, by, bz;
213345b6f7e9SBarry Smith   ISLocalToGlobalMapping ltog;
2134aa219208SBarry Smith   DMDAStencilType        st;
2135c1154cd5SBarry Smith   PetscBool              removedups = PETSC_FALSE;
213647c6ae99SBarry Smith 
213747c6ae99SBarry Smith   PetscFunctionBegin;
213847c6ae99SBarry Smith   /*
213947c6ae99SBarry Smith          nc - number of components per grid point
214047c6ae99SBarry Smith          col - number of colors needed in one direction for single component problem
214147c6ae99SBarry Smith   */
21429566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &nc, &s, &bx, &by, &bz, &st));
214347c6ae99SBarry Smith   col = 2 * s + 1;
214447c6ae99SBarry Smith 
2145c1154cd5SBarry Smith   /*
2146c1154cd5SBarry Smith        With one processor in periodic domains in a skinny dimension the code will label nonzero columns multiple times
2147c1154cd5SBarry Smith        because of "wrapping" around the end of the domain hitting an entry already counted in the other direction.
2148c1154cd5SBarry Smith   */
2149c1154cd5SBarry Smith   if (M == 1 && 2 * s >= m) removedups = PETSC_TRUE;
2150c1154cd5SBarry Smith   if (N == 1 && 2 * s >= n) removedups = PETSC_TRUE;
2151c1154cd5SBarry Smith   if (P == 1 && 2 * s >= p) removedups = PETSC_TRUE;
2152c1154cd5SBarry Smith 
21539566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz));
21549566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz));
21559566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
215647c6ae99SBarry Smith 
21579566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(col * col * col * nc, &cols));
21589566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
215947c6ae99SBarry Smith 
216047c6ae99SBarry Smith   /* determine the matrix preallocation information */
2161d0609cedSBarry Smith   MatPreallocateBegin(comm, nc * nx * ny * nz, nc * nx * ny * nz, dnz, onz);
216247c6ae99SBarry Smith 
21639566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, nc));
216447c6ae99SBarry Smith   for (i = xs; i < xs + nx; i++) {
2165bff4a2f0SMatthew G. Knepley     istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
2166bff4a2f0SMatthew G. Knepley     iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
216747c6ae99SBarry Smith     for (j = ys; j < ys + ny; j++) {
2168bff4a2f0SMatthew G. Knepley       jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
2169bff4a2f0SMatthew G. Knepley       jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
217047c6ae99SBarry Smith       for (k = zs; k < zs + nz; k++) {
2171bff4a2f0SMatthew G. Knepley         kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k));
2172bff4a2f0SMatthew G. Knepley         kend   = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1));
217347c6ae99SBarry Smith 
217447c6ae99SBarry Smith         slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs);
217547c6ae99SBarry Smith 
217647c6ae99SBarry Smith         for (l = 0; l < nc; l++) {
217747c6ae99SBarry Smith           cnt = 0;
217847c6ae99SBarry Smith           for (ii = istart; ii < iend + 1; ii++) {
217947c6ae99SBarry Smith             for (jj = jstart; jj < jend + 1; jj++) {
218047c6ae99SBarry Smith               for (kk = kstart; kk < kend + 1; kk++) {
218147c6ae99SBarry Smith                 if (ii || jj || kk) {
2182aa219208SBarry Smith                   if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/
21838865f1eaSKarl 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);
218447c6ae99SBarry Smith                   }
218547c6ae99SBarry Smith                 } else {
218647c6ae99SBarry Smith                   if (dfill) {
21878865f1eaSKarl 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);
218847c6ae99SBarry Smith                   } else {
21898865f1eaSKarl Rupp                     for (ifill_col = 0; ifill_col < nc; ifill_col++) cols[cnt++] = ifill_col + nc * (slot + ii + gnx * jj + gnx * gny * kk);
219047c6ae99SBarry Smith                   }
219147c6ae99SBarry Smith                 }
219247c6ae99SBarry Smith               }
219347c6ae99SBarry Smith             }
219447c6ae99SBarry Smith           }
219547c6ae99SBarry Smith           row    = l + nc * (slot);
2196c0ab637bSBarry Smith           maxcnt = PetscMax(maxcnt, cnt);
21971baa6e33SBarry Smith           if (removedups) PetscCall(MatPreallocateSetLocalRemoveDups(ltog, 1, &row, ltog, cnt, cols, dnz, onz));
21981baa6e33SBarry Smith           else PetscCall(MatPreallocateSetLocal(ltog, 1, &row, ltog, cnt, cols, dnz, onz));
219947c6ae99SBarry Smith         }
220047c6ae99SBarry Smith       }
220147c6ae99SBarry Smith     }
2202c1154cd5SBarry Smith   }
22039566063dSJacob Faibussowitsch   PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz));
22049566063dSJacob Faibussowitsch   PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz));
2205d0609cedSBarry Smith   MatPreallocateEnd(dnz, onz);
22069566063dSJacob Faibussowitsch   PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
220747c6ae99SBarry Smith 
220847c6ae99SBarry Smith   /*
220947c6ae99SBarry Smith     For each node in the grid: we get the neighbors in the local (on processor ordering
221047c6ae99SBarry Smith     that includes the ghost points) then MatSetValuesLocal() maps those indices to the global
221147c6ae99SBarry Smith     PETSc ordering.
221247c6ae99SBarry Smith   */
2213fcfd50ebSBarry Smith   if (!da->prealloc_only) {
22149566063dSJacob Faibussowitsch     PetscCall(PetscCalloc1(maxcnt, &values));
221547c6ae99SBarry Smith     for (i = xs; i < xs + nx; i++) {
2216bff4a2f0SMatthew G. Knepley       istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
2217bff4a2f0SMatthew G. Knepley       iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
221847c6ae99SBarry Smith       for (j = ys; j < ys + ny; j++) {
2219bff4a2f0SMatthew G. Knepley         jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
2220bff4a2f0SMatthew G. Knepley         jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
222147c6ae99SBarry Smith         for (k = zs; k < zs + nz; k++) {
2222bff4a2f0SMatthew G. Knepley           kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k));
2223bff4a2f0SMatthew G. Knepley           kend   = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1));
222447c6ae99SBarry Smith 
222547c6ae99SBarry Smith           slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs);
222647c6ae99SBarry Smith 
222747c6ae99SBarry Smith           for (l = 0; l < nc; l++) {
222847c6ae99SBarry Smith             cnt = 0;
222947c6ae99SBarry Smith             for (ii = istart; ii < iend + 1; ii++) {
223047c6ae99SBarry Smith               for (jj = jstart; jj < jend + 1; jj++) {
223147c6ae99SBarry Smith                 for (kk = kstart; kk < kend + 1; kk++) {
223247c6ae99SBarry Smith                   if (ii || jj || kk) {
2233aa219208SBarry Smith                     if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/
22348865f1eaSKarl 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);
223547c6ae99SBarry Smith                     }
223647c6ae99SBarry Smith                   } else {
223747c6ae99SBarry Smith                     if (dfill) {
22388865f1eaSKarl 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);
223947c6ae99SBarry Smith                     } else {
22408865f1eaSKarl Rupp                       for (ifill_col = 0; ifill_col < nc; ifill_col++) cols[cnt++] = ifill_col + nc * (slot + ii + gnx * jj + gnx * gny * kk);
224147c6ae99SBarry Smith                     }
224247c6ae99SBarry Smith                   }
224347c6ae99SBarry Smith                 }
224447c6ae99SBarry Smith               }
224547c6ae99SBarry Smith             }
224647c6ae99SBarry Smith             row = l + nc * (slot);
22479566063dSJacob Faibussowitsch             PetscCall(MatSetValuesLocal(J, 1, &row, cnt, cols, values, INSERT_VALUES));
224847c6ae99SBarry Smith           }
224947c6ae99SBarry Smith         }
225047c6ae99SBarry Smith       }
225147c6ae99SBarry Smith     }
22529566063dSJacob Faibussowitsch     PetscCall(PetscFree(values));
2253e7e92044SBarry Smith     /* do not copy values to GPU since they are all zero and not yet needed there */
22549566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_TRUE));
22559566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
22569566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
22579566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_FALSE));
22589566063dSJacob Faibussowitsch     PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
225947c6ae99SBarry Smith   }
22609566063dSJacob Faibussowitsch   PetscCall(PetscFree(cols));
22613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
226247c6ae99SBarry Smith }
2263