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