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