1 #define PETSCMAT_DLL 2 3 /* 4 This is where the abstract matrix operations are defined 5 */ 6 7 #include "private/matimpl.h" /*I "petscmat.h" I*/ 8 #include "private/vecimpl.h" 9 10 /* Logging support */ 11 PetscCookie PETSCMAT_DLLEXPORT MAT_COOKIE; 12 PetscCookie PETSCMAT_DLLEXPORT MAT_FDCOLORING_COOKIE; 13 14 PetscLogEvent MAT_Mult, MAT_Mults, MAT_MultConstrained, MAT_MultAdd, MAT_MultTranspose; 15 PetscLogEvent MAT_MultTransposeConstrained, MAT_MultTransposeAdd, MAT_Solve, MAT_Solves, MAT_SolveAdd, MAT_SolveTranspose, MAT_MatSolve; 16 PetscLogEvent MAT_SolveTransposeAdd, MAT_SOR, MAT_ForwardSolve, MAT_BackwardSolve, MAT_LUFactor, MAT_LUFactorSymbolic; 17 PetscLogEvent MAT_LUFactorNumeric, MAT_CholeskyFactor, MAT_CholeskyFactorSymbolic, MAT_CholeskyFactorNumeric, MAT_ILUFactor; 18 PetscLogEvent MAT_ILUFactorSymbolic, MAT_ICCFactorSymbolic, MAT_Copy, MAT_Convert, MAT_Scale, MAT_AssemblyBegin; 19 PetscLogEvent MAT_AssemblyEnd, MAT_SetValues, MAT_GetValues, MAT_GetRow, MAT_GetRowIJ, MAT_GetSubMatrices, MAT_GetColoring, MAT_GetOrdering, MAT_GetRedundantMatrix, MAT_GetSeqNonzeroStructure; 20 PetscLogEvent MAT_IncreaseOverlap, MAT_Partitioning, MAT_ZeroEntries, MAT_Load, MAT_View, MAT_AXPY, MAT_FDColoringCreate; 21 PetscLogEvent MAT_FDColoringApply,MAT_Transpose,MAT_FDColoringFunction; 22 PetscLogEvent MAT_MatMult, MAT_MatMultSymbolic, MAT_MatMultNumeric; 23 PetscLogEvent MAT_PtAP, MAT_PtAPSymbolic, MAT_PtAPNumeric; 24 PetscLogEvent MAT_MatMultTranspose, MAT_MatMultTransposeSymbolic, MAT_MatMultTransposeNumeric; 25 PetscLogEvent MAT_MultHermitianTranspose,MAT_MultHermitianTransposeAdd; 26 PetscLogEvent MAT_Getsymtranspose, MAT_Getsymtransreduced, MAT_Transpose_SeqAIJ, MAT_GetBrowsOfAcols; 27 PetscLogEvent MAT_GetBrowsOfAocols, MAT_Getlocalmat, MAT_Getlocalmatcondensed, MAT_Seqstompi, MAT_Seqstompinum, MAT_Seqstompisym; 28 PetscLogEvent MAT_Applypapt, MAT_Applypapt_numeric, MAT_Applypapt_symbolic, MAT_GetSequentialNonzeroStructure; 29 30 /* nasty global values for MatSetValue() */ 31 PetscInt PETSCMAT_DLLEXPORT MatSetValue_Row = 0; 32 PetscInt PETSCMAT_DLLEXPORT MatSetValue_Column = 0; 33 PetscScalar PETSCMAT_DLLEXPORT MatSetValue_Value = 0.0; 34 35 #undef __FUNCT__ 36 #define __FUNCT__ "MatGetDiagonalBlock" 37 /*@ 38 MatGetDiagonalBlock - Returns the part of the matrix associated with the on-process coupling 39 40 Not Collective 41 42 Input Parameters: 43 + mat - the matrix 44 - reuse - indicates you are passing in the a matrix and want it reused 45 46 Output Parameters: 47 + iscopy - indicates a copy of the diagonal matrix was created and you should use MatDestroy() on it 48 - a - the diagonal part (which is a SEQUENTIAL matrix) 49 50 Notes: see the manual page for MatCreateMPIAIJ() for more information on the "diagonal part" of the matrix 51 52 Level: advanced 53 54 @*/ 55 PetscErrorCode PETSCMAT_DLLEXPORT MatGetDiagonalBlock(Mat A,PetscTruth *iscopy,MatReuse reuse,Mat *a) 56 { 57 PetscErrorCode ierr,(*f)(Mat,PetscTruth*,MatReuse,Mat*); 58 PetscMPIInt size; 59 60 PetscFunctionBegin; 61 PetscValidHeaderSpecific(A,MAT_COOKIE,1); 62 PetscValidType(A,1); 63 PetscValidPointer(iscopy,2); 64 PetscValidPointer(a,3); 65 if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 66 if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 67 ierr = MPI_Comm_size(((PetscObject)A)->comm,&size);CHKERRQ(ierr); 68 ierr = PetscObjectQueryFunction((PetscObject)A,"MatGetDiagonalBlock_C",(void (**)(void))&f);CHKERRQ(ierr); 69 if (f) { 70 ierr = (*f)(A,iscopy,reuse,a);CHKERRQ(ierr); 71 } else if (size == 1) { 72 *a = A; 73 *iscopy = PETSC_FALSE; 74 } else { 75 SETERRQ(PETSC_ERR_SUP,"Cannot get diagonal part for this matrix"); 76 } 77 PetscFunctionReturn(0); 78 } 79 80 #undef __FUNCT__ 81 #define __FUNCT__ "MatGetTrace" 82 /*@ 83 MatGetTrace - Gets the trace of a matrix. The sum of the diagonal entries. 84 85 Collective on Mat 86 87 Input Parameters: 88 . mat - the matrix 89 90 Output Parameter: 91 . trace - the sum of the diagonal entries 92 93 Level: advanced 94 95 @*/ 96 PetscErrorCode MatGetTrace(Mat mat,PetscScalar *trace) 97 { 98 PetscErrorCode ierr; 99 Vec diag; 100 101 PetscFunctionBegin; 102 ierr = MatGetVecs(mat,&diag,PETSC_NULL);CHKERRQ(ierr); 103 ierr = MatGetDiagonal(mat,diag);CHKERRQ(ierr); 104 ierr = VecSum(diag,trace);CHKERRQ(ierr); 105 ierr = VecDestroy(diag);CHKERRQ(ierr); 106 PetscFunctionReturn(0); 107 } 108 109 #undef __FUNCT__ 110 #define __FUNCT__ "MatRealPart" 111 /*@ 112 MatRealPart - Zeros out the imaginary part of the matrix 113 114 Collective on Mat 115 116 Input Parameters: 117 . mat - the matrix 118 119 Level: advanced 120 121 122 .seealso: MatImaginaryPart() 123 @*/ 124 PetscErrorCode PETSCMAT_DLLEXPORT MatRealPart(Mat mat) 125 { 126 PetscErrorCode ierr; 127 128 PetscFunctionBegin; 129 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 130 PetscValidType(mat,1); 131 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 132 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 133 if (!mat->ops->realpart) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 134 ierr = MatPreallocated(mat);CHKERRQ(ierr); 135 ierr = (*mat->ops->realpart)(mat);CHKERRQ(ierr); 136 PetscFunctionReturn(0); 137 } 138 139 #undef __FUNCT__ 140 #define __FUNCT__ "MatGetGhosts" 141 /*@C 142 MatGetGhosts - Get the global index of all ghost nodes defined by the sparse matrix 143 144 Collective on Mat 145 146 Input Parameter: 147 . mat - the matrix 148 149 Output Parameters: 150 + nghosts - number of ghosts (note for BAIJ matrices there is one ghost for each block) 151 - ghosts - the global indices of the ghost points 152 153 Notes: the nghosts and ghosts are suitable to pass into VecCreateGhost() 154 155 Level: advanced 156 157 @*/ 158 PetscErrorCode PETSCMAT_DLLEXPORT MatGetGhosts(Mat mat,PetscInt *nghosts,const PetscInt *ghosts[]) 159 { 160 PetscErrorCode ierr; 161 162 PetscFunctionBegin; 163 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 164 PetscValidType(mat,1); 165 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 166 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 167 if (!mat->ops->getghosts) { 168 if (nghosts) *nghosts = 0; 169 if (ghosts) *ghosts = 0; 170 } else { 171 ierr = (*mat->ops->getghosts)(mat,nghosts,ghosts);CHKERRQ(ierr); 172 } 173 PetscFunctionReturn(0); 174 } 175 176 177 #undef __FUNCT__ 178 #define __FUNCT__ "MatImaginaryPart" 179 /*@ 180 MatImaginaryPart - Moves the imaginary part of the matrix to the real part and zeros the imaginary part 181 182 Collective on Mat 183 184 Input Parameters: 185 . mat - the matrix 186 187 Level: advanced 188 189 190 .seealso: MatRealPart() 191 @*/ 192 PetscErrorCode PETSCMAT_DLLEXPORT MatImaginaryPart(Mat mat) 193 { 194 PetscErrorCode ierr; 195 196 PetscFunctionBegin; 197 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 198 PetscValidType(mat,1); 199 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 200 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 201 if (!mat->ops->imaginarypart) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 202 ierr = MatPreallocated(mat);CHKERRQ(ierr); 203 ierr = (*mat->ops->imaginarypart)(mat);CHKERRQ(ierr); 204 PetscFunctionReturn(0); 205 } 206 207 #undef __FUNCT__ 208 #define __FUNCT__ "MatMissingDiagonal" 209 /*@ 210 MatMissingDiagonal - Determine if sparse matrix is missing a diagonal entry (or block entry for BAIJ matrices) 211 212 Collective on Mat 213 214 Input Parameter: 215 . mat - the matrix 216 217 Output Parameters: 218 + missing - is any diagonal missing 219 - dd - first diagonal entry that is missing (optional) 220 221 Level: advanced 222 223 224 .seealso: MatRealPart() 225 @*/ 226 PetscErrorCode PETSCMAT_DLLEXPORT MatMissingDiagonal(Mat mat,PetscTruth *missing,PetscInt *dd) 227 { 228 PetscErrorCode ierr; 229 230 PetscFunctionBegin; 231 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 232 PetscValidType(mat,1); 233 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 234 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 235 if (!mat->ops->missingdiagonal) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 236 ierr = (*mat->ops->missingdiagonal)(mat,missing,dd);CHKERRQ(ierr); 237 PetscFunctionReturn(0); 238 } 239 240 #undef __FUNCT__ 241 #define __FUNCT__ "MatGetRow" 242 /*@C 243 MatGetRow - Gets a row of a matrix. You MUST call MatRestoreRow() 244 for each row that you get to ensure that your application does 245 not bleed memory. 246 247 Not Collective 248 249 Input Parameters: 250 + mat - the matrix 251 - row - the row to get 252 253 Output Parameters: 254 + ncols - if not NULL, the number of nonzeros in the row 255 . cols - if not NULL, the column numbers 256 - vals - if not NULL, the values 257 258 Notes: 259 This routine is provided for people who need to have direct access 260 to the structure of a matrix. We hope that we provide enough 261 high-level matrix routines that few users will need it. 262 263 MatGetRow() always returns 0-based column indices, regardless of 264 whether the internal representation is 0-based (default) or 1-based. 265 266 For better efficiency, set cols and/or vals to PETSC_NULL if you do 267 not wish to extract these quantities. 268 269 The user can only examine the values extracted with MatGetRow(); 270 the values cannot be altered. To change the matrix entries, one 271 must use MatSetValues(). 272 273 You can only have one call to MatGetRow() outstanding for a particular 274 matrix at a time, per processor. MatGetRow() can only obtain rows 275 associated with the given processor, it cannot get rows from the 276 other processors; for that we suggest using MatGetSubMatrices(), then 277 MatGetRow() on the submatrix. The row indix passed to MatGetRows() 278 is in the global number of rows. 279 280 Fortran Notes: 281 The calling sequence from Fortran is 282 .vb 283 MatGetRow(matrix,row,ncols,cols,values,ierr) 284 Mat matrix (input) 285 integer row (input) 286 integer ncols (output) 287 integer cols(maxcols) (output) 288 double precision (or double complex) values(maxcols) output 289 .ve 290 where maxcols >= maximum nonzeros in any row of the matrix. 291 292 293 Caution: 294 Do not try to change the contents of the output arrays (cols and vals). 295 In some cases, this may corrupt the matrix. 296 297 Level: advanced 298 299 Concepts: matrices^row access 300 301 .seealso: MatRestoreRow(), MatSetValues(), MatGetValues(), MatGetSubMatrices(), MatGetDiagonal() 302 @*/ 303 PetscErrorCode PETSCMAT_DLLEXPORT MatGetRow(Mat mat,PetscInt row,PetscInt *ncols,const PetscInt *cols[],const PetscScalar *vals[]) 304 { 305 PetscErrorCode ierr; 306 PetscInt incols; 307 308 PetscFunctionBegin; 309 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 310 PetscValidType(mat,1); 311 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 312 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 313 if (!mat->ops->getrow) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 314 ierr = MatPreallocated(mat);CHKERRQ(ierr); 315 ierr = PetscLogEventBegin(MAT_GetRow,mat,0,0,0);CHKERRQ(ierr); 316 ierr = (*mat->ops->getrow)(mat,row,&incols,(PetscInt **)cols,(PetscScalar **)vals);CHKERRQ(ierr); 317 if (ncols) *ncols = incols; 318 ierr = PetscLogEventEnd(MAT_GetRow,mat,0,0,0);CHKERRQ(ierr); 319 PetscFunctionReturn(0); 320 } 321 322 #undef __FUNCT__ 323 #define __FUNCT__ "MatConjugate" 324 /*@ 325 MatConjugate - replaces the matrix values with their complex conjugates 326 327 Collective on Mat 328 329 Input Parameters: 330 . mat - the matrix 331 332 Level: advanced 333 334 .seealso: VecConjugate() 335 @*/ 336 PetscErrorCode PETSCMAT_DLLEXPORT MatConjugate(Mat mat) 337 { 338 PetscErrorCode ierr; 339 340 PetscFunctionBegin; 341 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 342 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 343 if (!mat->ops->conjugate) SETERRQ(PETSC_ERR_SUP,"Not provided for this matrix format, send email to petsc-maint@mcs.anl.gov"); 344 ierr = (*mat->ops->conjugate)(mat);CHKERRQ(ierr); 345 PetscFunctionReturn(0); 346 } 347 348 #undef __FUNCT__ 349 #define __FUNCT__ "MatRestoreRow" 350 /*@C 351 MatRestoreRow - Frees any temporary space allocated by MatGetRow(). 352 353 Not Collective 354 355 Input Parameters: 356 + mat - the matrix 357 . row - the row to get 358 . ncols, cols - the number of nonzeros and their columns 359 - vals - if nonzero the column values 360 361 Notes: 362 This routine should be called after you have finished examining the entries. 363 364 Fortran Notes: 365 The calling sequence from Fortran is 366 .vb 367 MatRestoreRow(matrix,row,ncols,cols,values,ierr) 368 Mat matrix (input) 369 integer row (input) 370 integer ncols (output) 371 integer cols(maxcols) (output) 372 double precision (or double complex) values(maxcols) output 373 .ve 374 Where maxcols >= maximum nonzeros in any row of the matrix. 375 376 In Fortran MatRestoreRow() MUST be called after MatGetRow() 377 before another call to MatGetRow() can be made. 378 379 Level: advanced 380 381 .seealso: MatGetRow() 382 @*/ 383 PetscErrorCode PETSCMAT_DLLEXPORT MatRestoreRow(Mat mat,PetscInt row,PetscInt *ncols,const PetscInt *cols[],const PetscScalar *vals[]) 384 { 385 PetscErrorCode ierr; 386 387 PetscFunctionBegin; 388 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 389 PetscValidIntPointer(ncols,3); 390 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 391 if (!mat->ops->restorerow) PetscFunctionReturn(0); 392 ierr = (*mat->ops->restorerow)(mat,row,ncols,(PetscInt **)cols,(PetscScalar **)vals);CHKERRQ(ierr); 393 PetscFunctionReturn(0); 394 } 395 396 #undef __FUNCT__ 397 #define __FUNCT__ "MatGetRowUpperTriangular" 398 /*@ 399 MatGetRowUpperTriangular - Sets a flag to enable calls to MatGetRow() for matrix in MATSBAIJ format. 400 You should call MatRestoreRowUpperTriangular() after calling MatGetRow/MatRestoreRow() to disable the flag. 401 402 Not Collective 403 404 Input Parameters: 405 + mat - the matrix 406 407 Notes: 408 The flag is to ensure that users are aware of MatGetRow() only provides the upper trianglular part of the row for the matrices in MATSBAIJ format. 409 410 Level: advanced 411 412 Concepts: matrices^row access 413 414 .seealso: MatRestoreRowRowUpperTriangular() 415 @*/ 416 PetscErrorCode PETSCMAT_DLLEXPORT MatGetRowUpperTriangular(Mat mat) 417 { 418 PetscErrorCode ierr; 419 420 PetscFunctionBegin; 421 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 422 PetscValidType(mat,1); 423 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 424 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 425 if (!mat->ops->getrowuppertriangular) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 426 ierr = MatPreallocated(mat);CHKERRQ(ierr); 427 ierr = (*mat->ops->getrowuppertriangular)(mat);CHKERRQ(ierr); 428 PetscFunctionReturn(0); 429 } 430 431 #undef __FUNCT__ 432 #define __FUNCT__ "MatRestoreRowUpperTriangular" 433 /*@ 434 MatRestoreRowUpperTriangular - Disable calls to MatGetRow() for matrix in MATSBAIJ format. 435 436 Not Collective 437 438 Input Parameters: 439 + mat - the matrix 440 441 Notes: 442 This routine should be called after you have finished MatGetRow/MatRestoreRow(). 443 444 445 Level: advanced 446 447 .seealso: MatGetRowUpperTriangular() 448 @*/ 449 PetscErrorCode PETSCMAT_DLLEXPORT MatRestoreRowUpperTriangular(Mat mat) 450 { 451 PetscErrorCode ierr; 452 453 PetscFunctionBegin; 454 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 455 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 456 if (!mat->ops->restorerowuppertriangular) PetscFunctionReturn(0); 457 ierr = (*mat->ops->restorerowuppertriangular)(mat);CHKERRQ(ierr); 458 PetscFunctionReturn(0); 459 } 460 461 #undef __FUNCT__ 462 #define __FUNCT__ "MatSetOptionsPrefix" 463 /*@C 464 MatSetOptionsPrefix - Sets the prefix used for searching for all 465 Mat options in the database. 466 467 Collective on Mat 468 469 Input Parameter: 470 + A - the Mat context 471 - prefix - the prefix to prepend to all option names 472 473 Notes: 474 A hyphen (-) must NOT be given at the beginning of the prefix name. 475 The first character of all runtime options is AUTOMATICALLY the hyphen. 476 477 Level: advanced 478 479 .keywords: Mat, set, options, prefix, database 480 481 .seealso: MatSetFromOptions() 482 @*/ 483 PetscErrorCode PETSCMAT_DLLEXPORT MatSetOptionsPrefix(Mat A,const char prefix[]) 484 { 485 PetscErrorCode ierr; 486 487 PetscFunctionBegin; 488 PetscValidHeaderSpecific(A,MAT_COOKIE,1); 489 ierr = PetscObjectSetOptionsPrefix((PetscObject)A,prefix);CHKERRQ(ierr); 490 PetscFunctionReturn(0); 491 } 492 493 #undef __FUNCT__ 494 #define __FUNCT__ "MatAppendOptionsPrefix" 495 /*@C 496 MatAppendOptionsPrefix - Appends to the prefix used for searching for all 497 Mat options in the database. 498 499 Collective on Mat 500 501 Input Parameters: 502 + A - the Mat context 503 - prefix - the prefix to prepend to all option names 504 505 Notes: 506 A hyphen (-) must NOT be given at the beginning of the prefix name. 507 The first character of all runtime options is AUTOMATICALLY the hyphen. 508 509 Level: advanced 510 511 .keywords: Mat, append, options, prefix, database 512 513 .seealso: MatGetOptionsPrefix() 514 @*/ 515 PetscErrorCode PETSCMAT_DLLEXPORT MatAppendOptionsPrefix(Mat A,const char prefix[]) 516 { 517 PetscErrorCode ierr; 518 519 PetscFunctionBegin; 520 PetscValidHeaderSpecific(A,MAT_COOKIE,1); 521 ierr = PetscObjectAppendOptionsPrefix((PetscObject)A,prefix);CHKERRQ(ierr); 522 PetscFunctionReturn(0); 523 } 524 525 #undef __FUNCT__ 526 #define __FUNCT__ "MatGetOptionsPrefix" 527 /*@C 528 MatGetOptionsPrefix - Sets the prefix used for searching for all 529 Mat options in the database. 530 531 Not Collective 532 533 Input Parameter: 534 . A - the Mat context 535 536 Output Parameter: 537 . prefix - pointer to the prefix string used 538 539 Notes: On the fortran side, the user should pass in a string 'prefix' of 540 sufficient length to hold the prefix. 541 542 Level: advanced 543 544 .keywords: Mat, get, options, prefix, database 545 546 .seealso: MatAppendOptionsPrefix() 547 @*/ 548 PetscErrorCode PETSCMAT_DLLEXPORT MatGetOptionsPrefix(Mat A,const char *prefix[]) 549 { 550 PetscErrorCode ierr; 551 552 PetscFunctionBegin; 553 PetscValidHeaderSpecific(A,MAT_COOKIE,1); 554 ierr = PetscObjectGetOptionsPrefix((PetscObject)A,prefix);CHKERRQ(ierr); 555 PetscFunctionReturn(0); 556 } 557 558 #undef __FUNCT__ 559 #define __FUNCT__ "MatSetUp" 560 /*@ 561 MatSetUp - Sets up the internal matrix data structures for the later use. 562 563 Collective on Mat 564 565 Input Parameters: 566 . A - the Mat context 567 568 Notes: 569 For basic use of the Mat classes the user need not explicitly call 570 MatSetUp(), since these actions will happen automatically. 571 572 Level: advanced 573 574 .keywords: Mat, setup 575 576 .seealso: MatCreate(), MatDestroy() 577 @*/ 578 PetscErrorCode PETSCMAT_DLLEXPORT MatSetUp(Mat A) 579 { 580 PetscMPIInt size; 581 PetscErrorCode ierr; 582 583 PetscFunctionBegin; 584 PetscValidHeaderSpecific(A,MAT_COOKIE,1); 585 if (!((PetscObject)A)->type_name) { 586 ierr = MPI_Comm_size(((PetscObject)A)->comm, &size);CHKERRQ(ierr); 587 if (size == 1) { 588 ierr = MatSetType(A, MATSEQAIJ);CHKERRQ(ierr); 589 } else { 590 ierr = MatSetType(A, MATMPIAIJ);CHKERRQ(ierr); 591 } 592 } 593 ierr = MatSetUpPreallocation(A);CHKERRQ(ierr); 594 PetscFunctionReturn(0); 595 } 596 597 #undef __FUNCT__ 598 #define __FUNCT__ "MatView" 599 /*@C 600 MatView - Visualizes a matrix object. 601 602 Collective on Mat 603 604 Input Parameters: 605 + mat - the matrix 606 - viewer - visualization context 607 608 Notes: 609 The available visualization contexts include 610 + PETSC_VIEWER_STDOUT_SELF - standard output (default) 611 . PETSC_VIEWER_STDOUT_WORLD - synchronized standard 612 output where only the first processor opens 613 the file. All other processors send their 614 data to the first processor to print. 615 - PETSC_VIEWER_DRAW_WORLD - graphical display of nonzero structure 616 617 The user can open alternative visualization contexts with 618 + PetscViewerASCIIOpen() - Outputs matrix to a specified file 619 . PetscViewerBinaryOpen() - Outputs matrix in binary to a 620 specified file; corresponding input uses MatLoad() 621 . PetscViewerDrawOpen() - Outputs nonzero matrix structure to 622 an X window display 623 - PetscViewerSocketOpen() - Outputs matrix to Socket viewer. 624 Currently only the sequential dense and AIJ 625 matrix types support the Socket viewer. 626 627 The user can call PetscViewerSetFormat() to specify the output 628 format of ASCII printed objects (when using PETSC_VIEWER_STDOUT_SELF, 629 PETSC_VIEWER_STDOUT_WORLD and PetscViewerASCIIOpen). Available formats include 630 + PETSC_VIEWER_DEFAULT - default, prints matrix contents 631 . PETSC_VIEWER_ASCII_MATLAB - prints matrix contents in Matlab format 632 . PETSC_VIEWER_ASCII_DENSE - prints entire matrix including zeros 633 . PETSC_VIEWER_ASCII_COMMON - prints matrix contents, using a sparse 634 format common among all matrix types 635 . PETSC_VIEWER_ASCII_IMPL - prints matrix contents, using an implementation-specific 636 format (which is in many cases the same as the default) 637 . PETSC_VIEWER_ASCII_INFO - prints basic information about the matrix 638 size and structure (not the matrix entries) 639 . PETSC_VIEWER_ASCII_INFO_DETAIL - prints more detailed information about 640 the matrix structure 641 642 Options Database Keys: 643 + -mat_view_info - Prints info on matrix at conclusion of MatEndAssembly() 644 . -mat_view_info_detailed - Prints more detailed info 645 . -mat_view - Prints matrix in ASCII format 646 . -mat_view_matlab - Prints matrix in Matlab format 647 . -mat_view_draw - PetscDraws nonzero structure of matrix, using MatView() and PetscDrawOpenX(). 648 . -display <name> - Sets display name (default is host) 649 . -draw_pause <sec> - Sets number of seconds to pause after display 650 . -mat_view_socket - Sends matrix to socket, can be accessed from Matlab (see users manual) 651 . -viewer_socket_machine <machine> 652 . -viewer_socket_port <port> 653 . -mat_view_binary - save matrix to file in binary format 654 - -viewer_binary_filename <name> 655 Level: beginner 656 657 Notes: see the manual page for MatLoad() for the exact format of the binary file when the binary 658 viewer is used. 659 660 See bin/matlab/PetscBinaryRead.m for a Matlab code that can read in the binary file when the binary 661 viewer is used. 662 663 Concepts: matrices^viewing 664 Concepts: matrices^plotting 665 Concepts: matrices^printing 666 667 .seealso: PetscViewerSetFormat(), PetscViewerASCIIOpen(), PetscViewerDrawOpen(), 668 PetscViewerSocketOpen(), PetscViewerBinaryOpen(), MatLoad() 669 @*/ 670 PetscErrorCode PETSCMAT_DLLEXPORT MatView(Mat mat,PetscViewer viewer) 671 { 672 PetscErrorCode ierr; 673 PetscInt rows,cols; 674 PetscTruth iascii; 675 const MatType cstr; 676 PetscViewerFormat format; 677 678 PetscFunctionBegin; 679 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 680 PetscValidType(mat,1); 681 if (!viewer) { 682 ierr = PetscViewerASCIIGetStdout(((PetscObject)mat)->comm,&viewer);CHKERRQ(ierr); 683 } 684 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_COOKIE,2); 685 PetscCheckSameComm(mat,1,viewer,2); 686 if (!mat->assembled) SETERRQ(PETSC_ERR_ORDER,"Must call MatAssemblyBegin/End() before viewing matrix"); 687 ierr = MatPreallocated(mat);CHKERRQ(ierr); 688 689 ierr = PetscLogEventBegin(MAT_View,mat,viewer,0,0);CHKERRQ(ierr); 690 ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr); 691 if (iascii) { 692 ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 693 if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) { 694 if (((PetscObject)mat)->prefix) { 695 ierr = PetscViewerASCIIPrintf(viewer,"Matrix Object:(%s)\n",((PetscObject)mat)->prefix);CHKERRQ(ierr); 696 } else { 697 ierr = PetscViewerASCIIPrintf(viewer,"Matrix Object:\n");CHKERRQ(ierr); 698 } 699 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 700 ierr = MatGetType(mat,&cstr);CHKERRQ(ierr); 701 ierr = MatGetSize(mat,&rows,&cols);CHKERRQ(ierr); 702 ierr = PetscViewerASCIIPrintf(viewer,"type=%s, rows=%D, cols=%D\n",cstr,rows,cols);CHKERRQ(ierr); 703 if (mat->factor) { 704 const MatSolverPackage solver; 705 ierr = MatFactorGetSolverPackage(mat,&solver);CHKERRQ(ierr); 706 ierr = PetscViewerASCIIPrintf(viewer,"package used to perform factorization: %s\n",solver);CHKERRQ(ierr); 707 } 708 if (mat->ops->getinfo) { 709 MatInfo info; 710 ierr = MatGetInfo(mat,MAT_GLOBAL_SUM,&info);CHKERRQ(ierr); 711 ierr = PetscViewerASCIIPrintf(viewer,"total: nonzeros=%D, allocated nonzeros=%D\n",(PetscInt)info.nz_used,(PetscInt)info.nz_allocated);CHKERRQ(ierr); 712 } 713 } 714 } 715 if (mat->ops->view) { 716 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 717 ierr = (*mat->ops->view)(mat,viewer);CHKERRQ(ierr); 718 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 719 } else if (!iascii) { 720 SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported",((PetscObject)viewer)->type_name); 721 } 722 if (iascii) { 723 ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 724 if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) { 725 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 726 } 727 } 728 ierr = PetscLogEventEnd(MAT_View,mat,viewer,0,0);CHKERRQ(ierr); 729 PetscFunctionReturn(0); 730 } 731 732 #undef __FUNCT__ 733 #define __FUNCT__ "MatScaleSystem" 734 /*@ 735 MatScaleSystem - Scale a vector solution and right hand side to 736 match the scaling of a scaled matrix. 737 738 Collective on Mat 739 740 Input Parameter: 741 + mat - the matrix 742 . b - right hand side vector (or PETSC_NULL) 743 - x - solution vector (or PETSC_NULL) 744 745 746 Notes: 747 For AIJ, and BAIJ matrix formats, the matrices are not 748 internally scaled, so this does nothing. 749 750 The KSP methods automatically call this routine when required 751 (via PCPreSolve()) so it is rarely used directly. 752 753 Level: Developer 754 755 Concepts: matrices^scaling 756 757 .seealso: MatUseScaledForm(), MatUnScaleSystem() 758 @*/ 759 PetscErrorCode PETSCMAT_DLLEXPORT MatScaleSystem(Mat mat,Vec b,Vec x) 760 { 761 PetscErrorCode ierr; 762 763 PetscFunctionBegin; 764 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 765 PetscValidType(mat,1); 766 ierr = MatPreallocated(mat);CHKERRQ(ierr); 767 if (x) {PetscValidHeaderSpecific(x,VEC_COOKIE,3);PetscCheckSameComm(mat,1,x,3);} 768 if (b) {PetscValidHeaderSpecific(b,VEC_COOKIE,2);PetscCheckSameComm(mat,1,b,2);} 769 770 if (mat->ops->scalesystem) { 771 ierr = (*mat->ops->scalesystem)(mat,b,x);CHKERRQ(ierr); 772 } 773 PetscFunctionReturn(0); 774 } 775 776 #undef __FUNCT__ 777 #define __FUNCT__ "MatUnScaleSystem" 778 /*@ 779 MatUnScaleSystem - Unscales a vector solution and right hand side to 780 match the original scaling of a scaled matrix. 781 782 Collective on Mat 783 784 Input Parameter: 785 + mat - the matrix 786 . b - right hand side vector (or PETSC_NULL) 787 - x - solution vector (or PETSC_NULL) 788 789 790 Notes: 791 For AIJ and BAIJ matrix formats, the matrices are not 792 internally scaled, so this does nothing. 793 794 The KSP methods automatically call this routine when required 795 (via PCPreSolve()) so it is rarely used directly. 796 797 Level: Developer 798 799 .seealso: MatUseScaledForm(), MatScaleSystem() 800 @*/ 801 PetscErrorCode PETSCMAT_DLLEXPORT MatUnScaleSystem(Mat mat,Vec b,Vec x) 802 { 803 PetscErrorCode ierr; 804 805 PetscFunctionBegin; 806 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 807 PetscValidType(mat,1); 808 ierr = MatPreallocated(mat);CHKERRQ(ierr); 809 if (x) {PetscValidHeaderSpecific(x,VEC_COOKIE,3);PetscCheckSameComm(mat,1,x,3);} 810 if (b) {PetscValidHeaderSpecific(b,VEC_COOKIE,2);PetscCheckSameComm(mat,1,b,2);} 811 if (mat->ops->unscalesystem) { 812 ierr = (*mat->ops->unscalesystem)(mat,b,x);CHKERRQ(ierr); 813 } 814 PetscFunctionReturn(0); 815 } 816 817 #undef __FUNCT__ 818 #define __FUNCT__ "MatUseScaledForm" 819 /*@ 820 MatUseScaledForm - For matrix storage formats that scale the 821 matrix indicates matrix operations (MatMult() etc) are 822 applied using the scaled matrix. 823 824 Collective on Mat 825 826 Input Parameter: 827 + mat - the matrix 828 - scaled - PETSC_TRUE for applying the scaled, PETSC_FALSE for 829 applying the original matrix 830 831 Notes: 832 For scaled matrix formats, applying the original, unscaled matrix 833 will be slightly more expensive 834 835 Level: Developer 836 837 .seealso: MatScaleSystem(), MatUnScaleSystem() 838 @*/ 839 PetscErrorCode PETSCMAT_DLLEXPORT MatUseScaledForm(Mat mat,PetscTruth scaled) 840 { 841 PetscErrorCode ierr; 842 843 PetscFunctionBegin; 844 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 845 PetscValidType(mat,1); 846 ierr = MatPreallocated(mat);CHKERRQ(ierr); 847 if (mat->ops->usescaledform) { 848 ierr = (*mat->ops->usescaledform)(mat,scaled);CHKERRQ(ierr); 849 } 850 PetscFunctionReturn(0); 851 } 852 853 #undef __FUNCT__ 854 #define __FUNCT__ "MatDestroy" 855 /*@ 856 MatDestroy - Frees space taken by a matrix. 857 858 Collective on Mat 859 860 Input Parameter: 861 . A - the matrix 862 863 Level: beginner 864 865 @*/ 866 PetscErrorCode PETSCMAT_DLLEXPORT MatDestroy(Mat A) 867 { 868 PetscErrorCode ierr; 869 PetscFunctionBegin; 870 PetscValidHeaderSpecific(A,MAT_COOKIE,1); 871 if (--((PetscObject)A)->refct > 0) PetscFunctionReturn(0); 872 ierr = MatPreallocated(A);CHKERRQ(ierr); 873 /* if memory was published with AMS then destroy it */ 874 ierr = PetscObjectDepublish(A);CHKERRQ(ierr); 875 if (A->ops->destroy) { 876 ierr = (*A->ops->destroy)(A);CHKERRQ(ierr); 877 } 878 if (A->mapping) { 879 ierr = ISLocalToGlobalMappingDestroy(A->mapping);CHKERRQ(ierr); 880 } 881 if (A->bmapping) { 882 ierr = ISLocalToGlobalMappingDestroy(A->bmapping);CHKERRQ(ierr); 883 } 884 885 if (A->spptr){ierr = PetscFree(A->spptr);CHKERRQ(ierr);} 886 ierr = PetscLayoutDestroy(A->rmap);CHKERRQ(ierr); 887 ierr = PetscLayoutDestroy(A->cmap);CHKERRQ(ierr); 888 ierr = PetscHeaderDestroy(A);CHKERRQ(ierr); 889 PetscFunctionReturn(0); 890 } 891 892 #undef __FUNCT__ 893 #define __FUNCT__ "MatValid" 894 /*@ 895 MatValid - Checks whether a matrix object is valid. 896 897 Collective on Mat 898 899 Input Parameter: 900 . m - the matrix to check 901 902 Output Parameter: 903 flg - flag indicating matrix status, either 904 PETSC_TRUE if matrix is valid, or PETSC_FALSE otherwise. 905 906 Level: developer 907 908 Concepts: matrices^validity 909 @*/ 910 PetscErrorCode PETSCMAT_DLLEXPORT MatValid(Mat m,PetscTruth *flg) 911 { 912 PetscFunctionBegin; 913 PetscValidIntPointer(flg,1); 914 if (!m) *flg = PETSC_FALSE; 915 else if (((PetscObject)m)->cookie != MAT_COOKIE) *flg = PETSC_FALSE; 916 else *flg = PETSC_TRUE; 917 PetscFunctionReturn(0); 918 } 919 920 #undef __FUNCT__ 921 #define __FUNCT__ "MatSetValues" 922 /*@ 923 MatSetValues - Inserts or adds a block of values into a matrix. 924 These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd() 925 MUST be called after all calls to MatSetValues() have been completed. 926 927 Not Collective 928 929 Input Parameters: 930 + mat - the matrix 931 . v - a logically two-dimensional array of values 932 . m, idxm - the number of rows and their global indices 933 . n, idxn - the number of columns and their global indices 934 - addv - either ADD_VALUES or INSERT_VALUES, where 935 ADD_VALUES adds values to any existing entries, and 936 INSERT_VALUES replaces existing entries with new values 937 938 Notes: 939 By default the values, v, are row-oriented. See MatSetOption() for other options. 940 941 Calls to MatSetValues() with the INSERT_VALUES and ADD_VALUES 942 options cannot be mixed without intervening calls to the assembly 943 routines. 944 945 MatSetValues() uses 0-based row and column numbers in Fortran 946 as well as in C. 947 948 Negative indices may be passed in idxm and idxn, these rows and columns are 949 simply ignored. This allows easily inserting element stiffness matrices 950 with homogeneous Dirchlet boundary conditions that you don't want represented 951 in the matrix. 952 953 Efficiency Alert: 954 The routine MatSetValuesBlocked() may offer much better efficiency 955 for users of block sparse formats (MATSEQBAIJ and MATMPIBAIJ). 956 957 Level: beginner 958 959 Concepts: matrices^putting entries in 960 961 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(), 962 InsertMode, INSERT_VALUES, ADD_VALUES 963 @*/ 964 PetscErrorCode PETSCMAT_DLLEXPORT MatSetValues(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],const PetscScalar v[],InsertMode addv) 965 { 966 PetscErrorCode ierr; 967 968 PetscFunctionBegin; 969 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 970 PetscValidType(mat,1); 971 if (!m || !n) PetscFunctionReturn(0); /* no values to insert */ 972 PetscValidIntPointer(idxm,3); 973 PetscValidIntPointer(idxn,5); 974 if (v) PetscValidDoublePointer(v,6); 975 ierr = MatPreallocated(mat);CHKERRQ(ierr); 976 if (mat->insertmode == NOT_SET_VALUES) { 977 mat->insertmode = addv; 978 } 979 #if defined(PETSC_USE_DEBUG) 980 else if (mat->insertmode != addv) { 981 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values"); 982 } 983 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 984 #endif 985 986 if (mat->assembled) { 987 mat->was_assembled = PETSC_TRUE; 988 mat->assembled = PETSC_FALSE; 989 } 990 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 991 if (!mat->ops->setvalues) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 992 ierr = (*mat->ops->setvalues)(mat,m,idxm,n,idxn,v,addv);CHKERRQ(ierr); 993 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 994 PetscFunctionReturn(0); 995 } 996 997 998 #undef __FUNCT__ 999 #define __FUNCT__ "MatSetValuesRowLocal" 1000 /*@ 1001 MatSetValuesRowLocal - Inserts a row (block row for BAIJ matrices) of nonzero 1002 values into a matrix 1003 1004 Not Collective 1005 1006 Input Parameters: 1007 + mat - the matrix 1008 . row - the (block) row to set 1009 - v - a logically two-dimensional array of values 1010 1011 Notes: 1012 By the values, v, are column-oriented (for the block version) and sorted 1013 1014 All the nonzeros in the row must be provided 1015 1016 The matrix must have previously had its column indices set 1017 1018 The row must belong to this process 1019 1020 Level: intermediate 1021 1022 Concepts: matrices^putting entries in 1023 1024 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(), 1025 InsertMode, INSERT_VALUES, ADD_VALUES, MatSetValues(), MatSetValuesRow(), MatSetLocalToGlobalMapping() 1026 @*/ 1027 PetscErrorCode PETSCMAT_DLLEXPORT MatSetValuesRowLocal(Mat mat,PetscInt row,const PetscScalar v[]) 1028 { 1029 PetscErrorCode ierr; 1030 1031 PetscFunctionBegin; 1032 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 1033 PetscValidType(mat,1); 1034 PetscValidScalarPointer(v,2); 1035 ierr = MatSetValuesRow(mat, mat->mapping->indices[row],v);CHKERRQ(ierr); 1036 PetscFunctionReturn(0); 1037 } 1038 1039 #undef __FUNCT__ 1040 #define __FUNCT__ "MatSetValuesRow" 1041 /*@ 1042 MatSetValuesRow - Inserts a row (block row for BAIJ matrices) of nonzero 1043 values into a matrix 1044 1045 Not Collective 1046 1047 Input Parameters: 1048 + mat - the matrix 1049 . row - the (block) row to set 1050 - v - a logically two-dimensional array of values 1051 1052 Notes: 1053 The values, v, are column-oriented for the block version. 1054 1055 All the nonzeros in the row must be provided 1056 1057 THE MATRIX MUSAT HAVE PREVIOUSLY HAD ITS COLUMN INDICES SET. IT IS RARE THAT THIS ROUTINE IS USED, usually MatSetValues() is used. 1058 1059 The row must belong to this process 1060 1061 Level: advanced 1062 1063 Concepts: matrices^putting entries in 1064 1065 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(), 1066 InsertMode, INSERT_VALUES, ADD_VALUES, MatSetValues() 1067 @*/ 1068 PetscErrorCode PETSCMAT_DLLEXPORT MatSetValuesRow(Mat mat,PetscInt row,const PetscScalar v[]) 1069 { 1070 PetscErrorCode ierr; 1071 1072 PetscFunctionBegin; 1073 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 1074 PetscValidType(mat,1); 1075 PetscValidScalarPointer(v,2); 1076 #if defined(PETSC_USE_DEBUG) 1077 if (mat->insertmode == ADD_VALUES) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add and insert values"); 1078 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1079 #endif 1080 mat->insertmode = INSERT_VALUES; 1081 1082 if (mat->assembled) { 1083 mat->was_assembled = PETSC_TRUE; 1084 mat->assembled = PETSC_FALSE; 1085 } 1086 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 1087 if (!mat->ops->setvaluesrow) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 1088 ierr = (*mat->ops->setvaluesrow)(mat,row,v);CHKERRQ(ierr); 1089 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 1090 PetscFunctionReturn(0); 1091 } 1092 1093 #undef __FUNCT__ 1094 #define __FUNCT__ "MatSetValuesStencil" 1095 /*@ 1096 MatSetValuesStencil - Inserts or adds a block of values into a matrix. 1097 Using structured grid indexing 1098 1099 Not Collective 1100 1101 Input Parameters: 1102 + mat - the matrix 1103 . m - number of rows being entered 1104 . idxm - grid coordinates (and component number when dof > 1) for matrix rows being entered 1105 . n - number of columns being entered 1106 . idxn - grid coordinates (and component number when dof > 1) for matrix columns being entered 1107 . v - a logically two-dimensional array of values 1108 - addv - either ADD_VALUES or INSERT_VALUES, where 1109 ADD_VALUES adds values to any existing entries, and 1110 INSERT_VALUES replaces existing entries with new values 1111 1112 Notes: 1113 By default the values, v, are row-oriented. See MatSetOption() for other options. 1114 1115 Calls to MatSetValuesStencil() with the INSERT_VALUES and ADD_VALUES 1116 options cannot be mixed without intervening calls to the assembly 1117 routines. 1118 1119 The grid coordinates are across the entire grid, not just the local portion 1120 1121 MatSetValuesStencil() uses 0-based row and column numbers in Fortran 1122 as well as in C. 1123 1124 For setting/accessing vector values via array coordinates you can use the DAVecGetArray() routine 1125 1126 In order to use this routine you must either obtain the matrix with DAGetMatrix() 1127 or call MatSetLocalToGlobalMapping() and MatSetStencil() first. 1128 1129 The columns and rows in the stencil passed in MUST be contained within the 1130 ghost region of the given process as set with DACreateXXX() or MatSetStencil(). For example, 1131 if you create a DA with an overlap of one grid level and on a particular process its first 1132 local nonghost x logical coordinate is 6 (so its first ghost x logical coordinate is 5) the 1133 first i index you can use in your column and row indices in MatSetStencil() is 5. 1134 1135 In Fortran idxm and idxn should be declared as 1136 $ MatStencil idxm(4,m),idxn(4,n) 1137 and the values inserted using 1138 $ idxm(MatStencil_i,1) = i 1139 $ idxm(MatStencil_j,1) = j 1140 $ idxm(MatStencil_k,1) = k 1141 $ idxm(MatStencil_c,1) = c 1142 etc 1143 1144 For periodic boundary conditions use negative indices for values to the left (below 0; that are to be 1145 obtained by wrapping values from right edge). For values to the right of the last entry using that index plus one 1146 etc to obtain values that obtained by wrapping the values from the left edge. This does not work for the DA_NONPERIODIC 1147 wrap. 1148 1149 For indices that don't mean anything for your case (like the k index when working in 2d) or the c index when you have 1150 a single value per point) you can skip filling those indices. 1151 1152 Inspired by the structured grid interface to the HYPRE package 1153 (http://www.llnl.gov/CASC/hypre) 1154 1155 Efficiency Alert: 1156 The routine MatSetValuesBlockedStencil() may offer much better efficiency 1157 for users of block sparse formats (MATSEQBAIJ and MATMPIBAIJ). 1158 1159 Level: beginner 1160 1161 Concepts: matrices^putting entries in 1162 1163 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal() 1164 MatSetValues(), MatSetValuesBlockedStencil(), MatSetStencil(), DAGetMatrix(), DAVecGetArray(), MatStencil 1165 @*/ 1166 PetscErrorCode PETSCMAT_DLLEXPORT MatSetValuesStencil(Mat mat,PetscInt m,const MatStencil idxm[],PetscInt n,const MatStencil idxn[],const PetscScalar v[],InsertMode addv) 1167 { 1168 PetscErrorCode ierr; 1169 PetscInt j,i,jdxm[128],jdxn[256],dim = mat->stencil.dim,*dims = mat->stencil.dims+1,tmp; 1170 PetscInt *starts = mat->stencil.starts,*dxm = (PetscInt*)idxm,*dxn = (PetscInt*)idxn,sdim = dim - (1 - (PetscInt)mat->stencil.noc); 1171 1172 PetscFunctionBegin; 1173 if (!m || !n) PetscFunctionReturn(0); /* no values to insert */ 1174 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 1175 PetscValidType(mat,1); 1176 PetscValidIntPointer(idxm,3); 1177 PetscValidIntPointer(idxn,5); 1178 PetscValidScalarPointer(v,6); 1179 1180 if (m > 128) SETERRQ1(PETSC_ERR_SUP,"Can only set 128 rows at a time; trying to set %D",m); 1181 if (n > 256) SETERRQ1(PETSC_ERR_SUP,"Can only set 256 columns at a time; trying to set %D",n); 1182 1183 for (i=0; i<m; i++) { 1184 for (j=0; j<3-sdim; j++) dxm++; 1185 tmp = *dxm++ - starts[0]; 1186 for (j=0; j<dim-1; j++) { 1187 if ((*dxm++ - starts[j+1]) < 0 || tmp < 0) tmp = PETSC_MIN_INT; 1188 else tmp = tmp*dims[j] + *(dxm-1) - starts[j+1]; 1189 } 1190 if (mat->stencil.noc) dxm++; 1191 jdxm[i] = tmp; 1192 } 1193 for (i=0; i<n; i++) { 1194 for (j=0; j<3-sdim; j++) dxn++; 1195 tmp = *dxn++ - starts[0]; 1196 for (j=0; j<dim-1; j++) { 1197 if ((*dxn++ - starts[j+1]) < 0 || tmp < 0) tmp = PETSC_MIN_INT; 1198 else tmp = tmp*dims[j] + *(dxn-1) - starts[j+1]; 1199 } 1200 if (mat->stencil.noc) dxn++; 1201 jdxn[i] = tmp; 1202 } 1203 ierr = MatSetValuesLocal(mat,m,jdxm,n,jdxn,v,addv);CHKERRQ(ierr); 1204 PetscFunctionReturn(0); 1205 } 1206 1207 #undef __FUNCT__ 1208 #define __FUNCT__ "MatSetValuesBlockedStencil" 1209 /*@C 1210 MatSetValuesBlockedStencil - Inserts or adds a block of values into a matrix. 1211 Using structured grid indexing 1212 1213 Not Collective 1214 1215 Input Parameters: 1216 + mat - the matrix 1217 . m - number of rows being entered 1218 . idxm - grid coordinates for matrix rows being entered 1219 . n - number of columns being entered 1220 . idxn - grid coordinates for matrix columns being entered 1221 . v - a logically two-dimensional array of values 1222 - addv - either ADD_VALUES or INSERT_VALUES, where 1223 ADD_VALUES adds values to any existing entries, and 1224 INSERT_VALUES replaces existing entries with new values 1225 1226 Notes: 1227 By default the values, v, are row-oriented and unsorted. 1228 See MatSetOption() for other options. 1229 1230 Calls to MatSetValuesBlockedStencil() with the INSERT_VALUES and ADD_VALUES 1231 options cannot be mixed without intervening calls to the assembly 1232 routines. 1233 1234 The grid coordinates are across the entire grid, not just the local portion 1235 1236 MatSetValuesBlockedStencil() uses 0-based row and column numbers in Fortran 1237 as well as in C. 1238 1239 For setting/accessing vector values via array coordinates you can use the DAVecGetArray() routine 1240 1241 In order to use this routine you must either obtain the matrix with DAGetMatrix() 1242 or call MatSetBlockSize(), MatSetLocalToGlobalMapping() and MatSetStencil() first. 1243 1244 The columns and rows in the stencil passed in MUST be contained within the 1245 ghost region of the given process as set with DACreateXXX() or MatSetStencil(). For example, 1246 if you create a DA with an overlap of one grid level and on a particular process its first 1247 local nonghost x logical coordinate is 6 (so its first ghost x logical coordinate is 5) the 1248 first i index you can use in your column and row indices in MatSetStencil() is 5. 1249 1250 In Fortran idxm and idxn should be declared as 1251 $ MatStencil idxm(4,m),idxn(4,n) 1252 and the values inserted using 1253 $ idxm(MatStencil_i,1) = i 1254 $ idxm(MatStencil_j,1) = j 1255 $ idxm(MatStencil_k,1) = k 1256 etc 1257 1258 Negative indices may be passed in idxm and idxn, these rows and columns are 1259 simply ignored. This allows easily inserting element stiffness matrices 1260 with homogeneous Dirchlet boundary conditions that you don't want represented 1261 in the matrix. 1262 1263 Inspired by the structured grid interface to the HYPRE package 1264 (http://www.llnl.gov/CASC/hypre) 1265 1266 Level: beginner 1267 1268 Concepts: matrices^putting entries in 1269 1270 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal() 1271 MatSetValues(), MatSetValuesStencil(), MatSetStencil(), DAGetMatrix(), DAVecGetArray(), MatStencil, 1272 MatSetBlockSize(), MatSetLocalToGlobalMapping() 1273 @*/ 1274 PetscErrorCode PETSCMAT_DLLEXPORT MatSetValuesBlockedStencil(Mat mat,PetscInt m,const MatStencil idxm[],PetscInt n,const MatStencil idxn[],const PetscScalar v[],InsertMode addv) 1275 { 1276 PetscErrorCode ierr; 1277 PetscInt j,i,jdxm[128],jdxn[256],dim = mat->stencil.dim,*dims = mat->stencil.dims+1,tmp; 1278 PetscInt *starts = mat->stencil.starts,*dxm = (PetscInt*)idxm,*dxn = (PetscInt*)idxn,sdim = dim - (1 - (PetscInt)mat->stencil.noc); 1279 1280 PetscFunctionBegin; 1281 if (!m || !n) PetscFunctionReturn(0); /* no values to insert */ 1282 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 1283 PetscValidType(mat,1); 1284 PetscValidIntPointer(idxm,3); 1285 PetscValidIntPointer(idxn,5); 1286 PetscValidScalarPointer(v,6); 1287 1288 if (m > 128) SETERRQ1(PETSC_ERR_SUP,"Can only set 128 rows at a time; trying to set %D",m); 1289 if (n > 128) SETERRQ1(PETSC_ERR_SUP,"Can only set 256 columns at a time; trying to set %D",n); 1290 1291 for (i=0; i<m; i++) { 1292 for (j=0; j<3-sdim; j++) dxm++; 1293 tmp = *dxm++ - starts[0]; 1294 for (j=0; j<sdim-1; j++) { 1295 if ((*dxm++ - starts[j+1]) < 0 || tmp < 0) tmp = PETSC_MIN_INT; 1296 else tmp = tmp*dims[j] + *(dxm-1) - starts[j+1]; 1297 } 1298 dxm++; 1299 jdxm[i] = tmp; 1300 } 1301 for (i=0; i<n; i++) { 1302 for (j=0; j<3-sdim; j++) dxn++; 1303 tmp = *dxn++ - starts[0]; 1304 for (j=0; j<sdim-1; j++) { 1305 if ((*dxn++ - starts[j+1]) < 0 || tmp < 0) tmp = PETSC_MIN_INT; 1306 else tmp = tmp*dims[j] + *(dxn-1) - starts[j+1]; 1307 } 1308 dxn++; 1309 jdxn[i] = tmp; 1310 } 1311 ierr = MatSetValuesBlockedLocal(mat,m,jdxm,n,jdxn,v,addv);CHKERRQ(ierr); 1312 PetscFunctionReturn(0); 1313 } 1314 1315 #undef __FUNCT__ 1316 #define __FUNCT__ "MatSetStencil" 1317 /*@ 1318 MatSetStencil - Sets the grid information for setting values into a matrix via 1319 MatSetValuesStencil() 1320 1321 Not Collective 1322 1323 Input Parameters: 1324 + mat - the matrix 1325 . dim - dimension of the grid 1, 2, or 3 1326 . dims - number of grid points in x, y, and z direction, including ghost points on your processor 1327 . starts - starting point of ghost nodes on your processor in x, y, and z direction 1328 - dof - number of degrees of freedom per node 1329 1330 1331 Inspired by the structured grid interface to the HYPRE package 1332 (www.llnl.gov/CASC/hyper) 1333 1334 For matrices generated with DAGetMatrix() this routine is automatically called and so not needed by the 1335 user. 1336 1337 Level: beginner 1338 1339 Concepts: matrices^putting entries in 1340 1341 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal() 1342 MatSetValues(), MatSetValuesBlockedStencil(), MatSetValuesStencil() 1343 @*/ 1344 PetscErrorCode PETSCMAT_DLLEXPORT MatSetStencil(Mat mat,PetscInt dim,const PetscInt dims[],const PetscInt starts[],PetscInt dof) 1345 { 1346 PetscInt i; 1347 1348 PetscFunctionBegin; 1349 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 1350 PetscValidIntPointer(dims,3); 1351 PetscValidIntPointer(starts,4); 1352 1353 mat->stencil.dim = dim + (dof > 1); 1354 for (i=0; i<dim; i++) { 1355 mat->stencil.dims[i] = dims[dim-i-1]; /* copy the values in backwards */ 1356 mat->stencil.starts[i] = starts[dim-i-1]; 1357 } 1358 mat->stencil.dims[dim] = dof; 1359 mat->stencil.starts[dim] = 0; 1360 mat->stencil.noc = (PetscTruth)(dof == 1); 1361 PetscFunctionReturn(0); 1362 } 1363 1364 #undef __FUNCT__ 1365 #define __FUNCT__ "MatSetValuesBlocked" 1366 /*@ 1367 MatSetValuesBlocked - Inserts or adds a block of values into a matrix. 1368 1369 Not Collective 1370 1371 Input Parameters: 1372 + mat - the matrix 1373 . v - a logically two-dimensional array of values 1374 . m, idxm - the number of block rows and their global block indices 1375 . n, idxn - the number of block columns and their global block indices 1376 - addv - either ADD_VALUES or INSERT_VALUES, where 1377 ADD_VALUES adds values to any existing entries, and 1378 INSERT_VALUES replaces existing entries with new values 1379 1380 Notes: 1381 The m and n count the NUMBER of blocks in the row direction and column direction, 1382 NOT the total number of rows/columns; for example, if the block size is 2 and 1383 you are passing in values for rows 2,3,4,5 then m would be 2 (not 4). 1384 The values in idxm would be 1 2; that is the first index for each block divided by 1385 the block size. 1386 1387 Note that you must call MatSetBlockSize() when constructing this matrix (after 1388 preallocating it). 1389 1390 By default the values, v, are row-oriented, so the layout of 1391 v is the same as for MatSetValues(). See MatSetOption() for other options. 1392 1393 Calls to MatSetValuesBlocked() with the INSERT_VALUES and ADD_VALUES 1394 options cannot be mixed without intervening calls to the assembly 1395 routines. 1396 1397 MatSetValuesBlocked() uses 0-based row and column numbers in Fortran 1398 as well as in C. 1399 1400 Negative indices may be passed in idxm and idxn, these rows and columns are 1401 simply ignored. This allows easily inserting element stiffness matrices 1402 with homogeneous Dirchlet boundary conditions that you don't want represented 1403 in the matrix. 1404 1405 Each time an entry is set within a sparse matrix via MatSetValues(), 1406 internal searching must be done to determine where to place the the 1407 data in the matrix storage space. By instead inserting blocks of 1408 entries via MatSetValuesBlocked(), the overhead of matrix assembly is 1409 reduced. 1410 1411 Example: 1412 $ Suppose m=n=2 and block size(bs) = 2 The array is 1413 $ 1414 $ 1 2 | 3 4 1415 $ 5 6 | 7 8 1416 $ - - - | - - - 1417 $ 9 10 | 11 12 1418 $ 13 14 | 15 16 1419 $ 1420 $ v[] should be passed in like 1421 $ v[] = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16] 1422 $ 1423 $ If you are not using row oriented storage of v (that is you called MatSetOption(mat,MAT_ROW_ORIENTED,PETSC_FALSE)) then 1424 $ v[] = [1,5,9,13,2,6,10,14,3,7,11,15,4,8,12,16] 1425 1426 Level: intermediate 1427 1428 Concepts: matrices^putting entries in blocked 1429 1430 .seealso: MatSetBlockSize(), MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesBlockedLocal() 1431 @*/ 1432 PetscErrorCode PETSCMAT_DLLEXPORT MatSetValuesBlocked(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],const PetscScalar v[],InsertMode addv) 1433 { 1434 PetscErrorCode ierr; 1435 1436 PetscFunctionBegin; 1437 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 1438 PetscValidType(mat,1); 1439 if (!m || !n) PetscFunctionReturn(0); /* no values to insert */ 1440 PetscValidIntPointer(idxm,3); 1441 PetscValidIntPointer(idxn,5); 1442 PetscValidScalarPointer(v,6); 1443 ierr = MatPreallocated(mat);CHKERRQ(ierr); 1444 if (mat->insertmode == NOT_SET_VALUES) { 1445 mat->insertmode = addv; 1446 } 1447 #if defined(PETSC_USE_DEBUG) 1448 else if (mat->insertmode != addv) { 1449 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values"); 1450 } 1451 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1452 #endif 1453 1454 if (mat->assembled) { 1455 mat->was_assembled = PETSC_TRUE; 1456 mat->assembled = PETSC_FALSE; 1457 } 1458 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 1459 if (mat->ops->setvaluesblocked) { 1460 ierr = (*mat->ops->setvaluesblocked)(mat,m,idxm,n,idxn,v,addv);CHKERRQ(ierr); 1461 } else { 1462 PetscInt buf[4096],*ibufm=0,*ibufn=0; 1463 PetscInt i,j,*iidxm,*iidxn,bs=mat->rmap->bs; 1464 if ((m+n)*bs <= 4096) { 1465 iidxm = buf; iidxn = buf + m*bs; 1466 } else { 1467 ierr = PetscMalloc2(m*bs,PetscInt,&ibufm,n*bs,PetscInt,&ibufn);CHKERRQ(ierr); 1468 iidxm = ibufm; iidxn = ibufn; 1469 } 1470 for (i=0; i<m; i++) { 1471 for (j=0; j<bs; j++) { 1472 iidxm[i*bs+j] = bs*idxm[i] + j; 1473 } 1474 } 1475 for (i=0; i<n; i++) { 1476 for (j=0; j<bs; j++) { 1477 iidxn[i*bs+j] = bs*idxn[i] + j; 1478 } 1479 } 1480 ierr = MatSetValues(mat,bs*m,iidxm,bs*n,iidxn,v,addv);CHKERRQ(ierr); 1481 ierr = PetscFree2(ibufm,ibufn);CHKERRQ(ierr); 1482 } 1483 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 1484 PetscFunctionReturn(0); 1485 } 1486 1487 #undef __FUNCT__ 1488 #define __FUNCT__ "MatGetValues" 1489 /*@ 1490 MatGetValues - Gets a block of values from a matrix. 1491 1492 Not Collective; currently only returns a local block 1493 1494 Input Parameters: 1495 + mat - the matrix 1496 . v - a logically two-dimensional array for storing the values 1497 . m, idxm - the number of rows and their global indices 1498 - n, idxn - the number of columns and their global indices 1499 1500 Notes: 1501 The user must allocate space (m*n PetscScalars) for the values, v. 1502 The values, v, are then returned in a row-oriented format, 1503 analogous to that used by default in MatSetValues(). 1504 1505 MatGetValues() uses 0-based row and column numbers in 1506 Fortran as well as in C. 1507 1508 MatGetValues() requires that the matrix has been assembled 1509 with MatAssemblyBegin()/MatAssemblyEnd(). Thus, calls to 1510 MatSetValues() and MatGetValues() CANNOT be made in succession 1511 without intermediate matrix assembly. 1512 1513 Negative row or column indices will be ignored and those locations in v[] will be 1514 left unchanged. 1515 1516 Level: advanced 1517 1518 Concepts: matrices^accessing values 1519 1520 .seealso: MatGetRow(), MatGetSubMatrices(), MatSetValues() 1521 @*/ 1522 PetscErrorCode PETSCMAT_DLLEXPORT MatGetValues(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],PetscScalar v[]) 1523 { 1524 PetscErrorCode ierr; 1525 1526 PetscFunctionBegin; 1527 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 1528 PetscValidType(mat,1); 1529 PetscValidIntPointer(idxm,3); 1530 PetscValidIntPointer(idxn,5); 1531 PetscValidScalarPointer(v,6); 1532 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1533 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1534 if (!mat->ops->getvalues) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 1535 ierr = MatPreallocated(mat);CHKERRQ(ierr); 1536 1537 ierr = PetscLogEventBegin(MAT_GetValues,mat,0,0,0);CHKERRQ(ierr); 1538 ierr = (*mat->ops->getvalues)(mat,m,idxm,n,idxn,v);CHKERRQ(ierr); 1539 ierr = PetscLogEventEnd(MAT_GetValues,mat,0,0,0);CHKERRQ(ierr); 1540 PetscFunctionReturn(0); 1541 } 1542 1543 #undef __FUNCT__ 1544 #define __FUNCT__ "MatSetLocalToGlobalMapping" 1545 /*@ 1546 MatSetLocalToGlobalMapping - Sets a local-to-global numbering for use by 1547 the routine MatSetValuesLocal() to allow users to insert matrix entries 1548 using a local (per-processor) numbering. 1549 1550 Not Collective 1551 1552 Input Parameters: 1553 + x - the matrix 1554 - mapping - mapping created with ISLocalToGlobalMappingCreate() 1555 or ISLocalToGlobalMappingCreateIS() 1556 1557 Level: intermediate 1558 1559 Concepts: matrices^local to global mapping 1560 Concepts: local to global mapping^for matrices 1561 1562 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesLocal() 1563 @*/ 1564 PetscErrorCode PETSCMAT_DLLEXPORT MatSetLocalToGlobalMapping(Mat x,ISLocalToGlobalMapping mapping) 1565 { 1566 PetscErrorCode ierr; 1567 PetscFunctionBegin; 1568 PetscValidHeaderSpecific(x,MAT_COOKIE,1); 1569 PetscValidType(x,1); 1570 PetscValidHeaderSpecific(mapping,IS_LTOGM_COOKIE,2); 1571 if (x->mapping) { 1572 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Mapping already set for matrix"); 1573 } 1574 ierr = MatPreallocated(x);CHKERRQ(ierr); 1575 1576 if (x->ops->setlocaltoglobalmapping) { 1577 ierr = (*x->ops->setlocaltoglobalmapping)(x,mapping);CHKERRQ(ierr); 1578 } else { 1579 ierr = PetscObjectReference((PetscObject)mapping);CHKERRQ(ierr); 1580 if (x->mapping) { ierr = ISLocalToGlobalMappingDestroy(x->mapping);CHKERRQ(ierr); } 1581 x->mapping = mapping; 1582 } 1583 PetscFunctionReturn(0); 1584 } 1585 1586 #undef __FUNCT__ 1587 #define __FUNCT__ "MatSetLocalToGlobalMappingBlock" 1588 /*@ 1589 MatSetLocalToGlobalMappingBlock - Sets a local-to-global numbering for use 1590 by the routine MatSetValuesBlockedLocal() to allow users to insert matrix 1591 entries using a local (per-processor) numbering. 1592 1593 Not Collective 1594 1595 Input Parameters: 1596 + x - the matrix 1597 - mapping - mapping created with ISLocalToGlobalMappingCreate() or 1598 ISLocalToGlobalMappingCreateIS() 1599 1600 Level: intermediate 1601 1602 Concepts: matrices^local to global mapping blocked 1603 Concepts: local to global mapping^for matrices, blocked 1604 1605 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesBlockedLocal(), 1606 MatSetValuesBlocked(), MatSetValuesLocal() 1607 @*/ 1608 PetscErrorCode PETSCMAT_DLLEXPORT MatSetLocalToGlobalMappingBlock(Mat x,ISLocalToGlobalMapping mapping) 1609 { 1610 PetscErrorCode ierr; 1611 PetscFunctionBegin; 1612 PetscValidHeaderSpecific(x,MAT_COOKIE,1); 1613 PetscValidType(x,1); 1614 PetscValidHeaderSpecific(mapping,IS_LTOGM_COOKIE,2); 1615 if (x->bmapping) { 1616 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Mapping already set for matrix"); 1617 } 1618 ierr = PetscObjectReference((PetscObject)mapping);CHKERRQ(ierr); 1619 if (x->bmapping) { ierr = ISLocalToGlobalMappingDestroy(x->bmapping);CHKERRQ(ierr); } 1620 x->bmapping = mapping; 1621 PetscFunctionReturn(0); 1622 } 1623 1624 #undef __FUNCT__ 1625 #define __FUNCT__ "MatSetValuesLocal" 1626 /*@ 1627 MatSetValuesLocal - Inserts or adds values into certain locations of a matrix, 1628 using a local ordering of the nodes. 1629 1630 Not Collective 1631 1632 Input Parameters: 1633 + x - the matrix 1634 . nrow, irow - number of rows and their local indices 1635 . ncol, icol - number of columns and their local indices 1636 . y - a logically two-dimensional array of values 1637 - addv - either INSERT_VALUES or ADD_VALUES, where 1638 ADD_VALUES adds values to any existing entries, and 1639 INSERT_VALUES replaces existing entries with new values 1640 1641 Notes: 1642 Before calling MatSetValuesLocal(), the user must first set the 1643 local-to-global mapping by calling MatSetLocalToGlobalMapping(). 1644 1645 Calls to MatSetValuesLocal() with the INSERT_VALUES and ADD_VALUES 1646 options cannot be mixed without intervening calls to the assembly 1647 routines. 1648 1649 These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd() 1650 MUST be called after all calls to MatSetValuesLocal() have been completed. 1651 1652 Level: intermediate 1653 1654 Concepts: matrices^putting entries in with local numbering 1655 1656 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetLocalToGlobalMapping(), 1657 MatSetValueLocal() 1658 @*/ 1659 PetscErrorCode PETSCMAT_DLLEXPORT MatSetValuesLocal(Mat mat,PetscInt nrow,const PetscInt irow[],PetscInt ncol,const PetscInt icol[],const PetscScalar y[],InsertMode addv) 1660 { 1661 PetscErrorCode ierr; 1662 PetscInt irowm[2048],icolm[2048]; 1663 1664 PetscFunctionBegin; 1665 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 1666 PetscValidType(mat,1); 1667 if (!nrow || !ncol) PetscFunctionReturn(0); /* no values to insert */ 1668 PetscValidIntPointer(irow,3); 1669 PetscValidIntPointer(icol,5); 1670 PetscValidScalarPointer(y,6); 1671 ierr = MatPreallocated(mat);CHKERRQ(ierr); 1672 if (mat->insertmode == NOT_SET_VALUES) { 1673 mat->insertmode = addv; 1674 } 1675 #if defined(PETSC_USE_DEBUG) 1676 else if (mat->insertmode != addv) { 1677 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values"); 1678 } 1679 if (!mat->ops->setvalueslocal && (nrow > 2048 || ncol > 2048)) { 1680 SETERRQ2(PETSC_ERR_SUP,"Number column/row indices must be <= 2048: are %D %D",nrow,ncol); 1681 } 1682 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1683 #endif 1684 1685 if (mat->assembled) { 1686 mat->was_assembled = PETSC_TRUE; 1687 mat->assembled = PETSC_FALSE; 1688 } 1689 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 1690 if (!mat->ops->setvalueslocal) { 1691 ierr = ISLocalToGlobalMappingApply(mat->mapping,nrow,irow,irowm);CHKERRQ(ierr); 1692 ierr = ISLocalToGlobalMappingApply(mat->mapping,ncol,icol,icolm);CHKERRQ(ierr); 1693 ierr = (*mat->ops->setvalues)(mat,nrow,irowm,ncol,icolm,y,addv);CHKERRQ(ierr); 1694 } else { 1695 ierr = (*mat->ops->setvalueslocal)(mat,nrow,irow,ncol,icol,y,addv);CHKERRQ(ierr); 1696 } 1697 mat->same_nonzero = PETSC_FALSE; 1698 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 1699 PetscFunctionReturn(0); 1700 } 1701 1702 #undef __FUNCT__ 1703 #define __FUNCT__ "MatSetValuesBlockedLocal" 1704 /*@ 1705 MatSetValuesBlockedLocal - Inserts or adds values into certain locations of a matrix, 1706 using a local ordering of the nodes a block at a time. 1707 1708 Not Collective 1709 1710 Input Parameters: 1711 + x - the matrix 1712 . nrow, irow - number of rows and their local indices 1713 . ncol, icol - number of columns and their local indices 1714 . y - a logically two-dimensional array of values 1715 - addv - either INSERT_VALUES or ADD_VALUES, where 1716 ADD_VALUES adds values to any existing entries, and 1717 INSERT_VALUES replaces existing entries with new values 1718 1719 Notes: 1720 Before calling MatSetValuesBlockedLocal(), the user must first set the 1721 block size using MatSetBlockSize(), and the local-to-global mapping by 1722 calling MatSetLocalToGlobalMappingBlock(), where the mapping MUST be 1723 set for matrix blocks, not for matrix elements. 1724 1725 Calls to MatSetValuesBlockedLocal() with the INSERT_VALUES and ADD_VALUES 1726 options cannot be mixed without intervening calls to the assembly 1727 routines. 1728 1729 These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd() 1730 MUST be called after all calls to MatSetValuesBlockedLocal() have been completed. 1731 1732 Level: intermediate 1733 1734 Concepts: matrices^putting blocked values in with local numbering 1735 1736 .seealso: MatSetBlockSize(), MatSetLocalToGlobalMappingBlock(), MatAssemblyBegin(), MatAssemblyEnd(), 1737 MatSetValuesLocal(), MatSetLocalToGlobalMappingBlock(), MatSetValuesBlocked() 1738 @*/ 1739 PetscErrorCode PETSCMAT_DLLEXPORT MatSetValuesBlockedLocal(Mat mat,PetscInt nrow,const PetscInt irow[],PetscInt ncol,const PetscInt icol[],const PetscScalar y[],InsertMode addv) 1740 { 1741 PetscErrorCode ierr; 1742 PetscInt irowm[2048],icolm[2048]; 1743 1744 PetscFunctionBegin; 1745 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 1746 PetscValidType(mat,1); 1747 if (!nrow || !ncol) PetscFunctionReturn(0); /* no values to insert */ 1748 PetscValidIntPointer(irow,3); 1749 PetscValidIntPointer(icol,5); 1750 PetscValidScalarPointer(y,6); 1751 ierr = MatPreallocated(mat);CHKERRQ(ierr); 1752 if (mat->insertmode == NOT_SET_VALUES) { 1753 mat->insertmode = addv; 1754 } 1755 #if defined(PETSC_USE_DEBUG) 1756 else if (mat->insertmode != addv) { 1757 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values"); 1758 } 1759 if (!mat->bmapping) { 1760 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Local to global never set with MatSetLocalToGlobalMappingBlock()"); 1761 } 1762 if (nrow > 2048 || ncol > 2048) { 1763 SETERRQ2(PETSC_ERR_SUP,"Number column/row indices must be <= 2048: are %D %D",nrow,ncol); 1764 } 1765 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1766 #endif 1767 1768 if (mat->assembled) { 1769 mat->was_assembled = PETSC_TRUE; 1770 mat->assembled = PETSC_FALSE; 1771 } 1772 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 1773 ierr = ISLocalToGlobalMappingApply(mat->bmapping,nrow,irow,irowm);CHKERRQ(ierr); 1774 ierr = ISLocalToGlobalMappingApply(mat->bmapping,ncol,icol,icolm);CHKERRQ(ierr); 1775 if (mat->ops->setvaluesblocked) { 1776 ierr = (*mat->ops->setvaluesblocked)(mat,nrow,irowm,ncol,icolm,y,addv);CHKERRQ(ierr); 1777 } else { 1778 PetscInt buf[4096],*ibufm=0,*ibufn=0; 1779 PetscInt i,j,*iirowm,*iicolm,bs=mat->rmap->bs; 1780 if ((nrow+ncol)*bs <= 4096) { 1781 iirowm = buf; iicolm = buf + nrow*bs; 1782 } else { 1783 ierr = PetscMalloc2(nrow*bs,PetscInt,&ibufm,ncol*bs,PetscInt,&ibufn);CHKERRQ(ierr); 1784 iirowm = ibufm; iicolm = ibufn; 1785 } 1786 for (i=0; i<nrow; i++) { 1787 for (j=0; j<bs; j++) { 1788 iirowm[i*bs+j] = bs*irowm[i] + j; 1789 } 1790 } 1791 for (i=0; i<ncol; i++) { 1792 for (j=0; j<bs; j++) { 1793 iicolm[i*bs+j] = bs*icolm[i] + j; 1794 } 1795 } 1796 ierr = MatSetValues(mat,bs*nrow,iirowm,bs*ncol,iicolm,y,addv);CHKERRQ(ierr); 1797 ierr = PetscFree2(ibufm,ibufn);CHKERRQ(ierr); 1798 } 1799 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 1800 PetscFunctionReturn(0); 1801 } 1802 1803 #undef __FUNCT__ 1804 #define __FUNCT__ "MatMultDiagonalBlock" 1805 /*@ 1806 MatMultDiagonalBlock - Computes the matrix-vector product, y = Dx. Where D is defined by the inode or block structure of the diagonal 1807 1808 Collective on Mat and Vec 1809 1810 Input Parameters: 1811 + mat - the matrix 1812 - x - the vector to be multiplied 1813 1814 Output Parameters: 1815 . y - the result 1816 1817 Notes: 1818 The vectors x and y cannot be the same. I.e., one cannot 1819 call MatMult(A,y,y). 1820 1821 Level: developer 1822 1823 Concepts: matrix-vector product 1824 1825 .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd() 1826 @*/ 1827 PetscErrorCode PETSCMAT_DLLEXPORT MatMultDiagonalBlock(Mat mat,Vec x,Vec y) 1828 { 1829 PetscErrorCode ierr; 1830 1831 PetscFunctionBegin; 1832 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 1833 PetscValidType(mat,1); 1834 PetscValidHeaderSpecific(x,VEC_COOKIE,2); 1835 PetscValidHeaderSpecific(y,VEC_COOKIE,3); 1836 1837 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1838 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1839 if (x == y) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors"); 1840 ierr = MatPreallocated(mat);CHKERRQ(ierr); 1841 1842 if (!mat->ops->multdiagonalblock) SETERRQ(PETSC_ERR_SUP,"This matrix type does not have a multiply defined"); 1843 ierr = (*mat->ops->multdiagonalblock)(mat,x,y);CHKERRQ(ierr); 1844 ierr = PetscObjectStateIncrease((PetscObject)y);CHKERRQ(ierr); 1845 PetscFunctionReturn(0); 1846 } 1847 1848 /* --------------------------------------------------------*/ 1849 #undef __FUNCT__ 1850 #define __FUNCT__ "MatMult" 1851 /*@ 1852 MatMult - Computes the matrix-vector product, y = Ax. 1853 1854 Collective on Mat and Vec 1855 1856 Input Parameters: 1857 + mat - the matrix 1858 - x - the vector to be multiplied 1859 1860 Output Parameters: 1861 . y - the result 1862 1863 Notes: 1864 The vectors x and y cannot be the same. I.e., one cannot 1865 call MatMult(A,y,y). 1866 1867 Level: beginner 1868 1869 Concepts: matrix-vector product 1870 1871 .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd() 1872 @*/ 1873 PetscErrorCode PETSCMAT_DLLEXPORT MatMult(Mat mat,Vec x,Vec y) 1874 { 1875 PetscErrorCode ierr; 1876 1877 PetscFunctionBegin; 1878 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 1879 PetscValidType(mat,1); 1880 PetscValidHeaderSpecific(x,VEC_COOKIE,2); 1881 PetscValidHeaderSpecific(y,VEC_COOKIE,3); 1882 1883 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1884 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1885 if (x == y) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors"); 1886 #ifndef PETSC_HAVE_CONSTRAINTS 1887 if (mat->cmap->N != x->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->cmap->N,x->map->N); 1888 if (mat->rmap->N != y->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %D %D",mat->rmap->N,y->map->N); 1889 if (mat->rmap->n != y->map->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: local dim %D %D",mat->rmap->n,y->map->n); 1890 #endif 1891 ierr = MatPreallocated(mat);CHKERRQ(ierr); 1892 1893 if (mat->nullsp) { 1894 ierr = MatNullSpaceRemove(mat->nullsp,x,&x);CHKERRQ(ierr); 1895 } 1896 1897 if (!mat->ops->mult) SETERRQ(PETSC_ERR_SUP,"This matrix type does not have a multiply defined"); 1898 ierr = PetscLogEventBegin(MAT_Mult,mat,x,y,0);CHKERRQ(ierr); 1899 ierr = (*mat->ops->mult)(mat,x,y);CHKERRQ(ierr); 1900 ierr = PetscLogEventEnd(MAT_Mult,mat,x,y,0);CHKERRQ(ierr); 1901 1902 if (mat->nullsp) { 1903 ierr = MatNullSpaceRemove(mat->nullsp,y,PETSC_NULL);CHKERRQ(ierr); 1904 } 1905 ierr = PetscObjectStateIncrease((PetscObject)y);CHKERRQ(ierr); 1906 PetscFunctionReturn(0); 1907 } 1908 1909 #undef __FUNCT__ 1910 #define __FUNCT__ "MatMultTranspose" 1911 /*@ 1912 MatMultTranspose - Computes matrix transpose times a vector. 1913 1914 Collective on Mat and Vec 1915 1916 Input Parameters: 1917 + mat - the matrix 1918 - x - the vector to be multilplied 1919 1920 Output Parameters: 1921 . y - the result 1922 1923 Notes: 1924 The vectors x and y cannot be the same. I.e., one cannot 1925 call MatMultTranspose(A,y,y). 1926 1927 Level: beginner 1928 1929 Concepts: matrix vector product^transpose 1930 1931 .seealso: MatMult(), MatMultAdd(), MatMultTransposeAdd() 1932 @*/ 1933 PetscErrorCode PETSCMAT_DLLEXPORT MatMultTranspose(Mat mat,Vec x,Vec y) 1934 { 1935 PetscErrorCode ierr; 1936 1937 PetscFunctionBegin; 1938 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 1939 PetscValidType(mat,1); 1940 PetscValidHeaderSpecific(x,VEC_COOKIE,2); 1941 PetscValidHeaderSpecific(y,VEC_COOKIE,3); 1942 1943 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1944 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1945 if (x == y) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors"); 1946 #ifndef PETSC_HAVE_CONSTRAINTS 1947 if (mat->rmap->N != x->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->rmap->N,x->map->N); 1948 if (mat->cmap->N != y->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %D %D",mat->cmap->N,y->map->N); 1949 #endif 1950 ierr = MatPreallocated(mat);CHKERRQ(ierr); 1951 1952 if (!mat->ops->multtranspose) SETERRQ(PETSC_ERR_SUP,"This matrix type does not have a multiply tranpose defined"); 1953 ierr = PetscLogEventBegin(MAT_MultTranspose,mat,x,y,0);CHKERRQ(ierr); 1954 ierr = (*mat->ops->multtranspose)(mat,x,y);CHKERRQ(ierr); 1955 ierr = PetscLogEventEnd(MAT_MultTranspose,mat,x,y,0);CHKERRQ(ierr); 1956 ierr = PetscObjectStateIncrease((PetscObject)y);CHKERRQ(ierr); 1957 PetscFunctionReturn(0); 1958 } 1959 1960 #undef __FUNCT__ 1961 #define __FUNCT__ "MatMultHermitianTranspose" 1962 /*@ 1963 MatMultHermitianTranspose - Computes matrix Hermitian transpose times a vector. 1964 1965 Collective on Mat and Vec 1966 1967 Input Parameters: 1968 + mat - the matrix 1969 - x - the vector to be multilplied 1970 1971 Output Parameters: 1972 . y - the result 1973 1974 Notes: 1975 The vectors x and y cannot be the same. I.e., one cannot 1976 call MatMultHermitianTranspose(A,y,y). 1977 1978 Level: beginner 1979 1980 Concepts: matrix vector product^transpose 1981 1982 .seealso: MatMult(), MatMultAdd(), MatMultHermitianTransposeAdd(), MatMultTranspose() 1983 @*/ 1984 PetscErrorCode PETSCMAT_DLLEXPORT MatMultHermitianTranspose(Mat mat,Vec x,Vec y) 1985 { 1986 PetscErrorCode ierr; 1987 1988 PetscFunctionBegin; 1989 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 1990 PetscValidType(mat,1); 1991 PetscValidHeaderSpecific(x,VEC_COOKIE,2); 1992 PetscValidHeaderSpecific(y,VEC_COOKIE,3); 1993 1994 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1995 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1996 if (x == y) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors"); 1997 #ifndef PETSC_HAVE_CONSTRAINTS 1998 if (mat->rmap->N != x->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->rmap->N,x->map->N); 1999 if (mat->cmap->N != y->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %D %D",mat->cmap->N,y->map->N); 2000 #endif 2001 ierr = MatPreallocated(mat);CHKERRQ(ierr); 2002 2003 if (!mat->ops->multtranspose) SETERRQ(PETSC_ERR_SUP,"This matrix type does not have a multiply tranpose defined"); 2004 ierr = PetscLogEventBegin(MAT_MultHermitianTranspose,mat,x,y,0);CHKERRQ(ierr); 2005 ierr = (*mat->ops->multhermitiantranspose)(mat,x,y);CHKERRQ(ierr); 2006 ierr = PetscLogEventEnd(MAT_MultHermitianTranspose,mat,x,y,0);CHKERRQ(ierr); 2007 ierr = PetscObjectStateIncrease((PetscObject)y);CHKERRQ(ierr); 2008 PetscFunctionReturn(0); 2009 } 2010 2011 #undef __FUNCT__ 2012 #define __FUNCT__ "MatMultAdd" 2013 /*@ 2014 MatMultAdd - Computes v3 = v2 + A * v1. 2015 2016 Collective on Mat and Vec 2017 2018 Input Parameters: 2019 + mat - the matrix 2020 - v1, v2 - the vectors 2021 2022 Output Parameters: 2023 . v3 - the result 2024 2025 Notes: 2026 The vectors v1 and v3 cannot be the same. I.e., one cannot 2027 call MatMultAdd(A,v1,v2,v1). 2028 2029 Level: beginner 2030 2031 Concepts: matrix vector product^addition 2032 2033 .seealso: MatMultTranspose(), MatMult(), MatMultTransposeAdd() 2034 @*/ 2035 PetscErrorCode PETSCMAT_DLLEXPORT MatMultAdd(Mat mat,Vec v1,Vec v2,Vec v3) 2036 { 2037 PetscErrorCode ierr; 2038 2039 PetscFunctionBegin; 2040 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 2041 PetscValidType(mat,1); 2042 PetscValidHeaderSpecific(v1,VEC_COOKIE,2); 2043 PetscValidHeaderSpecific(v2,VEC_COOKIE,3); 2044 PetscValidHeaderSpecific(v3,VEC_COOKIE,4); 2045 2046 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2047 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2048 if (mat->cmap->N != v1->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v1: global dim %D %D",mat->cmap->N,v1->map->N); 2049 /* if (mat->rmap->N != v2->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v2: global dim %D %D",mat->rmap->N,v2->map->N); 2050 if (mat->rmap->N != v3->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v3: global dim %D %D",mat->rmap->N,v3->map->N); */ 2051 if (mat->rmap->n != v3->map->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v3: local dim %D %D",mat->rmap->n,v3->map->n); 2052 if (mat->rmap->n != v2->map->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v2: local dim %D %D",mat->rmap->n,v2->map->n); 2053 if (v1 == v3) SETERRQ(PETSC_ERR_ARG_IDN,"v1 and v3 must be different vectors"); 2054 ierr = MatPreallocated(mat);CHKERRQ(ierr); 2055 2056 ierr = PetscLogEventBegin(MAT_MultAdd,mat,v1,v2,v3);CHKERRQ(ierr); 2057 ierr = (*mat->ops->multadd)(mat,v1,v2,v3);CHKERRQ(ierr); 2058 ierr = PetscLogEventEnd(MAT_MultAdd,mat,v1,v2,v3);CHKERRQ(ierr); 2059 ierr = PetscObjectStateIncrease((PetscObject)v3);CHKERRQ(ierr); 2060 PetscFunctionReturn(0); 2061 } 2062 2063 #undef __FUNCT__ 2064 #define __FUNCT__ "MatMultTransposeAdd" 2065 /*@ 2066 MatMultTransposeAdd - Computes v3 = v2 + A' * v1. 2067 2068 Collective on Mat and Vec 2069 2070 Input Parameters: 2071 + mat - the matrix 2072 - v1, v2 - the vectors 2073 2074 Output Parameters: 2075 . v3 - the result 2076 2077 Notes: 2078 The vectors v1 and v3 cannot be the same. I.e., one cannot 2079 call MatMultTransposeAdd(A,v1,v2,v1). 2080 2081 Level: beginner 2082 2083 Concepts: matrix vector product^transpose and addition 2084 2085 .seealso: MatMultTranspose(), MatMultAdd(), MatMult() 2086 @*/ 2087 PetscErrorCode PETSCMAT_DLLEXPORT MatMultTransposeAdd(Mat mat,Vec v1,Vec v2,Vec v3) 2088 { 2089 PetscErrorCode ierr; 2090 2091 PetscFunctionBegin; 2092 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 2093 PetscValidType(mat,1); 2094 PetscValidHeaderSpecific(v1,VEC_COOKIE,2); 2095 PetscValidHeaderSpecific(v2,VEC_COOKIE,3); 2096 PetscValidHeaderSpecific(v3,VEC_COOKIE,4); 2097 2098 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2099 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2100 if (!mat->ops->multtransposeadd) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 2101 if (v1 == v3) SETERRQ(PETSC_ERR_ARG_IDN,"v1 and v3 must be different vectors"); 2102 if (mat->rmap->N != v1->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v1: global dim %D %D",mat->rmap->N,v1->map->N); 2103 if (mat->cmap->N != v2->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v2: global dim %D %D",mat->cmap->N,v2->map->N); 2104 if (mat->cmap->N != v3->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v3: global dim %D %D",mat->cmap->N,v3->map->N); 2105 ierr = MatPreallocated(mat);CHKERRQ(ierr); 2106 2107 ierr = PetscLogEventBegin(MAT_MultTransposeAdd,mat,v1,v2,v3);CHKERRQ(ierr); 2108 ierr = (*mat->ops->multtransposeadd)(mat,v1,v2,v3);CHKERRQ(ierr); 2109 ierr = PetscLogEventEnd(MAT_MultTransposeAdd,mat,v1,v2,v3);CHKERRQ(ierr); 2110 ierr = PetscObjectStateIncrease((PetscObject)v3);CHKERRQ(ierr); 2111 PetscFunctionReturn(0); 2112 } 2113 2114 #undef __FUNCT__ 2115 #define __FUNCT__ "MatMultHermitianTransposeAdd" 2116 /*@ 2117 MatMultHermitianTransposeAdd - Computes v3 = v2 + A^H * v1. 2118 2119 Collective on Mat and Vec 2120 2121 Input Parameters: 2122 + mat - the matrix 2123 - v1, v2 - the vectors 2124 2125 Output Parameters: 2126 . v3 - the result 2127 2128 Notes: 2129 The vectors v1 and v3 cannot be the same. I.e., one cannot 2130 call MatMultHermitianTransposeAdd(A,v1,v2,v1). 2131 2132 Level: beginner 2133 2134 Concepts: matrix vector product^transpose and addition 2135 2136 .seealso: MatMultHermitianTranspose(), MatMultTranspose(), MatMultAdd(), MatMult() 2137 @*/ 2138 PetscErrorCode PETSCMAT_DLLEXPORT MatMultHermitianTransposeAdd(Mat mat,Vec v1,Vec v2,Vec v3) 2139 { 2140 PetscErrorCode ierr; 2141 2142 PetscFunctionBegin; 2143 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 2144 PetscValidType(mat,1); 2145 PetscValidHeaderSpecific(v1,VEC_COOKIE,2); 2146 PetscValidHeaderSpecific(v2,VEC_COOKIE,3); 2147 PetscValidHeaderSpecific(v3,VEC_COOKIE,4); 2148 2149 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2150 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2151 if (!mat->ops->multtransposeadd) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 2152 if (v1 == v3) SETERRQ(PETSC_ERR_ARG_IDN,"v1 and v3 must be different vectors"); 2153 if (mat->rmap->N != v1->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v1: global dim %D %D",mat->rmap->N,v1->map->N); 2154 if (mat->cmap->N != v2->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v2: global dim %D %D",mat->cmap->N,v2->map->N); 2155 if (mat->cmap->N != v3->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v3: global dim %D %D",mat->cmap->N,v3->map->N); 2156 ierr = MatPreallocated(mat);CHKERRQ(ierr); 2157 2158 ierr = PetscLogEventBegin(MAT_MultHermitianTransposeAdd,mat,v1,v2,v3);CHKERRQ(ierr); 2159 ierr = (*mat->ops->multhermitiantransposeadd)(mat,v1,v2,v3);CHKERRQ(ierr); 2160 ierr = PetscLogEventEnd(MAT_MultHermitianTransposeAdd,mat,v1,v2,v3);CHKERRQ(ierr); 2161 ierr = PetscObjectStateIncrease((PetscObject)v3);CHKERRQ(ierr); 2162 PetscFunctionReturn(0); 2163 } 2164 2165 #undef __FUNCT__ 2166 #define __FUNCT__ "MatMultConstrained" 2167 /*@ 2168 MatMultConstrained - The inner multiplication routine for a 2169 constrained matrix P^T A P. 2170 2171 Collective on Mat and Vec 2172 2173 Input Parameters: 2174 + mat - the matrix 2175 - x - the vector to be multilplied 2176 2177 Output Parameters: 2178 . y - the result 2179 2180 Notes: 2181 The vectors x and y cannot be the same. I.e., one cannot 2182 call MatMult(A,y,y). 2183 2184 Level: beginner 2185 2186 .keywords: matrix, multiply, matrix-vector product, constraint 2187 .seealso: MatMult(), MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd() 2188 @*/ 2189 PetscErrorCode PETSCMAT_DLLEXPORT MatMultConstrained(Mat mat,Vec x,Vec y) 2190 { 2191 PetscErrorCode ierr; 2192 2193 PetscFunctionBegin; 2194 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 2195 PetscValidHeaderSpecific(x,VEC_COOKIE,2); 2196 PetscValidHeaderSpecific(y,VEC_COOKIE,3); 2197 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2198 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2199 if (x == y) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors"); 2200 if (mat->cmap->N != x->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->cmap->N,x->map->N); 2201 if (mat->rmap->N != y->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %D %D",mat->rmap->N,y->map->N); 2202 if (mat->rmap->n != y->map->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: local dim %D %D",mat->rmap->n,y->map->n); 2203 2204 ierr = PetscLogEventBegin(MAT_MultConstrained,mat,x,y,0);CHKERRQ(ierr); 2205 ierr = (*mat->ops->multconstrained)(mat,x,y);CHKERRQ(ierr); 2206 ierr = PetscLogEventEnd(MAT_MultConstrained,mat,x,y,0);CHKERRQ(ierr); 2207 ierr = PetscObjectStateIncrease((PetscObject)y);CHKERRQ(ierr); 2208 2209 PetscFunctionReturn(0); 2210 } 2211 2212 #undef __FUNCT__ 2213 #define __FUNCT__ "MatMultTransposeConstrained" 2214 /*@ 2215 MatMultTransposeConstrained - The inner multiplication routine for a 2216 constrained matrix P^T A^T P. 2217 2218 Collective on Mat and Vec 2219 2220 Input Parameters: 2221 + mat - the matrix 2222 - x - the vector to be multilplied 2223 2224 Output Parameters: 2225 . y - the result 2226 2227 Notes: 2228 The vectors x and y cannot be the same. I.e., one cannot 2229 call MatMult(A,y,y). 2230 2231 Level: beginner 2232 2233 .keywords: matrix, multiply, matrix-vector product, constraint 2234 .seealso: MatMult(), MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd() 2235 @*/ 2236 PetscErrorCode PETSCMAT_DLLEXPORT MatMultTransposeConstrained(Mat mat,Vec x,Vec y) 2237 { 2238 PetscErrorCode ierr; 2239 2240 PetscFunctionBegin; 2241 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 2242 PetscValidHeaderSpecific(x,VEC_COOKIE,2); 2243 PetscValidHeaderSpecific(y,VEC_COOKIE,3); 2244 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2245 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2246 if (x == y) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors"); 2247 if (mat->rmap->N != x->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->cmap->N,x->map->N); 2248 if (mat->cmap->N != y->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %D %D",mat->rmap->N,y->map->N); 2249 2250 ierr = PetscLogEventBegin(MAT_MultConstrained,mat,x,y,0);CHKERRQ(ierr); 2251 ierr = (*mat->ops->multtransposeconstrained)(mat,x,y);CHKERRQ(ierr); 2252 ierr = PetscLogEventEnd(MAT_MultConstrained,mat,x,y,0);CHKERRQ(ierr); 2253 ierr = PetscObjectStateIncrease((PetscObject)y);CHKERRQ(ierr); 2254 2255 PetscFunctionReturn(0); 2256 } 2257 2258 #undef __FUNCT__ 2259 #define __FUNCT__ "MatGetFactorType" 2260 /*@C 2261 MatGetFactorType - gets the type of factorization it is 2262 2263 Note Collective 2264 as the flag 2265 2266 Input Parameters: 2267 . mat - the matrix 2268 2269 Output Parameters: 2270 . t - the type, one of MAT_FACTOR_NONE, MAT_FACTOR_LU, MAT_FACTOR_CHOLESKY, MAT_FACTOR_ILU, MAT_FACTOR_ICC,MAT_FACTOR_ILUDT 2271 2272 Level: intermediate 2273 2274 .seealso: MatFactorType, MatGetFactor() 2275 @*/ 2276 PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactorType(Mat mat,MatFactorType *t) 2277 { 2278 PetscFunctionBegin; 2279 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 2280 PetscValidType(mat,1); 2281 *t = mat->factor; 2282 PetscFunctionReturn(0); 2283 } 2284 2285 /* ------------------------------------------------------------*/ 2286 #undef __FUNCT__ 2287 #define __FUNCT__ "MatGetInfo" 2288 /*@C 2289 MatGetInfo - Returns information about matrix storage (number of 2290 nonzeros, memory, etc.). 2291 2292 Collective on Mat if MAT_GLOBAL_MAX or MAT_GLOBAL_SUM is used 2293 as the flag 2294 2295 Input Parameters: 2296 . mat - the matrix 2297 2298 Output Parameters: 2299 + flag - flag indicating the type of parameters to be returned 2300 (MAT_LOCAL - local matrix, MAT_GLOBAL_MAX - maximum over all processors, 2301 MAT_GLOBAL_SUM - sum over all processors) 2302 - info - matrix information context 2303 2304 Notes: 2305 The MatInfo context contains a variety of matrix data, including 2306 number of nonzeros allocated and used, number of mallocs during 2307 matrix assembly, etc. Additional information for factored matrices 2308 is provided (such as the fill ratio, number of mallocs during 2309 factorization, etc.). Much of this info is printed to PETSC_STDOUT 2310 when using the runtime options 2311 $ -info -mat_view_info 2312 2313 Example for C/C++ Users: 2314 See the file ${PETSC_DIR}/include/petscmat.h for a complete list of 2315 data within the MatInfo context. For example, 2316 .vb 2317 MatInfo info; 2318 Mat A; 2319 double mal, nz_a, nz_u; 2320 2321 MatGetInfo(A,MAT_LOCAL,&info); 2322 mal = info.mallocs; 2323 nz_a = info.nz_allocated; 2324 .ve 2325 2326 Example for Fortran Users: 2327 Fortran users should declare info as a double precision 2328 array of dimension MAT_INFO_SIZE, and then extract the parameters 2329 of interest. See the file ${PETSC_DIR}/include/finclude/petscmat.h 2330 a complete list of parameter names. 2331 .vb 2332 double precision info(MAT_INFO_SIZE) 2333 double precision mal, nz_a 2334 Mat A 2335 integer ierr 2336 2337 call MatGetInfo(A,MAT_LOCAL,info,ierr) 2338 mal = info(MAT_INFO_MALLOCS) 2339 nz_a = info(MAT_INFO_NZ_ALLOCATED) 2340 .ve 2341 2342 Level: intermediate 2343 2344 Concepts: matrices^getting information on 2345 2346 Developer Note: fortran interface is not autogenerated as the f90 2347 interface defintion cannot be generated correctly [due to MatInfo] 2348 2349 @*/ 2350 PetscErrorCode PETSCMAT_DLLEXPORT MatGetInfo(Mat mat,MatInfoType flag,MatInfo *info) 2351 { 2352 PetscErrorCode ierr; 2353 2354 PetscFunctionBegin; 2355 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 2356 PetscValidType(mat,1); 2357 PetscValidPointer(info,3); 2358 if (!mat->ops->getinfo) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 2359 ierr = MatPreallocated(mat);CHKERRQ(ierr); 2360 ierr = (*mat->ops->getinfo)(mat,flag,info);CHKERRQ(ierr); 2361 PetscFunctionReturn(0); 2362 } 2363 2364 /* ----------------------------------------------------------*/ 2365 2366 #undef __FUNCT__ 2367 #define __FUNCT__ "MatLUFactor" 2368 /*@C 2369 MatLUFactor - Performs in-place LU factorization of matrix. 2370 2371 Collective on Mat 2372 2373 Input Parameters: 2374 + mat - the matrix 2375 . row - row permutation 2376 . col - column permutation 2377 - info - options for factorization, includes 2378 $ fill - expected fill as ratio of original fill. 2379 $ dtcol - pivot tolerance (0 no pivot, 1 full column pivoting) 2380 $ Run with the option -info to determine an optimal value to use 2381 2382 Notes: 2383 Most users should employ the simplified KSP interface for linear solvers 2384 instead of working directly with matrix algebra routines such as this. 2385 See, e.g., KSPCreate(). 2386 2387 This changes the state of the matrix to a factored matrix; it cannot be used 2388 for example with MatSetValues() unless one first calls MatSetUnfactored(). 2389 2390 Level: developer 2391 2392 Concepts: matrices^LU factorization 2393 2394 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor(), 2395 MatGetOrdering(), MatSetUnfactored(), MatFactorInfo 2396 2397 Developer Note: fortran interface is not autogenerated as the f90 2398 interface defintion cannot be generated correctly [due to MatFactorInfo] 2399 2400 @*/ 2401 PetscErrorCode PETSCMAT_DLLEXPORT MatLUFactor(Mat mat,IS row,IS col,const MatFactorInfo *info) 2402 { 2403 PetscErrorCode ierr; 2404 2405 PetscFunctionBegin; 2406 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 2407 if (row) PetscValidHeaderSpecific(row,IS_COOKIE,2); 2408 if (col) PetscValidHeaderSpecific(col,IS_COOKIE,3); 2409 PetscValidPointer(info,4); 2410 PetscValidType(mat,1); 2411 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2412 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2413 if (!mat->ops->lufactor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 2414 ierr = MatPreallocated(mat);CHKERRQ(ierr); 2415 2416 ierr = PetscLogEventBegin(MAT_LUFactor,mat,row,col,0);CHKERRQ(ierr); 2417 ierr = (*mat->ops->lufactor)(mat,row,col,info);CHKERRQ(ierr); 2418 ierr = PetscLogEventEnd(MAT_LUFactor,mat,row,col,0);CHKERRQ(ierr); 2419 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 2420 PetscFunctionReturn(0); 2421 } 2422 2423 #undef __FUNCT__ 2424 #define __FUNCT__ "MatILUFactor" 2425 /*@C 2426 MatILUFactor - Performs in-place ILU factorization of matrix. 2427 2428 Collective on Mat 2429 2430 Input Parameters: 2431 + mat - the matrix 2432 . row - row permutation 2433 . col - column permutation 2434 - info - structure containing 2435 $ levels - number of levels of fill. 2436 $ expected fill - as ratio of original fill. 2437 $ 1 or 0 - indicating force fill on diagonal (improves robustness for matrices 2438 missing diagonal entries) 2439 2440 Notes: 2441 Probably really in-place only when level of fill is zero, otherwise allocates 2442 new space to store factored matrix and deletes previous memory. 2443 2444 Most users should employ the simplified KSP interface for linear solvers 2445 instead of working directly with matrix algebra routines such as this. 2446 See, e.g., KSPCreate(). 2447 2448 Level: developer 2449 2450 Concepts: matrices^ILU factorization 2451 2452 .seealso: MatILUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor(), MatFactorInfo 2453 2454 Developer Note: fortran interface is not autogenerated as the f90 2455 interface defintion cannot be generated correctly [due to MatFactorInfo] 2456 2457 @*/ 2458 PetscErrorCode PETSCMAT_DLLEXPORT MatILUFactor(Mat mat,IS row,IS col,const MatFactorInfo *info) 2459 { 2460 PetscErrorCode ierr; 2461 2462 PetscFunctionBegin; 2463 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 2464 if (row) PetscValidHeaderSpecific(row,IS_COOKIE,2); 2465 if (col) PetscValidHeaderSpecific(col,IS_COOKIE,3); 2466 PetscValidPointer(info,4); 2467 PetscValidType(mat,1); 2468 if (mat->rmap->N != mat->cmap->N) SETERRQ(PETSC_ERR_ARG_WRONG,"matrix must be square"); 2469 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2470 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2471 if (!mat->ops->ilufactor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 2472 ierr = MatPreallocated(mat);CHKERRQ(ierr); 2473 2474 ierr = PetscLogEventBegin(MAT_ILUFactor,mat,row,col,0);CHKERRQ(ierr); 2475 ierr = (*mat->ops->ilufactor)(mat,row,col,info);CHKERRQ(ierr); 2476 ierr = PetscLogEventEnd(MAT_ILUFactor,mat,row,col,0);CHKERRQ(ierr); 2477 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 2478 PetscFunctionReturn(0); 2479 } 2480 2481 #undef __FUNCT__ 2482 #define __FUNCT__ "MatLUFactorSymbolic" 2483 /*@C 2484 MatLUFactorSymbolic - Performs symbolic LU factorization of matrix. 2485 Call this routine before calling MatLUFactorNumeric(). 2486 2487 Collective on Mat 2488 2489 Input Parameters: 2490 + fact - the factor matrix obtained with MatGetFactor() 2491 . mat - the matrix 2492 . row, col - row and column permutations 2493 - info - options for factorization, includes 2494 $ fill - expected fill as ratio of original fill. 2495 $ dtcol - pivot tolerance (0 no pivot, 1 full column pivoting) 2496 $ Run with the option -info to determine an optimal value to use 2497 2498 2499 Notes: 2500 See the users manual for additional information about 2501 choosing the fill factor for better efficiency. 2502 2503 Most users should employ the simplified KSP interface for linear solvers 2504 instead of working directly with matrix algebra routines such as this. 2505 See, e.g., KSPCreate(). 2506 2507 Level: developer 2508 2509 Concepts: matrices^LU symbolic factorization 2510 2511 .seealso: MatLUFactor(), MatLUFactorNumeric(), MatCholeskyFactor(), MatFactorInfo 2512 2513 Developer Note: fortran interface is not autogenerated as the f90 2514 interface defintion cannot be generated correctly [due to MatFactorInfo] 2515 2516 @*/ 2517 PetscErrorCode PETSCMAT_DLLEXPORT MatLUFactorSymbolic(Mat fact,Mat mat,IS row,IS col,const MatFactorInfo *info) 2518 { 2519 PetscErrorCode ierr; 2520 2521 PetscFunctionBegin; 2522 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 2523 if (row) PetscValidHeaderSpecific(row,IS_COOKIE,2); 2524 if (col) PetscValidHeaderSpecific(col,IS_COOKIE,3); 2525 PetscValidPointer(info,4); 2526 PetscValidType(mat,1); 2527 PetscValidPointer(fact,5); 2528 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2529 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2530 if (!(fact)->ops->lufactorsymbolic) SETERRQ1(PETSC_ERR_SUP,"Matrix type %s symbolic LU",((PetscObject)mat)->type_name); 2531 ierr = MatPreallocated(mat);CHKERRQ(ierr); 2532 2533 ierr = PetscLogEventBegin(MAT_LUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr); 2534 ierr = (fact->ops->lufactorsymbolic)(fact,mat,row,col,info);CHKERRQ(ierr); 2535 ierr = PetscLogEventEnd(MAT_LUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr); 2536 ierr = PetscObjectStateIncrease((PetscObject)fact);CHKERRQ(ierr); 2537 PetscFunctionReturn(0); 2538 } 2539 2540 #undef __FUNCT__ 2541 #define __FUNCT__ "MatLUFactorNumeric" 2542 /*@C 2543 MatLUFactorNumeric - Performs numeric LU factorization of a matrix. 2544 Call this routine after first calling MatLUFactorSymbolic(). 2545 2546 Collective on Mat 2547 2548 Input Parameters: 2549 + fact - the factor matrix obtained with MatGetFactor() 2550 . mat - the matrix 2551 - info - options for factorization 2552 2553 Notes: 2554 See MatLUFactor() for in-place factorization. See 2555 MatCholeskyFactorNumeric() for the symmetric, positive definite case. 2556 2557 Most users should employ the simplified KSP interface for linear solvers 2558 instead of working directly with matrix algebra routines such as this. 2559 See, e.g., KSPCreate(). 2560 2561 Level: developer 2562 2563 Concepts: matrices^LU numeric factorization 2564 2565 .seealso: MatLUFactorSymbolic(), MatLUFactor(), MatCholeskyFactor() 2566 2567 Developer Note: fortran interface is not autogenerated as the f90 2568 interface defintion cannot be generated correctly [due to MatFactorInfo] 2569 2570 @*/ 2571 PetscErrorCode PETSCMAT_DLLEXPORT MatLUFactorNumeric(Mat fact,Mat mat,const MatFactorInfo *info) 2572 { 2573 PetscErrorCode ierr; 2574 2575 PetscFunctionBegin; 2576 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 2577 PetscValidType(mat,1); 2578 PetscValidPointer(fact,2); 2579 PetscValidHeaderSpecific(fact,MAT_COOKIE,2); 2580 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2581 if (mat->rmap->N != (fact)->rmap->N || mat->cmap->N != (fact)->cmap->N) { 2582 SETERRQ4(PETSC_ERR_ARG_SIZ,"Mat mat,Mat fact: global dimensions are different %D should = %D %D should = %D",mat->rmap->N,(fact)->rmap->N,mat->cmap->N,(fact)->cmap->N); 2583 } 2584 if (!(fact)->ops->lufactornumeric) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 2585 ierr = MatPreallocated(mat);CHKERRQ(ierr); 2586 ierr = PetscLogEventBegin(MAT_LUFactorNumeric,mat,fact,0,0);CHKERRQ(ierr); 2587 ierr = (fact->ops->lufactornumeric)(fact,mat,info);CHKERRQ(ierr); 2588 ierr = PetscLogEventEnd(MAT_LUFactorNumeric,mat,fact,0,0);CHKERRQ(ierr); 2589 2590 ierr = MatView_Private(fact);CHKERRQ(ierr); 2591 ierr = PetscObjectStateIncrease((PetscObject)fact);CHKERRQ(ierr); 2592 PetscFunctionReturn(0); 2593 } 2594 2595 #undef __FUNCT__ 2596 #define __FUNCT__ "MatCholeskyFactor" 2597 /*@C 2598 MatCholeskyFactor - Performs in-place Cholesky factorization of a 2599 symmetric matrix. 2600 2601 Collective on Mat 2602 2603 Input Parameters: 2604 + mat - the matrix 2605 . perm - row and column permutations 2606 - f - expected fill as ratio of original fill 2607 2608 Notes: 2609 See MatLUFactor() for the nonsymmetric case. See also 2610 MatCholeskyFactorSymbolic(), and MatCholeskyFactorNumeric(). 2611 2612 Most users should employ the simplified KSP interface for linear solvers 2613 instead of working directly with matrix algebra routines such as this. 2614 See, e.g., KSPCreate(). 2615 2616 Level: developer 2617 2618 Concepts: matrices^Cholesky factorization 2619 2620 .seealso: MatLUFactor(), MatCholeskyFactorSymbolic(), MatCholeskyFactorNumeric() 2621 MatGetOrdering() 2622 2623 Developer Note: fortran interface is not autogenerated as the f90 2624 interface defintion cannot be generated correctly [due to MatFactorInfo] 2625 2626 @*/ 2627 PetscErrorCode PETSCMAT_DLLEXPORT MatCholeskyFactor(Mat mat,IS perm,const MatFactorInfo *info) 2628 { 2629 PetscErrorCode ierr; 2630 2631 PetscFunctionBegin; 2632 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 2633 PetscValidType(mat,1); 2634 PetscValidHeaderSpecific(perm,IS_COOKIE,2); 2635 PetscValidPointer(info,3); 2636 if (mat->rmap->N != mat->cmap->N) SETERRQ(PETSC_ERR_ARG_WRONG,"Matrix must be square"); 2637 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2638 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2639 if (!mat->ops->choleskyfactor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 2640 ierr = MatPreallocated(mat);CHKERRQ(ierr); 2641 2642 ierr = PetscLogEventBegin(MAT_CholeskyFactor,mat,perm,0,0);CHKERRQ(ierr); 2643 ierr = (*mat->ops->choleskyfactor)(mat,perm,info);CHKERRQ(ierr); 2644 ierr = PetscLogEventEnd(MAT_CholeskyFactor,mat,perm,0,0);CHKERRQ(ierr); 2645 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 2646 PetscFunctionReturn(0); 2647 } 2648 2649 #undef __FUNCT__ 2650 #define __FUNCT__ "MatCholeskyFactorSymbolic" 2651 /*@C 2652 MatCholeskyFactorSymbolic - Performs symbolic Cholesky factorization 2653 of a symmetric matrix. 2654 2655 Collective on Mat 2656 2657 Input Parameters: 2658 + fact - the factor matrix obtained with MatGetFactor() 2659 . mat - the matrix 2660 . perm - row and column permutations 2661 - info - options for factorization, includes 2662 $ fill - expected fill as ratio of original fill. 2663 $ dtcol - pivot tolerance (0 no pivot, 1 full column pivoting) 2664 $ Run with the option -info to determine an optimal value to use 2665 2666 Notes: 2667 See MatLUFactorSymbolic() for the nonsymmetric case. See also 2668 MatCholeskyFactor() and MatCholeskyFactorNumeric(). 2669 2670 Most users should employ the simplified KSP interface for linear solvers 2671 instead of working directly with matrix algebra routines such as this. 2672 See, e.g., KSPCreate(). 2673 2674 Level: developer 2675 2676 Concepts: matrices^Cholesky symbolic factorization 2677 2678 .seealso: MatLUFactorSymbolic(), MatCholeskyFactor(), MatCholeskyFactorNumeric() 2679 MatGetOrdering() 2680 2681 Developer Note: fortran interface is not autogenerated as the f90 2682 interface defintion cannot be generated correctly [due to MatFactorInfo] 2683 2684 @*/ 2685 PetscErrorCode PETSCMAT_DLLEXPORT MatCholeskyFactorSymbolic(Mat fact,Mat mat,IS perm,const MatFactorInfo *info) 2686 { 2687 PetscErrorCode ierr; 2688 2689 PetscFunctionBegin; 2690 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 2691 PetscValidType(mat,1); 2692 if (perm) PetscValidHeaderSpecific(perm,IS_COOKIE,2); 2693 PetscValidPointer(info,3); 2694 PetscValidPointer(fact,4); 2695 if (mat->rmap->N != mat->cmap->N) SETERRQ(PETSC_ERR_ARG_WRONG,"Matrix must be square"); 2696 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2697 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2698 if (!(fact)->ops->choleskyfactorsymbolic) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 2699 ierr = MatPreallocated(mat);CHKERRQ(ierr); 2700 2701 ierr = PetscLogEventBegin(MAT_CholeskyFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr); 2702 ierr = (fact->ops->choleskyfactorsymbolic)(fact,mat,perm,info);CHKERRQ(ierr); 2703 ierr = PetscLogEventEnd(MAT_CholeskyFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr); 2704 ierr = PetscObjectStateIncrease((PetscObject)fact);CHKERRQ(ierr); 2705 PetscFunctionReturn(0); 2706 } 2707 2708 #undef __FUNCT__ 2709 #define __FUNCT__ "MatCholeskyFactorNumeric" 2710 /*@C 2711 MatCholeskyFactorNumeric - Performs numeric Cholesky factorization 2712 of a symmetric matrix. Call this routine after first calling 2713 MatCholeskyFactorSymbolic(). 2714 2715 Collective on Mat 2716 2717 Input Parameters: 2718 + fact - the factor matrix obtained with MatGetFactor() 2719 . mat - the initial matrix 2720 . info - options for factorization 2721 - fact - the symbolic factor of mat 2722 2723 2724 Notes: 2725 Most users should employ the simplified KSP interface for linear solvers 2726 instead of working directly with matrix algebra routines such as this. 2727 See, e.g., KSPCreate(). 2728 2729 Level: developer 2730 2731 Concepts: matrices^Cholesky numeric factorization 2732 2733 .seealso: MatCholeskyFactorSymbolic(), MatCholeskyFactor(), MatLUFactorNumeric() 2734 2735 Developer Note: fortran interface is not autogenerated as the f90 2736 interface defintion cannot be generated correctly [due to MatFactorInfo] 2737 2738 @*/ 2739 PetscErrorCode PETSCMAT_DLLEXPORT MatCholeskyFactorNumeric(Mat fact,Mat mat,const MatFactorInfo *info) 2740 { 2741 PetscErrorCode ierr; 2742 2743 PetscFunctionBegin; 2744 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 2745 PetscValidType(mat,1); 2746 PetscValidPointer(fact,2); 2747 PetscValidHeaderSpecific(fact,MAT_COOKIE,2); 2748 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2749 if (!(fact)->ops->choleskyfactornumeric) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 2750 if (mat->rmap->N != (fact)->rmap->N || mat->cmap->N != (fact)->cmap->N) { 2751 SETERRQ4(PETSC_ERR_ARG_SIZ,"Mat mat,Mat fact: global dim %D should = %D %D should = %D",mat->rmap->N,(fact)->rmap->N,mat->cmap->N,(fact)->cmap->N); 2752 } 2753 ierr = MatPreallocated(mat);CHKERRQ(ierr); 2754 2755 ierr = PetscLogEventBegin(MAT_CholeskyFactorNumeric,mat,fact,0,0);CHKERRQ(ierr); 2756 ierr = (fact->ops->choleskyfactornumeric)(fact,mat,info);CHKERRQ(ierr); 2757 ierr = PetscLogEventEnd(MAT_CholeskyFactorNumeric,mat,fact,0,0);CHKERRQ(ierr); 2758 2759 ierr = MatView_Private(fact);CHKERRQ(ierr); 2760 ierr = PetscObjectStateIncrease((PetscObject)fact);CHKERRQ(ierr); 2761 PetscFunctionReturn(0); 2762 } 2763 2764 /* ----------------------------------------------------------------*/ 2765 #undef __FUNCT__ 2766 #define __FUNCT__ "MatSolve" 2767 /*@ 2768 MatSolve - Solves A x = b, given a factored matrix. 2769 2770 Collective on Mat and Vec 2771 2772 Input Parameters: 2773 + mat - the factored matrix 2774 - b - the right-hand-side vector 2775 2776 Output Parameter: 2777 . x - the result vector 2778 2779 Notes: 2780 The vectors b and x cannot be the same. I.e., one cannot 2781 call MatSolve(A,x,x). 2782 2783 Notes: 2784 Most users should employ the simplified KSP interface for linear solvers 2785 instead of working directly with matrix algebra routines such as this. 2786 See, e.g., KSPCreate(). 2787 2788 Level: developer 2789 2790 Concepts: matrices^triangular solves 2791 2792 .seealso: MatSolveAdd(), MatSolveTranspose(), MatSolveTransposeAdd() 2793 @*/ 2794 PetscErrorCode PETSCMAT_DLLEXPORT MatSolve(Mat mat,Vec b,Vec x) 2795 { 2796 PetscErrorCode ierr; 2797 2798 PetscFunctionBegin; 2799 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 2800 PetscValidType(mat,1); 2801 PetscValidHeaderSpecific(b,VEC_COOKIE,2); 2802 PetscValidHeaderSpecific(x,VEC_COOKIE,3); 2803 PetscCheckSameComm(mat,1,b,2); 2804 PetscCheckSameComm(mat,1,x,3); 2805 if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 2806 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 2807 if (mat->cmap->N != x->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->cmap->N,x->map->N); 2808 if (mat->rmap->N != b->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %D %D",mat->rmap->N,b->map->N); 2809 if (mat->rmap->n != b->map->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %D %D",mat->rmap->n,b->map->n); 2810 if (!mat->rmap->N && !mat->cmap->N) PetscFunctionReturn(0); 2811 if (!mat->ops->solve) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 2812 ierr = MatPreallocated(mat);CHKERRQ(ierr); 2813 2814 ierr = PetscLogEventBegin(MAT_Solve,mat,b,x,0);CHKERRQ(ierr); 2815 ierr = (*mat->ops->solve)(mat,b,x);CHKERRQ(ierr); 2816 ierr = PetscLogEventEnd(MAT_Solve,mat,b,x,0);CHKERRQ(ierr); 2817 ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr); 2818 PetscFunctionReturn(0); 2819 } 2820 2821 #undef __FUNCT__ 2822 #define __FUNCT__ "MatMatSolve_Basic" 2823 PetscErrorCode PETSCMAT_DLLEXPORT MatMatSolve_Basic(Mat A,Mat B,Mat X) 2824 { 2825 PetscErrorCode ierr; 2826 Vec b,x; 2827 PetscInt m,N,i; 2828 PetscScalar *bb,*xx; 2829 2830 PetscFunctionBegin; 2831 ierr = MatGetArray(B,&bb);CHKERRQ(ierr); 2832 ierr = MatGetArray(X,&xx);CHKERRQ(ierr); 2833 ierr = MatGetLocalSize(B,&m,PETSC_NULL);CHKERRQ(ierr); /* number local rows */ 2834 ierr = MatGetSize(B,PETSC_NULL,&N);CHKERRQ(ierr); /* total columns in dense matrix */ 2835 ierr = MatGetVecs(A,&x,&b);CHKERRQ(ierr); 2836 for (i=0; i<N; i++) { 2837 ierr = VecPlaceArray(b,bb + i*m);CHKERRQ(ierr); 2838 ierr = VecPlaceArray(x,xx + i*m);CHKERRQ(ierr); 2839 ierr = MatSolve(A,b,x);CHKERRQ(ierr); 2840 ierr = VecResetArray(x);CHKERRQ(ierr); 2841 ierr = VecResetArray(b);CHKERRQ(ierr); 2842 } 2843 ierr = VecDestroy(b);CHKERRQ(ierr); 2844 ierr = VecDestroy(x);CHKERRQ(ierr); 2845 ierr = MatRestoreArray(B,&bb);CHKERRQ(ierr); 2846 ierr = MatRestoreArray(X,&xx);CHKERRQ(ierr); 2847 PetscFunctionReturn(0); 2848 } 2849 2850 #undef __FUNCT__ 2851 #define __FUNCT__ "MatMatSolve" 2852 /*@ 2853 MatMatSolve - Solves A X = B, given a factored matrix. 2854 2855 Collective on Mat 2856 2857 Input Parameters: 2858 + mat - the factored matrix 2859 - B - the right-hand-side matrix (dense matrix) 2860 2861 Output Parameter: 2862 . X - the result matrix (dense matrix) 2863 2864 Notes: 2865 The matrices b and x cannot be the same. I.e., one cannot 2866 call MatMatSolve(A,x,x). 2867 2868 Notes: 2869 Most users should usually employ the simplified KSP interface for linear solvers 2870 instead of working directly with matrix algebra routines such as this. 2871 See, e.g., KSPCreate(). However KSP can only solve for one vector (column of X) 2872 at a time. 2873 2874 Level: developer 2875 2876 Concepts: matrices^triangular solves 2877 2878 .seealso: MatMatSolveAdd(), MatMatSolveTranspose(), MatMatSolveTransposeAdd(), MatLUFactor(), MatCholeskyFactor() 2879 @*/ 2880 PetscErrorCode PETSCMAT_DLLEXPORT MatMatSolve(Mat A,Mat B,Mat X) 2881 { 2882 PetscErrorCode ierr; 2883 2884 PetscFunctionBegin; 2885 PetscValidHeaderSpecific(A,MAT_COOKIE,1); 2886 PetscValidType(A,1); 2887 PetscValidHeaderSpecific(B,MAT_COOKIE,2); 2888 PetscValidHeaderSpecific(X,MAT_COOKIE,3); 2889 PetscCheckSameComm(A,1,B,2); 2890 PetscCheckSameComm(A,1,X,3); 2891 if (X == B) SETERRQ(PETSC_ERR_ARG_IDN,"X and B must be different matrices"); 2892 if (!A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 2893 if (A->cmap->N != X->rmap->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat A,Mat X: global dim %D %D",A->cmap->N,X->rmap->N); 2894 if (A->rmap->N != B->rmap->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat A,Mat B: global dim %D %D",A->rmap->N,B->rmap->N); 2895 if (A->rmap->n != B->rmap->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat A,Mat B: local dim %D %D",A->rmap->n,B->rmap->n); 2896 if (!A->rmap->N && !A->cmap->N) PetscFunctionReturn(0); 2897 ierr = MatPreallocated(A);CHKERRQ(ierr); 2898 2899 ierr = PetscLogEventBegin(MAT_MatSolve,A,B,X,0);CHKERRQ(ierr); 2900 if (!A->ops->matsolve) { 2901 ierr = PetscInfo1(A,"Mat type %s using basic MatMatSolve",((PetscObject)A)->type_name);CHKERRQ(ierr); 2902 ierr = MatMatSolve_Basic(A,B,X);CHKERRQ(ierr); 2903 } else { 2904 ierr = (*A->ops->matsolve)(A,B,X);CHKERRQ(ierr); 2905 } 2906 ierr = PetscLogEventEnd(MAT_MatSolve,A,B,X,0);CHKERRQ(ierr); 2907 ierr = PetscObjectStateIncrease((PetscObject)X);CHKERRQ(ierr); 2908 PetscFunctionReturn(0); 2909 } 2910 2911 2912 #undef __FUNCT__ 2913 #define __FUNCT__ "MatForwardSolve" 2914 /*@ 2915 MatForwardSolve - Solves L x = b, given a factored matrix, A = LU, or 2916 U^T*D^(1/2) x = b, given a factored symmetric matrix, A = U^T*D*U, 2917 2918 Collective on Mat and Vec 2919 2920 Input Parameters: 2921 + mat - the factored matrix 2922 - b - the right-hand-side vector 2923 2924 Output Parameter: 2925 . x - the result vector 2926 2927 Notes: 2928 MatSolve() should be used for most applications, as it performs 2929 a forward solve followed by a backward solve. 2930 2931 The vectors b and x cannot be the same, i.e., one cannot 2932 call MatForwardSolve(A,x,x). 2933 2934 For matrix in seqsbaij format with block size larger than 1, 2935 the diagonal blocks are not implemented as D = D^(1/2) * D^(1/2) yet. 2936 MatForwardSolve() solves U^T*D y = b, and 2937 MatBackwardSolve() solves U x = y. 2938 Thus they do not provide a symmetric preconditioner. 2939 2940 Most users should employ the simplified KSP interface for linear solvers 2941 instead of working directly with matrix algebra routines such as this. 2942 See, e.g., KSPCreate(). 2943 2944 Level: developer 2945 2946 Concepts: matrices^forward solves 2947 2948 .seealso: MatSolve(), MatBackwardSolve() 2949 @*/ 2950 PetscErrorCode PETSCMAT_DLLEXPORT MatForwardSolve(Mat mat,Vec b,Vec x) 2951 { 2952 PetscErrorCode ierr; 2953 2954 PetscFunctionBegin; 2955 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 2956 PetscValidType(mat,1); 2957 PetscValidHeaderSpecific(b,VEC_COOKIE,2); 2958 PetscValidHeaderSpecific(x,VEC_COOKIE,3); 2959 PetscCheckSameComm(mat,1,b,2); 2960 PetscCheckSameComm(mat,1,x,3); 2961 if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 2962 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 2963 if (!mat->ops->forwardsolve) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 2964 if (mat->cmap->N != x->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->cmap->N,x->map->N); 2965 if (mat->rmap->N != b->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %D %D",mat->rmap->N,b->map->N); 2966 if (mat->rmap->n != b->map->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %D %D",mat->rmap->n,b->map->n); 2967 ierr = MatPreallocated(mat);CHKERRQ(ierr); 2968 ierr = PetscLogEventBegin(MAT_ForwardSolve,mat,b,x,0);CHKERRQ(ierr); 2969 ierr = (*mat->ops->forwardsolve)(mat,b,x);CHKERRQ(ierr); 2970 ierr = PetscLogEventEnd(MAT_ForwardSolve,mat,b,x,0);CHKERRQ(ierr); 2971 ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr); 2972 PetscFunctionReturn(0); 2973 } 2974 2975 #undef __FUNCT__ 2976 #define __FUNCT__ "MatBackwardSolve" 2977 /*@ 2978 MatBackwardSolve - Solves U x = b, given a factored matrix, A = LU. 2979 D^(1/2) U x = b, given a factored symmetric matrix, A = U^T*D*U, 2980 2981 Collective on Mat and Vec 2982 2983 Input Parameters: 2984 + mat - the factored matrix 2985 - b - the right-hand-side vector 2986 2987 Output Parameter: 2988 . x - the result vector 2989 2990 Notes: 2991 MatSolve() should be used for most applications, as it performs 2992 a forward solve followed by a backward solve. 2993 2994 The vectors b and x cannot be the same. I.e., one cannot 2995 call MatBackwardSolve(A,x,x). 2996 2997 For matrix in seqsbaij format with block size larger than 1, 2998 the diagonal blocks are not implemented as D = D^(1/2) * D^(1/2) yet. 2999 MatForwardSolve() solves U^T*D y = b, and 3000 MatBackwardSolve() solves U x = y. 3001 Thus they do not provide a symmetric preconditioner. 3002 3003 Most users should employ the simplified KSP interface for linear solvers 3004 instead of working directly with matrix algebra routines such as this. 3005 See, e.g., KSPCreate(). 3006 3007 Level: developer 3008 3009 Concepts: matrices^backward solves 3010 3011 .seealso: MatSolve(), MatForwardSolve() 3012 @*/ 3013 PetscErrorCode PETSCMAT_DLLEXPORT MatBackwardSolve(Mat mat,Vec b,Vec x) 3014 { 3015 PetscErrorCode ierr; 3016 3017 PetscFunctionBegin; 3018 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 3019 PetscValidType(mat,1); 3020 PetscValidHeaderSpecific(b,VEC_COOKIE,2); 3021 PetscValidHeaderSpecific(x,VEC_COOKIE,3); 3022 PetscCheckSameComm(mat,1,b,2); 3023 PetscCheckSameComm(mat,1,x,3); 3024 if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 3025 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 3026 if (!mat->ops->backwardsolve) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 3027 if (mat->cmap->N != x->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->cmap->N,x->map->N); 3028 if (mat->rmap->N != b->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %D %D",mat->rmap->N,b->map->N); 3029 if (mat->rmap->n != b->map->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %D %D",mat->rmap->n,b->map->n); 3030 ierr = MatPreallocated(mat);CHKERRQ(ierr); 3031 3032 ierr = PetscLogEventBegin(MAT_BackwardSolve,mat,b,x,0);CHKERRQ(ierr); 3033 ierr = (*mat->ops->backwardsolve)(mat,b,x);CHKERRQ(ierr); 3034 ierr = PetscLogEventEnd(MAT_BackwardSolve,mat,b,x,0);CHKERRQ(ierr); 3035 ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr); 3036 PetscFunctionReturn(0); 3037 } 3038 3039 #undef __FUNCT__ 3040 #define __FUNCT__ "MatSolveAdd" 3041 /*@ 3042 MatSolveAdd - Computes x = y + inv(A)*b, given a factored matrix. 3043 3044 Collective on Mat and Vec 3045 3046 Input Parameters: 3047 + mat - the factored matrix 3048 . b - the right-hand-side vector 3049 - y - the vector to be added to 3050 3051 Output Parameter: 3052 . x - the result vector 3053 3054 Notes: 3055 The vectors b and x cannot be the same. I.e., one cannot 3056 call MatSolveAdd(A,x,y,x). 3057 3058 Most users should employ the simplified KSP interface for linear solvers 3059 instead of working directly with matrix algebra routines such as this. 3060 See, e.g., KSPCreate(). 3061 3062 Level: developer 3063 3064 Concepts: matrices^triangular solves 3065 3066 .seealso: MatSolve(), MatSolveTranspose(), MatSolveTransposeAdd() 3067 @*/ 3068 PetscErrorCode PETSCMAT_DLLEXPORT MatSolveAdd(Mat mat,Vec b,Vec y,Vec x) 3069 { 3070 PetscScalar one = 1.0; 3071 Vec tmp; 3072 PetscErrorCode ierr; 3073 3074 PetscFunctionBegin; 3075 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 3076 PetscValidType(mat,1); 3077 PetscValidHeaderSpecific(y,VEC_COOKIE,2); 3078 PetscValidHeaderSpecific(b,VEC_COOKIE,3); 3079 PetscValidHeaderSpecific(x,VEC_COOKIE,4); 3080 PetscCheckSameComm(mat,1,b,2); 3081 PetscCheckSameComm(mat,1,y,2); 3082 PetscCheckSameComm(mat,1,x,3); 3083 if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 3084 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 3085 if (mat->cmap->N != x->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->cmap->N,x->map->N); 3086 if (mat->rmap->N != b->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %D %D",mat->rmap->N,b->map->N); 3087 if (mat->rmap->N != y->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %D %D",mat->rmap->N,y->map->N); 3088 if (mat->rmap->n != b->map->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %D %D",mat->rmap->n,b->map->n); 3089 if (x->map->n != y->map->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Vec x,Vec y: local dim %D %D",x->map->n,y->map->n); 3090 ierr = MatPreallocated(mat);CHKERRQ(ierr); 3091 3092 ierr = PetscLogEventBegin(MAT_SolveAdd,mat,b,x,y);CHKERRQ(ierr); 3093 if (mat->ops->solveadd) { 3094 ierr = (*mat->ops->solveadd)(mat,b,y,x);CHKERRQ(ierr); 3095 } else { 3096 /* do the solve then the add manually */ 3097 if (x != y) { 3098 ierr = MatSolve(mat,b,x);CHKERRQ(ierr); 3099 ierr = VecAXPY(x,one,y);CHKERRQ(ierr); 3100 } else { 3101 ierr = VecDuplicate(x,&tmp);CHKERRQ(ierr); 3102 ierr = PetscLogObjectParent(mat,tmp);CHKERRQ(ierr); 3103 ierr = VecCopy(x,tmp);CHKERRQ(ierr); 3104 ierr = MatSolve(mat,b,x);CHKERRQ(ierr); 3105 ierr = VecAXPY(x,one,tmp);CHKERRQ(ierr); 3106 ierr = VecDestroy(tmp);CHKERRQ(ierr); 3107 } 3108 } 3109 ierr = PetscLogEventEnd(MAT_SolveAdd,mat,b,x,y);CHKERRQ(ierr); 3110 ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr); 3111 PetscFunctionReturn(0); 3112 } 3113 3114 #undef __FUNCT__ 3115 #define __FUNCT__ "MatSolveTranspose" 3116 /*@ 3117 MatSolveTranspose - Solves A' x = b, given a factored matrix. 3118 3119 Collective on Mat and Vec 3120 3121 Input Parameters: 3122 + mat - the factored matrix 3123 - b - the right-hand-side vector 3124 3125 Output Parameter: 3126 . x - the result vector 3127 3128 Notes: 3129 The vectors b and x cannot be the same. I.e., one cannot 3130 call MatSolveTranspose(A,x,x). 3131 3132 Most users should employ the simplified KSP interface for linear solvers 3133 instead of working directly with matrix algebra routines such as this. 3134 See, e.g., KSPCreate(). 3135 3136 Level: developer 3137 3138 Concepts: matrices^triangular solves 3139 3140 .seealso: MatSolve(), MatSolveAdd(), MatSolveTransposeAdd() 3141 @*/ 3142 PetscErrorCode PETSCMAT_DLLEXPORT MatSolveTranspose(Mat mat,Vec b,Vec x) 3143 { 3144 PetscErrorCode ierr; 3145 3146 PetscFunctionBegin; 3147 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 3148 PetscValidType(mat,1); 3149 PetscValidHeaderSpecific(b,VEC_COOKIE,2); 3150 PetscValidHeaderSpecific(x,VEC_COOKIE,3); 3151 PetscCheckSameComm(mat,1,b,2); 3152 PetscCheckSameComm(mat,1,x,3); 3153 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 3154 if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 3155 if (!mat->ops->solvetranspose) SETERRQ1(PETSC_ERR_SUP,"Matrix type %s",((PetscObject)mat)->type_name); 3156 if (mat->rmap->N != x->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->rmap->N,x->map->N); 3157 if (mat->cmap->N != b->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %D %D",mat->cmap->N,b->map->N); 3158 ierr = MatPreallocated(mat);CHKERRQ(ierr); 3159 ierr = PetscLogEventBegin(MAT_SolveTranspose,mat,b,x,0);CHKERRQ(ierr); 3160 ierr = (*mat->ops->solvetranspose)(mat,b,x);CHKERRQ(ierr); 3161 ierr = PetscLogEventEnd(MAT_SolveTranspose,mat,b,x,0);CHKERRQ(ierr); 3162 ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr); 3163 PetscFunctionReturn(0); 3164 } 3165 3166 #undef __FUNCT__ 3167 #define __FUNCT__ "MatSolveTransposeAdd" 3168 /*@ 3169 MatSolveTransposeAdd - Computes x = y + inv(Transpose(A)) b, given a 3170 factored matrix. 3171 3172 Collective on Mat and Vec 3173 3174 Input Parameters: 3175 + mat - the factored matrix 3176 . b - the right-hand-side vector 3177 - y - the vector to be added to 3178 3179 Output Parameter: 3180 . x - the result vector 3181 3182 Notes: 3183 The vectors b and x cannot be the same. I.e., one cannot 3184 call MatSolveTransposeAdd(A,x,y,x). 3185 3186 Most users should employ the simplified KSP interface for linear solvers 3187 instead of working directly with matrix algebra routines such as this. 3188 See, e.g., KSPCreate(). 3189 3190 Level: developer 3191 3192 Concepts: matrices^triangular solves 3193 3194 .seealso: MatSolve(), MatSolveAdd(), MatSolveTranspose() 3195 @*/ 3196 PetscErrorCode PETSCMAT_DLLEXPORT MatSolveTransposeAdd(Mat mat,Vec b,Vec y,Vec x) 3197 { 3198 PetscScalar one = 1.0; 3199 PetscErrorCode ierr; 3200 Vec tmp; 3201 3202 PetscFunctionBegin; 3203 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 3204 PetscValidType(mat,1); 3205 PetscValidHeaderSpecific(y,VEC_COOKIE,2); 3206 PetscValidHeaderSpecific(b,VEC_COOKIE,3); 3207 PetscValidHeaderSpecific(x,VEC_COOKIE,4); 3208 PetscCheckSameComm(mat,1,b,2); 3209 PetscCheckSameComm(mat,1,y,3); 3210 PetscCheckSameComm(mat,1,x,4); 3211 if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 3212 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 3213 if (mat->rmap->N != x->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->rmap->N,x->map->N); 3214 if (mat->cmap->N != b->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %D %D",mat->cmap->N,b->map->N); 3215 if (mat->cmap->N != y->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %D %D",mat->cmap->N,y->map->N); 3216 if (x->map->n != y->map->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Vec x,Vec y: local dim %D %D",x->map->n,y->map->n); 3217 ierr = MatPreallocated(mat);CHKERRQ(ierr); 3218 3219 ierr = PetscLogEventBegin(MAT_SolveTransposeAdd,mat,b,x,y);CHKERRQ(ierr); 3220 if (mat->ops->solvetransposeadd) { 3221 ierr = (*mat->ops->solvetransposeadd)(mat,b,y,x);CHKERRQ(ierr); 3222 } else { 3223 /* do the solve then the add manually */ 3224 if (x != y) { 3225 ierr = MatSolveTranspose(mat,b,x);CHKERRQ(ierr); 3226 ierr = VecAXPY(x,one,y);CHKERRQ(ierr); 3227 } else { 3228 ierr = VecDuplicate(x,&tmp);CHKERRQ(ierr); 3229 ierr = PetscLogObjectParent(mat,tmp);CHKERRQ(ierr); 3230 ierr = VecCopy(x,tmp);CHKERRQ(ierr); 3231 ierr = MatSolveTranspose(mat,b,x);CHKERRQ(ierr); 3232 ierr = VecAXPY(x,one,tmp);CHKERRQ(ierr); 3233 ierr = VecDestroy(tmp);CHKERRQ(ierr); 3234 } 3235 } 3236 ierr = PetscLogEventEnd(MAT_SolveTransposeAdd,mat,b,x,y);CHKERRQ(ierr); 3237 ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr); 3238 PetscFunctionReturn(0); 3239 } 3240 /* ----------------------------------------------------------------*/ 3241 3242 #undef __FUNCT__ 3243 #define __FUNCT__ "MatSOR" 3244 /*@ 3245 MatSOR - Computes relaxation (SOR, Gauss-Seidel) sweeps. 3246 3247 Collective on Mat and Vec 3248 3249 Input Parameters: 3250 + mat - the matrix 3251 . b - the right hand side 3252 . omega - the relaxation factor 3253 . flag - flag indicating the type of SOR (see below) 3254 . shift - diagonal shift 3255 . its - the number of iterations 3256 - lits - the number of local iterations 3257 3258 Output Parameters: 3259 . x - the solution (can contain an initial guess, use option SOR_ZERO_INITIAL_GUESS to indicate no guess) 3260 3261 SOR Flags: 3262 . SOR_FORWARD_SWEEP - forward SOR 3263 . SOR_BACKWARD_SWEEP - backward SOR 3264 . SOR_SYMMETRIC_SWEEP - SSOR (symmetric SOR) 3265 . SOR_LOCAL_FORWARD_SWEEP - local forward SOR 3266 . SOR_LOCAL_BACKWARD_SWEEP - local forward SOR 3267 . SOR_LOCAL_SYMMETRIC_SWEEP - local SSOR 3268 . SOR_APPLY_UPPER, SOR_APPLY_LOWER - applies 3269 upper/lower triangular part of matrix to 3270 vector (with omega) 3271 . SOR_ZERO_INITIAL_GUESS - zero initial guess 3272 3273 Notes: 3274 SOR_LOCAL_FORWARD_SWEEP, SOR_LOCAL_BACKWARD_SWEEP, and 3275 SOR_LOCAL_SYMMETRIC_SWEEP perform separate independent smoothings 3276 on each processor. 3277 3278 Application programmers will not generally use MatSOR() directly, 3279 but instead will employ the KSP/PC interface. 3280 3281 Notes: for BAIJ, SBAIJ, and AIJ matrices with Inodes this does a block SOR smoothing, otherwise it does a pointwise smoothing 3282 3283 Notes for Advanced Users: 3284 The flags are implemented as bitwise inclusive or operations. 3285 For example, use (SOR_ZERO_INITIAL_GUESS | SOR_SYMMETRIC_SWEEP) 3286 to specify a zero initial guess for SSOR. 3287 3288 Most users should employ the simplified KSP interface for linear solvers 3289 instead of working directly with matrix algebra routines such as this. 3290 See, e.g., KSPCreate(). 3291 3292 3293 Level: developer 3294 3295 Concepts: matrices^relaxation 3296 Concepts: matrices^SOR 3297 Concepts: matrices^Gauss-Seidel 3298 3299 @*/ 3300 PetscErrorCode PETSCMAT_DLLEXPORT MatSOR(Mat mat,Vec b,PetscReal omega,MatSORType flag,PetscReal shift,PetscInt its,PetscInt lits,Vec x) 3301 { 3302 PetscErrorCode ierr; 3303 3304 PetscFunctionBegin; 3305 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 3306 PetscValidType(mat,1); 3307 PetscValidHeaderSpecific(b,VEC_COOKIE,2); 3308 PetscValidHeaderSpecific(x,VEC_COOKIE,8); 3309 PetscCheckSameComm(mat,1,b,2); 3310 PetscCheckSameComm(mat,1,x,8); 3311 if (!mat->ops->sor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 3312 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3313 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3314 if (mat->cmap->N != x->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->cmap->N,x->map->N); 3315 if (mat->rmap->N != b->map->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %D %D",mat->rmap->N,b->map->N); 3316 if (mat->rmap->n != b->map->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %D %D",mat->rmap->n,b->map->n); 3317 if (its <= 0) SETERRQ1(PETSC_ERR_ARG_WRONG,"Relaxation requires global its %D positive",its); 3318 if (lits <= 0) SETERRQ1(PETSC_ERR_ARG_WRONG,"Relaxation requires local its %D positive",lits); 3319 3320 ierr = MatPreallocated(mat);CHKERRQ(ierr); 3321 ierr = PetscLogEventBegin(MAT_SOR,mat,b,x,0);CHKERRQ(ierr); 3322 ierr =(*mat->ops->sor)(mat,b,omega,flag,shift,its,lits,x);CHKERRQ(ierr); 3323 ierr = PetscLogEventEnd(MAT_SOR,mat,b,x,0);CHKERRQ(ierr); 3324 ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr); 3325 PetscFunctionReturn(0); 3326 } 3327 3328 #undef __FUNCT__ 3329 #define __FUNCT__ "MatCopy_Basic" 3330 /* 3331 Default matrix copy routine. 3332 */ 3333 PetscErrorCode MatCopy_Basic(Mat A,Mat B,MatStructure str) 3334 { 3335 PetscErrorCode ierr; 3336 PetscInt i,rstart = 0,rend = 0,nz; 3337 const PetscInt *cwork; 3338 const PetscScalar *vwork; 3339 3340 PetscFunctionBegin; 3341 if (B->assembled) { 3342 ierr = MatZeroEntries(B);CHKERRQ(ierr); 3343 } 3344 ierr = MatGetOwnershipRange(A,&rstart,&rend);CHKERRQ(ierr); 3345 for (i=rstart; i<rend; i++) { 3346 ierr = MatGetRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr); 3347 ierr = MatSetValues(B,1,&i,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr); 3348 ierr = MatRestoreRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr); 3349 } 3350 ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3351 ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3352 ierr = PetscObjectStateIncrease((PetscObject)B);CHKERRQ(ierr); 3353 PetscFunctionReturn(0); 3354 } 3355 3356 #undef __FUNCT__ 3357 #define __FUNCT__ "MatCopy" 3358 /*@ 3359 MatCopy - Copys a matrix to another matrix. 3360 3361 Collective on Mat 3362 3363 Input Parameters: 3364 + A - the matrix 3365 - str - SAME_NONZERO_PATTERN or DIFFERENT_NONZERO_PATTERN 3366 3367 Output Parameter: 3368 . B - where the copy is put 3369 3370 Notes: 3371 If you use SAME_NONZERO_PATTERN then the two matrices had better have the 3372 same nonzero pattern or the routine will crash. 3373 3374 MatCopy() copies the matrix entries of a matrix to another existing 3375 matrix (after first zeroing the second matrix). A related routine is 3376 MatConvert(), which first creates a new matrix and then copies the data. 3377 3378 Level: intermediate 3379 3380 Concepts: matrices^copying 3381 3382 .seealso: MatConvert(), MatDuplicate() 3383 3384 @*/ 3385 PetscErrorCode PETSCMAT_DLLEXPORT MatCopy(Mat A,Mat B,MatStructure str) 3386 { 3387 PetscErrorCode ierr; 3388 PetscInt i; 3389 3390 PetscFunctionBegin; 3391 PetscValidHeaderSpecific(A,MAT_COOKIE,1); 3392 PetscValidHeaderSpecific(B,MAT_COOKIE,2); 3393 PetscValidType(A,1); 3394 PetscValidType(B,2); 3395 PetscCheckSameComm(A,1,B,2); 3396 ierr = MatPreallocated(B);CHKERRQ(ierr); 3397 if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3398 if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3399 if (A->rmap->N != B->rmap->N || A->cmap->N != B->cmap->N) SETERRQ4(PETSC_ERR_ARG_SIZ,"Mat A,Mat B: global dim (%D,%D) (%D,%D)",A->rmap->N,B->rmap->N,A->cmap->N,B->cmap->N); 3400 ierr = MatPreallocated(A);CHKERRQ(ierr); 3401 3402 ierr = PetscLogEventBegin(MAT_Copy,A,B,0,0);CHKERRQ(ierr); 3403 if (A->ops->copy) { 3404 ierr = (*A->ops->copy)(A,B,str);CHKERRQ(ierr); 3405 } else { /* generic conversion */ 3406 ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr); 3407 } 3408 if (A->mapping) { 3409 if (B->mapping) {ierr = ISLocalToGlobalMappingDestroy(B->mapping);CHKERRQ(ierr);B->mapping = 0;} 3410 ierr = MatSetLocalToGlobalMapping(B,A->mapping);CHKERRQ(ierr); 3411 } 3412 if (A->bmapping) { 3413 if (B->bmapping) {ierr = ISLocalToGlobalMappingDestroy(B->bmapping);CHKERRQ(ierr);B->bmapping = 0;} 3414 ierr = MatSetLocalToGlobalMappingBlock(B,A->mapping);CHKERRQ(ierr); 3415 } 3416 3417 B->stencil.dim = A->stencil.dim; 3418 B->stencil.noc = A->stencil.noc; 3419 for (i=0; i<=A->stencil.dim; i++) { 3420 B->stencil.dims[i] = A->stencil.dims[i]; 3421 B->stencil.starts[i] = A->stencil.starts[i]; 3422 } 3423 3424 ierr = PetscLogEventEnd(MAT_Copy,A,B,0,0);CHKERRQ(ierr); 3425 ierr = PetscObjectStateIncrease((PetscObject)B);CHKERRQ(ierr); 3426 PetscFunctionReturn(0); 3427 } 3428 3429 #undef __FUNCT__ 3430 #define __FUNCT__ "MatConvert" 3431 /*@C 3432 MatConvert - Converts a matrix to another matrix, either of the same 3433 or different type. 3434 3435 Collective on Mat 3436 3437 Input Parameters: 3438 + mat - the matrix 3439 . newtype - new matrix type. Use MATSAME to create a new matrix of the 3440 same type as the original matrix. 3441 - reuse - denotes if the destination matrix is to be created or reused. Currently 3442 MAT_REUSE_MATRIX is only supported for inplace conversion, otherwise use 3443 MAT_INITIAL_MATRIX. 3444 3445 Output Parameter: 3446 . M - pointer to place new matrix 3447 3448 Notes: 3449 MatConvert() first creates a new matrix and then copies the data from 3450 the first matrix. A related routine is MatCopy(), which copies the matrix 3451 entries of one matrix to another already existing matrix context. 3452 3453 Cannot be used to convert a sequential matrix to parallel or parallel to sequential, 3454 the MPI communicator of the generated matrix is always the same as the communicator 3455 of the input matrix. 3456 3457 Level: intermediate 3458 3459 Concepts: matrices^converting between storage formats 3460 3461 .seealso: MatCopy(), MatDuplicate() 3462 @*/ 3463 PetscErrorCode PETSCMAT_DLLEXPORT MatConvert(Mat mat, const MatType newtype,MatReuse reuse,Mat *M) 3464 { 3465 PetscErrorCode ierr; 3466 PetscTruth sametype,issame,flg; 3467 char convname[256],mtype[256]; 3468 Mat B; 3469 3470 PetscFunctionBegin; 3471 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 3472 PetscValidType(mat,1); 3473 PetscValidPointer(M,3); 3474 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3475 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3476 ierr = MatPreallocated(mat);CHKERRQ(ierr); 3477 3478 ierr = PetscOptionsGetString(((PetscObject)mat)->prefix,"-matconvert_type",mtype,256,&flg);CHKERRQ(ierr); 3479 if (flg) { 3480 newtype = mtype; 3481 } 3482 ierr = PetscTypeCompare((PetscObject)mat,newtype,&sametype);CHKERRQ(ierr); 3483 ierr = PetscStrcmp(newtype,"same",&issame);CHKERRQ(ierr); 3484 if ((reuse == MAT_REUSE_MATRIX) && (mat != *M)) { 3485 SETERRQ(PETSC_ERR_SUP,"MAT_REUSE_MATRIX only supported for in-place conversion currently"); 3486 } 3487 3488 if ((reuse == MAT_REUSE_MATRIX) && (issame || sametype)) PetscFunctionReturn(0); 3489 3490 if ((sametype || issame) && (reuse==MAT_INITIAL_MATRIX) && mat->ops->duplicate) { 3491 ierr = (*mat->ops->duplicate)(mat,MAT_COPY_VALUES,M);CHKERRQ(ierr); 3492 } else { 3493 PetscErrorCode (*conv)(Mat, const MatType,MatReuse,Mat*)=PETSC_NULL; 3494 const char *prefix[3] = {"seq","mpi",""}; 3495 PetscInt i; 3496 /* 3497 Order of precedence: 3498 1) See if a specialized converter is known to the current matrix. 3499 2) See if a specialized converter is known to the desired matrix class. 3500 3) See if a good general converter is registered for the desired class 3501 (as of 6/27/03 only MATMPIADJ falls into this category). 3502 4) See if a good general converter is known for the current matrix. 3503 5) Use a really basic converter. 3504 */ 3505 3506 /* 1) See if a specialized converter is known to the current matrix and the desired class */ 3507 for (i=0; i<3; i++) { 3508 ierr = PetscStrcpy(convname,"MatConvert_");CHKERRQ(ierr); 3509 ierr = PetscStrcat(convname,((PetscObject)mat)->type_name);CHKERRQ(ierr); 3510 ierr = PetscStrcat(convname,"_");CHKERRQ(ierr); 3511 ierr = PetscStrcat(convname,prefix[i]);CHKERRQ(ierr); 3512 ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr); 3513 ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr); 3514 ierr = PetscObjectQueryFunction((PetscObject)mat,convname,(void (**)(void))&conv);CHKERRQ(ierr); 3515 if (conv) goto foundconv; 3516 } 3517 3518 /* 2) See if a specialized converter is known to the desired matrix class. */ 3519 ierr = MatCreate(((PetscObject)mat)->comm,&B);CHKERRQ(ierr); 3520 ierr = MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->N,mat->cmap->N);CHKERRQ(ierr); 3521 ierr = MatSetType(B,newtype);CHKERRQ(ierr); 3522 for (i=0; i<3; i++) { 3523 ierr = PetscStrcpy(convname,"MatConvert_");CHKERRQ(ierr); 3524 ierr = PetscStrcat(convname,((PetscObject)mat)->type_name);CHKERRQ(ierr); 3525 ierr = PetscStrcat(convname,"_");CHKERRQ(ierr); 3526 ierr = PetscStrcat(convname,prefix[i]);CHKERRQ(ierr); 3527 ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr); 3528 ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr); 3529 ierr = PetscObjectQueryFunction((PetscObject)B,convname,(void (**)(void))&conv);CHKERRQ(ierr); 3530 if (conv) { 3531 ierr = MatDestroy(B);CHKERRQ(ierr); 3532 goto foundconv; 3533 } 3534 } 3535 3536 /* 3) See if a good general converter is registered for the desired class */ 3537 conv = B->ops->convertfrom; 3538 ierr = MatDestroy(B);CHKERRQ(ierr); 3539 if (conv) goto foundconv; 3540 3541 /* 4) See if a good general converter is known for the current matrix */ 3542 if (mat->ops->convert) { 3543 conv = mat->ops->convert; 3544 } 3545 if (conv) goto foundconv; 3546 3547 /* 5) Use a really basic converter. */ 3548 conv = MatConvert_Basic; 3549 3550 foundconv: 3551 ierr = PetscLogEventBegin(MAT_Convert,mat,0,0,0);CHKERRQ(ierr); 3552 ierr = (*conv)(mat,newtype,reuse,M);CHKERRQ(ierr); 3553 ierr = PetscLogEventEnd(MAT_Convert,mat,0,0,0);CHKERRQ(ierr); 3554 } 3555 ierr = PetscObjectStateIncrease((PetscObject)*M);CHKERRQ(ierr); 3556 PetscFunctionReturn(0); 3557 } 3558 3559 #undef __FUNCT__ 3560 #define __FUNCT__ "MatFactorGetSolverPackage" 3561 /*@C 3562 MatFactorGetSolverPackage - Returns name of the package providing the factorization routines 3563 3564 Not Collective 3565 3566 Input Parameter: 3567 . mat - the matrix, must be a factored matrix 3568 3569 Output Parameter: 3570 . type - the string name of the package (do not free this string) 3571 3572 Notes: 3573 In Fortran you pass in a empty string and the package name will be copied into it. 3574 (Make sure the string is long enough) 3575 3576 Level: intermediate 3577 3578 .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable(), MatGetFactor() 3579 @*/ 3580 PetscErrorCode PETSCMAT_DLLEXPORT MatFactorGetSolverPackage(Mat mat, const MatSolverPackage *type) 3581 { 3582 PetscErrorCode ierr; 3583 PetscErrorCode (*conv)(Mat,const MatSolverPackage*); 3584 3585 PetscFunctionBegin; 3586 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 3587 PetscValidType(mat,1); 3588 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Only for factored matrix"); 3589 ierr = PetscObjectQueryFunction((PetscObject)mat,"MatFactorGetSolverPackage_C",(void (**)(void))&conv);CHKERRQ(ierr); 3590 if (!conv) { 3591 *type = MAT_SOLVER_PETSC; 3592 } else { 3593 ierr = (*conv)(mat,type);CHKERRQ(ierr); 3594 } 3595 PetscFunctionReturn(0); 3596 } 3597 3598 #undef __FUNCT__ 3599 #define __FUNCT__ "MatGetFactor" 3600 /*@C 3601 MatGetFactor - Returns a matrix suitable to calls to MatXXFactorSymbolic() 3602 3603 Collective on Mat 3604 3605 Input Parameters: 3606 + mat - the matrix 3607 . type - name of solver type, for example, spooles, superlu, plapack, petsc (to use PETSc's default) 3608 - ftype - factor type, MAT_FACTOR_LU, MAT_FACTOR_CHOLESKY, MAT_FACTOR_ICC, MAT_FACTOR_ILU, 3609 3610 Output Parameters: 3611 . f - the factor matrix used with MatXXFactorSymbolic() calls 3612 3613 Notes: 3614 Some PETSc matrix formats have alternative solvers available that are contained in alternative packages 3615 such as pastix, superlu, mumps, spooles etc. 3616 3617 PETSc must have been config/configure.py to use the external solver, using the option --download-package 3618 3619 Level: intermediate 3620 3621 .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable() 3622 @*/ 3623 PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor(Mat mat, const MatSolverPackage type,MatFactorType ftype,Mat *f) 3624 { 3625 PetscErrorCode ierr; 3626 char convname[256]; 3627 PetscErrorCode (*conv)(Mat,MatFactorType,Mat*); 3628 3629 PetscFunctionBegin; 3630 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 3631 PetscValidType(mat,1); 3632 3633 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3634 ierr = MatPreallocated(mat);CHKERRQ(ierr); 3635 3636 ierr = PetscStrcpy(convname,"MatGetFactor_");CHKERRQ(ierr); 3637 ierr = PetscStrcat(convname,type);CHKERRQ(ierr); 3638 ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr); 3639 ierr = PetscObjectQueryFunction((PetscObject)mat,convname,(void (**)(void))&conv);CHKERRQ(ierr); 3640 if (!conv) { 3641 PetscTruth flag; 3642 ierr = PetscStrcasecmp(MAT_SOLVER_PETSC,type,&flag);CHKERRQ(ierr); 3643 if (flag) { 3644 SETERRQ1(PETSC_ERR_SUP,"Matrix format %s does not have a built-in PETSc direct solver",((PetscObject)mat)->type_name); 3645 } else { 3646 SETERRQ3(PETSC_ERR_SUP,"Matrix format %s does not have a solver %s. Perhaps you must config/configure.py with --download-%s",((PetscObject)mat)->type_name,type,type); 3647 } 3648 } 3649 ierr = (*conv)(mat,ftype,f);CHKERRQ(ierr); 3650 PetscFunctionReturn(0); 3651 } 3652 3653 #undef __FUNCT__ 3654 #define __FUNCT__ "MatGetFactorAvailable" 3655 /*@C 3656 MatGetFactorAvailable - Returns a a flag if matrix supports particular package and factor type 3657 3658 Collective on Mat 3659 3660 Input Parameters: 3661 + mat - the matrix 3662 . type - name of solver type, for example, spooles, superlu, plapack, petsc (to use PETSc's default) 3663 - ftype - factor type, MAT_FACTOR_LU, MAT_FACTOR_CHOLESKY, MAT_FACTOR_ICC, MAT_FACTOR_ILU, 3664 3665 Output Parameter: 3666 . flg - PETSC_TRUE if the factorization is available 3667 3668 Notes: 3669 Some PETSc matrix formats have alternative solvers available that are contained in alternative packages 3670 such as pastix, superlu, mumps, spooles etc. 3671 3672 PETSc must have been config/configure.py to use the external solver, using the option --download-package 3673 3674 Level: intermediate 3675 3676 .seealso: MatCopy(), MatDuplicate(), MatGetFactor() 3677 @*/ 3678 PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactorAvailable(Mat mat, const MatSolverPackage type,MatFactorType ftype,PetscTruth *flg) 3679 { 3680 PetscErrorCode ierr; 3681 char convname[256]; 3682 PetscErrorCode (*conv)(Mat,MatFactorType,PetscTruth*); 3683 3684 PetscFunctionBegin; 3685 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 3686 PetscValidType(mat,1); 3687 3688 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3689 ierr = MatPreallocated(mat);CHKERRQ(ierr); 3690 3691 ierr = PetscStrcpy(convname,"MatGetFactorAvailable_");CHKERRQ(ierr); 3692 ierr = PetscStrcat(convname,type);CHKERRQ(ierr); 3693 ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr); 3694 ierr = PetscObjectQueryFunction((PetscObject)mat,convname,(void (**)(void))&conv);CHKERRQ(ierr); 3695 if (!conv) { 3696 *flg = PETSC_FALSE; 3697 } else { 3698 ierr = (*conv)(mat,ftype,flg);CHKERRQ(ierr); 3699 } 3700 PetscFunctionReturn(0); 3701 } 3702 3703 3704 #undef __FUNCT__ 3705 #define __FUNCT__ "MatDuplicate" 3706 /*@ 3707 MatDuplicate - Duplicates a matrix including the non-zero structure. 3708 3709 Collective on Mat 3710 3711 Input Parameters: 3712 + mat - the matrix 3713 - op - either MAT_DO_NOT_COPY_VALUES or MAT_COPY_VALUES, cause it to copy the numerical values in the matrix 3714 MAT_SHARE_NONZERO_PATTERN to share the nonzero patterns with the previous matrix and not copy them. 3715 3716 Output Parameter: 3717 . M - pointer to place new matrix 3718 3719 Level: intermediate 3720 3721 Concepts: matrices^duplicating 3722 3723 Notes: You cannot change the nonzero pattern for the parent or child matrix if you use MAT_SHARE_NONZERO_PATTERN. 3724 3725 .seealso: MatCopy(), MatConvert() 3726 @*/ 3727 PetscErrorCode PETSCMAT_DLLEXPORT MatDuplicate(Mat mat,MatDuplicateOption op,Mat *M) 3728 { 3729 PetscErrorCode ierr; 3730 Mat B; 3731 PetscInt i; 3732 3733 PetscFunctionBegin; 3734 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 3735 PetscValidType(mat,1); 3736 PetscValidPointer(M,3); 3737 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3738 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3739 ierr = MatPreallocated(mat);CHKERRQ(ierr); 3740 3741 *M = 0; 3742 if (!mat->ops->duplicate) { 3743 SETERRQ(PETSC_ERR_SUP,"Not written for this matrix type"); 3744 } 3745 ierr = PetscLogEventBegin(MAT_Convert,mat,0,0,0);CHKERRQ(ierr); 3746 ierr = (*mat->ops->duplicate)(mat,op,M);CHKERRQ(ierr); 3747 B = *M; 3748 if (mat->mapping) { 3749 ierr = MatSetLocalToGlobalMapping(B,mat->mapping);CHKERRQ(ierr); 3750 } 3751 if (mat->bmapping) { 3752 ierr = MatSetLocalToGlobalMappingBlock(B,mat->bmapping);CHKERRQ(ierr); 3753 } 3754 ierr = PetscLayoutCopy(mat->rmap,&B->rmap);CHKERRQ(ierr); 3755 ierr = PetscLayoutCopy(mat->cmap,&B->cmap);CHKERRQ(ierr); 3756 3757 B->stencil.dim = mat->stencil.dim; 3758 B->stencil.noc = mat->stencil.noc; 3759 for (i=0; i<=mat->stencil.dim; i++) { 3760 B->stencil.dims[i] = mat->stencil.dims[i]; 3761 B->stencil.starts[i] = mat->stencil.starts[i]; 3762 } 3763 3764 ierr = PetscLogEventEnd(MAT_Convert,mat,0,0,0);CHKERRQ(ierr); 3765 ierr = PetscObjectStateIncrease((PetscObject)B);CHKERRQ(ierr); 3766 PetscFunctionReturn(0); 3767 } 3768 3769 #undef __FUNCT__ 3770 #define __FUNCT__ "MatGetDiagonal" 3771 /*@ 3772 MatGetDiagonal - Gets the diagonal of a matrix. 3773 3774 Collective on Mat and Vec 3775 3776 Input Parameters: 3777 + mat - the matrix 3778 - v - the vector for storing the diagonal 3779 3780 Output Parameter: 3781 . v - the diagonal of the matrix 3782 3783 Level: intermediate 3784 3785 Concepts: matrices^accessing diagonals 3786 3787 .seealso: MatGetRow(), MatGetSubMatrices(), MatGetSubmatrix(), MatGetRowMaxAbs() 3788 @*/ 3789 PetscErrorCode PETSCMAT_DLLEXPORT MatGetDiagonal(Mat mat,Vec v) 3790 { 3791 PetscErrorCode ierr; 3792 3793 PetscFunctionBegin; 3794 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 3795 PetscValidType(mat,1); 3796 PetscValidHeaderSpecific(v,VEC_COOKIE,2); 3797 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3798 if (!mat->ops->getdiagonal) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 3799 ierr = MatPreallocated(mat);CHKERRQ(ierr); 3800 3801 ierr = (*mat->ops->getdiagonal)(mat,v);CHKERRQ(ierr); 3802 ierr = PetscObjectStateIncrease((PetscObject)v);CHKERRQ(ierr); 3803 PetscFunctionReturn(0); 3804 } 3805 3806 #undef __FUNCT__ 3807 #define __FUNCT__ "MatGetRowMin" 3808 /*@ 3809 MatGetRowMin - Gets the minimum value (of the real part) of each 3810 row of the matrix 3811 3812 Collective on Mat and Vec 3813 3814 Input Parameters: 3815 . mat - the matrix 3816 3817 Output Parameter: 3818 + v - the vector for storing the maximums 3819 - idx - the indices of the column found for each row (optional) 3820 3821 Level: intermediate 3822 3823 Notes: The result of this call are the same as if one converted the matrix to dense format 3824 and found the minimum value in each row (i.e. the implicit zeros are counted as zeros). 3825 3826 This code is only implemented for a couple of matrix formats. 3827 3828 Concepts: matrices^getting row maximums 3829 3830 .seealso: MatGetDiagonal(), MatGetSubMatrices(), MatGetSubmatrix(), MatGetRowMaxAbs(), 3831 MatGetRowMax() 3832 @*/ 3833 PetscErrorCode PETSCMAT_DLLEXPORT MatGetRowMin(Mat mat,Vec v,PetscInt idx[]) 3834 { 3835 PetscErrorCode ierr; 3836 3837 PetscFunctionBegin; 3838 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 3839 PetscValidType(mat,1); 3840 PetscValidHeaderSpecific(v,VEC_COOKIE,2); 3841 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3842 if (!mat->ops->getrowmax) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 3843 ierr = MatPreallocated(mat);CHKERRQ(ierr); 3844 3845 ierr = (*mat->ops->getrowmin)(mat,v,idx);CHKERRQ(ierr); 3846 ierr = PetscObjectStateIncrease((PetscObject)v);CHKERRQ(ierr); 3847 PetscFunctionReturn(0); 3848 } 3849 3850 #undef __FUNCT__ 3851 #define __FUNCT__ "MatGetRowMinAbs" 3852 /*@ 3853 MatGetRowMinAbs - Gets the minimum value (in absolute value) of each 3854 row of the matrix 3855 3856 Collective on Mat and Vec 3857 3858 Input Parameters: 3859 . mat - the matrix 3860 3861 Output Parameter: 3862 + v - the vector for storing the minimums 3863 - idx - the indices of the column found for each row (optional) 3864 3865 Level: intermediate 3866 3867 Notes: if a row is completely empty or has only 0.0 values then the idx[] value for that 3868 row is 0 (the first column). 3869 3870 This code is only implemented for a couple of matrix formats. 3871 3872 Concepts: matrices^getting row maximums 3873 3874 .seealso: MatGetDiagonal(), MatGetSubMatrices(), MatGetSubmatrix(), MatGetRowMax(), MatGetRowMaxAbs(), MatGetRowMin() 3875 @*/ 3876 PetscErrorCode PETSCMAT_DLLEXPORT MatGetRowMinAbs(Mat mat,Vec v,PetscInt idx[]) 3877 { 3878 PetscErrorCode ierr; 3879 3880 PetscFunctionBegin; 3881 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 3882 PetscValidType(mat,1); 3883 PetscValidHeaderSpecific(v,VEC_COOKIE,2); 3884 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3885 if (!mat->ops->getrowminabs) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 3886 ierr = MatPreallocated(mat);CHKERRQ(ierr); 3887 if (idx) {ierr = PetscMemzero(idx,mat->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);} 3888 3889 ierr = (*mat->ops->getrowminabs)(mat,v,idx);CHKERRQ(ierr); 3890 ierr = PetscObjectStateIncrease((PetscObject)v);CHKERRQ(ierr); 3891 PetscFunctionReturn(0); 3892 } 3893 3894 #undef __FUNCT__ 3895 #define __FUNCT__ "MatGetRowMax" 3896 /*@ 3897 MatGetRowMax - Gets the maximum value (of the real part) of each 3898 row of the matrix 3899 3900 Collective on Mat and Vec 3901 3902 Input Parameters: 3903 . mat - the matrix 3904 3905 Output Parameter: 3906 + v - the vector for storing the maximums 3907 - idx - the indices of the column found for each row (optional) 3908 3909 Level: intermediate 3910 3911 Notes: The result of this call are the same as if one converted the matrix to dense format 3912 and found the minimum value in each row (i.e. the implicit zeros are counted as zeros). 3913 3914 This code is only implemented for a couple of matrix formats. 3915 3916 Concepts: matrices^getting row maximums 3917 3918 .seealso: MatGetDiagonal(), MatGetSubMatrices(), MatGetSubmatrix(), MatGetRowMaxAbs(), MatGetRowMin() 3919 @*/ 3920 PetscErrorCode PETSCMAT_DLLEXPORT MatGetRowMax(Mat mat,Vec v,PetscInt idx[]) 3921 { 3922 PetscErrorCode ierr; 3923 3924 PetscFunctionBegin; 3925 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 3926 PetscValidType(mat,1); 3927 PetscValidHeaderSpecific(v,VEC_COOKIE,2); 3928 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3929 if (!mat->ops->getrowmax) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 3930 ierr = MatPreallocated(mat);CHKERRQ(ierr); 3931 3932 ierr = (*mat->ops->getrowmax)(mat,v,idx);CHKERRQ(ierr); 3933 ierr = PetscObjectStateIncrease((PetscObject)v);CHKERRQ(ierr); 3934 PetscFunctionReturn(0); 3935 } 3936 3937 #undef __FUNCT__ 3938 #define __FUNCT__ "MatGetRowMaxAbs" 3939 /*@ 3940 MatGetRowMaxAbs - Gets the maximum value (in absolute value) of each 3941 row of the matrix 3942 3943 Collective on Mat and Vec 3944 3945 Input Parameters: 3946 . mat - the matrix 3947 3948 Output Parameter: 3949 + v - the vector for storing the maximums 3950 - idx - the indices of the column found for each row (optional) 3951 3952 Level: intermediate 3953 3954 Notes: if a row is completely empty or has only 0.0 values then the idx[] value for that 3955 row is 0 (the first column). 3956 3957 This code is only implemented for a couple of matrix formats. 3958 3959 Concepts: matrices^getting row maximums 3960 3961 .seealso: MatGetDiagonal(), MatGetSubMatrices(), MatGetSubmatrix(), MatGetRowMax(), MatGetRowMin() 3962 @*/ 3963 PetscErrorCode PETSCMAT_DLLEXPORT MatGetRowMaxAbs(Mat mat,Vec v,PetscInt idx[]) 3964 { 3965 PetscErrorCode ierr; 3966 3967 PetscFunctionBegin; 3968 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 3969 PetscValidType(mat,1); 3970 PetscValidHeaderSpecific(v,VEC_COOKIE,2); 3971 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3972 if (!mat->ops->getrowmaxabs) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 3973 ierr = MatPreallocated(mat);CHKERRQ(ierr); 3974 if (idx) {ierr = PetscMemzero(idx,mat->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);} 3975 3976 ierr = (*mat->ops->getrowmaxabs)(mat,v,idx);CHKERRQ(ierr); 3977 ierr = PetscObjectStateIncrease((PetscObject)v);CHKERRQ(ierr); 3978 PetscFunctionReturn(0); 3979 } 3980 3981 #undef __FUNCT__ 3982 #define __FUNCT__ "MatGetRowSum" 3983 /*@ 3984 MatGetRowSum - Gets the sum of each row of the matrix 3985 3986 Collective on Mat and Vec 3987 3988 Input Parameters: 3989 . mat - the matrix 3990 3991 Output Parameter: 3992 . v - the vector for storing the sum of rows 3993 3994 Level: intermediate 3995 3996 Notes: This code is slow since it is not currently specialized for different formats 3997 3998 Concepts: matrices^getting row sums 3999 4000 .seealso: MatGetDiagonal(), MatGetSubMatrices(), MatGetSubmatrix(), MatGetRowMax(), MatGetRowMin() 4001 @*/ 4002 PetscErrorCode PETSCMAT_DLLEXPORT MatGetRowSum(Mat mat, Vec v) 4003 { 4004 PetscInt start = 0, end = 0, row; 4005 PetscScalar *array; 4006 PetscErrorCode ierr; 4007 4008 PetscFunctionBegin; 4009 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 4010 PetscValidType(mat,1); 4011 PetscValidHeaderSpecific(v,VEC_COOKIE,2); 4012 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4013 ierr = MatPreallocated(mat);CHKERRQ(ierr); 4014 ierr = MatGetOwnershipRange(mat, &start, &end);CHKERRQ(ierr); 4015 ierr = VecGetArray(v, &array);CHKERRQ(ierr); 4016 for(row = start; row < end; ++row) { 4017 PetscInt ncols, col; 4018 const PetscInt *cols; 4019 const PetscScalar *vals; 4020 4021 array[row - start] = 0.0; 4022 ierr = MatGetRow(mat, row, &ncols, &cols, &vals);CHKERRQ(ierr); 4023 for(col = 0; col < ncols; col++) { 4024 array[row - start] += vals[col]; 4025 } 4026 ierr = MatRestoreRow(mat, row, &ncols, &cols, &vals);CHKERRQ(ierr); 4027 } 4028 ierr = VecRestoreArray(v, &array);CHKERRQ(ierr); 4029 ierr = PetscObjectStateIncrease((PetscObject) v);CHKERRQ(ierr); 4030 PetscFunctionReturn(0); 4031 } 4032 4033 #undef __FUNCT__ 4034 #define __FUNCT__ "MatTranspose" 4035 /*@ 4036 MatTranspose - Computes an in-place or out-of-place transpose of a matrix. 4037 4038 Collective on Mat 4039 4040 Input Parameter: 4041 + mat - the matrix to transpose 4042 - reuse - store the transpose matrix in the provided B 4043 4044 Output Parameters: 4045 . B - the transpose 4046 4047 Notes: 4048 If you pass in &mat for B the transpose will be done in place 4049 4050 Level: intermediate 4051 4052 Concepts: matrices^transposing 4053 4054 .seealso: MatMultTranspose(), MatMultTransposeAdd(), MatIsTranspose(), MatReuse 4055 @*/ 4056 PetscErrorCode PETSCMAT_DLLEXPORT MatTranspose(Mat mat,MatReuse reuse,Mat *B) 4057 { 4058 PetscErrorCode ierr; 4059 4060 PetscFunctionBegin; 4061 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 4062 PetscValidType(mat,1); 4063 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4064 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4065 if (!mat->ops->transpose) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 4066 ierr = MatPreallocated(mat);CHKERRQ(ierr); 4067 4068 ierr = PetscLogEventBegin(MAT_Transpose,mat,0,0,0);CHKERRQ(ierr); 4069 ierr = (*mat->ops->transpose)(mat,reuse,B);CHKERRQ(ierr); 4070 ierr = PetscLogEventEnd(MAT_Transpose,mat,0,0,0);CHKERRQ(ierr); 4071 if (B) {ierr = PetscObjectStateIncrease((PetscObject)*B);CHKERRQ(ierr);} 4072 PetscFunctionReturn(0); 4073 } 4074 4075 #undef __FUNCT__ 4076 #define __FUNCT__ "MatIsTranspose" 4077 /*@ 4078 MatIsTranspose - Test whether a matrix is another one's transpose, 4079 or its own, in which case it tests symmetry. 4080 4081 Collective on Mat 4082 4083 Input Parameter: 4084 + A - the matrix to test 4085 - B - the matrix to test against, this can equal the first parameter 4086 4087 Output Parameters: 4088 . flg - the result 4089 4090 Notes: 4091 Only available for SeqAIJ/MPIAIJ matrices. The sequential algorithm 4092 has a running time of the order of the number of nonzeros; the parallel 4093 test involves parallel copies of the block-offdiagonal parts of the matrix. 4094 4095 Level: intermediate 4096 4097 Concepts: matrices^transposing, matrix^symmetry 4098 4099 .seealso: MatTranspose(), MatIsSymmetric(), MatIsHermitian() 4100 @*/ 4101 PetscErrorCode PETSCMAT_DLLEXPORT MatIsTranspose(Mat A,Mat B,PetscReal tol,PetscTruth *flg) 4102 { 4103 PetscErrorCode ierr,(*f)(Mat,Mat,PetscReal,PetscTruth*),(*g)(Mat,Mat,PetscReal,PetscTruth*); 4104 4105 PetscFunctionBegin; 4106 PetscValidHeaderSpecific(A,MAT_COOKIE,1); 4107 PetscValidHeaderSpecific(B,MAT_COOKIE,2); 4108 PetscValidPointer(flg,3); 4109 ierr = PetscObjectQueryFunction((PetscObject)A,"MatIsTranspose_C",(void (**)(void))&f);CHKERRQ(ierr); 4110 ierr = PetscObjectQueryFunction((PetscObject)B,"MatIsTranspose_C",(void (**)(void))&g);CHKERRQ(ierr); 4111 if (f && g) { 4112 if (f==g) { 4113 ierr = (*f)(A,B,tol,flg);CHKERRQ(ierr); 4114 } else { 4115 SETERRQ(PETSC_ERR_ARG_NOTSAMETYPE,"Matrices do not have the same comparator for symmetry test"); 4116 } 4117 } 4118 PetscFunctionReturn(0); 4119 } 4120 4121 #undef __FUNCT__ 4122 #define __FUNCT__ "MatHermitianTranspose" 4123 /*@ 4124 MatHermitianTranspose - Computes an in-place or out-of-place transpose of a matrix in complex conjugate. 4125 4126 Collective on Mat 4127 4128 Input Parameter: 4129 + mat - the matrix to transpose and complex conjugate 4130 - reuse - store the transpose matrix in the provided B 4131 4132 Output Parameters: 4133 . B - the Hermitian 4134 4135 Notes: 4136 If you pass in &mat for B the Hermitian will be done in place 4137 4138 Level: intermediate 4139 4140 Concepts: matrices^transposing, complex conjugatex 4141 4142 .seealso: MatTranspose(), MatMultTranspose(), MatMultTransposeAdd(), MatIsTranspose(), MatReuse 4143 @*/ 4144 PetscErrorCode PETSCMAT_DLLEXPORT MatHermitianTranspose(Mat mat,MatReuse reuse,Mat *B) 4145 { 4146 PetscErrorCode ierr; 4147 4148 PetscFunctionBegin; 4149 ierr = MatTranspose(mat,reuse,B);CHKERRQ(ierr); 4150 #if defined(PETSC_USE_COMPLEX) 4151 ierr = MatConjugate(*B);CHKERRQ(ierr); 4152 #endif 4153 PetscFunctionReturn(0); 4154 } 4155 4156 #undef __FUNCT__ 4157 #define __FUNCT__ "MatIsHermitianTranspose" 4158 /*@ 4159 MatIsHermitianTranspose - Test whether a matrix is another one's Hermitian transpose, 4160 4161 Collective on Mat 4162 4163 Input Parameter: 4164 + A - the matrix to test 4165 - B - the matrix to test against, this can equal the first parameter 4166 4167 Output Parameters: 4168 . flg - the result 4169 4170 Notes: 4171 Only available for SeqAIJ/MPIAIJ matrices. The sequential algorithm 4172 has a running time of the order of the number of nonzeros; the parallel 4173 test involves parallel copies of the block-offdiagonal parts of the matrix. 4174 4175 Level: intermediate 4176 4177 Concepts: matrices^transposing, matrix^symmetry 4178 4179 .seealso: MatTranspose(), MatIsSymmetric(), MatIsHermitian(), MatIsTranspose() 4180 @*/ 4181 PetscErrorCode PETSCMAT_DLLEXPORT MatIsHermitianTranspose(Mat A,Mat B,PetscReal tol,PetscTruth *flg) 4182 { 4183 PetscErrorCode ierr,(*f)(Mat,Mat,PetscReal,PetscTruth*),(*g)(Mat,Mat,PetscReal,PetscTruth*); 4184 4185 PetscFunctionBegin; 4186 PetscValidHeaderSpecific(A,MAT_COOKIE,1); 4187 PetscValidHeaderSpecific(B,MAT_COOKIE,2); 4188 PetscValidPointer(flg,3); 4189 ierr = PetscObjectQueryFunction((PetscObject)A,"MatIsHermitianTranspose_C",(void (**)(void))&f);CHKERRQ(ierr); 4190 ierr = PetscObjectQueryFunction((PetscObject)B,"MatIsHermitianTranspose_C",(void (**)(void))&g);CHKERRQ(ierr); 4191 if (f && g) { 4192 if (f==g) { 4193 ierr = (*f)(A,B,tol,flg);CHKERRQ(ierr); 4194 } else { 4195 SETERRQ(PETSC_ERR_ARG_NOTSAMETYPE,"Matrices do not have the same comparator for Hermitian test"); 4196 } 4197 } 4198 PetscFunctionReturn(0); 4199 } 4200 4201 #undef __FUNCT__ 4202 #define __FUNCT__ "MatPermute" 4203 /*@ 4204 MatPermute - Creates a new matrix with rows and columns permuted from the 4205 original. 4206 4207 Collective on Mat 4208 4209 Input Parameters: 4210 + mat - the matrix to permute 4211 . row - row permutation, each processor supplies only the permutation for its rows 4212 - col - column permutation, each processor needs the entire column permutation, that is 4213 this is the same size as the total number of columns in the matrix. It can often 4214 be obtained with ISAllGather() on the row permutation 4215 4216 Output Parameters: 4217 . B - the permuted matrix 4218 4219 Level: advanced 4220 4221 Concepts: matrices^permuting 4222 4223 .seealso: MatGetOrdering(), ISAllGather() 4224 4225 @*/ 4226 PetscErrorCode PETSCMAT_DLLEXPORT MatPermute(Mat mat,IS row,IS col,Mat *B) 4227 { 4228 PetscErrorCode ierr; 4229 4230 PetscFunctionBegin; 4231 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 4232 PetscValidType(mat,1); 4233 PetscValidHeaderSpecific(row,IS_COOKIE,2); 4234 PetscValidHeaderSpecific(col,IS_COOKIE,3); 4235 PetscValidPointer(B,4); 4236 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4237 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4238 if (!mat->ops->permute) SETERRQ1(PETSC_ERR_SUP,"MatPermute not available for Mat type %s",((PetscObject)mat)->type_name); 4239 ierr = MatPreallocated(mat);CHKERRQ(ierr); 4240 4241 ierr = (*mat->ops->permute)(mat,row,col,B);CHKERRQ(ierr); 4242 ierr = PetscObjectStateIncrease((PetscObject)*B);CHKERRQ(ierr); 4243 PetscFunctionReturn(0); 4244 } 4245 4246 #undef __FUNCT__ 4247 #define __FUNCT__ "MatPermuteSparsify" 4248 /*@ 4249 MatPermuteSparsify - Creates a new matrix with rows and columns permuted from the 4250 original and sparsified to the prescribed tolerance. 4251 4252 Collective on Mat 4253 4254 Input Parameters: 4255 + A - The matrix to permute 4256 . band - The half-bandwidth of the sparsified matrix, or PETSC_DECIDE 4257 . frac - The half-bandwidth as a fraction of the total size, or 0.0 4258 . tol - The drop tolerance 4259 . rowp - The row permutation 4260 - colp - The column permutation 4261 4262 Output Parameter: 4263 . B - The permuted, sparsified matrix 4264 4265 Level: advanced 4266 4267 Note: 4268 The default behavior (band = PETSC_DECIDE and frac = 0.0) is to 4269 restrict the half-bandwidth of the resulting matrix to 5% of the 4270 total matrix size. 4271 4272 .keywords: matrix, permute, sparsify 4273 4274 .seealso: MatGetOrdering(), MatPermute() 4275 @*/ 4276 PetscErrorCode PETSCMAT_DLLEXPORT MatPermuteSparsify(Mat A, PetscInt band, PetscReal frac, PetscReal tol, IS rowp, IS colp, Mat *B) 4277 { 4278 IS irowp, icolp; 4279 const PetscInt *rows, *cols; 4280 PetscInt M, N, locRowStart = 0, locRowEnd = 0; 4281 PetscInt nz, newNz; 4282 const PetscInt *cwork; 4283 PetscInt *cnew; 4284 const PetscScalar *vwork; 4285 PetscScalar *vnew; 4286 PetscInt bw, issize; 4287 PetscInt row, locRow, newRow, col, newCol; 4288 PetscErrorCode ierr; 4289 4290 PetscFunctionBegin; 4291 PetscValidHeaderSpecific(A, MAT_COOKIE,1); 4292 PetscValidHeaderSpecific(rowp, IS_COOKIE,5); 4293 PetscValidHeaderSpecific(colp, IS_COOKIE,6); 4294 PetscValidPointer(B,7); 4295 if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE, "Not for unassembled matrix"); 4296 if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix"); 4297 if (!A->ops->permutesparsify) { 4298 ierr = MatGetSize(A, &M, &N);CHKERRQ(ierr); 4299 ierr = MatGetOwnershipRange(A, &locRowStart, &locRowEnd);CHKERRQ(ierr); 4300 ierr = ISGetSize(rowp, &issize);CHKERRQ(ierr); 4301 if (issize != M) SETERRQ2(PETSC_ERR_ARG_WRONG, "Wrong size %D for row permutation, should be %D", issize, M); 4302 ierr = ISGetSize(colp, &issize);CHKERRQ(ierr); 4303 if (issize != N) SETERRQ2(PETSC_ERR_ARG_WRONG, "Wrong size %D for column permutation, should be %D", issize, N); 4304 ierr = ISInvertPermutation(rowp, 0, &irowp);CHKERRQ(ierr); 4305 ierr = ISGetIndices(irowp, &rows);CHKERRQ(ierr); 4306 ierr = ISInvertPermutation(colp, 0, &icolp);CHKERRQ(ierr); 4307 ierr = ISGetIndices(icolp, &cols);CHKERRQ(ierr); 4308 ierr = PetscMalloc(N*sizeof(PetscInt),&cnew);CHKERRQ(ierr); 4309 ierr = PetscMalloc(N*sizeof(PetscScalar),&vnew);CHKERRQ(ierr); 4310 4311 /* Setup bandwidth to include */ 4312 if (band == PETSC_DECIDE) { 4313 if (frac <= 0.0) 4314 bw = (PetscInt) (M * 0.05); 4315 else 4316 bw = (PetscInt) (M * frac); 4317 } else { 4318 if (band <= 0) SETERRQ(PETSC_ERR_ARG_WRONG, "Bandwidth must be a positive integer"); 4319 bw = band; 4320 } 4321 4322 /* Put values into new matrix */ 4323 ierr = MatDuplicate(A, MAT_DO_NOT_COPY_VALUES, B);CHKERRQ(ierr); 4324 for(row = locRowStart, locRow = 0; row < locRowEnd; row++, locRow++) { 4325 ierr = MatGetRow(A, row, &nz, &cwork, &vwork);CHKERRQ(ierr); 4326 newRow = rows[locRow]+locRowStart; 4327 for(col = 0, newNz = 0; col < nz; col++) { 4328 newCol = cols[cwork[col]]; 4329 if ((newCol >= newRow - bw) && (newCol < newRow + bw) && (PetscAbsScalar(vwork[col]) >= tol)) { 4330 cnew[newNz] = newCol; 4331 vnew[newNz] = vwork[col]; 4332 newNz++; 4333 } 4334 } 4335 ierr = MatSetValues(*B, 1, &newRow, newNz, cnew, vnew, INSERT_VALUES);CHKERRQ(ierr); 4336 ierr = MatRestoreRow(A, row, &nz, &cwork, &vwork);CHKERRQ(ierr); 4337 } 4338 ierr = PetscFree(cnew);CHKERRQ(ierr); 4339 ierr = PetscFree(vnew);CHKERRQ(ierr); 4340 ierr = MatAssemblyBegin(*B, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4341 ierr = MatAssemblyEnd(*B, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4342 ierr = ISRestoreIndices(irowp, &rows);CHKERRQ(ierr); 4343 ierr = ISRestoreIndices(icolp, &cols);CHKERRQ(ierr); 4344 ierr = ISDestroy(irowp);CHKERRQ(ierr); 4345 ierr = ISDestroy(icolp);CHKERRQ(ierr); 4346 } else { 4347 ierr = (*A->ops->permutesparsify)(A, band, frac, tol, rowp, colp, B);CHKERRQ(ierr); 4348 } 4349 ierr = PetscObjectStateIncrease((PetscObject)*B);CHKERRQ(ierr); 4350 PetscFunctionReturn(0); 4351 } 4352 4353 #undef __FUNCT__ 4354 #define __FUNCT__ "MatEqual" 4355 /*@ 4356 MatEqual - Compares two matrices. 4357 4358 Collective on Mat 4359 4360 Input Parameters: 4361 + A - the first matrix 4362 - B - the second matrix 4363 4364 Output Parameter: 4365 . flg - PETSC_TRUE if the matrices are equal; PETSC_FALSE otherwise. 4366 4367 Level: intermediate 4368 4369 Concepts: matrices^equality between 4370 @*/ 4371 PetscErrorCode PETSCMAT_DLLEXPORT MatEqual(Mat A,Mat B,PetscTruth *flg) 4372 { 4373 PetscErrorCode ierr; 4374 4375 PetscFunctionBegin; 4376 PetscValidHeaderSpecific(A,MAT_COOKIE,1); 4377 PetscValidHeaderSpecific(B,MAT_COOKIE,2); 4378 PetscValidType(A,1); 4379 PetscValidType(B,2); 4380 PetscValidIntPointer(flg,3); 4381 PetscCheckSameComm(A,1,B,2); 4382 ierr = MatPreallocated(B);CHKERRQ(ierr); 4383 if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4384 if (!B->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4385 if (A->rmap->N != B->rmap->N || A->cmap->N != B->cmap->N) SETERRQ4(PETSC_ERR_ARG_SIZ,"Mat A,Mat B: global dim %D %D %D %D",A->rmap->N,B->rmap->N,A->cmap->N,B->cmap->N); 4386 if (!A->ops->equal) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)A)->type_name); 4387 if (!B->ops->equal) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)B)->type_name); 4388 if (A->ops->equal != B->ops->equal) SETERRQ2(PETSC_ERR_ARG_INCOMP,"A is type: %s\nB is type: %s",((PetscObject)A)->type_name,((PetscObject)B)->type_name); 4389 ierr = MatPreallocated(A);CHKERRQ(ierr); 4390 4391 ierr = (*A->ops->equal)(A,B,flg);CHKERRQ(ierr); 4392 PetscFunctionReturn(0); 4393 } 4394 4395 #undef __FUNCT__ 4396 #define __FUNCT__ "MatDiagonalScale" 4397 /*@ 4398 MatDiagonalScale - Scales a matrix on the left and right by diagonal 4399 matrices that are stored as vectors. Either of the two scaling 4400 matrices can be PETSC_NULL. 4401 4402 Collective on Mat 4403 4404 Input Parameters: 4405 + mat - the matrix to be scaled 4406 . l - the left scaling vector (or PETSC_NULL) 4407 - r - the right scaling vector (or PETSC_NULL) 4408 4409 Notes: 4410 MatDiagonalScale() computes A = LAR, where 4411 L = a diagonal matrix (stored as a vector), R = a diagonal matrix (stored as a vector) 4412 4413 Level: intermediate 4414 4415 Concepts: matrices^diagonal scaling 4416 Concepts: diagonal scaling of matrices 4417 4418 .seealso: MatScale() 4419 @*/ 4420 PetscErrorCode PETSCMAT_DLLEXPORT MatDiagonalScale(Mat mat,Vec l,Vec r) 4421 { 4422 PetscErrorCode ierr; 4423 4424 PetscFunctionBegin; 4425 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 4426 PetscValidType(mat,1); 4427 if (!mat->ops->diagonalscale) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 4428 if (l) {PetscValidHeaderSpecific(l,VEC_COOKIE,2);PetscCheckSameComm(mat,1,l,2);} 4429 if (r) {PetscValidHeaderSpecific(r,VEC_COOKIE,3);PetscCheckSameComm(mat,1,r,3);} 4430 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4431 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4432 ierr = MatPreallocated(mat);CHKERRQ(ierr); 4433 4434 ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 4435 ierr = (*mat->ops->diagonalscale)(mat,l,r);CHKERRQ(ierr); 4436 ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 4437 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 4438 PetscFunctionReturn(0); 4439 } 4440 4441 #undef __FUNCT__ 4442 #define __FUNCT__ "MatScale" 4443 /*@ 4444 MatScale - Scales all elements of a matrix by a given number. 4445 4446 Collective on Mat 4447 4448 Input Parameters: 4449 + mat - the matrix to be scaled 4450 - a - the scaling value 4451 4452 Output Parameter: 4453 . mat - the scaled matrix 4454 4455 Level: intermediate 4456 4457 Concepts: matrices^scaling all entries 4458 4459 .seealso: MatDiagonalScale() 4460 @*/ 4461 PetscErrorCode PETSCMAT_DLLEXPORT MatScale(Mat mat,PetscScalar a) 4462 { 4463 PetscErrorCode ierr; 4464 4465 PetscFunctionBegin; 4466 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 4467 PetscValidType(mat,1); 4468 if (a != 1.0 && !mat->ops->scale) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 4469 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4470 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4471 ierr = MatPreallocated(mat);CHKERRQ(ierr); 4472 4473 ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 4474 if (a != 1.0) { 4475 ierr = (*mat->ops->scale)(mat,a);CHKERRQ(ierr); 4476 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 4477 } 4478 ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 4479 PetscFunctionReturn(0); 4480 } 4481 4482 #undef __FUNCT__ 4483 #define __FUNCT__ "MatNorm" 4484 /*@ 4485 MatNorm - Calculates various norms of a matrix. 4486 4487 Collective on Mat 4488 4489 Input Parameters: 4490 + mat - the matrix 4491 - type - the type of norm, NORM_1, NORM_FROBENIUS, NORM_INFINITY 4492 4493 Output Parameters: 4494 . nrm - the resulting norm 4495 4496 Level: intermediate 4497 4498 Concepts: matrices^norm 4499 Concepts: norm^of matrix 4500 @*/ 4501 PetscErrorCode PETSCMAT_DLLEXPORT MatNorm(Mat mat,NormType type,PetscReal *nrm) 4502 { 4503 PetscErrorCode ierr; 4504 4505 PetscFunctionBegin; 4506 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 4507 PetscValidType(mat,1); 4508 PetscValidScalarPointer(nrm,3); 4509 4510 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4511 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4512 if (!mat->ops->norm) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 4513 ierr = MatPreallocated(mat);CHKERRQ(ierr); 4514 4515 ierr = (*mat->ops->norm)(mat,type,nrm);CHKERRQ(ierr); 4516 PetscFunctionReturn(0); 4517 } 4518 4519 /* 4520 This variable is used to prevent counting of MatAssemblyBegin() that 4521 are called from within a MatAssemblyEnd(). 4522 */ 4523 static PetscInt MatAssemblyEnd_InUse = 0; 4524 #undef __FUNCT__ 4525 #define __FUNCT__ "MatAssemblyBegin" 4526 /*@ 4527 MatAssemblyBegin - Begins assembling the matrix. This routine should 4528 be called after completing all calls to MatSetValues(). 4529 4530 Collective on Mat 4531 4532 Input Parameters: 4533 + mat - the matrix 4534 - type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY 4535 4536 Notes: 4537 MatSetValues() generally caches the values. The matrix is ready to 4538 use only after MatAssemblyBegin() and MatAssemblyEnd() have been called. 4539 Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES 4540 in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before 4541 using the matrix. 4542 4543 Level: beginner 4544 4545 Concepts: matrices^assembling 4546 4547 .seealso: MatAssemblyEnd(), MatSetValues(), MatAssembled() 4548 @*/ 4549 PetscErrorCode PETSCMAT_DLLEXPORT MatAssemblyBegin(Mat mat,MatAssemblyType type) 4550 { 4551 PetscErrorCode ierr; 4552 4553 PetscFunctionBegin; 4554 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 4555 PetscValidType(mat,1); 4556 ierr = MatPreallocated(mat);CHKERRQ(ierr); 4557 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix.\nDid you forget to call MatSetUnfactored()?"); 4558 if (mat->assembled) { 4559 mat->was_assembled = PETSC_TRUE; 4560 mat->assembled = PETSC_FALSE; 4561 } 4562 if (!MatAssemblyEnd_InUse) { 4563 ierr = PetscLogEventBegin(MAT_AssemblyBegin,mat,0,0,0);CHKERRQ(ierr); 4564 if (mat->ops->assemblybegin){ierr = (*mat->ops->assemblybegin)(mat,type);CHKERRQ(ierr);} 4565 ierr = PetscLogEventEnd(MAT_AssemblyBegin,mat,0,0,0);CHKERRQ(ierr); 4566 } else { 4567 if (mat->ops->assemblybegin){ierr = (*mat->ops->assemblybegin)(mat,type);CHKERRQ(ierr);} 4568 } 4569 PetscFunctionReturn(0); 4570 } 4571 4572 #undef __FUNCT__ 4573 #define __FUNCT__ "MatAssembled" 4574 /*@ 4575 MatAssembled - Indicates if a matrix has been assembled and is ready for 4576 use; for example, in matrix-vector product. 4577 4578 Collective on Mat 4579 4580 Input Parameter: 4581 . mat - the matrix 4582 4583 Output Parameter: 4584 . assembled - PETSC_TRUE or PETSC_FALSE 4585 4586 Level: advanced 4587 4588 Concepts: matrices^assembled? 4589 4590 .seealso: MatAssemblyEnd(), MatSetValues(), MatAssemblyBegin() 4591 @*/ 4592 PetscErrorCode PETSCMAT_DLLEXPORT MatAssembled(Mat mat,PetscTruth *assembled) 4593 { 4594 PetscFunctionBegin; 4595 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 4596 PetscValidType(mat,1); 4597 PetscValidPointer(assembled,2); 4598 *assembled = mat->assembled; 4599 PetscFunctionReturn(0); 4600 } 4601 4602 #undef __FUNCT__ 4603 #define __FUNCT__ "MatView_Private" 4604 /* 4605 Processes command line options to determine if/how a matrix 4606 is to be viewed. Called by MatAssemblyEnd() and MatLoad(). 4607 */ 4608 PetscErrorCode MatView_Private(Mat mat) 4609 { 4610 PetscErrorCode ierr; 4611 PetscTruth flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE,flg4 = PETSC_FALSE,flg6 = PETSC_FALSE,flg7 = PETSC_FALSE,flg8 = PETSC_FALSE; 4612 static PetscTruth incall = PETSC_FALSE; 4613 #if defined(PETSC_USE_SOCKET_VIEWER) 4614 PetscTruth flg5 = PETSC_FALSE; 4615 #endif 4616 4617 PetscFunctionBegin; 4618 if (incall) PetscFunctionReturn(0); 4619 incall = PETSC_TRUE; 4620 ierr = PetscOptionsBegin(((PetscObject)mat)->comm,((PetscObject)mat)->prefix,"Matrix Options","Mat");CHKERRQ(ierr); 4621 ierr = PetscOptionsTruth("-mat_view_info","Information on matrix size","MatView",flg1,&flg1,PETSC_NULL);CHKERRQ(ierr); 4622 ierr = PetscOptionsTruth("-mat_view_info_detailed","Nonzeros in the matrix","MatView",flg2,&flg2,PETSC_NULL);CHKERRQ(ierr); 4623 ierr = PetscOptionsTruth("-mat_view","Print matrix to stdout","MatView",flg3,&flg3,PETSC_NULL);CHKERRQ(ierr); 4624 ierr = PetscOptionsTruth("-mat_view_matlab","Print matrix to stdout in a format Matlab can read","MatView",flg4,&flg4,PETSC_NULL);CHKERRQ(ierr); 4625 #if defined(PETSC_USE_SOCKET_VIEWER) 4626 ierr = PetscOptionsTruth("-mat_view_socket","Send matrix to socket (can be read from matlab)","MatView",flg5,&flg5,PETSC_NULL);CHKERRQ(ierr); 4627 #endif 4628 ierr = PetscOptionsTruth("-mat_view_binary","Save matrix to file in binary format","MatView",flg6,&flg6,PETSC_NULL);CHKERRQ(ierr); 4629 ierr = PetscOptionsTruth("-mat_view_draw","Draw the matrix nonzero structure","MatView",flg7,&flg7,PETSC_NULL);CHKERRQ(ierr); 4630 ierr = PetscOptionsEnd();CHKERRQ(ierr); 4631 4632 if (flg1) { 4633 PetscViewer viewer; 4634 4635 ierr = PetscViewerASCIIGetStdout(((PetscObject)mat)->comm,&viewer);CHKERRQ(ierr); 4636 ierr = PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_INFO);CHKERRQ(ierr); 4637 ierr = MatView(mat,viewer);CHKERRQ(ierr); 4638 ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 4639 } 4640 if (flg2) { 4641 PetscViewer viewer; 4642 4643 ierr = PetscViewerASCIIGetStdout(((PetscObject)mat)->comm,&viewer);CHKERRQ(ierr); 4644 ierr = PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_INFO_DETAIL);CHKERRQ(ierr); 4645 ierr = MatView(mat,viewer);CHKERRQ(ierr); 4646 ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 4647 } 4648 if (flg3) { 4649 PetscViewer viewer; 4650 4651 ierr = PetscViewerASCIIGetStdout(((PetscObject)mat)->comm,&viewer);CHKERRQ(ierr); 4652 ierr = MatView(mat,viewer);CHKERRQ(ierr); 4653 } 4654 if (flg4) { 4655 PetscViewer viewer; 4656 4657 ierr = PetscViewerASCIIGetStdout(((PetscObject)mat)->comm,&viewer);CHKERRQ(ierr); 4658 ierr = PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB);CHKERRQ(ierr); 4659 ierr = MatView(mat,viewer);CHKERRQ(ierr); 4660 ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 4661 } 4662 #if defined(PETSC_USE_SOCKET_VIEWER) 4663 if (flg5) { 4664 ierr = MatView(mat,PETSC_VIEWER_SOCKET_(((PetscObject)mat)->comm));CHKERRQ(ierr); 4665 ierr = PetscViewerFlush(PETSC_VIEWER_SOCKET_(((PetscObject)mat)->comm));CHKERRQ(ierr); 4666 } 4667 #endif 4668 if (flg6) { 4669 ierr = MatView(mat,PETSC_VIEWER_BINARY_(((PetscObject)mat)->comm));CHKERRQ(ierr); 4670 ierr = PetscViewerFlush(PETSC_VIEWER_BINARY_(((PetscObject)mat)->comm));CHKERRQ(ierr); 4671 } 4672 if (flg7) { 4673 ierr = PetscOptionsGetTruth(((PetscObject)mat)->prefix,"-mat_view_contour",&flg8,PETSC_NULL);CHKERRQ(ierr); 4674 if (flg8) { 4675 PetscViewerPushFormat(PETSC_VIEWER_DRAW_(((PetscObject)mat)->comm),PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr); 4676 } 4677 ierr = MatView(mat,PETSC_VIEWER_DRAW_(((PetscObject)mat)->comm));CHKERRQ(ierr); 4678 ierr = PetscViewerFlush(PETSC_VIEWER_DRAW_(((PetscObject)mat)->comm));CHKERRQ(ierr); 4679 if (flg8) { 4680 PetscViewerPopFormat(PETSC_VIEWER_DRAW_(((PetscObject)mat)->comm));CHKERRQ(ierr); 4681 } 4682 } 4683 incall = PETSC_FALSE; 4684 PetscFunctionReturn(0); 4685 } 4686 4687 #undef __FUNCT__ 4688 #define __FUNCT__ "MatAssemblyEnd" 4689 /*@ 4690 MatAssemblyEnd - Completes assembling the matrix. This routine should 4691 be called after MatAssemblyBegin(). 4692 4693 Collective on Mat 4694 4695 Input Parameters: 4696 + mat - the matrix 4697 - type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY 4698 4699 Options Database Keys: 4700 + -mat_view_info - Prints info on matrix at conclusion of MatEndAssembly() 4701 . -mat_view_info_detailed - Prints more detailed info 4702 . -mat_view - Prints matrix in ASCII format 4703 . -mat_view_matlab - Prints matrix in Matlab format 4704 . -mat_view_draw - PetscDraws nonzero structure of matrix, using MatView() and PetscDrawOpenX(). 4705 . -display <name> - Sets display name (default is host) 4706 . -draw_pause <sec> - Sets number of seconds to pause after display 4707 . -mat_view_socket - Sends matrix to socket, can be accessed from Matlab (see users manual) 4708 . -viewer_socket_machine <machine> 4709 . -viewer_socket_port <port> 4710 . -mat_view_binary - save matrix to file in binary format 4711 - -viewer_binary_filename <name> 4712 4713 Notes: 4714 MatSetValues() generally caches the values. The matrix is ready to 4715 use only after MatAssemblyBegin() and MatAssemblyEnd() have been called. 4716 Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES 4717 in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before 4718 using the matrix. 4719 4720 Level: beginner 4721 4722 .seealso: MatAssemblyBegin(), MatSetValues(), PetscDrawOpenX(), MatView(), MatAssembled(), PetscViewerSocketOpen() 4723 @*/ 4724 PetscErrorCode PETSCMAT_DLLEXPORT MatAssemblyEnd(Mat mat,MatAssemblyType type) 4725 { 4726 PetscErrorCode ierr; 4727 static PetscInt inassm = 0; 4728 PetscTruth flg = PETSC_FALSE; 4729 4730 PetscFunctionBegin; 4731 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 4732 PetscValidType(mat,1); 4733 4734 inassm++; 4735 MatAssemblyEnd_InUse++; 4736 if (MatAssemblyEnd_InUse == 1) { /* Do the logging only the first time through */ 4737 ierr = PetscLogEventBegin(MAT_AssemblyEnd,mat,0,0,0);CHKERRQ(ierr); 4738 if (mat->ops->assemblyend) { 4739 ierr = (*mat->ops->assemblyend)(mat,type);CHKERRQ(ierr); 4740 } 4741 ierr = PetscLogEventEnd(MAT_AssemblyEnd,mat,0,0,0);CHKERRQ(ierr); 4742 } else { 4743 if (mat->ops->assemblyend) { 4744 ierr = (*mat->ops->assemblyend)(mat,type);CHKERRQ(ierr); 4745 } 4746 } 4747 4748 /* Flush assembly is not a true assembly */ 4749 if (type != MAT_FLUSH_ASSEMBLY) { 4750 mat->assembled = PETSC_TRUE; mat->num_ass++; 4751 } 4752 mat->insertmode = NOT_SET_VALUES; 4753 MatAssemblyEnd_InUse--; 4754 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 4755 if (!mat->symmetric_eternal) { 4756 mat->symmetric_set = PETSC_FALSE; 4757 mat->hermitian_set = PETSC_FALSE; 4758 mat->structurally_symmetric_set = PETSC_FALSE; 4759 } 4760 if (inassm == 1 && type != MAT_FLUSH_ASSEMBLY) { 4761 ierr = MatView_Private(mat);CHKERRQ(ierr); 4762 ierr = PetscOptionsGetTruth(((PetscObject)mat)->prefix,"-mat_is_symmetric",&flg,PETSC_NULL);CHKERRQ(ierr); 4763 if (flg) { 4764 PetscReal tol = 0.0; 4765 ierr = PetscOptionsGetReal(((PetscObject)mat)->prefix,"-mat_is_symmetric",&tol,PETSC_NULL);CHKERRQ(ierr); 4766 ierr = MatIsSymmetric(mat,tol,&flg);CHKERRQ(ierr); 4767 if (flg) { 4768 ierr = PetscPrintf(((PetscObject)mat)->comm,"Matrix is symmetric (tolerance %G)\n",tol);CHKERRQ(ierr); 4769 } else { 4770 ierr = PetscPrintf(((PetscObject)mat)->comm,"Matrix is not symmetric (tolerance %G)\n",tol);CHKERRQ(ierr); 4771 } 4772 } 4773 } 4774 inassm--; 4775 PetscFunctionReturn(0); 4776 } 4777 4778 #undef __FUNCT__ 4779 #define __FUNCT__ "MatSetOption" 4780 /*@ 4781 MatSetOption - Sets a parameter option for a matrix. Some options 4782 may be specific to certain storage formats. Some options 4783 determine how values will be inserted (or added). Sorted, 4784 row-oriented input will generally assemble the fastest. The default 4785 is row-oriented, nonsorted input. 4786 4787 Collective on Mat 4788 4789 Input Parameters: 4790 + mat - the matrix 4791 . option - the option, one of those listed below (and possibly others), 4792 - flg - turn the option on (PETSC_TRUE) or off (PETSC_FALSE) 4793 4794 Options Describing Matrix Structure: 4795 + MAT_SYMMETRIC - symmetric in terms of both structure and value 4796 . MAT_HERMITIAN - transpose is the complex conjugation 4797 . MAT_STRUCTURALLY_SYMMETRIC - symmetric nonzero structure 4798 - MAT_SYMMETRY_ETERNAL - if you would like the symmetry/Hermitian flag 4799 you set to be kept with all future use of the matrix 4800 including after MatAssemblyBegin/End() which could 4801 potentially change the symmetry structure, i.e. you 4802 KNOW the matrix will ALWAYS have the property you set. 4803 4804 4805 Options For Use with MatSetValues(): 4806 Insert a logically dense subblock, which can be 4807 . MAT_ROW_ORIENTED - row-oriented (default) 4808 4809 Note these options reflect the data you pass in with MatSetValues(); it has 4810 nothing to do with how the data is stored internally in the matrix 4811 data structure. 4812 4813 When (re)assembling a matrix, we can restrict the input for 4814 efficiency/debugging purposes. These options include 4815 + MAT_NEW_NONZERO_LOCATIONS - additional insertions will be 4816 allowed if they generate a new nonzero 4817 . MAT_NEW_DIAGONALS - new diagonals will be allowed (for block diagonal format only) 4818 . MAT_IGNORE_OFF_PROC_ENTRIES - drops off-processor entries 4819 . MAT_NEW_NONZERO_LOCATION_ERR - generates an error for new matrix entry 4820 - MAT_USE_HASH_TABLE - uses a hash table to speed up matrix assembly 4821 4822 Notes: 4823 Some options are relevant only for particular matrix types and 4824 are thus ignored by others. Other options are not supported by 4825 certain matrix types and will generate an error message if set. 4826 4827 If using a Fortran 77 module to compute a matrix, one may need to 4828 use the column-oriented option (or convert to the row-oriented 4829 format). 4830 4831 MAT_NEW_NONZERO_LOCATIONS set to PETSC_FALSE indicates that any add or insertion 4832 that would generate a new entry in the nonzero structure is instead 4833 ignored. Thus, if memory has not alredy been allocated for this particular 4834 data, then the insertion is ignored. For dense matrices, in which 4835 the entire array is allocated, no entries are ever ignored. 4836 Set after the first MatAssemblyEnd() 4837 4838 MAT_NEW_NONZERO_LOCATION_ERR indicates that any add or insertion 4839 that would generate a new entry in the nonzero structure instead produces 4840 an error. (Currently supported for AIJ and BAIJ formats only.) 4841 This is a useful flag when using SAME_NONZERO_PATTERN in calling 4842 KSPSetOperators() to ensure that the nonzero pattern truely does 4843 remain unchanged. Set after the first MatAssemblyEnd() 4844 4845 MAT_NEW_NONZERO_ALLOCATION_ERR indicates that any add or insertion 4846 that would generate a new entry that has not been preallocated will 4847 instead produce an error. (Currently supported for AIJ and BAIJ formats 4848 only.) This is a useful flag when debugging matrix memory preallocation. 4849 4850 MAT_IGNORE_OFF_PROC_ENTRIES indicates entries destined for 4851 other processors should be dropped, rather than stashed. 4852 This is useful if you know that the "owning" processor is also 4853 always generating the correct matrix entries, so that PETSc need 4854 not transfer duplicate entries generated on another processor. 4855 4856 MAT_USE_HASH_TABLE indicates that a hash table be used to improve the 4857 searches during matrix assembly. When this flag is set, the hash table 4858 is created during the first Matrix Assembly. This hash table is 4859 used the next time through, during MatSetVaules()/MatSetVaulesBlocked() 4860 to improve the searching of indices. MAT_NEW_NONZERO_LOCATIONS flag 4861 should be used with MAT_USE_HASH_TABLE flag. This option is currently 4862 supported by MATMPIBAIJ format only. 4863 4864 MAT_KEEP_NONZERO_PATTERN indicates when MatZeroRows() is called the zeroed entries 4865 are kept in the nonzero structure 4866 4867 MAT_IGNORE_ZERO_ENTRIES - for AIJ/IS matrices this will stop zero values from creating 4868 a zero location in the matrix 4869 4870 MAT_USE_INODES - indicates using inode version of the code - works with AIJ and 4871 ROWBS matrix types 4872 4873 Level: intermediate 4874 4875 Concepts: matrices^setting options 4876 4877 @*/ 4878 PetscErrorCode PETSCMAT_DLLEXPORT MatSetOption(Mat mat,MatOption op,PetscTruth flg) 4879 { 4880 PetscErrorCode ierr; 4881 4882 PetscFunctionBegin; 4883 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 4884 PetscValidType(mat,1); 4885 if (((int) op) < 0 || ((int) op) >= NUM_MAT_OPTIONS) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Options %d is out of range",(int)op); 4886 ierr = MatPreallocated(mat);CHKERRQ(ierr); 4887 switch (op) { 4888 case MAT_SYMMETRIC: 4889 mat->symmetric = flg; 4890 if (flg) mat->structurally_symmetric = PETSC_TRUE; 4891 mat->symmetric_set = PETSC_TRUE; 4892 mat->structurally_symmetric_set = flg; 4893 break; 4894 case MAT_HERMITIAN: 4895 mat->hermitian = flg; 4896 if (flg) mat->structurally_symmetric = PETSC_TRUE; 4897 mat->hermitian_set = PETSC_TRUE; 4898 mat->structurally_symmetric_set = flg; 4899 break; 4900 case MAT_STRUCTURALLY_SYMMETRIC: 4901 mat->structurally_symmetric = flg; 4902 mat->structurally_symmetric_set = PETSC_TRUE; 4903 break; 4904 case MAT_SYMMETRY_ETERNAL: 4905 mat->symmetric_eternal = flg; 4906 break; 4907 default: 4908 break; 4909 } 4910 if (mat->ops->setoption) { 4911 ierr = (*mat->ops->setoption)(mat,op,flg);CHKERRQ(ierr); 4912 } 4913 PetscFunctionReturn(0); 4914 } 4915 4916 #undef __FUNCT__ 4917 #define __FUNCT__ "MatZeroEntries" 4918 /*@ 4919 MatZeroEntries - Zeros all entries of a matrix. For sparse matrices 4920 this routine retains the old nonzero structure. 4921 4922 Collective on Mat 4923 4924 Input Parameters: 4925 . mat - the matrix 4926 4927 Level: intermediate 4928 4929 Concepts: matrices^zeroing 4930 4931 .seealso: MatZeroRows() 4932 @*/ 4933 PetscErrorCode PETSCMAT_DLLEXPORT MatZeroEntries(Mat mat) 4934 { 4935 PetscErrorCode ierr; 4936 4937 PetscFunctionBegin; 4938 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 4939 PetscValidType(mat,1); 4940 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4941 if (mat->insertmode != NOT_SET_VALUES) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for matrices where you have set values but not yet assembled"); 4942 if (!mat->ops->zeroentries) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 4943 ierr = MatPreallocated(mat);CHKERRQ(ierr); 4944 4945 ierr = PetscLogEventBegin(MAT_ZeroEntries,mat,0,0,0);CHKERRQ(ierr); 4946 ierr = (*mat->ops->zeroentries)(mat);CHKERRQ(ierr); 4947 ierr = PetscLogEventEnd(MAT_ZeroEntries,mat,0,0,0);CHKERRQ(ierr); 4948 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 4949 PetscFunctionReturn(0); 4950 } 4951 4952 #undef __FUNCT__ 4953 #define __FUNCT__ "MatZeroRows" 4954 /*@C 4955 MatZeroRows - Zeros all entries (except possibly the main diagonal) 4956 of a set of rows of a matrix. 4957 4958 Collective on Mat 4959 4960 Input Parameters: 4961 + mat - the matrix 4962 . numRows - the number of rows to remove 4963 . rows - the global row indices 4964 - diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry) 4965 4966 Notes: 4967 For the AIJ and BAIJ matrix formats this removes the old nonzero structure, 4968 but does not release memory. For the dense and block diagonal 4969 formats this does not alter the nonzero structure. 4970 4971 If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure 4972 of the matrix is not changed (even for AIJ and BAIJ matrices) the values are 4973 merely zeroed. 4974 4975 The user can set a value in the diagonal entry (or for the AIJ and 4976 row formats can optionally remove the main diagonal entry from the 4977 nonzero structure as well, by passing 0.0 as the final argument). 4978 4979 For the parallel case, all processes that share the matrix (i.e., 4980 those in the communicator used for matrix creation) MUST call this 4981 routine, regardless of whether any rows being zeroed are owned by 4982 them. 4983 4984 Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to 4985 list only rows local to itself). 4986 4987 Level: intermediate 4988 4989 Concepts: matrices^zeroing rows 4990 4991 .seealso: MatZeroRowsIS(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption() 4992 @*/ 4993 PetscErrorCode PETSCMAT_DLLEXPORT MatZeroRows(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag) 4994 { 4995 PetscErrorCode ierr; 4996 4997 PetscFunctionBegin; 4998 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 4999 PetscValidType(mat,1); 5000 if (numRows) PetscValidIntPointer(rows,3); 5001 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5002 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5003 if (!mat->ops->zerorows) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 5004 ierr = MatPreallocated(mat);CHKERRQ(ierr); 5005 5006 ierr = (*mat->ops->zerorows)(mat,numRows,rows,diag);CHKERRQ(ierr); 5007 ierr = MatView_Private(mat);CHKERRQ(ierr); 5008 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 5009 PetscFunctionReturn(0); 5010 } 5011 5012 #undef __FUNCT__ 5013 #define __FUNCT__ "MatZeroRowsIS" 5014 /*@C 5015 MatZeroRowsIS - Zeros all entries (except possibly the main diagonal) 5016 of a set of rows of a matrix. 5017 5018 Collective on Mat 5019 5020 Input Parameters: 5021 + mat - the matrix 5022 . is - index set of rows to remove 5023 - diag - value put in all diagonals of eliminated rows 5024 5025 Notes: 5026 For the AIJ and BAIJ matrix formats this removes the old nonzero structure, 5027 but does not release memory. For the dense and block diagonal 5028 formats this does not alter the nonzero structure. 5029 5030 If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure 5031 of the matrix is not changed (even for AIJ and BAIJ matrices) the values are 5032 merely zeroed. 5033 5034 The user can set a value in the diagonal entry (or for the AIJ and 5035 row formats can optionally remove the main diagonal entry from the 5036 nonzero structure as well, by passing 0.0 as the final argument). 5037 5038 For the parallel case, all processes that share the matrix (i.e., 5039 those in the communicator used for matrix creation) MUST call this 5040 routine, regardless of whether any rows being zeroed are owned by 5041 them. 5042 5043 Each processor should list the rows that IT wants zeroed 5044 5045 Level: intermediate 5046 5047 Concepts: matrices^zeroing rows 5048 5049 .seealso: MatZeroRows(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption() 5050 @*/ 5051 PetscErrorCode PETSCMAT_DLLEXPORT MatZeroRowsIS(Mat mat,IS is,PetscScalar diag) 5052 { 5053 PetscInt numRows; 5054 const PetscInt *rows; 5055 PetscErrorCode ierr; 5056 5057 PetscFunctionBegin; 5058 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 5059 PetscValidType(mat,1); 5060 PetscValidHeaderSpecific(is,IS_COOKIE,2); 5061 ierr = ISGetLocalSize(is,&numRows);CHKERRQ(ierr); 5062 ierr = ISGetIndices(is,&rows);CHKERRQ(ierr); 5063 ierr = MatZeroRows(mat,numRows,rows,diag);CHKERRQ(ierr); 5064 ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr); 5065 PetscFunctionReturn(0); 5066 } 5067 5068 #undef __FUNCT__ 5069 #define __FUNCT__ "MatZeroRowsLocal" 5070 /*@C 5071 MatZeroRowsLocal - Zeros all entries (except possibly the main diagonal) 5072 of a set of rows of a matrix; using local numbering of rows. 5073 5074 Collective on Mat 5075 5076 Input Parameters: 5077 + mat - the matrix 5078 . numRows - the number of rows to remove 5079 . rows - the global row indices 5080 - diag - value put in all diagonals of eliminated rows 5081 5082 Notes: 5083 Before calling MatZeroRowsLocal(), the user must first set the 5084 local-to-global mapping by calling MatSetLocalToGlobalMapping(). 5085 5086 For the AIJ matrix formats this removes the old nonzero structure, 5087 but does not release memory. For the dense and block diagonal 5088 formats this does not alter the nonzero structure. 5089 5090 If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure 5091 of the matrix is not changed (even for AIJ and BAIJ matrices) the values are 5092 merely zeroed. 5093 5094 The user can set a value in the diagonal entry (or for the AIJ and 5095 row formats can optionally remove the main diagonal entry from the 5096 nonzero structure as well, by passing 0.0 as the final argument). 5097 5098 Level: intermediate 5099 5100 Concepts: matrices^zeroing 5101 5102 .seealso: MatZeroRows(), MatZeroRowsLocalIS(), MatZeroEntries(), MatZeroRows(), MatSetLocalToGlobalMapping 5103 @*/ 5104 PetscErrorCode PETSCMAT_DLLEXPORT MatZeroRowsLocal(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag) 5105 { 5106 PetscErrorCode ierr; 5107 5108 PetscFunctionBegin; 5109 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 5110 PetscValidType(mat,1); 5111 if (numRows) PetscValidIntPointer(rows,3); 5112 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5113 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5114 ierr = MatPreallocated(mat);CHKERRQ(ierr); 5115 5116 if (mat->ops->zerorowslocal) { 5117 ierr = (*mat->ops->zerorowslocal)(mat,numRows,rows,diag);CHKERRQ(ierr); 5118 } else { 5119 IS is, newis; 5120 const PetscInt *newRows; 5121 5122 if (!mat->mapping) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Need to provide local to global mapping to matrix first"); 5123 ierr = ISCreateGeneral(PETSC_COMM_SELF,numRows,rows,&is);CHKERRQ(ierr); 5124 ierr = ISLocalToGlobalMappingApplyIS(mat->mapping,is,&newis);CHKERRQ(ierr); 5125 ierr = ISGetIndices(newis,&newRows);CHKERRQ(ierr); 5126 ierr = (*mat->ops->zerorows)(mat,numRows,newRows,diag);CHKERRQ(ierr); 5127 ierr = ISRestoreIndices(newis,&newRows);CHKERRQ(ierr); 5128 ierr = ISDestroy(newis);CHKERRQ(ierr); 5129 ierr = ISDestroy(is);CHKERRQ(ierr); 5130 } 5131 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 5132 PetscFunctionReturn(0); 5133 } 5134 5135 #undef __FUNCT__ 5136 #define __FUNCT__ "MatZeroRowsLocalIS" 5137 /*@C 5138 MatZeroRowsLocalIS - Zeros all entries (except possibly the main diagonal) 5139 of a set of rows of a matrix; using local numbering of rows. 5140 5141 Collective on Mat 5142 5143 Input Parameters: 5144 + mat - the matrix 5145 . is - index set of rows to remove 5146 - diag - value put in all diagonals of eliminated rows 5147 5148 Notes: 5149 Before calling MatZeroRowsLocalIS(), the user must first set the 5150 local-to-global mapping by calling MatSetLocalToGlobalMapping(). 5151 5152 For the AIJ matrix formats this removes the old nonzero structure, 5153 but does not release memory. For the dense and block diagonal 5154 formats this does not alter the nonzero structure. 5155 5156 If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure 5157 of the matrix is not changed (even for AIJ and BAIJ matrices) the values are 5158 merely zeroed. 5159 5160 The user can set a value in the diagonal entry (or for the AIJ and 5161 row formats can optionally remove the main diagonal entry from the 5162 nonzero structure as well, by passing 0.0 as the final argument). 5163 5164 Level: intermediate 5165 5166 Concepts: matrices^zeroing 5167 5168 .seealso: MatZeroRows(), MatZeroRowsLocal(), MatZeroEntries(), MatZeroRows(), MatSetLocalToGlobalMapping 5169 @*/ 5170 PetscErrorCode PETSCMAT_DLLEXPORT MatZeroRowsLocalIS(Mat mat,IS is,PetscScalar diag) 5171 { 5172 PetscErrorCode ierr; 5173 PetscInt numRows; 5174 const PetscInt *rows; 5175 5176 PetscFunctionBegin; 5177 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 5178 PetscValidType(mat,1); 5179 PetscValidHeaderSpecific(is,IS_COOKIE,2); 5180 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5181 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5182 ierr = MatPreallocated(mat);CHKERRQ(ierr); 5183 5184 ierr = ISGetLocalSize(is,&numRows);CHKERRQ(ierr); 5185 ierr = ISGetIndices(is,&rows);CHKERRQ(ierr); 5186 ierr = MatZeroRowsLocal(mat,numRows,rows,diag);CHKERRQ(ierr); 5187 ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr); 5188 PetscFunctionReturn(0); 5189 } 5190 5191 #undef __FUNCT__ 5192 #define __FUNCT__ "MatGetSize" 5193 /*@ 5194 MatGetSize - Returns the numbers of rows and columns in a matrix. 5195 5196 Not Collective 5197 5198 Input Parameter: 5199 . mat - the matrix 5200 5201 Output Parameters: 5202 + m - the number of global rows 5203 - n - the number of global columns 5204 5205 Note: both output parameters can be PETSC_NULL on input. 5206 5207 Level: beginner 5208 5209 Concepts: matrices^size 5210 5211 .seealso: MatGetLocalSize() 5212 @*/ 5213 PetscErrorCode PETSCMAT_DLLEXPORT MatGetSize(Mat mat,PetscInt *m,PetscInt* n) 5214 { 5215 PetscFunctionBegin; 5216 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 5217 if (m) *m = mat->rmap->N; 5218 if (n) *n = mat->cmap->N; 5219 PetscFunctionReturn(0); 5220 } 5221 5222 #undef __FUNCT__ 5223 #define __FUNCT__ "MatGetLocalSize" 5224 /*@ 5225 MatGetLocalSize - Returns the number of rows and columns in a matrix 5226 stored locally. This information may be implementation dependent, so 5227 use with care. 5228 5229 Not Collective 5230 5231 Input Parameters: 5232 . mat - the matrix 5233 5234 Output Parameters: 5235 + m - the number of local rows 5236 - n - the number of local columns 5237 5238 Note: both output parameters can be PETSC_NULL on input. 5239 5240 Level: beginner 5241 5242 Concepts: matrices^local size 5243 5244 .seealso: MatGetSize() 5245 @*/ 5246 PetscErrorCode PETSCMAT_DLLEXPORT MatGetLocalSize(Mat mat,PetscInt *m,PetscInt* n) 5247 { 5248 PetscFunctionBegin; 5249 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 5250 if (m) PetscValidIntPointer(m,2); 5251 if (n) PetscValidIntPointer(n,3); 5252 if (m) *m = mat->rmap->n; 5253 if (n) *n = mat->cmap->n; 5254 PetscFunctionReturn(0); 5255 } 5256 5257 #undef __FUNCT__ 5258 #define __FUNCT__ "MatGetOwnershipRangeColumn" 5259 /*@ 5260 MatGetOwnershipRangeColumn - Returns the range of matrix columns owned by 5261 this processor. 5262 5263 Not Collective, unless matrix has not been allocated, then collective on Mat 5264 5265 Input Parameters: 5266 . mat - the matrix 5267 5268 Output Parameters: 5269 + m - the global index of the first local column 5270 - n - one more than the global index of the last local column 5271 5272 Notes: both output parameters can be PETSC_NULL on input. 5273 5274 Level: developer 5275 5276 Concepts: matrices^column ownership 5277 5278 .seealso: MatGetOwnershipRange(), MatGetOwnershipRanges(), MatGetOwnershipRangesColumn() 5279 5280 @*/ 5281 PetscErrorCode PETSCMAT_DLLEXPORT MatGetOwnershipRangeColumn(Mat mat,PetscInt *m,PetscInt* n) 5282 { 5283 PetscErrorCode ierr; 5284 5285 PetscFunctionBegin; 5286 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 5287 PetscValidType(mat,1); 5288 if (m) PetscValidIntPointer(m,2); 5289 if (n) PetscValidIntPointer(n,3); 5290 ierr = MatPreallocated(mat);CHKERRQ(ierr); 5291 if (m) *m = mat->cmap->rstart; 5292 if (n) *n = mat->cmap->rend; 5293 PetscFunctionReturn(0); 5294 } 5295 5296 #undef __FUNCT__ 5297 #define __FUNCT__ "MatGetOwnershipRange" 5298 /*@ 5299 MatGetOwnershipRange - Returns the range of matrix rows owned by 5300 this processor, assuming that the matrix is laid out with the first 5301 n1 rows on the first processor, the next n2 rows on the second, etc. 5302 For certain parallel layouts this range may not be well defined. 5303 5304 Not Collective, unless matrix has not been allocated, then collective on Mat 5305 5306 Input Parameters: 5307 . mat - the matrix 5308 5309 Output Parameters: 5310 + m - the global index of the first local row 5311 - n - one more than the global index of the last local row 5312 5313 Note: both output parameters can be PETSC_NULL on input. 5314 5315 Level: beginner 5316 5317 Concepts: matrices^row ownership 5318 5319 .seealso: MatGetOwnershipRanges(), MatGetOwnershipRangeColumn(), MatGetOwnershipRangesColumn() 5320 5321 @*/ 5322 PetscErrorCode PETSCMAT_DLLEXPORT MatGetOwnershipRange(Mat mat,PetscInt *m,PetscInt* n) 5323 { 5324 PetscErrorCode ierr; 5325 5326 PetscFunctionBegin; 5327 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 5328 PetscValidType(mat,1); 5329 if (m) PetscValidIntPointer(m,2); 5330 if (n) PetscValidIntPointer(n,3); 5331 ierr = MatPreallocated(mat);CHKERRQ(ierr); 5332 if (m) *m = mat->rmap->rstart; 5333 if (n) *n = mat->rmap->rend; 5334 PetscFunctionReturn(0); 5335 } 5336 5337 #undef __FUNCT__ 5338 #define __FUNCT__ "MatGetOwnershipRanges" 5339 /*@C 5340 MatGetOwnershipRanges - Returns the range of matrix rows owned by 5341 each process 5342 5343 Not Collective, unless matrix has not been allocated, then collective on Mat 5344 5345 Input Parameters: 5346 . mat - the matrix 5347 5348 Output Parameters: 5349 . ranges - start of each processors portion plus one more then the total length at the end 5350 5351 Level: beginner 5352 5353 Concepts: matrices^row ownership 5354 5355 .seealso: MatGetOwnershipRange(), MatGetOwnershipRangeColumn(), MatGetOwnershipRangesColumn() 5356 5357 @*/ 5358 PetscErrorCode PETSCMAT_DLLEXPORT MatGetOwnershipRanges(Mat mat,const PetscInt **ranges) 5359 { 5360 PetscErrorCode ierr; 5361 5362 PetscFunctionBegin; 5363 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 5364 PetscValidType(mat,1); 5365 ierr = MatPreallocated(mat);CHKERRQ(ierr); 5366 ierr = PetscLayoutGetRanges(mat->rmap,ranges);CHKERRQ(ierr); 5367 PetscFunctionReturn(0); 5368 } 5369 5370 #undef __FUNCT__ 5371 #define __FUNCT__ "MatGetOwnershipRangesColumn" 5372 /*@C 5373 MatGetOwnershipRangesColumn - Returns the range of local columns for each process 5374 5375 Not Collective, unless matrix has not been allocated, then collective on Mat 5376 5377 Input Parameters: 5378 . mat - the matrix 5379 5380 Output Parameters: 5381 . ranges - start of each processors portion plus one more then the total length at the end 5382 5383 Level: beginner 5384 5385 Concepts: matrices^column ownership 5386 5387 .seealso: MatGetOwnershipRange(), MatGetOwnershipRangeColumn(), MatGetOwnershipRanges() 5388 5389 @*/ 5390 PetscErrorCode PETSCMAT_DLLEXPORT MatGetOwnershipRangesColumn(Mat mat,const PetscInt **ranges) 5391 { 5392 PetscErrorCode ierr; 5393 5394 PetscFunctionBegin; 5395 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 5396 PetscValidType(mat,1); 5397 ierr = MatPreallocated(mat);CHKERRQ(ierr); 5398 ierr = PetscLayoutGetRanges(mat->cmap,ranges);CHKERRQ(ierr); 5399 PetscFunctionReturn(0); 5400 } 5401 5402 #undef __FUNCT__ 5403 #define __FUNCT__ "MatILUFactorSymbolic" 5404 /*@C 5405 MatILUFactorSymbolic - Performs symbolic ILU factorization of a matrix. 5406 Uses levels of fill only, not drop tolerance. Use MatLUFactorNumeric() 5407 to complete the factorization. 5408 5409 Collective on Mat 5410 5411 Input Parameters: 5412 + mat - the matrix 5413 . row - row permutation 5414 . column - column permutation 5415 - info - structure containing 5416 $ levels - number of levels of fill. 5417 $ expected fill - as ratio of original fill. 5418 $ 1 or 0 - indicating force fill on diagonal (improves robustness for matrices 5419 missing diagonal entries) 5420 5421 Output Parameters: 5422 . fact - new matrix that has been symbolically factored 5423 5424 Notes: 5425 See the users manual for additional information about 5426 choosing the fill factor for better efficiency. 5427 5428 Most users should employ the simplified KSP interface for linear solvers 5429 instead of working directly with matrix algebra routines such as this. 5430 See, e.g., KSPCreate(). 5431 5432 Level: developer 5433 5434 Concepts: matrices^symbolic LU factorization 5435 Concepts: matrices^factorization 5436 Concepts: LU^symbolic factorization 5437 5438 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor() 5439 MatGetOrdering(), MatFactorInfo 5440 5441 Developer Note: fortran interface is not autogenerated as the f90 5442 interface defintion cannot be generated correctly [due to MatFactorInfo] 5443 5444 @*/ 5445 PetscErrorCode PETSCMAT_DLLEXPORT MatILUFactorSymbolic(Mat fact,Mat mat,IS row,IS col,const MatFactorInfo *info) 5446 { 5447 PetscErrorCode ierr; 5448 5449 PetscFunctionBegin; 5450 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 5451 PetscValidType(mat,1); 5452 PetscValidHeaderSpecific(row,IS_COOKIE,2); 5453 PetscValidHeaderSpecific(col,IS_COOKIE,3); 5454 PetscValidPointer(info,4); 5455 PetscValidPointer(fact,5); 5456 if (info->levels < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Levels of fill negative %D",(PetscInt)info->levels); 5457 if (info->fill < 1.0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %G",info->fill); 5458 if (!(fact)->ops->ilufactorsymbolic) SETERRQ1(PETSC_ERR_SUP,"Matrix type %s symbolic ILU",((PetscObject)mat)->type_name); 5459 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5460 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5461 ierr = MatPreallocated(mat);CHKERRQ(ierr); 5462 5463 ierr = PetscLogEventBegin(MAT_ILUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr); 5464 ierr = (fact->ops->ilufactorsymbolic)(fact,mat,row,col,info);CHKERRQ(ierr); 5465 ierr = PetscLogEventEnd(MAT_ILUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr); 5466 PetscFunctionReturn(0); 5467 } 5468 5469 #undef __FUNCT__ 5470 #define __FUNCT__ "MatICCFactorSymbolic" 5471 /*@C 5472 MatICCFactorSymbolic - Performs symbolic incomplete 5473 Cholesky factorization for a symmetric matrix. Use 5474 MatCholeskyFactorNumeric() to complete the factorization. 5475 5476 Collective on Mat 5477 5478 Input Parameters: 5479 + mat - the matrix 5480 . perm - row and column permutation 5481 - info - structure containing 5482 $ levels - number of levels of fill. 5483 $ expected fill - as ratio of original fill. 5484 5485 Output Parameter: 5486 . fact - the factored matrix 5487 5488 Notes: 5489 Most users should employ the KSP interface for linear solvers 5490 instead of working directly with matrix algebra routines such as this. 5491 See, e.g., KSPCreate(). 5492 5493 Level: developer 5494 5495 Concepts: matrices^symbolic incomplete Cholesky factorization 5496 Concepts: matrices^factorization 5497 Concepts: Cholsky^symbolic factorization 5498 5499 .seealso: MatCholeskyFactorNumeric(), MatCholeskyFactor(), MatFactorInfo 5500 5501 Developer Note: fortran interface is not autogenerated as the f90 5502 interface defintion cannot be generated correctly [due to MatFactorInfo] 5503 5504 @*/ 5505 PetscErrorCode PETSCMAT_DLLEXPORT MatICCFactorSymbolic(Mat fact,Mat mat,IS perm,const MatFactorInfo *info) 5506 { 5507 PetscErrorCode ierr; 5508 5509 PetscFunctionBegin; 5510 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 5511 PetscValidType(mat,1); 5512 PetscValidHeaderSpecific(perm,IS_COOKIE,2); 5513 PetscValidPointer(info,3); 5514 PetscValidPointer(fact,4); 5515 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5516 if (info->levels < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Levels negative %D",(PetscInt) info->levels); 5517 if (info->fill < 1.0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %G",info->fill); 5518 if (!(fact)->ops->iccfactorsymbolic) SETERRQ1(PETSC_ERR_SUP,"Matrix type %s symbolic ICC",((PetscObject)mat)->type_name); 5519 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5520 ierr = MatPreallocated(mat);CHKERRQ(ierr); 5521 5522 ierr = PetscLogEventBegin(MAT_ICCFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr); 5523 ierr = (fact->ops->iccfactorsymbolic)(fact,mat,perm,info);CHKERRQ(ierr); 5524 ierr = PetscLogEventEnd(MAT_ICCFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr); 5525 PetscFunctionReturn(0); 5526 } 5527 5528 #undef __FUNCT__ 5529 #define __FUNCT__ "MatGetArray" 5530 /*@C 5531 MatGetArray - Returns a pointer to the element values in the matrix. 5532 The result of this routine is dependent on the underlying matrix data 5533 structure, and may not even work for certain matrix types. You MUST 5534 call MatRestoreArray() when you no longer need to access the array. 5535 5536 Not Collective 5537 5538 Input Parameter: 5539 . mat - the matrix 5540 5541 Output Parameter: 5542 . v - the location of the values 5543 5544 5545 Fortran Note: 5546 This routine is used differently from Fortran, e.g., 5547 .vb 5548 Mat mat 5549 PetscScalar mat_array(1) 5550 PetscOffset i_mat 5551 PetscErrorCode ierr 5552 call MatGetArray(mat,mat_array,i_mat,ierr) 5553 5554 C Access first local entry in matrix; note that array is 5555 C treated as one dimensional 5556 value = mat_array(i_mat + 1) 5557 5558 [... other code ...] 5559 call MatRestoreArray(mat,mat_array,i_mat,ierr) 5560 .ve 5561 5562 See the Fortran chapter of the users manual and 5563 petsc/src/mat/examples/tests for details. 5564 5565 Level: advanced 5566 5567 Concepts: matrices^access array 5568 5569 .seealso: MatRestoreArray(), MatGetArrayF90(), MatGetRowIJ() 5570 @*/ 5571 PetscErrorCode PETSCMAT_DLLEXPORT MatGetArray(Mat mat,PetscScalar *v[]) 5572 { 5573 PetscErrorCode ierr; 5574 5575 PetscFunctionBegin; 5576 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 5577 PetscValidType(mat,1); 5578 PetscValidPointer(v,2); 5579 if (!mat->ops->getarray) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 5580 ierr = MatPreallocated(mat);CHKERRQ(ierr); 5581 ierr = (*mat->ops->getarray)(mat,v);CHKERRQ(ierr); 5582 CHKMEMQ; 5583 PetscFunctionReturn(0); 5584 } 5585 5586 #undef __FUNCT__ 5587 #define __FUNCT__ "MatRestoreArray" 5588 /*@C 5589 MatRestoreArray - Restores the matrix after MatGetArray() has been called. 5590 5591 Not Collective 5592 5593 Input Parameter: 5594 + mat - the matrix 5595 - v - the location of the values 5596 5597 Fortran Note: 5598 This routine is used differently from Fortran, e.g., 5599 .vb 5600 Mat mat 5601 PetscScalar mat_array(1) 5602 PetscOffset i_mat 5603 PetscErrorCode ierr 5604 call MatGetArray(mat,mat_array,i_mat,ierr) 5605 5606 C Access first local entry in matrix; note that array is 5607 C treated as one dimensional 5608 value = mat_array(i_mat + 1) 5609 5610 [... other code ...] 5611 call MatRestoreArray(mat,mat_array,i_mat,ierr) 5612 .ve 5613 5614 See the Fortran chapter of the users manual and 5615 petsc/src/mat/examples/tests for details 5616 5617 Level: advanced 5618 5619 .seealso: MatGetArray(), MatRestoreArrayF90() 5620 @*/ 5621 PetscErrorCode PETSCMAT_DLLEXPORT MatRestoreArray(Mat mat,PetscScalar *v[]) 5622 { 5623 PetscErrorCode ierr; 5624 5625 PetscFunctionBegin; 5626 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 5627 PetscValidType(mat,1); 5628 PetscValidPointer(v,2); 5629 #if defined(PETSC_USE_DEBUG) 5630 CHKMEMQ; 5631 #endif 5632 if (!mat->ops->restorearray) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 5633 ierr = (*mat->ops->restorearray)(mat,v);CHKERRQ(ierr); 5634 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 5635 PetscFunctionReturn(0); 5636 } 5637 5638 #undef __FUNCT__ 5639 #define __FUNCT__ "MatGetSubMatrices" 5640 /*@C 5641 MatGetSubMatrices - Extracts several submatrices from a matrix. If submat 5642 points to an array of valid matrices, they may be reused to store the new 5643 submatrices. 5644 5645 Collective on Mat 5646 5647 Input Parameters: 5648 + mat - the matrix 5649 . n - the number of submatrixes to be extracted (on this processor, may be zero) 5650 . irow, icol - index sets of rows and columns to extract 5651 - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 5652 5653 Output Parameter: 5654 . submat - the array of submatrices 5655 5656 Notes: 5657 MatGetSubMatrices() can extract ONLY sequential submatrices 5658 (from both sequential and parallel matrices). Use MatGetSubMatrix() 5659 to extract a parallel submatrix. 5660 5661 When extracting submatrices from a parallel matrix, each processor can 5662 form a different submatrix by setting the rows and columns of its 5663 individual index sets according to the local submatrix desired. 5664 5665 When finished using the submatrices, the user should destroy 5666 them with MatDestroyMatrices(). 5667 5668 MAT_REUSE_MATRIX can only be used when the nonzero structure of the 5669 original matrix has not changed from that last call to MatGetSubMatrices(). 5670 5671 This routine creates the matrices in submat; you should NOT create them before 5672 calling it. It also allocates the array of matrix pointers submat. 5673 5674 For BAIJ matrices the index sets must respect the block structure, that is if they 5675 request one row/column in a block, they must request all rows/columns that are in 5676 that block. For example, if the block size is 2 you cannot request just row 0 and 5677 column 0. 5678 5679 Fortran Note: 5680 The Fortran interface is slightly different from that given below; it 5681 requires one to pass in as submat a Mat (integer) array of size at least m. 5682 5683 Level: advanced 5684 5685 Concepts: matrices^accessing submatrices 5686 Concepts: submatrices 5687 5688 .seealso: MatDestroyMatrices(), MatGetSubMatrix(), MatGetRow(), MatGetDiagonal(), MatReuse 5689 @*/ 5690 PetscErrorCode PETSCMAT_DLLEXPORT MatGetSubMatrices(Mat mat,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *submat[]) 5691 { 5692 PetscErrorCode ierr; 5693 PetscInt i; 5694 PetscTruth eq; 5695 5696 PetscFunctionBegin; 5697 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 5698 PetscValidType(mat,1); 5699 if (n) { 5700 PetscValidPointer(irow,3); 5701 PetscValidHeaderSpecific(*irow,IS_COOKIE,3); 5702 PetscValidPointer(icol,4); 5703 PetscValidHeaderSpecific(*icol,IS_COOKIE,4); 5704 } 5705 PetscValidPointer(submat,6); 5706 if (n && scall == MAT_REUSE_MATRIX) { 5707 PetscValidPointer(*submat,6); 5708 PetscValidHeaderSpecific(**submat,MAT_COOKIE,6); 5709 } 5710 if (!mat->ops->getsubmatrices) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 5711 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5712 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5713 ierr = MatPreallocated(mat);CHKERRQ(ierr); 5714 5715 ierr = PetscLogEventBegin(MAT_GetSubMatrices,mat,0,0,0);CHKERRQ(ierr); 5716 ierr = (*mat->ops->getsubmatrices)(mat,n,irow,icol,scall,submat);CHKERRQ(ierr); 5717 ierr = PetscLogEventEnd(MAT_GetSubMatrices,mat,0,0,0);CHKERRQ(ierr); 5718 for (i=0; i<n; i++) { 5719 if (mat->symmetric || mat->structurally_symmetric || mat->hermitian) { 5720 ierr = ISEqual(irow[i],icol[i],&eq);CHKERRQ(ierr); 5721 if (eq) { 5722 if (mat->symmetric){ 5723 ierr = MatSetOption((*submat)[i],MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr); 5724 } else if (mat->hermitian) { 5725 ierr = MatSetOption((*submat)[i],MAT_HERMITIAN,PETSC_TRUE);CHKERRQ(ierr); 5726 } else if (mat->structurally_symmetric) { 5727 ierr = MatSetOption((*submat)[i],MAT_STRUCTURALLY_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr); 5728 } 5729 } 5730 } 5731 } 5732 PetscFunctionReturn(0); 5733 } 5734 5735 #undef __FUNCT__ 5736 #define __FUNCT__ "MatDestroyMatrices" 5737 /*@C 5738 MatDestroyMatrices - Destroys a set of matrices obtained with MatGetSubMatrices(). 5739 5740 Collective on Mat 5741 5742 Input Parameters: 5743 + n - the number of local matrices 5744 - mat - the matrices (note that this is a pointer to the array of matrices, just to match the calling 5745 sequence of MatGetSubMatrices()) 5746 5747 Level: advanced 5748 5749 Notes: Frees not only the matrices, but also the array that contains the matrices 5750 In Fortran will not free the array. 5751 5752 .seealso: MatGetSubMatrices() 5753 @*/ 5754 PetscErrorCode PETSCMAT_DLLEXPORT MatDestroyMatrices(PetscInt n,Mat *mat[]) 5755 { 5756 PetscErrorCode ierr; 5757 PetscInt i; 5758 5759 PetscFunctionBegin; 5760 if (n < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Trying to destroy negative number of matrices %D",n); 5761 PetscValidPointer(mat,2); 5762 for (i=0; i<n; i++) { 5763 ierr = MatDestroy((*mat)[i]);CHKERRQ(ierr); 5764 } 5765 /* memory is allocated even if n = 0 */ 5766 ierr = PetscFree(*mat);CHKERRQ(ierr); 5767 PetscFunctionReturn(0); 5768 } 5769 5770 #undef __FUNCT__ 5771 #define __FUNCT__ "MatGetSeqNonzeroStructure" 5772 /*@C 5773 MatGetSeqNonzeroStructure - Extracts the sequential nonzero structure from a matrix. 5774 5775 Collective on Mat 5776 5777 Input Parameters: 5778 . mat - the matrix 5779 5780 Output Parameter: 5781 . matstruct - the sequential matrix with the nonzero structure of mat 5782 5783 Level: intermediate 5784 5785 .seealso: MatDestroySeqNonzeroStructure(), MatGetSubMatrices(), MatDestroyMatrices() 5786 @*/ 5787 PetscErrorCode PETSCMAT_DLLEXPORT MatGetSeqNonzeroStructure(Mat mat,Mat *matstruct) 5788 { 5789 PetscErrorCode ierr; 5790 5791 PetscFunctionBegin; 5792 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 5793 PetscValidPointer(matstruct,2); 5794 5795 PetscValidType(mat,1); 5796 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5797 ierr = MatPreallocated(mat);CHKERRQ(ierr); 5798 5799 if (!mat->ops->getseqnonzerostructure) SETERRQ1(PETSC_ERR_SUP,"Not for matrix type %s\n",((PetscObject)mat)->type_name); 5800 ierr = PetscLogEventBegin(MAT_GetSeqNonzeroStructure,mat,0,0,0);CHKERRQ(ierr); 5801 ierr = (*mat->ops->getseqnonzerostructure)(mat,matstruct);CHKERRQ(ierr); 5802 ierr = PetscLogEventEnd(MAT_GetSeqNonzeroStructure,mat,0,0,0);CHKERRQ(ierr); 5803 PetscFunctionReturn(0); 5804 } 5805 5806 #undef __FUNCT__ 5807 #define __FUNCT__ "MatDestroySeqNonzeroStructure" 5808 /*@C 5809 MatDestroySeqNonzeroStructure - Destroys matrix obtained with MatGetSeqNonzeroStructure(). 5810 5811 Collective on Mat 5812 5813 Input Parameters: 5814 . mat - the matrix (note that this is a pointer to the array of matrices, just to match the calling 5815 sequence of MatGetSequentialNonzeroStructure()) 5816 5817 Level: advanced 5818 5819 Notes: Frees not only the matrices, but also the array that contains the matrices 5820 5821 .seealso: MatGetSeqNonzeroStructure() 5822 @*/ 5823 PetscErrorCode PETSCMAT_DLLEXPORT MatDestroySeqNonzeroStructure(Mat *mat) 5824 { 5825 PetscErrorCode ierr; 5826 5827 PetscFunctionBegin; 5828 PetscValidPointer(mat,1); 5829 ierr = MatDestroy(*mat);CHKERRQ(ierr); 5830 PetscFunctionReturn(0); 5831 } 5832 5833 #undef __FUNCT__ 5834 #define __FUNCT__ "MatIncreaseOverlap" 5835 /*@ 5836 MatIncreaseOverlap - Given a set of submatrices indicated by index sets, 5837 replaces the index sets by larger ones that represent submatrices with 5838 additional overlap. 5839 5840 Collective on Mat 5841 5842 Input Parameters: 5843 + mat - the matrix 5844 . n - the number of index sets 5845 . is - the array of index sets (these index sets will changed during the call) 5846 - ov - the additional overlap requested 5847 5848 Level: developer 5849 5850 Concepts: overlap 5851 Concepts: ASM^computing overlap 5852 5853 .seealso: MatGetSubMatrices() 5854 @*/ 5855 PetscErrorCode PETSCMAT_DLLEXPORT MatIncreaseOverlap(Mat mat,PetscInt n,IS is[],PetscInt ov) 5856 { 5857 PetscErrorCode ierr; 5858 5859 PetscFunctionBegin; 5860 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 5861 PetscValidType(mat,1); 5862 if (n < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Must have one or more domains, you have %D",n); 5863 if (n) { 5864 PetscValidPointer(is,3); 5865 PetscValidHeaderSpecific(*is,IS_COOKIE,3); 5866 } 5867 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5868 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5869 ierr = MatPreallocated(mat);CHKERRQ(ierr); 5870 5871 if (!ov) PetscFunctionReturn(0); 5872 if (!mat->ops->increaseoverlap) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 5873 ierr = PetscLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr); 5874 ierr = (*mat->ops->increaseoverlap)(mat,n,is,ov);CHKERRQ(ierr); 5875 ierr = PetscLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr); 5876 PetscFunctionReturn(0); 5877 } 5878 5879 #undef __FUNCT__ 5880 #define __FUNCT__ "MatGetBlockSize" 5881 /*@ 5882 MatGetBlockSize - Returns the matrix block size; useful especially for the 5883 block row and block diagonal formats. 5884 5885 Not Collective 5886 5887 Input Parameter: 5888 . mat - the matrix 5889 5890 Output Parameter: 5891 . bs - block size 5892 5893 Notes: 5894 Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ 5895 5896 Level: intermediate 5897 5898 Concepts: matrices^block size 5899 5900 .seealso: MatCreateSeqBAIJ(), MatCreateMPIBAIJ() 5901 @*/ 5902 PetscErrorCode PETSCMAT_DLLEXPORT MatGetBlockSize(Mat mat,PetscInt *bs) 5903 { 5904 PetscErrorCode ierr; 5905 5906 PetscFunctionBegin; 5907 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 5908 PetscValidType(mat,1); 5909 PetscValidIntPointer(bs,2); 5910 ierr = MatPreallocated(mat);CHKERRQ(ierr); 5911 *bs = mat->rmap->bs; 5912 PetscFunctionReturn(0); 5913 } 5914 5915 #undef __FUNCT__ 5916 #define __FUNCT__ "MatSetBlockSize" 5917 /*@ 5918 MatSetBlockSize - Sets the matrix block size; for many matrix types you 5919 cannot use this and MUST set the blocksize when you preallocate the matrix 5920 5921 Collective on Mat 5922 5923 Input Parameters: 5924 + mat - the matrix 5925 - bs - block size 5926 5927 Notes: 5928 For BAIJ matrices, this just checks that the block size agrees with the BAIJ size, 5929 it is not possible to change BAIJ block sizes after preallocation. 5930 5931 Level: intermediate 5932 5933 Concepts: matrices^block size 5934 5935 .seealso: MatCreateSeqBAIJ(), MatCreateMPIBAIJ(), MatGetBlockSize() 5936 @*/ 5937 PetscErrorCode PETSCMAT_DLLEXPORT MatSetBlockSize(Mat mat,PetscInt bs) 5938 { 5939 PetscErrorCode ierr; 5940 5941 PetscFunctionBegin; 5942 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 5943 PetscValidType(mat,1); 5944 ierr = MatPreallocated(mat);CHKERRQ(ierr); 5945 if (bs < 1) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Block size %d, must be positive",bs); 5946 if (mat->ops->setblocksize) { 5947 ierr = (*mat->ops->setblocksize)(mat,bs);CHKERRQ(ierr); 5948 } else { 5949 SETERRQ1(PETSC_ERR_ARG_INCOMP,"Cannot set the blocksize for matrix type %s",((PetscObject)mat)->type_name); 5950 } 5951 PetscFunctionReturn(0); 5952 } 5953 5954 #undef __FUNCT__ 5955 #define __FUNCT__ "MatGetRowIJ" 5956 /*@C 5957 MatGetRowIJ - Returns the compressed row storage i and j indices for sequential matrices. 5958 5959 Collective on Mat 5960 5961 Input Parameters: 5962 + mat - the matrix 5963 . shift - 0 or 1 indicating we want the indices starting at 0 or 1 5964 . symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 5965 symmetrized 5966 - inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the 5967 inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is 5968 always used. 5969 5970 Output Parameters: 5971 + n - number of rows in the (possibly compressed) matrix 5972 . ia - the row pointers [of length n+1] 5973 . ja - the column indices 5974 - done - indicates if the routine actually worked and returned appropriate ia[] and ja[] arrays; callers 5975 are responsible for handling the case when done == PETSC_FALSE and ia and ja are not set 5976 5977 Level: developer 5978 5979 Notes: You CANNOT change any of the ia[] or ja[] values. 5980 5981 Use MatRestoreRowIJ() when you are finished accessing the ia[] and ja[] values 5982 5983 Fortran Node 5984 5985 In Fortran use 5986 $ PetscInt ia(1), ja(1) 5987 $ PetscOffset iia, jja 5988 $ call MatGetRowIJ(mat,shift,symmetric,inodecompressed,n,ia,iia,ja,jja,done,ierr) 5989 $ 5990 $ or 5991 $ 5992 $ PetscScalar, pointer :: xx_v(:) 5993 $ call MatGetRowIJF90(mat,shift,symmetric,inodecompressed,n,ia,ja,done,ierr) 5994 5995 5996 Acess the ith and jth entries via ia(iia + i) and ja(jja + j) 5997 5998 .seealso: MatGetColumnIJ(), MatRestoreRowIJ(), MatGetArray() 5999 @*/ 6000 PetscErrorCode PETSCMAT_DLLEXPORT MatGetRowIJ(Mat mat,PetscInt shift,PetscTruth symmetric,PetscTruth inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt* ja[],PetscTruth *done) 6001 { 6002 PetscErrorCode ierr; 6003 6004 PetscFunctionBegin; 6005 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 6006 PetscValidType(mat,1); 6007 PetscValidIntPointer(n,4); 6008 if (ia) PetscValidIntPointer(ia,5); 6009 if (ja) PetscValidIntPointer(ja,6); 6010 PetscValidIntPointer(done,7); 6011 ierr = MatPreallocated(mat);CHKERRQ(ierr); 6012 if (!mat->ops->getrowij) *done = PETSC_FALSE; 6013 else { 6014 *done = PETSC_TRUE; 6015 ierr = PetscLogEventBegin(MAT_GetRowIJ,mat,0,0,0);CHKERRQ(ierr); 6016 ierr = (*mat->ops->getrowij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr); 6017 ierr = PetscLogEventEnd(MAT_GetRowIJ,mat,0,0,0);CHKERRQ(ierr); 6018 } 6019 PetscFunctionReturn(0); 6020 } 6021 6022 #undef __FUNCT__ 6023 #define __FUNCT__ "MatGetColumnIJ" 6024 /*@C 6025 MatGetColumnIJ - Returns the compressed column storage i and j indices for sequential matrices. 6026 6027 Collective on Mat 6028 6029 Input Parameters: 6030 + mat - the matrix 6031 . shift - 1 or zero indicating we want the indices starting at 0 or 1 6032 . symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 6033 symmetrized 6034 - inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the 6035 inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is 6036 always used. 6037 6038 Output Parameters: 6039 + n - number of columns in the (possibly compressed) matrix 6040 . ia - the column pointers 6041 . ja - the row indices 6042 - done - PETSC_TRUE or PETSC_FALSE, indicating whether the values have been returned 6043 6044 Level: developer 6045 6046 .seealso: MatGetRowIJ(), MatRestoreColumnIJ() 6047 @*/ 6048 PetscErrorCode PETSCMAT_DLLEXPORT MatGetColumnIJ(Mat mat,PetscInt shift,PetscTruth symmetric,PetscTruth inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt* ja[],PetscTruth *done) 6049 { 6050 PetscErrorCode ierr; 6051 6052 PetscFunctionBegin; 6053 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 6054 PetscValidType(mat,1); 6055 PetscValidIntPointer(n,4); 6056 if (ia) PetscValidIntPointer(ia,5); 6057 if (ja) PetscValidIntPointer(ja,6); 6058 PetscValidIntPointer(done,7); 6059 ierr = MatPreallocated(mat);CHKERRQ(ierr); 6060 if (!mat->ops->getcolumnij) *done = PETSC_FALSE; 6061 else { 6062 *done = PETSC_TRUE; 6063 ierr = (*mat->ops->getcolumnij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr); 6064 } 6065 PetscFunctionReturn(0); 6066 } 6067 6068 #undef __FUNCT__ 6069 #define __FUNCT__ "MatRestoreRowIJ" 6070 /*@C 6071 MatRestoreRowIJ - Call after you are completed with the ia,ja indices obtained with 6072 MatGetRowIJ(). 6073 6074 Collective on Mat 6075 6076 Input Parameters: 6077 + mat - the matrix 6078 . shift - 1 or zero indicating we want the indices starting at 0 or 1 6079 . symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 6080 symmetrized 6081 - inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the 6082 inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is 6083 always used. 6084 6085 Output Parameters: 6086 + n - size of (possibly compressed) matrix 6087 . ia - the row pointers 6088 . ja - the column indices 6089 - done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned 6090 6091 Level: developer 6092 6093 .seealso: MatGetRowIJ(), MatRestoreColumnIJ() 6094 @*/ 6095 PetscErrorCode PETSCMAT_DLLEXPORT MatRestoreRowIJ(Mat mat,PetscInt shift,PetscTruth symmetric,PetscTruth inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt* ja[],PetscTruth *done) 6096 { 6097 PetscErrorCode ierr; 6098 6099 PetscFunctionBegin; 6100 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 6101 PetscValidType(mat,1); 6102 if (ia) PetscValidIntPointer(ia,5); 6103 if (ja) PetscValidIntPointer(ja,6); 6104 PetscValidIntPointer(done,7); 6105 ierr = MatPreallocated(mat);CHKERRQ(ierr); 6106 6107 if (!mat->ops->restorerowij) *done = PETSC_FALSE; 6108 else { 6109 *done = PETSC_TRUE; 6110 ierr = (*mat->ops->restorerowij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr); 6111 } 6112 PetscFunctionReturn(0); 6113 } 6114 6115 #undef __FUNCT__ 6116 #define __FUNCT__ "MatRestoreColumnIJ" 6117 /*@C 6118 MatRestoreColumnIJ - Call after you are completed with the ia,ja indices obtained with 6119 MatGetColumnIJ(). 6120 6121 Collective on Mat 6122 6123 Input Parameters: 6124 + mat - the matrix 6125 . shift - 1 or zero indicating we want the indices starting at 0 or 1 6126 - symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 6127 symmetrized 6128 - inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the 6129 inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is 6130 always used. 6131 6132 Output Parameters: 6133 + n - size of (possibly compressed) matrix 6134 . ia - the column pointers 6135 . ja - the row indices 6136 - done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned 6137 6138 Level: developer 6139 6140 .seealso: MatGetColumnIJ(), MatRestoreRowIJ() 6141 @*/ 6142 PetscErrorCode PETSCMAT_DLLEXPORT MatRestoreColumnIJ(Mat mat,PetscInt shift,PetscTruth symmetric,PetscTruth inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt* ja[],PetscTruth *done) 6143 { 6144 PetscErrorCode ierr; 6145 6146 PetscFunctionBegin; 6147 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 6148 PetscValidType(mat,1); 6149 if (ia) PetscValidIntPointer(ia,5); 6150 if (ja) PetscValidIntPointer(ja,6); 6151 PetscValidIntPointer(done,7); 6152 ierr = MatPreallocated(mat);CHKERRQ(ierr); 6153 6154 if (!mat->ops->restorecolumnij) *done = PETSC_FALSE; 6155 else { 6156 *done = PETSC_TRUE; 6157 ierr = (*mat->ops->restorecolumnij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr); 6158 } 6159 PetscFunctionReturn(0); 6160 } 6161 6162 #undef __FUNCT__ 6163 #define __FUNCT__ "MatColoringPatch" 6164 /*@C 6165 MatColoringPatch -Used inside matrix coloring routines that 6166 use MatGetRowIJ() and/or MatGetColumnIJ(). 6167 6168 Collective on Mat 6169 6170 Input Parameters: 6171 + mat - the matrix 6172 . ncolors - max color value 6173 . n - number of entries in colorarray 6174 - colorarray - array indicating color for each column 6175 6176 Output Parameters: 6177 . iscoloring - coloring generated using colorarray information 6178 6179 Level: developer 6180 6181 .seealso: MatGetRowIJ(), MatGetColumnIJ() 6182 6183 @*/ 6184 PetscErrorCode PETSCMAT_DLLEXPORT MatColoringPatch(Mat mat,PetscInt ncolors,PetscInt n,ISColoringValue colorarray[],ISColoring *iscoloring) 6185 { 6186 PetscErrorCode ierr; 6187 6188 PetscFunctionBegin; 6189 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 6190 PetscValidType(mat,1); 6191 PetscValidIntPointer(colorarray,4); 6192 PetscValidPointer(iscoloring,5); 6193 ierr = MatPreallocated(mat);CHKERRQ(ierr); 6194 6195 if (!mat->ops->coloringpatch){ 6196 ierr = ISColoringCreate(((PetscObject)mat)->comm,ncolors,n,colorarray,iscoloring);CHKERRQ(ierr); 6197 } else { 6198 ierr = (*mat->ops->coloringpatch)(mat,ncolors,n,colorarray,iscoloring);CHKERRQ(ierr); 6199 } 6200 PetscFunctionReturn(0); 6201 } 6202 6203 6204 #undef __FUNCT__ 6205 #define __FUNCT__ "MatSetUnfactored" 6206 /*@ 6207 MatSetUnfactored - Resets a factored matrix to be treated as unfactored. 6208 6209 Collective on Mat 6210 6211 Input Parameter: 6212 . mat - the factored matrix to be reset 6213 6214 Notes: 6215 This routine should be used only with factored matrices formed by in-place 6216 factorization via ILU(0) (or by in-place LU factorization for the MATSEQDENSE 6217 format). This option can save memory, for example, when solving nonlinear 6218 systems with a matrix-free Newton-Krylov method and a matrix-based, in-place 6219 ILU(0) preconditioner. 6220 6221 Note that one can specify in-place ILU(0) factorization by calling 6222 .vb 6223 PCType(pc,PCILU); 6224 PCFactorSeUseInPlace(pc); 6225 .ve 6226 or by using the options -pc_type ilu -pc_factor_in_place 6227 6228 In-place factorization ILU(0) can also be used as a local 6229 solver for the blocks within the block Jacobi or additive Schwarz 6230 methods (runtime option: -sub_pc_factor_in_place). See the discussion 6231 of these preconditioners in the users manual for details on setting 6232 local solver options. 6233 6234 Most users should employ the simplified KSP interface for linear solvers 6235 instead of working directly with matrix algebra routines such as this. 6236 See, e.g., KSPCreate(). 6237 6238 Level: developer 6239 6240 .seealso: PCFactorSetUseInPlace() 6241 6242 Concepts: matrices^unfactored 6243 6244 @*/ 6245 PetscErrorCode PETSCMAT_DLLEXPORT MatSetUnfactored(Mat mat) 6246 { 6247 PetscErrorCode ierr; 6248 6249 PetscFunctionBegin; 6250 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 6251 PetscValidType(mat,1); 6252 ierr = MatPreallocated(mat);CHKERRQ(ierr); 6253 mat->factor = MAT_FACTOR_NONE; 6254 if (!mat->ops->setunfactored) PetscFunctionReturn(0); 6255 ierr = (*mat->ops->setunfactored)(mat);CHKERRQ(ierr); 6256 PetscFunctionReturn(0); 6257 } 6258 6259 /*MC 6260 MatGetArrayF90 - Accesses a matrix array from Fortran90. 6261 6262 Synopsis: 6263 MatGetArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr) 6264 6265 Not collective 6266 6267 Input Parameter: 6268 . x - matrix 6269 6270 Output Parameters: 6271 + xx_v - the Fortran90 pointer to the array 6272 - ierr - error code 6273 6274 Example of Usage: 6275 .vb 6276 PetscScalar, pointer xx_v(:) 6277 .... 6278 call MatGetArrayF90(x,xx_v,ierr) 6279 a = xx_v(3) 6280 call MatRestoreArrayF90(x,xx_v,ierr) 6281 .ve 6282 6283 Notes: 6284 Not yet supported for all F90 compilers 6285 6286 Level: advanced 6287 6288 .seealso: MatRestoreArrayF90(), MatGetArray(), MatRestoreArray() 6289 6290 Concepts: matrices^accessing array 6291 6292 M*/ 6293 6294 /*MC 6295 MatRestoreArrayF90 - Restores a matrix array that has been 6296 accessed with MatGetArrayF90(). 6297 6298 Synopsis: 6299 MatRestoreArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr) 6300 6301 Not collective 6302 6303 Input Parameters: 6304 + x - matrix 6305 - xx_v - the Fortran90 pointer to the array 6306 6307 Output Parameter: 6308 . ierr - error code 6309 6310 Example of Usage: 6311 .vb 6312 PetscScalar, pointer xx_v(:) 6313 .... 6314 call MatGetArrayF90(x,xx_v,ierr) 6315 a = xx_v(3) 6316 call MatRestoreArrayF90(x,xx_v,ierr) 6317 .ve 6318 6319 Notes: 6320 Not yet supported for all F90 compilers 6321 6322 Level: advanced 6323 6324 .seealso: MatGetArrayF90(), MatGetArray(), MatRestoreArray() 6325 6326 M*/ 6327 6328 6329 #undef __FUNCT__ 6330 #define __FUNCT__ "MatGetSubMatrix" 6331 /*@ 6332 MatGetSubMatrix - Gets a single submatrix on the same number of processors 6333 as the original matrix. 6334 6335 Collective on Mat 6336 6337 Input Parameters: 6338 + mat - the original matrix 6339 . isrow - parallel IS containing the rows this processor should obtain 6340 . iscol - parallel IS containing all columns you wish to keep. Each process should list the columns that will be in IT's "diagonal part" in the new matrix. 6341 - cll - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 6342 6343 Output Parameter: 6344 . newmat - the new submatrix, of the same type as the old 6345 6346 Level: advanced 6347 6348 Notes: 6349 The submatrix will be able to be multiplied with vectors using the same layout as iscol. 6350 6351 The rows in isrow will be sorted into the same order as the original matrix on each process. 6352 6353 The first time this is called you should use a cll of MAT_INITIAL_MATRIX, 6354 the MatGetSubMatrix() routine will create the newmat for you. Any additional calls 6355 to this routine with a mat of the same nonzero structure and with a call of MAT_REUSE_MATRIX 6356 will reuse the matrix generated the first time. You should call MatDestroy() on newmat when 6357 you are finished using it. 6358 6359 The communicator of the newly obtained matrix is ALWAYS the same as the communicator of 6360 the input matrix. 6361 6362 If iscol is PETSC_NULL then all columns are obtained (not supported in Fortran). 6363 6364 Example usage: 6365 Consider the following 8x8 matrix with 34 non-zero values, that is 6366 assembled across 3 processors. Lets assume that proc0 owns 3 rows, 6367 proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown 6368 as follows: 6369 6370 .vb 6371 1 2 0 | 0 3 0 | 0 4 6372 Proc0 0 5 6 | 7 0 0 | 8 0 6373 9 0 10 | 11 0 0 | 12 0 6374 ------------------------------------- 6375 13 0 14 | 15 16 17 | 0 0 6376 Proc1 0 18 0 | 19 20 21 | 0 0 6377 0 0 0 | 22 23 0 | 24 0 6378 ------------------------------------- 6379 Proc2 25 26 27 | 0 0 28 | 29 0 6380 30 0 0 | 31 32 33 | 0 34 6381 .ve 6382 6383 Suppose isrow = [0 1 | 4 | 5 6] and iscol = [1 2 | 3 4 5 | 6]. The resulting submatrix is 6384 6385 .vb 6386 2 0 | 0 3 0 | 0 6387 Proc0 5 6 | 7 0 0 | 8 6388 ------------------------------- 6389 Proc1 18 0 | 19 20 21 | 0 6390 ------------------------------- 6391 Proc2 26 27 | 0 0 28 | 29 6392 0 0 | 31 32 33 | 0 6393 .ve 6394 6395 6396 Concepts: matrices^submatrices 6397 6398 .seealso: MatGetSubMatrices() 6399 @*/ 6400 PetscErrorCode PETSCMAT_DLLEXPORT MatGetSubMatrix(Mat mat,IS isrow,IS iscol,MatReuse cll,Mat *newmat) 6401 { 6402 PetscErrorCode ierr; 6403 PetscMPIInt size; 6404 Mat *local; 6405 IS iscoltmp; 6406 6407 PetscFunctionBegin; 6408 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 6409 PetscValidHeaderSpecific(isrow,IS_COOKIE,2); 6410 if (iscol) PetscValidHeaderSpecific(iscol,IS_COOKIE,3); 6411 PetscValidPointer(newmat,5); 6412 if (cll == MAT_REUSE_MATRIX) PetscValidHeaderSpecific(*newmat,MAT_COOKIE,5); 6413 PetscValidType(mat,1); 6414 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6415 ierr = MatPreallocated(mat);CHKERRQ(ierr); 6416 ierr = MPI_Comm_size(((PetscObject)mat)->comm,&size);CHKERRQ(ierr); 6417 6418 if (!iscol) { 6419 ierr = ISCreateStride(((PetscObject)mat)->comm,mat->cmap->n,mat->cmap->rstart,1,&iscoltmp);CHKERRQ(ierr); 6420 } else { 6421 iscoltmp = iscol; 6422 } 6423 6424 /* if original matrix is on just one processor then use submatrix generated */ 6425 if (mat->ops->getsubmatrices && !mat->ops->getsubmatrix && size == 1 && cll == MAT_REUSE_MATRIX) { 6426 ierr = MatGetSubMatrices(mat,1,&isrow,&iscoltmp,MAT_REUSE_MATRIX,&newmat);CHKERRQ(ierr); 6427 if (!iscol) {ierr = ISDestroy(iscoltmp);CHKERRQ(ierr);} 6428 PetscFunctionReturn(0); 6429 } else if (mat->ops->getsubmatrices && !mat->ops->getsubmatrix && size == 1) { 6430 ierr = MatGetSubMatrices(mat,1,&isrow,&iscoltmp,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr); 6431 *newmat = *local; 6432 ierr = PetscFree(local);CHKERRQ(ierr); 6433 if (!iscol) {ierr = ISDestroy(iscoltmp);CHKERRQ(ierr);} 6434 PetscFunctionReturn(0); 6435 } else if (!mat->ops->getsubmatrix) { 6436 /* Create a new matrix type that implements the operation using the full matrix */ 6437 switch (cll) { 6438 case MAT_INITIAL_MATRIX: 6439 ierr = MatCreateSubMatrix(mat,isrow,iscoltmp,newmat);CHKERRQ(ierr); 6440 break; 6441 case MAT_REUSE_MATRIX: 6442 ierr = MatSubMatrixUpdate(*newmat,mat,isrow,iscoltmp);CHKERRQ(ierr); 6443 break; 6444 default: SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Invalid MatReuse, must be either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX"); 6445 } 6446 if (!iscol) {ierr = ISDestroy(iscoltmp);CHKERRQ(ierr);} 6447 PetscFunctionReturn(0); 6448 } 6449 6450 if (!mat->ops->getsubmatrix) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 6451 ierr = (*mat->ops->getsubmatrix)(mat,isrow,iscoltmp,cll,newmat);CHKERRQ(ierr); 6452 if (!iscol) {ierr = ISDestroy(iscoltmp);CHKERRQ(ierr);} 6453 ierr = PetscObjectStateIncrease((PetscObject)*newmat);CHKERRQ(ierr); 6454 PetscFunctionReturn(0); 6455 } 6456 6457 #undef __FUNCT__ 6458 #define __FUNCT__ "MatStashSetInitialSize" 6459 /*@ 6460 MatStashSetInitialSize - sets the sizes of the matrix stash, that is 6461 used during the assembly process to store values that belong to 6462 other processors. 6463 6464 Not Collective 6465 6466 Input Parameters: 6467 + mat - the matrix 6468 . size - the initial size of the stash. 6469 - bsize - the initial size of the block-stash(if used). 6470 6471 Options Database Keys: 6472 + -matstash_initial_size <size> or <size0,size1,...sizep-1> 6473 - -matstash_block_initial_size <bsize> or <bsize0,bsize1,...bsizep-1> 6474 6475 Level: intermediate 6476 6477 Notes: 6478 The block-stash is used for values set with MatSetValuesBlocked() while 6479 the stash is used for values set with MatSetValues() 6480 6481 Run with the option -info and look for output of the form 6482 MatAssemblyBegin_MPIXXX:Stash has MM entries, uses nn mallocs. 6483 to determine the appropriate value, MM, to use for size and 6484 MatAssemblyBegin_MPIXXX:Block-Stash has BMM entries, uses nn mallocs. 6485 to determine the value, BMM to use for bsize 6486 6487 Concepts: stash^setting matrix size 6488 Concepts: matrices^stash 6489 6490 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), Mat, MatStashGetInfo() 6491 6492 @*/ 6493 PetscErrorCode PETSCMAT_DLLEXPORT MatStashSetInitialSize(Mat mat,PetscInt size, PetscInt bsize) 6494 { 6495 PetscErrorCode ierr; 6496 6497 PetscFunctionBegin; 6498 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 6499 PetscValidType(mat,1); 6500 ierr = MatStashSetInitialSize_Private(&mat->stash,size);CHKERRQ(ierr); 6501 ierr = MatStashSetInitialSize_Private(&mat->bstash,bsize);CHKERRQ(ierr); 6502 PetscFunctionReturn(0); 6503 } 6504 6505 #undef __FUNCT__ 6506 #define __FUNCT__ "MatInterpolateAdd" 6507 /*@ 6508 MatInterpolateAdd - w = y + A*x or A'*x depending on the shape of 6509 the matrix 6510 6511 Collective on Mat 6512 6513 Input Parameters: 6514 + mat - the matrix 6515 . x,y - the vectors 6516 - w - where the result is stored 6517 6518 Level: intermediate 6519 6520 Notes: 6521 w may be the same vector as y. 6522 6523 This allows one to use either the restriction or interpolation (its transpose) 6524 matrix to do the interpolation 6525 6526 Concepts: interpolation 6527 6528 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict() 6529 6530 @*/ 6531 PetscErrorCode PETSCMAT_DLLEXPORT MatInterpolateAdd(Mat A,Vec x,Vec y,Vec w) 6532 { 6533 PetscErrorCode ierr; 6534 PetscInt M,N; 6535 6536 PetscFunctionBegin; 6537 PetscValidHeaderSpecific(A,MAT_COOKIE,1); 6538 PetscValidHeaderSpecific(x,VEC_COOKIE,2); 6539 PetscValidHeaderSpecific(y,VEC_COOKIE,3); 6540 PetscValidHeaderSpecific(w,VEC_COOKIE,4); 6541 PetscValidType(A,1); 6542 ierr = MatPreallocated(A);CHKERRQ(ierr); 6543 ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr); 6544 if (N > M) { 6545 ierr = MatMultTransposeAdd(A,x,y,w);CHKERRQ(ierr); 6546 } else { 6547 ierr = MatMultAdd(A,x,y,w);CHKERRQ(ierr); 6548 } 6549 PetscFunctionReturn(0); 6550 } 6551 6552 #undef __FUNCT__ 6553 #define __FUNCT__ "MatInterpolate" 6554 /*@ 6555 MatInterpolate - y = A*x or A'*x depending on the shape of 6556 the matrix 6557 6558 Collective on Mat 6559 6560 Input Parameters: 6561 + mat - the matrix 6562 - x,y - the vectors 6563 6564 Level: intermediate 6565 6566 Notes: 6567 This allows one to use either the restriction or interpolation (its transpose) 6568 matrix to do the interpolation 6569 6570 Concepts: matrices^interpolation 6571 6572 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict() 6573 6574 @*/ 6575 PetscErrorCode PETSCMAT_DLLEXPORT MatInterpolate(Mat A,Vec x,Vec y) 6576 { 6577 PetscErrorCode ierr; 6578 PetscInt M,N; 6579 6580 PetscFunctionBegin; 6581 PetscValidHeaderSpecific(A,MAT_COOKIE,1); 6582 PetscValidHeaderSpecific(x,VEC_COOKIE,2); 6583 PetscValidHeaderSpecific(y,VEC_COOKIE,3); 6584 PetscValidType(A,1); 6585 ierr = MatPreallocated(A);CHKERRQ(ierr); 6586 ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr); 6587 if (N > M) { 6588 ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr); 6589 } else { 6590 ierr = MatMult(A,x,y);CHKERRQ(ierr); 6591 } 6592 PetscFunctionReturn(0); 6593 } 6594 6595 #undef __FUNCT__ 6596 #define __FUNCT__ "MatRestrict" 6597 /*@ 6598 MatRestrict - y = A*x or A'*x 6599 6600 Collective on Mat 6601 6602 Input Parameters: 6603 + mat - the matrix 6604 - x,y - the vectors 6605 6606 Level: intermediate 6607 6608 Notes: 6609 This allows one to use either the restriction or interpolation (its transpose) 6610 matrix to do the restriction 6611 6612 Concepts: matrices^restriction 6613 6614 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatInterpolate() 6615 6616 @*/ 6617 PetscErrorCode PETSCMAT_DLLEXPORT MatRestrict(Mat A,Vec x,Vec y) 6618 { 6619 PetscErrorCode ierr; 6620 PetscInt M,N; 6621 6622 PetscFunctionBegin; 6623 PetscValidHeaderSpecific(A,MAT_COOKIE,1); 6624 PetscValidHeaderSpecific(x,VEC_COOKIE,2); 6625 PetscValidHeaderSpecific(y,VEC_COOKIE,3); 6626 PetscValidType(A,1); 6627 ierr = MatPreallocated(A);CHKERRQ(ierr); 6628 6629 ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr); 6630 if (N > M) { 6631 ierr = MatMult(A,x,y);CHKERRQ(ierr); 6632 } else { 6633 ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr); 6634 } 6635 PetscFunctionReturn(0); 6636 } 6637 6638 #undef __FUNCT__ 6639 #define __FUNCT__ "MatNullSpaceAttach" 6640 /*@ 6641 MatNullSpaceAttach - attaches a null space to a matrix. 6642 This null space will be removed from the resulting vector whenever 6643 MatMult() is called 6644 6645 Collective on Mat 6646 6647 Input Parameters: 6648 + mat - the matrix 6649 - nullsp - the null space object 6650 6651 Level: developer 6652 6653 Notes: 6654 Overwrites any previous null space that may have been attached 6655 6656 Concepts: null space^attaching to matrix 6657 6658 .seealso: MatCreate(), MatNullSpaceCreate() 6659 @*/ 6660 PetscErrorCode PETSCMAT_DLLEXPORT MatNullSpaceAttach(Mat mat,MatNullSpace nullsp) 6661 { 6662 PetscErrorCode ierr; 6663 6664 PetscFunctionBegin; 6665 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 6666 PetscValidType(mat,1); 6667 PetscValidHeaderSpecific(nullsp,MAT_NULLSPACE_COOKIE,2); 6668 ierr = MatPreallocated(mat);CHKERRQ(ierr); 6669 ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr); 6670 if (mat->nullsp) { ierr = MatNullSpaceDestroy(mat->nullsp);CHKERRQ(ierr); } 6671 mat->nullsp = nullsp; 6672 PetscFunctionReturn(0); 6673 } 6674 6675 #undef __FUNCT__ 6676 #define __FUNCT__ "MatICCFactor" 6677 /*@C 6678 MatICCFactor - Performs in-place incomplete Cholesky factorization of matrix. 6679 6680 Collective on Mat 6681 6682 Input Parameters: 6683 + mat - the matrix 6684 . row - row/column permutation 6685 . fill - expected fill factor >= 1.0 6686 - level - level of fill, for ICC(k) 6687 6688 Notes: 6689 Probably really in-place only when level of fill is zero, otherwise allocates 6690 new space to store factored matrix and deletes previous memory. 6691 6692 Most users should employ the simplified KSP interface for linear solvers 6693 instead of working directly with matrix algebra routines such as this. 6694 See, e.g., KSPCreate(). 6695 6696 Level: developer 6697 6698 Concepts: matrices^incomplete Cholesky factorization 6699 Concepts: Cholesky factorization 6700 6701 .seealso: MatICCFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor() 6702 6703 Developer Note: fortran interface is not autogenerated as the f90 6704 interface defintion cannot be generated correctly [due to MatFactorInfo] 6705 6706 @*/ 6707 PetscErrorCode PETSCMAT_DLLEXPORT MatICCFactor(Mat mat,IS row,const MatFactorInfo* info) 6708 { 6709 PetscErrorCode ierr; 6710 6711 PetscFunctionBegin; 6712 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 6713 PetscValidType(mat,1); 6714 if (row) PetscValidHeaderSpecific(row,IS_COOKIE,2); 6715 PetscValidPointer(info,3); 6716 if (mat->rmap->N != mat->cmap->N) SETERRQ(PETSC_ERR_ARG_WRONG,"matrix must be square"); 6717 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 6718 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6719 if (!mat->ops->iccfactor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 6720 ierr = MatPreallocated(mat);CHKERRQ(ierr); 6721 ierr = (*mat->ops->iccfactor)(mat,row,info);CHKERRQ(ierr); 6722 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 6723 PetscFunctionReturn(0); 6724 } 6725 6726 #undef __FUNCT__ 6727 #define __FUNCT__ "MatSetValuesAdic" 6728 /*@ 6729 MatSetValuesAdic - Sets values computed with ADIC automatic differentiation into a matrix. 6730 6731 Not Collective 6732 6733 Input Parameters: 6734 + mat - the matrix 6735 - v - the values compute with ADIC 6736 6737 Level: developer 6738 6739 Notes: 6740 Must call MatSetColoring() before using this routine. Also this matrix must already 6741 have its nonzero pattern determined. 6742 6743 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(), 6744 MatSetValues(), MatSetColoring(), MatSetValuesAdifor() 6745 @*/ 6746 PetscErrorCode PETSCMAT_DLLEXPORT MatSetValuesAdic(Mat mat,void *v) 6747 { 6748 PetscErrorCode ierr; 6749 6750 PetscFunctionBegin; 6751 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 6752 PetscValidType(mat,1); 6753 PetscValidPointer(mat,2); 6754 6755 if (!mat->assembled) { 6756 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled"); 6757 } 6758 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 6759 if (!mat->ops->setvaluesadic) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 6760 ierr = (*mat->ops->setvaluesadic)(mat,v);CHKERRQ(ierr); 6761 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 6762 ierr = MatView_Private(mat);CHKERRQ(ierr); 6763 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 6764 PetscFunctionReturn(0); 6765 } 6766 6767 6768 #undef __FUNCT__ 6769 #define __FUNCT__ "MatSetColoring" 6770 /*@ 6771 MatSetColoring - Sets a coloring used by calls to MatSetValuesAdic() 6772 6773 Not Collective 6774 6775 Input Parameters: 6776 + mat - the matrix 6777 - coloring - the coloring 6778 6779 Level: developer 6780 6781 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(), 6782 MatSetValues(), MatSetValuesAdic() 6783 @*/ 6784 PetscErrorCode PETSCMAT_DLLEXPORT MatSetColoring(Mat mat,ISColoring coloring) 6785 { 6786 PetscErrorCode ierr; 6787 6788 PetscFunctionBegin; 6789 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 6790 PetscValidType(mat,1); 6791 PetscValidPointer(coloring,2); 6792 6793 if (!mat->assembled) { 6794 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled"); 6795 } 6796 if (!mat->ops->setcoloring) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 6797 ierr = (*mat->ops->setcoloring)(mat,coloring);CHKERRQ(ierr); 6798 PetscFunctionReturn(0); 6799 } 6800 6801 #undef __FUNCT__ 6802 #define __FUNCT__ "MatSetValuesAdifor" 6803 /*@ 6804 MatSetValuesAdifor - Sets values computed with automatic differentiation into a matrix. 6805 6806 Not Collective 6807 6808 Input Parameters: 6809 + mat - the matrix 6810 . nl - leading dimension of v 6811 - v - the values compute with ADIFOR 6812 6813 Level: developer 6814 6815 Notes: 6816 Must call MatSetColoring() before using this routine. Also this matrix must already 6817 have its nonzero pattern determined. 6818 6819 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(), 6820 MatSetValues(), MatSetColoring() 6821 @*/ 6822 PetscErrorCode PETSCMAT_DLLEXPORT MatSetValuesAdifor(Mat mat,PetscInt nl,void *v) 6823 { 6824 PetscErrorCode ierr; 6825 6826 PetscFunctionBegin; 6827 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 6828 PetscValidType(mat,1); 6829 PetscValidPointer(v,3); 6830 6831 if (!mat->assembled) { 6832 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled"); 6833 } 6834 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 6835 if (!mat->ops->setvaluesadifor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 6836 ierr = (*mat->ops->setvaluesadifor)(mat,nl,v);CHKERRQ(ierr); 6837 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 6838 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 6839 PetscFunctionReturn(0); 6840 } 6841 6842 #undef __FUNCT__ 6843 #define __FUNCT__ "MatDiagonalScaleLocal" 6844 /*@ 6845 MatDiagonalScaleLocal - Scales columns of a matrix given the scaling values including the 6846 ghosted ones. 6847 6848 Not Collective 6849 6850 Input Parameters: 6851 + mat - the matrix 6852 - diag = the diagonal values, including ghost ones 6853 6854 Level: developer 6855 6856 Notes: Works only for MPIAIJ and MPIBAIJ matrices 6857 6858 .seealso: MatDiagonalScale() 6859 @*/ 6860 PetscErrorCode PETSCMAT_DLLEXPORT MatDiagonalScaleLocal(Mat mat,Vec diag) 6861 { 6862 PetscErrorCode ierr; 6863 PetscMPIInt size; 6864 6865 PetscFunctionBegin; 6866 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 6867 PetscValidHeaderSpecific(diag,VEC_COOKIE,2); 6868 PetscValidType(mat,1); 6869 6870 if (!mat->assembled) { 6871 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled"); 6872 } 6873 ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 6874 ierr = MPI_Comm_size(((PetscObject)mat)->comm,&size);CHKERRQ(ierr); 6875 if (size == 1) { 6876 PetscInt n,m; 6877 ierr = VecGetSize(diag,&n);CHKERRQ(ierr); 6878 ierr = MatGetSize(mat,0,&m);CHKERRQ(ierr); 6879 if (m == n) { 6880 ierr = MatDiagonalScale(mat,0,diag);CHKERRQ(ierr); 6881 } else { 6882 SETERRQ(PETSC_ERR_SUP,"Only supported for sequential matrices when no ghost points/periodic conditions"); 6883 } 6884 } else { 6885 PetscErrorCode (*f)(Mat,Vec); 6886 ierr = PetscObjectQueryFunction((PetscObject)mat,"MatDiagonalScaleLocal_C",(void (**)(void))&f);CHKERRQ(ierr); 6887 if (f) { 6888 ierr = (*f)(mat,diag);CHKERRQ(ierr); 6889 } else { 6890 SETERRQ(PETSC_ERR_SUP,"Only supported for MPIAIJ and MPIBAIJ parallel matrices"); 6891 } 6892 } 6893 ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 6894 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 6895 PetscFunctionReturn(0); 6896 } 6897 6898 #undef __FUNCT__ 6899 #define __FUNCT__ "MatGetInertia" 6900 /*@ 6901 MatGetInertia - Gets the inertia from a factored matrix 6902 6903 Collective on Mat 6904 6905 Input Parameter: 6906 . mat - the matrix 6907 6908 Output Parameters: 6909 + nneg - number of negative eigenvalues 6910 . nzero - number of zero eigenvalues 6911 - npos - number of positive eigenvalues 6912 6913 Level: advanced 6914 6915 Notes: Matrix must have been factored by MatCholeskyFactor() 6916 6917 6918 @*/ 6919 PetscErrorCode PETSCMAT_DLLEXPORT MatGetInertia(Mat mat,PetscInt *nneg,PetscInt *nzero,PetscInt *npos) 6920 { 6921 PetscErrorCode ierr; 6922 6923 PetscFunctionBegin; 6924 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 6925 PetscValidType(mat,1); 6926 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 6927 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Numeric factor mat is not assembled"); 6928 if (!mat->ops->getinertia) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 6929 ierr = (*mat->ops->getinertia)(mat,nneg,nzero,npos);CHKERRQ(ierr); 6930 PetscFunctionReturn(0); 6931 } 6932 6933 /* ----------------------------------------------------------------*/ 6934 #undef __FUNCT__ 6935 #define __FUNCT__ "MatSolves" 6936 /*@C 6937 MatSolves - Solves A x = b, given a factored matrix, for a collection of vectors 6938 6939 Collective on Mat and Vecs 6940 6941 Input Parameters: 6942 + mat - the factored matrix 6943 - b - the right-hand-side vectors 6944 6945 Output Parameter: 6946 . x - the result vectors 6947 6948 Notes: 6949 The vectors b and x cannot be the same. I.e., one cannot 6950 call MatSolves(A,x,x). 6951 6952 Notes: 6953 Most users should employ the simplified KSP interface for linear solvers 6954 instead of working directly with matrix algebra routines such as this. 6955 See, e.g., KSPCreate(). 6956 6957 Level: developer 6958 6959 Concepts: matrices^triangular solves 6960 6961 .seealso: MatSolveAdd(), MatSolveTranspose(), MatSolveTransposeAdd(), MatSolve() 6962 @*/ 6963 PetscErrorCode PETSCMAT_DLLEXPORT MatSolves(Mat mat,Vecs b,Vecs x) 6964 { 6965 PetscErrorCode ierr; 6966 6967 PetscFunctionBegin; 6968 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 6969 PetscValidType(mat,1); 6970 if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 6971 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 6972 if (!mat->rmap->N && !mat->cmap->N) PetscFunctionReturn(0); 6973 6974 if (!mat->ops->solves) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 6975 ierr = MatPreallocated(mat);CHKERRQ(ierr); 6976 ierr = PetscLogEventBegin(MAT_Solves,mat,0,0,0);CHKERRQ(ierr); 6977 ierr = (*mat->ops->solves)(mat,b,x);CHKERRQ(ierr); 6978 ierr = PetscLogEventEnd(MAT_Solves,mat,0,0,0);CHKERRQ(ierr); 6979 PetscFunctionReturn(0); 6980 } 6981 6982 #undef __FUNCT__ 6983 #define __FUNCT__ "MatIsSymmetric" 6984 /*@ 6985 MatIsSymmetric - Test whether a matrix is symmetric 6986 6987 Collective on Mat 6988 6989 Input Parameter: 6990 + A - the matrix to test 6991 - tol - difference between value and its transpose less than this amount counts as equal (use 0.0 for exact transpose) 6992 6993 Output Parameters: 6994 . flg - the result 6995 6996 Level: intermediate 6997 6998 Concepts: matrix^symmetry 6999 7000 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetricKnown() 7001 @*/ 7002 PetscErrorCode PETSCMAT_DLLEXPORT MatIsSymmetric(Mat A,PetscReal tol,PetscTruth *flg) 7003 { 7004 PetscErrorCode ierr; 7005 7006 PetscFunctionBegin; 7007 PetscValidHeaderSpecific(A,MAT_COOKIE,1); 7008 PetscValidPointer(flg,2); 7009 7010 if (!A->symmetric_set) { 7011 if (!A->ops->issymmetric) { 7012 const MatType mattype; 7013 ierr = MatGetType(A,&mattype);CHKERRQ(ierr); 7014 SETERRQ1(PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for symmetric",mattype); 7015 } 7016 ierr = (*A->ops->issymmetric)(A,tol,flg);CHKERRQ(ierr); 7017 if (!tol) { 7018 A->symmetric_set = PETSC_TRUE; 7019 A->symmetric = *flg; 7020 if (A->symmetric) { 7021 A->structurally_symmetric_set = PETSC_TRUE; 7022 A->structurally_symmetric = PETSC_TRUE; 7023 } 7024 } 7025 } else if (A->symmetric) { 7026 *flg = PETSC_TRUE; 7027 } else if (!tol) { 7028 *flg = PETSC_FALSE; 7029 } else { 7030 if (!A->ops->issymmetric) { 7031 const MatType mattype; 7032 ierr = MatGetType(A,&mattype);CHKERRQ(ierr); 7033 SETERRQ1(PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for symmetric",mattype); 7034 } 7035 ierr = (*A->ops->issymmetric)(A,tol,flg);CHKERRQ(ierr); 7036 } 7037 PetscFunctionReturn(0); 7038 } 7039 7040 #undef __FUNCT__ 7041 #define __FUNCT__ "MatIsHermitian" 7042 /*@ 7043 MatIsHermitian - Test whether a matrix is Hermitian 7044 7045 Collective on Mat 7046 7047 Input Parameter: 7048 + A - the matrix to test 7049 - tol - difference between value and its transpose less than this amount counts as equal (use 0.0 for exact Hermitian) 7050 7051 Output Parameters: 7052 . flg - the result 7053 7054 Level: intermediate 7055 7056 Concepts: matrix^symmetry 7057 7058 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetricKnown() 7059 @*/ 7060 PetscErrorCode PETSCMAT_DLLEXPORT MatIsHermitian(Mat A,PetscReal tol,PetscTruth *flg) 7061 { 7062 PetscErrorCode ierr; 7063 7064 PetscFunctionBegin; 7065 PetscValidHeaderSpecific(A,MAT_COOKIE,1); 7066 PetscValidPointer(flg,2); 7067 7068 if (!A->hermitian_set) { 7069 if (!A->ops->ishermitian) { 7070 const MatType mattype; 7071 ierr = MatGetType(A,&mattype);CHKERRQ(ierr); 7072 SETERRQ1(PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for hermitian",mattype); 7073 } 7074 ierr = (*A->ops->ishermitian)(A,tol,flg);CHKERRQ(ierr); 7075 if (!tol) { 7076 A->hermitian_set = PETSC_TRUE; 7077 A->hermitian = *flg; 7078 if (A->hermitian) { 7079 A->structurally_symmetric_set = PETSC_TRUE; 7080 A->structurally_symmetric = PETSC_TRUE; 7081 } 7082 } 7083 } else if (A->hermitian) { 7084 *flg = PETSC_TRUE; 7085 } else if (!tol) { 7086 *flg = PETSC_FALSE; 7087 } else { 7088 if (!A->ops->ishermitian) { 7089 const MatType mattype; 7090 ierr = MatGetType(A,&mattype);CHKERRQ(ierr); 7091 SETERRQ1(PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for hermitian",mattype); 7092 } 7093 ierr = (*A->ops->ishermitian)(A,tol,flg);CHKERRQ(ierr); 7094 } 7095 PetscFunctionReturn(0); 7096 } 7097 7098 #undef __FUNCT__ 7099 #define __FUNCT__ "MatIsSymmetricKnown" 7100 /*@ 7101 MatIsSymmetricKnown - Checks the flag on the matrix to see if it is symmetric. 7102 7103 Collective on Mat 7104 7105 Input Parameter: 7106 . A - the matrix to check 7107 7108 Output Parameters: 7109 + set - if the symmetric flag is set (this tells you if the next flag is valid) 7110 - flg - the result 7111 7112 Level: advanced 7113 7114 Concepts: matrix^symmetry 7115 7116 Note: Does not check the matrix values directly, so this may return unknown (set = PETSC_FALSE). Use MatIsSymmetric() 7117 if you want it explicitly checked 7118 7119 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetric() 7120 @*/ 7121 PetscErrorCode PETSCMAT_DLLEXPORT MatIsSymmetricKnown(Mat A,PetscTruth *set,PetscTruth *flg) 7122 { 7123 PetscFunctionBegin; 7124 PetscValidHeaderSpecific(A,MAT_COOKIE,1); 7125 PetscValidPointer(set,2); 7126 PetscValidPointer(flg,3); 7127 if (A->symmetric_set) { 7128 *set = PETSC_TRUE; 7129 *flg = A->symmetric; 7130 } else { 7131 *set = PETSC_FALSE; 7132 } 7133 PetscFunctionReturn(0); 7134 } 7135 7136 #undef __FUNCT__ 7137 #define __FUNCT__ "MatIsHermitianKnown" 7138 /*@ 7139 MatIsHermitianKnown - Checks the flag on the matrix to see if it is hermitian. 7140 7141 Collective on Mat 7142 7143 Input Parameter: 7144 . A - the matrix to check 7145 7146 Output Parameters: 7147 + set - if the hermitian flag is set (this tells you if the next flag is valid) 7148 - flg - the result 7149 7150 Level: advanced 7151 7152 Concepts: matrix^symmetry 7153 7154 Note: Does not check the matrix values directly, so this may return unknown (set = PETSC_FALSE). Use MatIsHermitian() 7155 if you want it explicitly checked 7156 7157 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetric() 7158 @*/ 7159 PetscErrorCode PETSCMAT_DLLEXPORT MatIsHermitianKnown(Mat A,PetscTruth *set,PetscTruth *flg) 7160 { 7161 PetscFunctionBegin; 7162 PetscValidHeaderSpecific(A,MAT_COOKIE,1); 7163 PetscValidPointer(set,2); 7164 PetscValidPointer(flg,3); 7165 if (A->hermitian_set) { 7166 *set = PETSC_TRUE; 7167 *flg = A->hermitian; 7168 } else { 7169 *set = PETSC_FALSE; 7170 } 7171 PetscFunctionReturn(0); 7172 } 7173 7174 #undef __FUNCT__ 7175 #define __FUNCT__ "MatIsStructurallySymmetric" 7176 /*@ 7177 MatIsStructurallySymmetric - Test whether a matrix is structurally symmetric 7178 7179 Collective on Mat 7180 7181 Input Parameter: 7182 . A - the matrix to test 7183 7184 Output Parameters: 7185 . flg - the result 7186 7187 Level: intermediate 7188 7189 Concepts: matrix^symmetry 7190 7191 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsSymmetric(), MatSetOption() 7192 @*/ 7193 PetscErrorCode PETSCMAT_DLLEXPORT MatIsStructurallySymmetric(Mat A,PetscTruth *flg) 7194 { 7195 PetscErrorCode ierr; 7196 7197 PetscFunctionBegin; 7198 PetscValidHeaderSpecific(A,MAT_COOKIE,1); 7199 PetscValidPointer(flg,2); 7200 if (!A->structurally_symmetric_set) { 7201 if (!A->ops->isstructurallysymmetric) SETERRQ(PETSC_ERR_SUP,"Matrix does not support checking for structural symmetric"); 7202 ierr = (*A->ops->isstructurallysymmetric)(A,&A->structurally_symmetric);CHKERRQ(ierr); 7203 A->structurally_symmetric_set = PETSC_TRUE; 7204 } 7205 *flg = A->structurally_symmetric; 7206 PetscFunctionReturn(0); 7207 } 7208 7209 #undef __FUNCT__ 7210 #define __FUNCT__ "MatStashGetInfo" 7211 extern PetscErrorCode MatStashGetInfo_Private(MatStash*,PetscInt*,PetscInt*); 7212 /*@ 7213 MatStashGetInfo - Gets how many values are currently in the vector stash, i.e. need 7214 to be communicated to other processors during the MatAssemblyBegin/End() process 7215 7216 Not collective 7217 7218 Input Parameter: 7219 . vec - the vector 7220 7221 Output Parameters: 7222 + nstash - the size of the stash 7223 . reallocs - the number of additional mallocs incurred. 7224 . bnstash - the size of the block stash 7225 - breallocs - the number of additional mallocs incurred.in the block stash 7226 7227 Level: advanced 7228 7229 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), Mat, MatStashSetInitialSize() 7230 7231 @*/ 7232 PetscErrorCode PETSCMAT_DLLEXPORT MatStashGetInfo(Mat mat,PetscInt *nstash,PetscInt *reallocs,PetscInt *bnstash,PetscInt *breallocs) 7233 { 7234 PetscErrorCode ierr; 7235 PetscFunctionBegin; 7236 ierr = MatStashGetInfo_Private(&mat->stash,nstash,reallocs);CHKERRQ(ierr); 7237 ierr = MatStashGetInfo_Private(&mat->bstash,bnstash,breallocs);CHKERRQ(ierr); 7238 PetscFunctionReturn(0); 7239 } 7240 7241 #undef __FUNCT__ 7242 #define __FUNCT__ "MatGetVecs" 7243 /*@C 7244 MatGetVecs - Get vector(s) compatible with the matrix, i.e. with the same 7245 parallel layout 7246 7247 Collective on Mat 7248 7249 Input Parameter: 7250 . mat - the matrix 7251 7252 Output Parameter: 7253 + right - (optional) vector that the matrix can be multiplied against 7254 - left - (optional) vector that the matrix vector product can be stored in 7255 7256 Level: advanced 7257 7258 .seealso: MatCreate() 7259 @*/ 7260 PetscErrorCode PETSCMAT_DLLEXPORT MatGetVecs(Mat mat,Vec *right,Vec *left) 7261 { 7262 PetscErrorCode ierr; 7263 7264 PetscFunctionBegin; 7265 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 7266 PetscValidType(mat,1); 7267 ierr = MatPreallocated(mat);CHKERRQ(ierr); 7268 if (mat->ops->getvecs) { 7269 ierr = (*mat->ops->getvecs)(mat,right,left);CHKERRQ(ierr); 7270 } else { 7271 PetscMPIInt size; 7272 ierr = MPI_Comm_size(((PetscObject)mat)->comm, &size);CHKERRQ(ierr); 7273 if (right) { 7274 ierr = VecCreate(((PetscObject)mat)->comm,right);CHKERRQ(ierr); 7275 ierr = VecSetSizes(*right,mat->cmap->n,PETSC_DETERMINE);CHKERRQ(ierr); 7276 ierr = VecSetBlockSize(*right,mat->rmap->bs);CHKERRQ(ierr); 7277 if (size > 1) { 7278 /* New vectors uses Mat cmap and does not create a new one */ 7279 ierr = PetscLayoutDestroy((*right)->map);CHKERRQ(ierr); 7280 (*right)->map = mat->cmap; 7281 mat->cmap->refcnt++; 7282 7283 ierr = VecSetType(*right,VECMPI);CHKERRQ(ierr); 7284 } else {ierr = VecSetType(*right,VECSEQ);CHKERRQ(ierr);} 7285 } 7286 if (left) { 7287 ierr = VecCreate(((PetscObject)mat)->comm,left);CHKERRQ(ierr); 7288 ierr = VecSetSizes(*left,mat->rmap->n,PETSC_DETERMINE);CHKERRQ(ierr); 7289 ierr = VecSetBlockSize(*left,mat->rmap->bs);CHKERRQ(ierr); 7290 if (size > 1) { 7291 /* New vectors uses Mat rmap and does not create a new one */ 7292 ierr = PetscLayoutDestroy((*left)->map);CHKERRQ(ierr); 7293 (*left)->map = mat->rmap; 7294 mat->rmap->refcnt++; 7295 7296 ierr = VecSetType(*left,VECMPI);CHKERRQ(ierr); 7297 } else {ierr = VecSetType(*left,VECSEQ);CHKERRQ(ierr);} 7298 } 7299 } 7300 if (mat->mapping) { 7301 if (right) {ierr = VecSetLocalToGlobalMapping(*right,mat->mapping);CHKERRQ(ierr);} 7302 if (left) {ierr = VecSetLocalToGlobalMapping(*left,mat->mapping);CHKERRQ(ierr);} 7303 } 7304 if (mat->bmapping) { 7305 if (right) {ierr = VecSetLocalToGlobalMappingBlock(*right,mat->bmapping);CHKERRQ(ierr);} 7306 if (left) {ierr = VecSetLocalToGlobalMappingBlock(*left,mat->bmapping);CHKERRQ(ierr);} 7307 } 7308 PetscFunctionReturn(0); 7309 } 7310 7311 #undef __FUNCT__ 7312 #define __FUNCT__ "MatFactorInfoInitialize" 7313 /*@C 7314 MatFactorInfoInitialize - Initializes a MatFactorInfo data structure 7315 with default values. 7316 7317 Not Collective 7318 7319 Input Parameters: 7320 . info - the MatFactorInfo data structure 7321 7322 7323 Notes: The solvers are generally used through the KSP and PC objects, for example 7324 PCLU, PCILU, PCCHOLESKY, PCICC 7325 7326 Level: developer 7327 7328 .seealso: MatFactorInfo 7329 7330 Developer Note: fortran interface is not autogenerated as the f90 7331 interface defintion cannot be generated correctly [due to MatFactorInfo] 7332 7333 @*/ 7334 7335 PetscErrorCode PETSCMAT_DLLEXPORT MatFactorInfoInitialize(MatFactorInfo *info) 7336 { 7337 PetscErrorCode ierr; 7338 7339 PetscFunctionBegin; 7340 ierr = PetscMemzero(info,sizeof(MatFactorInfo));CHKERRQ(ierr); 7341 PetscFunctionReturn(0); 7342 } 7343 7344 #undef __FUNCT__ 7345 #define __FUNCT__ "MatPtAP" 7346 /*@ 7347 MatPtAP - Creates the matrix projection C = P^T * A * P 7348 7349 Collective on Mat 7350 7351 Input Parameters: 7352 + A - the matrix 7353 . P - the projection matrix 7354 . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 7355 - fill - expected fill as ratio of nnz(C)/nnz(A) 7356 7357 Output Parameters: 7358 . C - the product matrix 7359 7360 Notes: 7361 C will be created and must be destroyed by the user with MatDestroy(). 7362 7363 This routine is currently only implemented for pairs of AIJ matrices and classes 7364 which inherit from AIJ. 7365 7366 Level: intermediate 7367 7368 .seealso: MatPtAPSymbolic(), MatPtAPNumeric(), MatMatMult() 7369 @*/ 7370 PetscErrorCode PETSCMAT_DLLEXPORT MatPtAP(Mat A,Mat P,MatReuse scall,PetscReal fill,Mat *C) 7371 { 7372 PetscErrorCode ierr; 7373 7374 PetscFunctionBegin; 7375 PetscValidHeaderSpecific(A,MAT_COOKIE,1); 7376 PetscValidType(A,1); 7377 if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 7378 if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7379 PetscValidHeaderSpecific(P,MAT_COOKIE,2); 7380 PetscValidType(P,2); 7381 ierr = MatPreallocated(P);CHKERRQ(ierr); 7382 if (!P->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 7383 if (P->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7384 PetscValidPointer(C,3); 7385 if (P->rmap->N!=A->cmap->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",P->rmap->N,A->cmap->N); 7386 if (fill < 1.0) SETERRQ1(PETSC_ERR_ARG_SIZ,"Expected fill=%G must be >= 1.0",fill); 7387 ierr = MatPreallocated(A);CHKERRQ(ierr); 7388 7389 if (!A->ops->ptap) { 7390 const MatType mattype; 7391 ierr = MatGetType(A,&mattype);CHKERRQ(ierr); 7392 SETERRQ1(PETSC_ERR_SUP,"Matrix of type <%s> does not support PtAP",mattype); 7393 } 7394 ierr = PetscLogEventBegin(MAT_PtAP,A,P,0,0);CHKERRQ(ierr); 7395 ierr = (*A->ops->ptap)(A,P,scall,fill,C);CHKERRQ(ierr); 7396 ierr = PetscLogEventEnd(MAT_PtAP,A,P,0,0);CHKERRQ(ierr); 7397 7398 PetscFunctionReturn(0); 7399 } 7400 7401 #undef __FUNCT__ 7402 #define __FUNCT__ "MatPtAPNumeric" 7403 /*@ 7404 MatPtAPNumeric - Computes the matrix projection C = P^T * A * P 7405 7406 Collective on Mat 7407 7408 Input Parameters: 7409 + A - the matrix 7410 - P - the projection matrix 7411 7412 Output Parameters: 7413 . C - the product matrix 7414 7415 Notes: 7416 C must have been created by calling MatPtAPSymbolic and must be destroyed by 7417 the user using MatDeatroy(). 7418 7419 This routine is currently only implemented for pairs of AIJ matrices and classes 7420 which inherit from AIJ. C will be of type MATAIJ. 7421 7422 Level: intermediate 7423 7424 .seealso: MatPtAP(), MatPtAPSymbolic(), MatMatMultNumeric() 7425 @*/ 7426 PetscErrorCode PETSCMAT_DLLEXPORT MatPtAPNumeric(Mat A,Mat P,Mat C) 7427 { 7428 PetscErrorCode ierr; 7429 7430 PetscFunctionBegin; 7431 PetscValidHeaderSpecific(A,MAT_COOKIE,1); 7432 PetscValidType(A,1); 7433 if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 7434 if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7435 PetscValidHeaderSpecific(P,MAT_COOKIE,2); 7436 PetscValidType(P,2); 7437 ierr = MatPreallocated(P);CHKERRQ(ierr); 7438 if (!P->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 7439 if (P->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7440 PetscValidHeaderSpecific(C,MAT_COOKIE,3); 7441 PetscValidType(C,3); 7442 ierr = MatPreallocated(C);CHKERRQ(ierr); 7443 if (C->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7444 if (P->cmap->N!=C->rmap->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",P->cmap->N,C->rmap->N); 7445 if (P->rmap->N!=A->cmap->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",P->rmap->N,A->cmap->N); 7446 if (A->rmap->N!=A->cmap->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix 'A' must be square, %D != %D",A->rmap->N,A->cmap->N); 7447 if (P->cmap->N!=C->cmap->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",P->cmap->N,C->cmap->N); 7448 ierr = MatPreallocated(A);CHKERRQ(ierr); 7449 7450 ierr = PetscLogEventBegin(MAT_PtAPNumeric,A,P,0,0);CHKERRQ(ierr); 7451 ierr = (*A->ops->ptapnumeric)(A,P,C);CHKERRQ(ierr); 7452 ierr = PetscLogEventEnd(MAT_PtAPNumeric,A,P,0,0);CHKERRQ(ierr); 7453 PetscFunctionReturn(0); 7454 } 7455 7456 #undef __FUNCT__ 7457 #define __FUNCT__ "MatPtAPSymbolic" 7458 /*@ 7459 MatPtAPSymbolic - Creates the (i,j) structure of the matrix projection C = P^T * A * P 7460 7461 Collective on Mat 7462 7463 Input Parameters: 7464 + A - the matrix 7465 - P - the projection matrix 7466 7467 Output Parameters: 7468 . C - the (i,j) structure of the product matrix 7469 7470 Notes: 7471 C will be created and must be destroyed by the user with MatDestroy(). 7472 7473 This routine is currently only implemented for pairs of SeqAIJ matrices and classes 7474 which inherit from SeqAIJ. C will be of type MATSEQAIJ. The product is computed using 7475 this (i,j) structure by calling MatPtAPNumeric(). 7476 7477 Level: intermediate 7478 7479 .seealso: MatPtAP(), MatPtAPNumeric(), MatMatMultSymbolic() 7480 @*/ 7481 PetscErrorCode PETSCMAT_DLLEXPORT MatPtAPSymbolic(Mat A,Mat P,PetscReal fill,Mat *C) 7482 { 7483 PetscErrorCode ierr; 7484 7485 PetscFunctionBegin; 7486 PetscValidHeaderSpecific(A,MAT_COOKIE,1); 7487 PetscValidType(A,1); 7488 if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 7489 if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7490 if (fill <1.0) SETERRQ1(PETSC_ERR_ARG_SIZ,"Expected fill=%G must be >= 1.0",fill); 7491 PetscValidHeaderSpecific(P,MAT_COOKIE,2); 7492 PetscValidType(P,2); 7493 ierr = MatPreallocated(P);CHKERRQ(ierr); 7494 if (!P->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 7495 if (P->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7496 PetscValidPointer(C,3); 7497 7498 if (P->rmap->N!=A->cmap->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",P->rmap->N,A->cmap->N); 7499 if (A->rmap->N!=A->cmap->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix 'A' must be square, %D != %D",A->rmap->N,A->cmap->N); 7500 ierr = MatPreallocated(A);CHKERRQ(ierr); 7501 ierr = PetscLogEventBegin(MAT_PtAPSymbolic,A,P,0,0);CHKERRQ(ierr); 7502 ierr = (*A->ops->ptapsymbolic)(A,P,fill,C);CHKERRQ(ierr); 7503 ierr = PetscLogEventEnd(MAT_PtAPSymbolic,A,P,0,0);CHKERRQ(ierr); 7504 7505 ierr = MatSetBlockSize(*C,A->rmap->bs);CHKERRQ(ierr); 7506 7507 PetscFunctionReturn(0); 7508 } 7509 7510 #undef __FUNCT__ 7511 #define __FUNCT__ "MatMatMult" 7512 /*@ 7513 MatMatMult - Performs Matrix-Matrix Multiplication C=A*B. 7514 7515 Collective on Mat 7516 7517 Input Parameters: 7518 + A - the left matrix 7519 . B - the right matrix 7520 . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 7521 - fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if you do not have a good estimate 7522 if the result is a dense matrix this is irrelevent 7523 7524 Output Parameters: 7525 . C - the product matrix 7526 7527 Notes: 7528 Unless scall is MAT_REUSE_MATRIX C will be created. 7529 7530 MAT_REUSE_MATRIX can only be used if the matrices A and B have the same nonzero pattern as in the previous call 7531 7532 To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value 7533 actually needed. 7534 7535 If you have many matrices with the same non-zero structure to multiply, you 7536 should either 7537 $ 1) use MAT_REUSE_MATRIX in all calls but the first or 7538 $ 2) call MatMatMultSymbolic() once and then MatMatMultNumeric() for each product needed 7539 7540 Level: intermediate 7541 7542 .seealso: MatMatMultSymbolic(), MatMatMultNumeric(), MatPtAP() 7543 @*/ 7544 PetscErrorCode PETSCMAT_DLLEXPORT MatMatMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C) 7545 { 7546 PetscErrorCode ierr; 7547 PetscErrorCode (*fA)(Mat,Mat,MatReuse,PetscReal,Mat*); 7548 PetscErrorCode (*fB)(Mat,Mat,MatReuse,PetscReal,Mat*); 7549 PetscErrorCode (*mult)(Mat,Mat,MatReuse,PetscReal,Mat *)=PETSC_NULL; 7550 7551 PetscFunctionBegin; 7552 PetscValidHeaderSpecific(A,MAT_COOKIE,1); 7553 PetscValidType(A,1); 7554 if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 7555 if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7556 PetscValidHeaderSpecific(B,MAT_COOKIE,2); 7557 PetscValidType(B,2); 7558 ierr = MatPreallocated(B);CHKERRQ(ierr); 7559 if (!B->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 7560 if (B->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7561 PetscValidPointer(C,3); 7562 if (B->rmap->N!=A->cmap->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",B->rmap->N,A->cmap->N); 7563 if (scall == MAT_REUSE_MATRIX){ 7564 PetscValidPointer(*C,5); 7565 PetscValidHeaderSpecific(*C,MAT_COOKIE,5); 7566 } 7567 if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0; 7568 if (fill < 1.0) SETERRQ1(PETSC_ERR_ARG_SIZ,"Expected fill=%G must be >= 1.0",fill); 7569 ierr = MatPreallocated(A);CHKERRQ(ierr); 7570 7571 fA = A->ops->matmult; 7572 fB = B->ops->matmult; 7573 if (fB == fA) { 7574 if (!fB) SETERRQ1(PETSC_ERR_SUP,"MatMatMult not supported for B of type %s",((PetscObject)B)->type_name); 7575 mult = fB; 7576 } else { 7577 /* dispatch based on the type of A and B */ 7578 char multname[256]; 7579 ierr = PetscStrcpy(multname,"MatMatMult_");CHKERRQ(ierr); 7580 ierr = PetscStrcat(multname,((PetscObject)A)->type_name);CHKERRQ(ierr); 7581 ierr = PetscStrcat(multname,"_");CHKERRQ(ierr); 7582 ierr = PetscStrcat(multname,((PetscObject)B)->type_name);CHKERRQ(ierr); 7583 ierr = PetscStrcat(multname,"_C");CHKERRQ(ierr); /* e.g., multname = "MatMatMult_seqdense_seqaij_C" */ 7584 ierr = PetscObjectQueryFunction((PetscObject)B,multname,(void (**)(void))&mult);CHKERRQ(ierr); 7585 if (!mult) SETERRQ2(PETSC_ERR_ARG_INCOMP,"MatMatMult requires A, %s, to be compatible with B, %s",((PetscObject)A)->type_name,((PetscObject)B)->type_name); 7586 } 7587 ierr = PetscLogEventBegin(MAT_MatMult,A,B,0,0);CHKERRQ(ierr); 7588 ierr = (*mult)(A,B,scall,fill,C);CHKERRQ(ierr); 7589 ierr = PetscLogEventEnd(MAT_MatMult,A,B,0,0);CHKERRQ(ierr); 7590 PetscFunctionReturn(0); 7591 } 7592 7593 #undef __FUNCT__ 7594 #define __FUNCT__ "MatMatMultSymbolic" 7595 /*@ 7596 MatMatMultSymbolic - Performs construction, preallocation, and computes the ij structure 7597 of the matrix-matrix product C=A*B. Call this routine before calling MatMatMultNumeric(). 7598 7599 Collective on Mat 7600 7601 Input Parameters: 7602 + A - the left matrix 7603 . B - the right matrix 7604 - fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if you do not have a good estimate, 7605 if C is a dense matrix this is irrelevent 7606 7607 Output Parameters: 7608 . C - the product matrix 7609 7610 Notes: 7611 Unless scall is MAT_REUSE_MATRIX C will be created. 7612 7613 To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value 7614 actually needed. 7615 7616 This routine is currently implemented for 7617 - pairs of AIJ matrices and classes which inherit from AIJ, C will be of type AIJ 7618 - pairs of AIJ (A) and Dense (B) matrix, C will be of type Dense. 7619 - pairs of Dense (A) and AIJ (B) matrix, C will be of type Dense. 7620 7621 Level: intermediate 7622 7623 .seealso: MatMatMult(), MatMatMultNumeric() 7624 @*/ 7625 PetscErrorCode PETSCMAT_DLLEXPORT MatMatMultSymbolic(Mat A,Mat B,PetscReal fill,Mat *C) 7626 { 7627 PetscErrorCode ierr; 7628 PetscErrorCode (*Asymbolic)(Mat,Mat,PetscReal,Mat *); 7629 PetscErrorCode (*Bsymbolic)(Mat,Mat,PetscReal,Mat *); 7630 PetscErrorCode (*symbolic)(Mat,Mat,PetscReal,Mat *)=PETSC_NULL; 7631 7632 PetscFunctionBegin; 7633 PetscValidHeaderSpecific(A,MAT_COOKIE,1); 7634 PetscValidType(A,1); 7635 if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 7636 if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7637 7638 PetscValidHeaderSpecific(B,MAT_COOKIE,2); 7639 PetscValidType(B,2); 7640 ierr = MatPreallocated(B);CHKERRQ(ierr); 7641 if (!B->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 7642 if (B->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7643 PetscValidPointer(C,3); 7644 7645 if (B->rmap->N!=A->cmap->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",B->rmap->N,A->cmap->N); 7646 if (fill == PETSC_DEFAULT) fill = 2.0; 7647 if (fill < 1.0) SETERRQ1(PETSC_ERR_ARG_SIZ,"Expected fill=%G must be > 1.0",fill); 7648 ierr = MatPreallocated(A);CHKERRQ(ierr); 7649 7650 Asymbolic = A->ops->matmultsymbolic; 7651 Bsymbolic = B->ops->matmultsymbolic; 7652 if (Asymbolic == Bsymbolic){ 7653 if (!Bsymbolic) SETERRQ1(PETSC_ERR_SUP,"C=A*B not implemented for B of type %s",((PetscObject)B)->type_name); 7654 symbolic = Bsymbolic; 7655 } else { /* dispatch based on the type of A and B */ 7656 char symbolicname[256]; 7657 ierr = PetscStrcpy(symbolicname,"MatMatMultSymbolic_");CHKERRQ(ierr); 7658 ierr = PetscStrcat(symbolicname,((PetscObject)A)->type_name);CHKERRQ(ierr); 7659 ierr = PetscStrcat(symbolicname,"_");CHKERRQ(ierr); 7660 ierr = PetscStrcat(symbolicname,((PetscObject)B)->type_name);CHKERRQ(ierr); 7661 ierr = PetscStrcat(symbolicname,"_C");CHKERRQ(ierr); 7662 ierr = PetscObjectQueryFunction((PetscObject)B,symbolicname,(void (**)(void))&symbolic);CHKERRQ(ierr); 7663 if (!symbolic) SETERRQ2(PETSC_ERR_ARG_INCOMP,"MatMatMultSymbolic requires A, %s, to be compatible with B, %s",((PetscObject)A)->type_name,((PetscObject)B)->type_name); 7664 } 7665 ierr = PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr); 7666 ierr = (*symbolic)(A,B,fill,C);CHKERRQ(ierr); 7667 ierr = PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr); 7668 PetscFunctionReturn(0); 7669 } 7670 7671 #undef __FUNCT__ 7672 #define __FUNCT__ "MatMatMultNumeric" 7673 /*@ 7674 MatMatMultNumeric - Performs the numeric matrix-matrix product. 7675 Call this routine after first calling MatMatMultSymbolic(). 7676 7677 Collective on Mat 7678 7679 Input Parameters: 7680 + A - the left matrix 7681 - B - the right matrix 7682 7683 Output Parameters: 7684 . C - the product matrix, which was created by from MatMatMultSymbolic() or a call to MatMatMult(). 7685 7686 Notes: 7687 C must have been created with MatMatMultSymbolic(). 7688 7689 This routine is currently implemented for 7690 - pairs of AIJ matrices and classes which inherit from AIJ, C will be of type MATAIJ. 7691 - pairs of AIJ (A) and Dense (B) matrix, C will be of type Dense. 7692 - pairs of Dense (A) and AIJ (B) matrix, C will be of type Dense. 7693 7694 Level: intermediate 7695 7696 .seealso: MatMatMult(), MatMatMultSymbolic() 7697 @*/ 7698 PetscErrorCode PETSCMAT_DLLEXPORT MatMatMultNumeric(Mat A,Mat B,Mat C) 7699 { 7700 PetscErrorCode ierr; 7701 PetscErrorCode (*Anumeric)(Mat,Mat,Mat); 7702 PetscErrorCode (*Bnumeric)(Mat,Mat,Mat); 7703 PetscErrorCode (*numeric)(Mat,Mat,Mat)=PETSC_NULL; 7704 7705 PetscFunctionBegin; 7706 PetscValidHeaderSpecific(A,MAT_COOKIE,1); 7707 PetscValidType(A,1); 7708 if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 7709 if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7710 7711 PetscValidHeaderSpecific(B,MAT_COOKIE,2); 7712 PetscValidType(B,2); 7713 ierr = MatPreallocated(B);CHKERRQ(ierr); 7714 if (!B->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 7715 if (B->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7716 7717 PetscValidHeaderSpecific(C,MAT_COOKIE,3); 7718 PetscValidType(C,3); 7719 ierr = MatPreallocated(C);CHKERRQ(ierr); 7720 if (!C->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 7721 if (C->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7722 7723 if (B->cmap->N!=C->cmap->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",B->cmap->N,C->cmap->N); 7724 if (B->rmap->N!=A->cmap->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",B->rmap->N,A->cmap->N); 7725 if (A->rmap->N!=C->rmap->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",A->rmap->N,C->rmap->N); 7726 ierr = MatPreallocated(A);CHKERRQ(ierr); 7727 7728 Anumeric = A->ops->matmultnumeric; 7729 Bnumeric = B->ops->matmultnumeric; 7730 if (Anumeric == Bnumeric){ 7731 if (!Bnumeric) SETERRQ1(PETSC_ERR_SUP,"MatMatMultNumeric not supported for B of type %s",((PetscObject)B)->type_name); 7732 numeric = Bnumeric; 7733 } else { 7734 char numericname[256]; 7735 ierr = PetscStrcpy(numericname,"MatMatMultNumeric_");CHKERRQ(ierr); 7736 ierr = PetscStrcat(numericname,((PetscObject)A)->type_name);CHKERRQ(ierr); 7737 ierr = PetscStrcat(numericname,"_");CHKERRQ(ierr); 7738 ierr = PetscStrcat(numericname,((PetscObject)B)->type_name);CHKERRQ(ierr); 7739 ierr = PetscStrcat(numericname,"_C");CHKERRQ(ierr); 7740 ierr = PetscObjectQueryFunction((PetscObject)B,numericname,(void (**)(void))&numeric);CHKERRQ(ierr); 7741 if (!numeric) 7742 SETERRQ2(PETSC_ERR_ARG_INCOMP,"MatMatMultNumeric requires A, %s, to be compatible with B, %s",((PetscObject)A)->type_name,((PetscObject)B)->type_name); 7743 } 7744 ierr = PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr); 7745 ierr = (*numeric)(A,B,C);CHKERRQ(ierr); 7746 ierr = PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr); 7747 PetscFunctionReturn(0); 7748 } 7749 7750 #undef __FUNCT__ 7751 #define __FUNCT__ "MatMatMultTranspose" 7752 /*@ 7753 MatMatMultTranspose - Performs Matrix-Matrix Multiplication C=A^T*B. 7754 7755 Collective on Mat 7756 7757 Input Parameters: 7758 + A - the left matrix 7759 . B - the right matrix 7760 . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 7761 - fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if not known 7762 7763 Output Parameters: 7764 . C - the product matrix 7765 7766 Notes: 7767 C will be created if MAT_INITIAL_MATRIX and must be destroyed by the user with MatDestroy(). 7768 7769 MAT_REUSE_MATRIX can only be used if the matrices A and B have the same nonzero pattern as in the previous call 7770 7771 To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value 7772 actually needed. 7773 7774 This routine is currently only implemented for pairs of SeqAIJ matrices and pairs of SeqDense matrices and classes 7775 which inherit from SeqAIJ. C will be of type MATSEQAIJ. 7776 7777 Level: intermediate 7778 7779 .seealso: MatMatMultTransposeSymbolic(), MatMatMultTransposeNumeric(), MatPtAP() 7780 @*/ 7781 PetscErrorCode PETSCMAT_DLLEXPORT MatMatMultTranspose(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C) 7782 { 7783 PetscErrorCode ierr; 7784 PetscErrorCode (*fA)(Mat,Mat,MatReuse,PetscReal,Mat*); 7785 PetscErrorCode (*fB)(Mat,Mat,MatReuse,PetscReal,Mat*); 7786 7787 PetscFunctionBegin; 7788 PetscValidHeaderSpecific(A,MAT_COOKIE,1); 7789 PetscValidType(A,1); 7790 if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 7791 if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7792 PetscValidHeaderSpecific(B,MAT_COOKIE,2); 7793 PetscValidType(B,2); 7794 ierr = MatPreallocated(B);CHKERRQ(ierr); 7795 if (!B->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 7796 if (B->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7797 PetscValidPointer(C,3); 7798 if (B->rmap->N!=A->rmap->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",B->rmap->N,A->rmap->N); 7799 if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0; 7800 if (fill < 1.0) SETERRQ1(PETSC_ERR_ARG_SIZ,"Expected fill=%G must be > 1.0",fill); 7801 ierr = MatPreallocated(A);CHKERRQ(ierr); 7802 7803 fA = A->ops->matmulttranspose; 7804 if (!fA) SETERRQ1(PETSC_ERR_SUP,"MatMatMultTranspose not supported for A of type %s",((PetscObject)A)->type_name); 7805 fB = B->ops->matmulttranspose; 7806 if (!fB) SETERRQ1(PETSC_ERR_SUP,"MatMatMultTranspose not supported for B of type %s",((PetscObject)B)->type_name); 7807 if (fB!=fA) SETERRQ2(PETSC_ERR_ARG_INCOMP,"MatMatMultTranspose requires A, %s, to be compatible with B, %s",((PetscObject)A)->type_name,((PetscObject)B)->type_name); 7808 7809 ierr = PetscLogEventBegin(MAT_MatMultTranspose,A,B,0,0);CHKERRQ(ierr); 7810 ierr = (*A->ops->matmulttranspose)(A,B,scall,fill,C);CHKERRQ(ierr); 7811 ierr = PetscLogEventEnd(MAT_MatMultTranspose,A,B,0,0);CHKERRQ(ierr); 7812 7813 PetscFunctionReturn(0); 7814 } 7815 7816 #undef __FUNCT__ 7817 #define __FUNCT__ "MatGetRedundantMatrix" 7818 /*@C 7819 MatGetRedundantMatrix - Create redundant matrices and put them into processors of subcommunicators. 7820 7821 Collective on Mat 7822 7823 Input Parameters: 7824 + mat - the matrix 7825 . nsubcomm - the number of subcommunicators (= number of redundant pareallel or sequential matrices) 7826 . subcomm - MPI communicator split from the communicator where mat resides in 7827 . mlocal_red - number of local rows of the redundant matrix 7828 - reuse - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 7829 7830 Output Parameter: 7831 . matredundant - redundant matrix 7832 7833 Notes: 7834 MAT_REUSE_MATRIX can only be used when the nonzero structure of the 7835 original matrix has not changed from that last call to MatGetRedundantMatrix(). 7836 7837 This routine creates the duplicated matrices in subcommunicators; you should NOT create them before 7838 calling it. 7839 7840 Only MPIAIJ matrix is supported. 7841 7842 Level: advanced 7843 7844 Concepts: subcommunicator 7845 Concepts: duplicate matrix 7846 7847 .seealso: MatDestroy() 7848 @*/ 7849 PetscErrorCode PETSCMAT_DLLEXPORT MatGetRedundantMatrix(Mat mat,PetscInt nsubcomm,MPI_Comm subcomm,PetscInt mlocal_red,MatReuse reuse,Mat *matredundant) 7850 { 7851 PetscErrorCode ierr; 7852 7853 PetscFunctionBegin; 7854 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 7855 if (nsubcomm && reuse == MAT_REUSE_MATRIX) { 7856 PetscValidPointer(*matredundant,6); 7857 PetscValidHeaderSpecific(*matredundant,MAT_COOKIE,6); 7858 } 7859 if (!mat->ops->getredundantmatrix) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 7860 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 7861 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7862 ierr = MatPreallocated(mat);CHKERRQ(ierr); 7863 7864 ierr = PetscLogEventBegin(MAT_GetRedundantMatrix,mat,0,0,0);CHKERRQ(ierr); 7865 ierr = (*mat->ops->getredundantmatrix)(mat,nsubcomm,subcomm,mlocal_red,reuse,matredundant);CHKERRQ(ierr); 7866 ierr = PetscLogEventEnd(MAT_GetRedundantMatrix,mat,0,0,0);CHKERRQ(ierr); 7867 PetscFunctionReturn(0); 7868 } 7869