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