147c6ae99SBarry Smith 2af0996ceSBarry Smith #include <petsc/private/dmdaimpl.h> /*I "petscdmda.h" I*/ 307475bc1SBarry Smith #include <petscmat.h> 4e432b41dSStefano Zampini #include <petscbt.h> 547c6ae99SBarry Smith 6e727c939SJed Brown extern PetscErrorCode DMCreateColoring_DA_1d_MPIAIJ(DM, ISColoringType, ISColoring *); 7e727c939SJed Brown extern PetscErrorCode DMCreateColoring_DA_2d_MPIAIJ(DM, ISColoringType, ISColoring *); 8e727c939SJed Brown extern PetscErrorCode DMCreateColoring_DA_2d_5pt_MPIAIJ(DM, ISColoringType, ISColoring *); 9e727c939SJed Brown extern PetscErrorCode DMCreateColoring_DA_3d_MPIAIJ(DM, ISColoringType, ISColoring *); 1047c6ae99SBarry Smith 1147c6ae99SBarry Smith /* 1247c6ae99SBarry Smith For ghost i that may be negative or greater than the upper bound this 1347c6ae99SBarry Smith maps it into the 0:m-1 range using periodicity 1447c6ae99SBarry Smith */ 1547c6ae99SBarry Smith #define SetInRange(i, m) ((i < 0) ? m + i : ((i >= m) ? i - m : i)) 1647c6ae99SBarry Smith 17d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDASetBlockFills_Private(const PetscInt *dfill, PetscInt w, PetscInt **rfill) 18d71ae5a4SJacob Faibussowitsch { 1947c6ae99SBarry Smith PetscInt i, j, nz, *fill; 2047c6ae99SBarry Smith 2147c6ae99SBarry Smith PetscFunctionBegin; 223ba16761SJacob Faibussowitsch if (!dfill) PetscFunctionReturn(PETSC_SUCCESS); 2347c6ae99SBarry Smith 2447c6ae99SBarry Smith /* count number nonzeros */ 2547c6ae99SBarry Smith nz = 0; 2647c6ae99SBarry Smith for (i = 0; i < w; i++) { 2747c6ae99SBarry Smith for (j = 0; j < w; j++) { 2847c6ae99SBarry Smith if (dfill[w * i + j]) nz++; 2947c6ae99SBarry Smith } 3047c6ae99SBarry Smith } 319566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nz + w + 1, &fill)); 3247c6ae99SBarry Smith /* construct modified CSR storage of nonzero structure */ 33ce308e1dSBarry Smith /* fill[0 -- w] marks starts of each row of column indices (and end of last row) 34ce308e1dSBarry Smith so fill[1] - fill[0] gives number of nonzeros in first row etc */ 3547c6ae99SBarry Smith nz = w + 1; 3647c6ae99SBarry Smith for (i = 0; i < w; i++) { 3747c6ae99SBarry Smith fill[i] = nz; 3847c6ae99SBarry Smith for (j = 0; j < w; j++) { 3947c6ae99SBarry Smith if (dfill[w * i + j]) { 4047c6ae99SBarry Smith fill[nz] = j; 4147c6ae99SBarry Smith nz++; 4247c6ae99SBarry Smith } 4347c6ae99SBarry Smith } 4447c6ae99SBarry Smith } 4547c6ae99SBarry Smith fill[w] = nz; 4647c6ae99SBarry Smith 4747c6ae99SBarry Smith *rfill = fill; 483ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4947c6ae99SBarry Smith } 5047c6ae99SBarry Smith 51d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDASetBlockFillsSparse_Private(const PetscInt *dfillsparse, PetscInt w, PetscInt **rfill) 52d71ae5a4SJacob Faibussowitsch { 53767d920cSKarl Rupp PetscInt nz; 5409e28618SBarry Smith 5509e28618SBarry Smith PetscFunctionBegin; 563ba16761SJacob Faibussowitsch if (!dfillsparse) PetscFunctionReturn(PETSC_SUCCESS); 5709e28618SBarry Smith 5809e28618SBarry Smith /* Determine number of non-zeros */ 5909e28618SBarry Smith nz = (dfillsparse[w] - w - 1); 6009e28618SBarry Smith 6109e28618SBarry Smith /* Allocate space for our copy of the given sparse matrix representation. */ 629566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nz + w + 1, rfill)); 639566063dSJacob Faibussowitsch PetscCall(PetscArraycpy(*rfill, dfillsparse, nz + w + 1)); 643ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 6509e28618SBarry Smith } 6609e28618SBarry Smith 67d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDASetBlockFills_Private2(DM_DA *dd) 68d71ae5a4SJacob Faibussowitsch { 6909e28618SBarry Smith PetscInt i, k, cnt = 1; 7009e28618SBarry Smith 7109e28618SBarry Smith PetscFunctionBegin; 7209e28618SBarry Smith 7309e28618SBarry Smith /* ofillcount tracks the columns of ofill that have any nonzero in thems; the value in each location is the number of 7409e28618SBarry Smith columns to the left with any nonzeros in them plus 1 */ 759566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(dd->w, &dd->ofillcols)); 7609e28618SBarry Smith for (i = 0; i < dd->w; i++) { 7709e28618SBarry Smith for (k = dd->ofill[i]; k < dd->ofill[i + 1]; k++) dd->ofillcols[dd->ofill[k]] = 1; 7809e28618SBarry Smith } 7909e28618SBarry Smith for (i = 0; i < dd->w; i++) { 80ad540459SPierre Jolivet if (dd->ofillcols[i]) dd->ofillcols[i] = cnt++; 8109e28618SBarry Smith } 823ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 8309e28618SBarry Smith } 8409e28618SBarry Smith 8547c6ae99SBarry Smith /*@ 86aa219208SBarry Smith DMDASetBlockFills - Sets the fill pattern in each block for a multi-component problem 87dce8aebaSBarry Smith of the matrix returned by `DMCreateMatrix()`. 8847c6ae99SBarry Smith 89*20f4b53cSBarry Smith Logically Collective 9047c6ae99SBarry Smith 91d8d19677SJose E. Roman Input Parameters: 9247c6ae99SBarry Smith + da - the distributed array 930298fd71SBarry Smith . dfill - the fill pattern in the diagonal block (may be NULL, means use dense block) 9447c6ae99SBarry Smith - ofill - the fill pattern in the off-diagonal blocks 9547c6ae99SBarry Smith 9647c6ae99SBarry Smith Level: developer 9747c6ae99SBarry Smith 9895452b02SPatrick Sanan Notes: 9995452b02SPatrick Sanan This only makes sense when you are doing multicomponent problems but using the 100dce8aebaSBarry Smith `MATMPIAIJ` matrix format 10147c6ae99SBarry Smith 10247c6ae99SBarry Smith The format for dfill and ofill is a 2 dimensional dof by dof matrix with 1 entries 10347c6ae99SBarry Smith representing coupling and 0 entries for missing coupling. For example 104dce8aebaSBarry Smith .vb 105dce8aebaSBarry Smith dfill[9] = {1, 0, 0, 106dce8aebaSBarry Smith 1, 1, 0, 107dce8aebaSBarry Smith 0, 1, 1} 108dce8aebaSBarry Smith .ve 10947c6ae99SBarry Smith means that row 0 is coupled with only itself in the diagonal block, row 1 is coupled with 11047c6ae99SBarry Smith itself and row 0 (in the diagonal block) and row 2 is coupled with itself and row 1 (in the 11147c6ae99SBarry Smith diagonal block). 11247c6ae99SBarry Smith 113dce8aebaSBarry Smith `DMDASetGetMatrix()` allows you to provide general code for those more complicated nonzero patterns then 11447c6ae99SBarry Smith can be represented in the dfill, ofill format 11547c6ae99SBarry Smith 11647c6ae99SBarry Smith Contributed by Glenn Hammond 11747c6ae99SBarry Smith 118dce8aebaSBarry Smith .seealso: `DM`, `DMDA`, `DMCreateMatrix()`, `DMDASetGetMatrix()`, `DMSetMatrixPreallocateOnly()` 11947c6ae99SBarry Smith @*/ 120d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDASetBlockFills(DM da, const PetscInt *dfill, const PetscInt *ofill) 121d71ae5a4SJacob Faibussowitsch { 12247c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 12347c6ae99SBarry Smith 12447c6ae99SBarry Smith PetscFunctionBegin; 12509e28618SBarry Smith /* save the given dfill and ofill information */ 1269566063dSJacob Faibussowitsch PetscCall(DMDASetBlockFills_Private(dfill, dd->w, &dd->dfill)); 1279566063dSJacob Faibussowitsch PetscCall(DMDASetBlockFills_Private(ofill, dd->w, &dd->ofill)); 128ae4f298aSBarry Smith 12909e28618SBarry Smith /* count nonzeros in ofill columns */ 1309566063dSJacob Faibussowitsch PetscCall(DMDASetBlockFills_Private2(dd)); 1313ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 132ae4f298aSBarry Smith } 13309e28618SBarry Smith 13409e28618SBarry Smith /*@ 13509e28618SBarry Smith DMDASetBlockFillsSparse - Sets the fill pattern in each block for a multi-component problem 136dce8aebaSBarry Smith of the matrix returned by `DMCreateMatrix()`, using sparse representations 13709e28618SBarry Smith of fill patterns. 13809e28618SBarry Smith 139*20f4b53cSBarry Smith Logically Collective 14009e28618SBarry Smith 141d8d19677SJose E. Roman Input Parameters: 14209e28618SBarry Smith + da - the distributed array 143*20f4b53cSBarry Smith . dfill - the sparse fill pattern in the diagonal block (may be `NULL`, means use dense block) 14409e28618SBarry Smith - ofill - the sparse fill pattern in the off-diagonal blocks 14509e28618SBarry Smith 14609e28618SBarry Smith Level: developer 14709e28618SBarry Smith 148dce8aebaSBarry Smith Notes: 149dce8aebaSBarry Smith This only makes sense when you are doing multicomponent problems but using the 150dce8aebaSBarry Smith `MATMPIAIJ` matrix format 15109e28618SBarry Smith 152*20f4b53cSBarry Smith The format for `dfill` and `ofill` is a sparse representation of a 15309e28618SBarry Smith dof-by-dof matrix with 1 entries representing coupling and 0 entries 15409e28618SBarry Smith for missing coupling. The sparse representation is a 1 dimensional 15509e28618SBarry Smith array of length nz + dof + 1, where nz is the number of non-zeros in 15609e28618SBarry Smith the matrix. The first dof entries in the array give the 15709e28618SBarry Smith starting array indices of each row's items in the rest of the array, 15860942847SBarry Smith the dof+1st item contains the value nz + dof + 1 (i.e. the entire length of the array) 15909e28618SBarry Smith and the remaining nz items give the column indices of each of 16009e28618SBarry Smith the 1s within the logical 2D matrix. Each row's items within 16109e28618SBarry Smith the array are the column indices of the 1s within that row 16209e28618SBarry Smith of the 2D matrix. PETSc developers may recognize that this is the 163dce8aebaSBarry Smith same format as that computed by the `DMDASetBlockFills_Private()` 16409e28618SBarry Smith function from a dense 2D matrix representation. 16509e28618SBarry Smith 166dce8aebaSBarry Smith `DMDASetGetMatrix()` allows you to provide general code for those more complicated nonzero patterns then 167*20f4b53cSBarry Smith can be represented in the `dfill`, `ofill` format 16809e28618SBarry Smith 16909e28618SBarry Smith Contributed by Philip C. Roth 17009e28618SBarry Smith 171dce8aebaSBarry Smith .seealso: `DM`, `DMDA`, `DMDASetBlockFills()`, `DMCreateMatrix()`, `DMDASetGetMatrix()`, `DMSetMatrixPreallocateOnly()` 17209e28618SBarry Smith @*/ 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 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) { 22447c6ae99SBarry Smith if (size == 1) { 22547c6ae99SBarry Smith ctype = IS_COLORING_GLOBAL; 2262ddab442SBarry Smith } else { 2272ddab442SBarry Smith PetscCheck((dim == 1) || !((m == 1 && bx == DM_BOUNDARY_PERIODIC) || (n == 1 && by == DM_BOUNDARY_PERIODIC) || (p == 1 && bz == DM_BOUNDARY_PERIODIC)), PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "IS_COLORING_LOCAL cannot be used for periodic boundary condition having both ends of the domain on the same process"); 22847c6ae99SBarry Smith } 22947c6ae99SBarry Smith } 23047c6ae99SBarry Smith 231aa219208SBarry Smith /* Tell the DMDA it has 1 degree of freedom per grid point so that the coloring for BAIJ 23247c6ae99SBarry Smith matrices is for the blocks, not the individual matrix elements */ 2339566063dSJacob Faibussowitsch PetscCall(PetscStrbeginswith(da->mattype, MATBAIJ, &isBAIJ)); 2349566063dSJacob Faibussowitsch if (!isBAIJ) PetscCall(PetscStrbeginswith(da->mattype, MATMPIBAIJ, &isBAIJ)); 2359566063dSJacob Faibussowitsch if (!isBAIJ) PetscCall(PetscStrbeginswith(da->mattype, MATSEQBAIJ, &isBAIJ)); 23647c6ae99SBarry Smith if (isBAIJ) { 23747c6ae99SBarry Smith dd->w = 1; 23847c6ae99SBarry Smith dd->xs = dd->xs / nc; 23947c6ae99SBarry Smith dd->xe = dd->xe / nc; 24047c6ae99SBarry Smith dd->Xs = dd->Xs / nc; 24147c6ae99SBarry Smith dd->Xe = dd->Xe / nc; 24247c6ae99SBarry Smith } 24347c6ae99SBarry Smith 24447c6ae99SBarry Smith /* 245aa219208SBarry Smith We do not provide a getcoloring function in the DMDA operations because 2469a1b256bSStefano Zampini the basic DMDA does not know about matrices. We think of DMDA as being 24747c6ae99SBarry Smith more low-level then matrices. 24847c6ae99SBarry Smith */ 2491baa6e33SBarry Smith if (dim == 1) PetscCall(DMCreateColoring_DA_1d_MPIAIJ(da, ctype, coloring)); 2501baa6e33SBarry Smith else if (dim == 2) PetscCall(DMCreateColoring_DA_2d_MPIAIJ(da, ctype, coloring)); 2511baa6e33SBarry Smith else if (dim == 3) PetscCall(DMCreateColoring_DA_3d_MPIAIJ(da, ctype, coloring)); 2521baa6e33SBarry 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); 25347c6ae99SBarry Smith if (isBAIJ) { 25447c6ae99SBarry Smith dd->w = nc; 25547c6ae99SBarry Smith dd->xs = dd->xs * nc; 25647c6ae99SBarry Smith dd->xe = dd->xe * nc; 25747c6ae99SBarry Smith dd->Xs = dd->Xs * nc; 25847c6ae99SBarry Smith dd->Xe = dd->Xe * nc; 25947c6ae99SBarry Smith } 2603ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 26147c6ae99SBarry Smith } 26247c6ae99SBarry Smith 263d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA_2d_MPIAIJ(DM da, ISColoringType ctype, ISColoring *coloring) 264d71ae5a4SJacob Faibussowitsch { 26547c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, ii, gxs, gys, gnx, gny, m, n, M, N, dim, s, k, nc, col; 266dec0b466SHong Zhang PetscInt ncolors = 0; 26747c6ae99SBarry Smith MPI_Comm comm; 268bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 269aa219208SBarry Smith DMDAStencilType st; 27047c6ae99SBarry Smith ISColoringValue *colors; 27147c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 27247c6ae99SBarry Smith 27347c6ae99SBarry Smith PetscFunctionBegin; 27447c6ae99SBarry Smith /* 27547c6ae99SBarry Smith nc - number of components per grid point 27647c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 27747c6ae99SBarry Smith 27847c6ae99SBarry Smith */ 2799566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, &M, &N, NULL, &nc, &s, &bx, &by, NULL, &st)); 28047c6ae99SBarry Smith col = 2 * s + 1; 2819566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 2829566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 2839566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 28447c6ae99SBarry Smith 28547c6ae99SBarry Smith /* special case as taught to us by Paul Hovland */ 286aa219208SBarry Smith if (st == DMDA_STENCIL_STAR && s == 1) { 2879566063dSJacob Faibussowitsch PetscCall(DMCreateColoring_DA_2d_5pt_MPIAIJ(da, ctype, coloring)); 28847c6ae99SBarry Smith } else { 28947c6ae99SBarry Smith if (ctype == IS_COLORING_GLOBAL) { 29047c6ae99SBarry Smith if (!dd->localcoloring) { 2919566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * nx * ny, &colors)); 29247c6ae99SBarry Smith ii = 0; 29347c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 29447c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 295ad540459SPierre Jolivet for (k = 0; k < nc; k++) colors[ii++] = k + nc * ((i % col) + col * (j % col)); 29647c6ae99SBarry Smith } 29747c6ae99SBarry Smith } 29847c6ae99SBarry Smith ncolors = nc + nc * (col - 1 + col * (col - 1)); 2999566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * nx * ny, colors, PETSC_OWN_POINTER, &dd->localcoloring)); 30047c6ae99SBarry Smith } 30147c6ae99SBarry Smith *coloring = dd->localcoloring; 3025bdb020cSBarry Smith } else if (ctype == IS_COLORING_LOCAL) { 30347c6ae99SBarry Smith if (!dd->ghostedcoloring) { 3049566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * gnx * gny, &colors)); 30547c6ae99SBarry Smith ii = 0; 30647c6ae99SBarry Smith for (j = gys; j < gys + gny; j++) { 30747c6ae99SBarry Smith for (i = gxs; i < gxs + gnx; i++) { 30847c6ae99SBarry Smith for (k = 0; k < nc; k++) { 30947c6ae99SBarry Smith /* the complicated stuff is to handle periodic boundaries */ 31047c6ae99SBarry Smith colors[ii++] = k + nc * ((SetInRange(i, m) % col) + col * (SetInRange(j, n) % col)); 31147c6ae99SBarry Smith } 31247c6ae99SBarry Smith } 31347c6ae99SBarry Smith } 31447c6ae99SBarry Smith ncolors = nc + nc * (col - 1 + col * (col - 1)); 3159566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * gnx * gny, colors, PETSC_OWN_POINTER, &dd->ghostedcoloring)); 31647c6ae99SBarry Smith /* PetscIntView(ncolors,(PetscInt*)colors,0); */ 31747c6ae99SBarry Smith 3189566063dSJacob Faibussowitsch PetscCall(ISColoringSetType(dd->ghostedcoloring, IS_COLORING_LOCAL)); 31947c6ae99SBarry Smith } 32047c6ae99SBarry Smith *coloring = dd->ghostedcoloring; 32198921bdaSJacob Faibussowitsch } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_ARG_WRONG, "Unknown ISColoringType %d", (int)ctype); 32247c6ae99SBarry Smith } 3239566063dSJacob Faibussowitsch PetscCall(ISColoringReference(*coloring)); 3243ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 32547c6ae99SBarry Smith } 32647c6ae99SBarry Smith 327d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA_3d_MPIAIJ(DM da, ISColoringType ctype, ISColoring *coloring) 328d71ae5a4SJacob Faibussowitsch { 32947c6ae99SBarry 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; 33047c6ae99SBarry Smith PetscInt ncolors; 33147c6ae99SBarry Smith MPI_Comm comm; 332bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by, bz; 333aa219208SBarry Smith DMDAStencilType st; 33447c6ae99SBarry Smith ISColoringValue *colors; 33547c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 33647c6ae99SBarry Smith 33747c6ae99SBarry Smith PetscFunctionBegin; 33847c6ae99SBarry Smith /* 33947c6ae99SBarry Smith nc - number of components per grid point 34047c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 34147c6ae99SBarry Smith */ 3429566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &nc, &s, &bx, &by, &bz, &st)); 34347c6ae99SBarry Smith col = 2 * s + 1; 344494b1ccaSBarry Smith PetscCheck(bx != DM_BOUNDARY_PERIODIC || (m % col) == 0, PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "For coloring efficiency ensure number of grid points in X is divisible\n\ 345494b1ccaSBarry Smith by 2*stencil_width + 1\n"); 346494b1ccaSBarry Smith PetscCheck(by != DM_BOUNDARY_PERIODIC || (n % col) == 0, PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "For coloring efficiency ensure number of grid points in Y is divisible\n\ 347494b1ccaSBarry Smith by 2*stencil_width + 1\n"); 348494b1ccaSBarry Smith PetscCheck(bz != DM_BOUNDARY_PERIODIC || (p % col) == 0, PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "For coloring efficiency ensure number of grid points in Z is divisible\n\ 349494b1ccaSBarry Smith by 2*stencil_width + 1\n"); 350494b1ccaSBarry Smith 3519566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 3529566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 3539566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 35447c6ae99SBarry Smith 35547c6ae99SBarry Smith /* create the coloring */ 35647c6ae99SBarry Smith if (ctype == IS_COLORING_GLOBAL) { 35747c6ae99SBarry Smith if (!dd->localcoloring) { 3589566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * nx * ny * nz, &colors)); 35947c6ae99SBarry Smith ii = 0; 36047c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 36147c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 36247c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 363ad540459SPierre Jolivet for (l = 0; l < nc; l++) colors[ii++] = l + nc * ((i % col) + col * (j % col) + col * col * (k % col)); 36447c6ae99SBarry Smith } 36547c6ae99SBarry Smith } 36647c6ae99SBarry Smith } 36747c6ae99SBarry Smith ncolors = nc + nc * (col - 1 + col * (col - 1) + col * col * (col - 1)); 3689566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * nx * ny * nz, colors, PETSC_OWN_POINTER, &dd->localcoloring)); 36947c6ae99SBarry Smith } 37047c6ae99SBarry Smith *coloring = dd->localcoloring; 3715bdb020cSBarry Smith } else if (ctype == IS_COLORING_LOCAL) { 37247c6ae99SBarry Smith if (!dd->ghostedcoloring) { 3739566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * gnx * gny * gnz, &colors)); 37447c6ae99SBarry Smith ii = 0; 37547c6ae99SBarry Smith for (k = gzs; k < gzs + gnz; k++) { 37647c6ae99SBarry Smith for (j = gys; j < gys + gny; j++) { 37747c6ae99SBarry Smith for (i = gxs; i < gxs + gnx; i++) { 37847c6ae99SBarry Smith for (l = 0; l < nc; l++) { 37947c6ae99SBarry Smith /* the complicated stuff is to handle periodic boundaries */ 38047c6ae99SBarry Smith colors[ii++] = l + nc * ((SetInRange(i, m) % col) + col * (SetInRange(j, n) % col) + col * col * (SetInRange(k, p) % col)); 38147c6ae99SBarry Smith } 38247c6ae99SBarry Smith } 38347c6ae99SBarry Smith } 38447c6ae99SBarry Smith } 38547c6ae99SBarry Smith ncolors = nc + nc * (col - 1 + col * (col - 1) + col * col * (col - 1)); 3869566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * gnx * gny * gnz, colors, PETSC_OWN_POINTER, &dd->ghostedcoloring)); 3879566063dSJacob Faibussowitsch PetscCall(ISColoringSetType(dd->ghostedcoloring, IS_COLORING_LOCAL)); 38847c6ae99SBarry Smith } 38947c6ae99SBarry Smith *coloring = dd->ghostedcoloring; 39098921bdaSJacob Faibussowitsch } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_ARG_WRONG, "Unknown ISColoringType %d", (int)ctype); 3919566063dSJacob Faibussowitsch PetscCall(ISColoringReference(*coloring)); 3923ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 39347c6ae99SBarry Smith } 39447c6ae99SBarry Smith 395d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA_1d_MPIAIJ(DM da, ISColoringType ctype, ISColoring *coloring) 396d71ae5a4SJacob Faibussowitsch { 39747c6ae99SBarry Smith PetscInt xs, nx, i, i1, gxs, gnx, l, m, M, dim, s, nc, col; 39847c6ae99SBarry Smith PetscInt ncolors; 39947c6ae99SBarry Smith MPI_Comm comm; 400bff4a2f0SMatthew G. Knepley DMBoundaryType bx; 40147c6ae99SBarry Smith ISColoringValue *colors; 40247c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 40347c6ae99SBarry Smith 40447c6ae99SBarry Smith PetscFunctionBegin; 40547c6ae99SBarry Smith /* 40647c6ae99SBarry Smith nc - number of components per grid point 40747c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 40847c6ae99SBarry Smith */ 4099566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, NULL, NULL, &M, NULL, NULL, &nc, &s, &bx, NULL, NULL, NULL)); 41047c6ae99SBarry Smith col = 2 * s + 1; 4119566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &nx, NULL, NULL)); 4129566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, NULL, NULL, &gnx, NULL, NULL)); 4139566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 41447c6ae99SBarry Smith 41547c6ae99SBarry Smith /* create the coloring */ 41647c6ae99SBarry Smith if (ctype == IS_COLORING_GLOBAL) { 41747c6ae99SBarry Smith if (!dd->localcoloring) { 4189566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * nx, &colors)); 419ae4f298aSBarry Smith if (dd->ofillcols) { 420ae4f298aSBarry Smith PetscInt tc = 0; 421ae4f298aSBarry Smith for (i = 0; i < nc; i++) tc += (PetscInt)(dd->ofillcols[i] > 0); 422ae4f298aSBarry Smith i1 = 0; 423ae4f298aSBarry Smith for (i = xs; i < xs + nx; i++) { 424ae4f298aSBarry Smith for (l = 0; l < nc; l++) { 425ae4f298aSBarry Smith if (dd->ofillcols[l] && (i % col)) { 426ae4f298aSBarry Smith colors[i1++] = nc - 1 + tc * ((i % col) - 1) + dd->ofillcols[l]; 427ae4f298aSBarry Smith } else { 428ae4f298aSBarry Smith colors[i1++] = l; 429ae4f298aSBarry Smith } 430ae4f298aSBarry Smith } 431ae4f298aSBarry Smith } 432ae4f298aSBarry Smith ncolors = nc + 2 * s * tc; 433ae4f298aSBarry Smith } else { 43447c6ae99SBarry Smith i1 = 0; 43547c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 436ad540459SPierre Jolivet for (l = 0; l < nc; l++) colors[i1++] = l + nc * (i % col); 43747c6ae99SBarry Smith } 43847c6ae99SBarry Smith ncolors = nc + nc * (col - 1); 439ae4f298aSBarry Smith } 4409566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * nx, colors, PETSC_OWN_POINTER, &dd->localcoloring)); 44147c6ae99SBarry Smith } 44247c6ae99SBarry Smith *coloring = dd->localcoloring; 4435bdb020cSBarry Smith } else if (ctype == IS_COLORING_LOCAL) { 44447c6ae99SBarry Smith if (!dd->ghostedcoloring) { 4459566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * gnx, &colors)); 44647c6ae99SBarry Smith i1 = 0; 44747c6ae99SBarry Smith for (i = gxs; i < gxs + gnx; i++) { 44847c6ae99SBarry Smith for (l = 0; l < nc; l++) { 44947c6ae99SBarry Smith /* the complicated stuff is to handle periodic boundaries */ 45047c6ae99SBarry Smith colors[i1++] = l + nc * (SetInRange(i, m) % col); 45147c6ae99SBarry Smith } 45247c6ae99SBarry Smith } 45347c6ae99SBarry Smith ncolors = nc + nc * (col - 1); 4549566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * gnx, colors, PETSC_OWN_POINTER, &dd->ghostedcoloring)); 4559566063dSJacob Faibussowitsch PetscCall(ISColoringSetType(dd->ghostedcoloring, IS_COLORING_LOCAL)); 45647c6ae99SBarry Smith } 45747c6ae99SBarry Smith *coloring = dd->ghostedcoloring; 45898921bdaSJacob Faibussowitsch } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_ARG_WRONG, "Unknown ISColoringType %d", (int)ctype); 4599566063dSJacob Faibussowitsch PetscCall(ISColoringReference(*coloring)); 4603ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 46147c6ae99SBarry Smith } 46247c6ae99SBarry Smith 463d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA_2d_5pt_MPIAIJ(DM da, ISColoringType ctype, ISColoring *coloring) 464d71ae5a4SJacob Faibussowitsch { 46547c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, ii, gxs, gys, gnx, gny, m, n, dim, s, k, nc; 46647c6ae99SBarry Smith PetscInt ncolors; 46747c6ae99SBarry Smith MPI_Comm comm; 468bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 46947c6ae99SBarry Smith ISColoringValue *colors; 47047c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 47147c6ae99SBarry Smith 47247c6ae99SBarry Smith PetscFunctionBegin; 47347c6ae99SBarry Smith /* 47447c6ae99SBarry Smith nc - number of components per grid point 47547c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 47647c6ae99SBarry Smith */ 4779566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, NULL, NULL, NULL, &nc, &s, &bx, &by, NULL, NULL)); 4789566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 4799566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 4809566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 48147c6ae99SBarry Smith /* create the coloring */ 48247c6ae99SBarry Smith if (ctype == IS_COLORING_GLOBAL) { 48347c6ae99SBarry Smith if (!dd->localcoloring) { 4849566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * nx * ny, &colors)); 48547c6ae99SBarry Smith ii = 0; 48647c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 48747c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 488ad540459SPierre Jolivet for (k = 0; k < nc; k++) colors[ii++] = k + nc * ((3 * j + i) % 5); 48947c6ae99SBarry Smith } 49047c6ae99SBarry Smith } 49147c6ae99SBarry Smith ncolors = 5 * nc; 4929566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * nx * ny, colors, PETSC_OWN_POINTER, &dd->localcoloring)); 49347c6ae99SBarry Smith } 49447c6ae99SBarry Smith *coloring = dd->localcoloring; 4955bdb020cSBarry Smith } else if (ctype == IS_COLORING_LOCAL) { 49647c6ae99SBarry Smith if (!dd->ghostedcoloring) { 4979566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * gnx * gny, &colors)); 49847c6ae99SBarry Smith ii = 0; 49947c6ae99SBarry Smith for (j = gys; j < gys + gny; j++) { 50047c6ae99SBarry Smith for (i = gxs; i < gxs + gnx; i++) { 501ad540459SPierre Jolivet for (k = 0; k < nc; k++) colors[ii++] = k + nc * ((3 * SetInRange(j, n) + SetInRange(i, m)) % 5); 50247c6ae99SBarry Smith } 50347c6ae99SBarry Smith } 50447c6ae99SBarry Smith ncolors = 5 * nc; 5059566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * gnx * gny, colors, PETSC_OWN_POINTER, &dd->ghostedcoloring)); 5069566063dSJacob Faibussowitsch PetscCall(ISColoringSetType(dd->ghostedcoloring, IS_COLORING_LOCAL)); 50747c6ae99SBarry Smith } 50847c6ae99SBarry Smith *coloring = dd->ghostedcoloring; 50998921bdaSJacob Faibussowitsch } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_ARG_WRONG, "Unknown ISColoringType %d", (int)ctype); 5103ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 51147c6ae99SBarry Smith } 51247c6ae99SBarry Smith 51347c6ae99SBarry Smith /* =========================================================================== */ 514e432b41dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_1d_MPIAIJ(DM, Mat); 515ce308e1dSBarry Smith extern PetscErrorCode DMCreateMatrix_DA_1d_MPIAIJ_Fill(DM, Mat); 516e432b41dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_1d_SeqAIJ_NoPreallocation(DM, Mat); 517e432b41dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_2d_MPIAIJ(DM, Mat); 518950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_2d_MPIAIJ_Fill(DM, Mat); 519e432b41dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_3d_MPIAIJ(DM, Mat); 520950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_3d_MPIAIJ_Fill(DM, Mat); 521950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_2d_MPIBAIJ(DM, Mat); 522950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_3d_MPIBAIJ(DM, Mat); 523950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_2d_MPISBAIJ(DM, Mat); 524950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_3d_MPISBAIJ(DM, Mat); 525d4002b98SHong Zhang extern PetscErrorCode DMCreateMatrix_DA_2d_MPISELL(DM, Mat); 526d4002b98SHong Zhang extern PetscErrorCode DMCreateMatrix_DA_3d_MPISELL(DM, Mat); 527e584696dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_IS(DM, Mat); 52847c6ae99SBarry Smith 5298bbdbebaSMatthew G Knepley /*@C 530dce8aebaSBarry Smith MatSetupDM - Sets the `DMDA` that is to be used by the HYPRE_StructMatrix PETSc matrix 53147c6ae99SBarry Smith 532*20f4b53cSBarry Smith Logically Collective 53347c6ae99SBarry Smith 53447c6ae99SBarry Smith Input Parameters: 53547c6ae99SBarry Smith + mat - the matrix 53647c6ae99SBarry Smith - da - the da 53747c6ae99SBarry Smith 53847c6ae99SBarry Smith Level: intermediate 53947c6ae99SBarry Smith 540*20f4b53cSBarry Smith .seealso: `DMDA`, `Mat`, `MatSetUp()` 54147c6ae99SBarry Smith @*/ 542d71ae5a4SJacob Faibussowitsch PetscErrorCode MatSetupDM(Mat mat, DM da) 543d71ae5a4SJacob Faibussowitsch { 54447c6ae99SBarry Smith PetscFunctionBegin; 54547c6ae99SBarry Smith PetscValidHeaderSpecific(mat, MAT_CLASSID, 1); 546064a246eSJacob Faibussowitsch PetscValidHeaderSpecificType(da, DM_CLASSID, 2, DMDA); 547cac4c232SBarry Smith PetscTryMethod(mat, "MatSetupDM_C", (Mat, DM), (mat, da)); 5483ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 54947c6ae99SBarry Smith } 55047c6ae99SBarry Smith 551d71ae5a4SJacob Faibussowitsch PetscErrorCode MatView_MPI_DA(Mat A, PetscViewer viewer) 552d71ae5a4SJacob Faibussowitsch { 5539a42bb27SBarry Smith DM da; 55447c6ae99SBarry Smith const char *prefix; 55547c6ae99SBarry Smith Mat Anatural; 55647c6ae99SBarry Smith AO ao; 55747c6ae99SBarry Smith PetscInt rstart, rend, *petsc, i; 55847c6ae99SBarry Smith IS is; 55947c6ae99SBarry Smith MPI_Comm comm; 56074388724SJed Brown PetscViewerFormat format; 56147c6ae99SBarry Smith 56247c6ae99SBarry Smith PetscFunctionBegin; 56374388724SJed Brown /* Check whether we are just printing info, in which case MatView() already viewed everything we wanted to view */ 5649566063dSJacob Faibussowitsch PetscCall(PetscViewerGetFormat(viewer, &format)); 5653ba16761SJacob Faibussowitsch if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) PetscFunctionReturn(PETSC_SUCCESS); 56674388724SJed Brown 5679566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)A, &comm)); 5689566063dSJacob Faibussowitsch PetscCall(MatGetDM(A, &da)); 5697a8be351SBarry Smith PetscCheck(da, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Matrix not generated from a DMDA"); 57047c6ae99SBarry Smith 5719566063dSJacob Faibussowitsch PetscCall(DMDAGetAO(da, &ao)); 5729566063dSJacob Faibussowitsch PetscCall(MatGetOwnershipRange(A, &rstart, &rend)); 5739566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(rend - rstart, &petsc)); 57447c6ae99SBarry Smith for (i = rstart; i < rend; i++) petsc[i - rstart] = i; 5759566063dSJacob Faibussowitsch PetscCall(AOApplicationToPetsc(ao, rend - rstart, petsc)); 5769566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(comm, rend - rstart, petsc, PETSC_OWN_POINTER, &is)); 57747c6ae99SBarry Smith 57847c6ae99SBarry Smith /* call viewer on natural ordering */ 5799566063dSJacob Faibussowitsch PetscCall(MatCreateSubMatrix(A, is, is, MAT_INITIAL_MATRIX, &Anatural)); 5809566063dSJacob Faibussowitsch PetscCall(ISDestroy(&is)); 5819566063dSJacob Faibussowitsch PetscCall(PetscObjectGetOptionsPrefix((PetscObject)A, &prefix)); 5829566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptionsPrefix((PetscObject)Anatural, prefix)); 5839566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)Anatural, ((PetscObject)A)->name)); 584f0ed2f47SStefano Zampini ((PetscObject)Anatural)->donotPetscObjectPrintClassNamePrefixType = PETSC_TRUE; 5859566063dSJacob Faibussowitsch PetscCall(MatView(Anatural, viewer)); 586f0ed2f47SStefano Zampini ((PetscObject)Anatural)->donotPetscObjectPrintClassNamePrefixType = PETSC_FALSE; 5879566063dSJacob Faibussowitsch PetscCall(MatDestroy(&Anatural)); 5883ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 58947c6ae99SBarry Smith } 59047c6ae99SBarry Smith 591d71ae5a4SJacob Faibussowitsch PetscErrorCode MatLoad_MPI_DA(Mat A, PetscViewer viewer) 592d71ae5a4SJacob Faibussowitsch { 5939a42bb27SBarry Smith DM da; 59447c6ae99SBarry Smith Mat Anatural, Aapp; 59547c6ae99SBarry Smith AO ao; 596539c167fSBarry Smith PetscInt rstart, rend, *app, i, m, n, M, N; 59747c6ae99SBarry Smith IS is; 59847c6ae99SBarry Smith MPI_Comm comm; 59947c6ae99SBarry Smith 60047c6ae99SBarry Smith PetscFunctionBegin; 6019566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)A, &comm)); 6029566063dSJacob Faibussowitsch PetscCall(MatGetDM(A, &da)); 6037a8be351SBarry Smith PetscCheck(da, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Matrix not generated from a DMDA"); 60447c6ae99SBarry Smith 60547c6ae99SBarry Smith /* Load the matrix in natural ordering */ 6069566063dSJacob Faibussowitsch PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &Anatural)); 6079566063dSJacob Faibussowitsch PetscCall(MatSetType(Anatural, ((PetscObject)A)->type_name)); 6089566063dSJacob Faibussowitsch PetscCall(MatGetSize(A, &M, &N)); 6099566063dSJacob Faibussowitsch PetscCall(MatGetLocalSize(A, &m, &n)); 6109566063dSJacob Faibussowitsch PetscCall(MatSetSizes(Anatural, m, n, M, N)); 6119566063dSJacob Faibussowitsch PetscCall(MatLoad(Anatural, viewer)); 61247c6ae99SBarry Smith 61347c6ae99SBarry Smith /* Map natural ordering to application ordering and create IS */ 6149566063dSJacob Faibussowitsch PetscCall(DMDAGetAO(da, &ao)); 6159566063dSJacob Faibussowitsch PetscCall(MatGetOwnershipRange(Anatural, &rstart, &rend)); 6169566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(rend - rstart, &app)); 61747c6ae99SBarry Smith for (i = rstart; i < rend; i++) app[i - rstart] = i; 6189566063dSJacob Faibussowitsch PetscCall(AOPetscToApplication(ao, rend - rstart, app)); 6199566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(comm, rend - rstart, app, PETSC_OWN_POINTER, &is)); 62047c6ae99SBarry Smith 62147c6ae99SBarry Smith /* Do permutation and replace header */ 6229566063dSJacob Faibussowitsch PetscCall(MatCreateSubMatrix(Anatural, is, is, MAT_INITIAL_MATRIX, &Aapp)); 6239566063dSJacob Faibussowitsch PetscCall(MatHeaderReplace(A, &Aapp)); 6249566063dSJacob Faibussowitsch PetscCall(ISDestroy(&is)); 6259566063dSJacob Faibussowitsch PetscCall(MatDestroy(&Anatural)); 6263ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 62747c6ae99SBarry Smith } 62847c6ae99SBarry Smith 629d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA(DM da, Mat *J) 630d71ae5a4SJacob Faibussowitsch { 63147c6ae99SBarry Smith PetscInt dim, dof, nx, ny, nz, dims[3], starts[3], M, N, P; 63247c6ae99SBarry Smith Mat A; 63347c6ae99SBarry Smith MPI_Comm comm; 63419fd82e9SBarry Smith MatType Atype; 635b412c318SBarry Smith MatType mtype; 63647c6ae99SBarry Smith PetscMPIInt size; 63747c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 638ade515a3SBarry Smith void (*aij)(void) = NULL, (*baij)(void) = NULL, (*sbaij)(void) = NULL, (*sell)(void) = NULL, (*is)(void) = NULL; 63947c6ae99SBarry Smith 64047c6ae99SBarry Smith PetscFunctionBegin; 6419566063dSJacob Faibussowitsch PetscCall(MatInitializePackage()); 642b412c318SBarry Smith mtype = da->mattype; 64347c6ae99SBarry Smith 64447c6ae99SBarry Smith /* 64547c6ae99SBarry Smith m 64647c6ae99SBarry Smith ------------------------------------------------------ 64747c6ae99SBarry Smith | | 64847c6ae99SBarry Smith | | 64947c6ae99SBarry Smith | ---------------------- | 65047c6ae99SBarry Smith | | | | 65147c6ae99SBarry Smith n | ny | | | 65247c6ae99SBarry Smith | | | | 65347c6ae99SBarry Smith | .--------------------- | 65447c6ae99SBarry Smith | (xs,ys) nx | 65547c6ae99SBarry Smith | . | 65647c6ae99SBarry Smith | (gxs,gys) | 65747c6ae99SBarry Smith | | 65847c6ae99SBarry Smith ----------------------------------------------------- 65947c6ae99SBarry Smith */ 66047c6ae99SBarry Smith 66147c6ae99SBarry Smith /* 66247c6ae99SBarry Smith nc - number of components per grid point 66347c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 66447c6ae99SBarry Smith */ 665e30e807fSPeter Brune M = dd->M; 666e30e807fSPeter Brune N = dd->N; 667e30e807fSPeter Brune P = dd->P; 668c73cfb54SMatthew G. Knepley dim = da->dim; 669e30e807fSPeter Brune dof = dd->w; 6709566063dSJacob Faibussowitsch /* PetscCall(DMDAGetInfo(da,&dim,&M,&N,&P,NULL,NULL,NULL,&dof,NULL,NULL,NULL,NULL,NULL)); */ 6719566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, NULL, NULL, NULL, &nx, &ny, &nz)); 6729566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 6739566063dSJacob Faibussowitsch PetscCall(MatCreate(comm, &A)); 6749566063dSJacob Faibussowitsch PetscCall(MatSetSizes(A, dof * nx * ny * nz, dof * nx * ny * nz, dof * M * N * P, dof * M * N * P)); 6759566063dSJacob Faibussowitsch PetscCall(MatSetType(A, mtype)); 6769566063dSJacob Faibussowitsch PetscCall(MatSetFromOptions(A)); 67774427ab1SRichard Tran Mills if (dof * nx * ny * nz < da->bind_below) { 6789566063dSJacob Faibussowitsch PetscCall(MatSetBindingPropagates(A, PETSC_TRUE)); 6799566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(A, PETSC_TRUE)); 68074427ab1SRichard Tran Mills } 6819566063dSJacob Faibussowitsch PetscCall(MatSetDM(A, da)); 6821baa6e33SBarry Smith if (da->structure_only) PetscCall(MatSetOption(A, MAT_STRUCTURE_ONLY, PETSC_TRUE)); 6839566063dSJacob Faibussowitsch PetscCall(MatGetType(A, &Atype)); 68447c6ae99SBarry Smith /* 685aa219208SBarry Smith We do not provide a getmatrix function in the DMDA operations because 686aa219208SBarry Smith the basic DMDA does not know about matrices. We think of DMDA as being more 68747c6ae99SBarry Smith more low-level than matrices. This is kind of cheating but, cause sometimes 688aa219208SBarry Smith we think of DMDA has higher level than matrices. 68947c6ae99SBarry Smith 69047c6ae99SBarry Smith We could switch based on Atype (or mtype), but we do not since the 691844bd0d7SStefano Zampini specialized setting routines depend only on the particular preallocation 69247c6ae99SBarry Smith details of the matrix, not the type itself. 69347c6ae99SBarry Smith */ 6949566063dSJacob Faibussowitsch PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatMPIAIJSetPreallocation_C", &aij)); 69548a46eb9SPierre Jolivet if (!aij) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatSeqAIJSetPreallocation_C", &aij)); 69647c6ae99SBarry Smith if (!aij) { 6979566063dSJacob Faibussowitsch PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatMPIBAIJSetPreallocation_C", &baij)); 69848a46eb9SPierre Jolivet if (!baij) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatSeqBAIJSetPreallocation_C", &baij)); 69947c6ae99SBarry Smith if (!baij) { 7009566063dSJacob Faibussowitsch PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatMPISBAIJSetPreallocation_C", &sbaij)); 70148a46eb9SPierre Jolivet if (!sbaij) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatSeqSBAIJSetPreallocation_C", &sbaij)); 7025e26d47bSHong Zhang if (!sbaij) { 7039566063dSJacob Faibussowitsch PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatMPISELLSetPreallocation_C", &sell)); 70448a46eb9SPierre Jolivet if (!sell) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatSeqSELLSetPreallocation_C", &sell)); 7055e26d47bSHong Zhang } 70648a46eb9SPierre Jolivet if (!sell) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatISSetPreallocation_C", &is)); 70747c6ae99SBarry Smith } 70847c6ae99SBarry Smith } 70947c6ae99SBarry Smith if (aij) { 71047c6ae99SBarry Smith if (dim == 1) { 711ce308e1dSBarry Smith if (dd->ofill) { 7129566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_1d_MPIAIJ_Fill(da, A)); 713ce308e1dSBarry Smith } else { 71419b08ed1SBarry Smith DMBoundaryType bx; 71519b08ed1SBarry Smith PetscMPIInt size; 7169566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &bx, NULL, NULL, NULL)); 7179566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)da), &size)); 71819b08ed1SBarry Smith if (size == 1 && bx == DM_BOUNDARY_NONE) { 7199566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_1d_SeqAIJ_NoPreallocation(da, A)); 72019b08ed1SBarry Smith } else { 7219566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_1d_MPIAIJ(da, A)); 722ce308e1dSBarry Smith } 72319b08ed1SBarry Smith } 72447c6ae99SBarry Smith } else if (dim == 2) { 72547c6ae99SBarry Smith if (dd->ofill) { 7269566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_2d_MPIAIJ_Fill(da, A)); 72747c6ae99SBarry Smith } else { 7289566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_2d_MPIAIJ(da, A)); 72947c6ae99SBarry Smith } 73047c6ae99SBarry Smith } else if (dim == 3) { 73147c6ae99SBarry Smith if (dd->ofill) { 7329566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_3d_MPIAIJ_Fill(da, A)); 73347c6ae99SBarry Smith } else { 7349566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_3d_MPIAIJ(da, A)); 73547c6ae99SBarry Smith } 73647c6ae99SBarry Smith } 73747c6ae99SBarry Smith } else if (baij) { 73847c6ae99SBarry Smith if (dim == 2) { 7399566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_2d_MPIBAIJ(da, A)); 74047c6ae99SBarry Smith } else if (dim == 3) { 7419566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_3d_MPIBAIJ(da, A)); 74263a3b9bcSJacob Faibussowitsch } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "Not implemented for %" PetscInt_FMT " dimension and Matrix Type: %s in %" PetscInt_FMT " dimension! Send mail to petsc-maint@mcs.anl.gov for code", dim, Atype, dim); 74347c6ae99SBarry Smith } else if (sbaij) { 74447c6ae99SBarry Smith if (dim == 2) { 7459566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_2d_MPISBAIJ(da, A)); 74647c6ae99SBarry Smith } else if (dim == 3) { 7479566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_3d_MPISBAIJ(da, A)); 74863a3b9bcSJacob Faibussowitsch } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "Not implemented for %" PetscInt_FMT " dimension and Matrix Type: %s in %" PetscInt_FMT " dimension! Send mail to petsc-maint@mcs.anl.gov for code", dim, Atype, dim); 749d4002b98SHong Zhang } else if (sell) { 7505e26d47bSHong Zhang if (dim == 2) { 7519566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_2d_MPISELL(da, A)); 752711261dbSHong Zhang } else if (dim == 3) { 7539566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_3d_MPISELL(da, A)); 75463a3b9bcSJacob Faibussowitsch } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "Not implemented for %" PetscInt_FMT " dimension and Matrix Type: %s in %" PetscInt_FMT " dimension! Send mail to petsc-maint@mcs.anl.gov for code", dim, Atype, dim); 755e584696dSStefano Zampini } else if (is) { 7569566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_IS(da, A)); 757869776cdSLisandro Dalcin } else { 75845b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 759e584696dSStefano Zampini 7609566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(A, dof)); 7619566063dSJacob Faibussowitsch PetscCall(MatSetUp(A)); 7629566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 7639566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(A, ltog, ltog)); 76447c6ae99SBarry Smith } 7659566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &starts[0], &starts[1], &starts[2], &dims[0], &dims[1], &dims[2])); 7669566063dSJacob Faibussowitsch PetscCall(MatSetStencil(A, dim, dims, starts, dof)); 7679566063dSJacob Faibussowitsch PetscCall(MatSetDM(A, da)); 7689566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(comm, &size)); 76947c6ae99SBarry Smith if (size > 1) { 77047c6ae99SBarry Smith /* change viewer to display matrix in natural ordering */ 7719566063dSJacob Faibussowitsch PetscCall(MatSetOperation(A, MATOP_VIEW, (void (*)(void))MatView_MPI_DA)); 7729566063dSJacob Faibussowitsch PetscCall(MatSetOperation(A, MATOP_LOAD, (void (*)(void))MatLoad_MPI_DA)); 77347c6ae99SBarry Smith } 77447c6ae99SBarry Smith *J = A; 7753ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 77647c6ae99SBarry Smith } 77747c6ae99SBarry Smith 77847c6ae99SBarry Smith /* ---------------------------------------------------------------------------------*/ 779844bd0d7SStefano Zampini PETSC_EXTERN PetscErrorCode MatISSetPreallocation_IS(Mat, PetscInt, const PetscInt[], PetscInt, const PetscInt[]); 780844bd0d7SStefano Zampini 781d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_IS(DM dm, Mat J) 782d71ae5a4SJacob Faibussowitsch { 783e584696dSStefano Zampini DM_DA *da = (DM_DA *)dm->data; 784e432b41dSStefano Zampini Mat lJ, P; 785e584696dSStefano Zampini ISLocalToGlobalMapping ltog; 786e432b41dSStefano Zampini IS is; 787e432b41dSStefano Zampini PetscBT bt; 78805339c03SStefano Zampini const PetscInt *e_loc, *idx; 789e432b41dSStefano Zampini PetscInt i, nel, nen, nv, dof, *gidx, n, N; 790e584696dSStefano Zampini 791e584696dSStefano Zampini /* The l2g map of DMDA has all ghosted nodes, and e_loc is a subset of all the local nodes (including the ghosted) 792e432b41dSStefano Zampini We need to filter out the local indices that are not represented through the DMDAGetElements decomposition */ 793e584696dSStefano Zampini PetscFunctionBegin; 794e584696dSStefano Zampini dof = da->w; 7959566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, dof)); 7969566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(dm, <og)); 797e432b41dSStefano Zampini 798e432b41dSStefano Zampini /* flag local elements indices in local DMDA numbering */ 7999566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingGetSize(ltog, &nv)); 8009566063dSJacob Faibussowitsch PetscCall(PetscBTCreate(nv / dof, &bt)); 8019566063dSJacob Faibussowitsch PetscCall(DMDAGetElements(dm, &nel, &nen, &e_loc)); /* this will throw an error if the stencil type is not DMDA_STENCIL_BOX */ 8029566063dSJacob Faibussowitsch for (i = 0; i < nel * nen; i++) PetscCall(PetscBTSet(bt, e_loc[i])); 803e432b41dSStefano Zampini 804e432b41dSStefano Zampini /* filter out (set to -1) the global indices not used by the local elements */ 8059566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nv / dof, &gidx)); 8069566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingGetBlockIndices(ltog, &idx)); 8079566063dSJacob Faibussowitsch PetscCall(PetscArraycpy(gidx, idx, nv / dof)); 8089566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingRestoreBlockIndices(ltog, &idx)); 8099371c9d4SSatish Balay for (i = 0; i < nv / dof; i++) 8109371c9d4SSatish Balay if (!PetscBTLookup(bt, i)) gidx[i] = -1; 8119566063dSJacob Faibussowitsch PetscCall(PetscBTDestroy(&bt)); 8129566063dSJacob Faibussowitsch PetscCall(ISCreateBlock(PetscObjectComm((PetscObject)dm), dof, nv / dof, gidx, PETSC_OWN_POINTER, &is)); 8139566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingCreateIS(is, <og)); 8149566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 8159566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingDestroy(<og)); 8169566063dSJacob Faibussowitsch PetscCall(ISDestroy(&is)); 81705339c03SStefano Zampini 818e432b41dSStefano Zampini /* Preallocation */ 819e306f867SJed Brown if (dm->prealloc_skip) { 8209566063dSJacob Faibussowitsch PetscCall(MatSetUp(J)); 821e306f867SJed Brown } else { 8229566063dSJacob Faibussowitsch PetscCall(MatISGetLocalMat(J, &lJ)); 8239566063dSJacob Faibussowitsch PetscCall(MatGetLocalToGlobalMapping(lJ, <og, NULL)); 8249566063dSJacob Faibussowitsch PetscCall(MatCreate(PetscObjectComm((PetscObject)lJ), &P)); 8259566063dSJacob Faibussowitsch PetscCall(MatSetType(P, MATPREALLOCATOR)); 8269566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(P, ltog, ltog)); 8279566063dSJacob Faibussowitsch PetscCall(MatGetSize(lJ, &N, NULL)); 8289566063dSJacob Faibussowitsch PetscCall(MatGetLocalSize(lJ, &n, NULL)); 8299566063dSJacob Faibussowitsch PetscCall(MatSetSizes(P, n, n, N, N)); 8309566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(P, dof)); 8319566063dSJacob Faibussowitsch PetscCall(MatSetUp(P)); 83248a46eb9SPierre Jolivet for (i = 0; i < nel; i++) PetscCall(MatSetValuesBlockedLocal(P, nen, e_loc + i * nen, nen, e_loc + i * nen, NULL, INSERT_VALUES)); 8339566063dSJacob Faibussowitsch PetscCall(MatPreallocatorPreallocate(P, (PetscBool)!da->prealloc_only, lJ)); 8349566063dSJacob Faibussowitsch PetscCall(MatISRestoreLocalMat(J, &lJ)); 8359566063dSJacob Faibussowitsch PetscCall(DMDARestoreElements(dm, &nel, &nen, &e_loc)); 8369566063dSJacob Faibussowitsch PetscCall(MatDestroy(&P)); 837e432b41dSStefano Zampini 8389566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 8399566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 840e306f867SJed Brown } 8413ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 842e584696dSStefano Zampini } 843e584696dSStefano Zampini 844d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPISELL(DM da, Mat J) 845d71ae5a4SJacob Faibussowitsch { 8465e26d47bSHong 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; 8475e26d47bSHong Zhang PetscInt lstart, lend, pstart, pend, *dnz, *onz; 8485e26d47bSHong Zhang MPI_Comm comm; 8495e26d47bSHong Zhang PetscScalar *values; 8505e26d47bSHong Zhang DMBoundaryType bx, by; 8515e26d47bSHong Zhang ISLocalToGlobalMapping ltog; 8525e26d47bSHong Zhang DMDAStencilType st; 8535e26d47bSHong Zhang 8545e26d47bSHong Zhang PetscFunctionBegin; 8555e26d47bSHong Zhang /* 8565e26d47bSHong Zhang nc - number of components per grid point 8575e26d47bSHong Zhang col - number of colors needed in one direction for single component problem 8585e26d47bSHong Zhang */ 8599566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, NULL, NULL, NULL, &nc, &s, &bx, &by, NULL, &st)); 8605e26d47bSHong Zhang col = 2 * s + 1; 8619566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 8629566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 8639566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 8645e26d47bSHong Zhang 8659566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * col * nc * nc, &cols)); 8669566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 8675e26d47bSHong Zhang 8689566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 8695e26d47bSHong Zhang /* determine the matrix preallocation information */ 870d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny, nc * nx * ny, dnz, onz); 8715e26d47bSHong Zhang for (i = xs; i < xs + nx; i++) { 8725e26d47bSHong Zhang pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 8735e26d47bSHong Zhang pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 8745e26d47bSHong Zhang 8755e26d47bSHong Zhang for (j = ys; j < ys + ny; j++) { 8765e26d47bSHong Zhang slot = i - gxs + gnx * (j - gys); 8775e26d47bSHong Zhang 8785e26d47bSHong Zhang lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 8795e26d47bSHong Zhang lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 8805e26d47bSHong Zhang 8815e26d47bSHong Zhang cnt = 0; 8825e26d47bSHong Zhang for (k = 0; k < nc; k++) { 8835e26d47bSHong Zhang for (l = lstart; l < lend + 1; l++) { 8845e26d47bSHong Zhang for (p = pstart; p < pend + 1; p++) { 8855e26d47bSHong Zhang if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star have either l = 0 or p = 0 */ 8865e26d47bSHong Zhang cols[cnt++] = k + nc * (slot + gnx * l + p); 8875e26d47bSHong Zhang } 8885e26d47bSHong Zhang } 8895e26d47bSHong Zhang } 8905e26d47bSHong Zhang rows[k] = k + nc * (slot); 8915e26d47bSHong Zhang } 8929566063dSJacob Faibussowitsch PetscCall(MatPreallocateSetLocal(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 8935e26d47bSHong Zhang } 8945e26d47bSHong Zhang } 8959566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 8969566063dSJacob Faibussowitsch PetscCall(MatSeqSELLSetPreallocation(J, 0, dnz)); 8979566063dSJacob Faibussowitsch PetscCall(MatMPISELLSetPreallocation(J, 0, dnz, 0, onz)); 898d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 8995e26d47bSHong Zhang 9009566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 9015e26d47bSHong Zhang 9025e26d47bSHong Zhang /* 9035e26d47bSHong Zhang For each node in the grid: we get the neighbors in the local (on processor ordering 9045e26d47bSHong Zhang that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 9055e26d47bSHong Zhang PETSc ordering. 9065e26d47bSHong Zhang */ 9075e26d47bSHong Zhang if (!da->prealloc_only) { 9089566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * nc * nc, &values)); 9095e26d47bSHong Zhang for (i = xs; i < xs + nx; i++) { 9105e26d47bSHong Zhang pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 9115e26d47bSHong Zhang pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 9125e26d47bSHong Zhang 9135e26d47bSHong Zhang for (j = ys; j < ys + ny; j++) { 9145e26d47bSHong Zhang slot = i - gxs + gnx * (j - gys); 9155e26d47bSHong Zhang 9165e26d47bSHong Zhang lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 9175e26d47bSHong Zhang lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 9185e26d47bSHong Zhang 9195e26d47bSHong Zhang cnt = 0; 9205e26d47bSHong Zhang for (k = 0; k < nc; k++) { 9215e26d47bSHong Zhang for (l = lstart; l < lend + 1; l++) { 9225e26d47bSHong Zhang for (p = pstart; p < pend + 1; p++) { 9235e26d47bSHong Zhang if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star have either l = 0 or p = 0 */ 9245e26d47bSHong Zhang cols[cnt++] = k + nc * (slot + gnx * l + p); 9255e26d47bSHong Zhang } 9265e26d47bSHong Zhang } 9275e26d47bSHong Zhang } 9285e26d47bSHong Zhang rows[k] = k + nc * (slot); 9295e26d47bSHong Zhang } 9309566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, values, INSERT_VALUES)); 9315e26d47bSHong Zhang } 9325e26d47bSHong Zhang } 9339566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 934e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 9359566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 9369566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 9379566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 9389566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 9399566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 9405e26d47bSHong Zhang } 9419566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 9423ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 9435e26d47bSHong Zhang } 9445e26d47bSHong Zhang 945d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPISELL(DM da, Mat J) 946d71ae5a4SJacob Faibussowitsch { 947711261dbSHong Zhang PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 948711261dbSHong Zhang PetscInt m, n, dim, s, *cols = NULL, k, nc, *rows = NULL, col, cnt, l, p, *dnz = NULL, *onz = NULL; 949711261dbSHong Zhang PetscInt istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk, M, N, P; 950711261dbSHong Zhang MPI_Comm comm; 951711261dbSHong Zhang PetscScalar *values; 952711261dbSHong Zhang DMBoundaryType bx, by, bz; 953711261dbSHong Zhang ISLocalToGlobalMapping ltog; 954711261dbSHong Zhang DMDAStencilType st; 955711261dbSHong Zhang 956711261dbSHong Zhang PetscFunctionBegin; 957711261dbSHong Zhang /* 958711261dbSHong Zhang nc - number of components per grid point 959711261dbSHong Zhang col - number of colors needed in one direction for single component problem 960711261dbSHong Zhang */ 9619566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &nc, &s, &bx, &by, &bz, &st)); 962711261dbSHong Zhang col = 2 * s + 1; 9639566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 9649566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 9659566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 966711261dbSHong Zhang 9679566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * col * col * nc * nc, &cols)); 9689566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 969711261dbSHong Zhang 9709566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 971711261dbSHong Zhang /* determine the matrix preallocation information */ 972d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny * nz, nc * nx * ny * nz, dnz, onz); 973711261dbSHong Zhang for (i = xs; i < xs + nx; i++) { 974711261dbSHong Zhang istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 975711261dbSHong Zhang iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 976711261dbSHong Zhang for (j = ys; j < ys + ny; j++) { 977711261dbSHong Zhang jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 978711261dbSHong Zhang jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 979711261dbSHong Zhang for (k = zs; k < zs + nz; k++) { 980711261dbSHong Zhang kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 981711261dbSHong Zhang kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 982711261dbSHong Zhang 983711261dbSHong Zhang slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 984711261dbSHong Zhang 985711261dbSHong Zhang cnt = 0; 986711261dbSHong Zhang for (l = 0; l < nc; l++) { 987711261dbSHong Zhang for (ii = istart; ii < iend + 1; ii++) { 988711261dbSHong Zhang for (jj = jstart; jj < jend + 1; jj++) { 989711261dbSHong Zhang for (kk = kstart; kk < kend + 1; kk++) { 990711261dbSHong Zhang if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 991711261dbSHong Zhang cols[cnt++] = l + nc * (slot + ii + gnx * jj + gnx * gny * kk); 992711261dbSHong Zhang } 993711261dbSHong Zhang } 994711261dbSHong Zhang } 995711261dbSHong Zhang } 996711261dbSHong Zhang rows[l] = l + nc * (slot); 997711261dbSHong Zhang } 9989566063dSJacob Faibussowitsch PetscCall(MatPreallocateSetLocal(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 999711261dbSHong Zhang } 1000711261dbSHong Zhang } 1001711261dbSHong Zhang } 10029566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 10039566063dSJacob Faibussowitsch PetscCall(MatSeqSELLSetPreallocation(J, 0, dnz)); 10049566063dSJacob Faibussowitsch PetscCall(MatMPISELLSetPreallocation(J, 0, dnz, 0, onz)); 1005d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 10069566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 1007711261dbSHong Zhang 1008711261dbSHong Zhang /* 1009711261dbSHong Zhang For each node in the grid: we get the neighbors in the local (on processor ordering 1010711261dbSHong Zhang that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 1011711261dbSHong Zhang PETSc ordering. 1012711261dbSHong Zhang */ 1013711261dbSHong Zhang if (!da->prealloc_only) { 10149566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * col * nc * nc * nc, &values)); 1015711261dbSHong Zhang for (i = xs; i < xs + nx; i++) { 1016711261dbSHong Zhang istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1017711261dbSHong Zhang iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 1018711261dbSHong Zhang for (j = ys; j < ys + ny; j++) { 1019711261dbSHong Zhang jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1020711261dbSHong Zhang jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 1021711261dbSHong Zhang for (k = zs; k < zs + nz; k++) { 1022711261dbSHong Zhang kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 1023711261dbSHong Zhang kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 1024711261dbSHong Zhang 1025711261dbSHong Zhang slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 1026711261dbSHong Zhang 1027711261dbSHong Zhang cnt = 0; 1028711261dbSHong Zhang for (l = 0; l < nc; l++) { 1029711261dbSHong Zhang for (ii = istart; ii < iend + 1; ii++) { 1030711261dbSHong Zhang for (jj = jstart; jj < jend + 1; jj++) { 1031711261dbSHong Zhang for (kk = kstart; kk < kend + 1; kk++) { 1032711261dbSHong Zhang if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 1033711261dbSHong Zhang cols[cnt++] = l + nc * (slot + ii + gnx * jj + gnx * gny * kk); 1034711261dbSHong Zhang } 1035711261dbSHong Zhang } 1036711261dbSHong Zhang } 1037711261dbSHong Zhang } 1038711261dbSHong Zhang rows[l] = l + nc * (slot); 1039711261dbSHong Zhang } 10409566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, values, INSERT_VALUES)); 1041711261dbSHong Zhang } 1042711261dbSHong Zhang } 1043711261dbSHong Zhang } 10449566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 1045e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 10469566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 10479566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 10489566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 10499566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 10509566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 1051711261dbSHong Zhang } 10529566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 10533ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1054711261dbSHong Zhang } 1055711261dbSHong Zhang 1056d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPIAIJ(DM da, Mat J) 1057d71ae5a4SJacob Faibussowitsch { 1058c1154cd5SBarry 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; 105947c6ae99SBarry Smith PetscInt lstart, lend, pstart, pend, *dnz, *onz; 106047c6ae99SBarry Smith MPI_Comm comm; 1061bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 1062844bd0d7SStefano Zampini ISLocalToGlobalMapping ltog, mltog; 1063aa219208SBarry Smith DMDAStencilType st; 1064b294de21SRichard Tran Mills PetscBool removedups = PETSC_FALSE, alreadyboundtocpu = PETSC_TRUE; 106547c6ae99SBarry Smith 106647c6ae99SBarry Smith PetscFunctionBegin; 106747c6ae99SBarry Smith /* 106847c6ae99SBarry Smith nc - number of components per grid point 106947c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 107047c6ae99SBarry Smith */ 1071924e958dSJunchao Zhang PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, &M, &N, NULL, &nc, &s, &bx, &by, NULL, &st)); 10721baa6e33SBarry Smith if (bx == DM_BOUNDARY_NONE && by == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_TRUE)); 107347c6ae99SBarry Smith col = 2 * s + 1; 1074c1154cd5SBarry Smith /* 1075c1154cd5SBarry Smith With one processor in periodic domains in a skinny dimension the code will label nonzero columns multiple times 1076c1154cd5SBarry Smith because of "wrapping" around the end of the domain hitting an entry already counted in the other direction. 1077c1154cd5SBarry Smith */ 1078c1154cd5SBarry Smith if (M == 1 && 2 * s >= m) removedups = PETSC_TRUE; 1079c1154cd5SBarry Smith if (N == 1 && 2 * s >= n) removedups = PETSC_TRUE; 10809566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 10819566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 10829566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 108347c6ae99SBarry Smith 10849566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * col * nc * nc, &cols)); 10859566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 108647c6ae99SBarry Smith 10879566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 108847c6ae99SBarry Smith /* determine the matrix preallocation information */ 1089d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny, nc * nx * ny, dnz, onz); 109047c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1091bff4a2f0SMatthew G. Knepley pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1092bff4a2f0SMatthew G. Knepley pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 109347c6ae99SBarry Smith 109447c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 109547c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 109647c6ae99SBarry Smith 1097bff4a2f0SMatthew G. Knepley lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1098bff4a2f0SMatthew G. Knepley lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 109947c6ae99SBarry Smith 110047c6ae99SBarry Smith cnt = 0; 110147c6ae99SBarry Smith for (k = 0; k < nc; k++) { 110247c6ae99SBarry Smith for (l = lstart; l < lend + 1; l++) { 110347c6ae99SBarry Smith for (p = pstart; p < pend + 1; p++) { 1104aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star have either l = 0 or p = 0 */ 110547c6ae99SBarry Smith cols[cnt++] = k + nc * (slot + gnx * l + p); 110647c6ae99SBarry Smith } 110747c6ae99SBarry Smith } 110847c6ae99SBarry Smith } 110947c6ae99SBarry Smith rows[k] = k + nc * (slot); 111047c6ae99SBarry Smith } 11111baa6e33SBarry Smith if (removedups) PetscCall(MatPreallocateSetLocalRemoveDups(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 11121baa6e33SBarry Smith else PetscCall(MatPreallocateSetLocal(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 111347c6ae99SBarry Smith } 1114c1154cd5SBarry Smith } 11159566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 11169566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz)); 11179566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz)); 1118d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 11199566063dSJacob Faibussowitsch PetscCall(MatGetLocalToGlobalMapping(J, &mltog, NULL)); 11201baa6e33SBarry Smith if (!mltog) PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 112147c6ae99SBarry Smith 112247c6ae99SBarry Smith /* 112347c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 112447c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 112547c6ae99SBarry Smith PETSc ordering. 112647c6ae99SBarry Smith */ 1127fcfd50ebSBarry Smith if (!da->prealloc_only) { 112847c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1129bff4a2f0SMatthew G. Knepley pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1130bff4a2f0SMatthew G. Knepley pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 113147c6ae99SBarry Smith 113247c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 113347c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 113447c6ae99SBarry Smith 1135bff4a2f0SMatthew G. Knepley lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1136bff4a2f0SMatthew G. Knepley lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 113747c6ae99SBarry Smith 113847c6ae99SBarry Smith cnt = 0; 113947c6ae99SBarry Smith for (l = lstart; l < lend + 1; l++) { 114047c6ae99SBarry Smith for (p = pstart; p < pend + 1; p++) { 1141aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star have either l = 0 or p = 0 */ 1142071fcb05SBarry Smith cols[cnt++] = nc * (slot + gnx * l + p); 1143071fcb05SBarry Smith for (k = 1; k < nc; k++) { 11449371c9d4SSatish Balay cols[cnt] = 1 + cols[cnt - 1]; 11459371c9d4SSatish Balay cnt++; 114647c6ae99SBarry Smith } 114747c6ae99SBarry Smith } 114847c6ae99SBarry Smith } 114947c6ae99SBarry Smith } 1150071fcb05SBarry Smith for (k = 0; k < nc; k++) rows[k] = k + nc * (slot); 11519566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, NULL, INSERT_VALUES)); 115247c6ae99SBarry Smith } 115347c6ae99SBarry Smith } 1154e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 11559566063dSJacob Faibussowitsch PetscCall(MatBoundToCPU(J, &alreadyboundtocpu)); 11569566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 11579566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 11589566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 11599566063dSJacob Faibussowitsch if (!alreadyboundtocpu) PetscCall(MatBindToCPU(J, PETSC_FALSE)); 11609566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 11611baa6e33SBarry Smith if (bx == DM_BOUNDARY_NONE && by == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE)); 116247c6ae99SBarry Smith } 11639566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 11643ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 116547c6ae99SBarry Smith } 116647c6ae99SBarry Smith 1167d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPIAIJ_Fill(DM da, Mat J) 1168d71ae5a4SJacob Faibussowitsch { 116947c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 1170c1154cd5SBarry Smith PetscInt m, n, dim, s, *cols, k, nc, row, col, cnt, maxcnt = 0, l, p, M, N; 117147c6ae99SBarry Smith PetscInt lstart, lend, pstart, pend, *dnz, *onz; 117247c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 117347c6ae99SBarry Smith PetscInt ifill_col, *ofill = dd->ofill, *dfill = dd->dfill; 117447c6ae99SBarry Smith MPI_Comm comm; 1175bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 117645b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 1177aa219208SBarry Smith DMDAStencilType st; 1178c1154cd5SBarry Smith PetscBool removedups = PETSC_FALSE; 117947c6ae99SBarry Smith 118047c6ae99SBarry Smith PetscFunctionBegin; 118147c6ae99SBarry Smith /* 118247c6ae99SBarry Smith nc - number of components per grid point 118347c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 118447c6ae99SBarry Smith */ 1185924e958dSJunchao Zhang PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, &M, &N, NULL, &nc, &s, &bx, &by, NULL, &st)); 118647c6ae99SBarry Smith col = 2 * s + 1; 1187c1154cd5SBarry Smith /* 1188c1154cd5SBarry Smith With one processor in periodic domains in a skinny dimension the code will label nonzero columns multiple times 1189c1154cd5SBarry Smith because of "wrapping" around the end of the domain hitting an entry already counted in the other direction. 1190c1154cd5SBarry Smith */ 1191c1154cd5SBarry Smith if (M == 1 && 2 * s >= m) removedups = PETSC_TRUE; 1192c1154cd5SBarry Smith if (N == 1 && 2 * s >= n) removedups = PETSC_TRUE; 11939566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 11949566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 11959566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 119647c6ae99SBarry Smith 11979566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * nc, &cols)); 11989566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 119947c6ae99SBarry Smith 12009566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 120147c6ae99SBarry Smith /* determine the matrix preallocation information */ 1202d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny, nc * nx * ny, dnz, onz); 120347c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1204bff4a2f0SMatthew G. Knepley pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1205bff4a2f0SMatthew G. Knepley pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 120647c6ae99SBarry Smith 120747c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 120847c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 120947c6ae99SBarry Smith 1210bff4a2f0SMatthew G. Knepley lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1211bff4a2f0SMatthew G. Knepley lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 121247c6ae99SBarry Smith 121347c6ae99SBarry Smith for (k = 0; k < nc; k++) { 121447c6ae99SBarry Smith cnt = 0; 121547c6ae99SBarry Smith for (l = lstart; l < lend + 1; l++) { 121647c6ae99SBarry Smith for (p = pstart; p < pend + 1; p++) { 121747c6ae99SBarry Smith if (l || p) { 1218aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star */ 12198865f1eaSKarl Rupp for (ifill_col = ofill[k]; ifill_col < ofill[k + 1]; ifill_col++) cols[cnt++] = ofill[ifill_col] + nc * (slot + gnx * l + p); 122047c6ae99SBarry Smith } 122147c6ae99SBarry Smith } else { 122247c6ae99SBarry Smith if (dfill) { 12238865f1eaSKarl Rupp for (ifill_col = dfill[k]; ifill_col < dfill[k + 1]; ifill_col++) cols[cnt++] = dfill[ifill_col] + nc * (slot + gnx * l + p); 122447c6ae99SBarry Smith } else { 12258865f1eaSKarl Rupp for (ifill_col = 0; ifill_col < nc; ifill_col++) cols[cnt++] = ifill_col + nc * (slot + gnx * l + p); 122647c6ae99SBarry Smith } 122747c6ae99SBarry Smith } 122847c6ae99SBarry Smith } 122947c6ae99SBarry Smith } 123047c6ae99SBarry Smith row = k + nc * (slot); 1231c0ab637bSBarry Smith maxcnt = PetscMax(maxcnt, cnt); 12321baa6e33SBarry Smith if (removedups) PetscCall(MatPreallocateSetLocalRemoveDups(ltog, 1, &row, ltog, cnt, cols, dnz, onz)); 12331baa6e33SBarry Smith else PetscCall(MatPreallocateSetLocal(ltog, 1, &row, ltog, cnt, cols, dnz, onz)); 123447c6ae99SBarry Smith } 123547c6ae99SBarry Smith } 1236c1154cd5SBarry Smith } 12379566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz)); 12389566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz)); 1239d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 12409566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 124147c6ae99SBarry Smith 124247c6ae99SBarry Smith /* 124347c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 124447c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 124547c6ae99SBarry Smith PETSc ordering. 124647c6ae99SBarry Smith */ 1247fcfd50ebSBarry Smith if (!da->prealloc_only) { 124847c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1249bff4a2f0SMatthew G. Knepley pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1250bff4a2f0SMatthew G. Knepley pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 125147c6ae99SBarry Smith 125247c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 125347c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 125447c6ae99SBarry Smith 1255bff4a2f0SMatthew G. Knepley lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1256bff4a2f0SMatthew G. Knepley lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 125747c6ae99SBarry Smith 125847c6ae99SBarry Smith for (k = 0; k < nc; k++) { 125947c6ae99SBarry Smith cnt = 0; 126047c6ae99SBarry Smith for (l = lstart; l < lend + 1; l++) { 126147c6ae99SBarry Smith for (p = pstart; p < pend + 1; p++) { 126247c6ae99SBarry Smith if (l || p) { 1263aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star */ 12648865f1eaSKarl Rupp for (ifill_col = ofill[k]; ifill_col < ofill[k + 1]; ifill_col++) cols[cnt++] = ofill[ifill_col] + nc * (slot + gnx * l + p); 126547c6ae99SBarry Smith } 126647c6ae99SBarry Smith } else { 126747c6ae99SBarry Smith if (dfill) { 12688865f1eaSKarl Rupp for (ifill_col = dfill[k]; ifill_col < dfill[k + 1]; ifill_col++) cols[cnt++] = dfill[ifill_col] + nc * (slot + gnx * l + p); 126947c6ae99SBarry Smith } else { 12708865f1eaSKarl Rupp for (ifill_col = 0; ifill_col < nc; ifill_col++) cols[cnt++] = ifill_col + nc * (slot + gnx * l + p); 127147c6ae99SBarry Smith } 127247c6ae99SBarry Smith } 127347c6ae99SBarry Smith } 127447c6ae99SBarry Smith } 127547c6ae99SBarry Smith row = k + nc * (slot); 12769566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, 1, &row, cnt, cols, NULL, INSERT_VALUES)); 127747c6ae99SBarry Smith } 127847c6ae99SBarry Smith } 127947c6ae99SBarry Smith } 1280e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 12819566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 12829566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 12839566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 12849566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 12859566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 128647c6ae99SBarry Smith } 12879566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 12883ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 128947c6ae99SBarry Smith } 129047c6ae99SBarry Smith 129147c6ae99SBarry Smith /* ---------------------------------------------------------------------------------*/ 129247c6ae99SBarry Smith 1293d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPIAIJ(DM da, Mat J) 1294d71ae5a4SJacob Faibussowitsch { 129547c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 12960298fd71SBarry Smith PetscInt m, n, dim, s, *cols = NULL, k, nc, *rows = NULL, col, cnt, l, p, *dnz = NULL, *onz = NULL; 1297c1154cd5SBarry Smith PetscInt istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk, M, N, P; 129847c6ae99SBarry Smith MPI_Comm comm; 1299bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by, bz; 1300844bd0d7SStefano Zampini ISLocalToGlobalMapping ltog, mltog; 1301aa219208SBarry Smith DMDAStencilType st; 1302c1154cd5SBarry Smith PetscBool removedups = PETSC_FALSE; 130347c6ae99SBarry Smith 130447c6ae99SBarry Smith PetscFunctionBegin; 130547c6ae99SBarry Smith /* 130647c6ae99SBarry Smith nc - number of components per grid point 130747c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 130847c6ae99SBarry Smith */ 13099566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &nc, &s, &bx, &by, &bz, &st)); 131048a46eb9SPierre Jolivet if (bx == DM_BOUNDARY_NONE && by == DM_BOUNDARY_NONE && bz == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_TRUE)); 131147c6ae99SBarry Smith col = 2 * s + 1; 131247c6ae99SBarry Smith 1313c1154cd5SBarry Smith /* 1314c1154cd5SBarry Smith With one processor in periodic domains in a skinny dimension the code will label nonzero columns multiple times 1315c1154cd5SBarry Smith because of "wrapping" around the end of the domain hitting an entry already counted in the other direction. 1316c1154cd5SBarry Smith */ 1317c1154cd5SBarry Smith if (M == 1 && 2 * s >= m) removedups = PETSC_TRUE; 1318c1154cd5SBarry Smith if (N == 1 && 2 * s >= n) removedups = PETSC_TRUE; 1319c1154cd5SBarry Smith if (P == 1 && 2 * s >= p) removedups = PETSC_TRUE; 1320c1154cd5SBarry Smith 13219566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 13229566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 13239566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 132447c6ae99SBarry Smith 13259566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * col * col * nc * nc, &cols)); 13269566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 132747c6ae99SBarry Smith 13289566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 132947c6ae99SBarry Smith /* determine the matrix preallocation information */ 1330d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny * nz, nc * nx * ny * nz, dnz, onz); 133147c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1332bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1333bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 133447c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1335bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1336bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 133747c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 1338bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 1339bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 134047c6ae99SBarry Smith 134147c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 134247c6ae99SBarry Smith 134347c6ae99SBarry Smith cnt = 0; 134447c6ae99SBarry Smith for (l = 0; l < nc; l++) { 134547c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 134647c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 134747c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 1348aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 134947c6ae99SBarry Smith cols[cnt++] = l + nc * (slot + ii + gnx * jj + gnx * gny * kk); 135047c6ae99SBarry Smith } 135147c6ae99SBarry Smith } 135247c6ae99SBarry Smith } 135347c6ae99SBarry Smith } 135447c6ae99SBarry Smith rows[l] = l + nc * (slot); 135547c6ae99SBarry Smith } 13561baa6e33SBarry Smith if (removedups) PetscCall(MatPreallocateSetLocalRemoveDups(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 13571baa6e33SBarry Smith else PetscCall(MatPreallocateSetLocal(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 135847c6ae99SBarry Smith } 135947c6ae99SBarry Smith } 1360c1154cd5SBarry Smith } 13619566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 13629566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz)); 13639566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz)); 1364d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 13659566063dSJacob Faibussowitsch PetscCall(MatGetLocalToGlobalMapping(J, &mltog, NULL)); 136648a46eb9SPierre Jolivet if (!mltog) PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 136747c6ae99SBarry Smith 136847c6ae99SBarry Smith /* 136947c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 137047c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 137147c6ae99SBarry Smith PETSc ordering. 137247c6ae99SBarry Smith */ 1373fcfd50ebSBarry Smith if (!da->prealloc_only) { 137447c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1375bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1376bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 137747c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1378bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1379bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 138047c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 1381bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 1382bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 138347c6ae99SBarry Smith 138447c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 138547c6ae99SBarry Smith 138647c6ae99SBarry Smith cnt = 0; 138747c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 1388071fcb05SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 1389071fcb05SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 1390aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 1391071fcb05SBarry Smith cols[cnt++] = nc * (slot + ii + gnx * jj + gnx * gny * kk); 1392071fcb05SBarry Smith for (l = 1; l < nc; l++) { 13939371c9d4SSatish Balay cols[cnt] = 1 + cols[cnt - 1]; 13949371c9d4SSatish Balay cnt++; 139547c6ae99SBarry Smith } 139647c6ae99SBarry Smith } 139747c6ae99SBarry Smith } 139847c6ae99SBarry Smith } 139947c6ae99SBarry Smith } 14009371c9d4SSatish Balay rows[0] = nc * (slot); 14019371c9d4SSatish Balay for (l = 1; l < nc; l++) rows[l] = 1 + rows[l - 1]; 14029566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, NULL, INSERT_VALUES)); 140347c6ae99SBarry Smith } 140447c6ae99SBarry Smith } 140547c6ae99SBarry Smith } 1406e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 14079566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 14089566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 14099566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 141048a46eb9SPierre Jolivet if (bx == DM_BOUNDARY_NONE && by == DM_BOUNDARY_NONE && bz == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE)); 14119566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 14129566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 141347c6ae99SBarry Smith } 14149566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 14153ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 141647c6ae99SBarry Smith } 141747c6ae99SBarry Smith 141847c6ae99SBarry Smith /* ---------------------------------------------------------------------------------*/ 141947c6ae99SBarry Smith 1420d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_1d_MPIAIJ_Fill(DM da, Mat J) 1421d71ae5a4SJacob Faibussowitsch { 1422ce308e1dSBarry Smith DM_DA *dd = (DM_DA *)da->data; 1423ce308e1dSBarry Smith PetscInt xs, nx, i, j, gxs, gnx, row, k, l; 14248d4c968fSBarry Smith PetscInt m, dim, s, *cols = NULL, nc, cnt, maxcnt = 0, *ocols; 14250acb5bebSBarry Smith PetscInt *ofill = dd->ofill, *dfill = dd->dfill; 1426bff4a2f0SMatthew G. Knepley DMBoundaryType bx; 142745b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 1428ce308e1dSBarry Smith PetscMPIInt rank, size; 1429ce308e1dSBarry Smith 1430ce308e1dSBarry Smith PetscFunctionBegin; 14319566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)da), &rank)); 14329566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)da), &size)); 1433ce308e1dSBarry Smith 1434ce308e1dSBarry Smith /* 1435ce308e1dSBarry Smith nc - number of components per grid point 1436ce308e1dSBarry Smith */ 14379566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, NULL, NULL, NULL, NULL, NULL, &nc, &s, &bx, NULL, NULL, NULL)); 143808401ef6SPierre Jolivet PetscCheck(s <= 1, PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "Matrix creation for 1d not implemented correctly for stencil width larger than 1"); 14399566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &nx, NULL, NULL)); 14409566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, NULL, NULL, &gnx, NULL, NULL)); 1441ce308e1dSBarry Smith 14429566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 14439566063dSJacob Faibussowitsch PetscCall(PetscCalloc2(nx * nc, &cols, nx * nc, &ocols)); 1444ce308e1dSBarry Smith 1445ce308e1dSBarry Smith /* 1446ce308e1dSBarry Smith note should be smaller for first and last process with no periodic 1447ce308e1dSBarry Smith does not handle dfill 1448ce308e1dSBarry Smith */ 1449ce308e1dSBarry Smith cnt = 0; 1450ce308e1dSBarry Smith /* coupling with process to the left */ 1451ce308e1dSBarry Smith for (i = 0; i < s; i++) { 1452ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 1453dd400576SPatrick Sanan ocols[cnt] = ((rank == 0) ? 0 : (s - i) * (ofill[j + 1] - ofill[j])); 14540acb5bebSBarry Smith cols[cnt] = dfill[j + 1] - dfill[j] + (s + i) * (ofill[j + 1] - ofill[j]); 1455dd400576SPatrick Sanan if (rank == 0 && (dd->bx == DM_BOUNDARY_PERIODIC)) { 1456831644c1SBarry Smith if (size > 1) ocols[cnt] += (s - i) * (ofill[j + 1] - ofill[j]); 1457831644c1SBarry Smith else cols[cnt] += (s - i) * (ofill[j + 1] - ofill[j]); 1458831644c1SBarry Smith } 1459c0ab637bSBarry Smith maxcnt = PetscMax(maxcnt, ocols[cnt] + cols[cnt]); 1460ce308e1dSBarry Smith cnt++; 1461ce308e1dSBarry Smith } 1462ce308e1dSBarry Smith } 1463ce308e1dSBarry Smith for (i = s; i < nx - s; i++) { 1464ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 14650acb5bebSBarry Smith cols[cnt] = dfill[j + 1] - dfill[j] + 2 * s * (ofill[j + 1] - ofill[j]); 1466c0ab637bSBarry Smith maxcnt = PetscMax(maxcnt, ocols[cnt] + cols[cnt]); 1467ce308e1dSBarry Smith cnt++; 1468ce308e1dSBarry Smith } 1469ce308e1dSBarry Smith } 1470ce308e1dSBarry Smith /* coupling with process to the right */ 1471ce308e1dSBarry Smith for (i = nx - s; i < nx; i++) { 1472ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 1473ce308e1dSBarry Smith ocols[cnt] = ((rank == (size - 1)) ? 0 : (i - nx + s + 1) * (ofill[j + 1] - ofill[j])); 14740acb5bebSBarry Smith cols[cnt] = dfill[j + 1] - dfill[j] + (s + nx - i - 1) * (ofill[j + 1] - ofill[j]); 1475831644c1SBarry Smith if ((rank == size - 1) && (dd->bx == DM_BOUNDARY_PERIODIC)) { 1476831644c1SBarry Smith if (size > 1) ocols[cnt] += (i - nx + s + 1) * (ofill[j + 1] - ofill[j]); 1477831644c1SBarry Smith else cols[cnt] += (i - nx + s + 1) * (ofill[j + 1] - ofill[j]); 1478831644c1SBarry Smith } 1479c0ab637bSBarry Smith maxcnt = PetscMax(maxcnt, ocols[cnt] + cols[cnt]); 1480ce308e1dSBarry Smith cnt++; 1481ce308e1dSBarry Smith } 1482ce308e1dSBarry Smith } 1483ce308e1dSBarry Smith 14849566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, 0, cols)); 14859566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, 0, cols, 0, ocols)); 14869566063dSJacob Faibussowitsch PetscCall(PetscFree2(cols, ocols)); 1487ce308e1dSBarry Smith 14889566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 14899566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 1490ce308e1dSBarry Smith 1491ce308e1dSBarry Smith /* 1492ce308e1dSBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 1493ce308e1dSBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 1494ce308e1dSBarry Smith PETSc ordering. 1495ce308e1dSBarry Smith */ 1496ce308e1dSBarry Smith if (!da->prealloc_only) { 14979566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(maxcnt, &cols)); 1498ce308e1dSBarry Smith row = xs * nc; 1499ce308e1dSBarry Smith /* coupling with process to the left */ 1500ce308e1dSBarry Smith for (i = xs; i < xs + s; i++) { 1501ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 1502ce308e1dSBarry Smith cnt = 0; 1503ce308e1dSBarry Smith if (rank) { 1504ce308e1dSBarry Smith for (l = 0; l < s; l++) { 1505ce308e1dSBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i - s + l) * nc + ofill[k]; 1506ce308e1dSBarry Smith } 1507ce308e1dSBarry Smith } 1508dd400576SPatrick Sanan if (rank == 0 && (dd->bx == DM_BOUNDARY_PERIODIC)) { 1509831644c1SBarry Smith for (l = 0; l < s; l++) { 1510831644c1SBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (m + i - s - l) * nc + ofill[k]; 1511831644c1SBarry Smith } 1512831644c1SBarry Smith } 15130acb5bebSBarry Smith if (dfill) { 1514ad540459SPierre Jolivet for (k = dfill[j]; k < dfill[j + 1]; k++) cols[cnt++] = i * nc + dfill[k]; 15150acb5bebSBarry Smith } else { 1516ad540459SPierre Jolivet for (k = 0; k < nc; k++) cols[cnt++] = i * nc + k; 15170acb5bebSBarry Smith } 1518ce308e1dSBarry Smith for (l = 0; l < s; l++) { 1519ce308e1dSBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i + s - l) * nc + ofill[k]; 1520ce308e1dSBarry Smith } 15219566063dSJacob Faibussowitsch PetscCall(MatSetValues(J, 1, &row, cnt, cols, NULL, INSERT_VALUES)); 1522ce308e1dSBarry Smith row++; 1523ce308e1dSBarry Smith } 1524ce308e1dSBarry Smith } 1525ce308e1dSBarry Smith for (i = xs + s; i < xs + nx - s; i++) { 1526ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 1527ce308e1dSBarry Smith cnt = 0; 1528ce308e1dSBarry Smith for (l = 0; l < s; l++) { 1529ce308e1dSBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i - s + l) * nc + ofill[k]; 1530ce308e1dSBarry Smith } 15310acb5bebSBarry Smith if (dfill) { 1532ad540459SPierre Jolivet for (k = dfill[j]; k < dfill[j + 1]; k++) cols[cnt++] = i * nc + dfill[k]; 15330acb5bebSBarry Smith } else { 1534ad540459SPierre Jolivet for (k = 0; k < nc; k++) cols[cnt++] = i * nc + k; 15350acb5bebSBarry Smith } 1536ce308e1dSBarry Smith for (l = 0; l < s; l++) { 1537ce308e1dSBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i + s - l) * nc + ofill[k]; 1538ce308e1dSBarry Smith } 15399566063dSJacob Faibussowitsch PetscCall(MatSetValues(J, 1, &row, cnt, cols, NULL, INSERT_VALUES)); 1540ce308e1dSBarry Smith row++; 1541ce308e1dSBarry Smith } 1542ce308e1dSBarry Smith } 1543ce308e1dSBarry Smith /* coupling with process to the right */ 1544ce308e1dSBarry Smith for (i = xs + nx - s; i < xs + nx; i++) { 1545ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 1546ce308e1dSBarry Smith cnt = 0; 1547ce308e1dSBarry Smith for (l = 0; l < s; l++) { 1548ce308e1dSBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i - s + l) * nc + ofill[k]; 1549ce308e1dSBarry Smith } 15500acb5bebSBarry Smith if (dfill) { 1551ad540459SPierre Jolivet for (k = dfill[j]; k < dfill[j + 1]; k++) cols[cnt++] = i * nc + dfill[k]; 15520acb5bebSBarry Smith } else { 1553ad540459SPierre Jolivet for (k = 0; k < nc; k++) cols[cnt++] = i * nc + k; 15540acb5bebSBarry Smith } 1555ce308e1dSBarry Smith if (rank < size - 1) { 1556ce308e1dSBarry Smith for (l = 0; l < s; l++) { 1557ce308e1dSBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i + s - l) * nc + ofill[k]; 1558ce308e1dSBarry Smith } 1559ce308e1dSBarry Smith } 1560831644c1SBarry Smith if ((rank == size - 1) && (dd->bx == DM_BOUNDARY_PERIODIC)) { 1561831644c1SBarry Smith for (l = 0; l < s; l++) { 1562831644c1SBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i - s - l - m + 2) * nc + ofill[k]; 1563831644c1SBarry Smith } 1564831644c1SBarry Smith } 15659566063dSJacob Faibussowitsch PetscCall(MatSetValues(J, 1, &row, cnt, cols, NULL, INSERT_VALUES)); 1566ce308e1dSBarry Smith row++; 1567ce308e1dSBarry Smith } 1568ce308e1dSBarry Smith } 15699566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 1570e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 15719566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 15729566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 15739566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 15749566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 15759566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 1576ce308e1dSBarry Smith } 15773ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1578ce308e1dSBarry Smith } 1579ce308e1dSBarry Smith 1580ce308e1dSBarry Smith /* ---------------------------------------------------------------------------------*/ 1581ce308e1dSBarry Smith 1582d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_1d_MPIAIJ(DM da, Mat J) 1583d71ae5a4SJacob Faibussowitsch { 158447c6ae99SBarry Smith PetscInt xs, nx, i, i1, slot, gxs, gnx; 15850298fd71SBarry Smith PetscInt m, dim, s, *cols = NULL, nc, *rows = NULL, col, cnt, l; 158647c6ae99SBarry Smith PetscInt istart, iend; 1587bff4a2f0SMatthew G. Knepley DMBoundaryType bx; 1588844bd0d7SStefano Zampini ISLocalToGlobalMapping ltog, mltog; 158947c6ae99SBarry Smith 159047c6ae99SBarry Smith PetscFunctionBegin; 159147c6ae99SBarry Smith /* 159247c6ae99SBarry Smith nc - number of components per grid point 159347c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 159447c6ae99SBarry Smith */ 15959566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, NULL, NULL, NULL, NULL, NULL, &nc, &s, &bx, NULL, NULL, NULL)); 159648a46eb9SPierre Jolivet if (bx == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_TRUE)); 159747c6ae99SBarry Smith col = 2 * s + 1; 159847c6ae99SBarry Smith 15999566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &nx, NULL, NULL)); 16009566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, NULL, NULL, &gnx, NULL, NULL)); 160147c6ae99SBarry Smith 16029566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 16039566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, col * nc, NULL)); 16049566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, col * nc, NULL, col * nc, NULL)); 160547c6ae99SBarry Smith 16069566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 16079566063dSJacob Faibussowitsch PetscCall(MatGetLocalToGlobalMapping(J, &mltog, NULL)); 160848a46eb9SPierre Jolivet if (!mltog) PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 160947c6ae99SBarry Smith 161047c6ae99SBarry Smith /* 161147c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 161247c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 161347c6ae99SBarry Smith PETSc ordering. 161447c6ae99SBarry Smith */ 1615fcfd50ebSBarry Smith if (!da->prealloc_only) { 16169566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * nc * nc, &cols)); 161747c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 161847c6ae99SBarry Smith istart = PetscMax(-s, gxs - i); 161947c6ae99SBarry Smith iend = PetscMin(s, gxs + gnx - i - 1); 162047c6ae99SBarry Smith slot = i - gxs; 162147c6ae99SBarry Smith 162247c6ae99SBarry Smith cnt = 0; 162347c6ae99SBarry Smith for (i1 = istart; i1 < iend + 1; i1++) { 1624071fcb05SBarry Smith cols[cnt++] = nc * (slot + i1); 1625071fcb05SBarry Smith for (l = 1; l < nc; l++) { 16269371c9d4SSatish Balay cols[cnt] = 1 + cols[cnt - 1]; 16279371c9d4SSatish Balay cnt++; 162847c6ae99SBarry Smith } 162947c6ae99SBarry Smith } 16309371c9d4SSatish Balay rows[0] = nc * (slot); 16319371c9d4SSatish Balay for (l = 1; l < nc; l++) rows[l] = 1 + rows[l - 1]; 16329566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, NULL, INSERT_VALUES)); 163347c6ae99SBarry Smith } 1634e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 16359566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 16369566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 16379566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 163848a46eb9SPierre Jolivet if (bx == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE)); 16399566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 16409566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 16419566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 1642ce308e1dSBarry Smith } 16433ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 164447c6ae99SBarry Smith } 164547c6ae99SBarry Smith 164619b08ed1SBarry Smith /* ---------------------------------------------------------------------------------*/ 164719b08ed1SBarry Smith 1648d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_1d_SeqAIJ_NoPreallocation(DM da, Mat J) 1649d71ae5a4SJacob Faibussowitsch { 165019b08ed1SBarry Smith PetscInt xs, nx, i, i1, slot, gxs, gnx; 165119b08ed1SBarry Smith PetscInt m, dim, s, *cols = NULL, nc, *rows = NULL, col, cnt, l; 165219b08ed1SBarry Smith PetscInt istart, iend; 165319b08ed1SBarry Smith DMBoundaryType bx; 165419b08ed1SBarry Smith ISLocalToGlobalMapping ltog, mltog; 165519b08ed1SBarry Smith 165619b08ed1SBarry Smith PetscFunctionBegin; 165719b08ed1SBarry Smith /* 165819b08ed1SBarry Smith nc - number of components per grid point 165919b08ed1SBarry Smith col - number of colors needed in one direction for single component problem 166019b08ed1SBarry Smith */ 16619566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, NULL, NULL, NULL, NULL, NULL, &nc, &s, &bx, NULL, NULL, NULL)); 166219b08ed1SBarry Smith col = 2 * s + 1; 166319b08ed1SBarry Smith 16649566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &nx, NULL, NULL)); 16659566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, NULL, NULL, &gnx, NULL, NULL)); 166619b08ed1SBarry Smith 16679566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 16689566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetTotalPreallocation(J, nx * nc * col * nc)); 166919b08ed1SBarry Smith 16709566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 16719566063dSJacob Faibussowitsch PetscCall(MatGetLocalToGlobalMapping(J, &mltog, NULL)); 167248a46eb9SPierre Jolivet if (!mltog) PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 167319b08ed1SBarry Smith 167419b08ed1SBarry Smith /* 167519b08ed1SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 167619b08ed1SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 167719b08ed1SBarry Smith PETSc ordering. 167819b08ed1SBarry Smith */ 167919b08ed1SBarry Smith if (!da->prealloc_only) { 16809566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * nc * nc, &cols)); 168119b08ed1SBarry Smith for (i = xs; i < xs + nx; i++) { 168219b08ed1SBarry Smith istart = PetscMax(-s, gxs - i); 168319b08ed1SBarry Smith iend = PetscMin(s, gxs + gnx - i - 1); 168419b08ed1SBarry Smith slot = i - gxs; 168519b08ed1SBarry Smith 168619b08ed1SBarry Smith cnt = 0; 168719b08ed1SBarry Smith for (i1 = istart; i1 < iend + 1; i1++) { 168819b08ed1SBarry Smith cols[cnt++] = nc * (slot + i1); 168919b08ed1SBarry Smith for (l = 1; l < nc; l++) { 16909371c9d4SSatish Balay cols[cnt] = 1 + cols[cnt - 1]; 16919371c9d4SSatish Balay cnt++; 169219b08ed1SBarry Smith } 169319b08ed1SBarry Smith } 16949371c9d4SSatish Balay rows[0] = nc * (slot); 16959371c9d4SSatish Balay for (l = 1; l < nc; l++) rows[l] = 1 + rows[l - 1]; 16969566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, NULL, INSERT_VALUES)); 169719b08ed1SBarry Smith } 169819b08ed1SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 16999566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 17009566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 17019566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 170248a46eb9SPierre Jolivet if (bx == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE)); 17039566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 17049566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 17059566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 170619b08ed1SBarry Smith } 17079566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE)); 17083ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 170919b08ed1SBarry Smith } 171019b08ed1SBarry Smith 1711d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPIBAIJ(DM da, Mat J) 1712d71ae5a4SJacob Faibussowitsch { 171347c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 171447c6ae99SBarry Smith PetscInt m, n, dim, s, *cols, nc, col, cnt, *dnz, *onz; 171547c6ae99SBarry Smith PetscInt istart, iend, jstart, jend, ii, jj; 171647c6ae99SBarry Smith MPI_Comm comm; 171747c6ae99SBarry Smith PetscScalar *values; 1718bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 1719aa219208SBarry Smith DMDAStencilType st; 172045b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 172147c6ae99SBarry Smith 172247c6ae99SBarry Smith PetscFunctionBegin; 172347c6ae99SBarry Smith /* 172447c6ae99SBarry Smith nc - number of components per grid point 172547c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 172647c6ae99SBarry Smith */ 17279566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, NULL, NULL, NULL, &nc, &s, &bx, &by, NULL, &st)); 172847c6ae99SBarry Smith col = 2 * s + 1; 172947c6ae99SBarry Smith 17309566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 17319566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 17329566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 173347c6ae99SBarry Smith 17349566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * nc * nc, &cols)); 173547c6ae99SBarry Smith 17369566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 173747c6ae99SBarry Smith 173847c6ae99SBarry Smith /* determine the matrix preallocation information */ 1739d0609cedSBarry Smith MatPreallocateBegin(comm, nx * ny, nx * ny, dnz, onz); 174047c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1741bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1742bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 174347c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1744bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1745bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 174647c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 174747c6ae99SBarry Smith 174847c6ae99SBarry Smith /* Find block columns in block row */ 174947c6ae99SBarry Smith cnt = 0; 175047c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 175147c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 1752aa219208SBarry Smith if (st == DMDA_STENCIL_BOX || !ii || !jj) { /* BOX or on the STAR */ 175347c6ae99SBarry Smith cols[cnt++] = slot + ii + gnx * jj; 175447c6ae99SBarry Smith } 175547c6ae99SBarry Smith } 175647c6ae99SBarry Smith } 17579566063dSJacob Faibussowitsch PetscCall(MatPreallocateSetLocalBlock(ltog, 1, &slot, ltog, cnt, cols, dnz, onz)); 175847c6ae99SBarry Smith } 175947c6ae99SBarry Smith } 17609566063dSJacob Faibussowitsch PetscCall(MatSeqBAIJSetPreallocation(J, nc, 0, dnz)); 17619566063dSJacob Faibussowitsch PetscCall(MatMPIBAIJSetPreallocation(J, nc, 0, dnz, 0, onz)); 1762d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 176347c6ae99SBarry Smith 17649566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 176547c6ae99SBarry Smith 176647c6ae99SBarry Smith /* 176747c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 176847c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 176947c6ae99SBarry Smith PETSc ordering. 177047c6ae99SBarry Smith */ 1771fcfd50ebSBarry Smith if (!da->prealloc_only) { 17729566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * nc * nc, &values)); 177347c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1774bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1775bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 177647c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1777bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1778bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 177947c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 178047c6ae99SBarry Smith cnt = 0; 178147c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 178247c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 1783aa219208SBarry Smith if (st == DMDA_STENCIL_BOX || !ii || !jj) { /* BOX or on the STAR */ 178447c6ae99SBarry Smith cols[cnt++] = slot + ii + gnx * jj; 178547c6ae99SBarry Smith } 178647c6ae99SBarry Smith } 178747c6ae99SBarry Smith } 17889566063dSJacob Faibussowitsch PetscCall(MatSetValuesBlockedLocal(J, 1, &slot, cnt, cols, values, INSERT_VALUES)); 178947c6ae99SBarry Smith } 179047c6ae99SBarry Smith } 17919566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 1792e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 17939566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 17949566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 17959566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 17969566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 17979566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 179847c6ae99SBarry Smith } 17999566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 18003ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 180147c6ae99SBarry Smith } 180247c6ae99SBarry Smith 1803d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPIBAIJ(DM da, Mat J) 1804d71ae5a4SJacob Faibussowitsch { 180547c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 180647c6ae99SBarry Smith PetscInt m, n, dim, s, *cols, k, nc, col, cnt, p, *dnz, *onz; 180747c6ae99SBarry Smith PetscInt istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk; 180847c6ae99SBarry Smith MPI_Comm comm; 180947c6ae99SBarry Smith PetscScalar *values; 1810bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by, bz; 1811aa219208SBarry Smith DMDAStencilType st; 181245b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 181347c6ae99SBarry Smith 181447c6ae99SBarry Smith PetscFunctionBegin; 181547c6ae99SBarry Smith /* 181647c6ae99SBarry Smith nc - number of components per grid point 181747c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 181847c6ae99SBarry Smith */ 18199566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, NULL, NULL, NULL, &nc, &s, &bx, &by, &bz, &st)); 182047c6ae99SBarry Smith col = 2 * s + 1; 182147c6ae99SBarry Smith 18229566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 18239566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 18249566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 182547c6ae99SBarry Smith 18269566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * col, &cols)); 182747c6ae99SBarry Smith 18289566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 182947c6ae99SBarry Smith 183047c6ae99SBarry Smith /* determine the matrix preallocation information */ 1831d0609cedSBarry Smith MatPreallocateBegin(comm, nx * ny * nz, nx * ny * nz, dnz, onz); 183247c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1833bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1834bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 183547c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1836bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1837bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 183847c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 1839bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 1840bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 184147c6ae99SBarry Smith 184247c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 184347c6ae99SBarry Smith 184447c6ae99SBarry Smith /* Find block columns in block row */ 184547c6ae99SBarry Smith cnt = 0; 184647c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 184747c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 184847c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 1849aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 185047c6ae99SBarry Smith cols[cnt++] = slot + ii + gnx * jj + gnx * gny * kk; 185147c6ae99SBarry Smith } 185247c6ae99SBarry Smith } 185347c6ae99SBarry Smith } 185447c6ae99SBarry Smith } 18559566063dSJacob Faibussowitsch PetscCall(MatPreallocateSetLocalBlock(ltog, 1, &slot, ltog, cnt, cols, dnz, onz)); 185647c6ae99SBarry Smith } 185747c6ae99SBarry Smith } 185847c6ae99SBarry Smith } 18599566063dSJacob Faibussowitsch PetscCall(MatSeqBAIJSetPreallocation(J, nc, 0, dnz)); 18609566063dSJacob Faibussowitsch PetscCall(MatMPIBAIJSetPreallocation(J, nc, 0, dnz, 0, onz)); 1861d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 186247c6ae99SBarry Smith 18639566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 186447c6ae99SBarry Smith 186547c6ae99SBarry Smith /* 186647c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 186747c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 186847c6ae99SBarry Smith PETSc ordering. 186947c6ae99SBarry Smith */ 1870fcfd50ebSBarry Smith if (!da->prealloc_only) { 18719566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * col * nc * nc, &values)); 187247c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1873bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1874bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 187547c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1876bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1877bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 187847c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 1879bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 1880bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 188147c6ae99SBarry Smith 188247c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 188347c6ae99SBarry Smith 188447c6ae99SBarry Smith cnt = 0; 188547c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 188647c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 188747c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 1888aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 188947c6ae99SBarry Smith cols[cnt++] = slot + ii + gnx * jj + gnx * gny * kk; 189047c6ae99SBarry Smith } 189147c6ae99SBarry Smith } 189247c6ae99SBarry Smith } 189347c6ae99SBarry Smith } 18949566063dSJacob Faibussowitsch PetscCall(MatSetValuesBlockedLocal(J, 1, &slot, cnt, cols, values, INSERT_VALUES)); 189547c6ae99SBarry Smith } 189647c6ae99SBarry Smith } 189747c6ae99SBarry Smith } 18989566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 1899e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 19009566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 19019566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 19029566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 19039566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 19049566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 190547c6ae99SBarry Smith } 19069566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 19073ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 190847c6ae99SBarry Smith } 190947c6ae99SBarry Smith 191047c6ae99SBarry Smith /* 191147c6ae99SBarry Smith This helper is for of SBAIJ preallocation, to discard the lower-triangular values which are difficult to 191247c6ae99SBarry Smith identify in the local ordering with periodic domain. 191347c6ae99SBarry Smith */ 1914d71ae5a4SJacob Faibussowitsch static PetscErrorCode L2GFilterUpperTriangular(ISLocalToGlobalMapping ltog, PetscInt *row, PetscInt *cnt, PetscInt col[]) 1915d71ae5a4SJacob Faibussowitsch { 191647c6ae99SBarry Smith PetscInt i, n; 191747c6ae99SBarry Smith 191847c6ae99SBarry Smith PetscFunctionBegin; 19199566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingApplyBlock(ltog, 1, row, row)); 19209566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingApplyBlock(ltog, *cnt, col, col)); 192147c6ae99SBarry Smith for (i = 0, n = 0; i < *cnt; i++) { 192247c6ae99SBarry Smith if (col[i] >= *row) col[n++] = col[i]; 192347c6ae99SBarry Smith } 192447c6ae99SBarry Smith *cnt = n; 19253ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 192647c6ae99SBarry Smith } 192747c6ae99SBarry Smith 1928d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPISBAIJ(DM da, Mat J) 1929d71ae5a4SJacob Faibussowitsch { 193047c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 193147c6ae99SBarry Smith PetscInt m, n, dim, s, *cols, nc, col, cnt, *dnz, *onz; 193247c6ae99SBarry Smith PetscInt istart, iend, jstart, jend, ii, jj; 193347c6ae99SBarry Smith MPI_Comm comm; 193447c6ae99SBarry Smith PetscScalar *values; 1935bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 1936aa219208SBarry Smith DMDAStencilType st; 193745b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 193847c6ae99SBarry Smith 193947c6ae99SBarry Smith PetscFunctionBegin; 194047c6ae99SBarry Smith /* 194147c6ae99SBarry Smith nc - number of components per grid point 194247c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 194347c6ae99SBarry Smith */ 19449566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, NULL, NULL, NULL, &nc, &s, &bx, &by, NULL, &st)); 194547c6ae99SBarry Smith col = 2 * s + 1; 194647c6ae99SBarry Smith 19479566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 19489566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 19499566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 195047c6ae99SBarry Smith 19519566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * nc * nc, &cols)); 195247c6ae99SBarry Smith 19539566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 195447c6ae99SBarry Smith 195547c6ae99SBarry Smith /* determine the matrix preallocation information */ 1956d0609cedSBarry Smith MatPreallocateBegin(comm, nx * ny, nx * ny, dnz, onz); 195747c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1958bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1959bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 196047c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1961bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1962bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 196347c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 196447c6ae99SBarry Smith 196547c6ae99SBarry Smith /* Find block columns in block row */ 196647c6ae99SBarry Smith cnt = 0; 196747c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 196847c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 1969ad540459SPierre Jolivet if (st == DMDA_STENCIL_BOX || !ii || !jj) cols[cnt++] = slot + ii + gnx * jj; 197047c6ae99SBarry Smith } 197147c6ae99SBarry Smith } 19729566063dSJacob Faibussowitsch PetscCall(L2GFilterUpperTriangular(ltog, &slot, &cnt, cols)); 19739566063dSJacob Faibussowitsch PetscCall(MatPreallocateSymmetricSetBlock(slot, cnt, cols, dnz, onz)); 197447c6ae99SBarry Smith } 197547c6ae99SBarry Smith } 19769566063dSJacob Faibussowitsch PetscCall(MatSeqSBAIJSetPreallocation(J, nc, 0, dnz)); 19779566063dSJacob Faibussowitsch PetscCall(MatMPISBAIJSetPreallocation(J, nc, 0, dnz, 0, onz)); 1978d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 197947c6ae99SBarry Smith 19809566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 198147c6ae99SBarry Smith 198247c6ae99SBarry Smith /* 198347c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 198447c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 198547c6ae99SBarry Smith PETSc ordering. 198647c6ae99SBarry Smith */ 1987fcfd50ebSBarry Smith if (!da->prealloc_only) { 19889566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * nc * nc, &values)); 198947c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1990bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1991bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 199247c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1993bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1994bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 199547c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 199647c6ae99SBarry Smith 199747c6ae99SBarry Smith /* Find block columns in block row */ 199847c6ae99SBarry Smith cnt = 0; 199947c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 200047c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 2001ad540459SPierre Jolivet if (st == DMDA_STENCIL_BOX || !ii || !jj) cols[cnt++] = slot + ii + gnx * jj; 200247c6ae99SBarry Smith } 200347c6ae99SBarry Smith } 20049566063dSJacob Faibussowitsch PetscCall(L2GFilterUpperTriangular(ltog, &slot, &cnt, cols)); 20059566063dSJacob Faibussowitsch PetscCall(MatSetValuesBlocked(J, 1, &slot, cnt, cols, values, INSERT_VALUES)); 200647c6ae99SBarry Smith } 200747c6ae99SBarry Smith } 20089566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 2009e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 20109566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 20119566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 20129566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 20139566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 20149566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 201547c6ae99SBarry Smith } 20169566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 20173ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 201847c6ae99SBarry Smith } 201947c6ae99SBarry Smith 2020d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPISBAIJ(DM da, Mat J) 2021d71ae5a4SJacob Faibussowitsch { 202247c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 202347c6ae99SBarry Smith PetscInt m, n, dim, s, *cols, k, nc, col, cnt, p, *dnz, *onz; 202447c6ae99SBarry Smith PetscInt istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk; 202547c6ae99SBarry Smith MPI_Comm comm; 202647c6ae99SBarry Smith PetscScalar *values; 2027bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by, bz; 2028aa219208SBarry Smith DMDAStencilType st; 202945b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 203047c6ae99SBarry Smith 203147c6ae99SBarry Smith PetscFunctionBegin; 203247c6ae99SBarry Smith /* 203347c6ae99SBarry Smith nc - number of components per grid point 203447c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 203547c6ae99SBarry Smith */ 20369566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, NULL, NULL, NULL, &nc, &s, &bx, &by, &bz, &st)); 203747c6ae99SBarry Smith col = 2 * s + 1; 203847c6ae99SBarry Smith 20399566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 20409566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 20419566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 204247c6ae99SBarry Smith 204347c6ae99SBarry Smith /* create the matrix */ 20449566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * col, &cols)); 204547c6ae99SBarry Smith 20469566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 204747c6ae99SBarry Smith 204847c6ae99SBarry Smith /* determine the matrix preallocation information */ 2049d0609cedSBarry Smith MatPreallocateBegin(comm, nx * ny * nz, nx * ny * nz, dnz, onz); 205047c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 2051bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 2052bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 205347c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 2054bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 2055bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 205647c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 2057bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 2058bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 205947c6ae99SBarry Smith 206047c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 206147c6ae99SBarry Smith 206247c6ae99SBarry Smith /* Find block columns in block row */ 206347c6ae99SBarry Smith cnt = 0; 206447c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 206547c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 206647c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 2067ad540459SPierre Jolivet if ((st == DMDA_STENCIL_BOX) || (!ii && !jj) || (!jj && !kk) || (!ii && !kk)) cols[cnt++] = slot + ii + gnx * jj + gnx * gny * kk; 206847c6ae99SBarry Smith } 206947c6ae99SBarry Smith } 207047c6ae99SBarry Smith } 20719566063dSJacob Faibussowitsch PetscCall(L2GFilterUpperTriangular(ltog, &slot, &cnt, cols)); 20729566063dSJacob Faibussowitsch PetscCall(MatPreallocateSymmetricSetBlock(slot, cnt, cols, dnz, onz)); 207347c6ae99SBarry Smith } 207447c6ae99SBarry Smith } 207547c6ae99SBarry Smith } 20769566063dSJacob Faibussowitsch PetscCall(MatSeqSBAIJSetPreallocation(J, nc, 0, dnz)); 20779566063dSJacob Faibussowitsch PetscCall(MatMPISBAIJSetPreallocation(J, nc, 0, dnz, 0, onz)); 2078d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 207947c6ae99SBarry Smith 20809566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 208147c6ae99SBarry Smith 208247c6ae99SBarry Smith /* 208347c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 208447c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 208547c6ae99SBarry Smith PETSc ordering. 208647c6ae99SBarry Smith */ 2087fcfd50ebSBarry Smith if (!da->prealloc_only) { 20889566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * col * nc * nc, &values)); 208947c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 2090bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 2091bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 209247c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 2093bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 2094bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 209547c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 2096bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 2097bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 209847c6ae99SBarry Smith 209947c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 210047c6ae99SBarry Smith 210147c6ae99SBarry Smith cnt = 0; 210247c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 210347c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 210447c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 2105ad540459SPierre Jolivet if ((st == DMDA_STENCIL_BOX) || (!ii && !jj) || (!jj && !kk) || (!ii && !kk)) cols[cnt++] = slot + ii + gnx * jj + gnx * gny * kk; 210647c6ae99SBarry Smith } 210747c6ae99SBarry Smith } 210847c6ae99SBarry Smith } 21099566063dSJacob Faibussowitsch PetscCall(L2GFilterUpperTriangular(ltog, &slot, &cnt, cols)); 21109566063dSJacob Faibussowitsch PetscCall(MatSetValuesBlocked(J, 1, &slot, cnt, cols, values, INSERT_VALUES)); 211147c6ae99SBarry Smith } 211247c6ae99SBarry Smith } 211347c6ae99SBarry Smith } 21149566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 2115e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 21169566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 21179566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 21189566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 21199566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 21209566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 212147c6ae99SBarry Smith } 21229566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 21233ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 212447c6ae99SBarry Smith } 212547c6ae99SBarry Smith 212647c6ae99SBarry Smith /* ---------------------------------------------------------------------------------*/ 212747c6ae99SBarry Smith 2128d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPIAIJ_Fill(DM da, Mat J) 2129d71ae5a4SJacob Faibussowitsch { 213047c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 2131c0ab637bSBarry Smith PetscInt m, n, dim, s, *cols, k, nc, row, col, cnt, maxcnt = 0, l, p, *dnz, *onz; 2132c1154cd5SBarry Smith PetscInt istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk, M, N, P; 213347c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 213447c6ae99SBarry Smith PetscInt ifill_col, *dfill = dd->dfill, *ofill = dd->ofill; 213547c6ae99SBarry Smith MPI_Comm comm; 213647c6ae99SBarry Smith PetscScalar *values; 2137bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by, bz; 213845b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 2139aa219208SBarry Smith DMDAStencilType st; 2140c1154cd5SBarry Smith PetscBool removedups = PETSC_FALSE; 214147c6ae99SBarry Smith 214247c6ae99SBarry Smith PetscFunctionBegin; 214347c6ae99SBarry Smith /* 214447c6ae99SBarry Smith nc - number of components per grid point 214547c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 214647c6ae99SBarry Smith */ 21479566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &nc, &s, &bx, &by, &bz, &st)); 214847c6ae99SBarry Smith col = 2 * s + 1; 214947c6ae99SBarry Smith 2150c1154cd5SBarry Smith /* 2151c1154cd5SBarry Smith With one processor in periodic domains in a skinny dimension the code will label nonzero columns multiple times 2152c1154cd5SBarry Smith because of "wrapping" around the end of the domain hitting an entry already counted in the other direction. 2153c1154cd5SBarry Smith */ 2154c1154cd5SBarry Smith if (M == 1 && 2 * s >= m) removedups = PETSC_TRUE; 2155c1154cd5SBarry Smith if (N == 1 && 2 * s >= n) removedups = PETSC_TRUE; 2156c1154cd5SBarry Smith if (P == 1 && 2 * s >= p) removedups = PETSC_TRUE; 2157c1154cd5SBarry Smith 21589566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 21599566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 21609566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 216147c6ae99SBarry Smith 21629566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * col * nc, &cols)); 21639566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 216447c6ae99SBarry Smith 216547c6ae99SBarry Smith /* determine the matrix preallocation information */ 2166d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny * nz, nc * nx * ny * nz, dnz, onz); 216747c6ae99SBarry Smith 21689566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 216947c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 2170bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 2171bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 217247c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 2173bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 2174bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 217547c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 2176bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 2177bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 217847c6ae99SBarry Smith 217947c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 218047c6ae99SBarry Smith 218147c6ae99SBarry Smith for (l = 0; l < nc; l++) { 218247c6ae99SBarry Smith cnt = 0; 218347c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 218447c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 218547c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 218647c6ae99SBarry Smith if (ii || jj || kk) { 2187aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 21888865f1eaSKarl 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); 218947c6ae99SBarry Smith } 219047c6ae99SBarry Smith } else { 219147c6ae99SBarry Smith if (dfill) { 21928865f1eaSKarl 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); 219347c6ae99SBarry Smith } else { 21948865f1eaSKarl Rupp for (ifill_col = 0; ifill_col < nc; ifill_col++) cols[cnt++] = ifill_col + nc * (slot + ii + gnx * jj + gnx * gny * kk); 219547c6ae99SBarry Smith } 219647c6ae99SBarry Smith } 219747c6ae99SBarry Smith } 219847c6ae99SBarry Smith } 219947c6ae99SBarry Smith } 220047c6ae99SBarry Smith row = l + nc * (slot); 2201c0ab637bSBarry Smith maxcnt = PetscMax(maxcnt, cnt); 22021baa6e33SBarry Smith if (removedups) PetscCall(MatPreallocateSetLocalRemoveDups(ltog, 1, &row, ltog, cnt, cols, dnz, onz)); 22031baa6e33SBarry Smith else PetscCall(MatPreallocateSetLocal(ltog, 1, &row, ltog, cnt, cols, dnz, onz)); 220447c6ae99SBarry Smith } 220547c6ae99SBarry Smith } 220647c6ae99SBarry Smith } 2207c1154cd5SBarry Smith } 22089566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz)); 22099566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz)); 2210d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 22119566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 221247c6ae99SBarry Smith 221347c6ae99SBarry Smith /* 221447c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 221547c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 221647c6ae99SBarry Smith PETSc ordering. 221747c6ae99SBarry Smith */ 2218fcfd50ebSBarry Smith if (!da->prealloc_only) { 22199566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(maxcnt, &values)); 222047c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 2221bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 2222bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 222347c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 2224bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 2225bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 222647c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 2227bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 2228bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 222947c6ae99SBarry Smith 223047c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 223147c6ae99SBarry Smith 223247c6ae99SBarry Smith for (l = 0; l < nc; l++) { 223347c6ae99SBarry Smith cnt = 0; 223447c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 223547c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 223647c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 223747c6ae99SBarry Smith if (ii || jj || kk) { 2238aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 22398865f1eaSKarl 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); 224047c6ae99SBarry Smith } 224147c6ae99SBarry Smith } else { 224247c6ae99SBarry Smith if (dfill) { 22438865f1eaSKarl 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); 224447c6ae99SBarry Smith } else { 22458865f1eaSKarl Rupp for (ifill_col = 0; ifill_col < nc; ifill_col++) cols[cnt++] = ifill_col + nc * (slot + ii + gnx * jj + gnx * gny * kk); 224647c6ae99SBarry Smith } 224747c6ae99SBarry Smith } 224847c6ae99SBarry Smith } 224947c6ae99SBarry Smith } 225047c6ae99SBarry Smith } 225147c6ae99SBarry Smith row = l + nc * (slot); 22529566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, 1, &row, cnt, cols, values, INSERT_VALUES)); 225347c6ae99SBarry Smith } 225447c6ae99SBarry Smith } 225547c6ae99SBarry Smith } 225647c6ae99SBarry Smith } 22579566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 2258e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 22599566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 22609566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 22619566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 22629566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 22639566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 226447c6ae99SBarry Smith } 22659566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 22663ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 226747c6ae99SBarry Smith } 2268