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