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