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