1 #ifdef PETSC_RCS_HEADER 2 static char vcid[] = "$Id: matrix.c,v 1.347 1999/09/29 00:54:10 bsmith Exp bsmith $"; 3 #endif 4 5 /* 6 This is where the abstract matrix operations are defined 7 */ 8 9 #include "src/mat/matimpl.h" /*I "mat.h" I*/ 10 #include "src/vec/vecimpl.h" 11 12 #undef __FUNC__ 13 #define __FUNC__ "MatGetRow" 14 /*@C 15 MatGetRow - Gets a row of a matrix. You MUST call MatRestoreRow() 16 for each row that you get to ensure that your application does 17 not bleed memory. 18 19 Not Collective 20 21 Input Parameters: 22 + mat - the matrix 23 - row - the row to get 24 25 Output Parameters: 26 + ncols - the number of nonzeros in the row 27 . cols - if nonzero, the column numbers 28 - vals - if nonzero, the values 29 30 Notes: 31 This routine is provided for people who need to have direct access 32 to the structure of a matrix. We hope that we provide enough 33 high-level matrix routines that few users will need it. 34 35 MatGetRow() always returns 0-based column indices, regardless of 36 whether the internal representation is 0-based (default) or 1-based. 37 38 For better efficiency, set cols and/or vals to PETSC_NULL if you do 39 not wish to extract these quantities. 40 41 The user can only examine the values extracted with MatGetRow(); 42 the values cannot be altered. To change the matrix entries, one 43 must use MatSetValues(). 44 45 You can only have one call to MatGetRow() outstanding for a particular 46 matrix at a time. 47 48 Fortran Notes: 49 The calling sequence from Fortran is 50 .vb 51 MatGetRow(matrix,row,ncols,cols,values,ierr) 52 Mat matrix (input) 53 integer row (input) 54 integer ncols (output) 55 integer cols(maxcols) (output) 56 double precision (or double complex) values(maxcols) output 57 .ve 58 where maxcols >= maximum nonzeros in any row of the matrix. 59 60 Caution: 61 Do not try to change the contents of the output arrays (cols and vals). 62 In some cases, this may corrupt the matrix. 63 64 Level: advanced 65 66 .keywords: matrix, row, get, extract 67 68 .seealso: MatRestoreRow(), MatSetValues(), MatGetValues(), MatGetSubmatrices(), MatGetDiagonal() 69 @*/ 70 int MatGetRow(Mat mat,int row,int *ncols,int **cols,Scalar **vals) 71 { 72 int ierr; 73 74 PetscFunctionBegin; 75 PetscValidHeaderSpecific(mat,MAT_COOKIE); 76 PetscValidIntPointer(ncols); 77 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 78 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 79 if (!mat->ops->getrow) SETERRQ(PETSC_ERR_SUP,0,""); 80 PLogEventBegin(MAT_GetRow,mat,0,0,0); 81 ierr = (*mat->ops->getrow)(mat,row,ncols,cols,vals);CHKERRQ(ierr); 82 PLogEventEnd(MAT_GetRow,mat,0,0,0); 83 PetscFunctionReturn(0); 84 } 85 86 #undef __FUNC__ 87 #define __FUNC__ "MatRestoreRow" 88 /*@C 89 MatRestoreRow - Frees any temporary space allocated by MatGetRow(). 90 91 Not Collective 92 93 Input Parameters: 94 + mat - the matrix 95 . row - the row to get 96 . ncols, cols - the number of nonzeros and their columns 97 - vals - if nonzero the column values 98 99 Notes: 100 This routine should be called after you have finished examining the entries. 101 102 Fortran Notes: 103 The calling sequence from Fortran is 104 .vb 105 MatRestoreRow(matrix,row,ncols,cols,values,ierr) 106 Mat matrix (input) 107 integer row (input) 108 integer ncols (output) 109 integer cols(maxcols) (output) 110 double precision (or double complex) values(maxcols) output 111 .ve 112 Where maxcols >= maximum nonzeros in any row of the matrix. 113 114 In Fortran MatRestoreRow() MUST be called after MatGetRow() 115 before another call to MatGetRow() can be made. 116 117 Level: advanced 118 119 .keywords: matrix, row, restore 120 121 .seealso: MatGetRow() 122 @*/ 123 int MatRestoreRow(Mat mat,int row,int *ncols,int **cols,Scalar **vals) 124 { 125 int ierr; 126 127 PetscFunctionBegin; 128 PetscValidHeaderSpecific(mat,MAT_COOKIE); 129 PetscValidIntPointer(ncols); 130 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 131 if (!mat->ops->restorerow) PetscFunctionReturn(0); 132 ierr = (*mat->ops->restorerow)(mat,row,ncols,cols,vals);CHKERRQ(ierr); 133 PetscFunctionReturn(0); 134 } 135 136 #undef __FUNC__ 137 #define __FUNC__ "MatView" 138 /*@C 139 MatView - Visualizes a matrix object. 140 141 Collective on Mat unless Viewer is VIEWER_STDOUT_SELF 142 143 Input Parameters: 144 + mat - the matrix 145 - ptr - visualization context 146 147 Notes: 148 The available visualization contexts include 149 + VIEWER_STDOUT_SELF - standard output (default) 150 . VIEWER_STDOUT_WORLD - synchronized standard 151 output where only the first processor opens 152 the file. All other processors send their 153 data to the first processor to print. 154 - VIEWER_DRAW_WORLD - graphical display of nonzero structure 155 156 The user can open alternative visualization contexts with 157 + ViewerASCIIOpen() - Outputs matrix to a specified file 158 . ViewerBinaryOpen() - Outputs matrix in binary to a 159 specified file; corresponding input uses MatLoad() 160 . ViewerDrawOpen() - Outputs nonzero matrix structure to 161 an X window display 162 - ViewerSocketOpen() - Outputs matrix to Socket viewer. 163 Currently only the sequential dense and AIJ 164 matrix types support the Socket viewer. 165 166 The user can call ViewerSetFormat() to specify the output 167 format of ASCII printed objects (when using VIEWER_STDOUT_SELF, 168 VIEWER_STDOUT_WORLD and ViewerASCIIOpen). Available formats include 169 + VIEWER_FORMAT_ASCII_DEFAULT - default, prints matrix contents 170 . VIEWER_FORMAT_ASCII_MATLAB - prints matrix contents in Matlab format 171 . VIEWER_FORMAT_ASCII_DENSE - prints entire matrix including zeros 172 . VIEWER_FORMAT_ASCII_COMMON - prints matrix contents, using a sparse 173 format common among all matrix types 174 . VIEWER_FORMAT_ASCII_IMPL - prints matrix contents, using an implementation-specific 175 format (which is in many cases the same as the default) 176 . VIEWER_FORMAT_ASCII_INFO - prints basic information about the matrix 177 size and structure (not the matrix entries) 178 - VIEWER_FORMAT_ASCII_INFO_LONG - prints more detailed information about 179 the matrix structure 180 181 Level: beginner 182 183 .keywords: matrix, view, visualize, output, print, write, draw 184 185 .seealso: ViewerSetFormat(), ViewerASCIIOpen(), ViewerDrawOpen(), 186 ViewerSocketOpen(), ViewerBinaryOpen(), MatLoad() 187 @*/ 188 int MatView(Mat mat,Viewer viewer) 189 { 190 int format, ierr, rows, cols; 191 char *cstr; 192 193 PetscFunctionBegin; 194 PetscValidHeaderSpecific(mat,MAT_COOKIE); 195 if (viewer) PetscValidHeaderSpecific(viewer,VIEWER_COOKIE); 196 197 if (!viewer) { 198 viewer = VIEWER_STDOUT_SELF; 199 } 200 201 if (PetscTypeCompare(viewer,ASCII_VIEWER)) { 202 ierr = ViewerGetFormat(viewer,&format);CHKERRQ(ierr); 203 if (format == VIEWER_FORMAT_ASCII_INFO || format == VIEWER_FORMAT_ASCII_INFO_LONG) { 204 ierr = ViewerASCIIPrintf(viewer,"Matrix Object:\n");CHKERRQ(ierr); 205 ierr = MatGetType(mat,PETSC_NULL,&cstr);CHKERRQ(ierr); 206 ierr = MatGetSize(mat,&rows,&cols);CHKERRQ(ierr); 207 ierr = ViewerASCIIPrintf(viewer," type=%s, rows=%d, cols=%d\n",cstr,rows,cols);CHKERRQ(ierr); 208 if (mat->ops->getinfo) { 209 MatInfo info; 210 ierr = MatGetInfo(mat,MAT_GLOBAL_SUM,&info);CHKERRQ(ierr); 211 ierr = ViewerASCIIPrintf(viewer," total: nonzeros=%d, allocated nonzeros=%d\n", 212 (int)info.nz_used,(int)info.nz_allocated);CHKERRQ(ierr); 213 } 214 } 215 } 216 if (mat->ops->view) { 217 ierr = ViewerASCIIPushTab(viewer);CHKERRQ(ierr); 218 ierr = (*mat->ops->view)(mat,viewer);CHKERRQ(ierr); 219 ierr = ViewerASCIIPopTab(viewer);CHKERRQ(ierr); 220 } 221 PetscFunctionReturn(0); 222 } 223 224 #undef __FUNC__ 225 #define __FUNC__ "MatScaleSystem" 226 /*@C 227 MatScaleSystem - Scale a vector solution and right hand side to 228 match the scaling of a scaled matrix. 229 230 Collective on Mat 231 232 Input Parameter: 233 + mat - the matrix 234 . x - solution vector (or PETSC_NULL) 235 + b - right hand side vector (or PETSC_NULL) 236 237 238 Notes: 239 For AIJ, BAIJ, and BDiag matrix formats, the matrices are not 240 internally scaled, so this does nothing. For MPIROWBS it 241 permutes and diagonally scales. 242 243 The SLES methods automatically call this routine when required 244 (via PCPreSolve()) so it is rarely used directly. 245 246 Level: Developer 247 248 .keywords: matrix, scale 249 250 .seealso: MatUseScaledForm(), MatUnScaleSystem() 251 @*/ 252 int MatScaleSystem(Mat mat,Vec x,Vec b) 253 { 254 int ierr; 255 256 PetscFunctionBegin; 257 PetscValidHeaderSpecific(mat,MAT_COOKIE); 258 if (x) {PetscValidHeaderSpecific(x,VEC_COOKIE);PetscCheckSameComm(mat,x);} 259 if (b) {PetscValidHeaderSpecific(b,VEC_COOKIE);PetscCheckSameComm(mat,b);} 260 261 if (mat->ops->scalesystem) { 262 ierr = (*mat->ops->scalesystem)(mat,x,b);CHKERRQ(ierr); 263 } 264 PetscFunctionReturn(0); 265 } 266 267 #undef __FUNC__ 268 #define __FUNC__ "MatUnScaleSystem" 269 /*@C 270 MatUnScaleSystem - Unscales a vector solution and right hand side to 271 match the original scaling of a scaled matrix. 272 273 Collective on Mat 274 275 Input Parameter: 276 + mat - the matrix 277 . x - solution vector (or PETSC_NULL) 278 + b - right hand side vector (or PETSC_NULL) 279 280 281 Notes: 282 For AIJ, BAIJ, and BDiag matrix formats, the matrices are not 283 internally scaled, so this does nothing. For MPIROWBS it 284 permutes and diagonally scales. 285 286 The SLES methods automatically call this routine when required 287 (via PCPreSolve()) so it is rarely used directly. 288 289 Level: Developer 290 291 .keywords: matrix, scale 292 293 .seealso: MatUseScaledForm(), MatScaleSystem() 294 @*/ 295 int MatUnScaleSystem(Mat mat,Vec x,Vec b) 296 { 297 int ierr; 298 299 PetscFunctionBegin; 300 PetscValidHeaderSpecific(mat,MAT_COOKIE); 301 if (x) {PetscValidHeaderSpecific(x,VEC_COOKIE);PetscCheckSameComm(mat,x);} 302 if (b) {PetscValidHeaderSpecific(b,VEC_COOKIE);PetscCheckSameComm(mat,x);} 303 if (mat->ops->unscalesystem) { 304 ierr = (*mat->ops->unscalesystem)(mat,x,b);CHKERRQ(ierr); 305 } 306 PetscFunctionReturn(0); 307 } 308 309 #undef __FUNC__ 310 #define __FUNC__ "MatUseScaledForm" 311 /*@C 312 MatUseScaledForm - For matrix storage formats that scale the 313 matrix (for example MPIRowBS matrices are diagonally scaled on 314 assembly) indicates matrix operations (MatMult() etc) are 315 applied using the scaled matrix. 316 317 Collective on Mat 318 319 Input Parameter: 320 + mat - the matrix 321 - scaled - PETSC_TRUE for applying the scaled, PETSC_FALSE for 322 applying the original matrix 323 324 Notes: 325 For scaled matrix formats, applying the original, unscaled matrix 326 will be slightly more expensive 327 328 Level: Developer 329 330 .keywords: matrix, scale 331 332 .seealso: MatScaleSystem(), MatUnScaleSystem() 333 @*/ 334 int MatUseScaledForm(Mat mat,PetscTruth scaled) 335 { 336 int ierr; 337 338 PetscFunctionBegin; 339 PetscValidHeaderSpecific(mat,MAT_COOKIE); 340 if (mat->ops->usescaledform) { 341 ierr = (*mat->ops->usescaledform)(mat,scaled);CHKERRQ(ierr); 342 } 343 PetscFunctionReturn(0); 344 } 345 346 #undef __FUNC__ 347 #define __FUNC__ "MatDestroy" 348 /*@C 349 MatDestroy - Frees space taken by a matrix. 350 351 Collective on Mat 352 353 Input Parameter: 354 . mat - the matrix 355 356 Level: beginner 357 358 .keywords: matrix, destroy 359 @*/ 360 int MatDestroy(Mat mat) 361 { 362 int ierr; 363 364 PetscFunctionBegin; 365 PetscValidHeaderSpecific(mat,MAT_COOKIE); 366 if (--mat->refct > 0) PetscFunctionReturn(0); 367 368 /* if memory was published with AMS then destroy it */ 369 ierr = PetscAMSDestroy(mat);CHKERRQ(ierr); 370 371 ierr = (*mat->ops->destroy)(mat);CHKERRQ(ierr); 372 PetscFunctionReturn(0); 373 } 374 375 #undef __FUNC__ 376 #define __FUNC__ "MatValid" 377 /*@ 378 MatValid - Checks whether a matrix object is valid. 379 380 Collective on Mat 381 382 Input Parameter: 383 . m - the matrix to check 384 385 Output Parameter: 386 flg - flag indicating matrix status, either 387 PETSC_TRUE if matrix is valid, or PETSC_FALSE otherwise. 388 389 Level: developer 390 391 .keywords: matrix, valid 392 @*/ 393 int MatValid(Mat m,PetscTruth *flg) 394 { 395 PetscFunctionBegin; 396 PetscValidIntPointer(flg); 397 if (!m) *flg = PETSC_FALSE; 398 else if (m->cookie != MAT_COOKIE) *flg = PETSC_FALSE; 399 else *flg = PETSC_TRUE; 400 PetscFunctionReturn(0); 401 } 402 403 #undef __FUNC__ 404 #define __FUNC__ "MatSetValues" 405 /*@ 406 MatSetValues - Inserts or adds a block of values into a matrix. 407 These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd() 408 MUST be called after all calls to MatSetValues() have been completed. 409 410 Not Collective 411 412 Input Parameters: 413 + mat - the matrix 414 . v - a logically two-dimensional array of values 415 . m, idxm - the number of rows and their global indices 416 . n, idxn - the number of columns and their global indices 417 - addv - either ADD_VALUES or INSERT_VALUES, where 418 ADD_VALUES adds values to any existing entries, and 419 INSERT_VALUES replaces existing entries with new values 420 421 Notes: 422 By default the values, v, are row-oriented and unsorted. 423 See MatSetOption() for other options. 424 425 Calls to MatSetValues() with the INSERT_VALUES and ADD_VALUES 426 options cannot be mixed without intervening calls to the assembly 427 routines. 428 429 MatSetValues() uses 0-based row and column numbers in Fortran 430 as well as in C. 431 432 Negative indices may be passed in idxm and idxn, these rows and columns are 433 simply ignored. This allows easily inserting element stiffness matrices 434 with homogeneous Dirchlet boundary conditions that you don't want represented 435 in the matrix. 436 437 Efficiency Alert: 438 The routine MatSetValuesBlocked() may offer much better efficiency 439 for users of block sparse formats (MATSEQBAIJ and MATMPIBAIJ). 440 441 Level: beginner 442 443 .keywords: matrix, insert, add, set, values 444 445 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal() 446 @*/ 447 int MatSetValues(Mat mat,int m,int *idxm,int n,int *idxn,Scalar *v,InsertMode addv) 448 { 449 int ierr; 450 451 PetscFunctionBegin; 452 if (!m || !n) PetscFunctionReturn(0); /* no values to insert */ 453 PetscValidHeaderSpecific(mat,MAT_COOKIE); 454 PetscValidIntPointer(idxm); 455 PetscValidIntPointer(idxn); 456 PetscValidScalarPointer(v); 457 if (mat->insertmode == NOT_SET_VALUES) { 458 mat->insertmode = addv; 459 } 460 #if defined(PETSC_USE_BOPT_g) 461 else if (mat->insertmode != addv) { 462 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,1,"Cannot mix add values and insert values"); 463 } 464 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 465 #endif 466 467 if (mat->assembled) { 468 mat->was_assembled = PETSC_TRUE; 469 mat->assembled = PETSC_FALSE; 470 } 471 PLogEventBegin(MAT_SetValues,mat,0,0,0); 472 if (!mat->ops->setvalues) SETERRQ(PETSC_ERR_SUP,1,"Not supported for this matrix type"); 473 ierr = (*mat->ops->setvalues)(mat,m,idxm,n,idxn,v,addv);CHKERRQ(ierr); 474 PLogEventEnd(MAT_SetValues,mat,0,0,0); 475 PetscFunctionReturn(0); 476 } 477 478 #undef __FUNC__ 479 #define __FUNC__ "MatSetValuesBlocked" 480 /*@ 481 MatSetValuesBlocked - Inserts or adds a block of values into a matrix. 482 483 Not Collective 484 485 Input Parameters: 486 + mat - the matrix 487 . v - a logically two-dimensional array of values 488 . m, idxm - the number of block rows and their global block indices 489 . n, idxn - the number of block columns and their global block indices 490 - addv - either ADD_VALUES or INSERT_VALUES, where 491 ADD_VALUES adds values to any existing entries, and 492 INSERT_VALUES replaces existing entries with new values 493 494 Notes: 495 By default the values, v, are row-oriented and unsorted. So the layout of 496 v is the same as for MatSetValues(). See MatSetOption() for other options. 497 498 Calls to MatSetValuesBlocked() with the INSERT_VALUES and ADD_VALUES 499 options cannot be mixed without intervening calls to the assembly 500 routines. 501 502 MatSetValuesBlocked() uses 0-based row and column numbers in Fortran 503 as well as in C. 504 505 Negative indices may be passed in idxm and idxn, these rows and columns are 506 simply ignored. This allows easily inserting element stiffness matrices 507 with homogeneous Dirchlet boundary conditions that you don't want represented 508 in the matrix. 509 510 Each time an entry is set within a sparse matrix via MatSetValues(), 511 internal searching must be done to determine where to place the the 512 data in the matrix storage space. By instead inserting blocks of 513 entries via MatSetValuesBlocked(), the overhead of matrix assembly is 514 reduced. 515 516 Restrictions: 517 MatSetValuesBlocked() is currently supported only for the block AIJ 518 matrix format (MATSEQBAIJ and MATMPIBAIJ, which are created via 519 MatCreateSeqBAIJ() and MatCreateMPIBAIJ()). 520 521 Level: intermediate 522 523 .keywords: matrix, insert, add, set, values 524 525 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesBlockedLocal() 526 @*/ 527 int MatSetValuesBlocked(Mat mat,int m,int *idxm,int n,int *idxn,Scalar *v,InsertMode addv) 528 { 529 int ierr; 530 531 PetscFunctionBegin; 532 if (!m || !n) PetscFunctionReturn(0); /* no values to insert */ 533 PetscValidHeaderSpecific(mat,MAT_COOKIE); 534 PetscValidIntPointer(idxm); 535 PetscValidIntPointer(idxn); 536 PetscValidScalarPointer(v); 537 if (mat->insertmode == NOT_SET_VALUES) { 538 mat->insertmode = addv; 539 } 540 #if defined(PETSC_USE_BOPT_g) 541 else if (mat->insertmode != addv) { 542 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,1,"Cannot mix add values and insert values"); 543 } 544 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 545 #endif 546 547 if (mat->assembled) { 548 mat->was_assembled = PETSC_TRUE; 549 mat->assembled = PETSC_FALSE; 550 } 551 PLogEventBegin(MAT_SetValues,mat,0,0,0); 552 if (!mat->ops->setvaluesblocked) SETERRQ(PETSC_ERR_SUP,1,"Not supported for this matrix type"); 553 ierr = (*mat->ops->setvaluesblocked)(mat,m,idxm,n,idxn,v,addv);CHKERRQ(ierr); 554 PLogEventEnd(MAT_SetValues,mat,0,0,0); 555 PetscFunctionReturn(0); 556 } 557 558 /*MC 559 MatSetValue - Set a single entry into a matrix. 560 561 Synopsis: 562 void MatSetValue(Mat m,int row,int col,Scalar value,InsertMode mode); 563 564 Not collective 565 566 Input Parameters: 567 + m - the matrix 568 . row - the row location of the entry 569 . col - the column location of the entry 570 . value - the value to insert 571 - mode - either INSERT_VALUES or ADD_VALUES 572 573 Notes: 574 For efficiency one should use MatSetValues() and set several or many 575 values simultaneously if possible. 576 577 Note that VecSetValue() does NOT return an error code (since this 578 is checked internally). 579 580 Level: beginner 581 582 .seealso: MatSetValues() 583 M*/ 584 585 #undef __FUNC__ 586 #define __FUNC__ "MatGetValues" 587 /*@ 588 MatGetValues - Gets a block of values from a matrix. 589 590 Not Collective; currently only returns a local block 591 592 Input Parameters: 593 + mat - the matrix 594 . v - a logically two-dimensional array for storing the values 595 . m, idxm - the number of rows and their global indices 596 - n, idxn - the number of columns and their global indices 597 598 Notes: 599 The user must allocate space (m*n Scalars) for the values, v. 600 The values, v, are then returned in a row-oriented format, 601 analogous to that used by default in MatSetValues(). 602 603 MatGetValues() uses 0-based row and column numbers in 604 Fortran as well as in C. 605 606 MatGetValues() requires that the matrix has been assembled 607 with MatAssemblyBegin()/MatAssemblyEnd(). Thus, calls to 608 MatSetValues() and MatGetValues() CANNOT be made in succession 609 without intermediate matrix assembly. 610 611 Level: advanced 612 613 .keywords: matrix, get, values 614 615 .seealso: MatGetRow(), MatGetSubmatrices(), MatSetValues() 616 @*/ 617 int MatGetValues(Mat mat,int m,int *idxm,int n,int *idxn,Scalar *v) 618 { 619 int ierr; 620 621 PetscFunctionBegin; 622 PetscValidHeaderSpecific(mat,MAT_COOKIE); 623 PetscValidIntPointer(idxm); 624 PetscValidIntPointer(idxn); 625 PetscValidScalarPointer(v); 626 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 627 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 628 if (!mat->ops->getvalues) SETERRQ(PETSC_ERR_SUP,0,""); 629 630 PLogEventBegin(MAT_GetValues,mat,0,0,0); 631 ierr = (*mat->ops->getvalues)(mat,m,idxm,n,idxn,v);CHKERRQ(ierr); 632 PLogEventEnd(MAT_GetValues,mat,0,0,0); 633 PetscFunctionReturn(0); 634 } 635 636 #undef __FUNC__ 637 #define __FUNC__ "MatSetLocalToGlobalMapping" 638 /*@ 639 MatSetLocalToGlobalMapping - Sets a local-to-global numbering for use by 640 the routine MatSetValuesLocal() to allow users to insert matrix entries 641 using a local (per-processor) numbering. 642 643 Not Collective 644 645 Input Parameters: 646 + x - the matrix 647 - mapping - mapping created with ISLocalToGlobalMappingCreate() 648 or ISLocalToGlobalMappingCreateIS() 649 650 Level: intermediate 651 652 .keywords: matrix, set, values, local, global, mapping 653 654 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesLocal() 655 @*/ 656 int MatSetLocalToGlobalMapping(Mat x,ISLocalToGlobalMapping mapping) 657 { 658 PetscFunctionBegin; 659 PetscValidHeaderSpecific(x,MAT_COOKIE); 660 PetscValidHeaderSpecific(mapping,IS_LTOGM_COOKIE); 661 if (x->mapping) { 662 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Mapping already set for matrix"); 663 } 664 665 x->mapping = mapping; 666 PetscObjectReference((PetscObject)mapping); 667 PetscFunctionReturn(0); 668 } 669 670 #undef __FUNC__ 671 #define __FUNC__ "MatSetLocalToGlobalMappingBlock" 672 /*@ 673 MatSetLocalToGlobalMappingBlock - Sets a local-to-global numbering for use 674 by the routine MatSetValuesBlockedLocal() to allow users to insert matrix 675 entries using a local (per-processor) numbering. 676 677 Not Collective 678 679 Input Parameters: 680 + x - the matrix 681 - mapping - mapping created with ISLocalToGlobalMappingCreate() or 682 ISLocalToGlobalMappingCreateIS() 683 684 Level: intermediate 685 686 .keywords: matrix, set, values, local ordering 687 688 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesBlockedLocal(), 689 MatSetValuesBlocked(), MatSetValuesLocal() 690 @*/ 691 int MatSetLocalToGlobalMappingBlock(Mat x,ISLocalToGlobalMapping mapping) 692 { 693 PetscFunctionBegin; 694 PetscValidHeaderSpecific(x,MAT_COOKIE); 695 PetscValidHeaderSpecific(mapping,IS_LTOGM_COOKIE); 696 if (x->bmapping) { 697 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Mapping already set for matrix"); 698 } 699 700 x->bmapping = mapping; 701 PetscObjectReference((PetscObject)mapping); 702 PetscFunctionReturn(0); 703 } 704 705 #undef __FUNC__ 706 #define __FUNC__ "MatSetValuesLocal" 707 /*@ 708 MatSetValuesLocal - Inserts or adds values into certain locations of a matrix, 709 using a local ordering of the nodes. 710 711 Not Collective 712 713 Input Parameters: 714 + x - the matrix 715 . nrow, irow - number of rows and their local indices 716 . ncol, icol - number of columns and their local indices 717 . y - a logically two-dimensional array of values 718 - addv - either INSERT_VALUES or ADD_VALUES, where 719 ADD_VALUES adds values to any existing entries, and 720 INSERT_VALUES replaces existing entries with new values 721 722 Notes: 723 Before calling MatSetValuesLocal(), the user must first set the 724 local-to-global mapping by calling MatSetLocalToGlobalMapping(). 725 726 Calls to MatSetValuesLocal() with the INSERT_VALUES and ADD_VALUES 727 options cannot be mixed without intervening calls to the assembly 728 routines. 729 730 These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd() 731 MUST be called after all calls to MatSetValuesLocal() have been completed. 732 733 Level: intermediate 734 735 .keywords: matrix, set, values, local ordering 736 737 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetLocalToGlobalMapping() 738 @*/ 739 int MatSetValuesLocal(Mat mat,int nrow,int *irow,int ncol, int *icol,Scalar *y,InsertMode addv) 740 { 741 int ierr,irowm[2048],icolm[2048]; 742 743 PetscFunctionBegin; 744 PetscValidHeaderSpecific(mat,MAT_COOKIE); 745 PetscValidIntPointer(irow); 746 PetscValidIntPointer(icol); 747 PetscValidScalarPointer(y); 748 749 if (mat->insertmode == NOT_SET_VALUES) { 750 mat->insertmode = addv; 751 } 752 #if defined(PETSC_USE_BOPT_g) 753 else if (mat->insertmode != addv) { 754 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,1,"Cannot mix add values and insert values"); 755 } 756 if (!mat->mapping) { 757 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Local to global never set with MatSetLocalToGlobalMapping()"); 758 } 759 if (nrow > 2048 || ncol > 2048) { 760 SETERRQ2(PETSC_ERR_SUP,0,"Number column/row indices must be <= 2048: are %d %d",nrow,ncol); 761 } 762 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 763 #endif 764 765 if (mat->assembled) { 766 mat->was_assembled = PETSC_TRUE; 767 mat->assembled = PETSC_FALSE; 768 } 769 PLogEventBegin(MAT_SetValues,mat,0,0,0); 770 ierr = ISLocalToGlobalMappingApply(mat->mapping,nrow,irow,irowm);CHKERRQ(ierr); 771 ierr = ISLocalToGlobalMappingApply(mat->mapping,ncol,icol,icolm);CHKERRQ(ierr); 772 ierr = (*mat->ops->setvalues)(mat,nrow,irowm,ncol,icolm,y,addv);CHKERRQ(ierr); 773 PLogEventEnd(MAT_SetValues,mat,0,0,0); 774 PetscFunctionReturn(0); 775 } 776 777 #undef __FUNC__ 778 #define __FUNC__ "MatSetValuesBlockedLocal" 779 /*@ 780 MatSetValuesBlockedLocal - Inserts or adds values into certain locations of a matrix, 781 using a local ordering of the nodes a block at a time. 782 783 Not Collective 784 785 Input Parameters: 786 + x - the matrix 787 . nrow, irow - number of rows and their local indices 788 . ncol, icol - number of columns and their local indices 789 . y - a logically two-dimensional array of values 790 - addv - either INSERT_VALUES or ADD_VALUES, where 791 ADD_VALUES adds values to any existing entries, and 792 INSERT_VALUES replaces existing entries with new values 793 794 Notes: 795 Before calling MatSetValuesBlockedLocal(), the user must first set the 796 local-to-global mapping by calling MatSetLocalToGlobalMappingBlock(), 797 where the mapping MUST be set for matrix blocks, not for matrix elements. 798 799 Calls to MatSetValuesBlockedLocal() with the INSERT_VALUES and ADD_VALUES 800 options cannot be mixed without intervening calls to the assembly 801 routines. 802 803 These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd() 804 MUST be called after all calls to MatSetValuesBlockedLocal() have been completed. 805 806 Level: intermediate 807 808 .keywords: matrix, set, values, blocked, local 809 810 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesLocal(), MatSetLocalToGlobalMappingBlock(), MatSetValuesBlocked() 811 @*/ 812 int MatSetValuesBlockedLocal(Mat mat,int nrow,int *irow,int ncol,int *icol,Scalar *y,InsertMode addv) 813 { 814 int ierr,irowm[2048],icolm[2048]; 815 816 PetscFunctionBegin; 817 PetscValidHeaderSpecific(mat,MAT_COOKIE); 818 PetscValidIntPointer(irow); 819 PetscValidIntPointer(icol); 820 PetscValidScalarPointer(y); 821 if (mat->insertmode == NOT_SET_VALUES) { 822 mat->insertmode = addv; 823 } 824 #if defined(PETSC_USE_BOPT_g) 825 else if (mat->insertmode != addv) { 826 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,1,"Cannot mix add values and insert values"); 827 } 828 if (!mat->bmapping) { 829 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Local to global never set with MatSetLocalToGlobalMappingBlock()"); 830 } 831 if (nrow > 2048 || ncol > 2048) { 832 SETERRQ2(PETSC_ERR_SUP,0,"Number column/row indices must be <= 2048: are %d %d",nrow,ncol); 833 } 834 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 835 #endif 836 837 if (mat->assembled) { 838 mat->was_assembled = PETSC_TRUE; 839 mat->assembled = PETSC_FALSE; 840 } 841 PLogEventBegin(MAT_SetValues,mat,0,0,0); 842 ierr = ISLocalToGlobalMappingApply(mat->bmapping,nrow,irow,irowm);CHKERRQ(ierr); 843 ierr = ISLocalToGlobalMappingApply(mat->bmapping,ncol,icol,icolm);CHKERRQ(ierr); 844 ierr = (*mat->ops->setvaluesblocked)(mat,nrow,irowm,ncol,icolm,y,addv);CHKERRQ(ierr); 845 PLogEventEnd(MAT_SetValues,mat,0,0,0); 846 PetscFunctionReturn(0); 847 } 848 849 /* --------------------------------------------------------*/ 850 #undef __FUNC__ 851 #define __FUNC__ "MatMult" 852 /*@ 853 MatMult - Computes the matrix-vector product, y = Ax. 854 855 Collective on Mat and Vec 856 857 Input Parameters: 858 + mat - the matrix 859 - x - the vector to be multilplied 860 861 Output Parameters: 862 . y - the result 863 864 Notes: 865 The vectors x and y cannot be the same. I.e., one cannot 866 call MatMult(A,y,y). 867 868 Level: beginner 869 870 .keywords: matrix, multiply, matrix-vector product 871 872 .seealso: MatMultTrans(), MatMultAdd(), MatMultTransAdd() 873 @*/ 874 int MatMult(Mat mat,Vec x,Vec y) 875 { 876 int ierr; 877 878 PetscFunctionBegin; 879 PetscValidHeaderSpecific(mat,MAT_COOKIE); 880 PetscValidHeaderSpecific(x,VEC_COOKIE); 881 PetscValidHeaderSpecific(y,VEC_COOKIE); 882 PetscCheckSameComm(mat,x); 883 PetscCheckSameComm(mat,y); 884 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 885 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 886 if (x == y) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"x and y must be different vectors"); 887 if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec x: global dim %d %d",mat->N,x->N); 888 if (mat->M != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec y: global dim %d %d",mat->M,y->N); 889 if (mat->m != y->n) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec y: local dim %d %d",mat->m,y->n); 890 891 PLogEventBegin(MAT_Mult,mat,x,y,0); 892 ierr = (*mat->ops->mult)(mat,x,y);CHKERRQ(ierr); 893 PLogEventEnd(MAT_Mult,mat,x,y,0); 894 895 PetscFunctionReturn(0); 896 } 897 898 #undef __FUNC__ 899 #define __FUNC__ "MatMultTrans" 900 /*@ 901 MatMultTrans - Computes matrix transpose times a vector. 902 903 Collective on Mat and Vec 904 905 Input Parameters: 906 + mat - the matrix 907 - x - the vector to be multilplied 908 909 Output Parameters: 910 . y - the result 911 912 Notes: 913 The vectors x and y cannot be the same. I.e., one cannot 914 call MatMultTrans(A,y,y). 915 916 Level: beginner 917 918 .keywords: matrix, multiply, matrix-vector product, transpose 919 920 .seealso: MatMult(), MatMultAdd(), MatMultTransAdd() 921 @*/ 922 int MatMultTrans(Mat mat,Vec x,Vec y) 923 { 924 int ierr; 925 926 PetscFunctionBegin; 927 PetscValidHeaderSpecific(mat,MAT_COOKIE); 928 PetscValidHeaderSpecific(x,VEC_COOKIE); 929 PetscValidHeaderSpecific(y,VEC_COOKIE); 930 PetscCheckSameComm(mat,x); 931 PetscCheckSameComm(mat,y); 932 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 933 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 934 if (x == y) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"x and y must be different vectors"); 935 if (mat->M != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec x: global dim %d %d",mat->M,x->N); 936 if (mat->N != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec y: global dim %d %d",mat->N,y->N); 937 938 PLogEventBegin(MAT_MultTrans,mat,x,y,0); 939 ierr = (*mat->ops->multtrans)(mat,x,y);CHKERRQ(ierr); 940 PLogEventEnd(MAT_MultTrans,mat,x,y,0); 941 PetscFunctionReturn(0); 942 } 943 944 #undef __FUNC__ 945 #define __FUNC__ "MatMultAdd" 946 /*@ 947 MatMultAdd - Computes v3 = v2 + A * v1. 948 949 Collective on Mat and Vec 950 951 Input Parameters: 952 + mat - the matrix 953 - v1, v2 - the vectors 954 955 Output Parameters: 956 . v3 - the result 957 958 Notes: 959 The vectors v1 and v3 cannot be the same. I.e., one cannot 960 call MatMultAdd(A,v1,v2,v1). 961 962 Level: beginner 963 964 .keywords: matrix, multiply, matrix-vector product, add 965 966 .seealso: MatMultTrans(), MatMult(), MatMultTransAdd() 967 @*/ 968 int MatMultAdd(Mat mat,Vec v1,Vec v2,Vec v3) 969 { 970 int ierr; 971 972 PetscFunctionBegin; 973 PetscValidHeaderSpecific(mat,MAT_COOKIE); 974 PetscValidHeaderSpecific(v1,VEC_COOKIE); 975 PetscValidHeaderSpecific(v2,VEC_COOKIE); 976 PetscValidHeaderSpecific(v3,VEC_COOKIE); 977 PetscCheckSameComm(mat,v1); 978 PetscCheckSameComm(mat,v2); 979 PetscCheckSameComm(mat,v3); 980 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 981 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 982 if (mat->N != v1->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec v1: global dim %d %d",mat->N,v1->N); 983 if (mat->M != v2->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec v2: global dim %d %d",mat->M,v2->N); 984 if (mat->M != v3->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec v3: global dim %d %d",mat->M,v3->N); 985 if (mat->m != v3->n) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec v3: local dim %d %d",mat->m,v3->n); 986 if (mat->m != v2->n) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec v2: local dim %d %d",mat->m,v2->n); 987 if (v1 == v3) SETERRQ(PETSC_ERR_ARG_IDN,0,"v1 and v3 must be different vectors"); 988 989 PLogEventBegin(MAT_MultAdd,mat,v1,v2,v3); 990 ierr = (*mat->ops->multadd)(mat,v1,v2,v3);CHKERRQ(ierr); 991 PLogEventEnd(MAT_MultAdd,mat,v1,v2,v3); 992 PetscFunctionReturn(0); 993 } 994 995 #undef __FUNC__ 996 #define __FUNC__ "MatMultTransAdd" 997 /*@ 998 MatMultTransAdd - Computes v3 = v2 + A' * v1. 999 1000 Collective on Mat and Vec 1001 1002 Input Parameters: 1003 + mat - the matrix 1004 - v1, v2 - the vectors 1005 1006 Output Parameters: 1007 . v3 - the result 1008 1009 Notes: 1010 The vectors v1 and v3 cannot be the same. I.e., one cannot 1011 call MatMultTransAdd(A,v1,v2,v1). 1012 1013 Level: beginner 1014 1015 .keywords: matrix, multiply, matrix-vector product, transpose, add 1016 1017 .seealso: MatMultTrans(), MatMultAdd(), MatMult() 1018 @*/ 1019 int MatMultTransAdd(Mat mat,Vec v1,Vec v2,Vec v3) 1020 { 1021 int ierr; 1022 1023 PetscFunctionBegin; 1024 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1025 PetscValidHeaderSpecific(v1,VEC_COOKIE); 1026 PetscValidHeaderSpecific(v2,VEC_COOKIE); 1027 PetscValidHeaderSpecific(v3,VEC_COOKIE); 1028 PetscCheckSameComm(mat,v1); 1029 PetscCheckSameComm(mat,v2); 1030 PetscCheckSameComm(mat,v3); 1031 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 1032 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 1033 if (!mat->ops->multtransadd) SETERRQ(PETSC_ERR_SUP,0,""); 1034 if (v1 == v3) SETERRQ(PETSC_ERR_ARG_IDN,0,"v1 and v3 must be different vectors"); 1035 if (mat->M != v1->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec v1: global dim %d %d",mat->M,v1->N); 1036 if (mat->N != v2->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec v2: global dim %d %d",mat->N,v2->N); 1037 if (mat->N != v3->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec v3: global dim %d %d",mat->N,v3->N); 1038 1039 PLogEventBegin(MAT_MultTransAdd,mat,v1,v2,v3); 1040 ierr = (*mat->ops->multtransadd)(mat,v1,v2,v3);CHKERRQ(ierr); 1041 PLogEventEnd(MAT_MultTransAdd,mat,v1,v2,v3); 1042 PetscFunctionReturn(0); 1043 } 1044 /* ------------------------------------------------------------*/ 1045 #undef __FUNC__ 1046 #define __FUNC__ "MatGetInfo" 1047 /*@C 1048 MatGetInfo - Returns information about matrix storage (number of 1049 nonzeros, memory, etc.). 1050 1051 Collective on Mat if MAT_GLOBAL_MAX or MAT_GLOBAL_SUM is used 1052 as the flag 1053 1054 Input Parameters: 1055 . mat - the matrix 1056 1057 Output Parameters: 1058 + flag - flag indicating the type of parameters to be returned 1059 (MAT_LOCAL - local matrix, MAT_GLOBAL_MAX - maximum over all processors, 1060 MAT_GLOBAL_SUM - sum over all processors) 1061 - info - matrix information context 1062 1063 Notes: 1064 The MatInfo context contains a variety of matrix data, including 1065 number of nonzeros allocated and used, number of mallocs during 1066 matrix assembly, etc. Additional information for factored matrices 1067 is provided (such as the fill ratio, number of mallocs during 1068 factorization, etc.). Much of this info is printed to STDOUT 1069 when using the runtime options 1070 $ -log_info -mat_view_info 1071 1072 Example for C/C++ Users: 1073 See the file ${PETSC_DIR}/include/mat.h for a complete list of 1074 data within the MatInfo context. For example, 1075 .vb 1076 MatInfo info; 1077 Mat A; 1078 double mal, nz_a, nz_u; 1079 1080 MatGetInfo(A,MAT_LOCAL,&info); 1081 mal = info.mallocs; 1082 nz_a = info.nz_allocated; 1083 .ve 1084 1085 Example for Fortran Users: 1086 Fortran users should declare info as a double precision 1087 array of dimension MAT_INFO_SIZE, and then extract the parameters 1088 of interest. See the file ${PETSC_DIR}/include/finclude/mat.h 1089 a complete list of parameter names. 1090 .vb 1091 double precision info(MAT_INFO_SIZE) 1092 double precision mal, nz_a 1093 Mat A 1094 integer ierr 1095 1096 call MatGetInfo(A,MAT_LOCAL,info,ierr) 1097 mal = info(MAT_INFO_MALLOCS) 1098 nz_a = info(MAT_INFO_NZ_ALLOCATED) 1099 .ve 1100 1101 Level: intermediate 1102 1103 .keywords: matrix, get, info, storage, nonzeros, memory, fill 1104 @*/ 1105 int MatGetInfo(Mat mat,MatInfoType flag,MatInfo *info) 1106 { 1107 int ierr; 1108 1109 PetscFunctionBegin; 1110 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1111 PetscValidPointer(info); 1112 if (!mat->ops->getinfo) SETERRQ(PETSC_ERR_SUP,0,""); 1113 ierr = (*mat->ops->getinfo)(mat,flag,info);CHKERRQ(ierr); 1114 PetscFunctionReturn(0); 1115 } 1116 1117 /* ----------------------------------------------------------*/ 1118 #undef __FUNC__ 1119 #define __FUNC__ "MatILUDTFactor" 1120 /*@ 1121 MatILUDTFactor - Performs a drop tolerance ILU factorization. 1122 1123 Collective on Mat 1124 1125 Input Parameters: 1126 + mat - the matrix 1127 . dt - the drop tolerance 1128 . maxnz - the maximum number of nonzeros per row allowed 1129 . row - row permutation 1130 - col - column permutation 1131 1132 Output Parameters: 1133 . fact - the factored matrix 1134 1135 Notes: 1136 Most users should employ the simplified SLES interface for linear solvers 1137 instead of working directly with matrix algebra routines such as this. 1138 See, e.g., SLESCreate(). 1139 1140 Level: developer 1141 1142 .keywords: matrix, factor, LU, in-place 1143 1144 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor() 1145 @*/ 1146 int MatILUDTFactor(Mat mat,double dt,int maxnz,IS row,IS col,Mat *fact) 1147 { 1148 int ierr; 1149 1150 PetscFunctionBegin; 1151 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1152 PetscValidPointer(fact); 1153 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 1154 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 1155 if (!mat->ops->iludtfactor) SETERRQ(PETSC_ERR_SUP,0,""); 1156 1157 PLogEventBegin(MAT_ILUFactor,mat,row,col,0); 1158 ierr = (*mat->ops->iludtfactor)(mat,dt,maxnz,row,col,fact);CHKERRQ(ierr); 1159 PLogEventEnd(MAT_ILUFactor,mat,row,col,0); 1160 1161 PetscFunctionReturn(0); 1162 } 1163 1164 #undef __FUNC__ 1165 #define __FUNC__ "MatLUFactor" 1166 /*@ 1167 MatLUFactor - Performs in-place LU factorization of matrix. 1168 1169 Collective on Mat 1170 1171 Input Parameters: 1172 + mat - the matrix 1173 . row - row permutation 1174 . col - column permutation 1175 - f - expected fill as ratio of original fill. 1176 1177 Notes: 1178 Most users should employ the simplified SLES interface for linear solvers 1179 instead of working directly with matrix algebra routines such as this. 1180 See, e.g., SLESCreate(). 1181 1182 Level: developer 1183 1184 .keywords: matrix, factor, LU, in-place 1185 1186 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor(), 1187 MatGetOrdering() 1188 1189 @*/ 1190 int MatLUFactor(Mat mat,IS row,IS col,double f) 1191 { 1192 int ierr; 1193 1194 PetscFunctionBegin; 1195 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1196 if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,0,"matrix must be square"); 1197 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 1198 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 1199 if (!mat->ops->lufactor) SETERRQ(PETSC_ERR_SUP,0,""); 1200 1201 PLogEventBegin(MAT_LUFactor,mat,row,col,0); 1202 ierr = (*mat->ops->lufactor)(mat,row,col,f);CHKERRQ(ierr); 1203 PLogEventEnd(MAT_LUFactor,mat,row,col,0); 1204 PetscFunctionReturn(0); 1205 } 1206 1207 #undef __FUNC__ 1208 #define __FUNC__ "MatILUFactor" 1209 /*@ 1210 MatILUFactor - Performs in-place ILU factorization of matrix. 1211 1212 Collective on Mat 1213 1214 Input Parameters: 1215 + mat - the matrix 1216 . row - row permutation 1217 . col - column permutation 1218 - info - structure containing 1219 $ levels - number of levels of fill. 1220 $ expected fill - as ratio of original fill. 1221 $ 1 or 0 - indicating force fill on diagonal (improves robustness for matrices 1222 missing diagonal entries) 1223 1224 Notes: 1225 Probably really in-place only when level of fill is zero, otherwise allocates 1226 new space to store factored matrix and deletes previous memory. 1227 1228 Most users should employ the simplified SLES interface for linear solvers 1229 instead of working directly with matrix algebra routines such as this. 1230 See, e.g., SLESCreate(). 1231 1232 Level: developer 1233 1234 .keywords: matrix, factor, ILU, in-place 1235 1236 .seealso: MatILUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor() 1237 @*/ 1238 int MatILUFactor(Mat mat,IS row,IS col,MatILUInfo *info) 1239 { 1240 int ierr; 1241 1242 PetscFunctionBegin; 1243 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1244 if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,0,"matrix must be square"); 1245 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 1246 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 1247 if (!mat->ops->ilufactor) SETERRQ(PETSC_ERR_SUP,0,""); 1248 1249 PLogEventBegin(MAT_ILUFactor,mat,row,col,0); 1250 ierr = (*mat->ops->ilufactor)(mat,row,col,info);CHKERRQ(ierr); 1251 PLogEventEnd(MAT_ILUFactor,mat,row,col,0); 1252 PetscFunctionReturn(0); 1253 } 1254 1255 #undef __FUNC__ 1256 #define __FUNC__ "MatLUFactorSymbolic" 1257 /*@ 1258 MatLUFactorSymbolic - Performs symbolic LU factorization of matrix. 1259 Call this routine before calling MatLUFactorNumeric(). 1260 1261 Collective on Mat 1262 1263 Input Parameters: 1264 + mat - the matrix 1265 . row, col - row and column permutations 1266 - f - expected fill as ratio of the original number of nonzeros, 1267 for example 3.0; choosing this parameter well can result in 1268 more efficient use of time and space. Run with the option -log_info 1269 to determine an optimal value to use 1270 1271 Output Parameter: 1272 . fact - new matrix that has been symbolically factored 1273 1274 Notes: 1275 See the users manual for additional information about 1276 choosing the fill factor for better efficiency. 1277 1278 Most users should employ the simplified SLES interface for linear solvers 1279 instead of working directly with matrix algebra routines such as this. 1280 See, e.g., SLESCreate(). 1281 1282 Level: developer 1283 1284 .keywords: matrix, factor, LU, symbolic, fill 1285 1286 .seealso: MatLUFactor(), MatLUFactorNumeric(), MatCholeskyFactor() 1287 @*/ 1288 int MatLUFactorSymbolic(Mat mat,IS row,IS col,double f,Mat *fact) 1289 { 1290 int ierr; 1291 1292 PetscFunctionBegin; 1293 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1294 if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,0,"matrix must be square"); 1295 PetscValidPointer(fact); 1296 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 1297 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 1298 if (!mat->ops->lufactorsymbolic) SETERRQ(PETSC_ERR_SUP,0,""); 1299 1300 PLogEventBegin(MAT_LUFactorSymbolic,mat,row,col,0); 1301 ierr = (*mat->ops->lufactorsymbolic)(mat,row,col,f,fact);CHKERRQ(ierr); 1302 PLogEventEnd(MAT_LUFactorSymbolic,mat,row,col,0); 1303 PetscFunctionReturn(0); 1304 } 1305 1306 #undef __FUNC__ 1307 #define __FUNC__ "MatLUFactorNumeric" 1308 /*@ 1309 MatLUFactorNumeric - Performs numeric LU factorization of a matrix. 1310 Call this routine after first calling MatLUFactorSymbolic(). 1311 1312 Collective on Mat 1313 1314 Input Parameters: 1315 + mat - the matrix 1316 - row, col - row and column permutations 1317 1318 Output Parameters: 1319 . fact - symbolically factored matrix that must have been generated 1320 by MatLUFactorSymbolic() 1321 1322 Notes: 1323 See MatLUFactor() for in-place factorization. See 1324 MatCholeskyFactorNumeric() for the symmetric, positive definite case. 1325 1326 Most users should employ the simplified SLES interface for linear solvers 1327 instead of working directly with matrix algebra routines such as this. 1328 See, e.g., SLESCreate(). 1329 1330 Level: developer 1331 1332 .keywords: matrix, factor, LU, numeric 1333 1334 .seealso: MatLUFactorSymbolic(), MatLUFactor(), MatCholeskyFactor() 1335 @*/ 1336 int MatLUFactorNumeric(Mat mat,Mat *fact) 1337 { 1338 int ierr,flg; 1339 1340 PetscFunctionBegin; 1341 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1342 PetscValidPointer(fact); 1343 PetscValidHeaderSpecific(*fact,MAT_COOKIE); 1344 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 1345 if (mat->M != (*fact)->M || mat->N != (*fact)->N) { 1346 SETERRQ4(PETSC_ERR_ARG_SIZ,0,"Mat mat,Mat *fact: global dimensions are different %d should = %d %d should = %d", 1347 mat->M,(*fact)->M,mat->N,(*fact)->N); 1348 } 1349 if (!(*fact)->ops->lufactornumeric) SETERRQ(PETSC_ERR_SUP,0,""); 1350 1351 PLogEventBegin(MAT_LUFactorNumeric,mat,*fact,0,0); 1352 ierr = (*(*fact)->ops->lufactornumeric)(mat,fact);CHKERRQ(ierr); 1353 PLogEventEnd(MAT_LUFactorNumeric,mat,*fact,0,0); 1354 ierr = OptionsHasName(PETSC_NULL,"-mat_view_draw",&flg);CHKERRQ(ierr); 1355 if (flg) { 1356 ierr = OptionsHasName(PETSC_NULL,"-mat_view_contour",&flg);CHKERRQ(ierr); 1357 if (flg) { 1358 ViewerPushFormat(VIEWER_DRAW_(mat->comm),VIEWER_FORMAT_DRAW_CONTOUR,0);CHKERRQ(ierr); 1359 } 1360 ierr = MatView(*fact,VIEWER_DRAW_(mat->comm));CHKERRQ(ierr); 1361 ierr = ViewerFlush(VIEWER_DRAW_(mat->comm));CHKERRQ(ierr); 1362 if (flg) { 1363 ViewerPopFormat(VIEWER_DRAW_(mat->comm));CHKERRQ(ierr); 1364 } 1365 } 1366 PetscFunctionReturn(0); 1367 } 1368 1369 #undef __FUNC__ 1370 #define __FUNC__ "MatCholeskyFactor" 1371 /*@ 1372 MatCholeskyFactor - Performs in-place Cholesky factorization of a 1373 symmetric matrix. 1374 1375 Collective on Mat 1376 1377 Input Parameters: 1378 + mat - the matrix 1379 . perm - row and column permutations 1380 - f - expected fill as ratio of original fill 1381 1382 Notes: 1383 See MatLUFactor() for the nonsymmetric case. See also 1384 MatCholeskyFactorSymbolic(), and MatCholeskyFactorNumeric(). 1385 1386 Most users should employ the simplified SLES interface for linear solvers 1387 instead of working directly with matrix algebra routines such as this. 1388 See, e.g., SLESCreate(). 1389 1390 Level: developer 1391 1392 .keywords: matrix, factor, in-place, Cholesky 1393 1394 .seealso: MatLUFactor(), MatCholeskyFactorSymbolic(), MatCholeskyFactorNumeric() 1395 MatGetOrdering() 1396 1397 @*/ 1398 int MatCholeskyFactor(Mat mat,IS perm,double f) 1399 { 1400 int ierr; 1401 1402 PetscFunctionBegin; 1403 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1404 if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,0,"Matrix must be square"); 1405 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 1406 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 1407 if (!mat->ops->choleskyfactor) SETERRQ(PETSC_ERR_SUP,0,""); 1408 1409 PLogEventBegin(MAT_CholeskyFactor,mat,perm,0,0); 1410 ierr = (*mat->ops->choleskyfactor)(mat,perm,f);CHKERRQ(ierr); 1411 PLogEventEnd(MAT_CholeskyFactor,mat,perm,0,0); 1412 PetscFunctionReturn(0); 1413 } 1414 1415 #undef __FUNC__ 1416 #define __FUNC__ "MatCholeskyFactorSymbolic" 1417 /*@ 1418 MatCholeskyFactorSymbolic - Performs symbolic Cholesky factorization 1419 of a symmetric matrix. 1420 1421 Collective on Mat 1422 1423 Input Parameters: 1424 + mat - the matrix 1425 . perm - row and column permutations 1426 - f - expected fill as ratio of original 1427 1428 Output Parameter: 1429 . fact - the factored matrix 1430 1431 Notes: 1432 See MatLUFactorSymbolic() for the nonsymmetric case. See also 1433 MatCholeskyFactor() and MatCholeskyFactorNumeric(). 1434 1435 Most users should employ the simplified SLES interface for linear solvers 1436 instead of working directly with matrix algebra routines such as this. 1437 See, e.g., SLESCreate(). 1438 1439 Level: developer 1440 1441 .keywords: matrix, factor, factorization, symbolic, Cholesky 1442 1443 .seealso: MatLUFactorSymbolic(), MatCholeskyFactor(), MatCholeskyFactorNumeric() 1444 MatGetOrdering() 1445 1446 @*/ 1447 int MatCholeskyFactorSymbolic(Mat mat,IS perm,double f,Mat *fact) 1448 { 1449 int ierr; 1450 1451 PetscFunctionBegin; 1452 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1453 PetscValidPointer(fact); 1454 if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,0,"Matrix must be square"); 1455 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 1456 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 1457 if (!mat->ops->choleskyfactorsymbolic) SETERRQ(PETSC_ERR_SUP,0,""); 1458 1459 PLogEventBegin(MAT_CholeskyFactorSymbolic,mat,perm,0,0); 1460 ierr = (*mat->ops->choleskyfactorsymbolic)(mat,perm,f,fact);CHKERRQ(ierr); 1461 PLogEventEnd(MAT_CholeskyFactorSymbolic,mat,perm,0,0); 1462 PetscFunctionReturn(0); 1463 } 1464 1465 #undef __FUNC__ 1466 #define __FUNC__ "MatCholeskyFactorNumeric" 1467 /*@ 1468 MatCholeskyFactorNumeric - Performs numeric Cholesky factorization 1469 of a symmetric matrix. Call this routine after first calling 1470 MatCholeskyFactorSymbolic(). 1471 1472 Collective on Mat 1473 1474 Input Parameter: 1475 . mat - the initial matrix 1476 1477 Output Parameter: 1478 . fact - the factored matrix 1479 1480 Notes: 1481 Most users should employ the simplified SLES interface for linear solvers 1482 instead of working directly with matrix algebra routines such as this. 1483 See, e.g., SLESCreate(). 1484 1485 Level: developer 1486 1487 .keywords: matrix, factor, numeric, Cholesky 1488 1489 .seealso: MatCholeskyFactorSymbolic(), MatCholeskyFactor(), MatLUFactorNumeric() 1490 @*/ 1491 int MatCholeskyFactorNumeric(Mat mat,Mat *fact) 1492 { 1493 int ierr; 1494 1495 PetscFunctionBegin; 1496 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1497 PetscValidPointer(fact); 1498 if (!mat->ops->choleskyfactornumeric) SETERRQ(PETSC_ERR_SUP,0,""); 1499 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 1500 if (mat->M != (*fact)->M || mat->N != (*fact)->N) { 1501 SETERRQ4(PETSC_ERR_ARG_SIZ,0,"Mat mat,Mat *fact: global dim %d should = %d %d should = %d", 1502 mat->M,(*fact)->M,mat->N,(*fact)->N); 1503 } 1504 1505 PLogEventBegin(MAT_CholeskyFactorNumeric,mat,*fact,0,0); 1506 ierr = (*mat->ops->choleskyfactornumeric)(mat,fact);CHKERRQ(ierr); 1507 PLogEventEnd(MAT_CholeskyFactorNumeric,mat,*fact,0,0); 1508 PetscFunctionReturn(0); 1509 } 1510 1511 /* ----------------------------------------------------------------*/ 1512 #undef __FUNC__ 1513 #define __FUNC__ "MatSolve" 1514 /*@ 1515 MatSolve - Solves A x = b, given a factored matrix. 1516 1517 Collective on Mat and Vec 1518 1519 Input Parameters: 1520 + mat - the factored matrix 1521 - b - the right-hand-side vector 1522 1523 Output Parameter: 1524 . x - the result vector 1525 1526 Notes: 1527 The vectors b and x cannot be the same. I.e., one cannot 1528 call MatSolve(A,x,x). 1529 1530 Notes: 1531 Most users should employ the simplified SLES interface for linear solvers 1532 instead of working directly with matrix algebra routines such as this. 1533 See, e.g., SLESCreate(). 1534 1535 Level: developer 1536 1537 .keywords: matrix, linear system, solve, LU, Cholesky, triangular solve 1538 1539 .seealso: MatSolveAdd(), MatSolveTrans(), MatSolveTransAdd() 1540 @*/ 1541 int MatSolve(Mat mat,Vec b,Vec x) 1542 { 1543 int ierr; 1544 1545 PetscFunctionBegin; 1546 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1547 PetscValidHeaderSpecific(b,VEC_COOKIE); 1548 PetscValidHeaderSpecific(x,VEC_COOKIE); 1549 PetscCheckSameComm(mat,b); 1550 PetscCheckSameComm(mat,x); 1551 if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,0,"x and b must be different vectors"); 1552 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Unfactored matrix"); 1553 if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec x: global dim %d %d",mat->N,x->N); 1554 if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec b: global dim %d %d",mat->M,b->N); 1555 if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec b: local dim %d %d",mat->m,b->n); 1556 if (mat->M == 0 && mat->N == 0) PetscFunctionReturn(0); 1557 1558 if (!mat->ops->solve) SETERRQ(PETSC_ERR_SUP,0,""); 1559 PLogEventBegin(MAT_Solve,mat,b,x,0); 1560 ierr = (*mat->ops->solve)(mat,b,x);CHKERRQ(ierr); 1561 PLogEventEnd(MAT_Solve,mat,b,x,0); 1562 PetscFunctionReturn(0); 1563 } 1564 1565 #undef __FUNC__ 1566 #define __FUNC__ "MatForwardSolve" 1567 /* @ 1568 MatForwardSolve - Solves L x = b, given a factored matrix, A = LU. 1569 1570 Collective on Mat and Vec 1571 1572 Input Parameters: 1573 + mat - the factored matrix 1574 - b - the right-hand-side vector 1575 1576 Output Parameter: 1577 . x - the result vector 1578 1579 Notes: 1580 MatSolve() should be used for most applications, as it performs 1581 a forward solve followed by a backward solve. 1582 1583 The vectors b and x cannot be the same. I.e., one cannot 1584 call MatForwardSolve(A,x,x). 1585 1586 Most users should employ the simplified SLES interface for linear solvers 1587 instead of working directly with matrix algebra routines such as this. 1588 See, e.g., SLESCreate(). 1589 1590 Level: developer 1591 1592 .keywords: matrix, forward, LU, Cholesky, triangular solve 1593 1594 .seealso: MatSolve(), MatBackwardSolve() 1595 @ */ 1596 int MatForwardSolve(Mat mat,Vec b,Vec x) 1597 { 1598 int ierr; 1599 1600 PetscFunctionBegin; 1601 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1602 PetscValidHeaderSpecific(b,VEC_COOKIE); 1603 PetscValidHeaderSpecific(x,VEC_COOKIE); 1604 PetscCheckSameComm(mat,b); 1605 PetscCheckSameComm(mat,x); 1606 if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,0,"x and b must be different vectors"); 1607 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Unfactored matrix"); 1608 if (!mat->ops->forwardsolve) SETERRQ(PETSC_ERR_SUP,0,""); 1609 if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec x: global dim %d %d",mat->N,x->N); 1610 if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec b: global dim %d %d",mat->M,b->N); 1611 if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec b: local dim %d %d",mat->m,b->n); 1612 1613 PLogEventBegin(MAT_ForwardSolve,mat,b,x,0); 1614 ierr = (*mat->ops->forwardsolve)(mat,b,x);CHKERRQ(ierr); 1615 PLogEventEnd(MAT_ForwardSolve,mat,b,x,0); 1616 PetscFunctionReturn(0); 1617 } 1618 1619 #undef __FUNC__ 1620 #define __FUNC__ "MatBackwardSolve" 1621 /* @ 1622 MatBackwardSolve - Solves U x = b, given a factored matrix, A = LU. 1623 1624 Collective on Mat and Vec 1625 1626 Input Parameters: 1627 + mat - the factored matrix 1628 - b - the right-hand-side vector 1629 1630 Output Parameter: 1631 . x - the result vector 1632 1633 Notes: 1634 MatSolve() should be used for most applications, as it performs 1635 a forward solve followed by a backward solve. 1636 1637 The vectors b and x cannot be the same. I.e., one cannot 1638 call MatBackwardSolve(A,x,x). 1639 1640 Most users should employ the simplified SLES interface for linear solvers 1641 instead of working directly with matrix algebra routines such as this. 1642 See, e.g., SLESCreate(). 1643 1644 Level: developer 1645 1646 .keywords: matrix, backward, LU, Cholesky, triangular solve 1647 1648 .seealso: MatSolve(), MatForwardSolve() 1649 @ */ 1650 int MatBackwardSolve(Mat mat,Vec b,Vec x) 1651 { 1652 int ierr; 1653 1654 PetscFunctionBegin; 1655 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1656 PetscValidHeaderSpecific(b,VEC_COOKIE); 1657 PetscValidHeaderSpecific(x,VEC_COOKIE); 1658 PetscCheckSameComm(mat,b); 1659 PetscCheckSameComm(mat,x); 1660 if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,0,"x and b must be different vectors"); 1661 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Unfactored matrix"); 1662 if (!mat->ops->backwardsolve) SETERRQ(PETSC_ERR_SUP,0,""); 1663 if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec x: global dim %d %d",mat->N,x->N); 1664 if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec b: global dim %d %d",mat->M,b->N); 1665 if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec b: local dim %d %d",mat->m,b->n); 1666 1667 PLogEventBegin(MAT_BackwardSolve,mat,b,x,0); 1668 ierr = (*mat->ops->backwardsolve)(mat,b,x);CHKERRQ(ierr); 1669 PLogEventEnd(MAT_BackwardSolve,mat,b,x,0); 1670 PetscFunctionReturn(0); 1671 } 1672 1673 #undef __FUNC__ 1674 #define __FUNC__ "MatSolveAdd" 1675 /*@ 1676 MatSolveAdd - Computes x = y + inv(A)*b, given a factored matrix. 1677 1678 Collective on Mat and Vec 1679 1680 Input Parameters: 1681 + mat - the factored matrix 1682 . b - the right-hand-side vector 1683 - y - the vector to be added to 1684 1685 Output Parameter: 1686 . x - the result vector 1687 1688 Notes: 1689 The vectors b and x cannot be the same. I.e., one cannot 1690 call MatSolveAdd(A,x,y,x). 1691 1692 Most users should employ the simplified SLES interface for linear solvers 1693 instead of working directly with matrix algebra routines such as this. 1694 See, e.g., SLESCreate(). 1695 1696 Level: developer 1697 1698 .keywords: matrix, linear system, solve, LU, Cholesky, add 1699 1700 .seealso: MatSolve(), MatSolveTrans(), MatSolveTransAdd() 1701 @*/ 1702 int MatSolveAdd(Mat mat,Vec b,Vec y,Vec x) 1703 { 1704 Scalar one = 1.0; 1705 Vec tmp; 1706 int ierr; 1707 1708 PetscFunctionBegin; 1709 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1710 PetscValidHeaderSpecific(y,VEC_COOKIE); 1711 PetscValidHeaderSpecific(b,VEC_COOKIE); 1712 PetscValidHeaderSpecific(x,VEC_COOKIE); 1713 PetscCheckSameComm(mat,b); 1714 PetscCheckSameComm(mat,y); 1715 PetscCheckSameComm(mat,x); 1716 if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,0,"x and b must be different vectors"); 1717 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Unfactored matrix"); 1718 if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec x: global dim %d %d",mat->N,x->N); 1719 if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec b: global dim %d %d",mat->M,b->N); 1720 if (mat->M != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec y: global dim %d %d",mat->M,y->N); 1721 if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec b: local dim %d %d",mat->m,b->n); 1722 if (x->n != y->n) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Vec x,Vec y: local dim %d %d",x->n,y->n); 1723 1724 PLogEventBegin(MAT_SolveAdd,mat,b,x,y); 1725 if (mat->ops->solveadd) { 1726 ierr = (*mat->ops->solveadd)(mat,b,y,x);CHKERRQ(ierr); 1727 } else { 1728 /* do the solve then the add manually */ 1729 if (x != y) { 1730 ierr = MatSolve(mat,b,x);CHKERRQ(ierr); 1731 ierr = VecAXPY(&one,y,x);CHKERRQ(ierr); 1732 } else { 1733 ierr = VecDuplicate(x,&tmp);CHKERRQ(ierr); 1734 PLogObjectParent(mat,tmp); 1735 ierr = VecCopy(x,tmp);CHKERRQ(ierr); 1736 ierr = MatSolve(mat,b,x);CHKERRQ(ierr); 1737 ierr = VecAXPY(&one,tmp,x);CHKERRQ(ierr); 1738 ierr = VecDestroy(tmp);CHKERRQ(ierr); 1739 } 1740 } 1741 PLogEventEnd(MAT_SolveAdd,mat,b,x,y); 1742 PetscFunctionReturn(0); 1743 } 1744 1745 #undef __FUNC__ 1746 #define __FUNC__ "MatSolveTrans" 1747 /*@ 1748 MatSolveTrans - Solves A' x = b, given a factored matrix. 1749 1750 Collective on Mat and Vec 1751 1752 Input Parameters: 1753 + mat - the factored matrix 1754 - b - the right-hand-side vector 1755 1756 Output Parameter: 1757 . x - the result vector 1758 1759 Notes: 1760 The vectors b and x cannot be the same. I.e., one cannot 1761 call MatSolveTrans(A,x,x). 1762 1763 Most users should employ the simplified SLES interface for linear solvers 1764 instead of working directly with matrix algebra routines such as this. 1765 See, e.g., SLESCreate(). 1766 1767 Level: developer 1768 1769 .keywords: matrix, linear system, solve, LU, Cholesky, transpose 1770 1771 .seealso: MatSolve(), MatSolveAdd(), MatSolveTransAdd() 1772 @*/ 1773 int MatSolveTrans(Mat mat,Vec b,Vec x) 1774 { 1775 int ierr; 1776 1777 PetscFunctionBegin; 1778 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1779 PetscValidHeaderSpecific(b,VEC_COOKIE); 1780 PetscValidHeaderSpecific(x,VEC_COOKIE); 1781 PetscCheckSameComm(mat,b); 1782 PetscCheckSameComm(mat,x); 1783 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Unfactored matrix"); 1784 if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,0,"x and b must be different vectors"); 1785 if (!mat->ops->solvetrans) SETERRQ(PETSC_ERR_SUP,0,""); 1786 if (mat->M != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec x: global dim %d %d",mat->M,x->N); 1787 if (mat->N != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec b: global dim %d %d",mat->N,b->N); 1788 1789 PLogEventBegin(MAT_SolveTrans,mat,b,x,0); 1790 ierr = (*mat->ops->solvetrans)(mat,b,x);CHKERRQ(ierr); 1791 PLogEventEnd(MAT_SolveTrans,mat,b,x,0); 1792 PetscFunctionReturn(0); 1793 } 1794 1795 #undef __FUNC__ 1796 #define __FUNC__ "MatSolveTransAdd" 1797 /*@ 1798 MatSolveTransAdd - Computes x = y + inv(trans(A)) b, given a 1799 factored matrix. 1800 1801 Collective on Mat and Vec 1802 1803 Input Parameters: 1804 + mat - the factored matrix 1805 . b - the right-hand-side vector 1806 - y - the vector to be added to 1807 1808 Output Parameter: 1809 . x - the result vector 1810 1811 Notes: 1812 The vectors b and x cannot be the same. I.e., one cannot 1813 call MatSolveTransAdd(A,x,y,x). 1814 1815 Most users should employ the simplified SLES interface for linear solvers 1816 instead of working directly with matrix algebra routines such as this. 1817 See, e.g., SLESCreate(). 1818 1819 Level: developer 1820 1821 .keywords: matrix, linear system, solve, LU, Cholesky, transpose, add 1822 1823 .seealso: MatSolve(), MatSolveAdd(), MatSolveTrans() 1824 @*/ 1825 int MatSolveTransAdd(Mat mat,Vec b,Vec y,Vec x) 1826 { 1827 Scalar one = 1.0; 1828 int ierr; 1829 Vec tmp; 1830 1831 PetscFunctionBegin; 1832 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1833 PetscValidHeaderSpecific(y,VEC_COOKIE); 1834 PetscValidHeaderSpecific(b,VEC_COOKIE); 1835 PetscValidHeaderSpecific(x,VEC_COOKIE); 1836 PetscCheckSameComm(mat,b); 1837 PetscCheckSameComm(mat,y); 1838 PetscCheckSameComm(mat,x); 1839 if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,0,"x and b must be different vectors"); 1840 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Unfactored matrix"); 1841 if (mat->M != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec x: global dim %d %d",mat->M,x->N); 1842 if (mat->N != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec b: global dim %d %d",mat->N,b->N); 1843 if (mat->N != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec y: global dim %d %d",mat->N,y->N); 1844 if (x->n != y->n) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Vec x,Vec y: local dim %d %d",x->n,y->n); 1845 1846 PLogEventBegin(MAT_SolveTransAdd,mat,b,x,y); 1847 if (mat->ops->solvetransadd) { 1848 ierr = (*mat->ops->solvetransadd)(mat,b,y,x);CHKERRQ(ierr); 1849 } else { 1850 /* do the solve then the add manually */ 1851 if (x != y) { 1852 ierr = MatSolveTrans(mat,b,x);CHKERRQ(ierr); 1853 ierr = VecAXPY(&one,y,x);CHKERRQ(ierr); 1854 } else { 1855 ierr = VecDuplicate(x,&tmp);CHKERRQ(ierr); 1856 PLogObjectParent(mat,tmp); 1857 ierr = VecCopy(x,tmp);CHKERRQ(ierr); 1858 ierr = MatSolveTrans(mat,b,x);CHKERRQ(ierr); 1859 ierr = VecAXPY(&one,tmp,x);CHKERRQ(ierr); 1860 ierr = VecDestroy(tmp);CHKERRQ(ierr); 1861 } 1862 } 1863 PLogEventEnd(MAT_SolveTransAdd,mat,b,x,y); 1864 PetscFunctionReturn(0); 1865 } 1866 /* ----------------------------------------------------------------*/ 1867 1868 #undef __FUNC__ 1869 #define __FUNC__ "MatRelax" 1870 /*@ 1871 MatRelax - Computes one relaxation sweep. 1872 1873 Collective on Mat and Vec 1874 1875 Input Parameters: 1876 + mat - the matrix 1877 . b - the right hand side 1878 . omega - the relaxation factor 1879 . flag - flag indicating the type of SOR (see below) 1880 . shift - diagonal shift 1881 - its - the number of iterations 1882 1883 Output Parameters: 1884 . x - the solution (can contain an initial guess) 1885 1886 SOR Flags: 1887 . SOR_FORWARD_SWEEP - forward SOR 1888 . SOR_BACKWARD_SWEEP - backward SOR 1889 . SOR_SYMMETRIC_SWEEP - SSOR (symmetric SOR) 1890 . SOR_LOCAL_FORWARD_SWEEP - local forward SOR 1891 . SOR_LOCAL_BACKWARD_SWEEP - local forward SOR 1892 . SOR_LOCAL_SYMMETRIC_SWEEP - local SSOR 1893 . SOR_APPLY_UPPER, SOR_APPLY_LOWER - applies 1894 upper/lower triangular part of matrix to 1895 vector (with omega) 1896 . SOR_ZERO_INITIAL_GUESS - zero initial guess 1897 1898 Notes: 1899 SOR_LOCAL_FORWARD_SWEEP, SOR_LOCAL_BACKWARD_SWEEP, and 1900 SOR_LOCAL_SYMMETRIC_SWEEP perform seperate independent smoothings 1901 on each processor. 1902 1903 Application programmers will not generally use MatRelax() directly, 1904 but instead will employ the SLES/PC interface. 1905 1906 Notes for Advanced Users: 1907 The flags are implemented as bitwise inclusive or operations. 1908 For example, use (SOR_ZERO_INITIAL_GUESS | SOR_SYMMETRIC_SWEEP) 1909 to specify a zero initial guess for SSOR. 1910 1911 Most users should employ the simplified SLES interface for linear solvers 1912 instead of working directly with matrix algebra routines such as this. 1913 See, e.g., SLESCreate(). 1914 1915 Level: developer 1916 1917 .keywords: matrix, relax, relaxation, sweep 1918 @*/ 1919 int MatRelax(Mat mat,Vec b,double omega,MatSORType flag,double shift,int its,Vec x) 1920 { 1921 int ierr; 1922 1923 PetscFunctionBegin; 1924 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1925 PetscValidHeaderSpecific(b,VEC_COOKIE); 1926 PetscValidHeaderSpecific(x,VEC_COOKIE); 1927 PetscCheckSameComm(mat,b); 1928 PetscCheckSameComm(mat,x); 1929 if (!mat->ops->relax) SETERRQ(PETSC_ERR_SUP,0,""); 1930 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 1931 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 1932 if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec x: global dim %d %d",mat->N,x->N); 1933 if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec b: global dim %d %d",mat->M,b->N); 1934 if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec b: local dim %d %d",mat->m,b->n); 1935 1936 PLogEventBegin(MAT_Relax,mat,b,x,0); 1937 ierr =(*mat->ops->relax)(mat,b,omega,flag,shift,its,x);CHKERRQ(ierr); 1938 PLogEventEnd(MAT_Relax,mat,b,x,0); 1939 PetscFunctionReturn(0); 1940 } 1941 1942 #undef __FUNC__ 1943 #define __FUNC__ "MatCopy_Basic" 1944 /* 1945 Default matrix copy routine. 1946 */ 1947 int MatCopy_Basic(Mat A,Mat B,MatStructure str) 1948 { 1949 int ierr,i,rstart,rend,nz,*cwork; 1950 Scalar *vwork; 1951 1952 PetscFunctionBegin; 1953 ierr = MatZeroEntries(B);CHKERRQ(ierr); 1954 ierr = MatGetOwnershipRange(A,&rstart,&rend);CHKERRQ(ierr); 1955 for (i=rstart; i<rend; i++) { 1956 ierr = MatGetRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr); 1957 ierr = MatSetValues(B,1,&i,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr); 1958 ierr = MatRestoreRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr); 1959 } 1960 ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1961 ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1962 PetscFunctionReturn(0); 1963 } 1964 1965 #undef __FUNC__ 1966 #define __FUNC__ "MatCopy" 1967 /*@C 1968 MatCopy - Copys a matrix to another matrix. 1969 1970 Collective on Mat 1971 1972 Input Parameters: 1973 + A - the matrix 1974 - str - SAME_NONZERO_PATTERN or DIFFERENT_NONZERO_PATTERN 1975 1976 Output Parameter: 1977 . B - where the copy is put 1978 1979 Notes: 1980 If you use SAME_NONZERO_PATTERN then the zero matrices had better have the 1981 same nonzero pattern or the routine will crash. 1982 1983 MatCopy() copies the matrix entries of a matrix to another existing 1984 matrix (after first zeroing the second matrix). A related routine is 1985 MatConvert(), which first creates a new matrix and then copies the data. 1986 1987 Level: intermediate 1988 1989 .keywords: matrix, copy, convert 1990 1991 .seealso: MatConvert() 1992 @*/ 1993 int MatCopy(Mat A,Mat B,MatStructure str) 1994 { 1995 int ierr; 1996 1997 PetscFunctionBegin; 1998 PetscValidHeaderSpecific(A,MAT_COOKIE); 1999 PetscValidHeaderSpecific(B,MAT_COOKIE); 2000 PetscCheckSameComm(A,B); 2001 if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 2002 if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 2003 if (A->M != B->M || A->N != B->N) SETERRQ4(PETSC_ERR_ARG_SIZ,0,"Mat A,Mat B: global dim %d %d",A->M,B->M, 2004 A->N,B->N); 2005 2006 PLogEventBegin(MAT_Copy,A,B,0,0); 2007 if (A->ops->copy) { 2008 ierr = (*A->ops->copy)(A,B,str);CHKERRQ(ierr); 2009 } else { /* generic conversion */ 2010 ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr); 2011 } 2012 PLogEventEnd(MAT_Copy,A,B,0,0); 2013 PetscFunctionReturn(0); 2014 } 2015 2016 static int MatConvertersSet = 0; 2017 static int (*MatConverters[MAX_MATRIX_TYPES][MAX_MATRIX_TYPES])(Mat,MatType,Mat*) = 2018 {{0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0}, 2019 {0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0}, 2020 {0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0}, 2021 {0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0}, 2022 {0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0}, 2023 {0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0}}; 2024 2025 #undef __FUNC__ 2026 #define __FUNC__ "MatConvertRegister" 2027 /*@C 2028 MatConvertRegister - Allows one to register a routine that converts between 2029 two matrix types. 2030 2031 Not Collective 2032 2033 Input Parameters: 2034 + intype - the type of matrix (defined in include/mat.h), for example, MATSEQAIJ. 2035 - outtype - new matrix type, or MATSAME 2036 2037 Level: advanced 2038 2039 .seealso: MatConvertRegisterAll() 2040 @*/ 2041 int MatConvertRegister(MatType intype,MatType outtype,int (*converter)(Mat,MatType,Mat*)) 2042 { 2043 PetscFunctionBegin; 2044 MatConverters[intype][outtype] = converter; 2045 MatConvertersSet = 1; 2046 PetscFunctionReturn(0); 2047 } 2048 2049 #undef __FUNC__ 2050 #define __FUNC__ "MatConvert" 2051 /*@C 2052 MatConvert - Converts a matrix to another matrix, either of the same 2053 or different type. 2054 2055 Collective on Mat 2056 2057 Input Parameters: 2058 + mat - the matrix 2059 - newtype - new matrix type. Use MATSAME to create a new matrix of the 2060 same type as the original matrix. 2061 2062 Output Parameter: 2063 . M - pointer to place new matrix 2064 2065 Notes: 2066 MatConvert() first creates a new matrix and then copies the data from 2067 the first matrix. A related routine is MatCopy(), which copies the matrix 2068 entries of one matrix to another already existing matrix context. 2069 2070 Level: intermediate 2071 2072 .keywords: matrix, copy, convert 2073 2074 .seealso: MatCopy(), MatDuplicate() 2075 @*/ 2076 int MatConvert(Mat mat,MatType newtype,Mat *M) 2077 { 2078 int ierr; 2079 2080 PetscFunctionBegin; 2081 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2082 PetscValidPointer(M); 2083 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 2084 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 2085 2086 if (newtype > MAX_MATRIX_TYPES || newtype < -1) { 2087 SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,1,"Not a valid matrix type"); 2088 } 2089 *M = 0; 2090 2091 if (!MatConvertersSet) { 2092 ierr = MatLoadRegisterAll();CHKERRQ(ierr); 2093 } 2094 2095 PLogEventBegin(MAT_Convert,mat,0,0,0); 2096 if ((newtype == mat->type || newtype == MATSAME) && mat->ops->duplicate) { 2097 ierr = (*mat->ops->duplicate)(mat,MAT_COPY_VALUES,M);CHKERRQ(ierr); 2098 } else { 2099 if (!MatConvertersSet) { 2100 ierr = MatConvertRegisterAll();CHKERRQ(ierr); 2101 } 2102 if (!MatConverters[mat->type][newtype]) { 2103 SETERRQ(PETSC_ERR_ARG_WRONG,1,"Invalid matrix type, or matrix converter not registered"); 2104 } 2105 ierr = (*MatConverters[mat->type][newtype])(mat,newtype,M);CHKERRQ(ierr); 2106 } 2107 PLogEventEnd(MAT_Convert,mat,0,0,0); 2108 PetscFunctionReturn(0); 2109 } 2110 2111 #undef __FUNC__ 2112 #define __FUNC__ "MatDuplicate" 2113 /*@C 2114 MatDuplicate - Duplicates a matrix including the non-zero structure. 2115 2116 Collective on Mat 2117 2118 Input Parameters: 2119 + mat - the matrix 2120 - op - either MAT_DO_NOT_COPY_VALUES or MAT_COPY_VALUES, cause it to copy nonzero 2121 values as well or not 2122 2123 Output Parameter: 2124 . M - pointer to place new matrix 2125 2126 Level: intermediate 2127 2128 .keywords: matrix, copy, convert, duplicate 2129 2130 .seealso: MatCopy(), MatConvert() 2131 @*/ 2132 int MatDuplicate(Mat mat,MatDuplicateOption op,Mat *M) 2133 { 2134 int ierr; 2135 2136 PetscFunctionBegin; 2137 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2138 PetscValidPointer(M); 2139 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 2140 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 2141 2142 *M = 0; 2143 PLogEventBegin(MAT_Convert,mat,0,0,0); 2144 if (!mat->ops->duplicate) { 2145 SETERRQ(PETSC_ERR_SUP,1,"Not written for this matrix type"); 2146 } 2147 ierr = (*mat->ops->duplicate)(mat,op,M);CHKERRQ(ierr); 2148 PLogEventEnd(MAT_Convert,mat,0,0,0); 2149 PetscFunctionReturn(0); 2150 } 2151 2152 #undef __FUNC__ 2153 #define __FUNC__ "MatGetDiagonal" 2154 /*@ 2155 MatGetDiagonal - Gets the diagonal of a matrix. 2156 2157 Collective on Mat and Vec 2158 2159 Input Parameters: 2160 + mat - the matrix 2161 - v - the vector for storing the diagonal 2162 2163 Output Parameter: 2164 . v - the diagonal of the matrix 2165 2166 Notes: 2167 For the SeqAIJ matrix format, this routine may also be called 2168 on a LU factored matrix; in that case it routines the reciprocal of 2169 the diagonal entries in U. It returns the entries permuted by the 2170 row and column permutation used during the symbolic factorization. 2171 2172 Level: intermediate 2173 2174 .keywords: matrix, get, diagonal 2175 2176 .seealso: MatGetRow(), MatGetSubmatrices(), MatGetSubmatrix() 2177 @*/ 2178 int MatGetDiagonal(Mat mat,Vec v) 2179 { 2180 int ierr; 2181 2182 PetscFunctionBegin; 2183 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2184 PetscValidHeaderSpecific(v,VEC_COOKIE); 2185 /* PetscCheckSameComm(mat,v); Could be MPI vector but Seq matrix cause of two submatrix storage */ 2186 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 2187 if (!mat->ops->getdiagonal) SETERRQ(PETSC_ERR_SUP,0,""); 2188 2189 ierr = (*mat->ops->getdiagonal)(mat,v);CHKERRQ(ierr); 2190 PetscFunctionReturn(0); 2191 } 2192 2193 #undef __FUNC__ 2194 #define __FUNC__ "MatTranspose" 2195 /*@C 2196 MatTranspose - Computes an in-place or out-of-place transpose of a matrix. 2197 2198 Collective on Mat 2199 2200 Input Parameter: 2201 . mat - the matrix to transpose 2202 2203 Output Parameters: 2204 . B - the transpose (or pass in PETSC_NULL for an in-place transpose) 2205 2206 Level: intermediate 2207 2208 .keywords: matrix, transpose 2209 2210 .seealso: MatMultTrans(), MatMultTransAdd() 2211 @*/ 2212 int MatTranspose(Mat mat,Mat *B) 2213 { 2214 int ierr; 2215 2216 PetscFunctionBegin; 2217 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2218 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 2219 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 2220 if (!mat->ops->transpose) SETERRQ(PETSC_ERR_SUP,0,""); 2221 ierr = (*mat->ops->transpose)(mat,B);CHKERRQ(ierr); 2222 PetscFunctionReturn(0); 2223 } 2224 2225 #undef __FUNC__ 2226 #define __FUNC__ "MatPermute" 2227 /*@C 2228 MatPermute - Creates a new matrix with rows and columns permuted from the 2229 original. 2230 2231 Collective on Mat 2232 2233 Input Parameters: 2234 + mat - the matrix to permute 2235 . row - row permutation 2236 - col - column permutation 2237 2238 Output Parameters: 2239 . B - the permuted matrix 2240 2241 Level: advanced 2242 2243 .keywords: matrix, transpose 2244 2245 .seealso: MatGetOrdering() 2246 @*/ 2247 int MatPermute(Mat mat,IS row,IS col,Mat *B) 2248 { 2249 int ierr; 2250 2251 PetscFunctionBegin; 2252 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2253 PetscValidHeaderSpecific(row,IS_COOKIE); 2254 PetscValidHeaderSpecific(col,IS_COOKIE); 2255 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 2256 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 2257 if (!mat->ops->permute) SETERRQ(PETSC_ERR_SUP,0,""); 2258 ierr = (*mat->ops->permute)(mat,row,col,B);CHKERRQ(ierr); 2259 PetscFunctionReturn(0); 2260 } 2261 2262 #undef __FUNC__ 2263 #define __FUNC__ "MatEqual" 2264 /*@ 2265 MatEqual - Compares two matrices. 2266 2267 Collective on Mat 2268 2269 Input Parameters: 2270 + A - the first matrix 2271 - B - the second matrix 2272 2273 Output Parameter: 2274 . flg - PETSC_TRUE if the matrices are equal; PETSC_FALSE otherwise. 2275 2276 Level: intermediate 2277 2278 .keywords: matrix, equal, equivalent 2279 @*/ 2280 int MatEqual(Mat A,Mat B,PetscTruth *flg) 2281 { 2282 int ierr; 2283 2284 PetscFunctionBegin; 2285 PetscValidHeaderSpecific(A,MAT_COOKIE); 2286 PetscValidHeaderSpecific(B,MAT_COOKIE); 2287 PetscValidIntPointer(flg); 2288 PetscCheckSameComm(A,B); 2289 if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 2290 if (!B->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 2291 if (A->M != B->M || A->N != B->N) SETERRQ4(PETSC_ERR_ARG_SIZ,0,"Mat A,Mat B: global dim %d %d %d %d", 2292 A->M,B->M,A->N,B->N); 2293 if (!A->ops->equal) SETERRQ(PETSC_ERR_SUP,0,""); 2294 ierr = (*A->ops->equal)(A,B,flg);CHKERRQ(ierr); 2295 PetscFunctionReturn(0); 2296 } 2297 2298 #undef __FUNC__ 2299 #define __FUNC__ "MatDiagonalScale" 2300 /*@ 2301 MatDiagonalScale - Scales a matrix on the left and right by diagonal 2302 matrices that are stored as vectors. Either of the two scaling 2303 matrices can be PETSC_NULL. 2304 2305 Collective on Mat 2306 2307 Input Parameters: 2308 + mat - the matrix to be scaled 2309 . l - the left scaling vector (or PETSC_NULL) 2310 - r - the right scaling vector (or PETSC_NULL) 2311 2312 Notes: 2313 MatDiagonalScale() computes A = LAR, where 2314 L = a diagonal matrix, R = a diagonal matrix 2315 2316 Level: intermediate 2317 2318 .keywords: matrix, diagonal, scale 2319 2320 .seealso: MatScale() 2321 @*/ 2322 int MatDiagonalScale(Mat mat,Vec l,Vec r) 2323 { 2324 int ierr; 2325 2326 PetscFunctionBegin; 2327 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2328 if (!mat->ops->diagonalscale) SETERRQ(PETSC_ERR_SUP,0,""); 2329 if (l) {PetscValidHeaderSpecific(l,VEC_COOKIE);PetscCheckSameComm(mat,l);} 2330 if (r) {PetscValidHeaderSpecific(r,VEC_COOKIE);PetscCheckSameComm(mat,r);} 2331 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 2332 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 2333 2334 PLogEventBegin(MAT_Scale,mat,0,0,0); 2335 ierr = (*mat->ops->diagonalscale)(mat,l,r);CHKERRQ(ierr); 2336 PLogEventEnd(MAT_Scale,mat,0,0,0); 2337 PetscFunctionReturn(0); 2338 } 2339 2340 #undef __FUNC__ 2341 #define __FUNC__ "MatScale" 2342 /*@ 2343 MatScale - Scales all elements of a matrix by a given number. 2344 2345 Collective on Mat 2346 2347 Input Parameters: 2348 + mat - the matrix to be scaled 2349 - a - the scaling value 2350 2351 Output Parameter: 2352 . mat - the scaled matrix 2353 2354 Level: intermediate 2355 2356 .keywords: matrix, scale 2357 2358 .seealso: MatDiagonalScale() 2359 @*/ 2360 int MatScale(Scalar *a,Mat mat) 2361 { 2362 int ierr; 2363 2364 PetscFunctionBegin; 2365 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2366 PetscValidScalarPointer(a); 2367 if (!mat->ops->scale) SETERRQ(PETSC_ERR_SUP,0,""); 2368 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 2369 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 2370 2371 PLogEventBegin(MAT_Scale,mat,0,0,0); 2372 ierr = (*mat->ops->scale)(a,mat);CHKERRQ(ierr); 2373 PLogEventEnd(MAT_Scale,mat,0,0,0); 2374 PetscFunctionReturn(0); 2375 } 2376 2377 #undef __FUNC__ 2378 #define __FUNC__ "MatNorm" 2379 /*@ 2380 MatNorm - Calculates various norms of a matrix. 2381 2382 Collective on Mat 2383 2384 Input Parameters: 2385 + mat - the matrix 2386 - type - the type of norm, NORM_1, NORM_2, NORM_FROBENIUS, NORM_INFINITY 2387 2388 Output Parameters: 2389 . norm - the resulting norm 2390 2391 Level: intermediate 2392 2393 .keywords: matrix, norm, Frobenius 2394 @*/ 2395 int MatNorm(Mat mat,NormType type,double *norm) 2396 { 2397 int ierr; 2398 2399 PetscFunctionBegin; 2400 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2401 PetscValidScalarPointer(norm); 2402 2403 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 2404 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 2405 if (!mat->ops->norm) SETERRQ(PETSC_ERR_SUP,0,"Not for this matrix type"); 2406 ierr = (*mat->ops->norm)(mat,type,norm);CHKERRQ(ierr); 2407 PetscFunctionReturn(0); 2408 } 2409 2410 /* 2411 This variable is used to prevent counting of MatAssemblyBegin() that 2412 are called from within a MatAssemblyEnd(). 2413 */ 2414 static int MatAssemblyEnd_InUse = 0; 2415 #undef __FUNC__ 2416 #define __FUNC__ "MatAssemblyBegin" 2417 /*@ 2418 MatAssemblyBegin - Begins assembling the matrix. This routine should 2419 be called after completing all calls to MatSetValues(). 2420 2421 Collective on Mat 2422 2423 Input Parameters: 2424 + mat - the matrix 2425 - type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY 2426 2427 Notes: 2428 MatSetValues() generally caches the values. The matrix is ready to 2429 use only after MatAssemblyBegin() and MatAssemblyEnd() have been called. 2430 Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES 2431 in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before 2432 using the matrix. 2433 2434 Level: beginner 2435 2436 .keywords: matrix, assembly, assemble, begin 2437 2438 .seealso: MatAssemblyEnd(), MatSetValues(), MatAssembled() 2439 @*/ 2440 int MatAssemblyBegin(Mat mat,MatAssemblyType type) 2441 { 2442 int ierr; 2443 2444 PetscFunctionBegin; 2445 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2446 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix.\n did you forget to call MatSetUnfactored()?"); 2447 if (mat->assembled) { 2448 mat->was_assembled = PETSC_TRUE; 2449 mat->assembled = PETSC_FALSE; 2450 } 2451 if (!MatAssemblyEnd_InUse) { 2452 PLogEventBegin(MAT_AssemblyBegin,mat,0,0,0); 2453 if (mat->ops->assemblybegin){ierr = (*mat->ops->assemblybegin)(mat,type);CHKERRQ(ierr);} 2454 PLogEventEnd(MAT_AssemblyBegin,mat,0,0,0); 2455 } else { 2456 if (mat->ops->assemblybegin){ierr = (*mat->ops->assemblybegin)(mat,type);CHKERRQ(ierr);} 2457 } 2458 PetscFunctionReturn(0); 2459 } 2460 2461 #undef __FUNC__ 2462 #define __FUNC__ "MatAssembed" 2463 /*@ 2464 MatAssembled - Indicates if a matrix has been assembled and is ready for 2465 use; for example, in matrix-vector product. 2466 2467 Collective on Mat 2468 2469 Input Parameter: 2470 . mat - the matrix 2471 2472 Output Parameter: 2473 . assembled - PETSC_TRUE or PETSC_FALSE 2474 2475 Level: advanced 2476 2477 .keywords: matrix, assembly, assemble, begin 2478 2479 .seealso: MatAssemblyEnd(), MatSetValues(), MatAssemblyBegin() 2480 @*/ 2481 int MatAssembled(Mat mat,PetscTruth *assembled) 2482 { 2483 PetscFunctionBegin; 2484 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2485 *assembled = mat->assembled; 2486 PetscFunctionReturn(0); 2487 } 2488 2489 #undef __FUNC__ 2490 #define __FUNC__ "MatView_Private" 2491 /* 2492 Processes command line options to determine if/how a matrix 2493 is to be viewed. Called by MatAssemblyEnd() and MatLoad(). 2494 */ 2495 int MatView_Private(Mat mat) 2496 { 2497 int ierr,flg; 2498 2499 PetscFunctionBegin; 2500 ierr = OptionsHasName(PETSC_NULL,"-mat_view_info",&flg);CHKERRQ(ierr); 2501 if (flg) { 2502 ierr = ViewerPushFormat(VIEWER_STDOUT_(mat->comm),VIEWER_FORMAT_ASCII_INFO,0);CHKERRQ(ierr); 2503 ierr = MatView(mat,VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr); 2504 ierr = ViewerPopFormat(VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr); 2505 } 2506 ierr = OptionsHasName(PETSC_NULL,"-mat_view_info_detailed",&flg);CHKERRQ(ierr); 2507 if (flg) { 2508 ierr = ViewerPushFormat(VIEWER_STDOUT_(mat->comm),VIEWER_FORMAT_ASCII_INFO_LONG,0);CHKERRQ(ierr); 2509 ierr = MatView(mat,VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr); 2510 ierr = ViewerPopFormat(VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr); 2511 } 2512 ierr = OptionsHasName(PETSC_NULL,"-mat_view",&flg);CHKERRQ(ierr); 2513 if (flg) { 2514 ierr = MatView(mat,VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr); 2515 } 2516 ierr = OptionsHasName(PETSC_NULL,"-mat_view_matlab",&flg);CHKERRQ(ierr); 2517 if (flg) { 2518 ierr = ViewerPushFormat(VIEWER_STDOUT_(mat->comm),VIEWER_FORMAT_ASCII_MATLAB,"M");CHKERRQ(ierr); 2519 ierr = MatView(mat,VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr); 2520 ierr = ViewerPopFormat(VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr); 2521 } 2522 ierr = OptionsHasName(PETSC_NULL,"-mat_view_draw",&flg);CHKERRQ(ierr); 2523 if (flg) { 2524 ierr = OptionsHasName(PETSC_NULL,"-mat_view_contour",&flg);CHKERRQ(ierr); 2525 if (flg) { 2526 ViewerPushFormat(VIEWER_DRAW_(mat->comm),VIEWER_FORMAT_DRAW_CONTOUR,0);CHKERRQ(ierr); 2527 } 2528 ierr = MatView(mat,VIEWER_DRAW_(mat->comm));CHKERRQ(ierr); 2529 ierr = ViewerFlush(VIEWER_DRAW_(mat->comm));CHKERRQ(ierr); 2530 if (flg) { 2531 ViewerPopFormat(VIEWER_DRAW_(mat->comm));CHKERRQ(ierr); 2532 } 2533 } 2534 ierr = OptionsHasName(PETSC_NULL,"-mat_view_socket",&flg);CHKERRQ(ierr); 2535 if (flg) { 2536 ierr = MatView(mat,VIEWER_SOCKET_(mat->comm));CHKERRQ(ierr); 2537 ierr = ViewerFlush(VIEWER_SOCKET_(mat->comm));CHKERRQ(ierr); 2538 } 2539 PetscFunctionReturn(0); 2540 } 2541 2542 #undef __FUNC__ 2543 #define __FUNC__ "MatAssemblyEnd" 2544 /*@ 2545 MatAssemblyEnd - Completes assembling the matrix. This routine should 2546 be called after MatAssemblyBegin(). 2547 2548 Collective on Mat 2549 2550 Input Parameters: 2551 + mat - the matrix 2552 - type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY 2553 2554 Options Database Keys: 2555 + -mat_view_info - Prints info on matrix at conclusion of MatEndAssembly() 2556 . -mat_view_info_detailed - Prints more detailed info 2557 . -mat_view - Prints matrix in ASCII format 2558 . -mat_view_matlab - Prints matrix in Matlab format 2559 . -mat_view_draw - Draws nonzero structure of matrix, using MatView() and DrawOpenX(). 2560 . -display <name> - Sets display name (default is host) 2561 - -draw_pause <sec> - Sets number of seconds to pause after display 2562 2563 Notes: 2564 MatSetValues() generally caches the values. The matrix is ready to 2565 use only after MatAssemblyBegin() and MatAssemblyEnd() have been called. 2566 Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES 2567 in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before 2568 using the matrix. 2569 2570 Level: beginner 2571 2572 .keywords: matrix, assembly, assemble, end 2573 2574 .seealso: MatAssemblyBegin(), MatSetValues(), DrawOpenX(), MatView(), MatAssembled() 2575 @*/ 2576 int MatAssemblyEnd(Mat mat,MatAssemblyType type) 2577 { 2578 int ierr; 2579 static int inassm = 0; 2580 2581 PetscFunctionBegin; 2582 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2583 2584 inassm++; 2585 MatAssemblyEnd_InUse++; 2586 if (MatAssemblyEnd_InUse == 1) { /* Do the logging only the first time through */ 2587 PLogEventBegin(MAT_AssemblyEnd,mat,0,0,0); 2588 if (mat->ops->assemblyend) { 2589 ierr = (*mat->ops->assemblyend)(mat,type);CHKERRQ(ierr); 2590 } 2591 PLogEventEnd(MAT_AssemblyEnd,mat,0,0,0); 2592 } else { 2593 if (mat->ops->assemblyend) { 2594 ierr = (*mat->ops->assemblyend)(mat,type);CHKERRQ(ierr); 2595 } 2596 } 2597 2598 /* Flush assembly is not a true assembly */ 2599 if (type != MAT_FLUSH_ASSEMBLY) { 2600 mat->assembled = PETSC_TRUE; mat->num_ass++; 2601 } 2602 mat->insertmode = NOT_SET_VALUES; 2603 MatAssemblyEnd_InUse--; 2604 2605 if (inassm == 1 && type != MAT_FLUSH_ASSEMBLY) { 2606 ierr = MatView_Private(mat);CHKERRQ(ierr); 2607 } 2608 inassm--; 2609 PetscFunctionReturn(0); 2610 } 2611 2612 2613 #undef __FUNC__ 2614 #define __FUNC__ "MatCompress" 2615 /*@ 2616 MatCompress - Tries to store the matrix in as little space as 2617 possible. May fail if memory is already fully used, since it 2618 tries to allocate new space. 2619 2620 Collective on Mat 2621 2622 Input Parameters: 2623 . mat - the matrix 2624 2625 Level: advanced 2626 2627 .keywords: matrix, compress 2628 @*/ 2629 int MatCompress(Mat mat) 2630 { 2631 int ierr; 2632 2633 PetscFunctionBegin; 2634 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2635 if (mat->ops->compress) {ierr = (*mat->ops->compress)(mat);CHKERRQ(ierr);} 2636 PetscFunctionReturn(0); 2637 } 2638 2639 #undef __FUNC__ 2640 #define __FUNC__ "MatSetOption" 2641 /*@ 2642 MatSetOption - Sets a parameter option for a matrix. Some options 2643 may be specific to certain storage formats. Some options 2644 determine how values will be inserted (or added). Sorted, 2645 row-oriented input will generally assemble the fastest. The default 2646 is row-oriented, nonsorted input. 2647 2648 Collective on Mat 2649 2650 Input Parameters: 2651 + mat - the matrix 2652 - option - the option, one of those listed below (and possibly others), 2653 e.g., MAT_ROWS_SORTED, MAT_NEW_NONZERO_LOCATION_ERR 2654 2655 Options Describing Matrix Structure: 2656 + MAT_SYMMETRIC - symmetric in terms of both structure and value 2657 - MAT_STRUCTURALLY_SYMMETRIC - symmetric nonzero structure 2658 2659 Options For Use with MatSetValues(): 2660 Insert a logically dense subblock, which can be 2661 + MAT_ROW_ORIENTED - row-oriented 2662 . MAT_COLUMN_ORIENTED - column-oriented 2663 . MAT_ROWS_SORTED - sorted by row 2664 . MAT_ROWS_UNSORTED - not sorted by row 2665 . MAT_COLUMNS_SORTED - sorted by column 2666 - MAT_COLUMNS_UNSORTED - not sorted by column 2667 2668 Not these options reflect the data you pass in with MatSetValues(); it has 2669 nothing to do with how the data is stored internally in the matrix 2670 data structure. 2671 2672 When (re)assembling a matrix, we can restrict the input for 2673 efficiency/debugging purposes. These options include 2674 + MAT_NO_NEW_NONZERO_LOCATIONS - additional insertions will not be 2675 allowed if they generate a new nonzero 2676 . MAT_YES_NEW_NONZERO_LOCATIONS - additional insertions will be allowed 2677 . MAT_NO_NEW_DIAGONALS - additional insertions will not be allowed if 2678 they generate a nonzero in a new diagonal (for block diagonal format only) 2679 . MAT_YES_NEW_DIAGONALS - new diagonals will be allowed (for block diagonal format only) 2680 . MAT_IGNORE_OFF_PROC_ENTRIES - drops off-processor entries 2681 . MAT_NEW_NONZERO_LOCATION_ERR - generates an error for new matrix entry 2682 - MAT_USE_HASH_TABLE - uses a hash table to speed up matrix assembly 2683 2684 Notes: 2685 Some options are relevant only for particular matrix types and 2686 are thus ignored by others. Other options are not supported by 2687 certain matrix types and will generate an error message if set. 2688 2689 If using a Fortran 77 module to compute a matrix, one may need to 2690 use the column-oriented option (or convert to the row-oriented 2691 format). 2692 2693 MAT_NO_NEW_NONZERO_LOCATIONS indicates that any add or insertion 2694 that would generate a new entry in the nonzero structure is instead 2695 ignored. Thus, if memory has not alredy been allocated for this particular 2696 data, then the insertion is ignored. For dense matrices, in which 2697 the entire array is allocated, no entries are ever ignored. 2698 2699 MAT_NEW_NONZERO_LOCATION_ERR indicates that any add or insertion 2700 that would generate a new entry in the nonzero structure instead produces 2701 an error. (Currently supported for AIJ and BAIJ formats only.) 2702 This is a useful flag when using SAME_NONZERO_PATTERN in calling 2703 SLESSetOperators() to ensure that the nonzero pattern truely does 2704 remain unchanged. 2705 2706 MAT_NEW_NONZERO_ALLOCATION_ERR indicates that any add or insertion 2707 that would generate a new entry that has not been preallocated will 2708 instead produce an error. (Currently supported for AIJ and BAIJ formats 2709 only.) This is a useful flag when debugging matrix memory preallocation. 2710 2711 MAT_IGNORE_OFF_PROC_ENTRIES indicates entries destined for 2712 other processors should be dropped, rather than stashed. 2713 This is useful if you know that the "owning" processor is also 2714 always generating the correct matrix entries, so that PETSc need 2715 not transfer duplicate entries generated on another processor. 2716 2717 MAT_USE_HASH_TABLE indicates that a hash table be used to improve the 2718 searches during matrix assembly. When this flag is set, the hash table 2719 is created during the first Matrix Assembly. This hash table is 2720 used the next time through, during MatSetVaules()/MatSetVaulesBlocked() 2721 to improve the searching of indices. MAT_NO_NEW_NONZERO_LOCATIONS flag 2722 should be used with MAT_USE_HASH_TABLE flag. This option is currently 2723 supported by MATMPIBAIJ format only. 2724 2725 Level: intermediate 2726 2727 .keywords: matrix, option, row-oriented, column-oriented, sorted, nonzero 2728 @*/ 2729 int MatSetOption(Mat mat,MatOption op) 2730 { 2731 int ierr; 2732 2733 PetscFunctionBegin; 2734 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2735 if (mat->ops->setoption) {ierr = (*mat->ops->setoption)(mat,op);CHKERRQ(ierr);} 2736 PetscFunctionReturn(0); 2737 } 2738 2739 #undef __FUNC__ 2740 #define __FUNC__ "MatZeroEntries" 2741 /*@ 2742 MatZeroEntries - Zeros all entries of a matrix. For sparse matrices 2743 this routine retains the old nonzero structure. 2744 2745 Collective on Mat 2746 2747 Input Parameters: 2748 . mat - the matrix 2749 2750 Level: intermediate 2751 2752 .keywords: matrix, zero, entries 2753 2754 .seealso: MatZeroRows() 2755 @*/ 2756 int MatZeroEntries(Mat mat) 2757 { 2758 int ierr; 2759 2760 PetscFunctionBegin; 2761 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2762 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 2763 if (!mat->ops->zeroentries) SETERRQ(PETSC_ERR_SUP,0,""); 2764 2765 PLogEventBegin(MAT_ZeroEntries,mat,0,0,0); 2766 ierr = (*mat->ops->zeroentries)(mat);CHKERRQ(ierr); 2767 PLogEventEnd(MAT_ZeroEntries,mat,0,0,0); 2768 PetscFunctionReturn(0); 2769 } 2770 2771 #undef __FUNC__ 2772 #define __FUNC__ "MatZeroRows" 2773 /*@C 2774 MatZeroRows - Zeros all entries (except possibly the main diagonal) 2775 of a set of rows of a matrix. 2776 2777 Collective on Mat 2778 2779 Input Parameters: 2780 + mat - the matrix 2781 . is - index set of rows to remove 2782 - diag - pointer to value put in all diagonals of eliminated rows. 2783 Note that diag is not a pointer to an array, but merely a 2784 pointer to a single value. 2785 2786 Notes: 2787 For the AIJ matrix formats this removes the old nonzero structure, 2788 but does not release memory. For the dense and block diagonal 2789 formats this does not alter the nonzero structure. 2790 2791 The user can set a value in the diagonal entry (or for the AIJ and 2792 row formats can optionally remove the main diagonal entry from the 2793 nonzero structure as well, by passing a null pointer (PETSC_NULL 2794 in C or PETSC_NULL_SCALAR in Fortran) as the final argument). 2795 2796 For the parallel case, all processes that share the matrix (i.e., 2797 those in the communicator used for matrix creation) MUST call this 2798 routine, regardless of whether any rows being zeroed are owned by 2799 them. 2800 2801 Level: intermediate 2802 2803 .keywords: matrix, zero, rows, boundary conditions 2804 2805 .seealso: MatZeroEntries(), MatZeroRowsLocal() 2806 @*/ 2807 int MatZeroRows(Mat mat,IS is, Scalar *diag) 2808 { 2809 int ierr; 2810 2811 PetscFunctionBegin; 2812 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2813 PetscValidHeaderSpecific(is,IS_COOKIE); 2814 if (diag) PetscValidScalarPointer(diag); 2815 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 2816 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 2817 if (!mat->ops->zerorows) SETERRQ(PETSC_ERR_SUP,0,""); 2818 2819 ierr = (*mat->ops->zerorows)(mat,is,diag);CHKERRQ(ierr); 2820 ierr = MatView_Private(mat);CHKERRQ(ierr); 2821 PetscFunctionReturn(0); 2822 } 2823 2824 #undef __FUNC__ 2825 #define __FUNC__ "MatZeroRowsLocal" 2826 /*@C 2827 MatZeroRowsLocal - Zeros all entries (except possibly the main diagonal) 2828 of a set of rows of a matrix; using local numbering of rows. 2829 2830 Collective on Mat 2831 2832 Input Parameters: 2833 + mat - the matrix 2834 . is - index set of rows to remove 2835 - diag - pointer to value put in all diagonals of eliminated rows. 2836 Note that diag is not a pointer to an array, but merely a 2837 pointer to a single value. 2838 2839 Notes: 2840 For the AIJ matrix formats this removes the old nonzero structure, 2841 but does not release memory. For the dense and block diagonal 2842 formats this does not alter the nonzero structure. 2843 2844 The user can set a value in the diagonal entry (or for the AIJ and 2845 row formats can optionally remove the main diagonal entry from the 2846 nonzero structure as well, by passing a null pointer (PETSC_NULL 2847 in C or PETSC_NULL_SCALAR in Fortran) as the final argument). 2848 2849 Level: intermediate 2850 2851 .keywords: matrix, zero, rows, boundary conditions 2852 2853 .seealso: MatZeroEntries(), MatZeroRows() 2854 @*/ 2855 int MatZeroRowsLocal(Mat mat,IS is, Scalar *diag) 2856 { 2857 int ierr; 2858 IS newis; 2859 2860 PetscFunctionBegin; 2861 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2862 PetscValidHeaderSpecific(is,IS_COOKIE); 2863 if (diag) PetscValidScalarPointer(diag); 2864 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 2865 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 2866 if (!mat->ops->zerorows) SETERRQ(PETSC_ERR_SUP,0,""); 2867 2868 ierr = ISLocalToGlobalMappingApplyIS(mat->mapping,is,&newis);CHKERRQ(ierr); 2869 ierr = (*mat->ops->zerorows)(mat,newis,diag);CHKERRQ(ierr); 2870 ierr = ISDestroy(newis);CHKERRQ(ierr); 2871 PetscFunctionReturn(0); 2872 } 2873 2874 #undef __FUNC__ 2875 #define __FUNC__ "MatGetSize" 2876 /*@ 2877 MatGetSize - Returns the numbers of rows and columns in a matrix. 2878 2879 Not Collective 2880 2881 Input Parameter: 2882 . mat - the matrix 2883 2884 Output Parameters: 2885 + m - the number of global rows 2886 - n - the number of global columns 2887 2888 Level: beginner 2889 2890 .keywords: matrix, dimension, size, rows, columns, global, get 2891 2892 .seealso: MatGetLocalSize() 2893 @*/ 2894 int MatGetSize(Mat mat,int *m,int* n) 2895 { 2896 int ierr; 2897 2898 PetscFunctionBegin; 2899 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2900 ierr = (*mat->ops->getsize)(mat,m,n);CHKERRQ(ierr); 2901 PetscFunctionReturn(0); 2902 } 2903 2904 #undef __FUNC__ 2905 #define __FUNC__ "MatGetLocalSize" 2906 /*@ 2907 MatGetLocalSize - Returns the number of rows and columns in a matrix 2908 stored locally. This information may be implementation dependent, so 2909 use with care. 2910 2911 Not Collective 2912 2913 Input Parameters: 2914 . mat - the matrix 2915 2916 Output Parameters: 2917 + m - the number of local rows 2918 - n - the number of local columns 2919 2920 Level: beginner 2921 2922 .keywords: matrix, dimension, size, local, rows, columns, get 2923 2924 .seealso: MatGetSize() 2925 @*/ 2926 int MatGetLocalSize(Mat mat,int *m,int* n) 2927 { 2928 int ierr; 2929 2930 PetscFunctionBegin; 2931 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2932 ierr = (*mat->ops->getlocalsize)(mat,m,n);CHKERRQ(ierr); 2933 PetscFunctionReturn(0); 2934 } 2935 2936 #undef __FUNC__ 2937 #define __FUNC__ "MatGetOwnershipRange" 2938 /*@ 2939 MatGetOwnershipRange - Returns the range of matrix rows owned by 2940 this processor, assuming that the matrix is laid out with the first 2941 n1 rows on the first processor, the next n2 rows on the second, etc. 2942 For certain parallel layouts this range may not be well defined. 2943 2944 Not Collective 2945 2946 Input Parameters: 2947 . mat - the matrix 2948 2949 Output Parameters: 2950 + m - the global index of the first local row 2951 - n - one more than the global index of the last local row 2952 2953 Level: beginner 2954 2955 .keywords: matrix, get, range, ownership 2956 @*/ 2957 int MatGetOwnershipRange(Mat mat,int *m,int* n) 2958 { 2959 int ierr; 2960 2961 PetscFunctionBegin; 2962 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2963 PetscValidIntPointer(m); 2964 PetscValidIntPointer(n); 2965 if (!mat->ops->getownershiprange) SETERRQ(PETSC_ERR_SUP,0,""); 2966 ierr = (*mat->ops->getownershiprange)(mat,m,n);CHKERRQ(ierr); 2967 PetscFunctionReturn(0); 2968 } 2969 2970 #undef __FUNC__ 2971 #define __FUNC__ "MatILUFactorSymbolic" 2972 /*@ 2973 MatILUFactorSymbolic - Performs symbolic ILU factorization of a matrix. 2974 Uses levels of fill only, not drop tolerance. Use MatLUFactorNumeric() 2975 to complete the factorization. 2976 2977 Collective on Mat 2978 2979 Input Parameters: 2980 + mat - the matrix 2981 . row - row permutation 2982 . column - column permutation 2983 - info - structure containing 2984 $ levels - number of levels of fill. 2985 $ expected fill - as ratio of original fill. 2986 $ 1 or 0 - indicating force fill on diagonal (improves robustness for matrices 2987 missing diagonal entries) 2988 2989 Output Parameters: 2990 . fact - new matrix that has been symbolically factored 2991 2992 Notes: 2993 See the users manual for additional information about 2994 choosing the fill factor for better efficiency. 2995 2996 Most users should employ the simplified SLES interface for linear solvers 2997 instead of working directly with matrix algebra routines such as this. 2998 See, e.g., SLESCreate(). 2999 3000 Level: developer 3001 3002 .keywords: matrix, factor, incomplete, ILU, symbolic, fill 3003 3004 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor() 3005 MatGetOrdering() 3006 3007 @*/ 3008 int MatILUFactorSymbolic(Mat mat,IS row,IS col,MatILUInfo *info,Mat *fact) 3009 { 3010 int ierr; 3011 3012 PetscFunctionBegin; 3013 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3014 PetscValidPointer(fact); 3015 if (info && info->levels < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,0,"Levels of fill negative %d",info->levels); 3016 if (info && info->fill < 1.0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,0,"Expected fill less than 1.0 %g",info->fill); 3017 if (!mat->ops->ilufactorsymbolic) SETERRQ(PETSC_ERR_SUP,0,"Only MatCreateMPIRowbs() matrices support parallel ILU"); 3018 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 3019 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 3020 3021 PLogEventBegin(MAT_ILUFactorSymbolic,mat,row,col,0); 3022 ierr = (*mat->ops->ilufactorsymbolic)(mat,row,col,info,fact);CHKERRQ(ierr); 3023 PLogEventEnd(MAT_ILUFactorSymbolic,mat,row,col,0); 3024 PetscFunctionReturn(0); 3025 } 3026 3027 #undef __FUNC__ 3028 #define __FUNC__ "MatIncompleteCholeskyFactorSymbolic" 3029 /*@ 3030 MatIncompleteCholeskyFactorSymbolic - Performs symbolic incomplete 3031 Cholesky factorization for a symmetric matrix. Use 3032 MatCholeskyFactorNumeric() to complete the factorization. 3033 3034 Collective on Mat 3035 3036 Input Parameters: 3037 + mat - the matrix 3038 . perm - row and column permutation 3039 . fill - levels of fill 3040 - f - expected fill as ratio of original fill 3041 3042 Output Parameter: 3043 . fact - the factored matrix 3044 3045 Notes: 3046 Currently only no-fill factorization is supported. 3047 3048 Most users should employ the simplified SLES interface for linear solvers 3049 instead of working directly with matrix algebra routines such as this. 3050 See, e.g., SLESCreate(). 3051 3052 Level: developer 3053 3054 .keywords: matrix, factor, incomplete, ICC, Cholesky, symbolic, fill 3055 3056 .seealso: MatCholeskyFactorNumeric(), MatCholeskyFactor() 3057 @*/ 3058 int MatIncompleteCholeskyFactorSymbolic(Mat mat,IS perm,double f,int fill,Mat *fact) 3059 { 3060 int ierr; 3061 3062 PetscFunctionBegin; 3063 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3064 PetscValidPointer(fact); 3065 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 3066 if (fill < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,0,"Fill negative %d",fill); 3067 if (f < 1.0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,0,"Expected fill less than 1.0 %g",f); 3068 if (!mat->ops->incompletecholeskyfactorsymbolic) SETERRQ(PETSC_ERR_SUP,0,"Currently only MatCreateMPIRowbs() matrices support ICC in parallel"); 3069 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 3070 3071 PLogEventBegin(MAT_IncompleteCholeskyFactorSymbolic,mat,perm,0,0); 3072 ierr = (*mat->ops->incompletecholeskyfactorsymbolic)(mat,perm,f,fill,fact);CHKERRQ(ierr); 3073 PLogEventEnd(MAT_IncompleteCholeskyFactorSymbolic,mat,perm,0,0); 3074 PetscFunctionReturn(0); 3075 } 3076 3077 #undef __FUNC__ 3078 #define __FUNC__ "MatGetArray" 3079 /*@C 3080 MatGetArray - Returns a pointer to the element values in the matrix. 3081 The result of this routine is dependent on the underlying matrix data 3082 structure, and may not even work for certain matrix types. You MUST 3083 call MatRestoreArray() when you no longer need to access the array. 3084 3085 Not Collective 3086 3087 Input Parameter: 3088 . mat - the matrix 3089 3090 Output Parameter: 3091 . v - the location of the values 3092 3093 Currently returns an array only for the dense formats, giving access to 3094 the local portion of the matrix in the usual Fortran column-oriented format. 3095 3096 Fortran Note: 3097 This routine is used differently from Fortran, e.g., 3098 .vb 3099 Mat mat 3100 Scalar mat_array(1) 3101 PetscOffset i_mat 3102 int ierr 3103 call MatGetArray(mat,mat_array,i_mat,ierr) 3104 3105 C Access first local entry in matrix; note that array is 3106 C treated as one dimensional 3107 value = mat_array(i_mat + 1) 3108 3109 [... other code ...] 3110 call MatRestoreArray(mat,mat_array,i_mat,ierr) 3111 .ve 3112 3113 See the Fortran chapter of the users manual and 3114 petsc/src/mat/examples/tests for details. 3115 3116 Level: advanced 3117 3118 .keywords: matrix, array, elements, values 3119 3120 .seealso: MatRestoreArray(), MatGetArrayF90() 3121 @*/ 3122 int MatGetArray(Mat mat,Scalar **v) 3123 { 3124 int ierr; 3125 3126 PetscFunctionBegin; 3127 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3128 PetscValidPointer(v); 3129 if (!mat->ops->getarray) SETERRQ(PETSC_ERR_SUP,0,""); 3130 ierr = (*mat->ops->getarray)(mat,v);CHKERRQ(ierr); 3131 PetscFunctionReturn(0); 3132 } 3133 3134 #undef __FUNC__ 3135 #define __FUNC__ "MatRestoreArray" 3136 /*@C 3137 MatRestoreArray - Restores the matrix after MatGetArray() has been called. 3138 3139 Not Collective 3140 3141 Input Parameter: 3142 + mat - the matrix 3143 - v - the location of the values 3144 3145 Fortran Note: 3146 This routine is used differently from Fortran, e.g., 3147 .vb 3148 Mat mat 3149 Scalar mat_array(1) 3150 PetscOffset i_mat 3151 int ierr 3152 call MatGetArray(mat,mat_array,i_mat,ierr) 3153 3154 C Access first local entry in matrix; note that array is 3155 C treated as one dimensional 3156 value = mat_array(i_mat + 1) 3157 3158 [... other code ...] 3159 call MatRestoreArray(mat,mat_array,i_mat,ierr) 3160 .ve 3161 3162 See the Fortran chapter of the users manual and 3163 petsc/src/mat/examples/tests for details 3164 3165 Level: advanced 3166 3167 .keywords: matrix, array, elements, values, restore 3168 3169 .seealso: MatGetArray(), MatRestoreArrayF90() 3170 @*/ 3171 int MatRestoreArray(Mat mat,Scalar **v) 3172 { 3173 int ierr; 3174 3175 PetscFunctionBegin; 3176 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3177 PetscValidPointer(v); 3178 if (!mat->ops->restorearray) SETERRQ(PETSC_ERR_SUP,0,""); 3179 ierr = (*mat->ops->restorearray)(mat,v);CHKERRQ(ierr); 3180 PetscFunctionReturn(0); 3181 } 3182 3183 #undef __FUNC__ 3184 #define __FUNC__ "MatGetSubMatrices" 3185 /*@C 3186 MatGetSubMatrices - Extracts several submatrices from a matrix. If submat 3187 points to an array of valid matrices, they may be reused to store the new 3188 submatrices. 3189 3190 Collective on Mat 3191 3192 Input Parameters: 3193 + mat - the matrix 3194 . n - the number of submatrixes to be extracted 3195 . irow, icol - index sets of rows and columns to extract 3196 - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 3197 3198 Output Parameter: 3199 . submat - the array of submatrices 3200 3201 Notes: 3202 MatGetSubMatrices() can extract only sequential submatrices 3203 (from both sequential and parallel matrices). Use MatGetSubMatrix() 3204 to extract a parallel submatrix. 3205 3206 When extracting submatrices from a parallel matrix, each processor can 3207 form a different submatrix by setting the rows and columns of its 3208 individual index sets according to the local submatrix desired. 3209 3210 When finished using the submatrices, the user should destroy 3211 them with MatDestroySubMatrices(). 3212 3213 MAT_REUSE_MATRIX can only be used when the nonzero structure of the 3214 original matrix has not changed from that last call to MatGetSubMatrices(). 3215 3216 Fortran Note: 3217 The Fortran interface is slightly different from that given below; it 3218 requires one to pass in as submat a Mat (integer) array of size at least m. 3219 3220 Level: advanced 3221 3222 .keywords: matrix, get, submatrix, submatrices 3223 3224 .seealso: MatDestroyMatrices(), MatGetSubMatrix(), MatGetRow(), MatGetDiagonal() 3225 @*/ 3226 int MatGetSubMatrices(Mat mat,int n,IS *irow,IS *icol,MatReuse scall,Mat **submat) 3227 { 3228 int ierr; 3229 3230 PetscFunctionBegin; 3231 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3232 if (!mat->ops->getsubmatrices) SETERRQ(PETSC_ERR_SUP,0,""); 3233 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 3234 3235 PLogEventBegin(MAT_GetSubMatrices,mat,0,0,0); 3236 ierr = (*mat->ops->getsubmatrices)(mat,n,irow,icol,scall,submat);CHKERRQ(ierr); 3237 PLogEventEnd(MAT_GetSubMatrices,mat,0,0,0); 3238 3239 PetscFunctionReturn(0); 3240 } 3241 3242 #undef __FUNC__ 3243 #define __FUNC__ "MatDestroyMatrices" 3244 /*@C 3245 MatDestroyMatrices - Destroys a set of matrices obtained with MatGetSubMatrices(). 3246 3247 Collective on Mat 3248 3249 Input Parameters: 3250 + n - the number of local matrices 3251 - mat - the matrices 3252 3253 Level: advanced 3254 3255 .keywords: matrix, destroy, submatrix, submatrices 3256 3257 .seealso: MatGetSubMatrices() 3258 @*/ 3259 int MatDestroyMatrices(int n,Mat **mat) 3260 { 3261 int ierr,i; 3262 3263 PetscFunctionBegin; 3264 if (n < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,1,"Trying to destroy negative number of matrices %d",n); 3265 PetscValidPointer(mat); 3266 for ( i=0; i<n; i++ ) { 3267 ierr = MatDestroy((*mat)[i]);CHKERRQ(ierr); 3268 } 3269 if (n) {ierr = PetscFree(*mat);CHKERRQ(ierr);} 3270 PetscFunctionReturn(0); 3271 } 3272 3273 #undef __FUNC__ 3274 #define __FUNC__ "MatIncreaseOverlap" 3275 /*@ 3276 MatIncreaseOverlap - Given a set of submatrices indicated by index sets, 3277 replaces the index sets by larger ones that represent submatrices with 3278 additional overlap. 3279 3280 Collective on Mat 3281 3282 Input Parameters: 3283 + mat - the matrix 3284 . n - the number of index sets 3285 . is - the array of pointers to index sets 3286 - ov - the additional overlap requested 3287 3288 Level: developer 3289 3290 .keywords: matrix, overlap, Schwarz 3291 3292 .seealso: MatGetSubMatrices() 3293 @*/ 3294 int MatIncreaseOverlap(Mat mat,int n, IS *is,int ov) 3295 { 3296 int ierr; 3297 3298 PetscFunctionBegin; 3299 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3300 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 3301 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 3302 3303 if (ov == 0) PetscFunctionReturn(0); 3304 if (!mat->ops->increaseoverlap) SETERRQ(PETSC_ERR_SUP,0,""); 3305 PLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0); 3306 ierr = (*mat->ops->increaseoverlap)(mat,n,is,ov);CHKERRQ(ierr); 3307 PLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0); 3308 PetscFunctionReturn(0); 3309 } 3310 3311 #undef __FUNC__ 3312 #define __FUNC__ "MatPrintHelp" 3313 /*@ 3314 MatPrintHelp - Prints all the options for the matrix. 3315 3316 Collective on Mat 3317 3318 Input Parameter: 3319 . mat - the matrix 3320 3321 Options Database Keys: 3322 + -help - Prints matrix options 3323 - -h - Prints matrix options 3324 3325 Level: developer 3326 3327 .keywords: mat, help 3328 3329 .seealso: MatCreate(), MatCreateXXX() 3330 @*/ 3331 int MatPrintHelp(Mat mat) 3332 { 3333 static int called = 0; 3334 int ierr; 3335 MPI_Comm comm; 3336 3337 PetscFunctionBegin; 3338 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3339 3340 comm = mat->comm; 3341 if (!called) { 3342 ierr = (*PetscHelpPrintf)(comm,"General matrix options:\n");CHKERRQ(ierr); 3343 ierr = (*PetscHelpPrintf)(comm," -mat_view_info: view basic matrix info during MatAssemblyEnd()\n");CHKERRQ(ierr); 3344 ierr = (*PetscHelpPrintf)(comm," -mat_view_info_detailed: view detailed matrix info during MatAssemblyEnd()\n");CHKERRQ(ierr); 3345 ierr = (*PetscHelpPrintf)(comm," -mat_view_draw: draw nonzero matrix structure during MatAssemblyEnd()\n");CHKERRQ(ierr); 3346 ierr = (*PetscHelpPrintf)(comm," -draw_pause <sec>: set seconds of display pause\n");CHKERRQ(ierr); 3347 ierr = (*PetscHelpPrintf)(comm," -display <name>: set alternate display\n");CHKERRQ(ierr); 3348 called = 1; 3349 } 3350 if (mat->ops->printhelp) { 3351 ierr = (*mat->ops->printhelp)(mat);CHKERRQ(ierr); 3352 } 3353 PetscFunctionReturn(0); 3354 } 3355 3356 #undef __FUNC__ 3357 #define __FUNC__ "MatGetBlockSize" 3358 /*@ 3359 MatGetBlockSize - Returns the matrix block size; useful especially for the 3360 block row and block diagonal formats. 3361 3362 Not Collective 3363 3364 Input Parameter: 3365 . mat - the matrix 3366 3367 Output Parameter: 3368 . bs - block size 3369 3370 Notes: 3371 Block diagonal formats are MATSEQBDIAG, MATMPIBDIAG. 3372 Block row formats are MATSEQBAIJ, MATMPIBAIJ 3373 3374 Level: intermediate 3375 3376 .keywords: matrix, get, block, size 3377 3378 .seealso: MatCreateSeqBAIJ(), MatCreateMPIBAIJ(), MatCreateSeqBDiag(), MatCreateMPIBDiag() 3379 @*/ 3380 int MatGetBlockSize(Mat mat,int *bs) 3381 { 3382 int ierr; 3383 3384 PetscFunctionBegin; 3385 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3386 PetscValidIntPointer(bs); 3387 if (!mat->ops->getblocksize) SETERRQ(PETSC_ERR_SUP,0,""); 3388 ierr = (*mat->ops->getblocksize)(mat,bs);CHKERRQ(ierr); 3389 PetscFunctionReturn(0); 3390 } 3391 3392 #undef __FUNC__ 3393 #define __FUNC__ "MatGetRowIJ" 3394 /*@C 3395 MatGetRowIJ - Returns the compressed row storage i and j indices for sequential matrices. 3396 3397 Collective on Mat 3398 3399 Input Parameters: 3400 + mat - the matrix 3401 . shift - 0 or 1 indicating we want the indices starting at 0 or 1 3402 - symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 3403 symmetrized 3404 3405 Output Parameters: 3406 + n - number of rows in the (possibly compressed) matrix 3407 . ia - the row pointers 3408 . ja - the column indices 3409 - done - PETSC_TRUE or PETSC_FALSE, indicating whether the values have been returned 3410 3411 Level: developer 3412 3413 .seealso: MatGetColumnIJ(), MatRestoreRowIJ() 3414 @*/ 3415 int MatGetRowIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int **ia,int** ja,PetscTruth *done) 3416 { 3417 int ierr; 3418 3419 PetscFunctionBegin; 3420 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3421 if (ia) PetscValidIntPointer(ia); 3422 if (ja) PetscValidIntPointer(ja); 3423 PetscValidIntPointer(done); 3424 if (!mat->ops->getrowij) *done = PETSC_FALSE; 3425 else { 3426 *done = PETSC_TRUE; 3427 ierr = (*mat->ops->getrowij)(mat,shift,symmetric,n,ia,ja,done);CHKERRQ(ierr); 3428 } 3429 PetscFunctionReturn(0); 3430 } 3431 3432 #undef __FUNC__ 3433 #define __FUNC__ "MatGetColumnIJ" 3434 /*@C 3435 MatGetColumnIJ - Returns the compressed column storage i and j indices for sequential matrices. 3436 3437 Collective on Mat 3438 3439 Input Parameters: 3440 + mat - the matrix 3441 . shift - 1 or zero indicating we want the indices starting at 0 or 1 3442 - symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 3443 symmetrized 3444 3445 Output Parameters: 3446 + n - number of columns in the (possibly compressed) matrix 3447 . ia - the column pointers 3448 . ja - the row indices 3449 - done - PETSC_TRUE or PETSC_FALSE, indicating whether the values have been returned 3450 3451 Level: developer 3452 3453 .seealso: MatGetRowIJ(), MatRestoreColumnIJ() 3454 @*/ 3455 int MatGetColumnIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int **ia,int** ja,PetscTruth *done) 3456 { 3457 int ierr; 3458 3459 PetscFunctionBegin; 3460 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3461 if (ia) PetscValidIntPointer(ia); 3462 if (ja) PetscValidIntPointer(ja); 3463 PetscValidIntPointer(done); 3464 3465 if (!mat->ops->getcolumnij) *done = PETSC_FALSE; 3466 else { 3467 *done = PETSC_TRUE; 3468 ierr = (*mat->ops->getcolumnij)(mat,shift,symmetric,n,ia,ja,done);CHKERRQ(ierr); 3469 } 3470 PetscFunctionReturn(0); 3471 } 3472 3473 #undef __FUNC__ 3474 #define __FUNC__ "MatRestoreRowIJ" 3475 /*@C 3476 MatRestoreRowIJ - Call after you are completed with the ia,ja indices obtained with 3477 MatGetRowIJ(). 3478 3479 Collective on Mat 3480 3481 Input Parameters: 3482 + mat - the matrix 3483 . shift - 1 or zero indicating we want the indices starting at 0 or 1 3484 - symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 3485 symmetrized 3486 3487 Output Parameters: 3488 + n - size of (possibly compressed) matrix 3489 . ia - the row pointers 3490 . ja - the column indices 3491 - done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned 3492 3493 Level: developer 3494 3495 .seealso: MatGetRowIJ(), MatRestoreColumnIJ() 3496 @*/ 3497 int MatRestoreRowIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int **ia,int** ja,PetscTruth *done) 3498 { 3499 int ierr; 3500 3501 PetscFunctionBegin; 3502 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3503 if (ia) PetscValidIntPointer(ia); 3504 if (ja) PetscValidIntPointer(ja); 3505 PetscValidIntPointer(done); 3506 3507 if (!mat->ops->restorerowij) *done = PETSC_FALSE; 3508 else { 3509 *done = PETSC_TRUE; 3510 ierr = (*mat->ops->restorerowij)(mat,shift,symmetric,n,ia,ja,done);CHKERRQ(ierr); 3511 } 3512 PetscFunctionReturn(0); 3513 } 3514 3515 #undef __FUNC__ 3516 #define __FUNC__ "MatRestoreColumnIJ" 3517 /*@C 3518 MatRestoreColumnIJ - Call after you are completed with the ia,ja indices obtained with 3519 MatGetColumnIJ(). 3520 3521 Collective on Mat 3522 3523 Input Parameters: 3524 + mat - the matrix 3525 . shift - 1 or zero indicating we want the indices starting at 0 or 1 3526 - symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 3527 symmetrized 3528 3529 Output Parameters: 3530 + n - size of (possibly compressed) matrix 3531 . ia - the column pointers 3532 . ja - the row indices 3533 - done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned 3534 3535 Level: developer 3536 3537 .seealso: MatGetColumnIJ(), MatRestoreRowIJ() 3538 @*/ 3539 int MatRestoreColumnIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int **ia,int** ja,PetscTruth *done) 3540 { 3541 int ierr; 3542 3543 PetscFunctionBegin; 3544 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3545 if (ia) PetscValidIntPointer(ia); 3546 if (ja) PetscValidIntPointer(ja); 3547 PetscValidIntPointer(done); 3548 3549 if (!mat->ops->restorecolumnij) *done = PETSC_FALSE; 3550 else { 3551 *done = PETSC_TRUE; 3552 ierr = (*mat->ops->restorecolumnij)(mat,shift,symmetric,n,ia,ja,done);CHKERRQ(ierr); 3553 } 3554 PetscFunctionReturn(0); 3555 } 3556 3557 #undef __FUNC__ 3558 #define __FUNC__ "MatColoringPatch" 3559 /*@C 3560 MatColoringPatch -Used inside matrix coloring routines that 3561 use MatGetRowIJ() and/or MatGetColumnIJ(). 3562 3563 Collective on Mat 3564 3565 Input Parameters: 3566 + mat - the matrix 3567 . n - number of colors 3568 - colorarray - array indicating color for each column 3569 3570 Output Parameters: 3571 . iscoloring - coloring generated using colorarray information 3572 3573 Level: developer 3574 3575 .seealso: MatGetRowIJ(), MatGetColumnIJ() 3576 3577 .keywords: mat, coloring, patch 3578 @*/ 3579 int MatColoringPatch(Mat mat,int n,int *colorarray,ISColoring *iscoloring) 3580 { 3581 int ierr; 3582 3583 PetscFunctionBegin; 3584 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3585 PetscValidIntPointer(colorarray); 3586 3587 if (!mat->ops->coloringpatch) {SETERRQ(PETSC_ERR_SUP,0,"");} 3588 else { 3589 ierr = (*mat->ops->coloringpatch)(mat,n,colorarray,iscoloring);CHKERRQ(ierr); 3590 } 3591 PetscFunctionReturn(0); 3592 } 3593 3594 3595 #undef __FUNC__ 3596 #define __FUNC__ "MatSetUnfactored" 3597 /*@ 3598 MatSetUnfactored - Resets a factored matrix to be treated as unfactored. 3599 3600 Collective on Mat 3601 3602 Input Parameter: 3603 . mat - the factored matrix to be reset 3604 3605 Notes: 3606 This routine should be used only with factored matrices formed by in-place 3607 factorization via ILU(0) (or by in-place LU factorization for the MATSEQDENSE 3608 format). This option can save memory, for example, when solving nonlinear 3609 systems with a matrix-free Newton-Krylov method and a matrix-based, in-place 3610 ILU(0) preconditioner. 3611 3612 Note that one can specify in-place ILU(0) factorization by calling 3613 .vb 3614 PCType(pc,PCILU); 3615 PCILUSeUseInPlace(pc); 3616 .ve 3617 or by using the options -pc_type ilu -pc_ilu_in_place 3618 3619 In-place factorization ILU(0) can also be used as a local 3620 solver for the blocks within the block Jacobi or additive Schwarz 3621 methods (runtime option: -sub_pc_ilu_in_place). See the discussion 3622 of these preconditioners in the users manual for details on setting 3623 local solver options. 3624 3625 Most users should employ the simplified SLES interface for linear solvers 3626 instead of working directly with matrix algebra routines such as this. 3627 See, e.g., SLESCreate(). 3628 3629 Level: developer 3630 3631 .seealso: PCILUSetUseInPlace(), PCLUSetUseInPlace() 3632 3633 .keywords: matrix-free, in-place ILU, in-place LU 3634 @*/ 3635 int MatSetUnfactored(Mat mat) 3636 { 3637 int ierr; 3638 3639 PetscFunctionBegin; 3640 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3641 mat->factor = 0; 3642 if (!mat->ops->setunfactored) PetscFunctionReturn(0); 3643 ierr = (*mat->ops->setunfactored)(mat);CHKERRQ(ierr); 3644 PetscFunctionReturn(0); 3645 } 3646 3647 #undef __FUNC__ 3648 #define __FUNC__ "MatGetType" 3649 /*@C 3650 MatGetType - Gets the matrix type and name (as a string) from the matrix. 3651 3652 Not Collective 3653 3654 Input Parameter: 3655 . mat - the matrix 3656 3657 Output Parameter: 3658 + type - the matrix type (or use PETSC_NULL) 3659 - name - name of matrix type (or use PETSC_NULL) 3660 3661 Level: intermediate 3662 3663 .keywords: matrix, get, type, name 3664 @*/ 3665 int MatGetType(Mat mat,MatType *type,char **name) 3666 { 3667 int itype = (int)mat->type; 3668 char *matname[10]; 3669 3670 PetscFunctionBegin; 3671 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3672 3673 if (type) *type = (MatType) mat->type; 3674 if (name) { 3675 /* Note: Be sure that this list corresponds to the enum in mat.h */ 3676 matname[0] = "MATSEQDENSE"; 3677 matname[1] = "MATSEQAIJ"; 3678 matname[2] = "MATMPIAIJ"; 3679 matname[3] = "MATSHELL"; 3680 matname[4] = "MATMPIROWBS"; 3681 matname[5] = "MATSEQBDIAG"; 3682 matname[6] = "MATMPIBDIAG"; 3683 matname[7] = "MATMPIDENSE"; 3684 matname[8] = "MATSEQBAIJ"; 3685 matname[9] = "MATMPIBAIJ"; 3686 3687 if (itype < 0 || itype > 9) *name = "Unknown matrix type"; 3688 else *name = matname[itype]; 3689 } 3690 PetscFunctionReturn(0); 3691 } 3692 3693 /*MC 3694 MatGetArrayF90 - Accesses a matrix array from Fortran90. 3695 3696 Synopsis: 3697 MatGetArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr) 3698 3699 Not collective 3700 3701 Input Parameter: 3702 . x - matrix 3703 3704 Output Parameters: 3705 + xx_v - the Fortran90 pointer to the array 3706 - ierr - error code 3707 3708 Example of Usage: 3709 .vb 3710 Scalar, pointer xx_v(:) 3711 .... 3712 call MatGetArrayF90(x,xx_v,ierr) 3713 a = xx_v(3) 3714 call MatRestoreArrayF90(x,xx_v,ierr) 3715 .ve 3716 3717 Notes: 3718 Not yet supported for all F90 compilers 3719 3720 Level: advanced 3721 3722 .seealso: MatRestoreArrayF90(), MatGetArray(), MatRestoreArray() 3723 3724 .keywords: matrix, array, f90 3725 M*/ 3726 3727 /*MC 3728 MatRestoreArrayF90 - Restores a matrix array that has been 3729 accessed with MatGetArrayF90(). 3730 3731 Synopsis: 3732 MatRestoreArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr) 3733 3734 Not collective 3735 3736 Input Parameters: 3737 + x - matrix 3738 - xx_v - the Fortran90 pointer to the array 3739 3740 Output Parameter: 3741 . ierr - error code 3742 3743 Example of Usage: 3744 .vb 3745 Scalar, pointer xx_v(:) 3746 .... 3747 call MatGetArrayF90(x,xx_v,ierr) 3748 a = xx_v(3) 3749 call MatRestoreArrayF90(x,xx_v,ierr) 3750 .ve 3751 3752 Notes: 3753 Not yet supported for all F90 compilers 3754 3755 Level: advanced 3756 3757 .seealso: MatGetArrayF90(), MatGetArray(), MatRestoreArray() 3758 3759 .keywords: matrix, array, f90 3760 M*/ 3761 3762 3763 #undef __FUNC__ 3764 #define __FUNC__ "MatGetSubMatrix" 3765 /*@ 3766 MatGetSubMatrix - Gets a single submatrix on the same number of processors 3767 as the original matrix. 3768 3769 Collective on Mat 3770 3771 Input Parameters: 3772 + mat - the original matrix 3773 . isrow - rows this processor should obtain 3774 . iscol - columns for all processors you wish to keep 3775 . csize - number of columns "local" to this processor (does nothing for sequential 3776 matrices). This should match the result from VecGetLocalSize(x,...) if you 3777 plan to use the matrix in a A*x; alternatively, you can use PETSC_DECIDE 3778 - cll - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 3779 3780 Output Parameter: 3781 . newmat - the new submatrix, of the same type as the old 3782 3783 Level: advanced 3784 3785 .keywords: matrix, get, submatrix, submatrices 3786 3787 .seealso: MatGetSubMatrices() 3788 @*/ 3789 int MatGetSubMatrix(Mat mat,IS isrow,IS iscol,int csize,MatReuse cll,Mat *newmat) 3790 { 3791 int ierr, size; 3792 Mat *local; 3793 3794 PetscFunctionBegin; 3795 ierr = MPI_Comm_size(mat->comm,&size);CHKERRQ(ierr); 3796 3797 /* if original matrix is on just one processor then use submatrix generated */ 3798 if (!mat->ops->getsubmatrix && size == 1 && cll == MAT_REUSE_MATRIX) { 3799 ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&newmat);CHKERRQ(ierr); 3800 PetscFunctionReturn(0); 3801 } else if (!mat->ops->getsubmatrix && size == 1) { 3802 ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr); 3803 *newmat = *local; 3804 ierr = PetscFree(local);CHKERRQ(ierr); 3805 PetscFunctionReturn(0); 3806 } 3807 3808 if (!mat->ops->getsubmatrix) SETERRQ(PETSC_ERR_SUP,0,"Not currently implemented"); 3809 ierr = (*mat->ops->getsubmatrix)(mat,isrow,iscol,csize,cll,newmat);CHKERRQ(ierr); 3810 PetscFunctionReturn(0); 3811 } 3812 3813 #undef __FUNC__ 3814 #define __FUNC__ "MatGetMaps" 3815 /*@C 3816 MatGetMaps - Returns the maps associated with the matrix. 3817 3818 Not Collective 3819 3820 Input Parameter: 3821 . mat - the matrix 3822 3823 Output Parameters: 3824 + rmap - the row (right) map 3825 - cmap - the column (left) map 3826 3827 Level: developer 3828 3829 .keywords: matrix, get, map 3830 @*/ 3831 int MatGetMaps(Mat mat,Map *rmap,Map *cmap) 3832 { 3833 int ierr; 3834 3835 PetscFunctionBegin; 3836 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3837 ierr = (*mat->ops->getmaps)(mat,rmap,cmap);CHKERRQ(ierr); 3838 PetscFunctionReturn(0); 3839 } 3840 3841 /* 3842 Version that works for all PETSc matrices 3843 */ 3844 #undef __FUNC__ 3845 #define __FUNC__ "MatGetMaps_Petsc" 3846 int MatGetMaps_Petsc(Mat mat,Map *rmap,Map *cmap) 3847 { 3848 PetscFunctionBegin; 3849 if (rmap) *rmap = mat->rmap; 3850 if (cmap) *cmap = mat->cmap; 3851 PetscFunctionReturn(0); 3852 } 3853 3854 #undef __FUNC__ 3855 #define __FUNC__ "MatSetStashInitialSize" 3856 /*@ 3857 MatSetStashInitialSize - sets the sizes of the matrix stash, that is 3858 used during the assembly process to store values that belong to 3859 other processors. 3860 3861 Not Collective 3862 3863 Input Parameters: 3864 + mat - the matrix 3865 . size - the initial size of the stash. 3866 - bsize - the initial size of the block-stash(if used). 3867 3868 Options Database Keys: 3869 + -matstash_initial_size <size> or <size0,size1,...sizep-1> 3870 - -matstash_block_initial_size <bsize> or <bsize0,bsize1,...bsizep-1> 3871 3872 Level: intermediate 3873 3874 Notes: 3875 The block-stash is used for values set with VecSetValuesBlocked() while 3876 the stash is used for values set with VecSetValues() 3877 3878 Run with the option -log_info and look for output of the form 3879 MatAssemblyBegin_MPIXXX:Stash has MM entries, uses nn mallocs. 3880 to determine the appropriate value, MM, to use for size and 3881 MatAssemblyBegin_MPIXXX:Block-Stash has BMM entries, uses nn mallocs. 3882 to determine the value, BMM to use for bsize 3883 3884 .keywords: matrix, stash, assembly 3885 @*/ 3886 int MatSetStashInitialSize(Mat mat,int size, int bsize) 3887 { 3888 int ierr; 3889 3890 PetscFunctionBegin; 3891 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3892 ierr = MatStashSetInitialSize_Private(&mat->stash,size);CHKERRQ(ierr); 3893 ierr = MatStashSetInitialSize_Private(&mat->bstash,bsize);CHKERRQ(ierr); 3894 PetscFunctionReturn(0); 3895 } 3896