1af0996ceSBarry Smith #include <petsc/private/dmdaimpl.h> /*I "petscdmda.h" I*/ 207475bc1SBarry Smith #include <petscmat.h> 3e432b41dSStefano Zampini #include <petscbt.h> 447c6ae99SBarry Smith 5e727c939SJed Brown extern PetscErrorCode DMCreateColoring_DA_1d_MPIAIJ(DM, ISColoringType, ISColoring *); 6e727c939SJed Brown extern PetscErrorCode DMCreateColoring_DA_2d_MPIAIJ(DM, ISColoringType, ISColoring *); 7e727c939SJed Brown extern PetscErrorCode DMCreateColoring_DA_2d_5pt_MPIAIJ(DM, ISColoringType, ISColoring *); 8e727c939SJed Brown extern PetscErrorCode DMCreateColoring_DA_3d_MPIAIJ(DM, ISColoringType, ISColoring *); 947c6ae99SBarry Smith 1047c6ae99SBarry Smith /* 1147c6ae99SBarry Smith For ghost i that may be negative or greater than the upper bound this 1247c6ae99SBarry Smith maps it into the 0:m-1 range using periodicity 1347c6ae99SBarry Smith */ 1447c6ae99SBarry Smith #define SetInRange(i, m) ((i < 0) ? m + i : ((i >= m) ? i - m : i)) 1547c6ae99SBarry Smith 16d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDASetBlockFills_Private(const PetscInt *dfill, PetscInt w, PetscInt **rfill) 17d71ae5a4SJacob Faibussowitsch { 1847c6ae99SBarry Smith PetscInt i, j, nz, *fill; 1947c6ae99SBarry Smith 2047c6ae99SBarry Smith PetscFunctionBegin; 213ba16761SJacob Faibussowitsch if (!dfill) PetscFunctionReturn(PETSC_SUCCESS); 2247c6ae99SBarry Smith 2347c6ae99SBarry Smith /* count number nonzeros */ 2447c6ae99SBarry Smith nz = 0; 2547c6ae99SBarry Smith for (i = 0; i < w; i++) { 2647c6ae99SBarry Smith for (j = 0; j < w; j++) { 2747c6ae99SBarry Smith if (dfill[w * i + j]) nz++; 2847c6ae99SBarry Smith } 2947c6ae99SBarry Smith } 309566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nz + w + 1, &fill)); 3147c6ae99SBarry Smith /* construct modified CSR storage of nonzero structure */ 32ce308e1dSBarry Smith /* fill[0 -- w] marks starts of each row of column indices (and end of last row) 33ce308e1dSBarry Smith so fill[1] - fill[0] gives number of nonzeros in first row etc */ 3447c6ae99SBarry Smith nz = w + 1; 3547c6ae99SBarry Smith for (i = 0; i < w; i++) { 3647c6ae99SBarry Smith fill[i] = nz; 3747c6ae99SBarry Smith for (j = 0; j < w; j++) { 3847c6ae99SBarry Smith if (dfill[w * i + j]) { 3947c6ae99SBarry Smith fill[nz] = j; 4047c6ae99SBarry Smith nz++; 4147c6ae99SBarry Smith } 4247c6ae99SBarry Smith } 4347c6ae99SBarry Smith } 4447c6ae99SBarry Smith fill[w] = nz; 4547c6ae99SBarry Smith 4647c6ae99SBarry Smith *rfill = fill; 473ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4847c6ae99SBarry Smith } 4947c6ae99SBarry Smith 50d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDASetBlockFillsSparse_Private(const PetscInt *dfillsparse, PetscInt w, PetscInt **rfill) 51d71ae5a4SJacob Faibussowitsch { 52767d920cSKarl Rupp PetscInt nz; 5309e28618SBarry Smith 5409e28618SBarry Smith PetscFunctionBegin; 553ba16761SJacob Faibussowitsch if (!dfillsparse) PetscFunctionReturn(PETSC_SUCCESS); 5609e28618SBarry Smith 5709e28618SBarry Smith /* Determine number of non-zeros */ 5809e28618SBarry Smith nz = (dfillsparse[w] - w - 1); 5909e28618SBarry Smith 6009e28618SBarry Smith /* Allocate space for our copy of the given sparse matrix representation. */ 619566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nz + w + 1, rfill)); 629566063dSJacob Faibussowitsch PetscCall(PetscArraycpy(*rfill, dfillsparse, nz + w + 1)); 633ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 6409e28618SBarry Smith } 6509e28618SBarry Smith 66d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDASetBlockFills_Private2(DM_DA *dd) 67d71ae5a4SJacob Faibussowitsch { 6809e28618SBarry Smith PetscInt i, k, cnt = 1; 6909e28618SBarry Smith 7009e28618SBarry Smith PetscFunctionBegin; 7109e28618SBarry Smith 7209e28618SBarry Smith /* ofillcount tracks the columns of ofill that have any nonzero in thems; the value in each location is the number of 7309e28618SBarry Smith columns to the left with any nonzeros in them plus 1 */ 749566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(dd->w, &dd->ofillcols)); 7509e28618SBarry Smith for (i = 0; i < dd->w; i++) { 7609e28618SBarry Smith for (k = dd->ofill[i]; k < dd->ofill[i + 1]; k++) dd->ofillcols[dd->ofill[k]] = 1; 7709e28618SBarry Smith } 7809e28618SBarry Smith for (i = 0; i < dd->w; i++) { 79ad540459SPierre Jolivet if (dd->ofillcols[i]) dd->ofillcols[i] = cnt++; 8009e28618SBarry Smith } 813ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 8209e28618SBarry Smith } 8309e28618SBarry Smith 8447c6ae99SBarry Smith /*@ 85aa219208SBarry Smith DMDASetBlockFills - Sets the fill pattern in each block for a multi-component problem 86dce8aebaSBarry Smith of the matrix returned by `DMCreateMatrix()`. 8747c6ae99SBarry Smith 8820f4b53cSBarry Smith Logically Collective 8947c6ae99SBarry Smith 90d8d19677SJose E. Roman Input Parameters: 9147c6ae99SBarry Smith + da - the distributed array 920298fd71SBarry Smith . dfill - the fill pattern in the diagonal block (may be NULL, means use dense block) 9347c6ae99SBarry Smith - ofill - the fill pattern in the off-diagonal blocks 9447c6ae99SBarry Smith 9547c6ae99SBarry Smith Level: developer 9647c6ae99SBarry Smith 9795452b02SPatrick Sanan Notes: 9895452b02SPatrick Sanan This only makes sense when you are doing multicomponent problems but using the 99dce8aebaSBarry Smith `MATMPIAIJ` matrix format 10047c6ae99SBarry Smith 10147c6ae99SBarry Smith The format for dfill and ofill is a 2 dimensional dof by dof matrix with 1 entries 10247c6ae99SBarry Smith representing coupling and 0 entries for missing coupling. For example 103dce8aebaSBarry Smith .vb 104dce8aebaSBarry Smith dfill[9] = {1, 0, 0, 105dce8aebaSBarry Smith 1, 1, 0, 106dce8aebaSBarry Smith 0, 1, 1} 107dce8aebaSBarry Smith .ve 10847c6ae99SBarry Smith means that row 0 is coupled with only itself in the diagonal block, row 1 is coupled with 10947c6ae99SBarry Smith itself and row 0 (in the diagonal block) and row 2 is coupled with itself and row 1 (in the 11047c6ae99SBarry Smith diagonal block). 11147c6ae99SBarry Smith 112dce8aebaSBarry Smith `DMDASetGetMatrix()` allows you to provide general code for those more complicated nonzero patterns then 11347c6ae99SBarry Smith can be represented in the dfill, ofill format 11447c6ae99SBarry Smith 11547c6ae99SBarry Smith Contributed by Glenn Hammond 11647c6ae99SBarry Smith 117dce8aebaSBarry Smith .seealso: `DM`, `DMDA`, `DMCreateMatrix()`, `DMDASetGetMatrix()`, `DMSetMatrixPreallocateOnly()` 11847c6ae99SBarry Smith @*/ 119d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDASetBlockFills(DM da, const PetscInt *dfill, const PetscInt *ofill) 120d71ae5a4SJacob Faibussowitsch { 12147c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 12247c6ae99SBarry Smith 12347c6ae99SBarry Smith PetscFunctionBegin; 12409e28618SBarry Smith /* save the given dfill and ofill information */ 1259566063dSJacob Faibussowitsch PetscCall(DMDASetBlockFills_Private(dfill, dd->w, &dd->dfill)); 1269566063dSJacob Faibussowitsch PetscCall(DMDASetBlockFills_Private(ofill, dd->w, &dd->ofill)); 127ae4f298aSBarry Smith 12809e28618SBarry Smith /* count nonzeros in ofill columns */ 1299566063dSJacob Faibussowitsch PetscCall(DMDASetBlockFills_Private2(dd)); 1303ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 131ae4f298aSBarry Smith } 13209e28618SBarry Smith 13309e28618SBarry Smith /*@ 13409e28618SBarry Smith DMDASetBlockFillsSparse - Sets the fill pattern in each block for a multi-component problem 135dce8aebaSBarry Smith of the matrix returned by `DMCreateMatrix()`, using sparse representations 13609e28618SBarry Smith of fill patterns. 13709e28618SBarry Smith 13820f4b53cSBarry Smith Logically Collective 13909e28618SBarry Smith 140d8d19677SJose E. Roman Input Parameters: 14109e28618SBarry Smith + da - the distributed array 14260225df5SJacob Faibussowitsch . dfillsparse - the sparse fill pattern in the diagonal block (may be `NULL`, means use dense block) 14360225df5SJacob Faibussowitsch - ofillsparse - the sparse fill pattern in the off-diagonal blocks 14409e28618SBarry Smith 14509e28618SBarry Smith Level: developer 14609e28618SBarry Smith 147dce8aebaSBarry Smith Notes: 148dce8aebaSBarry Smith This only makes sense when you are doing multicomponent problems but using the 149dce8aebaSBarry Smith `MATMPIAIJ` matrix format 15009e28618SBarry Smith 15120f4b53cSBarry Smith The format for `dfill` and `ofill` is a sparse representation of a 15209e28618SBarry Smith dof-by-dof matrix with 1 entries representing coupling and 0 entries 15309e28618SBarry Smith for missing coupling. The sparse representation is a 1 dimensional 15409e28618SBarry Smith array of length nz + dof + 1, where nz is the number of non-zeros in 15509e28618SBarry Smith the matrix. The first dof entries in the array give the 15609e28618SBarry Smith starting array indices of each row's items in the rest of the array, 15760942847SBarry Smith the dof+1st item contains the value nz + dof + 1 (i.e. the entire length of the array) 15809e28618SBarry Smith and the remaining nz items give the column indices of each of 15909e28618SBarry Smith the 1s within the logical 2D matrix. Each row's items within 16009e28618SBarry Smith the array are the column indices of the 1s within that row 16109e28618SBarry Smith of the 2D matrix. PETSc developers may recognize that this is the 162dce8aebaSBarry Smith same format as that computed by the `DMDASetBlockFills_Private()` 16309e28618SBarry Smith function from a dense 2D matrix representation. 16409e28618SBarry Smith 165dce8aebaSBarry Smith `DMDASetGetMatrix()` allows you to provide general code for those more complicated nonzero patterns then 16620f4b53cSBarry Smith can be represented in the `dfill`, `ofill` format 16709e28618SBarry Smith 16809e28618SBarry Smith Contributed by Philip C. Roth 16909e28618SBarry Smith 170dce8aebaSBarry Smith .seealso: `DM`, `DMDA`, `DMDASetBlockFills()`, `DMCreateMatrix()`, `DMDASetGetMatrix()`, `DMSetMatrixPreallocateOnly()` 17109e28618SBarry Smith @*/ 172d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDASetBlockFillsSparse(DM da, const PetscInt *dfillsparse, const PetscInt *ofillsparse) 173d71ae5a4SJacob Faibussowitsch { 17409e28618SBarry Smith DM_DA *dd = (DM_DA *)da->data; 17509e28618SBarry Smith 17609e28618SBarry Smith PetscFunctionBegin; 17709e28618SBarry Smith /* save the given dfill and ofill information */ 1789566063dSJacob Faibussowitsch PetscCall(DMDASetBlockFillsSparse_Private(dfillsparse, dd->w, &dd->dfill)); 1799566063dSJacob Faibussowitsch PetscCall(DMDASetBlockFillsSparse_Private(ofillsparse, dd->w, &dd->ofill)); 18009e28618SBarry Smith 18109e28618SBarry Smith /* count nonzeros in ofill columns */ 1829566063dSJacob Faibussowitsch PetscCall(DMDASetBlockFills_Private2(dd)); 1833ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 18447c6ae99SBarry Smith } 18547c6ae99SBarry Smith 186d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA(DM da, ISColoringType ctype, ISColoring *coloring) 187d71ae5a4SJacob Faibussowitsch { 18847c6ae99SBarry Smith PetscInt dim, m, n, p, nc; 189bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by, bz; 19047c6ae99SBarry Smith MPI_Comm comm; 19147c6ae99SBarry Smith PetscMPIInt size; 19247c6ae99SBarry Smith PetscBool isBAIJ; 19347c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 19447c6ae99SBarry Smith 19547c6ae99SBarry Smith PetscFunctionBegin; 19647c6ae99SBarry Smith /* 19747c6ae99SBarry Smith m 19847c6ae99SBarry Smith ------------------------------------------------------ 19947c6ae99SBarry Smith | | 20047c6ae99SBarry Smith | | 20147c6ae99SBarry Smith | ---------------------- | 20247c6ae99SBarry Smith | | | | 20347c6ae99SBarry Smith n | yn | | | 20447c6ae99SBarry Smith | | | | 20547c6ae99SBarry Smith | .--------------------- | 20647c6ae99SBarry Smith | (xs,ys) xn | 20747c6ae99SBarry Smith | . | 20847c6ae99SBarry Smith | (gxs,gys) | 20947c6ae99SBarry Smith | | 21047c6ae99SBarry Smith ----------------------------------------------------- 21147c6ae99SBarry Smith */ 21247c6ae99SBarry Smith 21347c6ae99SBarry Smith /* 21447c6ae99SBarry Smith nc - number of components per grid point 21547c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 21647c6ae99SBarry Smith 21747c6ae99SBarry Smith */ 2189566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, NULL, NULL, NULL, &m, &n, &p, &nc, NULL, &bx, &by, &bz, NULL)); 21947c6ae99SBarry Smith 2209566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 2219566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(comm, &size)); 2225bdb020cSBarry Smith if (ctype == IS_COLORING_LOCAL) { 22347c6ae99SBarry Smith if (size == 1) { 22447c6ae99SBarry Smith ctype = IS_COLORING_GLOBAL; 2252ddab442SBarry Smith } else { 2262ddab442SBarry Smith PetscCheck((dim == 1) || !((m == 1 && bx == DM_BOUNDARY_PERIODIC) || (n == 1 && by == DM_BOUNDARY_PERIODIC) || (p == 1 && bz == DM_BOUNDARY_PERIODIC)), PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "IS_COLORING_LOCAL cannot be used for periodic boundary condition having both ends of the domain on the same process"); 22747c6ae99SBarry Smith } 22847c6ae99SBarry Smith } 22947c6ae99SBarry Smith 230aa219208SBarry Smith /* Tell the DMDA it has 1 degree of freedom per grid point so that the coloring for BAIJ 23147c6ae99SBarry Smith matrices is for the blocks, not the individual matrix elements */ 2329566063dSJacob Faibussowitsch PetscCall(PetscStrbeginswith(da->mattype, MATBAIJ, &isBAIJ)); 2339566063dSJacob Faibussowitsch if (!isBAIJ) PetscCall(PetscStrbeginswith(da->mattype, MATMPIBAIJ, &isBAIJ)); 2349566063dSJacob Faibussowitsch if (!isBAIJ) PetscCall(PetscStrbeginswith(da->mattype, MATSEQBAIJ, &isBAIJ)); 23547c6ae99SBarry Smith if (isBAIJ) { 23647c6ae99SBarry Smith dd->w = 1; 23747c6ae99SBarry Smith dd->xs = dd->xs / nc; 23847c6ae99SBarry Smith dd->xe = dd->xe / nc; 23947c6ae99SBarry Smith dd->Xs = dd->Xs / nc; 24047c6ae99SBarry Smith dd->Xe = dd->Xe / nc; 24147c6ae99SBarry Smith } 24247c6ae99SBarry Smith 24347c6ae99SBarry Smith /* 244aa219208SBarry Smith We do not provide a getcoloring function in the DMDA operations because 2459a1b256bSStefano Zampini the basic DMDA does not know about matrices. We think of DMDA as being 24647c6ae99SBarry Smith more low-level then matrices. 24747c6ae99SBarry Smith */ 2481baa6e33SBarry Smith if (dim == 1) PetscCall(DMCreateColoring_DA_1d_MPIAIJ(da, ctype, coloring)); 2491baa6e33SBarry Smith else if (dim == 2) PetscCall(DMCreateColoring_DA_2d_MPIAIJ(da, ctype, coloring)); 2501baa6e33SBarry Smith else if (dim == 3) PetscCall(DMCreateColoring_DA_3d_MPIAIJ(da, ctype, coloring)); 2511baa6e33SBarry Smith else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "Not done for %" PetscInt_FMT " dimension, send us mail petsc-maint@mcs.anl.gov for code", dim); 25247c6ae99SBarry Smith if (isBAIJ) { 25347c6ae99SBarry Smith dd->w = nc; 25447c6ae99SBarry Smith dd->xs = dd->xs * nc; 25547c6ae99SBarry Smith dd->xe = dd->xe * nc; 25647c6ae99SBarry Smith dd->Xs = dd->Xs * nc; 25747c6ae99SBarry Smith dd->Xe = dd->Xe * nc; 25847c6ae99SBarry Smith } 2593ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 26047c6ae99SBarry Smith } 26147c6ae99SBarry Smith 262d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA_2d_MPIAIJ(DM da, ISColoringType ctype, ISColoring *coloring) 263d71ae5a4SJacob Faibussowitsch { 26447c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, ii, gxs, gys, gnx, gny, m, n, M, N, dim, s, k, nc, col; 265dec0b466SHong Zhang PetscInt ncolors = 0; 26647c6ae99SBarry Smith MPI_Comm comm; 267bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 268aa219208SBarry Smith DMDAStencilType st; 26947c6ae99SBarry Smith ISColoringValue *colors; 27047c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 27147c6ae99SBarry Smith 27247c6ae99SBarry Smith PetscFunctionBegin; 27347c6ae99SBarry Smith /* 27447c6ae99SBarry Smith nc - number of components per grid point 27547c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 27647c6ae99SBarry Smith 27747c6ae99SBarry Smith */ 2789566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, &M, &N, NULL, &nc, &s, &bx, &by, NULL, &st)); 27947c6ae99SBarry Smith col = 2 * s + 1; 2809566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 2819566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 2829566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 28347c6ae99SBarry Smith 28447c6ae99SBarry Smith /* special case as taught to us by Paul Hovland */ 285aa219208SBarry Smith if (st == DMDA_STENCIL_STAR && s == 1) { 2869566063dSJacob Faibussowitsch PetscCall(DMCreateColoring_DA_2d_5pt_MPIAIJ(da, ctype, coloring)); 28747c6ae99SBarry Smith } else { 28847c6ae99SBarry Smith if (ctype == IS_COLORING_GLOBAL) { 28947c6ae99SBarry Smith if (!dd->localcoloring) { 2909566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * nx * ny, &colors)); 29147c6ae99SBarry Smith ii = 0; 29247c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 29347c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 294ad540459SPierre Jolivet for (k = 0; k < nc; k++) colors[ii++] = k + nc * ((i % col) + col * (j % col)); 29547c6ae99SBarry Smith } 29647c6ae99SBarry Smith } 29747c6ae99SBarry Smith ncolors = nc + nc * (col - 1 + col * (col - 1)); 2989566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * nx * ny, colors, PETSC_OWN_POINTER, &dd->localcoloring)); 29947c6ae99SBarry Smith } 30047c6ae99SBarry Smith *coloring = dd->localcoloring; 3015bdb020cSBarry Smith } else if (ctype == IS_COLORING_LOCAL) { 30247c6ae99SBarry Smith if (!dd->ghostedcoloring) { 3039566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * gnx * gny, &colors)); 30447c6ae99SBarry Smith ii = 0; 30547c6ae99SBarry Smith for (j = gys; j < gys + gny; j++) { 30647c6ae99SBarry Smith for (i = gxs; i < gxs + gnx; i++) { 30747c6ae99SBarry Smith for (k = 0; k < nc; k++) { 30847c6ae99SBarry Smith /* the complicated stuff is to handle periodic boundaries */ 30947c6ae99SBarry Smith colors[ii++] = k + nc * ((SetInRange(i, m) % col) + col * (SetInRange(j, n) % col)); 31047c6ae99SBarry Smith } 31147c6ae99SBarry Smith } 31247c6ae99SBarry Smith } 31347c6ae99SBarry Smith ncolors = nc + nc * (col - 1 + col * (col - 1)); 3149566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * gnx * gny, colors, PETSC_OWN_POINTER, &dd->ghostedcoloring)); 31547c6ae99SBarry Smith /* PetscIntView(ncolors,(PetscInt*)colors,0); */ 31647c6ae99SBarry Smith 3179566063dSJacob Faibussowitsch PetscCall(ISColoringSetType(dd->ghostedcoloring, IS_COLORING_LOCAL)); 31847c6ae99SBarry Smith } 31947c6ae99SBarry Smith *coloring = dd->ghostedcoloring; 32098921bdaSJacob Faibussowitsch } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_ARG_WRONG, "Unknown ISColoringType %d", (int)ctype); 32147c6ae99SBarry Smith } 3229566063dSJacob Faibussowitsch PetscCall(ISColoringReference(*coloring)); 3233ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 32447c6ae99SBarry Smith } 32547c6ae99SBarry Smith 326d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA_3d_MPIAIJ(DM da, ISColoringType ctype, ISColoring *coloring) 327d71ae5a4SJacob Faibussowitsch { 32847c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, gxs, gys, gnx, gny, m, n, p, dim, s, k, nc, col, zs, gzs, ii, l, nz, gnz, M, N, P; 32947c6ae99SBarry Smith PetscInt ncolors; 33047c6ae99SBarry Smith MPI_Comm comm; 331bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by, bz; 332aa219208SBarry Smith DMDAStencilType st; 33347c6ae99SBarry Smith ISColoringValue *colors; 33447c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 33547c6ae99SBarry Smith 33647c6ae99SBarry Smith PetscFunctionBegin; 33747c6ae99SBarry Smith /* 33847c6ae99SBarry Smith nc - number of components per grid point 33947c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 34047c6ae99SBarry Smith */ 3419566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &nc, &s, &bx, &by, &bz, &st)); 34247c6ae99SBarry Smith col = 2 * s + 1; 343494b1ccaSBarry Smith PetscCheck(bx != DM_BOUNDARY_PERIODIC || (m % col) == 0, PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "For coloring efficiency ensure number of grid points in X is divisible\n\ 344494b1ccaSBarry Smith by 2*stencil_width + 1\n"); 345494b1ccaSBarry Smith PetscCheck(by != DM_BOUNDARY_PERIODIC || (n % col) == 0, PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "For coloring efficiency ensure number of grid points in Y is divisible\n\ 346494b1ccaSBarry Smith by 2*stencil_width + 1\n"); 347494b1ccaSBarry Smith PetscCheck(bz != DM_BOUNDARY_PERIODIC || (p % col) == 0, PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "For coloring efficiency ensure number of grid points in Z is divisible\n\ 348494b1ccaSBarry Smith by 2*stencil_width + 1\n"); 349494b1ccaSBarry Smith 3509566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 3519566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 3529566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 35347c6ae99SBarry Smith 35447c6ae99SBarry Smith /* create the coloring */ 35547c6ae99SBarry Smith if (ctype == IS_COLORING_GLOBAL) { 35647c6ae99SBarry Smith if (!dd->localcoloring) { 3579566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * nx * ny * nz, &colors)); 35847c6ae99SBarry Smith ii = 0; 35947c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 36047c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 36147c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 362ad540459SPierre Jolivet for (l = 0; l < nc; l++) colors[ii++] = l + nc * ((i % col) + col * (j % col) + col * col * (k % col)); 36347c6ae99SBarry Smith } 36447c6ae99SBarry Smith } 36547c6ae99SBarry Smith } 36647c6ae99SBarry Smith ncolors = nc + nc * (col - 1 + col * (col - 1) + col * col * (col - 1)); 3679566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * nx * ny * nz, colors, PETSC_OWN_POINTER, &dd->localcoloring)); 36847c6ae99SBarry Smith } 36947c6ae99SBarry Smith *coloring = dd->localcoloring; 3705bdb020cSBarry Smith } else if (ctype == IS_COLORING_LOCAL) { 37147c6ae99SBarry Smith if (!dd->ghostedcoloring) { 3729566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * gnx * gny * gnz, &colors)); 37347c6ae99SBarry Smith ii = 0; 37447c6ae99SBarry Smith for (k = gzs; k < gzs + gnz; k++) { 37547c6ae99SBarry Smith for (j = gys; j < gys + gny; j++) { 37647c6ae99SBarry Smith for (i = gxs; i < gxs + gnx; i++) { 37747c6ae99SBarry Smith for (l = 0; l < nc; l++) { 37847c6ae99SBarry Smith /* the complicated stuff is to handle periodic boundaries */ 37947c6ae99SBarry Smith colors[ii++] = l + nc * ((SetInRange(i, m) % col) + col * (SetInRange(j, n) % col) + col * col * (SetInRange(k, p) % col)); 38047c6ae99SBarry Smith } 38147c6ae99SBarry Smith } 38247c6ae99SBarry Smith } 38347c6ae99SBarry Smith } 38447c6ae99SBarry Smith ncolors = nc + nc * (col - 1 + col * (col - 1) + col * col * (col - 1)); 3859566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * gnx * gny * gnz, colors, PETSC_OWN_POINTER, &dd->ghostedcoloring)); 3869566063dSJacob Faibussowitsch PetscCall(ISColoringSetType(dd->ghostedcoloring, IS_COLORING_LOCAL)); 38747c6ae99SBarry Smith } 38847c6ae99SBarry Smith *coloring = dd->ghostedcoloring; 38998921bdaSJacob Faibussowitsch } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_ARG_WRONG, "Unknown ISColoringType %d", (int)ctype); 3909566063dSJacob Faibussowitsch PetscCall(ISColoringReference(*coloring)); 3913ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 39247c6ae99SBarry Smith } 39347c6ae99SBarry Smith 394d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA_1d_MPIAIJ(DM da, ISColoringType ctype, ISColoring *coloring) 395d71ae5a4SJacob Faibussowitsch { 39647c6ae99SBarry Smith PetscInt xs, nx, i, i1, gxs, gnx, l, m, M, dim, s, nc, col; 39747c6ae99SBarry Smith PetscInt ncolors; 39847c6ae99SBarry Smith MPI_Comm comm; 399bff4a2f0SMatthew G. Knepley DMBoundaryType bx; 40047c6ae99SBarry Smith ISColoringValue *colors; 40147c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 40247c6ae99SBarry Smith 40347c6ae99SBarry Smith PetscFunctionBegin; 40447c6ae99SBarry Smith /* 40547c6ae99SBarry Smith nc - number of components per grid point 40647c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 40747c6ae99SBarry Smith */ 4089566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, NULL, NULL, &M, NULL, NULL, &nc, &s, &bx, NULL, NULL, NULL)); 40947c6ae99SBarry Smith col = 2 * s + 1; 4109566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &nx, NULL, NULL)); 4119566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, NULL, NULL, &gnx, NULL, NULL)); 4129566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 41347c6ae99SBarry Smith 41447c6ae99SBarry Smith /* create the coloring */ 41547c6ae99SBarry Smith if (ctype == IS_COLORING_GLOBAL) { 41647c6ae99SBarry Smith if (!dd->localcoloring) { 4179566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * nx, &colors)); 418ae4f298aSBarry Smith if (dd->ofillcols) { 419ae4f298aSBarry Smith PetscInt tc = 0; 420ae4f298aSBarry Smith for (i = 0; i < nc; i++) tc += (PetscInt)(dd->ofillcols[i] > 0); 421ae4f298aSBarry Smith i1 = 0; 422ae4f298aSBarry Smith for (i = xs; i < xs + nx; i++) { 423ae4f298aSBarry Smith for (l = 0; l < nc; l++) { 424ae4f298aSBarry Smith if (dd->ofillcols[l] && (i % col)) { 425ae4f298aSBarry Smith colors[i1++] = nc - 1 + tc * ((i % col) - 1) + dd->ofillcols[l]; 426ae4f298aSBarry Smith } else { 427ae4f298aSBarry Smith colors[i1++] = l; 428ae4f298aSBarry Smith } 429ae4f298aSBarry Smith } 430ae4f298aSBarry Smith } 431ae4f298aSBarry Smith ncolors = nc + 2 * s * tc; 432ae4f298aSBarry Smith } else { 43347c6ae99SBarry Smith i1 = 0; 43447c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 435ad540459SPierre Jolivet for (l = 0; l < nc; l++) colors[i1++] = l + nc * (i % col); 43647c6ae99SBarry Smith } 43747c6ae99SBarry Smith ncolors = nc + nc * (col - 1); 438ae4f298aSBarry Smith } 4399566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * nx, colors, PETSC_OWN_POINTER, &dd->localcoloring)); 44047c6ae99SBarry Smith } 44147c6ae99SBarry Smith *coloring = dd->localcoloring; 4425bdb020cSBarry Smith } else if (ctype == IS_COLORING_LOCAL) { 44347c6ae99SBarry Smith if (!dd->ghostedcoloring) { 4449566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * gnx, &colors)); 44547c6ae99SBarry Smith i1 = 0; 44647c6ae99SBarry Smith for (i = gxs; i < gxs + gnx; i++) { 44747c6ae99SBarry Smith for (l = 0; l < nc; l++) { 44847c6ae99SBarry Smith /* the complicated stuff is to handle periodic boundaries */ 44947c6ae99SBarry Smith colors[i1++] = l + nc * (SetInRange(i, m) % col); 45047c6ae99SBarry Smith } 45147c6ae99SBarry Smith } 45247c6ae99SBarry Smith ncolors = nc + nc * (col - 1); 4539566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * gnx, colors, PETSC_OWN_POINTER, &dd->ghostedcoloring)); 4549566063dSJacob Faibussowitsch PetscCall(ISColoringSetType(dd->ghostedcoloring, IS_COLORING_LOCAL)); 45547c6ae99SBarry Smith } 45647c6ae99SBarry Smith *coloring = dd->ghostedcoloring; 45798921bdaSJacob Faibussowitsch } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_ARG_WRONG, "Unknown ISColoringType %d", (int)ctype); 4589566063dSJacob Faibussowitsch PetscCall(ISColoringReference(*coloring)); 4593ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 46047c6ae99SBarry Smith } 46147c6ae99SBarry Smith 462d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA_2d_5pt_MPIAIJ(DM da, ISColoringType ctype, ISColoring *coloring) 463d71ae5a4SJacob Faibussowitsch { 46447c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, ii, gxs, gys, gnx, gny, m, n, dim, s, k, nc; 46547c6ae99SBarry Smith PetscInt ncolors; 46647c6ae99SBarry Smith MPI_Comm comm; 467bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 46847c6ae99SBarry Smith ISColoringValue *colors; 46947c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 47047c6ae99SBarry Smith 47147c6ae99SBarry Smith PetscFunctionBegin; 47247c6ae99SBarry Smith /* 47347c6ae99SBarry Smith nc - number of components per grid point 47447c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 47547c6ae99SBarry Smith */ 4769566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, NULL, NULL, NULL, &nc, &s, &bx, &by, NULL, NULL)); 4779566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 4789566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 4799566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 48047c6ae99SBarry Smith /* create the coloring */ 48147c6ae99SBarry Smith if (ctype == IS_COLORING_GLOBAL) { 48247c6ae99SBarry Smith if (!dd->localcoloring) { 4839566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * nx * ny, &colors)); 48447c6ae99SBarry Smith ii = 0; 48547c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 48647c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 487ad540459SPierre Jolivet for (k = 0; k < nc; k++) colors[ii++] = k + nc * ((3 * j + i) % 5); 48847c6ae99SBarry Smith } 48947c6ae99SBarry Smith } 49047c6ae99SBarry Smith ncolors = 5 * nc; 4919566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * nx * ny, colors, PETSC_OWN_POINTER, &dd->localcoloring)); 49247c6ae99SBarry Smith } 49347c6ae99SBarry Smith *coloring = dd->localcoloring; 4945bdb020cSBarry Smith } else if (ctype == IS_COLORING_LOCAL) { 49547c6ae99SBarry Smith if (!dd->ghostedcoloring) { 4969566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * gnx * gny, &colors)); 49747c6ae99SBarry Smith ii = 0; 49847c6ae99SBarry Smith for (j = gys; j < gys + gny; j++) { 49947c6ae99SBarry Smith for (i = gxs; i < gxs + gnx; i++) { 500ad540459SPierre Jolivet for (k = 0; k < nc; k++) colors[ii++] = k + nc * ((3 * SetInRange(j, n) + SetInRange(i, m)) % 5); 50147c6ae99SBarry Smith } 50247c6ae99SBarry Smith } 50347c6ae99SBarry Smith ncolors = 5 * nc; 5049566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * gnx * gny, colors, PETSC_OWN_POINTER, &dd->ghostedcoloring)); 5059566063dSJacob Faibussowitsch PetscCall(ISColoringSetType(dd->ghostedcoloring, IS_COLORING_LOCAL)); 50647c6ae99SBarry Smith } 50747c6ae99SBarry Smith *coloring = dd->ghostedcoloring; 50898921bdaSJacob Faibussowitsch } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_ARG_WRONG, "Unknown ISColoringType %d", (int)ctype); 5093ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 51047c6ae99SBarry Smith } 51147c6ae99SBarry Smith 51247c6ae99SBarry Smith /* =========================================================================== */ 513e432b41dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_1d_MPIAIJ(DM, Mat); 514ce308e1dSBarry Smith extern PetscErrorCode DMCreateMatrix_DA_1d_MPIAIJ_Fill(DM, Mat); 515e432b41dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_1d_SeqAIJ_NoPreallocation(DM, Mat); 516e432b41dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_2d_MPIAIJ(DM, Mat); 517950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_2d_MPIAIJ_Fill(DM, Mat); 518e432b41dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_3d_MPIAIJ(DM, Mat); 519950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_3d_MPIAIJ_Fill(DM, Mat); 520950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_2d_MPIBAIJ(DM, Mat); 521950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_3d_MPIBAIJ(DM, Mat); 522950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_2d_MPISBAIJ(DM, Mat); 523950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_3d_MPISBAIJ(DM, Mat); 524d4002b98SHong Zhang extern PetscErrorCode DMCreateMatrix_DA_2d_MPISELL(DM, Mat); 525d4002b98SHong Zhang extern PetscErrorCode DMCreateMatrix_DA_3d_MPISELL(DM, Mat); 526e584696dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_IS(DM, Mat); 52747c6ae99SBarry Smith 528a4e35b19SJacob Faibussowitsch static PetscErrorCode MatView_MPI_DA(Mat A, PetscViewer viewer) 529d71ae5a4SJacob Faibussowitsch { 5309a42bb27SBarry Smith DM da; 53147c6ae99SBarry Smith const char *prefix; 5322d1451d4SHong Zhang Mat AA, Anatural; 53347c6ae99SBarry Smith AO ao; 53447c6ae99SBarry Smith PetscInt rstart, rend, *petsc, i; 53547c6ae99SBarry Smith IS is; 53647c6ae99SBarry Smith MPI_Comm comm; 53774388724SJed Brown PetscViewerFormat format; 5382d1451d4SHong Zhang PetscBool flag; 53947c6ae99SBarry Smith 54047c6ae99SBarry Smith PetscFunctionBegin; 54174388724SJed Brown /* Check whether we are just printing info, in which case MatView() already viewed everything we wanted to view */ 5429566063dSJacob Faibussowitsch PetscCall(PetscViewerGetFormat(viewer, &format)); 5433ba16761SJacob Faibussowitsch if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) PetscFunctionReturn(PETSC_SUCCESS); 54474388724SJed Brown 5459566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)A, &comm)); 5469566063dSJacob Faibussowitsch PetscCall(MatGetDM(A, &da)); 5477a8be351SBarry Smith PetscCheck(da, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Matrix not generated from a DMDA"); 54847c6ae99SBarry Smith 5499566063dSJacob Faibussowitsch PetscCall(DMDAGetAO(da, &ao)); 5509566063dSJacob Faibussowitsch PetscCall(MatGetOwnershipRange(A, &rstart, &rend)); 5519566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(rend - rstart, &petsc)); 55247c6ae99SBarry Smith for (i = rstart; i < rend; i++) petsc[i - rstart] = i; 5539566063dSJacob Faibussowitsch PetscCall(AOApplicationToPetsc(ao, rend - rstart, petsc)); 5549566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(comm, rend - rstart, petsc, PETSC_OWN_POINTER, &is)); 55547c6ae99SBarry Smith 55647c6ae99SBarry Smith /* call viewer on natural ordering */ 5572d1451d4SHong Zhang PetscCall(PetscObjectBaseTypeCompare((PetscObject)A, MATMPISELL, &flag)); 5582d1451d4SHong Zhang if (flag) { 5592d1451d4SHong Zhang PetscCall(MatConvert(A, MATAIJ, MAT_INITIAL_MATRIX, &AA)); 5602d1451d4SHong Zhang A = AA; 5612d1451d4SHong Zhang } 5629566063dSJacob Faibussowitsch PetscCall(MatCreateSubMatrix(A, is, is, MAT_INITIAL_MATRIX, &Anatural)); 5639566063dSJacob Faibussowitsch PetscCall(ISDestroy(&is)); 5649566063dSJacob Faibussowitsch PetscCall(PetscObjectGetOptionsPrefix((PetscObject)A, &prefix)); 5659566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptionsPrefix((PetscObject)Anatural, prefix)); 5669566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)Anatural, ((PetscObject)A)->name)); 567f0ed2f47SStefano Zampini ((PetscObject)Anatural)->donotPetscObjectPrintClassNamePrefixType = PETSC_TRUE; 5689566063dSJacob Faibussowitsch PetscCall(MatView(Anatural, viewer)); 569f0ed2f47SStefano Zampini ((PetscObject)Anatural)->donotPetscObjectPrintClassNamePrefixType = PETSC_FALSE; 5709566063dSJacob Faibussowitsch PetscCall(MatDestroy(&Anatural)); 5712d1451d4SHong Zhang if (flag) PetscCall(MatDestroy(&AA)); 5723ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 57347c6ae99SBarry Smith } 57447c6ae99SBarry Smith 575a4e35b19SJacob Faibussowitsch static PetscErrorCode MatLoad_MPI_DA(Mat A, PetscViewer viewer) 576d71ae5a4SJacob Faibussowitsch { 5779a42bb27SBarry Smith DM da; 57847c6ae99SBarry Smith Mat Anatural, Aapp; 57947c6ae99SBarry Smith AO ao; 580539c167fSBarry Smith PetscInt rstart, rend, *app, i, m, n, M, N; 58147c6ae99SBarry Smith IS is; 58247c6ae99SBarry Smith MPI_Comm comm; 58347c6ae99SBarry Smith 58447c6ae99SBarry Smith PetscFunctionBegin; 5859566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)A, &comm)); 5869566063dSJacob Faibussowitsch PetscCall(MatGetDM(A, &da)); 5877a8be351SBarry Smith PetscCheck(da, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Matrix not generated from a DMDA"); 58847c6ae99SBarry Smith 58947c6ae99SBarry Smith /* Load the matrix in natural ordering */ 5909566063dSJacob Faibussowitsch PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &Anatural)); 5919566063dSJacob Faibussowitsch PetscCall(MatSetType(Anatural, ((PetscObject)A)->type_name)); 5929566063dSJacob Faibussowitsch PetscCall(MatGetSize(A, &M, &N)); 5939566063dSJacob Faibussowitsch PetscCall(MatGetLocalSize(A, &m, &n)); 5949566063dSJacob Faibussowitsch PetscCall(MatSetSizes(Anatural, m, n, M, N)); 5959566063dSJacob Faibussowitsch PetscCall(MatLoad(Anatural, viewer)); 59647c6ae99SBarry Smith 59747c6ae99SBarry Smith /* Map natural ordering to application ordering and create IS */ 5989566063dSJacob Faibussowitsch PetscCall(DMDAGetAO(da, &ao)); 5999566063dSJacob Faibussowitsch PetscCall(MatGetOwnershipRange(Anatural, &rstart, &rend)); 6009566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(rend - rstart, &app)); 60147c6ae99SBarry Smith for (i = rstart; i < rend; i++) app[i - rstart] = i; 6029566063dSJacob Faibussowitsch PetscCall(AOPetscToApplication(ao, rend - rstart, app)); 6039566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(comm, rend - rstart, app, PETSC_OWN_POINTER, &is)); 60447c6ae99SBarry Smith 60547c6ae99SBarry Smith /* Do permutation and replace header */ 6069566063dSJacob Faibussowitsch PetscCall(MatCreateSubMatrix(Anatural, is, is, MAT_INITIAL_MATRIX, &Aapp)); 6079566063dSJacob Faibussowitsch PetscCall(MatHeaderReplace(A, &Aapp)); 6089566063dSJacob Faibussowitsch PetscCall(ISDestroy(&is)); 6099566063dSJacob Faibussowitsch PetscCall(MatDestroy(&Anatural)); 6103ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 61147c6ae99SBarry Smith } 61247c6ae99SBarry Smith 613d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA(DM da, Mat *J) 614d71ae5a4SJacob Faibussowitsch { 61547c6ae99SBarry Smith PetscInt dim, dof, nx, ny, nz, dims[3], starts[3], M, N, P; 61647c6ae99SBarry Smith Mat A; 61747c6ae99SBarry Smith MPI_Comm comm; 61819fd82e9SBarry Smith MatType Atype; 619b412c318SBarry Smith MatType mtype; 62047c6ae99SBarry Smith PetscMPIInt size; 62147c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 622ade515a3SBarry Smith void (*aij)(void) = NULL, (*baij)(void) = NULL, (*sbaij)(void) = NULL, (*sell)(void) = NULL, (*is)(void) = NULL; 62347c6ae99SBarry Smith 62447c6ae99SBarry Smith PetscFunctionBegin; 6259566063dSJacob Faibussowitsch PetscCall(MatInitializePackage()); 626b412c318SBarry Smith mtype = da->mattype; 62747c6ae99SBarry Smith 62847c6ae99SBarry Smith /* 62947c6ae99SBarry Smith m 63047c6ae99SBarry Smith ------------------------------------------------------ 63147c6ae99SBarry Smith | | 63247c6ae99SBarry Smith | | 63347c6ae99SBarry Smith | ---------------------- | 63447c6ae99SBarry Smith | | | | 63547c6ae99SBarry Smith n | ny | | | 63647c6ae99SBarry Smith | | | | 63747c6ae99SBarry Smith | .--------------------- | 63847c6ae99SBarry Smith | (xs,ys) nx | 63947c6ae99SBarry Smith | . | 64047c6ae99SBarry Smith | (gxs,gys) | 64147c6ae99SBarry Smith | | 64247c6ae99SBarry Smith ----------------------------------------------------- 64347c6ae99SBarry Smith */ 64447c6ae99SBarry Smith 64547c6ae99SBarry Smith /* 64647c6ae99SBarry Smith nc - number of components per grid point 64747c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 64847c6ae99SBarry Smith */ 649e30e807fSPeter Brune M = dd->M; 650e30e807fSPeter Brune N = dd->N; 651e30e807fSPeter Brune P = dd->P; 652c73cfb54SMatthew G. Knepley dim = da->dim; 653e30e807fSPeter Brune dof = dd->w; 6549566063dSJacob Faibussowitsch /* PetscCall(DMDAGetInfo(da,&dim,&M,&N,&P,NULL,NULL,NULL,&dof,NULL,NULL,NULL,NULL,NULL)); */ 6559566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, NULL, NULL, NULL, &nx, &ny, &nz)); 6569566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 6579566063dSJacob Faibussowitsch PetscCall(MatCreate(comm, &A)); 6589566063dSJacob Faibussowitsch PetscCall(MatSetSizes(A, dof * nx * ny * nz, dof * nx * ny * nz, dof * M * N * P, dof * M * N * P)); 6599566063dSJacob Faibussowitsch PetscCall(MatSetType(A, mtype)); 6609566063dSJacob Faibussowitsch PetscCall(MatSetFromOptions(A)); 66174427ab1SRichard Tran Mills if (dof * nx * ny * nz < da->bind_below) { 6629566063dSJacob Faibussowitsch PetscCall(MatSetBindingPropagates(A, PETSC_TRUE)); 6639566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(A, PETSC_TRUE)); 66474427ab1SRichard Tran Mills } 6659566063dSJacob Faibussowitsch PetscCall(MatSetDM(A, da)); 6661baa6e33SBarry Smith if (da->structure_only) PetscCall(MatSetOption(A, MAT_STRUCTURE_ONLY, PETSC_TRUE)); 6679566063dSJacob Faibussowitsch PetscCall(MatGetType(A, &Atype)); 66847c6ae99SBarry Smith /* 669aa219208SBarry Smith We do not provide a getmatrix function in the DMDA operations because 670aa219208SBarry Smith the basic DMDA does not know about matrices. We think of DMDA as being more 67147c6ae99SBarry Smith more low-level than matrices. This is kind of cheating but, cause sometimes 672aa219208SBarry Smith we think of DMDA has higher level than matrices. 67347c6ae99SBarry Smith 67447c6ae99SBarry Smith We could switch based on Atype (or mtype), but we do not since the 675844bd0d7SStefano Zampini specialized setting routines depend only on the particular preallocation 67647c6ae99SBarry Smith details of the matrix, not the type itself. 67747c6ae99SBarry Smith */ 678*d7dabc60SJed Brown if (!da->prealloc_skip) { // Flag is likely set when user intends to use MatSetPreallocationCOO() 6799566063dSJacob Faibussowitsch PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatMPIAIJSetPreallocation_C", &aij)); 68048a46eb9SPierre Jolivet if (!aij) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatSeqAIJSetPreallocation_C", &aij)); 68147c6ae99SBarry Smith if (!aij) { 6829566063dSJacob Faibussowitsch PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatMPIBAIJSetPreallocation_C", &baij)); 68348a46eb9SPierre Jolivet if (!baij) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatSeqBAIJSetPreallocation_C", &baij)); 68447c6ae99SBarry Smith if (!baij) { 6859566063dSJacob Faibussowitsch PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatMPISBAIJSetPreallocation_C", &sbaij)); 68648a46eb9SPierre Jolivet if (!sbaij) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatSeqSBAIJSetPreallocation_C", &sbaij)); 6875e26d47bSHong Zhang if (!sbaij) { 6889566063dSJacob Faibussowitsch PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatMPISELLSetPreallocation_C", &sell)); 68948a46eb9SPierre Jolivet if (!sell) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatSeqSELLSetPreallocation_C", &sell)); 6905e26d47bSHong Zhang } 69148a46eb9SPierre Jolivet if (!sell) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatISSetPreallocation_C", &is)); 69247c6ae99SBarry Smith } 69347c6ae99SBarry Smith } 694*d7dabc60SJed Brown } 69547c6ae99SBarry Smith if (aij) { 69647c6ae99SBarry Smith if (dim == 1) { 697ce308e1dSBarry Smith if (dd->ofill) { 6989566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_1d_MPIAIJ_Fill(da, A)); 699ce308e1dSBarry Smith } else { 70019b08ed1SBarry Smith DMBoundaryType bx; 70119b08ed1SBarry Smith PetscMPIInt size; 7029566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &bx, NULL, NULL, NULL)); 7039566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)da), &size)); 70419b08ed1SBarry Smith if (size == 1 && bx == DM_BOUNDARY_NONE) { 7059566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_1d_SeqAIJ_NoPreallocation(da, A)); 70619b08ed1SBarry Smith } else { 7079566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_1d_MPIAIJ(da, A)); 708ce308e1dSBarry Smith } 70919b08ed1SBarry Smith } 71047c6ae99SBarry Smith } else if (dim == 2) { 71147c6ae99SBarry Smith if (dd->ofill) { 7129566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_2d_MPIAIJ_Fill(da, A)); 71347c6ae99SBarry Smith } else { 7149566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_2d_MPIAIJ(da, A)); 71547c6ae99SBarry Smith } 71647c6ae99SBarry Smith } else if (dim == 3) { 71747c6ae99SBarry Smith if (dd->ofill) { 7189566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_3d_MPIAIJ_Fill(da, A)); 71947c6ae99SBarry Smith } else { 7209566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_3d_MPIAIJ(da, A)); 72147c6ae99SBarry Smith } 72247c6ae99SBarry Smith } 72347c6ae99SBarry Smith } else if (baij) { 72447c6ae99SBarry Smith if (dim == 2) { 7259566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_2d_MPIBAIJ(da, A)); 72647c6ae99SBarry Smith } else if (dim == 3) { 7279566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_3d_MPIBAIJ(da, A)); 72863a3b9bcSJacob 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); 72947c6ae99SBarry Smith } else if (sbaij) { 73047c6ae99SBarry Smith if (dim == 2) { 7319566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_2d_MPISBAIJ(da, A)); 73247c6ae99SBarry Smith } else if (dim == 3) { 7339566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_3d_MPISBAIJ(da, A)); 73463a3b9bcSJacob 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); 735d4002b98SHong Zhang } else if (sell) { 7365e26d47bSHong Zhang if (dim == 2) { 7379566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_2d_MPISELL(da, A)); 738711261dbSHong Zhang } else if (dim == 3) { 7399566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_3d_MPISELL(da, A)); 74063a3b9bcSJacob 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); 741e584696dSStefano Zampini } else if (is) { 7429566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_IS(da, A)); 743*d7dabc60SJed Brown } else { // unknown type or da->prealloc_skip so structural information may be needed, but no prealloc 74445b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 745e584696dSStefano Zampini 7469566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(A, dof)); 7479566063dSJacob Faibussowitsch PetscCall(MatSetUp(A)); 7489566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 7499566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(A, ltog, ltog)); 75047c6ae99SBarry Smith } 7519566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &starts[0], &starts[1], &starts[2], &dims[0], &dims[1], &dims[2])); 7529566063dSJacob Faibussowitsch PetscCall(MatSetStencil(A, dim, dims, starts, dof)); 7539566063dSJacob Faibussowitsch PetscCall(MatSetDM(A, da)); 7549566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(comm, &size)); 75547c6ae99SBarry Smith if (size > 1) { 75647c6ae99SBarry Smith /* change viewer to display matrix in natural ordering */ 7579566063dSJacob Faibussowitsch PetscCall(MatSetOperation(A, MATOP_VIEW, (void (*)(void))MatView_MPI_DA)); 7589566063dSJacob Faibussowitsch PetscCall(MatSetOperation(A, MATOP_LOAD, (void (*)(void))MatLoad_MPI_DA)); 75947c6ae99SBarry Smith } 76047c6ae99SBarry Smith *J = A; 7613ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 76247c6ae99SBarry Smith } 76347c6ae99SBarry Smith 764844bd0d7SStefano Zampini PETSC_EXTERN PetscErrorCode MatISSetPreallocation_IS(Mat, PetscInt, const PetscInt[], PetscInt, const PetscInt[]); 765844bd0d7SStefano Zampini 766d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_IS(DM dm, Mat J) 767d71ae5a4SJacob Faibussowitsch { 768e584696dSStefano Zampini DM_DA *da = (DM_DA *)dm->data; 769e432b41dSStefano Zampini Mat lJ, P; 770e584696dSStefano Zampini ISLocalToGlobalMapping ltog; 771e432b41dSStefano Zampini IS is; 772e432b41dSStefano Zampini PetscBT bt; 77305339c03SStefano Zampini const PetscInt *e_loc, *idx; 774e432b41dSStefano Zampini PetscInt i, nel, nen, nv, dof, *gidx, n, N; 775e584696dSStefano Zampini 776e584696dSStefano Zampini /* The l2g map of DMDA has all ghosted nodes, and e_loc is a subset of all the local nodes (including the ghosted) 777e432b41dSStefano Zampini We need to filter out the local indices that are not represented through the DMDAGetElements decomposition */ 778e584696dSStefano Zampini PetscFunctionBegin; 779e584696dSStefano Zampini dof = da->w; 7809566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, dof)); 7819566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(dm, <og)); 782e432b41dSStefano Zampini 783e432b41dSStefano Zampini /* flag local elements indices in local DMDA numbering */ 7849566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingGetSize(ltog, &nv)); 7859566063dSJacob Faibussowitsch PetscCall(PetscBTCreate(nv / dof, &bt)); 7869566063dSJacob Faibussowitsch PetscCall(DMDAGetElements(dm, &nel, &nen, &e_loc)); /* this will throw an error if the stencil type is not DMDA_STENCIL_BOX */ 7879566063dSJacob Faibussowitsch for (i = 0; i < nel * nen; i++) PetscCall(PetscBTSet(bt, e_loc[i])); 788e432b41dSStefano Zampini 789e432b41dSStefano Zampini /* filter out (set to -1) the global indices not used by the local elements */ 7909566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nv / dof, &gidx)); 7919566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingGetBlockIndices(ltog, &idx)); 7929566063dSJacob Faibussowitsch PetscCall(PetscArraycpy(gidx, idx, nv / dof)); 7939566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingRestoreBlockIndices(ltog, &idx)); 7949371c9d4SSatish Balay for (i = 0; i < nv / dof; i++) 7959371c9d4SSatish Balay if (!PetscBTLookup(bt, i)) gidx[i] = -1; 7969566063dSJacob Faibussowitsch PetscCall(PetscBTDestroy(&bt)); 7979566063dSJacob Faibussowitsch PetscCall(ISCreateBlock(PetscObjectComm((PetscObject)dm), dof, nv / dof, gidx, PETSC_OWN_POINTER, &is)); 7989566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingCreateIS(is, <og)); 7999566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 8009566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingDestroy(<og)); 8019566063dSJacob Faibussowitsch PetscCall(ISDestroy(&is)); 80205339c03SStefano Zampini 803e432b41dSStefano Zampini /* Preallocation */ 804e306f867SJed Brown if (dm->prealloc_skip) { 8059566063dSJacob Faibussowitsch PetscCall(MatSetUp(J)); 806e306f867SJed Brown } else { 8079566063dSJacob Faibussowitsch PetscCall(MatISGetLocalMat(J, &lJ)); 8089566063dSJacob Faibussowitsch PetscCall(MatGetLocalToGlobalMapping(lJ, <og, NULL)); 8099566063dSJacob Faibussowitsch PetscCall(MatCreate(PetscObjectComm((PetscObject)lJ), &P)); 8109566063dSJacob Faibussowitsch PetscCall(MatSetType(P, MATPREALLOCATOR)); 8119566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(P, ltog, ltog)); 8129566063dSJacob Faibussowitsch PetscCall(MatGetSize(lJ, &N, NULL)); 8139566063dSJacob Faibussowitsch PetscCall(MatGetLocalSize(lJ, &n, NULL)); 8149566063dSJacob Faibussowitsch PetscCall(MatSetSizes(P, n, n, N, N)); 8159566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(P, dof)); 8169566063dSJacob Faibussowitsch PetscCall(MatSetUp(P)); 81748a46eb9SPierre Jolivet for (i = 0; i < nel; i++) PetscCall(MatSetValuesBlockedLocal(P, nen, e_loc + i * nen, nen, e_loc + i * nen, NULL, INSERT_VALUES)); 8189566063dSJacob Faibussowitsch PetscCall(MatPreallocatorPreallocate(P, (PetscBool)!da->prealloc_only, lJ)); 8199566063dSJacob Faibussowitsch PetscCall(MatISRestoreLocalMat(J, &lJ)); 8209566063dSJacob Faibussowitsch PetscCall(DMDARestoreElements(dm, &nel, &nen, &e_loc)); 8219566063dSJacob Faibussowitsch PetscCall(MatDestroy(&P)); 822e432b41dSStefano Zampini 8239566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 8249566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 825e306f867SJed Brown } 8263ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 827e584696dSStefano Zampini } 828e584696dSStefano Zampini 829d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPISELL(DM da, Mat J) 830d71ae5a4SJacob Faibussowitsch { 8315e26d47bSHong 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; 8325e26d47bSHong Zhang PetscInt lstart, lend, pstart, pend, *dnz, *onz; 8335e26d47bSHong Zhang MPI_Comm comm; 8345e26d47bSHong Zhang PetscScalar *values; 8355e26d47bSHong Zhang DMBoundaryType bx, by; 8365e26d47bSHong Zhang ISLocalToGlobalMapping ltog; 8375e26d47bSHong Zhang DMDAStencilType st; 8385e26d47bSHong Zhang 8395e26d47bSHong Zhang PetscFunctionBegin; 8405e26d47bSHong Zhang /* 8415e26d47bSHong Zhang nc - number of components per grid point 8425e26d47bSHong Zhang col - number of colors needed in one direction for single component problem 8435e26d47bSHong Zhang */ 8449566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, NULL, NULL, NULL, &nc, &s, &bx, &by, NULL, &st)); 8455e26d47bSHong Zhang col = 2 * s + 1; 8469566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 8479566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 8489566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 8495e26d47bSHong Zhang 8509566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * col * nc * nc, &cols)); 8519566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 8525e26d47bSHong Zhang 8539566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 8545e26d47bSHong Zhang /* determine the matrix preallocation information */ 855d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny, nc * nx * ny, dnz, onz); 8565e26d47bSHong Zhang for (i = xs; i < xs + nx; i++) { 8575e26d47bSHong Zhang pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 8585e26d47bSHong Zhang pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 8595e26d47bSHong Zhang 8605e26d47bSHong Zhang for (j = ys; j < ys + ny; j++) { 8615e26d47bSHong Zhang slot = i - gxs + gnx * (j - gys); 8625e26d47bSHong Zhang 8635e26d47bSHong Zhang lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 8645e26d47bSHong Zhang lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 8655e26d47bSHong Zhang 8665e26d47bSHong Zhang cnt = 0; 8675e26d47bSHong Zhang for (k = 0; k < nc; k++) { 8685e26d47bSHong Zhang for (l = lstart; l < lend + 1; l++) { 8695e26d47bSHong Zhang for (p = pstart; p < pend + 1; p++) { 8705e26d47bSHong Zhang if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star have either l = 0 or p = 0 */ 8715e26d47bSHong Zhang cols[cnt++] = k + nc * (slot + gnx * l + p); 8725e26d47bSHong Zhang } 8735e26d47bSHong Zhang } 8745e26d47bSHong Zhang } 8755e26d47bSHong Zhang rows[k] = k + nc * (slot); 8765e26d47bSHong Zhang } 8779566063dSJacob Faibussowitsch PetscCall(MatPreallocateSetLocal(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 8785e26d47bSHong Zhang } 8795e26d47bSHong Zhang } 8809566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 8819566063dSJacob Faibussowitsch PetscCall(MatSeqSELLSetPreallocation(J, 0, dnz)); 8829566063dSJacob Faibussowitsch PetscCall(MatMPISELLSetPreallocation(J, 0, dnz, 0, onz)); 883d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 8845e26d47bSHong Zhang 8859566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 8865e26d47bSHong Zhang 8875e26d47bSHong Zhang /* 8885e26d47bSHong Zhang For each node in the grid: we get the neighbors in the local (on processor ordering 8895e26d47bSHong Zhang that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 8905e26d47bSHong Zhang PETSc ordering. 8915e26d47bSHong Zhang */ 8925e26d47bSHong Zhang if (!da->prealloc_only) { 8939566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * nc * nc, &values)); 8945e26d47bSHong Zhang for (i = xs; i < xs + nx; i++) { 8955e26d47bSHong Zhang pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 8965e26d47bSHong Zhang pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 8975e26d47bSHong Zhang 8985e26d47bSHong Zhang for (j = ys; j < ys + ny; j++) { 8995e26d47bSHong Zhang slot = i - gxs + gnx * (j - gys); 9005e26d47bSHong Zhang 9015e26d47bSHong Zhang lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 9025e26d47bSHong Zhang lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 9035e26d47bSHong Zhang 9045e26d47bSHong Zhang cnt = 0; 9055e26d47bSHong Zhang for (k = 0; k < nc; k++) { 9065e26d47bSHong Zhang for (l = lstart; l < lend + 1; l++) { 9075e26d47bSHong Zhang for (p = pstart; p < pend + 1; p++) { 9085e26d47bSHong Zhang if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star have either l = 0 or p = 0 */ 9095e26d47bSHong Zhang cols[cnt++] = k + nc * (slot + gnx * l + p); 9105e26d47bSHong Zhang } 9115e26d47bSHong Zhang } 9125e26d47bSHong Zhang } 9135e26d47bSHong Zhang rows[k] = k + nc * (slot); 9145e26d47bSHong Zhang } 9159566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, values, INSERT_VALUES)); 9165e26d47bSHong Zhang } 9175e26d47bSHong Zhang } 9189566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 919e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 9209566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 9219566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 9229566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 9239566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 9249566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 9255e26d47bSHong Zhang } 9269566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 9273ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 9285e26d47bSHong Zhang } 9295e26d47bSHong Zhang 930d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPISELL(DM da, Mat J) 931d71ae5a4SJacob Faibussowitsch { 932711261dbSHong Zhang PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 933711261dbSHong Zhang PetscInt m, n, dim, s, *cols = NULL, k, nc, *rows = NULL, col, cnt, l, p, *dnz = NULL, *onz = NULL; 934711261dbSHong Zhang PetscInt istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk, M, N, P; 935711261dbSHong Zhang MPI_Comm comm; 936711261dbSHong Zhang PetscScalar *values; 937711261dbSHong Zhang DMBoundaryType bx, by, bz; 938711261dbSHong Zhang ISLocalToGlobalMapping ltog; 939711261dbSHong Zhang DMDAStencilType st; 940711261dbSHong Zhang 941711261dbSHong Zhang PetscFunctionBegin; 942711261dbSHong Zhang /* 943711261dbSHong Zhang nc - number of components per grid point 944711261dbSHong Zhang col - number of colors needed in one direction for single component problem 945711261dbSHong Zhang */ 9469566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &nc, &s, &bx, &by, &bz, &st)); 947711261dbSHong Zhang col = 2 * s + 1; 9489566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 9499566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 9509566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 951711261dbSHong Zhang 9529566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * col * col * nc * nc, &cols)); 9539566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 954711261dbSHong Zhang 9559566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 956711261dbSHong Zhang /* determine the matrix preallocation information */ 957d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny * nz, nc * nx * ny * nz, dnz, onz); 958711261dbSHong Zhang for (i = xs; i < xs + nx; i++) { 959711261dbSHong Zhang istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 960711261dbSHong Zhang iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 961711261dbSHong Zhang for (j = ys; j < ys + ny; j++) { 962711261dbSHong Zhang jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 963711261dbSHong Zhang jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 964711261dbSHong Zhang for (k = zs; k < zs + nz; k++) { 965711261dbSHong Zhang kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 966711261dbSHong Zhang kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 967711261dbSHong Zhang 968711261dbSHong Zhang slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 969711261dbSHong Zhang 970711261dbSHong Zhang cnt = 0; 971711261dbSHong Zhang for (l = 0; l < nc; l++) { 972711261dbSHong Zhang for (ii = istart; ii < iend + 1; ii++) { 973711261dbSHong Zhang for (jj = jstart; jj < jend + 1; jj++) { 974711261dbSHong Zhang for (kk = kstart; kk < kend + 1; kk++) { 975711261dbSHong Zhang if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 976711261dbSHong Zhang cols[cnt++] = l + nc * (slot + ii + gnx * jj + gnx * gny * kk); 977711261dbSHong Zhang } 978711261dbSHong Zhang } 979711261dbSHong Zhang } 980711261dbSHong Zhang } 981711261dbSHong Zhang rows[l] = l + nc * (slot); 982711261dbSHong Zhang } 9839566063dSJacob Faibussowitsch PetscCall(MatPreallocateSetLocal(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 984711261dbSHong Zhang } 985711261dbSHong Zhang } 986711261dbSHong Zhang } 9879566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 9889566063dSJacob Faibussowitsch PetscCall(MatSeqSELLSetPreallocation(J, 0, dnz)); 9899566063dSJacob Faibussowitsch PetscCall(MatMPISELLSetPreallocation(J, 0, dnz, 0, onz)); 990d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 9919566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 992711261dbSHong Zhang 993711261dbSHong Zhang /* 994711261dbSHong Zhang For each node in the grid: we get the neighbors in the local (on processor ordering 995711261dbSHong Zhang that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 996711261dbSHong Zhang PETSc ordering. 997711261dbSHong Zhang */ 998711261dbSHong Zhang if (!da->prealloc_only) { 9999566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * col * nc * nc * nc, &values)); 1000711261dbSHong Zhang for (i = xs; i < xs + nx; i++) { 1001711261dbSHong Zhang istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1002711261dbSHong Zhang iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 1003711261dbSHong Zhang for (j = ys; j < ys + ny; j++) { 1004711261dbSHong Zhang jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1005711261dbSHong Zhang jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 1006711261dbSHong Zhang for (k = zs; k < zs + nz; k++) { 1007711261dbSHong Zhang kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 1008711261dbSHong Zhang kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 1009711261dbSHong Zhang 1010711261dbSHong Zhang slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 1011711261dbSHong Zhang 1012711261dbSHong Zhang cnt = 0; 1013711261dbSHong Zhang for (l = 0; l < nc; l++) { 1014711261dbSHong Zhang for (ii = istart; ii < iend + 1; ii++) { 1015711261dbSHong Zhang for (jj = jstart; jj < jend + 1; jj++) { 1016711261dbSHong Zhang for (kk = kstart; kk < kend + 1; kk++) { 1017711261dbSHong Zhang if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 1018711261dbSHong Zhang cols[cnt++] = l + nc * (slot + ii + gnx * jj + gnx * gny * kk); 1019711261dbSHong Zhang } 1020711261dbSHong Zhang } 1021711261dbSHong Zhang } 1022711261dbSHong Zhang } 1023711261dbSHong Zhang rows[l] = l + nc * (slot); 1024711261dbSHong Zhang } 10259566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, values, INSERT_VALUES)); 1026711261dbSHong Zhang } 1027711261dbSHong Zhang } 1028711261dbSHong Zhang } 10299566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 1030e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 10319566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 10329566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 10339566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 10349566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 10359566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 1036711261dbSHong Zhang } 10379566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 10383ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1039711261dbSHong Zhang } 1040711261dbSHong Zhang 1041d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPIAIJ(DM da, Mat J) 1042d71ae5a4SJacob Faibussowitsch { 1043c1154cd5SBarry 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; 104447c6ae99SBarry Smith PetscInt lstart, lend, pstart, pend, *dnz, *onz; 104547c6ae99SBarry Smith MPI_Comm comm; 1046bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 1047844bd0d7SStefano Zampini ISLocalToGlobalMapping ltog, mltog; 1048aa219208SBarry Smith DMDAStencilType st; 1049b294de21SRichard Tran Mills PetscBool removedups = PETSC_FALSE, alreadyboundtocpu = PETSC_TRUE; 105047c6ae99SBarry Smith 105147c6ae99SBarry Smith PetscFunctionBegin; 105247c6ae99SBarry Smith /* 105347c6ae99SBarry Smith nc - number of components per grid point 105447c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 105547c6ae99SBarry Smith */ 1056924e958dSJunchao Zhang PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, &M, &N, NULL, &nc, &s, &bx, &by, NULL, &st)); 10571baa6e33SBarry Smith if (bx == DM_BOUNDARY_NONE && by == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_TRUE)); 105847c6ae99SBarry Smith col = 2 * s + 1; 1059c1154cd5SBarry Smith /* 1060c1154cd5SBarry Smith With one processor in periodic domains in a skinny dimension the code will label nonzero columns multiple times 1061c1154cd5SBarry Smith because of "wrapping" around the end of the domain hitting an entry already counted in the other direction. 1062c1154cd5SBarry Smith */ 1063c1154cd5SBarry Smith if (M == 1 && 2 * s >= m) removedups = PETSC_TRUE; 1064c1154cd5SBarry Smith if (N == 1 && 2 * s >= n) removedups = PETSC_TRUE; 10659566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 10669566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 10679566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 106847c6ae99SBarry Smith 10699566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * col * nc * nc, &cols)); 10709566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 107147c6ae99SBarry Smith 10729566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 107347c6ae99SBarry Smith /* determine the matrix preallocation information */ 1074d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny, nc * nx * ny, dnz, onz); 107547c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1076bff4a2f0SMatthew G. Knepley pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1077bff4a2f0SMatthew G. Knepley pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 107847c6ae99SBarry Smith 107947c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 108047c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 108147c6ae99SBarry Smith 1082bff4a2f0SMatthew G. Knepley lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1083bff4a2f0SMatthew G. Knepley lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 108447c6ae99SBarry Smith 108547c6ae99SBarry Smith cnt = 0; 108647c6ae99SBarry Smith for (k = 0; k < nc; k++) { 108747c6ae99SBarry Smith for (l = lstart; l < lend + 1; l++) { 108847c6ae99SBarry Smith for (p = pstart; p < pend + 1; p++) { 1089aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star have either l = 0 or p = 0 */ 109047c6ae99SBarry Smith cols[cnt++] = k + nc * (slot + gnx * l + p); 109147c6ae99SBarry Smith } 109247c6ae99SBarry Smith } 109347c6ae99SBarry Smith } 109447c6ae99SBarry Smith rows[k] = k + nc * (slot); 109547c6ae99SBarry Smith } 10961baa6e33SBarry Smith if (removedups) PetscCall(MatPreallocateSetLocalRemoveDups(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 10971baa6e33SBarry Smith else PetscCall(MatPreallocateSetLocal(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 109847c6ae99SBarry Smith } 1099c1154cd5SBarry Smith } 11009566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 11019566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz)); 11029566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz)); 1103d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 11049566063dSJacob Faibussowitsch PetscCall(MatGetLocalToGlobalMapping(J, &mltog, NULL)); 11051baa6e33SBarry Smith if (!mltog) PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 110647c6ae99SBarry Smith 110747c6ae99SBarry Smith /* 110847c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 110947c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 111047c6ae99SBarry Smith PETSc ordering. 111147c6ae99SBarry Smith */ 1112fcfd50ebSBarry Smith if (!da->prealloc_only) { 111347c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1114bff4a2f0SMatthew G. Knepley pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1115bff4a2f0SMatthew G. Knepley pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 111647c6ae99SBarry Smith 111747c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 111847c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 111947c6ae99SBarry Smith 1120bff4a2f0SMatthew G. Knepley lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1121bff4a2f0SMatthew G. Knepley lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 112247c6ae99SBarry Smith 112347c6ae99SBarry Smith cnt = 0; 112447c6ae99SBarry Smith for (l = lstart; l < lend + 1; l++) { 112547c6ae99SBarry Smith for (p = pstart; p < pend + 1; p++) { 1126aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star have either l = 0 or p = 0 */ 1127071fcb05SBarry Smith cols[cnt++] = nc * (slot + gnx * l + p); 1128071fcb05SBarry Smith for (k = 1; k < nc; k++) { 11299371c9d4SSatish Balay cols[cnt] = 1 + cols[cnt - 1]; 11309371c9d4SSatish Balay cnt++; 113147c6ae99SBarry Smith } 113247c6ae99SBarry Smith } 113347c6ae99SBarry Smith } 113447c6ae99SBarry Smith } 1135071fcb05SBarry Smith for (k = 0; k < nc; k++) rows[k] = k + nc * (slot); 11369566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, NULL, INSERT_VALUES)); 113747c6ae99SBarry Smith } 113847c6ae99SBarry Smith } 1139e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 11409566063dSJacob Faibussowitsch PetscCall(MatBoundToCPU(J, &alreadyboundtocpu)); 11419566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 11429566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 11439566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 11449566063dSJacob Faibussowitsch if (!alreadyboundtocpu) PetscCall(MatBindToCPU(J, PETSC_FALSE)); 11459566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 11461baa6e33SBarry Smith if (bx == DM_BOUNDARY_NONE && by == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE)); 114747c6ae99SBarry Smith } 11489566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 11493ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 115047c6ae99SBarry Smith } 115147c6ae99SBarry Smith 1152d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPIAIJ_Fill(DM da, Mat J) 1153d71ae5a4SJacob Faibussowitsch { 115447c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 1155c1154cd5SBarry Smith PetscInt m, n, dim, s, *cols, k, nc, row, col, cnt, maxcnt = 0, l, p, M, N; 115647c6ae99SBarry Smith PetscInt lstart, lend, pstart, pend, *dnz, *onz; 115747c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 115847c6ae99SBarry Smith PetscInt ifill_col, *ofill = dd->ofill, *dfill = dd->dfill; 115947c6ae99SBarry Smith MPI_Comm comm; 1160bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 116145b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 1162aa219208SBarry Smith DMDAStencilType st; 1163c1154cd5SBarry Smith PetscBool removedups = PETSC_FALSE; 116447c6ae99SBarry Smith 116547c6ae99SBarry Smith PetscFunctionBegin; 116647c6ae99SBarry Smith /* 116747c6ae99SBarry Smith nc - number of components per grid point 116847c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 116947c6ae99SBarry Smith */ 1170924e958dSJunchao Zhang PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, &M, &N, NULL, &nc, &s, &bx, &by, NULL, &st)); 117147c6ae99SBarry Smith col = 2 * s + 1; 1172c1154cd5SBarry Smith /* 1173c1154cd5SBarry Smith With one processor in periodic domains in a skinny dimension the code will label nonzero columns multiple times 1174c1154cd5SBarry Smith because of "wrapping" around the end of the domain hitting an entry already counted in the other direction. 1175c1154cd5SBarry Smith */ 1176c1154cd5SBarry Smith if (M == 1 && 2 * s >= m) removedups = PETSC_TRUE; 1177c1154cd5SBarry Smith if (N == 1 && 2 * s >= n) removedups = PETSC_TRUE; 11789566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 11799566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 11809566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 118147c6ae99SBarry Smith 11829566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * nc, &cols)); 11839566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 118447c6ae99SBarry Smith 11859566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 118647c6ae99SBarry Smith /* determine the matrix preallocation information */ 1187d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny, nc * nx * ny, dnz, onz); 118847c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1189bff4a2f0SMatthew G. Knepley pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1190bff4a2f0SMatthew G. Knepley pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 119147c6ae99SBarry Smith 119247c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 119347c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 119447c6ae99SBarry Smith 1195bff4a2f0SMatthew G. Knepley lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1196bff4a2f0SMatthew G. Knepley lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 119747c6ae99SBarry Smith 119847c6ae99SBarry Smith for (k = 0; k < nc; k++) { 119947c6ae99SBarry Smith cnt = 0; 120047c6ae99SBarry Smith for (l = lstart; l < lend + 1; l++) { 120147c6ae99SBarry Smith for (p = pstart; p < pend + 1; p++) { 120247c6ae99SBarry Smith if (l || p) { 1203aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star */ 12048865f1eaSKarl Rupp for (ifill_col = ofill[k]; ifill_col < ofill[k + 1]; ifill_col++) cols[cnt++] = ofill[ifill_col] + nc * (slot + gnx * l + p); 120547c6ae99SBarry Smith } 120647c6ae99SBarry Smith } else { 120747c6ae99SBarry Smith if (dfill) { 12088865f1eaSKarl Rupp for (ifill_col = dfill[k]; ifill_col < dfill[k + 1]; ifill_col++) cols[cnt++] = dfill[ifill_col] + nc * (slot + gnx * l + p); 120947c6ae99SBarry Smith } else { 12108865f1eaSKarl Rupp for (ifill_col = 0; ifill_col < nc; ifill_col++) cols[cnt++] = ifill_col + nc * (slot + gnx * l + p); 121147c6ae99SBarry Smith } 121247c6ae99SBarry Smith } 121347c6ae99SBarry Smith } 121447c6ae99SBarry Smith } 121547c6ae99SBarry Smith row = k + nc * (slot); 1216c0ab637bSBarry Smith maxcnt = PetscMax(maxcnt, cnt); 12171baa6e33SBarry Smith if (removedups) PetscCall(MatPreallocateSetLocalRemoveDups(ltog, 1, &row, ltog, cnt, cols, dnz, onz)); 12181baa6e33SBarry Smith else PetscCall(MatPreallocateSetLocal(ltog, 1, &row, ltog, cnt, cols, dnz, onz)); 121947c6ae99SBarry Smith } 122047c6ae99SBarry Smith } 1221c1154cd5SBarry Smith } 12229566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz)); 12239566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz)); 1224d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 12259566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 122647c6ae99SBarry Smith 122747c6ae99SBarry Smith /* 122847c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 122947c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 123047c6ae99SBarry Smith PETSc ordering. 123147c6ae99SBarry Smith */ 1232fcfd50ebSBarry Smith if (!da->prealloc_only) { 123347c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1234bff4a2f0SMatthew G. Knepley pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1235bff4a2f0SMatthew G. Knepley pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 123647c6ae99SBarry Smith 123747c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 123847c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 123947c6ae99SBarry Smith 1240bff4a2f0SMatthew G. Knepley lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1241bff4a2f0SMatthew G. Knepley lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 124247c6ae99SBarry Smith 124347c6ae99SBarry Smith for (k = 0; k < nc; k++) { 124447c6ae99SBarry Smith cnt = 0; 124547c6ae99SBarry Smith for (l = lstart; l < lend + 1; l++) { 124647c6ae99SBarry Smith for (p = pstart; p < pend + 1; p++) { 124747c6ae99SBarry Smith if (l || p) { 1248aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star */ 12498865f1eaSKarl Rupp for (ifill_col = ofill[k]; ifill_col < ofill[k + 1]; ifill_col++) cols[cnt++] = ofill[ifill_col] + nc * (slot + gnx * l + p); 125047c6ae99SBarry Smith } 125147c6ae99SBarry Smith } else { 125247c6ae99SBarry Smith if (dfill) { 12538865f1eaSKarl Rupp for (ifill_col = dfill[k]; ifill_col < dfill[k + 1]; ifill_col++) cols[cnt++] = dfill[ifill_col] + nc * (slot + gnx * l + p); 125447c6ae99SBarry Smith } else { 12558865f1eaSKarl Rupp for (ifill_col = 0; ifill_col < nc; ifill_col++) cols[cnt++] = ifill_col + nc * (slot + gnx * l + p); 125647c6ae99SBarry Smith } 125747c6ae99SBarry Smith } 125847c6ae99SBarry Smith } 125947c6ae99SBarry Smith } 126047c6ae99SBarry Smith row = k + nc * (slot); 12619566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, 1, &row, cnt, cols, NULL, INSERT_VALUES)); 126247c6ae99SBarry Smith } 126347c6ae99SBarry Smith } 126447c6ae99SBarry Smith } 1265e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 12669566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 12679566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 12689566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 12699566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 12709566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 127147c6ae99SBarry Smith } 12729566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 12733ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 127447c6ae99SBarry Smith } 127547c6ae99SBarry Smith 1276d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPIAIJ(DM da, Mat J) 1277d71ae5a4SJacob Faibussowitsch { 127847c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 12790298fd71SBarry Smith PetscInt m, n, dim, s, *cols = NULL, k, nc, *rows = NULL, col, cnt, l, p, *dnz = NULL, *onz = NULL; 1280c1154cd5SBarry Smith PetscInt istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk, M, N, P; 128147c6ae99SBarry Smith MPI_Comm comm; 1282bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by, bz; 1283844bd0d7SStefano Zampini ISLocalToGlobalMapping ltog, mltog; 1284aa219208SBarry Smith DMDAStencilType st; 1285c1154cd5SBarry Smith PetscBool removedups = PETSC_FALSE; 128647c6ae99SBarry Smith 128747c6ae99SBarry Smith PetscFunctionBegin; 128847c6ae99SBarry Smith /* 128947c6ae99SBarry Smith nc - number of components per grid point 129047c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 129147c6ae99SBarry Smith */ 12929566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &nc, &s, &bx, &by, &bz, &st)); 129348a46eb9SPierre Jolivet if (bx == DM_BOUNDARY_NONE && by == DM_BOUNDARY_NONE && bz == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_TRUE)); 129447c6ae99SBarry Smith col = 2 * s + 1; 129547c6ae99SBarry Smith 1296c1154cd5SBarry Smith /* 1297c1154cd5SBarry Smith With one processor in periodic domains in a skinny dimension the code will label nonzero columns multiple times 1298c1154cd5SBarry Smith because of "wrapping" around the end of the domain hitting an entry already counted in the other direction. 1299c1154cd5SBarry Smith */ 1300c1154cd5SBarry Smith if (M == 1 && 2 * s >= m) removedups = PETSC_TRUE; 1301c1154cd5SBarry Smith if (N == 1 && 2 * s >= n) removedups = PETSC_TRUE; 1302c1154cd5SBarry Smith if (P == 1 && 2 * s >= p) removedups = PETSC_TRUE; 1303c1154cd5SBarry Smith 13049566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 13059566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 13069566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 130747c6ae99SBarry Smith 13089566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * col * col * nc * nc, &cols)); 13099566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 131047c6ae99SBarry Smith 13119566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 131247c6ae99SBarry Smith /* determine the matrix preallocation information */ 1313d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny * nz, nc * nx * ny * nz, dnz, onz); 131447c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1315bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1316bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 131747c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1318bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1319bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 132047c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 1321bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 1322bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 132347c6ae99SBarry Smith 132447c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 132547c6ae99SBarry Smith 132647c6ae99SBarry Smith cnt = 0; 132747c6ae99SBarry Smith for (l = 0; l < nc; l++) { 132847c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 132947c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 133047c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 1331aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 133247c6ae99SBarry Smith cols[cnt++] = l + nc * (slot + ii + gnx * jj + gnx * gny * kk); 133347c6ae99SBarry Smith } 133447c6ae99SBarry Smith } 133547c6ae99SBarry Smith } 133647c6ae99SBarry Smith } 133747c6ae99SBarry Smith rows[l] = l + nc * (slot); 133847c6ae99SBarry Smith } 13391baa6e33SBarry Smith if (removedups) PetscCall(MatPreallocateSetLocalRemoveDups(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 13401baa6e33SBarry Smith else PetscCall(MatPreallocateSetLocal(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 134147c6ae99SBarry Smith } 134247c6ae99SBarry Smith } 1343c1154cd5SBarry Smith } 13449566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 13459566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz)); 13469566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz)); 1347d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 13489566063dSJacob Faibussowitsch PetscCall(MatGetLocalToGlobalMapping(J, &mltog, NULL)); 134948a46eb9SPierre Jolivet if (!mltog) PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 135047c6ae99SBarry Smith 135147c6ae99SBarry Smith /* 135247c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 135347c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 135447c6ae99SBarry Smith PETSc ordering. 135547c6ae99SBarry Smith */ 1356fcfd50ebSBarry Smith if (!da->prealloc_only) { 135747c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1358bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1359bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 136047c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1361bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1362bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 136347c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 1364bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 1365bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 136647c6ae99SBarry Smith 136747c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 136847c6ae99SBarry Smith 136947c6ae99SBarry Smith cnt = 0; 137047c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 1371071fcb05SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 1372071fcb05SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 1373aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 1374071fcb05SBarry Smith cols[cnt++] = nc * (slot + ii + gnx * jj + gnx * gny * kk); 1375071fcb05SBarry Smith for (l = 1; l < nc; l++) { 13769371c9d4SSatish Balay cols[cnt] = 1 + cols[cnt - 1]; 13779371c9d4SSatish Balay cnt++; 137847c6ae99SBarry Smith } 137947c6ae99SBarry Smith } 138047c6ae99SBarry Smith } 138147c6ae99SBarry Smith } 138247c6ae99SBarry Smith } 13839371c9d4SSatish Balay rows[0] = nc * (slot); 13849371c9d4SSatish Balay for (l = 1; l < nc; l++) rows[l] = 1 + rows[l - 1]; 13859566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, NULL, INSERT_VALUES)); 138647c6ae99SBarry Smith } 138747c6ae99SBarry Smith } 138847c6ae99SBarry Smith } 1389e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 13909566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 13919566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 13929566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 139348a46eb9SPierre Jolivet if (bx == DM_BOUNDARY_NONE && by == DM_BOUNDARY_NONE && bz == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE)); 13949566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 13959566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 139647c6ae99SBarry Smith } 13979566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 13983ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 139947c6ae99SBarry Smith } 140047c6ae99SBarry Smith 1401d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_1d_MPIAIJ_Fill(DM da, Mat J) 1402d71ae5a4SJacob Faibussowitsch { 1403ce308e1dSBarry Smith DM_DA *dd = (DM_DA *)da->data; 1404ce308e1dSBarry Smith PetscInt xs, nx, i, j, gxs, gnx, row, k, l; 14058d4c968fSBarry Smith PetscInt m, dim, s, *cols = NULL, nc, cnt, maxcnt = 0, *ocols; 14060acb5bebSBarry Smith PetscInt *ofill = dd->ofill, *dfill = dd->dfill; 1407bff4a2f0SMatthew G. Knepley DMBoundaryType bx; 140845b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 1409ce308e1dSBarry Smith PetscMPIInt rank, size; 1410ce308e1dSBarry Smith 1411ce308e1dSBarry Smith PetscFunctionBegin; 14129566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)da), &rank)); 14139566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)da), &size)); 1414ce308e1dSBarry Smith 1415ce308e1dSBarry Smith /* 1416ce308e1dSBarry Smith nc - number of components per grid point 1417ce308e1dSBarry Smith */ 14189566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, NULL, NULL, NULL, NULL, NULL, &nc, &s, &bx, NULL, NULL, NULL)); 141908401ef6SPierre Jolivet PetscCheck(s <= 1, PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "Matrix creation for 1d not implemented correctly for stencil width larger than 1"); 14209566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &nx, NULL, NULL)); 14219566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, NULL, NULL, &gnx, NULL, NULL)); 1422ce308e1dSBarry Smith 14239566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 14249566063dSJacob Faibussowitsch PetscCall(PetscCalloc2(nx * nc, &cols, nx * nc, &ocols)); 1425ce308e1dSBarry Smith 1426ce308e1dSBarry Smith /* 1427ce308e1dSBarry Smith note should be smaller for first and last process with no periodic 1428ce308e1dSBarry Smith does not handle dfill 1429ce308e1dSBarry Smith */ 1430ce308e1dSBarry Smith cnt = 0; 1431ce308e1dSBarry Smith /* coupling with process to the left */ 1432ce308e1dSBarry Smith for (i = 0; i < s; i++) { 1433ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 1434dd400576SPatrick Sanan ocols[cnt] = ((rank == 0) ? 0 : (s - i) * (ofill[j + 1] - ofill[j])); 14350acb5bebSBarry Smith cols[cnt] = dfill[j + 1] - dfill[j] + (s + i) * (ofill[j + 1] - ofill[j]); 1436dd400576SPatrick Sanan if (rank == 0 && (dd->bx == DM_BOUNDARY_PERIODIC)) { 1437831644c1SBarry Smith if (size > 1) ocols[cnt] += (s - i) * (ofill[j + 1] - ofill[j]); 1438831644c1SBarry Smith else cols[cnt] += (s - i) * (ofill[j + 1] - ofill[j]); 1439831644c1SBarry Smith } 1440c0ab637bSBarry Smith maxcnt = PetscMax(maxcnt, ocols[cnt] + cols[cnt]); 1441ce308e1dSBarry Smith cnt++; 1442ce308e1dSBarry Smith } 1443ce308e1dSBarry Smith } 1444ce308e1dSBarry Smith for (i = s; i < nx - s; i++) { 1445ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 14460acb5bebSBarry Smith cols[cnt] = dfill[j + 1] - dfill[j] + 2 * s * (ofill[j + 1] - ofill[j]); 1447c0ab637bSBarry Smith maxcnt = PetscMax(maxcnt, ocols[cnt] + cols[cnt]); 1448ce308e1dSBarry Smith cnt++; 1449ce308e1dSBarry Smith } 1450ce308e1dSBarry Smith } 1451ce308e1dSBarry Smith /* coupling with process to the right */ 1452ce308e1dSBarry Smith for (i = nx - s; i < nx; i++) { 1453ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 1454ce308e1dSBarry Smith ocols[cnt] = ((rank == (size - 1)) ? 0 : (i - nx + s + 1) * (ofill[j + 1] - ofill[j])); 14550acb5bebSBarry Smith cols[cnt] = dfill[j + 1] - dfill[j] + (s + nx - i - 1) * (ofill[j + 1] - ofill[j]); 1456831644c1SBarry Smith if ((rank == size - 1) && (dd->bx == DM_BOUNDARY_PERIODIC)) { 1457831644c1SBarry Smith if (size > 1) ocols[cnt] += (i - nx + s + 1) * (ofill[j + 1] - ofill[j]); 1458831644c1SBarry Smith else cols[cnt] += (i - nx + s + 1) * (ofill[j + 1] - ofill[j]); 1459831644c1SBarry Smith } 1460c0ab637bSBarry Smith maxcnt = PetscMax(maxcnt, ocols[cnt] + cols[cnt]); 1461ce308e1dSBarry Smith cnt++; 1462ce308e1dSBarry Smith } 1463ce308e1dSBarry Smith } 1464ce308e1dSBarry Smith 14659566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, 0, cols)); 14669566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, 0, cols, 0, ocols)); 14679566063dSJacob Faibussowitsch PetscCall(PetscFree2(cols, ocols)); 1468ce308e1dSBarry Smith 14699566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 14709566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 1471ce308e1dSBarry Smith 1472ce308e1dSBarry Smith /* 1473ce308e1dSBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 1474ce308e1dSBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 1475ce308e1dSBarry Smith PETSc ordering. 1476ce308e1dSBarry Smith */ 1477ce308e1dSBarry Smith if (!da->prealloc_only) { 14789566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(maxcnt, &cols)); 1479ce308e1dSBarry Smith row = xs * nc; 1480ce308e1dSBarry Smith /* coupling with process to the left */ 1481ce308e1dSBarry Smith for (i = xs; i < xs + s; i++) { 1482ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 1483ce308e1dSBarry Smith cnt = 0; 1484ce308e1dSBarry Smith if (rank) { 1485ce308e1dSBarry Smith for (l = 0; l < s; l++) { 1486ce308e1dSBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i - s + l) * nc + ofill[k]; 1487ce308e1dSBarry Smith } 1488ce308e1dSBarry Smith } 1489dd400576SPatrick Sanan if (rank == 0 && (dd->bx == DM_BOUNDARY_PERIODIC)) { 1490831644c1SBarry Smith for (l = 0; l < s; l++) { 1491831644c1SBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (m + i - s - l) * nc + ofill[k]; 1492831644c1SBarry Smith } 1493831644c1SBarry Smith } 14940acb5bebSBarry Smith if (dfill) { 1495ad540459SPierre Jolivet for (k = dfill[j]; k < dfill[j + 1]; k++) cols[cnt++] = i * nc + dfill[k]; 14960acb5bebSBarry Smith } else { 1497ad540459SPierre Jolivet for (k = 0; k < nc; k++) cols[cnt++] = i * nc + k; 14980acb5bebSBarry Smith } 1499ce308e1dSBarry Smith for (l = 0; l < s; l++) { 1500ce308e1dSBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i + s - l) * nc + ofill[k]; 1501ce308e1dSBarry Smith } 15029566063dSJacob Faibussowitsch PetscCall(MatSetValues(J, 1, &row, cnt, cols, NULL, INSERT_VALUES)); 1503ce308e1dSBarry Smith row++; 1504ce308e1dSBarry Smith } 1505ce308e1dSBarry Smith } 1506ce308e1dSBarry Smith for (i = xs + s; i < xs + nx - s; i++) { 1507ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 1508ce308e1dSBarry Smith cnt = 0; 1509ce308e1dSBarry Smith for (l = 0; l < s; l++) { 1510ce308e1dSBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i - s + l) * nc + ofill[k]; 1511ce308e1dSBarry Smith } 15120acb5bebSBarry Smith if (dfill) { 1513ad540459SPierre Jolivet for (k = dfill[j]; k < dfill[j + 1]; k++) cols[cnt++] = i * nc + dfill[k]; 15140acb5bebSBarry Smith } else { 1515ad540459SPierre Jolivet for (k = 0; k < nc; k++) cols[cnt++] = i * nc + k; 15160acb5bebSBarry Smith } 1517ce308e1dSBarry Smith for (l = 0; l < s; l++) { 1518ce308e1dSBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i + s - l) * nc + ofill[k]; 1519ce308e1dSBarry Smith } 15209566063dSJacob Faibussowitsch PetscCall(MatSetValues(J, 1, &row, cnt, cols, NULL, INSERT_VALUES)); 1521ce308e1dSBarry Smith row++; 1522ce308e1dSBarry Smith } 1523ce308e1dSBarry Smith } 1524ce308e1dSBarry Smith /* coupling with process to the right */ 1525ce308e1dSBarry Smith for (i = xs + nx - s; i < xs + nx; 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 if (rank < size - 1) { 1537ce308e1dSBarry Smith for (l = 0; l < s; l++) { 1538ce308e1dSBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i + s - l) * nc + ofill[k]; 1539ce308e1dSBarry Smith } 1540ce308e1dSBarry Smith } 1541831644c1SBarry Smith if ((rank == size - 1) && (dd->bx == DM_BOUNDARY_PERIODIC)) { 1542831644c1SBarry Smith for (l = 0; l < s; l++) { 1543831644c1SBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i - s - l - m + 2) * nc + ofill[k]; 1544831644c1SBarry Smith } 1545831644c1SBarry Smith } 15469566063dSJacob Faibussowitsch PetscCall(MatSetValues(J, 1, &row, cnt, cols, NULL, INSERT_VALUES)); 1547ce308e1dSBarry Smith row++; 1548ce308e1dSBarry Smith } 1549ce308e1dSBarry Smith } 15509566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 1551e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 15529566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 15539566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 15549566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 15559566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 15569566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 1557ce308e1dSBarry Smith } 15583ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1559ce308e1dSBarry Smith } 1560ce308e1dSBarry Smith 1561d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_1d_MPIAIJ(DM da, Mat J) 1562d71ae5a4SJacob Faibussowitsch { 156347c6ae99SBarry Smith PetscInt xs, nx, i, i1, slot, gxs, gnx; 15640298fd71SBarry Smith PetscInt m, dim, s, *cols = NULL, nc, *rows = NULL, col, cnt, l; 156547c6ae99SBarry Smith PetscInt istart, iend; 1566bff4a2f0SMatthew G. Knepley DMBoundaryType bx; 1567844bd0d7SStefano Zampini ISLocalToGlobalMapping ltog, mltog; 156847c6ae99SBarry Smith 156947c6ae99SBarry Smith PetscFunctionBegin; 157047c6ae99SBarry Smith /* 157147c6ae99SBarry Smith nc - number of components per grid point 157247c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 157347c6ae99SBarry Smith */ 15749566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, NULL, NULL, NULL, NULL, NULL, &nc, &s, &bx, NULL, NULL, NULL)); 157548a46eb9SPierre Jolivet if (bx == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_TRUE)); 157647c6ae99SBarry Smith col = 2 * s + 1; 157747c6ae99SBarry Smith 15789566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &nx, NULL, NULL)); 15799566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, NULL, NULL, &gnx, NULL, NULL)); 158047c6ae99SBarry Smith 15819566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 15829566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, col * nc, NULL)); 15839566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, col * nc, NULL, col * nc, NULL)); 158447c6ae99SBarry Smith 15859566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 15869566063dSJacob Faibussowitsch PetscCall(MatGetLocalToGlobalMapping(J, &mltog, NULL)); 158748a46eb9SPierre Jolivet if (!mltog) PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 158847c6ae99SBarry Smith 158947c6ae99SBarry Smith /* 159047c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 159147c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 159247c6ae99SBarry Smith PETSc ordering. 159347c6ae99SBarry Smith */ 1594fcfd50ebSBarry Smith if (!da->prealloc_only) { 15959566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * nc * nc, &cols)); 159647c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 159747c6ae99SBarry Smith istart = PetscMax(-s, gxs - i); 159847c6ae99SBarry Smith iend = PetscMin(s, gxs + gnx - i - 1); 159947c6ae99SBarry Smith slot = i - gxs; 160047c6ae99SBarry Smith 160147c6ae99SBarry Smith cnt = 0; 160247c6ae99SBarry Smith for (i1 = istart; i1 < iend + 1; i1++) { 1603071fcb05SBarry Smith cols[cnt++] = nc * (slot + i1); 1604071fcb05SBarry Smith for (l = 1; l < nc; l++) { 16059371c9d4SSatish Balay cols[cnt] = 1 + cols[cnt - 1]; 16069371c9d4SSatish Balay cnt++; 160747c6ae99SBarry Smith } 160847c6ae99SBarry Smith } 16099371c9d4SSatish Balay rows[0] = nc * (slot); 16109371c9d4SSatish Balay for (l = 1; l < nc; l++) rows[l] = 1 + rows[l - 1]; 16119566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, NULL, INSERT_VALUES)); 161247c6ae99SBarry Smith } 1613e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 16149566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 16159566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 16169566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 161748a46eb9SPierre Jolivet if (bx == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE)); 16189566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 16199566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 16209566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 1621ce308e1dSBarry Smith } 16223ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 162347c6ae99SBarry Smith } 162447c6ae99SBarry Smith 1625d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_1d_SeqAIJ_NoPreallocation(DM da, Mat J) 1626d71ae5a4SJacob Faibussowitsch { 162719b08ed1SBarry Smith PetscInt xs, nx, i, i1, slot, gxs, gnx; 162819b08ed1SBarry Smith PetscInt m, dim, s, *cols = NULL, nc, *rows = NULL, col, cnt, l; 162919b08ed1SBarry Smith PetscInt istart, iend; 163019b08ed1SBarry Smith DMBoundaryType bx; 163119b08ed1SBarry Smith ISLocalToGlobalMapping ltog, mltog; 163219b08ed1SBarry Smith 163319b08ed1SBarry Smith PetscFunctionBegin; 163419b08ed1SBarry Smith /* 163519b08ed1SBarry Smith nc - number of components per grid point 163619b08ed1SBarry Smith col - number of colors needed in one direction for single component problem 163719b08ed1SBarry Smith */ 16389566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, NULL, NULL, NULL, NULL, NULL, &nc, &s, &bx, NULL, NULL, NULL)); 163919b08ed1SBarry Smith col = 2 * s + 1; 164019b08ed1SBarry Smith 16419566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &nx, NULL, NULL)); 16429566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, NULL, NULL, &gnx, NULL, NULL)); 164319b08ed1SBarry Smith 16449566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 16459566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetTotalPreallocation(J, nx * nc * col * nc)); 164619b08ed1SBarry Smith 16479566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 16489566063dSJacob Faibussowitsch PetscCall(MatGetLocalToGlobalMapping(J, &mltog, NULL)); 164948a46eb9SPierre Jolivet if (!mltog) PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 165019b08ed1SBarry Smith 165119b08ed1SBarry Smith /* 165219b08ed1SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 165319b08ed1SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 165419b08ed1SBarry Smith PETSc ordering. 165519b08ed1SBarry Smith */ 165619b08ed1SBarry Smith if (!da->prealloc_only) { 16579566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * nc * nc, &cols)); 165819b08ed1SBarry Smith for (i = xs; i < xs + nx; i++) { 165919b08ed1SBarry Smith istart = PetscMax(-s, gxs - i); 166019b08ed1SBarry Smith iend = PetscMin(s, gxs + gnx - i - 1); 166119b08ed1SBarry Smith slot = i - gxs; 166219b08ed1SBarry Smith 166319b08ed1SBarry Smith cnt = 0; 166419b08ed1SBarry Smith for (i1 = istart; i1 < iend + 1; i1++) { 166519b08ed1SBarry Smith cols[cnt++] = nc * (slot + i1); 166619b08ed1SBarry Smith for (l = 1; l < nc; l++) { 16679371c9d4SSatish Balay cols[cnt] = 1 + cols[cnt - 1]; 16689371c9d4SSatish Balay cnt++; 166919b08ed1SBarry Smith } 167019b08ed1SBarry Smith } 16719371c9d4SSatish Balay rows[0] = nc * (slot); 16729371c9d4SSatish Balay for (l = 1; l < nc; l++) rows[l] = 1 + rows[l - 1]; 16739566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, NULL, INSERT_VALUES)); 167419b08ed1SBarry Smith } 167519b08ed1SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 16769566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 16779566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 16789566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 167948a46eb9SPierre Jolivet if (bx == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE)); 16809566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 16819566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 16829566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 168319b08ed1SBarry Smith } 16849566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE)); 16853ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 168619b08ed1SBarry Smith } 168719b08ed1SBarry Smith 1688d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPIBAIJ(DM da, Mat J) 1689d71ae5a4SJacob Faibussowitsch { 169047c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 169147c6ae99SBarry Smith PetscInt m, n, dim, s, *cols, nc, col, cnt, *dnz, *onz; 169247c6ae99SBarry Smith PetscInt istart, iend, jstart, jend, ii, jj; 169347c6ae99SBarry Smith MPI_Comm comm; 169447c6ae99SBarry Smith PetscScalar *values; 1695bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 1696aa219208SBarry Smith DMDAStencilType st; 169745b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 169847c6ae99SBarry Smith 169947c6ae99SBarry Smith PetscFunctionBegin; 170047c6ae99SBarry Smith /* 170147c6ae99SBarry Smith nc - number of components per grid point 170247c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 170347c6ae99SBarry Smith */ 17049566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, NULL, NULL, NULL, &nc, &s, &bx, &by, NULL, &st)); 170547c6ae99SBarry Smith col = 2 * s + 1; 170647c6ae99SBarry Smith 17079566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 17089566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 17099566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 171047c6ae99SBarry Smith 17119566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * nc * nc, &cols)); 171247c6ae99SBarry Smith 17139566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 171447c6ae99SBarry Smith 171547c6ae99SBarry Smith /* determine the matrix preallocation information */ 1716d0609cedSBarry Smith MatPreallocateBegin(comm, nx * ny, nx * ny, dnz, onz); 171747c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1718bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1719bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 172047c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1721bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1722bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 172347c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 172447c6ae99SBarry Smith 172547c6ae99SBarry Smith /* Find block columns in block row */ 172647c6ae99SBarry Smith cnt = 0; 172747c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 172847c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 1729aa219208SBarry Smith if (st == DMDA_STENCIL_BOX || !ii || !jj) { /* BOX or on the STAR */ 173047c6ae99SBarry Smith cols[cnt++] = slot + ii + gnx * jj; 173147c6ae99SBarry Smith } 173247c6ae99SBarry Smith } 173347c6ae99SBarry Smith } 17349566063dSJacob Faibussowitsch PetscCall(MatPreallocateSetLocalBlock(ltog, 1, &slot, ltog, cnt, cols, dnz, onz)); 173547c6ae99SBarry Smith } 173647c6ae99SBarry Smith } 17379566063dSJacob Faibussowitsch PetscCall(MatSeqBAIJSetPreallocation(J, nc, 0, dnz)); 17389566063dSJacob Faibussowitsch PetscCall(MatMPIBAIJSetPreallocation(J, nc, 0, dnz, 0, onz)); 1739d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 174047c6ae99SBarry Smith 17419566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 174247c6ae99SBarry Smith 174347c6ae99SBarry Smith /* 174447c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 174547c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 174647c6ae99SBarry Smith PETSc ordering. 174747c6ae99SBarry Smith */ 1748fcfd50ebSBarry Smith if (!da->prealloc_only) { 17499566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * nc * nc, &values)); 175047c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1751bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1752bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 175347c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1754bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1755bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 175647c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 175747c6ae99SBarry Smith cnt = 0; 175847c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 175947c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 1760aa219208SBarry Smith if (st == DMDA_STENCIL_BOX || !ii || !jj) { /* BOX or on the STAR */ 176147c6ae99SBarry Smith cols[cnt++] = slot + ii + gnx * jj; 176247c6ae99SBarry Smith } 176347c6ae99SBarry Smith } 176447c6ae99SBarry Smith } 17659566063dSJacob Faibussowitsch PetscCall(MatSetValuesBlockedLocal(J, 1, &slot, cnt, cols, values, INSERT_VALUES)); 176647c6ae99SBarry Smith } 176747c6ae99SBarry Smith } 17689566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 1769e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 17709566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 17719566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 17729566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 17739566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 17749566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 177547c6ae99SBarry Smith } 17769566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 17773ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 177847c6ae99SBarry Smith } 177947c6ae99SBarry Smith 1780d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPIBAIJ(DM da, Mat J) 1781d71ae5a4SJacob Faibussowitsch { 178247c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 178347c6ae99SBarry Smith PetscInt m, n, dim, s, *cols, k, nc, col, cnt, p, *dnz, *onz; 178447c6ae99SBarry Smith PetscInt istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk; 178547c6ae99SBarry Smith MPI_Comm comm; 178647c6ae99SBarry Smith PetscScalar *values; 1787bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by, bz; 1788aa219208SBarry Smith DMDAStencilType st; 178945b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 179047c6ae99SBarry Smith 179147c6ae99SBarry Smith PetscFunctionBegin; 179247c6ae99SBarry Smith /* 179347c6ae99SBarry Smith nc - number of components per grid point 179447c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 179547c6ae99SBarry Smith */ 17969566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, NULL, NULL, NULL, &nc, &s, &bx, &by, &bz, &st)); 179747c6ae99SBarry Smith col = 2 * s + 1; 179847c6ae99SBarry Smith 17999566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 18009566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 18019566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 180247c6ae99SBarry Smith 18039566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * col, &cols)); 180447c6ae99SBarry Smith 18059566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 180647c6ae99SBarry Smith 180747c6ae99SBarry Smith /* determine the matrix preallocation information */ 1808d0609cedSBarry Smith MatPreallocateBegin(comm, nx * ny * nz, nx * ny * nz, dnz, onz); 180947c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1810bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1811bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 181247c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1813bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1814bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 181547c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 1816bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 1817bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 181847c6ae99SBarry Smith 181947c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 182047c6ae99SBarry Smith 182147c6ae99SBarry Smith /* Find block columns in block row */ 182247c6ae99SBarry Smith cnt = 0; 182347c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 182447c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 182547c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 1826aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 182747c6ae99SBarry Smith cols[cnt++] = slot + ii + gnx * jj + gnx * gny * kk; 182847c6ae99SBarry Smith } 182947c6ae99SBarry Smith } 183047c6ae99SBarry Smith } 183147c6ae99SBarry Smith } 18329566063dSJacob Faibussowitsch PetscCall(MatPreallocateSetLocalBlock(ltog, 1, &slot, ltog, cnt, cols, dnz, onz)); 183347c6ae99SBarry Smith } 183447c6ae99SBarry Smith } 183547c6ae99SBarry Smith } 18369566063dSJacob Faibussowitsch PetscCall(MatSeqBAIJSetPreallocation(J, nc, 0, dnz)); 18379566063dSJacob Faibussowitsch PetscCall(MatMPIBAIJSetPreallocation(J, nc, 0, dnz, 0, onz)); 1838d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 183947c6ae99SBarry Smith 18409566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 184147c6ae99SBarry Smith 184247c6ae99SBarry Smith /* 184347c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 184447c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 184547c6ae99SBarry Smith PETSc ordering. 184647c6ae99SBarry Smith */ 1847fcfd50ebSBarry Smith if (!da->prealloc_only) { 18489566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * col * nc * nc, &values)); 184947c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1850bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1851bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 185247c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1853bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1854bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 185547c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 1856bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 1857bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 185847c6ae99SBarry Smith 185947c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 186047c6ae99SBarry Smith 186147c6ae99SBarry Smith cnt = 0; 186247c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 186347c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 186447c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 1865aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 186647c6ae99SBarry Smith cols[cnt++] = slot + ii + gnx * jj + gnx * gny * kk; 186747c6ae99SBarry Smith } 186847c6ae99SBarry Smith } 186947c6ae99SBarry Smith } 187047c6ae99SBarry Smith } 18719566063dSJacob Faibussowitsch PetscCall(MatSetValuesBlockedLocal(J, 1, &slot, cnt, cols, values, INSERT_VALUES)); 187247c6ae99SBarry Smith } 187347c6ae99SBarry Smith } 187447c6ae99SBarry Smith } 18759566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 1876e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 18779566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 18789566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 18799566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 18809566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 18819566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 188247c6ae99SBarry Smith } 18839566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 18843ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 188547c6ae99SBarry Smith } 188647c6ae99SBarry Smith 188747c6ae99SBarry Smith /* 188847c6ae99SBarry Smith This helper is for of SBAIJ preallocation, to discard the lower-triangular values which are difficult to 188947c6ae99SBarry Smith identify in the local ordering with periodic domain. 189047c6ae99SBarry Smith */ 1891d71ae5a4SJacob Faibussowitsch static PetscErrorCode L2GFilterUpperTriangular(ISLocalToGlobalMapping ltog, PetscInt *row, PetscInt *cnt, PetscInt col[]) 1892d71ae5a4SJacob Faibussowitsch { 189347c6ae99SBarry Smith PetscInt i, n; 189447c6ae99SBarry Smith 189547c6ae99SBarry Smith PetscFunctionBegin; 18969566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingApplyBlock(ltog, 1, row, row)); 18979566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingApplyBlock(ltog, *cnt, col, col)); 189847c6ae99SBarry Smith for (i = 0, n = 0; i < *cnt; i++) { 189947c6ae99SBarry Smith if (col[i] >= *row) col[n++] = col[i]; 190047c6ae99SBarry Smith } 190147c6ae99SBarry Smith *cnt = n; 19023ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 190347c6ae99SBarry Smith } 190447c6ae99SBarry Smith 1905d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPISBAIJ(DM da, Mat J) 1906d71ae5a4SJacob Faibussowitsch { 190747c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 190847c6ae99SBarry Smith PetscInt m, n, dim, s, *cols, nc, col, cnt, *dnz, *onz; 190947c6ae99SBarry Smith PetscInt istart, iend, jstart, jend, ii, jj; 191047c6ae99SBarry Smith MPI_Comm comm; 191147c6ae99SBarry Smith PetscScalar *values; 1912bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 1913aa219208SBarry Smith DMDAStencilType st; 191445b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 191547c6ae99SBarry Smith 191647c6ae99SBarry Smith PetscFunctionBegin; 191747c6ae99SBarry Smith /* 191847c6ae99SBarry Smith nc - number of components per grid point 191947c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 192047c6ae99SBarry Smith */ 19219566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, NULL, NULL, NULL, &nc, &s, &bx, &by, NULL, &st)); 192247c6ae99SBarry Smith col = 2 * s + 1; 192347c6ae99SBarry Smith 19249566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 19259566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 19269566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 192747c6ae99SBarry Smith 19289566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * nc * nc, &cols)); 192947c6ae99SBarry Smith 19309566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 193147c6ae99SBarry Smith 193247c6ae99SBarry Smith /* determine the matrix preallocation information */ 1933d0609cedSBarry Smith MatPreallocateBegin(comm, nx * ny, nx * ny, dnz, onz); 193447c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1935bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1936bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 193747c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1938bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1939bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 194047c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 194147c6ae99SBarry Smith 194247c6ae99SBarry Smith /* Find block columns in block row */ 194347c6ae99SBarry Smith cnt = 0; 194447c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 194547c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 1946ad540459SPierre Jolivet if (st == DMDA_STENCIL_BOX || !ii || !jj) cols[cnt++] = slot + ii + gnx * jj; 194747c6ae99SBarry Smith } 194847c6ae99SBarry Smith } 19499566063dSJacob Faibussowitsch PetscCall(L2GFilterUpperTriangular(ltog, &slot, &cnt, cols)); 19509566063dSJacob Faibussowitsch PetscCall(MatPreallocateSymmetricSetBlock(slot, cnt, cols, dnz, onz)); 195147c6ae99SBarry Smith } 195247c6ae99SBarry Smith } 19539566063dSJacob Faibussowitsch PetscCall(MatSeqSBAIJSetPreallocation(J, nc, 0, dnz)); 19549566063dSJacob Faibussowitsch PetscCall(MatMPISBAIJSetPreallocation(J, nc, 0, dnz, 0, onz)); 1955d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 195647c6ae99SBarry Smith 19579566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 195847c6ae99SBarry Smith 195947c6ae99SBarry Smith /* 196047c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 196147c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 196247c6ae99SBarry Smith PETSc ordering. 196347c6ae99SBarry Smith */ 1964fcfd50ebSBarry Smith if (!da->prealloc_only) { 19659566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * nc * nc, &values)); 196647c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1967bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1968bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 196947c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1970bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1971bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 197247c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 197347c6ae99SBarry Smith 197447c6ae99SBarry Smith /* Find block columns in block row */ 197547c6ae99SBarry Smith cnt = 0; 197647c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 197747c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 1978ad540459SPierre Jolivet if (st == DMDA_STENCIL_BOX || !ii || !jj) cols[cnt++] = slot + ii + gnx * jj; 197947c6ae99SBarry Smith } 198047c6ae99SBarry Smith } 19819566063dSJacob Faibussowitsch PetscCall(L2GFilterUpperTriangular(ltog, &slot, &cnt, cols)); 19829566063dSJacob Faibussowitsch PetscCall(MatSetValuesBlocked(J, 1, &slot, cnt, cols, values, INSERT_VALUES)); 198347c6ae99SBarry Smith } 198447c6ae99SBarry Smith } 19859566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 1986e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 19879566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 19889566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 19899566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 19909566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 19919566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 199247c6ae99SBarry Smith } 19939566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 19943ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 199547c6ae99SBarry Smith } 199647c6ae99SBarry Smith 1997d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPISBAIJ(DM da, Mat J) 1998d71ae5a4SJacob Faibussowitsch { 199947c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 200047c6ae99SBarry Smith PetscInt m, n, dim, s, *cols, k, nc, col, cnt, p, *dnz, *onz; 200147c6ae99SBarry Smith PetscInt istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk; 200247c6ae99SBarry Smith MPI_Comm comm; 200347c6ae99SBarry Smith PetscScalar *values; 2004bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by, bz; 2005aa219208SBarry Smith DMDAStencilType st; 200645b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 200747c6ae99SBarry Smith 200847c6ae99SBarry Smith PetscFunctionBegin; 200947c6ae99SBarry Smith /* 201047c6ae99SBarry Smith nc - number of components per grid point 201147c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 201247c6ae99SBarry Smith */ 20139566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, NULL, NULL, NULL, &nc, &s, &bx, &by, &bz, &st)); 201447c6ae99SBarry Smith col = 2 * s + 1; 201547c6ae99SBarry Smith 20169566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 20179566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 20189566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 201947c6ae99SBarry Smith 202047c6ae99SBarry Smith /* create the matrix */ 20219566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * col, &cols)); 202247c6ae99SBarry Smith 20239566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 202447c6ae99SBarry Smith 202547c6ae99SBarry Smith /* determine the matrix preallocation information */ 2026d0609cedSBarry Smith MatPreallocateBegin(comm, nx * ny * nz, nx * ny * nz, dnz, onz); 202747c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 2028bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 2029bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 203047c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 2031bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 2032bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 203347c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 2034bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 2035bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 203647c6ae99SBarry Smith 203747c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 203847c6ae99SBarry Smith 203947c6ae99SBarry Smith /* Find block columns in block row */ 204047c6ae99SBarry Smith cnt = 0; 204147c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 204247c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 204347c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 2044ad540459SPierre Jolivet if ((st == DMDA_STENCIL_BOX) || (!ii && !jj) || (!jj && !kk) || (!ii && !kk)) cols[cnt++] = slot + ii + gnx * jj + gnx * gny * kk; 204547c6ae99SBarry Smith } 204647c6ae99SBarry Smith } 204747c6ae99SBarry Smith } 20489566063dSJacob Faibussowitsch PetscCall(L2GFilterUpperTriangular(ltog, &slot, &cnt, cols)); 20499566063dSJacob Faibussowitsch PetscCall(MatPreallocateSymmetricSetBlock(slot, cnt, cols, dnz, onz)); 205047c6ae99SBarry Smith } 205147c6ae99SBarry Smith } 205247c6ae99SBarry Smith } 20539566063dSJacob Faibussowitsch PetscCall(MatSeqSBAIJSetPreallocation(J, nc, 0, dnz)); 20549566063dSJacob Faibussowitsch PetscCall(MatMPISBAIJSetPreallocation(J, nc, 0, dnz, 0, onz)); 2055d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 205647c6ae99SBarry Smith 20579566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 205847c6ae99SBarry Smith 205947c6ae99SBarry Smith /* 206047c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 206147c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 206247c6ae99SBarry Smith PETSc ordering. 206347c6ae99SBarry Smith */ 2064fcfd50ebSBarry Smith if (!da->prealloc_only) { 20659566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * col * nc * nc, &values)); 206647c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 2067bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 2068bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 206947c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 2070bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 2071bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 207247c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 2073bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 2074bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 207547c6ae99SBarry Smith 207647c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 207747c6ae99SBarry Smith 207847c6ae99SBarry Smith cnt = 0; 207947c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 208047c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 208147c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 2082ad540459SPierre Jolivet if ((st == DMDA_STENCIL_BOX) || (!ii && !jj) || (!jj && !kk) || (!ii && !kk)) cols[cnt++] = slot + ii + gnx * jj + gnx * gny * kk; 208347c6ae99SBarry Smith } 208447c6ae99SBarry Smith } 208547c6ae99SBarry Smith } 20869566063dSJacob Faibussowitsch PetscCall(L2GFilterUpperTriangular(ltog, &slot, &cnt, cols)); 20879566063dSJacob Faibussowitsch PetscCall(MatSetValuesBlocked(J, 1, &slot, cnt, cols, values, INSERT_VALUES)); 208847c6ae99SBarry Smith } 208947c6ae99SBarry Smith } 209047c6ae99SBarry Smith } 20919566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 2092e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 20939566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 20949566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 20959566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 20969566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 20979566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 209847c6ae99SBarry Smith } 20999566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 21003ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 210147c6ae99SBarry Smith } 210247c6ae99SBarry Smith 2103d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPIAIJ_Fill(DM da, Mat J) 2104d71ae5a4SJacob Faibussowitsch { 210547c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 2106c0ab637bSBarry Smith PetscInt m, n, dim, s, *cols, k, nc, row, col, cnt, maxcnt = 0, l, p, *dnz, *onz; 2107c1154cd5SBarry Smith PetscInt istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk, M, N, P; 210847c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 210947c6ae99SBarry Smith PetscInt ifill_col, *dfill = dd->dfill, *ofill = dd->ofill; 211047c6ae99SBarry Smith MPI_Comm comm; 211147c6ae99SBarry Smith PetscScalar *values; 2112bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by, bz; 211345b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 2114aa219208SBarry Smith DMDAStencilType st; 2115c1154cd5SBarry Smith PetscBool removedups = PETSC_FALSE; 211647c6ae99SBarry Smith 211747c6ae99SBarry Smith PetscFunctionBegin; 211847c6ae99SBarry Smith /* 211947c6ae99SBarry Smith nc - number of components per grid point 212047c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 212147c6ae99SBarry Smith */ 21229566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &nc, &s, &bx, &by, &bz, &st)); 212347c6ae99SBarry Smith col = 2 * s + 1; 212447c6ae99SBarry Smith 2125c1154cd5SBarry Smith /* 2126c1154cd5SBarry Smith With one processor in periodic domains in a skinny dimension the code will label nonzero columns multiple times 2127c1154cd5SBarry Smith because of "wrapping" around the end of the domain hitting an entry already counted in the other direction. 2128c1154cd5SBarry Smith */ 2129c1154cd5SBarry Smith if (M == 1 && 2 * s >= m) removedups = PETSC_TRUE; 2130c1154cd5SBarry Smith if (N == 1 && 2 * s >= n) removedups = PETSC_TRUE; 2131c1154cd5SBarry Smith if (P == 1 && 2 * s >= p) removedups = PETSC_TRUE; 2132c1154cd5SBarry Smith 21339566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 21349566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 21359566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 213647c6ae99SBarry Smith 21379566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * col * nc, &cols)); 21389566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 213947c6ae99SBarry Smith 214047c6ae99SBarry Smith /* determine the matrix preallocation information */ 2141d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny * nz, nc * nx * ny * nz, dnz, onz); 214247c6ae99SBarry Smith 21439566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 214447c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 2145bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 2146bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 214747c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 2148bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 2149bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 215047c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 2151bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 2152bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 215347c6ae99SBarry Smith 215447c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 215547c6ae99SBarry Smith 215647c6ae99SBarry Smith for (l = 0; l < nc; l++) { 215747c6ae99SBarry Smith cnt = 0; 215847c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 215947c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 216047c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 216147c6ae99SBarry Smith if (ii || jj || kk) { 2162aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 21638865f1eaSKarl 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); 216447c6ae99SBarry Smith } 216547c6ae99SBarry Smith } else { 216647c6ae99SBarry Smith if (dfill) { 21678865f1eaSKarl 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); 216847c6ae99SBarry Smith } else { 21698865f1eaSKarl Rupp for (ifill_col = 0; ifill_col < nc; ifill_col++) cols[cnt++] = ifill_col + nc * (slot + ii + gnx * jj + gnx * gny * kk); 217047c6ae99SBarry Smith } 217147c6ae99SBarry Smith } 217247c6ae99SBarry Smith } 217347c6ae99SBarry Smith } 217447c6ae99SBarry Smith } 217547c6ae99SBarry Smith row = l + nc * (slot); 2176c0ab637bSBarry Smith maxcnt = PetscMax(maxcnt, cnt); 21771baa6e33SBarry Smith if (removedups) PetscCall(MatPreallocateSetLocalRemoveDups(ltog, 1, &row, ltog, cnt, cols, dnz, onz)); 21781baa6e33SBarry Smith else PetscCall(MatPreallocateSetLocal(ltog, 1, &row, ltog, cnt, cols, dnz, onz)); 217947c6ae99SBarry Smith } 218047c6ae99SBarry Smith } 218147c6ae99SBarry Smith } 2182c1154cd5SBarry Smith } 21839566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz)); 21849566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz)); 2185d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 21869566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 218747c6ae99SBarry Smith 218847c6ae99SBarry Smith /* 218947c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 219047c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 219147c6ae99SBarry Smith PETSc ordering. 219247c6ae99SBarry Smith */ 2193fcfd50ebSBarry Smith if (!da->prealloc_only) { 21949566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(maxcnt, &values)); 219547c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 2196bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 2197bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 219847c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 2199bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 2200bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 220147c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 2202bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 2203bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 220447c6ae99SBarry Smith 220547c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 220647c6ae99SBarry Smith 220747c6ae99SBarry Smith for (l = 0; l < nc; l++) { 220847c6ae99SBarry Smith cnt = 0; 220947c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 221047c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 221147c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 221247c6ae99SBarry Smith if (ii || jj || kk) { 2213aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 22148865f1eaSKarl 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); 221547c6ae99SBarry Smith } 221647c6ae99SBarry Smith } else { 221747c6ae99SBarry Smith if (dfill) { 22188865f1eaSKarl 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); 221947c6ae99SBarry Smith } else { 22208865f1eaSKarl Rupp for (ifill_col = 0; ifill_col < nc; ifill_col++) cols[cnt++] = ifill_col + nc * (slot + ii + gnx * jj + gnx * gny * kk); 222147c6ae99SBarry Smith } 222247c6ae99SBarry Smith } 222347c6ae99SBarry Smith } 222447c6ae99SBarry Smith } 222547c6ae99SBarry Smith } 222647c6ae99SBarry Smith row = l + nc * (slot); 22279566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, 1, &row, cnt, cols, values, INSERT_VALUES)); 222847c6ae99SBarry Smith } 222947c6ae99SBarry Smith } 223047c6ae99SBarry Smith } 223147c6ae99SBarry Smith } 22329566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 2233e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 22349566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 22359566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 22369566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 22379566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 22389566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 223947c6ae99SBarry Smith } 22409566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 22413ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 224247c6ae99SBarry Smith } 2243