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