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