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 1. 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 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. 4877 4878 .seealso: MatCopy(), MatConvert(), MatDuplicateOption 4879 @*/ 4880 PetscErrorCode MatDuplicate(Mat mat,MatDuplicateOption op,Mat *M) 4881 { 4882 PetscErrorCode ierr; 4883 Mat B; 4884 VecType vtype; 4885 PetscInt i; 4886 PetscObject dm; 4887 void (*viewf)(void); 4888 4889 PetscFunctionBegin; 4890 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4891 PetscValidType(mat,1); 4892 PetscValidPointer(M,3); 4893 PetscCheckFalse(op == MAT_COPY_VALUES && !mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"MAT_COPY_VALUES not allowed for unassembled matrix"); 4894 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4895 MatCheckPreallocated(mat,1); 4896 4897 *M = NULL; 4898 PetscCheckFalse(!mat->ops->duplicate,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Not written for matrix type %s",((PetscObject)mat)->type_name); 4899 ierr = PetscLogEventBegin(MAT_Convert,mat,0,0,0);CHKERRQ(ierr); 4900 ierr = (*mat->ops->duplicate)(mat,op,M);CHKERRQ(ierr); 4901 ierr = PetscLogEventEnd(MAT_Convert,mat,0,0,0);CHKERRQ(ierr); 4902 B = *M; 4903 4904 ierr = MatGetOperation(mat,MATOP_VIEW,&viewf);CHKERRQ(ierr); 4905 if (viewf) { 4906 ierr = MatSetOperation(B,MATOP_VIEW,viewf);CHKERRQ(ierr); 4907 } 4908 ierr = MatGetVecType(mat,&vtype);CHKERRQ(ierr); 4909 ierr = MatSetVecType(B,vtype);CHKERRQ(ierr); 4910 4911 B->stencil.dim = mat->stencil.dim; 4912 B->stencil.noc = mat->stencil.noc; 4913 for (i=0; i<=mat->stencil.dim; i++) { 4914 B->stencil.dims[i] = mat->stencil.dims[i]; 4915 B->stencil.starts[i] = mat->stencil.starts[i]; 4916 } 4917 4918 B->nooffproczerorows = mat->nooffproczerorows; 4919 B->nooffprocentries = mat->nooffprocentries; 4920 4921 ierr = PetscObjectQuery((PetscObject) mat, "__PETSc_dm", &dm);CHKERRQ(ierr); 4922 if (dm) { 4923 ierr = PetscObjectCompose((PetscObject) B, "__PETSc_dm", dm);CHKERRQ(ierr); 4924 } 4925 ierr = PetscObjectStateIncrease((PetscObject)B);CHKERRQ(ierr); 4926 PetscFunctionReturn(0); 4927 } 4928 4929 /*@ 4930 MatGetDiagonal - Gets the diagonal of a matrix. 4931 4932 Logically Collective on Mat 4933 4934 Input Parameters: 4935 + mat - the matrix 4936 - v - the vector for storing the diagonal 4937 4938 Output Parameter: 4939 . v - the diagonal of the matrix 4940 4941 Level: intermediate 4942 4943 Note: 4944 Currently only correct in parallel for square matrices. 4945 4946 .seealso: MatGetRow(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMaxAbs() 4947 @*/ 4948 PetscErrorCode MatGetDiagonal(Mat mat,Vec v) 4949 { 4950 PetscErrorCode ierr; 4951 4952 PetscFunctionBegin; 4953 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4954 PetscValidType(mat,1); 4955 PetscValidHeaderSpecific(v,VEC_CLASSID,2); 4956 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4957 PetscCheckFalse(!mat->ops->getdiagonal,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 4958 MatCheckPreallocated(mat,1); 4959 4960 ierr = (*mat->ops->getdiagonal)(mat,v);CHKERRQ(ierr); 4961 ierr = PetscObjectStateIncrease((PetscObject)v);CHKERRQ(ierr); 4962 PetscFunctionReturn(0); 4963 } 4964 4965 /*@C 4966 MatGetRowMin - Gets the minimum value (of the real part) of each 4967 row of the matrix 4968 4969 Logically Collective on Mat 4970 4971 Input Parameter: 4972 . mat - the matrix 4973 4974 Output Parameters: 4975 + v - the vector for storing the maximums 4976 - idx - the indices of the column found for each row (optional) 4977 4978 Level: intermediate 4979 4980 Notes: 4981 The result of this call are the same as if one converted the matrix to dense format 4982 and found the minimum value in each row (i.e. the implicit zeros are counted as zeros). 4983 4984 This code is only implemented for a couple of matrix formats. 4985 4986 .seealso: MatGetDiagonal(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMaxAbs(), 4987 MatGetRowMax() 4988 @*/ 4989 PetscErrorCode MatGetRowMin(Mat mat,Vec v,PetscInt idx[]) 4990 { 4991 PetscErrorCode ierr; 4992 4993 PetscFunctionBegin; 4994 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4995 PetscValidType(mat,1); 4996 PetscValidHeaderSpecific(v,VEC_CLASSID,2); 4997 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4998 4999 if (!mat->cmap->N) { 5000 ierr = VecSet(v,PETSC_MAX_REAL);CHKERRQ(ierr); 5001 if (idx) { 5002 PetscInt i,m = mat->rmap->n; 5003 for (i=0; i<m; i++) idx[i] = -1; 5004 } 5005 } else { 5006 PetscCheckFalse(!mat->ops->getrowmin,PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 5007 MatCheckPreallocated(mat,1); 5008 } 5009 ierr = (*mat->ops->getrowmin)(mat,v,idx);CHKERRQ(ierr); 5010 ierr = PetscObjectStateIncrease((PetscObject)v);CHKERRQ(ierr); 5011 PetscFunctionReturn(0); 5012 } 5013 5014 /*@C 5015 MatGetRowMinAbs - Gets the minimum value (in absolute value) of each 5016 row of the matrix 5017 5018 Logically Collective on Mat 5019 5020 Input Parameter: 5021 . mat - the matrix 5022 5023 Output Parameters: 5024 + v - the vector for storing the minimums 5025 - idx - the indices of the column found for each row (or NULL if not needed) 5026 5027 Level: intermediate 5028 5029 Notes: 5030 if a row is completely empty or has only 0.0 values then the idx[] value for that 5031 row is 0 (the first column). 5032 5033 This code is only implemented for a couple of matrix formats. 5034 5035 .seealso: MatGetDiagonal(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMax(), MatGetRowMaxAbs(), MatGetRowMin() 5036 @*/ 5037 PetscErrorCode MatGetRowMinAbs(Mat mat,Vec v,PetscInt idx[]) 5038 { 5039 PetscErrorCode ierr; 5040 5041 PetscFunctionBegin; 5042 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5043 PetscValidType(mat,1); 5044 PetscValidHeaderSpecific(v,VEC_CLASSID,2); 5045 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5046 PetscCheckFalse(mat->factortype,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5047 5048 if (!mat->cmap->N) { 5049 ierr = VecSet(v,0.0);CHKERRQ(ierr); 5050 if (idx) { 5051 PetscInt i,m = mat->rmap->n; 5052 for (i=0; i<m; i++) idx[i] = -1; 5053 } 5054 } else { 5055 PetscCheckFalse(!mat->ops->getrowminabs,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 5056 MatCheckPreallocated(mat,1); 5057 if (idx) {ierr = PetscArrayzero(idx,mat->rmap->n);CHKERRQ(ierr);} 5058 ierr = (*mat->ops->getrowminabs)(mat,v,idx);CHKERRQ(ierr); 5059 } 5060 ierr = PetscObjectStateIncrease((PetscObject)v);CHKERRQ(ierr); 5061 PetscFunctionReturn(0); 5062 } 5063 5064 /*@C 5065 MatGetRowMax - Gets the maximum value (of the real part) of each 5066 row of the matrix 5067 5068 Logically Collective on Mat 5069 5070 Input Parameter: 5071 . mat - the matrix 5072 5073 Output Parameters: 5074 + v - the vector for storing the maximums 5075 - idx - the indices of the column found for each row (optional) 5076 5077 Level: intermediate 5078 5079 Notes: 5080 The result of this call are the same as if one converted the matrix to dense format 5081 and found the minimum value in each row (i.e. the implicit zeros are counted as zeros). 5082 5083 This code is only implemented for a couple of matrix formats. 5084 5085 .seealso: MatGetDiagonal(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMaxAbs(), MatGetRowMin() 5086 @*/ 5087 PetscErrorCode MatGetRowMax(Mat mat,Vec v,PetscInt idx[]) 5088 { 5089 PetscErrorCode ierr; 5090 5091 PetscFunctionBegin; 5092 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5093 PetscValidType(mat,1); 5094 PetscValidHeaderSpecific(v,VEC_CLASSID,2); 5095 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5096 5097 if (!mat->cmap->N) { 5098 ierr = VecSet(v,PETSC_MIN_REAL);CHKERRQ(ierr); 5099 if (idx) { 5100 PetscInt i,m = mat->rmap->n; 5101 for (i=0; i<m; i++) idx[i] = -1; 5102 } 5103 } else { 5104 PetscCheckFalse(!mat->ops->getrowmax,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 5105 MatCheckPreallocated(mat,1); 5106 ierr = (*mat->ops->getrowmax)(mat,v,idx);CHKERRQ(ierr); 5107 } 5108 ierr = PetscObjectStateIncrease((PetscObject)v);CHKERRQ(ierr); 5109 PetscFunctionReturn(0); 5110 } 5111 5112 /*@C 5113 MatGetRowMaxAbs - Gets the maximum value (in absolute value) of each 5114 row of the matrix 5115 5116 Logically Collective on Mat 5117 5118 Input Parameter: 5119 . mat - the matrix 5120 5121 Output Parameters: 5122 + v - the vector for storing the maximums 5123 - idx - the indices of the column found for each row (or NULL if not needed) 5124 5125 Level: intermediate 5126 5127 Notes: 5128 if a row is completely empty or has only 0.0 values then the idx[] value for that 5129 row is 0 (the first column). 5130 5131 This code is only implemented for a couple of matrix formats. 5132 5133 .seealso: MatGetDiagonal(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMax(), MatGetRowMin() 5134 @*/ 5135 PetscErrorCode MatGetRowMaxAbs(Mat mat,Vec v,PetscInt idx[]) 5136 { 5137 PetscErrorCode ierr; 5138 5139 PetscFunctionBegin; 5140 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5141 PetscValidType(mat,1); 5142 PetscValidHeaderSpecific(v,VEC_CLASSID,2); 5143 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5144 5145 if (!mat->cmap->N) { 5146 ierr = VecSet(v,0.0);CHKERRQ(ierr); 5147 if (idx) { 5148 PetscInt i,m = mat->rmap->n; 5149 for (i=0; i<m; i++) idx[i] = -1; 5150 } 5151 } else { 5152 PetscCheckFalse(!mat->ops->getrowmaxabs,PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 5153 MatCheckPreallocated(mat,1); 5154 if (idx) {ierr = PetscArrayzero(idx,mat->rmap->n);CHKERRQ(ierr);} 5155 ierr = (*mat->ops->getrowmaxabs)(mat,v,idx);CHKERRQ(ierr); 5156 } 5157 ierr = PetscObjectStateIncrease((PetscObject)v);CHKERRQ(ierr); 5158 PetscFunctionReturn(0); 5159 } 5160 5161 /*@ 5162 MatGetRowSum - Gets the sum of each row of the matrix 5163 5164 Logically or Neighborhood Collective on Mat 5165 5166 Input Parameters: 5167 . mat - the matrix 5168 5169 Output Parameter: 5170 . v - the vector for storing the sum of rows 5171 5172 Level: intermediate 5173 5174 Notes: 5175 This code is slow since it is not currently specialized for different formats 5176 5177 .seealso: MatGetDiagonal(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMax(), MatGetRowMin() 5178 @*/ 5179 PetscErrorCode MatGetRowSum(Mat mat, Vec v) 5180 { 5181 Vec ones; 5182 PetscErrorCode ierr; 5183 5184 PetscFunctionBegin; 5185 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5186 PetscValidType(mat,1); 5187 PetscValidHeaderSpecific(v,VEC_CLASSID,2); 5188 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5189 MatCheckPreallocated(mat,1); 5190 ierr = MatCreateVecs(mat,&ones,NULL);CHKERRQ(ierr); 5191 ierr = VecSet(ones,1.);CHKERRQ(ierr); 5192 ierr = MatMult(mat,ones,v);CHKERRQ(ierr); 5193 ierr = VecDestroy(&ones);CHKERRQ(ierr); 5194 PetscFunctionReturn(0); 5195 } 5196 5197 /*@ 5198 MatTranspose - Computes an in-place or out-of-place transpose of a matrix. 5199 5200 Collective on Mat 5201 5202 Input Parameters: 5203 + mat - the matrix to transpose 5204 - reuse - either MAT_INITIAL_MATRIX, MAT_REUSE_MATRIX, or MAT_INPLACE_MATRIX 5205 5206 Output Parameter: 5207 . B - the transpose 5208 5209 Notes: 5210 If you use MAT_INPLACE_MATRIX then you must pass in &mat for B 5211 5212 MAT_REUSE_MATRIX causes the B matrix from a previous call to this function with MAT_INITIAL_MATRIX to be used 5213 5214 Consider using MatCreateTranspose() instead if you only need a matrix that behaves like the transpose, but don't need the storage to be changed. 5215 5216 Level: intermediate 5217 5218 .seealso: MatMultTranspose(), MatMultTransposeAdd(), MatIsTranspose(), MatReuse 5219 @*/ 5220 PetscErrorCode MatTranspose(Mat mat,MatReuse reuse,Mat *B) 5221 { 5222 PetscErrorCode ierr; 5223 5224 PetscFunctionBegin; 5225 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5226 PetscValidType(mat,1); 5227 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5228 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5229 PetscCheckFalse(!mat->ops->transpose,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 5230 PetscCheckFalse(reuse == MAT_INPLACE_MATRIX && mat != *B,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"MAT_INPLACE_MATRIX requires last matrix to match first"); 5231 PetscCheckFalse(reuse == MAT_REUSE_MATRIX && mat == *B,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Perhaps you mean MAT_INPLACE_MATRIX"); 5232 MatCheckPreallocated(mat,1); 5233 5234 ierr = PetscLogEventBegin(MAT_Transpose,mat,0,0,0);CHKERRQ(ierr); 5235 ierr = (*mat->ops->transpose)(mat,reuse,B);CHKERRQ(ierr); 5236 ierr = PetscLogEventEnd(MAT_Transpose,mat,0,0,0);CHKERRQ(ierr); 5237 if (B) {ierr = PetscObjectStateIncrease((PetscObject)*B);CHKERRQ(ierr);} 5238 PetscFunctionReturn(0); 5239 } 5240 5241 /*@ 5242 MatIsTranspose - Test whether a matrix is another one's transpose, 5243 or its own, in which case it tests symmetry. 5244 5245 Collective on Mat 5246 5247 Input Parameters: 5248 + A - the matrix to test 5249 - B - the matrix to test against, this can equal the first parameter 5250 5251 Output Parameters: 5252 . flg - the result 5253 5254 Notes: 5255 Only available for SeqAIJ/MPIAIJ matrices. The sequential algorithm 5256 has a running time of the order of the number of nonzeros; the parallel 5257 test involves parallel copies of the block-offdiagonal parts of the matrix. 5258 5259 Level: intermediate 5260 5261 .seealso: MatTranspose(), MatIsSymmetric(), MatIsHermitian() 5262 @*/ 5263 PetscErrorCode MatIsTranspose(Mat A,Mat B,PetscReal tol,PetscBool *flg) 5264 { 5265 PetscErrorCode ierr,(*f)(Mat,Mat,PetscReal,PetscBool*),(*g)(Mat,Mat,PetscReal,PetscBool*); 5266 5267 PetscFunctionBegin; 5268 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 5269 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 5270 PetscValidBoolPointer(flg,4); 5271 ierr = PetscObjectQueryFunction((PetscObject)A,"MatIsTranspose_C",&f);CHKERRQ(ierr); 5272 ierr = PetscObjectQueryFunction((PetscObject)B,"MatIsTranspose_C",&g);CHKERRQ(ierr); 5273 *flg = PETSC_FALSE; 5274 if (f && g) { 5275 if (f == g) { 5276 ierr = (*f)(A,B,tol,flg);CHKERRQ(ierr); 5277 } else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_NOTSAMETYPE,"Matrices do not have the same comparator for symmetry test"); 5278 } else { 5279 MatType mattype; 5280 if (!f) { 5281 ierr = MatGetType(A,&mattype);CHKERRQ(ierr); 5282 } else { 5283 ierr = MatGetType(B,&mattype);CHKERRQ(ierr); 5284 } 5285 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type %s does not support checking for transpose",mattype); 5286 } 5287 PetscFunctionReturn(0); 5288 } 5289 5290 /*@ 5291 MatHermitianTranspose - Computes an in-place or out-of-place transpose of a matrix in complex conjugate. 5292 5293 Collective on Mat 5294 5295 Input Parameters: 5296 + mat - the matrix to transpose and complex conjugate 5297 - reuse - either MAT_INITIAL_MATRIX, MAT_REUSE_MATRIX, or MAT_INPLACE_MATRIX 5298 5299 Output Parameter: 5300 . B - the Hermitian 5301 5302 Level: intermediate 5303 5304 .seealso: MatTranspose(), MatMultTranspose(), MatMultTransposeAdd(), MatIsTranspose(), MatReuse 5305 @*/ 5306 PetscErrorCode MatHermitianTranspose(Mat mat,MatReuse reuse,Mat *B) 5307 { 5308 PetscErrorCode ierr; 5309 5310 PetscFunctionBegin; 5311 ierr = MatTranspose(mat,reuse,B);CHKERRQ(ierr); 5312 #if defined(PETSC_USE_COMPLEX) 5313 ierr = MatConjugate(*B);CHKERRQ(ierr); 5314 #endif 5315 PetscFunctionReturn(0); 5316 } 5317 5318 /*@ 5319 MatIsHermitianTranspose - Test whether a matrix is another one's Hermitian transpose, 5320 5321 Collective on Mat 5322 5323 Input Parameters: 5324 + A - the matrix to test 5325 - B - the matrix to test against, this can equal the first parameter 5326 5327 Output Parameters: 5328 . flg - the result 5329 5330 Notes: 5331 Only available for SeqAIJ/MPIAIJ matrices. The sequential algorithm 5332 has a running time of the order of the number of nonzeros; the parallel 5333 test involves parallel copies of the block-offdiagonal parts of the matrix. 5334 5335 Level: intermediate 5336 5337 .seealso: MatTranspose(), MatIsSymmetric(), MatIsHermitian(), MatIsTranspose() 5338 @*/ 5339 PetscErrorCode MatIsHermitianTranspose(Mat A,Mat B,PetscReal tol,PetscBool *flg) 5340 { 5341 PetscErrorCode ierr,(*f)(Mat,Mat,PetscReal,PetscBool*),(*g)(Mat,Mat,PetscReal,PetscBool*); 5342 5343 PetscFunctionBegin; 5344 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 5345 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 5346 PetscValidBoolPointer(flg,4); 5347 ierr = PetscObjectQueryFunction((PetscObject)A,"MatIsHermitianTranspose_C",&f);CHKERRQ(ierr); 5348 ierr = PetscObjectQueryFunction((PetscObject)B,"MatIsHermitianTranspose_C",&g);CHKERRQ(ierr); 5349 if (f && g) { 5350 if (f==g) { 5351 ierr = (*f)(A,B,tol,flg);CHKERRQ(ierr); 5352 } else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_NOTSAMETYPE,"Matrices do not have the same comparator for Hermitian test"); 5353 } 5354 PetscFunctionReturn(0); 5355 } 5356 5357 /*@ 5358 MatPermute - Creates a new matrix with rows and columns permuted from the 5359 original. 5360 5361 Collective on Mat 5362 5363 Input Parameters: 5364 + mat - the matrix to permute 5365 . row - row permutation, each processor supplies only the permutation for its rows 5366 - col - column permutation, each processor supplies only the permutation for its columns 5367 5368 Output Parameters: 5369 . B - the permuted matrix 5370 5371 Level: advanced 5372 5373 Note: 5374 The index sets map from row/col of permuted matrix to row/col of original matrix. 5375 The index sets should be on the same communicator as Mat and have the same local sizes. 5376 5377 Developer Note: 5378 If you want to implement MatPermute for a matrix type, and your approach doesn't 5379 exploit the fact that row and col are permutations, consider implementing the 5380 more general MatCreateSubMatrix() instead. 5381 5382 .seealso: MatGetOrdering(), ISAllGather() 5383 5384 @*/ 5385 PetscErrorCode MatPermute(Mat mat,IS row,IS col,Mat *B) 5386 { 5387 PetscErrorCode ierr; 5388 5389 PetscFunctionBegin; 5390 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5391 PetscValidType(mat,1); 5392 PetscValidHeaderSpecific(row,IS_CLASSID,2); 5393 PetscValidHeaderSpecific(col,IS_CLASSID,3); 5394 PetscValidPointer(B,4); 5395 PetscCheckSameComm(mat,1,row,2); 5396 if (row != col) PetscCheckSameComm(row,2,col,3); 5397 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5398 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5399 PetscCheckFalse(!mat->ops->permute && !mat->ops->createsubmatrix,PETSC_COMM_SELF,PETSC_ERR_SUP,"MatPermute not available for Mat type %s",((PetscObject)mat)->type_name); 5400 MatCheckPreallocated(mat,1); 5401 5402 if (mat->ops->permute) { 5403 ierr = (*mat->ops->permute)(mat,row,col,B);CHKERRQ(ierr); 5404 ierr = PetscObjectStateIncrease((PetscObject)*B);CHKERRQ(ierr); 5405 } else { 5406 ierr = MatCreateSubMatrix(mat, row, col, MAT_INITIAL_MATRIX, B);CHKERRQ(ierr); 5407 } 5408 PetscFunctionReturn(0); 5409 } 5410 5411 /*@ 5412 MatEqual - Compares two matrices. 5413 5414 Collective on Mat 5415 5416 Input Parameters: 5417 + A - the first matrix 5418 - B - the second matrix 5419 5420 Output Parameter: 5421 . flg - PETSC_TRUE if the matrices are equal; PETSC_FALSE otherwise. 5422 5423 Level: intermediate 5424 5425 @*/ 5426 PetscErrorCode MatEqual(Mat A,Mat B,PetscBool *flg) 5427 { 5428 PetscErrorCode ierr; 5429 5430 PetscFunctionBegin; 5431 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 5432 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 5433 PetscValidType(A,1); 5434 PetscValidType(B,2); 5435 PetscValidBoolPointer(flg,3); 5436 PetscCheckSameComm(A,1,B,2); 5437 MatCheckPreallocated(A,1); 5438 MatCheckPreallocated(B,2); 5439 PetscCheckFalse(!A->assembled,PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5440 PetscCheckFalse(!B->assembled,PetscObjectComm((PetscObject)B),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5441 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); 5442 if (A->ops->equal && A->ops->equal == B->ops->equal) { 5443 ierr = (*A->ops->equal)(A,B,flg);CHKERRQ(ierr); 5444 } else { 5445 ierr = MatMultEqual(A,B,10,flg);CHKERRQ(ierr); 5446 } 5447 PetscFunctionReturn(0); 5448 } 5449 5450 /*@ 5451 MatDiagonalScale - Scales a matrix on the left and right by diagonal 5452 matrices that are stored as vectors. Either of the two scaling 5453 matrices can be NULL. 5454 5455 Collective on Mat 5456 5457 Input Parameters: 5458 + mat - the matrix to be scaled 5459 . l - the left scaling vector (or NULL) 5460 - r - the right scaling vector (or NULL) 5461 5462 Notes: 5463 MatDiagonalScale() computes A = LAR, where 5464 L = a diagonal matrix (stored as a vector), R = a diagonal matrix (stored as a vector) 5465 The L scales the rows of the matrix, the R scales the columns of the matrix. 5466 5467 Level: intermediate 5468 5469 .seealso: MatScale(), MatShift(), MatDiagonalSet() 5470 @*/ 5471 PetscErrorCode MatDiagonalScale(Mat mat,Vec l,Vec r) 5472 { 5473 PetscErrorCode ierr; 5474 5475 PetscFunctionBegin; 5476 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5477 PetscValidType(mat,1); 5478 if (l) {PetscValidHeaderSpecific(l,VEC_CLASSID,2);PetscCheckSameComm(mat,1,l,2);} 5479 if (r) {PetscValidHeaderSpecific(r,VEC_CLASSID,3);PetscCheckSameComm(mat,1,r,3);} 5480 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5481 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5482 MatCheckPreallocated(mat,1); 5483 if (!l && !r) PetscFunctionReturn(0); 5484 5485 PetscCheckFalse(!mat->ops->diagonalscale,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 5486 ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 5487 ierr = (*mat->ops->diagonalscale)(mat,l,r);CHKERRQ(ierr); 5488 ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 5489 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 5490 if (l != r && mat->symmetric) mat->symmetric = PETSC_FALSE; 5491 PetscFunctionReturn(0); 5492 } 5493 5494 /*@ 5495 MatScale - Scales all elements of a matrix by a given number. 5496 5497 Logically Collective on Mat 5498 5499 Input Parameters: 5500 + mat - the matrix to be scaled 5501 - a - the scaling value 5502 5503 Output Parameter: 5504 . mat - the scaled matrix 5505 5506 Level: intermediate 5507 5508 .seealso: MatDiagonalScale() 5509 @*/ 5510 PetscErrorCode MatScale(Mat mat,PetscScalar a) 5511 { 5512 PetscErrorCode ierr; 5513 5514 PetscFunctionBegin; 5515 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5516 PetscValidType(mat,1); 5517 PetscCheckFalse(a != (PetscScalar)1.0 && !mat->ops->scale,PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 5518 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5519 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5520 PetscValidLogicalCollectiveScalar(mat,a,2); 5521 MatCheckPreallocated(mat,1); 5522 5523 ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 5524 if (a != (PetscScalar)1.0) { 5525 ierr = (*mat->ops->scale)(mat,a);CHKERRQ(ierr); 5526 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 5527 } 5528 ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 5529 PetscFunctionReturn(0); 5530 } 5531 5532 /*@ 5533 MatNorm - Calculates various norms of a matrix. 5534 5535 Collective on Mat 5536 5537 Input Parameters: 5538 + mat - the matrix 5539 - type - the type of norm, NORM_1, NORM_FROBENIUS, NORM_INFINITY 5540 5541 Output Parameter: 5542 . nrm - the resulting norm 5543 5544 Level: intermediate 5545 5546 @*/ 5547 PetscErrorCode MatNorm(Mat mat,NormType type,PetscReal *nrm) 5548 { 5549 PetscErrorCode ierr; 5550 5551 PetscFunctionBegin; 5552 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5553 PetscValidType(mat,1); 5554 PetscValidRealPointer(nrm,3); 5555 5556 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5557 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5558 PetscCheckFalse(!mat->ops->norm,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 5559 MatCheckPreallocated(mat,1); 5560 5561 ierr = (*mat->ops->norm)(mat,type,nrm);CHKERRQ(ierr); 5562 PetscFunctionReturn(0); 5563 } 5564 5565 /* 5566 This variable is used to prevent counting of MatAssemblyBegin() that 5567 are called from within a MatAssemblyEnd(). 5568 */ 5569 static PetscInt MatAssemblyEnd_InUse = 0; 5570 /*@ 5571 MatAssemblyBegin - Begins assembling the matrix. This routine should 5572 be called after completing all calls to MatSetValues(). 5573 5574 Collective on Mat 5575 5576 Input Parameters: 5577 + mat - the matrix 5578 - type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY 5579 5580 Notes: 5581 MatSetValues() generally caches the values. The matrix is ready to 5582 use only after MatAssemblyBegin() and MatAssemblyEnd() have been called. 5583 Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES 5584 in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before 5585 using the matrix. 5586 5587 ALL processes that share a matrix MUST call MatAssemblyBegin() and MatAssemblyEnd() the SAME NUMBER of times, and each time with the 5588 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 5589 a global collective operation requring all processes that share the matrix. 5590 5591 Space for preallocated nonzeros that is not filled by a call to MatSetValues() or a related routine are compressed 5592 out by assembly. If you intend to use that extra space on a subsequent assembly, be sure to insert explicit zeros 5593 before MAT_FINAL_ASSEMBLY so the space is not compressed out. 5594 5595 Level: beginner 5596 5597 .seealso: MatAssemblyEnd(), MatSetValues(), MatAssembled() 5598 @*/ 5599 PetscErrorCode MatAssemblyBegin(Mat mat,MatAssemblyType type) 5600 { 5601 PetscErrorCode ierr; 5602 5603 PetscFunctionBegin; 5604 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5605 PetscValidType(mat,1); 5606 MatCheckPreallocated(mat,1); 5607 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix.\nDid you forget to call MatSetUnfactored()?"); 5608 if (mat->assembled) { 5609 mat->was_assembled = PETSC_TRUE; 5610 mat->assembled = PETSC_FALSE; 5611 } 5612 5613 if (!MatAssemblyEnd_InUse) { 5614 ierr = PetscLogEventBegin(MAT_AssemblyBegin,mat,0,0,0);CHKERRQ(ierr); 5615 if (mat->ops->assemblybegin) {ierr = (*mat->ops->assemblybegin)(mat,type);CHKERRQ(ierr);} 5616 ierr = PetscLogEventEnd(MAT_AssemblyBegin,mat,0,0,0);CHKERRQ(ierr); 5617 } else if (mat->ops->assemblybegin) { 5618 ierr = (*mat->ops->assemblybegin)(mat,type);CHKERRQ(ierr); 5619 } 5620 PetscFunctionReturn(0); 5621 } 5622 5623 /*@ 5624 MatAssembled - Indicates if a matrix has been assembled and is ready for 5625 use; for example, in matrix-vector product. 5626 5627 Not Collective 5628 5629 Input Parameter: 5630 . mat - the matrix 5631 5632 Output Parameter: 5633 . assembled - PETSC_TRUE or PETSC_FALSE 5634 5635 Level: advanced 5636 5637 .seealso: MatAssemblyEnd(), MatSetValues(), MatAssemblyBegin() 5638 @*/ 5639 PetscErrorCode MatAssembled(Mat mat,PetscBool *assembled) 5640 { 5641 PetscFunctionBegin; 5642 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5643 PetscValidPointer(assembled,2); 5644 *assembled = mat->assembled; 5645 PetscFunctionReturn(0); 5646 } 5647 5648 /*@ 5649 MatAssemblyEnd - Completes assembling the matrix. This routine should 5650 be called after MatAssemblyBegin(). 5651 5652 Collective on Mat 5653 5654 Input Parameters: 5655 + mat - the matrix 5656 - type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY 5657 5658 Options Database Keys: 5659 + -mat_view ::ascii_info - Prints info on matrix at conclusion of MatEndAssembly() 5660 . -mat_view ::ascii_info_detail - Prints more detailed info 5661 . -mat_view - Prints matrix in ASCII format 5662 . -mat_view ::ascii_matlab - Prints matrix in Matlab format 5663 . -mat_view draw - PetscDraws nonzero structure of matrix, using MatView() and PetscDrawOpenX(). 5664 . -display <name> - Sets display name (default is host) 5665 . -draw_pause <sec> - Sets number of seconds to pause after display 5666 . -mat_view socket - Sends matrix to socket, can be accessed from Matlab (See Users-Manual: ch_matlab) 5667 . -viewer_socket_machine <machine> - Machine to use for socket 5668 . -viewer_socket_port <port> - Port number to use for socket 5669 - -mat_view binary:filename[:append] - Save matrix to file in binary format 5670 5671 Notes: 5672 MatSetValues() generally caches the values. The matrix is ready to 5673 use only after MatAssemblyBegin() and MatAssemblyEnd() have been called. 5674 Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES 5675 in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before 5676 using the matrix. 5677 5678 Space for preallocated nonzeros that is not filled by a call to MatSetValues() or a related routine are compressed 5679 out by assembly. If you intend to use that extra space on a subsequent assembly, be sure to insert explicit zeros 5680 before MAT_FINAL_ASSEMBLY so the space is not compressed out. 5681 5682 Level: beginner 5683 5684 .seealso: MatAssemblyBegin(), MatSetValues(), PetscDrawOpenX(), PetscDrawCreate(), MatView(), MatAssembled(), PetscViewerSocketOpen() 5685 @*/ 5686 PetscErrorCode MatAssemblyEnd(Mat mat,MatAssemblyType type) 5687 { 5688 PetscErrorCode ierr; 5689 static PetscInt inassm = 0; 5690 PetscBool flg = PETSC_FALSE; 5691 5692 PetscFunctionBegin; 5693 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5694 PetscValidType(mat,1); 5695 5696 inassm++; 5697 MatAssemblyEnd_InUse++; 5698 if (MatAssemblyEnd_InUse == 1) { /* Do the logging only the first time through */ 5699 ierr = PetscLogEventBegin(MAT_AssemblyEnd,mat,0,0,0);CHKERRQ(ierr); 5700 if (mat->ops->assemblyend) { 5701 ierr = (*mat->ops->assemblyend)(mat,type);CHKERRQ(ierr); 5702 } 5703 ierr = PetscLogEventEnd(MAT_AssemblyEnd,mat,0,0,0);CHKERRQ(ierr); 5704 } else if (mat->ops->assemblyend) { 5705 ierr = (*mat->ops->assemblyend)(mat,type);CHKERRQ(ierr); 5706 } 5707 5708 /* Flush assembly is not a true assembly */ 5709 if (type != MAT_FLUSH_ASSEMBLY) { 5710 mat->num_ass++; 5711 mat->assembled = PETSC_TRUE; 5712 mat->ass_nonzerostate = mat->nonzerostate; 5713 } 5714 5715 mat->insertmode = NOT_SET_VALUES; 5716 MatAssemblyEnd_InUse--; 5717 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 5718 if (!mat->symmetric_eternal) { 5719 mat->symmetric_set = PETSC_FALSE; 5720 mat->hermitian_set = PETSC_FALSE; 5721 mat->structurally_symmetric_set = PETSC_FALSE; 5722 } 5723 if (inassm == 1 && type != MAT_FLUSH_ASSEMBLY) { 5724 ierr = MatViewFromOptions(mat,NULL,"-mat_view");CHKERRQ(ierr); 5725 5726 if (mat->checksymmetryonassembly) { 5727 ierr = MatIsSymmetric(mat,mat->checksymmetrytol,&flg);CHKERRQ(ierr); 5728 if (flg) { 5729 ierr = PetscPrintf(PetscObjectComm((PetscObject)mat),"Matrix is symmetric (tolerance %g)\n",(double)mat->checksymmetrytol);CHKERRQ(ierr); 5730 } else { 5731 ierr = PetscPrintf(PetscObjectComm((PetscObject)mat),"Matrix is not symmetric (tolerance %g)\n",(double)mat->checksymmetrytol);CHKERRQ(ierr); 5732 } 5733 } 5734 if (mat->nullsp && mat->checknullspaceonassembly) { 5735 ierr = MatNullSpaceTest(mat->nullsp,mat,NULL);CHKERRQ(ierr); 5736 } 5737 } 5738 inassm--; 5739 PetscFunctionReturn(0); 5740 } 5741 5742 /*@ 5743 MatSetOption - Sets a parameter option for a matrix. Some options 5744 may be specific to certain storage formats. Some options 5745 determine how values will be inserted (or added). Sorted, 5746 row-oriented input will generally assemble the fastest. The default 5747 is row-oriented. 5748 5749 Logically Collective on Mat for certain operations, such as MAT_SPD, not collective for MAT_ROW_ORIENTED, see MatOption 5750 5751 Input Parameters: 5752 + mat - the matrix 5753 . option - the option, one of those listed below (and possibly others), 5754 - flg - turn the option on (PETSC_TRUE) or off (PETSC_FALSE) 5755 5756 Options Describing Matrix Structure: 5757 + MAT_SPD - symmetric positive definite 5758 . MAT_SYMMETRIC - symmetric in terms of both structure and value 5759 . MAT_HERMITIAN - transpose is the complex conjugation 5760 . MAT_STRUCTURALLY_SYMMETRIC - symmetric nonzero structure 5761 - MAT_SYMMETRY_ETERNAL - if you would like the symmetry/Hermitian flag 5762 you set to be kept with all future use of the matrix 5763 including after MatAssemblyBegin/End() which could 5764 potentially change the symmetry structure, i.e. you 5765 KNOW the matrix will ALWAYS have the property you set. 5766 Note that setting this flag alone implies nothing about whether the matrix is symmetric/Hermitian; 5767 the relevant flags must be set independently. 5768 5769 Options For Use with MatSetValues(): 5770 Insert a logically dense subblock, which can be 5771 . MAT_ROW_ORIENTED - row-oriented (default) 5772 5773 Note these options reflect the data you pass in with MatSetValues(); it has 5774 nothing to do with how the data is stored internally in the matrix 5775 data structure. 5776 5777 When (re)assembling a matrix, we can restrict the input for 5778 efficiency/debugging purposes. These options include 5779 + MAT_NEW_NONZERO_LOCATIONS - additional insertions will be allowed if they generate a new nonzero (slow) 5780 . MAT_FORCE_DIAGONAL_ENTRIES - forces diagonal entries to be allocated 5781 . MAT_IGNORE_OFF_PROC_ENTRIES - drops off-processor entries 5782 . MAT_NEW_NONZERO_LOCATION_ERR - generates an error for new matrix entry 5783 . MAT_USE_HASH_TABLE - uses a hash table to speed up matrix assembly 5784 . MAT_NO_OFF_PROC_ENTRIES - you know each process will only set values for its own rows, will generate an error if 5785 any process sets values for another process. This avoids all reductions in the MatAssembly routines and thus improves 5786 performance for very large process counts. 5787 - MAT_SUBSET_OFF_PROC_ENTRIES - you know that the first assembly after setting this flag will set a superset 5788 of the off-process entries required for all subsequent assemblies. This avoids a rendezvous step in the MatAssembly 5789 functions, instead sending only neighbor messages. 5790 5791 Notes: 5792 Except for MAT_UNUSED_NONZERO_LOCATION_ERR and MAT_ROW_ORIENTED all processes that share the matrix must pass the same value in flg! 5793 5794 Some options are relevant only for particular matrix types and 5795 are thus ignored by others. Other options are not supported by 5796 certain matrix types and will generate an error message if set. 5797 5798 If using a Fortran 77 module to compute a matrix, one may need to 5799 use the column-oriented option (or convert to the row-oriented 5800 format). 5801 5802 MAT_NEW_NONZERO_LOCATIONS set to PETSC_FALSE indicates that any add or insertion 5803 that would generate a new entry in the nonzero structure is instead 5804 ignored. Thus, if memory has not alredy been allocated for this particular 5805 data, then the insertion is ignored. For dense matrices, in which 5806 the entire array is allocated, no entries are ever ignored. 5807 Set after the first MatAssemblyEnd(). If this option is set then the MatAssemblyBegin/End() processes has one less global reduction 5808 5809 MAT_NEW_NONZERO_LOCATION_ERR set to PETSC_TRUE indicates that any add or insertion 5810 that would generate a new entry in the nonzero structure instead produces 5811 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 5812 5813 MAT_NEW_NONZERO_ALLOCATION_ERR set to PETSC_TRUE indicates that any add or insertion 5814 that would generate a new entry that has not been preallocated will 5815 instead produce an error. (Currently supported for AIJ and BAIJ formats 5816 only.) This is a useful flag when debugging matrix memory preallocation. 5817 If this option is set then the MatAssemblyBegin/End() processes has one less global reduction 5818 5819 MAT_IGNORE_OFF_PROC_ENTRIES set to PETSC_TRUE indicates entries destined for 5820 other processors should be dropped, rather than stashed. 5821 This is useful if you know that the "owning" processor is also 5822 always generating the correct matrix entries, so that PETSc need 5823 not transfer duplicate entries generated on another processor. 5824 5825 MAT_USE_HASH_TABLE indicates that a hash table be used to improve the 5826 searches during matrix assembly. When this flag is set, the hash table 5827 is created during the first Matrix Assembly. This hash table is 5828 used the next time through, during MatSetVaules()/MatSetVaulesBlocked() 5829 to improve the searching of indices. MAT_NEW_NONZERO_LOCATIONS flag 5830 should be used with MAT_USE_HASH_TABLE flag. This option is currently 5831 supported by MATMPIBAIJ format only. 5832 5833 MAT_KEEP_NONZERO_PATTERN indicates when MatZeroRows() is called the zeroed entries 5834 are kept in the nonzero structure 5835 5836 MAT_IGNORE_ZERO_ENTRIES - for AIJ/IS matrices this will stop zero values from creating 5837 a zero location in the matrix 5838 5839 MAT_USE_INODES - indicates using inode version of the code - works with AIJ matrix types 5840 5841 MAT_NO_OFF_PROC_ZERO_ROWS - you know each process will only zero its own rows. This avoids all reductions in the 5842 zero row routines and thus improves performance for very large process counts. 5843 5844 MAT_IGNORE_LOWER_TRIANGULAR - For SBAIJ matrices will ignore any insertions you make in the lower triangular 5845 part of the matrix (since they should match the upper triangular part). 5846 5847 MAT_SORTED_FULL - each process provides exactly its local rows; all column indices for a given row are passed in a 5848 single call to MatSetValues(), preallocation is perfect, row oriented, INSERT_VALUES is used. Common 5849 with finite difference schemes with non-periodic boundary conditions. 5850 5851 Level: intermediate 5852 5853 .seealso: MatOption, Mat 5854 5855 @*/ 5856 PetscErrorCode MatSetOption(Mat mat,MatOption op,PetscBool flg) 5857 { 5858 PetscErrorCode ierr; 5859 5860 PetscFunctionBegin; 5861 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5862 if (op > 0) { 5863 PetscValidLogicalCollectiveEnum(mat,op,2); 5864 PetscValidLogicalCollectiveBool(mat,flg,3); 5865 } 5866 5867 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); 5868 5869 switch (op) { 5870 case MAT_FORCE_DIAGONAL_ENTRIES: 5871 mat->force_diagonals = flg; 5872 PetscFunctionReturn(0); 5873 case MAT_NO_OFF_PROC_ENTRIES: 5874 mat->nooffprocentries = flg; 5875 PetscFunctionReturn(0); 5876 case MAT_SUBSET_OFF_PROC_ENTRIES: 5877 mat->assembly_subset = flg; 5878 if (!mat->assembly_subset) { /* See the same logic in VecAssembly wrt VEC_SUBSET_OFF_PROC_ENTRIES */ 5879 #if !defined(PETSC_HAVE_MPIUNI) 5880 ierr = MatStashScatterDestroy_BTS(&mat->stash);CHKERRQ(ierr); 5881 #endif 5882 mat->stash.first_assembly_done = PETSC_FALSE; 5883 } 5884 PetscFunctionReturn(0); 5885 case MAT_NO_OFF_PROC_ZERO_ROWS: 5886 mat->nooffproczerorows = flg; 5887 PetscFunctionReturn(0); 5888 case MAT_SPD: 5889 mat->spd_set = PETSC_TRUE; 5890 mat->spd = flg; 5891 if (flg) { 5892 mat->symmetric = PETSC_TRUE; 5893 mat->structurally_symmetric = PETSC_TRUE; 5894 mat->symmetric_set = PETSC_TRUE; 5895 mat->structurally_symmetric_set = PETSC_TRUE; 5896 } 5897 break; 5898 case MAT_SYMMETRIC: 5899 mat->symmetric = flg; 5900 if (flg) mat->structurally_symmetric = PETSC_TRUE; 5901 mat->symmetric_set = PETSC_TRUE; 5902 mat->structurally_symmetric_set = flg; 5903 #if !defined(PETSC_USE_COMPLEX) 5904 mat->hermitian = flg; 5905 mat->hermitian_set = PETSC_TRUE; 5906 #endif 5907 break; 5908 case MAT_HERMITIAN: 5909 mat->hermitian = flg; 5910 if (flg) mat->structurally_symmetric = PETSC_TRUE; 5911 mat->hermitian_set = PETSC_TRUE; 5912 mat->structurally_symmetric_set = flg; 5913 #if !defined(PETSC_USE_COMPLEX) 5914 mat->symmetric = flg; 5915 mat->symmetric_set = PETSC_TRUE; 5916 #endif 5917 break; 5918 case MAT_STRUCTURALLY_SYMMETRIC: 5919 mat->structurally_symmetric = flg; 5920 mat->structurally_symmetric_set = PETSC_TRUE; 5921 break; 5922 case MAT_SYMMETRY_ETERNAL: 5923 mat->symmetric_eternal = flg; 5924 break; 5925 case MAT_STRUCTURE_ONLY: 5926 mat->structure_only = flg; 5927 break; 5928 case MAT_SORTED_FULL: 5929 mat->sortedfull = flg; 5930 break; 5931 default: 5932 break; 5933 } 5934 if (mat->ops->setoption) { 5935 ierr = (*mat->ops->setoption)(mat,op,flg);CHKERRQ(ierr); 5936 } 5937 PetscFunctionReturn(0); 5938 } 5939 5940 /*@ 5941 MatGetOption - Gets a parameter option that has been set for a matrix. 5942 5943 Logically Collective on Mat for certain operations, such as MAT_SPD, not collective for MAT_ROW_ORIENTED, see MatOption 5944 5945 Input Parameters: 5946 + mat - the matrix 5947 - option - the option, this only responds to certain options, check the code for which ones 5948 5949 Output Parameter: 5950 . flg - turn the option on (PETSC_TRUE) or off (PETSC_FALSE) 5951 5952 Notes: 5953 Can only be called after MatSetSizes() and MatSetType() have been set. 5954 5955 Level: intermediate 5956 5957 .seealso: MatOption, MatSetOption() 5958 5959 @*/ 5960 PetscErrorCode MatGetOption(Mat mat,MatOption op,PetscBool *flg) 5961 { 5962 PetscFunctionBegin; 5963 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5964 PetscValidType(mat,1); 5965 5966 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); 5967 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()"); 5968 5969 switch (op) { 5970 case MAT_NO_OFF_PROC_ENTRIES: 5971 *flg = mat->nooffprocentries; 5972 break; 5973 case MAT_NO_OFF_PROC_ZERO_ROWS: 5974 *flg = mat->nooffproczerorows; 5975 break; 5976 case MAT_SYMMETRIC: 5977 *flg = mat->symmetric; 5978 break; 5979 case MAT_HERMITIAN: 5980 *flg = mat->hermitian; 5981 break; 5982 case MAT_STRUCTURALLY_SYMMETRIC: 5983 *flg = mat->structurally_symmetric; 5984 break; 5985 case MAT_SYMMETRY_ETERNAL: 5986 *flg = mat->symmetric_eternal; 5987 break; 5988 case MAT_SPD: 5989 *flg = mat->spd; 5990 break; 5991 default: 5992 break; 5993 } 5994 PetscFunctionReturn(0); 5995 } 5996 5997 /*@ 5998 MatZeroEntries - Zeros all entries of a matrix. For sparse matrices 5999 this routine retains the old nonzero structure. 6000 6001 Logically Collective on Mat 6002 6003 Input Parameters: 6004 . mat - the matrix 6005 6006 Level: intermediate 6007 6008 Notes: 6009 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. 6010 See the Performance chapter of the users manual for information on preallocating matrices. 6011 6012 .seealso: MatZeroRows() 6013 @*/ 6014 PetscErrorCode MatZeroEntries(Mat mat) 6015 { 6016 PetscErrorCode ierr; 6017 6018 PetscFunctionBegin; 6019 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6020 PetscValidType(mat,1); 6021 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6022 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"); 6023 PetscCheckFalse(!mat->ops->zeroentries,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 6024 MatCheckPreallocated(mat,1); 6025 6026 ierr = PetscLogEventBegin(MAT_ZeroEntries,mat,0,0,0);CHKERRQ(ierr); 6027 ierr = (*mat->ops->zeroentries)(mat);CHKERRQ(ierr); 6028 ierr = PetscLogEventEnd(MAT_ZeroEntries,mat,0,0,0);CHKERRQ(ierr); 6029 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 6030 PetscFunctionReturn(0); 6031 } 6032 6033 /*@ 6034 MatZeroRowsColumns - Zeros all entries (except possibly the main diagonal) 6035 of a set of rows and columns of a matrix. 6036 6037 Collective on Mat 6038 6039 Input Parameters: 6040 + mat - the matrix 6041 . numRows - the number of rows to remove 6042 . rows - the global row indices 6043 . diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry) 6044 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 6045 - b - optional vector of right hand side, that will be adjusted by provided solution 6046 6047 Notes: 6048 This does not change the nonzero structure of the matrix, it merely zeros those entries in the matrix. 6049 6050 The user can set a value in the diagonal entry (or for the AIJ and 6051 row formats can optionally remove the main diagonal entry from the 6052 nonzero structure as well, by passing 0.0 as the final argument). 6053 6054 For the parallel case, all processes that share the matrix (i.e., 6055 those in the communicator used for matrix creation) MUST call this 6056 routine, regardless of whether any rows being zeroed are owned by 6057 them. 6058 6059 Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to 6060 list only rows local to itself). 6061 6062 The option MAT_NO_OFF_PROC_ZERO_ROWS does not apply to this routine. 6063 6064 Level: intermediate 6065 6066 .seealso: MatZeroRowsIS(), MatZeroRows(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 6067 MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil() 6068 @*/ 6069 PetscErrorCode MatZeroRowsColumns(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 6070 { 6071 PetscErrorCode ierr; 6072 6073 PetscFunctionBegin; 6074 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6075 PetscValidType(mat,1); 6076 if (numRows) PetscValidIntPointer(rows,3); 6077 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 6078 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6079 PetscCheckFalse(!mat->ops->zerorowscolumns,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 6080 MatCheckPreallocated(mat,1); 6081 6082 ierr = (*mat->ops->zerorowscolumns)(mat,numRows,rows,diag,x,b);CHKERRQ(ierr); 6083 ierr = MatViewFromOptions(mat,NULL,"-mat_view");CHKERRQ(ierr); 6084 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 6085 PetscFunctionReturn(0); 6086 } 6087 6088 /*@ 6089 MatZeroRowsColumnsIS - Zeros all entries (except possibly the main diagonal) 6090 of a set of rows and columns of a matrix. 6091 6092 Collective on Mat 6093 6094 Input Parameters: 6095 + mat - the matrix 6096 . is - the rows to zero 6097 . diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry) 6098 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 6099 - b - optional vector of right hand side, that will be adjusted by provided solution 6100 6101 Notes: 6102 This does not change the nonzero structure of the matrix, it merely zeros those entries in the matrix. 6103 6104 The user can set a value in the diagonal entry (or for the AIJ and 6105 row formats can optionally remove the main diagonal entry from the 6106 nonzero structure as well, by passing 0.0 as the final argument). 6107 6108 For the parallel case, all processes that share the matrix (i.e., 6109 those in the communicator used for matrix creation) MUST call this 6110 routine, regardless of whether any rows being zeroed are owned by 6111 them. 6112 6113 Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to 6114 list only rows local to itself). 6115 6116 The option MAT_NO_OFF_PROC_ZERO_ROWS does not apply to this routine. 6117 6118 Level: intermediate 6119 6120 .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 6121 MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRows(), MatZeroRowsColumnsStencil() 6122 @*/ 6123 PetscErrorCode MatZeroRowsColumnsIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b) 6124 { 6125 PetscErrorCode ierr; 6126 PetscInt numRows; 6127 const PetscInt *rows; 6128 6129 PetscFunctionBegin; 6130 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6131 PetscValidHeaderSpecific(is,IS_CLASSID,2); 6132 PetscValidType(mat,1); 6133 PetscValidType(is,2); 6134 ierr = ISGetLocalSize(is,&numRows);CHKERRQ(ierr); 6135 ierr = ISGetIndices(is,&rows);CHKERRQ(ierr); 6136 ierr = MatZeroRowsColumns(mat,numRows,rows,diag,x,b);CHKERRQ(ierr); 6137 ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr); 6138 PetscFunctionReturn(0); 6139 } 6140 6141 /*@ 6142 MatZeroRows - Zeros all entries (except possibly the main diagonal) 6143 of a set of rows of a matrix. 6144 6145 Collective on Mat 6146 6147 Input Parameters: 6148 + mat - the matrix 6149 . numRows - the number of rows to remove 6150 . rows - the global row indices 6151 . diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry) 6152 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 6153 - b - optional vector of right hand side, that will be adjusted by provided solution 6154 6155 Notes: 6156 For the AIJ and BAIJ matrix formats this removes the old nonzero structure, 6157 but does not release memory. For the dense and block diagonal 6158 formats this does not alter the nonzero structure. 6159 6160 If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure 6161 of the matrix is not changed (even for AIJ and BAIJ matrices) the values are 6162 merely zeroed. 6163 6164 The user can set a value in the diagonal entry (or for the AIJ and 6165 row formats can optionally remove the main diagonal entry from the 6166 nonzero structure as well, by passing 0.0 as the final argument). 6167 6168 For the parallel case, all processes that share the matrix (i.e., 6169 those in the communicator used for matrix creation) MUST call this 6170 routine, regardless of whether any rows being zeroed are owned by 6171 them. 6172 6173 Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to 6174 list only rows local to itself). 6175 6176 You can call MatSetOption(mat,MAT_NO_OFF_PROC_ZERO_ROWS,PETSC_TRUE) if each process indicates only rows it 6177 owns that are to be zeroed. This saves a global synchronization in the implementation. 6178 6179 Level: intermediate 6180 6181 .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 6182 MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil() 6183 @*/ 6184 PetscErrorCode MatZeroRows(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 6185 { 6186 PetscErrorCode ierr; 6187 6188 PetscFunctionBegin; 6189 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6190 PetscValidType(mat,1); 6191 if (numRows) PetscValidIntPointer(rows,3); 6192 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 6193 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6194 PetscCheckFalse(!mat->ops->zerorows,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 6195 MatCheckPreallocated(mat,1); 6196 6197 ierr = (*mat->ops->zerorows)(mat,numRows,rows,diag,x,b);CHKERRQ(ierr); 6198 ierr = MatViewFromOptions(mat,NULL,"-mat_view");CHKERRQ(ierr); 6199 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 6200 PetscFunctionReturn(0); 6201 } 6202 6203 /*@ 6204 MatZeroRowsIS - Zeros all entries (except possibly the main diagonal) 6205 of a set of rows of a matrix. 6206 6207 Collective on Mat 6208 6209 Input Parameters: 6210 + mat - the matrix 6211 . is - index set of rows to remove (if NULL then no row is removed) 6212 . diag - value put in all diagonals of eliminated rows 6213 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 6214 - b - optional vector of right hand side, that will be adjusted by provided solution 6215 6216 Notes: 6217 For the AIJ and BAIJ matrix formats this removes the old nonzero structure, 6218 but does not release memory. For the dense and block diagonal 6219 formats this does not alter the nonzero structure. 6220 6221 If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure 6222 of the matrix is not changed (even for AIJ and BAIJ matrices) the values are 6223 merely zeroed. 6224 6225 The user can set a value in the diagonal entry (or for the AIJ and 6226 row formats can optionally remove the main diagonal entry from the 6227 nonzero structure as well, by passing 0.0 as the final argument). 6228 6229 For the parallel case, all processes that share the matrix (i.e., 6230 those in the communicator used for matrix creation) MUST call this 6231 routine, regardless of whether any rows being zeroed are owned by 6232 them. 6233 6234 Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to 6235 list only rows local to itself). 6236 6237 You can call MatSetOption(mat,MAT_NO_OFF_PROC_ZERO_ROWS,PETSC_TRUE) if each process indicates only rows it 6238 owns that are to be zeroed. This saves a global synchronization in the implementation. 6239 6240 Level: intermediate 6241 6242 .seealso: MatZeroRows(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 6243 MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil() 6244 @*/ 6245 PetscErrorCode MatZeroRowsIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b) 6246 { 6247 PetscInt numRows = 0; 6248 const PetscInt *rows = NULL; 6249 PetscErrorCode ierr; 6250 6251 PetscFunctionBegin; 6252 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6253 PetscValidType(mat,1); 6254 if (is) { 6255 PetscValidHeaderSpecific(is,IS_CLASSID,2); 6256 ierr = ISGetLocalSize(is,&numRows);CHKERRQ(ierr); 6257 ierr = ISGetIndices(is,&rows);CHKERRQ(ierr); 6258 } 6259 ierr = MatZeroRows(mat,numRows,rows,diag,x,b);CHKERRQ(ierr); 6260 if (is) { 6261 ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr); 6262 } 6263 PetscFunctionReturn(0); 6264 } 6265 6266 /*@ 6267 MatZeroRowsStencil - Zeros all entries (except possibly the main diagonal) 6268 of a set of rows of a matrix. These rows must be local to the process. 6269 6270 Collective on Mat 6271 6272 Input Parameters: 6273 + mat - the matrix 6274 . numRows - the number of rows to remove 6275 . rows - the grid coordinates (and component number when dof > 1) for matrix rows 6276 . diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry) 6277 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 6278 - b - optional vector of right hand side, that will be adjusted by provided solution 6279 6280 Notes: 6281 For the AIJ and BAIJ matrix formats this removes the old nonzero structure, 6282 but does not release memory. For the dense and block diagonal 6283 formats this does not alter the nonzero structure. 6284 6285 If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure 6286 of the matrix is not changed (even for AIJ and BAIJ matrices) the values are 6287 merely zeroed. 6288 6289 The user can set a value in the diagonal entry (or for the AIJ and 6290 row formats can optionally remove the main diagonal entry from the 6291 nonzero structure as well, by passing 0.0 as the final argument). 6292 6293 For the parallel case, all processes that share the matrix (i.e., 6294 those in the communicator used for matrix creation) MUST call this 6295 routine, regardless of whether any rows being zeroed are owned by 6296 them. 6297 6298 Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to 6299 list only rows local to itself). 6300 6301 The grid coordinates are across the entire grid, not just the local portion 6302 6303 In Fortran idxm and idxn should be declared as 6304 $ MatStencil idxm(4,m) 6305 and the values inserted using 6306 $ idxm(MatStencil_i,1) = i 6307 $ idxm(MatStencil_j,1) = j 6308 $ idxm(MatStencil_k,1) = k 6309 $ idxm(MatStencil_c,1) = c 6310 etc 6311 6312 For periodic boundary conditions use negative indices for values to the left (below 0; that are to be 6313 obtained by wrapping values from right edge). For values to the right of the last entry using that index plus one 6314 etc to obtain values that obtained by wrapping the values from the left edge. This does not work for anything but the 6315 DM_BOUNDARY_PERIODIC boundary type. 6316 6317 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 6318 a single value per point) you can skip filling those indices. 6319 6320 Level: intermediate 6321 6322 .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsl(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 6323 MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil() 6324 @*/ 6325 PetscErrorCode MatZeroRowsStencil(Mat mat,PetscInt numRows,const MatStencil rows[],PetscScalar diag,Vec x,Vec b) 6326 { 6327 PetscInt dim = mat->stencil.dim; 6328 PetscInt sdim = dim - (1 - (PetscInt) mat->stencil.noc); 6329 PetscInt *dims = mat->stencil.dims+1; 6330 PetscInt *starts = mat->stencil.starts; 6331 PetscInt *dxm = (PetscInt*) rows; 6332 PetscInt *jdxm, i, j, tmp, numNewRows = 0; 6333 PetscErrorCode ierr; 6334 6335 PetscFunctionBegin; 6336 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6337 PetscValidType(mat,1); 6338 if (numRows) PetscValidPointer(rows,3); 6339 6340 ierr = PetscMalloc1(numRows, &jdxm);CHKERRQ(ierr); 6341 for (i = 0; i < numRows; ++i) { 6342 /* Skip unused dimensions (they are ordered k, j, i, c) */ 6343 for (j = 0; j < 3-sdim; ++j) dxm++; 6344 /* Local index in X dir */ 6345 tmp = *dxm++ - starts[0]; 6346 /* Loop over remaining dimensions */ 6347 for (j = 0; j < dim-1; ++j) { 6348 /* If nonlocal, set index to be negative */ 6349 if ((*dxm++ - starts[j+1]) < 0 || tmp < 0) tmp = PETSC_MIN_INT; 6350 /* Update local index */ 6351 else tmp = tmp*dims[j] + *(dxm-1) - starts[j+1]; 6352 } 6353 /* Skip component slot if necessary */ 6354 if (mat->stencil.noc) dxm++; 6355 /* Local row number */ 6356 if (tmp >= 0) { 6357 jdxm[numNewRows++] = tmp; 6358 } 6359 } 6360 ierr = MatZeroRowsLocal(mat,numNewRows,jdxm,diag,x,b);CHKERRQ(ierr); 6361 ierr = PetscFree(jdxm);CHKERRQ(ierr); 6362 PetscFunctionReturn(0); 6363 } 6364 6365 /*@ 6366 MatZeroRowsColumnsStencil - Zeros all row and column entries (except possibly the main diagonal) 6367 of a set of rows and columns of a matrix. 6368 6369 Collective on Mat 6370 6371 Input Parameters: 6372 + mat - the matrix 6373 . numRows - the number of rows/columns to remove 6374 . rows - the grid coordinates (and component number when dof > 1) for matrix rows 6375 . diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry) 6376 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 6377 - b - optional vector of right hand side, that will be adjusted by provided solution 6378 6379 Notes: 6380 For the AIJ and BAIJ matrix formats this removes the old nonzero structure, 6381 but does not release memory. For the dense and block diagonal 6382 formats this does not alter the nonzero structure. 6383 6384 If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure 6385 of the matrix is not changed (even for AIJ and BAIJ matrices) the values are 6386 merely zeroed. 6387 6388 The user can set a value in the diagonal entry (or for the AIJ and 6389 row formats can optionally remove the main diagonal entry from the 6390 nonzero structure as well, by passing 0.0 as the final argument). 6391 6392 For the parallel case, all processes that share the matrix (i.e., 6393 those in the communicator used for matrix creation) MUST call this 6394 routine, regardless of whether any rows being zeroed are owned by 6395 them. 6396 6397 Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to 6398 list only rows local to itself, but the row/column numbers are given in local numbering). 6399 6400 The grid coordinates are across the entire grid, not just the local portion 6401 6402 In Fortran idxm and idxn should be declared as 6403 $ MatStencil idxm(4,m) 6404 and the values inserted using 6405 $ idxm(MatStencil_i,1) = i 6406 $ idxm(MatStencil_j,1) = j 6407 $ idxm(MatStencil_k,1) = k 6408 $ idxm(MatStencil_c,1) = c 6409 etc 6410 6411 For periodic boundary conditions use negative indices for values to the left (below 0; that are to be 6412 obtained by wrapping values from right edge). For values to the right of the last entry using that index plus one 6413 etc to obtain values that obtained by wrapping the values from the left edge. This does not work for anything but the 6414 DM_BOUNDARY_PERIODIC boundary type. 6415 6416 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 6417 a single value per point) you can skip filling those indices. 6418 6419 Level: intermediate 6420 6421 .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 6422 MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRows() 6423 @*/ 6424 PetscErrorCode MatZeroRowsColumnsStencil(Mat mat,PetscInt numRows,const MatStencil rows[],PetscScalar diag,Vec x,Vec b) 6425 { 6426 PetscInt dim = mat->stencil.dim; 6427 PetscInt sdim = dim - (1 - (PetscInt) mat->stencil.noc); 6428 PetscInt *dims = mat->stencil.dims+1; 6429 PetscInt *starts = mat->stencil.starts; 6430 PetscInt *dxm = (PetscInt*) rows; 6431 PetscInt *jdxm, i, j, tmp, numNewRows = 0; 6432 PetscErrorCode ierr; 6433 6434 PetscFunctionBegin; 6435 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6436 PetscValidType(mat,1); 6437 if (numRows) PetscValidPointer(rows,3); 6438 6439 ierr = PetscMalloc1(numRows, &jdxm);CHKERRQ(ierr); 6440 for (i = 0; i < numRows; ++i) { 6441 /* Skip unused dimensions (they are ordered k, j, i, c) */ 6442 for (j = 0; j < 3-sdim; ++j) dxm++; 6443 /* Local index in X dir */ 6444 tmp = *dxm++ - starts[0]; 6445 /* Loop over remaining dimensions */ 6446 for (j = 0; j < dim-1; ++j) { 6447 /* If nonlocal, set index to be negative */ 6448 if ((*dxm++ - starts[j+1]) < 0 || tmp < 0) tmp = PETSC_MIN_INT; 6449 /* Update local index */ 6450 else tmp = tmp*dims[j] + *(dxm-1) - starts[j+1]; 6451 } 6452 /* Skip component slot if necessary */ 6453 if (mat->stencil.noc) dxm++; 6454 /* Local row number */ 6455 if (tmp >= 0) { 6456 jdxm[numNewRows++] = tmp; 6457 } 6458 } 6459 ierr = MatZeroRowsColumnsLocal(mat,numNewRows,jdxm,diag,x,b);CHKERRQ(ierr); 6460 ierr = PetscFree(jdxm);CHKERRQ(ierr); 6461 PetscFunctionReturn(0); 6462 } 6463 6464 /*@C 6465 MatZeroRowsLocal - Zeros all entries (except possibly the main diagonal) 6466 of a set of rows of a matrix; using local numbering of rows. 6467 6468 Collective on Mat 6469 6470 Input Parameters: 6471 + mat - the matrix 6472 . numRows - the number of rows to remove 6473 . rows - the local row indices 6474 . diag - value put in all diagonals of eliminated rows 6475 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 6476 - b - optional vector of right hand side, that will be adjusted by provided solution 6477 6478 Notes: 6479 Before calling MatZeroRowsLocal(), the user must first set the 6480 local-to-global mapping by calling MatSetLocalToGlobalMapping(). 6481 6482 For the AIJ matrix formats this removes the old nonzero structure, 6483 but does not release memory. For the dense and block diagonal 6484 formats this does not alter the nonzero structure. 6485 6486 If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure 6487 of the matrix is not changed (even for AIJ and BAIJ matrices) the values are 6488 merely zeroed. 6489 6490 The user can set a value in the diagonal entry (or for the AIJ and 6491 row formats can optionally remove the main diagonal entry from the 6492 nonzero structure as well, by passing 0.0 as the final argument). 6493 6494 You can call MatSetOption(mat,MAT_NO_OFF_PROC_ZERO_ROWS,PETSC_TRUE) if each process indicates only rows it 6495 owns that are to be zeroed. This saves a global synchronization in the implementation. 6496 6497 Level: intermediate 6498 6499 .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRows(), MatSetOption(), 6500 MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil() 6501 @*/ 6502 PetscErrorCode MatZeroRowsLocal(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 6503 { 6504 PetscErrorCode ierr; 6505 6506 PetscFunctionBegin; 6507 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6508 PetscValidType(mat,1); 6509 if (numRows) PetscValidIntPointer(rows,3); 6510 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 6511 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6512 MatCheckPreallocated(mat,1); 6513 6514 if (mat->ops->zerorowslocal) { 6515 ierr = (*mat->ops->zerorowslocal)(mat,numRows,rows,diag,x,b);CHKERRQ(ierr); 6516 } else { 6517 IS is, newis; 6518 const PetscInt *newRows; 6519 6520 PetscCheckFalse(!mat->rmap->mapping,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Need to provide local to global mapping to matrix first"); 6521 ierr = ISCreateGeneral(PETSC_COMM_SELF,numRows,rows,PETSC_COPY_VALUES,&is);CHKERRQ(ierr); 6522 ierr = ISLocalToGlobalMappingApplyIS(mat->rmap->mapping,is,&newis);CHKERRQ(ierr); 6523 ierr = ISGetIndices(newis,&newRows);CHKERRQ(ierr); 6524 ierr = (*mat->ops->zerorows)(mat,numRows,newRows,diag,x,b);CHKERRQ(ierr); 6525 ierr = ISRestoreIndices(newis,&newRows);CHKERRQ(ierr); 6526 ierr = ISDestroy(&newis);CHKERRQ(ierr); 6527 ierr = ISDestroy(&is);CHKERRQ(ierr); 6528 } 6529 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 6530 PetscFunctionReturn(0); 6531 } 6532 6533 /*@ 6534 MatZeroRowsLocalIS - Zeros all entries (except possibly the main diagonal) 6535 of a set of rows of a matrix; using local numbering of rows. 6536 6537 Collective on Mat 6538 6539 Input Parameters: 6540 + mat - the matrix 6541 . is - index set of rows to remove 6542 . diag - value put in all diagonals of eliminated rows 6543 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 6544 - b - optional vector of right hand side, that will be adjusted by provided solution 6545 6546 Notes: 6547 Before calling MatZeroRowsLocalIS(), the user must first set the 6548 local-to-global mapping by calling MatSetLocalToGlobalMapping(). 6549 6550 For the AIJ matrix formats this removes the old nonzero structure, 6551 but does not release memory. For the dense and block diagonal 6552 formats this does not alter the nonzero structure. 6553 6554 If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure 6555 of the matrix is not changed (even for AIJ and BAIJ matrices) the values are 6556 merely zeroed. 6557 6558 The user can set a value in the diagonal entry (or for the AIJ and 6559 row formats can optionally remove the main diagonal entry from the 6560 nonzero structure as well, by passing 0.0 as the final argument). 6561 6562 You can call MatSetOption(mat,MAT_NO_OFF_PROC_ZERO_ROWS,PETSC_TRUE) if each process indicates only rows it 6563 owns that are to be zeroed. This saves a global synchronization in the implementation. 6564 6565 Level: intermediate 6566 6567 .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRows(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 6568 MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil() 6569 @*/ 6570 PetscErrorCode MatZeroRowsLocalIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b) 6571 { 6572 PetscErrorCode ierr; 6573 PetscInt numRows; 6574 const PetscInt *rows; 6575 6576 PetscFunctionBegin; 6577 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6578 PetscValidType(mat,1); 6579 PetscValidHeaderSpecific(is,IS_CLASSID,2); 6580 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 6581 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6582 MatCheckPreallocated(mat,1); 6583 6584 ierr = ISGetLocalSize(is,&numRows);CHKERRQ(ierr); 6585 ierr = ISGetIndices(is,&rows);CHKERRQ(ierr); 6586 ierr = MatZeroRowsLocal(mat,numRows,rows,diag,x,b);CHKERRQ(ierr); 6587 ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr); 6588 PetscFunctionReturn(0); 6589 } 6590 6591 /*@ 6592 MatZeroRowsColumnsLocal - Zeros all entries (except possibly the main diagonal) 6593 of a set of rows and columns of a matrix; using local numbering of rows. 6594 6595 Collective on Mat 6596 6597 Input Parameters: 6598 + mat - the matrix 6599 . numRows - the number of rows to remove 6600 . rows - the global row indices 6601 . diag - value put in all diagonals of eliminated rows 6602 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 6603 - b - optional vector of right hand side, that will be adjusted by provided solution 6604 6605 Notes: 6606 Before calling MatZeroRowsColumnsLocal(), the user must first set the 6607 local-to-global mapping by calling MatSetLocalToGlobalMapping(). 6608 6609 The user can set a value in the diagonal entry (or for the AIJ and 6610 row formats can optionally remove the main diagonal entry from the 6611 nonzero structure as well, by passing 0.0 as the final argument). 6612 6613 Level: intermediate 6614 6615 .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 6616 MatZeroRows(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil() 6617 @*/ 6618 PetscErrorCode MatZeroRowsColumnsLocal(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 6619 { 6620 PetscErrorCode ierr; 6621 IS is, newis; 6622 const PetscInt *newRows; 6623 6624 PetscFunctionBegin; 6625 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6626 PetscValidType(mat,1); 6627 if (numRows) PetscValidIntPointer(rows,3); 6628 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 6629 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6630 MatCheckPreallocated(mat,1); 6631 6632 PetscCheckFalse(!mat->cmap->mapping,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Need to provide local to global mapping to matrix first"); 6633 ierr = ISCreateGeneral(PETSC_COMM_SELF,numRows,rows,PETSC_COPY_VALUES,&is);CHKERRQ(ierr); 6634 ierr = ISLocalToGlobalMappingApplyIS(mat->cmap->mapping,is,&newis);CHKERRQ(ierr); 6635 ierr = ISGetIndices(newis,&newRows);CHKERRQ(ierr); 6636 ierr = (*mat->ops->zerorowscolumns)(mat,numRows,newRows,diag,x,b);CHKERRQ(ierr); 6637 ierr = ISRestoreIndices(newis,&newRows);CHKERRQ(ierr); 6638 ierr = ISDestroy(&newis);CHKERRQ(ierr); 6639 ierr = ISDestroy(&is);CHKERRQ(ierr); 6640 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 6641 PetscFunctionReturn(0); 6642 } 6643 6644 /*@ 6645 MatZeroRowsColumnsLocalIS - Zeros all entries (except possibly the main diagonal) 6646 of a set of rows and columns of a matrix; using local numbering of rows. 6647 6648 Collective on Mat 6649 6650 Input Parameters: 6651 + mat - the matrix 6652 . is - index set of rows to remove 6653 . diag - value put in all diagonals of eliminated rows 6654 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 6655 - b - optional vector of right hand side, that will be adjusted by provided solution 6656 6657 Notes: 6658 Before calling MatZeroRowsColumnsLocalIS(), the user must first set the 6659 local-to-global mapping by calling MatSetLocalToGlobalMapping(). 6660 6661 The user can set a value in the diagonal entry (or for the AIJ and 6662 row formats can optionally remove the main diagonal entry from the 6663 nonzero structure as well, by passing 0.0 as the final argument). 6664 6665 Level: intermediate 6666 6667 .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 6668 MatZeroRowsColumnsLocal(), MatZeroRows(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil() 6669 @*/ 6670 PetscErrorCode MatZeroRowsColumnsLocalIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b) 6671 { 6672 PetscErrorCode ierr; 6673 PetscInt numRows; 6674 const PetscInt *rows; 6675 6676 PetscFunctionBegin; 6677 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6678 PetscValidType(mat,1); 6679 PetscValidHeaderSpecific(is,IS_CLASSID,2); 6680 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 6681 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6682 MatCheckPreallocated(mat,1); 6683 6684 ierr = ISGetLocalSize(is,&numRows);CHKERRQ(ierr); 6685 ierr = ISGetIndices(is,&rows);CHKERRQ(ierr); 6686 ierr = MatZeroRowsColumnsLocal(mat,numRows,rows,diag,x,b);CHKERRQ(ierr); 6687 ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr); 6688 PetscFunctionReturn(0); 6689 } 6690 6691 /*@C 6692 MatGetSize - Returns the numbers of rows and columns in a matrix. 6693 6694 Not Collective 6695 6696 Input Parameter: 6697 . mat - the matrix 6698 6699 Output Parameters: 6700 + m - the number of global rows 6701 - n - the number of global columns 6702 6703 Note: both output parameters can be NULL on input. 6704 6705 Level: beginner 6706 6707 .seealso: MatGetLocalSize() 6708 @*/ 6709 PetscErrorCode MatGetSize(Mat mat,PetscInt *m,PetscInt *n) 6710 { 6711 PetscFunctionBegin; 6712 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6713 if (m) *m = mat->rmap->N; 6714 if (n) *n = mat->cmap->N; 6715 PetscFunctionReturn(0); 6716 } 6717 6718 /*@C 6719 MatGetLocalSize - Returns the number of local rows and local columns 6720 of a matrix, that is the local size of the left and right vectors as returned by MatCreateVecs(). 6721 6722 Not Collective 6723 6724 Input Parameter: 6725 . mat - the matrix 6726 6727 Output Parameters: 6728 + m - the number of local rows 6729 - n - the number of local columns 6730 6731 Note: both output parameters can be NULL on input. 6732 6733 Level: beginner 6734 6735 .seealso: MatGetSize() 6736 @*/ 6737 PetscErrorCode MatGetLocalSize(Mat mat,PetscInt *m,PetscInt *n) 6738 { 6739 PetscFunctionBegin; 6740 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6741 if (m) PetscValidIntPointer(m,2); 6742 if (n) PetscValidIntPointer(n,3); 6743 if (m) *m = mat->rmap->n; 6744 if (n) *n = mat->cmap->n; 6745 PetscFunctionReturn(0); 6746 } 6747 6748 /*@C 6749 MatGetOwnershipRangeColumn - Returns the range of matrix columns associated with rows of a vector one multiplies by that owned by 6750 this processor. (The columns of the "diagonal block") 6751 6752 Not Collective, unless matrix has not been allocated, then collective on Mat 6753 6754 Input Parameter: 6755 . mat - the matrix 6756 6757 Output Parameters: 6758 + m - the global index of the first local column 6759 - n - one more than the global index of the last local column 6760 6761 Notes: 6762 both output parameters can be NULL on input. 6763 6764 Level: developer 6765 6766 .seealso: MatGetOwnershipRange(), MatGetOwnershipRanges(), MatGetOwnershipRangesColumn() 6767 6768 @*/ 6769 PetscErrorCode MatGetOwnershipRangeColumn(Mat mat,PetscInt *m,PetscInt *n) 6770 { 6771 PetscFunctionBegin; 6772 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6773 PetscValidType(mat,1); 6774 if (m) PetscValidIntPointer(m,2); 6775 if (n) PetscValidIntPointer(n,3); 6776 MatCheckPreallocated(mat,1); 6777 if (m) *m = mat->cmap->rstart; 6778 if (n) *n = mat->cmap->rend; 6779 PetscFunctionReturn(0); 6780 } 6781 6782 /*@C 6783 MatGetOwnershipRange - Returns the range of matrix rows owned by 6784 this processor, assuming that the matrix is laid out with the first 6785 n1 rows on the first processor, the next n2 rows on the second, etc. 6786 For certain parallel layouts this range may not be well defined. 6787 6788 Not Collective 6789 6790 Input Parameter: 6791 . mat - the matrix 6792 6793 Output Parameters: 6794 + m - the global index of the first local row 6795 - n - one more than the global index of the last local row 6796 6797 Note: Both output parameters can be NULL on input. 6798 $ This function requires that the matrix be preallocated. If you have not preallocated, consider using 6799 $ PetscSplitOwnership(MPI_Comm comm, PetscInt *n, PetscInt *N) 6800 $ and then MPI_Scan() to calculate prefix sums of the local sizes. 6801 6802 Level: beginner 6803 6804 .seealso: MatGetOwnershipRanges(), MatGetOwnershipRangeColumn(), MatGetOwnershipRangesColumn(), PetscSplitOwnership(), PetscSplitOwnershipBlock() 6805 6806 @*/ 6807 PetscErrorCode MatGetOwnershipRange(Mat mat,PetscInt *m,PetscInt *n) 6808 { 6809 PetscFunctionBegin; 6810 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6811 PetscValidType(mat,1); 6812 if (m) PetscValidIntPointer(m,2); 6813 if (n) PetscValidIntPointer(n,3); 6814 MatCheckPreallocated(mat,1); 6815 if (m) *m = mat->rmap->rstart; 6816 if (n) *n = mat->rmap->rend; 6817 PetscFunctionReturn(0); 6818 } 6819 6820 /*@C 6821 MatGetOwnershipRanges - Returns the range of matrix rows owned by 6822 each process 6823 6824 Not Collective, unless matrix has not been allocated, then collective on Mat 6825 6826 Input Parameters: 6827 . mat - the matrix 6828 6829 Output Parameters: 6830 . ranges - start of each processors portion plus one more than the total length at the end 6831 6832 Level: beginner 6833 6834 .seealso: MatGetOwnershipRange(), MatGetOwnershipRangeColumn(), MatGetOwnershipRangesColumn() 6835 6836 @*/ 6837 PetscErrorCode MatGetOwnershipRanges(Mat mat,const PetscInt **ranges) 6838 { 6839 PetscErrorCode ierr; 6840 6841 PetscFunctionBegin; 6842 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6843 PetscValidType(mat,1); 6844 MatCheckPreallocated(mat,1); 6845 ierr = PetscLayoutGetRanges(mat->rmap,ranges);CHKERRQ(ierr); 6846 PetscFunctionReturn(0); 6847 } 6848 6849 /*@C 6850 MatGetOwnershipRangesColumn - Returns the range of matrix columns associated with rows of a vector one multiplies by that owned by 6851 this processor. (The columns of the "diagonal blocks" for each process) 6852 6853 Not Collective, unless matrix has not been allocated, then collective on Mat 6854 6855 Input Parameters: 6856 . mat - the matrix 6857 6858 Output Parameters: 6859 . ranges - start of each processors portion plus one more then the total length at the end 6860 6861 Level: beginner 6862 6863 .seealso: MatGetOwnershipRange(), MatGetOwnershipRangeColumn(), MatGetOwnershipRanges() 6864 6865 @*/ 6866 PetscErrorCode MatGetOwnershipRangesColumn(Mat mat,const PetscInt **ranges) 6867 { 6868 PetscErrorCode ierr; 6869 6870 PetscFunctionBegin; 6871 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6872 PetscValidType(mat,1); 6873 MatCheckPreallocated(mat,1); 6874 ierr = PetscLayoutGetRanges(mat->cmap,ranges);CHKERRQ(ierr); 6875 PetscFunctionReturn(0); 6876 } 6877 6878 /*@C 6879 MatGetOwnershipIS - Get row and column ownership as index sets 6880 6881 Not Collective 6882 6883 Input Parameter: 6884 . A - matrix 6885 6886 Output Parameters: 6887 + rows - rows in which this process owns elements 6888 - cols - columns in which this process owns elements 6889 6890 Level: intermediate 6891 6892 .seealso: MatGetOwnershipRange(), MatGetOwnershipRangeColumn(), MatSetValues(), MATELEMENTAL, MATSCALAPACK 6893 @*/ 6894 PetscErrorCode MatGetOwnershipIS(Mat A,IS *rows,IS *cols) 6895 { 6896 PetscErrorCode ierr,(*f)(Mat,IS*,IS*); 6897 6898 PetscFunctionBegin; 6899 MatCheckPreallocated(A,1); 6900 ierr = PetscObjectQueryFunction((PetscObject)A,"MatGetOwnershipIS_C",&f);CHKERRQ(ierr); 6901 if (f) { 6902 ierr = (*f)(A,rows,cols);CHKERRQ(ierr); 6903 } else { /* Create a standard row-based partition, each process is responsible for ALL columns in their row block */ 6904 if (rows) {ierr = ISCreateStride(PETSC_COMM_SELF,A->rmap->n,A->rmap->rstart,1,rows);CHKERRQ(ierr);} 6905 if (cols) {ierr = ISCreateStride(PETSC_COMM_SELF,A->cmap->N,0,1,cols);CHKERRQ(ierr);} 6906 } 6907 PetscFunctionReturn(0); 6908 } 6909 6910 /*@C 6911 MatILUFactorSymbolic - Performs symbolic ILU factorization of a matrix. 6912 Uses levels of fill only, not drop tolerance. Use MatLUFactorNumeric() 6913 to complete the factorization. 6914 6915 Collective on Mat 6916 6917 Input Parameters: 6918 + mat - the matrix 6919 . row - row permutation 6920 . column - column permutation 6921 - info - structure containing 6922 $ levels - number of levels of fill. 6923 $ expected fill - as ratio of original fill. 6924 $ 1 or 0 - indicating force fill on diagonal (improves robustness for matrices 6925 missing diagonal entries) 6926 6927 Output Parameters: 6928 . fact - new matrix that has been symbolically factored 6929 6930 Notes: 6931 See Users-Manual: ch_mat for additional information about choosing the fill factor for better efficiency. 6932 6933 Most users should employ the simplified KSP interface for linear solvers 6934 instead of working directly with matrix algebra routines such as this. 6935 See, e.g., KSPCreate(). 6936 6937 Level: developer 6938 6939 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor() 6940 MatGetOrdering(), MatFactorInfo 6941 6942 Note: this uses the definition of level of fill as in Y. Saad, 2003 6943 6944 Developer Note: fortran interface is not autogenerated as the f90 6945 interface definition cannot be generated correctly [due to MatFactorInfo] 6946 6947 References: 6948 Y. Saad, Iterative methods for sparse linear systems Philadelphia: Society for Industrial and Applied Mathematics, 2003 6949 @*/ 6950 PetscErrorCode MatILUFactorSymbolic(Mat fact,Mat mat,IS row,IS col,const MatFactorInfo *info) 6951 { 6952 PetscErrorCode ierr; 6953 6954 PetscFunctionBegin; 6955 PetscValidHeaderSpecific(mat,MAT_CLASSID,2); 6956 PetscValidType(mat,2); 6957 if (row) PetscValidHeaderSpecific(row,IS_CLASSID,3); 6958 if (col) PetscValidHeaderSpecific(col,IS_CLASSID,4); 6959 PetscValidPointer(info,5); 6960 PetscValidPointer(fact,1); 6961 PetscCheckFalse(info->levels < 0,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Levels of fill negative %" PetscInt_FMT,(PetscInt)info->levels); 6962 PetscCheckFalse(info->fill < 1.0,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %g",(double)info->fill); 6963 if (!fact->ops->ilufactorsymbolic) { 6964 MatSolverType stype; 6965 ierr = MatFactorGetSolverType(fact,&stype);CHKERRQ(ierr); 6966 SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s symbolic ILU using solver type %s",((PetscObject)mat)->type_name,stype); 6967 } 6968 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 6969 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6970 MatCheckPreallocated(mat,2); 6971 6972 if (!fact->trivialsymbolic) {ierr = PetscLogEventBegin(MAT_ILUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr);} 6973 ierr = (fact->ops->ilufactorsymbolic)(fact,mat,row,col,info);CHKERRQ(ierr); 6974 if (!fact->trivialsymbolic) {ierr = PetscLogEventEnd(MAT_ILUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr);} 6975 PetscFunctionReturn(0); 6976 } 6977 6978 /*@C 6979 MatICCFactorSymbolic - Performs symbolic incomplete 6980 Cholesky factorization for a symmetric matrix. Use 6981 MatCholeskyFactorNumeric() to complete the factorization. 6982 6983 Collective on Mat 6984 6985 Input Parameters: 6986 + mat - the matrix 6987 . perm - row and column permutation 6988 - info - structure containing 6989 $ levels - number of levels of fill. 6990 $ expected fill - as ratio of original fill. 6991 6992 Output Parameter: 6993 . fact - the factored matrix 6994 6995 Notes: 6996 Most users should employ the KSP interface for linear solvers 6997 instead of working directly with matrix algebra routines such as this. 6998 See, e.g., KSPCreate(). 6999 7000 Level: developer 7001 7002 .seealso: MatCholeskyFactorNumeric(), MatCholeskyFactor(), MatFactorInfo 7003 7004 Note: this uses the definition of level of fill as in Y. Saad, 2003 7005 7006 Developer Note: fortran interface is not autogenerated as the f90 7007 interface definition cannot be generated correctly [due to MatFactorInfo] 7008 7009 References: 7010 Y. Saad, Iterative methods for sparse linear systems Philadelphia: Society for Industrial and Applied Mathematics, 2003 7011 @*/ 7012 PetscErrorCode MatICCFactorSymbolic(Mat fact,Mat mat,IS perm,const MatFactorInfo *info) 7013 { 7014 PetscErrorCode ierr; 7015 7016 PetscFunctionBegin; 7017 PetscValidHeaderSpecific(mat,MAT_CLASSID,2); 7018 PetscValidType(mat,2); 7019 if (perm) PetscValidHeaderSpecific(perm,IS_CLASSID,3); 7020 PetscValidPointer(info,4); 7021 PetscValidPointer(fact,1); 7022 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7023 PetscCheckFalse(info->levels < 0,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Levels negative %" PetscInt_FMT,(PetscInt) info->levels); 7024 PetscCheckFalse(info->fill < 1.0,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %g",(double)info->fill); 7025 if (!(fact)->ops->iccfactorsymbolic) { 7026 MatSolverType stype; 7027 ierr = MatFactorGetSolverType(fact,&stype);CHKERRQ(ierr); 7028 SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s symbolic ICC using solver type %s",((PetscObject)mat)->type_name,stype); 7029 } 7030 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 7031 MatCheckPreallocated(mat,2); 7032 7033 if (!fact->trivialsymbolic) {ierr = PetscLogEventBegin(MAT_ICCFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr);} 7034 ierr = (fact->ops->iccfactorsymbolic)(fact,mat,perm,info);CHKERRQ(ierr); 7035 if (!fact->trivialsymbolic) {ierr = PetscLogEventEnd(MAT_ICCFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr);} 7036 PetscFunctionReturn(0); 7037 } 7038 7039 /*@C 7040 MatCreateSubMatrices - Extracts several submatrices from a matrix. If submat 7041 points to an array of valid matrices, they may be reused to store the new 7042 submatrices. 7043 7044 Collective on Mat 7045 7046 Input Parameters: 7047 + mat - the matrix 7048 . n - the number of submatrixes to be extracted (on this processor, may be zero) 7049 . irow, icol - index sets of rows and columns to extract 7050 - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 7051 7052 Output Parameter: 7053 . submat - the array of submatrices 7054 7055 Notes: 7056 MatCreateSubMatrices() can extract ONLY sequential submatrices 7057 (from both sequential and parallel matrices). Use MatCreateSubMatrix() 7058 to extract a parallel submatrix. 7059 7060 Some matrix types place restrictions on the row and column 7061 indices, such as that they be sorted or that they be equal to each other. 7062 7063 The index sets may not have duplicate entries. 7064 7065 When extracting submatrices from a parallel matrix, each processor can 7066 form a different submatrix by setting the rows and columns of its 7067 individual index sets according to the local submatrix desired. 7068 7069 When finished using the submatrices, the user should destroy 7070 them with MatDestroySubMatrices(). 7071 7072 MAT_REUSE_MATRIX can only be used when the nonzero structure of the 7073 original matrix has not changed from that last call to MatCreateSubMatrices(). 7074 7075 This routine creates the matrices in submat; you should NOT create them before 7076 calling it. It also allocates the array of matrix pointers submat. 7077 7078 For BAIJ matrices the index sets must respect the block structure, that is if they 7079 request one row/column in a block, they must request all rows/columns that are in 7080 that block. For example, if the block size is 2 you cannot request just row 0 and 7081 column 0. 7082 7083 Fortran Note: 7084 The Fortran interface is slightly different from that given below; it 7085 requires one to pass in as submat a Mat (integer) array of size at least n+1. 7086 7087 Level: advanced 7088 7089 .seealso: MatDestroySubMatrices(), MatCreateSubMatrix(), MatGetRow(), MatGetDiagonal(), MatReuse 7090 @*/ 7091 PetscErrorCode MatCreateSubMatrices(Mat mat,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *submat[]) 7092 { 7093 PetscErrorCode ierr; 7094 PetscInt i; 7095 PetscBool eq; 7096 7097 PetscFunctionBegin; 7098 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7099 PetscValidType(mat,1); 7100 if (n) { 7101 PetscValidPointer(irow,3); 7102 PetscValidHeaderSpecific(*irow,IS_CLASSID,3); 7103 PetscValidPointer(icol,4); 7104 PetscValidHeaderSpecific(*icol,IS_CLASSID,4); 7105 } 7106 PetscValidPointer(submat,6); 7107 if (n && scall == MAT_REUSE_MATRIX) { 7108 PetscValidPointer(*submat,6); 7109 PetscValidHeaderSpecific(**submat,MAT_CLASSID,6); 7110 } 7111 PetscCheckFalse(!mat->ops->createsubmatrices,PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 7112 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 7113 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7114 MatCheckPreallocated(mat,1); 7115 7116 ierr = PetscLogEventBegin(MAT_CreateSubMats,mat,0,0,0);CHKERRQ(ierr); 7117 ierr = (*mat->ops->createsubmatrices)(mat,n,irow,icol,scall,submat);CHKERRQ(ierr); 7118 ierr = PetscLogEventEnd(MAT_CreateSubMats,mat,0,0,0);CHKERRQ(ierr); 7119 for (i=0; i<n; i++) { 7120 (*submat)[i]->factortype = MAT_FACTOR_NONE; /* in case in place factorization was previously done on submatrix */ 7121 ierr = ISEqualUnsorted(irow[i],icol[i],&eq);CHKERRQ(ierr); 7122 if (eq) { 7123 ierr = MatPropagateSymmetryOptions(mat,(*submat)[i]);CHKERRQ(ierr); 7124 } 7125 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 7126 if (mat->boundtocpu && mat->bindingpropagates) { 7127 ierr = MatBindToCPU((*submat)[i],PETSC_TRUE);CHKERRQ(ierr); 7128 ierr = MatSetBindingPropagates((*submat)[i],PETSC_TRUE);CHKERRQ(ierr); 7129 } 7130 #endif 7131 } 7132 PetscFunctionReturn(0); 7133 } 7134 7135 /*@C 7136 MatCreateSubMatricesMPI - Extracts MPI submatrices across a sub communicator of mat (by pairs of IS that may live on subcomms). 7137 7138 Collective on Mat 7139 7140 Input Parameters: 7141 + mat - the matrix 7142 . n - the number of submatrixes to be extracted 7143 . irow, icol - index sets of rows and columns to extract 7144 - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 7145 7146 Output Parameter: 7147 . submat - the array of submatrices 7148 7149 Level: advanced 7150 7151 .seealso: MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRow(), MatGetDiagonal(), MatReuse 7152 @*/ 7153 PetscErrorCode MatCreateSubMatricesMPI(Mat mat,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *submat[]) 7154 { 7155 PetscErrorCode ierr; 7156 PetscInt i; 7157 PetscBool eq; 7158 7159 PetscFunctionBegin; 7160 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7161 PetscValidType(mat,1); 7162 if (n) { 7163 PetscValidPointer(irow,3); 7164 PetscValidHeaderSpecific(*irow,IS_CLASSID,3); 7165 PetscValidPointer(icol,4); 7166 PetscValidHeaderSpecific(*icol,IS_CLASSID,4); 7167 } 7168 PetscValidPointer(submat,6); 7169 if (n && scall == MAT_REUSE_MATRIX) { 7170 PetscValidPointer(*submat,6); 7171 PetscValidHeaderSpecific(**submat,MAT_CLASSID,6); 7172 } 7173 PetscCheckFalse(!mat->ops->createsubmatricesmpi,PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 7174 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 7175 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7176 MatCheckPreallocated(mat,1); 7177 7178 ierr = PetscLogEventBegin(MAT_CreateSubMats,mat,0,0,0);CHKERRQ(ierr); 7179 ierr = (*mat->ops->createsubmatricesmpi)(mat,n,irow,icol,scall,submat);CHKERRQ(ierr); 7180 ierr = PetscLogEventEnd(MAT_CreateSubMats,mat,0,0,0);CHKERRQ(ierr); 7181 for (i=0; i<n; i++) { 7182 ierr = ISEqualUnsorted(irow[i],icol[i],&eq);CHKERRQ(ierr); 7183 if (eq) { 7184 ierr = MatPropagateSymmetryOptions(mat,(*submat)[i]);CHKERRQ(ierr); 7185 } 7186 } 7187 PetscFunctionReturn(0); 7188 } 7189 7190 /*@C 7191 MatDestroyMatrices - Destroys an array of matrices. 7192 7193 Collective on Mat 7194 7195 Input Parameters: 7196 + n - the number of local matrices 7197 - mat - the matrices (note that this is a pointer to the array of matrices) 7198 7199 Level: advanced 7200 7201 Notes: 7202 Frees not only the matrices, but also the array that contains the matrices 7203 In Fortran will not free the array. 7204 7205 .seealso: MatCreateSubMatrices() MatDestroySubMatrices() 7206 @*/ 7207 PetscErrorCode MatDestroyMatrices(PetscInt n,Mat *mat[]) 7208 { 7209 PetscErrorCode ierr; 7210 PetscInt i; 7211 7212 PetscFunctionBegin; 7213 if (!*mat) PetscFunctionReturn(0); 7214 PetscCheckFalse(n < 0,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Trying to destroy negative number of matrices %" PetscInt_FMT,n); 7215 PetscValidPointer(mat,2); 7216 7217 for (i=0; i<n; i++) { 7218 ierr = MatDestroy(&(*mat)[i]);CHKERRQ(ierr); 7219 } 7220 7221 /* memory is allocated even if n = 0 */ 7222 ierr = PetscFree(*mat);CHKERRQ(ierr); 7223 PetscFunctionReturn(0); 7224 } 7225 7226 /*@C 7227 MatDestroySubMatrices - Destroys a set of matrices obtained with MatCreateSubMatrices(). 7228 7229 Collective on Mat 7230 7231 Input Parameters: 7232 + n - the number of local matrices 7233 - mat - the matrices (note that this is a pointer to the array of matrices, just to match the calling 7234 sequence of MatCreateSubMatrices()) 7235 7236 Level: advanced 7237 7238 Notes: 7239 Frees not only the matrices, but also the array that contains the matrices 7240 In Fortran will not free the array. 7241 7242 .seealso: MatCreateSubMatrices() 7243 @*/ 7244 PetscErrorCode MatDestroySubMatrices(PetscInt n,Mat *mat[]) 7245 { 7246 PetscErrorCode ierr; 7247 Mat mat0; 7248 7249 PetscFunctionBegin; 7250 if (!*mat) PetscFunctionReturn(0); 7251 /* mat[] is an array of length n+1, see MatCreateSubMatrices_xxx() */ 7252 PetscCheckFalse(n < 0,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Trying to destroy negative number of matrices %" PetscInt_FMT,n); 7253 PetscValidPointer(mat,2); 7254 7255 mat0 = (*mat)[0]; 7256 if (mat0 && mat0->ops->destroysubmatrices) { 7257 ierr = (mat0->ops->destroysubmatrices)(n,mat);CHKERRQ(ierr); 7258 } else { 7259 ierr = MatDestroyMatrices(n,mat);CHKERRQ(ierr); 7260 } 7261 PetscFunctionReturn(0); 7262 } 7263 7264 /*@C 7265 MatGetSeqNonzeroStructure - Extracts the sequential nonzero structure from a matrix. 7266 7267 Collective on Mat 7268 7269 Input Parameters: 7270 . mat - the matrix 7271 7272 Output Parameter: 7273 . matstruct - the sequential matrix with the nonzero structure of mat 7274 7275 Level: intermediate 7276 7277 .seealso: MatDestroySeqNonzeroStructure(), MatCreateSubMatrices(), MatDestroyMatrices() 7278 @*/ 7279 PetscErrorCode MatGetSeqNonzeroStructure(Mat mat,Mat *matstruct) 7280 { 7281 PetscErrorCode ierr; 7282 7283 PetscFunctionBegin; 7284 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7285 PetscValidPointer(matstruct,2); 7286 7287 PetscValidType(mat,1); 7288 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7289 MatCheckPreallocated(mat,1); 7290 7291 PetscCheckFalse(!mat->ops->getseqnonzerostructure,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Not for matrix type %s",((PetscObject)mat)->type_name); 7292 ierr = PetscLogEventBegin(MAT_GetSeqNonzeroStructure,mat,0,0,0);CHKERRQ(ierr); 7293 ierr = (*mat->ops->getseqnonzerostructure)(mat,matstruct);CHKERRQ(ierr); 7294 ierr = PetscLogEventEnd(MAT_GetSeqNonzeroStructure,mat,0,0,0);CHKERRQ(ierr); 7295 PetscFunctionReturn(0); 7296 } 7297 7298 /*@C 7299 MatDestroySeqNonzeroStructure - Destroys matrix obtained with MatGetSeqNonzeroStructure(). 7300 7301 Collective on Mat 7302 7303 Input Parameters: 7304 . mat - the matrix (note that this is a pointer to the array of matrices, just to match the calling 7305 sequence of MatGetSequentialNonzeroStructure()) 7306 7307 Level: advanced 7308 7309 Notes: 7310 Frees not only the matrices, but also the array that contains the matrices 7311 7312 .seealso: MatGetSeqNonzeroStructure() 7313 @*/ 7314 PetscErrorCode MatDestroySeqNonzeroStructure(Mat *mat) 7315 { 7316 PetscErrorCode ierr; 7317 7318 PetscFunctionBegin; 7319 PetscValidPointer(mat,1); 7320 ierr = MatDestroy(mat);CHKERRQ(ierr); 7321 PetscFunctionReturn(0); 7322 } 7323 7324 /*@ 7325 MatIncreaseOverlap - Given a set of submatrices indicated by index sets, 7326 replaces the index sets by larger ones that represent submatrices with 7327 additional overlap. 7328 7329 Collective on Mat 7330 7331 Input Parameters: 7332 + mat - the matrix 7333 . n - the number of index sets 7334 . is - the array of index sets (these index sets will changed during the call) 7335 - ov - the additional overlap requested 7336 7337 Options Database: 7338 . -mat_increase_overlap_scalable - use a scalable algorithm to compute the overlap (supported by MPIAIJ matrix) 7339 7340 Level: developer 7341 7342 Developer Note: 7343 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. 7344 7345 .seealso: MatCreateSubMatrices() 7346 @*/ 7347 PetscErrorCode MatIncreaseOverlap(Mat mat,PetscInt n,IS is[],PetscInt ov) 7348 { 7349 PetscErrorCode ierr; 7350 PetscInt i,bs,cbs; 7351 7352 PetscFunctionBegin; 7353 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7354 PetscValidType(mat,1); 7355 PetscCheckFalse(n < 0,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Must have one or more domains, you have %" PetscInt_FMT,n); 7356 if (n) { 7357 PetscValidPointer(is,3); 7358 PetscValidHeaderSpecific(*is,IS_CLASSID,3); 7359 PetscValidLogicalCollectiveInt(*is,n,2); 7360 } 7361 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 7362 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7363 MatCheckPreallocated(mat,1); 7364 7365 if (!ov) PetscFunctionReturn(0); 7366 PetscCheckFalse(!mat->ops->increaseoverlap,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 7367 ierr = PetscLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr); 7368 ierr = (*mat->ops->increaseoverlap)(mat,n,is,ov);CHKERRQ(ierr); 7369 ierr = PetscLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr); 7370 ierr = MatGetBlockSizes(mat,&bs,&cbs);CHKERRQ(ierr); 7371 if (bs == cbs) { 7372 for (i=0; i<n; i++) { 7373 ierr = ISSetBlockSize(is[i],bs);CHKERRQ(ierr); 7374 } 7375 } 7376 PetscFunctionReturn(0); 7377 } 7378 7379 PetscErrorCode MatIncreaseOverlapSplit_Single(Mat,IS*,PetscInt); 7380 7381 /*@ 7382 MatIncreaseOverlapSplit - Given a set of submatrices indicated by index sets across 7383 a sub communicator, replaces the index sets by larger ones that represent submatrices with 7384 additional overlap. 7385 7386 Collective on Mat 7387 7388 Input Parameters: 7389 + mat - the matrix 7390 . n - the number of index sets 7391 . is - the array of index sets (these index sets will changed during the call) 7392 - ov - the additional overlap requested 7393 7394 Options Database: 7395 . -mat_increase_overlap_scalable - use a scalable algorithm to compute the overlap (supported by MPIAIJ matrix) 7396 7397 Level: developer 7398 7399 .seealso: MatCreateSubMatrices() 7400 @*/ 7401 PetscErrorCode MatIncreaseOverlapSplit(Mat mat,PetscInt n,IS is[],PetscInt ov) 7402 { 7403 PetscInt i; 7404 PetscErrorCode ierr; 7405 7406 PetscFunctionBegin; 7407 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7408 PetscValidType(mat,1); 7409 PetscCheckFalse(n < 0,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Must have one or more domains, you have %" PetscInt_FMT,n); 7410 if (n) { 7411 PetscValidPointer(is,3); 7412 PetscValidHeaderSpecific(*is,IS_CLASSID,3); 7413 } 7414 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 7415 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7416 MatCheckPreallocated(mat,1); 7417 if (!ov) PetscFunctionReturn(0); 7418 ierr = PetscLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr); 7419 for (i=0; i<n; i++) { 7420 ierr = MatIncreaseOverlapSplit_Single(mat,&is[i],ov);CHKERRQ(ierr); 7421 } 7422 ierr = PetscLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr); 7423 PetscFunctionReturn(0); 7424 } 7425 7426 /*@ 7427 MatGetBlockSize - Returns the matrix block size. 7428 7429 Not Collective 7430 7431 Input Parameter: 7432 . mat - the matrix 7433 7434 Output Parameter: 7435 . bs - block size 7436 7437 Notes: 7438 Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ. These formats ALWAYS have square block storage in the matrix. 7439 7440 If the block size has not been set yet this routine returns 1. 7441 7442 Level: intermediate 7443 7444 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSizes() 7445 @*/ 7446 PetscErrorCode MatGetBlockSize(Mat mat,PetscInt *bs) 7447 { 7448 PetscFunctionBegin; 7449 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7450 PetscValidIntPointer(bs,2); 7451 *bs = PetscAbs(mat->rmap->bs); 7452 PetscFunctionReturn(0); 7453 } 7454 7455 /*@ 7456 MatGetBlockSizes - Returns the matrix block row and column sizes. 7457 7458 Not Collective 7459 7460 Input Parameter: 7461 . mat - the matrix 7462 7463 Output Parameters: 7464 + rbs - row block size 7465 - cbs - column block size 7466 7467 Notes: 7468 Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ. These formats ALWAYS have square block storage in the matrix. 7469 If you pass a different block size for the columns than the rows, the row block size determines the square block storage. 7470 7471 If a block size has not been set yet this routine returns 1. 7472 7473 Level: intermediate 7474 7475 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSize(), MatSetBlockSizes() 7476 @*/ 7477 PetscErrorCode MatGetBlockSizes(Mat mat,PetscInt *rbs, PetscInt *cbs) 7478 { 7479 PetscFunctionBegin; 7480 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7481 if (rbs) PetscValidIntPointer(rbs,2); 7482 if (cbs) PetscValidIntPointer(cbs,3); 7483 if (rbs) *rbs = PetscAbs(mat->rmap->bs); 7484 if (cbs) *cbs = PetscAbs(mat->cmap->bs); 7485 PetscFunctionReturn(0); 7486 } 7487 7488 /*@ 7489 MatSetBlockSize - Sets the matrix block size. 7490 7491 Logically Collective on Mat 7492 7493 Input Parameters: 7494 + mat - the matrix 7495 - bs - block size 7496 7497 Notes: 7498 Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ. These formats ALWAYS have square block storage in the matrix. 7499 This must be called before MatSetUp() or MatXXXSetPreallocation() (or will default to 1) and the block size cannot be changed later. 7500 7501 For MATMPIAIJ and MATSEQAIJ matrix formats, this function can be called at a later stage, provided that the specified block size 7502 is compatible with the matrix local sizes. 7503 7504 Level: intermediate 7505 7506 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSizes(), MatGetBlockSizes() 7507 @*/ 7508 PetscErrorCode MatSetBlockSize(Mat mat,PetscInt bs) 7509 { 7510 PetscErrorCode ierr; 7511 7512 PetscFunctionBegin; 7513 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7514 PetscValidLogicalCollectiveInt(mat,bs,2); 7515 ierr = MatSetBlockSizes(mat,bs,bs);CHKERRQ(ierr); 7516 PetscFunctionReturn(0); 7517 } 7518 7519 /*@ 7520 MatSetVariableBlockSizes - Sets diagonal point-blocks of the matrix that need not be of the same size 7521 7522 Logically Collective on Mat 7523 7524 Input Parameters: 7525 + mat - the matrix 7526 . nblocks - the number of blocks on this process 7527 - bsizes - the block sizes 7528 7529 Notes: 7530 Currently used by PCVPBJACOBI for AIJ matrices 7531 7532 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. 7533 7534 Level: intermediate 7535 7536 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSizes(), MatGetBlockSizes(), MatGetVariableBlockSizes(), PCVPBJACOBI 7537 @*/ 7538 PetscErrorCode MatSetVariableBlockSizes(Mat mat,PetscInt nblocks,PetscInt *bsizes) 7539 { 7540 PetscErrorCode ierr; 7541 PetscInt i,ncnt = 0, nlocal; 7542 7543 PetscFunctionBegin; 7544 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7545 PetscCheckFalse(nblocks < 0,PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Number of local blocks must be great than or equal to zero"); 7546 ierr = MatGetLocalSize(mat,&nlocal,NULL);CHKERRQ(ierr); 7547 for (i=0; i<nblocks; i++) ncnt += bsizes[i]; 7548 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); 7549 ierr = PetscFree(mat->bsizes);CHKERRQ(ierr); 7550 mat->nblocks = nblocks; 7551 ierr = PetscMalloc1(nblocks,&mat->bsizes);CHKERRQ(ierr); 7552 ierr = PetscArraycpy(mat->bsizes,bsizes,nblocks);CHKERRQ(ierr); 7553 PetscFunctionReturn(0); 7554 } 7555 7556 /*@C 7557 MatGetVariableBlockSizes - Gets a diagonal blocks of the matrix that need not be of the same size 7558 7559 Logically Collective on Mat 7560 7561 Input Parameter: 7562 . mat - the matrix 7563 7564 Output Parameters: 7565 + nblocks - the number of blocks on this process 7566 - bsizes - the block sizes 7567 7568 Notes: Currently not supported from Fortran 7569 7570 Level: intermediate 7571 7572 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSizes(), MatGetBlockSizes(), MatSetVariableBlockSizes() 7573 @*/ 7574 PetscErrorCode MatGetVariableBlockSizes(Mat mat,PetscInt *nblocks,const PetscInt **bsizes) 7575 { 7576 PetscFunctionBegin; 7577 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7578 *nblocks = mat->nblocks; 7579 *bsizes = mat->bsizes; 7580 PetscFunctionReturn(0); 7581 } 7582 7583 /*@ 7584 MatSetBlockSizes - Sets the matrix block row and column sizes. 7585 7586 Logically Collective on Mat 7587 7588 Input Parameters: 7589 + mat - the matrix 7590 . rbs - row block size 7591 - cbs - column block size 7592 7593 Notes: 7594 Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ. These formats ALWAYS have square block storage in the matrix. 7595 If you pass a different block size for the columns than the rows, the row block size determines the square block storage. 7596 This must be called before MatSetUp() or MatXXXSetPreallocation() (or will default to 1) and the block size cannot be changed later. 7597 7598 For MATMPIAIJ and MATSEQAIJ matrix formats, this function can be called at a later stage, provided that the specified block sizes 7599 are compatible with the matrix local sizes. 7600 7601 The row and column block size determine the blocksize of the "row" and "column" vectors returned by MatCreateVecs(). 7602 7603 Level: intermediate 7604 7605 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSize(), MatGetBlockSizes() 7606 @*/ 7607 PetscErrorCode MatSetBlockSizes(Mat mat,PetscInt rbs,PetscInt cbs) 7608 { 7609 PetscErrorCode ierr; 7610 7611 PetscFunctionBegin; 7612 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7613 PetscValidLogicalCollectiveInt(mat,rbs,2); 7614 PetscValidLogicalCollectiveInt(mat,cbs,3); 7615 if (mat->ops->setblocksizes) { 7616 ierr = (*mat->ops->setblocksizes)(mat,rbs,cbs);CHKERRQ(ierr); 7617 } 7618 if (mat->rmap->refcnt) { 7619 ISLocalToGlobalMapping l2g = NULL; 7620 PetscLayout nmap = NULL; 7621 7622 ierr = PetscLayoutDuplicate(mat->rmap,&nmap);CHKERRQ(ierr); 7623 if (mat->rmap->mapping) { 7624 ierr = ISLocalToGlobalMappingDuplicate(mat->rmap->mapping,&l2g);CHKERRQ(ierr); 7625 } 7626 ierr = PetscLayoutDestroy(&mat->rmap);CHKERRQ(ierr); 7627 mat->rmap = nmap; 7628 mat->rmap->mapping = l2g; 7629 } 7630 if (mat->cmap->refcnt) { 7631 ISLocalToGlobalMapping l2g = NULL; 7632 PetscLayout nmap = NULL; 7633 7634 ierr = PetscLayoutDuplicate(mat->cmap,&nmap);CHKERRQ(ierr); 7635 if (mat->cmap->mapping) { 7636 ierr = ISLocalToGlobalMappingDuplicate(mat->cmap->mapping,&l2g);CHKERRQ(ierr); 7637 } 7638 ierr = PetscLayoutDestroy(&mat->cmap);CHKERRQ(ierr); 7639 mat->cmap = nmap; 7640 mat->cmap->mapping = l2g; 7641 } 7642 ierr = PetscLayoutSetBlockSize(mat->rmap,rbs);CHKERRQ(ierr); 7643 ierr = PetscLayoutSetBlockSize(mat->cmap,cbs);CHKERRQ(ierr); 7644 PetscFunctionReturn(0); 7645 } 7646 7647 /*@ 7648 MatSetBlockSizesFromMats - Sets the matrix block row and column sizes to match a pair of matrices 7649 7650 Logically Collective on Mat 7651 7652 Input Parameters: 7653 + mat - the matrix 7654 . fromRow - matrix from which to copy row block size 7655 - fromCol - matrix from which to copy column block size (can be same as fromRow) 7656 7657 Level: developer 7658 7659 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSizes() 7660 @*/ 7661 PetscErrorCode MatSetBlockSizesFromMats(Mat mat,Mat fromRow,Mat fromCol) 7662 { 7663 PetscErrorCode ierr; 7664 7665 PetscFunctionBegin; 7666 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7667 PetscValidHeaderSpecific(fromRow,MAT_CLASSID,2); 7668 PetscValidHeaderSpecific(fromCol,MAT_CLASSID,3); 7669 if (fromRow->rmap->bs > 0) {ierr = PetscLayoutSetBlockSize(mat->rmap,fromRow->rmap->bs);CHKERRQ(ierr);} 7670 if (fromCol->cmap->bs > 0) {ierr = PetscLayoutSetBlockSize(mat->cmap,fromCol->cmap->bs);CHKERRQ(ierr);} 7671 PetscFunctionReturn(0); 7672 } 7673 7674 /*@ 7675 MatResidual - Default routine to calculate the residual. 7676 7677 Collective on Mat 7678 7679 Input Parameters: 7680 + mat - the matrix 7681 . b - the right-hand-side 7682 - x - the approximate solution 7683 7684 Output Parameter: 7685 . r - location to store the residual 7686 7687 Level: developer 7688 7689 .seealso: PCMGSetResidual() 7690 @*/ 7691 PetscErrorCode MatResidual(Mat mat,Vec b,Vec x,Vec r) 7692 { 7693 PetscErrorCode ierr; 7694 7695 PetscFunctionBegin; 7696 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7697 PetscValidHeaderSpecific(b,VEC_CLASSID,2); 7698 PetscValidHeaderSpecific(x,VEC_CLASSID,3); 7699 PetscValidHeaderSpecific(r,VEC_CLASSID,4); 7700 PetscValidType(mat,1); 7701 MatCheckPreallocated(mat,1); 7702 ierr = PetscLogEventBegin(MAT_Residual,mat,0,0,0);CHKERRQ(ierr); 7703 if (!mat->ops->residual) { 7704 ierr = MatMult(mat,x,r);CHKERRQ(ierr); 7705 ierr = VecAYPX(r,-1.0,b);CHKERRQ(ierr); 7706 } else { 7707 ierr = (*mat->ops->residual)(mat,b,x,r);CHKERRQ(ierr); 7708 } 7709 ierr = PetscLogEventEnd(MAT_Residual,mat,0,0,0);CHKERRQ(ierr); 7710 PetscFunctionReturn(0); 7711 } 7712 7713 /*@C 7714 MatGetRowIJ - Returns the compressed row storage i and j indices for sequential matrices. 7715 7716 Collective on Mat 7717 7718 Input Parameters: 7719 + mat - the matrix 7720 . shift - 0 or 1 indicating we want the indices starting at 0 or 1 7721 . symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be symmetrized 7722 - inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the 7723 inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is 7724 always used. 7725 7726 Output Parameters: 7727 + n - number of rows in the (possibly compressed) matrix 7728 . ia - the row pointers; that is ia[0] = 0, ia[row] = ia[row-1] + number of elements in that row of the matrix 7729 . ja - the column indices 7730 - done - indicates if the routine actually worked and returned appropriate ia[] and ja[] arrays; callers 7731 are responsible for handling the case when done == PETSC_FALSE and ia and ja are not set 7732 7733 Level: developer 7734 7735 Notes: 7736 You CANNOT change any of the ia[] or ja[] values. 7737 7738 Use MatRestoreRowIJ() when you are finished accessing the ia[] and ja[] values. 7739 7740 Fortran Notes: 7741 In Fortran use 7742 $ 7743 $ PetscInt ia(1), ja(1) 7744 $ PetscOffset iia, jja 7745 $ call MatGetRowIJ(mat,shift,symmetric,inodecompressed,n,ia,iia,ja,jja,done,ierr) 7746 $ ! Access the ith and jth entries via ia(iia + i) and ja(jja + j) 7747 7748 or 7749 $ 7750 $ PetscInt, pointer :: ia(:),ja(:) 7751 $ call MatGetRowIJF90(mat,shift,symmetric,inodecompressed,n,ia,ja,done,ierr) 7752 $ ! Access the ith and jth entries via ia(i) and ja(j) 7753 7754 .seealso: MatGetColumnIJ(), MatRestoreRowIJ(), MatSeqAIJGetArray() 7755 @*/ 7756 PetscErrorCode MatGetRowIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool *done) 7757 { 7758 PetscErrorCode ierr; 7759 7760 PetscFunctionBegin; 7761 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7762 PetscValidType(mat,1); 7763 PetscValidIntPointer(n,5); 7764 if (ia) PetscValidIntPointer(ia,6); 7765 if (ja) PetscValidIntPointer(ja,7); 7766 PetscValidBoolPointer(done,8); 7767 MatCheckPreallocated(mat,1); 7768 if (!mat->ops->getrowij) *done = PETSC_FALSE; 7769 else { 7770 *done = PETSC_TRUE; 7771 ierr = PetscLogEventBegin(MAT_GetRowIJ,mat,0,0,0);CHKERRQ(ierr); 7772 ierr = (*mat->ops->getrowij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr); 7773 ierr = PetscLogEventEnd(MAT_GetRowIJ,mat,0,0,0);CHKERRQ(ierr); 7774 } 7775 PetscFunctionReturn(0); 7776 } 7777 7778 /*@C 7779 MatGetColumnIJ - Returns the compressed column storage i and j indices for sequential matrices. 7780 7781 Collective on Mat 7782 7783 Input Parameters: 7784 + mat - the matrix 7785 . shift - 1 or zero indicating we want the indices starting at 0 or 1 7786 . symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 7787 symmetrized 7788 . inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the 7789 inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is 7790 always used. 7791 . n - number of columns in the (possibly compressed) matrix 7792 . ia - the column pointers; that is ia[0] = 0, ia[col] = i[col-1] + number of elements in that col of the matrix 7793 - ja - the row indices 7794 7795 Output Parameters: 7796 . done - PETSC_TRUE or PETSC_FALSE, indicating whether the values have been returned 7797 7798 Level: developer 7799 7800 .seealso: MatGetRowIJ(), MatRestoreColumnIJ() 7801 @*/ 7802 PetscErrorCode MatGetColumnIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool *done) 7803 { 7804 PetscErrorCode ierr; 7805 7806 PetscFunctionBegin; 7807 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7808 PetscValidType(mat,1); 7809 PetscValidIntPointer(n,5); 7810 if (ia) PetscValidIntPointer(ia,6); 7811 if (ja) PetscValidIntPointer(ja,7); 7812 PetscValidBoolPointer(done,8); 7813 MatCheckPreallocated(mat,1); 7814 if (!mat->ops->getcolumnij) *done = PETSC_FALSE; 7815 else { 7816 *done = PETSC_TRUE; 7817 ierr = (*mat->ops->getcolumnij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr); 7818 } 7819 PetscFunctionReturn(0); 7820 } 7821 7822 /*@C 7823 MatRestoreRowIJ - Call after you are completed with the ia,ja indices obtained with 7824 MatGetRowIJ(). 7825 7826 Collective on Mat 7827 7828 Input Parameters: 7829 + mat - the matrix 7830 . shift - 1 or zero indicating we want the indices starting at 0 or 1 7831 . symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 7832 symmetrized 7833 . inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the 7834 inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is 7835 always used. 7836 . n - size of (possibly compressed) matrix 7837 . ia - the row pointers 7838 - ja - the column indices 7839 7840 Output Parameters: 7841 . done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned 7842 7843 Note: 7844 This routine zeros out n, ia, and ja. This is to prevent accidental 7845 us of the array after it has been restored. If you pass NULL, it will 7846 not zero the pointers. Use of ia or ja after MatRestoreRowIJ() is invalid. 7847 7848 Level: developer 7849 7850 .seealso: MatGetRowIJ(), MatRestoreColumnIJ() 7851 @*/ 7852 PetscErrorCode MatRestoreRowIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool *done) 7853 { 7854 PetscErrorCode ierr; 7855 7856 PetscFunctionBegin; 7857 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7858 PetscValidType(mat,1); 7859 if (ia) PetscValidIntPointer(ia,6); 7860 if (ja) PetscValidIntPointer(ja,7); 7861 PetscValidBoolPointer(done,8); 7862 MatCheckPreallocated(mat,1); 7863 7864 if (!mat->ops->restorerowij) *done = PETSC_FALSE; 7865 else { 7866 *done = PETSC_TRUE; 7867 ierr = (*mat->ops->restorerowij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr); 7868 if (n) *n = 0; 7869 if (ia) *ia = NULL; 7870 if (ja) *ja = NULL; 7871 } 7872 PetscFunctionReturn(0); 7873 } 7874 7875 /*@C 7876 MatRestoreColumnIJ - Call after you are completed with the ia,ja indices obtained with 7877 MatGetColumnIJ(). 7878 7879 Collective on Mat 7880 7881 Input Parameters: 7882 + mat - the matrix 7883 . shift - 1 or zero indicating we want the indices starting at 0 or 1 7884 . symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 7885 symmetrized 7886 - inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the 7887 inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is 7888 always used. 7889 7890 Output Parameters: 7891 + n - size of (possibly compressed) matrix 7892 . ia - the column pointers 7893 . ja - the row indices 7894 - done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned 7895 7896 Level: developer 7897 7898 .seealso: MatGetColumnIJ(), MatRestoreRowIJ() 7899 @*/ 7900 PetscErrorCode MatRestoreColumnIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool *done) 7901 { 7902 PetscErrorCode ierr; 7903 7904 PetscFunctionBegin; 7905 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7906 PetscValidType(mat,1); 7907 if (ia) PetscValidIntPointer(ia,6); 7908 if (ja) PetscValidIntPointer(ja,7); 7909 PetscValidBoolPointer(done,8); 7910 MatCheckPreallocated(mat,1); 7911 7912 if (!mat->ops->restorecolumnij) *done = PETSC_FALSE; 7913 else { 7914 *done = PETSC_TRUE; 7915 ierr = (*mat->ops->restorecolumnij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr); 7916 if (n) *n = 0; 7917 if (ia) *ia = NULL; 7918 if (ja) *ja = NULL; 7919 } 7920 PetscFunctionReturn(0); 7921 } 7922 7923 /*@C 7924 MatColoringPatch -Used inside matrix coloring routines that 7925 use MatGetRowIJ() and/or MatGetColumnIJ(). 7926 7927 Collective on Mat 7928 7929 Input Parameters: 7930 + mat - the matrix 7931 . ncolors - max color value 7932 . n - number of entries in colorarray 7933 - colorarray - array indicating color for each column 7934 7935 Output Parameters: 7936 . iscoloring - coloring generated using colorarray information 7937 7938 Level: developer 7939 7940 .seealso: MatGetRowIJ(), MatGetColumnIJ() 7941 7942 @*/ 7943 PetscErrorCode MatColoringPatch(Mat mat,PetscInt ncolors,PetscInt n,ISColoringValue colorarray[],ISColoring *iscoloring) 7944 { 7945 PetscErrorCode ierr; 7946 7947 PetscFunctionBegin; 7948 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7949 PetscValidType(mat,1); 7950 PetscValidIntPointer(colorarray,4); 7951 PetscValidPointer(iscoloring,5); 7952 MatCheckPreallocated(mat,1); 7953 7954 if (!mat->ops->coloringpatch) { 7955 ierr = ISColoringCreate(PetscObjectComm((PetscObject)mat),ncolors,n,colorarray,PETSC_OWN_POINTER,iscoloring);CHKERRQ(ierr); 7956 } else { 7957 ierr = (*mat->ops->coloringpatch)(mat,ncolors,n,colorarray,iscoloring);CHKERRQ(ierr); 7958 } 7959 PetscFunctionReturn(0); 7960 } 7961 7962 /*@ 7963 MatSetUnfactored - Resets a factored matrix to be treated as unfactored. 7964 7965 Logically Collective on Mat 7966 7967 Input Parameter: 7968 . mat - the factored matrix to be reset 7969 7970 Notes: 7971 This routine should be used only with factored matrices formed by in-place 7972 factorization via ILU(0) (or by in-place LU factorization for the MATSEQDENSE 7973 format). This option can save memory, for example, when solving nonlinear 7974 systems with a matrix-free Newton-Krylov method and a matrix-based, in-place 7975 ILU(0) preconditioner. 7976 7977 Note that one can specify in-place ILU(0) factorization by calling 7978 .vb 7979 PCType(pc,PCILU); 7980 PCFactorSeUseInPlace(pc); 7981 .ve 7982 or by using the options -pc_type ilu -pc_factor_in_place 7983 7984 In-place factorization ILU(0) can also be used as a local 7985 solver for the blocks within the block Jacobi or additive Schwarz 7986 methods (runtime option: -sub_pc_factor_in_place). See Users-Manual: ch_pc 7987 for details on setting local solver options. 7988 7989 Most users should employ the simplified KSP interface for linear solvers 7990 instead of working directly with matrix algebra routines such as this. 7991 See, e.g., KSPCreate(). 7992 7993 Level: developer 7994 7995 .seealso: PCFactorSetUseInPlace(), PCFactorGetUseInPlace() 7996 7997 @*/ 7998 PetscErrorCode MatSetUnfactored(Mat mat) 7999 { 8000 PetscErrorCode ierr; 8001 8002 PetscFunctionBegin; 8003 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8004 PetscValidType(mat,1); 8005 MatCheckPreallocated(mat,1); 8006 mat->factortype = MAT_FACTOR_NONE; 8007 if (!mat->ops->setunfactored) PetscFunctionReturn(0); 8008 ierr = (*mat->ops->setunfactored)(mat);CHKERRQ(ierr); 8009 PetscFunctionReturn(0); 8010 } 8011 8012 /*MC 8013 MatDenseGetArrayF90 - Accesses a matrix array from Fortran90. 8014 8015 Synopsis: 8016 MatDenseGetArrayF90(Mat x,{Scalar, pointer :: xx_v(:,:)},integer ierr) 8017 8018 Not collective 8019 8020 Input Parameter: 8021 . x - matrix 8022 8023 Output Parameters: 8024 + xx_v - the Fortran90 pointer to the array 8025 - ierr - error code 8026 8027 Example of Usage: 8028 .vb 8029 PetscScalar, pointer xx_v(:,:) 8030 .... 8031 call MatDenseGetArrayF90(x,xx_v,ierr) 8032 a = xx_v(3) 8033 call MatDenseRestoreArrayF90(x,xx_v,ierr) 8034 .ve 8035 8036 Level: advanced 8037 8038 .seealso: MatDenseRestoreArrayF90(), MatDenseGetArray(), MatDenseRestoreArray(), MatSeqAIJGetArrayF90() 8039 8040 M*/ 8041 8042 /*MC 8043 MatDenseRestoreArrayF90 - Restores a matrix array that has been 8044 accessed with MatDenseGetArrayF90(). 8045 8046 Synopsis: 8047 MatDenseRestoreArrayF90(Mat x,{Scalar, pointer :: xx_v(:,:)},integer ierr) 8048 8049 Not collective 8050 8051 Input Parameters: 8052 + x - matrix 8053 - xx_v - the Fortran90 pointer to the array 8054 8055 Output Parameter: 8056 . ierr - error code 8057 8058 Example of Usage: 8059 .vb 8060 PetscScalar, pointer xx_v(:,:) 8061 .... 8062 call MatDenseGetArrayF90(x,xx_v,ierr) 8063 a = xx_v(3) 8064 call MatDenseRestoreArrayF90(x,xx_v,ierr) 8065 .ve 8066 8067 Level: advanced 8068 8069 .seealso: MatDenseGetArrayF90(), MatDenseGetArray(), MatDenseRestoreArray(), MatSeqAIJRestoreArrayF90() 8070 8071 M*/ 8072 8073 /*MC 8074 MatSeqAIJGetArrayF90 - Accesses a matrix array from Fortran90. 8075 8076 Synopsis: 8077 MatSeqAIJGetArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr) 8078 8079 Not collective 8080 8081 Input Parameter: 8082 . x - matrix 8083 8084 Output Parameters: 8085 + xx_v - the Fortran90 pointer to the array 8086 - ierr - error code 8087 8088 Example of Usage: 8089 .vb 8090 PetscScalar, pointer xx_v(:) 8091 .... 8092 call MatSeqAIJGetArrayF90(x,xx_v,ierr) 8093 a = xx_v(3) 8094 call MatSeqAIJRestoreArrayF90(x,xx_v,ierr) 8095 .ve 8096 8097 Level: advanced 8098 8099 .seealso: MatSeqAIJRestoreArrayF90(), MatSeqAIJGetArray(), MatSeqAIJRestoreArray(), MatDenseGetArrayF90() 8100 8101 M*/ 8102 8103 /*MC 8104 MatSeqAIJRestoreArrayF90 - Restores a matrix array that has been 8105 accessed with MatSeqAIJGetArrayF90(). 8106 8107 Synopsis: 8108 MatSeqAIJRestoreArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr) 8109 8110 Not collective 8111 8112 Input Parameters: 8113 + x - matrix 8114 - xx_v - the Fortran90 pointer to the array 8115 8116 Output Parameter: 8117 . ierr - error code 8118 8119 Example of Usage: 8120 .vb 8121 PetscScalar, pointer xx_v(:) 8122 .... 8123 call MatSeqAIJGetArrayF90(x,xx_v,ierr) 8124 a = xx_v(3) 8125 call MatSeqAIJRestoreArrayF90(x,xx_v,ierr) 8126 .ve 8127 8128 Level: advanced 8129 8130 .seealso: MatSeqAIJGetArrayF90(), MatSeqAIJGetArray(), MatSeqAIJRestoreArray(), MatDenseRestoreArrayF90() 8131 8132 M*/ 8133 8134 /*@ 8135 MatCreateSubMatrix - Gets a single submatrix on the same number of processors 8136 as the original matrix. 8137 8138 Collective on Mat 8139 8140 Input Parameters: 8141 + mat - the original matrix 8142 . isrow - parallel IS containing the rows this processor should obtain 8143 . 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. 8144 - cll - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 8145 8146 Output Parameter: 8147 . newmat - the new submatrix, of the same type as the old 8148 8149 Level: advanced 8150 8151 Notes: 8152 The submatrix will be able to be multiplied with vectors using the same layout as iscol. 8153 8154 Some matrix types place restrictions on the row and column indices, such 8155 as that they be sorted or that they be equal to each other. 8156 8157 The index sets may not have duplicate entries. 8158 8159 The first time this is called you should use a cll of MAT_INITIAL_MATRIX, 8160 the MatCreateSubMatrix() routine will create the newmat for you. Any additional calls 8161 to this routine with a mat of the same nonzero structure and with a call of MAT_REUSE_MATRIX 8162 will reuse the matrix generated the first time. You should call MatDestroy() on newmat when 8163 you are finished using it. 8164 8165 The communicator of the newly obtained matrix is ALWAYS the same as the communicator of 8166 the input matrix. 8167 8168 If iscol is NULL then all columns are obtained (not supported in Fortran). 8169 8170 Example usage: 8171 Consider the following 8x8 matrix with 34 non-zero values, that is 8172 assembled across 3 processors. Let's assume that proc0 owns 3 rows, 8173 proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown 8174 as follows: 8175 8176 .vb 8177 1 2 0 | 0 3 0 | 0 4 8178 Proc0 0 5 6 | 7 0 0 | 8 0 8179 9 0 10 | 11 0 0 | 12 0 8180 ------------------------------------- 8181 13 0 14 | 15 16 17 | 0 0 8182 Proc1 0 18 0 | 19 20 21 | 0 0 8183 0 0 0 | 22 23 0 | 24 0 8184 ------------------------------------- 8185 Proc2 25 26 27 | 0 0 28 | 29 0 8186 30 0 0 | 31 32 33 | 0 34 8187 .ve 8188 8189 Suppose isrow = [0 1 | 4 | 6 7] and iscol = [1 2 | 3 4 5 | 6]. The resulting submatrix is 8190 8191 .vb 8192 2 0 | 0 3 0 | 0 8193 Proc0 5 6 | 7 0 0 | 8 8194 ------------------------------- 8195 Proc1 18 0 | 19 20 21 | 0 8196 ------------------------------- 8197 Proc2 26 27 | 0 0 28 | 29 8198 0 0 | 31 32 33 | 0 8199 .ve 8200 8201 .seealso: MatCreateSubMatrices(), MatCreateSubMatricesMPI(), MatCreateSubMatrixVirtual(), MatSubMatrixVirtualUpdate() 8202 @*/ 8203 PetscErrorCode MatCreateSubMatrix(Mat mat,IS isrow,IS iscol,MatReuse cll,Mat *newmat) 8204 { 8205 PetscErrorCode ierr; 8206 PetscMPIInt size; 8207 Mat *local; 8208 IS iscoltmp; 8209 PetscBool flg; 8210 8211 PetscFunctionBegin; 8212 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8213 PetscValidHeaderSpecific(isrow,IS_CLASSID,2); 8214 if (iscol) PetscValidHeaderSpecific(iscol,IS_CLASSID,3); 8215 PetscValidPointer(newmat,5); 8216 if (cll == MAT_REUSE_MATRIX) PetscValidHeaderSpecific(*newmat,MAT_CLASSID,5); 8217 PetscValidType(mat,1); 8218 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 8219 PetscCheckFalse(cll == MAT_IGNORE_MATRIX,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Cannot use MAT_IGNORE_MATRIX"); 8220 8221 MatCheckPreallocated(mat,1); 8222 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);CHKERRMPI(ierr); 8223 8224 if (!iscol || isrow == iscol) { 8225 PetscBool stride; 8226 PetscMPIInt grabentirematrix = 0,grab; 8227 ierr = PetscObjectTypeCompare((PetscObject)isrow,ISSTRIDE,&stride);CHKERRQ(ierr); 8228 if (stride) { 8229 PetscInt first,step,n,rstart,rend; 8230 ierr = ISStrideGetInfo(isrow,&first,&step);CHKERRQ(ierr); 8231 if (step == 1) { 8232 ierr = MatGetOwnershipRange(mat,&rstart,&rend);CHKERRQ(ierr); 8233 if (rstart == first) { 8234 ierr = ISGetLocalSize(isrow,&n);CHKERRQ(ierr); 8235 if (n == rend-rstart) { 8236 grabentirematrix = 1; 8237 } 8238 } 8239 } 8240 } 8241 ierr = MPIU_Allreduce(&grabentirematrix,&grab,1,MPI_INT,MPI_MIN,PetscObjectComm((PetscObject)mat));CHKERRMPI(ierr); 8242 if (grab) { 8243 ierr = PetscInfo(mat,"Getting entire matrix as submatrix\n");CHKERRQ(ierr); 8244 if (cll == MAT_INITIAL_MATRIX) { 8245 *newmat = mat; 8246 ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr); 8247 } 8248 PetscFunctionReturn(0); 8249 } 8250 } 8251 8252 if (!iscol) { 8253 ierr = ISCreateStride(PetscObjectComm((PetscObject)mat),mat->cmap->n,mat->cmap->rstart,1,&iscoltmp);CHKERRQ(ierr); 8254 } else { 8255 iscoltmp = iscol; 8256 } 8257 8258 /* if original matrix is on just one processor then use submatrix generated */ 8259 if (mat->ops->createsubmatrices && !mat->ops->createsubmatrix && size == 1 && cll == MAT_REUSE_MATRIX) { 8260 ierr = MatCreateSubMatrices(mat,1,&isrow,&iscoltmp,MAT_REUSE_MATRIX,&newmat);CHKERRQ(ierr); 8261 goto setproperties; 8262 } else if (mat->ops->createsubmatrices && !mat->ops->createsubmatrix && size == 1) { 8263 ierr = MatCreateSubMatrices(mat,1,&isrow,&iscoltmp,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr); 8264 *newmat = *local; 8265 ierr = PetscFree(local);CHKERRQ(ierr); 8266 goto setproperties; 8267 } else if (!mat->ops->createsubmatrix) { 8268 /* Create a new matrix type that implements the operation using the full matrix */ 8269 ierr = PetscLogEventBegin(MAT_CreateSubMat,mat,0,0,0);CHKERRQ(ierr); 8270 switch (cll) { 8271 case MAT_INITIAL_MATRIX: 8272 ierr = MatCreateSubMatrixVirtual(mat,isrow,iscoltmp,newmat);CHKERRQ(ierr); 8273 break; 8274 case MAT_REUSE_MATRIX: 8275 ierr = MatSubMatrixVirtualUpdate(*newmat,mat,isrow,iscoltmp);CHKERRQ(ierr); 8276 break; 8277 default: SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Invalid MatReuse, must be either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX"); 8278 } 8279 ierr = PetscLogEventEnd(MAT_CreateSubMat,mat,0,0,0);CHKERRQ(ierr); 8280 goto setproperties; 8281 } 8282 8283 PetscCheckFalse(!mat->ops->createsubmatrix,PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 8284 ierr = PetscLogEventBegin(MAT_CreateSubMat,mat,0,0,0);CHKERRQ(ierr); 8285 ierr = (*mat->ops->createsubmatrix)(mat,isrow,iscoltmp,cll,newmat);CHKERRQ(ierr); 8286 ierr = PetscLogEventEnd(MAT_CreateSubMat,mat,0,0,0);CHKERRQ(ierr); 8287 8288 setproperties: 8289 ierr = ISEqualUnsorted(isrow,iscoltmp,&flg);CHKERRQ(ierr); 8290 if (flg) { 8291 ierr = MatPropagateSymmetryOptions(mat,*newmat);CHKERRQ(ierr); 8292 } 8293 if (!iscol) {ierr = ISDestroy(&iscoltmp);CHKERRQ(ierr);} 8294 if (*newmat && cll == MAT_INITIAL_MATRIX) {ierr = PetscObjectStateIncrease((PetscObject)*newmat);CHKERRQ(ierr);} 8295 PetscFunctionReturn(0); 8296 } 8297 8298 /*@ 8299 MatPropagateSymmetryOptions - Propagates symmetry options set on a matrix to another matrix 8300 8301 Not Collective 8302 8303 Input Parameters: 8304 + A - the matrix we wish to propagate options from 8305 - B - the matrix we wish to propagate options to 8306 8307 Level: beginner 8308 8309 Notes: Propagates the options associated to MAT_SYMMETRY_ETERNAL, MAT_STRUCTURALLY_SYMMETRIC, MAT_HERMITIAN, MAT_SPD and MAT_SYMMETRIC 8310 8311 .seealso: MatSetOption() 8312 @*/ 8313 PetscErrorCode MatPropagateSymmetryOptions(Mat A, Mat B) 8314 { 8315 PetscErrorCode ierr; 8316 8317 PetscFunctionBegin; 8318 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 8319 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 8320 if (A->symmetric_eternal) { /* symmetric_eternal does not have a corresponding *set flag */ 8321 ierr = MatSetOption(B,MAT_SYMMETRY_ETERNAL,A->symmetric_eternal);CHKERRQ(ierr); 8322 } 8323 if (A->structurally_symmetric_set) { 8324 ierr = MatSetOption(B,MAT_STRUCTURALLY_SYMMETRIC,A->structurally_symmetric);CHKERRQ(ierr); 8325 } 8326 if (A->hermitian_set) { 8327 ierr = MatSetOption(B,MAT_HERMITIAN,A->hermitian);CHKERRQ(ierr); 8328 } 8329 if (A->spd_set) { 8330 ierr = MatSetOption(B,MAT_SPD,A->spd);CHKERRQ(ierr); 8331 } 8332 if (A->symmetric_set) { 8333 ierr = MatSetOption(B,MAT_SYMMETRIC,A->symmetric);CHKERRQ(ierr); 8334 } 8335 PetscFunctionReturn(0); 8336 } 8337 8338 /*@ 8339 MatStashSetInitialSize - sets the sizes of the matrix stash, that is 8340 used during the assembly process to store values that belong to 8341 other processors. 8342 8343 Not Collective 8344 8345 Input Parameters: 8346 + mat - the matrix 8347 . size - the initial size of the stash. 8348 - bsize - the initial size of the block-stash(if used). 8349 8350 Options Database Keys: 8351 + -matstash_initial_size <size> or <size0,size1,...sizep-1> 8352 - -matstash_block_initial_size <bsize> or <bsize0,bsize1,...bsizep-1> 8353 8354 Level: intermediate 8355 8356 Notes: 8357 The block-stash is used for values set with MatSetValuesBlocked() while 8358 the stash is used for values set with MatSetValues() 8359 8360 Run with the option -info and look for output of the form 8361 MatAssemblyBegin_MPIXXX:Stash has MM entries, uses nn mallocs. 8362 to determine the appropriate value, MM, to use for size and 8363 MatAssemblyBegin_MPIXXX:Block-Stash has BMM entries, uses nn mallocs. 8364 to determine the value, BMM to use for bsize 8365 8366 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), Mat, MatStashGetInfo() 8367 8368 @*/ 8369 PetscErrorCode MatStashSetInitialSize(Mat mat,PetscInt size, PetscInt bsize) 8370 { 8371 PetscErrorCode ierr; 8372 8373 PetscFunctionBegin; 8374 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8375 PetscValidType(mat,1); 8376 ierr = MatStashSetInitialSize_Private(&mat->stash,size);CHKERRQ(ierr); 8377 ierr = MatStashSetInitialSize_Private(&mat->bstash,bsize);CHKERRQ(ierr); 8378 PetscFunctionReturn(0); 8379 } 8380 8381 /*@ 8382 MatInterpolateAdd - w = y + A*x or A'*x depending on the shape of 8383 the matrix 8384 8385 Neighbor-wise Collective on Mat 8386 8387 Input Parameters: 8388 + mat - the matrix 8389 . x,y - the vectors 8390 - w - where the result is stored 8391 8392 Level: intermediate 8393 8394 Notes: 8395 w may be the same vector as y. 8396 8397 This allows one to use either the restriction or interpolation (its transpose) 8398 matrix to do the interpolation 8399 8400 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict() 8401 8402 @*/ 8403 PetscErrorCode MatInterpolateAdd(Mat A,Vec x,Vec y,Vec w) 8404 { 8405 PetscErrorCode ierr; 8406 PetscInt M,N,Ny; 8407 8408 PetscFunctionBegin; 8409 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 8410 PetscValidHeaderSpecific(x,VEC_CLASSID,2); 8411 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 8412 PetscValidHeaderSpecific(w,VEC_CLASSID,4); 8413 ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr); 8414 ierr = VecGetSize(y,&Ny);CHKERRQ(ierr); 8415 if (M == Ny) { 8416 ierr = MatMultAdd(A,x,y,w);CHKERRQ(ierr); 8417 } else { 8418 ierr = MatMultTransposeAdd(A,x,y,w);CHKERRQ(ierr); 8419 } 8420 PetscFunctionReturn(0); 8421 } 8422 8423 /*@ 8424 MatInterpolate - y = A*x or A'*x depending on the shape of 8425 the matrix 8426 8427 Neighbor-wise Collective on Mat 8428 8429 Input Parameters: 8430 + mat - the matrix 8431 - x,y - the vectors 8432 8433 Level: intermediate 8434 8435 Notes: 8436 This allows one to use either the restriction or interpolation (its transpose) 8437 matrix to do the interpolation 8438 8439 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict() 8440 8441 @*/ 8442 PetscErrorCode MatInterpolate(Mat A,Vec x,Vec y) 8443 { 8444 PetscErrorCode ierr; 8445 PetscInt M,N,Ny; 8446 8447 PetscFunctionBegin; 8448 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 8449 PetscValidHeaderSpecific(x,VEC_CLASSID,2); 8450 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 8451 ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr); 8452 ierr = VecGetSize(y,&Ny);CHKERRQ(ierr); 8453 if (M == Ny) { 8454 ierr = MatMult(A,x,y);CHKERRQ(ierr); 8455 } else { 8456 ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr); 8457 } 8458 PetscFunctionReturn(0); 8459 } 8460 8461 /*@ 8462 MatRestrict - y = A*x or A'*x 8463 8464 Neighbor-wise Collective on Mat 8465 8466 Input Parameters: 8467 + mat - the matrix 8468 - x,y - the vectors 8469 8470 Level: intermediate 8471 8472 Notes: 8473 This allows one to use either the restriction or interpolation (its transpose) 8474 matrix to do the restriction 8475 8476 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatInterpolate() 8477 8478 @*/ 8479 PetscErrorCode MatRestrict(Mat A,Vec x,Vec y) 8480 { 8481 PetscErrorCode ierr; 8482 PetscInt M,N,Ny; 8483 8484 PetscFunctionBegin; 8485 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 8486 PetscValidHeaderSpecific(x,VEC_CLASSID,2); 8487 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 8488 ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr); 8489 ierr = VecGetSize(y,&Ny);CHKERRQ(ierr); 8490 if (M == Ny) { 8491 ierr = MatMult(A,x,y);CHKERRQ(ierr); 8492 } else { 8493 ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr); 8494 } 8495 PetscFunctionReturn(0); 8496 } 8497 8498 /*@ 8499 MatMatInterpolateAdd - Y = W + A*X or W + A'*X 8500 8501 Neighbor-wise Collective on Mat 8502 8503 Input Parameters: 8504 + mat - the matrix 8505 - w, x - the input dense matrices 8506 8507 Output Parameters: 8508 . y - the output dense matrix 8509 8510 Level: intermediate 8511 8512 Notes: 8513 This allows one to use either the restriction or interpolation (its transpose) 8514 matrix to do the interpolation. y matrix can be reused if already created with the proper sizes, 8515 otherwise it will be recreated. y must be initialized to NULL if not supplied. 8516 8517 .seealso: MatInterpolateAdd(), MatMatInterpolate(), MatMatRestrict() 8518 8519 @*/ 8520 PetscErrorCode MatMatInterpolateAdd(Mat A,Mat x,Mat w,Mat *y) 8521 { 8522 PetscErrorCode ierr; 8523 PetscInt M,N,Mx,Nx,Mo,My = 0,Ny = 0; 8524 PetscBool trans = PETSC_TRUE; 8525 MatReuse reuse = MAT_INITIAL_MATRIX; 8526 8527 PetscFunctionBegin; 8528 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 8529 PetscValidHeaderSpecific(x,MAT_CLASSID,2); 8530 PetscValidType(x,2); 8531 if (w) PetscValidHeaderSpecific(w,MAT_CLASSID,3); 8532 if (*y) PetscValidHeaderSpecific(*y,MAT_CLASSID,4); 8533 ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr); 8534 ierr = MatGetSize(x,&Mx,&Nx);CHKERRQ(ierr); 8535 if (N == Mx) trans = PETSC_FALSE; 8536 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); 8537 Mo = trans ? N : M; 8538 if (*y) { 8539 ierr = MatGetSize(*y,&My,&Ny);CHKERRQ(ierr); 8540 if (Mo == My && Nx == Ny) { reuse = MAT_REUSE_MATRIX; } 8541 else { 8542 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); 8543 ierr = MatDestroy(y);CHKERRQ(ierr); 8544 } 8545 } 8546 8547 if (w && *y == w) { /* this is to minimize changes in PCMG */ 8548 PetscBool flg; 8549 8550 ierr = PetscObjectQuery((PetscObject)*y,"__MatMatIntAdd_w",(PetscObject*)&w);CHKERRQ(ierr); 8551 if (w) { 8552 PetscInt My,Ny,Mw,Nw; 8553 8554 ierr = PetscObjectTypeCompare((PetscObject)*y,((PetscObject)w)->type_name,&flg);CHKERRQ(ierr); 8555 ierr = MatGetSize(*y,&My,&Ny);CHKERRQ(ierr); 8556 ierr = MatGetSize(w,&Mw,&Nw);CHKERRQ(ierr); 8557 if (!flg || My != Mw || Ny != Nw) w = NULL; 8558 } 8559 if (!w) { 8560 ierr = MatDuplicate(*y,MAT_COPY_VALUES,&w);CHKERRQ(ierr); 8561 ierr = PetscObjectCompose((PetscObject)*y,"__MatMatIntAdd_w",(PetscObject)w);CHKERRQ(ierr); 8562 ierr = PetscLogObjectParent((PetscObject)*y,(PetscObject)w);CHKERRQ(ierr); 8563 ierr = PetscObjectDereference((PetscObject)w);CHKERRQ(ierr); 8564 } else { 8565 ierr = MatCopy(*y,w,UNKNOWN_NONZERO_PATTERN);CHKERRQ(ierr); 8566 } 8567 } 8568 if (!trans) { 8569 ierr = MatMatMult(A,x,reuse,PETSC_DEFAULT,y);CHKERRQ(ierr); 8570 } else { 8571 ierr = MatTransposeMatMult(A,x,reuse,PETSC_DEFAULT,y);CHKERRQ(ierr); 8572 } 8573 if (w) { 8574 ierr = MatAXPY(*y,1.0,w,UNKNOWN_NONZERO_PATTERN);CHKERRQ(ierr); 8575 } 8576 PetscFunctionReturn(0); 8577 } 8578 8579 /*@ 8580 MatMatInterpolate - Y = A*X or A'*X 8581 8582 Neighbor-wise Collective on Mat 8583 8584 Input Parameters: 8585 + mat - the matrix 8586 - x - the input dense matrix 8587 8588 Output Parameters: 8589 . y - the output dense matrix 8590 8591 Level: intermediate 8592 8593 Notes: 8594 This allows one to use either the restriction or interpolation (its transpose) 8595 matrix to do the interpolation. y matrix can be reused if already created with the proper sizes, 8596 otherwise it will be recreated. y must be initialized to NULL if not supplied. 8597 8598 .seealso: MatInterpolate(), MatRestrict(), MatMatRestrict() 8599 8600 @*/ 8601 PetscErrorCode MatMatInterpolate(Mat A,Mat x,Mat *y) 8602 { 8603 PetscErrorCode ierr; 8604 8605 PetscFunctionBegin; 8606 ierr = MatMatInterpolateAdd(A,x,NULL,y);CHKERRQ(ierr); 8607 PetscFunctionReturn(0); 8608 } 8609 8610 /*@ 8611 MatMatRestrict - Y = A*X or A'*X 8612 8613 Neighbor-wise Collective on Mat 8614 8615 Input Parameters: 8616 + mat - the matrix 8617 - x - the input dense matrix 8618 8619 Output Parameters: 8620 . y - the output dense matrix 8621 8622 Level: intermediate 8623 8624 Notes: 8625 This allows one to use either the restriction or interpolation (its transpose) 8626 matrix to do the restriction. y matrix can be reused if already created with the proper sizes, 8627 otherwise it will be recreated. y must be initialized to NULL if not supplied. 8628 8629 .seealso: MatRestrict(), MatInterpolate(), MatMatInterpolate() 8630 @*/ 8631 PetscErrorCode MatMatRestrict(Mat A,Mat x,Mat *y) 8632 { 8633 PetscErrorCode ierr; 8634 8635 PetscFunctionBegin; 8636 ierr = MatMatInterpolateAdd(A,x,NULL,y);CHKERRQ(ierr); 8637 PetscFunctionReturn(0); 8638 } 8639 8640 /*@ 8641 MatGetNullSpace - retrieves the null space of a matrix. 8642 8643 Logically Collective on Mat 8644 8645 Input Parameters: 8646 + mat - the matrix 8647 - nullsp - the null space object 8648 8649 Level: developer 8650 8651 .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace(), MatSetNullSpace() 8652 @*/ 8653 PetscErrorCode MatGetNullSpace(Mat mat, MatNullSpace *nullsp) 8654 { 8655 PetscFunctionBegin; 8656 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8657 PetscValidPointer(nullsp,2); 8658 *nullsp = (mat->symmetric_set && mat->symmetric && !mat->nullsp) ? mat->transnullsp : mat->nullsp; 8659 PetscFunctionReturn(0); 8660 } 8661 8662 /*@ 8663 MatSetNullSpace - attaches a null space to a matrix. 8664 8665 Logically Collective on Mat 8666 8667 Input Parameters: 8668 + mat - the matrix 8669 - nullsp - the null space object 8670 8671 Level: advanced 8672 8673 Notes: 8674 This null space is used by the linear solvers. Overwrites any previous null space that may have been attached 8675 8676 For inconsistent singular systems (linear systems where the right hand side is not in the range of the operator) you also likely should 8677 call MatSetTransposeNullSpace(). This allows the linear system to be solved in a least squares sense. 8678 8679 You can remove the null space by calling this routine with an nullsp of NULL 8680 8681 The fundamental theorem of linear algebra (Gilbert Strang, Introduction to Applied Mathematics, page 72) states that 8682 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). 8683 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 8684 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 8685 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). 8686 8687 Krylov solvers can produce the minimal norm solution to the least squares problem by utilizing MatNullSpaceRemove(). 8688 8689 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 8690 routine also automatically calls MatSetTransposeNullSpace(). 8691 8692 .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace(), MatGetNullSpace(), MatSetTransposeNullSpace(), MatGetTransposeNullSpace(), MatNullSpaceRemove() 8693 @*/ 8694 PetscErrorCode MatSetNullSpace(Mat mat,MatNullSpace nullsp) 8695 { 8696 PetscErrorCode ierr; 8697 8698 PetscFunctionBegin; 8699 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8700 if (nullsp) PetscValidHeaderSpecific(nullsp,MAT_NULLSPACE_CLASSID,2); 8701 if (nullsp) {ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr);} 8702 ierr = MatNullSpaceDestroy(&mat->nullsp);CHKERRQ(ierr); 8703 mat->nullsp = nullsp; 8704 if (mat->symmetric_set && mat->symmetric) { 8705 ierr = MatSetTransposeNullSpace(mat,nullsp);CHKERRQ(ierr); 8706 } 8707 PetscFunctionReturn(0); 8708 } 8709 8710 /*@ 8711 MatGetTransposeNullSpace - retrieves the null space of the transpose of a matrix. 8712 8713 Logically Collective on Mat 8714 8715 Input Parameters: 8716 + mat - the matrix 8717 - nullsp - the null space object 8718 8719 Level: developer 8720 8721 .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace(), MatSetTransposeNullSpace(), MatSetNullSpace(), MatGetNullSpace() 8722 @*/ 8723 PetscErrorCode MatGetTransposeNullSpace(Mat mat, MatNullSpace *nullsp) 8724 { 8725 PetscFunctionBegin; 8726 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8727 PetscValidType(mat,1); 8728 PetscValidPointer(nullsp,2); 8729 *nullsp = (mat->symmetric_set && mat->symmetric && !mat->transnullsp) ? mat->nullsp : mat->transnullsp; 8730 PetscFunctionReturn(0); 8731 } 8732 8733 /*@ 8734 MatSetTransposeNullSpace - attaches a null space to a matrix. 8735 8736 Logically Collective on Mat 8737 8738 Input Parameters: 8739 + mat - the matrix 8740 - nullsp - the null space object 8741 8742 Level: advanced 8743 8744 Notes: 8745 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. 8746 You must also call MatSetNullSpace() 8747 8748 The fundamental theorem of linear algebra (Gilbert Strang, Introduction to Applied Mathematics, page 72) states that 8749 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). 8750 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 8751 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 8752 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). 8753 8754 Krylov solvers can produce the minimal norm solution to the least squares problem by utilizing MatNullSpaceRemove(). 8755 8756 .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace(), MatGetNullSpace(), MatSetNullSpace(), MatGetTransposeNullSpace(), MatNullSpaceRemove() 8757 @*/ 8758 PetscErrorCode MatSetTransposeNullSpace(Mat mat,MatNullSpace nullsp) 8759 { 8760 PetscErrorCode ierr; 8761 8762 PetscFunctionBegin; 8763 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8764 if (nullsp) PetscValidHeaderSpecific(nullsp,MAT_NULLSPACE_CLASSID,2); 8765 if (nullsp) {ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr);} 8766 ierr = MatNullSpaceDestroy(&mat->transnullsp);CHKERRQ(ierr); 8767 mat->transnullsp = nullsp; 8768 PetscFunctionReturn(0); 8769 } 8770 8771 /*@ 8772 MatSetNearNullSpace - attaches a null space to a matrix, which is often the null space (rigid body modes) of the operator without boundary conditions 8773 This null space will be used to provide near null space vectors to a multigrid preconditioner built from this matrix. 8774 8775 Logically Collective on Mat 8776 8777 Input Parameters: 8778 + mat - the matrix 8779 - nullsp - the null space object 8780 8781 Level: advanced 8782 8783 Notes: 8784 Overwrites any previous near null space that may have been attached 8785 8786 You can remove the null space by calling this routine with an nullsp of NULL 8787 8788 .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNullSpace(), MatNullSpaceCreateRigidBody(), MatGetNearNullSpace() 8789 @*/ 8790 PetscErrorCode MatSetNearNullSpace(Mat mat,MatNullSpace nullsp) 8791 { 8792 PetscErrorCode ierr; 8793 8794 PetscFunctionBegin; 8795 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8796 PetscValidType(mat,1); 8797 if (nullsp) PetscValidHeaderSpecific(nullsp,MAT_NULLSPACE_CLASSID,2); 8798 MatCheckPreallocated(mat,1); 8799 if (nullsp) {ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr);} 8800 ierr = MatNullSpaceDestroy(&mat->nearnullsp);CHKERRQ(ierr); 8801 mat->nearnullsp = nullsp; 8802 PetscFunctionReturn(0); 8803 } 8804 8805 /*@ 8806 MatGetNearNullSpace - Get null space attached with MatSetNearNullSpace() 8807 8808 Not Collective 8809 8810 Input Parameter: 8811 . mat - the matrix 8812 8813 Output Parameter: 8814 . nullsp - the null space object, NULL if not set 8815 8816 Level: developer 8817 8818 .seealso: MatSetNearNullSpace(), MatGetNullSpace(), MatNullSpaceCreate() 8819 @*/ 8820 PetscErrorCode MatGetNearNullSpace(Mat mat,MatNullSpace *nullsp) 8821 { 8822 PetscFunctionBegin; 8823 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8824 PetscValidType(mat,1); 8825 PetscValidPointer(nullsp,2); 8826 MatCheckPreallocated(mat,1); 8827 *nullsp = mat->nearnullsp; 8828 PetscFunctionReturn(0); 8829 } 8830 8831 /*@C 8832 MatICCFactor - Performs in-place incomplete Cholesky factorization of matrix. 8833 8834 Collective on Mat 8835 8836 Input Parameters: 8837 + mat - the matrix 8838 . row - row/column permutation 8839 . fill - expected fill factor >= 1.0 8840 - level - level of fill, for ICC(k) 8841 8842 Notes: 8843 Probably really in-place only when level of fill is zero, otherwise allocates 8844 new space to store factored matrix and deletes previous memory. 8845 8846 Most users should employ the simplified KSP interface for linear solvers 8847 instead of working directly with matrix algebra routines such as this. 8848 See, e.g., KSPCreate(). 8849 8850 Level: developer 8851 8852 .seealso: MatICCFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor() 8853 8854 Developer Note: fortran interface is not autogenerated as the f90 8855 interface definition cannot be generated correctly [due to MatFactorInfo] 8856 8857 @*/ 8858 PetscErrorCode MatICCFactor(Mat mat,IS row,const MatFactorInfo *info) 8859 { 8860 PetscErrorCode ierr; 8861 8862 PetscFunctionBegin; 8863 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8864 PetscValidType(mat,1); 8865 if (row) PetscValidHeaderSpecific(row,IS_CLASSID,2); 8866 PetscValidPointer(info,3); 8867 PetscCheckFalse(mat->rmap->N != mat->cmap->N,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONG,"matrix must be square"); 8868 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 8869 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 8870 PetscCheckFalse(!mat->ops->iccfactor,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 8871 MatCheckPreallocated(mat,1); 8872 ierr = (*mat->ops->iccfactor)(mat,row,info);CHKERRQ(ierr); 8873 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 8874 PetscFunctionReturn(0); 8875 } 8876 8877 /*@ 8878 MatDiagonalScaleLocal - Scales columns of a matrix given the scaling values including the 8879 ghosted ones. 8880 8881 Not Collective 8882 8883 Input Parameters: 8884 + mat - the matrix 8885 - diag = the diagonal values, including ghost ones 8886 8887 Level: developer 8888 8889 Notes: 8890 Works only for MPIAIJ and MPIBAIJ matrices 8891 8892 .seealso: MatDiagonalScale() 8893 @*/ 8894 PetscErrorCode MatDiagonalScaleLocal(Mat mat,Vec diag) 8895 { 8896 PetscErrorCode ierr; 8897 PetscMPIInt size; 8898 8899 PetscFunctionBegin; 8900 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8901 PetscValidHeaderSpecific(diag,VEC_CLASSID,2); 8902 PetscValidType(mat,1); 8903 8904 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled"); 8905 ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 8906 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);CHKERRMPI(ierr); 8907 if (size == 1) { 8908 PetscInt n,m; 8909 ierr = VecGetSize(diag,&n);CHKERRQ(ierr); 8910 ierr = MatGetSize(mat,NULL,&m);CHKERRQ(ierr); 8911 if (m == n) { 8912 ierr = MatDiagonalScale(mat,NULL,diag);CHKERRQ(ierr); 8913 } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only supported for sequential matrices when no ghost points/periodic conditions"); 8914 } else { 8915 ierr = PetscUseMethod(mat,"MatDiagonalScaleLocal_C",(Mat,Vec),(mat,diag));CHKERRQ(ierr); 8916 } 8917 ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 8918 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 8919 PetscFunctionReturn(0); 8920 } 8921 8922 /*@ 8923 MatGetInertia - Gets the inertia from a factored matrix 8924 8925 Collective on Mat 8926 8927 Input Parameter: 8928 . mat - the matrix 8929 8930 Output Parameters: 8931 + nneg - number of negative eigenvalues 8932 . nzero - number of zero eigenvalues 8933 - npos - number of positive eigenvalues 8934 8935 Level: advanced 8936 8937 Notes: 8938 Matrix must have been factored by MatCholeskyFactor() 8939 8940 @*/ 8941 PetscErrorCode MatGetInertia(Mat mat,PetscInt *nneg,PetscInt *nzero,PetscInt *npos) 8942 { 8943 PetscErrorCode ierr; 8944 8945 PetscFunctionBegin; 8946 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8947 PetscValidType(mat,1); 8948 PetscCheckFalse(!mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 8949 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Numeric factor mat is not assembled"); 8950 PetscCheckFalse(!mat->ops->getinertia,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 8951 ierr = (*mat->ops->getinertia)(mat,nneg,nzero,npos);CHKERRQ(ierr); 8952 PetscFunctionReturn(0); 8953 } 8954 8955 /* ----------------------------------------------------------------*/ 8956 /*@C 8957 MatSolves - Solves A x = b, given a factored matrix, for a collection of vectors 8958 8959 Neighbor-wise Collective on Mats 8960 8961 Input Parameters: 8962 + mat - the factored matrix 8963 - b - the right-hand-side vectors 8964 8965 Output Parameter: 8966 . x - the result vectors 8967 8968 Notes: 8969 The vectors b and x cannot be the same. I.e., one cannot 8970 call MatSolves(A,x,x). 8971 8972 Notes: 8973 Most users should employ the simplified KSP interface for linear solvers 8974 instead of working directly with matrix algebra routines such as this. 8975 See, e.g., KSPCreate(). 8976 8977 Level: developer 8978 8979 .seealso: MatSolveAdd(), MatSolveTranspose(), MatSolveTransposeAdd(), MatSolve() 8980 @*/ 8981 PetscErrorCode MatSolves(Mat mat,Vecs b,Vecs x) 8982 { 8983 PetscErrorCode ierr; 8984 8985 PetscFunctionBegin; 8986 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8987 PetscValidType(mat,1); 8988 PetscCheckFalse(x == b,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 8989 PetscCheckFalse(!mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 8990 if (!mat->rmap->N && !mat->cmap->N) PetscFunctionReturn(0); 8991 8992 PetscCheckFalse(!mat->ops->solves,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 8993 MatCheckPreallocated(mat,1); 8994 ierr = PetscLogEventBegin(MAT_Solves,mat,0,0,0);CHKERRQ(ierr); 8995 ierr = (*mat->ops->solves)(mat,b,x);CHKERRQ(ierr); 8996 ierr = PetscLogEventEnd(MAT_Solves,mat,0,0,0);CHKERRQ(ierr); 8997 PetscFunctionReturn(0); 8998 } 8999 9000 /*@ 9001 MatIsSymmetric - Test whether a matrix is symmetric 9002 9003 Collective on Mat 9004 9005 Input Parameters: 9006 + A - the matrix to test 9007 - tol - difference between value and its transpose less than this amount counts as equal (use 0.0 for exact transpose) 9008 9009 Output Parameters: 9010 . flg - the result 9011 9012 Notes: 9013 For real numbers MatIsSymmetric() and MatIsHermitian() return identical results 9014 9015 Level: intermediate 9016 9017 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetricKnown() 9018 @*/ 9019 PetscErrorCode MatIsSymmetric(Mat A,PetscReal tol,PetscBool *flg) 9020 { 9021 PetscErrorCode ierr; 9022 9023 PetscFunctionBegin; 9024 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 9025 PetscValidBoolPointer(flg,3); 9026 9027 if (!A->symmetric_set) { 9028 if (!A->ops->issymmetric) { 9029 MatType mattype; 9030 ierr = MatGetType(A,&mattype);CHKERRQ(ierr); 9031 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type %s does not support checking for symmetric",mattype); 9032 } 9033 ierr = (*A->ops->issymmetric)(A,tol,flg);CHKERRQ(ierr); 9034 if (!tol) { 9035 ierr = MatSetOption(A,MAT_SYMMETRIC,*flg);CHKERRQ(ierr); 9036 } 9037 } else if (A->symmetric) { 9038 *flg = PETSC_TRUE; 9039 } else if (!tol) { 9040 *flg = PETSC_FALSE; 9041 } else { 9042 if (!A->ops->issymmetric) { 9043 MatType mattype; 9044 ierr = MatGetType(A,&mattype);CHKERRQ(ierr); 9045 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type %s does not support checking for symmetric",mattype); 9046 } 9047 ierr = (*A->ops->issymmetric)(A,tol,flg);CHKERRQ(ierr); 9048 } 9049 PetscFunctionReturn(0); 9050 } 9051 9052 /*@ 9053 MatIsHermitian - Test whether a matrix is Hermitian 9054 9055 Collective on Mat 9056 9057 Input Parameters: 9058 + A - the matrix to test 9059 - tol - difference between value and its transpose less than this amount counts as equal (use 0.0 for exact Hermitian) 9060 9061 Output Parameters: 9062 . flg - the result 9063 9064 Level: intermediate 9065 9066 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), 9067 MatIsSymmetricKnown(), MatIsSymmetric() 9068 @*/ 9069 PetscErrorCode MatIsHermitian(Mat A,PetscReal tol,PetscBool *flg) 9070 { 9071 PetscErrorCode ierr; 9072 9073 PetscFunctionBegin; 9074 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 9075 PetscValidBoolPointer(flg,3); 9076 9077 if (!A->hermitian_set) { 9078 if (!A->ops->ishermitian) { 9079 MatType mattype; 9080 ierr = MatGetType(A,&mattype);CHKERRQ(ierr); 9081 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type %s does not support checking for hermitian",mattype); 9082 } 9083 ierr = (*A->ops->ishermitian)(A,tol,flg);CHKERRQ(ierr); 9084 if (!tol) { 9085 ierr = MatSetOption(A,MAT_HERMITIAN,*flg);CHKERRQ(ierr); 9086 } 9087 } else if (A->hermitian) { 9088 *flg = PETSC_TRUE; 9089 } else if (!tol) { 9090 *flg = PETSC_FALSE; 9091 } else { 9092 if (!A->ops->ishermitian) { 9093 MatType mattype; 9094 ierr = MatGetType(A,&mattype);CHKERRQ(ierr); 9095 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type %s does not support checking for hermitian",mattype); 9096 } 9097 ierr = (*A->ops->ishermitian)(A,tol,flg);CHKERRQ(ierr); 9098 } 9099 PetscFunctionReturn(0); 9100 } 9101 9102 /*@ 9103 MatIsSymmetricKnown - Checks the flag on the matrix to see if it is symmetric. 9104 9105 Not Collective 9106 9107 Input Parameter: 9108 . A - the matrix to check 9109 9110 Output Parameters: 9111 + set - if the symmetric flag is set (this tells you if the next flag is valid) 9112 - flg - the result 9113 9114 Level: advanced 9115 9116 Note: Does not check the matrix values directly, so this may return unknown (set = PETSC_FALSE). Use MatIsSymmetric() 9117 if you want it explicitly checked 9118 9119 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetric() 9120 @*/ 9121 PetscErrorCode MatIsSymmetricKnown(Mat A,PetscBool *set,PetscBool *flg) 9122 { 9123 PetscFunctionBegin; 9124 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 9125 PetscValidPointer(set,2); 9126 PetscValidBoolPointer(flg,3); 9127 if (A->symmetric_set) { 9128 *set = PETSC_TRUE; 9129 *flg = A->symmetric; 9130 } else { 9131 *set = PETSC_FALSE; 9132 } 9133 PetscFunctionReturn(0); 9134 } 9135 9136 /*@ 9137 MatIsHermitianKnown - Checks the flag on the matrix to see if it is hermitian. 9138 9139 Not Collective 9140 9141 Input Parameter: 9142 . A - the matrix to check 9143 9144 Output Parameters: 9145 + set - if the hermitian flag is set (this tells you if the next flag is valid) 9146 - flg - the result 9147 9148 Level: advanced 9149 9150 Note: Does not check the matrix values directly, so this may return unknown (set = PETSC_FALSE). Use MatIsHermitian() 9151 if you want it explicitly checked 9152 9153 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetric() 9154 @*/ 9155 PetscErrorCode MatIsHermitianKnown(Mat A,PetscBool *set,PetscBool *flg) 9156 { 9157 PetscFunctionBegin; 9158 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 9159 PetscValidPointer(set,2); 9160 PetscValidBoolPointer(flg,3); 9161 if (A->hermitian_set) { 9162 *set = PETSC_TRUE; 9163 *flg = A->hermitian; 9164 } else { 9165 *set = PETSC_FALSE; 9166 } 9167 PetscFunctionReturn(0); 9168 } 9169 9170 /*@ 9171 MatIsStructurallySymmetric - Test whether a matrix is structurally symmetric 9172 9173 Collective on Mat 9174 9175 Input Parameter: 9176 . A - the matrix to test 9177 9178 Output Parameters: 9179 . flg - the result 9180 9181 Level: intermediate 9182 9183 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsSymmetric(), MatSetOption() 9184 @*/ 9185 PetscErrorCode MatIsStructurallySymmetric(Mat A,PetscBool *flg) 9186 { 9187 PetscErrorCode ierr; 9188 9189 PetscFunctionBegin; 9190 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 9191 PetscValidBoolPointer(flg,2); 9192 if (!A->structurally_symmetric_set) { 9193 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); 9194 ierr = (*A->ops->isstructurallysymmetric)(A,flg);CHKERRQ(ierr); 9195 ierr = MatSetOption(A,MAT_STRUCTURALLY_SYMMETRIC,*flg);CHKERRQ(ierr); 9196 } else *flg = A->structurally_symmetric; 9197 PetscFunctionReturn(0); 9198 } 9199 9200 /*@ 9201 MatStashGetInfo - Gets how many values are currently in the matrix stash, i.e. need 9202 to be communicated to other processors during the MatAssemblyBegin/End() process 9203 9204 Not collective 9205 9206 Input Parameter: 9207 . vec - the vector 9208 9209 Output Parameters: 9210 + nstash - the size of the stash 9211 . reallocs - the number of additional mallocs incurred. 9212 . bnstash - the size of the block stash 9213 - breallocs - the number of additional mallocs incurred.in the block stash 9214 9215 Level: advanced 9216 9217 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), Mat, MatStashSetInitialSize() 9218 9219 @*/ 9220 PetscErrorCode MatStashGetInfo(Mat mat,PetscInt *nstash,PetscInt *reallocs,PetscInt *bnstash,PetscInt *breallocs) 9221 { 9222 PetscErrorCode ierr; 9223 9224 PetscFunctionBegin; 9225 ierr = MatStashGetInfo_Private(&mat->stash,nstash,reallocs);CHKERRQ(ierr); 9226 ierr = MatStashGetInfo_Private(&mat->bstash,bnstash,breallocs);CHKERRQ(ierr); 9227 PetscFunctionReturn(0); 9228 } 9229 9230 /*@C 9231 MatCreateVecs - Get vector(s) compatible with the matrix, i.e. with the same 9232 parallel layout 9233 9234 Collective on Mat 9235 9236 Input Parameter: 9237 . mat - the matrix 9238 9239 Output Parameters: 9240 + right - (optional) vector that the matrix can be multiplied against 9241 - left - (optional) vector that the matrix vector product can be stored in 9242 9243 Notes: 9244 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(). 9245 9246 Notes: 9247 These are new vectors which are not owned by the Mat, they should be destroyed in VecDestroy() when no longer needed 9248 9249 Level: advanced 9250 9251 .seealso: MatCreate(), VecDestroy() 9252 @*/ 9253 PetscErrorCode MatCreateVecs(Mat mat,Vec *right,Vec *left) 9254 { 9255 PetscErrorCode ierr; 9256 9257 PetscFunctionBegin; 9258 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 9259 PetscValidType(mat,1); 9260 if (mat->ops->getvecs) { 9261 ierr = (*mat->ops->getvecs)(mat,right,left);CHKERRQ(ierr); 9262 } else { 9263 PetscInt rbs,cbs; 9264 ierr = MatGetBlockSizes(mat,&rbs,&cbs);CHKERRQ(ierr); 9265 if (right) { 9266 PetscCheckFalse(mat->cmap->n < 0,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"PetscLayout for columns not yet setup"); 9267 ierr = VecCreate(PetscObjectComm((PetscObject)mat),right);CHKERRQ(ierr); 9268 ierr = VecSetSizes(*right,mat->cmap->n,PETSC_DETERMINE);CHKERRQ(ierr); 9269 ierr = VecSetBlockSize(*right,cbs);CHKERRQ(ierr); 9270 ierr = VecSetType(*right,mat->defaultvectype);CHKERRQ(ierr); 9271 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 9272 if (mat->boundtocpu && mat->bindingpropagates) { 9273 ierr = VecSetBindingPropagates(*right,PETSC_TRUE);CHKERRQ(ierr); 9274 ierr = VecBindToCPU(*right,PETSC_TRUE);CHKERRQ(ierr); 9275 } 9276 #endif 9277 ierr = PetscLayoutReference(mat->cmap,&(*right)->map);CHKERRQ(ierr); 9278 } 9279 if (left) { 9280 PetscCheckFalse(mat->rmap->n < 0,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"PetscLayout for rows not yet setup"); 9281 ierr = VecCreate(PetscObjectComm((PetscObject)mat),left);CHKERRQ(ierr); 9282 ierr = VecSetSizes(*left,mat->rmap->n,PETSC_DETERMINE);CHKERRQ(ierr); 9283 ierr = VecSetBlockSize(*left,rbs);CHKERRQ(ierr); 9284 ierr = VecSetType(*left,mat->defaultvectype);CHKERRQ(ierr); 9285 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 9286 if (mat->boundtocpu && mat->bindingpropagates) { 9287 ierr = VecSetBindingPropagates(*left,PETSC_TRUE);CHKERRQ(ierr); 9288 ierr = VecBindToCPU(*left,PETSC_TRUE);CHKERRQ(ierr); 9289 } 9290 #endif 9291 ierr = PetscLayoutReference(mat->rmap,&(*left)->map);CHKERRQ(ierr); 9292 } 9293 } 9294 PetscFunctionReturn(0); 9295 } 9296 9297 /*@C 9298 MatFactorInfoInitialize - Initializes a MatFactorInfo data structure 9299 with default values. 9300 9301 Not Collective 9302 9303 Input Parameters: 9304 . info - the MatFactorInfo data structure 9305 9306 Notes: 9307 The solvers are generally used through the KSP and PC objects, for example 9308 PCLU, PCILU, PCCHOLESKY, PCICC 9309 9310 Level: developer 9311 9312 .seealso: MatFactorInfo 9313 9314 Developer Note: fortran interface is not autogenerated as the f90 9315 interface definition cannot be generated correctly [due to MatFactorInfo] 9316 9317 @*/ 9318 9319 PetscErrorCode MatFactorInfoInitialize(MatFactorInfo *info) 9320 { 9321 PetscErrorCode ierr; 9322 9323 PetscFunctionBegin; 9324 ierr = PetscMemzero(info,sizeof(MatFactorInfo));CHKERRQ(ierr); 9325 PetscFunctionReturn(0); 9326 } 9327 9328 /*@ 9329 MatFactorSetSchurIS - Set indices corresponding to the Schur complement you wish to have computed 9330 9331 Collective on Mat 9332 9333 Input Parameters: 9334 + mat - the factored matrix 9335 - is - the index set defining the Schur indices (0-based) 9336 9337 Notes: 9338 Call MatFactorSolveSchurComplement() or MatFactorSolveSchurComplementTranspose() after this call to solve a Schur complement system. 9339 9340 You can call MatFactorGetSchurComplement() or MatFactorCreateSchurComplement() after this call. 9341 9342 Level: developer 9343 9344 .seealso: MatGetFactor(), MatFactorGetSchurComplement(), MatFactorRestoreSchurComplement(), MatFactorCreateSchurComplement(), MatFactorSolveSchurComplement(), 9345 MatFactorSolveSchurComplementTranspose(), MatFactorSolveSchurComplement() 9346 9347 @*/ 9348 PetscErrorCode MatFactorSetSchurIS(Mat mat,IS is) 9349 { 9350 PetscErrorCode ierr,(*f)(Mat,IS); 9351 9352 PetscFunctionBegin; 9353 PetscValidType(mat,1); 9354 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 9355 PetscValidType(is,2); 9356 PetscValidHeaderSpecific(is,IS_CLASSID,2); 9357 PetscCheckSameComm(mat,1,is,2); 9358 PetscCheckFalse(!mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Only for factored matrix"); 9359 ierr = PetscObjectQueryFunction((PetscObject)mat,"MatFactorSetSchurIS_C",&f);CHKERRQ(ierr); 9360 PetscCheckFalse(!f,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"The selected MatSolverType does not support Schur complement computation. You should use MATSOLVERMUMPS or MATSOLVERMKL_PARDISO"); 9361 ierr = MatDestroy(&mat->schur);CHKERRQ(ierr); 9362 ierr = (*f)(mat,is);CHKERRQ(ierr); 9363 PetscCheckFalse(!mat->schur,PetscObjectComm((PetscObject)mat),PETSC_ERR_PLIB,"Schur complement has not been created"); 9364 PetscFunctionReturn(0); 9365 } 9366 9367 /*@ 9368 MatFactorCreateSchurComplement - Create a Schur complement matrix object using Schur data computed during the factorization step 9369 9370 Logically Collective on Mat 9371 9372 Input Parameters: 9373 + F - the factored matrix obtained by calling MatGetFactor() from PETSc-MUMPS interface 9374 . S - location where to return the Schur complement, can be NULL 9375 - status - the status of the Schur complement matrix, can be NULL 9376 9377 Notes: 9378 You must call MatFactorSetSchurIS() before calling this routine. 9379 9380 The routine provides a copy of the Schur matrix stored within the solver data structures. 9381 The caller must destroy the object when it is no longer needed. 9382 If MatFactorInvertSchurComplement() has been called, the routine gets back the inverse. 9383 9384 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) 9385 9386 Developer Notes: 9387 The reason this routine exists is because the representation of the Schur complement within the factor matrix may be different than a standard PETSc 9388 matrix representation and we normally do not want to use the time or memory to make a copy as a regular PETSc matrix. 9389 9390 See MatCreateSchurComplement() or MatGetSchurComplement() for ways to create virtual or approximate Schur complements. 9391 9392 Level: advanced 9393 9394 References: 9395 9396 .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorGetSchurComplement(), MatFactorSchurStatus 9397 @*/ 9398 PetscErrorCode MatFactorCreateSchurComplement(Mat F,Mat* S,MatFactorSchurStatus* status) 9399 { 9400 PetscErrorCode ierr; 9401 9402 PetscFunctionBegin; 9403 PetscValidHeaderSpecific(F,MAT_CLASSID,1); 9404 if (S) PetscValidPointer(S,2); 9405 if (status) PetscValidPointer(status,3); 9406 if (S) { 9407 PetscErrorCode (*f)(Mat,Mat*); 9408 9409 ierr = PetscObjectQueryFunction((PetscObject)F,"MatFactorCreateSchurComplement_C",&f);CHKERRQ(ierr); 9410 if (f) { 9411 ierr = (*f)(F,S);CHKERRQ(ierr); 9412 } else { 9413 ierr = MatDuplicate(F->schur,MAT_COPY_VALUES,S);CHKERRQ(ierr); 9414 } 9415 } 9416 if (status) *status = F->schur_status; 9417 PetscFunctionReturn(0); 9418 } 9419 9420 /*@ 9421 MatFactorGetSchurComplement - Gets access to a Schur complement matrix using the current Schur data within a factored matrix 9422 9423 Logically Collective on Mat 9424 9425 Input Parameters: 9426 + F - the factored matrix obtained by calling MatGetFactor() 9427 . *S - location where to return the Schur complement, can be NULL 9428 - status - the status of the Schur complement matrix, can be NULL 9429 9430 Notes: 9431 You must call MatFactorSetSchurIS() before calling this routine. 9432 9433 Schur complement mode is currently implemented for sequential matrices. 9434 The routine returns a the Schur Complement stored within the data strutures of the solver. 9435 If MatFactorInvertSchurComplement() has previously been called, the returned matrix is actually the inverse of the Schur complement. 9436 The returned matrix should not be destroyed; the caller should call MatFactorRestoreSchurComplement() when the object is no longer needed. 9437 9438 Use MatFactorCreateSchurComplement() to create a copy of the Schur complement matrix that is within a factored matrix 9439 9440 See MatCreateSchurComplement() or MatGetSchurComplement() for ways to create virtual or approximate Schur complements. 9441 9442 Level: advanced 9443 9444 References: 9445 9446 .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorRestoreSchurComplement(), MatFactorCreateSchurComplement(), MatFactorSchurStatus 9447 @*/ 9448 PetscErrorCode MatFactorGetSchurComplement(Mat F,Mat* S,MatFactorSchurStatus* status) 9449 { 9450 PetscFunctionBegin; 9451 PetscValidHeaderSpecific(F,MAT_CLASSID,1); 9452 if (S) PetscValidPointer(S,2); 9453 if (status) PetscValidPointer(status,3); 9454 if (S) *S = F->schur; 9455 if (status) *status = F->schur_status; 9456 PetscFunctionReturn(0); 9457 } 9458 9459 /*@ 9460 MatFactorRestoreSchurComplement - Restore the Schur complement matrix object obtained from a call to MatFactorGetSchurComplement 9461 9462 Logically Collective on Mat 9463 9464 Input Parameters: 9465 + F - the factored matrix obtained by calling MatGetFactor() 9466 . *S - location where the Schur complement is stored 9467 - status - the status of the Schur complement matrix (see MatFactorSchurStatus) 9468 9469 Notes: 9470 9471 Level: advanced 9472 9473 References: 9474 9475 .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorRestoreSchurComplement(), MatFactorCreateSchurComplement(), MatFactorSchurStatus 9476 @*/ 9477 PetscErrorCode MatFactorRestoreSchurComplement(Mat F,Mat* S,MatFactorSchurStatus status) 9478 { 9479 PetscErrorCode ierr; 9480 9481 PetscFunctionBegin; 9482 PetscValidHeaderSpecific(F,MAT_CLASSID,1); 9483 if (S) { 9484 PetscValidHeaderSpecific(*S,MAT_CLASSID,2); 9485 *S = NULL; 9486 } 9487 F->schur_status = status; 9488 ierr = MatFactorUpdateSchurStatus_Private(F);CHKERRQ(ierr); 9489 PetscFunctionReturn(0); 9490 } 9491 9492 /*@ 9493 MatFactorSolveSchurComplementTranspose - Solve the transpose of the Schur complement system computed during the factorization step 9494 9495 Logically Collective on Mat 9496 9497 Input Parameters: 9498 + F - the factored matrix obtained by calling MatGetFactor() 9499 . rhs - location where the right hand side of the Schur complement system is stored 9500 - sol - location where the solution of the Schur complement system has to be returned 9501 9502 Notes: 9503 The sizes of the vectors should match the size of the Schur complement 9504 9505 Must be called after MatFactorSetSchurIS() 9506 9507 Level: advanced 9508 9509 References: 9510 9511 .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorSolveSchurComplement() 9512 @*/ 9513 PetscErrorCode MatFactorSolveSchurComplementTranspose(Mat F, Vec rhs, Vec sol) 9514 { 9515 PetscErrorCode ierr; 9516 9517 PetscFunctionBegin; 9518 PetscValidType(F,1); 9519 PetscValidType(rhs,2); 9520 PetscValidType(sol,3); 9521 PetscValidHeaderSpecific(F,MAT_CLASSID,1); 9522 PetscValidHeaderSpecific(rhs,VEC_CLASSID,2); 9523 PetscValidHeaderSpecific(sol,VEC_CLASSID,3); 9524 PetscCheckSameComm(F,1,rhs,2); 9525 PetscCheckSameComm(F,1,sol,3); 9526 ierr = MatFactorFactorizeSchurComplement(F);CHKERRQ(ierr); 9527 switch (F->schur_status) { 9528 case MAT_FACTOR_SCHUR_FACTORED: 9529 ierr = MatSolveTranspose(F->schur,rhs,sol);CHKERRQ(ierr); 9530 break; 9531 case MAT_FACTOR_SCHUR_INVERTED: 9532 ierr = MatMultTranspose(F->schur,rhs,sol);CHKERRQ(ierr); 9533 break; 9534 default: 9535 SETERRQ(PetscObjectComm((PetscObject)F),PETSC_ERR_SUP,"Unhandled MatFactorSchurStatus %d",F->schur_status); 9536 } 9537 PetscFunctionReturn(0); 9538 } 9539 9540 /*@ 9541 MatFactorSolveSchurComplement - Solve the Schur complement system computed during the factorization step 9542 9543 Logically Collective on Mat 9544 9545 Input Parameters: 9546 + F - the factored matrix obtained by calling MatGetFactor() 9547 . rhs - location where the right hand side of the Schur complement system is stored 9548 - sol - location where the solution of the Schur complement system has to be returned 9549 9550 Notes: 9551 The sizes of the vectors should match the size of the Schur complement 9552 9553 Must be called after MatFactorSetSchurIS() 9554 9555 Level: advanced 9556 9557 References: 9558 9559 .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorSolveSchurComplementTranspose() 9560 @*/ 9561 PetscErrorCode MatFactorSolveSchurComplement(Mat F, Vec rhs, Vec sol) 9562 { 9563 PetscErrorCode ierr; 9564 9565 PetscFunctionBegin; 9566 PetscValidType(F,1); 9567 PetscValidType(rhs,2); 9568 PetscValidType(sol,3); 9569 PetscValidHeaderSpecific(F,MAT_CLASSID,1); 9570 PetscValidHeaderSpecific(rhs,VEC_CLASSID,2); 9571 PetscValidHeaderSpecific(sol,VEC_CLASSID,3); 9572 PetscCheckSameComm(F,1,rhs,2); 9573 PetscCheckSameComm(F,1,sol,3); 9574 ierr = MatFactorFactorizeSchurComplement(F);CHKERRQ(ierr); 9575 switch (F->schur_status) { 9576 case MAT_FACTOR_SCHUR_FACTORED: 9577 ierr = MatSolve(F->schur,rhs,sol);CHKERRQ(ierr); 9578 break; 9579 case MAT_FACTOR_SCHUR_INVERTED: 9580 ierr = MatMult(F->schur,rhs,sol);CHKERRQ(ierr); 9581 break; 9582 default: 9583 SETERRQ(PetscObjectComm((PetscObject)F),PETSC_ERR_SUP,"Unhandled MatFactorSchurStatus %d",F->schur_status); 9584 } 9585 PetscFunctionReturn(0); 9586 } 9587 9588 /*@ 9589 MatFactorInvertSchurComplement - Invert the Schur complement matrix computed during the factorization step 9590 9591 Logically Collective on Mat 9592 9593 Input Parameters: 9594 . F - the factored matrix obtained by calling MatGetFactor() 9595 9596 Notes: 9597 Must be called after MatFactorSetSchurIS(). 9598 9599 Call MatFactorGetSchurComplement() or MatFactorCreateSchurComplement() AFTER this call to actually compute the inverse and get access to it. 9600 9601 Level: advanced 9602 9603 References: 9604 9605 .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorGetSchurComplement(), MatFactorCreateSchurComplement() 9606 @*/ 9607 PetscErrorCode MatFactorInvertSchurComplement(Mat F) 9608 { 9609 PetscErrorCode ierr; 9610 9611 PetscFunctionBegin; 9612 PetscValidType(F,1); 9613 PetscValidHeaderSpecific(F,MAT_CLASSID,1); 9614 if (F->schur_status == MAT_FACTOR_SCHUR_INVERTED) PetscFunctionReturn(0); 9615 ierr = MatFactorFactorizeSchurComplement(F);CHKERRQ(ierr); 9616 ierr = MatFactorInvertSchurComplement_Private(F);CHKERRQ(ierr); 9617 F->schur_status = MAT_FACTOR_SCHUR_INVERTED; 9618 PetscFunctionReturn(0); 9619 } 9620 9621 /*@ 9622 MatFactorFactorizeSchurComplement - Factorize the Schur complement matrix computed during the factorization step 9623 9624 Logically Collective on Mat 9625 9626 Input Parameters: 9627 . F - the factored matrix obtained by calling MatGetFactor() 9628 9629 Notes: 9630 Must be called after MatFactorSetSchurIS(). 9631 9632 Level: advanced 9633 9634 References: 9635 9636 .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorInvertSchurComplement() 9637 @*/ 9638 PetscErrorCode MatFactorFactorizeSchurComplement(Mat F) 9639 { 9640 PetscErrorCode ierr; 9641 9642 PetscFunctionBegin; 9643 PetscValidType(F,1); 9644 PetscValidHeaderSpecific(F,MAT_CLASSID,1); 9645 if (F->schur_status == MAT_FACTOR_SCHUR_INVERTED || F->schur_status == MAT_FACTOR_SCHUR_FACTORED) PetscFunctionReturn(0); 9646 ierr = MatFactorFactorizeSchurComplement_Private(F);CHKERRQ(ierr); 9647 F->schur_status = MAT_FACTOR_SCHUR_FACTORED; 9648 PetscFunctionReturn(0); 9649 } 9650 9651 /*@ 9652 MatPtAP - Creates the matrix product C = P^T * A * P 9653 9654 Neighbor-wise Collective on Mat 9655 9656 Input Parameters: 9657 + A - the matrix 9658 . P - the projection matrix 9659 . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 9660 - fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(P)), use PETSC_DEFAULT if you do not have a good estimate 9661 if the result is a dense matrix this is irrelevant 9662 9663 Output Parameters: 9664 . C - the product matrix 9665 9666 Notes: 9667 C will be created and must be destroyed by the user with MatDestroy(). 9668 9669 For matrix types without special implementation the function fallbacks to MatMatMult() followed by MatTransposeMatMult(). 9670 9671 Level: intermediate 9672 9673 .seealso: MatMatMult(), MatRARt() 9674 @*/ 9675 PetscErrorCode MatPtAP(Mat A,Mat P,MatReuse scall,PetscReal fill,Mat *C) 9676 { 9677 PetscErrorCode ierr; 9678 9679 PetscFunctionBegin; 9680 if (scall == MAT_REUSE_MATRIX) MatCheckProduct(*C,5); 9681 PetscCheckFalse(scall == MAT_INPLACE_MATRIX,PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported"); 9682 9683 if (scall == MAT_INITIAL_MATRIX) { 9684 ierr = MatProductCreate(A,P,NULL,C);CHKERRQ(ierr); 9685 ierr = MatProductSetType(*C,MATPRODUCT_PtAP);CHKERRQ(ierr); 9686 ierr = MatProductSetAlgorithm(*C,"default");CHKERRQ(ierr); 9687 ierr = MatProductSetFill(*C,fill);CHKERRQ(ierr); 9688 9689 (*C)->product->api_user = PETSC_TRUE; 9690 ierr = MatProductSetFromOptions(*C);CHKERRQ(ierr); 9691 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); 9692 ierr = MatProductSymbolic(*C);CHKERRQ(ierr); 9693 } else { /* scall == MAT_REUSE_MATRIX */ 9694 ierr = MatProductReplaceMats(A,P,NULL,*C);CHKERRQ(ierr); 9695 } 9696 9697 ierr = MatProductNumeric(*C);CHKERRQ(ierr); 9698 if (A->symmetric_set && A->symmetric) { 9699 ierr = MatSetOption(*C,MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr); 9700 } 9701 PetscFunctionReturn(0); 9702 } 9703 9704 /*@ 9705 MatRARt - Creates the matrix product C = R * A * R^T 9706 9707 Neighbor-wise Collective on Mat 9708 9709 Input Parameters: 9710 + A - the matrix 9711 . R - the projection matrix 9712 . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 9713 - fill - expected fill as ratio of nnz(C)/nnz(A), use PETSC_DEFAULT if you do not have a good estimate 9714 if the result is a dense matrix this is irrelevant 9715 9716 Output Parameters: 9717 . C - the product matrix 9718 9719 Notes: 9720 C will be created and must be destroyed by the user with MatDestroy(). 9721 9722 This routine is currently only implemented for pairs of AIJ matrices and classes 9723 which inherit from AIJ. Due to PETSc sparse matrix block row distribution among processes, 9724 parallel MatRARt is implemented via explicit transpose of R, which could be very expensive. 9725 We recommend using MatPtAP(). 9726 9727 Level: intermediate 9728 9729 .seealso: MatMatMult(), MatPtAP() 9730 @*/ 9731 PetscErrorCode MatRARt(Mat A,Mat R,MatReuse scall,PetscReal fill,Mat *C) 9732 { 9733 PetscErrorCode ierr; 9734 9735 PetscFunctionBegin; 9736 if (scall == MAT_REUSE_MATRIX) MatCheckProduct(*C,5); 9737 PetscCheckFalse(scall == MAT_INPLACE_MATRIX,PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported"); 9738 9739 if (scall == MAT_INITIAL_MATRIX) { 9740 ierr = MatProductCreate(A,R,NULL,C);CHKERRQ(ierr); 9741 ierr = MatProductSetType(*C,MATPRODUCT_RARt);CHKERRQ(ierr); 9742 ierr = MatProductSetAlgorithm(*C,"default");CHKERRQ(ierr); 9743 ierr = MatProductSetFill(*C,fill);CHKERRQ(ierr); 9744 9745 (*C)->product->api_user = PETSC_TRUE; 9746 ierr = MatProductSetFromOptions(*C);CHKERRQ(ierr); 9747 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); 9748 ierr = MatProductSymbolic(*C);CHKERRQ(ierr); 9749 } else { /* scall == MAT_REUSE_MATRIX */ 9750 ierr = MatProductReplaceMats(A,R,NULL,*C);CHKERRQ(ierr); 9751 } 9752 9753 ierr = MatProductNumeric(*C);CHKERRQ(ierr); 9754 if (A->symmetric_set && A->symmetric) { 9755 ierr = MatSetOption(*C,MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr); 9756 } 9757 PetscFunctionReturn(0); 9758 } 9759 9760 static PetscErrorCode MatProduct_Private(Mat A,Mat B,MatReuse scall,PetscReal fill,MatProductType ptype, Mat *C) 9761 { 9762 PetscErrorCode ierr; 9763 9764 PetscFunctionBegin; 9765 PetscCheckFalse(scall == MAT_INPLACE_MATRIX,PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported"); 9766 9767 if (scall == MAT_INITIAL_MATRIX) { 9768 ierr = PetscInfo(A,"Calling MatProduct API with MAT_INITIAL_MATRIX and product type %s\n",MatProductTypes[ptype]);CHKERRQ(ierr); 9769 ierr = MatProductCreate(A,B,NULL,C);CHKERRQ(ierr); 9770 ierr = MatProductSetType(*C,ptype);CHKERRQ(ierr); 9771 ierr = MatProductSetAlgorithm(*C,MATPRODUCTALGORITHMDEFAULT);CHKERRQ(ierr); 9772 ierr = MatProductSetFill(*C,fill);CHKERRQ(ierr); 9773 9774 (*C)->product->api_user = PETSC_TRUE; 9775 ierr = MatProductSetFromOptions(*C);CHKERRQ(ierr); 9776 ierr = MatProductSymbolic(*C);CHKERRQ(ierr); 9777 } else { /* scall == MAT_REUSE_MATRIX */ 9778 Mat_Product *product = (*C)->product; 9779 PetscBool isdense; 9780 9781 ierr = PetscObjectBaseTypeCompareAny((PetscObject)(*C),&isdense,MATSEQDENSE,MATMPIDENSE,"");CHKERRQ(ierr); 9782 if (isdense && product && product->type != ptype) { 9783 ierr = MatProductClear(*C);CHKERRQ(ierr); 9784 product = NULL; 9785 } 9786 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); 9787 if (!product) { /* user provide the dense matrix *C without calling MatProductCreate() or reusing it from previous calls */ 9788 if (isdense) { 9789 ierr = MatProductCreate_Private(A,B,NULL,*C);CHKERRQ(ierr); 9790 product = (*C)->product; 9791 product->fill = fill; 9792 product->api_user = PETSC_TRUE; 9793 product->clear = PETSC_TRUE; 9794 9795 ierr = MatProductSetType(*C,ptype);CHKERRQ(ierr); 9796 ierr = MatProductSetFromOptions(*C);CHKERRQ(ierr); 9797 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); 9798 ierr = MatProductSymbolic(*C);CHKERRQ(ierr); 9799 } else SETERRQ(PetscObjectComm((PetscObject)(*C)),PETSC_ERR_SUP,"Call MatProductCreate() first"); 9800 } else { /* user may change input matrices A or B when REUSE */ 9801 ierr = MatProductReplaceMats(A,B,NULL,*C);CHKERRQ(ierr); 9802 } 9803 } 9804 ierr = MatProductNumeric(*C);CHKERRQ(ierr); 9805 PetscFunctionReturn(0); 9806 } 9807 9808 /*@ 9809 MatMatMult - Performs Matrix-Matrix Multiplication C=A*B. 9810 9811 Neighbor-wise Collective on Mat 9812 9813 Input Parameters: 9814 + A - the left matrix 9815 . B - the right matrix 9816 . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 9817 - fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if you do not have a good estimate 9818 if the result is a dense matrix this is irrelevant 9819 9820 Output Parameters: 9821 . C - the product matrix 9822 9823 Notes: 9824 Unless scall is MAT_REUSE_MATRIX C will be created. 9825 9826 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 9827 call to this function with MAT_INITIAL_MATRIX. 9828 9829 To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value actually needed. 9830 9831 If you have many matrices with the same non-zero structure to multiply, you should use MatProductCreate()/MatProductSymbolic()/MatProductReplaceMats(), and call MatProductNumeric() repeatedly. 9832 9833 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. 9834 9835 Example of Usage: 9836 .vb 9837 MatProductCreate(A,B,NULL,&C); 9838 MatProductSetType(C,MATPRODUCT_AB); 9839 MatProductSymbolic(C); 9840 MatProductNumeric(C); // compute C=A * B 9841 MatProductReplaceMats(A1,B1,NULL,C); // compute C=A1 * B1 9842 MatProductNumeric(C); 9843 MatProductReplaceMats(A2,NULL,NULL,C); // compute C=A2 * B1 9844 MatProductNumeric(C); 9845 .ve 9846 9847 Level: intermediate 9848 9849 .seealso: MatTransposeMatMult(), MatMatTransposeMult(), MatPtAP(), MatProductCreate(), MatProductSymbolic(), MatProductReplaceMats(), MatProductNumeric() 9850 @*/ 9851 PetscErrorCode MatMatMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C) 9852 { 9853 PetscErrorCode ierr; 9854 9855 PetscFunctionBegin; 9856 ierr = MatProduct_Private(A,B,scall,fill,MATPRODUCT_AB,C);CHKERRQ(ierr); 9857 PetscFunctionReturn(0); 9858 } 9859 9860 /*@ 9861 MatMatTransposeMult - Performs Matrix-Matrix Multiplication C=A*B^T. 9862 9863 Neighbor-wise Collective on Mat 9864 9865 Input Parameters: 9866 + A - the left matrix 9867 . B - the right matrix 9868 . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 9869 - fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if not known 9870 9871 Output Parameters: 9872 . C - the product matrix 9873 9874 Notes: 9875 C will be created if MAT_INITIAL_MATRIX and must be destroyed by the user with MatDestroy(). 9876 9877 MAT_REUSE_MATRIX can only be used if the matrices A and B have the same nonzero pattern as in the previous call 9878 9879 To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value 9880 actually needed. 9881 9882 This routine is currently only implemented for pairs of SeqAIJ matrices, for the SeqDense class, 9883 and for pairs of MPIDense matrices. 9884 9885 Options Database Keys: 9886 . -matmattransmult_mpidense_mpidense_via {allgatherv,cyclic} - Choose between algorthims for MPIDense matrices: the 9887 first redundantly copies the transposed B matrix on each process and requiers O(log P) communication complexity; 9888 the second never stores more than one portion of the B matrix at a time by requires O(P) communication complexity. 9889 9890 Level: intermediate 9891 9892 .seealso: MatMatMult(), MatTransposeMatMult() MatPtAP() 9893 @*/ 9894 PetscErrorCode MatMatTransposeMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C) 9895 { 9896 PetscErrorCode ierr; 9897 9898 PetscFunctionBegin; 9899 ierr = MatProduct_Private(A,B,scall,fill,MATPRODUCT_ABt,C);CHKERRQ(ierr); 9900 PetscFunctionReturn(0); 9901 } 9902 9903 /*@ 9904 MatTransposeMatMult - Performs Matrix-Matrix Multiplication C=A^T*B. 9905 9906 Neighbor-wise Collective on Mat 9907 9908 Input Parameters: 9909 + A - the left matrix 9910 . B - the right matrix 9911 . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 9912 - fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if not known 9913 9914 Output Parameters: 9915 . C - the product matrix 9916 9917 Notes: 9918 C will be created if MAT_INITIAL_MATRIX and must be destroyed by the user with MatDestroy(). 9919 9920 MAT_REUSE_MATRIX can only be used if the matrices A and B have the same nonzero pattern as in the previous call. 9921 9922 To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value 9923 actually needed. 9924 9925 This routine is currently implemented for pairs of AIJ matrices and pairs of SeqDense matrices and classes 9926 which inherit from SeqAIJ. C will be of same type as the input matrices. 9927 9928 Level: intermediate 9929 9930 .seealso: MatMatMult(), MatMatTransposeMult(), MatPtAP() 9931 @*/ 9932 PetscErrorCode MatTransposeMatMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C) 9933 { 9934 PetscErrorCode ierr; 9935 9936 PetscFunctionBegin; 9937 ierr = MatProduct_Private(A,B,scall,fill,MATPRODUCT_AtB,C);CHKERRQ(ierr); 9938 PetscFunctionReturn(0); 9939 } 9940 9941 /*@ 9942 MatMatMatMult - Performs Matrix-Matrix-Matrix Multiplication D=A*B*C. 9943 9944 Neighbor-wise Collective on Mat 9945 9946 Input Parameters: 9947 + A - the left matrix 9948 . B - the middle matrix 9949 . C - the right matrix 9950 . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 9951 - 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 9952 if the result is a dense matrix this is irrelevant 9953 9954 Output Parameters: 9955 . D - the product matrix 9956 9957 Notes: 9958 Unless scall is MAT_REUSE_MATRIX D will be created. 9959 9960 MAT_REUSE_MATRIX can only be used if the matrices A, B and C have the same nonzero pattern as in the previous call 9961 9962 To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value 9963 actually needed. 9964 9965 If you have many matrices with the same non-zero structure to multiply, you 9966 should use MAT_REUSE_MATRIX in all calls but the first or 9967 9968 Level: intermediate 9969 9970 .seealso: MatMatMult, MatPtAP() 9971 @*/ 9972 PetscErrorCode MatMatMatMult(Mat A,Mat B,Mat C,MatReuse scall,PetscReal fill,Mat *D) 9973 { 9974 PetscErrorCode ierr; 9975 9976 PetscFunctionBegin; 9977 if (scall == MAT_REUSE_MATRIX) MatCheckProduct(*D,6); 9978 PetscCheckFalse(scall == MAT_INPLACE_MATRIX,PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported"); 9979 9980 if (scall == MAT_INITIAL_MATRIX) { 9981 ierr = MatProductCreate(A,B,C,D);CHKERRQ(ierr); 9982 ierr = MatProductSetType(*D,MATPRODUCT_ABC);CHKERRQ(ierr); 9983 ierr = MatProductSetAlgorithm(*D,"default");CHKERRQ(ierr); 9984 ierr = MatProductSetFill(*D,fill);CHKERRQ(ierr); 9985 9986 (*D)->product->api_user = PETSC_TRUE; 9987 ierr = MatProductSetFromOptions(*D);CHKERRQ(ierr); 9988 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); 9989 ierr = MatProductSymbolic(*D);CHKERRQ(ierr); 9990 } else { /* user may change input matrices when REUSE */ 9991 ierr = MatProductReplaceMats(A,B,C,*D);CHKERRQ(ierr); 9992 } 9993 ierr = MatProductNumeric(*D);CHKERRQ(ierr); 9994 PetscFunctionReturn(0); 9995 } 9996 9997 /*@ 9998 MatCreateRedundantMatrix - Create redundant matrices and put them into processors of subcommunicators. 9999 10000 Collective on Mat 10001 10002 Input Parameters: 10003 + mat - the matrix 10004 . nsubcomm - the number of subcommunicators (= number of redundant parallel or sequential matrices) 10005 . subcomm - MPI communicator split from the communicator where mat resides in (or MPI_COMM_NULL if nsubcomm is used) 10006 - reuse - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 10007 10008 Output Parameter: 10009 . matredundant - redundant matrix 10010 10011 Notes: 10012 MAT_REUSE_MATRIX can only be used when the nonzero structure of the 10013 original matrix has not changed from that last call to MatCreateRedundantMatrix(). 10014 10015 This routine creates the duplicated matrices in subcommunicators; you should NOT create them before 10016 calling it. 10017 10018 Level: advanced 10019 10020 .seealso: MatDestroy() 10021 @*/ 10022 PetscErrorCode MatCreateRedundantMatrix(Mat mat,PetscInt nsubcomm,MPI_Comm subcomm,MatReuse reuse,Mat *matredundant) 10023 { 10024 PetscErrorCode ierr; 10025 MPI_Comm comm; 10026 PetscMPIInt size; 10027 PetscInt mloc_sub,nloc_sub,rstart,rend,M=mat->rmap->N,N=mat->cmap->N,bs=mat->rmap->bs; 10028 Mat_Redundant *redund=NULL; 10029 PetscSubcomm psubcomm=NULL; 10030 MPI_Comm subcomm_in=subcomm; 10031 Mat *matseq; 10032 IS isrow,iscol; 10033 PetscBool newsubcomm=PETSC_FALSE; 10034 10035 PetscFunctionBegin; 10036 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10037 if (nsubcomm && reuse == MAT_REUSE_MATRIX) { 10038 PetscValidPointer(*matredundant,5); 10039 PetscValidHeaderSpecific(*matredundant,MAT_CLASSID,5); 10040 } 10041 10042 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);CHKERRMPI(ierr); 10043 if (size == 1 || nsubcomm == 1) { 10044 if (reuse == MAT_INITIAL_MATRIX) { 10045 ierr = MatDuplicate(mat,MAT_COPY_VALUES,matredundant);CHKERRQ(ierr); 10046 } else { 10047 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"); 10048 ierr = MatCopy(mat,*matredundant,SAME_NONZERO_PATTERN);CHKERRQ(ierr); 10049 } 10050 PetscFunctionReturn(0); 10051 } 10052 10053 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 10054 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 10055 MatCheckPreallocated(mat,1); 10056 10057 ierr = PetscLogEventBegin(MAT_RedundantMat,mat,0,0,0);CHKERRQ(ierr); 10058 if (subcomm_in == MPI_COMM_NULL && reuse == MAT_INITIAL_MATRIX) { /* get subcomm if user does not provide subcomm */ 10059 /* create psubcomm, then get subcomm */ 10060 ierr = PetscObjectGetComm((PetscObject)mat,&comm);CHKERRQ(ierr); 10061 ierr = MPI_Comm_size(comm,&size);CHKERRMPI(ierr); 10062 PetscCheckFalse(nsubcomm < 1 || nsubcomm > size,PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"nsubcomm must between 1 and %d",size); 10063 10064 ierr = PetscSubcommCreate(comm,&psubcomm);CHKERRQ(ierr); 10065 ierr = PetscSubcommSetNumber(psubcomm,nsubcomm);CHKERRQ(ierr); 10066 ierr = PetscSubcommSetType(psubcomm,PETSC_SUBCOMM_CONTIGUOUS);CHKERRQ(ierr); 10067 ierr = PetscSubcommSetFromOptions(psubcomm);CHKERRQ(ierr); 10068 ierr = PetscCommDuplicate(PetscSubcommChild(psubcomm),&subcomm,NULL);CHKERRQ(ierr); 10069 newsubcomm = PETSC_TRUE; 10070 ierr = PetscSubcommDestroy(&psubcomm);CHKERRQ(ierr); 10071 } 10072 10073 /* get isrow, iscol and a local sequential matrix matseq[0] */ 10074 if (reuse == MAT_INITIAL_MATRIX) { 10075 mloc_sub = PETSC_DECIDE; 10076 nloc_sub = PETSC_DECIDE; 10077 if (bs < 1) { 10078 ierr = PetscSplitOwnership(subcomm,&mloc_sub,&M);CHKERRQ(ierr); 10079 ierr = PetscSplitOwnership(subcomm,&nloc_sub,&N);CHKERRQ(ierr); 10080 } else { 10081 ierr = PetscSplitOwnershipBlock(subcomm,bs,&mloc_sub,&M);CHKERRQ(ierr); 10082 ierr = PetscSplitOwnershipBlock(subcomm,bs,&nloc_sub,&N);CHKERRQ(ierr); 10083 } 10084 ierr = MPI_Scan(&mloc_sub,&rend,1,MPIU_INT,MPI_SUM,subcomm);CHKERRMPI(ierr); 10085 rstart = rend - mloc_sub; 10086 ierr = ISCreateStride(PETSC_COMM_SELF,mloc_sub,rstart,1,&isrow);CHKERRQ(ierr); 10087 ierr = ISCreateStride(PETSC_COMM_SELF,N,0,1,&iscol);CHKERRQ(ierr); 10088 } else { /* reuse == MAT_REUSE_MATRIX */ 10089 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"); 10090 /* retrieve subcomm */ 10091 ierr = PetscObjectGetComm((PetscObject)(*matredundant),&subcomm);CHKERRQ(ierr); 10092 redund = (*matredundant)->redundant; 10093 isrow = redund->isrow; 10094 iscol = redund->iscol; 10095 matseq = redund->matseq; 10096 } 10097 ierr = MatCreateSubMatrices(mat,1,&isrow,&iscol,reuse,&matseq);CHKERRQ(ierr); 10098 10099 /* get matredundant over subcomm */ 10100 if (reuse == MAT_INITIAL_MATRIX) { 10101 ierr = MatCreateMPIMatConcatenateSeqMat(subcomm,matseq[0],nloc_sub,reuse,matredundant);CHKERRQ(ierr); 10102 10103 /* create a supporting struct and attach it to C for reuse */ 10104 ierr = PetscNewLog(*matredundant,&redund);CHKERRQ(ierr); 10105 (*matredundant)->redundant = redund; 10106 redund->isrow = isrow; 10107 redund->iscol = iscol; 10108 redund->matseq = matseq; 10109 if (newsubcomm) { 10110 redund->subcomm = subcomm; 10111 } else { 10112 redund->subcomm = MPI_COMM_NULL; 10113 } 10114 } else { 10115 ierr = MatCreateMPIMatConcatenateSeqMat(subcomm,matseq[0],PETSC_DECIDE,reuse,matredundant);CHKERRQ(ierr); 10116 } 10117 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 10118 if (matseq[0]->boundtocpu && matseq[0]->bindingpropagates) { 10119 ierr = MatBindToCPU(*matredundant,PETSC_TRUE);CHKERRQ(ierr); 10120 ierr = MatSetBindingPropagates(*matredundant,PETSC_TRUE);CHKERRQ(ierr); 10121 } 10122 #endif 10123 ierr = PetscLogEventEnd(MAT_RedundantMat,mat,0,0,0);CHKERRQ(ierr); 10124 PetscFunctionReturn(0); 10125 } 10126 10127 /*@C 10128 MatGetMultiProcBlock - Create multiple [bjacobi] 'parallel submatrices' from 10129 a given 'mat' object. Each submatrix can span multiple procs. 10130 10131 Collective on Mat 10132 10133 Input Parameters: 10134 + mat - the matrix 10135 . subcomm - the subcommunicator obtained by com_split(comm) 10136 - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 10137 10138 Output Parameter: 10139 . subMat - 'parallel submatrices each spans a given subcomm 10140 10141 Notes: 10142 The submatrix partition across processors is dictated by 'subComm' a 10143 communicator obtained by com_split(comm). The comm_split 10144 is not restriced to be grouped with consecutive original ranks. 10145 10146 Due the comm_split() usage, the parallel layout of the submatrices 10147 map directly to the layout of the original matrix [wrt the local 10148 row,col partitioning]. So the original 'DiagonalMat' naturally maps 10149 into the 'DiagonalMat' of the subMat, hence it is used directly from 10150 the subMat. However the offDiagMat looses some columns - and this is 10151 reconstructed with MatSetValues() 10152 10153 Level: advanced 10154 10155 .seealso: MatCreateSubMatrices() 10156 @*/ 10157 PetscErrorCode MatGetMultiProcBlock(Mat mat, MPI_Comm subComm, MatReuse scall,Mat *subMat) 10158 { 10159 PetscErrorCode ierr; 10160 PetscMPIInt commsize,subCommSize; 10161 10162 PetscFunctionBegin; 10163 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&commsize);CHKERRMPI(ierr); 10164 ierr = MPI_Comm_size(subComm,&subCommSize);CHKERRMPI(ierr); 10165 PetscCheckFalse(subCommSize > commsize,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"CommSize %d < SubCommZize %d",commsize,subCommSize); 10166 10167 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"); 10168 ierr = PetscLogEventBegin(MAT_GetMultiProcBlock,mat,0,0,0);CHKERRQ(ierr); 10169 ierr = (*mat->ops->getmultiprocblock)(mat,subComm,scall,subMat);CHKERRQ(ierr); 10170 ierr = PetscLogEventEnd(MAT_GetMultiProcBlock,mat,0,0,0);CHKERRQ(ierr); 10171 PetscFunctionReturn(0); 10172 } 10173 10174 /*@ 10175 MatGetLocalSubMatrix - Gets a reference to a submatrix specified in local numbering 10176 10177 Not Collective 10178 10179 Input Parameters: 10180 + mat - matrix to extract local submatrix from 10181 . isrow - local row indices for submatrix 10182 - iscol - local column indices for submatrix 10183 10184 Output Parameter: 10185 . submat - the submatrix 10186 10187 Level: intermediate 10188 10189 Notes: 10190 The submat should be returned with MatRestoreLocalSubMatrix(). 10191 10192 Depending on the format of mat, the returned submat may not implement MatMult(). Its communicator may be 10193 the same as mat, it may be PETSC_COMM_SELF, or some other subcomm of mat's. 10194 10195 The submat always implements MatSetValuesLocal(). If isrow and iscol have the same block size, then 10196 MatSetValuesBlockedLocal() will also be implemented. 10197 10198 The mat must have had a ISLocalToGlobalMapping provided to it with MatSetLocalToGlobalMapping(). Note that 10199 matrices obtained with DMCreateMatrix() generally already have the local to global mapping provided. 10200 10201 .seealso: MatRestoreLocalSubMatrix(), MatCreateLocalRef(), MatSetLocalToGlobalMapping() 10202 @*/ 10203 PetscErrorCode MatGetLocalSubMatrix(Mat mat,IS isrow,IS iscol,Mat *submat) 10204 { 10205 PetscErrorCode ierr; 10206 10207 PetscFunctionBegin; 10208 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10209 PetscValidHeaderSpecific(isrow,IS_CLASSID,2); 10210 PetscValidHeaderSpecific(iscol,IS_CLASSID,3); 10211 PetscCheckSameComm(isrow,2,iscol,3); 10212 PetscValidPointer(submat,4); 10213 PetscCheckFalse(!mat->rmap->mapping,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Matrix must have local to global mapping provided before this call"); 10214 10215 if (mat->ops->getlocalsubmatrix) { 10216 ierr = (*mat->ops->getlocalsubmatrix)(mat,isrow,iscol,submat);CHKERRQ(ierr); 10217 } else { 10218 ierr = MatCreateLocalRef(mat,isrow,iscol,submat);CHKERRQ(ierr); 10219 } 10220 PetscFunctionReturn(0); 10221 } 10222 10223 /*@ 10224 MatRestoreLocalSubMatrix - Restores a reference to a submatrix specified in local numbering 10225 10226 Not Collective 10227 10228 Input Parameters: 10229 + mat - matrix to extract local submatrix from 10230 . isrow - local row indices for submatrix 10231 . iscol - local column indices for submatrix 10232 - submat - the submatrix 10233 10234 Level: intermediate 10235 10236 .seealso: MatGetLocalSubMatrix() 10237 @*/ 10238 PetscErrorCode MatRestoreLocalSubMatrix(Mat mat,IS isrow,IS iscol,Mat *submat) 10239 { 10240 PetscErrorCode ierr; 10241 10242 PetscFunctionBegin; 10243 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10244 PetscValidHeaderSpecific(isrow,IS_CLASSID,2); 10245 PetscValidHeaderSpecific(iscol,IS_CLASSID,3); 10246 PetscCheckSameComm(isrow,2,iscol,3); 10247 PetscValidPointer(submat,4); 10248 if (*submat) { 10249 PetscValidHeaderSpecific(*submat,MAT_CLASSID,4); 10250 } 10251 10252 if (mat->ops->restorelocalsubmatrix) { 10253 ierr = (*mat->ops->restorelocalsubmatrix)(mat,isrow,iscol,submat);CHKERRQ(ierr); 10254 } else { 10255 ierr = MatDestroy(submat);CHKERRQ(ierr); 10256 } 10257 *submat = NULL; 10258 PetscFunctionReturn(0); 10259 } 10260 10261 /* --------------------------------------------------------*/ 10262 /*@ 10263 MatFindZeroDiagonals - Finds all the rows of a matrix that have zero or no diagonal entry in the matrix 10264 10265 Collective on Mat 10266 10267 Input Parameter: 10268 . mat - the matrix 10269 10270 Output Parameter: 10271 . is - if any rows have zero diagonals this contains the list of them 10272 10273 Level: developer 10274 10275 .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd() 10276 @*/ 10277 PetscErrorCode MatFindZeroDiagonals(Mat mat,IS *is) 10278 { 10279 PetscErrorCode ierr; 10280 10281 PetscFunctionBegin; 10282 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10283 PetscValidType(mat,1); 10284 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 10285 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 10286 10287 if (!mat->ops->findzerodiagonals) { 10288 Vec diag; 10289 const PetscScalar *a; 10290 PetscInt *rows; 10291 PetscInt rStart, rEnd, r, nrow = 0; 10292 10293 ierr = MatCreateVecs(mat, &diag, NULL);CHKERRQ(ierr); 10294 ierr = MatGetDiagonal(mat, diag);CHKERRQ(ierr); 10295 ierr = MatGetOwnershipRange(mat, &rStart, &rEnd);CHKERRQ(ierr); 10296 ierr = VecGetArrayRead(diag, &a);CHKERRQ(ierr); 10297 for (r = 0; r < rEnd-rStart; ++r) if (a[r] == 0.0) ++nrow; 10298 ierr = PetscMalloc1(nrow, &rows);CHKERRQ(ierr); 10299 nrow = 0; 10300 for (r = 0; r < rEnd-rStart; ++r) if (a[r] == 0.0) rows[nrow++] = r+rStart; 10301 ierr = VecRestoreArrayRead(diag, &a);CHKERRQ(ierr); 10302 ierr = VecDestroy(&diag);CHKERRQ(ierr); 10303 ierr = ISCreateGeneral(PetscObjectComm((PetscObject) mat), nrow, rows, PETSC_OWN_POINTER, is);CHKERRQ(ierr); 10304 } else { 10305 ierr = (*mat->ops->findzerodiagonals)(mat, is);CHKERRQ(ierr); 10306 } 10307 PetscFunctionReturn(0); 10308 } 10309 10310 /*@ 10311 MatFindOffBlockDiagonalEntries - Finds all the rows of a matrix that have entries outside of the main diagonal block (defined by the matrix block size) 10312 10313 Collective on Mat 10314 10315 Input Parameter: 10316 . mat - the matrix 10317 10318 Output Parameter: 10319 . is - contains the list of rows with off block diagonal entries 10320 10321 Level: developer 10322 10323 .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd() 10324 @*/ 10325 PetscErrorCode MatFindOffBlockDiagonalEntries(Mat mat,IS *is) 10326 { 10327 PetscErrorCode ierr; 10328 10329 PetscFunctionBegin; 10330 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10331 PetscValidType(mat,1); 10332 PetscCheckFalse(!mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 10333 PetscCheckFalse(mat->factortype,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 10334 10335 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); 10336 ierr = (*mat->ops->findoffblockdiagonalentries)(mat,is);CHKERRQ(ierr); 10337 PetscFunctionReturn(0); 10338 } 10339 10340 /*@C 10341 MatInvertBlockDiagonal - Inverts the block diagonal entries. 10342 10343 Collective on Mat 10344 10345 Input Parameters: 10346 . mat - the matrix 10347 10348 Output Parameters: 10349 . values - the block inverses in column major order (FORTRAN-like) 10350 10351 Note: 10352 The size of the blocks is determined by the block size of the matrix. 10353 10354 Fortran Note: 10355 This routine is not available from Fortran. 10356 10357 Level: advanced 10358 10359 .seealso: MatInvertBockDiagonalMat() 10360 @*/ 10361 PetscErrorCode MatInvertBlockDiagonal(Mat mat,const PetscScalar **values) 10362 { 10363 PetscErrorCode ierr; 10364 10365 PetscFunctionBegin; 10366 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10367 PetscCheckFalse(!mat->assembled,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 10368 PetscCheckFalse(mat->factortype,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 10369 PetscCheckFalse(!mat->ops->invertblockdiagonal,PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported for type %s",((PetscObject)mat)->type_name); 10370 ierr = (*mat->ops->invertblockdiagonal)(mat,values);CHKERRQ(ierr); 10371 PetscFunctionReturn(0); 10372 } 10373 10374 /*@C 10375 MatInvertVariableBlockDiagonal - Inverts the point block diagonal entries. 10376 10377 Collective on Mat 10378 10379 Input Parameters: 10380 + mat - the matrix 10381 . nblocks - the number of blocks 10382 - bsizes - the size of each block 10383 10384 Output Parameters: 10385 . values - the block inverses in column major order (FORTRAN-like) 10386 10387 Note: 10388 This routine is not available from Fortran. 10389 10390 Level: advanced 10391 10392 .seealso: MatInvertBockDiagonal() 10393 @*/ 10394 PetscErrorCode MatInvertVariableBlockDiagonal(Mat mat,PetscInt nblocks,const PetscInt *bsizes,PetscScalar *values) 10395 { 10396 PetscErrorCode ierr; 10397 10398 PetscFunctionBegin; 10399 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10400 PetscCheckFalse(!mat->assembled,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 10401 PetscCheckFalse(mat->factortype,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 10402 PetscCheckFalse(!mat->ops->invertvariableblockdiagonal,PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported for type %s",((PetscObject)mat)->type_name); 10403 ierr = (*mat->ops->invertvariableblockdiagonal)(mat,nblocks,bsizes,values);CHKERRQ(ierr); 10404 PetscFunctionReturn(0); 10405 } 10406 10407 /*@ 10408 MatInvertBlockDiagonalMat - set matrix C to be the inverted block diagonal of matrix A 10409 10410 Collective on Mat 10411 10412 Input Parameters: 10413 . A - the matrix 10414 10415 Output Parameters: 10416 . C - matrix with inverted block diagonal of A. This matrix should be created and may have its type set. 10417 10418 Notes: the blocksize of the matrix is used to determine the blocks on the diagonal of C 10419 10420 Level: advanced 10421 10422 .seealso: MatInvertBockDiagonal() 10423 @*/ 10424 PetscErrorCode MatInvertBlockDiagonalMat(Mat A,Mat C) 10425 { 10426 PetscErrorCode ierr; 10427 const PetscScalar *vals; 10428 PetscInt *dnnz; 10429 PetscInt M,N,m,n,rstart,rend,bs,i,j; 10430 10431 PetscFunctionBegin; 10432 ierr = MatInvertBlockDiagonal(A,&vals);CHKERRQ(ierr); 10433 ierr = MatGetBlockSize(A,&bs);CHKERRQ(ierr); 10434 ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr); 10435 ierr = MatGetLocalSize(A,&m,&n);CHKERRQ(ierr); 10436 ierr = MatSetSizes(C,m,n,M,N);CHKERRQ(ierr); 10437 ierr = MatSetBlockSize(C,bs);CHKERRQ(ierr); 10438 ierr = PetscMalloc1(m/bs,&dnnz);CHKERRQ(ierr); 10439 for (j = 0; j < m/bs; j++) dnnz[j] = 1; 10440 ierr = MatXAIJSetPreallocation(C,bs,dnnz,NULL,NULL,NULL);CHKERRQ(ierr); 10441 ierr = PetscFree(dnnz);CHKERRQ(ierr); 10442 ierr = MatGetOwnershipRange(C,&rstart,&rend);CHKERRQ(ierr); 10443 ierr = MatSetOption(C,MAT_ROW_ORIENTED,PETSC_FALSE);CHKERRQ(ierr); 10444 for (i = rstart/bs; i < rend/bs; i++) { 10445 ierr = MatSetValuesBlocked(C,1,&i,1,&i,&vals[(i-rstart/bs)*bs*bs],INSERT_VALUES);CHKERRQ(ierr); 10446 } 10447 ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 10448 ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 10449 ierr = MatSetOption(C,MAT_ROW_ORIENTED,PETSC_TRUE);CHKERRQ(ierr); 10450 PetscFunctionReturn(0); 10451 } 10452 10453 /*@C 10454 MatTransposeColoringDestroy - Destroys a coloring context for matrix product C=A*B^T that was created 10455 via MatTransposeColoringCreate(). 10456 10457 Collective on MatTransposeColoring 10458 10459 Input Parameter: 10460 . c - coloring context 10461 10462 Level: intermediate 10463 10464 .seealso: MatTransposeColoringCreate() 10465 @*/ 10466 PetscErrorCode MatTransposeColoringDestroy(MatTransposeColoring *c) 10467 { 10468 PetscErrorCode ierr; 10469 MatTransposeColoring matcolor=*c; 10470 10471 PetscFunctionBegin; 10472 if (!matcolor) PetscFunctionReturn(0); 10473 if (--((PetscObject)matcolor)->refct > 0) {matcolor = NULL; PetscFunctionReturn(0);} 10474 10475 ierr = PetscFree3(matcolor->ncolumns,matcolor->nrows,matcolor->colorforrow);CHKERRQ(ierr); 10476 ierr = PetscFree(matcolor->rows);CHKERRQ(ierr); 10477 ierr = PetscFree(matcolor->den2sp);CHKERRQ(ierr); 10478 ierr = PetscFree(matcolor->colorforcol);CHKERRQ(ierr); 10479 ierr = PetscFree(matcolor->columns);CHKERRQ(ierr); 10480 if (matcolor->brows>0) { 10481 ierr = PetscFree(matcolor->lstart);CHKERRQ(ierr); 10482 } 10483 ierr = PetscHeaderDestroy(c);CHKERRQ(ierr); 10484 PetscFunctionReturn(0); 10485 } 10486 10487 /*@C 10488 MatTransColoringApplySpToDen - Given a symbolic matrix product C=A*B^T for which 10489 a MatTransposeColoring context has been created, computes a dense B^T by Apply 10490 MatTransposeColoring to sparse B. 10491 10492 Collective on MatTransposeColoring 10493 10494 Input Parameters: 10495 + B - sparse matrix B 10496 . Btdense - symbolic dense matrix B^T 10497 - coloring - coloring context created with MatTransposeColoringCreate() 10498 10499 Output Parameter: 10500 . Btdense - dense matrix B^T 10501 10502 Level: advanced 10503 10504 Notes: 10505 These are used internally for some implementations of MatRARt() 10506 10507 .seealso: MatTransposeColoringCreate(), MatTransposeColoringDestroy(), MatTransColoringApplyDenToSp() 10508 10509 @*/ 10510 PetscErrorCode MatTransColoringApplySpToDen(MatTransposeColoring coloring,Mat B,Mat Btdense) 10511 { 10512 PetscErrorCode ierr; 10513 10514 PetscFunctionBegin; 10515 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 10516 PetscValidHeaderSpecific(Btdense,MAT_CLASSID,3); 10517 PetscValidHeaderSpecific(coloring,MAT_TRANSPOSECOLORING_CLASSID,1); 10518 10519 PetscCheckFalse(!B->ops->transcoloringapplysptoden,PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported for this matrix type %s",((PetscObject)B)->type_name); 10520 ierr = (B->ops->transcoloringapplysptoden)(coloring,B,Btdense);CHKERRQ(ierr); 10521 PetscFunctionReturn(0); 10522 } 10523 10524 /*@C 10525 MatTransColoringApplyDenToSp - Given a symbolic matrix product Csp=A*B^T for which 10526 a MatTransposeColoring context has been created and a dense matrix Cden=A*Btdense 10527 in which Btdens is obtained from MatTransColoringApplySpToDen(), recover sparse matrix 10528 Csp from Cden. 10529 10530 Collective on MatTransposeColoring 10531 10532 Input Parameters: 10533 + coloring - coloring context created with MatTransposeColoringCreate() 10534 - Cden - matrix product of a sparse matrix and a dense matrix Btdense 10535 10536 Output Parameter: 10537 . Csp - sparse matrix 10538 10539 Level: advanced 10540 10541 Notes: 10542 These are used internally for some implementations of MatRARt() 10543 10544 .seealso: MatTransposeColoringCreate(), MatTransposeColoringDestroy(), MatTransColoringApplySpToDen() 10545 10546 @*/ 10547 PetscErrorCode MatTransColoringApplyDenToSp(MatTransposeColoring matcoloring,Mat Cden,Mat Csp) 10548 { 10549 PetscErrorCode ierr; 10550 10551 PetscFunctionBegin; 10552 PetscValidHeaderSpecific(matcoloring,MAT_TRANSPOSECOLORING_CLASSID,1); 10553 PetscValidHeaderSpecific(Cden,MAT_CLASSID,2); 10554 PetscValidHeaderSpecific(Csp,MAT_CLASSID,3); 10555 10556 PetscCheckFalse(!Csp->ops->transcoloringapplydentosp,PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported for this matrix type %s",((PetscObject)Csp)->type_name); 10557 ierr = (Csp->ops->transcoloringapplydentosp)(matcoloring,Cden,Csp);CHKERRQ(ierr); 10558 ierr = MatAssemblyBegin(Csp,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 10559 ierr = MatAssemblyEnd(Csp,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 10560 PetscFunctionReturn(0); 10561 } 10562 10563 /*@C 10564 MatTransposeColoringCreate - Creates a matrix coloring context for matrix product C=A*B^T. 10565 10566 Collective on Mat 10567 10568 Input Parameters: 10569 + mat - the matrix product C 10570 - iscoloring - the coloring of the matrix; usually obtained with MatColoringCreate() or DMCreateColoring() 10571 10572 Output Parameter: 10573 . color - the new coloring context 10574 10575 Level: intermediate 10576 10577 .seealso: MatTransposeColoringDestroy(), MatTransColoringApplySpToDen(), 10578 MatTransColoringApplyDenToSp() 10579 @*/ 10580 PetscErrorCode MatTransposeColoringCreate(Mat mat,ISColoring iscoloring,MatTransposeColoring *color) 10581 { 10582 MatTransposeColoring c; 10583 MPI_Comm comm; 10584 PetscErrorCode ierr; 10585 10586 PetscFunctionBegin; 10587 ierr = PetscLogEventBegin(MAT_TransposeColoringCreate,mat,0,0,0);CHKERRQ(ierr); 10588 ierr = PetscObjectGetComm((PetscObject)mat,&comm);CHKERRQ(ierr); 10589 ierr = PetscHeaderCreate(c,MAT_TRANSPOSECOLORING_CLASSID,"MatTransposeColoring","Matrix product C=A*B^T via coloring","Mat",comm,MatTransposeColoringDestroy,NULL);CHKERRQ(ierr); 10590 10591 c->ctype = iscoloring->ctype; 10592 if (mat->ops->transposecoloringcreate) { 10593 ierr = (*mat->ops->transposecoloringcreate)(mat,iscoloring,c);CHKERRQ(ierr); 10594 } else SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Code not yet written for matrix type %s",((PetscObject)mat)->type_name); 10595 10596 *color = c; 10597 ierr = PetscLogEventEnd(MAT_TransposeColoringCreate,mat,0,0,0);CHKERRQ(ierr); 10598 PetscFunctionReturn(0); 10599 } 10600 10601 /*@ 10602 MatGetNonzeroState - Returns a 64 bit integer representing the current state of nonzeros in the matrix. If the 10603 matrix has had no new nonzero locations added to the matrix since the previous call then the value will be the 10604 same, otherwise it will be larger 10605 10606 Not Collective 10607 10608 Input Parameter: 10609 . A - the matrix 10610 10611 Output Parameter: 10612 . state - the current state 10613 10614 Notes: 10615 You can only compare states from two different calls to the SAME matrix, you cannot compare calls between 10616 different matrices 10617 10618 Level: intermediate 10619 10620 @*/ 10621 PetscErrorCode MatGetNonzeroState(Mat mat,PetscObjectState *state) 10622 { 10623 PetscFunctionBegin; 10624 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10625 *state = mat->nonzerostate; 10626 PetscFunctionReturn(0); 10627 } 10628 10629 /*@ 10630 MatCreateMPIMatConcatenateSeqMat - Creates a single large PETSc matrix by concatenating sequential 10631 matrices from each processor 10632 10633 Collective 10634 10635 Input Parameters: 10636 + comm - the communicators the parallel matrix will live on 10637 . seqmat - the input sequential matrices 10638 . n - number of local columns (or PETSC_DECIDE) 10639 - reuse - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 10640 10641 Output Parameter: 10642 . mpimat - the parallel matrix generated 10643 10644 Level: advanced 10645 10646 Notes: 10647 The number of columns of the matrix in EACH processor MUST be the same. 10648 10649 @*/ 10650 PetscErrorCode MatCreateMPIMatConcatenateSeqMat(MPI_Comm comm,Mat seqmat,PetscInt n,MatReuse reuse,Mat *mpimat) 10651 { 10652 PetscErrorCode ierr; 10653 10654 PetscFunctionBegin; 10655 PetscCheckFalse(!seqmat->ops->creatempimatconcatenateseqmat,PetscObjectComm((PetscObject)seqmat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)seqmat)->type_name); 10656 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"); 10657 10658 ierr = PetscLogEventBegin(MAT_Merge,seqmat,0,0,0);CHKERRQ(ierr); 10659 ierr = (*seqmat->ops->creatempimatconcatenateseqmat)(comm,seqmat,n,reuse,mpimat);CHKERRQ(ierr); 10660 ierr = PetscLogEventEnd(MAT_Merge,seqmat,0,0,0);CHKERRQ(ierr); 10661 PetscFunctionReturn(0); 10662 } 10663 10664 /*@ 10665 MatSubdomainsCreateCoalesce - Creates index subdomains by coalescing adjacent 10666 ranks' ownership ranges. 10667 10668 Collective on A 10669 10670 Input Parameters: 10671 + A - the matrix to create subdomains from 10672 - N - requested number of subdomains 10673 10674 Output Parameters: 10675 + n - number of subdomains resulting on this rank 10676 - iss - IS list with indices of subdomains on this rank 10677 10678 Level: advanced 10679 10680 Notes: 10681 number of subdomains must be smaller than the communicator size 10682 @*/ 10683 PetscErrorCode MatSubdomainsCreateCoalesce(Mat A,PetscInt N,PetscInt *n,IS *iss[]) 10684 { 10685 MPI_Comm comm,subcomm; 10686 PetscMPIInt size,rank,color; 10687 PetscInt rstart,rend,k; 10688 PetscErrorCode ierr; 10689 10690 PetscFunctionBegin; 10691 ierr = PetscObjectGetComm((PetscObject)A,&comm);CHKERRQ(ierr); 10692 ierr = MPI_Comm_size(comm,&size);CHKERRMPI(ierr); 10693 ierr = MPI_Comm_rank(comm,&rank);CHKERRMPI(ierr); 10694 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); 10695 *n = 1; 10696 k = ((PetscInt)size)/N + ((PetscInt)size%N>0); /* There are up to k ranks to a color */ 10697 color = rank/k; 10698 ierr = MPI_Comm_split(comm,color,rank,&subcomm);CHKERRMPI(ierr); 10699 ierr = PetscMalloc1(1,iss);CHKERRQ(ierr); 10700 ierr = MatGetOwnershipRange(A,&rstart,&rend);CHKERRQ(ierr); 10701 ierr = ISCreateStride(subcomm,rend-rstart,rstart,1,iss[0]);CHKERRQ(ierr); 10702 ierr = MPI_Comm_free(&subcomm);CHKERRMPI(ierr); 10703 PetscFunctionReturn(0); 10704 } 10705 10706 /*@ 10707 MatGalerkin - Constructs the coarse grid problem via Galerkin projection. 10708 10709 If the interpolation and restriction operators are the same, uses MatPtAP. 10710 If they are not the same, use MatMatMatMult. 10711 10712 Once the coarse grid problem is constructed, correct for interpolation operators 10713 that are not of full rank, which can legitimately happen in the case of non-nested 10714 geometric multigrid. 10715 10716 Input Parameters: 10717 + restrct - restriction operator 10718 . dA - fine grid matrix 10719 . interpolate - interpolation operator 10720 . reuse - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 10721 - fill - expected fill, use PETSC_DEFAULT if you do not have a good estimate 10722 10723 Output Parameters: 10724 . A - the Galerkin coarse matrix 10725 10726 Options Database Key: 10727 . -pc_mg_galerkin <both,pmat,mat,none> 10728 10729 Level: developer 10730 10731 .seealso: MatPtAP(), MatMatMatMult() 10732 @*/ 10733 PetscErrorCode MatGalerkin(Mat restrct, Mat dA, Mat interpolate, MatReuse reuse, PetscReal fill, Mat *A) 10734 { 10735 PetscErrorCode ierr; 10736 IS zerorows; 10737 Vec diag; 10738 10739 PetscFunctionBegin; 10740 PetscCheckFalse(reuse == MAT_INPLACE_MATRIX,PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported"); 10741 /* Construct the coarse grid matrix */ 10742 if (interpolate == restrct) { 10743 ierr = MatPtAP(dA,interpolate,reuse,fill,A);CHKERRQ(ierr); 10744 } else { 10745 ierr = MatMatMatMult(restrct,dA,interpolate,reuse,fill,A);CHKERRQ(ierr); 10746 } 10747 10748 /* If the interpolation matrix is not of full rank, A will have zero rows. 10749 This can legitimately happen in the case of non-nested geometric multigrid. 10750 In that event, we set the rows of the matrix to the rows of the identity, 10751 ignoring the equations (as the RHS will also be zero). */ 10752 10753 ierr = MatFindZeroRows(*A, &zerorows);CHKERRQ(ierr); 10754 10755 if (zerorows != NULL) { /* if there are any zero rows */ 10756 ierr = MatCreateVecs(*A, &diag, NULL);CHKERRQ(ierr); 10757 ierr = MatGetDiagonal(*A, diag);CHKERRQ(ierr); 10758 ierr = VecISSet(diag, zerorows, 1.0);CHKERRQ(ierr); 10759 ierr = MatDiagonalSet(*A, diag, INSERT_VALUES);CHKERRQ(ierr); 10760 ierr = VecDestroy(&diag);CHKERRQ(ierr); 10761 ierr = ISDestroy(&zerorows);CHKERRQ(ierr); 10762 } 10763 PetscFunctionReturn(0); 10764 } 10765 10766 /*@C 10767 MatSetOperation - Allows user to set a matrix operation for any matrix type 10768 10769 Logically Collective on Mat 10770 10771 Input Parameters: 10772 + mat - the matrix 10773 . op - the name of the operation 10774 - f - the function that provides the operation 10775 10776 Level: developer 10777 10778 Usage: 10779 $ extern PetscErrorCode usermult(Mat,Vec,Vec); 10780 $ ierr = MatCreateXXX(comm,...&A); 10781 $ ierr = MatSetOperation(A,MATOP_MULT,(void(*)(void))usermult); 10782 10783 Notes: 10784 See the file include/petscmat.h for a complete list of matrix 10785 operations, which all have the form MATOP_<OPERATION>, where 10786 <OPERATION> is the name (in all capital letters) of the 10787 user interface routine (e.g., MatMult() -> MATOP_MULT). 10788 10789 All user-provided functions (except for MATOP_DESTROY) should have the same calling 10790 sequence as the usual matrix interface routines, since they 10791 are intended to be accessed via the usual matrix interface 10792 routines, e.g., 10793 $ MatMult(Mat,Vec,Vec) -> usermult(Mat,Vec,Vec) 10794 10795 In particular each function MUST return an error code of 0 on success and 10796 nonzero on failure. 10797 10798 This routine is distinct from MatShellSetOperation() in that it can be called on any matrix type. 10799 10800 .seealso: MatGetOperation(), MatCreateShell(), MatShellSetContext(), MatShellSetOperation() 10801 @*/ 10802 PetscErrorCode MatSetOperation(Mat mat,MatOperation op,void (*f)(void)) 10803 { 10804 PetscFunctionBegin; 10805 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10806 if (op == MATOP_VIEW && !mat->ops->viewnative && f != (void (*)(void))(mat->ops->view)) { 10807 mat->ops->viewnative = mat->ops->view; 10808 } 10809 (((void(**)(void))mat->ops)[op]) = f; 10810 PetscFunctionReturn(0); 10811 } 10812 10813 /*@C 10814 MatGetOperation - Gets a matrix operation for any matrix type. 10815 10816 Not Collective 10817 10818 Input Parameters: 10819 + mat - the matrix 10820 - op - the name of the operation 10821 10822 Output Parameter: 10823 . f - the function that provides the operation 10824 10825 Level: developer 10826 10827 Usage: 10828 $ PetscErrorCode (*usermult)(Mat,Vec,Vec); 10829 $ ierr = MatGetOperation(A,MATOP_MULT,(void(**)(void))&usermult); 10830 10831 Notes: 10832 See the file include/petscmat.h for a complete list of matrix 10833 operations, which all have the form MATOP_<OPERATION>, where 10834 <OPERATION> is the name (in all capital letters) of the 10835 user interface routine (e.g., MatMult() -> MATOP_MULT). 10836 10837 This routine is distinct from MatShellGetOperation() in that it can be called on any matrix type. 10838 10839 .seealso: MatSetOperation(), MatCreateShell(), MatShellGetContext(), MatShellGetOperation() 10840 @*/ 10841 PetscErrorCode MatGetOperation(Mat mat,MatOperation op,void(**f)(void)) 10842 { 10843 PetscFunctionBegin; 10844 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10845 *f = (((void (**)(void))mat->ops)[op]); 10846 PetscFunctionReturn(0); 10847 } 10848 10849 /*@ 10850 MatHasOperation - Determines whether the given matrix supports the particular 10851 operation. 10852 10853 Not Collective 10854 10855 Input Parameters: 10856 + mat - the matrix 10857 - op - the operation, for example, MATOP_GET_DIAGONAL 10858 10859 Output Parameter: 10860 . has - either PETSC_TRUE or PETSC_FALSE 10861 10862 Level: advanced 10863 10864 Notes: 10865 See the file include/petscmat.h for a complete list of matrix 10866 operations, which all have the form MATOP_<OPERATION>, where 10867 <OPERATION> is the name (in all capital letters) of the 10868 user-level routine. E.g., MatNorm() -> MATOP_NORM. 10869 10870 .seealso: MatCreateShell() 10871 @*/ 10872 PetscErrorCode MatHasOperation(Mat mat,MatOperation op,PetscBool *has) 10873 { 10874 PetscErrorCode ierr; 10875 10876 PetscFunctionBegin; 10877 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10878 PetscValidPointer(has,3); 10879 if (mat->ops->hasoperation) { 10880 ierr = (*mat->ops->hasoperation)(mat,op,has);CHKERRQ(ierr); 10881 } else { 10882 if (((void**)mat->ops)[op]) *has = PETSC_TRUE; 10883 else { 10884 *has = PETSC_FALSE; 10885 if (op == MATOP_CREATE_SUBMATRIX) { 10886 PetscMPIInt size; 10887 10888 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);CHKERRMPI(ierr); 10889 if (size == 1) { 10890 ierr = MatHasOperation(mat,MATOP_CREATE_SUBMATRICES,has);CHKERRQ(ierr); 10891 } 10892 } 10893 } 10894 } 10895 PetscFunctionReturn(0); 10896 } 10897 10898 /*@ 10899 MatHasCongruentLayouts - Determines whether the rows and columns layouts 10900 of the matrix are congruent 10901 10902 Collective on mat 10903 10904 Input Parameters: 10905 . mat - the matrix 10906 10907 Output Parameter: 10908 . cong - either PETSC_TRUE or PETSC_FALSE 10909 10910 Level: beginner 10911 10912 Notes: 10913 10914 .seealso: MatCreate(), MatSetSizes() 10915 @*/ 10916 PetscErrorCode MatHasCongruentLayouts(Mat mat,PetscBool *cong) 10917 { 10918 PetscErrorCode ierr; 10919 10920 PetscFunctionBegin; 10921 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10922 PetscValidType(mat,1); 10923 PetscValidPointer(cong,2); 10924 if (!mat->rmap || !mat->cmap) { 10925 *cong = mat->rmap == mat->cmap ? PETSC_TRUE : PETSC_FALSE; 10926 PetscFunctionReturn(0); 10927 } 10928 if (mat->congruentlayouts == PETSC_DECIDE) { /* first time we compare rows and cols layouts */ 10929 ierr = PetscLayoutSetUp(mat->rmap);CHKERRQ(ierr); 10930 ierr = PetscLayoutSetUp(mat->cmap);CHKERRQ(ierr); 10931 ierr = PetscLayoutCompare(mat->rmap,mat->cmap,cong);CHKERRQ(ierr); 10932 if (*cong) mat->congruentlayouts = 1; 10933 else mat->congruentlayouts = 0; 10934 } else *cong = mat->congruentlayouts ? PETSC_TRUE : PETSC_FALSE; 10935 PetscFunctionReturn(0); 10936 } 10937 10938 PetscErrorCode MatSetInf(Mat A) 10939 { 10940 PetscErrorCode ierr; 10941 10942 PetscFunctionBegin; 10943 PetscCheckFalse(!A->ops->setinf,PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"No support for this operation for this matrix type"); 10944 ierr = (*A->ops->setinf)(A);CHKERRQ(ierr); 10945 PetscFunctionReturn(0); 10946 } 10947