1 #include <petsc/private/matimpl.h> /*I "petscmat.h" I*/ 2 3 typedef struct { 4 Vec diag; 5 PetscBool diag_valid; 6 Vec inv_diag; 7 PetscBool inv_diag_valid; 8 PetscObjectState diag_state, inv_diag_state; 9 PetscInt *col; 10 PetscScalar *val; 11 } Mat_Diagonal; 12 13 static PetscErrorCode MatDiagonalSetUpDiagonal(Mat A) 14 { 15 Mat_Diagonal *ctx = (Mat_Diagonal *)A->data; 16 17 PetscFunctionBegin; 18 if (!ctx->diag_valid) { 19 PetscAssert(ctx->inv_diag_valid, PetscObjectComm((PetscObject)A), PETSC_ERR_PLIB, "Neither diagonal nor inverse diagonal is in a valid state"); 20 PetscCall(VecCopy(ctx->inv_diag, ctx->diag)); 21 PetscCall(VecReciprocal(ctx->diag)); 22 ctx->diag_valid = PETSC_TRUE; 23 } 24 PetscFunctionReturn(PETSC_SUCCESS); 25 } 26 27 static PetscErrorCode MatDiagonalSetUpInverseDiagonal(Mat A) 28 { 29 Mat_Diagonal *ctx = (Mat_Diagonal *)A->data; 30 31 PetscFunctionBegin; 32 if (!ctx->inv_diag_valid) { 33 PetscAssert(ctx->diag_valid, PetscObjectComm((PetscObject)A), PETSC_ERR_PLIB, "Neither diagonal nor inverse diagonal is in a valid state"); 34 PetscCall(VecCopy(ctx->diag, ctx->inv_diag)); 35 PetscCall(VecReciprocal(ctx->inv_diag)); 36 ctx->inv_diag_valid = PETSC_TRUE; 37 } 38 PetscFunctionReturn(PETSC_SUCCESS); 39 } 40 41 static PetscErrorCode MatAXPY_Diagonal(Mat Y, PetscScalar a, Mat X, MatStructure str) 42 { 43 Mat_Diagonal *yctx = (Mat_Diagonal *)Y->data; 44 Mat_Diagonal *xctx = (Mat_Diagonal *)X->data; 45 46 PetscFunctionBegin; 47 PetscCall(MatDiagonalSetUpDiagonal(Y)); 48 PetscCall(MatDiagonalSetUpDiagonal(X)); 49 PetscCall(VecAXPY(yctx->diag, a, xctx->diag)); 50 yctx->inv_diag_valid = PETSC_FALSE; 51 PetscFunctionReturn(PETSC_SUCCESS); 52 } 53 54 static PetscErrorCode MatGetRow_Diagonal(Mat A, PetscInt row, PetscInt *ncols, PetscInt **cols, PetscScalar **vals) 55 { 56 Mat_Diagonal *mat = (Mat_Diagonal *)A->data; 57 58 PetscFunctionBegin; 59 if (ncols) *ncols = 1; 60 if (cols) { 61 if (!mat->col) PetscCall(PetscMalloc1(1, &mat->col)); 62 *mat->col = row; 63 *cols = mat->col; 64 } 65 if (vals) { 66 const PetscScalar *v; 67 68 if (!mat->val) PetscCall(PetscMalloc1(1, &mat->val)); 69 PetscCall(VecGetArrayRead(mat->diag, &v)); 70 *mat->val = v[row]; 71 *vals = mat->val; 72 PetscCall(VecRestoreArrayRead(mat->diag, &v)); 73 } 74 PetscFunctionReturn(PETSC_SUCCESS); 75 } 76 77 static PetscErrorCode MatMult_Diagonal(Mat A, Vec x, Vec y) 78 { 79 Mat_Diagonal *ctx = (Mat_Diagonal *)A->data; 80 81 PetscFunctionBegin; 82 PetscCall(MatDiagonalSetUpDiagonal(A)); 83 PetscCall(VecPointwiseMult(y, ctx->diag, x)); 84 PetscFunctionReturn(PETSC_SUCCESS); 85 } 86 87 static PetscErrorCode MatMultAdd_Diagonal(Mat mat, Vec v1, Vec v2, Vec v3) 88 { 89 Mat_Diagonal *ctx = (Mat_Diagonal *)mat->data; 90 91 PetscFunctionBegin; 92 PetscCall(MatDiagonalSetUpDiagonal(mat)); 93 if (v2 != v3) { 94 PetscCall(VecPointwiseMult(v3, ctx->diag, v1)); 95 PetscCall(VecAXPY(v3, 1.0, v2)); 96 } else { 97 Vec w; 98 PetscCall(VecDuplicate(v3, &w)); 99 PetscCall(VecPointwiseMult(w, ctx->diag, v1)); 100 PetscCall(VecAXPY(v3, 1.0, w)); 101 PetscCall(VecDestroy(&w)); 102 } 103 PetscFunctionReturn(PETSC_SUCCESS); 104 } 105 106 static PetscErrorCode MatNorm_Diagonal(Mat A, NormType type, PetscReal *nrm) 107 { 108 Mat_Diagonal *ctx = (Mat_Diagonal *)A->data; 109 110 PetscFunctionBegin; 111 PetscCall(MatDiagonalSetUpDiagonal(A)); 112 type = (type == NORM_FROBENIUS) ? NORM_2 : NORM_INFINITY; 113 PetscCall(VecNorm(ctx->diag, type, nrm)); 114 PetscFunctionReturn(PETSC_SUCCESS); 115 } 116 117 static PetscErrorCode MatDuplicate_Diagonal(Mat A, MatDuplicateOption op, Mat *B) 118 { 119 Mat_Diagonal *actx = (Mat_Diagonal *)A->data; 120 121 PetscFunctionBegin; 122 PetscCall(MatCreate(PetscObjectComm((PetscObject)A), B)); 123 PetscCall(MatSetSizes(*B, A->rmap->n, A->cmap->n, A->rmap->N, A->cmap->N)); 124 PetscCall(MatSetBlockSizesFromMats(*B, A, A)); 125 PetscCall(MatSetType(*B, MATDIAGONAL)); 126 PetscCall(PetscLayoutReference(A->rmap, &(*B)->rmap)); 127 PetscCall(PetscLayoutReference(A->cmap, &(*B)->cmap)); 128 if (op == MAT_COPY_VALUES) { 129 Mat_Diagonal *bctx = (Mat_Diagonal *)(*B)->data; 130 131 PetscCall(MatSetUp(A)); 132 PetscCall(MatSetUp(*B)); 133 PetscCall(VecCopy(actx->diag, bctx->diag)); 134 PetscCall(VecCopy(actx->inv_diag, bctx->inv_diag)); 135 bctx->diag_valid = actx->diag_valid; 136 bctx->inv_diag_valid = actx->inv_diag_valid; 137 } 138 PetscFunctionReturn(PETSC_SUCCESS); 139 } 140 141 /*@ 142 MatDiagonalGetDiagonal - Get the diagonal of a `MATDIAGONAL` 143 144 Input Parameter: 145 . A - the `MATDIAGONAL` 146 147 Output Parameter: 148 . diag - the `Vec` that defines the diagonal 149 150 Level: developer 151 152 Note: 153 The user must call 154 `MatDiagonalRestoreDiagonal()` before using the matrix again. 155 156 For a copy of the diagonal values, rather than a reference, use `MatGetDiagonal()` 157 158 Any changes to the obtained vector immediately change the action of the `Mat`. The matrix can be changed more efficiently by accessing this vector and changing its values, instead of filling a work vector and using `MatDiagonalSet()` 159 160 .seealso: [](ch_matrices), `MATDIAGONAL`, `MatCreateDiagonal()`, `MatDiagonalRestoreDiagonal()`, `MatDiagonalGetInverseDiagonal()`, `MatGetDiagonal()` 161 @*/ 162 PetscErrorCode MatDiagonalGetDiagonal(Mat A, Vec *diag) 163 { 164 PetscFunctionBegin; 165 PetscValidHeaderSpecific(A, MAT_CLASSID, 1); 166 PetscAssertPointer(diag, 2); 167 *diag = NULL; 168 PetscUseMethod(A, "MatDiagonalGetDiagonal_C", (Mat, Vec *), (A, diag)); 169 PetscFunctionReturn(PETSC_SUCCESS); 170 } 171 172 static PetscErrorCode MatDiagonalGetDiagonal_Diagonal(Mat A, Vec *diag) 173 { 174 Mat_Diagonal *ctx = (Mat_Diagonal *)A->data; 175 176 PetscFunctionBegin; 177 PetscCall(MatSetUp(A)); 178 PetscCall(MatDiagonalSetUpDiagonal(A)); 179 *diag = ctx->diag; 180 PetscCall(PetscObjectStateGet((PetscObject)*diag, &ctx->diag_state)); 181 PetscFunctionReturn(PETSC_SUCCESS); 182 } 183 184 /*@ 185 MatDiagonalRestoreDiagonal - Restore the diagonal of a `MATDIAGONAL` 186 187 Input Parameters: 188 + A - the `MATDIAGONAL` 189 - diag - the `Vec` obtained from `MatDiagonalGetDiagonal()` 190 191 Level: developer 192 193 Note: 194 Use `MatDiagonalSet()` to change the values by copy, rather than reference. 195 196 .seealso: [](ch_matrices), `MATDIAGONAL`, `MatCreateDiagonal()`, `MatDiagonalGetDiagonal()` 197 @*/ 198 PetscErrorCode MatDiagonalRestoreDiagonal(Mat A, Vec *diag) 199 { 200 PetscFunctionBegin; 201 PetscValidHeaderSpecific(A, MAT_CLASSID, 1); 202 PetscAssertPointer(diag, 2); 203 PetscUseMethod(A, "MatDiagonalRestoreDiagonal_C", (Mat, Vec *), (A, diag)); 204 PetscFunctionReturn(PETSC_SUCCESS); 205 } 206 207 static PetscErrorCode MatDiagonalRestoreDiagonal_Diagonal(Mat A, Vec *diag) 208 { 209 Mat_Diagonal *ctx = (Mat_Diagonal *)A->data; 210 PetscObjectState diag_state; 211 212 PetscFunctionBegin; 213 PetscCheck(ctx->diag == *diag, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Restored a different diagonal vector"); 214 ctx->diag_valid = PETSC_TRUE; 215 PetscCall(PetscObjectStateGet((PetscObject)*diag, &diag_state)); 216 if (ctx->diag_state != diag_state) { 217 PetscCall(PetscObjectStateIncrease((PetscObject)A)); 218 ctx->inv_diag_valid = PETSC_FALSE; 219 } 220 *diag = NULL; 221 PetscFunctionReturn(PETSC_SUCCESS); 222 } 223 224 /*@ 225 MatDiagonalGetInverseDiagonal - Get the inverse diagonal of a `MATDIAGONAL` 226 227 Input Parameter: 228 . A - the `MATDIAGONAL` 229 230 Output Parameter: 231 . inv_diag - the `Vec` that defines the inverse diagonal 232 233 Level: developer 234 235 Note: 236 The user must call 237 `MatDiagonalRestoreInverseDiagonal()` before using the matrix again. 238 239 If a matrix is created only to call `MatSolve()` (which happens for `MATLMVMDIAGBROYDEN`), 240 using `MatDiagonalGetInverseDiagonal()` and `MatDiagonalRestoreInverseDiagonal()` avoids copies 241 and avoids any call to `VecReciprocal()`. 242 243 .seealso: [](ch_matrices), `MATDIAGONAL`, `MatCreateDiagonal()`, `MatDiagonalRestoreInverseDiagonal()`, `MatDiagonalGetDiagonal()`, `MATLMVMBROYDEN`, `MatSolve()` 244 @*/ 245 PetscErrorCode MatDiagonalGetInverseDiagonal(Mat A, Vec *inv_diag) 246 { 247 PetscFunctionBegin; 248 PetscValidHeaderSpecific(A, MAT_CLASSID, 1); 249 PetscAssertPointer(inv_diag, 2); 250 *inv_diag = NULL; 251 PetscUseMethod(A, "MatDiagonalGetInverseDiagonal_C", (Mat, Vec *), (A, inv_diag)); 252 PetscFunctionReturn(PETSC_SUCCESS); 253 } 254 255 static PetscErrorCode MatDiagonalGetInverseDiagonal_Diagonal(Mat A, Vec *inv_diag) 256 { 257 Mat_Diagonal *ctx = (Mat_Diagonal *)A->data; 258 259 PetscFunctionBegin; 260 PetscCall(MatSetUp(A)); 261 PetscCall(MatDiagonalSetUpInverseDiagonal(A)); 262 *inv_diag = ctx->inv_diag; 263 PetscCall(PetscObjectStateGet((PetscObject)*inv_diag, &ctx->inv_diag_state)); 264 PetscFunctionReturn(PETSC_SUCCESS); 265 } 266 267 /*@ 268 MatDiagonalRestoreInverseDiagonal - Restore the inverse diagonal of a `MATDIAGONAL` 269 270 Input Parameters: 271 + A - the `MATDIAGONAL` 272 - inv_diag - the `Vec` obtained from `MatDiagonalGetInverseDiagonal()` 273 274 Level: developer 275 276 .seealso: [](ch_matrices), `MATDIAGONAL`, `MatCreateDiagonal()`, `MatDiagonalGetInverseDiagonal()` 277 @*/ 278 PetscErrorCode MatDiagonalRestoreInverseDiagonal(Mat A, Vec *inv_diag) 279 { 280 PetscFunctionBegin; 281 PetscValidHeaderSpecific(A, MAT_CLASSID, 1); 282 PetscAssertPointer(inv_diag, 2); 283 PetscUseMethod(A, "MatDiagonalRestoreInverseDiagonal_C", (Mat, Vec *), (A, inv_diag)); 284 PetscFunctionReturn(PETSC_SUCCESS); 285 } 286 287 static PetscErrorCode MatDiagonalRestoreInverseDiagonal_Diagonal(Mat A, Vec *inv_diag) 288 { 289 Mat_Diagonal *ctx = (Mat_Diagonal *)A->data; 290 PetscObjectState inv_diag_state; 291 292 PetscFunctionBegin; 293 PetscCheck(ctx->inv_diag == *inv_diag, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Restored a different diagonal vector"); 294 ctx->inv_diag_valid = PETSC_TRUE; 295 PetscCall(PetscObjectStateGet((PetscObject)*inv_diag, &inv_diag_state)); 296 if (ctx->inv_diag_state != inv_diag_state) { 297 PetscCall(PetscObjectStateIncrease((PetscObject)A)); 298 ctx->diag_valid = PETSC_FALSE; 299 } 300 *inv_diag = NULL; 301 PetscFunctionReturn(PETSC_SUCCESS); 302 } 303 304 static PetscErrorCode MatPermute_Diagonal(Mat A, IS rowp, IS colp, Mat *B) 305 { 306 Mat_Diagonal *ctx = (Mat_Diagonal *)A->data; 307 Vec v; 308 309 PetscFunctionBegin; 310 PetscCheck(rowp == colp, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_INCOMP, "Row permutation and column permutation must be the same"); 311 PetscCall(VecDuplicate(ctx->diag, &v)); 312 PetscCall(VecCopy(ctx->diag, v)); 313 PetscCall(VecPermute(v, rowp, PETSC_FALSE)); 314 PetscCall(MatCreateDiagonal(v, B)); 315 PetscCall(VecDestroy(&v)); 316 PetscFunctionReturn(PETSC_SUCCESS); 317 } 318 319 static PetscErrorCode MatSetRandom_Diagonal(Mat A, PetscRandom rand) 320 { 321 Vec d; 322 323 PetscFunctionBegin; 324 PetscCall(MatDiagonalGetDiagonal(A, &d)); 325 PetscCall(VecSetRandom(d, rand)); 326 PetscCall(MatDiagonalRestoreDiagonal(A, &d)); 327 PetscFunctionReturn(PETSC_SUCCESS); 328 } 329 330 static PetscErrorCode MatDestroy_Diagonal(Mat mat) 331 { 332 Mat_Diagonal *ctx = (Mat_Diagonal *)mat->data; 333 334 PetscFunctionBegin; 335 PetscCall(VecDestroy(&ctx->diag)); 336 PetscCall(VecDestroy(&ctx->inv_diag)); 337 PetscCall(PetscFree(ctx->col)); 338 PetscCall(PetscFree(ctx->val)); 339 PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDiagonalGetDiagonal_C", NULL)); 340 PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDiagonalRestoreDiagonal_C", NULL)); 341 PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDiagonalGetInverseDiagonal_C", NULL)); 342 PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDiagonalRestoreInverseDiagonal_C", NULL)); 343 PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_diagonal_seqdense_C", NULL)); 344 PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_diagonal_mpidense_C", NULL)); 345 PetscCall(PetscFree(mat->data)); 346 mat->structural_symmetry_eternal = PETSC_FALSE; 347 mat->symmetry_eternal = PETSC_FALSE; 348 PetscFunctionReturn(PETSC_SUCCESS); 349 } 350 351 static PetscErrorCode MatView_Diagonal(Mat J, PetscViewer viewer) 352 { 353 Mat_Diagonal *ctx = (Mat_Diagonal *)J->data; 354 PetscBool isascii; 355 356 PetscFunctionBegin; 357 PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii)); 358 if (isascii) { 359 PetscViewerFormat format; 360 361 PetscCall(MatDiagonalSetUpDiagonal(J)); 362 PetscCall(PetscViewerGetFormat(viewer, &format)); 363 if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO) { 364 PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)ctx->diag, viewer)); 365 PetscFunctionReturn(PETSC_SUCCESS); 366 } 367 PetscCall(VecView(ctx->diag, viewer)); 368 } 369 PetscFunctionReturn(PETSC_SUCCESS); 370 } 371 372 static PetscErrorCode MatGetDiagonal_Diagonal(Mat J, Vec x) 373 { 374 Mat_Diagonal *ctx = (Mat_Diagonal *)J->data; 375 376 PetscFunctionBegin; 377 PetscCall(MatDiagonalSetUpDiagonal(J)); 378 PetscCall(VecCopy(ctx->diag, x)); 379 PetscFunctionReturn(PETSC_SUCCESS); 380 } 381 382 static PetscErrorCode MatDiagonalSet_Diagonal(Mat J, Vec D, InsertMode is) 383 { 384 Mat_Diagonal *ctx = (Mat_Diagonal *)J->data; 385 386 PetscFunctionBegin; 387 switch (is) { 388 case ADD_VALUES: 389 case ADD_ALL_VALUES: 390 case ADD_BC_VALUES: 391 PetscCall(MatDiagonalSetUpDiagonal(J)); 392 PetscCall(VecAXPY(ctx->diag, 1.0, D)); 393 ctx->inv_diag_valid = PETSC_FALSE; 394 break; 395 case INSERT_VALUES: 396 case INSERT_BC_VALUES: 397 case INSERT_ALL_VALUES: 398 PetscCall(MatSetUp(J)); 399 PetscCall(VecCopy(D, ctx->diag)); 400 ctx->diag_valid = PETSC_TRUE; 401 ctx->inv_diag_valid = PETSC_FALSE; 402 break; 403 case MAX_VALUES: 404 PetscCall(MatDiagonalSetUpDiagonal(J)); 405 PetscCall(VecPointwiseMax(ctx->diag, D, ctx->diag)); 406 ctx->inv_diag_valid = PETSC_FALSE; 407 break; 408 case MIN_VALUES: 409 PetscCall(MatDiagonalSetUpDiagonal(J)); 410 PetscCall(VecPointwiseMin(ctx->diag, D, ctx->diag)); 411 ctx->inv_diag_valid = PETSC_FALSE; 412 break; 413 case NOT_SET_VALUES: 414 break; 415 } 416 PetscFunctionReturn(PETSC_SUCCESS); 417 } 418 419 static PetscErrorCode MatShift_Diagonal(Mat Y, PetscScalar a) 420 { 421 Mat_Diagonal *ctx = (Mat_Diagonal *)Y->data; 422 423 PetscFunctionBegin; 424 PetscCall(MatDiagonalSetUpDiagonal(Y)); 425 PetscCall(VecShift(ctx->diag, a)); 426 ctx->inv_diag_valid = PETSC_FALSE; 427 PetscFunctionReturn(PETSC_SUCCESS); 428 } 429 430 static PetscErrorCode MatScale_Diagonal(Mat Y, PetscScalar a) 431 { 432 Mat_Diagonal *ctx = (Mat_Diagonal *)Y->data; 433 434 PetscFunctionBegin; 435 PetscCall(MatSetUp(Y)); 436 PetscCall(MatDiagonalSetUpDiagonal(Y)); 437 PetscCall(VecScale(ctx->diag, a)); 438 ctx->inv_diag_valid = PETSC_FALSE; 439 PetscFunctionReturn(PETSC_SUCCESS); 440 } 441 442 static PetscErrorCode MatDiagonalScale_Diagonal(Mat Y, Vec l, Vec r) 443 { 444 Mat_Diagonal *ctx = (Mat_Diagonal *)Y->data; 445 446 PetscFunctionBegin; 447 PetscCall(MatDiagonalSetUpDiagonal(Y)); 448 if (l) { 449 PetscCall(VecPointwiseMult(ctx->diag, ctx->diag, l)); 450 ctx->inv_diag_valid = PETSC_FALSE; 451 } 452 if (r) { 453 PetscCall(VecPointwiseMult(ctx->diag, ctx->diag, r)); 454 ctx->inv_diag_valid = PETSC_FALSE; 455 } 456 PetscFunctionReturn(PETSC_SUCCESS); 457 } 458 459 static PetscErrorCode MatConjugate_Diagonal(Mat Y) 460 { 461 PetscFunctionBegin; 462 Mat_Diagonal *ctx = (Mat_Diagonal *)Y->data; 463 464 if (ctx->diag_valid) { 465 PetscCall(VecConjugate(ctx->diag)); 466 PetscCall(PetscObjectStateGet((PetscObject)ctx->diag, &ctx->diag_state)); 467 } 468 if (ctx->inv_diag_valid) { 469 PetscCall(VecConjugate(ctx->inv_diag)); 470 PetscCall(PetscObjectStateGet((PetscObject)ctx->inv_diag, &ctx->inv_diag_state)); 471 } 472 PetscFunctionReturn(PETSC_SUCCESS); 473 } 474 475 static PetscErrorCode MatTranspose_Diagonal(Mat A, MatReuse reuse, Mat *matout) 476 { 477 PetscFunctionBegin; 478 if (reuse == MAT_INPLACE_MATRIX) { 479 PetscLayout tmplayout = A->rmap; 480 481 A->rmap = A->cmap; 482 A->cmap = tmplayout; 483 } else { 484 Vec diag, newdiag; 485 if (reuse == MAT_INITIAL_MATRIX) { 486 PetscCall(MatDiagonalGetDiagonal(A, &diag)); 487 PetscCall(VecDuplicate(diag, &newdiag)); 488 PetscCall(VecCopy(diag, newdiag)); 489 PetscCall(MatDiagonalRestoreDiagonal(A, &diag)); 490 PetscCall(MatCreateDiagonal(newdiag, matout)); 491 PetscCall(VecDestroy(&newdiag)); 492 } else { 493 PetscCall(MatDiagonalGetDiagonal(A, &diag)); 494 PetscCall(MatDiagonalGetDiagonal(*matout, &newdiag)); 495 PetscCall(VecCopy(diag, newdiag)); 496 PetscCall(MatDiagonalRestoreDiagonal(*matout, &newdiag)); 497 PetscCall(MatDiagonalRestoreDiagonal(A, &diag)); 498 } 499 } 500 PetscFunctionReturn(PETSC_SUCCESS); 501 } 502 503 static PetscErrorCode MatSetUp_Diagonal(Mat A) 504 { 505 Mat_Diagonal *ctx = (Mat_Diagonal *)A->data; 506 507 PetscFunctionBegin; 508 if (!ctx->diag) { 509 PetscCall(PetscLayoutSetUp(A->cmap)); 510 PetscCall(PetscLayoutSetUp(A->rmap)); 511 PetscCall(MatCreateVecs(A, &ctx->diag, NULL)); 512 PetscCall(VecDuplicate(ctx->diag, &ctx->inv_diag)); 513 PetscCall(VecZeroEntries(ctx->diag)); 514 ctx->diag_valid = PETSC_TRUE; 515 } 516 A->assembled = PETSC_TRUE; 517 PetscFunctionReturn(PETSC_SUCCESS); 518 } 519 520 static PetscErrorCode MatZeroEntries_Diagonal(Mat Y) 521 { 522 Mat_Diagonal *ctx = (Mat_Diagonal *)Y->data; 523 524 PetscFunctionBegin; 525 PetscCall(MatSetUp(Y)); 526 PetscCall(VecZeroEntries(ctx->diag)); 527 ctx->diag_valid = PETSC_TRUE; 528 ctx->inv_diag_valid = PETSC_FALSE; 529 PetscFunctionReturn(PETSC_SUCCESS); 530 } 531 532 static PetscErrorCode MatSolve_Diagonal(Mat matin, Vec b, Vec x) 533 { 534 Mat_Diagonal *ctx = (Mat_Diagonal *)matin->data; 535 536 PetscFunctionBegin; 537 PetscCall(MatDiagonalSetUpInverseDiagonal(matin)); 538 PetscCall(VecPointwiseMult(x, b, ctx->inv_diag)); 539 PetscFunctionReturn(PETSC_SUCCESS); 540 } 541 542 static PetscErrorCode MatGetInfo_Diagonal(Mat A, MatInfoType flag, MatInfo *info) 543 { 544 PetscFunctionBegin; 545 info->block_size = 1.0; 546 info->nz_allocated = A->cmap->N; 547 info->nz_used = A->cmap->N; 548 info->nz_unneeded = 0.0; 549 info->assemblies = A->num_ass; 550 info->mallocs = 0.0; 551 info->memory = 0; 552 info->fill_ratio_given = 0; 553 info->fill_ratio_needed = 0; 554 info->factor_mallocs = 0; 555 PetscFunctionReturn(PETSC_SUCCESS); 556 } 557 558 /*@ 559 MatCreateDiagonal - Creates a matrix defined by a given vector along its diagonal. 560 561 Collective 562 563 Input Parameter: 564 . diag - vector for the diagonal 565 566 Output Parameter: 567 . J - the diagonal matrix 568 569 Level: advanced 570 571 Notes: 572 Only supports square matrices with the same number of local rows and columns. 573 574 The input vector `diag` will be referenced internally: any changes to `diag` 575 will affect the matrix `J`. 576 577 .seealso: [](ch_matrices), `Mat`, `MatDestroy()`, `MATCONSTANTDIAGONAL`, `MatScale()`, `MatShift()`, `MatMult()`, `MatGetDiagonal()`, `MatSolve()` 578 `MatDiagonalRestoreInverseDiagonal()`, `MatDiagonalGetDiagonal()`, `MatDiagonalRestoreDiagonal()`, `MatDiagonalGetInverseDiagonal()` 579 @*/ 580 PetscErrorCode MatCreateDiagonal(Vec diag, Mat *J) 581 { 582 PetscFunctionBegin; 583 PetscValidHeaderSpecific(diag, VEC_CLASSID, 1); 584 PetscCall(MatCreate(PetscObjectComm((PetscObject)diag), J)); 585 PetscInt m, M; 586 PetscCall(VecGetLocalSize(diag, &m)); 587 PetscCall(VecGetSize(diag, &M)); 588 PetscCall(MatSetSizes(*J, m, m, M, M)); 589 PetscCall(MatSetType(*J, MATDIAGONAL)); 590 591 PetscLayout map; 592 PetscCall(VecGetLayout(diag, &map)); 593 PetscCall(MatSetLayouts(*J, map, map)); 594 Mat_Diagonal *ctx = (Mat_Diagonal *)(*J)->data; 595 PetscCall(PetscObjectReference((PetscObject)diag)); 596 PetscCall(VecDestroy(&ctx->diag)); 597 PetscCall(VecDestroy(&ctx->inv_diag)); 598 ctx->diag = diag; 599 ctx->diag_valid = PETSC_TRUE; 600 ctx->inv_diag_valid = PETSC_FALSE; 601 VecType type; 602 PetscCall(VecDuplicate(ctx->diag, &ctx->inv_diag)); 603 PetscCall(VecGetType(diag, &type)); 604 PetscCall(PetscFree((*J)->defaultvectype)); 605 PetscCall(PetscStrallocpy(type, &(*J)->defaultvectype)); 606 PetscCall(MatSetUp(*J)); 607 ctx->col = NULL; 608 ctx->val = NULL; 609 PetscFunctionReturn(PETSC_SUCCESS); 610 } 611 612 static PetscErrorCode MatProductNumeric_Diagonal_Dense(Mat C) 613 { 614 Mat A, B; 615 Mat_Diagonal *a; 616 PetscScalar *c; 617 const PetscScalar *b, *alpha; 618 PetscInt ldb, ldc; 619 620 PetscFunctionBegin; 621 MatCheckProduct(C, 1); 622 A = C->product->A; 623 B = C->product->B; 624 a = (Mat_Diagonal *)A->data; 625 PetscCall(VecGetArrayRead(a->diag, &alpha)); 626 PetscCall(MatDenseGetLDA(B, &ldb)); 627 PetscCall(MatDenseGetLDA(C, &ldc)); 628 PetscCall(MatDenseGetArrayRead(B, &b)); 629 PetscCall(MatDenseGetArrayWrite(C, &c)); 630 for (PetscInt j = 0; j < B->cmap->N; j++) 631 for (PetscInt i = 0; i < B->rmap->n; i++) c[i + j * ldc] = alpha[i] * b[i + j * ldb]; 632 PetscCall(MatDenseRestoreArrayWrite(C, &c)); 633 PetscCall(MatDenseRestoreArrayRead(B, &b)); 634 PetscCall(VecRestoreArrayRead(a->diag, &alpha)); 635 PetscCall(PetscLogFlops(B->cmap->N * B->rmap->n)); 636 PetscFunctionReturn(PETSC_SUCCESS); 637 } 638 639 static PetscErrorCode MatProductSymbolic_Diagonal_Dense(Mat C) 640 { 641 Mat A, B; 642 PetscInt n, N, m, M; 643 644 PetscFunctionBegin; 645 MatCheckProduct(C, 1); 646 PetscCheck(!C->product->data, PetscObjectComm((PetscObject)C), PETSC_ERR_PLIB, "Product data not empty"); 647 A = C->product->A; 648 B = C->product->B; 649 PetscCall(MatDiagonalSetUpDiagonal(A)); 650 PetscCall(MatGetLocalSize(C, &m, &n)); 651 PetscCall(MatGetSize(C, &M, &N)); 652 if (m == PETSC_DECIDE || n == PETSC_DECIDE || M == PETSC_DECIDE || N == PETSC_DECIDE) { 653 PetscCall(MatGetLocalSize(B, NULL, &n)); 654 PetscCall(MatGetSize(B, NULL, &N)); 655 PetscCall(MatGetLocalSize(A, &m, NULL)); 656 PetscCall(MatGetSize(A, &M, NULL)); 657 PetscCall(MatSetSizes(C, m, n, M, N)); 658 } 659 PetscCall(MatSetType(C, ((PetscObject)B)->type_name)); 660 PetscCall(MatSetUp(C)); 661 C->ops->productnumeric = MatProductNumeric_Diagonal_Dense; 662 PetscFunctionReturn(PETSC_SUCCESS); 663 } 664 665 static PetscErrorCode MatProductSetFromOptions_Diagonal_Dense_AB(Mat C) 666 { 667 PetscFunctionBegin; 668 C->ops->productsymbolic = MatProductSymbolic_Diagonal_Dense; 669 PetscFunctionReturn(PETSC_SUCCESS); 670 } 671 672 static PetscErrorCode MatProductSetFromOptions_Diagonal_Dense(Mat C) 673 { 674 Mat_Product *product = C->product; 675 676 PetscFunctionBegin; 677 if (product->type == MATPRODUCT_AB || product->type == MATPRODUCT_AtB) PetscCall(MatProductSetFromOptions_Diagonal_Dense_AB(C)); 678 PetscFunctionReturn(PETSC_SUCCESS); 679 } 680 681 /*MC 682 MATDIAGONAL - MATDIAGONAL = "diagonal" - A diagonal matrix type with the diagonal implemented as a `Vec`. Useful for 683 cases where `VecPointwiseMult()` or `VecPointwiseDivide()` should be thought of as the actions of a linear operator. 684 685 Level: advanced 686 687 .seealso: [](ch_matrices), `Mat`, `MatCreateDiagonal()`, `MatDiagonalRestoreInverseDiagonal()`, `MatDiagonalGetDiagonal()`, `MatDiagonalRestoreDiagonal()`, `MatDiagonalGetInverseDiagonal()` 688 M*/ 689 PETSC_INTERN PetscErrorCode MatCreate_Diagonal(Mat A) 690 { 691 Mat_Diagonal *ctx; 692 693 PetscFunctionBegin; 694 PetscCall(PetscNew(&ctx)); 695 A->data = (void *)ctx; 696 697 A->structurally_symmetric = PETSC_BOOL3_TRUE; 698 A->structural_symmetry_eternal = PETSC_TRUE; 699 A->symmetry_eternal = PETSC_TRUE; 700 A->symmetric = PETSC_BOOL3_TRUE; 701 if (!PetscDefined(USE_COMPLEX)) A->hermitian = PETSC_BOOL3_TRUE; 702 703 A->ops->getrow = MatGetRow_Diagonal; 704 A->ops->mult = MatMult_Diagonal; 705 A->ops->multadd = MatMultAdd_Diagonal; 706 A->ops->multtranspose = MatMult_Diagonal; 707 A->ops->multtransposeadd = MatMultAdd_Diagonal; 708 A->ops->norm = MatNorm_Diagonal; 709 A->ops->duplicate = MatDuplicate_Diagonal; 710 A->ops->solve = MatSolve_Diagonal; 711 A->ops->solvetranspose = MatSolve_Diagonal; 712 A->ops->shift = MatShift_Diagonal; 713 A->ops->scale = MatScale_Diagonal; 714 A->ops->diagonalscale = MatDiagonalScale_Diagonal; 715 A->ops->getdiagonal = MatGetDiagonal_Diagonal; 716 A->ops->diagonalset = MatDiagonalSet_Diagonal; 717 A->ops->view = MatView_Diagonal; 718 A->ops->zeroentries = MatZeroEntries_Diagonal; 719 A->ops->destroy = MatDestroy_Diagonal; 720 A->ops->getinfo = MatGetInfo_Diagonal; 721 A->ops->axpy = MatAXPY_Diagonal; 722 A->ops->setup = MatSetUp_Diagonal; 723 A->ops->permute = MatPermute_Diagonal; 724 A->ops->setrandom = MatSetRandom_Diagonal; 725 A->ops->conjugate = MatConjugate_Diagonal; 726 A->ops->transpose = MatTranspose_Diagonal; 727 728 PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatDiagonalGetDiagonal_C", MatDiagonalGetDiagonal_Diagonal)); 729 PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatDiagonalRestoreDiagonal_C", MatDiagonalRestoreDiagonal_Diagonal)); 730 PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatDiagonalGetInverseDiagonal_C", MatDiagonalGetInverseDiagonal_Diagonal)); 731 PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatDiagonalRestoreInverseDiagonal_C", MatDiagonalRestoreInverseDiagonal_Diagonal)); 732 PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatProductSetFromOptions_diagonal_seqdense_C", MatProductSetFromOptions_Diagonal_Dense)); 733 PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatProductSetFromOptions_diagonal_mpidense_C", MatProductSetFromOptions_Diagonal_Dense)); 734 PetscCall(PetscObjectChangeTypeName((PetscObject)A, MATDIAGONAL)); 735 PetscFunctionReturn(PETSC_SUCCESS); 736 } 737