1 /* 2 Routines for matrix products. Calling procedure: 3 4 MatProductCreate(A,B,C,&D); or MatProductCreateWithMat(A,B,C,D) 5 MatProductSetType(D, MATPRODUCT_AB/AtB/ABt/PtAP/RARt/ABC) 6 MatProductSetAlgorithm(D, alg) 7 MatProductSetFill(D,fill) 8 MatProductSetFromOptions(D) 9 -> MatProductSetFromOptions_Private(D) 10 # Check matrix global sizes 11 if the matrices have the same setfromoptions routine, use it 12 if not, try: 13 -> Query MatProductSetFromOptions_Atype_Btype_Ctype_C(D) from A, B and C (in order) 14 if found -> run the specific setup that must set the symbolic operation (these callbacks should never fail) 15 if callback not found or no symbolic operation set 16 -> Query MatProductSetFromOptions_anytype_C(D) from A, B and C (in order) (e.g, matrices may have inner matrices like MATTRANSPOSEVIRTUAL) 17 if dispatch found but combination still not present do 18 -> check if B is dense and product type AtB or AB -> if true, basic looping of dense columns 19 -> check if triple product (PtAP, RARt or ABC) -> if true, set the Basic routines 20 21 # The setfromoptions calls MatProductSetFromOptions_Atype_Btype_Ctype should 22 # Check matrix local sizes for mpi matrices 23 # Set default algorithm 24 # Get runtime option 25 # Set D->ops->productsymbolic = MatProductSymbolic_productype_Atype_Btype_Ctype if found 26 27 MatProductSymbolic(D) 28 # Call MatProductSymbolic_productype_Atype_Btype_Ctype() 29 the callback must set the numeric phase D->ops->productnumeric = MatProductNumeric_productype_Atype_Btype_Ctype 30 31 MatProductNumeric(D) 32 # Call the numeric phase 33 34 # The symbolic phases are allowed to set extra data structures and attach those to the product 35 # this additional data can be reused between multiple numeric phases with the same matrices 36 # if not needed, call 37 MatProductClear(D) 38 */ 39 40 #include <petsc/private/matimpl.h> /*I "petscmat.h" I*/ 41 42 const char *const MatProductTypes[] = {"UNSPECIFIED", "AB", "AtB", "ABt", "PtAP", "RARt", "ABC"}; 43 44 /* these are basic implementations relying on the old function pointers 45 * they are dangerous and should be removed in the future */ 46 static PetscErrorCode MatProductNumeric_PtAP_Unsafe(Mat C) 47 { 48 Mat_Product *product = C->product; 49 Mat P = product->B, AP = product->Dwork; 50 51 PetscFunctionBegin; 52 /* AP = A*P */ 53 PetscCall(MatProductNumeric(AP)); 54 /* C = P^T*AP */ 55 PetscCall((*C->ops->transposematmultnumeric)(P, AP, C)); 56 PetscFunctionReturn(PETSC_SUCCESS); 57 } 58 59 static PetscErrorCode MatProductSymbolic_PtAP_Unsafe(Mat C) 60 { 61 Mat_Product *product = C->product; 62 Mat A = product->A, P = product->B, AP; 63 PetscReal fill = product->fill; 64 65 PetscFunctionBegin; 66 PetscCall(PetscInfo((PetscObject)C, "for A %s, P %s is used\n", ((PetscObject)product->A)->type_name, ((PetscObject)product->B)->type_name)); 67 /* AP = A*P */ 68 PetscCall(MatProductCreate(A, P, NULL, &AP)); 69 PetscCall(MatProductSetType(AP, MATPRODUCT_AB)); 70 PetscCall(MatProductSetAlgorithm(AP, MATPRODUCTALGORITHMDEFAULT)); 71 PetscCall(MatProductSetFill(AP, fill)); 72 PetscCall(MatProductSetFromOptions(AP)); 73 PetscCall(MatProductSymbolic(AP)); 74 75 /* C = P^T*AP */ 76 PetscCall(MatProductSetType(C, MATPRODUCT_AtB)); 77 PetscCall(MatProductSetAlgorithm(C, MATPRODUCTALGORITHMDEFAULT)); 78 product->A = P; 79 product->B = AP; 80 PetscCall(MatProductSetFromOptions(C)); 81 PetscCall(MatProductSymbolic(C)); 82 83 /* resume user's original input matrix setting for A and B */ 84 product->A = A; 85 product->B = P; 86 product->Dwork = AP; 87 88 C->ops->productnumeric = MatProductNumeric_PtAP_Unsafe; 89 PetscFunctionReturn(PETSC_SUCCESS); 90 } 91 92 static PetscErrorCode MatProductNumeric_RARt_Unsafe(Mat C) 93 { 94 Mat_Product *product = C->product; 95 Mat R = product->B, RA = product->Dwork; 96 97 PetscFunctionBegin; 98 /* RA = R*A */ 99 PetscCall(MatProductNumeric(RA)); 100 /* C = RA*R^T */ 101 PetscCall((*C->ops->mattransposemultnumeric)(RA, R, C)); 102 PetscFunctionReturn(PETSC_SUCCESS); 103 } 104 105 static PetscErrorCode MatProductSymbolic_RARt_Unsafe(Mat C) 106 { 107 Mat_Product *product = C->product; 108 Mat A = product->A, R = product->B, RA; 109 PetscReal fill = product->fill; 110 111 PetscFunctionBegin; 112 PetscCall(PetscInfo((PetscObject)C, "for A %s, R %s is used\n", ((PetscObject)product->A)->type_name, ((PetscObject)product->B)->type_name)); 113 /* RA = R*A */ 114 PetscCall(MatProductCreate(R, A, NULL, &RA)); 115 PetscCall(MatProductSetType(RA, MATPRODUCT_AB)); 116 PetscCall(MatProductSetAlgorithm(RA, MATPRODUCTALGORITHMDEFAULT)); 117 PetscCall(MatProductSetFill(RA, fill)); 118 PetscCall(MatProductSetFromOptions(RA)); 119 PetscCall(MatProductSymbolic(RA)); 120 121 /* C = RA*R^T */ 122 PetscCall(MatProductSetType(C, MATPRODUCT_ABt)); 123 PetscCall(MatProductSetAlgorithm(C, MATPRODUCTALGORITHMDEFAULT)); 124 product->A = RA; 125 PetscCall(MatProductSetFromOptions(C)); 126 PetscCall(MatProductSymbolic(C)); 127 128 /* resume user's original input matrix setting for A */ 129 product->A = A; 130 product->Dwork = RA; /* save here so it will be destroyed with product C */ 131 C->ops->productnumeric = MatProductNumeric_RARt_Unsafe; 132 PetscFunctionReturn(PETSC_SUCCESS); 133 } 134 135 static PetscErrorCode MatProductNumeric_ABC_Unsafe(Mat mat) 136 { 137 Mat_Product *product = mat->product; 138 Mat A = product->A, BC = product->Dwork; 139 140 PetscFunctionBegin; 141 /* Numeric BC = B*C */ 142 PetscCall(MatProductNumeric(BC)); 143 /* Numeric mat = A*BC */ 144 PetscCall((*mat->ops->matmultnumeric)(A, BC, mat)); 145 PetscFunctionReturn(PETSC_SUCCESS); 146 } 147 148 static PetscErrorCode MatProductSymbolic_ABC_Unsafe(Mat mat) 149 { 150 Mat_Product *product = mat->product; 151 Mat B = product->B, C = product->C, BC; 152 PetscReal fill = product->fill; 153 154 PetscFunctionBegin; 155 PetscCall(PetscInfo((PetscObject)mat, "for A %s, B %s, C %s is used\n", ((PetscObject)product->A)->type_name, ((PetscObject)product->B)->type_name, ((PetscObject)product->C)->type_name)); 156 /* Symbolic BC = B*C */ 157 PetscCall(MatProductCreate(B, C, NULL, &BC)); 158 PetscCall(MatProductSetType(BC, MATPRODUCT_AB)); 159 PetscCall(MatProductSetAlgorithm(BC, MATPRODUCTALGORITHMDEFAULT)); 160 PetscCall(MatProductSetFill(BC, fill)); 161 PetscCall(MatProductSetFromOptions(BC)); 162 PetscCall(MatProductSymbolic(BC)); 163 164 /* Symbolic mat = A*BC */ 165 PetscCall(MatProductSetType(mat, MATPRODUCT_AB)); 166 PetscCall(MatProductSetAlgorithm(mat, MATPRODUCTALGORITHMDEFAULT)); 167 product->B = BC; 168 product->Dwork = BC; 169 PetscCall(MatProductSetFromOptions(mat)); 170 PetscCall(MatProductSymbolic(mat)); 171 172 /* resume user's original input matrix setting for B */ 173 product->B = B; 174 mat->ops->productnumeric = MatProductNumeric_ABC_Unsafe; 175 PetscFunctionReturn(PETSC_SUCCESS); 176 } 177 178 static PetscErrorCode MatProductSymbolic_Unsafe(Mat mat) 179 { 180 Mat_Product *product = mat->product; 181 182 PetscFunctionBegin; 183 switch (product->type) { 184 case MATPRODUCT_PtAP: 185 PetscCall(MatProductSymbolic_PtAP_Unsafe(mat)); 186 break; 187 case MATPRODUCT_RARt: 188 PetscCall(MatProductSymbolic_RARt_Unsafe(mat)); 189 break; 190 case MATPRODUCT_ABC: 191 PetscCall(MatProductSymbolic_ABC_Unsafe(mat)); 192 break; 193 default: 194 SETERRQ(PetscObjectComm((PetscObject)mat), PETSC_ERR_SUP, "ProductType %s is not supported", MatProductTypes[product->type]); 195 } 196 PetscFunctionReturn(PETSC_SUCCESS); 197 } 198 199 /*@C 200 MatProductReplaceMats - Replace the input matrices for the matrix-matrix product operation inside the computed matrix 201 202 Collective 203 204 Input Parameters: 205 + A - the matrix or `NULL` if not being replaced 206 . B - the matrix or `NULL` if not being replaced 207 . C - the matrix or `NULL` if not being replaced 208 - D - the matrix whose values are computed via a matrix-matrix product operation 209 210 Level: intermediate 211 212 Note: 213 To reuse the symbolic phase, the input matrices must have exactly the same data structure as the replaced one. 214 If the type of any of the input matrices is different than what was previously used, or their symmetry flag changed but 215 the symbolic phase took advantage of their symmetry, the product is cleared and `MatProductSetFromOptions()` 216 and `MatProductSymbolic()` are invoked again. 217 218 .seealso: [](chapter_matrices), `MatProduct`, `Mat`, `MatProductCreate()`, `MatProductSetFromOptions()`, `MatProductSymbolic().` `MatProductClear()` 219 @*/ 220 PetscErrorCode MatProductReplaceMats(Mat A, Mat B, Mat C, Mat D) 221 { 222 Mat_Product *product; 223 PetscBool flgA = PETSC_TRUE, flgB = PETSC_TRUE, flgC = PETSC_TRUE, isset, issym; 224 225 PetscFunctionBegin; 226 PetscValidHeaderSpecific(D, MAT_CLASSID, 4); 227 MatCheckProduct(D, 4); 228 product = D->product; 229 if (A) { 230 PetscValidHeaderSpecific(A, MAT_CLASSID, 1); 231 PetscCall(PetscObjectReference((PetscObject)A)); 232 PetscCall(PetscObjectTypeCompare((PetscObject)product->A, ((PetscObject)A)->type_name, &flgA)); 233 PetscCall(MatIsSymmetricKnown(A, &isset, &issym)); 234 if (product->symbolic_used_the_fact_A_is_symmetric && isset && !issym) { /* symbolic was built around a symmetric A, but the new A is not anymore */ 235 flgA = PETSC_FALSE; 236 product->symbolic_used_the_fact_A_is_symmetric = PETSC_FALSE; /* reinit */ 237 } 238 PetscCall(MatDestroy(&product->A)); 239 product->A = A; 240 } 241 if (B) { 242 PetscValidHeaderSpecific(B, MAT_CLASSID, 2); 243 PetscCall(PetscObjectReference((PetscObject)B)); 244 PetscCall(PetscObjectTypeCompare((PetscObject)product->B, ((PetscObject)B)->type_name, &flgB)); 245 PetscCall(MatIsSymmetricKnown(B, &isset, &issym)); 246 if (product->symbolic_used_the_fact_B_is_symmetric && isset && !issym) { 247 flgB = PETSC_FALSE; 248 product->symbolic_used_the_fact_B_is_symmetric = PETSC_FALSE; /* reinit */ 249 } 250 PetscCall(MatDestroy(&product->B)); 251 product->B = B; 252 } 253 if (C) { 254 PetscValidHeaderSpecific(C, MAT_CLASSID, 3); 255 PetscCall(PetscObjectReference((PetscObject)C)); 256 PetscCall(PetscObjectTypeCompare((PetscObject)product->C, ((PetscObject)C)->type_name, &flgC)); 257 PetscCall(MatIsSymmetricKnown(C, &isset, &issym)); 258 if (product->symbolic_used_the_fact_C_is_symmetric && isset && !issym) { 259 flgC = PETSC_FALSE; 260 product->symbolic_used_the_fact_C_is_symmetric = PETSC_FALSE; /* reinit */ 261 } 262 PetscCall(MatDestroy(&product->C)); 263 product->C = C; 264 } 265 /* Any of the replaced mats is of a different type, reset */ 266 if (!flgA || !flgB || !flgC) { 267 if (D->product->destroy) PetscCall((*D->product->destroy)(D->product->data)); 268 D->product->destroy = NULL; 269 D->product->data = NULL; 270 if (D->ops->productnumeric || D->ops->productsymbolic) { 271 PetscCall(MatProductSetFromOptions(D)); 272 PetscCall(MatProductSymbolic(D)); 273 } 274 } 275 PetscFunctionReturn(PETSC_SUCCESS); 276 } 277 278 static PetscErrorCode MatProductNumeric_X_Dense(Mat C) 279 { 280 Mat_Product *product = C->product; 281 Mat A = product->A, B = product->B; 282 PetscInt k, K = B->cmap->N; 283 PetscBool t = PETSC_TRUE, iscuda = PETSC_FALSE; 284 PetscBool Bcpu = PETSC_TRUE, Ccpu = PETSC_TRUE; 285 char *Btype = NULL, *Ctype = NULL; 286 287 PetscFunctionBegin; 288 switch (product->type) { 289 case MATPRODUCT_AB: 290 t = PETSC_FALSE; 291 case MATPRODUCT_AtB: 292 break; 293 default: 294 SETERRQ(PetscObjectComm((PetscObject)C), PETSC_ERR_SUP, "MatProductNumeric type %s not supported for %s and %s matrices", MatProductTypes[product->type], ((PetscObject)A)->type_name, ((PetscObject)B)->type_name); 295 } 296 if (PetscDefined(HAVE_CUDA)) { 297 VecType vtype; 298 299 PetscCall(MatGetVecType(A, &vtype)); 300 PetscCall(PetscStrcmp(vtype, VECCUDA, &iscuda)); 301 if (!iscuda) PetscCall(PetscStrcmp(vtype, VECSEQCUDA, &iscuda)); 302 if (!iscuda) PetscCall(PetscStrcmp(vtype, VECMPICUDA, &iscuda)); 303 if (iscuda) { /* Make sure we have up-to-date data on the GPU */ 304 PetscCall(PetscStrallocpy(((PetscObject)B)->type_name, &Btype)); 305 PetscCall(PetscStrallocpy(((PetscObject)C)->type_name, &Ctype)); 306 PetscCall(MatConvert(B, MATDENSECUDA, MAT_INPLACE_MATRIX, &B)); 307 if (!C->assembled) { /* need to flag the matrix as assembled, otherwise MatConvert will complain */ 308 PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY)); 309 PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY)); 310 } 311 PetscCall(MatConvert(C, MATDENSECUDA, MAT_INPLACE_MATRIX, &C)); 312 } else { /* Make sure we have up-to-date data on the CPU */ 313 #if defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_VIENNACL) 314 Bcpu = B->boundtocpu; 315 Ccpu = C->boundtocpu; 316 #endif 317 PetscCall(MatBindToCPU(B, PETSC_TRUE)); 318 PetscCall(MatBindToCPU(C, PETSC_TRUE)); 319 } 320 } 321 for (k = 0; k < K; k++) { 322 Vec x, y; 323 324 PetscCall(MatDenseGetColumnVecRead(B, k, &x)); 325 PetscCall(MatDenseGetColumnVecWrite(C, k, &y)); 326 if (t) { 327 PetscCall(MatMultTranspose(A, x, y)); 328 } else { 329 PetscCall(MatMult(A, x, y)); 330 } 331 PetscCall(MatDenseRestoreColumnVecRead(B, k, &x)); 332 PetscCall(MatDenseRestoreColumnVecWrite(C, k, &y)); 333 } 334 PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY)); 335 PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY)); 336 if (PetscDefined(HAVE_CUDA)) { 337 if (iscuda) { 338 PetscCall(MatConvert(B, Btype, MAT_INPLACE_MATRIX, &B)); 339 PetscCall(MatConvert(C, Ctype, MAT_INPLACE_MATRIX, &C)); 340 } else { 341 PetscCall(MatBindToCPU(B, Bcpu)); 342 PetscCall(MatBindToCPU(C, Ccpu)); 343 } 344 } 345 PetscCall(PetscFree(Btype)); 346 PetscCall(PetscFree(Ctype)); 347 PetscFunctionReturn(PETSC_SUCCESS); 348 } 349 350 static PetscErrorCode MatProductSymbolic_X_Dense(Mat C) 351 { 352 Mat_Product *product = C->product; 353 Mat A = product->A, B = product->B; 354 PetscBool isdense; 355 356 PetscFunctionBegin; 357 switch (product->type) { 358 case MATPRODUCT_AB: 359 PetscCall(MatSetSizes(C, A->rmap->n, B->cmap->n, A->rmap->N, B->cmap->N)); 360 break; 361 case MATPRODUCT_AtB: 362 PetscCall(MatSetSizes(C, A->cmap->n, B->cmap->n, A->cmap->N, B->cmap->N)); 363 break; 364 default: 365 SETERRQ(PetscObjectComm((PetscObject)C), PETSC_ERR_SUP, "MatProductSymbolic type %s not supported for %s and %s matrices", MatProductTypes[product->type], ((PetscObject)A)->type_name, ((PetscObject)B)->type_name); 366 } 367 PetscCall(PetscObjectBaseTypeCompareAny((PetscObject)C, &isdense, MATSEQDENSE, MATMPIDENSE, "")); 368 if (!isdense) { 369 PetscCall(MatSetType(C, ((PetscObject)B)->type_name)); 370 /* If matrix type of C was not set or not dense, we need to reset the pointer */ 371 C->ops->productsymbolic = MatProductSymbolic_X_Dense; 372 } 373 C->ops->productnumeric = MatProductNumeric_X_Dense; 374 PetscCall(MatSetUp(C)); 375 PetscFunctionReturn(PETSC_SUCCESS); 376 } 377 378 /* a single driver to query the dispatching */ 379 static PetscErrorCode MatProductSetFromOptions_Private(Mat mat) 380 { 381 Mat_Product *product = mat->product; 382 PetscInt Am, An, Bm, Bn, Cm, Cn; 383 Mat A = product->A, B = product->B, C = product->C; 384 const char *const Bnames[] = {"B", "R", "P"}; 385 const char *bname; 386 PetscErrorCode (*fA)(Mat); 387 PetscErrorCode (*fB)(Mat); 388 PetscErrorCode (*fC)(Mat); 389 PetscErrorCode (*f)(Mat) = NULL; 390 391 PetscFunctionBegin; 392 mat->ops->productsymbolic = NULL; 393 mat->ops->productnumeric = NULL; 394 if (product->type == MATPRODUCT_UNSPECIFIED) PetscFunctionReturn(PETSC_SUCCESS); 395 PetscCheck(A, PetscObjectComm((PetscObject)mat), PETSC_ERR_PLIB, "Missing A mat"); 396 PetscCheck(B, PetscObjectComm((PetscObject)mat), PETSC_ERR_PLIB, "Missing B mat"); 397 PetscCheck(product->type != MATPRODUCT_ABC || C, PetscObjectComm((PetscObject)mat), PETSC_ERR_PLIB, "Missing C mat"); 398 if (product->type != MATPRODUCT_ABC) C = NULL; /* do not use C if not needed */ 399 if (product->type == MATPRODUCT_RARt) bname = Bnames[1]; 400 else if (product->type == MATPRODUCT_PtAP) bname = Bnames[2]; 401 else bname = Bnames[0]; 402 403 /* Check matrices sizes */ 404 Am = A->rmap->N; 405 An = A->cmap->N; 406 Bm = B->rmap->N; 407 Bn = B->cmap->N; 408 Cm = C ? C->rmap->N : 0; 409 Cn = C ? C->cmap->N : 0; 410 if (product->type == MATPRODUCT_RARt || product->type == MATPRODUCT_ABt) { 411 PetscInt t = Bn; 412 Bn = Bm; 413 Bm = t; 414 } 415 if (product->type == MATPRODUCT_AtB) { 416 PetscInt t = An; 417 An = Am; 418 Am = t; 419 } 420 PetscCheck(An == Bm, PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_SIZ, "Matrix dimensions of A and %s are incompatible for MatProductType %s: A %" PetscInt_FMT "x%" PetscInt_FMT ", %s %" PetscInt_FMT "x%" PetscInt_FMT, bname, 421 MatProductTypes[product->type], A->rmap->N, A->cmap->N, bname, B->rmap->N, B->cmap->N); 422 PetscCheck(!Cm || Cm == Bn, PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_SIZ, "Matrix dimensions of B and C are incompatible for MatProductType %s: B %" PetscInt_FMT "x%" PetscInt_FMT ", C %" PetscInt_FMT "x%" PetscInt_FMT, 423 MatProductTypes[product->type], B->rmap->N, B->cmap->N, Cm, Cn); 424 425 fA = A->ops->productsetfromoptions; 426 fB = B->ops->productsetfromoptions; 427 fC = C ? C->ops->productsetfromoptions : fA; 428 if (C) { 429 PetscCall(PetscInfo(mat, "MatProductType %s for A %s, %s %s, C %s\n", MatProductTypes[product->type], ((PetscObject)A)->type_name, bname, ((PetscObject)B)->type_name, ((PetscObject)C)->type_name)); 430 } else { 431 PetscCall(PetscInfo(mat, "MatProductType %s for A %s, %s %s\n", MatProductTypes[product->type], ((PetscObject)A)->type_name, bname, ((PetscObject)B)->type_name)); 432 } 433 if (fA == fB && fA == fC && fA) { 434 PetscCall(PetscInfo(mat, " matching op\n")); 435 PetscCall((*fA)(mat)); 436 } 437 /* We may have found f but it did not succeed */ 438 if (!mat->ops->productsymbolic) { /* query MatProductSetFromOptions_Atype_Btype_Ctype */ 439 char mtypes[256]; 440 PetscCall(PetscStrncpy(mtypes, "MatProductSetFromOptions_", sizeof(mtypes))); 441 PetscCall(PetscStrlcat(mtypes, ((PetscObject)A)->type_name, sizeof(mtypes))); 442 PetscCall(PetscStrlcat(mtypes, "_", sizeof(mtypes))); 443 PetscCall(PetscStrlcat(mtypes, ((PetscObject)B)->type_name, sizeof(mtypes))); 444 if (C) { 445 PetscCall(PetscStrlcat(mtypes, "_", sizeof(mtypes))); 446 PetscCall(PetscStrlcat(mtypes, ((PetscObject)C)->type_name, sizeof(mtypes))); 447 } 448 PetscCall(PetscStrlcat(mtypes, "_C", sizeof(mtypes))); 449 #if defined(__clang__) 450 #pragma clang diagnostic push 451 #pragma clang diagnostic ignored "-Wformat-pedantic" 452 #elif defined(__GNUC__) || defined(__GNUG__) 453 #pragma GCC diagnostic push 454 #pragma GCC diagnostic ignored "-Wformat" 455 #endif 456 PetscCall(PetscObjectQueryFunction((PetscObject)A, mtypes, &f)); 457 PetscCall(PetscInfo(mat, " querying %s from A? %p\n", mtypes, f)); 458 if (!f) { 459 PetscCall(PetscObjectQueryFunction((PetscObject)B, mtypes, &f)); 460 PetscCall(PetscInfo(mat, " querying %s from %s? %p\n", mtypes, bname, f)); 461 } 462 if (!f && C) { 463 PetscCall(PetscObjectQueryFunction((PetscObject)C, mtypes, &f)); 464 PetscCall(PetscInfo(mat, " querying %s from C? %p\n", mtypes, f)); 465 } 466 if (f) PetscCall((*f)(mat)); 467 468 /* We may have found f but it did not succeed */ 469 /* some matrices (i.e. MATTRANSPOSEVIRTUAL, MATSHELL constructed from MatConvert), knows what to do with their inner matrices */ 470 if (!mat->ops->productsymbolic) { 471 PetscCall(PetscStrncpy(mtypes, "MatProductSetFromOptions_anytype_C", sizeof(mtypes))); 472 PetscCall(PetscObjectQueryFunction((PetscObject)A, mtypes, &f)); 473 PetscCall(PetscInfo(mat, " querying %s from A? %p\n", mtypes, f)); 474 if (!f) { 475 PetscCall(PetscObjectQueryFunction((PetscObject)B, mtypes, &f)); 476 PetscCall(PetscInfo(mat, " querying %s from %s? %p\n", mtypes, bname, f)); 477 } 478 if (!f && C) { 479 PetscCall(PetscObjectQueryFunction((PetscObject)C, mtypes, &f)); 480 PetscCall(PetscInfo(mat, " querying %s from C? %p\n", mtypes, f)); 481 } 482 } 483 if (f) PetscCall((*f)(mat)); 484 } 485 #if defined(__clang__) 486 #pragma clang diagnostic pop 487 #elif defined(__GNUC__) || defined(__GNUG__) 488 #pragma GCC diagnostic pop 489 #endif 490 /* We may have found f but it did not succeed */ 491 if (!mat->ops->productsymbolic) { 492 /* we can still compute the product if B is of type dense */ 493 if (product->type == MATPRODUCT_AB || product->type == MATPRODUCT_AtB) { 494 PetscBool isdense; 495 496 PetscCall(PetscObjectBaseTypeCompareAny((PetscObject)B, &isdense, MATSEQDENSE, MATMPIDENSE, "")); 497 if (isdense) { 498 mat->ops->productsymbolic = MatProductSymbolic_X_Dense; 499 PetscCall(PetscInfo(mat, " using basic looping over columns of a dense matrix\n")); 500 } 501 } else if (product->type != MATPRODUCT_ABt) { /* use MatProductSymbolic/Numeric_Unsafe() for triple products only */ 502 /* 503 TODO: this should be changed to a proper setfromoptions, not setting the symbolic pointer here, because we do not know if 504 the combination will succeed. In order to be sure, we need MatProductGetProductType to return the type of the result 505 before computing the symbolic phase 506 */ 507 PetscCall(PetscInfo(mat, " symbolic product not supported, using MatProductSymbolic_Unsafe() implementation\n")); 508 mat->ops->productsymbolic = MatProductSymbolic_Unsafe; 509 } 510 } 511 if (!mat->ops->productsymbolic) PetscCall(PetscInfo(mat, " symbolic product is not supported\n")); 512 PetscFunctionReturn(PETSC_SUCCESS); 513 } 514 515 /*@C 516 MatProductSetFromOptions - Sets the options for the computation of a matrix-matrix product operation where the type, 517 the algorithm etc are determined from the options database. 518 519 Logically Collective 520 521 Input Parameter: 522 . mat - the matrix whose values are computed via a matrix-matrix product operation 523 524 Options Database Keys: 525 + -mat_product_clear - Clear intermediate data structures after `MatProductNumeric()` has been called 526 . -mat_product_algorithm <algorithm> - Sets the algorithm, see `MatProductAlgorithm` for possible values 527 - -mat_product_algorithm_backend_cpu - Use the CPU to perform the computation even if the matrix is a GPU matrix 528 529 Level: intermediate 530 531 Note: 532 The `-mat_product_clear` option reduces memory usage but means that the matrix cannot be re-used for a matrix-matrix product operation 533 534 .seealso: [](chapter_matrices), `MatProduct`, `Mat`, `MatSetFromOptions()`, `MatProductCreate()`, `MatProductCreateWithMat()`, `MatProductNumeric()`, 535 `MatProductSetType()`, `MatProductSetAlgorithm()`, `MatProductAlgorithm` 536 @*/ 537 PetscErrorCode MatProductSetFromOptions(Mat mat) 538 { 539 PetscFunctionBegin; 540 PetscValidHeaderSpecific(mat, MAT_CLASSID, 1); 541 MatCheckProduct(mat, 1); 542 PetscCheck(!mat->product->data, PetscObjectComm((PetscObject)mat), PETSC_ERR_ORDER, "Cannot call MatProductSetFromOptions with already present data"); 543 PetscObjectOptionsBegin((PetscObject)mat); 544 PetscCall(PetscOptionsBool("-mat_product_clear", "Clear intermediate data structures after MatProductNumeric() has been called", "MatProductClear", mat->product->clear, &mat->product->clear, NULL)); 545 PetscCall(PetscOptionsDeprecated("-mat_freeintermediatedatastructures", "-mat_product_clear", "3.13", "Or call MatProductClear() after MatProductNumeric()")); 546 PetscOptionsEnd(); 547 PetscCall(MatProductSetFromOptions_Private(mat)); 548 PetscCheck(mat->product, PetscObjectComm((PetscObject)mat), PETSC_ERR_PLIB, "Missing product after setup phase"); 549 PetscFunctionReturn(PETSC_SUCCESS); 550 } 551 552 /*@C 553 MatProductView - View the private matrix-matrix algorithm object within a matrix 554 555 Logically Collective 556 557 Input Parameters: 558 + mat - the matrix obtained with `MatProductCreate()` or `MatProductCreateWithMat()` 559 - viewer - where the information on the matrix-matrix algorithm of `mat` should be reviewed 560 561 Level: intermediate 562 563 .seealso: [](chapter_matrices), `MatProductType`, `Mat`, `MatProductSetFromOptions()`, `MatView()`, `MatProductCreate()`, `MatProductCreateWithMat()` 564 @*/ 565 PetscErrorCode MatProductView(Mat mat, PetscViewer viewer) 566 { 567 PetscFunctionBegin; 568 PetscValidHeaderSpecific(mat, MAT_CLASSID, 1); 569 if (!mat->product) PetscFunctionReturn(PETSC_SUCCESS); 570 if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)mat), &viewer)); 571 PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 572 PetscCheckSameComm(mat, 1, viewer, 2); 573 if (mat->product->view) PetscCall((*mat->product->view)(mat, viewer)); 574 PetscFunctionReturn(PETSC_SUCCESS); 575 } 576 577 /* these are basic implementations relying on the old function pointers 578 * they are dangerous and should be removed in the future */ 579 PetscErrorCode MatProductNumeric_AB(Mat mat) 580 { 581 Mat_Product *product = mat->product; 582 Mat A = product->A, B = product->B; 583 584 PetscFunctionBegin; 585 PetscCall((*mat->ops->matmultnumeric)(A, B, mat)); 586 PetscFunctionReturn(PETSC_SUCCESS); 587 } 588 589 PetscErrorCode MatProductNumeric_AtB(Mat mat) 590 { 591 Mat_Product *product = mat->product; 592 Mat A = product->A, B = product->B; 593 594 PetscFunctionBegin; 595 PetscCall((*mat->ops->transposematmultnumeric)(A, B, mat)); 596 PetscFunctionReturn(PETSC_SUCCESS); 597 } 598 599 PetscErrorCode MatProductNumeric_ABt(Mat mat) 600 { 601 Mat_Product *product = mat->product; 602 Mat A = product->A, B = product->B; 603 604 PetscFunctionBegin; 605 PetscCall((*mat->ops->mattransposemultnumeric)(A, B, mat)); 606 PetscFunctionReturn(PETSC_SUCCESS); 607 } 608 609 PetscErrorCode MatProductNumeric_PtAP(Mat mat) 610 { 611 Mat_Product *product = mat->product; 612 Mat A = product->A, B = product->B; 613 614 PetscFunctionBegin; 615 PetscCall((*mat->ops->ptapnumeric)(A, B, mat)); 616 PetscFunctionReturn(PETSC_SUCCESS); 617 } 618 619 PetscErrorCode MatProductNumeric_RARt(Mat mat) 620 { 621 Mat_Product *product = mat->product; 622 Mat A = product->A, B = product->B; 623 624 PetscFunctionBegin; 625 PetscCall((*mat->ops->rartnumeric)(A, B, mat)); 626 PetscFunctionReturn(PETSC_SUCCESS); 627 } 628 629 PetscErrorCode MatProductNumeric_ABC(Mat mat) 630 { 631 Mat_Product *product = mat->product; 632 Mat A = product->A, B = product->B, C = product->C; 633 634 PetscFunctionBegin; 635 PetscCall((*mat->ops->matmatmultnumeric)(A, B, C, mat)); 636 PetscFunctionReturn(PETSC_SUCCESS); 637 } 638 639 /*@ 640 MatProductNumeric - Compute a matrix-matrix product operation with the numerical values 641 642 Collective 643 644 Input/Output Parameter: 645 . mat - the matrix whose values are computed via a matrix-matrix product operation 646 647 Level: intermediate 648 649 Note: 650 `MatProductSymbolic()` must have been called on `mat` before calling this function 651 652 .seealso: [](chapter_matrices), `MatProduct`, `Mat`, `MatProductSetAlgorithm()`, `MatProductSetType()`, `MatProductCreate()`, `MatSetType()`, `MatProductSymbolic()` 653 @*/ 654 PetscErrorCode MatProductNumeric(Mat mat) 655 { 656 PetscLogEvent eventtype = -1; 657 PetscBool missing = PETSC_FALSE; 658 659 PetscFunctionBegin; 660 PetscValidHeaderSpecific(mat, MAT_CLASSID, 1); 661 MatCheckProduct(mat, 1); 662 switch (mat->product->type) { 663 case MATPRODUCT_AB: 664 eventtype = MAT_MatMultNumeric; 665 break; 666 case MATPRODUCT_AtB: 667 eventtype = MAT_TransposeMatMultNumeric; 668 break; 669 case MATPRODUCT_ABt: 670 eventtype = MAT_MatTransposeMultNumeric; 671 break; 672 case MATPRODUCT_PtAP: 673 eventtype = MAT_PtAPNumeric; 674 break; 675 case MATPRODUCT_RARt: 676 eventtype = MAT_RARtNumeric; 677 break; 678 case MATPRODUCT_ABC: 679 eventtype = MAT_MatMatMultNumeric; 680 break; 681 default: 682 SETERRQ(PetscObjectComm((PetscObject)mat), PETSC_ERR_SUP, "ProductType %s is not supported", MatProductTypes[mat->product->type]); 683 } 684 685 if (mat->ops->productnumeric) { 686 PetscCall(PetscLogEventBegin(eventtype, mat, 0, 0, 0)); 687 PetscUseTypeMethod(mat, productnumeric); 688 PetscCall(PetscLogEventEnd(eventtype, mat, 0, 0, 0)); 689 } else missing = PETSC_TRUE; 690 691 if (missing || !mat->product) { 692 char errstr[256]; 693 694 if (mat->product->type == MATPRODUCT_ABC) { 695 PetscCall(PetscSNPrintf(errstr, 256, "%s with A %s, B %s, C %s", MatProductTypes[mat->product->type], ((PetscObject)mat->product->A)->type_name, ((PetscObject)mat->product->B)->type_name, ((PetscObject)mat->product->C)->type_name)); 696 } else { 697 PetscCall(PetscSNPrintf(errstr, 256, "%s with A %s, B %s", MatProductTypes[mat->product->type], ((PetscObject)mat->product->A)->type_name, ((PetscObject)mat->product->B)->type_name)); 698 } 699 PetscCheck(!missing, PetscObjectComm((PetscObject)mat), PETSC_ERR_PLIB, "Unspecified numeric phase for product %s", errstr); 700 PetscCheck(mat->product, PetscObjectComm((PetscObject)mat), PETSC_ERR_PLIB, "Missing struct after symbolic phase for product %s", errstr); 701 } 702 703 if (mat->product->clear) PetscCall(MatProductClear(mat)); 704 PetscCall(PetscObjectStateIncrease((PetscObject)mat)); 705 PetscFunctionReturn(PETSC_SUCCESS); 706 } 707 708 /* these are basic implementations relying on the old function pointers 709 * they are dangerous and should be removed in the future */ 710 PetscErrorCode MatProductSymbolic_AB(Mat mat) 711 { 712 Mat_Product *product = mat->product; 713 Mat A = product->A, B = product->B; 714 715 PetscFunctionBegin; 716 PetscCall((*mat->ops->matmultsymbolic)(A, B, product->fill, mat)); 717 mat->ops->productnumeric = MatProductNumeric_AB; 718 PetscFunctionReturn(PETSC_SUCCESS); 719 } 720 721 PetscErrorCode MatProductSymbolic_AtB(Mat mat) 722 { 723 Mat_Product *product = mat->product; 724 Mat A = product->A, B = product->B; 725 726 PetscFunctionBegin; 727 PetscCall((*mat->ops->transposematmultsymbolic)(A, B, product->fill, mat)); 728 mat->ops->productnumeric = MatProductNumeric_AtB; 729 PetscFunctionReturn(PETSC_SUCCESS); 730 } 731 732 PetscErrorCode MatProductSymbolic_ABt(Mat mat) 733 { 734 Mat_Product *product = mat->product; 735 Mat A = product->A, B = product->B; 736 737 PetscFunctionBegin; 738 PetscCall((*mat->ops->mattransposemultsymbolic)(A, B, product->fill, mat)); 739 mat->ops->productnumeric = MatProductNumeric_ABt; 740 PetscFunctionReturn(PETSC_SUCCESS); 741 } 742 743 PetscErrorCode MatProductSymbolic_ABC(Mat mat) 744 { 745 Mat_Product *product = mat->product; 746 Mat A = product->A, B = product->B, C = product->C; 747 748 PetscFunctionBegin; 749 PetscCall((*mat->ops->matmatmultsymbolic)(A, B, C, product->fill, mat)); 750 mat->ops->productnumeric = MatProductNumeric_ABC; 751 PetscFunctionReturn(PETSC_SUCCESS); 752 } 753 754 /*@ 755 MatProductSymbolic - Perform the symbolic portion of a matrix-matrix product operation, this creates a data structure for use with the numerical 756 product to be done with `MatProductNumeric()` 757 758 Collective 759 760 Input/Output Parameter: 761 . mat - the matrix whose values are to be computed via a matrix-matrix product operation 762 763 Level: intermediate 764 765 Note: 766 `MatProductSetFromOptions()` must have been called on `mat` before calling this function 767 768 .seealso: [](chapter_matrices), `MatProduct`, `Mat`, `MatProductCreate()`, `MatProductCreateWithMat()`, `MatProductSetFromOptions()`, `MatProductNumeric()`, `MatProductSetType()`, `MatProductSetAlgorithm()` 769 @*/ 770 PetscErrorCode MatProductSymbolic(Mat mat) 771 { 772 PetscLogEvent eventtype = -1; 773 PetscBool missing = PETSC_FALSE; 774 775 PetscFunctionBegin; 776 PetscValidHeaderSpecific(mat, MAT_CLASSID, 1); 777 MatCheckProduct(mat, 1); 778 PetscCheck(!mat->product->data, PetscObjectComm((PetscObject)mat), PETSC_ERR_ORDER, "Cannot run symbolic phase. Product data not empty"); 779 switch (mat->product->type) { 780 case MATPRODUCT_AB: 781 eventtype = MAT_MatMultSymbolic; 782 break; 783 case MATPRODUCT_AtB: 784 eventtype = MAT_TransposeMatMultSymbolic; 785 break; 786 case MATPRODUCT_ABt: 787 eventtype = MAT_MatTransposeMultSymbolic; 788 break; 789 case MATPRODUCT_PtAP: 790 eventtype = MAT_PtAPSymbolic; 791 break; 792 case MATPRODUCT_RARt: 793 eventtype = MAT_RARtSymbolic; 794 break; 795 case MATPRODUCT_ABC: 796 eventtype = MAT_MatMatMultSymbolic; 797 break; 798 default: 799 SETERRQ(PetscObjectComm((PetscObject)mat), PETSC_ERR_SUP, "ProductType %s is not supported", MatProductTypes[mat->product->type]); 800 } 801 mat->ops->productnumeric = NULL; 802 if (mat->ops->productsymbolic) { 803 PetscCall(PetscLogEventBegin(eventtype, mat, 0, 0, 0)); 804 PetscUseTypeMethod(mat, productsymbolic); 805 PetscCall(PetscLogEventEnd(eventtype, mat, 0, 0, 0)); 806 } else missing = PETSC_TRUE; 807 808 if (missing || !mat->product || !mat->ops->productnumeric) { 809 char errstr[256]; 810 811 if (mat->product->type == MATPRODUCT_ABC) { 812 PetscCall(PetscSNPrintf(errstr, 256, "%s with A %s, B %s, C %s", MatProductTypes[mat->product->type], ((PetscObject)mat->product->A)->type_name, ((PetscObject)mat->product->B)->type_name, ((PetscObject)mat->product->C)->type_name)); 813 } else { 814 PetscCall(PetscSNPrintf(errstr, 256, "%s with A %s, B %s", MatProductTypes[mat->product->type], ((PetscObject)mat->product->A)->type_name, ((PetscObject)mat->product->B)->type_name)); 815 } 816 PetscCheck(!missing, PetscObjectComm((PetscObject)mat), PETSC_ERR_PLIB, "Unspecified symbolic phase for product %s. Call MatProductSetFromOptions() first", errstr); 817 PetscCheck(mat->product, PetscObjectComm((PetscObject)mat), PETSC_ERR_PLIB, "Missing struct after symbolic phase for product %s", errstr); 818 } 819 PetscFunctionReturn(PETSC_SUCCESS); 820 } 821 822 /*@ 823 MatProductSetFill - Set an expected fill of the matrix whose values are computed via a matrix-matrix product operation 824 825 Collective 826 827 Input Parameters: 828 + mat - the matrix whose values are to be computed via a matrix-matrix product operation 829 - fill - expected fill as ratio of nnz(mat)/(nnz(A) + nnz(B) + nnz(C)); use `PETSC_DEFAULT` if you do not have a good estimate. 830 If the product is a dense matrix, this value is not used. 831 832 Level: intermediate 833 834 .seealso: [](chapter_matrices), `MatProduct`, `Mat`, `MatProductSetFromOptions()`, `MatProductSetType()`, `MatProductSetAlgorithm()`, `MatProductCreate()` 835 @*/ 836 PetscErrorCode MatProductSetFill(Mat mat, PetscReal fill) 837 { 838 PetscFunctionBegin; 839 PetscValidHeaderSpecific(mat, MAT_CLASSID, 1); 840 MatCheckProduct(mat, 1); 841 if (fill == (PetscReal)PETSC_DEFAULT || fill == (PetscReal)PETSC_DECIDE) mat->product->fill = 2.0; 842 else mat->product->fill = fill; 843 PetscFunctionReturn(PETSC_SUCCESS); 844 } 845 846 /*@ 847 MatProductSetAlgorithm - Requests a particular algorithm for a matrix-matrix product operation that will perform to compute the given matrix 848 849 Collective 850 851 Input Parameters: 852 + mat - the matrix whose values are computed via a matrix-matrix product operation 853 - alg - particular implementation algorithm of the matrix product, e.g., `MATPRODUCTALGORITHMDEFAULT`. 854 855 Options Database Key: 856 . -mat_product_algorithm <algorithm> - Sets the algorithm, see `MatProductAlgorithm` 857 858 Level: intermediate 859 860 .seealso: [](chapter_matrices), `MatProduct`, `Mat`, `MatProductClear()`, `MatProductSetType()`, `MatProductSetFill()`, `MatProductCreate()`, `MatProductAlgorithm`, `MatProductType` 861 @*/ 862 PetscErrorCode MatProductSetAlgorithm(Mat mat, MatProductAlgorithm alg) 863 { 864 PetscFunctionBegin; 865 PetscValidHeaderSpecific(mat, MAT_CLASSID, 1); 866 MatCheckProduct(mat, 1); 867 PetscCall(PetscFree(mat->product->alg)); 868 PetscCall(PetscStrallocpy(alg, &mat->product->alg)); 869 PetscFunctionReturn(PETSC_SUCCESS); 870 } 871 872 /*@ 873 MatProductSetType - Sets a particular matrix-matrix product operation to be used to compute the values of the given matrix 874 875 Collective 876 877 Input Parameters: 878 + mat - the matrix whose values are computed via a matrix-matrix product operation 879 - productype - matrix product type, e.g., `MATPRODUCT_AB`,`MATPRODUCT_AtB`,`MATPRODUCT_ABt`,`MATPRODUCT_PtAP`,`MATPRODUCT_RARt`,`MATPRODUCT_ABC`, 880 see `MatProductType` 881 882 Level: intermediate 883 884 Note: 885 The small t represents the transpose operation. 886 887 .seealso: [](chapter_matrices), `MatProduct`, `Mat`, `MatProductCreate()`, `MatProductType`, `MatProductType`, 888 `MATPRODUCT_AB`, `MATPRODUCT_AtB`, `MATPRODUCT_ABt`, `MATPRODUCT_PtAP`, `MATPRODUCT_RARt`, `MATPRODUCT_ABC` 889 @*/ 890 PetscErrorCode MatProductSetType(Mat mat, MatProductType productype) 891 { 892 PetscFunctionBegin; 893 PetscValidHeaderSpecific(mat, MAT_CLASSID, 1); 894 MatCheckProduct(mat, 1); 895 PetscValidLogicalCollectiveEnum(mat, productype, 2); 896 if (productype != mat->product->type) { 897 if (mat->product->destroy) PetscCall((*mat->product->destroy)(mat->product->data)); 898 mat->product->destroy = NULL; 899 mat->product->data = NULL; 900 mat->ops->productsymbolic = NULL; 901 mat->ops->productnumeric = NULL; 902 } 903 mat->product->type = productype; 904 PetscFunctionReturn(PETSC_SUCCESS); 905 } 906 907 /*@ 908 MatProductClear - Clears from the matrix any internal data structures related to the computation of the values of the matrix from matrix-matrix product operations 909 910 Collective 911 912 Input Parameters: 913 . mat - the matrix whose values are to be computed via a matrix-matrix product operation 914 915 Options Database Key: 916 . -mat_product_clear - Clear intermediate data structures after `MatProductNumeric()` has been called 917 918 Level: intermediate 919 920 Notes: 921 This function should be called to remove any intermediate data used to compute the matrix to free up memory. 922 923 After having called this function, matrix-matrix product operations can no longer be used on `mat` 924 925 .seealso: [](chapter_matrices), `MatProduct`, `Mat`, `MatProductCreate()` 926 @*/ 927 PetscErrorCode MatProductClear(Mat mat) 928 { 929 Mat_Product *product = mat->product; 930 931 PetscFunctionBegin; 932 PetscValidHeaderSpecific(mat, MAT_CLASSID, 1); 933 if (product) { 934 PetscCall(MatDestroy(&product->A)); 935 PetscCall(MatDestroy(&product->B)); 936 PetscCall(MatDestroy(&product->C)); 937 PetscCall(PetscFree(product->alg)); 938 PetscCall(MatDestroy(&product->Dwork)); 939 if (product->destroy) PetscCall((*product->destroy)(product->data)); 940 } 941 PetscCall(PetscFree(mat->product)); 942 mat->ops->productsymbolic = NULL; 943 mat->ops->productnumeric = NULL; 944 PetscFunctionReturn(PETSC_SUCCESS); 945 } 946 947 /* Create a supporting struct and attach it to the matrix product */ 948 PetscErrorCode MatProductCreate_Private(Mat A, Mat B, Mat C, Mat D) 949 { 950 Mat_Product *product = NULL; 951 952 PetscFunctionBegin; 953 PetscValidHeaderSpecific(D, MAT_CLASSID, 4); 954 PetscCheck(!D->product, PetscObjectComm((PetscObject)D), PETSC_ERR_PLIB, "Product already present"); 955 PetscCall(PetscNew(&product)); 956 product->A = A; 957 product->B = B; 958 product->C = C; 959 product->type = MATPRODUCT_UNSPECIFIED; 960 product->Dwork = NULL; 961 product->api_user = PETSC_FALSE; 962 product->clear = PETSC_FALSE; 963 D->product = product; 964 965 PetscCall(MatProductSetAlgorithm(D, MATPRODUCTALGORITHMDEFAULT)); 966 PetscCall(MatProductSetFill(D, PETSC_DEFAULT)); 967 968 PetscCall(PetscObjectReference((PetscObject)A)); 969 PetscCall(PetscObjectReference((PetscObject)B)); 970 PetscCall(PetscObjectReference((PetscObject)C)); 971 PetscFunctionReturn(PETSC_SUCCESS); 972 } 973 974 /*@ 975 MatProductCreateWithMat - Set a given matrix to have its values computed via matrix-matrix operations on other matrices. 976 977 Collective 978 979 Input Parameters: 980 + A - the first matrix 981 . B - the second matrix 982 . C - the third matrix (optional, use `NULL` if not needed) 983 - D - the matrix whose values are to be computed via a matrix-matrix product operation 984 985 Level: intermediate 986 987 Notes: 988 Use `MatProductCreate()` if the matrix you wish computed (the `D` matrix) does not already exist 989 990 See `MatProductCreate()` for details on the usage of the matrix-matrix product operations 991 992 Any product data currently attached to `D` will be cleared 993 994 .seealso: [](chapter_matrices), `MatProduct`, `Mat`, `MatProductType`, `MatProductSetType()`, `MatProductAlgorithm`, 995 `MatProductSetAlgorithm`, `MatProductCreate()`, `MatProductClear()` 996 @*/ 997 PetscErrorCode MatProductCreateWithMat(Mat A, Mat B, Mat C, Mat D) 998 { 999 PetscFunctionBegin; 1000 PetscValidHeaderSpecific(A, MAT_CLASSID, 1); 1001 PetscValidType(A, 1); 1002 MatCheckPreallocated(A, 1); 1003 PetscCheck(A->assembled, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONGSTATE, "Not for unassembled matrix"); 1004 PetscCheck(!A->factortype, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix"); 1005 1006 PetscValidHeaderSpecific(B, MAT_CLASSID, 2); 1007 PetscValidType(B, 2); 1008 MatCheckPreallocated(B, 2); 1009 PetscCheck(B->assembled, PetscObjectComm((PetscObject)B), PETSC_ERR_ARG_WRONGSTATE, "Not for unassembled matrix"); 1010 PetscCheck(!B->factortype, PetscObjectComm((PetscObject)B), PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix"); 1011 1012 if (C) { 1013 PetscValidHeaderSpecific(C, MAT_CLASSID, 3); 1014 PetscValidType(C, 3); 1015 MatCheckPreallocated(C, 3); 1016 PetscCheck(C->assembled, PetscObjectComm((PetscObject)C), PETSC_ERR_ARG_WRONGSTATE, "Not for unassembled matrix"); 1017 PetscCheck(!C->factortype, PetscObjectComm((PetscObject)C), PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix"); 1018 } 1019 1020 PetscValidHeaderSpecific(D, MAT_CLASSID, 4); 1021 PetscValidType(D, 4); 1022 MatCheckPreallocated(D, 4); 1023 PetscCheck(D->assembled, PetscObjectComm((PetscObject)D), PETSC_ERR_ARG_WRONGSTATE, "Not for unassembled matrix"); 1024 PetscCheck(!D->factortype, PetscObjectComm((PetscObject)D), PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix"); 1025 1026 /* Create a supporting struct and attach it to D */ 1027 PetscCall(MatProductClear(D)); 1028 PetscCall(MatProductCreate_Private(A, B, C, D)); 1029 PetscFunctionReturn(PETSC_SUCCESS); 1030 } 1031 1032 /*@ 1033 MatProductCreate - create a matrix to hold the result of a matrix-matrix product operation 1034 1035 Collective 1036 1037 Input Parameters: 1038 + A - the first matrix 1039 . B - the second matrix 1040 - C - the third matrix (or `NULL`) 1041 1042 Output Parameters: 1043 . D - the matrix whose values are to be computed via a matrix-matrix product operation 1044 1045 Level: intermediate 1046 1047 Example: 1048 .vb 1049 MatProductCreate(A,B,C,&D); or MatProductCreateWithMat(A,B,C,D) 1050 MatProductSetType(D, MATPRODUCT_AB or MATPRODUCT_AtB or MATPRODUCT_ABt or MATPRODUCT_PtAP or MATPRODUCT_RARt or MATPRODUCT_ABC) 1051 MatProductSetAlgorithm(D, alg) 1052 MatProductSetFill(D,fill) 1053 MatProductSetFromOptions(D) 1054 MatProductSymbolic(D) 1055 MatProductNumeric(D) 1056 Change numerical values in some of the matrices 1057 MatProductNumeric(D) 1058 .ve 1059 1060 Notes: 1061 Use `MatProductCreateWithMat()` if the matrix you wish computed, the `D` matrix, already exists. 1062 1063 The information computed during the symbolic stage can be reused for new numerical computations with the same non-zero structure 1064 1065 Developer Note: 1066 It is undocumented what happens if the nonzero structure of the input matrices changes. Is the symbolic stage automatically redone? Does it crash? 1067 Is there error checking for it? 1068 1069 .seealso: [](chapter_matrices), `MatProduct`, `Mat`, `MatProductCreateWithMat()`, `MatProductSetType()`, `MatProductSetAlgorithm()`, `MatProductClear()` 1070 @*/ 1071 PetscErrorCode MatProductCreate(Mat A, Mat B, Mat C, Mat *D) 1072 { 1073 PetscFunctionBegin; 1074 PetscValidHeaderSpecific(A, MAT_CLASSID, 1); 1075 PetscValidType(A, 1); 1076 PetscValidHeaderSpecific(B, MAT_CLASSID, 2); 1077 PetscValidType(B, 2); 1078 PetscCheck(!A->factortype, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix A"); 1079 PetscCheck(!B->factortype, PetscObjectComm((PetscObject)B), PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix B"); 1080 1081 if (C) { 1082 PetscValidHeaderSpecific(C, MAT_CLASSID, 3); 1083 PetscValidType(C, 3); 1084 PetscCheck(!C->factortype, PetscObjectComm((PetscObject)C), PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix C"); 1085 } 1086 1087 PetscValidPointer(D, 4); 1088 PetscCall(MatCreate(PetscObjectComm((PetscObject)A), D)); 1089 /* Delay setting type of D to the MatProduct symbolic phase, as we allow sparse A and dense B */ 1090 PetscCall(MatProductCreate_Private(A, B, C, *D)); 1091 PetscFunctionReturn(PETSC_SUCCESS); 1092 } 1093 1094 /* 1095 These are safe basic implementations of ABC, RARt and PtAP 1096 that do not rely on mat->ops->matmatop function pointers. 1097 They only use the MatProduct API and are currently used by 1098 cuSPARSE and KOKKOS-KERNELS backends 1099 */ 1100 typedef struct { 1101 Mat BC; 1102 Mat ABC; 1103 } MatMatMatPrivate; 1104 1105 static PetscErrorCode MatDestroy_MatMatMatPrivate(void *data) 1106 { 1107 MatMatMatPrivate *mmdata = (MatMatMatPrivate *)data; 1108 1109 PetscFunctionBegin; 1110 PetscCall(MatDestroy(&mmdata->BC)); 1111 PetscCall(MatDestroy(&mmdata->ABC)); 1112 PetscCall(PetscFree(data)); 1113 PetscFunctionReturn(PETSC_SUCCESS); 1114 } 1115 1116 static PetscErrorCode MatProductNumeric_ABC_Basic(Mat mat) 1117 { 1118 Mat_Product *product = mat->product; 1119 MatMatMatPrivate *mmabc; 1120 1121 PetscFunctionBegin; 1122 MatCheckProduct(mat, 1); 1123 PetscCheck(mat->product->data, PetscObjectComm((PetscObject)mat), PETSC_ERR_PLIB, "Product data empty"); 1124 mmabc = (MatMatMatPrivate *)mat->product->data; 1125 PetscCheck(mmabc->BC->ops->productnumeric, PetscObjectComm((PetscObject)mat), PETSC_ERR_PLIB, "Missing numeric stage"); 1126 /* use function pointer directly to prevent logging */ 1127 PetscCall((*mmabc->BC->ops->productnumeric)(mmabc->BC)); 1128 /* swap ABC product stuff with that of ABC for the numeric phase on mat */ 1129 mat->product = mmabc->ABC->product; 1130 mat->ops->productnumeric = mmabc->ABC->ops->productnumeric; 1131 /* use function pointer directly to prevent logging */ 1132 PetscUseTypeMethod(mat, productnumeric); 1133 mat->ops->productnumeric = MatProductNumeric_ABC_Basic; 1134 mat->product = product; 1135 PetscFunctionReturn(PETSC_SUCCESS); 1136 } 1137 1138 PetscErrorCode MatProductSymbolic_ABC_Basic(Mat mat) 1139 { 1140 Mat_Product *product = mat->product; 1141 Mat A, B, C; 1142 MatProductType p1, p2; 1143 MatMatMatPrivate *mmabc; 1144 const char *prefix; 1145 1146 PetscFunctionBegin; 1147 MatCheckProduct(mat, 1); 1148 PetscCheck(!mat->product->data, PetscObjectComm((PetscObject)mat), PETSC_ERR_PLIB, "Product data not empty"); 1149 PetscCall(MatGetOptionsPrefix(mat, &prefix)); 1150 PetscCall(PetscNew(&mmabc)); 1151 product->data = mmabc; 1152 product->destroy = MatDestroy_MatMatMatPrivate; 1153 switch (product->type) { 1154 case MATPRODUCT_PtAP: 1155 p1 = MATPRODUCT_AB; 1156 p2 = MATPRODUCT_AtB; 1157 A = product->B; 1158 B = product->A; 1159 C = product->B; 1160 break; 1161 case MATPRODUCT_RARt: 1162 p1 = MATPRODUCT_ABt; 1163 p2 = MATPRODUCT_AB; 1164 A = product->B; 1165 B = product->A; 1166 C = product->B; 1167 break; 1168 case MATPRODUCT_ABC: 1169 p1 = MATPRODUCT_AB; 1170 p2 = MATPRODUCT_AB; 1171 A = product->A; 1172 B = product->B; 1173 C = product->C; 1174 break; 1175 default: 1176 SETERRQ(PetscObjectComm((PetscObject)mat), PETSC_ERR_PLIB, "Not for ProductType %s", MatProductTypes[product->type]); 1177 } 1178 PetscCall(MatProductCreate(B, C, NULL, &mmabc->BC)); 1179 PetscCall(MatSetOptionsPrefix(mmabc->BC, prefix)); 1180 PetscCall(MatAppendOptionsPrefix(mmabc->BC, "P1_")); 1181 PetscCall(MatProductSetType(mmabc->BC, p1)); 1182 PetscCall(MatProductSetAlgorithm(mmabc->BC, MATPRODUCTALGORITHMDEFAULT)); 1183 PetscCall(MatProductSetFill(mmabc->BC, product->fill)); 1184 mmabc->BC->product->api_user = product->api_user; 1185 PetscCall(MatProductSetFromOptions(mmabc->BC)); 1186 PetscCheck(mmabc->BC->ops->productsymbolic, PetscObjectComm((PetscObject)mat), PETSC_ERR_SUP, "Symbolic ProductType %s not supported with %s and %s", MatProductTypes[p1], ((PetscObject)B)->type_name, ((PetscObject)C)->type_name); 1187 /* use function pointer directly to prevent logging */ 1188 PetscCall((*mmabc->BC->ops->productsymbolic)(mmabc->BC)); 1189 1190 PetscCall(MatProductCreate(A, mmabc->BC, NULL, &mmabc->ABC)); 1191 PetscCall(MatSetOptionsPrefix(mmabc->ABC, prefix)); 1192 PetscCall(MatAppendOptionsPrefix(mmabc->ABC, "P2_")); 1193 PetscCall(MatProductSetType(mmabc->ABC, p2)); 1194 PetscCall(MatProductSetAlgorithm(mmabc->ABC, MATPRODUCTALGORITHMDEFAULT)); 1195 PetscCall(MatProductSetFill(mmabc->ABC, product->fill)); 1196 mmabc->ABC->product->api_user = product->api_user; 1197 PetscCall(MatProductSetFromOptions(mmabc->ABC)); 1198 PetscCheck(mmabc->ABC->ops->productsymbolic, PetscObjectComm((PetscObject)mat), PETSC_ERR_SUP, "Symbolic ProductType %s not supported with %s and %s", MatProductTypes[p2], ((PetscObject)A)->type_name, ((PetscObject)mmabc->BC)->type_name); 1199 /* swap ABC product stuff with that of ABC for the symbolic phase on mat */ 1200 mat->product = mmabc->ABC->product; 1201 mat->ops->productsymbolic = mmabc->ABC->ops->productsymbolic; 1202 /* use function pointer directly to prevent logging */ 1203 PetscUseTypeMethod(mat, productsymbolic); 1204 mmabc->ABC->ops->productnumeric = mat->ops->productnumeric; 1205 mat->ops->productsymbolic = MatProductSymbolic_ABC_Basic; 1206 mat->ops->productnumeric = MatProductNumeric_ABC_Basic; 1207 mat->product = product; 1208 PetscFunctionReturn(PETSC_SUCCESS); 1209 } 1210 1211 /*@ 1212 MatProductGetType - Returns the type of matrix-matrix product associated with computing values for the given matrix 1213 1214 Not Collective 1215 1216 Input Parameter: 1217 . mat - the matrix whose values are to be computed via a matrix-matrix product operation 1218 1219 Output Parameter: 1220 . mtype - the `MatProductType` 1221 1222 Level: intermediate 1223 1224 .seealso: [](chapter_matrices), `MatProduct`, `Mat`, `MatProductCreateWithMat()`, `MatProductSetType()`, `MatProductCreate()`, `MatProductType`, `MatProductAlgorithm` 1225 @*/ 1226 PetscErrorCode MatProductGetType(Mat mat, MatProductType *mtype) 1227 { 1228 PetscFunctionBegin; 1229 PetscValidHeaderSpecific(mat, MAT_CLASSID, 1); 1230 PetscValidPointer(mtype, 2); 1231 *mtype = MATPRODUCT_UNSPECIFIED; 1232 if (mat->product) *mtype = mat->product->type; 1233 PetscFunctionReturn(PETSC_SUCCESS); 1234 } 1235 1236 /*@ 1237 MatProductGetMats - Returns the matrices associated with the matrix-matrix product associated with computing values for the given matrix 1238 1239 Not Collective 1240 1241 Input Parameter: 1242 . mat - the matrix whose values are to be computed via a matrix-matrix product operation 1243 1244 Output Parameters: 1245 + A - the first matrix 1246 . B - the second matrix 1247 - C - the third matrix (may be `NULL` for some `MatProductType`) 1248 1249 Level: intermediate 1250 1251 .seealso: [](chapter_matrices), `MatProduct`, `Mat`, `MatProductCreateWithMat()`, `MatProductSetType()`, `MatProductSetAlgorithm()`, `MatProductCreate()` 1252 @*/ 1253 PetscErrorCode MatProductGetMats(Mat mat, Mat *A, Mat *B, Mat *C) 1254 { 1255 PetscFunctionBegin; 1256 PetscValidHeaderSpecific(mat, MAT_CLASSID, 1); 1257 if (A) *A = mat->product ? mat->product->A : NULL; 1258 if (B) *B = mat->product ? mat->product->B : NULL; 1259 if (C) *C = mat->product ? mat->product->C : NULL; 1260 PetscFunctionReturn(PETSC_SUCCESS); 1261 } 1262