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