xref: /petsc/src/mat/impls/normal/normm.c (revision 57d508425293f0bb93f59574d14951d8faac9af8)
1 #include <../src/mat/impls/shell/shell.h> /*I "petscmat.h" I*/
2 
3 typedef struct {
4   Mat A;
5   Mat D; /* local submatrix for diagonal part */
6   Vec w;
7 } Mat_Normal;
8 
9 static PetscErrorCode MatIncreaseOverlap_Normal(Mat A, PetscInt is_max, IS is[], PetscInt ov)
10 {
11   Mat_Normal *a;
12   Mat         pattern;
13 
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(MatProductSetType(pattern, MATPRODUCT_AtB));
19   PetscCall(MatProductSetFromOptions(pattern));
20   PetscCall(MatProductSymbolic(pattern));
21   PetscCall(MatIncreaseOverlap(pattern, is_max, is, ov));
22   PetscCall(MatDestroy(&pattern));
23   PetscFunctionReturn(PETSC_SUCCESS);
24 }
25 
26 static PetscErrorCode MatCreateSubMatrices_Normal(Mat mat, PetscInt n, const IS irow[], const IS icol[], MatReuse scall, Mat *submat[])
27 {
28   Mat_Normal *a;
29   Mat         B, *suba;
30   IS         *row;
31   PetscScalar shift, scale;
32   PetscInt    M;
33 
34   PetscFunctionBegin;
35   PetscCheck(irow == icol, PetscObjectComm((PetscObject)mat), PETSC_ERR_SUP, "Not implemented");
36   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));
37   PetscCall(MatShellGetContext(mat, &a));
38   B = a->A;
39   if (scall != MAT_REUSE_MATRIX) PetscCall(PetscCalloc1(n, submat));
40   PetscCall(MatGetSize(B, &M, NULL));
41   PetscCall(PetscMalloc1(n, &row));
42   PetscCall(ISCreateStride(PETSC_COMM_SELF, M, 0, 1, &row[0]));
43   PetscCall(ISSetIdentity(row[0]));
44   for (M = 1; M < n; ++M) row[M] = row[0];
45   PetscCall(MatCreateSubMatrices(B, n, row, icol, MAT_INITIAL_MATRIX, &suba));
46   for (M = 0; M < n; ++M) {
47     PetscCall(MatCreateNormal(suba[M], *submat + M));
48     PetscCall(MatShift((*submat)[M], shift));
49     PetscCall(MatScale((*submat)[M], scale));
50   }
51   PetscCall(ISDestroy(&row[0]));
52   PetscCall(PetscFree(row));
53   PetscCall(MatDestroySubMatrices(n, &suba));
54   PetscFunctionReturn(PETSC_SUCCESS);
55 }
56 
57 static PetscErrorCode MatPermute_Normal(Mat A, IS rowp, IS colp, Mat *B)
58 {
59   Mat_Normal *a;
60   Mat         C, Aa;
61   IS          row;
62   PetscScalar shift, scale;
63 
64   PetscFunctionBegin;
65   PetscCheck(rowp == colp, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_INCOMP, "Row permutation and column permutation must be the same");
66   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));
67   PetscCall(MatShellGetContext(A, &a));
68   Aa = a->A;
69   PetscCall(ISCreateStride(PetscObjectComm((PetscObject)Aa), Aa->rmap->n, Aa->rmap->rstart, 1, &row));
70   PetscCall(ISSetIdentity(row));
71   PetscCall(MatPermute(Aa, row, colp, &C));
72   PetscCall(ISDestroy(&row));
73   PetscCall(MatCreateNormal(C, B));
74   PetscCall(MatDestroy(&C));
75   PetscCall(MatShift(*B, shift));
76   PetscCall(MatScale(*B, scale));
77   PetscFunctionReturn(PETSC_SUCCESS);
78 }
79 
80 static PetscErrorCode MatDuplicate_Normal(Mat A, MatDuplicateOption op, Mat *B)
81 {
82   Mat_Normal *a;
83   Mat         C;
84 
85   PetscFunctionBegin;
86   PetscCall(MatShellGetContext(A, &a));
87   PetscCall(MatDuplicate(a->A, op, &C));
88   PetscCall(MatCreateNormal(C, B));
89   PetscCall(MatDestroy(&C));
90   if (op == MAT_COPY_VALUES) PetscCall(MatCopy(A, *B, SAME_NONZERO_PATTERN));
91   PetscFunctionReturn(PETSC_SUCCESS);
92 }
93 
94 static PetscErrorCode MatCopy_Normal(Mat A, Mat B, MatStructure str)
95 {
96   Mat_Normal *a, *b;
97 
98   PetscFunctionBegin;
99   PetscCall(MatShellGetContext(A, &a));
100   PetscCall(MatShellGetContext(B, &b));
101   PetscCall(MatCopy(a->A, b->A, str));
102   PetscFunctionReturn(PETSC_SUCCESS);
103 }
104 
105 static PetscErrorCode MatMult_Normal(Mat N, Vec x, Vec y)
106 {
107   Mat_Normal *Na;
108 
109   PetscFunctionBegin;
110   PetscCall(MatShellGetContext(N, &Na));
111   PetscCall(MatMult(Na->A, x, Na->w));
112   PetscCall(MatMultTranspose(Na->A, Na->w, y));
113   PetscFunctionReturn(PETSC_SUCCESS);
114 }
115 
116 static PetscErrorCode MatDestroy_Normal(Mat N)
117 {
118   Mat_Normal *Na;
119 
120   PetscFunctionBegin;
121   PetscCall(MatShellGetContext(N, &Na));
122   PetscCall(MatDestroy(&Na->A));
123   PetscCall(MatDestroy(&Na->D));
124   PetscCall(VecDestroy(&Na->w));
125   PetscCall(PetscFree(Na));
126   PetscCall(PetscObjectComposeFunction((PetscObject)N, "MatNormalGetMat_C", NULL));
127   PetscCall(PetscObjectComposeFunction((PetscObject)N, "MatConvert_normal_seqaij_C", NULL));
128   PetscCall(PetscObjectComposeFunction((PetscObject)N, "MatConvert_normal_mpiaij_C", NULL));
129 #if defined(PETSC_HAVE_HYPRE)
130   PetscCall(PetscObjectComposeFunction((PetscObject)N, "MatConvert_normal_hypre_C", NULL));
131 #endif
132   PetscCall(PetscObjectComposeFunction((PetscObject)N, "MatProductSetFromOptions_normal_seqdense_C", NULL));
133   PetscCall(PetscObjectComposeFunction((PetscObject)N, "MatProductSetFromOptions_normal_mpidense_C", NULL));
134   PetscCall(PetscObjectComposeFunction((PetscObject)N, "MatShellSetContext_C", NULL));
135   PetscFunctionReturn(PETSC_SUCCESS);
136 }
137 
138 /*
139       Slow, nonscalable version
140 */
141 static PetscErrorCode MatGetDiagonal_Normal(Mat N, Vec v)
142 {
143   Mat_Normal        *Na;
144   Mat                A;
145   PetscInt           i, j, rstart, rend, nnz;
146   const PetscInt    *cols;
147   PetscScalar       *work, *values;
148   const PetscScalar *mvalues;
149 
150   PetscFunctionBegin;
151   PetscCall(MatShellGetContext(N, &Na));
152   A = Na->A;
153   PetscCall(PetscMalloc1(A->cmap->N, &work));
154   PetscCall(PetscArrayzero(work, A->cmap->N));
155   PetscCall(MatGetOwnershipRange(A, &rstart, &rend));
156   for (i = rstart; i < rend; i++) {
157     PetscCall(MatGetRow(A, i, &nnz, &cols, &mvalues));
158     for (j = 0; j < nnz; j++) work[cols[j]] += mvalues[j] * mvalues[j];
159     PetscCall(MatRestoreRow(A, i, &nnz, &cols, &mvalues));
160   }
161   PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, work, A->cmap->N, MPIU_SCALAR, MPIU_SUM, PetscObjectComm((PetscObject)N)));
162   rstart = N->cmap->rstart;
163   rend   = N->cmap->rend;
164   PetscCall(VecGetArray(v, &values));
165   PetscCall(PetscArraycpy(values, work + rstart, rend - rstart));
166   PetscCall(VecRestoreArray(v, &values));
167   PetscCall(PetscFree(work));
168   PetscFunctionReturn(PETSC_SUCCESS);
169 }
170 
171 static PetscErrorCode MatGetDiagonalBlock_Normal(Mat N, Mat *D)
172 {
173   Mat_Normal *Na;
174   Mat         M, A;
175 
176   PetscFunctionBegin;
177   PetscCall(MatShellGetContext(N, &Na));
178   A = Na->A;
179   PetscCall(MatGetDiagonalBlock(A, &M));
180   PetscCall(MatCreateNormal(M, &Na->D));
181   *D = Na->D;
182   PetscFunctionReturn(PETSC_SUCCESS);
183 }
184 
185 static PetscErrorCode MatNormalGetMat_Normal(Mat A, Mat *M)
186 {
187   Mat_Normal *Aa;
188 
189   PetscFunctionBegin;
190   PetscCall(MatShellGetContext(A, &Aa));
191   *M = Aa->A;
192   PetscFunctionReturn(PETSC_SUCCESS);
193 }
194 
195 /*@
196   MatNormalGetMat - Gets the `Mat` object stored inside a `MATNORMAL`
197 
198   Logically Collective
199 
200   Input Parameter:
201 . A - the `MATNORMAL` matrix
202 
203   Output Parameter:
204 . M - the matrix object stored inside `A`
205 
206   Level: intermediate
207 
208 .seealso: [](ch_matrices), `Mat`, `MATNORMAL`, `MATNORMALHERMITIAN`, `MatCreateNormal()`
209 @*/
210 PetscErrorCode MatNormalGetMat(Mat A, Mat *M)
211 {
212   PetscFunctionBegin;
213   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
214   PetscValidType(A, 1);
215   PetscAssertPointer(M, 2);
216   PetscUseMethod(A, "MatNormalGetMat_C", (Mat, Mat *), (A, M));
217   PetscFunctionReturn(PETSC_SUCCESS);
218 }
219 
220 static PetscErrorCode MatConvert_Normal_AIJ(Mat A, MatType newtype, MatReuse reuse, Mat *newmat)
221 {
222   Mat_Normal *Aa;
223   Mat         B;
224   PetscInt    m, n, M, N;
225 
226   PetscFunctionBegin;
227   PetscCall(MatShellGetContext(A, &Aa));
228   PetscCall(MatGetSize(A, &M, &N));
229   PetscCall(MatGetLocalSize(A, &m, &n));
230   if (reuse == MAT_REUSE_MATRIX) {
231     B = *newmat;
232     PetscCall(MatProductReplaceMats(Aa->A, Aa->A, NULL, B));
233   } else {
234     PetscCall(MatProductCreate(Aa->A, Aa->A, NULL, &B));
235     PetscCall(MatProductSetType(B, MATPRODUCT_AtB));
236     PetscCall(MatProductSetFromOptions(B));
237     PetscCall(MatProductSymbolic(B));
238     PetscCall(MatSetOption(B, MAT_SYMMETRIC, PETSC_TRUE));
239   }
240   PetscCall(MatProductNumeric(B));
241   if (reuse == MAT_INPLACE_MATRIX) PetscCall(MatHeaderReplace(A, &B));
242   else if (reuse == MAT_INITIAL_MATRIX) *newmat = B;
243   PetscCall(MatConvert(*newmat, MATAIJ, MAT_INPLACE_MATRIX, newmat));
244   PetscFunctionReturn(PETSC_SUCCESS);
245 }
246 
247 #if defined(PETSC_HAVE_HYPRE)
248 static PetscErrorCode MatConvert_Normal_HYPRE(Mat A, MatType type, MatReuse reuse, Mat *B)
249 {
250   PetscFunctionBegin;
251   if (reuse == MAT_INITIAL_MATRIX) {
252     PetscCall(MatConvert(A, MATAIJ, reuse, B));
253     PetscCall(MatConvert(*B, type, MAT_INPLACE_MATRIX, B));
254   } else PetscCall(MatConvert_Basic(A, type, reuse, B)); /* fall back to basic convert */
255   PetscFunctionReturn(PETSC_SUCCESS);
256 }
257 #endif
258 
259 typedef struct {
260   Mat work[2];
261 } Normal_Dense;
262 
263 static PetscErrorCode MatProductNumeric_Normal_Dense(Mat C)
264 {
265   Mat           A, B;
266   Normal_Dense *contents;
267   Mat_Normal   *a;
268   Vec           right;
269   PetscScalar  *array, scale;
270 
271   PetscFunctionBegin;
272   MatCheckProduct(C, 1);
273   A = C->product->A;
274   B = C->product->B;
275   PetscCall(MatShellGetContext(A, &a));
276   contents = (Normal_Dense *)C->product->data;
277   PetscCheck(contents, PetscObjectComm((PetscObject)C), PETSC_ERR_PLIB, "Product data empty");
278   PetscCall(MatShellGetScalingShifts(A, (PetscScalar *)MAT_SHELL_NOT_ALLOWED, &scale, (Vec *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, &right, (Mat *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED));
279   if (right) {
280     PetscCall(MatCopy(B, C, SAME_NONZERO_PATTERN));
281     PetscCall(MatDiagonalScale(C, right, NULL));
282   }
283   PetscCall(MatProductNumeric(contents->work[0]));
284   PetscCall(MatDenseGetArrayWrite(C, &array));
285   PetscCall(MatDensePlaceArray(contents->work[1], array));
286   PetscCall(MatProductNumeric(contents->work[1]));
287   PetscCall(MatDenseRestoreArrayWrite(C, &array));
288   PetscCall(MatDenseResetArray(contents->work[1]));
289   PetscCall(MatSetOption(C, MAT_NO_OFF_PROC_ENTRIES, PETSC_TRUE));
290   PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY));
291   PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY));
292   PetscCall(MatScale(C, scale));
293   PetscFunctionReturn(PETSC_SUCCESS);
294 }
295 
296 static PetscErrorCode MatNormal_DenseDestroy(void *ctx)
297 {
298   Normal_Dense *contents = (Normal_Dense *)ctx;
299 
300   PetscFunctionBegin;
301   PetscCall(MatDestroy(contents->work));
302   PetscCall(MatDestroy(contents->work + 1));
303   PetscCall(PetscFree(contents));
304   PetscFunctionReturn(PETSC_SUCCESS);
305 }
306 
307 static PetscErrorCode MatProductSymbolic_Normal_Dense(Mat C)
308 {
309   Mat           A, B;
310   Normal_Dense *contents = NULL;
311   Mat_Normal   *a;
312   Vec           right;
313   PetscScalar  *array, scale;
314   PetscInt      n, N, m, M;
315 
316   PetscFunctionBegin;
317   MatCheckProduct(C, 1);
318   PetscCheck(!C->product->data, PetscObjectComm((PetscObject)C), PETSC_ERR_PLIB, "Product data not empty");
319   A = C->product->A;
320   B = C->product->B;
321   PetscCall(MatShellGetScalingShifts(A, (PetscScalar *)MAT_SHELL_NOT_ALLOWED, &scale, (Vec *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, &right, (Mat *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED));
322   PetscCall(MatShellGetContext(A, &a));
323   PetscCall(MatGetLocalSize(C, &m, &n));
324   PetscCall(MatGetSize(C, &M, &N));
325   if (m == PETSC_DECIDE || n == PETSC_DECIDE || M == PETSC_DECIDE || N == PETSC_DECIDE) {
326     PetscCall(MatGetLocalSize(B, NULL, &n));
327     PetscCall(MatGetSize(B, NULL, &N));
328     PetscCall(MatGetLocalSize(A, &m, NULL));
329     PetscCall(MatGetSize(A, &M, NULL));
330     PetscCall(MatSetSizes(C, m, n, M, N));
331   }
332   PetscCall(MatSetType(C, ((PetscObject)B)->type_name));
333   PetscCall(MatSetUp(C));
334   PetscCall(PetscNew(&contents));
335   C->product->data    = contents;
336   C->product->destroy = MatNormal_DenseDestroy;
337   if (right) PetscCall(MatProductCreate(a->A, C, NULL, contents->work));
338   else PetscCall(MatProductCreate(a->A, B, NULL, contents->work));
339   PetscCall(MatProductSetType(contents->work[0], MATPRODUCT_AB));
340   PetscCall(MatProductSetFromOptions(contents->work[0]));
341   PetscCall(MatProductSymbolic(contents->work[0]));
342   PetscCall(MatProductCreate(a->A, contents->work[0], NULL, contents->work + 1));
343   PetscCall(MatProductSetType(contents->work[1], MATPRODUCT_AtB));
344   PetscCall(MatProductSetFromOptions(contents->work[1]));
345   PetscCall(MatProductSymbolic(contents->work[1]));
346   PetscCall(MatDenseGetArrayWrite(C, &array));
347   PetscCall(MatSeqDenseSetPreallocation(contents->work[1], array));
348   PetscCall(MatMPIDenseSetPreallocation(contents->work[1], array));
349   PetscCall(MatDenseRestoreArrayWrite(C, &array));
350   C->ops->productnumeric = MatProductNumeric_Normal_Dense;
351   PetscFunctionReturn(PETSC_SUCCESS);
352 }
353 
354 static PetscErrorCode MatProductSetFromOptions_Normal_Dense(Mat C)
355 {
356   Mat_Product *product = C->product;
357 
358   PetscFunctionBegin;
359   if (product->type == MATPRODUCT_AB) C->ops->productsymbolic = MatProductSymbolic_Normal_Dense;
360   PetscFunctionReturn(PETSC_SUCCESS);
361 }
362 
363 /*MC
364   MATNORMAL - a matrix that behaves like A'*A for `MatMult()` while only containing A
365 
366   Level: intermediate
367 
368   Developer Notes:
369   This is implemented on top of `MATSHELL` to get support for scaling and shifting without requiring duplicate code
370 
371   Users can not call `MatShellSetOperation()` operations on this class, there is some error checking for that incorrect usage
372 
373 .seealso: [](ch_matrices), `Mat`, `MatCreateNormal()`, `MatMult()`, `MatNormalGetMat()`, `MATNORMALHERMITIAN`, `MatCreateNormalHermitian()`
374 M*/
375 
376 /*@
377   MatCreateNormal - Creates a new `MATNORMAL` matrix object that behaves like $A^T A$.
378 
379   Collective
380 
381   Input Parameter:
382 . A - the (possibly rectangular) matrix
383 
384   Output Parameter:
385 . N - the matrix that represents $A^T A $
386 
387   Level: intermediate
388 
389   Notes:
390   The product $A^T A$ is NOT actually formed! Rather the new matrix
391   object performs the matrix-vector product, `MatMult()`, by first multiplying by
392   $A$ and then $A^T$
393 
394   If `MatGetFactor()` is called on this matrix with `MAT_FACTOR_QR` then the inner matrix `A` is used for the factorization
395 
396 .seealso: [](ch_matrices), `Mat`, `MATNORMAL`, `MatMult()`, `MatNormalGetMat()`, `MATNORMALHERMITIAN`, `MatCreateNormalHermitian()`
397 @*/
398 PetscErrorCode MatCreateNormal(Mat A, Mat *N)
399 {
400   Mat_Normal *Na;
401   VecType     vtype;
402 
403   PetscFunctionBegin;
404   PetscCall(MatCreate(PetscObjectComm((PetscObject)A), N));
405   PetscCall(PetscLayoutReference(A->cmap, &(*N)->rmap));
406   PetscCall(PetscLayoutReference(A->cmap, &(*N)->cmap));
407   PetscCall(MatSetType(*N, MATSHELL));
408   PetscCall(PetscNew(&Na));
409   PetscCall(MatShellSetContext(*N, Na));
410   PetscCall(PetscObjectReference((PetscObject)A));
411   Na->A = A;
412   PetscCall(MatCreateVecs(A, NULL, &Na->w));
413 
414   PetscCall(MatSetBlockSize(*N, A->cmap->bs));
415   PetscCall(MatShellSetOperation(*N, MATOP_DESTROY, (PetscErrorCodeFn *)MatDestroy_Normal));
416   PetscCall(MatShellSetOperation(*N, MATOP_MULT, (PetscErrorCodeFn *)MatMult_Normal));
417   PetscCall(MatShellSetOperation(*N, MATOP_MULT_TRANSPOSE, (PetscErrorCodeFn *)MatMult_Normal));
418   PetscCall(MatShellSetOperation(*N, MATOP_DUPLICATE, (PetscErrorCodeFn *)MatDuplicate_Normal));
419   PetscCall(MatShellSetOperation(*N, MATOP_GET_DIAGONAL, (PetscErrorCodeFn *)MatGetDiagonal_Normal));
420   PetscCall(MatShellSetOperation(*N, MATOP_GET_DIAGONAL_BLOCK, (PetscErrorCodeFn *)MatGetDiagonalBlock_Normal));
421   PetscCall(MatShellSetOperation(*N, MATOP_COPY, (PetscErrorCodeFn *)MatCopy_Normal));
422   (*N)->ops->increaseoverlap   = MatIncreaseOverlap_Normal;
423   (*N)->ops->createsubmatrices = MatCreateSubMatrices_Normal;
424   (*N)->ops->permute           = MatPermute_Normal;
425 
426   PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatNormalGetMat_C", MatNormalGetMat_Normal));
427   PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatConvert_normal_seqaij_C", MatConvert_Normal_AIJ));
428   PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatConvert_normal_mpiaij_C", MatConvert_Normal_AIJ));
429 #if defined(PETSC_HAVE_HYPRE)
430   PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatConvert_normal_hypre_C", MatConvert_Normal_HYPRE));
431 #endif
432   PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatProductSetFromOptions_normal_seqdense_C", MatProductSetFromOptions_Normal_Dense));
433   PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatProductSetFromOptions_normal_mpidense_C", MatProductSetFromOptions_Normal_Dense));
434   PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatShellSetContext_C", MatShellSetContext_Immutable));
435   PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatShellSetContextDestroy_C", MatShellSetContextDestroy_Immutable));
436   PetscCall(PetscObjectComposeFunction((PetscObject)*N, "MatShellSetManageScalingShifts_C", MatShellSetManageScalingShifts_Immutable));
437   PetscCall(MatSetOption(*N, MAT_SYMMETRIC, PETSC_TRUE));
438   PetscCall(MatGetVecType(A, &vtype));
439   PetscCall(MatSetVecType(*N, vtype));
440 #if defined(PETSC_HAVE_DEVICE)
441   PetscCall(MatBindToCPU(*N, A->boundtocpu));
442 #endif
443   PetscCall(MatSetUp(*N));
444   PetscCall(PetscObjectChangeTypeName((PetscObject)*N, MATNORMAL));
445   PetscFunctionReturn(PETSC_SUCCESS);
446 }
447