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