147c6ae99SBarry Smith 2af0996ceSBarry Smith #include <petsc/private/dmdaimpl.h> /*I "petscdmda.h" I*/ 307475bc1SBarry Smith #include <petscmat.h> 4e432b41dSStefano Zampini #include <petscbt.h> 547c6ae99SBarry Smith 6e727c939SJed Brown extern PetscErrorCode DMCreateColoring_DA_1d_MPIAIJ(DM, ISColoringType, ISColoring *); 7e727c939SJed Brown extern PetscErrorCode DMCreateColoring_DA_2d_MPIAIJ(DM, ISColoringType, ISColoring *); 8e727c939SJed Brown extern PetscErrorCode DMCreateColoring_DA_2d_5pt_MPIAIJ(DM, ISColoringType, ISColoring *); 9e727c939SJed Brown extern PetscErrorCode DMCreateColoring_DA_3d_MPIAIJ(DM, ISColoringType, ISColoring *); 1047c6ae99SBarry Smith 1147c6ae99SBarry Smith /* 1247c6ae99SBarry Smith For ghost i that may be negative or greater than the upper bound this 1347c6ae99SBarry Smith maps it into the 0:m-1 range using periodicity 1447c6ae99SBarry Smith */ 1547c6ae99SBarry Smith #define SetInRange(i, m) ((i < 0) ? m + i : ((i >= m) ? i - m : i)) 1647c6ae99SBarry Smith 17d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDASetBlockFills_Private(const PetscInt *dfill, PetscInt w, PetscInt **rfill) 18d71ae5a4SJacob Faibussowitsch { 1947c6ae99SBarry Smith PetscInt i, j, nz, *fill; 2047c6ae99SBarry Smith 2147c6ae99SBarry Smith PetscFunctionBegin; 2247c6ae99SBarry Smith if (!dfill) PetscFunctionReturn(0); 2347c6ae99SBarry Smith 2447c6ae99SBarry Smith /* count number nonzeros */ 2547c6ae99SBarry Smith nz = 0; 2647c6ae99SBarry Smith for (i = 0; i < w; i++) { 2747c6ae99SBarry Smith for (j = 0; j < w; j++) { 2847c6ae99SBarry Smith if (dfill[w * i + j]) nz++; 2947c6ae99SBarry Smith } 3047c6ae99SBarry Smith } 319566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nz + w + 1, &fill)); 3247c6ae99SBarry Smith /* construct modified CSR storage of nonzero structure */ 33ce308e1dSBarry Smith /* fill[0 -- w] marks starts of each row of column indices (and end of last row) 34ce308e1dSBarry Smith so fill[1] - fill[0] gives number of nonzeros in first row etc */ 3547c6ae99SBarry Smith nz = w + 1; 3647c6ae99SBarry Smith for (i = 0; i < w; i++) { 3747c6ae99SBarry Smith fill[i] = nz; 3847c6ae99SBarry Smith for (j = 0; j < w; j++) { 3947c6ae99SBarry Smith if (dfill[w * i + j]) { 4047c6ae99SBarry Smith fill[nz] = j; 4147c6ae99SBarry Smith nz++; 4247c6ae99SBarry Smith } 4347c6ae99SBarry Smith } 4447c6ae99SBarry Smith } 4547c6ae99SBarry Smith fill[w] = nz; 4647c6ae99SBarry Smith 4747c6ae99SBarry Smith *rfill = fill; 4847c6ae99SBarry Smith PetscFunctionReturn(0); 4947c6ae99SBarry Smith } 5047c6ae99SBarry Smith 51d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDASetBlockFillsSparse_Private(const PetscInt *dfillsparse, PetscInt w, PetscInt **rfill) 52d71ae5a4SJacob Faibussowitsch { 53767d920cSKarl Rupp PetscInt nz; 5409e28618SBarry Smith 5509e28618SBarry Smith PetscFunctionBegin; 5609e28618SBarry Smith if (!dfillsparse) PetscFunctionReturn(0); 5709e28618SBarry Smith 5809e28618SBarry Smith /* Determine number of non-zeros */ 5909e28618SBarry Smith nz = (dfillsparse[w] - w - 1); 6009e28618SBarry Smith 6109e28618SBarry Smith /* Allocate space for our copy of the given sparse matrix representation. */ 629566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nz + w + 1, rfill)); 639566063dSJacob Faibussowitsch PetscCall(PetscArraycpy(*rfill, dfillsparse, nz + w + 1)); 6409e28618SBarry Smith PetscFunctionReturn(0); 6509e28618SBarry Smith } 6609e28618SBarry Smith 67d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDASetBlockFills_Private2(DM_DA *dd) 68d71ae5a4SJacob Faibussowitsch { 6909e28618SBarry Smith PetscInt i, k, cnt = 1; 7009e28618SBarry Smith 7109e28618SBarry Smith PetscFunctionBegin; 7209e28618SBarry Smith 7309e28618SBarry Smith /* ofillcount tracks the columns of ofill that have any nonzero in thems; the value in each location is the number of 7409e28618SBarry Smith columns to the left with any nonzeros in them plus 1 */ 759566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(dd->w, &dd->ofillcols)); 7609e28618SBarry Smith for (i = 0; i < dd->w; i++) { 7709e28618SBarry Smith for (k = dd->ofill[i]; k < dd->ofill[i + 1]; k++) dd->ofillcols[dd->ofill[k]] = 1; 7809e28618SBarry Smith } 7909e28618SBarry Smith for (i = 0; i < dd->w; i++) { 80ad540459SPierre Jolivet if (dd->ofillcols[i]) dd->ofillcols[i] = cnt++; 8109e28618SBarry Smith } 8209e28618SBarry Smith PetscFunctionReturn(0); 8309e28618SBarry Smith } 8409e28618SBarry Smith 8547c6ae99SBarry Smith /*@ 86aa219208SBarry Smith DMDASetBlockFills - Sets the fill pattern in each block for a multi-component problem 87950540a4SJed Brown of the matrix returned by DMCreateMatrix(). 8847c6ae99SBarry Smith 89d083f849SBarry Smith Logically Collective on da 9047c6ae99SBarry Smith 91d8d19677SJose E. Roman Input Parameters: 9247c6ae99SBarry Smith + da - the distributed array 930298fd71SBarry Smith . dfill - the fill pattern in the diagonal block (may be NULL, means use dense block) 9447c6ae99SBarry Smith - ofill - the fill pattern in the off-diagonal blocks 9547c6ae99SBarry Smith 9647c6ae99SBarry Smith Level: developer 9747c6ae99SBarry Smith 9895452b02SPatrick Sanan Notes: 9995452b02SPatrick Sanan This only makes sense when you are doing multicomponent problems but using the 10047c6ae99SBarry Smith MPIAIJ matrix format 10147c6ae99SBarry Smith 10247c6ae99SBarry Smith The format for dfill and ofill is a 2 dimensional dof by dof matrix with 1 entries 10347c6ae99SBarry Smith representing coupling and 0 entries for missing coupling. For example 10447c6ae99SBarry Smith $ dfill[9] = {1, 0, 0, 10547c6ae99SBarry Smith $ 1, 1, 0, 10647c6ae99SBarry Smith $ 0, 1, 1} 10747c6ae99SBarry Smith means that row 0 is coupled with only itself in the diagonal block, row 1 is coupled with 10847c6ae99SBarry Smith itself and row 0 (in the diagonal block) and row 2 is coupled with itself and row 1 (in the 10947c6ae99SBarry Smith diagonal block). 11047c6ae99SBarry Smith 111aa219208SBarry Smith DMDASetGetMatrix() allows you to provide general code for those more complicated nonzero patterns then 11247c6ae99SBarry Smith can be represented in the dfill, ofill format 11347c6ae99SBarry Smith 11447c6ae99SBarry Smith Contributed by Glenn Hammond 11547c6ae99SBarry Smith 116db781477SPatrick Sanan .seealso `DMCreateMatrix()`, `DMDASetGetMatrix()`, `DMSetMatrixPreallocateOnly()` 11747c6ae99SBarry Smith 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)); 13009e28618SBarry Smith PetscFunctionReturn(0); 131ae4f298aSBarry Smith } 13209e28618SBarry Smith 13309e28618SBarry Smith /*@ 13409e28618SBarry Smith DMDASetBlockFillsSparse - Sets the fill pattern in each block for a multi-component problem 13509e28618SBarry Smith of the matrix returned by DMCreateMatrix(), using sparse representations 13609e28618SBarry Smith of fill patterns. 13709e28618SBarry Smith 138d083f849SBarry Smith Logically Collective on da 13909e28618SBarry Smith 140d8d19677SJose E. Roman Input Parameters: 14109e28618SBarry Smith + da - the distributed array 14209e28618SBarry Smith . dfill - the sparse fill pattern in the diagonal block (may be NULL, means use dense block) 14309e28618SBarry Smith - ofill - the sparse fill pattern in the off-diagonal blocks 14409e28618SBarry Smith 14509e28618SBarry Smith Level: developer 14609e28618SBarry Smith 14709e28618SBarry Smith Notes: This only makes sense when you are doing multicomponent problems but using the 14809e28618SBarry Smith MPIAIJ matrix format 14909e28618SBarry Smith 15009e28618SBarry Smith The format for dfill and ofill is a sparse representation of a 15109e28618SBarry Smith dof-by-dof matrix with 1 entries representing coupling and 0 entries 15209e28618SBarry Smith for missing coupling. The sparse representation is a 1 dimensional 15309e28618SBarry Smith array of length nz + dof + 1, where nz is the number of non-zeros in 15409e28618SBarry Smith the matrix. The first dof entries in the array give the 15509e28618SBarry Smith starting array indices of each row's items in the rest of the array, 15660942847SBarry Smith the dof+1st item contains the value nz + dof + 1 (i.e. the entire length of the array) 15709e28618SBarry Smith and the remaining nz items give the column indices of each of 15809e28618SBarry Smith the 1s within the logical 2D matrix. Each row's items within 15909e28618SBarry Smith the array are the column indices of the 1s within that row 16009e28618SBarry Smith of the 2D matrix. PETSc developers may recognize that this is the 16109e28618SBarry Smith same format as that computed by the DMDASetBlockFills_Private() 16209e28618SBarry Smith function from a dense 2D matrix representation. 16309e28618SBarry Smith 16409e28618SBarry Smith DMDASetGetMatrix() allows you to provide general code for those more complicated nonzero patterns then 16509e28618SBarry Smith can be represented in the dfill, ofill format 16609e28618SBarry Smith 16709e28618SBarry Smith Contributed by Philip C. Roth 16809e28618SBarry Smith 169db781477SPatrick Sanan .seealso `DMDASetBlockFills()`, `DMCreateMatrix()`, `DMDASetGetMatrix()`, `DMSetMatrixPreallocateOnly()` 17009e28618SBarry Smith 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)); 18347c6ae99SBarry Smith PetscFunctionReturn(0); 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; 22547c6ae99SBarry Smith } else if (dim > 1) { 226bff4a2f0SMatthew G. Knepley if ((m == 1 && bx == DM_BOUNDARY_PERIODIC) || (n == 1 && by == DM_BOUNDARY_PERIODIC) || (p == 1 && bz == DM_BOUNDARY_PERIODIC)) { 2275bdb020cSBarry Smith SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "IS_COLORING_LOCAL cannot be used for periodic boundary condition having both ends of the domain on the same process"); 22847c6ae99SBarry Smith } 22947c6ae99SBarry Smith } 23047c6ae99SBarry Smith } 23147c6ae99SBarry Smith 232aa219208SBarry Smith /* Tell the DMDA it has 1 degree of freedom per grid point so that the coloring for BAIJ 23347c6ae99SBarry Smith matrices is for the blocks, not the individual matrix elements */ 2349566063dSJacob Faibussowitsch PetscCall(PetscStrbeginswith(da->mattype, MATBAIJ, &isBAIJ)); 2359566063dSJacob Faibussowitsch if (!isBAIJ) PetscCall(PetscStrbeginswith(da->mattype, MATMPIBAIJ, &isBAIJ)); 2369566063dSJacob Faibussowitsch if (!isBAIJ) PetscCall(PetscStrbeginswith(da->mattype, MATSEQBAIJ, &isBAIJ)); 23747c6ae99SBarry Smith if (isBAIJ) { 23847c6ae99SBarry Smith dd->w = 1; 23947c6ae99SBarry Smith dd->xs = dd->xs / nc; 24047c6ae99SBarry Smith dd->xe = dd->xe / nc; 24147c6ae99SBarry Smith dd->Xs = dd->Xs / nc; 24247c6ae99SBarry Smith dd->Xe = dd->Xe / nc; 24347c6ae99SBarry Smith } 24447c6ae99SBarry Smith 24547c6ae99SBarry Smith /* 246aa219208SBarry Smith We do not provide a getcoloring function in the DMDA operations because 2479a1b256bSStefano Zampini the basic DMDA does not know about matrices. We think of DMDA as being 24847c6ae99SBarry Smith more low-level then matrices. 24947c6ae99SBarry Smith */ 2501baa6e33SBarry Smith if (dim == 1) PetscCall(DMCreateColoring_DA_1d_MPIAIJ(da, ctype, coloring)); 2511baa6e33SBarry Smith else if (dim == 2) PetscCall(DMCreateColoring_DA_2d_MPIAIJ(da, ctype, coloring)); 2521baa6e33SBarry Smith else if (dim == 3) PetscCall(DMCreateColoring_DA_3d_MPIAIJ(da, ctype, coloring)); 2531baa6e33SBarry Smith else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "Not done for %" PetscInt_FMT " dimension, send us mail petsc-maint@mcs.anl.gov for code", dim); 25447c6ae99SBarry Smith if (isBAIJ) { 25547c6ae99SBarry Smith dd->w = nc; 25647c6ae99SBarry Smith dd->xs = dd->xs * nc; 25747c6ae99SBarry Smith dd->xe = dd->xe * nc; 25847c6ae99SBarry Smith dd->Xs = dd->Xs * nc; 25947c6ae99SBarry Smith dd->Xe = dd->Xe * nc; 26047c6ae99SBarry Smith } 26147c6ae99SBarry Smith PetscFunctionReturn(0); 26247c6ae99SBarry Smith } 26347c6ae99SBarry Smith 26447c6ae99SBarry Smith /* ---------------------------------------------------------------------------------*/ 26547c6ae99SBarry Smith 266d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA_2d_MPIAIJ(DM da, ISColoringType ctype, ISColoring *coloring) 267d71ae5a4SJacob Faibussowitsch { 26847c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, ii, gxs, gys, gnx, gny, m, n, M, N, dim, s, k, nc, col; 269*dec0b466SHong Zhang PetscInt ncolors = 0; 27047c6ae99SBarry Smith MPI_Comm comm; 271bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 272aa219208SBarry Smith DMDAStencilType st; 27347c6ae99SBarry Smith ISColoringValue *colors; 27447c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 27547c6ae99SBarry Smith 27647c6ae99SBarry Smith PetscFunctionBegin; 27747c6ae99SBarry Smith /* 27847c6ae99SBarry Smith nc - number of components per grid point 27947c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 28047c6ae99SBarry Smith 28147c6ae99SBarry Smith */ 2829566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, &M, &N, NULL, &nc, &s, &bx, &by, NULL, &st)); 28347c6ae99SBarry Smith col = 2 * s + 1; 2849566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 2859566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 2869566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 28747c6ae99SBarry Smith 28847c6ae99SBarry Smith /* special case as taught to us by Paul Hovland */ 289aa219208SBarry Smith if (st == DMDA_STENCIL_STAR && s == 1) { 2909566063dSJacob Faibussowitsch PetscCall(DMCreateColoring_DA_2d_5pt_MPIAIJ(da, ctype, coloring)); 29147c6ae99SBarry Smith } else { 29247c6ae99SBarry Smith if (ctype == IS_COLORING_GLOBAL) { 29347c6ae99SBarry Smith if (!dd->localcoloring) { 2949566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * nx * ny, &colors)); 29547c6ae99SBarry Smith ii = 0; 29647c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 29747c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 298ad540459SPierre Jolivet for (k = 0; k < nc; k++) colors[ii++] = k + nc * ((i % col) + col * (j % col)); 29947c6ae99SBarry Smith } 30047c6ae99SBarry Smith } 30147c6ae99SBarry Smith ncolors = nc + nc * (col - 1 + col * (col - 1)); 3029566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * nx * ny, colors, PETSC_OWN_POINTER, &dd->localcoloring)); 30347c6ae99SBarry Smith } 30447c6ae99SBarry Smith *coloring = dd->localcoloring; 3055bdb020cSBarry Smith } else if (ctype == IS_COLORING_LOCAL) { 30647c6ae99SBarry Smith if (!dd->ghostedcoloring) { 3079566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * gnx * gny, &colors)); 30847c6ae99SBarry Smith ii = 0; 30947c6ae99SBarry Smith for (j = gys; j < gys + gny; j++) { 31047c6ae99SBarry Smith for (i = gxs; i < gxs + gnx; i++) { 31147c6ae99SBarry Smith for (k = 0; k < nc; k++) { 31247c6ae99SBarry Smith /* the complicated stuff is to handle periodic boundaries */ 31347c6ae99SBarry Smith colors[ii++] = k + nc * ((SetInRange(i, m) % col) + col * (SetInRange(j, n) % col)); 31447c6ae99SBarry Smith } 31547c6ae99SBarry Smith } 31647c6ae99SBarry Smith } 31747c6ae99SBarry Smith ncolors = nc + nc * (col - 1 + col * (col - 1)); 3189566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * gnx * gny, colors, PETSC_OWN_POINTER, &dd->ghostedcoloring)); 31947c6ae99SBarry Smith /* PetscIntView(ncolors,(PetscInt*)colors,0); */ 32047c6ae99SBarry Smith 3219566063dSJacob Faibussowitsch PetscCall(ISColoringSetType(dd->ghostedcoloring, IS_COLORING_LOCAL)); 32247c6ae99SBarry Smith } 32347c6ae99SBarry Smith *coloring = dd->ghostedcoloring; 32498921bdaSJacob Faibussowitsch } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_ARG_WRONG, "Unknown ISColoringType %d", (int)ctype); 32547c6ae99SBarry Smith } 3269566063dSJacob Faibussowitsch PetscCall(ISColoringReference(*coloring)); 32747c6ae99SBarry Smith PetscFunctionReturn(0); 32847c6ae99SBarry Smith } 32947c6ae99SBarry Smith 33047c6ae99SBarry Smith /* ---------------------------------------------------------------------------------*/ 33147c6ae99SBarry Smith 332d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA_3d_MPIAIJ(DM da, ISColoringType ctype, ISColoring *coloring) 333d71ae5a4SJacob Faibussowitsch { 33447c6ae99SBarry 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; 33547c6ae99SBarry Smith PetscInt ncolors; 33647c6ae99SBarry Smith MPI_Comm comm; 337bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by, bz; 338aa219208SBarry Smith DMDAStencilType st; 33947c6ae99SBarry Smith ISColoringValue *colors; 34047c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 34147c6ae99SBarry Smith 34247c6ae99SBarry Smith PetscFunctionBegin; 34347c6ae99SBarry Smith /* 34447c6ae99SBarry Smith nc - number of components per grid point 34547c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 34647c6ae99SBarry Smith 34747c6ae99SBarry Smith */ 3489566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &nc, &s, &bx, &by, &bz, &st)); 34947c6ae99SBarry Smith col = 2 * s + 1; 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)); 39147c6ae99SBarry Smith PetscFunctionReturn(0); 39247c6ae99SBarry Smith } 39347c6ae99SBarry Smith 39447c6ae99SBarry Smith /* ---------------------------------------------------------------------------------*/ 39547c6ae99SBarry Smith 396d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA_1d_MPIAIJ(DM da, ISColoringType ctype, ISColoring *coloring) 397d71ae5a4SJacob Faibussowitsch { 39847c6ae99SBarry Smith PetscInt xs, nx, i, i1, gxs, gnx, l, m, M, dim, s, nc, col; 39947c6ae99SBarry Smith PetscInt ncolors; 40047c6ae99SBarry Smith MPI_Comm comm; 401bff4a2f0SMatthew G. Knepley DMBoundaryType bx; 40247c6ae99SBarry Smith ISColoringValue *colors; 40347c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 40447c6ae99SBarry Smith 40547c6ae99SBarry Smith PetscFunctionBegin; 40647c6ae99SBarry Smith /* 40747c6ae99SBarry Smith nc - number of components per grid point 40847c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 40947c6ae99SBarry Smith 41047c6ae99SBarry Smith */ 4119566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, NULL, NULL, &M, NULL, NULL, &nc, &s, &bx, NULL, NULL, NULL)); 41247c6ae99SBarry Smith col = 2 * s + 1; 4139566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &nx, NULL, NULL)); 4149566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, NULL, NULL, &gnx, NULL, NULL)); 4159566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 41647c6ae99SBarry Smith 41747c6ae99SBarry Smith /* create the coloring */ 41847c6ae99SBarry Smith if (ctype == IS_COLORING_GLOBAL) { 41947c6ae99SBarry Smith if (!dd->localcoloring) { 4209566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * nx, &colors)); 421ae4f298aSBarry Smith if (dd->ofillcols) { 422ae4f298aSBarry Smith PetscInt tc = 0; 423ae4f298aSBarry Smith for (i = 0; i < nc; i++) tc += (PetscInt)(dd->ofillcols[i] > 0); 424ae4f298aSBarry Smith i1 = 0; 425ae4f298aSBarry Smith for (i = xs; i < xs + nx; i++) { 426ae4f298aSBarry Smith for (l = 0; l < nc; l++) { 427ae4f298aSBarry Smith if (dd->ofillcols[l] && (i % col)) { 428ae4f298aSBarry Smith colors[i1++] = nc - 1 + tc * ((i % col) - 1) + dd->ofillcols[l]; 429ae4f298aSBarry Smith } else { 430ae4f298aSBarry Smith colors[i1++] = l; 431ae4f298aSBarry Smith } 432ae4f298aSBarry Smith } 433ae4f298aSBarry Smith } 434ae4f298aSBarry Smith ncolors = nc + 2 * s * tc; 435ae4f298aSBarry Smith } else { 43647c6ae99SBarry Smith i1 = 0; 43747c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 438ad540459SPierre Jolivet for (l = 0; l < nc; l++) colors[i1++] = l + nc * (i % col); 43947c6ae99SBarry Smith } 44047c6ae99SBarry Smith ncolors = nc + nc * (col - 1); 441ae4f298aSBarry Smith } 4429566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * nx, colors, PETSC_OWN_POINTER, &dd->localcoloring)); 44347c6ae99SBarry Smith } 44447c6ae99SBarry Smith *coloring = dd->localcoloring; 4455bdb020cSBarry Smith } else if (ctype == IS_COLORING_LOCAL) { 44647c6ae99SBarry Smith if (!dd->ghostedcoloring) { 4479566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * gnx, &colors)); 44847c6ae99SBarry Smith i1 = 0; 44947c6ae99SBarry Smith for (i = gxs; i < gxs + gnx; i++) { 45047c6ae99SBarry Smith for (l = 0; l < nc; l++) { 45147c6ae99SBarry Smith /* the complicated stuff is to handle periodic boundaries */ 45247c6ae99SBarry Smith colors[i1++] = l + nc * (SetInRange(i, m) % col); 45347c6ae99SBarry Smith } 45447c6ae99SBarry Smith } 45547c6ae99SBarry Smith ncolors = nc + nc * (col - 1); 4569566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * gnx, colors, PETSC_OWN_POINTER, &dd->ghostedcoloring)); 4579566063dSJacob Faibussowitsch PetscCall(ISColoringSetType(dd->ghostedcoloring, IS_COLORING_LOCAL)); 45847c6ae99SBarry Smith } 45947c6ae99SBarry Smith *coloring = dd->ghostedcoloring; 46098921bdaSJacob Faibussowitsch } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_ARG_WRONG, "Unknown ISColoringType %d", (int)ctype); 4619566063dSJacob Faibussowitsch PetscCall(ISColoringReference(*coloring)); 46247c6ae99SBarry Smith PetscFunctionReturn(0); 46347c6ae99SBarry Smith } 46447c6ae99SBarry Smith 465d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA_2d_5pt_MPIAIJ(DM da, ISColoringType ctype, ISColoring *coloring) 466d71ae5a4SJacob Faibussowitsch { 46747c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, ii, gxs, gys, gnx, gny, m, n, dim, s, k, nc; 46847c6ae99SBarry Smith PetscInt ncolors; 46947c6ae99SBarry Smith MPI_Comm comm; 470bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 47147c6ae99SBarry Smith ISColoringValue *colors; 47247c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 47347c6ae99SBarry Smith 47447c6ae99SBarry Smith PetscFunctionBegin; 47547c6ae99SBarry Smith /* 47647c6ae99SBarry Smith nc - number of components per grid point 47747c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 47847c6ae99SBarry Smith 47947c6ae99SBarry Smith */ 4809566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, NULL, NULL, NULL, &nc, &s, &bx, &by, NULL, NULL)); 4819566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 4829566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 4839566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 48447c6ae99SBarry Smith /* create the coloring */ 48547c6ae99SBarry Smith if (ctype == IS_COLORING_GLOBAL) { 48647c6ae99SBarry Smith if (!dd->localcoloring) { 4879566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * nx * ny, &colors)); 48847c6ae99SBarry Smith ii = 0; 48947c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 49047c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 491ad540459SPierre Jolivet for (k = 0; k < nc; k++) colors[ii++] = k + nc * ((3 * j + i) % 5); 49247c6ae99SBarry Smith } 49347c6ae99SBarry Smith } 49447c6ae99SBarry Smith ncolors = 5 * nc; 4959566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * nx * ny, colors, PETSC_OWN_POINTER, &dd->localcoloring)); 49647c6ae99SBarry Smith } 49747c6ae99SBarry Smith *coloring = dd->localcoloring; 4985bdb020cSBarry Smith } else if (ctype == IS_COLORING_LOCAL) { 49947c6ae99SBarry Smith if (!dd->ghostedcoloring) { 5009566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * gnx * gny, &colors)); 50147c6ae99SBarry Smith ii = 0; 50247c6ae99SBarry Smith for (j = gys; j < gys + gny; j++) { 50347c6ae99SBarry Smith for (i = gxs; i < gxs + gnx; i++) { 504ad540459SPierre Jolivet for (k = 0; k < nc; k++) colors[ii++] = k + nc * ((3 * SetInRange(j, n) + SetInRange(i, m)) % 5); 50547c6ae99SBarry Smith } 50647c6ae99SBarry Smith } 50747c6ae99SBarry Smith ncolors = 5 * nc; 5089566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * gnx * gny, colors, PETSC_OWN_POINTER, &dd->ghostedcoloring)); 5099566063dSJacob Faibussowitsch PetscCall(ISColoringSetType(dd->ghostedcoloring, IS_COLORING_LOCAL)); 51047c6ae99SBarry Smith } 51147c6ae99SBarry Smith *coloring = dd->ghostedcoloring; 51298921bdaSJacob Faibussowitsch } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_ARG_WRONG, "Unknown ISColoringType %d", (int)ctype); 51347c6ae99SBarry Smith PetscFunctionReturn(0); 51447c6ae99SBarry Smith } 51547c6ae99SBarry Smith 51647c6ae99SBarry Smith /* =========================================================================== */ 517e432b41dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_1d_MPIAIJ(DM, Mat); 518ce308e1dSBarry Smith extern PetscErrorCode DMCreateMatrix_DA_1d_MPIAIJ_Fill(DM, Mat); 519e432b41dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_1d_SeqAIJ_NoPreallocation(DM, Mat); 520e432b41dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_2d_MPIAIJ(DM, Mat); 521950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_2d_MPIAIJ_Fill(DM, Mat); 522e432b41dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_3d_MPIAIJ(DM, Mat); 523950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_3d_MPIAIJ_Fill(DM, Mat); 524950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_2d_MPIBAIJ(DM, Mat); 525950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_3d_MPIBAIJ(DM, Mat); 526950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_2d_MPISBAIJ(DM, Mat); 527950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_3d_MPISBAIJ(DM, Mat); 528d4002b98SHong Zhang extern PetscErrorCode DMCreateMatrix_DA_2d_MPISELL(DM, Mat); 529d4002b98SHong Zhang extern PetscErrorCode DMCreateMatrix_DA_3d_MPISELL(DM, Mat); 530e584696dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_IS(DM, Mat); 53147c6ae99SBarry Smith 5328bbdbebaSMatthew G Knepley /*@C 533c688c046SMatthew G Knepley MatSetupDM - Sets the DMDA that is to be used by the HYPRE_StructMatrix PETSc matrix 53447c6ae99SBarry Smith 535d083f849SBarry Smith Logically Collective on mat 53647c6ae99SBarry Smith 53747c6ae99SBarry Smith Input Parameters: 53847c6ae99SBarry Smith + mat - the matrix 53947c6ae99SBarry Smith - da - the da 54047c6ae99SBarry Smith 54147c6ae99SBarry Smith Level: intermediate 54247c6ae99SBarry Smith 54347c6ae99SBarry Smith @*/ 544d71ae5a4SJacob Faibussowitsch PetscErrorCode MatSetupDM(Mat mat, DM da) 545d71ae5a4SJacob Faibussowitsch { 54647c6ae99SBarry Smith PetscFunctionBegin; 54747c6ae99SBarry Smith PetscValidHeaderSpecific(mat, MAT_CLASSID, 1); 548064a246eSJacob Faibussowitsch PetscValidHeaderSpecificType(da, DM_CLASSID, 2, DMDA); 549cac4c232SBarry Smith PetscTryMethod(mat, "MatSetupDM_C", (Mat, DM), (mat, da)); 55047c6ae99SBarry Smith PetscFunctionReturn(0); 55147c6ae99SBarry Smith } 55247c6ae99SBarry Smith 553d71ae5a4SJacob Faibussowitsch PetscErrorCode MatView_MPI_DA(Mat A, PetscViewer viewer) 554d71ae5a4SJacob Faibussowitsch { 5559a42bb27SBarry Smith DM da; 55647c6ae99SBarry Smith const char *prefix; 55747c6ae99SBarry Smith Mat Anatural; 55847c6ae99SBarry Smith AO ao; 55947c6ae99SBarry Smith PetscInt rstart, rend, *petsc, i; 56047c6ae99SBarry Smith IS is; 56147c6ae99SBarry Smith MPI_Comm comm; 56274388724SJed Brown PetscViewerFormat format; 56347c6ae99SBarry Smith 56447c6ae99SBarry Smith PetscFunctionBegin; 56574388724SJed Brown /* Check whether we are just printing info, in which case MatView() already viewed everything we wanted to view */ 5669566063dSJacob Faibussowitsch PetscCall(PetscViewerGetFormat(viewer, &format)); 56774388724SJed Brown if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) PetscFunctionReturn(0); 56874388724SJed Brown 5699566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)A, &comm)); 5709566063dSJacob Faibussowitsch PetscCall(MatGetDM(A, &da)); 5717a8be351SBarry Smith PetscCheck(da, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Matrix not generated from a DMDA"); 57247c6ae99SBarry Smith 5739566063dSJacob Faibussowitsch PetscCall(DMDAGetAO(da, &ao)); 5749566063dSJacob Faibussowitsch PetscCall(MatGetOwnershipRange(A, &rstart, &rend)); 5759566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(rend - rstart, &petsc)); 57647c6ae99SBarry Smith for (i = rstart; i < rend; i++) petsc[i - rstart] = i; 5779566063dSJacob Faibussowitsch PetscCall(AOApplicationToPetsc(ao, rend - rstart, petsc)); 5789566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(comm, rend - rstart, petsc, PETSC_OWN_POINTER, &is)); 57947c6ae99SBarry Smith 58047c6ae99SBarry Smith /* call viewer on natural ordering */ 5819566063dSJacob Faibussowitsch PetscCall(MatCreateSubMatrix(A, is, is, MAT_INITIAL_MATRIX, &Anatural)); 5829566063dSJacob Faibussowitsch PetscCall(ISDestroy(&is)); 5839566063dSJacob Faibussowitsch PetscCall(PetscObjectGetOptionsPrefix((PetscObject)A, &prefix)); 5849566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptionsPrefix((PetscObject)Anatural, prefix)); 5859566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)Anatural, ((PetscObject)A)->name)); 586f0ed2f47SStefano Zampini ((PetscObject)Anatural)->donotPetscObjectPrintClassNamePrefixType = PETSC_TRUE; 5879566063dSJacob Faibussowitsch PetscCall(MatView(Anatural, viewer)); 588f0ed2f47SStefano Zampini ((PetscObject)Anatural)->donotPetscObjectPrintClassNamePrefixType = PETSC_FALSE; 5899566063dSJacob Faibussowitsch PetscCall(MatDestroy(&Anatural)); 59047c6ae99SBarry Smith PetscFunctionReturn(0); 59147c6ae99SBarry Smith } 59247c6ae99SBarry Smith 593d71ae5a4SJacob Faibussowitsch PetscErrorCode MatLoad_MPI_DA(Mat A, PetscViewer viewer) 594d71ae5a4SJacob Faibussowitsch { 5959a42bb27SBarry Smith DM da; 59647c6ae99SBarry Smith Mat Anatural, Aapp; 59747c6ae99SBarry Smith AO ao; 598539c167fSBarry Smith PetscInt rstart, rend, *app, i, m, n, M, N; 59947c6ae99SBarry Smith IS is; 60047c6ae99SBarry Smith MPI_Comm comm; 60147c6ae99SBarry Smith 60247c6ae99SBarry Smith PetscFunctionBegin; 6039566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)A, &comm)); 6049566063dSJacob Faibussowitsch PetscCall(MatGetDM(A, &da)); 6057a8be351SBarry Smith PetscCheck(da, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Matrix not generated from a DMDA"); 60647c6ae99SBarry Smith 60747c6ae99SBarry Smith /* Load the matrix in natural ordering */ 6089566063dSJacob Faibussowitsch PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &Anatural)); 6099566063dSJacob Faibussowitsch PetscCall(MatSetType(Anatural, ((PetscObject)A)->type_name)); 6109566063dSJacob Faibussowitsch PetscCall(MatGetSize(A, &M, &N)); 6119566063dSJacob Faibussowitsch PetscCall(MatGetLocalSize(A, &m, &n)); 6129566063dSJacob Faibussowitsch PetscCall(MatSetSizes(Anatural, m, n, M, N)); 6139566063dSJacob Faibussowitsch PetscCall(MatLoad(Anatural, viewer)); 61447c6ae99SBarry Smith 61547c6ae99SBarry Smith /* Map natural ordering to application ordering and create IS */ 6169566063dSJacob Faibussowitsch PetscCall(DMDAGetAO(da, &ao)); 6179566063dSJacob Faibussowitsch PetscCall(MatGetOwnershipRange(Anatural, &rstart, &rend)); 6189566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(rend - rstart, &app)); 61947c6ae99SBarry Smith for (i = rstart; i < rend; i++) app[i - rstart] = i; 6209566063dSJacob Faibussowitsch PetscCall(AOPetscToApplication(ao, rend - rstart, app)); 6219566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(comm, rend - rstart, app, PETSC_OWN_POINTER, &is)); 62247c6ae99SBarry Smith 62347c6ae99SBarry Smith /* Do permutation and replace header */ 6249566063dSJacob Faibussowitsch PetscCall(MatCreateSubMatrix(Anatural, is, is, MAT_INITIAL_MATRIX, &Aapp)); 6259566063dSJacob Faibussowitsch PetscCall(MatHeaderReplace(A, &Aapp)); 6269566063dSJacob Faibussowitsch PetscCall(ISDestroy(&is)); 6279566063dSJacob Faibussowitsch PetscCall(MatDestroy(&Anatural)); 62847c6ae99SBarry Smith PetscFunctionReturn(0); 62947c6ae99SBarry Smith } 63047c6ae99SBarry Smith 631d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA(DM da, Mat *J) 632d71ae5a4SJacob Faibussowitsch { 63347c6ae99SBarry Smith PetscInt dim, dof, nx, ny, nz, dims[3], starts[3], M, N, P; 63447c6ae99SBarry Smith Mat A; 63547c6ae99SBarry Smith MPI_Comm comm; 63619fd82e9SBarry Smith MatType Atype; 637e584696dSStefano Zampini void (*aij)(void) = NULL, (*baij)(void) = NULL, (*sbaij)(void) = NULL, (*sell)(void) = NULL, (*is)(void) = NULL; 638b412c318SBarry Smith MatType mtype; 63947c6ae99SBarry Smith PetscMPIInt size; 64047c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 64147c6ae99SBarry Smith 64247c6ae99SBarry Smith PetscFunctionBegin; 6439566063dSJacob Faibussowitsch PetscCall(MatInitializePackage()); 644b412c318SBarry Smith mtype = da->mattype; 64547c6ae99SBarry Smith 64647c6ae99SBarry Smith /* 64747c6ae99SBarry Smith m 64847c6ae99SBarry Smith ------------------------------------------------------ 64947c6ae99SBarry Smith | | 65047c6ae99SBarry Smith | | 65147c6ae99SBarry Smith | ---------------------- | 65247c6ae99SBarry Smith | | | | 65347c6ae99SBarry Smith n | ny | | | 65447c6ae99SBarry Smith | | | | 65547c6ae99SBarry Smith | .--------------------- | 65647c6ae99SBarry Smith | (xs,ys) nx | 65747c6ae99SBarry Smith | . | 65847c6ae99SBarry Smith | (gxs,gys) | 65947c6ae99SBarry Smith | | 66047c6ae99SBarry Smith ----------------------------------------------------- 66147c6ae99SBarry Smith */ 66247c6ae99SBarry Smith 66347c6ae99SBarry Smith /* 66447c6ae99SBarry Smith nc - number of components per grid point 66547c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 66647c6ae99SBarry Smith 66747c6ae99SBarry Smith */ 668e30e807fSPeter Brune M = dd->M; 669e30e807fSPeter Brune N = dd->N; 670e30e807fSPeter Brune P = dd->P; 671c73cfb54SMatthew G. Knepley dim = da->dim; 672e30e807fSPeter Brune dof = dd->w; 6739566063dSJacob Faibussowitsch /* PetscCall(DMDAGetInfo(da,&dim,&M,&N,&P,NULL,NULL,NULL,&dof,NULL,NULL,NULL,NULL,NULL)); */ 6749566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, NULL, NULL, NULL, &nx, &ny, &nz)); 6759566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 6769566063dSJacob Faibussowitsch PetscCall(MatCreate(comm, &A)); 6779566063dSJacob Faibussowitsch PetscCall(MatSetSizes(A, dof * nx * ny * nz, dof * nx * ny * nz, dof * M * N * P, dof * M * N * P)); 6789566063dSJacob Faibussowitsch PetscCall(MatSetType(A, mtype)); 6799566063dSJacob Faibussowitsch PetscCall(MatSetFromOptions(A)); 68074427ab1SRichard Tran Mills if (dof * nx * ny * nz < da->bind_below) { 6819566063dSJacob Faibussowitsch PetscCall(MatSetBindingPropagates(A, PETSC_TRUE)); 6829566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(A, PETSC_TRUE)); 68374427ab1SRichard Tran Mills } 6849566063dSJacob Faibussowitsch PetscCall(MatSetDM(A, da)); 6851baa6e33SBarry Smith if (da->structure_only) PetscCall(MatSetOption(A, MAT_STRUCTURE_ONLY, PETSC_TRUE)); 6869566063dSJacob Faibussowitsch PetscCall(MatGetType(A, &Atype)); 68747c6ae99SBarry Smith /* 688aa219208SBarry Smith We do not provide a getmatrix function in the DMDA operations because 689aa219208SBarry Smith the basic DMDA does not know about matrices. We think of DMDA as being more 69047c6ae99SBarry Smith more low-level than matrices. This is kind of cheating but, cause sometimes 691aa219208SBarry Smith we think of DMDA has higher level than matrices. 69247c6ae99SBarry Smith 69347c6ae99SBarry Smith We could switch based on Atype (or mtype), but we do not since the 694844bd0d7SStefano Zampini specialized setting routines depend only on the particular preallocation 69547c6ae99SBarry Smith details of the matrix, not the type itself. 69647c6ae99SBarry Smith */ 6979566063dSJacob Faibussowitsch PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatMPIAIJSetPreallocation_C", &aij)); 69848a46eb9SPierre Jolivet if (!aij) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatSeqAIJSetPreallocation_C", &aij)); 69947c6ae99SBarry Smith if (!aij) { 7009566063dSJacob Faibussowitsch PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatMPIBAIJSetPreallocation_C", &baij)); 70148a46eb9SPierre Jolivet if (!baij) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatSeqBAIJSetPreallocation_C", &baij)); 70247c6ae99SBarry Smith if (!baij) { 7039566063dSJacob Faibussowitsch PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatMPISBAIJSetPreallocation_C", &sbaij)); 70448a46eb9SPierre Jolivet if (!sbaij) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatSeqSBAIJSetPreallocation_C", &sbaij)); 7055e26d47bSHong Zhang if (!sbaij) { 7069566063dSJacob Faibussowitsch PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatMPISELLSetPreallocation_C", &sell)); 70748a46eb9SPierre Jolivet if (!sell) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatSeqSELLSetPreallocation_C", &sell)); 7085e26d47bSHong Zhang } 70948a46eb9SPierre Jolivet if (!sell) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatISSetPreallocation_C", &is)); 71047c6ae99SBarry Smith } 71147c6ae99SBarry Smith } 71247c6ae99SBarry Smith if (aij) { 71347c6ae99SBarry Smith if (dim == 1) { 714ce308e1dSBarry Smith if (dd->ofill) { 7159566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_1d_MPIAIJ_Fill(da, A)); 716ce308e1dSBarry Smith } else { 71719b08ed1SBarry Smith DMBoundaryType bx; 71819b08ed1SBarry Smith PetscMPIInt size; 7199566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &bx, NULL, NULL, NULL)); 7209566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)da), &size)); 72119b08ed1SBarry Smith if (size == 1 && bx == DM_BOUNDARY_NONE) { 7229566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_1d_SeqAIJ_NoPreallocation(da, A)); 72319b08ed1SBarry Smith } else { 7249566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_1d_MPIAIJ(da, A)); 725ce308e1dSBarry Smith } 72619b08ed1SBarry Smith } 72747c6ae99SBarry Smith } else if (dim == 2) { 72847c6ae99SBarry Smith if (dd->ofill) { 7299566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_2d_MPIAIJ_Fill(da, A)); 73047c6ae99SBarry Smith } else { 7319566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_2d_MPIAIJ(da, A)); 73247c6ae99SBarry Smith } 73347c6ae99SBarry Smith } else if (dim == 3) { 73447c6ae99SBarry Smith if (dd->ofill) { 7359566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_3d_MPIAIJ_Fill(da, A)); 73647c6ae99SBarry Smith } else { 7379566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_3d_MPIAIJ(da, A)); 73847c6ae99SBarry Smith } 73947c6ae99SBarry Smith } 74047c6ae99SBarry Smith } else if (baij) { 74147c6ae99SBarry Smith if (dim == 2) { 7429566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_2d_MPIBAIJ(da, A)); 74347c6ae99SBarry Smith } else if (dim == 3) { 7449566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_3d_MPIBAIJ(da, A)); 74563a3b9bcSJacob 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); 74647c6ae99SBarry Smith } else if (sbaij) { 74747c6ae99SBarry Smith if (dim == 2) { 7489566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_2d_MPISBAIJ(da, A)); 74947c6ae99SBarry Smith } else if (dim == 3) { 7509566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_3d_MPISBAIJ(da, A)); 75163a3b9bcSJacob 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); 752d4002b98SHong Zhang } else if (sell) { 7535e26d47bSHong Zhang if (dim == 2) { 7549566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_2d_MPISELL(da, A)); 755711261dbSHong Zhang } else if (dim == 3) { 7569566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_3d_MPISELL(da, A)); 75763a3b9bcSJacob 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); 758e584696dSStefano Zampini } else if (is) { 7599566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_IS(da, A)); 760869776cdSLisandro Dalcin } else { 76145b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 762e584696dSStefano Zampini 7639566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(A, dof)); 7649566063dSJacob Faibussowitsch PetscCall(MatSetUp(A)); 7659566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 7669566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(A, ltog, ltog)); 76747c6ae99SBarry Smith } 7689566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &starts[0], &starts[1], &starts[2], &dims[0], &dims[1], &dims[2])); 7699566063dSJacob Faibussowitsch PetscCall(MatSetStencil(A, dim, dims, starts, dof)); 7709566063dSJacob Faibussowitsch PetscCall(MatSetDM(A, da)); 7719566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(comm, &size)); 77247c6ae99SBarry Smith if (size > 1) { 77347c6ae99SBarry Smith /* change viewer to display matrix in natural ordering */ 7749566063dSJacob Faibussowitsch PetscCall(MatSetOperation(A, MATOP_VIEW, (void (*)(void))MatView_MPI_DA)); 7759566063dSJacob Faibussowitsch PetscCall(MatSetOperation(A, MATOP_LOAD, (void (*)(void))MatLoad_MPI_DA)); 77647c6ae99SBarry Smith } 77747c6ae99SBarry Smith *J = A; 77847c6ae99SBarry Smith PetscFunctionReturn(0); 77947c6ae99SBarry Smith } 78047c6ae99SBarry Smith 78147c6ae99SBarry Smith /* ---------------------------------------------------------------------------------*/ 782844bd0d7SStefano Zampini PETSC_EXTERN PetscErrorCode MatISSetPreallocation_IS(Mat, PetscInt, const PetscInt[], PetscInt, const PetscInt[]); 783844bd0d7SStefano Zampini 784d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_IS(DM dm, Mat J) 785d71ae5a4SJacob Faibussowitsch { 786e584696dSStefano Zampini DM_DA *da = (DM_DA *)dm->data; 787e432b41dSStefano Zampini Mat lJ, P; 788e584696dSStefano Zampini ISLocalToGlobalMapping ltog; 789e432b41dSStefano Zampini IS is; 790e432b41dSStefano Zampini PetscBT bt; 79105339c03SStefano Zampini const PetscInt *e_loc, *idx; 792e432b41dSStefano Zampini PetscInt i, nel, nen, nv, dof, *gidx, n, N; 793e584696dSStefano Zampini 794e584696dSStefano Zampini /* The l2g map of DMDA has all ghosted nodes, and e_loc is a subset of all the local nodes (including the ghosted) 795e432b41dSStefano Zampini We need to filter out the local indices that are not represented through the DMDAGetElements decomposition */ 796e584696dSStefano Zampini PetscFunctionBegin; 797e584696dSStefano Zampini dof = da->w; 7989566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, dof)); 7999566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(dm, <og)); 800e432b41dSStefano Zampini 801e432b41dSStefano Zampini /* flag local elements indices in local DMDA numbering */ 8029566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingGetSize(ltog, &nv)); 8039566063dSJacob Faibussowitsch PetscCall(PetscBTCreate(nv / dof, &bt)); 8049566063dSJacob Faibussowitsch PetscCall(DMDAGetElements(dm, &nel, &nen, &e_loc)); /* this will throw an error if the stencil type is not DMDA_STENCIL_BOX */ 8059566063dSJacob Faibussowitsch for (i = 0; i < nel * nen; i++) PetscCall(PetscBTSet(bt, e_loc[i])); 806e432b41dSStefano Zampini 807e432b41dSStefano Zampini /* filter out (set to -1) the global indices not used by the local elements */ 8089566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nv / dof, &gidx)); 8099566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingGetBlockIndices(ltog, &idx)); 8109566063dSJacob Faibussowitsch PetscCall(PetscArraycpy(gidx, idx, nv / dof)); 8119566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingRestoreBlockIndices(ltog, &idx)); 8129371c9d4SSatish Balay for (i = 0; i < nv / dof; i++) 8139371c9d4SSatish Balay if (!PetscBTLookup(bt, i)) gidx[i] = -1; 8149566063dSJacob Faibussowitsch PetscCall(PetscBTDestroy(&bt)); 8159566063dSJacob Faibussowitsch PetscCall(ISCreateBlock(PetscObjectComm((PetscObject)dm), dof, nv / dof, gidx, PETSC_OWN_POINTER, &is)); 8169566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingCreateIS(is, <og)); 8179566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 8189566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingDestroy(<og)); 8199566063dSJacob Faibussowitsch PetscCall(ISDestroy(&is)); 82005339c03SStefano Zampini 821e432b41dSStefano Zampini /* Preallocation */ 822e306f867SJed Brown if (dm->prealloc_skip) { 8239566063dSJacob Faibussowitsch PetscCall(MatSetUp(J)); 824e306f867SJed Brown } else { 8259566063dSJacob Faibussowitsch PetscCall(MatISGetLocalMat(J, &lJ)); 8269566063dSJacob Faibussowitsch PetscCall(MatGetLocalToGlobalMapping(lJ, <og, NULL)); 8279566063dSJacob Faibussowitsch PetscCall(MatCreate(PetscObjectComm((PetscObject)lJ), &P)); 8289566063dSJacob Faibussowitsch PetscCall(MatSetType(P, MATPREALLOCATOR)); 8299566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(P, ltog, ltog)); 8309566063dSJacob Faibussowitsch PetscCall(MatGetSize(lJ, &N, NULL)); 8319566063dSJacob Faibussowitsch PetscCall(MatGetLocalSize(lJ, &n, NULL)); 8329566063dSJacob Faibussowitsch PetscCall(MatSetSizes(P, n, n, N, N)); 8339566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(P, dof)); 8349566063dSJacob Faibussowitsch PetscCall(MatSetUp(P)); 83548a46eb9SPierre Jolivet for (i = 0; i < nel; i++) PetscCall(MatSetValuesBlockedLocal(P, nen, e_loc + i * nen, nen, e_loc + i * nen, NULL, INSERT_VALUES)); 8369566063dSJacob Faibussowitsch PetscCall(MatPreallocatorPreallocate(P, (PetscBool)!da->prealloc_only, lJ)); 8379566063dSJacob Faibussowitsch PetscCall(MatISRestoreLocalMat(J, &lJ)); 8389566063dSJacob Faibussowitsch PetscCall(DMDARestoreElements(dm, &nel, &nen, &e_loc)); 8399566063dSJacob Faibussowitsch PetscCall(MatDestroy(&P)); 840e432b41dSStefano Zampini 8419566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 8429566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 843e306f867SJed Brown } 844e584696dSStefano Zampini PetscFunctionReturn(0); 845e584696dSStefano Zampini } 846e584696dSStefano Zampini 847d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPISELL(DM da, Mat J) 848d71ae5a4SJacob Faibussowitsch { 8495e26d47bSHong 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; 8505e26d47bSHong Zhang PetscInt lstart, lend, pstart, pend, *dnz, *onz; 8515e26d47bSHong Zhang MPI_Comm comm; 8525e26d47bSHong Zhang PetscScalar *values; 8535e26d47bSHong Zhang DMBoundaryType bx, by; 8545e26d47bSHong Zhang ISLocalToGlobalMapping ltog; 8555e26d47bSHong Zhang DMDAStencilType st; 8565e26d47bSHong Zhang 8575e26d47bSHong Zhang PetscFunctionBegin; 8585e26d47bSHong Zhang /* 8595e26d47bSHong Zhang nc - number of components per grid point 8605e26d47bSHong Zhang col - number of colors needed in one direction for single component problem 8615e26d47bSHong Zhang 8625e26d47bSHong Zhang */ 8639566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, NULL, NULL, NULL, &nc, &s, &bx, &by, NULL, &st)); 8645e26d47bSHong Zhang col = 2 * s + 1; 8659566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 8669566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 8679566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 8685e26d47bSHong Zhang 8699566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * col * nc * nc, &cols)); 8709566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 8715e26d47bSHong Zhang 8729566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 8735e26d47bSHong Zhang /* determine the matrix preallocation information */ 874d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny, nc * nx * ny, dnz, onz); 8755e26d47bSHong Zhang for (i = xs; i < xs + nx; i++) { 8765e26d47bSHong Zhang pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 8775e26d47bSHong Zhang pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 8785e26d47bSHong Zhang 8795e26d47bSHong Zhang for (j = ys; j < ys + ny; j++) { 8805e26d47bSHong Zhang slot = i - gxs + gnx * (j - gys); 8815e26d47bSHong Zhang 8825e26d47bSHong Zhang lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 8835e26d47bSHong Zhang lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 8845e26d47bSHong Zhang 8855e26d47bSHong Zhang cnt = 0; 8865e26d47bSHong Zhang for (k = 0; k < nc; k++) { 8875e26d47bSHong Zhang for (l = lstart; l < lend + 1; l++) { 8885e26d47bSHong Zhang for (p = pstart; p < pend + 1; p++) { 8895e26d47bSHong Zhang if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star have either l = 0 or p = 0 */ 8905e26d47bSHong Zhang cols[cnt++] = k + nc * (slot + gnx * l + p); 8915e26d47bSHong Zhang } 8925e26d47bSHong Zhang } 8935e26d47bSHong Zhang } 8945e26d47bSHong Zhang rows[k] = k + nc * (slot); 8955e26d47bSHong Zhang } 8969566063dSJacob Faibussowitsch PetscCall(MatPreallocateSetLocal(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 8975e26d47bSHong Zhang } 8985e26d47bSHong Zhang } 8999566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 9009566063dSJacob Faibussowitsch PetscCall(MatSeqSELLSetPreallocation(J, 0, dnz)); 9019566063dSJacob Faibussowitsch PetscCall(MatMPISELLSetPreallocation(J, 0, dnz, 0, onz)); 902d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 9035e26d47bSHong Zhang 9049566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 9055e26d47bSHong Zhang 9065e26d47bSHong Zhang /* 9075e26d47bSHong Zhang For each node in the grid: we get the neighbors in the local (on processor ordering 9085e26d47bSHong Zhang that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 9095e26d47bSHong Zhang PETSc ordering. 9105e26d47bSHong Zhang */ 9115e26d47bSHong Zhang if (!da->prealloc_only) { 9129566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * nc * nc, &values)); 9135e26d47bSHong Zhang for (i = xs; i < xs + nx; i++) { 9145e26d47bSHong Zhang pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 9155e26d47bSHong Zhang pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 9165e26d47bSHong Zhang 9175e26d47bSHong Zhang for (j = ys; j < ys + ny; j++) { 9185e26d47bSHong Zhang slot = i - gxs + gnx * (j - gys); 9195e26d47bSHong Zhang 9205e26d47bSHong Zhang lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 9215e26d47bSHong Zhang lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 9225e26d47bSHong Zhang 9235e26d47bSHong Zhang cnt = 0; 9245e26d47bSHong Zhang for (k = 0; k < nc; k++) { 9255e26d47bSHong Zhang for (l = lstart; l < lend + 1; l++) { 9265e26d47bSHong Zhang for (p = pstart; p < pend + 1; p++) { 9275e26d47bSHong Zhang if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star have either l = 0 or p = 0 */ 9285e26d47bSHong Zhang cols[cnt++] = k + nc * (slot + gnx * l + p); 9295e26d47bSHong Zhang } 9305e26d47bSHong Zhang } 9315e26d47bSHong Zhang } 9325e26d47bSHong Zhang rows[k] = k + nc * (slot); 9335e26d47bSHong Zhang } 9349566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, values, INSERT_VALUES)); 9355e26d47bSHong Zhang } 9365e26d47bSHong Zhang } 9379566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 938e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 9399566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 9409566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 9419566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 9429566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 9439566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 9445e26d47bSHong Zhang } 9459566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 9465e26d47bSHong Zhang PetscFunctionReturn(0); 9475e26d47bSHong Zhang } 9485e26d47bSHong Zhang 949d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPISELL(DM da, Mat J) 950d71ae5a4SJacob Faibussowitsch { 951711261dbSHong Zhang PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 952711261dbSHong Zhang PetscInt m, n, dim, s, *cols = NULL, k, nc, *rows = NULL, col, cnt, l, p, *dnz = NULL, *onz = NULL; 953711261dbSHong Zhang PetscInt istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk, M, N, P; 954711261dbSHong Zhang MPI_Comm comm; 955711261dbSHong Zhang PetscScalar *values; 956711261dbSHong Zhang DMBoundaryType bx, by, bz; 957711261dbSHong Zhang ISLocalToGlobalMapping ltog; 958711261dbSHong Zhang DMDAStencilType st; 959711261dbSHong Zhang 960711261dbSHong Zhang PetscFunctionBegin; 961711261dbSHong Zhang /* 962711261dbSHong Zhang nc - number of components per grid point 963711261dbSHong Zhang col - number of colors needed in one direction for single component problem 964711261dbSHong Zhang 965711261dbSHong Zhang */ 9669566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &nc, &s, &bx, &by, &bz, &st)); 967711261dbSHong Zhang col = 2 * s + 1; 9689566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 9699566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 9709566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 971711261dbSHong Zhang 9729566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * col * col * nc * nc, &cols)); 9739566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 974711261dbSHong Zhang 9759566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 976711261dbSHong Zhang /* determine the matrix preallocation information */ 977d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny * nz, nc * nx * ny * nz, dnz, onz); 978711261dbSHong Zhang for (i = xs; i < xs + nx; i++) { 979711261dbSHong Zhang istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 980711261dbSHong Zhang iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 981711261dbSHong Zhang for (j = ys; j < ys + ny; j++) { 982711261dbSHong Zhang jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 983711261dbSHong Zhang jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 984711261dbSHong Zhang for (k = zs; k < zs + nz; k++) { 985711261dbSHong Zhang kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 986711261dbSHong Zhang kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 987711261dbSHong Zhang 988711261dbSHong Zhang slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 989711261dbSHong Zhang 990711261dbSHong Zhang cnt = 0; 991711261dbSHong Zhang for (l = 0; l < nc; l++) { 992711261dbSHong Zhang for (ii = istart; ii < iend + 1; ii++) { 993711261dbSHong Zhang for (jj = jstart; jj < jend + 1; jj++) { 994711261dbSHong Zhang for (kk = kstart; kk < kend + 1; kk++) { 995711261dbSHong Zhang if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 996711261dbSHong Zhang cols[cnt++] = l + nc * (slot + ii + gnx * jj + gnx * gny * kk); 997711261dbSHong Zhang } 998711261dbSHong Zhang } 999711261dbSHong Zhang } 1000711261dbSHong Zhang } 1001711261dbSHong Zhang rows[l] = l + nc * (slot); 1002711261dbSHong Zhang } 10039566063dSJacob Faibussowitsch PetscCall(MatPreallocateSetLocal(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 1004711261dbSHong Zhang } 1005711261dbSHong Zhang } 1006711261dbSHong Zhang } 10079566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 10089566063dSJacob Faibussowitsch PetscCall(MatSeqSELLSetPreallocation(J, 0, dnz)); 10099566063dSJacob Faibussowitsch PetscCall(MatMPISELLSetPreallocation(J, 0, dnz, 0, onz)); 1010d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 10119566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 1012711261dbSHong Zhang 1013711261dbSHong Zhang /* 1014711261dbSHong Zhang For each node in the grid: we get the neighbors in the local (on processor ordering 1015711261dbSHong Zhang that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 1016711261dbSHong Zhang PETSc ordering. 1017711261dbSHong Zhang */ 1018711261dbSHong Zhang if (!da->prealloc_only) { 10199566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * col * nc * nc * nc, &values)); 1020711261dbSHong Zhang for (i = xs; i < xs + nx; i++) { 1021711261dbSHong Zhang istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1022711261dbSHong Zhang iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 1023711261dbSHong Zhang for (j = ys; j < ys + ny; j++) { 1024711261dbSHong Zhang jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1025711261dbSHong Zhang jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 1026711261dbSHong Zhang for (k = zs; k < zs + nz; k++) { 1027711261dbSHong Zhang kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 1028711261dbSHong Zhang kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 1029711261dbSHong Zhang 1030711261dbSHong Zhang slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 1031711261dbSHong Zhang 1032711261dbSHong Zhang cnt = 0; 1033711261dbSHong Zhang for (l = 0; l < nc; l++) { 1034711261dbSHong Zhang for (ii = istart; ii < iend + 1; ii++) { 1035711261dbSHong Zhang for (jj = jstart; jj < jend + 1; jj++) { 1036711261dbSHong Zhang for (kk = kstart; kk < kend + 1; kk++) { 1037711261dbSHong Zhang if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 1038711261dbSHong Zhang cols[cnt++] = l + nc * (slot + ii + gnx * jj + gnx * gny * kk); 1039711261dbSHong Zhang } 1040711261dbSHong Zhang } 1041711261dbSHong Zhang } 1042711261dbSHong Zhang } 1043711261dbSHong Zhang rows[l] = l + nc * (slot); 1044711261dbSHong Zhang } 10459566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, values, INSERT_VALUES)); 1046711261dbSHong Zhang } 1047711261dbSHong Zhang } 1048711261dbSHong Zhang } 10499566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 1050e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 10519566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 10529566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 10539566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 10549566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 10559566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 1056711261dbSHong Zhang } 10579566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 1058711261dbSHong Zhang PetscFunctionReturn(0); 1059711261dbSHong Zhang } 1060711261dbSHong Zhang 1061d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPIAIJ(DM da, Mat J) 1062d71ae5a4SJacob Faibussowitsch { 1063c1154cd5SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny, m, n, dim, s, *cols = NULL, k, nc, *rows = NULL, col, cnt, l, p, M, N; 106447c6ae99SBarry Smith PetscInt lstart, lend, pstart, pend, *dnz, *onz; 106547c6ae99SBarry Smith MPI_Comm comm; 1066bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 1067844bd0d7SStefano Zampini ISLocalToGlobalMapping ltog, mltog; 1068aa219208SBarry Smith DMDAStencilType st; 1069b294de21SRichard Tran Mills PetscBool removedups = PETSC_FALSE, alreadyboundtocpu = PETSC_TRUE; 107047c6ae99SBarry Smith 107147c6ae99SBarry Smith PetscFunctionBegin; 107247c6ae99SBarry Smith /* 107347c6ae99SBarry Smith nc - number of components per grid point 107447c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 107547c6ae99SBarry Smith 107647c6ae99SBarry Smith */ 1077924e958dSJunchao Zhang PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, &M, &N, NULL, &nc, &s, &bx, &by, NULL, &st)); 10781baa6e33SBarry Smith if (bx == DM_BOUNDARY_NONE && by == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_TRUE)); 107947c6ae99SBarry Smith col = 2 * s + 1; 1080c1154cd5SBarry Smith /* 1081c1154cd5SBarry Smith With one processor in periodic domains in a skinny dimension the code will label nonzero columns multiple times 1082c1154cd5SBarry Smith because of "wrapping" around the end of the domain hitting an entry already counted in the other direction. 1083c1154cd5SBarry Smith */ 1084c1154cd5SBarry Smith if (M == 1 && 2 * s >= m) removedups = PETSC_TRUE; 1085c1154cd5SBarry Smith if (N == 1 && 2 * s >= n) removedups = PETSC_TRUE; 10869566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 10879566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 10889566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 108947c6ae99SBarry Smith 10909566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * col * nc * nc, &cols)); 10919566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 109247c6ae99SBarry Smith 10939566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 109447c6ae99SBarry Smith /* determine the matrix preallocation information */ 1095d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny, nc * nx * ny, dnz, onz); 109647c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1097bff4a2f0SMatthew G. Knepley pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1098bff4a2f0SMatthew G. Knepley pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 109947c6ae99SBarry Smith 110047c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 110147c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 110247c6ae99SBarry Smith 1103bff4a2f0SMatthew G. Knepley lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1104bff4a2f0SMatthew G. Knepley lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 110547c6ae99SBarry Smith 110647c6ae99SBarry Smith cnt = 0; 110747c6ae99SBarry Smith for (k = 0; k < nc; k++) { 110847c6ae99SBarry Smith for (l = lstart; l < lend + 1; l++) { 110947c6ae99SBarry Smith for (p = pstart; p < pend + 1; p++) { 1110aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star have either l = 0 or p = 0 */ 111147c6ae99SBarry Smith cols[cnt++] = k + nc * (slot + gnx * l + p); 111247c6ae99SBarry Smith } 111347c6ae99SBarry Smith } 111447c6ae99SBarry Smith } 111547c6ae99SBarry Smith rows[k] = k + nc * (slot); 111647c6ae99SBarry Smith } 11171baa6e33SBarry Smith if (removedups) PetscCall(MatPreallocateSetLocalRemoveDups(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 11181baa6e33SBarry Smith else PetscCall(MatPreallocateSetLocal(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 111947c6ae99SBarry Smith } 1120c1154cd5SBarry Smith } 11219566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 11229566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz)); 11239566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz)); 1124d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 11259566063dSJacob Faibussowitsch PetscCall(MatGetLocalToGlobalMapping(J, &mltog, NULL)); 11261baa6e33SBarry Smith if (!mltog) PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 112747c6ae99SBarry Smith 112847c6ae99SBarry Smith /* 112947c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 113047c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 113147c6ae99SBarry Smith PETSc ordering. 113247c6ae99SBarry Smith */ 1133fcfd50ebSBarry Smith if (!da->prealloc_only) { 113447c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1135bff4a2f0SMatthew G. Knepley pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1136bff4a2f0SMatthew G. Knepley pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 113747c6ae99SBarry Smith 113847c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 113947c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 114047c6ae99SBarry Smith 1141bff4a2f0SMatthew G. Knepley lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1142bff4a2f0SMatthew G. Knepley lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 114347c6ae99SBarry Smith 114447c6ae99SBarry Smith cnt = 0; 114547c6ae99SBarry Smith for (l = lstart; l < lend + 1; l++) { 114647c6ae99SBarry Smith for (p = pstart; p < pend + 1; p++) { 1147aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star have either l = 0 or p = 0 */ 1148071fcb05SBarry Smith cols[cnt++] = nc * (slot + gnx * l + p); 1149071fcb05SBarry Smith for (k = 1; k < nc; k++) { 11509371c9d4SSatish Balay cols[cnt] = 1 + cols[cnt - 1]; 11519371c9d4SSatish Balay cnt++; 115247c6ae99SBarry Smith } 115347c6ae99SBarry Smith } 115447c6ae99SBarry Smith } 115547c6ae99SBarry Smith } 1156071fcb05SBarry Smith for (k = 0; k < nc; k++) rows[k] = k + nc * (slot); 11579566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, NULL, INSERT_VALUES)); 115847c6ae99SBarry Smith } 115947c6ae99SBarry Smith } 1160e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 11619566063dSJacob Faibussowitsch PetscCall(MatBoundToCPU(J, &alreadyboundtocpu)); 11629566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 11639566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 11649566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 11659566063dSJacob Faibussowitsch if (!alreadyboundtocpu) PetscCall(MatBindToCPU(J, PETSC_FALSE)); 11669566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 11671baa6e33SBarry Smith if (bx == DM_BOUNDARY_NONE && by == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE)); 116847c6ae99SBarry Smith } 11699566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 117047c6ae99SBarry Smith PetscFunctionReturn(0); 117147c6ae99SBarry Smith } 117247c6ae99SBarry Smith 1173d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPIAIJ_Fill(DM da, Mat J) 1174d71ae5a4SJacob Faibussowitsch { 117547c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 1176c1154cd5SBarry Smith PetscInt m, n, dim, s, *cols, k, nc, row, col, cnt, maxcnt = 0, l, p, M, N; 117747c6ae99SBarry Smith PetscInt lstart, lend, pstart, pend, *dnz, *onz; 117847c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 117947c6ae99SBarry Smith PetscInt ifill_col, *ofill = dd->ofill, *dfill = dd->dfill; 118047c6ae99SBarry Smith MPI_Comm comm; 1181bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 118245b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 1183aa219208SBarry Smith DMDAStencilType st; 1184c1154cd5SBarry Smith PetscBool removedups = PETSC_FALSE; 118547c6ae99SBarry Smith 118647c6ae99SBarry Smith PetscFunctionBegin; 118747c6ae99SBarry Smith /* 118847c6ae99SBarry Smith nc - number of components per grid point 118947c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 119047c6ae99SBarry Smith 119147c6ae99SBarry Smith */ 1192924e958dSJunchao Zhang PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, &M, &N, NULL, &nc, &s, &bx, &by, NULL, &st)); 119347c6ae99SBarry Smith col = 2 * s + 1; 1194c1154cd5SBarry Smith /* 1195c1154cd5SBarry Smith With one processor in periodic domains in a skinny dimension the code will label nonzero columns multiple times 1196c1154cd5SBarry Smith because of "wrapping" around the end of the domain hitting an entry already counted in the other direction. 1197c1154cd5SBarry Smith */ 1198c1154cd5SBarry Smith if (M == 1 && 2 * s >= m) removedups = PETSC_TRUE; 1199c1154cd5SBarry Smith if (N == 1 && 2 * s >= n) removedups = PETSC_TRUE; 12009566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 12019566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 12029566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 120347c6ae99SBarry Smith 12049566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * nc, &cols)); 12059566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 120647c6ae99SBarry Smith 12079566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 120847c6ae99SBarry Smith /* determine the matrix preallocation information */ 1209d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny, nc * nx * ny, dnz, onz); 121047c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1211bff4a2f0SMatthew G. Knepley pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1212bff4a2f0SMatthew G. Knepley pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 121347c6ae99SBarry Smith 121447c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 121547c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 121647c6ae99SBarry Smith 1217bff4a2f0SMatthew G. Knepley lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1218bff4a2f0SMatthew G. Knepley lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 121947c6ae99SBarry Smith 122047c6ae99SBarry Smith for (k = 0; k < nc; k++) { 122147c6ae99SBarry Smith cnt = 0; 122247c6ae99SBarry Smith for (l = lstart; l < lend + 1; l++) { 122347c6ae99SBarry Smith for (p = pstart; p < pend + 1; p++) { 122447c6ae99SBarry Smith if (l || p) { 1225aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star */ 12268865f1eaSKarl Rupp for (ifill_col = ofill[k]; ifill_col < ofill[k + 1]; ifill_col++) cols[cnt++] = ofill[ifill_col] + nc * (slot + gnx * l + p); 122747c6ae99SBarry Smith } 122847c6ae99SBarry Smith } else { 122947c6ae99SBarry Smith if (dfill) { 12308865f1eaSKarl Rupp for (ifill_col = dfill[k]; ifill_col < dfill[k + 1]; ifill_col++) cols[cnt++] = dfill[ifill_col] + nc * (slot + gnx * l + p); 123147c6ae99SBarry Smith } else { 12328865f1eaSKarl Rupp for (ifill_col = 0; ifill_col < nc; ifill_col++) cols[cnt++] = ifill_col + nc * (slot + gnx * l + p); 123347c6ae99SBarry Smith } 123447c6ae99SBarry Smith } 123547c6ae99SBarry Smith } 123647c6ae99SBarry Smith } 123747c6ae99SBarry Smith row = k + nc * (slot); 1238c0ab637bSBarry Smith maxcnt = PetscMax(maxcnt, cnt); 12391baa6e33SBarry Smith if (removedups) PetscCall(MatPreallocateSetLocalRemoveDups(ltog, 1, &row, ltog, cnt, cols, dnz, onz)); 12401baa6e33SBarry Smith else PetscCall(MatPreallocateSetLocal(ltog, 1, &row, ltog, cnt, cols, dnz, onz)); 124147c6ae99SBarry Smith } 124247c6ae99SBarry Smith } 1243c1154cd5SBarry Smith } 12449566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz)); 12459566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz)); 1246d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 12479566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 124847c6ae99SBarry Smith 124947c6ae99SBarry Smith /* 125047c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 125147c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 125247c6ae99SBarry Smith PETSc ordering. 125347c6ae99SBarry Smith */ 1254fcfd50ebSBarry Smith if (!da->prealloc_only) { 125547c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1256bff4a2f0SMatthew G. Knepley pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1257bff4a2f0SMatthew G. Knepley pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 125847c6ae99SBarry Smith 125947c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 126047c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 126147c6ae99SBarry Smith 1262bff4a2f0SMatthew G. Knepley lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1263bff4a2f0SMatthew G. Knepley lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 126447c6ae99SBarry Smith 126547c6ae99SBarry Smith for (k = 0; k < nc; k++) { 126647c6ae99SBarry Smith cnt = 0; 126747c6ae99SBarry Smith for (l = lstart; l < lend + 1; l++) { 126847c6ae99SBarry Smith for (p = pstart; p < pend + 1; p++) { 126947c6ae99SBarry Smith if (l || p) { 1270aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star */ 12718865f1eaSKarl Rupp for (ifill_col = ofill[k]; ifill_col < ofill[k + 1]; ifill_col++) cols[cnt++] = ofill[ifill_col] + nc * (slot + gnx * l + p); 127247c6ae99SBarry Smith } 127347c6ae99SBarry Smith } else { 127447c6ae99SBarry Smith if (dfill) { 12758865f1eaSKarl Rupp for (ifill_col = dfill[k]; ifill_col < dfill[k + 1]; ifill_col++) cols[cnt++] = dfill[ifill_col] + nc * (slot + gnx * l + p); 127647c6ae99SBarry Smith } else { 12778865f1eaSKarl Rupp for (ifill_col = 0; ifill_col < nc; ifill_col++) cols[cnt++] = ifill_col + nc * (slot + gnx * l + p); 127847c6ae99SBarry Smith } 127947c6ae99SBarry Smith } 128047c6ae99SBarry Smith } 128147c6ae99SBarry Smith } 128247c6ae99SBarry Smith row = k + nc * (slot); 12839566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, 1, &row, cnt, cols, NULL, INSERT_VALUES)); 128447c6ae99SBarry Smith } 128547c6ae99SBarry Smith } 128647c6ae99SBarry Smith } 1287e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 12889566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 12899566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 12909566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 12919566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 12929566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 129347c6ae99SBarry Smith } 12949566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 129547c6ae99SBarry Smith PetscFunctionReturn(0); 129647c6ae99SBarry Smith } 129747c6ae99SBarry Smith 129847c6ae99SBarry Smith /* ---------------------------------------------------------------------------------*/ 129947c6ae99SBarry Smith 1300d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPIAIJ(DM da, Mat J) 1301d71ae5a4SJacob Faibussowitsch { 130247c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 13030298fd71SBarry Smith PetscInt m, n, dim, s, *cols = NULL, k, nc, *rows = NULL, col, cnt, l, p, *dnz = NULL, *onz = NULL; 1304c1154cd5SBarry Smith PetscInt istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk, M, N, P; 130547c6ae99SBarry Smith MPI_Comm comm; 1306bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by, bz; 1307844bd0d7SStefano Zampini ISLocalToGlobalMapping ltog, mltog; 1308aa219208SBarry Smith DMDAStencilType st; 1309c1154cd5SBarry Smith PetscBool removedups = PETSC_FALSE; 131047c6ae99SBarry Smith 131147c6ae99SBarry Smith PetscFunctionBegin; 131247c6ae99SBarry Smith /* 131347c6ae99SBarry Smith nc - number of components per grid point 131447c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 131547c6ae99SBarry Smith 131647c6ae99SBarry Smith */ 13179566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &nc, &s, &bx, &by, &bz, &st)); 131848a46eb9SPierre Jolivet if (bx == DM_BOUNDARY_NONE && by == DM_BOUNDARY_NONE && bz == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_TRUE)); 131947c6ae99SBarry Smith col = 2 * s + 1; 132047c6ae99SBarry Smith 1321c1154cd5SBarry Smith /* 1322c1154cd5SBarry Smith With one processor in periodic domains in a skinny dimension the code will label nonzero columns multiple times 1323c1154cd5SBarry Smith because of "wrapping" around the end of the domain hitting an entry already counted in the other direction. 1324c1154cd5SBarry Smith */ 1325c1154cd5SBarry Smith if (M == 1 && 2 * s >= m) removedups = PETSC_TRUE; 1326c1154cd5SBarry Smith if (N == 1 && 2 * s >= n) removedups = PETSC_TRUE; 1327c1154cd5SBarry Smith if (P == 1 && 2 * s >= p) removedups = PETSC_TRUE; 1328c1154cd5SBarry Smith 13299566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 13309566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 13319566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 133247c6ae99SBarry Smith 13339566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * col * col * nc * nc, &cols)); 13349566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 133547c6ae99SBarry Smith 13369566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 133747c6ae99SBarry Smith /* determine the matrix preallocation information */ 1338d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny * nz, nc * nx * ny * nz, dnz, onz); 133947c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1340bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1341bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 134247c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1343bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1344bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 134547c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 1346bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 1347bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 134847c6ae99SBarry Smith 134947c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 135047c6ae99SBarry Smith 135147c6ae99SBarry Smith cnt = 0; 135247c6ae99SBarry Smith for (l = 0; l < nc; l++) { 135347c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 135447c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 135547c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 1356aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 135747c6ae99SBarry Smith cols[cnt++] = l + nc * (slot + ii + gnx * jj + gnx * gny * kk); 135847c6ae99SBarry Smith } 135947c6ae99SBarry Smith } 136047c6ae99SBarry Smith } 136147c6ae99SBarry Smith } 136247c6ae99SBarry Smith rows[l] = l + nc * (slot); 136347c6ae99SBarry Smith } 13641baa6e33SBarry Smith if (removedups) PetscCall(MatPreallocateSetLocalRemoveDups(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 13651baa6e33SBarry Smith else PetscCall(MatPreallocateSetLocal(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 136647c6ae99SBarry Smith } 136747c6ae99SBarry Smith } 1368c1154cd5SBarry Smith } 13699566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 13709566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz)); 13719566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz)); 1372d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 13739566063dSJacob Faibussowitsch PetscCall(MatGetLocalToGlobalMapping(J, &mltog, NULL)); 137448a46eb9SPierre Jolivet if (!mltog) PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 137547c6ae99SBarry Smith 137647c6ae99SBarry Smith /* 137747c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 137847c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 137947c6ae99SBarry Smith PETSc ordering. 138047c6ae99SBarry Smith */ 1381fcfd50ebSBarry Smith if (!da->prealloc_only) { 138247c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1383bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1384bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 138547c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1386bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1387bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 138847c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 1389bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 1390bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 139147c6ae99SBarry Smith 139247c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 139347c6ae99SBarry Smith 139447c6ae99SBarry Smith cnt = 0; 139547c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 1396071fcb05SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 1397071fcb05SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 1398aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 1399071fcb05SBarry Smith cols[cnt++] = nc * (slot + ii + gnx * jj + gnx * gny * kk); 1400071fcb05SBarry Smith for (l = 1; l < nc; l++) { 14019371c9d4SSatish Balay cols[cnt] = 1 + cols[cnt - 1]; 14029371c9d4SSatish Balay cnt++; 140347c6ae99SBarry Smith } 140447c6ae99SBarry Smith } 140547c6ae99SBarry Smith } 140647c6ae99SBarry Smith } 140747c6ae99SBarry Smith } 14089371c9d4SSatish Balay rows[0] = nc * (slot); 14099371c9d4SSatish Balay for (l = 1; l < nc; l++) rows[l] = 1 + rows[l - 1]; 14109566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, NULL, INSERT_VALUES)); 141147c6ae99SBarry Smith } 141247c6ae99SBarry Smith } 141347c6ae99SBarry Smith } 1414e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 14159566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 14169566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 14179566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 141848a46eb9SPierre Jolivet if (bx == DM_BOUNDARY_NONE && by == DM_BOUNDARY_NONE && bz == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE)); 14199566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 14209566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 142147c6ae99SBarry Smith } 14229566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 142347c6ae99SBarry Smith PetscFunctionReturn(0); 142447c6ae99SBarry Smith } 142547c6ae99SBarry Smith 142647c6ae99SBarry Smith /* ---------------------------------------------------------------------------------*/ 142747c6ae99SBarry Smith 1428d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_1d_MPIAIJ_Fill(DM da, Mat J) 1429d71ae5a4SJacob Faibussowitsch { 1430ce308e1dSBarry Smith DM_DA *dd = (DM_DA *)da->data; 1431ce308e1dSBarry Smith PetscInt xs, nx, i, j, gxs, gnx, row, k, l; 14328d4c968fSBarry Smith PetscInt m, dim, s, *cols = NULL, nc, cnt, maxcnt = 0, *ocols; 14330acb5bebSBarry Smith PetscInt *ofill = dd->ofill, *dfill = dd->dfill; 1434bff4a2f0SMatthew G. Knepley DMBoundaryType bx; 143545b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 1436ce308e1dSBarry Smith PetscMPIInt rank, size; 1437ce308e1dSBarry Smith 1438ce308e1dSBarry Smith PetscFunctionBegin; 14399566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)da), &rank)); 14409566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)da), &size)); 1441ce308e1dSBarry Smith 1442ce308e1dSBarry Smith /* 1443ce308e1dSBarry Smith nc - number of components per grid point 1444ce308e1dSBarry Smith 1445ce308e1dSBarry Smith */ 14469566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, NULL, NULL, NULL, NULL, NULL, &nc, &s, &bx, NULL, NULL, NULL)); 144708401ef6SPierre Jolivet PetscCheck(s <= 1, PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "Matrix creation for 1d not implemented correctly for stencil width larger than 1"); 14489566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &nx, NULL, NULL)); 14499566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, NULL, NULL, &gnx, NULL, NULL)); 1450ce308e1dSBarry Smith 14519566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 14529566063dSJacob Faibussowitsch PetscCall(PetscCalloc2(nx * nc, &cols, nx * nc, &ocols)); 1453ce308e1dSBarry Smith 1454ce308e1dSBarry Smith /* 1455ce308e1dSBarry Smith note should be smaller for first and last process with no periodic 1456ce308e1dSBarry Smith does not handle dfill 1457ce308e1dSBarry Smith */ 1458ce308e1dSBarry Smith cnt = 0; 1459ce308e1dSBarry Smith /* coupling with process to the left */ 1460ce308e1dSBarry Smith for (i = 0; i < s; i++) { 1461ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 1462dd400576SPatrick Sanan ocols[cnt] = ((rank == 0) ? 0 : (s - i) * (ofill[j + 1] - ofill[j])); 14630acb5bebSBarry Smith cols[cnt] = dfill[j + 1] - dfill[j] + (s + i) * (ofill[j + 1] - ofill[j]); 1464dd400576SPatrick Sanan if (rank == 0 && (dd->bx == DM_BOUNDARY_PERIODIC)) { 1465831644c1SBarry Smith if (size > 1) ocols[cnt] += (s - i) * (ofill[j + 1] - ofill[j]); 1466831644c1SBarry Smith else cols[cnt] += (s - i) * (ofill[j + 1] - ofill[j]); 1467831644c1SBarry Smith } 1468c0ab637bSBarry Smith maxcnt = PetscMax(maxcnt, ocols[cnt] + cols[cnt]); 1469ce308e1dSBarry Smith cnt++; 1470ce308e1dSBarry Smith } 1471ce308e1dSBarry Smith } 1472ce308e1dSBarry Smith for (i = s; i < nx - s; i++) { 1473ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 14740acb5bebSBarry Smith cols[cnt] = dfill[j + 1] - dfill[j] + 2 * s * (ofill[j + 1] - ofill[j]); 1475c0ab637bSBarry Smith maxcnt = PetscMax(maxcnt, ocols[cnt] + cols[cnt]); 1476ce308e1dSBarry Smith cnt++; 1477ce308e1dSBarry Smith } 1478ce308e1dSBarry Smith } 1479ce308e1dSBarry Smith /* coupling with process to the right */ 1480ce308e1dSBarry Smith for (i = nx - s; i < nx; i++) { 1481ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 1482ce308e1dSBarry Smith ocols[cnt] = ((rank == (size - 1)) ? 0 : (i - nx + s + 1) * (ofill[j + 1] - ofill[j])); 14830acb5bebSBarry Smith cols[cnt] = dfill[j + 1] - dfill[j] + (s + nx - i - 1) * (ofill[j + 1] - ofill[j]); 1484831644c1SBarry Smith if ((rank == size - 1) && (dd->bx == DM_BOUNDARY_PERIODIC)) { 1485831644c1SBarry Smith if (size > 1) ocols[cnt] += (i - nx + s + 1) * (ofill[j + 1] - ofill[j]); 1486831644c1SBarry Smith else cols[cnt] += (i - nx + s + 1) * (ofill[j + 1] - ofill[j]); 1487831644c1SBarry Smith } 1488c0ab637bSBarry Smith maxcnt = PetscMax(maxcnt, ocols[cnt] + cols[cnt]); 1489ce308e1dSBarry Smith cnt++; 1490ce308e1dSBarry Smith } 1491ce308e1dSBarry Smith } 1492ce308e1dSBarry Smith 14939566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, 0, cols)); 14949566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, 0, cols, 0, ocols)); 14959566063dSJacob Faibussowitsch PetscCall(PetscFree2(cols, ocols)); 1496ce308e1dSBarry Smith 14979566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 14989566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 1499ce308e1dSBarry Smith 1500ce308e1dSBarry Smith /* 1501ce308e1dSBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 1502ce308e1dSBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 1503ce308e1dSBarry Smith PETSc ordering. 1504ce308e1dSBarry Smith */ 1505ce308e1dSBarry Smith if (!da->prealloc_only) { 15069566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(maxcnt, &cols)); 1507ce308e1dSBarry Smith row = xs * nc; 1508ce308e1dSBarry Smith /* coupling with process to the left */ 1509ce308e1dSBarry Smith for (i = xs; i < xs + s; i++) { 1510ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 1511ce308e1dSBarry Smith cnt = 0; 1512ce308e1dSBarry Smith if (rank) { 1513ce308e1dSBarry Smith for (l = 0; l < s; l++) { 1514ce308e1dSBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i - s + l) * nc + ofill[k]; 1515ce308e1dSBarry Smith } 1516ce308e1dSBarry Smith } 1517dd400576SPatrick Sanan if (rank == 0 && (dd->bx == DM_BOUNDARY_PERIODIC)) { 1518831644c1SBarry Smith for (l = 0; l < s; l++) { 1519831644c1SBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (m + i - s - l) * nc + ofill[k]; 1520831644c1SBarry Smith } 1521831644c1SBarry Smith } 15220acb5bebSBarry Smith if (dfill) { 1523ad540459SPierre Jolivet for (k = dfill[j]; k < dfill[j + 1]; k++) cols[cnt++] = i * nc + dfill[k]; 15240acb5bebSBarry Smith } else { 1525ad540459SPierre Jolivet for (k = 0; k < nc; k++) cols[cnt++] = i * nc + k; 15260acb5bebSBarry Smith } 1527ce308e1dSBarry Smith for (l = 0; l < s; l++) { 1528ce308e1dSBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i + s - l) * nc + ofill[k]; 1529ce308e1dSBarry Smith } 15309566063dSJacob Faibussowitsch PetscCall(MatSetValues(J, 1, &row, cnt, cols, NULL, INSERT_VALUES)); 1531ce308e1dSBarry Smith row++; 1532ce308e1dSBarry Smith } 1533ce308e1dSBarry Smith } 1534ce308e1dSBarry Smith for (i = xs + s; i < xs + nx - s; i++) { 1535ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 1536ce308e1dSBarry Smith cnt = 0; 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 } 15400acb5bebSBarry Smith if (dfill) { 1541ad540459SPierre Jolivet for (k = dfill[j]; k < dfill[j + 1]; k++) cols[cnt++] = i * nc + dfill[k]; 15420acb5bebSBarry Smith } else { 1543ad540459SPierre Jolivet for (k = 0; k < nc; k++) cols[cnt++] = i * nc + k; 15440acb5bebSBarry Smith } 1545ce308e1dSBarry Smith for (l = 0; l < s; l++) { 1546ce308e1dSBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i + s - l) * nc + ofill[k]; 1547ce308e1dSBarry Smith } 15489566063dSJacob Faibussowitsch PetscCall(MatSetValues(J, 1, &row, cnt, cols, NULL, INSERT_VALUES)); 1549ce308e1dSBarry Smith row++; 1550ce308e1dSBarry Smith } 1551ce308e1dSBarry Smith } 1552ce308e1dSBarry Smith /* coupling with process to the right */ 1553ce308e1dSBarry Smith for (i = xs + nx - s; i < xs + nx; i++) { 1554ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 1555ce308e1dSBarry Smith cnt = 0; 1556ce308e1dSBarry Smith for (l = 0; l < s; l++) { 1557ce308e1dSBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i - s + l) * nc + ofill[k]; 1558ce308e1dSBarry Smith } 15590acb5bebSBarry Smith if (dfill) { 1560ad540459SPierre Jolivet for (k = dfill[j]; k < dfill[j + 1]; k++) cols[cnt++] = i * nc + dfill[k]; 15610acb5bebSBarry Smith } else { 1562ad540459SPierre Jolivet for (k = 0; k < nc; k++) cols[cnt++] = i * nc + k; 15630acb5bebSBarry Smith } 1564ce308e1dSBarry Smith if (rank < size - 1) { 1565ce308e1dSBarry Smith for (l = 0; l < s; l++) { 1566ce308e1dSBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i + s - l) * nc + ofill[k]; 1567ce308e1dSBarry Smith } 1568ce308e1dSBarry Smith } 1569831644c1SBarry Smith if ((rank == size - 1) && (dd->bx == DM_BOUNDARY_PERIODIC)) { 1570831644c1SBarry Smith for (l = 0; l < s; l++) { 1571831644c1SBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i - s - l - m + 2) * nc + ofill[k]; 1572831644c1SBarry Smith } 1573831644c1SBarry Smith } 15749566063dSJacob Faibussowitsch PetscCall(MatSetValues(J, 1, &row, cnt, cols, NULL, INSERT_VALUES)); 1575ce308e1dSBarry Smith row++; 1576ce308e1dSBarry Smith } 1577ce308e1dSBarry Smith } 15789566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 1579e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 15809566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 15819566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 15829566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 15839566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 15849566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 1585ce308e1dSBarry Smith } 1586ce308e1dSBarry Smith PetscFunctionReturn(0); 1587ce308e1dSBarry Smith } 1588ce308e1dSBarry Smith 1589ce308e1dSBarry Smith /* ---------------------------------------------------------------------------------*/ 1590ce308e1dSBarry Smith 1591d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_1d_MPIAIJ(DM da, Mat J) 1592d71ae5a4SJacob Faibussowitsch { 159347c6ae99SBarry Smith PetscInt xs, nx, i, i1, slot, gxs, gnx; 15940298fd71SBarry Smith PetscInt m, dim, s, *cols = NULL, nc, *rows = NULL, col, cnt, l; 159547c6ae99SBarry Smith PetscInt istart, iend; 1596bff4a2f0SMatthew G. Knepley DMBoundaryType bx; 1597844bd0d7SStefano Zampini ISLocalToGlobalMapping ltog, mltog; 159847c6ae99SBarry Smith 159947c6ae99SBarry Smith PetscFunctionBegin; 160047c6ae99SBarry Smith /* 160147c6ae99SBarry Smith nc - number of components per grid point 160247c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 160347c6ae99SBarry Smith 160447c6ae99SBarry Smith */ 16059566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, NULL, NULL, NULL, NULL, NULL, &nc, &s, &bx, NULL, NULL, NULL)); 160648a46eb9SPierre Jolivet if (bx == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_TRUE)); 160747c6ae99SBarry Smith col = 2 * s + 1; 160847c6ae99SBarry Smith 16099566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &nx, NULL, NULL)); 16109566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, NULL, NULL, &gnx, NULL, NULL)); 161147c6ae99SBarry Smith 16129566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 16139566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, col * nc, NULL)); 16149566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, col * nc, NULL, col * nc, NULL)); 161547c6ae99SBarry Smith 16169566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 16179566063dSJacob Faibussowitsch PetscCall(MatGetLocalToGlobalMapping(J, &mltog, NULL)); 161848a46eb9SPierre Jolivet if (!mltog) PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 161947c6ae99SBarry Smith 162047c6ae99SBarry Smith /* 162147c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 162247c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 162347c6ae99SBarry Smith PETSc ordering. 162447c6ae99SBarry Smith */ 1625fcfd50ebSBarry Smith if (!da->prealloc_only) { 16269566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * nc * nc, &cols)); 162747c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 162847c6ae99SBarry Smith istart = PetscMax(-s, gxs - i); 162947c6ae99SBarry Smith iend = PetscMin(s, gxs + gnx - i - 1); 163047c6ae99SBarry Smith slot = i - gxs; 163147c6ae99SBarry Smith 163247c6ae99SBarry Smith cnt = 0; 163347c6ae99SBarry Smith for (i1 = istart; i1 < iend + 1; i1++) { 1634071fcb05SBarry Smith cols[cnt++] = nc * (slot + i1); 1635071fcb05SBarry Smith for (l = 1; l < nc; l++) { 16369371c9d4SSatish Balay cols[cnt] = 1 + cols[cnt - 1]; 16379371c9d4SSatish Balay cnt++; 163847c6ae99SBarry Smith } 163947c6ae99SBarry Smith } 16409371c9d4SSatish Balay rows[0] = nc * (slot); 16419371c9d4SSatish Balay for (l = 1; l < nc; l++) rows[l] = 1 + rows[l - 1]; 16429566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, NULL, INSERT_VALUES)); 164347c6ae99SBarry Smith } 1644e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 16459566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 16469566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 16479566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 164848a46eb9SPierre Jolivet if (bx == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE)); 16499566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 16509566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 16519566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 1652ce308e1dSBarry Smith } 165347c6ae99SBarry Smith PetscFunctionReturn(0); 165447c6ae99SBarry Smith } 165547c6ae99SBarry Smith 165619b08ed1SBarry Smith /* ---------------------------------------------------------------------------------*/ 165719b08ed1SBarry Smith 1658d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_1d_SeqAIJ_NoPreallocation(DM da, Mat J) 1659d71ae5a4SJacob Faibussowitsch { 166019b08ed1SBarry Smith PetscInt xs, nx, i, i1, slot, gxs, gnx; 166119b08ed1SBarry Smith PetscInt m, dim, s, *cols = NULL, nc, *rows = NULL, col, cnt, l; 166219b08ed1SBarry Smith PetscInt istart, iend; 166319b08ed1SBarry Smith DMBoundaryType bx; 166419b08ed1SBarry Smith ISLocalToGlobalMapping ltog, mltog; 166519b08ed1SBarry Smith 166619b08ed1SBarry Smith PetscFunctionBegin; 166719b08ed1SBarry Smith /* 166819b08ed1SBarry Smith nc - number of components per grid point 166919b08ed1SBarry Smith col - number of colors needed in one direction for single component problem 167019b08ed1SBarry Smith */ 16719566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, NULL, NULL, NULL, NULL, NULL, &nc, &s, &bx, NULL, NULL, NULL)); 167219b08ed1SBarry Smith col = 2 * s + 1; 167319b08ed1SBarry Smith 16749566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &nx, NULL, NULL)); 16759566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, NULL, NULL, &gnx, NULL, NULL)); 167619b08ed1SBarry Smith 16779566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 16789566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetTotalPreallocation(J, nx * nc * col * nc)); 167919b08ed1SBarry Smith 16809566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 16819566063dSJacob Faibussowitsch PetscCall(MatGetLocalToGlobalMapping(J, &mltog, NULL)); 168248a46eb9SPierre Jolivet if (!mltog) PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 168319b08ed1SBarry Smith 168419b08ed1SBarry Smith /* 168519b08ed1SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 168619b08ed1SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 168719b08ed1SBarry Smith PETSc ordering. 168819b08ed1SBarry Smith */ 168919b08ed1SBarry Smith if (!da->prealloc_only) { 16909566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * nc * nc, &cols)); 169119b08ed1SBarry Smith for (i = xs; i < xs + nx; i++) { 169219b08ed1SBarry Smith istart = PetscMax(-s, gxs - i); 169319b08ed1SBarry Smith iend = PetscMin(s, gxs + gnx - i - 1); 169419b08ed1SBarry Smith slot = i - gxs; 169519b08ed1SBarry Smith 169619b08ed1SBarry Smith cnt = 0; 169719b08ed1SBarry Smith for (i1 = istart; i1 < iend + 1; i1++) { 169819b08ed1SBarry Smith cols[cnt++] = nc * (slot + i1); 169919b08ed1SBarry Smith for (l = 1; l < nc; l++) { 17009371c9d4SSatish Balay cols[cnt] = 1 + cols[cnt - 1]; 17019371c9d4SSatish Balay cnt++; 170219b08ed1SBarry Smith } 170319b08ed1SBarry Smith } 17049371c9d4SSatish Balay rows[0] = nc * (slot); 17059371c9d4SSatish Balay for (l = 1; l < nc; l++) rows[l] = 1 + rows[l - 1]; 17069566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, NULL, INSERT_VALUES)); 170719b08ed1SBarry Smith } 170819b08ed1SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 17099566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 17109566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 17119566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 171248a46eb9SPierre Jolivet if (bx == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE)); 17139566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 17149566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 17159566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 171619b08ed1SBarry Smith } 17179566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE)); 171819b08ed1SBarry Smith PetscFunctionReturn(0); 171919b08ed1SBarry Smith } 172019b08ed1SBarry Smith 1721d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPIBAIJ(DM da, Mat J) 1722d71ae5a4SJacob Faibussowitsch { 172347c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 172447c6ae99SBarry Smith PetscInt m, n, dim, s, *cols, nc, col, cnt, *dnz, *onz; 172547c6ae99SBarry Smith PetscInt istart, iend, jstart, jend, ii, jj; 172647c6ae99SBarry Smith MPI_Comm comm; 172747c6ae99SBarry Smith PetscScalar *values; 1728bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 1729aa219208SBarry Smith DMDAStencilType st; 173045b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 173147c6ae99SBarry Smith 173247c6ae99SBarry Smith PetscFunctionBegin; 173347c6ae99SBarry Smith /* 173447c6ae99SBarry Smith nc - number of components per grid point 173547c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 173647c6ae99SBarry Smith */ 17379566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, NULL, NULL, NULL, &nc, &s, &bx, &by, NULL, &st)); 173847c6ae99SBarry Smith col = 2 * s + 1; 173947c6ae99SBarry Smith 17409566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 17419566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 17429566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 174347c6ae99SBarry Smith 17449566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * nc * nc, &cols)); 174547c6ae99SBarry Smith 17469566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 174747c6ae99SBarry Smith 174847c6ae99SBarry Smith /* determine the matrix preallocation information */ 1749d0609cedSBarry Smith MatPreallocateBegin(comm, nx * ny, nx * ny, dnz, onz); 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 175847c6ae99SBarry Smith /* Find block columns in block row */ 175947c6ae99SBarry Smith cnt = 0; 176047c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 176147c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 1762aa219208SBarry Smith if (st == DMDA_STENCIL_BOX || !ii || !jj) { /* BOX or on the STAR */ 176347c6ae99SBarry Smith cols[cnt++] = slot + ii + gnx * jj; 176447c6ae99SBarry Smith } 176547c6ae99SBarry Smith } 176647c6ae99SBarry Smith } 17679566063dSJacob Faibussowitsch PetscCall(MatPreallocateSetLocalBlock(ltog, 1, &slot, ltog, cnt, cols, dnz, onz)); 176847c6ae99SBarry Smith } 176947c6ae99SBarry Smith } 17709566063dSJacob Faibussowitsch PetscCall(MatSeqBAIJSetPreallocation(J, nc, 0, dnz)); 17719566063dSJacob Faibussowitsch PetscCall(MatMPIBAIJSetPreallocation(J, nc, 0, dnz, 0, onz)); 1772d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 177347c6ae99SBarry Smith 17749566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 177547c6ae99SBarry Smith 177647c6ae99SBarry Smith /* 177747c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 177847c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 177947c6ae99SBarry Smith PETSc ordering. 178047c6ae99SBarry Smith */ 1781fcfd50ebSBarry Smith if (!da->prealloc_only) { 17829566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * nc * nc, &values)); 178347c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1784bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1785bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 178647c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1787bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1788bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 178947c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 179047c6ae99SBarry Smith cnt = 0; 179147c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 179247c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 1793aa219208SBarry Smith if (st == DMDA_STENCIL_BOX || !ii || !jj) { /* BOX or on the STAR */ 179447c6ae99SBarry Smith cols[cnt++] = slot + ii + gnx * jj; 179547c6ae99SBarry Smith } 179647c6ae99SBarry Smith } 179747c6ae99SBarry Smith } 17989566063dSJacob Faibussowitsch PetscCall(MatSetValuesBlockedLocal(J, 1, &slot, cnt, cols, values, INSERT_VALUES)); 179947c6ae99SBarry Smith } 180047c6ae99SBarry Smith } 18019566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 1802e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 18039566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 18049566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 18059566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 18069566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 18079566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 180847c6ae99SBarry Smith } 18099566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 181047c6ae99SBarry Smith PetscFunctionReturn(0); 181147c6ae99SBarry Smith } 181247c6ae99SBarry Smith 1813d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPIBAIJ(DM da, Mat J) 1814d71ae5a4SJacob Faibussowitsch { 181547c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 181647c6ae99SBarry Smith PetscInt m, n, dim, s, *cols, k, nc, col, cnt, p, *dnz, *onz; 181747c6ae99SBarry Smith PetscInt istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk; 181847c6ae99SBarry Smith MPI_Comm comm; 181947c6ae99SBarry Smith PetscScalar *values; 1820bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by, bz; 1821aa219208SBarry Smith DMDAStencilType st; 182245b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 182347c6ae99SBarry Smith 182447c6ae99SBarry Smith PetscFunctionBegin; 182547c6ae99SBarry Smith /* 182647c6ae99SBarry Smith nc - number of components per grid point 182747c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 182847c6ae99SBarry Smith 182947c6ae99SBarry Smith */ 18309566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, NULL, NULL, NULL, &nc, &s, &bx, &by, &bz, &st)); 183147c6ae99SBarry Smith col = 2 * s + 1; 183247c6ae99SBarry Smith 18339566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 18349566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 18359566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 183647c6ae99SBarry Smith 18379566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * col, &cols)); 183847c6ae99SBarry Smith 18399566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 184047c6ae99SBarry Smith 184147c6ae99SBarry Smith /* determine the matrix preallocation information */ 1842d0609cedSBarry Smith MatPreallocateBegin(comm, nx * ny * nz, nx * ny * nz, dnz, onz); 184347c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1844bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1845bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 184647c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1847bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1848bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 184947c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 1850bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 1851bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 185247c6ae99SBarry Smith 185347c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 185447c6ae99SBarry Smith 185547c6ae99SBarry Smith /* Find block columns in block row */ 185647c6ae99SBarry Smith cnt = 0; 185747c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 185847c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 185947c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 1860aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 186147c6ae99SBarry Smith cols[cnt++] = slot + ii + gnx * jj + gnx * gny * kk; 186247c6ae99SBarry Smith } 186347c6ae99SBarry Smith } 186447c6ae99SBarry Smith } 186547c6ae99SBarry Smith } 18669566063dSJacob Faibussowitsch PetscCall(MatPreallocateSetLocalBlock(ltog, 1, &slot, ltog, cnt, cols, dnz, onz)); 186747c6ae99SBarry Smith } 186847c6ae99SBarry Smith } 186947c6ae99SBarry Smith } 18709566063dSJacob Faibussowitsch PetscCall(MatSeqBAIJSetPreallocation(J, nc, 0, dnz)); 18719566063dSJacob Faibussowitsch PetscCall(MatMPIBAIJSetPreallocation(J, nc, 0, dnz, 0, onz)); 1872d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 187347c6ae99SBarry Smith 18749566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 187547c6ae99SBarry Smith 187647c6ae99SBarry Smith /* 187747c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 187847c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 187947c6ae99SBarry Smith PETSc ordering. 188047c6ae99SBarry Smith */ 1881fcfd50ebSBarry Smith if (!da->prealloc_only) { 18829566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * col * nc * nc, &values)); 188347c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1884bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1885bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 188647c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1887bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1888bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 188947c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 1890bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 1891bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 189247c6ae99SBarry Smith 189347c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 189447c6ae99SBarry Smith 189547c6ae99SBarry Smith cnt = 0; 189647c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 189747c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 189847c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 1899aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 190047c6ae99SBarry Smith cols[cnt++] = slot + ii + gnx * jj + gnx * gny * kk; 190147c6ae99SBarry Smith } 190247c6ae99SBarry Smith } 190347c6ae99SBarry Smith } 190447c6ae99SBarry Smith } 19059566063dSJacob Faibussowitsch PetscCall(MatSetValuesBlockedLocal(J, 1, &slot, cnt, cols, values, INSERT_VALUES)); 190647c6ae99SBarry Smith } 190747c6ae99SBarry Smith } 190847c6ae99SBarry Smith } 19099566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 1910e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 19119566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 19129566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 19139566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 19149566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 19159566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 191647c6ae99SBarry Smith } 19179566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 191847c6ae99SBarry Smith PetscFunctionReturn(0); 191947c6ae99SBarry Smith } 192047c6ae99SBarry Smith 192147c6ae99SBarry Smith /* 192247c6ae99SBarry Smith This helper is for of SBAIJ preallocation, to discard the lower-triangular values which are difficult to 192347c6ae99SBarry Smith identify in the local ordering with periodic domain. 192447c6ae99SBarry Smith */ 1925d71ae5a4SJacob Faibussowitsch static PetscErrorCode L2GFilterUpperTriangular(ISLocalToGlobalMapping ltog, PetscInt *row, PetscInt *cnt, PetscInt col[]) 1926d71ae5a4SJacob Faibussowitsch { 192747c6ae99SBarry Smith PetscInt i, n; 192847c6ae99SBarry Smith 192947c6ae99SBarry Smith PetscFunctionBegin; 19309566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingApplyBlock(ltog, 1, row, row)); 19319566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingApplyBlock(ltog, *cnt, col, col)); 193247c6ae99SBarry Smith for (i = 0, n = 0; i < *cnt; i++) { 193347c6ae99SBarry Smith if (col[i] >= *row) col[n++] = col[i]; 193447c6ae99SBarry Smith } 193547c6ae99SBarry Smith *cnt = n; 193647c6ae99SBarry Smith PetscFunctionReturn(0); 193747c6ae99SBarry Smith } 193847c6ae99SBarry Smith 1939d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPISBAIJ(DM da, Mat J) 1940d71ae5a4SJacob Faibussowitsch { 194147c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 194247c6ae99SBarry Smith PetscInt m, n, dim, s, *cols, nc, col, cnt, *dnz, *onz; 194347c6ae99SBarry Smith PetscInt istart, iend, jstart, jend, ii, jj; 194447c6ae99SBarry Smith MPI_Comm comm; 194547c6ae99SBarry Smith PetscScalar *values; 1946bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 1947aa219208SBarry Smith DMDAStencilType st; 194845b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 194947c6ae99SBarry Smith 195047c6ae99SBarry Smith PetscFunctionBegin; 195147c6ae99SBarry Smith /* 195247c6ae99SBarry Smith nc - number of components per grid point 195347c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 195447c6ae99SBarry Smith */ 19559566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, NULL, NULL, NULL, &nc, &s, &bx, &by, NULL, &st)); 195647c6ae99SBarry Smith col = 2 * s + 1; 195747c6ae99SBarry Smith 19589566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 19599566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 19609566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 196147c6ae99SBarry Smith 19629566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * nc * nc, &cols)); 196347c6ae99SBarry Smith 19649566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 196547c6ae99SBarry Smith 196647c6ae99SBarry Smith /* determine the matrix preallocation information */ 1967d0609cedSBarry Smith MatPreallocateBegin(comm, nx * ny, nx * ny, dnz, onz); 196847c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1969bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1970bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 197147c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1972bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1973bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 197447c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 197547c6ae99SBarry Smith 197647c6ae99SBarry Smith /* Find block columns in block row */ 197747c6ae99SBarry Smith cnt = 0; 197847c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 197947c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 1980ad540459SPierre Jolivet if (st == DMDA_STENCIL_BOX || !ii || !jj) cols[cnt++] = slot + ii + gnx * jj; 198147c6ae99SBarry Smith } 198247c6ae99SBarry Smith } 19839566063dSJacob Faibussowitsch PetscCall(L2GFilterUpperTriangular(ltog, &slot, &cnt, cols)); 19849566063dSJacob Faibussowitsch PetscCall(MatPreallocateSymmetricSetBlock(slot, cnt, cols, dnz, onz)); 198547c6ae99SBarry Smith } 198647c6ae99SBarry Smith } 19879566063dSJacob Faibussowitsch PetscCall(MatSeqSBAIJSetPreallocation(J, nc, 0, dnz)); 19889566063dSJacob Faibussowitsch PetscCall(MatMPISBAIJSetPreallocation(J, nc, 0, dnz, 0, onz)); 1989d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 199047c6ae99SBarry Smith 19919566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 199247c6ae99SBarry Smith 199347c6ae99SBarry Smith /* 199447c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 199547c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 199647c6ae99SBarry Smith PETSc ordering. 199747c6ae99SBarry Smith */ 1998fcfd50ebSBarry Smith if (!da->prealloc_only) { 19999566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * nc * nc, &values)); 200047c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 2001bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 2002bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 200347c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 2004bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 2005bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 200647c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 200747c6ae99SBarry Smith 200847c6ae99SBarry Smith /* Find block columns in block row */ 200947c6ae99SBarry Smith cnt = 0; 201047c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 201147c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 2012ad540459SPierre Jolivet if (st == DMDA_STENCIL_BOX || !ii || !jj) cols[cnt++] = slot + ii + gnx * jj; 201347c6ae99SBarry Smith } 201447c6ae99SBarry Smith } 20159566063dSJacob Faibussowitsch PetscCall(L2GFilterUpperTriangular(ltog, &slot, &cnt, cols)); 20169566063dSJacob Faibussowitsch PetscCall(MatSetValuesBlocked(J, 1, &slot, cnt, cols, values, INSERT_VALUES)); 201747c6ae99SBarry Smith } 201847c6ae99SBarry Smith } 20199566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 2020e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 20219566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 20229566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 20239566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 20249566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 20259566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 202647c6ae99SBarry Smith } 20279566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 202847c6ae99SBarry Smith PetscFunctionReturn(0); 202947c6ae99SBarry Smith } 203047c6ae99SBarry Smith 2031d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPISBAIJ(DM da, Mat J) 2032d71ae5a4SJacob Faibussowitsch { 203347c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 203447c6ae99SBarry Smith PetscInt m, n, dim, s, *cols, k, nc, col, cnt, p, *dnz, *onz; 203547c6ae99SBarry Smith PetscInt istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk; 203647c6ae99SBarry Smith MPI_Comm comm; 203747c6ae99SBarry Smith PetscScalar *values; 2038bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by, bz; 2039aa219208SBarry Smith DMDAStencilType st; 204045b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 204147c6ae99SBarry Smith 204247c6ae99SBarry Smith PetscFunctionBegin; 204347c6ae99SBarry Smith /* 204447c6ae99SBarry Smith nc - number of components per grid point 204547c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 204647c6ae99SBarry Smith */ 20479566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, NULL, NULL, NULL, &nc, &s, &bx, &by, &bz, &st)); 204847c6ae99SBarry Smith col = 2 * s + 1; 204947c6ae99SBarry Smith 20509566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 20519566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 20529566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 205347c6ae99SBarry Smith 205447c6ae99SBarry Smith /* create the matrix */ 20559566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * col, &cols)); 205647c6ae99SBarry Smith 20579566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 205847c6ae99SBarry Smith 205947c6ae99SBarry Smith /* determine the matrix preallocation information */ 2060d0609cedSBarry Smith MatPreallocateBegin(comm, nx * ny * nz, nx * ny * nz, dnz, onz); 206147c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 2062bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 2063bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 206447c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 2065bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 2066bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 206747c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 2068bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 2069bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 207047c6ae99SBarry Smith 207147c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 207247c6ae99SBarry Smith 207347c6ae99SBarry Smith /* Find block columns in block row */ 207447c6ae99SBarry Smith cnt = 0; 207547c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 207647c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 207747c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 2078ad540459SPierre Jolivet if ((st == DMDA_STENCIL_BOX) || (!ii && !jj) || (!jj && !kk) || (!ii && !kk)) cols[cnt++] = slot + ii + gnx * jj + gnx * gny * kk; 207947c6ae99SBarry Smith } 208047c6ae99SBarry Smith } 208147c6ae99SBarry Smith } 20829566063dSJacob Faibussowitsch PetscCall(L2GFilterUpperTriangular(ltog, &slot, &cnt, cols)); 20839566063dSJacob Faibussowitsch PetscCall(MatPreallocateSymmetricSetBlock(slot, cnt, cols, dnz, onz)); 208447c6ae99SBarry Smith } 208547c6ae99SBarry Smith } 208647c6ae99SBarry Smith } 20879566063dSJacob Faibussowitsch PetscCall(MatSeqSBAIJSetPreallocation(J, nc, 0, dnz)); 20889566063dSJacob Faibussowitsch PetscCall(MatMPISBAIJSetPreallocation(J, nc, 0, dnz, 0, onz)); 2089d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 209047c6ae99SBarry Smith 20919566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 209247c6ae99SBarry Smith 209347c6ae99SBarry Smith /* 209447c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 209547c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 209647c6ae99SBarry Smith PETSc ordering. 209747c6ae99SBarry Smith */ 2098fcfd50ebSBarry Smith if (!da->prealloc_only) { 20999566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * col * nc * nc, &values)); 210047c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 2101bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 2102bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 210347c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 2104bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 2105bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 210647c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 2107bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 2108bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 210947c6ae99SBarry Smith 211047c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 211147c6ae99SBarry Smith 211247c6ae99SBarry Smith cnt = 0; 211347c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 211447c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 211547c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 2116ad540459SPierre Jolivet if ((st == DMDA_STENCIL_BOX) || (!ii && !jj) || (!jj && !kk) || (!ii && !kk)) cols[cnt++] = slot + ii + gnx * jj + gnx * gny * kk; 211747c6ae99SBarry Smith } 211847c6ae99SBarry Smith } 211947c6ae99SBarry Smith } 21209566063dSJacob Faibussowitsch PetscCall(L2GFilterUpperTriangular(ltog, &slot, &cnt, cols)); 21219566063dSJacob Faibussowitsch PetscCall(MatSetValuesBlocked(J, 1, &slot, cnt, cols, values, INSERT_VALUES)); 212247c6ae99SBarry Smith } 212347c6ae99SBarry Smith } 212447c6ae99SBarry Smith } 21259566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 2126e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 21279566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 21289566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 21299566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 21309566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 21319566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 213247c6ae99SBarry Smith } 21339566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 213447c6ae99SBarry Smith PetscFunctionReturn(0); 213547c6ae99SBarry Smith } 213647c6ae99SBarry Smith 213747c6ae99SBarry Smith /* ---------------------------------------------------------------------------------*/ 213847c6ae99SBarry Smith 2139d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPIAIJ_Fill(DM da, Mat J) 2140d71ae5a4SJacob Faibussowitsch { 214147c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 2142c0ab637bSBarry Smith PetscInt m, n, dim, s, *cols, k, nc, row, col, cnt, maxcnt = 0, l, p, *dnz, *onz; 2143c1154cd5SBarry Smith PetscInt istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk, M, N, P; 214447c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 214547c6ae99SBarry Smith PetscInt ifill_col, *dfill = dd->dfill, *ofill = dd->ofill; 214647c6ae99SBarry Smith MPI_Comm comm; 214747c6ae99SBarry Smith PetscScalar *values; 2148bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by, bz; 214945b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 2150aa219208SBarry Smith DMDAStencilType st; 2151c1154cd5SBarry Smith PetscBool removedups = PETSC_FALSE; 215247c6ae99SBarry Smith 215347c6ae99SBarry Smith PetscFunctionBegin; 215447c6ae99SBarry Smith /* 215547c6ae99SBarry Smith nc - number of components per grid point 215647c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 215747c6ae99SBarry Smith 215847c6ae99SBarry Smith */ 21599566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &nc, &s, &bx, &by, &bz, &st)); 216047c6ae99SBarry Smith col = 2 * s + 1; 21611dca8a05SBarry 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\ 216247c6ae99SBarry Smith by 2*stencil_width + 1\n"); 21631dca8a05SBarry 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\ 216447c6ae99SBarry Smith by 2*stencil_width + 1\n"); 21651dca8a05SBarry 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\ 216647c6ae99SBarry Smith by 2*stencil_width + 1\n"); 216747c6ae99SBarry Smith 2168c1154cd5SBarry Smith /* 2169c1154cd5SBarry Smith With one processor in periodic domains in a skinny dimension the code will label nonzero columns multiple times 2170c1154cd5SBarry Smith because of "wrapping" around the end of the domain hitting an entry already counted in the other direction. 2171c1154cd5SBarry Smith */ 2172c1154cd5SBarry Smith if (M == 1 && 2 * s >= m) removedups = PETSC_TRUE; 2173c1154cd5SBarry Smith if (N == 1 && 2 * s >= n) removedups = PETSC_TRUE; 2174c1154cd5SBarry Smith if (P == 1 && 2 * s >= p) removedups = PETSC_TRUE; 2175c1154cd5SBarry Smith 21769566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 21779566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 21789566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 217947c6ae99SBarry Smith 21809566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * col * nc, &cols)); 21819566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 218247c6ae99SBarry Smith 218347c6ae99SBarry Smith /* determine the matrix preallocation information */ 2184d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny * nz, nc * nx * ny * nz, dnz, onz); 218547c6ae99SBarry Smith 21869566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 218747c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 2188bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 2189bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 219047c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 2191bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 2192bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 219347c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 2194bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 2195bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 219647c6ae99SBarry Smith 219747c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 219847c6ae99SBarry Smith 219947c6ae99SBarry Smith for (l = 0; l < nc; l++) { 220047c6ae99SBarry Smith cnt = 0; 220147c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 220247c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 220347c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 220447c6ae99SBarry Smith if (ii || jj || kk) { 2205aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 22068865f1eaSKarl 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); 220747c6ae99SBarry Smith } 220847c6ae99SBarry Smith } else { 220947c6ae99SBarry Smith if (dfill) { 22108865f1eaSKarl 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); 221147c6ae99SBarry Smith } else { 22128865f1eaSKarl Rupp for (ifill_col = 0; ifill_col < nc; ifill_col++) cols[cnt++] = ifill_col + nc * (slot + ii + gnx * jj + gnx * gny * kk); 221347c6ae99SBarry Smith } 221447c6ae99SBarry Smith } 221547c6ae99SBarry Smith } 221647c6ae99SBarry Smith } 221747c6ae99SBarry Smith } 221847c6ae99SBarry Smith row = l + nc * (slot); 2219c0ab637bSBarry Smith maxcnt = PetscMax(maxcnt, cnt); 22201baa6e33SBarry Smith if (removedups) PetscCall(MatPreallocateSetLocalRemoveDups(ltog, 1, &row, ltog, cnt, cols, dnz, onz)); 22211baa6e33SBarry Smith else PetscCall(MatPreallocateSetLocal(ltog, 1, &row, ltog, cnt, cols, dnz, onz)); 222247c6ae99SBarry Smith } 222347c6ae99SBarry Smith } 222447c6ae99SBarry Smith } 2225c1154cd5SBarry Smith } 22269566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz)); 22279566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz)); 2228d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 22299566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 223047c6ae99SBarry Smith 223147c6ae99SBarry Smith /* 223247c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 223347c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 223447c6ae99SBarry Smith PETSc ordering. 223547c6ae99SBarry Smith */ 2236fcfd50ebSBarry Smith if (!da->prealloc_only) { 22379566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(maxcnt, &values)); 223847c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 2239bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 2240bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 224147c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 2242bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 2243bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 224447c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 2245bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 2246bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 224747c6ae99SBarry Smith 224847c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 224947c6ae99SBarry Smith 225047c6ae99SBarry Smith for (l = 0; l < nc; l++) { 225147c6ae99SBarry Smith cnt = 0; 225247c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 225347c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 225447c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 225547c6ae99SBarry Smith if (ii || jj || kk) { 2256aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 22578865f1eaSKarl 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); 225847c6ae99SBarry Smith } 225947c6ae99SBarry Smith } else { 226047c6ae99SBarry Smith if (dfill) { 22618865f1eaSKarl 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); 226247c6ae99SBarry Smith } else { 22638865f1eaSKarl Rupp for (ifill_col = 0; ifill_col < nc; ifill_col++) cols[cnt++] = ifill_col + nc * (slot + ii + gnx * jj + gnx * gny * kk); 226447c6ae99SBarry Smith } 226547c6ae99SBarry Smith } 226647c6ae99SBarry Smith } 226747c6ae99SBarry Smith } 226847c6ae99SBarry Smith } 226947c6ae99SBarry Smith row = l + nc * (slot); 22709566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, 1, &row, cnt, cols, values, INSERT_VALUES)); 227147c6ae99SBarry Smith } 227247c6ae99SBarry Smith } 227347c6ae99SBarry Smith } 227447c6ae99SBarry Smith } 22759566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 2276e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 22779566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 22789566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 22799566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 22809566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 22819566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 228247c6ae99SBarry Smith } 22839566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 228447c6ae99SBarry Smith PetscFunctionReturn(0); 228547c6ae99SBarry Smith } 2286