Actual source code: normm.c

  1: #include <../src/mat/impls/shell/shell.h>

  3: typedef struct {
  4:   Mat A;
  5:   Mat D; /* local submatrix for diagonal part */
  6:   Vec w;
  7: } Mat_Normal;

  9: static PetscErrorCode MatIncreaseOverlap_Normal(Mat A, PetscInt is_max, IS is[], PetscInt ov)
 10: {
 11:   Mat_Normal *a;
 12:   Mat         pattern;

 14:   PetscFunctionBegin;
 15:   PetscCheck(ov >= 0, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_OUTOFRANGE, "Negative overlap specified");
 16:   PetscCall(MatShellGetContext(A, &a));
 17:   PetscCall(MatProductCreate(a->A, a->A, NULL, &pattern));
 18:   PetscCall(MatSetOption(pattern, MAT_STRUCTURE_ONLY, PETSC_TRUE));
 19:   PetscCall(MatProductSetType(pattern, MATPRODUCT_AtB));
 20:   PetscCall(MatProductSetFromOptions(pattern));
 21:   PetscCall(MatProductSymbolic(pattern));
 22:   /* release symbolic product workspace before increasing overlap */
 23:   PetscCall(MatProductClear(pattern));
 24:   PetscCall(MatIncreaseOverlap(pattern, is_max, is, ov));
 25:   PetscCall(MatDestroy(&pattern));
 26:   PetscFunctionReturn(PETSC_SUCCESS);
 27: }

 29: static PetscErrorCode MatCreateSubMatrices_Normal(Mat mat, PetscInt n, const IS irow[], const IS icol[], MatReuse scall, Mat *submat[])
 30: {
 31:   Mat_Normal *a;
 32:   Mat         B, *suba;
 33:   IS         *row;
 34:   PetscScalar shift, scale;
 35:   PetscInt    M;

 37:   PetscFunctionBegin;
 38:   PetscCheck(irow == icol, PetscObjectComm((PetscObject)mat), PETSC_ERR_SUP, "Not implemented");
 39:   PetscCall(MatShellGetScalingShifts(mat, &shift, &scale, (Vec *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Mat *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED));
 40:   PetscCall(MatShellGetContext(mat, &a));
 41:   B = a->A;
 42:   if (scall != MAT_REUSE_MATRIX) PetscCall(PetscCalloc1(n, submat));
 43:   PetscCall(MatGetSize(B, &M, NULL));
 44:   PetscCall(PetscMalloc1(n, &row));
 45:   PetscCall(ISCreateStride(PETSC_COMM_SELF, M, 0, 1, &row[0]));
 46:   PetscCall(ISSetIdentity(row[0]));
 47:   for (M = 1; M < n; ++M) row[M] = row[0];
 48:   PetscCall(MatCreateSubMatrices(B, n, row, icol, MAT_INITIAL_MATRIX, &suba));
 49:   for (M = 0; M < n; ++M) {
 50:     PetscCall(MatCreateNormal(suba[M], *submat + M));
 51:     PetscCall(MatShift((*submat)[M], shift));
 52:     PetscCall(MatScale((*submat)[M], scale));
 53:   }
 54:   PetscCall(ISDestroy(&row[0]));
 55:   PetscCall(PetscFree(row));
 56:   PetscCall(MatDestroySubMatrices(n, &suba));
 57:   PetscFunctionReturn(PETSC_SUCCESS);
 58: }

 60: static PetscErrorCode MatPermute_Normal(Mat A, IS rowp, IS colp, Mat *B)
 61: {
 62:   Mat_Normal *a;
 63:   Mat         C, Aa;
 64:   IS          row;
 65:   PetscScalar shift, scale;

 67:   PetscFunctionBegin;
 68:   PetscCheck(rowp == colp, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_INCOMP, "Row permutation and column permutation must be the same");
 69:   PetscCall(MatShellGetScalingShifts(A, &shift, &scale, (Vec *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Mat *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED));
 70:   PetscCall(MatShellGetContext(A, &a));
 71:   Aa = a->A;
 72:   PetscCall(ISCreateStride(PetscObjectComm((PetscObject)Aa), Aa->rmap->n, Aa->rmap->rstart, 1, &row));
 73:   PetscCall(ISSetIdentity(row));
 74:   PetscCall(MatPermute(Aa, row, colp, &C));
 75:   PetscCall(ISDestroy(&row));
 76:   PetscCall(MatCreateNormal(C, B));
 77:   PetscCall(MatDestroy(&C));
 78:   PetscCall(MatShift(*B, shift));
 79:   PetscCall(MatScale(*B, scale));
 80:   PetscFunctionReturn(PETSC_SUCCESS);
 81: }

 83: static PetscErrorCode MatDuplicate_Normal(Mat A, MatDuplicateOption op, Mat *B)
 84: {
 85:   Mat_Normal *a;
 86:   Mat         C;

 88:   PetscFunctionBegin;
 89:   PetscCall(MatShellGetContext(A, &a));
 90:   PetscCall(MatDuplicate(a->A, op, &C));
 91:   PetscCall(MatCreateNormal(C, B));
 92:   PetscCall(MatDestroy(&C));
 93:   if (op == MAT_COPY_VALUES) PetscCall(MatCopy(A, *B, SAME_NONZERO_PATTERN));
 94:   PetscFunctionReturn(PETSC_SUCCESS);
 95: }

 97: static PetscErrorCode MatCopy_Normal(Mat A, Mat B, MatStructure str)
 98: {
 99:   Mat_Normal *a, *b;

101:   PetscFunctionBegin;
102:   PetscCall(MatShellGetContext(A, &a));
103:   PetscCall(MatShellGetContext(B, &b));
104:   PetscCall(MatCopy(a->A, b->A, str));
105:   PetscFunctionReturn(PETSC_SUCCESS);
106: }

108: static PetscErrorCode MatMult_Normal(Mat N, Vec x, Vec y)
109: {
110:   Mat_Normal *Na;

112:   PetscFunctionBegin;
113:   PetscCall(MatShellGetContext(N, &Na));
114:   PetscCall(MatMult(Na->A, x, Na->w));
115:   PetscCall(MatMultTranspose(Na->A, Na->w, y));
116:   PetscFunctionReturn(PETSC_SUCCESS);
117: }

119: static PetscErrorCode MatDestroy_Normal(Mat N)
120: {
121:   Mat_Normal *Na;

123:   PetscFunctionBegin;
124:   PetscCall(MatShellGetContext(N, &Na));
125:   PetscCall(MatDestroy(&Na->A));
126:   PetscCall(MatDestroy(&Na->D));
127:   PetscCall(VecDestroy(&Na->w));
128:   PetscCall(PetscFree(Na));
129:   PetscCall(PetscObjectComposeFunction((PetscObject)N, "MatNormalGetMat_C", NULL));
130:   PetscCall(PetscObjectComposeFunction((PetscObject)N, "MatConvert_normal_seqaij_C", NULL));
131:   PetscCall(PetscObjectComposeFunction((PetscObject)N, "MatConvert_normal_mpiaij_C", NULL));
132: #if PetscDefined(HAVE_HYPRE)
133:   PetscCall(PetscObjectComposeFunction((PetscObject)N, "MatConvert_normal_hypre_C", NULL));
134: #endif
135:   PetscCall(PetscObjectComposeFunction((PetscObject)N, "MatProductSetFromOptions_normal_seqdense_C", NULL));
136:   PetscCall(PetscObjectComposeFunction((PetscObject)N, "MatProductSetFromOptions_normal_mpidense_C", NULL));
137:   PetscCall(PetscObjectComposeFunction((PetscObject)N, "MatShellSetContext_C", NULL));
138:   PetscFunctionReturn(PETSC_SUCCESS);
139: }

141: /*
142:       Slow, nonscalable version
143: */
144: static PetscErrorCode MatGetDiagonal_Normal(Mat N, Vec v)
145: {
146:   Mat_Normal        *Na;
147:   Mat                A;
148:   PetscInt           i, j, rstart, rend, nnz;
149:   const PetscInt    *cols;
150:   PetscScalar       *work, *values;
151:   const PetscScalar *mvalues;

153:   PetscFunctionBegin;
154:   PetscCall(MatShellGetContext(N, &Na));
155:   A = Na->A;
156:   PetscCall(PetscMalloc1(A->cmap->N, &work));
157:   PetscCall(PetscArrayzero(work, A->cmap->N));
158:   PetscCall(MatGetOwnershipRange(A, &rstart, &rend));
159:   for (i = rstart; i < rend; i++) {
160:     PetscCall(MatGetRow(A, i, &nnz, &cols, &mvalues));
161:     for (j = 0; j < nnz; j++) work[cols[j]] += mvalues[j] * mvalues[j];
162:     PetscCall(MatRestoreRow(A, i, &nnz, &cols, &mvalues));
163:   }
164:   PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, work, A->cmap->N, MPIU_SCALAR, MPIU_SUM, PetscObjectComm((PetscObject)N)));
165:   rstart = N->cmap->rstart;
166:   rend   = N->cmap->rend;
167:   PetscCall(VecGetArray(v, &values));
168:   PetscCall(PetscArraycpy(values, work + rstart, rend - rstart));
169:   PetscCall(VecRestoreArray(v, &values));
170:   PetscCall(PetscFree(work));
171:   PetscFunctionReturn(PETSC_SUCCESS);
172: }

174: static PetscErrorCode MatGetDiagonalBlock_Normal(Mat N, Mat *D)
175: {
176:   Mat_Normal *Na;
177:   Mat         M, A;

179:   PetscFunctionBegin;
180:   PetscCall(MatShellGetContext(N, &Na));
181:   A = Na->A;
182:   PetscCall(MatGetDiagonalBlock(A, &M));
183:   PetscCall(MatCreateNormal(M, &Na->D));
184:   *D = Na->D;
185:   PetscFunctionReturn(PETSC_SUCCESS);
186: }

188: static PetscErrorCode MatNormalGetMat_Normal(Mat A, Mat *M)
189: {
190:   Mat_Normal *Aa;

192:   PetscFunctionBegin;
193:   PetscCall(MatShellGetContext(A, &Aa));
194:   *M = Aa->A;
195:   PetscFunctionReturn(PETSC_SUCCESS);
196: }

198: /*@
199:   MatNormalGetMat - Gets the `Mat` object stored inside a `MATNORMAL`

201:   Logically Collective

203:   Input Parameter:
204: . A - the `MATNORMAL` matrix

206:   Output Parameter:
207: . M - the matrix object stored inside `A`

209:   Level: intermediate

211: .seealso: [](ch_matrices), `Mat`, `MATNORMAL`, `MATNORMALHERMITIAN`, `MatCreateNormal()`
212: @*/
213: PetscErrorCode MatNormalGetMat(Mat A, Mat *M)
214: {
215:   PetscFunctionBegin;
218:   PetscAssertPointer(M, 2);
219:   PetscUseMethod(A, "MatNormalGetMat_C", (Mat, Mat *), (A, M));
220:   PetscFunctionReturn(PETSC_SUCCESS);
221: }

223: static PetscErrorCode MatConvert_Normal_AIJ(Mat A, MatType newtype, MatReuse reuse, Mat *newmat)
224: {
225:   Mat_Normal *Aa;
226:   Mat         B;
227:   Vec         left, right, dshift;
228:   PetscScalar scale, shift;
229:   PetscInt    m, n, M, N;

231:   PetscFunctionBegin;
232:   PetscCall(MatShellGetContext(A, &Aa));
233:   PetscCall(MatShellGetScalingShifts(A, &shift, &scale, &dshift, &left, &right, (Mat *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED));
234:   PetscCall(MatGetSize(A, &M, &N));
235:   PetscCall(MatGetLocalSize(A, &m, &n));
236:   if (reuse == MAT_REUSE_MATRIX) {
237:     B = *newmat;
238:     PetscCall(MatProductReplaceMats(Aa->A, Aa->A, NULL, B));
239:   } else {
240:     PetscCall(MatProductCreate(Aa->A, Aa->A, NULL, &B));
241:     PetscCall(MatProductSetType(B, MATPRODUCT_AtB));
242:     PetscCall(MatProductSetFromOptions(B));
243:     PetscCall(MatProductSymbolic(B));
244:     PetscCall(MatSetOption(B, MAT_SYMMETRIC, PETSC_TRUE));
245:   }
246:   PetscCall(MatProductNumeric(B));
247:   if (reuse == MAT_INPLACE_MATRIX) PetscCall(MatHeaderReplace(A, &B));
248:   else if (reuse == MAT_INITIAL_MATRIX) *newmat = B;
249:   PetscCall(MatConvert(*newmat, MATAIJ, MAT_INPLACE_MATRIX, newmat));
250:   PetscCall(MatDiagonalScale(*newmat, left, right));
251:   PetscCall(MatScale(*newmat, scale));
252:   PetscCall(MatShift(*newmat, shift));
253:   if (dshift) PetscCall(MatDiagonalSet(*newmat, dshift, ADD_VALUES));
254:   PetscFunctionReturn(PETSC_SUCCESS);
255: }

257: #if PetscDefined(HAVE_HYPRE)
258: static PetscErrorCode MatConvert_Normal_HYPRE(Mat A, MatType type, MatReuse reuse, Mat *B)
259: {
260:   PetscFunctionBegin;
261:   if (reuse == MAT_INITIAL_MATRIX) {
262:     PetscCall(MatConvert(A, MATAIJ, reuse, B));
263:     PetscCall(MatConvert(*B, type, MAT_INPLACE_MATRIX, B));
264:   } else PetscCall(MatConvert_Basic(A, type, reuse, B)); /* fall back to basic convert */
265:   PetscFunctionReturn(PETSC_SUCCESS);
266: }
267: #endif

269: typedef struct {
270:   Mat work[3];
271: } Normal_Dense;

273: static PetscErrorCode MatProductNumeric_Normal_Dense(Mat C)
274: {
275:   Mat                A, B;
276:   Normal_Dense      *contents;
277:   Mat_Normal        *a;
278:   Vec                left, right;
279:   PetscScalar       *array, scale;
280:   const PetscScalar *read = NULL;
281:   PetscInt           lda;

283:   PetscFunctionBegin;
284:   MatCheckProduct(C, 1);
285:   A = C->product->A;
286:   B = C->product->B;
287:   PetscCall(MatShellGetContext(A, &a));
288:   contents = (Normal_Dense *)C->product->data;
289:   PetscCheck(contents, PetscObjectComm((PetscObject)C), PETSC_ERR_PLIB, "Product data empty");
290:   PetscCall(MatShellGetScalingShifts(A, (PetscScalar *)MAT_SHELL_NOT_ALLOWED, &scale, (Vec *)MAT_SHELL_NOT_ALLOWED, &left, &right, (Mat *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED));
291:   PetscCall(MatDenseGetLDA(C, &lda));
292:   if (right) {
293:     PetscCall(MatCopy(B, C, SAME_NONZERO_PATTERN));
294:     PetscCall(MatDiagonalScale(C, right, NULL));
295:     PetscCall(MatDenseGetArrayRead(C, &read));
296:     PetscCall(MatDenseSetLDA(contents->work[2], lda));
297:     PetscCall(MatDensePlaceArray(contents->work[2], read));
298:   }
299:   PetscCall(MatProductNumeric(contents->work[0]));
300:   if (right) {
301:     PetscCall(MatDenseResetArray(contents->work[2]));
302:     PetscCall(MatDenseRestoreArrayRead(C, &read));
303:   }
304:   PetscCall(MatDenseGetArrayWrite(C, &array));
305:   PetscCall(MatDenseSetLDA(contents->work[1], lda));
306:   PetscCall(MatDensePlaceArray(contents->work[1], array));
307:   PetscCall(MatProductNumeric(contents->work[1]));
308:   PetscCall(MatDenseResetArray(contents->work[1]));
309:   PetscCall(MatDenseRestoreArrayWrite(C, &array));
310:   PetscCall(MatSetOption(C, MAT_NO_OFF_PROC_ENTRIES, PETSC_TRUE));
311:   PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY));
312:   PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY));
313:   PetscCall(MatDiagonalScale(C, left, NULL));
314:   PetscCall(MatScale(C, scale));
315:   PetscFunctionReturn(PETSC_SUCCESS);
316: }

318: static PetscErrorCode MatNormal_DenseDestroy(PetscCtxRt ctx)
319: {
320:   Normal_Dense *contents = *(Normal_Dense **)ctx;

322:   PetscFunctionBegin;
323:   PetscCall(MatDestroy(contents->work));
324:   PetscCall(MatDestroy(contents->work + 1));
325:   PetscCall(MatDestroy(contents->work + 2));
326:   PetscCall(PetscFree(contents));
327:   PetscFunctionReturn(PETSC_SUCCESS);
328: }

330: static PetscErrorCode MatProductSymbolic_Normal_Dense(Mat C)
331: {
332:   Mat           A, B;
333:   Normal_Dense *contents = NULL;
334:   Mat_Normal   *a;
335:   Vec           left, right;
336:   PetscScalar  *array, scale;
337:   PetscInt      n, N, m, M, lda;

339:   PetscFunctionBegin;
340:   MatCheckProduct(C, 1);
341:   PetscCheck(!C->product->data, PetscObjectComm((PetscObject)C), PETSC_ERR_PLIB, "Product data not empty");
342:   A = C->product->A;
343:   B = C->product->B;
344:   PetscCall(MatShellGetScalingShifts(A, (PetscScalar *)MAT_SHELL_NOT_ALLOWED, &scale, (Vec *)MAT_SHELL_NOT_ALLOWED, &left, &right, (Mat *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED));
345:   PetscCall(MatShellGetContext(A, &a));
346:   PetscCall(MatGetLocalSize(B, NULL, &n));
347:   PetscCall(MatGetSize(B, NULL, &N));
348:   PetscCall(MatGetLocalSize(A, &m, NULL));
349:   PetscCall(MatGetSize(A, &M, NULL));
350:   PetscCall(MatSetSizes(C, m, n, M, N));
351:   PetscCall(MatSetType(C, ((PetscObject)B)->type_name));
352:   PetscCall(MatSetVecType(C, B->defaultvectype));
353:   PetscCall(MatSetUp(C));
354:   PetscCall(PetscNew(&contents));
355:   C->product->data    = contents;
356:   C->product->destroy = MatNormal_DenseDestroy;
357:   PetscCall(MatDenseGetLDA(C, &lda));
358:   if (right) {
359:     /* use a view of C to avoid a reference cycle */
360:     PetscCall(MatDenseGetArrayWrite(C, &array));
361:     PetscCall(MatCreateDenseWithMemType(PetscObjectComm((PetscObject)C), PETSC_MEMTYPE_HOST, m, n, M, N, lda, array, contents->work + 2));
362:     PetscCall(MatSetVecType(contents->work[2], C->defaultvectype));
363:     PetscCall(MatDenseRestoreArrayWrite(C, &array));
364:     PetscCall(MatProductCreate(a->A, contents->work[2], NULL, contents->work));
365:   } else PetscCall(MatProductCreate(a->A, B, NULL, contents->work));
366:   PetscCall(MatProductSetType(contents->work[0], MATPRODUCT_AB));
367:   PetscCall(MatProductSetFromOptions(contents->work[0]));
368:   PetscCall(MatProductSymbolic(contents->work[0]));
369:   PetscCall(MatDenseGetArrayWrite(C, &array));
370:   PetscCall(MatCreateDenseWithMemType(PetscObjectComm((PetscObject)C), PETSC_MEMTYPE_HOST, m, n, M, N, lda, array, contents->work + 1));
371:   PetscCall(MatSetVecType(contents->work[1], C->defaultvectype));
372:   PetscCall(MatDenseRestoreArrayWrite(C, &array));
373:   PetscCall(MatProductCreateWithMat(a->A, contents->work[0], NULL, contents->work[1]));
374:   PetscCall(MatProductSetType(contents->work[1], MATPRODUCT_AtB));
375:   PetscCall(MatProductSetFromOptions(contents->work[1]));
376:   PetscCall(MatProductSymbolic(contents->work[1]));
377:   C->ops->productnumeric = MatProductNumeric_Normal_Dense;
378:   PetscFunctionReturn(PETSC_SUCCESS);
379: }

381: static PetscErrorCode MatProductSetFromOptions_Normal_Dense(Mat C)
382: {
383:   Mat_Product *product = C->product;

385:   PetscFunctionBegin;
386:   if (product->type == MATPRODUCT_AB) C->ops->productsymbolic = MatProductSymbolic_Normal_Dense;
387:   PetscFunctionReturn(PETSC_SUCCESS);
388: }

390: /*MC
391:   MATNORMAL - a matrix that behaves like A'*A for `MatMult()` while only containing A

393:   Level: intermediate

395:   Developer Notes:
396:   This is implemented on top of `MATSHELL` to get support for scaling and shifting without requiring duplicate code

398:   Users can not call `MatShellSetOperation()` operations on this class, there is some error checking for that incorrect usage

400: .seealso: [](ch_matrices), `Mat`, `MatCreateNormal()`, `MatMult()`, `MatNormalGetMat()`, `MATNORMALHERMITIAN`, `MatCreateNormalHermitian()`
401: M*/

403: /*@
404:   MatCreateNormal - Creates a new `MATNORMAL` matrix object that behaves like $A^T A$.

406:   Collective

408:   Input Parameter:
409: . A - the (possibly rectangular) matrix

411:   Output Parameter:
412: . N - the matrix that represents $A^T A $

414:   Level: intermediate

416:   Notes:
417:   The product $A^T A$ is NOT actually formed! Rather the new matrix
418:   object performs the matrix-vector product, `MatMult()`, by first multiplying by
419:   $A$ and then $A^T$

421:   If `MatGetFactor()` is called on this matrix with `MAT_FACTOR_QR` then the inner matrix `A` is used for the factorization

423: .seealso: [](ch_matrices), `Mat`, `MATNORMAL`, `MatMult()`, `MatNormalGetMat()`, `MATNORMALHERMITIAN`, `MatCreateNormalHermitian()`
424: @*/
425: PetscErrorCode MatCreateNormal(Mat A, Mat *N)
426: {
427:   Mat_Normal *Na;
428:   VecType     vtype;

430:   PetscFunctionBegin;
431:   PetscCall(MatCreate(PetscObjectComm((PetscObject)A), N));
432:   PetscCall(PetscLayoutReference(A->cmap, &(*N)->rmap));
433:   PetscCall(PetscLayoutReference(A->cmap, &(*N)->cmap));
434:   PetscCall(MatSetType(*N, MATSHELL));
435:   PetscCall(PetscNew(&Na));
436:   PetscCall(MatShellSetContext(*N, Na));
437:   PetscCall(PetscObjectReference((PetscObject)A));
438:   Na->A = A;
439:   PetscCall(MatCreateVecs(A, NULL, &Na->w));

441:   PetscCall(MatSetBlockSize(*N, A->cmap->bs));
442:   PetscCall(MatShellSetOperation(*N, MATOP_DESTROY, (PetscErrorCodeFn *)MatDestroy_Normal));
443:   PetscCall(MatShellSetOperation(*N, MATOP_MULT, (PetscErrorCodeFn *)MatMult_Normal));
444:   PetscCall(MatShellSetOperation(*N, MATOP_MULT_TRANSPOSE, (PetscErrorCodeFn *)MatMult_Normal));
445:   PetscCall(MatShellSetOperation(*N, MATOP_DUPLICATE, (PetscErrorCodeFn *)MatDuplicate_Normal));
446:   PetscCall(MatShellSetOperation(*N, MATOP_GET_DIAGONAL, (PetscErrorCodeFn *)MatGetDiagonal_Normal));
447:   PetscCall(MatShellSetOperation(*N, MATOP_GET_DIAGONAL_BLOCK, (PetscErrorCodeFn *)MatGetDiagonalBlock_Normal));
448:   PetscCall(MatShellSetOperation(*N, MATOP_COPY, (PetscErrorCodeFn *)MatCopy_Normal));
449:   (*N)->ops->increaseoverlap   = MatIncreaseOverlap_Normal;
450:   (*N)->ops->createsubmatrices = MatCreateSubMatrices_Normal;
451:   (*N)->ops->permute           = MatPermute_Normal;

453:   PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatNormalGetMat_C", MatNormalGetMat_Normal));
454:   PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatConvert_normal_seqaij_C", MatConvert_Normal_AIJ));
455:   PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatConvert_normal_mpiaij_C", MatConvert_Normal_AIJ));
456: #if PetscDefined(HAVE_HYPRE)
457:   PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatConvert_normal_hypre_C", MatConvert_Normal_HYPRE));
458: #endif
459:   PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatProductSetFromOptions_normal_seqdense_C", MatProductSetFromOptions_Normal_Dense));
460:   PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatProductSetFromOptions_normal_mpidense_C", MatProductSetFromOptions_Normal_Dense));
461:   PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatShellSetContext_C", MatShellSetContext_Immutable));
462:   PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatShellSetContextDestroy_C", MatShellSetContextDestroy_Immutable));
463:   PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatShellSetManageScalingShifts_C", MatShellSetManageScalingShifts_Immutable));
464:   PetscCall(MatSetOption(*N, MAT_SYMMETRIC, PETSC_TRUE));
465:   PetscCall(MatGetVecType(A, &vtype));
466:   PetscCall(MatSetVecType(*N, vtype));
467: #if PetscDefined(HAVE_DEVICE)
468:   PetscCall(MatBindToCPU(*N, A->boundtocpu));
469: #endif
470:   PetscCall(MatSetUp(*N));
471:   PetscCall(PetscObjectChangeTypeName((PetscObject)*N, MATNORMAL));
472:   PetscFunctionReturn(PETSC_SUCCESS);
473: }