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; 2262ddab442SBarry Smith } else { 2272ddab442SBarry Smith PetscCheck((dim == 1) || !((m == 1 && bx == DM_BOUNDARY_PERIODIC) || (n == 1 && by == DM_BOUNDARY_PERIODIC) || (p == 1 && bz == DM_BOUNDARY_PERIODIC)), PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "IS_COLORING_LOCAL cannot be used for periodic boundary condition having both ends of the domain on the same process"); 22847c6ae99SBarry Smith } 22947c6ae99SBarry Smith } 23047c6ae99SBarry Smith 231aa219208SBarry Smith /* Tell the DMDA it has 1 degree of freedom per grid point so that the coloring for BAIJ 23247c6ae99SBarry Smith matrices is for the blocks, not the individual matrix elements */ 2339566063dSJacob Faibussowitsch PetscCall(PetscStrbeginswith(da->mattype, MATBAIJ, &isBAIJ)); 2349566063dSJacob Faibussowitsch if (!isBAIJ) PetscCall(PetscStrbeginswith(da->mattype, MATMPIBAIJ, &isBAIJ)); 2359566063dSJacob Faibussowitsch if (!isBAIJ) PetscCall(PetscStrbeginswith(da->mattype, MATSEQBAIJ, &isBAIJ)); 23647c6ae99SBarry Smith if (isBAIJ) { 23747c6ae99SBarry Smith dd->w = 1; 23847c6ae99SBarry Smith dd->xs = dd->xs / nc; 23947c6ae99SBarry Smith dd->xe = dd->xe / nc; 24047c6ae99SBarry Smith dd->Xs = dd->Xs / nc; 24147c6ae99SBarry Smith dd->Xe = dd->Xe / nc; 24247c6ae99SBarry Smith } 24347c6ae99SBarry Smith 24447c6ae99SBarry Smith /* 245aa219208SBarry Smith We do not provide a getcoloring function in the DMDA operations because 2469a1b256bSStefano Zampini the basic DMDA does not know about matrices. We think of DMDA as being 24747c6ae99SBarry Smith more low-level then matrices. 24847c6ae99SBarry Smith */ 2491baa6e33SBarry Smith if (dim == 1) PetscCall(DMCreateColoring_DA_1d_MPIAIJ(da, ctype, coloring)); 2501baa6e33SBarry Smith else if (dim == 2) PetscCall(DMCreateColoring_DA_2d_MPIAIJ(da, ctype, coloring)); 2511baa6e33SBarry Smith else if (dim == 3) PetscCall(DMCreateColoring_DA_3d_MPIAIJ(da, ctype, coloring)); 2521baa6e33SBarry Smith else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "Not done for %" PetscInt_FMT " dimension, send us mail petsc-maint@mcs.anl.gov for code", dim); 25347c6ae99SBarry Smith if (isBAIJ) { 25447c6ae99SBarry Smith dd->w = nc; 25547c6ae99SBarry Smith dd->xs = dd->xs * nc; 25647c6ae99SBarry Smith dd->xe = dd->xe * nc; 25747c6ae99SBarry Smith dd->Xs = dd->Xs * nc; 25847c6ae99SBarry Smith dd->Xe = dd->Xe * nc; 25947c6ae99SBarry Smith } 2603ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 26147c6ae99SBarry Smith } 26247c6ae99SBarry Smith 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; 348*494b1ccaSBarry 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\ 349*494b1ccaSBarry Smith by 2*stencil_width + 1\n"); 350*494b1ccaSBarry 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\ 351*494b1ccaSBarry Smith by 2*stencil_width + 1\n"); 352*494b1ccaSBarry 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\ 353*494b1ccaSBarry Smith by 2*stencil_width + 1\n"); 354*494b1ccaSBarry Smith 3559566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 3569566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 3579566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 35847c6ae99SBarry Smith 35947c6ae99SBarry Smith /* create the coloring */ 36047c6ae99SBarry Smith if (ctype == IS_COLORING_GLOBAL) { 36147c6ae99SBarry Smith if (!dd->localcoloring) { 3629566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * nx * ny * nz, &colors)); 36347c6ae99SBarry Smith ii = 0; 36447c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 36547c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 36647c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 367ad540459SPierre Jolivet for (l = 0; l < nc; l++) colors[ii++] = l + nc * ((i % col) + col * (j % col) + col * col * (k % col)); 36847c6ae99SBarry Smith } 36947c6ae99SBarry Smith } 37047c6ae99SBarry Smith } 37147c6ae99SBarry Smith ncolors = nc + nc * (col - 1 + col * (col - 1) + col * col * (col - 1)); 3729566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * nx * ny * nz, colors, PETSC_OWN_POINTER, &dd->localcoloring)); 37347c6ae99SBarry Smith } 37447c6ae99SBarry Smith *coloring = dd->localcoloring; 3755bdb020cSBarry Smith } else if (ctype == IS_COLORING_LOCAL) { 37647c6ae99SBarry Smith if (!dd->ghostedcoloring) { 3779566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * gnx * gny * gnz, &colors)); 37847c6ae99SBarry Smith ii = 0; 37947c6ae99SBarry Smith for (k = gzs; k < gzs + gnz; k++) { 38047c6ae99SBarry Smith for (j = gys; j < gys + gny; j++) { 38147c6ae99SBarry Smith for (i = gxs; i < gxs + gnx; i++) { 38247c6ae99SBarry Smith for (l = 0; l < nc; l++) { 38347c6ae99SBarry Smith /* the complicated stuff is to handle periodic boundaries */ 38447c6ae99SBarry Smith colors[ii++] = l + nc * ((SetInRange(i, m) % col) + col * (SetInRange(j, n) % col) + col * col * (SetInRange(k, p) % col)); 38547c6ae99SBarry Smith } 38647c6ae99SBarry Smith } 38747c6ae99SBarry Smith } 38847c6ae99SBarry Smith } 38947c6ae99SBarry Smith ncolors = nc + nc * (col - 1 + col * (col - 1) + col * col * (col - 1)); 3909566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * gnx * gny * gnz, colors, PETSC_OWN_POINTER, &dd->ghostedcoloring)); 3919566063dSJacob Faibussowitsch PetscCall(ISColoringSetType(dd->ghostedcoloring, IS_COLORING_LOCAL)); 39247c6ae99SBarry Smith } 39347c6ae99SBarry Smith *coloring = dd->ghostedcoloring; 39498921bdaSJacob Faibussowitsch } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_ARG_WRONG, "Unknown ISColoringType %d", (int)ctype); 3959566063dSJacob Faibussowitsch PetscCall(ISColoringReference(*coloring)); 3963ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 39747c6ae99SBarry Smith } 39847c6ae99SBarry Smith 39947c6ae99SBarry Smith /* ---------------------------------------------------------------------------------*/ 40047c6ae99SBarry Smith 401d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA_1d_MPIAIJ(DM da, ISColoringType ctype, ISColoring *coloring) 402d71ae5a4SJacob Faibussowitsch { 40347c6ae99SBarry Smith PetscInt xs, nx, i, i1, gxs, gnx, l, m, M, dim, s, nc, col; 40447c6ae99SBarry Smith PetscInt ncolors; 40547c6ae99SBarry Smith MPI_Comm comm; 406bff4a2f0SMatthew G. Knepley DMBoundaryType bx; 40747c6ae99SBarry Smith ISColoringValue *colors; 40847c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 40947c6ae99SBarry Smith 41047c6ae99SBarry Smith PetscFunctionBegin; 41147c6ae99SBarry Smith /* 41247c6ae99SBarry Smith nc - number of components per grid point 41347c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 41447c6ae99SBarry Smith */ 4159566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, NULL, NULL, &M, NULL, NULL, &nc, &s, &bx, NULL, NULL, NULL)); 41647c6ae99SBarry Smith col = 2 * s + 1; 4179566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &nx, NULL, NULL)); 4189566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, NULL, NULL, &gnx, NULL, NULL)); 4199566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 42047c6ae99SBarry Smith 42147c6ae99SBarry Smith /* create the coloring */ 42247c6ae99SBarry Smith if (ctype == IS_COLORING_GLOBAL) { 42347c6ae99SBarry Smith if (!dd->localcoloring) { 4249566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * nx, &colors)); 425ae4f298aSBarry Smith if (dd->ofillcols) { 426ae4f298aSBarry Smith PetscInt tc = 0; 427ae4f298aSBarry Smith for (i = 0; i < nc; i++) tc += (PetscInt)(dd->ofillcols[i] > 0); 428ae4f298aSBarry Smith i1 = 0; 429ae4f298aSBarry Smith for (i = xs; i < xs + nx; i++) { 430ae4f298aSBarry Smith for (l = 0; l < nc; l++) { 431ae4f298aSBarry Smith if (dd->ofillcols[l] && (i % col)) { 432ae4f298aSBarry Smith colors[i1++] = nc - 1 + tc * ((i % col) - 1) + dd->ofillcols[l]; 433ae4f298aSBarry Smith } else { 434ae4f298aSBarry Smith colors[i1++] = l; 435ae4f298aSBarry Smith } 436ae4f298aSBarry Smith } 437ae4f298aSBarry Smith } 438ae4f298aSBarry Smith ncolors = nc + 2 * s * tc; 439ae4f298aSBarry Smith } else { 44047c6ae99SBarry Smith i1 = 0; 44147c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 442ad540459SPierre Jolivet for (l = 0; l < nc; l++) colors[i1++] = l + nc * (i % col); 44347c6ae99SBarry Smith } 44447c6ae99SBarry Smith ncolors = nc + nc * (col - 1); 445ae4f298aSBarry Smith } 4469566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * nx, colors, PETSC_OWN_POINTER, &dd->localcoloring)); 44747c6ae99SBarry Smith } 44847c6ae99SBarry Smith *coloring = dd->localcoloring; 4495bdb020cSBarry Smith } else if (ctype == IS_COLORING_LOCAL) { 45047c6ae99SBarry Smith if (!dd->ghostedcoloring) { 4519566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * gnx, &colors)); 45247c6ae99SBarry Smith i1 = 0; 45347c6ae99SBarry Smith for (i = gxs; i < gxs + gnx; i++) { 45447c6ae99SBarry Smith for (l = 0; l < nc; l++) { 45547c6ae99SBarry Smith /* the complicated stuff is to handle periodic boundaries */ 45647c6ae99SBarry Smith colors[i1++] = l + nc * (SetInRange(i, m) % col); 45747c6ae99SBarry Smith } 45847c6ae99SBarry Smith } 45947c6ae99SBarry Smith ncolors = nc + nc * (col - 1); 4609566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * gnx, colors, PETSC_OWN_POINTER, &dd->ghostedcoloring)); 4619566063dSJacob Faibussowitsch PetscCall(ISColoringSetType(dd->ghostedcoloring, IS_COLORING_LOCAL)); 46247c6ae99SBarry Smith } 46347c6ae99SBarry Smith *coloring = dd->ghostedcoloring; 46498921bdaSJacob Faibussowitsch } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_ARG_WRONG, "Unknown ISColoringType %d", (int)ctype); 4659566063dSJacob Faibussowitsch PetscCall(ISColoringReference(*coloring)); 4663ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 46747c6ae99SBarry Smith } 46847c6ae99SBarry Smith 469d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA_2d_5pt_MPIAIJ(DM da, ISColoringType ctype, ISColoring *coloring) 470d71ae5a4SJacob Faibussowitsch { 47147c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, ii, gxs, gys, gnx, gny, m, n, dim, s, k, nc; 47247c6ae99SBarry Smith PetscInt ncolors; 47347c6ae99SBarry Smith MPI_Comm comm; 474bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 47547c6ae99SBarry Smith ISColoringValue *colors; 47647c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 47747c6ae99SBarry Smith 47847c6ae99SBarry Smith PetscFunctionBegin; 47947c6ae99SBarry Smith /* 48047c6ae99SBarry Smith nc - number of components per grid point 48147c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 48247c6ae99SBarry Smith */ 4839566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, NULL, NULL, NULL, &nc, &s, &bx, &by, NULL, NULL)); 4849566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 4859566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 4869566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 48747c6ae99SBarry Smith /* create the coloring */ 48847c6ae99SBarry Smith if (ctype == IS_COLORING_GLOBAL) { 48947c6ae99SBarry Smith if (!dd->localcoloring) { 4909566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * nx * ny, &colors)); 49147c6ae99SBarry Smith ii = 0; 49247c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 49347c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 494ad540459SPierre Jolivet for (k = 0; k < nc; k++) colors[ii++] = k + nc * ((3 * j + i) % 5); 49547c6ae99SBarry Smith } 49647c6ae99SBarry Smith } 49747c6ae99SBarry Smith ncolors = 5 * nc; 4989566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * nx * ny, colors, PETSC_OWN_POINTER, &dd->localcoloring)); 49947c6ae99SBarry Smith } 50047c6ae99SBarry Smith *coloring = dd->localcoloring; 5015bdb020cSBarry Smith } else if (ctype == IS_COLORING_LOCAL) { 50247c6ae99SBarry Smith if (!dd->ghostedcoloring) { 5039566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * gnx * gny, &colors)); 50447c6ae99SBarry Smith ii = 0; 50547c6ae99SBarry Smith for (j = gys; j < gys + gny; j++) { 50647c6ae99SBarry Smith for (i = gxs; i < gxs + gnx; i++) { 507ad540459SPierre Jolivet for (k = 0; k < nc; k++) colors[ii++] = k + nc * ((3 * SetInRange(j, n) + SetInRange(i, m)) % 5); 50847c6ae99SBarry Smith } 50947c6ae99SBarry Smith } 51047c6ae99SBarry Smith ncolors = 5 * nc; 5119566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * gnx * gny, colors, PETSC_OWN_POINTER, &dd->ghostedcoloring)); 5129566063dSJacob Faibussowitsch PetscCall(ISColoringSetType(dd->ghostedcoloring, IS_COLORING_LOCAL)); 51347c6ae99SBarry Smith } 51447c6ae99SBarry Smith *coloring = dd->ghostedcoloring; 51598921bdaSJacob Faibussowitsch } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_ARG_WRONG, "Unknown ISColoringType %d", (int)ctype); 5163ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 51747c6ae99SBarry Smith } 51847c6ae99SBarry Smith 51947c6ae99SBarry Smith /* =========================================================================== */ 520e432b41dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_1d_MPIAIJ(DM, Mat); 521ce308e1dSBarry Smith extern PetscErrorCode DMCreateMatrix_DA_1d_MPIAIJ_Fill(DM, Mat); 522e432b41dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_1d_SeqAIJ_NoPreallocation(DM, Mat); 523e432b41dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_2d_MPIAIJ(DM, Mat); 524950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_2d_MPIAIJ_Fill(DM, Mat); 525e432b41dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_3d_MPIAIJ(DM, Mat); 526950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_3d_MPIAIJ_Fill(DM, Mat); 527950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_2d_MPIBAIJ(DM, Mat); 528950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_3d_MPIBAIJ(DM, Mat); 529950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_2d_MPISBAIJ(DM, Mat); 530950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_3d_MPISBAIJ(DM, Mat); 531d4002b98SHong Zhang extern PetscErrorCode DMCreateMatrix_DA_2d_MPISELL(DM, Mat); 532d4002b98SHong Zhang extern PetscErrorCode DMCreateMatrix_DA_3d_MPISELL(DM, Mat); 533e584696dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_IS(DM, Mat); 53447c6ae99SBarry Smith 5358bbdbebaSMatthew G Knepley /*@C 536dce8aebaSBarry Smith MatSetupDM - Sets the `DMDA` that is to be used by the HYPRE_StructMatrix PETSc matrix 53747c6ae99SBarry Smith 538d083f849SBarry Smith Logically Collective on mat 53947c6ae99SBarry Smith 54047c6ae99SBarry Smith Input Parameters: 54147c6ae99SBarry Smith + mat - the matrix 54247c6ae99SBarry Smith - da - the da 54347c6ae99SBarry Smith 54447c6ae99SBarry Smith Level: intermediate 54547c6ae99SBarry Smith 546dce8aebaSBarry Smith .seealso: `Mat`, `MatSetUp()` 54747c6ae99SBarry Smith @*/ 548d71ae5a4SJacob Faibussowitsch PetscErrorCode MatSetupDM(Mat mat, DM da) 549d71ae5a4SJacob Faibussowitsch { 55047c6ae99SBarry Smith PetscFunctionBegin; 55147c6ae99SBarry Smith PetscValidHeaderSpecific(mat, MAT_CLASSID, 1); 552064a246eSJacob Faibussowitsch PetscValidHeaderSpecificType(da, DM_CLASSID, 2, DMDA); 553cac4c232SBarry Smith PetscTryMethod(mat, "MatSetupDM_C", (Mat, DM), (mat, da)); 5543ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 55547c6ae99SBarry Smith } 55647c6ae99SBarry Smith 557d71ae5a4SJacob Faibussowitsch PetscErrorCode MatView_MPI_DA(Mat A, PetscViewer viewer) 558d71ae5a4SJacob Faibussowitsch { 5599a42bb27SBarry Smith DM da; 56047c6ae99SBarry Smith const char *prefix; 56147c6ae99SBarry Smith Mat Anatural; 56247c6ae99SBarry Smith AO ao; 56347c6ae99SBarry Smith PetscInt rstart, rend, *petsc, i; 56447c6ae99SBarry Smith IS is; 56547c6ae99SBarry Smith MPI_Comm comm; 56674388724SJed Brown PetscViewerFormat format; 56747c6ae99SBarry Smith 56847c6ae99SBarry Smith PetscFunctionBegin; 56974388724SJed Brown /* Check whether we are just printing info, in which case MatView() already viewed everything we wanted to view */ 5709566063dSJacob Faibussowitsch PetscCall(PetscViewerGetFormat(viewer, &format)); 5713ba16761SJacob Faibussowitsch if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) PetscFunctionReturn(PETSC_SUCCESS); 57274388724SJed Brown 5739566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)A, &comm)); 5749566063dSJacob Faibussowitsch PetscCall(MatGetDM(A, &da)); 5757a8be351SBarry Smith PetscCheck(da, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Matrix not generated from a DMDA"); 57647c6ae99SBarry Smith 5779566063dSJacob Faibussowitsch PetscCall(DMDAGetAO(da, &ao)); 5789566063dSJacob Faibussowitsch PetscCall(MatGetOwnershipRange(A, &rstart, &rend)); 5799566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(rend - rstart, &petsc)); 58047c6ae99SBarry Smith for (i = rstart; i < rend; i++) petsc[i - rstart] = i; 5819566063dSJacob Faibussowitsch PetscCall(AOApplicationToPetsc(ao, rend - rstart, petsc)); 5829566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(comm, rend - rstart, petsc, PETSC_OWN_POINTER, &is)); 58347c6ae99SBarry Smith 58447c6ae99SBarry Smith /* call viewer on natural ordering */ 5859566063dSJacob Faibussowitsch PetscCall(MatCreateSubMatrix(A, is, is, MAT_INITIAL_MATRIX, &Anatural)); 5869566063dSJacob Faibussowitsch PetscCall(ISDestroy(&is)); 5879566063dSJacob Faibussowitsch PetscCall(PetscObjectGetOptionsPrefix((PetscObject)A, &prefix)); 5889566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptionsPrefix((PetscObject)Anatural, prefix)); 5899566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)Anatural, ((PetscObject)A)->name)); 590f0ed2f47SStefano Zampini ((PetscObject)Anatural)->donotPetscObjectPrintClassNamePrefixType = PETSC_TRUE; 5919566063dSJacob Faibussowitsch PetscCall(MatView(Anatural, viewer)); 592f0ed2f47SStefano Zampini ((PetscObject)Anatural)->donotPetscObjectPrintClassNamePrefixType = PETSC_FALSE; 5939566063dSJacob Faibussowitsch PetscCall(MatDestroy(&Anatural)); 5943ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 59547c6ae99SBarry Smith } 59647c6ae99SBarry Smith 597d71ae5a4SJacob Faibussowitsch PetscErrorCode MatLoad_MPI_DA(Mat A, PetscViewer viewer) 598d71ae5a4SJacob Faibussowitsch { 5999a42bb27SBarry Smith DM da; 60047c6ae99SBarry Smith Mat Anatural, Aapp; 60147c6ae99SBarry Smith AO ao; 602539c167fSBarry Smith PetscInt rstart, rend, *app, i, m, n, M, N; 60347c6ae99SBarry Smith IS is; 60447c6ae99SBarry Smith MPI_Comm comm; 60547c6ae99SBarry Smith 60647c6ae99SBarry Smith PetscFunctionBegin; 6079566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)A, &comm)); 6089566063dSJacob Faibussowitsch PetscCall(MatGetDM(A, &da)); 6097a8be351SBarry Smith PetscCheck(da, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Matrix not generated from a DMDA"); 61047c6ae99SBarry Smith 61147c6ae99SBarry Smith /* Load the matrix in natural ordering */ 6129566063dSJacob Faibussowitsch PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &Anatural)); 6139566063dSJacob Faibussowitsch PetscCall(MatSetType(Anatural, ((PetscObject)A)->type_name)); 6149566063dSJacob Faibussowitsch PetscCall(MatGetSize(A, &M, &N)); 6159566063dSJacob Faibussowitsch PetscCall(MatGetLocalSize(A, &m, &n)); 6169566063dSJacob Faibussowitsch PetscCall(MatSetSizes(Anatural, m, n, M, N)); 6179566063dSJacob Faibussowitsch PetscCall(MatLoad(Anatural, viewer)); 61847c6ae99SBarry Smith 61947c6ae99SBarry Smith /* Map natural ordering to application ordering and create IS */ 6209566063dSJacob Faibussowitsch PetscCall(DMDAGetAO(da, &ao)); 6219566063dSJacob Faibussowitsch PetscCall(MatGetOwnershipRange(Anatural, &rstart, &rend)); 6229566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(rend - rstart, &app)); 62347c6ae99SBarry Smith for (i = rstart; i < rend; i++) app[i - rstart] = i; 6249566063dSJacob Faibussowitsch PetscCall(AOPetscToApplication(ao, rend - rstart, app)); 6259566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(comm, rend - rstart, app, PETSC_OWN_POINTER, &is)); 62647c6ae99SBarry Smith 62747c6ae99SBarry Smith /* Do permutation and replace header */ 6289566063dSJacob Faibussowitsch PetscCall(MatCreateSubMatrix(Anatural, is, is, MAT_INITIAL_MATRIX, &Aapp)); 6299566063dSJacob Faibussowitsch PetscCall(MatHeaderReplace(A, &Aapp)); 6309566063dSJacob Faibussowitsch PetscCall(ISDestroy(&is)); 6319566063dSJacob Faibussowitsch PetscCall(MatDestroy(&Anatural)); 6323ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 63347c6ae99SBarry Smith } 63447c6ae99SBarry Smith 635d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA(DM da, Mat *J) 636d71ae5a4SJacob Faibussowitsch { 63747c6ae99SBarry Smith PetscInt dim, dof, nx, ny, nz, dims[3], starts[3], M, N, P; 63847c6ae99SBarry Smith Mat A; 63947c6ae99SBarry Smith MPI_Comm comm; 64019fd82e9SBarry Smith MatType Atype; 641b412c318SBarry Smith MatType mtype; 64247c6ae99SBarry Smith PetscMPIInt size; 64347c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 644ade515a3SBarry Smith void (*aij)(void) = NULL, (*baij)(void) = NULL, (*sbaij)(void) = NULL, (*sell)(void) = NULL, (*is)(void) = NULL; 64547c6ae99SBarry Smith 64647c6ae99SBarry Smith PetscFunctionBegin; 6479566063dSJacob Faibussowitsch PetscCall(MatInitializePackage()); 648b412c318SBarry Smith mtype = da->mattype; 64947c6ae99SBarry Smith 65047c6ae99SBarry Smith /* 65147c6ae99SBarry Smith m 65247c6ae99SBarry Smith ------------------------------------------------------ 65347c6ae99SBarry Smith | | 65447c6ae99SBarry Smith | | 65547c6ae99SBarry Smith | ---------------------- | 65647c6ae99SBarry Smith | | | | 65747c6ae99SBarry Smith n | ny | | | 65847c6ae99SBarry Smith | | | | 65947c6ae99SBarry Smith | .--------------------- | 66047c6ae99SBarry Smith | (xs,ys) nx | 66147c6ae99SBarry Smith | . | 66247c6ae99SBarry Smith | (gxs,gys) | 66347c6ae99SBarry Smith | | 66447c6ae99SBarry Smith ----------------------------------------------------- 66547c6ae99SBarry Smith */ 66647c6ae99SBarry Smith 66747c6ae99SBarry Smith /* 66847c6ae99SBarry Smith nc - number of components per grid point 66947c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 67047c6ae99SBarry Smith */ 671e30e807fSPeter Brune M = dd->M; 672e30e807fSPeter Brune N = dd->N; 673e30e807fSPeter Brune P = dd->P; 674c73cfb54SMatthew G. Knepley dim = da->dim; 675e30e807fSPeter Brune dof = dd->w; 6769566063dSJacob Faibussowitsch /* PetscCall(DMDAGetInfo(da,&dim,&M,&N,&P,NULL,NULL,NULL,&dof,NULL,NULL,NULL,NULL,NULL)); */ 6779566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, NULL, NULL, NULL, &nx, &ny, &nz)); 6789566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 6799566063dSJacob Faibussowitsch PetscCall(MatCreate(comm, &A)); 6809566063dSJacob Faibussowitsch PetscCall(MatSetSizes(A, dof * nx * ny * nz, dof * nx * ny * nz, dof * M * N * P, dof * M * N * P)); 6819566063dSJacob Faibussowitsch PetscCall(MatSetType(A, mtype)); 6829566063dSJacob Faibussowitsch PetscCall(MatSetFromOptions(A)); 68374427ab1SRichard Tran Mills if (dof * nx * ny * nz < da->bind_below) { 6849566063dSJacob Faibussowitsch PetscCall(MatSetBindingPropagates(A, PETSC_TRUE)); 6859566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(A, PETSC_TRUE)); 68674427ab1SRichard Tran Mills } 6879566063dSJacob Faibussowitsch PetscCall(MatSetDM(A, da)); 6881baa6e33SBarry Smith if (da->structure_only) PetscCall(MatSetOption(A, MAT_STRUCTURE_ONLY, PETSC_TRUE)); 6899566063dSJacob Faibussowitsch PetscCall(MatGetType(A, &Atype)); 69047c6ae99SBarry Smith /* 691aa219208SBarry Smith We do not provide a getmatrix function in the DMDA operations because 692aa219208SBarry Smith the basic DMDA does not know about matrices. We think of DMDA as being more 69347c6ae99SBarry Smith more low-level than matrices. This is kind of cheating but, cause sometimes 694aa219208SBarry Smith we think of DMDA has higher level than matrices. 69547c6ae99SBarry Smith 69647c6ae99SBarry Smith We could switch based on Atype (or mtype), but we do not since the 697844bd0d7SStefano Zampini specialized setting routines depend only on the particular preallocation 69847c6ae99SBarry Smith details of the matrix, not the type itself. 69947c6ae99SBarry Smith */ 7009566063dSJacob Faibussowitsch PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatMPIAIJSetPreallocation_C", &aij)); 70148a46eb9SPierre Jolivet if (!aij) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatSeqAIJSetPreallocation_C", &aij)); 70247c6ae99SBarry Smith if (!aij) { 7039566063dSJacob Faibussowitsch PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatMPIBAIJSetPreallocation_C", &baij)); 70448a46eb9SPierre Jolivet if (!baij) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatSeqBAIJSetPreallocation_C", &baij)); 70547c6ae99SBarry Smith if (!baij) { 7069566063dSJacob Faibussowitsch PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatMPISBAIJSetPreallocation_C", &sbaij)); 70748a46eb9SPierre Jolivet if (!sbaij) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatSeqSBAIJSetPreallocation_C", &sbaij)); 7085e26d47bSHong Zhang if (!sbaij) { 7099566063dSJacob Faibussowitsch PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatMPISELLSetPreallocation_C", &sell)); 71048a46eb9SPierre Jolivet if (!sell) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatSeqSELLSetPreallocation_C", &sell)); 7115e26d47bSHong Zhang } 71248a46eb9SPierre Jolivet if (!sell) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatISSetPreallocation_C", &is)); 71347c6ae99SBarry Smith } 71447c6ae99SBarry Smith } 71547c6ae99SBarry Smith if (aij) { 71647c6ae99SBarry Smith if (dim == 1) { 717ce308e1dSBarry Smith if (dd->ofill) { 7189566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_1d_MPIAIJ_Fill(da, A)); 719ce308e1dSBarry Smith } else { 72019b08ed1SBarry Smith DMBoundaryType bx; 72119b08ed1SBarry Smith PetscMPIInt size; 7229566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &bx, NULL, NULL, NULL)); 7239566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)da), &size)); 72419b08ed1SBarry Smith if (size == 1 && bx == DM_BOUNDARY_NONE) { 7259566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_1d_SeqAIJ_NoPreallocation(da, A)); 72619b08ed1SBarry Smith } else { 7279566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_1d_MPIAIJ(da, A)); 728ce308e1dSBarry Smith } 72919b08ed1SBarry Smith } 73047c6ae99SBarry Smith } else if (dim == 2) { 73147c6ae99SBarry Smith if (dd->ofill) { 7329566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_2d_MPIAIJ_Fill(da, A)); 73347c6ae99SBarry Smith } else { 7349566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_2d_MPIAIJ(da, A)); 73547c6ae99SBarry Smith } 73647c6ae99SBarry Smith } else if (dim == 3) { 73747c6ae99SBarry Smith if (dd->ofill) { 7389566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_3d_MPIAIJ_Fill(da, A)); 73947c6ae99SBarry Smith } else { 7409566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_3d_MPIAIJ(da, A)); 74147c6ae99SBarry Smith } 74247c6ae99SBarry Smith } 74347c6ae99SBarry Smith } else if (baij) { 74447c6ae99SBarry Smith if (dim == 2) { 7459566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_2d_MPIBAIJ(da, A)); 74647c6ae99SBarry Smith } else if (dim == 3) { 7479566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_3d_MPIBAIJ(da, A)); 74863a3b9bcSJacob Faibussowitsch } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "Not implemented for %" PetscInt_FMT " dimension and Matrix Type: %s in %" PetscInt_FMT " dimension! Send mail to petsc-maint@mcs.anl.gov for code", dim, Atype, dim); 74947c6ae99SBarry Smith } else if (sbaij) { 75047c6ae99SBarry Smith if (dim == 2) { 7519566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_2d_MPISBAIJ(da, A)); 75247c6ae99SBarry Smith } else if (dim == 3) { 7539566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_3d_MPISBAIJ(da, A)); 75463a3b9bcSJacob Faibussowitsch } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "Not implemented for %" PetscInt_FMT " dimension and Matrix Type: %s in %" PetscInt_FMT " dimension! Send mail to petsc-maint@mcs.anl.gov for code", dim, Atype, dim); 755d4002b98SHong Zhang } else if (sell) { 7565e26d47bSHong Zhang if (dim == 2) { 7579566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_2d_MPISELL(da, A)); 758711261dbSHong Zhang } else if (dim == 3) { 7599566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_3d_MPISELL(da, A)); 76063a3b9bcSJacob 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); 761e584696dSStefano Zampini } else if (is) { 7629566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_IS(da, A)); 763869776cdSLisandro Dalcin } else { 76445b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 765e584696dSStefano Zampini 7669566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(A, dof)); 7679566063dSJacob Faibussowitsch PetscCall(MatSetUp(A)); 7689566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 7699566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(A, ltog, ltog)); 77047c6ae99SBarry Smith } 7719566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &starts[0], &starts[1], &starts[2], &dims[0], &dims[1], &dims[2])); 7729566063dSJacob Faibussowitsch PetscCall(MatSetStencil(A, dim, dims, starts, dof)); 7739566063dSJacob Faibussowitsch PetscCall(MatSetDM(A, da)); 7749566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(comm, &size)); 77547c6ae99SBarry Smith if (size > 1) { 77647c6ae99SBarry Smith /* change viewer to display matrix in natural ordering */ 7779566063dSJacob Faibussowitsch PetscCall(MatSetOperation(A, MATOP_VIEW, (void (*)(void))MatView_MPI_DA)); 7789566063dSJacob Faibussowitsch PetscCall(MatSetOperation(A, MATOP_LOAD, (void (*)(void))MatLoad_MPI_DA)); 77947c6ae99SBarry Smith } 78047c6ae99SBarry Smith *J = A; 7813ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 78247c6ae99SBarry Smith } 78347c6ae99SBarry Smith 78447c6ae99SBarry Smith /* ---------------------------------------------------------------------------------*/ 785844bd0d7SStefano Zampini PETSC_EXTERN PetscErrorCode MatISSetPreallocation_IS(Mat, PetscInt, const PetscInt[], PetscInt, const PetscInt[]); 786844bd0d7SStefano Zampini 787d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_IS(DM dm, Mat J) 788d71ae5a4SJacob Faibussowitsch { 789e584696dSStefano Zampini DM_DA *da = (DM_DA *)dm->data; 790e432b41dSStefano Zampini Mat lJ, P; 791e584696dSStefano Zampini ISLocalToGlobalMapping ltog; 792e432b41dSStefano Zampini IS is; 793e432b41dSStefano Zampini PetscBT bt; 79405339c03SStefano Zampini const PetscInt *e_loc, *idx; 795e432b41dSStefano Zampini PetscInt i, nel, nen, nv, dof, *gidx, n, N; 796e584696dSStefano Zampini 797e584696dSStefano Zampini /* The l2g map of DMDA has all ghosted nodes, and e_loc is a subset of all the local nodes (including the ghosted) 798e432b41dSStefano Zampini We need to filter out the local indices that are not represented through the DMDAGetElements decomposition */ 799e584696dSStefano Zampini PetscFunctionBegin; 800e584696dSStefano Zampini dof = da->w; 8019566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, dof)); 8029566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(dm, <og)); 803e432b41dSStefano Zampini 804e432b41dSStefano Zampini /* flag local elements indices in local DMDA numbering */ 8059566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingGetSize(ltog, &nv)); 8069566063dSJacob Faibussowitsch PetscCall(PetscBTCreate(nv / dof, &bt)); 8079566063dSJacob Faibussowitsch PetscCall(DMDAGetElements(dm, &nel, &nen, &e_loc)); /* this will throw an error if the stencil type is not DMDA_STENCIL_BOX */ 8089566063dSJacob Faibussowitsch for (i = 0; i < nel * nen; i++) PetscCall(PetscBTSet(bt, e_loc[i])); 809e432b41dSStefano Zampini 810e432b41dSStefano Zampini /* filter out (set to -1) the global indices not used by the local elements */ 8119566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nv / dof, &gidx)); 8129566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingGetBlockIndices(ltog, &idx)); 8139566063dSJacob Faibussowitsch PetscCall(PetscArraycpy(gidx, idx, nv / dof)); 8149566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingRestoreBlockIndices(ltog, &idx)); 8159371c9d4SSatish Balay for (i = 0; i < nv / dof; i++) 8169371c9d4SSatish Balay if (!PetscBTLookup(bt, i)) gidx[i] = -1; 8179566063dSJacob Faibussowitsch PetscCall(PetscBTDestroy(&bt)); 8189566063dSJacob Faibussowitsch PetscCall(ISCreateBlock(PetscObjectComm((PetscObject)dm), dof, nv / dof, gidx, PETSC_OWN_POINTER, &is)); 8199566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingCreateIS(is, <og)); 8209566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 8219566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingDestroy(<og)); 8229566063dSJacob Faibussowitsch PetscCall(ISDestroy(&is)); 82305339c03SStefano Zampini 824e432b41dSStefano Zampini /* Preallocation */ 825e306f867SJed Brown if (dm->prealloc_skip) { 8269566063dSJacob Faibussowitsch PetscCall(MatSetUp(J)); 827e306f867SJed Brown } else { 8289566063dSJacob Faibussowitsch PetscCall(MatISGetLocalMat(J, &lJ)); 8299566063dSJacob Faibussowitsch PetscCall(MatGetLocalToGlobalMapping(lJ, <og, NULL)); 8309566063dSJacob Faibussowitsch PetscCall(MatCreate(PetscObjectComm((PetscObject)lJ), &P)); 8319566063dSJacob Faibussowitsch PetscCall(MatSetType(P, MATPREALLOCATOR)); 8329566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(P, ltog, ltog)); 8339566063dSJacob Faibussowitsch PetscCall(MatGetSize(lJ, &N, NULL)); 8349566063dSJacob Faibussowitsch PetscCall(MatGetLocalSize(lJ, &n, NULL)); 8359566063dSJacob Faibussowitsch PetscCall(MatSetSizes(P, n, n, N, N)); 8369566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(P, dof)); 8379566063dSJacob Faibussowitsch PetscCall(MatSetUp(P)); 83848a46eb9SPierre Jolivet for (i = 0; i < nel; i++) PetscCall(MatSetValuesBlockedLocal(P, nen, e_loc + i * nen, nen, e_loc + i * nen, NULL, INSERT_VALUES)); 8399566063dSJacob Faibussowitsch PetscCall(MatPreallocatorPreallocate(P, (PetscBool)!da->prealloc_only, lJ)); 8409566063dSJacob Faibussowitsch PetscCall(MatISRestoreLocalMat(J, &lJ)); 8419566063dSJacob Faibussowitsch PetscCall(DMDARestoreElements(dm, &nel, &nen, &e_loc)); 8429566063dSJacob Faibussowitsch PetscCall(MatDestroy(&P)); 843e432b41dSStefano Zampini 8449566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 8459566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 846e306f867SJed Brown } 8473ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 848e584696dSStefano Zampini } 849e584696dSStefano Zampini 850d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPISELL(DM da, Mat J) 851d71ae5a4SJacob Faibussowitsch { 8525e26d47bSHong 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; 8535e26d47bSHong Zhang PetscInt lstart, lend, pstart, pend, *dnz, *onz; 8545e26d47bSHong Zhang MPI_Comm comm; 8555e26d47bSHong Zhang PetscScalar *values; 8565e26d47bSHong Zhang DMBoundaryType bx, by; 8575e26d47bSHong Zhang ISLocalToGlobalMapping ltog; 8585e26d47bSHong Zhang DMDAStencilType st; 8595e26d47bSHong Zhang 8605e26d47bSHong Zhang PetscFunctionBegin; 8615e26d47bSHong Zhang /* 8625e26d47bSHong Zhang nc - number of components per grid point 8635e26d47bSHong Zhang col - number of colors needed in one direction for single component problem 8645e26d47bSHong Zhang */ 8659566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, NULL, NULL, NULL, &nc, &s, &bx, &by, NULL, &st)); 8665e26d47bSHong Zhang col = 2 * s + 1; 8679566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 8689566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 8699566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 8705e26d47bSHong Zhang 8719566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * col * nc * nc, &cols)); 8729566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 8735e26d47bSHong Zhang 8749566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 8755e26d47bSHong Zhang /* determine the matrix preallocation information */ 876d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny, nc * nx * ny, dnz, onz); 8775e26d47bSHong Zhang for (i = xs; i < xs + nx; i++) { 8785e26d47bSHong Zhang pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 8795e26d47bSHong Zhang pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 8805e26d47bSHong Zhang 8815e26d47bSHong Zhang for (j = ys; j < ys + ny; j++) { 8825e26d47bSHong Zhang slot = i - gxs + gnx * (j - gys); 8835e26d47bSHong Zhang 8845e26d47bSHong Zhang lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 8855e26d47bSHong Zhang lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 8865e26d47bSHong Zhang 8875e26d47bSHong Zhang cnt = 0; 8885e26d47bSHong Zhang for (k = 0; k < nc; k++) { 8895e26d47bSHong Zhang for (l = lstart; l < lend + 1; l++) { 8905e26d47bSHong Zhang for (p = pstart; p < pend + 1; p++) { 8915e26d47bSHong Zhang if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star have either l = 0 or p = 0 */ 8925e26d47bSHong Zhang cols[cnt++] = k + nc * (slot + gnx * l + p); 8935e26d47bSHong Zhang } 8945e26d47bSHong Zhang } 8955e26d47bSHong Zhang } 8965e26d47bSHong Zhang rows[k] = k + nc * (slot); 8975e26d47bSHong Zhang } 8989566063dSJacob Faibussowitsch PetscCall(MatPreallocateSetLocal(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 8995e26d47bSHong Zhang } 9005e26d47bSHong Zhang } 9019566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 9029566063dSJacob Faibussowitsch PetscCall(MatSeqSELLSetPreallocation(J, 0, dnz)); 9039566063dSJacob Faibussowitsch PetscCall(MatMPISELLSetPreallocation(J, 0, dnz, 0, onz)); 904d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 9055e26d47bSHong Zhang 9069566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 9075e26d47bSHong Zhang 9085e26d47bSHong Zhang /* 9095e26d47bSHong Zhang For each node in the grid: we get the neighbors in the local (on processor ordering 9105e26d47bSHong Zhang that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 9115e26d47bSHong Zhang PETSc ordering. 9125e26d47bSHong Zhang */ 9135e26d47bSHong Zhang if (!da->prealloc_only) { 9149566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * nc * nc, &values)); 9155e26d47bSHong Zhang for (i = xs; i < xs + nx; i++) { 9165e26d47bSHong Zhang pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 9175e26d47bSHong Zhang pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 9185e26d47bSHong Zhang 9195e26d47bSHong Zhang for (j = ys; j < ys + ny; j++) { 9205e26d47bSHong Zhang slot = i - gxs + gnx * (j - gys); 9215e26d47bSHong Zhang 9225e26d47bSHong Zhang lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 9235e26d47bSHong Zhang lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 9245e26d47bSHong Zhang 9255e26d47bSHong Zhang cnt = 0; 9265e26d47bSHong Zhang for (k = 0; k < nc; k++) { 9275e26d47bSHong Zhang for (l = lstart; l < lend + 1; l++) { 9285e26d47bSHong Zhang for (p = pstart; p < pend + 1; p++) { 9295e26d47bSHong Zhang if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star have either l = 0 or p = 0 */ 9305e26d47bSHong Zhang cols[cnt++] = k + nc * (slot + gnx * l + p); 9315e26d47bSHong Zhang } 9325e26d47bSHong Zhang } 9335e26d47bSHong Zhang } 9345e26d47bSHong Zhang rows[k] = k + nc * (slot); 9355e26d47bSHong Zhang } 9369566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, values, INSERT_VALUES)); 9375e26d47bSHong Zhang } 9385e26d47bSHong Zhang } 9399566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 940e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 9419566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 9429566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 9439566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 9449566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 9459566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 9465e26d47bSHong Zhang } 9479566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 9483ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 9495e26d47bSHong Zhang } 9505e26d47bSHong Zhang 951d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPISELL(DM da, Mat J) 952d71ae5a4SJacob Faibussowitsch { 953711261dbSHong Zhang PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 954711261dbSHong Zhang PetscInt m, n, dim, s, *cols = NULL, k, nc, *rows = NULL, col, cnt, l, p, *dnz = NULL, *onz = NULL; 955711261dbSHong Zhang PetscInt istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk, M, N, P; 956711261dbSHong Zhang MPI_Comm comm; 957711261dbSHong Zhang PetscScalar *values; 958711261dbSHong Zhang DMBoundaryType bx, by, bz; 959711261dbSHong Zhang ISLocalToGlobalMapping ltog; 960711261dbSHong Zhang DMDAStencilType st; 961711261dbSHong Zhang 962711261dbSHong Zhang PetscFunctionBegin; 963711261dbSHong Zhang /* 964711261dbSHong Zhang nc - number of components per grid point 965711261dbSHong Zhang col - number of colors needed in one direction for single component problem 966711261dbSHong Zhang */ 9679566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &nc, &s, &bx, &by, &bz, &st)); 968711261dbSHong Zhang col = 2 * s + 1; 9699566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 9709566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 9719566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 972711261dbSHong Zhang 9739566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * col * col * nc * nc, &cols)); 9749566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 975711261dbSHong Zhang 9769566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 977711261dbSHong Zhang /* determine the matrix preallocation information */ 978d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny * nz, nc * nx * ny * nz, dnz, onz); 979711261dbSHong Zhang for (i = xs; i < xs + nx; i++) { 980711261dbSHong Zhang istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 981711261dbSHong Zhang iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 982711261dbSHong Zhang for (j = ys; j < ys + ny; j++) { 983711261dbSHong Zhang jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 984711261dbSHong Zhang jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 985711261dbSHong Zhang for (k = zs; k < zs + nz; k++) { 986711261dbSHong Zhang kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 987711261dbSHong Zhang kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 988711261dbSHong Zhang 989711261dbSHong Zhang slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 990711261dbSHong Zhang 991711261dbSHong Zhang cnt = 0; 992711261dbSHong Zhang for (l = 0; l < nc; l++) { 993711261dbSHong Zhang for (ii = istart; ii < iend + 1; ii++) { 994711261dbSHong Zhang for (jj = jstart; jj < jend + 1; jj++) { 995711261dbSHong Zhang for (kk = kstart; kk < kend + 1; kk++) { 996711261dbSHong Zhang if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 997711261dbSHong Zhang cols[cnt++] = l + nc * (slot + ii + gnx * jj + gnx * gny * kk); 998711261dbSHong Zhang } 999711261dbSHong Zhang } 1000711261dbSHong Zhang } 1001711261dbSHong Zhang } 1002711261dbSHong Zhang rows[l] = l + nc * (slot); 1003711261dbSHong Zhang } 10049566063dSJacob Faibussowitsch PetscCall(MatPreallocateSetLocal(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 1005711261dbSHong Zhang } 1006711261dbSHong Zhang } 1007711261dbSHong Zhang } 10089566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 10099566063dSJacob Faibussowitsch PetscCall(MatSeqSELLSetPreallocation(J, 0, dnz)); 10109566063dSJacob Faibussowitsch PetscCall(MatMPISELLSetPreallocation(J, 0, dnz, 0, onz)); 1011d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 10129566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 1013711261dbSHong Zhang 1014711261dbSHong Zhang /* 1015711261dbSHong Zhang For each node in the grid: we get the neighbors in the local (on processor ordering 1016711261dbSHong Zhang that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 1017711261dbSHong Zhang PETSc ordering. 1018711261dbSHong Zhang */ 1019711261dbSHong Zhang if (!da->prealloc_only) { 10209566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * col * nc * nc * nc, &values)); 1021711261dbSHong Zhang for (i = xs; i < xs + nx; i++) { 1022711261dbSHong Zhang istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1023711261dbSHong Zhang iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 1024711261dbSHong Zhang for (j = ys; j < ys + ny; j++) { 1025711261dbSHong Zhang jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1026711261dbSHong Zhang jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 1027711261dbSHong Zhang for (k = zs; k < zs + nz; k++) { 1028711261dbSHong Zhang kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 1029711261dbSHong Zhang kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 1030711261dbSHong Zhang 1031711261dbSHong Zhang slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 1032711261dbSHong Zhang 1033711261dbSHong Zhang cnt = 0; 1034711261dbSHong Zhang for (l = 0; l < nc; l++) { 1035711261dbSHong Zhang for (ii = istart; ii < iend + 1; ii++) { 1036711261dbSHong Zhang for (jj = jstart; jj < jend + 1; jj++) { 1037711261dbSHong Zhang for (kk = kstart; kk < kend + 1; kk++) { 1038711261dbSHong Zhang if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 1039711261dbSHong Zhang cols[cnt++] = l + nc * (slot + ii + gnx * jj + gnx * gny * kk); 1040711261dbSHong Zhang } 1041711261dbSHong Zhang } 1042711261dbSHong Zhang } 1043711261dbSHong Zhang } 1044711261dbSHong Zhang rows[l] = l + nc * (slot); 1045711261dbSHong Zhang } 10469566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, values, INSERT_VALUES)); 1047711261dbSHong Zhang } 1048711261dbSHong Zhang } 1049711261dbSHong Zhang } 10509566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 1051e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 10529566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 10539566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 10549566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 10559566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 10569566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 1057711261dbSHong Zhang } 10589566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 10593ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1060711261dbSHong Zhang } 1061711261dbSHong Zhang 1062d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPIAIJ(DM da, Mat J) 1063d71ae5a4SJacob Faibussowitsch { 1064c1154cd5SBarry 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; 106547c6ae99SBarry Smith PetscInt lstart, lend, pstart, pend, *dnz, *onz; 106647c6ae99SBarry Smith MPI_Comm comm; 1067bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 1068844bd0d7SStefano Zampini ISLocalToGlobalMapping ltog, mltog; 1069aa219208SBarry Smith DMDAStencilType st; 1070b294de21SRichard Tran Mills PetscBool removedups = PETSC_FALSE, alreadyboundtocpu = PETSC_TRUE; 107147c6ae99SBarry Smith 107247c6ae99SBarry Smith PetscFunctionBegin; 107347c6ae99SBarry Smith /* 107447c6ae99SBarry Smith nc - number of components per grid point 107547c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 107647c6ae99SBarry Smith */ 1077924e958dSJunchao Zhang PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, &M, &N, NULL, &nc, &s, &bx, &by, NULL, &st)); 10781baa6e33SBarry Smith if (bx == DM_BOUNDARY_NONE && by == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_TRUE)); 107947c6ae99SBarry Smith col = 2 * s + 1; 1080c1154cd5SBarry Smith /* 1081c1154cd5SBarry Smith With one processor in periodic domains in a skinny dimension the code will label nonzero columns multiple times 1082c1154cd5SBarry Smith because of "wrapping" around the end of the domain hitting an entry already counted in the other direction. 1083c1154cd5SBarry Smith */ 1084c1154cd5SBarry Smith if (M == 1 && 2 * s >= m) removedups = PETSC_TRUE; 1085c1154cd5SBarry Smith if (N == 1 && 2 * s >= n) removedups = PETSC_TRUE; 10869566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 10879566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 10889566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 108947c6ae99SBarry Smith 10909566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * col * nc * nc, &cols)); 10919566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 109247c6ae99SBarry Smith 10939566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 109447c6ae99SBarry Smith /* determine the matrix preallocation information */ 1095d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny, nc * nx * ny, dnz, onz); 109647c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1097bff4a2f0SMatthew G. Knepley pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1098bff4a2f0SMatthew G. Knepley pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 109947c6ae99SBarry Smith 110047c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 110147c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 110247c6ae99SBarry Smith 1103bff4a2f0SMatthew G. Knepley lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1104bff4a2f0SMatthew G. Knepley lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 110547c6ae99SBarry Smith 110647c6ae99SBarry Smith cnt = 0; 110747c6ae99SBarry Smith for (k = 0; k < nc; k++) { 110847c6ae99SBarry Smith for (l = lstart; l < lend + 1; l++) { 110947c6ae99SBarry Smith for (p = pstart; p < pend + 1; p++) { 1110aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star have either l = 0 or p = 0 */ 111147c6ae99SBarry Smith cols[cnt++] = k + nc * (slot + gnx * l + p); 111247c6ae99SBarry Smith } 111347c6ae99SBarry Smith } 111447c6ae99SBarry Smith } 111547c6ae99SBarry Smith rows[k] = k + nc * (slot); 111647c6ae99SBarry Smith } 11171baa6e33SBarry Smith if (removedups) PetscCall(MatPreallocateSetLocalRemoveDups(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 11181baa6e33SBarry Smith else PetscCall(MatPreallocateSetLocal(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 111947c6ae99SBarry Smith } 1120c1154cd5SBarry Smith } 11219566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 11229566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz)); 11239566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz)); 1124d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 11259566063dSJacob Faibussowitsch PetscCall(MatGetLocalToGlobalMapping(J, &mltog, NULL)); 11261baa6e33SBarry Smith if (!mltog) PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 112747c6ae99SBarry Smith 112847c6ae99SBarry Smith /* 112947c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 113047c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 113147c6ae99SBarry Smith PETSc ordering. 113247c6ae99SBarry Smith */ 1133fcfd50ebSBarry Smith if (!da->prealloc_only) { 113447c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1135bff4a2f0SMatthew G. Knepley pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1136bff4a2f0SMatthew G. Knepley pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 113747c6ae99SBarry Smith 113847c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 113947c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 114047c6ae99SBarry Smith 1141bff4a2f0SMatthew G. Knepley lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1142bff4a2f0SMatthew G. Knepley lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 114347c6ae99SBarry Smith 114447c6ae99SBarry Smith cnt = 0; 114547c6ae99SBarry Smith for (l = lstart; l < lend + 1; l++) { 114647c6ae99SBarry Smith for (p = pstart; p < pend + 1; p++) { 1147aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star have either l = 0 or p = 0 */ 1148071fcb05SBarry Smith cols[cnt++] = nc * (slot + gnx * l + p); 1149071fcb05SBarry Smith for (k = 1; k < nc; k++) { 11509371c9d4SSatish Balay cols[cnt] = 1 + cols[cnt - 1]; 11519371c9d4SSatish Balay cnt++; 115247c6ae99SBarry Smith } 115347c6ae99SBarry Smith } 115447c6ae99SBarry Smith } 115547c6ae99SBarry Smith } 1156071fcb05SBarry Smith for (k = 0; k < nc; k++) rows[k] = k + nc * (slot); 11579566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, NULL, INSERT_VALUES)); 115847c6ae99SBarry Smith } 115947c6ae99SBarry Smith } 1160e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 11619566063dSJacob Faibussowitsch PetscCall(MatBoundToCPU(J, &alreadyboundtocpu)); 11629566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 11639566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 11649566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 11659566063dSJacob Faibussowitsch if (!alreadyboundtocpu) PetscCall(MatBindToCPU(J, PETSC_FALSE)); 11669566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 11671baa6e33SBarry Smith if (bx == DM_BOUNDARY_NONE && by == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE)); 116847c6ae99SBarry Smith } 11699566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 11703ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 117147c6ae99SBarry Smith } 117247c6ae99SBarry Smith 1173d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPIAIJ_Fill(DM da, Mat J) 1174d71ae5a4SJacob Faibussowitsch { 117547c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 1176c1154cd5SBarry Smith PetscInt m, n, dim, s, *cols, k, nc, row, col, cnt, maxcnt = 0, l, p, M, N; 117747c6ae99SBarry Smith PetscInt lstart, lend, pstart, pend, *dnz, *onz; 117847c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 117947c6ae99SBarry Smith PetscInt ifill_col, *ofill = dd->ofill, *dfill = dd->dfill; 118047c6ae99SBarry Smith MPI_Comm comm; 1181bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 118245b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 1183aa219208SBarry Smith DMDAStencilType st; 1184c1154cd5SBarry Smith PetscBool removedups = PETSC_FALSE; 118547c6ae99SBarry Smith 118647c6ae99SBarry Smith PetscFunctionBegin; 118747c6ae99SBarry Smith /* 118847c6ae99SBarry Smith nc - number of components per grid point 118947c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 119047c6ae99SBarry Smith */ 1191924e958dSJunchao Zhang PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, &M, &N, NULL, &nc, &s, &bx, &by, NULL, &st)); 119247c6ae99SBarry Smith col = 2 * s + 1; 1193c1154cd5SBarry Smith /* 1194c1154cd5SBarry Smith With one processor in periodic domains in a skinny dimension the code will label nonzero columns multiple times 1195c1154cd5SBarry Smith because of "wrapping" around the end of the domain hitting an entry already counted in the other direction. 1196c1154cd5SBarry Smith */ 1197c1154cd5SBarry Smith if (M == 1 && 2 * s >= m) removedups = PETSC_TRUE; 1198c1154cd5SBarry Smith if (N == 1 && 2 * s >= n) removedups = PETSC_TRUE; 11999566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 12009566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 12019566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 120247c6ae99SBarry Smith 12039566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * nc, &cols)); 12049566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 120547c6ae99SBarry Smith 12069566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 120747c6ae99SBarry Smith /* determine the matrix preallocation information */ 1208d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny, nc * nx * ny, dnz, onz); 120947c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1210bff4a2f0SMatthew G. Knepley pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1211bff4a2f0SMatthew G. Knepley pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 121247c6ae99SBarry Smith 121347c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 121447c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 121547c6ae99SBarry Smith 1216bff4a2f0SMatthew G. Knepley lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1217bff4a2f0SMatthew G. Knepley lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 121847c6ae99SBarry Smith 121947c6ae99SBarry Smith for (k = 0; k < nc; k++) { 122047c6ae99SBarry Smith cnt = 0; 122147c6ae99SBarry Smith for (l = lstart; l < lend + 1; l++) { 122247c6ae99SBarry Smith for (p = pstart; p < pend + 1; p++) { 122347c6ae99SBarry Smith if (l || p) { 1224aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star */ 12258865f1eaSKarl Rupp for (ifill_col = ofill[k]; ifill_col < ofill[k + 1]; ifill_col++) cols[cnt++] = ofill[ifill_col] + nc * (slot + gnx * l + p); 122647c6ae99SBarry Smith } 122747c6ae99SBarry Smith } else { 122847c6ae99SBarry Smith if (dfill) { 12298865f1eaSKarl Rupp for (ifill_col = dfill[k]; ifill_col < dfill[k + 1]; ifill_col++) cols[cnt++] = dfill[ifill_col] + nc * (slot + gnx * l + p); 123047c6ae99SBarry Smith } else { 12318865f1eaSKarl Rupp for (ifill_col = 0; ifill_col < nc; ifill_col++) cols[cnt++] = ifill_col + nc * (slot + gnx * l + p); 123247c6ae99SBarry Smith } 123347c6ae99SBarry Smith } 123447c6ae99SBarry Smith } 123547c6ae99SBarry Smith } 123647c6ae99SBarry Smith row = k + nc * (slot); 1237c0ab637bSBarry Smith maxcnt = PetscMax(maxcnt, cnt); 12381baa6e33SBarry Smith if (removedups) PetscCall(MatPreallocateSetLocalRemoveDups(ltog, 1, &row, ltog, cnt, cols, dnz, onz)); 12391baa6e33SBarry Smith else PetscCall(MatPreallocateSetLocal(ltog, 1, &row, ltog, cnt, cols, dnz, onz)); 124047c6ae99SBarry Smith } 124147c6ae99SBarry Smith } 1242c1154cd5SBarry Smith } 12439566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz)); 12449566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz)); 1245d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 12469566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 124747c6ae99SBarry Smith 124847c6ae99SBarry Smith /* 124947c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 125047c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 125147c6ae99SBarry Smith PETSc ordering. 125247c6ae99SBarry Smith */ 1253fcfd50ebSBarry Smith if (!da->prealloc_only) { 125447c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1255bff4a2f0SMatthew G. Knepley pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1256bff4a2f0SMatthew G. Knepley pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 125747c6ae99SBarry Smith 125847c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 125947c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 126047c6ae99SBarry Smith 1261bff4a2f0SMatthew G. Knepley lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1262bff4a2f0SMatthew G. Knepley lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 126347c6ae99SBarry Smith 126447c6ae99SBarry Smith for (k = 0; k < nc; k++) { 126547c6ae99SBarry Smith cnt = 0; 126647c6ae99SBarry Smith for (l = lstart; l < lend + 1; l++) { 126747c6ae99SBarry Smith for (p = pstart; p < pend + 1; p++) { 126847c6ae99SBarry Smith if (l || p) { 1269aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star */ 12708865f1eaSKarl Rupp for (ifill_col = ofill[k]; ifill_col < ofill[k + 1]; ifill_col++) cols[cnt++] = ofill[ifill_col] + nc * (slot + gnx * l + p); 127147c6ae99SBarry Smith } 127247c6ae99SBarry Smith } else { 127347c6ae99SBarry Smith if (dfill) { 12748865f1eaSKarl Rupp for (ifill_col = dfill[k]; ifill_col < dfill[k + 1]; ifill_col++) cols[cnt++] = dfill[ifill_col] + nc * (slot + gnx * l + p); 127547c6ae99SBarry Smith } else { 12768865f1eaSKarl Rupp for (ifill_col = 0; ifill_col < nc; ifill_col++) cols[cnt++] = ifill_col + nc * (slot + gnx * l + p); 127747c6ae99SBarry Smith } 127847c6ae99SBarry Smith } 127947c6ae99SBarry Smith } 128047c6ae99SBarry Smith } 128147c6ae99SBarry Smith row = k + nc * (slot); 12829566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, 1, &row, cnt, cols, NULL, INSERT_VALUES)); 128347c6ae99SBarry Smith } 128447c6ae99SBarry Smith } 128547c6ae99SBarry Smith } 1286e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 12879566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 12889566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 12899566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 12909566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 12919566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 129247c6ae99SBarry Smith } 12939566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 12943ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 129547c6ae99SBarry Smith } 129647c6ae99SBarry Smith 129747c6ae99SBarry Smith /* ---------------------------------------------------------------------------------*/ 129847c6ae99SBarry Smith 1299d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPIAIJ(DM da, Mat J) 1300d71ae5a4SJacob Faibussowitsch { 130147c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 13020298fd71SBarry Smith PetscInt m, n, dim, s, *cols = NULL, k, nc, *rows = NULL, col, cnt, l, p, *dnz = NULL, *onz = NULL; 1303c1154cd5SBarry Smith PetscInt istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk, M, N, P; 130447c6ae99SBarry Smith MPI_Comm comm; 1305bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by, bz; 1306844bd0d7SStefano Zampini ISLocalToGlobalMapping ltog, mltog; 1307aa219208SBarry Smith DMDAStencilType st; 1308c1154cd5SBarry Smith PetscBool removedups = PETSC_FALSE; 130947c6ae99SBarry Smith 131047c6ae99SBarry Smith PetscFunctionBegin; 131147c6ae99SBarry Smith /* 131247c6ae99SBarry Smith nc - number of components per grid point 131347c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 131447c6ae99SBarry Smith */ 13159566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &nc, &s, &bx, &by, &bz, &st)); 131648a46eb9SPierre Jolivet if (bx == DM_BOUNDARY_NONE && by == DM_BOUNDARY_NONE && bz == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_TRUE)); 131747c6ae99SBarry Smith col = 2 * s + 1; 131847c6ae99SBarry Smith 1319c1154cd5SBarry Smith /* 1320c1154cd5SBarry Smith With one processor in periodic domains in a skinny dimension the code will label nonzero columns multiple times 1321c1154cd5SBarry Smith because of "wrapping" around the end of the domain hitting an entry already counted in the other direction. 1322c1154cd5SBarry Smith */ 1323c1154cd5SBarry Smith if (M == 1 && 2 * s >= m) removedups = PETSC_TRUE; 1324c1154cd5SBarry Smith if (N == 1 && 2 * s >= n) removedups = PETSC_TRUE; 1325c1154cd5SBarry Smith if (P == 1 && 2 * s >= p) removedups = PETSC_TRUE; 1326c1154cd5SBarry Smith 13279566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 13289566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 13299566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 133047c6ae99SBarry Smith 13319566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * col * col * nc * nc, &cols)); 13329566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 133347c6ae99SBarry Smith 13349566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 133547c6ae99SBarry Smith /* determine the matrix preallocation information */ 1336d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny * nz, nc * nx * ny * nz, dnz, onz); 133747c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1338bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1339bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 134047c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1341bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1342bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 134347c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 1344bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 1345bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 134647c6ae99SBarry Smith 134747c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 134847c6ae99SBarry Smith 134947c6ae99SBarry Smith cnt = 0; 135047c6ae99SBarry Smith for (l = 0; l < nc; l++) { 135147c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 135247c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 135347c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 1354aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 135547c6ae99SBarry Smith cols[cnt++] = l + nc * (slot + ii + gnx * jj + gnx * gny * kk); 135647c6ae99SBarry Smith } 135747c6ae99SBarry Smith } 135847c6ae99SBarry Smith } 135947c6ae99SBarry Smith } 136047c6ae99SBarry Smith rows[l] = l + nc * (slot); 136147c6ae99SBarry Smith } 13621baa6e33SBarry Smith if (removedups) PetscCall(MatPreallocateSetLocalRemoveDups(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 13631baa6e33SBarry Smith else PetscCall(MatPreallocateSetLocal(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 136447c6ae99SBarry Smith } 136547c6ae99SBarry Smith } 1366c1154cd5SBarry Smith } 13679566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 13689566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz)); 13699566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz)); 1370d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 13719566063dSJacob Faibussowitsch PetscCall(MatGetLocalToGlobalMapping(J, &mltog, NULL)); 137248a46eb9SPierre Jolivet if (!mltog) PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 137347c6ae99SBarry Smith 137447c6ae99SBarry Smith /* 137547c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 137647c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 137747c6ae99SBarry Smith PETSc ordering. 137847c6ae99SBarry Smith */ 1379fcfd50ebSBarry Smith if (!da->prealloc_only) { 138047c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1381bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1382bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 138347c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1384bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1385bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 138647c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 1387bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 1388bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 138947c6ae99SBarry Smith 139047c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 139147c6ae99SBarry Smith 139247c6ae99SBarry Smith cnt = 0; 139347c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 1394071fcb05SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 1395071fcb05SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 1396aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 1397071fcb05SBarry Smith cols[cnt++] = nc * (slot + ii + gnx * jj + gnx * gny * kk); 1398071fcb05SBarry Smith for (l = 1; l < nc; l++) { 13999371c9d4SSatish Balay cols[cnt] = 1 + cols[cnt - 1]; 14009371c9d4SSatish Balay cnt++; 140147c6ae99SBarry Smith } 140247c6ae99SBarry Smith } 140347c6ae99SBarry Smith } 140447c6ae99SBarry Smith } 140547c6ae99SBarry Smith } 14069371c9d4SSatish Balay rows[0] = nc * (slot); 14079371c9d4SSatish Balay for (l = 1; l < nc; l++) rows[l] = 1 + rows[l - 1]; 14089566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, NULL, INSERT_VALUES)); 140947c6ae99SBarry Smith } 141047c6ae99SBarry Smith } 141147c6ae99SBarry Smith } 1412e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 14139566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 14149566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 14159566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 141648a46eb9SPierre Jolivet if (bx == DM_BOUNDARY_NONE && by == DM_BOUNDARY_NONE && bz == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE)); 14179566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 14189566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 141947c6ae99SBarry Smith } 14209566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 14213ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 142247c6ae99SBarry Smith } 142347c6ae99SBarry Smith 142447c6ae99SBarry Smith /* ---------------------------------------------------------------------------------*/ 142547c6ae99SBarry Smith 1426d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_1d_MPIAIJ_Fill(DM da, Mat J) 1427d71ae5a4SJacob Faibussowitsch { 1428ce308e1dSBarry Smith DM_DA *dd = (DM_DA *)da->data; 1429ce308e1dSBarry Smith PetscInt xs, nx, i, j, gxs, gnx, row, k, l; 14308d4c968fSBarry Smith PetscInt m, dim, s, *cols = NULL, nc, cnt, maxcnt = 0, *ocols; 14310acb5bebSBarry Smith PetscInt *ofill = dd->ofill, *dfill = dd->dfill; 1432bff4a2f0SMatthew G. Knepley DMBoundaryType bx; 143345b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 1434ce308e1dSBarry Smith PetscMPIInt rank, size; 1435ce308e1dSBarry Smith 1436ce308e1dSBarry Smith PetscFunctionBegin; 14379566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)da), &rank)); 14389566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)da), &size)); 1439ce308e1dSBarry Smith 1440ce308e1dSBarry Smith /* 1441ce308e1dSBarry Smith nc - number of components per grid point 1442ce308e1dSBarry Smith */ 14439566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, NULL, NULL, NULL, NULL, NULL, &nc, &s, &bx, NULL, NULL, NULL)); 144408401ef6SPierre Jolivet PetscCheck(s <= 1, PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "Matrix creation for 1d not implemented correctly for stencil width larger than 1"); 14459566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &nx, NULL, NULL)); 14469566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, NULL, NULL, &gnx, NULL, NULL)); 1447ce308e1dSBarry Smith 14489566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 14499566063dSJacob Faibussowitsch PetscCall(PetscCalloc2(nx * nc, &cols, nx * nc, &ocols)); 1450ce308e1dSBarry Smith 1451ce308e1dSBarry Smith /* 1452ce308e1dSBarry Smith note should be smaller for first and last process with no periodic 1453ce308e1dSBarry Smith does not handle dfill 1454ce308e1dSBarry Smith */ 1455ce308e1dSBarry Smith cnt = 0; 1456ce308e1dSBarry Smith /* coupling with process to the left */ 1457ce308e1dSBarry Smith for (i = 0; i < s; i++) { 1458ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 1459dd400576SPatrick Sanan ocols[cnt] = ((rank == 0) ? 0 : (s - i) * (ofill[j + 1] - ofill[j])); 14600acb5bebSBarry Smith cols[cnt] = dfill[j + 1] - dfill[j] + (s + i) * (ofill[j + 1] - ofill[j]); 1461dd400576SPatrick Sanan if (rank == 0 && (dd->bx == DM_BOUNDARY_PERIODIC)) { 1462831644c1SBarry Smith if (size > 1) ocols[cnt] += (s - i) * (ofill[j + 1] - ofill[j]); 1463831644c1SBarry Smith else cols[cnt] += (s - i) * (ofill[j + 1] - ofill[j]); 1464831644c1SBarry Smith } 1465c0ab637bSBarry Smith maxcnt = PetscMax(maxcnt, ocols[cnt] + cols[cnt]); 1466ce308e1dSBarry Smith cnt++; 1467ce308e1dSBarry Smith } 1468ce308e1dSBarry Smith } 1469ce308e1dSBarry Smith for (i = s; i < nx - s; i++) { 1470ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 14710acb5bebSBarry Smith cols[cnt] = dfill[j + 1] - dfill[j] + 2 * s * (ofill[j + 1] - ofill[j]); 1472c0ab637bSBarry Smith maxcnt = PetscMax(maxcnt, ocols[cnt] + cols[cnt]); 1473ce308e1dSBarry Smith cnt++; 1474ce308e1dSBarry Smith } 1475ce308e1dSBarry Smith } 1476ce308e1dSBarry Smith /* coupling with process to the right */ 1477ce308e1dSBarry Smith for (i = nx - s; i < nx; i++) { 1478ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 1479ce308e1dSBarry Smith ocols[cnt] = ((rank == (size - 1)) ? 0 : (i - nx + s + 1) * (ofill[j + 1] - ofill[j])); 14800acb5bebSBarry Smith cols[cnt] = dfill[j + 1] - dfill[j] + (s + nx - i - 1) * (ofill[j + 1] - ofill[j]); 1481831644c1SBarry Smith if ((rank == size - 1) && (dd->bx == DM_BOUNDARY_PERIODIC)) { 1482831644c1SBarry Smith if (size > 1) ocols[cnt] += (i - nx + s + 1) * (ofill[j + 1] - ofill[j]); 1483831644c1SBarry Smith else cols[cnt] += (i - nx + s + 1) * (ofill[j + 1] - ofill[j]); 1484831644c1SBarry Smith } 1485c0ab637bSBarry Smith maxcnt = PetscMax(maxcnt, ocols[cnt] + cols[cnt]); 1486ce308e1dSBarry Smith cnt++; 1487ce308e1dSBarry Smith } 1488ce308e1dSBarry Smith } 1489ce308e1dSBarry Smith 14909566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, 0, cols)); 14919566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, 0, cols, 0, ocols)); 14929566063dSJacob Faibussowitsch PetscCall(PetscFree2(cols, ocols)); 1493ce308e1dSBarry Smith 14949566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 14959566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 1496ce308e1dSBarry Smith 1497ce308e1dSBarry Smith /* 1498ce308e1dSBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 1499ce308e1dSBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 1500ce308e1dSBarry Smith PETSc ordering. 1501ce308e1dSBarry Smith */ 1502ce308e1dSBarry Smith if (!da->prealloc_only) { 15039566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(maxcnt, &cols)); 1504ce308e1dSBarry Smith row = xs * nc; 1505ce308e1dSBarry Smith /* coupling with process to the left */ 1506ce308e1dSBarry Smith for (i = xs; i < xs + s; i++) { 1507ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 1508ce308e1dSBarry Smith cnt = 0; 1509ce308e1dSBarry Smith if (rank) { 1510ce308e1dSBarry Smith for (l = 0; l < s; l++) { 1511ce308e1dSBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i - s + l) * nc + ofill[k]; 1512ce308e1dSBarry Smith } 1513ce308e1dSBarry Smith } 1514dd400576SPatrick Sanan if (rank == 0 && (dd->bx == DM_BOUNDARY_PERIODIC)) { 1515831644c1SBarry Smith for (l = 0; l < s; l++) { 1516831644c1SBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (m + i - s - l) * nc + ofill[k]; 1517831644c1SBarry Smith } 1518831644c1SBarry Smith } 15190acb5bebSBarry Smith if (dfill) { 1520ad540459SPierre Jolivet for (k = dfill[j]; k < dfill[j + 1]; k++) cols[cnt++] = i * nc + dfill[k]; 15210acb5bebSBarry Smith } else { 1522ad540459SPierre Jolivet for (k = 0; k < nc; k++) cols[cnt++] = i * nc + k; 15230acb5bebSBarry Smith } 1524ce308e1dSBarry Smith for (l = 0; l < s; l++) { 1525ce308e1dSBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i + s - l) * nc + ofill[k]; 1526ce308e1dSBarry Smith } 15279566063dSJacob Faibussowitsch PetscCall(MatSetValues(J, 1, &row, cnt, cols, NULL, INSERT_VALUES)); 1528ce308e1dSBarry Smith row++; 1529ce308e1dSBarry Smith } 1530ce308e1dSBarry Smith } 1531ce308e1dSBarry Smith for (i = xs + s; i < xs + nx - s; i++) { 1532ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 1533ce308e1dSBarry Smith cnt = 0; 1534ce308e1dSBarry Smith for (l = 0; l < s; l++) { 1535ce308e1dSBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i - s + l) * nc + ofill[k]; 1536ce308e1dSBarry Smith } 15370acb5bebSBarry Smith if (dfill) { 1538ad540459SPierre Jolivet for (k = dfill[j]; k < dfill[j + 1]; k++) cols[cnt++] = i * nc + dfill[k]; 15390acb5bebSBarry Smith } else { 1540ad540459SPierre Jolivet for (k = 0; k < nc; k++) cols[cnt++] = i * nc + k; 15410acb5bebSBarry Smith } 1542ce308e1dSBarry Smith for (l = 0; l < s; l++) { 1543ce308e1dSBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i + s - l) * nc + ofill[k]; 1544ce308e1dSBarry Smith } 15459566063dSJacob Faibussowitsch PetscCall(MatSetValues(J, 1, &row, cnt, cols, NULL, INSERT_VALUES)); 1546ce308e1dSBarry Smith row++; 1547ce308e1dSBarry Smith } 1548ce308e1dSBarry Smith } 1549ce308e1dSBarry Smith /* coupling with process to the right */ 1550ce308e1dSBarry Smith for (i = xs + nx - s; i < xs + nx; i++) { 1551ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 1552ce308e1dSBarry Smith cnt = 0; 1553ce308e1dSBarry Smith for (l = 0; l < s; l++) { 1554ce308e1dSBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i - s + l) * nc + ofill[k]; 1555ce308e1dSBarry Smith } 15560acb5bebSBarry Smith if (dfill) { 1557ad540459SPierre Jolivet for (k = dfill[j]; k < dfill[j + 1]; k++) cols[cnt++] = i * nc + dfill[k]; 15580acb5bebSBarry Smith } else { 1559ad540459SPierre Jolivet for (k = 0; k < nc; k++) cols[cnt++] = i * nc + k; 15600acb5bebSBarry Smith } 1561ce308e1dSBarry Smith if (rank < size - 1) { 1562ce308e1dSBarry Smith for (l = 0; l < s; l++) { 1563ce308e1dSBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i + s - l) * nc + ofill[k]; 1564ce308e1dSBarry Smith } 1565ce308e1dSBarry Smith } 1566831644c1SBarry Smith if ((rank == size - 1) && (dd->bx == DM_BOUNDARY_PERIODIC)) { 1567831644c1SBarry Smith for (l = 0; l < s; l++) { 1568831644c1SBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i - s - l - m + 2) * nc + ofill[k]; 1569831644c1SBarry Smith } 1570831644c1SBarry Smith } 15719566063dSJacob Faibussowitsch PetscCall(MatSetValues(J, 1, &row, cnt, cols, NULL, INSERT_VALUES)); 1572ce308e1dSBarry Smith row++; 1573ce308e1dSBarry Smith } 1574ce308e1dSBarry Smith } 15759566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 1576e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 15779566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 15789566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 15799566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 15809566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 15819566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 1582ce308e1dSBarry Smith } 15833ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1584ce308e1dSBarry Smith } 1585ce308e1dSBarry Smith 1586ce308e1dSBarry Smith /* ---------------------------------------------------------------------------------*/ 1587ce308e1dSBarry Smith 1588d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_1d_MPIAIJ(DM da, Mat J) 1589d71ae5a4SJacob Faibussowitsch { 159047c6ae99SBarry Smith PetscInt xs, nx, i, i1, slot, gxs, gnx; 15910298fd71SBarry Smith PetscInt m, dim, s, *cols = NULL, nc, *rows = NULL, col, cnt, l; 159247c6ae99SBarry Smith PetscInt istart, iend; 1593bff4a2f0SMatthew G. Knepley DMBoundaryType bx; 1594844bd0d7SStefano Zampini ISLocalToGlobalMapping ltog, mltog; 159547c6ae99SBarry Smith 159647c6ae99SBarry Smith PetscFunctionBegin; 159747c6ae99SBarry Smith /* 159847c6ae99SBarry Smith nc - number of components per grid point 159947c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 160047c6ae99SBarry Smith */ 16019566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, NULL, NULL, NULL, NULL, NULL, &nc, &s, &bx, NULL, NULL, NULL)); 160248a46eb9SPierre Jolivet if (bx == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_TRUE)); 160347c6ae99SBarry Smith col = 2 * s + 1; 160447c6ae99SBarry Smith 16059566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &nx, NULL, NULL)); 16069566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, NULL, NULL, &gnx, NULL, NULL)); 160747c6ae99SBarry Smith 16089566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 16099566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, col * nc, NULL)); 16109566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, col * nc, NULL, col * nc, NULL)); 161147c6ae99SBarry Smith 16129566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 16139566063dSJacob Faibussowitsch PetscCall(MatGetLocalToGlobalMapping(J, &mltog, NULL)); 161448a46eb9SPierre Jolivet if (!mltog) PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 161547c6ae99SBarry Smith 161647c6ae99SBarry Smith /* 161747c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 161847c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 161947c6ae99SBarry Smith PETSc ordering. 162047c6ae99SBarry Smith */ 1621fcfd50ebSBarry Smith if (!da->prealloc_only) { 16229566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * nc * nc, &cols)); 162347c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 162447c6ae99SBarry Smith istart = PetscMax(-s, gxs - i); 162547c6ae99SBarry Smith iend = PetscMin(s, gxs + gnx - i - 1); 162647c6ae99SBarry Smith slot = i - gxs; 162747c6ae99SBarry Smith 162847c6ae99SBarry Smith cnt = 0; 162947c6ae99SBarry Smith for (i1 = istart; i1 < iend + 1; i1++) { 1630071fcb05SBarry Smith cols[cnt++] = nc * (slot + i1); 1631071fcb05SBarry Smith for (l = 1; l < nc; l++) { 16329371c9d4SSatish Balay cols[cnt] = 1 + cols[cnt - 1]; 16339371c9d4SSatish Balay cnt++; 163447c6ae99SBarry Smith } 163547c6ae99SBarry Smith } 16369371c9d4SSatish Balay rows[0] = nc * (slot); 16379371c9d4SSatish Balay for (l = 1; l < nc; l++) rows[l] = 1 + rows[l - 1]; 16389566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, NULL, INSERT_VALUES)); 163947c6ae99SBarry Smith } 1640e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 16419566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 16429566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 16439566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 164448a46eb9SPierre Jolivet if (bx == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE)); 16459566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 16469566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 16479566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 1648ce308e1dSBarry Smith } 16493ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 165047c6ae99SBarry Smith } 165147c6ae99SBarry Smith 165219b08ed1SBarry Smith /* ---------------------------------------------------------------------------------*/ 165319b08ed1SBarry Smith 1654d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_1d_SeqAIJ_NoPreallocation(DM da, Mat J) 1655d71ae5a4SJacob Faibussowitsch { 165619b08ed1SBarry Smith PetscInt xs, nx, i, i1, slot, gxs, gnx; 165719b08ed1SBarry Smith PetscInt m, dim, s, *cols = NULL, nc, *rows = NULL, col, cnt, l; 165819b08ed1SBarry Smith PetscInt istart, iend; 165919b08ed1SBarry Smith DMBoundaryType bx; 166019b08ed1SBarry Smith ISLocalToGlobalMapping ltog, mltog; 166119b08ed1SBarry Smith 166219b08ed1SBarry Smith PetscFunctionBegin; 166319b08ed1SBarry Smith /* 166419b08ed1SBarry Smith nc - number of components per grid point 166519b08ed1SBarry Smith col - number of colors needed in one direction for single component problem 166619b08ed1SBarry Smith */ 16679566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, NULL, NULL, NULL, NULL, NULL, &nc, &s, &bx, NULL, NULL, NULL)); 166819b08ed1SBarry Smith col = 2 * s + 1; 166919b08ed1SBarry Smith 16709566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &nx, NULL, NULL)); 16719566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, NULL, NULL, &gnx, NULL, NULL)); 167219b08ed1SBarry Smith 16739566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 16749566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetTotalPreallocation(J, nx * nc * col * nc)); 167519b08ed1SBarry Smith 16769566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 16779566063dSJacob Faibussowitsch PetscCall(MatGetLocalToGlobalMapping(J, &mltog, NULL)); 167848a46eb9SPierre Jolivet if (!mltog) PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 167919b08ed1SBarry Smith 168019b08ed1SBarry Smith /* 168119b08ed1SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 168219b08ed1SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 168319b08ed1SBarry Smith PETSc ordering. 168419b08ed1SBarry Smith */ 168519b08ed1SBarry Smith if (!da->prealloc_only) { 16869566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * nc * nc, &cols)); 168719b08ed1SBarry Smith for (i = xs; i < xs + nx; i++) { 168819b08ed1SBarry Smith istart = PetscMax(-s, gxs - i); 168919b08ed1SBarry Smith iend = PetscMin(s, gxs + gnx - i - 1); 169019b08ed1SBarry Smith slot = i - gxs; 169119b08ed1SBarry Smith 169219b08ed1SBarry Smith cnt = 0; 169319b08ed1SBarry Smith for (i1 = istart; i1 < iend + 1; i1++) { 169419b08ed1SBarry Smith cols[cnt++] = nc * (slot + i1); 169519b08ed1SBarry Smith for (l = 1; l < nc; l++) { 16969371c9d4SSatish Balay cols[cnt] = 1 + cols[cnt - 1]; 16979371c9d4SSatish Balay cnt++; 169819b08ed1SBarry Smith } 169919b08ed1SBarry Smith } 17009371c9d4SSatish Balay rows[0] = nc * (slot); 17019371c9d4SSatish Balay for (l = 1; l < nc; l++) rows[l] = 1 + rows[l - 1]; 17029566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, NULL, INSERT_VALUES)); 170319b08ed1SBarry Smith } 170419b08ed1SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 17059566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 17069566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 17079566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 170848a46eb9SPierre Jolivet if (bx == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE)); 17099566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 17109566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 17119566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 171219b08ed1SBarry Smith } 17139566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE)); 17143ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 171519b08ed1SBarry Smith } 171619b08ed1SBarry Smith 1717d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPIBAIJ(DM da, Mat J) 1718d71ae5a4SJacob Faibussowitsch { 171947c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 172047c6ae99SBarry Smith PetscInt m, n, dim, s, *cols, nc, col, cnt, *dnz, *onz; 172147c6ae99SBarry Smith PetscInt istart, iend, jstart, jend, ii, jj; 172247c6ae99SBarry Smith MPI_Comm comm; 172347c6ae99SBarry Smith PetscScalar *values; 1724bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 1725aa219208SBarry Smith DMDAStencilType st; 172645b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 172747c6ae99SBarry Smith 172847c6ae99SBarry Smith PetscFunctionBegin; 172947c6ae99SBarry Smith /* 173047c6ae99SBarry Smith nc - number of components per grid point 173147c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 173247c6ae99SBarry Smith */ 17339566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, NULL, NULL, NULL, &nc, &s, &bx, &by, NULL, &st)); 173447c6ae99SBarry Smith col = 2 * s + 1; 173547c6ae99SBarry Smith 17369566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 17379566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 17389566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 173947c6ae99SBarry Smith 17409566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * nc * nc, &cols)); 174147c6ae99SBarry Smith 17429566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 174347c6ae99SBarry Smith 174447c6ae99SBarry Smith /* determine the matrix preallocation information */ 1745d0609cedSBarry Smith MatPreallocateBegin(comm, nx * ny, nx * ny, dnz, onz); 174647c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1747bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1748bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 174947c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1750bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1751bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 175247c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 175347c6ae99SBarry Smith 175447c6ae99SBarry Smith /* Find block columns in block row */ 175547c6ae99SBarry Smith cnt = 0; 175647c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 175747c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 1758aa219208SBarry Smith if (st == DMDA_STENCIL_BOX || !ii || !jj) { /* BOX or on the STAR */ 175947c6ae99SBarry Smith cols[cnt++] = slot + ii + gnx * jj; 176047c6ae99SBarry Smith } 176147c6ae99SBarry Smith } 176247c6ae99SBarry Smith } 17639566063dSJacob Faibussowitsch PetscCall(MatPreallocateSetLocalBlock(ltog, 1, &slot, ltog, cnt, cols, dnz, onz)); 176447c6ae99SBarry Smith } 176547c6ae99SBarry Smith } 17669566063dSJacob Faibussowitsch PetscCall(MatSeqBAIJSetPreallocation(J, nc, 0, dnz)); 17679566063dSJacob Faibussowitsch PetscCall(MatMPIBAIJSetPreallocation(J, nc, 0, dnz, 0, onz)); 1768d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 176947c6ae99SBarry Smith 17709566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 177147c6ae99SBarry Smith 177247c6ae99SBarry Smith /* 177347c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 177447c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 177547c6ae99SBarry Smith PETSc ordering. 177647c6ae99SBarry Smith */ 1777fcfd50ebSBarry Smith if (!da->prealloc_only) { 17789566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * nc * nc, &values)); 177947c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1780bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1781bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 178247c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1783bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1784bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 178547c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 178647c6ae99SBarry Smith cnt = 0; 178747c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 178847c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 1789aa219208SBarry Smith if (st == DMDA_STENCIL_BOX || !ii || !jj) { /* BOX or on the STAR */ 179047c6ae99SBarry Smith cols[cnt++] = slot + ii + gnx * jj; 179147c6ae99SBarry Smith } 179247c6ae99SBarry Smith } 179347c6ae99SBarry Smith } 17949566063dSJacob Faibussowitsch PetscCall(MatSetValuesBlockedLocal(J, 1, &slot, cnt, cols, values, INSERT_VALUES)); 179547c6ae99SBarry Smith } 179647c6ae99SBarry Smith } 17979566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 1798e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 17999566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 18009566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 18019566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 18029566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 18039566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 180447c6ae99SBarry Smith } 18059566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 18063ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 180747c6ae99SBarry Smith } 180847c6ae99SBarry Smith 1809d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPIBAIJ(DM da, Mat J) 1810d71ae5a4SJacob Faibussowitsch { 181147c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 181247c6ae99SBarry Smith PetscInt m, n, dim, s, *cols, k, nc, col, cnt, p, *dnz, *onz; 181347c6ae99SBarry Smith PetscInt istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk; 181447c6ae99SBarry Smith MPI_Comm comm; 181547c6ae99SBarry Smith PetscScalar *values; 1816bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by, bz; 1817aa219208SBarry Smith DMDAStencilType st; 181845b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 181947c6ae99SBarry Smith 182047c6ae99SBarry Smith PetscFunctionBegin; 182147c6ae99SBarry Smith /* 182247c6ae99SBarry Smith nc - number of components per grid point 182347c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 182447c6ae99SBarry Smith */ 18259566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, NULL, NULL, NULL, &nc, &s, &bx, &by, &bz, &st)); 182647c6ae99SBarry Smith col = 2 * s + 1; 182747c6ae99SBarry Smith 18289566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 18299566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 18309566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 183147c6ae99SBarry Smith 18329566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * col, &cols)); 183347c6ae99SBarry Smith 18349566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 183547c6ae99SBarry Smith 183647c6ae99SBarry Smith /* determine the matrix preallocation information */ 1837d0609cedSBarry Smith MatPreallocateBegin(comm, nx * ny * nz, nx * ny * nz, dnz, onz); 183847c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1839bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1840bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 184147c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1842bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1843bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 184447c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 1845bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 1846bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 184747c6ae99SBarry Smith 184847c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 184947c6ae99SBarry Smith 185047c6ae99SBarry Smith /* Find block columns in block row */ 185147c6ae99SBarry Smith cnt = 0; 185247c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 185347c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 185447c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 1855aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 185647c6ae99SBarry Smith cols[cnt++] = slot + ii + gnx * jj + gnx * gny * kk; 185747c6ae99SBarry Smith } 185847c6ae99SBarry Smith } 185947c6ae99SBarry Smith } 186047c6ae99SBarry Smith } 18619566063dSJacob Faibussowitsch PetscCall(MatPreallocateSetLocalBlock(ltog, 1, &slot, ltog, cnt, cols, dnz, onz)); 186247c6ae99SBarry Smith } 186347c6ae99SBarry Smith } 186447c6ae99SBarry Smith } 18659566063dSJacob Faibussowitsch PetscCall(MatSeqBAIJSetPreallocation(J, nc, 0, dnz)); 18669566063dSJacob Faibussowitsch PetscCall(MatMPIBAIJSetPreallocation(J, nc, 0, dnz, 0, onz)); 1867d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 186847c6ae99SBarry Smith 18699566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 187047c6ae99SBarry Smith 187147c6ae99SBarry Smith /* 187247c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 187347c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 187447c6ae99SBarry Smith PETSc ordering. 187547c6ae99SBarry Smith */ 1876fcfd50ebSBarry Smith if (!da->prealloc_only) { 18779566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * col * nc * nc, &values)); 187847c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1879bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1880bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 188147c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1882bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1883bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 188447c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 1885bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 1886bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 188747c6ae99SBarry Smith 188847c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 188947c6ae99SBarry Smith 189047c6ae99SBarry Smith cnt = 0; 189147c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 189247c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 189347c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 1894aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 189547c6ae99SBarry Smith cols[cnt++] = slot + ii + gnx * jj + gnx * gny * kk; 189647c6ae99SBarry Smith } 189747c6ae99SBarry Smith } 189847c6ae99SBarry Smith } 189947c6ae99SBarry Smith } 19009566063dSJacob Faibussowitsch PetscCall(MatSetValuesBlockedLocal(J, 1, &slot, cnt, cols, values, INSERT_VALUES)); 190147c6ae99SBarry Smith } 190247c6ae99SBarry Smith } 190347c6ae99SBarry Smith } 19049566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 1905e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 19069566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 19079566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 19089566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 19099566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 19109566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 191147c6ae99SBarry Smith } 19129566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 19133ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 191447c6ae99SBarry Smith } 191547c6ae99SBarry Smith 191647c6ae99SBarry Smith /* 191747c6ae99SBarry Smith This helper is for of SBAIJ preallocation, to discard the lower-triangular values which are difficult to 191847c6ae99SBarry Smith identify in the local ordering with periodic domain. 191947c6ae99SBarry Smith */ 1920d71ae5a4SJacob Faibussowitsch static PetscErrorCode L2GFilterUpperTriangular(ISLocalToGlobalMapping ltog, PetscInt *row, PetscInt *cnt, PetscInt col[]) 1921d71ae5a4SJacob Faibussowitsch { 192247c6ae99SBarry Smith PetscInt i, n; 192347c6ae99SBarry Smith 192447c6ae99SBarry Smith PetscFunctionBegin; 19259566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingApplyBlock(ltog, 1, row, row)); 19269566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingApplyBlock(ltog, *cnt, col, col)); 192747c6ae99SBarry Smith for (i = 0, n = 0; i < *cnt; i++) { 192847c6ae99SBarry Smith if (col[i] >= *row) col[n++] = col[i]; 192947c6ae99SBarry Smith } 193047c6ae99SBarry Smith *cnt = n; 19313ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 193247c6ae99SBarry Smith } 193347c6ae99SBarry Smith 1934d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPISBAIJ(DM da, Mat J) 1935d71ae5a4SJacob Faibussowitsch { 193647c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 193747c6ae99SBarry Smith PetscInt m, n, dim, s, *cols, nc, col, cnt, *dnz, *onz; 193847c6ae99SBarry Smith PetscInt istart, iend, jstart, jend, ii, jj; 193947c6ae99SBarry Smith MPI_Comm comm; 194047c6ae99SBarry Smith PetscScalar *values; 1941bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 1942aa219208SBarry Smith DMDAStencilType st; 194345b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 194447c6ae99SBarry Smith 194547c6ae99SBarry Smith PetscFunctionBegin; 194647c6ae99SBarry Smith /* 194747c6ae99SBarry Smith nc - number of components per grid point 194847c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 194947c6ae99SBarry Smith */ 19509566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, NULL, NULL, NULL, &nc, &s, &bx, &by, NULL, &st)); 195147c6ae99SBarry Smith col = 2 * s + 1; 195247c6ae99SBarry Smith 19539566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 19549566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 19559566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 195647c6ae99SBarry Smith 19579566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * nc * nc, &cols)); 195847c6ae99SBarry Smith 19599566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 196047c6ae99SBarry Smith 196147c6ae99SBarry Smith /* determine the matrix preallocation information */ 1962d0609cedSBarry Smith MatPreallocateBegin(comm, nx * ny, nx * ny, dnz, onz); 196347c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1964bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1965bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 196647c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1967bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1968bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 196947c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 197047c6ae99SBarry Smith 197147c6ae99SBarry Smith /* Find block columns in block row */ 197247c6ae99SBarry Smith cnt = 0; 197347c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 197447c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 1975ad540459SPierre Jolivet if (st == DMDA_STENCIL_BOX || !ii || !jj) cols[cnt++] = slot + ii + gnx * jj; 197647c6ae99SBarry Smith } 197747c6ae99SBarry Smith } 19789566063dSJacob Faibussowitsch PetscCall(L2GFilterUpperTriangular(ltog, &slot, &cnt, cols)); 19799566063dSJacob Faibussowitsch PetscCall(MatPreallocateSymmetricSetBlock(slot, cnt, cols, dnz, onz)); 198047c6ae99SBarry Smith } 198147c6ae99SBarry Smith } 19829566063dSJacob Faibussowitsch PetscCall(MatSeqSBAIJSetPreallocation(J, nc, 0, dnz)); 19839566063dSJacob Faibussowitsch PetscCall(MatMPISBAIJSetPreallocation(J, nc, 0, dnz, 0, onz)); 1984d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 198547c6ae99SBarry Smith 19869566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 198747c6ae99SBarry Smith 198847c6ae99SBarry Smith /* 198947c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 199047c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 199147c6ae99SBarry Smith PETSc ordering. 199247c6ae99SBarry Smith */ 1993fcfd50ebSBarry Smith if (!da->prealloc_only) { 19949566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * nc * nc, &values)); 199547c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1996bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1997bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 199847c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1999bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 2000bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 200147c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 200247c6ae99SBarry Smith 200347c6ae99SBarry Smith /* Find block columns in block row */ 200447c6ae99SBarry Smith cnt = 0; 200547c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 200647c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 2007ad540459SPierre Jolivet if (st == DMDA_STENCIL_BOX || !ii || !jj) cols[cnt++] = slot + ii + gnx * jj; 200847c6ae99SBarry Smith } 200947c6ae99SBarry Smith } 20109566063dSJacob Faibussowitsch PetscCall(L2GFilterUpperTriangular(ltog, &slot, &cnt, cols)); 20119566063dSJacob Faibussowitsch PetscCall(MatSetValuesBlocked(J, 1, &slot, cnt, cols, values, INSERT_VALUES)); 201247c6ae99SBarry Smith } 201347c6ae99SBarry Smith } 20149566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 2015e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 20169566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 20179566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 20189566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 20199566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 20209566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 202147c6ae99SBarry Smith } 20229566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 20233ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 202447c6ae99SBarry Smith } 202547c6ae99SBarry Smith 2026d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPISBAIJ(DM da, Mat J) 2027d71ae5a4SJacob Faibussowitsch { 202847c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 202947c6ae99SBarry Smith PetscInt m, n, dim, s, *cols, k, nc, col, cnt, p, *dnz, *onz; 203047c6ae99SBarry Smith PetscInt istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk; 203147c6ae99SBarry Smith MPI_Comm comm; 203247c6ae99SBarry Smith PetscScalar *values; 2033bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by, bz; 2034aa219208SBarry Smith DMDAStencilType st; 203545b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 203647c6ae99SBarry Smith 203747c6ae99SBarry Smith PetscFunctionBegin; 203847c6ae99SBarry Smith /* 203947c6ae99SBarry Smith nc - number of components per grid point 204047c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 204147c6ae99SBarry Smith */ 20429566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, NULL, NULL, NULL, &nc, &s, &bx, &by, &bz, &st)); 204347c6ae99SBarry Smith col = 2 * s + 1; 204447c6ae99SBarry Smith 20459566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 20469566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 20479566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 204847c6ae99SBarry Smith 204947c6ae99SBarry Smith /* create the matrix */ 20509566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * col, &cols)); 205147c6ae99SBarry Smith 20529566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 205347c6ae99SBarry Smith 205447c6ae99SBarry Smith /* determine the matrix preallocation information */ 2055d0609cedSBarry Smith MatPreallocateBegin(comm, nx * ny * nz, nx * ny * nz, dnz, onz); 205647c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 2057bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 2058bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 205947c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 2060bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 2061bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 206247c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 2063bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 2064bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 206547c6ae99SBarry Smith 206647c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 206747c6ae99SBarry Smith 206847c6ae99SBarry Smith /* Find block columns in block row */ 206947c6ae99SBarry Smith cnt = 0; 207047c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 207147c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 207247c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 2073ad540459SPierre Jolivet if ((st == DMDA_STENCIL_BOX) || (!ii && !jj) || (!jj && !kk) || (!ii && !kk)) cols[cnt++] = slot + ii + gnx * jj + gnx * gny * kk; 207447c6ae99SBarry Smith } 207547c6ae99SBarry Smith } 207647c6ae99SBarry Smith } 20779566063dSJacob Faibussowitsch PetscCall(L2GFilterUpperTriangular(ltog, &slot, &cnt, cols)); 20789566063dSJacob Faibussowitsch PetscCall(MatPreallocateSymmetricSetBlock(slot, cnt, cols, dnz, onz)); 207947c6ae99SBarry Smith } 208047c6ae99SBarry Smith } 208147c6ae99SBarry Smith } 20829566063dSJacob Faibussowitsch PetscCall(MatSeqSBAIJSetPreallocation(J, nc, 0, dnz)); 20839566063dSJacob Faibussowitsch PetscCall(MatMPISBAIJSetPreallocation(J, nc, 0, dnz, 0, onz)); 2084d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 208547c6ae99SBarry Smith 20869566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 208747c6ae99SBarry Smith 208847c6ae99SBarry Smith /* 208947c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 209047c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 209147c6ae99SBarry Smith PETSc ordering. 209247c6ae99SBarry Smith */ 2093fcfd50ebSBarry Smith if (!da->prealloc_only) { 20949566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * col * nc * nc, &values)); 209547c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 2096bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 2097bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 209847c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 2099bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 2100bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 210147c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 2102bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 2103bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 210447c6ae99SBarry Smith 210547c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 210647c6ae99SBarry Smith 210747c6ae99SBarry Smith cnt = 0; 210847c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 210947c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 211047c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 2111ad540459SPierre Jolivet if ((st == DMDA_STENCIL_BOX) || (!ii && !jj) || (!jj && !kk) || (!ii && !kk)) cols[cnt++] = slot + ii + gnx * jj + gnx * gny * kk; 211247c6ae99SBarry Smith } 211347c6ae99SBarry Smith } 211447c6ae99SBarry Smith } 21159566063dSJacob Faibussowitsch PetscCall(L2GFilterUpperTriangular(ltog, &slot, &cnt, cols)); 21169566063dSJacob Faibussowitsch PetscCall(MatSetValuesBlocked(J, 1, &slot, cnt, cols, values, INSERT_VALUES)); 211747c6ae99SBarry Smith } 211847c6ae99SBarry Smith } 211947c6ae99SBarry Smith } 21209566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 2121e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 21229566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 21239566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 21249566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 21259566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 21269566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 212747c6ae99SBarry Smith } 21289566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 21293ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 213047c6ae99SBarry Smith } 213147c6ae99SBarry Smith 213247c6ae99SBarry Smith /* ---------------------------------------------------------------------------------*/ 213347c6ae99SBarry Smith 2134d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPIAIJ_Fill(DM da, Mat J) 2135d71ae5a4SJacob Faibussowitsch { 213647c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 2137c0ab637bSBarry Smith PetscInt m, n, dim, s, *cols, k, nc, row, col, cnt, maxcnt = 0, l, p, *dnz, *onz; 2138c1154cd5SBarry Smith PetscInt istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk, M, N, P; 213947c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 214047c6ae99SBarry Smith PetscInt ifill_col, *dfill = dd->dfill, *ofill = dd->ofill; 214147c6ae99SBarry Smith MPI_Comm comm; 214247c6ae99SBarry Smith PetscScalar *values; 2143bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by, bz; 214445b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 2145aa219208SBarry Smith DMDAStencilType st; 2146c1154cd5SBarry Smith PetscBool removedups = PETSC_FALSE; 214747c6ae99SBarry Smith 214847c6ae99SBarry Smith PetscFunctionBegin; 214947c6ae99SBarry Smith /* 215047c6ae99SBarry Smith nc - number of components per grid point 215147c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 215247c6ae99SBarry Smith */ 21539566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &nc, &s, &bx, &by, &bz, &st)); 215447c6ae99SBarry Smith col = 2 * s + 1; 215547c6ae99SBarry Smith 2156c1154cd5SBarry Smith /* 2157c1154cd5SBarry Smith With one processor in periodic domains in a skinny dimension the code will label nonzero columns multiple times 2158c1154cd5SBarry Smith because of "wrapping" around the end of the domain hitting an entry already counted in the other direction. 2159c1154cd5SBarry Smith */ 2160c1154cd5SBarry Smith if (M == 1 && 2 * s >= m) removedups = PETSC_TRUE; 2161c1154cd5SBarry Smith if (N == 1 && 2 * s >= n) removedups = PETSC_TRUE; 2162c1154cd5SBarry Smith if (P == 1 && 2 * s >= p) removedups = PETSC_TRUE; 2163c1154cd5SBarry Smith 21649566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 21659566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 21669566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 216747c6ae99SBarry Smith 21689566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * col * nc, &cols)); 21699566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 217047c6ae99SBarry Smith 217147c6ae99SBarry Smith /* determine the matrix preallocation information */ 2172d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny * nz, nc * nx * ny * nz, dnz, onz); 217347c6ae99SBarry Smith 21749566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 217547c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 2176bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 2177bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 217847c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 2179bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 2180bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 218147c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 2182bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 2183bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 218447c6ae99SBarry Smith 218547c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 218647c6ae99SBarry Smith 218747c6ae99SBarry Smith for (l = 0; l < nc; l++) { 218847c6ae99SBarry Smith cnt = 0; 218947c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 219047c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 219147c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 219247c6ae99SBarry Smith if (ii || jj || kk) { 2193aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 21948865f1eaSKarl 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); 219547c6ae99SBarry Smith } 219647c6ae99SBarry Smith } else { 219747c6ae99SBarry Smith if (dfill) { 21988865f1eaSKarl 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); 219947c6ae99SBarry Smith } else { 22008865f1eaSKarl Rupp for (ifill_col = 0; ifill_col < nc; ifill_col++) cols[cnt++] = ifill_col + nc * (slot + ii + gnx * jj + gnx * gny * kk); 220147c6ae99SBarry Smith } 220247c6ae99SBarry Smith } 220347c6ae99SBarry Smith } 220447c6ae99SBarry Smith } 220547c6ae99SBarry Smith } 220647c6ae99SBarry Smith row = l + nc * (slot); 2207c0ab637bSBarry Smith maxcnt = PetscMax(maxcnt, cnt); 22081baa6e33SBarry Smith if (removedups) PetscCall(MatPreallocateSetLocalRemoveDups(ltog, 1, &row, ltog, cnt, cols, dnz, onz)); 22091baa6e33SBarry Smith else PetscCall(MatPreallocateSetLocal(ltog, 1, &row, ltog, cnt, cols, dnz, onz)); 221047c6ae99SBarry Smith } 221147c6ae99SBarry Smith } 221247c6ae99SBarry Smith } 2213c1154cd5SBarry Smith } 22149566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz)); 22159566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz)); 2216d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 22179566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 221847c6ae99SBarry Smith 221947c6ae99SBarry Smith /* 222047c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 222147c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 222247c6ae99SBarry Smith PETSc ordering. 222347c6ae99SBarry Smith */ 2224fcfd50ebSBarry Smith if (!da->prealloc_only) { 22259566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(maxcnt, &values)); 222647c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 2227bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 2228bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 222947c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 2230bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 2231bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 223247c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 2233bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 2234bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 223547c6ae99SBarry Smith 223647c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 223747c6ae99SBarry Smith 223847c6ae99SBarry Smith for (l = 0; l < nc; l++) { 223947c6ae99SBarry Smith cnt = 0; 224047c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 224147c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 224247c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 224347c6ae99SBarry Smith if (ii || jj || kk) { 2244aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 22458865f1eaSKarl 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); 224647c6ae99SBarry Smith } 224747c6ae99SBarry Smith } else { 224847c6ae99SBarry Smith if (dfill) { 22498865f1eaSKarl 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); 225047c6ae99SBarry Smith } else { 22518865f1eaSKarl Rupp for (ifill_col = 0; ifill_col < nc; ifill_col++) cols[cnt++] = ifill_col + nc * (slot + ii + gnx * jj + gnx * gny * kk); 225247c6ae99SBarry Smith } 225347c6ae99SBarry Smith } 225447c6ae99SBarry Smith } 225547c6ae99SBarry Smith } 225647c6ae99SBarry Smith } 225747c6ae99SBarry Smith row = l + nc * (slot); 22589566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, 1, &row, cnt, cols, values, INSERT_VALUES)); 225947c6ae99SBarry Smith } 226047c6ae99SBarry Smith } 226147c6ae99SBarry Smith } 226247c6ae99SBarry Smith } 22639566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 2264e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 22659566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 22669566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 22679566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 22689566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 22699566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 227047c6ae99SBarry Smith } 22719566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 22723ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 227347c6ae99SBarry Smith } 2274