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