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