1 2 /* 3 This is where the abstract matrix operations are defined 4 */ 5 6 #include "src/mat/matimpl.h" /*I "petscmat.h" I*/ 7 #include "vecimpl.h" 8 9 /* Logging support */ 10 int MAT_COOKIE = 0; 11 int MATSNESMFCTX_COOKIE = 0; 12 int MAT_Mult = 0, MAT_MultMatrixFree = 0, MAT_Mults = 0, MAT_MultConstrained = 0, MAT_MultAdd = 0, MAT_MultTranspose = 0; 13 int MAT_MultTransposeConstrained = 0, MAT_MultTransposeAdd = 0, MAT_Solve = 0, MAT_Solves = 0, MAT_SolveAdd = 0, MAT_SolveTranspose = 0; 14 int MAT_SolveTransposeAdd = 0, MAT_Relax = 0, MAT_ForwardSolve = 0, MAT_BackwardSolve = 0, MAT_LUFactor = 0, MAT_LUFactorSymbolic = 0; 15 int MAT_LUFactorNumeric = 0, MAT_CholeskyFactor = 0, MAT_CholeskyFactorSymbolic = 0, MAT_CholeskyFactorNumeric = 0, MAT_ILUFactor = 0; 16 int MAT_ILUFactorSymbolic = 0, MAT_ICCFactorSymbolic = 0, MAT_Copy = 0, MAT_Convert = 0, MAT_Scale = 0, MAT_AssemblyBegin = 0; 17 int MAT_AssemblyEnd = 0, MAT_SetValues = 0, MAT_GetValues = 0, MAT_GetRow = 0, MAT_GetSubMatrices = 0, MAT_GetColoring = 0, MAT_GetOrdering = 0; 18 int MAT_IncreaseOverlap = 0, MAT_Partitioning = 0, MAT_ZeroEntries = 0, MAT_Load = 0, MAT_View = 0, MAT_AXPY = 0, MAT_FDColoringCreate = 0; 19 int MAT_FDColoringApply = 0,MAT_Transpose = 0,MAT_FDColoringFunction = 0; 20 21 /* nasty global values for MatSetValue() */ 22 int MatSetValue_Row = 0, MatSetValue_Column = 0; 23 PetscScalar MatSetValue_Value = 0.0; 24 25 #undef __FUNCT__ 26 #define __FUNCT__ "MatGetRow" 27 /*@C 28 MatGetRow - Gets a row of a matrix. You MUST call MatRestoreRow() 29 for each row that you get to ensure that your application does 30 not bleed memory. 31 32 Not Collective 33 34 Input Parameters: 35 + mat - the matrix 36 - row - the row to get 37 38 Output Parameters: 39 + ncols - if not NULL, the number of nonzeros in the row 40 . cols - if not NULL, the column numbers 41 - vals - if not NULL, the values 42 43 Notes: 44 This routine is provided for people who need to have direct access 45 to the structure of a matrix. We hope that we provide enough 46 high-level matrix routines that few users will need it. 47 48 MatGetRow() always returns 0-based column indices, regardless of 49 whether the internal representation is 0-based (default) or 1-based. 50 51 For better efficiency, set cols and/or vals to PETSC_NULL if you do 52 not wish to extract these quantities. 53 54 The user can only examine the values extracted with MatGetRow(); 55 the values cannot be altered. To change the matrix entries, one 56 must use MatSetValues(). 57 58 You can only have one call to MatGetRow() outstanding for a particular 59 matrix at a time, per processor. MatGetRow() can only obtained rows 60 associated with the given processor, it cannot get rows from the 61 other processors; for that we suggest using MatGetSubMatrices(), then 62 MatGetRow() on the submatrix. The row indix passed to MatGetRows() 63 is in the global number of rows. 64 65 Fortran Notes: 66 The calling sequence from Fortran is 67 .vb 68 MatGetRow(matrix,row,ncols,cols,values,ierr) 69 Mat matrix (input) 70 integer row (input) 71 integer ncols (output) 72 integer cols(maxcols) (output) 73 double precision (or double complex) values(maxcols) output 74 .ve 75 where maxcols >= maximum nonzeros in any row of the matrix. 76 77 Caution: 78 Do not try to change the contents of the output arrays (cols and vals). 79 In some cases, this may corrupt the matrix. 80 81 Level: advanced 82 83 Concepts: matrices^row access 84 85 .seealso: MatRestoreRow(), MatSetValues(), MatGetValues(), MatGetSubmatrices(), MatGetDiagonal() 86 @*/ 87 int MatGetRow(Mat mat,int row,int *ncols,int *cols[],PetscScalar *vals[]) 88 { 89 int incols,ierr; 90 91 PetscFunctionBegin; 92 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 93 PetscValidType(mat,1); 94 MatPreallocated(mat); 95 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 96 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 97 if (!mat->ops->getrow) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 98 ierr = PetscLogEventBegin(MAT_GetRow,mat,0,0,0);CHKERRQ(ierr); 99 ierr = (*mat->ops->getrow)(mat,row,&incols,cols,vals);CHKERRQ(ierr); 100 if (ncols) *ncols = incols; 101 ierr = PetscLogEventEnd(MAT_GetRow,mat,0,0,0);CHKERRQ(ierr); 102 PetscFunctionReturn(0); 103 } 104 105 #undef __FUNCT__ 106 #define __FUNCT__ "MatRestoreRow" 107 /*@C 108 MatRestoreRow - Frees any temporary space allocated by MatGetRow(). 109 110 Not Collective 111 112 Input Parameters: 113 + mat - the matrix 114 . row - the row to get 115 . ncols, cols - the number of nonzeros and their columns 116 - vals - if nonzero the column values 117 118 Notes: 119 This routine should be called after you have finished examining the entries. 120 121 Fortran Notes: 122 The calling sequence from Fortran is 123 .vb 124 MatRestoreRow(matrix,row,ncols,cols,values,ierr) 125 Mat matrix (input) 126 integer row (input) 127 integer ncols (output) 128 integer cols(maxcols) (output) 129 double precision (or double complex) values(maxcols) output 130 .ve 131 Where maxcols >= maximum nonzeros in any row of the matrix. 132 133 In Fortran MatRestoreRow() MUST be called after MatGetRow() 134 before another call to MatGetRow() can be made. 135 136 Level: advanced 137 138 .seealso: MatGetRow() 139 @*/ 140 int MatRestoreRow(Mat mat,int row,int *ncols,int *cols[],PetscScalar *vals[]) 141 { 142 int ierr; 143 144 PetscFunctionBegin; 145 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 146 PetscValidIntPointer(ncols,3); 147 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 148 if (!mat->ops->restorerow) PetscFunctionReturn(0); 149 ierr = (*mat->ops->restorerow)(mat,row,ncols,cols,vals);CHKERRQ(ierr); 150 ierr = PetscObjectIncreaseState((PetscObject)mat);CHKERRQ(ierr); 151 PetscFunctionReturn(0); 152 } 153 154 #undef __FUNCT__ 155 #define __FUNCT__ "MatView" 156 /*@C 157 MatView - Visualizes a matrix object. 158 159 Collective on Mat 160 161 Input Parameters: 162 + mat - the matrix 163 - viewer - visualization context 164 165 Notes: 166 The available visualization contexts include 167 + PETSC_VIEWER_STDOUT_SELF - standard output (default) 168 . PETSC_VIEWER_STDOUT_WORLD - synchronized standard 169 output where only the first processor opens 170 the file. All other processors send their 171 data to the first processor to print. 172 - PETSC_VIEWER_DRAW_WORLD - graphical display of nonzero structure 173 174 The user can open alternative visualization contexts with 175 + PetscViewerASCIIOpen() - Outputs matrix to a specified file 176 . PetscViewerBinaryOpen() - Outputs matrix in binary to a 177 specified file; corresponding input uses MatLoad() 178 . PetscViewerDrawOpen() - Outputs nonzero matrix structure to 179 an X window display 180 - PetscViewerSocketOpen() - Outputs matrix to Socket viewer. 181 Currently only the sequential dense and AIJ 182 matrix types support the Socket viewer. 183 184 The user can call PetscViewerSetFormat() to specify the output 185 format of ASCII printed objects (when using PETSC_VIEWER_STDOUT_SELF, 186 PETSC_VIEWER_STDOUT_WORLD and PetscViewerASCIIOpen). Available formats include 187 + PETSC_VIEWER_ASCII_DEFAULT - default, prints matrix contents 188 . PETSC_VIEWER_ASCII_MATLAB - prints matrix contents in Matlab format 189 . PETSC_VIEWER_ASCII_DENSE - prints entire matrix including zeros 190 . PETSC_VIEWER_ASCII_COMMON - prints matrix contents, using a sparse 191 format common among all matrix types 192 . PETSC_VIEWER_ASCII_IMPL - prints matrix contents, using an implementation-specific 193 format (which is in many cases the same as the default) 194 . PETSC_VIEWER_ASCII_INFO - prints basic information about the matrix 195 size and structure (not the matrix entries) 196 . PETSC_VIEWER_ASCII_INFO_DETAIL - prints more detailed information about 197 the matrix structure 198 199 Options Database Keys: 200 + -mat_view_info - Prints info on matrix at conclusion of MatEndAssembly() 201 . -mat_view_info_detailed - Prints more detailed info 202 . -mat_view - Prints matrix in ASCII format 203 . -mat_view_matlab - Prints matrix in Matlab format 204 . -mat_view_draw - PetscDraws nonzero structure of matrix, using MatView() and PetscDrawOpenX(). 205 . -display <name> - Sets display name (default is host) 206 . -draw_pause <sec> - Sets number of seconds to pause after display 207 . -mat_view_socket - Sends matrix to socket, can be accessed from Matlab (see users manual) 208 . -viewer_socket_machine <machine> 209 . -viewer_socket_port <port> 210 . -mat_view_binary - save matrix to file in binary format 211 - -viewer_binary_filename <name> 212 Level: beginner 213 214 Concepts: matrices^viewing 215 Concepts: matrices^plotting 216 Concepts: matrices^printing 217 218 .seealso: PetscViewerSetFormat(), PetscViewerASCIIOpen(), PetscViewerDrawOpen(), 219 PetscViewerSocketOpen(), PetscViewerBinaryOpen(), MatLoad() 220 @*/ 221 int MatView(Mat mat,PetscViewer viewer) 222 { 223 int ierr,rows,cols; 224 PetscTruth iascii; 225 char *cstr; 226 PetscViewerFormat format; 227 228 PetscFunctionBegin; 229 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 230 PetscValidType(mat,1); 231 MatPreallocated(mat); 232 if (!viewer) viewer = PETSC_VIEWER_STDOUT_(mat->comm); 233 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_COOKIE,2); 234 PetscCheckSameComm(mat,1,viewer,2); 235 if (!mat->assembled) SETERRQ(1,"Must call MatAssemblyBegin/End() before viewing matrix"); 236 237 ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr); 238 if (iascii) { 239 ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 240 if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) { 241 if (mat->prefix) { 242 ierr = PetscViewerASCIIPrintf(viewer,"Matrix Object:(%s)\n",mat->prefix);CHKERRQ(ierr); 243 } else { 244 ierr = PetscViewerASCIIPrintf(viewer,"Matrix Object:\n");CHKERRQ(ierr); 245 } 246 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 247 ierr = MatGetType(mat,&cstr);CHKERRQ(ierr); 248 ierr = MatGetSize(mat,&rows,&cols);CHKERRQ(ierr); 249 ierr = PetscViewerASCIIPrintf(viewer,"type=%s, rows=%d, cols=%d\n",cstr,rows,cols);CHKERRQ(ierr); 250 if (mat->ops->getinfo) { 251 MatInfo info; 252 ierr = MatGetInfo(mat,MAT_GLOBAL_SUM,&info);CHKERRQ(ierr); 253 ierr = PetscViewerASCIIPrintf(viewer,"total: nonzeros=%d, allocated nonzeros=%d\n", 254 (int)info.nz_used,(int)info.nz_allocated);CHKERRQ(ierr); 255 } 256 } 257 } 258 if (mat->ops->view) { 259 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 260 ierr = (*mat->ops->view)(mat,viewer);CHKERRQ(ierr); 261 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 262 } else if (!iascii) { 263 SETERRQ1(1,"Viewer type %s not supported",((PetscObject)viewer)->type_name); 264 } 265 if (iascii) { 266 ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 267 if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) { 268 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 269 } 270 } 271 PetscFunctionReturn(0); 272 } 273 274 #undef __FUNCT__ 275 #define __FUNCT__ "MatScaleSystem" 276 /*@C 277 MatScaleSystem - Scale a vector solution and right hand side to 278 match the scaling of a scaled matrix. 279 280 Collective on Mat 281 282 Input Parameter: 283 + mat - the matrix 284 . x - solution vector (or PETSC_NULL) 285 - b - right hand side vector (or PETSC_NULL) 286 287 288 Notes: 289 For AIJ, BAIJ, and BDiag matrix formats, the matrices are not 290 internally scaled, so this does nothing. For MPIROWBS it 291 permutes and diagonally scales. 292 293 The KSP methods automatically call this routine when required 294 (via PCPreSolve()) so it is rarely used directly. 295 296 Level: Developer 297 298 Concepts: matrices^scaling 299 300 .seealso: MatUseScaledForm(), MatUnScaleSystem() 301 @*/ 302 int MatScaleSystem(Mat mat,Vec x,Vec b) 303 { 304 int ierr; 305 306 PetscFunctionBegin; 307 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 308 PetscValidType(mat,1); 309 MatPreallocated(mat); 310 if (x) {PetscValidHeaderSpecific(x,VEC_COOKIE,2);PetscCheckSameComm(mat,1,x,2);} 311 if (b) {PetscValidHeaderSpecific(b,VEC_COOKIE,3);PetscCheckSameComm(mat,1,b,3);} 312 313 if (mat->ops->scalesystem) { 314 ierr = (*mat->ops->scalesystem)(mat,x,b);CHKERRQ(ierr); 315 } 316 PetscFunctionReturn(0); 317 } 318 319 #undef __FUNCT__ 320 #define __FUNCT__ "MatUnScaleSystem" 321 /*@C 322 MatUnScaleSystem - Unscales a vector solution and right hand side to 323 match the original scaling of a scaled matrix. 324 325 Collective on Mat 326 327 Input Parameter: 328 + mat - the matrix 329 . x - solution vector (or PETSC_NULL) 330 - b - right hand side vector (or PETSC_NULL) 331 332 333 Notes: 334 For AIJ, BAIJ, and BDiag matrix formats, the matrices are not 335 internally scaled, so this does nothing. For MPIROWBS it 336 permutes and diagonally scales. 337 338 The KSP methods automatically call this routine when required 339 (via PCPreSolve()) so it is rarely used directly. 340 341 Level: Developer 342 343 .seealso: MatUseScaledForm(), MatScaleSystem() 344 @*/ 345 int MatUnScaleSystem(Mat mat,Vec x,Vec b) 346 { 347 int ierr; 348 349 PetscFunctionBegin; 350 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 351 PetscValidType(mat,1); 352 MatPreallocated(mat); 353 if (x) {PetscValidHeaderSpecific(x,VEC_COOKIE,2);PetscCheckSameComm(mat,1,x,2);} 354 if (b) {PetscValidHeaderSpecific(b,VEC_COOKIE,3);PetscCheckSameComm(mat,1,b,3);} 355 if (mat->ops->unscalesystem) { 356 ierr = (*mat->ops->unscalesystem)(mat,x,b);CHKERRQ(ierr); 357 } 358 PetscFunctionReturn(0); 359 } 360 361 #undef __FUNCT__ 362 #define __FUNCT__ "MatUseScaledForm" 363 /*@C 364 MatUseScaledForm - For matrix storage formats that scale the 365 matrix (for example MPIRowBS matrices are diagonally scaled on 366 assembly) indicates matrix operations (MatMult() etc) are 367 applied using the scaled matrix. 368 369 Collective on Mat 370 371 Input Parameter: 372 + mat - the matrix 373 - scaled - PETSC_TRUE for applying the scaled, PETSC_FALSE for 374 applying the original matrix 375 376 Notes: 377 For scaled matrix formats, applying the original, unscaled matrix 378 will be slightly more expensive 379 380 Level: Developer 381 382 .seealso: MatScaleSystem(), MatUnScaleSystem() 383 @*/ 384 int MatUseScaledForm(Mat mat,PetscTruth scaled) 385 { 386 int ierr; 387 388 PetscFunctionBegin; 389 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 390 PetscValidType(mat,1); 391 MatPreallocated(mat); 392 if (mat->ops->usescaledform) { 393 ierr = (*mat->ops->usescaledform)(mat,scaled);CHKERRQ(ierr); 394 } 395 PetscFunctionReturn(0); 396 } 397 398 #undef __FUNCT__ 399 #define __FUNCT__ "MatDestroy" 400 /*@C 401 MatDestroy - Frees space taken by a matrix. 402 403 Collective on Mat 404 405 Input Parameter: 406 . A - the matrix 407 408 Level: beginner 409 410 @*/ 411 int MatDestroy(Mat A) 412 { 413 int ierr; 414 415 PetscFunctionBegin; 416 PetscValidHeaderSpecific(A,MAT_COOKIE,1); 417 PetscValidType(A,1); 418 MatPreallocated(A); 419 if (--A->refct > 0) PetscFunctionReturn(0); 420 421 /* if memory was published with AMS then destroy it */ 422 ierr = PetscObjectDepublish(A);CHKERRQ(ierr); 423 if (A->mapping) { 424 ierr = ISLocalToGlobalMappingDestroy(A->mapping);CHKERRQ(ierr); 425 } 426 if (A->bmapping) { 427 ierr = ISLocalToGlobalMappingDestroy(A->bmapping);CHKERRQ(ierr); 428 } 429 if (A->rmap) { 430 ierr = PetscMapDestroy(A->rmap);CHKERRQ(ierr); 431 } 432 if (A->cmap) { 433 ierr = PetscMapDestroy(A->cmap);CHKERRQ(ierr); 434 } 435 436 ierr = (*A->ops->destroy)(A);CHKERRQ(ierr); 437 PetscLogObjectDestroy(A); 438 PetscHeaderDestroy(A); 439 PetscFunctionReturn(0); 440 } 441 442 #undef __FUNCT__ 443 #define __FUNCT__ "MatValid" 444 /*@ 445 MatValid - Checks whether a matrix object is valid. 446 447 Collective on Mat 448 449 Input Parameter: 450 . m - the matrix to check 451 452 Output Parameter: 453 flg - flag indicating matrix status, either 454 PETSC_TRUE if matrix is valid, or PETSC_FALSE otherwise. 455 456 Level: developer 457 458 Concepts: matrices^validity 459 @*/ 460 int MatValid(Mat m,PetscTruth *flg) 461 { 462 PetscFunctionBegin; 463 PetscValidIntPointer(flg,1); 464 if (!m) *flg = PETSC_FALSE; 465 else if (m->cookie != MAT_COOKIE) *flg = PETSC_FALSE; 466 else *flg = PETSC_TRUE; 467 PetscFunctionReturn(0); 468 } 469 470 #undef __FUNCT__ 471 #define __FUNCT__ "MatSetValues" 472 /*@ 473 MatSetValues - Inserts or adds a block of values into a matrix. 474 These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd() 475 MUST be called after all calls to MatSetValues() have been completed. 476 477 Not Collective 478 479 Input Parameters: 480 + mat - the matrix 481 . v - a logically two-dimensional array of values 482 . m, idxm - the number of rows and their global indices 483 . n, idxn - the number of columns and their global indices 484 - addv - either ADD_VALUES or INSERT_VALUES, where 485 ADD_VALUES adds values to any existing entries, and 486 INSERT_VALUES replaces existing entries with new values 487 488 Notes: 489 By default the values, v, are row-oriented and unsorted. 490 See MatSetOption() for other options. 491 492 Calls to MatSetValues() with the INSERT_VALUES and ADD_VALUES 493 options cannot be mixed without intervening calls to the assembly 494 routines. 495 496 MatSetValues() uses 0-based row and column numbers in Fortran 497 as well as in C. 498 499 Negative indices may be passed in idxm and idxn, these rows and columns are 500 simply ignored. This allows easily inserting element stiffness matrices 501 with homogeneous Dirchlet boundary conditions that you don't want represented 502 in the matrix. 503 504 Efficiency Alert: 505 The routine MatSetValuesBlocked() may offer much better efficiency 506 for users of block sparse formats (MATSEQBAIJ and MATMPIBAIJ). 507 508 Level: beginner 509 510 Concepts: matrices^putting entries in 511 512 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(), 513 InsertMode, INSERT_VALUES, ADD_VALUES 514 @*/ 515 int MatSetValues(Mat mat,int m,const int idxm[],int n,const int idxn[],const PetscScalar v[],InsertMode addv) 516 { 517 int ierr; 518 519 PetscFunctionBegin; 520 if (!m || !n) PetscFunctionReturn(0); /* no values to insert */ 521 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 522 PetscValidType(mat,1); 523 MatPreallocated(mat); 524 PetscValidIntPointer(idxm,3); 525 PetscValidIntPointer(idxn,5); 526 PetscValidScalarPointer(v,6); 527 if (mat->insertmode == NOT_SET_VALUES) { 528 mat->insertmode = addv; 529 } 530 #if defined(PETSC_USE_BOPT_g) 531 else if (mat->insertmode != addv) { 532 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values"); 533 } 534 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 535 #endif 536 537 if (mat->assembled) { 538 mat->was_assembled = PETSC_TRUE; 539 mat->assembled = PETSC_FALSE; 540 } 541 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 542 if (!mat->ops->setvalues) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 543 ierr = (*mat->ops->setvalues)(mat,m,idxm,n,idxn,v,addv);CHKERRQ(ierr); 544 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 545 PetscFunctionReturn(0); 546 } 547 548 #undef __FUNCT__ 549 #define __FUNCT__ "MatSetValuesStencil" 550 /*@C 551 MatSetValuesStencil - Inserts or adds a block of values into a matrix. 552 Using structured grid indexing 553 554 Not Collective 555 556 Input Parameters: 557 + mat - the matrix 558 . v - a logically two-dimensional array of values 559 . m - number of rows being entered 560 . idxm - grid coordinates (and component number when dof > 1) for matrix rows being entered 561 . n - number of columns being entered 562 . idxn - grid coordinates (and component number when dof > 1) for matrix columns being entered 563 - addv - either ADD_VALUES or INSERT_VALUES, where 564 ADD_VALUES adds values to any existing entries, and 565 INSERT_VALUES replaces existing entries with new values 566 567 Notes: 568 By default the values, v, are row-oriented and unsorted. 569 See MatSetOption() for other options. 570 571 Calls to MatSetValuesStencil() with the INSERT_VALUES and ADD_VALUES 572 options cannot be mixed without intervening calls to the assembly 573 routines. 574 575 The grid coordinates are across the entire grid, not just the local portion 576 577 MatSetValuesStencil() uses 0-based row and column numbers in Fortran 578 as well as in C. 579 580 For setting/accessing vector values via array coordinates you can use the DAVecGetArray() routine 581 582 In order to use this routine you must either obtain the matrix with DAGetMatrix() 583 or call MatSetLocalToGlobalMapping() and MatSetStencil() first. 584 585 The columns and rows in the stencil passed in MUST be contained within the 586 ghost region of the given process as set with DACreateXXX() or MatSetStencil(). For example, 587 if you create a DA with an overlap of one grid level and on a particular process its first 588 local nonghost x logical coordinate is 6 (so its first ghost x logical coordinate is 5) the 589 first i index you can use in your column and row indices in MatSetStencil() is 5. 590 591 In Fortran idxm and idxn should be declared as 592 $ MatStencil idxm(4,m),idxn(4,n) 593 and the values inserted using 594 $ idxm(MatStencil_i,1) = i 595 $ idxm(MatStencil_j,1) = j 596 $ idxm(MatStencil_k,1) = k 597 $ idxm(MatStencil_c,1) = c 598 etc 599 600 Negative indices may be passed in idxm and idxn, these rows and columns are 601 simply ignored. This allows easily inserting element stiffness matrices 602 with homogeneous Dirchlet boundary conditions that you don't want represented 603 in the matrix. 604 605 Inspired by the structured grid interface to the HYPRE package 606 (http://www.llnl.gov/CASC/hypre) 607 608 Efficiency Alert: 609 The routine MatSetValuesBlockedStencil() may offer much better efficiency 610 for users of block sparse formats (MATSEQBAIJ and MATMPIBAIJ). 611 612 Level: beginner 613 614 Concepts: matrices^putting entries in 615 616 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal() 617 MatSetValues(), MatSetValuesBlockedStencil(), MatSetStencil(), DAGetMatrix(), DAVecGetArray(), MatStencil 618 @*/ 619 int MatSetValuesStencil(Mat mat,int m,const MatStencil idxm[],int n,const MatStencil idxn[],const PetscScalar v[],InsertMode addv) 620 { 621 int j,i,ierr,jdxm[128],jdxn[256],dim = mat->stencil.dim,*dims = mat->stencil.dims+1,tmp; 622 int *starts = mat->stencil.starts,*dxm = (int*)idxm,*dxn = (int*)idxn,sdim = dim - (1 - (int)mat->stencil.noc); 623 624 PetscFunctionBegin; 625 if (!m || !n) PetscFunctionReturn(0); /* no values to insert */ 626 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 627 PetscValidType(mat,1); 628 PetscValidIntPointer(idxm,3); 629 PetscValidIntPointer(idxn,5); 630 PetscValidScalarPointer(v,6); 631 632 if (m > 128) SETERRQ1(1,"Can only set 128 rows at a time; trying to set %d",m); 633 if (n > 128) SETERRQ1(1,"Can only set 256 columns at a time; trying to set %d",n); 634 635 for (i=0; i<m; i++) { 636 for (j=0; j<3-sdim; j++) dxm++; 637 if (*dxm++ < 0) tmp = PETSC_MIN_INT; 638 else tmp = dxm[-1] - starts[0]; 639 for (j=0; j<dim-1; j++) { 640 if (*dxm++ < 0 || tmp < 0) tmp = PETSC_MIN_INT; 641 else tmp = tmp*dims[j] + dxm[-1] - starts[j+1]; 642 } 643 if (mat->stencil.noc) dxm++; 644 jdxm[i] = tmp; 645 } 646 for (i=0; i<n; i++) { 647 for (j=0; j<3-sdim; j++) dxn++; 648 if (*dxn++ < 0) tmp = PETSC_MIN_INT; 649 else tmp = dxn[-1] - starts[0]; 650 for (j=0; j<dim-1; j++) { 651 if (*dxn++ < 0 || tmp < 0) tmp = PETSC_MIN_INT; 652 else tmp = tmp*dims[j] + dxn[-1] - starts[j+1]; 653 } 654 if (mat->stencil.noc) dxn++; 655 jdxn[i] = tmp; 656 } 657 ierr = MatSetValuesLocal(mat,m,jdxm,n,jdxn,v,addv);CHKERRQ(ierr); 658 PetscFunctionReturn(0); 659 } 660 661 #undef __FUNCT__ 662 #define __FUNCT__ "MatSetValuesBlockedStencil" 663 /*@C 664 MatSetValuesBlockedStencil - Inserts or adds a block of values into a matrix. 665 Using structured grid indexing 666 667 Not Collective 668 669 Input Parameters: 670 + mat - the matrix 671 . v - a logically two-dimensional array of values 672 . m - number of rows being entered 673 . idxm - grid coordinates for matrix rows being entered 674 . n - number of columns being entered 675 . idxn - grid coordinates for matrix columns being entered 676 - addv - either ADD_VALUES or INSERT_VALUES, where 677 ADD_VALUES adds values to any existing entries, and 678 INSERT_VALUES replaces existing entries with new values 679 680 Notes: 681 By default the values, v, are row-oriented and unsorted. 682 See MatSetOption() for other options. 683 684 Calls to MatSetValuesBlockedStencil() with the INSERT_VALUES and ADD_VALUES 685 options cannot be mixed without intervening calls to the assembly 686 routines. 687 688 The grid coordinates are across the entire grid, not just the local portion 689 690 MatSetValuesBlockedStencil() uses 0-based row and column numbers in Fortran 691 as well as in C. 692 693 For setting/accessing vector values via array coordinates you can use the DAVecGetArray() routine 694 695 In order to use this routine you must either obtain the matrix with DAGetMatrix() 696 or call MatSetLocalToGlobalMapping() and MatSetStencil() first. 697 698 The columns and rows in the stencil passed in MUST be contained within the 699 ghost region of the given process as set with DACreateXXX() or MatSetStencil(). For example, 700 if you create a DA with an overlap of one grid level and on a particular process its first 701 local nonghost x logical coordinate is 6 (so its first ghost x logical coordinate is 5) the 702 first i index you can use in your column and row indices in MatSetStencil() is 5. 703 704 In Fortran idxm and idxn should be declared as 705 $ MatStencil idxm(4,m),idxn(4,n) 706 and the values inserted using 707 $ idxm(MatStencil_i,1) = i 708 $ idxm(MatStencil_j,1) = j 709 $ idxm(MatStencil_k,1) = k 710 etc 711 712 Negative indices may be passed in idxm and idxn, these rows and columns are 713 simply ignored. This allows easily inserting element stiffness matrices 714 with homogeneous Dirchlet boundary conditions that you don't want represented 715 in the matrix. 716 717 Inspired by the structured grid interface to the HYPRE package 718 (http://www.llnl.gov/CASC/hypre) 719 720 Level: beginner 721 722 Concepts: matrices^putting entries in 723 724 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal() 725 MatSetValues(), MatSetValuesStencil(), MatSetStencil(), DAGetMatrix(), DAVecGetArray(), MatStencil 726 @*/ 727 int MatSetValuesBlockedStencil(Mat mat,int m,const MatStencil idxm[],int n,const MatStencil idxn[],const PetscScalar v[],InsertMode addv) 728 { 729 int j,i,ierr,jdxm[128],jdxn[256],dim = mat->stencil.dim,*dims = mat->stencil.dims+1,tmp; 730 int *starts = mat->stencil.starts,*dxm = (int*)idxm,*dxn = (int*)idxn,sdim = dim - (1 - (int)mat->stencil.noc); 731 732 PetscFunctionBegin; 733 if (!m || !n) PetscFunctionReturn(0); /* no values to insert */ 734 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 735 PetscValidType(mat,1); 736 PetscValidIntPointer(idxm,3); 737 PetscValidIntPointer(idxn,5); 738 PetscValidScalarPointer(v,6); 739 740 if (m > 128) SETERRQ1(1,"Can only set 128 rows at a time; trying to set %d",m); 741 if (n > 128) SETERRQ1(1,"Can only set 256 columns at a time; trying to set %d",n); 742 743 for (i=0; i<m; i++) { 744 for (j=0; j<3-sdim; j++) dxm++; 745 tmp = *dxm++ - starts[0]; 746 for (j=0; j<sdim-1; j++) { 747 tmp = tmp*dims[j] + *dxm++ - starts[j+1]; 748 } 749 dxm++; 750 jdxm[i] = tmp; 751 } 752 for (i=0; i<n; i++) { 753 for (j=0; j<3-sdim; j++) dxn++; 754 tmp = *dxn++ - starts[0]; 755 for (j=0; j<sdim-1; j++) { 756 tmp = tmp*dims[j] + *dxn++ - starts[j+1]; 757 } 758 dxn++; 759 jdxn[i] = tmp; 760 } 761 ierr = MatSetValuesBlockedLocal(mat,m,jdxm,n,jdxn,v,addv);CHKERRQ(ierr); 762 PetscFunctionReturn(0); 763 } 764 765 #undef __FUNCT__ 766 #define __FUNCT__ "MatSetStencil" 767 /*@ 768 MatSetStencil - Sets the grid information for setting values into a matrix via 769 MatSetValuesStencil() 770 771 Not Collective 772 773 Input Parameters: 774 + mat - the matrix 775 . dim - dimension of the grid 1, 2, or 3 776 . dims - number of grid points in x, y, and z direction, including ghost points on your processor 777 . starts - starting point of ghost nodes on your processor in x, y, and z direction 778 - dof - number of degrees of freedom per node 779 780 781 Inspired by the structured grid interface to the HYPRE package 782 (www.llnl.gov/CASC/hyper) 783 784 For matrices generated with DAGetMatrix() this routine is automatically called and so not needed by the 785 user. 786 787 Level: beginner 788 789 Concepts: matrices^putting entries in 790 791 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal() 792 MatSetValues(), MatSetValuesBlockedStencil(), MatSetValuesStencil() 793 @*/ 794 int MatSetStencil(Mat mat,int dim,const int dims[],const int starts[],int dof) 795 { 796 int i; 797 798 PetscFunctionBegin; 799 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 800 PetscValidIntPointer(dims,3); 801 PetscValidIntPointer(starts,4); 802 803 mat->stencil.dim = dim + (dof > 1); 804 for (i=0; i<dim; i++) { 805 mat->stencil.dims[i] = dims[dim-i-1]; /* copy the values in backwards */ 806 mat->stencil.starts[i] = starts[dim-i-1]; 807 } 808 mat->stencil.dims[dim] = dof; 809 mat->stencil.starts[dim] = 0; 810 mat->stencil.noc = (PetscTruth)(dof == 1); 811 PetscFunctionReturn(0); 812 } 813 814 #undef __FUNCT__ 815 #define __FUNCT__ "MatSetValuesBlocked" 816 /*@ 817 MatSetValuesBlocked - Inserts or adds a block of values into a matrix. 818 819 Not Collective 820 821 Input Parameters: 822 + mat - the matrix 823 . v - a logically two-dimensional array of values 824 . m, idxm - the number of block rows and their global block indices 825 . n, idxn - the number of block columns and their global block indices 826 - addv - either ADD_VALUES or INSERT_VALUES, where 827 ADD_VALUES adds values to any existing entries, and 828 INSERT_VALUES replaces existing entries with new values 829 830 Notes: 831 By default the values, v, are row-oriented and unsorted. So the layout of 832 v is the same as for MatSetValues(). See MatSetOption() for other options. 833 834 Calls to MatSetValuesBlocked() with the INSERT_VALUES and ADD_VALUES 835 options cannot be mixed without intervening calls to the assembly 836 routines. 837 838 MatSetValuesBlocked() uses 0-based row and column numbers in Fortran 839 as well as in C. 840 841 Negative indices may be passed in idxm and idxn, these rows and columns are 842 simply ignored. This allows easily inserting element stiffness matrices 843 with homogeneous Dirchlet boundary conditions that you don't want represented 844 in the matrix. 845 846 Each time an entry is set within a sparse matrix via MatSetValues(), 847 internal searching must be done to determine where to place the the 848 data in the matrix storage space. By instead inserting blocks of 849 entries via MatSetValuesBlocked(), the overhead of matrix assembly is 850 reduced. 851 852 Restrictions: 853 MatSetValuesBlocked() is currently supported only for the BAIJ and SBAIJ formats 854 855 Level: intermediate 856 857 Concepts: matrices^putting entries in blocked 858 859 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesBlockedLocal() 860 @*/ 861 int MatSetValuesBlocked(Mat mat,int m,const int idxm[],int n,const int idxn[],const PetscScalar v[],InsertMode addv) 862 { 863 int ierr; 864 865 PetscFunctionBegin; 866 if (!m || !n) PetscFunctionReturn(0); /* no values to insert */ 867 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 868 PetscValidType(mat,1); 869 MatPreallocated(mat); 870 PetscValidIntPointer(idxm,3); 871 PetscValidIntPointer(idxn,5); 872 PetscValidScalarPointer(v,6); 873 if (mat->insertmode == NOT_SET_VALUES) { 874 mat->insertmode = addv; 875 } 876 #if defined(PETSC_USE_BOPT_g) 877 else if (mat->insertmode != addv) { 878 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values"); 879 } 880 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 881 #endif 882 883 if (mat->assembled) { 884 mat->was_assembled = PETSC_TRUE; 885 mat->assembled = PETSC_FALSE; 886 } 887 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 888 if (!mat->ops->setvaluesblocked) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 889 ierr = (*mat->ops->setvaluesblocked)(mat,m,idxm,n,idxn,v,addv);CHKERRQ(ierr); 890 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 891 PetscFunctionReturn(0); 892 } 893 894 #undef __FUNCT__ 895 #define __FUNCT__ "MatGetValues" 896 /*@ 897 MatGetValues - Gets a block of values from a matrix. 898 899 Not Collective; currently only returns a local block 900 901 Input Parameters: 902 + mat - the matrix 903 . v - a logically two-dimensional array for storing the values 904 . m, idxm - the number of rows and their global indices 905 - n, idxn - the number of columns and their global indices 906 907 Notes: 908 The user must allocate space (m*n PetscScalars) for the values, v. 909 The values, v, are then returned in a row-oriented format, 910 analogous to that used by default in MatSetValues(). 911 912 MatGetValues() uses 0-based row and column numbers in 913 Fortran as well as in C. 914 915 MatGetValues() requires that the matrix has been assembled 916 with MatAssemblyBegin()/MatAssemblyEnd(). Thus, calls to 917 MatSetValues() and MatGetValues() CANNOT be made in succession 918 without intermediate matrix assembly. 919 920 Level: advanced 921 922 Concepts: matrices^accessing values 923 924 .seealso: MatGetRow(), MatGetSubmatrices(), MatSetValues() 925 @*/ 926 int MatGetValues(Mat mat,int m,const int idxm[],int n,const int idxn[],PetscScalar v[]) 927 { 928 int ierr; 929 930 PetscFunctionBegin; 931 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 932 PetscValidType(mat,1); 933 MatPreallocated(mat); 934 PetscValidIntPointer(idxm,3); 935 PetscValidIntPointer(idxn,5); 936 PetscValidScalarPointer(v,6); 937 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 938 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 939 if (!mat->ops->getvalues) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 940 941 ierr = PetscLogEventBegin(MAT_GetValues,mat,0,0,0);CHKERRQ(ierr); 942 ierr = (*mat->ops->getvalues)(mat,m,idxm,n,idxn,v);CHKERRQ(ierr); 943 ierr = PetscLogEventEnd(MAT_GetValues,mat,0,0,0);CHKERRQ(ierr); 944 PetscFunctionReturn(0); 945 } 946 947 #undef __FUNCT__ 948 #define __FUNCT__ "MatSetLocalToGlobalMapping" 949 /*@ 950 MatSetLocalToGlobalMapping - Sets a local-to-global numbering for use by 951 the routine MatSetValuesLocal() to allow users to insert matrix entries 952 using a local (per-processor) numbering. 953 954 Not Collective 955 956 Input Parameters: 957 + x - the matrix 958 - mapping - mapping created with ISLocalToGlobalMappingCreate() 959 or ISLocalToGlobalMappingCreateIS() 960 961 Level: intermediate 962 963 Concepts: matrices^local to global mapping 964 Concepts: local to global mapping^for matrices 965 966 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesLocal() 967 @*/ 968 int MatSetLocalToGlobalMapping(Mat x,ISLocalToGlobalMapping mapping) 969 { 970 int ierr; 971 PetscFunctionBegin; 972 PetscValidHeaderSpecific(x,MAT_COOKIE,1); 973 PetscValidType(x,1); 974 MatPreallocated(x); 975 PetscValidHeaderSpecific(mapping,IS_LTOGM_COOKIE,2); 976 if (x->mapping) { 977 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Mapping already set for matrix"); 978 } 979 980 if (x->ops->setlocaltoglobalmapping) { 981 ierr = (*x->ops->setlocaltoglobalmapping)(x,mapping);CHKERRQ(ierr); 982 } else { 983 x->mapping = mapping; 984 ierr = PetscObjectReference((PetscObject)mapping);CHKERRQ(ierr); 985 } 986 PetscFunctionReturn(0); 987 } 988 989 #undef __FUNCT__ 990 #define __FUNCT__ "MatSetLocalToGlobalMappingBlock" 991 /*@ 992 MatSetLocalToGlobalMappingBlock - Sets a local-to-global numbering for use 993 by the routine MatSetValuesBlockedLocal() to allow users to insert matrix 994 entries using a local (per-processor) numbering. 995 996 Not Collective 997 998 Input Parameters: 999 + x - the matrix 1000 - mapping - mapping created with ISLocalToGlobalMappingCreate() or 1001 ISLocalToGlobalMappingCreateIS() 1002 1003 Level: intermediate 1004 1005 Concepts: matrices^local to global mapping blocked 1006 Concepts: local to global mapping^for matrices, blocked 1007 1008 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesBlockedLocal(), 1009 MatSetValuesBlocked(), MatSetValuesLocal() 1010 @*/ 1011 int MatSetLocalToGlobalMappingBlock(Mat x,ISLocalToGlobalMapping mapping) 1012 { 1013 int ierr; 1014 PetscFunctionBegin; 1015 PetscValidHeaderSpecific(x,MAT_COOKIE,1); 1016 PetscValidType(x,1); 1017 MatPreallocated(x); 1018 PetscValidHeaderSpecific(mapping,IS_LTOGM_COOKIE,2); 1019 if (x->bmapping) { 1020 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Mapping already set for matrix"); 1021 } 1022 1023 x->bmapping = mapping; 1024 ierr = PetscObjectReference((PetscObject)mapping);CHKERRQ(ierr); 1025 PetscFunctionReturn(0); 1026 } 1027 1028 #undef __FUNCT__ 1029 #define __FUNCT__ "MatSetValuesLocal" 1030 /*@ 1031 MatSetValuesLocal - Inserts or adds values into certain locations of a matrix, 1032 using a local ordering of the nodes. 1033 1034 Not Collective 1035 1036 Input Parameters: 1037 + x - the matrix 1038 . nrow, irow - number of rows and their local indices 1039 . ncol, icol - number of columns and their local indices 1040 . y - a logically two-dimensional array of values 1041 - addv - either INSERT_VALUES or ADD_VALUES, where 1042 ADD_VALUES adds values to any existing entries, and 1043 INSERT_VALUES replaces existing entries with new values 1044 1045 Notes: 1046 Before calling MatSetValuesLocal(), the user must first set the 1047 local-to-global mapping by calling MatSetLocalToGlobalMapping(). 1048 1049 Calls to MatSetValuesLocal() with the INSERT_VALUES and ADD_VALUES 1050 options cannot be mixed without intervening calls to the assembly 1051 routines. 1052 1053 These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd() 1054 MUST be called after all calls to MatSetValuesLocal() have been completed. 1055 1056 Level: intermediate 1057 1058 Concepts: matrices^putting entries in with local numbering 1059 1060 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetLocalToGlobalMapping(), 1061 MatSetValueLocal() 1062 @*/ 1063 int MatSetValuesLocal(Mat mat,int nrow,const int irow[],int ncol,const int icol[],const PetscScalar y[],InsertMode addv) 1064 { 1065 int ierr,irowm[2048],icolm[2048]; 1066 1067 PetscFunctionBegin; 1068 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 1069 PetscValidType(mat,1); 1070 MatPreallocated(mat); 1071 PetscValidIntPointer(irow,3); 1072 PetscValidIntPointer(icol,5); 1073 PetscValidScalarPointer(y,6); 1074 1075 if (mat->insertmode == NOT_SET_VALUES) { 1076 mat->insertmode = addv; 1077 } 1078 #if defined(PETSC_USE_BOPT_g) 1079 else if (mat->insertmode != addv) { 1080 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values"); 1081 } 1082 if (!mat->ops->setvalueslocal && (nrow > 2048 || ncol > 2048)) { 1083 SETERRQ2(PETSC_ERR_SUP,"Number column/row indices must be <= 2048: are %d %d",nrow,ncol); 1084 } 1085 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1086 #endif 1087 1088 if (mat->assembled) { 1089 mat->was_assembled = PETSC_TRUE; 1090 mat->assembled = PETSC_FALSE; 1091 } 1092 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 1093 if (!mat->ops->setvalueslocal) { 1094 ierr = ISLocalToGlobalMappingApply(mat->mapping,nrow,irow,irowm);CHKERRQ(ierr); 1095 ierr = ISLocalToGlobalMappingApply(mat->mapping,ncol,icol,icolm);CHKERRQ(ierr); 1096 ierr = (*mat->ops->setvalues)(mat,nrow,irowm,ncol,icolm,y,addv);CHKERRQ(ierr); 1097 } else { 1098 ierr = (*mat->ops->setvalueslocal)(mat,nrow,irow,ncol,icol,y,addv);CHKERRQ(ierr); 1099 } 1100 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 1101 PetscFunctionReturn(0); 1102 } 1103 1104 #undef __FUNCT__ 1105 #define __FUNCT__ "MatSetValuesBlockedLocal" 1106 /*@ 1107 MatSetValuesBlockedLocal - Inserts or adds values into certain locations of a matrix, 1108 using a local ordering of the nodes a block at a time. 1109 1110 Not Collective 1111 1112 Input Parameters: 1113 + x - the matrix 1114 . nrow, irow - number of rows and their local indices 1115 . ncol, icol - number of columns and their local indices 1116 . y - a logically two-dimensional array of values 1117 - addv - either INSERT_VALUES or ADD_VALUES, where 1118 ADD_VALUES adds values to any existing entries, and 1119 INSERT_VALUES replaces existing entries with new values 1120 1121 Notes: 1122 Before calling MatSetValuesBlockedLocal(), the user must first set the 1123 local-to-global mapping by calling MatSetLocalToGlobalMappingBlock(), 1124 where the mapping MUST be set for matrix blocks, not for matrix elements. 1125 1126 Calls to MatSetValuesBlockedLocal() with the INSERT_VALUES and ADD_VALUES 1127 options cannot be mixed without intervening calls to the assembly 1128 routines. 1129 1130 These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd() 1131 MUST be called after all calls to MatSetValuesBlockedLocal() have been completed. 1132 1133 Level: intermediate 1134 1135 Concepts: matrices^putting blocked values in with local numbering 1136 1137 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesLocal(), MatSetLocalToGlobalMappingBlock(), MatSetValuesBlocked() 1138 @*/ 1139 int MatSetValuesBlockedLocal(Mat mat,int nrow,const int irow[],int ncol,const int icol[],const PetscScalar y[],InsertMode addv) 1140 { 1141 int ierr,irowm[2048],icolm[2048]; 1142 1143 PetscFunctionBegin; 1144 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 1145 PetscValidType(mat,1); 1146 MatPreallocated(mat); 1147 PetscValidIntPointer(irow,3); 1148 PetscValidIntPointer(icol,5); 1149 PetscValidScalarPointer(y,6); 1150 if (mat->insertmode == NOT_SET_VALUES) { 1151 mat->insertmode = addv; 1152 } 1153 #if defined(PETSC_USE_BOPT_g) 1154 else if (mat->insertmode != addv) { 1155 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values"); 1156 } 1157 if (!mat->bmapping) { 1158 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Local to global never set with MatSetLocalToGlobalMappingBlock()"); 1159 } 1160 if (nrow > 2048 || ncol > 2048) { 1161 SETERRQ2(PETSC_ERR_SUP,"Number column/row indices must be <= 2048: are %d %d",nrow,ncol); 1162 } 1163 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1164 #endif 1165 1166 if (mat->assembled) { 1167 mat->was_assembled = PETSC_TRUE; 1168 mat->assembled = PETSC_FALSE; 1169 } 1170 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 1171 ierr = ISLocalToGlobalMappingApply(mat->bmapping,nrow,irow,irowm);CHKERRQ(ierr); 1172 ierr = ISLocalToGlobalMappingApply(mat->bmapping,ncol,icol,icolm);CHKERRQ(ierr); 1173 ierr = (*mat->ops->setvaluesblocked)(mat,nrow,irowm,ncol,icolm,y,addv);CHKERRQ(ierr); 1174 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 1175 PetscFunctionReturn(0); 1176 } 1177 1178 /* --------------------------------------------------------*/ 1179 #undef __FUNCT__ 1180 #define __FUNCT__ "MatMult" 1181 /*@ 1182 MatMult - Computes the matrix-vector product, y = Ax. 1183 1184 Collective on Mat and Vec 1185 1186 Input Parameters: 1187 + mat - the matrix 1188 - x - the vector to be multiplied 1189 1190 Output Parameters: 1191 . y - the result 1192 1193 Notes: 1194 The vectors x and y cannot be the same. I.e., one cannot 1195 call MatMult(A,y,y). 1196 1197 Level: beginner 1198 1199 Concepts: matrix-vector product 1200 1201 .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd() 1202 @*/ 1203 int MatMult(Mat mat,Vec x,Vec y) 1204 { 1205 int ierr; 1206 1207 PetscFunctionBegin; 1208 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 1209 PetscValidType(mat,1); 1210 MatPreallocated(mat); 1211 PetscValidHeaderSpecific(x,VEC_COOKIE,2); 1212 PetscValidHeaderSpecific(y,VEC_COOKIE,3); 1213 1214 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1215 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1216 if (x == y) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors"); 1217 #ifndef PETSC_HAVE_CONSTRAINTS 1218 if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N); 1219 if (mat->M != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %d %d",mat->M,y->N); 1220 if (mat->m != y->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: local dim %d %d",mat->m,y->n); 1221 #endif 1222 1223 if (mat->nullsp) { 1224 ierr = MatNullSpaceRemove(mat->nullsp,x,&x);CHKERRQ(ierr); 1225 } 1226 1227 ierr = PetscLogEventBegin(MAT_Mult,mat,x,y,0);CHKERRQ(ierr); 1228 ierr = (*mat->ops->mult)(mat,x,y);CHKERRQ(ierr); 1229 ierr = PetscLogEventEnd(MAT_Mult,mat,x,y,0);CHKERRQ(ierr); 1230 1231 if (mat->nullsp) { 1232 ierr = MatNullSpaceRemove(mat->nullsp,y,PETSC_NULL);CHKERRQ(ierr); 1233 } 1234 ierr = PetscObjectIncreaseState((PetscObject)y);CHKERRQ(ierr); 1235 PetscFunctionReturn(0); 1236 } 1237 1238 #undef __FUNCT__ 1239 #define __FUNCT__ "MatMultTranspose" 1240 /*@ 1241 MatMultTranspose - Computes matrix transpose times a vector. 1242 1243 Collective on Mat and Vec 1244 1245 Input Parameters: 1246 + mat - the matrix 1247 - x - the vector to be multilplied 1248 1249 Output Parameters: 1250 . y - the result 1251 1252 Notes: 1253 The vectors x and y cannot be the same. I.e., one cannot 1254 call MatMultTranspose(A,y,y). 1255 1256 Level: beginner 1257 1258 Concepts: matrix vector product^transpose 1259 1260 .seealso: MatMult(), MatMultAdd(), MatMultTransposeAdd() 1261 @*/ 1262 int MatMultTranspose(Mat mat,Vec x,Vec y) 1263 { 1264 int ierr; 1265 PetscTruth flg1, flg2; 1266 1267 PetscFunctionBegin; 1268 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 1269 PetscValidType(mat,1); 1270 MatPreallocated(mat); 1271 PetscValidHeaderSpecific(x,VEC_COOKIE,2); 1272 PetscValidHeaderSpecific(y,VEC_COOKIE,3); 1273 1274 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1275 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1276 if (x == y) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors"); 1277 #ifndef PETSC_HAVE_CONSTRAINTS 1278 if (mat->M != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->M,x->N); 1279 if (mat->N != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %d %d",mat->N,y->N); 1280 #endif 1281 1282 if (!mat->ops->multtranspose) SETERRQ(PETSC_ERR_SUP, "Operation not supported"); 1283 ierr = PetscLogEventBegin(MAT_MultTranspose,mat,x,y,0);CHKERRQ(ierr); 1284 if (!mat->ops->multtranspose) SETERRQ(PETSC_ERR_SUP,"This matrix type does not have a multiply tranpose defined"); 1285 1286 ierr = PetscTypeCompare((PetscObject)mat,MATSEQSBAIJ,&flg1); 1287 ierr = PetscTypeCompare((PetscObject)mat,MATMPISBAIJ,&flg2); 1288 if (flg1 || flg2) { /* mat is in sbaij format */ 1289 ierr = (*mat->ops->mult)(mat,x,y);CHKERRQ(ierr); 1290 } else { 1291 ierr = (*mat->ops->multtranspose)(mat,x,y);CHKERRQ(ierr); 1292 } 1293 ierr = PetscLogEventEnd(MAT_MultTranspose,mat,x,y,0);CHKERRQ(ierr); 1294 ierr = PetscObjectIncreaseState((PetscObject)y);CHKERRQ(ierr); 1295 PetscFunctionReturn(0); 1296 } 1297 1298 #undef __FUNCT__ 1299 #define __FUNCT__ "MatMultAdd" 1300 /*@ 1301 MatMultAdd - Computes v3 = v2 + A * v1. 1302 1303 Collective on Mat and Vec 1304 1305 Input Parameters: 1306 + mat - the matrix 1307 - v1, v2 - the vectors 1308 1309 Output Parameters: 1310 . v3 - the result 1311 1312 Notes: 1313 The vectors v1 and v3 cannot be the same. I.e., one cannot 1314 call MatMultAdd(A,v1,v2,v1). 1315 1316 Level: beginner 1317 1318 Concepts: matrix vector product^addition 1319 1320 .seealso: MatMultTranspose(), MatMult(), MatMultTransposeAdd() 1321 @*/ 1322 int MatMultAdd(Mat mat,Vec v1,Vec v2,Vec v3) 1323 { 1324 int ierr; 1325 1326 PetscFunctionBegin; 1327 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 1328 PetscValidType(mat,1); 1329 MatPreallocated(mat); 1330 PetscValidHeaderSpecific(v1,VEC_COOKIE,2); 1331 PetscValidHeaderSpecific(v2,VEC_COOKIE,3); 1332 PetscValidHeaderSpecific(v3,VEC_COOKIE,4); 1333 1334 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1335 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1336 if (mat->N != v1->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v1: global dim %d %d",mat->N,v1->N); 1337 if (mat->M != v2->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v2: global dim %d %d",mat->M,v2->N); 1338 if (mat->M != v3->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v3: global dim %d %d",mat->M,v3->N); 1339 if (mat->m != v3->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v3: local dim %d %d",mat->m,v3->n); 1340 if (mat->m != v2->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v2: local dim %d %d",mat->m,v2->n); 1341 if (v1 == v3) SETERRQ(PETSC_ERR_ARG_IDN,"v1 and v3 must be different vectors"); 1342 1343 ierr = PetscLogEventBegin(MAT_MultAdd,mat,v1,v2,v3);CHKERRQ(ierr); 1344 ierr = (*mat->ops->multadd)(mat,v1,v2,v3);CHKERRQ(ierr); 1345 ierr = PetscLogEventEnd(MAT_MultAdd,mat,v1,v2,v3);CHKERRQ(ierr); 1346 ierr = PetscObjectIncreaseState((PetscObject)v3);CHKERRQ(ierr); 1347 PetscFunctionReturn(0); 1348 } 1349 1350 #undef __FUNCT__ 1351 #define __FUNCT__ "MatMultTransposeAdd" 1352 /*@ 1353 MatMultTransposeAdd - Computes v3 = v2 + A' * v1. 1354 1355 Collective on Mat and Vec 1356 1357 Input Parameters: 1358 + mat - the matrix 1359 - v1, v2 - the vectors 1360 1361 Output Parameters: 1362 . v3 - the result 1363 1364 Notes: 1365 The vectors v1 and v3 cannot be the same. I.e., one cannot 1366 call MatMultTransposeAdd(A,v1,v2,v1). 1367 1368 Level: beginner 1369 1370 Concepts: matrix vector product^transpose and addition 1371 1372 .seealso: MatMultTranspose(), MatMultAdd(), MatMult() 1373 @*/ 1374 int MatMultTransposeAdd(Mat mat,Vec v1,Vec v2,Vec v3) 1375 { 1376 int ierr; 1377 1378 PetscFunctionBegin; 1379 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 1380 PetscValidType(mat,1); 1381 MatPreallocated(mat); 1382 PetscValidHeaderSpecific(v1,VEC_COOKIE,2); 1383 PetscValidHeaderSpecific(v2,VEC_COOKIE,3); 1384 PetscValidHeaderSpecific(v3,VEC_COOKIE,4); 1385 1386 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1387 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1388 if (!mat->ops->multtransposeadd) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 1389 if (v1 == v3) SETERRQ(PETSC_ERR_ARG_IDN,"v1 and v3 must be different vectors"); 1390 if (mat->M != v1->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v1: global dim %d %d",mat->M,v1->N); 1391 if (mat->N != v2->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v2: global dim %d %d",mat->N,v2->N); 1392 if (mat->N != v3->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v3: global dim %d %d",mat->N,v3->N); 1393 1394 ierr = PetscLogEventBegin(MAT_MultTransposeAdd,mat,v1,v2,v3);CHKERRQ(ierr); 1395 ierr = (*mat->ops->multtransposeadd)(mat,v1,v2,v3);CHKERRQ(ierr); 1396 ierr = PetscLogEventEnd(MAT_MultTransposeAdd,mat,v1,v2,v3);CHKERRQ(ierr); 1397 ierr = PetscObjectIncreaseState((PetscObject)v3);CHKERRQ(ierr); 1398 PetscFunctionReturn(0); 1399 } 1400 1401 #undef __FUNCT__ 1402 #define __FUNCT__ "MatMultConstrained" 1403 /*@ 1404 MatMultConstrained - The inner multiplication routine for a 1405 constrained matrix P^T A P. 1406 1407 Collective on Mat and Vec 1408 1409 Input Parameters: 1410 + mat - the matrix 1411 - x - the vector to be multilplied 1412 1413 Output Parameters: 1414 . y - the result 1415 1416 Notes: 1417 The vectors x and y cannot be the same. I.e., one cannot 1418 call MatMult(A,y,y). 1419 1420 Level: beginner 1421 1422 .keywords: matrix, multiply, matrix-vector product, constraint 1423 .seealso: MatMult(), MatMultTrans(), MatMultAdd(), MatMultTransAdd() 1424 @*/ 1425 int MatMultConstrained(Mat mat,Vec x,Vec y) 1426 { 1427 int ierr; 1428 1429 PetscFunctionBegin; 1430 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 1431 PetscValidHeaderSpecific(x,VEC_COOKIE,2); 1432 PetscValidHeaderSpecific(y,VEC_COOKIE,3); 1433 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1434 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1435 if (x == y) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors"); 1436 if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N); 1437 if (mat->M != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %d %d",mat->M,y->N); 1438 if (mat->m != y->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: local dim %d %d",mat->m,y->n); 1439 1440 ierr = PetscLogEventBegin(MAT_MultConstrained,mat,x,y,0);CHKERRQ(ierr); 1441 ierr = (*mat->ops->multconstrained)(mat,x,y);CHKERRQ(ierr); 1442 ierr = PetscLogEventEnd(MAT_MultConstrained,mat,x,y,0);CHKERRQ(ierr); 1443 ierr = PetscObjectIncreaseState((PetscObject)y);CHKERRQ(ierr); 1444 1445 PetscFunctionReturn(0); 1446 } 1447 1448 #undef __FUNCT__ 1449 #define __FUNCT__ "MatMultTransposeConstrained" 1450 /*@ 1451 MatMultTransposeConstrained - The inner multiplication routine for a 1452 constrained matrix P^T A^T P. 1453 1454 Collective on Mat and Vec 1455 1456 Input Parameters: 1457 + mat - the matrix 1458 - x - the vector to be multilplied 1459 1460 Output Parameters: 1461 . y - the result 1462 1463 Notes: 1464 The vectors x and y cannot be the same. I.e., one cannot 1465 call MatMult(A,y,y). 1466 1467 Level: beginner 1468 1469 .keywords: matrix, multiply, matrix-vector product, constraint 1470 .seealso: MatMult(), MatMultTrans(), MatMultAdd(), MatMultTransAdd() 1471 @*/ 1472 int MatMultTransposeConstrained(Mat mat,Vec x,Vec y) 1473 { 1474 int ierr; 1475 1476 PetscFunctionBegin; 1477 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 1478 PetscValidHeaderSpecific(x,VEC_COOKIE,2); 1479 PetscValidHeaderSpecific(y,VEC_COOKIE,3); 1480 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1481 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1482 if (x == y) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors"); 1483 if (mat->M != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N); 1484 if (mat->N != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %d %d",mat->M,y->N); 1485 1486 ierr = PetscLogEventBegin(MAT_MultConstrained,mat,x,y,0);CHKERRQ(ierr); 1487 ierr = (*mat->ops->multtransposeconstrained)(mat,x,y);CHKERRQ(ierr); 1488 ierr = PetscLogEventEnd(MAT_MultConstrained,mat,x,y,0);CHKERRQ(ierr); 1489 ierr = PetscObjectIncreaseState((PetscObject)y);CHKERRQ(ierr); 1490 1491 PetscFunctionReturn(0); 1492 } 1493 /* ------------------------------------------------------------*/ 1494 #undef __FUNCT__ 1495 #define __FUNCT__ "MatGetInfo" 1496 /*@C 1497 MatGetInfo - Returns information about matrix storage (number of 1498 nonzeros, memory, etc.). 1499 1500 Collective on Mat if MAT_GLOBAL_MAX or MAT_GLOBAL_SUM is used 1501 as the flag 1502 1503 Input Parameters: 1504 . mat - the matrix 1505 1506 Output Parameters: 1507 + flag - flag indicating the type of parameters to be returned 1508 (MAT_LOCAL - local matrix, MAT_GLOBAL_MAX - maximum over all processors, 1509 MAT_GLOBAL_SUM - sum over all processors) 1510 - info - matrix information context 1511 1512 Notes: 1513 The MatInfo context contains a variety of matrix data, including 1514 number of nonzeros allocated and used, number of mallocs during 1515 matrix assembly, etc. Additional information for factored matrices 1516 is provided (such as the fill ratio, number of mallocs during 1517 factorization, etc.). Much of this info is printed to STDOUT 1518 when using the runtime options 1519 $ -log_info -mat_view_info 1520 1521 Example for C/C++ Users: 1522 See the file ${PETSC_DIR}/include/petscmat.h for a complete list of 1523 data within the MatInfo context. For example, 1524 .vb 1525 MatInfo info; 1526 Mat A; 1527 double mal, nz_a, nz_u; 1528 1529 MatGetInfo(A,MAT_LOCAL,&info); 1530 mal = info.mallocs; 1531 nz_a = info.nz_allocated; 1532 .ve 1533 1534 Example for Fortran Users: 1535 Fortran users should declare info as a double precision 1536 array of dimension MAT_INFO_SIZE, and then extract the parameters 1537 of interest. See the file ${PETSC_DIR}/include/finclude/petscmat.h 1538 a complete list of parameter names. 1539 .vb 1540 double precision info(MAT_INFO_SIZE) 1541 double precision mal, nz_a 1542 Mat A 1543 integer ierr 1544 1545 call MatGetInfo(A,MAT_LOCAL,info,ierr) 1546 mal = info(MAT_INFO_MALLOCS) 1547 nz_a = info(MAT_INFO_NZ_ALLOCATED) 1548 .ve 1549 1550 Level: intermediate 1551 1552 Concepts: matrices^getting information on 1553 1554 @*/ 1555 int MatGetInfo(Mat mat,MatInfoType flag,MatInfo *info) 1556 { 1557 int ierr; 1558 1559 PetscFunctionBegin; 1560 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 1561 PetscValidType(mat,1); 1562 MatPreallocated(mat); 1563 PetscValidPointer(info,3); 1564 if (!mat->ops->getinfo) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 1565 ierr = (*mat->ops->getinfo)(mat,flag,info);CHKERRQ(ierr); 1566 PetscFunctionReturn(0); 1567 } 1568 1569 /* ----------------------------------------------------------*/ 1570 #undef __FUNCT__ 1571 #define __FUNCT__ "MatILUDTFactor" 1572 /*@C 1573 MatILUDTFactor - Performs a drop tolerance ILU factorization. 1574 1575 Collective on Mat 1576 1577 Input Parameters: 1578 + mat - the matrix 1579 . info - information about the factorization to be done 1580 . row - row permutation 1581 - col - column permutation 1582 1583 Output Parameters: 1584 . fact - the factored matrix 1585 1586 Level: developer 1587 1588 Notes: 1589 Most users should employ the simplified KSP interface for linear solvers 1590 instead of working directly with matrix algebra routines such as this. 1591 See, e.g., KSPCreate(). 1592 1593 This is currently only supported for the SeqAIJ matrix format using code 1594 from Yousef Saad's SPARSEKIT2 package (translated to C with f2c) and/or 1595 Matlab. SPARSEKIT2 is copyrighted by Yousef Saad with the GNU copyright 1596 and thus can be distributed with PETSc. 1597 1598 Concepts: matrices^ILUDT factorization 1599 1600 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor(), MatFactorInfo 1601 @*/ 1602 int MatILUDTFactor(Mat mat,MatFactorInfo *info,IS row,IS col,Mat *fact) 1603 { 1604 int ierr; 1605 1606 PetscFunctionBegin; 1607 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 1608 PetscValidType(mat,1); 1609 MatPreallocated(mat); 1610 PetscValidPointer(info,2); 1611 if (row) PetscValidHeaderSpecific(row,IS_COOKIE,3); 1612 if (col) PetscValidHeaderSpecific(col,IS_COOKIE,4); 1613 PetscValidPointer(fact,5); 1614 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1615 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1616 if (!mat->ops->iludtfactor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 1617 1618 ierr = PetscLogEventBegin(MAT_ILUFactor,mat,row,col,0);CHKERRQ(ierr); 1619 ierr = (*mat->ops->iludtfactor)(mat,info,row,col,fact);CHKERRQ(ierr); 1620 ierr = PetscLogEventEnd(MAT_ILUFactor,mat,row,col,0);CHKERRQ(ierr); 1621 ierr = PetscObjectIncreaseState((PetscObject)*fact);CHKERRQ(ierr); 1622 1623 PetscFunctionReturn(0); 1624 } 1625 1626 #undef __FUNCT__ 1627 #define __FUNCT__ "MatLUFactor" 1628 /*@ 1629 MatLUFactor - Performs in-place LU factorization of matrix. 1630 1631 Collective on Mat 1632 1633 Input Parameters: 1634 + mat - the matrix 1635 . row - row permutation 1636 . col - column permutation 1637 - info - options for factorization, includes 1638 $ fill - expected fill as ratio of original fill. 1639 $ dtcol - pivot tolerance (0 no pivot, 1 full column pivoting) 1640 $ Run with the option -log_info to determine an optimal value to use 1641 1642 Notes: 1643 Most users should employ the simplified KSP interface for linear solvers 1644 instead of working directly with matrix algebra routines such as this. 1645 See, e.g., KSPCreate(). 1646 1647 This changes the state of the matrix to a factored matrix; it cannot be used 1648 for example with MatSetValues() unless one first calls MatSetUnfactored(). 1649 1650 Level: developer 1651 1652 Concepts: matrices^LU factorization 1653 1654 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor(), 1655 MatGetOrdering(), MatSetUnfactored(), MatFactorInfo 1656 1657 @*/ 1658 int MatLUFactor(Mat mat,IS row,IS col,MatFactorInfo *info) 1659 { 1660 int ierr; 1661 1662 PetscFunctionBegin; 1663 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 1664 if (row) PetscValidHeaderSpecific(row,IS_COOKIE,2); 1665 if (col) PetscValidHeaderSpecific(col,IS_COOKIE,3); 1666 PetscValidPointer(info,4); 1667 PetscValidType(mat,1); 1668 MatPreallocated(mat); 1669 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1670 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1671 if (!mat->ops->lufactor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 1672 1673 ierr = PetscLogEventBegin(MAT_LUFactor,mat,row,col,0);CHKERRQ(ierr); 1674 ierr = (*mat->ops->lufactor)(mat,row,col,info);CHKERRQ(ierr); 1675 ierr = PetscLogEventEnd(MAT_LUFactor,mat,row,col,0);CHKERRQ(ierr); 1676 ierr = PetscObjectIncreaseState((PetscObject)mat);CHKERRQ(ierr); 1677 PetscFunctionReturn(0); 1678 } 1679 1680 #undef __FUNCT__ 1681 #define __FUNCT__ "MatILUFactor" 1682 /*@ 1683 MatILUFactor - Performs in-place ILU factorization of matrix. 1684 1685 Collective on Mat 1686 1687 Input Parameters: 1688 + mat - the matrix 1689 . row - row permutation 1690 . col - column permutation 1691 - info - structure containing 1692 $ levels - number of levels of fill. 1693 $ expected fill - as ratio of original fill. 1694 $ 1 or 0 - indicating force fill on diagonal (improves robustness for matrices 1695 missing diagonal entries) 1696 1697 Notes: 1698 Probably really in-place only when level of fill is zero, otherwise allocates 1699 new space to store factored matrix and deletes previous memory. 1700 1701 Most users should employ the simplified KSP interface for linear solvers 1702 instead of working directly with matrix algebra routines such as this. 1703 See, e.g., KSPCreate(). 1704 1705 Level: developer 1706 1707 Concepts: matrices^ILU factorization 1708 1709 .seealso: MatILUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor(), MatFactorInfo 1710 @*/ 1711 int MatILUFactor(Mat mat,IS row,IS col,MatFactorInfo *info) 1712 { 1713 int ierr; 1714 1715 PetscFunctionBegin; 1716 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 1717 if (row) PetscValidHeaderSpecific(row,IS_COOKIE,2); 1718 if (col) PetscValidHeaderSpecific(col,IS_COOKIE,3); 1719 PetscValidPointer(info,4); 1720 PetscValidType(mat,1); 1721 MatPreallocated(mat); 1722 if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,"matrix must be square"); 1723 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1724 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1725 if (!mat->ops->ilufactor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 1726 1727 ierr = PetscLogEventBegin(MAT_ILUFactor,mat,row,col,0);CHKERRQ(ierr); 1728 ierr = (*mat->ops->ilufactor)(mat,row,col,info);CHKERRQ(ierr); 1729 ierr = PetscLogEventEnd(MAT_ILUFactor,mat,row,col,0);CHKERRQ(ierr); 1730 ierr = PetscObjectIncreaseState((PetscObject)mat);CHKERRQ(ierr); 1731 PetscFunctionReturn(0); 1732 } 1733 1734 #undef __FUNCT__ 1735 #define __FUNCT__ "MatLUFactorSymbolic" 1736 /*@ 1737 MatLUFactorSymbolic - Performs symbolic LU factorization of matrix. 1738 Call this routine before calling MatLUFactorNumeric(). 1739 1740 Collective on Mat 1741 1742 Input Parameters: 1743 + mat - the matrix 1744 . row, col - row and column permutations 1745 - info - options for factorization, includes 1746 $ fill - expected fill as ratio of original fill. 1747 $ dtcol - pivot tolerance (0 no pivot, 1 full column pivoting) 1748 $ Run with the option -log_info to determine an optimal value to use 1749 1750 Output Parameter: 1751 . fact - new matrix that has been symbolically factored 1752 1753 Notes: 1754 See the users manual for additional information about 1755 choosing the fill factor for better efficiency. 1756 1757 Most users should employ the simplified KSP interface for linear solvers 1758 instead of working directly with matrix algebra routines such as this. 1759 See, e.g., KSPCreate(). 1760 1761 Level: developer 1762 1763 Concepts: matrices^LU symbolic factorization 1764 1765 .seealso: MatLUFactor(), MatLUFactorNumeric(), MatCholeskyFactor(), MatFactorInfo 1766 @*/ 1767 int MatLUFactorSymbolic(Mat mat,IS row,IS col,MatFactorInfo *info,Mat *fact) 1768 { 1769 int ierr; 1770 1771 PetscFunctionBegin; 1772 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 1773 if (row) PetscValidHeaderSpecific(row,IS_COOKIE,2); 1774 if (col) PetscValidHeaderSpecific(col,IS_COOKIE,3); 1775 PetscValidPointer(info,4); 1776 PetscValidType(mat,1); 1777 MatPreallocated(mat); 1778 PetscValidPointer(fact,5); 1779 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1780 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1781 if (!mat->ops->lufactorsymbolic) SETERRQ1(PETSC_ERR_SUP,"Matrix type %s symbolic LU",mat->type_name); 1782 1783 ierr = PetscLogEventBegin(MAT_LUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr); 1784 ierr = (*mat->ops->lufactorsymbolic)(mat,row,col,info,fact);CHKERRQ(ierr); 1785 ierr = PetscLogEventEnd(MAT_LUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr); 1786 ierr = PetscObjectIncreaseState((PetscObject)*fact);CHKERRQ(ierr); 1787 PetscFunctionReturn(0); 1788 } 1789 1790 #undef __FUNCT__ 1791 #define __FUNCT__ "MatLUFactorNumeric" 1792 /*@ 1793 MatLUFactorNumeric - Performs numeric LU factorization of a matrix. 1794 Call this routine after first calling MatLUFactorSymbolic(). 1795 1796 Collective on Mat 1797 1798 Input Parameters: 1799 + mat - the matrix 1800 - fact - the matrix generated for the factor, from MatLUFactorSymbolic() 1801 1802 Notes: 1803 See MatLUFactor() for in-place factorization. See 1804 MatCholeskyFactorNumeric() for the symmetric, positive definite case. 1805 1806 Most users should employ the simplified KSP interface for linear solvers 1807 instead of working directly with matrix algebra routines such as this. 1808 See, e.g., KSPCreate(). 1809 1810 Level: developer 1811 1812 Concepts: matrices^LU numeric factorization 1813 1814 .seealso: MatLUFactorSymbolic(), MatLUFactor(), MatCholeskyFactor() 1815 @*/ 1816 int MatLUFactorNumeric(Mat mat,Mat *fact) 1817 { 1818 int ierr; 1819 1820 PetscFunctionBegin; 1821 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 1822 PetscValidType(mat,1); 1823 MatPreallocated(mat); 1824 PetscValidPointer(fact,2); 1825 PetscValidHeaderSpecific(*fact,MAT_COOKIE,2); 1826 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1827 if (mat->M != (*fact)->M || mat->N != (*fact)->N) { 1828 SETERRQ4(PETSC_ERR_ARG_SIZ,"Mat mat,Mat *fact: global dimensions are different %d should = %d %d should = %d", 1829 mat->M,(*fact)->M,mat->N,(*fact)->N); 1830 } 1831 if (!(*fact)->ops->lufactornumeric) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 1832 1833 ierr = PetscLogEventBegin(MAT_LUFactorNumeric,mat,*fact,0,0);CHKERRQ(ierr); 1834 ierr = (*(*fact)->ops->lufactornumeric)(mat,fact);CHKERRQ(ierr); 1835 ierr = PetscLogEventEnd(MAT_LUFactorNumeric,mat,*fact,0,0);CHKERRQ(ierr); 1836 1837 ierr = MatView_Private(*fact);CHKERRQ(ierr); 1838 ierr = PetscObjectIncreaseState((PetscObject)*fact);CHKERRQ(ierr); 1839 PetscFunctionReturn(0); 1840 } 1841 1842 #undef __FUNCT__ 1843 #define __FUNCT__ "MatCholeskyFactor" 1844 /*@ 1845 MatCholeskyFactor - Performs in-place Cholesky factorization of a 1846 symmetric matrix. 1847 1848 Collective on Mat 1849 1850 Input Parameters: 1851 + mat - the matrix 1852 . perm - row and column permutations 1853 - f - expected fill as ratio of original fill 1854 1855 Notes: 1856 See MatLUFactor() for the nonsymmetric case. See also 1857 MatCholeskyFactorSymbolic(), and MatCholeskyFactorNumeric(). 1858 1859 Most users should employ the simplified KSP interface for linear solvers 1860 instead of working directly with matrix algebra routines such as this. 1861 See, e.g., KSPCreate(). 1862 1863 Level: developer 1864 1865 Concepts: matrices^Cholesky factorization 1866 1867 .seealso: MatLUFactor(), MatCholeskyFactorSymbolic(), MatCholeskyFactorNumeric() 1868 MatGetOrdering() 1869 1870 @*/ 1871 int MatCholeskyFactor(Mat mat,IS perm,MatFactorInfo *info) 1872 { 1873 int ierr; 1874 1875 PetscFunctionBegin; 1876 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 1877 PetscValidType(mat,1); 1878 MatPreallocated(mat); 1879 PetscValidHeaderSpecific(perm,IS_COOKIE,2); 1880 PetscValidPointer(info,3); 1881 if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,"Matrix must be square"); 1882 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1883 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1884 if (!mat->ops->choleskyfactor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 1885 1886 ierr = PetscLogEventBegin(MAT_CholeskyFactor,mat,perm,0,0);CHKERRQ(ierr); 1887 ierr = (*mat->ops->choleskyfactor)(mat,perm,info);CHKERRQ(ierr); 1888 ierr = PetscLogEventEnd(MAT_CholeskyFactor,mat,perm,0,0);CHKERRQ(ierr); 1889 ierr = PetscObjectIncreaseState((PetscObject)mat);CHKERRQ(ierr); 1890 PetscFunctionReturn(0); 1891 } 1892 1893 #undef __FUNCT__ 1894 #define __FUNCT__ "MatCholeskyFactorSymbolic" 1895 /*@ 1896 MatCholeskyFactorSymbolic - Performs symbolic Cholesky factorization 1897 of a symmetric matrix. 1898 1899 Collective on Mat 1900 1901 Input Parameters: 1902 + mat - the matrix 1903 . perm - row and column permutations 1904 - info - options for factorization, includes 1905 $ fill - expected fill as ratio of original fill. 1906 $ dtcol - pivot tolerance (0 no pivot, 1 full column pivoting) 1907 $ Run with the option -log_info to determine an optimal value to use 1908 1909 Output Parameter: 1910 . fact - the factored matrix 1911 1912 Notes: 1913 See MatLUFactorSymbolic() for the nonsymmetric case. See also 1914 MatCholeskyFactor() and MatCholeskyFactorNumeric(). 1915 1916 Most users should employ the simplified KSP interface for linear solvers 1917 instead of working directly with matrix algebra routines such as this. 1918 See, e.g., KSPCreate(). 1919 1920 Level: developer 1921 1922 Concepts: matrices^Cholesky symbolic factorization 1923 1924 .seealso: MatLUFactorSymbolic(), MatCholeskyFactor(), MatCholeskyFactorNumeric() 1925 MatGetOrdering() 1926 1927 @*/ 1928 int MatCholeskyFactorSymbolic(Mat mat,IS perm,MatFactorInfo *info,Mat *fact) 1929 { 1930 int ierr; 1931 1932 PetscFunctionBegin; 1933 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 1934 PetscValidType(mat,1); 1935 MatPreallocated(mat); 1936 if (perm) PetscValidHeaderSpecific(perm,IS_COOKIE,2); 1937 PetscValidPointer(info,3); 1938 PetscValidPointer(fact,4); 1939 if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,"Matrix must be square"); 1940 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1941 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1942 if (!mat->ops->choleskyfactorsymbolic) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 1943 1944 ierr = PetscLogEventBegin(MAT_CholeskyFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr); 1945 ierr = (*mat->ops->choleskyfactorsymbolic)(mat,perm,info,fact);CHKERRQ(ierr); 1946 ierr = PetscLogEventEnd(MAT_CholeskyFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr); 1947 ierr = PetscObjectIncreaseState((PetscObject)*fact);CHKERRQ(ierr); 1948 PetscFunctionReturn(0); 1949 } 1950 1951 #undef __FUNCT__ 1952 #define __FUNCT__ "MatCholeskyFactorNumeric" 1953 /*@ 1954 MatCholeskyFactorNumeric - Performs numeric Cholesky factorization 1955 of a symmetric matrix. Call this routine after first calling 1956 MatCholeskyFactorSymbolic(). 1957 1958 Collective on Mat 1959 1960 Input Parameter: 1961 . mat - the initial matrix 1962 1963 Output Parameter: 1964 . fact - the factored matrix 1965 1966 Notes: 1967 Most users should employ the simplified KSP interface for linear solvers 1968 instead of working directly with matrix algebra routines such as this. 1969 See, e.g., KSPCreate(). 1970 1971 Level: developer 1972 1973 Concepts: matrices^Cholesky numeric factorization 1974 1975 .seealso: MatCholeskyFactorSymbolic(), MatCholeskyFactor(), MatLUFactorNumeric() 1976 @*/ 1977 int MatCholeskyFactorNumeric(Mat mat,Mat *fact) 1978 { 1979 int ierr; 1980 1981 PetscFunctionBegin; 1982 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 1983 PetscValidType(mat,1); 1984 MatPreallocated(mat); 1985 PetscValidPointer(fact,2); 1986 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1987 if (!(*fact)->ops->choleskyfactornumeric) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 1988 if (mat->M != (*fact)->M || mat->N != (*fact)->N) { 1989 SETERRQ4(PETSC_ERR_ARG_SIZ,"Mat mat,Mat *fact: global dim %d should = %d %d should = %d", 1990 mat->M,(*fact)->M,mat->N,(*fact)->N); 1991 } 1992 1993 ierr = PetscLogEventBegin(MAT_CholeskyFactorNumeric,mat,*fact,0,0);CHKERRQ(ierr); 1994 ierr = (*(*fact)->ops->choleskyfactornumeric)(mat,fact);CHKERRQ(ierr); 1995 ierr = PetscLogEventEnd(MAT_CholeskyFactorNumeric,mat,*fact,0,0);CHKERRQ(ierr); 1996 ierr = PetscObjectIncreaseState((PetscObject)*fact);CHKERRQ(ierr); 1997 PetscFunctionReturn(0); 1998 } 1999 2000 /* ----------------------------------------------------------------*/ 2001 #undef __FUNCT__ 2002 #define __FUNCT__ "MatSolve" 2003 /*@ 2004 MatSolve - Solves A x = b, given a factored matrix. 2005 2006 Collective on Mat and Vec 2007 2008 Input Parameters: 2009 + mat - the factored matrix 2010 - b - the right-hand-side vector 2011 2012 Output Parameter: 2013 . x - the result vector 2014 2015 Notes: 2016 The vectors b and x cannot be the same. I.e., one cannot 2017 call MatSolve(A,x,x). 2018 2019 Notes: 2020 Most users should employ the simplified KSP interface for linear solvers 2021 instead of working directly with matrix algebra routines such as this. 2022 See, e.g., KSPCreate(). 2023 2024 Level: developer 2025 2026 Concepts: matrices^triangular solves 2027 2028 .seealso: MatSolveAdd(), MatSolveTranspose(), MatSolveTransposeAdd() 2029 @*/ 2030 int MatSolve(Mat mat,Vec b,Vec x) 2031 { 2032 int ierr; 2033 2034 PetscFunctionBegin; 2035 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 2036 PetscValidType(mat,1); 2037 MatPreallocated(mat); 2038 PetscValidHeaderSpecific(b,VEC_COOKIE,2); 2039 PetscValidHeaderSpecific(x,VEC_COOKIE,3); 2040 PetscCheckSameComm(mat,1,b,2); 2041 PetscCheckSameComm(mat,1,x,3); 2042 if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 2043 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 2044 if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N); 2045 if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->M,b->N); 2046 if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %d %d",mat->m,b->n); 2047 if (mat->M == 0 && mat->N == 0) PetscFunctionReturn(0); 2048 2049 if (!mat->ops->solve) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 2050 ierr = PetscLogEventBegin(MAT_Solve,mat,b,x,0);CHKERRQ(ierr); 2051 ierr = (*mat->ops->solve)(mat,b,x);CHKERRQ(ierr); 2052 ierr = PetscLogEventEnd(MAT_Solve,mat,b,x,0);CHKERRQ(ierr); 2053 ierr = PetscObjectIncreaseState((PetscObject)x);CHKERRQ(ierr); 2054 PetscFunctionReturn(0); 2055 } 2056 2057 #undef __FUNCT__ 2058 #define __FUNCT__ "MatForwardSolve" 2059 /* @ 2060 MatForwardSolve - Solves L x = b, given a factored matrix, A = LU. 2061 2062 Collective on Mat and Vec 2063 2064 Input Parameters: 2065 + mat - the factored matrix 2066 - b - the right-hand-side vector 2067 2068 Output Parameter: 2069 . x - the result vector 2070 2071 Notes: 2072 MatSolve() should be used for most applications, as it performs 2073 a forward solve followed by a backward solve. 2074 2075 The vectors b and x cannot be the same. I.e., one cannot 2076 call MatForwardSolve(A,x,x). 2077 2078 Most users should employ the simplified KSP interface for linear solvers 2079 instead of working directly with matrix algebra routines such as this. 2080 See, e.g., KSPCreate(). 2081 2082 Level: developer 2083 2084 Concepts: matrices^forward solves 2085 2086 .seealso: MatSolve(), MatBackwardSolve() 2087 @ */ 2088 int MatForwardSolve(Mat mat,Vec b,Vec x) 2089 { 2090 int ierr; 2091 2092 PetscFunctionBegin; 2093 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 2094 PetscValidType(mat,1); 2095 MatPreallocated(mat); 2096 PetscValidHeaderSpecific(b,VEC_COOKIE,2); 2097 PetscValidHeaderSpecific(x,VEC_COOKIE,3); 2098 PetscCheckSameComm(mat,1,b,2); 2099 PetscCheckSameComm(mat,1,x,3); 2100 if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 2101 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 2102 if (!mat->ops->forwardsolve) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 2103 if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N); 2104 if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->M,b->N); 2105 if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %d %d",mat->m,b->n); 2106 2107 ierr = PetscLogEventBegin(MAT_ForwardSolve,mat,b,x,0);CHKERRQ(ierr); 2108 ierr = (*mat->ops->forwardsolve)(mat,b,x);CHKERRQ(ierr); 2109 ierr = PetscLogEventEnd(MAT_ForwardSolve,mat,b,x,0);CHKERRQ(ierr); 2110 ierr = PetscObjectIncreaseState((PetscObject)x);CHKERRQ(ierr); 2111 PetscFunctionReturn(0); 2112 } 2113 2114 #undef __FUNCT__ 2115 #define __FUNCT__ "MatBackwardSolve" 2116 /* @ 2117 MatBackwardSolve - Solves U x = b, given a factored matrix, A = LU. 2118 2119 Collective on Mat and Vec 2120 2121 Input Parameters: 2122 + mat - the factored matrix 2123 - b - the right-hand-side vector 2124 2125 Output Parameter: 2126 . x - the result vector 2127 2128 Notes: 2129 MatSolve() should be used for most applications, as it performs 2130 a forward solve followed by a backward solve. 2131 2132 The vectors b and x cannot be the same. I.e., one cannot 2133 call MatBackwardSolve(A,x,x). 2134 2135 Most users should employ the simplified KSP interface for linear solvers 2136 instead of working directly with matrix algebra routines such as this. 2137 See, e.g., KSPCreate(). 2138 2139 Level: developer 2140 2141 Concepts: matrices^backward solves 2142 2143 .seealso: MatSolve(), MatForwardSolve() 2144 @ */ 2145 int MatBackwardSolve(Mat mat,Vec b,Vec x) 2146 { 2147 int ierr; 2148 2149 PetscFunctionBegin; 2150 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 2151 PetscValidType(mat,1); 2152 MatPreallocated(mat); 2153 PetscValidHeaderSpecific(b,VEC_COOKIE,2); 2154 PetscValidHeaderSpecific(x,VEC_COOKIE,3); 2155 PetscCheckSameComm(mat,1,b,2); 2156 PetscCheckSameComm(mat,1,x,3); 2157 if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 2158 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 2159 if (!mat->ops->backwardsolve) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 2160 if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N); 2161 if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->M,b->N); 2162 if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %d %d",mat->m,b->n); 2163 2164 ierr = PetscLogEventBegin(MAT_BackwardSolve,mat,b,x,0);CHKERRQ(ierr); 2165 ierr = (*mat->ops->backwardsolve)(mat,b,x);CHKERRQ(ierr); 2166 ierr = PetscLogEventEnd(MAT_BackwardSolve,mat,b,x,0);CHKERRQ(ierr); 2167 ierr = PetscObjectIncreaseState((PetscObject)x);CHKERRQ(ierr); 2168 PetscFunctionReturn(0); 2169 } 2170 2171 #undef __FUNCT__ 2172 #define __FUNCT__ "MatSolveAdd" 2173 /*@ 2174 MatSolveAdd - Computes x = y + inv(A)*b, given a factored matrix. 2175 2176 Collective on Mat and Vec 2177 2178 Input Parameters: 2179 + mat - the factored matrix 2180 . b - the right-hand-side vector 2181 - y - the vector to be added to 2182 2183 Output Parameter: 2184 . x - the result vector 2185 2186 Notes: 2187 The vectors b and x cannot be the same. I.e., one cannot 2188 call MatSolveAdd(A,x,y,x). 2189 2190 Most users should employ the simplified KSP interface for linear solvers 2191 instead of working directly with matrix algebra routines such as this. 2192 See, e.g., KSPCreate(). 2193 2194 Level: developer 2195 2196 Concepts: matrices^triangular solves 2197 2198 .seealso: MatSolve(), MatSolveTranspose(), MatSolveTransposeAdd() 2199 @*/ 2200 int MatSolveAdd(Mat mat,Vec b,Vec y,Vec x) 2201 { 2202 PetscScalar one = 1.0; 2203 Vec tmp; 2204 int ierr; 2205 2206 PetscFunctionBegin; 2207 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 2208 PetscValidType(mat,1); 2209 MatPreallocated(mat); 2210 PetscValidHeaderSpecific(y,VEC_COOKIE,2); 2211 PetscValidHeaderSpecific(b,VEC_COOKIE,3); 2212 PetscValidHeaderSpecific(x,VEC_COOKIE,4); 2213 PetscCheckSameComm(mat,1,b,2); 2214 PetscCheckSameComm(mat,1,y,2); 2215 PetscCheckSameComm(mat,1,x,3); 2216 if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 2217 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 2218 if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N); 2219 if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->M,b->N); 2220 if (mat->M != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %d %d",mat->M,y->N); 2221 if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %d %d",mat->m,b->n); 2222 if (x->n != y->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Vec x,Vec y: local dim %d %d",x->n,y->n); 2223 2224 ierr = PetscLogEventBegin(MAT_SolveAdd,mat,b,x,y);CHKERRQ(ierr); 2225 if (mat->ops->solveadd) { 2226 ierr = (*mat->ops->solveadd)(mat,b,y,x);CHKERRQ(ierr); 2227 } else { 2228 /* do the solve then the add manually */ 2229 if (x != y) { 2230 ierr = MatSolve(mat,b,x);CHKERRQ(ierr); 2231 ierr = VecAXPY(&one,y,x);CHKERRQ(ierr); 2232 } else { 2233 ierr = VecDuplicate(x,&tmp);CHKERRQ(ierr); 2234 PetscLogObjectParent(mat,tmp); 2235 ierr = VecCopy(x,tmp);CHKERRQ(ierr); 2236 ierr = MatSolve(mat,b,x);CHKERRQ(ierr); 2237 ierr = VecAXPY(&one,tmp,x);CHKERRQ(ierr); 2238 ierr = VecDestroy(tmp);CHKERRQ(ierr); 2239 } 2240 } 2241 ierr = PetscLogEventEnd(MAT_SolveAdd,mat,b,x,y);CHKERRQ(ierr); 2242 ierr = PetscObjectIncreaseState((PetscObject)x);CHKERRQ(ierr); 2243 PetscFunctionReturn(0); 2244 } 2245 2246 #undef __FUNCT__ 2247 #define __FUNCT__ "MatSolveTranspose" 2248 /*@ 2249 MatSolveTranspose - Solves A' x = b, given a factored matrix. 2250 2251 Collective on Mat and Vec 2252 2253 Input Parameters: 2254 + mat - the factored matrix 2255 - b - the right-hand-side vector 2256 2257 Output Parameter: 2258 . x - the result vector 2259 2260 Notes: 2261 The vectors b and x cannot be the same. I.e., one cannot 2262 call MatSolveTranspose(A,x,x). 2263 2264 Most users should employ the simplified KSP interface for linear solvers 2265 instead of working directly with matrix algebra routines such as this. 2266 See, e.g., KSPCreate(). 2267 2268 Level: developer 2269 2270 Concepts: matrices^triangular solves 2271 2272 .seealso: MatSolve(), MatSolveAdd(), MatSolveTransposeAdd() 2273 @*/ 2274 int MatSolveTranspose(Mat mat,Vec b,Vec x) 2275 { 2276 int ierr; 2277 2278 PetscFunctionBegin; 2279 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 2280 PetscValidType(mat,1); 2281 MatPreallocated(mat); 2282 PetscValidHeaderSpecific(b,VEC_COOKIE,2); 2283 PetscValidHeaderSpecific(x,VEC_COOKIE,3); 2284 PetscCheckSameComm(mat,1,b,2); 2285 PetscCheckSameComm(mat,1,x,3); 2286 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 2287 if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 2288 if (!mat->ops->solvetranspose) SETERRQ1(PETSC_ERR_SUP,"Matrix type %s",mat->type_name); 2289 if (mat->M != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->M,x->N); 2290 if (mat->N != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->N,b->N); 2291 2292 ierr = PetscLogEventBegin(MAT_SolveTranspose,mat,b,x,0);CHKERRQ(ierr); 2293 ierr = (*mat->ops->solvetranspose)(mat,b,x);CHKERRQ(ierr); 2294 ierr = PetscLogEventEnd(MAT_SolveTranspose,mat,b,x,0);CHKERRQ(ierr); 2295 ierr = PetscObjectIncreaseState((PetscObject)x);CHKERRQ(ierr); 2296 PetscFunctionReturn(0); 2297 } 2298 2299 #undef __FUNCT__ 2300 #define __FUNCT__ "MatSolveTransposeAdd" 2301 /*@ 2302 MatSolveTransposeAdd - Computes x = y + inv(Transpose(A)) b, given a 2303 factored matrix. 2304 2305 Collective on Mat and Vec 2306 2307 Input Parameters: 2308 + mat - the factored matrix 2309 . b - the right-hand-side vector 2310 - y - the vector to be added to 2311 2312 Output Parameter: 2313 . x - the result vector 2314 2315 Notes: 2316 The vectors b and x cannot be the same. I.e., one cannot 2317 call MatSolveTransposeAdd(A,x,y,x). 2318 2319 Most users should employ the simplified KSP interface for linear solvers 2320 instead of working directly with matrix algebra routines such as this. 2321 See, e.g., KSPCreate(). 2322 2323 Level: developer 2324 2325 Concepts: matrices^triangular solves 2326 2327 .seealso: MatSolve(), MatSolveAdd(), MatSolveTranspose() 2328 @*/ 2329 int MatSolveTransposeAdd(Mat mat,Vec b,Vec y,Vec x) 2330 { 2331 PetscScalar one = 1.0; 2332 int ierr; 2333 Vec tmp; 2334 2335 PetscFunctionBegin; 2336 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 2337 PetscValidType(mat,1); 2338 MatPreallocated(mat); 2339 PetscValidHeaderSpecific(y,VEC_COOKIE,2); 2340 PetscValidHeaderSpecific(b,VEC_COOKIE,3); 2341 PetscValidHeaderSpecific(x,VEC_COOKIE,4); 2342 PetscCheckSameComm(mat,1,b,2); 2343 PetscCheckSameComm(mat,1,y,3); 2344 PetscCheckSameComm(mat,1,x,4); 2345 if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 2346 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 2347 if (mat->M != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->M,x->N); 2348 if (mat->N != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->N,b->N); 2349 if (mat->N != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %d %d",mat->N,y->N); 2350 if (x->n != y->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Vec x,Vec y: local dim %d %d",x->n,y->n); 2351 2352 ierr = PetscLogEventBegin(MAT_SolveTransposeAdd,mat,b,x,y);CHKERRQ(ierr); 2353 if (mat->ops->solvetransposeadd) { 2354 ierr = (*mat->ops->solvetransposeadd)(mat,b,y,x);CHKERRQ(ierr); 2355 } else { 2356 /* do the solve then the add manually */ 2357 if (x != y) { 2358 ierr = MatSolveTranspose(mat,b,x);CHKERRQ(ierr); 2359 ierr = VecAXPY(&one,y,x);CHKERRQ(ierr); 2360 } else { 2361 ierr = VecDuplicate(x,&tmp);CHKERRQ(ierr); 2362 PetscLogObjectParent(mat,tmp); 2363 ierr = VecCopy(x,tmp);CHKERRQ(ierr); 2364 ierr = MatSolveTranspose(mat,b,x);CHKERRQ(ierr); 2365 ierr = VecAXPY(&one,tmp,x);CHKERRQ(ierr); 2366 ierr = VecDestroy(tmp);CHKERRQ(ierr); 2367 } 2368 } 2369 ierr = PetscLogEventEnd(MAT_SolveTransposeAdd,mat,b,x,y);CHKERRQ(ierr); 2370 ierr = PetscObjectIncreaseState((PetscObject)x);CHKERRQ(ierr); 2371 PetscFunctionReturn(0); 2372 } 2373 /* ----------------------------------------------------------------*/ 2374 2375 #undef __FUNCT__ 2376 #define __FUNCT__ "MatRelax" 2377 /*@ 2378 MatRelax - Computes relaxation (SOR, Gauss-Seidel) sweeps. 2379 2380 Collective on Mat and Vec 2381 2382 Input Parameters: 2383 + mat - the matrix 2384 . b - the right hand side 2385 . omega - the relaxation factor 2386 . flag - flag indicating the type of SOR (see below) 2387 . shift - diagonal shift 2388 - its - the number of iterations 2389 - lits - the number of local iterations 2390 2391 Output Parameters: 2392 . x - the solution (can contain an initial guess) 2393 2394 SOR Flags: 2395 . SOR_FORWARD_SWEEP - forward SOR 2396 . SOR_BACKWARD_SWEEP - backward SOR 2397 . SOR_SYMMETRIC_SWEEP - SSOR (symmetric SOR) 2398 . SOR_LOCAL_FORWARD_SWEEP - local forward SOR 2399 . SOR_LOCAL_BACKWARD_SWEEP - local forward SOR 2400 . SOR_LOCAL_SYMMETRIC_SWEEP - local SSOR 2401 . SOR_APPLY_UPPER, SOR_APPLY_LOWER - applies 2402 upper/lower triangular part of matrix to 2403 vector (with omega) 2404 . SOR_ZERO_INITIAL_GUESS - zero initial guess 2405 2406 Notes: 2407 SOR_LOCAL_FORWARD_SWEEP, SOR_LOCAL_BACKWARD_SWEEP, and 2408 SOR_LOCAL_SYMMETRIC_SWEEP perform seperate independent smoothings 2409 on each processor. 2410 2411 Application programmers will not generally use MatRelax() directly, 2412 but instead will employ the KSP/PC interface. 2413 2414 Notes for Advanced Users: 2415 The flags are implemented as bitwise inclusive or operations. 2416 For example, use (SOR_ZERO_INITIAL_GUESS | SOR_SYMMETRIC_SWEEP) 2417 to specify a zero initial guess for SSOR. 2418 2419 Most users should employ the simplified KSP interface for linear solvers 2420 instead of working directly with matrix algebra routines such as this. 2421 See, e.g., KSPCreate(). 2422 2423 See also, MatPBRelax(). This routine will automatically call the point block 2424 version if the point version is not available. 2425 2426 Level: developer 2427 2428 Concepts: matrices^relaxation 2429 Concepts: matrices^SOR 2430 Concepts: matrices^Gauss-Seidel 2431 2432 @*/ 2433 int MatRelax(Mat mat,Vec b,PetscReal omega,MatSORType flag,PetscReal shift,int its,int lits,Vec x) 2434 { 2435 int ierr; 2436 2437 PetscFunctionBegin; 2438 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 2439 PetscValidType(mat,1); 2440 MatPreallocated(mat); 2441 PetscValidHeaderSpecific(b,VEC_COOKIE,2); 2442 PetscValidHeaderSpecific(x,VEC_COOKIE,8); 2443 PetscCheckSameComm(mat,1,b,2); 2444 PetscCheckSameComm(mat,1,x,8); 2445 if (!mat->ops->relax && !mat->ops->pbrelax) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 2446 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2447 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2448 if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N); 2449 if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->M,b->N); 2450 if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %d %d",mat->m,b->n); 2451 2452 ierr = PetscLogEventBegin(MAT_Relax,mat,b,x,0);CHKERRQ(ierr); 2453 if (mat->ops->relax) { 2454 ierr =(*mat->ops->relax)(mat,b,omega,flag,shift,its,lits,x);CHKERRQ(ierr); 2455 } else { 2456 ierr =(*mat->ops->pbrelax)(mat,b,omega,flag,shift,its,lits,x);CHKERRQ(ierr); 2457 } 2458 ierr = PetscLogEventEnd(MAT_Relax,mat,b,x,0);CHKERRQ(ierr); 2459 ierr = PetscObjectIncreaseState((PetscObject)x);CHKERRQ(ierr); 2460 PetscFunctionReturn(0); 2461 } 2462 2463 #undef __FUNCT__ 2464 #define __FUNCT__ "MatPBRelax" 2465 /*@ 2466 MatPBRelax - Computes relaxation (SOR, Gauss-Seidel) sweeps. 2467 2468 Collective on Mat and Vec 2469 2470 See MatRelax() for usage 2471 2472 For multi-component PDEs where the Jacobian is stored in a point block format 2473 (with the PETSc BAIJ matrix formats) the relaxation is done one point block at 2474 a time. That is, the small (for example, 4 by 4) blocks along the diagonal are solved 2475 simultaneously (that is a 4 by 4 linear solve is done) to update all the values at a point. 2476 2477 Level: developer 2478 2479 @*/ 2480 int MatPBRelax(Mat mat,Vec b,PetscReal omega,MatSORType flag,PetscReal shift,int its,int lits,Vec x) 2481 { 2482 int ierr; 2483 2484 PetscFunctionBegin; 2485 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 2486 PetscValidType(mat,1); 2487 MatPreallocated(mat); 2488 PetscValidHeaderSpecific(b,VEC_COOKIE,2); 2489 PetscValidHeaderSpecific(x,VEC_COOKIE,8); 2490 PetscCheckSameComm(mat,1,b,2); 2491 PetscCheckSameComm(mat,1,x,8); 2492 if (!mat->ops->pbrelax) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 2493 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2494 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2495 if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N); 2496 if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->M,b->N); 2497 if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %d %d",mat->m,b->n); 2498 2499 ierr = PetscLogEventBegin(MAT_Relax,mat,b,x,0);CHKERRQ(ierr); 2500 ierr =(*mat->ops->pbrelax)(mat,b,omega,flag,shift,its,lits,x);CHKERRQ(ierr); 2501 ierr = PetscLogEventEnd(MAT_Relax,mat,b,x,0);CHKERRQ(ierr); 2502 ierr = PetscObjectIncreaseState((PetscObject)x);CHKERRQ(ierr); 2503 PetscFunctionReturn(0); 2504 } 2505 2506 #undef __FUNCT__ 2507 #define __FUNCT__ "MatCopy_Basic" 2508 /* 2509 Default matrix copy routine. 2510 */ 2511 int MatCopy_Basic(Mat A,Mat B,MatStructure str) 2512 { 2513 int ierr,i,rstart,rend,nz,*cwork; 2514 PetscScalar *vwork; 2515 2516 PetscFunctionBegin; 2517 ierr = MatZeroEntries(B);CHKERRQ(ierr); 2518 ierr = MatGetOwnershipRange(A,&rstart,&rend);CHKERRQ(ierr); 2519 for (i=rstart; i<rend; i++) { 2520 ierr = MatGetRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr); 2521 ierr = MatSetValues(B,1,&i,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr); 2522 ierr = MatRestoreRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr); 2523 } 2524 ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2525 ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2526 ierr = PetscObjectIncreaseState((PetscObject)B);CHKERRQ(ierr); 2527 PetscFunctionReturn(0); 2528 } 2529 2530 #undef __FUNCT__ 2531 #define __FUNCT__ "MatCopy" 2532 /*@C 2533 MatCopy - Copys a matrix to another matrix. 2534 2535 Collective on Mat 2536 2537 Input Parameters: 2538 + A - the matrix 2539 - str - SAME_NONZERO_PATTERN or DIFFERENT_NONZERO_PATTERN 2540 2541 Output Parameter: 2542 . B - where the copy is put 2543 2544 Notes: 2545 If you use SAME_NONZERO_PATTERN then the two matrices had better have the 2546 same nonzero pattern or the routine will crash. 2547 2548 MatCopy() copies the matrix entries of a matrix to another existing 2549 matrix (after first zeroing the second matrix). A related routine is 2550 MatConvert(), which first creates a new matrix and then copies the data. 2551 2552 Level: intermediate 2553 2554 Concepts: matrices^copying 2555 2556 .seealso: MatConvert(), MatDuplicate() 2557 2558 @*/ 2559 int MatCopy(Mat A,Mat B,MatStructure str) 2560 { 2561 int ierr; 2562 2563 PetscFunctionBegin; 2564 PetscValidHeaderSpecific(A,MAT_COOKIE,1); 2565 PetscValidHeaderSpecific(B,MAT_COOKIE,2); 2566 PetscValidType(A,1); 2567 MatPreallocated(A); 2568 PetscValidType(B,2); 2569 MatPreallocated(B); 2570 PetscCheckSameComm(A,1,B,2); 2571 if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2572 if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2573 if (A->M != B->M || A->N != B->N) SETERRQ4(PETSC_ERR_ARG_SIZ,"Mat A,Mat B: global dim (%d,%d) (%d,%d)",A->M,B->M, 2574 A->N,B->N); 2575 2576 ierr = PetscLogEventBegin(MAT_Copy,A,B,0,0);CHKERRQ(ierr); 2577 if (A->ops->copy) { 2578 ierr = (*A->ops->copy)(A,B,str);CHKERRQ(ierr); 2579 } else { /* generic conversion */ 2580 ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr); 2581 } 2582 if (A->mapping) { 2583 if (B->mapping) {ierr = ISLocalToGlobalMappingDestroy(B->mapping);CHKERRQ(ierr);B->mapping = 0;} 2584 ierr = MatSetLocalToGlobalMapping(B,A->mapping);CHKERRQ(ierr); 2585 } 2586 if (A->bmapping) { 2587 if (B->bmapping) {ierr = ISLocalToGlobalMappingDestroy(B->bmapping);CHKERRQ(ierr);B->bmapping = 0;} 2588 ierr = MatSetLocalToGlobalMappingBlock(B,A->mapping);CHKERRQ(ierr); 2589 } 2590 ierr = PetscLogEventEnd(MAT_Copy,A,B,0,0);CHKERRQ(ierr); 2591 ierr = PetscObjectIncreaseState((PetscObject)B);CHKERRQ(ierr); 2592 PetscFunctionReturn(0); 2593 } 2594 2595 #include "petscsys.h" 2596 PetscTruth MatConvertRegisterAllCalled = PETSC_FALSE; 2597 PetscFList MatConvertList = 0; 2598 2599 #undef __FUNCT__ 2600 #define __FUNCT__ "MatConvertRegister" 2601 /*@C 2602 MatConvertRegister - Allows one to register a routine that converts a sparse matrix 2603 from one format to another. 2604 2605 Not Collective 2606 2607 Input Parameters: 2608 + type - the type of matrix (defined in include/petscmat.h), for example, MATSEQAIJ. 2609 - Converter - the function that reads the matrix from the binary file. 2610 2611 Level: developer 2612 2613 .seealso: MatConvertRegisterAll(), MatConvert() 2614 2615 @*/ 2616 int MatConvertRegister(const char sname[],const char path[],const char name[],int (*function)(Mat,MatType,Mat*)) 2617 { 2618 int ierr; 2619 char fullname[PETSC_MAX_PATH_LEN]; 2620 2621 PetscFunctionBegin; 2622 ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr); 2623 ierr = PetscFListAdd(&MatConvertList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr); 2624 PetscFunctionReturn(0); 2625 } 2626 2627 #undef __FUNCT__ 2628 #define __FUNCT__ "MatConvert" 2629 /*@C 2630 MatConvert - Converts a matrix to another matrix, either of the same 2631 or different type. 2632 2633 Collective on Mat 2634 2635 Input Parameters: 2636 + mat - the matrix 2637 - newtype - new matrix type. Use MATSAME to create a new matrix of the 2638 same type as the original matrix. 2639 2640 Output Parameter: 2641 . M - pointer to place new matrix 2642 2643 Notes: 2644 MatConvert() first creates a new matrix and then copies the data from 2645 the first matrix. A related routine is MatCopy(), which copies the matrix 2646 entries of one matrix to another already existing matrix context. 2647 2648 Level: intermediate 2649 2650 Concepts: matrices^converting between storage formats 2651 2652 .seealso: MatCopy(), MatDuplicate() 2653 @*/ 2654 int MatConvert(Mat mat,const MatType newtype,Mat *M) 2655 { 2656 int ierr; 2657 PetscTruth sametype,issame,flg; 2658 char convname[256],mtype[256]; 2659 2660 PetscFunctionBegin; 2661 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 2662 PetscValidType(mat,1); 2663 MatPreallocated(mat); 2664 PetscValidPointer(M,3); 2665 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2666 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2667 2668 ierr = PetscOptionsGetString(PETSC_NULL,"-matconvert_type",mtype,256,&flg);CHKERRQ(ierr); 2669 if (flg) { 2670 newtype = mtype; 2671 } 2672 ierr = PetscLogEventBegin(MAT_Convert,mat,0,0,0);CHKERRQ(ierr); 2673 2674 ierr = PetscTypeCompare((PetscObject)mat,newtype,&sametype);CHKERRQ(ierr); 2675 ierr = PetscStrcmp(newtype,"same",&issame);CHKERRQ(ierr); 2676 if ((sametype || issame) && mat->ops->duplicate) { 2677 ierr = (*mat->ops->duplicate)(mat,MAT_COPY_VALUES,M);CHKERRQ(ierr); 2678 } else { 2679 int (*conv)(Mat,const MatType,Mat*)=PETSC_NULL; 2680 /* 2681 Order of precedence: 2682 1) See if a specialized converter is known to the current matrix. 2683 2) See if a specialized converter is known to the desired matrix class. 2684 3) See if a good general converter is registered for the desired class 2685 (as of 6/27/03 only MATMPIADJ falls into this category). 2686 4) See if a good general converter is known for the current matrix. 2687 5) Use a really basic converter. 2688 */ 2689 ierr = PetscStrcpy(convname,"MatConvert_");CHKERRQ(ierr); 2690 ierr = PetscStrcat(convname,mat->type_name);CHKERRQ(ierr); 2691 ierr = PetscStrcat(convname,"_");CHKERRQ(ierr); 2692 ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr); 2693 ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr); 2694 ierr = PetscObjectQueryFunction((PetscObject)mat,convname,(void (**)(void))&conv);CHKERRQ(ierr); 2695 if (!conv) { 2696 Mat B; 2697 ierr = MatCreate(mat->comm,0,0,0,0,&B);CHKERRQ(ierr); 2698 ierr = MatSetType(B,newtype);CHKERRQ(ierr); 2699 ierr = PetscObjectQueryFunction((PetscObject)B,convname,(void (**)(void))&conv);CHKERRQ(ierr); 2700 ierr = MatDestroy(B);CHKERRQ(ierr); 2701 if (!conv) { 2702 if (!MatConvertRegisterAllCalled) { 2703 ierr = MatConvertRegisterAll(PETSC_NULL);CHKERRQ(ierr); 2704 } 2705 ierr = PetscFListFind(mat->comm,MatConvertList,newtype,(void(**)(void))&conv);CHKERRQ(ierr); 2706 if (!conv) { 2707 if (mat->ops->convert) { 2708 conv = mat->ops->convert; 2709 } else { 2710 conv = MatConvert_Basic; 2711 } 2712 } 2713 } 2714 } 2715 ierr = (*conv)(mat,newtype,M);CHKERRQ(ierr); 2716 } 2717 ierr = PetscLogEventEnd(MAT_Convert,mat,0,0,0);CHKERRQ(ierr); 2718 ierr = PetscObjectIncreaseState((PetscObject)*M);CHKERRQ(ierr); 2719 PetscFunctionReturn(0); 2720 } 2721 2722 2723 #undef __FUNCT__ 2724 #define __FUNCT__ "MatDuplicate" 2725 /*@C 2726 MatDuplicate - Duplicates a matrix including the non-zero structure. 2727 2728 Collective on Mat 2729 2730 Input Parameters: 2731 + mat - the matrix 2732 - op - either MAT_DO_NOT_COPY_VALUES or MAT_COPY_VALUES, cause it to copy nonzero 2733 values as well or not 2734 2735 Output Parameter: 2736 . M - pointer to place new matrix 2737 2738 Level: intermediate 2739 2740 Concepts: matrices^duplicating 2741 2742 .seealso: MatCopy(), MatConvert() 2743 @*/ 2744 int MatDuplicate(Mat mat,MatDuplicateOption op,Mat *M) 2745 { 2746 int ierr; 2747 2748 PetscFunctionBegin; 2749 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 2750 PetscValidType(mat,1); 2751 MatPreallocated(mat); 2752 PetscValidPointer(M,3); 2753 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2754 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2755 2756 *M = 0; 2757 ierr = PetscLogEventBegin(MAT_Convert,mat,0,0,0);CHKERRQ(ierr); 2758 if (!mat->ops->duplicate) { 2759 SETERRQ(PETSC_ERR_SUP,"Not written for this matrix type"); 2760 } 2761 ierr = (*mat->ops->duplicate)(mat,op,M);CHKERRQ(ierr); 2762 if (mat->mapping) { 2763 ierr = MatSetLocalToGlobalMapping(*M,mat->mapping);CHKERRQ(ierr); 2764 } 2765 if (mat->bmapping) { 2766 ierr = MatSetLocalToGlobalMappingBlock(*M,mat->mapping);CHKERRQ(ierr); 2767 } 2768 ierr = PetscLogEventEnd(MAT_Convert,mat,0,0,0);CHKERRQ(ierr); 2769 ierr = PetscObjectIncreaseState((PetscObject)*M);CHKERRQ(ierr); 2770 PetscFunctionReturn(0); 2771 } 2772 2773 #undef __FUNCT__ 2774 #define __FUNCT__ "MatGetDiagonal" 2775 /*@ 2776 MatGetDiagonal - Gets the diagonal of a matrix. 2777 2778 Collective on Mat and Vec 2779 2780 Input Parameters: 2781 + mat - the matrix 2782 - v - the vector for storing the diagonal 2783 2784 Output Parameter: 2785 . v - the diagonal of the matrix 2786 2787 Notes: 2788 For the SeqAIJ matrix format, this routine may also be called 2789 on a LU factored matrix; in that case it routines the reciprocal of 2790 the diagonal entries in U. It returns the entries permuted by the 2791 row and column permutation used during the symbolic factorization. 2792 2793 Level: intermediate 2794 2795 Concepts: matrices^accessing diagonals 2796 2797 .seealso: MatGetRow(), MatGetSubmatrices(), MatGetSubmatrix(), MatGetRowMax() 2798 @*/ 2799 int MatGetDiagonal(Mat mat,Vec v) 2800 { 2801 int ierr; 2802 2803 PetscFunctionBegin; 2804 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 2805 PetscValidType(mat,1); 2806 MatPreallocated(mat); 2807 PetscValidHeaderSpecific(v,VEC_COOKIE,2); 2808 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2809 if (!mat->ops->getdiagonal) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 2810 2811 ierr = (*mat->ops->getdiagonal)(mat,v);CHKERRQ(ierr); 2812 ierr = PetscObjectIncreaseState((PetscObject)v);CHKERRQ(ierr); 2813 PetscFunctionReturn(0); 2814 } 2815 2816 #undef __FUNCT__ 2817 #define __FUNCT__ "MatGetRowMax" 2818 /*@ 2819 MatGetRowMax - Gets the maximum value (in absolute value) of each 2820 row of the matrix 2821 2822 Collective on Mat and Vec 2823 2824 Input Parameters: 2825 . mat - the matrix 2826 2827 Output Parameter: 2828 . v - the vector for storing the maximums 2829 2830 Level: intermediate 2831 2832 Concepts: matrices^getting row maximums 2833 2834 .seealso: MatGetDiagonal(), MatGetSubmatrices(), MatGetSubmatrix() 2835 @*/ 2836 int MatGetRowMax(Mat mat,Vec v) 2837 { 2838 int ierr; 2839 2840 PetscFunctionBegin; 2841 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 2842 PetscValidType(mat,1); 2843 MatPreallocated(mat); 2844 PetscValidHeaderSpecific(v,VEC_COOKIE,2); 2845 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2846 if (!mat->ops->getrowmax) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 2847 2848 ierr = (*mat->ops->getrowmax)(mat,v);CHKERRQ(ierr); 2849 ierr = PetscObjectIncreaseState((PetscObject)v);CHKERRQ(ierr); 2850 PetscFunctionReturn(0); 2851 } 2852 2853 #undef __FUNCT__ 2854 #define __FUNCT__ "MatTranspose" 2855 /*@C 2856 MatTranspose - Computes an in-place or out-of-place transpose of a matrix. 2857 2858 Collective on Mat 2859 2860 Input Parameter: 2861 . mat - the matrix to transpose 2862 2863 Output Parameters: 2864 . B - the transpose (or pass in PETSC_NULL for an in-place transpose) 2865 2866 Level: intermediate 2867 2868 Concepts: matrices^transposing 2869 2870 .seealso: MatMultTranspose(), MatMultTransposeAdd(), MatIsTranspose() 2871 @*/ 2872 int MatTranspose(Mat mat,Mat *B) 2873 { 2874 int ierr; 2875 2876 PetscFunctionBegin; 2877 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 2878 PetscValidType(mat,1); 2879 MatPreallocated(mat); 2880 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2881 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2882 if (!mat->ops->transpose) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 2883 2884 ierr = PetscLogEventBegin(MAT_Transpose,mat,0,0,0);CHKERRQ(ierr); 2885 ierr = (*mat->ops->transpose)(mat,B);CHKERRQ(ierr); 2886 ierr = PetscLogEventEnd(MAT_Transpose,mat,0,0,0);CHKERRQ(ierr); 2887 if (B) {ierr = PetscObjectIncreaseState((PetscObject)*B);CHKERRQ(ierr);} 2888 PetscFunctionReturn(0); 2889 } 2890 2891 #undef __FUNCT__ 2892 #define __FUNCT__ "MatIsTranspose" 2893 /*@C 2894 MatIsTranspose - Test whether a matrix is another one's transpose, 2895 or its own, in which case it tests symmetry. 2896 2897 Collective on Mat 2898 2899 Input Parameter: 2900 + A - the matrix to test 2901 - B - the matrix to test against, this can equal the first parameter 2902 2903 Output Parameters: 2904 . flg - the result 2905 2906 Notes: 2907 Only available for SeqAIJ/MPIAIJ matrices. The sequential algorithm 2908 has a running time of the order of the number of nonzeros; the parallel 2909 test involves parallel copies of the block-offdiagonal parts of the matrix. 2910 2911 Level: intermediate 2912 2913 Concepts: matrices^transposing, matrix^symmetry 2914 2915 .seealso: MatTranspose(), MatIsSymmetric(), MatIsHermitian() 2916 @*/ 2917 int MatIsTranspose(Mat A,Mat B,PetscReal tol,PetscTruth *flg) 2918 { 2919 int ierr,(*f)(Mat,Mat,PetscReal,PetscTruth*),(*g)(Mat,Mat,PetscReal,PetscTruth*); 2920 2921 PetscFunctionBegin; 2922 PetscValidHeaderSpecific(A,MAT_COOKIE,1); 2923 PetscValidHeaderSpecific(B,MAT_COOKIE,2); 2924 PetscValidPointer(flg,3); 2925 ierr = PetscObjectQueryFunction((PetscObject)A,"MatIsTranspose_C",(void (**)(void))&f);CHKERRQ(ierr); 2926 ierr = PetscObjectQueryFunction((PetscObject)B,"MatIsTranspose_C",(void (**)(void))&g);CHKERRQ(ierr); 2927 if (f && g) { 2928 if (f==g) { 2929 ierr = (*f)(A,B,tol,flg);CHKERRQ(ierr); 2930 } else { 2931 SETERRQ(1,"Matrices do not have the same comparator for symmetry test"); 2932 } 2933 } 2934 PetscFunctionReturn(0); 2935 } 2936 2937 #undef __FUNCT__ 2938 #define __FUNCT__ "MatPermute" 2939 /*@C 2940 MatPermute - Creates a new matrix with rows and columns permuted from the 2941 original. 2942 2943 Collective on Mat 2944 2945 Input Parameters: 2946 + mat - the matrix to permute 2947 . row - row permutation, each processor supplies only the permutation for its rows 2948 - col - column permutation, each processor needs the entire column permutation, that is 2949 this is the same size as the total number of columns in the matrix 2950 2951 Output Parameters: 2952 . B - the permuted matrix 2953 2954 Level: advanced 2955 2956 Concepts: matrices^permuting 2957 2958 .seealso: MatGetOrdering() 2959 @*/ 2960 int MatPermute(Mat mat,IS row,IS col,Mat *B) 2961 { 2962 int ierr; 2963 2964 PetscFunctionBegin; 2965 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 2966 PetscValidType(mat,1); 2967 MatPreallocated(mat); 2968 PetscValidHeaderSpecific(row,IS_COOKIE,2); 2969 PetscValidHeaderSpecific(col,IS_COOKIE,3); 2970 PetscValidPointer(B,4); 2971 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2972 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2973 if (!mat->ops->permute) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 2974 ierr = (*mat->ops->permute)(mat,row,col,B);CHKERRQ(ierr); 2975 ierr = PetscObjectIncreaseState((PetscObject)*B);CHKERRQ(ierr); 2976 PetscFunctionReturn(0); 2977 } 2978 2979 #undef __FUNCT__ 2980 #define __FUNCT__ "MatPermuteSparsify" 2981 /*@C 2982 MatPermuteSparsify - Creates a new matrix with rows and columns permuted from the 2983 original and sparsified to the prescribed tolerance. 2984 2985 Collective on Mat 2986 2987 Input Parameters: 2988 + A - The matrix to permute 2989 . band - The half-bandwidth of the sparsified matrix, or PETSC_DECIDE 2990 . frac - The half-bandwidth as a fraction of the total size, or 0.0 2991 . tol - The drop tolerance 2992 . rowp - The row permutation 2993 - colp - The column permutation 2994 2995 Output Parameter: 2996 . B - The permuted, sparsified matrix 2997 2998 Level: advanced 2999 3000 Note: 3001 The default behavior (band = PETSC_DECIDE and frac = 0.0) is to 3002 restrict the half-bandwidth of the resulting matrix to 5% of the 3003 total matrix size. 3004 3005 .keywords: matrix, permute, sparsify 3006 3007 .seealso: MatGetOrdering(), MatPermute() 3008 @*/ 3009 int MatPermuteSparsify(Mat A, int band, PetscReal frac, PetscReal tol, IS rowp, IS colp, Mat *B) 3010 { 3011 IS irowp, icolp; 3012 int *rows, *cols; 3013 int M, N, locRowStart, locRowEnd; 3014 int nz, newNz; 3015 int *cwork, *cnew; 3016 PetscScalar *vwork, *vnew; 3017 int bw, size; 3018 int row, locRow, newRow, col, newCol; 3019 int ierr; 3020 3021 PetscFunctionBegin; 3022 PetscValidHeaderSpecific(A, MAT_COOKIE,1); 3023 PetscValidHeaderSpecific(rowp, IS_COOKIE,5); 3024 PetscValidHeaderSpecific(colp, IS_COOKIE,6); 3025 PetscValidPointer(B,7); 3026 if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE, "Not for unassembled matrix"); 3027 if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix"); 3028 if (!A->ops->permutesparsify) { 3029 ierr = MatGetSize(A, &M, &N);CHKERRQ(ierr); 3030 ierr = MatGetOwnershipRange(A, &locRowStart, &locRowEnd);CHKERRQ(ierr); 3031 ierr = ISGetSize(rowp, &size);CHKERRQ(ierr); 3032 if (size != M) SETERRQ2(PETSC_ERR_ARG_WRONG, "Wrong size %d for row permutation, should be %d", size, M); 3033 ierr = ISGetSize(colp, &size);CHKERRQ(ierr); 3034 if (size != N) SETERRQ2(PETSC_ERR_ARG_WRONG, "Wrong size %d for column permutation, should be %d", size, N); 3035 ierr = ISInvertPermutation(rowp, 0, &irowp);CHKERRQ(ierr); 3036 ierr = ISGetIndices(irowp, &rows);CHKERRQ(ierr); 3037 ierr = ISInvertPermutation(colp, 0, &icolp);CHKERRQ(ierr); 3038 ierr = ISGetIndices(icolp, &cols);CHKERRQ(ierr); 3039 ierr = PetscMalloc(N * sizeof(int), &cnew);CHKERRQ(ierr); 3040 ierr = PetscMalloc(N * sizeof(PetscScalar), &vnew);CHKERRQ(ierr); 3041 3042 /* Setup bandwidth to include */ 3043 if (band == PETSC_DECIDE) { 3044 if (frac <= 0.0) 3045 bw = (int) (M * 0.05); 3046 else 3047 bw = (int) (M * frac); 3048 } else { 3049 if (band <= 0) SETERRQ(PETSC_ERR_ARG_WRONG, "Bandwidth must be a positive integer"); 3050 bw = band; 3051 } 3052 3053 /* Put values into new matrix */ 3054 ierr = MatDuplicate(A, MAT_DO_NOT_COPY_VALUES, B);CHKERRQ(ierr); 3055 for(row = locRowStart, locRow = 0; row < locRowEnd; row++, locRow++) { 3056 ierr = MatGetRow(A, row, &nz, &cwork, &vwork);CHKERRQ(ierr); 3057 newRow = rows[locRow]+locRowStart; 3058 for(col = 0, newNz = 0; col < nz; col++) { 3059 newCol = cols[cwork[col]]; 3060 if ((newCol >= newRow - bw) && (newCol < newRow + bw) && (PetscAbsScalar(vwork[col]) >= tol)) { 3061 cnew[newNz] = newCol; 3062 vnew[newNz] = vwork[col]; 3063 newNz++; 3064 } 3065 } 3066 ierr = MatSetValues(*B, 1, &newRow, newNz, cnew, vnew, INSERT_VALUES);CHKERRQ(ierr); 3067 ierr = MatRestoreRow(A, row, &nz, &cwork, &vwork);CHKERRQ(ierr); 3068 } 3069 ierr = PetscFree(cnew);CHKERRQ(ierr); 3070 ierr = PetscFree(vnew);CHKERRQ(ierr); 3071 ierr = MatAssemblyBegin(*B, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3072 ierr = MatAssemblyEnd(*B, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3073 ierr = ISRestoreIndices(irowp, &rows);CHKERRQ(ierr); 3074 ierr = ISRestoreIndices(icolp, &cols);CHKERRQ(ierr); 3075 ierr = ISDestroy(irowp);CHKERRQ(ierr); 3076 ierr = ISDestroy(icolp);CHKERRQ(ierr); 3077 } else { 3078 ierr = (*A->ops->permutesparsify)(A, band, frac, tol, rowp, colp, B);CHKERRQ(ierr); 3079 } 3080 ierr = PetscObjectIncreaseState((PetscObject)*B);CHKERRQ(ierr); 3081 PetscFunctionReturn(0); 3082 } 3083 3084 #undef __FUNCT__ 3085 #define __FUNCT__ "MatEqual" 3086 /*@ 3087 MatEqual - Compares two matrices. 3088 3089 Collective on Mat 3090 3091 Input Parameters: 3092 + A - the first matrix 3093 - B - the second matrix 3094 3095 Output Parameter: 3096 . flg - PETSC_TRUE if the matrices are equal; PETSC_FALSE otherwise. 3097 3098 Level: intermediate 3099 3100 Concepts: matrices^equality between 3101 @*/ 3102 int MatEqual(Mat A,Mat B,PetscTruth *flg) 3103 { 3104 int ierr; 3105 3106 PetscFunctionBegin; 3107 PetscValidHeaderSpecific(A,MAT_COOKIE,1); 3108 PetscValidHeaderSpecific(B,MAT_COOKIE,2); 3109 PetscValidType(A,1); 3110 MatPreallocated(A); 3111 PetscValidType(B,2); 3112 MatPreallocated(B); 3113 PetscValidIntPointer(flg,3); 3114 PetscCheckSameComm(A,1,B,2); 3115 if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3116 if (!B->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3117 if (A->M != B->M || A->N != B->N) SETERRQ4(PETSC_ERR_ARG_SIZ,"Mat A,Mat B: global dim %d %d %d %d",A->M,B->M,A->N,B->N); 3118 if (!A->ops->equal) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",A->type_name); 3119 if (!B->ops->equal) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",B->type_name); 3120 if (A->ops->equal != B->ops->equal) SETERRQ2(PETSC_ERR_ARG_INCOMP,"A is type: %s\nB is type: %s",A->type_name,B->type_name); 3121 ierr = (*A->ops->equal)(A,B,flg);CHKERRQ(ierr); 3122 PetscFunctionReturn(0); 3123 } 3124 3125 #undef __FUNCT__ 3126 #define __FUNCT__ "MatDiagonalScale" 3127 /*@ 3128 MatDiagonalScale - Scales a matrix on the left and right by diagonal 3129 matrices that are stored as vectors. Either of the two scaling 3130 matrices can be PETSC_NULL. 3131 3132 Collective on Mat 3133 3134 Input Parameters: 3135 + mat - the matrix to be scaled 3136 . l - the left scaling vector (or PETSC_NULL) 3137 - r - the right scaling vector (or PETSC_NULL) 3138 3139 Notes: 3140 MatDiagonalScale() computes A = LAR, where 3141 L = a diagonal matrix, R = a diagonal matrix 3142 3143 Level: intermediate 3144 3145 Concepts: matrices^diagonal scaling 3146 Concepts: diagonal scaling of matrices 3147 3148 .seealso: MatScale() 3149 @*/ 3150 int MatDiagonalScale(Mat mat,Vec l,Vec r) 3151 { 3152 int ierr; 3153 3154 PetscFunctionBegin; 3155 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 3156 PetscValidType(mat,1); 3157 MatPreallocated(mat); 3158 if (!mat->ops->diagonalscale) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 3159 if (l) {PetscValidHeaderSpecific(l,VEC_COOKIE,2);PetscCheckSameComm(mat,1,l,2);} 3160 if (r) {PetscValidHeaderSpecific(r,VEC_COOKIE,3);PetscCheckSameComm(mat,1,r,3);} 3161 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3162 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3163 3164 ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 3165 ierr = (*mat->ops->diagonalscale)(mat,l,r);CHKERRQ(ierr); 3166 ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 3167 ierr = PetscObjectIncreaseState((PetscObject)mat);CHKERRQ(ierr); 3168 PetscFunctionReturn(0); 3169 } 3170 3171 #undef __FUNCT__ 3172 #define __FUNCT__ "MatScale" 3173 /*@ 3174 MatScale - Scales all elements of a matrix by a given number. 3175 3176 Collective on Mat 3177 3178 Input Parameters: 3179 + mat - the matrix to be scaled 3180 - a - the scaling value 3181 3182 Output Parameter: 3183 . mat - the scaled matrix 3184 3185 Level: intermediate 3186 3187 Concepts: matrices^scaling all entries 3188 3189 .seealso: MatDiagonalScale() 3190 @*/ 3191 int MatScale(const PetscScalar *a,Mat mat) 3192 { 3193 int ierr; 3194 3195 PetscFunctionBegin; 3196 PetscValidScalarPointer(a,1); 3197 PetscValidHeaderSpecific(mat,MAT_COOKIE,2); 3198 PetscValidType(mat,2); 3199 MatPreallocated(mat); 3200 if (!mat->ops->scale) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 3201 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3202 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3203 3204 ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 3205 ierr = (*mat->ops->scale)(a,mat);CHKERRQ(ierr); 3206 ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 3207 ierr = PetscObjectIncreaseState((PetscObject)mat);CHKERRQ(ierr); 3208 PetscFunctionReturn(0); 3209 } 3210 3211 #undef __FUNCT__ 3212 #define __FUNCT__ "MatNorm" 3213 /*@ 3214 MatNorm - Calculates various norms of a matrix. 3215 3216 Collective on Mat 3217 3218 Input Parameters: 3219 + mat - the matrix 3220 - type - the type of norm, NORM_1, NORM_FROBENIUS, NORM_INFINITY 3221 3222 Output Parameters: 3223 . nrm - the resulting norm 3224 3225 Level: intermediate 3226 3227 Concepts: matrices^norm 3228 Concepts: norm^of matrix 3229 @*/ 3230 int MatNorm(Mat mat,NormType type,PetscReal *nrm) 3231 { 3232 int ierr; 3233 3234 PetscFunctionBegin; 3235 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 3236 PetscValidType(mat,1); 3237 MatPreallocated(mat); 3238 PetscValidScalarPointer(nrm,3); 3239 3240 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3241 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3242 if (!mat->ops->norm) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 3243 ierr = (*mat->ops->norm)(mat,type,nrm);CHKERRQ(ierr); 3244 PetscFunctionReturn(0); 3245 } 3246 3247 /* 3248 This variable is used to prevent counting of MatAssemblyBegin() that 3249 are called from within a MatAssemblyEnd(). 3250 */ 3251 static int MatAssemblyEnd_InUse = 0; 3252 #undef __FUNCT__ 3253 #define __FUNCT__ "MatAssemblyBegin" 3254 /*@ 3255 MatAssemblyBegin - Begins assembling the matrix. This routine should 3256 be called after completing all calls to MatSetValues(). 3257 3258 Collective on Mat 3259 3260 Input Parameters: 3261 + mat - the matrix 3262 - type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY 3263 3264 Notes: 3265 MatSetValues() generally caches the values. The matrix is ready to 3266 use only after MatAssemblyBegin() and MatAssemblyEnd() have been called. 3267 Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES 3268 in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before 3269 using the matrix. 3270 3271 Level: beginner 3272 3273 Concepts: matrices^assembling 3274 3275 .seealso: MatAssemblyEnd(), MatSetValues(), MatAssembled() 3276 @*/ 3277 int MatAssemblyBegin(Mat mat,MatAssemblyType type) 3278 { 3279 int ierr; 3280 3281 PetscFunctionBegin; 3282 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 3283 PetscValidType(mat,1); 3284 MatPreallocated(mat); 3285 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix.\nDid you forget to call MatSetUnfactored()?"); 3286 if (mat->assembled) { 3287 mat->was_assembled = PETSC_TRUE; 3288 mat->assembled = PETSC_FALSE; 3289 } 3290 if (!MatAssemblyEnd_InUse) { 3291 ierr = PetscLogEventBegin(MAT_AssemblyBegin,mat,0,0,0);CHKERRQ(ierr); 3292 if (mat->ops->assemblybegin){ierr = (*mat->ops->assemblybegin)(mat,type);CHKERRQ(ierr);} 3293 ierr = PetscLogEventEnd(MAT_AssemblyBegin,mat,0,0,0);CHKERRQ(ierr); 3294 } else { 3295 if (mat->ops->assemblybegin){ierr = (*mat->ops->assemblybegin)(mat,type);CHKERRQ(ierr);} 3296 } 3297 PetscFunctionReturn(0); 3298 } 3299 3300 #undef __FUNCT__ 3301 #define __FUNCT__ "MatAssembed" 3302 /*@ 3303 MatAssembled - Indicates if a matrix has been assembled and is ready for 3304 use; for example, in matrix-vector product. 3305 3306 Collective on Mat 3307 3308 Input Parameter: 3309 . mat - the matrix 3310 3311 Output Parameter: 3312 . assembled - PETSC_TRUE or PETSC_FALSE 3313 3314 Level: advanced 3315 3316 Concepts: matrices^assembled? 3317 3318 .seealso: MatAssemblyEnd(), MatSetValues(), MatAssemblyBegin() 3319 @*/ 3320 int MatAssembled(Mat mat,PetscTruth *assembled) 3321 { 3322 PetscFunctionBegin; 3323 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 3324 PetscValidType(mat,1); 3325 MatPreallocated(mat); 3326 PetscValidPointer(assembled,2); 3327 *assembled = mat->assembled; 3328 PetscFunctionReturn(0); 3329 } 3330 3331 #undef __FUNCT__ 3332 #define __FUNCT__ "MatView_Private" 3333 /* 3334 Processes command line options to determine if/how a matrix 3335 is to be viewed. Called by MatAssemblyEnd() and MatLoad(). 3336 */ 3337 int MatView_Private(Mat mat) 3338 { 3339 int ierr; 3340 PetscTruth flg; 3341 static PetscTruth incall = PETSC_FALSE; 3342 3343 PetscFunctionBegin; 3344 if (incall) PetscFunctionReturn(0); 3345 incall = PETSC_TRUE; 3346 ierr = PetscOptionsBegin(mat->comm,mat->prefix,"Matrix Options","Mat");CHKERRQ(ierr); 3347 ierr = PetscOptionsName("-mat_view_info","Information on matrix size","MatView",&flg);CHKERRQ(ierr); 3348 if (flg) { 3349 ierr = PetscViewerPushFormat(PETSC_VIEWER_STDOUT_(mat->comm),PETSC_VIEWER_ASCII_INFO);CHKERRQ(ierr); 3350 ierr = MatView(mat,PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr); 3351 ierr = PetscViewerPopFormat(PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr); 3352 } 3353 ierr = PetscOptionsName("-mat_view_info_detailed","Nonzeros in the matrix","MatView",&flg);CHKERRQ(ierr); 3354 if (flg) { 3355 ierr = PetscViewerPushFormat(PETSC_VIEWER_STDOUT_(mat->comm),PETSC_VIEWER_ASCII_INFO_DETAIL);CHKERRQ(ierr); 3356 ierr = MatView(mat,PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr); 3357 ierr = PetscViewerPopFormat(PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr); 3358 } 3359 ierr = PetscOptionsName("-mat_view","Print matrix to stdout","MatView",&flg);CHKERRQ(ierr); 3360 if (flg) { 3361 ierr = MatView(mat,PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr); 3362 } 3363 ierr = PetscOptionsName("-mat_view_matlab","Print matrix to stdout in a format Matlab can read","MatView",&flg);CHKERRQ(ierr); 3364 if (flg) { 3365 ierr = PetscViewerPushFormat(PETSC_VIEWER_STDOUT_(mat->comm),PETSC_VIEWER_ASCII_MATLAB);CHKERRQ(ierr); 3366 ierr = MatView(mat,PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr); 3367 ierr = PetscViewerPopFormat(PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr); 3368 } 3369 ierr = PetscOptionsName("-mat_view_socket","Send matrix to socket (can be read from matlab)","MatView",&flg);CHKERRQ(ierr); 3370 if (flg) { 3371 ierr = MatView(mat,PETSC_VIEWER_SOCKET_(mat->comm));CHKERRQ(ierr); 3372 ierr = PetscViewerFlush(PETSC_VIEWER_SOCKET_(mat->comm));CHKERRQ(ierr); 3373 } 3374 ierr = PetscOptionsName("-mat_view_binary","Save matrix to file in binary format","MatView",&flg);CHKERRQ(ierr); 3375 if (flg) { 3376 ierr = MatView(mat,PETSC_VIEWER_BINARY_(mat->comm));CHKERRQ(ierr); 3377 ierr = PetscViewerFlush(PETSC_VIEWER_BINARY_(mat->comm));CHKERRQ(ierr); 3378 } 3379 ierr = PetscOptionsEnd();CHKERRQ(ierr); 3380 /* cannot have inside PetscOptionsBegin() because uses PetscOptionsBegin() */ 3381 ierr = PetscOptionsHasName(mat->prefix,"-mat_view_draw",&flg);CHKERRQ(ierr); 3382 if (flg) { 3383 ierr = PetscOptionsHasName(mat->prefix,"-mat_view_contour",&flg);CHKERRQ(ierr); 3384 if (flg) { 3385 PetscViewerPushFormat(PETSC_VIEWER_DRAW_(mat->comm),PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr); 3386 } 3387 ierr = MatView(mat,PETSC_VIEWER_DRAW_(mat->comm));CHKERRQ(ierr); 3388 ierr = PetscViewerFlush(PETSC_VIEWER_DRAW_(mat->comm));CHKERRQ(ierr); 3389 if (flg) { 3390 PetscViewerPopFormat(PETSC_VIEWER_DRAW_(mat->comm));CHKERRQ(ierr); 3391 } 3392 } 3393 incall = PETSC_FALSE; 3394 PetscFunctionReturn(0); 3395 } 3396 3397 #undef __FUNCT__ 3398 #define __FUNCT__ "MatAssemblyEnd" 3399 /*@ 3400 MatAssemblyEnd - Completes assembling the matrix. This routine should 3401 be called after MatAssemblyBegin(). 3402 3403 Collective on Mat 3404 3405 Input Parameters: 3406 + mat - the matrix 3407 - type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY 3408 3409 Options Database Keys: 3410 + -mat_view_info - Prints info on matrix at conclusion of MatEndAssembly() 3411 . -mat_view_info_detailed - Prints more detailed info 3412 . -mat_view - Prints matrix in ASCII format 3413 . -mat_view_matlab - Prints matrix in Matlab format 3414 . -mat_view_draw - PetscDraws nonzero structure of matrix, using MatView() and PetscDrawOpenX(). 3415 . -display <name> - Sets display name (default is host) 3416 . -draw_pause <sec> - Sets number of seconds to pause after display 3417 . -mat_view_socket - Sends matrix to socket, can be accessed from Matlab (see users manual) 3418 . -viewer_socket_machine <machine> 3419 . -viewer_socket_port <port> 3420 . -mat_view_binary - save matrix to file in binary format 3421 - -viewer_binary_filename <name> 3422 3423 Notes: 3424 MatSetValues() generally caches the values. The matrix is ready to 3425 use only after MatAssemblyBegin() and MatAssemblyEnd() have been called. 3426 Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES 3427 in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before 3428 using the matrix. 3429 3430 Level: beginner 3431 3432 .seealso: MatAssemblyBegin(), MatSetValues(), PetscDrawOpenX(), MatView(), MatAssembled(), PetscViewerSocketOpen() 3433 @*/ 3434 int MatAssemblyEnd(Mat mat,MatAssemblyType type) 3435 { 3436 int ierr; 3437 static int inassm = 0; 3438 PetscTruth flg; 3439 3440 PetscFunctionBegin; 3441 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 3442 PetscValidType(mat,1); 3443 MatPreallocated(mat); 3444 3445 inassm++; 3446 MatAssemblyEnd_InUse++; 3447 if (MatAssemblyEnd_InUse == 1) { /* Do the logging only the first time through */ 3448 ierr = PetscLogEventBegin(MAT_AssemblyEnd,mat,0,0,0);CHKERRQ(ierr); 3449 if (mat->ops->assemblyend) { 3450 ierr = (*mat->ops->assemblyend)(mat,type);CHKERRQ(ierr); 3451 } 3452 ierr = PetscLogEventEnd(MAT_AssemblyEnd,mat,0,0,0);CHKERRQ(ierr); 3453 } else { 3454 if (mat->ops->assemblyend) { 3455 ierr = (*mat->ops->assemblyend)(mat,type);CHKERRQ(ierr); 3456 } 3457 } 3458 3459 /* Flush assembly is not a true assembly */ 3460 if (type != MAT_FLUSH_ASSEMBLY) { 3461 mat->assembled = PETSC_TRUE; mat->num_ass++; 3462 } 3463 mat->insertmode = NOT_SET_VALUES; 3464 MatAssemblyEnd_InUse--; 3465 ierr = PetscObjectIncreaseState((PetscObject)mat);CHKERRQ(ierr); 3466 if (!mat->symmetric_eternal) { 3467 mat->symmetric_set = PETSC_FALSE; 3468 mat->hermitian_set = PETSC_FALSE; 3469 mat->structurally_symmetric_set = PETSC_FALSE; 3470 } 3471 if (inassm == 1 && type != MAT_FLUSH_ASSEMBLY) { 3472 ierr = MatView_Private(mat);CHKERRQ(ierr); 3473 ierr = PetscOptionsHasName(mat->prefix,"-mat_is_symmetric",&flg);CHKERRQ(ierr); 3474 if (flg) { 3475 PetscReal tol = 0.0; 3476 ierr = PetscOptionsGetReal(mat->prefix,"-mat_is_symmetric",&tol,PETSC_NULL);CHKERRQ(ierr); 3477 ierr = MatIsSymmetric(mat,tol,&flg);CHKERRQ(ierr); 3478 if (flg) { 3479 ierr = PetscPrintf(mat->comm,"Matrix is symmetric (tolerance %g)\n",tol);CHKERRQ(ierr); 3480 } else { 3481 ierr = PetscPrintf(mat->comm,"Matrix is not symmetric (tolerance %g)\n",tol);CHKERRQ(ierr); 3482 } 3483 } 3484 } 3485 inassm--; 3486 ierr = PetscOptionsHasName(mat->prefix,"-help",&flg);CHKERRQ(ierr); 3487 if (flg) { 3488 ierr = MatPrintHelp(mat);CHKERRQ(ierr); 3489 } 3490 PetscFunctionReturn(0); 3491 } 3492 3493 3494 #undef __FUNCT__ 3495 #define __FUNCT__ "MatCompress" 3496 /*@ 3497 MatCompress - Tries to store the matrix in as little space as 3498 possible. May fail if memory is already fully used, since it 3499 tries to allocate new space. 3500 3501 Collective on Mat 3502 3503 Input Parameters: 3504 . mat - the matrix 3505 3506 Level: advanced 3507 3508 @*/ 3509 int MatCompress(Mat mat) 3510 { 3511 int ierr; 3512 3513 PetscFunctionBegin; 3514 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 3515 PetscValidType(mat,1); 3516 MatPreallocated(mat); 3517 if (mat->ops->compress) {ierr = (*mat->ops->compress)(mat);CHKERRQ(ierr);} 3518 PetscFunctionReturn(0); 3519 } 3520 3521 #undef __FUNCT__ 3522 #define __FUNCT__ "MatSetOption" 3523 /*@ 3524 MatSetOption - Sets a parameter option for a matrix. Some options 3525 may be specific to certain storage formats. Some options 3526 determine how values will be inserted (or added). Sorted, 3527 row-oriented input will generally assemble the fastest. The default 3528 is row-oriented, nonsorted input. 3529 3530 Collective on Mat 3531 3532 Input Parameters: 3533 + mat - the matrix 3534 - option - the option, one of those listed below (and possibly others), 3535 e.g., MAT_ROWS_SORTED, MAT_NEW_NONZERO_LOCATION_ERR 3536 3537 Options Describing Matrix Structure: 3538 + MAT_SYMMETRIC - symmetric in terms of both structure and value 3539 . MAT_HERMITIAN - transpose is the complex conjugation 3540 . MAT_STRUCTURALLY_SYMMETRIC - symmetric nonzero structure 3541 . MAT_NOT_SYMMETRIC - not symmetric in value 3542 . MAT_NOT_HERMITIAN - transpose is not the complex conjugation 3543 . MAT_NOT_STRUCTURALLY_SYMMETRIC - not symmetric nonzero structure 3544 . MAT_SYMMETRY_ETERNAL - if you would like the symmetry/Hermitian flag 3545 you set to be kept with all future use of the matrix 3546 including after MatAssemblyBegin/End() which could 3547 potentially change the symmetry structure, i.e. you 3548 KNOW the matrix will ALWAYS have the property you set. 3549 - MAT_NOT_SYMMETRY_ETERNAL - if MatAssemblyBegin/End() is called then the 3550 flags you set will be dropped (in case potentially 3551 the symmetry etc was lost). 3552 3553 Options For Use with MatSetValues(): 3554 Insert a logically dense subblock, which can be 3555 + MAT_ROW_ORIENTED - row-oriented (default) 3556 . MAT_COLUMN_ORIENTED - column-oriented 3557 . MAT_ROWS_SORTED - sorted by row 3558 . MAT_ROWS_UNSORTED - not sorted by row (default) 3559 . MAT_COLUMNS_SORTED - sorted by column 3560 - MAT_COLUMNS_UNSORTED - not sorted by column (default) 3561 3562 Not these options reflect the data you pass in with MatSetValues(); it has 3563 nothing to do with how the data is stored internally in the matrix 3564 data structure. 3565 3566 When (re)assembling a matrix, we can restrict the input for 3567 efficiency/debugging purposes. These options include 3568 + MAT_NO_NEW_NONZERO_LOCATIONS - additional insertions will not be 3569 allowed if they generate a new nonzero 3570 . MAT_YES_NEW_NONZERO_LOCATIONS - additional insertions will be allowed 3571 . MAT_NO_NEW_DIAGONALS - additional insertions will not be allowed if 3572 they generate a nonzero in a new diagonal (for block diagonal format only) 3573 . MAT_YES_NEW_DIAGONALS - new diagonals will be allowed (for block diagonal format only) 3574 . MAT_IGNORE_OFF_PROC_ENTRIES - drops off-processor entries 3575 . MAT_NEW_NONZERO_LOCATION_ERR - generates an error for new matrix entry 3576 - MAT_USE_HASH_TABLE - uses a hash table to speed up matrix assembly 3577 3578 Notes: 3579 Some options are relevant only for particular matrix types and 3580 are thus ignored by others. Other options are not supported by 3581 certain matrix types and will generate an error message if set. 3582 3583 If using a Fortran 77 module to compute a matrix, one may need to 3584 use the column-oriented option (or convert to the row-oriented 3585 format). 3586 3587 MAT_NO_NEW_NONZERO_LOCATIONS indicates that any add or insertion 3588 that would generate a new entry in the nonzero structure is instead 3589 ignored. Thus, if memory has not alredy been allocated for this particular 3590 data, then the insertion is ignored. For dense matrices, in which 3591 the entire array is allocated, no entries are ever ignored. 3592 Set after the first MatAssemblyEnd() 3593 3594 MAT_NEW_NONZERO_LOCATION_ERR indicates that any add or insertion 3595 that would generate a new entry in the nonzero structure instead produces 3596 an error. (Currently supported for AIJ and BAIJ formats only.) 3597 This is a useful flag when using SAME_NONZERO_PATTERN in calling 3598 KSPSetOperators() to ensure that the nonzero pattern truely does 3599 remain unchanged. Set after the first MatAssemblyEnd() 3600 3601 MAT_NEW_NONZERO_ALLOCATION_ERR indicates that any add or insertion 3602 that would generate a new entry that has not been preallocated will 3603 instead produce an error. (Currently supported for AIJ and BAIJ formats 3604 only.) This is a useful flag when debugging matrix memory preallocation. 3605 3606 MAT_IGNORE_OFF_PROC_ENTRIES indicates entries destined for 3607 other processors should be dropped, rather than stashed. 3608 This is useful if you know that the "owning" processor is also 3609 always generating the correct matrix entries, so that PETSc need 3610 not transfer duplicate entries generated on another processor. 3611 3612 MAT_USE_HASH_TABLE indicates that a hash table be used to improve the 3613 searches during matrix assembly. When this flag is set, the hash table 3614 is created during the first Matrix Assembly. This hash table is 3615 used the next time through, during MatSetVaules()/MatSetVaulesBlocked() 3616 to improve the searching of indices. MAT_NO_NEW_NONZERO_LOCATIONS flag 3617 should be used with MAT_USE_HASH_TABLE flag. This option is currently 3618 supported by MATMPIBAIJ format only. 3619 3620 MAT_KEEP_ZEROED_ROWS indicates when MatZeroRows() is called the zeroed entries 3621 are kept in the nonzero structure 3622 3623 MAT_IGNORE_ZERO_ENTRIES - for AIJ matrices this will stop zero values from creating 3624 a zero location in the matrix 3625 3626 MAT_USE_INODES - indicates using inode version of the code - works with AIJ and 3627 ROWBS matrix types 3628 3629 MAT_DO_NOT_USE_INODES - indicates not using inode version of the code - works 3630 with AIJ and ROWBS matrix types 3631 3632 Level: intermediate 3633 3634 Concepts: matrices^setting options 3635 3636 @*/ 3637 int MatSetOption(Mat mat,MatOption op) 3638 { 3639 int ierr; 3640 3641 PetscFunctionBegin; 3642 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 3643 PetscValidType(mat,1); 3644 MatPreallocated(mat); 3645 switch (op) { 3646 case MAT_SYMMETRIC: 3647 mat->symmetric = PETSC_TRUE; 3648 mat->structurally_symmetric = PETSC_TRUE; 3649 mat->symmetric_set = PETSC_TRUE; 3650 mat->structurally_symmetric_set = PETSC_TRUE; 3651 break; 3652 case MAT_HERMITIAN: 3653 mat->hermitian = PETSC_TRUE; 3654 mat->structurally_symmetric = PETSC_TRUE; 3655 mat->hermitian_set = PETSC_TRUE; 3656 mat->structurally_symmetric_set = PETSC_TRUE; 3657 break; 3658 case MAT_STRUCTURALLY_SYMMETRIC: 3659 mat->structurally_symmetric = PETSC_TRUE; 3660 mat->structurally_symmetric_set = PETSC_TRUE; 3661 break; 3662 case MAT_NOT_SYMMETRIC: 3663 mat->symmetric = PETSC_FALSE; 3664 mat->symmetric_set = PETSC_TRUE; 3665 break; 3666 case MAT_NOT_HERMITIAN: 3667 mat->hermitian = PETSC_FALSE; 3668 mat->hermitian_set = PETSC_TRUE; 3669 break; 3670 case MAT_NOT_STRUCTURALLY_SYMMETRIC: 3671 mat->structurally_symmetric = PETSC_FALSE; 3672 mat->structurally_symmetric_set = PETSC_TRUE; 3673 break; 3674 case MAT_SYMMETRY_ETERNAL: 3675 mat->symmetric_eternal = PETSC_TRUE; 3676 break; 3677 case MAT_NOT_SYMMETRY_ETERNAL: 3678 mat->symmetric_eternal = PETSC_FALSE; 3679 break; 3680 default: 3681 break; 3682 } 3683 if (mat->ops->setoption) { 3684 ierr = (*mat->ops->setoption)(mat,op);CHKERRQ(ierr); 3685 } 3686 PetscFunctionReturn(0); 3687 } 3688 3689 #undef __FUNCT__ 3690 #define __FUNCT__ "MatZeroEntries" 3691 /*@ 3692 MatZeroEntries - Zeros all entries of a matrix. For sparse matrices 3693 this routine retains the old nonzero structure. 3694 3695 Collective on Mat 3696 3697 Input Parameters: 3698 . mat - the matrix 3699 3700 Level: intermediate 3701 3702 Concepts: matrices^zeroing 3703 3704 .seealso: MatZeroRows() 3705 @*/ 3706 int MatZeroEntries(Mat mat) 3707 { 3708 int ierr; 3709 3710 PetscFunctionBegin; 3711 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 3712 PetscValidType(mat,1); 3713 MatPreallocated(mat); 3714 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3715 if (!mat->ops->zeroentries) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 3716 3717 ierr = PetscLogEventBegin(MAT_ZeroEntries,mat,0,0,0);CHKERRQ(ierr); 3718 ierr = (*mat->ops->zeroentries)(mat);CHKERRQ(ierr); 3719 ierr = PetscLogEventEnd(MAT_ZeroEntries,mat,0,0,0);CHKERRQ(ierr); 3720 ierr = PetscObjectIncreaseState((PetscObject)mat);CHKERRQ(ierr); 3721 PetscFunctionReturn(0); 3722 } 3723 3724 #undef __FUNCT__ 3725 #define __FUNCT__ "MatZeroRows" 3726 /*@C 3727 MatZeroRows - Zeros all entries (except possibly the main diagonal) 3728 of a set of rows of a matrix. 3729 3730 Collective on Mat 3731 3732 Input Parameters: 3733 + mat - the matrix 3734 . is - index set of rows to remove 3735 - diag - pointer to value put in all diagonals of eliminated rows. 3736 Note that diag is not a pointer to an array, but merely a 3737 pointer to a single value. 3738 3739 Notes: 3740 For the AIJ and BAIJ matrix formats this removes the old nonzero structure, 3741 but does not release memory. For the dense and block diagonal 3742 formats this does not alter the nonzero structure. 3743 3744 If the option MatSetOption(mat,MAT_KEEP_ZEROED_ROWS) the nonzero structure 3745 of the matrix is not changed (even for AIJ and BAIJ matrices) the values are 3746 merely zeroed. 3747 3748 The user can set a value in the diagonal entry (or for the AIJ and 3749 row formats can optionally remove the main diagonal entry from the 3750 nonzero structure as well, by passing a null pointer (PETSC_NULL 3751 in C or PETSC_NULL_SCALAR in Fortran) as the final argument). 3752 3753 For the parallel case, all processes that share the matrix (i.e., 3754 those in the communicator used for matrix creation) MUST call this 3755 routine, regardless of whether any rows being zeroed are owned by 3756 them. 3757 3758 Level: intermediate 3759 3760 Concepts: matrices^zeroing rows 3761 3762 .seealso: MatZeroEntries(), MatZeroRowsLocal(), MatSetOption() 3763 @*/ 3764 int MatZeroRows(Mat mat,IS is,const PetscScalar *diag) 3765 { 3766 int ierr; 3767 3768 PetscFunctionBegin; 3769 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 3770 PetscValidType(mat,1); 3771 MatPreallocated(mat); 3772 PetscValidHeaderSpecific(is,IS_COOKIE,2); 3773 if (diag) PetscValidScalarPointer(diag,3); 3774 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3775 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3776 if (!mat->ops->zerorows) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 3777 3778 ierr = (*mat->ops->zerorows)(mat,is,diag);CHKERRQ(ierr); 3779 ierr = MatView_Private(mat);CHKERRQ(ierr); 3780 ierr = PetscObjectIncreaseState((PetscObject)mat);CHKERRQ(ierr); 3781 PetscFunctionReturn(0); 3782 } 3783 3784 #undef __FUNCT__ 3785 #define __FUNCT__ "MatZeroRowsLocal" 3786 /*@C 3787 MatZeroRowsLocal - Zeros all entries (except possibly the main diagonal) 3788 of a set of rows of a matrix; using local numbering of rows. 3789 3790 Collective on Mat 3791 3792 Input Parameters: 3793 + mat - the matrix 3794 . is - index set of rows to remove 3795 - diag - pointer to value put in all diagonals of eliminated rows. 3796 Note that diag is not a pointer to an array, but merely a 3797 pointer to a single value. 3798 3799 Notes: 3800 Before calling MatZeroRowsLocal(), the user must first set the 3801 local-to-global mapping by calling MatSetLocalToGlobalMapping(). 3802 3803 For the AIJ matrix formats this removes the old nonzero structure, 3804 but does not release memory. For the dense and block diagonal 3805 formats this does not alter the nonzero structure. 3806 3807 If the option MatSetOption(mat,MAT_KEEP_ZEROED_ROWS) the nonzero structure 3808 of the matrix is not changed (even for AIJ and BAIJ matrices) the values are 3809 merely zeroed. 3810 3811 The user can set a value in the diagonal entry (or for the AIJ and 3812 row formats can optionally remove the main diagonal entry from the 3813 nonzero structure as well, by passing a null pointer (PETSC_NULL 3814 in C or PETSC_NULL_SCALAR in Fortran) as the final argument). 3815 3816 Level: intermediate 3817 3818 Concepts: matrices^zeroing 3819 3820 .seealso: MatZeroEntries(), MatZeroRows(), MatSetLocalToGlobalMapping 3821 @*/ 3822 int MatZeroRowsLocal(Mat mat,IS is,const PetscScalar *diag) 3823 { 3824 int ierr; 3825 IS newis; 3826 3827 PetscFunctionBegin; 3828 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 3829 PetscValidType(mat,1); 3830 MatPreallocated(mat); 3831 PetscValidHeaderSpecific(is,IS_COOKIE,2); 3832 if (diag) PetscValidScalarPointer(diag,3); 3833 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3834 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3835 3836 if (mat->ops->zerorowslocal) { 3837 ierr = (*mat->ops->zerorowslocal)(mat,is,diag);CHKERRQ(ierr); 3838 } else { 3839 if (!mat->mapping) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Need to provide local to global mapping to matrix first"); 3840 ierr = ISLocalToGlobalMappingApplyIS(mat->mapping,is,&newis);CHKERRQ(ierr); 3841 ierr = (*mat->ops->zerorows)(mat,newis,diag);CHKERRQ(ierr); 3842 ierr = ISDestroy(newis);CHKERRQ(ierr); 3843 } 3844 ierr = PetscObjectIncreaseState((PetscObject)mat);CHKERRQ(ierr); 3845 PetscFunctionReturn(0); 3846 } 3847 3848 #undef __FUNCT__ 3849 #define __FUNCT__ "MatGetSize" 3850 /*@ 3851 MatGetSize - Returns the numbers of rows and columns in a matrix. 3852 3853 Not Collective 3854 3855 Input Parameter: 3856 . mat - the matrix 3857 3858 Output Parameters: 3859 + m - the number of global rows 3860 - n - the number of global columns 3861 3862 Level: beginner 3863 3864 Concepts: matrices^size 3865 3866 .seealso: MatGetLocalSize() 3867 @*/ 3868 int MatGetSize(Mat mat,int *m,int* n) 3869 { 3870 PetscFunctionBegin; 3871 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 3872 if (m) *m = mat->M; 3873 if (n) *n = mat->N; 3874 PetscFunctionReturn(0); 3875 } 3876 3877 #undef __FUNCT__ 3878 #define __FUNCT__ "MatGetLocalSize" 3879 /*@ 3880 MatGetLocalSize - Returns the number of rows and columns in a matrix 3881 stored locally. This information may be implementation dependent, so 3882 use with care. 3883 3884 Not Collective 3885 3886 Input Parameters: 3887 . mat - the matrix 3888 3889 Output Parameters: 3890 + m - the number of local rows 3891 - n - the number of local columns 3892 3893 Level: beginner 3894 3895 Concepts: matrices^local size 3896 3897 .seealso: MatGetSize() 3898 @*/ 3899 int MatGetLocalSize(Mat mat,int *m,int* n) 3900 { 3901 PetscFunctionBegin; 3902 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 3903 if (m) PetscValidIntPointer(m,2); 3904 if (n) PetscValidIntPointer(n,3); 3905 if (m) *m = mat->m; 3906 if (n) *n = mat->n; 3907 PetscFunctionReturn(0); 3908 } 3909 3910 #undef __FUNCT__ 3911 #define __FUNCT__ "MatGetOwnershipRange" 3912 /*@ 3913 MatGetOwnershipRange - Returns the range of matrix rows owned by 3914 this processor, assuming that the matrix is laid out with the first 3915 n1 rows on the first processor, the next n2 rows on the second, etc. 3916 For certain parallel layouts this range may not be well defined. 3917 3918 Not Collective 3919 3920 Input Parameters: 3921 . mat - the matrix 3922 3923 Output Parameters: 3924 + m - the global index of the first local row 3925 - n - one more than the global index of the last local row 3926 3927 Level: beginner 3928 3929 Concepts: matrices^row ownership 3930 @*/ 3931 int MatGetOwnershipRange(Mat mat,int *m,int* n) 3932 { 3933 int ierr; 3934 3935 PetscFunctionBegin; 3936 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 3937 PetscValidType(mat,1); 3938 MatPreallocated(mat); 3939 if (m) PetscValidIntPointer(m,2); 3940 if (n) PetscValidIntPointer(n,3); 3941 ierr = PetscMapGetLocalRange(mat->rmap,m,n);CHKERRQ(ierr); 3942 PetscFunctionReturn(0); 3943 } 3944 3945 #undef __FUNCT__ 3946 #define __FUNCT__ "MatILUFactorSymbolic" 3947 /*@ 3948 MatILUFactorSymbolic - Performs symbolic ILU factorization of a matrix. 3949 Uses levels of fill only, not drop tolerance. Use MatLUFactorNumeric() 3950 to complete the factorization. 3951 3952 Collective on Mat 3953 3954 Input Parameters: 3955 + mat - the matrix 3956 . row - row permutation 3957 . column - column permutation 3958 - info - structure containing 3959 $ levels - number of levels of fill. 3960 $ expected fill - as ratio of original fill. 3961 $ 1 or 0 - indicating force fill on diagonal (improves robustness for matrices 3962 missing diagonal entries) 3963 3964 Output Parameters: 3965 . fact - new matrix that has been symbolically factored 3966 3967 Notes: 3968 See the users manual for additional information about 3969 choosing the fill factor for better efficiency. 3970 3971 Most users should employ the simplified KSP interface for linear solvers 3972 instead of working directly with matrix algebra routines such as this. 3973 See, e.g., KSPCreate(). 3974 3975 Level: developer 3976 3977 Concepts: matrices^symbolic LU factorization 3978 Concepts: matrices^factorization 3979 Concepts: LU^symbolic factorization 3980 3981 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor() 3982 MatGetOrdering(), MatFactorInfo 3983 3984 @*/ 3985 int MatILUFactorSymbolic(Mat mat,IS row,IS col,MatFactorInfo *info,Mat *fact) 3986 { 3987 int ierr; 3988 3989 PetscFunctionBegin; 3990 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 3991 PetscValidType(mat,1); 3992 MatPreallocated(mat); 3993 PetscValidHeaderSpecific(row,IS_COOKIE,2); 3994 PetscValidHeaderSpecific(col,IS_COOKIE,3); 3995 PetscValidPointer(info,4); 3996 PetscValidPointer(fact,5); 3997 if (info->levels < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Levels of fill negative %d",(int)info->levels); 3998 if (info->fill < 1.0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %g",info->fill); 3999 if (!mat->ops->ilufactorsymbolic) SETERRQ1(PETSC_ERR_SUP,"Matrix type %s symbolic ILU",mat->type_name); 4000 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4001 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4002 4003 ierr = PetscLogEventBegin(MAT_ILUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr); 4004 ierr = (*mat->ops->ilufactorsymbolic)(mat,row,col,info,fact);CHKERRQ(ierr); 4005 ierr = PetscLogEventEnd(MAT_ILUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr); 4006 PetscFunctionReturn(0); 4007 } 4008 4009 #undef __FUNCT__ 4010 #define __FUNCT__ "MatICCFactorSymbolic" 4011 /*@ 4012 MatICCFactorSymbolic - Performs symbolic incomplete 4013 Cholesky factorization for a symmetric matrix. Use 4014 MatCholeskyFactorNumeric() to complete the factorization. 4015 4016 Collective on Mat 4017 4018 Input Parameters: 4019 + mat - the matrix 4020 . perm - row and column permutation 4021 - info - structure containing 4022 $ levels - number of levels of fill. 4023 $ expected fill - as ratio of original fill. 4024 4025 Output Parameter: 4026 . fact - the factored matrix 4027 4028 Notes: 4029 Currently only no-fill factorization is supported. 4030 4031 Most users should employ the simplified KSP interface for linear solvers 4032 instead of working directly with matrix algebra routines such as this. 4033 See, e.g., KSPCreate(). 4034 4035 Level: developer 4036 4037 Concepts: matrices^symbolic incomplete Cholesky factorization 4038 Concepts: matrices^factorization 4039 Concepts: Cholsky^symbolic factorization 4040 4041 .seealso: MatCholeskyFactorNumeric(), MatCholeskyFactor(), MatFactorInfo 4042 @*/ 4043 int MatICCFactorSymbolic(Mat mat,IS perm,MatFactorInfo *info,Mat *fact) 4044 { 4045 int ierr; 4046 4047 PetscFunctionBegin; 4048 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 4049 PetscValidType(mat,1); 4050 MatPreallocated(mat); 4051 PetscValidHeaderSpecific(perm,IS_COOKIE,2); 4052 PetscValidPointer(info,3); 4053 PetscValidPointer(fact,4); 4054 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4055 if (info->levels < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Levels negative %d",(int) info->levels); 4056 if (info->fill < 1.0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %g",info->fill); 4057 if (!mat->ops->iccfactorsymbolic) SETERRQ1(PETSC_ERR_SUP,"Matrix type %s symbolic ICC",mat->type_name); 4058 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4059 4060 ierr = PetscLogEventBegin(MAT_ICCFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr); 4061 ierr = (*mat->ops->iccfactorsymbolic)(mat,perm,info,fact);CHKERRQ(ierr); 4062 ierr = PetscLogEventEnd(MAT_ICCFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr); 4063 PetscFunctionReturn(0); 4064 } 4065 4066 #undef __FUNCT__ 4067 #define __FUNCT__ "MatGetArray" 4068 /*@C 4069 MatGetArray - Returns a pointer to the element values in the matrix. 4070 The result of this routine is dependent on the underlying matrix data 4071 structure, and may not even work for certain matrix types. You MUST 4072 call MatRestoreArray() when you no longer need to access the array. 4073 4074 Not Collective 4075 4076 Input Parameter: 4077 . mat - the matrix 4078 4079 Output Parameter: 4080 . v - the location of the values 4081 4082 4083 Fortran Note: 4084 This routine is used differently from Fortran, e.g., 4085 .vb 4086 Mat mat 4087 PetscScalar mat_array(1) 4088 PetscOffset i_mat 4089 int ierr 4090 call MatGetArray(mat,mat_array,i_mat,ierr) 4091 4092 C Access first local entry in matrix; note that array is 4093 C treated as one dimensional 4094 value = mat_array(i_mat + 1) 4095 4096 [... other code ...] 4097 call MatRestoreArray(mat,mat_array,i_mat,ierr) 4098 .ve 4099 4100 See the Fortran chapter of the users manual and 4101 petsc/src/mat/examples/tests for details. 4102 4103 Level: advanced 4104 4105 Concepts: matrices^access array 4106 4107 .seealso: MatRestoreArray(), MatGetArrayF90() 4108 @*/ 4109 int MatGetArray(Mat mat,PetscScalar *v[]) 4110 { 4111 int ierr; 4112 4113 PetscFunctionBegin; 4114 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 4115 PetscValidType(mat,1); 4116 MatPreallocated(mat); 4117 PetscValidPointer(v,2); 4118 if (!mat->ops->getarray) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 4119 ierr = (*mat->ops->getarray)(mat,v);CHKERRQ(ierr); 4120 PetscFunctionReturn(0); 4121 } 4122 4123 #undef __FUNCT__ 4124 #define __FUNCT__ "MatRestoreArray" 4125 /*@C 4126 MatRestoreArray - Restores the matrix after MatGetArray() has been called. 4127 4128 Not Collective 4129 4130 Input Parameter: 4131 + mat - the matrix 4132 - v - the location of the values 4133 4134 Fortran Note: 4135 This routine is used differently from Fortran, e.g., 4136 .vb 4137 Mat mat 4138 PetscScalar mat_array(1) 4139 PetscOffset i_mat 4140 int ierr 4141 call MatGetArray(mat,mat_array,i_mat,ierr) 4142 4143 C Access first local entry in matrix; note that array is 4144 C treated as one dimensional 4145 value = mat_array(i_mat + 1) 4146 4147 [... other code ...] 4148 call MatRestoreArray(mat,mat_array,i_mat,ierr) 4149 .ve 4150 4151 See the Fortran chapter of the users manual and 4152 petsc/src/mat/examples/tests for details 4153 4154 Level: advanced 4155 4156 .seealso: MatGetArray(), MatRestoreArrayF90() 4157 @*/ 4158 int MatRestoreArray(Mat mat,PetscScalar *v[]) 4159 { 4160 int ierr; 4161 4162 PetscFunctionBegin; 4163 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 4164 PetscValidType(mat,1); 4165 MatPreallocated(mat); 4166 PetscValidPointer(v,2); 4167 #if defined(PETSC_USE_BOPT_g) 4168 CHKMEMQ; 4169 #endif 4170 if (!mat->ops->restorearray) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 4171 ierr = (*mat->ops->restorearray)(mat,v);CHKERRQ(ierr); 4172 ierr = PetscObjectIncreaseState((PetscObject)mat);CHKERRQ(ierr); 4173 PetscFunctionReturn(0); 4174 } 4175 4176 #undef __FUNCT__ 4177 #define __FUNCT__ "MatGetSubMatrices" 4178 /*@C 4179 MatGetSubMatrices - Extracts several submatrices from a matrix. If submat 4180 points to an array of valid matrices, they may be reused to store the new 4181 submatrices. 4182 4183 Collective on Mat 4184 4185 Input Parameters: 4186 + mat - the matrix 4187 . n - the number of submatrixes to be extracted (on this processor, may be zero) 4188 . irow, icol - index sets of rows and columns to extract 4189 - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 4190 4191 Output Parameter: 4192 . submat - the array of submatrices 4193 4194 Notes: 4195 MatGetSubMatrices() can extract only sequential submatrices 4196 (from both sequential and parallel matrices). Use MatGetSubMatrix() 4197 to extract a parallel submatrix. 4198 4199 When extracting submatrices from a parallel matrix, each processor can 4200 form a different submatrix by setting the rows and columns of its 4201 individual index sets according to the local submatrix desired. 4202 4203 When finished using the submatrices, the user should destroy 4204 them with MatDestroyMatrices(). 4205 4206 MAT_REUSE_MATRIX can only be used when the nonzero structure of the 4207 original matrix has not changed from that last call to MatGetSubMatrices(). 4208 4209 This routine creates the matrices in submat; you should NOT create them before 4210 calling it. It also allocates the array of matrix pointers submat. 4211 4212 Fortran Note: 4213 The Fortran interface is slightly different from that given below; it 4214 requires one to pass in as submat a Mat (integer) array of size at least m. 4215 4216 Level: advanced 4217 4218 Concepts: matrices^accessing submatrices 4219 Concepts: submatrices 4220 4221 .seealso: MatDestroyMatrices(), MatGetSubMatrix(), MatGetRow(), MatGetDiagonal() 4222 @*/ 4223 int MatGetSubMatrices(Mat mat,int n,const IS irow[],const IS icol[],MatReuse scall,Mat *submat[]) 4224 { 4225 int i,ierr; 4226 PetscTruth eq; 4227 4228 PetscFunctionBegin; 4229 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 4230 PetscValidType(mat,1); 4231 MatPreallocated(mat); 4232 if (n) { 4233 PetscValidPointer(irow,3); 4234 PetscValidHeaderSpecific(*irow,IS_COOKIE,3); 4235 PetscValidPointer(icol,4); 4236 PetscValidHeaderSpecific(*icol,IS_COOKIE,4); 4237 } 4238 PetscValidPointer(submat,6); 4239 if (n && scall == MAT_REUSE_MATRIX) { 4240 PetscValidPointer(*submat,6); 4241 PetscValidHeaderSpecific(**submat,MAT_COOKIE,6); 4242 } 4243 if (!mat->ops->getsubmatrices) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 4244 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4245 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4246 4247 ierr = PetscLogEventBegin(MAT_GetSubMatrices,mat,0,0,0);CHKERRQ(ierr); 4248 ierr = (*mat->ops->getsubmatrices)(mat,n,irow,icol,scall,submat);CHKERRQ(ierr); 4249 ierr = PetscLogEventEnd(MAT_GetSubMatrices,mat,0,0,0);CHKERRQ(ierr); 4250 for (i=0; i<n; i++) { 4251 if (mat->symmetric || mat->structurally_symmetric || mat->hermitian) { 4252 ierr = ISEqual(irow[i],icol[i],&eq);CHKERRQ(ierr); 4253 if (eq) { 4254 if (mat->symmetric){ 4255 ierr = MatSetOption((*submat)[i],MAT_SYMMETRIC);CHKERRQ(ierr); 4256 } else if (mat->hermitian) { 4257 ierr = MatSetOption((*submat)[i],MAT_HERMITIAN);CHKERRQ(ierr); 4258 } else if (mat->structurally_symmetric) { 4259 ierr = MatSetOption((*submat)[i],MAT_STRUCTURALLY_SYMMETRIC);CHKERRQ(ierr); 4260 } 4261 } 4262 } 4263 } 4264 PetscFunctionReturn(0); 4265 } 4266 4267 #undef __FUNCT__ 4268 #define __FUNCT__ "MatDestroyMatrices" 4269 /*@C 4270 MatDestroyMatrices - Destroys a set of matrices obtained with MatGetSubMatrices(). 4271 4272 Collective on Mat 4273 4274 Input Parameters: 4275 + n - the number of local matrices 4276 - mat - the matrices (note that this is a pointer to the array of matrices, just to match the calling 4277 sequence of MatGetSubMatrices()) 4278 4279 Level: advanced 4280 4281 Notes: Frees not only the matrices, but also the array that contains the matrices 4282 4283 .seealso: MatGetSubMatrices() 4284 @*/ 4285 int MatDestroyMatrices(int n,Mat *mat[]) 4286 { 4287 int ierr,i; 4288 4289 PetscFunctionBegin; 4290 if (n < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Trying to destroy negative number of matrices %d",n); 4291 PetscValidPointer(mat,2); 4292 for (i=0; i<n; i++) { 4293 ierr = MatDestroy((*mat)[i]);CHKERRQ(ierr); 4294 } 4295 /* memory is allocated even if n = 0 */ 4296 ierr = PetscFree(*mat);CHKERRQ(ierr); 4297 PetscFunctionReturn(0); 4298 } 4299 4300 #undef __FUNCT__ 4301 #define __FUNCT__ "MatIncreaseOverlap" 4302 /*@ 4303 MatIncreaseOverlap - Given a set of submatrices indicated by index sets, 4304 replaces the index sets by larger ones that represent submatrices with 4305 additional overlap. 4306 4307 Collective on Mat 4308 4309 Input Parameters: 4310 + mat - the matrix 4311 . n - the number of index sets 4312 . is - the array of index sets (these index sets will changed during the call) 4313 - ov - the additional overlap requested 4314 4315 Level: developer 4316 4317 Concepts: overlap 4318 Concepts: ASM^computing overlap 4319 4320 .seealso: MatGetSubMatrices() 4321 @*/ 4322 int MatIncreaseOverlap(Mat mat,int n,IS is[],int ov) 4323 { 4324 int ierr; 4325 4326 PetscFunctionBegin; 4327 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 4328 PetscValidType(mat,1); 4329 MatPreallocated(mat); 4330 if (n < 0) SETERRQ1(1,"Must have one or more domains, you have %d",n); 4331 if (n) { 4332 PetscValidPointer(is,3); 4333 PetscValidHeaderSpecific(*is,IS_COOKIE,3); 4334 } 4335 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4336 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4337 4338 if (!ov) PetscFunctionReturn(0); 4339 if (!mat->ops->increaseoverlap) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 4340 ierr = PetscLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr); 4341 ierr = (*mat->ops->increaseoverlap)(mat,n,is,ov);CHKERRQ(ierr); 4342 ierr = PetscLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr); 4343 PetscFunctionReturn(0); 4344 } 4345 4346 #undef __FUNCT__ 4347 #define __FUNCT__ "MatPrintHelp" 4348 /*@ 4349 MatPrintHelp - Prints all the options for the matrix. 4350 4351 Collective on Mat 4352 4353 Input Parameter: 4354 . mat - the matrix 4355 4356 Options Database Keys: 4357 + -help - Prints matrix options 4358 - -h - Prints matrix options 4359 4360 Level: developer 4361 4362 .seealso: MatCreate(), MatCreateXXX() 4363 @*/ 4364 int MatPrintHelp(Mat mat) 4365 { 4366 static PetscTruth called = PETSC_FALSE; 4367 int ierr; 4368 4369 PetscFunctionBegin; 4370 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 4371 PetscValidType(mat,1); 4372 MatPreallocated(mat); 4373 4374 if (!called) { 4375 if (mat->ops->printhelp) { 4376 ierr = (*mat->ops->printhelp)(mat);CHKERRQ(ierr); 4377 } 4378 called = PETSC_TRUE; 4379 } 4380 PetscFunctionReturn(0); 4381 } 4382 4383 #undef __FUNCT__ 4384 #define __FUNCT__ "MatGetBlockSize" 4385 /*@ 4386 MatGetBlockSize - Returns the matrix block size; useful especially for the 4387 block row and block diagonal formats. 4388 4389 Not Collective 4390 4391 Input Parameter: 4392 . mat - the matrix 4393 4394 Output Parameter: 4395 . bs - block size 4396 4397 Notes: 4398 Block diagonal formats are MATSEQBDIAG, MATMPIBDIAG. 4399 Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ 4400 4401 Level: intermediate 4402 4403 Concepts: matrices^block size 4404 4405 .seealso: MatCreateSeqBAIJ(), MatCreateMPIBAIJ(), MatCreateSeqBDiag(), MatCreateMPIBDiag() 4406 @*/ 4407 int MatGetBlockSize(Mat mat,int *bs) 4408 { 4409 int ierr; 4410 4411 PetscFunctionBegin; 4412 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 4413 PetscValidType(mat,1); 4414 MatPreallocated(mat); 4415 PetscValidIntPointer(bs,2); 4416 if (!mat->ops->getblocksize) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 4417 ierr = (*mat->ops->getblocksize)(mat,bs);CHKERRQ(ierr); 4418 PetscFunctionReturn(0); 4419 } 4420 4421 #undef __FUNCT__ 4422 #define __FUNCT__ "MatGetRowIJ" 4423 /*@C 4424 MatGetRowIJ - Returns the compressed row storage i and j indices for sequential matrices. 4425 4426 Collective on Mat 4427 4428 Input Parameters: 4429 + mat - the matrix 4430 . shift - 0 or 1 indicating we want the indices starting at 0 or 1 4431 - symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 4432 symmetrized 4433 4434 Output Parameters: 4435 + n - number of rows in the (possibly compressed) matrix 4436 . ia - the row pointers 4437 . ja - the column indices 4438 - done - PETSC_TRUE or PETSC_FALSE, indicating whether the values have been returned 4439 4440 Level: developer 4441 4442 .seealso: MatGetColumnIJ(), MatRestoreRowIJ() 4443 @*/ 4444 int MatGetRowIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int *ia[],int* ja[],PetscTruth *done) 4445 { 4446 int ierr; 4447 4448 PetscFunctionBegin; 4449 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 4450 PetscValidType(mat,1); 4451 MatPreallocated(mat); 4452 PetscValidIntPointer(n,4); 4453 if (ia) PetscValidIntPointer(ia,5); 4454 if (ja) PetscValidIntPointer(ja,6); 4455 PetscValidIntPointer(done,7); 4456 if (!mat->ops->getrowij) *done = PETSC_FALSE; 4457 else { 4458 *done = PETSC_TRUE; 4459 ierr = (*mat->ops->getrowij)(mat,shift,symmetric,n,ia,ja,done);CHKERRQ(ierr); 4460 } 4461 PetscFunctionReturn(0); 4462 } 4463 4464 #undef __FUNCT__ 4465 #define __FUNCT__ "MatGetColumnIJ" 4466 /*@C 4467 MatGetColumnIJ - Returns the compressed column storage i and j indices for sequential matrices. 4468 4469 Collective on Mat 4470 4471 Input Parameters: 4472 + mat - the matrix 4473 . shift - 1 or zero indicating we want the indices starting at 0 or 1 4474 - symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 4475 symmetrized 4476 4477 Output Parameters: 4478 + n - number of columns in the (possibly compressed) matrix 4479 . ia - the column pointers 4480 . ja - the row indices 4481 - done - PETSC_TRUE or PETSC_FALSE, indicating whether the values have been returned 4482 4483 Level: developer 4484 4485 .seealso: MatGetRowIJ(), MatRestoreColumnIJ() 4486 @*/ 4487 int MatGetColumnIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int *ia[],int* ja[],PetscTruth *done) 4488 { 4489 int ierr; 4490 4491 PetscFunctionBegin; 4492 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 4493 PetscValidType(mat,1); 4494 MatPreallocated(mat); 4495 PetscValidIntPointer(n,4); 4496 if (ia) PetscValidIntPointer(ia,5); 4497 if (ja) PetscValidIntPointer(ja,6); 4498 PetscValidIntPointer(done,7); 4499 4500 if (!mat->ops->getcolumnij) *done = PETSC_FALSE; 4501 else { 4502 *done = PETSC_TRUE; 4503 ierr = (*mat->ops->getcolumnij)(mat,shift,symmetric,n,ia,ja,done);CHKERRQ(ierr); 4504 } 4505 PetscFunctionReturn(0); 4506 } 4507 4508 #undef __FUNCT__ 4509 #define __FUNCT__ "MatRestoreRowIJ" 4510 /*@C 4511 MatRestoreRowIJ - Call after you are completed with the ia,ja indices obtained with 4512 MatGetRowIJ(). 4513 4514 Collective on Mat 4515 4516 Input Parameters: 4517 + mat - the matrix 4518 . shift - 1 or zero indicating we want the indices starting at 0 or 1 4519 - symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 4520 symmetrized 4521 4522 Output Parameters: 4523 + n - size of (possibly compressed) matrix 4524 . ia - the row pointers 4525 . ja - the column indices 4526 - done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned 4527 4528 Level: developer 4529 4530 .seealso: MatGetRowIJ(), MatRestoreColumnIJ() 4531 @*/ 4532 int MatRestoreRowIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int *ia[],int* ja[],PetscTruth *done) 4533 { 4534 int ierr; 4535 4536 PetscFunctionBegin; 4537 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 4538 PetscValidType(mat,1); 4539 MatPreallocated(mat); 4540 if (ia) PetscValidIntPointer(ia,5); 4541 if (ja) PetscValidIntPointer(ja,6); 4542 PetscValidIntPointer(done,7); 4543 4544 if (!mat->ops->restorerowij) *done = PETSC_FALSE; 4545 else { 4546 *done = PETSC_TRUE; 4547 ierr = (*mat->ops->restorerowij)(mat,shift,symmetric,n,ia,ja,done);CHKERRQ(ierr); 4548 } 4549 PetscFunctionReturn(0); 4550 } 4551 4552 #undef __FUNCT__ 4553 #define __FUNCT__ "MatRestoreColumnIJ" 4554 /*@C 4555 MatRestoreColumnIJ - Call after you are completed with the ia,ja indices obtained with 4556 MatGetColumnIJ(). 4557 4558 Collective on Mat 4559 4560 Input Parameters: 4561 + mat - the matrix 4562 . shift - 1 or zero indicating we want the indices starting at 0 or 1 4563 - symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 4564 symmetrized 4565 4566 Output Parameters: 4567 + n - size of (possibly compressed) matrix 4568 . ia - the column pointers 4569 . ja - the row indices 4570 - done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned 4571 4572 Level: developer 4573 4574 .seealso: MatGetColumnIJ(), MatRestoreRowIJ() 4575 @*/ 4576 int MatRestoreColumnIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int *ia[],int* ja[],PetscTruth *done) 4577 { 4578 int ierr; 4579 4580 PetscFunctionBegin; 4581 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 4582 PetscValidType(mat,1); 4583 MatPreallocated(mat); 4584 if (ia) PetscValidIntPointer(ia,5); 4585 if (ja) PetscValidIntPointer(ja,6); 4586 PetscValidIntPointer(done,7); 4587 4588 if (!mat->ops->restorecolumnij) *done = PETSC_FALSE; 4589 else { 4590 *done = PETSC_TRUE; 4591 ierr = (*mat->ops->restorecolumnij)(mat,shift,symmetric,n,ia,ja,done);CHKERRQ(ierr); 4592 } 4593 PetscFunctionReturn(0); 4594 } 4595 4596 #undef __FUNCT__ 4597 #define __FUNCT__ "MatColoringPatch" 4598 /*@C 4599 MatColoringPatch -Used inside matrix coloring routines that 4600 use MatGetRowIJ() and/or MatGetColumnIJ(). 4601 4602 Collective on Mat 4603 4604 Input Parameters: 4605 + mat - the matrix 4606 . n - number of colors 4607 - colorarray - array indicating color for each column 4608 4609 Output Parameters: 4610 . iscoloring - coloring generated using colorarray information 4611 4612 Level: developer 4613 4614 .seealso: MatGetRowIJ(), MatGetColumnIJ() 4615 4616 @*/ 4617 int MatColoringPatch(Mat mat,int n,int ncolors,const ISColoringValue colorarray[],ISColoring *iscoloring) 4618 { 4619 int ierr; 4620 4621 PetscFunctionBegin; 4622 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 4623 PetscValidType(mat,1); 4624 MatPreallocated(mat); 4625 PetscValidIntPointer(colorarray,4); 4626 PetscValidPointer(iscoloring,5); 4627 4628 if (!mat->ops->coloringpatch){ 4629 ierr = ISColoringCreate(mat->comm,n,colorarray,iscoloring);CHKERRQ(ierr); 4630 } else { 4631 ierr = (*mat->ops->coloringpatch)(mat,n,ncolors,colorarray,iscoloring);CHKERRQ(ierr); 4632 } 4633 PetscFunctionReturn(0); 4634 } 4635 4636 4637 #undef __FUNCT__ 4638 #define __FUNCT__ "MatSetUnfactored" 4639 /*@ 4640 MatSetUnfactored - Resets a factored matrix to be treated as unfactored. 4641 4642 Collective on Mat 4643 4644 Input Parameter: 4645 . mat - the factored matrix to be reset 4646 4647 Notes: 4648 This routine should be used only with factored matrices formed by in-place 4649 factorization via ILU(0) (or by in-place LU factorization for the MATSEQDENSE 4650 format). This option can save memory, for example, when solving nonlinear 4651 systems with a matrix-free Newton-Krylov method and a matrix-based, in-place 4652 ILU(0) preconditioner. 4653 4654 Note that one can specify in-place ILU(0) factorization by calling 4655 .vb 4656 PCType(pc,PCILU); 4657 PCILUSeUseInPlace(pc); 4658 .ve 4659 or by using the options -pc_type ilu -pc_ilu_in_place 4660 4661 In-place factorization ILU(0) can also be used as a local 4662 solver for the blocks within the block Jacobi or additive Schwarz 4663 methods (runtime option: -sub_pc_ilu_in_place). See the discussion 4664 of these preconditioners in the users manual for details on setting 4665 local solver options. 4666 4667 Most users should employ the simplified KSP interface for linear solvers 4668 instead of working directly with matrix algebra routines such as this. 4669 See, e.g., KSPCreate(). 4670 4671 Level: developer 4672 4673 .seealso: PCILUSetUseInPlace(), PCLUSetUseInPlace() 4674 4675 Concepts: matrices^unfactored 4676 4677 @*/ 4678 int MatSetUnfactored(Mat mat) 4679 { 4680 int ierr; 4681 4682 PetscFunctionBegin; 4683 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 4684 PetscValidType(mat,1); 4685 MatPreallocated(mat); 4686 mat->factor = 0; 4687 if (!mat->ops->setunfactored) PetscFunctionReturn(0); 4688 ierr = (*mat->ops->setunfactored)(mat);CHKERRQ(ierr); 4689 PetscFunctionReturn(0); 4690 } 4691 4692 /*MC 4693 MatGetArrayF90 - Accesses a matrix array from Fortran90. 4694 4695 Synopsis: 4696 MatGetArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr) 4697 4698 Not collective 4699 4700 Input Parameter: 4701 . x - matrix 4702 4703 Output Parameters: 4704 + xx_v - the Fortran90 pointer to the array 4705 - ierr - error code 4706 4707 Example of Usage: 4708 .vb 4709 PetscScalar, pointer xx_v(:) 4710 .... 4711 call MatGetArrayF90(x,xx_v,ierr) 4712 a = xx_v(3) 4713 call MatRestoreArrayF90(x,xx_v,ierr) 4714 .ve 4715 4716 Notes: 4717 Not yet supported for all F90 compilers 4718 4719 Level: advanced 4720 4721 .seealso: MatRestoreArrayF90(), MatGetArray(), MatRestoreArray() 4722 4723 Concepts: matrices^accessing array 4724 4725 M*/ 4726 4727 /*MC 4728 MatRestoreArrayF90 - Restores a matrix array that has been 4729 accessed with MatGetArrayF90(). 4730 4731 Synopsis: 4732 MatRestoreArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr) 4733 4734 Not collective 4735 4736 Input Parameters: 4737 + x - matrix 4738 - xx_v - the Fortran90 pointer to the array 4739 4740 Output Parameter: 4741 . ierr - error code 4742 4743 Example of Usage: 4744 .vb 4745 PetscScalar, pointer xx_v(:) 4746 .... 4747 call MatGetArrayF90(x,xx_v,ierr) 4748 a = xx_v(3) 4749 call MatRestoreArrayF90(x,xx_v,ierr) 4750 .ve 4751 4752 Notes: 4753 Not yet supported for all F90 compilers 4754 4755 Level: advanced 4756 4757 .seealso: MatGetArrayF90(), MatGetArray(), MatRestoreArray() 4758 4759 M*/ 4760 4761 4762 #undef __FUNCT__ 4763 #define __FUNCT__ "MatGetSubMatrix" 4764 /*@ 4765 MatGetSubMatrix - Gets a single submatrix on the same number of processors 4766 as the original matrix. 4767 4768 Collective on Mat 4769 4770 Input Parameters: 4771 + mat - the original matrix 4772 . isrow - rows this processor should obtain 4773 . iscol - columns for all processors you wish to keep 4774 . csize - number of columns "local" to this processor (does nothing for sequential 4775 matrices). This should match the result from VecGetLocalSize(x,...) if you 4776 plan to use the matrix in a A*x; alternatively, you can use PETSC_DECIDE 4777 - cll - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 4778 4779 Output Parameter: 4780 . newmat - the new submatrix, of the same type as the old 4781 4782 Level: advanced 4783 4784 Notes: the iscol argument MUST be the same on each processor. You might be 4785 able to create the iscol argument with ISAllGather(). 4786 4787 The first time this is called you should use a cll of MAT_INITIAL_MATRIX, 4788 the MatGetSubMatrix() routine will create the newmat for you. Any additional calls 4789 to this routine with a mat of the same nonzero structure will reuse the matrix 4790 generated the first time. 4791 4792 Concepts: matrices^submatrices 4793 4794 .seealso: MatGetSubMatrices(), ISAllGather() 4795 @*/ 4796 int MatGetSubMatrix(Mat mat,IS isrow,IS iscol,int csize,MatReuse cll,Mat *newmat) 4797 { 4798 int ierr, size; 4799 Mat *local; 4800 4801 PetscFunctionBegin; 4802 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 4803 PetscValidHeaderSpecific(isrow,IS_COOKIE,2); 4804 PetscValidHeaderSpecific(iscol,IS_COOKIE,3); 4805 PetscValidPointer(newmat,6); 4806 if (cll == MAT_REUSE_MATRIX) PetscValidHeaderSpecific(*newmat,MAT_COOKIE,6); 4807 PetscValidType(mat,1); 4808 MatPreallocated(mat); 4809 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4810 ierr = MPI_Comm_size(mat->comm,&size);CHKERRQ(ierr); 4811 4812 /* if original matrix is on just one processor then use submatrix generated */ 4813 if (!mat->ops->getsubmatrix && size == 1 && cll == MAT_REUSE_MATRIX) { 4814 ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&newmat);CHKERRQ(ierr); 4815 PetscFunctionReturn(0); 4816 } else if (!mat->ops->getsubmatrix && size == 1) { 4817 ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr); 4818 *newmat = *local; 4819 ierr = PetscFree(local);CHKERRQ(ierr); 4820 PetscFunctionReturn(0); 4821 } 4822 4823 if (!mat->ops->getsubmatrix) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 4824 ierr = (*mat->ops->getsubmatrix)(mat,isrow,iscol,csize,cll,newmat);CHKERRQ(ierr); 4825 ierr = PetscObjectIncreaseState((PetscObject)*newmat);CHKERRQ(ierr); 4826 PetscFunctionReturn(0); 4827 } 4828 4829 #undef __FUNCT__ 4830 #define __FUNCT__ "MatGetPetscMaps" 4831 /*@C 4832 MatGetPetscMaps - Returns the maps associated with the matrix. 4833 4834 Not Collective 4835 4836 Input Parameter: 4837 . mat - the matrix 4838 4839 Output Parameters: 4840 + rmap - the row (right) map 4841 - cmap - the column (left) map 4842 4843 Level: developer 4844 4845 Concepts: maps^getting from matrix 4846 4847 @*/ 4848 int MatGetPetscMaps(Mat mat,PetscMap *rmap,PetscMap *cmap) 4849 { 4850 int ierr; 4851 4852 PetscFunctionBegin; 4853 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 4854 PetscValidType(mat,1); 4855 MatPreallocated(mat); 4856 ierr = (*mat->ops->getmaps)(mat,rmap,cmap);CHKERRQ(ierr); 4857 PetscFunctionReturn(0); 4858 } 4859 4860 /* 4861 Version that works for all PETSc matrices 4862 */ 4863 #undef __FUNCT__ 4864 #define __FUNCT__ "MatGetPetscMaps_Petsc" 4865 int MatGetPetscMaps_Petsc(Mat mat,PetscMap *rmap,PetscMap *cmap) 4866 { 4867 PetscFunctionBegin; 4868 if (rmap) *rmap = mat->rmap; 4869 if (cmap) *cmap = mat->cmap; 4870 PetscFunctionReturn(0); 4871 } 4872 4873 #undef __FUNCT__ 4874 #define __FUNCT__ "MatStashSetInitialSize" 4875 /*@ 4876 MatStashSetInitialSize - sets the sizes of the matrix stash, that is 4877 used during the assembly process to store values that belong to 4878 other processors. 4879 4880 Not Collective 4881 4882 Input Parameters: 4883 + mat - the matrix 4884 . size - the initial size of the stash. 4885 - bsize - the initial size of the block-stash(if used). 4886 4887 Options Database Keys: 4888 + -matstash_initial_size <size> or <size0,size1,...sizep-1> 4889 - -matstash_block_initial_size <bsize> or <bsize0,bsize1,...bsizep-1> 4890 4891 Level: intermediate 4892 4893 Notes: 4894 The block-stash is used for values set with VecSetValuesBlocked() while 4895 the stash is used for values set with VecSetValues() 4896 4897 Run with the option -log_info and look for output of the form 4898 MatAssemblyBegin_MPIXXX:Stash has MM entries, uses nn mallocs. 4899 to determine the appropriate value, MM, to use for size and 4900 MatAssemblyBegin_MPIXXX:Block-Stash has BMM entries, uses nn mallocs. 4901 to determine the value, BMM to use for bsize 4902 4903 Concepts: stash^setting matrix size 4904 Concepts: matrices^stash 4905 4906 @*/ 4907 int MatStashSetInitialSize(Mat mat,int size, int bsize) 4908 { 4909 int ierr; 4910 4911 PetscFunctionBegin; 4912 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 4913 PetscValidType(mat,1); 4914 MatPreallocated(mat); 4915 ierr = MatStashSetInitialSize_Private(&mat->stash,size);CHKERRQ(ierr); 4916 ierr = MatStashSetInitialSize_Private(&mat->bstash,bsize);CHKERRQ(ierr); 4917 PetscFunctionReturn(0); 4918 } 4919 4920 #undef __FUNCT__ 4921 #define __FUNCT__ "MatInterpolateAdd" 4922 /*@ 4923 MatInterpolateAdd - w = y + A*x or A'*x depending on the shape of 4924 the matrix 4925 4926 Collective on Mat 4927 4928 Input Parameters: 4929 + mat - the matrix 4930 . x,y - the vectors 4931 - w - where the result is stored 4932 4933 Level: intermediate 4934 4935 Notes: 4936 w may be the same vector as y. 4937 4938 This allows one to use either the restriction or interpolation (its transpose) 4939 matrix to do the interpolation 4940 4941 Concepts: interpolation 4942 4943 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict() 4944 4945 @*/ 4946 int MatInterpolateAdd(Mat A,Vec x,Vec y,Vec w) 4947 { 4948 int M,N,ierr; 4949 4950 PetscFunctionBegin; 4951 PetscValidHeaderSpecific(A,MAT_COOKIE,1); 4952 PetscValidHeaderSpecific(x,VEC_COOKIE,2); 4953 PetscValidHeaderSpecific(y,VEC_COOKIE,3); 4954 PetscValidHeaderSpecific(w,VEC_COOKIE,4); 4955 PetscValidType(A,1); 4956 MatPreallocated(A); 4957 ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr); 4958 if (N > M) { 4959 ierr = MatMultTransposeAdd(A,x,y,w);CHKERRQ(ierr); 4960 } else { 4961 ierr = MatMultAdd(A,x,y,w);CHKERRQ(ierr); 4962 } 4963 PetscFunctionReturn(0); 4964 } 4965 4966 #undef __FUNCT__ 4967 #define __FUNCT__ "MatInterpolate" 4968 /*@ 4969 MatInterpolate - y = A*x or A'*x depending on the shape of 4970 the matrix 4971 4972 Collective on Mat 4973 4974 Input Parameters: 4975 + mat - the matrix 4976 - x,y - the vectors 4977 4978 Level: intermediate 4979 4980 Notes: 4981 This allows one to use either the restriction or interpolation (its transpose) 4982 matrix to do the interpolation 4983 4984 Concepts: matrices^interpolation 4985 4986 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict() 4987 4988 @*/ 4989 int MatInterpolate(Mat A,Vec x,Vec y) 4990 { 4991 int M,N,ierr; 4992 4993 PetscFunctionBegin; 4994 PetscValidHeaderSpecific(A,MAT_COOKIE,1); 4995 PetscValidHeaderSpecific(x,VEC_COOKIE,2); 4996 PetscValidHeaderSpecific(y,VEC_COOKIE,3); 4997 PetscValidType(A,1); 4998 MatPreallocated(A); 4999 ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr); 5000 if (N > M) { 5001 ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr); 5002 } else { 5003 ierr = MatMult(A,x,y);CHKERRQ(ierr); 5004 } 5005 PetscFunctionReturn(0); 5006 } 5007 5008 #undef __FUNCT__ 5009 #define __FUNCT__ "MatRestrict" 5010 /*@ 5011 MatRestrict - y = A*x or A'*x 5012 5013 Collective on Mat 5014 5015 Input Parameters: 5016 + mat - the matrix 5017 - x,y - the vectors 5018 5019 Level: intermediate 5020 5021 Notes: 5022 This allows one to use either the restriction or interpolation (its transpose) 5023 matrix to do the restriction 5024 5025 Concepts: matrices^restriction 5026 5027 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatInterpolate() 5028 5029 @*/ 5030 int MatRestrict(Mat A,Vec x,Vec y) 5031 { 5032 int M,N,ierr; 5033 5034 PetscFunctionBegin; 5035 PetscValidHeaderSpecific(A,MAT_COOKIE,1); 5036 PetscValidHeaderSpecific(x,VEC_COOKIE,2); 5037 PetscValidHeaderSpecific(y,VEC_COOKIE,3); 5038 PetscValidType(A,1); 5039 MatPreallocated(A); 5040 ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr); 5041 if (N > M) { 5042 ierr = MatMult(A,x,y);CHKERRQ(ierr); 5043 } else { 5044 ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr); 5045 } 5046 PetscFunctionReturn(0); 5047 } 5048 5049 #undef __FUNCT__ 5050 #define __FUNCT__ "MatNullSpaceAttach" 5051 /*@C 5052 MatNullSpaceAttach - attaches a null space to a matrix. 5053 This null space will be removed from the resulting vector whenever 5054 MatMult() is called 5055 5056 Collective on Mat 5057 5058 Input Parameters: 5059 + mat - the matrix 5060 - nullsp - the null space object 5061 5062 Level: developer 5063 5064 Notes: 5065 Overwrites any previous null space that may have been attached 5066 5067 Concepts: null space^attaching to matrix 5068 5069 .seealso: MatCreate(), MatNullSpaceCreate() 5070 @*/ 5071 int MatNullSpaceAttach(Mat mat,MatNullSpace nullsp) 5072 { 5073 int ierr; 5074 5075 PetscFunctionBegin; 5076 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 5077 PetscValidType(mat,1); 5078 MatPreallocated(mat); 5079 PetscValidHeaderSpecific(nullsp,MAT_NULLSPACE_COOKIE,2); 5080 5081 if (mat->nullsp) { 5082 ierr = MatNullSpaceDestroy(mat->nullsp);CHKERRQ(ierr); 5083 } 5084 mat->nullsp = nullsp; 5085 ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr); 5086 PetscFunctionReturn(0); 5087 } 5088 5089 #undef __FUNCT__ 5090 #define __FUNCT__ "MatICCFactor" 5091 /*@ 5092 MatICCFactor - Performs in-place incomplete Cholesky factorization of matrix. 5093 5094 Collective on Mat 5095 5096 Input Parameters: 5097 + mat - the matrix 5098 . row - row/column permutation 5099 . fill - expected fill factor >= 1.0 5100 - level - level of fill, for ICC(k) 5101 5102 Notes: 5103 Probably really in-place only when level of fill is zero, otherwise allocates 5104 new space to store factored matrix and deletes previous memory. 5105 5106 Most users should employ the simplified KSP interface for linear solvers 5107 instead of working directly with matrix algebra routines such as this. 5108 See, e.g., KSPCreate(). 5109 5110 Level: developer 5111 5112 Concepts: matrices^incomplete Cholesky factorization 5113 Concepts: Cholesky factorization 5114 5115 .seealso: MatICCFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor() 5116 @*/ 5117 int MatICCFactor(Mat mat,IS row,MatFactorInfo* info) 5118 { 5119 int ierr; 5120 5121 PetscFunctionBegin; 5122 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 5123 PetscValidType(mat,1); 5124 MatPreallocated(mat); 5125 if (row) PetscValidHeaderSpecific(row,IS_COOKIE,2); 5126 PetscValidPointer(info,3); 5127 if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,"matrix must be square"); 5128 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5129 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5130 if (!mat->ops->iccfactor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 5131 ierr = (*mat->ops->iccfactor)(mat,row,info);CHKERRQ(ierr); 5132 ierr = PetscObjectIncreaseState((PetscObject)mat);CHKERRQ(ierr); 5133 PetscFunctionReturn(0); 5134 } 5135 5136 #undef __FUNCT__ 5137 #define __FUNCT__ "MatSetValuesAdic" 5138 /*@ 5139 MatSetValuesAdic - Sets values computed with ADIC automatic differentiation into a matrix. 5140 5141 Not Collective 5142 5143 Input Parameters: 5144 + mat - the matrix 5145 - v - the values compute with ADIC 5146 5147 Level: developer 5148 5149 Notes: 5150 Must call MatSetColoring() before using this routine. Also this matrix must already 5151 have its nonzero pattern determined. 5152 5153 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(), 5154 MatSetValues(), MatSetColoring(), MatSetValuesAdifor() 5155 @*/ 5156 int MatSetValuesAdic(Mat mat,void *v) 5157 { 5158 int ierr; 5159 5160 PetscFunctionBegin; 5161 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 5162 PetscValidType(mat,1); 5163 PetscValidPointer(mat,2); 5164 5165 if (!mat->assembled) { 5166 SETERRQ(1,"Matrix must be already assembled"); 5167 } 5168 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 5169 if (!mat->ops->setvaluesadic) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 5170 ierr = (*mat->ops->setvaluesadic)(mat,v);CHKERRQ(ierr); 5171 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 5172 ierr = MatView_Private(mat);CHKERRQ(ierr); 5173 ierr = PetscObjectIncreaseState((PetscObject)mat);CHKERRQ(ierr); 5174 PetscFunctionReturn(0); 5175 } 5176 5177 5178 #undef __FUNCT__ 5179 #define __FUNCT__ "MatSetColoring" 5180 /*@ 5181 MatSetColoring - Sets a coloring used by calls to MatSetValuesAdic() 5182 5183 Not Collective 5184 5185 Input Parameters: 5186 + mat - the matrix 5187 - coloring - the coloring 5188 5189 Level: developer 5190 5191 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(), 5192 MatSetValues(), MatSetValuesAdic() 5193 @*/ 5194 int MatSetColoring(Mat mat,ISColoring coloring) 5195 { 5196 int ierr; 5197 5198 PetscFunctionBegin; 5199 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 5200 PetscValidType(mat,1); 5201 PetscValidPointer(coloring,2); 5202 5203 if (!mat->assembled) { 5204 SETERRQ(1,"Matrix must be already assembled"); 5205 } 5206 if (!mat->ops->setcoloring) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 5207 ierr = (*mat->ops->setcoloring)(mat,coloring);CHKERRQ(ierr); 5208 PetscFunctionReturn(0); 5209 } 5210 5211 #undef __FUNCT__ 5212 #define __FUNCT__ "MatSetValuesAdifor" 5213 /*@ 5214 MatSetValuesAdifor - Sets values computed with automatic differentiation into a matrix. 5215 5216 Not Collective 5217 5218 Input Parameters: 5219 + mat - the matrix 5220 . nl - leading dimension of v 5221 - v - the values compute with ADIFOR 5222 5223 Level: developer 5224 5225 Notes: 5226 Must call MatSetColoring() before using this routine. Also this matrix must already 5227 have its nonzero pattern determined. 5228 5229 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(), 5230 MatSetValues(), MatSetColoring() 5231 @*/ 5232 int MatSetValuesAdifor(Mat mat,int nl,void *v) 5233 { 5234 int ierr; 5235 5236 PetscFunctionBegin; 5237 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 5238 PetscValidType(mat,1); 5239 PetscValidPointer(v,3); 5240 5241 if (!mat->assembled) { 5242 SETERRQ(1,"Matrix must be already assembled"); 5243 } 5244 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 5245 if (!mat->ops->setvaluesadifor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 5246 ierr = (*mat->ops->setvaluesadifor)(mat,nl,v);CHKERRQ(ierr); 5247 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 5248 ierr = PetscObjectIncreaseState((PetscObject)mat);CHKERRQ(ierr); 5249 PetscFunctionReturn(0); 5250 } 5251 5252 EXTERN int MatMPIAIJDiagonalScaleLocal(Mat,Vec); 5253 EXTERN int MatMPIBAIJDiagonalScaleLocal(Mat,Vec); 5254 5255 #undef __FUNCT__ 5256 #define __FUNCT__ "MatDiagonalScaleLocal" 5257 /*@ 5258 MatDiagonalScaleLocal - Scales columns of a matrix given the scaling values including the 5259 ghosted ones. 5260 5261 Not Collective 5262 5263 Input Parameters: 5264 + mat - the matrix 5265 - diag = the diagonal values, including ghost ones 5266 5267 Level: developer 5268 5269 Notes: Works only for MPIAIJ and MPIBAIJ matrices 5270 5271 .seealso: MatDiagonalScale() 5272 @*/ 5273 int MatDiagonalScaleLocal(Mat mat,Vec diag) 5274 { 5275 int ierr,size; 5276 5277 PetscFunctionBegin; 5278 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 5279 PetscValidHeaderSpecific(diag,VEC_COOKIE,2); 5280 PetscValidType(mat,1); 5281 5282 if (!mat->assembled) { 5283 SETERRQ(1,"Matrix must be already assembled"); 5284 } 5285 ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 5286 ierr = MPI_Comm_size(mat->comm,&size);CHKERRQ(ierr); 5287 if (size == 1) { 5288 int n,m; 5289 ierr = VecGetSize(diag,&n);CHKERRQ(ierr); 5290 ierr = MatGetSize(mat,0,&m);CHKERRQ(ierr); 5291 if (m == n) { 5292 ierr = MatDiagonalScale(mat,0,diag);CHKERRQ(ierr); 5293 } else { 5294 SETERRQ(1,"Only supported for sequential matrices when no ghost points/periodic conditions"); 5295 } 5296 } else { 5297 int (*f)(Mat,Vec); 5298 ierr = PetscObjectQueryFunction((PetscObject)mat,"MatDiagonalScaleLocal_C",(void (**)(void))&f);CHKERRQ(ierr); 5299 if (f) { 5300 ierr = (*f)(mat,diag);CHKERRQ(ierr); 5301 } else { 5302 SETERRQ(1,"Only supported for MPIAIJ and MPIBAIJ parallel matrices"); 5303 } 5304 } 5305 ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 5306 ierr = PetscObjectIncreaseState((PetscObject)mat);CHKERRQ(ierr); 5307 PetscFunctionReturn(0); 5308 } 5309 5310 #undef __FUNCT__ 5311 #define __FUNCT__ "MatGetInertia" 5312 /*@ 5313 MatGetInertia - Gets the inertia from a factored matrix 5314 5315 Collective on Mat 5316 5317 Input Parameter: 5318 . mat - the matrix 5319 5320 Output Parameters: 5321 + nneg - number of negative eigenvalues 5322 . nzero - number of zero eigenvalues 5323 - npos - number of positive eigenvalues 5324 5325 Level: advanced 5326 5327 Notes: Matrix must have been factored by MatCholeskyFactor() 5328 5329 5330 @*/ 5331 int MatGetInertia(Mat mat,int *nneg,int *nzero,int *npos) 5332 { 5333 int ierr; 5334 5335 PetscFunctionBegin; 5336 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 5337 PetscValidType(mat,1); 5338 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 5339 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Numeric factor mat is not assembled"); 5340 if (!mat->ops->getinertia) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 5341 ierr = (*mat->ops->getinertia)(mat,nneg,nzero,npos);CHKERRQ(ierr); 5342 PetscFunctionReturn(0); 5343 } 5344 5345 /* ----------------------------------------------------------------*/ 5346 #undef __FUNCT__ 5347 #define __FUNCT__ "MatSolves" 5348 /*@ 5349 MatSolves - Solves A x = b, given a factored matrix, for a collection of vectors 5350 5351 Collective on Mat and Vecs 5352 5353 Input Parameters: 5354 + mat - the factored matrix 5355 - b - the right-hand-side vectors 5356 5357 Output Parameter: 5358 . x - the result vectors 5359 5360 Notes: 5361 The vectors b and x cannot be the same. I.e., one cannot 5362 call MatSolves(A,x,x). 5363 5364 Notes: 5365 Most users should employ the simplified KSP interface for linear solvers 5366 instead of working directly with matrix algebra routines such as this. 5367 See, e.g., KSPCreate(). 5368 5369 Level: developer 5370 5371 Concepts: matrices^triangular solves 5372 5373 .seealso: MatSolveAdd(), MatSolveTranspose(), MatSolveTransposeAdd(), MatSolve() 5374 @*/ 5375 int MatSolves(Mat mat,Vecs b,Vecs x) 5376 { 5377 int ierr; 5378 5379 PetscFunctionBegin; 5380 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 5381 PetscValidType(mat,1); 5382 MatPreallocated(mat); 5383 if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 5384 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 5385 if (mat->M == 0 && mat->N == 0) PetscFunctionReturn(0); 5386 5387 if (!mat->ops->solves) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 5388 ierr = PetscLogEventBegin(MAT_Solves,mat,0,0,0);CHKERRQ(ierr); 5389 ierr = (*mat->ops->solves)(mat,b,x);CHKERRQ(ierr); 5390 ierr = PetscLogEventEnd(MAT_Solves,mat,0,0,0);CHKERRQ(ierr); 5391 PetscFunctionReturn(0); 5392 } 5393 5394 #undef __FUNCT__ 5395 #define __FUNCT__ "MatIsSymmetric" 5396 /*@C 5397 MatIsSymmetric - Test whether a matrix is symmetric 5398 5399 Collective on Mat 5400 5401 Input Parameter: 5402 + A - the matrix to test 5403 - tol - difference between value and its transpose less than this amount counts as equal (use 0.0 for exact transpose) 5404 5405 Output Parameters: 5406 . flg - the result 5407 5408 Level: intermediate 5409 5410 Concepts: matrix^symmetry 5411 5412 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetricKnown() 5413 @*/ 5414 int MatIsSymmetric(Mat A,PetscReal tol,PetscTruth *flg) 5415 { 5416 int ierr; 5417 5418 PetscFunctionBegin; 5419 PetscValidHeaderSpecific(A,MAT_COOKIE,1); 5420 PetscValidPointer(flg,2); 5421 if (!A->symmetric_set) { 5422 if (!A->ops->issymmetric) { 5423 MatType mattype; 5424 ierr = MatGetType(A,&mattype);CHKERRQ(ierr); 5425 SETERRQ1(1,"Matrix of type <%s> does not support checking for symmetric",mattype); 5426 } 5427 ierr = (*A->ops->issymmetric)(A,tol,&A->symmetric);CHKERRQ(ierr); 5428 A->symmetric_set = PETSC_TRUE; 5429 if (A->symmetric) { 5430 A->structurally_symmetric_set = PETSC_TRUE; 5431 A->structurally_symmetric = PETSC_TRUE; 5432 } 5433 } 5434 *flg = A->symmetric; 5435 PetscFunctionReturn(0); 5436 } 5437 5438 #undef __FUNCT__ 5439 #define __FUNCT__ "MatIsSymmetricKnown" 5440 /*@C 5441 MatIsSymmetricKnown - Checks the flag on the matrix to see if it is symmetric. 5442 5443 Collective on Mat 5444 5445 Input Parameter: 5446 . A - the matrix to check 5447 5448 Output Parameters: 5449 + set - if the symmetric flag is set (this tells you if the next flag is valid) 5450 - flg - the result 5451 5452 Level: advanced 5453 5454 Concepts: matrix^symmetry 5455 5456 Note: Does not check the matrix values directly, so this may return unknown (set = PETSC_FALSE). Use MatIsSymmetric() 5457 if you want it explicitly checked 5458 5459 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetric() 5460 @*/ 5461 int MatIsSymmetricKnown(Mat A,PetscTruth *set,PetscTruth *flg) 5462 { 5463 PetscFunctionBegin; 5464 PetscValidHeaderSpecific(A,MAT_COOKIE,1); 5465 PetscValidPointer(set,2); 5466 PetscValidPointer(flg,3); 5467 if (A->symmetric_set) { 5468 *set = PETSC_TRUE; 5469 *flg = A->symmetric; 5470 } else { 5471 *set = PETSC_FALSE; 5472 } 5473 PetscFunctionReturn(0); 5474 } 5475 5476 #undef __FUNCT__ 5477 #define __FUNCT__ "MatIsStructurallySymmetric" 5478 /*@C 5479 MatIsStructurallySymmetric - Test whether a matrix is structurally symmetric 5480 5481 Collective on Mat 5482 5483 Input Parameter: 5484 . A - the matrix to test 5485 5486 Output Parameters: 5487 . flg - the result 5488 5489 Level: intermediate 5490 5491 Concepts: matrix^symmetry 5492 5493 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsSymmetric(), MatSetOption() 5494 @*/ 5495 int MatIsStructurallySymmetric(Mat A,PetscTruth *flg) 5496 { 5497 int ierr; 5498 5499 PetscFunctionBegin; 5500 PetscValidHeaderSpecific(A,MAT_COOKIE,1); 5501 PetscValidPointer(flg,2); 5502 if (!A->structurally_symmetric_set) { 5503 if (!A->ops->isstructurallysymmetric) SETERRQ(1,"Matrix does not support checking for structural symmetric"); 5504 ierr = (*A->ops->isstructurallysymmetric)(A,&A->structurally_symmetric);CHKERRQ(ierr); 5505 A->structurally_symmetric_set = PETSC_TRUE; 5506 } 5507 *flg = A->structurally_symmetric; 5508 PetscFunctionReturn(0); 5509 } 5510 5511 #undef __FUNCT__ 5512 #define __FUNCT__ "MatIsHermitian" 5513 /*@C 5514 MatIsHermitian - Test whether a matrix is Hermitian, i.e. it is the complex conjugate of its transpose. 5515 5516 Collective on Mat 5517 5518 Input Parameter: 5519 . A - the matrix to test 5520 5521 Output Parameters: 5522 . flg - the result 5523 5524 Level: intermediate 5525 5526 Concepts: matrix^symmetry 5527 5528 .seealso: MatTranspose(), MatIsTranspose(), MatIsSymmetric(), MatIsStructurallySymmetric(), MatSetOption() 5529 @*/ 5530 int MatIsHermitian(Mat A,PetscTruth *flg) 5531 { 5532 int ierr; 5533 5534 PetscFunctionBegin; 5535 PetscValidHeaderSpecific(A,MAT_COOKIE,1); 5536 PetscValidPointer(flg,2); 5537 if (!A->hermitian_set) { 5538 if (!A->ops->ishermitian) SETERRQ(1,"Matrix does not support checking for being Hermitian"); 5539 ierr = (*A->ops->ishermitian)(A,&A->hermitian);CHKERRQ(ierr); 5540 A->hermitian_set = PETSC_TRUE; 5541 if (A->hermitian) { 5542 A->structurally_symmetric_set = PETSC_TRUE; 5543 A->structurally_symmetric = PETSC_TRUE; 5544 } 5545 } 5546 *flg = A->hermitian; 5547 PetscFunctionReturn(0); 5548 } 5549 5550 #undef __FUNCT__ 5551 #define __FUNCT__ "MatStashGetInfo" 5552 extern int MatStashGetInfo_Private(MatStash*,int*,int*); 5553 /*@ 5554 MatStashGetInfo - Gets how many values are currently in the vector stash, i.e. need 5555 to be communicated to other processors during the MatAssemblyBegin/End() process 5556 5557 Not collective 5558 5559 Input Parameter: 5560 . vec - the vector 5561 5562 Output Parameters: 5563 + nstash - the size of the stash 5564 . reallocs - the number of additional mallocs incurred. 5565 . bnstash - the size of the block stash 5566 - breallocs - the number of additional mallocs incurred.in the block stash 5567 5568 Level: advanced 5569 5570 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), Mat, MatStashSetInitialSize() 5571 5572 @*/ 5573 int MatStashGetInfo(Mat mat,int *nstash,int *reallocs,int *bnstash,int *brealloc) 5574 { 5575 int ierr; 5576 PetscFunctionBegin; 5577 ierr = MatStashGetInfo_Private(&mat->stash,nstash,reallocs);CHKERRQ(ierr); 5578 ierr = MatStashGetInfo_Private(&mat->bstash,nstash,reallocs);CHKERRQ(ierr); 5579 PetscFunctionReturn(0); 5580 } 5581 5582 #undef __FUNCT__ 5583 #define __FUNCT__ "MatGetVecs" 5584 /*@ 5585 MatGetVecs - Get vector(s) compatible with the matrix, i.e. with the same 5586 parallel layout 5587 5588 Collective on Mat 5589 5590 Input Parameter: 5591 . mat - the matrix 5592 5593 Output Parameter: 5594 + right - (optional) vector that the matrix can be multiplied against 5595 - left - (optional) vector that the matrix vector product can be stored in 5596 5597 Level: advanced 5598 5599 .seealso: MatCreate() 5600 @*/ 5601 int MatGetVecs(Mat mat,Vec *right,Vec *left) 5602 { 5603 int ierr; 5604 5605 PetscFunctionBegin; 5606 PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 5607 PetscValidType(mat,1); 5608 MatPreallocated(mat); 5609 if (mat->ops->getvecs) { 5610 ierr = (*mat->ops->getvecs)(mat,right,left);CHKERRQ(ierr); 5611 } else { 5612 int size; 5613 ierr = MPI_Comm_size(mat->comm, &size);CHKERRQ(ierr); 5614 if (right) { 5615 ierr = VecCreate(mat->comm,right);CHKERRQ(ierr); 5616 ierr = VecSetSizes(*right,mat->n,PETSC_DETERMINE);CHKERRQ(ierr); 5617 if (size > 1) {ierr = VecSetType(*right,VECMPI);CHKERRQ(ierr);} 5618 else {ierr = VecSetType(*right,VECSEQ);CHKERRQ(ierr);} 5619 } 5620 if (left) { 5621 ierr = VecCreate(mat->comm,left);CHKERRQ(ierr); 5622 ierr = VecSetSizes(*left,mat->m,PETSC_DETERMINE);CHKERRQ(ierr); 5623 if (size > 1) {ierr = VecSetType(*left,VECMPI);CHKERRQ(ierr);} 5624 else {ierr = VecSetType(*left,VECSEQ);CHKERRQ(ierr);} 5625 } 5626 } 5627 PetscFunctionReturn(0); 5628 } 5629