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; 22647c6ae99SBarry Smith } else if (dim > 1) { 227bff4a2f0SMatthew G. Knepley if ((m == 1 && bx == DM_BOUNDARY_PERIODIC) || (n == 1 && by == DM_BOUNDARY_PERIODIC) || (p == 1 && bz == DM_BOUNDARY_PERIODIC)) { 2285bdb020cSBarry Smith SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "IS_COLORING_LOCAL cannot be used for periodic boundary condition having both ends of the domain on the same process"); 22947c6ae99SBarry Smith } 23047c6ae99SBarry Smith } 23147c6ae99SBarry Smith } 23247c6ae99SBarry Smith 233aa219208SBarry Smith /* Tell the DMDA it has 1 degree of freedom per grid point so that the coloring for BAIJ 23447c6ae99SBarry Smith matrices is for the blocks, not the individual matrix elements */ 2359566063dSJacob Faibussowitsch PetscCall(PetscStrbeginswith(da->mattype, MATBAIJ, &isBAIJ)); 2369566063dSJacob Faibussowitsch if (!isBAIJ) PetscCall(PetscStrbeginswith(da->mattype, MATMPIBAIJ, &isBAIJ)); 2379566063dSJacob Faibussowitsch if (!isBAIJ) PetscCall(PetscStrbeginswith(da->mattype, MATSEQBAIJ, &isBAIJ)); 23847c6ae99SBarry Smith if (isBAIJ) { 23947c6ae99SBarry Smith dd->w = 1; 24047c6ae99SBarry Smith dd->xs = dd->xs / nc; 24147c6ae99SBarry Smith dd->xe = dd->xe / nc; 24247c6ae99SBarry Smith dd->Xs = dd->Xs / nc; 24347c6ae99SBarry Smith dd->Xe = dd->Xe / nc; 24447c6ae99SBarry Smith } 24547c6ae99SBarry Smith 24647c6ae99SBarry Smith /* 247aa219208SBarry Smith We do not provide a getcoloring function in the DMDA operations because 2489a1b256bSStefano Zampini the basic DMDA does not know about matrices. We think of DMDA as being 24947c6ae99SBarry Smith more low-level then matrices. 25047c6ae99SBarry Smith */ 2511baa6e33SBarry Smith if (dim == 1) PetscCall(DMCreateColoring_DA_1d_MPIAIJ(da, ctype, coloring)); 2521baa6e33SBarry Smith else if (dim == 2) PetscCall(DMCreateColoring_DA_2d_MPIAIJ(da, ctype, coloring)); 2531baa6e33SBarry Smith else if (dim == 3) PetscCall(DMCreateColoring_DA_3d_MPIAIJ(da, ctype, coloring)); 2541baa6e33SBarry Smith else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "Not done for %" PetscInt_FMT " dimension, send us mail petsc-maint@mcs.anl.gov for code", dim); 25547c6ae99SBarry Smith if (isBAIJ) { 25647c6ae99SBarry Smith dd->w = nc; 25747c6ae99SBarry Smith dd->xs = dd->xs * nc; 25847c6ae99SBarry Smith dd->xe = dd->xe * nc; 25947c6ae99SBarry Smith dd->Xs = dd->Xs * nc; 26047c6ae99SBarry Smith dd->Xe = dd->Xe * nc; 26147c6ae99SBarry Smith } 2623ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 26347c6ae99SBarry Smith } 26447c6ae99SBarry Smith 26547c6ae99SBarry Smith /* ---------------------------------------------------------------------------------*/ 26647c6ae99SBarry Smith 267d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA_2d_MPIAIJ(DM da, ISColoringType ctype, ISColoring *coloring) 268d71ae5a4SJacob Faibussowitsch { 26947c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, ii, gxs, gys, gnx, gny, m, n, M, N, dim, s, k, nc, col; 270dec0b466SHong Zhang PetscInt ncolors = 0; 27147c6ae99SBarry Smith MPI_Comm comm; 272bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 273aa219208SBarry Smith DMDAStencilType st; 27447c6ae99SBarry Smith ISColoringValue *colors; 27547c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 27647c6ae99SBarry Smith 27747c6ae99SBarry Smith PetscFunctionBegin; 27847c6ae99SBarry Smith /* 27947c6ae99SBarry Smith nc - number of components per grid point 28047c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 28147c6ae99SBarry Smith 28247c6ae99SBarry Smith */ 2839566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, &M, &N, NULL, &nc, &s, &bx, &by, NULL, &st)); 28447c6ae99SBarry Smith col = 2 * s + 1; 2859566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 2869566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 2879566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 28847c6ae99SBarry Smith 28947c6ae99SBarry Smith /* special case as taught to us by Paul Hovland */ 290aa219208SBarry Smith if (st == DMDA_STENCIL_STAR && s == 1) { 2919566063dSJacob Faibussowitsch PetscCall(DMCreateColoring_DA_2d_5pt_MPIAIJ(da, ctype, coloring)); 29247c6ae99SBarry Smith } else { 29347c6ae99SBarry Smith if (ctype == IS_COLORING_GLOBAL) { 29447c6ae99SBarry Smith if (!dd->localcoloring) { 2959566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * nx * ny, &colors)); 29647c6ae99SBarry Smith ii = 0; 29747c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 29847c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 299ad540459SPierre Jolivet for (k = 0; k < nc; k++) colors[ii++] = k + nc * ((i % col) + col * (j % col)); 30047c6ae99SBarry Smith } 30147c6ae99SBarry Smith } 30247c6ae99SBarry Smith ncolors = nc + nc * (col - 1 + col * (col - 1)); 3039566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * nx * ny, colors, PETSC_OWN_POINTER, &dd->localcoloring)); 30447c6ae99SBarry Smith } 30547c6ae99SBarry Smith *coloring = dd->localcoloring; 3065bdb020cSBarry Smith } else if (ctype == IS_COLORING_LOCAL) { 30747c6ae99SBarry Smith if (!dd->ghostedcoloring) { 3089566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * gnx * gny, &colors)); 30947c6ae99SBarry Smith ii = 0; 31047c6ae99SBarry Smith for (j = gys; j < gys + gny; j++) { 31147c6ae99SBarry Smith for (i = gxs; i < gxs + gnx; i++) { 31247c6ae99SBarry Smith for (k = 0; k < nc; k++) { 31347c6ae99SBarry Smith /* the complicated stuff is to handle periodic boundaries */ 31447c6ae99SBarry Smith colors[ii++] = k + nc * ((SetInRange(i, m) % col) + col * (SetInRange(j, n) % col)); 31547c6ae99SBarry Smith } 31647c6ae99SBarry Smith } 31747c6ae99SBarry Smith } 31847c6ae99SBarry Smith ncolors = nc + nc * (col - 1 + col * (col - 1)); 3199566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * gnx * gny, colors, PETSC_OWN_POINTER, &dd->ghostedcoloring)); 32047c6ae99SBarry Smith /* PetscIntView(ncolors,(PetscInt*)colors,0); */ 32147c6ae99SBarry Smith 3229566063dSJacob Faibussowitsch PetscCall(ISColoringSetType(dd->ghostedcoloring, IS_COLORING_LOCAL)); 32347c6ae99SBarry Smith } 32447c6ae99SBarry Smith *coloring = dd->ghostedcoloring; 32598921bdaSJacob Faibussowitsch } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_ARG_WRONG, "Unknown ISColoringType %d", (int)ctype); 32647c6ae99SBarry Smith } 3279566063dSJacob Faibussowitsch PetscCall(ISColoringReference(*coloring)); 3283ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 32947c6ae99SBarry Smith } 33047c6ae99SBarry Smith 33147c6ae99SBarry Smith /* ---------------------------------------------------------------------------------*/ 33247c6ae99SBarry Smith 333d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA_3d_MPIAIJ(DM da, ISColoringType ctype, ISColoring *coloring) 334d71ae5a4SJacob Faibussowitsch { 33547c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, gxs, gys, gnx, gny, m, n, p, dim, s, k, nc, col, zs, gzs, ii, l, nz, gnz, M, N, P; 33647c6ae99SBarry Smith PetscInt ncolors; 33747c6ae99SBarry Smith MPI_Comm comm; 338bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by, bz; 339aa219208SBarry Smith DMDAStencilType st; 34047c6ae99SBarry Smith ISColoringValue *colors; 34147c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 34247c6ae99SBarry Smith 34347c6ae99SBarry Smith PetscFunctionBegin; 34447c6ae99SBarry Smith /* 34547c6ae99SBarry Smith nc - number of components per grid point 34647c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 34747c6ae99SBarry Smith */ 3489566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &nc, &s, &bx, &by, &bz, &st)); 34947c6ae99SBarry Smith col = 2 * s + 1; 3509566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 3519566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 3529566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 35347c6ae99SBarry Smith 35447c6ae99SBarry Smith /* create the coloring */ 35547c6ae99SBarry Smith if (ctype == IS_COLORING_GLOBAL) { 35647c6ae99SBarry Smith if (!dd->localcoloring) { 3579566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * nx * ny * nz, &colors)); 35847c6ae99SBarry Smith ii = 0; 35947c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 36047c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 36147c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 362ad540459SPierre Jolivet for (l = 0; l < nc; l++) colors[ii++] = l + nc * ((i % col) + col * (j % col) + col * col * (k % col)); 36347c6ae99SBarry Smith } 36447c6ae99SBarry Smith } 36547c6ae99SBarry Smith } 36647c6ae99SBarry Smith ncolors = nc + nc * (col - 1 + col * (col - 1) + col * col * (col - 1)); 3679566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * nx * ny * nz, colors, PETSC_OWN_POINTER, &dd->localcoloring)); 36847c6ae99SBarry Smith } 36947c6ae99SBarry Smith *coloring = dd->localcoloring; 3705bdb020cSBarry Smith } else if (ctype == IS_COLORING_LOCAL) { 37147c6ae99SBarry Smith if (!dd->ghostedcoloring) { 3729566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * gnx * gny * gnz, &colors)); 37347c6ae99SBarry Smith ii = 0; 37447c6ae99SBarry Smith for (k = gzs; k < gzs + gnz; k++) { 37547c6ae99SBarry Smith for (j = gys; j < gys + gny; j++) { 37647c6ae99SBarry Smith for (i = gxs; i < gxs + gnx; i++) { 37747c6ae99SBarry Smith for (l = 0; l < nc; l++) { 37847c6ae99SBarry Smith /* the complicated stuff is to handle periodic boundaries */ 37947c6ae99SBarry Smith colors[ii++] = l + nc * ((SetInRange(i, m) % col) + col * (SetInRange(j, n) % col) + col * col * (SetInRange(k, p) % col)); 38047c6ae99SBarry Smith } 38147c6ae99SBarry Smith } 38247c6ae99SBarry Smith } 38347c6ae99SBarry Smith } 38447c6ae99SBarry Smith ncolors = nc + nc * (col - 1 + col * (col - 1) + col * col * (col - 1)); 3859566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * gnx * gny * gnz, colors, PETSC_OWN_POINTER, &dd->ghostedcoloring)); 3869566063dSJacob Faibussowitsch PetscCall(ISColoringSetType(dd->ghostedcoloring, IS_COLORING_LOCAL)); 38747c6ae99SBarry Smith } 38847c6ae99SBarry Smith *coloring = dd->ghostedcoloring; 38998921bdaSJacob Faibussowitsch } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_ARG_WRONG, "Unknown ISColoringType %d", (int)ctype); 3909566063dSJacob Faibussowitsch PetscCall(ISColoringReference(*coloring)); 3913ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 39247c6ae99SBarry Smith } 39347c6ae99SBarry Smith 39447c6ae99SBarry Smith /* ---------------------------------------------------------------------------------*/ 39547c6ae99SBarry Smith 396d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA_1d_MPIAIJ(DM da, ISColoringType ctype, ISColoring *coloring) 397d71ae5a4SJacob Faibussowitsch { 39847c6ae99SBarry Smith PetscInt xs, nx, i, i1, gxs, gnx, l, m, M, dim, s, nc, col; 39947c6ae99SBarry Smith PetscInt ncolors; 40047c6ae99SBarry Smith MPI_Comm comm; 401bff4a2f0SMatthew G. Knepley DMBoundaryType bx; 40247c6ae99SBarry Smith ISColoringValue *colors; 40347c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 40447c6ae99SBarry Smith 40547c6ae99SBarry Smith PetscFunctionBegin; 40647c6ae99SBarry Smith /* 40747c6ae99SBarry Smith nc - number of components per grid point 40847c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 40947c6ae99SBarry Smith */ 4109566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, NULL, NULL, &M, NULL, NULL, &nc, &s, &bx, NULL, NULL, NULL)); 41147c6ae99SBarry Smith col = 2 * s + 1; 4129566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &nx, NULL, NULL)); 4139566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, NULL, NULL, &gnx, NULL, NULL)); 4149566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 41547c6ae99SBarry Smith 41647c6ae99SBarry Smith /* create the coloring */ 41747c6ae99SBarry Smith if (ctype == IS_COLORING_GLOBAL) { 41847c6ae99SBarry Smith if (!dd->localcoloring) { 4199566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * nx, &colors)); 420ae4f298aSBarry Smith if (dd->ofillcols) { 421ae4f298aSBarry Smith PetscInt tc = 0; 422ae4f298aSBarry Smith for (i = 0; i < nc; i++) tc += (PetscInt)(dd->ofillcols[i] > 0); 423ae4f298aSBarry Smith i1 = 0; 424ae4f298aSBarry Smith for (i = xs; i < xs + nx; i++) { 425ae4f298aSBarry Smith for (l = 0; l < nc; l++) { 426ae4f298aSBarry Smith if (dd->ofillcols[l] && (i % col)) { 427ae4f298aSBarry Smith colors[i1++] = nc - 1 + tc * ((i % col) - 1) + dd->ofillcols[l]; 428ae4f298aSBarry Smith } else { 429ae4f298aSBarry Smith colors[i1++] = l; 430ae4f298aSBarry Smith } 431ae4f298aSBarry Smith } 432ae4f298aSBarry Smith } 433ae4f298aSBarry Smith ncolors = nc + 2 * s * tc; 434ae4f298aSBarry Smith } else { 43547c6ae99SBarry Smith i1 = 0; 43647c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 437ad540459SPierre Jolivet for (l = 0; l < nc; l++) colors[i1++] = l + nc * (i % col); 43847c6ae99SBarry Smith } 43947c6ae99SBarry Smith ncolors = nc + nc * (col - 1); 440ae4f298aSBarry Smith } 4419566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * nx, colors, PETSC_OWN_POINTER, &dd->localcoloring)); 44247c6ae99SBarry Smith } 44347c6ae99SBarry Smith *coloring = dd->localcoloring; 4445bdb020cSBarry Smith } else if (ctype == IS_COLORING_LOCAL) { 44547c6ae99SBarry Smith if (!dd->ghostedcoloring) { 4469566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * gnx, &colors)); 44747c6ae99SBarry Smith i1 = 0; 44847c6ae99SBarry Smith for (i = gxs; i < gxs + gnx; i++) { 44947c6ae99SBarry Smith for (l = 0; l < nc; l++) { 45047c6ae99SBarry Smith /* the complicated stuff is to handle periodic boundaries */ 45147c6ae99SBarry Smith colors[i1++] = l + nc * (SetInRange(i, m) % col); 45247c6ae99SBarry Smith } 45347c6ae99SBarry Smith } 45447c6ae99SBarry Smith ncolors = nc + nc * (col - 1); 4559566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * gnx, colors, PETSC_OWN_POINTER, &dd->ghostedcoloring)); 4569566063dSJacob Faibussowitsch PetscCall(ISColoringSetType(dd->ghostedcoloring, IS_COLORING_LOCAL)); 45747c6ae99SBarry Smith } 45847c6ae99SBarry Smith *coloring = dd->ghostedcoloring; 45998921bdaSJacob Faibussowitsch } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_ARG_WRONG, "Unknown ISColoringType %d", (int)ctype); 4609566063dSJacob Faibussowitsch PetscCall(ISColoringReference(*coloring)); 4613ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 46247c6ae99SBarry Smith } 46347c6ae99SBarry Smith 464d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring_DA_2d_5pt_MPIAIJ(DM da, ISColoringType ctype, ISColoring *coloring) 465d71ae5a4SJacob Faibussowitsch { 46647c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, ii, gxs, gys, gnx, gny, m, n, dim, s, k, nc; 46747c6ae99SBarry Smith PetscInt ncolors; 46847c6ae99SBarry Smith MPI_Comm comm; 469bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 47047c6ae99SBarry Smith ISColoringValue *colors; 47147c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 47247c6ae99SBarry Smith 47347c6ae99SBarry Smith PetscFunctionBegin; 47447c6ae99SBarry Smith /* 47547c6ae99SBarry Smith nc - number of components per grid point 47647c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 47747c6ae99SBarry Smith */ 4789566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, NULL, NULL, NULL, &nc, &s, &bx, &by, NULL, NULL)); 4799566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 4809566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 4819566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 48247c6ae99SBarry Smith /* create the coloring */ 48347c6ae99SBarry Smith if (ctype == IS_COLORING_GLOBAL) { 48447c6ae99SBarry Smith if (!dd->localcoloring) { 4859566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * nx * ny, &colors)); 48647c6ae99SBarry Smith ii = 0; 48747c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 48847c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 489ad540459SPierre Jolivet for (k = 0; k < nc; k++) colors[ii++] = k + nc * ((3 * j + i) % 5); 49047c6ae99SBarry Smith } 49147c6ae99SBarry Smith } 49247c6ae99SBarry Smith ncolors = 5 * nc; 4939566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * nx * ny, colors, PETSC_OWN_POINTER, &dd->localcoloring)); 49447c6ae99SBarry Smith } 49547c6ae99SBarry Smith *coloring = dd->localcoloring; 4965bdb020cSBarry Smith } else if (ctype == IS_COLORING_LOCAL) { 49747c6ae99SBarry Smith if (!dd->ghostedcoloring) { 4989566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nc * gnx * gny, &colors)); 49947c6ae99SBarry Smith ii = 0; 50047c6ae99SBarry Smith for (j = gys; j < gys + gny; j++) { 50147c6ae99SBarry Smith for (i = gxs; i < gxs + gnx; i++) { 502ad540459SPierre Jolivet for (k = 0; k < nc; k++) colors[ii++] = k + nc * ((3 * SetInRange(j, n) + SetInRange(i, m)) % 5); 50347c6ae99SBarry Smith } 50447c6ae99SBarry Smith } 50547c6ae99SBarry Smith ncolors = 5 * nc; 5069566063dSJacob Faibussowitsch PetscCall(ISColoringCreate(comm, ncolors, nc * gnx * gny, colors, PETSC_OWN_POINTER, &dd->ghostedcoloring)); 5079566063dSJacob Faibussowitsch PetscCall(ISColoringSetType(dd->ghostedcoloring, IS_COLORING_LOCAL)); 50847c6ae99SBarry Smith } 50947c6ae99SBarry Smith *coloring = dd->ghostedcoloring; 51098921bdaSJacob Faibussowitsch } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_ARG_WRONG, "Unknown ISColoringType %d", (int)ctype); 5113ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 51247c6ae99SBarry Smith } 51347c6ae99SBarry Smith 51447c6ae99SBarry Smith /* =========================================================================== */ 515e432b41dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_1d_MPIAIJ(DM, Mat); 516ce308e1dSBarry Smith extern PetscErrorCode DMCreateMatrix_DA_1d_MPIAIJ_Fill(DM, Mat); 517e432b41dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_1d_SeqAIJ_NoPreallocation(DM, Mat); 518e432b41dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_2d_MPIAIJ(DM, Mat); 519950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_2d_MPIAIJ_Fill(DM, Mat); 520e432b41dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_3d_MPIAIJ(DM, Mat); 521950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_3d_MPIAIJ_Fill(DM, Mat); 522950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_2d_MPIBAIJ(DM, Mat); 523950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_3d_MPIBAIJ(DM, Mat); 524950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_2d_MPISBAIJ(DM, Mat); 525950540a4SJed Brown extern PetscErrorCode DMCreateMatrix_DA_3d_MPISBAIJ(DM, Mat); 526d4002b98SHong Zhang extern PetscErrorCode DMCreateMatrix_DA_2d_MPISELL(DM, Mat); 527d4002b98SHong Zhang extern PetscErrorCode DMCreateMatrix_DA_3d_MPISELL(DM, Mat); 528e584696dSStefano Zampini extern PetscErrorCode DMCreateMatrix_DA_IS(DM, Mat); 52947c6ae99SBarry Smith 5308bbdbebaSMatthew G Knepley /*@C 531dce8aebaSBarry Smith MatSetupDM - Sets the `DMDA` that is to be used by the HYPRE_StructMatrix PETSc matrix 53247c6ae99SBarry Smith 533d083f849SBarry Smith Logically Collective on mat 53447c6ae99SBarry Smith 53547c6ae99SBarry Smith Input Parameters: 53647c6ae99SBarry Smith + mat - the matrix 53747c6ae99SBarry Smith - da - the da 53847c6ae99SBarry Smith 53947c6ae99SBarry Smith Level: intermediate 54047c6ae99SBarry Smith 541dce8aebaSBarry Smith .seealso: `Mat`, `MatSetUp()` 54247c6ae99SBarry Smith @*/ 543d71ae5a4SJacob Faibussowitsch PetscErrorCode MatSetupDM(Mat mat, DM da) 544d71ae5a4SJacob Faibussowitsch { 54547c6ae99SBarry Smith PetscFunctionBegin; 54647c6ae99SBarry Smith PetscValidHeaderSpecific(mat, MAT_CLASSID, 1); 547064a246eSJacob Faibussowitsch PetscValidHeaderSpecificType(da, DM_CLASSID, 2, DMDA); 548cac4c232SBarry Smith PetscTryMethod(mat, "MatSetupDM_C", (Mat, DM), (mat, da)); 5493ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 55047c6ae99SBarry Smith } 55147c6ae99SBarry Smith 552d71ae5a4SJacob Faibussowitsch PetscErrorCode MatView_MPI_DA(Mat A, PetscViewer viewer) 553d71ae5a4SJacob Faibussowitsch { 5549a42bb27SBarry Smith DM da; 55547c6ae99SBarry Smith const char *prefix; 55647c6ae99SBarry Smith Mat Anatural; 55747c6ae99SBarry Smith AO ao; 55847c6ae99SBarry Smith PetscInt rstart, rend, *petsc, i; 55947c6ae99SBarry Smith IS is; 56047c6ae99SBarry Smith MPI_Comm comm; 56174388724SJed Brown PetscViewerFormat format; 56247c6ae99SBarry Smith 56347c6ae99SBarry Smith PetscFunctionBegin; 56474388724SJed Brown /* Check whether we are just printing info, in which case MatView() already viewed everything we wanted to view */ 5659566063dSJacob Faibussowitsch PetscCall(PetscViewerGetFormat(viewer, &format)); 5663ba16761SJacob Faibussowitsch if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) PetscFunctionReturn(PETSC_SUCCESS); 56774388724SJed Brown 5689566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)A, &comm)); 5699566063dSJacob Faibussowitsch PetscCall(MatGetDM(A, &da)); 5707a8be351SBarry Smith PetscCheck(da, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Matrix not generated from a DMDA"); 57147c6ae99SBarry Smith 5729566063dSJacob Faibussowitsch PetscCall(DMDAGetAO(da, &ao)); 5739566063dSJacob Faibussowitsch PetscCall(MatGetOwnershipRange(A, &rstart, &rend)); 5749566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(rend - rstart, &petsc)); 57547c6ae99SBarry Smith for (i = rstart; i < rend; i++) petsc[i - rstart] = i; 5769566063dSJacob Faibussowitsch PetscCall(AOApplicationToPetsc(ao, rend - rstart, petsc)); 5779566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(comm, rend - rstart, petsc, PETSC_OWN_POINTER, &is)); 57847c6ae99SBarry Smith 57947c6ae99SBarry Smith /* call viewer on natural ordering */ 5809566063dSJacob Faibussowitsch PetscCall(MatCreateSubMatrix(A, is, is, MAT_INITIAL_MATRIX, &Anatural)); 5819566063dSJacob Faibussowitsch PetscCall(ISDestroy(&is)); 5829566063dSJacob Faibussowitsch PetscCall(PetscObjectGetOptionsPrefix((PetscObject)A, &prefix)); 5839566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptionsPrefix((PetscObject)Anatural, prefix)); 5849566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)Anatural, ((PetscObject)A)->name)); 585f0ed2f47SStefano Zampini ((PetscObject)Anatural)->donotPetscObjectPrintClassNamePrefixType = PETSC_TRUE; 5869566063dSJacob Faibussowitsch PetscCall(MatView(Anatural, viewer)); 587f0ed2f47SStefano Zampini ((PetscObject)Anatural)->donotPetscObjectPrintClassNamePrefixType = PETSC_FALSE; 5889566063dSJacob Faibussowitsch PetscCall(MatDestroy(&Anatural)); 5893ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 59047c6ae99SBarry Smith } 59147c6ae99SBarry Smith 592d71ae5a4SJacob Faibussowitsch PetscErrorCode MatLoad_MPI_DA(Mat A, PetscViewer viewer) 593d71ae5a4SJacob Faibussowitsch { 5949a42bb27SBarry Smith DM da; 59547c6ae99SBarry Smith Mat Anatural, Aapp; 59647c6ae99SBarry Smith AO ao; 597539c167fSBarry Smith PetscInt rstart, rend, *app, i, m, n, M, N; 59847c6ae99SBarry Smith IS is; 59947c6ae99SBarry Smith MPI_Comm comm; 60047c6ae99SBarry Smith 60147c6ae99SBarry Smith PetscFunctionBegin; 6029566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)A, &comm)); 6039566063dSJacob Faibussowitsch PetscCall(MatGetDM(A, &da)); 6047a8be351SBarry Smith PetscCheck(da, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Matrix not generated from a DMDA"); 60547c6ae99SBarry Smith 60647c6ae99SBarry Smith /* Load the matrix in natural ordering */ 6079566063dSJacob Faibussowitsch PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &Anatural)); 6089566063dSJacob Faibussowitsch PetscCall(MatSetType(Anatural, ((PetscObject)A)->type_name)); 6099566063dSJacob Faibussowitsch PetscCall(MatGetSize(A, &M, &N)); 6109566063dSJacob Faibussowitsch PetscCall(MatGetLocalSize(A, &m, &n)); 6119566063dSJacob Faibussowitsch PetscCall(MatSetSizes(Anatural, m, n, M, N)); 6129566063dSJacob Faibussowitsch PetscCall(MatLoad(Anatural, viewer)); 61347c6ae99SBarry Smith 61447c6ae99SBarry Smith /* Map natural ordering to application ordering and create IS */ 6159566063dSJacob Faibussowitsch PetscCall(DMDAGetAO(da, &ao)); 6169566063dSJacob Faibussowitsch PetscCall(MatGetOwnershipRange(Anatural, &rstart, &rend)); 6179566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(rend - rstart, &app)); 61847c6ae99SBarry Smith for (i = rstart; i < rend; i++) app[i - rstart] = i; 6199566063dSJacob Faibussowitsch PetscCall(AOPetscToApplication(ao, rend - rstart, app)); 6209566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(comm, rend - rstart, app, PETSC_OWN_POINTER, &is)); 62147c6ae99SBarry Smith 62247c6ae99SBarry Smith /* Do permutation and replace header */ 6239566063dSJacob Faibussowitsch PetscCall(MatCreateSubMatrix(Anatural, is, is, MAT_INITIAL_MATRIX, &Aapp)); 6249566063dSJacob Faibussowitsch PetscCall(MatHeaderReplace(A, &Aapp)); 6259566063dSJacob Faibussowitsch PetscCall(ISDestroy(&is)); 6269566063dSJacob Faibussowitsch PetscCall(MatDestroy(&Anatural)); 6273ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 62847c6ae99SBarry Smith } 62947c6ae99SBarry Smith 630d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA(DM da, Mat *J) 631d71ae5a4SJacob Faibussowitsch { 63247c6ae99SBarry Smith PetscInt dim, dof, nx, ny, nz, dims[3], starts[3], M, N, P; 63347c6ae99SBarry Smith Mat A; 63447c6ae99SBarry Smith MPI_Comm comm; 63519fd82e9SBarry Smith MatType Atype; 636b412c318SBarry Smith MatType mtype; 63747c6ae99SBarry Smith PetscMPIInt size; 63847c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 639*ade515a3SBarry Smith void (*aij)(void) = NULL, (*baij)(void) = NULL, (*sbaij)(void) = NULL, (*sell)(void) = NULL, (*is)(void) = NULL; 64047c6ae99SBarry Smith 64147c6ae99SBarry Smith PetscFunctionBegin; 6429566063dSJacob Faibussowitsch PetscCall(MatInitializePackage()); 643b412c318SBarry Smith mtype = da->mattype; 64447c6ae99SBarry Smith 64547c6ae99SBarry Smith /* 64647c6ae99SBarry Smith m 64747c6ae99SBarry Smith ------------------------------------------------------ 64847c6ae99SBarry Smith | | 64947c6ae99SBarry Smith | | 65047c6ae99SBarry Smith | ---------------------- | 65147c6ae99SBarry Smith | | | | 65247c6ae99SBarry Smith n | ny | | | 65347c6ae99SBarry Smith | | | | 65447c6ae99SBarry Smith | .--------------------- | 65547c6ae99SBarry Smith | (xs,ys) nx | 65647c6ae99SBarry Smith | . | 65747c6ae99SBarry Smith | (gxs,gys) | 65847c6ae99SBarry Smith | | 65947c6ae99SBarry Smith ----------------------------------------------------- 66047c6ae99SBarry Smith */ 66147c6ae99SBarry Smith 66247c6ae99SBarry Smith /* 66347c6ae99SBarry Smith nc - number of components per grid point 66447c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 66547c6ae99SBarry Smith */ 666e30e807fSPeter Brune M = dd->M; 667e30e807fSPeter Brune N = dd->N; 668e30e807fSPeter Brune P = dd->P; 669c73cfb54SMatthew G. Knepley dim = da->dim; 670e30e807fSPeter Brune dof = dd->w; 6719566063dSJacob Faibussowitsch /* PetscCall(DMDAGetInfo(da,&dim,&M,&N,&P,NULL,NULL,NULL,&dof,NULL,NULL,NULL,NULL,NULL)); */ 6729566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, NULL, NULL, NULL, &nx, &ny, &nz)); 6739566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 6749566063dSJacob Faibussowitsch PetscCall(MatCreate(comm, &A)); 6759566063dSJacob Faibussowitsch PetscCall(MatSetSizes(A, dof * nx * ny * nz, dof * nx * ny * nz, dof * M * N * P, dof * M * N * P)); 6769566063dSJacob Faibussowitsch PetscCall(MatSetType(A, mtype)); 6779566063dSJacob Faibussowitsch PetscCall(MatSetFromOptions(A)); 67874427ab1SRichard Tran Mills if (dof * nx * ny * nz < da->bind_below) { 6799566063dSJacob Faibussowitsch PetscCall(MatSetBindingPropagates(A, PETSC_TRUE)); 6809566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(A, PETSC_TRUE)); 68174427ab1SRichard Tran Mills } 6829566063dSJacob Faibussowitsch PetscCall(MatSetDM(A, da)); 6831baa6e33SBarry Smith if (da->structure_only) PetscCall(MatSetOption(A, MAT_STRUCTURE_ONLY, PETSC_TRUE)); 6849566063dSJacob Faibussowitsch PetscCall(MatGetType(A, &Atype)); 68547c6ae99SBarry Smith /* 686aa219208SBarry Smith We do not provide a getmatrix function in the DMDA operations because 687aa219208SBarry Smith the basic DMDA does not know about matrices. We think of DMDA as being more 68847c6ae99SBarry Smith more low-level than matrices. This is kind of cheating but, cause sometimes 689aa219208SBarry Smith we think of DMDA has higher level than matrices. 69047c6ae99SBarry Smith 69147c6ae99SBarry Smith We could switch based on Atype (or mtype), but we do not since the 692844bd0d7SStefano Zampini specialized setting routines depend only on the particular preallocation 69347c6ae99SBarry Smith details of the matrix, not the type itself. 69447c6ae99SBarry Smith */ 6959566063dSJacob Faibussowitsch PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatMPIAIJSetPreallocation_C", &aij)); 69648a46eb9SPierre Jolivet if (!aij) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatSeqAIJSetPreallocation_C", &aij)); 69747c6ae99SBarry Smith if (!aij) { 6989566063dSJacob Faibussowitsch PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatMPIBAIJSetPreallocation_C", &baij)); 69948a46eb9SPierre Jolivet if (!baij) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatSeqBAIJSetPreallocation_C", &baij)); 70047c6ae99SBarry Smith if (!baij) { 7019566063dSJacob Faibussowitsch PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatMPISBAIJSetPreallocation_C", &sbaij)); 70248a46eb9SPierre Jolivet if (!sbaij) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatSeqSBAIJSetPreallocation_C", &sbaij)); 7035e26d47bSHong Zhang if (!sbaij) { 7049566063dSJacob Faibussowitsch PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatMPISELLSetPreallocation_C", &sell)); 70548a46eb9SPierre Jolivet if (!sell) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatSeqSELLSetPreallocation_C", &sell)); 7065e26d47bSHong Zhang } 70748a46eb9SPierre Jolivet if (!sell) PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatISSetPreallocation_C", &is)); 70847c6ae99SBarry Smith } 70947c6ae99SBarry Smith } 71047c6ae99SBarry Smith if (aij) { 71147c6ae99SBarry Smith if (dim == 1) { 712ce308e1dSBarry Smith if (dd->ofill) { 7139566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_1d_MPIAIJ_Fill(da, A)); 714ce308e1dSBarry Smith } else { 71519b08ed1SBarry Smith DMBoundaryType bx; 71619b08ed1SBarry Smith PetscMPIInt size; 7179566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &bx, NULL, NULL, NULL)); 7189566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)da), &size)); 71919b08ed1SBarry Smith if (size == 1 && bx == DM_BOUNDARY_NONE) { 7209566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_1d_SeqAIJ_NoPreallocation(da, A)); 72119b08ed1SBarry Smith } else { 7229566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_1d_MPIAIJ(da, A)); 723ce308e1dSBarry Smith } 72419b08ed1SBarry Smith } 72547c6ae99SBarry Smith } else if (dim == 2) { 72647c6ae99SBarry Smith if (dd->ofill) { 7279566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_2d_MPIAIJ_Fill(da, A)); 72847c6ae99SBarry Smith } else { 7299566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_2d_MPIAIJ(da, A)); 73047c6ae99SBarry Smith } 73147c6ae99SBarry Smith } else if (dim == 3) { 73247c6ae99SBarry Smith if (dd->ofill) { 7339566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_3d_MPIAIJ_Fill(da, A)); 73447c6ae99SBarry Smith } else { 7359566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_3d_MPIAIJ(da, A)); 73647c6ae99SBarry Smith } 73747c6ae99SBarry Smith } 73847c6ae99SBarry Smith } else if (baij) { 73947c6ae99SBarry Smith if (dim == 2) { 7409566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_2d_MPIBAIJ(da, A)); 74147c6ae99SBarry Smith } else if (dim == 3) { 7429566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_3d_MPIBAIJ(da, A)); 74363a3b9bcSJacob 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); 74447c6ae99SBarry Smith } else if (sbaij) { 74547c6ae99SBarry Smith if (dim == 2) { 7469566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_2d_MPISBAIJ(da, A)); 74747c6ae99SBarry Smith } else if (dim == 3) { 7489566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_3d_MPISBAIJ(da, A)); 74963a3b9bcSJacob 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); 750d4002b98SHong Zhang } else if (sell) { 7515e26d47bSHong Zhang if (dim == 2) { 7529566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_2d_MPISELL(da, A)); 753711261dbSHong Zhang } else if (dim == 3) { 7549566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_3d_MPISELL(da, A)); 75563a3b9bcSJacob 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); 756e584696dSStefano Zampini } else if (is) { 7579566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix_DA_IS(da, A)); 758869776cdSLisandro Dalcin } else { 75945b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 760e584696dSStefano Zampini 7619566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(A, dof)); 7629566063dSJacob Faibussowitsch PetscCall(MatSetUp(A)); 7639566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 7649566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(A, ltog, ltog)); 76547c6ae99SBarry Smith } 7669566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &starts[0], &starts[1], &starts[2], &dims[0], &dims[1], &dims[2])); 7679566063dSJacob Faibussowitsch PetscCall(MatSetStencil(A, dim, dims, starts, dof)); 7689566063dSJacob Faibussowitsch PetscCall(MatSetDM(A, da)); 7699566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(comm, &size)); 77047c6ae99SBarry Smith if (size > 1) { 77147c6ae99SBarry Smith /* change viewer to display matrix in natural ordering */ 7729566063dSJacob Faibussowitsch PetscCall(MatSetOperation(A, MATOP_VIEW, (void (*)(void))MatView_MPI_DA)); 7739566063dSJacob Faibussowitsch PetscCall(MatSetOperation(A, MATOP_LOAD, (void (*)(void))MatLoad_MPI_DA)); 77447c6ae99SBarry Smith } 77547c6ae99SBarry Smith *J = A; 7763ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 77747c6ae99SBarry Smith } 77847c6ae99SBarry Smith 77947c6ae99SBarry Smith /* ---------------------------------------------------------------------------------*/ 780844bd0d7SStefano Zampini PETSC_EXTERN PetscErrorCode MatISSetPreallocation_IS(Mat, PetscInt, const PetscInt[], PetscInt, const PetscInt[]); 781844bd0d7SStefano Zampini 782d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_IS(DM dm, Mat J) 783d71ae5a4SJacob Faibussowitsch { 784e584696dSStefano Zampini DM_DA *da = (DM_DA *)dm->data; 785e432b41dSStefano Zampini Mat lJ, P; 786e584696dSStefano Zampini ISLocalToGlobalMapping ltog; 787e432b41dSStefano Zampini IS is; 788e432b41dSStefano Zampini PetscBT bt; 78905339c03SStefano Zampini const PetscInt *e_loc, *idx; 790e432b41dSStefano Zampini PetscInt i, nel, nen, nv, dof, *gidx, n, N; 791e584696dSStefano Zampini 792e584696dSStefano Zampini /* The l2g map of DMDA has all ghosted nodes, and e_loc is a subset of all the local nodes (including the ghosted) 793e432b41dSStefano Zampini We need to filter out the local indices that are not represented through the DMDAGetElements decomposition */ 794e584696dSStefano Zampini PetscFunctionBegin; 795e584696dSStefano Zampini dof = da->w; 7969566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, dof)); 7979566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(dm, <og)); 798e432b41dSStefano Zampini 799e432b41dSStefano Zampini /* flag local elements indices in local DMDA numbering */ 8009566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingGetSize(ltog, &nv)); 8019566063dSJacob Faibussowitsch PetscCall(PetscBTCreate(nv / dof, &bt)); 8029566063dSJacob Faibussowitsch PetscCall(DMDAGetElements(dm, &nel, &nen, &e_loc)); /* this will throw an error if the stencil type is not DMDA_STENCIL_BOX */ 8039566063dSJacob Faibussowitsch for (i = 0; i < nel * nen; i++) PetscCall(PetscBTSet(bt, e_loc[i])); 804e432b41dSStefano Zampini 805e432b41dSStefano Zampini /* filter out (set to -1) the global indices not used by the local elements */ 8069566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nv / dof, &gidx)); 8079566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingGetBlockIndices(ltog, &idx)); 8089566063dSJacob Faibussowitsch PetscCall(PetscArraycpy(gidx, idx, nv / dof)); 8099566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingRestoreBlockIndices(ltog, &idx)); 8109371c9d4SSatish Balay for (i = 0; i < nv / dof; i++) 8119371c9d4SSatish Balay if (!PetscBTLookup(bt, i)) gidx[i] = -1; 8129566063dSJacob Faibussowitsch PetscCall(PetscBTDestroy(&bt)); 8139566063dSJacob Faibussowitsch PetscCall(ISCreateBlock(PetscObjectComm((PetscObject)dm), dof, nv / dof, gidx, PETSC_OWN_POINTER, &is)); 8149566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingCreateIS(is, <og)); 8159566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 8169566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingDestroy(<og)); 8179566063dSJacob Faibussowitsch PetscCall(ISDestroy(&is)); 81805339c03SStefano Zampini 819e432b41dSStefano Zampini /* Preallocation */ 820e306f867SJed Brown if (dm->prealloc_skip) { 8219566063dSJacob Faibussowitsch PetscCall(MatSetUp(J)); 822e306f867SJed Brown } else { 8239566063dSJacob Faibussowitsch PetscCall(MatISGetLocalMat(J, &lJ)); 8249566063dSJacob Faibussowitsch PetscCall(MatGetLocalToGlobalMapping(lJ, <og, NULL)); 8259566063dSJacob Faibussowitsch PetscCall(MatCreate(PetscObjectComm((PetscObject)lJ), &P)); 8269566063dSJacob Faibussowitsch PetscCall(MatSetType(P, MATPREALLOCATOR)); 8279566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(P, ltog, ltog)); 8289566063dSJacob Faibussowitsch PetscCall(MatGetSize(lJ, &N, NULL)); 8299566063dSJacob Faibussowitsch PetscCall(MatGetLocalSize(lJ, &n, NULL)); 8309566063dSJacob Faibussowitsch PetscCall(MatSetSizes(P, n, n, N, N)); 8319566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(P, dof)); 8329566063dSJacob Faibussowitsch PetscCall(MatSetUp(P)); 83348a46eb9SPierre Jolivet for (i = 0; i < nel; i++) PetscCall(MatSetValuesBlockedLocal(P, nen, e_loc + i * nen, nen, e_loc + i * nen, NULL, INSERT_VALUES)); 8349566063dSJacob Faibussowitsch PetscCall(MatPreallocatorPreallocate(P, (PetscBool)!da->prealloc_only, lJ)); 8359566063dSJacob Faibussowitsch PetscCall(MatISRestoreLocalMat(J, &lJ)); 8369566063dSJacob Faibussowitsch PetscCall(DMDARestoreElements(dm, &nel, &nen, &e_loc)); 8379566063dSJacob Faibussowitsch PetscCall(MatDestroy(&P)); 838e432b41dSStefano Zampini 8399566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 8409566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 841e306f867SJed Brown } 8423ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 843e584696dSStefano Zampini } 844e584696dSStefano Zampini 845d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPISELL(DM da, Mat J) 846d71ae5a4SJacob Faibussowitsch { 8475e26d47bSHong 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; 8485e26d47bSHong Zhang PetscInt lstart, lend, pstart, pend, *dnz, *onz; 8495e26d47bSHong Zhang MPI_Comm comm; 8505e26d47bSHong Zhang PetscScalar *values; 8515e26d47bSHong Zhang DMBoundaryType bx, by; 8525e26d47bSHong Zhang ISLocalToGlobalMapping ltog; 8535e26d47bSHong Zhang DMDAStencilType st; 8545e26d47bSHong Zhang 8555e26d47bSHong Zhang PetscFunctionBegin; 8565e26d47bSHong Zhang /* 8575e26d47bSHong Zhang nc - number of components per grid point 8585e26d47bSHong Zhang col - number of colors needed in one direction for single component problem 8595e26d47bSHong Zhang */ 8609566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, NULL, NULL, NULL, &nc, &s, &bx, &by, NULL, &st)); 8615e26d47bSHong Zhang col = 2 * s + 1; 8629566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 8639566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 8649566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 8655e26d47bSHong Zhang 8669566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * col * nc * nc, &cols)); 8679566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 8685e26d47bSHong Zhang 8699566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 8705e26d47bSHong Zhang /* determine the matrix preallocation information */ 871d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny, nc * nx * ny, dnz, onz); 8725e26d47bSHong Zhang for (i = xs; i < xs + nx; i++) { 8735e26d47bSHong Zhang pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 8745e26d47bSHong Zhang pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 8755e26d47bSHong Zhang 8765e26d47bSHong Zhang for (j = ys; j < ys + ny; j++) { 8775e26d47bSHong Zhang slot = i - gxs + gnx * (j - gys); 8785e26d47bSHong Zhang 8795e26d47bSHong Zhang lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 8805e26d47bSHong Zhang lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 8815e26d47bSHong Zhang 8825e26d47bSHong Zhang cnt = 0; 8835e26d47bSHong Zhang for (k = 0; k < nc; k++) { 8845e26d47bSHong Zhang for (l = lstart; l < lend + 1; l++) { 8855e26d47bSHong Zhang for (p = pstart; p < pend + 1; p++) { 8865e26d47bSHong Zhang if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star have either l = 0 or p = 0 */ 8875e26d47bSHong Zhang cols[cnt++] = k + nc * (slot + gnx * l + p); 8885e26d47bSHong Zhang } 8895e26d47bSHong Zhang } 8905e26d47bSHong Zhang } 8915e26d47bSHong Zhang rows[k] = k + nc * (slot); 8925e26d47bSHong Zhang } 8939566063dSJacob Faibussowitsch PetscCall(MatPreallocateSetLocal(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 8945e26d47bSHong Zhang } 8955e26d47bSHong Zhang } 8969566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 8979566063dSJacob Faibussowitsch PetscCall(MatSeqSELLSetPreallocation(J, 0, dnz)); 8989566063dSJacob Faibussowitsch PetscCall(MatMPISELLSetPreallocation(J, 0, dnz, 0, onz)); 899d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 9005e26d47bSHong Zhang 9019566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 9025e26d47bSHong Zhang 9035e26d47bSHong Zhang /* 9045e26d47bSHong Zhang For each node in the grid: we get the neighbors in the local (on processor ordering 9055e26d47bSHong Zhang that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 9065e26d47bSHong Zhang PETSc ordering. 9075e26d47bSHong Zhang */ 9085e26d47bSHong Zhang if (!da->prealloc_only) { 9099566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * nc * nc, &values)); 9105e26d47bSHong Zhang for (i = xs; i < xs + nx; i++) { 9115e26d47bSHong Zhang pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 9125e26d47bSHong Zhang pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 9135e26d47bSHong Zhang 9145e26d47bSHong Zhang for (j = ys; j < ys + ny; j++) { 9155e26d47bSHong Zhang slot = i - gxs + gnx * (j - gys); 9165e26d47bSHong Zhang 9175e26d47bSHong Zhang lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 9185e26d47bSHong Zhang lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 9195e26d47bSHong Zhang 9205e26d47bSHong Zhang cnt = 0; 9215e26d47bSHong Zhang for (k = 0; k < nc; k++) { 9225e26d47bSHong Zhang for (l = lstart; l < lend + 1; l++) { 9235e26d47bSHong Zhang for (p = pstart; p < pend + 1; p++) { 9245e26d47bSHong Zhang if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star have either l = 0 or p = 0 */ 9255e26d47bSHong Zhang cols[cnt++] = k + nc * (slot + gnx * l + p); 9265e26d47bSHong Zhang } 9275e26d47bSHong Zhang } 9285e26d47bSHong Zhang } 9295e26d47bSHong Zhang rows[k] = k + nc * (slot); 9305e26d47bSHong Zhang } 9319566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, values, INSERT_VALUES)); 9325e26d47bSHong Zhang } 9335e26d47bSHong Zhang } 9349566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 935e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 9369566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 9379566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 9389566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 9399566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 9409566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 9415e26d47bSHong Zhang } 9429566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 9433ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 9445e26d47bSHong Zhang } 9455e26d47bSHong Zhang 946d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPISELL(DM da, Mat J) 947d71ae5a4SJacob Faibussowitsch { 948711261dbSHong Zhang PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 949711261dbSHong Zhang PetscInt m, n, dim, s, *cols = NULL, k, nc, *rows = NULL, col, cnt, l, p, *dnz = NULL, *onz = NULL; 950711261dbSHong Zhang PetscInt istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk, M, N, P; 951711261dbSHong Zhang MPI_Comm comm; 952711261dbSHong Zhang PetscScalar *values; 953711261dbSHong Zhang DMBoundaryType bx, by, bz; 954711261dbSHong Zhang ISLocalToGlobalMapping ltog; 955711261dbSHong Zhang DMDAStencilType st; 956711261dbSHong Zhang 957711261dbSHong Zhang PetscFunctionBegin; 958711261dbSHong Zhang /* 959711261dbSHong Zhang nc - number of components per grid point 960711261dbSHong Zhang col - number of colors needed in one direction for single component problem 961711261dbSHong Zhang */ 9629566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &nc, &s, &bx, &by, &bz, &st)); 963711261dbSHong Zhang col = 2 * s + 1; 9649566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 9659566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 9669566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 967711261dbSHong Zhang 9689566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * col * col * nc * nc, &cols)); 9699566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 970711261dbSHong Zhang 9719566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 972711261dbSHong Zhang /* determine the matrix preallocation information */ 973d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny * nz, nc * nx * ny * nz, dnz, onz); 974711261dbSHong Zhang for (i = xs; i < xs + nx; i++) { 975711261dbSHong Zhang istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 976711261dbSHong Zhang iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 977711261dbSHong Zhang for (j = ys; j < ys + ny; j++) { 978711261dbSHong Zhang jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 979711261dbSHong Zhang jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 980711261dbSHong Zhang for (k = zs; k < zs + nz; k++) { 981711261dbSHong Zhang kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 982711261dbSHong Zhang kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 983711261dbSHong Zhang 984711261dbSHong Zhang slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 985711261dbSHong Zhang 986711261dbSHong Zhang cnt = 0; 987711261dbSHong Zhang for (l = 0; l < nc; l++) { 988711261dbSHong Zhang for (ii = istart; ii < iend + 1; ii++) { 989711261dbSHong Zhang for (jj = jstart; jj < jend + 1; jj++) { 990711261dbSHong Zhang for (kk = kstart; kk < kend + 1; kk++) { 991711261dbSHong Zhang if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 992711261dbSHong Zhang cols[cnt++] = l + nc * (slot + ii + gnx * jj + gnx * gny * kk); 993711261dbSHong Zhang } 994711261dbSHong Zhang } 995711261dbSHong Zhang } 996711261dbSHong Zhang } 997711261dbSHong Zhang rows[l] = l + nc * (slot); 998711261dbSHong Zhang } 9999566063dSJacob Faibussowitsch PetscCall(MatPreallocateSetLocal(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 1000711261dbSHong Zhang } 1001711261dbSHong Zhang } 1002711261dbSHong Zhang } 10039566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 10049566063dSJacob Faibussowitsch PetscCall(MatSeqSELLSetPreallocation(J, 0, dnz)); 10059566063dSJacob Faibussowitsch PetscCall(MatMPISELLSetPreallocation(J, 0, dnz, 0, onz)); 1006d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 10079566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 1008711261dbSHong Zhang 1009711261dbSHong Zhang /* 1010711261dbSHong Zhang For each node in the grid: we get the neighbors in the local (on processor ordering 1011711261dbSHong Zhang that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 1012711261dbSHong Zhang PETSc ordering. 1013711261dbSHong Zhang */ 1014711261dbSHong Zhang if (!da->prealloc_only) { 10159566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * col * nc * nc * nc, &values)); 1016711261dbSHong Zhang for (i = xs; i < xs + nx; i++) { 1017711261dbSHong Zhang istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1018711261dbSHong Zhang iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 1019711261dbSHong Zhang for (j = ys; j < ys + ny; j++) { 1020711261dbSHong Zhang jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1021711261dbSHong Zhang jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 1022711261dbSHong Zhang for (k = zs; k < zs + nz; k++) { 1023711261dbSHong Zhang kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 1024711261dbSHong Zhang kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 1025711261dbSHong Zhang 1026711261dbSHong Zhang slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 1027711261dbSHong Zhang 1028711261dbSHong Zhang cnt = 0; 1029711261dbSHong Zhang for (l = 0; l < nc; l++) { 1030711261dbSHong Zhang for (ii = istart; ii < iend + 1; ii++) { 1031711261dbSHong Zhang for (jj = jstart; jj < jend + 1; jj++) { 1032711261dbSHong Zhang for (kk = kstart; kk < kend + 1; kk++) { 1033711261dbSHong Zhang if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 1034711261dbSHong Zhang cols[cnt++] = l + nc * (slot + ii + gnx * jj + gnx * gny * kk); 1035711261dbSHong Zhang } 1036711261dbSHong Zhang } 1037711261dbSHong Zhang } 1038711261dbSHong Zhang } 1039711261dbSHong Zhang rows[l] = l + nc * (slot); 1040711261dbSHong Zhang } 10419566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, values, INSERT_VALUES)); 1042711261dbSHong Zhang } 1043711261dbSHong Zhang } 1044711261dbSHong Zhang } 10459566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 1046e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 10479566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 10489566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 10499566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 10509566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 10519566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 1052711261dbSHong Zhang } 10539566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 10543ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1055711261dbSHong Zhang } 1056711261dbSHong Zhang 1057d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPIAIJ(DM da, Mat J) 1058d71ae5a4SJacob Faibussowitsch { 1059c1154cd5SBarry 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; 106047c6ae99SBarry Smith PetscInt lstart, lend, pstart, pend, *dnz, *onz; 106147c6ae99SBarry Smith MPI_Comm comm; 1062bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 1063844bd0d7SStefano Zampini ISLocalToGlobalMapping ltog, mltog; 1064aa219208SBarry Smith DMDAStencilType st; 1065b294de21SRichard Tran Mills PetscBool removedups = PETSC_FALSE, alreadyboundtocpu = PETSC_TRUE; 106647c6ae99SBarry Smith 106747c6ae99SBarry Smith PetscFunctionBegin; 106847c6ae99SBarry Smith /* 106947c6ae99SBarry Smith nc - number of components per grid point 107047c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 107147c6ae99SBarry Smith */ 1072924e958dSJunchao Zhang PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, &M, &N, NULL, &nc, &s, &bx, &by, NULL, &st)); 10731baa6e33SBarry Smith if (bx == DM_BOUNDARY_NONE && by == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_TRUE)); 107447c6ae99SBarry Smith col = 2 * s + 1; 1075c1154cd5SBarry Smith /* 1076c1154cd5SBarry Smith With one processor in periodic domains in a skinny dimension the code will label nonzero columns multiple times 1077c1154cd5SBarry Smith because of "wrapping" around the end of the domain hitting an entry already counted in the other direction. 1078c1154cd5SBarry Smith */ 1079c1154cd5SBarry Smith if (M == 1 && 2 * s >= m) removedups = PETSC_TRUE; 1080c1154cd5SBarry Smith if (N == 1 && 2 * s >= n) removedups = PETSC_TRUE; 10819566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 10829566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 10839566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 108447c6ae99SBarry Smith 10859566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * col * nc * nc, &cols)); 10869566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 108747c6ae99SBarry Smith 10889566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 108947c6ae99SBarry Smith /* determine the matrix preallocation information */ 1090d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny, nc * nx * ny, dnz, onz); 109147c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1092bff4a2f0SMatthew G. Knepley pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1093bff4a2f0SMatthew G. Knepley pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 109447c6ae99SBarry Smith 109547c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 109647c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 109747c6ae99SBarry Smith 1098bff4a2f0SMatthew G. Knepley lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1099bff4a2f0SMatthew G. Knepley lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 110047c6ae99SBarry Smith 110147c6ae99SBarry Smith cnt = 0; 110247c6ae99SBarry Smith for (k = 0; k < nc; k++) { 110347c6ae99SBarry Smith for (l = lstart; l < lend + 1; l++) { 110447c6ae99SBarry Smith for (p = pstart; p < pend + 1; p++) { 1105aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star have either l = 0 or p = 0 */ 110647c6ae99SBarry Smith cols[cnt++] = k + nc * (slot + gnx * l + p); 110747c6ae99SBarry Smith } 110847c6ae99SBarry Smith } 110947c6ae99SBarry Smith } 111047c6ae99SBarry Smith rows[k] = k + nc * (slot); 111147c6ae99SBarry Smith } 11121baa6e33SBarry Smith if (removedups) PetscCall(MatPreallocateSetLocalRemoveDups(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 11131baa6e33SBarry Smith else PetscCall(MatPreallocateSetLocal(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 111447c6ae99SBarry Smith } 1115c1154cd5SBarry Smith } 11169566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 11179566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz)); 11189566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz)); 1119d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 11209566063dSJacob Faibussowitsch PetscCall(MatGetLocalToGlobalMapping(J, &mltog, NULL)); 11211baa6e33SBarry Smith if (!mltog) PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 112247c6ae99SBarry Smith 112347c6ae99SBarry Smith /* 112447c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 112547c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 112647c6ae99SBarry Smith PETSc ordering. 112747c6ae99SBarry Smith */ 1128fcfd50ebSBarry Smith if (!da->prealloc_only) { 112947c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1130bff4a2f0SMatthew G. Knepley pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1131bff4a2f0SMatthew G. Knepley pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 113247c6ae99SBarry Smith 113347c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 113447c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 113547c6ae99SBarry Smith 1136bff4a2f0SMatthew G. Knepley lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1137bff4a2f0SMatthew G. Knepley lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 113847c6ae99SBarry Smith 113947c6ae99SBarry Smith cnt = 0; 114047c6ae99SBarry Smith for (l = lstart; l < lend + 1; l++) { 114147c6ae99SBarry Smith for (p = pstart; p < pend + 1; p++) { 1142aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star have either l = 0 or p = 0 */ 1143071fcb05SBarry Smith cols[cnt++] = nc * (slot + gnx * l + p); 1144071fcb05SBarry Smith for (k = 1; k < nc; k++) { 11459371c9d4SSatish Balay cols[cnt] = 1 + cols[cnt - 1]; 11469371c9d4SSatish Balay cnt++; 114747c6ae99SBarry Smith } 114847c6ae99SBarry Smith } 114947c6ae99SBarry Smith } 115047c6ae99SBarry Smith } 1151071fcb05SBarry Smith for (k = 0; k < nc; k++) rows[k] = k + nc * (slot); 11529566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, NULL, INSERT_VALUES)); 115347c6ae99SBarry Smith } 115447c6ae99SBarry Smith } 1155e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 11569566063dSJacob Faibussowitsch PetscCall(MatBoundToCPU(J, &alreadyboundtocpu)); 11579566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 11589566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 11599566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 11609566063dSJacob Faibussowitsch if (!alreadyboundtocpu) PetscCall(MatBindToCPU(J, PETSC_FALSE)); 11619566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 11621baa6e33SBarry Smith if (bx == DM_BOUNDARY_NONE && by == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE)); 116347c6ae99SBarry Smith } 11649566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 11653ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 116647c6ae99SBarry Smith } 116747c6ae99SBarry Smith 1168d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPIAIJ_Fill(DM da, Mat J) 1169d71ae5a4SJacob Faibussowitsch { 117047c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 1171c1154cd5SBarry Smith PetscInt m, n, dim, s, *cols, k, nc, row, col, cnt, maxcnt = 0, l, p, M, N; 117247c6ae99SBarry Smith PetscInt lstart, lend, pstart, pend, *dnz, *onz; 117347c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 117447c6ae99SBarry Smith PetscInt ifill_col, *ofill = dd->ofill, *dfill = dd->dfill; 117547c6ae99SBarry Smith MPI_Comm comm; 1176bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 117745b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 1178aa219208SBarry Smith DMDAStencilType st; 1179c1154cd5SBarry Smith PetscBool removedups = PETSC_FALSE; 118047c6ae99SBarry Smith 118147c6ae99SBarry Smith PetscFunctionBegin; 118247c6ae99SBarry Smith /* 118347c6ae99SBarry Smith nc - number of components per grid point 118447c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 118547c6ae99SBarry Smith */ 1186924e958dSJunchao Zhang PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, &M, &N, NULL, &nc, &s, &bx, &by, NULL, &st)); 118747c6ae99SBarry Smith col = 2 * s + 1; 1188c1154cd5SBarry Smith /* 1189c1154cd5SBarry Smith With one processor in periodic domains in a skinny dimension the code will label nonzero columns multiple times 1190c1154cd5SBarry Smith because of "wrapping" around the end of the domain hitting an entry already counted in the other direction. 1191c1154cd5SBarry Smith */ 1192c1154cd5SBarry Smith if (M == 1 && 2 * s >= m) removedups = PETSC_TRUE; 1193c1154cd5SBarry Smith if (N == 1 && 2 * s >= n) removedups = PETSC_TRUE; 11949566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 11959566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 11969566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 119747c6ae99SBarry Smith 11989566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * nc, &cols)); 11999566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 120047c6ae99SBarry Smith 12019566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 120247c6ae99SBarry Smith /* determine the matrix preallocation information */ 1203d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny, nc * nx * ny, dnz, onz); 120447c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1205bff4a2f0SMatthew G. Knepley pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1206bff4a2f0SMatthew G. Knepley pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 120747c6ae99SBarry Smith 120847c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 120947c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 121047c6ae99SBarry Smith 1211bff4a2f0SMatthew G. Knepley lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1212bff4a2f0SMatthew G. Knepley lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 121347c6ae99SBarry Smith 121447c6ae99SBarry Smith for (k = 0; k < nc; k++) { 121547c6ae99SBarry Smith cnt = 0; 121647c6ae99SBarry Smith for (l = lstart; l < lend + 1; l++) { 121747c6ae99SBarry Smith for (p = pstart; p < pend + 1; p++) { 121847c6ae99SBarry Smith if (l || p) { 1219aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star */ 12208865f1eaSKarl Rupp for (ifill_col = ofill[k]; ifill_col < ofill[k + 1]; ifill_col++) cols[cnt++] = ofill[ifill_col] + nc * (slot + gnx * l + p); 122147c6ae99SBarry Smith } 122247c6ae99SBarry Smith } else { 122347c6ae99SBarry Smith if (dfill) { 12248865f1eaSKarl Rupp for (ifill_col = dfill[k]; ifill_col < dfill[k + 1]; ifill_col++) cols[cnt++] = dfill[ifill_col] + nc * (slot + gnx * l + p); 122547c6ae99SBarry Smith } else { 12268865f1eaSKarl Rupp for (ifill_col = 0; ifill_col < nc; ifill_col++) cols[cnt++] = ifill_col + nc * (slot + gnx * l + p); 122747c6ae99SBarry Smith } 122847c6ae99SBarry Smith } 122947c6ae99SBarry Smith } 123047c6ae99SBarry Smith } 123147c6ae99SBarry Smith row = k + nc * (slot); 1232c0ab637bSBarry Smith maxcnt = PetscMax(maxcnt, cnt); 12331baa6e33SBarry Smith if (removedups) PetscCall(MatPreallocateSetLocalRemoveDups(ltog, 1, &row, ltog, cnt, cols, dnz, onz)); 12341baa6e33SBarry Smith else PetscCall(MatPreallocateSetLocal(ltog, 1, &row, ltog, cnt, cols, dnz, onz)); 123547c6ae99SBarry Smith } 123647c6ae99SBarry Smith } 1237c1154cd5SBarry Smith } 12389566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz)); 12399566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz)); 1240d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 12419566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 124247c6ae99SBarry Smith 124347c6ae99SBarry Smith /* 124447c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 124547c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 124647c6ae99SBarry Smith PETSc ordering. 124747c6ae99SBarry Smith */ 1248fcfd50ebSBarry Smith if (!da->prealloc_only) { 124947c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1250bff4a2f0SMatthew G. Knepley pstart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1251bff4a2f0SMatthew G. Knepley pend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 125247c6ae99SBarry Smith 125347c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 125447c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 125547c6ae99SBarry Smith 1256bff4a2f0SMatthew G. Knepley lstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1257bff4a2f0SMatthew G. Knepley lend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 125847c6ae99SBarry Smith 125947c6ae99SBarry Smith for (k = 0; k < nc; k++) { 126047c6ae99SBarry Smith cnt = 0; 126147c6ae99SBarry Smith for (l = lstart; l < lend + 1; l++) { 126247c6ae99SBarry Smith for (p = pstart; p < pend + 1; p++) { 126347c6ae99SBarry Smith if (l || p) { 1264aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || (!l || !p)) { /* entries on star */ 12658865f1eaSKarl Rupp for (ifill_col = ofill[k]; ifill_col < ofill[k + 1]; ifill_col++) cols[cnt++] = ofill[ifill_col] + nc * (slot + gnx * l + p); 126647c6ae99SBarry Smith } 126747c6ae99SBarry Smith } else { 126847c6ae99SBarry Smith if (dfill) { 12698865f1eaSKarl Rupp for (ifill_col = dfill[k]; ifill_col < dfill[k + 1]; ifill_col++) cols[cnt++] = dfill[ifill_col] + nc * (slot + gnx * l + p); 127047c6ae99SBarry Smith } else { 12718865f1eaSKarl Rupp for (ifill_col = 0; ifill_col < nc; ifill_col++) cols[cnt++] = ifill_col + nc * (slot + gnx * l + p); 127247c6ae99SBarry Smith } 127347c6ae99SBarry Smith } 127447c6ae99SBarry Smith } 127547c6ae99SBarry Smith } 127647c6ae99SBarry Smith row = k + nc * (slot); 12779566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, 1, &row, cnt, cols, NULL, INSERT_VALUES)); 127847c6ae99SBarry Smith } 127947c6ae99SBarry Smith } 128047c6ae99SBarry Smith } 1281e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 12829566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 12839566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 12849566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 12859566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 12869566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 128747c6ae99SBarry Smith } 12889566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 12893ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 129047c6ae99SBarry Smith } 129147c6ae99SBarry Smith 129247c6ae99SBarry Smith /* ---------------------------------------------------------------------------------*/ 129347c6ae99SBarry Smith 1294d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPIAIJ(DM da, Mat J) 1295d71ae5a4SJacob Faibussowitsch { 129647c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 12970298fd71SBarry Smith PetscInt m, n, dim, s, *cols = NULL, k, nc, *rows = NULL, col, cnt, l, p, *dnz = NULL, *onz = NULL; 1298c1154cd5SBarry Smith PetscInt istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk, M, N, P; 129947c6ae99SBarry Smith MPI_Comm comm; 1300bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by, bz; 1301844bd0d7SStefano Zampini ISLocalToGlobalMapping ltog, mltog; 1302aa219208SBarry Smith DMDAStencilType st; 1303c1154cd5SBarry Smith PetscBool removedups = PETSC_FALSE; 130447c6ae99SBarry Smith 130547c6ae99SBarry Smith PetscFunctionBegin; 130647c6ae99SBarry Smith /* 130747c6ae99SBarry Smith nc - number of components per grid point 130847c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 130947c6ae99SBarry Smith */ 13109566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &nc, &s, &bx, &by, &bz, &st)); 131148a46eb9SPierre Jolivet if (bx == DM_BOUNDARY_NONE && by == DM_BOUNDARY_NONE && bz == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_TRUE)); 131247c6ae99SBarry Smith col = 2 * s + 1; 131347c6ae99SBarry Smith 1314c1154cd5SBarry Smith /* 1315c1154cd5SBarry Smith With one processor in periodic domains in a skinny dimension the code will label nonzero columns multiple times 1316c1154cd5SBarry Smith because of "wrapping" around the end of the domain hitting an entry already counted in the other direction. 1317c1154cd5SBarry Smith */ 1318c1154cd5SBarry Smith if (M == 1 && 2 * s >= m) removedups = PETSC_TRUE; 1319c1154cd5SBarry Smith if (N == 1 && 2 * s >= n) removedups = PETSC_TRUE; 1320c1154cd5SBarry Smith if (P == 1 && 2 * s >= p) removedups = PETSC_TRUE; 1321c1154cd5SBarry Smith 13229566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 13239566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 13249566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 132547c6ae99SBarry Smith 13269566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * col * col * nc * nc, &cols)); 13279566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 132847c6ae99SBarry Smith 13299566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 133047c6ae99SBarry Smith /* determine the matrix preallocation information */ 1331d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny * nz, nc * nx * ny * nz, dnz, onz); 133247c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1333bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1334bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 133547c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1336bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1337bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 133847c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 1339bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 1340bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 134147c6ae99SBarry Smith 134247c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 134347c6ae99SBarry Smith 134447c6ae99SBarry Smith cnt = 0; 134547c6ae99SBarry Smith for (l = 0; l < nc; l++) { 134647c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 134747c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 134847c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 1349aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 135047c6ae99SBarry Smith cols[cnt++] = l + nc * (slot + ii + gnx * jj + gnx * gny * kk); 135147c6ae99SBarry Smith } 135247c6ae99SBarry Smith } 135347c6ae99SBarry Smith } 135447c6ae99SBarry Smith } 135547c6ae99SBarry Smith rows[l] = l + nc * (slot); 135647c6ae99SBarry Smith } 13571baa6e33SBarry Smith if (removedups) PetscCall(MatPreallocateSetLocalRemoveDups(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 13581baa6e33SBarry Smith else PetscCall(MatPreallocateSetLocal(ltog, nc, rows, ltog, cnt, cols, dnz, onz)); 135947c6ae99SBarry Smith } 136047c6ae99SBarry Smith } 1361c1154cd5SBarry Smith } 13629566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 13639566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz)); 13649566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz)); 1365d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 13669566063dSJacob Faibussowitsch PetscCall(MatGetLocalToGlobalMapping(J, &mltog, NULL)); 136748a46eb9SPierre Jolivet if (!mltog) PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 136847c6ae99SBarry Smith 136947c6ae99SBarry Smith /* 137047c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 137147c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 137247c6ae99SBarry Smith PETSc ordering. 137347c6ae99SBarry Smith */ 1374fcfd50ebSBarry Smith if (!da->prealloc_only) { 137547c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1376bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1377bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 137847c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1379bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1380bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 138147c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 1382bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 1383bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 138447c6ae99SBarry Smith 138547c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 138647c6ae99SBarry Smith 138747c6ae99SBarry Smith cnt = 0; 138847c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 1389071fcb05SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 1390071fcb05SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 1391aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 1392071fcb05SBarry Smith cols[cnt++] = nc * (slot + ii + gnx * jj + gnx * gny * kk); 1393071fcb05SBarry Smith for (l = 1; l < nc; l++) { 13949371c9d4SSatish Balay cols[cnt] = 1 + cols[cnt - 1]; 13959371c9d4SSatish Balay cnt++; 139647c6ae99SBarry Smith } 139747c6ae99SBarry Smith } 139847c6ae99SBarry Smith } 139947c6ae99SBarry Smith } 140047c6ae99SBarry Smith } 14019371c9d4SSatish Balay rows[0] = nc * (slot); 14029371c9d4SSatish Balay for (l = 1; l < nc; l++) rows[l] = 1 + rows[l - 1]; 14039566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, NULL, INSERT_VALUES)); 140447c6ae99SBarry Smith } 140547c6ae99SBarry Smith } 140647c6ae99SBarry Smith } 1407e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 14089566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 14099566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 14109566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 141148a46eb9SPierre Jolivet if (bx == DM_BOUNDARY_NONE && by == DM_BOUNDARY_NONE && bz == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE)); 14129566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 14139566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 141447c6ae99SBarry Smith } 14159566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 14163ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 141747c6ae99SBarry Smith } 141847c6ae99SBarry Smith 141947c6ae99SBarry Smith /* ---------------------------------------------------------------------------------*/ 142047c6ae99SBarry Smith 1421d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_1d_MPIAIJ_Fill(DM da, Mat J) 1422d71ae5a4SJacob Faibussowitsch { 1423ce308e1dSBarry Smith DM_DA *dd = (DM_DA *)da->data; 1424ce308e1dSBarry Smith PetscInt xs, nx, i, j, gxs, gnx, row, k, l; 14258d4c968fSBarry Smith PetscInt m, dim, s, *cols = NULL, nc, cnt, maxcnt = 0, *ocols; 14260acb5bebSBarry Smith PetscInt *ofill = dd->ofill, *dfill = dd->dfill; 1427bff4a2f0SMatthew G. Knepley DMBoundaryType bx; 142845b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 1429ce308e1dSBarry Smith PetscMPIInt rank, size; 1430ce308e1dSBarry Smith 1431ce308e1dSBarry Smith PetscFunctionBegin; 14329566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)da), &rank)); 14339566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)da), &size)); 1434ce308e1dSBarry Smith 1435ce308e1dSBarry Smith /* 1436ce308e1dSBarry Smith nc - number of components per grid point 1437ce308e1dSBarry Smith */ 14389566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, NULL, NULL, NULL, NULL, NULL, &nc, &s, &bx, NULL, NULL, NULL)); 143908401ef6SPierre Jolivet PetscCheck(s <= 1, PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "Matrix creation for 1d not implemented correctly for stencil width larger than 1"); 14409566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &nx, NULL, NULL)); 14419566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, NULL, NULL, &gnx, NULL, NULL)); 1442ce308e1dSBarry Smith 14439566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 14449566063dSJacob Faibussowitsch PetscCall(PetscCalloc2(nx * nc, &cols, nx * nc, &ocols)); 1445ce308e1dSBarry Smith 1446ce308e1dSBarry Smith /* 1447ce308e1dSBarry Smith note should be smaller for first and last process with no periodic 1448ce308e1dSBarry Smith does not handle dfill 1449ce308e1dSBarry Smith */ 1450ce308e1dSBarry Smith cnt = 0; 1451ce308e1dSBarry Smith /* coupling with process to the left */ 1452ce308e1dSBarry Smith for (i = 0; i < s; i++) { 1453ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 1454dd400576SPatrick Sanan ocols[cnt] = ((rank == 0) ? 0 : (s - i) * (ofill[j + 1] - ofill[j])); 14550acb5bebSBarry Smith cols[cnt] = dfill[j + 1] - dfill[j] + (s + i) * (ofill[j + 1] - ofill[j]); 1456dd400576SPatrick Sanan if (rank == 0 && (dd->bx == DM_BOUNDARY_PERIODIC)) { 1457831644c1SBarry Smith if (size > 1) ocols[cnt] += (s - i) * (ofill[j + 1] - ofill[j]); 1458831644c1SBarry Smith else cols[cnt] += (s - i) * (ofill[j + 1] - ofill[j]); 1459831644c1SBarry Smith } 1460c0ab637bSBarry Smith maxcnt = PetscMax(maxcnt, ocols[cnt] + cols[cnt]); 1461ce308e1dSBarry Smith cnt++; 1462ce308e1dSBarry Smith } 1463ce308e1dSBarry Smith } 1464ce308e1dSBarry Smith for (i = s; i < nx - s; i++) { 1465ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 14660acb5bebSBarry Smith cols[cnt] = dfill[j + 1] - dfill[j] + 2 * s * (ofill[j + 1] - ofill[j]); 1467c0ab637bSBarry Smith maxcnt = PetscMax(maxcnt, ocols[cnt] + cols[cnt]); 1468ce308e1dSBarry Smith cnt++; 1469ce308e1dSBarry Smith } 1470ce308e1dSBarry Smith } 1471ce308e1dSBarry Smith /* coupling with process to the right */ 1472ce308e1dSBarry Smith for (i = nx - s; i < nx; i++) { 1473ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 1474ce308e1dSBarry Smith ocols[cnt] = ((rank == (size - 1)) ? 0 : (i - nx + s + 1) * (ofill[j + 1] - ofill[j])); 14750acb5bebSBarry Smith cols[cnt] = dfill[j + 1] - dfill[j] + (s + nx - i - 1) * (ofill[j + 1] - ofill[j]); 1476831644c1SBarry Smith if ((rank == size - 1) && (dd->bx == DM_BOUNDARY_PERIODIC)) { 1477831644c1SBarry Smith if (size > 1) ocols[cnt] += (i - nx + s + 1) * (ofill[j + 1] - ofill[j]); 1478831644c1SBarry Smith else cols[cnt] += (i - nx + s + 1) * (ofill[j + 1] - ofill[j]); 1479831644c1SBarry Smith } 1480c0ab637bSBarry Smith maxcnt = PetscMax(maxcnt, ocols[cnt] + cols[cnt]); 1481ce308e1dSBarry Smith cnt++; 1482ce308e1dSBarry Smith } 1483ce308e1dSBarry Smith } 1484ce308e1dSBarry Smith 14859566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, 0, cols)); 14869566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, 0, cols, 0, ocols)); 14879566063dSJacob Faibussowitsch PetscCall(PetscFree2(cols, ocols)); 1488ce308e1dSBarry Smith 14899566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 14909566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 1491ce308e1dSBarry Smith 1492ce308e1dSBarry Smith /* 1493ce308e1dSBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 1494ce308e1dSBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 1495ce308e1dSBarry Smith PETSc ordering. 1496ce308e1dSBarry Smith */ 1497ce308e1dSBarry Smith if (!da->prealloc_only) { 14989566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(maxcnt, &cols)); 1499ce308e1dSBarry Smith row = xs * nc; 1500ce308e1dSBarry Smith /* coupling with process to the left */ 1501ce308e1dSBarry Smith for (i = xs; i < xs + s; i++) { 1502ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 1503ce308e1dSBarry Smith cnt = 0; 1504ce308e1dSBarry Smith if (rank) { 1505ce308e1dSBarry Smith for (l = 0; l < s; l++) { 1506ce308e1dSBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i - s + l) * nc + ofill[k]; 1507ce308e1dSBarry Smith } 1508ce308e1dSBarry Smith } 1509dd400576SPatrick Sanan if (rank == 0 && (dd->bx == DM_BOUNDARY_PERIODIC)) { 1510831644c1SBarry Smith for (l = 0; l < s; l++) { 1511831644c1SBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (m + i - s - l) * nc + ofill[k]; 1512831644c1SBarry Smith } 1513831644c1SBarry Smith } 15140acb5bebSBarry Smith if (dfill) { 1515ad540459SPierre Jolivet for (k = dfill[j]; k < dfill[j + 1]; k++) cols[cnt++] = i * nc + dfill[k]; 15160acb5bebSBarry Smith } else { 1517ad540459SPierre Jolivet for (k = 0; k < nc; k++) cols[cnt++] = i * nc + k; 15180acb5bebSBarry Smith } 1519ce308e1dSBarry Smith for (l = 0; l < s; l++) { 1520ce308e1dSBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i + s - l) * nc + ofill[k]; 1521ce308e1dSBarry Smith } 15229566063dSJacob Faibussowitsch PetscCall(MatSetValues(J, 1, &row, cnt, cols, NULL, INSERT_VALUES)); 1523ce308e1dSBarry Smith row++; 1524ce308e1dSBarry Smith } 1525ce308e1dSBarry Smith } 1526ce308e1dSBarry Smith for (i = xs + s; i < xs + nx - s; i++) { 1527ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 1528ce308e1dSBarry Smith cnt = 0; 1529ce308e1dSBarry Smith for (l = 0; l < s; l++) { 1530ce308e1dSBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i - s + l) * nc + ofill[k]; 1531ce308e1dSBarry Smith } 15320acb5bebSBarry Smith if (dfill) { 1533ad540459SPierre Jolivet for (k = dfill[j]; k < dfill[j + 1]; k++) cols[cnt++] = i * nc + dfill[k]; 15340acb5bebSBarry Smith } else { 1535ad540459SPierre Jolivet for (k = 0; k < nc; k++) cols[cnt++] = i * nc + k; 15360acb5bebSBarry Smith } 1537ce308e1dSBarry Smith for (l = 0; l < s; l++) { 1538ce308e1dSBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i + s - l) * nc + ofill[k]; 1539ce308e1dSBarry Smith } 15409566063dSJacob Faibussowitsch PetscCall(MatSetValues(J, 1, &row, cnt, cols, NULL, INSERT_VALUES)); 1541ce308e1dSBarry Smith row++; 1542ce308e1dSBarry Smith } 1543ce308e1dSBarry Smith } 1544ce308e1dSBarry Smith /* coupling with process to the right */ 1545ce308e1dSBarry Smith for (i = xs + nx - s; i < xs + nx; i++) { 1546ce308e1dSBarry Smith for (j = 0; j < nc; j++) { 1547ce308e1dSBarry Smith cnt = 0; 1548ce308e1dSBarry Smith for (l = 0; l < s; l++) { 1549ce308e1dSBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i - s + l) * nc + ofill[k]; 1550ce308e1dSBarry Smith } 15510acb5bebSBarry Smith if (dfill) { 1552ad540459SPierre Jolivet for (k = dfill[j]; k < dfill[j + 1]; k++) cols[cnt++] = i * nc + dfill[k]; 15530acb5bebSBarry Smith } else { 1554ad540459SPierre Jolivet for (k = 0; k < nc; k++) cols[cnt++] = i * nc + k; 15550acb5bebSBarry Smith } 1556ce308e1dSBarry Smith if (rank < size - 1) { 1557ce308e1dSBarry Smith for (l = 0; l < s; l++) { 1558ce308e1dSBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i + s - l) * nc + ofill[k]; 1559ce308e1dSBarry Smith } 1560ce308e1dSBarry Smith } 1561831644c1SBarry Smith if ((rank == size - 1) && (dd->bx == DM_BOUNDARY_PERIODIC)) { 1562831644c1SBarry Smith for (l = 0; l < s; l++) { 1563831644c1SBarry Smith for (k = ofill[j]; k < ofill[j + 1]; k++) cols[cnt++] = (i - s - l - m + 2) * nc + ofill[k]; 1564831644c1SBarry Smith } 1565831644c1SBarry Smith } 15669566063dSJacob Faibussowitsch PetscCall(MatSetValues(J, 1, &row, cnt, cols, NULL, INSERT_VALUES)); 1567ce308e1dSBarry Smith row++; 1568ce308e1dSBarry Smith } 1569ce308e1dSBarry Smith } 15709566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 1571e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 15729566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 15739566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 15749566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 15759566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 15769566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 1577ce308e1dSBarry Smith } 15783ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1579ce308e1dSBarry Smith } 1580ce308e1dSBarry Smith 1581ce308e1dSBarry Smith /* ---------------------------------------------------------------------------------*/ 1582ce308e1dSBarry Smith 1583d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_1d_MPIAIJ(DM da, Mat J) 1584d71ae5a4SJacob Faibussowitsch { 158547c6ae99SBarry Smith PetscInt xs, nx, i, i1, slot, gxs, gnx; 15860298fd71SBarry Smith PetscInt m, dim, s, *cols = NULL, nc, *rows = NULL, col, cnt, l; 158747c6ae99SBarry Smith PetscInt istart, iend; 1588bff4a2f0SMatthew G. Knepley DMBoundaryType bx; 1589844bd0d7SStefano Zampini ISLocalToGlobalMapping ltog, mltog; 159047c6ae99SBarry Smith 159147c6ae99SBarry Smith PetscFunctionBegin; 159247c6ae99SBarry Smith /* 159347c6ae99SBarry Smith nc - number of components per grid point 159447c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 159547c6ae99SBarry Smith */ 15969566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, NULL, NULL, NULL, NULL, NULL, &nc, &s, &bx, NULL, NULL, NULL)); 159748a46eb9SPierre Jolivet if (bx == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_TRUE)); 159847c6ae99SBarry Smith col = 2 * s + 1; 159947c6ae99SBarry Smith 16009566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &nx, NULL, NULL)); 16019566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, NULL, NULL, &gnx, NULL, NULL)); 160247c6ae99SBarry Smith 16039566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 16049566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, col * nc, NULL)); 16059566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, col * nc, NULL, col * nc, NULL)); 160647c6ae99SBarry Smith 16079566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 16089566063dSJacob Faibussowitsch PetscCall(MatGetLocalToGlobalMapping(J, &mltog, NULL)); 160948a46eb9SPierre Jolivet if (!mltog) PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 161047c6ae99SBarry Smith 161147c6ae99SBarry Smith /* 161247c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 161347c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 161447c6ae99SBarry Smith PETSc ordering. 161547c6ae99SBarry Smith */ 1616fcfd50ebSBarry Smith if (!da->prealloc_only) { 16179566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * nc * nc, &cols)); 161847c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 161947c6ae99SBarry Smith istart = PetscMax(-s, gxs - i); 162047c6ae99SBarry Smith iend = PetscMin(s, gxs + gnx - i - 1); 162147c6ae99SBarry Smith slot = i - gxs; 162247c6ae99SBarry Smith 162347c6ae99SBarry Smith cnt = 0; 162447c6ae99SBarry Smith for (i1 = istart; i1 < iend + 1; i1++) { 1625071fcb05SBarry Smith cols[cnt++] = nc * (slot + i1); 1626071fcb05SBarry Smith for (l = 1; l < nc; l++) { 16279371c9d4SSatish Balay cols[cnt] = 1 + cols[cnt - 1]; 16289371c9d4SSatish Balay cnt++; 162947c6ae99SBarry Smith } 163047c6ae99SBarry Smith } 16319371c9d4SSatish Balay rows[0] = nc * (slot); 16329371c9d4SSatish Balay for (l = 1; l < nc; l++) rows[l] = 1 + rows[l - 1]; 16339566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, NULL, INSERT_VALUES)); 163447c6ae99SBarry Smith } 1635e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 16369566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 16379566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 16389566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 163948a46eb9SPierre Jolivet if (bx == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE)); 16409566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 16419566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 16429566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 1643ce308e1dSBarry Smith } 16443ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 164547c6ae99SBarry Smith } 164647c6ae99SBarry Smith 164719b08ed1SBarry Smith /* ---------------------------------------------------------------------------------*/ 164819b08ed1SBarry Smith 1649d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_1d_SeqAIJ_NoPreallocation(DM da, Mat J) 1650d71ae5a4SJacob Faibussowitsch { 165119b08ed1SBarry Smith PetscInt xs, nx, i, i1, slot, gxs, gnx; 165219b08ed1SBarry Smith PetscInt m, dim, s, *cols = NULL, nc, *rows = NULL, col, cnt, l; 165319b08ed1SBarry Smith PetscInt istart, iend; 165419b08ed1SBarry Smith DMBoundaryType bx; 165519b08ed1SBarry Smith ISLocalToGlobalMapping ltog, mltog; 165619b08ed1SBarry Smith 165719b08ed1SBarry Smith PetscFunctionBegin; 165819b08ed1SBarry Smith /* 165919b08ed1SBarry Smith nc - number of components per grid point 166019b08ed1SBarry Smith col - number of colors needed in one direction for single component problem 166119b08ed1SBarry Smith */ 16629566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, NULL, NULL, NULL, NULL, NULL, &nc, &s, &bx, NULL, NULL, NULL)); 166319b08ed1SBarry Smith col = 2 * s + 1; 166419b08ed1SBarry Smith 16659566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &nx, NULL, NULL)); 16669566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, NULL, NULL, &gnx, NULL, NULL)); 166719b08ed1SBarry Smith 16689566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 16699566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetTotalPreallocation(J, nx * nc * col * nc)); 167019b08ed1SBarry Smith 16719566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 16729566063dSJacob Faibussowitsch PetscCall(MatGetLocalToGlobalMapping(J, &mltog, NULL)); 167348a46eb9SPierre Jolivet if (!mltog) PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 167419b08ed1SBarry Smith 167519b08ed1SBarry Smith /* 167619b08ed1SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 167719b08ed1SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 167819b08ed1SBarry Smith PETSc ordering. 167919b08ed1SBarry Smith */ 168019b08ed1SBarry Smith if (!da->prealloc_only) { 16819566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(nc, &rows, col * nc * nc, &cols)); 168219b08ed1SBarry Smith for (i = xs; i < xs + nx; i++) { 168319b08ed1SBarry Smith istart = PetscMax(-s, gxs - i); 168419b08ed1SBarry Smith iend = PetscMin(s, gxs + gnx - i - 1); 168519b08ed1SBarry Smith slot = i - gxs; 168619b08ed1SBarry Smith 168719b08ed1SBarry Smith cnt = 0; 168819b08ed1SBarry Smith for (i1 = istart; i1 < iend + 1; i1++) { 168919b08ed1SBarry Smith cols[cnt++] = nc * (slot + i1); 169019b08ed1SBarry Smith for (l = 1; l < nc; l++) { 16919371c9d4SSatish Balay cols[cnt] = 1 + cols[cnt - 1]; 16929371c9d4SSatish Balay cnt++; 169319b08ed1SBarry Smith } 169419b08ed1SBarry Smith } 16959371c9d4SSatish Balay rows[0] = nc * (slot); 16969371c9d4SSatish Balay for (l = 1; l < nc; l++) rows[l] = 1 + rows[l - 1]; 16979566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, nc, rows, cnt, cols, NULL, INSERT_VALUES)); 169819b08ed1SBarry Smith } 169919b08ed1SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 17009566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 17019566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 17029566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 170348a46eb9SPierre Jolivet if (bx == DM_BOUNDARY_NONE) PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE)); 17049566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 17059566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 17069566063dSJacob Faibussowitsch PetscCall(PetscFree2(rows, cols)); 170719b08ed1SBarry Smith } 17089566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_SORTED_FULL, PETSC_FALSE)); 17093ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 171019b08ed1SBarry Smith } 171119b08ed1SBarry Smith 1712d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPIBAIJ(DM da, Mat J) 1713d71ae5a4SJacob Faibussowitsch { 171447c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 171547c6ae99SBarry Smith PetscInt m, n, dim, s, *cols, nc, col, cnt, *dnz, *onz; 171647c6ae99SBarry Smith PetscInt istart, iend, jstart, jend, ii, jj; 171747c6ae99SBarry Smith MPI_Comm comm; 171847c6ae99SBarry Smith PetscScalar *values; 1719bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 1720aa219208SBarry Smith DMDAStencilType st; 172145b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 172247c6ae99SBarry Smith 172347c6ae99SBarry Smith PetscFunctionBegin; 172447c6ae99SBarry Smith /* 172547c6ae99SBarry Smith nc - number of components per grid point 172647c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 172747c6ae99SBarry Smith */ 17289566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, NULL, NULL, NULL, &nc, &s, &bx, &by, NULL, &st)); 172947c6ae99SBarry Smith col = 2 * s + 1; 173047c6ae99SBarry Smith 17319566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 17329566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 17339566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 173447c6ae99SBarry Smith 17359566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * nc * nc, &cols)); 173647c6ae99SBarry Smith 17379566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 173847c6ae99SBarry Smith 173947c6ae99SBarry Smith /* determine the matrix preallocation information */ 1740d0609cedSBarry Smith MatPreallocateBegin(comm, nx * ny, nx * ny, dnz, onz); 174147c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1742bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1743bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 174447c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1745bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1746bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 174747c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 174847c6ae99SBarry Smith 174947c6ae99SBarry Smith /* Find block columns in block row */ 175047c6ae99SBarry Smith cnt = 0; 175147c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 175247c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 1753aa219208SBarry Smith if (st == DMDA_STENCIL_BOX || !ii || !jj) { /* BOX or on the STAR */ 175447c6ae99SBarry Smith cols[cnt++] = slot + ii + gnx * jj; 175547c6ae99SBarry Smith } 175647c6ae99SBarry Smith } 175747c6ae99SBarry Smith } 17589566063dSJacob Faibussowitsch PetscCall(MatPreallocateSetLocalBlock(ltog, 1, &slot, ltog, cnt, cols, dnz, onz)); 175947c6ae99SBarry Smith } 176047c6ae99SBarry Smith } 17619566063dSJacob Faibussowitsch PetscCall(MatSeqBAIJSetPreallocation(J, nc, 0, dnz)); 17629566063dSJacob Faibussowitsch PetscCall(MatMPIBAIJSetPreallocation(J, nc, 0, dnz, 0, onz)); 1763d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 176447c6ae99SBarry Smith 17659566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 176647c6ae99SBarry Smith 176747c6ae99SBarry Smith /* 176847c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 176947c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 177047c6ae99SBarry Smith PETSc ordering. 177147c6ae99SBarry Smith */ 1772fcfd50ebSBarry Smith if (!da->prealloc_only) { 17739566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * nc * nc, &values)); 177447c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1775bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1776bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 177747c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1778bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1779bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 178047c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 178147c6ae99SBarry Smith cnt = 0; 178247c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 178347c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 1784aa219208SBarry Smith if (st == DMDA_STENCIL_BOX || !ii || !jj) { /* BOX or on the STAR */ 178547c6ae99SBarry Smith cols[cnt++] = slot + ii + gnx * jj; 178647c6ae99SBarry Smith } 178747c6ae99SBarry Smith } 178847c6ae99SBarry Smith } 17899566063dSJacob Faibussowitsch PetscCall(MatSetValuesBlockedLocal(J, 1, &slot, cnt, cols, values, INSERT_VALUES)); 179047c6ae99SBarry Smith } 179147c6ae99SBarry Smith } 17929566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 1793e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 17949566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 17959566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 17969566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 17979566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 17989566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 179947c6ae99SBarry Smith } 18009566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 18013ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 180247c6ae99SBarry Smith } 180347c6ae99SBarry Smith 1804d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPIBAIJ(DM da, Mat J) 1805d71ae5a4SJacob Faibussowitsch { 180647c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 180747c6ae99SBarry Smith PetscInt m, n, dim, s, *cols, k, nc, col, cnt, p, *dnz, *onz; 180847c6ae99SBarry Smith PetscInt istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk; 180947c6ae99SBarry Smith MPI_Comm comm; 181047c6ae99SBarry Smith PetscScalar *values; 1811bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by, bz; 1812aa219208SBarry Smith DMDAStencilType st; 181345b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 181447c6ae99SBarry Smith 181547c6ae99SBarry Smith PetscFunctionBegin; 181647c6ae99SBarry Smith /* 181747c6ae99SBarry Smith nc - number of components per grid point 181847c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 181947c6ae99SBarry Smith */ 18209566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, NULL, NULL, NULL, &nc, &s, &bx, &by, &bz, &st)); 182147c6ae99SBarry Smith col = 2 * s + 1; 182247c6ae99SBarry Smith 18239566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 18249566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 18259566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 182647c6ae99SBarry Smith 18279566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * col, &cols)); 182847c6ae99SBarry Smith 18299566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 183047c6ae99SBarry Smith 183147c6ae99SBarry Smith /* determine the matrix preallocation information */ 1832d0609cedSBarry Smith MatPreallocateBegin(comm, nx * ny * nz, nx * ny * nz, dnz, onz); 183347c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1834bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1835bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 183647c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1837bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1838bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 183947c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 1840bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 1841bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 184247c6ae99SBarry Smith 184347c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 184447c6ae99SBarry Smith 184547c6ae99SBarry Smith /* Find block columns in block row */ 184647c6ae99SBarry Smith cnt = 0; 184747c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 184847c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 184947c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 1850aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 185147c6ae99SBarry Smith cols[cnt++] = slot + ii + gnx * jj + gnx * gny * kk; 185247c6ae99SBarry Smith } 185347c6ae99SBarry Smith } 185447c6ae99SBarry Smith } 185547c6ae99SBarry Smith } 18569566063dSJacob Faibussowitsch PetscCall(MatPreallocateSetLocalBlock(ltog, 1, &slot, ltog, cnt, cols, dnz, onz)); 185747c6ae99SBarry Smith } 185847c6ae99SBarry Smith } 185947c6ae99SBarry Smith } 18609566063dSJacob Faibussowitsch PetscCall(MatSeqBAIJSetPreallocation(J, nc, 0, dnz)); 18619566063dSJacob Faibussowitsch PetscCall(MatMPIBAIJSetPreallocation(J, nc, 0, dnz, 0, onz)); 1862d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 186347c6ae99SBarry Smith 18649566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 186547c6ae99SBarry Smith 186647c6ae99SBarry Smith /* 186747c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 186847c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 186947c6ae99SBarry Smith PETSc ordering. 187047c6ae99SBarry Smith */ 1871fcfd50ebSBarry Smith if (!da->prealloc_only) { 18729566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * col * nc * nc, &values)); 187347c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1874bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1875bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 187647c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1877bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1878bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 187947c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 1880bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 1881bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 188247c6ae99SBarry Smith 188347c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 188447c6ae99SBarry Smith 188547c6ae99SBarry Smith cnt = 0; 188647c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 188747c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 188847c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 1889aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 189047c6ae99SBarry Smith cols[cnt++] = slot + ii + gnx * jj + gnx * gny * kk; 189147c6ae99SBarry Smith } 189247c6ae99SBarry Smith } 189347c6ae99SBarry Smith } 189447c6ae99SBarry Smith } 18959566063dSJacob Faibussowitsch PetscCall(MatSetValuesBlockedLocal(J, 1, &slot, cnt, cols, values, INSERT_VALUES)); 189647c6ae99SBarry Smith } 189747c6ae99SBarry Smith } 189847c6ae99SBarry Smith } 18999566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 1900e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 19019566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 19029566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 19039566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 19049566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 19059566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 190647c6ae99SBarry Smith } 19079566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 19083ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 190947c6ae99SBarry Smith } 191047c6ae99SBarry Smith 191147c6ae99SBarry Smith /* 191247c6ae99SBarry Smith This helper is for of SBAIJ preallocation, to discard the lower-triangular values which are difficult to 191347c6ae99SBarry Smith identify in the local ordering with periodic domain. 191447c6ae99SBarry Smith */ 1915d71ae5a4SJacob Faibussowitsch static PetscErrorCode L2GFilterUpperTriangular(ISLocalToGlobalMapping ltog, PetscInt *row, PetscInt *cnt, PetscInt col[]) 1916d71ae5a4SJacob Faibussowitsch { 191747c6ae99SBarry Smith PetscInt i, n; 191847c6ae99SBarry Smith 191947c6ae99SBarry Smith PetscFunctionBegin; 19209566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingApplyBlock(ltog, 1, row, row)); 19219566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingApplyBlock(ltog, *cnt, col, col)); 192247c6ae99SBarry Smith for (i = 0, n = 0; i < *cnt; i++) { 192347c6ae99SBarry Smith if (col[i] >= *row) col[n++] = col[i]; 192447c6ae99SBarry Smith } 192547c6ae99SBarry Smith *cnt = n; 19263ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 192747c6ae99SBarry Smith } 192847c6ae99SBarry Smith 1929d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_2d_MPISBAIJ(DM da, Mat J) 1930d71ae5a4SJacob Faibussowitsch { 193147c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 193247c6ae99SBarry Smith PetscInt m, n, dim, s, *cols, nc, col, cnt, *dnz, *onz; 193347c6ae99SBarry Smith PetscInt istart, iend, jstart, jend, ii, jj; 193447c6ae99SBarry Smith MPI_Comm comm; 193547c6ae99SBarry Smith PetscScalar *values; 1936bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by; 1937aa219208SBarry Smith DMDAStencilType st; 193845b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 193947c6ae99SBarry Smith 194047c6ae99SBarry Smith PetscFunctionBegin; 194147c6ae99SBarry Smith /* 194247c6ae99SBarry Smith nc - number of components per grid point 194347c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 194447c6ae99SBarry Smith */ 19459566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, NULL, NULL, NULL, NULL, &nc, &s, &bx, &by, NULL, &st)); 194647c6ae99SBarry Smith col = 2 * s + 1; 194747c6ae99SBarry Smith 19489566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &nx, &ny, NULL)); 19499566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, NULL, &gnx, &gny, NULL)); 19509566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 195147c6ae99SBarry Smith 19529566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * nc * nc, &cols)); 195347c6ae99SBarry Smith 19549566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 195547c6ae99SBarry Smith 195647c6ae99SBarry Smith /* determine the matrix preallocation information */ 1957d0609cedSBarry Smith MatPreallocateBegin(comm, nx * ny, nx * ny, dnz, onz); 195847c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1959bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1960bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 196147c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1962bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1963bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 196447c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 196547c6ae99SBarry Smith 196647c6ae99SBarry Smith /* Find block columns in block row */ 196747c6ae99SBarry Smith cnt = 0; 196847c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 196947c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 1970ad540459SPierre Jolivet if (st == DMDA_STENCIL_BOX || !ii || !jj) cols[cnt++] = slot + ii + gnx * jj; 197147c6ae99SBarry Smith } 197247c6ae99SBarry Smith } 19739566063dSJacob Faibussowitsch PetscCall(L2GFilterUpperTriangular(ltog, &slot, &cnt, cols)); 19749566063dSJacob Faibussowitsch PetscCall(MatPreallocateSymmetricSetBlock(slot, cnt, cols, dnz, onz)); 197547c6ae99SBarry Smith } 197647c6ae99SBarry Smith } 19779566063dSJacob Faibussowitsch PetscCall(MatSeqSBAIJSetPreallocation(J, nc, 0, dnz)); 19789566063dSJacob Faibussowitsch PetscCall(MatMPISBAIJSetPreallocation(J, nc, 0, dnz, 0, onz)); 1979d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 198047c6ae99SBarry Smith 19819566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 198247c6ae99SBarry Smith 198347c6ae99SBarry Smith /* 198447c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 198547c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 198647c6ae99SBarry Smith PETSc ordering. 198747c6ae99SBarry Smith */ 1988fcfd50ebSBarry Smith if (!da->prealloc_only) { 19899566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * nc * nc, &values)); 199047c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 1991bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 1992bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 199347c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 1994bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 1995bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 199647c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys); 199747c6ae99SBarry Smith 199847c6ae99SBarry Smith /* Find block columns in block row */ 199947c6ae99SBarry Smith cnt = 0; 200047c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 200147c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 2002ad540459SPierre Jolivet if (st == DMDA_STENCIL_BOX || !ii || !jj) cols[cnt++] = slot + ii + gnx * jj; 200347c6ae99SBarry Smith } 200447c6ae99SBarry Smith } 20059566063dSJacob Faibussowitsch PetscCall(L2GFilterUpperTriangular(ltog, &slot, &cnt, cols)); 20069566063dSJacob Faibussowitsch PetscCall(MatSetValuesBlocked(J, 1, &slot, cnt, cols, values, INSERT_VALUES)); 200747c6ae99SBarry Smith } 200847c6ae99SBarry Smith } 20099566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 2010e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 20119566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 20129566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 20139566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 20149566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 20159566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 201647c6ae99SBarry Smith } 20179566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 20183ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 201947c6ae99SBarry Smith } 202047c6ae99SBarry Smith 2021d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPISBAIJ(DM da, Mat J) 2022d71ae5a4SJacob Faibussowitsch { 202347c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 202447c6ae99SBarry Smith PetscInt m, n, dim, s, *cols, k, nc, col, cnt, p, *dnz, *onz; 202547c6ae99SBarry Smith PetscInt istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk; 202647c6ae99SBarry Smith MPI_Comm comm; 202747c6ae99SBarry Smith PetscScalar *values; 2028bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by, bz; 2029aa219208SBarry Smith DMDAStencilType st; 203045b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 203147c6ae99SBarry Smith 203247c6ae99SBarry Smith PetscFunctionBegin; 203347c6ae99SBarry Smith /* 203447c6ae99SBarry Smith nc - number of components per grid point 203547c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 203647c6ae99SBarry Smith */ 20379566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, NULL, NULL, NULL, &nc, &s, &bx, &by, &bz, &st)); 203847c6ae99SBarry Smith col = 2 * s + 1; 203947c6ae99SBarry Smith 20409566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 20419566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 20429566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 204347c6ae99SBarry Smith 204447c6ae99SBarry Smith /* create the matrix */ 20459566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * col, &cols)); 204647c6ae99SBarry Smith 20479566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 204847c6ae99SBarry Smith 204947c6ae99SBarry Smith /* determine the matrix preallocation information */ 2050d0609cedSBarry Smith MatPreallocateBegin(comm, nx * ny * nz, nx * ny * nz, dnz, onz); 205147c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 2052bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 2053bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 205447c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 2055bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 2056bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 205747c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 2058bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 2059bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 206047c6ae99SBarry Smith 206147c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 206247c6ae99SBarry Smith 206347c6ae99SBarry Smith /* Find block columns in block row */ 206447c6ae99SBarry Smith cnt = 0; 206547c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 206647c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 206747c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 2068ad540459SPierre Jolivet if ((st == DMDA_STENCIL_BOX) || (!ii && !jj) || (!jj && !kk) || (!ii && !kk)) cols[cnt++] = slot + ii + gnx * jj + gnx * gny * kk; 206947c6ae99SBarry Smith } 207047c6ae99SBarry Smith } 207147c6ae99SBarry Smith } 20729566063dSJacob Faibussowitsch PetscCall(L2GFilterUpperTriangular(ltog, &slot, &cnt, cols)); 20739566063dSJacob Faibussowitsch PetscCall(MatPreallocateSymmetricSetBlock(slot, cnt, cols, dnz, onz)); 207447c6ae99SBarry Smith } 207547c6ae99SBarry Smith } 207647c6ae99SBarry Smith } 20779566063dSJacob Faibussowitsch PetscCall(MatSeqSBAIJSetPreallocation(J, nc, 0, dnz)); 20789566063dSJacob Faibussowitsch PetscCall(MatMPISBAIJSetPreallocation(J, nc, 0, dnz, 0, onz)); 2079d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 208047c6ae99SBarry Smith 20819566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 208247c6ae99SBarry Smith 208347c6ae99SBarry Smith /* 208447c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 208547c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 208647c6ae99SBarry Smith PETSc ordering. 208747c6ae99SBarry Smith */ 2088fcfd50ebSBarry Smith if (!da->prealloc_only) { 20899566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(col * col * col * nc * nc, &values)); 209047c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 2091bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 2092bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 209347c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 2094bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 2095bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 209647c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 2097bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 2098bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 209947c6ae99SBarry Smith 210047c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 210147c6ae99SBarry Smith 210247c6ae99SBarry Smith cnt = 0; 210347c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 210447c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 210547c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 2106ad540459SPierre Jolivet if ((st == DMDA_STENCIL_BOX) || (!ii && !jj) || (!jj && !kk) || (!ii && !kk)) cols[cnt++] = slot + ii + gnx * jj + gnx * gny * kk; 210747c6ae99SBarry Smith } 210847c6ae99SBarry Smith } 210947c6ae99SBarry Smith } 21109566063dSJacob Faibussowitsch PetscCall(L2GFilterUpperTriangular(ltog, &slot, &cnt, cols)); 21119566063dSJacob Faibussowitsch PetscCall(MatSetValuesBlocked(J, 1, &slot, cnt, cols, values, INSERT_VALUES)); 211247c6ae99SBarry Smith } 211347c6ae99SBarry Smith } 211447c6ae99SBarry Smith } 21159566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 2116e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 21179566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 21189566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 21199566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 21209566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 21219566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 212247c6ae99SBarry Smith } 21239566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 21243ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 212547c6ae99SBarry Smith } 212647c6ae99SBarry Smith 212747c6ae99SBarry Smith /* ---------------------------------------------------------------------------------*/ 212847c6ae99SBarry Smith 2129d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix_DA_3d_MPIAIJ_Fill(DM da, Mat J) 2130d71ae5a4SJacob Faibussowitsch { 213147c6ae99SBarry Smith PetscInt xs, ys, nx, ny, i, j, slot, gxs, gys, gnx, gny; 2132c0ab637bSBarry Smith PetscInt m, n, dim, s, *cols, k, nc, row, col, cnt, maxcnt = 0, l, p, *dnz, *onz; 2133c1154cd5SBarry Smith PetscInt istart, iend, jstart, jend, kstart, kend, zs, nz, gzs, gnz, ii, jj, kk, M, N, P; 213447c6ae99SBarry Smith DM_DA *dd = (DM_DA *)da->data; 213547c6ae99SBarry Smith PetscInt ifill_col, *dfill = dd->dfill, *ofill = dd->ofill; 213647c6ae99SBarry Smith MPI_Comm comm; 213747c6ae99SBarry Smith PetscScalar *values; 2138bff4a2f0SMatthew G. Knepley DMBoundaryType bx, by, bz; 213945b6f7e9SBarry Smith ISLocalToGlobalMapping ltog; 2140aa219208SBarry Smith DMDAStencilType st; 2141c1154cd5SBarry Smith PetscBool removedups = PETSC_FALSE; 214247c6ae99SBarry Smith 214347c6ae99SBarry Smith PetscFunctionBegin; 214447c6ae99SBarry Smith /* 214547c6ae99SBarry Smith nc - number of components per grid point 214647c6ae99SBarry Smith col - number of colors needed in one direction for single component problem 214747c6ae99SBarry Smith */ 21489566063dSJacob Faibussowitsch PetscCall(DMDAGetInfo(da, &dim, &m, &n, &p, &M, &N, &P, &nc, &s, &bx, &by, &bz, &st)); 214947c6ae99SBarry Smith col = 2 * s + 1; 21501dca8a05SBarry 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\ 215147c6ae99SBarry Smith by 2*stencil_width + 1\n"); 21521dca8a05SBarry 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\ 215347c6ae99SBarry Smith by 2*stencil_width + 1\n"); 21541dca8a05SBarry 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\ 215547c6ae99SBarry Smith by 2*stencil_width + 1\n"); 215647c6ae99SBarry Smith 2157c1154cd5SBarry Smith /* 2158c1154cd5SBarry Smith With one processor in periodic domains in a skinny dimension the code will label nonzero columns multiple times 2159c1154cd5SBarry Smith because of "wrapping" around the end of the domain hitting an entry already counted in the other direction. 2160c1154cd5SBarry Smith */ 2161c1154cd5SBarry Smith if (M == 1 && 2 * s >= m) removedups = PETSC_TRUE; 2162c1154cd5SBarry Smith if (N == 1 && 2 * s >= n) removedups = PETSC_TRUE; 2163c1154cd5SBarry Smith if (P == 1 && 2 * s >= p) removedups = PETSC_TRUE; 2164c1154cd5SBarry Smith 21659566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da, &xs, &ys, &zs, &nx, &ny, &nz)); 21669566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(da, &gxs, &gys, &gzs, &gnx, &gny, &gnz)); 21679566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)da, &comm)); 216847c6ae99SBarry Smith 21699566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(col * col * col * nc, &cols)); 21709566063dSJacob Faibussowitsch PetscCall(DMGetLocalToGlobalMapping(da, <og)); 217147c6ae99SBarry Smith 217247c6ae99SBarry Smith /* determine the matrix preallocation information */ 2173d0609cedSBarry Smith MatPreallocateBegin(comm, nc * nx * ny * nz, nc * nx * ny * nz, dnz, onz); 217447c6ae99SBarry Smith 21759566063dSJacob Faibussowitsch PetscCall(MatSetBlockSize(J, nc)); 217647c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 2177bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 2178bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 217947c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 2180bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 2181bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 218247c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 2183bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 2184bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 218547c6ae99SBarry Smith 218647c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 218747c6ae99SBarry Smith 218847c6ae99SBarry Smith for (l = 0; l < nc; l++) { 218947c6ae99SBarry Smith cnt = 0; 219047c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 219147c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 219247c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 219347c6ae99SBarry Smith if (ii || jj || kk) { 2194aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 21958865f1eaSKarl 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); 219647c6ae99SBarry Smith } 219747c6ae99SBarry Smith } else { 219847c6ae99SBarry Smith if (dfill) { 21998865f1eaSKarl 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); 220047c6ae99SBarry Smith } else { 22018865f1eaSKarl Rupp for (ifill_col = 0; ifill_col < nc; ifill_col++) cols[cnt++] = ifill_col + nc * (slot + ii + gnx * jj + gnx * gny * kk); 220247c6ae99SBarry Smith } 220347c6ae99SBarry Smith } 220447c6ae99SBarry Smith } 220547c6ae99SBarry Smith } 220647c6ae99SBarry Smith } 220747c6ae99SBarry Smith row = l + nc * (slot); 2208c0ab637bSBarry Smith maxcnt = PetscMax(maxcnt, cnt); 22091baa6e33SBarry Smith if (removedups) PetscCall(MatPreallocateSetLocalRemoveDups(ltog, 1, &row, ltog, cnt, cols, dnz, onz)); 22101baa6e33SBarry Smith else PetscCall(MatPreallocateSetLocal(ltog, 1, &row, ltog, cnt, cols, dnz, onz)); 221147c6ae99SBarry Smith } 221247c6ae99SBarry Smith } 221347c6ae99SBarry Smith } 2214c1154cd5SBarry Smith } 22159566063dSJacob Faibussowitsch PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz)); 22169566063dSJacob Faibussowitsch PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz)); 2217d0609cedSBarry Smith MatPreallocateEnd(dnz, onz); 22189566063dSJacob Faibussowitsch PetscCall(MatSetLocalToGlobalMapping(J, ltog, ltog)); 221947c6ae99SBarry Smith 222047c6ae99SBarry Smith /* 222147c6ae99SBarry Smith For each node in the grid: we get the neighbors in the local (on processor ordering 222247c6ae99SBarry Smith that includes the ghost points) then MatSetValuesLocal() maps those indices to the global 222347c6ae99SBarry Smith PETSc ordering. 222447c6ae99SBarry Smith */ 2225fcfd50ebSBarry Smith if (!da->prealloc_only) { 22269566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(maxcnt, &values)); 222747c6ae99SBarry Smith for (i = xs; i < xs + nx; i++) { 2228bff4a2f0SMatthew G. Knepley istart = (bx == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -i)); 2229bff4a2f0SMatthew G. Knepley iend = (bx == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, m - i - 1)); 223047c6ae99SBarry Smith for (j = ys; j < ys + ny; j++) { 2231bff4a2f0SMatthew G. Knepley jstart = (by == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -j)); 2232bff4a2f0SMatthew G. Knepley jend = (by == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, n - j - 1)); 223347c6ae99SBarry Smith for (k = zs; k < zs + nz; k++) { 2234bff4a2f0SMatthew G. Knepley kstart = (bz == DM_BOUNDARY_PERIODIC) ? -s : (PetscMax(-s, -k)); 2235bff4a2f0SMatthew G. Knepley kend = (bz == DM_BOUNDARY_PERIODIC) ? s : (PetscMin(s, p - k - 1)); 223647c6ae99SBarry Smith 223747c6ae99SBarry Smith slot = i - gxs + gnx * (j - gys) + gnx * gny * (k - gzs); 223847c6ae99SBarry Smith 223947c6ae99SBarry Smith for (l = 0; l < nc; l++) { 224047c6ae99SBarry Smith cnt = 0; 224147c6ae99SBarry Smith for (ii = istart; ii < iend + 1; ii++) { 224247c6ae99SBarry Smith for (jj = jstart; jj < jend + 1; jj++) { 224347c6ae99SBarry Smith for (kk = kstart; kk < kend + 1; kk++) { 224447c6ae99SBarry Smith if (ii || jj || kk) { 2245aa219208SBarry Smith if ((st == DMDA_STENCIL_BOX) || ((!ii && !jj) || (!jj && !kk) || (!ii && !kk))) { /* entries on star*/ 22468865f1eaSKarl 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); 224747c6ae99SBarry Smith } 224847c6ae99SBarry Smith } else { 224947c6ae99SBarry Smith if (dfill) { 22508865f1eaSKarl 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); 225147c6ae99SBarry Smith } else { 22528865f1eaSKarl Rupp for (ifill_col = 0; ifill_col < nc; ifill_col++) cols[cnt++] = ifill_col + nc * (slot + ii + gnx * jj + gnx * gny * kk); 225347c6ae99SBarry Smith } 225447c6ae99SBarry Smith } 225547c6ae99SBarry Smith } 225647c6ae99SBarry Smith } 225747c6ae99SBarry Smith } 225847c6ae99SBarry Smith row = l + nc * (slot); 22599566063dSJacob Faibussowitsch PetscCall(MatSetValuesLocal(J, 1, &row, cnt, cols, values, INSERT_VALUES)); 226047c6ae99SBarry Smith } 226147c6ae99SBarry Smith } 226247c6ae99SBarry Smith } 226347c6ae99SBarry Smith } 22649566063dSJacob Faibussowitsch PetscCall(PetscFree(values)); 2265e7e92044SBarry Smith /* do not copy values to GPU since they are all zero and not yet needed there */ 22669566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_TRUE)); 22679566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 22689566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 22699566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(J, PETSC_FALSE)); 22709566063dSJacob Faibussowitsch PetscCall(MatSetOption(J, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE)); 227147c6ae99SBarry Smith } 22729566063dSJacob Faibussowitsch PetscCall(PetscFree(cols)); 22733ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 227447c6ae99SBarry Smith } 2275