147c6ae99SBarry Smith 2af0996ceSBarry Smith #include <petsc/private/dmdaimpl.h> /*I "petscdmda.h" I*/ 307475bc1SBarry Smith #include <petscmat.h> 4e432b41dSStefano Zampini #include <petscbt.h> 547c6ae99SBarry Smith 6e727c939SJed Brown extern PetscErrorCode DMCreateColoring_DA_1d_MPIAIJ(DM, ISColoringType, ISColoring *); 7e727c939SJed Brown extern PetscErrorCode DMCreateColoring_DA_2d_MPIAIJ(DM, ISColoringType, ISColoring *); 8e727c939SJed Brown extern PetscErrorCode DMCreateColoring_DA_2d_5pt_MPIAIJ(DM, ISColoringType, ISColoring *); 9e727c939SJed Brown extern PetscErrorCode DMCreateColoring_DA_3d_MPIAIJ(DM, ISColoringType, ISColoring *); 1047c6ae99SBarry Smith 1147c6ae99SBarry Smith /* 1247c6ae99SBarry Smith For ghost i that may be negative or greater than the upper bound this 1347c6ae99SBarry Smith maps it into the 0:m-1 range using periodicity 1447c6ae99SBarry Smith */ 1547c6ae99SBarry Smith #define SetInRange(i, m) ((i < 0) ? m + i : ((i >= m) ? i - m : i)) 1647c6ae99SBarry Smith 17d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDASetBlockFills_Private(const PetscInt *dfill, PetscInt w, PetscInt **rfill) 18d71ae5a4SJacob Faibussowitsch { 1947c6ae99SBarry Smith PetscInt i, j, nz, *fill; 2047c6ae99SBarry Smith 2147c6ae99SBarry Smith PetscFunctionBegin; 223ba16761SJacob Faibussowitsch if (!dfill) PetscFunctionReturn(PETSC_SUCCESS); 2347c6ae99SBarry Smith 2447c6ae99SBarry Smith /* count number nonzeros */ 2547c6ae99SBarry Smith nz = 0; 2647c6ae99SBarry Smith for (i = 0; i < w; i++) { 2747c6ae99SBarry Smith for (j = 0; j < w; j++) { 2847c6ae99SBarry Smith if (dfill[w * i + j]) nz++; 2947c6ae99SBarry Smith } 3047c6ae99SBarry Smith } 319566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nz + w + 1, &fill)); 3247c6ae99SBarry Smith /* construct modified CSR storage of nonzero structure */ 33ce308e1dSBarry Smith /* fill[0 -- w] marks starts of each row of column indices (and end of last row) 34ce308e1dSBarry Smith so fill[1] - fill[0] gives number of nonzeros in first row etc */ 3547c6ae99SBarry Smith nz = w + 1; 3647c6ae99SBarry Smith for (i = 0; i < w; i++) { 3747c6ae99SBarry Smith fill[i] = nz; 3847c6ae99SBarry Smith for (j = 0; j < w; j++) { 3947c6ae99SBarry Smith if (dfill[w * i + j]) { 4047c6ae99SBarry Smith fill[nz] = j; 4147c6ae99SBarry Smith nz++; 4247c6ae99SBarry Smith } 4347c6ae99SBarry Smith } 4447c6ae99SBarry Smith } 4547c6ae99SBarry Smith fill[w] = nz; 4647c6ae99SBarry Smith 4747c6ae99SBarry Smith *rfill = fill; 483ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4947c6ae99SBarry Smith } 5047c6ae99SBarry Smith 51d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDASetBlockFillsSparse_Private(const PetscInt *dfillsparse, PetscInt w, PetscInt **rfill) 52d71ae5a4SJacob Faibussowitsch { 53767d920cSKarl Rupp PetscInt nz; 5409e28618SBarry Smith 5509e28618SBarry Smith PetscFunctionBegin; 563ba16761SJacob Faibussowitsch if (!dfillsparse) PetscFunctionReturn(PETSC_SUCCESS); 5709e28618SBarry Smith 5809e28618SBarry Smith /* Determine number of non-zeros */ 5909e28618SBarry Smith nz = (dfillsparse[w] - w - 1); 6009e28618SBarry Smith 6109e28618SBarry Smith /* Allocate space for our copy of the given sparse matrix representation. */ 629566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nz + w + 1, rfill)); 639566063dSJacob Faibussowitsch PetscCall(PetscArraycpy(*rfill, dfillsparse, nz + w + 1)); 643ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 6509e28618SBarry Smith } 6609e28618SBarry Smith 67d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDASetBlockFills_Private2(DM_DA *dd) 68d71ae5a4SJacob Faibussowitsch { 6909e28618SBarry Smith PetscInt i, k, cnt = 1; 7009e28618SBarry Smith 7109e28618SBarry Smith PetscFunctionBegin; 7209e28618SBarry Smith 7309e28618SBarry Smith /* ofillcount tracks the columns of ofill that have any nonzero in thems; the value in each location is the number of 7409e28618SBarry Smith columns to the left with any nonzeros in them plus 1 */ 759566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(dd->w, &dd->ofillcols)); 7609e28618SBarry Smith for (i = 0; i < dd->w; i++) { 7709e28618SBarry Smith for (k = dd->ofill[i]; k < dd->ofill[i + 1]; k++) dd->ofillcols[dd->ofill[k]] = 1; 7809e28618SBarry Smith } 7909e28618SBarry Smith for (i = 0; i < dd->w; i++) { 80ad540459SPierre Jolivet if (dd->ofillcols[i]) dd->ofillcols[i] = cnt++; 8109e28618SBarry Smith } 823ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 8309e28618SBarry Smith } 8409e28618SBarry Smith 8547c6ae99SBarry Smith /*@ 86aa219208SBarry Smith DMDASetBlockFills - Sets the fill pattern in each block for a multi-component problem 87dce8aebaSBarry Smith of the matrix returned by `DMCreateMatrix()`. 8847c6ae99SBarry Smith 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 100dce8aebaSBarry Smith `MATMPIAIJ` matrix format 10147c6ae99SBarry Smith 10247c6ae99SBarry Smith The format for dfill and ofill is a 2 dimensional dof by dof matrix with 1 entries 10347c6ae99SBarry Smith representing coupling and 0 entries for missing coupling. For example 104dce8aebaSBarry Smith .vb 105dce8aebaSBarry Smith dfill[9] = {1, 0, 0, 106dce8aebaSBarry Smith 1, 1, 0, 107dce8aebaSBarry Smith 0, 1, 1} 108dce8aebaSBarry Smith .ve 10947c6ae99SBarry Smith means that row 0 is coupled with only itself in the diagonal block, row 1 is coupled with 11047c6ae99SBarry Smith itself and row 0 (in the diagonal block) and row 2 is coupled with itself and row 1 (in the 11147c6ae99SBarry Smith diagonal block). 11247c6ae99SBarry Smith 113dce8aebaSBarry Smith `DMDASetGetMatrix()` allows you to provide general code for those more complicated nonzero patterns then 11447c6ae99SBarry Smith can be represented in the dfill, ofill format 11547c6ae99SBarry Smith 11647c6ae99SBarry Smith Contributed by Glenn Hammond 11747c6ae99SBarry Smith 118dce8aebaSBarry Smith .seealso: `DM`, `DMDA`, `DMCreateMatrix()`, `DMDASetGetMatrix()`, `DMSetMatrixPreallocateOnly()` 11947c6ae99SBarry Smith @*/ 120d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDASetBlockFills(DM da, const PetscInt *dfill, const PetscInt *ofill) 121d71ae5a4SJacob Faibussowitsch { 12247c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 12347c6ae99SBarry Smith 12447c6ae99SBarry Smith PetscFunctionBegin; 12509e28618SBarry Smith /* save the given dfill and ofill information */ 1269566063dSJacob Faibussowitsch PetscCall(DMDASetBlockFills_Private(dfill, dd->w, &dd->dfill)); 1279566063dSJacob Faibussowitsch PetscCall(DMDASetBlockFills_Private(ofill, dd->w, &dd->ofill)); 128ae4f298aSBarry Smith 12909e28618SBarry Smith /* count nonzeros in ofill columns */ 1309566063dSJacob Faibussowitsch PetscCall(DMDASetBlockFills_Private2(dd)); 1313ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 132ae4f298aSBarry Smith } 13309e28618SBarry Smith 13409e28618SBarry Smith /*@ 13509e28618SBarry Smith DMDASetBlockFillsSparse - Sets the fill pattern in each block for a multi-component problem 136dce8aebaSBarry Smith of the matrix returned by `DMCreateMatrix()`, using sparse representations 13709e28618SBarry Smith of fill patterns. 13809e28618SBarry Smith 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 148dce8aebaSBarry Smith Notes: 149dce8aebaSBarry Smith This only makes sense when you are doing multicomponent problems but using the 150dce8aebaSBarry Smith `MATMPIAIJ` matrix format 15109e28618SBarry Smith 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 163dce8aebaSBarry Smith same format as that computed by the `DMDASetBlockFills_Private()` 16409e28618SBarry Smith function from a dense 2D matrix representation. 16509e28618SBarry Smith 166dce8aebaSBarry Smith `DMDASetGetMatrix()` allows you to provide general code for those more complicated nonzero patterns then 16709e28618SBarry Smith can be represented in the dfill, ofill format 16809e28618SBarry Smith 16909e28618SBarry Smith Contributed by Philip C. Roth 17009e28618SBarry Smith 171dce8aebaSBarry Smith .seealso: `DM`, `DMDA`, `DMDASetBlockFills()`, `DMCreateMatrix()`, `DMDASetGetMatrix()`, `DMSetMatrixPreallocateOnly()` 17209e28618SBarry Smith @*/ 173d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDASetBlockFillsSparse(DM da, const PetscInt *dfillsparse, const PetscInt *ofillsparse) 174d71ae5a4SJacob Faibussowitsch { 17509e28618SBarry Smith DM_DA *dd = (DM_DA *)da->data; 17609e28618SBarry Smith 17709e28618SBarry Smith PetscFunctionBegin; 17809e28618SBarry Smith /* save the given dfill and ofill information */ 1799566063dSJacob Faibussowitsch PetscCall(DMDASetBlockFillsSparse_Private(dfillsparse, dd->w, &dd->dfill)); 1809566063dSJacob Faibussowitsch PetscCall(DMDASetBlockFillsSparse_Private(ofillsparse, dd->w, &dd->ofill)); 18109e28618SBarry Smith 18209e28618SBarry Smith /* count nonzeros in ofill columns */ 1839566063dSJacob Faibussowitsch PetscCall(DMDASetBlockFills_Private2(dd)); 1843ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 18547c6ae99SBarry Smith } 18647c6ae99SBarry Smith 187d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA(DM da, ISColoringType ctype, ISColoring *coloring) 188d71ae5a4SJacob Faibussowitsch { 18947c6ae99SBarry Smith PetscInt dim, m, n, p, nc; 190bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by, bz; 19147c6ae99SBarry Smith MPI_Comm comm; 19247c6ae99SBarry Smith PetscMPIInt size; 19347c6ae99SBarry Smith PetscBool isBAIJ; 19447c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 19547c6ae99SBarry Smith 19647c6ae99SBarry Smith PetscFunctionBegin; 19747c6ae99SBarry Smith /* 19847c6ae99SBarry Smith m 19947c6ae99SBarry Smith ------------------------------------------------------ 20047c6ae99SBarry Smith | | 20147c6ae99SBarry Smith | | 20247c6ae99SBarry Smith | ---------------------- | 20347c6ae99SBarry Smith | | | | 20447c6ae99SBarry Smith n | yn | | | 20547c6ae99SBarry Smith | | | | 20647c6ae99SBarry Smith | .--------------------- | 20747c6ae99SBarry Smith | (xs,ys) xn | 20847c6ae99SBarry Smith | . | 20947c6ae99SBarry Smith | (gxs,gys) | 21047c6ae99SBarry Smith | | 21147c6ae99SBarry Smith ----------------------------------------------------- 21247c6ae99SBarry Smith */ 21347c6ae99SBarry Smith 21447c6ae99SBarry Smith /* 21547c6ae99SBarry Smith nc - number of components per grid point 21647c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 21747c6ae99SBarry Smith 21847c6ae99SBarry Smith */ 2199566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, NULL, NULL, NULL, &m, &n, &p, &nc, NULL, &bx, &by, &bz, NULL)); 22047c6ae99SBarry Smith 2219566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 2229566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(comm, &size)); 2235bdb020cSBarry Smith if (ctype == IS_COLORING_LOCAL) { 22447c6ae99SBarry Smith if (size == 1) { 22547c6ae99SBarry Smith ctype = IS_COLORING_GLOBAL; 226*2ddab442SBarry Smith } else { 227*2ddab442SBarry Smith PetscCheck((dim == 1) || !((m == 1 && bx == DM_BOUNDARY_PERIODIC) || (n == 1 && by == DM_BOUNDARY_PERIODIC) || (p == 1 && bz == DM_BOUNDARY_PERIODIC)), PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "IS_COLORING_LOCAL cannot be used for periodic boundary condition having both ends of the domain on the same process"); 22847c6ae99SBarry Smith } 22947c6ae99SBarry Smith } 23047c6ae99SBarry Smith 231aa219208SBarry Smith /* Tell the DMDA it has 1 degree of freedom per grid point so that the coloring for BAIJ 23247c6ae99SBarry Smith matrices is for the blocks, not the individual matrix elements */ 2339566063dSJacob Faibussowitsch PetscCall(PetscStrbeginswith(da->mattype, MATBAIJ, &isBAIJ)); 2349566063dSJacob Faibussowitsch if (!isBAIJ) PetscCall(PetscStrbeginswith(da->mattype, MATMPIBAIJ, &isBAIJ)); 2359566063dSJacob Faibussowitsch if (!isBAIJ) PetscCall(PetscStrbeginswith(da->mattype, MATSEQBAIJ, &isBAIJ)); 23647c6ae99SBarry Smith if (isBAIJ) { 23747c6ae99SBarry Smith dd->w = 1; 23847c6ae99SBarry Smith dd->xs = dd->xs / nc; 23947c6ae99SBarry Smith dd->xe = dd->xe / nc; 24047c6ae99SBarry Smith dd->Xs = dd->Xs / nc; 24147c6ae99SBarry Smith dd->Xe = dd->Xe / nc; 24247c6ae99SBarry Smith } 24347c6ae99SBarry Smith 24447c6ae99SBarry Smith /* 245aa219208SBarry Smith We do not provide a getcoloring function in the DMDA operations because 2469a1b256bSStefano Zampini the basic DMDA does not know about matrices. We think of DMDA as being 24747c6ae99SBarry Smith more low-level then matrices. 24847c6ae99SBarry Smith */ 2491baa6e33SBarry Smith if (dim == 1) PetscCall(DMCreateColoring_DA_1d_MPIAIJ(da, ctype, coloring)); 2501baa6e33SBarry Smith else if (dim == 2) PetscCall(DMCreateColoring_DA_2d_MPIAIJ(da, ctype, coloring)); 2511baa6e33SBarry Smith else if (dim == 3) PetscCall(DMCreateColoring_DA_3d_MPIAIJ(da, ctype, coloring)); 2521baa6e33SBarry Smith else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "Not done for %" PetscInt_FMT " dimension, send us mail petsc-maint@mcs.anl.gov for code", dim); 25347c6ae99SBarry Smith if (isBAIJ) { 25447c6ae99SBarry Smith dd->w = nc; 25547c6ae99SBarry Smith dd->xs = dd->xs * nc; 25647c6ae99SBarry Smith dd->xe = dd->xe * nc; 25747c6ae99SBarry Smith dd->Xs = dd->Xs * nc; 25847c6ae99SBarry Smith dd->Xe = dd->Xe * nc; 25947c6ae99SBarry Smith } 2603ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 26147c6ae99SBarry Smith } 26247c6ae99SBarry Smith 26347c6ae99SBarry Smith /* ---------------------------------------------------------------------------------*/ 26447c6ae99SBarry Smith 265d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA_2d_MPIAIJ(DM da, ISColoringType ctype, ISColoring *coloring) 266d71ae5a4SJacob Faibussowitsch { 26747c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, ii, gxs, gys, gnx, gny, m, n, M, N, dim, s, k, nc, col; 268dec0b466SHong Zhang PetscInt ncolors = 0; 26947c6ae99SBarry Smith MPI_Comm comm; 270bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 271aa219208SBarry Smith DMDAStencilType st; 27247c6ae99SBarry Smith ISColoringValue *colors; 27347c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 27447c6ae99SBarry Smith 27547c6ae99SBarry Smith PetscFunctionBegin; 27647c6ae99SBarry Smith /* 27747c6ae99SBarry Smith nc - number of components per grid point 27847c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 27947c6ae99SBarry Smith 28047c6ae99SBarry Smith */ 2819566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, &M, &N, NULL, &nc, &s, &bx, &by, NULL, &st)); 28247c6ae99SBarry Smith col = 2 * s + 1; 2839566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 2849566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 2859566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 28647c6ae99SBarry Smith 28747c6ae99SBarry Smith /* special case as taught to us by Paul Hovland */ 288aa219208SBarry Smith if (st == DMDA_STENCIL_STAR && s == 1) { 2899566063dSJacob Faibussowitsch PetscCall(DMCreateColoring_DA_2d_5pt_MPIAIJ(da, ctype, coloring)); 29047c6ae99SBarry Smith } else { 29147c6ae99SBarry Smith if (ctype == IS_COLORING_GLOBAL) { 29247c6ae99SBarry Smith if (!dd->localcoloring) { 2939566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * nx * ny, &colors)); 29447c6ae99SBarry Smith ii = 0; 29547c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 29647c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 297ad540459SPierre Jolivet for (k = 0; k < nc; k++) colors[ii++] = k + nc * ((i % col) + col * (j % col)); 29847c6ae99SBarry Smith } 29947c6ae99SBarry Smith } 30047c6ae99SBarry Smith ncolors = nc + nc * (col - 1 + col * (col - 1)); 3019566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * nx * ny, colors, PETSC_OWN_POINTER, &dd->localcoloring)); 30247c6ae99SBarry Smith } 30347c6ae99SBarry Smith *coloring = dd->localcoloring; 3045bdb020cSBarry Smith } else if (ctype == IS_COLORING_LOCAL) { 30547c6ae99SBarry Smith if (!dd->ghostedcoloring) { 3069566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * gnx * gny, &colors)); 30747c6ae99SBarry Smith ii = 0; 30847c6ae99SBarry Smith for (j = gys; j < gys + gny; j++) { 30947c6ae99SBarry Smith for (i = gxs; i < gxs + gnx; i++) { 31047c6ae99SBarry Smith for (k = 0; k < nc; k++) { 31147c6ae99SBarry Smith /* the complicated stuff is to handle periodic boundaries */ 31247c6ae99SBarry Smith colors[ii++] = k + nc * ((SetInRange(i, m) % col) + col * (SetInRange(j, n) % col)); 31347c6ae99SBarry Smith } 31447c6ae99SBarry Smith } 31547c6ae99SBarry Smith } 31647c6ae99SBarry Smith ncolors = nc + nc * (col - 1 + col * (col - 1)); 3179566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * gnx * gny, colors, PETSC_OWN_POINTER, &dd->ghostedcoloring)); 31847c6ae99SBarry Smith /* PetscIntView(ncolors,(PetscInt*)colors,0); */ 31947c6ae99SBarry Smith 3209566063dSJacob Faibussowitsch PetscCall(ISColoringSetType(dd->ghostedcoloring, IS_COLORING_LOCAL)); 32147c6ae99SBarry Smith } 32247c6ae99SBarry Smith *coloring = dd->ghostedcoloring; 32398921bdaSJacob Faibussowitsch } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_ARG_WRONG, "Unknown ISColoringType %d", (int)ctype); 32447c6ae99SBarry Smith } 3259566063dSJacob Faibussowitsch PetscCall(ISColoringReference(*coloring)); 3263ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 32747c6ae99SBarry Smith } 32847c6ae99SBarry Smith 32947c6ae99SBarry Smith /* ---------------------------------------------------------------------------------*/ 33047c6ae99SBarry Smith 331d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA_3d_MPIAIJ(DM da, ISColoringType ctype, ISColoring *coloring) 332d71ae5a4SJacob Faibussowitsch { 33347c6ae99SBarry 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; 33447c6ae99SBarry Smith PetscInt ncolors; 33547c6ae99SBarry Smith MPI_Comm comm; 336bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by, bz; 337aa219208SBarry Smith DMDAStencilType st; 33847c6ae99SBarry Smith ISColoringValue *colors; 33947c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 34047c6ae99SBarry Smith 34147c6ae99SBarry Smith PetscFunctionBegin; 34247c6ae99SBarry Smith /* 34347c6ae99SBarry Smith nc - number of components per grid point 34447c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 34547c6ae99SBarry Smith */ 3469566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &nc, &s, &bx, &by, &bz, &st)); 34747c6ae99SBarry Smith col = 2 * s + 1; 3489566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 3499566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 3509566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 35147c6ae99SBarry Smith 35247c6ae99SBarry Smith /* create the coloring */ 35347c6ae99SBarry Smith if (ctype == IS_COLORING_GLOBAL) { 35447c6ae99SBarry Smith if (!dd->localcoloring) { 3559566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * nx * ny * nz, &colors)); 35647c6ae99SBarry Smith ii = 0; 35747c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 35847c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 35947c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 360ad540459SPierre Jolivet for (l = 0; l < nc; l++) colors[ii++] = l + nc * ((i % col) + col * (j % col) + col * col * (k % col)); 36147c6ae99SBarry Smith } 36247c6ae99SBarry Smith } 36347c6ae99SBarry Smith } 36447c6ae99SBarry Smith ncolors = nc + nc * (col - 1 + col * (col - 1) + col * col * (col - 1)); 3659566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * nx * ny * nz, colors, PETSC_OWN_POINTER, &dd->localcoloring)); 36647c6ae99SBarry Smith } 36747c6ae99SBarry Smith *coloring = dd->localcoloring; 3685bdb020cSBarry Smith } else if (ctype == IS_COLORING_LOCAL) { 36947c6ae99SBarry Smith if (!dd->ghostedcoloring) { 3709566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * gnx * gny * gnz, &colors)); 37147c6ae99SBarry Smith ii = 0; 37247c6ae99SBarry Smith for (k = gzs; k < gzs + gnz; k++) { 37347c6ae99SBarry Smith for (j = gys; j < gys + gny; j++) { 37447c6ae99SBarry Smith for (i = gxs; i < gxs + gnx; i++) { 37547c6ae99SBarry Smith for (l = 0; l < nc; l++) { 37647c6ae99SBarry Smith /* the complicated stuff is to handle periodic boundaries */ 37747c6ae99SBarry Smith colors[ii++] = l + nc * ((SetInRange(i, m) % col) + col * (SetInRange(j, n) % col) + col * col * (SetInRange(k, p) % col)); 37847c6ae99SBarry Smith } 37947c6ae99SBarry Smith } 38047c6ae99SBarry Smith } 38147c6ae99SBarry Smith } 38247c6ae99SBarry Smith ncolors = nc + nc * (col - 1 + col * (col - 1) + col * col * (col - 1)); 3839566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * gnx * gny * gnz, colors, PETSC_OWN_POINTER, &dd->ghostedcoloring)); 3849566063dSJacob Faibussowitsch PetscCall(ISColoringSetType(dd->ghostedcoloring, IS_COLORING_LOCAL)); 38547c6ae99SBarry Smith } 38647c6ae99SBarry Smith *coloring = dd->ghostedcoloring; 38798921bdaSJacob Faibussowitsch } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_ARG_WRONG, "Unknown ISColoringType %d", (int)ctype); 3889566063dSJacob Faibussowitsch PetscCall(ISColoringReference(*coloring)); 3893ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 39047c6ae99SBarry Smith } 39147c6ae99SBarry Smith 39247c6ae99SBarry Smith /* ---------------------------------------------------------------------------------*/ 39347c6ae99SBarry Smith 394d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA_1d_MPIAIJ(DM da, ISColoringType ctype, ISColoring *coloring) 395d71ae5a4SJacob Faibussowitsch { 39647c6ae99SBarry Smith PetscInt xs, nx, i, i1, gxs, gnx, l, m, M, dim, s, nc, col; 39747c6ae99SBarry Smith PetscInt ncolors; 39847c6ae99SBarry Smith MPI_Comm comm; 399bff4a2f0SMatthew G. Knepley DMBoundaryType bx; 40047c6ae99SBarry Smith ISColoringValue *colors; 40147c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 40247c6ae99SBarry Smith 40347c6ae99SBarry Smith PetscFunctionBegin; 40447c6ae99SBarry Smith /* 40547c6ae99SBarry Smith nc - number of components per grid point 40647c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 40747c6ae99SBarry Smith */ 4089566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, NULL, NULL, &M, NULL, NULL, &nc, &s, &bx, NULL, NULL, NULL)); 40947c6ae99SBarry Smith col = 2 * s + 1; 4109566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &nx, NULL, NULL)); 4119566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, NULL, NULL, &gnx, NULL, NULL)); 4129566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 41347c6ae99SBarry Smith 41447c6ae99SBarry Smith /* create the coloring */ 41547c6ae99SBarry Smith if (ctype == IS_COLORING_GLOBAL) { 41647c6ae99SBarry Smith if (!dd->localcoloring) { 4179566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * nx, &colors)); 418ae4f298aSBarry Smith if (dd->ofillcols) { 419ae4f298aSBarry Smith PetscInt tc = 0; 420ae4f298aSBarry Smith for (i = 0; i < nc; i++) tc += (PetscInt)(dd->ofillcols[i] > 0); 421ae4f298aSBarry Smith i1 = 0; 422ae4f298aSBarry Smith for (i = xs; i < xs + nx; i++) { 423ae4f298aSBarry Smith for (l = 0; l < nc; l++) { 424ae4f298aSBarry Smith if (dd->ofillcols[l] && (i % col)) { 425ae4f298aSBarry Smith colors[i1++] = nc - 1 + tc * ((i % col) - 1) + dd->ofillcols[l]; 426ae4f298aSBarry Smith } else { 427ae4f298aSBarry Smith colors[i1++] = l; 428ae4f298aSBarry Smith } 429ae4f298aSBarry Smith } 430ae4f298aSBarry Smith } 431ae4f298aSBarry Smith ncolors = nc + 2 * s * tc; 432ae4f298aSBarry Smith } else { 43347c6ae99SBarry Smith i1 = 0; 43447c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 435ad540459SPierre Jolivet for (l = 0; l < nc; l++) colors[i1++] = l + nc * (i % col); 43647c6ae99SBarry Smith } 43747c6ae99SBarry Smith ncolors = nc + nc * (col - 1); 438ae4f298aSBarry Smith } 4399566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * nx, colors, PETSC_OWN_POINTER, &dd->localcoloring)); 44047c6ae99SBarry Smith } 44147c6ae99SBarry Smith *coloring = dd->localcoloring; 4425bdb020cSBarry Smith } else if (ctype == IS_COLORING_LOCAL) { 44347c6ae99SBarry Smith if (!dd->ghostedcoloring) { 4449566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * gnx, &colors)); 44547c6ae99SBarry Smith i1 = 0; 44647c6ae99SBarry Smith for (i = gxs; i < gxs + gnx; i++) { 44747c6ae99SBarry Smith for (l = 0; l < nc; l++) { 44847c6ae99SBarry Smith /* the complicated stuff is to handle periodic boundaries */ 44947c6ae99SBarry Smith colors[i1++] = l + nc * (SetInRange(i, m) % col); 45047c6ae99SBarry Smith } 45147c6ae99SBarry Smith } 45247c6ae99SBarry Smith ncolors = nc + nc * (col - 1); 4539566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * gnx, colors, PETSC_OWN_POINTER, &dd->ghostedcoloring)); 4549566063dSJacob Faibussowitsch PetscCall(ISColoringSetType(dd->ghostedcoloring, IS_COLORING_LOCAL)); 45547c6ae99SBarry Smith } 45647c6ae99SBarry Smith *coloring = dd->ghostedcoloring; 45798921bdaSJacob Faibussowitsch } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_ARG_WRONG, "Unknown ISColoringType %d", (int)ctype); 4589566063dSJacob Faibussowitsch PetscCall(ISColoringReference(*coloring)); 4593ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 46047c6ae99SBarry Smith } 46147c6ae99SBarry Smith 462d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA_2d_5pt_MPIAIJ(DM da, ISColoringType ctype, ISColoring *coloring) 463d71ae5a4SJacob Faibussowitsch { 46447c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, ii, gxs, gys, gnx, gny, m, n, dim, s, k, nc; 46547c6ae99SBarry Smith PetscInt ncolors; 46647c6ae99SBarry Smith MPI_Comm comm; 467bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 46847c6ae99SBarry Smith ISColoringValue *colors; 46947c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 47047c6ae99SBarry Smith 47147c6ae99SBarry Smith PetscFunctionBegin; 47247c6ae99SBarry Smith /* 47347c6ae99SBarry Smith nc - number of components per grid point 47447c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 47547c6ae99SBarry Smith */ 4769566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, NULL, NULL, NULL, &nc, &s, &bx, &by, NULL, NULL)); 4779566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 4789566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 4799566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 48047c6ae99SBarry Smith /* create the coloring */ 48147c6ae99SBarry Smith if (ctype == IS_COLORING_GLOBAL) { 48247c6ae99SBarry Smith if (!dd->localcoloring) { 4839566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * nx * ny, &colors)); 48447c6ae99SBarry Smith ii = 0; 48547c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 48647c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 487ad540459SPierre Jolivet for (k = 0; k < nc; k++) colors[ii++] = k + nc * ((3 * j + i) % 5); 48847c6ae99SBarry Smith } 48947c6ae99SBarry Smith } 49047c6ae99SBarry Smith ncolors = 5 * nc; 4919566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * nx * ny, colors, PETSC_OWN_POINTER, &dd->localcoloring)); 49247c6ae99SBarry Smith } 49347c6ae99SBarry Smith *coloring = dd->localcoloring; 4945bdb020cSBarry Smith } else if (ctype == IS_COLORING_LOCAL) { 49547c6ae99SBarry Smith if (!dd->ghostedcoloring) { 4969566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * gnx * gny, &colors)); 49747c6ae99SBarry Smith ii = 0; 49847c6ae99SBarry Smith for (j = gys; j < gys + gny; j++) { 49947c6ae99SBarry Smith for (i = gxs; i < gxs + gnx; i++) { 500ad540459SPierre Jolivet for (k = 0; k < nc; k++) colors[ii++] = k + nc * ((3 * SetInRange(j, n) + SetInRange(i, m)) % 5); 50147c6ae99SBarry Smith } 50247c6ae99SBarry Smith } 50347c6ae99SBarry Smith ncolors = 5 * nc; 5049566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * gnx * gny, colors, PETSC_OWN_POINTER, &dd->ghostedcoloring)); 5059566063dSJacob Faibussowitsch PetscCall(ISColoringSetType(dd->ghostedcoloring, IS_COLORING_LOCAL)); 50647c6ae99SBarry Smith } 50747c6ae99SBarry Smith *coloring = dd->ghostedcoloring; 50898921bdaSJacob Faibussowitsch } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_ARG_WRONG, "Unknown ISColoringType %d", (int)ctype); 5093ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 51047c6ae99SBarry Smith } 51147c6ae99SBarry Smith 51247c6ae99SBarry Smith /* =========================================================================== */ 513e432b41dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_1d_MPIAIJ(DM, Mat); 514ce308e1dSBarry Smith extern PetscErrorCode DMCreateMatrix_DA_1d_MPIAIJ_Fill(DM, Mat); 515e432b41dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_1d_SeqAIJ_NoPreallocation(DM, Mat); 516e432b41dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_2d_MPIAIJ(DM, Mat); 517950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_2d_MPIAIJ_Fill(DM, Mat); 518e432b41dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_3d_MPIAIJ(DM, Mat); 519950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_3d_MPIAIJ_Fill(DM, Mat); 520950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_2d_MPIBAIJ(DM, Mat); 521950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_3d_MPIBAIJ(DM, Mat); 522950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_2d_MPISBAIJ(DM, Mat); 523950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_3d_MPISBAIJ(DM, Mat); 524d4002b98SHong Zhang extern PetscErrorCode DMCreateMatrix_DA_2d_MPISELL(DM, Mat); 525d4002b98SHong Zhang extern PetscErrorCode DMCreateMatrix_DA_3d_MPISELL(DM, Mat); 526e584696dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_IS(DM, Mat); 52747c6ae99SBarry Smith 5288bbdbebaSMatthew G Knepley /*@C 529dce8aebaSBarry Smith MatSetupDM - Sets the `DMDA` that is to be used by the HYPRE_StructMatrix PETSc matrix 53047c6ae99SBarry Smith 531d083f849SBarry Smith Logically Collective on mat 53247c6ae99SBarry Smith 53347c6ae99SBarry Smith Input Parameters: 53447c6ae99SBarry Smith + mat - the matrix 53547c6ae99SBarry Smith - da - the da 53647c6ae99SBarry Smith 53747c6ae99SBarry Smith Level: intermediate 53847c6ae99SBarry Smith 539dce8aebaSBarry Smith .seealso: `Mat`, `MatSetUp()` 54047c6ae99SBarry Smith @*/ 541d71ae5a4SJacob Faibussowitsch PetscErrorCode MatSetupDM(Mat mat, DM da) 542d71ae5a4SJacob Faibussowitsch { 54347c6ae99SBarry Smith PetscFunctionBegin; 54447c6ae99SBarry Smith PetscValidHeaderSpecific(mat, MAT_CLASSID, 1); 545064a246eSJacob Faibussowitsch PetscValidHeaderSpecificType(da, DM_CLASSID, 2, DMDA); 546cac4c232SBarry Smith PetscTryMethod(mat, "MatSetupDM_C", (Mat, DM), (mat, da)); 5473ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 54847c6ae99SBarry Smith } 54947c6ae99SBarry Smith 550d71ae5a4SJacob Faibussowitsch PetscErrorCode MatView_MPI_DA(Mat A, PetscViewer viewer) 551d71ae5a4SJacob Faibussowitsch { 5529a42bb27SBarry Smith DM da; 55347c6ae99SBarry Smith const char *prefix; 55447c6ae99SBarry Smith Mat Anatural; 55547c6ae99SBarry Smith AO ao; 55647c6ae99SBarry Smith PetscInt rstart, rend, *petsc, i; 55747c6ae99SBarry Smith IS is; 55847c6ae99SBarry Smith MPI_Comm comm; 55974388724SJed Brown PetscViewerFormat format; 56047c6ae99SBarry Smith 56147c6ae99SBarry Smith PetscFunctionBegin; 56274388724SJed Brown /* Check whether we are just printing info, in which case MatView() already viewed everything we wanted to view */ 5639566063dSJacob Faibussowitsch PetscCall(PetscViewerGetFormat(viewer, &format)); 5643ba16761SJacob Faibussowitsch if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) PetscFunctionReturn(PETSC_SUCCESS); 56574388724SJed Brown 5669566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)A, &comm)); 5679566063dSJacob Faibussowitsch PetscCall(MatGetDM(A, &da)); 5687a8be351SBarry Smith PetscCheck(da, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Matrix not generated from a DMDA"); 56947c6ae99SBarry Smith 5709566063dSJacob Faibussowitsch PetscCall(DMDAGetAO(da, &ao)); 5719566063dSJacob Faibussowitsch PetscCall(MatGetOwnershipRange(A, &rstart, &rend)); 5729566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(rend - rstart, &petsc)); 57347c6ae99SBarry Smith for (i = rstart; i < rend; i++) petsc[i - rstart] = i; 5749566063dSJacob Faibussowitsch PetscCall(AOApplicationToPetsc(ao, rend - rstart, petsc)); 5759566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(comm, rend - rstart, petsc, PETSC_OWN_POINTER, &is)); 57647c6ae99SBarry Smith 57747c6ae99SBarry Smith /* call viewer on natural ordering */ 5789566063dSJacob Faibussowitsch PetscCall(MatCreateSubMatrix(A, is, is, MAT_INITIAL_MATRIX, &Anatural)); 5799566063dSJacob Faibussowitsch PetscCall(ISDestroy(&is)); 5809566063dSJacob Faibussowitsch PetscCall(PetscObjectGetOptionsPrefix((PetscObject)A, &prefix)); 5819566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptionsPrefix((PetscObject)Anatural, prefix)); 5829566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)Anatural, ((PetscObject)A)->name)); 583f0ed2f47SStefano Zampini ((PetscObject)Anatural)->donotPetscObjectPrintClassNamePrefixType = PETSC_TRUE; 5849566063dSJacob Faibussowitsch PetscCall(MatView(Anatural, viewer)); 585f0ed2f47SStefano Zampini ((PetscObject)Anatural)->donotPetscObjectPrintClassNamePrefixType = PETSC_FALSE; 5869566063dSJacob Faibussowitsch PetscCall(MatDestroy(&Anatural)); 5873ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 58847c6ae99SBarry Smith } 58947c6ae99SBarry Smith 590d71ae5a4SJacob Faibussowitsch PetscErrorCode MatLoad_MPI_DA(Mat A, PetscViewer viewer) 591d71ae5a4SJacob Faibussowitsch { 5929a42bb27SBarry Smith DM da; 59347c6ae99SBarry Smith Mat Anatural, Aapp; 59447c6ae99SBarry Smith AO ao; 595539c167fSBarry Smith PetscInt rstart, rend, *app, i, m, n, M, N; 59647c6ae99SBarry Smith IS is; 59747c6ae99SBarry Smith MPI_Comm comm; 59847c6ae99SBarry Smith 59947c6ae99SBarry Smith PetscFunctionBegin; 6009566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)A, &comm)); 6019566063dSJacob Faibussowitsch PetscCall(MatGetDM(A, &da)); 6027a8be351SBarry Smith PetscCheck(da, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Matrix not generated from a DMDA"); 60347c6ae99SBarry Smith 60447c6ae99SBarry Smith /* Load the matrix in natural ordering */ 6059566063dSJacob Faibussowitsch PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &Anatural)); 6069566063dSJacob Faibussowitsch PetscCall(MatSetType(Anatural, ((PetscObject)A)->type_name)); 6079566063dSJacob Faibussowitsch PetscCall(MatGetSize(A, &M, &N)); 6089566063dSJacob Faibussowitsch PetscCall(MatGetLocalSize(A, &m, &n)); 6099566063dSJacob Faibussowitsch PetscCall(MatSetSizes(Anatural, m, n, M, N)); 6109566063dSJacob Faibussowitsch PetscCall(MatLoad(Anatural, viewer)); 61147c6ae99SBarry Smith 61247c6ae99SBarry Smith /* Map natural ordering to application ordering and create IS */ 6139566063dSJacob Faibussowitsch PetscCall(DMDAGetAO(da, &ao)); 6149566063dSJacob Faibussowitsch PetscCall(MatGetOwnershipRange(Anatural, &rstart, &rend)); 6159566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(rend - rstart, &app)); 61647c6ae99SBarry Smith for (i = rstart; i < rend; i++) app[i - rstart] = i; 6179566063dSJacob Faibussowitsch PetscCall(AOPetscToApplication(ao, rend - rstart, app)); 6189566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(comm, rend - rstart, app, PETSC_OWN_POINTER, &is)); 61947c6ae99SBarry Smith 62047c6ae99SBarry Smith /* Do permutation and replace header */ 6219566063dSJacob Faibussowitsch PetscCall(MatCreateSubMatrix(Anatural, is, is, MAT_INITIAL_MATRIX, &Aapp)); 6229566063dSJacob Faibussowitsch PetscCall(MatHeaderReplace(A, &Aapp)); 6239566063dSJacob Faibussowitsch PetscCall(ISDestroy(&is)); 6249566063dSJacob Faibussowitsch PetscCall(MatDestroy(&Anatural)); 6253ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 62647c6ae99SBarry Smith } 62747c6ae99SBarry Smith 628d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA(DM da, Mat *J) 629d71ae5a4SJacob Faibussowitsch { 63047c6ae99SBarry Smith PetscInt dim, dof, nx, ny, nz, dims[3], starts[3], M, N, P; 63147c6ae99SBarry Smith Mat A; 63247c6ae99SBarry Smith MPI_Comm comm; 63319fd82e9SBarry Smith MatType Atype; 634b412c318SBarry Smith MatType mtype; 63547c6ae99SBarry Smith PetscMPIInt size; 63647c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 637ade515a3SBarry Smith void (*aij)(void) = NULL, (*baij)(void) = NULL, (*sbaij)(void) = NULL, (*sell)(void) = NULL, (*is)(void) = NULL; 63847c6ae99SBarry Smith 63947c6ae99SBarry Smith PetscFunctionBegin; 6409566063dSJacob Faibussowitsch PetscCall(MatInitializePackage()); 641b412c318SBarry Smith mtype = da->mattype; 64247c6ae99SBarry Smith 64347c6ae99SBarry Smith /* 64447c6ae99SBarry Smith m 64547c6ae99SBarry Smith ------------------------------------------------------ 64647c6ae99SBarry Smith | | 64747c6ae99SBarry Smith | | 64847c6ae99SBarry Smith | ---------------------- | 64947c6ae99SBarry Smith | | | | 65047c6ae99SBarry Smith n | ny | | | 65147c6ae99SBarry Smith | | | | 65247c6ae99SBarry Smith | .--------------------- | 65347c6ae99SBarry Smith | (xs,ys) nx | 65447c6ae99SBarry Smith | . | 65547c6ae99SBarry Smith | (gxs,gys) | 65647c6ae99SBarry Smith | | 65747c6ae99SBarry Smith ----------------------------------------------------- 65847c6ae99SBarry Smith */ 65947c6ae99SBarry Smith 66047c6ae99SBarry Smith /* 66147c6ae99SBarry Smith nc - number of components per grid point 66247c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 66347c6ae99SBarry Smith */ 664e30e807fSPeter Brune M = dd->M; 665e30e807fSPeter Brune N = dd->N; 666e30e807fSPeter Brune P = dd->P; 667c73cfb54SMatthew G. Knepley dim = da->dim; 668e30e807fSPeter Brune dof = dd->w; 6699566063dSJacob Faibussowitsch /* PetscCall(DMDAGetInfo(da,&dim,&M,&N,&P,NULL,NULL,NULL,&dof,NULL,NULL,NULL,NULL,NULL)); */ 6709566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, NULL, NULL, NULL, &nx, &ny, &nz)); 6719566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 6729566063dSJacob Faibussowitsch PetscCall(MatCreate(comm, &A)); 6739566063dSJacob Faibussowitsch PetscCall(MatSetSizes(A, dof * nx * ny * nz, dof * nx * ny * nz, dof * M * N * P, dof * M * N * P)); 6749566063dSJacob Faibussowitsch PetscCall(MatSetType(A, mtype)); 6759566063dSJacob Faibussowitsch PetscCall(MatSetFromOptions(A)); 67674427ab1SRichard Tran Mills if (dof * nx * ny * nz < da->bind_below) { 6779566063dSJacob Faibussowitsch PetscCall(MatSetBindingPropagates(A, PETSC_TRUE)); 6789566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(A, PETSC_TRUE)); 67974427ab1SRichard Tran Mills } 6809566063dSJacob Faibussowitsch PetscCall(MatSetDM(A, da)); 6811baa6e33SBarry Smith if (da->structure_only) PetscCall(MatSetOption(A, MAT_STRUCTURE_ONLY, PETSC_TRUE)); 6829566063dSJacob Faibussowitsch PetscCall(MatGetType(A, &Atype)); 68347c6ae99SBarry Smith /* 684aa219208SBarry Smith We do not provide a getmatrix function in the DMDA operations because 685aa219208SBarry Smith the basic DMDA does not know about matrices. We think of DMDA as being more 68647c6ae99SBarry Smith more low-level than matrices. This is kind of cheating but, cause sometimes 687aa219208SBarry Smith we think of DMDA has higher level than matrices. 68847c6ae99SBarry Smith 68947c6ae99SBarry Smith We could switch based on Atype (or mtype), but we do not since the 690844bd0d7SStefano Zampini specialized setting routines depend only on the particular preallocation 69147c6ae99SBarry Smith details of the matrix, not the type itself. 69247c6ae99SBarry Smith */ 6939566063dSJacob Faibussowitsch PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatMPIAIJSetPreallocation_C", &aij)); 69448a46eb9SPierre Jolivet if (!aij) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatSeqAIJSetPreallocation_C", &aij)); 69547c6ae99SBarry Smith if (!aij) { 6969566063dSJacob Faibussowitsch PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatMPIBAIJSetPreallocation_C", &baij)); 69748a46eb9SPierre Jolivet if (!baij) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatSeqBAIJSetPreallocation_C", &baij)); 69847c6ae99SBarry Smith if (!baij) { 6999566063dSJacob Faibussowitsch PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatMPISBAIJSetPreallocation_C", &sbaij)); 70048a46eb9SPierre Jolivet if (!sbaij) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatSeqSBAIJSetPreallocation_C", &sbaij)); 7015e26d47bSHong Zhang if (!sbaij) { 7029566063dSJacob Faibussowitsch PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatMPISELLSetPreallocation_C", &sell)); 70348a46eb9SPierre Jolivet if (!sell) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatSeqSELLSetPreallocation_C", &sell)); 7045e26d47bSHong Zhang } 70548a46eb9SPierre Jolivet if (!sell) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatISSetPreallocation_C", &is)); 70647c6ae99SBarry Smith } 70747c6ae99SBarry Smith } 70847c6ae99SBarry Smith if (aij) { 70947c6ae99SBarry Smith if (dim == 1) { 710ce308e1dSBarry Smith if (dd->ofill) { 7119566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_1d_MPIAIJ_Fill(da, A)); 712ce308e1dSBarry Smith } else { 71319b08ed1SBarry Smith DMBoundaryType bx; 71419b08ed1SBarry Smith PetscMPIInt size; 7159566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &bx, NULL, NULL, NULL)); 7169566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)da), &size)); 71719b08ed1SBarry Smith if (size == 1 && bx == DM_BOUNDARY_NONE) { 7189566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_1d_SeqAIJ_NoPreallocation(da, A)); 71919b08ed1SBarry Smith } else { 7209566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_1d_MPIAIJ(da, A)); 721ce308e1dSBarry Smith } 72219b08ed1SBarry Smith } 72347c6ae99SBarry Smith } else if (dim == 2) { 72447c6ae99SBarry Smith if (dd->ofill) { 7259566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_2d_MPIAIJ_Fill(da, A)); 72647c6ae99SBarry Smith } else { 7279566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_2d_MPIAIJ(da, A)); 72847c6ae99SBarry Smith } 72947c6ae99SBarry Smith } else if (dim == 3) { 73047c6ae99SBarry Smith if (dd->ofill) { 7319566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_3d_MPIAIJ_Fill(da, A)); 73247c6ae99SBarry Smith } else { 7339566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_3d_MPIAIJ(da, A)); 73447c6ae99SBarry Smith } 73547c6ae99SBarry Smith } 73647c6ae99SBarry Smith } else if (baij) { 73747c6ae99SBarry Smith if (dim == 2) { 7389566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_2d_MPIBAIJ(da, A)); 73947c6ae99SBarry Smith } else if (dim == 3) { 7409566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_3d_MPIBAIJ(da, A)); 74163a3b9bcSJacob 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); 74247c6ae99SBarry Smith } else if (sbaij) { 74347c6ae99SBarry Smith if (dim == 2) { 7449566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_2d_MPISBAIJ(da, A)); 74547c6ae99SBarry Smith } else if (dim == 3) { 7469566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_3d_MPISBAIJ(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); 748d4002b98SHong Zhang } else if (sell) { 7495e26d47bSHong Zhang if (dim == 2) { 7509566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_2d_MPISELL(da, A)); 751711261dbSHong Zhang } else if (dim == 3) { 7529566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_3d_MPISELL(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); 754e584696dSStefano Zampini } else if (is) { 7559566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_IS(da, A)); 756869776cdSLisandro Dalcin } else { 75745b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 758e584696dSStefano Zampini 7599566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(A, dof)); 7609566063dSJacob Faibussowitsch PetscCall(MatSetUp(A)); 7619566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 7629566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(A, ltog, ltog)); 76347c6ae99SBarry Smith } 7649566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &starts[0], &starts[1], &starts[2], &dims[0], &dims[1], &dims[2])); 7659566063dSJacob Faibussowitsch PetscCall(MatSetStencil(A, dim, dims, starts, dof)); 7669566063dSJacob Faibussowitsch PetscCall(MatSetDM(A, da)); 7679566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(comm, &size)); 76847c6ae99SBarry Smith if (size > 1) { 76947c6ae99SBarry Smith /* change viewer to display matrix in natural ordering */ 7709566063dSJacob Faibussowitsch PetscCall(MatSetOperation(A, MATOP_VIEW, (void (*)(void))MatView_MPI_DA)); 7719566063dSJacob Faibussowitsch PetscCall(MatSetOperation(A, MATOP_LOAD, (void (*)(void))MatLoad_MPI_DA)); 77247c6ae99SBarry Smith } 77347c6ae99SBarry Smith *J = A; 7743ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 77547c6ae99SBarry Smith } 77647c6ae99SBarry Smith 77747c6ae99SBarry Smith /* ---------------------------------------------------------------------------------*/ 778844bd0d7SStefano Zampini PETSC_EXTERN PetscErrorCode MatISSetPreallocation_IS(Mat, PetscInt, const PetscInt[], PetscInt, const PetscInt[]); 779844bd0d7SStefano Zampini 780d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_IS(DM dm, Mat J) 781d71ae5a4SJacob Faibussowitsch { 782e584696dSStefano Zampini DM_DA *da = (DM_DA *)dm->data; 783e432b41dSStefano Zampini Mat lJ, P; 784e584696dSStefano Zampini ISLocalToGlobalMapping ltog; 785e432b41dSStefano Zampini IS is; 786e432b41dSStefano Zampini PetscBT bt; 78705339c03SStefano Zampini const PetscInt *e_loc, *idx; 788e432b41dSStefano Zampini PetscInt i, nel, nen, nv, dof, *gidx, n, N; 789e584696dSStefano Zampini 790e584696dSStefano Zampini /* The l2g map of DMDA has all ghosted nodes, and e_loc is a subset of all the local nodes (including the ghosted) 791e432b41dSStefano Zampini We need to filter out the local indices that are not represented through the DMDAGetElements decomposition */ 792e584696dSStefano Zampini PetscFunctionBegin; 793e584696dSStefano Zampini dof = da->w; 7949566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, dof)); 7959566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(dm, <og)); 796e432b41dSStefano Zampini 797e432b41dSStefano Zampini /* flag local elements indices in local DMDA numbering */ 7989566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingGetSize(ltog, &nv)); 7999566063dSJacob Faibussowitsch PetscCall(PetscBTCreate(nv / dof, &bt)); 8009566063dSJacob Faibussowitsch PetscCall(DMDAGetElements(dm, &nel, &nen, &e_loc)); /* this will throw an error if the stencil type is not DMDA_STENCIL_BOX */ 8019566063dSJacob Faibussowitsch for (i = 0; i < nel * nen; i++) PetscCall(PetscBTSet(bt, e_loc[i])); 802e432b41dSStefano Zampini 803e432b41dSStefano Zampini /* filter out (set to -1) the global indices not used by the local elements */ 8049566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nv / dof, &gidx)); 8059566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingGetBlockIndices(ltog, &idx)); 8069566063dSJacob Faibussowitsch PetscCall(PetscArraycpy(gidx, idx, nv / dof)); 8079566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingRestoreBlockIndices(ltog, &idx)); 8089371c9d4SSatish Balay for (i = 0; i < nv / dof; i++) 8099371c9d4SSatish Balay if (!PetscBTLookup(bt, i)) gidx[i] = -1; 8109566063dSJacob Faibussowitsch PetscCall(PetscBTDestroy(&bt)); 8119566063dSJacob Faibussowitsch PetscCall(ISCreateBlock(PetscObjectComm((PetscObject)dm), dof, nv / dof, gidx, PETSC_OWN_POINTER, &is)); 8129566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingCreateIS(is, <og)); 8139566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 8149566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingDestroy(<og)); 8159566063dSJacob Faibussowitsch PetscCall(ISDestroy(&is)); 81605339c03SStefano Zampini 817e432b41dSStefano Zampini /* Preallocation */ 818e306f867SJed Brown if (dm->prealloc_skip) { 8199566063dSJacob Faibussowitsch PetscCall(MatSetUp(J)); 820e306f867SJed Brown } else { 8219566063dSJacob Faibussowitsch PetscCall(MatISGetLocalMat(J, &lJ)); 8229566063dSJacob Faibussowitsch PetscCall(MatGetLocalToGlobalMapping(lJ, <og, NULL)); 8239566063dSJacob Faibussowitsch PetscCall(MatCreate(PetscObjectComm((PetscObject)lJ), &P)); 8249566063dSJacob Faibussowitsch PetscCall(MatSetType(P, MATPREALLOCATOR)); 8259566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(P, ltog, ltog)); 8269566063dSJacob Faibussowitsch PetscCall(MatGetSize(lJ, &N, NULL)); 8279566063dSJacob Faibussowitsch PetscCall(MatGetLocalSize(lJ, &n, NULL)); 8289566063dSJacob Faibussowitsch PetscCall(MatSetSizes(P, n, n, N, N)); 8299566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(P, dof)); 8309566063dSJacob Faibussowitsch PetscCall(MatSetUp(P)); 83148a46eb9SPierre Jolivet for (i = 0; i < nel; i++) PetscCall(MatSetValuesBlockedLocal(P, nen, e_loc + i * nen, nen, e_loc + i * nen, NULL, INSERT_VALUES)); 8329566063dSJacob Faibussowitsch PetscCall(MatPreallocatorPreallocate(P, (PetscBool)!da->prealloc_only, lJ)); 8339566063dSJacob Faibussowitsch PetscCall(MatISRestoreLocalMat(J, &lJ)); 8349566063dSJacob Faibussowitsch PetscCall(DMDARestoreElements(dm, &nel, &nen, &e_loc)); 8359566063dSJacob Faibussowitsch PetscCall(MatDestroy(&P)); 836e432b41dSStefano Zampini 8379566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 8389566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 839e306f867SJed Brown } 8403ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 841e584696dSStefano Zampini } 842e584696dSStefano Zampini 843d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPISELL(DM da, Mat J) 844d71ae5a4SJacob Faibussowitsch { 8455e26d47bSHong 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; 8465e26d47bSHong Zhang PetscInt lstart, lend, pstart, pend, *dnz, *onz; 8475e26d47bSHong Zhang MPI_Comm comm; 8485e26d47bSHong Zhang PetscScalar *values; 8495e26d47bSHong Zhang DMBoundaryType bx, by; 8505e26d47bSHong Zhang ISLocalToGlobalMapping ltog; 8515e26d47bSHong Zhang DMDAStencilType st; 8525e26d47bSHong Zhang 8535e26d47bSHong Zhang PetscFunctionBegin; 8545e26d47bSHong Zhang /* 8555e26d47bSHong Zhang nc - number of components per grid point 8565e26d47bSHong Zhang col - number of colors needed in one direction for single component problem 8575e26d47bSHong Zhang */ 8589566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, NULL, NULL, NULL, &nc, &s, &bx, &by, NULL, &st)); 8595e26d47bSHong Zhang col = 2 * s + 1; 8609566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 8619566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 8629566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 8635e26d47bSHong Zhang 8649566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * col * nc * nc, &cols)); 8659566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 8665e26d47bSHong Zhang 8679566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 8685e26d47bSHong Zhang /* determine the matrix preallocation information */ 869d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny, nc * nx * ny, dnz, onz); 8705e26d47bSHong Zhang for (i = xs; i < xs + nx; i++) { 8715e26d47bSHong Zhang pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 8725e26d47bSHong Zhang pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 8735e26d47bSHong Zhang 8745e26d47bSHong Zhang for (j = ys; j < ys + ny; j++) { 8755e26d47bSHong Zhang slot = i - gxs + gnx * (j - gys); 8765e26d47bSHong Zhang 8775e26d47bSHong Zhang lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 8785e26d47bSHong Zhang lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 8795e26d47bSHong Zhang 8805e26d47bSHong Zhang cnt = 0; 8815e26d47bSHong Zhang for (k = 0; k < nc; k++) { 8825e26d47bSHong Zhang for (l = lstart; l < lend + 1; l++) { 8835e26d47bSHong Zhang for (p = pstart; p < pend + 1; p++) { 8845e26d47bSHong Zhang if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star have either l = 0 or p = 0 */ 8855e26d47bSHong Zhang cols[cnt++] = k + nc * (slot + gnx * l + p); 8865e26d47bSHong Zhang } 8875e26d47bSHong Zhang } 8885e26d47bSHong Zhang } 8895e26d47bSHong Zhang rows[k] = k + nc * (slot); 8905e26d47bSHong Zhang } 8919566063dSJacob Faibussowitsch PetscCall(MatPreallocateSetLocal(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 8925e26d47bSHong Zhang } 8935e26d47bSHong Zhang } 8949566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 8959566063dSJacob Faibussowitsch PetscCall(MatSeqSELLSetPreallocation(J, 0, dnz)); 8969566063dSJacob Faibussowitsch PetscCall(MatMPISELLSetPreallocation(J, 0, dnz, 0, onz)); 897d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 8985e26d47bSHong Zhang 8999566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 9005e26d47bSHong Zhang 9015e26d47bSHong Zhang /* 9025e26d47bSHong Zhang For each node in the grid: we get the neighbors in the local (on processor ordering 9035e26d47bSHong Zhang that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 9045e26d47bSHong Zhang PETSc ordering. 9055e26d47bSHong Zhang */ 9065e26d47bSHong Zhang if (!da->prealloc_only) { 9079566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * nc * nc, &values)); 9085e26d47bSHong Zhang for (i = xs; i < xs + nx; i++) { 9095e26d47bSHong Zhang pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 9105e26d47bSHong Zhang pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 9115e26d47bSHong Zhang 9125e26d47bSHong Zhang for (j = ys; j < ys + ny; j++) { 9135e26d47bSHong Zhang slot = i - gxs + gnx * (j - gys); 9145e26d47bSHong Zhang 9155e26d47bSHong Zhang lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 9165e26d47bSHong Zhang lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 9175e26d47bSHong Zhang 9185e26d47bSHong Zhang cnt = 0; 9195e26d47bSHong Zhang for (k = 0; k < nc; k++) { 9205e26d47bSHong Zhang for (l = lstart; l < lend + 1; l++) { 9215e26d47bSHong Zhang for (p = pstart; p < pend + 1; p++) { 9225e26d47bSHong Zhang if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star have either l = 0 or p = 0 */ 9235e26d47bSHong Zhang cols[cnt++] = k + nc * (slot + gnx * l + p); 9245e26d47bSHong Zhang } 9255e26d47bSHong Zhang } 9265e26d47bSHong Zhang } 9275e26d47bSHong Zhang rows[k] = k + nc * (slot); 9285e26d47bSHong Zhang } 9299566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, values, INSERT_VALUES)); 9305e26d47bSHong Zhang } 9315e26d47bSHong Zhang } 9329566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 933e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 9349566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 9359566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 9369566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 9379566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 9389566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 9395e26d47bSHong Zhang } 9409566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 9413ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 9425e26d47bSHong Zhang } 9435e26d47bSHong Zhang 944d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPISELL(DM da, Mat J) 945d71ae5a4SJacob Faibussowitsch { 946711261dbSHong Zhang PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 947711261dbSHong Zhang PetscInt m, n, dim, s, *cols = NULL, k, nc, *rows = NULL, col, cnt, l, p, *dnz = NULL, *onz = NULL; 948711261dbSHong Zhang PetscInt istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk, M, N, P; 949711261dbSHong Zhang MPI_Comm comm; 950711261dbSHong Zhang PetscScalar *values; 951711261dbSHong Zhang DMBoundaryType bx, by, bz; 952711261dbSHong Zhang ISLocalToGlobalMapping ltog; 953711261dbSHong Zhang DMDAStencilType st; 954711261dbSHong Zhang 955711261dbSHong Zhang PetscFunctionBegin; 956711261dbSHong Zhang /* 957711261dbSHong Zhang nc - number of components per grid point 958711261dbSHong Zhang col - number of colors needed in one direction for single component problem 959711261dbSHong Zhang */ 9609566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &nc, &s, &bx, &by, &bz, &st)); 961711261dbSHong Zhang col = 2 * s + 1; 9629566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 9639566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 9649566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 965711261dbSHong Zhang 9669566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * col * col * nc * nc, &cols)); 9679566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 968711261dbSHong Zhang 9699566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 970711261dbSHong Zhang /* determine the matrix preallocation information */ 971d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny * nz, nc * nx * ny * nz, dnz, onz); 972711261dbSHong Zhang for (i = xs; i < xs + nx; i++) { 973711261dbSHong Zhang istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 974711261dbSHong Zhang iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 975711261dbSHong Zhang for (j = ys; j < ys + ny; j++) { 976711261dbSHong Zhang jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 977711261dbSHong Zhang jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 978711261dbSHong Zhang for (k = zs; k < zs + nz; k++) { 979711261dbSHong Zhang kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 980711261dbSHong Zhang kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 981711261dbSHong Zhang 982711261dbSHong Zhang slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 983711261dbSHong Zhang 984711261dbSHong Zhang cnt = 0; 985711261dbSHong Zhang for (l = 0; l < nc; l++) { 986711261dbSHong Zhang for (ii = istart; ii < iend + 1; ii++) { 987711261dbSHong Zhang for (jj = jstart; jj < jend + 1; jj++) { 988711261dbSHong Zhang for (kk = kstart; kk < kend + 1; kk++) { 989711261dbSHong Zhang if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 990711261dbSHong Zhang cols[cnt++] = l + nc * (slot + ii + gnx * jj + gnx * gny * kk); 991711261dbSHong Zhang } 992711261dbSHong Zhang } 993711261dbSHong Zhang } 994711261dbSHong Zhang } 995711261dbSHong Zhang rows[l] = l + nc * (slot); 996711261dbSHong Zhang } 9979566063dSJacob Faibussowitsch PetscCall(MatPreallocateSetLocal(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 998711261dbSHong Zhang } 999711261dbSHong Zhang } 1000711261dbSHong Zhang } 10019566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 10029566063dSJacob Faibussowitsch PetscCall(MatSeqSELLSetPreallocation(J, 0, dnz)); 10039566063dSJacob Faibussowitsch PetscCall(MatMPISELLSetPreallocation(J, 0, dnz, 0, onz)); 1004d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 10059566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 1006711261dbSHong Zhang 1007711261dbSHong Zhang /* 1008711261dbSHong Zhang For each node in the grid: we get the neighbors in the local (on processor ordering 1009711261dbSHong Zhang that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 1010711261dbSHong Zhang PETSc ordering. 1011711261dbSHong Zhang */ 1012711261dbSHong Zhang if (!da->prealloc_only) { 10139566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * col * nc * nc * nc, &values)); 1014711261dbSHong Zhang for (i = xs; i < xs + nx; i++) { 1015711261dbSHong Zhang istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1016711261dbSHong Zhang iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 1017711261dbSHong Zhang for (j = ys; j < ys + ny; j++) { 1018711261dbSHong Zhang jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1019711261dbSHong Zhang jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 1020711261dbSHong Zhang for (k = zs; k < zs + nz; k++) { 1021711261dbSHong Zhang kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 1022711261dbSHong Zhang kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 1023711261dbSHong Zhang 1024711261dbSHong Zhang slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 1025711261dbSHong Zhang 1026711261dbSHong Zhang cnt = 0; 1027711261dbSHong Zhang for (l = 0; l < nc; l++) { 1028711261dbSHong Zhang for (ii = istart; ii < iend + 1; ii++) { 1029711261dbSHong Zhang for (jj = jstart; jj < jend + 1; jj++) { 1030711261dbSHong Zhang for (kk = kstart; kk < kend + 1; kk++) { 1031711261dbSHong Zhang if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 1032711261dbSHong Zhang cols[cnt++] = l + nc * (slot + ii + gnx * jj + gnx * gny * kk); 1033711261dbSHong Zhang } 1034711261dbSHong Zhang } 1035711261dbSHong Zhang } 1036711261dbSHong Zhang } 1037711261dbSHong Zhang rows[l] = l + nc * (slot); 1038711261dbSHong Zhang } 10399566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, values, INSERT_VALUES)); 1040711261dbSHong Zhang } 1041711261dbSHong Zhang } 1042711261dbSHong Zhang } 10439566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 1044e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 10459566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 10469566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 10479566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 10489566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 10499566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 1050711261dbSHong Zhang } 10519566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 10523ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1053711261dbSHong Zhang } 1054711261dbSHong Zhang 1055d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPIAIJ(DM da, Mat J) 1056d71ae5a4SJacob Faibussowitsch { 1057c1154cd5SBarry 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; 105847c6ae99SBarry Smith PetscInt lstart, lend, pstart, pend, *dnz, *onz; 105947c6ae99SBarry Smith MPI_Comm comm; 1060bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 1061844bd0d7SStefano Zampini ISLocalToGlobalMapping ltog, mltog; 1062aa219208SBarry Smith DMDAStencilType st; 1063b294de21SRichard Tran Mills PetscBool removedups = PETSC_FALSE, alreadyboundtocpu = PETSC_TRUE; 106447c6ae99SBarry Smith 106547c6ae99SBarry Smith PetscFunctionBegin; 106647c6ae99SBarry Smith /* 106747c6ae99SBarry Smith nc - number of components per grid point 106847c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 106947c6ae99SBarry Smith */ 1070924e958dSJunchao Zhang PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, &M, &N, NULL, &nc, &s, &bx, &by, NULL, &st)); 10711baa6e33SBarry Smith if (bx == DM_BOUNDARY_NONE && by == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_TRUE)); 107247c6ae99SBarry Smith col = 2 * s + 1; 1073c1154cd5SBarry Smith /* 1074c1154cd5SBarry Smith With one processor in periodic domains in a skinny dimension the code will label nonzero columns multiple times 1075c1154cd5SBarry Smith because of "wrapping" around the end of the domain hitting an entry already counted in the other direction. 1076c1154cd5SBarry Smith */ 1077c1154cd5SBarry Smith if (M == 1 && 2 * s >= m) removedups = PETSC_TRUE; 1078c1154cd5SBarry Smith if (N == 1 && 2 * s >= n) removedups = PETSC_TRUE; 10799566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 10809566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 10819566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 108247c6ae99SBarry Smith 10839566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * col * nc * nc, &cols)); 10849566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 108547c6ae99SBarry Smith 10869566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 108747c6ae99SBarry Smith /* determine the matrix preallocation information */ 1088d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny, nc * nx * ny, dnz, onz); 108947c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1090bff4a2f0SMatthew G. Knepley pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1091bff4a2f0SMatthew G. Knepley pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 109247c6ae99SBarry Smith 109347c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 109447c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 109547c6ae99SBarry Smith 1096bff4a2f0SMatthew G. Knepley lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1097bff4a2f0SMatthew G. Knepley lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 109847c6ae99SBarry Smith 109947c6ae99SBarry Smith cnt = 0; 110047c6ae99SBarry Smith for (k = 0; k < nc; k++) { 110147c6ae99SBarry Smith for (l = lstart; l < lend + 1; l++) { 110247c6ae99SBarry Smith for (p = pstart; p < pend + 1; p++) { 1103aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star have either l = 0 or p = 0 */ 110447c6ae99SBarry Smith cols[cnt++] = k + nc * (slot + gnx * l + p); 110547c6ae99SBarry Smith } 110647c6ae99SBarry Smith } 110747c6ae99SBarry Smith } 110847c6ae99SBarry Smith rows[k] = k + nc * (slot); 110947c6ae99SBarry Smith } 11101baa6e33SBarry Smith if (removedups) PetscCall(MatPreallocateSetLocalRemoveDups(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 11111baa6e33SBarry Smith else PetscCall(MatPreallocateSetLocal(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 111247c6ae99SBarry Smith } 1113c1154cd5SBarry Smith } 11149566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 11159566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz)); 11169566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz)); 1117d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 11189566063dSJacob Faibussowitsch PetscCall(MatGetLocalToGlobalMapping(J, &mltog, NULL)); 11191baa6e33SBarry Smith if (!mltog) PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 112047c6ae99SBarry Smith 112147c6ae99SBarry Smith /* 112247c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 112347c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 112447c6ae99SBarry Smith PETSc ordering. 112547c6ae99SBarry Smith */ 1126fcfd50ebSBarry Smith if (!da->prealloc_only) { 112747c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1128bff4a2f0SMatthew G. Knepley pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1129bff4a2f0SMatthew G. Knepley pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 113047c6ae99SBarry Smith 113147c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 113247c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 113347c6ae99SBarry Smith 1134bff4a2f0SMatthew G. Knepley lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1135bff4a2f0SMatthew G. Knepley lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 113647c6ae99SBarry Smith 113747c6ae99SBarry Smith cnt = 0; 113847c6ae99SBarry Smith for (l = lstart; l < lend + 1; l++) { 113947c6ae99SBarry Smith for (p = pstart; p < pend + 1; p++) { 1140aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star have either l = 0 or p = 0 */ 1141071fcb05SBarry Smith cols[cnt++] = nc * (slot + gnx * l + p); 1142071fcb05SBarry Smith for (k = 1; k < nc; k++) { 11439371c9d4SSatish Balay cols[cnt] = 1 + cols[cnt - 1]; 11449371c9d4SSatish Balay cnt++; 114547c6ae99SBarry Smith } 114647c6ae99SBarry Smith } 114747c6ae99SBarry Smith } 114847c6ae99SBarry Smith } 1149071fcb05SBarry Smith for (k = 0; k < nc; k++) rows[k] = k + nc * (slot); 11509566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, NULL, INSERT_VALUES)); 115147c6ae99SBarry Smith } 115247c6ae99SBarry Smith } 1153e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 11549566063dSJacob Faibussowitsch PetscCall(MatBoundToCPU(J, &alreadyboundtocpu)); 11559566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 11569566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 11579566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 11589566063dSJacob Faibussowitsch if (!alreadyboundtocpu) PetscCall(MatBindToCPU(J, PETSC_FALSE)); 11599566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 11601baa6e33SBarry Smith if (bx == DM_BOUNDARY_NONE && by == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE)); 116147c6ae99SBarry Smith } 11629566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 11633ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 116447c6ae99SBarry Smith } 116547c6ae99SBarry Smith 1166d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPIAIJ_Fill(DM da, Mat J) 1167d71ae5a4SJacob Faibussowitsch { 116847c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 1169c1154cd5SBarry Smith PetscInt m, n, dim, s, *cols, k, nc, row, col, cnt, maxcnt = 0, l, p, M, N; 117047c6ae99SBarry Smith PetscInt lstart, lend, pstart, pend, *dnz, *onz; 117147c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 117247c6ae99SBarry Smith PetscInt ifill_col, *ofill = dd->ofill, *dfill = dd->dfill; 117347c6ae99SBarry Smith MPI_Comm comm; 1174bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 117545b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 1176aa219208SBarry Smith DMDAStencilType st; 1177c1154cd5SBarry Smith PetscBool removedups = PETSC_FALSE; 117847c6ae99SBarry Smith 117947c6ae99SBarry Smith PetscFunctionBegin; 118047c6ae99SBarry Smith /* 118147c6ae99SBarry Smith nc - number of components per grid point 118247c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 118347c6ae99SBarry Smith */ 1184924e958dSJunchao Zhang PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, &M, &N, NULL, &nc, &s, &bx, &by, NULL, &st)); 118547c6ae99SBarry Smith col = 2 * s + 1; 1186c1154cd5SBarry Smith /* 1187c1154cd5SBarry Smith With one processor in periodic domains in a skinny dimension the code will label nonzero columns multiple times 1188c1154cd5SBarry Smith because of "wrapping" around the end of the domain hitting an entry already counted in the other direction. 1189c1154cd5SBarry Smith */ 1190c1154cd5SBarry Smith if (M == 1 && 2 * s >= m) removedups = PETSC_TRUE; 1191c1154cd5SBarry Smith if (N == 1 && 2 * s >= n) removedups = PETSC_TRUE; 11929566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 11939566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 11949566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 119547c6ae99SBarry Smith 11969566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * nc, &cols)); 11979566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 119847c6ae99SBarry Smith 11999566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 120047c6ae99SBarry Smith /* determine the matrix preallocation information */ 1201d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny, nc * nx * ny, dnz, onz); 120247c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1203bff4a2f0SMatthew G. Knepley pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1204bff4a2f0SMatthew G. Knepley pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 120547c6ae99SBarry Smith 120647c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 120747c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 120847c6ae99SBarry Smith 1209bff4a2f0SMatthew G. Knepley lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1210bff4a2f0SMatthew G. Knepley lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 121147c6ae99SBarry Smith 121247c6ae99SBarry Smith for (k = 0; k < nc; k++) { 121347c6ae99SBarry Smith cnt = 0; 121447c6ae99SBarry Smith for (l = lstart; l < lend + 1; l++) { 121547c6ae99SBarry Smith for (p = pstart; p < pend + 1; p++) { 121647c6ae99SBarry Smith if (l || p) { 1217aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star */ 12188865f1eaSKarl Rupp for (ifill_col = ofill[k]; ifill_col < ofill[k + 1]; ifill_col++) cols[cnt++] = ofill[ifill_col] + nc * (slot + gnx * l + p); 121947c6ae99SBarry Smith } 122047c6ae99SBarry Smith } else { 122147c6ae99SBarry Smith if (dfill) { 12228865f1eaSKarl Rupp for (ifill_col = dfill[k]; ifill_col < dfill[k + 1]; ifill_col++) cols[cnt++] = dfill[ifill_col] + nc * (slot + gnx * l + p); 122347c6ae99SBarry Smith } else { 12248865f1eaSKarl Rupp for (ifill_col = 0; ifill_col < nc; ifill_col++) cols[cnt++] = ifill_col + nc * (slot + gnx * l + p); 122547c6ae99SBarry Smith } 122647c6ae99SBarry Smith } 122747c6ae99SBarry Smith } 122847c6ae99SBarry Smith } 122947c6ae99SBarry Smith row = k + nc * (slot); 1230c0ab637bSBarry Smith maxcnt = PetscMax(maxcnt, cnt); 12311baa6e33SBarry Smith if (removedups) PetscCall(MatPreallocateSetLocalRemoveDups(ltog, 1, &row, ltog, cnt, cols, dnz, onz)); 12321baa6e33SBarry Smith else PetscCall(MatPreallocateSetLocal(ltog, 1, &row, ltog, cnt, cols, dnz, onz)); 123347c6ae99SBarry Smith } 123447c6ae99SBarry Smith } 1235c1154cd5SBarry Smith } 12369566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz)); 12379566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz)); 1238d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 12399566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 124047c6ae99SBarry Smith 124147c6ae99SBarry Smith /* 124247c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 124347c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 124447c6ae99SBarry Smith PETSc ordering. 124547c6ae99SBarry Smith */ 1246fcfd50ebSBarry Smith if (!da->prealloc_only) { 124747c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1248bff4a2f0SMatthew G. Knepley pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1249bff4a2f0SMatthew G. Knepley pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 125047c6ae99SBarry Smith 125147c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 125247c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 125347c6ae99SBarry Smith 1254bff4a2f0SMatthew G. Knepley lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1255bff4a2f0SMatthew G. Knepley lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 125647c6ae99SBarry Smith 125747c6ae99SBarry Smith for (k = 0; k < nc; k++) { 125847c6ae99SBarry Smith cnt = 0; 125947c6ae99SBarry Smith for (l = lstart; l < lend + 1; l++) { 126047c6ae99SBarry Smith for (p = pstart; p < pend + 1; p++) { 126147c6ae99SBarry Smith if (l || p) { 1262aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star */ 12638865f1eaSKarl Rupp for (ifill_col = ofill[k]; ifill_col < ofill[k + 1]; ifill_col++) cols[cnt++] = ofill[ifill_col] + nc * (slot + gnx * l + p); 126447c6ae99SBarry Smith } 126547c6ae99SBarry Smith } else { 126647c6ae99SBarry Smith if (dfill) { 12678865f1eaSKarl Rupp for (ifill_col = dfill[k]; ifill_col < dfill[k + 1]; ifill_col++) cols[cnt++] = dfill[ifill_col] + nc * (slot + gnx * l + p); 126847c6ae99SBarry Smith } else { 12698865f1eaSKarl Rupp for (ifill_col = 0; ifill_col < nc; ifill_col++) cols[cnt++] = ifill_col + nc * (slot + gnx * l + p); 127047c6ae99SBarry Smith } 127147c6ae99SBarry Smith } 127247c6ae99SBarry Smith } 127347c6ae99SBarry Smith } 127447c6ae99SBarry Smith row = k + nc * (slot); 12759566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, 1, &row, cnt, cols, NULL, INSERT_VALUES)); 127647c6ae99SBarry Smith } 127747c6ae99SBarry Smith } 127847c6ae99SBarry Smith } 1279e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 12809566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 12819566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 12829566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 12839566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 12849566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 128547c6ae99SBarry Smith } 12869566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 12873ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 128847c6ae99SBarry Smith } 128947c6ae99SBarry Smith 129047c6ae99SBarry Smith /* ---------------------------------------------------------------------------------*/ 129147c6ae99SBarry Smith 1292d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPIAIJ(DM da, Mat J) 1293d71ae5a4SJacob Faibussowitsch { 129447c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 12950298fd71SBarry Smith PetscInt m, n, dim, s, *cols = NULL, k, nc, *rows = NULL, col, cnt, l, p, *dnz = NULL, *onz = NULL; 1296c1154cd5SBarry Smith PetscInt istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk, M, N, P; 129747c6ae99SBarry Smith MPI_Comm comm; 1298bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by, bz; 1299844bd0d7SStefano Zampini ISLocalToGlobalMapping ltog, mltog; 1300aa219208SBarry Smith DMDAStencilType st; 1301c1154cd5SBarry Smith PetscBool removedups = PETSC_FALSE; 130247c6ae99SBarry Smith 130347c6ae99SBarry Smith PetscFunctionBegin; 130447c6ae99SBarry Smith /* 130547c6ae99SBarry Smith nc - number of components per grid point 130647c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 130747c6ae99SBarry Smith */ 13089566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &nc, &s, &bx, &by, &bz, &st)); 130948a46eb9SPierre Jolivet if (bx == DM_BOUNDARY_NONE && by == DM_BOUNDARY_NONE && bz == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_TRUE)); 131047c6ae99SBarry Smith col = 2 * s + 1; 131147c6ae99SBarry Smith 1312c1154cd5SBarry Smith /* 1313c1154cd5SBarry Smith With one processor in periodic domains in a skinny dimension the code will label nonzero columns multiple times 1314c1154cd5SBarry Smith because of "wrapping" around the end of the domain hitting an entry already counted in the other direction. 1315c1154cd5SBarry Smith */ 1316c1154cd5SBarry Smith if (M == 1 && 2 * s >= m) removedups = PETSC_TRUE; 1317c1154cd5SBarry Smith if (N == 1 && 2 * s >= n) removedups = PETSC_TRUE; 1318c1154cd5SBarry Smith if (P == 1 && 2 * s >= p) removedups = PETSC_TRUE; 1319c1154cd5SBarry Smith 13209566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 13219566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 13229566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 132347c6ae99SBarry Smith 13249566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * col * col * nc * nc, &cols)); 13259566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 132647c6ae99SBarry Smith 13279566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 132847c6ae99SBarry Smith /* determine the matrix preallocation information */ 1329d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny * nz, nc * nx * ny * nz, dnz, onz); 133047c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1331bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1332bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 133347c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1334bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1335bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 133647c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 1337bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 1338bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 133947c6ae99SBarry Smith 134047c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 134147c6ae99SBarry Smith 134247c6ae99SBarry Smith cnt = 0; 134347c6ae99SBarry Smith for (l = 0; l < nc; l++) { 134447c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 134547c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 134647c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 1347aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 134847c6ae99SBarry Smith cols[cnt++] = l + nc * (slot + ii + gnx * jj + gnx * gny * kk); 134947c6ae99SBarry Smith } 135047c6ae99SBarry Smith } 135147c6ae99SBarry Smith } 135247c6ae99SBarry Smith } 135347c6ae99SBarry Smith rows[l] = l + nc * (slot); 135447c6ae99SBarry Smith } 13551baa6e33SBarry Smith if (removedups) PetscCall(MatPreallocateSetLocalRemoveDups(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 13561baa6e33SBarry Smith else PetscCall(MatPreallocateSetLocal(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 135747c6ae99SBarry Smith } 135847c6ae99SBarry Smith } 1359c1154cd5SBarry Smith } 13609566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 13619566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz)); 13629566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz)); 1363d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 13649566063dSJacob Faibussowitsch PetscCall(MatGetLocalToGlobalMapping(J, &mltog, NULL)); 136548a46eb9SPierre Jolivet if (!mltog) PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 136647c6ae99SBarry Smith 136747c6ae99SBarry Smith /* 136847c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 136947c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 137047c6ae99SBarry Smith PETSc ordering. 137147c6ae99SBarry Smith */ 1372fcfd50ebSBarry Smith if (!da->prealloc_only) { 137347c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1374bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1375bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 137647c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1377bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1378bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 137947c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 1380bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 1381bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 138247c6ae99SBarry Smith 138347c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 138447c6ae99SBarry Smith 138547c6ae99SBarry Smith cnt = 0; 138647c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 1387071fcb05SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 1388071fcb05SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 1389aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 1390071fcb05SBarry Smith cols[cnt++] = nc * (slot + ii + gnx * jj + gnx * gny * kk); 1391071fcb05SBarry Smith for (l = 1; l < nc; l++) { 13929371c9d4SSatish Balay cols[cnt] = 1 + cols[cnt - 1]; 13939371c9d4SSatish Balay cnt++; 139447c6ae99SBarry Smith } 139547c6ae99SBarry Smith } 139647c6ae99SBarry Smith } 139747c6ae99SBarry Smith } 139847c6ae99SBarry Smith } 13999371c9d4SSatish Balay rows[0] = nc * (slot); 14009371c9d4SSatish Balay for (l = 1; l < nc; l++) rows[l] = 1 + rows[l - 1]; 14019566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, NULL, INSERT_VALUES)); 140247c6ae99SBarry Smith } 140347c6ae99SBarry Smith } 140447c6ae99SBarry Smith } 1405e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 14069566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 14079566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 14089566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 140948a46eb9SPierre Jolivet if (bx == DM_BOUNDARY_NONE && by == DM_BOUNDARY_NONE && bz == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE)); 14109566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 14119566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 141247c6ae99SBarry Smith } 14139566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 14143ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 141547c6ae99SBarry Smith } 141647c6ae99SBarry Smith 141747c6ae99SBarry Smith /* ---------------------------------------------------------------------------------*/ 141847c6ae99SBarry Smith 1419d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_1d_MPIAIJ_Fill(DM da, Mat J) 1420d71ae5a4SJacob Faibussowitsch { 1421ce308e1dSBarry Smith DM_DA *dd = (DM_DA *)da->data; 1422ce308e1dSBarry Smith PetscInt xs, nx, i, j, gxs, gnx, row, k, l; 14238d4c968fSBarry Smith PetscInt m, dim, s, *cols = NULL, nc, cnt, maxcnt = 0, *ocols; 14240acb5bebSBarry Smith PetscInt *ofill = dd->ofill, *dfill = dd->dfill; 1425bff4a2f0SMatthew G. Knepley DMBoundaryType bx; 142645b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 1427ce308e1dSBarry Smith PetscMPIInt rank, size; 1428ce308e1dSBarry Smith 1429ce308e1dSBarry Smith PetscFunctionBegin; 14309566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)da), &rank)); 14319566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)da), &size)); 1432ce308e1dSBarry Smith 1433ce308e1dSBarry Smith /* 1434ce308e1dSBarry Smith nc - number of components per grid point 1435ce308e1dSBarry Smith */ 14369566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, NULL, NULL, NULL, NULL, NULL, &nc, &s, &bx, NULL, NULL, NULL)); 143708401ef6SPierre Jolivet PetscCheck(s <= 1, PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "Matrix creation for 1d not implemented correctly for stencil width larger than 1"); 14389566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &nx, NULL, NULL)); 14399566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, NULL, NULL, &gnx, NULL, NULL)); 1440ce308e1dSBarry Smith 14419566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 14429566063dSJacob Faibussowitsch PetscCall(PetscCalloc2(nx * nc, &cols, nx * nc, &ocols)); 1443ce308e1dSBarry Smith 1444ce308e1dSBarry Smith /* 1445ce308e1dSBarry Smith note should be smaller for first and last process with no periodic 1446ce308e1dSBarry Smith does not handle dfill 1447ce308e1dSBarry Smith */ 1448ce308e1dSBarry Smith cnt = 0; 1449ce308e1dSBarry Smith /* coupling with process to the left */ 1450ce308e1dSBarry Smith for (i = 0; i < s; i++) { 1451ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 1452dd400576SPatrick Sanan ocols[cnt] = ((rank == 0) ? 0 : (s - i) * (ofill[j + 1] - ofill[j])); 14530acb5bebSBarry Smith cols[cnt] = dfill[j + 1] - dfill[j] + (s + i) * (ofill[j + 1] - ofill[j]); 1454dd400576SPatrick Sanan if (rank == 0 && (dd->bx == DM_BOUNDARY_PERIODIC)) { 1455831644c1SBarry Smith if (size > 1) ocols[cnt] += (s - i) * (ofill[j + 1] - ofill[j]); 1456831644c1SBarry Smith else cols[cnt] += (s - i) * (ofill[j + 1] - ofill[j]); 1457831644c1SBarry Smith } 1458c0ab637bSBarry Smith maxcnt = PetscMax(maxcnt, ocols[cnt] + cols[cnt]); 1459ce308e1dSBarry Smith cnt++; 1460ce308e1dSBarry Smith } 1461ce308e1dSBarry Smith } 1462ce308e1dSBarry Smith for (i = s; i < nx - s; i++) { 1463ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 14640acb5bebSBarry Smith cols[cnt] = dfill[j + 1] - dfill[j] + 2 * s * (ofill[j + 1] - ofill[j]); 1465c0ab637bSBarry Smith maxcnt = PetscMax(maxcnt, ocols[cnt] + cols[cnt]); 1466ce308e1dSBarry Smith cnt++; 1467ce308e1dSBarry Smith } 1468ce308e1dSBarry Smith } 1469ce308e1dSBarry Smith /* coupling with process to the right */ 1470ce308e1dSBarry Smith for (i = nx - s; i < nx; i++) { 1471ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 1472ce308e1dSBarry Smith ocols[cnt] = ((rank == (size - 1)) ? 0 : (i - nx + s + 1) * (ofill[j + 1] - ofill[j])); 14730acb5bebSBarry Smith cols[cnt] = dfill[j + 1] - dfill[j] + (s + nx - i - 1) * (ofill[j + 1] - ofill[j]); 1474831644c1SBarry Smith if ((rank == size - 1) && (dd->bx == DM_BOUNDARY_PERIODIC)) { 1475831644c1SBarry Smith if (size > 1) ocols[cnt] += (i - nx + s + 1) * (ofill[j + 1] - ofill[j]); 1476831644c1SBarry Smith else cols[cnt] += (i - nx + s + 1) * (ofill[j + 1] - ofill[j]); 1477831644c1SBarry Smith } 1478c0ab637bSBarry Smith maxcnt = PetscMax(maxcnt, ocols[cnt] + cols[cnt]); 1479ce308e1dSBarry Smith cnt++; 1480ce308e1dSBarry Smith } 1481ce308e1dSBarry Smith } 1482ce308e1dSBarry Smith 14839566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, 0, cols)); 14849566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, 0, cols, 0, ocols)); 14859566063dSJacob Faibussowitsch PetscCall(PetscFree2(cols, ocols)); 1486ce308e1dSBarry Smith 14879566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 14889566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 1489ce308e1dSBarry Smith 1490ce308e1dSBarry Smith /* 1491ce308e1dSBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 1492ce308e1dSBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 1493ce308e1dSBarry Smith PETSc ordering. 1494ce308e1dSBarry Smith */ 1495ce308e1dSBarry Smith if (!da->prealloc_only) { 14969566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(maxcnt, &cols)); 1497ce308e1dSBarry Smith row = xs * nc; 1498ce308e1dSBarry Smith /* coupling with process to the left */ 1499ce308e1dSBarry Smith for (i = xs; i < xs + s; i++) { 1500ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 1501ce308e1dSBarry Smith cnt = 0; 1502ce308e1dSBarry Smith if (rank) { 1503ce308e1dSBarry Smith for (l = 0; l < s; l++) { 1504ce308e1dSBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i - s + l) * nc + ofill[k]; 1505ce308e1dSBarry Smith } 1506ce308e1dSBarry Smith } 1507dd400576SPatrick Sanan if (rank == 0 && (dd->bx == DM_BOUNDARY_PERIODIC)) { 1508831644c1SBarry Smith for (l = 0; l < s; l++) { 1509831644c1SBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (m + i - s - l) * nc + ofill[k]; 1510831644c1SBarry Smith } 1511831644c1SBarry Smith } 15120acb5bebSBarry Smith if (dfill) { 1513ad540459SPierre Jolivet for (k = dfill[j]; k < dfill[j + 1]; k++) cols[cnt++] = i * nc + dfill[k]; 15140acb5bebSBarry Smith } else { 1515ad540459SPierre Jolivet for (k = 0; k < nc; k++) cols[cnt++] = i * nc + k; 15160acb5bebSBarry Smith } 1517ce308e1dSBarry Smith for (l = 0; l < s; l++) { 1518ce308e1dSBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i + s - l) * nc + ofill[k]; 1519ce308e1dSBarry Smith } 15209566063dSJacob Faibussowitsch PetscCall(MatSetValues(J, 1, &row, cnt, cols, NULL, INSERT_VALUES)); 1521ce308e1dSBarry Smith row++; 1522ce308e1dSBarry Smith } 1523ce308e1dSBarry Smith } 1524ce308e1dSBarry Smith for (i = xs + s; i < xs + nx - s; i++) { 1525ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 1526ce308e1dSBarry Smith cnt = 0; 1527ce308e1dSBarry Smith for (l = 0; l < s; l++) { 1528ce308e1dSBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i - s + l) * nc + ofill[k]; 1529ce308e1dSBarry Smith } 15300acb5bebSBarry Smith if (dfill) { 1531ad540459SPierre Jolivet for (k = dfill[j]; k < dfill[j + 1]; k++) cols[cnt++] = i * nc + dfill[k]; 15320acb5bebSBarry Smith } else { 1533ad540459SPierre Jolivet for (k = 0; k < nc; k++) cols[cnt++] = i * nc + k; 15340acb5bebSBarry Smith } 1535ce308e1dSBarry Smith for (l = 0; l < s; l++) { 1536ce308e1dSBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i + s - l) * nc + ofill[k]; 1537ce308e1dSBarry Smith } 15389566063dSJacob Faibussowitsch PetscCall(MatSetValues(J, 1, &row, cnt, cols, NULL, INSERT_VALUES)); 1539ce308e1dSBarry Smith row++; 1540ce308e1dSBarry Smith } 1541ce308e1dSBarry Smith } 1542ce308e1dSBarry Smith /* coupling with process to the right */ 1543ce308e1dSBarry Smith for (i = xs + nx - s; i < xs + nx; i++) { 1544ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 1545ce308e1dSBarry Smith cnt = 0; 1546ce308e1dSBarry Smith for (l = 0; l < s; l++) { 1547ce308e1dSBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i - s + l) * nc + ofill[k]; 1548ce308e1dSBarry Smith } 15490acb5bebSBarry Smith if (dfill) { 1550ad540459SPierre Jolivet for (k = dfill[j]; k < dfill[j + 1]; k++) cols[cnt++] = i * nc + dfill[k]; 15510acb5bebSBarry Smith } else { 1552ad540459SPierre Jolivet for (k = 0; k < nc; k++) cols[cnt++] = i * nc + k; 15530acb5bebSBarry Smith } 1554ce308e1dSBarry Smith if (rank < size - 1) { 1555ce308e1dSBarry Smith for (l = 0; l < s; l++) { 1556ce308e1dSBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i + s - l) * nc + ofill[k]; 1557ce308e1dSBarry Smith } 1558ce308e1dSBarry Smith } 1559831644c1SBarry Smith if ((rank == size - 1) && (dd->bx == DM_BOUNDARY_PERIODIC)) { 1560831644c1SBarry Smith for (l = 0; l < s; l++) { 1561831644c1SBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i - s - l - m + 2) * nc + ofill[k]; 1562831644c1SBarry Smith } 1563831644c1SBarry Smith } 15649566063dSJacob Faibussowitsch PetscCall(MatSetValues(J, 1, &row, cnt, cols, NULL, INSERT_VALUES)); 1565ce308e1dSBarry Smith row++; 1566ce308e1dSBarry Smith } 1567ce308e1dSBarry Smith } 15689566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 1569e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 15709566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 15719566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 15729566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 15739566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 15749566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 1575ce308e1dSBarry Smith } 15763ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1577ce308e1dSBarry Smith } 1578ce308e1dSBarry Smith 1579ce308e1dSBarry Smith /* ---------------------------------------------------------------------------------*/ 1580ce308e1dSBarry Smith 1581d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_1d_MPIAIJ(DM da, Mat J) 1582d71ae5a4SJacob Faibussowitsch { 158347c6ae99SBarry Smith PetscInt xs, nx, i, i1, slot, gxs, gnx; 15840298fd71SBarry Smith PetscInt m, dim, s, *cols = NULL, nc, *rows = NULL, col, cnt, l; 158547c6ae99SBarry Smith PetscInt istart, iend; 1586bff4a2f0SMatthew G. Knepley DMBoundaryType bx; 1587844bd0d7SStefano Zampini ISLocalToGlobalMapping ltog, mltog; 158847c6ae99SBarry Smith 158947c6ae99SBarry Smith PetscFunctionBegin; 159047c6ae99SBarry Smith /* 159147c6ae99SBarry Smith nc - number of components per grid point 159247c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 159347c6ae99SBarry Smith */ 15949566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, NULL, NULL, NULL, NULL, NULL, &nc, &s, &bx, NULL, NULL, NULL)); 159548a46eb9SPierre Jolivet if (bx == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_TRUE)); 159647c6ae99SBarry Smith col = 2 * s + 1; 159747c6ae99SBarry Smith 15989566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &nx, NULL, NULL)); 15999566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, NULL, NULL, &gnx, NULL, NULL)); 160047c6ae99SBarry Smith 16019566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 16029566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, col * nc, NULL)); 16039566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, col * nc, NULL, col * nc, NULL)); 160447c6ae99SBarry Smith 16059566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 16069566063dSJacob Faibussowitsch PetscCall(MatGetLocalToGlobalMapping(J, &mltog, NULL)); 160748a46eb9SPierre Jolivet if (!mltog) PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 160847c6ae99SBarry Smith 160947c6ae99SBarry Smith /* 161047c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 161147c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 161247c6ae99SBarry Smith PETSc ordering. 161347c6ae99SBarry Smith */ 1614fcfd50ebSBarry Smith if (!da->prealloc_only) { 16159566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * nc * nc, &cols)); 161647c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 161747c6ae99SBarry Smith istart = PetscMax(-s, gxs - i); 161847c6ae99SBarry Smith iend = PetscMin(s, gxs + gnx - i - 1); 161947c6ae99SBarry Smith slot = i - gxs; 162047c6ae99SBarry Smith 162147c6ae99SBarry Smith cnt = 0; 162247c6ae99SBarry Smith for (i1 = istart; i1 < iend + 1; i1++) { 1623071fcb05SBarry Smith cols[cnt++] = nc * (slot + i1); 1624071fcb05SBarry Smith for (l = 1; l < nc; l++) { 16259371c9d4SSatish Balay cols[cnt] = 1 + cols[cnt - 1]; 16269371c9d4SSatish Balay cnt++; 162747c6ae99SBarry Smith } 162847c6ae99SBarry Smith } 16299371c9d4SSatish Balay rows[0] = nc * (slot); 16309371c9d4SSatish Balay for (l = 1; l < nc; l++) rows[l] = 1 + rows[l - 1]; 16319566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, NULL, INSERT_VALUES)); 163247c6ae99SBarry Smith } 1633e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 16349566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 16359566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 16369566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 163748a46eb9SPierre Jolivet if (bx == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE)); 16389566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 16399566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 16409566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 1641ce308e1dSBarry Smith } 16423ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 164347c6ae99SBarry Smith } 164447c6ae99SBarry Smith 164519b08ed1SBarry Smith /* ---------------------------------------------------------------------------------*/ 164619b08ed1SBarry Smith 1647d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_1d_SeqAIJ_NoPreallocation(DM da, Mat J) 1648d71ae5a4SJacob Faibussowitsch { 164919b08ed1SBarry Smith PetscInt xs, nx, i, i1, slot, gxs, gnx; 165019b08ed1SBarry Smith PetscInt m, dim, s, *cols = NULL, nc, *rows = NULL, col, cnt, l; 165119b08ed1SBarry Smith PetscInt istart, iend; 165219b08ed1SBarry Smith DMBoundaryType bx; 165319b08ed1SBarry Smith ISLocalToGlobalMapping ltog, mltog; 165419b08ed1SBarry Smith 165519b08ed1SBarry Smith PetscFunctionBegin; 165619b08ed1SBarry Smith /* 165719b08ed1SBarry Smith nc - number of components per grid point 165819b08ed1SBarry Smith col - number of colors needed in one direction for single component problem 165919b08ed1SBarry Smith */ 16609566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, NULL, NULL, NULL, NULL, NULL, &nc, &s, &bx, NULL, NULL, NULL)); 166119b08ed1SBarry Smith col = 2 * s + 1; 166219b08ed1SBarry Smith 16639566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &nx, NULL, NULL)); 16649566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, NULL, NULL, &gnx, NULL, NULL)); 166519b08ed1SBarry Smith 16669566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 16679566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetTotalPreallocation(J, nx * nc * col * nc)); 166819b08ed1SBarry Smith 16699566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 16709566063dSJacob Faibussowitsch PetscCall(MatGetLocalToGlobalMapping(J, &mltog, NULL)); 167148a46eb9SPierre Jolivet if (!mltog) PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 167219b08ed1SBarry Smith 167319b08ed1SBarry Smith /* 167419b08ed1SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 167519b08ed1SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 167619b08ed1SBarry Smith PETSc ordering. 167719b08ed1SBarry Smith */ 167819b08ed1SBarry Smith if (!da->prealloc_only) { 16799566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * nc * nc, &cols)); 168019b08ed1SBarry Smith for (i = xs; i < xs + nx; i++) { 168119b08ed1SBarry Smith istart = PetscMax(-s, gxs - i); 168219b08ed1SBarry Smith iend = PetscMin(s, gxs + gnx - i - 1); 168319b08ed1SBarry Smith slot = i - gxs; 168419b08ed1SBarry Smith 168519b08ed1SBarry Smith cnt = 0; 168619b08ed1SBarry Smith for (i1 = istart; i1 < iend + 1; i1++) { 168719b08ed1SBarry Smith cols[cnt++] = nc * (slot + i1); 168819b08ed1SBarry Smith for (l = 1; l < nc; l++) { 16899371c9d4SSatish Balay cols[cnt] = 1 + cols[cnt - 1]; 16909371c9d4SSatish Balay cnt++; 169119b08ed1SBarry Smith } 169219b08ed1SBarry Smith } 16939371c9d4SSatish Balay rows[0] = nc * (slot); 16949371c9d4SSatish Balay for (l = 1; l < nc; l++) rows[l] = 1 + rows[l - 1]; 16959566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, NULL, INSERT_VALUES)); 169619b08ed1SBarry Smith } 169719b08ed1SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 16989566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 16999566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 17009566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 170148a46eb9SPierre Jolivet if (bx == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE)); 17029566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 17039566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 17049566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 170519b08ed1SBarry Smith } 17069566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE)); 17073ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 170819b08ed1SBarry Smith } 170919b08ed1SBarry Smith 1710d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPIBAIJ(DM da, Mat J) 1711d71ae5a4SJacob Faibussowitsch { 171247c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 171347c6ae99SBarry Smith PetscInt m, n, dim, s, *cols, nc, col, cnt, *dnz, *onz; 171447c6ae99SBarry Smith PetscInt istart, iend, jstart, jend, ii, jj; 171547c6ae99SBarry Smith MPI_Comm comm; 171647c6ae99SBarry Smith PetscScalar *values; 1717bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 1718aa219208SBarry Smith DMDAStencilType st; 171945b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 172047c6ae99SBarry Smith 172147c6ae99SBarry Smith PetscFunctionBegin; 172247c6ae99SBarry Smith /* 172347c6ae99SBarry Smith nc - number of components per grid point 172447c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 172547c6ae99SBarry Smith */ 17269566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, NULL, NULL, NULL, &nc, &s, &bx, &by, NULL, &st)); 172747c6ae99SBarry Smith col = 2 * s + 1; 172847c6ae99SBarry Smith 17299566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 17309566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 17319566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 173247c6ae99SBarry Smith 17339566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * nc * nc, &cols)); 173447c6ae99SBarry Smith 17359566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 173647c6ae99SBarry Smith 173747c6ae99SBarry Smith /* determine the matrix preallocation information */ 1738d0609cedSBarry Smith MatPreallocateBegin(comm, nx * ny, nx * ny, dnz, onz); 173947c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1740bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1741bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 174247c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1743bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1744bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 174547c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 174647c6ae99SBarry Smith 174747c6ae99SBarry Smith /* Find block columns in block row */ 174847c6ae99SBarry Smith cnt = 0; 174947c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 175047c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 1751aa219208SBarry Smith if (st == DMDA_STENCIL_BOX || !ii || !jj) { /* BOX or on the STAR */ 175247c6ae99SBarry Smith cols[cnt++] = slot + ii + gnx * jj; 175347c6ae99SBarry Smith } 175447c6ae99SBarry Smith } 175547c6ae99SBarry Smith } 17569566063dSJacob Faibussowitsch PetscCall(MatPreallocateSetLocalBlock(ltog, 1, &slot, ltog, cnt, cols, dnz, onz)); 175747c6ae99SBarry Smith } 175847c6ae99SBarry Smith } 17599566063dSJacob Faibussowitsch PetscCall(MatSeqBAIJSetPreallocation(J, nc, 0, dnz)); 17609566063dSJacob Faibussowitsch PetscCall(MatMPIBAIJSetPreallocation(J, nc, 0, dnz, 0, onz)); 1761d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 176247c6ae99SBarry Smith 17639566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 176447c6ae99SBarry Smith 176547c6ae99SBarry Smith /* 176647c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 176747c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 176847c6ae99SBarry Smith PETSc ordering. 176947c6ae99SBarry Smith */ 1770fcfd50ebSBarry Smith if (!da->prealloc_only) { 17719566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * nc * nc, &values)); 177247c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1773bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1774bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 177547c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1776bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1777bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 177847c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 177947c6ae99SBarry Smith cnt = 0; 178047c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 178147c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 1782aa219208SBarry Smith if (st == DMDA_STENCIL_BOX || !ii || !jj) { /* BOX or on the STAR */ 178347c6ae99SBarry Smith cols[cnt++] = slot + ii + gnx * jj; 178447c6ae99SBarry Smith } 178547c6ae99SBarry Smith } 178647c6ae99SBarry Smith } 17879566063dSJacob Faibussowitsch PetscCall(MatSetValuesBlockedLocal(J, 1, &slot, cnt, cols, values, INSERT_VALUES)); 178847c6ae99SBarry Smith } 178947c6ae99SBarry Smith } 17909566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 1791e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 17929566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 17939566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 17949566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 17959566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 17969566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 179747c6ae99SBarry Smith } 17989566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 17993ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 180047c6ae99SBarry Smith } 180147c6ae99SBarry Smith 1802d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPIBAIJ(DM da, Mat J) 1803d71ae5a4SJacob Faibussowitsch { 180447c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 180547c6ae99SBarry Smith PetscInt m, n, dim, s, *cols, k, nc, col, cnt, p, *dnz, *onz; 180647c6ae99SBarry Smith PetscInt istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk; 180747c6ae99SBarry Smith MPI_Comm comm; 180847c6ae99SBarry Smith PetscScalar *values; 1809bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by, bz; 1810aa219208SBarry Smith DMDAStencilType st; 181145b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 181247c6ae99SBarry Smith 181347c6ae99SBarry Smith PetscFunctionBegin; 181447c6ae99SBarry Smith /* 181547c6ae99SBarry Smith nc - number of components per grid point 181647c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 181747c6ae99SBarry Smith */ 18189566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, NULL, NULL, NULL, &nc, &s, &bx, &by, &bz, &st)); 181947c6ae99SBarry Smith col = 2 * s + 1; 182047c6ae99SBarry Smith 18219566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 18229566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 18239566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 182447c6ae99SBarry Smith 18259566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * col, &cols)); 182647c6ae99SBarry Smith 18279566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 182847c6ae99SBarry Smith 182947c6ae99SBarry Smith /* determine the matrix preallocation information */ 1830d0609cedSBarry Smith MatPreallocateBegin(comm, nx * ny * nz, nx * ny * nz, dnz, onz); 183147c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1832bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1833bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 183447c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1835bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1836bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 183747c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 1838bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 1839bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 184047c6ae99SBarry Smith 184147c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 184247c6ae99SBarry Smith 184347c6ae99SBarry Smith /* Find block columns in block row */ 184447c6ae99SBarry Smith cnt = 0; 184547c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 184647c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 184747c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 1848aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 184947c6ae99SBarry Smith cols[cnt++] = slot + ii + gnx * jj + gnx * gny * kk; 185047c6ae99SBarry Smith } 185147c6ae99SBarry Smith } 185247c6ae99SBarry Smith } 185347c6ae99SBarry Smith } 18549566063dSJacob Faibussowitsch PetscCall(MatPreallocateSetLocalBlock(ltog, 1, &slot, ltog, cnt, cols, dnz, onz)); 185547c6ae99SBarry Smith } 185647c6ae99SBarry Smith } 185747c6ae99SBarry Smith } 18589566063dSJacob Faibussowitsch PetscCall(MatSeqBAIJSetPreallocation(J, nc, 0, dnz)); 18599566063dSJacob Faibussowitsch PetscCall(MatMPIBAIJSetPreallocation(J, nc, 0, dnz, 0, onz)); 1860d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 186147c6ae99SBarry Smith 18629566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 186347c6ae99SBarry Smith 186447c6ae99SBarry Smith /* 186547c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 186647c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 186747c6ae99SBarry Smith PETSc ordering. 186847c6ae99SBarry Smith */ 1869fcfd50ebSBarry Smith if (!da->prealloc_only) { 18709566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * col * nc * nc, &values)); 187147c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1872bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1873bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 187447c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1875bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1876bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 187747c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 1878bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 1879bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 188047c6ae99SBarry Smith 188147c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 188247c6ae99SBarry Smith 188347c6ae99SBarry Smith cnt = 0; 188447c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 188547c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 188647c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 1887aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 188847c6ae99SBarry Smith cols[cnt++] = slot + ii + gnx * jj + gnx * gny * kk; 188947c6ae99SBarry Smith } 189047c6ae99SBarry Smith } 189147c6ae99SBarry Smith } 189247c6ae99SBarry Smith } 18939566063dSJacob Faibussowitsch PetscCall(MatSetValuesBlockedLocal(J, 1, &slot, cnt, cols, values, INSERT_VALUES)); 189447c6ae99SBarry Smith } 189547c6ae99SBarry Smith } 189647c6ae99SBarry Smith } 18979566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 1898e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 18999566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 19009566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 19019566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 19029566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 19039566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 190447c6ae99SBarry Smith } 19059566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 19063ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 190747c6ae99SBarry Smith } 190847c6ae99SBarry Smith 190947c6ae99SBarry Smith /* 191047c6ae99SBarry Smith This helper is for of SBAIJ preallocation, to discard the lower-triangular values which are difficult to 191147c6ae99SBarry Smith identify in the local ordering with periodic domain. 191247c6ae99SBarry Smith */ 1913d71ae5a4SJacob Faibussowitsch static PetscErrorCode L2GFilterUpperTriangular(ISLocalToGlobalMapping ltog, PetscInt *row, PetscInt *cnt, PetscInt col[]) 1914d71ae5a4SJacob Faibussowitsch { 191547c6ae99SBarry Smith PetscInt i, n; 191647c6ae99SBarry Smith 191747c6ae99SBarry Smith PetscFunctionBegin; 19189566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingApplyBlock(ltog, 1, row, row)); 19199566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingApplyBlock(ltog, *cnt, col, col)); 192047c6ae99SBarry Smith for (i = 0, n = 0; i < *cnt; i++) { 192147c6ae99SBarry Smith if (col[i] >= *row) col[n++] = col[i]; 192247c6ae99SBarry Smith } 192347c6ae99SBarry Smith *cnt = n; 19243ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 192547c6ae99SBarry Smith } 192647c6ae99SBarry Smith 1927d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPISBAIJ(DM da, Mat J) 1928d71ae5a4SJacob Faibussowitsch { 192947c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 193047c6ae99SBarry Smith PetscInt m, n, dim, s, *cols, nc, col, cnt, *dnz, *onz; 193147c6ae99SBarry Smith PetscInt istart, iend, jstart, jend, ii, jj; 193247c6ae99SBarry Smith MPI_Comm comm; 193347c6ae99SBarry Smith PetscScalar *values; 1934bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 1935aa219208SBarry Smith DMDAStencilType st; 193645b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 193747c6ae99SBarry Smith 193847c6ae99SBarry Smith PetscFunctionBegin; 193947c6ae99SBarry Smith /* 194047c6ae99SBarry Smith nc - number of components per grid point 194147c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 194247c6ae99SBarry Smith */ 19439566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, NULL, NULL, NULL, &nc, &s, &bx, &by, NULL, &st)); 194447c6ae99SBarry Smith col = 2 * s + 1; 194547c6ae99SBarry Smith 19469566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 19479566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 19489566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 194947c6ae99SBarry Smith 19509566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * nc * nc, &cols)); 195147c6ae99SBarry Smith 19529566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 195347c6ae99SBarry Smith 195447c6ae99SBarry Smith /* determine the matrix preallocation information */ 1955d0609cedSBarry Smith MatPreallocateBegin(comm, nx * ny, nx * ny, dnz, onz); 195647c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1957bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1958bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 195947c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1960bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1961bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 196247c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 196347c6ae99SBarry Smith 196447c6ae99SBarry Smith /* Find block columns in block row */ 196547c6ae99SBarry Smith cnt = 0; 196647c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 196747c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 1968ad540459SPierre Jolivet if (st == DMDA_STENCIL_BOX || !ii || !jj) cols[cnt++] = slot + ii + gnx * jj; 196947c6ae99SBarry Smith } 197047c6ae99SBarry Smith } 19719566063dSJacob Faibussowitsch PetscCall(L2GFilterUpperTriangular(ltog, &slot, &cnt, cols)); 19729566063dSJacob Faibussowitsch PetscCall(MatPreallocateSymmetricSetBlock(slot, cnt, cols, dnz, onz)); 197347c6ae99SBarry Smith } 197447c6ae99SBarry Smith } 19759566063dSJacob Faibussowitsch PetscCall(MatSeqSBAIJSetPreallocation(J, nc, 0, dnz)); 19769566063dSJacob Faibussowitsch PetscCall(MatMPISBAIJSetPreallocation(J, nc, 0, dnz, 0, onz)); 1977d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 197847c6ae99SBarry Smith 19799566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 198047c6ae99SBarry Smith 198147c6ae99SBarry Smith /* 198247c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 198347c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 198447c6ae99SBarry Smith PETSc ordering. 198547c6ae99SBarry Smith */ 1986fcfd50ebSBarry Smith if (!da->prealloc_only) { 19879566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * nc * nc, &values)); 198847c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1989bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1990bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 199147c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1992bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1993bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 199447c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 199547c6ae99SBarry Smith 199647c6ae99SBarry Smith /* Find block columns in block row */ 199747c6ae99SBarry Smith cnt = 0; 199847c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 199947c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 2000ad540459SPierre Jolivet if (st == DMDA_STENCIL_BOX || !ii || !jj) cols[cnt++] = slot + ii + gnx * jj; 200147c6ae99SBarry Smith } 200247c6ae99SBarry Smith } 20039566063dSJacob Faibussowitsch PetscCall(L2GFilterUpperTriangular(ltog, &slot, &cnt, cols)); 20049566063dSJacob Faibussowitsch PetscCall(MatSetValuesBlocked(J, 1, &slot, cnt, cols, values, INSERT_VALUES)); 200547c6ae99SBarry Smith } 200647c6ae99SBarry Smith } 20079566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 2008e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 20099566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 20109566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 20119566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 20129566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 20139566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 201447c6ae99SBarry Smith } 20159566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 20163ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 201747c6ae99SBarry Smith } 201847c6ae99SBarry Smith 2019d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPISBAIJ(DM da, Mat J) 2020d71ae5a4SJacob Faibussowitsch { 202147c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 202247c6ae99SBarry Smith PetscInt m, n, dim, s, *cols, k, nc, col, cnt, p, *dnz, *onz; 202347c6ae99SBarry Smith PetscInt istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk; 202447c6ae99SBarry Smith MPI_Comm comm; 202547c6ae99SBarry Smith PetscScalar *values; 2026bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by, bz; 2027aa219208SBarry Smith DMDAStencilType st; 202845b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 202947c6ae99SBarry Smith 203047c6ae99SBarry Smith PetscFunctionBegin; 203147c6ae99SBarry Smith /* 203247c6ae99SBarry Smith nc - number of components per grid point 203347c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 203447c6ae99SBarry Smith */ 20359566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, NULL, NULL, NULL, &nc, &s, &bx, &by, &bz, &st)); 203647c6ae99SBarry Smith col = 2 * s + 1; 203747c6ae99SBarry Smith 20389566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 20399566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 20409566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 204147c6ae99SBarry Smith 204247c6ae99SBarry Smith /* create the matrix */ 20439566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * col, &cols)); 204447c6ae99SBarry Smith 20459566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 204647c6ae99SBarry Smith 204747c6ae99SBarry Smith /* determine the matrix preallocation information */ 2048d0609cedSBarry Smith MatPreallocateBegin(comm, nx * ny * nz, nx * ny * nz, dnz, onz); 204947c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 2050bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 2051bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 205247c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 2053bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 2054bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 205547c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 2056bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 2057bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 205847c6ae99SBarry Smith 205947c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 206047c6ae99SBarry Smith 206147c6ae99SBarry Smith /* Find block columns in block row */ 206247c6ae99SBarry Smith cnt = 0; 206347c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 206447c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 206547c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 2066ad540459SPierre Jolivet if ((st == DMDA_STENCIL_BOX) || (!ii && !jj) || (!jj && !kk) || (!ii && !kk)) cols[cnt++] = slot + ii + gnx * jj + gnx * gny * kk; 206747c6ae99SBarry Smith } 206847c6ae99SBarry Smith } 206947c6ae99SBarry Smith } 20709566063dSJacob Faibussowitsch PetscCall(L2GFilterUpperTriangular(ltog, &slot, &cnt, cols)); 20719566063dSJacob Faibussowitsch PetscCall(MatPreallocateSymmetricSetBlock(slot, cnt, cols, dnz, onz)); 207247c6ae99SBarry Smith } 207347c6ae99SBarry Smith } 207447c6ae99SBarry Smith } 20759566063dSJacob Faibussowitsch PetscCall(MatSeqSBAIJSetPreallocation(J, nc, 0, dnz)); 20769566063dSJacob Faibussowitsch PetscCall(MatMPISBAIJSetPreallocation(J, nc, 0, dnz, 0, onz)); 2077d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 207847c6ae99SBarry Smith 20799566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 208047c6ae99SBarry Smith 208147c6ae99SBarry Smith /* 208247c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 208347c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 208447c6ae99SBarry Smith PETSc ordering. 208547c6ae99SBarry Smith */ 2086fcfd50ebSBarry Smith if (!da->prealloc_only) { 20879566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * col * nc * nc, &values)); 208847c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 2089bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 2090bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 209147c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 2092bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 2093bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 209447c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 2095bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 2096bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 209747c6ae99SBarry Smith 209847c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 209947c6ae99SBarry Smith 210047c6ae99SBarry Smith cnt = 0; 210147c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 210247c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 210347c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 2104ad540459SPierre Jolivet if ((st == DMDA_STENCIL_BOX) || (!ii && !jj) || (!jj && !kk) || (!ii && !kk)) cols[cnt++] = slot + ii + gnx * jj + gnx * gny * kk; 210547c6ae99SBarry Smith } 210647c6ae99SBarry Smith } 210747c6ae99SBarry Smith } 21089566063dSJacob Faibussowitsch PetscCall(L2GFilterUpperTriangular(ltog, &slot, &cnt, cols)); 21099566063dSJacob Faibussowitsch PetscCall(MatSetValuesBlocked(J, 1, &slot, cnt, cols, values, INSERT_VALUES)); 211047c6ae99SBarry Smith } 211147c6ae99SBarry Smith } 211247c6ae99SBarry Smith } 21139566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 2114e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 21159566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 21169566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 21179566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 21189566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 21199566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 212047c6ae99SBarry Smith } 21219566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 21223ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 212347c6ae99SBarry Smith } 212447c6ae99SBarry Smith 212547c6ae99SBarry Smith /* ---------------------------------------------------------------------------------*/ 212647c6ae99SBarry Smith 2127d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPIAIJ_Fill(DM da, Mat J) 2128d71ae5a4SJacob Faibussowitsch { 212947c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 2130c0ab637bSBarry Smith PetscInt m, n, dim, s, *cols, k, nc, row, col, cnt, maxcnt = 0, l, p, *dnz, *onz; 2131c1154cd5SBarry Smith PetscInt istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk, M, N, P; 213247c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 213347c6ae99SBarry Smith PetscInt ifill_col, *dfill = dd->dfill, *ofill = dd->ofill; 213447c6ae99SBarry Smith MPI_Comm comm; 213547c6ae99SBarry Smith PetscScalar *values; 2136bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by, bz; 213745b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 2138aa219208SBarry Smith DMDAStencilType st; 2139c1154cd5SBarry Smith PetscBool removedups = PETSC_FALSE; 214047c6ae99SBarry Smith 214147c6ae99SBarry Smith PetscFunctionBegin; 214247c6ae99SBarry Smith /* 214347c6ae99SBarry Smith nc - number of components per grid point 214447c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 214547c6ae99SBarry Smith */ 21469566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &nc, &s, &bx, &by, &bz, &st)); 214747c6ae99SBarry Smith col = 2 * s + 1; 21481dca8a05SBarry 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\ 214947c6ae99SBarry Smith by 2*stencil_width + 1\n"); 21501dca8a05SBarry 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\ 215147c6ae99SBarry Smith by 2*stencil_width + 1\n"); 21521dca8a05SBarry 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\ 215347c6ae99SBarry Smith by 2*stencil_width + 1\n"); 215447c6ae99SBarry Smith 2155c1154cd5SBarry Smith /* 2156c1154cd5SBarry Smith With one processor in periodic domains in a skinny dimension the code will label nonzero columns multiple times 2157c1154cd5SBarry Smith because of "wrapping" around the end of the domain hitting an entry already counted in the other direction. 2158c1154cd5SBarry Smith */ 2159c1154cd5SBarry Smith if (M == 1 && 2 * s >= m) removedups = PETSC_TRUE; 2160c1154cd5SBarry Smith if (N == 1 && 2 * s >= n) removedups = PETSC_TRUE; 2161c1154cd5SBarry Smith if (P == 1 && 2 * s >= p) removedups = PETSC_TRUE; 2162c1154cd5SBarry Smith 21639566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 21649566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 21659566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 216647c6ae99SBarry Smith 21679566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * col * nc, &cols)); 21689566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 216947c6ae99SBarry Smith 217047c6ae99SBarry Smith /* determine the matrix preallocation information */ 2171d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny * nz, nc * nx * ny * nz, dnz, onz); 217247c6ae99SBarry Smith 21739566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 217447c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 2175bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 2176bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 217747c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 2178bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 2179bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 218047c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 2181bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 2182bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 218347c6ae99SBarry Smith 218447c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 218547c6ae99SBarry Smith 218647c6ae99SBarry Smith for (l = 0; l < nc; l++) { 218747c6ae99SBarry Smith cnt = 0; 218847c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 218947c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 219047c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 219147c6ae99SBarry Smith if (ii || jj || kk) { 2192aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 21938865f1eaSKarl 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); 219447c6ae99SBarry Smith } 219547c6ae99SBarry Smith } else { 219647c6ae99SBarry Smith if (dfill) { 21978865f1eaSKarl 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); 219847c6ae99SBarry Smith } else { 21998865f1eaSKarl Rupp for (ifill_col = 0; ifill_col < nc; ifill_col++) cols[cnt++] = ifill_col + nc * (slot + ii + gnx * jj + gnx * gny * kk); 220047c6ae99SBarry Smith } 220147c6ae99SBarry Smith } 220247c6ae99SBarry Smith } 220347c6ae99SBarry Smith } 220447c6ae99SBarry Smith } 220547c6ae99SBarry Smith row = l + nc * (slot); 2206c0ab637bSBarry Smith maxcnt = PetscMax(maxcnt, cnt); 22071baa6e33SBarry Smith if (removedups) PetscCall(MatPreallocateSetLocalRemoveDups(ltog, 1, &row, ltog, cnt, cols, dnz, onz)); 22081baa6e33SBarry Smith else PetscCall(MatPreallocateSetLocal(ltog, 1, &row, ltog, cnt, cols, dnz, onz)); 220947c6ae99SBarry Smith } 221047c6ae99SBarry Smith } 221147c6ae99SBarry Smith } 2212c1154cd5SBarry Smith } 22139566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz)); 22149566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz)); 2215d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 22169566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 221747c6ae99SBarry Smith 221847c6ae99SBarry Smith /* 221947c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 222047c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 222147c6ae99SBarry Smith PETSc ordering. 222247c6ae99SBarry Smith */ 2223fcfd50ebSBarry Smith if (!da->prealloc_only) { 22249566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(maxcnt, &values)); 222547c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 2226bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 2227bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 222847c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 2229bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 2230bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 223147c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 2232bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 2233bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 223447c6ae99SBarry Smith 223547c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 223647c6ae99SBarry Smith 223747c6ae99SBarry Smith for (l = 0; l < nc; l++) { 223847c6ae99SBarry Smith cnt = 0; 223947c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 224047c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 224147c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 224247c6ae99SBarry Smith if (ii || jj || kk) { 2243aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 22448865f1eaSKarl 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); 224547c6ae99SBarry Smith } 224647c6ae99SBarry Smith } else { 224747c6ae99SBarry Smith if (dfill) { 22488865f1eaSKarl 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); 224947c6ae99SBarry Smith } else { 22508865f1eaSKarl Rupp for (ifill_col = 0; ifill_col < nc; ifill_col++) cols[cnt++] = ifill_col + nc * (slot + ii + gnx * jj + gnx * gny * kk); 225147c6ae99SBarry Smith } 225247c6ae99SBarry Smith } 225347c6ae99SBarry Smith } 225447c6ae99SBarry Smith } 225547c6ae99SBarry Smith } 225647c6ae99SBarry Smith row = l + nc * (slot); 22579566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, 1, &row, cnt, cols, values, INSERT_VALUES)); 225847c6ae99SBarry Smith } 225947c6ae99SBarry Smith } 226047c6ae99SBarry Smith } 226147c6ae99SBarry Smith } 22629566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 2263e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 22649566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 22659566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 22669566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 22679566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 22689566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 226947c6ae99SBarry Smith } 22709566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 22713ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 227247c6ae99SBarry Smith } 2273