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 87*dce8aebaSBarry Smith 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 100*dce8aebaSBarry Smith `MATMPIAIJ` matrix format 10147c6ae99SBarry Smith 10247c6ae99SBarry Smith The format for dfill and ofill is a 2 dimensional dof by dof matrix with 1 entries 10347c6ae99SBarry Smith representing coupling and 0 entries for missing coupling. For example 104*dce8aebaSBarry Smith .vb 105*dce8aebaSBarry Smith dfill[9] = {1, 0, 0, 106*dce8aebaSBarry Smith 1, 1, 0, 107*dce8aebaSBarry Smith 0, 1, 1} 108*dce8aebaSBarry Smith .ve 10947c6ae99SBarry Smith means that row 0 is coupled with only itself in the diagonal block, row 1 is coupled with 11047c6ae99SBarry Smith itself and row 0 (in the diagonal block) and row 2 is coupled with itself and row 1 (in the 11147c6ae99SBarry Smith diagonal block). 11247c6ae99SBarry Smith 113*dce8aebaSBarry Smith `DMDASetGetMatrix()` allows you to provide general code for those more complicated nonzero patterns then 11447c6ae99SBarry Smith can be represented in the dfill, ofill format 11547c6ae99SBarry Smith 11647c6ae99SBarry Smith Contributed by Glenn Hammond 11747c6ae99SBarry Smith 118*dce8aebaSBarry Smith .seealso: `DM`, `DMDA`, `DMCreateMatrix()`, `DMDASetGetMatrix()`, `DMSetMatrixPreallocateOnly()` 11947c6ae99SBarry Smith @*/ 120d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDASetBlockFills(DM da, const PetscInt *dfill, const PetscInt *ofill) 121d71ae5a4SJacob Faibussowitsch { 12247c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 12347c6ae99SBarry Smith 12447c6ae99SBarry Smith PetscFunctionBegin; 12509e28618SBarry Smith /* save the given dfill and ofill information */ 1269566063dSJacob Faibussowitsch PetscCall(DMDASetBlockFills_Private(dfill, dd->w, &dd->dfill)); 1279566063dSJacob Faibussowitsch PetscCall(DMDASetBlockFills_Private(ofill, dd->w, &dd->ofill)); 128ae4f298aSBarry Smith 12909e28618SBarry Smith /* count nonzeros in ofill columns */ 1309566063dSJacob Faibussowitsch PetscCall(DMDASetBlockFills_Private2(dd)); 13109e28618SBarry Smith PetscFunctionReturn(0); 132ae4f298aSBarry Smith } 13309e28618SBarry Smith 13409e28618SBarry Smith /*@ 13509e28618SBarry Smith DMDASetBlockFillsSparse - Sets the fill pattern in each block for a multi-component problem 136*dce8aebaSBarry Smith of the matrix returned by `DMCreateMatrix()`, using sparse representations 13709e28618SBarry Smith of fill patterns. 13809e28618SBarry Smith 139d083f849SBarry Smith Logically Collective on da 14009e28618SBarry Smith 141d8d19677SJose E. Roman Input Parameters: 14209e28618SBarry Smith + da - the distributed array 14309e28618SBarry Smith . dfill - the sparse fill pattern in the diagonal block (may be NULL, means use dense block) 14409e28618SBarry Smith - ofill - the sparse fill pattern in the off-diagonal blocks 14509e28618SBarry Smith 14609e28618SBarry Smith Level: developer 14709e28618SBarry Smith 148*dce8aebaSBarry Smith Notes: 149*dce8aebaSBarry Smith This only makes sense when you are doing multicomponent problems but using the 150*dce8aebaSBarry Smith `MATMPIAIJ` matrix format 15109e28618SBarry Smith 15209e28618SBarry Smith The format for dfill and ofill is a sparse representation of a 15309e28618SBarry Smith dof-by-dof matrix with 1 entries representing coupling and 0 entries 15409e28618SBarry Smith for missing coupling. The sparse representation is a 1 dimensional 15509e28618SBarry Smith array of length nz + dof + 1, where nz is the number of non-zeros in 15609e28618SBarry Smith the matrix. The first dof entries in the array give the 15709e28618SBarry Smith starting array indices of each row's items in the rest of the array, 15860942847SBarry Smith the dof+1st item contains the value nz + dof + 1 (i.e. the entire length of the array) 15909e28618SBarry Smith and the remaining nz items give the column indices of each of 16009e28618SBarry Smith the 1s within the logical 2D matrix. Each row's items within 16109e28618SBarry Smith the array are the column indices of the 1s within that row 16209e28618SBarry Smith of the 2D matrix. PETSc developers may recognize that this is the 163*dce8aebaSBarry Smith same format as that computed by the `DMDASetBlockFills_Private()` 16409e28618SBarry Smith function from a dense 2D matrix representation. 16509e28618SBarry Smith 166*dce8aebaSBarry Smith `DMDASetGetMatrix()` allows you to provide general code for those more complicated nonzero patterns then 16709e28618SBarry Smith can be represented in the dfill, ofill format 16809e28618SBarry Smith 16909e28618SBarry Smith Contributed by Philip C. Roth 17009e28618SBarry Smith 171*dce8aebaSBarry Smith .seealso: `DM`, `DMDA`, `DMDASetBlockFills()`, `DMCreateMatrix()`, `DMDASetGetMatrix()`, `DMSetMatrixPreallocateOnly()` 17209e28618SBarry Smith @*/ 173d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDASetBlockFillsSparse(DM da, const PetscInt *dfillsparse, const PetscInt *ofillsparse) 174d71ae5a4SJacob Faibussowitsch { 17509e28618SBarry Smith DM_DA *dd = (DM_DA *)da->data; 17609e28618SBarry Smith 17709e28618SBarry Smith PetscFunctionBegin; 17809e28618SBarry Smith /* save the given dfill and ofill information */ 1799566063dSJacob Faibussowitsch PetscCall(DMDASetBlockFillsSparse_Private(dfillsparse, dd->w, &dd->dfill)); 1809566063dSJacob Faibussowitsch PetscCall(DMDASetBlockFillsSparse_Private(ofillsparse, dd->w, &dd->ofill)); 18109e28618SBarry Smith 18209e28618SBarry Smith /* count nonzeros in ofill columns */ 1839566063dSJacob Faibussowitsch PetscCall(DMDASetBlockFills_Private2(dd)); 18447c6ae99SBarry Smith PetscFunctionReturn(0); 18547c6ae99SBarry Smith } 18647c6ae99SBarry Smith 187d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA(DM da, ISColoringType ctype, ISColoring *coloring) 188d71ae5a4SJacob Faibussowitsch { 18947c6ae99SBarry Smith PetscInt dim, m, n, p, nc; 190bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by, bz; 19147c6ae99SBarry Smith MPI_Comm comm; 19247c6ae99SBarry Smith PetscMPIInt size; 19347c6ae99SBarry Smith PetscBool isBAIJ; 19447c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 19547c6ae99SBarry Smith 19647c6ae99SBarry Smith PetscFunctionBegin; 19747c6ae99SBarry Smith /* 19847c6ae99SBarry Smith m 19947c6ae99SBarry Smith ------------------------------------------------------ 20047c6ae99SBarry Smith | | 20147c6ae99SBarry Smith | | 20247c6ae99SBarry Smith | ---------------------- | 20347c6ae99SBarry Smith | | | | 20447c6ae99SBarry Smith n | yn | | | 20547c6ae99SBarry Smith | | | | 20647c6ae99SBarry Smith | .--------------------- | 20747c6ae99SBarry Smith | (xs,ys) xn | 20847c6ae99SBarry Smith | . | 20947c6ae99SBarry Smith | (gxs,gys) | 21047c6ae99SBarry Smith | | 21147c6ae99SBarry Smith ----------------------------------------------------- 21247c6ae99SBarry Smith */ 21347c6ae99SBarry Smith 21447c6ae99SBarry Smith /* 21547c6ae99SBarry Smith nc - number of components per grid point 21647c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 21747c6ae99SBarry Smith 21847c6ae99SBarry Smith */ 2199566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, NULL, NULL, NULL, &m, &n, &p, &nc, NULL, &bx, &by, &bz, NULL)); 22047c6ae99SBarry Smith 2219566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 2229566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(comm, &size)); 2235bdb020cSBarry Smith if (ctype == IS_COLORING_LOCAL) { 22447c6ae99SBarry Smith if (size == 1) { 22547c6ae99SBarry Smith ctype = IS_COLORING_GLOBAL; 22647c6ae99SBarry Smith } else if (dim > 1) { 227bff4a2f0SMatthew G. Knepley if ((m == 1 && bx == DM_BOUNDARY_PERIODIC) || (n == 1 && by == DM_BOUNDARY_PERIODIC) || (p == 1 && bz == DM_BOUNDARY_PERIODIC)) { 2285bdb020cSBarry 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"); 22947c6ae99SBarry Smith } 23047c6ae99SBarry Smith } 23147c6ae99SBarry Smith } 23247c6ae99SBarry Smith 233aa219208SBarry Smith /* Tell the DMDA it has 1 degree of freedom per grid point so that the coloring for BAIJ 23447c6ae99SBarry Smith matrices is for the blocks, not the individual matrix elements */ 2359566063dSJacob Faibussowitsch PetscCall(PetscStrbeginswith(da->mattype, MATBAIJ, &isBAIJ)); 2369566063dSJacob Faibussowitsch if (!isBAIJ) PetscCall(PetscStrbeginswith(da->mattype, MATMPIBAIJ, &isBAIJ)); 2379566063dSJacob Faibussowitsch if (!isBAIJ) PetscCall(PetscStrbeginswith(da->mattype, MATSEQBAIJ, &isBAIJ)); 23847c6ae99SBarry Smith if (isBAIJ) { 23947c6ae99SBarry Smith dd->w = 1; 24047c6ae99SBarry Smith dd->xs = dd->xs / nc; 24147c6ae99SBarry Smith dd->xe = dd->xe / nc; 24247c6ae99SBarry Smith dd->Xs = dd->Xs / nc; 24347c6ae99SBarry Smith dd->Xe = dd->Xe / nc; 24447c6ae99SBarry Smith } 24547c6ae99SBarry Smith 24647c6ae99SBarry Smith /* 247aa219208SBarry Smith We do not provide a getcoloring function in the DMDA operations because 2489a1b256bSStefano Zampini the basic DMDA does not know about matrices. We think of DMDA as being 24947c6ae99SBarry Smith more low-level then matrices. 25047c6ae99SBarry Smith */ 2511baa6e33SBarry Smith if (dim == 1) PetscCall(DMCreateColoring_DA_1d_MPIAIJ(da, ctype, coloring)); 2521baa6e33SBarry Smith else if (dim == 2) PetscCall(DMCreateColoring_DA_2d_MPIAIJ(da, ctype, coloring)); 2531baa6e33SBarry Smith else if (dim == 3) PetscCall(DMCreateColoring_DA_3d_MPIAIJ(da, ctype, coloring)); 2541baa6e33SBarry 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); 25547c6ae99SBarry Smith if (isBAIJ) { 25647c6ae99SBarry Smith dd->w = nc; 25747c6ae99SBarry Smith dd->xs = dd->xs * nc; 25847c6ae99SBarry Smith dd->xe = dd->xe * nc; 25947c6ae99SBarry Smith dd->Xs = dd->Xs * nc; 26047c6ae99SBarry Smith dd->Xe = dd->Xe * nc; 26147c6ae99SBarry Smith } 26247c6ae99SBarry Smith PetscFunctionReturn(0); 26347c6ae99SBarry Smith } 26447c6ae99SBarry Smith 26547c6ae99SBarry Smith /* ---------------------------------------------------------------------------------*/ 26647c6ae99SBarry Smith 267d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA_2d_MPIAIJ(DM da, ISColoringType ctype, ISColoring *coloring) 268d71ae5a4SJacob Faibussowitsch { 26947c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, ii, gxs, gys, gnx, gny, m, n, M, N, dim, s, k, nc, col; 27047c6ae99SBarry Smith PetscInt ncolors; 27147c6ae99SBarry Smith MPI_Comm comm; 272bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 273aa219208SBarry Smith DMDAStencilType st; 27447c6ae99SBarry Smith ISColoringValue *colors; 27547c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 27647c6ae99SBarry Smith 27747c6ae99SBarry Smith PetscFunctionBegin; 27847c6ae99SBarry Smith /* 27947c6ae99SBarry Smith nc - number of components per grid point 28047c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 28147c6ae99SBarry Smith 28247c6ae99SBarry Smith */ 2839566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, &M, &N, NULL, &nc, &s, &bx, &by, NULL, &st)); 28447c6ae99SBarry Smith col = 2 * s + 1; 2859566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 2869566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 2879566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 28847c6ae99SBarry Smith 28947c6ae99SBarry Smith /* special case as taught to us by Paul Hovland */ 290aa219208SBarry Smith if (st == DMDA_STENCIL_STAR && s == 1) { 2919566063dSJacob Faibussowitsch PetscCall(DMCreateColoring_DA_2d_5pt_MPIAIJ(da, ctype, coloring)); 29247c6ae99SBarry Smith } else { 29347c6ae99SBarry Smith if (ctype == IS_COLORING_GLOBAL) { 29447c6ae99SBarry Smith if (!dd->localcoloring) { 2959566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * nx * ny, &colors)); 29647c6ae99SBarry Smith ii = 0; 29747c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 29847c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 299ad540459SPierre Jolivet for (k = 0; k < nc; k++) colors[ii++] = k + nc * ((i % col) + col * (j % col)); 30047c6ae99SBarry Smith } 30147c6ae99SBarry Smith } 30247c6ae99SBarry Smith ncolors = nc + nc * (col - 1 + col * (col - 1)); 3039566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * nx * ny, colors, PETSC_OWN_POINTER, &dd->localcoloring)); 30447c6ae99SBarry Smith } 30547c6ae99SBarry Smith *coloring = dd->localcoloring; 3065bdb020cSBarry Smith } else if (ctype == IS_COLORING_LOCAL) { 30747c6ae99SBarry Smith if (!dd->ghostedcoloring) { 3089566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * gnx * gny, &colors)); 30947c6ae99SBarry Smith ii = 0; 31047c6ae99SBarry Smith for (j = gys; j < gys + gny; j++) { 31147c6ae99SBarry Smith for (i = gxs; i < gxs + gnx; i++) { 31247c6ae99SBarry Smith for (k = 0; k < nc; k++) { 31347c6ae99SBarry Smith /* the complicated stuff is to handle periodic boundaries */ 31447c6ae99SBarry Smith colors[ii++] = k + nc * ((SetInRange(i, m) % col) + col * (SetInRange(j, n) % col)); 31547c6ae99SBarry Smith } 31647c6ae99SBarry Smith } 31747c6ae99SBarry Smith } 31847c6ae99SBarry Smith ncolors = nc + nc * (col - 1 + col * (col - 1)); 3199566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * gnx * gny, colors, PETSC_OWN_POINTER, &dd->ghostedcoloring)); 32047c6ae99SBarry Smith /* PetscIntView(ncolors,(PetscInt*)colors,0); */ 32147c6ae99SBarry Smith 3229566063dSJacob Faibussowitsch PetscCall(ISColoringSetType(dd->ghostedcoloring, IS_COLORING_LOCAL)); 32347c6ae99SBarry Smith } 32447c6ae99SBarry Smith *coloring = dd->ghostedcoloring; 32598921bdaSJacob Faibussowitsch } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_ARG_WRONG, "Unknown ISColoringType %d", (int)ctype); 32647c6ae99SBarry Smith } 3279566063dSJacob Faibussowitsch PetscCall(ISColoringReference(*coloring)); 32847c6ae99SBarry Smith PetscFunctionReturn(0); 32947c6ae99SBarry Smith } 33047c6ae99SBarry Smith 33147c6ae99SBarry Smith /* ---------------------------------------------------------------------------------*/ 33247c6ae99SBarry Smith 333d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA_3d_MPIAIJ(DM da, ISColoringType ctype, ISColoring *coloring) 334d71ae5a4SJacob Faibussowitsch { 33547c6ae99SBarry 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; 33647c6ae99SBarry Smith PetscInt ncolors; 33747c6ae99SBarry Smith MPI_Comm comm; 338bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by, bz; 339aa219208SBarry Smith DMDAStencilType st; 34047c6ae99SBarry Smith ISColoringValue *colors; 34147c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 34247c6ae99SBarry Smith 34347c6ae99SBarry Smith PetscFunctionBegin; 34447c6ae99SBarry Smith /* 34547c6ae99SBarry Smith nc - number of components per grid point 34647c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 34747c6ae99SBarry Smith 34847c6ae99SBarry Smith */ 3499566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &nc, &s, &bx, &by, &bz, &st)); 35047c6ae99SBarry Smith col = 2 * s + 1; 3519566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 3529566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 3539566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 35447c6ae99SBarry Smith 35547c6ae99SBarry Smith /* create the coloring */ 35647c6ae99SBarry Smith if (ctype == IS_COLORING_GLOBAL) { 35747c6ae99SBarry Smith if (!dd->localcoloring) { 3589566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * nx * ny * nz, &colors)); 35947c6ae99SBarry Smith ii = 0; 36047c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 36147c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 36247c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 363ad540459SPierre Jolivet for (l = 0; l < nc; l++) colors[ii++] = l + nc * ((i % col) + col * (j % col) + col * col * (k % col)); 36447c6ae99SBarry Smith } 36547c6ae99SBarry Smith } 36647c6ae99SBarry Smith } 36747c6ae99SBarry Smith ncolors = nc + nc * (col - 1 + col * (col - 1) + col * col * (col - 1)); 3689566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * nx * ny * nz, colors, PETSC_OWN_POINTER, &dd->localcoloring)); 36947c6ae99SBarry Smith } 37047c6ae99SBarry Smith *coloring = dd->localcoloring; 3715bdb020cSBarry Smith } else if (ctype == IS_COLORING_LOCAL) { 37247c6ae99SBarry Smith if (!dd->ghostedcoloring) { 3739566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * gnx * gny * gnz, &colors)); 37447c6ae99SBarry Smith ii = 0; 37547c6ae99SBarry Smith for (k = gzs; k < gzs + gnz; k++) { 37647c6ae99SBarry Smith for (j = gys; j < gys + gny; j++) { 37747c6ae99SBarry Smith for (i = gxs; i < gxs + gnx; i++) { 37847c6ae99SBarry Smith for (l = 0; l < nc; l++) { 37947c6ae99SBarry Smith /* the complicated stuff is to handle periodic boundaries */ 38047c6ae99SBarry Smith colors[ii++] = l + nc * ((SetInRange(i, m) % col) + col * (SetInRange(j, n) % col) + col * col * (SetInRange(k, p) % col)); 38147c6ae99SBarry Smith } 38247c6ae99SBarry Smith } 38347c6ae99SBarry Smith } 38447c6ae99SBarry Smith } 38547c6ae99SBarry Smith ncolors = nc + nc * (col - 1 + col * (col - 1) + col * col * (col - 1)); 3869566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * gnx * gny * gnz, colors, PETSC_OWN_POINTER, &dd->ghostedcoloring)); 3879566063dSJacob Faibussowitsch PetscCall(ISColoringSetType(dd->ghostedcoloring, IS_COLORING_LOCAL)); 38847c6ae99SBarry Smith } 38947c6ae99SBarry Smith *coloring = dd->ghostedcoloring; 39098921bdaSJacob Faibussowitsch } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_ARG_WRONG, "Unknown ISColoringType %d", (int)ctype); 3919566063dSJacob Faibussowitsch PetscCall(ISColoringReference(*coloring)); 39247c6ae99SBarry Smith PetscFunctionReturn(0); 39347c6ae99SBarry Smith } 39447c6ae99SBarry Smith 39547c6ae99SBarry Smith /* ---------------------------------------------------------------------------------*/ 39647c6ae99SBarry Smith 397d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA_1d_MPIAIJ(DM da, ISColoringType ctype, ISColoring *coloring) 398d71ae5a4SJacob Faibussowitsch { 39947c6ae99SBarry Smith PetscInt xs, nx, i, i1, gxs, gnx, l, m, M, dim, s, nc, col; 40047c6ae99SBarry Smith PetscInt ncolors; 40147c6ae99SBarry Smith MPI_Comm comm; 402bff4a2f0SMatthew G. Knepley DMBoundaryType bx; 40347c6ae99SBarry Smith ISColoringValue *colors; 40447c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 40547c6ae99SBarry Smith 40647c6ae99SBarry Smith PetscFunctionBegin; 40747c6ae99SBarry Smith /* 40847c6ae99SBarry Smith nc - number of components per grid point 40947c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 41047c6ae99SBarry Smith 41147c6ae99SBarry Smith */ 4129566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, NULL, NULL, &M, NULL, NULL, &nc, &s, &bx, NULL, NULL, NULL)); 41347c6ae99SBarry Smith col = 2 * s + 1; 4149566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &nx, NULL, NULL)); 4159566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, NULL, NULL, &gnx, NULL, NULL)); 4169566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 41747c6ae99SBarry Smith 41847c6ae99SBarry Smith /* create the coloring */ 41947c6ae99SBarry Smith if (ctype == IS_COLORING_GLOBAL) { 42047c6ae99SBarry Smith if (!dd->localcoloring) { 4219566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * nx, &colors)); 422ae4f298aSBarry Smith if (dd->ofillcols) { 423ae4f298aSBarry Smith PetscInt tc = 0; 424ae4f298aSBarry Smith for (i = 0; i < nc; i++) tc += (PetscInt)(dd->ofillcols[i] > 0); 425ae4f298aSBarry Smith i1 = 0; 426ae4f298aSBarry Smith for (i = xs; i < xs + nx; i++) { 427ae4f298aSBarry Smith for (l = 0; l < nc; l++) { 428ae4f298aSBarry Smith if (dd->ofillcols[l] && (i % col)) { 429ae4f298aSBarry Smith colors[i1++] = nc - 1 + tc * ((i % col) - 1) + dd->ofillcols[l]; 430ae4f298aSBarry Smith } else { 431ae4f298aSBarry Smith colors[i1++] = l; 432ae4f298aSBarry Smith } 433ae4f298aSBarry Smith } 434ae4f298aSBarry Smith } 435ae4f298aSBarry Smith ncolors = nc + 2 * s * tc; 436ae4f298aSBarry Smith } else { 43747c6ae99SBarry Smith i1 = 0; 43847c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 439ad540459SPierre Jolivet for (l = 0; l < nc; l++) colors[i1++] = l + nc * (i % col); 44047c6ae99SBarry Smith } 44147c6ae99SBarry Smith ncolors = nc + nc * (col - 1); 442ae4f298aSBarry Smith } 4439566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * nx, colors, PETSC_OWN_POINTER, &dd->localcoloring)); 44447c6ae99SBarry Smith } 44547c6ae99SBarry Smith *coloring = dd->localcoloring; 4465bdb020cSBarry Smith } else if (ctype == IS_COLORING_LOCAL) { 44747c6ae99SBarry Smith if (!dd->ghostedcoloring) { 4489566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * gnx, &colors)); 44947c6ae99SBarry Smith i1 = 0; 45047c6ae99SBarry Smith for (i = gxs; i < gxs + gnx; i++) { 45147c6ae99SBarry Smith for (l = 0; l < nc; l++) { 45247c6ae99SBarry Smith /* the complicated stuff is to handle periodic boundaries */ 45347c6ae99SBarry Smith colors[i1++] = l + nc * (SetInRange(i, m) % col); 45447c6ae99SBarry Smith } 45547c6ae99SBarry Smith } 45647c6ae99SBarry Smith ncolors = nc + nc * (col - 1); 4579566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * gnx, colors, PETSC_OWN_POINTER, &dd->ghostedcoloring)); 4589566063dSJacob Faibussowitsch PetscCall(ISColoringSetType(dd->ghostedcoloring, IS_COLORING_LOCAL)); 45947c6ae99SBarry Smith } 46047c6ae99SBarry Smith *coloring = dd->ghostedcoloring; 46198921bdaSJacob Faibussowitsch } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_ARG_WRONG, "Unknown ISColoringType %d", (int)ctype); 4629566063dSJacob Faibussowitsch PetscCall(ISColoringReference(*coloring)); 46347c6ae99SBarry Smith PetscFunctionReturn(0); 46447c6ae99SBarry Smith } 46547c6ae99SBarry Smith 466d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA_2d_5pt_MPIAIJ(DM da, ISColoringType ctype, ISColoring *coloring) 467d71ae5a4SJacob Faibussowitsch { 46847c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, ii, gxs, gys, gnx, gny, m, n, dim, s, k, nc; 46947c6ae99SBarry Smith PetscInt ncolors; 47047c6ae99SBarry Smith MPI_Comm comm; 471bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 47247c6ae99SBarry Smith ISColoringValue *colors; 47347c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 47447c6ae99SBarry Smith 47547c6ae99SBarry Smith PetscFunctionBegin; 47647c6ae99SBarry Smith /* 47747c6ae99SBarry Smith nc - number of components per grid point 47847c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 47947c6ae99SBarry Smith 48047c6ae99SBarry Smith */ 4819566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, NULL, NULL, NULL, &nc, &s, &bx, &by, NULL, NULL)); 4829566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 4839566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 4849566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 48547c6ae99SBarry Smith /* create the coloring */ 48647c6ae99SBarry Smith if (ctype == IS_COLORING_GLOBAL) { 48747c6ae99SBarry Smith if (!dd->localcoloring) { 4889566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * nx * ny, &colors)); 48947c6ae99SBarry Smith ii = 0; 49047c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 49147c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 492ad540459SPierre Jolivet for (k = 0; k < nc; k++) colors[ii++] = k + nc * ((3 * j + i) % 5); 49347c6ae99SBarry Smith } 49447c6ae99SBarry Smith } 49547c6ae99SBarry Smith ncolors = 5 * nc; 4969566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * nx * ny, colors, PETSC_OWN_POINTER, &dd->localcoloring)); 49747c6ae99SBarry Smith } 49847c6ae99SBarry Smith *coloring = dd->localcoloring; 4995bdb020cSBarry Smith } else if (ctype == IS_COLORING_LOCAL) { 50047c6ae99SBarry Smith if (!dd->ghostedcoloring) { 5019566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * gnx * gny, &colors)); 50247c6ae99SBarry Smith ii = 0; 50347c6ae99SBarry Smith for (j = gys; j < gys + gny; j++) { 50447c6ae99SBarry Smith for (i = gxs; i < gxs + gnx; i++) { 505ad540459SPierre Jolivet for (k = 0; k < nc; k++) colors[ii++] = k + nc * ((3 * SetInRange(j, n) + SetInRange(i, m)) % 5); 50647c6ae99SBarry Smith } 50747c6ae99SBarry Smith } 50847c6ae99SBarry Smith ncolors = 5 * nc; 5099566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * gnx * gny, colors, PETSC_OWN_POINTER, &dd->ghostedcoloring)); 5109566063dSJacob Faibussowitsch PetscCall(ISColoringSetType(dd->ghostedcoloring, IS_COLORING_LOCAL)); 51147c6ae99SBarry Smith } 51247c6ae99SBarry Smith *coloring = dd->ghostedcoloring; 51398921bdaSJacob Faibussowitsch } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_ARG_WRONG, "Unknown ISColoringType %d", (int)ctype); 51447c6ae99SBarry Smith PetscFunctionReturn(0); 51547c6ae99SBarry Smith } 51647c6ae99SBarry Smith 51747c6ae99SBarry Smith /* =========================================================================== */ 518e432b41dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_1d_MPIAIJ(DM, Mat); 519ce308e1dSBarry Smith extern PetscErrorCode DMCreateMatrix_DA_1d_MPIAIJ_Fill(DM, Mat); 520e432b41dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_1d_SeqAIJ_NoPreallocation(DM, Mat); 521e432b41dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_2d_MPIAIJ(DM, Mat); 522950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_2d_MPIAIJ_Fill(DM, Mat); 523e432b41dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_3d_MPIAIJ(DM, Mat); 524950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_3d_MPIAIJ_Fill(DM, Mat); 525950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_2d_MPIBAIJ(DM, Mat); 526950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_3d_MPIBAIJ(DM, Mat); 527950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_2d_MPISBAIJ(DM, Mat); 528950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_3d_MPISBAIJ(DM, Mat); 529d4002b98SHong Zhang extern PetscErrorCode DMCreateMatrix_DA_2d_MPISELL(DM, Mat); 530d4002b98SHong Zhang extern PetscErrorCode DMCreateMatrix_DA_3d_MPISELL(DM, Mat); 531e584696dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_IS(DM, Mat); 53247c6ae99SBarry Smith 5338bbdbebaSMatthew G Knepley /*@C 534*dce8aebaSBarry Smith MatSetupDM - Sets the `DMDA` that is to be used by the HYPRE_StructMatrix PETSc matrix 53547c6ae99SBarry Smith 536d083f849SBarry Smith Logically Collective on mat 53747c6ae99SBarry Smith 53847c6ae99SBarry Smith Input Parameters: 53947c6ae99SBarry Smith + mat - the matrix 54047c6ae99SBarry Smith - da - the da 54147c6ae99SBarry Smith 54247c6ae99SBarry Smith Level: intermediate 54347c6ae99SBarry Smith 544*dce8aebaSBarry Smith .seealso: `Mat`, `MatSetUp()` 54547c6ae99SBarry Smith @*/ 546d71ae5a4SJacob Faibussowitsch PetscErrorCode MatSetupDM(Mat mat, DM da) 547d71ae5a4SJacob Faibussowitsch { 54847c6ae99SBarry Smith PetscFunctionBegin; 54947c6ae99SBarry Smith PetscValidHeaderSpecific(mat, MAT_CLASSID, 1); 550064a246eSJacob Faibussowitsch PetscValidHeaderSpecificType(da, DM_CLASSID, 2, DMDA); 551cac4c232SBarry Smith PetscTryMethod(mat, "MatSetupDM_C", (Mat, DM), (mat, da)); 55247c6ae99SBarry Smith PetscFunctionReturn(0); 55347c6ae99SBarry Smith } 55447c6ae99SBarry Smith 555d71ae5a4SJacob Faibussowitsch PetscErrorCode MatView_MPI_DA(Mat A, PetscViewer viewer) 556d71ae5a4SJacob Faibussowitsch { 5579a42bb27SBarry Smith DM da; 55847c6ae99SBarry Smith const char *prefix; 55947c6ae99SBarry Smith Mat Anatural; 56047c6ae99SBarry Smith AO ao; 56147c6ae99SBarry Smith PetscInt rstart, rend, *petsc, i; 56247c6ae99SBarry Smith IS is; 56347c6ae99SBarry Smith MPI_Comm comm; 56474388724SJed Brown PetscViewerFormat format; 56547c6ae99SBarry Smith 56647c6ae99SBarry Smith PetscFunctionBegin; 56774388724SJed Brown /* Check whether we are just printing info, in which case MatView() already viewed everything we wanted to view */ 5689566063dSJacob Faibussowitsch PetscCall(PetscViewerGetFormat(viewer, &format)); 56974388724SJed Brown if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) PetscFunctionReturn(0); 57074388724SJed Brown 5719566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)A, &comm)); 5729566063dSJacob Faibussowitsch PetscCall(MatGetDM(A, &da)); 5737a8be351SBarry Smith PetscCheck(da, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Matrix not generated from a DMDA"); 57447c6ae99SBarry Smith 5759566063dSJacob Faibussowitsch PetscCall(DMDAGetAO(da, &ao)); 5769566063dSJacob Faibussowitsch PetscCall(MatGetOwnershipRange(A, &rstart, &rend)); 5779566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(rend - rstart, &petsc)); 57847c6ae99SBarry Smith for (i = rstart; i < rend; i++) petsc[i - rstart] = i; 5799566063dSJacob Faibussowitsch PetscCall(AOApplicationToPetsc(ao, rend - rstart, petsc)); 5809566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(comm, rend - rstart, petsc, PETSC_OWN_POINTER, &is)); 58147c6ae99SBarry Smith 58247c6ae99SBarry Smith /* call viewer on natural ordering */ 5839566063dSJacob Faibussowitsch PetscCall(MatCreateSubMatrix(A, is, is, MAT_INITIAL_MATRIX, &Anatural)); 5849566063dSJacob Faibussowitsch PetscCall(ISDestroy(&is)); 5859566063dSJacob Faibussowitsch PetscCall(PetscObjectGetOptionsPrefix((PetscObject)A, &prefix)); 5869566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptionsPrefix((PetscObject)Anatural, prefix)); 5879566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)Anatural, ((PetscObject)A)->name)); 588f0ed2f47SStefano Zampini ((PetscObject)Anatural)->donotPetscObjectPrintClassNamePrefixType = PETSC_TRUE; 5899566063dSJacob Faibussowitsch PetscCall(MatView(Anatural, viewer)); 590f0ed2f47SStefano Zampini ((PetscObject)Anatural)->donotPetscObjectPrintClassNamePrefixType = PETSC_FALSE; 5919566063dSJacob Faibussowitsch PetscCall(MatDestroy(&Anatural)); 59247c6ae99SBarry Smith PetscFunctionReturn(0); 59347c6ae99SBarry Smith } 59447c6ae99SBarry Smith 595d71ae5a4SJacob Faibussowitsch PetscErrorCode MatLoad_MPI_DA(Mat A, PetscViewer viewer) 596d71ae5a4SJacob Faibussowitsch { 5979a42bb27SBarry Smith DM da; 59847c6ae99SBarry Smith Mat Anatural, Aapp; 59947c6ae99SBarry Smith AO ao; 600539c167fSBarry Smith PetscInt rstart, rend, *app, i, m, n, M, N; 60147c6ae99SBarry Smith IS is; 60247c6ae99SBarry Smith MPI_Comm comm; 60347c6ae99SBarry Smith 60447c6ae99SBarry Smith PetscFunctionBegin; 6059566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)A, &comm)); 6069566063dSJacob Faibussowitsch PetscCall(MatGetDM(A, &da)); 6077a8be351SBarry Smith PetscCheck(da, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Matrix not generated from a DMDA"); 60847c6ae99SBarry Smith 60947c6ae99SBarry Smith /* Load the matrix in natural ordering */ 6109566063dSJacob Faibussowitsch PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &Anatural)); 6119566063dSJacob Faibussowitsch PetscCall(MatSetType(Anatural, ((PetscObject)A)->type_name)); 6129566063dSJacob Faibussowitsch PetscCall(MatGetSize(A, &M, &N)); 6139566063dSJacob Faibussowitsch PetscCall(MatGetLocalSize(A, &m, &n)); 6149566063dSJacob Faibussowitsch PetscCall(MatSetSizes(Anatural, m, n, M, N)); 6159566063dSJacob Faibussowitsch PetscCall(MatLoad(Anatural, viewer)); 61647c6ae99SBarry Smith 61747c6ae99SBarry Smith /* Map natural ordering to application ordering and create IS */ 6189566063dSJacob Faibussowitsch PetscCall(DMDAGetAO(da, &ao)); 6199566063dSJacob Faibussowitsch PetscCall(MatGetOwnershipRange(Anatural, &rstart, &rend)); 6209566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(rend - rstart, &app)); 62147c6ae99SBarry Smith for (i = rstart; i < rend; i++) app[i - rstart] = i; 6229566063dSJacob Faibussowitsch PetscCall(AOPetscToApplication(ao, rend - rstart, app)); 6239566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(comm, rend - rstart, app, PETSC_OWN_POINTER, &is)); 62447c6ae99SBarry Smith 62547c6ae99SBarry Smith /* Do permutation and replace header */ 6269566063dSJacob Faibussowitsch PetscCall(MatCreateSubMatrix(Anatural, is, is, MAT_INITIAL_MATRIX, &Aapp)); 6279566063dSJacob Faibussowitsch PetscCall(MatHeaderReplace(A, &Aapp)); 6289566063dSJacob Faibussowitsch PetscCall(ISDestroy(&is)); 6299566063dSJacob Faibussowitsch PetscCall(MatDestroy(&Anatural)); 63047c6ae99SBarry Smith PetscFunctionReturn(0); 63147c6ae99SBarry Smith } 63247c6ae99SBarry Smith 633d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA(DM da, Mat *J) 634d71ae5a4SJacob Faibussowitsch { 63547c6ae99SBarry Smith PetscInt dim, dof, nx, ny, nz, dims[3], starts[3], M, N, P; 63647c6ae99SBarry Smith Mat A; 63747c6ae99SBarry Smith MPI_Comm comm; 63819fd82e9SBarry Smith MatType Atype; 639e584696dSStefano Zampini void (*aij)(void) = NULL, (*baij)(void) = NULL, (*sbaij)(void) = NULL, (*sell)(void) = NULL, (*is)(void) = NULL; 640b412c318SBarry Smith MatType mtype; 64147c6ae99SBarry Smith PetscMPIInt size; 64247c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 64347c6ae99SBarry Smith 64447c6ae99SBarry Smith PetscFunctionBegin; 6459566063dSJacob Faibussowitsch PetscCall(MatInitializePackage()); 646b412c318SBarry Smith mtype = da->mattype; 64747c6ae99SBarry Smith 64847c6ae99SBarry Smith /* 64947c6ae99SBarry Smith m 65047c6ae99SBarry Smith ------------------------------------------------------ 65147c6ae99SBarry Smith | | 65247c6ae99SBarry Smith | | 65347c6ae99SBarry Smith | ---------------------- | 65447c6ae99SBarry Smith | | | | 65547c6ae99SBarry Smith n | ny | | | 65647c6ae99SBarry Smith | | | | 65747c6ae99SBarry Smith | .--------------------- | 65847c6ae99SBarry Smith | (xs,ys) nx | 65947c6ae99SBarry Smith | . | 66047c6ae99SBarry Smith | (gxs,gys) | 66147c6ae99SBarry Smith | | 66247c6ae99SBarry Smith ----------------------------------------------------- 66347c6ae99SBarry Smith */ 66447c6ae99SBarry Smith 66547c6ae99SBarry Smith /* 66647c6ae99SBarry Smith nc - number of components per grid point 66747c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 66847c6ae99SBarry Smith 66947c6ae99SBarry Smith */ 670e30e807fSPeter Brune M = dd->M; 671e30e807fSPeter Brune N = dd->N; 672e30e807fSPeter Brune P = dd->P; 673c73cfb54SMatthew G. Knepley dim = da->dim; 674e30e807fSPeter Brune dof = dd->w; 6759566063dSJacob Faibussowitsch /* PetscCall(DMDAGetInfo(da,&dim,&M,&N,&P,NULL,NULL,NULL,&dof,NULL,NULL,NULL,NULL,NULL)); */ 6769566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, NULL, NULL, NULL, &nx, &ny, &nz)); 6779566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 6789566063dSJacob Faibussowitsch PetscCall(MatCreate(comm, &A)); 6799566063dSJacob Faibussowitsch PetscCall(MatSetSizes(A, dof * nx * ny * nz, dof * nx * ny * nz, dof * M * N * P, dof * M * N * P)); 6809566063dSJacob Faibussowitsch PetscCall(MatSetType(A, mtype)); 6819566063dSJacob Faibussowitsch PetscCall(MatSetFromOptions(A)); 68274427ab1SRichard Tran Mills if (dof * nx * ny * nz < da->bind_below) { 6839566063dSJacob Faibussowitsch PetscCall(MatSetBindingPropagates(A, PETSC_TRUE)); 6849566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(A, PETSC_TRUE)); 68574427ab1SRichard Tran Mills } 6869566063dSJacob Faibussowitsch PetscCall(MatSetDM(A, da)); 6871baa6e33SBarry Smith if (da->structure_only) PetscCall(MatSetOption(A, MAT_STRUCTURE_ONLY, PETSC_TRUE)); 6889566063dSJacob Faibussowitsch PetscCall(MatGetType(A, &Atype)); 68947c6ae99SBarry Smith /* 690aa219208SBarry Smith We do not provide a getmatrix function in the DMDA operations because 691aa219208SBarry Smith the basic DMDA does not know about matrices. We think of DMDA as being more 69247c6ae99SBarry Smith more low-level than matrices. This is kind of cheating but, cause sometimes 693aa219208SBarry Smith we think of DMDA has higher level than matrices. 69447c6ae99SBarry Smith 69547c6ae99SBarry Smith We could switch based on Atype (or mtype), but we do not since the 696844bd0d7SStefano Zampini specialized setting routines depend only on the particular preallocation 69747c6ae99SBarry Smith details of the matrix, not the type itself. 69847c6ae99SBarry Smith */ 6999566063dSJacob Faibussowitsch PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatMPIAIJSetPreallocation_C", &aij)); 70048a46eb9SPierre Jolivet if (!aij) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatSeqAIJSetPreallocation_C", &aij)); 70147c6ae99SBarry Smith if (!aij) { 7029566063dSJacob Faibussowitsch PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatMPIBAIJSetPreallocation_C", &baij)); 70348a46eb9SPierre Jolivet if (!baij) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatSeqBAIJSetPreallocation_C", &baij)); 70447c6ae99SBarry Smith if (!baij) { 7059566063dSJacob Faibussowitsch PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatMPISBAIJSetPreallocation_C", &sbaij)); 70648a46eb9SPierre Jolivet if (!sbaij) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatSeqSBAIJSetPreallocation_C", &sbaij)); 7075e26d47bSHong Zhang if (!sbaij) { 7089566063dSJacob Faibussowitsch PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatMPISELLSetPreallocation_C", &sell)); 70948a46eb9SPierre Jolivet if (!sell) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatSeqSELLSetPreallocation_C", &sell)); 7105e26d47bSHong Zhang } 71148a46eb9SPierre Jolivet if (!sell) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatISSetPreallocation_C", &is)); 71247c6ae99SBarry Smith } 71347c6ae99SBarry Smith } 71447c6ae99SBarry Smith if (aij) { 71547c6ae99SBarry Smith if (dim == 1) { 716ce308e1dSBarry Smith if (dd->ofill) { 7179566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_1d_MPIAIJ_Fill(da, A)); 718ce308e1dSBarry Smith } else { 71919b08ed1SBarry Smith DMBoundaryType bx; 72019b08ed1SBarry Smith PetscMPIInt size; 7219566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &bx, NULL, NULL, NULL)); 7229566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)da), &size)); 72319b08ed1SBarry Smith if (size == 1 && bx == DM_BOUNDARY_NONE) { 7249566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_1d_SeqAIJ_NoPreallocation(da, A)); 72519b08ed1SBarry Smith } else { 7269566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_1d_MPIAIJ(da, A)); 727ce308e1dSBarry Smith } 72819b08ed1SBarry Smith } 72947c6ae99SBarry Smith } else if (dim == 2) { 73047c6ae99SBarry Smith if (dd->ofill) { 7319566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_2d_MPIAIJ_Fill(da, A)); 73247c6ae99SBarry Smith } else { 7339566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_2d_MPIAIJ(da, A)); 73447c6ae99SBarry Smith } 73547c6ae99SBarry Smith } else if (dim == 3) { 73647c6ae99SBarry Smith if (dd->ofill) { 7379566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_3d_MPIAIJ_Fill(da, A)); 73847c6ae99SBarry Smith } else { 7399566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_3d_MPIAIJ(da, A)); 74047c6ae99SBarry Smith } 74147c6ae99SBarry Smith } 74247c6ae99SBarry Smith } else if (baij) { 74347c6ae99SBarry Smith if (dim == 2) { 7449566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_2d_MPIBAIJ(da, A)); 74547c6ae99SBarry Smith } else if (dim == 3) { 7469566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_3d_MPIBAIJ(da, A)); 74763a3b9bcSJacob 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); 74847c6ae99SBarry Smith } else if (sbaij) { 74947c6ae99SBarry Smith if (dim == 2) { 7509566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_2d_MPISBAIJ(da, A)); 75147c6ae99SBarry Smith } else if (dim == 3) { 7529566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_3d_MPISBAIJ(da, A)); 75363a3b9bcSJacob 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); 754d4002b98SHong Zhang } else if (sell) { 7555e26d47bSHong Zhang if (dim == 2) { 7569566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_2d_MPISELL(da, A)); 757711261dbSHong Zhang } else if (dim == 3) { 7589566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_3d_MPISELL(da, A)); 75963a3b9bcSJacob 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); 760e584696dSStefano Zampini } else if (is) { 7619566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_IS(da, A)); 762869776cdSLisandro Dalcin } else { 76345b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 764e584696dSStefano Zampini 7659566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(A, dof)); 7669566063dSJacob Faibussowitsch PetscCall(MatSetUp(A)); 7679566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 7689566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(A, ltog, ltog)); 76947c6ae99SBarry Smith } 7709566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &starts[0], &starts[1], &starts[2], &dims[0], &dims[1], &dims[2])); 7719566063dSJacob Faibussowitsch PetscCall(MatSetStencil(A, dim, dims, starts, dof)); 7729566063dSJacob Faibussowitsch PetscCall(MatSetDM(A, da)); 7739566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(comm, &size)); 77447c6ae99SBarry Smith if (size > 1) { 77547c6ae99SBarry Smith /* change viewer to display matrix in natural ordering */ 7769566063dSJacob Faibussowitsch PetscCall(MatSetOperation(A, MATOP_VIEW, (void (*)(void))MatView_MPI_DA)); 7779566063dSJacob Faibussowitsch PetscCall(MatSetOperation(A, MATOP_LOAD, (void (*)(void))MatLoad_MPI_DA)); 77847c6ae99SBarry Smith } 77947c6ae99SBarry Smith *J = A; 78047c6ae99SBarry Smith PetscFunctionReturn(0); 78147c6ae99SBarry Smith } 78247c6ae99SBarry Smith 78347c6ae99SBarry Smith /* ---------------------------------------------------------------------------------*/ 784844bd0d7SStefano Zampini PETSC_EXTERN PetscErrorCode MatISSetPreallocation_IS(Mat, PetscInt, const PetscInt[], PetscInt, const PetscInt[]); 785844bd0d7SStefano Zampini 786d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_IS(DM dm, Mat J) 787d71ae5a4SJacob Faibussowitsch { 788e584696dSStefano Zampini DM_DA *da = (DM_DA *)dm->data; 789e432b41dSStefano Zampini Mat lJ, P; 790e584696dSStefano Zampini ISLocalToGlobalMapping ltog; 791e432b41dSStefano Zampini IS is; 792e432b41dSStefano Zampini PetscBT bt; 79305339c03SStefano Zampini const PetscInt *e_loc, *idx; 794e432b41dSStefano Zampini PetscInt i, nel, nen, nv, dof, *gidx, n, N; 795e584696dSStefano Zampini 796e584696dSStefano Zampini /* The l2g map of DMDA has all ghosted nodes, and e_loc is a subset of all the local nodes (including the ghosted) 797e432b41dSStefano Zampini We need to filter out the local indices that are not represented through the DMDAGetElements decomposition */ 798e584696dSStefano Zampini PetscFunctionBegin; 799e584696dSStefano Zampini dof = da->w; 8009566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, dof)); 8019566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(dm, <og)); 802e432b41dSStefano Zampini 803e432b41dSStefano Zampini /* flag local elements indices in local DMDA numbering */ 8049566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingGetSize(ltog, &nv)); 8059566063dSJacob Faibussowitsch PetscCall(PetscBTCreate(nv / dof, &bt)); 8069566063dSJacob Faibussowitsch PetscCall(DMDAGetElements(dm, &nel, &nen, &e_loc)); /* this will throw an error if the stencil type is not DMDA_STENCIL_BOX */ 8079566063dSJacob Faibussowitsch for (i = 0; i < nel * nen; i++) PetscCall(PetscBTSet(bt, e_loc[i])); 808e432b41dSStefano Zampini 809e432b41dSStefano Zampini /* filter out (set to -1) the global indices not used by the local elements */ 8109566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nv / dof, &gidx)); 8119566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingGetBlockIndices(ltog, &idx)); 8129566063dSJacob Faibussowitsch PetscCall(PetscArraycpy(gidx, idx, nv / dof)); 8139566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingRestoreBlockIndices(ltog, &idx)); 8149371c9d4SSatish Balay for (i = 0; i < nv / dof; i++) 8159371c9d4SSatish Balay if (!PetscBTLookup(bt, i)) gidx[i] = -1; 8169566063dSJacob Faibussowitsch PetscCall(PetscBTDestroy(&bt)); 8179566063dSJacob Faibussowitsch PetscCall(ISCreateBlock(PetscObjectComm((PetscObject)dm), dof, nv / dof, gidx, PETSC_OWN_POINTER, &is)); 8189566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingCreateIS(is, <og)); 8199566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 8209566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingDestroy(<og)); 8219566063dSJacob Faibussowitsch PetscCall(ISDestroy(&is)); 82205339c03SStefano Zampini 823e432b41dSStefano Zampini /* Preallocation */ 824e306f867SJed Brown if (dm->prealloc_skip) { 8259566063dSJacob Faibussowitsch PetscCall(MatSetUp(J)); 826e306f867SJed Brown } else { 8279566063dSJacob Faibussowitsch PetscCall(MatISGetLocalMat(J, &lJ)); 8289566063dSJacob Faibussowitsch PetscCall(MatGetLocalToGlobalMapping(lJ, <og, NULL)); 8299566063dSJacob Faibussowitsch PetscCall(MatCreate(PetscObjectComm((PetscObject)lJ), &P)); 8309566063dSJacob Faibussowitsch PetscCall(MatSetType(P, MATPREALLOCATOR)); 8319566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(P, ltog, ltog)); 8329566063dSJacob Faibussowitsch PetscCall(MatGetSize(lJ, &N, NULL)); 8339566063dSJacob Faibussowitsch PetscCall(MatGetLocalSize(lJ, &n, NULL)); 8349566063dSJacob Faibussowitsch PetscCall(MatSetSizes(P, n, n, N, N)); 8359566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(P, dof)); 8369566063dSJacob Faibussowitsch PetscCall(MatSetUp(P)); 83748a46eb9SPierre Jolivet for (i = 0; i < nel; i++) PetscCall(MatSetValuesBlockedLocal(P, nen, e_loc + i * nen, nen, e_loc + i * nen, NULL, INSERT_VALUES)); 8389566063dSJacob Faibussowitsch PetscCall(MatPreallocatorPreallocate(P, (PetscBool)!da->prealloc_only, lJ)); 8399566063dSJacob Faibussowitsch PetscCall(MatISRestoreLocalMat(J, &lJ)); 8409566063dSJacob Faibussowitsch PetscCall(DMDARestoreElements(dm, &nel, &nen, &e_loc)); 8419566063dSJacob Faibussowitsch PetscCall(MatDestroy(&P)); 842e432b41dSStefano Zampini 8439566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 8449566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 845e306f867SJed Brown } 846e584696dSStefano Zampini PetscFunctionReturn(0); 847e584696dSStefano Zampini } 848e584696dSStefano Zampini 849d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPISELL(DM da, Mat J) 850d71ae5a4SJacob Faibussowitsch { 8515e26d47bSHong Zhang PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny, m, n, dim, s, *cols = NULL, k, nc, *rows = NULL, col, cnt, l, p; 8525e26d47bSHong Zhang PetscInt lstart, lend, pstart, pend, *dnz, *onz; 8535e26d47bSHong Zhang MPI_Comm comm; 8545e26d47bSHong Zhang PetscScalar *values; 8555e26d47bSHong Zhang DMBoundaryType bx, by; 8565e26d47bSHong Zhang ISLocalToGlobalMapping ltog; 8575e26d47bSHong Zhang DMDAStencilType st; 8585e26d47bSHong Zhang 8595e26d47bSHong Zhang PetscFunctionBegin; 8605e26d47bSHong Zhang /* 8615e26d47bSHong Zhang nc - number of components per grid point 8625e26d47bSHong Zhang col - number of colors needed in one direction for single component problem 8635e26d47bSHong Zhang 8645e26d47bSHong Zhang */ 8659566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, NULL, NULL, NULL, &nc, &s, &bx, &by, NULL, &st)); 8665e26d47bSHong Zhang col = 2 * s + 1; 8679566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 8689566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 8699566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 8705e26d47bSHong Zhang 8719566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * col * nc * nc, &cols)); 8729566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 8735e26d47bSHong Zhang 8749566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 8755e26d47bSHong Zhang /* determine the matrix preallocation information */ 876d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny, nc * nx * ny, dnz, onz); 8775e26d47bSHong Zhang for (i = xs; i < xs + nx; i++) { 8785e26d47bSHong Zhang pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 8795e26d47bSHong Zhang pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 8805e26d47bSHong Zhang 8815e26d47bSHong Zhang for (j = ys; j < ys + ny; j++) { 8825e26d47bSHong Zhang slot = i - gxs + gnx * (j - gys); 8835e26d47bSHong Zhang 8845e26d47bSHong Zhang lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 8855e26d47bSHong Zhang lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 8865e26d47bSHong Zhang 8875e26d47bSHong Zhang cnt = 0; 8885e26d47bSHong Zhang for (k = 0; k < nc; k++) { 8895e26d47bSHong Zhang for (l = lstart; l < lend + 1; l++) { 8905e26d47bSHong Zhang for (p = pstart; p < pend + 1; p++) { 8915e26d47bSHong Zhang if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star have either l = 0 or p = 0 */ 8925e26d47bSHong Zhang cols[cnt++] = k + nc * (slot + gnx * l + p); 8935e26d47bSHong Zhang } 8945e26d47bSHong Zhang } 8955e26d47bSHong Zhang } 8965e26d47bSHong Zhang rows[k] = k + nc * (slot); 8975e26d47bSHong Zhang } 8989566063dSJacob Faibussowitsch PetscCall(MatPreallocateSetLocal(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 8995e26d47bSHong Zhang } 9005e26d47bSHong Zhang } 9019566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 9029566063dSJacob Faibussowitsch PetscCall(MatSeqSELLSetPreallocation(J, 0, dnz)); 9039566063dSJacob Faibussowitsch PetscCall(MatMPISELLSetPreallocation(J, 0, dnz, 0, onz)); 904d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 9055e26d47bSHong Zhang 9069566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 9075e26d47bSHong Zhang 9085e26d47bSHong Zhang /* 9095e26d47bSHong Zhang For each node in the grid: we get the neighbors in the local (on processor ordering 9105e26d47bSHong Zhang that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 9115e26d47bSHong Zhang PETSc ordering. 9125e26d47bSHong Zhang */ 9135e26d47bSHong Zhang if (!da->prealloc_only) { 9149566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * nc * nc, &values)); 9155e26d47bSHong Zhang for (i = xs; i < xs + nx; i++) { 9165e26d47bSHong Zhang pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 9175e26d47bSHong Zhang pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 9185e26d47bSHong Zhang 9195e26d47bSHong Zhang for (j = ys; j < ys + ny; j++) { 9205e26d47bSHong Zhang slot = i - gxs + gnx * (j - gys); 9215e26d47bSHong Zhang 9225e26d47bSHong Zhang lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 9235e26d47bSHong Zhang lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 9245e26d47bSHong Zhang 9255e26d47bSHong Zhang cnt = 0; 9265e26d47bSHong Zhang for (k = 0; k < nc; k++) { 9275e26d47bSHong Zhang for (l = lstart; l < lend + 1; l++) { 9285e26d47bSHong Zhang for (p = pstart; p < pend + 1; p++) { 9295e26d47bSHong Zhang if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star have either l = 0 or p = 0 */ 9305e26d47bSHong Zhang cols[cnt++] = k + nc * (slot + gnx * l + p); 9315e26d47bSHong Zhang } 9325e26d47bSHong Zhang } 9335e26d47bSHong Zhang } 9345e26d47bSHong Zhang rows[k] = k + nc * (slot); 9355e26d47bSHong Zhang } 9369566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, values, INSERT_VALUES)); 9375e26d47bSHong Zhang } 9385e26d47bSHong Zhang } 9399566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 940e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 9419566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 9429566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 9439566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 9449566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 9459566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 9465e26d47bSHong Zhang } 9479566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 9485e26d47bSHong Zhang PetscFunctionReturn(0); 9495e26d47bSHong Zhang } 9505e26d47bSHong Zhang 951d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPISELL(DM da, Mat J) 952d71ae5a4SJacob Faibussowitsch { 953711261dbSHong Zhang PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 954711261dbSHong Zhang PetscInt m, n, dim, s, *cols = NULL, k, nc, *rows = NULL, col, cnt, l, p, *dnz = NULL, *onz = NULL; 955711261dbSHong Zhang PetscInt istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk, M, N, P; 956711261dbSHong Zhang MPI_Comm comm; 957711261dbSHong Zhang PetscScalar *values; 958711261dbSHong Zhang DMBoundaryType bx, by, bz; 959711261dbSHong Zhang ISLocalToGlobalMapping ltog; 960711261dbSHong Zhang DMDAStencilType st; 961711261dbSHong Zhang 962711261dbSHong Zhang PetscFunctionBegin; 963711261dbSHong Zhang /* 964711261dbSHong Zhang nc - number of components per grid point 965711261dbSHong Zhang col - number of colors needed in one direction for single component problem 966711261dbSHong Zhang 967711261dbSHong Zhang */ 9689566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &nc, &s, &bx, &by, &bz, &st)); 969711261dbSHong Zhang col = 2 * s + 1; 9709566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 9719566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 9729566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 973711261dbSHong Zhang 9749566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * col * col * nc * nc, &cols)); 9759566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 976711261dbSHong Zhang 9779566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 978711261dbSHong Zhang /* determine the matrix preallocation information */ 979d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny * nz, nc * nx * ny * nz, dnz, onz); 980711261dbSHong Zhang for (i = xs; i < xs + nx; i++) { 981711261dbSHong Zhang istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 982711261dbSHong Zhang iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 983711261dbSHong Zhang for (j = ys; j < ys + ny; j++) { 984711261dbSHong Zhang jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 985711261dbSHong Zhang jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 986711261dbSHong Zhang for (k = zs; k < zs + nz; k++) { 987711261dbSHong Zhang kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 988711261dbSHong Zhang kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 989711261dbSHong Zhang 990711261dbSHong Zhang slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 991711261dbSHong Zhang 992711261dbSHong Zhang cnt = 0; 993711261dbSHong Zhang for (l = 0; l < nc; l++) { 994711261dbSHong Zhang for (ii = istart; ii < iend + 1; ii++) { 995711261dbSHong Zhang for (jj = jstart; jj < jend + 1; jj++) { 996711261dbSHong Zhang for (kk = kstart; kk < kend + 1; kk++) { 997711261dbSHong Zhang if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 998711261dbSHong Zhang cols[cnt++] = l + nc * (slot + ii + gnx * jj + gnx * gny * kk); 999711261dbSHong Zhang } 1000711261dbSHong Zhang } 1001711261dbSHong Zhang } 1002711261dbSHong Zhang } 1003711261dbSHong Zhang rows[l] = l + nc * (slot); 1004711261dbSHong Zhang } 10059566063dSJacob Faibussowitsch PetscCall(MatPreallocateSetLocal(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 1006711261dbSHong Zhang } 1007711261dbSHong Zhang } 1008711261dbSHong Zhang } 10099566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 10109566063dSJacob Faibussowitsch PetscCall(MatSeqSELLSetPreallocation(J, 0, dnz)); 10119566063dSJacob Faibussowitsch PetscCall(MatMPISELLSetPreallocation(J, 0, dnz, 0, onz)); 1012d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 10139566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 1014711261dbSHong Zhang 1015711261dbSHong Zhang /* 1016711261dbSHong Zhang For each node in the grid: we get the neighbors in the local (on processor ordering 1017711261dbSHong Zhang that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 1018711261dbSHong Zhang PETSc ordering. 1019711261dbSHong Zhang */ 1020711261dbSHong Zhang if (!da->prealloc_only) { 10219566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * col * nc * nc * nc, &values)); 1022711261dbSHong Zhang for (i = xs; i < xs + nx; i++) { 1023711261dbSHong Zhang istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1024711261dbSHong Zhang iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 1025711261dbSHong Zhang for (j = ys; j < ys + ny; j++) { 1026711261dbSHong Zhang jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1027711261dbSHong Zhang jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 1028711261dbSHong Zhang for (k = zs; k < zs + nz; k++) { 1029711261dbSHong Zhang kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 1030711261dbSHong Zhang kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 1031711261dbSHong Zhang 1032711261dbSHong Zhang slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 1033711261dbSHong Zhang 1034711261dbSHong Zhang cnt = 0; 1035711261dbSHong Zhang for (l = 0; l < nc; l++) { 1036711261dbSHong Zhang for (ii = istart; ii < iend + 1; ii++) { 1037711261dbSHong Zhang for (jj = jstart; jj < jend + 1; jj++) { 1038711261dbSHong Zhang for (kk = kstart; kk < kend + 1; kk++) { 1039711261dbSHong Zhang if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 1040711261dbSHong Zhang cols[cnt++] = l + nc * (slot + ii + gnx * jj + gnx * gny * kk); 1041711261dbSHong Zhang } 1042711261dbSHong Zhang } 1043711261dbSHong Zhang } 1044711261dbSHong Zhang } 1045711261dbSHong Zhang rows[l] = l + nc * (slot); 1046711261dbSHong Zhang } 10479566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, values, INSERT_VALUES)); 1048711261dbSHong Zhang } 1049711261dbSHong Zhang } 1050711261dbSHong Zhang } 10519566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 1052e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 10539566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 10549566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 10559566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 10569566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 10579566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 1058711261dbSHong Zhang } 10599566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 1060711261dbSHong Zhang PetscFunctionReturn(0); 1061711261dbSHong Zhang } 1062711261dbSHong Zhang 1063d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPIAIJ(DM da, Mat J) 1064d71ae5a4SJacob Faibussowitsch { 1065c1154cd5SBarry 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; 106647c6ae99SBarry Smith PetscInt lstart, lend, pstart, pend, *dnz, *onz; 106747c6ae99SBarry Smith MPI_Comm comm; 1068bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 1069844bd0d7SStefano Zampini ISLocalToGlobalMapping ltog, mltog; 1070aa219208SBarry Smith DMDAStencilType st; 1071b294de21SRichard Tran Mills PetscBool removedups = PETSC_FALSE, alreadyboundtocpu = PETSC_TRUE; 107247c6ae99SBarry Smith 107347c6ae99SBarry Smith PetscFunctionBegin; 107447c6ae99SBarry Smith /* 107547c6ae99SBarry Smith nc - number of components per grid point 107647c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 107747c6ae99SBarry Smith 107847c6ae99SBarry Smith */ 1079924e958dSJunchao Zhang PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, &M, &N, NULL, &nc, &s, &bx, &by, NULL, &st)); 10801baa6e33SBarry Smith if (bx == DM_BOUNDARY_NONE && by == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_TRUE)); 108147c6ae99SBarry Smith col = 2 * s + 1; 1082c1154cd5SBarry Smith /* 1083c1154cd5SBarry Smith With one processor in periodic domains in a skinny dimension the code will label nonzero columns multiple times 1084c1154cd5SBarry Smith because of "wrapping" around the end of the domain hitting an entry already counted in the other direction. 1085c1154cd5SBarry Smith */ 1086c1154cd5SBarry Smith if (M == 1 && 2 * s >= m) removedups = PETSC_TRUE; 1087c1154cd5SBarry Smith if (N == 1 && 2 * s >= n) removedups = PETSC_TRUE; 10889566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 10899566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 10909566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 109147c6ae99SBarry Smith 10929566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * col * nc * nc, &cols)); 10939566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 109447c6ae99SBarry Smith 10959566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 109647c6ae99SBarry Smith /* determine the matrix preallocation information */ 1097d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny, nc * nx * ny, dnz, onz); 109847c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1099bff4a2f0SMatthew G. Knepley pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1100bff4a2f0SMatthew G. Knepley pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 110147c6ae99SBarry Smith 110247c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 110347c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 110447c6ae99SBarry Smith 1105bff4a2f0SMatthew G. Knepley lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1106bff4a2f0SMatthew G. Knepley lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 110747c6ae99SBarry Smith 110847c6ae99SBarry Smith cnt = 0; 110947c6ae99SBarry Smith for (k = 0; k < nc; k++) { 111047c6ae99SBarry Smith for (l = lstart; l < lend + 1; l++) { 111147c6ae99SBarry Smith for (p = pstart; p < pend + 1; p++) { 1112aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star have either l = 0 or p = 0 */ 111347c6ae99SBarry Smith cols[cnt++] = k + nc * (slot + gnx * l + p); 111447c6ae99SBarry Smith } 111547c6ae99SBarry Smith } 111647c6ae99SBarry Smith } 111747c6ae99SBarry Smith rows[k] = k + nc * (slot); 111847c6ae99SBarry Smith } 11191baa6e33SBarry Smith if (removedups) PetscCall(MatPreallocateSetLocalRemoveDups(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 11201baa6e33SBarry Smith else PetscCall(MatPreallocateSetLocal(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 112147c6ae99SBarry Smith } 1122c1154cd5SBarry Smith } 11239566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 11249566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz)); 11259566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz)); 1126d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 11279566063dSJacob Faibussowitsch PetscCall(MatGetLocalToGlobalMapping(J, &mltog, NULL)); 11281baa6e33SBarry Smith if (!mltog) PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 112947c6ae99SBarry Smith 113047c6ae99SBarry Smith /* 113147c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 113247c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 113347c6ae99SBarry Smith PETSc ordering. 113447c6ae99SBarry Smith */ 1135fcfd50ebSBarry Smith if (!da->prealloc_only) { 113647c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1137bff4a2f0SMatthew G. Knepley pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1138bff4a2f0SMatthew G. Knepley pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 113947c6ae99SBarry Smith 114047c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 114147c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 114247c6ae99SBarry Smith 1143bff4a2f0SMatthew G. Knepley lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1144bff4a2f0SMatthew G. Knepley lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 114547c6ae99SBarry Smith 114647c6ae99SBarry Smith cnt = 0; 114747c6ae99SBarry Smith for (l = lstart; l < lend + 1; l++) { 114847c6ae99SBarry Smith for (p = pstart; p < pend + 1; p++) { 1149aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star have either l = 0 or p = 0 */ 1150071fcb05SBarry Smith cols[cnt++] = nc * (slot + gnx * l + p); 1151071fcb05SBarry Smith for (k = 1; k < nc; k++) { 11529371c9d4SSatish Balay cols[cnt] = 1 + cols[cnt - 1]; 11539371c9d4SSatish Balay cnt++; 115447c6ae99SBarry Smith } 115547c6ae99SBarry Smith } 115647c6ae99SBarry Smith } 115747c6ae99SBarry Smith } 1158071fcb05SBarry Smith for (k = 0; k < nc; k++) rows[k] = k + nc * (slot); 11599566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, NULL, INSERT_VALUES)); 116047c6ae99SBarry Smith } 116147c6ae99SBarry Smith } 1162e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 11639566063dSJacob Faibussowitsch PetscCall(MatBoundToCPU(J, &alreadyboundtocpu)); 11649566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 11659566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 11669566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 11679566063dSJacob Faibussowitsch if (!alreadyboundtocpu) PetscCall(MatBindToCPU(J, PETSC_FALSE)); 11689566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 11691baa6e33SBarry Smith if (bx == DM_BOUNDARY_NONE && by == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE)); 117047c6ae99SBarry Smith } 11719566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 117247c6ae99SBarry Smith PetscFunctionReturn(0); 117347c6ae99SBarry Smith } 117447c6ae99SBarry Smith 1175d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPIAIJ_Fill(DM da, Mat J) 1176d71ae5a4SJacob Faibussowitsch { 117747c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 1178c1154cd5SBarry Smith PetscInt m, n, dim, s, *cols, k, nc, row, col, cnt, maxcnt = 0, l, p, M, N; 117947c6ae99SBarry Smith PetscInt lstart, lend, pstart, pend, *dnz, *onz; 118047c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 118147c6ae99SBarry Smith PetscInt ifill_col, *ofill = dd->ofill, *dfill = dd->dfill; 118247c6ae99SBarry Smith MPI_Comm comm; 1183bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 118445b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 1185aa219208SBarry Smith DMDAStencilType st; 1186c1154cd5SBarry Smith PetscBool removedups = PETSC_FALSE; 118747c6ae99SBarry Smith 118847c6ae99SBarry Smith PetscFunctionBegin; 118947c6ae99SBarry Smith /* 119047c6ae99SBarry Smith nc - number of components per grid point 119147c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 119247c6ae99SBarry Smith 119347c6ae99SBarry Smith */ 1194924e958dSJunchao Zhang PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, &M, &N, NULL, &nc, &s, &bx, &by, NULL, &st)); 119547c6ae99SBarry Smith col = 2 * s + 1; 1196c1154cd5SBarry Smith /* 1197c1154cd5SBarry Smith With one processor in periodic domains in a skinny dimension the code will label nonzero columns multiple times 1198c1154cd5SBarry Smith because of "wrapping" around the end of the domain hitting an entry already counted in the other direction. 1199c1154cd5SBarry Smith */ 1200c1154cd5SBarry Smith if (M == 1 && 2 * s >= m) removedups = PETSC_TRUE; 1201c1154cd5SBarry Smith if (N == 1 && 2 * s >= n) removedups = PETSC_TRUE; 12029566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 12039566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 12049566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 120547c6ae99SBarry Smith 12069566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * nc, &cols)); 12079566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 120847c6ae99SBarry Smith 12099566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 121047c6ae99SBarry Smith /* determine the matrix preallocation information */ 1211d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny, nc * nx * ny, dnz, onz); 121247c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1213bff4a2f0SMatthew G. Knepley pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1214bff4a2f0SMatthew G. Knepley pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 121547c6ae99SBarry Smith 121647c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 121747c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 121847c6ae99SBarry Smith 1219bff4a2f0SMatthew G. Knepley lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1220bff4a2f0SMatthew G. Knepley lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 122147c6ae99SBarry Smith 122247c6ae99SBarry Smith for (k = 0; k < nc; k++) { 122347c6ae99SBarry Smith cnt = 0; 122447c6ae99SBarry Smith for (l = lstart; l < lend + 1; l++) { 122547c6ae99SBarry Smith for (p = pstart; p < pend + 1; p++) { 122647c6ae99SBarry Smith if (l || p) { 1227aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star */ 12288865f1eaSKarl Rupp for (ifill_col = ofill[k]; ifill_col < ofill[k + 1]; ifill_col++) cols[cnt++] = ofill[ifill_col] + nc * (slot + gnx * l + p); 122947c6ae99SBarry Smith } 123047c6ae99SBarry Smith } else { 123147c6ae99SBarry Smith if (dfill) { 12328865f1eaSKarl Rupp for (ifill_col = dfill[k]; ifill_col < dfill[k + 1]; ifill_col++) cols[cnt++] = dfill[ifill_col] + nc * (slot + gnx * l + p); 123347c6ae99SBarry Smith } else { 12348865f1eaSKarl Rupp for (ifill_col = 0; ifill_col < nc; ifill_col++) cols[cnt++] = ifill_col + nc * (slot + gnx * l + p); 123547c6ae99SBarry Smith } 123647c6ae99SBarry Smith } 123747c6ae99SBarry Smith } 123847c6ae99SBarry Smith } 123947c6ae99SBarry Smith row = k + nc * (slot); 1240c0ab637bSBarry Smith maxcnt = PetscMax(maxcnt, cnt); 12411baa6e33SBarry Smith if (removedups) PetscCall(MatPreallocateSetLocalRemoveDups(ltog, 1, &row, ltog, cnt, cols, dnz, onz)); 12421baa6e33SBarry Smith else PetscCall(MatPreallocateSetLocal(ltog, 1, &row, ltog, cnt, cols, dnz, onz)); 124347c6ae99SBarry Smith } 124447c6ae99SBarry Smith } 1245c1154cd5SBarry Smith } 12469566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz)); 12479566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz)); 1248d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 12499566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 125047c6ae99SBarry Smith 125147c6ae99SBarry Smith /* 125247c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 125347c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 125447c6ae99SBarry Smith PETSc ordering. 125547c6ae99SBarry Smith */ 1256fcfd50ebSBarry Smith if (!da->prealloc_only) { 125747c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1258bff4a2f0SMatthew G. Knepley pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1259bff4a2f0SMatthew G. Knepley pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 126047c6ae99SBarry Smith 126147c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 126247c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 126347c6ae99SBarry Smith 1264bff4a2f0SMatthew G. Knepley lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1265bff4a2f0SMatthew G. Knepley lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 126647c6ae99SBarry Smith 126747c6ae99SBarry Smith for (k = 0; k < nc; k++) { 126847c6ae99SBarry Smith cnt = 0; 126947c6ae99SBarry Smith for (l = lstart; l < lend + 1; l++) { 127047c6ae99SBarry Smith for (p = pstart; p < pend + 1; p++) { 127147c6ae99SBarry Smith if (l || p) { 1272aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star */ 12738865f1eaSKarl Rupp for (ifill_col = ofill[k]; ifill_col < ofill[k + 1]; ifill_col++) cols[cnt++] = ofill[ifill_col] + nc * (slot + gnx * l + p); 127447c6ae99SBarry Smith } 127547c6ae99SBarry Smith } else { 127647c6ae99SBarry Smith if (dfill) { 12778865f1eaSKarl Rupp for (ifill_col = dfill[k]; ifill_col < dfill[k + 1]; ifill_col++) cols[cnt++] = dfill[ifill_col] + nc * (slot + gnx * l + p); 127847c6ae99SBarry Smith } else { 12798865f1eaSKarl Rupp for (ifill_col = 0; ifill_col < nc; ifill_col++) cols[cnt++] = ifill_col + nc * (slot + gnx * l + p); 128047c6ae99SBarry Smith } 128147c6ae99SBarry Smith } 128247c6ae99SBarry Smith } 128347c6ae99SBarry Smith } 128447c6ae99SBarry Smith row = k + nc * (slot); 12859566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, 1, &row, cnt, cols, NULL, INSERT_VALUES)); 128647c6ae99SBarry Smith } 128747c6ae99SBarry Smith } 128847c6ae99SBarry Smith } 1289e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 12909566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 12919566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 12929566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 12939566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 12949566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 129547c6ae99SBarry Smith } 12969566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 129747c6ae99SBarry Smith PetscFunctionReturn(0); 129847c6ae99SBarry Smith } 129947c6ae99SBarry Smith 130047c6ae99SBarry Smith /* ---------------------------------------------------------------------------------*/ 130147c6ae99SBarry Smith 1302d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPIAIJ(DM da, Mat J) 1303d71ae5a4SJacob Faibussowitsch { 130447c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 13050298fd71SBarry Smith PetscInt m, n, dim, s, *cols = NULL, k, nc, *rows = NULL, col, cnt, l, p, *dnz = NULL, *onz = NULL; 1306c1154cd5SBarry Smith PetscInt istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk, M, N, P; 130747c6ae99SBarry Smith MPI_Comm comm; 1308bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by, bz; 1309844bd0d7SStefano Zampini ISLocalToGlobalMapping ltog, mltog; 1310aa219208SBarry Smith DMDAStencilType st; 1311c1154cd5SBarry Smith PetscBool removedups = PETSC_FALSE; 131247c6ae99SBarry Smith 131347c6ae99SBarry Smith PetscFunctionBegin; 131447c6ae99SBarry Smith /* 131547c6ae99SBarry Smith nc - number of components per grid point 131647c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 131747c6ae99SBarry Smith 131847c6ae99SBarry Smith */ 13199566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &nc, &s, &bx, &by, &bz, &st)); 132048a46eb9SPierre Jolivet if (bx == DM_BOUNDARY_NONE && by == DM_BOUNDARY_NONE && bz == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_TRUE)); 132147c6ae99SBarry Smith col = 2 * s + 1; 132247c6ae99SBarry Smith 1323c1154cd5SBarry Smith /* 1324c1154cd5SBarry Smith With one processor in periodic domains in a skinny dimension the code will label nonzero columns multiple times 1325c1154cd5SBarry Smith because of "wrapping" around the end of the domain hitting an entry already counted in the other direction. 1326c1154cd5SBarry Smith */ 1327c1154cd5SBarry Smith if (M == 1 && 2 * s >= m) removedups = PETSC_TRUE; 1328c1154cd5SBarry Smith if (N == 1 && 2 * s >= n) removedups = PETSC_TRUE; 1329c1154cd5SBarry Smith if (P == 1 && 2 * s >= p) removedups = PETSC_TRUE; 1330c1154cd5SBarry Smith 13319566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 13329566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 13339566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 133447c6ae99SBarry Smith 13359566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * col * col * nc * nc, &cols)); 13369566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 133747c6ae99SBarry Smith 13389566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 133947c6ae99SBarry Smith /* determine the matrix preallocation information */ 1340d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny * nz, nc * nx * ny * nz, dnz, onz); 134147c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1342bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1343bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 134447c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1345bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1346bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 134747c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 1348bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 1349bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 135047c6ae99SBarry Smith 135147c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 135247c6ae99SBarry Smith 135347c6ae99SBarry Smith cnt = 0; 135447c6ae99SBarry Smith for (l = 0; l < nc; l++) { 135547c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 135647c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 135747c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 1358aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 135947c6ae99SBarry Smith cols[cnt++] = l + nc * (slot + ii + gnx * jj + gnx * gny * kk); 136047c6ae99SBarry Smith } 136147c6ae99SBarry Smith } 136247c6ae99SBarry Smith } 136347c6ae99SBarry Smith } 136447c6ae99SBarry Smith rows[l] = l + nc * (slot); 136547c6ae99SBarry Smith } 13661baa6e33SBarry Smith if (removedups) PetscCall(MatPreallocateSetLocalRemoveDups(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 13671baa6e33SBarry Smith else PetscCall(MatPreallocateSetLocal(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 136847c6ae99SBarry Smith } 136947c6ae99SBarry Smith } 1370c1154cd5SBarry Smith } 13719566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 13729566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz)); 13739566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz)); 1374d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 13759566063dSJacob Faibussowitsch PetscCall(MatGetLocalToGlobalMapping(J, &mltog, NULL)); 137648a46eb9SPierre Jolivet if (!mltog) PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 137747c6ae99SBarry Smith 137847c6ae99SBarry Smith /* 137947c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 138047c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 138147c6ae99SBarry Smith PETSc ordering. 138247c6ae99SBarry Smith */ 1383fcfd50ebSBarry Smith if (!da->prealloc_only) { 138447c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1385bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1386bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 138747c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1388bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1389bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 139047c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 1391bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 1392bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 139347c6ae99SBarry Smith 139447c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 139547c6ae99SBarry Smith 139647c6ae99SBarry Smith cnt = 0; 139747c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 1398071fcb05SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 1399071fcb05SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 1400aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 1401071fcb05SBarry Smith cols[cnt++] = nc * (slot + ii + gnx * jj + gnx * gny * kk); 1402071fcb05SBarry Smith for (l = 1; l < nc; l++) { 14039371c9d4SSatish Balay cols[cnt] = 1 + cols[cnt - 1]; 14049371c9d4SSatish Balay cnt++; 140547c6ae99SBarry Smith } 140647c6ae99SBarry Smith } 140747c6ae99SBarry Smith } 140847c6ae99SBarry Smith } 140947c6ae99SBarry Smith } 14109371c9d4SSatish Balay rows[0] = nc * (slot); 14119371c9d4SSatish Balay for (l = 1; l < nc; l++) rows[l] = 1 + rows[l - 1]; 14129566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, NULL, INSERT_VALUES)); 141347c6ae99SBarry Smith } 141447c6ae99SBarry Smith } 141547c6ae99SBarry Smith } 1416e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 14179566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 14189566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 14199566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 142048a46eb9SPierre Jolivet if (bx == DM_BOUNDARY_NONE && by == DM_BOUNDARY_NONE && bz == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE)); 14219566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 14229566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 142347c6ae99SBarry Smith } 14249566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 142547c6ae99SBarry Smith PetscFunctionReturn(0); 142647c6ae99SBarry Smith } 142747c6ae99SBarry Smith 142847c6ae99SBarry Smith /* ---------------------------------------------------------------------------------*/ 142947c6ae99SBarry Smith 1430d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_1d_MPIAIJ_Fill(DM da, Mat J) 1431d71ae5a4SJacob Faibussowitsch { 1432ce308e1dSBarry Smith DM_DA *dd = (DM_DA *)da->data; 1433ce308e1dSBarry Smith PetscInt xs, nx, i, j, gxs, gnx, row, k, l; 14348d4c968fSBarry Smith PetscInt m, dim, s, *cols = NULL, nc, cnt, maxcnt = 0, *ocols; 14350acb5bebSBarry Smith PetscInt *ofill = dd->ofill, *dfill = dd->dfill; 1436bff4a2f0SMatthew G. Knepley DMBoundaryType bx; 143745b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 1438ce308e1dSBarry Smith PetscMPIInt rank, size; 1439ce308e1dSBarry Smith 1440ce308e1dSBarry Smith PetscFunctionBegin; 14419566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)da), &rank)); 14429566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)da), &size)); 1443ce308e1dSBarry Smith 1444ce308e1dSBarry Smith /* 1445ce308e1dSBarry Smith nc - number of components per grid point 1446ce308e1dSBarry Smith 1447ce308e1dSBarry Smith */ 14489566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, NULL, NULL, NULL, NULL, NULL, &nc, &s, &bx, NULL, NULL, NULL)); 144908401ef6SPierre Jolivet PetscCheck(s <= 1, PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "Matrix creation for 1d not implemented correctly for stencil width larger than 1"); 14509566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &nx, NULL, NULL)); 14519566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, NULL, NULL, &gnx, NULL, NULL)); 1452ce308e1dSBarry Smith 14539566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 14549566063dSJacob Faibussowitsch PetscCall(PetscCalloc2(nx * nc, &cols, nx * nc, &ocols)); 1455ce308e1dSBarry Smith 1456ce308e1dSBarry Smith /* 1457ce308e1dSBarry Smith note should be smaller for first and last process with no periodic 1458ce308e1dSBarry Smith does not handle dfill 1459ce308e1dSBarry Smith */ 1460ce308e1dSBarry Smith cnt = 0; 1461ce308e1dSBarry Smith /* coupling with process to the left */ 1462ce308e1dSBarry Smith for (i = 0; i < s; i++) { 1463ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 1464dd400576SPatrick Sanan ocols[cnt] = ((rank == 0) ? 0 : (s - i) * (ofill[j + 1] - ofill[j])); 14650acb5bebSBarry Smith cols[cnt] = dfill[j + 1] - dfill[j] + (s + i) * (ofill[j + 1] - ofill[j]); 1466dd400576SPatrick Sanan if (rank == 0 && (dd->bx == DM_BOUNDARY_PERIODIC)) { 1467831644c1SBarry Smith if (size > 1) ocols[cnt] += (s - i) * (ofill[j + 1] - ofill[j]); 1468831644c1SBarry Smith else cols[cnt] += (s - i) * (ofill[j + 1] - ofill[j]); 1469831644c1SBarry Smith } 1470c0ab637bSBarry Smith maxcnt = PetscMax(maxcnt, ocols[cnt] + cols[cnt]); 1471ce308e1dSBarry Smith cnt++; 1472ce308e1dSBarry Smith } 1473ce308e1dSBarry Smith } 1474ce308e1dSBarry Smith for (i = s; i < nx - s; i++) { 1475ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 14760acb5bebSBarry Smith cols[cnt] = dfill[j + 1] - dfill[j] + 2 * s * (ofill[j + 1] - ofill[j]); 1477c0ab637bSBarry Smith maxcnt = PetscMax(maxcnt, ocols[cnt] + cols[cnt]); 1478ce308e1dSBarry Smith cnt++; 1479ce308e1dSBarry Smith } 1480ce308e1dSBarry Smith } 1481ce308e1dSBarry Smith /* coupling with process to the right */ 1482ce308e1dSBarry Smith for (i = nx - s; i < nx; i++) { 1483ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 1484ce308e1dSBarry Smith ocols[cnt] = ((rank == (size - 1)) ? 0 : (i - nx + s + 1) * (ofill[j + 1] - ofill[j])); 14850acb5bebSBarry Smith cols[cnt] = dfill[j + 1] - dfill[j] + (s + nx - i - 1) * (ofill[j + 1] - ofill[j]); 1486831644c1SBarry Smith if ((rank == size - 1) && (dd->bx == DM_BOUNDARY_PERIODIC)) { 1487831644c1SBarry Smith if (size > 1) ocols[cnt] += (i - nx + s + 1) * (ofill[j + 1] - ofill[j]); 1488831644c1SBarry Smith else cols[cnt] += (i - nx + s + 1) * (ofill[j + 1] - ofill[j]); 1489831644c1SBarry Smith } 1490c0ab637bSBarry Smith maxcnt = PetscMax(maxcnt, ocols[cnt] + cols[cnt]); 1491ce308e1dSBarry Smith cnt++; 1492ce308e1dSBarry Smith } 1493ce308e1dSBarry Smith } 1494ce308e1dSBarry Smith 14959566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, 0, cols)); 14969566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, 0, cols, 0, ocols)); 14979566063dSJacob Faibussowitsch PetscCall(PetscFree2(cols, ocols)); 1498ce308e1dSBarry Smith 14999566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 15009566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 1501ce308e1dSBarry Smith 1502ce308e1dSBarry Smith /* 1503ce308e1dSBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 1504ce308e1dSBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 1505ce308e1dSBarry Smith PETSc ordering. 1506ce308e1dSBarry Smith */ 1507ce308e1dSBarry Smith if (!da->prealloc_only) { 15089566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(maxcnt, &cols)); 1509ce308e1dSBarry Smith row = xs * nc; 1510ce308e1dSBarry Smith /* coupling with process to the left */ 1511ce308e1dSBarry Smith for (i = xs; i < xs + s; i++) { 1512ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 1513ce308e1dSBarry Smith cnt = 0; 1514ce308e1dSBarry Smith if (rank) { 1515ce308e1dSBarry Smith for (l = 0; l < s; l++) { 1516ce308e1dSBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i - s + l) * nc + ofill[k]; 1517ce308e1dSBarry Smith } 1518ce308e1dSBarry Smith } 1519dd400576SPatrick Sanan if (rank == 0 && (dd->bx == DM_BOUNDARY_PERIODIC)) { 1520831644c1SBarry Smith for (l = 0; l < s; l++) { 1521831644c1SBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (m + i - s - l) * nc + ofill[k]; 1522831644c1SBarry Smith } 1523831644c1SBarry Smith } 15240acb5bebSBarry Smith if (dfill) { 1525ad540459SPierre Jolivet for (k = dfill[j]; k < dfill[j + 1]; k++) cols[cnt++] = i * nc + dfill[k]; 15260acb5bebSBarry Smith } else { 1527ad540459SPierre Jolivet for (k = 0; k < nc; k++) cols[cnt++] = i * nc + k; 15280acb5bebSBarry Smith } 1529ce308e1dSBarry Smith for (l = 0; l < s; l++) { 1530ce308e1dSBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i + s - l) * nc + ofill[k]; 1531ce308e1dSBarry Smith } 15329566063dSJacob Faibussowitsch PetscCall(MatSetValues(J, 1, &row, cnt, cols, NULL, INSERT_VALUES)); 1533ce308e1dSBarry Smith row++; 1534ce308e1dSBarry Smith } 1535ce308e1dSBarry Smith } 1536ce308e1dSBarry Smith for (i = xs + s; i < xs + nx - s; i++) { 1537ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 1538ce308e1dSBarry Smith cnt = 0; 1539ce308e1dSBarry Smith for (l = 0; l < s; l++) { 1540ce308e1dSBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i - s + l) * nc + ofill[k]; 1541ce308e1dSBarry Smith } 15420acb5bebSBarry Smith if (dfill) { 1543ad540459SPierre Jolivet for (k = dfill[j]; k < dfill[j + 1]; k++) cols[cnt++] = i * nc + dfill[k]; 15440acb5bebSBarry Smith } else { 1545ad540459SPierre Jolivet for (k = 0; k < nc; k++) cols[cnt++] = i * nc + k; 15460acb5bebSBarry Smith } 1547ce308e1dSBarry Smith for (l = 0; l < s; l++) { 1548ce308e1dSBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i + s - l) * nc + ofill[k]; 1549ce308e1dSBarry Smith } 15509566063dSJacob Faibussowitsch PetscCall(MatSetValues(J, 1, &row, cnt, cols, NULL, INSERT_VALUES)); 1551ce308e1dSBarry Smith row++; 1552ce308e1dSBarry Smith } 1553ce308e1dSBarry Smith } 1554ce308e1dSBarry Smith /* coupling with process to the right */ 1555ce308e1dSBarry Smith for (i = xs + nx - s; i < xs + nx; i++) { 1556ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 1557ce308e1dSBarry Smith cnt = 0; 1558ce308e1dSBarry Smith for (l = 0; l < s; l++) { 1559ce308e1dSBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i - s + l) * nc + ofill[k]; 1560ce308e1dSBarry Smith } 15610acb5bebSBarry Smith if (dfill) { 1562ad540459SPierre Jolivet for (k = dfill[j]; k < dfill[j + 1]; k++) cols[cnt++] = i * nc + dfill[k]; 15630acb5bebSBarry Smith } else { 1564ad540459SPierre Jolivet for (k = 0; k < nc; k++) cols[cnt++] = i * nc + k; 15650acb5bebSBarry Smith } 1566ce308e1dSBarry Smith if (rank < size - 1) { 1567ce308e1dSBarry Smith for (l = 0; l < s; l++) { 1568ce308e1dSBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i + s - l) * nc + ofill[k]; 1569ce308e1dSBarry Smith } 1570ce308e1dSBarry Smith } 1571831644c1SBarry Smith if ((rank == size - 1) && (dd->bx == DM_BOUNDARY_PERIODIC)) { 1572831644c1SBarry Smith for (l = 0; l < s; l++) { 1573831644c1SBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i - s - l - m + 2) * nc + ofill[k]; 1574831644c1SBarry Smith } 1575831644c1SBarry Smith } 15769566063dSJacob Faibussowitsch PetscCall(MatSetValues(J, 1, &row, cnt, cols, NULL, INSERT_VALUES)); 1577ce308e1dSBarry Smith row++; 1578ce308e1dSBarry Smith } 1579ce308e1dSBarry Smith } 15809566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 1581e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 15829566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 15839566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 15849566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 15859566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 15869566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 1587ce308e1dSBarry Smith } 1588ce308e1dSBarry Smith PetscFunctionReturn(0); 1589ce308e1dSBarry Smith } 1590ce308e1dSBarry Smith 1591ce308e1dSBarry Smith /* ---------------------------------------------------------------------------------*/ 1592ce308e1dSBarry Smith 1593d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_1d_MPIAIJ(DM da, Mat J) 1594d71ae5a4SJacob Faibussowitsch { 159547c6ae99SBarry Smith PetscInt xs, nx, i, i1, slot, gxs, gnx; 15960298fd71SBarry Smith PetscInt m, dim, s, *cols = NULL, nc, *rows = NULL, col, cnt, l; 159747c6ae99SBarry Smith PetscInt istart, iend; 1598bff4a2f0SMatthew G. Knepley DMBoundaryType bx; 1599844bd0d7SStefano Zampini ISLocalToGlobalMapping ltog, mltog; 160047c6ae99SBarry Smith 160147c6ae99SBarry Smith PetscFunctionBegin; 160247c6ae99SBarry Smith /* 160347c6ae99SBarry Smith nc - number of components per grid point 160447c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 160547c6ae99SBarry Smith 160647c6ae99SBarry Smith */ 16079566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, NULL, NULL, NULL, NULL, NULL, &nc, &s, &bx, NULL, NULL, NULL)); 160848a46eb9SPierre Jolivet if (bx == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_TRUE)); 160947c6ae99SBarry Smith col = 2 * s + 1; 161047c6ae99SBarry Smith 16119566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &nx, NULL, NULL)); 16129566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, NULL, NULL, &gnx, NULL, NULL)); 161347c6ae99SBarry Smith 16149566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 16159566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, col * nc, NULL)); 16169566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, col * nc, NULL, col * nc, NULL)); 161747c6ae99SBarry Smith 16189566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 16199566063dSJacob Faibussowitsch PetscCall(MatGetLocalToGlobalMapping(J, &mltog, NULL)); 162048a46eb9SPierre Jolivet if (!mltog) PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 162147c6ae99SBarry Smith 162247c6ae99SBarry Smith /* 162347c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 162447c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 162547c6ae99SBarry Smith PETSc ordering. 162647c6ae99SBarry Smith */ 1627fcfd50ebSBarry Smith if (!da->prealloc_only) { 16289566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * nc * nc, &cols)); 162947c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 163047c6ae99SBarry Smith istart = PetscMax(-s, gxs - i); 163147c6ae99SBarry Smith iend = PetscMin(s, gxs + gnx - i - 1); 163247c6ae99SBarry Smith slot = i - gxs; 163347c6ae99SBarry Smith 163447c6ae99SBarry Smith cnt = 0; 163547c6ae99SBarry Smith for (i1 = istart; i1 < iend + 1; i1++) { 1636071fcb05SBarry Smith cols[cnt++] = nc * (slot + i1); 1637071fcb05SBarry Smith for (l = 1; l < nc; l++) { 16389371c9d4SSatish Balay cols[cnt] = 1 + cols[cnt - 1]; 16399371c9d4SSatish Balay cnt++; 164047c6ae99SBarry Smith } 164147c6ae99SBarry Smith } 16429371c9d4SSatish Balay rows[0] = nc * (slot); 16439371c9d4SSatish Balay for (l = 1; l < nc; l++) rows[l] = 1 + rows[l - 1]; 16449566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, NULL, INSERT_VALUES)); 164547c6ae99SBarry Smith } 1646e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 16479566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 16489566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 16499566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 165048a46eb9SPierre Jolivet if (bx == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE)); 16519566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 16529566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 16539566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 1654ce308e1dSBarry Smith } 165547c6ae99SBarry Smith PetscFunctionReturn(0); 165647c6ae99SBarry Smith } 165747c6ae99SBarry Smith 165819b08ed1SBarry Smith /* ---------------------------------------------------------------------------------*/ 165919b08ed1SBarry Smith 1660d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_1d_SeqAIJ_NoPreallocation(DM da, Mat J) 1661d71ae5a4SJacob Faibussowitsch { 166219b08ed1SBarry Smith PetscInt xs, nx, i, i1, slot, gxs, gnx; 166319b08ed1SBarry Smith PetscInt m, dim, s, *cols = NULL, nc, *rows = NULL, col, cnt, l; 166419b08ed1SBarry Smith PetscInt istart, iend; 166519b08ed1SBarry Smith DMBoundaryType bx; 166619b08ed1SBarry Smith ISLocalToGlobalMapping ltog, mltog; 166719b08ed1SBarry Smith 166819b08ed1SBarry Smith PetscFunctionBegin; 166919b08ed1SBarry Smith /* 167019b08ed1SBarry Smith nc - number of components per grid point 167119b08ed1SBarry Smith col - number of colors needed in one direction for single component problem 167219b08ed1SBarry Smith */ 16739566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, NULL, NULL, NULL, NULL, NULL, &nc, &s, &bx, NULL, NULL, NULL)); 167419b08ed1SBarry Smith col = 2 * s + 1; 167519b08ed1SBarry Smith 16769566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &nx, NULL, NULL)); 16779566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, NULL, NULL, &gnx, NULL, NULL)); 167819b08ed1SBarry Smith 16799566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 16809566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetTotalPreallocation(J, nx * nc * col * nc)); 168119b08ed1SBarry Smith 16829566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 16839566063dSJacob Faibussowitsch PetscCall(MatGetLocalToGlobalMapping(J, &mltog, NULL)); 168448a46eb9SPierre Jolivet if (!mltog) PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 168519b08ed1SBarry Smith 168619b08ed1SBarry Smith /* 168719b08ed1SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 168819b08ed1SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 168919b08ed1SBarry Smith PETSc ordering. 169019b08ed1SBarry Smith */ 169119b08ed1SBarry Smith if (!da->prealloc_only) { 16929566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * nc * nc, &cols)); 169319b08ed1SBarry Smith for (i = xs; i < xs + nx; i++) { 169419b08ed1SBarry Smith istart = PetscMax(-s, gxs - i); 169519b08ed1SBarry Smith iend = PetscMin(s, gxs + gnx - i - 1); 169619b08ed1SBarry Smith slot = i - gxs; 169719b08ed1SBarry Smith 169819b08ed1SBarry Smith cnt = 0; 169919b08ed1SBarry Smith for (i1 = istart; i1 < iend + 1; i1++) { 170019b08ed1SBarry Smith cols[cnt++] = nc * (slot + i1); 170119b08ed1SBarry Smith for (l = 1; l < nc; l++) { 17029371c9d4SSatish Balay cols[cnt] = 1 + cols[cnt - 1]; 17039371c9d4SSatish Balay cnt++; 170419b08ed1SBarry Smith } 170519b08ed1SBarry Smith } 17069371c9d4SSatish Balay rows[0] = nc * (slot); 17079371c9d4SSatish Balay for (l = 1; l < nc; l++) rows[l] = 1 + rows[l - 1]; 17089566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, NULL, INSERT_VALUES)); 170919b08ed1SBarry Smith } 171019b08ed1SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 17119566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 17129566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 17139566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 171448a46eb9SPierre Jolivet if (bx == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE)); 17159566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 17169566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 17179566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 171819b08ed1SBarry Smith } 17199566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE)); 172019b08ed1SBarry Smith PetscFunctionReturn(0); 172119b08ed1SBarry Smith } 172219b08ed1SBarry Smith 1723d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPIBAIJ(DM da, Mat J) 1724d71ae5a4SJacob Faibussowitsch { 172547c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 172647c6ae99SBarry Smith PetscInt m, n, dim, s, *cols, nc, col, cnt, *dnz, *onz; 172747c6ae99SBarry Smith PetscInt istart, iend, jstart, jend, ii, jj; 172847c6ae99SBarry Smith MPI_Comm comm; 172947c6ae99SBarry Smith PetscScalar *values; 1730bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 1731aa219208SBarry Smith DMDAStencilType st; 173245b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 173347c6ae99SBarry Smith 173447c6ae99SBarry Smith PetscFunctionBegin; 173547c6ae99SBarry Smith /* 173647c6ae99SBarry Smith nc - number of components per grid point 173747c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 173847c6ae99SBarry Smith */ 17399566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, NULL, NULL, NULL, &nc, &s, &bx, &by, NULL, &st)); 174047c6ae99SBarry Smith col = 2 * s + 1; 174147c6ae99SBarry Smith 17429566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 17439566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 17449566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 174547c6ae99SBarry Smith 17469566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * nc * nc, &cols)); 174747c6ae99SBarry Smith 17489566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 174947c6ae99SBarry Smith 175047c6ae99SBarry Smith /* determine the matrix preallocation information */ 1751d0609cedSBarry Smith MatPreallocateBegin(comm, nx * ny, nx * ny, dnz, onz); 175247c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1753bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1754bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 175547c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1756bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1757bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 175847c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 175947c6ae99SBarry Smith 176047c6ae99SBarry Smith /* Find block columns in block row */ 176147c6ae99SBarry Smith cnt = 0; 176247c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 176347c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 1764aa219208SBarry Smith if (st == DMDA_STENCIL_BOX || !ii || !jj) { /* BOX or on the STAR */ 176547c6ae99SBarry Smith cols[cnt++] = slot + ii + gnx * jj; 176647c6ae99SBarry Smith } 176747c6ae99SBarry Smith } 176847c6ae99SBarry Smith } 17699566063dSJacob Faibussowitsch PetscCall(MatPreallocateSetLocalBlock(ltog, 1, &slot, ltog, cnt, cols, dnz, onz)); 177047c6ae99SBarry Smith } 177147c6ae99SBarry Smith } 17729566063dSJacob Faibussowitsch PetscCall(MatSeqBAIJSetPreallocation(J, nc, 0, dnz)); 17739566063dSJacob Faibussowitsch PetscCall(MatMPIBAIJSetPreallocation(J, nc, 0, dnz, 0, onz)); 1774d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 177547c6ae99SBarry Smith 17769566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 177747c6ae99SBarry Smith 177847c6ae99SBarry Smith /* 177947c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 178047c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 178147c6ae99SBarry Smith PETSc ordering. 178247c6ae99SBarry Smith */ 1783fcfd50ebSBarry Smith if (!da->prealloc_only) { 17849566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * nc * nc, &values)); 178547c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1786bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1787bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 178847c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1789bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1790bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 179147c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 179247c6ae99SBarry Smith cnt = 0; 179347c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 179447c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 1795aa219208SBarry Smith if (st == DMDA_STENCIL_BOX || !ii || !jj) { /* BOX or on the STAR */ 179647c6ae99SBarry Smith cols[cnt++] = slot + ii + gnx * jj; 179747c6ae99SBarry Smith } 179847c6ae99SBarry Smith } 179947c6ae99SBarry Smith } 18009566063dSJacob Faibussowitsch PetscCall(MatSetValuesBlockedLocal(J, 1, &slot, cnt, cols, values, INSERT_VALUES)); 180147c6ae99SBarry Smith } 180247c6ae99SBarry Smith } 18039566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 1804e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 18059566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 18069566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 18079566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 18089566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 18099566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 181047c6ae99SBarry Smith } 18119566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 181247c6ae99SBarry Smith PetscFunctionReturn(0); 181347c6ae99SBarry Smith } 181447c6ae99SBarry Smith 1815d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPIBAIJ(DM da, Mat J) 1816d71ae5a4SJacob Faibussowitsch { 181747c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 181847c6ae99SBarry Smith PetscInt m, n, dim, s, *cols, k, nc, col, cnt, p, *dnz, *onz; 181947c6ae99SBarry Smith PetscInt istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk; 182047c6ae99SBarry Smith MPI_Comm comm; 182147c6ae99SBarry Smith PetscScalar *values; 1822bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by, bz; 1823aa219208SBarry Smith DMDAStencilType st; 182445b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 182547c6ae99SBarry Smith 182647c6ae99SBarry Smith PetscFunctionBegin; 182747c6ae99SBarry Smith /* 182847c6ae99SBarry Smith nc - number of components per grid point 182947c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 183047c6ae99SBarry Smith 183147c6ae99SBarry Smith */ 18329566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, NULL, NULL, NULL, &nc, &s, &bx, &by, &bz, &st)); 183347c6ae99SBarry Smith col = 2 * s + 1; 183447c6ae99SBarry Smith 18359566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 18369566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 18379566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 183847c6ae99SBarry Smith 18399566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * col, &cols)); 184047c6ae99SBarry Smith 18419566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 184247c6ae99SBarry Smith 184347c6ae99SBarry Smith /* determine the matrix preallocation information */ 1844d0609cedSBarry Smith MatPreallocateBegin(comm, nx * ny * nz, nx * ny * nz, dnz, onz); 184547c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1846bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1847bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 184847c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1849bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1850bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 185147c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 1852bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 1853bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 185447c6ae99SBarry Smith 185547c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 185647c6ae99SBarry Smith 185747c6ae99SBarry Smith /* Find block columns in block row */ 185847c6ae99SBarry Smith cnt = 0; 185947c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 186047c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 186147c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 1862aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 186347c6ae99SBarry Smith cols[cnt++] = slot + ii + gnx * jj + gnx * gny * kk; 186447c6ae99SBarry Smith } 186547c6ae99SBarry Smith } 186647c6ae99SBarry Smith } 186747c6ae99SBarry Smith } 18689566063dSJacob Faibussowitsch PetscCall(MatPreallocateSetLocalBlock(ltog, 1, &slot, ltog, cnt, cols, dnz, onz)); 186947c6ae99SBarry Smith } 187047c6ae99SBarry Smith } 187147c6ae99SBarry Smith } 18729566063dSJacob Faibussowitsch PetscCall(MatSeqBAIJSetPreallocation(J, nc, 0, dnz)); 18739566063dSJacob Faibussowitsch PetscCall(MatMPIBAIJSetPreallocation(J, nc, 0, dnz, 0, onz)); 1874d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 187547c6ae99SBarry Smith 18769566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 187747c6ae99SBarry Smith 187847c6ae99SBarry Smith /* 187947c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 188047c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 188147c6ae99SBarry Smith PETSc ordering. 188247c6ae99SBarry Smith */ 1883fcfd50ebSBarry Smith if (!da->prealloc_only) { 18849566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * col * nc * nc, &values)); 188547c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1886bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1887bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 188847c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1889bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1890bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 189147c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 1892bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 1893bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 189447c6ae99SBarry Smith 189547c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 189647c6ae99SBarry Smith 189747c6ae99SBarry Smith cnt = 0; 189847c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 189947c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 190047c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 1901aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 190247c6ae99SBarry Smith cols[cnt++] = slot + ii + gnx * jj + gnx * gny * kk; 190347c6ae99SBarry Smith } 190447c6ae99SBarry Smith } 190547c6ae99SBarry Smith } 190647c6ae99SBarry Smith } 19079566063dSJacob Faibussowitsch PetscCall(MatSetValuesBlockedLocal(J, 1, &slot, cnt, cols, values, INSERT_VALUES)); 190847c6ae99SBarry Smith } 190947c6ae99SBarry Smith } 191047c6ae99SBarry Smith } 19119566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 1912e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 19139566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 19149566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 19159566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 19169566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 19179566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 191847c6ae99SBarry Smith } 19199566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 192047c6ae99SBarry Smith PetscFunctionReturn(0); 192147c6ae99SBarry Smith } 192247c6ae99SBarry Smith 192347c6ae99SBarry Smith /* 192447c6ae99SBarry Smith This helper is for of SBAIJ preallocation, to discard the lower-triangular values which are difficult to 192547c6ae99SBarry Smith identify in the local ordering with periodic domain. 192647c6ae99SBarry Smith */ 1927d71ae5a4SJacob Faibussowitsch static PetscErrorCode L2GFilterUpperTriangular(ISLocalToGlobalMapping ltog, PetscInt *row, PetscInt *cnt, PetscInt col[]) 1928d71ae5a4SJacob Faibussowitsch { 192947c6ae99SBarry Smith PetscInt i, n; 193047c6ae99SBarry Smith 193147c6ae99SBarry Smith PetscFunctionBegin; 19329566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingApplyBlock(ltog, 1, row, row)); 19339566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingApplyBlock(ltog, *cnt, col, col)); 193447c6ae99SBarry Smith for (i = 0, n = 0; i < *cnt; i++) { 193547c6ae99SBarry Smith if (col[i] >= *row) col[n++] = col[i]; 193647c6ae99SBarry Smith } 193747c6ae99SBarry Smith *cnt = n; 193847c6ae99SBarry Smith PetscFunctionReturn(0); 193947c6ae99SBarry Smith } 194047c6ae99SBarry Smith 1941d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPISBAIJ(DM da, Mat J) 1942d71ae5a4SJacob Faibussowitsch { 194347c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 194447c6ae99SBarry Smith PetscInt m, n, dim, s, *cols, nc, col, cnt, *dnz, *onz; 194547c6ae99SBarry Smith PetscInt istart, iend, jstart, jend, ii, jj; 194647c6ae99SBarry Smith MPI_Comm comm; 194747c6ae99SBarry Smith PetscScalar *values; 1948bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 1949aa219208SBarry Smith DMDAStencilType st; 195045b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 195147c6ae99SBarry Smith 195247c6ae99SBarry Smith PetscFunctionBegin; 195347c6ae99SBarry Smith /* 195447c6ae99SBarry Smith nc - number of components per grid point 195547c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 195647c6ae99SBarry Smith */ 19579566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, NULL, NULL, NULL, &nc, &s, &bx, &by, NULL, &st)); 195847c6ae99SBarry Smith col = 2 * s + 1; 195947c6ae99SBarry Smith 19609566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 19619566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 19629566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 196347c6ae99SBarry Smith 19649566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * nc * nc, &cols)); 196547c6ae99SBarry Smith 19669566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 196747c6ae99SBarry Smith 196847c6ae99SBarry Smith /* determine the matrix preallocation information */ 1969d0609cedSBarry Smith MatPreallocateBegin(comm, nx * ny, nx * ny, dnz, onz); 197047c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1971bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1972bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 197347c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1974bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1975bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 197647c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 197747c6ae99SBarry Smith 197847c6ae99SBarry Smith /* Find block columns in block row */ 197947c6ae99SBarry Smith cnt = 0; 198047c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 198147c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 1982ad540459SPierre Jolivet if (st == DMDA_STENCIL_BOX || !ii || !jj) cols[cnt++] = slot + ii + gnx * jj; 198347c6ae99SBarry Smith } 198447c6ae99SBarry Smith } 19859566063dSJacob Faibussowitsch PetscCall(L2GFilterUpperTriangular(ltog, &slot, &cnt, cols)); 19869566063dSJacob Faibussowitsch PetscCall(MatPreallocateSymmetricSetBlock(slot, cnt, cols, dnz, onz)); 198747c6ae99SBarry Smith } 198847c6ae99SBarry Smith } 19899566063dSJacob Faibussowitsch PetscCall(MatSeqSBAIJSetPreallocation(J, nc, 0, dnz)); 19909566063dSJacob Faibussowitsch PetscCall(MatMPISBAIJSetPreallocation(J, nc, 0, dnz, 0, onz)); 1991d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 199247c6ae99SBarry Smith 19939566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 199447c6ae99SBarry Smith 199547c6ae99SBarry Smith /* 199647c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 199747c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 199847c6ae99SBarry Smith PETSc ordering. 199947c6ae99SBarry Smith */ 2000fcfd50ebSBarry Smith if (!da->prealloc_only) { 20019566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * nc * nc, &values)); 200247c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 2003bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 2004bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 200547c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 2006bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 2007bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 200847c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 200947c6ae99SBarry Smith 201047c6ae99SBarry Smith /* Find block columns in block row */ 201147c6ae99SBarry Smith cnt = 0; 201247c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 201347c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 2014ad540459SPierre Jolivet if (st == DMDA_STENCIL_BOX || !ii || !jj) cols[cnt++] = slot + ii + gnx * jj; 201547c6ae99SBarry Smith } 201647c6ae99SBarry Smith } 20179566063dSJacob Faibussowitsch PetscCall(L2GFilterUpperTriangular(ltog, &slot, &cnt, cols)); 20189566063dSJacob Faibussowitsch PetscCall(MatSetValuesBlocked(J, 1, &slot, cnt, cols, values, INSERT_VALUES)); 201947c6ae99SBarry Smith } 202047c6ae99SBarry Smith } 20219566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 2022e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 20239566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 20249566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 20259566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 20269566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 20279566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 202847c6ae99SBarry Smith } 20299566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 203047c6ae99SBarry Smith PetscFunctionReturn(0); 203147c6ae99SBarry Smith } 203247c6ae99SBarry Smith 2033d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPISBAIJ(DM da, Mat J) 2034d71ae5a4SJacob Faibussowitsch { 203547c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 203647c6ae99SBarry Smith PetscInt m, n, dim, s, *cols, k, nc, col, cnt, p, *dnz, *onz; 203747c6ae99SBarry Smith PetscInt istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk; 203847c6ae99SBarry Smith MPI_Comm comm; 203947c6ae99SBarry Smith PetscScalar *values; 2040bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by, bz; 2041aa219208SBarry Smith DMDAStencilType st; 204245b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 204347c6ae99SBarry Smith 204447c6ae99SBarry Smith PetscFunctionBegin; 204547c6ae99SBarry Smith /* 204647c6ae99SBarry Smith nc - number of components per grid point 204747c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 204847c6ae99SBarry Smith */ 20499566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, NULL, NULL, NULL, &nc, &s, &bx, &by, &bz, &st)); 205047c6ae99SBarry Smith col = 2 * s + 1; 205147c6ae99SBarry Smith 20529566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 20539566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 20549566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 205547c6ae99SBarry Smith 205647c6ae99SBarry Smith /* create the matrix */ 20579566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * col, &cols)); 205847c6ae99SBarry Smith 20599566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 206047c6ae99SBarry Smith 206147c6ae99SBarry Smith /* determine the matrix preallocation information */ 2062d0609cedSBarry Smith MatPreallocateBegin(comm, nx * ny * nz, nx * ny * nz, dnz, onz); 206347c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 2064bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 2065bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 206647c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 2067bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 2068bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 206947c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 2070bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 2071bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 207247c6ae99SBarry Smith 207347c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 207447c6ae99SBarry Smith 207547c6ae99SBarry Smith /* Find block columns in block row */ 207647c6ae99SBarry Smith cnt = 0; 207747c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 207847c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 207947c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 2080ad540459SPierre Jolivet if ((st == DMDA_STENCIL_BOX) || (!ii && !jj) || (!jj && !kk) || (!ii && !kk)) cols[cnt++] = slot + ii + gnx * jj + gnx * gny * kk; 208147c6ae99SBarry Smith } 208247c6ae99SBarry Smith } 208347c6ae99SBarry Smith } 20849566063dSJacob Faibussowitsch PetscCall(L2GFilterUpperTriangular(ltog, &slot, &cnt, cols)); 20859566063dSJacob Faibussowitsch PetscCall(MatPreallocateSymmetricSetBlock(slot, cnt, cols, dnz, onz)); 208647c6ae99SBarry Smith } 208747c6ae99SBarry Smith } 208847c6ae99SBarry Smith } 20899566063dSJacob Faibussowitsch PetscCall(MatSeqSBAIJSetPreallocation(J, nc, 0, dnz)); 20909566063dSJacob Faibussowitsch PetscCall(MatMPISBAIJSetPreallocation(J, nc, 0, dnz, 0, onz)); 2091d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 209247c6ae99SBarry Smith 20939566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 209447c6ae99SBarry Smith 209547c6ae99SBarry Smith /* 209647c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 209747c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 209847c6ae99SBarry Smith PETSc ordering. 209947c6ae99SBarry Smith */ 2100fcfd50ebSBarry Smith if (!da->prealloc_only) { 21019566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * col * nc * nc, &values)); 210247c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 2103bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 2104bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 210547c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 2106bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 2107bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 210847c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 2109bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 2110bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 211147c6ae99SBarry Smith 211247c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 211347c6ae99SBarry Smith 211447c6ae99SBarry Smith cnt = 0; 211547c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 211647c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 211747c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 2118ad540459SPierre Jolivet if ((st == DMDA_STENCIL_BOX) || (!ii && !jj) || (!jj && !kk) || (!ii && !kk)) cols[cnt++] = slot + ii + gnx * jj + gnx * gny * kk; 211947c6ae99SBarry Smith } 212047c6ae99SBarry Smith } 212147c6ae99SBarry Smith } 21229566063dSJacob Faibussowitsch PetscCall(L2GFilterUpperTriangular(ltog, &slot, &cnt, cols)); 21239566063dSJacob Faibussowitsch PetscCall(MatSetValuesBlocked(J, 1, &slot, cnt, cols, values, INSERT_VALUES)); 212447c6ae99SBarry Smith } 212547c6ae99SBarry Smith } 212647c6ae99SBarry Smith } 21279566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 2128e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 21299566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 21309566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 21319566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 21329566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 21339566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 213447c6ae99SBarry Smith } 21359566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 213647c6ae99SBarry Smith PetscFunctionReturn(0); 213747c6ae99SBarry Smith } 213847c6ae99SBarry Smith 213947c6ae99SBarry Smith /* ---------------------------------------------------------------------------------*/ 214047c6ae99SBarry Smith 2141d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPIAIJ_Fill(DM da, Mat J) 2142d71ae5a4SJacob Faibussowitsch { 214347c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 2144c0ab637bSBarry Smith PetscInt m, n, dim, s, *cols, k, nc, row, col, cnt, maxcnt = 0, l, p, *dnz, *onz; 2145c1154cd5SBarry Smith PetscInt istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk, M, N, P; 214647c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 214747c6ae99SBarry Smith PetscInt ifill_col, *dfill = dd->dfill, *ofill = dd->ofill; 214847c6ae99SBarry Smith MPI_Comm comm; 214947c6ae99SBarry Smith PetscScalar *values; 2150bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by, bz; 215145b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 2152aa219208SBarry Smith DMDAStencilType st; 2153c1154cd5SBarry Smith PetscBool removedups = PETSC_FALSE; 215447c6ae99SBarry Smith 215547c6ae99SBarry Smith PetscFunctionBegin; 215647c6ae99SBarry Smith /* 215747c6ae99SBarry Smith nc - number of components per grid point 215847c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 215947c6ae99SBarry Smith 216047c6ae99SBarry Smith */ 21619566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &nc, &s, &bx, &by, &bz, &st)); 216247c6ae99SBarry Smith col = 2 * s + 1; 21631dca8a05SBarry 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\ 216447c6ae99SBarry Smith by 2*stencil_width + 1\n"); 21651dca8a05SBarry 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\ 216647c6ae99SBarry Smith by 2*stencil_width + 1\n"); 21671dca8a05SBarry 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\ 216847c6ae99SBarry Smith by 2*stencil_width + 1\n"); 216947c6ae99SBarry Smith 2170c1154cd5SBarry Smith /* 2171c1154cd5SBarry Smith With one processor in periodic domains in a skinny dimension the code will label nonzero columns multiple times 2172c1154cd5SBarry Smith because of "wrapping" around the end of the domain hitting an entry already counted in the other direction. 2173c1154cd5SBarry Smith */ 2174c1154cd5SBarry Smith if (M == 1 && 2 * s >= m) removedups = PETSC_TRUE; 2175c1154cd5SBarry Smith if (N == 1 && 2 * s >= n) removedups = PETSC_TRUE; 2176c1154cd5SBarry Smith if (P == 1 && 2 * s >= p) removedups = PETSC_TRUE; 2177c1154cd5SBarry Smith 21789566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 21799566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 21809566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 218147c6ae99SBarry Smith 21829566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * col * nc, &cols)); 21839566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 218447c6ae99SBarry Smith 218547c6ae99SBarry Smith /* determine the matrix preallocation information */ 2186d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny * nz, nc * nx * ny * nz, dnz, onz); 218747c6ae99SBarry Smith 21889566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 218947c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 2190bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 2191bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 219247c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 2193bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 2194bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 219547c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 2196bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 2197bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 219847c6ae99SBarry Smith 219947c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 220047c6ae99SBarry Smith 220147c6ae99SBarry Smith for (l = 0; l < nc; l++) { 220247c6ae99SBarry Smith cnt = 0; 220347c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 220447c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 220547c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 220647c6ae99SBarry Smith if (ii || jj || kk) { 2207aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 22088865f1eaSKarl 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); 220947c6ae99SBarry Smith } 221047c6ae99SBarry Smith } else { 221147c6ae99SBarry Smith if (dfill) { 22128865f1eaSKarl 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); 221347c6ae99SBarry Smith } else { 22148865f1eaSKarl Rupp for (ifill_col = 0; ifill_col < nc; ifill_col++) cols[cnt++] = ifill_col + nc * (slot + ii + gnx * jj + gnx * gny * kk); 221547c6ae99SBarry Smith } 221647c6ae99SBarry Smith } 221747c6ae99SBarry Smith } 221847c6ae99SBarry Smith } 221947c6ae99SBarry Smith } 222047c6ae99SBarry Smith row = l + nc * (slot); 2221c0ab637bSBarry Smith maxcnt = PetscMax(maxcnt, cnt); 22221baa6e33SBarry Smith if (removedups) PetscCall(MatPreallocateSetLocalRemoveDups(ltog, 1, &row, ltog, cnt, cols, dnz, onz)); 22231baa6e33SBarry Smith else PetscCall(MatPreallocateSetLocal(ltog, 1, &row, ltog, cnt, cols, dnz, onz)); 222447c6ae99SBarry Smith } 222547c6ae99SBarry Smith } 222647c6ae99SBarry Smith } 2227c1154cd5SBarry Smith } 22289566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz)); 22299566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz)); 2230d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 22319566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 223247c6ae99SBarry Smith 223347c6ae99SBarry Smith /* 223447c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 223547c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 223647c6ae99SBarry Smith PETSc ordering. 223747c6ae99SBarry Smith */ 2238fcfd50ebSBarry Smith if (!da->prealloc_only) { 22399566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(maxcnt, &values)); 224047c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 2241bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 2242bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 224347c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 2244bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 2245bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 224647c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 2247bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 2248bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 224947c6ae99SBarry Smith 225047c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 225147c6ae99SBarry Smith 225247c6ae99SBarry Smith for (l = 0; l < nc; l++) { 225347c6ae99SBarry Smith cnt = 0; 225447c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 225547c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 225647c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 225747c6ae99SBarry Smith if (ii || jj || kk) { 2258aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 22598865f1eaSKarl 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); 226047c6ae99SBarry Smith } 226147c6ae99SBarry Smith } else { 226247c6ae99SBarry Smith if (dfill) { 22638865f1eaSKarl 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); 226447c6ae99SBarry Smith } else { 22658865f1eaSKarl Rupp for (ifill_col = 0; ifill_col < nc; ifill_col++) cols[cnt++] = ifill_col + nc * (slot + ii + gnx * jj + gnx * gny * kk); 226647c6ae99SBarry Smith } 226747c6ae99SBarry Smith } 226847c6ae99SBarry Smith } 226947c6ae99SBarry Smith } 227047c6ae99SBarry Smith } 227147c6ae99SBarry Smith row = l + nc * (slot); 22729566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, 1, &row, cnt, cols, values, INSERT_VALUES)); 227347c6ae99SBarry Smith } 227447c6ae99SBarry Smith } 227547c6ae99SBarry Smith } 227647c6ae99SBarry Smith } 22779566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 2278e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 22799566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 22809566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 22819566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 22829566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 22839566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 228447c6ae99SBarry Smith } 22859566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 228647c6ae99SBarry Smith PetscFunctionReturn(0); 228747c6ae99SBarry Smith } 2288