1 #ifndef lint 2 static char vcid[] = "$Id: matrix.c,v 1.122 1996/01/01 01:02:59 bsmith Exp bsmith $"; 3 #endif 4 5 /* 6 This is where the abstract matrix operations are defined 7 */ 8 9 #include "petsc.h" 10 #include "matimpl.h" /*I "mat.h" I*/ 11 #include "vec/vecimpl.h" 12 #include "pinclude/pviewer.h" 13 #include "draw.h" 14 15 /*@C 16 MatGetReordering - Gets a reordering for a matrix to reduce fill or to 17 improve numerical stability of LU factorization. 18 19 Input Parameters: 20 . mat - the matrix 21 . type - type of reordering, one of the following: 22 $ ORDER_NATURAL - Natural 23 $ ORDER_ND - Nested Dissection 24 $ ORDER_1WD - One-way Dissection 25 $ ORDER_RCM - Reverse Cuthill-McGee 26 $ ORDER_QMD - Quotient Minimum Degree 27 28 Output Parameters: 29 . rperm - row permutation indices 30 . cperm - column permutation indices 31 32 Options Database Keys: 33 To specify the ordering through the options database, use one of 34 the following 35 $ -mat_order natural, -mat_order nd, -mat_order 1wd, 36 $ -mat_order rcm, -mat_order qmd 37 38 Notes: 39 If the column permutations and row permutations are the same, 40 then MatGetReordering() returns 0 in cperm. 41 42 The user can define additional orderings; see MatReorderingRegister(). 43 44 .keywords: matrix, set, ordering, factorization, direct, ILU, LU, 45 fill, reordering, natural, Nested Dissection, 46 One-way Dissection, Cholesky, Reverse Cuthill-McGee, 47 Quotient Minimum Degree 48 49 .seealso: MatGetReorderingTypeFromOptions(), MatReorderingRegister() 50 @*/ 51 int MatGetReordering(Mat mat,MatOrdering type,IS *rperm,IS *cperm) 52 { 53 int ierr; 54 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 55 56 if (!mat->ops.getreordering) {*rperm = 0; *cperm = 0; return 0;} 57 PLogEventBegin(MAT_GetReordering,mat,0,0,0); 58 ierr = MatGetReorderingTypeFromOptions(0,&type); CHKERRQ(ierr); 59 ierr = (*mat->ops.getreordering)(mat,type,rperm,cperm); CHKERRQ(ierr); 60 PLogEventEnd(MAT_GetReordering,mat,0,0,0); 61 return 0; 62 } 63 64 /*@C 65 MatGetRow - Gets a row of a matrix. You MUST call MatRestoreRow() 66 for each row that you get to ensure that your application does 67 not bleed memory. 68 69 Input Parameters: 70 . mat - the matrix 71 . row - the row to get 72 73 Output Parameters: 74 . ncols - the number of nonzeros in the row 75 . cols - if nonzero, the column numbers 76 . vals - if nonzero, the values 77 78 Notes: 79 This routine is provided for people who need to have direct access 80 to the structure of a matrix. We hope that we provide enough 81 high-level matrix routines that few users will need it. 82 83 For better efficiency, set cols and/or vals to zero if you do not 84 wish to extract these quantities. 85 86 .keywords: matrix, row, get, extract 87 88 .seealso: MatRestoreRow() 89 @*/ 90 int MatGetRow(Mat mat,int row,int *ncols,int **cols,Scalar **vals) 91 { 92 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 93 return (*mat->ops.getrow)(mat,row,ncols,cols,vals); 94 } 95 96 /*@C 97 MatRestoreRow - Frees any temporary space allocated by MatGetRow(). 98 99 Input Parameters: 100 . mat - the matrix 101 . row - the row to get 102 . ncols, cols - the number of nonzeros and their columns 103 . vals - if nonzero the column values 104 105 .keywords: matrix, row, restore 106 107 .seealso: MatGetRow() 108 @*/ 109 int MatRestoreRow(Mat mat,int row,int *ncols,int **cols,Scalar **vals) 110 { 111 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 112 if (!mat->ops.restorerow) return 0; 113 return (*mat->ops.restorerow)(mat,row,ncols,cols,vals); 114 } 115 /*@ 116 MatView - Visualizes a matrix object. 117 118 Input Parameters: 119 . mat - the matrix 120 . ptr - visualization context 121 122 Notes: 123 The available visualization contexts include 124 $ STDOUT_VIEWER_SELF - standard output (default) 125 $ STDOUT_VIEWER_WORLD - synchronized standard 126 $ output where only the first processor opens 127 $ the file. All other processors send their 128 $ data to the first processor to print. 129 130 The user can open alternative vistualization contexts with 131 $ ViewerFileOpenASCII() - output to a specified file 132 $ ViewerFileOpenBinary() - output in binary to a 133 $ specified file; corresponding input uses MatLoad() 134 $ DrawOpenX() - output nonzero matrix structure to 135 $ an X window display 136 $ ViewerMatlabOpen() - output matrix to Matlab viewer. 137 $ Currently only the sequential dense and AIJ 138 $ matrix types support the Matlab viewer. 139 140 The user can call ViewerFileSetFormat() to specify the output 141 format of ASCII printed objects (when using STDOUT_VIEWER_SELF, 142 STDOUT_VIEWER_WORLD and ViewerFileOpenASCII). Available formats include 143 $ FILE_FORMAT_DEFAULT - default, prints matrix contents 144 $ FILE_FORMAT_IMPL - implementation-specific format 145 $ (which is in many cases the same as the default) 146 $ FILE_FORMAT_INFO - basic information about the matrix 147 $ size and structure (not the matrix entries) 148 $ FILE_FORMAT_INFO_DETAILED - more detailed information about the 149 $ matrix structure 150 151 .keywords: matrix, view, visualize, output, print, write, draw 152 153 .seealso: ViewerFileSetFormat(), ViewerFileOpenASCII(), DrawOpenX(), 154 ViewerMatlabOpen(), MatLoad() 155 @*/ 156 int MatView(Mat mat,Viewer ptr) 157 { 158 int format, ierr, rows, cols,nz, nzalloc, mem; 159 FILE *fd; 160 char *cstring; 161 PetscObject vobj = (PetscObject) ptr; 162 163 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 164 if (!ptr) { /* so that viewers may be used from debuggers */ 165 ptr = STDOUT_VIEWER_SELF; vobj = (PetscObject) ptr; 166 } 167 ierr = ViewerFileGetFormat_Private(ptr,&format); CHKERRQ(ierr); 168 ierr = ViewerFileGetPointer_Private(ptr,&fd); CHKERRQ(ierr); 169 if (vobj->cookie == VIEWER_COOKIE && 170 (format == FILE_FORMAT_INFO || format == FILE_FORMAT_INFO_DETAILED) && 171 (vobj->type == ASCII_FILE_VIEWER || vobj->type == ASCII_FILES_VIEWER)) { 172 MPIU_fprintf(mat->comm,fd,"Matrix Object:\n"); 173 ierr = MatGetType(mat,PETSC_NULL,&cstring); CHKERRQ(ierr); 174 ierr = MatGetSize(mat,&rows,&cols); CHKERRQ(ierr); 175 MPIU_fprintf(mat->comm,fd," type=%s, rows=%d, cols=%d\n",cstring,rows,cols); 176 if (mat->ops.getinfo) { 177 ierr = MatGetInfo(mat,MAT_GLOBAL_SUM,&nz,&nzalloc,&mem); CHKERRQ(ierr); 178 MPIU_fprintf(mat->comm,fd," total: nonzeros=%d, allocated nonzeros=%d\n",nz,nzalloc); 179 } 180 } 181 if (mat->view) {ierr = (*mat->view)((PetscObject)mat,ptr); CHKERRQ(ierr);} 182 return 0; 183 } 184 /*@C 185 MatDestroy - Frees space taken by a matrix. 186 187 Input Parameter: 188 . mat - the matrix 189 190 .keywords: matrix, destroy 191 @*/ 192 int MatDestroy(Mat mat) 193 { 194 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 195 return (*mat->destroy)((PetscObject)mat); 196 } 197 /*@ 198 MatValidMatrix - Returns 1 if a valid matrix else 0. 199 200 Input Parameter: 201 . m - the matrix to check 202 203 .keywords: matrix, valid 204 @*/ 205 int MatValidMatrix(Mat m) 206 { 207 if (!m) return 0; 208 if (m->cookie != MAT_COOKIE) return 0; 209 return 1; 210 } 211 212 /*@ 213 MatSetValues - Inserts or adds a block of values into a matrix. 214 These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd() 215 MUST be called after all calls to MatSetValues() have been completed. 216 217 Input Parameters: 218 . mat - the matrix 219 . v - a logically two-dimensional array of values 220 . m, indexm - the number of rows and their global indices 221 . n, indexn - the number of columns and their global indices 222 . addv - either ADD_VALUES or INSERT_VALUES, where 223 $ ADD_VALUES - adds values to any existing entries 224 $ INSERT_VALUES - replaces existing entries with new values 225 226 Notes: 227 By default the values, v, are row-oriented and unsorted. 228 See MatSetOptions() for other options. 229 230 Calls to MatSetValues() with the INSERT_VALUES and ADD_VALUES 231 options cannot be mixed without intervening calls to the assembly 232 routines. 233 234 .keywords: matrix, insert, add, set, values 235 236 .seealso: MatSetOptions(), MatAssemblyBegin(), MatAssemblyEnd() 237 @*/ 238 int MatSetValues(Mat mat,int m,int *idxm,int n,int *idxn,Scalar *v, 239 InsertMode addv) 240 { 241 int ierr; 242 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 243 PLogEventBegin(MAT_SetValues,mat,0,0,0); 244 ierr = (*mat->ops.setvalues)(mat,m,idxm,n,idxn,v,addv);CHKERRQ(ierr); 245 PLogEventEnd(MAT_SetValues,mat,0,0,0); 246 return 0; 247 } 248 249 /*@ 250 MatGetValues - Gets a block of values from a matrix. 251 252 Input Parameters: 253 . mat - the matrix 254 . v - a logically two-dimensional array for storing the values 255 . m, indexm - the number of rows and their global indices 256 . n, indexn - the number of columns and their global indices 257 258 Notes: 259 The user must allocate space (m*n Scalars) for the values, v. 260 The values, v, are then returned in a row-oriented format, 261 analogous to that used by default in MatSetValues(). 262 263 .keywords: matrix, get, values 264 265 .seealso: MatGetRow(), MatGetSubmatrix(), MatGetSubmatrices(), MatSetValues() 266 @*/ 267 int MatGetValues(Mat mat,int m,int *idxm,int n,int *idxn,Scalar *v) 268 { 269 int ierr; 270 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 271 PLogEventBegin(MAT_GetValues,mat,0,0,0); 272 ierr = (*mat->ops.getvalues)(mat,m,idxm,n,idxn,v); CHKERRQ(ierr); 273 PLogEventEnd(MAT_GetValues,mat,0,0,0); 274 return 0; 275 } 276 277 /* --------------------------------------------------------*/ 278 /*@ 279 MatMult - Computes matrix-vector product. 280 281 Input Parameters: 282 . mat - the matrix 283 . x - the vector to be multilplied 284 285 Output Parameters: 286 . y - the result 287 288 .keywords: matrix, multiply, matrix-vector product 289 290 .seealso: MatMultTrans(), MatMultAdd(), MatMultTransAdd() 291 @*/ 292 int MatMult(Mat mat,Vec x,Vec y) 293 { 294 int ierr; 295 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 296 PETSCVALIDHEADERSPECIFIC(x,VEC_COOKIE);PETSCVALIDHEADERSPECIFIC(y,VEC_COOKIE); 297 PLogEventBegin(MAT_Mult,mat,x,y,0); 298 ierr = (*mat->ops.mult)(mat,x,y); CHKERRQ(ierr); 299 PLogEventEnd(MAT_Mult,mat,x,y,0); 300 return 0; 301 } 302 /*@ 303 MatMultTrans - Computes matrix transpose times a vector. 304 305 Input Parameters: 306 . mat - the matrix 307 . x - the vector to be multilplied 308 309 Output Parameters: 310 . y - the result 311 312 .keywords: matrix, multiply, matrix-vector product, transpose 313 314 .seealso: MatMult(), MatMultAdd(), MatMultTransAdd() 315 @*/ 316 int MatMultTrans(Mat mat,Vec x,Vec y) 317 { 318 int ierr; 319 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 320 PETSCVALIDHEADERSPECIFIC(x,VEC_COOKIE); PETSCVALIDHEADERSPECIFIC(y,VEC_COOKIE); 321 PLogEventBegin(MAT_MultTrans,mat,x,y,0); 322 ierr = (*mat->ops.multtrans)(mat,x,y); CHKERRQ(ierr); 323 PLogEventEnd(MAT_MultTrans,mat,x,y,0); 324 return 0; 325 } 326 /*@ 327 MatMultAdd - Computes v3 = v2 + A * v1. 328 329 Input Parameters: 330 . mat - the matrix 331 . v1, v2 - the vectors 332 333 Output Parameters: 334 . v3 - the result 335 336 .keywords: matrix, multiply, matrix-vector product, add 337 338 .seealso: MatMultTrans(), MatMult(), MatMultTransAdd() 339 @*/ 340 int MatMultAdd(Mat mat,Vec v1,Vec v2,Vec v3) 341 { 342 int ierr; 343 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE);PETSCVALIDHEADERSPECIFIC(v1,VEC_COOKIE); 344 PETSCVALIDHEADERSPECIFIC(v2,VEC_COOKIE); PETSCVALIDHEADERSPECIFIC(v3,VEC_COOKIE); 345 PLogEventBegin(MAT_MultAdd,mat,v1,v2,v3); 346 ierr = (*mat->ops.multadd)(mat,v1,v2,v3); CHKERRQ(ierr); 347 PLogEventEnd(MAT_MultAdd,mat,v1,v2,v3); 348 return 0; 349 } 350 /*@ 351 MatMultTransAdd - Computes v3 = v2 + A' * v1. 352 353 Input Parameters: 354 . mat - the matrix 355 . v1, v2 - the vectors 356 357 Output Parameters: 358 . v3 - the result 359 360 .keywords: matrix, multiply, matrix-vector product, transpose, add 361 362 .seealso: MatMultTrans(), MatMultAdd(), MatMult() 363 @*/ 364 int MatMultTransAdd(Mat mat,Vec v1,Vec v2,Vec v3) 365 { 366 int ierr; 367 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); PETSCVALIDHEADERSPECIFIC(v1,VEC_COOKIE); 368 PETSCVALIDHEADERSPECIFIC(v2,VEC_COOKIE); PETSCVALIDHEADERSPECIFIC(v3,VEC_COOKIE); 369 if (!mat->ops.multtransadd) SETERRQ(PETSC_ERR_SUP,"MatMultTransAdd"); 370 PLogEventBegin(MAT_MultTransAdd,mat,v1,v2,v3); 371 ierr = (*mat->ops.multtransadd)(mat,v1,v2,v3); CHKERRQ(ierr); 372 PLogEventEnd(MAT_MultTransAdd,mat,v1,v2,v3); 373 return 0; 374 } 375 /* ------------------------------------------------------------*/ 376 /*@ 377 MatGetInfo - Returns information about matrix storage (number of 378 nonzeros, memory). 379 380 Input Parameters: 381 . mat - the matrix 382 383 Output Parameters: 384 . flag - flag indicating the type of parameters to be returned 385 $ flag = MAT_LOCAL: local matrix 386 $ flag = MAT_GLOBAL_MAX: maximum over all processors 387 $ flag = MAT_GLOBAL_SUM: sum over all processors 388 . nz - the number of nonzeros 389 . nzalloc - the number of allocated nonzeros 390 . mem - the memory used (in bytes) 391 392 .keywords: matrix, get, info, storage, nonzeros, memory 393 @*/ 394 int MatGetInfo(Mat mat,MatInfoType flag,int *nz,int *nzalloc,int *mem) 395 { 396 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 397 if (!mat->ops.getinfo) SETERRQ(PETSC_ERR_SUP,"MatGetInfo"); 398 return (*mat->ops.getinfo)(mat,flag,nz,nzalloc,mem); 399 } 400 /* ----------------------------------------------------------*/ 401 /*@ 402 MatLUFactor - Performs in-place LU factorization of matrix. 403 404 Input Parameters: 405 . mat - the matrix 406 . row - row permutation 407 . col - column permutation 408 . f - expected fill as ratio of original fill. 409 410 .keywords: matrix, factor, LU, in-place 411 412 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor() 413 @*/ 414 int MatLUFactor(Mat mat,IS row,IS col,double f) 415 { 416 int ierr; 417 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 418 if (!mat->ops.lufactor) SETERRQ(PETSC_ERR_SUP,"MatLUFactor"); 419 PLogEventBegin(MAT_LUFactor,mat,row,col,0); 420 ierr = (*mat->ops.lufactor)(mat,row,col,f); CHKERRQ(ierr); 421 PLogEventEnd(MAT_LUFactor,mat,row,col,0); 422 return 0; 423 } 424 /*@ 425 MatILUFactor - Performs in-place ILU factorization of matrix. 426 427 Input Parameters: 428 . mat - the matrix 429 . row - row permutation 430 . col - column permutation 431 . f - expected fill as ratio of original fill. 432 . level - number of levels of fill. 433 434 Note: probably really only in-place when level is zero. 435 .keywords: matrix, factor, ILU, in-place 436 437 .seealso: MatILUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor() 438 @*/ 439 int MatILUFactor(Mat mat,IS row,IS col,double f,int level) 440 { 441 int ierr; 442 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 443 if (!mat->ops.ilufactor) SETERRQ(PETSC_ERR_SUP,"MatILUFactor"); 444 PLogEventBegin(MAT_ILUFactor,mat,row,col,0); 445 ierr = (*mat->ops.ilufactor)(mat,row,col,f,level); CHKERRQ(ierr); 446 PLogEventEnd(MAT_ILUFactor,mat,row,col,0); 447 return 0; 448 } 449 450 /*@ 451 MatLUFactorSymbolic - Performs symbolic LU factorization of matrix. 452 Call this routine before calling MatLUFactorNumeric(). 453 454 Input Parameters: 455 . mat - the matrix 456 . row, col - row and column permutations 457 . f - expected fill as ratio of the original number of nonzeros, 458 for example 3.0; choosing this parameter well can result in 459 more efficient use of time and space. 460 461 Output Parameters: 462 . fact - new matrix that has been symbolically factored 463 464 .keywords: matrix, factor, LU, symbol CHKERRQ(ierr);ic 465 466 .seealso: MatLUFactor(), MatLUFactorNumeric(), MatCholeskyFactor() 467 @*/ 468 int MatLUFactorSymbolic(Mat mat,IS row,IS col,double f,Mat *fact) 469 { 470 int ierr; 471 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 472 if (!fact) SETERRQ(1,"MatLUFactorSymbolic:Missing factor matrix argument"); 473 if (!mat->ops.lufactorsymbolic) SETERRQ(PETSC_ERR_SUP,"MatLUFactorSymbolic"); 474 OptionsGetDouble(PETSC_NULL,"-mat_lu_fill",&f); 475 PLogEventBegin(MAT_LUFactorSymbolic,mat,row,col,0); 476 ierr = (*mat->ops.lufactorsymbolic)(mat,row,col,f,fact); CHKERRQ(ierr); 477 PLogEventEnd(MAT_LUFactorSymbolic,mat,row,col,0); 478 return 0; 479 } 480 /*@ 481 MatLUFactorNumeric - Performs numeric LU factorization of a matrix. 482 Call this routine after first calling MatLUFactorSymbolic(). 483 484 Input Parameters: 485 . mat - the matrix 486 . row, col - row and column permutations 487 488 Output Parameters: 489 . fact - symbolically factored matrix that must have been generated 490 by MatLUFactorSymbolic() 491 492 Notes: 493 See MatLUFactor() for in-place factorization. See 494 MatCholeskyFactorNumeric() for the symmetric, positive definite case. 495 496 .keywords: matrix, factor, LU, numeric 497 498 .seealso: MatLUFactorSymbolic(), MatLUFactor(), MatCholeskyFactor() 499 @*/ 500 int MatLUFactorNumeric(Mat mat,Mat *fact) 501 { 502 int ierr; 503 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 504 if (!fact) SETERRQ(1,"MatLUFactorNumeric:Missing factor matrix argument"); 505 if (!mat->ops.lufactornumeric) SETERRQ(PETSC_ERR_SUP,"MatLUFactorNumeric"); 506 PLogEventBegin(MAT_LUFactorNumeric,mat,*fact,0,0); 507 ierr = (*mat->ops.lufactornumeric)(mat,fact); CHKERRQ(ierr); 508 PLogEventEnd(MAT_LUFactorNumeric,mat,*fact,0,0); 509 if (OptionsHasName(PETSC_NULL,"-mat_view_draw")) { 510 Draw win; 511 ierr = DrawOpenX((*fact)->comm,0,0,0,0,300,300,&win); CHKERRQ(ierr); 512 ierr = MatView(*fact,(Viewer)win); CHKERRQ(ierr); 513 ierr = DrawSyncFlush(win); CHKERRQ(ierr); 514 ierr = DrawDestroy(win); CHKERRQ(ierr); 515 } 516 return 0; 517 } 518 /*@ 519 MatCholeskyFactor - Performs in-place Cholesky factorization of a 520 symmetric matrix. 521 522 Input Parameters: 523 . mat - the matrix 524 . perm - row and column permutations 525 . f - expected fill as ratio of original fill 526 527 Notes: 528 See MatLUFactor() for the nonsymmetric case. See also 529 MatCholeskyFactorSymbolic(), and MatCholeskyFactorNumeric(). 530 531 .keywords: matrix, factor, in-place, Cholesky 532 533 .seealso: MatLUFactor(), MatCholeskyFactorSymbolic() 534 .seealso: MatCholeskyFactorNumeric() 535 @*/ 536 int MatCholeskyFactor(Mat mat,IS perm,double f) 537 { 538 int ierr; 539 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 540 if (!mat->ops.choleskyfactor) SETERRQ(PETSC_ERR_SUP,"MatCholeskyFactor"); 541 PLogEventBegin(MAT_CholeskyFactor,mat,perm,0,0); 542 ierr = (*mat->ops.choleskyfactor)(mat,perm,f); CHKERRQ(ierr); 543 PLogEventEnd(MAT_CholeskyFactor,mat,perm,0,0); 544 return 0; 545 } 546 /*@ 547 MatCholeskyFactorSymbolic - Performs symbolic Cholesky factorization 548 of a symmetric matrix. 549 550 Input Parameters: 551 . mat - the matrix 552 . perm - row and column permutations 553 . f - expected fill as ratio of original 554 555 Output Parameter: 556 . fact - the factored matrix 557 558 Notes: 559 See MatLUFactorSymbolic() for the nonsymmetric case. See also 560 MatCholeskyFactor() and MatCholeskyFactorNumeric(). 561 562 .keywords: matrix, factor, factorization, symbolic, Cholesky 563 564 .seealso: MatLUFactorSymbolic() 565 .seealso: MatCholeskyFactor(), MatCholeskyFactorNumeric() 566 @*/ 567 int MatCholeskyFactorSymbolic(Mat mat,IS perm,double f,Mat *fact) 568 { 569 int ierr; 570 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 571 if (!fact) 572 SETERRQ(1,"MatCholeskyFactorSymbolic:Missing factor matrix argument"); 573 if (!mat->ops.choleskyfactorsymbolic)SETERRQ(PETSC_ERR_SUP,"MatCholeskyFactorSymbolic"); 574 PLogEventBegin(MAT_CholeskyFactorSymbolic,mat,perm,0,0); 575 ierr = (*mat->ops.choleskyfactorsymbolic)(mat,perm,f,fact); CHKERRQ(ierr); 576 PLogEventEnd(MAT_CholeskyFactorSymbolic,mat,perm,0,0); 577 return 0; 578 } 579 /*@ 580 MatCholeskyFactorNumeric - Performs numeric Cholesky factorization 581 of a symmetric matrix. Call this routine after first calling 582 MatCholeskyFactorSymbolic(). 583 584 Input Parameter: 585 . mat - the initial matrix 586 587 Output Parameter: 588 . fact - the factored matrix 589 590 .keywords: matrix, factor, numeric, Cholesky 591 592 .seealso: MatCholeskyFactorSymbolic(), MatCholeskyFactor() 593 .seealso: MatLUFactorNumeric() 594 @*/ 595 int MatCholeskyFactorNumeric(Mat mat,Mat *fact) 596 { 597 int ierr; 598 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 599 if (!fact) 600 SETERRQ(1,"MatCholeskyFactorNumeric:Missing factor matrix argument"); 601 if (!mat->ops.choleskyfactornumeric) SETERRQ(PETSC_ERR_SUP,"MatCholeskyFactorNumeric"); 602 PLogEventBegin(MAT_CholeskyFactorNumeric,mat,*fact,0,0); 603 ierr = (*mat->ops.choleskyfactornumeric)(mat,fact); CHKERRQ(ierr); 604 PLogEventEnd(MAT_CholeskyFactorNumeric,mat,*fact,0,0); 605 return 0; 606 } 607 /* ----------------------------------------------------------------*/ 608 /*@ 609 MatSolve - Solves A x = b, given a factored matrix. 610 611 Input Parameters: 612 . mat - the factored matrix 613 . b - the right-hand-side vector 614 615 Output Parameter: 616 . x - the result vector 617 618 .keywords: matrix, linear system, solve, LU, Cholesky, triangular solve 619 620 .seealso: MatSolveAdd(), MatSolveTrans(), MatSolveTransAdd() 621 @*/ 622 int MatSolve(Mat mat,Vec b,Vec x) 623 { 624 int ierr; 625 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 626 PETSCVALIDHEADERSPECIFIC(b,VEC_COOKIE); PETSCVALIDHEADERSPECIFIC(x,VEC_COOKIE); 627 if (!mat->factor) SETERRQ(1,"MatSolve:Unfactored matrix"); 628 if (!mat->ops.solve) SETERRQ(PETSC_ERR_SUP,"MatSolve"); 629 PLogEventBegin(MAT_Solve,mat,b,x,0); 630 ierr = (*mat->ops.solve)(mat,b,x); CHKERRQ(ierr); 631 PLogEventEnd(MAT_Solve,mat,b,x,0); 632 return 0; 633 } 634 635 /* @ 636 MatForwardSolve - Solves L x = b, given a factored matrix, A = LU. 637 638 Input Parameters: 639 . mat - the factored matrix 640 . b - the right-hand-side vector 641 642 Output Parameter: 643 . x - the result vector 644 645 Notes: 646 MatSolve() should be used for most applications, as it performs 647 a forward solve followed by a backward solve. 648 649 .keywords: matrix, forward, LU, Cholesky, triangular solve 650 651 .seealso: MatSolve(), MatBackwardSolve() 652 @ */ 653 int MatForwardSolve(Mat mat,Vec b,Vec x) 654 { 655 int ierr; 656 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 657 PETSCVALIDHEADERSPECIFIC(b,VEC_COOKIE); PETSCVALIDHEADERSPECIFIC(x,VEC_COOKIE); 658 if (!mat->factor) SETERRQ(1,"MatForwardSolve:Unfactored matrix"); 659 if (!mat->ops.forwardsolve) SETERRQ(PETSC_ERR_SUP,"MatForwardSolve"); 660 PLogEventBegin(MAT_ForwardSolve,mat,b,x,0); 661 ierr = (*mat->ops.forwardsolve)(mat,b,x); CHKERRQ(ierr); 662 PLogEventEnd(MAT_ForwardSolve,mat,b,x,0); 663 return 0; 664 } 665 666 /* @ 667 MatBackwardSolve - Solves U x = b, given a factored matrix, A = LU. 668 669 Input Parameters: 670 . mat - the factored matrix 671 . b - the right-hand-side vector 672 673 Output Parameter: 674 . x - the result vector 675 676 Notes: 677 MatSolve() should be used for most applications, as it performs 678 a forward solve followed by a backward solve. 679 680 .keywords: matrix, backward, LU, Cholesky, triangular solve 681 682 .seealso: MatSolve(), MatForwardSolve() 683 @ */ 684 int MatBackwardSolve(Mat mat,Vec b,Vec x) 685 { 686 int ierr; 687 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 688 PETSCVALIDHEADERSPECIFIC(b,VEC_COOKIE); PETSCVALIDHEADERSPECIFIC(x,VEC_COOKIE); 689 if (!mat->factor) SETERRQ(1,"MatBackwardSolve:Unfactored matrix"); 690 if (!mat->ops.backwardsolve) SETERRQ(PETSC_ERR_SUP,"MatBackwardSolve"); 691 PLogEventBegin(MAT_BackwardSolve,mat,b,x,0); 692 ierr = (*mat->ops.backwardsolve)(mat,b,x); CHKERRQ(ierr); 693 PLogEventEnd(MAT_BackwardSolve,mat,b,x,0); 694 return 0; 695 } 696 697 /*@ 698 MatSolveAdd - Computes x = y + inv(A)*b, given a factored matrix. 699 700 Input Parameters: 701 . mat - the factored matrix 702 . b - the right-hand-side vector 703 . y - the vector to be added to 704 705 Output Parameter: 706 . x - the result vector 707 708 .keywords: matrix, linear system, solve, LU, Cholesky, add 709 710 .seealso: MatSolve(), MatSolveTrans(), MatSolveTransAdd() 711 @*/ 712 int MatSolveAdd(Mat mat,Vec b,Vec y,Vec x) 713 { 714 Scalar one = 1.0; 715 Vec tmp; 716 int ierr; 717 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE);PETSCVALIDHEADERSPECIFIC(y,VEC_COOKIE); 718 PETSCVALIDHEADERSPECIFIC(b,VEC_COOKIE); PETSCVALIDHEADERSPECIFIC(x,VEC_COOKIE); 719 if (!mat->factor) SETERRQ(1,"MatSolveAdd:Unfactored matrix"); 720 PLogEventBegin(MAT_SolveAdd,mat,b,x,y); 721 if (mat->ops.solveadd) { 722 ierr = (*mat->ops.solveadd)(mat,b,y,x); CHKERRQ(ierr); 723 } 724 else { 725 /* do the solve then the add manually */ 726 if (x != y) { 727 ierr = MatSolve(mat,b,x); CHKERRQ(ierr); 728 ierr = VecAXPY(&one,y,x); CHKERRQ(ierr); 729 } 730 else { 731 ierr = VecDuplicate(x,&tmp); CHKERRQ(ierr); 732 PLogObjectParent(mat,tmp); 733 ierr = VecCopy(x,tmp); CHKERRQ(ierr); 734 ierr = MatSolve(mat,b,x); CHKERRQ(ierr); 735 ierr = VecAXPY(&one,tmp,x); CHKERRQ(ierr); 736 ierr = VecDestroy(tmp); CHKERRQ(ierr); 737 } 738 } 739 PLogEventEnd(MAT_SolveAdd,mat,b,x,y); 740 return 0; 741 } 742 /*@ 743 MatSolveTrans - Solves A' x = b, given a factored matrix. 744 745 Input Parameters: 746 . mat - the factored matrix 747 . b - the right-hand-side vector 748 749 Output Parameter: 750 . x - the result vector 751 752 .keywords: matrix, linear system, solve, LU, Cholesky, transpose 753 754 .seealso: MatSolve(), MatSolveAdd(), MatSolveTransAdd() 755 @*/ 756 int MatSolveTrans(Mat mat,Vec b,Vec x) 757 { 758 int ierr; 759 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 760 PETSCVALIDHEADERSPECIFIC(b,VEC_COOKIE); PETSCVALIDHEADERSPECIFIC(x,VEC_COOKIE); 761 if (!mat->factor) SETERRQ(1,"MatSolveTrans:Unfactored matrix"); 762 if (!mat->ops.solvetrans) SETERRQ(PETSC_ERR_SUP,"MatSolveTrans"); 763 PLogEventBegin(MAT_SolveTrans,mat,b,x,0); 764 ierr = (*mat->ops.solvetrans)(mat,b,x); CHKERRQ(ierr); 765 PLogEventEnd(MAT_SolveTrans,mat,b,x,0); 766 return 0; 767 } 768 /*@ 769 MatSolveTransAdd - Computes x = y + inv(trans(A)) b, given a 770 factored matrix. 771 772 Input Parameters: 773 . mat - the factored matrix 774 . b - the right-hand-side vector 775 . y - the vector to be added to 776 777 Output Parameter: 778 . x - the result vector 779 780 .keywords: matrix, linear system, solve, LU, Cholesky, transpose, add 781 782 .seealso: MatSolve(), MatSolveAdd(), MatSolveTrans() 783 @*/ 784 int MatSolveTransAdd(Mat mat,Vec b,Vec y,Vec x) 785 { 786 Scalar one = 1.0; 787 int ierr; 788 Vec tmp; 789 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE);PETSCVALIDHEADERSPECIFIC(y,VEC_COOKIE); 790 PETSCVALIDHEADERSPECIFIC(b,VEC_COOKIE); PETSCVALIDHEADERSPECIFIC(x,VEC_COOKIE); 791 if (!mat->factor) SETERRQ(1,"MatSolveTransAdd:Unfactored matrix"); 792 PLogEventBegin(MAT_SolveTransAdd,mat,b,x,y); 793 if (mat->ops.solvetransadd) { 794 ierr = (*mat->ops.solvetransadd)(mat,b,y,x); CHKERRQ(ierr); 795 } 796 else { 797 /* do the solve then the add manually */ 798 if (x != y) { 799 ierr = MatSolveTrans(mat,b,x); CHKERRQ(ierr); 800 ierr = VecAXPY(&one,y,x); CHKERRQ(ierr); 801 } 802 else { 803 ierr = VecDuplicate(x,&tmp); CHKERRQ(ierr); 804 PLogObjectParent(mat,tmp); 805 ierr = VecCopy(x,tmp); CHKERRQ(ierr); 806 ierr = MatSolveTrans(mat,b,x); CHKERRQ(ierr); 807 ierr = VecAXPY(&one,tmp,x); CHKERRQ(ierr); 808 ierr = VecDestroy(tmp); CHKERRQ(ierr); 809 } 810 } 811 PLogEventEnd(MAT_SolveTransAdd,mat,b,x,y); 812 return 0; 813 } 814 /* ----------------------------------------------------------------*/ 815 816 /*@ 817 MatRelax - Computes one relaxation sweep. 818 819 Input Parameters: 820 . mat - the matrix 821 . b - the right hand side 822 . omega - the relaxation factor 823 . flag - flag indicating the type of SOR, one of 824 $ SOR_FORWARD_SWEEP 825 $ SOR_BACKWARD_SWEEP 826 $ SOR_SYMMETRIC_SWEEP (SSOR method) 827 $ SOR_LOCAL_FORWARD_SWEEP 828 $ SOR_LOCAL_BACKWARD_SWEEP 829 $ SOR_LOCAL_SYMMETRIC_SWEEP (local SSOR) 830 $ SOR_APPLY_UPPER, SOR_APPLY_LOWER - applies 831 $ upper/lower triangular part of matrix to 832 $ vector (with omega) 833 $ SOR_ZERO_INITIAL_GUESS - zero initial guess 834 . shift - diagonal shift 835 . its - the number of iterations 836 837 Output Parameters: 838 . x - the solution (can contain an initial guess) 839 840 Notes: 841 SOR_LOCAL_FORWARD_SWEEP, SOR_LOCAL_BACKWARD_SWEEP, and 842 SOR_LOCAL_SYMMETRIC_SWEEP perform seperate independent smoothings 843 on each processor. 844 845 Application programmers will not generally use MatRelax() directly, 846 but instead will employ the SLES/PC interface. 847 848 Notes for Advanced Users: 849 The flags are implemented as bitwise inclusive or operations. 850 For example, use (SOR_ZERO_INITIAL_GUESS | SOR_SYMMETRIC_SWEEP) 851 to specify a zero initial guess for SSOR. 852 853 .keywords: matrix, relax, relaxation, sweep 854 @*/ 855 int MatRelax(Mat mat,Vec b,double omega,MatSORType flag,double shift, 856 int its,Vec x) 857 { 858 int ierr; 859 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 860 PETSCVALIDHEADERSPECIFIC(b,VEC_COOKIE); PETSCVALIDHEADERSPECIFIC(x,VEC_COOKIE); 861 if (!mat->ops.relax) SETERRQ(PETSC_ERR_SUP,"MatRelax"); 862 PLogEventBegin(MAT_Relax,mat,b,x,0); 863 ierr =(*mat->ops.relax)(mat,b,omega,flag,shift,its,x); CHKERRQ(ierr); 864 PLogEventEnd(MAT_Relax,mat,b,x,0); 865 return 0; 866 } 867 868 /* 869 Default matrix copy routine. 870 */ 871 int MatCopy_Basic(Mat A,Mat B) 872 { 873 int ierr,i,rstart,rend,nz,*cwork; 874 Scalar *vwork; 875 876 ierr = MatZeroEntries(B); CHKERRQ(ierr); 877 ierr = MatGetOwnershipRange(A,&rstart,&rend); CHKERRQ(ierr); 878 for (i=rstart; i<rend; i++) { 879 ierr = MatGetRow(A,i,&nz,&cwork,&vwork); CHKERRQ(ierr); 880 ierr = MatSetValues(B,1,&i,nz,cwork,vwork,INSERT_VALUES); CHKERRQ(ierr); 881 ierr = MatRestoreRow(A,i,&nz,&cwork,&vwork); CHKERRQ(ierr); 882 } 883 ierr = MatAssemblyBegin(B,FINAL_ASSEMBLY); CHKERRQ(ierr); 884 ierr = MatAssemblyEnd(B,FINAL_ASSEMBLY); CHKERRQ(ierr); 885 return 0; 886 } 887 888 /*@C 889 MatCopy - Copys a matrix to another matrix. 890 891 Input Parameters: 892 . A - the matrix 893 894 Output Parameter: 895 . B - where the copy is put 896 897 .keywords: matrix, copy, convert 898 @*/ 899 int MatCopy(Mat A,Mat B) 900 { 901 int ierr; 902 PETSCVALIDHEADERSPECIFIC(A,MAT_COOKIE); 903 PETSCVALIDHEADERSPECIFIC(B,MAT_COOKIE); 904 905 PLogEventBegin(MAT_Copy,A,B,0,0); 906 if (A->ops.copy) { 907 ierr = (*A->ops.copy)(A,B); CHKERRQ(ierr); 908 } 909 else { /* generic conversion */ 910 ierr = MatCopy_Basic(A,B); CHKERRQ(ierr); 911 } 912 PLogEventEnd(MAT_Copy,A,B,0,0); 913 return 0; 914 } 915 916 /*@C 917 MatConvert - Converts a matrix to another matrix, either of the same 918 or different type. 919 920 Input Parameters: 921 . mat - the matrix 922 . newtype - new matrix type. Use MATSAME to create a new matrix of the 923 same type as the original matrix. 924 925 Output Parameter: 926 . M - pointer to place new matrix 927 928 .keywords: matrix, copy, convert 929 @*/ 930 int MatConvert(Mat mat,MatType newtype,Mat *M) 931 { 932 int ierr; 933 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 934 if (!M) SETERRQ(1,"MatConvert:Bad new matrix address"); 935 PLogEventBegin(MAT_Convert,mat,0,0,0); 936 if (newtype == mat->type || newtype == MATSAME) { 937 if (mat->ops.convertsametype) { /* customized copy */ 938 ierr = (*mat->ops.convertsametype)(mat,M,COPY_VALUES); CHKERRQ(ierr); 939 } 940 } 941 else if (mat->ops.convert) { /* customized conversion */ 942 ierr = (*mat->ops.convert)(mat,newtype,M); CHKERRQ(ierr); 943 } 944 else { /* generic conversion */ 945 ierr = MatConvert_Basic(mat,newtype,M); CHKERRQ(ierr); 946 } 947 PLogEventEnd(MAT_Convert,mat,0,0,0); 948 return 0; 949 } 950 951 /*@ 952 MatGetDiagonal - Gets the diagonal of a matrix. 953 954 Input Parameters: 955 . mat - the matrix 956 957 Output Parameters: 958 . v - the vector for storing the diagonal 959 960 .keywords: matrix, get, diagonal 961 @*/ 962 int MatGetDiagonal(Mat mat,Vec v) 963 { 964 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 965 PETSCVALIDHEADERSPECIFIC(v,VEC_COOKIE); 966 if (mat->ops.getdiagonal) return (*mat->ops.getdiagonal)(mat,v); 967 SETERRQ(PETSC_ERR_SUP,"MatGetDiagonal"); 968 } 969 970 /*@C 971 MatTranspose - Computes an in-place or out-of-place transpose of a matrix. 972 973 Input Parameters: 974 . mat - the matrix to transpose 975 976 Output Parameters: 977 . B - the transpose - pass in zero for an in-place transpose 978 979 .keywords: matrix, transpose 980 @*/ 981 int MatTranspose(Mat mat,Mat *B) 982 { 983 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 984 if (mat->ops.transpose) return (*mat->ops.transpose)(mat,B); 985 SETERRQ(PETSC_ERR_SUP,"MatTranspose"); 986 } 987 988 /*@ 989 MatEqual - Compares two matrices. Returns 1 if two matrices are equal. 990 991 Input Parameters: 992 . mat1 - the first matrix 993 . mat2 - the second matrix 994 995 Returns: 996 Returns 1 if the matrices are equal; returns 0 otherwise. 997 998 .keywords: matrix, equal, equivalent 999 @*/ 1000 int MatEqual(Mat mat1,Mat mat2) 1001 { 1002 PETSCVALIDHEADERSPECIFIC(mat1,MAT_COOKIE); PETSCVALIDHEADERSPECIFIC(mat2,MAT_COOKIE); 1003 if (mat1->ops.equal) return (*mat1->ops.equal)(mat1,mat2); 1004 SETERRQ(PETSC_ERR_SUP,"MatEqual"); 1005 } 1006 1007 /*@ 1008 MatScale - Scales a matrix on the left and right by diagonal 1009 matrices that are stored as vectors. Either of the two scaling 1010 matrices can be null. 1011 1012 Input Parameters: 1013 . mat - the matrix to be scaled 1014 . l - the left scaling vector 1015 . r - the right scaling vector 1016 1017 .keywords: matrix, scale 1018 @*/ 1019 int MatScale(Mat mat,Vec l,Vec r) 1020 { 1021 int ierr; 1022 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 1023 if (!mat->ops.scale) SETERRQ(PETSC_ERR_SUP,"MatScale"); 1024 if (l) PETSCVALIDHEADERSPECIFIC(l,VEC_COOKIE); 1025 if (r) PETSCVALIDHEADERSPECIFIC(r,VEC_COOKIE); 1026 PLogEventBegin(MAT_Scale,mat,0,0,0); 1027 ierr = (*mat->ops.scale)(mat,l,r); CHKERRQ(ierr); 1028 PLogEventEnd(MAT_Scale,mat,0,0,0); 1029 return 0; 1030 } 1031 1032 /*@ 1033 MatNorm - Calculates various norms of a matrix. 1034 1035 Input Parameters: 1036 . mat - the matrix 1037 . type - the type of norm, NORM_1, NORM_2, NORM_FROBENIUS, NORM_INFINITY 1038 1039 Output Parameters: 1040 . norm - the resulting norm 1041 1042 .keywords: matrix, norm, Frobenius 1043 @*/ 1044 int MatNorm(Mat mat,NormType type,double *norm) 1045 { 1046 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 1047 if (!norm) SETERRQ(1,"MatNorm:bad addess for value"); 1048 if (mat->ops.norm) return (*mat->ops.norm)(mat,type,norm); 1049 SETERRQ(PETSC_ERR_SUP,"MatNorm:Not for this matrix type"); 1050 } 1051 1052 /*@ 1053 MatAssemblyBegin - Begins assembling the matrix. This routine should 1054 be called after completing all calls to MatSetValues(). 1055 1056 Input Parameters: 1057 . mat - the matrix 1058 . type - type of assembly, either FLUSH_ASSEMBLY or FINAL_ASSEMBLY 1059 1060 Notes: 1061 MatSetValues() generally caches the values. The matrix is ready to 1062 use only after MatAssemblyBegin() and MatAssemblyEnd() have been called. 1063 Use FLUSH_ASSEMBLY when switching between ADD_VALUES and SetValues; use 1064 FINAL_ASSEMBLY for the final assembly before the matrix is used. 1065 1066 .keywords: matrix, assembly, assemble, begin 1067 1068 .seealso: MatAssemblyEnd(), MatSetValues() 1069 @*/ 1070 int MatAssemblyBegin(Mat mat,MatAssemblyType type) 1071 { 1072 int ierr; 1073 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 1074 PLogEventBegin(MAT_AssemblyBegin,mat,0,0,0); 1075 if (mat->ops.assemblybegin) {ierr = (*mat->ops.assemblybegin)(mat,type); CHKERRQ(ierr);} 1076 PLogEventEnd(MAT_AssemblyBegin,mat,0,0,0); 1077 return 0; 1078 } 1079 1080 /*@ 1081 MatAssemblyEnd - Completes assembling the matrix. This routine should 1082 be called after all calls to MatSetValues() and after MatAssemblyBegin(). 1083 1084 Input Parameters: 1085 . mat - the matrix 1086 . type - type of assembly, either FLUSH_ASSEMBLY or FINAL_ASSEMBLY 1087 1088 Options Database Keys: 1089 $ -mat_view_draw : Draw nonzero structure of matrix at conclusion of MatEndAssembly(), 1090 using MatView() and DrawOpenX(). 1091 $ -mat_view_info : Prints info on matrix. 1092 $ -mat_view_info_detailed: More detailed information. 1093 $ -mat_view_ascii : Prints matrix out in ascii. 1094 $ -display <name> : Set display name (default is host) 1095 $ -pause <sec> : Set number of seconds to pause after display 1096 1097 Note: 1098 MatSetValues() generally caches the values. The matrix is ready to 1099 use only after MatAssemblyBegin() and MatAssemblyEnd() have been called. 1100 Use FLUSH_ASSEMBLY when switching between ADD_VALUES and SetValues; use 1101 FINAL_ASSEMBLY for the final assembly before the matrix is used. 1102 1103 .keywords: matrix, assembly, assemble, end 1104 1105 .seealso: MatAssemblyBegin(), MatSetValues() 1106 @*/ 1107 int MatAssemblyEnd(Mat mat,MatAssemblyType type) 1108 { 1109 int ierr; 1110 static int inassm = 0; 1111 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 1112 inassm++; 1113 PLogEventBegin(MAT_AssemblyEnd,mat,0,0,0); 1114 if (mat->ops.assemblyend) {ierr = (*mat->ops.assemblyend)(mat,type); CHKERRQ(ierr);} 1115 PLogEventEnd(MAT_AssemblyEnd,mat,0,0,0); 1116 if (inassm == 1) { 1117 if (OptionsHasName(PETSC_NULL,"-mat_view_info")) { 1118 Viewer viewer; 1119 ierr = ViewerFileOpenASCII(mat->comm,"stdout",&viewer);CHKERRQ(ierr); 1120 ierr = ViewerFileSetFormat(viewer,FILE_FORMAT_INFO,0);CHKERRQ(ierr); 1121 ierr = MatView(mat,viewer); CHKERRQ(ierr); 1122 ierr = ViewerDestroy(viewer); CHKERRQ(ierr); 1123 } 1124 if (OptionsHasName(PETSC_NULL,"-mat_view_info_detailed")) { 1125 Viewer viewer; 1126 ierr = ViewerFileOpenASCII(mat->comm,"stdout",&viewer);CHKERRQ(ierr); 1127 ierr = ViewerFileSetFormat(viewer,FILE_FORMAT_INFO_DETAILED,0);CHKERRQ(ierr); 1128 ierr = MatView(mat,viewer); CHKERRQ(ierr); 1129 ierr = ViewerDestroy(viewer); CHKERRQ(ierr); 1130 } 1131 if (OptionsHasName(PETSC_NULL,"-mat_view_draw")) { 1132 Draw win; 1133 ierr = DrawOpenX(mat->comm,0,0,0,0,300,300,&win); CHKERRQ(ierr); 1134 ierr = MatView(mat,(Viewer)win); CHKERRQ(ierr); 1135 ierr = DrawSyncFlush(win); CHKERRQ(ierr); 1136 ierr = DrawDestroy(win); CHKERRQ(ierr); 1137 } 1138 } 1139 inassm--; 1140 return 0; 1141 } 1142 1143 /*@ 1144 MatCompress - Tries to store the matrix in as little space as 1145 possible. May fail if memory is already fully used, since it 1146 tries to allocate new space. 1147 1148 Input Parameters: 1149 . mat - the matrix 1150 1151 .keywords: matrix, compress 1152 @*/ 1153 int MatCompress(Mat mat) 1154 { 1155 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 1156 if (mat->ops.compress) return (*mat->ops.compress)(mat); 1157 return 0; 1158 } 1159 /*@ 1160 MatSetOption - Sets a parameter option for a matrix. Some options 1161 may be specific to certain storage formats. Some options 1162 determine how values will be inserted (or added). Sorted, 1163 row-oriented input will generally assemble the fastest. The default 1164 is row-oriented, nonsorted input. 1165 1166 Input Parameters: 1167 . mat - the matrix 1168 . option - the option, one of the following: 1169 $ ROW_ORIENTED 1170 $ COLUMN_ORIENTED, 1171 $ ROWS_SORTED, 1172 $ COLUMNS_SORTED, 1173 $ NO_NEW_NONZERO_LOCATIONS, 1174 $ YES_NEW_NONZERO_LOCATIONS, 1175 $ SYMMETRIC_MATRIX, 1176 $ STRUCTURALLY_SYMMETRIC_MATRIX, 1177 $ NO_NEW_DIAGONALS, 1178 $ YES_NEW_DIAGONALS, 1179 $ and possibly others. 1180 1181 Notes: 1182 Some options are relevant only for particular matrix types and 1183 are thus ignored by others. Other options are not supported by 1184 certain matrix types and will generate an error message if set. 1185 1186 If using a Fortran 77 module to compute a matrix, one may need to 1187 use the column-oriented option (or convert to the row-oriented 1188 format). 1189 1190 NO_NEW_NONZERO_LOCATIONS indicates that any add or insertion 1191 that will generate a new entry in the nonzero structure is ignored. 1192 What this means is if memory is not allocated for this particular 1193 lot, then the insertion is ignored. For dense matrices, where 1194 the entire array is allocated, no entries are ever ignored. 1195 1196 .keywords: matrix, option, row-oriented, column-oriented, sorted, nonzero 1197 @*/ 1198 int MatSetOption(Mat mat,MatOption op) 1199 { 1200 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 1201 if (mat->ops.setoption) return (*mat->ops.setoption)(mat,op); 1202 return 0; 1203 } 1204 1205 /*@ 1206 MatZeroEntries - Zeros all entries of a matrix. For sparse matrices 1207 this routine retains the old nonzero structure. 1208 1209 Input Parameters: 1210 . mat - the matrix 1211 1212 .keywords: matrix, zero, entries 1213 1214 .seealso: MatZeroRows() 1215 @*/ 1216 int MatZeroEntries(Mat mat) 1217 { 1218 int ierr; 1219 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 1220 if (!mat->ops.zeroentries) SETERRQ(PETSC_ERR_SUP,"MatZeroEntries"); 1221 PLogEventBegin(MAT_ZeroEntries,mat,0,0,0); 1222 ierr = (*mat->ops.zeroentries)(mat); CHKERRQ(ierr); 1223 PLogEventEnd(MAT_ZeroEntries,mat,0,0,0); 1224 return 0; 1225 } 1226 1227 /*@ 1228 MatZeroRows - Zeros all entries (except possibly the main diagonal) 1229 of a set of rows of a matrix. 1230 1231 Input Parameters: 1232 . mat - the matrix 1233 . is - index set of rows to remove 1234 . diag - pointer to value put in all diagonals of eliminated rows. 1235 Note that diag is not a pointer to an array, but merely a 1236 pointer to a single value. 1237 1238 Notes: 1239 For the AIJ and row matrix formats this removes the old nonzero 1240 structure, but does not release memory. For the dense and block 1241 diagonal formats this does not alter the nonzero structure. 1242 1243 The user can set a value in the diagonal entry (or for the AIJ and 1244 row formats can optionally remove the main diagonal entry from the 1245 nonzero structure as well, by passing a null pointer as the final 1246 argument). 1247 1248 .keywords: matrix, zero, rows, boundary conditions 1249 1250 .seealso: MatZeroEntries(), MatGetSubMatrix(), MatGetSubMatrixInPlace() 1251 @*/ 1252 int MatZeroRows(Mat mat,IS is, Scalar *diag) 1253 { 1254 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 1255 if (mat->ops.zerorows) return (*mat->ops.zerorows)(mat,is,diag); 1256 SETERRQ(PETSC_ERR_SUP,"MatZeroRows"); 1257 } 1258 1259 /*@ 1260 MatGetSize - Returns the numbers of rows and columns in a matrix. 1261 1262 Input Parameter: 1263 . mat - the matrix 1264 1265 Output Parameters: 1266 . m - the number of global rows 1267 . n - the number of global columns 1268 1269 .keywords: matrix, dimension, size, rows, columns, global, get 1270 1271 .seealso: MatGetLocalSize() 1272 @*/ 1273 int MatGetSize(Mat mat,int *m,int* n) 1274 { 1275 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 1276 if (!m || !n) SETERRQ(1,"MatGetSize:Bad address for result"); 1277 return (*mat->ops.getsize)(mat,m,n); 1278 } 1279 1280 /*@ 1281 MatGetLocalSize - Returns the number of rows and columns in a matrix 1282 stored locally. This information may be implementation dependent, so 1283 use with care. 1284 1285 Input Parameters: 1286 . mat - the matrix 1287 1288 Output Parameters: 1289 . m - the number of local rows 1290 . n - the number of local columns 1291 1292 .keywords: matrix, dimension, size, local, rows, columns, get 1293 1294 .seealso: MatGetSize() 1295 @*/ 1296 int MatGetLocalSize(Mat mat,int *m,int* n) 1297 { 1298 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 1299 if (!m || !n) SETERRQ(1,"MatGetLocalSize:Bad address for result"); 1300 return (*mat->ops.getlocalsize)(mat,m,n); 1301 } 1302 1303 /*@ 1304 MatGetOwnershipRange - Returns the range of matrix rows owned by 1305 this processor, assuming that the matrix is laid out with the first 1306 n1 rows on the first processor, the next n2 rows on the second, etc. 1307 For certain parallel layouts this range may not be well-defined. 1308 1309 Input Parameters: 1310 . mat - the matrix 1311 1312 Output Parameters: 1313 . m - the first local row 1314 . n - one more then the last local row 1315 1316 .keywords: matrix, get, range, ownership 1317 @*/ 1318 int MatGetOwnershipRange(Mat mat,int *m,int* n) 1319 { 1320 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 1321 if (!m || !n) SETERRQ(1,"MatGetOwnershipRange:Bad address for result"); 1322 if (mat->ops.getownershiprange) return (*mat->ops.getownershiprange)(mat,m,n); 1323 SETERRQ(PETSC_ERR_SUP,"MatGetOwnershipRange"); 1324 } 1325 1326 /*@ 1327 MatILUFactorSymbolic - Performs symbolic ILU factorization of a matrix. 1328 Uses levels of fill only, not drop tolerance. Use MatLUFactorNumeric() 1329 to complete the factorization. 1330 1331 Input Parameters: 1332 . mat - the matrix 1333 . row - row permutation 1334 . column - column permutation 1335 . fill - number of levels of fill 1336 . f - expected fill as ratio of original fill 1337 1338 Output Parameters: 1339 . fact - puts factor 1340 1341 .keywords: matrix, factor, incomplete, ILU, symbolic, fill 1342 1343 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric() 1344 @*/ 1345 int MatILUFactorSymbolic(Mat mat,IS row,IS col,double f,int fill,Mat *fact) 1346 { 1347 int ierr; 1348 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 1349 if (fill < 0) SETERRQ(1,"MatILUFactorSymbolic:Levels of fill negative"); 1350 if (!fact) SETERRQ(1,"MatILUFactorSymbolic:Fact argument is missing"); 1351 if (!mat->ops.ilufactorsymbolic) SETERRQ(PETSC_ERR_SUP,"MatILUFactorSymbolic"); 1352 OptionsGetDouble(PETSC_NULL,"-mat_ilu_fill",&f); 1353 PLogEventBegin(MAT_ILUFactorSymbolic,mat,row,col,0); 1354 ierr = (*mat->ops.ilufactorsymbolic)(mat,row,col,f,fill,fact); 1355 CHKERRQ(ierr); 1356 PLogEventEnd(MAT_ILUFactorSymbolic,mat,row,col,0); 1357 return 0; 1358 } 1359 1360 /*@ 1361 MatIncompleteCholeskyFactorSymbolic - Performs symbolic incomplete 1362 Cholesky factorization for a symmetric matrix. Use 1363 MatCholeskyFactorNumeric() to complete the factorization. 1364 1365 Input Parameters: 1366 . mat - the matrix 1367 . perm - row and column permutation 1368 . fill - levels of fill 1369 . f - expected fill as ratio of original fill 1370 1371 Output Parameter: 1372 . fact - the factored matrix 1373 1374 Note: Currently only no-fill factorization is supported. 1375 1376 .keywords: matrix, factor, incomplete, ICC, Cholesky, symbolic, fill 1377 1378 .seealso: MatCholeskyFactorNumeric(), MatCholeskyFactor() 1379 @*/ 1380 int MatIncompleteCholeskyFactorSymbolic(Mat mat,IS perm,double f,int fill, 1381 Mat *fact) 1382 { 1383 int ierr; 1384 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 1385 if (fill < 0) SETERRQ(1,"MatIncompleteCholeskyFactorSymbolic:Fill negative"); 1386 if (!fact) SETERRQ(1,"MatIncompleteCholeskyFactorSymbolic:Missing fact argument"); 1387 if (!mat->ops.incompletecholeskyfactorsymbolic) 1388 SETERRQ(PETSC_ERR_SUP,"MatIncompleteCholeskyFactorSymbolic"); 1389 PLogEventBegin(MAT_IncompleteCholeskyFactorSymbolic,mat,perm,0,0); 1390 ierr = (*mat->ops.incompletecholeskyfactorsymbolic)(mat,perm,f,fill,fact); 1391 CHKERRQ(ierr); 1392 PLogEventEnd(MAT_IncompleteCholeskyFactorSymbolic,mat,perm,0,0); 1393 return 0; 1394 } 1395 1396 /*@C 1397 MatGetArray - Returns a pointer to the element values in the matrix. 1398 This routine is implementation dependent, and may not even work for 1399 certain matrix types. 1400 1401 Input Parameter: 1402 . mat - the matrix 1403 1404 Output Parameter: 1405 . v - the location of the values 1406 1407 Fortran Note: 1408 The Fortran interface is slightly different from that given below. 1409 See the users manual and petsc/src/mat/examples for details. 1410 1411 .keywords: matrix, array, elements, values 1412 @*/ 1413 int MatGetArray(Mat mat,Scalar **v) 1414 { 1415 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 1416 if (!v) SETERRQ(1,"MatGetArray:Bad input, array pointer location"); 1417 if (!mat->ops.getarray) SETERRQ(PETSC_ERR_SUP,"MatGetArraye"); 1418 return (*mat->ops.getarray)(mat,v); 1419 } 1420 1421 /*@C 1422 MatGetSubMatrix - Extracts a submatrix from a matrix. If submat points 1423 to a valid matrix, it may be reused. 1424 1425 Input Parameters: 1426 . mat - the matrix 1427 . irow, icol - index sets of rows and columns to extract 1428 . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 1429 1430 Output Parameter: 1431 . submat - the submatrix 1432 1433 Notes: 1434 MatGetSubMatrix() can be useful in setting boundary conditions. 1435 1436 Use MatGetSubMatrices() to extract multiple submatrices. 1437 1438 .keywords: matrix, get, submatrix, boundary conditions 1439 1440 .seealso: MatZeroRows(), MatGetSubMatrixInPlace(), MatGetSubMatrices() 1441 @*/ 1442 int MatGetSubMatrix(Mat mat,IS irow,IS icol,MatGetSubMatrixCall scall,Mat *submat) 1443 { 1444 int ierr; 1445 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 1446 if (scall == MAT_REUSE_MATRIX) { 1447 PETSCVALIDHEADERSPECIFIC(*submat,MAT_COOKIE); 1448 } 1449 if (!mat->ops.getsubmatrix) SETERRQ(PETSC_ERR_SUP,"MatGetSubMatrix"); 1450 PLogEventBegin(MAT_GetSubMatrix,mat,irow,icol,0); 1451 ierr = (*mat->ops.getsubmatrix)(mat,irow,icol,scall,submat); CHKERRQ(ierr); 1452 PLogEventEnd(MAT_GetSubMatrix,mat,irow,icol,0); 1453 return 0; 1454 } 1455 1456 /*@C 1457 MatGetSubMatrices - Extracts several submatrices from a matrix. If submat 1458 points to an array of valid matrices, it may be reused. 1459 1460 Input Parameters: 1461 . mat - the matrix 1462 . irow, icol - index sets of rows and columns to extract 1463 1464 Output Parameter: 1465 . submat - the submatrices 1466 1467 Note: 1468 Use MatGetSubMatrix() for extracting a sinble submatrix. 1469 1470 .keywords: matrix, get, submatrix, submatrices 1471 1472 .seealso: MatGetSubMatrix() 1473 @*/ 1474 int MatGetSubMatrices(Mat mat,int n, IS *irow,IS *icol,MatGetSubMatrixCall scall, 1475 Mat **submat) 1476 { 1477 int ierr; 1478 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 1479 if (!mat->ops.getsubmatrices) SETERRQ(PETSC_ERR_SUP,"MatGetSubMatrices"); 1480 PLogEventBegin(MAT_GetSubMatrices,mat,0,0,0); 1481 ierr = (*mat->ops.getsubmatrices)(mat,n,irow,icol,scall,submat); CHKERRQ(ierr); 1482 PLogEventEnd(MAT_GetSubMatrices,mat,0,0,0); 1483 return 0; 1484 } 1485 1486 /*@ 1487 MatGetSubMatrixInPlace - Extracts a submatrix from a matrix, returning 1488 the submatrix in place of the original matrix. 1489 1490 Input Parameters: 1491 . mat - the matrix 1492 . irow, icol - index sets of rows and columns to extract 1493 1494 .keywords: matrix, get, submatrix, boundary conditions, in-place 1495 1496 .seealso: MatZeroRows(), MatGetSubMatrix() 1497 @*/ 1498 int MatGetSubMatrixInPlace(Mat mat,IS irow,IS icol) 1499 { 1500 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 1501 if (!mat->ops.getsubmatrixinplace) SETERRQ(PETSC_ERR_SUP,"MatGetSubmatrixInPlace"); 1502 return (*mat->ops.getsubmatrixinplace)(mat,irow,icol); 1503 } 1504 1505 /*@ 1506 MatIncreaseOverlap - Given a set of submatrices indicated by index sets, 1507 replaces the index by larger ones that represent submatrices with more 1508 overlap. 1509 1510 Input Parameters: 1511 . mat - the matrix 1512 . n - the number of index sets 1513 . is - the array of pointers to index sets 1514 . ov - the additional overlap requested 1515 1516 .keywords: matrix, overlap, Schwarz 1517 1518 .seealso: MatGetSubMatrices() 1519 @*/ 1520 int MatIncreaseOverlap(Mat mat,int n, IS *is, int ov) 1521 { 1522 int ierr; 1523 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 1524 if (!mat->ops.increaseoverlap) SETERRQ(PETSC_ERR_SUP,"MatIncreaseOverlap"); 1525 ierr = (*mat->ops.increaseoverlap)(mat,n,is,ov); CHKERRQ(ierr); 1526 return 0; 1527 } 1528 1529 /*@ 1530 MatPrintHelp - Prints all the options for the matrix. 1531 1532 Input Parameter: 1533 . mat - the matrix 1534 1535 Options Database Keys: 1536 $ -help, -h 1537 1538 .keywords: mat, help 1539 1540 .seealso: MatCreate(), MatCreateXXX() 1541 @*/ 1542 int MatPrintHelp(Mat mat) 1543 { 1544 static int called = 0; 1545 MPI_Comm comm = mat->comm; 1546 1547 if (!called) { 1548 MPIU_printf(comm,"General matrix options:\n"); 1549 MPIU_printf(comm," -mat_view_info : view basic matrix info during MatAssemblyEnd()\n"); 1550 MPIU_printf(comm," -mat_view_info_detailed : view detailed matrix info during MatAssemblyEnd()\n"); 1551 MPIU_printf(comm," -mat_view_draw : draw nonzero matrix structure during MatAssemblyEnd()\n"); 1552 MPIU_printf(comm," -draw_pause <sec> : set seconds of display pause\n"); 1553 MPIU_printf(comm," -display <name> : set alternate display\n"); 1554 called = 1; 1555 } 1556 if (mat->ops.printhelp) (*mat->ops.printhelp)(mat); 1557 return 0; 1558 } 1559