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