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