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