1 #ifndef lint 2 static char vcid[] = "$Id: matrix.c,v 1.183 1996/08/06 15:51:59 curfman Exp curfman $"; 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 "src/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,MatReordering type,IS *rperm,IS *cperm) 52 { 53 int ierr; 54 PetscValidHeaderSpecific(mat,MAT_COOKIE); 55 if (!mat->assembled) SETERRQ(1,"MatGetReordering:Not for unassembled matrix"); 56 57 if (!mat->ops.getreordering) {*rperm = 0; *cperm = 0; return 0;} 58 PLogEventBegin(MAT_GetReordering,mat,0,0,0); 59 ierr = MatGetReorderingTypeFromOptions(0,&type); CHKERRQ(ierr); 60 ierr = (*mat->ops.getreordering)(mat,type,rperm,cperm); CHKERRQ(ierr); 61 PLogEventEnd(MAT_GetReordering,mat,0,0,0); 62 return 0; 63 } 64 65 /*@C 66 MatGetRow - Gets a row of a matrix. You MUST call MatRestoreRow() 67 for each row that you get to ensure that your application does 68 not bleed memory. 69 70 Input Parameters: 71 . mat - the matrix 72 . row - the row to get 73 74 Output Parameters: 75 . ncols - the number of nonzeros in the row 76 . cols - if nonzero, the column numbers 77 . vals - if nonzero, the values 78 79 Notes: 80 This routine is provided for people who need to have direct access 81 to the structure of a matrix. We hope that we provide enough 82 high-level matrix routines that few users will need it. 83 84 For better efficiency, set cols and/or vals to PETSC_NULL if you do 85 not wish to extract these quantities. 86 87 The user can only examine the values extracted with MatGetRow(); 88 the values cannot be altered. To change the matrix entries, one 89 must use MatSetValues(). 90 91 Caution: 92 Do not try to change the contents of the output arrays (cols and vals). 93 In some cases, this may corrupt the matrix. 94 95 .keywords: matrix, row, get, extract 96 97 .seealso: MatRestoreRow(), MatSetValues() 98 @*/ 99 int MatGetRow(Mat mat,int row,int *ncols,int **cols,Scalar **vals) 100 { 101 int ierr; 102 PetscValidHeaderSpecific(mat,MAT_COOKIE); 103 PetscValidIntPointer(ncols); 104 if (!mat->assembled) SETERRQ(1,"MatGetRow:Not for unassembled matrix"); 105 PLogEventBegin(MAT_GetRow,mat,0,0,0); 106 ierr = (*mat->ops.getrow)(mat,row,ncols,cols,vals); CHKERRQ(ierr); 107 PLogEventEnd(MAT_GetRow,mat,0,0,0); 108 return 0; 109 } 110 111 /*@C 112 MatRestoreRow - Frees any temporary space allocated by MatGetRow(). 113 114 Input Parameters: 115 . mat - the matrix 116 . row - the row to get 117 . ncols, cols - the number of nonzeros and their columns 118 . vals - if nonzero the column values 119 120 .keywords: matrix, row, restore 121 122 .seealso: MatGetRow() 123 @*/ 124 int MatRestoreRow(Mat mat,int row,int *ncols,int **cols,Scalar **vals) 125 { 126 PetscValidHeaderSpecific(mat,MAT_COOKIE); 127 PetscValidIntPointer(ncols); 128 if (!mat->assembled) SETERRQ(1,"MatRestoreRow:Not for unassembled matrix"); 129 if (!mat->ops.restorerow) return 0; 130 return (*mat->ops.restorerow)(mat,row,ncols,cols,vals); 131 } 132 /*@ 133 MatView - Visualizes a matrix object. 134 135 Input Parameters: 136 . mat - the matrix 137 . ptr - visualization context 138 139 Notes: 140 The available visualization contexts include 141 $ VIEWER_STDOUT_SELF - standard output (default) 142 $ VIEWER_STDOUT_WORLD - synchronized standard 143 $ output where only the first processor opens 144 $ the file. All other processors send their 145 $ data to the first processor to print. 146 147 The user can open alternative vistualization contexts with 148 $ ViewerFileOpenASCII() - output to a specified file 149 $ ViewerFileOpenBinary() - output in binary to a 150 $ specified file; corresponding input uses MatLoad() 151 $ ViewerDrawOpenX() - output nonzero matrix structure to 152 $ an X window display 153 $ ViewerMatlabOpen() - output matrix to Matlab viewer. 154 $ Currently only the sequential dense and AIJ 155 $ matrix types support the Matlab viewer. 156 157 The user can call ViewerSetFormat() to specify the output 158 format of ASCII printed objects (when using VIEWER_STDOUT_SELF, 159 VIEWER_STDOUT_WORLD and ViewerFileOpenASCII). Available formats include 160 $ ASCII_FORMAT_DEFAULT - default, prints matrix contents 161 $ ASCII_FORMAT_MATLAB - Matlab format 162 $ ASCII_FORMAT_IMPL - implementation-specific format 163 $ (which is in many cases the same as the default) 164 $ ASCII_FORMAT_INFO - basic information about the matrix 165 $ size and structure (not the matrix entries) 166 $ ASCII_FORMAT_INFO_DETAILED - more detailed information about the 167 $ matrix structure 168 169 .keywords: matrix, view, visualize, output, print, write, draw 170 171 .seealso: ViewerSetFormat(), ViewerFileOpenASCII(), ViewerDrawOpenX(), 172 ViewerMatlabOpen(), ViewerFileOpenBinary(), MatLoad() 173 @*/ 174 int MatView(Mat mat,Viewer viewer) 175 { 176 int format, ierr, rows, cols,nz, nzalloc, mem; 177 FILE *fd; 178 char *cstr; 179 ViewerType vtype; 180 MPI_Comm comm = mat->comm; 181 182 PetscValidHeaderSpecific(mat,MAT_COOKIE); 183 if (!mat->assembled) SETERRQ(1,"MatView:Not for unassembled matrix"); 184 185 if (!viewer) { 186 viewer = VIEWER_STDOUT_SELF; 187 } 188 189 ierr = ViewerGetType(viewer,&vtype); 190 if (vtype == ASCII_FILE_VIEWER || vtype == ASCII_FILES_VIEWER) { 191 ierr = ViewerGetFormat(viewer,&format); CHKERRQ(ierr); 192 ierr = ViewerASCIIGetPointer(viewer,&fd); CHKERRQ(ierr); 193 if (format == ASCII_FORMAT_INFO || format == ASCII_FORMAT_INFO_DETAILED) { 194 PetscFPrintf(comm,fd,"Matrix Object:\n"); 195 ierr = MatGetType(mat,PETSC_NULL,&cstr); CHKERRQ(ierr); 196 ierr = MatGetSize(mat,&rows,&cols); CHKERRQ(ierr); 197 PetscFPrintf(comm,fd," type=%s, rows=%d, cols=%d\n",cstr,rows,cols); 198 if (mat->ops.getinfo) { 199 ierr = MatGetInfo(mat,MAT_GLOBAL_SUM,&nz,&nzalloc,&mem); CHKERRQ(ierr); 200 PetscFPrintf(comm,fd," total: nonzeros=%d, allocated nonzeros=%d\n",nz, 201 nzalloc); 202 } 203 } 204 } 205 if (mat->view) {ierr = (*mat->view)((PetscObject)mat,viewer); CHKERRQ(ierr);} 206 return 0; 207 } 208 209 /*@C 210 MatDestroy - Frees space taken by a matrix. 211 212 Input Parameter: 213 . mat - the matrix 214 215 .keywords: matrix, destroy 216 @*/ 217 int MatDestroy(Mat mat) 218 { 219 int ierr; 220 PetscValidHeaderSpecific(mat,MAT_COOKIE); 221 ierr = (*mat->destroy)((PetscObject)mat); CHKERRQ(ierr); 222 return 0; 223 } 224 /*@ 225 MatValid - Checks whether a matrix object is valid. 226 227 Input Parameter: 228 . m - the matrix to check 229 230 Output Parameter: 231 flg - flag indicating matrix status, either 232 $ PETSC_TRUE if matrix is valid; 233 $ PETSC_FALSE otherwise. 234 235 .keywords: matrix, valid 236 @*/ 237 int MatValid(Mat m,PetscTruth *flg) 238 { 239 PetscValidIntPointer(flg); 240 if (!m) *flg = PETSC_FALSE; 241 else if (m->cookie != MAT_COOKIE) *flg = PETSC_FALSE; 242 else *flg = PETSC_TRUE; 243 return 0; 244 } 245 246 /*@ 247 MatSetValues - Inserts or adds a block of values into a matrix. 248 These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd() 249 MUST be called after all calls to MatSetValues() have been completed. 250 251 Input Parameters: 252 . mat - the matrix 253 . v - a logically two-dimensional array of values 254 . m, indexm - the number of rows and their global indices 255 . n, indexn - the number of columns and their global indices 256 . addv - either ADD_VALUES or INSERT_VALUES, where 257 $ ADD_VALUES - adds values to any existing entries 258 $ INSERT_VALUES - replaces existing entries with new values 259 260 Notes: 261 By default the values, v, are row-oriented and unsorted. 262 See MatSetOptions() for other options. 263 264 Calls to MatSetValues() with the INSERT_VALUES and ADD_VALUES 265 options cannot be mixed without intervening calls to the assembly 266 routines. 267 268 .keywords: matrix, insert, add, set, values 269 270 .seealso: MatSetOptions(), MatAssemblyBegin(), MatAssemblyEnd() 271 @*/ 272 int MatSetValues(Mat mat,int m,int *idxm,int n,int *idxn,Scalar *v,InsertMode addv) 273 { 274 int ierr; 275 PetscValidHeaderSpecific(mat,MAT_COOKIE); 276 if (!m || !n) return 0; /* no values to insert */ 277 PetscValidIntPointer(idxm); 278 PetscValidIntPointer(idxn); 279 PetscValidScalarPointer(v); 280 281 if (mat->assembled) { 282 mat->was_assembled = PETSC_TRUE; 283 mat->assembled = PETSC_FALSE; 284 } 285 PLogEventBegin(MAT_SetValues,mat,0,0,0); 286 ierr = (*mat->ops.setvalues)(mat,m,idxm,n,idxn,v,addv);CHKERRQ(ierr); 287 PLogEventEnd(MAT_SetValues,mat,0,0,0); 288 return 0; 289 } 290 291 /*@ 292 MatGetValues - Gets a block of values from a matrix. 293 294 Input Parameters: 295 . mat - the matrix 296 . v - a logically two-dimensional array for storing the values 297 . m, indexm - the number of rows and their global indices 298 . n, indexn - the number of columns and their global indices 299 300 Notes: 301 The user must allocate space (m*n Scalars) for the values, v. 302 The values, v, are then returned in a row-oriented format, 303 analogous to that used by default in MatSetValues(). 304 305 .keywords: matrix, get, values 306 307 .seealso: MatGetRow(), MatGetSubmatrices(), MatSetValues() 308 @*/ 309 int MatGetValues(Mat mat,int m,int *idxm,int n,int *idxn,Scalar *v) 310 { 311 int ierr; 312 313 PetscValidHeaderSpecific(mat,MAT_COOKIE); 314 PetscValidIntPointer(idxm); 315 PetscValidIntPointer(idxn); 316 PetscValidScalarPointer(v); 317 if (!mat->assembled) SETERRQ(1,"MatGetValues:Not for unassembled matrix"); 318 319 PLogEventBegin(MAT_GetValues,mat,0,0,0); 320 ierr = (*mat->ops.getvalues)(mat,m,idxm,n,idxn,v); CHKERRQ(ierr); 321 PLogEventEnd(MAT_GetValues,mat,0,0,0); 322 return 0; 323 } 324 325 /* --------------------------------------------------------*/ 326 /*@ 327 MatMult - Computes the matrix-vector product, y = Ax. 328 329 Input Parameters: 330 . mat - the matrix 331 . x - the vector to be multilplied 332 333 Output Parameters: 334 . y - the result 335 336 .keywords: matrix, multiply, matrix-vector product 337 338 .seealso: MatMultTrans(), MatMultAdd(), MatMultTransAdd() 339 @*/ 340 int MatMult(Mat mat,Vec x,Vec y) 341 { 342 int ierr; 343 PetscValidHeaderSpecific(mat,MAT_COOKIE); 344 PetscValidHeaderSpecific(x,VEC_COOKIE);PetscValidHeaderSpecific(y,VEC_COOKIE); 345 if (!mat->assembled) SETERRQ(1,"MatMult:Not for unassembled matrix"); 346 /* if (mat->factor) SETERRQ(1,"MatMult:Not for factored matrix"); */ 347 if (x == y) SETERRQ(1,"MatMult:x and y must be different vectors"); 348 if (mat->N != x->N) SETERRQ(PETSC_ERR_SIZ,"MatMult:Mat mat,Vec x: global dim"); 349 if (mat->M != y->N) SETERRQ(PETSC_ERR_SIZ,"MatMult:Mat mat,Vec y: global dim"); 350 if (mat->m != y->n) SETERRQ(PETSC_ERR_SIZ,"MatMult:Mat mat,Vec y: local dim"); 351 352 PLogEventBegin(MAT_Mult,mat,x,y,0); 353 ierr = (*mat->ops.mult)(mat,x,y); CHKERRQ(ierr); 354 PLogEventEnd(MAT_Mult,mat,x,y,0); 355 return 0; 356 } 357 /*@ 358 MatMultTrans - Computes matrix transpose times a vector. 359 360 Input Parameters: 361 . mat - the matrix 362 . x - the vector to be multilplied 363 364 Output Parameters: 365 . y - the result 366 367 .keywords: matrix, multiply, matrix-vector product, transpose 368 369 .seealso: MatMult(), MatMultAdd(), MatMultTransAdd() 370 @*/ 371 int MatMultTrans(Mat mat,Vec x,Vec y) 372 { 373 int ierr; 374 PetscValidHeaderSpecific(mat,MAT_COOKIE); 375 PetscValidHeaderSpecific(x,VEC_COOKIE); PetscValidHeaderSpecific(y,VEC_COOKIE); 376 if (!mat->assembled) SETERRQ(1,"MatMultTrans:Not for unassembled matrix"); 377 /* if (mat->factor) SETERRQ(1,"MatMult:Not for factored matrix"); */ 378 if (x == y) SETERRQ(1,"MatMultTrans:x and y must be different vectors"); 379 if (mat->M != x->N) SETERRQ(PETSC_ERR_SIZ,"MatMultTrans:Mat mat,Vec x: global dim"); 380 if (mat->N != y->N) SETERRQ(PETSC_ERR_SIZ,"MatMultTrans:Mat mat,Vec y: global dim"); 381 382 PLogEventBegin(MAT_MultTrans,mat,x,y,0); 383 ierr = (*mat->ops.multtrans)(mat,x,y); CHKERRQ(ierr); 384 PLogEventEnd(MAT_MultTrans,mat,x,y,0); 385 return 0; 386 } 387 /*@ 388 MatMultAdd - Computes v3 = v2 + A * v1. 389 390 Input Parameters: 391 . mat - the matrix 392 . v1, v2 - the vectors 393 394 Output Parameters: 395 . v3 - the result 396 397 .keywords: matrix, multiply, matrix-vector product, add 398 399 .seealso: MatMultTrans(), MatMult(), MatMultTransAdd() 400 @*/ 401 int MatMultAdd(Mat mat,Vec v1,Vec v2,Vec v3) 402 { 403 int ierr; 404 PetscValidHeaderSpecific(mat,MAT_COOKIE);PetscValidHeaderSpecific(v1,VEC_COOKIE); 405 PetscValidHeaderSpecific(v2,VEC_COOKIE); PetscValidHeaderSpecific(v3,VEC_COOKIE); 406 if (!mat->assembled) SETERRQ(1,"MatMultAdd:Not for unassembled matrix"); 407 /* if (mat->factor) SETERRQ(1,"MatMult:Not for factored matrix"); */ 408 if (mat->N != v1->N) SETERRQ(PETSC_ERR_SIZ,"MatMultAdd:Mat mat,Vec v1: global dim"); 409 if (mat->M != v2->N) SETERRQ(PETSC_ERR_SIZ,"MatMultAdd:Mat mat,Vec v2: global dim"); 410 if (mat->M != v3->N) SETERRQ(PETSC_ERR_SIZ,"MatMultAdd:Mat mat,Vec v3: global dim"); 411 if (mat->m != v3->n) SETERRQ(PETSC_ERR_SIZ,"MatMultAdd:Mat mat,Vec v3: local dim"); 412 if (mat->m != v2->n) SETERRQ(PETSC_ERR_SIZ,"MatMultAdd:Mat mat,Vec v2: local dim"); 413 414 PLogEventBegin(MAT_MultAdd,mat,v1,v2,v3); 415 if (v1 == v3) SETERRQ(1,"MatMultAdd:v1 and v3 must be different vectors"); 416 ierr = (*mat->ops.multadd)(mat,v1,v2,v3); CHKERRQ(ierr); 417 PLogEventEnd(MAT_MultAdd,mat,v1,v2,v3); 418 return 0; 419 } 420 /*@ 421 MatMultTransAdd - Computes v3 = v2 + A' * v1. 422 423 Input Parameters: 424 . mat - the matrix 425 . v1, v2 - the vectors 426 427 Output Parameters: 428 . v3 - the result 429 430 .keywords: matrix, multiply, matrix-vector product, transpose, add 431 432 .seealso: MatMultTrans(), MatMultAdd(), MatMult() 433 @*/ 434 int MatMultTransAdd(Mat mat,Vec v1,Vec v2,Vec v3) 435 { 436 int ierr; 437 PetscValidHeaderSpecific(mat,MAT_COOKIE);PetscValidHeaderSpecific(v1,VEC_COOKIE); 438 PetscValidHeaderSpecific(v2,VEC_COOKIE);PetscValidHeaderSpecific(v3,VEC_COOKIE); 439 if (!mat->assembled) SETERRQ(1,"MatMultTransAdd:Not for unassembled matrix"); 440 /* if (mat->factor) SETERRQ(1,"MatMult:Not for factored matrix"); */ 441 if (!mat->ops.multtransadd) SETERRQ(PETSC_ERR_SUP,"MatMultTransAdd"); 442 if (v1 == v3) SETERRQ(1,"MatMultTransAdd:v1 and v2 must be different vectors"); 443 if (mat->M != v1->N) SETERRQ(PETSC_ERR_SIZ,"MatMultTransAdd:Mat mat,Vec v1: global dim"); 444 if (mat->N != v2->N) SETERRQ(PETSC_ERR_SIZ,"MatMultTransAdd:Mat mat,Vec v2: global dim"); 445 if (mat->N != v3->N) SETERRQ(PETSC_ERR_SIZ,"MatMultTransAdd:Mat mat,Vec v3: global dim"); 446 447 PLogEventBegin(MAT_MultTransAdd,mat,v1,v2,v3); 448 ierr = (*mat->ops.multtransadd)(mat,v1,v2,v3); CHKERRQ(ierr); 449 PLogEventEnd(MAT_MultTransAdd,mat,v1,v2,v3); 450 return 0; 451 } 452 /* ------------------------------------------------------------*/ 453 /*@C 454 MatGetInfo - Returns information about matrix storage (number of 455 nonzeros, memory). 456 457 Input Parameters: 458 . mat - the matrix 459 460 Output Parameters: 461 . flag - flag indicating the type of parameters to be returned 462 $ flag = MAT_LOCAL: local matrix 463 $ flag = MAT_GLOBAL_MAX: maximum over all processors 464 $ flag = MAT_GLOBAL_SUM: sum over all processors 465 . nz - the number of nonzeros [or PETSC_NULL] 466 . nzalloc - the number of allocated nonzeros [or PETSC_NULL] 467 . mem - the memory used (in bytes) [or PETSC_NULL] 468 469 .keywords: matrix, get, info, storage, nonzeros, memory 470 @*/ 471 int MatGetInfo(Mat mat,MatInfoType flag,int *nz,int *nzalloc,int *mem) 472 { 473 PetscValidHeaderSpecific(mat,MAT_COOKIE); 474 if (!mat->ops.getinfo) SETERRQ(PETSC_ERR_SUP,"MatGetInfo"); 475 return (*mat->ops.getinfo)(mat,flag,nz,nzalloc,mem); 476 } 477 /* ----------------------------------------------------------*/ 478 /*@ 479 MatILUDTFactor - Performs a drop tolerance ILU factorization. 480 481 Input Parameters: 482 . mat - the matrix 483 . dt - the drop tolerance 484 . maxnz - the maximum number of nonzeros per row allowed? 485 . row - row permutation 486 . col - column permutation 487 488 Output Parameters: 489 . fact - the factored matrix 490 491 .keywords: matrix, factor, LU, in-place 492 493 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor() 494 @*/ 495 int MatILUDTFactor(Mat mat,double dt,int maxnz,IS row,IS col,Mat *fact) 496 { 497 int ierr; 498 PetscValidHeaderSpecific(mat,MAT_COOKIE); 499 if (!mat->ops.iludtfactor) SETERRQ(PETSC_ERR_SUP,"MatILUDTFactor"); 500 if (!mat->assembled) SETERRQ(1,"MatILUDTFactor:Not for unassembled matrix"); 501 502 PLogEventBegin(MAT_ILUFactor,mat,row,col,0); 503 ierr = (*mat->ops.iludtfactor)(mat,dt,maxnz,row,col,fact); CHKERRQ(ierr); 504 PLogEventEnd(MAT_ILUFactor,mat,row,col,0); 505 506 return 0; 507 } 508 509 /*@ 510 MatLUFactor - Performs in-place LU factorization of matrix. 511 512 Input Parameters: 513 . mat - the matrix 514 . row - row permutation 515 . col - column permutation 516 . f - expected fill as ratio of original fill. 517 518 .keywords: matrix, factor, LU, in-place 519 520 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor() 521 @*/ 522 int MatLUFactor(Mat mat,IS row,IS col,double f) 523 { 524 int ierr; 525 PetscValidHeaderSpecific(mat,MAT_COOKIE); 526 PetscValidHeaderSpecific(row,IS_COOKIE); 527 PetscValidHeaderSpecific(col,IS_COOKIE); 528 if (mat->M != mat->N) SETERRQ(1,"MatLUFactor:matrix must be square"); 529 if (!mat->ops.lufactor) SETERRQ(PETSC_ERR_SUP,"MatLUFactor"); 530 if (!mat->assembled) SETERRQ(1,"MatLUFactor:Not for unassembled matrix"); 531 532 PLogEventBegin(MAT_LUFactor,mat,row,col,0); 533 ierr = (*mat->ops.lufactor)(mat,row,col,f); CHKERRQ(ierr); 534 PLogEventEnd(MAT_LUFactor,mat,row,col,0); 535 return 0; 536 } 537 /*@ 538 MatILUFactor - Performs in-place ILU factorization of matrix. 539 540 Input Parameters: 541 . mat - the matrix 542 . row - row permutation 543 . col - column permutation 544 . f - expected fill as ratio of original fill. 545 . level - number of levels of fill. 546 547 Note: probably really only in-place when level is zero. 548 .keywords: matrix, factor, ILU, in-place 549 550 .seealso: MatILUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor() 551 @*/ 552 int MatILUFactor(Mat mat,IS row,IS col,double f,int level) 553 { 554 int ierr; 555 PetscValidHeaderSpecific(mat,MAT_COOKIE); 556 PetscValidHeaderSpecific(row,IS_COOKIE); 557 PetscValidHeaderSpecific(col,IS_COOKIE); 558 if (mat->M != mat->N) SETERRQ(1,"MatILUFactor:matrix must be square"); 559 if (!mat->ops.ilufactor) SETERRQ(PETSC_ERR_SUP,"MatILUFactor"); 560 if (!mat->assembled) SETERRQ(1,"MatILUFactor:Not for unassembled matrix"); 561 562 PLogEventBegin(MAT_ILUFactor,mat,row,col,0); 563 ierr = (*mat->ops.ilufactor)(mat,row,col,f,level); CHKERRQ(ierr); 564 PLogEventEnd(MAT_ILUFactor,mat,row,col,0); 565 return 0; 566 } 567 568 /*@ 569 MatLUFactorSymbolic - Performs symbolic LU factorization of matrix. 570 Call this routine before calling MatLUFactorNumeric(). 571 572 Input Parameters: 573 . mat - the matrix 574 . row, col - row and column permutations 575 . f - expected fill as ratio of the original number of nonzeros, 576 for example 3.0; choosing this parameter well can result in 577 more efficient use of time and space. 578 579 Output Parameter: 580 . fact - new matrix that has been symbolically factored 581 582 Options Database Key: 583 $ -mat_lu_fill <f>, where f is the fill ratio 584 585 Notes: 586 See the file $(PETSC_DIR)/Performace for additional information about 587 choosing the fill factor for better efficiency. 588 589 .keywords: matrix, factor, LU, symbolic, fill 590 591 .seealso: MatLUFactor(), MatLUFactorNumeric(), MatCholeskyFactor() 592 @*/ 593 int MatLUFactorSymbolic(Mat mat,IS row,IS col,double f,Mat *fact) 594 { 595 int ierr,flg; 596 PetscValidHeaderSpecific(mat,MAT_COOKIE); 597 PetscValidHeaderSpecific(row,IS_COOKIE); 598 PetscValidHeaderSpecific(col,IS_COOKIE); 599 if (mat->M != mat->N) SETERRQ(1,"MatLUFactorSymbolic:matrix must be square"); 600 if (!fact) SETERRQ(1,"MatLUFactorSymbolic:Missing factor matrix argument"); 601 if (!mat->ops.lufactorsymbolic) SETERRQ(PETSC_ERR_SUP,"MatLUFactorSymbolic"); 602 if (!mat->assembled) SETERRQ(1,"MatLUFactorSymbolic:Not for unassembled matrix"); 603 604 ierr = OptionsGetDouble(PETSC_NULL,"-mat_lu_fill",&f,&flg); CHKERRQ(ierr); 605 PLogEventBegin(MAT_LUFactorSymbolic,mat,row,col,0); 606 ierr = (*mat->ops.lufactorsymbolic)(mat,row,col,f,fact); CHKERRQ(ierr); 607 PLogEventEnd(MAT_LUFactorSymbolic,mat,row,col,0); 608 return 0; 609 } 610 /*@ 611 MatLUFactorNumeric - Performs numeric LU factorization of a matrix. 612 Call this routine after first calling MatLUFactorSymbolic(). 613 614 Input Parameters: 615 . mat - the matrix 616 . row, col - row and column permutations 617 618 Output Parameters: 619 . fact - symbolically factored matrix that must have been generated 620 by MatLUFactorSymbolic() 621 622 Notes: 623 See MatLUFactor() for in-place factorization. See 624 MatCholeskyFactorNumeric() for the symmetric, positive definite case. 625 626 .keywords: matrix, factor, LU, numeric 627 628 .seealso: MatLUFactorSymbolic(), MatLUFactor(), MatCholeskyFactor() 629 @*/ 630 int MatLUFactorNumeric(Mat mat,Mat *fact) 631 { 632 int ierr,flg; 633 634 PetscValidHeaderSpecific(mat,MAT_COOKIE); 635 if (!fact) SETERRQ(1,"MatLUFactorNumeric:Missing factor matrix argument"); 636 if (!mat->ops.lufactornumeric) SETERRQ(PETSC_ERR_SUP,"MatLUFactorNumeric"); 637 if (!mat->assembled) SETERRQ(1,"MatLUFactorNumeric:Not for unassembled matrix"); 638 if (mat->M != (*fact)->M || mat->N != (*fact)->N) 639 SETERRQ(PETSC_ERR_SIZ,"MatLUFactorNumeric:Mat mat,Mat *fact: global dim"); 640 641 PLogEventBegin(MAT_LUFactorNumeric,mat,*fact,0,0); 642 ierr = (*mat->ops.lufactornumeric)(mat,fact); CHKERRQ(ierr); 643 PLogEventEnd(MAT_LUFactorNumeric,mat,*fact,0,0); 644 ierr = OptionsHasName(PETSC_NULL,"-mat_view_draw",&flg); CHKERRQ(ierr); 645 if (flg) { 646 Viewer viewer; 647 ierr = ViewerDrawOpenX((*fact)->comm,0,0,0,0,300,300,&viewer);CHKERRQ(ierr); 648 ierr = MatView(*fact,viewer); CHKERRQ(ierr); 649 ierr = ViewerFlush(viewer); CHKERRQ(ierr); 650 ierr = ViewerDestroy(viewer); CHKERRQ(ierr); 651 } 652 return 0; 653 } 654 /*@ 655 MatCholeskyFactor - Performs in-place Cholesky factorization of a 656 symmetric matrix. 657 658 Input Parameters: 659 . mat - the matrix 660 . perm - row and column permutations 661 . f - expected fill as ratio of original fill 662 663 Notes: 664 See MatLUFactor() for the nonsymmetric case. See also 665 MatCholeskyFactorSymbolic(), and MatCholeskyFactorNumeric(). 666 667 .keywords: matrix, factor, in-place, Cholesky 668 669 .seealso: MatLUFactor(), MatCholeskyFactorSymbolic(), MatCholeskyFactorNumeric() 670 @*/ 671 int MatCholeskyFactor(Mat mat,IS perm,double f) 672 { 673 int ierr; 674 PetscValidHeaderSpecific(mat,MAT_COOKIE); 675 PetscValidHeaderSpecific(perm,IS_COOKIE); 676 if (mat->M != mat->N) SETERRQ(1,"MatCholeskyFactor:matrix must be square"); 677 if (!mat->ops.choleskyfactor) SETERRQ(PETSC_ERR_SUP,"MatCholeskyFactor"); 678 if (!mat->assembled) SETERRQ(1,"MatCholeskyFactor:Not for unassembled matrix"); 679 680 PLogEventBegin(MAT_CholeskyFactor,mat,perm,0,0); 681 ierr = (*mat->ops.choleskyfactor)(mat,perm,f); CHKERRQ(ierr); 682 PLogEventEnd(MAT_CholeskyFactor,mat,perm,0,0); 683 return 0; 684 } 685 /*@ 686 MatCholeskyFactorSymbolic - Performs symbolic Cholesky factorization 687 of a symmetric matrix. 688 689 Input Parameters: 690 . mat - the matrix 691 . perm - row and column permutations 692 . f - expected fill as ratio of original 693 694 Output Parameter: 695 . fact - the factored matrix 696 697 Notes: 698 See MatLUFactorSymbolic() for the nonsymmetric case. See also 699 MatCholeskyFactor() and MatCholeskyFactorNumeric(). 700 701 .keywords: matrix, factor, factorization, symbolic, Cholesky 702 703 .seealso: MatLUFactorSymbolic(), MatCholeskyFactor(), MatCholeskyFactorNumeric() 704 @*/ 705 int MatCholeskyFactorSymbolic(Mat mat,IS perm,double f,Mat *fact) 706 { 707 int ierr; 708 PetscValidHeaderSpecific(mat,MAT_COOKIE); 709 PetscValidHeaderSpecific(perm,IS_COOKIE); 710 if (mat->M != mat->N) SETERRQ(1,"MatCholeskyFactorSymbolic:matrix must be square"); 711 if (!fact) SETERRQ(1,"MatCholeskyFactorSymbolic:Missing factor matrix argument"); 712 if (!mat->ops.choleskyfactorsymbolic)SETERRQ(PETSC_ERR_SUP,"MatCholeskyFactorSymbolic"); 713 if (!mat->assembled) SETERRQ(1,"MatCholeskyFactorSymbolic:Not for unassembled matrix"); 714 715 PLogEventBegin(MAT_CholeskyFactorSymbolic,mat,perm,0,0); 716 ierr = (*mat->ops.choleskyfactorsymbolic)(mat,perm,f,fact); CHKERRQ(ierr); 717 PLogEventEnd(MAT_CholeskyFactorSymbolic,mat,perm,0,0); 718 return 0; 719 } 720 /*@ 721 MatCholeskyFactorNumeric - Performs numeric Cholesky factorization 722 of a symmetric matrix. Call this routine after first calling 723 MatCholeskyFactorSymbolic(). 724 725 Input Parameter: 726 . mat - the initial matrix 727 728 Output Parameter: 729 . fact - the factored matrix 730 731 .keywords: matrix, factor, numeric, Cholesky 732 733 .seealso: MatCholeskyFactorSymbolic(), MatCholeskyFactor(), MatLUFactorNumeric() 734 @*/ 735 int MatCholeskyFactorNumeric(Mat mat,Mat *fact) 736 { 737 int ierr; 738 PetscValidHeaderSpecific(mat,MAT_COOKIE); 739 if (!fact) SETERRQ(1,"MatCholeskyFactorNumeric:Missing factor matrix argument"); 740 if (!mat->ops.choleskyfactornumeric) SETERRQ(PETSC_ERR_SUP,"MatCholeskyFactorNumeric"); 741 if (!mat->assembled) SETERRQ(1,"MatCholeskyFactorNumeric:Not for unassembled matrix"); 742 if (mat->M != (*fact)->M || mat->N != (*fact)->N) 743 SETERRQ(PETSC_ERR_SIZ,"MatCholeskyFactorNumeric:Mat mat,Mat *fact: global dim"); 744 745 PLogEventBegin(MAT_CholeskyFactorNumeric,mat,*fact,0,0); 746 ierr = (*mat->ops.choleskyfactornumeric)(mat,fact); CHKERRQ(ierr); 747 PLogEventEnd(MAT_CholeskyFactorNumeric,mat,*fact,0,0); 748 return 0; 749 } 750 /* ----------------------------------------------------------------*/ 751 /*@ 752 MatSolve - Solves A x = b, given a factored matrix. 753 754 Input Parameters: 755 . mat - the factored matrix 756 . b - the right-hand-side vector 757 758 Output Parameter: 759 . x - the result vector 760 761 .keywords: matrix, linear system, solve, LU, Cholesky, triangular solve 762 763 .seealso: MatSolveAdd(), MatSolveTrans(), MatSolveTransAdd() 764 @*/ 765 int MatSolve(Mat mat,Vec b,Vec x) 766 { 767 int ierr; 768 PetscValidHeaderSpecific(mat,MAT_COOKIE); 769 PetscValidHeaderSpecific(b,VEC_COOKIE); PetscValidHeaderSpecific(x,VEC_COOKIE); 770 if (x == b) SETERRQ(1,"MatSolve:x and y must be different vectors"); 771 if (!mat->factor) SETERRQ(1,"MatSolve:Unfactored matrix"); 772 if (mat->N != x->N) SETERRQ(PETSC_ERR_SIZ,"MatSolve:Mat mat,Vec x: global dim"); 773 if (mat->M != b->N) SETERRQ(PETSC_ERR_SIZ,"MatSolve:Mat mat,Vec b: global dim"); 774 if (mat->m != b->n) SETERRQ(PETSC_ERR_SIZ,"MatSolve:Mat mat,Vec b: local dim"); 775 776 if (!mat->ops.solve) SETERRQ(PETSC_ERR_SUP,"MatSolve"); 777 PLogEventBegin(MAT_Solve,mat,b,x,0); 778 ierr = (*mat->ops.solve)(mat,b,x); CHKERRQ(ierr); 779 PLogEventEnd(MAT_Solve,mat,b,x,0); 780 return 0; 781 } 782 783 /* @ 784 MatForwardSolve - Solves L x = b, given a factored matrix, A = LU. 785 786 Input Parameters: 787 . mat - the factored matrix 788 . b - the right-hand-side vector 789 790 Output Parameter: 791 . x - the result vector 792 793 Notes: 794 MatSolve() should be used for most applications, as it performs 795 a forward solve followed by a backward solve. 796 797 .keywords: matrix, forward, LU, Cholesky, triangular solve 798 799 .seealso: MatSolve(), MatBackwardSolve() 800 @ */ 801 int MatForwardSolve(Mat mat,Vec b,Vec x) 802 { 803 int ierr; 804 PetscValidHeaderSpecific(mat,MAT_COOKIE); 805 PetscValidHeaderSpecific(b,VEC_COOKIE); PetscValidHeaderSpecific(x,VEC_COOKIE); 806 if (x == b) SETERRQ(1,"MatForwardSolve:x and y must be different vectors"); 807 if (!mat->factor) SETERRQ(1,"MatForwardSolve:Unfactored matrix"); 808 if (!mat->ops.forwardsolve) SETERRQ(PETSC_ERR_SUP,"MatForwardSolve"); 809 if (mat->N != x->N) SETERRQ(PETSC_ERR_SIZ,"MatForwardSolve:Mat mat,Vec x: global dim"); 810 if (mat->M != b->N) SETERRQ(PETSC_ERR_SIZ,"MatForwardSolve:Mat mat,Vec b: global dim"); 811 if (mat->m != b->n) SETERRQ(PETSC_ERR_SIZ,"MatForwardSolve:Mat mat,Vec b: local dim"); 812 813 PLogEventBegin(MAT_ForwardSolve,mat,b,x,0); 814 ierr = (*mat->ops.forwardsolve)(mat,b,x); CHKERRQ(ierr); 815 PLogEventEnd(MAT_ForwardSolve,mat,b,x,0); 816 return 0; 817 } 818 819 /* @ 820 MatBackwardSolve - Solves U x = b, given a factored matrix, A = LU. 821 822 Input Parameters: 823 . mat - the factored matrix 824 . b - the right-hand-side vector 825 826 Output Parameter: 827 . x - the result vector 828 829 Notes: 830 MatSolve() should be used for most applications, as it performs 831 a forward solve followed by a backward solve. 832 833 .keywords: matrix, backward, LU, Cholesky, triangular solve 834 835 .seealso: MatSolve(), MatForwardSolve() 836 @ */ 837 int MatBackwardSolve(Mat mat,Vec b,Vec x) 838 { 839 int ierr; 840 PetscValidHeaderSpecific(mat,MAT_COOKIE); 841 PetscValidHeaderSpecific(b,VEC_COOKIE); PetscValidHeaderSpecific(x,VEC_COOKIE); 842 if (x == b) SETERRQ(1,"MatBackwardSolve:x and b must be different vectors"); 843 if (!mat->factor) SETERRQ(1,"MatBackwardSolve:Unfactored matrix"); 844 if (!mat->ops.backwardsolve) SETERRQ(PETSC_ERR_SUP,"MatBackwardSolve"); 845 if (mat->N != x->N) SETERRQ(PETSC_ERR_SIZ,"MatBackwardSolve:Mat mat,Vec x: global dim"); 846 if (mat->M != b->N) SETERRQ(PETSC_ERR_SIZ,"MatBackwardSolve:Mat mat,Vec b: global dim"); 847 if (mat->m != b->n) SETERRQ(PETSC_ERR_SIZ,"MatBackwardSolve:Mat mat,Vec b: local dim"); 848 849 PLogEventBegin(MAT_BackwardSolve,mat,b,x,0); 850 ierr = (*mat->ops.backwardsolve)(mat,b,x); CHKERRQ(ierr); 851 PLogEventEnd(MAT_BackwardSolve,mat,b,x,0); 852 return 0; 853 } 854 855 /*@ 856 MatSolveAdd - Computes x = y + inv(A)*b, given a factored matrix. 857 858 Input Parameters: 859 . mat - the factored matrix 860 . b - the right-hand-side vector 861 . y - the vector to be added to 862 863 Output Parameter: 864 . x - the result vector 865 866 .keywords: matrix, linear system, solve, LU, Cholesky, add 867 868 .seealso: MatSolve(), MatSolveTrans(), MatSolveTransAdd() 869 @*/ 870 int MatSolveAdd(Mat mat,Vec b,Vec y,Vec x) 871 { 872 Scalar one = 1.0; 873 Vec tmp; 874 int ierr; 875 PetscValidHeaderSpecific(mat,MAT_COOKIE);PetscValidHeaderSpecific(y,VEC_COOKIE); 876 PetscValidHeaderSpecific(b,VEC_COOKIE); PetscValidHeaderSpecific(x,VEC_COOKIE); 877 if (x == b) SETERRQ(1,"MatSolveAdd:x and b must be different vectors"); 878 if (!mat->factor) SETERRQ(1,"MatSolveAdd:Unfactored matrix"); 879 if (mat->N != x->N) SETERRQ(PETSC_ERR_SIZ,"MatSolveAdd:Mat mat,Vec x: global dim"); 880 if (mat->M != b->N) SETERRQ(PETSC_ERR_SIZ,"MatSolveAdd:Mat mat,Vec b: global dim"); 881 if (mat->M != y->N) SETERRQ(PETSC_ERR_SIZ,"MatSolveAdd:Mat mat,Vec y: global dim"); 882 if (mat->m != b->n) SETERRQ(PETSC_ERR_SIZ,"MatSolveAdd:Mat mat,Vec b: local dim"); 883 if (x->n != y->n) SETERRQ(PETSC_ERR_SIZ,"MatSolveAdd:Vec x,Vec y: local dim"); 884 885 PLogEventBegin(MAT_SolveAdd,mat,b,x,y); 886 if (mat->ops.solveadd) { 887 ierr = (*mat->ops.solveadd)(mat,b,y,x); CHKERRQ(ierr); 888 } 889 else { 890 /* do the solve then the add manually */ 891 if (x != y) { 892 ierr = MatSolve(mat,b,x); CHKERRQ(ierr); 893 ierr = VecAXPY(&one,y,x); CHKERRQ(ierr); 894 } 895 else { 896 ierr = VecDuplicate(x,&tmp); CHKERRQ(ierr); 897 PLogObjectParent(mat,tmp); 898 ierr = VecCopy(x,tmp); CHKERRQ(ierr); 899 ierr = MatSolve(mat,b,x); CHKERRQ(ierr); 900 ierr = VecAXPY(&one,tmp,x); CHKERRQ(ierr); 901 ierr = VecDestroy(tmp); CHKERRQ(ierr); 902 } 903 } 904 PLogEventEnd(MAT_SolveAdd,mat,b,x,y); 905 return 0; 906 } 907 /*@ 908 MatSolveTrans - Solves A' x = b, given a factored matrix. 909 910 Input Parameters: 911 . mat - the factored matrix 912 . b - the right-hand-side vector 913 914 Output Parameter: 915 . x - the result vector 916 917 .keywords: matrix, linear system, solve, LU, Cholesky, transpose 918 919 .seealso: MatSolve(), MatSolveAdd(), MatSolveTransAdd() 920 @*/ 921 int MatSolveTrans(Mat mat,Vec b,Vec x) 922 { 923 int ierr; 924 PetscValidHeaderSpecific(mat,MAT_COOKIE); 925 PetscValidHeaderSpecific(b,VEC_COOKIE); PetscValidHeaderSpecific(x,VEC_COOKIE); 926 if (!mat->factor) SETERRQ(1,"MatSolveTrans:Unfactored matrix"); 927 if (x == b) SETERRQ(1,"MatSolveTrans:x and b must be different vectors"); 928 if (!mat->ops.solvetrans) SETERRQ(PETSC_ERR_SUP,"MatSolveTrans"); 929 if (mat->M != x->N) SETERRQ(PETSC_ERR_SIZ,"MatSolveTrans:Mat mat,Vec x: global dim"); 930 if (mat->N != b->N) SETERRQ(PETSC_ERR_SIZ,"MatSolveTrans:Mat mat,Vec b: global dim"); 931 932 PLogEventBegin(MAT_SolveTrans,mat,b,x,0); 933 ierr = (*mat->ops.solvetrans)(mat,b,x); CHKERRQ(ierr); 934 PLogEventEnd(MAT_SolveTrans,mat,b,x,0); 935 return 0; 936 } 937 /*@ 938 MatSolveTransAdd - Computes x = y + inv(trans(A)) b, given a 939 factored matrix. 940 941 Input Parameters: 942 . mat - the factored matrix 943 . b - the right-hand-side vector 944 . y - the vector to be added to 945 946 Output Parameter: 947 . x - the result vector 948 949 .keywords: matrix, linear system, solve, LU, Cholesky, transpose, add 950 951 .seealso: MatSolve(), MatSolveAdd(), MatSolveTrans() 952 @*/ 953 int MatSolveTransAdd(Mat mat,Vec b,Vec y,Vec x) 954 { 955 Scalar one = 1.0; 956 int ierr; 957 Vec tmp; 958 PetscValidHeaderSpecific(mat,MAT_COOKIE);PetscValidHeaderSpecific(y,VEC_COOKIE); 959 PetscValidHeaderSpecific(b,VEC_COOKIE); PetscValidHeaderSpecific(x,VEC_COOKIE); 960 if (x == b) SETERRQ(1,"MatSolveTransAdd:x and b must be different vectors"); 961 if (!mat->factor) SETERRQ(1,"MatSolveTransAdd:Unfactored matrix"); 962 if (mat->M != x->N) SETERRQ(PETSC_ERR_SIZ,"MatSolveTransAdd:Mat mat,Vec x: global dim"); 963 if (mat->N != b->N) SETERRQ(PETSC_ERR_SIZ,"MatSolveTransAdd:Mat mat,Vec b: global dim"); 964 if (mat->N != y->N) SETERRQ(PETSC_ERR_SIZ,"MatSolveTransAdd:Mat mat,Vec y: global dim"); 965 if (x->n != y->n) SETERRQ(PETSC_ERR_SIZ,"MatSolveTransAdd:Vec x,Vec y: local dim"); 966 967 PLogEventBegin(MAT_SolveTransAdd,mat,b,x,y); 968 if (mat->ops.solvetransadd) { 969 ierr = (*mat->ops.solvetransadd)(mat,b,y,x); CHKERRQ(ierr); 970 } 971 else { 972 /* do the solve then the add manually */ 973 if (x != y) { 974 ierr = MatSolveTrans(mat,b,x); CHKERRQ(ierr); 975 ierr = VecAXPY(&one,y,x); CHKERRQ(ierr); 976 } 977 else { 978 ierr = VecDuplicate(x,&tmp); CHKERRQ(ierr); 979 PLogObjectParent(mat,tmp); 980 ierr = VecCopy(x,tmp); CHKERRQ(ierr); 981 ierr = MatSolveTrans(mat,b,x); CHKERRQ(ierr); 982 ierr = VecAXPY(&one,tmp,x); CHKERRQ(ierr); 983 ierr = VecDestroy(tmp); CHKERRQ(ierr); 984 } 985 } 986 PLogEventEnd(MAT_SolveTransAdd,mat,b,x,y); 987 return 0; 988 } 989 /* ----------------------------------------------------------------*/ 990 991 /*@ 992 MatRelax - Computes one relaxation sweep. 993 994 Input Parameters: 995 . mat - the matrix 996 . b - the right hand side 997 . omega - the relaxation factor 998 . flag - flag indicating the type of SOR, one of 999 $ SOR_FORWARD_SWEEP 1000 $ SOR_BACKWARD_SWEEP 1001 $ SOR_SYMMETRIC_SWEEP (SSOR method) 1002 $ SOR_LOCAL_FORWARD_SWEEP 1003 $ SOR_LOCAL_BACKWARD_SWEEP 1004 $ SOR_LOCAL_SYMMETRIC_SWEEP (local SSOR) 1005 $ SOR_APPLY_UPPER, SOR_APPLY_LOWER - applies 1006 $ upper/lower triangular part of matrix to 1007 $ vector (with omega) 1008 $ SOR_ZERO_INITIAL_GUESS - zero initial guess 1009 . shift - diagonal shift 1010 . its - the number of iterations 1011 1012 Output Parameters: 1013 . x - the solution (can contain an initial guess) 1014 1015 Notes: 1016 SOR_LOCAL_FORWARD_SWEEP, SOR_LOCAL_BACKWARD_SWEEP, and 1017 SOR_LOCAL_SYMMETRIC_SWEEP perform seperate independent smoothings 1018 on each processor. 1019 1020 Application programmers will not generally use MatRelax() directly, 1021 but instead will employ the SLES/PC interface. 1022 1023 Notes for Advanced Users: 1024 The flags are implemented as bitwise inclusive or operations. 1025 For example, use (SOR_ZERO_INITIAL_GUESS | SOR_SYMMETRIC_SWEEP) 1026 to specify a zero initial guess for SSOR. 1027 1028 .keywords: matrix, relax, relaxation, sweep 1029 @*/ 1030 int MatRelax(Mat mat,Vec b,double omega,MatSORType flag,double shift, 1031 int its,Vec x) 1032 { 1033 int ierr; 1034 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1035 PetscValidHeaderSpecific(b,VEC_COOKIE); PetscValidHeaderSpecific(x,VEC_COOKIE); 1036 if (!mat->ops.relax) SETERRQ(PETSC_ERR_SUP,"MatRelax"); 1037 if (!mat->assembled) SETERRQ(1,"MatRelax:Not for unassembled matrix"); 1038 if (mat->N != x->N) SETERRQ(PETSC_ERR_SIZ,"MatRelax:Mat mat,Vec x: global dim"); 1039 if (mat->M != b->N) SETERRQ(PETSC_ERR_SIZ,"MatRelax:Mat mat,Vec b: global dim"); 1040 if (mat->m != b->n) SETERRQ(PETSC_ERR_SIZ,"MatRelax:Mat mat,Vec b: local dim"); 1041 1042 PLogEventBegin(MAT_Relax,mat,b,x,0); 1043 ierr =(*mat->ops.relax)(mat,b,omega,flag,shift,its,x); CHKERRQ(ierr); 1044 PLogEventEnd(MAT_Relax,mat,b,x,0); 1045 return 0; 1046 } 1047 1048 /* 1049 Default matrix copy routine. 1050 */ 1051 int MatCopy_Basic(Mat A,Mat B) 1052 { 1053 int ierr,i,rstart,rend,nz,*cwork; 1054 Scalar *vwork; 1055 1056 ierr = MatZeroEntries(B); CHKERRQ(ierr); 1057 ierr = MatGetOwnershipRange(A,&rstart,&rend); CHKERRQ(ierr); 1058 for (i=rstart; i<rend; i++) { 1059 ierr = MatGetRow(A,i,&nz,&cwork,&vwork); CHKERRQ(ierr); 1060 ierr = MatSetValues(B,1,&i,nz,cwork,vwork,INSERT_VALUES); CHKERRQ(ierr); 1061 ierr = MatRestoreRow(A,i,&nz,&cwork,&vwork); CHKERRQ(ierr); 1062 } 1063 ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 1064 ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 1065 return 0; 1066 } 1067 1068 /*@C 1069 MatCopy - Copys a matrix to another matrix. 1070 1071 Input Parameters: 1072 . A - the matrix 1073 1074 Output Parameter: 1075 . B - where the copy is put 1076 1077 Notes: 1078 MatCopy() copies the matrix entries of a matrix to another existing 1079 matrix (after first zeroing the second matrix). A related routine is 1080 MatConvert(), which first creates a new matrix and then copies the data. 1081 1082 .keywords: matrix, copy, convert 1083 1084 .seealso: MatConvert() 1085 @*/ 1086 int MatCopy(Mat A,Mat B) 1087 { 1088 int ierr; 1089 PetscValidHeaderSpecific(A,MAT_COOKIE); PetscValidHeaderSpecific(B,MAT_COOKIE); 1090 if (!A->assembled) SETERRQ(1,"MatCopy:Not for unassembled matrix"); 1091 if (A->M != B->M || A->N != B->N) SETERRQ(PETSC_ERR_SIZ,"MatCopy:Mat A,Mat B: global dim"); 1092 1093 PLogEventBegin(MAT_Copy,A,B,0,0); 1094 if (A->ops.copy) { 1095 ierr = (*A->ops.copy)(A,B); CHKERRQ(ierr); 1096 } 1097 else { /* generic conversion */ 1098 ierr = MatCopy_Basic(A,B); CHKERRQ(ierr); 1099 } 1100 PLogEventEnd(MAT_Copy,A,B,0,0); 1101 return 0; 1102 } 1103 1104 /*@C 1105 MatConvert - Converts a matrix to another matrix, either of the same 1106 or different type. 1107 1108 Input Parameters: 1109 . mat - the matrix 1110 . newtype - new matrix type. Use MATSAME to create a new matrix of the 1111 same type as the original matrix. 1112 1113 Output Parameter: 1114 . M - pointer to place new matrix 1115 1116 Notes: 1117 MatConvert() first creates a new matrix and then copies the data from 1118 the first matrix. A related routine is MatCopy(), which copies the matrix 1119 entries of one matrix to another already existing matrix context. 1120 1121 .keywords: matrix, copy, convert 1122 1123 .seealso: MatCopy() 1124 @*/ 1125 int MatConvert(Mat mat,MatType newtype,Mat *M) 1126 { 1127 int ierr; 1128 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1129 if (!M) SETERRQ(1,"MatConvert:Bad new matrix address"); 1130 if (!mat->assembled) SETERRQ(1,"MatConvert:Not for unassembled matrix"); 1131 1132 PLogEventBegin(MAT_Convert,mat,0,0,0); 1133 if (newtype == mat->type || newtype == MATSAME) { 1134 if (mat->ops.convertsametype) { /* customized copy */ 1135 ierr = (*mat->ops.convertsametype)(mat,M,COPY_VALUES); CHKERRQ(ierr); 1136 } 1137 else { /* generic conversion */ 1138 ierr = MatConvert_Basic(mat,newtype,M); CHKERRQ(ierr); 1139 } 1140 } 1141 else if (mat->ops.convert) { /* customized conversion */ 1142 ierr = (*mat->ops.convert)(mat,newtype,M); CHKERRQ(ierr); 1143 } 1144 else { /* generic conversion */ 1145 ierr = MatConvert_Basic(mat,newtype,M); CHKERRQ(ierr); 1146 } 1147 PLogEventEnd(MAT_Convert,mat,0,0,0); 1148 return 0; 1149 } 1150 1151 /*@ 1152 MatGetDiagonal - Gets the diagonal of a matrix. 1153 1154 Input Parameters: 1155 . mat - the matrix 1156 . v - the vector for storing the diagonal 1157 1158 Output Parameter: 1159 . v - the diagonal of the matrix 1160 1161 .keywords: matrix, get, diagonal 1162 @*/ 1163 int MatGetDiagonal(Mat mat,Vec v) 1164 { 1165 PetscValidHeaderSpecific(mat,MAT_COOKIE);PetscValidHeaderSpecific(v,VEC_COOKIE); 1166 if (!mat->assembled) SETERRQ(1,"MatGetDiagonal:Not for unassembled matrix"); 1167 if (mat->ops.getdiagonal) return (*mat->ops.getdiagonal)(mat,v); 1168 SETERRQ(PETSC_ERR_SUP,"MatGetDiagonal"); 1169 } 1170 1171 /*@C 1172 MatTranspose - Computes an in-place or out-of-place transpose of a matrix. 1173 1174 Input Parameter: 1175 . mat - the matrix to transpose 1176 1177 Output Parameters: 1178 . B - the transpose (or pass in PETSC_NULL for an in-place transpose) 1179 1180 .keywords: matrix, transpose 1181 1182 .seealso: MatMultTrans(), MatMultTransAdd() 1183 @*/ 1184 int MatTranspose(Mat mat,Mat *B) 1185 { 1186 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1187 if (!mat->assembled) SETERRQ(1,"MatTranspose:Not for unassembled matrix"); 1188 if (mat->ops.transpose) return (*mat->ops.transpose)(mat,B); 1189 SETERRQ(PETSC_ERR_SUP,"MatTranspose"); 1190 } 1191 1192 /*@ 1193 MatEqual - Compares two matrices. 1194 1195 Input Parameters: 1196 . A - the first matrix 1197 . B - the second matrix 1198 1199 Output Parameter: 1200 . flg : PETSC_TRUE if the matrices are equal; 1201 PETSC_FALSE otherwise. 1202 1203 .keywords: matrix, equal, equivalent 1204 @*/ 1205 int MatEqual(Mat A,Mat B,PetscTruth *flg) 1206 { 1207 PetscValidHeaderSpecific(A,MAT_COOKIE); PetscValidHeaderSpecific(B,MAT_COOKIE); 1208 PetscValidIntPointer(flg); 1209 if (!A->assembled) SETERRQ(1,"MatEqual:Not for unassembled matrix"); 1210 if (!B->assembled) SETERRQ(1,"MatEqual:Not for unassembled matrix"); 1211 if (A->M != B->M || A->N != B->N) SETERRQ(PETSC_ERR_SIZ,"MatCopy:Mat A,Mat B: global dim"); 1212 if (A->ops.equal) return (*A->ops.equal)(A,B,flg); 1213 SETERRQ(PETSC_ERR_SUP,"MatEqual"); 1214 } 1215 1216 /*@ 1217 MatDiagonalScale - Scales a matrix on the left and right by diagonal 1218 matrices that are stored as vectors. Either of the two scaling 1219 matrices can be PETSC_NULL. 1220 1221 Input Parameters: 1222 . mat - the matrix to be scaled 1223 . l - the left scaling vector (or PETSC_NULL) 1224 . r - the right scaling vector (or PETSC_NULL) 1225 1226 Notes: 1227 MatDiagonalScale() computes A <- LAR, where 1228 $ L = a diagonal matrix 1229 $ R = a diagonal matrix 1230 1231 .keywords: matrix, diagonal, scale 1232 1233 .seealso: MatDiagonalScale() 1234 @*/ 1235 int MatDiagonalScale(Mat mat,Vec l,Vec r) 1236 { 1237 int ierr; 1238 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1239 if (!mat->ops.diagonalscale) SETERRQ(PETSC_ERR_SUP,"MatDiagonalScale"); 1240 if (l) PetscValidHeaderSpecific(l,VEC_COOKIE); 1241 if (r) PetscValidHeaderSpecific(r,VEC_COOKIE); 1242 if (!mat->assembled) SETERRQ(1,"MatDiagonalScale:Not for unassembled matrix"); 1243 1244 PLogEventBegin(MAT_Scale,mat,0,0,0); 1245 ierr = (*mat->ops.diagonalscale)(mat,l,r); CHKERRQ(ierr); 1246 PLogEventEnd(MAT_Scale,mat,0,0,0); 1247 return 0; 1248 } 1249 1250 /*@ 1251 MatScale - Scales all elements of a matrix by a given number. 1252 1253 Input Parameters: 1254 . mat - the matrix to be scaled 1255 . a - the scaling value 1256 1257 Output Parameter: 1258 . mat - the scaled matrix 1259 1260 .keywords: matrix, scale 1261 1262 .seealso: MatDiagonalScale() 1263 @*/ 1264 int MatScale(Scalar *a,Mat mat) 1265 { 1266 int ierr; 1267 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1268 PetscValidScalarPointer(a); 1269 if (!mat->ops.scale) SETERRQ(PETSC_ERR_SUP,"MatScale"); 1270 if (!mat->assembled) SETERRQ(1,"MatScale:Not for unassembled matrix"); 1271 1272 PLogEventBegin(MAT_Scale,mat,0,0,0); 1273 ierr = (*mat->ops.scale)(a,mat); CHKERRQ(ierr); 1274 PLogEventEnd(MAT_Scale,mat,0,0,0); 1275 return 0; 1276 } 1277 1278 /*@ 1279 MatNorm - Calculates various norms of a matrix. 1280 1281 Input Parameters: 1282 . mat - the matrix 1283 . type - the type of norm, NORM_1, NORM_2, NORM_FROBENIUS, NORM_INFINITY 1284 1285 Output Parameters: 1286 . norm - the resulting norm 1287 1288 .keywords: matrix, norm, Frobenius 1289 @*/ 1290 int MatNorm(Mat mat,NormType type,double *norm) 1291 { 1292 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1293 PetscValidScalarPointer(norm); 1294 1295 if (!norm) SETERRQ(1,"MatNorm:bad addess for value"); 1296 if (!mat->assembled) SETERRQ(1,"MatNorm:Not for unassembled matrix"); 1297 if (mat->ops.norm) return (*mat->ops.norm)(mat,type,norm); 1298 SETERRQ(PETSC_ERR_SUP,"MatNorm:Not for this matrix type"); 1299 } 1300 1301 /*@ 1302 MatAssemblyBegin - Begins assembling the matrix. This routine should 1303 be called after completing all calls to MatSetValues(). 1304 1305 Input Parameters: 1306 . mat - the matrix 1307 . type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY 1308 1309 Notes: 1310 MatSetValues() generally caches the values. The matrix is ready to 1311 use only after MatAssemblyBegin() and MatAssemblyEnd() have been called. 1312 Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES 1313 in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before 1314 using the matrix. 1315 1316 .keywords: matrix, assembly, assemble, begin 1317 1318 .seealso: MatAssemblyEnd(), MatSetValues() 1319 @*/ 1320 int MatAssemblyBegin(Mat mat,MatAssemblyType type) 1321 { 1322 int ierr; 1323 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1324 if (mat->assembled) { 1325 mat->was_assembled = PETSC_TRUE; 1326 mat->assembled = PETSC_FALSE; 1327 } 1328 PLogEventBegin(MAT_AssemblyBegin,mat,0,0,0); 1329 if (mat->ops.assemblybegin){ierr = (*mat->ops.assemblybegin)(mat,type);CHKERRQ(ierr);} 1330 PLogEventEnd(MAT_AssemblyBegin,mat,0,0,0); 1331 return 0; 1332 } 1333 1334 /*@ 1335 MatAssemblyEnd - Completes assembling the matrix. This routine should 1336 be called after MatAssemblyBegin(). 1337 1338 Input Parameters: 1339 . mat - the matrix 1340 . type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY 1341 1342 Options Database Keys: 1343 $ -mat_view_info : Prints info on matrix at 1344 $ conclusion of MatEndAssembly() 1345 $ -mat_view_info_detailed: Prints more detailed info. 1346 $ -mat_view : Prints matrix in ASCII format. 1347 $ -mat_view_matlab : Prints matrix in Matlab format. 1348 $ -mat_view_draw : Draws nonzero structure of matrix, 1349 $ using MatView() and DrawOpenX(). 1350 $ -display <name> : Set display name (default is host) 1351 $ -draw_pause <sec> : Set number of seconds to pause after display 1352 1353 Notes: 1354 MatSetValues() generally caches the values. The matrix is ready to 1355 use only after MatAssemblyBegin() and MatAssemblyEnd() have been called. 1356 Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES 1357 in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before 1358 using the matrix. 1359 1360 .keywords: matrix, assembly, assemble, end 1361 1362 .seealso: MatAssemblyBegin(), MatSetValues(), DrawOpenX(), MatView() 1363 @*/ 1364 int MatAssemblyEnd(Mat mat,MatAssemblyType type) 1365 { 1366 int ierr,flg; 1367 static int inassm = 0; 1368 1369 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1370 inassm++; 1371 PLogEventBegin(MAT_AssemblyEnd,mat,0,0,0); 1372 if (mat->ops.assemblyend) { 1373 ierr = (*mat->ops.assemblyend)(mat,type); CHKERRQ(ierr); 1374 } 1375 mat->assembled = PETSC_TRUE; mat->num_ass++; 1376 PLogEventEnd(MAT_AssemblyEnd,mat,0,0,0); 1377 1378 if (inassm == 1) { 1379 ierr = OptionsHasName(PETSC_NULL,"-mat_view_info",&flg); CHKERRQ(ierr); 1380 if (flg) { 1381 Viewer viewer; 1382 ierr = ViewerFileOpenASCII(mat->comm,"stdout",&viewer);CHKERRQ(ierr); 1383 ierr = ViewerSetFormat(viewer,ASCII_FORMAT_INFO,0);CHKERRQ(ierr); 1384 ierr = MatView(mat,viewer); CHKERRQ(ierr); 1385 ierr = ViewerDestroy(viewer); CHKERRQ(ierr); 1386 } 1387 ierr = OptionsHasName(PETSC_NULL,"-mat_view_info_detailed",&flg);CHKERRQ(ierr); 1388 if (flg) { 1389 Viewer viewer; 1390 ierr = ViewerFileOpenASCII(mat->comm,"stdout",&viewer);CHKERRQ(ierr); 1391 ierr = ViewerSetFormat(viewer,ASCII_FORMAT_INFO_DETAILED,0);CHKERRQ(ierr); 1392 ierr = MatView(mat,viewer); CHKERRQ(ierr); 1393 ierr = ViewerDestroy(viewer); CHKERRQ(ierr); 1394 } 1395 ierr = OptionsHasName(PETSC_NULL,"-mat_view",&flg); CHKERRQ(ierr); 1396 if (flg) { 1397 Viewer viewer; 1398 ierr = ViewerFileOpenASCII(mat->comm,"stdout",&viewer);CHKERRQ(ierr); 1399 ierr = MatView(mat,viewer); CHKERRQ(ierr); 1400 ierr = ViewerDestroy(viewer); CHKERRQ(ierr); 1401 } 1402 ierr = OptionsHasName(PETSC_NULL,"-mat_view_matlab",&flg); CHKERRQ(ierr); 1403 if (flg) { 1404 Viewer viewer; 1405 ierr = ViewerFileOpenASCII(mat->comm,"stdout",&viewer);CHKERRQ(ierr); 1406 ierr = ViewerSetFormat(viewer,ASCII_FORMAT_MATLAB,"M");CHKERRQ(ierr); 1407 ierr = MatView(mat,viewer); CHKERRQ(ierr); 1408 ierr = ViewerDestroy(viewer); CHKERRQ(ierr); 1409 } 1410 ierr = OptionsHasName(PETSC_NULL,"-mat_view_draw",&flg); CHKERRQ(ierr); 1411 if (flg) { 1412 Viewer viewer; 1413 ierr = ViewerDrawOpenX(mat->comm,0,0,0,0,300,300,&viewer); CHKERRQ(ierr); 1414 ierr = MatView(mat,viewer); CHKERRQ(ierr); 1415 ierr = ViewerFlush(viewer); CHKERRQ(ierr); 1416 ierr = ViewerDestroy(viewer); CHKERRQ(ierr); 1417 } 1418 } 1419 inassm--; 1420 return 0; 1421 } 1422 1423 /*@ 1424 MatCompress - Tries to store the matrix in as little space as 1425 possible. May fail if memory is already fully used, since it 1426 tries to allocate new space. 1427 1428 Input Parameters: 1429 . mat - the matrix 1430 1431 .keywords: matrix, compress 1432 @*/ 1433 int MatCompress(Mat mat) 1434 { 1435 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1436 if (mat->ops.compress) return (*mat->ops.compress)(mat); 1437 return 0; 1438 } 1439 /*@ 1440 MatSetOption - Sets a parameter option for a matrix. Some options 1441 may be specific to certain storage formats. Some options 1442 determine how values will be inserted (or added). Sorted, 1443 row-oriented input will generally assemble the fastest. The default 1444 is row-oriented, nonsorted input. 1445 1446 Input Parameters: 1447 . mat - the matrix 1448 . option - the option, one of the following: 1449 $ MAT_ROW_ORIENTED 1450 $ MAT_COLUMN_ORIENTED, 1451 $ MAT_ROWS_SORTED, 1452 $ MAT_COLUMNS_SORTED, 1453 $ MAT_NO_NEW_NONZERO_LOCATIONS, 1454 $ MAT_YES_NEW_NONZERO_LOCATIONS, 1455 $ MAT_SYMMETRIC, 1456 $ MAT_STRUCTURALLY_SYMMETRIC, 1457 $ MAT_NO_NEW_DIAGONALS, 1458 $ MAT_YES_NEW_DIAGONALS, 1459 $ and possibly others. 1460 1461 Notes: 1462 Some options are relevant only for particular matrix types and 1463 are thus ignored by others. Other options are not supported by 1464 certain matrix types and will generate an error message if set. 1465 1466 If using a Fortran 77 module to compute a matrix, one may need to 1467 use the column-oriented option (or convert to the row-oriented 1468 format). 1469 1470 MAT_NO_NEW_NONZERO_LOCATIONS indicates that any add or insertion 1471 that will generate a new entry in the nonzero structure is ignored. 1472 What this means is if memory is not allocated for this particular 1473 lot, then the insertion is ignored. For dense matrices, where 1474 the entire array is allocated, no entries are ever ignored. 1475 1476 .keywords: matrix, option, row-oriented, column-oriented, sorted, nonzero 1477 @*/ 1478 int MatSetOption(Mat mat,MatOption op) 1479 { 1480 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1481 if (mat->ops.setoption) return (*mat->ops.setoption)(mat,op); 1482 return 0; 1483 } 1484 1485 /*@ 1486 MatZeroEntries - Zeros all entries of a matrix. For sparse matrices 1487 this routine retains the old nonzero structure. 1488 1489 Input Parameters: 1490 . mat - the matrix 1491 1492 .keywords: matrix, zero, entries 1493 1494 .seealso: MatZeroRows() 1495 @*/ 1496 int MatZeroEntries(Mat mat) 1497 { 1498 int ierr; 1499 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1500 if (!mat->ops.zeroentries) SETERRQ(PETSC_ERR_SUP,"MatZeroEntries"); 1501 1502 PLogEventBegin(MAT_ZeroEntries,mat,0,0,0); 1503 ierr = (*mat->ops.zeroentries)(mat); CHKERRQ(ierr); 1504 PLogEventEnd(MAT_ZeroEntries,mat,0,0,0); 1505 return 0; 1506 } 1507 1508 /*@ 1509 MatZeroRows - Zeros all entries (except possibly the main diagonal) 1510 of a set of rows of a matrix. 1511 1512 Input Parameters: 1513 . mat - the matrix 1514 . is - index set of rows to remove 1515 . diag - pointer to value put in all diagonals of eliminated rows. 1516 Note that diag is not a pointer to an array, but merely a 1517 pointer to a single value. 1518 1519 Notes: 1520 For the AIJ matrix formats this removes the old nonzero structure, 1521 but does not release memory. For the dense and block diagonal 1522 formats this does not alter the nonzero structure. 1523 1524 The user can set a value in the diagonal entry (or for the AIJ and 1525 row formats can optionally remove the main diagonal entry from the 1526 nonzero structure as well, by passing a null pointer as the final 1527 argument). 1528 1529 .keywords: matrix, zero, rows, boundary conditions 1530 1531 .seealso: MatZeroEntries(), 1532 @*/ 1533 int MatZeroRows(Mat mat,IS is, Scalar *diag) 1534 { 1535 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1536 if (!mat->assembled) SETERRQ(1,"MatZeroRows:Not for unassembled matrix"); 1537 if (mat->ops.zerorows) return (*mat->ops.zerorows)(mat,is,diag); 1538 SETERRQ(PETSC_ERR_SUP,"MatZeroRows"); 1539 } 1540 1541 /*@ 1542 MatGetSize - Returns the numbers of rows and columns in a matrix. 1543 1544 Input Parameter: 1545 . mat - the matrix 1546 1547 Output Parameters: 1548 . m - the number of global rows 1549 . n - the number of global columns 1550 1551 .keywords: matrix, dimension, size, rows, columns, global, get 1552 1553 .seealso: MatGetLocalSize() 1554 @*/ 1555 int MatGetSize(Mat mat,int *m,int* n) 1556 { 1557 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1558 PetscValidIntPointer(m); 1559 PetscValidIntPointer(n); 1560 return (*mat->ops.getsize)(mat,m,n); 1561 } 1562 1563 /*@ 1564 MatGetLocalSize - Returns the number of rows and columns in a matrix 1565 stored locally. This information may be implementation dependent, so 1566 use with care. 1567 1568 Input Parameters: 1569 . mat - the matrix 1570 1571 Output Parameters: 1572 . m - the number of local rows 1573 . n - the number of local columns 1574 1575 .keywords: matrix, dimension, size, local, rows, columns, get 1576 1577 .seealso: MatGetSize() 1578 @*/ 1579 int MatGetLocalSize(Mat mat,int *m,int* n) 1580 { 1581 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1582 PetscValidIntPointer(m); 1583 PetscValidIntPointer(n); 1584 return (*mat->ops.getlocalsize)(mat,m,n); 1585 } 1586 1587 /*@ 1588 MatGetOwnershipRange - Returns the range of matrix rows owned by 1589 this processor, assuming that the matrix is laid out with the first 1590 n1 rows on the first processor, the next n2 rows on the second, etc. 1591 For certain parallel layouts this range may not be well-defined. 1592 1593 Input Parameters: 1594 . mat - the matrix 1595 1596 Output Parameters: 1597 . m - the first local row 1598 . n - one more then the last local row 1599 1600 .keywords: matrix, get, range, ownership 1601 @*/ 1602 int MatGetOwnershipRange(Mat mat,int *m,int* n) 1603 { 1604 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1605 PetscValidIntPointer(m); 1606 PetscValidIntPointer(n); 1607 if (mat->ops.getownershiprange) return (*mat->ops.getownershiprange)(mat,m,n); 1608 SETERRQ(PETSC_ERR_SUP,"MatGetOwnershipRange"); 1609 } 1610 1611 /*@ 1612 MatILUFactorSymbolic - Performs symbolic ILU factorization of a matrix. 1613 Uses levels of fill only, not drop tolerance. Use MatLUFactorNumeric() 1614 to complete the factorization. 1615 1616 Input Parameters: 1617 . mat - the matrix 1618 . row - row permutation 1619 . column - column permutation 1620 . fill - number of levels of fill 1621 . f - expected fill as ratio of the original number of nonzeros, 1622 for example 3.0; choosing this parameter well can result in 1623 more efficient use of time and space. 1624 1625 Output Parameters: 1626 . fact - new matrix that has been symbolically factored 1627 1628 Options Database Key: 1629 $ -mat_ilu_fill <f>, where f is the fill ratio 1630 1631 Notes: 1632 See the file $(PETSC_DIR)/Performace for additional information about 1633 choosing the fill factor for better efficiency. 1634 1635 .keywords: matrix, factor, incomplete, ILU, symbolic, fill 1636 1637 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric() 1638 @*/ 1639 int MatILUFactorSymbolic(Mat mat,IS row,IS col,double f,int fill,Mat *fact) 1640 { 1641 int ierr,flg; 1642 1643 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1644 if (fill < 0) SETERRQ(1,"MatILUFactorSymbolic:Levels of fill negative"); 1645 if (!fact) SETERRQ(1,"MatILUFactorSymbolic:Fact argument is missing"); 1646 if (!mat->ops.ilufactorsymbolic) SETERRQ(PETSC_ERR_SUP,"MatILUFactorSymbolic"); 1647 if (!mat->assembled) SETERRQ(1,"MatILUFactorSymbolic:Not for unassembled matrix"); 1648 1649 ierr = OptionsGetDouble(PETSC_NULL,"-mat_ilu_fill",&f,&flg); CHKERRQ(ierr); 1650 PLogEventBegin(MAT_ILUFactorSymbolic,mat,row,col,0); 1651 ierr = (*mat->ops.ilufactorsymbolic)(mat,row,col,f,fill,fact); CHKERRQ(ierr); 1652 PLogEventEnd(MAT_ILUFactorSymbolic,mat,row,col,0); 1653 return 0; 1654 } 1655 1656 /*@ 1657 MatIncompleteCholeskyFactorSymbolic - Performs symbolic incomplete 1658 Cholesky factorization for a symmetric matrix. Use 1659 MatCholeskyFactorNumeric() to complete the factorization. 1660 1661 Input Parameters: 1662 . mat - the matrix 1663 . perm - row and column permutation 1664 . fill - levels of fill 1665 . f - expected fill as ratio of original fill 1666 1667 Output Parameter: 1668 . fact - the factored matrix 1669 1670 Note: Currently only no-fill factorization is supported. 1671 1672 .keywords: matrix, factor, incomplete, ICC, Cholesky, symbolic, fill 1673 1674 .seealso: MatCholeskyFactorNumeric(), MatCholeskyFactor() 1675 @*/ 1676 int MatIncompleteCholeskyFactorSymbolic(Mat mat,IS perm,double f,int fill, 1677 Mat *fact) 1678 { 1679 int ierr; 1680 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1681 if (fill < 0) SETERRQ(1,"MatIncompleteCholeskyFactorSymbolic:Fill negative"); 1682 if (!fact) SETERRQ(1,"MatIncompleteCholeskyFactorSymbolic:Missing fact argument"); 1683 if (!mat->ops.incompletecholeskyfactorsymbolic) 1684 SETERRQ(PETSC_ERR_SUP,"MatIncompleteCholeskyFactorSymbolic"); 1685 if (!mat->assembled) 1686 SETERRQ(1,"MatIncompleteCholeskyFactorSymbolic:Not for unassembled matrix"); 1687 1688 PLogEventBegin(MAT_IncompleteCholeskyFactorSymbolic,mat,perm,0,0); 1689 ierr = (*mat->ops.incompletecholeskyfactorsymbolic)(mat,perm,f,fill,fact);CHKERRQ(ierr); 1690 PLogEventEnd(MAT_IncompleteCholeskyFactorSymbolic,mat,perm,0,0); 1691 return 0; 1692 } 1693 1694 /*@C 1695 MatGetArray - Returns a pointer to the element values in the matrix. 1696 This routine is implementation dependent, and may not even work for 1697 certain matrix types. You MUST call MatRestoreArray() when you no 1698 longer need to access the array. 1699 1700 Input Parameter: 1701 . mat - the matrix 1702 1703 Output Parameter: 1704 . v - the location of the values 1705 1706 Fortran Note: 1707 The Fortran interface is slightly different from that given below. 1708 See the Fortran chapter of the users manual and 1709 petsc/src/mat/examples for details. 1710 1711 .keywords: matrix, array, elements, values 1712 1713 .seeaols: MatRestoreArray() 1714 @*/ 1715 int MatGetArray(Mat mat,Scalar **v) 1716 { 1717 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1718 if (!v) SETERRQ(1,"MatGetArray:Bad input, array pointer location"); 1719 if (!mat->ops.getarray) SETERRQ(PETSC_ERR_SUP,"MatGetArray"); 1720 return (*mat->ops.getarray)(mat,v); 1721 } 1722 1723 /*@C 1724 MatRestoreArray - Restores the matrix after MatGetArray has been called. 1725 1726 Input Parameter: 1727 . mat - the matrix 1728 . v - the location of the values 1729 1730 Fortran Note: 1731 The Fortran interface is slightly different from that given below. 1732 See the users manual and petsc/src/mat/examples for details. 1733 1734 .keywords: matrix, array, elements, values, resrore 1735 1736 .seealso: MatGetArray() 1737 @*/ 1738 int MatRestoreArray(Mat mat,Scalar **v) 1739 { 1740 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1741 if (!v) SETERRQ(1,"MatRestoreArray:Bad input, array pointer location"); 1742 if (!mat->ops.restorearray) SETERRQ(PETSC_ERR_SUP,"MatResroreArray"); 1743 return (*mat->ops.restorearray)(mat,v); 1744 } 1745 1746 /*@C 1747 MatGetSubMatrices - Extracts several submatrices from a matrix. If submat 1748 points to an array of valid matrices, it may be reused. 1749 1750 Input Parameters: 1751 . mat - the matrix 1752 . irow, icol - index sets of rows and columns to extract 1753 . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 1754 1755 Output Parameter: 1756 . submat - the array of submatrices 1757 1758 Notes: 1759 The when finished using the submatrices, the user should 1760 destroy them with MatDestroySubMatrices(). 1761 1762 .keywords: matrix, get, submatrix, submatrices 1763 1764 .seealso: MatDestroyMatrices() 1765 @*/ 1766 int MatGetSubMatrices(Mat mat,int n,IS *irow,IS *icol,MatGetSubMatrixCall scall, 1767 Mat **submat) 1768 { 1769 int ierr; 1770 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1771 if (!mat->ops.getsubmatrices) SETERRQ(PETSC_ERR_SUP,"MatGetSubMatrices"); 1772 if (!mat->assembled) SETERRQ(1,"MatGetSubMatrices:Not for unassembled matrix"); 1773 1774 PLogEventBegin(MAT_GetSubMatrices,mat,0,0,0); 1775 ierr = (*mat->ops.getsubmatrices)(mat,n,irow,icol,scall,submat); CHKERRQ(ierr); 1776 PLogEventEnd(MAT_GetSubMatrices,mat,0,0,0); 1777 return 0; 1778 } 1779 1780 /*@C 1781 MatDestroyMatrices - Destroys a set of matrices obtained with MatGetSubMatrices(). 1782 1783 Input Parameters: 1784 . n - the number of local matrices 1785 . mat - the matrices 1786 1787 .keywords: matrix, destroy, submatrix, submatrices 1788 1789 .seealso: MatGetSubMatrices() 1790 @*/ 1791 int MatDestroyMatrices(int n,Mat **mat) 1792 { 1793 int ierr,i; 1794 1795 for ( i=0; i<n; i++ ) { 1796 ierr = MatDestroy((*mat)[i]); CHKERRQ(ierr); 1797 } 1798 return 0; 1799 } 1800 1801 /*@ 1802 MatIncreaseOverlap - Given a set of submatrices indicated by index sets, 1803 replaces the index by larger ones that represent submatrices with more 1804 overlap. 1805 1806 Input Parameters: 1807 . mat - the matrix 1808 . n - the number of index sets 1809 . is - the array of pointers to index sets 1810 . ov - the additional overlap requested 1811 1812 .keywords: matrix, overlap, Schwarz 1813 1814 .seealso: MatGetSubMatrices() 1815 @*/ 1816 int MatIncreaseOverlap(Mat mat,int n, IS *is,int ov) 1817 { 1818 int ierr; 1819 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1820 if (!mat->assembled) SETERRQ(1,"MatIncreaseOverlap:Not for unassembled matrix"); 1821 1822 if (ov == 0) return 0; 1823 if (!mat->ops.increaseoverlap) SETERRQ(PETSC_ERR_SUP,"MatIncreaseOverlap"); 1824 PLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0); 1825 ierr = (*mat->ops.increaseoverlap)(mat,n,is,ov); CHKERRQ(ierr); 1826 PLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0); 1827 return 0; 1828 } 1829 1830 /*@ 1831 MatPrintHelp - Prints all the options for the matrix. 1832 1833 Input Parameter: 1834 . mat - the matrix 1835 1836 Options Database Keys: 1837 $ -help, -h 1838 1839 .keywords: mat, help 1840 1841 .seealso: MatCreate(), MatCreateXXX() 1842 @*/ 1843 int MatPrintHelp(Mat mat) 1844 { 1845 static int called = 0; 1846 MPI_Comm comm = mat->comm; 1847 1848 if (!called) { 1849 PetscPrintf(comm,"General matrix options:\n"); 1850 PetscPrintf(comm," -mat_view_info : view basic matrix info during MatAssemblyEnd()\n"); 1851 PetscPrintf(comm," -mat_view_info_detailed : view detailed matrix info during MatAssemblyEnd()\n"); 1852 PetscPrintf(comm," -mat_view_draw : draw nonzero matrix structure during MatAssemblyEnd()\n"); 1853 PetscPrintf(comm," -draw_pause <sec> : set seconds of display pause\n"); 1854 PetscPrintf(comm," -display <name> : set alternate display\n"); 1855 called = 1; 1856 } 1857 if (mat->ops.printhelp) (*mat->ops.printhelp)(mat); 1858 return 0; 1859 } 1860 1861 /*@ 1862 MatGetBlockSize - Returns the block size. useful especially for 1863 MATBAIJ, MATBDIAG formats 1864 1865 1866 Input Parameter: 1867 . mat - the matrix 1868 1869 Output Parameter: 1870 . bs - block size 1871 1872 .keywords: mat, block, size 1873 1874 .seealso: MatCreateXXXBAIJ() 1875 @*/ 1876 int MatGetBlockSize(Mat mat,int *bs) 1877 { 1878 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1879 PetscValidIntPointer(bs); 1880 if (!mat->ops.getblocksize) SETERRQ(PETSC_ERR_SUP,"MatGetBlockSize"); 1881 return (*mat->ops.getblocksize)(mat,bs); 1882 } 1883