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