xref: /petsc/src/dm/impls/da/fdda.c (revision 2286efddd54511ab18e8e2adb1e023c4bf8f0b92)
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 
DMDASetBlockFills_Private(const PetscInt * dfill,PetscInt w,PetscInt ** rfill)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 
DMDASetBlockFillsSparse_Private(const PetscInt * dfillsparse,PetscInt w,PetscInt ** rfill)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 
DMDASetBlockFills_Private2(DM_DA * dd)66d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDASetBlockFills_Private2(DM_DA *dd)
67d71ae5a4SJacob Faibussowitsch {
6809e28618SBarry Smith   PetscInt i, k, cnt = 1;
6909e28618SBarry Smith 
7009e28618SBarry Smith   PetscFunctionBegin;
7146091a0eSPierre Jolivet   /* ofillcount tracks the columns of ofill that have any nonzero in them; the value in each location is the number of
7209e28618SBarry Smith    columns to the left with any nonzeros in them plus 1 */
739566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(dd->w, &dd->ofillcols));
7409e28618SBarry Smith   for (i = 0; i < dd->w; i++) {
7509e28618SBarry Smith     for (k = dd->ofill[i]; k < dd->ofill[i + 1]; k++) dd->ofillcols[dd->ofill[k]] = 1;
7609e28618SBarry Smith   }
7709e28618SBarry Smith   for (i = 0; i < dd->w; i++) {
78ad540459SPierre Jolivet     if (dd->ofillcols[i]) dd->ofillcols[i] = cnt++;
7909e28618SBarry Smith   }
803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8109e28618SBarry Smith }
8209e28618SBarry Smith 
8347c6ae99SBarry Smith /*@
84aa219208SBarry Smith   DMDASetBlockFills - Sets the fill pattern in each block for a multi-component problem
85dce8aebaSBarry Smith   of the matrix returned by `DMCreateMatrix()`.
8647c6ae99SBarry Smith 
8720f4b53cSBarry Smith   Logically Collective
8847c6ae99SBarry Smith 
89d8d19677SJose E. Roman   Input Parameters:
9072fd0fbdSBarry Smith + da    - the `DMDA`
9112b4a537SBarry Smith . dfill - the fill pattern in the diagonal block (may be `NULL`, means use dense block)
9247c6ae99SBarry Smith - ofill - the fill pattern in the off-diagonal blocks
9347c6ae99SBarry Smith 
9447c6ae99SBarry Smith   Level: developer
9547c6ae99SBarry Smith 
9695452b02SPatrick Sanan   Notes:
9795452b02SPatrick Sanan   This only makes sense when you are doing multicomponent problems but using the
98dce8aebaSBarry Smith   `MATMPIAIJ` matrix format
9947c6ae99SBarry Smith 
10012b4a537SBarry Smith   The format for `dfill` and `ofill` is a 2 dimensional dof by dof matrix with 1 entries
10147c6ae99SBarry Smith   representing coupling and 0 entries for missing coupling. For example
102dce8aebaSBarry Smith .vb
103dce8aebaSBarry Smith             dfill[9] = {1, 0, 0,
104dce8aebaSBarry Smith                         1, 1, 0,
105dce8aebaSBarry Smith                         0, 1, 1}
106dce8aebaSBarry Smith .ve
10747c6ae99SBarry Smith   means that row 0 is coupled with only itself in the diagonal block, row 1 is coupled with
10847c6ae99SBarry Smith   itself and row 0 (in the diagonal block) and row 2 is coupled with itself and row 1 (in the
10947c6ae99SBarry Smith   diagonal block).
11047c6ae99SBarry Smith 
111dce8aebaSBarry Smith   `DMDASetGetMatrix()` allows you to provide general code for those more complicated nonzero patterns then
11212b4a537SBarry Smith   can be represented in the `dfill`, `ofill` format
11347c6ae99SBarry Smith 
1141d27aa22SBarry Smith   Contributed by\:
1151d27aa22SBarry Smith   Glenn Hammond
11647c6ae99SBarry Smith 
11712b4a537SBarry Smith .seealso: [](sec_struct), `DM`, `DMDA`, `DMCreateMatrix()`, `DMDASetGetMatrix()`, `DMSetMatrixPreallocateOnly()`, `DMDASetBlockFillsSparse()`
11847c6ae99SBarry Smith @*/
DMDASetBlockFills(DM da,const PetscInt * dfill,const PetscInt * ofill)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:
14172fd0fbdSBarry Smith + da          - the `DMDA`
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 
1681d27aa22SBarry Smith   Contributed by\:
1691d27aa22SBarry Smith   Philip C. Roth
17009e28618SBarry Smith 
17112b4a537SBarry Smith .seealso: [](sec_struct), `DM`, `DMDA`, `DMDASetBlockFills()`, `DMCreateMatrix()`, `DMDASetGetMatrix()`, `DMSetMatrixPreallocateOnly()`
17209e28618SBarry Smith @*/
DMDASetBlockFillsSparse(DM da,const PetscInt * dfillsparse,const PetscInt * ofillsparse)173d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDASetBlockFillsSparse(DM da, const PetscInt *dfillsparse, const PetscInt *ofillsparse)
174d71ae5a4SJacob Faibussowitsch {
17509e28618SBarry Smith   DM_DA *dd = (DM_DA *)da->data;
17609e28618SBarry Smith 
17709e28618SBarry Smith   PetscFunctionBegin;
17809e28618SBarry Smith   /* save the given dfill and ofill information */
1799566063dSJacob Faibussowitsch   PetscCall(DMDASetBlockFillsSparse_Private(dfillsparse, dd->w, &dd->dfill));
1809566063dSJacob Faibussowitsch   PetscCall(DMDASetBlockFillsSparse_Private(ofillsparse, dd->w, &dd->ofill));
18109e28618SBarry Smith 
18209e28618SBarry Smith   /* count nonzeros in ofill columns */
1839566063dSJacob Faibussowitsch   PetscCall(DMDASetBlockFills_Private2(dd));
1843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
18547c6ae99SBarry Smith }
18647c6ae99SBarry Smith 
DMCreateColoring_DA(DM da,ISColoringType ctype,ISColoring * coloring)187d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA(DM da, ISColoringType ctype, ISColoring *coloring)
188d71ae5a4SJacob Faibussowitsch {
18947c6ae99SBarry Smith   PetscInt       dim, m, n, p, nc;
190bff4a2f0SMatthew G. Knepley   DMBoundaryType bx, by, bz;
19147c6ae99SBarry Smith   MPI_Comm       comm;
19247c6ae99SBarry Smith   PetscMPIInt    size;
19347c6ae99SBarry Smith   PetscBool      isBAIJ;
19447c6ae99SBarry Smith   DM_DA         *dd = (DM_DA *)da->data;
19547c6ae99SBarry Smith 
19647c6ae99SBarry Smith   PetscFunctionBegin;
19747c6ae99SBarry Smith   /*
19847c6ae99SBarry Smith                                   m
19947c6ae99SBarry Smith           ------------------------------------------------------
20047c6ae99SBarry Smith          |                                                     |
20147c6ae99SBarry Smith          |                                                     |
20247c6ae99SBarry Smith          |               ----------------------                |
20347c6ae99SBarry Smith          |               |                    |                |
20447c6ae99SBarry Smith       n  |           yn  |                    |                |
20547c6ae99SBarry Smith          |               |                    |                |
20647c6ae99SBarry Smith          |               .---------------------                |
20747c6ae99SBarry Smith          |             (xs,ys)     xn                          |
20847c6ae99SBarry Smith          |            .                                        |
20947c6ae99SBarry Smith          |         (gxs,gys)                                   |
21047c6ae99SBarry Smith          |                                                     |
21147c6ae99SBarry Smith           -----------------------------------------------------
21247c6ae99SBarry Smith   */
21347c6ae99SBarry Smith 
21447c6ae99SBarry Smith   /*
21547c6ae99SBarry Smith          nc - number of components per grid point
21647c6ae99SBarry Smith          col - number of colors needed in one direction for single component problem
21747c6ae99SBarry Smith 
21847c6ae99SBarry Smith   */
2199566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, NULL, NULL, NULL, &m, &n, &p, &nc, NULL, &bx, &by, &bz, NULL));
22047c6ae99SBarry Smith 
2219566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
2229566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
2235bdb020cSBarry Smith   if (ctype == IS_COLORING_LOCAL) {
224cc85f647SBarry Smith     PetscCheck(!((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 sides of the domain on the same process");
22547c6ae99SBarry Smith   }
22647c6ae99SBarry Smith 
227aa219208SBarry Smith   /* Tell the DMDA it has 1 degree of freedom per grid point so that the coloring for BAIJ
22847c6ae99SBarry Smith      matrices is for the blocks, not the individual matrix elements  */
2299566063dSJacob Faibussowitsch   PetscCall(PetscStrbeginswith(da->mattype, MATBAIJ, &isBAIJ));
2309566063dSJacob Faibussowitsch   if (!isBAIJ) PetscCall(PetscStrbeginswith(da->mattype, MATMPIBAIJ, &isBAIJ));
2319566063dSJacob Faibussowitsch   if (!isBAIJ) PetscCall(PetscStrbeginswith(da->mattype, MATSEQBAIJ, &isBAIJ));
23247c6ae99SBarry Smith   if (isBAIJ) {
23347c6ae99SBarry Smith     dd->w  = 1;
23447c6ae99SBarry Smith     dd->xs = dd->xs / nc;
23547c6ae99SBarry Smith     dd->xe = dd->xe / nc;
23647c6ae99SBarry Smith     dd->Xs = dd->Xs / nc;
23747c6ae99SBarry Smith     dd->Xe = dd->Xe / nc;
23847c6ae99SBarry Smith   }
23947c6ae99SBarry Smith 
24047c6ae99SBarry Smith   /*
241aa219208SBarry Smith      We do not provide a getcoloring function in the DMDA operations because
2429a1b256bSStefano Zampini    the basic DMDA does not know about matrices. We think of DMDA as being
24347c6ae99SBarry Smith    more low-level then matrices.
24447c6ae99SBarry Smith   */
2451baa6e33SBarry Smith   if (dim == 1) PetscCall(DMCreateColoring_DA_1d_MPIAIJ(da, ctype, coloring));
2461baa6e33SBarry Smith   else if (dim == 2) PetscCall(DMCreateColoring_DA_2d_MPIAIJ(da, ctype, coloring));
2471baa6e33SBarry Smith   else if (dim == 3) PetscCall(DMCreateColoring_DA_3d_MPIAIJ(da, ctype, coloring));
2481baa6e33SBarry 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);
24947c6ae99SBarry Smith   if (isBAIJ) {
25047c6ae99SBarry Smith     dd->w  = nc;
25147c6ae99SBarry Smith     dd->xs = dd->xs * nc;
25247c6ae99SBarry Smith     dd->xe = dd->xe * nc;
25347c6ae99SBarry Smith     dd->Xs = dd->Xs * nc;
25447c6ae99SBarry Smith     dd->Xe = dd->Xe * nc;
25547c6ae99SBarry Smith   }
2563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
25747c6ae99SBarry Smith }
25847c6ae99SBarry Smith 
DMCreateColoring_DA_2d_MPIAIJ(DM da,ISColoringType ctype,ISColoring * coloring)259d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA_2d_MPIAIJ(DM da, ISColoringType ctype, ISColoring *coloring)
260d71ae5a4SJacob Faibussowitsch {
26147c6ae99SBarry Smith   PetscInt         xs, ys, nx, ny, i, j, ii, gxs, gys, gnx, gny, m, n, M, N, dim, s, k, nc, col;
262dec0b466SHong Zhang   PetscInt         ncolors = 0;
26347c6ae99SBarry Smith   MPI_Comm         comm;
264bff4a2f0SMatthew G. Knepley   DMBoundaryType   bx, by;
265aa219208SBarry Smith   DMDAStencilType  st;
26647c6ae99SBarry Smith   ISColoringValue *colors;
26747c6ae99SBarry Smith   DM_DA           *dd = (DM_DA *)da->data;
26847c6ae99SBarry Smith 
26947c6ae99SBarry Smith   PetscFunctionBegin;
27047c6ae99SBarry Smith   /*
27147c6ae99SBarry Smith          nc - number of components per grid point
27247c6ae99SBarry Smith          col - number of colors needed in one direction for single component problem
27347c6ae99SBarry Smith 
27447c6ae99SBarry Smith   */
2759566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, &M, &N, NULL, &nc, &s, &bx, &by, NULL, &st));
27647c6ae99SBarry Smith   col = 2 * s + 1;
2779566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL));
2789566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL));
2799566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
28047c6ae99SBarry Smith 
28147c6ae99SBarry Smith   /* special case as taught to us by Paul Hovland */
282aa219208SBarry Smith   if (st == DMDA_STENCIL_STAR && s == 1) {
2839566063dSJacob Faibussowitsch     PetscCall(DMCreateColoring_DA_2d_5pt_MPIAIJ(da, ctype, coloring));
28447c6ae99SBarry Smith   } else {
28547c6ae99SBarry Smith     if (ctype == IS_COLORING_GLOBAL) {
28647c6ae99SBarry Smith       if (!dd->localcoloring) {
2879566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1(nc * nx * ny, &colors));
28847c6ae99SBarry Smith         ii = 0;
28947c6ae99SBarry Smith         for (j = ys; j < ys + ny; j++) {
29047c6ae99SBarry Smith           for (i = xs; i < xs + nx; i++) {
2916497c311SBarry Smith             for (k = 0; k < nc; k++) PetscCall(ISColoringValueCast(k + nc * ((i % col) + col * (j % col)), colors + ii++));
29247c6ae99SBarry Smith           }
29347c6ae99SBarry Smith         }
29447c6ae99SBarry Smith         ncolors = nc + nc * (col - 1 + col * (col - 1));
2959566063dSJacob Faibussowitsch         PetscCall(ISColoringCreate(comm, ncolors, nc * nx * ny, colors, PETSC_OWN_POINTER, &dd->localcoloring));
29647c6ae99SBarry Smith       }
29747c6ae99SBarry Smith       *coloring = dd->localcoloring;
2985bdb020cSBarry Smith     } else if (ctype == IS_COLORING_LOCAL) {
29947c6ae99SBarry Smith       if (!dd->ghostedcoloring) {
3009566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1(nc * gnx * gny, &colors));
30147c6ae99SBarry Smith         ii = 0;
30247c6ae99SBarry Smith         for (j = gys; j < gys + gny; j++) {
30347c6ae99SBarry Smith           for (i = gxs; i < gxs + gnx; i++) {
30447c6ae99SBarry Smith             for (k = 0; k < nc; k++) {
30547c6ae99SBarry Smith               /* the complicated stuff is to handle periodic boundaries */
3066497c311SBarry Smith               PetscCall(ISColoringValueCast(k + nc * ((SetInRange(i, m) % col) + col * (SetInRange(j, n) % col)), colors + ii++));
30747c6ae99SBarry Smith             }
30847c6ae99SBarry Smith           }
30947c6ae99SBarry Smith         }
31047c6ae99SBarry Smith         ncolors = nc + nc * (col - 1 + col * (col - 1));
3119566063dSJacob Faibussowitsch         PetscCall(ISColoringCreate(comm, ncolors, nc * gnx * gny, colors, PETSC_OWN_POINTER, &dd->ghostedcoloring));
31247c6ae99SBarry Smith         /* PetscIntView(ncolors,(PetscInt*)colors,0); */
31347c6ae99SBarry Smith 
3149566063dSJacob Faibussowitsch         PetscCall(ISColoringSetType(dd->ghostedcoloring, IS_COLORING_LOCAL));
31547c6ae99SBarry Smith       }
31647c6ae99SBarry Smith       *coloring = dd->ghostedcoloring;
31798921bdaSJacob Faibussowitsch     } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_ARG_WRONG, "Unknown ISColoringType %d", (int)ctype);
31847c6ae99SBarry Smith   }
3199566063dSJacob Faibussowitsch   PetscCall(ISColoringReference(*coloring));
3203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
32147c6ae99SBarry Smith }
32247c6ae99SBarry Smith 
DMCreateColoring_DA_3d_MPIAIJ(DM da,ISColoringType ctype,ISColoring * coloring)323d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA_3d_MPIAIJ(DM da, ISColoringType ctype, ISColoring *coloring)
324d71ae5a4SJacob Faibussowitsch {
32547c6ae99SBarry 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;
32647c6ae99SBarry Smith   PetscInt         ncolors;
32747c6ae99SBarry Smith   MPI_Comm         comm;
328bff4a2f0SMatthew G. Knepley   DMBoundaryType   bx, by, bz;
329aa219208SBarry Smith   DMDAStencilType  st;
33047c6ae99SBarry Smith   ISColoringValue *colors;
33147c6ae99SBarry Smith   DM_DA           *dd = (DM_DA *)da->data;
33247c6ae99SBarry Smith 
33347c6ae99SBarry Smith   PetscFunctionBegin;
33447c6ae99SBarry Smith   /*
33547c6ae99SBarry Smith          nc - number of components per grid point
33647c6ae99SBarry Smith          col - number of colors needed in one direction for single component problem
33747c6ae99SBarry Smith   */
3389566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &nc, &s, &bx, &by, &bz, &st));
33947c6ae99SBarry Smith   col = 2 * s + 1;
34000045ab3SPierre Jolivet   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 by 2*stencil_width + 1");
34100045ab3SPierre Jolivet   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 by 2*stencil_width + 1");
34200045ab3SPierre Jolivet   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 by 2*stencil_width + 1");
343494b1ccaSBarry Smith 
3449566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz));
3459566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz));
3469566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
34747c6ae99SBarry Smith 
34847c6ae99SBarry Smith   /* create the coloring */
34947c6ae99SBarry Smith   if (ctype == IS_COLORING_GLOBAL) {
35047c6ae99SBarry Smith     if (!dd->localcoloring) {
3519566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nc * nx * ny * nz, &colors));
35247c6ae99SBarry Smith       ii = 0;
35347c6ae99SBarry Smith       for (k = zs; k < zs + nz; k++) {
35447c6ae99SBarry Smith         for (j = ys; j < ys + ny; j++) {
35547c6ae99SBarry Smith           for (i = xs; i < xs + nx; i++) {
3566497c311SBarry Smith             for (l = 0; l < nc; l++) PetscCall(ISColoringValueCast(l + nc * ((i % col) + col * (j % col) + col * col * (k % col)), colors + ii++));
35747c6ae99SBarry Smith           }
35847c6ae99SBarry Smith         }
35947c6ae99SBarry Smith       }
36047c6ae99SBarry Smith       ncolors = nc + nc * (col - 1 + col * (col - 1) + col * col * (col - 1));
3619566063dSJacob Faibussowitsch       PetscCall(ISColoringCreate(comm, ncolors, nc * nx * ny * nz, colors, PETSC_OWN_POINTER, &dd->localcoloring));
36247c6ae99SBarry Smith     }
36347c6ae99SBarry Smith     *coloring = dd->localcoloring;
3645bdb020cSBarry Smith   } else if (ctype == IS_COLORING_LOCAL) {
36547c6ae99SBarry Smith     if (!dd->ghostedcoloring) {
3669566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nc * gnx * gny * gnz, &colors));
36747c6ae99SBarry Smith       ii = 0;
36847c6ae99SBarry Smith       for (k = gzs; k < gzs + gnz; k++) {
36947c6ae99SBarry Smith         for (j = gys; j < gys + gny; j++) {
37047c6ae99SBarry Smith           for (i = gxs; i < gxs + gnx; i++) {
37147c6ae99SBarry Smith             for (l = 0; l < nc; l++) {
37247c6ae99SBarry Smith               /* the complicated stuff is to handle periodic boundaries */
3736497c311SBarry Smith               PetscCall(ISColoringValueCast(l + nc * ((SetInRange(i, m) % col) + col * (SetInRange(j, n) % col) + col * col * (SetInRange(k, p) % col)), colors + ii++));
37447c6ae99SBarry Smith             }
37547c6ae99SBarry Smith           }
37647c6ae99SBarry Smith         }
37747c6ae99SBarry Smith       }
37847c6ae99SBarry Smith       ncolors = nc + nc * (col - 1 + col * (col - 1) + col * col * (col - 1));
3799566063dSJacob Faibussowitsch       PetscCall(ISColoringCreate(comm, ncolors, nc * gnx * gny * gnz, colors, PETSC_OWN_POINTER, &dd->ghostedcoloring));
3809566063dSJacob Faibussowitsch       PetscCall(ISColoringSetType(dd->ghostedcoloring, IS_COLORING_LOCAL));
38147c6ae99SBarry Smith     }
38247c6ae99SBarry Smith     *coloring = dd->ghostedcoloring;
38398921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_ARG_WRONG, "Unknown ISColoringType %d", (int)ctype);
3849566063dSJacob Faibussowitsch   PetscCall(ISColoringReference(*coloring));
3853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
38647c6ae99SBarry Smith }
38747c6ae99SBarry Smith 
DMCreateColoring_DA_1d_MPIAIJ(DM da,ISColoringType ctype,ISColoring * coloring)388d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA_1d_MPIAIJ(DM da, ISColoringType ctype, ISColoring *coloring)
389d71ae5a4SJacob Faibussowitsch {
39047c6ae99SBarry Smith   PetscInt         xs, nx, i, i1, gxs, gnx, l, m, M, dim, s, nc, col;
39147c6ae99SBarry Smith   PetscInt         ncolors;
39247c6ae99SBarry Smith   MPI_Comm         comm;
393bff4a2f0SMatthew G. Knepley   DMBoundaryType   bx;
39447c6ae99SBarry Smith   ISColoringValue *colors;
39547c6ae99SBarry Smith   DM_DA           *dd = (DM_DA *)da->data;
39647c6ae99SBarry Smith 
39747c6ae99SBarry Smith   PetscFunctionBegin;
39847c6ae99SBarry Smith   /*
39947c6ae99SBarry Smith          nc - number of components per grid point
40047c6ae99SBarry Smith          col - number of colors needed in one direction for single component problem
40147c6ae99SBarry Smith   */
4029566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, NULL, NULL, &M, NULL, NULL, &nc, &s, &bx, NULL, NULL, NULL));
40347c6ae99SBarry Smith   col = 2 * s + 1;
404cc85f647SBarry Smith   PetscCheck(bx != DM_BOUNDARY_PERIODIC || !(m % col), PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "IS_COLORING_GLOBAL can only be used for periodic boundary conditions if the number of grid points %" PetscInt_FMT " is divisible by the number of colors %" PetscInt_FMT, m, col);
4059566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &nx, NULL, NULL));
4069566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, NULL, NULL, &gnx, NULL, NULL));
4079566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
40847c6ae99SBarry Smith 
40947c6ae99SBarry Smith   /* create the coloring */
41047c6ae99SBarry Smith   if (ctype == IS_COLORING_GLOBAL) {
41147c6ae99SBarry Smith     if (!dd->localcoloring) {
4129566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nc * nx, &colors));
413ae4f298aSBarry Smith       if (dd->ofillcols) {
414ae4f298aSBarry Smith         PetscInt tc = 0;
415ae4f298aSBarry Smith         for (i = 0; i < nc; i++) tc += (PetscInt)(dd->ofillcols[i] > 0);
416ae4f298aSBarry Smith         i1 = 0;
417ae4f298aSBarry Smith         for (i = xs; i < xs + nx; i++) {
418ae4f298aSBarry Smith           for (l = 0; l < nc; l++) {
419ae4f298aSBarry Smith             if (dd->ofillcols[l] && (i % col)) {
4206497c311SBarry Smith               PetscCall(ISColoringValueCast(nc - 1 + tc * ((i % col) - 1) + dd->ofillcols[l], colors + i1++));
421ae4f298aSBarry Smith             } else {
4226497c311SBarry Smith               PetscCall(ISColoringValueCast(l, colors + i1++));
423ae4f298aSBarry Smith             }
424ae4f298aSBarry Smith           }
425ae4f298aSBarry Smith         }
426ae4f298aSBarry Smith         ncolors = nc + 2 * s * tc;
427ae4f298aSBarry Smith       } else {
42847c6ae99SBarry Smith         i1 = 0;
42947c6ae99SBarry Smith         for (i = xs; i < xs + nx; i++) {
4306497c311SBarry Smith           for (l = 0; l < nc; l++) PetscCall(ISColoringValueCast(l + nc * (i % col), colors + i1++));
43147c6ae99SBarry Smith         }
43247c6ae99SBarry Smith         ncolors = nc + nc * (col - 1);
433ae4f298aSBarry Smith       }
4349566063dSJacob Faibussowitsch       PetscCall(ISColoringCreate(comm, ncolors, nc * nx, colors, PETSC_OWN_POINTER, &dd->localcoloring));
43547c6ae99SBarry Smith     }
43647c6ae99SBarry Smith     *coloring = dd->localcoloring;
4375bdb020cSBarry Smith   } else if (ctype == IS_COLORING_LOCAL) {
43847c6ae99SBarry Smith     if (!dd->ghostedcoloring) {
4399566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nc * gnx, &colors));
44047c6ae99SBarry Smith       i1 = 0;
44147c6ae99SBarry Smith       for (i = gxs; i < gxs + gnx; i++) {
44247c6ae99SBarry Smith         for (l = 0; l < nc; l++) {
44347c6ae99SBarry Smith           /* the complicated stuff is to handle periodic boundaries */
4446497c311SBarry Smith           PetscCall(ISColoringValueCast(l + nc * (SetInRange(i, m) % col), colors + i1++));
44547c6ae99SBarry Smith         }
44647c6ae99SBarry Smith       }
44747c6ae99SBarry Smith       ncolors = nc + nc * (col - 1);
4489566063dSJacob Faibussowitsch       PetscCall(ISColoringCreate(comm, ncolors, nc * gnx, colors, PETSC_OWN_POINTER, &dd->ghostedcoloring));
4499566063dSJacob Faibussowitsch       PetscCall(ISColoringSetType(dd->ghostedcoloring, IS_COLORING_LOCAL));
45047c6ae99SBarry Smith     }
45147c6ae99SBarry Smith     *coloring = dd->ghostedcoloring;
45298921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_ARG_WRONG, "Unknown ISColoringType %d", (int)ctype);
4539566063dSJacob Faibussowitsch   PetscCall(ISColoringReference(*coloring));
4543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
45547c6ae99SBarry Smith }
45647c6ae99SBarry Smith 
DMCreateColoring_DA_2d_5pt_MPIAIJ(DM da,ISColoringType ctype,ISColoring * coloring)457d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA_2d_5pt_MPIAIJ(DM da, ISColoringType ctype, ISColoring *coloring)
458d71ae5a4SJacob Faibussowitsch {
45947c6ae99SBarry Smith   PetscInt         xs, ys, nx, ny, i, j, ii, gxs, gys, gnx, gny, m, n, dim, s, k, nc;
46047c6ae99SBarry Smith   PetscInt         ncolors;
46147c6ae99SBarry Smith   MPI_Comm         comm;
462bff4a2f0SMatthew G. Knepley   DMBoundaryType   bx, by;
46347c6ae99SBarry Smith   ISColoringValue *colors;
46447c6ae99SBarry Smith   DM_DA           *dd = (DM_DA *)da->data;
46547c6ae99SBarry Smith 
46647c6ae99SBarry Smith   PetscFunctionBegin;
46747c6ae99SBarry Smith   /*
46847c6ae99SBarry Smith          nc - number of components per grid point
46947c6ae99SBarry Smith          col - number of colors needed in one direction for single component problem
47047c6ae99SBarry Smith   */
4719566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, NULL, NULL, NULL, &nc, &s, &bx, &by, NULL, NULL));
4729566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL));
4739566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL));
4749566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
47547c6ae99SBarry Smith   /* create the coloring */
47647c6ae99SBarry Smith   if (ctype == IS_COLORING_GLOBAL) {
47747c6ae99SBarry Smith     if (!dd->localcoloring) {
4789566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nc * nx * ny, &colors));
47947c6ae99SBarry Smith       ii = 0;
48047c6ae99SBarry Smith       for (j = ys; j < ys + ny; j++) {
48147c6ae99SBarry Smith         for (i = xs; i < xs + nx; i++) {
4826497c311SBarry Smith           for (k = 0; k < nc; k++) PetscCall(ISColoringValueCast(k + nc * ((3 * j + i) % 5), colors + ii++));
48347c6ae99SBarry Smith         }
48447c6ae99SBarry Smith       }
48547c6ae99SBarry Smith       ncolors = 5 * nc;
4869566063dSJacob Faibussowitsch       PetscCall(ISColoringCreate(comm, ncolors, nc * nx * ny, colors, PETSC_OWN_POINTER, &dd->localcoloring));
48747c6ae99SBarry Smith     }
48847c6ae99SBarry Smith     *coloring = dd->localcoloring;
4895bdb020cSBarry Smith   } else if (ctype == IS_COLORING_LOCAL) {
49047c6ae99SBarry Smith     if (!dd->ghostedcoloring) {
4919566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nc * gnx * gny, &colors));
49247c6ae99SBarry Smith       ii = 0;
49347c6ae99SBarry Smith       for (j = gys; j < gys + gny; j++) {
49447c6ae99SBarry Smith         for (i = gxs; i < gxs + gnx; i++) {
4956497c311SBarry Smith           for (k = 0; k < nc; k++) PetscCall(ISColoringValueCast(k + nc * ((3 * SetInRange(j, n) + SetInRange(i, m)) % 5), colors + ii++));
49647c6ae99SBarry Smith         }
49747c6ae99SBarry Smith       }
49847c6ae99SBarry Smith       ncolors = 5 * nc;
4999566063dSJacob Faibussowitsch       PetscCall(ISColoringCreate(comm, ncolors, nc * gnx * gny, colors, PETSC_OWN_POINTER, &dd->ghostedcoloring));
5009566063dSJacob Faibussowitsch       PetscCall(ISColoringSetType(dd->ghostedcoloring, IS_COLORING_LOCAL));
50147c6ae99SBarry Smith     }
50247c6ae99SBarry Smith     *coloring = dd->ghostedcoloring;
50398921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_ARG_WRONG, "Unknown ISColoringType %d", (int)ctype);
5043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
50547c6ae99SBarry Smith }
50647c6ae99SBarry Smith 
50747c6ae99SBarry Smith /* =========================================================================== */
508e432b41dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_1d_MPIAIJ(DM, Mat);
509ce308e1dSBarry Smith extern PetscErrorCode DMCreateMatrix_DA_1d_MPIAIJ_Fill(DM, Mat);
510e432b41dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_1d_SeqAIJ_NoPreallocation(DM, Mat);
511e432b41dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_2d_MPIAIJ(DM, Mat);
512950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_2d_MPIAIJ_Fill(DM, Mat);
513e432b41dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_3d_MPIAIJ(DM, Mat);
514950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_3d_MPIAIJ_Fill(DM, Mat);
515950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_2d_MPIBAIJ(DM, Mat);
516950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_3d_MPIBAIJ(DM, Mat);
517950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_2d_MPISBAIJ(DM, Mat);
518950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_3d_MPISBAIJ(DM, Mat);
519d4002b98SHong Zhang extern PetscErrorCode DMCreateMatrix_DA_2d_MPISELL(DM, Mat);
520d4002b98SHong Zhang extern PetscErrorCode DMCreateMatrix_DA_3d_MPISELL(DM, Mat);
521e584696dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_IS(DM, Mat);
52247c6ae99SBarry Smith 
MatView_MPI_DA(Mat A,PetscViewer viewer)523a4e35b19SJacob Faibussowitsch static PetscErrorCode MatView_MPI_DA(Mat A, PetscViewer viewer)
524d71ae5a4SJacob Faibussowitsch {
5259a42bb27SBarry Smith   DM                da;
52647c6ae99SBarry Smith   const char       *prefix;
5272d1451d4SHong Zhang   Mat               AA, Anatural;
52847c6ae99SBarry Smith   AO                ao;
52947c6ae99SBarry Smith   PetscInt          rstart, rend, *petsc, i;
53047c6ae99SBarry Smith   IS                is;
53147c6ae99SBarry Smith   MPI_Comm          comm;
53274388724SJed Brown   PetscViewerFormat format;
5332d1451d4SHong Zhang   PetscBool         flag;
53447c6ae99SBarry Smith 
53547c6ae99SBarry Smith   PetscFunctionBegin;
53674388724SJed Brown   /* Check whether we are just printing info, in which case MatView() already viewed everything we wanted to view */
5379566063dSJacob Faibussowitsch   PetscCall(PetscViewerGetFormat(viewer, &format));
5383ba16761SJacob Faibussowitsch   if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) PetscFunctionReturn(PETSC_SUCCESS);
53974388724SJed Brown 
5409566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)A, &comm));
5419566063dSJacob Faibussowitsch   PetscCall(MatGetDM(A, &da));
5427a8be351SBarry Smith   PetscCheck(da, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Matrix not generated from a DMDA");
54347c6ae99SBarry Smith 
5449566063dSJacob Faibussowitsch   PetscCall(DMDAGetAO(da, &ao));
5459566063dSJacob Faibussowitsch   PetscCall(MatGetOwnershipRange(A, &rstart, &rend));
5469566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(rend - rstart, &petsc));
54747c6ae99SBarry Smith   for (i = rstart; i < rend; i++) petsc[i - rstart] = i;
5489566063dSJacob Faibussowitsch   PetscCall(AOApplicationToPetsc(ao, rend - rstart, petsc));
5499566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(comm, rend - rstart, petsc, PETSC_OWN_POINTER, &is));
55047c6ae99SBarry Smith 
55147c6ae99SBarry Smith   /* call viewer on natural ordering */
5522d1451d4SHong Zhang   PetscCall(PetscObjectBaseTypeCompare((PetscObject)A, MATMPISELL, &flag));
5532d1451d4SHong Zhang   if (flag) {
5542d1451d4SHong Zhang     PetscCall(MatConvert(A, MATAIJ, MAT_INITIAL_MATRIX, &AA));
5552d1451d4SHong Zhang     A = AA;
5562d1451d4SHong Zhang   }
5579566063dSJacob Faibussowitsch   PetscCall(MatCreateSubMatrix(A, is, is, MAT_INITIAL_MATRIX, &Anatural));
5589566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&is));
5599566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)A, &prefix));
5609566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)Anatural, prefix));
5619566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)Anatural, ((PetscObject)A)->name));
562f0ed2f47SStefano Zampini   ((PetscObject)Anatural)->donotPetscObjectPrintClassNamePrefixType = PETSC_TRUE;
5639566063dSJacob Faibussowitsch   PetscCall(MatView(Anatural, viewer));
564f0ed2f47SStefano Zampini   ((PetscObject)Anatural)->donotPetscObjectPrintClassNamePrefixType = PETSC_FALSE;
5659566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&Anatural));
5662d1451d4SHong Zhang   if (flag) PetscCall(MatDestroy(&AA));
5673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
56847c6ae99SBarry Smith }
56947c6ae99SBarry Smith 
MatLoad_MPI_DA(Mat A,PetscViewer viewer)570a4e35b19SJacob Faibussowitsch static PetscErrorCode MatLoad_MPI_DA(Mat A, PetscViewer viewer)
571d71ae5a4SJacob Faibussowitsch {
5729a42bb27SBarry Smith   DM       da;
57347c6ae99SBarry Smith   Mat      Anatural, Aapp;
57447c6ae99SBarry Smith   AO       ao;
575539c167fSBarry Smith   PetscInt rstart, rend, *app, i, m, n, M, N;
57647c6ae99SBarry Smith   IS       is;
57747c6ae99SBarry Smith   MPI_Comm comm;
57847c6ae99SBarry Smith 
57947c6ae99SBarry Smith   PetscFunctionBegin;
5809566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)A, &comm));
5819566063dSJacob Faibussowitsch   PetscCall(MatGetDM(A, &da));
5827a8be351SBarry Smith   PetscCheck(da, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Matrix not generated from a DMDA");
58347c6ae99SBarry Smith 
58447c6ae99SBarry Smith   /* Load the matrix in natural ordering */
5859566063dSJacob Faibussowitsch   PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &Anatural));
5869566063dSJacob Faibussowitsch   PetscCall(MatSetType(Anatural, ((PetscObject)A)->type_name));
5879566063dSJacob Faibussowitsch   PetscCall(MatGetSize(A, &M, &N));
5889566063dSJacob Faibussowitsch   PetscCall(MatGetLocalSize(A, &m, &n));
5899566063dSJacob Faibussowitsch   PetscCall(MatSetSizes(Anatural, m, n, M, N));
5909566063dSJacob Faibussowitsch   PetscCall(MatLoad(Anatural, viewer));
59147c6ae99SBarry Smith 
59247c6ae99SBarry Smith   /* Map natural ordering to application ordering and create IS */
5939566063dSJacob Faibussowitsch   PetscCall(DMDAGetAO(da, &ao));
5949566063dSJacob Faibussowitsch   PetscCall(MatGetOwnershipRange(Anatural, &rstart, &rend));
5959566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(rend - rstart, &app));
59647c6ae99SBarry Smith   for (i = rstart; i < rend; i++) app[i - rstart] = i;
5979566063dSJacob Faibussowitsch   PetscCall(AOPetscToApplication(ao, rend - rstart, app));
5989566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(comm, rend - rstart, app, PETSC_OWN_POINTER, &is));
59947c6ae99SBarry Smith 
60047c6ae99SBarry Smith   /* Do permutation and replace header */
6019566063dSJacob Faibussowitsch   PetscCall(MatCreateSubMatrix(Anatural, is, is, MAT_INITIAL_MATRIX, &Aapp));
6029566063dSJacob Faibussowitsch   PetscCall(MatHeaderReplace(A, &Aapp));
6039566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&is));
6049566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&Anatural));
6053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
60647c6ae99SBarry Smith }
60747c6ae99SBarry Smith 
DMCreateMatrix_DA(DM da,Mat * J)608d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA(DM da, Mat *J)
609d71ae5a4SJacob Faibussowitsch {
61047c6ae99SBarry Smith   PetscInt    dim, dof, nx, ny, nz, dims[3], starts[3], M, N, P;
61147c6ae99SBarry Smith   Mat         A;
61247c6ae99SBarry Smith   MPI_Comm    comm;
61319fd82e9SBarry Smith   MatType     Atype;
614b412c318SBarry Smith   MatType     mtype;
61547c6ae99SBarry Smith   PetscMPIInt size;
61647c6ae99SBarry Smith   DM_DA      *dd  = (DM_DA *)da->data;
6170cd8b6e2SStefano Zampini   PetscBool   aij = PETSC_FALSE, baij = PETSC_FALSE, sbaij = PETSC_FALSE, sell = PETSC_FALSE, is = PETSC_FALSE;
61847c6ae99SBarry Smith 
61947c6ae99SBarry Smith   PetscFunctionBegin;
6209566063dSJacob Faibussowitsch   PetscCall(MatInitializePackage());
621b412c318SBarry Smith   mtype = da->mattype;
62247c6ae99SBarry Smith 
62347c6ae99SBarry Smith   /*
62447c6ae99SBarry Smith                                   m
62547c6ae99SBarry Smith           ------------------------------------------------------
62647c6ae99SBarry Smith          |                                                     |
62747c6ae99SBarry Smith          |                                                     |
62847c6ae99SBarry Smith          |               ----------------------                |
62947c6ae99SBarry Smith          |               |                    |                |
63047c6ae99SBarry Smith       n  |           ny  |                    |                |
63147c6ae99SBarry Smith          |               |                    |                |
63247c6ae99SBarry Smith          |               .---------------------                |
63347c6ae99SBarry Smith          |             (xs,ys)     nx                          |
63447c6ae99SBarry Smith          |            .                                        |
63547c6ae99SBarry Smith          |         (gxs,gys)                                   |
63647c6ae99SBarry Smith          |                                                     |
63747c6ae99SBarry Smith           -----------------------------------------------------
63847c6ae99SBarry Smith   */
63947c6ae99SBarry Smith 
64047c6ae99SBarry Smith   /*
64147c6ae99SBarry Smith          nc - number of components per grid point
64247c6ae99SBarry Smith          col - number of colors needed in one direction for single component problem
64347c6ae99SBarry Smith   */
644e30e807fSPeter Brune   M   = dd->M;
645e30e807fSPeter Brune   N   = dd->N;
646e30e807fSPeter Brune   P   = dd->P;
647c73cfb54SMatthew G. Knepley   dim = da->dim;
648e30e807fSPeter Brune   dof = dd->w;
6499566063dSJacob Faibussowitsch   /* PetscCall(DMDAGetInfo(da,&dim,&M,&N,&P,NULL,NULL,NULL,&dof,NULL,NULL,NULL,NULL,NULL)); */
6509566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, NULL, NULL, NULL, &nx, &ny, &nz));
6519566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
6529566063dSJacob Faibussowitsch   PetscCall(MatCreate(comm, &A));
6539566063dSJacob Faibussowitsch   PetscCall(MatSetSizes(A, dof * nx * ny * nz, dof * nx * ny * nz, dof * M * N * P, dof * M * N * P));
6549566063dSJacob Faibussowitsch   PetscCall(MatSetType(A, mtype));
6559566063dSJacob Faibussowitsch   PetscCall(MatSetFromOptions(A));
65674427ab1SRichard Tran Mills   if (dof * nx * ny * nz < da->bind_below) {
6579566063dSJacob Faibussowitsch     PetscCall(MatSetBindingPropagates(A, PETSC_TRUE));
6589566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(A, PETSC_TRUE));
65974427ab1SRichard Tran Mills   }
6609566063dSJacob Faibussowitsch   PetscCall(MatSetDM(A, da));
6611baa6e33SBarry Smith   if (da->structure_only) PetscCall(MatSetOption(A, MAT_STRUCTURE_ONLY, PETSC_TRUE));
6629566063dSJacob Faibussowitsch   PetscCall(MatGetType(A, &Atype));
66347c6ae99SBarry Smith   /*
664aa219208SBarry Smith      We do not provide a getmatrix function in the DMDA operations because
665aa219208SBarry Smith    the basic DMDA does not know about matrices. We think of DMDA as being more
66647c6ae99SBarry Smith    more low-level than matrices. This is kind of cheating but, cause sometimes
667aa219208SBarry Smith    we think of DMDA has higher level than matrices.
66847c6ae99SBarry Smith 
66947c6ae99SBarry Smith      We could switch based on Atype (or mtype), but we do not since the
670844bd0d7SStefano Zampini    specialized setting routines depend only on the particular preallocation
67147c6ae99SBarry Smith    details of the matrix, not the type itself.
67247c6ae99SBarry Smith   */
673d7dabc60SJed Brown   if (!da->prealloc_skip) { // Flag is likely set when user intends to use MatSetPreallocationCOO()
6740cd8b6e2SStefano Zampini     PetscCall(PetscObjectHasFunction((PetscObject)A, "MatMPIAIJSetPreallocation_C", &aij));
6750cd8b6e2SStefano Zampini     if (!aij) PetscCall(PetscObjectHasFunction((PetscObject)A, "MatSeqAIJSetPreallocation_C", &aij));
67647c6ae99SBarry Smith     if (!aij) {
6770cd8b6e2SStefano Zampini       PetscCall(PetscObjectHasFunction((PetscObject)A, "MatMPIBAIJSetPreallocation_C", &baij));
6780cd8b6e2SStefano Zampini       if (!baij) PetscCall(PetscObjectHasFunction((PetscObject)A, "MatSeqBAIJSetPreallocation_C", &baij));
67947c6ae99SBarry Smith       if (!baij) {
6800cd8b6e2SStefano Zampini         PetscCall(PetscObjectHasFunction((PetscObject)A, "MatMPISBAIJSetPreallocation_C", &sbaij));
6810cd8b6e2SStefano Zampini         if (!sbaij) PetscCall(PetscObjectHasFunction((PetscObject)A, "MatSeqSBAIJSetPreallocation_C", &sbaij));
6825e26d47bSHong Zhang         if (!sbaij) {
6830cd8b6e2SStefano Zampini           PetscCall(PetscObjectHasFunction((PetscObject)A, "MatMPISELLSetPreallocation_C", &sell));
6840cd8b6e2SStefano Zampini           if (!sell) PetscCall(PetscObjectHasFunction((PetscObject)A, "MatSeqSELLSetPreallocation_C", &sell));
6855e26d47bSHong Zhang         }
6860cd8b6e2SStefano Zampini         if (!sell) PetscCall(PetscObjectHasFunction((PetscObject)A, "MatISSetPreallocation_C", &is));
68747c6ae99SBarry Smith       }
68847c6ae99SBarry Smith     }
689d7dabc60SJed Brown   }
69047c6ae99SBarry Smith   if (aij) {
69147c6ae99SBarry Smith     if (dim == 1) {
692ce308e1dSBarry Smith       if (dd->ofill) {
6939566063dSJacob Faibussowitsch         PetscCall(DMCreateMatrix_DA_1d_MPIAIJ_Fill(da, A));
694ce308e1dSBarry Smith       } else {
69519b08ed1SBarry Smith         DMBoundaryType bx;
69619b08ed1SBarry Smith         PetscMPIInt    size;
6979566063dSJacob Faibussowitsch         PetscCall(DMDAGetInfo(da, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &bx, NULL, NULL, NULL));
6989566063dSJacob Faibussowitsch         PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)da), &size));
69919b08ed1SBarry Smith         if (size == 1 && bx == DM_BOUNDARY_NONE) {
7009566063dSJacob Faibussowitsch           PetscCall(DMCreateMatrix_DA_1d_SeqAIJ_NoPreallocation(da, A));
70119b08ed1SBarry Smith         } else {
7029566063dSJacob Faibussowitsch           PetscCall(DMCreateMatrix_DA_1d_MPIAIJ(da, A));
703ce308e1dSBarry Smith         }
70419b08ed1SBarry Smith       }
70547c6ae99SBarry Smith     } else if (dim == 2) {
70647c6ae99SBarry Smith       if (dd->ofill) {
7079566063dSJacob Faibussowitsch         PetscCall(DMCreateMatrix_DA_2d_MPIAIJ_Fill(da, A));
70847c6ae99SBarry Smith       } else {
7099566063dSJacob Faibussowitsch         PetscCall(DMCreateMatrix_DA_2d_MPIAIJ(da, A));
71047c6ae99SBarry Smith       }
71147c6ae99SBarry Smith     } else if (dim == 3) {
71247c6ae99SBarry Smith       if (dd->ofill) {
7139566063dSJacob Faibussowitsch         PetscCall(DMCreateMatrix_DA_3d_MPIAIJ_Fill(da, A));
71447c6ae99SBarry Smith       } else {
7159566063dSJacob Faibussowitsch         PetscCall(DMCreateMatrix_DA_3d_MPIAIJ(da, A));
71647c6ae99SBarry Smith       }
71747c6ae99SBarry Smith     }
71847c6ae99SBarry Smith   } else if (baij) {
71947c6ae99SBarry Smith     if (dim == 2) {
7209566063dSJacob Faibussowitsch       PetscCall(DMCreateMatrix_DA_2d_MPIBAIJ(da, A));
72147c6ae99SBarry Smith     } else if (dim == 3) {
7229566063dSJacob Faibussowitsch       PetscCall(DMCreateMatrix_DA_3d_MPIBAIJ(da, A));
72363a3b9bcSJacob 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);
72447c6ae99SBarry Smith   } else if (sbaij) {
72547c6ae99SBarry Smith     if (dim == 2) {
7269566063dSJacob Faibussowitsch       PetscCall(DMCreateMatrix_DA_2d_MPISBAIJ(da, A));
72747c6ae99SBarry Smith     } else if (dim == 3) {
7289566063dSJacob Faibussowitsch       PetscCall(DMCreateMatrix_DA_3d_MPISBAIJ(da, A));
72963a3b9bcSJacob 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);
730d4002b98SHong Zhang   } else if (sell) {
7315e26d47bSHong Zhang     if (dim == 2) {
7329566063dSJacob Faibussowitsch       PetscCall(DMCreateMatrix_DA_2d_MPISELL(da, A));
733711261dbSHong Zhang     } else if (dim == 3) {
7349566063dSJacob Faibussowitsch       PetscCall(DMCreateMatrix_DA_3d_MPISELL(da, A));
73563a3b9bcSJacob 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);
736e584696dSStefano Zampini   } else if (is) {
7379566063dSJacob Faibussowitsch     PetscCall(DMCreateMatrix_DA_IS(da, A));
738d7dabc60SJed Brown   } else { // unknown type or da->prealloc_skip so structural information may be needed, but no prealloc
73945b6f7e9SBarry Smith     ISLocalToGlobalMapping ltog;
740e584696dSStefano Zampini 
7419566063dSJacob Faibussowitsch     PetscCall(MatSetBlockSize(A, dof));
7429566063dSJacob Faibussowitsch     PetscCall(MatSetUp(A));
7439566063dSJacob Faibussowitsch     PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
7449566063dSJacob Faibussowitsch     PetscCall(MatSetLocalToGlobalMapping(A, ltog, ltog));
74547c6ae99SBarry Smith   }
7469566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &starts[0], &starts[1], &starts[2], &dims[0], &dims[1], &dims[2]));
7479566063dSJacob Faibussowitsch   PetscCall(MatSetStencil(A, dim, dims, starts, dof));
7489566063dSJacob Faibussowitsch   PetscCall(MatSetDM(A, da));
7499566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
75047c6ae99SBarry Smith   if (size > 1) {
75147c6ae99SBarry Smith     /* change viewer to display matrix in natural ordering */
752*57d50842SBarry Smith     PetscCall(MatSetOperation(A, MATOP_VIEW, (PetscErrorCodeFn *)MatView_MPI_DA));
753*57d50842SBarry Smith     PetscCall(MatSetOperation(A, MATOP_LOAD, (PetscErrorCodeFn *)MatLoad_MPI_DA));
75447c6ae99SBarry Smith   }
75547c6ae99SBarry Smith   *J = A;
7563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
75747c6ae99SBarry Smith }
75847c6ae99SBarry Smith 
DMCreateMatrix_DA_IS(DM dm,Mat J)759d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_IS(DM dm, Mat J)
760d71ae5a4SJacob Faibussowitsch {
761e584696dSStefano Zampini   DM_DA                 *da = (DM_DA *)dm->data;
762e432b41dSStefano Zampini   Mat                    lJ, P;
763e584696dSStefano Zampini   ISLocalToGlobalMapping ltog;
764e432b41dSStefano Zampini   IS                     is;
765e432b41dSStefano Zampini   PetscBT                bt;
76605339c03SStefano Zampini   const PetscInt        *e_loc, *idx;
767e432b41dSStefano Zampini   PetscInt               i, nel, nen, nv, dof, *gidx, n, N;
768e584696dSStefano Zampini 
769e584696dSStefano Zampini   /* The l2g map of DMDA has all ghosted nodes, and e_loc is a subset of all the local nodes (including the ghosted)
770e432b41dSStefano Zampini      We need to filter out the local indices that are not represented through the DMDAGetElements decomposition */
771e584696dSStefano Zampini   PetscFunctionBegin;
772e584696dSStefano Zampini   dof = da->w;
7739566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, dof));
7749566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(dm, &ltog));
775e432b41dSStefano Zampini 
776e432b41dSStefano Zampini   /* flag local elements indices in local DMDA numbering */
7779566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingGetSize(ltog, &nv));
7789566063dSJacob Faibussowitsch   PetscCall(PetscBTCreate(nv / dof, &bt));
7799566063dSJacob Faibussowitsch   PetscCall(DMDAGetElements(dm, &nel, &nen, &e_loc)); /* this will throw an error if the stencil type is not DMDA_STENCIL_BOX */
7809566063dSJacob Faibussowitsch   for (i = 0; i < nel * nen; i++) PetscCall(PetscBTSet(bt, e_loc[i]));
781e432b41dSStefano Zampini 
782e432b41dSStefano Zampini   /* filter out (set to -1) the global indices not used by the local elements */
7839566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(nv / dof, &gidx));
7849566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingGetBlockIndices(ltog, &idx));
7859566063dSJacob Faibussowitsch   PetscCall(PetscArraycpy(gidx, idx, nv / dof));
7869566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingRestoreBlockIndices(ltog, &idx));
7879371c9d4SSatish Balay   for (i = 0; i < nv / dof; i++)
7889371c9d4SSatish Balay     if (!PetscBTLookup(bt, i)) gidx[i] = -1;
7899566063dSJacob Faibussowitsch   PetscCall(PetscBTDestroy(&bt));
7909566063dSJacob Faibussowitsch   PetscCall(ISCreateBlock(PetscObjectComm((PetscObject)dm), dof, nv / dof, gidx, PETSC_OWN_POINTER, &is));
7919566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingCreateIS(is, &ltog));
7929566063dSJacob Faibussowitsch   PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
7939566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingDestroy(&ltog));
7949566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&is));
79505339c03SStefano Zampini 
796e432b41dSStefano Zampini   /* Preallocation */
797e306f867SJed Brown   if (dm->prealloc_skip) {
7989566063dSJacob Faibussowitsch     PetscCall(MatSetUp(J));
799e306f867SJed Brown   } else {
8009566063dSJacob Faibussowitsch     PetscCall(MatISGetLocalMat(J, &lJ));
8019566063dSJacob Faibussowitsch     PetscCall(MatGetLocalToGlobalMapping(lJ, &ltog, NULL));
8029566063dSJacob Faibussowitsch     PetscCall(MatCreate(PetscObjectComm((PetscObject)lJ), &P));
8039566063dSJacob Faibussowitsch     PetscCall(MatSetType(P, MATPREALLOCATOR));
8049566063dSJacob Faibussowitsch     PetscCall(MatSetLocalToGlobalMapping(P, ltog, ltog));
8059566063dSJacob Faibussowitsch     PetscCall(MatGetSize(lJ, &N, NULL));
8069566063dSJacob Faibussowitsch     PetscCall(MatGetLocalSize(lJ, &n, NULL));
8079566063dSJacob Faibussowitsch     PetscCall(MatSetSizes(P, n, n, N, N));
8089566063dSJacob Faibussowitsch     PetscCall(MatSetBlockSize(P, dof));
8099566063dSJacob Faibussowitsch     PetscCall(MatSetUp(P));
81048a46eb9SPierre Jolivet     for (i = 0; i < nel; i++) PetscCall(MatSetValuesBlockedLocal(P, nen, e_loc + i * nen, nen, e_loc + i * nen, NULL, INSERT_VALUES));
8119566063dSJacob Faibussowitsch     PetscCall(MatPreallocatorPreallocate(P, (PetscBool)!da->prealloc_only, lJ));
8129566063dSJacob Faibussowitsch     PetscCall(MatISRestoreLocalMat(J, &lJ));
8139566063dSJacob Faibussowitsch     PetscCall(DMDARestoreElements(dm, &nel, &nen, &e_loc));
8149566063dSJacob Faibussowitsch     PetscCall(MatDestroy(&P));
815e432b41dSStefano Zampini 
8169566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
8179566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
818e306f867SJed Brown   }
8193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
820e584696dSStefano Zampini }
821e584696dSStefano Zampini 
DMCreateMatrix_DA_2d_MPISELL(DM da,Mat J)822d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPISELL(DM da, Mat J)
823d71ae5a4SJacob Faibussowitsch {
8245e26d47bSHong 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;
8255e26d47bSHong Zhang   PetscInt               lstart, lend, pstart, pend, *dnz, *onz;
8265e26d47bSHong Zhang   MPI_Comm               comm;
8275e26d47bSHong Zhang   PetscScalar           *values;
8285e26d47bSHong Zhang   DMBoundaryType         bx, by;
8295e26d47bSHong Zhang   ISLocalToGlobalMapping ltog;
8305e26d47bSHong Zhang   DMDAStencilType        st;
8315e26d47bSHong Zhang 
8325e26d47bSHong Zhang   PetscFunctionBegin;
8335e26d47bSHong Zhang   /*
8345e26d47bSHong Zhang          nc - number of components per grid point
8355e26d47bSHong Zhang          col - number of colors needed in one direction for single component problem
8365e26d47bSHong Zhang   */
8379566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, NULL, NULL, NULL, &nc, &s, &bx, &by, NULL, &st));
8385e26d47bSHong Zhang   col = 2 * s + 1;
8399566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL));
8409566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL));
8419566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
8425e26d47bSHong Zhang 
8439566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(nc, &rows, col * col * nc * nc, &cols));
8449566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
8455e26d47bSHong Zhang 
8469566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, nc));
8475e26d47bSHong Zhang   /* determine the matrix preallocation information */
848d0609cedSBarry Smith   MatPreallocateBegin(comm, nc * nx * ny, nc * nx * ny, dnz, onz);
8495e26d47bSHong Zhang   for (i = xs; i < xs + nx; i++) {
8505e26d47bSHong Zhang     pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
8515e26d47bSHong Zhang     pend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
8525e26d47bSHong Zhang 
8535e26d47bSHong Zhang     for (j = ys; j < ys + ny; j++) {
8545e26d47bSHong Zhang       slot = i - gxs + gnx * (j - gys);
8555e26d47bSHong Zhang 
8565e26d47bSHong Zhang       lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
8575e26d47bSHong Zhang       lend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
8585e26d47bSHong Zhang 
8595e26d47bSHong Zhang       cnt = 0;
8605e26d47bSHong Zhang       for (k = 0; k < nc; k++) {
8615e26d47bSHong Zhang         for (l = lstart; l < lend + 1; l++) {
8625e26d47bSHong Zhang           for (p = pstart; p < pend + 1; p++) {
863b6555650SPierre Jolivet             if (st == DMDA_STENCIL_BOX || !l || !p) { /* entries on star have either l = 0 or p = 0 */
8645e26d47bSHong Zhang               cols[cnt++] = k + nc * (slot + gnx * l + p);
8655e26d47bSHong Zhang             }
8665e26d47bSHong Zhang           }
8675e26d47bSHong Zhang         }
8685e26d47bSHong Zhang         rows[k] = k + nc * (slot);
8695e26d47bSHong Zhang       }
8709566063dSJacob Faibussowitsch       PetscCall(MatPreallocateSetLocal(ltog, nc, rows, ltog, cnt, cols, dnz, onz));
8715e26d47bSHong Zhang     }
8725e26d47bSHong Zhang   }
8739566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, nc));
8749566063dSJacob Faibussowitsch   PetscCall(MatSeqSELLSetPreallocation(J, 0, dnz));
8759566063dSJacob Faibussowitsch   PetscCall(MatMPISELLSetPreallocation(J, 0, dnz, 0, onz));
876d0609cedSBarry Smith   MatPreallocateEnd(dnz, onz);
8775e26d47bSHong Zhang 
8789566063dSJacob Faibussowitsch   PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
8795e26d47bSHong Zhang 
8805e26d47bSHong Zhang   /*
8815e26d47bSHong Zhang     For each node in the grid: we get the neighbors in the local (on processor ordering
8825e26d47bSHong Zhang     that includes the ghost points) then MatSetValuesLocal() maps those indices to the global
8835e26d47bSHong Zhang     PETSc ordering.
8845e26d47bSHong Zhang   */
8855e26d47bSHong Zhang   if (!da->prealloc_only) {
8869566063dSJacob Faibussowitsch     PetscCall(PetscCalloc1(col * col * nc * nc, &values));
8875e26d47bSHong Zhang     for (i = xs; i < xs + nx; i++) {
8885e26d47bSHong Zhang       pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
8895e26d47bSHong Zhang       pend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
8905e26d47bSHong Zhang 
8915e26d47bSHong Zhang       for (j = ys; j < ys + ny; j++) {
8925e26d47bSHong Zhang         slot = i - gxs + gnx * (j - gys);
8935e26d47bSHong Zhang 
8945e26d47bSHong Zhang         lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
8955e26d47bSHong Zhang         lend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
8965e26d47bSHong Zhang 
8975e26d47bSHong Zhang         cnt = 0;
8985e26d47bSHong Zhang         for (k = 0; k < nc; k++) {
8995e26d47bSHong Zhang           for (l = lstart; l < lend + 1; l++) {
9005e26d47bSHong Zhang             for (p = pstart; p < pend + 1; p++) {
901b6555650SPierre Jolivet               if (st == DMDA_STENCIL_BOX || !l || !p) { /* entries on star have either l = 0 or p = 0 */
9025e26d47bSHong Zhang                 cols[cnt++] = k + nc * (slot + gnx * l + p);
9035e26d47bSHong Zhang               }
9045e26d47bSHong Zhang             }
9055e26d47bSHong Zhang           }
9065e26d47bSHong Zhang           rows[k] = k + nc * (slot);
9075e26d47bSHong Zhang         }
9089566063dSJacob Faibussowitsch         PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, values, INSERT_VALUES));
9095e26d47bSHong Zhang       }
9105e26d47bSHong Zhang     }
9119566063dSJacob Faibussowitsch     PetscCall(PetscFree(values));
912e7e92044SBarry Smith     /* do not copy values to GPU since they are all zero and not yet needed there */
9139566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_TRUE));
9149566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
9159566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
9169566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_FALSE));
9179566063dSJacob Faibussowitsch     PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
9185e26d47bSHong Zhang   }
9199566063dSJacob Faibussowitsch   PetscCall(PetscFree2(rows, cols));
9203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9215e26d47bSHong Zhang }
9225e26d47bSHong Zhang 
DMCreateMatrix_DA_3d_MPISELL(DM da,Mat J)923d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPISELL(DM da, Mat J)
924d71ae5a4SJacob Faibussowitsch {
925711261dbSHong Zhang   PetscInt               xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny;
926711261dbSHong Zhang   PetscInt               m, n, dim, s, *cols = NULL, k, nc, *rows = NULL, col, cnt, l, p, *dnz = NULL, *onz = NULL;
927711261dbSHong Zhang   PetscInt               istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk, M, N, P;
928711261dbSHong Zhang   MPI_Comm               comm;
929711261dbSHong Zhang   PetscScalar           *values;
930711261dbSHong Zhang   DMBoundaryType         bx, by, bz;
931711261dbSHong Zhang   ISLocalToGlobalMapping ltog;
932711261dbSHong Zhang   DMDAStencilType        st;
933711261dbSHong Zhang 
934711261dbSHong Zhang   PetscFunctionBegin;
935711261dbSHong Zhang   /*
936711261dbSHong Zhang          nc - number of components per grid point
937711261dbSHong Zhang          col - number of colors needed in one direction for single component problem
938711261dbSHong Zhang   */
9399566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &nc, &s, &bx, &by, &bz, &st));
940711261dbSHong Zhang   col = 2 * s + 1;
9419566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz));
9429566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz));
9439566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
944711261dbSHong Zhang 
9459566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(nc, &rows, col * col * col * nc * nc, &cols));
9469566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
947711261dbSHong Zhang 
9489566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, nc));
949711261dbSHong Zhang   /* determine the matrix preallocation information */
950d0609cedSBarry Smith   MatPreallocateBegin(comm, nc * nx * ny * nz, nc * nx * ny * nz, dnz, onz);
951711261dbSHong Zhang   for (i = xs; i < xs + nx; i++) {
952711261dbSHong Zhang     istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
953711261dbSHong Zhang     iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
954711261dbSHong Zhang     for (j = ys; j < ys + ny; j++) {
955711261dbSHong Zhang       jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
956711261dbSHong Zhang       jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
957711261dbSHong Zhang       for (k = zs; k < zs + nz; k++) {
958711261dbSHong Zhang         kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k));
959711261dbSHong Zhang         kend   = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1));
960711261dbSHong Zhang 
961711261dbSHong Zhang         slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs);
962711261dbSHong Zhang 
963711261dbSHong Zhang         cnt = 0;
964711261dbSHong Zhang         for (l = 0; l < nc; l++) {
965711261dbSHong Zhang           for (ii = istart; ii < iend + 1; ii++) {
966711261dbSHong Zhang             for (jj = jstart; jj < jend + 1; jj++) {
967711261dbSHong Zhang               for (kk = kstart; kk < kend + 1; kk++) {
968b6555650SPierre Jolivet                 if (st == DMDA_STENCIL_BOX || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/
969711261dbSHong Zhang                   cols[cnt++] = l + nc * (slot + ii + gnx * jj + gnx * gny * kk);
970711261dbSHong Zhang                 }
971711261dbSHong Zhang               }
972711261dbSHong Zhang             }
973711261dbSHong Zhang           }
974711261dbSHong Zhang           rows[l] = l + nc * (slot);
975711261dbSHong Zhang         }
9769566063dSJacob Faibussowitsch         PetscCall(MatPreallocateSetLocal(ltog, nc, rows, ltog, cnt, cols, dnz, onz));
977711261dbSHong Zhang       }
978711261dbSHong Zhang     }
979711261dbSHong Zhang   }
9809566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, nc));
9819566063dSJacob Faibussowitsch   PetscCall(MatSeqSELLSetPreallocation(J, 0, dnz));
9829566063dSJacob Faibussowitsch   PetscCall(MatMPISELLSetPreallocation(J, 0, dnz, 0, onz));
983d0609cedSBarry Smith   MatPreallocateEnd(dnz, onz);
9849566063dSJacob Faibussowitsch   PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
985711261dbSHong Zhang 
986711261dbSHong Zhang   /*
987711261dbSHong Zhang     For each node in the grid: we get the neighbors in the local (on processor ordering
988711261dbSHong Zhang     that includes the ghost points) then MatSetValuesLocal() maps those indices to the global
989711261dbSHong Zhang     PETSc ordering.
990711261dbSHong Zhang   */
991711261dbSHong Zhang   if (!da->prealloc_only) {
9929566063dSJacob Faibussowitsch     PetscCall(PetscCalloc1(col * col * col * nc * nc * nc, &values));
993711261dbSHong Zhang     for (i = xs; i < xs + nx; i++) {
994711261dbSHong Zhang       istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
995711261dbSHong Zhang       iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
996711261dbSHong Zhang       for (j = ys; j < ys + ny; j++) {
997711261dbSHong Zhang         jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
998711261dbSHong Zhang         jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
999711261dbSHong Zhang         for (k = zs; k < zs + nz; k++) {
1000711261dbSHong Zhang           kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k));
1001711261dbSHong Zhang           kend   = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1));
1002711261dbSHong Zhang 
1003711261dbSHong Zhang           slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs);
1004711261dbSHong Zhang 
1005711261dbSHong Zhang           cnt = 0;
1006711261dbSHong Zhang           for (l = 0; l < nc; l++) {
1007711261dbSHong Zhang             for (ii = istart; ii < iend + 1; ii++) {
1008711261dbSHong Zhang               for (jj = jstart; jj < jend + 1; jj++) {
1009711261dbSHong Zhang                 for (kk = kstart; kk < kend + 1; kk++) {
1010b6555650SPierre Jolivet                   if (st == DMDA_STENCIL_BOX || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/
1011711261dbSHong Zhang                     cols[cnt++] = l + nc * (slot + ii + gnx * jj + gnx * gny * kk);
1012711261dbSHong Zhang                   }
1013711261dbSHong Zhang                 }
1014711261dbSHong Zhang               }
1015711261dbSHong Zhang             }
1016711261dbSHong Zhang             rows[l] = l + nc * (slot);
1017711261dbSHong Zhang           }
10189566063dSJacob Faibussowitsch           PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, values, INSERT_VALUES));
1019711261dbSHong Zhang         }
1020711261dbSHong Zhang       }
1021711261dbSHong Zhang     }
10229566063dSJacob Faibussowitsch     PetscCall(PetscFree(values));
1023e7e92044SBarry Smith     /* do not copy values to GPU since they are all zero and not yet needed there */
10249566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_TRUE));
10259566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
10269566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
10279566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_FALSE));
10289566063dSJacob Faibussowitsch     PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
1029711261dbSHong Zhang   }
10309566063dSJacob Faibussowitsch   PetscCall(PetscFree2(rows, cols));
10313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1032711261dbSHong Zhang }
1033711261dbSHong Zhang 
DMCreateMatrix_DA_2d_MPIAIJ(DM da,Mat J)1034d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPIAIJ(DM da, Mat J)
1035d71ae5a4SJacob Faibussowitsch {
1036c1154cd5SBarry 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;
103747c6ae99SBarry Smith   PetscInt               lstart, lend, pstart, pend, *dnz, *onz;
103847c6ae99SBarry Smith   MPI_Comm               comm;
1039bff4a2f0SMatthew G. Knepley   DMBoundaryType         bx, by;
1040844bd0d7SStefano Zampini   ISLocalToGlobalMapping ltog, mltog;
1041aa219208SBarry Smith   DMDAStencilType        st;
1042b294de21SRichard Tran Mills   PetscBool              removedups = PETSC_FALSE, alreadyboundtocpu = PETSC_TRUE;
104347c6ae99SBarry Smith 
104447c6ae99SBarry Smith   PetscFunctionBegin;
104547c6ae99SBarry Smith   /*
104647c6ae99SBarry Smith          nc - number of components per grid point
104747c6ae99SBarry Smith          col - number of colors needed in one direction for single component problem
104847c6ae99SBarry Smith   */
1049924e958dSJunchao Zhang   PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, &M, &N, NULL, &nc, &s, &bx, &by, NULL, &st));
10501baa6e33SBarry Smith   if (bx == DM_BOUNDARY_NONE && by == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_TRUE));
105147c6ae99SBarry Smith   col = 2 * s + 1;
1052c1154cd5SBarry Smith   /*
1053c1154cd5SBarry Smith        With one processor in periodic domains in a skinny dimension the code will label nonzero columns multiple times
1054c1154cd5SBarry Smith        because of "wrapping" around the end of the domain hitting an entry already counted in the other direction.
1055c1154cd5SBarry Smith   */
1056c1154cd5SBarry Smith   if (M == 1 && 2 * s >= m) removedups = PETSC_TRUE;
1057c1154cd5SBarry Smith   if (N == 1 && 2 * s >= n) removedups = PETSC_TRUE;
10589566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL));
10599566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL));
10609566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
106147c6ae99SBarry Smith 
10629566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(nc, &rows, col * col * nc * nc, &cols));
10639566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
106447c6ae99SBarry Smith 
10659566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, nc));
106647c6ae99SBarry Smith   /* determine the matrix preallocation information */
1067d0609cedSBarry Smith   MatPreallocateBegin(comm, nc * nx * ny, nc * nx * ny, dnz, onz);
106847c6ae99SBarry Smith   for (i = xs; i < xs + nx; i++) {
1069bff4a2f0SMatthew G. Knepley     pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
1070bff4a2f0SMatthew G. Knepley     pend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
107147c6ae99SBarry Smith 
107247c6ae99SBarry Smith     for (j = ys; j < ys + ny; j++) {
107347c6ae99SBarry Smith       slot = i - gxs + gnx * (j - gys);
107447c6ae99SBarry Smith 
1075bff4a2f0SMatthew G. Knepley       lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
1076bff4a2f0SMatthew G. Knepley       lend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
107747c6ae99SBarry Smith 
107847c6ae99SBarry Smith       cnt = 0;
107947c6ae99SBarry Smith       for (k = 0; k < nc; k++) {
108047c6ae99SBarry Smith         for (l = lstart; l < lend + 1; l++) {
108147c6ae99SBarry Smith           for (p = pstart; p < pend + 1; p++) {
1082b6555650SPierre Jolivet             if (st == DMDA_STENCIL_BOX || !l || !p) { /* entries on star have either l = 0 or p = 0 */
108347c6ae99SBarry Smith               cols[cnt++] = k + nc * (slot + gnx * l + p);
108447c6ae99SBarry Smith             }
108547c6ae99SBarry Smith           }
108647c6ae99SBarry Smith         }
108747c6ae99SBarry Smith         rows[k] = k + nc * (slot);
108847c6ae99SBarry Smith       }
10891baa6e33SBarry Smith       if (removedups) PetscCall(MatPreallocateSetLocalRemoveDups(ltog, nc, rows, ltog, cnt, cols, dnz, onz));
10901baa6e33SBarry Smith       else PetscCall(MatPreallocateSetLocal(ltog, nc, rows, ltog, cnt, cols, dnz, onz));
109147c6ae99SBarry Smith     }
1092c1154cd5SBarry Smith   }
10939566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, nc));
10949566063dSJacob Faibussowitsch   PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz));
10959566063dSJacob Faibussowitsch   PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz));
1096d0609cedSBarry Smith   MatPreallocateEnd(dnz, onz);
10979566063dSJacob Faibussowitsch   PetscCall(MatGetLocalToGlobalMapping(J, &mltog, NULL));
10981baa6e33SBarry Smith   if (!mltog) PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
109947c6ae99SBarry Smith 
110047c6ae99SBarry Smith   /*
110147c6ae99SBarry Smith     For each node in the grid: we get the neighbors in the local (on processor ordering
110247c6ae99SBarry Smith     that includes the ghost points) then MatSetValuesLocal() maps those indices to the global
110347c6ae99SBarry Smith     PETSc ordering.
110447c6ae99SBarry Smith   */
1105fcfd50ebSBarry Smith   if (!da->prealloc_only) {
110647c6ae99SBarry Smith     for (i = xs; i < xs + nx; i++) {
1107bff4a2f0SMatthew G. Knepley       pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
1108bff4a2f0SMatthew G. Knepley       pend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
110947c6ae99SBarry Smith 
111047c6ae99SBarry Smith       for (j = ys; j < ys + ny; j++) {
111147c6ae99SBarry Smith         slot = i - gxs + gnx * (j - gys);
111247c6ae99SBarry Smith 
1113bff4a2f0SMatthew G. Knepley         lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
1114bff4a2f0SMatthew G. Knepley         lend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
111547c6ae99SBarry Smith 
111647c6ae99SBarry Smith         cnt = 0;
111747c6ae99SBarry Smith         for (l = lstart; l < lend + 1; l++) {
111847c6ae99SBarry Smith           for (p = pstart; p < pend + 1; p++) {
1119b6555650SPierre Jolivet             if (st == DMDA_STENCIL_BOX || !l || !p) { /* entries on star have either l = 0 or p = 0 */
1120071fcb05SBarry Smith               cols[cnt++] = nc * (slot + gnx * l + p);
1121071fcb05SBarry Smith               for (k = 1; k < nc; k++) {
11229371c9d4SSatish Balay                 cols[cnt] = 1 + cols[cnt - 1];
11239371c9d4SSatish Balay                 cnt++;
112447c6ae99SBarry Smith               }
112547c6ae99SBarry Smith             }
112647c6ae99SBarry Smith           }
112747c6ae99SBarry Smith         }
1128071fcb05SBarry Smith         for (k = 0; k < nc; k++) rows[k] = k + nc * (slot);
11299566063dSJacob Faibussowitsch         PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, NULL, INSERT_VALUES));
113047c6ae99SBarry Smith       }
113147c6ae99SBarry Smith     }
1132e7e92044SBarry Smith     /* do not copy values to GPU since they are all zero and not yet needed there */
11339566063dSJacob Faibussowitsch     PetscCall(MatBoundToCPU(J, &alreadyboundtocpu));
11349566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_TRUE));
11359566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
11369566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
11379566063dSJacob Faibussowitsch     if (!alreadyboundtocpu) PetscCall(MatBindToCPU(J, PETSC_FALSE));
11389566063dSJacob Faibussowitsch     PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
11391baa6e33SBarry Smith     if (bx == DM_BOUNDARY_NONE && by == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE));
114047c6ae99SBarry Smith   }
11419566063dSJacob Faibussowitsch   PetscCall(PetscFree2(rows, cols));
11423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
114347c6ae99SBarry Smith }
114447c6ae99SBarry Smith 
DMCreateMatrix_DA_2d_MPIAIJ_Fill(DM da,Mat J)1145d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPIAIJ_Fill(DM da, Mat J)
1146d71ae5a4SJacob Faibussowitsch {
114747c6ae99SBarry Smith   PetscInt               xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny;
1148c1154cd5SBarry Smith   PetscInt               m, n, dim, s, *cols, k, nc, row, col, cnt, maxcnt = 0, l, p, M, N;
114947c6ae99SBarry Smith   PetscInt               lstart, lend, pstart, pend, *dnz, *onz;
115047c6ae99SBarry Smith   DM_DA                 *dd = (DM_DA *)da->data;
115147c6ae99SBarry Smith   PetscInt               ifill_col, *ofill = dd->ofill, *dfill = dd->dfill;
115247c6ae99SBarry Smith   MPI_Comm               comm;
1153bff4a2f0SMatthew G. Knepley   DMBoundaryType         bx, by;
115445b6f7e9SBarry Smith   ISLocalToGlobalMapping ltog;
1155aa219208SBarry Smith   DMDAStencilType        st;
1156c1154cd5SBarry Smith   PetscBool              removedups = PETSC_FALSE;
115747c6ae99SBarry Smith 
115847c6ae99SBarry Smith   PetscFunctionBegin;
115947c6ae99SBarry Smith   /*
116047c6ae99SBarry Smith          nc - number of components per grid point
116147c6ae99SBarry Smith          col - number of colors needed in one direction for single component problem
116247c6ae99SBarry Smith   */
1163924e958dSJunchao Zhang   PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, &M, &N, NULL, &nc, &s, &bx, &by, NULL, &st));
116447c6ae99SBarry Smith   col = 2 * s + 1;
1165c1154cd5SBarry Smith   /*
1166c1154cd5SBarry Smith        With one processor in periodic domains in a skinny dimension the code will label nonzero columns multiple times
1167c1154cd5SBarry Smith        because of "wrapping" around the end of the domain hitting an entry already counted in the other direction.
1168c1154cd5SBarry Smith   */
1169c1154cd5SBarry Smith   if (M == 1 && 2 * s >= m) removedups = PETSC_TRUE;
1170c1154cd5SBarry Smith   if (N == 1 && 2 * s >= n) removedups = PETSC_TRUE;
11719566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL));
11729566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL));
11739566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
117447c6ae99SBarry Smith 
11759566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(col * col * nc, &cols));
11769566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
117747c6ae99SBarry Smith 
11789566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, nc));
117947c6ae99SBarry Smith   /* determine the matrix preallocation information */
1180d0609cedSBarry Smith   MatPreallocateBegin(comm, nc * nx * ny, nc * nx * ny, dnz, onz);
118147c6ae99SBarry Smith   for (i = xs; i < xs + nx; i++) {
1182bff4a2f0SMatthew G. Knepley     pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
1183bff4a2f0SMatthew G. Knepley     pend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
118447c6ae99SBarry Smith 
118547c6ae99SBarry Smith     for (j = ys; j < ys + ny; j++) {
118647c6ae99SBarry Smith       slot = i - gxs + gnx * (j - gys);
118747c6ae99SBarry Smith 
1188bff4a2f0SMatthew G. Knepley       lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
1189bff4a2f0SMatthew G. Knepley       lend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
119047c6ae99SBarry Smith 
119147c6ae99SBarry Smith       for (k = 0; k < nc; k++) {
119247c6ae99SBarry Smith         cnt = 0;
119347c6ae99SBarry Smith         for (l = lstart; l < lend + 1; l++) {
119447c6ae99SBarry Smith           for (p = pstart; p < pend + 1; p++) {
119547c6ae99SBarry Smith             if (l || p) {
1196b6555650SPierre Jolivet               if (st == DMDA_STENCIL_BOX || !l || !p) { /* entries on star */
11978865f1eaSKarl Rupp                 for (ifill_col = ofill[k]; ifill_col < ofill[k + 1]; ifill_col++) cols[cnt++] = ofill[ifill_col] + nc * (slot + gnx * l + p);
119847c6ae99SBarry Smith               }
119947c6ae99SBarry Smith             } else {
120047c6ae99SBarry Smith               if (dfill) {
12018865f1eaSKarl Rupp                 for (ifill_col = dfill[k]; ifill_col < dfill[k + 1]; ifill_col++) cols[cnt++] = dfill[ifill_col] + nc * (slot + gnx * l + p);
120247c6ae99SBarry Smith               } else {
12038865f1eaSKarl Rupp                 for (ifill_col = 0; ifill_col < nc; ifill_col++) cols[cnt++] = ifill_col + nc * (slot + gnx * l + p);
120447c6ae99SBarry Smith               }
120547c6ae99SBarry Smith             }
120647c6ae99SBarry Smith           }
120747c6ae99SBarry Smith         }
120847c6ae99SBarry Smith         row    = k + nc * (slot);
1209c0ab637bSBarry Smith         maxcnt = PetscMax(maxcnt, cnt);
12101baa6e33SBarry Smith         if (removedups) PetscCall(MatPreallocateSetLocalRemoveDups(ltog, 1, &row, ltog, cnt, cols, dnz, onz));
12111baa6e33SBarry Smith         else PetscCall(MatPreallocateSetLocal(ltog, 1, &row, ltog, cnt, cols, dnz, onz));
121247c6ae99SBarry Smith       }
121347c6ae99SBarry Smith     }
1214c1154cd5SBarry Smith   }
12159566063dSJacob Faibussowitsch   PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz));
12169566063dSJacob Faibussowitsch   PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz));
1217d0609cedSBarry Smith   MatPreallocateEnd(dnz, onz);
12189566063dSJacob Faibussowitsch   PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
121947c6ae99SBarry Smith 
122047c6ae99SBarry Smith   /*
122147c6ae99SBarry Smith     For each node in the grid: we get the neighbors in the local (on processor ordering
122247c6ae99SBarry Smith     that includes the ghost points) then MatSetValuesLocal() maps those indices to the global
122347c6ae99SBarry Smith     PETSc ordering.
122447c6ae99SBarry Smith   */
1225fcfd50ebSBarry Smith   if (!da->prealloc_only) {
122647c6ae99SBarry Smith     for (i = xs; i < xs + nx; i++) {
1227bff4a2f0SMatthew G. Knepley       pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
1228bff4a2f0SMatthew G. Knepley       pend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
122947c6ae99SBarry Smith 
123047c6ae99SBarry Smith       for (j = ys; j < ys + ny; j++) {
123147c6ae99SBarry Smith         slot = i - gxs + gnx * (j - gys);
123247c6ae99SBarry Smith 
1233bff4a2f0SMatthew G. Knepley         lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
1234bff4a2f0SMatthew G. Knepley         lend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
123547c6ae99SBarry Smith 
123647c6ae99SBarry Smith         for (k = 0; k < nc; k++) {
123747c6ae99SBarry Smith           cnt = 0;
123847c6ae99SBarry Smith           for (l = lstart; l < lend + 1; l++) {
123947c6ae99SBarry Smith             for (p = pstart; p < pend + 1; p++) {
124047c6ae99SBarry Smith               if (l || p) {
1241b6555650SPierre Jolivet                 if (st == DMDA_STENCIL_BOX || !l || !p) { /* entries on star */
12428865f1eaSKarl Rupp                   for (ifill_col = ofill[k]; ifill_col < ofill[k + 1]; ifill_col++) cols[cnt++] = ofill[ifill_col] + nc * (slot + gnx * l + p);
124347c6ae99SBarry Smith                 }
124447c6ae99SBarry Smith               } else {
124547c6ae99SBarry Smith                 if (dfill) {
12468865f1eaSKarl Rupp                   for (ifill_col = dfill[k]; ifill_col < dfill[k + 1]; ifill_col++) cols[cnt++] = dfill[ifill_col] + nc * (slot + gnx * l + p);
124747c6ae99SBarry Smith                 } else {
12488865f1eaSKarl Rupp                   for (ifill_col = 0; ifill_col < nc; ifill_col++) cols[cnt++] = ifill_col + nc * (slot + gnx * l + p);
124947c6ae99SBarry Smith                 }
125047c6ae99SBarry Smith               }
125147c6ae99SBarry Smith             }
125247c6ae99SBarry Smith           }
125347c6ae99SBarry Smith           row = k + nc * (slot);
12549566063dSJacob Faibussowitsch           PetscCall(MatSetValuesLocal(J, 1, &row, cnt, cols, NULL, INSERT_VALUES));
125547c6ae99SBarry Smith         }
125647c6ae99SBarry Smith       }
125747c6ae99SBarry Smith     }
1258e7e92044SBarry Smith     /* do not copy values to GPU since they are all zero and not yet needed there */
12599566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_TRUE));
12609566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
12619566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
12629566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_FALSE));
12639566063dSJacob Faibussowitsch     PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
126447c6ae99SBarry Smith   }
12659566063dSJacob Faibussowitsch   PetscCall(PetscFree(cols));
12663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
126747c6ae99SBarry Smith }
126847c6ae99SBarry Smith 
DMCreateMatrix_DA_3d_MPIAIJ(DM da,Mat J)1269d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPIAIJ(DM da, Mat J)
1270d71ae5a4SJacob Faibussowitsch {
127147c6ae99SBarry Smith   PetscInt               xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny;
12720298fd71SBarry Smith   PetscInt               m, n, dim, s, *cols = NULL, k, nc, *rows = NULL, col, cnt, l, p, *dnz = NULL, *onz = NULL;
1273c1154cd5SBarry Smith   PetscInt               istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk, M, N, P;
127447c6ae99SBarry Smith   MPI_Comm               comm;
1275bff4a2f0SMatthew G. Knepley   DMBoundaryType         bx, by, bz;
1276844bd0d7SStefano Zampini   ISLocalToGlobalMapping ltog, mltog;
1277aa219208SBarry Smith   DMDAStencilType        st;
1278c1154cd5SBarry Smith   PetscBool              removedups = PETSC_FALSE;
127947c6ae99SBarry Smith 
128047c6ae99SBarry Smith   PetscFunctionBegin;
128147c6ae99SBarry Smith   /*
128247c6ae99SBarry Smith          nc - number of components per grid point
128347c6ae99SBarry Smith          col - number of colors needed in one direction for single component problem
128447c6ae99SBarry Smith   */
12859566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &nc, &s, &bx, &by, &bz, &st));
128648a46eb9SPierre Jolivet   if (bx == DM_BOUNDARY_NONE && by == DM_BOUNDARY_NONE && bz == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_TRUE));
128747c6ae99SBarry Smith   col = 2 * s + 1;
128847c6ae99SBarry Smith 
1289c1154cd5SBarry Smith   /*
1290c1154cd5SBarry Smith        With one processor in periodic domains in a skinny dimension the code will label nonzero columns multiple times
1291c1154cd5SBarry Smith        because of "wrapping" around the end of the domain hitting an entry already counted in the other direction.
1292c1154cd5SBarry Smith   */
1293c1154cd5SBarry Smith   if (M == 1 && 2 * s >= m) removedups = PETSC_TRUE;
1294c1154cd5SBarry Smith   if (N == 1 && 2 * s >= n) removedups = PETSC_TRUE;
1295c1154cd5SBarry Smith   if (P == 1 && 2 * s >= p) removedups = PETSC_TRUE;
1296c1154cd5SBarry Smith 
12979566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz));
12989566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz));
12999566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
130047c6ae99SBarry Smith 
13019566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(nc, &rows, col * col * col * nc * nc, &cols));
13029566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
130347c6ae99SBarry Smith 
13049566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, nc));
130547c6ae99SBarry Smith   /* determine the matrix preallocation information */
1306d0609cedSBarry Smith   MatPreallocateBegin(comm, nc * nx * ny * nz, nc * nx * ny * nz, dnz, onz);
130747c6ae99SBarry Smith   for (i = xs; i < xs + nx; i++) {
1308bff4a2f0SMatthew G. Knepley     istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
1309bff4a2f0SMatthew G. Knepley     iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
131047c6ae99SBarry Smith     for (j = ys; j < ys + ny; j++) {
1311bff4a2f0SMatthew G. Knepley       jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
1312bff4a2f0SMatthew G. Knepley       jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
131347c6ae99SBarry Smith       for (k = zs; k < zs + nz; k++) {
1314bff4a2f0SMatthew G. Knepley         kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k));
1315bff4a2f0SMatthew G. Knepley         kend   = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1));
131647c6ae99SBarry Smith 
131747c6ae99SBarry Smith         slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs);
131847c6ae99SBarry Smith 
131947c6ae99SBarry Smith         cnt = 0;
132047c6ae99SBarry Smith         for (l = 0; l < nc; l++) {
132147c6ae99SBarry Smith           for (ii = istart; ii < iend + 1; ii++) {
132247c6ae99SBarry Smith             for (jj = jstart; jj < jend + 1; jj++) {
132347c6ae99SBarry Smith               for (kk = kstart; kk < kend + 1; kk++) {
1324b6555650SPierre Jolivet                 if (st == DMDA_STENCIL_BOX || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/
132547c6ae99SBarry Smith                   cols[cnt++] = l + nc * (slot + ii + gnx * jj + gnx * gny * kk);
132647c6ae99SBarry Smith                 }
132747c6ae99SBarry Smith               }
132847c6ae99SBarry Smith             }
132947c6ae99SBarry Smith           }
133047c6ae99SBarry Smith           rows[l] = l + nc * (slot);
133147c6ae99SBarry Smith         }
13321baa6e33SBarry Smith         if (removedups) PetscCall(MatPreallocateSetLocalRemoveDups(ltog, nc, rows, ltog, cnt, cols, dnz, onz));
13331baa6e33SBarry Smith         else PetscCall(MatPreallocateSetLocal(ltog, nc, rows, ltog, cnt, cols, dnz, onz));
133447c6ae99SBarry Smith       }
133547c6ae99SBarry Smith     }
1336c1154cd5SBarry Smith   }
13379566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, nc));
13389566063dSJacob Faibussowitsch   PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz));
13399566063dSJacob Faibussowitsch   PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz));
1340d0609cedSBarry Smith   MatPreallocateEnd(dnz, onz);
13419566063dSJacob Faibussowitsch   PetscCall(MatGetLocalToGlobalMapping(J, &mltog, NULL));
134248a46eb9SPierre Jolivet   if (!mltog) PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
134347c6ae99SBarry Smith 
134447c6ae99SBarry Smith   /*
134547c6ae99SBarry Smith     For each node in the grid: we get the neighbors in the local (on processor ordering
134647c6ae99SBarry Smith     that includes the ghost points) then MatSetValuesLocal() maps those indices to the global
134747c6ae99SBarry Smith     PETSc ordering.
134847c6ae99SBarry Smith   */
1349fcfd50ebSBarry Smith   if (!da->prealloc_only) {
135047c6ae99SBarry Smith     for (i = xs; i < xs + nx; i++) {
1351bff4a2f0SMatthew G. Knepley       istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
1352bff4a2f0SMatthew G. Knepley       iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
135347c6ae99SBarry Smith       for (j = ys; j < ys + ny; j++) {
1354bff4a2f0SMatthew G. Knepley         jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
1355bff4a2f0SMatthew G. Knepley         jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
135647c6ae99SBarry Smith         for (k = zs; k < zs + nz; k++) {
1357bff4a2f0SMatthew G. Knepley           kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k));
1358bff4a2f0SMatthew G. Knepley           kend   = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1));
135947c6ae99SBarry Smith 
136047c6ae99SBarry Smith           slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs);
136147c6ae99SBarry Smith 
136247c6ae99SBarry Smith           cnt = 0;
136347c6ae99SBarry Smith           for (kk = kstart; kk < kend + 1; kk++) {
1364071fcb05SBarry Smith             for (jj = jstart; jj < jend + 1; jj++) {
1365071fcb05SBarry Smith               for (ii = istart; ii < iend + 1; ii++) {
1366b6555650SPierre Jolivet                 if (st == DMDA_STENCIL_BOX || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/
1367071fcb05SBarry Smith                   cols[cnt++] = nc * (slot + ii + gnx * jj + gnx * gny * kk);
1368071fcb05SBarry Smith                   for (l = 1; l < nc; l++) {
13699371c9d4SSatish Balay                     cols[cnt] = 1 + cols[cnt - 1];
13709371c9d4SSatish Balay                     cnt++;
137147c6ae99SBarry Smith                   }
137247c6ae99SBarry Smith                 }
137347c6ae99SBarry Smith               }
137447c6ae99SBarry Smith             }
137547c6ae99SBarry Smith           }
13769371c9d4SSatish Balay           rows[0] = nc * (slot);
13779371c9d4SSatish Balay           for (l = 1; l < nc; l++) rows[l] = 1 + rows[l - 1];
13789566063dSJacob Faibussowitsch           PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, NULL, INSERT_VALUES));
137947c6ae99SBarry Smith         }
138047c6ae99SBarry Smith       }
138147c6ae99SBarry Smith     }
1382e7e92044SBarry Smith     /* do not copy values to GPU since they are all zero and not yet needed there */
13839566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_TRUE));
13849566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
13859566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
138648a46eb9SPierre Jolivet     if (bx == DM_BOUNDARY_NONE && by == DM_BOUNDARY_NONE && bz == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE));
13879566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_FALSE));
13889566063dSJacob Faibussowitsch     PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
138947c6ae99SBarry Smith   }
13909566063dSJacob Faibussowitsch   PetscCall(PetscFree2(rows, cols));
13913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
139247c6ae99SBarry Smith }
139347c6ae99SBarry Smith 
DMCreateMatrix_DA_1d_MPIAIJ_Fill(DM da,Mat J)1394d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_1d_MPIAIJ_Fill(DM da, Mat J)
1395d71ae5a4SJacob Faibussowitsch {
1396ce308e1dSBarry Smith   DM_DA                 *dd = (DM_DA *)da->data;
1397ce308e1dSBarry Smith   PetscInt               xs, nx, i, j, gxs, gnx, row, k, l;
13988d4c968fSBarry Smith   PetscInt               m, dim, s, *cols = NULL, nc, cnt, maxcnt = 0, *ocols;
13990acb5bebSBarry Smith   PetscInt              *ofill = dd->ofill, *dfill = dd->dfill;
1400bff4a2f0SMatthew G. Knepley   DMBoundaryType         bx;
140145b6f7e9SBarry Smith   ISLocalToGlobalMapping ltog;
1402ce308e1dSBarry Smith   PetscMPIInt            rank, size;
1403ce308e1dSBarry Smith 
1404ce308e1dSBarry Smith   PetscFunctionBegin;
14059566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)da), &rank));
14069566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)da), &size));
1407ce308e1dSBarry Smith 
1408ce308e1dSBarry Smith   /*
1409ce308e1dSBarry Smith          nc - number of components per grid point
1410ce308e1dSBarry Smith   */
14119566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, NULL, NULL, NULL, NULL, NULL, &nc, &s, &bx, NULL, NULL, NULL));
141208401ef6SPierre Jolivet   PetscCheck(s <= 1, PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "Matrix creation for 1d not implemented correctly for stencil width larger than 1");
14139566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &nx, NULL, NULL));
14149566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, NULL, NULL, &gnx, NULL, NULL));
1415ce308e1dSBarry Smith 
14169566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, nc));
14179566063dSJacob Faibussowitsch   PetscCall(PetscCalloc2(nx * nc, &cols, nx * nc, &ocols));
1418ce308e1dSBarry Smith 
1419ce308e1dSBarry Smith   /*
1420ce308e1dSBarry Smith         note should be smaller for first and last process with no periodic
1421ce308e1dSBarry Smith         does not handle dfill
1422ce308e1dSBarry Smith   */
1423ce308e1dSBarry Smith   cnt = 0;
1424ce308e1dSBarry Smith   /* coupling with process to the left */
1425ce308e1dSBarry Smith   for (i = 0; i < s; i++) {
1426ce308e1dSBarry Smith     for (j = 0; j < nc; j++) {
1427dd400576SPatrick Sanan       ocols[cnt] = ((rank == 0) ? 0 : (s - i) * (ofill[j + 1] - ofill[j]));
14280acb5bebSBarry Smith       cols[cnt]  = dfill[j + 1] - dfill[j] + (s + i) * (ofill[j + 1] - ofill[j]);
1429dd400576SPatrick Sanan       if (rank == 0 && (dd->bx == DM_BOUNDARY_PERIODIC)) {
1430831644c1SBarry Smith         if (size > 1) ocols[cnt] += (s - i) * (ofill[j + 1] - ofill[j]);
1431831644c1SBarry Smith         else cols[cnt] += (s - i) * (ofill[j + 1] - ofill[j]);
1432831644c1SBarry Smith       }
1433c0ab637bSBarry Smith       maxcnt = PetscMax(maxcnt, ocols[cnt] + cols[cnt]);
1434ce308e1dSBarry Smith       cnt++;
1435ce308e1dSBarry Smith     }
1436ce308e1dSBarry Smith   }
1437ce308e1dSBarry Smith   for (i = s; i < nx - s; i++) {
1438ce308e1dSBarry Smith     for (j = 0; j < nc; j++) {
14390acb5bebSBarry Smith       cols[cnt] = dfill[j + 1] - dfill[j] + 2 * s * (ofill[j + 1] - ofill[j]);
1440c0ab637bSBarry Smith       maxcnt    = PetscMax(maxcnt, ocols[cnt] + cols[cnt]);
1441ce308e1dSBarry Smith       cnt++;
1442ce308e1dSBarry Smith     }
1443ce308e1dSBarry Smith   }
1444ce308e1dSBarry Smith   /* coupling with process to the right */
1445ce308e1dSBarry Smith   for (i = nx - s; i < nx; i++) {
1446ce308e1dSBarry Smith     for (j = 0; j < nc; j++) {
1447ce308e1dSBarry Smith       ocols[cnt] = ((rank == (size - 1)) ? 0 : (i - nx + s + 1) * (ofill[j + 1] - ofill[j]));
14480acb5bebSBarry Smith       cols[cnt]  = dfill[j + 1] - dfill[j] + (s + nx - i - 1) * (ofill[j + 1] - ofill[j]);
1449831644c1SBarry Smith       if ((rank == size - 1) && (dd->bx == DM_BOUNDARY_PERIODIC)) {
1450831644c1SBarry Smith         if (size > 1) ocols[cnt] += (i - nx + s + 1) * (ofill[j + 1] - ofill[j]);
1451831644c1SBarry Smith         else cols[cnt] += (i - nx + s + 1) * (ofill[j + 1] - ofill[j]);
1452831644c1SBarry Smith       }
1453c0ab637bSBarry Smith       maxcnt = PetscMax(maxcnt, ocols[cnt] + cols[cnt]);
1454ce308e1dSBarry Smith       cnt++;
1455ce308e1dSBarry Smith     }
1456ce308e1dSBarry Smith   }
1457ce308e1dSBarry Smith 
14589566063dSJacob Faibussowitsch   PetscCall(MatSeqAIJSetPreallocation(J, 0, cols));
14599566063dSJacob Faibussowitsch   PetscCall(MatMPIAIJSetPreallocation(J, 0, cols, 0, ocols));
14609566063dSJacob Faibussowitsch   PetscCall(PetscFree2(cols, ocols));
1461ce308e1dSBarry Smith 
14629566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
14639566063dSJacob Faibussowitsch   PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
1464ce308e1dSBarry Smith 
1465ce308e1dSBarry Smith   /*
1466ce308e1dSBarry Smith     For each node in the grid: we get the neighbors in the local (on processor ordering
1467ce308e1dSBarry Smith     that includes the ghost points) then MatSetValuesLocal() maps those indices to the global
1468ce308e1dSBarry Smith     PETSc ordering.
1469ce308e1dSBarry Smith   */
1470ce308e1dSBarry Smith   if (!da->prealloc_only) {
14719566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(maxcnt, &cols));
1472ce308e1dSBarry Smith     row = xs * nc;
1473ce308e1dSBarry Smith     /* coupling with process to the left */
1474ce308e1dSBarry Smith     for (i = xs; i < xs + s; i++) {
1475ce308e1dSBarry Smith       for (j = 0; j < nc; j++) {
1476ce308e1dSBarry Smith         cnt = 0;
1477ce308e1dSBarry Smith         if (rank) {
1478ce308e1dSBarry Smith           for (l = 0; l < s; l++) {
1479ce308e1dSBarry Smith             for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i - s + l) * nc + ofill[k];
1480ce308e1dSBarry Smith           }
1481ce308e1dSBarry Smith         }
1482dd400576SPatrick Sanan         if (rank == 0 && (dd->bx == DM_BOUNDARY_PERIODIC)) {
1483831644c1SBarry Smith           for (l = 0; l < s; l++) {
1484831644c1SBarry Smith             for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (m + i - s - l) * nc + ofill[k];
1485831644c1SBarry Smith           }
1486831644c1SBarry Smith         }
14870acb5bebSBarry Smith         if (dfill) {
1488ad540459SPierre Jolivet           for (k = dfill[j]; k < dfill[j + 1]; k++) cols[cnt++] = i * nc + dfill[k];
14890acb5bebSBarry Smith         } else {
1490ad540459SPierre Jolivet           for (k = 0; k < nc; k++) cols[cnt++] = i * nc + k;
14910acb5bebSBarry Smith         }
1492ce308e1dSBarry Smith         for (l = 0; l < s; l++) {
1493ce308e1dSBarry Smith           for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i + s - l) * nc + ofill[k];
1494ce308e1dSBarry Smith         }
14959566063dSJacob Faibussowitsch         PetscCall(MatSetValues(J, 1, &row, cnt, cols, NULL, INSERT_VALUES));
1496ce308e1dSBarry Smith         row++;
1497ce308e1dSBarry Smith       }
1498ce308e1dSBarry Smith     }
1499ce308e1dSBarry Smith     for (i = xs + s; i < xs + nx - s; i++) {
1500ce308e1dSBarry Smith       for (j = 0; j < nc; j++) {
1501ce308e1dSBarry Smith         cnt = 0;
1502ce308e1dSBarry Smith         for (l = 0; l < s; l++) {
1503ce308e1dSBarry Smith           for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i - s + l) * nc + ofill[k];
1504ce308e1dSBarry Smith         }
15050acb5bebSBarry Smith         if (dfill) {
1506ad540459SPierre Jolivet           for (k = dfill[j]; k < dfill[j + 1]; k++) cols[cnt++] = i * nc + dfill[k];
15070acb5bebSBarry Smith         } else {
1508ad540459SPierre Jolivet           for (k = 0; k < nc; k++) cols[cnt++] = i * nc + k;
15090acb5bebSBarry Smith         }
1510ce308e1dSBarry Smith         for (l = 0; l < s; l++) {
1511ce308e1dSBarry Smith           for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i + s - l) * nc + ofill[k];
1512ce308e1dSBarry Smith         }
15139566063dSJacob Faibussowitsch         PetscCall(MatSetValues(J, 1, &row, cnt, cols, NULL, INSERT_VALUES));
1514ce308e1dSBarry Smith         row++;
1515ce308e1dSBarry Smith       }
1516ce308e1dSBarry Smith     }
1517ce308e1dSBarry Smith     /* coupling with process to the right */
1518ce308e1dSBarry Smith     for (i = xs + nx - s; i < xs + nx; i++) {
1519ce308e1dSBarry Smith       for (j = 0; j < nc; j++) {
1520ce308e1dSBarry Smith         cnt = 0;
1521ce308e1dSBarry Smith         for (l = 0; l < s; l++) {
1522ce308e1dSBarry Smith           for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i - s + l) * nc + ofill[k];
1523ce308e1dSBarry Smith         }
15240acb5bebSBarry Smith         if (dfill) {
1525ad540459SPierre Jolivet           for (k = dfill[j]; k < dfill[j + 1]; k++) cols[cnt++] = i * nc + dfill[k];
15260acb5bebSBarry Smith         } else {
1527ad540459SPierre Jolivet           for (k = 0; k < nc; k++) cols[cnt++] = i * nc + k;
15280acb5bebSBarry Smith         }
1529ce308e1dSBarry Smith         if (rank < size - 1) {
1530ce308e1dSBarry Smith           for (l = 0; l < s; l++) {
1531ce308e1dSBarry Smith             for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i + s - l) * nc + ofill[k];
1532ce308e1dSBarry Smith           }
1533ce308e1dSBarry Smith         }
1534831644c1SBarry Smith         if ((rank == size - 1) && (dd->bx == DM_BOUNDARY_PERIODIC)) {
1535831644c1SBarry Smith           for (l = 0; l < s; l++) {
1536831644c1SBarry Smith             for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i - s - l - m + 2) * nc + ofill[k];
1537831644c1SBarry Smith           }
1538831644c1SBarry Smith         }
15399566063dSJacob Faibussowitsch         PetscCall(MatSetValues(J, 1, &row, cnt, cols, NULL, INSERT_VALUES));
1540ce308e1dSBarry Smith         row++;
1541ce308e1dSBarry Smith       }
1542ce308e1dSBarry Smith     }
15439566063dSJacob Faibussowitsch     PetscCall(PetscFree(cols));
1544e7e92044SBarry Smith     /* do not copy values to GPU since they are all zero and not yet needed there */
15459566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_TRUE));
15469566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
15479566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
15489566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_FALSE));
15499566063dSJacob Faibussowitsch     PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
1550ce308e1dSBarry Smith   }
15513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1552ce308e1dSBarry Smith }
1553ce308e1dSBarry Smith 
DMCreateMatrix_DA_1d_MPIAIJ(DM da,Mat J)1554d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_1d_MPIAIJ(DM da, Mat J)
1555d71ae5a4SJacob Faibussowitsch {
155647c6ae99SBarry Smith   PetscInt               xs, nx, i, i1, slot, gxs, gnx;
15570298fd71SBarry Smith   PetscInt               m, dim, s, *cols = NULL, nc, *rows = NULL, col, cnt, l;
155847c6ae99SBarry Smith   PetscInt               istart, iend;
1559bff4a2f0SMatthew G. Knepley   DMBoundaryType         bx;
1560844bd0d7SStefano Zampini   ISLocalToGlobalMapping ltog, mltog;
156147c6ae99SBarry Smith 
156247c6ae99SBarry Smith   PetscFunctionBegin;
156347c6ae99SBarry Smith   /*
156447c6ae99SBarry Smith          nc - number of components per grid point
156547c6ae99SBarry Smith          col - number of colors needed in one direction for single component problem
156647c6ae99SBarry Smith   */
15679566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, NULL, NULL, NULL, NULL, NULL, &nc, &s, &bx, NULL, NULL, NULL));
156848a46eb9SPierre Jolivet   if (bx == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_TRUE));
156947c6ae99SBarry Smith   col = 2 * s + 1;
157047c6ae99SBarry Smith 
15719566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &nx, NULL, NULL));
15729566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, NULL, NULL, &gnx, NULL, NULL));
157347c6ae99SBarry Smith 
15749566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, nc));
15759566063dSJacob Faibussowitsch   PetscCall(MatSeqAIJSetPreallocation(J, col * nc, NULL));
15769566063dSJacob Faibussowitsch   PetscCall(MatMPIAIJSetPreallocation(J, col * nc, NULL, col * nc, NULL));
157747c6ae99SBarry Smith 
15789566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
15799566063dSJacob Faibussowitsch   PetscCall(MatGetLocalToGlobalMapping(J, &mltog, NULL));
158048a46eb9SPierre Jolivet   if (!mltog) PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
158147c6ae99SBarry Smith 
158247c6ae99SBarry Smith   /*
158347c6ae99SBarry Smith     For each node in the grid: we get the neighbors in the local (on processor ordering
158447c6ae99SBarry Smith     that includes the ghost points) then MatSetValuesLocal() maps those indices to the global
158547c6ae99SBarry Smith     PETSc ordering.
158647c6ae99SBarry Smith   */
1587fcfd50ebSBarry Smith   if (!da->prealloc_only) {
15889566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(nc, &rows, col * nc * nc, &cols));
158947c6ae99SBarry Smith     for (i = xs; i < xs + nx; i++) {
159047c6ae99SBarry Smith       istart = PetscMax(-s, gxs - i);
159147c6ae99SBarry Smith       iend   = PetscMin(s, gxs + gnx - i - 1);
159247c6ae99SBarry Smith       slot   = i - gxs;
159347c6ae99SBarry Smith 
159447c6ae99SBarry Smith       cnt = 0;
159547c6ae99SBarry Smith       for (i1 = istart; i1 < iend + 1; i1++) {
1596071fcb05SBarry Smith         cols[cnt++] = nc * (slot + i1);
1597071fcb05SBarry Smith         for (l = 1; l < nc; l++) {
15989371c9d4SSatish Balay           cols[cnt] = 1 + cols[cnt - 1];
15999371c9d4SSatish Balay           cnt++;
160047c6ae99SBarry Smith         }
160147c6ae99SBarry Smith       }
16029371c9d4SSatish Balay       rows[0] = nc * (slot);
16039371c9d4SSatish Balay       for (l = 1; l < nc; l++) rows[l] = 1 + rows[l - 1];
16049566063dSJacob Faibussowitsch       PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, NULL, INSERT_VALUES));
160547c6ae99SBarry Smith     }
1606e7e92044SBarry Smith     /* do not copy values to GPU since they are all zero and not yet needed there */
16079566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_TRUE));
16089566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
16099566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
161048a46eb9SPierre Jolivet     if (bx == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE));
16119566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_FALSE));
16129566063dSJacob Faibussowitsch     PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
16139566063dSJacob Faibussowitsch     PetscCall(PetscFree2(rows, cols));
1614ce308e1dSBarry Smith   }
16153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
161647c6ae99SBarry Smith }
161747c6ae99SBarry Smith 
DMCreateMatrix_DA_1d_SeqAIJ_NoPreallocation(DM da,Mat J)1618d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_1d_SeqAIJ_NoPreallocation(DM da, Mat J)
1619d71ae5a4SJacob Faibussowitsch {
162019b08ed1SBarry Smith   PetscInt               xs, nx, i, i1, slot, gxs, gnx;
162119b08ed1SBarry Smith   PetscInt               m, dim, s, *cols = NULL, nc, *rows = NULL, col, cnt, l;
162219b08ed1SBarry Smith   PetscInt               istart, iend;
162319b08ed1SBarry Smith   DMBoundaryType         bx;
162419b08ed1SBarry Smith   ISLocalToGlobalMapping ltog, mltog;
162519b08ed1SBarry Smith 
162619b08ed1SBarry Smith   PetscFunctionBegin;
162719b08ed1SBarry Smith   /*
162819b08ed1SBarry Smith          nc - number of components per grid point
162919b08ed1SBarry Smith          col - number of colors needed in one direction for single component problem
163019b08ed1SBarry Smith   */
16319566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, NULL, NULL, NULL, NULL, NULL, &nc, &s, &bx, NULL, NULL, NULL));
163219b08ed1SBarry Smith   col = 2 * s + 1;
163319b08ed1SBarry Smith 
16349566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &nx, NULL, NULL));
16359566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, NULL, NULL, &gnx, NULL, NULL));
163619b08ed1SBarry Smith 
16379566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, nc));
16389566063dSJacob Faibussowitsch   PetscCall(MatSeqAIJSetTotalPreallocation(J, nx * nc * col * nc));
163919b08ed1SBarry Smith 
16409566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
16419566063dSJacob Faibussowitsch   PetscCall(MatGetLocalToGlobalMapping(J, &mltog, NULL));
164248a46eb9SPierre Jolivet   if (!mltog) PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
164319b08ed1SBarry Smith 
164419b08ed1SBarry Smith   /*
164519b08ed1SBarry Smith     For each node in the grid: we get the neighbors in the local (on processor ordering
164619b08ed1SBarry Smith     that includes the ghost points) then MatSetValuesLocal() maps those indices to the global
164719b08ed1SBarry Smith     PETSc ordering.
164819b08ed1SBarry Smith   */
164919b08ed1SBarry Smith   if (!da->prealloc_only) {
16509566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(nc, &rows, col * nc * nc, &cols));
165119b08ed1SBarry Smith     for (i = xs; i < xs + nx; i++) {
165219b08ed1SBarry Smith       istart = PetscMax(-s, gxs - i);
165319b08ed1SBarry Smith       iend   = PetscMin(s, gxs + gnx - i - 1);
165419b08ed1SBarry Smith       slot   = i - gxs;
165519b08ed1SBarry Smith 
165619b08ed1SBarry Smith       cnt = 0;
165719b08ed1SBarry Smith       for (i1 = istart; i1 < iend + 1; i1++) {
165819b08ed1SBarry Smith         cols[cnt++] = nc * (slot + i1);
165919b08ed1SBarry Smith         for (l = 1; l < nc; l++) {
16609371c9d4SSatish Balay           cols[cnt] = 1 + cols[cnt - 1];
16619371c9d4SSatish Balay           cnt++;
166219b08ed1SBarry Smith         }
166319b08ed1SBarry Smith       }
16649371c9d4SSatish Balay       rows[0] = nc * (slot);
16659371c9d4SSatish Balay       for (l = 1; l < nc; l++) rows[l] = 1 + rows[l - 1];
16669566063dSJacob Faibussowitsch       PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, NULL, INSERT_VALUES));
166719b08ed1SBarry Smith     }
166819b08ed1SBarry Smith     /* do not copy values to GPU since they are all zero and not yet needed there */
16699566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_TRUE));
16709566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
16719566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
167248a46eb9SPierre Jolivet     if (bx == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE));
16739566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_FALSE));
16749566063dSJacob Faibussowitsch     PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
16759566063dSJacob Faibussowitsch     PetscCall(PetscFree2(rows, cols));
167619b08ed1SBarry Smith   }
16779566063dSJacob Faibussowitsch   PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE));
16783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
167919b08ed1SBarry Smith }
168019b08ed1SBarry Smith 
DMCreateMatrix_DA_2d_MPIBAIJ(DM da,Mat J)1681d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPIBAIJ(DM da, Mat J)
1682d71ae5a4SJacob Faibussowitsch {
168347c6ae99SBarry Smith   PetscInt               xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny;
168447c6ae99SBarry Smith   PetscInt               m, n, dim, s, *cols, nc, col, cnt, *dnz, *onz;
168547c6ae99SBarry Smith   PetscInt               istart, iend, jstart, jend, ii, jj;
168647c6ae99SBarry Smith   MPI_Comm               comm;
168747c6ae99SBarry Smith   PetscScalar           *values;
1688bff4a2f0SMatthew G. Knepley   DMBoundaryType         bx, by;
1689aa219208SBarry Smith   DMDAStencilType        st;
169045b6f7e9SBarry Smith   ISLocalToGlobalMapping ltog;
169147c6ae99SBarry Smith 
169247c6ae99SBarry Smith   PetscFunctionBegin;
169347c6ae99SBarry Smith   /*
169447c6ae99SBarry Smith      nc - number of components per grid point
169547c6ae99SBarry Smith      col - number of colors needed in one direction for single component problem
169647c6ae99SBarry Smith   */
16979566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, NULL, NULL, NULL, &nc, &s, &bx, &by, NULL, &st));
169847c6ae99SBarry Smith   col = 2 * s + 1;
169947c6ae99SBarry Smith 
17009566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL));
17019566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL));
17029566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
170347c6ae99SBarry Smith 
17049566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(col * col * nc * nc, &cols));
170547c6ae99SBarry Smith 
17069566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
170747c6ae99SBarry Smith 
170847c6ae99SBarry Smith   /* determine the matrix preallocation information */
1709d0609cedSBarry Smith   MatPreallocateBegin(comm, nx * ny, nx * ny, dnz, onz);
171047c6ae99SBarry Smith   for (i = xs; i < xs + nx; i++) {
1711bff4a2f0SMatthew G. Knepley     istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
1712bff4a2f0SMatthew G. Knepley     iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
171347c6ae99SBarry Smith     for (j = ys; j < ys + ny; j++) {
1714bff4a2f0SMatthew G. Knepley       jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
1715bff4a2f0SMatthew G. Knepley       jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
171647c6ae99SBarry Smith       slot   = i - gxs + gnx * (j - gys);
171747c6ae99SBarry Smith 
171847c6ae99SBarry Smith       /* Find block columns in block row */
171947c6ae99SBarry Smith       cnt = 0;
172047c6ae99SBarry Smith       for (ii = istart; ii < iend + 1; ii++) {
172147c6ae99SBarry Smith         for (jj = jstart; jj < jend + 1; jj++) {
1722aa219208SBarry Smith           if (st == DMDA_STENCIL_BOX || !ii || !jj) { /* BOX or on the STAR */
172347c6ae99SBarry Smith             cols[cnt++] = slot + ii + gnx * jj;
172447c6ae99SBarry Smith           }
172547c6ae99SBarry Smith         }
172647c6ae99SBarry Smith       }
17279566063dSJacob Faibussowitsch       PetscCall(MatPreallocateSetLocalBlock(ltog, 1, &slot, ltog, cnt, cols, dnz, onz));
172847c6ae99SBarry Smith     }
172947c6ae99SBarry Smith   }
17309566063dSJacob Faibussowitsch   PetscCall(MatSeqBAIJSetPreallocation(J, nc, 0, dnz));
17319566063dSJacob Faibussowitsch   PetscCall(MatMPIBAIJSetPreallocation(J, nc, 0, dnz, 0, onz));
1732d0609cedSBarry Smith   MatPreallocateEnd(dnz, onz);
173347c6ae99SBarry Smith 
17349566063dSJacob Faibussowitsch   PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
173547c6ae99SBarry Smith 
173647c6ae99SBarry Smith   /*
173747c6ae99SBarry Smith     For each node in the grid: we get the neighbors in the local (on processor ordering
173847c6ae99SBarry Smith     that includes the ghost points) then MatSetValuesLocal() maps those indices to the global
173947c6ae99SBarry Smith     PETSc ordering.
174047c6ae99SBarry Smith   */
1741fcfd50ebSBarry Smith   if (!da->prealloc_only) {
17429566063dSJacob Faibussowitsch     PetscCall(PetscCalloc1(col * col * nc * nc, &values));
174347c6ae99SBarry Smith     for (i = xs; i < xs + nx; i++) {
1744bff4a2f0SMatthew G. Knepley       istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
1745bff4a2f0SMatthew G. Knepley       iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
174647c6ae99SBarry Smith       for (j = ys; j < ys + ny; j++) {
1747bff4a2f0SMatthew G. Knepley         jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
1748bff4a2f0SMatthew G. Knepley         jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
174947c6ae99SBarry Smith         slot   = i - gxs + gnx * (j - gys);
175047c6ae99SBarry Smith         cnt    = 0;
175147c6ae99SBarry Smith         for (ii = istart; ii < iend + 1; ii++) {
175247c6ae99SBarry Smith           for (jj = jstart; jj < jend + 1; jj++) {
1753aa219208SBarry Smith             if (st == DMDA_STENCIL_BOX || !ii || !jj) { /* BOX or on the STAR */
175447c6ae99SBarry Smith               cols[cnt++] = slot + ii + gnx * jj;
175547c6ae99SBarry Smith             }
175647c6ae99SBarry Smith           }
175747c6ae99SBarry Smith         }
17589566063dSJacob Faibussowitsch         PetscCall(MatSetValuesBlockedLocal(J, 1, &slot, cnt, cols, values, INSERT_VALUES));
175947c6ae99SBarry Smith       }
176047c6ae99SBarry Smith     }
17619566063dSJacob Faibussowitsch     PetscCall(PetscFree(values));
1762e7e92044SBarry Smith     /* do not copy values to GPU since they are all zero and not yet needed there */
17639566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_TRUE));
17649566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
17659566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
17669566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_FALSE));
17679566063dSJacob Faibussowitsch     PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
176847c6ae99SBarry Smith   }
17699566063dSJacob Faibussowitsch   PetscCall(PetscFree(cols));
17703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
177147c6ae99SBarry Smith }
177247c6ae99SBarry Smith 
DMCreateMatrix_DA_3d_MPIBAIJ(DM da,Mat J)1773d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPIBAIJ(DM da, Mat J)
1774d71ae5a4SJacob Faibussowitsch {
177547c6ae99SBarry Smith   PetscInt               xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny;
177647c6ae99SBarry Smith   PetscInt               m, n, dim, s, *cols, k, nc, col, cnt, p, *dnz, *onz;
177747c6ae99SBarry Smith   PetscInt               istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk;
177847c6ae99SBarry Smith   MPI_Comm               comm;
177947c6ae99SBarry Smith   PetscScalar           *values;
1780bff4a2f0SMatthew G. Knepley   DMBoundaryType         bx, by, bz;
1781aa219208SBarry Smith   DMDAStencilType        st;
178245b6f7e9SBarry Smith   ISLocalToGlobalMapping ltog;
178347c6ae99SBarry Smith 
178447c6ae99SBarry Smith   PetscFunctionBegin;
178547c6ae99SBarry Smith   /*
178647c6ae99SBarry Smith          nc - number of components per grid point
178747c6ae99SBarry Smith          col - number of colors needed in one direction for single component problem
178847c6ae99SBarry Smith   */
17899566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, NULL, NULL, NULL, &nc, &s, &bx, &by, &bz, &st));
179047c6ae99SBarry Smith   col = 2 * s + 1;
179147c6ae99SBarry Smith 
17929566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz));
17939566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz));
17949566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
179547c6ae99SBarry Smith 
17969566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(col * col * col, &cols));
179747c6ae99SBarry Smith 
17989566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
179947c6ae99SBarry Smith 
180047c6ae99SBarry Smith   /* determine the matrix preallocation information */
1801d0609cedSBarry Smith   MatPreallocateBegin(comm, nx * ny * nz, nx * ny * nz, dnz, onz);
180247c6ae99SBarry Smith   for (i = xs; i < xs + nx; i++) {
1803bff4a2f0SMatthew G. Knepley     istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
1804bff4a2f0SMatthew G. Knepley     iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
180547c6ae99SBarry Smith     for (j = ys; j < ys + ny; j++) {
1806bff4a2f0SMatthew G. Knepley       jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
1807bff4a2f0SMatthew G. Knepley       jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
180847c6ae99SBarry Smith       for (k = zs; k < zs + nz; k++) {
1809bff4a2f0SMatthew G. Knepley         kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k));
1810bff4a2f0SMatthew G. Knepley         kend   = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1));
181147c6ae99SBarry Smith 
181247c6ae99SBarry Smith         slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs);
181347c6ae99SBarry Smith 
181447c6ae99SBarry Smith         /* Find block columns in block row */
181547c6ae99SBarry Smith         cnt = 0;
181647c6ae99SBarry Smith         for (ii = istart; ii < iend + 1; ii++) {
181747c6ae99SBarry Smith           for (jj = jstart; jj < jend + 1; jj++) {
181847c6ae99SBarry Smith             for (kk = kstart; kk < kend + 1; kk++) {
1819b6555650SPierre Jolivet               if (st == DMDA_STENCIL_BOX || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/
182047c6ae99SBarry Smith                 cols[cnt++] = slot + ii + gnx * jj + gnx * gny * kk;
182147c6ae99SBarry Smith               }
182247c6ae99SBarry Smith             }
182347c6ae99SBarry Smith           }
182447c6ae99SBarry Smith         }
18259566063dSJacob Faibussowitsch         PetscCall(MatPreallocateSetLocalBlock(ltog, 1, &slot, ltog, cnt, cols, dnz, onz));
182647c6ae99SBarry Smith       }
182747c6ae99SBarry Smith     }
182847c6ae99SBarry Smith   }
18299566063dSJacob Faibussowitsch   PetscCall(MatSeqBAIJSetPreallocation(J, nc, 0, dnz));
18309566063dSJacob Faibussowitsch   PetscCall(MatMPIBAIJSetPreallocation(J, nc, 0, dnz, 0, onz));
1831d0609cedSBarry Smith   MatPreallocateEnd(dnz, onz);
183247c6ae99SBarry Smith 
18339566063dSJacob Faibussowitsch   PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
183447c6ae99SBarry Smith 
183547c6ae99SBarry Smith   /*
183647c6ae99SBarry Smith     For each node in the grid: we get the neighbors in the local (on processor ordering
183747c6ae99SBarry Smith     that includes the ghost points) then MatSetValuesLocal() maps those indices to the global
183847c6ae99SBarry Smith     PETSc ordering.
183947c6ae99SBarry Smith   */
1840fcfd50ebSBarry Smith   if (!da->prealloc_only) {
18419566063dSJacob Faibussowitsch     PetscCall(PetscCalloc1(col * col * col * nc * nc, &values));
184247c6ae99SBarry Smith     for (i = xs; i < xs + nx; i++) {
1843bff4a2f0SMatthew G. Knepley       istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
1844bff4a2f0SMatthew G. Knepley       iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
184547c6ae99SBarry Smith       for (j = ys; j < ys + ny; j++) {
1846bff4a2f0SMatthew G. Knepley         jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
1847bff4a2f0SMatthew G. Knepley         jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
184847c6ae99SBarry Smith         for (k = zs; k < zs + nz; k++) {
1849bff4a2f0SMatthew G. Knepley           kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k));
1850bff4a2f0SMatthew G. Knepley           kend   = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1));
185147c6ae99SBarry Smith 
185247c6ae99SBarry Smith           slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs);
185347c6ae99SBarry Smith 
185447c6ae99SBarry Smith           cnt = 0;
185547c6ae99SBarry Smith           for (ii = istart; ii < iend + 1; ii++) {
185647c6ae99SBarry Smith             for (jj = jstart; jj < jend + 1; jj++) {
185747c6ae99SBarry Smith               for (kk = kstart; kk < kend + 1; kk++) {
1858b6555650SPierre Jolivet                 if (st == DMDA_STENCIL_BOX || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/
185947c6ae99SBarry Smith                   cols[cnt++] = slot + ii + gnx * jj + gnx * gny * kk;
186047c6ae99SBarry Smith                 }
186147c6ae99SBarry Smith               }
186247c6ae99SBarry Smith             }
186347c6ae99SBarry Smith           }
18649566063dSJacob Faibussowitsch           PetscCall(MatSetValuesBlockedLocal(J, 1, &slot, cnt, cols, values, INSERT_VALUES));
186547c6ae99SBarry Smith         }
186647c6ae99SBarry Smith       }
186747c6ae99SBarry Smith     }
18689566063dSJacob Faibussowitsch     PetscCall(PetscFree(values));
1869e7e92044SBarry Smith     /* do not copy values to GPU since they are all zero and not yet needed there */
18709566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_TRUE));
18719566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
18729566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
18739566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_FALSE));
18749566063dSJacob Faibussowitsch     PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
187547c6ae99SBarry Smith   }
18769566063dSJacob Faibussowitsch   PetscCall(PetscFree(cols));
18773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
187847c6ae99SBarry Smith }
187947c6ae99SBarry Smith 
188047c6ae99SBarry Smith /*
188147c6ae99SBarry Smith   This helper is for of SBAIJ preallocation, to discard the lower-triangular values which are difficult to
188247c6ae99SBarry Smith   identify in the local ordering with periodic domain.
188347c6ae99SBarry Smith */
L2GFilterUpperTriangular(ISLocalToGlobalMapping ltog,PetscInt * row,PetscInt * cnt,PetscInt col[])1884d71ae5a4SJacob Faibussowitsch static PetscErrorCode L2GFilterUpperTriangular(ISLocalToGlobalMapping ltog, PetscInt *row, PetscInt *cnt, PetscInt col[])
1885d71ae5a4SJacob Faibussowitsch {
188647c6ae99SBarry Smith   PetscInt i, n;
188747c6ae99SBarry Smith 
188847c6ae99SBarry Smith   PetscFunctionBegin;
18899566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingApplyBlock(ltog, 1, row, row));
18909566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingApplyBlock(ltog, *cnt, col, col));
189147c6ae99SBarry Smith   for (i = 0, n = 0; i < *cnt; i++) {
189247c6ae99SBarry Smith     if (col[i] >= *row) col[n++] = col[i];
189347c6ae99SBarry Smith   }
189447c6ae99SBarry Smith   *cnt = n;
18953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
189647c6ae99SBarry Smith }
189747c6ae99SBarry Smith 
DMCreateMatrix_DA_2d_MPISBAIJ(DM da,Mat J)1898d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPISBAIJ(DM da, Mat J)
1899d71ae5a4SJacob Faibussowitsch {
190047c6ae99SBarry Smith   PetscInt               xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny;
190147c6ae99SBarry Smith   PetscInt               m, n, dim, s, *cols, nc, col, cnt, *dnz, *onz;
190247c6ae99SBarry Smith   PetscInt               istart, iend, jstart, jend, ii, jj;
190347c6ae99SBarry Smith   MPI_Comm               comm;
190447c6ae99SBarry Smith   PetscScalar           *values;
1905bff4a2f0SMatthew G. Knepley   DMBoundaryType         bx, by;
1906aa219208SBarry Smith   DMDAStencilType        st;
190745b6f7e9SBarry Smith   ISLocalToGlobalMapping ltog;
190847c6ae99SBarry Smith 
190947c6ae99SBarry Smith   PetscFunctionBegin;
191047c6ae99SBarry Smith   /*
191147c6ae99SBarry Smith      nc - number of components per grid point
191247c6ae99SBarry Smith      col - number of colors needed in one direction for single component problem
191347c6ae99SBarry Smith   */
19149566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, NULL, NULL, NULL, &nc, &s, &bx, &by, NULL, &st));
191547c6ae99SBarry Smith   col = 2 * s + 1;
191647c6ae99SBarry Smith 
19179566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL));
19189566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL));
19199566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
192047c6ae99SBarry Smith 
19219566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(col * col * nc * nc, &cols));
192247c6ae99SBarry Smith 
19239566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
192447c6ae99SBarry Smith 
192547c6ae99SBarry Smith   /* determine the matrix preallocation information */
1926d0609cedSBarry Smith   MatPreallocateBegin(comm, nx * ny, nx * ny, dnz, onz);
192747c6ae99SBarry Smith   for (i = xs; i < xs + nx; i++) {
1928bff4a2f0SMatthew G. Knepley     istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
1929bff4a2f0SMatthew G. Knepley     iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
193047c6ae99SBarry Smith     for (j = ys; j < ys + ny; j++) {
1931bff4a2f0SMatthew G. Knepley       jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
1932bff4a2f0SMatthew G. Knepley       jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
193347c6ae99SBarry Smith       slot   = i - gxs + gnx * (j - gys);
193447c6ae99SBarry Smith 
193547c6ae99SBarry Smith       /* Find block columns in block row */
193647c6ae99SBarry Smith       cnt = 0;
193747c6ae99SBarry Smith       for (ii = istart; ii < iend + 1; ii++) {
193847c6ae99SBarry Smith         for (jj = jstart; jj < jend + 1; jj++) {
1939ad540459SPierre Jolivet           if (st == DMDA_STENCIL_BOX || !ii || !jj) cols[cnt++] = slot + ii + gnx * jj;
194047c6ae99SBarry Smith         }
194147c6ae99SBarry Smith       }
19429566063dSJacob Faibussowitsch       PetscCall(L2GFilterUpperTriangular(ltog, &slot, &cnt, cols));
19439566063dSJacob Faibussowitsch       PetscCall(MatPreallocateSymmetricSetBlock(slot, cnt, cols, dnz, onz));
194447c6ae99SBarry Smith     }
194547c6ae99SBarry Smith   }
19469566063dSJacob Faibussowitsch   PetscCall(MatSeqSBAIJSetPreallocation(J, nc, 0, dnz));
19479566063dSJacob Faibussowitsch   PetscCall(MatMPISBAIJSetPreallocation(J, nc, 0, dnz, 0, onz));
1948d0609cedSBarry Smith   MatPreallocateEnd(dnz, onz);
194947c6ae99SBarry Smith 
19509566063dSJacob Faibussowitsch   PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
195147c6ae99SBarry Smith 
195247c6ae99SBarry Smith   /*
195347c6ae99SBarry Smith     For each node in the grid: we get the neighbors in the local (on processor ordering
195447c6ae99SBarry Smith     that includes the ghost points) then MatSetValuesLocal() maps those indices to the global
195547c6ae99SBarry Smith     PETSc ordering.
195647c6ae99SBarry Smith   */
1957fcfd50ebSBarry Smith   if (!da->prealloc_only) {
19589566063dSJacob Faibussowitsch     PetscCall(PetscCalloc1(col * col * nc * nc, &values));
195947c6ae99SBarry Smith     for (i = xs; i < xs + nx; i++) {
1960bff4a2f0SMatthew G. Knepley       istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
1961bff4a2f0SMatthew G. Knepley       iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
196247c6ae99SBarry Smith       for (j = ys; j < ys + ny; j++) {
1963bff4a2f0SMatthew G. Knepley         jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
1964bff4a2f0SMatthew G. Knepley         jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
196547c6ae99SBarry Smith         slot   = i - gxs + gnx * (j - gys);
196647c6ae99SBarry Smith 
196747c6ae99SBarry Smith         /* Find block columns in block row */
196847c6ae99SBarry Smith         cnt = 0;
196947c6ae99SBarry Smith         for (ii = istart; ii < iend + 1; ii++) {
197047c6ae99SBarry Smith           for (jj = jstart; jj < jend + 1; jj++) {
1971ad540459SPierre Jolivet             if (st == DMDA_STENCIL_BOX || !ii || !jj) cols[cnt++] = slot + ii + gnx * jj;
197247c6ae99SBarry Smith           }
197347c6ae99SBarry Smith         }
19749566063dSJacob Faibussowitsch         PetscCall(L2GFilterUpperTriangular(ltog, &slot, &cnt, cols));
19759566063dSJacob Faibussowitsch         PetscCall(MatSetValuesBlocked(J, 1, &slot, cnt, cols, values, INSERT_VALUES));
197647c6ae99SBarry Smith       }
197747c6ae99SBarry Smith     }
19789566063dSJacob Faibussowitsch     PetscCall(PetscFree(values));
1979e7e92044SBarry Smith     /* do not copy values to GPU since they are all zero and not yet needed there */
19809566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_TRUE));
19819566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
19829566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
19839566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_FALSE));
19849566063dSJacob Faibussowitsch     PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
198547c6ae99SBarry Smith   }
19869566063dSJacob Faibussowitsch   PetscCall(PetscFree(cols));
19873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
198847c6ae99SBarry Smith }
198947c6ae99SBarry Smith 
DMCreateMatrix_DA_3d_MPISBAIJ(DM da,Mat J)1990d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPISBAIJ(DM da, Mat J)
1991d71ae5a4SJacob Faibussowitsch {
199247c6ae99SBarry Smith   PetscInt               xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny;
199347c6ae99SBarry Smith   PetscInt               m, n, dim, s, *cols, k, nc, col, cnt, p, *dnz, *onz;
199447c6ae99SBarry Smith   PetscInt               istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk;
199547c6ae99SBarry Smith   MPI_Comm               comm;
199647c6ae99SBarry Smith   PetscScalar           *values;
1997bff4a2f0SMatthew G. Knepley   DMBoundaryType         bx, by, bz;
1998aa219208SBarry Smith   DMDAStencilType        st;
199945b6f7e9SBarry Smith   ISLocalToGlobalMapping ltog;
200047c6ae99SBarry Smith 
200147c6ae99SBarry Smith   PetscFunctionBegin;
200247c6ae99SBarry Smith   /*
200347c6ae99SBarry Smith      nc - number of components per grid point
200447c6ae99SBarry Smith      col - number of colors needed in one direction for single component problem
200547c6ae99SBarry Smith   */
20069566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, NULL, NULL, NULL, &nc, &s, &bx, &by, &bz, &st));
200747c6ae99SBarry Smith   col = 2 * s + 1;
200847c6ae99SBarry Smith 
20099566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz));
20109566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz));
20119566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
201247c6ae99SBarry Smith 
201347c6ae99SBarry Smith   /* create the matrix */
20149566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(col * col * col, &cols));
201547c6ae99SBarry Smith 
20169566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
201747c6ae99SBarry Smith 
201847c6ae99SBarry Smith   /* determine the matrix preallocation information */
2019d0609cedSBarry Smith   MatPreallocateBegin(comm, nx * ny * nz, nx * ny * nz, dnz, onz);
202047c6ae99SBarry Smith   for (i = xs; i < xs + nx; i++) {
2021bff4a2f0SMatthew G. Knepley     istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
2022bff4a2f0SMatthew G. Knepley     iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
202347c6ae99SBarry Smith     for (j = ys; j < ys + ny; j++) {
2024bff4a2f0SMatthew G. Knepley       jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
2025bff4a2f0SMatthew G. Knepley       jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
202647c6ae99SBarry Smith       for (k = zs; k < zs + nz; k++) {
2027bff4a2f0SMatthew G. Knepley         kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k));
2028bff4a2f0SMatthew G. Knepley         kend   = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1));
202947c6ae99SBarry Smith 
203047c6ae99SBarry Smith         slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs);
203147c6ae99SBarry Smith 
203247c6ae99SBarry Smith         /* Find block columns in block row */
203347c6ae99SBarry Smith         cnt = 0;
203447c6ae99SBarry Smith         for (ii = istart; ii < iend + 1; ii++) {
203547c6ae99SBarry Smith           for (jj = jstart; jj < jend + 1; jj++) {
203647c6ae99SBarry Smith             for (kk = kstart; kk < kend + 1; kk++) {
2037b6555650SPierre Jolivet               if (st == DMDA_STENCIL_BOX || (!ii && !jj) || (!jj && !kk) || (!ii && !kk)) cols[cnt++] = slot + ii + gnx * jj + gnx * gny * kk;
203847c6ae99SBarry Smith             }
203947c6ae99SBarry Smith           }
204047c6ae99SBarry Smith         }
20419566063dSJacob Faibussowitsch         PetscCall(L2GFilterUpperTriangular(ltog, &slot, &cnt, cols));
20429566063dSJacob Faibussowitsch         PetscCall(MatPreallocateSymmetricSetBlock(slot, cnt, cols, dnz, onz));
204347c6ae99SBarry Smith       }
204447c6ae99SBarry Smith     }
204547c6ae99SBarry Smith   }
20469566063dSJacob Faibussowitsch   PetscCall(MatSeqSBAIJSetPreallocation(J, nc, 0, dnz));
20479566063dSJacob Faibussowitsch   PetscCall(MatMPISBAIJSetPreallocation(J, nc, 0, dnz, 0, onz));
2048d0609cedSBarry Smith   MatPreallocateEnd(dnz, onz);
204947c6ae99SBarry Smith 
20509566063dSJacob Faibussowitsch   PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
205147c6ae99SBarry Smith 
205247c6ae99SBarry Smith   /*
205347c6ae99SBarry Smith     For each node in the grid: we get the neighbors in the local (on processor ordering
205447c6ae99SBarry Smith     that includes the ghost points) then MatSetValuesLocal() maps those indices to the global
205547c6ae99SBarry Smith     PETSc ordering.
205647c6ae99SBarry Smith   */
2057fcfd50ebSBarry Smith   if (!da->prealloc_only) {
20589566063dSJacob Faibussowitsch     PetscCall(PetscCalloc1(col * col * col * nc * nc, &values));
205947c6ae99SBarry Smith     for (i = xs; i < xs + nx; i++) {
2060bff4a2f0SMatthew G. Knepley       istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
2061bff4a2f0SMatthew G. Knepley       iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
206247c6ae99SBarry Smith       for (j = ys; j < ys + ny; j++) {
2063bff4a2f0SMatthew G. Knepley         jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
2064bff4a2f0SMatthew G. Knepley         jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
206547c6ae99SBarry Smith         for (k = zs; k < zs + nz; k++) {
2066bff4a2f0SMatthew G. Knepley           kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k));
2067bff4a2f0SMatthew G. Knepley           kend   = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1));
206847c6ae99SBarry Smith 
206947c6ae99SBarry Smith           slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs);
207047c6ae99SBarry Smith 
207147c6ae99SBarry Smith           cnt = 0;
207247c6ae99SBarry Smith           for (ii = istart; ii < iend + 1; ii++) {
207347c6ae99SBarry Smith             for (jj = jstart; jj < jend + 1; jj++) {
207447c6ae99SBarry Smith               for (kk = kstart; kk < kend + 1; kk++) {
2075b6555650SPierre Jolivet                 if (st == DMDA_STENCIL_BOX || (!ii && !jj) || (!jj && !kk) || (!ii && !kk)) cols[cnt++] = slot + ii + gnx * jj + gnx * gny * kk;
207647c6ae99SBarry Smith               }
207747c6ae99SBarry Smith             }
207847c6ae99SBarry Smith           }
20799566063dSJacob Faibussowitsch           PetscCall(L2GFilterUpperTriangular(ltog, &slot, &cnt, cols));
20809566063dSJacob Faibussowitsch           PetscCall(MatSetValuesBlocked(J, 1, &slot, cnt, cols, values, INSERT_VALUES));
208147c6ae99SBarry Smith         }
208247c6ae99SBarry Smith       }
208347c6ae99SBarry Smith     }
20849566063dSJacob Faibussowitsch     PetscCall(PetscFree(values));
2085e7e92044SBarry Smith     /* do not copy values to GPU since they are all zero and not yet needed there */
20869566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_TRUE));
20879566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
20889566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
20899566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_FALSE));
20909566063dSJacob Faibussowitsch     PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
209147c6ae99SBarry Smith   }
20929566063dSJacob Faibussowitsch   PetscCall(PetscFree(cols));
20933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
209447c6ae99SBarry Smith }
209547c6ae99SBarry Smith 
DMCreateMatrix_DA_3d_MPIAIJ_Fill(DM da,Mat J)2096d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPIAIJ_Fill(DM da, Mat J)
2097d71ae5a4SJacob Faibussowitsch {
209847c6ae99SBarry Smith   PetscInt               xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny;
2099c0ab637bSBarry Smith   PetscInt               m, n, dim, s, *cols, k, nc, row, col, cnt, maxcnt = 0, l, p, *dnz, *onz;
2100c1154cd5SBarry Smith   PetscInt               istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk, M, N, P;
210147c6ae99SBarry Smith   DM_DA                 *dd = (DM_DA *)da->data;
210247c6ae99SBarry Smith   PetscInt               ifill_col, *dfill = dd->dfill, *ofill = dd->ofill;
210347c6ae99SBarry Smith   MPI_Comm               comm;
210447c6ae99SBarry Smith   PetscScalar           *values;
2105bff4a2f0SMatthew G. Knepley   DMBoundaryType         bx, by, bz;
210645b6f7e9SBarry Smith   ISLocalToGlobalMapping ltog;
2107aa219208SBarry Smith   DMDAStencilType        st;
2108c1154cd5SBarry Smith   PetscBool              removedups = PETSC_FALSE;
210947c6ae99SBarry Smith 
211047c6ae99SBarry Smith   PetscFunctionBegin;
211147c6ae99SBarry Smith   /*
211247c6ae99SBarry Smith          nc - number of components per grid point
211347c6ae99SBarry Smith          col - number of colors needed in one direction for single component problem
211447c6ae99SBarry Smith   */
21159566063dSJacob Faibussowitsch   PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &nc, &s, &bx, &by, &bz, &st));
211647c6ae99SBarry Smith   col = 2 * s + 1;
211747c6ae99SBarry Smith 
2118c1154cd5SBarry Smith   /*
2119c1154cd5SBarry Smith        With one processor in periodic domains in a skinny dimension the code will label nonzero columns multiple times
2120c1154cd5SBarry Smith        because of "wrapping" around the end of the domain hitting an entry already counted in the other direction.
2121c1154cd5SBarry Smith   */
2122c1154cd5SBarry Smith   if (M == 1 && 2 * s >= m) removedups = PETSC_TRUE;
2123c1154cd5SBarry Smith   if (N == 1 && 2 * s >= n) removedups = PETSC_TRUE;
2124c1154cd5SBarry Smith   if (P == 1 && 2 * s >= p) removedups = PETSC_TRUE;
2125c1154cd5SBarry Smith 
21269566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz));
21279566063dSJacob Faibussowitsch   PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz));
21289566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)da, &comm));
212947c6ae99SBarry Smith 
21309566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(col * col * col * nc, &cols));
21319566063dSJacob Faibussowitsch   PetscCall(DMGetLocalToGlobalMapping(da, &ltog));
213247c6ae99SBarry Smith 
213347c6ae99SBarry Smith   /* determine the matrix preallocation information */
2134d0609cedSBarry Smith   MatPreallocateBegin(comm, nc * nx * ny * nz, nc * nx * ny * nz, dnz, onz);
213547c6ae99SBarry Smith 
21369566063dSJacob Faibussowitsch   PetscCall(MatSetBlockSize(J, nc));
213747c6ae99SBarry Smith   for (i = xs; i < xs + nx; i++) {
2138bff4a2f0SMatthew G. Knepley     istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
2139bff4a2f0SMatthew G. Knepley     iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
214047c6ae99SBarry Smith     for (j = ys; j < ys + ny; j++) {
2141bff4a2f0SMatthew G. Knepley       jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
2142bff4a2f0SMatthew G. Knepley       jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
214347c6ae99SBarry Smith       for (k = zs; k < zs + nz; k++) {
2144bff4a2f0SMatthew G. Knepley         kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k));
2145bff4a2f0SMatthew G. Knepley         kend   = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1));
214647c6ae99SBarry Smith 
214747c6ae99SBarry Smith         slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs);
214847c6ae99SBarry Smith 
214947c6ae99SBarry Smith         for (l = 0; l < nc; l++) {
215047c6ae99SBarry Smith           cnt = 0;
215147c6ae99SBarry Smith           for (ii = istart; ii < iend + 1; ii++) {
215247c6ae99SBarry Smith             for (jj = jstart; jj < jend + 1; jj++) {
215347c6ae99SBarry Smith               for (kk = kstart; kk < kend + 1; kk++) {
215447c6ae99SBarry Smith                 if (ii || jj || kk) {
2155b6555650SPierre Jolivet                   if (st == DMDA_STENCIL_BOX || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/
21568865f1eaSKarl 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);
215747c6ae99SBarry Smith                   }
215847c6ae99SBarry Smith                 } else {
215947c6ae99SBarry Smith                   if (dfill) {
21608865f1eaSKarl 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);
216147c6ae99SBarry Smith                   } else {
21628865f1eaSKarl Rupp                     for (ifill_col = 0; ifill_col < nc; ifill_col++) cols[cnt++] = ifill_col + nc * (slot + ii + gnx * jj + gnx * gny * kk);
216347c6ae99SBarry Smith                   }
216447c6ae99SBarry Smith                 }
216547c6ae99SBarry Smith               }
216647c6ae99SBarry Smith             }
216747c6ae99SBarry Smith           }
216847c6ae99SBarry Smith           row    = l + nc * (slot);
2169c0ab637bSBarry Smith           maxcnt = PetscMax(maxcnt, cnt);
21701baa6e33SBarry Smith           if (removedups) PetscCall(MatPreallocateSetLocalRemoveDups(ltog, 1, &row, ltog, cnt, cols, dnz, onz));
21711baa6e33SBarry Smith           else PetscCall(MatPreallocateSetLocal(ltog, 1, &row, ltog, cnt, cols, dnz, onz));
217247c6ae99SBarry Smith         }
217347c6ae99SBarry Smith       }
217447c6ae99SBarry Smith     }
2175c1154cd5SBarry Smith   }
21769566063dSJacob Faibussowitsch   PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz));
21779566063dSJacob Faibussowitsch   PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz));
2178d0609cedSBarry Smith   MatPreallocateEnd(dnz, onz);
21799566063dSJacob Faibussowitsch   PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog));
218047c6ae99SBarry Smith 
218147c6ae99SBarry Smith   /*
218247c6ae99SBarry Smith     For each node in the grid: we get the neighbors in the local (on processor ordering
218347c6ae99SBarry Smith     that includes the ghost points) then MatSetValuesLocal() maps those indices to the global
218447c6ae99SBarry Smith     PETSc ordering.
218547c6ae99SBarry Smith   */
2186fcfd50ebSBarry Smith   if (!da->prealloc_only) {
21879566063dSJacob Faibussowitsch     PetscCall(PetscCalloc1(maxcnt, &values));
218847c6ae99SBarry Smith     for (i = xs; i < xs + nx; i++) {
2189bff4a2f0SMatthew G. Knepley       istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i));
2190bff4a2f0SMatthew G. Knepley       iend   = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1));
219147c6ae99SBarry Smith       for (j = ys; j < ys + ny; j++) {
2192bff4a2f0SMatthew G. Knepley         jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j));
2193bff4a2f0SMatthew G. Knepley         jend   = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1));
219447c6ae99SBarry Smith         for (k = zs; k < zs + nz; k++) {
2195bff4a2f0SMatthew G. Knepley           kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k));
2196bff4a2f0SMatthew G. Knepley           kend   = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1));
219747c6ae99SBarry Smith 
219847c6ae99SBarry Smith           slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs);
219947c6ae99SBarry Smith 
220047c6ae99SBarry Smith           for (l = 0; l < nc; l++) {
220147c6ae99SBarry Smith             cnt = 0;
220247c6ae99SBarry Smith             for (ii = istart; ii < iend + 1; ii++) {
220347c6ae99SBarry Smith               for (jj = jstart; jj < jend + 1; jj++) {
220447c6ae99SBarry Smith                 for (kk = kstart; kk < kend + 1; kk++) {
220547c6ae99SBarry Smith                   if (ii || jj || kk) {
2206b6555650SPierre Jolivet                     if (st == DMDA_STENCIL_BOX || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/
22078865f1eaSKarl 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);
220847c6ae99SBarry Smith                     }
220947c6ae99SBarry Smith                   } else {
221047c6ae99SBarry Smith                     if (dfill) {
22118865f1eaSKarl 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);
221247c6ae99SBarry Smith                     } else {
22138865f1eaSKarl Rupp                       for (ifill_col = 0; ifill_col < nc; ifill_col++) cols[cnt++] = ifill_col + nc * (slot + ii + gnx * jj + gnx * gny * kk);
221447c6ae99SBarry Smith                     }
221547c6ae99SBarry Smith                   }
221647c6ae99SBarry Smith                 }
221747c6ae99SBarry Smith               }
221847c6ae99SBarry Smith             }
221947c6ae99SBarry Smith             row = l + nc * (slot);
22209566063dSJacob Faibussowitsch             PetscCall(MatSetValuesLocal(J, 1, &row, cnt, cols, values, INSERT_VALUES));
222147c6ae99SBarry Smith           }
222247c6ae99SBarry Smith         }
222347c6ae99SBarry Smith       }
222447c6ae99SBarry Smith     }
22259566063dSJacob Faibussowitsch     PetscCall(PetscFree(values));
2226e7e92044SBarry Smith     /* do not copy values to GPU since they are all zero and not yet needed there */
22279566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_TRUE));
22289566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
22299566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
22309566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(J, PETSC_FALSE));
22319566063dSJacob Faibussowitsch     PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
223247c6ae99SBarry Smith   }
22339566063dSJacob Faibussowitsch   PetscCall(PetscFree(cols));
22343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
223547c6ae99SBarry Smith }
2236