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_QRFactorNumeric, MAT_QRFactorSymbolic, MAT_QRFactor; 21 PetscLogEvent MAT_AssemblyEnd, MAT_SetValues, MAT_GetValues, MAT_GetRow, MAT_GetRowIJ, MAT_CreateSubMats, MAT_GetOrdering, MAT_RedundantMat, MAT_GetSeqNonzeroStructure; 22 PetscLogEvent MAT_IncreaseOverlap, MAT_Partitioning, MAT_PartitioningND, MAT_Coarsen, MAT_ZeroEntries, MAT_Load, MAT_View, MAT_AXPY, MAT_FDColoringCreate; 23 PetscLogEvent MAT_FDColoringSetUp, MAT_FDColoringApply,MAT_Transpose,MAT_FDColoringFunction, MAT_CreateSubMat; 24 PetscLogEvent MAT_TransposeColoringCreate; 25 PetscLogEvent MAT_MatMult, MAT_MatMultSymbolic, MAT_MatMultNumeric; 26 PetscLogEvent MAT_PtAP, MAT_PtAPSymbolic, MAT_PtAPNumeric,MAT_RARt, MAT_RARtSymbolic, MAT_RARtNumeric; 27 PetscLogEvent MAT_MatTransposeMult, MAT_MatTransposeMultSymbolic, MAT_MatTransposeMultNumeric; 28 PetscLogEvent MAT_TransposeMatMult, MAT_TransposeMatMultSymbolic, MAT_TransposeMatMultNumeric; 29 PetscLogEvent MAT_MatMatMult, MAT_MatMatMultSymbolic, MAT_MatMatMultNumeric; 30 PetscLogEvent MAT_MultHermitianTranspose,MAT_MultHermitianTransposeAdd; 31 PetscLogEvent MAT_Getsymtranspose, MAT_Getsymtransreduced, MAT_GetBrowsOfAcols; 32 PetscLogEvent MAT_GetBrowsOfAocols, MAT_Getlocalmat, MAT_Getlocalmatcondensed, MAT_Seqstompi, MAT_Seqstompinum, MAT_Seqstompisym; 33 PetscLogEvent MAT_Applypapt, MAT_Applypapt_numeric, MAT_Applypapt_symbolic, MAT_GetSequentialNonzeroStructure; 34 PetscLogEvent MAT_GetMultiProcBlock; 35 PetscLogEvent MAT_CUSPARSECopyToGPU, MAT_CUSPARSECopyFromGPU, MAT_CUSPARSEGenerateTranspose, MAT_CUSPARSESolveAnalysis; 36 PetscLogEvent MAT_PreallCOO, MAT_SetVCOO; 37 PetscLogEvent MAT_SetValuesBatch; 38 PetscLogEvent MAT_ViennaCLCopyToGPU; 39 PetscLogEvent MAT_DenseCopyToGPU, MAT_DenseCopyFromGPU; 40 PetscLogEvent MAT_Merge,MAT_Residual,MAT_SetRandom; 41 PetscLogEvent MAT_FactorFactS,MAT_FactorInvS; 42 PetscLogEvent MATCOLORING_Apply,MATCOLORING_Comm,MATCOLORING_Local,MATCOLORING_ISCreate,MATCOLORING_SetUp,MATCOLORING_Weights; 43 PetscLogEvent MAT_H2Opus_Build,MAT_H2Opus_Compress,MAT_H2Opus_Orthog,MAT_H2Opus_LR; 44 45 const char *const MatFactorTypes[] = {"NONE","LU","CHOLESKY","ILU","ICC","ILUDT","QR","MatFactorType","MAT_FACTOR_",NULL}; 46 47 /*@ 48 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, 49 for sparse matrices that already have locations it fills the locations with random numbers 50 51 Logically Collective on Mat 52 53 Input Parameters: 54 + x - the matrix 55 - rctx - the random number context, formed by PetscRandomCreate(), or NULL and 56 it will create one internally. 57 58 Output Parameter: 59 . x - the matrix 60 61 Example of Usage: 62 .vb 63 PetscRandomCreate(PETSC_COMM_WORLD,&rctx); 64 MatSetRandom(x,rctx); 65 PetscRandomDestroy(rctx); 66 .ve 67 68 Level: intermediate 69 70 .seealso: MatZeroEntries(), MatSetValues(), PetscRandomCreate(), PetscRandomDestroy() 71 @*/ 72 PetscErrorCode MatSetRandom(Mat x,PetscRandom rctx) 73 { 74 PetscErrorCode ierr; 75 PetscRandom randObj = NULL; 76 77 PetscFunctionBegin; 78 PetscValidHeaderSpecific(x,MAT_CLASSID,1); 79 if (rctx) PetscValidHeaderSpecific(rctx,PETSC_RANDOM_CLASSID,2); 80 PetscValidType(x,1); 81 MatCheckPreallocated(x,1); 82 83 PetscCheckFalse(!x->ops->setrandom,PetscObjectComm((PetscObject)x),PETSC_ERR_SUP,"Mat type %s",((PetscObject)x)->type_name); 84 85 if (!rctx) { 86 MPI_Comm comm; 87 ierr = PetscObjectGetComm((PetscObject)x,&comm);CHKERRQ(ierr); 88 ierr = PetscRandomCreate(comm,&randObj);CHKERRQ(ierr); 89 ierr = PetscRandomSetFromOptions(randObj);CHKERRQ(ierr); 90 rctx = randObj; 91 } 92 ierr = PetscLogEventBegin(MAT_SetRandom,x,rctx,0,0);CHKERRQ(ierr); 93 ierr = (*x->ops->setrandom)(x,rctx);CHKERRQ(ierr); 94 ierr = PetscLogEventEnd(MAT_SetRandom,x,rctx,0,0);CHKERRQ(ierr); 95 96 ierr = MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 97 ierr = MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 98 ierr = PetscRandomDestroy(&randObj);CHKERRQ(ierr); 99 PetscFunctionReturn(0); 100 } 101 102 /*@ 103 MatFactorGetErrorZeroPivot - returns the pivot value that was determined to be zero and the row it occurred in 104 105 Logically Collective on Mat 106 107 Input Parameter: 108 . mat - the factored matrix 109 110 Output Parameters: 111 + pivot - the pivot value computed 112 - row - the row that the zero pivot occurred. Note that this row must be interpreted carefully due to row reorderings and which processes 113 the share the matrix 114 115 Level: advanced 116 117 Notes: 118 This routine does not work for factorizations done with external packages. 119 120 This routine should only be called if MatGetFactorError() returns a value of MAT_FACTOR_NUMERIC_ZEROPIVOT 121 122 This can be called on non-factored matrices that come from, for example, matrices used in SOR. 123 124 .seealso: MatZeroEntries(), MatFactor(), MatGetFactor(), MatLUFactorSymbolic(), MatCholeskyFactorSymbolic(), MatFactorClearError(), MatFactorGetErrorZeroPivot() 125 @*/ 126 PetscErrorCode MatFactorGetErrorZeroPivot(Mat mat,PetscReal *pivot,PetscInt *row) 127 { 128 PetscFunctionBegin; 129 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 130 *pivot = mat->factorerror_zeropivot_value; 131 *row = mat->factorerror_zeropivot_row; 132 PetscFunctionReturn(0); 133 } 134 135 /*@ 136 MatFactorGetError - gets the error code from a factorization 137 138 Logically Collective on Mat 139 140 Input Parameters: 141 . mat - the factored matrix 142 143 Output Parameter: 144 . err - the error code 145 146 Level: advanced 147 148 Notes: 149 This can be called on non-factored matrices that come from, for example, matrices used in SOR. 150 151 .seealso: MatZeroEntries(), MatFactor(), MatGetFactor(), MatLUFactorSymbolic(), MatCholeskyFactorSymbolic(), MatFactorClearError(), MatFactorGetErrorZeroPivot() 152 @*/ 153 PetscErrorCode MatFactorGetError(Mat mat,MatFactorError *err) 154 { 155 PetscFunctionBegin; 156 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 157 *err = mat->factorerrortype; 158 PetscFunctionReturn(0); 159 } 160 161 /*@ 162 MatFactorClearError - clears the error code in a factorization 163 164 Logically Collective on Mat 165 166 Input Parameter: 167 . mat - the factored matrix 168 169 Level: developer 170 171 Notes: 172 This can be called on non-factored matrices that come from, for example, matrices used in SOR. 173 174 .seealso: MatZeroEntries(), MatFactor(), MatGetFactor(), MatLUFactorSymbolic(), MatCholeskyFactorSymbolic(), MatFactorGetError(), MatFactorGetErrorZeroPivot() 175 @*/ 176 PetscErrorCode MatFactorClearError(Mat mat) 177 { 178 PetscFunctionBegin; 179 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 180 mat->factorerrortype = MAT_FACTOR_NOERROR; 181 mat->factorerror_zeropivot_value = 0.0; 182 mat->factorerror_zeropivot_row = 0; 183 PetscFunctionReturn(0); 184 } 185 186 PETSC_INTERN PetscErrorCode MatFindNonzeroRowsOrCols_Basic(Mat mat,PetscBool cols,PetscReal tol,IS *nonzero) 187 { 188 PetscErrorCode ierr; 189 Vec r,l; 190 const PetscScalar *al; 191 PetscInt i,nz,gnz,N,n; 192 193 PetscFunctionBegin; 194 ierr = MatCreateVecs(mat,&r,&l);CHKERRQ(ierr); 195 if (!cols) { /* nonzero rows */ 196 ierr = MatGetSize(mat,&N,NULL);CHKERRQ(ierr); 197 ierr = MatGetLocalSize(mat,&n,NULL);CHKERRQ(ierr); 198 ierr = VecSet(l,0.0);CHKERRQ(ierr); 199 ierr = VecSetRandom(r,NULL);CHKERRQ(ierr); 200 ierr = MatMult(mat,r,l);CHKERRQ(ierr); 201 ierr = VecGetArrayRead(l,&al);CHKERRQ(ierr); 202 } else { /* nonzero columns */ 203 ierr = MatGetSize(mat,NULL,&N);CHKERRQ(ierr); 204 ierr = MatGetLocalSize(mat,NULL,&n);CHKERRQ(ierr); 205 ierr = VecSet(r,0.0);CHKERRQ(ierr); 206 ierr = VecSetRandom(l,NULL);CHKERRQ(ierr); 207 ierr = MatMultTranspose(mat,l,r);CHKERRQ(ierr); 208 ierr = VecGetArrayRead(r,&al);CHKERRQ(ierr); 209 } 210 if (tol <= 0.0) { for (i=0,nz=0;i<n;i++) if (al[i] != 0.0) nz++; } 211 else { for (i=0,nz=0;i<n;i++) if (PetscAbsScalar(al[i]) > tol) nz++; } 212 ierr = MPIU_Allreduce(&nz,&gnz,1,MPIU_INT,MPI_SUM,PetscObjectComm((PetscObject)mat));CHKERRMPI(ierr); 213 if (gnz != N) { 214 PetscInt *nzr; 215 ierr = PetscMalloc1(nz,&nzr);CHKERRQ(ierr); 216 if (nz) { 217 if (tol < 0) { for (i=0,nz=0;i<n;i++) if (al[i] != 0.0) nzr[nz++] = i; } 218 else { for (i=0,nz=0;i<n;i++) if (PetscAbsScalar(al[i]) > tol) nzr[nz++] = i; } 219 } 220 ierr = ISCreateGeneral(PetscObjectComm((PetscObject)mat),nz,nzr,PETSC_OWN_POINTER,nonzero);CHKERRQ(ierr); 221 } else *nonzero = NULL; 222 if (!cols) { /* nonzero rows */ 223 ierr = VecRestoreArrayRead(l,&al);CHKERRQ(ierr); 224 } else { 225 ierr = VecRestoreArrayRead(r,&al);CHKERRQ(ierr); 226 } 227 ierr = VecDestroy(&l);CHKERRQ(ierr); 228 ierr = VecDestroy(&r);CHKERRQ(ierr); 229 PetscFunctionReturn(0); 230 } 231 232 /*@ 233 MatFindNonzeroRows - Locate all rows that are not completely zero in the matrix 234 235 Input Parameter: 236 . A - the matrix 237 238 Output Parameter: 239 . keptrows - the rows that are not completely zero 240 241 Notes: 242 keptrows is set to NULL if all rows are nonzero. 243 244 Level: intermediate 245 246 @*/ 247 PetscErrorCode MatFindNonzeroRows(Mat mat,IS *keptrows) 248 { 249 PetscErrorCode ierr; 250 251 PetscFunctionBegin; 252 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 253 PetscValidType(mat,1); 254 PetscValidPointer(keptrows,2); 255 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 256 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 257 if (!mat->ops->findnonzerorows) { 258 ierr = MatFindNonzeroRowsOrCols_Basic(mat,PETSC_FALSE,0.0,keptrows);CHKERRQ(ierr); 259 } else { 260 ierr = (*mat->ops->findnonzerorows)(mat,keptrows);CHKERRQ(ierr); 261 } 262 PetscFunctionReturn(0); 263 } 264 265 /*@ 266 MatFindZeroRows - Locate all rows that are completely zero in the matrix 267 268 Input Parameter: 269 . A - the matrix 270 271 Output Parameter: 272 . zerorows - the rows that are completely zero 273 274 Notes: 275 zerorows is set to NULL if no rows are zero. 276 277 Level: intermediate 278 279 @*/ 280 PetscErrorCode MatFindZeroRows(Mat mat,IS *zerorows) 281 { 282 PetscErrorCode ierr; 283 IS keptrows; 284 PetscInt m, n; 285 286 PetscFunctionBegin; 287 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 288 PetscValidType(mat,1); 289 PetscValidPointer(zerorows,2); 290 ierr = MatFindNonzeroRows(mat, &keptrows);CHKERRQ(ierr); 291 /* MatFindNonzeroRows sets keptrows to NULL if there are no zero rows. 292 In keeping with this convention, we set zerorows to NULL if there are no zero 293 rows. */ 294 if (keptrows == NULL) { 295 *zerorows = NULL; 296 } else { 297 ierr = MatGetOwnershipRange(mat,&m,&n);CHKERRQ(ierr); 298 ierr = ISComplement(keptrows,m,n,zerorows);CHKERRQ(ierr); 299 ierr = ISDestroy(&keptrows);CHKERRQ(ierr); 300 } 301 PetscFunctionReturn(0); 302 } 303 304 /*@ 305 MatGetDiagonalBlock - Returns the part of the matrix associated with the on-process coupling 306 307 Not Collective 308 309 Input Parameters: 310 . A - the matrix 311 312 Output Parameters: 313 . a - the diagonal part (which is a SEQUENTIAL matrix) 314 315 Notes: 316 see the manual page for MatCreateAIJ() for more information on the "diagonal part" of the matrix. 317 Use caution, as the reference count on the returned matrix is not incremented and it is used as 318 part of the containing MPI Mat's normal operation. 319 320 Level: advanced 321 322 @*/ 323 PetscErrorCode MatGetDiagonalBlock(Mat A,Mat *a) 324 { 325 PetscErrorCode ierr; 326 327 PetscFunctionBegin; 328 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 329 PetscValidType(A,1); 330 PetscValidPointer(a,2); 331 PetscCheckFalse(A->factortype,PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 332 if (!A->ops->getdiagonalblock) { 333 PetscMPIInt size; 334 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)A),&size);CHKERRMPI(ierr); 335 if (size == 1) { 336 *a = A; 337 PetscFunctionReturn(0); 338 } else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Not coded for matrix type %s",((PetscObject)A)->type_name); 339 } 340 ierr = (*A->ops->getdiagonalblock)(A,a);CHKERRQ(ierr); 341 PetscFunctionReturn(0); 342 } 343 344 /*@ 345 MatGetTrace - Gets the trace of a matrix. The sum of the diagonal entries. 346 347 Collective on Mat 348 349 Input Parameters: 350 . mat - the matrix 351 352 Output Parameter: 353 . trace - the sum of the diagonal entries 354 355 Level: advanced 356 357 @*/ 358 PetscErrorCode MatGetTrace(Mat mat,PetscScalar *trace) 359 { 360 PetscErrorCode ierr; 361 Vec diag; 362 363 PetscFunctionBegin; 364 ierr = MatCreateVecs(mat,&diag,NULL);CHKERRQ(ierr); 365 ierr = MatGetDiagonal(mat,diag);CHKERRQ(ierr); 366 ierr = VecSum(diag,trace);CHKERRQ(ierr); 367 ierr = VecDestroy(&diag);CHKERRQ(ierr); 368 PetscFunctionReturn(0); 369 } 370 371 /*@ 372 MatRealPart - Zeros out the imaginary part of the matrix 373 374 Logically Collective on Mat 375 376 Input Parameters: 377 . mat - the matrix 378 379 Level: advanced 380 381 .seealso: MatImaginaryPart() 382 @*/ 383 PetscErrorCode MatRealPart(Mat mat) 384 { 385 PetscErrorCode ierr; 386 387 PetscFunctionBegin; 388 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 389 PetscValidType(mat,1); 390 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 391 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 392 PetscCheckFalse(!mat->ops->realpart,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 393 MatCheckPreallocated(mat,1); 394 ierr = (*mat->ops->realpart)(mat);CHKERRQ(ierr); 395 PetscFunctionReturn(0); 396 } 397 398 /*@C 399 MatGetGhosts - Get the global index of all ghost nodes defined by the sparse matrix 400 401 Collective on Mat 402 403 Input Parameter: 404 . mat - the matrix 405 406 Output Parameters: 407 + nghosts - number of ghosts (note for BAIJ matrices there is one ghost for each block) 408 - ghosts - the global indices of the ghost points 409 410 Notes: 411 the nghosts and ghosts are suitable to pass into VecCreateGhost() 412 413 Level: advanced 414 415 @*/ 416 PetscErrorCode MatGetGhosts(Mat mat,PetscInt *nghosts,const PetscInt *ghosts[]) 417 { 418 PetscErrorCode ierr; 419 420 PetscFunctionBegin; 421 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 422 PetscValidType(mat,1); 423 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 424 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 425 if (!mat->ops->getghosts) { 426 if (nghosts) *nghosts = 0; 427 if (ghosts) *ghosts = NULL; 428 } else { 429 ierr = (*mat->ops->getghosts)(mat,nghosts,ghosts);CHKERRQ(ierr); 430 } 431 PetscFunctionReturn(0); 432 } 433 434 /*@ 435 MatImaginaryPart - Moves the imaginary part of the matrix to the real part and zeros the imaginary part 436 437 Logically Collective on Mat 438 439 Input Parameters: 440 . mat - the matrix 441 442 Level: advanced 443 444 .seealso: MatRealPart() 445 @*/ 446 PetscErrorCode MatImaginaryPart(Mat mat) 447 { 448 PetscErrorCode ierr; 449 450 PetscFunctionBegin; 451 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 452 PetscValidType(mat,1); 453 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 454 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 455 PetscCheckFalse(!mat->ops->imaginarypart,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 456 MatCheckPreallocated(mat,1); 457 ierr = (*mat->ops->imaginarypart)(mat);CHKERRQ(ierr); 458 PetscFunctionReturn(0); 459 } 460 461 /*@ 462 MatMissingDiagonal - Determine if sparse matrix is missing a diagonal entry (or block entry for BAIJ matrices) 463 464 Not Collective 465 466 Input Parameter: 467 . mat - the matrix 468 469 Output Parameters: 470 + missing - is any diagonal missing 471 - dd - first diagonal entry that is missing (optional) on this process 472 473 Level: advanced 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 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix %s",((PetscObject)mat)->type_name); 486 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 487 PetscCheckFalse(!mat->ops->missingdiagonal,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 Caution: 543 Do not try to change the contents of the output arrays (cols and vals). 544 In some cases, this may corrupt the matrix. 545 546 Level: advanced 547 548 .seealso: MatRestoreRow(), MatSetValues(), MatGetValues(), MatCreateSubMatrices(), MatGetDiagonal() 549 @*/ 550 PetscErrorCode MatGetRow(Mat mat,PetscInt row,PetscInt *ncols,const PetscInt *cols[],const PetscScalar *vals[]) 551 { 552 PetscErrorCode ierr; 553 PetscInt incols; 554 555 PetscFunctionBegin; 556 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 557 PetscValidType(mat,1); 558 PetscCheckFalse(!mat->assembled,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 559 PetscCheckFalse(mat->factortype,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 560 PetscCheckFalse(!mat->ops->getrow,PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 561 MatCheckPreallocated(mat,1); 562 PetscCheckFalse(row < mat->rmap->rstart || row >= mat->rmap->rend,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Only for local rows, %" PetscInt_FMT " not in [%" PetscInt_FMT ",%" PetscInt_FMT ")",row,mat->rmap->rstart,mat->rmap->rend); 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 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 590 PetscCheckFalse(!mat->ops->conjugate,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 PetscCheckFalse(!mat->assembled,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 PetscCheckFalse(!mat->assembled,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 675 PetscCheckFalse(mat->factortype,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 Level: advanced 694 695 .seealso: MatGetRowUpperTriangular() 696 @*/ 697 PetscErrorCode MatRestoreRowUpperTriangular(Mat mat) 698 { 699 PetscErrorCode ierr; 700 701 PetscFunctionBegin; 702 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 703 PetscValidType(mat,1); 704 PetscCheckFalse(!mat->assembled,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 705 PetscCheckFalse(mat->factortype,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 706 MatCheckPreallocated(mat,1); 707 if (!mat->ops->restorerowuppertriangular) PetscFunctionReturn(0); 708 ierr = (*mat->ops->restorerowuppertriangular)(mat);CHKERRQ(ierr); 709 PetscFunctionReturn(0); 710 } 711 712 /*@C 713 MatSetOptionsPrefix - Sets the prefix used for searching for all 714 Mat options in the database. 715 716 Logically Collective on Mat 717 718 Input Parameters: 719 + A - the Mat context 720 - prefix - the prefix to prepend to all option names 721 722 Notes: 723 A hyphen (-) must NOT be given at the beginning of the prefix name. 724 The first character of all runtime options is AUTOMATICALLY the hyphen. 725 726 Level: advanced 727 728 .seealso: MatSetFromOptions() 729 @*/ 730 PetscErrorCode MatSetOptionsPrefix(Mat A,const char prefix[]) 731 { 732 PetscErrorCode ierr; 733 734 PetscFunctionBegin; 735 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 736 ierr = PetscObjectSetOptionsPrefix((PetscObject)A,prefix);CHKERRQ(ierr); 737 PetscFunctionReturn(0); 738 } 739 740 /*@C 741 MatAppendOptionsPrefix - Appends to the prefix used for searching for all 742 Mat options in the database. 743 744 Logically Collective on Mat 745 746 Input Parameters: 747 + A - the Mat context 748 - prefix - the prefix to prepend to all option names 749 750 Notes: 751 A hyphen (-) must NOT be given at the beginning of the prefix name. 752 The first character of all runtime options is AUTOMATICALLY the hyphen. 753 754 Level: advanced 755 756 .seealso: MatGetOptionsPrefix() 757 @*/ 758 PetscErrorCode MatAppendOptionsPrefix(Mat A,const char prefix[]) 759 { 760 PetscErrorCode ierr; 761 762 PetscFunctionBegin; 763 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 764 ierr = PetscObjectAppendOptionsPrefix((PetscObject)A,prefix);CHKERRQ(ierr); 765 PetscFunctionReturn(0); 766 } 767 768 /*@C 769 MatGetOptionsPrefix - Gets the prefix used for searching for all 770 Mat options in the database. 771 772 Not Collective 773 774 Input Parameter: 775 . A - the Mat context 776 777 Output Parameter: 778 . prefix - pointer to the prefix string used 779 780 Notes: 781 On the fortran side, the user should pass in a string 'prefix' of 782 sufficient length to hold the prefix. 783 784 Level: advanced 785 786 .seealso: MatAppendOptionsPrefix() 787 @*/ 788 PetscErrorCode MatGetOptionsPrefix(Mat A,const char *prefix[]) 789 { 790 PetscErrorCode ierr; 791 792 PetscFunctionBegin; 793 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 794 ierr = PetscObjectGetOptionsPrefix((PetscObject)A,prefix);CHKERRQ(ierr); 795 PetscFunctionReturn(0); 796 } 797 798 /*@ 799 MatResetPreallocation - Reset mat to use the original nonzero pattern provided by users. 800 801 Collective on Mat 802 803 Input Parameters: 804 . A - the Mat context 805 806 Notes: 807 The allocated memory will be shrunk after calling MatAssembly with MAT_FINAL_ASSEMBLY. Users can reset the preallocation to access the original memory. 808 Currently support MPIAIJ and SEQAIJ. 809 810 Level: beginner 811 812 .seealso: MatSeqAIJSetPreallocation(), MatMPIAIJSetPreallocation(), MatXAIJSetPreallocation() 813 @*/ 814 PetscErrorCode MatResetPreallocation(Mat A) 815 { 816 PetscErrorCode ierr; 817 818 PetscFunctionBegin; 819 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 820 PetscValidType(A,1); 821 ierr = PetscUseMethod(A,"MatResetPreallocation_C",(Mat),(A));CHKERRQ(ierr); 822 PetscFunctionReturn(0); 823 } 824 825 /*@ 826 MatSetUp - Sets up the internal matrix data structures for later use. 827 828 Collective on Mat 829 830 Input Parameters: 831 . A - the Mat context 832 833 Notes: 834 If the user has not set preallocation for this matrix then a default preallocation that is likely to be inefficient is used. 835 836 If a suitable preallocation routine is used, this function does not need to be called. 837 838 See the Performance chapter of the PETSc users manual for how to preallocate matrices 839 840 Level: beginner 841 842 .seealso: MatCreate(), MatDestroy() 843 @*/ 844 PetscErrorCode MatSetUp(Mat A) 845 { 846 PetscMPIInt size; 847 PetscErrorCode ierr; 848 849 PetscFunctionBegin; 850 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 851 if (!((PetscObject)A)->type_name) { 852 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)A), &size);CHKERRMPI(ierr); 853 if (size == 1) { 854 ierr = MatSetType(A, MATSEQAIJ);CHKERRQ(ierr); 855 } else { 856 ierr = MatSetType(A, MATMPIAIJ);CHKERRQ(ierr); 857 } 858 } 859 if (!A->preallocated && A->ops->setup) { 860 ierr = PetscInfo(A,"Warning not preallocating matrix storage\n");CHKERRQ(ierr); 861 ierr = (*A->ops->setup)(A);CHKERRQ(ierr); 862 } 863 ierr = PetscLayoutSetUp(A->rmap);CHKERRQ(ierr); 864 ierr = PetscLayoutSetUp(A->cmap);CHKERRQ(ierr); 865 A->preallocated = PETSC_TRUE; 866 PetscFunctionReturn(0); 867 } 868 869 #if defined(PETSC_HAVE_SAWS) 870 #include <petscviewersaws.h> 871 #endif 872 873 /*@C 874 MatViewFromOptions - View from Options 875 876 Collective on Mat 877 878 Input Parameters: 879 + A - the Mat context 880 . obj - Optional object 881 - name - command line option 882 883 Level: intermediate 884 .seealso: Mat, MatView, PetscObjectViewFromOptions(), MatCreate() 885 @*/ 886 PetscErrorCode MatViewFromOptions(Mat A,PetscObject obj,const char name[]) 887 { 888 PetscErrorCode ierr; 889 890 PetscFunctionBegin; 891 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 892 ierr = PetscObjectViewFromOptions((PetscObject)A,obj,name);CHKERRQ(ierr); 893 PetscFunctionReturn(0); 894 } 895 896 /*@C 897 MatView - Visualizes a matrix object. 898 899 Collective on Mat 900 901 Input Parameters: 902 + mat - the matrix 903 - viewer - visualization context 904 905 Notes: 906 The available visualization contexts include 907 + PETSC_VIEWER_STDOUT_SELF - for sequential matrices 908 . PETSC_VIEWER_STDOUT_WORLD - for parallel matrices created on PETSC_COMM_WORLD 909 . PETSC_VIEWER_STDOUT_(comm) - for matrices created on MPI communicator comm 910 - PETSC_VIEWER_DRAW_WORLD - graphical display of nonzero structure 911 912 The user can open alternative visualization contexts with 913 + PetscViewerASCIIOpen() - Outputs matrix to a specified file 914 . PetscViewerBinaryOpen() - Outputs matrix in binary to a 915 specified file; corresponding input uses MatLoad() 916 . PetscViewerDrawOpen() - Outputs nonzero matrix structure to 917 an X window display 918 - PetscViewerSocketOpen() - Outputs matrix to Socket viewer. 919 Currently only the sequential dense and AIJ 920 matrix types support the Socket viewer. 921 922 The user can call PetscViewerPushFormat() to specify the output 923 format of ASCII printed objects (when using PETSC_VIEWER_STDOUT_SELF, 924 PETSC_VIEWER_STDOUT_WORLD and PetscViewerASCIIOpen). Available formats include 925 + PETSC_VIEWER_DEFAULT - default, prints matrix contents 926 . PETSC_VIEWER_ASCII_MATLAB - prints matrix contents in Matlab format 927 . PETSC_VIEWER_ASCII_DENSE - prints entire matrix including zeros 928 . PETSC_VIEWER_ASCII_COMMON - prints matrix contents, using a sparse 929 format common among all matrix types 930 . PETSC_VIEWER_ASCII_IMPL - prints matrix contents, using an implementation-specific 931 format (which is in many cases the same as the default) 932 . PETSC_VIEWER_ASCII_INFO - prints basic information about the matrix 933 size and structure (not the matrix entries) 934 - PETSC_VIEWER_ASCII_INFO_DETAIL - prints more detailed information about 935 the matrix structure 936 937 Options Database Keys: 938 + -mat_view ::ascii_info - Prints info on matrix at conclusion of MatAssemblyEnd() 939 . -mat_view ::ascii_info_detail - Prints more detailed info 940 . -mat_view - Prints matrix in ASCII format 941 . -mat_view ::ascii_matlab - Prints matrix in Matlab format 942 . -mat_view draw - PetscDraws nonzero structure of matrix, using MatView() and PetscDrawOpenX(). 943 . -display <name> - Sets display name (default is host) 944 . -draw_pause <sec> - Sets number of seconds to pause after display 945 . -mat_view socket - Sends matrix to socket, can be accessed from Matlab (see Users-Manual: ch_matlab for details) 946 . -viewer_socket_machine <machine> - 947 . -viewer_socket_port <port> - 948 . -mat_view binary - save matrix to file in binary format 949 - -viewer_binary_filename <name> - 950 Level: beginner 951 952 Notes: 953 The ASCII viewers are only recommended for small matrices on at most a moderate number of processes, 954 the program will seemingly hang and take hours for larger matrices, for larger matrices one should use the binary format. 955 956 In the debugger you can do "call MatView(mat,0)" to display the matrix. (The same holds for any PETSc object viewer). 957 958 See the manual page for MatLoad() for the exact format of the binary file when the binary 959 viewer is used. 960 961 See share/petsc/matlab/PetscBinaryRead.m for a Matlab code that can read in the binary file when the binary 962 viewer is used and lib/petsc/bin/PetscBinaryIO.py for loading them into Python. 963 964 One can use '-mat_view draw -draw_pause -1' to pause the graphical display of matrix nonzero structure, 965 and then use the following mouse functions. 966 + left mouse: zoom in 967 . middle mouse: zoom out 968 - right mouse: continue with the simulation 969 970 .seealso: PetscViewerPushFormat(), PetscViewerASCIIOpen(), PetscViewerDrawOpen(), 971 PetscViewerSocketOpen(), PetscViewerBinaryOpen(), MatLoad() 972 @*/ 973 PetscErrorCode MatView(Mat mat,PetscViewer viewer) 974 { 975 PetscErrorCode ierr; 976 PetscInt rows,cols,rbs,cbs; 977 PetscBool isascii,isstring,issaws; 978 PetscViewerFormat format; 979 PetscMPIInt size; 980 981 PetscFunctionBegin; 982 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 983 PetscValidType(mat,1); 984 if (!viewer) {ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)mat),&viewer);CHKERRQ(ierr);} 985 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 986 PetscCheckSameComm(mat,1,viewer,2); 987 MatCheckPreallocated(mat,1); 988 989 ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 990 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);CHKERRMPI(ierr); 991 if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0); 992 993 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 994 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);CHKERRQ(ierr); 995 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr); 996 if ((!isascii || (format != PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL)) && mat->factortype) { 997 SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"No viewers for factored matrix except ASCII info or info_detail"); 998 } 999 1000 ierr = PetscLogEventBegin(MAT_View,mat,viewer,0,0);CHKERRQ(ierr); 1001 if (isascii) { 1002 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ORDER,"Must call MatAssemblyBegin/End() before viewing matrix"); 1003 ierr = PetscObjectPrintClassNamePrefixType((PetscObject)mat,viewer);CHKERRQ(ierr); 1004 if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) { 1005 MatNullSpace nullsp,transnullsp; 1006 1007 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1008 ierr = MatGetSize(mat,&rows,&cols);CHKERRQ(ierr); 1009 ierr = MatGetBlockSizes(mat,&rbs,&cbs);CHKERRQ(ierr); 1010 if (rbs != 1 || cbs != 1) { 1011 if (rbs != cbs) {ierr = PetscViewerASCIIPrintf(viewer,"rows=%" PetscInt_FMT ", cols=%" PetscInt_FMT ", rbs=%" PetscInt_FMT ", cbs=%" PetscInt_FMT "\n",rows,cols,rbs,cbs);CHKERRQ(ierr);} 1012 else {ierr = PetscViewerASCIIPrintf(viewer,"rows=%" PetscInt_FMT ", cols=%" PetscInt_FMT ", bs=%" PetscInt_FMT "\n",rows,cols,rbs);CHKERRQ(ierr);} 1013 } else { 1014 ierr = PetscViewerASCIIPrintf(viewer,"rows=%" PetscInt_FMT ", cols=%" PetscInt_FMT "\n",rows,cols);CHKERRQ(ierr); 1015 } 1016 if (mat->factortype) { 1017 MatSolverType solver; 1018 ierr = MatFactorGetSolverType(mat,&solver);CHKERRQ(ierr); 1019 ierr = PetscViewerASCIIPrintf(viewer,"package used to perform factorization: %s\n",solver);CHKERRQ(ierr); 1020 } 1021 if (mat->ops->getinfo) { 1022 MatInfo info; 1023 ierr = MatGetInfo(mat,MAT_GLOBAL_SUM,&info);CHKERRQ(ierr); 1024 ierr = PetscViewerASCIIPrintf(viewer,"total: nonzeros=%.f, allocated nonzeros=%.f\n",info.nz_used,info.nz_allocated);CHKERRQ(ierr); 1025 if (!mat->factortype) { 1026 ierr = PetscViewerASCIIPrintf(viewer,"total number of mallocs used during MatSetValues calls=%" PetscInt_FMT "\n",(PetscInt)info.mallocs);CHKERRQ(ierr); 1027 } 1028 } 1029 ierr = MatGetNullSpace(mat,&nullsp);CHKERRQ(ierr); 1030 ierr = MatGetTransposeNullSpace(mat,&transnullsp);CHKERRQ(ierr); 1031 if (nullsp) {ierr = PetscViewerASCIIPrintf(viewer," has attached null space\n");CHKERRQ(ierr);} 1032 if (transnullsp && transnullsp != nullsp) {ierr = PetscViewerASCIIPrintf(viewer," has attached transposed null space\n");CHKERRQ(ierr);} 1033 ierr = MatGetNearNullSpace(mat,&nullsp);CHKERRQ(ierr); 1034 if (nullsp) {ierr = PetscViewerASCIIPrintf(viewer," has attached near null space\n");CHKERRQ(ierr);} 1035 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1036 ierr = MatProductView(mat,viewer);CHKERRQ(ierr); 1037 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1038 } 1039 } else if (issaws) { 1040 #if defined(PETSC_HAVE_SAWS) 1041 PetscMPIInt rank; 1042 1043 ierr = PetscObjectName((PetscObject)mat);CHKERRQ(ierr); 1044 ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRMPI(ierr); 1045 if (!((PetscObject)mat)->amsmem && rank == 0) { 1046 ierr = PetscObjectViewSAWs((PetscObject)mat,viewer);CHKERRQ(ierr); 1047 } 1048 #endif 1049 } else if (isstring) { 1050 const char *type; 1051 ierr = MatGetType(mat,&type);CHKERRQ(ierr); 1052 ierr = PetscViewerStringSPrintf(viewer," MatType: %-7.7s",type);CHKERRQ(ierr); 1053 if (mat->ops->view) {ierr = (*mat->ops->view)(mat,viewer);CHKERRQ(ierr);} 1054 } 1055 if ((format == PETSC_VIEWER_NATIVE || format == PETSC_VIEWER_LOAD_BALANCE) && mat->ops->viewnative) { 1056 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1057 ierr = (*mat->ops->viewnative)(mat,viewer);CHKERRQ(ierr); 1058 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1059 } else if (mat->ops->view) { 1060 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1061 ierr = (*mat->ops->view)(mat,viewer);CHKERRQ(ierr); 1062 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1063 } 1064 if (isascii) { 1065 ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 1066 if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) { 1067 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1068 } 1069 } 1070 ierr = PetscLogEventEnd(MAT_View,mat,viewer,0,0);CHKERRQ(ierr); 1071 PetscFunctionReturn(0); 1072 } 1073 1074 #if defined(PETSC_USE_DEBUG) 1075 #include <../src/sys/totalview/tv_data_display.h> 1076 PETSC_UNUSED static int TV_display_type(const struct _p_Mat *mat) 1077 { 1078 TV_add_row("Local rows", "int", &mat->rmap->n); 1079 TV_add_row("Local columns", "int", &mat->cmap->n); 1080 TV_add_row("Global rows", "int", &mat->rmap->N); 1081 TV_add_row("Global columns", "int", &mat->cmap->N); 1082 TV_add_row("Typename", TV_ascii_string_type, ((PetscObject)mat)->type_name); 1083 return TV_format_OK; 1084 } 1085 #endif 1086 1087 /*@C 1088 MatLoad - Loads a matrix that has been stored in binary/HDF5 format 1089 with MatView(). The matrix format is determined from the options database. 1090 Generates a parallel MPI matrix if the communicator has more than one 1091 processor. The default matrix type is AIJ. 1092 1093 Collective on PetscViewer 1094 1095 Input Parameters: 1096 + mat - the newly loaded matrix, this needs to have been created with MatCreate() 1097 or some related function before a call to MatLoad() 1098 - viewer - binary/HDF5 file viewer 1099 1100 Options Database Keys: 1101 Used with block matrix formats (MATSEQBAIJ, ...) to specify 1102 block size 1103 . -matload_block_size <bs> 1104 1105 Level: beginner 1106 1107 Notes: 1108 If the Mat type has not yet been given then MATAIJ is used, call MatSetFromOptions() on the 1109 Mat before calling this routine if you wish to set it from the options database. 1110 1111 MatLoad() automatically loads into the options database any options 1112 given in the file filename.info where filename is the name of the file 1113 that was passed to the PetscViewerBinaryOpen(). The options in the info 1114 file will be ignored if you use the -viewer_binary_skip_info option. 1115 1116 If the type or size of mat is not set before a call to MatLoad, PETSc 1117 sets the default matrix type AIJ and sets the local and global sizes. 1118 If type and/or size is already set, then the same are used. 1119 1120 In parallel, each processor can load a subset of rows (or the 1121 entire matrix). This routine is especially useful when a large 1122 matrix is stored on disk and only part of it is desired on each 1123 processor. For example, a parallel solver may access only some of 1124 the rows from each processor. The algorithm used here reads 1125 relatively small blocks of data rather than reading the entire 1126 matrix and then subsetting it. 1127 1128 Viewer's PetscViewerType must be either PETSCVIEWERBINARY or PETSCVIEWERHDF5. 1129 Such viewer can be created using PetscViewerBinaryOpen()/PetscViewerHDF5Open(), 1130 or the sequence like 1131 $ PetscViewer v; 1132 $ PetscViewerCreate(PETSC_COMM_WORLD,&v); 1133 $ PetscViewerSetType(v,PETSCVIEWERBINARY); 1134 $ PetscViewerSetFromOptions(v); 1135 $ PetscViewerFileSetMode(v,FILE_MODE_READ); 1136 $ PetscViewerFileSetName(v,"datafile"); 1137 The optional PetscViewerSetFromOptions() call allows to override PetscViewerSetType() using option 1138 $ -viewer_type {binary,hdf5} 1139 1140 See the example src/ksp/ksp/tutorials/ex27.c with the first approach, 1141 and src/mat/tutorials/ex10.c with the second approach. 1142 1143 Notes about the PETSc binary format: 1144 In case of PETSCVIEWERBINARY, a native PETSc binary format is used. Each of the blocks 1145 is read onto rank 0 and then shipped to its destination rank, one after another. 1146 Multiple objects, both matrices and vectors, can be stored within the same file. 1147 Their PetscObject name is ignored; they are loaded in the order of their storage. 1148 1149 Most users should not need to know the details of the binary storage 1150 format, since MatLoad() and MatView() completely hide these details. 1151 But for anyone who's interested, the standard binary matrix storage 1152 format is 1153 1154 $ PetscInt MAT_FILE_CLASSID 1155 $ PetscInt number of rows 1156 $ PetscInt number of columns 1157 $ PetscInt total number of nonzeros 1158 $ PetscInt *number nonzeros in each row 1159 $ PetscInt *column indices of all nonzeros (starting index is zero) 1160 $ PetscScalar *values of all nonzeros 1161 1162 PETSc automatically does the byte swapping for 1163 machines that store the bytes reversed, e.g. DEC alpha, freebsd, 1164 Linux, Microsoft Windows and the Intel Paragon; thus if you write your own binary 1165 read/write routines you have to swap the bytes; see PetscBinaryRead() 1166 and PetscBinaryWrite() to see how this may be done. 1167 1168 Notes about the HDF5 (MATLAB MAT-File Version 7.3) format: 1169 In case of PETSCVIEWERHDF5, a parallel HDF5 reader is used. 1170 Each processor's chunk is loaded independently by its owning rank. 1171 Multiple objects, both matrices and vectors, can be stored within the same file. 1172 They are looked up by their PetscObject name. 1173 1174 As the MATLAB MAT-File Version 7.3 format is also a HDF5 flavor, we decided to use 1175 by default the same structure and naming of the AIJ arrays and column count 1176 within the HDF5 file. This means that a MAT file saved with -v7.3 flag, e.g. 1177 $ save example.mat A b -v7.3 1178 can be directly read by this routine (see Reference 1 for details). 1179 Note that depending on your MATLAB version, this format might be a default, 1180 otherwise you can set it as default in Preferences. 1181 1182 Unless -nocompression flag is used to save the file in MATLAB, 1183 PETSc must be configured with ZLIB package. 1184 1185 See also examples src/mat/tutorials/ex10.c and src/ksp/ksp/tutorials/ex27.c 1186 1187 Current HDF5 (MAT-File) limitations: 1188 This reader currently supports only real MATSEQAIJ, MATMPIAIJ, MATSEQDENSE and MATMPIDENSE matrices. 1189 1190 Corresponding MatView() is not yet implemented. 1191 1192 The loaded matrix is actually a transpose of the original one in MATLAB, 1193 unless you push PETSC_VIEWER_HDF5_MAT format (see examples above). 1194 With this format, matrix is automatically transposed by PETSc, 1195 unless the matrix is marked as SPD or symmetric 1196 (see MatSetOption(), MAT_SPD, MAT_SYMMETRIC). 1197 1198 References: 1199 . * - MATLAB(R) Documentation, manual page of save(), https://www.mathworks.com/help/matlab/ref/save.html#btox10b-1-version 1200 1201 .seealso: PetscViewerBinaryOpen(), PetscViewerSetType(), MatView(), VecLoad() 1202 1203 @*/ 1204 PetscErrorCode MatLoad(Mat mat,PetscViewer viewer) 1205 { 1206 PetscErrorCode ierr; 1207 PetscBool flg; 1208 1209 PetscFunctionBegin; 1210 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 1211 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1212 1213 if (!((PetscObject)mat)->type_name) { 1214 ierr = MatSetType(mat,MATAIJ);CHKERRQ(ierr); 1215 } 1216 1217 flg = PETSC_FALSE; 1218 ierr = PetscOptionsGetBool(((PetscObject)mat)->options,((PetscObject)mat)->prefix,"-matload_symmetric",&flg,NULL);CHKERRQ(ierr); 1219 if (flg) { 1220 ierr = MatSetOption(mat,MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr); 1221 ierr = MatSetOption(mat,MAT_SYMMETRY_ETERNAL,PETSC_TRUE);CHKERRQ(ierr); 1222 } 1223 flg = PETSC_FALSE; 1224 ierr = PetscOptionsGetBool(((PetscObject)mat)->options,((PetscObject)mat)->prefix,"-matload_spd",&flg,NULL);CHKERRQ(ierr); 1225 if (flg) { 1226 ierr = MatSetOption(mat,MAT_SPD,PETSC_TRUE);CHKERRQ(ierr); 1227 } 1228 1229 PetscCheckFalse(!mat->ops->load,PETSC_COMM_SELF,PETSC_ERR_SUP,"MatLoad is not supported for type %s",((PetscObject)mat)->type_name); 1230 ierr = PetscLogEventBegin(MAT_Load,mat,viewer,0,0);CHKERRQ(ierr); 1231 ierr = (*mat->ops->load)(mat,viewer);CHKERRQ(ierr); 1232 ierr = PetscLogEventEnd(MAT_Load,mat,viewer,0,0);CHKERRQ(ierr); 1233 PetscFunctionReturn(0); 1234 } 1235 1236 static PetscErrorCode MatDestroy_Redundant(Mat_Redundant **redundant) 1237 { 1238 PetscErrorCode ierr; 1239 Mat_Redundant *redund = *redundant; 1240 PetscInt i; 1241 1242 PetscFunctionBegin; 1243 if (redund) { 1244 if (redund->matseq) { /* via MatCreateSubMatrices() */ 1245 ierr = ISDestroy(&redund->isrow);CHKERRQ(ierr); 1246 ierr = ISDestroy(&redund->iscol);CHKERRQ(ierr); 1247 ierr = MatDestroySubMatrices(1,&redund->matseq);CHKERRQ(ierr); 1248 } else { 1249 ierr = PetscFree2(redund->send_rank,redund->recv_rank);CHKERRQ(ierr); 1250 ierr = PetscFree(redund->sbuf_j);CHKERRQ(ierr); 1251 ierr = PetscFree(redund->sbuf_a);CHKERRQ(ierr); 1252 for (i=0; i<redund->nrecvs; i++) { 1253 ierr = PetscFree(redund->rbuf_j[i]);CHKERRQ(ierr); 1254 ierr = PetscFree(redund->rbuf_a[i]);CHKERRQ(ierr); 1255 } 1256 ierr = PetscFree4(redund->sbuf_nz,redund->rbuf_nz,redund->rbuf_j,redund->rbuf_a);CHKERRQ(ierr); 1257 } 1258 1259 if (redund->subcomm) { 1260 ierr = PetscCommDestroy(&redund->subcomm);CHKERRQ(ierr); 1261 } 1262 ierr = PetscFree(redund);CHKERRQ(ierr); 1263 } 1264 PetscFunctionReturn(0); 1265 } 1266 1267 /*@C 1268 MatDestroy - Frees space taken by a matrix. 1269 1270 Collective on Mat 1271 1272 Input Parameter: 1273 . A - the matrix 1274 1275 Level: beginner 1276 1277 @*/ 1278 PetscErrorCode MatDestroy(Mat *A) 1279 { 1280 PetscErrorCode ierr; 1281 1282 PetscFunctionBegin; 1283 if (!*A) PetscFunctionReturn(0); 1284 PetscValidHeaderSpecific(*A,MAT_CLASSID,1); 1285 if (--((PetscObject)(*A))->refct > 0) {*A = NULL; PetscFunctionReturn(0);} 1286 1287 /* if memory was published with SAWs then destroy it */ 1288 ierr = PetscObjectSAWsViewOff((PetscObject)*A);CHKERRQ(ierr); 1289 if ((*A)->ops->destroy) { 1290 ierr = (*(*A)->ops->destroy)(*A);CHKERRQ(ierr); 1291 } 1292 1293 ierr = PetscFree((*A)->defaultvectype);CHKERRQ(ierr); 1294 ierr = PetscFree((*A)->bsizes);CHKERRQ(ierr); 1295 ierr = PetscFree((*A)->solvertype);CHKERRQ(ierr); 1296 for (PetscInt i=0; i<MAT_FACTOR_NUM_TYPES; i++) { 1297 ierr = PetscFree((*A)->preferredordering[i]);CHKERRQ(ierr); 1298 } 1299 ierr = MatDestroy_Redundant(&(*A)->redundant);CHKERRQ(ierr); 1300 ierr = MatProductClear(*A);CHKERRQ(ierr); 1301 ierr = MatNullSpaceDestroy(&(*A)->nullsp);CHKERRQ(ierr); 1302 ierr = MatNullSpaceDestroy(&(*A)->transnullsp);CHKERRQ(ierr); 1303 ierr = MatNullSpaceDestroy(&(*A)->nearnullsp);CHKERRQ(ierr); 1304 ierr = MatDestroy(&(*A)->schur);CHKERRQ(ierr); 1305 ierr = PetscLayoutDestroy(&(*A)->rmap);CHKERRQ(ierr); 1306 ierr = PetscLayoutDestroy(&(*A)->cmap);CHKERRQ(ierr); 1307 ierr = PetscHeaderDestroy(A);CHKERRQ(ierr); 1308 PetscFunctionReturn(0); 1309 } 1310 1311 /*@C 1312 MatSetValues - Inserts or adds a block of values into a matrix. 1313 These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd() 1314 MUST be called after all calls to MatSetValues() have been completed. 1315 1316 Not Collective 1317 1318 Input Parameters: 1319 + mat - the matrix 1320 . v - a logically two-dimensional array of values 1321 . m, idxm - the number of rows and their global indices 1322 . n, idxn - the number of columns and their global indices 1323 - addv - either ADD_VALUES or INSERT_VALUES, where 1324 ADD_VALUES adds values to any existing entries, and 1325 INSERT_VALUES replaces existing entries with new values 1326 1327 Notes: 1328 If you create the matrix yourself (that is not with a call to DMCreateMatrix()) then you MUST call MatXXXXSetPreallocation() or 1329 MatSetUp() before using this routine 1330 1331 By default the values, v, are row-oriented. See MatSetOption() for other options. 1332 1333 Calls to MatSetValues() with the INSERT_VALUES and ADD_VALUES 1334 options cannot be mixed without intervening calls to the assembly 1335 routines. 1336 1337 MatSetValues() uses 0-based row and column numbers in Fortran 1338 as well as in C. 1339 1340 Negative indices may be passed in idxm and idxn, these rows and columns are 1341 simply ignored. This allows easily inserting element stiffness matrices 1342 with homogeneous Dirchlet boundary conditions that you don't want represented 1343 in the matrix. 1344 1345 Efficiency Alert: 1346 The routine MatSetValuesBlocked() may offer much better efficiency 1347 for users of block sparse formats (MATSEQBAIJ and MATMPIBAIJ). 1348 1349 Level: beginner 1350 1351 Developer Notes: 1352 This is labeled with C so does not automatically generate Fortran stubs and interfaces 1353 because it requires multiple Fortran interfaces depending on which arguments are scalar or arrays. 1354 1355 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(), 1356 InsertMode, INSERT_VALUES, ADD_VALUES 1357 @*/ 1358 PetscErrorCode MatSetValues(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],const PetscScalar v[],InsertMode addv) 1359 { 1360 PetscErrorCode ierr; 1361 1362 PetscFunctionBeginHot; 1363 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 1364 PetscValidType(mat,1); 1365 if (!m || !n) PetscFunctionReturn(0); /* no values to insert */ 1366 PetscValidIntPointer(idxm,3); 1367 PetscValidIntPointer(idxn,5); 1368 MatCheckPreallocated(mat,1); 1369 1370 if (mat->insertmode == NOT_SET_VALUES) mat->insertmode = addv; 1371 else PetscCheckFalse(mat->insertmode != addv,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values"); 1372 1373 if (PetscDefined(USE_DEBUG)) { 1374 PetscInt i,j; 1375 1376 PetscCheckFalse(mat->factortype,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1377 PetscCheckFalse(!mat->ops->setvalues,PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 1378 1379 for (i=0; i<m; i++) { 1380 for (j=0; j<n; j++) { 1381 if (mat->erroriffailure && PetscIsInfOrNanScalar(v[i*n+j])) 1382 #if defined(PETSC_USE_COMPLEX) 1383 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FP,"Inserting %g+i%g at matrix entry (%" PetscInt_FMT ",%" PetscInt_FMT ")",(double)PetscRealPart(v[i*n+j]),(double)PetscImaginaryPart(v[i*n+j]),idxm[i],idxn[j]); 1384 #else 1385 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FP,"Inserting %g at matrix entry (%" PetscInt_FMT ",%" PetscInt_FMT ")",(double)v[i*n+j],idxm[i],idxn[j]); 1386 #endif 1387 } 1388 } 1389 for (i=0; i<m; i++) PetscCheckFalse(idxm[i] >= mat->rmap->N,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot insert in row %" PetscInt_FMT ", maximum is %" PetscInt_FMT,idxm[i],mat->rmap->N-1); 1390 for (i=0; i<n; i++) PetscCheckFalse(idxn[i] >= mat->cmap->N,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot insert in column %" PetscInt_FMT ", maximum is %" PetscInt_FMT,idxn[i],mat->cmap->N-1); 1391 } 1392 1393 if (mat->assembled) { 1394 mat->was_assembled = PETSC_TRUE; 1395 mat->assembled = PETSC_FALSE; 1396 } 1397 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 1398 ierr = (*mat->ops->setvalues)(mat,m,idxm,n,idxn,v,addv);CHKERRQ(ierr); 1399 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 1400 PetscFunctionReturn(0); 1401 } 1402 1403 /*@ 1404 MatSetValuesRowLocal - Inserts a row (block row for BAIJ matrices) of nonzero 1405 values into a matrix 1406 1407 Not Collective 1408 1409 Input Parameters: 1410 + mat - the matrix 1411 . row - the (block) row to set 1412 - v - a logically two-dimensional array of values 1413 1414 Notes: 1415 By the values, v, are column-oriented (for the block version) and sorted 1416 1417 All the nonzeros in the row must be provided 1418 1419 The matrix must have previously had its column indices set 1420 1421 The row must belong to this process 1422 1423 Level: intermediate 1424 1425 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(), 1426 InsertMode, INSERT_VALUES, ADD_VALUES, MatSetValues(), MatSetValuesRow(), MatSetLocalToGlobalMapping() 1427 @*/ 1428 PetscErrorCode MatSetValuesRowLocal(Mat mat,PetscInt row,const PetscScalar v[]) 1429 { 1430 PetscErrorCode ierr; 1431 PetscInt globalrow; 1432 1433 PetscFunctionBegin; 1434 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 1435 PetscValidType(mat,1); 1436 PetscValidScalarPointer(v,3); 1437 ierr = ISLocalToGlobalMappingApply(mat->rmap->mapping,1,&row,&globalrow);CHKERRQ(ierr); 1438 ierr = MatSetValuesRow(mat,globalrow,v);CHKERRQ(ierr); 1439 PetscFunctionReturn(0); 1440 } 1441 1442 /*@ 1443 MatSetValuesRow - Inserts a row (block row for BAIJ matrices) of nonzero 1444 values into a matrix 1445 1446 Not Collective 1447 1448 Input Parameters: 1449 + mat - the matrix 1450 . row - the (block) row to set 1451 - 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 1452 1453 Notes: 1454 The values, v, are column-oriented for the block version. 1455 1456 All the nonzeros in the row must be provided 1457 1458 THE MATRIX MUST HAVE PREVIOUSLY HAD ITS COLUMN INDICES SET. IT IS RARE THAT THIS ROUTINE IS USED, usually MatSetValues() is used. 1459 1460 The row must belong to this process 1461 1462 Level: advanced 1463 1464 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(), 1465 InsertMode, INSERT_VALUES, ADD_VALUES, MatSetValues() 1466 @*/ 1467 PetscErrorCode MatSetValuesRow(Mat mat,PetscInt row,const PetscScalar v[]) 1468 { 1469 PetscErrorCode ierr; 1470 1471 PetscFunctionBeginHot; 1472 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 1473 PetscValidType(mat,1); 1474 MatCheckPreallocated(mat,1); 1475 PetscValidScalarPointer(v,3); 1476 PetscCheckFalse(mat->insertmode == ADD_VALUES,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add and insert values"); 1477 PetscCheckFalse(mat->factortype,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1478 mat->insertmode = INSERT_VALUES; 1479 1480 if (mat->assembled) { 1481 mat->was_assembled = PETSC_TRUE; 1482 mat->assembled = PETSC_FALSE; 1483 } 1484 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 1485 PetscCheckFalse(!mat->ops->setvaluesrow,PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 1486 ierr = (*mat->ops->setvaluesrow)(mat,row,v);CHKERRQ(ierr); 1487 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 1488 PetscFunctionReturn(0); 1489 } 1490 1491 /*@ 1492 MatSetValuesStencil - Inserts or adds a block of values into a matrix. 1493 Using structured grid indexing 1494 1495 Not Collective 1496 1497 Input Parameters: 1498 + mat - the matrix 1499 . m - number of rows being entered 1500 . idxm - grid coordinates (and component number when dof > 1) for matrix rows being entered 1501 . n - number of columns being entered 1502 . idxn - grid coordinates (and component number when dof > 1) for matrix columns being entered 1503 . v - a logically two-dimensional array of values 1504 - addv - either ADD_VALUES or INSERT_VALUES, where 1505 ADD_VALUES adds values to any existing entries, and 1506 INSERT_VALUES replaces existing entries with new values 1507 1508 Notes: 1509 By default the values, v, are row-oriented. See MatSetOption() for other options. 1510 1511 Calls to MatSetValuesStencil() with the INSERT_VALUES and ADD_VALUES 1512 options cannot be mixed without intervening calls to the assembly 1513 routines. 1514 1515 The grid coordinates are across the entire grid, not just the local portion 1516 1517 MatSetValuesStencil() uses 0-based row and column numbers in Fortran 1518 as well as in C. 1519 1520 For setting/accessing vector values via array coordinates you can use the DMDAVecGetArray() routine 1521 1522 In order to use this routine you must either obtain the matrix with DMCreateMatrix() 1523 or call MatSetLocalToGlobalMapping() and MatSetStencil() first. 1524 1525 The columns and rows in the stencil passed in MUST be contained within the 1526 ghost region of the given process as set with DMDACreateXXX() or MatSetStencil(). For example, 1527 if you create a DMDA with an overlap of one grid level and on a particular process its first 1528 local nonghost x logical coordinate is 6 (so its first ghost x logical coordinate is 5) the 1529 first i index you can use in your column and row indices in MatSetStencil() is 5. 1530 1531 In Fortran idxm and idxn should be declared as 1532 $ MatStencil idxm(4,m),idxn(4,n) 1533 and the values inserted using 1534 $ idxm(MatStencil_i,1) = i 1535 $ idxm(MatStencil_j,1) = j 1536 $ idxm(MatStencil_k,1) = k 1537 $ idxm(MatStencil_c,1) = c 1538 etc 1539 1540 For periodic boundary conditions use negative indices for values to the left (below 0; that are to be 1541 obtained by wrapping values from right edge). For values to the right of the last entry using that index plus one 1542 etc to obtain values that obtained by wrapping the values from the left edge. This does not work for anything but the 1543 DM_BOUNDARY_PERIODIC boundary type. 1544 1545 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 1546 a single value per point) you can skip filling those indices. 1547 1548 Inspired by the structured grid interface to the HYPRE package 1549 (https://computation.llnl.gov/projects/hypre-scalable-linear-solvers-multigrid-methods) 1550 1551 Efficiency Alert: 1552 The routine MatSetValuesBlockedStencil() may offer much better efficiency 1553 for users of block sparse formats (MATSEQBAIJ and MATMPIBAIJ). 1554 1555 Level: beginner 1556 1557 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal() 1558 MatSetValues(), MatSetValuesBlockedStencil(), MatSetStencil(), DMCreateMatrix(), DMDAVecGetArray(), MatStencil 1559 @*/ 1560 PetscErrorCode MatSetValuesStencil(Mat mat,PetscInt m,const MatStencil idxm[],PetscInt n,const MatStencil idxn[],const PetscScalar v[],InsertMode addv) 1561 { 1562 PetscErrorCode ierr; 1563 PetscInt buf[8192],*bufm=NULL,*bufn=NULL,*jdxm,*jdxn; 1564 PetscInt j,i,dim = mat->stencil.dim,*dims = mat->stencil.dims+1,tmp; 1565 PetscInt *starts = mat->stencil.starts,*dxm = (PetscInt*)idxm,*dxn = (PetscInt*)idxn,sdim = dim - (1 - (PetscInt)mat->stencil.noc); 1566 1567 PetscFunctionBegin; 1568 if (!m || !n) PetscFunctionReturn(0); /* no values to insert */ 1569 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 1570 PetscValidType(mat,1); 1571 PetscValidPointer(idxm,3); 1572 PetscValidPointer(idxn,5); 1573 1574 if ((m+n) <= (PetscInt)(sizeof(buf)/sizeof(PetscInt))) { 1575 jdxm = buf; jdxn = buf+m; 1576 } else { 1577 ierr = PetscMalloc2(m,&bufm,n,&bufn);CHKERRQ(ierr); 1578 jdxm = bufm; jdxn = bufn; 1579 } 1580 for (i=0; i<m; i++) { 1581 for (j=0; j<3-sdim; j++) dxm++; 1582 tmp = *dxm++ - starts[0]; 1583 for (j=0; j<dim-1; j++) { 1584 if ((*dxm++ - starts[j+1]) < 0 || tmp < 0) tmp = -1; 1585 else tmp = tmp*dims[j] + *(dxm-1) - starts[j+1]; 1586 } 1587 if (mat->stencil.noc) dxm++; 1588 jdxm[i] = tmp; 1589 } 1590 for (i=0; i<n; i++) { 1591 for (j=0; j<3-sdim; j++) dxn++; 1592 tmp = *dxn++ - starts[0]; 1593 for (j=0; j<dim-1; j++) { 1594 if ((*dxn++ - starts[j+1]) < 0 || tmp < 0) tmp = -1; 1595 else tmp = tmp*dims[j] + *(dxn-1) - starts[j+1]; 1596 } 1597 if (mat->stencil.noc) dxn++; 1598 jdxn[i] = tmp; 1599 } 1600 ierr = MatSetValuesLocal(mat,m,jdxm,n,jdxn,v,addv);CHKERRQ(ierr); 1601 ierr = PetscFree2(bufm,bufn);CHKERRQ(ierr); 1602 PetscFunctionReturn(0); 1603 } 1604 1605 /*@ 1606 MatSetValuesBlockedStencil - Inserts or adds a block of values into a matrix. 1607 Using structured grid indexing 1608 1609 Not Collective 1610 1611 Input Parameters: 1612 + mat - the matrix 1613 . m - number of rows being entered 1614 . idxm - grid coordinates for matrix rows being entered 1615 . n - number of columns being entered 1616 . idxn - grid coordinates for matrix columns being entered 1617 . v - a logically two-dimensional array of values 1618 - addv - either ADD_VALUES or INSERT_VALUES, where 1619 ADD_VALUES adds values to any existing entries, and 1620 INSERT_VALUES replaces existing entries with new values 1621 1622 Notes: 1623 By default the values, v, are row-oriented and unsorted. 1624 See MatSetOption() for other options. 1625 1626 Calls to MatSetValuesBlockedStencil() with the INSERT_VALUES and ADD_VALUES 1627 options cannot be mixed without intervening calls to the assembly 1628 routines. 1629 1630 The grid coordinates are across the entire grid, not just the local portion 1631 1632 MatSetValuesBlockedStencil() uses 0-based row and column numbers in Fortran 1633 as well as in C. 1634 1635 For setting/accessing vector values via array coordinates you can use the DMDAVecGetArray() routine 1636 1637 In order to use this routine you must either obtain the matrix with DMCreateMatrix() 1638 or call MatSetBlockSize(), MatSetLocalToGlobalMapping() and MatSetStencil() first. 1639 1640 The columns and rows in the stencil passed in MUST be contained within the 1641 ghost region of the given process as set with DMDACreateXXX() or MatSetStencil(). For example, 1642 if you create a DMDA with an overlap of one grid level and on a particular process its first 1643 local nonghost x logical coordinate is 6 (so its first ghost x logical coordinate is 5) the 1644 first i index you can use in your column and row indices in MatSetStencil() is 5. 1645 1646 In Fortran idxm and idxn should be declared as 1647 $ MatStencil idxm(4,m),idxn(4,n) 1648 and the values inserted using 1649 $ idxm(MatStencil_i,1) = i 1650 $ idxm(MatStencil_j,1) = j 1651 $ idxm(MatStencil_k,1) = k 1652 etc 1653 1654 Negative indices may be passed in idxm and idxn, these rows and columns are 1655 simply ignored. This allows easily inserting element stiffness matrices 1656 with homogeneous Dirchlet boundary conditions that you don't want represented 1657 in the matrix. 1658 1659 Inspired by the structured grid interface to the HYPRE package 1660 (https://computation.llnl.gov/projects/hypre-scalable-linear-solvers-multigrid-methods) 1661 1662 Level: beginner 1663 1664 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal() 1665 MatSetValues(), MatSetValuesStencil(), MatSetStencil(), DMCreateMatrix(), DMDAVecGetArray(), MatStencil, 1666 MatSetBlockSize(), MatSetLocalToGlobalMapping() 1667 @*/ 1668 PetscErrorCode MatSetValuesBlockedStencil(Mat mat,PetscInt m,const MatStencil idxm[],PetscInt n,const MatStencil idxn[],const PetscScalar v[],InsertMode addv) 1669 { 1670 PetscErrorCode ierr; 1671 PetscInt buf[8192],*bufm=NULL,*bufn=NULL,*jdxm,*jdxn; 1672 PetscInt j,i,dim = mat->stencil.dim,*dims = mat->stencil.dims+1,tmp; 1673 PetscInt *starts = mat->stencil.starts,*dxm = (PetscInt*)idxm,*dxn = (PetscInt*)idxn,sdim = dim - (1 - (PetscInt)mat->stencil.noc); 1674 1675 PetscFunctionBegin; 1676 if (!m || !n) PetscFunctionReturn(0); /* no values to insert */ 1677 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 1678 PetscValidType(mat,1); 1679 PetscValidPointer(idxm,3); 1680 PetscValidPointer(idxn,5); 1681 PetscValidScalarPointer(v,6); 1682 1683 if ((m+n) <= (PetscInt)(sizeof(buf)/sizeof(PetscInt))) { 1684 jdxm = buf; jdxn = buf+m; 1685 } else { 1686 ierr = PetscMalloc2(m,&bufm,n,&bufn);CHKERRQ(ierr); 1687 jdxm = bufm; jdxn = bufn; 1688 } 1689 for (i=0; i<m; i++) { 1690 for (j=0; j<3-sdim; j++) dxm++; 1691 tmp = *dxm++ - starts[0]; 1692 for (j=0; j<sdim-1; j++) { 1693 if ((*dxm++ - starts[j+1]) < 0 || tmp < 0) tmp = -1; 1694 else tmp = tmp*dims[j] + *(dxm-1) - starts[j+1]; 1695 } 1696 dxm++; 1697 jdxm[i] = tmp; 1698 } 1699 for (i=0; i<n; i++) { 1700 for (j=0; j<3-sdim; j++) dxn++; 1701 tmp = *dxn++ - starts[0]; 1702 for (j=0; j<sdim-1; j++) { 1703 if ((*dxn++ - starts[j+1]) < 0 || tmp < 0) tmp = -1; 1704 else tmp = tmp*dims[j] + *(dxn-1) - starts[j+1]; 1705 } 1706 dxn++; 1707 jdxn[i] = tmp; 1708 } 1709 ierr = MatSetValuesBlockedLocal(mat,m,jdxm,n,jdxn,v,addv);CHKERRQ(ierr); 1710 ierr = PetscFree2(bufm,bufn);CHKERRQ(ierr); 1711 PetscFunctionReturn(0); 1712 } 1713 1714 /*@ 1715 MatSetStencil - Sets the grid information for setting values into a matrix via 1716 MatSetValuesStencil() 1717 1718 Not Collective 1719 1720 Input Parameters: 1721 + mat - the matrix 1722 . dim - dimension of the grid 1, 2, or 3 1723 . dims - number of grid points in x, y, and z direction, including ghost points on your processor 1724 . starts - starting point of ghost nodes on your processor in x, y, and z direction 1725 - dof - number of degrees of freedom per node 1726 1727 Inspired by the structured grid interface to the HYPRE package 1728 (www.llnl.gov/CASC/hyper) 1729 1730 For matrices generated with DMCreateMatrix() this routine is automatically called and so not needed by the 1731 user. 1732 1733 Level: beginner 1734 1735 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal() 1736 MatSetValues(), MatSetValuesBlockedStencil(), MatSetValuesStencil() 1737 @*/ 1738 PetscErrorCode MatSetStencil(Mat mat,PetscInt dim,const PetscInt dims[],const PetscInt starts[],PetscInt dof) 1739 { 1740 PetscInt i; 1741 1742 PetscFunctionBegin; 1743 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 1744 PetscValidIntPointer(dims,3); 1745 PetscValidIntPointer(starts,4); 1746 1747 mat->stencil.dim = dim + (dof > 1); 1748 for (i=0; i<dim; i++) { 1749 mat->stencil.dims[i] = dims[dim-i-1]; /* copy the values in backwards */ 1750 mat->stencil.starts[i] = starts[dim-i-1]; 1751 } 1752 mat->stencil.dims[dim] = dof; 1753 mat->stencil.starts[dim] = 0; 1754 mat->stencil.noc = (PetscBool)(dof == 1); 1755 PetscFunctionReturn(0); 1756 } 1757 1758 /*@C 1759 MatSetValuesBlocked - Inserts or adds a block of values into a matrix. 1760 1761 Not Collective 1762 1763 Input Parameters: 1764 + mat - the matrix 1765 . v - a logically two-dimensional array of values 1766 . m, idxm - the number of block rows and their global block indices 1767 . n, idxn - the number of block columns and their global block indices 1768 - addv - either ADD_VALUES or INSERT_VALUES, where 1769 ADD_VALUES adds values to any existing entries, and 1770 INSERT_VALUES replaces existing entries with new values 1771 1772 Notes: 1773 If you create the matrix yourself (that is not with a call to DMCreateMatrix()) then you MUST call 1774 MatXXXXSetPreallocation() or MatSetUp() before using this routine. 1775 1776 The m and n count the NUMBER of blocks in the row direction and column direction, 1777 NOT the total number of rows/columns; for example, if the block size is 2 and 1778 you are passing in values for rows 2,3,4,5 then m would be 2 (not 4). 1779 The values in idxm would be 1 2; that is the first index for each block divided by 1780 the block size. 1781 1782 Note that you must call MatSetBlockSize() when constructing this matrix (before 1783 preallocating it). 1784 1785 By default the values, v, are row-oriented, so the layout of 1786 v is the same as for MatSetValues(). See MatSetOption() for other options. 1787 1788 Calls to MatSetValuesBlocked() with the INSERT_VALUES and ADD_VALUES 1789 options cannot be mixed without intervening calls to the assembly 1790 routines. 1791 1792 MatSetValuesBlocked() uses 0-based row and column numbers in Fortran 1793 as well as in C. 1794 1795 Negative indices may be passed in idxm and idxn, these rows and columns are 1796 simply ignored. This allows easily inserting element stiffness matrices 1797 with homogeneous Dirchlet boundary conditions that you don't want represented 1798 in the matrix. 1799 1800 Each time an entry is set within a sparse matrix via MatSetValues(), 1801 internal searching must be done to determine where to place the 1802 data in the matrix storage space. By instead inserting blocks of 1803 entries via MatSetValuesBlocked(), the overhead of matrix assembly is 1804 reduced. 1805 1806 Example: 1807 $ Suppose m=n=2 and block size(bs) = 2 The array is 1808 $ 1809 $ 1 2 | 3 4 1810 $ 5 6 | 7 8 1811 $ - - - | - - - 1812 $ 9 10 | 11 12 1813 $ 13 14 | 15 16 1814 $ 1815 $ v[] should be passed in like 1816 $ v[] = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16] 1817 $ 1818 $ If you are not using row oriented storage of v (that is you called MatSetOption(mat,MAT_ROW_ORIENTED,PETSC_FALSE)) then 1819 $ v[] = [1,5,9,13,2,6,10,14,3,7,11,15,4,8,12,16] 1820 1821 Level: intermediate 1822 1823 .seealso: MatSetBlockSize(), MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesBlockedLocal() 1824 @*/ 1825 PetscErrorCode MatSetValuesBlocked(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],const PetscScalar v[],InsertMode addv) 1826 { 1827 PetscErrorCode ierr; 1828 1829 PetscFunctionBeginHot; 1830 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 1831 PetscValidType(mat,1); 1832 if (!m || !n) PetscFunctionReturn(0); /* no values to insert */ 1833 PetscValidIntPointer(idxm,3); 1834 PetscValidIntPointer(idxn,5); 1835 MatCheckPreallocated(mat,1); 1836 if (mat->insertmode == NOT_SET_VALUES) mat->insertmode = addv; 1837 else PetscCheckFalse(mat->insertmode != addv,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values"); 1838 if (PetscDefined(USE_DEBUG)) { 1839 PetscCheckFalse(mat->factortype,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1840 PetscCheckFalse(!mat->ops->setvaluesblocked && !mat->ops->setvalues,PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 1841 } 1842 if (PetscDefined(USE_DEBUG)) { 1843 PetscInt rbs,cbs,M,N,i; 1844 ierr = MatGetBlockSizes(mat,&rbs,&cbs);CHKERRQ(ierr); 1845 ierr = MatGetSize(mat,&M,&N);CHKERRQ(ierr); 1846 for (i=0; i<m; i++) { 1847 PetscCheckFalse(idxm[i]*rbs >= M,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row block index %" PetscInt_FMT " (index %" PetscInt_FMT ") greater than row length %" PetscInt_FMT,i,idxm[i],M); 1848 } 1849 for (i=0; i<n; i++) { 1850 PetscCheckFalse(idxn[i]*cbs >= N,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column block index %" PetscInt_FMT " (index %" PetscInt_FMT ") great than column length %" PetscInt_FMT,i,idxn[i],N); 1851 } 1852 } 1853 if (mat->assembled) { 1854 mat->was_assembled = PETSC_TRUE; 1855 mat->assembled = PETSC_FALSE; 1856 } 1857 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 1858 if (mat->ops->setvaluesblocked) { 1859 ierr = (*mat->ops->setvaluesblocked)(mat,m,idxm,n,idxn,v,addv);CHKERRQ(ierr); 1860 } else { 1861 PetscInt buf[8192],*bufr=NULL,*bufc=NULL,*iidxm,*iidxn; 1862 PetscInt i,j,bs,cbs; 1863 1864 ierr = MatGetBlockSizes(mat,&bs,&cbs);CHKERRQ(ierr); 1865 if (m*bs+n*cbs <= (PetscInt)(sizeof(buf)/sizeof(PetscInt))) { 1866 iidxm = buf; 1867 iidxn = buf + m*bs; 1868 } else { 1869 ierr = PetscMalloc2(m*bs,&bufr,n*cbs,&bufc);CHKERRQ(ierr); 1870 iidxm = bufr; 1871 iidxn = bufc; 1872 } 1873 for (i=0; i<m; i++) { 1874 for (j=0; j<bs; j++) { 1875 iidxm[i*bs+j] = bs*idxm[i] + j; 1876 } 1877 } 1878 if (m != n || bs != cbs || idxm != idxn) { 1879 for (i=0; i<n; i++) { 1880 for (j=0; j<cbs; j++) { 1881 iidxn[i*cbs+j] = cbs*idxn[i] + j; 1882 } 1883 } 1884 } else iidxn = iidxm; 1885 ierr = MatSetValues(mat,m*bs,iidxm,n*cbs,iidxn,v,addv);CHKERRQ(ierr); 1886 ierr = PetscFree2(bufr,bufc);CHKERRQ(ierr); 1887 } 1888 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 1889 PetscFunctionReturn(0); 1890 } 1891 1892 /*@C 1893 MatGetValues - Gets a block of values from a matrix. 1894 1895 Not Collective; can only return values that are owned by the give process 1896 1897 Input Parameters: 1898 + mat - the matrix 1899 . v - a logically two-dimensional array for storing the values 1900 . m, idxm - the number of rows and their global indices 1901 - n, idxn - the number of columns and their global indices 1902 1903 Notes: 1904 The user must allocate space (m*n PetscScalars) for the values, v. 1905 The values, v, are then returned in a row-oriented format, 1906 analogous to that used by default in MatSetValues(). 1907 1908 MatGetValues() uses 0-based row and column numbers in 1909 Fortran as well as in C. 1910 1911 MatGetValues() requires that the matrix has been assembled 1912 with MatAssemblyBegin()/MatAssemblyEnd(). Thus, calls to 1913 MatSetValues() and MatGetValues() CANNOT be made in succession 1914 without intermediate matrix assembly. 1915 1916 Negative row or column indices will be ignored and those locations in v[] will be 1917 left unchanged. 1918 1919 For the standard row-based matrix formats, idxm[] can only contain rows owned by the requesting MPI rank. 1920 That is, rows with global index greater than or equal to rstart and less than rend where rstart and rend are obtainable 1921 from MatGetOwnershipRange(mat,&rstart,&rend). 1922 1923 Level: advanced 1924 1925 .seealso: MatGetRow(), MatCreateSubMatrices(), MatSetValues(), MatGetOwnershipRange(), MatGetValuesLocal(), MatGetValue() 1926 @*/ 1927 PetscErrorCode MatGetValues(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],PetscScalar v[]) 1928 { 1929 PetscErrorCode ierr; 1930 1931 PetscFunctionBegin; 1932 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 1933 PetscValidType(mat,1); 1934 if (!m || !n) PetscFunctionReturn(0); 1935 PetscValidIntPointer(idxm,3); 1936 PetscValidIntPointer(idxn,5); 1937 PetscValidScalarPointer(v,6); 1938 PetscCheckFalse(!mat->assembled,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1939 PetscCheckFalse(mat->factortype,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1940 PetscCheckFalse(!mat->ops->getvalues,PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 1941 MatCheckPreallocated(mat,1); 1942 1943 ierr = PetscLogEventBegin(MAT_GetValues,mat,0,0,0);CHKERRQ(ierr); 1944 ierr = (*mat->ops->getvalues)(mat,m,idxm,n,idxn,v);CHKERRQ(ierr); 1945 ierr = PetscLogEventEnd(MAT_GetValues,mat,0,0,0);CHKERRQ(ierr); 1946 PetscFunctionReturn(0); 1947 } 1948 1949 /*@C 1950 MatGetValuesLocal - retrieves values from certain locations in a matrix using the local numbering of the indices 1951 defined previously by MatSetLocalToGlobalMapping() 1952 1953 Not Collective 1954 1955 Input Parameters: 1956 + mat - the matrix 1957 . nrow, irow - number of rows and their local indices 1958 - ncol, icol - number of columns and their local indices 1959 1960 Output Parameter: 1961 . y - a logically two-dimensional array of values 1962 1963 Notes: 1964 If you create the matrix yourself (that is not with a call to DMCreateMatrix()) then you MUST call MatSetLocalToGlobalMapping() before using this routine. 1965 1966 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, 1967 are greater than or equal to rstart and less than rend where rstart and rend are obtainable from MatGetOwnershipRange(mat,&rstart,&rend). One can 1968 determine if the resulting global row associated with the local row r is owned by the requesting MPI rank by applying the ISLocalToGlobalMapping set 1969 with MatSetLocalToGlobalMapping(). 1970 1971 Developer Notes: 1972 This is labelled with C so does not automatically generate Fortran stubs and interfaces 1973 because it requires multiple Fortran interfaces depending on which arguments are scalar or arrays. 1974 1975 Level: advanced 1976 1977 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetLocalToGlobalMapping(), 1978 MatSetValuesLocal(), MatGetValues() 1979 @*/ 1980 PetscErrorCode MatGetValuesLocal(Mat mat,PetscInt nrow,const PetscInt irow[],PetscInt ncol,const PetscInt icol[],PetscScalar y[]) 1981 { 1982 PetscErrorCode ierr; 1983 1984 PetscFunctionBeginHot; 1985 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 1986 PetscValidType(mat,1); 1987 MatCheckPreallocated(mat,1); 1988 if (!nrow || !ncol) PetscFunctionReturn(0); /* no values to retrieve */ 1989 PetscValidIntPointer(irow,3); 1990 PetscValidIntPointer(icol,5); 1991 if (PetscDefined(USE_DEBUG)) { 1992 PetscCheckFalse(mat->factortype,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1993 PetscCheckFalse(!mat->ops->getvalueslocal && !mat->ops->getvalues,PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 1994 } 1995 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1996 ierr = PetscLogEventBegin(MAT_GetValues,mat,0,0,0);CHKERRQ(ierr); 1997 if (mat->ops->getvalueslocal) { 1998 ierr = (*mat->ops->getvalueslocal)(mat,nrow,irow,ncol,icol,y);CHKERRQ(ierr); 1999 } else { 2000 PetscInt buf[8192],*bufr=NULL,*bufc=NULL,*irowm,*icolm; 2001 if ((nrow+ncol) <= (PetscInt)(sizeof(buf)/sizeof(PetscInt))) { 2002 irowm = buf; icolm = buf+nrow; 2003 } else { 2004 ierr = PetscMalloc2(nrow,&bufr,ncol,&bufc);CHKERRQ(ierr); 2005 irowm = bufr; icolm = bufc; 2006 } 2007 PetscCheckFalse(!mat->rmap->mapping,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"MatGetValuesLocal() cannot proceed without local-to-global row mapping (See MatSetLocalToGlobalMapping())."); 2008 PetscCheckFalse(!mat->cmap->mapping,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"MatGetValuesLocal() cannot proceed without local-to-global column mapping (See MatSetLocalToGlobalMapping())."); 2009 ierr = ISLocalToGlobalMappingApply(mat->rmap->mapping,nrow,irow,irowm);CHKERRQ(ierr); 2010 ierr = ISLocalToGlobalMappingApply(mat->cmap->mapping,ncol,icol,icolm);CHKERRQ(ierr); 2011 ierr = MatGetValues(mat,nrow,irowm,ncol,icolm,y);CHKERRQ(ierr); 2012 ierr = PetscFree2(bufr,bufc);CHKERRQ(ierr); 2013 } 2014 ierr = PetscLogEventEnd(MAT_GetValues,mat,0,0,0);CHKERRQ(ierr); 2015 PetscFunctionReturn(0); 2016 } 2017 2018 /*@ 2019 MatSetValuesBatch - Adds (ADD_VALUES) many blocks of values into a matrix at once. The blocks must all be square and 2020 the same size. Currently, this can only be called once and creates the given matrix. 2021 2022 Not Collective 2023 2024 Input Parameters: 2025 + mat - the matrix 2026 . nb - the number of blocks 2027 . bs - the number of rows (and columns) in each block 2028 . rows - a concatenation of the rows for each block 2029 - v - a concatenation of logically two-dimensional arrays of values 2030 2031 Notes: 2032 In the future, we will extend this routine to handle rectangular blocks, and to allow multiple calls for a given matrix. 2033 2034 Level: advanced 2035 2036 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(), 2037 InsertMode, INSERT_VALUES, ADD_VALUES, MatSetValues() 2038 @*/ 2039 PetscErrorCode MatSetValuesBatch(Mat mat, PetscInt nb, PetscInt bs, PetscInt rows[], const PetscScalar v[]) 2040 { 2041 PetscErrorCode ierr; 2042 2043 PetscFunctionBegin; 2044 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2045 PetscValidType(mat,1); 2046 PetscValidIntPointer(rows,4); 2047 PetscValidScalarPointer(v,5); 2048 PetscAssert(!mat->factortype,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2049 2050 ierr = PetscLogEventBegin(MAT_SetValuesBatch,mat,0,0,0);CHKERRQ(ierr); 2051 if (mat->ops->setvaluesbatch) { 2052 ierr = (*mat->ops->setvaluesbatch)(mat,nb,bs,rows,v);CHKERRQ(ierr); 2053 } else { 2054 PetscInt b; 2055 for (b = 0; b < nb; ++b) { 2056 ierr = MatSetValues(mat, bs, &rows[b*bs], bs, &rows[b*bs], &v[b*bs*bs], ADD_VALUES);CHKERRQ(ierr); 2057 } 2058 } 2059 ierr = PetscLogEventEnd(MAT_SetValuesBatch,mat,0,0,0);CHKERRQ(ierr); 2060 PetscFunctionReturn(0); 2061 } 2062 2063 /*@ 2064 MatSetLocalToGlobalMapping - Sets a local-to-global numbering for use by 2065 the routine MatSetValuesLocal() to allow users to insert matrix entries 2066 using a local (per-processor) numbering. 2067 2068 Not Collective 2069 2070 Input Parameters: 2071 + x - the matrix 2072 . rmapping - row mapping created with ISLocalToGlobalMappingCreate() or ISLocalToGlobalMappingCreateIS() 2073 - cmapping - column mapping 2074 2075 Level: intermediate 2076 2077 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesLocal(), MatGetValuesLocal() 2078 @*/ 2079 PetscErrorCode MatSetLocalToGlobalMapping(Mat x,ISLocalToGlobalMapping rmapping,ISLocalToGlobalMapping cmapping) 2080 { 2081 PetscErrorCode ierr; 2082 2083 PetscFunctionBegin; 2084 PetscValidHeaderSpecific(x,MAT_CLASSID,1); 2085 PetscValidType(x,1); 2086 if (rmapping) PetscValidHeaderSpecific(rmapping,IS_LTOGM_CLASSID,2); 2087 if (cmapping) PetscValidHeaderSpecific(cmapping,IS_LTOGM_CLASSID,3); 2088 if (x->ops->setlocaltoglobalmapping) { 2089 ierr = (*x->ops->setlocaltoglobalmapping)(x,rmapping,cmapping);CHKERRQ(ierr); 2090 } else { 2091 ierr = PetscLayoutSetISLocalToGlobalMapping(x->rmap,rmapping);CHKERRQ(ierr); 2092 ierr = PetscLayoutSetISLocalToGlobalMapping(x->cmap,cmapping);CHKERRQ(ierr); 2093 } 2094 PetscFunctionReturn(0); 2095 } 2096 2097 /*@ 2098 MatGetLocalToGlobalMapping - Gets the local-to-global numbering set by MatSetLocalToGlobalMapping() 2099 2100 Not Collective 2101 2102 Input Parameter: 2103 . A - the matrix 2104 2105 Output Parameters: 2106 + rmapping - row mapping 2107 - cmapping - column mapping 2108 2109 Level: advanced 2110 2111 .seealso: MatSetValuesLocal() 2112 @*/ 2113 PetscErrorCode MatGetLocalToGlobalMapping(Mat A,ISLocalToGlobalMapping *rmapping,ISLocalToGlobalMapping *cmapping) 2114 { 2115 PetscFunctionBegin; 2116 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 2117 PetscValidType(A,1); 2118 if (rmapping) PetscValidPointer(rmapping,2); 2119 if (cmapping) PetscValidPointer(cmapping,3); 2120 if (rmapping) *rmapping = A->rmap->mapping; 2121 if (cmapping) *cmapping = A->cmap->mapping; 2122 PetscFunctionReturn(0); 2123 } 2124 2125 /*@ 2126 MatSetLayouts - Sets the PetscLayout objects for rows and columns of a matrix 2127 2128 Logically Collective on A 2129 2130 Input Parameters: 2131 + A - the matrix 2132 . rmap - row layout 2133 - cmap - column layout 2134 2135 Level: advanced 2136 2137 .seealso: MatCreateVecs(), MatGetLocalToGlobalMapping(), MatGetLayouts() 2138 @*/ 2139 PetscErrorCode MatSetLayouts(Mat A,PetscLayout rmap,PetscLayout cmap) 2140 { 2141 PetscErrorCode ierr; 2142 2143 PetscFunctionBegin; 2144 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 2145 2146 ierr = PetscLayoutReference(rmap,&A->rmap);CHKERRQ(ierr); 2147 ierr = PetscLayoutReference(cmap,&A->cmap);CHKERRQ(ierr); 2148 PetscFunctionReturn(0); 2149 } 2150 2151 /*@ 2152 MatGetLayouts - Gets the PetscLayout objects for rows and columns 2153 2154 Not Collective 2155 2156 Input Parameter: 2157 . A - the matrix 2158 2159 Output Parameters: 2160 + rmap - row layout 2161 - cmap - column layout 2162 2163 Level: advanced 2164 2165 .seealso: MatCreateVecs(), MatGetLocalToGlobalMapping(), MatSetLayouts() 2166 @*/ 2167 PetscErrorCode MatGetLayouts(Mat A,PetscLayout *rmap,PetscLayout *cmap) 2168 { 2169 PetscFunctionBegin; 2170 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 2171 PetscValidType(A,1); 2172 if (rmap) PetscValidPointer(rmap,2); 2173 if (cmap) PetscValidPointer(cmap,3); 2174 if (rmap) *rmap = A->rmap; 2175 if (cmap) *cmap = A->cmap; 2176 PetscFunctionReturn(0); 2177 } 2178 2179 /*@C 2180 MatSetValuesLocal - Inserts or adds values into certain locations of a matrix, 2181 using a local numbering of the nodes. 2182 2183 Not Collective 2184 2185 Input Parameters: 2186 + mat - the matrix 2187 . nrow, irow - number of rows and their local indices 2188 . ncol, icol - number of columns and their local indices 2189 . y - a logically two-dimensional array of values 2190 - addv - either INSERT_VALUES or ADD_VALUES, where 2191 ADD_VALUES adds values to any existing entries, and 2192 INSERT_VALUES replaces existing entries with new values 2193 2194 Notes: 2195 If you create the matrix yourself (that is not with a call to DMCreateMatrix()) then you MUST call MatXXXXSetPreallocation() or 2196 MatSetUp() before using this routine 2197 2198 If you create the matrix yourself (that is not with a call to DMCreateMatrix()) then you MUST call MatSetLocalToGlobalMapping() before using this routine 2199 2200 Calls to MatSetValuesLocal() with the INSERT_VALUES and ADD_VALUES 2201 options cannot be mixed without intervening calls to the assembly 2202 routines. 2203 2204 These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd() 2205 MUST be called after all calls to MatSetValuesLocal() have been completed. 2206 2207 Level: intermediate 2208 2209 Developer Notes: 2210 This is labeled with C so does not automatically generate Fortran stubs and interfaces 2211 because it requires multiple Fortran interfaces depending on which arguments are scalar or arrays. 2212 2213 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetLocalToGlobalMapping(), 2214 MatSetValueLocal(), MatGetValuesLocal() 2215 @*/ 2216 PetscErrorCode MatSetValuesLocal(Mat mat,PetscInt nrow,const PetscInt irow[],PetscInt ncol,const PetscInt icol[],const PetscScalar y[],InsertMode addv) 2217 { 2218 PetscErrorCode ierr; 2219 2220 PetscFunctionBeginHot; 2221 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2222 PetscValidType(mat,1); 2223 MatCheckPreallocated(mat,1); 2224 if (!nrow || !ncol) PetscFunctionReturn(0); /* no values to insert */ 2225 PetscValidIntPointer(irow,3); 2226 PetscValidIntPointer(icol,5); 2227 if (mat->insertmode == NOT_SET_VALUES) mat->insertmode = addv; 2228 else PetscCheckFalse(mat->insertmode != addv,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values"); 2229 if (PetscDefined(USE_DEBUG)) { 2230 PetscCheckFalse(mat->factortype,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2231 PetscCheckFalse(!mat->ops->setvalueslocal && !mat->ops->setvalues,PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 2232 } 2233 2234 if (mat->assembled) { 2235 mat->was_assembled = PETSC_TRUE; 2236 mat->assembled = PETSC_FALSE; 2237 } 2238 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 2239 if (mat->ops->setvalueslocal) { 2240 ierr = (*mat->ops->setvalueslocal)(mat,nrow,irow,ncol,icol,y,addv);CHKERRQ(ierr); 2241 } else { 2242 PetscInt buf[8192],*bufr=NULL,*bufc=NULL; 2243 const PetscInt *irowm,*icolm; 2244 2245 if ((!mat->rmap->mapping && !mat->cmap->mapping) || (nrow+ncol) <= (PetscInt)(sizeof(buf)/sizeof(PetscInt))) { 2246 bufr = buf; 2247 bufc = buf + nrow; 2248 irowm = bufr; 2249 icolm = bufc; 2250 } else { 2251 ierr = PetscMalloc2(nrow,&bufr,ncol,&bufc);CHKERRQ(ierr); 2252 irowm = bufr; 2253 icolm = bufc; 2254 } 2255 if (mat->rmap->mapping) { ierr = ISLocalToGlobalMappingApply(mat->rmap->mapping,nrow,irow,bufr);CHKERRQ(ierr); } 2256 else irowm = irow; 2257 if (mat->cmap->mapping) { 2258 if (mat->cmap->mapping != mat->rmap->mapping || ncol != nrow || icol != irow) { 2259 ierr = ISLocalToGlobalMappingApply(mat->cmap->mapping,ncol,icol,bufc);CHKERRQ(ierr); 2260 } else icolm = irowm; 2261 } else icolm = icol; 2262 ierr = MatSetValues(mat,nrow,irowm,ncol,icolm,y,addv);CHKERRQ(ierr); 2263 if (bufr != buf) { ierr = PetscFree2(bufr,bufc);CHKERRQ(ierr); } 2264 } 2265 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 2266 PetscFunctionReturn(0); 2267 } 2268 2269 /*@C 2270 MatSetValuesBlockedLocal - Inserts or adds values into certain locations of a matrix, 2271 using a local ordering of the nodes a block at a time. 2272 2273 Not Collective 2274 2275 Input Parameters: 2276 + x - the matrix 2277 . nrow, irow - number of rows and their local indices 2278 . ncol, icol - number of columns and their local indices 2279 . y - a logically two-dimensional array of values 2280 - addv - either INSERT_VALUES or ADD_VALUES, where 2281 ADD_VALUES adds values to any existing entries, and 2282 INSERT_VALUES replaces existing entries with new values 2283 2284 Notes: 2285 If you create the matrix yourself (that is not with a call to DMCreateMatrix()) then you MUST call MatXXXXSetPreallocation() or 2286 MatSetUp() before using this routine 2287 2288 If you create the matrix yourself (that is not with a call to DMCreateMatrix()) then you MUST call MatSetBlockSize() and MatSetLocalToGlobalMapping() 2289 before using this routineBefore calling MatSetValuesLocal(), the user must first set the 2290 2291 Calls to MatSetValuesBlockedLocal() with the INSERT_VALUES and ADD_VALUES 2292 options cannot be mixed without intervening calls to the assembly 2293 routines. 2294 2295 These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd() 2296 MUST be called after all calls to MatSetValuesBlockedLocal() have been completed. 2297 2298 Level: intermediate 2299 2300 Developer Notes: 2301 This is labeled with C so does not automatically generate Fortran stubs and interfaces 2302 because it requires multiple Fortran interfaces depending on which arguments are scalar or arrays. 2303 2304 .seealso: MatSetBlockSize(), MatSetLocalToGlobalMapping(), MatAssemblyBegin(), MatAssemblyEnd(), 2305 MatSetValuesLocal(), MatSetValuesBlocked() 2306 @*/ 2307 PetscErrorCode MatSetValuesBlockedLocal(Mat mat,PetscInt nrow,const PetscInt irow[],PetscInt ncol,const PetscInt icol[],const PetscScalar y[],InsertMode addv) 2308 { 2309 PetscErrorCode ierr; 2310 2311 PetscFunctionBeginHot; 2312 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2313 PetscValidType(mat,1); 2314 MatCheckPreallocated(mat,1); 2315 if (!nrow || !ncol) PetscFunctionReturn(0); /* no values to insert */ 2316 PetscValidIntPointer(irow,3); 2317 PetscValidIntPointer(icol,5); 2318 if (mat->insertmode == NOT_SET_VALUES) mat->insertmode = addv; 2319 else PetscCheckFalse(mat->insertmode != addv,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values"); 2320 if (PetscDefined(USE_DEBUG)) { 2321 PetscCheckFalse(mat->factortype,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2322 PetscCheckFalse(!mat->ops->setvaluesblockedlocal && !mat->ops->setvaluesblocked && !mat->ops->setvalueslocal && !mat->ops->setvalues,PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 2323 } 2324 2325 if (mat->assembled) { 2326 mat->was_assembled = PETSC_TRUE; 2327 mat->assembled = PETSC_FALSE; 2328 } 2329 if (PetscUnlikelyDebug(mat->rmap->mapping)) { /* Condition on the mapping existing, because MatSetValuesBlockedLocal_IS does not require it to be set. */ 2330 PetscInt irbs, rbs; 2331 ierr = MatGetBlockSizes(mat, &rbs, NULL);CHKERRQ(ierr); 2332 ierr = ISLocalToGlobalMappingGetBlockSize(mat->rmap->mapping,&irbs);CHKERRQ(ierr); 2333 PetscCheckFalse(rbs != irbs,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Different row block sizes! mat %" PetscInt_FMT ", row l2g map %" PetscInt_FMT,rbs,irbs); 2334 } 2335 if (PetscUnlikelyDebug(mat->cmap->mapping)) { 2336 PetscInt icbs, cbs; 2337 ierr = MatGetBlockSizes(mat,NULL,&cbs);CHKERRQ(ierr); 2338 ierr = ISLocalToGlobalMappingGetBlockSize(mat->cmap->mapping,&icbs);CHKERRQ(ierr); 2339 PetscCheckFalse(cbs != icbs,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Different col block sizes! mat %" PetscInt_FMT ", col l2g map %" PetscInt_FMT,cbs,icbs); 2340 } 2341 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 2342 if (mat->ops->setvaluesblockedlocal) { 2343 ierr = (*mat->ops->setvaluesblockedlocal)(mat,nrow,irow,ncol,icol,y,addv);CHKERRQ(ierr); 2344 } else { 2345 PetscInt buf[8192],*bufr=NULL,*bufc=NULL; 2346 const PetscInt *irowm,*icolm; 2347 2348 if ((!mat->rmap->mapping && !mat->cmap->mapping) || (nrow+ncol) <= (PetscInt)(sizeof(buf)/sizeof(PetscInt))) { 2349 bufr = buf; 2350 bufc = buf + nrow; 2351 irowm = bufr; 2352 icolm = bufc; 2353 } else { 2354 ierr = PetscMalloc2(nrow,&bufr,ncol,&bufc);CHKERRQ(ierr); 2355 irowm = bufr; 2356 icolm = bufc; 2357 } 2358 if (mat->rmap->mapping) { ierr = ISLocalToGlobalMappingApplyBlock(mat->rmap->mapping,nrow,irow,bufr);CHKERRQ(ierr); } 2359 else irowm = irow; 2360 if (mat->cmap->mapping) { 2361 if (mat->cmap->mapping != mat->rmap->mapping || ncol != nrow || icol != irow) { 2362 ierr = ISLocalToGlobalMappingApplyBlock(mat->cmap->mapping,ncol,icol,bufc);CHKERRQ(ierr); 2363 } else icolm = irowm; 2364 } else icolm = icol; 2365 ierr = MatSetValuesBlocked(mat,nrow,irowm,ncol,icolm,y,addv);CHKERRQ(ierr); 2366 if (bufr != buf) { ierr = PetscFree2(bufr,bufc);CHKERRQ(ierr); } 2367 } 2368 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 2369 PetscFunctionReturn(0); 2370 } 2371 2372 /*@ 2373 MatMultDiagonalBlock - Computes the matrix-vector product, y = Dx. Where D is defined by the inode or block structure of the diagonal 2374 2375 Collective on Mat 2376 2377 Input Parameters: 2378 + mat - the matrix 2379 - x - the vector to be multiplied 2380 2381 Output Parameters: 2382 . y - the result 2383 2384 Notes: 2385 The vectors x and y cannot be the same. I.e., one cannot 2386 call MatMult(A,y,y). 2387 2388 Level: developer 2389 2390 .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd() 2391 @*/ 2392 PetscErrorCode MatMultDiagonalBlock(Mat mat,Vec x,Vec y) 2393 { 2394 PetscErrorCode ierr; 2395 2396 PetscFunctionBegin; 2397 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2398 PetscValidType(mat,1); 2399 PetscValidHeaderSpecific(x,VEC_CLASSID,2); 2400 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 2401 2402 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2403 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2404 PetscCheckFalse(x == y,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors"); 2405 MatCheckPreallocated(mat,1); 2406 2407 PetscCheckFalse(!mat->ops->multdiagonalblock,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s does not have a multiply defined",((PetscObject)mat)->type_name); 2408 ierr = (*mat->ops->multdiagonalblock)(mat,x,y);CHKERRQ(ierr); 2409 ierr = PetscObjectStateIncrease((PetscObject)y);CHKERRQ(ierr); 2410 PetscFunctionReturn(0); 2411 } 2412 2413 /* --------------------------------------------------------*/ 2414 /*@ 2415 MatMult - Computes the matrix-vector product, y = Ax. 2416 2417 Neighbor-wise Collective on Mat 2418 2419 Input Parameters: 2420 + mat - the matrix 2421 - x - the vector to be multiplied 2422 2423 Output Parameters: 2424 . y - the result 2425 2426 Notes: 2427 The vectors x and y cannot be the same. I.e., one cannot 2428 call MatMult(A,y,y). 2429 2430 Level: beginner 2431 2432 .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd() 2433 @*/ 2434 PetscErrorCode MatMult(Mat mat,Vec x,Vec y) 2435 { 2436 PetscErrorCode ierr; 2437 2438 PetscFunctionBegin; 2439 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2440 PetscValidType(mat,1); 2441 PetscValidHeaderSpecific(x,VEC_CLASSID,2); 2442 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 2443 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2444 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2445 PetscCheckFalse(x == y,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors"); 2446 PetscCheckFalse(mat->cmap->N != x->map->N,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %" PetscInt_FMT " %" PetscInt_FMT,mat->cmap->N,x->map->N); 2447 PetscCheckFalse(mat->rmap->N != y->map->N,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %" PetscInt_FMT " %" PetscInt_FMT,mat->rmap->N,y->map->N); 2448 PetscCheckFalse(mat->cmap->n != x->map->n,PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: local dim %" PetscInt_FMT " %" PetscInt_FMT,mat->cmap->n,x->map->n); 2449 PetscCheckFalse(mat->rmap->n != y->map->n,PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: local dim %" PetscInt_FMT " %" PetscInt_FMT,mat->rmap->n,y->map->n); 2450 ierr = VecSetErrorIfLocked(y,3);CHKERRQ(ierr); 2451 if (mat->erroriffailure) {ierr = VecValidValues(x,2,PETSC_TRUE);CHKERRQ(ierr);} 2452 MatCheckPreallocated(mat,1); 2453 2454 ierr = VecLockReadPush(x);CHKERRQ(ierr); 2455 PetscCheckFalse(!mat->ops->mult,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s does not have a multiply defined",((PetscObject)mat)->type_name); 2456 ierr = PetscLogEventBegin(MAT_Mult,mat,x,y,0);CHKERRQ(ierr); 2457 ierr = (*mat->ops->mult)(mat,x,y);CHKERRQ(ierr); 2458 ierr = PetscLogEventEnd(MAT_Mult,mat,x,y,0);CHKERRQ(ierr); 2459 if (mat->erroriffailure) {ierr = VecValidValues(y,3,PETSC_FALSE);CHKERRQ(ierr);} 2460 ierr = VecLockReadPop(x);CHKERRQ(ierr); 2461 PetscFunctionReturn(0); 2462 } 2463 2464 /*@ 2465 MatMultTranspose - Computes matrix transpose times a vector y = A^T * x. 2466 2467 Neighbor-wise Collective on Mat 2468 2469 Input Parameters: 2470 + mat - the matrix 2471 - x - the vector to be multiplied 2472 2473 Output Parameters: 2474 . y - the result 2475 2476 Notes: 2477 The vectors x and y cannot be the same. I.e., one cannot 2478 call MatMultTranspose(A,y,y). 2479 2480 For complex numbers this does NOT compute the Hermitian (complex conjugate) transpose multiple, 2481 use MatMultHermitianTranspose() 2482 2483 Level: beginner 2484 2485 .seealso: MatMult(), MatMultAdd(), MatMultTransposeAdd(), MatMultHermitianTranspose(), MatTranspose() 2486 @*/ 2487 PetscErrorCode MatMultTranspose(Mat mat,Vec x,Vec y) 2488 { 2489 PetscErrorCode (*op)(Mat,Vec,Vec)=NULL,ierr; 2490 2491 PetscFunctionBegin; 2492 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2493 PetscValidType(mat,1); 2494 PetscValidHeaderSpecific(x,VEC_CLASSID,2); 2495 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 2496 2497 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2498 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2499 PetscCheckFalse(x == y,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors"); 2500 PetscCheckFalse(mat->cmap->N != y->map->N,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %" PetscInt_FMT " %" PetscInt_FMT,mat->cmap->N,y->map->N); 2501 PetscCheckFalse(mat->rmap->N != x->map->N,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %" PetscInt_FMT " %" PetscInt_FMT,mat->rmap->N,x->map->N); 2502 PetscCheckFalse(mat->cmap->n != y->map->n,PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: local dim %" PetscInt_FMT " %" PetscInt_FMT,mat->cmap->n,y->map->n); 2503 PetscCheckFalse(mat->rmap->n != x->map->n,PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: local dim %" PetscInt_FMT " %" PetscInt_FMT,mat->rmap->n,x->map->n); 2504 if (mat->erroriffailure) {ierr = VecValidValues(x,2,PETSC_TRUE);CHKERRQ(ierr);} 2505 MatCheckPreallocated(mat,1); 2506 2507 if (!mat->ops->multtranspose) { 2508 if (mat->symmetric && mat->ops->mult) op = mat->ops->mult; 2509 PetscCheckFalse(!op,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); 2510 } else op = mat->ops->multtranspose; 2511 ierr = PetscLogEventBegin(MAT_MultTranspose,mat,x,y,0);CHKERRQ(ierr); 2512 ierr = VecLockReadPush(x);CHKERRQ(ierr); 2513 ierr = (*op)(mat,x,y);CHKERRQ(ierr); 2514 ierr = VecLockReadPop(x);CHKERRQ(ierr); 2515 ierr = PetscLogEventEnd(MAT_MultTranspose,mat,x,y,0);CHKERRQ(ierr); 2516 ierr = PetscObjectStateIncrease((PetscObject)y);CHKERRQ(ierr); 2517 if (mat->erroriffailure) {ierr = VecValidValues(y,3,PETSC_FALSE);CHKERRQ(ierr);} 2518 PetscFunctionReturn(0); 2519 } 2520 2521 /*@ 2522 MatMultHermitianTranspose - Computes matrix Hermitian transpose times a vector. 2523 2524 Neighbor-wise Collective on Mat 2525 2526 Input Parameters: 2527 + mat - the matrix 2528 - x - the vector to be multilplied 2529 2530 Output Parameters: 2531 . y - the result 2532 2533 Notes: 2534 The vectors x and y cannot be the same. I.e., one cannot 2535 call MatMultHermitianTranspose(A,y,y). 2536 2537 Also called the conjugate transpose, complex conjugate transpose, or adjoint. 2538 2539 For real numbers MatMultTranspose() and MatMultHermitianTranspose() are identical. 2540 2541 Level: beginner 2542 2543 .seealso: MatMult(), MatMultAdd(), MatMultHermitianTransposeAdd(), MatMultTranspose() 2544 @*/ 2545 PetscErrorCode MatMultHermitianTranspose(Mat mat,Vec x,Vec y) 2546 { 2547 PetscErrorCode ierr; 2548 2549 PetscFunctionBegin; 2550 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2551 PetscValidType(mat,1); 2552 PetscValidHeaderSpecific(x,VEC_CLASSID,2); 2553 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 2554 2555 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2556 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2557 PetscCheckFalse(x == y,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors"); 2558 PetscCheckFalse(mat->cmap->N != y->map->N,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %" PetscInt_FMT " %" PetscInt_FMT,mat->cmap->N,y->map->N); 2559 PetscCheckFalse(mat->rmap->N != x->map->N,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %" PetscInt_FMT " %" PetscInt_FMT,mat->rmap->N,x->map->N); 2560 PetscCheckFalse(mat->cmap->n != y->map->n,PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: local dim %" PetscInt_FMT " %" PetscInt_FMT,mat->cmap->n,y->map->n); 2561 PetscCheckFalse(mat->rmap->n != x->map->n,PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: local dim %" PetscInt_FMT " %" PetscInt_FMT,mat->rmap->n,x->map->n); 2562 MatCheckPreallocated(mat,1); 2563 2564 ierr = PetscLogEventBegin(MAT_MultHermitianTranspose,mat,x,y,0);CHKERRQ(ierr); 2565 #if defined(PETSC_USE_COMPLEX) 2566 if (mat->ops->multhermitiantranspose || (mat->hermitian && mat->ops->mult)) { 2567 ierr = VecLockReadPush(x);CHKERRQ(ierr); 2568 if (mat->ops->multhermitiantranspose) { 2569 ierr = (*mat->ops->multhermitiantranspose)(mat,x,y);CHKERRQ(ierr); 2570 } else { 2571 ierr = (*mat->ops->mult)(mat,x,y);CHKERRQ(ierr); 2572 } 2573 ierr = VecLockReadPop(x);CHKERRQ(ierr); 2574 } else { 2575 Vec w; 2576 ierr = VecDuplicate(x,&w);CHKERRQ(ierr); 2577 ierr = VecCopy(x,w);CHKERRQ(ierr); 2578 ierr = VecConjugate(w);CHKERRQ(ierr); 2579 ierr = MatMultTranspose(mat,w,y);CHKERRQ(ierr); 2580 ierr = VecDestroy(&w);CHKERRQ(ierr); 2581 ierr = VecConjugate(y);CHKERRQ(ierr); 2582 } 2583 ierr = PetscObjectStateIncrease((PetscObject)y);CHKERRQ(ierr); 2584 #else 2585 ierr = MatMultTranspose(mat,x,y);CHKERRQ(ierr); 2586 #endif 2587 ierr = PetscLogEventEnd(MAT_MultHermitianTranspose,mat,x,y,0);CHKERRQ(ierr); 2588 PetscFunctionReturn(0); 2589 } 2590 2591 /*@ 2592 MatMultAdd - Computes v3 = v2 + A * v1. 2593 2594 Neighbor-wise Collective on Mat 2595 2596 Input Parameters: 2597 + mat - the matrix 2598 - v1, v2 - the vectors 2599 2600 Output Parameters: 2601 . v3 - the result 2602 2603 Notes: 2604 The vectors v1 and v3 cannot be the same. I.e., one cannot 2605 call MatMultAdd(A,v1,v2,v1). 2606 2607 Level: beginner 2608 2609 .seealso: MatMultTranspose(), MatMult(), MatMultTransposeAdd() 2610 @*/ 2611 PetscErrorCode MatMultAdd(Mat mat,Vec v1,Vec v2,Vec v3) 2612 { 2613 PetscErrorCode ierr; 2614 2615 PetscFunctionBegin; 2616 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2617 PetscValidType(mat,1); 2618 PetscValidHeaderSpecific(v1,VEC_CLASSID,2); 2619 PetscValidHeaderSpecific(v2,VEC_CLASSID,3); 2620 PetscValidHeaderSpecific(v3,VEC_CLASSID,4); 2621 2622 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2623 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2624 PetscCheckFalse(mat->cmap->N != v1->map->N,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec v1: global dim %" PetscInt_FMT " %" PetscInt_FMT,mat->cmap->N,v1->map->N); 2625 /* PetscCheckFalse(mat->rmap->N != v2->map->N,PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec v2: global dim %" PetscInt_FMT " %" PetscInt_FMT,mat->rmap->N,v2->map->N); 2626 PetscCheckFalse(mat->rmap->N != v3->map->N,PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec v3: global dim %" PetscInt_FMT " %" PetscInt_FMT,mat->rmap->N,v3->map->N); */ 2627 PetscCheckFalse(mat->rmap->n != v3->map->n,PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec v3: local dim %" PetscInt_FMT " %" PetscInt_FMT,mat->rmap->n,v3->map->n); 2628 PetscCheckFalse(mat->rmap->n != v2->map->n,PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec v2: local dim %" PetscInt_FMT " %" PetscInt_FMT,mat->rmap->n,v2->map->n); 2629 PetscCheckFalse(v1 == v3,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"v1 and v3 must be different vectors"); 2630 MatCheckPreallocated(mat,1); 2631 2632 PetscCheckFalse(!mat->ops->multadd,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"No MatMultAdd() for matrix type %s",((PetscObject)mat)->type_name); 2633 ierr = PetscLogEventBegin(MAT_MultAdd,mat,v1,v2,v3);CHKERRQ(ierr); 2634 ierr = VecLockReadPush(v1);CHKERRQ(ierr); 2635 ierr = (*mat->ops->multadd)(mat,v1,v2,v3);CHKERRQ(ierr); 2636 ierr = VecLockReadPop(v1);CHKERRQ(ierr); 2637 ierr = PetscLogEventEnd(MAT_MultAdd,mat,v1,v2,v3);CHKERRQ(ierr); 2638 ierr = PetscObjectStateIncrease((PetscObject)v3);CHKERRQ(ierr); 2639 PetscFunctionReturn(0); 2640 } 2641 2642 /*@ 2643 MatMultTransposeAdd - Computes v3 = v2 + A' * v1. 2644 2645 Neighbor-wise Collective on Mat 2646 2647 Input Parameters: 2648 + mat - the matrix 2649 - v1, v2 - the vectors 2650 2651 Output Parameters: 2652 . v3 - the result 2653 2654 Notes: 2655 The vectors v1 and v3 cannot be the same. I.e., one cannot 2656 call MatMultTransposeAdd(A,v1,v2,v1). 2657 2658 Level: beginner 2659 2660 .seealso: MatMultTranspose(), MatMultAdd(), MatMult() 2661 @*/ 2662 PetscErrorCode MatMultTransposeAdd(Mat mat,Vec v1,Vec v2,Vec v3) 2663 { 2664 PetscErrorCode ierr; 2665 PetscErrorCode (*op)(Mat,Vec,Vec,Vec) = (!mat->ops->multtransposeadd && mat->symmetric) ? mat->ops->multadd : mat->ops->multtransposeadd; 2666 2667 PetscFunctionBegin; 2668 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2669 PetscValidType(mat,1); 2670 PetscValidHeaderSpecific(v1,VEC_CLASSID,2); 2671 PetscValidHeaderSpecific(v2,VEC_CLASSID,3); 2672 PetscValidHeaderSpecific(v3,VEC_CLASSID,4); 2673 2674 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2675 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2676 PetscCheckFalse(mat->rmap->N != v1->map->N,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec v1: global dim %" PetscInt_FMT " %" PetscInt_FMT,mat->rmap->N,v1->map->N); 2677 PetscCheckFalse(mat->cmap->N != v2->map->N,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec v2: global dim %" PetscInt_FMT " %" PetscInt_FMT,mat->cmap->N,v2->map->N); 2678 PetscCheckFalse(mat->cmap->N != v3->map->N,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec v3: global dim %" PetscInt_FMT " %" PetscInt_FMT,mat->cmap->N,v3->map->N); 2679 PetscCheckFalse(v1 == v3,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"v1 and v3 must be different vectors"); 2680 PetscCheckFalse(!op,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 2681 MatCheckPreallocated(mat,1); 2682 2683 ierr = PetscLogEventBegin(MAT_MultTransposeAdd,mat,v1,v2,v3);CHKERRQ(ierr); 2684 ierr = VecLockReadPush(v1);CHKERRQ(ierr); 2685 ierr = (*op)(mat,v1,v2,v3);CHKERRQ(ierr); 2686 ierr = VecLockReadPop(v1);CHKERRQ(ierr); 2687 ierr = PetscLogEventEnd(MAT_MultTransposeAdd,mat,v1,v2,v3);CHKERRQ(ierr); 2688 ierr = PetscObjectStateIncrease((PetscObject)v3);CHKERRQ(ierr); 2689 PetscFunctionReturn(0); 2690 } 2691 2692 /*@ 2693 MatMultHermitianTransposeAdd - Computes v3 = v2 + A^H * v1. 2694 2695 Neighbor-wise Collective on Mat 2696 2697 Input Parameters: 2698 + mat - the matrix 2699 - v1, v2 - the vectors 2700 2701 Output Parameters: 2702 . v3 - the result 2703 2704 Notes: 2705 The vectors v1 and v3 cannot be the same. I.e., one cannot 2706 call MatMultHermitianTransposeAdd(A,v1,v2,v1). 2707 2708 Level: beginner 2709 2710 .seealso: MatMultHermitianTranspose(), MatMultTranspose(), MatMultAdd(), MatMult() 2711 @*/ 2712 PetscErrorCode MatMultHermitianTransposeAdd(Mat mat,Vec v1,Vec v2,Vec v3) 2713 { 2714 PetscErrorCode ierr; 2715 2716 PetscFunctionBegin; 2717 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2718 PetscValidType(mat,1); 2719 PetscValidHeaderSpecific(v1,VEC_CLASSID,2); 2720 PetscValidHeaderSpecific(v2,VEC_CLASSID,3); 2721 PetscValidHeaderSpecific(v3,VEC_CLASSID,4); 2722 2723 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2724 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2725 PetscCheckFalse(v1 == v3,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"v1 and v3 must be different vectors"); 2726 PetscCheckFalse(mat->rmap->N != v1->map->N,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec v1: global dim %" PetscInt_FMT " %" PetscInt_FMT,mat->rmap->N,v1->map->N); 2727 PetscCheckFalse(mat->cmap->N != v2->map->N,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec v2: global dim %" PetscInt_FMT " %" PetscInt_FMT,mat->cmap->N,v2->map->N); 2728 PetscCheckFalse(mat->cmap->N != v3->map->N,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec v3: global dim %" PetscInt_FMT " %" PetscInt_FMT,mat->cmap->N,v3->map->N); 2729 MatCheckPreallocated(mat,1); 2730 2731 ierr = PetscLogEventBegin(MAT_MultHermitianTransposeAdd,mat,v1,v2,v3);CHKERRQ(ierr); 2732 ierr = VecLockReadPush(v1);CHKERRQ(ierr); 2733 if (mat->ops->multhermitiantransposeadd) { 2734 ierr = (*mat->ops->multhermitiantransposeadd)(mat,v1,v2,v3);CHKERRQ(ierr); 2735 } else { 2736 Vec w,z; 2737 ierr = VecDuplicate(v1,&w);CHKERRQ(ierr); 2738 ierr = VecCopy(v1,w);CHKERRQ(ierr); 2739 ierr = VecConjugate(w);CHKERRQ(ierr); 2740 ierr = VecDuplicate(v3,&z);CHKERRQ(ierr); 2741 ierr = MatMultTranspose(mat,w,z);CHKERRQ(ierr); 2742 ierr = VecDestroy(&w);CHKERRQ(ierr); 2743 ierr = VecConjugate(z);CHKERRQ(ierr); 2744 if (v2 != v3) { 2745 ierr = VecWAXPY(v3,1.0,v2,z);CHKERRQ(ierr); 2746 } else { 2747 ierr = VecAXPY(v3,1.0,z);CHKERRQ(ierr); 2748 } 2749 ierr = VecDestroy(&z);CHKERRQ(ierr); 2750 } 2751 ierr = VecLockReadPop(v1);CHKERRQ(ierr); 2752 ierr = PetscLogEventEnd(MAT_MultHermitianTransposeAdd,mat,v1,v2,v3);CHKERRQ(ierr); 2753 ierr = PetscObjectStateIncrease((PetscObject)v3);CHKERRQ(ierr); 2754 PetscFunctionReturn(0); 2755 } 2756 2757 /*@ 2758 MatMultConstrained - The inner multiplication routine for a 2759 constrained matrix P^T A P. 2760 2761 Neighbor-wise Collective on Mat 2762 2763 Input Parameters: 2764 + mat - the matrix 2765 - x - the vector to be multilplied 2766 2767 Output Parameters: 2768 . y - the result 2769 2770 Notes: 2771 The vectors x and y cannot be the same. I.e., one cannot 2772 call MatMult(A,y,y). 2773 2774 Level: beginner 2775 2776 .seealso: MatMult(), MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd() 2777 @*/ 2778 PetscErrorCode MatMultConstrained(Mat mat,Vec x,Vec y) 2779 { 2780 PetscErrorCode ierr; 2781 2782 PetscFunctionBegin; 2783 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2784 PetscValidHeaderSpecific(x,VEC_CLASSID,2); 2785 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 2786 PetscCheckFalse(!mat->assembled,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2787 PetscCheckFalse(mat->factortype,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2788 PetscCheckFalse(x == y,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors"); 2789 PetscCheckFalse(mat->cmap->N != x->map->N,PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %" PetscInt_FMT " %" PetscInt_FMT,mat->cmap->N,x->map->N); 2790 PetscCheckFalse(mat->rmap->N != y->map->N,PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %" PetscInt_FMT " %" PetscInt_FMT,mat->rmap->N,y->map->N); 2791 PetscCheckFalse(mat->rmap->n != y->map->n,PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: local dim %" PetscInt_FMT " %" PetscInt_FMT,mat->rmap->n,y->map->n); 2792 2793 ierr = PetscLogEventBegin(MAT_MultConstrained,mat,x,y,0);CHKERRQ(ierr); 2794 ierr = VecLockReadPush(x);CHKERRQ(ierr); 2795 ierr = (*mat->ops->multconstrained)(mat,x,y);CHKERRQ(ierr); 2796 ierr = VecLockReadPop(x);CHKERRQ(ierr); 2797 ierr = PetscLogEventEnd(MAT_MultConstrained,mat,x,y,0);CHKERRQ(ierr); 2798 ierr = PetscObjectStateIncrease((PetscObject)y);CHKERRQ(ierr); 2799 PetscFunctionReturn(0); 2800 } 2801 2802 /*@ 2803 MatMultTransposeConstrained - The inner multiplication routine for a 2804 constrained matrix P^T A^T P. 2805 2806 Neighbor-wise Collective on Mat 2807 2808 Input Parameters: 2809 + mat - the matrix 2810 - x - the vector to be multilplied 2811 2812 Output Parameters: 2813 . y - the result 2814 2815 Notes: 2816 The vectors x and y cannot be the same. I.e., one cannot 2817 call MatMult(A,y,y). 2818 2819 Level: beginner 2820 2821 .seealso: MatMult(), MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd() 2822 @*/ 2823 PetscErrorCode MatMultTransposeConstrained(Mat mat,Vec x,Vec y) 2824 { 2825 PetscErrorCode ierr; 2826 2827 PetscFunctionBegin; 2828 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2829 PetscValidHeaderSpecific(x,VEC_CLASSID,2); 2830 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 2831 PetscCheckFalse(!mat->assembled,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2832 PetscCheckFalse(mat->factortype,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2833 PetscCheckFalse(x == y,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors"); 2834 PetscCheckFalse(mat->rmap->N != x->map->N,PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %" PetscInt_FMT " %" PetscInt_FMT,mat->cmap->N,x->map->N); 2835 PetscCheckFalse(mat->cmap->N != y->map->N,PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %" PetscInt_FMT " %" PetscInt_FMT,mat->rmap->N,y->map->N); 2836 2837 ierr = PetscLogEventBegin(MAT_MultConstrained,mat,x,y,0);CHKERRQ(ierr); 2838 ierr = (*mat->ops->multtransposeconstrained)(mat,x,y);CHKERRQ(ierr); 2839 ierr = PetscLogEventEnd(MAT_MultConstrained,mat,x,y,0);CHKERRQ(ierr); 2840 ierr = PetscObjectStateIncrease((PetscObject)y);CHKERRQ(ierr); 2841 PetscFunctionReturn(0); 2842 } 2843 2844 /*@C 2845 MatGetFactorType - gets the type of factorization it is 2846 2847 Not Collective 2848 2849 Input Parameters: 2850 . mat - the matrix 2851 2852 Output Parameters: 2853 . t - the type, one of MAT_FACTOR_NONE, MAT_FACTOR_LU, MAT_FACTOR_CHOLESKY, MAT_FACTOR_ILU, MAT_FACTOR_ICC,MAT_FACTOR_ILUDT 2854 2855 Level: intermediate 2856 2857 .seealso: MatFactorType, MatGetFactor(), MatSetFactorType() 2858 @*/ 2859 PetscErrorCode MatGetFactorType(Mat mat,MatFactorType *t) 2860 { 2861 PetscFunctionBegin; 2862 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2863 PetscValidType(mat,1); 2864 PetscValidPointer(t,2); 2865 *t = mat->factortype; 2866 PetscFunctionReturn(0); 2867 } 2868 2869 /*@C 2870 MatSetFactorType - sets the type of factorization it is 2871 2872 Logically Collective on Mat 2873 2874 Input Parameters: 2875 + mat - the matrix 2876 - t - the type, one of MAT_FACTOR_NONE, MAT_FACTOR_LU, MAT_FACTOR_CHOLESKY, MAT_FACTOR_ILU, MAT_FACTOR_ICC,MAT_FACTOR_ILUDT 2877 2878 Level: intermediate 2879 2880 .seealso: MatFactorType, MatGetFactor(), MatGetFactorType() 2881 @*/ 2882 PetscErrorCode MatSetFactorType(Mat mat, MatFactorType t) 2883 { 2884 PetscFunctionBegin; 2885 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2886 PetscValidType(mat,1); 2887 mat->factortype = t; 2888 PetscFunctionReturn(0); 2889 } 2890 2891 /* ------------------------------------------------------------*/ 2892 /*@C 2893 MatGetInfo - Returns information about matrix storage (number of 2894 nonzeros, memory, etc.). 2895 2896 Collective on Mat if MAT_GLOBAL_MAX or MAT_GLOBAL_SUM is used as the flag 2897 2898 Input Parameter: 2899 . mat - the matrix 2900 2901 Output Parameters: 2902 + flag - flag indicating the type of parameters to be returned 2903 (MAT_LOCAL - local matrix, MAT_GLOBAL_MAX - maximum over all processors, 2904 MAT_GLOBAL_SUM - sum over all processors) 2905 - info - matrix information context 2906 2907 Notes: 2908 The MatInfo context contains a variety of matrix data, including 2909 number of nonzeros allocated and used, number of mallocs during 2910 matrix assembly, etc. Additional information for factored matrices 2911 is provided (such as the fill ratio, number of mallocs during 2912 factorization, etc.). Much of this info is printed to PETSC_STDOUT 2913 when using the runtime options 2914 $ -info -mat_view ::ascii_info 2915 2916 Example for C/C++ Users: 2917 See the file ${PETSC_DIR}/include/petscmat.h for a complete list of 2918 data within the MatInfo context. For example, 2919 .vb 2920 MatInfo info; 2921 Mat A; 2922 double mal, nz_a, nz_u; 2923 2924 MatGetInfo(A,MAT_LOCAL,&info); 2925 mal = info.mallocs; 2926 nz_a = info.nz_allocated; 2927 .ve 2928 2929 Example for Fortran Users: 2930 Fortran users should declare info as a double precision 2931 array of dimension MAT_INFO_SIZE, and then extract the parameters 2932 of interest. See the file ${PETSC_DIR}/include/petsc/finclude/petscmat.h 2933 a complete list of parameter names. 2934 .vb 2935 double precision info(MAT_INFO_SIZE) 2936 double precision mal, nz_a 2937 Mat A 2938 integer ierr 2939 2940 call MatGetInfo(A,MAT_LOCAL,info,ierr) 2941 mal = info(MAT_INFO_MALLOCS) 2942 nz_a = info(MAT_INFO_NZ_ALLOCATED) 2943 .ve 2944 2945 Level: intermediate 2946 2947 Developer Note: fortran interface is not autogenerated as the f90 2948 interface definition cannot be generated correctly [due to MatInfo] 2949 2950 .seealso: MatStashGetInfo() 2951 2952 @*/ 2953 PetscErrorCode MatGetInfo(Mat mat,MatInfoType flag,MatInfo *info) 2954 { 2955 PetscErrorCode ierr; 2956 2957 PetscFunctionBegin; 2958 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2959 PetscValidType(mat,1); 2960 PetscValidPointer(info,3); 2961 PetscCheckFalse(!mat->ops->getinfo,PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 2962 MatCheckPreallocated(mat,1); 2963 ierr = (*mat->ops->getinfo)(mat,flag,info);CHKERRQ(ierr); 2964 PetscFunctionReturn(0); 2965 } 2966 2967 /* 2968 This is used by external packages where it is not easy to get the info from the actual 2969 matrix factorization. 2970 */ 2971 PetscErrorCode MatGetInfo_External(Mat A,MatInfoType flag,MatInfo *info) 2972 { 2973 PetscErrorCode ierr; 2974 2975 PetscFunctionBegin; 2976 ierr = PetscMemzero(info,sizeof(MatInfo));CHKERRQ(ierr); 2977 PetscFunctionReturn(0); 2978 } 2979 2980 /* ----------------------------------------------------------*/ 2981 2982 /*@C 2983 MatLUFactor - Performs in-place LU factorization of matrix. 2984 2985 Collective on Mat 2986 2987 Input Parameters: 2988 + mat - the matrix 2989 . row - row permutation 2990 . col - column permutation 2991 - info - options for factorization, includes 2992 $ fill - expected fill as ratio of original fill. 2993 $ dtcol - pivot tolerance (0 no pivot, 1 full column pivoting) 2994 $ Run with the option -info to determine an optimal value to use 2995 2996 Notes: 2997 Most users should employ the simplified KSP interface for linear solvers 2998 instead of working directly with matrix algebra routines such as this. 2999 See, e.g., KSPCreate(). 3000 3001 This changes the state of the matrix to a factored matrix; it cannot be used 3002 for example with MatSetValues() unless one first calls MatSetUnfactored(). 3003 3004 Level: developer 3005 3006 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor(), 3007 MatGetOrdering(), MatSetUnfactored(), MatFactorInfo, MatGetFactor() 3008 3009 Developer Note: fortran interface is not autogenerated as the f90 3010 interface definition cannot be generated correctly [due to MatFactorInfo] 3011 3012 @*/ 3013 PetscErrorCode MatLUFactor(Mat mat,IS row,IS col,const MatFactorInfo *info) 3014 { 3015 PetscErrorCode ierr; 3016 MatFactorInfo tinfo; 3017 3018 PetscFunctionBegin; 3019 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3020 if (row) PetscValidHeaderSpecific(row,IS_CLASSID,2); 3021 if (col) PetscValidHeaderSpecific(col,IS_CLASSID,3); 3022 if (info) PetscValidPointer(info,4); 3023 PetscValidType(mat,1); 3024 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3025 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3026 PetscCheckFalse(!mat->ops->lufactor,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 3027 MatCheckPreallocated(mat,1); 3028 if (!info) { 3029 ierr = MatFactorInfoInitialize(&tinfo);CHKERRQ(ierr); 3030 info = &tinfo; 3031 } 3032 3033 ierr = PetscLogEventBegin(MAT_LUFactor,mat,row,col,0);CHKERRQ(ierr); 3034 ierr = (*mat->ops->lufactor)(mat,row,col,info);CHKERRQ(ierr); 3035 ierr = PetscLogEventEnd(MAT_LUFactor,mat,row,col,0);CHKERRQ(ierr); 3036 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 3037 PetscFunctionReturn(0); 3038 } 3039 3040 /*@C 3041 MatILUFactor - Performs in-place ILU factorization of matrix. 3042 3043 Collective on Mat 3044 3045 Input Parameters: 3046 + mat - the matrix 3047 . row - row permutation 3048 . col - column permutation 3049 - info - structure containing 3050 $ levels - number of levels of fill. 3051 $ expected fill - as ratio of original fill. 3052 $ 1 or 0 - indicating force fill on diagonal (improves robustness for matrices 3053 missing diagonal entries) 3054 3055 Notes: 3056 Probably really in-place only when level of fill is zero, otherwise allocates 3057 new space to store factored matrix and deletes previous memory. 3058 3059 Most users should employ the simplified KSP interface for linear solvers 3060 instead of working directly with matrix algebra routines such as this. 3061 See, e.g., KSPCreate(). 3062 3063 Level: developer 3064 3065 .seealso: MatILUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor(), MatFactorInfo 3066 3067 Developer Note: fortran interface is not autogenerated as the f90 3068 interface definition cannot be generated correctly [due to MatFactorInfo] 3069 3070 @*/ 3071 PetscErrorCode MatILUFactor(Mat mat,IS row,IS col,const MatFactorInfo *info) 3072 { 3073 PetscErrorCode ierr; 3074 3075 PetscFunctionBegin; 3076 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3077 if (row) PetscValidHeaderSpecific(row,IS_CLASSID,2); 3078 if (col) PetscValidHeaderSpecific(col,IS_CLASSID,3); 3079 PetscValidPointer(info,4); 3080 PetscValidType(mat,1); 3081 PetscCheckFalse(mat->rmap->N != mat->cmap->N,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONG,"matrix must be square"); 3082 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3083 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3084 PetscCheckFalse(!mat->ops->ilufactor,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 3085 MatCheckPreallocated(mat,1); 3086 3087 ierr = PetscLogEventBegin(MAT_ILUFactor,mat,row,col,0);CHKERRQ(ierr); 3088 ierr = (*mat->ops->ilufactor)(mat,row,col,info);CHKERRQ(ierr); 3089 ierr = PetscLogEventEnd(MAT_ILUFactor,mat,row,col,0);CHKERRQ(ierr); 3090 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 3091 PetscFunctionReturn(0); 3092 } 3093 3094 /*@C 3095 MatLUFactorSymbolic - Performs symbolic LU factorization of matrix. 3096 Call this routine before calling MatLUFactorNumeric(). 3097 3098 Collective on Mat 3099 3100 Input Parameters: 3101 + fact - the factor matrix obtained with MatGetFactor() 3102 . mat - the matrix 3103 . row, col - row and column permutations 3104 - info - options for factorization, includes 3105 $ fill - expected fill as ratio of original fill. 3106 $ dtcol - pivot tolerance (0 no pivot, 1 full column pivoting) 3107 $ Run with the option -info to determine an optimal value to use 3108 3109 Notes: 3110 See Users-Manual: ch_mat for additional information about choosing the fill factor for better efficiency. 3111 3112 Most users should employ the simplified KSP interface for linear solvers 3113 instead of working directly with matrix algebra routines such as this. 3114 See, e.g., KSPCreate(). 3115 3116 Level: developer 3117 3118 .seealso: MatLUFactor(), MatLUFactorNumeric(), MatCholeskyFactor(), MatFactorInfo, MatFactorInfoInitialize() 3119 3120 Developer Note: fortran interface is not autogenerated as the f90 3121 interface definition cannot be generated correctly [due to MatFactorInfo] 3122 3123 @*/ 3124 PetscErrorCode MatLUFactorSymbolic(Mat fact,Mat mat,IS row,IS col,const MatFactorInfo *info) 3125 { 3126 PetscErrorCode ierr; 3127 MatFactorInfo tinfo; 3128 3129 PetscFunctionBegin; 3130 PetscValidHeaderSpecific(mat,MAT_CLASSID,2); 3131 if (row) PetscValidHeaderSpecific(row,IS_CLASSID,3); 3132 if (col) PetscValidHeaderSpecific(col,IS_CLASSID,4); 3133 if (info) PetscValidPointer(info,5); 3134 PetscValidType(mat,2); 3135 PetscValidPointer(fact,1); 3136 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3137 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3138 if (!(fact)->ops->lufactorsymbolic) { 3139 MatSolverType stype; 3140 ierr = MatFactorGetSolverType(fact,&stype);CHKERRQ(ierr); 3141 SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s symbolic LU using solver package %s",((PetscObject)mat)->type_name,stype); 3142 } 3143 MatCheckPreallocated(mat,2); 3144 if (!info) { 3145 ierr = MatFactorInfoInitialize(&tinfo);CHKERRQ(ierr); 3146 info = &tinfo; 3147 } 3148 3149 if (!fact->trivialsymbolic) {ierr = PetscLogEventBegin(MAT_LUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr);} 3150 ierr = (fact->ops->lufactorsymbolic)(fact,mat,row,col,info);CHKERRQ(ierr); 3151 if (!fact->trivialsymbolic) {ierr = PetscLogEventEnd(MAT_LUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr);} 3152 ierr = PetscObjectStateIncrease((PetscObject)fact);CHKERRQ(ierr); 3153 PetscFunctionReturn(0); 3154 } 3155 3156 /*@C 3157 MatLUFactorNumeric - Performs numeric LU factorization of a matrix. 3158 Call this routine after first calling MatLUFactorSymbolic(). 3159 3160 Collective on Mat 3161 3162 Input Parameters: 3163 + fact - the factor matrix obtained with MatGetFactor() 3164 . mat - the matrix 3165 - info - options for factorization 3166 3167 Notes: 3168 See MatLUFactor() for in-place factorization. See 3169 MatCholeskyFactorNumeric() for the symmetric, positive definite case. 3170 3171 Most users should employ the simplified KSP interface for linear solvers 3172 instead of working directly with matrix algebra routines such as this. 3173 See, e.g., KSPCreate(). 3174 3175 Level: developer 3176 3177 .seealso: MatLUFactorSymbolic(), MatLUFactor(), MatCholeskyFactor() 3178 3179 Developer Note: fortran interface is not autogenerated as the f90 3180 interface definition cannot be generated correctly [due to MatFactorInfo] 3181 3182 @*/ 3183 PetscErrorCode MatLUFactorNumeric(Mat fact,Mat mat,const MatFactorInfo *info) 3184 { 3185 MatFactorInfo tinfo; 3186 PetscErrorCode ierr; 3187 3188 PetscFunctionBegin; 3189 PetscValidHeaderSpecific(mat,MAT_CLASSID,2); 3190 PetscValidType(mat,2); 3191 PetscValidPointer(fact,1); 3192 PetscValidHeaderSpecific(fact,MAT_CLASSID,1); 3193 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3194 PetscCheckFalse(mat->rmap->N != (fact)->rmap->N || mat->cmap->N != (fact)->cmap->N,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Mat fact: global dimensions are different %" PetscInt_FMT " should = %" PetscInt_FMT " %" PetscInt_FMT " should = %" PetscInt_FMT,mat->rmap->N,(fact)->rmap->N,mat->cmap->N,(fact)->cmap->N); 3195 3196 PetscCheckFalse(!(fact)->ops->lufactornumeric,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s numeric LU",((PetscObject)mat)->type_name); 3197 MatCheckPreallocated(mat,2); 3198 if (!info) { 3199 ierr = MatFactorInfoInitialize(&tinfo);CHKERRQ(ierr); 3200 info = &tinfo; 3201 } 3202 3203 if (!fact->trivialsymbolic) {ierr = PetscLogEventBegin(MAT_LUFactorNumeric,mat,fact,0,0);CHKERRQ(ierr);} 3204 else {ierr = PetscLogEventBegin(MAT_LUFactor,mat,fact,0,0);CHKERRQ(ierr);} 3205 ierr = (fact->ops->lufactornumeric)(fact,mat,info);CHKERRQ(ierr); 3206 if (!fact->trivialsymbolic) {ierr = PetscLogEventEnd(MAT_LUFactorNumeric,mat,fact,0,0);CHKERRQ(ierr);} 3207 else {ierr = PetscLogEventEnd(MAT_LUFactor,mat,fact,0,0);CHKERRQ(ierr);} 3208 ierr = MatViewFromOptions(fact,NULL,"-mat_factor_view");CHKERRQ(ierr); 3209 ierr = PetscObjectStateIncrease((PetscObject)fact);CHKERRQ(ierr); 3210 PetscFunctionReturn(0); 3211 } 3212 3213 /*@C 3214 MatCholeskyFactor - Performs in-place Cholesky factorization of a 3215 symmetric matrix. 3216 3217 Collective on Mat 3218 3219 Input Parameters: 3220 + mat - the matrix 3221 . perm - row and column permutations 3222 - f - expected fill as ratio of original fill 3223 3224 Notes: 3225 See MatLUFactor() for the nonsymmetric case. See also 3226 MatCholeskyFactorSymbolic(), and MatCholeskyFactorNumeric(). 3227 3228 Most users should employ the simplified KSP interface for linear solvers 3229 instead of working directly with matrix algebra routines such as this. 3230 See, e.g., KSPCreate(). 3231 3232 Level: developer 3233 3234 .seealso: MatLUFactor(), MatCholeskyFactorSymbolic(), MatCholeskyFactorNumeric() 3235 MatGetOrdering() 3236 3237 Developer Note: fortran interface is not autogenerated as the f90 3238 interface definition cannot be generated correctly [due to MatFactorInfo] 3239 3240 @*/ 3241 PetscErrorCode MatCholeskyFactor(Mat mat,IS perm,const MatFactorInfo *info) 3242 { 3243 PetscErrorCode ierr; 3244 MatFactorInfo tinfo; 3245 3246 PetscFunctionBegin; 3247 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3248 PetscValidType(mat,1); 3249 if (perm) PetscValidHeaderSpecific(perm,IS_CLASSID,2); 3250 if (info) PetscValidPointer(info,3); 3251 PetscCheckFalse(mat->rmap->N != mat->cmap->N,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONG,"Matrix must be square"); 3252 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3253 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3254 PetscCheckFalse(!mat->ops->choleskyfactor,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); 3255 MatCheckPreallocated(mat,1); 3256 if (!info) { 3257 ierr = MatFactorInfoInitialize(&tinfo);CHKERRQ(ierr); 3258 info = &tinfo; 3259 } 3260 3261 ierr = PetscLogEventBegin(MAT_CholeskyFactor,mat,perm,0,0);CHKERRQ(ierr); 3262 ierr = (*mat->ops->choleskyfactor)(mat,perm,info);CHKERRQ(ierr); 3263 ierr = PetscLogEventEnd(MAT_CholeskyFactor,mat,perm,0,0);CHKERRQ(ierr); 3264 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 3265 PetscFunctionReturn(0); 3266 } 3267 3268 /*@C 3269 MatCholeskyFactorSymbolic - Performs symbolic Cholesky factorization 3270 of a symmetric matrix. 3271 3272 Collective on Mat 3273 3274 Input Parameters: 3275 + fact - the factor matrix obtained with MatGetFactor() 3276 . mat - the matrix 3277 . perm - row and column permutations 3278 - info - options for factorization, includes 3279 $ fill - expected fill as ratio of original fill. 3280 $ dtcol - pivot tolerance (0 no pivot, 1 full column pivoting) 3281 $ Run with the option -info to determine an optimal value to use 3282 3283 Notes: 3284 See MatLUFactorSymbolic() for the nonsymmetric case. See also 3285 MatCholeskyFactor() and MatCholeskyFactorNumeric(). 3286 3287 Most users should employ the simplified KSP interface for linear solvers 3288 instead of working directly with matrix algebra routines such as this. 3289 See, e.g., KSPCreate(). 3290 3291 Level: developer 3292 3293 .seealso: MatLUFactorSymbolic(), MatCholeskyFactor(), MatCholeskyFactorNumeric() 3294 MatGetOrdering() 3295 3296 Developer Note: fortran interface is not autogenerated as the f90 3297 interface definition cannot be generated correctly [due to MatFactorInfo] 3298 3299 @*/ 3300 PetscErrorCode MatCholeskyFactorSymbolic(Mat fact,Mat mat,IS perm,const MatFactorInfo *info) 3301 { 3302 PetscErrorCode ierr; 3303 MatFactorInfo tinfo; 3304 3305 PetscFunctionBegin; 3306 PetscValidHeaderSpecific(mat,MAT_CLASSID,2); 3307 PetscValidType(mat,2); 3308 if (perm) PetscValidHeaderSpecific(perm,IS_CLASSID,3); 3309 if (info) PetscValidPointer(info,4); 3310 PetscValidPointer(fact,1); 3311 PetscCheckFalse(mat->rmap->N != mat->cmap->N,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONG,"Matrix must be square"); 3312 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3313 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3314 if (!(fact)->ops->choleskyfactorsymbolic) { 3315 MatSolverType stype; 3316 ierr = MatFactorGetSolverType(fact,&stype);CHKERRQ(ierr); 3317 SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s symbolic factor Cholesky using solver package %s",((PetscObject)mat)->type_name,stype); 3318 } 3319 MatCheckPreallocated(mat,2); 3320 if (!info) { 3321 ierr = MatFactorInfoInitialize(&tinfo);CHKERRQ(ierr); 3322 info = &tinfo; 3323 } 3324 3325 if (!fact->trivialsymbolic) {ierr = PetscLogEventBegin(MAT_CholeskyFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr);} 3326 ierr = (fact->ops->choleskyfactorsymbolic)(fact,mat,perm,info);CHKERRQ(ierr); 3327 if (!fact->trivialsymbolic) {ierr = PetscLogEventEnd(MAT_CholeskyFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr);} 3328 ierr = PetscObjectStateIncrease((PetscObject)fact);CHKERRQ(ierr); 3329 PetscFunctionReturn(0); 3330 } 3331 3332 /*@C 3333 MatCholeskyFactorNumeric - Performs numeric Cholesky factorization 3334 of a symmetric matrix. Call this routine after first calling 3335 MatCholeskyFactorSymbolic(). 3336 3337 Collective on Mat 3338 3339 Input Parameters: 3340 + fact - the factor matrix obtained with MatGetFactor() 3341 . mat - the initial matrix 3342 . info - options for factorization 3343 - fact - the symbolic factor of mat 3344 3345 Notes: 3346 Most users should employ the simplified KSP interface for linear solvers 3347 instead of working directly with matrix algebra routines such as this. 3348 See, e.g., KSPCreate(). 3349 3350 Level: developer 3351 3352 .seealso: MatCholeskyFactorSymbolic(), MatCholeskyFactor(), MatLUFactorNumeric() 3353 3354 Developer Note: fortran interface is not autogenerated as the f90 3355 interface definition cannot be generated correctly [due to MatFactorInfo] 3356 3357 @*/ 3358 PetscErrorCode MatCholeskyFactorNumeric(Mat fact,Mat mat,const MatFactorInfo *info) 3359 { 3360 MatFactorInfo tinfo; 3361 PetscErrorCode ierr; 3362 3363 PetscFunctionBegin; 3364 PetscValidHeaderSpecific(mat,MAT_CLASSID,2); 3365 PetscValidType(mat,2); 3366 PetscValidPointer(fact,1); 3367 PetscValidHeaderSpecific(fact,MAT_CLASSID,1); 3368 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3369 PetscCheckFalse(!(fact)->ops->choleskyfactornumeric,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s numeric factor Cholesky",((PetscObject)mat)->type_name); 3370 PetscCheckFalse(mat->rmap->N != (fact)->rmap->N || mat->cmap->N != (fact)->cmap->N,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Mat fact: global dim %" PetscInt_FMT " should = %" PetscInt_FMT " %" PetscInt_FMT " should = %" PetscInt_FMT,mat->rmap->N,(fact)->rmap->N,mat->cmap->N,(fact)->cmap->N); 3371 MatCheckPreallocated(mat,2); 3372 if (!info) { 3373 ierr = MatFactorInfoInitialize(&tinfo);CHKERRQ(ierr); 3374 info = &tinfo; 3375 } 3376 3377 if (!fact->trivialsymbolic) {ierr = PetscLogEventBegin(MAT_CholeskyFactorNumeric,mat,fact,0,0);CHKERRQ(ierr);} 3378 else {ierr = PetscLogEventBegin(MAT_CholeskyFactor,mat,fact,0,0);CHKERRQ(ierr);} 3379 ierr = (fact->ops->choleskyfactornumeric)(fact,mat,info);CHKERRQ(ierr); 3380 if (!fact->trivialsymbolic) {ierr = PetscLogEventEnd(MAT_CholeskyFactorNumeric,mat,fact,0,0);CHKERRQ(ierr);} 3381 else {ierr = PetscLogEventEnd(MAT_CholeskyFactor,mat,fact,0,0);CHKERRQ(ierr);} 3382 ierr = MatViewFromOptions(fact,NULL,"-mat_factor_view");CHKERRQ(ierr); 3383 ierr = PetscObjectStateIncrease((PetscObject)fact);CHKERRQ(ierr); 3384 PetscFunctionReturn(0); 3385 } 3386 3387 /*@ 3388 MatQRFactor - Performs in-place QR factorization of matrix. 3389 3390 Collective on Mat 3391 3392 Input Parameters: 3393 + mat - the matrix 3394 . col - column permutation 3395 - info - options for factorization, includes 3396 $ fill - expected fill as ratio of original fill. 3397 $ dtcol - pivot tolerance (0 no pivot, 1 full column pivoting) 3398 $ Run with the option -info to determine an optimal value to use 3399 3400 Notes: 3401 Most users should employ the simplified KSP interface for linear solvers 3402 instead of working directly with matrix algebra routines such as this. 3403 See, e.g., KSPCreate(). 3404 3405 This changes the state of the matrix to a factored matrix; it cannot be used 3406 for example with MatSetValues() unless one first calls MatSetUnfactored(). 3407 3408 Level: developer 3409 3410 .seealso: MatQRFactorSymbolic(), MatQRFactorNumeric(), MatLUFactor(), 3411 MatSetUnfactored(), MatFactorInfo, MatGetFactor() 3412 3413 Developer Note: fortran interface is not autogenerated as the f90 3414 interface definition cannot be generated correctly [due to MatFactorInfo] 3415 3416 @*/ 3417 PetscErrorCode MatQRFactor(Mat mat, IS col, const MatFactorInfo *info) 3418 { 3419 PetscErrorCode ierr; 3420 3421 PetscFunctionBegin; 3422 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3423 if (col) PetscValidHeaderSpecific(col,IS_CLASSID,2); 3424 if (info) PetscValidPointer(info,3); 3425 PetscValidType(mat,1); 3426 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3427 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3428 MatCheckPreallocated(mat,1); 3429 ierr = PetscLogEventBegin(MAT_QRFactor,mat,col,0,0);CHKERRQ(ierr); 3430 ierr = PetscUseMethod(mat,"MatQRFactor_C", (Mat,IS,const MatFactorInfo*), (mat, col, info));CHKERRQ(ierr); 3431 ierr = PetscLogEventEnd(MAT_QRFactor,mat,col,0,0);CHKERRQ(ierr); 3432 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 3433 PetscFunctionReturn(0); 3434 } 3435 3436 /*@ 3437 MatQRFactorSymbolic - Performs symbolic QR factorization of matrix. 3438 Call this routine before calling MatQRFactorNumeric(). 3439 3440 Collective on Mat 3441 3442 Input Parameters: 3443 + fact - the factor matrix obtained with MatGetFactor() 3444 . mat - the matrix 3445 . col - column permutation 3446 - info - options for factorization, includes 3447 $ fill - expected fill as ratio of original fill. 3448 $ dtcol - pivot tolerance (0 no pivot, 1 full column pivoting) 3449 $ Run with the option -info to determine an optimal value to use 3450 3451 Most users should employ the simplified KSP interface for linear solvers 3452 instead of working directly with matrix algebra routines such as this. 3453 See, e.g., KSPCreate(). 3454 3455 Level: developer 3456 3457 .seealso: MatQRFactor(), MatQRFactorNumeric(), MatLUFactor(), MatFactorInfo, MatFactorInfoInitialize() 3458 3459 Developer Note: fortran interface is not autogenerated as the f90 3460 interface definition cannot be generated correctly [due to MatFactorInfo] 3461 3462 @*/ 3463 PetscErrorCode MatQRFactorSymbolic(Mat fact,Mat mat,IS col,const MatFactorInfo *info) 3464 { 3465 PetscErrorCode ierr; 3466 MatFactorInfo tinfo; 3467 3468 PetscFunctionBegin; 3469 PetscValidHeaderSpecific(mat,MAT_CLASSID,2); 3470 if (col) PetscValidHeaderSpecific(col,IS_CLASSID,3); 3471 if (info) PetscValidPointer(info,4); 3472 PetscValidType(mat,2); 3473 PetscValidPointer(fact,1); 3474 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3475 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3476 MatCheckPreallocated(mat,2); 3477 if (!info) { 3478 ierr = MatFactorInfoInitialize(&tinfo);CHKERRQ(ierr); 3479 info = &tinfo; 3480 } 3481 3482 if (!fact->trivialsymbolic) {ierr = PetscLogEventBegin(MAT_QRFactorSymbolic,fact,mat,col,0);CHKERRQ(ierr);} 3483 ierr = PetscUseMethod(fact,"MatQRFactorSymbolic_C", (Mat,Mat,IS,const MatFactorInfo*), (fact, mat, col, info));CHKERRQ(ierr); 3484 if (!fact->trivialsymbolic) {ierr = PetscLogEventEnd(MAT_QRFactorSymbolic,fact,mat,col,0);CHKERRQ(ierr);} 3485 ierr = PetscObjectStateIncrease((PetscObject)fact);CHKERRQ(ierr); 3486 PetscFunctionReturn(0); 3487 } 3488 3489 /*@ 3490 MatQRFactorNumeric - Performs numeric QR factorization of a matrix. 3491 Call this routine after first calling MatQRFactorSymbolic(). 3492 3493 Collective on Mat 3494 3495 Input Parameters: 3496 + fact - the factor matrix obtained with MatGetFactor() 3497 . mat - the matrix 3498 - info - options for factorization 3499 3500 Notes: 3501 See MatQRFactor() for in-place factorization. 3502 3503 Most users should employ the simplified KSP interface for linear solvers 3504 instead of working directly with matrix algebra routines such as this. 3505 See, e.g., KSPCreate(). 3506 3507 Level: developer 3508 3509 .seealso: MatQRFactorSymbolic(), MatLUFactor() 3510 3511 Developer Note: fortran interface is not autogenerated as the f90 3512 interface definition cannot be generated correctly [due to MatFactorInfo] 3513 3514 @*/ 3515 PetscErrorCode MatQRFactorNumeric(Mat fact,Mat mat,const MatFactorInfo *info) 3516 { 3517 MatFactorInfo tinfo; 3518 PetscErrorCode ierr; 3519 3520 PetscFunctionBegin; 3521 PetscValidHeaderSpecific(mat,MAT_CLASSID,2); 3522 PetscValidType(mat,2); 3523 PetscValidPointer(fact,1); 3524 PetscValidHeaderSpecific(fact,MAT_CLASSID,1); 3525 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3526 PetscCheckFalse(mat->rmap->N != (fact)->rmap->N || mat->cmap->N != (fact)->cmap->N,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Mat fact: global dimensions are different %" PetscInt_FMT " should = %" PetscInt_FMT " %" PetscInt_FMT " should = %" PetscInt_FMT,mat->rmap->N,(fact)->rmap->N,mat->cmap->N,(fact)->cmap->N); 3527 3528 MatCheckPreallocated(mat,2); 3529 if (!info) { 3530 ierr = MatFactorInfoInitialize(&tinfo);CHKERRQ(ierr); 3531 info = &tinfo; 3532 } 3533 3534 if (!fact->trivialsymbolic) {ierr = PetscLogEventBegin(MAT_QRFactorNumeric,mat,fact,0,0);CHKERRQ(ierr);} 3535 else {ierr = PetscLogEventBegin(MAT_QRFactor,mat,fact,0,0);CHKERRQ(ierr);} 3536 ierr = PetscUseMethod(fact,"MatQRFactorNumeric_C", (Mat,Mat,const MatFactorInfo*), (fact, mat, info));CHKERRQ(ierr); 3537 if (!fact->trivialsymbolic) {ierr = PetscLogEventEnd(MAT_QRFactorNumeric,mat,fact,0,0);CHKERRQ(ierr);} 3538 else {ierr = PetscLogEventEnd(MAT_QRFactor,mat,fact,0,0);CHKERRQ(ierr);} 3539 ierr = MatViewFromOptions(fact,NULL,"-mat_factor_view");CHKERRQ(ierr); 3540 ierr = PetscObjectStateIncrease((PetscObject)fact);CHKERRQ(ierr); 3541 PetscFunctionReturn(0); 3542 } 3543 3544 /* ----------------------------------------------------------------*/ 3545 /*@ 3546 MatSolve - Solves A x = b, given a factored matrix. 3547 3548 Neighbor-wise Collective on Mat 3549 3550 Input Parameters: 3551 + mat - the factored matrix 3552 - b - the right-hand-side vector 3553 3554 Output Parameter: 3555 . x - the result vector 3556 3557 Notes: 3558 The vectors b and x cannot be the same. I.e., one cannot 3559 call MatSolve(A,x,x). 3560 3561 Notes: 3562 Most users should employ the simplified KSP interface for linear solvers 3563 instead of working directly with matrix algebra routines such as this. 3564 See, e.g., KSPCreate(). 3565 3566 Level: developer 3567 3568 .seealso: MatSolveAdd(), MatSolveTranspose(), MatSolveTransposeAdd() 3569 @*/ 3570 PetscErrorCode MatSolve(Mat mat,Vec b,Vec x) 3571 { 3572 PetscErrorCode ierr; 3573 3574 PetscFunctionBegin; 3575 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3576 PetscValidType(mat,1); 3577 PetscValidHeaderSpecific(b,VEC_CLASSID,2); 3578 PetscValidHeaderSpecific(x,VEC_CLASSID,3); 3579 PetscCheckSameComm(mat,1,b,2); 3580 PetscCheckSameComm(mat,1,x,3); 3581 PetscCheckFalse(x == b,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 3582 PetscCheckFalse(mat->cmap->N != x->map->N,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %" PetscInt_FMT " %" PetscInt_FMT,mat->cmap->N,x->map->N); 3583 PetscCheckFalse(mat->rmap->N != b->map->N,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %" PetscInt_FMT " %" PetscInt_FMT,mat->rmap->N,b->map->N); 3584 PetscCheckFalse(mat->rmap->n != b->map->n,PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %" PetscInt_FMT " %" PetscInt_FMT,mat->rmap->n,b->map->n); 3585 if (!mat->rmap->N && !mat->cmap->N) PetscFunctionReturn(0); 3586 MatCheckPreallocated(mat,1); 3587 3588 ierr = PetscLogEventBegin(MAT_Solve,mat,b,x,0);CHKERRQ(ierr); 3589 if (mat->factorerrortype) { 3590 ierr = PetscInfo(mat,"MatFactorError %d\n",mat->factorerrortype);CHKERRQ(ierr); 3591 ierr = VecSetInf(x);CHKERRQ(ierr); 3592 } else { 3593 PetscCheckFalse(!mat->ops->solve,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 3594 ierr = (*mat->ops->solve)(mat,b,x);CHKERRQ(ierr); 3595 } 3596 ierr = PetscLogEventEnd(MAT_Solve,mat,b,x,0);CHKERRQ(ierr); 3597 ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr); 3598 PetscFunctionReturn(0); 3599 } 3600 3601 static PetscErrorCode MatMatSolve_Basic(Mat A,Mat B,Mat X,PetscBool trans) 3602 { 3603 PetscErrorCode ierr; 3604 Vec b,x; 3605 PetscInt N,i; 3606 PetscErrorCode (*f)(Mat,Vec,Vec); 3607 PetscBool Abound,Bneedconv = PETSC_FALSE,Xneedconv = PETSC_FALSE; 3608 3609 PetscFunctionBegin; 3610 if (A->factorerrortype) { 3611 ierr = PetscInfo(A,"MatFactorError %d\n",A->factorerrortype);CHKERRQ(ierr); 3612 ierr = MatSetInf(X);CHKERRQ(ierr); 3613 PetscFunctionReturn(0); 3614 } 3615 f = (!trans || (!A->ops->solvetranspose && A->symmetric)) ? A->ops->solve : A->ops->solvetranspose; 3616 PetscCheckFalse(!f,PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Mat type %s",((PetscObject)A)->type_name); 3617 ierr = MatBoundToCPU(A,&Abound);CHKERRQ(ierr); 3618 if (!Abound) { 3619 ierr = PetscObjectTypeCompareAny((PetscObject)B,&Bneedconv,MATSEQDENSE,MATMPIDENSE,"");CHKERRQ(ierr); 3620 ierr = PetscObjectTypeCompareAny((PetscObject)X,&Xneedconv,MATSEQDENSE,MATMPIDENSE,"");CHKERRQ(ierr); 3621 } 3622 if (Bneedconv) { 3623 ierr = MatConvert(B,MATDENSECUDA,MAT_INPLACE_MATRIX,&B);CHKERRQ(ierr); 3624 } 3625 if (Xneedconv) { 3626 ierr = MatConvert(X,MATDENSECUDA,MAT_INPLACE_MATRIX,&X);CHKERRQ(ierr); 3627 } 3628 ierr = MatGetSize(B,NULL,&N);CHKERRQ(ierr); 3629 for (i=0; i<N; i++) { 3630 ierr = MatDenseGetColumnVecRead(B,i,&b);CHKERRQ(ierr); 3631 ierr = MatDenseGetColumnVecWrite(X,i,&x);CHKERRQ(ierr); 3632 ierr = (*f)(A,b,x);CHKERRQ(ierr); 3633 ierr = MatDenseRestoreColumnVecWrite(X,i,&x);CHKERRQ(ierr); 3634 ierr = MatDenseRestoreColumnVecRead(B,i,&b);CHKERRQ(ierr); 3635 } 3636 if (Bneedconv) { 3637 ierr = MatConvert(B,MATDENSE,MAT_INPLACE_MATRIX,&B);CHKERRQ(ierr); 3638 } 3639 if (Xneedconv) { 3640 ierr = MatConvert(X,MATDENSE,MAT_INPLACE_MATRIX,&X);CHKERRQ(ierr); 3641 } 3642 PetscFunctionReturn(0); 3643 } 3644 3645 /*@ 3646 MatMatSolve - Solves A X = B, given a factored matrix. 3647 3648 Neighbor-wise Collective on Mat 3649 3650 Input Parameters: 3651 + A - the factored matrix 3652 - B - the right-hand-side matrix MATDENSE (or sparse -- when using MUMPS) 3653 3654 Output Parameter: 3655 . X - the result matrix (dense matrix) 3656 3657 Notes: 3658 If B is a MATDENSE matrix then one can call MatMatSolve(A,B,B) except with MKL_CPARDISO; 3659 otherwise, B and X cannot be the same. 3660 3661 Notes: 3662 Most users should usually employ the simplified KSP interface for linear solvers 3663 instead of working directly with matrix algebra routines such as this. 3664 See, e.g., KSPCreate(). However KSP can only solve for one vector (column of X) 3665 at a time. 3666 3667 Level: developer 3668 3669 .seealso: MatMatSolveTranspose(), MatLUFactor(), MatCholeskyFactor() 3670 @*/ 3671 PetscErrorCode MatMatSolve(Mat A,Mat B,Mat X) 3672 { 3673 PetscErrorCode ierr; 3674 3675 PetscFunctionBegin; 3676 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 3677 PetscValidType(A,1); 3678 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 3679 PetscValidHeaderSpecific(X,MAT_CLASSID,3); 3680 PetscCheckSameComm(A,1,B,2); 3681 PetscCheckSameComm(A,1,X,3); 3682 PetscCheckFalse(A->cmap->N != X->rmap->N,PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Mat A,Mat X: global dim %" PetscInt_FMT " %" PetscInt_FMT,A->cmap->N,X->rmap->N); 3683 PetscCheckFalse(A->rmap->N != B->rmap->N,PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Mat A,Mat B: global dim %" PetscInt_FMT " %" PetscInt_FMT,A->rmap->N,B->rmap->N); 3684 PetscCheckFalse(X->cmap->N != B->cmap->N,PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Solution matrix must have same number of columns as rhs matrix"); 3685 if (!A->rmap->N && !A->cmap->N) PetscFunctionReturn(0); 3686 PetscCheckFalse(!A->factortype,PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 3687 MatCheckPreallocated(A,1); 3688 3689 ierr = PetscLogEventBegin(MAT_MatSolve,A,B,X,0);CHKERRQ(ierr); 3690 if (!A->ops->matsolve) { 3691 ierr = PetscInfo(A,"Mat type %s using basic MatMatSolve\n",((PetscObject)A)->type_name);CHKERRQ(ierr); 3692 ierr = MatMatSolve_Basic(A,B,X,PETSC_FALSE);CHKERRQ(ierr); 3693 } else { 3694 ierr = (*A->ops->matsolve)(A,B,X);CHKERRQ(ierr); 3695 } 3696 ierr = PetscLogEventEnd(MAT_MatSolve,A,B,X,0);CHKERRQ(ierr); 3697 ierr = PetscObjectStateIncrease((PetscObject)X);CHKERRQ(ierr); 3698 PetscFunctionReturn(0); 3699 } 3700 3701 /*@ 3702 MatMatSolveTranspose - Solves A^T X = B, given a factored matrix. 3703 3704 Neighbor-wise Collective on Mat 3705 3706 Input Parameters: 3707 + A - the factored matrix 3708 - B - the right-hand-side matrix (dense matrix) 3709 3710 Output Parameter: 3711 . X - the result matrix (dense matrix) 3712 3713 Notes: 3714 The matrices B and X cannot be the same. I.e., one cannot 3715 call MatMatSolveTranspose(A,X,X). 3716 3717 Notes: 3718 Most users should usually employ the simplified KSP interface for linear solvers 3719 instead of working directly with matrix algebra routines such as this. 3720 See, e.g., KSPCreate(). However KSP can only solve for one vector (column of X) 3721 at a time. 3722 3723 When using SuperLU_Dist or MUMPS as a parallel solver, PETSc will use their functionality to solve multiple right hand sides simultaneously. 3724 3725 Level: developer 3726 3727 .seealso: MatMatSolve(), MatLUFactor(), MatCholeskyFactor() 3728 @*/ 3729 PetscErrorCode MatMatSolveTranspose(Mat A,Mat B,Mat X) 3730 { 3731 PetscErrorCode ierr; 3732 3733 PetscFunctionBegin; 3734 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 3735 PetscValidType(A,1); 3736 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 3737 PetscValidHeaderSpecific(X,MAT_CLASSID,3); 3738 PetscCheckSameComm(A,1,B,2); 3739 PetscCheckSameComm(A,1,X,3); 3740 PetscCheckFalse(X == B,PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_IDN,"X and B must be different matrices"); 3741 PetscCheckFalse(A->cmap->N != X->rmap->N,PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Mat A,Mat X: global dim %" PetscInt_FMT " %" PetscInt_FMT,A->cmap->N,X->rmap->N); 3742 PetscCheckFalse(A->rmap->N != B->rmap->N,PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Mat A,Mat B: global dim %" PetscInt_FMT " %" PetscInt_FMT,A->rmap->N,B->rmap->N); 3743 PetscCheckFalse(A->rmap->n != B->rmap->n,PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat A,Mat B: local dim %" PetscInt_FMT " %" PetscInt_FMT,A->rmap->n,B->rmap->n); 3744 PetscCheckFalse(X->cmap->N < B->cmap->N,PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Solution matrix must have same number of columns as rhs matrix"); 3745 if (!A->rmap->N && !A->cmap->N) PetscFunctionReturn(0); 3746 PetscCheckFalse(!A->factortype,PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 3747 MatCheckPreallocated(A,1); 3748 3749 ierr = PetscLogEventBegin(MAT_MatSolve,A,B,X,0);CHKERRQ(ierr); 3750 if (!A->ops->matsolvetranspose) { 3751 ierr = PetscInfo(A,"Mat type %s using basic MatMatSolveTranspose\n",((PetscObject)A)->type_name);CHKERRQ(ierr); 3752 ierr = MatMatSolve_Basic(A,B,X,PETSC_TRUE);CHKERRQ(ierr); 3753 } else { 3754 ierr = (*A->ops->matsolvetranspose)(A,B,X);CHKERRQ(ierr); 3755 } 3756 ierr = PetscLogEventEnd(MAT_MatSolve,A,B,X,0);CHKERRQ(ierr); 3757 ierr = PetscObjectStateIncrease((PetscObject)X);CHKERRQ(ierr); 3758 PetscFunctionReturn(0); 3759 } 3760 3761 /*@ 3762 MatMatTransposeSolve - Solves A X = B^T, given a factored matrix. 3763 3764 Neighbor-wise Collective on Mat 3765 3766 Input Parameters: 3767 + A - the factored matrix 3768 - Bt - the transpose of right-hand-side matrix 3769 3770 Output Parameter: 3771 . X - the result matrix (dense matrix) 3772 3773 Notes: 3774 Most users should usually employ the simplified KSP interface for linear solvers 3775 instead of working directly with matrix algebra routines such as this. 3776 See, e.g., KSPCreate(). However KSP can only solve for one vector (column of X) 3777 at a time. 3778 3779 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(). 3780 3781 Level: developer 3782 3783 .seealso: MatMatSolve(), MatMatSolveTranspose(), MatLUFactor(), MatCholeskyFactor() 3784 @*/ 3785 PetscErrorCode MatMatTransposeSolve(Mat A,Mat Bt,Mat X) 3786 { 3787 PetscErrorCode ierr; 3788 3789 PetscFunctionBegin; 3790 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 3791 PetscValidType(A,1); 3792 PetscValidHeaderSpecific(Bt,MAT_CLASSID,2); 3793 PetscValidHeaderSpecific(X,MAT_CLASSID,3); 3794 PetscCheckSameComm(A,1,Bt,2); 3795 PetscCheckSameComm(A,1,X,3); 3796 3797 PetscCheckFalse(X == Bt,PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_IDN,"X and B must be different matrices"); 3798 PetscCheckFalse(A->cmap->N != X->rmap->N,PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Mat A,Mat X: global dim %" PetscInt_FMT " %" PetscInt_FMT,A->cmap->N,X->rmap->N); 3799 PetscCheckFalse(A->rmap->N != Bt->cmap->N,PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Mat A,Mat Bt: global dim %" PetscInt_FMT " %" PetscInt_FMT,A->rmap->N,Bt->cmap->N); 3800 PetscCheckFalse(X->cmap->N < Bt->rmap->N,PetscObjectComm((PetscObject)X),PETSC_ERR_ARG_SIZ,"Solution matrix must have same number of columns as row number of the rhs matrix"); 3801 if (!A->rmap->N && !A->cmap->N) PetscFunctionReturn(0); 3802 PetscCheckFalse(!A->factortype,PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 3803 MatCheckPreallocated(A,1); 3804 3805 PetscCheckFalse(!A->ops->mattransposesolve,PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Mat type %s",((PetscObject)A)->type_name); 3806 ierr = PetscLogEventBegin(MAT_MatTrSolve,A,Bt,X,0);CHKERRQ(ierr); 3807 ierr = (*A->ops->mattransposesolve)(A,Bt,X);CHKERRQ(ierr); 3808 ierr = PetscLogEventEnd(MAT_MatTrSolve,A,Bt,X,0);CHKERRQ(ierr); 3809 ierr = PetscObjectStateIncrease((PetscObject)X);CHKERRQ(ierr); 3810 PetscFunctionReturn(0); 3811 } 3812 3813 /*@ 3814 MatForwardSolve - Solves L x = b, given a factored matrix, A = LU, or 3815 U^T*D^(1/2) x = b, given a factored symmetric matrix, A = U^T*D*U, 3816 3817 Neighbor-wise Collective on Mat 3818 3819 Input Parameters: 3820 + mat - the factored matrix 3821 - b - the right-hand-side vector 3822 3823 Output Parameter: 3824 . x - the result vector 3825 3826 Notes: 3827 MatSolve() should be used for most applications, as it performs 3828 a forward solve followed by a backward solve. 3829 3830 The vectors b and x cannot be the same, i.e., one cannot 3831 call MatForwardSolve(A,x,x). 3832 3833 For matrix in seqsbaij format with block size larger than 1, 3834 the diagonal blocks are not implemented as D = D^(1/2) * D^(1/2) yet. 3835 MatForwardSolve() solves U^T*D y = b, and 3836 MatBackwardSolve() solves U x = y. 3837 Thus they do not provide a symmetric preconditioner. 3838 3839 Most users should employ the simplified KSP interface for linear solvers 3840 instead of working directly with matrix algebra routines such as this. 3841 See, e.g., KSPCreate(). 3842 3843 Level: developer 3844 3845 .seealso: MatSolve(), MatBackwardSolve() 3846 @*/ 3847 PetscErrorCode MatForwardSolve(Mat mat,Vec b,Vec x) 3848 { 3849 PetscErrorCode ierr; 3850 3851 PetscFunctionBegin; 3852 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3853 PetscValidType(mat,1); 3854 PetscValidHeaderSpecific(b,VEC_CLASSID,2); 3855 PetscValidHeaderSpecific(x,VEC_CLASSID,3); 3856 PetscCheckSameComm(mat,1,b,2); 3857 PetscCheckSameComm(mat,1,x,3); 3858 PetscCheckFalse(x == b,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 3859 PetscCheckFalse(mat->cmap->N != x->map->N,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %" PetscInt_FMT " %" PetscInt_FMT,mat->cmap->N,x->map->N); 3860 PetscCheckFalse(mat->rmap->N != b->map->N,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %" PetscInt_FMT " %" PetscInt_FMT,mat->rmap->N,b->map->N); 3861 PetscCheckFalse(mat->rmap->n != b->map->n,PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %" PetscInt_FMT " %" PetscInt_FMT,mat->rmap->n,b->map->n); 3862 if (!mat->rmap->N && !mat->cmap->N) PetscFunctionReturn(0); 3863 MatCheckPreallocated(mat,1); 3864 3865 PetscCheckFalse(!mat->ops->forwardsolve,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 3866 ierr = PetscLogEventBegin(MAT_ForwardSolve,mat,b,x,0);CHKERRQ(ierr); 3867 ierr = (*mat->ops->forwardsolve)(mat,b,x);CHKERRQ(ierr); 3868 ierr = PetscLogEventEnd(MAT_ForwardSolve,mat,b,x,0);CHKERRQ(ierr); 3869 ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr); 3870 PetscFunctionReturn(0); 3871 } 3872 3873 /*@ 3874 MatBackwardSolve - Solves U x = b, given a factored matrix, A = LU. 3875 D^(1/2) U x = b, given a factored symmetric matrix, A = U^T*D*U, 3876 3877 Neighbor-wise Collective on Mat 3878 3879 Input Parameters: 3880 + mat - the factored matrix 3881 - b - the right-hand-side vector 3882 3883 Output Parameter: 3884 . x - the result vector 3885 3886 Notes: 3887 MatSolve() should be used for most applications, as it performs 3888 a forward solve followed by a backward solve. 3889 3890 The vectors b and x cannot be the same. I.e., one cannot 3891 call MatBackwardSolve(A,x,x). 3892 3893 For matrix in seqsbaij format with block size larger than 1, 3894 the diagonal blocks are not implemented as D = D^(1/2) * D^(1/2) yet. 3895 MatForwardSolve() solves U^T*D y = b, and 3896 MatBackwardSolve() solves U x = y. 3897 Thus they do not provide a symmetric preconditioner. 3898 3899 Most users should employ the simplified KSP interface for linear solvers 3900 instead of working directly with matrix algebra routines such as this. 3901 See, e.g., KSPCreate(). 3902 3903 Level: developer 3904 3905 .seealso: MatSolve(), MatForwardSolve() 3906 @*/ 3907 PetscErrorCode MatBackwardSolve(Mat mat,Vec b,Vec x) 3908 { 3909 PetscErrorCode ierr; 3910 3911 PetscFunctionBegin; 3912 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3913 PetscValidType(mat,1); 3914 PetscValidHeaderSpecific(b,VEC_CLASSID,2); 3915 PetscValidHeaderSpecific(x,VEC_CLASSID,3); 3916 PetscCheckSameComm(mat,1,b,2); 3917 PetscCheckSameComm(mat,1,x,3); 3918 PetscCheckFalse(x == b,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 3919 PetscCheckFalse(mat->cmap->N != x->map->N,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %" PetscInt_FMT " %" PetscInt_FMT,mat->cmap->N,x->map->N); 3920 PetscCheckFalse(mat->rmap->N != b->map->N,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %" PetscInt_FMT " %" PetscInt_FMT,mat->rmap->N,b->map->N); 3921 PetscCheckFalse(mat->rmap->n != b->map->n,PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %" PetscInt_FMT " %" PetscInt_FMT,mat->rmap->n,b->map->n); 3922 if (!mat->rmap->N && !mat->cmap->N) PetscFunctionReturn(0); 3923 MatCheckPreallocated(mat,1); 3924 3925 PetscCheckFalse(!mat->ops->backwardsolve,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 3926 ierr = PetscLogEventBegin(MAT_BackwardSolve,mat,b,x,0);CHKERRQ(ierr); 3927 ierr = (*mat->ops->backwardsolve)(mat,b,x);CHKERRQ(ierr); 3928 ierr = PetscLogEventEnd(MAT_BackwardSolve,mat,b,x,0);CHKERRQ(ierr); 3929 ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr); 3930 PetscFunctionReturn(0); 3931 } 3932 3933 /*@ 3934 MatSolveAdd - Computes x = y + inv(A)*b, given a factored matrix. 3935 3936 Neighbor-wise Collective on Mat 3937 3938 Input Parameters: 3939 + mat - the factored matrix 3940 . b - the right-hand-side vector 3941 - y - the vector to be added to 3942 3943 Output Parameter: 3944 . x - the result vector 3945 3946 Notes: 3947 The vectors b and x cannot be the same. I.e., one cannot 3948 call MatSolveAdd(A,x,y,x). 3949 3950 Most users should employ the simplified KSP interface for linear solvers 3951 instead of working directly with matrix algebra routines such as this. 3952 See, e.g., KSPCreate(). 3953 3954 Level: developer 3955 3956 .seealso: MatSolve(), MatSolveTranspose(), MatSolveTransposeAdd() 3957 @*/ 3958 PetscErrorCode MatSolveAdd(Mat mat,Vec b,Vec y,Vec x) 3959 { 3960 PetscScalar one = 1.0; 3961 Vec tmp; 3962 PetscErrorCode ierr; 3963 3964 PetscFunctionBegin; 3965 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3966 PetscValidType(mat,1); 3967 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 3968 PetscValidHeaderSpecific(b,VEC_CLASSID,2); 3969 PetscValidHeaderSpecific(x,VEC_CLASSID,4); 3970 PetscCheckSameComm(mat,1,b,2); 3971 PetscCheckSameComm(mat,1,y,3); 3972 PetscCheckSameComm(mat,1,x,4); 3973 PetscCheckFalse(x == b,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 3974 PetscCheckFalse(mat->cmap->N != x->map->N,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %" PetscInt_FMT " %" PetscInt_FMT,mat->cmap->N,x->map->N); 3975 PetscCheckFalse(mat->rmap->N != b->map->N,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %" PetscInt_FMT " %" PetscInt_FMT,mat->rmap->N,b->map->N); 3976 PetscCheckFalse(mat->rmap->N != y->map->N,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %" PetscInt_FMT " %" PetscInt_FMT,mat->rmap->N,y->map->N); 3977 PetscCheckFalse(mat->rmap->n != b->map->n,PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %" PetscInt_FMT " %" PetscInt_FMT,mat->rmap->n,b->map->n); 3978 PetscCheckFalse(x->map->n != y->map->n,PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Vec x,Vec y: local dim %" PetscInt_FMT " %" PetscInt_FMT,x->map->n,y->map->n); 3979 if (!mat->rmap->N && !mat->cmap->N) PetscFunctionReturn(0); 3980 MatCheckPreallocated(mat,1); 3981 3982 ierr = PetscLogEventBegin(MAT_SolveAdd,mat,b,x,y);CHKERRQ(ierr); 3983 if (mat->factorerrortype) { 3984 3985 ierr = PetscInfo(mat,"MatFactorError %d\n",mat->factorerrortype);CHKERRQ(ierr); 3986 ierr = VecSetInf(x);CHKERRQ(ierr); 3987 } else if (mat->ops->solveadd) { 3988 ierr = (*mat->ops->solveadd)(mat,b,y,x);CHKERRQ(ierr); 3989 } else { 3990 /* do the solve then the add manually */ 3991 if (x != y) { 3992 ierr = MatSolve(mat,b,x);CHKERRQ(ierr); 3993 ierr = VecAXPY(x,one,y);CHKERRQ(ierr); 3994 } else { 3995 ierr = VecDuplicate(x,&tmp);CHKERRQ(ierr); 3996 ierr = PetscLogObjectParent((PetscObject)mat,(PetscObject)tmp);CHKERRQ(ierr); 3997 ierr = VecCopy(x,tmp);CHKERRQ(ierr); 3998 ierr = MatSolve(mat,b,x);CHKERRQ(ierr); 3999 ierr = VecAXPY(x,one,tmp);CHKERRQ(ierr); 4000 ierr = VecDestroy(&tmp);CHKERRQ(ierr); 4001 } 4002 } 4003 ierr = PetscLogEventEnd(MAT_SolveAdd,mat,b,x,y);CHKERRQ(ierr); 4004 ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr); 4005 PetscFunctionReturn(0); 4006 } 4007 4008 /*@ 4009 MatSolveTranspose - Solves A' x = b, given a factored matrix. 4010 4011 Neighbor-wise Collective on Mat 4012 4013 Input Parameters: 4014 + mat - the factored matrix 4015 - b - the right-hand-side vector 4016 4017 Output Parameter: 4018 . x - the result vector 4019 4020 Notes: 4021 The vectors b and x cannot be the same. I.e., one cannot 4022 call MatSolveTranspose(A,x,x). 4023 4024 Most users should employ the simplified KSP interface for linear solvers 4025 instead of working directly with matrix algebra routines such as this. 4026 See, e.g., KSPCreate(). 4027 4028 Level: developer 4029 4030 .seealso: MatSolve(), MatSolveAdd(), MatSolveTransposeAdd() 4031 @*/ 4032 PetscErrorCode MatSolveTranspose(Mat mat,Vec b,Vec x) 4033 { 4034 PetscErrorCode ierr; 4035 PetscErrorCode (*f)(Mat,Vec,Vec) = (!mat->ops->solvetranspose && mat->symmetric) ? mat->ops->solve : mat->ops->solvetranspose; 4036 4037 PetscFunctionBegin; 4038 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4039 PetscValidType(mat,1); 4040 PetscValidHeaderSpecific(b,VEC_CLASSID,2); 4041 PetscValidHeaderSpecific(x,VEC_CLASSID,3); 4042 PetscCheckSameComm(mat,1,b,2); 4043 PetscCheckSameComm(mat,1,x,3); 4044 PetscCheckFalse(x == b,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 4045 PetscCheckFalse(mat->rmap->N != x->map->N,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %" PetscInt_FMT " %" PetscInt_FMT,mat->rmap->N,x->map->N); 4046 PetscCheckFalse(mat->cmap->N != b->map->N,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %" PetscInt_FMT " %" PetscInt_FMT,mat->cmap->N,b->map->N); 4047 if (!mat->rmap->N && !mat->cmap->N) PetscFunctionReturn(0); 4048 MatCheckPreallocated(mat,1); 4049 ierr = PetscLogEventBegin(MAT_SolveTranspose,mat,b,x,0);CHKERRQ(ierr); 4050 if (mat->factorerrortype) { 4051 ierr = PetscInfo(mat,"MatFactorError %d\n",mat->factorerrortype);CHKERRQ(ierr); 4052 ierr = VecSetInf(x);CHKERRQ(ierr); 4053 } else { 4054 PetscCheckFalse(!f,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s",((PetscObject)mat)->type_name); 4055 ierr = (*f)(mat,b,x);CHKERRQ(ierr); 4056 } 4057 ierr = PetscLogEventEnd(MAT_SolveTranspose,mat,b,x,0);CHKERRQ(ierr); 4058 ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr); 4059 PetscFunctionReturn(0); 4060 } 4061 4062 /*@ 4063 MatSolveTransposeAdd - Computes x = y + inv(Transpose(A)) b, given a 4064 factored matrix. 4065 4066 Neighbor-wise Collective on Mat 4067 4068 Input Parameters: 4069 + mat - the factored matrix 4070 . b - the right-hand-side vector 4071 - y - the vector to be added to 4072 4073 Output Parameter: 4074 . x - the result vector 4075 4076 Notes: 4077 The vectors b and x cannot be the same. I.e., one cannot 4078 call MatSolveTransposeAdd(A,x,y,x). 4079 4080 Most users should employ the simplified KSP interface for linear solvers 4081 instead of working directly with matrix algebra routines such as this. 4082 See, e.g., KSPCreate(). 4083 4084 Level: developer 4085 4086 .seealso: MatSolve(), MatSolveAdd(), MatSolveTranspose() 4087 @*/ 4088 PetscErrorCode MatSolveTransposeAdd(Mat mat,Vec b,Vec y,Vec x) 4089 { 4090 PetscScalar one = 1.0; 4091 PetscErrorCode ierr; 4092 Vec tmp; 4093 PetscErrorCode (*f)(Mat,Vec,Vec,Vec) = (!mat->ops->solvetransposeadd && mat->symmetric) ? mat->ops->solveadd : mat->ops->solvetransposeadd; 4094 4095 PetscFunctionBegin; 4096 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4097 PetscValidType(mat,1); 4098 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 4099 PetscValidHeaderSpecific(b,VEC_CLASSID,2); 4100 PetscValidHeaderSpecific(x,VEC_CLASSID,4); 4101 PetscCheckSameComm(mat,1,b,2); 4102 PetscCheckSameComm(mat,1,y,3); 4103 PetscCheckSameComm(mat,1,x,4); 4104 PetscCheckFalse(x == b,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 4105 PetscCheckFalse(mat->rmap->N != x->map->N,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %" PetscInt_FMT " %" PetscInt_FMT,mat->rmap->N,x->map->N); 4106 PetscCheckFalse(mat->cmap->N != b->map->N,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %" PetscInt_FMT " %" PetscInt_FMT,mat->cmap->N,b->map->N); 4107 PetscCheckFalse(mat->cmap->N != y->map->N,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %" PetscInt_FMT " %" PetscInt_FMT,mat->cmap->N,y->map->N); 4108 PetscCheckFalse(x->map->n != y->map->n,PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Vec x,Vec y: local dim %" PetscInt_FMT " %" PetscInt_FMT,x->map->n,y->map->n); 4109 if (!mat->rmap->N && !mat->cmap->N) PetscFunctionReturn(0); 4110 MatCheckPreallocated(mat,1); 4111 4112 ierr = PetscLogEventBegin(MAT_SolveTransposeAdd,mat,b,x,y);CHKERRQ(ierr); 4113 if (mat->factorerrortype) { 4114 ierr = PetscInfo(mat,"MatFactorError %d\n",mat->factorerrortype);CHKERRQ(ierr); 4115 ierr = VecSetInf(x);CHKERRQ(ierr); 4116 } else if (f) { 4117 ierr = (*f)(mat,b,y,x);CHKERRQ(ierr); 4118 } else { 4119 /* do the solve then the add manually */ 4120 if (x != y) { 4121 ierr = MatSolveTranspose(mat,b,x);CHKERRQ(ierr); 4122 ierr = VecAXPY(x,one,y);CHKERRQ(ierr); 4123 } else { 4124 ierr = VecDuplicate(x,&tmp);CHKERRQ(ierr); 4125 ierr = PetscLogObjectParent((PetscObject)mat,(PetscObject)tmp);CHKERRQ(ierr); 4126 ierr = VecCopy(x,tmp);CHKERRQ(ierr); 4127 ierr = MatSolveTranspose(mat,b,x);CHKERRQ(ierr); 4128 ierr = VecAXPY(x,one,tmp);CHKERRQ(ierr); 4129 ierr = VecDestroy(&tmp);CHKERRQ(ierr); 4130 } 4131 } 4132 ierr = PetscLogEventEnd(MAT_SolveTransposeAdd,mat,b,x,y);CHKERRQ(ierr); 4133 ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr); 4134 PetscFunctionReturn(0); 4135 } 4136 /* ----------------------------------------------------------------*/ 4137 4138 /*@ 4139 MatSOR - Computes relaxation (SOR, Gauss-Seidel) sweeps. 4140 4141 Neighbor-wise Collective on Mat 4142 4143 Input Parameters: 4144 + mat - the matrix 4145 . b - the right hand side 4146 . omega - the relaxation factor 4147 . flag - flag indicating the type of SOR (see below) 4148 . shift - diagonal shift 4149 . its - the number of iterations 4150 - lits - the number of local iterations 4151 4152 Output Parameter: 4153 . x - the solution (can contain an initial guess, use option SOR_ZERO_INITIAL_GUESS to indicate no guess) 4154 4155 SOR Flags: 4156 + SOR_FORWARD_SWEEP - forward SOR 4157 . SOR_BACKWARD_SWEEP - backward SOR 4158 . SOR_SYMMETRIC_SWEEP - SSOR (symmetric SOR) 4159 . SOR_LOCAL_FORWARD_SWEEP - local forward SOR 4160 . SOR_LOCAL_BACKWARD_SWEEP - local forward SOR 4161 . SOR_LOCAL_SYMMETRIC_SWEEP - local SSOR 4162 . SOR_APPLY_UPPER, SOR_APPLY_LOWER - applies 4163 upper/lower triangular part of matrix to 4164 vector (with omega) 4165 - SOR_ZERO_INITIAL_GUESS - zero initial guess 4166 4167 Notes: 4168 SOR_LOCAL_FORWARD_SWEEP, SOR_LOCAL_BACKWARD_SWEEP, and 4169 SOR_LOCAL_SYMMETRIC_SWEEP perform separate independent smoothings 4170 on each processor. 4171 4172 Application programmers will not generally use MatSOR() directly, 4173 but instead will employ the KSP/PC interface. 4174 4175 Notes: 4176 for BAIJ, SBAIJ, and AIJ matrices with Inodes this does a block SOR smoothing, otherwise it does a pointwise smoothing 4177 4178 Notes for Advanced Users: 4179 The flags are implemented as bitwise inclusive or operations. 4180 For example, use (SOR_ZERO_INITIAL_GUESS | SOR_SYMMETRIC_SWEEP) 4181 to specify a zero initial guess for SSOR. 4182 4183 Most users should employ the simplified KSP interface for linear solvers 4184 instead of working directly with matrix algebra routines such as this. 4185 See, e.g., KSPCreate(). 4186 4187 Vectors x and b CANNOT be the same 4188 4189 Developer Note: We should add block SOR support for AIJ matrices with block size set to great than one and no inodes 4190 4191 Level: developer 4192 4193 @*/ 4194 PetscErrorCode MatSOR(Mat mat,Vec b,PetscReal omega,MatSORType flag,PetscReal shift,PetscInt its,PetscInt lits,Vec x) 4195 { 4196 PetscErrorCode ierr; 4197 4198 PetscFunctionBegin; 4199 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4200 PetscValidType(mat,1); 4201 PetscValidHeaderSpecific(b,VEC_CLASSID,2); 4202 PetscValidHeaderSpecific(x,VEC_CLASSID,8); 4203 PetscCheckSameComm(mat,1,b,2); 4204 PetscCheckSameComm(mat,1,x,8); 4205 PetscCheckFalse(!mat->ops->sor,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 4206 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4207 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4208 PetscCheckFalse(mat->cmap->N != x->map->N,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %" PetscInt_FMT " %" PetscInt_FMT,mat->cmap->N,x->map->N); 4209 PetscCheckFalse(mat->rmap->N != b->map->N,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %" PetscInt_FMT " %" PetscInt_FMT,mat->rmap->N,b->map->N); 4210 PetscCheckFalse(mat->rmap->n != b->map->n,PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %" PetscInt_FMT " %" PetscInt_FMT,mat->rmap->n,b->map->n); 4211 PetscCheckFalse(its <= 0,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Relaxation requires global its %" PetscInt_FMT " positive",its); 4212 PetscCheckFalse(lits <= 0,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Relaxation requires local its %" PetscInt_FMT " positive",lits); 4213 PetscCheckFalse(b == x,PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"b and x vector cannot be the same"); 4214 4215 MatCheckPreallocated(mat,1); 4216 ierr = PetscLogEventBegin(MAT_SOR,mat,b,x,0);CHKERRQ(ierr); 4217 ierr = (*mat->ops->sor)(mat,b,omega,flag,shift,its,lits,x);CHKERRQ(ierr); 4218 ierr = PetscLogEventEnd(MAT_SOR,mat,b,x,0);CHKERRQ(ierr); 4219 ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr); 4220 PetscFunctionReturn(0); 4221 } 4222 4223 /* 4224 Default matrix copy routine. 4225 */ 4226 PetscErrorCode MatCopy_Basic(Mat A,Mat B,MatStructure str) 4227 { 4228 PetscErrorCode ierr; 4229 PetscInt i,rstart = 0,rend = 0,nz; 4230 const PetscInt *cwork; 4231 const PetscScalar *vwork; 4232 4233 PetscFunctionBegin; 4234 if (B->assembled) { 4235 ierr = MatZeroEntries(B);CHKERRQ(ierr); 4236 } 4237 if (str == SAME_NONZERO_PATTERN) { 4238 ierr = MatGetOwnershipRange(A,&rstart,&rend);CHKERRQ(ierr); 4239 for (i=rstart; i<rend; i++) { 4240 ierr = MatGetRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr); 4241 ierr = MatSetValues(B,1,&i,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr); 4242 ierr = MatRestoreRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr); 4243 } 4244 } else { 4245 ierr = MatAYPX(B,0.0,A,str);CHKERRQ(ierr); 4246 } 4247 ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4248 ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4249 PetscFunctionReturn(0); 4250 } 4251 4252 /*@ 4253 MatCopy - Copies a matrix to another matrix. 4254 4255 Collective on Mat 4256 4257 Input Parameters: 4258 + A - the matrix 4259 - str - SAME_NONZERO_PATTERN or DIFFERENT_NONZERO_PATTERN 4260 4261 Output Parameter: 4262 . B - where the copy is put 4263 4264 Notes: 4265 If you use SAME_NONZERO_PATTERN then the two matrices must have the same nonzero pattern or the routine will crash. 4266 4267 MatCopy() copies the matrix entries of a matrix to another existing 4268 matrix (after first zeroing the second matrix). A related routine is 4269 MatConvert(), which first creates a new matrix and then copies the data. 4270 4271 Level: intermediate 4272 4273 .seealso: MatConvert(), MatDuplicate() 4274 @*/ 4275 PetscErrorCode MatCopy(Mat A,Mat B,MatStructure str) 4276 { 4277 PetscErrorCode ierr; 4278 PetscInt i; 4279 4280 PetscFunctionBegin; 4281 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 4282 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 4283 PetscValidType(A,1); 4284 PetscValidType(B,2); 4285 PetscCheckSameComm(A,1,B,2); 4286 MatCheckPreallocated(B,2); 4287 PetscCheckFalse(!A->assembled,PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4288 PetscCheckFalse(A->factortype,PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4289 PetscCheckFalse(A->rmap->N != B->rmap->N || A->cmap->N != B->cmap->N,PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Mat A,Mat B: global dim (%" PetscInt_FMT ",%" PetscInt_FMT ") (%" PetscInt_FMT ",%" PetscInt_FMT ")",A->rmap->N,B->rmap->N,A->cmap->N,B->cmap->N); 4290 MatCheckPreallocated(A,1); 4291 if (A == B) PetscFunctionReturn(0); 4292 4293 ierr = PetscLogEventBegin(MAT_Copy,A,B,0,0);CHKERRQ(ierr); 4294 if (A->ops->copy) { 4295 ierr = (*A->ops->copy)(A,B,str);CHKERRQ(ierr); 4296 } else { /* generic conversion */ 4297 ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr); 4298 } 4299 4300 B->stencil.dim = A->stencil.dim; 4301 B->stencil.noc = A->stencil.noc; 4302 for (i=0; i<=A->stencil.dim; i++) { 4303 B->stencil.dims[i] = A->stencil.dims[i]; 4304 B->stencil.starts[i] = A->stencil.starts[i]; 4305 } 4306 4307 ierr = PetscLogEventEnd(MAT_Copy,A,B,0,0);CHKERRQ(ierr); 4308 ierr = PetscObjectStateIncrease((PetscObject)B);CHKERRQ(ierr); 4309 PetscFunctionReturn(0); 4310 } 4311 4312 /*@C 4313 MatConvert - Converts a matrix to another matrix, either of the same 4314 or different type. 4315 4316 Collective on Mat 4317 4318 Input Parameters: 4319 + mat - the matrix 4320 . newtype - new matrix type. Use MATSAME to create a new matrix of the 4321 same type as the original matrix. 4322 - reuse - denotes if the destination matrix is to be created or reused. 4323 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 4324 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). 4325 4326 Output Parameter: 4327 . M - pointer to place new matrix 4328 4329 Notes: 4330 MatConvert() first creates a new matrix and then copies the data from 4331 the first matrix. A related routine is MatCopy(), which copies the matrix 4332 entries of one matrix to another already existing matrix context. 4333 4334 Cannot be used to convert a sequential matrix to parallel or parallel to sequential, 4335 the MPI communicator of the generated matrix is always the same as the communicator 4336 of the input matrix. 4337 4338 Level: intermediate 4339 4340 .seealso: MatCopy(), MatDuplicate() 4341 @*/ 4342 PetscErrorCode MatConvert(Mat mat,MatType newtype,MatReuse reuse,Mat *M) 4343 { 4344 PetscErrorCode ierr; 4345 PetscBool sametype,issame,flg,issymmetric,ishermitian; 4346 char convname[256],mtype[256]; 4347 Mat B; 4348 4349 PetscFunctionBegin; 4350 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4351 PetscValidType(mat,1); 4352 PetscValidPointer(M,4); 4353 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4354 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4355 MatCheckPreallocated(mat,1); 4356 4357 ierr = PetscOptionsGetString(((PetscObject)mat)->options,((PetscObject)mat)->prefix,"-matconvert_type",mtype,sizeof(mtype),&flg);CHKERRQ(ierr); 4358 if (flg) newtype = mtype; 4359 4360 ierr = PetscObjectTypeCompare((PetscObject)mat,newtype,&sametype);CHKERRQ(ierr); 4361 ierr = PetscStrcmp(newtype,"same",&issame);CHKERRQ(ierr); 4362 PetscCheckFalse((reuse == MAT_INPLACE_MATRIX) && (mat != *M),PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"MAT_INPLACE_MATRIX requires same input and output matrix"); 4363 PetscCheckFalse((reuse == MAT_REUSE_MATRIX) && (mat == *M),PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"MAT_REUSE_MATRIX means reuse matrix in final argument, perhaps you mean MAT_INPLACE_MATRIX"); 4364 4365 if ((reuse == MAT_INPLACE_MATRIX) && (issame || sametype)) { 4366 ierr = PetscInfo(mat,"Early return for inplace %s %d %d\n",((PetscObject)mat)->type_name,sametype,issame);CHKERRQ(ierr); 4367 PetscFunctionReturn(0); 4368 } 4369 4370 /* Cache Mat options because some converter use MatHeaderReplace */ 4371 issymmetric = mat->symmetric; 4372 ishermitian = mat->hermitian; 4373 4374 if ((sametype || issame) && (reuse==MAT_INITIAL_MATRIX) && mat->ops->duplicate) { 4375 ierr = PetscInfo(mat,"Calling duplicate for initial matrix %s %d %d\n",((PetscObject)mat)->type_name,sametype,issame);CHKERRQ(ierr); 4376 ierr = (*mat->ops->duplicate)(mat,MAT_COPY_VALUES,M);CHKERRQ(ierr); 4377 } else { 4378 PetscErrorCode (*conv)(Mat, MatType,MatReuse,Mat*)=NULL; 4379 const char *prefix[3] = {"seq","mpi",""}; 4380 PetscInt i; 4381 /* 4382 Order of precedence: 4383 0) See if newtype is a superclass of the current matrix. 4384 1) See if a specialized converter is known to the current matrix. 4385 2) See if a specialized converter is known to the desired matrix class. 4386 3) See if a good general converter is registered for the desired class 4387 (as of 6/27/03 only MATMPIADJ falls into this category). 4388 4) See if a good general converter is known for the current matrix. 4389 5) Use a really basic converter. 4390 */ 4391 4392 /* 0) See if newtype is a superclass of the current matrix. 4393 i.e mat is mpiaij and newtype is aij */ 4394 for (i=0; i<2; i++) { 4395 ierr = PetscStrncpy(convname,prefix[i],sizeof(convname));CHKERRQ(ierr); 4396 ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr); 4397 ierr = PetscStrcmp(convname,((PetscObject)mat)->type_name,&flg);CHKERRQ(ierr); 4398 ierr = PetscInfo(mat,"Check superclass %s %s -> %d\n",convname,((PetscObject)mat)->type_name,flg);CHKERRQ(ierr); 4399 if (flg) { 4400 if (reuse == MAT_INPLACE_MATRIX) { 4401 ierr = PetscInfo(mat,"Early return\n");CHKERRQ(ierr); 4402 PetscFunctionReturn(0); 4403 } else if (reuse == MAT_INITIAL_MATRIX && mat->ops->duplicate) { 4404 ierr = PetscInfo(mat,"Calling MatDuplicate\n");CHKERRQ(ierr); 4405 ierr = (*mat->ops->duplicate)(mat,MAT_COPY_VALUES,M);CHKERRQ(ierr); 4406 PetscFunctionReturn(0); 4407 } else if (reuse == MAT_REUSE_MATRIX && mat->ops->copy) { 4408 ierr = PetscInfo(mat,"Calling MatCopy\n");CHKERRQ(ierr); 4409 ierr = MatCopy(mat,*M,SAME_NONZERO_PATTERN);CHKERRQ(ierr); 4410 PetscFunctionReturn(0); 4411 } 4412 } 4413 } 4414 /* 1) See if a specialized converter is known to the current matrix and the desired class */ 4415 for (i=0; i<3; i++) { 4416 ierr = PetscStrncpy(convname,"MatConvert_",sizeof(convname));CHKERRQ(ierr); 4417 ierr = PetscStrlcat(convname,((PetscObject)mat)->type_name,sizeof(convname));CHKERRQ(ierr); 4418 ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr); 4419 ierr = PetscStrlcat(convname,prefix[i],sizeof(convname));CHKERRQ(ierr); 4420 ierr = PetscStrlcat(convname,issame ? ((PetscObject)mat)->type_name : newtype,sizeof(convname));CHKERRQ(ierr); 4421 ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr); 4422 ierr = PetscObjectQueryFunction((PetscObject)mat,convname,&conv);CHKERRQ(ierr); 4423 ierr = PetscInfo(mat,"Check specialized (1) %s (%s) -> %d\n",convname,((PetscObject)mat)->type_name,!!conv);CHKERRQ(ierr); 4424 if (conv) goto foundconv; 4425 } 4426 4427 /* 2) See if a specialized converter is known to the desired matrix class. */ 4428 ierr = MatCreate(PetscObjectComm((PetscObject)mat),&B);CHKERRQ(ierr); 4429 ierr = MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->N,mat->cmap->N);CHKERRQ(ierr); 4430 ierr = MatSetType(B,newtype);CHKERRQ(ierr); 4431 for (i=0; i<3; i++) { 4432 ierr = PetscStrncpy(convname,"MatConvert_",sizeof(convname));CHKERRQ(ierr); 4433 ierr = PetscStrlcat(convname,((PetscObject)mat)->type_name,sizeof(convname));CHKERRQ(ierr); 4434 ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr); 4435 ierr = PetscStrlcat(convname,prefix[i],sizeof(convname));CHKERRQ(ierr); 4436 ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr); 4437 ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr); 4438 ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr); 4439 ierr = PetscInfo(mat,"Check specialized (2) %s (%s) -> %d\n",convname,((PetscObject)B)->type_name,!!conv);CHKERRQ(ierr); 4440 if (conv) { 4441 ierr = MatDestroy(&B);CHKERRQ(ierr); 4442 goto foundconv; 4443 } 4444 } 4445 4446 /* 3) See if a good general converter is registered for the desired class */ 4447 conv = B->ops->convertfrom; 4448 ierr = PetscInfo(mat,"Check convertfrom (%s) -> %d\n",((PetscObject)B)->type_name,!!conv);CHKERRQ(ierr); 4449 ierr = MatDestroy(&B);CHKERRQ(ierr); 4450 if (conv) goto foundconv; 4451 4452 /* 4) See if a good general converter is known for the current matrix */ 4453 if (mat->ops->convert) conv = mat->ops->convert; 4454 ierr = PetscInfo(mat,"Check general convert (%s) -> %d\n",((PetscObject)mat)->type_name,!!conv);CHKERRQ(ierr); 4455 if (conv) goto foundconv; 4456 4457 /* 5) Use a really basic converter. */ 4458 ierr = PetscInfo(mat,"Using MatConvert_Basic\n");CHKERRQ(ierr); 4459 conv = MatConvert_Basic; 4460 4461 foundconv: 4462 ierr = PetscLogEventBegin(MAT_Convert,mat,0,0,0);CHKERRQ(ierr); 4463 ierr = (*conv)(mat,newtype,reuse,M);CHKERRQ(ierr); 4464 if (mat->rmap->mapping && mat->cmap->mapping && !(*M)->rmap->mapping && !(*M)->cmap->mapping) { 4465 /* the block sizes must be same if the mappings are copied over */ 4466 (*M)->rmap->bs = mat->rmap->bs; 4467 (*M)->cmap->bs = mat->cmap->bs; 4468 ierr = PetscObjectReference((PetscObject)mat->rmap->mapping);CHKERRQ(ierr); 4469 ierr = PetscObjectReference((PetscObject)mat->cmap->mapping);CHKERRQ(ierr); 4470 (*M)->rmap->mapping = mat->rmap->mapping; 4471 (*M)->cmap->mapping = mat->cmap->mapping; 4472 } 4473 (*M)->stencil.dim = mat->stencil.dim; 4474 (*M)->stencil.noc = mat->stencil.noc; 4475 for (i=0; i<=mat->stencil.dim; i++) { 4476 (*M)->stencil.dims[i] = mat->stencil.dims[i]; 4477 (*M)->stencil.starts[i] = mat->stencil.starts[i]; 4478 } 4479 ierr = PetscLogEventEnd(MAT_Convert,mat,0,0,0);CHKERRQ(ierr); 4480 } 4481 ierr = PetscObjectStateIncrease((PetscObject)*M);CHKERRQ(ierr); 4482 4483 /* Copy Mat options */ 4484 if (issymmetric) { 4485 ierr = MatSetOption(*M,MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr); 4486 } 4487 if (ishermitian) { 4488 ierr = MatSetOption(*M,MAT_HERMITIAN,PETSC_TRUE);CHKERRQ(ierr); 4489 } 4490 PetscFunctionReturn(0); 4491 } 4492 4493 /*@C 4494 MatFactorGetSolverType - Returns name of the package providing the factorization routines 4495 4496 Not Collective 4497 4498 Input Parameter: 4499 . mat - the matrix, must be a factored matrix 4500 4501 Output Parameter: 4502 . type - the string name of the package (do not free this string) 4503 4504 Notes: 4505 In Fortran you pass in a empty string and the package name will be copied into it. 4506 (Make sure the string is long enough) 4507 4508 Level: intermediate 4509 4510 .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable(), MatGetFactor() 4511 @*/ 4512 PetscErrorCode MatFactorGetSolverType(Mat mat, MatSolverType *type) 4513 { 4514 PetscErrorCode ierr, (*conv)(Mat,MatSolverType*); 4515 4516 PetscFunctionBegin; 4517 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4518 PetscValidType(mat,1); 4519 PetscCheckFalse(!mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Only for factored matrix"); 4520 ierr = PetscObjectQueryFunction((PetscObject)mat,"MatFactorGetSolverType_C",&conv);CHKERRQ(ierr); 4521 if (!conv) { 4522 *type = MATSOLVERPETSC; 4523 } else { 4524 ierr = (*conv)(mat,type);CHKERRQ(ierr); 4525 } 4526 PetscFunctionReturn(0); 4527 } 4528 4529 typedef struct _MatSolverTypeForSpecifcType* MatSolverTypeForSpecifcType; 4530 struct _MatSolverTypeForSpecifcType { 4531 MatType mtype; 4532 /* no entry for MAT_FACTOR_NONE */ 4533 PetscErrorCode (*createfactor[MAT_FACTOR_NUM_TYPES-1])(Mat,MatFactorType,Mat*); 4534 MatSolverTypeForSpecifcType next; 4535 }; 4536 4537 typedef struct _MatSolverTypeHolder* MatSolverTypeHolder; 4538 struct _MatSolverTypeHolder { 4539 char *name; 4540 MatSolverTypeForSpecifcType handlers; 4541 MatSolverTypeHolder next; 4542 }; 4543 4544 static MatSolverTypeHolder MatSolverTypeHolders = NULL; 4545 4546 /*@C 4547 MatSolverTypeRegister - Registers a MatSolverType that works for a particular matrix type 4548 4549 Input Parameters: 4550 + package - name of the package, for example petsc or superlu 4551 . mtype - the matrix type that works with this package 4552 . ftype - the type of factorization supported by the package 4553 - createfactor - routine that will create the factored matrix ready to be used 4554 4555 Level: intermediate 4556 4557 .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable(), MatGetFactor() 4558 @*/ 4559 PetscErrorCode MatSolverTypeRegister(MatSolverType package,MatType mtype,MatFactorType ftype,PetscErrorCode (*createfactor)(Mat,MatFactorType,Mat*)) 4560 { 4561 PetscErrorCode ierr; 4562 MatSolverTypeHolder next = MatSolverTypeHolders,prev = NULL; 4563 PetscBool flg; 4564 MatSolverTypeForSpecifcType inext,iprev = NULL; 4565 4566 PetscFunctionBegin; 4567 ierr = MatInitializePackage();CHKERRQ(ierr); 4568 if (!next) { 4569 ierr = PetscNew(&MatSolverTypeHolders);CHKERRQ(ierr); 4570 ierr = PetscStrallocpy(package,&MatSolverTypeHolders->name);CHKERRQ(ierr); 4571 ierr = PetscNew(&MatSolverTypeHolders->handlers);CHKERRQ(ierr); 4572 ierr = PetscStrallocpy(mtype,(char **)&MatSolverTypeHolders->handlers->mtype);CHKERRQ(ierr); 4573 MatSolverTypeHolders->handlers->createfactor[(int)ftype-1] = createfactor; 4574 PetscFunctionReturn(0); 4575 } 4576 while (next) { 4577 ierr = PetscStrcasecmp(package,next->name,&flg);CHKERRQ(ierr); 4578 if (flg) { 4579 PetscCheckFalse(!next->handlers,PETSC_COMM_SELF,PETSC_ERR_PLIB,"MatSolverTypeHolder is missing handlers"); 4580 inext = next->handlers; 4581 while (inext) { 4582 ierr = PetscStrcasecmp(mtype,inext->mtype,&flg);CHKERRQ(ierr); 4583 if (flg) { 4584 inext->createfactor[(int)ftype-1] = createfactor; 4585 PetscFunctionReturn(0); 4586 } 4587 iprev = inext; 4588 inext = inext->next; 4589 } 4590 ierr = PetscNew(&iprev->next);CHKERRQ(ierr); 4591 ierr = PetscStrallocpy(mtype,(char **)&iprev->next->mtype);CHKERRQ(ierr); 4592 iprev->next->createfactor[(int)ftype-1] = createfactor; 4593 PetscFunctionReturn(0); 4594 } 4595 prev = next; 4596 next = next->next; 4597 } 4598 ierr = PetscNew(&prev->next);CHKERRQ(ierr); 4599 ierr = PetscStrallocpy(package,&prev->next->name);CHKERRQ(ierr); 4600 ierr = PetscNew(&prev->next->handlers);CHKERRQ(ierr); 4601 ierr = PetscStrallocpy(mtype,(char **)&prev->next->handlers->mtype);CHKERRQ(ierr); 4602 prev->next->handlers->createfactor[(int)ftype-1] = createfactor; 4603 PetscFunctionReturn(0); 4604 } 4605 4606 /*@C 4607 MatSolveTypeGet - Gets the function that creates the factor matrix if it exist 4608 4609 Input Parameters: 4610 + type - name of the package, for example petsc or superlu 4611 . ftype - the type of factorization supported by the type 4612 - mtype - the matrix type that works with this type 4613 4614 Output Parameters: 4615 + foundtype - PETSC_TRUE if the type was registered 4616 . foundmtype - PETSC_TRUE if the type supports the requested mtype 4617 - createfactor - routine that will create the factored matrix ready to be used or NULL if not found 4618 4619 Level: intermediate 4620 4621 .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable(), MatSolverTypeRegister(), MatGetFactor() 4622 @*/ 4623 PetscErrorCode MatSolverTypeGet(MatSolverType type,MatType mtype,MatFactorType ftype,PetscBool *foundtype,PetscBool *foundmtype,PetscErrorCode (**createfactor)(Mat,MatFactorType,Mat*)) 4624 { 4625 PetscErrorCode ierr; 4626 MatSolverTypeHolder next = MatSolverTypeHolders; 4627 PetscBool flg; 4628 MatSolverTypeForSpecifcType inext; 4629 4630 PetscFunctionBegin; 4631 if (foundtype) *foundtype = PETSC_FALSE; 4632 if (foundmtype) *foundmtype = PETSC_FALSE; 4633 if (createfactor) *createfactor = NULL; 4634 4635 if (type) { 4636 while (next) { 4637 ierr = PetscStrcasecmp(type,next->name,&flg);CHKERRQ(ierr); 4638 if (flg) { 4639 if (foundtype) *foundtype = PETSC_TRUE; 4640 inext = next->handlers; 4641 while (inext) { 4642 ierr = PetscStrbeginswith(mtype,inext->mtype,&flg);CHKERRQ(ierr); 4643 if (flg) { 4644 if (foundmtype) *foundmtype = PETSC_TRUE; 4645 if (createfactor) *createfactor = inext->createfactor[(int)ftype-1]; 4646 PetscFunctionReturn(0); 4647 } 4648 inext = inext->next; 4649 } 4650 } 4651 next = next->next; 4652 } 4653 } else { 4654 while (next) { 4655 inext = next->handlers; 4656 while (inext) { 4657 ierr = PetscStrcmp(mtype,inext->mtype,&flg);CHKERRQ(ierr); 4658 if (flg && inext->createfactor[(int)ftype-1]) { 4659 if (foundtype) *foundtype = PETSC_TRUE; 4660 if (foundmtype) *foundmtype = PETSC_TRUE; 4661 if (createfactor) *createfactor = inext->createfactor[(int)ftype-1]; 4662 PetscFunctionReturn(0); 4663 } 4664 inext = inext->next; 4665 } 4666 next = next->next; 4667 } 4668 /* try with base classes inext->mtype */ 4669 next = MatSolverTypeHolders; 4670 while (next) { 4671 inext = next->handlers; 4672 while (inext) { 4673 ierr = PetscStrbeginswith(mtype,inext->mtype,&flg);CHKERRQ(ierr); 4674 if (flg && inext->createfactor[(int)ftype-1]) { 4675 if (foundtype) *foundtype = PETSC_TRUE; 4676 if (foundmtype) *foundmtype = PETSC_TRUE; 4677 if (createfactor) *createfactor = inext->createfactor[(int)ftype-1]; 4678 PetscFunctionReturn(0); 4679 } 4680 inext = inext->next; 4681 } 4682 next = next->next; 4683 } 4684 } 4685 PetscFunctionReturn(0); 4686 } 4687 4688 PetscErrorCode MatSolverTypeDestroy(void) 4689 { 4690 PetscErrorCode ierr; 4691 MatSolverTypeHolder next = MatSolverTypeHolders,prev; 4692 MatSolverTypeForSpecifcType inext,iprev; 4693 4694 PetscFunctionBegin; 4695 while (next) { 4696 ierr = PetscFree(next->name);CHKERRQ(ierr); 4697 inext = next->handlers; 4698 while (inext) { 4699 ierr = PetscFree(inext->mtype);CHKERRQ(ierr); 4700 iprev = inext; 4701 inext = inext->next; 4702 ierr = PetscFree(iprev);CHKERRQ(ierr); 4703 } 4704 prev = next; 4705 next = next->next; 4706 ierr = PetscFree(prev);CHKERRQ(ierr); 4707 } 4708 MatSolverTypeHolders = NULL; 4709 PetscFunctionReturn(0); 4710 } 4711 4712 /*@C 4713 MatFactorGetCanUseOrdering - Indicates if the factorization can use the ordering provided in MatLUFactorSymbolic(), MatCholeskyFactorSymbolic() 4714 4715 Logically Collective on Mat 4716 4717 Input Parameters: 4718 . mat - the matrix 4719 4720 Output Parameters: 4721 . flg - PETSC_TRUE if uses the ordering 4722 4723 Notes: 4724 Most internal PETSc factorizations use the ordering passed to the factorization routine but external 4725 packages do not, thus we want to skip generating the ordering when it is not needed or used. 4726 4727 Level: developer 4728 4729 .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable(), MatGetFactor(), MatLUFactorSymbolic(), MatCholeskyFactorSymbolic() 4730 @*/ 4731 PetscErrorCode MatFactorGetCanUseOrdering(Mat mat, PetscBool *flg) 4732 { 4733 PetscFunctionBegin; 4734 *flg = mat->canuseordering; 4735 PetscFunctionReturn(0); 4736 } 4737 4738 /*@C 4739 MatFactorGetPreferredOrdering - The preferred ordering for a particular matrix factor object 4740 4741 Logically Collective on Mat 4742 4743 Input Parameters: 4744 . mat - the matrix 4745 4746 Output Parameters: 4747 . otype - the preferred type 4748 4749 Level: developer 4750 4751 .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable(), MatGetFactor(), MatLUFactorSymbolic(), MatCholeskyFactorSymbolic() 4752 @*/ 4753 PetscErrorCode MatFactorGetPreferredOrdering(Mat mat, MatFactorType ftype, MatOrderingType *otype) 4754 { 4755 PetscFunctionBegin; 4756 *otype = mat->preferredordering[ftype]; 4757 PetscCheckFalse(!*otype,PETSC_COMM_SELF,PETSC_ERR_PLIB,"MatFactor did not have a preferred ordering"); 4758 PetscFunctionReturn(0); 4759 } 4760 4761 /*@C 4762 MatGetFactor - Returns a matrix suitable to calls to MatXXFactorSymbolic() 4763 4764 Collective on Mat 4765 4766 Input Parameters: 4767 + mat - the matrix 4768 . type - name of solver type, for example, superlu, petsc (to use PETSc's default) 4769 - ftype - factor type, MAT_FACTOR_LU, MAT_FACTOR_CHOLESKY, MAT_FACTOR_ICC, MAT_FACTOR_ILU, 4770 4771 Output Parameters: 4772 . f - the factor matrix used with MatXXFactorSymbolic() calls 4773 4774 Notes: 4775 Some PETSc matrix formats have alternative solvers available that are contained in alternative packages 4776 such as pastix, superlu, mumps etc. 4777 4778 PETSc must have been ./configure to use the external solver, using the option --download-package 4779 4780 Developer Notes: 4781 This should actually be called MatCreateFactor() since it creates a new factor object 4782 4783 Level: intermediate 4784 4785 .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable(), MatFactorGetCanUseOrdering(), MatSolverTypeRegister() 4786 @*/ 4787 PetscErrorCode MatGetFactor(Mat mat, MatSolverType type,MatFactorType ftype,Mat *f) 4788 { 4789 PetscErrorCode ierr,(*conv)(Mat,MatFactorType,Mat*); 4790 PetscBool foundtype,foundmtype; 4791 4792 PetscFunctionBegin; 4793 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4794 PetscValidType(mat,1); 4795 4796 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4797 MatCheckPreallocated(mat,1); 4798 4799 ierr = MatSolverTypeGet(type,((PetscObject)mat)->type_name,ftype,&foundtype,&foundmtype,&conv);CHKERRQ(ierr); 4800 if (!foundtype) { 4801 if (type) { 4802 SETERRQ(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); 4803 } else { 4804 SETERRQ(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); 4805 } 4806 } 4807 PetscCheckFalse(!foundmtype,PetscObjectComm((PetscObject)mat),PETSC_ERR_MISSING_FACTOR,"MatSolverType %s does not support matrix type %s",type,((PetscObject)mat)->type_name); 4808 PetscCheckFalse(!conv,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); 4809 4810 ierr = (*conv)(mat,ftype,f);CHKERRQ(ierr); 4811 PetscFunctionReturn(0); 4812 } 4813 4814 /*@C 4815 MatGetFactorAvailable - Returns a a flag if matrix supports particular type and factor type 4816 4817 Not Collective 4818 4819 Input Parameters: 4820 + mat - the matrix 4821 . type - name of solver type, for example, superlu, petsc (to use PETSc's default) 4822 - ftype - factor type, MAT_FACTOR_LU, MAT_FACTOR_CHOLESKY, MAT_FACTOR_ICC, MAT_FACTOR_ILU, 4823 4824 Output Parameter: 4825 . flg - PETSC_TRUE if the factorization is available 4826 4827 Notes: 4828 Some PETSc matrix formats have alternative solvers available that are contained in alternative packages 4829 such as pastix, superlu, mumps etc. 4830 4831 PETSc must have been ./configure to use the external solver, using the option --download-package 4832 4833 Developer Notes: 4834 This should actually be called MatCreateFactorAvailable() since MatGetFactor() creates a new factor object 4835 4836 Level: intermediate 4837 4838 .seealso: MatCopy(), MatDuplicate(), MatGetFactor(), MatSolverTypeRegister() 4839 @*/ 4840 PetscErrorCode MatGetFactorAvailable(Mat mat, MatSolverType type,MatFactorType ftype,PetscBool *flg) 4841 { 4842 PetscErrorCode ierr, (*gconv)(Mat,MatFactorType,Mat*); 4843 4844 PetscFunctionBegin; 4845 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4846 PetscValidType(mat,1); 4847 4848 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4849 MatCheckPreallocated(mat,1); 4850 4851 *flg = PETSC_FALSE; 4852 ierr = MatSolverTypeGet(type,((PetscObject)mat)->type_name,ftype,NULL,NULL,&gconv);CHKERRQ(ierr); 4853 if (gconv) { 4854 *flg = PETSC_TRUE; 4855 } 4856 PetscFunctionReturn(0); 4857 } 4858 4859 /*@ 4860 MatDuplicate - Duplicates a matrix including the non-zero structure. 4861 4862 Collective on Mat 4863 4864 Input Parameters: 4865 + mat - the matrix 4866 - op - One of MAT_DO_NOT_COPY_VALUES, MAT_COPY_VALUES, or MAT_SHARE_NONZERO_PATTERN. 4867 See the manual page for MatDuplicateOption for an explanation of these options. 4868 4869 Output Parameter: 4870 . M - pointer to place new matrix 4871 4872 Level: intermediate 4873 4874 Notes: 4875 You cannot change the nonzero pattern for the parent or child matrix if you use MAT_SHARE_NONZERO_PATTERN. 4876 May be called with an unassembled input Mat if MAT_DO_NOT_COPY_VALUES is used, in which case the output Mat is unassembled as well. 4877 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. 4878 4879 .seealso: MatCopy(), MatConvert(), MatDuplicateOption 4880 @*/ 4881 PetscErrorCode MatDuplicate(Mat mat,MatDuplicateOption op,Mat *M) 4882 { 4883 PetscErrorCode ierr; 4884 Mat B; 4885 VecType vtype; 4886 PetscInt i; 4887 PetscObject dm; 4888 void (*viewf)(void); 4889 4890 PetscFunctionBegin; 4891 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4892 PetscValidType(mat,1); 4893 PetscValidPointer(M,3); 4894 PetscCheckFalse(op == MAT_COPY_VALUES && !mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"MAT_COPY_VALUES not allowed for unassembled matrix"); 4895 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4896 MatCheckPreallocated(mat,1); 4897 4898 *M = NULL; 4899 PetscCheckFalse(!mat->ops->duplicate,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Not written for matrix type %s",((PetscObject)mat)->type_name); 4900 ierr = PetscLogEventBegin(MAT_Convert,mat,0,0,0);CHKERRQ(ierr); 4901 ierr = (*mat->ops->duplicate)(mat,op,M);CHKERRQ(ierr); 4902 ierr = PetscLogEventEnd(MAT_Convert,mat,0,0,0);CHKERRQ(ierr); 4903 B = *M; 4904 4905 ierr = MatGetOperation(mat,MATOP_VIEW,&viewf);CHKERRQ(ierr); 4906 if (viewf) { 4907 ierr = MatSetOperation(B,MATOP_VIEW,viewf);CHKERRQ(ierr); 4908 } 4909 ierr = MatGetVecType(mat,&vtype);CHKERRQ(ierr); 4910 ierr = MatSetVecType(B,vtype);CHKERRQ(ierr); 4911 4912 B->stencil.dim = mat->stencil.dim; 4913 B->stencil.noc = mat->stencil.noc; 4914 for (i=0; i<=mat->stencil.dim; i++) { 4915 B->stencil.dims[i] = mat->stencil.dims[i]; 4916 B->stencil.starts[i] = mat->stencil.starts[i]; 4917 } 4918 4919 B->nooffproczerorows = mat->nooffproczerorows; 4920 B->nooffprocentries = mat->nooffprocentries; 4921 4922 ierr = PetscObjectQuery((PetscObject) mat, "__PETSc_dm", &dm);CHKERRQ(ierr); 4923 if (dm) { 4924 ierr = PetscObjectCompose((PetscObject) B, "__PETSc_dm", dm);CHKERRQ(ierr); 4925 } 4926 ierr = PetscObjectStateIncrease((PetscObject)B);CHKERRQ(ierr); 4927 PetscFunctionReturn(0); 4928 } 4929 4930 /*@ 4931 MatGetDiagonal - Gets the diagonal of a matrix. 4932 4933 Logically Collective on Mat 4934 4935 Input Parameters: 4936 + mat - the matrix 4937 - v - the vector for storing the diagonal 4938 4939 Output Parameter: 4940 . v - the diagonal of the matrix 4941 4942 Level: intermediate 4943 4944 Note: 4945 Currently only correct in parallel for square matrices. 4946 4947 .seealso: MatGetRow(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMaxAbs() 4948 @*/ 4949 PetscErrorCode MatGetDiagonal(Mat mat,Vec v) 4950 { 4951 PetscErrorCode ierr; 4952 4953 PetscFunctionBegin; 4954 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4955 PetscValidType(mat,1); 4956 PetscValidHeaderSpecific(v,VEC_CLASSID,2); 4957 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4958 PetscCheckFalse(!mat->ops->getdiagonal,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 4959 MatCheckPreallocated(mat,1); 4960 4961 ierr = (*mat->ops->getdiagonal)(mat,v);CHKERRQ(ierr); 4962 ierr = PetscObjectStateIncrease((PetscObject)v);CHKERRQ(ierr); 4963 PetscFunctionReturn(0); 4964 } 4965 4966 /*@C 4967 MatGetRowMin - Gets the minimum value (of the real part) of each 4968 row of the matrix 4969 4970 Logically Collective on Mat 4971 4972 Input Parameter: 4973 . mat - the matrix 4974 4975 Output Parameters: 4976 + v - the vector for storing the maximums 4977 - idx - the indices of the column found for each row (optional) 4978 4979 Level: intermediate 4980 4981 Notes: 4982 The result of this call are the same as if one converted the matrix to dense format 4983 and found the minimum value in each row (i.e. the implicit zeros are counted as zeros). 4984 4985 This code is only implemented for a couple of matrix formats. 4986 4987 .seealso: MatGetDiagonal(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMaxAbs(), 4988 MatGetRowMax() 4989 @*/ 4990 PetscErrorCode MatGetRowMin(Mat mat,Vec v,PetscInt idx[]) 4991 { 4992 PetscErrorCode ierr; 4993 4994 PetscFunctionBegin; 4995 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4996 PetscValidType(mat,1); 4997 PetscValidHeaderSpecific(v,VEC_CLASSID,2); 4998 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4999 5000 if (!mat->cmap->N) { 5001 ierr = VecSet(v,PETSC_MAX_REAL);CHKERRQ(ierr); 5002 if (idx) { 5003 PetscInt i,m = mat->rmap->n; 5004 for (i=0; i<m; i++) idx[i] = -1; 5005 } 5006 } else { 5007 PetscCheckFalse(!mat->ops->getrowmin,PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 5008 MatCheckPreallocated(mat,1); 5009 } 5010 ierr = (*mat->ops->getrowmin)(mat,v,idx);CHKERRQ(ierr); 5011 ierr = PetscObjectStateIncrease((PetscObject)v);CHKERRQ(ierr); 5012 PetscFunctionReturn(0); 5013 } 5014 5015 /*@C 5016 MatGetRowMinAbs - Gets the minimum value (in absolute value) of each 5017 row of the matrix 5018 5019 Logically Collective on Mat 5020 5021 Input Parameter: 5022 . mat - the matrix 5023 5024 Output Parameters: 5025 + v - the vector for storing the minimums 5026 - idx - the indices of the column found for each row (or NULL if not needed) 5027 5028 Level: intermediate 5029 5030 Notes: 5031 if a row is completely empty or has only 0.0 values then the idx[] value for that 5032 row is 0 (the first column). 5033 5034 This code is only implemented for a couple of matrix formats. 5035 5036 .seealso: MatGetDiagonal(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMax(), MatGetRowMaxAbs(), MatGetRowMin() 5037 @*/ 5038 PetscErrorCode MatGetRowMinAbs(Mat mat,Vec v,PetscInt idx[]) 5039 { 5040 PetscErrorCode ierr; 5041 5042 PetscFunctionBegin; 5043 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5044 PetscValidType(mat,1); 5045 PetscValidHeaderSpecific(v,VEC_CLASSID,2); 5046 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5047 PetscCheckFalse(mat->factortype,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5048 5049 if (!mat->cmap->N) { 5050 ierr = VecSet(v,0.0);CHKERRQ(ierr); 5051 if (idx) { 5052 PetscInt i,m = mat->rmap->n; 5053 for (i=0; i<m; i++) idx[i] = -1; 5054 } 5055 } else { 5056 PetscCheckFalse(!mat->ops->getrowminabs,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 5057 MatCheckPreallocated(mat,1); 5058 if (idx) {ierr = PetscArrayzero(idx,mat->rmap->n);CHKERRQ(ierr);} 5059 ierr = (*mat->ops->getrowminabs)(mat,v,idx);CHKERRQ(ierr); 5060 } 5061 ierr = PetscObjectStateIncrease((PetscObject)v);CHKERRQ(ierr); 5062 PetscFunctionReturn(0); 5063 } 5064 5065 /*@C 5066 MatGetRowMax - Gets the maximum value (of the real part) of each 5067 row of the matrix 5068 5069 Logically Collective on Mat 5070 5071 Input Parameter: 5072 . mat - the matrix 5073 5074 Output Parameters: 5075 + v - the vector for storing the maximums 5076 - idx - the indices of the column found for each row (optional) 5077 5078 Level: intermediate 5079 5080 Notes: 5081 The result of this call are the same as if one converted the matrix to dense format 5082 and found the minimum value in each row (i.e. the implicit zeros are counted as zeros). 5083 5084 This code is only implemented for a couple of matrix formats. 5085 5086 .seealso: MatGetDiagonal(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMaxAbs(), MatGetRowMin() 5087 @*/ 5088 PetscErrorCode MatGetRowMax(Mat mat,Vec v,PetscInt idx[]) 5089 { 5090 PetscErrorCode ierr; 5091 5092 PetscFunctionBegin; 5093 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5094 PetscValidType(mat,1); 5095 PetscValidHeaderSpecific(v,VEC_CLASSID,2); 5096 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5097 5098 if (!mat->cmap->N) { 5099 ierr = VecSet(v,PETSC_MIN_REAL);CHKERRQ(ierr); 5100 if (idx) { 5101 PetscInt i,m = mat->rmap->n; 5102 for (i=0; i<m; i++) idx[i] = -1; 5103 } 5104 } else { 5105 PetscCheckFalse(!mat->ops->getrowmax,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 5106 MatCheckPreallocated(mat,1); 5107 ierr = (*mat->ops->getrowmax)(mat,v,idx);CHKERRQ(ierr); 5108 } 5109 ierr = PetscObjectStateIncrease((PetscObject)v);CHKERRQ(ierr); 5110 PetscFunctionReturn(0); 5111 } 5112 5113 /*@C 5114 MatGetRowMaxAbs - Gets the maximum value (in absolute value) of each 5115 row of the matrix 5116 5117 Logically Collective on Mat 5118 5119 Input Parameter: 5120 . mat - the matrix 5121 5122 Output Parameters: 5123 + v - the vector for storing the maximums 5124 - idx - the indices of the column found for each row (or NULL if not needed) 5125 5126 Level: intermediate 5127 5128 Notes: 5129 if a row is completely empty or has only 0.0 values then the idx[] value for that 5130 row is 0 (the first column). 5131 5132 This code is only implemented for a couple of matrix formats. 5133 5134 .seealso: MatGetDiagonal(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMax(), MatGetRowMin() 5135 @*/ 5136 PetscErrorCode MatGetRowMaxAbs(Mat mat,Vec v,PetscInt idx[]) 5137 { 5138 PetscErrorCode ierr; 5139 5140 PetscFunctionBegin; 5141 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5142 PetscValidType(mat,1); 5143 PetscValidHeaderSpecific(v,VEC_CLASSID,2); 5144 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5145 5146 if (!mat->cmap->N) { 5147 ierr = VecSet(v,0.0);CHKERRQ(ierr); 5148 if (idx) { 5149 PetscInt i,m = mat->rmap->n; 5150 for (i=0; i<m; i++) idx[i] = -1; 5151 } 5152 } else { 5153 PetscCheckFalse(!mat->ops->getrowmaxabs,PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 5154 MatCheckPreallocated(mat,1); 5155 if (idx) {ierr = PetscArrayzero(idx,mat->rmap->n);CHKERRQ(ierr);} 5156 ierr = (*mat->ops->getrowmaxabs)(mat,v,idx);CHKERRQ(ierr); 5157 } 5158 ierr = PetscObjectStateIncrease((PetscObject)v);CHKERRQ(ierr); 5159 PetscFunctionReturn(0); 5160 } 5161 5162 /*@ 5163 MatGetRowSum - Gets the sum of each row of the matrix 5164 5165 Logically or Neighborhood Collective on Mat 5166 5167 Input Parameters: 5168 . mat - the matrix 5169 5170 Output Parameter: 5171 . v - the vector for storing the sum of rows 5172 5173 Level: intermediate 5174 5175 Notes: 5176 This code is slow since it is not currently specialized for different formats 5177 5178 .seealso: MatGetDiagonal(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMax(), MatGetRowMin() 5179 @*/ 5180 PetscErrorCode MatGetRowSum(Mat mat, Vec v) 5181 { 5182 Vec ones; 5183 PetscErrorCode ierr; 5184 5185 PetscFunctionBegin; 5186 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5187 PetscValidType(mat,1); 5188 PetscValidHeaderSpecific(v,VEC_CLASSID,2); 5189 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5190 MatCheckPreallocated(mat,1); 5191 ierr = MatCreateVecs(mat,&ones,NULL);CHKERRQ(ierr); 5192 ierr = VecSet(ones,1.);CHKERRQ(ierr); 5193 ierr = MatMult(mat,ones,v);CHKERRQ(ierr); 5194 ierr = VecDestroy(&ones);CHKERRQ(ierr); 5195 PetscFunctionReturn(0); 5196 } 5197 5198 /*@ 5199 MatTranspose - Computes an in-place or out-of-place transpose of a matrix. 5200 5201 Collective on Mat 5202 5203 Input Parameters: 5204 + mat - the matrix to transpose 5205 - reuse - either MAT_INITIAL_MATRIX, MAT_REUSE_MATRIX, or MAT_INPLACE_MATRIX 5206 5207 Output Parameter: 5208 . B - the transpose 5209 5210 Notes: 5211 If you use MAT_INPLACE_MATRIX then you must pass in &mat for B 5212 5213 MAT_REUSE_MATRIX causes the B matrix from a previous call to this function with MAT_INITIAL_MATRIX to be used 5214 5215 Consider using MatCreateTranspose() instead if you only need a matrix that behaves like the transpose, but don't need the storage to be changed. 5216 5217 Level: intermediate 5218 5219 .seealso: MatMultTranspose(), MatMultTransposeAdd(), MatIsTranspose(), MatReuse 5220 @*/ 5221 PetscErrorCode MatTranspose(Mat mat,MatReuse reuse,Mat *B) 5222 { 5223 PetscErrorCode ierr; 5224 5225 PetscFunctionBegin; 5226 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5227 PetscValidType(mat,1); 5228 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5229 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5230 PetscCheckFalse(!mat->ops->transpose,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 5231 PetscCheckFalse(reuse == MAT_INPLACE_MATRIX && mat != *B,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"MAT_INPLACE_MATRIX requires last matrix to match first"); 5232 PetscCheckFalse(reuse == MAT_REUSE_MATRIX && mat == *B,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Perhaps you mean MAT_INPLACE_MATRIX"); 5233 MatCheckPreallocated(mat,1); 5234 5235 ierr = PetscLogEventBegin(MAT_Transpose,mat,0,0,0);CHKERRQ(ierr); 5236 ierr = (*mat->ops->transpose)(mat,reuse,B);CHKERRQ(ierr); 5237 ierr = PetscLogEventEnd(MAT_Transpose,mat,0,0,0);CHKERRQ(ierr); 5238 if (B) {ierr = PetscObjectStateIncrease((PetscObject)*B);CHKERRQ(ierr);} 5239 PetscFunctionReturn(0); 5240 } 5241 5242 /*@ 5243 MatIsTranspose - Test whether a matrix is another one's transpose, 5244 or its own, in which case it tests symmetry. 5245 5246 Collective on Mat 5247 5248 Input Parameters: 5249 + A - the matrix to test 5250 - B - the matrix to test against, this can equal the first parameter 5251 5252 Output Parameters: 5253 . flg - the result 5254 5255 Notes: 5256 Only available for SeqAIJ/MPIAIJ matrices. The sequential algorithm 5257 has a running time of the order of the number of nonzeros; the parallel 5258 test involves parallel copies of the block-offdiagonal parts of the matrix. 5259 5260 Level: intermediate 5261 5262 .seealso: MatTranspose(), MatIsSymmetric(), MatIsHermitian() 5263 @*/ 5264 PetscErrorCode MatIsTranspose(Mat A,Mat B,PetscReal tol,PetscBool *flg) 5265 { 5266 PetscErrorCode ierr,(*f)(Mat,Mat,PetscReal,PetscBool*),(*g)(Mat,Mat,PetscReal,PetscBool*); 5267 5268 PetscFunctionBegin; 5269 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 5270 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 5271 PetscValidBoolPointer(flg,4); 5272 ierr = PetscObjectQueryFunction((PetscObject)A,"MatIsTranspose_C",&f);CHKERRQ(ierr); 5273 ierr = PetscObjectQueryFunction((PetscObject)B,"MatIsTranspose_C",&g);CHKERRQ(ierr); 5274 *flg = PETSC_FALSE; 5275 if (f && g) { 5276 if (f == g) { 5277 ierr = (*f)(A,B,tol,flg);CHKERRQ(ierr); 5278 } else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_NOTSAMETYPE,"Matrices do not have the same comparator for symmetry test"); 5279 } else { 5280 MatType mattype; 5281 if (!f) { 5282 ierr = MatGetType(A,&mattype);CHKERRQ(ierr); 5283 } else { 5284 ierr = MatGetType(B,&mattype);CHKERRQ(ierr); 5285 } 5286 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type %s does not support checking for transpose",mattype); 5287 } 5288 PetscFunctionReturn(0); 5289 } 5290 5291 /*@ 5292 MatHermitianTranspose - Computes an in-place or out-of-place transpose of a matrix in complex conjugate. 5293 5294 Collective on Mat 5295 5296 Input Parameters: 5297 + mat - the matrix to transpose and complex conjugate 5298 - reuse - either MAT_INITIAL_MATRIX, MAT_REUSE_MATRIX, or MAT_INPLACE_MATRIX 5299 5300 Output Parameter: 5301 . B - the Hermitian 5302 5303 Level: intermediate 5304 5305 .seealso: MatTranspose(), MatMultTranspose(), MatMultTransposeAdd(), MatIsTranspose(), MatReuse 5306 @*/ 5307 PetscErrorCode MatHermitianTranspose(Mat mat,MatReuse reuse,Mat *B) 5308 { 5309 PetscErrorCode ierr; 5310 5311 PetscFunctionBegin; 5312 ierr = MatTranspose(mat,reuse,B);CHKERRQ(ierr); 5313 #if defined(PETSC_USE_COMPLEX) 5314 ierr = MatConjugate(*B);CHKERRQ(ierr); 5315 #endif 5316 PetscFunctionReturn(0); 5317 } 5318 5319 /*@ 5320 MatIsHermitianTranspose - Test whether a matrix is another one's Hermitian transpose, 5321 5322 Collective on Mat 5323 5324 Input Parameters: 5325 + A - the matrix to test 5326 - B - the matrix to test against, this can equal the first parameter 5327 5328 Output Parameters: 5329 . flg - the result 5330 5331 Notes: 5332 Only available for SeqAIJ/MPIAIJ matrices. The sequential algorithm 5333 has a running time of the order of the number of nonzeros; the parallel 5334 test involves parallel copies of the block-offdiagonal parts of the matrix. 5335 5336 Level: intermediate 5337 5338 .seealso: MatTranspose(), MatIsSymmetric(), MatIsHermitian(), MatIsTranspose() 5339 @*/ 5340 PetscErrorCode MatIsHermitianTranspose(Mat A,Mat B,PetscReal tol,PetscBool *flg) 5341 { 5342 PetscErrorCode ierr,(*f)(Mat,Mat,PetscReal,PetscBool*),(*g)(Mat,Mat,PetscReal,PetscBool*); 5343 5344 PetscFunctionBegin; 5345 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 5346 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 5347 PetscValidBoolPointer(flg,4); 5348 ierr = PetscObjectQueryFunction((PetscObject)A,"MatIsHermitianTranspose_C",&f);CHKERRQ(ierr); 5349 ierr = PetscObjectQueryFunction((PetscObject)B,"MatIsHermitianTranspose_C",&g);CHKERRQ(ierr); 5350 if (f && g) { 5351 if (f==g) { 5352 ierr = (*f)(A,B,tol,flg);CHKERRQ(ierr); 5353 } else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_NOTSAMETYPE,"Matrices do not have the same comparator for Hermitian test"); 5354 } 5355 PetscFunctionReturn(0); 5356 } 5357 5358 /*@ 5359 MatPermute - Creates a new matrix with rows and columns permuted from the 5360 original. 5361 5362 Collective on Mat 5363 5364 Input Parameters: 5365 + mat - the matrix to permute 5366 . row - row permutation, each processor supplies only the permutation for its rows 5367 - col - column permutation, each processor supplies only the permutation for its columns 5368 5369 Output Parameters: 5370 . B - the permuted matrix 5371 5372 Level: advanced 5373 5374 Note: 5375 The index sets map from row/col of permuted matrix to row/col of original matrix. 5376 The index sets should be on the same communicator as Mat and have the same local sizes. 5377 5378 Developer Note: 5379 If you want to implement MatPermute for a matrix type, and your approach doesn't 5380 exploit the fact that row and col are permutations, consider implementing the 5381 more general MatCreateSubMatrix() instead. 5382 5383 .seealso: MatGetOrdering(), ISAllGather() 5384 5385 @*/ 5386 PetscErrorCode MatPermute(Mat mat,IS row,IS col,Mat *B) 5387 { 5388 PetscErrorCode ierr; 5389 5390 PetscFunctionBegin; 5391 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5392 PetscValidType(mat,1); 5393 PetscValidHeaderSpecific(row,IS_CLASSID,2); 5394 PetscValidHeaderSpecific(col,IS_CLASSID,3); 5395 PetscValidPointer(B,4); 5396 PetscCheckSameComm(mat,1,row,2); 5397 if (row != col) PetscCheckSameComm(row,2,col,3); 5398 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5399 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5400 PetscCheckFalse(!mat->ops->permute && !mat->ops->createsubmatrix,PETSC_COMM_SELF,PETSC_ERR_SUP,"MatPermute not available for Mat type %s",((PetscObject)mat)->type_name); 5401 MatCheckPreallocated(mat,1); 5402 5403 if (mat->ops->permute) { 5404 ierr = (*mat->ops->permute)(mat,row,col,B);CHKERRQ(ierr); 5405 ierr = PetscObjectStateIncrease((PetscObject)*B);CHKERRQ(ierr); 5406 } else { 5407 ierr = MatCreateSubMatrix(mat, row, col, MAT_INITIAL_MATRIX, B);CHKERRQ(ierr); 5408 } 5409 PetscFunctionReturn(0); 5410 } 5411 5412 /*@ 5413 MatEqual - Compares two matrices. 5414 5415 Collective on Mat 5416 5417 Input Parameters: 5418 + A - the first matrix 5419 - B - the second matrix 5420 5421 Output Parameter: 5422 . flg - PETSC_TRUE if the matrices are equal; PETSC_FALSE otherwise. 5423 5424 Level: intermediate 5425 5426 @*/ 5427 PetscErrorCode MatEqual(Mat A,Mat B,PetscBool *flg) 5428 { 5429 PetscErrorCode ierr; 5430 5431 PetscFunctionBegin; 5432 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 5433 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 5434 PetscValidType(A,1); 5435 PetscValidType(B,2); 5436 PetscValidBoolPointer(flg,3); 5437 PetscCheckSameComm(A,1,B,2); 5438 MatCheckPreallocated(A,1); 5439 MatCheckPreallocated(B,2); 5440 PetscCheckFalse(!A->assembled,PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5441 PetscCheckFalse(!B->assembled,PetscObjectComm((PetscObject)B),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5442 PetscCheckFalse(A->rmap->N != B->rmap->N || A->cmap->N != B->cmap->N,PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Mat A,Mat B: global dim %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT,A->rmap->N,B->rmap->N,A->cmap->N,B->cmap->N); 5443 if (A->ops->equal && A->ops->equal == B->ops->equal) { 5444 ierr = (*A->ops->equal)(A,B,flg);CHKERRQ(ierr); 5445 } else { 5446 ierr = MatMultEqual(A,B,10,flg);CHKERRQ(ierr); 5447 } 5448 PetscFunctionReturn(0); 5449 } 5450 5451 /*@ 5452 MatDiagonalScale - Scales a matrix on the left and right by diagonal 5453 matrices that are stored as vectors. Either of the two scaling 5454 matrices can be NULL. 5455 5456 Collective on Mat 5457 5458 Input Parameters: 5459 + mat - the matrix to be scaled 5460 . l - the left scaling vector (or NULL) 5461 - r - the right scaling vector (or NULL) 5462 5463 Notes: 5464 MatDiagonalScale() computes A = LAR, where 5465 L = a diagonal matrix (stored as a vector), R = a diagonal matrix (stored as a vector) 5466 The L scales the rows of the matrix, the R scales the columns of the matrix. 5467 5468 Level: intermediate 5469 5470 .seealso: MatScale(), MatShift(), MatDiagonalSet() 5471 @*/ 5472 PetscErrorCode MatDiagonalScale(Mat mat,Vec l,Vec r) 5473 { 5474 PetscErrorCode ierr; 5475 5476 PetscFunctionBegin; 5477 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5478 PetscValidType(mat,1); 5479 if (l) {PetscValidHeaderSpecific(l,VEC_CLASSID,2);PetscCheckSameComm(mat,1,l,2);} 5480 if (r) {PetscValidHeaderSpecific(r,VEC_CLASSID,3);PetscCheckSameComm(mat,1,r,3);} 5481 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5482 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5483 MatCheckPreallocated(mat,1); 5484 if (!l && !r) PetscFunctionReturn(0); 5485 5486 PetscCheckFalse(!mat->ops->diagonalscale,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 5487 ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 5488 ierr = (*mat->ops->diagonalscale)(mat,l,r);CHKERRQ(ierr); 5489 ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 5490 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 5491 if (l != r && mat->symmetric) mat->symmetric = PETSC_FALSE; 5492 PetscFunctionReturn(0); 5493 } 5494 5495 /*@ 5496 MatScale - Scales all elements of a matrix by a given number. 5497 5498 Logically Collective on Mat 5499 5500 Input Parameters: 5501 + mat - the matrix to be scaled 5502 - a - the scaling value 5503 5504 Output Parameter: 5505 . mat - the scaled matrix 5506 5507 Level: intermediate 5508 5509 .seealso: MatDiagonalScale() 5510 @*/ 5511 PetscErrorCode MatScale(Mat mat,PetscScalar a) 5512 { 5513 PetscErrorCode ierr; 5514 5515 PetscFunctionBegin; 5516 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5517 PetscValidType(mat,1); 5518 PetscCheckFalse(a != (PetscScalar)1.0 && !mat->ops->scale,PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 5519 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5520 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5521 PetscValidLogicalCollectiveScalar(mat,a,2); 5522 MatCheckPreallocated(mat,1); 5523 5524 ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 5525 if (a != (PetscScalar)1.0) { 5526 ierr = (*mat->ops->scale)(mat,a);CHKERRQ(ierr); 5527 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 5528 } 5529 ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 5530 PetscFunctionReturn(0); 5531 } 5532 5533 /*@ 5534 MatNorm - Calculates various norms of a matrix. 5535 5536 Collective on Mat 5537 5538 Input Parameters: 5539 + mat - the matrix 5540 - type - the type of norm, NORM_1, NORM_FROBENIUS, NORM_INFINITY 5541 5542 Output Parameter: 5543 . nrm - the resulting norm 5544 5545 Level: intermediate 5546 5547 @*/ 5548 PetscErrorCode MatNorm(Mat mat,NormType type,PetscReal *nrm) 5549 { 5550 PetscErrorCode ierr; 5551 5552 PetscFunctionBegin; 5553 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5554 PetscValidType(mat,1); 5555 PetscValidRealPointer(nrm,3); 5556 5557 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5558 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5559 PetscCheckFalse(!mat->ops->norm,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 5560 MatCheckPreallocated(mat,1); 5561 5562 ierr = (*mat->ops->norm)(mat,type,nrm);CHKERRQ(ierr); 5563 PetscFunctionReturn(0); 5564 } 5565 5566 /* 5567 This variable is used to prevent counting of MatAssemblyBegin() that 5568 are called from within a MatAssemblyEnd(). 5569 */ 5570 static PetscInt MatAssemblyEnd_InUse = 0; 5571 /*@ 5572 MatAssemblyBegin - Begins assembling the matrix. This routine should 5573 be called after completing all calls to MatSetValues(). 5574 5575 Collective on Mat 5576 5577 Input Parameters: 5578 + mat - the matrix 5579 - type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY 5580 5581 Notes: 5582 MatSetValues() generally caches the values. The matrix is ready to 5583 use only after MatAssemblyBegin() and MatAssemblyEnd() have been called. 5584 Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES 5585 in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before 5586 using the matrix. 5587 5588 ALL processes that share a matrix MUST call MatAssemblyBegin() and MatAssemblyEnd() the SAME NUMBER of times, and each time with the 5589 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 5590 a global collective operation requring all processes that share the matrix. 5591 5592 Space for preallocated nonzeros that is not filled by a call to MatSetValues() or a related routine are compressed 5593 out by assembly. If you intend to use that extra space on a subsequent assembly, be sure to insert explicit zeros 5594 before MAT_FINAL_ASSEMBLY so the space is not compressed out. 5595 5596 Level: beginner 5597 5598 .seealso: MatAssemblyEnd(), MatSetValues(), MatAssembled() 5599 @*/ 5600 PetscErrorCode MatAssemblyBegin(Mat mat,MatAssemblyType type) 5601 { 5602 PetscErrorCode ierr; 5603 5604 PetscFunctionBegin; 5605 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5606 PetscValidType(mat,1); 5607 MatCheckPreallocated(mat,1); 5608 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix.\nDid you forget to call MatSetUnfactored()?"); 5609 if (mat->assembled) { 5610 mat->was_assembled = PETSC_TRUE; 5611 mat->assembled = PETSC_FALSE; 5612 } 5613 5614 if (!MatAssemblyEnd_InUse) { 5615 ierr = PetscLogEventBegin(MAT_AssemblyBegin,mat,0,0,0);CHKERRQ(ierr); 5616 if (mat->ops->assemblybegin) {ierr = (*mat->ops->assemblybegin)(mat,type);CHKERRQ(ierr);} 5617 ierr = PetscLogEventEnd(MAT_AssemblyBegin,mat,0,0,0);CHKERRQ(ierr); 5618 } else if (mat->ops->assemblybegin) { 5619 ierr = (*mat->ops->assemblybegin)(mat,type);CHKERRQ(ierr); 5620 } 5621 PetscFunctionReturn(0); 5622 } 5623 5624 /*@ 5625 MatAssembled - Indicates if a matrix has been assembled and is ready for 5626 use; for example, in matrix-vector product. 5627 5628 Not Collective 5629 5630 Input Parameter: 5631 . mat - the matrix 5632 5633 Output Parameter: 5634 . assembled - PETSC_TRUE or PETSC_FALSE 5635 5636 Level: advanced 5637 5638 .seealso: MatAssemblyEnd(), MatSetValues(), MatAssemblyBegin() 5639 @*/ 5640 PetscErrorCode MatAssembled(Mat mat,PetscBool *assembled) 5641 { 5642 PetscFunctionBegin; 5643 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5644 PetscValidPointer(assembled,2); 5645 *assembled = mat->assembled; 5646 PetscFunctionReturn(0); 5647 } 5648 5649 /*@ 5650 MatAssemblyEnd - Completes assembling the matrix. This routine should 5651 be called after MatAssemblyBegin(). 5652 5653 Collective on Mat 5654 5655 Input Parameters: 5656 + mat - the matrix 5657 - type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY 5658 5659 Options Database Keys: 5660 + -mat_view ::ascii_info - Prints info on matrix at conclusion of MatEndAssembly() 5661 . -mat_view ::ascii_info_detail - Prints more detailed info 5662 . -mat_view - Prints matrix in ASCII format 5663 . -mat_view ::ascii_matlab - Prints matrix in Matlab format 5664 . -mat_view draw - PetscDraws nonzero structure of matrix, using MatView() and PetscDrawOpenX(). 5665 . -display <name> - Sets display name (default is host) 5666 . -draw_pause <sec> - Sets number of seconds to pause after display 5667 . -mat_view socket - Sends matrix to socket, can be accessed from Matlab (See Users-Manual: ch_matlab) 5668 . -viewer_socket_machine <machine> - Machine to use for socket 5669 . -viewer_socket_port <port> - Port number to use for socket 5670 - -mat_view binary:filename[:append] - Save matrix to file in binary format 5671 5672 Notes: 5673 MatSetValues() generally caches the values. The matrix is ready to 5674 use only after MatAssemblyBegin() and MatAssemblyEnd() have been called. 5675 Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES 5676 in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before 5677 using the matrix. 5678 5679 Space for preallocated nonzeros that is not filled by a call to MatSetValues() or a related routine are compressed 5680 out by assembly. If you intend to use that extra space on a subsequent assembly, be sure to insert explicit zeros 5681 before MAT_FINAL_ASSEMBLY so the space is not compressed out. 5682 5683 Level: beginner 5684 5685 .seealso: MatAssemblyBegin(), MatSetValues(), PetscDrawOpenX(), PetscDrawCreate(), MatView(), MatAssembled(), PetscViewerSocketOpen() 5686 @*/ 5687 PetscErrorCode MatAssemblyEnd(Mat mat,MatAssemblyType type) 5688 { 5689 PetscErrorCode ierr; 5690 static PetscInt inassm = 0; 5691 PetscBool flg = PETSC_FALSE; 5692 5693 PetscFunctionBegin; 5694 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5695 PetscValidType(mat,1); 5696 5697 inassm++; 5698 MatAssemblyEnd_InUse++; 5699 if (MatAssemblyEnd_InUse == 1) { /* Do the logging only the first time through */ 5700 ierr = PetscLogEventBegin(MAT_AssemblyEnd,mat,0,0,0);CHKERRQ(ierr); 5701 if (mat->ops->assemblyend) { 5702 ierr = (*mat->ops->assemblyend)(mat,type);CHKERRQ(ierr); 5703 } 5704 ierr = PetscLogEventEnd(MAT_AssemblyEnd,mat,0,0,0);CHKERRQ(ierr); 5705 } else if (mat->ops->assemblyend) { 5706 ierr = (*mat->ops->assemblyend)(mat,type);CHKERRQ(ierr); 5707 } 5708 5709 /* Flush assembly is not a true assembly */ 5710 if (type != MAT_FLUSH_ASSEMBLY) { 5711 mat->num_ass++; 5712 mat->assembled = PETSC_TRUE; 5713 mat->ass_nonzerostate = mat->nonzerostate; 5714 } 5715 5716 mat->insertmode = NOT_SET_VALUES; 5717 MatAssemblyEnd_InUse--; 5718 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 5719 if (!mat->symmetric_eternal) { 5720 mat->symmetric_set = PETSC_FALSE; 5721 mat->hermitian_set = PETSC_FALSE; 5722 mat->structurally_symmetric_set = PETSC_FALSE; 5723 } 5724 if (inassm == 1 && type != MAT_FLUSH_ASSEMBLY) { 5725 ierr = MatViewFromOptions(mat,NULL,"-mat_view");CHKERRQ(ierr); 5726 5727 if (mat->checksymmetryonassembly) { 5728 ierr = MatIsSymmetric(mat,mat->checksymmetrytol,&flg);CHKERRQ(ierr); 5729 if (flg) { 5730 ierr = PetscPrintf(PetscObjectComm((PetscObject)mat),"Matrix is symmetric (tolerance %g)\n",(double)mat->checksymmetrytol);CHKERRQ(ierr); 5731 } else { 5732 ierr = PetscPrintf(PetscObjectComm((PetscObject)mat),"Matrix is not symmetric (tolerance %g)\n",(double)mat->checksymmetrytol);CHKERRQ(ierr); 5733 } 5734 } 5735 if (mat->nullsp && mat->checknullspaceonassembly) { 5736 ierr = MatNullSpaceTest(mat->nullsp,mat,NULL);CHKERRQ(ierr); 5737 } 5738 } 5739 inassm--; 5740 PetscFunctionReturn(0); 5741 } 5742 5743 /*@ 5744 MatSetOption - Sets a parameter option for a matrix. Some options 5745 may be specific to certain storage formats. Some options 5746 determine how values will be inserted (or added). Sorted, 5747 row-oriented input will generally assemble the fastest. The default 5748 is row-oriented. 5749 5750 Logically Collective on Mat for certain operations, such as MAT_SPD, not collective for MAT_ROW_ORIENTED, see MatOption 5751 5752 Input Parameters: 5753 + mat - the matrix 5754 . option - the option, one of those listed below (and possibly others), 5755 - flg - turn the option on (PETSC_TRUE) or off (PETSC_FALSE) 5756 5757 Options Describing Matrix Structure: 5758 + MAT_SPD - symmetric positive definite 5759 . MAT_SYMMETRIC - symmetric in terms of both structure and value 5760 . MAT_HERMITIAN - transpose is the complex conjugation 5761 . MAT_STRUCTURALLY_SYMMETRIC - symmetric nonzero structure 5762 - MAT_SYMMETRY_ETERNAL - if you would like the symmetry/Hermitian flag 5763 you set to be kept with all future use of the matrix 5764 including after MatAssemblyBegin/End() which could 5765 potentially change the symmetry structure, i.e. you 5766 KNOW the matrix will ALWAYS have the property you set. 5767 Note that setting this flag alone implies nothing about whether the matrix is symmetric/Hermitian; 5768 the relevant flags must be set independently. 5769 5770 Options For Use with MatSetValues(): 5771 Insert a logically dense subblock, which can be 5772 . MAT_ROW_ORIENTED - row-oriented (default) 5773 5774 Note these options reflect the data you pass in with MatSetValues(); it has 5775 nothing to do with how the data is stored internally in the matrix 5776 data structure. 5777 5778 When (re)assembling a matrix, we can restrict the input for 5779 efficiency/debugging purposes. These options include 5780 + MAT_NEW_NONZERO_LOCATIONS - additional insertions will be allowed if they generate a new nonzero (slow) 5781 . MAT_FORCE_DIAGONAL_ENTRIES - forces diagonal entries to be allocated 5782 . MAT_IGNORE_OFF_PROC_ENTRIES - drops off-processor entries 5783 . MAT_NEW_NONZERO_LOCATION_ERR - generates an error for new matrix entry 5784 . MAT_USE_HASH_TABLE - uses a hash table to speed up matrix assembly 5785 . MAT_NO_OFF_PROC_ENTRIES - you know each process will only set values for its own rows, will generate an error if 5786 any process sets values for another process. This avoids all reductions in the MatAssembly routines and thus improves 5787 performance for very large process counts. 5788 - MAT_SUBSET_OFF_PROC_ENTRIES - you know that the first assembly after setting this flag will set a superset 5789 of the off-process entries required for all subsequent assemblies. This avoids a rendezvous step in the MatAssembly 5790 functions, instead sending only neighbor messages. 5791 5792 Notes: 5793 Except for MAT_UNUSED_NONZERO_LOCATION_ERR and MAT_ROW_ORIENTED all processes that share the matrix must pass the same value in flg! 5794 5795 Some options are relevant only for particular matrix types and 5796 are thus ignored by others. Other options are not supported by 5797 certain matrix types and will generate an error message if set. 5798 5799 If using a Fortran 77 module to compute a matrix, one may need to 5800 use the column-oriented option (or convert to the row-oriented 5801 format). 5802 5803 MAT_NEW_NONZERO_LOCATIONS set to PETSC_FALSE indicates that any add or insertion 5804 that would generate a new entry in the nonzero structure is instead 5805 ignored. Thus, if memory has not alredy been allocated for this particular 5806 data, then the insertion is ignored. For dense matrices, in which 5807 the entire array is allocated, no entries are ever ignored. 5808 Set after the first MatAssemblyEnd(). If this option is set then the MatAssemblyBegin/End() processes has one less global reduction 5809 5810 MAT_NEW_NONZERO_LOCATION_ERR set to PETSC_TRUE indicates that any add or insertion 5811 that would generate a new entry in the nonzero structure instead produces 5812 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 5813 5814 MAT_NEW_NONZERO_ALLOCATION_ERR set to PETSC_TRUE indicates that any add or insertion 5815 that would generate a new entry that has not been preallocated will 5816 instead produce an error. (Currently supported for AIJ and BAIJ formats 5817 only.) This is a useful flag when debugging matrix memory preallocation. 5818 If this option is set then the MatAssemblyBegin/End() processes has one less global reduction 5819 5820 MAT_IGNORE_OFF_PROC_ENTRIES set to PETSC_TRUE indicates entries destined for 5821 other processors should be dropped, rather than stashed. 5822 This is useful if you know that the "owning" processor is also 5823 always generating the correct matrix entries, so that PETSc need 5824 not transfer duplicate entries generated on another processor. 5825 5826 MAT_USE_HASH_TABLE indicates that a hash table be used to improve the 5827 searches during matrix assembly. When this flag is set, the hash table 5828 is created during the first Matrix Assembly. This hash table is 5829 used the next time through, during MatSetVaules()/MatSetVaulesBlocked() 5830 to improve the searching of indices. MAT_NEW_NONZERO_LOCATIONS flag 5831 should be used with MAT_USE_HASH_TABLE flag. This option is currently 5832 supported by MATMPIBAIJ format only. 5833 5834 MAT_KEEP_NONZERO_PATTERN indicates when MatZeroRows() is called the zeroed entries 5835 are kept in the nonzero structure 5836 5837 MAT_IGNORE_ZERO_ENTRIES - for AIJ/IS matrices this will stop zero values from creating 5838 a zero location in the matrix 5839 5840 MAT_USE_INODES - indicates using inode version of the code - works with AIJ matrix types 5841 5842 MAT_NO_OFF_PROC_ZERO_ROWS - you know each process will only zero its own rows. This avoids all reductions in the 5843 zero row routines and thus improves performance for very large process counts. 5844 5845 MAT_IGNORE_LOWER_TRIANGULAR - For SBAIJ matrices will ignore any insertions you make in the lower triangular 5846 part of the matrix (since they should match the upper triangular part). 5847 5848 MAT_SORTED_FULL - each process provides exactly its local rows; all column indices for a given row are passed in a 5849 single call to MatSetValues(), preallocation is perfect, row oriented, INSERT_VALUES is used. Common 5850 with finite difference schemes with non-periodic boundary conditions. 5851 5852 Level: intermediate 5853 5854 .seealso: MatOption, Mat 5855 5856 @*/ 5857 PetscErrorCode MatSetOption(Mat mat,MatOption op,PetscBool flg) 5858 { 5859 PetscErrorCode ierr; 5860 5861 PetscFunctionBegin; 5862 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5863 if (op > 0) { 5864 PetscValidLogicalCollectiveEnum(mat,op,2); 5865 PetscValidLogicalCollectiveBool(mat,flg,3); 5866 } 5867 5868 PetscCheckFalse(((int) op) <= MAT_OPTION_MIN || ((int) op) >= MAT_OPTION_MAX,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Options %d is out of range",(int)op); 5869 5870 switch (op) { 5871 case MAT_FORCE_DIAGONAL_ENTRIES: 5872 mat->force_diagonals = flg; 5873 PetscFunctionReturn(0); 5874 case MAT_NO_OFF_PROC_ENTRIES: 5875 mat->nooffprocentries = flg; 5876 PetscFunctionReturn(0); 5877 case MAT_SUBSET_OFF_PROC_ENTRIES: 5878 mat->assembly_subset = flg; 5879 if (!mat->assembly_subset) { /* See the same logic in VecAssembly wrt VEC_SUBSET_OFF_PROC_ENTRIES */ 5880 #if !defined(PETSC_HAVE_MPIUNI) 5881 ierr = MatStashScatterDestroy_BTS(&mat->stash);CHKERRQ(ierr); 5882 #endif 5883 mat->stash.first_assembly_done = PETSC_FALSE; 5884 } 5885 PetscFunctionReturn(0); 5886 case MAT_NO_OFF_PROC_ZERO_ROWS: 5887 mat->nooffproczerorows = flg; 5888 PetscFunctionReturn(0); 5889 case MAT_SPD: 5890 mat->spd_set = PETSC_TRUE; 5891 mat->spd = flg; 5892 if (flg) { 5893 mat->symmetric = PETSC_TRUE; 5894 mat->structurally_symmetric = PETSC_TRUE; 5895 mat->symmetric_set = PETSC_TRUE; 5896 mat->structurally_symmetric_set = PETSC_TRUE; 5897 } 5898 break; 5899 case MAT_SYMMETRIC: 5900 mat->symmetric = flg; 5901 if (flg) mat->structurally_symmetric = PETSC_TRUE; 5902 mat->symmetric_set = PETSC_TRUE; 5903 mat->structurally_symmetric_set = flg; 5904 #if !defined(PETSC_USE_COMPLEX) 5905 mat->hermitian = flg; 5906 mat->hermitian_set = PETSC_TRUE; 5907 #endif 5908 break; 5909 case MAT_HERMITIAN: 5910 mat->hermitian = flg; 5911 if (flg) mat->structurally_symmetric = PETSC_TRUE; 5912 mat->hermitian_set = PETSC_TRUE; 5913 mat->structurally_symmetric_set = flg; 5914 #if !defined(PETSC_USE_COMPLEX) 5915 mat->symmetric = flg; 5916 mat->symmetric_set = PETSC_TRUE; 5917 #endif 5918 break; 5919 case MAT_STRUCTURALLY_SYMMETRIC: 5920 mat->structurally_symmetric = flg; 5921 mat->structurally_symmetric_set = PETSC_TRUE; 5922 break; 5923 case MAT_SYMMETRY_ETERNAL: 5924 mat->symmetric_eternal = flg; 5925 break; 5926 case MAT_STRUCTURE_ONLY: 5927 mat->structure_only = flg; 5928 break; 5929 case MAT_SORTED_FULL: 5930 mat->sortedfull = flg; 5931 break; 5932 default: 5933 break; 5934 } 5935 if (mat->ops->setoption) { 5936 ierr = (*mat->ops->setoption)(mat,op,flg);CHKERRQ(ierr); 5937 } 5938 PetscFunctionReturn(0); 5939 } 5940 5941 /*@ 5942 MatGetOption - Gets a parameter option that has been set for a matrix. 5943 5944 Logically Collective on Mat for certain operations, such as MAT_SPD, not collective for MAT_ROW_ORIENTED, see MatOption 5945 5946 Input Parameters: 5947 + mat - the matrix 5948 - option - the option, this only responds to certain options, check the code for which ones 5949 5950 Output Parameter: 5951 . flg - turn the option on (PETSC_TRUE) or off (PETSC_FALSE) 5952 5953 Notes: 5954 Can only be called after MatSetSizes() and MatSetType() have been set. 5955 5956 Level: intermediate 5957 5958 .seealso: MatOption, MatSetOption() 5959 5960 @*/ 5961 PetscErrorCode MatGetOption(Mat mat,MatOption op,PetscBool *flg) 5962 { 5963 PetscFunctionBegin; 5964 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5965 PetscValidType(mat,1); 5966 5967 PetscCheckFalse(((int) op) <= MAT_OPTION_MIN || ((int) op) >= MAT_OPTION_MAX,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Options %d is out of range",(int)op); 5968 PetscCheckFalse(!((PetscObject)mat)->type_name,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_TYPENOTSET,"Cannot get options until type and size have been set, see MatSetType() and MatSetSizes()"); 5969 5970 switch (op) { 5971 case MAT_NO_OFF_PROC_ENTRIES: 5972 *flg = mat->nooffprocentries; 5973 break; 5974 case MAT_NO_OFF_PROC_ZERO_ROWS: 5975 *flg = mat->nooffproczerorows; 5976 break; 5977 case MAT_SYMMETRIC: 5978 *flg = mat->symmetric; 5979 break; 5980 case MAT_HERMITIAN: 5981 *flg = mat->hermitian; 5982 break; 5983 case MAT_STRUCTURALLY_SYMMETRIC: 5984 *flg = mat->structurally_symmetric; 5985 break; 5986 case MAT_SYMMETRY_ETERNAL: 5987 *flg = mat->symmetric_eternal; 5988 break; 5989 case MAT_SPD: 5990 *flg = mat->spd; 5991 break; 5992 default: 5993 break; 5994 } 5995 PetscFunctionReturn(0); 5996 } 5997 5998 /*@ 5999 MatZeroEntries - Zeros all entries of a matrix. For sparse matrices 6000 this routine retains the old nonzero structure. 6001 6002 Logically Collective on Mat 6003 6004 Input Parameters: 6005 . mat - the matrix 6006 6007 Level: intermediate 6008 6009 Notes: 6010 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. 6011 See the Performance chapter of the users manual for information on preallocating matrices. 6012 6013 .seealso: MatZeroRows() 6014 @*/ 6015 PetscErrorCode MatZeroEntries(Mat mat) 6016 { 6017 PetscErrorCode ierr; 6018 6019 PetscFunctionBegin; 6020 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6021 PetscValidType(mat,1); 6022 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6023 PetscCheckFalse(mat->insertmode != NOT_SET_VALUES,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for matrices where you have set values but not yet assembled"); 6024 PetscCheckFalse(!mat->ops->zeroentries,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 6025 MatCheckPreallocated(mat,1); 6026 6027 ierr = PetscLogEventBegin(MAT_ZeroEntries,mat,0,0,0);CHKERRQ(ierr); 6028 ierr = (*mat->ops->zeroentries)(mat);CHKERRQ(ierr); 6029 ierr = PetscLogEventEnd(MAT_ZeroEntries,mat,0,0,0);CHKERRQ(ierr); 6030 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 6031 PetscFunctionReturn(0); 6032 } 6033 6034 /*@ 6035 MatZeroRowsColumns - Zeros all entries (except possibly the main diagonal) 6036 of a set of rows and columns of a matrix. 6037 6038 Collective on Mat 6039 6040 Input Parameters: 6041 + mat - the matrix 6042 . numRows - the number of rows to remove 6043 . rows - the global row indices 6044 . diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry) 6045 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 6046 - b - optional vector of right hand side, that will be adjusted by provided solution 6047 6048 Notes: 6049 This does not change the nonzero structure of the matrix, it merely zeros those entries in the matrix. 6050 6051 The user can set a value in the diagonal entry (or for the AIJ and 6052 row formats can optionally remove the main diagonal entry from the 6053 nonzero structure as well, by passing 0.0 as the final argument). 6054 6055 For the parallel case, all processes that share the matrix (i.e., 6056 those in the communicator used for matrix creation) MUST call this 6057 routine, regardless of whether any rows being zeroed are owned by 6058 them. 6059 6060 Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to 6061 list only rows local to itself). 6062 6063 The option MAT_NO_OFF_PROC_ZERO_ROWS does not apply to this routine. 6064 6065 Level: intermediate 6066 6067 .seealso: MatZeroRowsIS(), MatZeroRows(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 6068 MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil() 6069 @*/ 6070 PetscErrorCode MatZeroRowsColumns(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 6071 { 6072 PetscErrorCode ierr; 6073 6074 PetscFunctionBegin; 6075 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6076 PetscValidType(mat,1); 6077 if (numRows) PetscValidIntPointer(rows,3); 6078 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 6079 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6080 PetscCheckFalse(!mat->ops->zerorowscolumns,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 6081 MatCheckPreallocated(mat,1); 6082 6083 ierr = (*mat->ops->zerorowscolumns)(mat,numRows,rows,diag,x,b);CHKERRQ(ierr); 6084 ierr = MatViewFromOptions(mat,NULL,"-mat_view");CHKERRQ(ierr); 6085 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 6086 PetscFunctionReturn(0); 6087 } 6088 6089 /*@ 6090 MatZeroRowsColumnsIS - Zeros all entries (except possibly the main diagonal) 6091 of a set of rows and columns of a matrix. 6092 6093 Collective on Mat 6094 6095 Input Parameters: 6096 + mat - the matrix 6097 . is - the rows to zero 6098 . diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry) 6099 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 6100 - b - optional vector of right hand side, that will be adjusted by provided solution 6101 6102 Notes: 6103 This does not change the nonzero structure of the matrix, it merely zeros those entries in the matrix. 6104 6105 The user can set a value in the diagonal entry (or for the AIJ and 6106 row formats can optionally remove the main diagonal entry from the 6107 nonzero structure as well, by passing 0.0 as the final argument). 6108 6109 For the parallel case, all processes that share the matrix (i.e., 6110 those in the communicator used for matrix creation) MUST call this 6111 routine, regardless of whether any rows being zeroed are owned by 6112 them. 6113 6114 Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to 6115 list only rows local to itself). 6116 6117 The option MAT_NO_OFF_PROC_ZERO_ROWS does not apply to this routine. 6118 6119 Level: intermediate 6120 6121 .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 6122 MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRows(), MatZeroRowsColumnsStencil() 6123 @*/ 6124 PetscErrorCode MatZeroRowsColumnsIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b) 6125 { 6126 PetscErrorCode ierr; 6127 PetscInt numRows; 6128 const PetscInt *rows; 6129 6130 PetscFunctionBegin; 6131 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6132 PetscValidHeaderSpecific(is,IS_CLASSID,2); 6133 PetscValidType(mat,1); 6134 PetscValidType(is,2); 6135 ierr = ISGetLocalSize(is,&numRows);CHKERRQ(ierr); 6136 ierr = ISGetIndices(is,&rows);CHKERRQ(ierr); 6137 ierr = MatZeroRowsColumns(mat,numRows,rows,diag,x,b);CHKERRQ(ierr); 6138 ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr); 6139 PetscFunctionReturn(0); 6140 } 6141 6142 /*@ 6143 MatZeroRows - Zeros all entries (except possibly the main diagonal) 6144 of a set of rows of a matrix. 6145 6146 Collective on Mat 6147 6148 Input Parameters: 6149 + mat - the matrix 6150 . numRows - the number of rows to remove 6151 . rows - the global row indices 6152 . diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry) 6153 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 6154 - b - optional vector of right hand side, that will be adjusted by provided solution 6155 6156 Notes: 6157 For the AIJ and BAIJ matrix formats this removes the old nonzero structure, 6158 but does not release memory. For the dense and block diagonal 6159 formats this does not alter the nonzero structure. 6160 6161 If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure 6162 of the matrix is not changed (even for AIJ and BAIJ matrices) the values are 6163 merely zeroed. 6164 6165 The user can set a value in the diagonal entry (or for the AIJ and 6166 row formats can optionally remove the main diagonal entry from the 6167 nonzero structure as well, by passing 0.0 as the final argument). 6168 6169 For the parallel case, all processes that share the matrix (i.e., 6170 those in the communicator used for matrix creation) MUST call this 6171 routine, regardless of whether any rows being zeroed are owned by 6172 them. 6173 6174 Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to 6175 list only rows local to itself). 6176 6177 You can call MatSetOption(mat,MAT_NO_OFF_PROC_ZERO_ROWS,PETSC_TRUE) if each process indicates only rows it 6178 owns that are to be zeroed. This saves a global synchronization in the implementation. 6179 6180 Level: intermediate 6181 6182 .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 6183 MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil() 6184 @*/ 6185 PetscErrorCode MatZeroRows(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 6186 { 6187 PetscErrorCode ierr; 6188 6189 PetscFunctionBegin; 6190 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6191 PetscValidType(mat,1); 6192 if (numRows) PetscValidIntPointer(rows,3); 6193 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 6194 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6195 PetscCheckFalse(!mat->ops->zerorows,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 6196 MatCheckPreallocated(mat,1); 6197 6198 ierr = (*mat->ops->zerorows)(mat,numRows,rows,diag,x,b);CHKERRQ(ierr); 6199 ierr = MatViewFromOptions(mat,NULL,"-mat_view");CHKERRQ(ierr); 6200 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 6201 PetscFunctionReturn(0); 6202 } 6203 6204 /*@ 6205 MatZeroRowsIS - Zeros all entries (except possibly the main diagonal) 6206 of a set of rows of a matrix. 6207 6208 Collective on Mat 6209 6210 Input Parameters: 6211 + mat - the matrix 6212 . is - index set of rows to remove (if NULL then no row is removed) 6213 . diag - value put in all diagonals of eliminated rows 6214 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 6215 - b - optional vector of right hand side, that will be adjusted by provided solution 6216 6217 Notes: 6218 For the AIJ and BAIJ matrix formats this removes the old nonzero structure, 6219 but does not release memory. For the dense and block diagonal 6220 formats this does not alter the nonzero structure. 6221 6222 If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure 6223 of the matrix is not changed (even for AIJ and BAIJ matrices) the values are 6224 merely zeroed. 6225 6226 The user can set a value in the diagonal entry (or for the AIJ and 6227 row formats can optionally remove the main diagonal entry from the 6228 nonzero structure as well, by passing 0.0 as the final argument). 6229 6230 For the parallel case, all processes that share the matrix (i.e., 6231 those in the communicator used for matrix creation) MUST call this 6232 routine, regardless of whether any rows being zeroed are owned by 6233 them. 6234 6235 Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to 6236 list only rows local to itself). 6237 6238 You can call MatSetOption(mat,MAT_NO_OFF_PROC_ZERO_ROWS,PETSC_TRUE) if each process indicates only rows it 6239 owns that are to be zeroed. This saves a global synchronization in the implementation. 6240 6241 Level: intermediate 6242 6243 .seealso: MatZeroRows(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 6244 MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil() 6245 @*/ 6246 PetscErrorCode MatZeroRowsIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b) 6247 { 6248 PetscInt numRows = 0; 6249 const PetscInt *rows = NULL; 6250 PetscErrorCode ierr; 6251 6252 PetscFunctionBegin; 6253 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6254 PetscValidType(mat,1); 6255 if (is) { 6256 PetscValidHeaderSpecific(is,IS_CLASSID,2); 6257 ierr = ISGetLocalSize(is,&numRows);CHKERRQ(ierr); 6258 ierr = ISGetIndices(is,&rows);CHKERRQ(ierr); 6259 } 6260 ierr = MatZeroRows(mat,numRows,rows,diag,x,b);CHKERRQ(ierr); 6261 if (is) { 6262 ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr); 6263 } 6264 PetscFunctionReturn(0); 6265 } 6266 6267 /*@ 6268 MatZeroRowsStencil - Zeros all entries (except possibly the main diagonal) 6269 of a set of rows of a matrix. These rows must be local to the process. 6270 6271 Collective on Mat 6272 6273 Input Parameters: 6274 + mat - the matrix 6275 . numRows - the number of rows to remove 6276 . rows - the grid coordinates (and component number when dof > 1) for matrix rows 6277 . diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry) 6278 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 6279 - b - optional vector of right hand side, that will be adjusted by provided solution 6280 6281 Notes: 6282 For the AIJ and BAIJ matrix formats this removes the old nonzero structure, 6283 but does not release memory. For the dense and block diagonal 6284 formats this does not alter the nonzero structure. 6285 6286 If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure 6287 of the matrix is not changed (even for AIJ and BAIJ matrices) the values are 6288 merely zeroed. 6289 6290 The user can set a value in the diagonal entry (or for the AIJ and 6291 row formats can optionally remove the main diagonal entry from the 6292 nonzero structure as well, by passing 0.0 as the final argument). 6293 6294 For the parallel case, all processes that share the matrix (i.e., 6295 those in the communicator used for matrix creation) MUST call this 6296 routine, regardless of whether any rows being zeroed are owned by 6297 them. 6298 6299 Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to 6300 list only rows local to itself). 6301 6302 The grid coordinates are across the entire grid, not just the local portion 6303 6304 In Fortran idxm and idxn should be declared as 6305 $ MatStencil idxm(4,m) 6306 and the values inserted using 6307 $ idxm(MatStencil_i,1) = i 6308 $ idxm(MatStencil_j,1) = j 6309 $ idxm(MatStencil_k,1) = k 6310 $ idxm(MatStencil_c,1) = c 6311 etc 6312 6313 For periodic boundary conditions use negative indices for values to the left (below 0; that are to be 6314 obtained by wrapping values from right edge). For values to the right of the last entry using that index plus one 6315 etc to obtain values that obtained by wrapping the values from the left edge. This does not work for anything but the 6316 DM_BOUNDARY_PERIODIC boundary type. 6317 6318 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 6319 a single value per point) you can skip filling those indices. 6320 6321 Level: intermediate 6322 6323 .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsl(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 6324 MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil() 6325 @*/ 6326 PetscErrorCode MatZeroRowsStencil(Mat mat,PetscInt numRows,const MatStencil rows[],PetscScalar diag,Vec x,Vec b) 6327 { 6328 PetscInt dim = mat->stencil.dim; 6329 PetscInt sdim = dim - (1 - (PetscInt) mat->stencil.noc); 6330 PetscInt *dims = mat->stencil.dims+1; 6331 PetscInt *starts = mat->stencil.starts; 6332 PetscInt *dxm = (PetscInt*) rows; 6333 PetscInt *jdxm, i, j, tmp, numNewRows = 0; 6334 PetscErrorCode ierr; 6335 6336 PetscFunctionBegin; 6337 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6338 PetscValidType(mat,1); 6339 if (numRows) PetscValidPointer(rows,3); 6340 6341 ierr = PetscMalloc1(numRows, &jdxm);CHKERRQ(ierr); 6342 for (i = 0; i < numRows; ++i) { 6343 /* Skip unused dimensions (they are ordered k, j, i, c) */ 6344 for (j = 0; j < 3-sdim; ++j) dxm++; 6345 /* Local index in X dir */ 6346 tmp = *dxm++ - starts[0]; 6347 /* Loop over remaining dimensions */ 6348 for (j = 0; j < dim-1; ++j) { 6349 /* If nonlocal, set index to be negative */ 6350 if ((*dxm++ - starts[j+1]) < 0 || tmp < 0) tmp = PETSC_MIN_INT; 6351 /* Update local index */ 6352 else tmp = tmp*dims[j] + *(dxm-1) - starts[j+1]; 6353 } 6354 /* Skip component slot if necessary */ 6355 if (mat->stencil.noc) dxm++; 6356 /* Local row number */ 6357 if (tmp >= 0) { 6358 jdxm[numNewRows++] = tmp; 6359 } 6360 } 6361 ierr = MatZeroRowsLocal(mat,numNewRows,jdxm,diag,x,b);CHKERRQ(ierr); 6362 ierr = PetscFree(jdxm);CHKERRQ(ierr); 6363 PetscFunctionReturn(0); 6364 } 6365 6366 /*@ 6367 MatZeroRowsColumnsStencil - Zeros all row and column entries (except possibly the main diagonal) 6368 of a set of rows and columns of a matrix. 6369 6370 Collective on Mat 6371 6372 Input Parameters: 6373 + mat - the matrix 6374 . numRows - the number of rows/columns to remove 6375 . rows - the grid coordinates (and component number when dof > 1) for matrix rows 6376 . diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry) 6377 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 6378 - b - optional vector of right hand side, that will be adjusted by provided solution 6379 6380 Notes: 6381 For the AIJ and BAIJ matrix formats this removes the old nonzero structure, 6382 but does not release memory. For the dense and block diagonal 6383 formats this does not alter the nonzero structure. 6384 6385 If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure 6386 of the matrix is not changed (even for AIJ and BAIJ matrices) the values are 6387 merely zeroed. 6388 6389 The user can set a value in the diagonal entry (or for the AIJ and 6390 row formats can optionally remove the main diagonal entry from the 6391 nonzero structure as well, by passing 0.0 as the final argument). 6392 6393 For the parallel case, all processes that share the matrix (i.e., 6394 those in the communicator used for matrix creation) MUST call this 6395 routine, regardless of whether any rows being zeroed are owned by 6396 them. 6397 6398 Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to 6399 list only rows local to itself, but the row/column numbers are given in local numbering). 6400 6401 The grid coordinates are across the entire grid, not just the local portion 6402 6403 In Fortran idxm and idxn should be declared as 6404 $ MatStencil idxm(4,m) 6405 and the values inserted using 6406 $ idxm(MatStencil_i,1) = i 6407 $ idxm(MatStencil_j,1) = j 6408 $ idxm(MatStencil_k,1) = k 6409 $ idxm(MatStencil_c,1) = c 6410 etc 6411 6412 For periodic boundary conditions use negative indices for values to the left (below 0; that are to be 6413 obtained by wrapping values from right edge). For values to the right of the last entry using that index plus one 6414 etc to obtain values that obtained by wrapping the values from the left edge. This does not work for anything but the 6415 DM_BOUNDARY_PERIODIC boundary type. 6416 6417 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 6418 a single value per point) you can skip filling those indices. 6419 6420 Level: intermediate 6421 6422 .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 6423 MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRows() 6424 @*/ 6425 PetscErrorCode MatZeroRowsColumnsStencil(Mat mat,PetscInt numRows,const MatStencil rows[],PetscScalar diag,Vec x,Vec b) 6426 { 6427 PetscInt dim = mat->stencil.dim; 6428 PetscInt sdim = dim - (1 - (PetscInt) mat->stencil.noc); 6429 PetscInt *dims = mat->stencil.dims+1; 6430 PetscInt *starts = mat->stencil.starts; 6431 PetscInt *dxm = (PetscInt*) rows; 6432 PetscInt *jdxm, i, j, tmp, numNewRows = 0; 6433 PetscErrorCode ierr; 6434 6435 PetscFunctionBegin; 6436 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6437 PetscValidType(mat,1); 6438 if (numRows) PetscValidPointer(rows,3); 6439 6440 ierr = PetscMalloc1(numRows, &jdxm);CHKERRQ(ierr); 6441 for (i = 0; i < numRows; ++i) { 6442 /* Skip unused dimensions (they are ordered k, j, i, c) */ 6443 for (j = 0; j < 3-sdim; ++j) dxm++; 6444 /* Local index in X dir */ 6445 tmp = *dxm++ - starts[0]; 6446 /* Loop over remaining dimensions */ 6447 for (j = 0; j < dim-1; ++j) { 6448 /* If nonlocal, set index to be negative */ 6449 if ((*dxm++ - starts[j+1]) < 0 || tmp < 0) tmp = PETSC_MIN_INT; 6450 /* Update local index */ 6451 else tmp = tmp*dims[j] + *(dxm-1) - starts[j+1]; 6452 } 6453 /* Skip component slot if necessary */ 6454 if (mat->stencil.noc) dxm++; 6455 /* Local row number */ 6456 if (tmp >= 0) { 6457 jdxm[numNewRows++] = tmp; 6458 } 6459 } 6460 ierr = MatZeroRowsColumnsLocal(mat,numNewRows,jdxm,diag,x,b);CHKERRQ(ierr); 6461 ierr = PetscFree(jdxm);CHKERRQ(ierr); 6462 PetscFunctionReturn(0); 6463 } 6464 6465 /*@C 6466 MatZeroRowsLocal - Zeros all entries (except possibly the main diagonal) 6467 of a set of rows of a matrix; using local numbering of rows. 6468 6469 Collective on Mat 6470 6471 Input Parameters: 6472 + mat - the matrix 6473 . numRows - the number of rows to remove 6474 . rows - the local row indices 6475 . diag - value put in all diagonals of eliminated rows 6476 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 6477 - b - optional vector of right hand side, that will be adjusted by provided solution 6478 6479 Notes: 6480 Before calling MatZeroRowsLocal(), the user must first set the 6481 local-to-global mapping by calling MatSetLocalToGlobalMapping(). 6482 6483 For the AIJ matrix formats this removes the old nonzero structure, 6484 but does not release memory. For the dense and block diagonal 6485 formats this does not alter the nonzero structure. 6486 6487 If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure 6488 of the matrix is not changed (even for AIJ and BAIJ matrices) the values are 6489 merely zeroed. 6490 6491 The user can set a value in the diagonal entry (or for the AIJ and 6492 row formats can optionally remove the main diagonal entry from the 6493 nonzero structure as well, by passing 0.0 as the final argument). 6494 6495 You can call MatSetOption(mat,MAT_NO_OFF_PROC_ZERO_ROWS,PETSC_TRUE) if each process indicates only rows it 6496 owns that are to be zeroed. This saves a global synchronization in the implementation. 6497 6498 Level: intermediate 6499 6500 .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRows(), MatSetOption(), 6501 MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil() 6502 @*/ 6503 PetscErrorCode MatZeroRowsLocal(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 6504 { 6505 PetscErrorCode ierr; 6506 6507 PetscFunctionBegin; 6508 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6509 PetscValidType(mat,1); 6510 if (numRows) PetscValidIntPointer(rows,3); 6511 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 6512 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6513 MatCheckPreallocated(mat,1); 6514 6515 if (mat->ops->zerorowslocal) { 6516 ierr = (*mat->ops->zerorowslocal)(mat,numRows,rows,diag,x,b);CHKERRQ(ierr); 6517 } else { 6518 IS is, newis; 6519 const PetscInt *newRows; 6520 6521 PetscCheckFalse(!mat->rmap->mapping,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Need to provide local to global mapping to matrix first"); 6522 ierr = ISCreateGeneral(PETSC_COMM_SELF,numRows,rows,PETSC_COPY_VALUES,&is);CHKERRQ(ierr); 6523 ierr = ISLocalToGlobalMappingApplyIS(mat->rmap->mapping,is,&newis);CHKERRQ(ierr); 6524 ierr = ISGetIndices(newis,&newRows);CHKERRQ(ierr); 6525 ierr = (*mat->ops->zerorows)(mat,numRows,newRows,diag,x,b);CHKERRQ(ierr); 6526 ierr = ISRestoreIndices(newis,&newRows);CHKERRQ(ierr); 6527 ierr = ISDestroy(&newis);CHKERRQ(ierr); 6528 ierr = ISDestroy(&is);CHKERRQ(ierr); 6529 } 6530 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 6531 PetscFunctionReturn(0); 6532 } 6533 6534 /*@ 6535 MatZeroRowsLocalIS - Zeros all entries (except possibly the main diagonal) 6536 of a set of rows of a matrix; using local numbering of rows. 6537 6538 Collective on Mat 6539 6540 Input Parameters: 6541 + mat - the matrix 6542 . is - index set of rows to remove 6543 . diag - value put in all diagonals of eliminated rows 6544 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 6545 - b - optional vector of right hand side, that will be adjusted by provided solution 6546 6547 Notes: 6548 Before calling MatZeroRowsLocalIS(), the user must first set the 6549 local-to-global mapping by calling MatSetLocalToGlobalMapping(). 6550 6551 For the AIJ matrix formats this removes the old nonzero structure, 6552 but does not release memory. For the dense and block diagonal 6553 formats this does not alter the nonzero structure. 6554 6555 If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure 6556 of the matrix is not changed (even for AIJ and BAIJ matrices) the values are 6557 merely zeroed. 6558 6559 The user can set a value in the diagonal entry (or for the AIJ and 6560 row formats can optionally remove the main diagonal entry from the 6561 nonzero structure as well, by passing 0.0 as the final argument). 6562 6563 You can call MatSetOption(mat,MAT_NO_OFF_PROC_ZERO_ROWS,PETSC_TRUE) if each process indicates only rows it 6564 owns that are to be zeroed. This saves a global synchronization in the implementation. 6565 6566 Level: intermediate 6567 6568 .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRows(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 6569 MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil() 6570 @*/ 6571 PetscErrorCode MatZeroRowsLocalIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b) 6572 { 6573 PetscErrorCode ierr; 6574 PetscInt numRows; 6575 const PetscInt *rows; 6576 6577 PetscFunctionBegin; 6578 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6579 PetscValidType(mat,1); 6580 PetscValidHeaderSpecific(is,IS_CLASSID,2); 6581 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 6582 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6583 MatCheckPreallocated(mat,1); 6584 6585 ierr = ISGetLocalSize(is,&numRows);CHKERRQ(ierr); 6586 ierr = ISGetIndices(is,&rows);CHKERRQ(ierr); 6587 ierr = MatZeroRowsLocal(mat,numRows,rows,diag,x,b);CHKERRQ(ierr); 6588 ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr); 6589 PetscFunctionReturn(0); 6590 } 6591 6592 /*@ 6593 MatZeroRowsColumnsLocal - Zeros all entries (except possibly the main diagonal) 6594 of a set of rows and columns of a matrix; using local numbering of rows. 6595 6596 Collective on Mat 6597 6598 Input Parameters: 6599 + mat - the matrix 6600 . numRows - the number of rows to remove 6601 . rows - the global row indices 6602 . diag - value put in all diagonals of eliminated rows 6603 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 6604 - b - optional vector of right hand side, that will be adjusted by provided solution 6605 6606 Notes: 6607 Before calling MatZeroRowsColumnsLocal(), the user must first set the 6608 local-to-global mapping by calling MatSetLocalToGlobalMapping(). 6609 6610 The user can set a value in the diagonal entry (or for the AIJ and 6611 row formats can optionally remove the main diagonal entry from the 6612 nonzero structure as well, by passing 0.0 as the final argument). 6613 6614 Level: intermediate 6615 6616 .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 6617 MatZeroRows(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil() 6618 @*/ 6619 PetscErrorCode MatZeroRowsColumnsLocal(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 6620 { 6621 PetscErrorCode ierr; 6622 IS is, newis; 6623 const PetscInt *newRows; 6624 6625 PetscFunctionBegin; 6626 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6627 PetscValidType(mat,1); 6628 if (numRows) PetscValidIntPointer(rows,3); 6629 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 6630 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6631 MatCheckPreallocated(mat,1); 6632 6633 PetscCheckFalse(!mat->cmap->mapping,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Need to provide local to global mapping to matrix first"); 6634 ierr = ISCreateGeneral(PETSC_COMM_SELF,numRows,rows,PETSC_COPY_VALUES,&is);CHKERRQ(ierr); 6635 ierr = ISLocalToGlobalMappingApplyIS(mat->cmap->mapping,is,&newis);CHKERRQ(ierr); 6636 ierr = ISGetIndices(newis,&newRows);CHKERRQ(ierr); 6637 ierr = (*mat->ops->zerorowscolumns)(mat,numRows,newRows,diag,x,b);CHKERRQ(ierr); 6638 ierr = ISRestoreIndices(newis,&newRows);CHKERRQ(ierr); 6639 ierr = ISDestroy(&newis);CHKERRQ(ierr); 6640 ierr = ISDestroy(&is);CHKERRQ(ierr); 6641 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 6642 PetscFunctionReturn(0); 6643 } 6644 6645 /*@ 6646 MatZeroRowsColumnsLocalIS - Zeros all entries (except possibly the main diagonal) 6647 of a set of rows and columns of a matrix; using local numbering of rows. 6648 6649 Collective on Mat 6650 6651 Input Parameters: 6652 + mat - the matrix 6653 . is - index set of rows to remove 6654 . diag - value put in all diagonals of eliminated rows 6655 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 6656 - b - optional vector of right hand side, that will be adjusted by provided solution 6657 6658 Notes: 6659 Before calling MatZeroRowsColumnsLocalIS(), the user must first set the 6660 local-to-global mapping by calling MatSetLocalToGlobalMapping(). 6661 6662 The user can set a value in the diagonal entry (or for the AIJ and 6663 row formats can optionally remove the main diagonal entry from the 6664 nonzero structure as well, by passing 0.0 as the final argument). 6665 6666 Level: intermediate 6667 6668 .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 6669 MatZeroRowsColumnsLocal(), MatZeroRows(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil() 6670 @*/ 6671 PetscErrorCode MatZeroRowsColumnsLocalIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b) 6672 { 6673 PetscErrorCode ierr; 6674 PetscInt numRows; 6675 const PetscInt *rows; 6676 6677 PetscFunctionBegin; 6678 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6679 PetscValidType(mat,1); 6680 PetscValidHeaderSpecific(is,IS_CLASSID,2); 6681 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 6682 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6683 MatCheckPreallocated(mat,1); 6684 6685 ierr = ISGetLocalSize(is,&numRows);CHKERRQ(ierr); 6686 ierr = ISGetIndices(is,&rows);CHKERRQ(ierr); 6687 ierr = MatZeroRowsColumnsLocal(mat,numRows,rows,diag,x,b);CHKERRQ(ierr); 6688 ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr); 6689 PetscFunctionReturn(0); 6690 } 6691 6692 /*@C 6693 MatGetSize - Returns the numbers of rows and columns in a matrix. 6694 6695 Not Collective 6696 6697 Input Parameter: 6698 . mat - the matrix 6699 6700 Output Parameters: 6701 + m - the number of global rows 6702 - n - the number of global columns 6703 6704 Note: both output parameters can be NULL on input. 6705 6706 Level: beginner 6707 6708 .seealso: MatGetLocalSize() 6709 @*/ 6710 PetscErrorCode MatGetSize(Mat mat,PetscInt *m,PetscInt *n) 6711 { 6712 PetscFunctionBegin; 6713 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6714 if (m) *m = mat->rmap->N; 6715 if (n) *n = mat->cmap->N; 6716 PetscFunctionReturn(0); 6717 } 6718 6719 /*@C 6720 MatGetLocalSize - Returns the number of local rows and local columns 6721 of a matrix, that is the local size of the left and right vectors as returned by MatCreateVecs(). 6722 6723 Not Collective 6724 6725 Input Parameter: 6726 . mat - the matrix 6727 6728 Output Parameters: 6729 + m - the number of local rows 6730 - n - the number of local columns 6731 6732 Note: both output parameters can be NULL on input. 6733 6734 Level: beginner 6735 6736 .seealso: MatGetSize() 6737 @*/ 6738 PetscErrorCode MatGetLocalSize(Mat mat,PetscInt *m,PetscInt *n) 6739 { 6740 PetscFunctionBegin; 6741 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6742 if (m) PetscValidIntPointer(m,2); 6743 if (n) PetscValidIntPointer(n,3); 6744 if (m) *m = mat->rmap->n; 6745 if (n) *n = mat->cmap->n; 6746 PetscFunctionReturn(0); 6747 } 6748 6749 /*@C 6750 MatGetOwnershipRangeColumn - Returns the range of matrix columns associated with rows of a vector one multiplies by that owned by 6751 this processor. (The columns of the "diagonal block") 6752 6753 Not Collective, unless matrix has not been allocated, then collective on Mat 6754 6755 Input Parameter: 6756 . mat - the matrix 6757 6758 Output Parameters: 6759 + m - the global index of the first local column 6760 - n - one more than the global index of the last local column 6761 6762 Notes: 6763 both output parameters can be NULL on input. 6764 6765 Level: developer 6766 6767 .seealso: MatGetOwnershipRange(), MatGetOwnershipRanges(), MatGetOwnershipRangesColumn() 6768 6769 @*/ 6770 PetscErrorCode MatGetOwnershipRangeColumn(Mat mat,PetscInt *m,PetscInt *n) 6771 { 6772 PetscFunctionBegin; 6773 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6774 PetscValidType(mat,1); 6775 if (m) PetscValidIntPointer(m,2); 6776 if (n) PetscValidIntPointer(n,3); 6777 MatCheckPreallocated(mat,1); 6778 if (m) *m = mat->cmap->rstart; 6779 if (n) *n = mat->cmap->rend; 6780 PetscFunctionReturn(0); 6781 } 6782 6783 /*@C 6784 MatGetOwnershipRange - Returns the range of matrix rows owned by 6785 this processor, assuming that the matrix is laid out with the first 6786 n1 rows on the first processor, the next n2 rows on the second, etc. 6787 For certain parallel layouts this range may not be well defined. 6788 6789 Not Collective 6790 6791 Input Parameter: 6792 . mat - the matrix 6793 6794 Output Parameters: 6795 + m - the global index of the first local row 6796 - n - one more than the global index of the last local row 6797 6798 Note: Both output parameters can be NULL on input. 6799 $ This function requires that the matrix be preallocated. If you have not preallocated, consider using 6800 $ PetscSplitOwnership(MPI_Comm comm, PetscInt *n, PetscInt *N) 6801 $ and then MPI_Scan() to calculate prefix sums of the local sizes. 6802 6803 Level: beginner 6804 6805 .seealso: MatGetOwnershipRanges(), MatGetOwnershipRangeColumn(), MatGetOwnershipRangesColumn(), PetscSplitOwnership(), PetscSplitOwnershipBlock() 6806 6807 @*/ 6808 PetscErrorCode MatGetOwnershipRange(Mat mat,PetscInt *m,PetscInt *n) 6809 { 6810 PetscFunctionBegin; 6811 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6812 PetscValidType(mat,1); 6813 if (m) PetscValidIntPointer(m,2); 6814 if (n) PetscValidIntPointer(n,3); 6815 MatCheckPreallocated(mat,1); 6816 if (m) *m = mat->rmap->rstart; 6817 if (n) *n = mat->rmap->rend; 6818 PetscFunctionReturn(0); 6819 } 6820 6821 /*@C 6822 MatGetOwnershipRanges - Returns the range of matrix rows owned by 6823 each process 6824 6825 Not Collective, unless matrix has not been allocated, then collective on Mat 6826 6827 Input Parameters: 6828 . mat - the matrix 6829 6830 Output Parameters: 6831 . ranges - start of each processors portion plus one more than the total length at the end 6832 6833 Level: beginner 6834 6835 .seealso: MatGetOwnershipRange(), MatGetOwnershipRangeColumn(), MatGetOwnershipRangesColumn() 6836 6837 @*/ 6838 PetscErrorCode MatGetOwnershipRanges(Mat mat,const PetscInt **ranges) 6839 { 6840 PetscErrorCode ierr; 6841 6842 PetscFunctionBegin; 6843 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6844 PetscValidType(mat,1); 6845 MatCheckPreallocated(mat,1); 6846 ierr = PetscLayoutGetRanges(mat->rmap,ranges);CHKERRQ(ierr); 6847 PetscFunctionReturn(0); 6848 } 6849 6850 /*@C 6851 MatGetOwnershipRangesColumn - Returns the range of matrix columns associated with rows of a vector one multiplies by that owned by 6852 this processor. (The columns of the "diagonal blocks" for each process) 6853 6854 Not Collective, unless matrix has not been allocated, then collective on Mat 6855 6856 Input Parameters: 6857 . mat - the matrix 6858 6859 Output Parameters: 6860 . ranges - start of each processors portion plus one more then the total length at the end 6861 6862 Level: beginner 6863 6864 .seealso: MatGetOwnershipRange(), MatGetOwnershipRangeColumn(), MatGetOwnershipRanges() 6865 6866 @*/ 6867 PetscErrorCode MatGetOwnershipRangesColumn(Mat mat,const PetscInt **ranges) 6868 { 6869 PetscErrorCode ierr; 6870 6871 PetscFunctionBegin; 6872 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6873 PetscValidType(mat,1); 6874 MatCheckPreallocated(mat,1); 6875 ierr = PetscLayoutGetRanges(mat->cmap,ranges);CHKERRQ(ierr); 6876 PetscFunctionReturn(0); 6877 } 6878 6879 /*@C 6880 MatGetOwnershipIS - Get row and column ownership as index sets 6881 6882 Not Collective 6883 6884 Input Parameter: 6885 . A - matrix 6886 6887 Output Parameters: 6888 + rows - rows in which this process owns elements 6889 - cols - columns in which this process owns elements 6890 6891 Level: intermediate 6892 6893 .seealso: MatGetOwnershipRange(), MatGetOwnershipRangeColumn(), MatSetValues(), MATELEMENTAL, MATSCALAPACK 6894 @*/ 6895 PetscErrorCode MatGetOwnershipIS(Mat A,IS *rows,IS *cols) 6896 { 6897 PetscErrorCode ierr,(*f)(Mat,IS*,IS*); 6898 6899 PetscFunctionBegin; 6900 MatCheckPreallocated(A,1); 6901 ierr = PetscObjectQueryFunction((PetscObject)A,"MatGetOwnershipIS_C",&f);CHKERRQ(ierr); 6902 if (f) { 6903 ierr = (*f)(A,rows,cols);CHKERRQ(ierr); 6904 } else { /* Create a standard row-based partition, each process is responsible for ALL columns in their row block */ 6905 if (rows) {ierr = ISCreateStride(PETSC_COMM_SELF,A->rmap->n,A->rmap->rstart,1,rows);CHKERRQ(ierr);} 6906 if (cols) {ierr = ISCreateStride(PETSC_COMM_SELF,A->cmap->N,0,1,cols);CHKERRQ(ierr);} 6907 } 6908 PetscFunctionReturn(0); 6909 } 6910 6911 /*@C 6912 MatILUFactorSymbolic - Performs symbolic ILU factorization of a matrix. 6913 Uses levels of fill only, not drop tolerance. Use MatLUFactorNumeric() 6914 to complete the factorization. 6915 6916 Collective on Mat 6917 6918 Input Parameters: 6919 + mat - the matrix 6920 . row - row permutation 6921 . column - column permutation 6922 - info - structure containing 6923 $ levels - number of levels of fill. 6924 $ expected fill - as ratio of original fill. 6925 $ 1 or 0 - indicating force fill on diagonal (improves robustness for matrices 6926 missing diagonal entries) 6927 6928 Output Parameters: 6929 . fact - new matrix that has been symbolically factored 6930 6931 Notes: 6932 See Users-Manual: ch_mat for additional information about choosing the fill factor for better efficiency. 6933 6934 Most users should employ the simplified KSP interface for linear solvers 6935 instead of working directly with matrix algebra routines such as this. 6936 See, e.g., KSPCreate(). 6937 6938 Level: developer 6939 6940 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor() 6941 MatGetOrdering(), MatFactorInfo 6942 6943 Note: this uses the definition of level of fill as in Y. Saad, 2003 6944 6945 Developer Note: fortran interface is not autogenerated as the f90 6946 interface definition cannot be generated correctly [due to MatFactorInfo] 6947 6948 References: 6949 . * - Y. Saad, Iterative methods for sparse linear systems Philadelphia: Society for Industrial and Applied Mathematics, 2003 6950 @*/ 6951 PetscErrorCode MatILUFactorSymbolic(Mat fact,Mat mat,IS row,IS col,const MatFactorInfo *info) 6952 { 6953 PetscErrorCode ierr; 6954 6955 PetscFunctionBegin; 6956 PetscValidHeaderSpecific(mat,MAT_CLASSID,2); 6957 PetscValidType(mat,2); 6958 if (row) PetscValidHeaderSpecific(row,IS_CLASSID,3); 6959 if (col) PetscValidHeaderSpecific(col,IS_CLASSID,4); 6960 PetscValidPointer(info,5); 6961 PetscValidPointer(fact,1); 6962 PetscCheckFalse(info->levels < 0,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Levels of fill negative %" PetscInt_FMT,(PetscInt)info->levels); 6963 PetscCheckFalse(info->fill < 1.0,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %g",(double)info->fill); 6964 if (!fact->ops->ilufactorsymbolic) { 6965 MatSolverType stype; 6966 ierr = MatFactorGetSolverType(fact,&stype);CHKERRQ(ierr); 6967 SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s symbolic ILU using solver type %s",((PetscObject)mat)->type_name,stype); 6968 } 6969 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 6970 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6971 MatCheckPreallocated(mat,2); 6972 6973 if (!fact->trivialsymbolic) {ierr = PetscLogEventBegin(MAT_ILUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr);} 6974 ierr = (fact->ops->ilufactorsymbolic)(fact,mat,row,col,info);CHKERRQ(ierr); 6975 if (!fact->trivialsymbolic) {ierr = PetscLogEventEnd(MAT_ILUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr);} 6976 PetscFunctionReturn(0); 6977 } 6978 6979 /*@C 6980 MatICCFactorSymbolic - Performs symbolic incomplete 6981 Cholesky factorization for a symmetric matrix. Use 6982 MatCholeskyFactorNumeric() to complete the factorization. 6983 6984 Collective on Mat 6985 6986 Input Parameters: 6987 + mat - the matrix 6988 . perm - row and column permutation 6989 - info - structure containing 6990 $ levels - number of levels of fill. 6991 $ expected fill - as ratio of original fill. 6992 6993 Output Parameter: 6994 . fact - the factored matrix 6995 6996 Notes: 6997 Most users should employ the KSP interface for linear solvers 6998 instead of working directly with matrix algebra routines such as this. 6999 See, e.g., KSPCreate(). 7000 7001 Level: developer 7002 7003 .seealso: MatCholeskyFactorNumeric(), MatCholeskyFactor(), MatFactorInfo 7004 7005 Note: this uses the definition of level of fill as in Y. Saad, 2003 7006 7007 Developer Note: fortran interface is not autogenerated as the f90 7008 interface definition cannot be generated correctly [due to MatFactorInfo] 7009 7010 References: 7011 . * - Y. Saad, Iterative methods for sparse linear systems Philadelphia: Society for Industrial and Applied Mathematics, 2003 7012 @*/ 7013 PetscErrorCode MatICCFactorSymbolic(Mat fact,Mat mat,IS perm,const MatFactorInfo *info) 7014 { 7015 PetscErrorCode ierr; 7016 7017 PetscFunctionBegin; 7018 PetscValidHeaderSpecific(mat,MAT_CLASSID,2); 7019 PetscValidType(mat,2); 7020 if (perm) PetscValidHeaderSpecific(perm,IS_CLASSID,3); 7021 PetscValidPointer(info,4); 7022 PetscValidPointer(fact,1); 7023 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7024 PetscCheckFalse(info->levels < 0,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Levels negative %" PetscInt_FMT,(PetscInt) info->levels); 7025 PetscCheckFalse(info->fill < 1.0,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %g",(double)info->fill); 7026 if (!(fact)->ops->iccfactorsymbolic) { 7027 MatSolverType stype; 7028 ierr = MatFactorGetSolverType(fact,&stype);CHKERRQ(ierr); 7029 SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s symbolic ICC using solver type %s",((PetscObject)mat)->type_name,stype); 7030 } 7031 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 7032 MatCheckPreallocated(mat,2); 7033 7034 if (!fact->trivialsymbolic) {ierr = PetscLogEventBegin(MAT_ICCFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr);} 7035 ierr = (fact->ops->iccfactorsymbolic)(fact,mat,perm,info);CHKERRQ(ierr); 7036 if (!fact->trivialsymbolic) {ierr = PetscLogEventEnd(MAT_ICCFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr);} 7037 PetscFunctionReturn(0); 7038 } 7039 7040 /*@C 7041 MatCreateSubMatrices - Extracts several submatrices from a matrix. If submat 7042 points to an array of valid matrices, they may be reused to store the new 7043 submatrices. 7044 7045 Collective on Mat 7046 7047 Input Parameters: 7048 + mat - the matrix 7049 . n - the number of submatrixes to be extracted (on this processor, may be zero) 7050 . irow, icol - index sets of rows and columns to extract 7051 - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 7052 7053 Output Parameter: 7054 . submat - the array of submatrices 7055 7056 Notes: 7057 MatCreateSubMatrices() can extract ONLY sequential submatrices 7058 (from both sequential and parallel matrices). Use MatCreateSubMatrix() 7059 to extract a parallel submatrix. 7060 7061 Some matrix types place restrictions on the row and column 7062 indices, such as that they be sorted or that they be equal to each other. 7063 7064 The index sets may not have duplicate entries. 7065 7066 When extracting submatrices from a parallel matrix, each processor can 7067 form a different submatrix by setting the rows and columns of its 7068 individual index sets according to the local submatrix desired. 7069 7070 When finished using the submatrices, the user should destroy 7071 them with MatDestroySubMatrices(). 7072 7073 MAT_REUSE_MATRIX can only be used when the nonzero structure of the 7074 original matrix has not changed from that last call to MatCreateSubMatrices(). 7075 7076 This routine creates the matrices in submat; you should NOT create them before 7077 calling it. It also allocates the array of matrix pointers submat. 7078 7079 For BAIJ matrices the index sets must respect the block structure, that is if they 7080 request one row/column in a block, they must request all rows/columns that are in 7081 that block. For example, if the block size is 2 you cannot request just row 0 and 7082 column 0. 7083 7084 Fortran Note: 7085 The Fortran interface is slightly different from that given below; it 7086 requires one to pass in as submat a Mat (integer) array of size at least n+1. 7087 7088 Level: advanced 7089 7090 .seealso: MatDestroySubMatrices(), MatCreateSubMatrix(), MatGetRow(), MatGetDiagonal(), MatReuse 7091 @*/ 7092 PetscErrorCode MatCreateSubMatrices(Mat mat,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *submat[]) 7093 { 7094 PetscErrorCode ierr; 7095 PetscInt i; 7096 PetscBool eq; 7097 7098 PetscFunctionBegin; 7099 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7100 PetscValidType(mat,1); 7101 if (n) { 7102 PetscValidPointer(irow,3); 7103 PetscValidHeaderSpecific(*irow,IS_CLASSID,3); 7104 PetscValidPointer(icol,4); 7105 PetscValidHeaderSpecific(*icol,IS_CLASSID,4); 7106 } 7107 PetscValidPointer(submat,6); 7108 if (n && scall == MAT_REUSE_MATRIX) { 7109 PetscValidPointer(*submat,6); 7110 PetscValidHeaderSpecific(**submat,MAT_CLASSID,6); 7111 } 7112 PetscCheckFalse(!mat->ops->createsubmatrices,PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 7113 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 7114 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7115 MatCheckPreallocated(mat,1); 7116 7117 ierr = PetscLogEventBegin(MAT_CreateSubMats,mat,0,0,0);CHKERRQ(ierr); 7118 ierr = (*mat->ops->createsubmatrices)(mat,n,irow,icol,scall,submat);CHKERRQ(ierr); 7119 ierr = PetscLogEventEnd(MAT_CreateSubMats,mat,0,0,0);CHKERRQ(ierr); 7120 for (i=0; i<n; i++) { 7121 (*submat)[i]->factortype = MAT_FACTOR_NONE; /* in case in place factorization was previously done on submatrix */ 7122 ierr = ISEqualUnsorted(irow[i],icol[i],&eq);CHKERRQ(ierr); 7123 if (eq) { 7124 ierr = MatPropagateSymmetryOptions(mat,(*submat)[i]);CHKERRQ(ierr); 7125 } 7126 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 7127 if (mat->boundtocpu && mat->bindingpropagates) { 7128 ierr = MatBindToCPU((*submat)[i],PETSC_TRUE);CHKERRQ(ierr); 7129 ierr = MatSetBindingPropagates((*submat)[i],PETSC_TRUE);CHKERRQ(ierr); 7130 } 7131 #endif 7132 } 7133 PetscFunctionReturn(0); 7134 } 7135 7136 /*@C 7137 MatCreateSubMatricesMPI - Extracts MPI submatrices across a sub communicator of mat (by pairs of IS that may live on subcomms). 7138 7139 Collective on Mat 7140 7141 Input Parameters: 7142 + mat - the matrix 7143 . n - the number of submatrixes to be extracted 7144 . irow, icol - index sets of rows and columns to extract 7145 - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 7146 7147 Output Parameter: 7148 . submat - the array of submatrices 7149 7150 Level: advanced 7151 7152 .seealso: MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRow(), MatGetDiagonal(), MatReuse 7153 @*/ 7154 PetscErrorCode MatCreateSubMatricesMPI(Mat mat,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *submat[]) 7155 { 7156 PetscErrorCode ierr; 7157 PetscInt i; 7158 PetscBool eq; 7159 7160 PetscFunctionBegin; 7161 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7162 PetscValidType(mat,1); 7163 if (n) { 7164 PetscValidPointer(irow,3); 7165 PetscValidHeaderSpecific(*irow,IS_CLASSID,3); 7166 PetscValidPointer(icol,4); 7167 PetscValidHeaderSpecific(*icol,IS_CLASSID,4); 7168 } 7169 PetscValidPointer(submat,6); 7170 if (n && scall == MAT_REUSE_MATRIX) { 7171 PetscValidPointer(*submat,6); 7172 PetscValidHeaderSpecific(**submat,MAT_CLASSID,6); 7173 } 7174 PetscCheckFalse(!mat->ops->createsubmatricesmpi,PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 7175 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 7176 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7177 MatCheckPreallocated(mat,1); 7178 7179 ierr = PetscLogEventBegin(MAT_CreateSubMats,mat,0,0,0);CHKERRQ(ierr); 7180 ierr = (*mat->ops->createsubmatricesmpi)(mat,n,irow,icol,scall,submat);CHKERRQ(ierr); 7181 ierr = PetscLogEventEnd(MAT_CreateSubMats,mat,0,0,0);CHKERRQ(ierr); 7182 for (i=0; i<n; i++) { 7183 ierr = ISEqualUnsorted(irow[i],icol[i],&eq);CHKERRQ(ierr); 7184 if (eq) { 7185 ierr = MatPropagateSymmetryOptions(mat,(*submat)[i]);CHKERRQ(ierr); 7186 } 7187 } 7188 PetscFunctionReturn(0); 7189 } 7190 7191 /*@C 7192 MatDestroyMatrices - Destroys an array of matrices. 7193 7194 Collective on Mat 7195 7196 Input Parameters: 7197 + n - the number of local matrices 7198 - mat - the matrices (note that this is a pointer to the array of matrices) 7199 7200 Level: advanced 7201 7202 Notes: 7203 Frees not only the matrices, but also the array that contains the matrices 7204 In Fortran will not free the array. 7205 7206 .seealso: MatCreateSubMatrices() MatDestroySubMatrices() 7207 @*/ 7208 PetscErrorCode MatDestroyMatrices(PetscInt n,Mat *mat[]) 7209 { 7210 PetscErrorCode ierr; 7211 PetscInt i; 7212 7213 PetscFunctionBegin; 7214 if (!*mat) PetscFunctionReturn(0); 7215 PetscCheckFalse(n < 0,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Trying to destroy negative number of matrices %" PetscInt_FMT,n); 7216 PetscValidPointer(mat,2); 7217 7218 for (i=0; i<n; i++) { 7219 ierr = MatDestroy(&(*mat)[i]);CHKERRQ(ierr); 7220 } 7221 7222 /* memory is allocated even if n = 0 */ 7223 ierr = PetscFree(*mat);CHKERRQ(ierr); 7224 PetscFunctionReturn(0); 7225 } 7226 7227 /*@C 7228 MatDestroySubMatrices - Destroys a set of matrices obtained with MatCreateSubMatrices(). 7229 7230 Collective on Mat 7231 7232 Input Parameters: 7233 + n - the number of local matrices 7234 - mat - the matrices (note that this is a pointer to the array of matrices, just to match the calling 7235 sequence of MatCreateSubMatrices()) 7236 7237 Level: advanced 7238 7239 Notes: 7240 Frees not only the matrices, but also the array that contains the matrices 7241 In Fortran will not free the array. 7242 7243 .seealso: MatCreateSubMatrices() 7244 @*/ 7245 PetscErrorCode MatDestroySubMatrices(PetscInt n,Mat *mat[]) 7246 { 7247 PetscErrorCode ierr; 7248 Mat mat0; 7249 7250 PetscFunctionBegin; 7251 if (!*mat) PetscFunctionReturn(0); 7252 /* mat[] is an array of length n+1, see MatCreateSubMatrices_xxx() */ 7253 PetscCheckFalse(n < 0,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Trying to destroy negative number of matrices %" PetscInt_FMT,n); 7254 PetscValidPointer(mat,2); 7255 7256 mat0 = (*mat)[0]; 7257 if (mat0 && mat0->ops->destroysubmatrices) { 7258 ierr = (mat0->ops->destroysubmatrices)(n,mat);CHKERRQ(ierr); 7259 } else { 7260 ierr = MatDestroyMatrices(n,mat);CHKERRQ(ierr); 7261 } 7262 PetscFunctionReturn(0); 7263 } 7264 7265 /*@C 7266 MatGetSeqNonzeroStructure - Extracts the sequential nonzero structure from a matrix. 7267 7268 Collective on Mat 7269 7270 Input Parameters: 7271 . mat - the matrix 7272 7273 Output Parameter: 7274 . matstruct - the sequential matrix with the nonzero structure of mat 7275 7276 Level: intermediate 7277 7278 .seealso: MatDestroySeqNonzeroStructure(), MatCreateSubMatrices(), MatDestroyMatrices() 7279 @*/ 7280 PetscErrorCode MatGetSeqNonzeroStructure(Mat mat,Mat *matstruct) 7281 { 7282 PetscErrorCode ierr; 7283 7284 PetscFunctionBegin; 7285 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7286 PetscValidPointer(matstruct,2); 7287 7288 PetscValidType(mat,1); 7289 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7290 MatCheckPreallocated(mat,1); 7291 7292 PetscCheckFalse(!mat->ops->getseqnonzerostructure,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Not for matrix type %s",((PetscObject)mat)->type_name); 7293 ierr = PetscLogEventBegin(MAT_GetSeqNonzeroStructure,mat,0,0,0);CHKERRQ(ierr); 7294 ierr = (*mat->ops->getseqnonzerostructure)(mat,matstruct);CHKERRQ(ierr); 7295 ierr = PetscLogEventEnd(MAT_GetSeqNonzeroStructure,mat,0,0,0);CHKERRQ(ierr); 7296 PetscFunctionReturn(0); 7297 } 7298 7299 /*@C 7300 MatDestroySeqNonzeroStructure - Destroys matrix obtained with MatGetSeqNonzeroStructure(). 7301 7302 Collective on Mat 7303 7304 Input Parameters: 7305 . mat - the matrix (note that this is a pointer to the array of matrices, just to match the calling 7306 sequence of MatGetSequentialNonzeroStructure()) 7307 7308 Level: advanced 7309 7310 Notes: 7311 Frees not only the matrices, but also the array that contains the matrices 7312 7313 .seealso: MatGetSeqNonzeroStructure() 7314 @*/ 7315 PetscErrorCode MatDestroySeqNonzeroStructure(Mat *mat) 7316 { 7317 PetscErrorCode ierr; 7318 7319 PetscFunctionBegin; 7320 PetscValidPointer(mat,1); 7321 ierr = MatDestroy(mat);CHKERRQ(ierr); 7322 PetscFunctionReturn(0); 7323 } 7324 7325 /*@ 7326 MatIncreaseOverlap - Given a set of submatrices indicated by index sets, 7327 replaces the index sets by larger ones that represent submatrices with 7328 additional overlap. 7329 7330 Collective on Mat 7331 7332 Input Parameters: 7333 + mat - the matrix 7334 . n - the number of index sets 7335 . is - the array of index sets (these index sets will changed during the call) 7336 - ov - the additional overlap requested 7337 7338 Options Database: 7339 . -mat_increase_overlap_scalable - use a scalable algorithm to compute the overlap (supported by MPIAIJ matrix) 7340 7341 Level: developer 7342 7343 Developer Note: 7344 Any implementation must preserve block sizes. That is: if the row block size and the column block size of mat are equal to bs, then the output index sets must be compatible with bs. 7345 7346 .seealso: MatCreateSubMatrices() 7347 @*/ 7348 PetscErrorCode MatIncreaseOverlap(Mat mat,PetscInt n,IS is[],PetscInt ov) 7349 { 7350 PetscErrorCode ierr; 7351 PetscInt i,bs,cbs; 7352 7353 PetscFunctionBegin; 7354 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7355 PetscValidType(mat,1); 7356 PetscCheckFalse(n < 0,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Must have one or more domains, you have %" PetscInt_FMT,n); 7357 if (n) { 7358 PetscValidPointer(is,3); 7359 PetscValidHeaderSpecific(*is,IS_CLASSID,3); 7360 PetscValidLogicalCollectiveInt(*is,n,2); 7361 } 7362 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 7363 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7364 MatCheckPreallocated(mat,1); 7365 7366 if (!ov) PetscFunctionReturn(0); 7367 PetscCheckFalse(!mat->ops->increaseoverlap,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 7368 ierr = PetscLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr); 7369 ierr = (*mat->ops->increaseoverlap)(mat,n,is,ov);CHKERRQ(ierr); 7370 ierr = PetscLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr); 7371 ierr = MatGetBlockSizes(mat,&bs,&cbs);CHKERRQ(ierr); 7372 if (bs == cbs) { 7373 for (i=0; i<n; i++) { 7374 ierr = ISSetBlockSize(is[i],bs);CHKERRQ(ierr); 7375 } 7376 } 7377 PetscFunctionReturn(0); 7378 } 7379 7380 PetscErrorCode MatIncreaseOverlapSplit_Single(Mat,IS*,PetscInt); 7381 7382 /*@ 7383 MatIncreaseOverlapSplit - Given a set of submatrices indicated by index sets across 7384 a sub communicator, replaces the index sets by larger ones that represent submatrices with 7385 additional overlap. 7386 7387 Collective on Mat 7388 7389 Input Parameters: 7390 + mat - the matrix 7391 . n - the number of index sets 7392 . is - the array of index sets (these index sets will changed during the call) 7393 - ov - the additional overlap requested 7394 7395 Options Database: 7396 . -mat_increase_overlap_scalable - use a scalable algorithm to compute the overlap (supported by MPIAIJ matrix) 7397 7398 Level: developer 7399 7400 .seealso: MatCreateSubMatrices() 7401 @*/ 7402 PetscErrorCode MatIncreaseOverlapSplit(Mat mat,PetscInt n,IS is[],PetscInt ov) 7403 { 7404 PetscInt i; 7405 PetscErrorCode ierr; 7406 7407 PetscFunctionBegin; 7408 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7409 PetscValidType(mat,1); 7410 PetscCheckFalse(n < 0,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Must have one or more domains, you have %" PetscInt_FMT,n); 7411 if (n) { 7412 PetscValidPointer(is,3); 7413 PetscValidHeaderSpecific(*is,IS_CLASSID,3); 7414 } 7415 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 7416 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7417 MatCheckPreallocated(mat,1); 7418 if (!ov) PetscFunctionReturn(0); 7419 ierr = PetscLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr); 7420 for (i=0; i<n; i++) { 7421 ierr = MatIncreaseOverlapSplit_Single(mat,&is[i],ov);CHKERRQ(ierr); 7422 } 7423 ierr = PetscLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr); 7424 PetscFunctionReturn(0); 7425 } 7426 7427 /*@ 7428 MatGetBlockSize - Returns the matrix block size. 7429 7430 Not Collective 7431 7432 Input Parameter: 7433 . mat - the matrix 7434 7435 Output Parameter: 7436 . bs - block size 7437 7438 Notes: 7439 Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ. These formats ALWAYS have square block storage in the matrix. 7440 7441 If the block size has not been set yet this routine returns 1. 7442 7443 Level: intermediate 7444 7445 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSizes() 7446 @*/ 7447 PetscErrorCode MatGetBlockSize(Mat mat,PetscInt *bs) 7448 { 7449 PetscFunctionBegin; 7450 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7451 PetscValidIntPointer(bs,2); 7452 *bs = PetscAbs(mat->rmap->bs); 7453 PetscFunctionReturn(0); 7454 } 7455 7456 /*@ 7457 MatGetBlockSizes - Returns the matrix block row and column sizes. 7458 7459 Not Collective 7460 7461 Input Parameter: 7462 . mat - the matrix 7463 7464 Output Parameters: 7465 + rbs - row block size 7466 - cbs - column block size 7467 7468 Notes: 7469 Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ. These formats ALWAYS have square block storage in the matrix. 7470 If you pass a different block size for the columns than the rows, the row block size determines the square block storage. 7471 7472 If a block size has not been set yet this routine returns 1. 7473 7474 Level: intermediate 7475 7476 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSize(), MatSetBlockSizes() 7477 @*/ 7478 PetscErrorCode MatGetBlockSizes(Mat mat,PetscInt *rbs, PetscInt *cbs) 7479 { 7480 PetscFunctionBegin; 7481 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7482 if (rbs) PetscValidIntPointer(rbs,2); 7483 if (cbs) PetscValidIntPointer(cbs,3); 7484 if (rbs) *rbs = PetscAbs(mat->rmap->bs); 7485 if (cbs) *cbs = PetscAbs(mat->cmap->bs); 7486 PetscFunctionReturn(0); 7487 } 7488 7489 /*@ 7490 MatSetBlockSize - Sets the matrix block size. 7491 7492 Logically Collective on Mat 7493 7494 Input Parameters: 7495 + mat - the matrix 7496 - bs - block size 7497 7498 Notes: 7499 Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ. These formats ALWAYS have square block storage in the matrix. 7500 This must be called before MatSetUp() or MatXXXSetPreallocation() (or will default to 1) and the block size cannot be changed later. 7501 7502 For MATMPIAIJ and MATSEQAIJ matrix formats, this function can be called at a later stage, provided that the specified block size 7503 is compatible with the matrix local sizes. 7504 7505 Level: intermediate 7506 7507 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSizes(), MatGetBlockSizes() 7508 @*/ 7509 PetscErrorCode MatSetBlockSize(Mat mat,PetscInt bs) 7510 { 7511 PetscErrorCode ierr; 7512 7513 PetscFunctionBegin; 7514 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7515 PetscValidLogicalCollectiveInt(mat,bs,2); 7516 ierr = MatSetBlockSizes(mat,bs,bs);CHKERRQ(ierr); 7517 PetscFunctionReturn(0); 7518 } 7519 7520 /*@ 7521 MatSetVariableBlockSizes - Sets diagonal point-blocks of the matrix that need not be of the same size 7522 7523 Logically Collective on Mat 7524 7525 Input Parameters: 7526 + mat - the matrix 7527 . nblocks - the number of blocks on this process 7528 - bsizes - the block sizes 7529 7530 Notes: 7531 Currently used by PCVPBJACOBI for AIJ matrices 7532 7533 Each variable point-block set of degrees of freedom must live on a single MPI rank. That is a point block cannot straddle two MPI ranks. 7534 7535 Level: intermediate 7536 7537 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSizes(), MatGetBlockSizes(), MatGetVariableBlockSizes(), PCVPBJACOBI 7538 @*/ 7539 PetscErrorCode MatSetVariableBlockSizes(Mat mat,PetscInt nblocks,PetscInt *bsizes) 7540 { 7541 PetscErrorCode ierr; 7542 PetscInt i,ncnt = 0, nlocal; 7543 7544 PetscFunctionBegin; 7545 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7546 PetscCheckFalse(nblocks < 0,PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Number of local blocks must be great than or equal to zero"); 7547 ierr = MatGetLocalSize(mat,&nlocal,NULL);CHKERRQ(ierr); 7548 for (i=0; i<nblocks; i++) ncnt += bsizes[i]; 7549 PetscCheckFalse(ncnt != nlocal,PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Sum of local block sizes %" PetscInt_FMT " does not equal local size of matrix %" PetscInt_FMT,ncnt,nlocal); 7550 ierr = PetscFree(mat->bsizes);CHKERRQ(ierr); 7551 mat->nblocks = nblocks; 7552 ierr = PetscMalloc1(nblocks,&mat->bsizes);CHKERRQ(ierr); 7553 ierr = PetscArraycpy(mat->bsizes,bsizes,nblocks);CHKERRQ(ierr); 7554 PetscFunctionReturn(0); 7555 } 7556 7557 /*@C 7558 MatGetVariableBlockSizes - Gets a diagonal blocks of the matrix that need not be of the same size 7559 7560 Logically Collective on Mat 7561 7562 Input Parameter: 7563 . mat - the matrix 7564 7565 Output Parameters: 7566 + nblocks - the number of blocks on this process 7567 - bsizes - the block sizes 7568 7569 Notes: Currently not supported from Fortran 7570 7571 Level: intermediate 7572 7573 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSizes(), MatGetBlockSizes(), MatSetVariableBlockSizes() 7574 @*/ 7575 PetscErrorCode MatGetVariableBlockSizes(Mat mat,PetscInt *nblocks,const PetscInt **bsizes) 7576 { 7577 PetscFunctionBegin; 7578 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7579 *nblocks = mat->nblocks; 7580 *bsizes = mat->bsizes; 7581 PetscFunctionReturn(0); 7582 } 7583 7584 /*@ 7585 MatSetBlockSizes - Sets the matrix block row and column sizes. 7586 7587 Logically Collective on Mat 7588 7589 Input Parameters: 7590 + mat - the matrix 7591 . rbs - row block size 7592 - cbs - column block size 7593 7594 Notes: 7595 Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ. These formats ALWAYS have square block storage in the matrix. 7596 If you pass a different block size for the columns than the rows, the row block size determines the square block storage. 7597 This must be called before MatSetUp() or MatXXXSetPreallocation() (or will default to 1) and the block size cannot be changed later. 7598 7599 For MATMPIAIJ and MATSEQAIJ matrix formats, this function can be called at a later stage, provided that the specified block sizes 7600 are compatible with the matrix local sizes. 7601 7602 The row and column block size determine the blocksize of the "row" and "column" vectors returned by MatCreateVecs(). 7603 7604 Level: intermediate 7605 7606 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSize(), MatGetBlockSizes() 7607 @*/ 7608 PetscErrorCode MatSetBlockSizes(Mat mat,PetscInt rbs,PetscInt cbs) 7609 { 7610 PetscErrorCode ierr; 7611 7612 PetscFunctionBegin; 7613 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7614 PetscValidLogicalCollectiveInt(mat,rbs,2); 7615 PetscValidLogicalCollectiveInt(mat,cbs,3); 7616 if (mat->ops->setblocksizes) { 7617 ierr = (*mat->ops->setblocksizes)(mat,rbs,cbs);CHKERRQ(ierr); 7618 } 7619 if (mat->rmap->refcnt) { 7620 ISLocalToGlobalMapping l2g = NULL; 7621 PetscLayout nmap = NULL; 7622 7623 ierr = PetscLayoutDuplicate(mat->rmap,&nmap);CHKERRQ(ierr); 7624 if (mat->rmap->mapping) { 7625 ierr = ISLocalToGlobalMappingDuplicate(mat->rmap->mapping,&l2g);CHKERRQ(ierr); 7626 } 7627 ierr = PetscLayoutDestroy(&mat->rmap);CHKERRQ(ierr); 7628 mat->rmap = nmap; 7629 mat->rmap->mapping = l2g; 7630 } 7631 if (mat->cmap->refcnt) { 7632 ISLocalToGlobalMapping l2g = NULL; 7633 PetscLayout nmap = NULL; 7634 7635 ierr = PetscLayoutDuplicate(mat->cmap,&nmap);CHKERRQ(ierr); 7636 if (mat->cmap->mapping) { 7637 ierr = ISLocalToGlobalMappingDuplicate(mat->cmap->mapping,&l2g);CHKERRQ(ierr); 7638 } 7639 ierr = PetscLayoutDestroy(&mat->cmap);CHKERRQ(ierr); 7640 mat->cmap = nmap; 7641 mat->cmap->mapping = l2g; 7642 } 7643 ierr = PetscLayoutSetBlockSize(mat->rmap,rbs);CHKERRQ(ierr); 7644 ierr = PetscLayoutSetBlockSize(mat->cmap,cbs);CHKERRQ(ierr); 7645 PetscFunctionReturn(0); 7646 } 7647 7648 /*@ 7649 MatSetBlockSizesFromMats - Sets the matrix block row and column sizes to match a pair of matrices 7650 7651 Logically Collective on Mat 7652 7653 Input Parameters: 7654 + mat - the matrix 7655 . fromRow - matrix from which to copy row block size 7656 - fromCol - matrix from which to copy column block size (can be same as fromRow) 7657 7658 Level: developer 7659 7660 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSizes() 7661 @*/ 7662 PetscErrorCode MatSetBlockSizesFromMats(Mat mat,Mat fromRow,Mat fromCol) 7663 { 7664 PetscErrorCode ierr; 7665 7666 PetscFunctionBegin; 7667 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7668 PetscValidHeaderSpecific(fromRow,MAT_CLASSID,2); 7669 PetscValidHeaderSpecific(fromCol,MAT_CLASSID,3); 7670 if (fromRow->rmap->bs > 0) {ierr = PetscLayoutSetBlockSize(mat->rmap,fromRow->rmap->bs);CHKERRQ(ierr);} 7671 if (fromCol->cmap->bs > 0) {ierr = PetscLayoutSetBlockSize(mat->cmap,fromCol->cmap->bs);CHKERRQ(ierr);} 7672 PetscFunctionReturn(0); 7673 } 7674 7675 /*@ 7676 MatResidual - Default routine to calculate the residual. 7677 7678 Collective on Mat 7679 7680 Input Parameters: 7681 + mat - the matrix 7682 . b - the right-hand-side 7683 - x - the approximate solution 7684 7685 Output Parameter: 7686 . r - location to store the residual 7687 7688 Level: developer 7689 7690 .seealso: PCMGSetResidual() 7691 @*/ 7692 PetscErrorCode MatResidual(Mat mat,Vec b,Vec x,Vec r) 7693 { 7694 PetscErrorCode ierr; 7695 7696 PetscFunctionBegin; 7697 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7698 PetscValidHeaderSpecific(b,VEC_CLASSID,2); 7699 PetscValidHeaderSpecific(x,VEC_CLASSID,3); 7700 PetscValidHeaderSpecific(r,VEC_CLASSID,4); 7701 PetscValidType(mat,1); 7702 MatCheckPreallocated(mat,1); 7703 ierr = PetscLogEventBegin(MAT_Residual,mat,0,0,0);CHKERRQ(ierr); 7704 if (!mat->ops->residual) { 7705 ierr = MatMult(mat,x,r);CHKERRQ(ierr); 7706 ierr = VecAYPX(r,-1.0,b);CHKERRQ(ierr); 7707 } else { 7708 ierr = (*mat->ops->residual)(mat,b,x,r);CHKERRQ(ierr); 7709 } 7710 ierr = PetscLogEventEnd(MAT_Residual,mat,0,0,0);CHKERRQ(ierr); 7711 PetscFunctionReturn(0); 7712 } 7713 7714 /*@C 7715 MatGetRowIJ - Returns the compressed row storage i and j indices for sequential matrices. 7716 7717 Collective on Mat 7718 7719 Input Parameters: 7720 + mat - the matrix 7721 . shift - 0 or 1 indicating we want the indices starting at 0 or 1 7722 . symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be symmetrized 7723 - inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the 7724 inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is 7725 always used. 7726 7727 Output Parameters: 7728 + n - number of rows in the (possibly compressed) matrix 7729 . ia - the row pointers; that is ia[0] = 0, ia[row] = ia[row-1] + number of elements in that row of the matrix 7730 . ja - the column indices 7731 - done - indicates if the routine actually worked and returned appropriate ia[] and ja[] arrays; callers 7732 are responsible for handling the case when done == PETSC_FALSE and ia and ja are not set 7733 7734 Level: developer 7735 7736 Notes: 7737 You CANNOT change any of the ia[] or ja[] values. 7738 7739 Use MatRestoreRowIJ() when you are finished accessing the ia[] and ja[] values. 7740 7741 Fortran Notes: 7742 In Fortran use 7743 $ 7744 $ PetscInt ia(1), ja(1) 7745 $ PetscOffset iia, jja 7746 $ call MatGetRowIJ(mat,shift,symmetric,inodecompressed,n,ia,iia,ja,jja,done,ierr) 7747 $ ! Access the ith and jth entries via ia(iia + i) and ja(jja + j) 7748 7749 or 7750 $ 7751 $ PetscInt, pointer :: ia(:),ja(:) 7752 $ call MatGetRowIJF90(mat,shift,symmetric,inodecompressed,n,ia,ja,done,ierr) 7753 $ ! Access the ith and jth entries via ia(i) and ja(j) 7754 7755 .seealso: MatGetColumnIJ(), MatRestoreRowIJ(), MatSeqAIJGetArray() 7756 @*/ 7757 PetscErrorCode MatGetRowIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool *done) 7758 { 7759 PetscErrorCode ierr; 7760 7761 PetscFunctionBegin; 7762 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7763 PetscValidType(mat,1); 7764 PetscValidIntPointer(n,5); 7765 if (ia) PetscValidIntPointer(ia,6); 7766 if (ja) PetscValidIntPointer(ja,7); 7767 PetscValidBoolPointer(done,8); 7768 MatCheckPreallocated(mat,1); 7769 if (!mat->ops->getrowij) *done = PETSC_FALSE; 7770 else { 7771 *done = PETSC_TRUE; 7772 ierr = PetscLogEventBegin(MAT_GetRowIJ,mat,0,0,0);CHKERRQ(ierr); 7773 ierr = (*mat->ops->getrowij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr); 7774 ierr = PetscLogEventEnd(MAT_GetRowIJ,mat,0,0,0);CHKERRQ(ierr); 7775 } 7776 PetscFunctionReturn(0); 7777 } 7778 7779 /*@C 7780 MatGetColumnIJ - Returns the compressed column storage i and j indices for sequential matrices. 7781 7782 Collective on Mat 7783 7784 Input Parameters: 7785 + mat - the matrix 7786 . shift - 1 or zero indicating we want the indices starting at 0 or 1 7787 . symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 7788 symmetrized 7789 . inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the 7790 inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is 7791 always used. 7792 . n - number of columns in the (possibly compressed) matrix 7793 . ia - the column pointers; that is ia[0] = 0, ia[col] = i[col-1] + number of elements in that col of the matrix 7794 - ja - the row indices 7795 7796 Output Parameters: 7797 . done - PETSC_TRUE or PETSC_FALSE, indicating whether the values have been returned 7798 7799 Level: developer 7800 7801 .seealso: MatGetRowIJ(), MatRestoreColumnIJ() 7802 @*/ 7803 PetscErrorCode MatGetColumnIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool *done) 7804 { 7805 PetscErrorCode ierr; 7806 7807 PetscFunctionBegin; 7808 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7809 PetscValidType(mat,1); 7810 PetscValidIntPointer(n,5); 7811 if (ia) PetscValidIntPointer(ia,6); 7812 if (ja) PetscValidIntPointer(ja,7); 7813 PetscValidBoolPointer(done,8); 7814 MatCheckPreallocated(mat,1); 7815 if (!mat->ops->getcolumnij) *done = PETSC_FALSE; 7816 else { 7817 *done = PETSC_TRUE; 7818 ierr = (*mat->ops->getcolumnij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr); 7819 } 7820 PetscFunctionReturn(0); 7821 } 7822 7823 /*@C 7824 MatRestoreRowIJ - Call after you are completed with the ia,ja indices obtained with 7825 MatGetRowIJ(). 7826 7827 Collective on Mat 7828 7829 Input Parameters: 7830 + mat - the matrix 7831 . shift - 1 or zero indicating we want the indices starting at 0 or 1 7832 . symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 7833 symmetrized 7834 . inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the 7835 inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is 7836 always used. 7837 . n - size of (possibly compressed) matrix 7838 . ia - the row pointers 7839 - ja - the column indices 7840 7841 Output Parameters: 7842 . done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned 7843 7844 Note: 7845 This routine zeros out n, ia, and ja. This is to prevent accidental 7846 us of the array after it has been restored. If you pass NULL, it will 7847 not zero the pointers. Use of ia or ja after MatRestoreRowIJ() is invalid. 7848 7849 Level: developer 7850 7851 .seealso: MatGetRowIJ(), MatRestoreColumnIJ() 7852 @*/ 7853 PetscErrorCode MatRestoreRowIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool *done) 7854 { 7855 PetscErrorCode ierr; 7856 7857 PetscFunctionBegin; 7858 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7859 PetscValidType(mat,1); 7860 if (ia) PetscValidIntPointer(ia,6); 7861 if (ja) PetscValidIntPointer(ja,7); 7862 PetscValidBoolPointer(done,8); 7863 MatCheckPreallocated(mat,1); 7864 7865 if (!mat->ops->restorerowij) *done = PETSC_FALSE; 7866 else { 7867 *done = PETSC_TRUE; 7868 ierr = (*mat->ops->restorerowij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr); 7869 if (n) *n = 0; 7870 if (ia) *ia = NULL; 7871 if (ja) *ja = NULL; 7872 } 7873 PetscFunctionReturn(0); 7874 } 7875 7876 /*@C 7877 MatRestoreColumnIJ - Call after you are completed with the ia,ja indices obtained with 7878 MatGetColumnIJ(). 7879 7880 Collective on Mat 7881 7882 Input Parameters: 7883 + mat - the matrix 7884 . shift - 1 or zero indicating we want the indices starting at 0 or 1 7885 . symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 7886 symmetrized 7887 - inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the 7888 inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is 7889 always used. 7890 7891 Output Parameters: 7892 + n - size of (possibly compressed) matrix 7893 . ia - the column pointers 7894 . ja - the row indices 7895 - done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned 7896 7897 Level: developer 7898 7899 .seealso: MatGetColumnIJ(), MatRestoreRowIJ() 7900 @*/ 7901 PetscErrorCode MatRestoreColumnIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool *done) 7902 { 7903 PetscErrorCode ierr; 7904 7905 PetscFunctionBegin; 7906 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7907 PetscValidType(mat,1); 7908 if (ia) PetscValidIntPointer(ia,6); 7909 if (ja) PetscValidIntPointer(ja,7); 7910 PetscValidBoolPointer(done,8); 7911 MatCheckPreallocated(mat,1); 7912 7913 if (!mat->ops->restorecolumnij) *done = PETSC_FALSE; 7914 else { 7915 *done = PETSC_TRUE; 7916 ierr = (*mat->ops->restorecolumnij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr); 7917 if (n) *n = 0; 7918 if (ia) *ia = NULL; 7919 if (ja) *ja = NULL; 7920 } 7921 PetscFunctionReturn(0); 7922 } 7923 7924 /*@C 7925 MatColoringPatch -Used inside matrix coloring routines that 7926 use MatGetRowIJ() and/or MatGetColumnIJ(). 7927 7928 Collective on Mat 7929 7930 Input Parameters: 7931 + mat - the matrix 7932 . ncolors - max color value 7933 . n - number of entries in colorarray 7934 - colorarray - array indicating color for each column 7935 7936 Output Parameters: 7937 . iscoloring - coloring generated using colorarray information 7938 7939 Level: developer 7940 7941 .seealso: MatGetRowIJ(), MatGetColumnIJ() 7942 7943 @*/ 7944 PetscErrorCode MatColoringPatch(Mat mat,PetscInt ncolors,PetscInt n,ISColoringValue colorarray[],ISColoring *iscoloring) 7945 { 7946 PetscErrorCode ierr; 7947 7948 PetscFunctionBegin; 7949 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7950 PetscValidType(mat,1); 7951 PetscValidIntPointer(colorarray,4); 7952 PetscValidPointer(iscoloring,5); 7953 MatCheckPreallocated(mat,1); 7954 7955 if (!mat->ops->coloringpatch) { 7956 ierr = ISColoringCreate(PetscObjectComm((PetscObject)mat),ncolors,n,colorarray,PETSC_OWN_POINTER,iscoloring);CHKERRQ(ierr); 7957 } else { 7958 ierr = (*mat->ops->coloringpatch)(mat,ncolors,n,colorarray,iscoloring);CHKERRQ(ierr); 7959 } 7960 PetscFunctionReturn(0); 7961 } 7962 7963 /*@ 7964 MatSetUnfactored - Resets a factored matrix to be treated as unfactored. 7965 7966 Logically Collective on Mat 7967 7968 Input Parameter: 7969 . mat - the factored matrix to be reset 7970 7971 Notes: 7972 This routine should be used only with factored matrices formed by in-place 7973 factorization via ILU(0) (or by in-place LU factorization for the MATSEQDENSE 7974 format). This option can save memory, for example, when solving nonlinear 7975 systems with a matrix-free Newton-Krylov method and a matrix-based, in-place 7976 ILU(0) preconditioner. 7977 7978 Note that one can specify in-place ILU(0) factorization by calling 7979 .vb 7980 PCType(pc,PCILU); 7981 PCFactorSeUseInPlace(pc); 7982 .ve 7983 or by using the options -pc_type ilu -pc_factor_in_place 7984 7985 In-place factorization ILU(0) can also be used as a local 7986 solver for the blocks within the block Jacobi or additive Schwarz 7987 methods (runtime option: -sub_pc_factor_in_place). See Users-Manual: ch_pc 7988 for details on setting local solver options. 7989 7990 Most users should employ the simplified KSP interface for linear solvers 7991 instead of working directly with matrix algebra routines such as this. 7992 See, e.g., KSPCreate(). 7993 7994 Level: developer 7995 7996 .seealso: PCFactorSetUseInPlace(), PCFactorGetUseInPlace() 7997 7998 @*/ 7999 PetscErrorCode MatSetUnfactored(Mat mat) 8000 { 8001 PetscErrorCode ierr; 8002 8003 PetscFunctionBegin; 8004 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8005 PetscValidType(mat,1); 8006 MatCheckPreallocated(mat,1); 8007 mat->factortype = MAT_FACTOR_NONE; 8008 if (!mat->ops->setunfactored) PetscFunctionReturn(0); 8009 ierr = (*mat->ops->setunfactored)(mat);CHKERRQ(ierr); 8010 PetscFunctionReturn(0); 8011 } 8012 8013 /*MC 8014 MatDenseGetArrayF90 - Accesses a matrix array from Fortran90. 8015 8016 Synopsis: 8017 MatDenseGetArrayF90(Mat x,{Scalar, pointer :: xx_v(:,:)},integer ierr) 8018 8019 Not collective 8020 8021 Input Parameter: 8022 . x - matrix 8023 8024 Output Parameters: 8025 + xx_v - the Fortran90 pointer to the array 8026 - ierr - error code 8027 8028 Example of Usage: 8029 .vb 8030 PetscScalar, pointer xx_v(:,:) 8031 .... 8032 call MatDenseGetArrayF90(x,xx_v,ierr) 8033 a = xx_v(3) 8034 call MatDenseRestoreArrayF90(x,xx_v,ierr) 8035 .ve 8036 8037 Level: advanced 8038 8039 .seealso: MatDenseRestoreArrayF90(), MatDenseGetArray(), MatDenseRestoreArray(), MatSeqAIJGetArrayF90() 8040 8041 M*/ 8042 8043 /*MC 8044 MatDenseRestoreArrayF90 - Restores a matrix array that has been 8045 accessed with MatDenseGetArrayF90(). 8046 8047 Synopsis: 8048 MatDenseRestoreArrayF90(Mat x,{Scalar, pointer :: xx_v(:,:)},integer ierr) 8049 8050 Not collective 8051 8052 Input Parameters: 8053 + x - matrix 8054 - xx_v - the Fortran90 pointer to the array 8055 8056 Output Parameter: 8057 . ierr - error code 8058 8059 Example of Usage: 8060 .vb 8061 PetscScalar, pointer xx_v(:,:) 8062 .... 8063 call MatDenseGetArrayF90(x,xx_v,ierr) 8064 a = xx_v(3) 8065 call MatDenseRestoreArrayF90(x,xx_v,ierr) 8066 .ve 8067 8068 Level: advanced 8069 8070 .seealso: MatDenseGetArrayF90(), MatDenseGetArray(), MatDenseRestoreArray(), MatSeqAIJRestoreArrayF90() 8071 8072 M*/ 8073 8074 /*MC 8075 MatSeqAIJGetArrayF90 - Accesses a matrix array from Fortran90. 8076 8077 Synopsis: 8078 MatSeqAIJGetArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr) 8079 8080 Not collective 8081 8082 Input Parameter: 8083 . x - matrix 8084 8085 Output Parameters: 8086 + xx_v - the Fortran90 pointer to the array 8087 - ierr - error code 8088 8089 Example of Usage: 8090 .vb 8091 PetscScalar, pointer xx_v(:) 8092 .... 8093 call MatSeqAIJGetArrayF90(x,xx_v,ierr) 8094 a = xx_v(3) 8095 call MatSeqAIJRestoreArrayF90(x,xx_v,ierr) 8096 .ve 8097 8098 Level: advanced 8099 8100 .seealso: MatSeqAIJRestoreArrayF90(), MatSeqAIJGetArray(), MatSeqAIJRestoreArray(), MatDenseGetArrayF90() 8101 8102 M*/ 8103 8104 /*MC 8105 MatSeqAIJRestoreArrayF90 - Restores a matrix array that has been 8106 accessed with MatSeqAIJGetArrayF90(). 8107 8108 Synopsis: 8109 MatSeqAIJRestoreArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr) 8110 8111 Not collective 8112 8113 Input Parameters: 8114 + x - matrix 8115 - xx_v - the Fortran90 pointer to the array 8116 8117 Output Parameter: 8118 . ierr - error code 8119 8120 Example of Usage: 8121 .vb 8122 PetscScalar, pointer xx_v(:) 8123 .... 8124 call MatSeqAIJGetArrayF90(x,xx_v,ierr) 8125 a = xx_v(3) 8126 call MatSeqAIJRestoreArrayF90(x,xx_v,ierr) 8127 .ve 8128 8129 Level: advanced 8130 8131 .seealso: MatSeqAIJGetArrayF90(), MatSeqAIJGetArray(), MatSeqAIJRestoreArray(), MatDenseRestoreArrayF90() 8132 8133 M*/ 8134 8135 /*@ 8136 MatCreateSubMatrix - Gets a single submatrix on the same number of processors 8137 as the original matrix. 8138 8139 Collective on Mat 8140 8141 Input Parameters: 8142 + mat - the original matrix 8143 . isrow - parallel IS containing the rows this processor should obtain 8144 . 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. 8145 - cll - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 8146 8147 Output Parameter: 8148 . newmat - the new submatrix, of the same type as the old 8149 8150 Level: advanced 8151 8152 Notes: 8153 The submatrix will be able to be multiplied with vectors using the same layout as iscol. 8154 8155 Some matrix types place restrictions on the row and column indices, such 8156 as that they be sorted or that they be equal to each other. 8157 8158 The index sets may not have duplicate entries. 8159 8160 The first time this is called you should use a cll of MAT_INITIAL_MATRIX, 8161 the MatCreateSubMatrix() routine will create the newmat for you. Any additional calls 8162 to this routine with a mat of the same nonzero structure and with a call of MAT_REUSE_MATRIX 8163 will reuse the matrix generated the first time. You should call MatDestroy() on newmat when 8164 you are finished using it. 8165 8166 The communicator of the newly obtained matrix is ALWAYS the same as the communicator of 8167 the input matrix. 8168 8169 If iscol is NULL then all columns are obtained (not supported in Fortran). 8170 8171 Example usage: 8172 Consider the following 8x8 matrix with 34 non-zero values, that is 8173 assembled across 3 processors. Let's assume that proc0 owns 3 rows, 8174 proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown 8175 as follows: 8176 8177 .vb 8178 1 2 0 | 0 3 0 | 0 4 8179 Proc0 0 5 6 | 7 0 0 | 8 0 8180 9 0 10 | 11 0 0 | 12 0 8181 ------------------------------------- 8182 13 0 14 | 15 16 17 | 0 0 8183 Proc1 0 18 0 | 19 20 21 | 0 0 8184 0 0 0 | 22 23 0 | 24 0 8185 ------------------------------------- 8186 Proc2 25 26 27 | 0 0 28 | 29 0 8187 30 0 0 | 31 32 33 | 0 34 8188 .ve 8189 8190 Suppose isrow = [0 1 | 4 | 6 7] and iscol = [1 2 | 3 4 5 | 6]. The resulting submatrix is 8191 8192 .vb 8193 2 0 | 0 3 0 | 0 8194 Proc0 5 6 | 7 0 0 | 8 8195 ------------------------------- 8196 Proc1 18 0 | 19 20 21 | 0 8197 ------------------------------- 8198 Proc2 26 27 | 0 0 28 | 29 8199 0 0 | 31 32 33 | 0 8200 .ve 8201 8202 .seealso: MatCreateSubMatrices(), MatCreateSubMatricesMPI(), MatCreateSubMatrixVirtual(), MatSubMatrixVirtualUpdate() 8203 @*/ 8204 PetscErrorCode MatCreateSubMatrix(Mat mat,IS isrow,IS iscol,MatReuse cll,Mat *newmat) 8205 { 8206 PetscErrorCode ierr; 8207 PetscMPIInt size; 8208 Mat *local; 8209 IS iscoltmp; 8210 PetscBool flg; 8211 8212 PetscFunctionBegin; 8213 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8214 PetscValidHeaderSpecific(isrow,IS_CLASSID,2); 8215 if (iscol) PetscValidHeaderSpecific(iscol,IS_CLASSID,3); 8216 PetscValidPointer(newmat,5); 8217 if (cll == MAT_REUSE_MATRIX) PetscValidHeaderSpecific(*newmat,MAT_CLASSID,5); 8218 PetscValidType(mat,1); 8219 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 8220 PetscCheckFalse(cll == MAT_IGNORE_MATRIX,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Cannot use MAT_IGNORE_MATRIX"); 8221 8222 MatCheckPreallocated(mat,1); 8223 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);CHKERRMPI(ierr); 8224 8225 if (!iscol || isrow == iscol) { 8226 PetscBool stride; 8227 PetscMPIInt grabentirematrix = 0,grab; 8228 ierr = PetscObjectTypeCompare((PetscObject)isrow,ISSTRIDE,&stride);CHKERRQ(ierr); 8229 if (stride) { 8230 PetscInt first,step,n,rstart,rend; 8231 ierr = ISStrideGetInfo(isrow,&first,&step);CHKERRQ(ierr); 8232 if (step == 1) { 8233 ierr = MatGetOwnershipRange(mat,&rstart,&rend);CHKERRQ(ierr); 8234 if (rstart == first) { 8235 ierr = ISGetLocalSize(isrow,&n);CHKERRQ(ierr); 8236 if (n == rend-rstart) { 8237 grabentirematrix = 1; 8238 } 8239 } 8240 } 8241 } 8242 ierr = MPIU_Allreduce(&grabentirematrix,&grab,1,MPI_INT,MPI_MIN,PetscObjectComm((PetscObject)mat));CHKERRMPI(ierr); 8243 if (grab) { 8244 ierr = PetscInfo(mat,"Getting entire matrix as submatrix\n");CHKERRQ(ierr); 8245 if (cll == MAT_INITIAL_MATRIX) { 8246 *newmat = mat; 8247 ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr); 8248 } 8249 PetscFunctionReturn(0); 8250 } 8251 } 8252 8253 if (!iscol) { 8254 ierr = ISCreateStride(PetscObjectComm((PetscObject)mat),mat->cmap->n,mat->cmap->rstart,1,&iscoltmp);CHKERRQ(ierr); 8255 } else { 8256 iscoltmp = iscol; 8257 } 8258 8259 /* if original matrix is on just one processor then use submatrix generated */ 8260 if (mat->ops->createsubmatrices && !mat->ops->createsubmatrix && size == 1 && cll == MAT_REUSE_MATRIX) { 8261 ierr = MatCreateSubMatrices(mat,1,&isrow,&iscoltmp,MAT_REUSE_MATRIX,&newmat);CHKERRQ(ierr); 8262 goto setproperties; 8263 } else if (mat->ops->createsubmatrices && !mat->ops->createsubmatrix && size == 1) { 8264 ierr = MatCreateSubMatrices(mat,1,&isrow,&iscoltmp,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr); 8265 *newmat = *local; 8266 ierr = PetscFree(local);CHKERRQ(ierr); 8267 goto setproperties; 8268 } else if (!mat->ops->createsubmatrix) { 8269 /* Create a new matrix type that implements the operation using the full matrix */ 8270 ierr = PetscLogEventBegin(MAT_CreateSubMat,mat,0,0,0);CHKERRQ(ierr); 8271 switch (cll) { 8272 case MAT_INITIAL_MATRIX: 8273 ierr = MatCreateSubMatrixVirtual(mat,isrow,iscoltmp,newmat);CHKERRQ(ierr); 8274 break; 8275 case MAT_REUSE_MATRIX: 8276 ierr = MatSubMatrixVirtualUpdate(*newmat,mat,isrow,iscoltmp);CHKERRQ(ierr); 8277 break; 8278 default: SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Invalid MatReuse, must be either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX"); 8279 } 8280 ierr = PetscLogEventEnd(MAT_CreateSubMat,mat,0,0,0);CHKERRQ(ierr); 8281 goto setproperties; 8282 } 8283 8284 PetscCheckFalse(!mat->ops->createsubmatrix,PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 8285 ierr = PetscLogEventBegin(MAT_CreateSubMat,mat,0,0,0);CHKERRQ(ierr); 8286 ierr = (*mat->ops->createsubmatrix)(mat,isrow,iscoltmp,cll,newmat);CHKERRQ(ierr); 8287 ierr = PetscLogEventEnd(MAT_CreateSubMat,mat,0,0,0);CHKERRQ(ierr); 8288 8289 setproperties: 8290 ierr = ISEqualUnsorted(isrow,iscoltmp,&flg);CHKERRQ(ierr); 8291 if (flg) { 8292 ierr = MatPropagateSymmetryOptions(mat,*newmat);CHKERRQ(ierr); 8293 } 8294 if (!iscol) {ierr = ISDestroy(&iscoltmp);CHKERRQ(ierr);} 8295 if (*newmat && cll == MAT_INITIAL_MATRIX) {ierr = PetscObjectStateIncrease((PetscObject)*newmat);CHKERRQ(ierr);} 8296 PetscFunctionReturn(0); 8297 } 8298 8299 /*@ 8300 MatPropagateSymmetryOptions - Propagates symmetry options set on a matrix to another matrix 8301 8302 Not Collective 8303 8304 Input Parameters: 8305 + A - the matrix we wish to propagate options from 8306 - B - the matrix we wish to propagate options to 8307 8308 Level: beginner 8309 8310 Notes: Propagates the options associated to MAT_SYMMETRY_ETERNAL, MAT_STRUCTURALLY_SYMMETRIC, MAT_HERMITIAN, MAT_SPD and MAT_SYMMETRIC 8311 8312 .seealso: MatSetOption() 8313 @*/ 8314 PetscErrorCode MatPropagateSymmetryOptions(Mat A, Mat B) 8315 { 8316 PetscErrorCode ierr; 8317 8318 PetscFunctionBegin; 8319 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 8320 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 8321 if (A->symmetric_eternal) { /* symmetric_eternal does not have a corresponding *set flag */ 8322 ierr = MatSetOption(B,MAT_SYMMETRY_ETERNAL,A->symmetric_eternal);CHKERRQ(ierr); 8323 } 8324 if (A->structurally_symmetric_set) { 8325 ierr = MatSetOption(B,MAT_STRUCTURALLY_SYMMETRIC,A->structurally_symmetric);CHKERRQ(ierr); 8326 } 8327 if (A->hermitian_set) { 8328 ierr = MatSetOption(B,MAT_HERMITIAN,A->hermitian);CHKERRQ(ierr); 8329 } 8330 if (A->spd_set) { 8331 ierr = MatSetOption(B,MAT_SPD,A->spd);CHKERRQ(ierr); 8332 } 8333 if (A->symmetric_set) { 8334 ierr = MatSetOption(B,MAT_SYMMETRIC,A->symmetric);CHKERRQ(ierr); 8335 } 8336 PetscFunctionReturn(0); 8337 } 8338 8339 /*@ 8340 MatStashSetInitialSize - sets the sizes of the matrix stash, that is 8341 used during the assembly process to store values that belong to 8342 other processors. 8343 8344 Not Collective 8345 8346 Input Parameters: 8347 + mat - the matrix 8348 . size - the initial size of the stash. 8349 - bsize - the initial size of the block-stash(if used). 8350 8351 Options Database Keys: 8352 + -matstash_initial_size <size> or <size0,size1,...sizep-1> 8353 - -matstash_block_initial_size <bsize> or <bsize0,bsize1,...bsizep-1> 8354 8355 Level: intermediate 8356 8357 Notes: 8358 The block-stash is used for values set with MatSetValuesBlocked() while 8359 the stash is used for values set with MatSetValues() 8360 8361 Run with the option -info and look for output of the form 8362 MatAssemblyBegin_MPIXXX:Stash has MM entries, uses nn mallocs. 8363 to determine the appropriate value, MM, to use for size and 8364 MatAssemblyBegin_MPIXXX:Block-Stash has BMM entries, uses nn mallocs. 8365 to determine the value, BMM to use for bsize 8366 8367 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), Mat, MatStashGetInfo() 8368 8369 @*/ 8370 PetscErrorCode MatStashSetInitialSize(Mat mat,PetscInt size, PetscInt bsize) 8371 { 8372 PetscErrorCode ierr; 8373 8374 PetscFunctionBegin; 8375 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8376 PetscValidType(mat,1); 8377 ierr = MatStashSetInitialSize_Private(&mat->stash,size);CHKERRQ(ierr); 8378 ierr = MatStashSetInitialSize_Private(&mat->bstash,bsize);CHKERRQ(ierr); 8379 PetscFunctionReturn(0); 8380 } 8381 8382 /*@ 8383 MatInterpolateAdd - w = y + A*x or A'*x depending on the shape of 8384 the matrix 8385 8386 Neighbor-wise Collective on Mat 8387 8388 Input Parameters: 8389 + mat - the matrix 8390 . x,y - the vectors 8391 - w - where the result is stored 8392 8393 Level: intermediate 8394 8395 Notes: 8396 w may be the same vector as y. 8397 8398 This allows one to use either the restriction or interpolation (its transpose) 8399 matrix to do the interpolation 8400 8401 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict() 8402 8403 @*/ 8404 PetscErrorCode MatInterpolateAdd(Mat A,Vec x,Vec y,Vec w) 8405 { 8406 PetscErrorCode ierr; 8407 PetscInt M,N,Ny; 8408 8409 PetscFunctionBegin; 8410 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 8411 PetscValidHeaderSpecific(x,VEC_CLASSID,2); 8412 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 8413 PetscValidHeaderSpecific(w,VEC_CLASSID,4); 8414 ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr); 8415 ierr = VecGetSize(y,&Ny);CHKERRQ(ierr); 8416 if (M == Ny) { 8417 ierr = MatMultAdd(A,x,y,w);CHKERRQ(ierr); 8418 } else { 8419 ierr = MatMultTransposeAdd(A,x,y,w);CHKERRQ(ierr); 8420 } 8421 PetscFunctionReturn(0); 8422 } 8423 8424 /*@ 8425 MatInterpolate - y = A*x or A'*x depending on the shape of 8426 the matrix 8427 8428 Neighbor-wise Collective on Mat 8429 8430 Input Parameters: 8431 + mat - the matrix 8432 - x,y - the vectors 8433 8434 Level: intermediate 8435 8436 Notes: 8437 This allows one to use either the restriction or interpolation (its transpose) 8438 matrix to do the interpolation 8439 8440 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict() 8441 8442 @*/ 8443 PetscErrorCode MatInterpolate(Mat A,Vec x,Vec y) 8444 { 8445 PetscErrorCode ierr; 8446 PetscInt M,N,Ny; 8447 8448 PetscFunctionBegin; 8449 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 8450 PetscValidHeaderSpecific(x,VEC_CLASSID,2); 8451 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 8452 ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr); 8453 ierr = VecGetSize(y,&Ny);CHKERRQ(ierr); 8454 if (M == Ny) { 8455 ierr = MatMult(A,x,y);CHKERRQ(ierr); 8456 } else { 8457 ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr); 8458 } 8459 PetscFunctionReturn(0); 8460 } 8461 8462 /*@ 8463 MatRestrict - y = A*x or A'*x 8464 8465 Neighbor-wise Collective on Mat 8466 8467 Input Parameters: 8468 + mat - the matrix 8469 - x,y - the vectors 8470 8471 Level: intermediate 8472 8473 Notes: 8474 This allows one to use either the restriction or interpolation (its transpose) 8475 matrix to do the restriction 8476 8477 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatInterpolate() 8478 8479 @*/ 8480 PetscErrorCode MatRestrict(Mat A,Vec x,Vec y) 8481 { 8482 PetscErrorCode ierr; 8483 PetscInt M,N,Ny; 8484 8485 PetscFunctionBegin; 8486 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 8487 PetscValidHeaderSpecific(x,VEC_CLASSID,2); 8488 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 8489 ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr); 8490 ierr = VecGetSize(y,&Ny);CHKERRQ(ierr); 8491 if (M == Ny) { 8492 ierr = MatMult(A,x,y);CHKERRQ(ierr); 8493 } else { 8494 ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr); 8495 } 8496 PetscFunctionReturn(0); 8497 } 8498 8499 /*@ 8500 MatMatInterpolateAdd - Y = W + A*X or W + A'*X 8501 8502 Neighbor-wise Collective on Mat 8503 8504 Input Parameters: 8505 + mat - the matrix 8506 - w, x - the input dense matrices 8507 8508 Output Parameters: 8509 . y - the output dense matrix 8510 8511 Level: intermediate 8512 8513 Notes: 8514 This allows one to use either the restriction or interpolation (its transpose) 8515 matrix to do the interpolation. y matrix can be reused if already created with the proper sizes, 8516 otherwise it will be recreated. y must be initialized to NULL if not supplied. 8517 8518 .seealso: MatInterpolateAdd(), MatMatInterpolate(), MatMatRestrict() 8519 8520 @*/ 8521 PetscErrorCode MatMatInterpolateAdd(Mat A,Mat x,Mat w,Mat *y) 8522 { 8523 PetscErrorCode ierr; 8524 PetscInt M,N,Mx,Nx,Mo,My = 0,Ny = 0; 8525 PetscBool trans = PETSC_TRUE; 8526 MatReuse reuse = MAT_INITIAL_MATRIX; 8527 8528 PetscFunctionBegin; 8529 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 8530 PetscValidHeaderSpecific(x,MAT_CLASSID,2); 8531 PetscValidType(x,2); 8532 if (w) PetscValidHeaderSpecific(w,MAT_CLASSID,3); 8533 if (*y) PetscValidHeaderSpecific(*y,MAT_CLASSID,4); 8534 ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr); 8535 ierr = MatGetSize(x,&Mx,&Nx);CHKERRQ(ierr); 8536 if (N == Mx) trans = PETSC_FALSE; 8537 else PetscCheckFalse(M != Mx,PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Size mismatch: A %" PetscInt_FMT "x%" PetscInt_FMT ", X %" PetscInt_FMT "x%" PetscInt_FMT,M,N,Mx,Nx); 8538 Mo = trans ? N : M; 8539 if (*y) { 8540 ierr = MatGetSize(*y,&My,&Ny);CHKERRQ(ierr); 8541 if (Mo == My && Nx == Ny) { reuse = MAT_REUSE_MATRIX; } 8542 else { 8543 PetscCheckFalse(w && *y == w,PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Cannot reuse y and w, size mismatch: A %" PetscInt_FMT "x%" PetscInt_FMT ", X %" PetscInt_FMT "x%" PetscInt_FMT ", Y %" PetscInt_FMT "x%" PetscInt_FMT,M,N,Mx,Nx,My,Ny); 8544 ierr = MatDestroy(y);CHKERRQ(ierr); 8545 } 8546 } 8547 8548 if (w && *y == w) { /* this is to minimize changes in PCMG */ 8549 PetscBool flg; 8550 8551 ierr = PetscObjectQuery((PetscObject)*y,"__MatMatIntAdd_w",(PetscObject*)&w);CHKERRQ(ierr); 8552 if (w) { 8553 PetscInt My,Ny,Mw,Nw; 8554 8555 ierr = PetscObjectTypeCompare((PetscObject)*y,((PetscObject)w)->type_name,&flg);CHKERRQ(ierr); 8556 ierr = MatGetSize(*y,&My,&Ny);CHKERRQ(ierr); 8557 ierr = MatGetSize(w,&Mw,&Nw);CHKERRQ(ierr); 8558 if (!flg || My != Mw || Ny != Nw) w = NULL; 8559 } 8560 if (!w) { 8561 ierr = MatDuplicate(*y,MAT_COPY_VALUES,&w);CHKERRQ(ierr); 8562 ierr = PetscObjectCompose((PetscObject)*y,"__MatMatIntAdd_w",(PetscObject)w);CHKERRQ(ierr); 8563 ierr = PetscLogObjectParent((PetscObject)*y,(PetscObject)w);CHKERRQ(ierr); 8564 ierr = PetscObjectDereference((PetscObject)w);CHKERRQ(ierr); 8565 } else { 8566 ierr = MatCopy(*y,w,UNKNOWN_NONZERO_PATTERN);CHKERRQ(ierr); 8567 } 8568 } 8569 if (!trans) { 8570 ierr = MatMatMult(A,x,reuse,PETSC_DEFAULT,y);CHKERRQ(ierr); 8571 } else { 8572 ierr = MatTransposeMatMult(A,x,reuse,PETSC_DEFAULT,y);CHKERRQ(ierr); 8573 } 8574 if (w) { 8575 ierr = MatAXPY(*y,1.0,w,UNKNOWN_NONZERO_PATTERN);CHKERRQ(ierr); 8576 } 8577 PetscFunctionReturn(0); 8578 } 8579 8580 /*@ 8581 MatMatInterpolate - Y = A*X or A'*X 8582 8583 Neighbor-wise Collective on Mat 8584 8585 Input Parameters: 8586 + mat - the matrix 8587 - x - the input dense matrix 8588 8589 Output Parameters: 8590 . y - the output dense matrix 8591 8592 Level: intermediate 8593 8594 Notes: 8595 This allows one to use either the restriction or interpolation (its transpose) 8596 matrix to do the interpolation. y matrix can be reused if already created with the proper sizes, 8597 otherwise it will be recreated. y must be initialized to NULL if not supplied. 8598 8599 .seealso: MatInterpolate(), MatRestrict(), MatMatRestrict() 8600 8601 @*/ 8602 PetscErrorCode MatMatInterpolate(Mat A,Mat x,Mat *y) 8603 { 8604 PetscErrorCode ierr; 8605 8606 PetscFunctionBegin; 8607 ierr = MatMatInterpolateAdd(A,x,NULL,y);CHKERRQ(ierr); 8608 PetscFunctionReturn(0); 8609 } 8610 8611 /*@ 8612 MatMatRestrict - Y = A*X or A'*X 8613 8614 Neighbor-wise Collective on Mat 8615 8616 Input Parameters: 8617 + mat - the matrix 8618 - x - the input dense matrix 8619 8620 Output Parameters: 8621 . y - the output dense matrix 8622 8623 Level: intermediate 8624 8625 Notes: 8626 This allows one to use either the restriction or interpolation (its transpose) 8627 matrix to do the restriction. y matrix can be reused if already created with the proper sizes, 8628 otherwise it will be recreated. y must be initialized to NULL if not supplied. 8629 8630 .seealso: MatRestrict(), MatInterpolate(), MatMatInterpolate() 8631 @*/ 8632 PetscErrorCode MatMatRestrict(Mat A,Mat x,Mat *y) 8633 { 8634 PetscErrorCode ierr; 8635 8636 PetscFunctionBegin; 8637 ierr = MatMatInterpolateAdd(A,x,NULL,y);CHKERRQ(ierr); 8638 PetscFunctionReturn(0); 8639 } 8640 8641 /*@ 8642 MatGetNullSpace - retrieves the null space of a matrix. 8643 8644 Logically Collective on Mat 8645 8646 Input Parameters: 8647 + mat - the matrix 8648 - nullsp - the null space object 8649 8650 Level: developer 8651 8652 .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace(), MatSetNullSpace() 8653 @*/ 8654 PetscErrorCode MatGetNullSpace(Mat mat, MatNullSpace *nullsp) 8655 { 8656 PetscFunctionBegin; 8657 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8658 PetscValidPointer(nullsp,2); 8659 *nullsp = (mat->symmetric_set && mat->symmetric && !mat->nullsp) ? mat->transnullsp : mat->nullsp; 8660 PetscFunctionReturn(0); 8661 } 8662 8663 /*@ 8664 MatSetNullSpace - attaches a null space to a matrix. 8665 8666 Logically Collective on Mat 8667 8668 Input Parameters: 8669 + mat - the matrix 8670 - nullsp - the null space object 8671 8672 Level: advanced 8673 8674 Notes: 8675 This null space is used by the linear solvers. Overwrites any previous null space that may have been attached 8676 8677 For inconsistent singular systems (linear systems where the right hand side is not in the range of the operator) you also likely should 8678 call MatSetTransposeNullSpace(). This allows the linear system to be solved in a least squares sense. 8679 8680 You can remove the null space by calling this routine with an nullsp of NULL 8681 8682 The fundamental theorem of linear algebra (Gilbert Strang, Introduction to Applied Mathematics, page 72) states that 8683 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). 8684 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 8685 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 8686 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). 8687 8688 Krylov solvers can produce the minimal norm solution to the least squares problem by utilizing MatNullSpaceRemove(). 8689 8690 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 8691 routine also automatically calls MatSetTransposeNullSpace(). 8692 8693 .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace(), MatGetNullSpace(), MatSetTransposeNullSpace(), MatGetTransposeNullSpace(), MatNullSpaceRemove() 8694 @*/ 8695 PetscErrorCode MatSetNullSpace(Mat mat,MatNullSpace nullsp) 8696 { 8697 PetscErrorCode ierr; 8698 8699 PetscFunctionBegin; 8700 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8701 if (nullsp) PetscValidHeaderSpecific(nullsp,MAT_NULLSPACE_CLASSID,2); 8702 if (nullsp) {ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr);} 8703 ierr = MatNullSpaceDestroy(&mat->nullsp);CHKERRQ(ierr); 8704 mat->nullsp = nullsp; 8705 if (mat->symmetric_set && mat->symmetric) { 8706 ierr = MatSetTransposeNullSpace(mat,nullsp);CHKERRQ(ierr); 8707 } 8708 PetscFunctionReturn(0); 8709 } 8710 8711 /*@ 8712 MatGetTransposeNullSpace - retrieves the null space of the transpose of a matrix. 8713 8714 Logically Collective on Mat 8715 8716 Input Parameters: 8717 + mat - the matrix 8718 - nullsp - the null space object 8719 8720 Level: developer 8721 8722 .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace(), MatSetTransposeNullSpace(), MatSetNullSpace(), MatGetNullSpace() 8723 @*/ 8724 PetscErrorCode MatGetTransposeNullSpace(Mat mat, MatNullSpace *nullsp) 8725 { 8726 PetscFunctionBegin; 8727 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8728 PetscValidType(mat,1); 8729 PetscValidPointer(nullsp,2); 8730 *nullsp = (mat->symmetric_set && mat->symmetric && !mat->transnullsp) ? mat->nullsp : mat->transnullsp; 8731 PetscFunctionReturn(0); 8732 } 8733 8734 /*@ 8735 MatSetTransposeNullSpace - attaches a null space to a matrix. 8736 8737 Logically Collective on Mat 8738 8739 Input Parameters: 8740 + mat - the matrix 8741 - nullsp - the null space object 8742 8743 Level: advanced 8744 8745 Notes: 8746 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. 8747 You must also call MatSetNullSpace() 8748 8749 The fundamental theorem of linear algebra (Gilbert Strang, Introduction to Applied Mathematics, page 72) states that 8750 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). 8751 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 8752 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 8753 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). 8754 8755 Krylov solvers can produce the minimal norm solution to the least squares problem by utilizing MatNullSpaceRemove(). 8756 8757 .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace(), MatGetNullSpace(), MatSetNullSpace(), MatGetTransposeNullSpace(), MatNullSpaceRemove() 8758 @*/ 8759 PetscErrorCode MatSetTransposeNullSpace(Mat mat,MatNullSpace nullsp) 8760 { 8761 PetscErrorCode ierr; 8762 8763 PetscFunctionBegin; 8764 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8765 if (nullsp) PetscValidHeaderSpecific(nullsp,MAT_NULLSPACE_CLASSID,2); 8766 if (nullsp) {ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr);} 8767 ierr = MatNullSpaceDestroy(&mat->transnullsp);CHKERRQ(ierr); 8768 mat->transnullsp = nullsp; 8769 PetscFunctionReturn(0); 8770 } 8771 8772 /*@ 8773 MatSetNearNullSpace - attaches a null space to a matrix, which is often the null space (rigid body modes) of the operator without boundary conditions 8774 This null space will be used to provide near null space vectors to a multigrid preconditioner built from this matrix. 8775 8776 Logically Collective on Mat 8777 8778 Input Parameters: 8779 + mat - the matrix 8780 - nullsp - the null space object 8781 8782 Level: advanced 8783 8784 Notes: 8785 Overwrites any previous near null space that may have been attached 8786 8787 You can remove the null space by calling this routine with an nullsp of NULL 8788 8789 .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNullSpace(), MatNullSpaceCreateRigidBody(), MatGetNearNullSpace() 8790 @*/ 8791 PetscErrorCode MatSetNearNullSpace(Mat mat,MatNullSpace nullsp) 8792 { 8793 PetscErrorCode ierr; 8794 8795 PetscFunctionBegin; 8796 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8797 PetscValidType(mat,1); 8798 if (nullsp) PetscValidHeaderSpecific(nullsp,MAT_NULLSPACE_CLASSID,2); 8799 MatCheckPreallocated(mat,1); 8800 if (nullsp) {ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr);} 8801 ierr = MatNullSpaceDestroy(&mat->nearnullsp);CHKERRQ(ierr); 8802 mat->nearnullsp = nullsp; 8803 PetscFunctionReturn(0); 8804 } 8805 8806 /*@ 8807 MatGetNearNullSpace - Get null space attached with MatSetNearNullSpace() 8808 8809 Not Collective 8810 8811 Input Parameter: 8812 . mat - the matrix 8813 8814 Output Parameter: 8815 . nullsp - the null space object, NULL if not set 8816 8817 Level: developer 8818 8819 .seealso: MatSetNearNullSpace(), MatGetNullSpace(), MatNullSpaceCreate() 8820 @*/ 8821 PetscErrorCode MatGetNearNullSpace(Mat mat,MatNullSpace *nullsp) 8822 { 8823 PetscFunctionBegin; 8824 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8825 PetscValidType(mat,1); 8826 PetscValidPointer(nullsp,2); 8827 MatCheckPreallocated(mat,1); 8828 *nullsp = mat->nearnullsp; 8829 PetscFunctionReturn(0); 8830 } 8831 8832 /*@C 8833 MatICCFactor - Performs in-place incomplete Cholesky factorization of matrix. 8834 8835 Collective on Mat 8836 8837 Input Parameters: 8838 + mat - the matrix 8839 . row - row/column permutation 8840 . fill - expected fill factor >= 1.0 8841 - level - level of fill, for ICC(k) 8842 8843 Notes: 8844 Probably really in-place only when level of fill is zero, otherwise allocates 8845 new space to store factored matrix and deletes previous memory. 8846 8847 Most users should employ the simplified KSP interface for linear solvers 8848 instead of working directly with matrix algebra routines such as this. 8849 See, e.g., KSPCreate(). 8850 8851 Level: developer 8852 8853 .seealso: MatICCFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor() 8854 8855 Developer Note: fortran interface is not autogenerated as the f90 8856 interface definition cannot be generated correctly [due to MatFactorInfo] 8857 8858 @*/ 8859 PetscErrorCode MatICCFactor(Mat mat,IS row,const MatFactorInfo *info) 8860 { 8861 PetscErrorCode ierr; 8862 8863 PetscFunctionBegin; 8864 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8865 PetscValidType(mat,1); 8866 if (row) PetscValidHeaderSpecific(row,IS_CLASSID,2); 8867 PetscValidPointer(info,3); 8868 PetscCheckFalse(mat->rmap->N != mat->cmap->N,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONG,"matrix must be square"); 8869 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 8870 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 8871 PetscCheckFalse(!mat->ops->iccfactor,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 8872 MatCheckPreallocated(mat,1); 8873 ierr = (*mat->ops->iccfactor)(mat,row,info);CHKERRQ(ierr); 8874 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 8875 PetscFunctionReturn(0); 8876 } 8877 8878 /*@ 8879 MatDiagonalScaleLocal - Scales columns of a matrix given the scaling values including the 8880 ghosted ones. 8881 8882 Not Collective 8883 8884 Input Parameters: 8885 + mat - the matrix 8886 - diag = the diagonal values, including ghost ones 8887 8888 Level: developer 8889 8890 Notes: 8891 Works only for MPIAIJ and MPIBAIJ matrices 8892 8893 .seealso: MatDiagonalScale() 8894 @*/ 8895 PetscErrorCode MatDiagonalScaleLocal(Mat mat,Vec diag) 8896 { 8897 PetscErrorCode ierr; 8898 PetscMPIInt size; 8899 8900 PetscFunctionBegin; 8901 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8902 PetscValidHeaderSpecific(diag,VEC_CLASSID,2); 8903 PetscValidType(mat,1); 8904 8905 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled"); 8906 ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 8907 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);CHKERRMPI(ierr); 8908 if (size == 1) { 8909 PetscInt n,m; 8910 ierr = VecGetSize(diag,&n);CHKERRQ(ierr); 8911 ierr = MatGetSize(mat,NULL,&m);CHKERRQ(ierr); 8912 if (m == n) { 8913 ierr = MatDiagonalScale(mat,NULL,diag);CHKERRQ(ierr); 8914 } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only supported for sequential matrices when no ghost points/periodic conditions"); 8915 } else { 8916 ierr = PetscUseMethod(mat,"MatDiagonalScaleLocal_C",(Mat,Vec),(mat,diag));CHKERRQ(ierr); 8917 } 8918 ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 8919 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 8920 PetscFunctionReturn(0); 8921 } 8922 8923 /*@ 8924 MatGetInertia - Gets the inertia from a factored matrix 8925 8926 Collective on Mat 8927 8928 Input Parameter: 8929 . mat - the matrix 8930 8931 Output Parameters: 8932 + nneg - number of negative eigenvalues 8933 . nzero - number of zero eigenvalues 8934 - npos - number of positive eigenvalues 8935 8936 Level: advanced 8937 8938 Notes: 8939 Matrix must have been factored by MatCholeskyFactor() 8940 8941 @*/ 8942 PetscErrorCode MatGetInertia(Mat mat,PetscInt *nneg,PetscInt *nzero,PetscInt *npos) 8943 { 8944 PetscErrorCode ierr; 8945 8946 PetscFunctionBegin; 8947 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8948 PetscValidType(mat,1); 8949 PetscCheckFalse(!mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 8950 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Numeric factor mat is not assembled"); 8951 PetscCheckFalse(!mat->ops->getinertia,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 8952 ierr = (*mat->ops->getinertia)(mat,nneg,nzero,npos);CHKERRQ(ierr); 8953 PetscFunctionReturn(0); 8954 } 8955 8956 /* ----------------------------------------------------------------*/ 8957 /*@C 8958 MatSolves - Solves A x = b, given a factored matrix, for a collection of vectors 8959 8960 Neighbor-wise Collective on Mats 8961 8962 Input Parameters: 8963 + mat - the factored matrix 8964 - b - the right-hand-side vectors 8965 8966 Output Parameter: 8967 . x - the result vectors 8968 8969 Notes: 8970 The vectors b and x cannot be the same. I.e., one cannot 8971 call MatSolves(A,x,x). 8972 8973 Notes: 8974 Most users should employ the simplified KSP interface for linear solvers 8975 instead of working directly with matrix algebra routines such as this. 8976 See, e.g., KSPCreate(). 8977 8978 Level: developer 8979 8980 .seealso: MatSolveAdd(), MatSolveTranspose(), MatSolveTransposeAdd(), MatSolve() 8981 @*/ 8982 PetscErrorCode MatSolves(Mat mat,Vecs b,Vecs x) 8983 { 8984 PetscErrorCode ierr; 8985 8986 PetscFunctionBegin; 8987 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8988 PetscValidType(mat,1); 8989 PetscCheckFalse(x == b,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 8990 PetscCheckFalse(!mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 8991 if (!mat->rmap->N && !mat->cmap->N) PetscFunctionReturn(0); 8992 8993 PetscCheckFalse(!mat->ops->solves,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 8994 MatCheckPreallocated(mat,1); 8995 ierr = PetscLogEventBegin(MAT_Solves,mat,0,0,0);CHKERRQ(ierr); 8996 ierr = (*mat->ops->solves)(mat,b,x);CHKERRQ(ierr); 8997 ierr = PetscLogEventEnd(MAT_Solves,mat,0,0,0);CHKERRQ(ierr); 8998 PetscFunctionReturn(0); 8999 } 9000 9001 /*@ 9002 MatIsSymmetric - Test whether a matrix is symmetric 9003 9004 Collective on Mat 9005 9006 Input Parameters: 9007 + A - the matrix to test 9008 - tol - difference between value and its transpose less than this amount counts as equal (use 0.0 for exact transpose) 9009 9010 Output Parameters: 9011 . flg - the result 9012 9013 Notes: 9014 For real numbers MatIsSymmetric() and MatIsHermitian() return identical results 9015 9016 Level: intermediate 9017 9018 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetricKnown() 9019 @*/ 9020 PetscErrorCode MatIsSymmetric(Mat A,PetscReal tol,PetscBool *flg) 9021 { 9022 PetscErrorCode ierr; 9023 9024 PetscFunctionBegin; 9025 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 9026 PetscValidBoolPointer(flg,3); 9027 9028 if (!A->symmetric_set) { 9029 if (!A->ops->issymmetric) { 9030 MatType mattype; 9031 ierr = MatGetType(A,&mattype);CHKERRQ(ierr); 9032 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type %s does not support checking for symmetric",mattype); 9033 } 9034 ierr = (*A->ops->issymmetric)(A,tol,flg);CHKERRQ(ierr); 9035 if (!tol) { 9036 ierr = MatSetOption(A,MAT_SYMMETRIC,*flg);CHKERRQ(ierr); 9037 } 9038 } else if (A->symmetric) { 9039 *flg = PETSC_TRUE; 9040 } else if (!tol) { 9041 *flg = PETSC_FALSE; 9042 } else { 9043 if (!A->ops->issymmetric) { 9044 MatType mattype; 9045 ierr = MatGetType(A,&mattype);CHKERRQ(ierr); 9046 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type %s does not support checking for symmetric",mattype); 9047 } 9048 ierr = (*A->ops->issymmetric)(A,tol,flg);CHKERRQ(ierr); 9049 } 9050 PetscFunctionReturn(0); 9051 } 9052 9053 /*@ 9054 MatIsHermitian - Test whether a matrix is Hermitian 9055 9056 Collective on Mat 9057 9058 Input Parameters: 9059 + A - the matrix to test 9060 - tol - difference between value and its transpose less than this amount counts as equal (use 0.0 for exact Hermitian) 9061 9062 Output Parameters: 9063 . flg - the result 9064 9065 Level: intermediate 9066 9067 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), 9068 MatIsSymmetricKnown(), MatIsSymmetric() 9069 @*/ 9070 PetscErrorCode MatIsHermitian(Mat A,PetscReal tol,PetscBool *flg) 9071 { 9072 PetscErrorCode ierr; 9073 9074 PetscFunctionBegin; 9075 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 9076 PetscValidBoolPointer(flg,3); 9077 9078 if (!A->hermitian_set) { 9079 if (!A->ops->ishermitian) { 9080 MatType mattype; 9081 ierr = MatGetType(A,&mattype);CHKERRQ(ierr); 9082 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type %s does not support checking for hermitian",mattype); 9083 } 9084 ierr = (*A->ops->ishermitian)(A,tol,flg);CHKERRQ(ierr); 9085 if (!tol) { 9086 ierr = MatSetOption(A,MAT_HERMITIAN,*flg);CHKERRQ(ierr); 9087 } 9088 } else if (A->hermitian) { 9089 *flg = PETSC_TRUE; 9090 } else if (!tol) { 9091 *flg = PETSC_FALSE; 9092 } else { 9093 if (!A->ops->ishermitian) { 9094 MatType mattype; 9095 ierr = MatGetType(A,&mattype);CHKERRQ(ierr); 9096 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type %s does not support checking for hermitian",mattype); 9097 } 9098 ierr = (*A->ops->ishermitian)(A,tol,flg);CHKERRQ(ierr); 9099 } 9100 PetscFunctionReturn(0); 9101 } 9102 9103 /*@ 9104 MatIsSymmetricKnown - Checks the flag on the matrix to see if it is symmetric. 9105 9106 Not Collective 9107 9108 Input Parameter: 9109 . A - the matrix to check 9110 9111 Output Parameters: 9112 + set - if the symmetric flag is set (this tells you if the next flag is valid) 9113 - flg - the result 9114 9115 Level: advanced 9116 9117 Note: Does not check the matrix values directly, so this may return unknown (set = PETSC_FALSE). Use MatIsSymmetric() 9118 if you want it explicitly checked 9119 9120 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetric() 9121 @*/ 9122 PetscErrorCode MatIsSymmetricKnown(Mat A,PetscBool *set,PetscBool *flg) 9123 { 9124 PetscFunctionBegin; 9125 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 9126 PetscValidPointer(set,2); 9127 PetscValidBoolPointer(flg,3); 9128 if (A->symmetric_set) { 9129 *set = PETSC_TRUE; 9130 *flg = A->symmetric; 9131 } else { 9132 *set = PETSC_FALSE; 9133 } 9134 PetscFunctionReturn(0); 9135 } 9136 9137 /*@ 9138 MatIsHermitianKnown - Checks the flag on the matrix to see if it is hermitian. 9139 9140 Not Collective 9141 9142 Input Parameter: 9143 . A - the matrix to check 9144 9145 Output Parameters: 9146 + set - if the hermitian flag is set (this tells you if the next flag is valid) 9147 - flg - the result 9148 9149 Level: advanced 9150 9151 Note: Does not check the matrix values directly, so this may return unknown (set = PETSC_FALSE). Use MatIsHermitian() 9152 if you want it explicitly checked 9153 9154 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetric() 9155 @*/ 9156 PetscErrorCode MatIsHermitianKnown(Mat A,PetscBool *set,PetscBool *flg) 9157 { 9158 PetscFunctionBegin; 9159 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 9160 PetscValidPointer(set,2); 9161 PetscValidBoolPointer(flg,3); 9162 if (A->hermitian_set) { 9163 *set = PETSC_TRUE; 9164 *flg = A->hermitian; 9165 } else { 9166 *set = PETSC_FALSE; 9167 } 9168 PetscFunctionReturn(0); 9169 } 9170 9171 /*@ 9172 MatIsStructurallySymmetric - Test whether a matrix is structurally symmetric 9173 9174 Collective on Mat 9175 9176 Input Parameter: 9177 . A - the matrix to test 9178 9179 Output Parameters: 9180 . flg - the result 9181 9182 Level: intermediate 9183 9184 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsSymmetric(), MatSetOption() 9185 @*/ 9186 PetscErrorCode MatIsStructurallySymmetric(Mat A,PetscBool *flg) 9187 { 9188 PetscErrorCode ierr; 9189 9190 PetscFunctionBegin; 9191 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 9192 PetscValidBoolPointer(flg,2); 9193 if (!A->structurally_symmetric_set) { 9194 PetscCheckFalse(!A->ops->isstructurallysymmetric,PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Matrix of type %s does not support checking for structural symmetric",((PetscObject)A)->type_name); 9195 ierr = (*A->ops->isstructurallysymmetric)(A,flg);CHKERRQ(ierr); 9196 ierr = MatSetOption(A,MAT_STRUCTURALLY_SYMMETRIC,*flg);CHKERRQ(ierr); 9197 } else *flg = A->structurally_symmetric; 9198 PetscFunctionReturn(0); 9199 } 9200 9201 /*@ 9202 MatStashGetInfo - Gets how many values are currently in the matrix stash, i.e. need 9203 to be communicated to other processors during the MatAssemblyBegin/End() process 9204 9205 Not collective 9206 9207 Input Parameter: 9208 . vec - the vector 9209 9210 Output Parameters: 9211 + nstash - the size of the stash 9212 . reallocs - the number of additional mallocs incurred. 9213 . bnstash - the size of the block stash 9214 - breallocs - the number of additional mallocs incurred.in the block stash 9215 9216 Level: advanced 9217 9218 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), Mat, MatStashSetInitialSize() 9219 9220 @*/ 9221 PetscErrorCode MatStashGetInfo(Mat mat,PetscInt *nstash,PetscInt *reallocs,PetscInt *bnstash,PetscInt *breallocs) 9222 { 9223 PetscErrorCode ierr; 9224 9225 PetscFunctionBegin; 9226 ierr = MatStashGetInfo_Private(&mat->stash,nstash,reallocs);CHKERRQ(ierr); 9227 ierr = MatStashGetInfo_Private(&mat->bstash,bnstash,breallocs);CHKERRQ(ierr); 9228 PetscFunctionReturn(0); 9229 } 9230 9231 /*@C 9232 MatCreateVecs - Get vector(s) compatible with the matrix, i.e. with the same 9233 parallel layout 9234 9235 Collective on Mat 9236 9237 Input Parameter: 9238 . mat - the matrix 9239 9240 Output Parameters: 9241 + right - (optional) vector that the matrix can be multiplied against 9242 - left - (optional) vector that the matrix vector product can be stored in 9243 9244 Notes: 9245 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(). 9246 9247 Notes: 9248 These are new vectors which are not owned by the Mat, they should be destroyed in VecDestroy() when no longer needed 9249 9250 Level: advanced 9251 9252 .seealso: MatCreate(), VecDestroy() 9253 @*/ 9254 PetscErrorCode MatCreateVecs(Mat mat,Vec *right,Vec *left) 9255 { 9256 PetscErrorCode ierr; 9257 9258 PetscFunctionBegin; 9259 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 9260 PetscValidType(mat,1); 9261 if (mat->ops->getvecs) { 9262 ierr = (*mat->ops->getvecs)(mat,right,left);CHKERRQ(ierr); 9263 } else { 9264 PetscInt rbs,cbs; 9265 ierr = MatGetBlockSizes(mat,&rbs,&cbs);CHKERRQ(ierr); 9266 if (right) { 9267 PetscCheckFalse(mat->cmap->n < 0,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"PetscLayout for columns not yet setup"); 9268 ierr = VecCreate(PetscObjectComm((PetscObject)mat),right);CHKERRQ(ierr); 9269 ierr = VecSetSizes(*right,mat->cmap->n,PETSC_DETERMINE);CHKERRQ(ierr); 9270 ierr = VecSetBlockSize(*right,cbs);CHKERRQ(ierr); 9271 ierr = VecSetType(*right,mat->defaultvectype);CHKERRQ(ierr); 9272 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 9273 if (mat->boundtocpu && mat->bindingpropagates) { 9274 ierr = VecSetBindingPropagates(*right,PETSC_TRUE);CHKERRQ(ierr); 9275 ierr = VecBindToCPU(*right,PETSC_TRUE);CHKERRQ(ierr); 9276 } 9277 #endif 9278 ierr = PetscLayoutReference(mat->cmap,&(*right)->map);CHKERRQ(ierr); 9279 } 9280 if (left) { 9281 PetscCheckFalse(mat->rmap->n < 0,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"PetscLayout for rows not yet setup"); 9282 ierr = VecCreate(PetscObjectComm((PetscObject)mat),left);CHKERRQ(ierr); 9283 ierr = VecSetSizes(*left,mat->rmap->n,PETSC_DETERMINE);CHKERRQ(ierr); 9284 ierr = VecSetBlockSize(*left,rbs);CHKERRQ(ierr); 9285 ierr = VecSetType(*left,mat->defaultvectype);CHKERRQ(ierr); 9286 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 9287 if (mat->boundtocpu && mat->bindingpropagates) { 9288 ierr = VecSetBindingPropagates(*left,PETSC_TRUE);CHKERRQ(ierr); 9289 ierr = VecBindToCPU(*left,PETSC_TRUE);CHKERRQ(ierr); 9290 } 9291 #endif 9292 ierr = PetscLayoutReference(mat->rmap,&(*left)->map);CHKERRQ(ierr); 9293 } 9294 } 9295 PetscFunctionReturn(0); 9296 } 9297 9298 /*@C 9299 MatFactorInfoInitialize - Initializes a MatFactorInfo data structure 9300 with default values. 9301 9302 Not Collective 9303 9304 Input Parameters: 9305 . info - the MatFactorInfo data structure 9306 9307 Notes: 9308 The solvers are generally used through the KSP and PC objects, for example 9309 PCLU, PCILU, PCCHOLESKY, PCICC 9310 9311 Level: developer 9312 9313 .seealso: MatFactorInfo 9314 9315 Developer Note: fortran interface is not autogenerated as the f90 9316 interface definition cannot be generated correctly [due to MatFactorInfo] 9317 9318 @*/ 9319 9320 PetscErrorCode MatFactorInfoInitialize(MatFactorInfo *info) 9321 { 9322 PetscErrorCode ierr; 9323 9324 PetscFunctionBegin; 9325 ierr = PetscMemzero(info,sizeof(MatFactorInfo));CHKERRQ(ierr); 9326 PetscFunctionReturn(0); 9327 } 9328 9329 /*@ 9330 MatFactorSetSchurIS - Set indices corresponding to the Schur complement you wish to have computed 9331 9332 Collective on Mat 9333 9334 Input Parameters: 9335 + mat - the factored matrix 9336 - is - the index set defining the Schur indices (0-based) 9337 9338 Notes: 9339 Call MatFactorSolveSchurComplement() or MatFactorSolveSchurComplementTranspose() after this call to solve a Schur complement system. 9340 9341 You can call MatFactorGetSchurComplement() or MatFactorCreateSchurComplement() after this call. 9342 9343 Level: developer 9344 9345 .seealso: MatGetFactor(), MatFactorGetSchurComplement(), MatFactorRestoreSchurComplement(), MatFactorCreateSchurComplement(), MatFactorSolveSchurComplement(), 9346 MatFactorSolveSchurComplementTranspose(), MatFactorSolveSchurComplement() 9347 9348 @*/ 9349 PetscErrorCode MatFactorSetSchurIS(Mat mat,IS is) 9350 { 9351 PetscErrorCode ierr,(*f)(Mat,IS); 9352 9353 PetscFunctionBegin; 9354 PetscValidType(mat,1); 9355 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 9356 PetscValidType(is,2); 9357 PetscValidHeaderSpecific(is,IS_CLASSID,2); 9358 PetscCheckSameComm(mat,1,is,2); 9359 PetscCheckFalse(!mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Only for factored matrix"); 9360 ierr = PetscObjectQueryFunction((PetscObject)mat,"MatFactorSetSchurIS_C",&f);CHKERRQ(ierr); 9361 PetscCheckFalse(!f,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"The selected MatSolverType does not support Schur complement computation. You should use MATSOLVERMUMPS or MATSOLVERMKL_PARDISO"); 9362 ierr = MatDestroy(&mat->schur);CHKERRQ(ierr); 9363 ierr = (*f)(mat,is);CHKERRQ(ierr); 9364 PetscCheckFalse(!mat->schur,PetscObjectComm((PetscObject)mat),PETSC_ERR_PLIB,"Schur complement has not been created"); 9365 PetscFunctionReturn(0); 9366 } 9367 9368 /*@ 9369 MatFactorCreateSchurComplement - Create a Schur complement matrix object using Schur data computed during the factorization step 9370 9371 Logically Collective on Mat 9372 9373 Input Parameters: 9374 + F - the factored matrix obtained by calling MatGetFactor() from PETSc-MUMPS interface 9375 . S - location where to return the Schur complement, can be NULL 9376 - status - the status of the Schur complement matrix, can be NULL 9377 9378 Notes: 9379 You must call MatFactorSetSchurIS() before calling this routine. 9380 9381 The routine provides a copy of the Schur matrix stored within the solver data structures. 9382 The caller must destroy the object when it is no longer needed. 9383 If MatFactorInvertSchurComplement() has been called, the routine gets back the inverse. 9384 9385 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) 9386 9387 Developer Notes: 9388 The reason this routine exists is because the representation of the Schur complement within the factor matrix may be different than a standard PETSc 9389 matrix representation and we normally do not want to use the time or memory to make a copy as a regular PETSc matrix. 9390 9391 See MatCreateSchurComplement() or MatGetSchurComplement() for ways to create virtual or approximate Schur complements. 9392 9393 Level: advanced 9394 9395 References: 9396 9397 .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorGetSchurComplement(), MatFactorSchurStatus 9398 @*/ 9399 PetscErrorCode MatFactorCreateSchurComplement(Mat F,Mat* S,MatFactorSchurStatus* status) 9400 { 9401 PetscErrorCode ierr; 9402 9403 PetscFunctionBegin; 9404 PetscValidHeaderSpecific(F,MAT_CLASSID,1); 9405 if (S) PetscValidPointer(S,2); 9406 if (status) PetscValidPointer(status,3); 9407 if (S) { 9408 PetscErrorCode (*f)(Mat,Mat*); 9409 9410 ierr = PetscObjectQueryFunction((PetscObject)F,"MatFactorCreateSchurComplement_C",&f);CHKERRQ(ierr); 9411 if (f) { 9412 ierr = (*f)(F,S);CHKERRQ(ierr); 9413 } else { 9414 ierr = MatDuplicate(F->schur,MAT_COPY_VALUES,S);CHKERRQ(ierr); 9415 } 9416 } 9417 if (status) *status = F->schur_status; 9418 PetscFunctionReturn(0); 9419 } 9420 9421 /*@ 9422 MatFactorGetSchurComplement - Gets access to a Schur complement matrix using the current Schur data within a factored matrix 9423 9424 Logically Collective on Mat 9425 9426 Input Parameters: 9427 + F - the factored matrix obtained by calling MatGetFactor() 9428 . *S - location where to return the Schur complement, can be NULL 9429 - status - the status of the Schur complement matrix, can be NULL 9430 9431 Notes: 9432 You must call MatFactorSetSchurIS() before calling this routine. 9433 9434 Schur complement mode is currently implemented for sequential matrices. 9435 The routine returns a the Schur Complement stored within the data strutures of the solver. 9436 If MatFactorInvertSchurComplement() has previously been called, the returned matrix is actually the inverse of the Schur complement. 9437 The returned matrix should not be destroyed; the caller should call MatFactorRestoreSchurComplement() when the object is no longer needed. 9438 9439 Use MatFactorCreateSchurComplement() to create a copy of the Schur complement matrix that is within a factored matrix 9440 9441 See MatCreateSchurComplement() or MatGetSchurComplement() for ways to create virtual or approximate Schur complements. 9442 9443 Level: advanced 9444 9445 References: 9446 9447 .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorRestoreSchurComplement(), MatFactorCreateSchurComplement(), MatFactorSchurStatus 9448 @*/ 9449 PetscErrorCode MatFactorGetSchurComplement(Mat F,Mat* S,MatFactorSchurStatus* status) 9450 { 9451 PetscFunctionBegin; 9452 PetscValidHeaderSpecific(F,MAT_CLASSID,1); 9453 if (S) PetscValidPointer(S,2); 9454 if (status) PetscValidPointer(status,3); 9455 if (S) *S = F->schur; 9456 if (status) *status = F->schur_status; 9457 PetscFunctionReturn(0); 9458 } 9459 9460 /*@ 9461 MatFactorRestoreSchurComplement - Restore the Schur complement matrix object obtained from a call to MatFactorGetSchurComplement 9462 9463 Logically Collective on Mat 9464 9465 Input Parameters: 9466 + F - the factored matrix obtained by calling MatGetFactor() 9467 . *S - location where the Schur complement is stored 9468 - status - the status of the Schur complement matrix (see MatFactorSchurStatus) 9469 9470 Notes: 9471 9472 Level: advanced 9473 9474 References: 9475 9476 .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorRestoreSchurComplement(), MatFactorCreateSchurComplement(), MatFactorSchurStatus 9477 @*/ 9478 PetscErrorCode MatFactorRestoreSchurComplement(Mat F,Mat* S,MatFactorSchurStatus status) 9479 { 9480 PetscErrorCode ierr; 9481 9482 PetscFunctionBegin; 9483 PetscValidHeaderSpecific(F,MAT_CLASSID,1); 9484 if (S) { 9485 PetscValidHeaderSpecific(*S,MAT_CLASSID,2); 9486 *S = NULL; 9487 } 9488 F->schur_status = status; 9489 ierr = MatFactorUpdateSchurStatus_Private(F);CHKERRQ(ierr); 9490 PetscFunctionReturn(0); 9491 } 9492 9493 /*@ 9494 MatFactorSolveSchurComplementTranspose - Solve the transpose of the Schur complement system computed during the factorization step 9495 9496 Logically Collective on Mat 9497 9498 Input Parameters: 9499 + F - the factored matrix obtained by calling MatGetFactor() 9500 . rhs - location where the right hand side of the Schur complement system is stored 9501 - sol - location where the solution of the Schur complement system has to be returned 9502 9503 Notes: 9504 The sizes of the vectors should match the size of the Schur complement 9505 9506 Must be called after MatFactorSetSchurIS() 9507 9508 Level: advanced 9509 9510 References: 9511 9512 .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorSolveSchurComplement() 9513 @*/ 9514 PetscErrorCode MatFactorSolveSchurComplementTranspose(Mat F, Vec rhs, Vec sol) 9515 { 9516 PetscErrorCode ierr; 9517 9518 PetscFunctionBegin; 9519 PetscValidType(F,1); 9520 PetscValidType(rhs,2); 9521 PetscValidType(sol,3); 9522 PetscValidHeaderSpecific(F,MAT_CLASSID,1); 9523 PetscValidHeaderSpecific(rhs,VEC_CLASSID,2); 9524 PetscValidHeaderSpecific(sol,VEC_CLASSID,3); 9525 PetscCheckSameComm(F,1,rhs,2); 9526 PetscCheckSameComm(F,1,sol,3); 9527 ierr = MatFactorFactorizeSchurComplement(F);CHKERRQ(ierr); 9528 switch (F->schur_status) { 9529 case MAT_FACTOR_SCHUR_FACTORED: 9530 ierr = MatSolveTranspose(F->schur,rhs,sol);CHKERRQ(ierr); 9531 break; 9532 case MAT_FACTOR_SCHUR_INVERTED: 9533 ierr = MatMultTranspose(F->schur,rhs,sol);CHKERRQ(ierr); 9534 break; 9535 default: 9536 SETERRQ(PetscObjectComm((PetscObject)F),PETSC_ERR_SUP,"Unhandled MatFactorSchurStatus %d",F->schur_status); 9537 } 9538 PetscFunctionReturn(0); 9539 } 9540 9541 /*@ 9542 MatFactorSolveSchurComplement - Solve the Schur complement system computed during the factorization step 9543 9544 Logically Collective on Mat 9545 9546 Input Parameters: 9547 + F - the factored matrix obtained by calling MatGetFactor() 9548 . rhs - location where the right hand side of the Schur complement system is stored 9549 - sol - location where the solution of the Schur complement system has to be returned 9550 9551 Notes: 9552 The sizes of the vectors should match the size of the Schur complement 9553 9554 Must be called after MatFactorSetSchurIS() 9555 9556 Level: advanced 9557 9558 References: 9559 9560 .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorSolveSchurComplementTranspose() 9561 @*/ 9562 PetscErrorCode MatFactorSolveSchurComplement(Mat F, Vec rhs, Vec sol) 9563 { 9564 PetscErrorCode ierr; 9565 9566 PetscFunctionBegin; 9567 PetscValidType(F,1); 9568 PetscValidType(rhs,2); 9569 PetscValidType(sol,3); 9570 PetscValidHeaderSpecific(F,MAT_CLASSID,1); 9571 PetscValidHeaderSpecific(rhs,VEC_CLASSID,2); 9572 PetscValidHeaderSpecific(sol,VEC_CLASSID,3); 9573 PetscCheckSameComm(F,1,rhs,2); 9574 PetscCheckSameComm(F,1,sol,3); 9575 ierr = MatFactorFactorizeSchurComplement(F);CHKERRQ(ierr); 9576 switch (F->schur_status) { 9577 case MAT_FACTOR_SCHUR_FACTORED: 9578 ierr = MatSolve(F->schur,rhs,sol);CHKERRQ(ierr); 9579 break; 9580 case MAT_FACTOR_SCHUR_INVERTED: 9581 ierr = MatMult(F->schur,rhs,sol);CHKERRQ(ierr); 9582 break; 9583 default: 9584 SETERRQ(PetscObjectComm((PetscObject)F),PETSC_ERR_SUP,"Unhandled MatFactorSchurStatus %d",F->schur_status); 9585 } 9586 PetscFunctionReturn(0); 9587 } 9588 9589 /*@ 9590 MatFactorInvertSchurComplement - Invert the Schur complement matrix computed during the factorization step 9591 9592 Logically Collective on Mat 9593 9594 Input Parameters: 9595 . F - the factored matrix obtained by calling MatGetFactor() 9596 9597 Notes: 9598 Must be called after MatFactorSetSchurIS(). 9599 9600 Call MatFactorGetSchurComplement() or MatFactorCreateSchurComplement() AFTER this call to actually compute the inverse and get access to it. 9601 9602 Level: advanced 9603 9604 References: 9605 9606 .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorGetSchurComplement(), MatFactorCreateSchurComplement() 9607 @*/ 9608 PetscErrorCode MatFactorInvertSchurComplement(Mat F) 9609 { 9610 PetscErrorCode ierr; 9611 9612 PetscFunctionBegin; 9613 PetscValidType(F,1); 9614 PetscValidHeaderSpecific(F,MAT_CLASSID,1); 9615 if (F->schur_status == MAT_FACTOR_SCHUR_INVERTED) PetscFunctionReturn(0); 9616 ierr = MatFactorFactorizeSchurComplement(F);CHKERRQ(ierr); 9617 ierr = MatFactorInvertSchurComplement_Private(F);CHKERRQ(ierr); 9618 F->schur_status = MAT_FACTOR_SCHUR_INVERTED; 9619 PetscFunctionReturn(0); 9620 } 9621 9622 /*@ 9623 MatFactorFactorizeSchurComplement - Factorize the Schur complement matrix computed during the factorization step 9624 9625 Logically Collective on Mat 9626 9627 Input Parameters: 9628 . F - the factored matrix obtained by calling MatGetFactor() 9629 9630 Notes: 9631 Must be called after MatFactorSetSchurIS(). 9632 9633 Level: advanced 9634 9635 References: 9636 9637 .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorInvertSchurComplement() 9638 @*/ 9639 PetscErrorCode MatFactorFactorizeSchurComplement(Mat F) 9640 { 9641 PetscErrorCode ierr; 9642 9643 PetscFunctionBegin; 9644 PetscValidType(F,1); 9645 PetscValidHeaderSpecific(F,MAT_CLASSID,1); 9646 if (F->schur_status == MAT_FACTOR_SCHUR_INVERTED || F->schur_status == MAT_FACTOR_SCHUR_FACTORED) PetscFunctionReturn(0); 9647 ierr = MatFactorFactorizeSchurComplement_Private(F);CHKERRQ(ierr); 9648 F->schur_status = MAT_FACTOR_SCHUR_FACTORED; 9649 PetscFunctionReturn(0); 9650 } 9651 9652 /*@ 9653 MatPtAP - Creates the matrix product C = P^T * A * P 9654 9655 Neighbor-wise Collective on Mat 9656 9657 Input Parameters: 9658 + A - the matrix 9659 . P - the projection matrix 9660 . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 9661 - fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(P)), use PETSC_DEFAULT if you do not have a good estimate 9662 if the result is a dense matrix this is irrelevant 9663 9664 Output Parameters: 9665 . C - the product matrix 9666 9667 Notes: 9668 C will be created and must be destroyed by the user with MatDestroy(). 9669 9670 For matrix types without special implementation the function fallbacks to MatMatMult() followed by MatTransposeMatMult(). 9671 9672 Level: intermediate 9673 9674 .seealso: MatMatMult(), MatRARt() 9675 @*/ 9676 PetscErrorCode MatPtAP(Mat A,Mat P,MatReuse scall,PetscReal fill,Mat *C) 9677 { 9678 PetscErrorCode ierr; 9679 9680 PetscFunctionBegin; 9681 if (scall == MAT_REUSE_MATRIX) MatCheckProduct(*C,5); 9682 PetscCheckFalse(scall == MAT_INPLACE_MATRIX,PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported"); 9683 9684 if (scall == MAT_INITIAL_MATRIX) { 9685 ierr = MatProductCreate(A,P,NULL,C);CHKERRQ(ierr); 9686 ierr = MatProductSetType(*C,MATPRODUCT_PtAP);CHKERRQ(ierr); 9687 ierr = MatProductSetAlgorithm(*C,"default");CHKERRQ(ierr); 9688 ierr = MatProductSetFill(*C,fill);CHKERRQ(ierr); 9689 9690 (*C)->product->api_user = PETSC_TRUE; 9691 ierr = MatProductSetFromOptions(*C);CHKERRQ(ierr); 9692 PetscCheckFalse(!(*C)->ops->productsymbolic,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); 9693 ierr = MatProductSymbolic(*C);CHKERRQ(ierr); 9694 } else { /* scall == MAT_REUSE_MATRIX */ 9695 ierr = MatProductReplaceMats(A,P,NULL,*C);CHKERRQ(ierr); 9696 } 9697 9698 ierr = MatProductNumeric(*C);CHKERRQ(ierr); 9699 if (A->symmetric) { 9700 if (A->spd) { 9701 ierr = MatSetOption(*C,MAT_SPD,PETSC_TRUE);CHKERRQ(ierr); 9702 } else { 9703 ierr = MatSetOption(*C,MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr); 9704 } 9705 } 9706 PetscFunctionReturn(0); 9707 } 9708 9709 /*@ 9710 MatRARt - Creates the matrix product C = R * A * R^T 9711 9712 Neighbor-wise Collective on Mat 9713 9714 Input Parameters: 9715 + A - the matrix 9716 . R - the projection matrix 9717 . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 9718 - fill - expected fill as ratio of nnz(C)/nnz(A), use PETSC_DEFAULT if you do not have a good estimate 9719 if the result is a dense matrix this is irrelevant 9720 9721 Output Parameters: 9722 . C - the product matrix 9723 9724 Notes: 9725 C will be created and must be destroyed by the user with MatDestroy(). 9726 9727 This routine is currently only implemented for pairs of AIJ matrices and classes 9728 which inherit from AIJ. Due to PETSc sparse matrix block row distribution among processes, 9729 parallel MatRARt is implemented via explicit transpose of R, which could be very expensive. 9730 We recommend using MatPtAP(). 9731 9732 Level: intermediate 9733 9734 .seealso: MatMatMult(), MatPtAP() 9735 @*/ 9736 PetscErrorCode MatRARt(Mat A,Mat R,MatReuse scall,PetscReal fill,Mat *C) 9737 { 9738 PetscErrorCode ierr; 9739 9740 PetscFunctionBegin; 9741 if (scall == MAT_REUSE_MATRIX) MatCheckProduct(*C,5); 9742 PetscCheckFalse(scall == MAT_INPLACE_MATRIX,PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported"); 9743 9744 if (scall == MAT_INITIAL_MATRIX) { 9745 ierr = MatProductCreate(A,R,NULL,C);CHKERRQ(ierr); 9746 ierr = MatProductSetType(*C,MATPRODUCT_RARt);CHKERRQ(ierr); 9747 ierr = MatProductSetAlgorithm(*C,"default");CHKERRQ(ierr); 9748 ierr = MatProductSetFill(*C,fill);CHKERRQ(ierr); 9749 9750 (*C)->product->api_user = PETSC_TRUE; 9751 ierr = MatProductSetFromOptions(*C);CHKERRQ(ierr); 9752 PetscCheckFalse(!(*C)->ops->productsymbolic,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); 9753 ierr = MatProductSymbolic(*C);CHKERRQ(ierr); 9754 } else { /* scall == MAT_REUSE_MATRIX */ 9755 ierr = MatProductReplaceMats(A,R,NULL,*C);CHKERRQ(ierr); 9756 } 9757 9758 ierr = MatProductNumeric(*C);CHKERRQ(ierr); 9759 if (A->symmetric_set && A->symmetric) { 9760 ierr = MatSetOption(*C,MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr); 9761 } 9762 PetscFunctionReturn(0); 9763 } 9764 9765 static PetscErrorCode MatProduct_Private(Mat A,Mat B,MatReuse scall,PetscReal fill,MatProductType ptype, Mat *C) 9766 { 9767 PetscErrorCode ierr; 9768 9769 PetscFunctionBegin; 9770 PetscCheckFalse(scall == MAT_INPLACE_MATRIX,PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported"); 9771 9772 if (scall == MAT_INITIAL_MATRIX) { 9773 ierr = PetscInfo(A,"Calling MatProduct API with MAT_INITIAL_MATRIX and product type %s\n",MatProductTypes[ptype]);CHKERRQ(ierr); 9774 ierr = MatProductCreate(A,B,NULL,C);CHKERRQ(ierr); 9775 ierr = MatProductSetType(*C,ptype);CHKERRQ(ierr); 9776 ierr = MatProductSetAlgorithm(*C,MATPRODUCTALGORITHMDEFAULT);CHKERRQ(ierr); 9777 ierr = MatProductSetFill(*C,fill);CHKERRQ(ierr); 9778 9779 (*C)->product->api_user = PETSC_TRUE; 9780 ierr = MatProductSetFromOptions(*C);CHKERRQ(ierr); 9781 ierr = MatProductSymbolic(*C);CHKERRQ(ierr); 9782 } else { /* scall == MAT_REUSE_MATRIX */ 9783 Mat_Product *product = (*C)->product; 9784 PetscBool isdense; 9785 9786 ierr = PetscObjectBaseTypeCompareAny((PetscObject)(*C),&isdense,MATSEQDENSE,MATMPIDENSE,"");CHKERRQ(ierr); 9787 if (isdense && product && product->type != ptype) { 9788 ierr = MatProductClear(*C);CHKERRQ(ierr); 9789 product = NULL; 9790 } 9791 ierr = PetscInfo(A,"Calling MatProduct API with MAT_REUSE_MATRIX %s product present and product type %s\n",product ? "with" : "without",MatProductTypes[ptype]);CHKERRQ(ierr); 9792 if (!product) { /* user provide the dense matrix *C without calling MatProductCreate() or reusing it from previous calls */ 9793 if (isdense) { 9794 ierr = MatProductCreate_Private(A,B,NULL,*C);CHKERRQ(ierr); 9795 product = (*C)->product; 9796 product->fill = fill; 9797 product->api_user = PETSC_TRUE; 9798 product->clear = PETSC_TRUE; 9799 9800 ierr = MatProductSetType(*C,ptype);CHKERRQ(ierr); 9801 ierr = MatProductSetFromOptions(*C);CHKERRQ(ierr); 9802 PetscCheckFalse(!(*C)->ops->productsymbolic,PetscObjectComm((PetscObject)(*C)),PETSC_ERR_SUP,"MatProduct %s not supported for %s and %s",MatProductTypes[ptype],((PetscObject)A)->type_name,((PetscObject)B)->type_name); 9803 ierr = MatProductSymbolic(*C);CHKERRQ(ierr); 9804 } else SETERRQ(PetscObjectComm((PetscObject)(*C)),PETSC_ERR_SUP,"Call MatProductCreate() first"); 9805 } else { /* user may change input matrices A or B when REUSE */ 9806 ierr = MatProductReplaceMats(A,B,NULL,*C);CHKERRQ(ierr); 9807 } 9808 } 9809 ierr = MatProductNumeric(*C);CHKERRQ(ierr); 9810 PetscFunctionReturn(0); 9811 } 9812 9813 /*@ 9814 MatMatMult - Performs Matrix-Matrix Multiplication C=A*B. 9815 9816 Neighbor-wise Collective on Mat 9817 9818 Input Parameters: 9819 + A - the left matrix 9820 . B - the right matrix 9821 . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 9822 - fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if you do not have a good estimate 9823 if the result is a dense matrix this is irrelevant 9824 9825 Output Parameters: 9826 . C - the product matrix 9827 9828 Notes: 9829 Unless scall is MAT_REUSE_MATRIX C will be created. 9830 9831 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 9832 call to this function with MAT_INITIAL_MATRIX. 9833 9834 To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value actually needed. 9835 9836 If you have many matrices with the same non-zero structure to multiply, you should use MatProductCreate()/MatProductSymbolic()/MatProductReplaceMats(), and call MatProductNumeric() repeatedly. 9837 9838 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. 9839 9840 Example of Usage: 9841 .vb 9842 MatProductCreate(A,B,NULL,&C); 9843 MatProductSetType(C,MATPRODUCT_AB); 9844 MatProductSymbolic(C); 9845 MatProductNumeric(C); // compute C=A * B 9846 MatProductReplaceMats(A1,B1,NULL,C); // compute C=A1 * B1 9847 MatProductNumeric(C); 9848 MatProductReplaceMats(A2,NULL,NULL,C); // compute C=A2 * B1 9849 MatProductNumeric(C); 9850 .ve 9851 9852 Level: intermediate 9853 9854 .seealso: MatTransposeMatMult(), MatMatTransposeMult(), MatPtAP(), MatProductCreate(), MatProductSymbolic(), MatProductReplaceMats(), MatProductNumeric() 9855 @*/ 9856 PetscErrorCode MatMatMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C) 9857 { 9858 PetscErrorCode ierr; 9859 9860 PetscFunctionBegin; 9861 ierr = MatProduct_Private(A,B,scall,fill,MATPRODUCT_AB,C);CHKERRQ(ierr); 9862 PetscFunctionReturn(0); 9863 } 9864 9865 /*@ 9866 MatMatTransposeMult - Performs Matrix-Matrix Multiplication C=A*B^T. 9867 9868 Neighbor-wise Collective on Mat 9869 9870 Input Parameters: 9871 + A - the left matrix 9872 . B - the right matrix 9873 . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 9874 - fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if not known 9875 9876 Output Parameters: 9877 . C - the product matrix 9878 9879 Notes: 9880 C will be created if MAT_INITIAL_MATRIX and must be destroyed by the user with MatDestroy(). 9881 9882 MAT_REUSE_MATRIX can only be used if the matrices A and B have the same nonzero pattern as in the previous call 9883 9884 To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value 9885 actually needed. 9886 9887 This routine is currently only implemented for pairs of SeqAIJ matrices, for the SeqDense class, 9888 and for pairs of MPIDense matrices. 9889 9890 Options Database Keys: 9891 . -matmattransmult_mpidense_mpidense_via {allgatherv,cyclic} - Choose between algorthims for MPIDense matrices: the 9892 first redundantly copies the transposed B matrix on each process and requiers O(log P) communication complexity; 9893 the second never stores more than one portion of the B matrix at a time by requires O(P) communication complexity. 9894 9895 Level: intermediate 9896 9897 .seealso: MatMatMult(), MatTransposeMatMult() MatPtAP() 9898 @*/ 9899 PetscErrorCode MatMatTransposeMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C) 9900 { 9901 PetscErrorCode ierr; 9902 9903 PetscFunctionBegin; 9904 ierr = MatProduct_Private(A,B,scall,fill,MATPRODUCT_ABt,C);CHKERRQ(ierr); 9905 PetscFunctionReturn(0); 9906 } 9907 9908 /*@ 9909 MatTransposeMatMult - Performs Matrix-Matrix Multiplication C=A^T*B. 9910 9911 Neighbor-wise Collective on Mat 9912 9913 Input Parameters: 9914 + A - the left matrix 9915 . B - the right matrix 9916 . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 9917 - fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if not known 9918 9919 Output Parameters: 9920 . C - the product matrix 9921 9922 Notes: 9923 C will be created if MAT_INITIAL_MATRIX and must be destroyed by the user with MatDestroy(). 9924 9925 MAT_REUSE_MATRIX can only be used if the matrices A and B have the same nonzero pattern as in the previous call. 9926 9927 To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value 9928 actually needed. 9929 9930 This routine is currently implemented for pairs of AIJ matrices and pairs of SeqDense matrices and classes 9931 which inherit from SeqAIJ. C will be of same type as the input matrices. 9932 9933 Level: intermediate 9934 9935 .seealso: MatMatMult(), MatMatTransposeMult(), MatPtAP() 9936 @*/ 9937 PetscErrorCode MatTransposeMatMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C) 9938 { 9939 PetscErrorCode ierr; 9940 9941 PetscFunctionBegin; 9942 ierr = MatProduct_Private(A,B,scall,fill,MATPRODUCT_AtB,C);CHKERRQ(ierr); 9943 PetscFunctionReturn(0); 9944 } 9945 9946 /*@ 9947 MatMatMatMult - Performs Matrix-Matrix-Matrix Multiplication D=A*B*C. 9948 9949 Neighbor-wise Collective on Mat 9950 9951 Input Parameters: 9952 + A - the left matrix 9953 . B - the middle matrix 9954 . C - the right matrix 9955 . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 9956 - 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 9957 if the result is a dense matrix this is irrelevant 9958 9959 Output Parameters: 9960 . D - the product matrix 9961 9962 Notes: 9963 Unless scall is MAT_REUSE_MATRIX D will be created. 9964 9965 MAT_REUSE_MATRIX can only be used if the matrices A, B and C have the same nonzero pattern as in the previous call 9966 9967 To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value 9968 actually needed. 9969 9970 If you have many matrices with the same non-zero structure to multiply, you 9971 should use MAT_REUSE_MATRIX in all calls but the first or 9972 9973 Level: intermediate 9974 9975 .seealso: MatMatMult, MatPtAP() 9976 @*/ 9977 PetscErrorCode MatMatMatMult(Mat A,Mat B,Mat C,MatReuse scall,PetscReal fill,Mat *D) 9978 { 9979 PetscErrorCode ierr; 9980 9981 PetscFunctionBegin; 9982 if (scall == MAT_REUSE_MATRIX) MatCheckProduct(*D,6); 9983 PetscCheckFalse(scall == MAT_INPLACE_MATRIX,PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported"); 9984 9985 if (scall == MAT_INITIAL_MATRIX) { 9986 ierr = MatProductCreate(A,B,C,D);CHKERRQ(ierr); 9987 ierr = MatProductSetType(*D,MATPRODUCT_ABC);CHKERRQ(ierr); 9988 ierr = MatProductSetAlgorithm(*D,"default");CHKERRQ(ierr); 9989 ierr = MatProductSetFill(*D,fill);CHKERRQ(ierr); 9990 9991 (*D)->product->api_user = PETSC_TRUE; 9992 ierr = MatProductSetFromOptions(*D);CHKERRQ(ierr); 9993 PetscCheckFalse(!(*D)->ops->productsymbolic,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); 9994 ierr = MatProductSymbolic(*D);CHKERRQ(ierr); 9995 } else { /* user may change input matrices when REUSE */ 9996 ierr = MatProductReplaceMats(A,B,C,*D);CHKERRQ(ierr); 9997 } 9998 ierr = MatProductNumeric(*D);CHKERRQ(ierr); 9999 PetscFunctionReturn(0); 10000 } 10001 10002 /*@ 10003 MatCreateRedundantMatrix - Create redundant matrices and put them into processors of subcommunicators. 10004 10005 Collective on Mat 10006 10007 Input Parameters: 10008 + mat - the matrix 10009 . nsubcomm - the number of subcommunicators (= number of redundant parallel or sequential matrices) 10010 . subcomm - MPI communicator split from the communicator where mat resides in (or MPI_COMM_NULL if nsubcomm is used) 10011 - reuse - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 10012 10013 Output Parameter: 10014 . matredundant - redundant matrix 10015 10016 Notes: 10017 MAT_REUSE_MATRIX can only be used when the nonzero structure of the 10018 original matrix has not changed from that last call to MatCreateRedundantMatrix(). 10019 10020 This routine creates the duplicated matrices in subcommunicators; you should NOT create them before 10021 calling it. 10022 10023 Level: advanced 10024 10025 .seealso: MatDestroy() 10026 @*/ 10027 PetscErrorCode MatCreateRedundantMatrix(Mat mat,PetscInt nsubcomm,MPI_Comm subcomm,MatReuse reuse,Mat *matredundant) 10028 { 10029 PetscErrorCode ierr; 10030 MPI_Comm comm; 10031 PetscMPIInt size; 10032 PetscInt mloc_sub,nloc_sub,rstart,rend,M=mat->rmap->N,N=mat->cmap->N,bs=mat->rmap->bs; 10033 Mat_Redundant *redund=NULL; 10034 PetscSubcomm psubcomm=NULL; 10035 MPI_Comm subcomm_in=subcomm; 10036 Mat *matseq; 10037 IS isrow,iscol; 10038 PetscBool newsubcomm=PETSC_FALSE; 10039 10040 PetscFunctionBegin; 10041 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10042 if (nsubcomm && reuse == MAT_REUSE_MATRIX) { 10043 PetscValidPointer(*matredundant,5); 10044 PetscValidHeaderSpecific(*matredundant,MAT_CLASSID,5); 10045 } 10046 10047 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);CHKERRMPI(ierr); 10048 if (size == 1 || nsubcomm == 1) { 10049 if (reuse == MAT_INITIAL_MATRIX) { 10050 ierr = MatDuplicate(mat,MAT_COPY_VALUES,matredundant);CHKERRQ(ierr); 10051 } else { 10052 PetscCheckFalse(*matredundant == mat,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"MAT_REUSE_MATRIX means reuse the matrix passed in as the final argument, not the original matrix"); 10053 ierr = MatCopy(mat,*matredundant,SAME_NONZERO_PATTERN);CHKERRQ(ierr); 10054 } 10055 PetscFunctionReturn(0); 10056 } 10057 10058 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 10059 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 10060 MatCheckPreallocated(mat,1); 10061 10062 ierr = PetscLogEventBegin(MAT_RedundantMat,mat,0,0,0);CHKERRQ(ierr); 10063 if (subcomm_in == MPI_COMM_NULL && reuse == MAT_INITIAL_MATRIX) { /* get subcomm if user does not provide subcomm */ 10064 /* create psubcomm, then get subcomm */ 10065 ierr = PetscObjectGetComm((PetscObject)mat,&comm);CHKERRQ(ierr); 10066 ierr = MPI_Comm_size(comm,&size);CHKERRMPI(ierr); 10067 PetscCheckFalse(nsubcomm < 1 || nsubcomm > size,PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"nsubcomm must between 1 and %d",size); 10068 10069 ierr = PetscSubcommCreate(comm,&psubcomm);CHKERRQ(ierr); 10070 ierr = PetscSubcommSetNumber(psubcomm,nsubcomm);CHKERRQ(ierr); 10071 ierr = PetscSubcommSetType(psubcomm,PETSC_SUBCOMM_CONTIGUOUS);CHKERRQ(ierr); 10072 ierr = PetscSubcommSetFromOptions(psubcomm);CHKERRQ(ierr); 10073 ierr = PetscCommDuplicate(PetscSubcommChild(psubcomm),&subcomm,NULL);CHKERRQ(ierr); 10074 newsubcomm = PETSC_TRUE; 10075 ierr = PetscSubcommDestroy(&psubcomm);CHKERRQ(ierr); 10076 } 10077 10078 /* get isrow, iscol and a local sequential matrix matseq[0] */ 10079 if (reuse == MAT_INITIAL_MATRIX) { 10080 mloc_sub = PETSC_DECIDE; 10081 nloc_sub = PETSC_DECIDE; 10082 if (bs < 1) { 10083 ierr = PetscSplitOwnership(subcomm,&mloc_sub,&M);CHKERRQ(ierr); 10084 ierr = PetscSplitOwnership(subcomm,&nloc_sub,&N);CHKERRQ(ierr); 10085 } else { 10086 ierr = PetscSplitOwnershipBlock(subcomm,bs,&mloc_sub,&M);CHKERRQ(ierr); 10087 ierr = PetscSplitOwnershipBlock(subcomm,bs,&nloc_sub,&N);CHKERRQ(ierr); 10088 } 10089 ierr = MPI_Scan(&mloc_sub,&rend,1,MPIU_INT,MPI_SUM,subcomm);CHKERRMPI(ierr); 10090 rstart = rend - mloc_sub; 10091 ierr = ISCreateStride(PETSC_COMM_SELF,mloc_sub,rstart,1,&isrow);CHKERRQ(ierr); 10092 ierr = ISCreateStride(PETSC_COMM_SELF,N,0,1,&iscol);CHKERRQ(ierr); 10093 } else { /* reuse == MAT_REUSE_MATRIX */ 10094 PetscCheckFalse(*matredundant == mat,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"MAT_REUSE_MATRIX means reuse the matrix passed in as the final argument, not the original matrix"); 10095 /* retrieve subcomm */ 10096 ierr = PetscObjectGetComm((PetscObject)(*matredundant),&subcomm);CHKERRQ(ierr); 10097 redund = (*matredundant)->redundant; 10098 isrow = redund->isrow; 10099 iscol = redund->iscol; 10100 matseq = redund->matseq; 10101 } 10102 ierr = MatCreateSubMatrices(mat,1,&isrow,&iscol,reuse,&matseq);CHKERRQ(ierr); 10103 10104 /* get matredundant over subcomm */ 10105 if (reuse == MAT_INITIAL_MATRIX) { 10106 ierr = MatCreateMPIMatConcatenateSeqMat(subcomm,matseq[0],nloc_sub,reuse,matredundant);CHKERRQ(ierr); 10107 10108 /* create a supporting struct and attach it to C for reuse */ 10109 ierr = PetscNewLog(*matredundant,&redund);CHKERRQ(ierr); 10110 (*matredundant)->redundant = redund; 10111 redund->isrow = isrow; 10112 redund->iscol = iscol; 10113 redund->matseq = matseq; 10114 if (newsubcomm) { 10115 redund->subcomm = subcomm; 10116 } else { 10117 redund->subcomm = MPI_COMM_NULL; 10118 } 10119 } else { 10120 ierr = MatCreateMPIMatConcatenateSeqMat(subcomm,matseq[0],PETSC_DECIDE,reuse,matredundant);CHKERRQ(ierr); 10121 } 10122 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 10123 if (matseq[0]->boundtocpu && matseq[0]->bindingpropagates) { 10124 ierr = MatBindToCPU(*matredundant,PETSC_TRUE);CHKERRQ(ierr); 10125 ierr = MatSetBindingPropagates(*matredundant,PETSC_TRUE);CHKERRQ(ierr); 10126 } 10127 #endif 10128 ierr = PetscLogEventEnd(MAT_RedundantMat,mat,0,0,0);CHKERRQ(ierr); 10129 PetscFunctionReturn(0); 10130 } 10131 10132 /*@C 10133 MatGetMultiProcBlock - Create multiple [bjacobi] 'parallel submatrices' from 10134 a given 'mat' object. Each submatrix can span multiple procs. 10135 10136 Collective on Mat 10137 10138 Input Parameters: 10139 + mat - the matrix 10140 . subcomm - the subcommunicator obtained by com_split(comm) 10141 - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 10142 10143 Output Parameter: 10144 . subMat - 'parallel submatrices each spans a given subcomm 10145 10146 Notes: 10147 The submatrix partition across processors is dictated by 'subComm' a 10148 communicator obtained by com_split(comm). The comm_split 10149 is not restriced to be grouped with consecutive original ranks. 10150 10151 Due the comm_split() usage, the parallel layout of the submatrices 10152 map directly to the layout of the original matrix [wrt the local 10153 row,col partitioning]. So the original 'DiagonalMat' naturally maps 10154 into the 'DiagonalMat' of the subMat, hence it is used directly from 10155 the subMat. However the offDiagMat looses some columns - and this is 10156 reconstructed with MatSetValues() 10157 10158 Level: advanced 10159 10160 .seealso: MatCreateSubMatrices() 10161 @*/ 10162 PetscErrorCode MatGetMultiProcBlock(Mat mat, MPI_Comm subComm, MatReuse scall,Mat *subMat) 10163 { 10164 PetscErrorCode ierr; 10165 PetscMPIInt commsize,subCommSize; 10166 10167 PetscFunctionBegin; 10168 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&commsize);CHKERRMPI(ierr); 10169 ierr = MPI_Comm_size(subComm,&subCommSize);CHKERRMPI(ierr); 10170 PetscCheckFalse(subCommSize > commsize,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"CommSize %d < SubCommZize %d",commsize,subCommSize); 10171 10172 PetscCheckFalse(scall == MAT_REUSE_MATRIX && *subMat == mat,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"MAT_REUSE_MATRIX means reuse the matrix passed in as the final argument, not the original matrix"); 10173 ierr = PetscLogEventBegin(MAT_GetMultiProcBlock,mat,0,0,0);CHKERRQ(ierr); 10174 ierr = (*mat->ops->getmultiprocblock)(mat,subComm,scall,subMat);CHKERRQ(ierr); 10175 ierr = PetscLogEventEnd(MAT_GetMultiProcBlock,mat,0,0,0);CHKERRQ(ierr); 10176 PetscFunctionReturn(0); 10177 } 10178 10179 /*@ 10180 MatGetLocalSubMatrix - Gets a reference to a submatrix specified in local numbering 10181 10182 Not Collective 10183 10184 Input Parameters: 10185 + mat - matrix to extract local submatrix from 10186 . isrow - local row indices for submatrix 10187 - iscol - local column indices for submatrix 10188 10189 Output Parameter: 10190 . submat - the submatrix 10191 10192 Level: intermediate 10193 10194 Notes: 10195 The submat should be returned with MatRestoreLocalSubMatrix(). 10196 10197 Depending on the format of mat, the returned submat may not implement MatMult(). Its communicator may be 10198 the same as mat, it may be PETSC_COMM_SELF, or some other subcomm of mat's. 10199 10200 The submat always implements MatSetValuesLocal(). If isrow and iscol have the same block size, then 10201 MatSetValuesBlockedLocal() will also be implemented. 10202 10203 The mat must have had a ISLocalToGlobalMapping provided to it with MatSetLocalToGlobalMapping(). Note that 10204 matrices obtained with DMCreateMatrix() generally already have the local to global mapping provided. 10205 10206 .seealso: MatRestoreLocalSubMatrix(), MatCreateLocalRef(), MatSetLocalToGlobalMapping() 10207 @*/ 10208 PetscErrorCode MatGetLocalSubMatrix(Mat mat,IS isrow,IS iscol,Mat *submat) 10209 { 10210 PetscErrorCode ierr; 10211 10212 PetscFunctionBegin; 10213 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10214 PetscValidHeaderSpecific(isrow,IS_CLASSID,2); 10215 PetscValidHeaderSpecific(iscol,IS_CLASSID,3); 10216 PetscCheckSameComm(isrow,2,iscol,3); 10217 PetscValidPointer(submat,4); 10218 PetscCheckFalse(!mat->rmap->mapping,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Matrix must have local to global mapping provided before this call"); 10219 10220 if (mat->ops->getlocalsubmatrix) { 10221 ierr = (*mat->ops->getlocalsubmatrix)(mat,isrow,iscol,submat);CHKERRQ(ierr); 10222 } else { 10223 ierr = MatCreateLocalRef(mat,isrow,iscol,submat);CHKERRQ(ierr); 10224 } 10225 PetscFunctionReturn(0); 10226 } 10227 10228 /*@ 10229 MatRestoreLocalSubMatrix - Restores a reference to a submatrix specified in local numbering 10230 10231 Not Collective 10232 10233 Input Parameters: 10234 + mat - matrix to extract local submatrix from 10235 . isrow - local row indices for submatrix 10236 . iscol - local column indices for submatrix 10237 - submat - the submatrix 10238 10239 Level: intermediate 10240 10241 .seealso: MatGetLocalSubMatrix() 10242 @*/ 10243 PetscErrorCode MatRestoreLocalSubMatrix(Mat mat,IS isrow,IS iscol,Mat *submat) 10244 { 10245 PetscErrorCode ierr; 10246 10247 PetscFunctionBegin; 10248 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10249 PetscValidHeaderSpecific(isrow,IS_CLASSID,2); 10250 PetscValidHeaderSpecific(iscol,IS_CLASSID,3); 10251 PetscCheckSameComm(isrow,2,iscol,3); 10252 PetscValidPointer(submat,4); 10253 if (*submat) { 10254 PetscValidHeaderSpecific(*submat,MAT_CLASSID,4); 10255 } 10256 10257 if (mat->ops->restorelocalsubmatrix) { 10258 ierr = (*mat->ops->restorelocalsubmatrix)(mat,isrow,iscol,submat);CHKERRQ(ierr); 10259 } else { 10260 ierr = MatDestroy(submat);CHKERRQ(ierr); 10261 } 10262 *submat = NULL; 10263 PetscFunctionReturn(0); 10264 } 10265 10266 /* --------------------------------------------------------*/ 10267 /*@ 10268 MatFindZeroDiagonals - Finds all the rows of a matrix that have zero or no diagonal entry in the matrix 10269 10270 Collective on Mat 10271 10272 Input Parameter: 10273 . mat - the matrix 10274 10275 Output Parameter: 10276 . is - if any rows have zero diagonals this contains the list of them 10277 10278 Level: developer 10279 10280 .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd() 10281 @*/ 10282 PetscErrorCode MatFindZeroDiagonals(Mat mat,IS *is) 10283 { 10284 PetscErrorCode ierr; 10285 10286 PetscFunctionBegin; 10287 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10288 PetscValidType(mat,1); 10289 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 10290 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 10291 10292 if (!mat->ops->findzerodiagonals) { 10293 Vec diag; 10294 const PetscScalar *a; 10295 PetscInt *rows; 10296 PetscInt rStart, rEnd, r, nrow = 0; 10297 10298 ierr = MatCreateVecs(mat, &diag, NULL);CHKERRQ(ierr); 10299 ierr = MatGetDiagonal(mat, diag);CHKERRQ(ierr); 10300 ierr = MatGetOwnershipRange(mat, &rStart, &rEnd);CHKERRQ(ierr); 10301 ierr = VecGetArrayRead(diag, &a);CHKERRQ(ierr); 10302 for (r = 0; r < rEnd-rStart; ++r) if (a[r] == 0.0) ++nrow; 10303 ierr = PetscMalloc1(nrow, &rows);CHKERRQ(ierr); 10304 nrow = 0; 10305 for (r = 0; r < rEnd-rStart; ++r) if (a[r] == 0.0) rows[nrow++] = r+rStart; 10306 ierr = VecRestoreArrayRead(diag, &a);CHKERRQ(ierr); 10307 ierr = VecDestroy(&diag);CHKERRQ(ierr); 10308 ierr = ISCreateGeneral(PetscObjectComm((PetscObject) mat), nrow, rows, PETSC_OWN_POINTER, is);CHKERRQ(ierr); 10309 } else { 10310 ierr = (*mat->ops->findzerodiagonals)(mat, is);CHKERRQ(ierr); 10311 } 10312 PetscFunctionReturn(0); 10313 } 10314 10315 /*@ 10316 MatFindOffBlockDiagonalEntries - Finds all the rows of a matrix that have entries outside of the main diagonal block (defined by the matrix block size) 10317 10318 Collective on Mat 10319 10320 Input Parameter: 10321 . mat - the matrix 10322 10323 Output Parameter: 10324 . is - contains the list of rows with off block diagonal entries 10325 10326 Level: developer 10327 10328 .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd() 10329 @*/ 10330 PetscErrorCode MatFindOffBlockDiagonalEntries(Mat mat,IS *is) 10331 { 10332 PetscErrorCode ierr; 10333 10334 PetscFunctionBegin; 10335 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10336 PetscValidType(mat,1); 10337 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 10338 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 10339 10340 PetscCheckFalse(!mat->ops->findoffblockdiagonalentries,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s does not have a find off block diagonal entries defined",((PetscObject)mat)->type_name); 10341 ierr = (*mat->ops->findoffblockdiagonalentries)(mat,is);CHKERRQ(ierr); 10342 PetscFunctionReturn(0); 10343 } 10344 10345 /*@C 10346 MatInvertBlockDiagonal - Inverts the block diagonal entries. 10347 10348 Collective on Mat 10349 10350 Input Parameters: 10351 . mat - the matrix 10352 10353 Output Parameters: 10354 . values - the block inverses in column major order (FORTRAN-like) 10355 10356 Note: 10357 The size of the blocks is determined by the block size of the matrix. 10358 10359 Fortran Note: 10360 This routine is not available from Fortran. 10361 10362 Level: advanced 10363 10364 .seealso: MatInvertBockDiagonalMat() 10365 @*/ 10366 PetscErrorCode MatInvertBlockDiagonal(Mat mat,const PetscScalar **values) 10367 { 10368 PetscErrorCode ierr; 10369 10370 PetscFunctionBegin; 10371 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10372 PetscCheckFalse(!mat->assembled,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 10373 PetscCheckFalse(mat->factortype,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 10374 PetscCheckFalse(!mat->ops->invertblockdiagonal,PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported for type %s",((PetscObject)mat)->type_name); 10375 ierr = (*mat->ops->invertblockdiagonal)(mat,values);CHKERRQ(ierr); 10376 PetscFunctionReturn(0); 10377 } 10378 10379 /*@C 10380 MatInvertVariableBlockDiagonal - Inverts the point block diagonal entries. 10381 10382 Collective on Mat 10383 10384 Input Parameters: 10385 + mat - the matrix 10386 . nblocks - the number of blocks 10387 - bsizes - the size of each block 10388 10389 Output Parameters: 10390 . values - the block inverses in column major order (FORTRAN-like) 10391 10392 Note: 10393 This routine is not available from Fortran. 10394 10395 Level: advanced 10396 10397 .seealso: MatInvertBockDiagonal() 10398 @*/ 10399 PetscErrorCode MatInvertVariableBlockDiagonal(Mat mat,PetscInt nblocks,const PetscInt *bsizes,PetscScalar *values) 10400 { 10401 PetscErrorCode ierr; 10402 10403 PetscFunctionBegin; 10404 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10405 PetscCheckFalse(!mat->assembled,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 10406 PetscCheckFalse(mat->factortype,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 10407 PetscCheckFalse(!mat->ops->invertvariableblockdiagonal,PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported for type %s",((PetscObject)mat)->type_name); 10408 ierr = (*mat->ops->invertvariableblockdiagonal)(mat,nblocks,bsizes,values);CHKERRQ(ierr); 10409 PetscFunctionReturn(0); 10410 } 10411 10412 /*@ 10413 MatInvertBlockDiagonalMat - set matrix C to be the inverted block diagonal of matrix A 10414 10415 Collective on Mat 10416 10417 Input Parameters: 10418 . A - the matrix 10419 10420 Output Parameters: 10421 . C - matrix with inverted block diagonal of A. This matrix should be created and may have its type set. 10422 10423 Notes: the blocksize of the matrix is used to determine the blocks on the diagonal of C 10424 10425 Level: advanced 10426 10427 .seealso: MatInvertBockDiagonal() 10428 @*/ 10429 PetscErrorCode MatInvertBlockDiagonalMat(Mat A,Mat C) 10430 { 10431 PetscErrorCode ierr; 10432 const PetscScalar *vals; 10433 PetscInt *dnnz; 10434 PetscInt M,N,m,n,rstart,rend,bs,i,j; 10435 10436 PetscFunctionBegin; 10437 ierr = MatInvertBlockDiagonal(A,&vals);CHKERRQ(ierr); 10438 ierr = MatGetBlockSize(A,&bs);CHKERRQ(ierr); 10439 ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr); 10440 ierr = MatGetLocalSize(A,&m,&n);CHKERRQ(ierr); 10441 ierr = MatSetSizes(C,m,n,M,N);CHKERRQ(ierr); 10442 ierr = MatSetBlockSize(C,bs);CHKERRQ(ierr); 10443 ierr = PetscMalloc1(m/bs,&dnnz);CHKERRQ(ierr); 10444 for (j = 0; j < m/bs; j++) dnnz[j] = 1; 10445 ierr = MatXAIJSetPreallocation(C,bs,dnnz,NULL,NULL,NULL);CHKERRQ(ierr); 10446 ierr = PetscFree(dnnz);CHKERRQ(ierr); 10447 ierr = MatGetOwnershipRange(C,&rstart,&rend);CHKERRQ(ierr); 10448 ierr = MatSetOption(C,MAT_ROW_ORIENTED,PETSC_FALSE);CHKERRQ(ierr); 10449 for (i = rstart/bs; i < rend/bs; i++) { 10450 ierr = MatSetValuesBlocked(C,1,&i,1,&i,&vals[(i-rstart/bs)*bs*bs],INSERT_VALUES);CHKERRQ(ierr); 10451 } 10452 ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 10453 ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 10454 ierr = MatSetOption(C,MAT_ROW_ORIENTED,PETSC_TRUE);CHKERRQ(ierr); 10455 PetscFunctionReturn(0); 10456 } 10457 10458 /*@C 10459 MatTransposeColoringDestroy - Destroys a coloring context for matrix product C=A*B^T that was created 10460 via MatTransposeColoringCreate(). 10461 10462 Collective on MatTransposeColoring 10463 10464 Input Parameter: 10465 . c - coloring context 10466 10467 Level: intermediate 10468 10469 .seealso: MatTransposeColoringCreate() 10470 @*/ 10471 PetscErrorCode MatTransposeColoringDestroy(MatTransposeColoring *c) 10472 { 10473 PetscErrorCode ierr; 10474 MatTransposeColoring matcolor=*c; 10475 10476 PetscFunctionBegin; 10477 if (!matcolor) PetscFunctionReturn(0); 10478 if (--((PetscObject)matcolor)->refct > 0) {matcolor = NULL; PetscFunctionReturn(0);} 10479 10480 ierr = PetscFree3(matcolor->ncolumns,matcolor->nrows,matcolor->colorforrow);CHKERRQ(ierr); 10481 ierr = PetscFree(matcolor->rows);CHKERRQ(ierr); 10482 ierr = PetscFree(matcolor->den2sp);CHKERRQ(ierr); 10483 ierr = PetscFree(matcolor->colorforcol);CHKERRQ(ierr); 10484 ierr = PetscFree(matcolor->columns);CHKERRQ(ierr); 10485 if (matcolor->brows>0) { 10486 ierr = PetscFree(matcolor->lstart);CHKERRQ(ierr); 10487 } 10488 ierr = PetscHeaderDestroy(c);CHKERRQ(ierr); 10489 PetscFunctionReturn(0); 10490 } 10491 10492 /*@C 10493 MatTransColoringApplySpToDen - Given a symbolic matrix product C=A*B^T for which 10494 a MatTransposeColoring context has been created, computes a dense B^T by Apply 10495 MatTransposeColoring to sparse B. 10496 10497 Collective on MatTransposeColoring 10498 10499 Input Parameters: 10500 + B - sparse matrix B 10501 . Btdense - symbolic dense matrix B^T 10502 - coloring - coloring context created with MatTransposeColoringCreate() 10503 10504 Output Parameter: 10505 . Btdense - dense matrix B^T 10506 10507 Level: advanced 10508 10509 Notes: 10510 These are used internally for some implementations of MatRARt() 10511 10512 .seealso: MatTransposeColoringCreate(), MatTransposeColoringDestroy(), MatTransColoringApplyDenToSp() 10513 10514 @*/ 10515 PetscErrorCode MatTransColoringApplySpToDen(MatTransposeColoring coloring,Mat B,Mat Btdense) 10516 { 10517 PetscErrorCode ierr; 10518 10519 PetscFunctionBegin; 10520 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 10521 PetscValidHeaderSpecific(Btdense,MAT_CLASSID,3); 10522 PetscValidHeaderSpecific(coloring,MAT_TRANSPOSECOLORING_CLASSID,1); 10523 10524 PetscCheckFalse(!B->ops->transcoloringapplysptoden,PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported for this matrix type %s",((PetscObject)B)->type_name); 10525 ierr = (B->ops->transcoloringapplysptoden)(coloring,B,Btdense);CHKERRQ(ierr); 10526 PetscFunctionReturn(0); 10527 } 10528 10529 /*@C 10530 MatTransColoringApplyDenToSp - Given a symbolic matrix product Csp=A*B^T for which 10531 a MatTransposeColoring context has been created and a dense matrix Cden=A*Btdense 10532 in which Btdens is obtained from MatTransColoringApplySpToDen(), recover sparse matrix 10533 Csp from Cden. 10534 10535 Collective on MatTransposeColoring 10536 10537 Input Parameters: 10538 + coloring - coloring context created with MatTransposeColoringCreate() 10539 - Cden - matrix product of a sparse matrix and a dense matrix Btdense 10540 10541 Output Parameter: 10542 . Csp - sparse matrix 10543 10544 Level: advanced 10545 10546 Notes: 10547 These are used internally for some implementations of MatRARt() 10548 10549 .seealso: MatTransposeColoringCreate(), MatTransposeColoringDestroy(), MatTransColoringApplySpToDen() 10550 10551 @*/ 10552 PetscErrorCode MatTransColoringApplyDenToSp(MatTransposeColoring matcoloring,Mat Cden,Mat Csp) 10553 { 10554 PetscErrorCode ierr; 10555 10556 PetscFunctionBegin; 10557 PetscValidHeaderSpecific(matcoloring,MAT_TRANSPOSECOLORING_CLASSID,1); 10558 PetscValidHeaderSpecific(Cden,MAT_CLASSID,2); 10559 PetscValidHeaderSpecific(Csp,MAT_CLASSID,3); 10560 10561 PetscCheckFalse(!Csp->ops->transcoloringapplydentosp,PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported for this matrix type %s",((PetscObject)Csp)->type_name); 10562 ierr = (Csp->ops->transcoloringapplydentosp)(matcoloring,Cden,Csp);CHKERRQ(ierr); 10563 ierr = MatAssemblyBegin(Csp,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 10564 ierr = MatAssemblyEnd(Csp,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 10565 PetscFunctionReturn(0); 10566 } 10567 10568 /*@C 10569 MatTransposeColoringCreate - Creates a matrix coloring context for matrix product C=A*B^T. 10570 10571 Collective on Mat 10572 10573 Input Parameters: 10574 + mat - the matrix product C 10575 - iscoloring - the coloring of the matrix; usually obtained with MatColoringCreate() or DMCreateColoring() 10576 10577 Output Parameter: 10578 . color - the new coloring context 10579 10580 Level: intermediate 10581 10582 .seealso: MatTransposeColoringDestroy(), MatTransColoringApplySpToDen(), 10583 MatTransColoringApplyDenToSp() 10584 @*/ 10585 PetscErrorCode MatTransposeColoringCreate(Mat mat,ISColoring iscoloring,MatTransposeColoring *color) 10586 { 10587 MatTransposeColoring c; 10588 MPI_Comm comm; 10589 PetscErrorCode ierr; 10590 10591 PetscFunctionBegin; 10592 ierr = PetscLogEventBegin(MAT_TransposeColoringCreate,mat,0,0,0);CHKERRQ(ierr); 10593 ierr = PetscObjectGetComm((PetscObject)mat,&comm);CHKERRQ(ierr); 10594 ierr = PetscHeaderCreate(c,MAT_TRANSPOSECOLORING_CLASSID,"MatTransposeColoring","Matrix product C=A*B^T via coloring","Mat",comm,MatTransposeColoringDestroy,NULL);CHKERRQ(ierr); 10595 10596 c->ctype = iscoloring->ctype; 10597 if (mat->ops->transposecoloringcreate) { 10598 ierr = (*mat->ops->transposecoloringcreate)(mat,iscoloring,c);CHKERRQ(ierr); 10599 } else SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Code not yet written for matrix type %s",((PetscObject)mat)->type_name); 10600 10601 *color = c; 10602 ierr = PetscLogEventEnd(MAT_TransposeColoringCreate,mat,0,0,0);CHKERRQ(ierr); 10603 PetscFunctionReturn(0); 10604 } 10605 10606 /*@ 10607 MatGetNonzeroState - Returns a 64 bit integer representing the current state of nonzeros in the matrix. If the 10608 matrix has had no new nonzero locations added to the matrix since the previous call then the value will be the 10609 same, otherwise it will be larger 10610 10611 Not Collective 10612 10613 Input Parameter: 10614 . A - the matrix 10615 10616 Output Parameter: 10617 . state - the current state 10618 10619 Notes: 10620 You can only compare states from two different calls to the SAME matrix, you cannot compare calls between 10621 different matrices 10622 10623 Level: intermediate 10624 10625 @*/ 10626 PetscErrorCode MatGetNonzeroState(Mat mat,PetscObjectState *state) 10627 { 10628 PetscFunctionBegin; 10629 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10630 *state = mat->nonzerostate; 10631 PetscFunctionReturn(0); 10632 } 10633 10634 /*@ 10635 MatCreateMPIMatConcatenateSeqMat - Creates a single large PETSc matrix by concatenating sequential 10636 matrices from each processor 10637 10638 Collective 10639 10640 Input Parameters: 10641 + comm - the communicators the parallel matrix will live on 10642 . seqmat - the input sequential matrices 10643 . n - number of local columns (or PETSC_DECIDE) 10644 - reuse - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 10645 10646 Output Parameter: 10647 . mpimat - the parallel matrix generated 10648 10649 Level: advanced 10650 10651 Notes: 10652 The number of columns of the matrix in EACH processor MUST be the same. 10653 10654 @*/ 10655 PetscErrorCode MatCreateMPIMatConcatenateSeqMat(MPI_Comm comm,Mat seqmat,PetscInt n,MatReuse reuse,Mat *mpimat) 10656 { 10657 PetscErrorCode ierr; 10658 10659 PetscFunctionBegin; 10660 PetscCheckFalse(!seqmat->ops->creatempimatconcatenateseqmat,PetscObjectComm((PetscObject)seqmat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)seqmat)->type_name); 10661 PetscCheckFalse(reuse == MAT_REUSE_MATRIX && seqmat == *mpimat,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"MAT_REUSE_MATRIX means reuse the matrix passed in as the final argument, not the original matrix"); 10662 10663 ierr = PetscLogEventBegin(MAT_Merge,seqmat,0,0,0);CHKERRQ(ierr); 10664 ierr = (*seqmat->ops->creatempimatconcatenateseqmat)(comm,seqmat,n,reuse,mpimat);CHKERRQ(ierr); 10665 ierr = PetscLogEventEnd(MAT_Merge,seqmat,0,0,0);CHKERRQ(ierr); 10666 PetscFunctionReturn(0); 10667 } 10668 10669 /*@ 10670 MatSubdomainsCreateCoalesce - Creates index subdomains by coalescing adjacent 10671 ranks' ownership ranges. 10672 10673 Collective on A 10674 10675 Input Parameters: 10676 + A - the matrix to create subdomains from 10677 - N - requested number of subdomains 10678 10679 Output Parameters: 10680 + n - number of subdomains resulting on this rank 10681 - iss - IS list with indices of subdomains on this rank 10682 10683 Level: advanced 10684 10685 Notes: 10686 number of subdomains must be smaller than the communicator size 10687 @*/ 10688 PetscErrorCode MatSubdomainsCreateCoalesce(Mat A,PetscInt N,PetscInt *n,IS *iss[]) 10689 { 10690 MPI_Comm comm,subcomm; 10691 PetscMPIInt size,rank,color; 10692 PetscInt rstart,rend,k; 10693 PetscErrorCode ierr; 10694 10695 PetscFunctionBegin; 10696 ierr = PetscObjectGetComm((PetscObject)A,&comm);CHKERRQ(ierr); 10697 ierr = MPI_Comm_size(comm,&size);CHKERRMPI(ierr); 10698 ierr = MPI_Comm_rank(comm,&rank);CHKERRMPI(ierr); 10699 PetscCheck(N >= 1 && N < (PetscInt)size,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"number of subdomains must be > 0 and < %d, got N = %" PetscInt_FMT,size,N); 10700 *n = 1; 10701 k = ((PetscInt)size)/N + ((PetscInt)size%N>0); /* There are up to k ranks to a color */ 10702 color = rank/k; 10703 ierr = MPI_Comm_split(comm,color,rank,&subcomm);CHKERRMPI(ierr); 10704 ierr = PetscMalloc1(1,iss);CHKERRQ(ierr); 10705 ierr = MatGetOwnershipRange(A,&rstart,&rend);CHKERRQ(ierr); 10706 ierr = ISCreateStride(subcomm,rend-rstart,rstart,1,iss[0]);CHKERRQ(ierr); 10707 ierr = MPI_Comm_free(&subcomm);CHKERRMPI(ierr); 10708 PetscFunctionReturn(0); 10709 } 10710 10711 /*@ 10712 MatGalerkin - Constructs the coarse grid problem via Galerkin projection. 10713 10714 If the interpolation and restriction operators are the same, uses MatPtAP. 10715 If they are not the same, use MatMatMatMult. 10716 10717 Once the coarse grid problem is constructed, correct for interpolation operators 10718 that are not of full rank, which can legitimately happen in the case of non-nested 10719 geometric multigrid. 10720 10721 Input Parameters: 10722 + restrct - restriction operator 10723 . dA - fine grid matrix 10724 . interpolate - interpolation operator 10725 . reuse - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 10726 - fill - expected fill, use PETSC_DEFAULT if you do not have a good estimate 10727 10728 Output Parameters: 10729 . A - the Galerkin coarse matrix 10730 10731 Options Database Key: 10732 . -pc_mg_galerkin <both,pmat,mat,none> 10733 10734 Level: developer 10735 10736 .seealso: MatPtAP(), MatMatMatMult() 10737 @*/ 10738 PetscErrorCode MatGalerkin(Mat restrct, Mat dA, Mat interpolate, MatReuse reuse, PetscReal fill, Mat *A) 10739 { 10740 PetscErrorCode ierr; 10741 IS zerorows; 10742 Vec diag; 10743 10744 PetscFunctionBegin; 10745 PetscCheckFalse(reuse == MAT_INPLACE_MATRIX,PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported"); 10746 /* Construct the coarse grid matrix */ 10747 if (interpolate == restrct) { 10748 ierr = MatPtAP(dA,interpolate,reuse,fill,A);CHKERRQ(ierr); 10749 } else { 10750 ierr = MatMatMatMult(restrct,dA,interpolate,reuse,fill,A);CHKERRQ(ierr); 10751 } 10752 10753 /* If the interpolation matrix is not of full rank, A will have zero rows. 10754 This can legitimately happen in the case of non-nested geometric multigrid. 10755 In that event, we set the rows of the matrix to the rows of the identity, 10756 ignoring the equations (as the RHS will also be zero). */ 10757 10758 ierr = MatFindZeroRows(*A, &zerorows);CHKERRQ(ierr); 10759 10760 if (zerorows != NULL) { /* if there are any zero rows */ 10761 ierr = MatCreateVecs(*A, &diag, NULL);CHKERRQ(ierr); 10762 ierr = MatGetDiagonal(*A, diag);CHKERRQ(ierr); 10763 ierr = VecISSet(diag, zerorows, 1.0);CHKERRQ(ierr); 10764 ierr = MatDiagonalSet(*A, diag, INSERT_VALUES);CHKERRQ(ierr); 10765 ierr = VecDestroy(&diag);CHKERRQ(ierr); 10766 ierr = ISDestroy(&zerorows);CHKERRQ(ierr); 10767 } 10768 PetscFunctionReturn(0); 10769 } 10770 10771 /*@C 10772 MatSetOperation - Allows user to set a matrix operation for any matrix type 10773 10774 Logically Collective on Mat 10775 10776 Input Parameters: 10777 + mat - the matrix 10778 . op - the name of the operation 10779 - f - the function that provides the operation 10780 10781 Level: developer 10782 10783 Usage: 10784 $ extern PetscErrorCode usermult(Mat,Vec,Vec); 10785 $ ierr = MatCreateXXX(comm,...&A); 10786 $ ierr = MatSetOperation(A,MATOP_MULT,(void(*)(void))usermult); 10787 10788 Notes: 10789 See the file include/petscmat.h for a complete list of matrix 10790 operations, which all have the form MATOP_<OPERATION>, where 10791 <OPERATION> is the name (in all capital letters) of the 10792 user interface routine (e.g., MatMult() -> MATOP_MULT). 10793 10794 All user-provided functions (except for MATOP_DESTROY) should have the same calling 10795 sequence as the usual matrix interface routines, since they 10796 are intended to be accessed via the usual matrix interface 10797 routines, e.g., 10798 $ MatMult(Mat,Vec,Vec) -> usermult(Mat,Vec,Vec) 10799 10800 In particular each function MUST return an error code of 0 on success and 10801 nonzero on failure. 10802 10803 This routine is distinct from MatShellSetOperation() in that it can be called on any matrix type. 10804 10805 .seealso: MatGetOperation(), MatCreateShell(), MatShellSetContext(), MatShellSetOperation() 10806 @*/ 10807 PetscErrorCode MatSetOperation(Mat mat,MatOperation op,void (*f)(void)) 10808 { 10809 PetscFunctionBegin; 10810 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10811 if (op == MATOP_VIEW && !mat->ops->viewnative && f != (void (*)(void))(mat->ops->view)) { 10812 mat->ops->viewnative = mat->ops->view; 10813 } 10814 (((void(**)(void))mat->ops)[op]) = f; 10815 PetscFunctionReturn(0); 10816 } 10817 10818 /*@C 10819 MatGetOperation - Gets a matrix operation for any matrix type. 10820 10821 Not Collective 10822 10823 Input Parameters: 10824 + mat - the matrix 10825 - op - the name of the operation 10826 10827 Output Parameter: 10828 . f - the function that provides the operation 10829 10830 Level: developer 10831 10832 Usage: 10833 $ PetscErrorCode (*usermult)(Mat,Vec,Vec); 10834 $ ierr = MatGetOperation(A,MATOP_MULT,(void(**)(void))&usermult); 10835 10836 Notes: 10837 See the file include/petscmat.h for a complete list of matrix 10838 operations, which all have the form MATOP_<OPERATION>, where 10839 <OPERATION> is the name (in all capital letters) of the 10840 user interface routine (e.g., MatMult() -> MATOP_MULT). 10841 10842 This routine is distinct from MatShellGetOperation() in that it can be called on any matrix type. 10843 10844 .seealso: MatSetOperation(), MatCreateShell(), MatShellGetContext(), MatShellGetOperation() 10845 @*/ 10846 PetscErrorCode MatGetOperation(Mat mat,MatOperation op,void(**f)(void)) 10847 { 10848 PetscFunctionBegin; 10849 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10850 *f = (((void (**)(void))mat->ops)[op]); 10851 PetscFunctionReturn(0); 10852 } 10853 10854 /*@ 10855 MatHasOperation - Determines whether the given matrix supports the particular 10856 operation. 10857 10858 Not Collective 10859 10860 Input Parameters: 10861 + mat - the matrix 10862 - op - the operation, for example, MATOP_GET_DIAGONAL 10863 10864 Output Parameter: 10865 . has - either PETSC_TRUE or PETSC_FALSE 10866 10867 Level: advanced 10868 10869 Notes: 10870 See the file include/petscmat.h for a complete list of matrix 10871 operations, which all have the form MATOP_<OPERATION>, where 10872 <OPERATION> is the name (in all capital letters) of the 10873 user-level routine. E.g., MatNorm() -> MATOP_NORM. 10874 10875 .seealso: MatCreateShell() 10876 @*/ 10877 PetscErrorCode MatHasOperation(Mat mat,MatOperation op,PetscBool *has) 10878 { 10879 PetscErrorCode ierr; 10880 10881 PetscFunctionBegin; 10882 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10883 PetscValidPointer(has,3); 10884 if (mat->ops->hasoperation) { 10885 ierr = (*mat->ops->hasoperation)(mat,op,has);CHKERRQ(ierr); 10886 } else { 10887 if (((void**)mat->ops)[op]) *has = PETSC_TRUE; 10888 else { 10889 *has = PETSC_FALSE; 10890 if (op == MATOP_CREATE_SUBMATRIX) { 10891 PetscMPIInt size; 10892 10893 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);CHKERRMPI(ierr); 10894 if (size == 1) { 10895 ierr = MatHasOperation(mat,MATOP_CREATE_SUBMATRICES,has);CHKERRQ(ierr); 10896 } 10897 } 10898 } 10899 } 10900 PetscFunctionReturn(0); 10901 } 10902 10903 /*@ 10904 MatHasCongruentLayouts - Determines whether the rows and columns layouts 10905 of the matrix are congruent 10906 10907 Collective on mat 10908 10909 Input Parameters: 10910 . mat - the matrix 10911 10912 Output Parameter: 10913 . cong - either PETSC_TRUE or PETSC_FALSE 10914 10915 Level: beginner 10916 10917 Notes: 10918 10919 .seealso: MatCreate(), MatSetSizes() 10920 @*/ 10921 PetscErrorCode MatHasCongruentLayouts(Mat mat,PetscBool *cong) 10922 { 10923 PetscErrorCode ierr; 10924 10925 PetscFunctionBegin; 10926 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10927 PetscValidType(mat,1); 10928 PetscValidPointer(cong,2); 10929 if (!mat->rmap || !mat->cmap) { 10930 *cong = mat->rmap == mat->cmap ? PETSC_TRUE : PETSC_FALSE; 10931 PetscFunctionReturn(0); 10932 } 10933 if (mat->congruentlayouts == PETSC_DECIDE) { /* first time we compare rows and cols layouts */ 10934 ierr = PetscLayoutSetUp(mat->rmap);CHKERRQ(ierr); 10935 ierr = PetscLayoutSetUp(mat->cmap);CHKERRQ(ierr); 10936 ierr = PetscLayoutCompare(mat->rmap,mat->cmap,cong);CHKERRQ(ierr); 10937 if (*cong) mat->congruentlayouts = 1; 10938 else mat->congruentlayouts = 0; 10939 } else *cong = mat->congruentlayouts ? PETSC_TRUE : PETSC_FALSE; 10940 PetscFunctionReturn(0); 10941 } 10942 10943 PetscErrorCode MatSetInf(Mat A) 10944 { 10945 PetscErrorCode ierr; 10946 10947 PetscFunctionBegin; 10948 PetscCheckFalse(!A->ops->setinf,PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"No support for this operation for this matrix type"); 10949 ierr = (*A->ops->setinf)(A);CHKERRQ(ierr); 10950 PetscFunctionReturn(0); 10951 } 10952