Actual source code: matpreallocator.c

  1: #include <petsc/private/matimpl.h>
  2: #include <petsc/private/hashsetij.h>

  4: typedef struct {
  5:   PetscHSetIJ ht;
  6:   PetscInt   *dnz, *onz;
  7:   PetscInt   *dnzu, *onzu;
  8:   PetscBool   used;
  9: } Mat_Preallocator;

 11: static PetscErrorCode MatDestroy_Preallocator(Mat A)
 12: {
 13:   Mat_Preallocator *p = (Mat_Preallocator *)A->data;

 15:   PetscFunctionBegin;
 16:   PetscCall(MatStashDestroy_Private(&A->stash));
 17:   PetscCall(PetscHSetIJDestroy(&p->ht));
 18:   PetscCall(PetscFree4(p->dnz, p->onz, p->dnzu, p->onzu));
 19:   PetscCall(PetscFree(A->data));
 20:   PetscCall(PetscObjectChangeTypeName((PetscObject)A, NULL));
 21:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatPreallocatorPreallocate_C", NULL));
 22:   PetscFunctionReturn(PETSC_SUCCESS);
 23: }

 25: static PetscErrorCode MatSetUp_Preallocator(Mat A)
 26: {
 27:   Mat_Preallocator *p = (Mat_Preallocator *)A->data;
 28:   PetscInt          m, bs, mbs;

 30:   PetscFunctionBegin;
 31:   PetscCall(PetscLayoutSetUp(A->rmap));
 32:   PetscCall(PetscLayoutSetUp(A->cmap));
 33:   PetscCall(MatGetLocalSize(A, &m, NULL));
 34:   PetscCall(PetscHSetIJCreate(&p->ht));
 35:   PetscCall(MatGetBlockSize(A, &bs));
 36:   /* Do not bother bstash since MatPreallocator does not implement MatSetValuesBlocked */
 37:   PetscCall(MatStashCreate_Private(PetscObjectComm((PetscObject)A), 1, &A->stash));
 38:   /* arrays are for blocked rows/cols */
 39:   mbs = m / bs;
 40:   PetscCall(PetscCalloc4(mbs, &p->dnz, mbs, &p->onz, mbs, &p->dnzu, mbs, &p->onzu));
 41:   PetscFunctionReturn(PETSC_SUCCESS);
 42: }

 44: static PetscErrorCode MatSetValues_Preallocator(Mat A, PetscInt m, const PetscInt *rows, PetscInt n, const PetscInt *cols, const PetscScalar *values, InsertMode addv)
 45: {
 46:   Mat_Preallocator *p = (Mat_Preallocator *)A->data;
 47:   PetscInt          rStart, rEnd, r, cStart, cEnd, c, bs;

 49:   PetscFunctionBegin;
 50:   PetscCall(MatGetBlockSize(A, &bs));
 51:   PetscCall(MatGetOwnershipRange(A, &rStart, &rEnd));
 52:   PetscCall(MatGetOwnershipRangeColumn(A, &cStart, &cEnd));
 53:   for (r = 0; r < m; ++r) {
 54:     PetscHashIJKey key;
 55:     PetscBool      missing;

 57:     key.i = rows[r];
 58:     if (key.i < 0) continue;
 59:     if (key.i < rStart || key.i >= rEnd) {
 60:       PetscCall(MatStashValuesRow_Private(&A->stash, key.i, n, cols, values, PETSC_FALSE));
 61:     } else { /* Hash table is for blocked rows/cols */
 62:       key.i = rows[r] / bs;
 63:       for (c = 0; c < n; ++c) {
 64:         key.j = cols[c] / bs;
 65:         if (key.j < 0) continue;
 66:         PetscCall(PetscHSetIJQueryAdd(p->ht, key, &missing));
 67:         if (missing) {
 68:           if (key.j >= cStart / bs && key.j < cEnd / bs) {
 69:             ++p->dnz[key.i - rStart / bs];
 70:             if (key.j >= key.i) ++p->dnzu[key.i - rStart / bs];
 71:           } else {
 72:             ++p->onz[key.i - rStart / bs];
 73:             if (key.j >= key.i) ++p->onzu[key.i - rStart / bs];
 74:           }
 75:         }
 76:       }
 77:     }
 78:   }
 79:   PetscFunctionReturn(PETSC_SUCCESS);
 80: }

 82: static PetscErrorCode MatAssemblyBegin_Preallocator(Mat A, MatAssemblyType type)
 83: {
 84:   PetscInt nstash, reallocs;

 86:   PetscFunctionBegin;
 87:   PetscCall(MatStashScatterBegin_Private(A, &A->stash, A->rmap->range));
 88:   PetscCall(MatStashGetInfo_Private(&A->stash, &nstash, &reallocs));
 89:   PetscCall(PetscInfo(A, "Stash has %" PetscInt_FMT " entries, uses %" PetscInt_FMT " mallocs.\n", nstash, reallocs));
 90:   PetscFunctionReturn(PETSC_SUCCESS);
 91: }

 93: static PetscErrorCode MatAssemblyEnd_Preallocator(Mat A, MatAssemblyType type)
 94: {
 95:   PetscScalar *val;
 96:   PetscInt    *row, *col;
 97:   PetscInt     i, j, rstart, ncols, flg;
 98:   PetscMPIInt  n;

100:   PetscFunctionBegin;
101:   while (1) {
102:     PetscCall(MatStashScatterGetMesg_Private(&A->stash, &n, &row, &col, &val, &flg));
103:     if (!flg) break;

105:     for (i = 0; i < n;) {
106:       /* Now identify the consecutive vals belonging to the same row */
107:       for (j = i, rstart = row[j]; j < n; j++) {
108:         if (row[j] != rstart) break;
109:       }
110:       if (j < n) ncols = j - i;
111:       else ncols = n - i;
112:       /* Now assemble all these values with a single function call */
113:       PetscCall(MatSetValues_Preallocator(A, 1, row + i, ncols, col + i, val + i, INSERT_VALUES));
114:       i = j;
115:     }
116:   }
117:   PetscCall(MatStashScatterEnd_Private(&A->stash));
118:   PetscFunctionReturn(PETSC_SUCCESS);
119: }

121: static PetscErrorCode MatPreallocatorPreallocate_Preallocator(Mat mat, PetscBool fill, Mat A)
122: {
123:   Mat_Preallocator *p = (Mat_Preallocator *)mat->data;
124:   PetscInt          bs;

126:   PetscFunctionBegin;
127:   PetscCheck(!p->used, PetscObjectComm((PetscObject)mat), PETSC_ERR_SUP, "MatPreallocatorPreallocate() can only be used once for a give MatPreallocator object. Consider using MatDuplicate() after preallocation.");
128:   p->used = PETSC_TRUE;
129:   if (!fill) PetscCall(PetscHSetIJDestroy(&p->ht));
130:   PetscCall(MatGetBlockSize(mat, &bs));
131:   PetscCall(MatXAIJSetPreallocation(A, bs, p->dnz, p->onz, p->dnzu, p->onzu));
132:   PetscCall(MatSetUp(A));
133:   PetscCall(MatSetOption(A, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_TRUE));
134:   if (fill) {
135:     PetscHashIter   hi;
136:     PetscHashIJKey  key;
137:     PetscScalar    *zeros = NULL;
138:     PetscInt        n, maxrow = 1, *cols, rStart, rEnd, *rowstarts;
139:     const PetscBool nooffprocentries = A->nooffprocentries;

141:     PetscCall(MatGetOwnershipRange(A, &rStart, &rEnd));
142:     // Ownership range is in terms of scalar entries, but we deal with blocks
143:     rStart /= bs;
144:     rEnd /= bs;
145:     PetscCall(PetscHSetIJGetSize(p->ht, &n));
146:     PetscCall(PetscMalloc2(n, &cols, rEnd - rStart + 1, &rowstarts));
147:     rowstarts[0] = 0;
148:     for (PetscInt i = 0; i < rEnd - rStart; i++) {
149:       rowstarts[i + 1] = rowstarts[i] + p->dnz[i] + p->onz[i];
150:       maxrow           = PetscMax(maxrow, p->dnz[i] + p->onz[i]);
151:     }
152:     PetscCheck(rowstarts[rEnd - rStart] == n, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Hash claims %" PetscInt_FMT " entries, but dnz+onz counts %" PetscInt_FMT, n, rowstarts[rEnd - rStart]);

154:     PetscHashIterBegin(p->ht, hi);
155:     while (!PetscHashIterAtEnd(p->ht, hi)) {
156:       PetscHashIterGetKey(p->ht, hi, key);
157:       PetscInt lrow         = key.i - rStart;
158:       cols[rowstarts[lrow]] = key.j;
159:       rowstarts[lrow]++;
160:       PetscHashIterNext(p->ht, hi);
161:     }
162:     PetscCall(PetscHSetIJDestroy(&p->ht));

164:     if (!A->structure_only) PetscCall(PetscCalloc1(maxrow * bs * bs, &zeros));
165:     for (PetscInt i = 0; i < rEnd - rStart; i++) {
166:       PetscInt grow = rStart + i;
167:       PetscInt end = rowstarts[i], start = end - p->dnz[i] - p->onz[i];
168:       PetscCall(PetscSortInt(end - start, PetscSafePointerPlusOffset(cols, start)));
169:       PetscCall(MatSetValuesBlocked(A, 1, &grow, end - start, PetscSafePointerPlusOffset(cols, start), zeros, INSERT_VALUES));
170:     }
171:     PetscCall(PetscFree(zeros));
172:     PetscCall(PetscFree2(cols, rowstarts));

174:     A->nooffprocentries = PETSC_TRUE;
175:     PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
176:     PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
177:     A->nooffprocentries = nooffprocentries;
178:   }
179:   PetscFunctionReturn(PETSC_SUCCESS);
180: }

182: /*@
183:   MatPreallocatorPreallocate - Preallocates the A matrix, using information from a `MATPREALLOCATOR` mat, optionally filling A with zeros

185:   Input Parameters:
186: + mat  - the `MATPREALLOCATOR` preallocator matrix
187: . fill - fill the matrix with zeros
188: - A    - the matrix to be preallocated

190:   Notes:
191:   This `MatType` implementation provides a helper utility to define the correct
192:   preallocation data for a given nonzero structure. Use this object like a
193:   regular matrix, e.g. loop over the nonzero structure of the matrix and
194:   call `MatSetValues()` or `MatSetValuesBlocked()` to indicate the nonzero locations.
195:   The matrix entries provided to `MatSetValues()` will be ignored, it only uses
196:   the row / col indices provided to determine the information required to be
197:   passed to `MatXAIJSetPreallocation()`. Once you have looped over the nonzero
198:   structure, you must call `MatAssemblyBegin()`, `MatAssemblyEnd()` on mat.

200:   After you have assembled the preallocator matrix (mat), call `MatPreallocatorPreallocate()`
201:   to define the preallocation information on the matrix (A). Setting the parameter
202:   fill = PETSC_TRUE will insert zeros into the matrix A. Internally `MatPreallocatorPreallocate()`
203:   will call `MatSetOption`(A, `MAT_NEW_NONZERO_ALLOCATION_ERR`, `PETSC_TRUE`);

205:   This function may only be called once for a given `MATPREALLOCATOR` object. If
206:   multiple `Mat`s need to be preallocated, consider using `MatDuplicate()` after
207:   this function.

209:   Level: advanced

211: .seealso: `MATPREALLOCATOR`, `MatXAIJSetPreallocation()`
212: @*/
213: PetscErrorCode MatPreallocatorPreallocate(Mat mat, PetscBool fill, Mat A)
214: {
215:   PetscFunctionBegin;
219:   PetscUseMethod(mat, "MatPreallocatorPreallocate_C", (Mat, PetscBool, Mat), (mat, fill, A));
220:   PetscFunctionReturn(PETSC_SUCCESS);
221: }

223: /*MC
224:    MATPREALLOCATOR - MATPREALLOCATOR = "preallocator" - A matrix type to be used for computing a matrix preallocation.

226:    Operations Provided:
227: .vb
228:   MatSetValues()
229: .ve

231:    Options Database Keys:
232: . -mat_type preallocator - sets the matrix type to `MATPREALLOCATOR` during a call to `MatSetFromOptions()`

234:   Level: advanced

236: .seealso: `MATPREALLOCATOR`, `Mat`, `MatPreallocatorPreallocate()`
237: M*/

239: PETSC_EXTERN PetscErrorCode MatCreate_Preallocator(Mat A)
240: {
241:   Mat_Preallocator *p;

243:   PetscFunctionBegin;
244:   PetscCall(PetscNew(&p));
245:   A->data = (void *)p;

247:   p->ht   = NULL;
248:   p->dnz  = NULL;
249:   p->onz  = NULL;
250:   p->dnzu = NULL;
251:   p->onzu = NULL;
252:   p->used = PETSC_FALSE;

254:   /* matrix ops */
255:   PetscCall(PetscMemzero(A->ops, sizeof(struct _MatOps)));

257:   A->ops->destroy       = MatDestroy_Preallocator;
258:   A->ops->setup         = MatSetUp_Preallocator;
259:   A->ops->setvalues     = MatSetValues_Preallocator;
260:   A->ops->assemblybegin = MatAssemblyBegin_Preallocator;
261:   A->ops->assemblyend   = MatAssemblyEnd_Preallocator;
262:   A->ops->setblocksizes = MatSetBlockSizes_Default; /* once set, user is not allowed to change the block sizes */

264:   /* special MATPREALLOCATOR functions */
265:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatPreallocatorPreallocate_C", MatPreallocatorPreallocate_Preallocator));
266:   PetscCall(PetscObjectChangeTypeName((PetscObject)A, MATPREALLOCATOR));
267:   PetscFunctionReturn(PETSC_SUCCESS);
268: }