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