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