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