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