1 /* 2 This is where the abstract matrix operations are defined 3 */ 4 5 #include <petsc/private/matimpl.h> /*I "petscmat.h" I*/ 6 #include <petsc/private/isimpl.h> 7 #include <petsc/private/vecimpl.h> 8 9 /* Logging support */ 10 PetscClassId MAT_CLASSID; 11 PetscClassId MAT_COLORING_CLASSID; 12 PetscClassId MAT_FDCOLORING_CLASSID; 13 PetscClassId MAT_TRANSPOSECOLORING_CLASSID; 14 15 PetscLogEvent MAT_Mult, MAT_Mults, MAT_MultConstrained, MAT_MultAdd, MAT_MultTranspose; 16 PetscLogEvent MAT_MultTransposeConstrained, MAT_MultTransposeAdd, MAT_Solve, MAT_Solves, MAT_SolveAdd, MAT_SolveTranspose, MAT_MatSolve,MAT_MatTrSolve; 17 PetscLogEvent MAT_SolveTransposeAdd, MAT_SOR, MAT_ForwardSolve, MAT_BackwardSolve, MAT_LUFactor, MAT_LUFactorSymbolic; 18 PetscLogEvent MAT_LUFactorNumeric, MAT_CholeskyFactor, MAT_CholeskyFactorSymbolic, MAT_CholeskyFactorNumeric, MAT_ILUFactor; 19 PetscLogEvent MAT_ILUFactorSymbolic, MAT_ICCFactorSymbolic, MAT_Copy, MAT_Convert, MAT_Scale, MAT_AssemblyBegin; 20 PetscLogEvent MAT_QRFactorNumeric, MAT_QRFactorSymbolic, MAT_QRFactor; 21 PetscLogEvent MAT_AssemblyEnd, MAT_SetValues, MAT_GetValues, MAT_GetRow, MAT_GetRowIJ, MAT_CreateSubMats, MAT_GetOrdering, MAT_RedundantMat, MAT_GetSeqNonzeroStructure; 22 PetscLogEvent MAT_IncreaseOverlap, MAT_Partitioning, MAT_PartitioningND, MAT_Coarsen, MAT_ZeroEntries, MAT_Load, MAT_View, MAT_AXPY, MAT_FDColoringCreate; 23 PetscLogEvent MAT_FDColoringSetUp, MAT_FDColoringApply,MAT_Transpose,MAT_FDColoringFunction, MAT_CreateSubMat; 24 PetscLogEvent MAT_TransposeColoringCreate; 25 PetscLogEvent MAT_MatMult, MAT_MatMultSymbolic, MAT_MatMultNumeric; 26 PetscLogEvent MAT_PtAP, MAT_PtAPSymbolic, MAT_PtAPNumeric,MAT_RARt, MAT_RARtSymbolic, MAT_RARtNumeric; 27 PetscLogEvent MAT_MatTransposeMult, MAT_MatTransposeMultSymbolic, MAT_MatTransposeMultNumeric; 28 PetscLogEvent MAT_TransposeMatMult, MAT_TransposeMatMultSymbolic, MAT_TransposeMatMultNumeric; 29 PetscLogEvent MAT_MatMatMult, MAT_MatMatMultSymbolic, MAT_MatMatMultNumeric; 30 PetscLogEvent MAT_MultHermitianTranspose,MAT_MultHermitianTransposeAdd; 31 PetscLogEvent MAT_Getsymtranspose, MAT_Getsymtransreduced, MAT_GetBrowsOfAcols; 32 PetscLogEvent MAT_GetBrowsOfAocols, MAT_Getlocalmat, MAT_Getlocalmatcondensed, MAT_Seqstompi, MAT_Seqstompinum, MAT_Seqstompisym; 33 PetscLogEvent MAT_Applypapt, MAT_Applypapt_numeric, MAT_Applypapt_symbolic, MAT_GetSequentialNonzeroStructure; 34 PetscLogEvent MAT_GetMultiProcBlock; 35 PetscLogEvent MAT_CUSPARSECopyToGPU, MAT_CUSPARSECopyFromGPU, MAT_CUSPARSEGenerateTranspose, MAT_CUSPARSESolveAnalysis; 36 PetscLogEvent MAT_PreallCOO, MAT_SetVCOO; 37 PetscLogEvent MAT_SetValuesBatch; 38 PetscLogEvent MAT_ViennaCLCopyToGPU; 39 PetscLogEvent MAT_DenseCopyToGPU, MAT_DenseCopyFromGPU; 40 PetscLogEvent MAT_Merge,MAT_Residual,MAT_SetRandom; 41 PetscLogEvent MAT_FactorFactS,MAT_FactorInvS; 42 PetscLogEvent MATCOLORING_Apply,MATCOLORING_Comm,MATCOLORING_Local,MATCOLORING_ISCreate,MATCOLORING_SetUp,MATCOLORING_Weights; 43 44 const char *const MatFactorTypes[] = {"NONE","LU","CHOLESKY","ILU","ICC","ILUDT","QR","MatFactorType","MAT_FACTOR_",NULL}; 45 46 /*@ 47 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, 48 for sparse matrices that already have locations it fills the locations with random numbers 49 50 Logically Collective on Mat 51 52 Input Parameters: 53 + x - the matrix 54 - rctx - the random number context, formed by PetscRandomCreate(), or NULL and 55 it will create one internally. 56 57 Output Parameter: 58 . x - the matrix 59 60 Example of Usage: 61 .vb 62 PetscRandomCreate(PETSC_COMM_WORLD,&rctx); 63 MatSetRandom(x,rctx); 64 PetscRandomDestroy(rctx); 65 .ve 66 67 Level: intermediate 68 69 .seealso: MatZeroEntries(), MatSetValues(), PetscRandomCreate(), PetscRandomDestroy() 70 @*/ 71 PetscErrorCode MatSetRandom(Mat x,PetscRandom rctx) 72 { 73 PetscErrorCode ierr; 74 PetscRandom randObj = NULL; 75 76 PetscFunctionBegin; 77 PetscValidHeaderSpecific(x,MAT_CLASSID,1); 78 if (rctx) PetscValidHeaderSpecific(rctx,PETSC_RANDOM_CLASSID,2); 79 PetscValidType(x,1); 80 81 if (!x->ops->setrandom) SETERRQ1(PetscObjectComm((PetscObject)x),PETSC_ERR_SUP,"Mat type %s",((PetscObject)x)->type_name); 82 83 if (!rctx) { 84 MPI_Comm comm; 85 ierr = PetscObjectGetComm((PetscObject)x,&comm);CHKERRQ(ierr); 86 ierr = PetscRandomCreate(comm,&randObj);CHKERRQ(ierr); 87 ierr = PetscRandomSetFromOptions(randObj);CHKERRQ(ierr); 88 rctx = randObj; 89 } 90 91 ierr = PetscLogEventBegin(MAT_SetRandom,x,rctx,0,0);CHKERRQ(ierr); 92 ierr = (*x->ops->setrandom)(x,rctx);CHKERRQ(ierr); 93 ierr = PetscLogEventEnd(MAT_SetRandom,x,rctx,0,0);CHKERRQ(ierr); 94 95 ierr = MatAssemblyBegin(x, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 96 ierr = MatAssemblyEnd(x, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 97 ierr = PetscRandomDestroy(&randObj);CHKERRQ(ierr); 98 PetscFunctionReturn(0); 99 } 100 101 /*@ 102 MatFactorGetErrorZeroPivot - returns the pivot value that was determined to be zero and the row it occurred in 103 104 Logically Collective on Mat 105 106 Input Parameters: 107 . mat - the factored matrix 108 109 Output Parameter: 110 + pivot - the pivot value computed 111 - row - the row that the zero pivot occurred. Note that this row must be interpreted carefully due to row reorderings and which processes 112 the share the matrix 113 114 Level: advanced 115 116 Notes: 117 This routine does not work for factorizations done with external packages. 118 119 This routine should only be called if MatGetFactorError() returns a value of MAT_FACTOR_NUMERIC_ZEROPIVOT 120 121 This can be called on non-factored matrices that come from, for example, matrices used in SOR. 122 123 .seealso: MatZeroEntries(), MatFactor(), MatGetFactor(), MatLUFactorSymbolic(), MatCholeskyFactorSymbolic(), MatFactorClearError(), MatFactorGetErrorZeroPivot() 124 @*/ 125 PetscErrorCode MatFactorGetErrorZeroPivot(Mat mat,PetscReal *pivot,PetscInt *row) 126 { 127 PetscFunctionBegin; 128 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 129 *pivot = mat->factorerror_zeropivot_value; 130 *row = mat->factorerror_zeropivot_row; 131 PetscFunctionReturn(0); 132 } 133 134 /*@ 135 MatFactorGetError - gets the error code from a factorization 136 137 Logically Collective on Mat 138 139 Input Parameters: 140 . mat - the factored matrix 141 142 Output Parameter: 143 . err - the error code 144 145 Level: advanced 146 147 Notes: 148 This can be called on non-factored matrices that come from, for example, matrices used in SOR. 149 150 .seealso: MatZeroEntries(), MatFactor(), MatGetFactor(), MatLUFactorSymbolic(), MatCholeskyFactorSymbolic(), MatFactorClearError(), MatFactorGetErrorZeroPivot() 151 @*/ 152 PetscErrorCode MatFactorGetError(Mat mat,MatFactorError *err) 153 { 154 PetscFunctionBegin; 155 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 156 *err = mat->factorerrortype; 157 PetscFunctionReturn(0); 158 } 159 160 /*@ 161 MatFactorClearError - clears the error code in a factorization 162 163 Logically Collective on Mat 164 165 Input Parameter: 166 . mat - the factored matrix 167 168 Level: developer 169 170 Notes: 171 This can be called on non-factored matrices that come from, for example, matrices used in SOR. 172 173 .seealso: MatZeroEntries(), MatFactor(), MatGetFactor(), MatLUFactorSymbolic(), MatCholeskyFactorSymbolic(), MatFactorGetError(), MatFactorGetErrorZeroPivot() 174 @*/ 175 PetscErrorCode MatFactorClearError(Mat mat) 176 { 177 PetscFunctionBegin; 178 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 179 mat->factorerrortype = MAT_FACTOR_NOERROR; 180 mat->factorerror_zeropivot_value = 0.0; 181 mat->factorerror_zeropivot_row = 0; 182 PetscFunctionReturn(0); 183 } 184 185 PETSC_INTERN PetscErrorCode MatFindNonzeroRowsOrCols_Basic(Mat mat,PetscBool cols,PetscReal tol,IS *nonzero) 186 { 187 PetscErrorCode ierr; 188 Vec r,l; 189 const PetscScalar *al; 190 PetscInt i,nz,gnz,N,n; 191 192 PetscFunctionBegin; 193 ierr = MatCreateVecs(mat,&r,&l);CHKERRQ(ierr); 194 if (!cols) { /* nonzero rows */ 195 ierr = MatGetSize(mat,&N,NULL);CHKERRQ(ierr); 196 ierr = MatGetLocalSize(mat,&n,NULL);CHKERRQ(ierr); 197 ierr = VecSet(l,0.0);CHKERRQ(ierr); 198 ierr = VecSetRandom(r,NULL);CHKERRQ(ierr); 199 ierr = MatMult(mat,r,l);CHKERRQ(ierr); 200 ierr = VecGetArrayRead(l,&al);CHKERRQ(ierr); 201 } else { /* nonzero columns */ 202 ierr = MatGetSize(mat,NULL,&N);CHKERRQ(ierr); 203 ierr = MatGetLocalSize(mat,NULL,&n);CHKERRQ(ierr); 204 ierr = VecSet(r,0.0);CHKERRQ(ierr); 205 ierr = VecSetRandom(l,NULL);CHKERRQ(ierr); 206 ierr = MatMultTranspose(mat,l,r);CHKERRQ(ierr); 207 ierr = VecGetArrayRead(r,&al);CHKERRQ(ierr); 208 } 209 if (tol <= 0.0) { for (i=0,nz=0;i<n;i++) if (al[i] != 0.0) nz++; } 210 else { for (i=0,nz=0;i<n;i++) if (PetscAbsScalar(al[i]) > tol) nz++; } 211 ierr = MPIU_Allreduce(&nz,&gnz,1,MPIU_INT,MPI_SUM,PetscObjectComm((PetscObject)mat));CHKERRMPI(ierr); 212 if (gnz != N) { 213 PetscInt *nzr; 214 ierr = PetscMalloc1(nz,&nzr);CHKERRQ(ierr); 215 if (nz) { 216 if (tol < 0) { for (i=0,nz=0;i<n;i++) if (al[i] != 0.0) nzr[nz++] = i; } 217 else { for (i=0,nz=0;i<n;i++) if (PetscAbsScalar(al[i]) > tol) nzr[nz++] = i; } 218 } 219 ierr = ISCreateGeneral(PetscObjectComm((PetscObject)mat),nz,nzr,PETSC_OWN_POINTER,nonzero);CHKERRQ(ierr); 220 } else *nonzero = NULL; 221 if (!cols) { /* nonzero rows */ 222 ierr = VecRestoreArrayRead(l,&al);CHKERRQ(ierr); 223 } else { 224 ierr = VecRestoreArrayRead(r,&al);CHKERRQ(ierr); 225 } 226 ierr = VecDestroy(&l);CHKERRQ(ierr); 227 ierr = VecDestroy(&r);CHKERRQ(ierr); 228 PetscFunctionReturn(0); 229 } 230 231 /*@ 232 MatFindNonzeroRows - Locate all rows that are not completely zero in the matrix 233 234 Input Parameter: 235 . A - the matrix 236 237 Output Parameter: 238 . keptrows - the rows that are not completely zero 239 240 Notes: 241 keptrows is set to NULL if all rows are nonzero. 242 243 Level: intermediate 244 245 @*/ 246 PetscErrorCode MatFindNonzeroRows(Mat mat,IS *keptrows) 247 { 248 PetscErrorCode ierr; 249 250 PetscFunctionBegin; 251 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 252 PetscValidType(mat,1); 253 PetscValidPointer(keptrows,2); 254 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 255 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 256 if (!mat->ops->findnonzerorows) { 257 ierr = MatFindNonzeroRowsOrCols_Basic(mat,PETSC_FALSE,0.0,keptrows);CHKERRQ(ierr); 258 } else { 259 ierr = (*mat->ops->findnonzerorows)(mat,keptrows);CHKERRQ(ierr); 260 } 261 PetscFunctionReturn(0); 262 } 263 264 /*@ 265 MatFindZeroRows - Locate all rows that are completely zero in the matrix 266 267 Input Parameter: 268 . A - the matrix 269 270 Output Parameter: 271 . zerorows - the rows that are completely zero 272 273 Notes: 274 zerorows is set to NULL if no rows are zero. 275 276 Level: intermediate 277 278 @*/ 279 PetscErrorCode MatFindZeroRows(Mat mat,IS *zerorows) 280 { 281 PetscErrorCode ierr; 282 IS keptrows; 283 PetscInt m, n; 284 285 PetscFunctionBegin; 286 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 287 PetscValidType(mat,1); 288 PetscValidPointer(zerorows,2); 289 ierr = MatFindNonzeroRows(mat, &keptrows);CHKERRQ(ierr); 290 /* MatFindNonzeroRows sets keptrows to NULL if there are no zero rows. 291 In keeping with this convention, we set zerorows to NULL if there are no zero 292 rows. */ 293 if (keptrows == NULL) { 294 *zerorows = NULL; 295 } else { 296 ierr = MatGetOwnershipRange(mat,&m,&n);CHKERRQ(ierr); 297 ierr = ISComplement(keptrows,m,n,zerorows);CHKERRQ(ierr); 298 ierr = ISDestroy(&keptrows);CHKERRQ(ierr); 299 } 300 PetscFunctionReturn(0); 301 } 302 303 /*@ 304 MatGetDiagonalBlock - Returns the part of the matrix associated with the on-process coupling 305 306 Not Collective 307 308 Input Parameters: 309 . A - the matrix 310 311 Output Parameters: 312 . a - the diagonal part (which is a SEQUENTIAL matrix) 313 314 Notes: 315 see the manual page for MatCreateAIJ() for more information on the "diagonal part" of the matrix. 316 Use caution, as the reference count on the returned matrix is not incremented and it is used as 317 part of the containing MPI Mat's normal operation. 318 319 Level: advanced 320 321 @*/ 322 PetscErrorCode MatGetDiagonalBlock(Mat A,Mat *a) 323 { 324 PetscErrorCode ierr; 325 326 PetscFunctionBegin; 327 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 328 PetscValidType(A,1); 329 PetscValidPointer(a,2); 330 if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 331 if (!A->ops->getdiagonalblock) { 332 PetscMPIInt size; 333 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)A),&size);CHKERRMPI(ierr); 334 if (size == 1) { 335 *a = A; 336 PetscFunctionReturn(0); 337 } else SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Not coded for matrix type %s",((PetscObject)A)->type_name); 338 } 339 ierr = (*A->ops->getdiagonalblock)(A,a);CHKERRQ(ierr); 340 PetscFunctionReturn(0); 341 } 342 343 /*@ 344 MatGetTrace - Gets the trace of a matrix. The sum of the diagonal entries. 345 346 Collective on Mat 347 348 Input Parameters: 349 . mat - the matrix 350 351 Output Parameter: 352 . trace - the sum of the diagonal entries 353 354 Level: advanced 355 356 @*/ 357 PetscErrorCode MatGetTrace(Mat mat,PetscScalar *trace) 358 { 359 PetscErrorCode ierr; 360 Vec diag; 361 362 PetscFunctionBegin; 363 ierr = MatCreateVecs(mat,&diag,NULL);CHKERRQ(ierr); 364 ierr = MatGetDiagonal(mat,diag);CHKERRQ(ierr); 365 ierr = VecSum(diag,trace);CHKERRQ(ierr); 366 ierr = VecDestroy(&diag);CHKERRQ(ierr); 367 PetscFunctionReturn(0); 368 } 369 370 /*@ 371 MatRealPart - Zeros out the imaginary part of the matrix 372 373 Logically Collective on Mat 374 375 Input Parameters: 376 . mat - the matrix 377 378 Level: advanced 379 380 .seealso: MatImaginaryPart() 381 @*/ 382 PetscErrorCode MatRealPart(Mat mat) 383 { 384 PetscErrorCode ierr; 385 386 PetscFunctionBegin; 387 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 388 PetscValidType(mat,1); 389 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 390 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 391 if (!mat->ops->realpart) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 392 MatCheckPreallocated(mat,1); 393 ierr = (*mat->ops->realpart)(mat);CHKERRQ(ierr); 394 PetscFunctionReturn(0); 395 } 396 397 /*@C 398 MatGetGhosts - Get the global index of all ghost nodes defined by the sparse matrix 399 400 Collective on Mat 401 402 Input Parameter: 403 . mat - the matrix 404 405 Output Parameters: 406 + nghosts - number of ghosts (note for BAIJ matrices there is one ghost for each block) 407 - ghosts - the global indices of the ghost points 408 409 Notes: 410 the nghosts and ghosts are suitable to pass into VecCreateGhost() 411 412 Level: advanced 413 414 @*/ 415 PetscErrorCode MatGetGhosts(Mat mat,PetscInt *nghosts,const PetscInt *ghosts[]) 416 { 417 PetscErrorCode ierr; 418 419 PetscFunctionBegin; 420 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 421 PetscValidType(mat,1); 422 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 423 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 424 if (!mat->ops->getghosts) { 425 if (nghosts) *nghosts = 0; 426 if (ghosts) *ghosts = NULL; 427 } else { 428 ierr = (*mat->ops->getghosts)(mat,nghosts,ghosts);CHKERRQ(ierr); 429 } 430 PetscFunctionReturn(0); 431 } 432 433 /*@ 434 MatImaginaryPart - Moves the imaginary part of the matrix to the real part and zeros the imaginary part 435 436 Logically Collective on Mat 437 438 Input Parameters: 439 . mat - the matrix 440 441 Level: advanced 442 443 .seealso: MatRealPart() 444 @*/ 445 PetscErrorCode MatImaginaryPart(Mat mat) 446 { 447 PetscErrorCode ierr; 448 449 PetscFunctionBegin; 450 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 451 PetscValidType(mat,1); 452 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 453 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 454 if (!mat->ops->imaginarypart) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 455 MatCheckPreallocated(mat,1); 456 ierr = (*mat->ops->imaginarypart)(mat);CHKERRQ(ierr); 457 PetscFunctionReturn(0); 458 } 459 460 /*@ 461 MatMissingDiagonal - Determine if sparse matrix is missing a diagonal entry (or block entry for BAIJ matrices) 462 463 Not Collective 464 465 Input Parameter: 466 . mat - the matrix 467 468 Output Parameters: 469 + missing - is any diagonal missing 470 - dd - first diagonal entry that is missing (optional) on this process 471 472 Level: advanced 473 474 .seealso: MatRealPart() 475 @*/ 476 PetscErrorCode MatMissingDiagonal(Mat mat,PetscBool *missing,PetscInt *dd) 477 { 478 PetscErrorCode ierr; 479 480 PetscFunctionBegin; 481 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 482 PetscValidType(mat,1); 483 PetscValidPointer(missing,2); 484 if (!mat->assembled) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix %s",((PetscObject)mat)->type_name); 485 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 486 if (!mat->ops->missingdiagonal) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 487 ierr = (*mat->ops->missingdiagonal)(mat,missing,dd);CHKERRQ(ierr); 488 PetscFunctionReturn(0); 489 } 490 491 /*@C 492 MatGetRow - Gets a row of a matrix. You MUST call MatRestoreRow() 493 for each row that you get to ensure that your application does 494 not bleed memory. 495 496 Not Collective 497 498 Input Parameters: 499 + mat - the matrix 500 - row - the row to get 501 502 Output Parameters: 503 + ncols - if not NULL, the number of nonzeros in the row 504 . cols - if not NULL, the column numbers 505 - vals - if not NULL, the values 506 507 Notes: 508 This routine is provided for people who need to have direct access 509 to the structure of a matrix. We hope that we provide enough 510 high-level matrix routines that few users will need it. 511 512 MatGetRow() always returns 0-based column indices, regardless of 513 whether the internal representation is 0-based (default) or 1-based. 514 515 For better efficiency, set cols and/or vals to NULL if you do 516 not wish to extract these quantities. 517 518 The user can only examine the values extracted with MatGetRow(); 519 the values cannot be altered. To change the matrix entries, one 520 must use MatSetValues(). 521 522 You can only have one call to MatGetRow() outstanding for a particular 523 matrix at a time, per processor. MatGetRow() can only obtain rows 524 associated with the given processor, it cannot get rows from the 525 other processors; for that we suggest using MatCreateSubMatrices(), then 526 MatGetRow() on the submatrix. The row index passed to MatGetRow() 527 is in the global number of rows. 528 529 Fortran Notes: 530 The calling sequence from Fortran is 531 .vb 532 MatGetRow(matrix,row,ncols,cols,values,ierr) 533 Mat matrix (input) 534 integer row (input) 535 integer ncols (output) 536 integer cols(maxcols) (output) 537 double precision (or double complex) values(maxcols) output 538 .ve 539 where maxcols >= maximum nonzeros in any row of the matrix. 540 541 Caution: 542 Do not try to change the contents of the output arrays (cols and vals). 543 In some cases, this may corrupt the matrix. 544 545 Level: advanced 546 547 .seealso: MatRestoreRow(), MatSetValues(), MatGetValues(), MatCreateSubMatrices(), MatGetDiagonal() 548 @*/ 549 PetscErrorCode MatGetRow(Mat mat,PetscInt row,PetscInt *ncols,const PetscInt *cols[],const PetscScalar *vals[]) 550 { 551 PetscErrorCode ierr; 552 PetscInt incols; 553 554 PetscFunctionBegin; 555 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 556 PetscValidType(mat,1); 557 if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 558 if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 559 if (!mat->ops->getrow) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 560 MatCheckPreallocated(mat,1); 561 if (row < mat->rmap->rstart || row >= mat->rmap->rend) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Only for local rows, %D not in [%D,%D)",row,mat->rmap->rstart,mat->rmap->rend); 562 ierr = PetscLogEventBegin(MAT_GetRow,mat,0,0,0);CHKERRQ(ierr); 563 ierr = (*mat->ops->getrow)(mat,row,&incols,(PetscInt**)cols,(PetscScalar**)vals);CHKERRQ(ierr); 564 if (ncols) *ncols = incols; 565 ierr = PetscLogEventEnd(MAT_GetRow,mat,0,0,0);CHKERRQ(ierr); 566 PetscFunctionReturn(0); 567 } 568 569 /*@ 570 MatConjugate - replaces the matrix values with their complex conjugates 571 572 Logically Collective on Mat 573 574 Input Parameters: 575 . mat - the matrix 576 577 Level: advanced 578 579 .seealso: VecConjugate() 580 @*/ 581 PetscErrorCode MatConjugate(Mat mat) 582 { 583 #if defined(PETSC_USE_COMPLEX) 584 PetscErrorCode ierr; 585 586 PetscFunctionBegin; 587 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 588 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 589 if (!mat->ops->conjugate) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Not provided for matrix type %s, send email to petsc-maint@mcs.anl.gov",((PetscObject)mat)->type_name); 590 ierr = (*mat->ops->conjugate)(mat);CHKERRQ(ierr); 591 #else 592 PetscFunctionBegin; 593 #endif 594 PetscFunctionReturn(0); 595 } 596 597 /*@C 598 MatRestoreRow - Frees any temporary space allocated by MatGetRow(). 599 600 Not Collective 601 602 Input Parameters: 603 + mat - the matrix 604 . row - the row to get 605 . ncols, cols - the number of nonzeros and their columns 606 - vals - if nonzero the column values 607 608 Notes: 609 This routine should be called after you have finished examining the entries. 610 611 This routine zeros out ncols, cols, and vals. This is to prevent accidental 612 us of the array after it has been restored. If you pass NULL, it will 613 not zero the pointers. Use of cols or vals after MatRestoreRow is invalid. 614 615 Fortran Notes: 616 The calling sequence from Fortran is 617 .vb 618 MatRestoreRow(matrix,row,ncols,cols,values,ierr) 619 Mat matrix (input) 620 integer row (input) 621 integer ncols (output) 622 integer cols(maxcols) (output) 623 double precision (or double complex) values(maxcols) output 624 .ve 625 Where maxcols >= maximum nonzeros in any row of the matrix. 626 627 In Fortran MatRestoreRow() MUST be called after MatGetRow() 628 before another call to MatGetRow() can be made. 629 630 Level: advanced 631 632 .seealso: MatGetRow() 633 @*/ 634 PetscErrorCode MatRestoreRow(Mat mat,PetscInt row,PetscInt *ncols,const PetscInt *cols[],const PetscScalar *vals[]) 635 { 636 PetscErrorCode ierr; 637 638 PetscFunctionBegin; 639 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 640 if (ncols) PetscValidIntPointer(ncols,3); 641 if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 642 if (!mat->ops->restorerow) PetscFunctionReturn(0); 643 ierr = (*mat->ops->restorerow)(mat,row,ncols,(PetscInt **)cols,(PetscScalar **)vals);CHKERRQ(ierr); 644 if (ncols) *ncols = 0; 645 if (cols) *cols = NULL; 646 if (vals) *vals = NULL; 647 PetscFunctionReturn(0); 648 } 649 650 /*@ 651 MatGetRowUpperTriangular - Sets a flag to enable calls to MatGetRow() for matrix in MATSBAIJ format. 652 You should call MatRestoreRowUpperTriangular() after calling MatGetRow/MatRestoreRow() to disable the flag. 653 654 Not Collective 655 656 Input Parameters: 657 . mat - the matrix 658 659 Notes: 660 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. 661 662 Level: advanced 663 664 .seealso: MatRestoreRowUpperTriangular() 665 @*/ 666 PetscErrorCode MatGetRowUpperTriangular(Mat mat) 667 { 668 PetscErrorCode ierr; 669 670 PetscFunctionBegin; 671 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 672 PetscValidType(mat,1); 673 if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 674 if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 675 MatCheckPreallocated(mat,1); 676 if (!mat->ops->getrowuppertriangular) PetscFunctionReturn(0); 677 ierr = (*mat->ops->getrowuppertriangular)(mat);CHKERRQ(ierr); 678 PetscFunctionReturn(0); 679 } 680 681 /*@ 682 MatRestoreRowUpperTriangular - Disable calls to MatGetRow() for matrix in MATSBAIJ format. 683 684 Not Collective 685 686 Input Parameters: 687 . mat - the matrix 688 689 Notes: 690 This routine should be called after you have finished MatGetRow/MatRestoreRow(). 691 692 Level: advanced 693 694 .seealso: MatGetRowUpperTriangular() 695 @*/ 696 PetscErrorCode MatRestoreRowUpperTriangular(Mat mat) 697 { 698 PetscErrorCode ierr; 699 700 PetscFunctionBegin; 701 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 702 PetscValidType(mat,1); 703 if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 704 if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 705 MatCheckPreallocated(mat,1); 706 if (!mat->ops->restorerowuppertriangular) PetscFunctionReturn(0); 707 ierr = (*mat->ops->restorerowuppertriangular)(mat);CHKERRQ(ierr); 708 PetscFunctionReturn(0); 709 } 710 711 /*@C 712 MatSetOptionsPrefix - Sets the prefix used for searching for all 713 Mat options in the database. 714 715 Logically Collective on Mat 716 717 Input Parameter: 718 + A - the Mat context 719 - prefix - the prefix to prepend to all option names 720 721 Notes: 722 A hyphen (-) must NOT be given at the beginning of the prefix name. 723 The first character of all runtime options is AUTOMATICALLY the hyphen. 724 725 Level: advanced 726 727 .seealso: MatSetFromOptions() 728 @*/ 729 PetscErrorCode MatSetOptionsPrefix(Mat A,const char prefix[]) 730 { 731 PetscErrorCode ierr; 732 733 PetscFunctionBegin; 734 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 735 ierr = PetscObjectSetOptionsPrefix((PetscObject)A,prefix);CHKERRQ(ierr); 736 PetscFunctionReturn(0); 737 } 738 739 /*@C 740 MatAppendOptionsPrefix - Appends to the prefix used for searching for all 741 Mat options in the database. 742 743 Logically Collective on Mat 744 745 Input Parameters: 746 + A - the Mat context 747 - prefix - the prefix to prepend to all option names 748 749 Notes: 750 A hyphen (-) must NOT be given at the beginning of the prefix name. 751 The first character of all runtime options is AUTOMATICALLY the hyphen. 752 753 Level: advanced 754 755 .seealso: MatGetOptionsPrefix() 756 @*/ 757 PetscErrorCode MatAppendOptionsPrefix(Mat A,const char prefix[]) 758 { 759 PetscErrorCode ierr; 760 761 PetscFunctionBegin; 762 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 763 ierr = PetscObjectAppendOptionsPrefix((PetscObject)A,prefix);CHKERRQ(ierr); 764 PetscFunctionReturn(0); 765 } 766 767 /*@C 768 MatGetOptionsPrefix - Gets the prefix used for searching for all 769 Mat options in the database. 770 771 Not Collective 772 773 Input Parameter: 774 . A - the Mat context 775 776 Output Parameter: 777 . prefix - pointer to the prefix string used 778 779 Notes: 780 On the fortran side, the user should pass in a string 'prefix' of 781 sufficient length to hold the prefix. 782 783 Level: advanced 784 785 .seealso: MatAppendOptionsPrefix() 786 @*/ 787 PetscErrorCode MatGetOptionsPrefix(Mat A,const char *prefix[]) 788 { 789 PetscErrorCode ierr; 790 791 PetscFunctionBegin; 792 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 793 ierr = PetscObjectGetOptionsPrefix((PetscObject)A,prefix);CHKERRQ(ierr); 794 PetscFunctionReturn(0); 795 } 796 797 /*@ 798 MatResetPreallocation - Reset mat to use the original nonzero pattern provided by users. 799 800 Collective on Mat 801 802 Input Parameters: 803 . A - the Mat context 804 805 Notes: 806 The allocated memory will be shrunk after calling MatAssembly with MAT_FINAL_ASSEMBLY. Users can reset the preallocation to access the original memory. 807 Currently support MPIAIJ and SEQAIJ. 808 809 Level: beginner 810 811 .seealso: MatSeqAIJSetPreallocation(), MatMPIAIJSetPreallocation(), MatXAIJSetPreallocation() 812 @*/ 813 PetscErrorCode MatResetPreallocation(Mat A) 814 { 815 PetscErrorCode ierr; 816 817 PetscFunctionBegin; 818 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 819 PetscValidType(A,1); 820 ierr = PetscUseMethod(A,"MatResetPreallocation_C",(Mat),(A));CHKERRQ(ierr); 821 PetscFunctionReturn(0); 822 } 823 824 /*@ 825 MatSetUp - Sets up the internal matrix data structures for later use. 826 827 Collective on Mat 828 829 Input Parameters: 830 . A - the Mat context 831 832 Notes: 833 If the user has not set preallocation for this matrix then a default preallocation that is likely to be inefficient is used. 834 835 If a suitable preallocation routine is used, this function does not need to be called. 836 837 See the Performance chapter of the PETSc users manual for how to preallocate matrices 838 839 Level: beginner 840 841 .seealso: MatCreate(), MatDestroy() 842 @*/ 843 PetscErrorCode MatSetUp(Mat A) 844 { 845 PetscMPIInt size; 846 PetscErrorCode ierr; 847 848 PetscFunctionBegin; 849 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 850 if (!((PetscObject)A)->type_name) { 851 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)A), &size);CHKERRMPI(ierr); 852 if (size == 1) { 853 ierr = MatSetType(A, MATSEQAIJ);CHKERRQ(ierr); 854 } else { 855 ierr = MatSetType(A, MATMPIAIJ);CHKERRQ(ierr); 856 } 857 } 858 if (!A->preallocated && A->ops->setup) { 859 ierr = PetscInfo(A,"Warning not preallocating matrix storage\n");CHKERRQ(ierr); 860 ierr = (*A->ops->setup)(A);CHKERRQ(ierr); 861 } 862 ierr = PetscLayoutSetUp(A->rmap);CHKERRQ(ierr); 863 ierr = PetscLayoutSetUp(A->cmap);CHKERRQ(ierr); 864 A->preallocated = PETSC_TRUE; 865 PetscFunctionReturn(0); 866 } 867 868 #if defined(PETSC_HAVE_SAWS) 869 #include <petscviewersaws.h> 870 #endif 871 872 /*@C 873 MatViewFromOptions - View from Options 874 875 Collective on Mat 876 877 Input Parameters: 878 + A - the Mat context 879 . obj - Optional object 880 - name - command line option 881 882 Level: intermediate 883 .seealso: Mat, MatView, PetscObjectViewFromOptions(), MatCreate() 884 @*/ 885 PetscErrorCode MatViewFromOptions(Mat A,PetscObject obj,const char name[]) 886 { 887 PetscErrorCode ierr; 888 889 PetscFunctionBegin; 890 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 891 ierr = PetscObjectViewFromOptions((PetscObject)A,obj,name);CHKERRQ(ierr); 892 PetscFunctionReturn(0); 893 } 894 895 /*@C 896 MatView - Visualizes a matrix object. 897 898 Collective on Mat 899 900 Input Parameters: 901 + mat - the matrix 902 - viewer - visualization context 903 904 Notes: 905 The available visualization contexts include 906 + PETSC_VIEWER_STDOUT_SELF - for sequential matrices 907 . PETSC_VIEWER_STDOUT_WORLD - for parallel matrices created on PETSC_COMM_WORLD 908 . PETSC_VIEWER_STDOUT_(comm) - for matrices created on MPI communicator comm 909 - PETSC_VIEWER_DRAW_WORLD - graphical display of nonzero structure 910 911 The user can open alternative visualization contexts with 912 + PetscViewerASCIIOpen() - Outputs matrix to a specified file 913 . PetscViewerBinaryOpen() - Outputs matrix in binary to a 914 specified file; corresponding input uses MatLoad() 915 . PetscViewerDrawOpen() - Outputs nonzero matrix structure to 916 an X window display 917 - PetscViewerSocketOpen() - Outputs matrix to Socket viewer. 918 Currently only the sequential dense and AIJ 919 matrix types support the Socket viewer. 920 921 The user can call PetscViewerPushFormat() to specify the output 922 format of ASCII printed objects (when using PETSC_VIEWER_STDOUT_SELF, 923 PETSC_VIEWER_STDOUT_WORLD and PetscViewerASCIIOpen). Available formats include 924 + PETSC_VIEWER_DEFAULT - default, prints matrix contents 925 . PETSC_VIEWER_ASCII_MATLAB - prints matrix contents in Matlab format 926 . PETSC_VIEWER_ASCII_DENSE - prints entire matrix including zeros 927 . PETSC_VIEWER_ASCII_COMMON - prints matrix contents, using a sparse 928 format common among all matrix types 929 . PETSC_VIEWER_ASCII_IMPL - prints matrix contents, using an implementation-specific 930 format (which is in many cases the same as the default) 931 . PETSC_VIEWER_ASCII_INFO - prints basic information about the matrix 932 size and structure (not the matrix entries) 933 - PETSC_VIEWER_ASCII_INFO_DETAIL - prints more detailed information about 934 the matrix structure 935 936 Options Database Keys: 937 + -mat_view ::ascii_info - Prints info on matrix at conclusion of MatAssemblyEnd() 938 . -mat_view ::ascii_info_detail - Prints more detailed info 939 . -mat_view - Prints matrix in ASCII format 940 . -mat_view ::ascii_matlab - Prints matrix in Matlab format 941 . -mat_view draw - PetscDraws nonzero structure of matrix, using MatView() and PetscDrawOpenX(). 942 . -display <name> - Sets display name (default is host) 943 . -draw_pause <sec> - Sets number of seconds to pause after display 944 . -mat_view socket - Sends matrix to socket, can be accessed from Matlab (see Users-Manual: ch_matlab for details) 945 . -viewer_socket_machine <machine> - 946 . -viewer_socket_port <port> - 947 . -mat_view binary - save matrix to file in binary format 948 - -viewer_binary_filename <name> - 949 Level: beginner 950 951 Notes: 952 The ASCII viewers are only recommended for small matrices on at most a moderate number of processes, 953 the program will seemingly hang and take hours for larger matrices, for larger matrices one should use the binary format. 954 955 In the debugger you can do "call MatView(mat,0)" to display the matrix. (The same holds for any PETSc object viewer). 956 957 See the manual page for MatLoad() for the exact format of the binary file when the binary 958 viewer is used. 959 960 See share/petsc/matlab/PetscBinaryRead.m for a Matlab code that can read in the binary file when the binary 961 viewer is used and lib/petsc/bin/PetscBinaryIO.py for loading them into Python. 962 963 One can use '-mat_view draw -draw_pause -1' to pause the graphical display of matrix nonzero structure, 964 and then use the following mouse functions. 965 + left mouse: zoom in 966 . middle mouse: zoom out 967 - right mouse: continue with the simulation 968 969 .seealso: PetscViewerPushFormat(), PetscViewerASCIIOpen(), PetscViewerDrawOpen(), 970 PetscViewerSocketOpen(), PetscViewerBinaryOpen(), MatLoad() 971 @*/ 972 PetscErrorCode MatView(Mat mat,PetscViewer viewer) 973 { 974 PetscErrorCode ierr; 975 PetscInt rows,cols,rbs,cbs; 976 PetscBool isascii,isstring,issaws; 977 PetscViewerFormat format; 978 PetscMPIInt size; 979 980 PetscFunctionBegin; 981 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 982 PetscValidType(mat,1); 983 if (!viewer) {ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)mat),&viewer);CHKERRQ(ierr);} 984 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 985 PetscCheckSameComm(mat,1,viewer,2); 986 MatCheckPreallocated(mat,1); 987 988 ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 989 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);CHKERRMPI(ierr); 990 if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0); 991 992 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 993 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);CHKERRQ(ierr); 994 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr); 995 if ((!isascii || (format != PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL)) && mat->factortype) { 996 SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"No viewers for factored matrix except ASCII info or info_detail"); 997 } 998 999 ierr = PetscLogEventBegin(MAT_View,mat,viewer,0,0);CHKERRQ(ierr); 1000 if (isascii) { 1001 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ORDER,"Must call MatAssemblyBegin/End() before viewing matrix"); 1002 ierr = PetscObjectPrintClassNamePrefixType((PetscObject)mat,viewer);CHKERRQ(ierr); 1003 if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) { 1004 MatNullSpace nullsp,transnullsp; 1005 1006 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1007 ierr = MatGetSize(mat,&rows,&cols);CHKERRQ(ierr); 1008 ierr = MatGetBlockSizes(mat,&rbs,&cbs);CHKERRQ(ierr); 1009 if (rbs != 1 || cbs != 1) { 1010 if (rbs != cbs) {ierr = PetscViewerASCIIPrintf(viewer,"rows=%D, cols=%D, rbs=%D, cbs=%D\n",rows,cols,rbs,cbs);CHKERRQ(ierr);} 1011 else {ierr = PetscViewerASCIIPrintf(viewer,"rows=%D, cols=%D, bs=%D\n",rows,cols,rbs);CHKERRQ(ierr);} 1012 } else { 1013 ierr = PetscViewerASCIIPrintf(viewer,"rows=%D, cols=%D\n",rows,cols);CHKERRQ(ierr); 1014 } 1015 if (mat->factortype) { 1016 MatSolverType solver; 1017 ierr = MatFactorGetSolverType(mat,&solver);CHKERRQ(ierr); 1018 ierr = PetscViewerASCIIPrintf(viewer,"package used to perform factorization: %s\n",solver);CHKERRQ(ierr); 1019 } 1020 if (mat->ops->getinfo) { 1021 MatInfo info; 1022 ierr = MatGetInfo(mat,MAT_GLOBAL_SUM,&info);CHKERRQ(ierr); 1023 ierr = PetscViewerASCIIPrintf(viewer,"total: nonzeros=%.f, allocated nonzeros=%.f\n",info.nz_used,info.nz_allocated);CHKERRQ(ierr); 1024 if (!mat->factortype) { 1025 ierr = PetscViewerASCIIPrintf(viewer,"total number of mallocs used during MatSetValues calls=%D\n",(PetscInt)info.mallocs);CHKERRQ(ierr); 1026 } 1027 } 1028 ierr = MatGetNullSpace(mat,&nullsp);CHKERRQ(ierr); 1029 ierr = MatGetTransposeNullSpace(mat,&transnullsp);CHKERRQ(ierr); 1030 if (nullsp) {ierr = PetscViewerASCIIPrintf(viewer," has attached null space\n");CHKERRQ(ierr);} 1031 if (transnullsp && transnullsp != nullsp) {ierr = PetscViewerASCIIPrintf(viewer," has attached transposed null space\n");CHKERRQ(ierr);} 1032 ierr = MatGetNearNullSpace(mat,&nullsp);CHKERRQ(ierr); 1033 if (nullsp) {ierr = PetscViewerASCIIPrintf(viewer," has attached near null space\n");CHKERRQ(ierr);} 1034 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1035 ierr = MatProductView(mat,viewer);CHKERRQ(ierr); 1036 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1037 } 1038 } else if (issaws) { 1039 #if defined(PETSC_HAVE_SAWS) 1040 PetscMPIInt rank; 1041 1042 ierr = PetscObjectName((PetscObject)mat);CHKERRQ(ierr); 1043 ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRMPI(ierr); 1044 if (!((PetscObject)mat)->amsmem && !rank) { 1045 ierr = PetscObjectViewSAWs((PetscObject)mat,viewer);CHKERRQ(ierr); 1046 } 1047 #endif 1048 } else if (isstring) { 1049 const char *type; 1050 ierr = MatGetType(mat,&type);CHKERRQ(ierr); 1051 ierr = PetscViewerStringSPrintf(viewer," MatType: %-7.7s",type);CHKERRQ(ierr); 1052 if (mat->ops->view) {ierr = (*mat->ops->view)(mat,viewer);CHKERRQ(ierr);} 1053 } 1054 if ((format == PETSC_VIEWER_NATIVE || format == PETSC_VIEWER_LOAD_BALANCE) && mat->ops->viewnative) { 1055 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1056 ierr = (*mat->ops->viewnative)(mat,viewer);CHKERRQ(ierr); 1057 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1058 } else if (mat->ops->view) { 1059 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1060 ierr = (*mat->ops->view)(mat,viewer);CHKERRQ(ierr); 1061 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1062 } 1063 if (isascii) { 1064 ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 1065 if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) { 1066 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1067 } 1068 } 1069 ierr = PetscLogEventEnd(MAT_View,mat,viewer,0,0);CHKERRQ(ierr); 1070 PetscFunctionReturn(0); 1071 } 1072 1073 #if defined(PETSC_USE_DEBUG) 1074 #include <../src/sys/totalview/tv_data_display.h> 1075 PETSC_UNUSED static int TV_display_type(const struct _p_Mat *mat) 1076 { 1077 TV_add_row("Local rows", "int", &mat->rmap->n); 1078 TV_add_row("Local columns", "int", &mat->cmap->n); 1079 TV_add_row("Global rows", "int", &mat->rmap->N); 1080 TV_add_row("Global columns", "int", &mat->cmap->N); 1081 TV_add_row("Typename", TV_ascii_string_type, ((PetscObject)mat)->type_name); 1082 return TV_format_OK; 1083 } 1084 #endif 1085 1086 /*@C 1087 MatLoad - Loads a matrix that has been stored in binary/HDF5 format 1088 with MatView(). The matrix format is determined from the options database. 1089 Generates a parallel MPI matrix if the communicator has more than one 1090 processor. The default matrix type is AIJ. 1091 1092 Collective on PetscViewer 1093 1094 Input Parameters: 1095 + mat - the newly loaded matrix, this needs to have been created with MatCreate() 1096 or some related function before a call to MatLoad() 1097 - viewer - binary/HDF5 file viewer 1098 1099 Options Database Keys: 1100 Used with block matrix formats (MATSEQBAIJ, ...) to specify 1101 block size 1102 . -matload_block_size <bs> 1103 1104 Level: beginner 1105 1106 Notes: 1107 If the Mat type has not yet been given then MATAIJ is used, call MatSetFromOptions() on the 1108 Mat before calling this routine if you wish to set it from the options database. 1109 1110 MatLoad() automatically loads into the options database any options 1111 given in the file filename.info where filename is the name of the file 1112 that was passed to the PetscViewerBinaryOpen(). The options in the info 1113 file will be ignored if you use the -viewer_binary_skip_info option. 1114 1115 If the type or size of mat is not set before a call to MatLoad, PETSc 1116 sets the default matrix type AIJ and sets the local and global sizes. 1117 If type and/or size is already set, then the same are used. 1118 1119 In parallel, each processor can load a subset of rows (or the 1120 entire matrix). This routine is especially useful when a large 1121 matrix is stored on disk and only part of it is desired on each 1122 processor. For example, a parallel solver may access only some of 1123 the rows from each processor. The algorithm used here reads 1124 relatively small blocks of data rather than reading the entire 1125 matrix and then subsetting it. 1126 1127 Viewer's PetscViewerType must be either PETSCVIEWERBINARY or PETSCVIEWERHDF5. 1128 Such viewer can be created using PetscViewerBinaryOpen()/PetscViewerHDF5Open(), 1129 or the sequence like 1130 $ PetscViewer v; 1131 $ PetscViewerCreate(PETSC_COMM_WORLD,&v); 1132 $ PetscViewerSetType(v,PETSCVIEWERBINARY); 1133 $ PetscViewerSetFromOptions(v); 1134 $ PetscViewerFileSetMode(v,FILE_MODE_READ); 1135 $ PetscViewerFileSetName(v,"datafile"); 1136 The optional PetscViewerSetFromOptions() call allows to override PetscViewerSetType() using option 1137 $ -viewer_type {binary,hdf5} 1138 1139 See the example src/ksp/ksp/tutorials/ex27.c with the first approach, 1140 and src/mat/tutorials/ex10.c with the second approach. 1141 1142 Notes about the PETSc binary format: 1143 In case of PETSCVIEWERBINARY, a native PETSc binary format is used. Each of the blocks 1144 is read onto rank 0 and then shipped to its destination rank, one after another. 1145 Multiple objects, both matrices and vectors, can be stored within the same file. 1146 Their PetscObject name is ignored; they are loaded in the order of their storage. 1147 1148 Most users should not need to know the details of the binary storage 1149 format, since MatLoad() and MatView() completely hide these details. 1150 But for anyone who's interested, the standard binary matrix storage 1151 format is 1152 1153 $ PetscInt MAT_FILE_CLASSID 1154 $ PetscInt number of rows 1155 $ PetscInt number of columns 1156 $ PetscInt total number of nonzeros 1157 $ PetscInt *number nonzeros in each row 1158 $ PetscInt *column indices of all nonzeros (starting index is zero) 1159 $ PetscScalar *values of all nonzeros 1160 1161 PETSc automatically does the byte swapping for 1162 machines that store the bytes reversed, e.g. DEC alpha, freebsd, 1163 linux, Windows and the paragon; thus if you write your own binary 1164 read/write routines you have to swap the bytes; see PetscBinaryRead() 1165 and PetscBinaryWrite() to see how this may be done. 1166 1167 Notes about the HDF5 (MATLAB MAT-File Version 7.3) format: 1168 In case of PETSCVIEWERHDF5, a parallel HDF5 reader is used. 1169 Each processor's chunk is loaded independently by its owning rank. 1170 Multiple objects, both matrices and vectors, can be stored within the same file. 1171 They are looked up by their PetscObject name. 1172 1173 As the MATLAB MAT-File Version 7.3 format is also a HDF5 flavor, we decided to use 1174 by default the same structure and naming of the AIJ arrays and column count 1175 within the HDF5 file. This means that a MAT file saved with -v7.3 flag, e.g. 1176 $ save example.mat A b -v7.3 1177 can be directly read by this routine (see Reference 1 for details). 1178 Note that depending on your MATLAB version, this format might be a default, 1179 otherwise you can set it as default in Preferences. 1180 1181 Unless -nocompression flag is used to save the file in MATLAB, 1182 PETSc must be configured with ZLIB package. 1183 1184 See also examples src/mat/tutorials/ex10.c and src/ksp/ksp/tutorials/ex27.c 1185 1186 Current HDF5 (MAT-File) limitations: 1187 This reader currently supports only real MATSEQAIJ, MATMPIAIJ, MATSEQDENSE and MATMPIDENSE matrices. 1188 1189 Corresponding MatView() is not yet implemented. 1190 1191 The loaded matrix is actually a transpose of the original one in MATLAB, 1192 unless you push PETSC_VIEWER_HDF5_MAT format (see examples above). 1193 With this format, matrix is automatically transposed by PETSc, 1194 unless the matrix is marked as SPD or symmetric 1195 (see MatSetOption(), MAT_SPD, MAT_SYMMETRIC). 1196 1197 References: 1198 1. MATLAB(R) Documentation, manual page of save(), https://www.mathworks.com/help/matlab/ref/save.html#btox10b-1-version 1199 1200 .seealso: PetscViewerBinaryOpen(), PetscViewerSetType(), MatView(), VecLoad() 1201 1202 @*/ 1203 PetscErrorCode MatLoad(Mat mat,PetscViewer viewer) 1204 { 1205 PetscErrorCode ierr; 1206 PetscBool flg; 1207 1208 PetscFunctionBegin; 1209 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 1210 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1211 1212 if (!((PetscObject)mat)->type_name) { 1213 ierr = MatSetType(mat,MATAIJ);CHKERRQ(ierr); 1214 } 1215 1216 flg = PETSC_FALSE; 1217 ierr = PetscOptionsGetBool(((PetscObject)mat)->options,((PetscObject)mat)->prefix,"-matload_symmetric",&flg,NULL);CHKERRQ(ierr); 1218 if (flg) { 1219 ierr = MatSetOption(mat,MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr); 1220 ierr = MatSetOption(mat,MAT_SYMMETRY_ETERNAL,PETSC_TRUE);CHKERRQ(ierr); 1221 } 1222 flg = PETSC_FALSE; 1223 ierr = PetscOptionsGetBool(((PetscObject)mat)->options,((PetscObject)mat)->prefix,"-matload_spd",&flg,NULL);CHKERRQ(ierr); 1224 if (flg) { 1225 ierr = MatSetOption(mat,MAT_SPD,PETSC_TRUE);CHKERRQ(ierr); 1226 } 1227 1228 if (!mat->ops->load) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatLoad is not supported for type %s",((PetscObject)mat)->type_name); 1229 ierr = PetscLogEventBegin(MAT_Load,mat,viewer,0,0);CHKERRQ(ierr); 1230 ierr = (*mat->ops->load)(mat,viewer);CHKERRQ(ierr); 1231 ierr = PetscLogEventEnd(MAT_Load,mat,viewer,0,0);CHKERRQ(ierr); 1232 PetscFunctionReturn(0); 1233 } 1234 1235 static PetscErrorCode MatDestroy_Redundant(Mat_Redundant **redundant) 1236 { 1237 PetscErrorCode ierr; 1238 Mat_Redundant *redund = *redundant; 1239 PetscInt i; 1240 1241 PetscFunctionBegin; 1242 if (redund) { 1243 if (redund->matseq) { /* via MatCreateSubMatrices() */ 1244 ierr = ISDestroy(&redund->isrow);CHKERRQ(ierr); 1245 ierr = ISDestroy(&redund->iscol);CHKERRQ(ierr); 1246 ierr = MatDestroySubMatrices(1,&redund->matseq);CHKERRQ(ierr); 1247 } else { 1248 ierr = PetscFree2(redund->send_rank,redund->recv_rank);CHKERRQ(ierr); 1249 ierr = PetscFree(redund->sbuf_j);CHKERRQ(ierr); 1250 ierr = PetscFree(redund->sbuf_a);CHKERRQ(ierr); 1251 for (i=0; i<redund->nrecvs; i++) { 1252 ierr = PetscFree(redund->rbuf_j[i]);CHKERRQ(ierr); 1253 ierr = PetscFree(redund->rbuf_a[i]);CHKERRQ(ierr); 1254 } 1255 ierr = PetscFree4(redund->sbuf_nz,redund->rbuf_nz,redund->rbuf_j,redund->rbuf_a);CHKERRQ(ierr); 1256 } 1257 1258 if (redund->subcomm) { 1259 ierr = PetscCommDestroy(&redund->subcomm);CHKERRQ(ierr); 1260 } 1261 ierr = PetscFree(redund);CHKERRQ(ierr); 1262 } 1263 PetscFunctionReturn(0); 1264 } 1265 1266 /*@C 1267 MatDestroy - Frees space taken by a matrix. 1268 1269 Collective on Mat 1270 1271 Input Parameter: 1272 . A - the matrix 1273 1274 Level: beginner 1275 1276 @*/ 1277 PetscErrorCode MatDestroy(Mat *A) 1278 { 1279 PetscErrorCode ierr; 1280 1281 PetscFunctionBegin; 1282 if (!*A) PetscFunctionReturn(0); 1283 PetscValidHeaderSpecific(*A,MAT_CLASSID,1); 1284 if (--((PetscObject)(*A))->refct > 0) {*A = NULL; PetscFunctionReturn(0);} 1285 1286 /* if memory was published with SAWs then destroy it */ 1287 ierr = PetscObjectSAWsViewOff((PetscObject)*A);CHKERRQ(ierr); 1288 if ((*A)->ops->destroy) { 1289 ierr = (*(*A)->ops->destroy)(*A);CHKERRQ(ierr); 1290 } 1291 1292 ierr = PetscFree((*A)->defaultvectype);CHKERRQ(ierr); 1293 ierr = PetscFree((*A)->bsizes);CHKERRQ(ierr); 1294 ierr = PetscFree((*A)->solvertype);CHKERRQ(ierr); 1295 for (PetscInt i=0; i<MAT_FACTOR_NUM_TYPES; i++) { 1296 ierr = PetscFree((*A)->preferredordering[i]);CHKERRQ(ierr); 1297 } 1298 ierr = MatDestroy_Redundant(&(*A)->redundant);CHKERRQ(ierr); 1299 ierr = MatProductClear(*A);CHKERRQ(ierr); 1300 ierr = MatNullSpaceDestroy(&(*A)->nullsp);CHKERRQ(ierr); 1301 ierr = MatNullSpaceDestroy(&(*A)->transnullsp);CHKERRQ(ierr); 1302 ierr = MatNullSpaceDestroy(&(*A)->nearnullsp);CHKERRQ(ierr); 1303 ierr = MatDestroy(&(*A)->schur);CHKERRQ(ierr); 1304 ierr = PetscLayoutDestroy(&(*A)->rmap);CHKERRQ(ierr); 1305 ierr = PetscLayoutDestroy(&(*A)->cmap);CHKERRQ(ierr); 1306 ierr = PetscHeaderDestroy(A);CHKERRQ(ierr); 1307 PetscFunctionReturn(0); 1308 } 1309 1310 /*@C 1311 MatSetValues - Inserts or adds a block of values into a matrix. 1312 These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd() 1313 MUST be called after all calls to MatSetValues() have been completed. 1314 1315 Not Collective 1316 1317 Input Parameters: 1318 + mat - the matrix 1319 . v - a logically two-dimensional array of values 1320 . m, idxm - the number of rows and their global indices 1321 . n, idxn - the number of columns and their global indices 1322 - addv - either ADD_VALUES or INSERT_VALUES, where 1323 ADD_VALUES adds values to any existing entries, and 1324 INSERT_VALUES replaces existing entries with new values 1325 1326 Notes: 1327 If you create the matrix yourself (that is not with a call to DMCreateMatrix()) then you MUST call MatXXXXSetPreallocation() or 1328 MatSetUp() before using this routine 1329 1330 By default the values, v, are row-oriented. See MatSetOption() for other options. 1331 1332 Calls to MatSetValues() with the INSERT_VALUES and ADD_VALUES 1333 options cannot be mixed without intervening calls to the assembly 1334 routines. 1335 1336 MatSetValues() uses 0-based row and column numbers in Fortran 1337 as well as in C. 1338 1339 Negative indices may be passed in idxm and idxn, these rows and columns are 1340 simply ignored. This allows easily inserting element stiffness matrices 1341 with homogeneous Dirchlet boundary conditions that you don't want represented 1342 in the matrix. 1343 1344 Efficiency Alert: 1345 The routine MatSetValuesBlocked() may offer much better efficiency 1346 for users of block sparse formats (MATSEQBAIJ and MATMPIBAIJ). 1347 1348 Level: beginner 1349 1350 Developer Notes: 1351 This is labeled with C so does not automatically generate Fortran stubs and interfaces 1352 because it requires multiple Fortran interfaces depending on which arguments are scalar or arrays. 1353 1354 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(), 1355 InsertMode, INSERT_VALUES, ADD_VALUES 1356 @*/ 1357 PetscErrorCode MatSetValues(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],const PetscScalar v[],InsertMode addv) 1358 { 1359 PetscErrorCode ierr; 1360 1361 PetscFunctionBeginHot; 1362 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 1363 PetscValidType(mat,1); 1364 if (!m || !n) PetscFunctionReturn(0); /* no values to insert */ 1365 PetscValidIntPointer(idxm,3); 1366 PetscValidIntPointer(idxn,5); 1367 MatCheckPreallocated(mat,1); 1368 1369 if (mat->insertmode == NOT_SET_VALUES) { 1370 mat->insertmode = addv; 1371 } else if (PetscUnlikely(mat->insertmode != addv)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values"); 1372 if (PetscDefined(USE_DEBUG)) { 1373 PetscInt i,j; 1374 1375 if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1376 if (!mat->ops->setvalues) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 1377 1378 for (i=0; i<m; i++) { 1379 for (j=0; j<n; j++) { 1380 if (mat->erroriffailure && PetscIsInfOrNanScalar(v[i*n+j])) 1381 #if defined(PETSC_USE_COMPLEX) 1382 SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_FP,"Inserting %g+ig at matrix entry (%D,%D)",(double)PetscRealPart(v[i*n+j]),(double)PetscImaginaryPart(v[i*n+j]),idxm[i],idxn[j]); 1383 #else 1384 SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_FP,"Inserting %g at matrix entry (%D,%D)",(double)v[i*n+j],idxm[i],idxn[j]); 1385 #endif 1386 } 1387 } 1388 for (i=0; i<m; i++) if (idxm[i] >= mat->rmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot insert in row %D, maximum is %D",idxm[i],mat->rmap->N-1); 1389 for (i=0; i<n; i++) if (idxn[i] >= mat->cmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot insert in column %D, maximum is %D",idxn[i],mat->cmap->N-1); 1390 } 1391 1392 if (mat->assembled) { 1393 mat->was_assembled = PETSC_TRUE; 1394 mat->assembled = PETSC_FALSE; 1395 } 1396 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 1397 ierr = (*mat->ops->setvalues)(mat,m,idxm,n,idxn,v,addv);CHKERRQ(ierr); 1398 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 1399 PetscFunctionReturn(0); 1400 } 1401 1402 /*@ 1403 MatSetValuesRowLocal - Inserts a row (block row for BAIJ matrices) of nonzero 1404 values into a matrix 1405 1406 Not Collective 1407 1408 Input Parameters: 1409 + mat - the matrix 1410 . row - the (block) row to set 1411 - v - a logically two-dimensional array of values 1412 1413 Notes: 1414 By the values, v, are column-oriented (for the block version) and sorted 1415 1416 All the nonzeros in the row must be provided 1417 1418 The matrix must have previously had its column indices set 1419 1420 The row must belong to this process 1421 1422 Level: intermediate 1423 1424 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(), 1425 InsertMode, INSERT_VALUES, ADD_VALUES, MatSetValues(), MatSetValuesRow(), MatSetLocalToGlobalMapping() 1426 @*/ 1427 PetscErrorCode MatSetValuesRowLocal(Mat mat,PetscInt row,const PetscScalar v[]) 1428 { 1429 PetscErrorCode ierr; 1430 PetscInt globalrow; 1431 1432 PetscFunctionBegin; 1433 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 1434 PetscValidType(mat,1); 1435 PetscValidScalarPointer(v,3); 1436 ierr = ISLocalToGlobalMappingApply(mat->rmap->mapping,1,&row,&globalrow);CHKERRQ(ierr); 1437 ierr = MatSetValuesRow(mat,globalrow,v);CHKERRQ(ierr); 1438 PetscFunctionReturn(0); 1439 } 1440 1441 /*@ 1442 MatSetValuesRow - Inserts a row (block row for BAIJ matrices) of nonzero 1443 values into a matrix 1444 1445 Not Collective 1446 1447 Input Parameters: 1448 + mat - the matrix 1449 . row - the (block) row to set 1450 - 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 1451 1452 Notes: 1453 The values, v, are column-oriented for the block version. 1454 1455 All the nonzeros in the row must be provided 1456 1457 THE MATRIX MUST HAVE PREVIOUSLY HAD ITS COLUMN INDICES SET. IT IS RARE THAT THIS ROUTINE IS USED, usually MatSetValues() is used. 1458 1459 The row must belong to this process 1460 1461 Level: advanced 1462 1463 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(), 1464 InsertMode, INSERT_VALUES, ADD_VALUES, MatSetValues() 1465 @*/ 1466 PetscErrorCode MatSetValuesRow(Mat mat,PetscInt row,const PetscScalar v[]) 1467 { 1468 PetscErrorCode ierr; 1469 1470 PetscFunctionBeginHot; 1471 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 1472 PetscValidType(mat,1); 1473 MatCheckPreallocated(mat,1); 1474 PetscValidScalarPointer(v,3); 1475 if (PetscUnlikely(mat->insertmode == ADD_VALUES)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add and insert values"); 1476 if (PetscUnlikely(mat->factortype)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1477 mat->insertmode = INSERT_VALUES; 1478 1479 if (mat->assembled) { 1480 mat->was_assembled = PETSC_TRUE; 1481 mat->assembled = PETSC_FALSE; 1482 } 1483 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 1484 if (!mat->ops->setvaluesrow) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 1485 ierr = (*mat->ops->setvaluesrow)(mat,row,v);CHKERRQ(ierr); 1486 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 1487 PetscFunctionReturn(0); 1488 } 1489 1490 /*@ 1491 MatSetValuesStencil - Inserts or adds a block of values into a matrix. 1492 Using structured grid indexing 1493 1494 Not Collective 1495 1496 Input Parameters: 1497 + mat - the matrix 1498 . m - number of rows being entered 1499 . idxm - grid coordinates (and component number when dof > 1) for matrix rows being entered 1500 . n - number of columns being entered 1501 . idxn - grid coordinates (and component number when dof > 1) for matrix columns being entered 1502 . v - a logically two-dimensional array of values 1503 - addv - either ADD_VALUES or INSERT_VALUES, where 1504 ADD_VALUES adds values to any existing entries, and 1505 INSERT_VALUES replaces existing entries with new values 1506 1507 Notes: 1508 By default the values, v, are row-oriented. See MatSetOption() for other options. 1509 1510 Calls to MatSetValuesStencil() with the INSERT_VALUES and ADD_VALUES 1511 options cannot be mixed without intervening calls to the assembly 1512 routines. 1513 1514 The grid coordinates are across the entire grid, not just the local portion 1515 1516 MatSetValuesStencil() uses 0-based row and column numbers in Fortran 1517 as well as in C. 1518 1519 For setting/accessing vector values via array coordinates you can use the DMDAVecGetArray() routine 1520 1521 In order to use this routine you must either obtain the matrix with DMCreateMatrix() 1522 or call MatSetLocalToGlobalMapping() and MatSetStencil() first. 1523 1524 The columns and rows in the stencil passed in MUST be contained within the 1525 ghost region of the given process as set with DMDACreateXXX() or MatSetStencil(). For example, 1526 if you create a DMDA with an overlap of one grid level and on a particular process its first 1527 local nonghost x logical coordinate is 6 (so its first ghost x logical coordinate is 5) the 1528 first i index you can use in your column and row indices in MatSetStencil() is 5. 1529 1530 In Fortran idxm and idxn should be declared as 1531 $ MatStencil idxm(4,m),idxn(4,n) 1532 and the values inserted using 1533 $ idxm(MatStencil_i,1) = i 1534 $ idxm(MatStencil_j,1) = j 1535 $ idxm(MatStencil_k,1) = k 1536 $ idxm(MatStencil_c,1) = c 1537 etc 1538 1539 For periodic boundary conditions use negative indices for values to the left (below 0; that are to be 1540 obtained by wrapping values from right edge). For values to the right of the last entry using that index plus one 1541 etc to obtain values that obtained by wrapping the values from the left edge. This does not work for anything but the 1542 DM_BOUNDARY_PERIODIC boundary type. 1543 1544 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 1545 a single value per point) you can skip filling those indices. 1546 1547 Inspired by the structured grid interface to the HYPRE package 1548 (https://computation.llnl.gov/projects/hypre-scalable-linear-solvers-multigrid-methods) 1549 1550 Efficiency Alert: 1551 The routine MatSetValuesBlockedStencil() may offer much better efficiency 1552 for users of block sparse formats (MATSEQBAIJ and MATMPIBAIJ). 1553 1554 Level: beginner 1555 1556 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal() 1557 MatSetValues(), MatSetValuesBlockedStencil(), MatSetStencil(), DMCreateMatrix(), DMDAVecGetArray(), MatStencil 1558 @*/ 1559 PetscErrorCode MatSetValuesStencil(Mat mat,PetscInt m,const MatStencil idxm[],PetscInt n,const MatStencil idxn[],const PetscScalar v[],InsertMode addv) 1560 { 1561 PetscErrorCode ierr; 1562 PetscInt buf[8192],*bufm=NULL,*bufn=NULL,*jdxm,*jdxn; 1563 PetscInt j,i,dim = mat->stencil.dim,*dims = mat->stencil.dims+1,tmp; 1564 PetscInt *starts = mat->stencil.starts,*dxm = (PetscInt*)idxm,*dxn = (PetscInt*)idxn,sdim = dim - (1 - (PetscInt)mat->stencil.noc); 1565 1566 PetscFunctionBegin; 1567 if (!m || !n) PetscFunctionReturn(0); /* no values to insert */ 1568 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 1569 PetscValidType(mat,1); 1570 PetscValidPointer(idxm,3); 1571 PetscValidPointer(idxn,5); 1572 1573 if ((m+n) <= (PetscInt)(sizeof(buf)/sizeof(PetscInt))) { 1574 jdxm = buf; jdxn = buf+m; 1575 } else { 1576 ierr = PetscMalloc2(m,&bufm,n,&bufn);CHKERRQ(ierr); 1577 jdxm = bufm; jdxn = bufn; 1578 } 1579 for (i=0; i<m; i++) { 1580 for (j=0; j<3-sdim; j++) dxm++; 1581 tmp = *dxm++ - starts[0]; 1582 for (j=0; j<dim-1; j++) { 1583 if ((*dxm++ - starts[j+1]) < 0 || tmp < 0) tmp = -1; 1584 else tmp = tmp*dims[j] + *(dxm-1) - starts[j+1]; 1585 } 1586 if (mat->stencil.noc) dxm++; 1587 jdxm[i] = tmp; 1588 } 1589 for (i=0; i<n; i++) { 1590 for (j=0; j<3-sdim; j++) dxn++; 1591 tmp = *dxn++ - starts[0]; 1592 for (j=0; j<dim-1; j++) { 1593 if ((*dxn++ - starts[j+1]) < 0 || tmp < 0) tmp = -1; 1594 else tmp = tmp*dims[j] + *(dxn-1) - starts[j+1]; 1595 } 1596 if (mat->stencil.noc) dxn++; 1597 jdxn[i] = tmp; 1598 } 1599 ierr = MatSetValuesLocal(mat,m,jdxm,n,jdxn,v,addv);CHKERRQ(ierr); 1600 ierr = PetscFree2(bufm,bufn);CHKERRQ(ierr); 1601 PetscFunctionReturn(0); 1602 } 1603 1604 /*@ 1605 MatSetValuesBlockedStencil - Inserts or adds a block of values into a matrix. 1606 Using structured grid indexing 1607 1608 Not Collective 1609 1610 Input Parameters: 1611 + mat - the matrix 1612 . m - number of rows being entered 1613 . idxm - grid coordinates for matrix rows being entered 1614 . n - number of columns being entered 1615 . idxn - grid coordinates for matrix columns being entered 1616 . v - a logically two-dimensional array of values 1617 - addv - either ADD_VALUES or INSERT_VALUES, where 1618 ADD_VALUES adds values to any existing entries, and 1619 INSERT_VALUES replaces existing entries with new values 1620 1621 Notes: 1622 By default the values, v, are row-oriented and unsorted. 1623 See MatSetOption() for other options. 1624 1625 Calls to MatSetValuesBlockedStencil() with the INSERT_VALUES and ADD_VALUES 1626 options cannot be mixed without intervening calls to the assembly 1627 routines. 1628 1629 The grid coordinates are across the entire grid, not just the local portion 1630 1631 MatSetValuesBlockedStencil() uses 0-based row and column numbers in Fortran 1632 as well as in C. 1633 1634 For setting/accessing vector values via array coordinates you can use the DMDAVecGetArray() routine 1635 1636 In order to use this routine you must either obtain the matrix with DMCreateMatrix() 1637 or call MatSetBlockSize(), MatSetLocalToGlobalMapping() and MatSetStencil() first. 1638 1639 The columns and rows in the stencil passed in MUST be contained within the 1640 ghost region of the given process as set with DMDACreateXXX() or MatSetStencil(). For example, 1641 if you create a DMDA with an overlap of one grid level and on a particular process its first 1642 local nonghost x logical coordinate is 6 (so its first ghost x logical coordinate is 5) the 1643 first i index you can use in your column and row indices in MatSetStencil() is 5. 1644 1645 In Fortran idxm and idxn should be declared as 1646 $ MatStencil idxm(4,m),idxn(4,n) 1647 and the values inserted using 1648 $ idxm(MatStencil_i,1) = i 1649 $ idxm(MatStencil_j,1) = j 1650 $ idxm(MatStencil_k,1) = k 1651 etc 1652 1653 Negative indices may be passed in idxm and idxn, these rows and columns are 1654 simply ignored. This allows easily inserting element stiffness matrices 1655 with homogeneous Dirchlet boundary conditions that you don't want represented 1656 in the matrix. 1657 1658 Inspired by the structured grid interface to the HYPRE package 1659 (https://computation.llnl.gov/projects/hypre-scalable-linear-solvers-multigrid-methods) 1660 1661 Level: beginner 1662 1663 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal() 1664 MatSetValues(), MatSetValuesStencil(), MatSetStencil(), DMCreateMatrix(), DMDAVecGetArray(), MatStencil, 1665 MatSetBlockSize(), MatSetLocalToGlobalMapping() 1666 @*/ 1667 PetscErrorCode MatSetValuesBlockedStencil(Mat mat,PetscInt m,const MatStencil idxm[],PetscInt n,const MatStencil idxn[],const PetscScalar v[],InsertMode addv) 1668 { 1669 PetscErrorCode ierr; 1670 PetscInt buf[8192],*bufm=NULL,*bufn=NULL,*jdxm,*jdxn; 1671 PetscInt j,i,dim = mat->stencil.dim,*dims = mat->stencil.dims+1,tmp; 1672 PetscInt *starts = mat->stencil.starts,*dxm = (PetscInt*)idxm,*dxn = (PetscInt*)idxn,sdim = dim - (1 - (PetscInt)mat->stencil.noc); 1673 1674 PetscFunctionBegin; 1675 if (!m || !n) PetscFunctionReturn(0); /* no values to insert */ 1676 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 1677 PetscValidType(mat,1); 1678 PetscValidPointer(idxm,3); 1679 PetscValidPointer(idxn,5); 1680 PetscValidScalarPointer(v,6); 1681 1682 if ((m+n) <= (PetscInt)(sizeof(buf)/sizeof(PetscInt))) { 1683 jdxm = buf; jdxn = buf+m; 1684 } else { 1685 ierr = PetscMalloc2(m,&bufm,n,&bufn);CHKERRQ(ierr); 1686 jdxm = bufm; jdxn = bufn; 1687 } 1688 for (i=0; i<m; i++) { 1689 for (j=0; j<3-sdim; j++) dxm++; 1690 tmp = *dxm++ - starts[0]; 1691 for (j=0; j<sdim-1; j++) { 1692 if ((*dxm++ - starts[j+1]) < 0 || tmp < 0) tmp = -1; 1693 else tmp = tmp*dims[j] + *(dxm-1) - starts[j+1]; 1694 } 1695 dxm++; 1696 jdxm[i] = tmp; 1697 } 1698 for (i=0; i<n; i++) { 1699 for (j=0; j<3-sdim; j++) dxn++; 1700 tmp = *dxn++ - starts[0]; 1701 for (j=0; j<sdim-1; j++) { 1702 if ((*dxn++ - starts[j+1]) < 0 || tmp < 0) tmp = -1; 1703 else tmp = tmp*dims[j] + *(dxn-1) - starts[j+1]; 1704 } 1705 dxn++; 1706 jdxn[i] = tmp; 1707 } 1708 ierr = MatSetValuesBlockedLocal(mat,m,jdxm,n,jdxn,v,addv);CHKERRQ(ierr); 1709 ierr = PetscFree2(bufm,bufn);CHKERRQ(ierr); 1710 PetscFunctionReturn(0); 1711 } 1712 1713 /*@ 1714 MatSetStencil - Sets the grid information for setting values into a matrix via 1715 MatSetValuesStencil() 1716 1717 Not Collective 1718 1719 Input Parameters: 1720 + mat - the matrix 1721 . dim - dimension of the grid 1, 2, or 3 1722 . dims - number of grid points in x, y, and z direction, including ghost points on your processor 1723 . starts - starting point of ghost nodes on your processor in x, y, and z direction 1724 - dof - number of degrees of freedom per node 1725 1726 Inspired by the structured grid interface to the HYPRE package 1727 (www.llnl.gov/CASC/hyper) 1728 1729 For matrices generated with DMCreateMatrix() this routine is automatically called and so not needed by the 1730 user. 1731 1732 Level: beginner 1733 1734 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal() 1735 MatSetValues(), MatSetValuesBlockedStencil(), MatSetValuesStencil() 1736 @*/ 1737 PetscErrorCode MatSetStencil(Mat mat,PetscInt dim,const PetscInt dims[],const PetscInt starts[],PetscInt dof) 1738 { 1739 PetscInt i; 1740 1741 PetscFunctionBegin; 1742 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 1743 PetscValidIntPointer(dims,3); 1744 PetscValidIntPointer(starts,4); 1745 1746 mat->stencil.dim = dim + (dof > 1); 1747 for (i=0; i<dim; i++) { 1748 mat->stencil.dims[i] = dims[dim-i-1]; /* copy the values in backwards */ 1749 mat->stencil.starts[i] = starts[dim-i-1]; 1750 } 1751 mat->stencil.dims[dim] = dof; 1752 mat->stencil.starts[dim] = 0; 1753 mat->stencil.noc = (PetscBool)(dof == 1); 1754 PetscFunctionReturn(0); 1755 } 1756 1757 /*@C 1758 MatSetValuesBlocked - Inserts or adds a block of values into a matrix. 1759 1760 Not Collective 1761 1762 Input Parameters: 1763 + mat - the matrix 1764 . v - a logically two-dimensional array of values 1765 . m, idxm - the number of block rows and their global block indices 1766 . n, idxn - the number of block columns and their global block indices 1767 - addv - either ADD_VALUES or INSERT_VALUES, where 1768 ADD_VALUES adds values to any existing entries, and 1769 INSERT_VALUES replaces existing entries with new values 1770 1771 Notes: 1772 If you create the matrix yourself (that is not with a call to DMCreateMatrix()) then you MUST call 1773 MatXXXXSetPreallocation() or MatSetUp() before using this routine. 1774 1775 The m and n count the NUMBER of blocks in the row direction and column direction, 1776 NOT the total number of rows/columns; for example, if the block size is 2 and 1777 you are passing in values for rows 2,3,4,5 then m would be 2 (not 4). 1778 The values in idxm would be 1 2; that is the first index for each block divided by 1779 the block size. 1780 1781 Note that you must call MatSetBlockSize() when constructing this matrix (before 1782 preallocating it). 1783 1784 By default the values, v, are row-oriented, so the layout of 1785 v is the same as for MatSetValues(). See MatSetOption() for other options. 1786 1787 Calls to MatSetValuesBlocked() with the INSERT_VALUES and ADD_VALUES 1788 options cannot be mixed without intervening calls to the assembly 1789 routines. 1790 1791 MatSetValuesBlocked() uses 0-based row and column numbers in Fortran 1792 as well as in C. 1793 1794 Negative indices may be passed in idxm and idxn, these rows and columns are 1795 simply ignored. This allows easily inserting element stiffness matrices 1796 with homogeneous Dirchlet boundary conditions that you don't want represented 1797 in the matrix. 1798 1799 Each time an entry is set within a sparse matrix via MatSetValues(), 1800 internal searching must be done to determine where to place the 1801 data in the matrix storage space. By instead inserting blocks of 1802 entries via MatSetValuesBlocked(), the overhead of matrix assembly is 1803 reduced. 1804 1805 Example: 1806 $ Suppose m=n=2 and block size(bs) = 2 The array is 1807 $ 1808 $ 1 2 | 3 4 1809 $ 5 6 | 7 8 1810 $ - - - | - - - 1811 $ 9 10 | 11 12 1812 $ 13 14 | 15 16 1813 $ 1814 $ v[] should be passed in like 1815 $ v[] = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16] 1816 $ 1817 $ If you are not using row oriented storage of v (that is you called MatSetOption(mat,MAT_ROW_ORIENTED,PETSC_FALSE)) then 1818 $ v[] = [1,5,9,13,2,6,10,14,3,7,11,15,4,8,12,16] 1819 1820 Level: intermediate 1821 1822 .seealso: MatSetBlockSize(), MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesBlockedLocal() 1823 @*/ 1824 PetscErrorCode MatSetValuesBlocked(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],const PetscScalar v[],InsertMode addv) 1825 { 1826 PetscErrorCode ierr; 1827 1828 PetscFunctionBeginHot; 1829 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 1830 PetscValidType(mat,1); 1831 if (!m || !n) PetscFunctionReturn(0); /* no values to insert */ 1832 PetscValidIntPointer(idxm,3); 1833 PetscValidIntPointer(idxn,5); 1834 PetscValidScalarPointer(v,6); 1835 MatCheckPreallocated(mat,1); 1836 if (mat->insertmode == NOT_SET_VALUES) { 1837 mat->insertmode = addv; 1838 } else if (PetscUnlikely(mat->insertmode != addv)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values"); 1839 if (PetscDefined(USE_DEBUG)) { 1840 if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1841 if (!mat->ops->setvaluesblocked && !mat->ops->setvalues) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 1842 } 1843 if (PetscDefined(USE_DEBUG)) { 1844 PetscInt rbs,cbs,M,N,i; 1845 ierr = MatGetBlockSizes(mat,&rbs,&cbs);CHKERRQ(ierr); 1846 ierr = MatGetSize(mat,&M,&N);CHKERRQ(ierr); 1847 for (i=0; i<m; i++) { 1848 if (idxm[i]*rbs >= M) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row block index %D (index %D) greater than row length %D",i,idxm[i],M); 1849 } 1850 for (i=0; i<n; i++) { 1851 if (idxn[i]*cbs >= N) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column block index %D (index %D) great than column length %D",i,idxn[i],N); 1852 } 1853 } 1854 if (mat->assembled) { 1855 mat->was_assembled = PETSC_TRUE; 1856 mat->assembled = PETSC_FALSE; 1857 } 1858 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 1859 if (mat->ops->setvaluesblocked) { 1860 ierr = (*mat->ops->setvaluesblocked)(mat,m,idxm,n,idxn,v,addv);CHKERRQ(ierr); 1861 } else { 1862 PetscInt buf[8192],*bufr=NULL,*bufc=NULL,*iidxm,*iidxn; 1863 PetscInt i,j,bs,cbs; 1864 ierr = MatGetBlockSizes(mat,&bs,&cbs);CHKERRQ(ierr); 1865 if (m*bs+n*cbs <= (PetscInt)(sizeof(buf)/sizeof(PetscInt))) { 1866 iidxm = buf; iidxn = buf + m*bs; 1867 } else { 1868 ierr = PetscMalloc2(m*bs,&bufr,n*cbs,&bufc);CHKERRQ(ierr); 1869 iidxm = bufr; iidxn = bufc; 1870 } 1871 for (i=0; i<m; i++) { 1872 for (j=0; j<bs; j++) { 1873 iidxm[i*bs+j] = bs*idxm[i] + j; 1874 } 1875 } 1876 for (i=0; i<n; i++) { 1877 for (j=0; j<cbs; j++) { 1878 iidxn[i*cbs+j] = cbs*idxn[i] + j; 1879 } 1880 } 1881 ierr = MatSetValues(mat,m*bs,iidxm,n*cbs,iidxn,v,addv);CHKERRQ(ierr); 1882 ierr = PetscFree2(bufr,bufc);CHKERRQ(ierr); 1883 } 1884 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 1885 PetscFunctionReturn(0); 1886 } 1887 1888 /*@C 1889 MatGetValues - Gets a block of values from a matrix. 1890 1891 Not Collective; can only return values that are owned by the give process 1892 1893 Input Parameters: 1894 + mat - the matrix 1895 . v - a logically two-dimensional array for storing the values 1896 . m, idxm - the number of rows and their global indices 1897 - n, idxn - the number of columns and their global indices 1898 1899 Notes: 1900 The user must allocate space (m*n PetscScalars) for the values, v. 1901 The values, v, are then returned in a row-oriented format, 1902 analogous to that used by default in MatSetValues(). 1903 1904 MatGetValues() uses 0-based row and column numbers in 1905 Fortran as well as in C. 1906 1907 MatGetValues() requires that the matrix has been assembled 1908 with MatAssemblyBegin()/MatAssemblyEnd(). Thus, calls to 1909 MatSetValues() and MatGetValues() CANNOT be made in succession 1910 without intermediate matrix assembly. 1911 1912 Negative row or column indices will be ignored and those locations in v[] will be 1913 left unchanged. 1914 1915 For the standard row-based matrix formats, idxm[] can only contain rows owned by the requesting MPI rank. 1916 That is, rows with global index greater than or equal to restart and less than rend where restart and rend are obtainable 1917 from MatGetOwnershipRange(mat,&rstart,&rend). 1918 1919 Level: advanced 1920 1921 .seealso: MatGetRow(), MatCreateSubMatrices(), MatSetValues(), MatGetOwnershipRange(), MatGetValuesLocal() 1922 @*/ 1923 PetscErrorCode MatGetValues(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],PetscScalar v[]) 1924 { 1925 PetscErrorCode ierr; 1926 1927 PetscFunctionBegin; 1928 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 1929 PetscValidType(mat,1); 1930 if (!m || !n) PetscFunctionReturn(0); 1931 PetscValidIntPointer(idxm,3); 1932 PetscValidIntPointer(idxn,5); 1933 PetscValidScalarPointer(v,6); 1934 if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1935 if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1936 if (!mat->ops->getvalues) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 1937 MatCheckPreallocated(mat,1); 1938 1939 ierr = PetscLogEventBegin(MAT_GetValues,mat,0,0,0);CHKERRQ(ierr); 1940 ierr = (*mat->ops->getvalues)(mat,m,idxm,n,idxn,v);CHKERRQ(ierr); 1941 ierr = PetscLogEventEnd(MAT_GetValues,mat,0,0,0);CHKERRQ(ierr); 1942 PetscFunctionReturn(0); 1943 } 1944 1945 /*@C 1946 MatGetValuesLocal - retrieves values from certain locations in a matrix using the local numbering of the indices 1947 defined previously by MatSetLocalToGlobalMapping() 1948 1949 Not Collective 1950 1951 Input Parameters: 1952 + mat - the matrix 1953 . nrow, irow - number of rows and their local indices 1954 - ncol, icol - number of columns and their local indices 1955 1956 Output Parameter: 1957 . y - a logically two-dimensional array of values 1958 1959 Notes: 1960 If you create the matrix yourself (that is not with a call to DMCreateMatrix()) then you MUST call MatSetLocalToGlobalMapping() before using this routine. 1961 1962 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, 1963 are greater than or equal to restart and less than rend where restart and rend are obtainable from MatGetOwnershipRange(mat,&rstart,&rend). One can 1964 determine if the resulting global row associated with the local row r is owned by the requesting MPI rank by applying the ISLocalToGlobalMapping set 1965 with MatSetLocalToGlobalMapping(). 1966 1967 Developer Notes: 1968 This is labelled with C so does not automatically generate Fortran stubs and interfaces 1969 because it requires multiple Fortran interfaces depending on which arguments are scalar or arrays. 1970 1971 Level: advanced 1972 1973 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetLocalToGlobalMapping(), 1974 MatSetValuesLocal(), MatGetValues() 1975 @*/ 1976 PetscErrorCode MatGetValuesLocal(Mat mat,PetscInt nrow,const PetscInt irow[],PetscInt ncol,const PetscInt icol[],PetscScalar y[]) 1977 { 1978 PetscErrorCode ierr; 1979 1980 PetscFunctionBeginHot; 1981 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 1982 PetscValidType(mat,1); 1983 MatCheckPreallocated(mat,1); 1984 if (!nrow || !ncol) PetscFunctionReturn(0); /* no values to retrieve */ 1985 PetscValidIntPointer(irow,3); 1986 PetscValidIntPointer(icol,5); 1987 if (PetscDefined(USE_DEBUG)) { 1988 if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1989 if (!mat->ops->getvalueslocal && !mat->ops->getvalues) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 1990 } 1991 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1992 ierr = PetscLogEventBegin(MAT_GetValues,mat,0,0,0);CHKERRQ(ierr); 1993 if (mat->ops->getvalueslocal) { 1994 ierr = (*mat->ops->getvalueslocal)(mat,nrow,irow,ncol,icol,y);CHKERRQ(ierr); 1995 } else { 1996 PetscInt buf[8192],*bufr=NULL,*bufc=NULL,*irowm,*icolm; 1997 if ((nrow+ncol) <= (PetscInt)(sizeof(buf)/sizeof(PetscInt))) { 1998 irowm = buf; icolm = buf+nrow; 1999 } else { 2000 ierr = PetscMalloc2(nrow,&bufr,ncol,&bufc);CHKERRQ(ierr); 2001 irowm = bufr; icolm = bufc; 2002 } 2003 if (!mat->rmap->mapping) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"MatGetValuesLocal() cannot proceed without local-to-global row mapping (See MatSetLocalToGlobalMapping())."); 2004 if (!mat->cmap->mapping) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"MatGetValuesLocal() cannot proceed without local-to-global column mapping (See MatSetLocalToGlobalMapping())."); 2005 ierr = ISLocalToGlobalMappingApply(mat->rmap->mapping,nrow,irow,irowm);CHKERRQ(ierr); 2006 ierr = ISLocalToGlobalMappingApply(mat->cmap->mapping,ncol,icol,icolm);CHKERRQ(ierr); 2007 ierr = MatGetValues(mat,nrow,irowm,ncol,icolm,y);CHKERRQ(ierr); 2008 ierr = PetscFree2(bufr,bufc);CHKERRQ(ierr); 2009 } 2010 ierr = PetscLogEventEnd(MAT_GetValues,mat,0,0,0);CHKERRQ(ierr); 2011 PetscFunctionReturn(0); 2012 } 2013 2014 /*@ 2015 MatSetValuesBatch - Adds (ADD_VALUES) many blocks of values into a matrix at once. The blocks must all be square and 2016 the same size. Currently, this can only be called once and creates the given matrix. 2017 2018 Not Collective 2019 2020 Input Parameters: 2021 + mat - the matrix 2022 . nb - the number of blocks 2023 . bs - the number of rows (and columns) in each block 2024 . rows - a concatenation of the rows for each block 2025 - v - a concatenation of logically two-dimensional arrays of values 2026 2027 Notes: 2028 In the future, we will extend this routine to handle rectangular blocks, and to allow multiple calls for a given matrix. 2029 2030 Level: advanced 2031 2032 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(), 2033 InsertMode, INSERT_VALUES, ADD_VALUES, MatSetValues() 2034 @*/ 2035 PetscErrorCode MatSetValuesBatch(Mat mat, PetscInt nb, PetscInt bs, PetscInt rows[], const PetscScalar v[]) 2036 { 2037 PetscErrorCode ierr; 2038 2039 PetscFunctionBegin; 2040 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2041 PetscValidType(mat,1); 2042 PetscValidIntPointer(rows,4); 2043 PetscValidScalarPointer(v,5); 2044 if (PetscUnlikelyDebug(mat->factortype)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2045 2046 ierr = PetscLogEventBegin(MAT_SetValuesBatch,mat,0,0,0);CHKERRQ(ierr); 2047 if (mat->ops->setvaluesbatch) { 2048 ierr = (*mat->ops->setvaluesbatch)(mat,nb,bs,rows,v);CHKERRQ(ierr); 2049 } else { 2050 PetscInt b; 2051 for (b = 0; b < nb; ++b) { 2052 ierr = MatSetValues(mat, bs, &rows[b*bs], bs, &rows[b*bs], &v[b*bs*bs], ADD_VALUES);CHKERRQ(ierr); 2053 } 2054 } 2055 ierr = PetscLogEventEnd(MAT_SetValuesBatch,mat,0,0,0);CHKERRQ(ierr); 2056 PetscFunctionReturn(0); 2057 } 2058 2059 /*@ 2060 MatSetLocalToGlobalMapping - Sets a local-to-global numbering for use by 2061 the routine MatSetValuesLocal() to allow users to insert matrix entries 2062 using a local (per-processor) numbering. 2063 2064 Not Collective 2065 2066 Input Parameters: 2067 + x - the matrix 2068 . rmapping - row mapping created with ISLocalToGlobalMappingCreate() or ISLocalToGlobalMappingCreateIS() 2069 - cmapping - column mapping 2070 2071 Level: intermediate 2072 2073 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesLocal(), MatGetValuesLocal() 2074 @*/ 2075 PetscErrorCode MatSetLocalToGlobalMapping(Mat x,ISLocalToGlobalMapping rmapping,ISLocalToGlobalMapping cmapping) 2076 { 2077 PetscErrorCode ierr; 2078 2079 PetscFunctionBegin; 2080 PetscValidHeaderSpecific(x,MAT_CLASSID,1); 2081 PetscValidType(x,1); 2082 PetscValidHeaderSpecific(rmapping,IS_LTOGM_CLASSID,2); 2083 PetscValidHeaderSpecific(cmapping,IS_LTOGM_CLASSID,3); 2084 2085 if (x->ops->setlocaltoglobalmapping) { 2086 ierr = (*x->ops->setlocaltoglobalmapping)(x,rmapping,cmapping);CHKERRQ(ierr); 2087 } else { 2088 ierr = PetscLayoutSetISLocalToGlobalMapping(x->rmap,rmapping);CHKERRQ(ierr); 2089 ierr = PetscLayoutSetISLocalToGlobalMapping(x->cmap,cmapping);CHKERRQ(ierr); 2090 } 2091 PetscFunctionReturn(0); 2092 } 2093 2094 /*@ 2095 MatGetLocalToGlobalMapping - Gets the local-to-global numbering set by MatSetLocalToGlobalMapping() 2096 2097 Not Collective 2098 2099 Input Parameters: 2100 . A - the matrix 2101 2102 Output Parameters: 2103 + rmapping - row mapping 2104 - cmapping - column mapping 2105 2106 Level: advanced 2107 2108 .seealso: MatSetValuesLocal() 2109 @*/ 2110 PetscErrorCode MatGetLocalToGlobalMapping(Mat A,ISLocalToGlobalMapping *rmapping,ISLocalToGlobalMapping *cmapping) 2111 { 2112 PetscFunctionBegin; 2113 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 2114 PetscValidType(A,1); 2115 if (rmapping) PetscValidPointer(rmapping,2); 2116 if (cmapping) PetscValidPointer(cmapping,3); 2117 if (rmapping) *rmapping = A->rmap->mapping; 2118 if (cmapping) *cmapping = A->cmap->mapping; 2119 PetscFunctionReturn(0); 2120 } 2121 2122 /*@ 2123 MatSetLayouts - Sets the PetscLayout objects for rows and columns of a matrix 2124 2125 Logically Collective on A 2126 2127 Input Parameters: 2128 + A - the matrix 2129 . rmap - row layout 2130 - cmap - column layout 2131 2132 Level: advanced 2133 2134 .seealso: MatCreateVecs(), MatGetLocalToGlobalMapping(), MatGetLayouts() 2135 @*/ 2136 PetscErrorCode MatSetLayouts(Mat A,PetscLayout rmap,PetscLayout cmap) 2137 { 2138 PetscErrorCode ierr; 2139 2140 PetscFunctionBegin; 2141 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 2142 2143 ierr = PetscLayoutReference(rmap,&A->rmap);CHKERRQ(ierr); 2144 ierr = PetscLayoutReference(cmap,&A->cmap);CHKERRQ(ierr); 2145 PetscFunctionReturn(0); 2146 } 2147 2148 /*@ 2149 MatGetLayouts - Gets the PetscLayout objects for rows and columns 2150 2151 Not Collective 2152 2153 Input Parameters: 2154 . A - the matrix 2155 2156 Output Parameters: 2157 + rmap - row layout 2158 - cmap - column layout 2159 2160 Level: advanced 2161 2162 .seealso: MatCreateVecs(), MatGetLocalToGlobalMapping(), MatSetLayouts() 2163 @*/ 2164 PetscErrorCode MatGetLayouts(Mat A,PetscLayout *rmap,PetscLayout *cmap) 2165 { 2166 PetscFunctionBegin; 2167 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 2168 PetscValidType(A,1); 2169 if (rmap) PetscValidPointer(rmap,2); 2170 if (cmap) PetscValidPointer(cmap,3); 2171 if (rmap) *rmap = A->rmap; 2172 if (cmap) *cmap = A->cmap; 2173 PetscFunctionReturn(0); 2174 } 2175 2176 /*@C 2177 MatSetValuesLocal - Inserts or adds values into certain locations of a matrix, 2178 using a local numbering of the nodes. 2179 2180 Not Collective 2181 2182 Input Parameters: 2183 + mat - the matrix 2184 . nrow, irow - number of rows and their local indices 2185 . ncol, icol - number of columns and their local indices 2186 . y - a logically two-dimensional array of values 2187 - addv - either INSERT_VALUES or ADD_VALUES, where 2188 ADD_VALUES adds values to any existing entries, and 2189 INSERT_VALUES replaces existing entries with new values 2190 2191 Notes: 2192 If you create the matrix yourself (that is not with a call to DMCreateMatrix()) then you MUST call MatXXXXSetPreallocation() or 2193 MatSetUp() before using this routine 2194 2195 If you create the matrix yourself (that is not with a call to DMCreateMatrix()) then you MUST call MatSetLocalToGlobalMapping() before using this routine 2196 2197 Calls to MatSetValuesLocal() with the INSERT_VALUES and ADD_VALUES 2198 options cannot be mixed without intervening calls to the assembly 2199 routines. 2200 2201 These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd() 2202 MUST be called after all calls to MatSetValuesLocal() have been completed. 2203 2204 Level: intermediate 2205 2206 Developer Notes: 2207 This is labeled with C so does not automatically generate Fortran stubs and interfaces 2208 because it requires multiple Fortran interfaces depending on which arguments are scalar or arrays. 2209 2210 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetLocalToGlobalMapping(), 2211 MatSetValueLocal(), MatGetValuesLocal() 2212 @*/ 2213 PetscErrorCode MatSetValuesLocal(Mat mat,PetscInt nrow,const PetscInt irow[],PetscInt ncol,const PetscInt icol[],const PetscScalar y[],InsertMode addv) 2214 { 2215 PetscErrorCode ierr; 2216 2217 PetscFunctionBeginHot; 2218 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2219 PetscValidType(mat,1); 2220 MatCheckPreallocated(mat,1); 2221 if (!nrow || !ncol) PetscFunctionReturn(0); /* no values to insert */ 2222 PetscValidIntPointer(irow,3); 2223 PetscValidIntPointer(icol,5); 2224 if (mat->insertmode == NOT_SET_VALUES) { 2225 mat->insertmode = addv; 2226 } 2227 else if (PetscUnlikely(mat->insertmode != addv)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values"); 2228 if (PetscDefined(USE_DEBUG)) { 2229 if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2230 if (!mat->ops->setvalueslocal && !mat->ops->setvalues) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 2231 } 2232 2233 if (mat->assembled) { 2234 mat->was_assembled = PETSC_TRUE; 2235 mat->assembled = PETSC_FALSE; 2236 } 2237 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 2238 if (mat->ops->setvalueslocal) { 2239 ierr = (*mat->ops->setvalueslocal)(mat,nrow,irow,ncol,icol,y,addv);CHKERRQ(ierr); 2240 } else { 2241 PetscInt buf[8192],*bufr=NULL,*bufc=NULL,*irowm,*icolm; 2242 if ((nrow+ncol) <= (PetscInt)(sizeof(buf)/sizeof(PetscInt))) { 2243 irowm = buf; icolm = buf+nrow; 2244 } else { 2245 ierr = PetscMalloc2(nrow,&bufr,ncol,&bufc);CHKERRQ(ierr); 2246 irowm = bufr; icolm = bufc; 2247 } 2248 if (!mat->rmap->mapping) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"MatSetValuesLocal() cannot proceed without local-to-global row mapping (See MatSetLocalToGlobalMapping())."); 2249 if (!mat->cmap->mapping) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"MatSetValuesLocal() cannot proceed without local-to-global column mapping (See MatSetLocalToGlobalMapping())."); 2250 ierr = ISLocalToGlobalMappingApply(mat->rmap->mapping,nrow,irow,irowm);CHKERRQ(ierr); 2251 ierr = ISLocalToGlobalMappingApply(mat->cmap->mapping,ncol,icol,icolm);CHKERRQ(ierr); 2252 ierr = MatSetValues(mat,nrow,irowm,ncol,icolm,y,addv);CHKERRQ(ierr); 2253 ierr = PetscFree2(bufr,bufc);CHKERRQ(ierr); 2254 } 2255 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 2256 PetscFunctionReturn(0); 2257 } 2258 2259 /*@C 2260 MatSetValuesBlockedLocal - Inserts or adds values into certain locations of a matrix, 2261 using a local ordering of the nodes a block at a time. 2262 2263 Not Collective 2264 2265 Input Parameters: 2266 + x - the matrix 2267 . nrow, irow - number of rows and their local indices 2268 . ncol, icol - number of columns and their local indices 2269 . y - a logically two-dimensional array of values 2270 - addv - either INSERT_VALUES or ADD_VALUES, where 2271 ADD_VALUES adds values to any existing entries, and 2272 INSERT_VALUES replaces existing entries with new values 2273 2274 Notes: 2275 If you create the matrix yourself (that is not with a call to DMCreateMatrix()) then you MUST call MatXXXXSetPreallocation() or 2276 MatSetUp() before using this routine 2277 2278 If you create the matrix yourself (that is not with a call to DMCreateMatrix()) then you MUST call MatSetBlockSize() and MatSetLocalToGlobalMapping() 2279 before using this routineBefore calling MatSetValuesLocal(), the user must first set the 2280 2281 Calls to MatSetValuesBlockedLocal() with the INSERT_VALUES and ADD_VALUES 2282 options cannot be mixed without intervening calls to the assembly 2283 routines. 2284 2285 These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd() 2286 MUST be called after all calls to MatSetValuesBlockedLocal() have been completed. 2287 2288 Level: intermediate 2289 2290 Developer Notes: 2291 This is labeled with C so does not automatically generate Fortran stubs and interfaces 2292 because it requires multiple Fortran interfaces depending on which arguments are scalar or arrays. 2293 2294 .seealso: MatSetBlockSize(), MatSetLocalToGlobalMapping(), MatAssemblyBegin(), MatAssemblyEnd(), 2295 MatSetValuesLocal(), MatSetValuesBlocked() 2296 @*/ 2297 PetscErrorCode MatSetValuesBlockedLocal(Mat mat,PetscInt nrow,const PetscInt irow[],PetscInt ncol,const PetscInt icol[],const PetscScalar y[],InsertMode addv) 2298 { 2299 PetscErrorCode ierr; 2300 2301 PetscFunctionBeginHot; 2302 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2303 PetscValidType(mat,1); 2304 MatCheckPreallocated(mat,1); 2305 if (!nrow || !ncol) PetscFunctionReturn(0); /* no values to insert */ 2306 PetscValidIntPointer(irow,3); 2307 PetscValidIntPointer(icol,5); 2308 PetscValidScalarPointer(y,6); 2309 if (mat->insertmode == NOT_SET_VALUES) { 2310 mat->insertmode = addv; 2311 } else if (PetscUnlikely(mat->insertmode != addv)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values"); 2312 if (PetscDefined(USE_DEBUG)) { 2313 if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2314 if (!mat->ops->setvaluesblockedlocal && !mat->ops->setvaluesblocked && !mat->ops->setvalueslocal && !mat->ops->setvalues) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 2315 } 2316 2317 if (mat->assembled) { 2318 mat->was_assembled = PETSC_TRUE; 2319 mat->assembled = PETSC_FALSE; 2320 } 2321 if (PetscUnlikelyDebug(mat->rmap->mapping)) { /* Condition on the mapping existing, because MatSetValuesBlockedLocal_IS does not require it to be set. */ 2322 PetscInt irbs, rbs; 2323 ierr = MatGetBlockSizes(mat, &rbs, NULL);CHKERRQ(ierr); 2324 ierr = ISLocalToGlobalMappingGetBlockSize(mat->rmap->mapping,&irbs);CHKERRQ(ierr); 2325 if (rbs != irbs) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Different row block sizes! mat %D, row l2g map %D",rbs,irbs); 2326 } 2327 if (PetscUnlikelyDebug(mat->cmap->mapping)) { 2328 PetscInt icbs, cbs; 2329 ierr = MatGetBlockSizes(mat,NULL,&cbs);CHKERRQ(ierr); 2330 ierr = ISLocalToGlobalMappingGetBlockSize(mat->cmap->mapping,&icbs);CHKERRQ(ierr); 2331 if (cbs != icbs) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Different col block sizes! mat %D, col l2g map %D",cbs,icbs); 2332 } 2333 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 2334 if (mat->ops->setvaluesblockedlocal) { 2335 ierr = (*mat->ops->setvaluesblockedlocal)(mat,nrow,irow,ncol,icol,y,addv);CHKERRQ(ierr); 2336 } else { 2337 PetscInt buf[8192],*bufr=NULL,*bufc=NULL,*irowm,*icolm; 2338 if ((nrow+ncol) <= (PetscInt)(sizeof(buf)/sizeof(PetscInt))) { 2339 irowm = buf; icolm = buf + nrow; 2340 } else { 2341 ierr = PetscMalloc2(nrow,&bufr,ncol,&bufc);CHKERRQ(ierr); 2342 irowm = bufr; icolm = bufc; 2343 } 2344 ierr = ISLocalToGlobalMappingApplyBlock(mat->rmap->mapping,nrow,irow,irowm);CHKERRQ(ierr); 2345 ierr = ISLocalToGlobalMappingApplyBlock(mat->cmap->mapping,ncol,icol,icolm);CHKERRQ(ierr); 2346 ierr = MatSetValuesBlocked(mat,nrow,irowm,ncol,icolm,y,addv);CHKERRQ(ierr); 2347 ierr = PetscFree2(bufr,bufc);CHKERRQ(ierr); 2348 } 2349 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 2350 PetscFunctionReturn(0); 2351 } 2352 2353 /*@ 2354 MatMultDiagonalBlock - Computes the matrix-vector product, y = Dx. Where D is defined by the inode or block structure of the diagonal 2355 2356 Collective on Mat 2357 2358 Input Parameters: 2359 + mat - the matrix 2360 - x - the vector to be multiplied 2361 2362 Output Parameters: 2363 . y - the result 2364 2365 Notes: 2366 The vectors x and y cannot be the same. I.e., one cannot 2367 call MatMult(A,y,y). 2368 2369 Level: developer 2370 2371 .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd() 2372 @*/ 2373 PetscErrorCode MatMultDiagonalBlock(Mat mat,Vec x,Vec y) 2374 { 2375 PetscErrorCode ierr; 2376 2377 PetscFunctionBegin; 2378 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2379 PetscValidType(mat,1); 2380 PetscValidHeaderSpecific(x,VEC_CLASSID,2); 2381 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 2382 2383 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2384 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2385 if (x == y) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors"); 2386 MatCheckPreallocated(mat,1); 2387 2388 if (!mat->ops->multdiagonalblock) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s does not have a multiply defined",((PetscObject)mat)->type_name); 2389 ierr = (*mat->ops->multdiagonalblock)(mat,x,y);CHKERRQ(ierr); 2390 ierr = PetscObjectStateIncrease((PetscObject)y);CHKERRQ(ierr); 2391 PetscFunctionReturn(0); 2392 } 2393 2394 /* --------------------------------------------------------*/ 2395 /*@ 2396 MatMult - Computes the matrix-vector product, y = Ax. 2397 2398 Neighbor-wise Collective on Mat 2399 2400 Input Parameters: 2401 + mat - the matrix 2402 - x - the vector to be multiplied 2403 2404 Output Parameters: 2405 . y - the result 2406 2407 Notes: 2408 The vectors x and y cannot be the same. I.e., one cannot 2409 call MatMult(A,y,y). 2410 2411 Level: beginner 2412 2413 .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd() 2414 @*/ 2415 PetscErrorCode MatMult(Mat mat,Vec x,Vec y) 2416 { 2417 PetscErrorCode ierr; 2418 2419 PetscFunctionBegin; 2420 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2421 PetscValidType(mat,1); 2422 PetscValidHeaderSpecific(x,VEC_CLASSID,2); 2423 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 2424 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2425 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2426 if (x == y) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors"); 2427 #if !defined(PETSC_HAVE_CONSTRAINTS) 2428 if (mat->cmap->N != x->map->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->cmap->N,x->map->N); 2429 if (mat->rmap->N != y->map->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %D %D",mat->rmap->N,y->map->N); 2430 if (mat->rmap->n != y->map->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: local dim %D %D",mat->rmap->n,y->map->n); 2431 #endif 2432 ierr = VecSetErrorIfLocked(y,3);CHKERRQ(ierr); 2433 if (mat->erroriffailure) {ierr = VecValidValues(x,2,PETSC_TRUE);CHKERRQ(ierr);} 2434 MatCheckPreallocated(mat,1); 2435 2436 ierr = VecLockReadPush(x);CHKERRQ(ierr); 2437 if (!mat->ops->mult) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s does not have a multiply defined",((PetscObject)mat)->type_name); 2438 ierr = PetscLogEventBegin(MAT_Mult,mat,x,y,0);CHKERRQ(ierr); 2439 ierr = (*mat->ops->mult)(mat,x,y);CHKERRQ(ierr); 2440 ierr = PetscLogEventEnd(MAT_Mult,mat,x,y,0);CHKERRQ(ierr); 2441 if (mat->erroriffailure) {ierr = VecValidValues(y,3,PETSC_FALSE);CHKERRQ(ierr);} 2442 ierr = VecLockReadPop(x);CHKERRQ(ierr); 2443 PetscFunctionReturn(0); 2444 } 2445 2446 /*@ 2447 MatMultTranspose - Computes matrix transpose times a vector y = A^T * x. 2448 2449 Neighbor-wise Collective on Mat 2450 2451 Input Parameters: 2452 + mat - the matrix 2453 - x - the vector to be multiplied 2454 2455 Output Parameters: 2456 . y - the result 2457 2458 Notes: 2459 The vectors x and y cannot be the same. I.e., one cannot 2460 call MatMultTranspose(A,y,y). 2461 2462 For complex numbers this does NOT compute the Hermitian (complex conjugate) transpose multiple, 2463 use MatMultHermitianTranspose() 2464 2465 Level: beginner 2466 2467 .seealso: MatMult(), MatMultAdd(), MatMultTransposeAdd(), MatMultHermitianTranspose(), MatTranspose() 2468 @*/ 2469 PetscErrorCode MatMultTranspose(Mat mat,Vec x,Vec y) 2470 { 2471 PetscErrorCode (*op)(Mat,Vec,Vec)=NULL,ierr; 2472 2473 PetscFunctionBegin; 2474 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2475 PetscValidType(mat,1); 2476 PetscValidHeaderSpecific(x,VEC_CLASSID,2); 2477 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 2478 2479 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2480 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2481 if (x == y) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors"); 2482 #if !defined(PETSC_HAVE_CONSTRAINTS) 2483 if (mat->rmap->N != x->map->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->rmap->N,x->map->N); 2484 if (mat->cmap->N != y->map->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %D %D",mat->cmap->N,y->map->N); 2485 #endif 2486 if (mat->erroriffailure) {ierr = VecValidValues(x,2,PETSC_TRUE);CHKERRQ(ierr);} 2487 MatCheckPreallocated(mat,1); 2488 2489 if (!mat->ops->multtranspose) { 2490 if (mat->symmetric && mat->ops->mult) op = mat->ops->mult; 2491 if (!op) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s does not have a multiply transpose defined or is symmetric and does not have a multiply defined",((PetscObject)mat)->type_name); 2492 } else op = mat->ops->multtranspose; 2493 ierr = PetscLogEventBegin(MAT_MultTranspose,mat,x,y,0);CHKERRQ(ierr); 2494 ierr = VecLockReadPush(x);CHKERRQ(ierr); 2495 ierr = (*op)(mat,x,y);CHKERRQ(ierr); 2496 ierr = VecLockReadPop(x);CHKERRQ(ierr); 2497 ierr = PetscLogEventEnd(MAT_MultTranspose,mat,x,y,0);CHKERRQ(ierr); 2498 ierr = PetscObjectStateIncrease((PetscObject)y);CHKERRQ(ierr); 2499 if (mat->erroriffailure) {ierr = VecValidValues(y,3,PETSC_FALSE);CHKERRQ(ierr);} 2500 PetscFunctionReturn(0); 2501 } 2502 2503 /*@ 2504 MatMultHermitianTranspose - Computes matrix Hermitian transpose times a vector. 2505 2506 Neighbor-wise Collective on Mat 2507 2508 Input Parameters: 2509 + mat - the matrix 2510 - x - the vector to be multilplied 2511 2512 Output Parameters: 2513 . y - the result 2514 2515 Notes: 2516 The vectors x and y cannot be the same. I.e., one cannot 2517 call MatMultHermitianTranspose(A,y,y). 2518 2519 Also called the conjugate transpose, complex conjugate transpose, or adjoint. 2520 2521 For real numbers MatMultTranspose() and MatMultHermitianTranspose() are identical. 2522 2523 Level: beginner 2524 2525 .seealso: MatMult(), MatMultAdd(), MatMultHermitianTransposeAdd(), MatMultTranspose() 2526 @*/ 2527 PetscErrorCode MatMultHermitianTranspose(Mat mat,Vec x,Vec y) 2528 { 2529 PetscErrorCode ierr; 2530 2531 PetscFunctionBegin; 2532 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2533 PetscValidType(mat,1); 2534 PetscValidHeaderSpecific(x,VEC_CLASSID,2); 2535 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 2536 2537 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2538 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2539 if (x == y) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors"); 2540 #if !defined(PETSC_HAVE_CONSTRAINTS) 2541 if (mat->rmap->N != x->map->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->rmap->N,x->map->N); 2542 if (mat->cmap->N != y->map->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %D %D",mat->cmap->N,y->map->N); 2543 #endif 2544 MatCheckPreallocated(mat,1); 2545 2546 ierr = PetscLogEventBegin(MAT_MultHermitianTranspose,mat,x,y,0);CHKERRQ(ierr); 2547 #if defined(PETSC_USE_COMPLEX) 2548 if (mat->ops->multhermitiantranspose || (mat->hermitian && mat->ops->mult)) { 2549 ierr = VecLockReadPush(x);CHKERRQ(ierr); 2550 if (mat->ops->multhermitiantranspose) { 2551 ierr = (*mat->ops->multhermitiantranspose)(mat,x,y);CHKERRQ(ierr); 2552 } else { 2553 ierr = (*mat->ops->mult)(mat,x,y);CHKERRQ(ierr); 2554 } 2555 ierr = VecLockReadPop(x);CHKERRQ(ierr); 2556 } else { 2557 Vec w; 2558 ierr = VecDuplicate(x,&w);CHKERRQ(ierr); 2559 ierr = VecCopy(x,w);CHKERRQ(ierr); 2560 ierr = VecConjugate(w);CHKERRQ(ierr); 2561 ierr = MatMultTranspose(mat,w,y);CHKERRQ(ierr); 2562 ierr = VecDestroy(&w);CHKERRQ(ierr); 2563 ierr = VecConjugate(y);CHKERRQ(ierr); 2564 } 2565 ierr = PetscObjectStateIncrease((PetscObject)y);CHKERRQ(ierr); 2566 #else 2567 ierr = MatMultTranspose(mat,x,y);CHKERRQ(ierr); 2568 #endif 2569 ierr = PetscLogEventEnd(MAT_MultHermitianTranspose,mat,x,y,0);CHKERRQ(ierr); 2570 PetscFunctionReturn(0); 2571 } 2572 2573 /*@ 2574 MatMultAdd - Computes v3 = v2 + A * v1. 2575 2576 Neighbor-wise Collective on Mat 2577 2578 Input Parameters: 2579 + mat - the matrix 2580 - v1, v2 - the vectors 2581 2582 Output Parameters: 2583 . v3 - the result 2584 2585 Notes: 2586 The vectors v1 and v3 cannot be the same. I.e., one cannot 2587 call MatMultAdd(A,v1,v2,v1). 2588 2589 Level: beginner 2590 2591 .seealso: MatMultTranspose(), MatMult(), MatMultTransposeAdd() 2592 @*/ 2593 PetscErrorCode MatMultAdd(Mat mat,Vec v1,Vec v2,Vec v3) 2594 { 2595 PetscErrorCode ierr; 2596 2597 PetscFunctionBegin; 2598 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2599 PetscValidType(mat,1); 2600 PetscValidHeaderSpecific(v1,VEC_CLASSID,2); 2601 PetscValidHeaderSpecific(v2,VEC_CLASSID,3); 2602 PetscValidHeaderSpecific(v3,VEC_CLASSID,4); 2603 2604 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2605 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2606 if (mat->cmap->N != v1->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec v1: global dim %D %D",mat->cmap->N,v1->map->N); 2607 /* if (mat->rmap->N != v2->map->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec v2: global dim %D %D",mat->rmap->N,v2->map->N); 2608 if (mat->rmap->N != v3->map->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec v3: global dim %D %D",mat->rmap->N,v3->map->N); */ 2609 if (mat->rmap->n != v3->map->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec v3: local dim %D %D",mat->rmap->n,v3->map->n); 2610 if (mat->rmap->n != v2->map->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec v2: local dim %D %D",mat->rmap->n,v2->map->n); 2611 if (v1 == v3) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"v1 and v3 must be different vectors"); 2612 MatCheckPreallocated(mat,1); 2613 2614 if (!mat->ops->multadd) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"No MatMultAdd() for matrix type %s",((PetscObject)mat)->type_name); 2615 ierr = PetscLogEventBegin(MAT_MultAdd,mat,v1,v2,v3);CHKERRQ(ierr); 2616 ierr = VecLockReadPush(v1);CHKERRQ(ierr); 2617 ierr = (*mat->ops->multadd)(mat,v1,v2,v3);CHKERRQ(ierr); 2618 ierr = VecLockReadPop(v1);CHKERRQ(ierr); 2619 ierr = PetscLogEventEnd(MAT_MultAdd,mat,v1,v2,v3);CHKERRQ(ierr); 2620 ierr = PetscObjectStateIncrease((PetscObject)v3);CHKERRQ(ierr); 2621 PetscFunctionReturn(0); 2622 } 2623 2624 /*@ 2625 MatMultTransposeAdd - Computes v3 = v2 + A' * v1. 2626 2627 Neighbor-wise Collective on Mat 2628 2629 Input Parameters: 2630 + mat - the matrix 2631 - v1, v2 - the vectors 2632 2633 Output Parameters: 2634 . v3 - the result 2635 2636 Notes: 2637 The vectors v1 and v3 cannot be the same. I.e., one cannot 2638 call MatMultTransposeAdd(A,v1,v2,v1). 2639 2640 Level: beginner 2641 2642 .seealso: MatMultTranspose(), MatMultAdd(), MatMult() 2643 @*/ 2644 PetscErrorCode MatMultTransposeAdd(Mat mat,Vec v1,Vec v2,Vec v3) 2645 { 2646 PetscErrorCode ierr; 2647 2648 PetscFunctionBegin; 2649 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2650 PetscValidType(mat,1); 2651 PetscValidHeaderSpecific(v1,VEC_CLASSID,2); 2652 PetscValidHeaderSpecific(v2,VEC_CLASSID,3); 2653 PetscValidHeaderSpecific(v3,VEC_CLASSID,4); 2654 2655 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2656 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2657 if (!mat->ops->multtransposeadd) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 2658 if (v1 == v3) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"v1 and v3 must be different vectors"); 2659 if (mat->rmap->N != v1->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec v1: global dim %D %D",mat->rmap->N,v1->map->N); 2660 if (mat->cmap->N != v2->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec v2: global dim %D %D",mat->cmap->N,v2->map->N); 2661 if (mat->cmap->N != v3->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec v3: global dim %D %D",mat->cmap->N,v3->map->N); 2662 MatCheckPreallocated(mat,1); 2663 2664 ierr = PetscLogEventBegin(MAT_MultTransposeAdd,mat,v1,v2,v3);CHKERRQ(ierr); 2665 ierr = VecLockReadPush(v1);CHKERRQ(ierr); 2666 ierr = (*mat->ops->multtransposeadd)(mat,v1,v2,v3);CHKERRQ(ierr); 2667 ierr = VecLockReadPop(v1);CHKERRQ(ierr); 2668 ierr = PetscLogEventEnd(MAT_MultTransposeAdd,mat,v1,v2,v3);CHKERRQ(ierr); 2669 ierr = PetscObjectStateIncrease((PetscObject)v3);CHKERRQ(ierr); 2670 PetscFunctionReturn(0); 2671 } 2672 2673 /*@ 2674 MatMultHermitianTransposeAdd - Computes v3 = v2 + A^H * v1. 2675 2676 Neighbor-wise Collective on Mat 2677 2678 Input Parameters: 2679 + mat - the matrix 2680 - v1, v2 - the vectors 2681 2682 Output Parameters: 2683 . v3 - the result 2684 2685 Notes: 2686 The vectors v1 and v3 cannot be the same. I.e., one cannot 2687 call MatMultHermitianTransposeAdd(A,v1,v2,v1). 2688 2689 Level: beginner 2690 2691 .seealso: MatMultHermitianTranspose(), MatMultTranspose(), MatMultAdd(), MatMult() 2692 @*/ 2693 PetscErrorCode MatMultHermitianTransposeAdd(Mat mat,Vec v1,Vec v2,Vec v3) 2694 { 2695 PetscErrorCode ierr; 2696 2697 PetscFunctionBegin; 2698 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2699 PetscValidType(mat,1); 2700 PetscValidHeaderSpecific(v1,VEC_CLASSID,2); 2701 PetscValidHeaderSpecific(v2,VEC_CLASSID,3); 2702 PetscValidHeaderSpecific(v3,VEC_CLASSID,4); 2703 2704 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2705 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2706 if (v1 == v3) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"v1 and v3 must be different vectors"); 2707 if (mat->rmap->N != v1->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec v1: global dim %D %D",mat->rmap->N,v1->map->N); 2708 if (mat->cmap->N != v2->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec v2: global dim %D %D",mat->cmap->N,v2->map->N); 2709 if (mat->cmap->N != v3->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec v3: global dim %D %D",mat->cmap->N,v3->map->N); 2710 MatCheckPreallocated(mat,1); 2711 2712 ierr = PetscLogEventBegin(MAT_MultHermitianTransposeAdd,mat,v1,v2,v3);CHKERRQ(ierr); 2713 ierr = VecLockReadPush(v1);CHKERRQ(ierr); 2714 if (mat->ops->multhermitiantransposeadd) { 2715 ierr = (*mat->ops->multhermitiantransposeadd)(mat,v1,v2,v3);CHKERRQ(ierr); 2716 } else { 2717 Vec w,z; 2718 ierr = VecDuplicate(v1,&w);CHKERRQ(ierr); 2719 ierr = VecCopy(v1,w);CHKERRQ(ierr); 2720 ierr = VecConjugate(w);CHKERRQ(ierr); 2721 ierr = VecDuplicate(v3,&z);CHKERRQ(ierr); 2722 ierr = MatMultTranspose(mat,w,z);CHKERRQ(ierr); 2723 ierr = VecDestroy(&w);CHKERRQ(ierr); 2724 ierr = VecConjugate(z);CHKERRQ(ierr); 2725 if (v2 != v3) { 2726 ierr = VecWAXPY(v3,1.0,v2,z);CHKERRQ(ierr); 2727 } else { 2728 ierr = VecAXPY(v3,1.0,z);CHKERRQ(ierr); 2729 } 2730 ierr = VecDestroy(&z);CHKERRQ(ierr); 2731 } 2732 ierr = VecLockReadPop(v1);CHKERRQ(ierr); 2733 ierr = PetscLogEventEnd(MAT_MultHermitianTransposeAdd,mat,v1,v2,v3);CHKERRQ(ierr); 2734 ierr = PetscObjectStateIncrease((PetscObject)v3);CHKERRQ(ierr); 2735 PetscFunctionReturn(0); 2736 } 2737 2738 /*@ 2739 MatMultConstrained - The inner multiplication routine for a 2740 constrained matrix P^T A P. 2741 2742 Neighbor-wise Collective on Mat 2743 2744 Input Parameters: 2745 + mat - the matrix 2746 - x - the vector to be multilplied 2747 2748 Output Parameters: 2749 . y - the result 2750 2751 Notes: 2752 The vectors x and y cannot be the same. I.e., one cannot 2753 call MatMult(A,y,y). 2754 2755 Level: beginner 2756 2757 .seealso: MatMult(), MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd() 2758 @*/ 2759 PetscErrorCode MatMultConstrained(Mat mat,Vec x,Vec y) 2760 { 2761 PetscErrorCode ierr; 2762 2763 PetscFunctionBegin; 2764 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2765 PetscValidHeaderSpecific(x,VEC_CLASSID,2); 2766 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 2767 if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2768 if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2769 if (x == y) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors"); 2770 if (mat->cmap->N != x->map->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->cmap->N,x->map->N); 2771 if (mat->rmap->N != y->map->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %D %D",mat->rmap->N,y->map->N); 2772 if (mat->rmap->n != y->map->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: local dim %D %D",mat->rmap->n,y->map->n); 2773 2774 ierr = PetscLogEventBegin(MAT_MultConstrained,mat,x,y,0);CHKERRQ(ierr); 2775 ierr = VecLockReadPush(x);CHKERRQ(ierr); 2776 ierr = (*mat->ops->multconstrained)(mat,x,y);CHKERRQ(ierr); 2777 ierr = VecLockReadPop(x);CHKERRQ(ierr); 2778 ierr = PetscLogEventEnd(MAT_MultConstrained,mat,x,y,0);CHKERRQ(ierr); 2779 ierr = PetscObjectStateIncrease((PetscObject)y);CHKERRQ(ierr); 2780 PetscFunctionReturn(0); 2781 } 2782 2783 /*@ 2784 MatMultTransposeConstrained - The inner multiplication routine for a 2785 constrained matrix P^T A^T P. 2786 2787 Neighbor-wise Collective on Mat 2788 2789 Input Parameters: 2790 + mat - the matrix 2791 - x - the vector to be multilplied 2792 2793 Output Parameters: 2794 . y - the result 2795 2796 Notes: 2797 The vectors x and y cannot be the same. I.e., one cannot 2798 call MatMult(A,y,y). 2799 2800 Level: beginner 2801 2802 .seealso: MatMult(), MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd() 2803 @*/ 2804 PetscErrorCode MatMultTransposeConstrained(Mat mat,Vec x,Vec y) 2805 { 2806 PetscErrorCode ierr; 2807 2808 PetscFunctionBegin; 2809 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2810 PetscValidHeaderSpecific(x,VEC_CLASSID,2); 2811 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 2812 if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2813 if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2814 if (x == y) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors"); 2815 if (mat->rmap->N != x->map->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->cmap->N,x->map->N); 2816 if (mat->cmap->N != y->map->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %D %D",mat->rmap->N,y->map->N); 2817 2818 ierr = PetscLogEventBegin(MAT_MultConstrained,mat,x,y,0);CHKERRQ(ierr); 2819 ierr = (*mat->ops->multtransposeconstrained)(mat,x,y);CHKERRQ(ierr); 2820 ierr = PetscLogEventEnd(MAT_MultConstrained,mat,x,y,0);CHKERRQ(ierr); 2821 ierr = PetscObjectStateIncrease((PetscObject)y);CHKERRQ(ierr); 2822 PetscFunctionReturn(0); 2823 } 2824 2825 /*@C 2826 MatGetFactorType - gets the type of factorization it is 2827 2828 Not Collective 2829 2830 Input Parameters: 2831 . mat - the matrix 2832 2833 Output Parameters: 2834 . t - the type, one of MAT_FACTOR_NONE, MAT_FACTOR_LU, MAT_FACTOR_CHOLESKY, MAT_FACTOR_ILU, MAT_FACTOR_ICC,MAT_FACTOR_ILUDT 2835 2836 Level: intermediate 2837 2838 .seealso: MatFactorType, MatGetFactor(), MatSetFactorType() 2839 @*/ 2840 PetscErrorCode MatGetFactorType(Mat mat,MatFactorType *t) 2841 { 2842 PetscFunctionBegin; 2843 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2844 PetscValidType(mat,1); 2845 PetscValidPointer(t,2); 2846 *t = mat->factortype; 2847 PetscFunctionReturn(0); 2848 } 2849 2850 /*@C 2851 MatSetFactorType - sets the type of factorization it is 2852 2853 Logically Collective on Mat 2854 2855 Input Parameters: 2856 + mat - the matrix 2857 - t - the type, one of MAT_FACTOR_NONE, MAT_FACTOR_LU, MAT_FACTOR_CHOLESKY, MAT_FACTOR_ILU, MAT_FACTOR_ICC,MAT_FACTOR_ILUDT 2858 2859 Level: intermediate 2860 2861 .seealso: MatFactorType, MatGetFactor(), MatGetFactorType() 2862 @*/ 2863 PetscErrorCode MatSetFactorType(Mat mat, MatFactorType t) 2864 { 2865 PetscFunctionBegin; 2866 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2867 PetscValidType(mat,1); 2868 mat->factortype = t; 2869 PetscFunctionReturn(0); 2870 } 2871 2872 /* ------------------------------------------------------------*/ 2873 /*@C 2874 MatGetInfo - Returns information about matrix storage (number of 2875 nonzeros, memory, etc.). 2876 2877 Collective on Mat if MAT_GLOBAL_MAX or MAT_GLOBAL_SUM is used as the flag 2878 2879 Input Parameters: 2880 . mat - the matrix 2881 2882 Output Parameters: 2883 + flag - flag indicating the type of parameters to be returned 2884 (MAT_LOCAL - local matrix, MAT_GLOBAL_MAX - maximum over all processors, 2885 MAT_GLOBAL_SUM - sum over all processors) 2886 - info - matrix information context 2887 2888 Notes: 2889 The MatInfo context contains a variety of matrix data, including 2890 number of nonzeros allocated and used, number of mallocs during 2891 matrix assembly, etc. Additional information for factored matrices 2892 is provided (such as the fill ratio, number of mallocs during 2893 factorization, etc.). Much of this info is printed to PETSC_STDOUT 2894 when using the runtime options 2895 $ -info -mat_view ::ascii_info 2896 2897 Example for C/C++ Users: 2898 See the file ${PETSC_DIR}/include/petscmat.h for a complete list of 2899 data within the MatInfo context. For example, 2900 .vb 2901 MatInfo info; 2902 Mat A; 2903 double mal, nz_a, nz_u; 2904 2905 MatGetInfo(A,MAT_LOCAL,&info); 2906 mal = info.mallocs; 2907 nz_a = info.nz_allocated; 2908 .ve 2909 2910 Example for Fortran Users: 2911 Fortran users should declare info as a double precision 2912 array of dimension MAT_INFO_SIZE, and then extract the parameters 2913 of interest. See the file ${PETSC_DIR}/include/petsc/finclude/petscmat.h 2914 a complete list of parameter names. 2915 .vb 2916 double precision info(MAT_INFO_SIZE) 2917 double precision mal, nz_a 2918 Mat A 2919 integer ierr 2920 2921 call MatGetInfo(A,MAT_LOCAL,info,ierr) 2922 mal = info(MAT_INFO_MALLOCS) 2923 nz_a = info(MAT_INFO_NZ_ALLOCATED) 2924 .ve 2925 2926 Level: intermediate 2927 2928 Developer Note: fortran interface is not autogenerated as the f90 2929 interface defintion cannot be generated correctly [due to MatInfo] 2930 2931 .seealso: MatStashGetInfo() 2932 2933 @*/ 2934 PetscErrorCode MatGetInfo(Mat mat,MatInfoType flag,MatInfo *info) 2935 { 2936 PetscErrorCode ierr; 2937 2938 PetscFunctionBegin; 2939 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2940 PetscValidType(mat,1); 2941 PetscValidPointer(info,3); 2942 if (!mat->ops->getinfo) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 2943 MatCheckPreallocated(mat,1); 2944 ierr = (*mat->ops->getinfo)(mat,flag,info);CHKERRQ(ierr); 2945 PetscFunctionReturn(0); 2946 } 2947 2948 /* 2949 This is used by external packages where it is not easy to get the info from the actual 2950 matrix factorization. 2951 */ 2952 PetscErrorCode MatGetInfo_External(Mat A,MatInfoType flag,MatInfo *info) 2953 { 2954 PetscErrorCode ierr; 2955 2956 PetscFunctionBegin; 2957 ierr = PetscMemzero(info,sizeof(MatInfo));CHKERRQ(ierr); 2958 PetscFunctionReturn(0); 2959 } 2960 2961 /* ----------------------------------------------------------*/ 2962 2963 /*@C 2964 MatLUFactor - Performs in-place LU factorization of matrix. 2965 2966 Collective on Mat 2967 2968 Input Parameters: 2969 + mat - the matrix 2970 . row - row permutation 2971 . col - column permutation 2972 - info - options for factorization, includes 2973 $ fill - expected fill as ratio of original fill. 2974 $ dtcol - pivot tolerance (0 no pivot, 1 full column pivoting) 2975 $ Run with the option -info to determine an optimal value to use 2976 2977 Notes: 2978 Most users should employ the simplified KSP interface for linear solvers 2979 instead of working directly with matrix algebra routines such as this. 2980 See, e.g., KSPCreate(). 2981 2982 This changes the state of the matrix to a factored matrix; it cannot be used 2983 for example with MatSetValues() unless one first calls MatSetUnfactored(). 2984 2985 Level: developer 2986 2987 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor(), 2988 MatGetOrdering(), MatSetUnfactored(), MatFactorInfo, MatGetFactor() 2989 2990 Developer Note: fortran interface is not autogenerated as the f90 2991 interface defintion cannot be generated correctly [due to MatFactorInfo] 2992 2993 @*/ 2994 PetscErrorCode MatLUFactor(Mat mat,IS row,IS col,const MatFactorInfo *info) 2995 { 2996 PetscErrorCode ierr; 2997 MatFactorInfo tinfo; 2998 2999 PetscFunctionBegin; 3000 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3001 if (row) PetscValidHeaderSpecific(row,IS_CLASSID,2); 3002 if (col) PetscValidHeaderSpecific(col,IS_CLASSID,3); 3003 if (info) PetscValidPointer(info,4); 3004 PetscValidType(mat,1); 3005 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3006 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3007 if (!mat->ops->lufactor) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 3008 MatCheckPreallocated(mat,1); 3009 if (!info) { 3010 ierr = MatFactorInfoInitialize(&tinfo);CHKERRQ(ierr); 3011 info = &tinfo; 3012 } 3013 3014 ierr = PetscLogEventBegin(MAT_LUFactor,mat,row,col,0);CHKERRQ(ierr); 3015 ierr = (*mat->ops->lufactor)(mat,row,col,info);CHKERRQ(ierr); 3016 ierr = PetscLogEventEnd(MAT_LUFactor,mat,row,col,0);CHKERRQ(ierr); 3017 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 3018 PetscFunctionReturn(0); 3019 } 3020 3021 /*@C 3022 MatILUFactor - Performs in-place ILU factorization of matrix. 3023 3024 Collective on Mat 3025 3026 Input Parameters: 3027 + mat - the matrix 3028 . row - row permutation 3029 . col - column permutation 3030 - info - structure containing 3031 $ levels - number of levels of fill. 3032 $ expected fill - as ratio of original fill. 3033 $ 1 or 0 - indicating force fill on diagonal (improves robustness for matrices 3034 missing diagonal entries) 3035 3036 Notes: 3037 Probably really in-place only when level of fill is zero, otherwise allocates 3038 new space to store factored matrix and deletes previous memory. 3039 3040 Most users should employ the simplified KSP interface for linear solvers 3041 instead of working directly with matrix algebra routines such as this. 3042 See, e.g., KSPCreate(). 3043 3044 Level: developer 3045 3046 .seealso: MatILUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor(), MatFactorInfo 3047 3048 Developer Note: fortran interface is not autogenerated as the f90 3049 interface defintion cannot be generated correctly [due to MatFactorInfo] 3050 3051 @*/ 3052 PetscErrorCode MatILUFactor(Mat mat,IS row,IS col,const MatFactorInfo *info) 3053 { 3054 PetscErrorCode ierr; 3055 3056 PetscFunctionBegin; 3057 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3058 if (row) PetscValidHeaderSpecific(row,IS_CLASSID,2); 3059 if (col) PetscValidHeaderSpecific(col,IS_CLASSID,3); 3060 PetscValidPointer(info,4); 3061 PetscValidType(mat,1); 3062 if (mat->rmap->N != mat->cmap->N) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONG,"matrix must be square"); 3063 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3064 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3065 if (!mat->ops->ilufactor) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 3066 MatCheckPreallocated(mat,1); 3067 3068 ierr = PetscLogEventBegin(MAT_ILUFactor,mat,row,col,0);CHKERRQ(ierr); 3069 ierr = (*mat->ops->ilufactor)(mat,row,col,info);CHKERRQ(ierr); 3070 ierr = PetscLogEventEnd(MAT_ILUFactor,mat,row,col,0);CHKERRQ(ierr); 3071 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 3072 PetscFunctionReturn(0); 3073 } 3074 3075 /*@C 3076 MatLUFactorSymbolic - Performs symbolic LU factorization of matrix. 3077 Call this routine before calling MatLUFactorNumeric(). 3078 3079 Collective on Mat 3080 3081 Input Parameters: 3082 + fact - the factor matrix obtained with MatGetFactor() 3083 . mat - the matrix 3084 . row, col - row and column permutations 3085 - info - options for factorization, includes 3086 $ fill - expected fill as ratio of original fill. 3087 $ dtcol - pivot tolerance (0 no pivot, 1 full column pivoting) 3088 $ Run with the option -info to determine an optimal value to use 3089 3090 Notes: 3091 See Users-Manual: ch_mat for additional information about choosing the fill factor for better efficiency. 3092 3093 Most users should employ the simplified KSP interface for linear solvers 3094 instead of working directly with matrix algebra routines such as this. 3095 See, e.g., KSPCreate(). 3096 3097 Level: developer 3098 3099 .seealso: MatLUFactor(), MatLUFactorNumeric(), MatCholeskyFactor(), MatFactorInfo, MatFactorInfoInitialize() 3100 3101 Developer Note: fortran interface is not autogenerated as the f90 3102 interface defintion cannot be generated correctly [due to MatFactorInfo] 3103 3104 @*/ 3105 PetscErrorCode MatLUFactorSymbolic(Mat fact,Mat mat,IS row,IS col,const MatFactorInfo *info) 3106 { 3107 PetscErrorCode ierr; 3108 MatFactorInfo tinfo; 3109 3110 PetscFunctionBegin; 3111 PetscValidHeaderSpecific(mat,MAT_CLASSID,2); 3112 if (row) PetscValidHeaderSpecific(row,IS_CLASSID,3); 3113 if (col) PetscValidHeaderSpecific(col,IS_CLASSID,4); 3114 if (info) PetscValidPointer(info,5); 3115 PetscValidType(mat,2); 3116 PetscValidPointer(fact,1); 3117 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3118 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3119 if (!(fact)->ops->lufactorsymbolic) { 3120 MatSolverType stype; 3121 ierr = MatFactorGetSolverType(fact,&stype);CHKERRQ(ierr); 3122 SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s symbolic LU using solver package %s",((PetscObject)mat)->type_name,stype); 3123 } 3124 MatCheckPreallocated(mat,2); 3125 if (!info) { 3126 ierr = MatFactorInfoInitialize(&tinfo);CHKERRQ(ierr); 3127 info = &tinfo; 3128 } 3129 3130 if (!fact->trivialsymbolic) {ierr = PetscLogEventBegin(MAT_LUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr);} 3131 ierr = (fact->ops->lufactorsymbolic)(fact,mat,row,col,info);CHKERRQ(ierr); 3132 if (!fact->trivialsymbolic) {ierr = PetscLogEventEnd(MAT_LUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr);} 3133 ierr = PetscObjectStateIncrease((PetscObject)fact);CHKERRQ(ierr); 3134 PetscFunctionReturn(0); 3135 } 3136 3137 /*@C 3138 MatLUFactorNumeric - Performs numeric LU factorization of a matrix. 3139 Call this routine after first calling MatLUFactorSymbolic(). 3140 3141 Collective on Mat 3142 3143 Input Parameters: 3144 + fact - the factor matrix obtained with MatGetFactor() 3145 . mat - the matrix 3146 - info - options for factorization 3147 3148 Notes: 3149 See MatLUFactor() for in-place factorization. See 3150 MatCholeskyFactorNumeric() for the symmetric, positive definite case. 3151 3152 Most users should employ the simplified KSP interface for linear solvers 3153 instead of working directly with matrix algebra routines such as this. 3154 See, e.g., KSPCreate(). 3155 3156 Level: developer 3157 3158 .seealso: MatLUFactorSymbolic(), MatLUFactor(), MatCholeskyFactor() 3159 3160 Developer Note: fortran interface is not autogenerated as the f90 3161 interface defintion cannot be generated correctly [due to MatFactorInfo] 3162 3163 @*/ 3164 PetscErrorCode MatLUFactorNumeric(Mat fact,Mat mat,const MatFactorInfo *info) 3165 { 3166 MatFactorInfo tinfo; 3167 PetscErrorCode ierr; 3168 3169 PetscFunctionBegin; 3170 PetscValidHeaderSpecific(mat,MAT_CLASSID,2); 3171 PetscValidType(mat,2); 3172 PetscValidPointer(fact,1); 3173 PetscValidHeaderSpecific(fact,MAT_CLASSID,1); 3174 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3175 if (mat->rmap->N != (fact)->rmap->N || mat->cmap->N != (fact)->cmap->N) SETERRQ4(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Mat fact: global dimensions are different %D should = %D %D should = %D",mat->rmap->N,(fact)->rmap->N,mat->cmap->N,(fact)->cmap->N); 3176 3177 if (!(fact)->ops->lufactornumeric) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s numeric LU",((PetscObject)mat)->type_name); 3178 MatCheckPreallocated(mat,2); 3179 if (!info) { 3180 ierr = MatFactorInfoInitialize(&tinfo);CHKERRQ(ierr); 3181 info = &tinfo; 3182 } 3183 3184 if (!fact->trivialsymbolic) {ierr = PetscLogEventBegin(MAT_LUFactorNumeric,mat,fact,0,0);CHKERRQ(ierr);} 3185 else {ierr = PetscLogEventBegin(MAT_LUFactor,mat,fact,0,0);CHKERRQ(ierr);} 3186 ierr = (fact->ops->lufactornumeric)(fact,mat,info);CHKERRQ(ierr); 3187 if (!fact->trivialsymbolic) {ierr = PetscLogEventEnd(MAT_LUFactorNumeric,mat,fact,0,0);CHKERRQ(ierr);} 3188 else {ierr = PetscLogEventEnd(MAT_LUFactor,mat,fact,0,0);CHKERRQ(ierr);} 3189 ierr = MatViewFromOptions(fact,NULL,"-mat_factor_view");CHKERRQ(ierr); 3190 ierr = PetscObjectStateIncrease((PetscObject)fact);CHKERRQ(ierr); 3191 PetscFunctionReturn(0); 3192 } 3193 3194 /*@C 3195 MatCholeskyFactor - Performs in-place Cholesky factorization of a 3196 symmetric matrix. 3197 3198 Collective on Mat 3199 3200 Input Parameters: 3201 + mat - the matrix 3202 . perm - row and column permutations 3203 - f - expected fill as ratio of original fill 3204 3205 Notes: 3206 See MatLUFactor() for the nonsymmetric case. See also 3207 MatCholeskyFactorSymbolic(), and MatCholeskyFactorNumeric(). 3208 3209 Most users should employ the simplified KSP interface for linear solvers 3210 instead of working directly with matrix algebra routines such as this. 3211 See, e.g., KSPCreate(). 3212 3213 Level: developer 3214 3215 .seealso: MatLUFactor(), MatCholeskyFactorSymbolic(), MatCholeskyFactorNumeric() 3216 MatGetOrdering() 3217 3218 Developer Note: fortran interface is not autogenerated as the f90 3219 interface defintion cannot be generated correctly [due to MatFactorInfo] 3220 3221 @*/ 3222 PetscErrorCode MatCholeskyFactor(Mat mat,IS perm,const MatFactorInfo *info) 3223 { 3224 PetscErrorCode ierr; 3225 MatFactorInfo tinfo; 3226 3227 PetscFunctionBegin; 3228 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3229 PetscValidType(mat,1); 3230 if (perm) PetscValidHeaderSpecific(perm,IS_CLASSID,2); 3231 if (info) PetscValidPointer(info,3); 3232 if (mat->rmap->N != mat->cmap->N) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONG,"Matrix must be square"); 3233 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3234 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3235 if (!mat->ops->choleskyfactor) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"In-place factorization for Mat type %s is not supported, try out-of-place factorization. See MatCholeskyFactorSymbolic/Numeric",((PetscObject)mat)->type_name); 3236 MatCheckPreallocated(mat,1); 3237 if (!info) { 3238 ierr = MatFactorInfoInitialize(&tinfo);CHKERRQ(ierr); 3239 info = &tinfo; 3240 } 3241 3242 ierr = PetscLogEventBegin(MAT_CholeskyFactor,mat,perm,0,0);CHKERRQ(ierr); 3243 ierr = (*mat->ops->choleskyfactor)(mat,perm,info);CHKERRQ(ierr); 3244 ierr = PetscLogEventEnd(MAT_CholeskyFactor,mat,perm,0,0);CHKERRQ(ierr); 3245 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 3246 PetscFunctionReturn(0); 3247 } 3248 3249 /*@C 3250 MatCholeskyFactorSymbolic - Performs symbolic Cholesky factorization 3251 of a symmetric matrix. 3252 3253 Collective on Mat 3254 3255 Input Parameters: 3256 + fact - the factor matrix obtained with MatGetFactor() 3257 . mat - the matrix 3258 . perm - row and column permutations 3259 - info - options for factorization, includes 3260 $ fill - expected fill as ratio of original fill. 3261 $ dtcol - pivot tolerance (0 no pivot, 1 full column pivoting) 3262 $ Run with the option -info to determine an optimal value to use 3263 3264 Notes: 3265 See MatLUFactorSymbolic() for the nonsymmetric case. See also 3266 MatCholeskyFactor() and MatCholeskyFactorNumeric(). 3267 3268 Most users should employ the simplified KSP interface for linear solvers 3269 instead of working directly with matrix algebra routines such as this. 3270 See, e.g., KSPCreate(). 3271 3272 Level: developer 3273 3274 .seealso: MatLUFactorSymbolic(), MatCholeskyFactor(), MatCholeskyFactorNumeric() 3275 MatGetOrdering() 3276 3277 Developer Note: fortran interface is not autogenerated as the f90 3278 interface defintion cannot be generated correctly [due to MatFactorInfo] 3279 3280 @*/ 3281 PetscErrorCode MatCholeskyFactorSymbolic(Mat fact,Mat mat,IS perm,const MatFactorInfo *info) 3282 { 3283 PetscErrorCode ierr; 3284 MatFactorInfo tinfo; 3285 3286 PetscFunctionBegin; 3287 PetscValidHeaderSpecific(mat,MAT_CLASSID,2); 3288 PetscValidType(mat,2); 3289 if (perm) PetscValidHeaderSpecific(perm,IS_CLASSID,3); 3290 if (info) PetscValidPointer(info,4); 3291 PetscValidPointer(fact,1); 3292 if (mat->rmap->N != mat->cmap->N) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONG,"Matrix must be square"); 3293 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3294 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3295 if (!(fact)->ops->choleskyfactorsymbolic) { 3296 MatSolverType stype; 3297 ierr = MatFactorGetSolverType(fact,&stype);CHKERRQ(ierr); 3298 SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s symbolic factor Cholesky using solver package %s",((PetscObject)mat)->type_name,stype); 3299 } 3300 MatCheckPreallocated(mat,2); 3301 if (!info) { 3302 ierr = MatFactorInfoInitialize(&tinfo);CHKERRQ(ierr); 3303 info = &tinfo; 3304 } 3305 3306 if (!fact->trivialsymbolic) {ierr = PetscLogEventBegin(MAT_CholeskyFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr);} 3307 ierr = (fact->ops->choleskyfactorsymbolic)(fact,mat,perm,info);CHKERRQ(ierr); 3308 if (!fact->trivialsymbolic) {ierr = PetscLogEventEnd(MAT_CholeskyFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr);} 3309 ierr = PetscObjectStateIncrease((PetscObject)fact);CHKERRQ(ierr); 3310 PetscFunctionReturn(0); 3311 } 3312 3313 /*@C 3314 MatCholeskyFactorNumeric - Performs numeric Cholesky factorization 3315 of a symmetric matrix. Call this routine after first calling 3316 MatCholeskyFactorSymbolic(). 3317 3318 Collective on Mat 3319 3320 Input Parameters: 3321 + fact - the factor matrix obtained with MatGetFactor() 3322 . mat - the initial matrix 3323 . info - options for factorization 3324 - fact - the symbolic factor of mat 3325 3326 Notes: 3327 Most users should employ the simplified KSP interface for linear solvers 3328 instead of working directly with matrix algebra routines such as this. 3329 See, e.g., KSPCreate(). 3330 3331 Level: developer 3332 3333 .seealso: MatCholeskyFactorSymbolic(), MatCholeskyFactor(), MatLUFactorNumeric() 3334 3335 Developer Note: fortran interface is not autogenerated as the f90 3336 interface defintion cannot be generated correctly [due to MatFactorInfo] 3337 3338 @*/ 3339 PetscErrorCode MatCholeskyFactorNumeric(Mat fact,Mat mat,const MatFactorInfo *info) 3340 { 3341 MatFactorInfo tinfo; 3342 PetscErrorCode ierr; 3343 3344 PetscFunctionBegin; 3345 PetscValidHeaderSpecific(mat,MAT_CLASSID,2); 3346 PetscValidType(mat,2); 3347 PetscValidPointer(fact,1); 3348 PetscValidHeaderSpecific(fact,MAT_CLASSID,1); 3349 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3350 if (!(fact)->ops->choleskyfactornumeric) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s numeric factor Cholesky",((PetscObject)mat)->type_name); 3351 if (mat->rmap->N != (fact)->rmap->N || mat->cmap->N != (fact)->cmap->N) SETERRQ4(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Mat fact: global dim %D should = %D %D should = %D",mat->rmap->N,(fact)->rmap->N,mat->cmap->N,(fact)->cmap->N); 3352 MatCheckPreallocated(mat,2); 3353 if (!info) { 3354 ierr = MatFactorInfoInitialize(&tinfo);CHKERRQ(ierr); 3355 info = &tinfo; 3356 } 3357 3358 if (!fact->trivialsymbolic) {ierr = PetscLogEventBegin(MAT_CholeskyFactorNumeric,mat,fact,0,0);CHKERRQ(ierr);} 3359 else {ierr = PetscLogEventBegin(MAT_CholeskyFactor,mat,fact,0,0);CHKERRQ(ierr);} 3360 ierr = (fact->ops->choleskyfactornumeric)(fact,mat,info);CHKERRQ(ierr); 3361 if (!fact->trivialsymbolic) {ierr = PetscLogEventEnd(MAT_CholeskyFactorNumeric,mat,fact,0,0);CHKERRQ(ierr);} 3362 else {ierr = PetscLogEventEnd(MAT_CholeskyFactor,mat,fact,0,0);CHKERRQ(ierr);} 3363 ierr = MatViewFromOptions(fact,NULL,"-mat_factor_view");CHKERRQ(ierr); 3364 ierr = PetscObjectStateIncrease((PetscObject)fact);CHKERRQ(ierr); 3365 PetscFunctionReturn(0); 3366 } 3367 3368 /*@C 3369 MatQRFactor - Performs in-place QR factorization of matrix. 3370 3371 Collective on Mat 3372 3373 Input Parameters: 3374 + mat - the matrix 3375 . col - column permutation 3376 - info - options for factorization, includes 3377 $ fill - expected fill as ratio of original fill. 3378 $ dtcol - pivot tolerance (0 no pivot, 1 full column pivoting) 3379 $ Run with the option -info to determine an optimal value to use 3380 3381 Notes: 3382 Most users should employ the simplified KSP interface for linear solvers 3383 instead of working directly with matrix algebra routines such as this. 3384 See, e.g., KSPCreate(). 3385 3386 This changes the state of the matrix to a factored matrix; it cannot be used 3387 for example with MatSetValues() unless one first calls MatSetUnfactored(). 3388 3389 Level: developer 3390 3391 .seealso: MatQRFactorSymbolic(), MatQRFactorNumeric(), MatLUFactor(), 3392 MatSetUnfactored(), MatFactorInfo, MatGetFactor() 3393 3394 Developer Note: fortran interface is not autogenerated as the f90 3395 interface defintion cannot be generated correctly [due to MatFactorInfo] 3396 3397 @*/ 3398 PetscErrorCode MatQRFactor(Mat mat, IS col, const MatFactorInfo *info) 3399 { 3400 PetscErrorCode ierr; 3401 3402 PetscFunctionBegin; 3403 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3404 if (col) PetscValidHeaderSpecific(col,IS_CLASSID,2); 3405 if (info) PetscValidPointer(info,3); 3406 PetscValidType(mat,1); 3407 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3408 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3409 MatCheckPreallocated(mat,1); 3410 ierr = PetscLogEventBegin(MAT_QRFactor,mat,col,0,0);CHKERRQ(ierr); 3411 ierr = PetscUseMethod(mat,"MatQRFactor_C", (Mat,IS,const MatFactorInfo*), (mat, col, info));CHKERRQ(ierr); 3412 ierr = PetscLogEventEnd(MAT_QRFactor,mat,col,0,0);CHKERRQ(ierr); 3413 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 3414 PetscFunctionReturn(0); 3415 } 3416 3417 /*@C 3418 MatQRFactorSymbolic - Performs symbolic QR factorization of matrix. 3419 Call this routine before calling MatQRFactorNumeric(). 3420 3421 Collective on Mat 3422 3423 Input Parameters: 3424 + fact - the factor matrix obtained with MatGetFactor() 3425 . mat - the matrix 3426 . col - column permutation 3427 - info - options for factorization, includes 3428 $ fill - expected fill as ratio of original fill. 3429 $ dtcol - pivot tolerance (0 no pivot, 1 full column pivoting) 3430 $ Run with the option -info to determine an optimal value to use 3431 3432 Most users should employ the simplified KSP interface for linear solvers 3433 instead of working directly with matrix algebra routines such as this. 3434 See, e.g., KSPCreate(). 3435 3436 Level: developer 3437 3438 .seealso: MatQRFactor(), MatQRFactorNumeric(), MatLUFactor(), MatFactorInfo, MatFactorInfoInitialize() 3439 3440 Developer Note: fortran interface is not autogenerated as the f90 3441 interface defintion cannot be generated correctly [due to MatFactorInfo] 3442 3443 @*/ 3444 PetscErrorCode MatQRFactorSymbolic(Mat fact,Mat mat,IS col,const MatFactorInfo *info) 3445 { 3446 PetscErrorCode ierr; 3447 MatFactorInfo tinfo; 3448 3449 PetscFunctionBegin; 3450 PetscValidHeaderSpecific(mat,MAT_CLASSID,2); 3451 if (col) PetscValidHeaderSpecific(col,IS_CLASSID,3); 3452 if (info) PetscValidPointer(info,4); 3453 PetscValidType(mat,2); 3454 PetscValidPointer(fact,1); 3455 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3456 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3457 MatCheckPreallocated(mat,2); 3458 if (!info) { 3459 ierr = MatFactorInfoInitialize(&tinfo);CHKERRQ(ierr); 3460 info = &tinfo; 3461 } 3462 3463 if (!fact->trivialsymbolic) {ierr = PetscLogEventBegin(MAT_QRFactorSymbolic,fact,mat,col,0);CHKERRQ(ierr);} 3464 ierr = PetscUseMethod(fact,"MatQRFactorSymbolic_C", (Mat,Mat,IS,const MatFactorInfo*), (fact, mat, col, info));CHKERRQ(ierr); 3465 if (!fact->trivialsymbolic) {ierr = PetscLogEventEnd(MAT_QRFactorSymbolic,fact,mat,col,0);CHKERRQ(ierr);} 3466 ierr = PetscObjectStateIncrease((PetscObject)fact);CHKERRQ(ierr); 3467 PetscFunctionReturn(0); 3468 } 3469 3470 /*@C 3471 MatQRFactorNumeric - Performs numeric QR factorization of a matrix. 3472 Call this routine after first calling MatQRFactorSymbolic(). 3473 3474 Collective on Mat 3475 3476 Input Parameters: 3477 + fact - the factor matrix obtained with MatGetFactor() 3478 . mat - the matrix 3479 - info - options for factorization 3480 3481 Notes: 3482 See MatQRFactor() for in-place factorization. 3483 3484 Most users should employ the simplified KSP interface for linear solvers 3485 instead of working directly with matrix algebra routines such as this. 3486 See, e.g., KSPCreate(). 3487 3488 Level: developer 3489 3490 .seealso: MatQRFactorSymbolic(), MatLUFactor() 3491 3492 Developer Note: fortran interface is not autogenerated as the f90 3493 interface defintion cannot be generated correctly [due to MatFactorInfo] 3494 3495 @*/ 3496 PetscErrorCode MatQRFactorNumeric(Mat fact,Mat mat,const MatFactorInfo *info) 3497 { 3498 MatFactorInfo tinfo; 3499 PetscErrorCode ierr; 3500 3501 PetscFunctionBegin; 3502 PetscValidHeaderSpecific(mat,MAT_CLASSID,2); 3503 PetscValidType(mat,2); 3504 PetscValidPointer(fact,1); 3505 PetscValidHeaderSpecific(fact,MAT_CLASSID,1); 3506 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3507 if (mat->rmap->N != (fact)->rmap->N || mat->cmap->N != (fact)->cmap->N) SETERRQ4(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Mat fact: global dimensions are different %D should = %D %D should = %D",mat->rmap->N,(fact)->rmap->N,mat->cmap->N,(fact)->cmap->N); 3508 3509 MatCheckPreallocated(mat,2); 3510 if (!info) { 3511 ierr = MatFactorInfoInitialize(&tinfo);CHKERRQ(ierr); 3512 info = &tinfo; 3513 } 3514 3515 if (!fact->trivialsymbolic) {ierr = PetscLogEventBegin(MAT_QRFactorNumeric,mat,fact,0,0);CHKERRQ(ierr);} 3516 else {ierr = PetscLogEventBegin(MAT_QRFactor,mat,fact,0,0);CHKERRQ(ierr);} 3517 ierr = PetscUseMethod(fact,"MatQRFactorNumeric_C", (Mat,Mat,const MatFactorInfo*), (fact, mat, info));CHKERRQ(ierr); 3518 if (!fact->trivialsymbolic) {ierr = PetscLogEventEnd(MAT_QRFactorNumeric,mat,fact,0,0);CHKERRQ(ierr);} 3519 else {ierr = PetscLogEventEnd(MAT_QRFactor,mat,fact,0,0);CHKERRQ(ierr);} 3520 ierr = MatViewFromOptions(fact,NULL,"-mat_factor_view");CHKERRQ(ierr); 3521 ierr = PetscObjectStateIncrease((PetscObject)fact);CHKERRQ(ierr); 3522 PetscFunctionReturn(0); 3523 } 3524 3525 /* ----------------------------------------------------------------*/ 3526 /*@ 3527 MatSolve - Solves A x = b, given a factored matrix. 3528 3529 Neighbor-wise Collective on Mat 3530 3531 Input Parameters: 3532 + mat - the factored matrix 3533 - b - the right-hand-side vector 3534 3535 Output Parameter: 3536 . x - the result vector 3537 3538 Notes: 3539 The vectors b and x cannot be the same. I.e., one cannot 3540 call MatSolve(A,x,x). 3541 3542 Notes: 3543 Most users should employ the simplified KSP interface for linear solvers 3544 instead of working directly with matrix algebra routines such as this. 3545 See, e.g., KSPCreate(). 3546 3547 Level: developer 3548 3549 .seealso: MatSolveAdd(), MatSolveTranspose(), MatSolveTransposeAdd() 3550 @*/ 3551 PetscErrorCode MatSolve(Mat mat,Vec b,Vec x) 3552 { 3553 PetscErrorCode ierr; 3554 3555 PetscFunctionBegin; 3556 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3557 PetscValidType(mat,1); 3558 PetscValidHeaderSpecific(b,VEC_CLASSID,2); 3559 PetscValidHeaderSpecific(x,VEC_CLASSID,3); 3560 PetscCheckSameComm(mat,1,b,2); 3561 PetscCheckSameComm(mat,1,x,3); 3562 if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 3563 if (mat->cmap->N != x->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->cmap->N,x->map->N); 3564 if (mat->rmap->N != b->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %D %D",mat->rmap->N,b->map->N); 3565 if (mat->rmap->n != b->map->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %D %D",mat->rmap->n,b->map->n); 3566 if (!mat->rmap->N && !mat->cmap->N) PetscFunctionReturn(0); 3567 MatCheckPreallocated(mat,1); 3568 3569 ierr = PetscLogEventBegin(MAT_Solve,mat,b,x,0);CHKERRQ(ierr); 3570 if (mat->factorerrortype) { 3571 ierr = PetscInfo1(mat,"MatFactorError %D\n",mat->factorerrortype);CHKERRQ(ierr); 3572 ierr = VecSetInf(x);CHKERRQ(ierr); 3573 } else { 3574 if (!mat->ops->solve) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 3575 ierr = (*mat->ops->solve)(mat,b,x);CHKERRQ(ierr); 3576 } 3577 ierr = PetscLogEventEnd(MAT_Solve,mat,b,x,0);CHKERRQ(ierr); 3578 ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr); 3579 PetscFunctionReturn(0); 3580 } 3581 3582 static PetscErrorCode MatMatSolve_Basic(Mat A,Mat B,Mat X,PetscBool trans) 3583 { 3584 PetscErrorCode ierr; 3585 Vec b,x; 3586 PetscInt m,N,i; 3587 PetscScalar *bb,*xx; 3588 PetscErrorCode (*f)(Mat,Vec,Vec); 3589 3590 PetscFunctionBegin; 3591 if (A->factorerrortype) { 3592 ierr = PetscInfo1(A,"MatFactorError %D\n",A->factorerrortype);CHKERRQ(ierr); 3593 ierr = MatSetInf(X);CHKERRQ(ierr); 3594 PetscFunctionReturn(0); 3595 } 3596 f = trans ? A->ops->solvetranspose : A->ops->solve; 3597 if (!f) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Mat type %s",((PetscObject)A)->type_name); 3598 3599 ierr = MatDenseGetArrayRead(B,(const PetscScalar**)&bb);CHKERRQ(ierr); 3600 ierr = MatDenseGetArray(X,&xx);CHKERRQ(ierr); 3601 ierr = MatGetLocalSize(B,&m,NULL);CHKERRQ(ierr); /* number local rows */ 3602 ierr = MatGetSize(B,NULL,&N);CHKERRQ(ierr); /* total columns in dense matrix */ 3603 ierr = MatCreateVecs(A,&x,&b);CHKERRQ(ierr); 3604 for (i=0; i<N; i++) { 3605 ierr = VecPlaceArray(b,bb + i*m);CHKERRQ(ierr); 3606 ierr = VecPlaceArray(x,xx + i*m);CHKERRQ(ierr); 3607 ierr = (*f)(A,b,x);CHKERRQ(ierr); 3608 ierr = VecResetArray(x);CHKERRQ(ierr); 3609 ierr = VecResetArray(b);CHKERRQ(ierr); 3610 } 3611 ierr = VecDestroy(&b);CHKERRQ(ierr); 3612 ierr = VecDestroy(&x);CHKERRQ(ierr); 3613 ierr = MatDenseRestoreArrayRead(B,(const PetscScalar**)&bb);CHKERRQ(ierr); 3614 ierr = MatDenseRestoreArray(X,&xx);CHKERRQ(ierr); 3615 PetscFunctionReturn(0); 3616 } 3617 3618 /*@ 3619 MatMatSolve - Solves A X = B, given a factored matrix. 3620 3621 Neighbor-wise Collective on Mat 3622 3623 Input Parameters: 3624 + A - the factored matrix 3625 - B - the right-hand-side matrix MATDENSE (or sparse -- when using MUMPS) 3626 3627 Output Parameter: 3628 . X - the result matrix (dense matrix) 3629 3630 Notes: 3631 If B is a MATDENSE matrix then one can call MatMatSolve(A,B,B) except with MKL_CPARDISO; 3632 otherwise, B and X cannot be the same. 3633 3634 Notes: 3635 Most users should usually employ the simplified KSP interface for linear solvers 3636 instead of working directly with matrix algebra routines such as this. 3637 See, e.g., KSPCreate(). However KSP can only solve for one vector (column of X) 3638 at a time. 3639 3640 Level: developer 3641 3642 .seealso: MatMatSolveTranspose(), MatLUFactor(), MatCholeskyFactor() 3643 @*/ 3644 PetscErrorCode MatMatSolve(Mat A,Mat B,Mat X) 3645 { 3646 PetscErrorCode ierr; 3647 3648 PetscFunctionBegin; 3649 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 3650 PetscValidType(A,1); 3651 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 3652 PetscValidHeaderSpecific(X,MAT_CLASSID,3); 3653 PetscCheckSameComm(A,1,B,2); 3654 PetscCheckSameComm(A,1,X,3); 3655 if (A->cmap->N != X->rmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Mat A,Mat X: global dim %D %D",A->cmap->N,X->rmap->N); 3656 if (A->rmap->N != B->rmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Mat A,Mat B: global dim %D %D",A->rmap->N,B->rmap->N); 3657 if (X->cmap->N != B->cmap->N) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Solution matrix must have same number of columns as rhs matrix"); 3658 if (!A->rmap->N && !A->cmap->N) PetscFunctionReturn(0); 3659 if (!A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 3660 MatCheckPreallocated(A,1); 3661 3662 ierr = PetscLogEventBegin(MAT_MatSolve,A,B,X,0);CHKERRQ(ierr); 3663 if (!A->ops->matsolve) { 3664 ierr = PetscInfo1(A,"Mat type %s using basic MatMatSolve\n",((PetscObject)A)->type_name);CHKERRQ(ierr); 3665 ierr = MatMatSolve_Basic(A,B,X,PETSC_FALSE);CHKERRQ(ierr); 3666 } else { 3667 ierr = (*A->ops->matsolve)(A,B,X);CHKERRQ(ierr); 3668 } 3669 ierr = PetscLogEventEnd(MAT_MatSolve,A,B,X,0);CHKERRQ(ierr); 3670 ierr = PetscObjectStateIncrease((PetscObject)X);CHKERRQ(ierr); 3671 PetscFunctionReturn(0); 3672 } 3673 3674 /*@ 3675 MatMatSolveTranspose - Solves A^T X = B, given a factored matrix. 3676 3677 Neighbor-wise Collective on Mat 3678 3679 Input Parameters: 3680 + A - the factored matrix 3681 - B - the right-hand-side matrix (dense matrix) 3682 3683 Output Parameter: 3684 . X - the result matrix (dense matrix) 3685 3686 Notes: 3687 The matrices B and X cannot be the same. I.e., one cannot 3688 call MatMatSolveTranspose(A,X,X). 3689 3690 Notes: 3691 Most users should usually employ the simplified KSP interface for linear solvers 3692 instead of working directly with matrix algebra routines such as this. 3693 See, e.g., KSPCreate(). However KSP can only solve for one vector (column of X) 3694 at a time. 3695 3696 When using SuperLU_Dist or MUMPS as a parallel solver, PETSc will use their functionality to solve multiple right hand sides simultaneously. 3697 3698 Level: developer 3699 3700 .seealso: MatMatSolve(), MatLUFactor(), MatCholeskyFactor() 3701 @*/ 3702 PetscErrorCode MatMatSolveTranspose(Mat A,Mat B,Mat X) 3703 { 3704 PetscErrorCode ierr; 3705 3706 PetscFunctionBegin; 3707 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 3708 PetscValidType(A,1); 3709 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 3710 PetscValidHeaderSpecific(X,MAT_CLASSID,3); 3711 PetscCheckSameComm(A,1,B,2); 3712 PetscCheckSameComm(A,1,X,3); 3713 if (X == B) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_IDN,"X and B must be different matrices"); 3714 if (A->cmap->N != X->rmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Mat A,Mat X: global dim %D %D",A->cmap->N,X->rmap->N); 3715 if (A->rmap->N != B->rmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Mat A,Mat B: global dim %D %D",A->rmap->N,B->rmap->N); 3716 if (A->rmap->n != B->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat A,Mat B: local dim %D %D",A->rmap->n,B->rmap->n); 3717 if (X->cmap->N < B->cmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Solution matrix must have same number of columns as rhs matrix"); 3718 if (!A->rmap->N && !A->cmap->N) PetscFunctionReturn(0); 3719 if (!A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 3720 MatCheckPreallocated(A,1); 3721 3722 ierr = PetscLogEventBegin(MAT_MatSolve,A,B,X,0);CHKERRQ(ierr); 3723 if (!A->ops->matsolvetranspose) { 3724 ierr = PetscInfo1(A,"Mat type %s using basic MatMatSolveTranspose\n",((PetscObject)A)->type_name);CHKERRQ(ierr); 3725 ierr = MatMatSolve_Basic(A,B,X,PETSC_TRUE);CHKERRQ(ierr); 3726 } else { 3727 ierr = (*A->ops->matsolvetranspose)(A,B,X);CHKERRQ(ierr); 3728 } 3729 ierr = PetscLogEventEnd(MAT_MatSolve,A,B,X,0);CHKERRQ(ierr); 3730 ierr = PetscObjectStateIncrease((PetscObject)X);CHKERRQ(ierr); 3731 PetscFunctionReturn(0); 3732 } 3733 3734 /*@ 3735 MatMatTransposeSolve - Solves A X = B^T, given a factored matrix. 3736 3737 Neighbor-wise Collective on Mat 3738 3739 Input Parameters: 3740 + A - the factored matrix 3741 - Bt - the transpose of right-hand-side matrix 3742 3743 Output Parameter: 3744 . X - the result matrix (dense matrix) 3745 3746 Notes: 3747 Most users should usually employ the simplified KSP interface for linear solvers 3748 instead of working directly with matrix algebra routines such as this. 3749 See, e.g., KSPCreate(). However KSP can only solve for one vector (column of X) 3750 at a time. 3751 3752 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(). 3753 3754 Level: developer 3755 3756 .seealso: MatMatSolve(), MatMatSolveTranspose(), MatLUFactor(), MatCholeskyFactor() 3757 @*/ 3758 PetscErrorCode MatMatTransposeSolve(Mat A,Mat Bt,Mat X) 3759 { 3760 PetscErrorCode ierr; 3761 3762 PetscFunctionBegin; 3763 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 3764 PetscValidType(A,1); 3765 PetscValidHeaderSpecific(Bt,MAT_CLASSID,2); 3766 PetscValidHeaderSpecific(X,MAT_CLASSID,3); 3767 PetscCheckSameComm(A,1,Bt,2); 3768 PetscCheckSameComm(A,1,X,3); 3769 3770 if (X == Bt) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_IDN,"X and B must be different matrices"); 3771 if (A->cmap->N != X->rmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Mat A,Mat X: global dim %D %D",A->cmap->N,X->rmap->N); 3772 if (A->rmap->N != Bt->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Mat A,Mat Bt: global dim %D %D",A->rmap->N,Bt->cmap->N); 3773 if (X->cmap->N < Bt->rmap->N) SETERRQ(PetscObjectComm((PetscObject)X),PETSC_ERR_ARG_SIZ,"Solution matrix must have same number of columns as row number of the rhs matrix"); 3774 if (!A->rmap->N && !A->cmap->N) PetscFunctionReturn(0); 3775 if (!A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 3776 MatCheckPreallocated(A,1); 3777 3778 if (!A->ops->mattransposesolve) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Mat type %s",((PetscObject)A)->type_name); 3779 ierr = PetscLogEventBegin(MAT_MatTrSolve,A,Bt,X,0);CHKERRQ(ierr); 3780 ierr = (*A->ops->mattransposesolve)(A,Bt,X);CHKERRQ(ierr); 3781 ierr = PetscLogEventEnd(MAT_MatTrSolve,A,Bt,X,0);CHKERRQ(ierr); 3782 ierr = PetscObjectStateIncrease((PetscObject)X);CHKERRQ(ierr); 3783 PetscFunctionReturn(0); 3784 } 3785 3786 /*@ 3787 MatForwardSolve - Solves L x = b, given a factored matrix, A = LU, or 3788 U^T*D^(1/2) x = b, given a factored symmetric matrix, A = U^T*D*U, 3789 3790 Neighbor-wise Collective on Mat 3791 3792 Input Parameters: 3793 + mat - the factored matrix 3794 - b - the right-hand-side vector 3795 3796 Output Parameter: 3797 . x - the result vector 3798 3799 Notes: 3800 MatSolve() should be used for most applications, as it performs 3801 a forward solve followed by a backward solve. 3802 3803 The vectors b and x cannot be the same, i.e., one cannot 3804 call MatForwardSolve(A,x,x). 3805 3806 For matrix in seqsbaij format with block size larger than 1, 3807 the diagonal blocks are not implemented as D = D^(1/2) * D^(1/2) yet. 3808 MatForwardSolve() solves U^T*D y = b, and 3809 MatBackwardSolve() solves U x = y. 3810 Thus they do not provide a symmetric preconditioner. 3811 3812 Most users should employ the simplified KSP interface for linear solvers 3813 instead of working directly with matrix algebra routines such as this. 3814 See, e.g., KSPCreate(). 3815 3816 Level: developer 3817 3818 .seealso: MatSolve(), MatBackwardSolve() 3819 @*/ 3820 PetscErrorCode MatForwardSolve(Mat mat,Vec b,Vec x) 3821 { 3822 PetscErrorCode ierr; 3823 3824 PetscFunctionBegin; 3825 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3826 PetscValidType(mat,1); 3827 PetscValidHeaderSpecific(b,VEC_CLASSID,2); 3828 PetscValidHeaderSpecific(x,VEC_CLASSID,3); 3829 PetscCheckSameComm(mat,1,b,2); 3830 PetscCheckSameComm(mat,1,x,3); 3831 if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 3832 if (mat->cmap->N != x->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->cmap->N,x->map->N); 3833 if (mat->rmap->N != b->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %D %D",mat->rmap->N,b->map->N); 3834 if (mat->rmap->n != b->map->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %D %D",mat->rmap->n,b->map->n); 3835 if (!mat->rmap->N && !mat->cmap->N) PetscFunctionReturn(0); 3836 MatCheckPreallocated(mat,1); 3837 3838 if (!mat->ops->forwardsolve) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 3839 ierr = PetscLogEventBegin(MAT_ForwardSolve,mat,b,x,0);CHKERRQ(ierr); 3840 ierr = (*mat->ops->forwardsolve)(mat,b,x);CHKERRQ(ierr); 3841 ierr = PetscLogEventEnd(MAT_ForwardSolve,mat,b,x,0);CHKERRQ(ierr); 3842 ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr); 3843 PetscFunctionReturn(0); 3844 } 3845 3846 /*@ 3847 MatBackwardSolve - Solves U x = b, given a factored matrix, A = LU. 3848 D^(1/2) U x = b, given a factored symmetric matrix, A = U^T*D*U, 3849 3850 Neighbor-wise Collective on Mat 3851 3852 Input Parameters: 3853 + mat - the factored matrix 3854 - b - the right-hand-side vector 3855 3856 Output Parameter: 3857 . x - the result vector 3858 3859 Notes: 3860 MatSolve() should be used for most applications, as it performs 3861 a forward solve followed by a backward solve. 3862 3863 The vectors b and x cannot be the same. I.e., one cannot 3864 call MatBackwardSolve(A,x,x). 3865 3866 For matrix in seqsbaij format with block size larger than 1, 3867 the diagonal blocks are not implemented as D = D^(1/2) * D^(1/2) yet. 3868 MatForwardSolve() solves U^T*D y = b, and 3869 MatBackwardSolve() solves U x = y. 3870 Thus they do not provide a symmetric preconditioner. 3871 3872 Most users should employ the simplified KSP interface for linear solvers 3873 instead of working directly with matrix algebra routines such as this. 3874 See, e.g., KSPCreate(). 3875 3876 Level: developer 3877 3878 .seealso: MatSolve(), MatForwardSolve() 3879 @*/ 3880 PetscErrorCode MatBackwardSolve(Mat mat,Vec b,Vec x) 3881 { 3882 PetscErrorCode ierr; 3883 3884 PetscFunctionBegin; 3885 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3886 PetscValidType(mat,1); 3887 PetscValidHeaderSpecific(b,VEC_CLASSID,2); 3888 PetscValidHeaderSpecific(x,VEC_CLASSID,3); 3889 PetscCheckSameComm(mat,1,b,2); 3890 PetscCheckSameComm(mat,1,x,3); 3891 if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 3892 if (mat->cmap->N != x->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->cmap->N,x->map->N); 3893 if (mat->rmap->N != b->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %D %D",mat->rmap->N,b->map->N); 3894 if (mat->rmap->n != b->map->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %D %D",mat->rmap->n,b->map->n); 3895 if (!mat->rmap->N && !mat->cmap->N) PetscFunctionReturn(0); 3896 MatCheckPreallocated(mat,1); 3897 3898 if (!mat->ops->backwardsolve) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 3899 ierr = PetscLogEventBegin(MAT_BackwardSolve,mat,b,x,0);CHKERRQ(ierr); 3900 ierr = (*mat->ops->backwardsolve)(mat,b,x);CHKERRQ(ierr); 3901 ierr = PetscLogEventEnd(MAT_BackwardSolve,mat,b,x,0);CHKERRQ(ierr); 3902 ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr); 3903 PetscFunctionReturn(0); 3904 } 3905 3906 /*@ 3907 MatSolveAdd - Computes x = y + inv(A)*b, given a factored matrix. 3908 3909 Neighbor-wise Collective on Mat 3910 3911 Input Parameters: 3912 + mat - the factored matrix 3913 . b - the right-hand-side vector 3914 - y - the vector to be added to 3915 3916 Output Parameter: 3917 . x - the result vector 3918 3919 Notes: 3920 The vectors b and x cannot be the same. I.e., one cannot 3921 call MatSolveAdd(A,x,y,x). 3922 3923 Most users should employ the simplified KSP interface for linear solvers 3924 instead of working directly with matrix algebra routines such as this. 3925 See, e.g., KSPCreate(). 3926 3927 Level: developer 3928 3929 .seealso: MatSolve(), MatSolveTranspose(), MatSolveTransposeAdd() 3930 @*/ 3931 PetscErrorCode MatSolveAdd(Mat mat,Vec b,Vec y,Vec x) 3932 { 3933 PetscScalar one = 1.0; 3934 Vec tmp; 3935 PetscErrorCode ierr; 3936 3937 PetscFunctionBegin; 3938 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3939 PetscValidType(mat,1); 3940 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 3941 PetscValidHeaderSpecific(b,VEC_CLASSID,2); 3942 PetscValidHeaderSpecific(x,VEC_CLASSID,4); 3943 PetscCheckSameComm(mat,1,b,2); 3944 PetscCheckSameComm(mat,1,y,3); 3945 PetscCheckSameComm(mat,1,x,4); 3946 if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 3947 if (mat->cmap->N != x->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->cmap->N,x->map->N); 3948 if (mat->rmap->N != b->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %D %D",mat->rmap->N,b->map->N); 3949 if (mat->rmap->N != y->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %D %D",mat->rmap->N,y->map->N); 3950 if (mat->rmap->n != b->map->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %D %D",mat->rmap->n,b->map->n); 3951 if (x->map->n != y->map->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Vec x,Vec y: local dim %D %D",x->map->n,y->map->n); 3952 if (!mat->rmap->N && !mat->cmap->N) PetscFunctionReturn(0); 3953 MatCheckPreallocated(mat,1); 3954 3955 ierr = PetscLogEventBegin(MAT_SolveAdd,mat,b,x,y);CHKERRQ(ierr); 3956 if (mat->factorerrortype) { 3957 ierr = PetscInfo1(mat,"MatFactorError %D\n",mat->factorerrortype);CHKERRQ(ierr); 3958 ierr = VecSetInf(x);CHKERRQ(ierr); 3959 } else if (mat->ops->solveadd) { 3960 ierr = (*mat->ops->solveadd)(mat,b,y,x);CHKERRQ(ierr); 3961 } else { 3962 /* do the solve then the add manually */ 3963 if (x != y) { 3964 ierr = MatSolve(mat,b,x);CHKERRQ(ierr); 3965 ierr = VecAXPY(x,one,y);CHKERRQ(ierr); 3966 } else { 3967 ierr = VecDuplicate(x,&tmp);CHKERRQ(ierr); 3968 ierr = PetscLogObjectParent((PetscObject)mat,(PetscObject)tmp);CHKERRQ(ierr); 3969 ierr = VecCopy(x,tmp);CHKERRQ(ierr); 3970 ierr = MatSolve(mat,b,x);CHKERRQ(ierr); 3971 ierr = VecAXPY(x,one,tmp);CHKERRQ(ierr); 3972 ierr = VecDestroy(&tmp);CHKERRQ(ierr); 3973 } 3974 } 3975 ierr = PetscLogEventEnd(MAT_SolveAdd,mat,b,x,y);CHKERRQ(ierr); 3976 ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr); 3977 PetscFunctionReturn(0); 3978 } 3979 3980 /*@ 3981 MatSolveTranspose - Solves A' x = b, given a factored matrix. 3982 3983 Neighbor-wise Collective on Mat 3984 3985 Input Parameters: 3986 + mat - the factored matrix 3987 - b - the right-hand-side vector 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 MatSolveTranspose(A,x,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(), MatSolveTransposeAdd() 4003 @*/ 4004 PetscErrorCode MatSolveTranspose(Mat mat,Vec b,Vec x) 4005 { 4006 PetscErrorCode ierr; 4007 4008 PetscFunctionBegin; 4009 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4010 PetscValidType(mat,1); 4011 PetscValidHeaderSpecific(b,VEC_CLASSID,2); 4012 PetscValidHeaderSpecific(x,VEC_CLASSID,3); 4013 PetscCheckSameComm(mat,1,b,2); 4014 PetscCheckSameComm(mat,1,x,3); 4015 if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 4016 if (mat->rmap->N != x->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->rmap->N,x->map->N); 4017 if (mat->cmap->N != b->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %D %D",mat->cmap->N,b->map->N); 4018 if (!mat->rmap->N && !mat->cmap->N) PetscFunctionReturn(0); 4019 MatCheckPreallocated(mat,1); 4020 ierr = PetscLogEventBegin(MAT_SolveTranspose,mat,b,x,0);CHKERRQ(ierr); 4021 if (mat->factorerrortype) { 4022 ierr = PetscInfo1(mat,"MatFactorError %D\n",mat->factorerrortype);CHKERRQ(ierr); 4023 ierr = VecSetInf(x);CHKERRQ(ierr); 4024 } else { 4025 if (!mat->ops->solvetranspose) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s",((PetscObject)mat)->type_name); 4026 ierr = (*mat->ops->solvetranspose)(mat,b,x);CHKERRQ(ierr); 4027 } 4028 ierr = PetscLogEventEnd(MAT_SolveTranspose,mat,b,x,0);CHKERRQ(ierr); 4029 ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr); 4030 PetscFunctionReturn(0); 4031 } 4032 4033 /*@ 4034 MatSolveTransposeAdd - Computes x = y + inv(Transpose(A)) b, given a 4035 factored matrix. 4036 4037 Neighbor-wise Collective on Mat 4038 4039 Input Parameters: 4040 + mat - the factored matrix 4041 . b - the right-hand-side vector 4042 - y - the vector to be added to 4043 4044 Output Parameter: 4045 . x - the result vector 4046 4047 Notes: 4048 The vectors b and x cannot be the same. I.e., one cannot 4049 call MatSolveTransposeAdd(A,x,y,x). 4050 4051 Most users should employ the simplified KSP interface for linear solvers 4052 instead of working directly with matrix algebra routines such as this. 4053 See, e.g., KSPCreate(). 4054 4055 Level: developer 4056 4057 .seealso: MatSolve(), MatSolveAdd(), MatSolveTranspose() 4058 @*/ 4059 PetscErrorCode MatSolveTransposeAdd(Mat mat,Vec b,Vec y,Vec x) 4060 { 4061 PetscScalar one = 1.0; 4062 PetscErrorCode ierr; 4063 Vec tmp; 4064 4065 PetscFunctionBegin; 4066 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4067 PetscValidType(mat,1); 4068 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 4069 PetscValidHeaderSpecific(b,VEC_CLASSID,2); 4070 PetscValidHeaderSpecific(x,VEC_CLASSID,4); 4071 PetscCheckSameComm(mat,1,b,2); 4072 PetscCheckSameComm(mat,1,y,3); 4073 PetscCheckSameComm(mat,1,x,4); 4074 if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 4075 if (mat->rmap->N != x->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->rmap->N,x->map->N); 4076 if (mat->cmap->N != b->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %D %D",mat->cmap->N,b->map->N); 4077 if (mat->cmap->N != y->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %D %D",mat->cmap->N,y->map->N); 4078 if (x->map->n != y->map->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Vec x,Vec y: local dim %D %D",x->map->n,y->map->n); 4079 if (!mat->rmap->N && !mat->cmap->N) PetscFunctionReturn(0); 4080 MatCheckPreallocated(mat,1); 4081 4082 ierr = PetscLogEventBegin(MAT_SolveTransposeAdd,mat,b,x,y);CHKERRQ(ierr); 4083 if (mat->factorerrortype) { 4084 ierr = PetscInfo1(mat,"MatFactorError %D\n",mat->factorerrortype);CHKERRQ(ierr); 4085 ierr = VecSetInf(x);CHKERRQ(ierr); 4086 } else if (mat->ops->solvetransposeadd) { 4087 ierr = (*mat->ops->solvetransposeadd)(mat,b,y,x);CHKERRQ(ierr); 4088 } else { 4089 /* do the solve then the add manually */ 4090 if (x != y) { 4091 ierr = MatSolveTranspose(mat,b,x);CHKERRQ(ierr); 4092 ierr = VecAXPY(x,one,y);CHKERRQ(ierr); 4093 } else { 4094 ierr = VecDuplicate(x,&tmp);CHKERRQ(ierr); 4095 ierr = PetscLogObjectParent((PetscObject)mat,(PetscObject)tmp);CHKERRQ(ierr); 4096 ierr = VecCopy(x,tmp);CHKERRQ(ierr); 4097 ierr = MatSolveTranspose(mat,b,x);CHKERRQ(ierr); 4098 ierr = VecAXPY(x,one,tmp);CHKERRQ(ierr); 4099 ierr = VecDestroy(&tmp);CHKERRQ(ierr); 4100 } 4101 } 4102 ierr = PetscLogEventEnd(MAT_SolveTransposeAdd,mat,b,x,y);CHKERRQ(ierr); 4103 ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr); 4104 PetscFunctionReturn(0); 4105 } 4106 /* ----------------------------------------------------------------*/ 4107 4108 /*@ 4109 MatSOR - Computes relaxation (SOR, Gauss-Seidel) sweeps. 4110 4111 Neighbor-wise Collective on Mat 4112 4113 Input Parameters: 4114 + mat - the matrix 4115 . b - the right hand side 4116 . omega - the relaxation factor 4117 . flag - flag indicating the type of SOR (see below) 4118 . shift - diagonal shift 4119 . its - the number of iterations 4120 - lits - the number of local iterations 4121 4122 Output Parameters: 4123 . x - the solution (can contain an initial guess, use option SOR_ZERO_INITIAL_GUESS to indicate no guess) 4124 4125 SOR Flags: 4126 + SOR_FORWARD_SWEEP - forward SOR 4127 . SOR_BACKWARD_SWEEP - backward SOR 4128 . SOR_SYMMETRIC_SWEEP - SSOR (symmetric SOR) 4129 . SOR_LOCAL_FORWARD_SWEEP - local forward SOR 4130 . SOR_LOCAL_BACKWARD_SWEEP - local forward SOR 4131 . SOR_LOCAL_SYMMETRIC_SWEEP - local SSOR 4132 . SOR_APPLY_UPPER, SOR_APPLY_LOWER - applies 4133 upper/lower triangular part of matrix to 4134 vector (with omega) 4135 - SOR_ZERO_INITIAL_GUESS - zero initial guess 4136 4137 Notes: 4138 SOR_LOCAL_FORWARD_SWEEP, SOR_LOCAL_BACKWARD_SWEEP, and 4139 SOR_LOCAL_SYMMETRIC_SWEEP perform separate independent smoothings 4140 on each processor. 4141 4142 Application programmers will not generally use MatSOR() directly, 4143 but instead will employ the KSP/PC interface. 4144 4145 Notes: 4146 for BAIJ, SBAIJ, and AIJ matrices with Inodes this does a block SOR smoothing, otherwise it does a pointwise smoothing 4147 4148 Notes for Advanced Users: 4149 The flags are implemented as bitwise inclusive or operations. 4150 For example, use (SOR_ZERO_INITIAL_GUESS | SOR_SYMMETRIC_SWEEP) 4151 to specify a zero initial guess for SSOR. 4152 4153 Most users should employ the simplified KSP interface for linear solvers 4154 instead of working directly with matrix algebra routines such as this. 4155 See, e.g., KSPCreate(). 4156 4157 Vectors x and b CANNOT be the same 4158 4159 Developer Note: We should add block SOR support for AIJ matrices with block size set to great than one and no inodes 4160 4161 Level: developer 4162 4163 @*/ 4164 PetscErrorCode MatSOR(Mat mat,Vec b,PetscReal omega,MatSORType flag,PetscReal shift,PetscInt its,PetscInt lits,Vec x) 4165 { 4166 PetscErrorCode ierr; 4167 4168 PetscFunctionBegin; 4169 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4170 PetscValidType(mat,1); 4171 PetscValidHeaderSpecific(b,VEC_CLASSID,2); 4172 PetscValidHeaderSpecific(x,VEC_CLASSID,8); 4173 PetscCheckSameComm(mat,1,b,2); 4174 PetscCheckSameComm(mat,1,x,8); 4175 if (!mat->ops->sor) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 4176 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4177 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4178 if (mat->cmap->N != x->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->cmap->N,x->map->N); 4179 if (mat->rmap->N != b->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %D %D",mat->rmap->N,b->map->N); 4180 if (mat->rmap->n != b->map->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %D %D",mat->rmap->n,b->map->n); 4181 if (its <= 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Relaxation requires global its %D positive",its); 4182 if (lits <= 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Relaxation requires local its %D positive",lits); 4183 if (b == x) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"b and x vector cannot be the same"); 4184 4185 MatCheckPreallocated(mat,1); 4186 ierr = PetscLogEventBegin(MAT_SOR,mat,b,x,0);CHKERRQ(ierr); 4187 ierr =(*mat->ops->sor)(mat,b,omega,flag,shift,its,lits,x);CHKERRQ(ierr); 4188 ierr = PetscLogEventEnd(MAT_SOR,mat,b,x,0);CHKERRQ(ierr); 4189 ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr); 4190 PetscFunctionReturn(0); 4191 } 4192 4193 /* 4194 Default matrix copy routine. 4195 */ 4196 PetscErrorCode MatCopy_Basic(Mat A,Mat B,MatStructure str) 4197 { 4198 PetscErrorCode ierr; 4199 PetscInt i,rstart = 0,rend = 0,nz; 4200 const PetscInt *cwork; 4201 const PetscScalar *vwork; 4202 4203 PetscFunctionBegin; 4204 if (B->assembled) { 4205 ierr = MatZeroEntries(B);CHKERRQ(ierr); 4206 } 4207 if (str == SAME_NONZERO_PATTERN) { 4208 ierr = MatGetOwnershipRange(A,&rstart,&rend);CHKERRQ(ierr); 4209 for (i=rstart; i<rend; i++) { 4210 ierr = MatGetRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr); 4211 ierr = MatSetValues(B,1,&i,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr); 4212 ierr = MatRestoreRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr); 4213 } 4214 } else { 4215 ierr = MatAYPX(B,0.0,A,str);CHKERRQ(ierr); 4216 } 4217 ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4218 ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4219 PetscFunctionReturn(0); 4220 } 4221 4222 /*@ 4223 MatCopy - Copies a matrix to another matrix. 4224 4225 Collective on Mat 4226 4227 Input Parameters: 4228 + A - the matrix 4229 - str - SAME_NONZERO_PATTERN or DIFFERENT_NONZERO_PATTERN 4230 4231 Output Parameter: 4232 . B - where the copy is put 4233 4234 Notes: 4235 If you use SAME_NONZERO_PATTERN then the two matrices must have the same nonzero pattern or the routine will crash. 4236 4237 MatCopy() copies the matrix entries of a matrix to another existing 4238 matrix (after first zeroing the second matrix). A related routine is 4239 MatConvert(), which first creates a new matrix and then copies the data. 4240 4241 Level: intermediate 4242 4243 .seealso: MatConvert(), MatDuplicate() 4244 4245 @*/ 4246 PetscErrorCode MatCopy(Mat A,Mat B,MatStructure str) 4247 { 4248 PetscErrorCode ierr; 4249 PetscInt i; 4250 4251 PetscFunctionBegin; 4252 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 4253 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 4254 PetscValidType(A,1); 4255 PetscValidType(B,2); 4256 PetscCheckSameComm(A,1,B,2); 4257 MatCheckPreallocated(B,2); 4258 if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4259 if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4260 if (A->rmap->N != B->rmap->N || A->cmap->N != B->cmap->N) SETERRQ4(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Mat A,Mat B: global dim (%D,%D) (%D,%D)",A->rmap->N,B->rmap->N,A->cmap->N,B->cmap->N); 4261 MatCheckPreallocated(A,1); 4262 if (A == B) PetscFunctionReturn(0); 4263 4264 ierr = PetscLogEventBegin(MAT_Copy,A,B,0,0);CHKERRQ(ierr); 4265 if (A->ops->copy) { 4266 ierr = (*A->ops->copy)(A,B,str);CHKERRQ(ierr); 4267 } else { /* generic conversion */ 4268 ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr); 4269 } 4270 4271 B->stencil.dim = A->stencil.dim; 4272 B->stencil.noc = A->stencil.noc; 4273 for (i=0; i<=A->stencil.dim; i++) { 4274 B->stencil.dims[i] = A->stencil.dims[i]; 4275 B->stencil.starts[i] = A->stencil.starts[i]; 4276 } 4277 4278 ierr = PetscLogEventEnd(MAT_Copy,A,B,0,0);CHKERRQ(ierr); 4279 ierr = PetscObjectStateIncrease((PetscObject)B);CHKERRQ(ierr); 4280 PetscFunctionReturn(0); 4281 } 4282 4283 /*@C 4284 MatConvert - Converts a matrix to another matrix, either of the same 4285 or different type. 4286 4287 Collective on Mat 4288 4289 Input Parameters: 4290 + mat - the matrix 4291 . newtype - new matrix type. Use MATSAME to create a new matrix of the 4292 same type as the original matrix. 4293 - reuse - denotes if the destination matrix is to be created or reused. 4294 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 4295 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). 4296 4297 Output Parameter: 4298 . M - pointer to place new matrix 4299 4300 Notes: 4301 MatConvert() first creates a new matrix and then copies the data from 4302 the first matrix. A related routine is MatCopy(), which copies the matrix 4303 entries of one matrix to another already existing matrix context. 4304 4305 Cannot be used to convert a sequential matrix to parallel or parallel to sequential, 4306 the MPI communicator of the generated matrix is always the same as the communicator 4307 of the input matrix. 4308 4309 Level: intermediate 4310 4311 .seealso: MatCopy(), MatDuplicate() 4312 @*/ 4313 PetscErrorCode MatConvert(Mat mat, MatType newtype,MatReuse reuse,Mat *M) 4314 { 4315 PetscErrorCode ierr; 4316 PetscBool sametype,issame,flg,issymmetric,ishermitian; 4317 char convname[256],mtype[256]; 4318 Mat B; 4319 4320 PetscFunctionBegin; 4321 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4322 PetscValidType(mat,1); 4323 PetscValidPointer(M,4); 4324 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4325 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4326 MatCheckPreallocated(mat,1); 4327 4328 ierr = PetscOptionsGetString(((PetscObject)mat)->options,((PetscObject)mat)->prefix,"-matconvert_type",mtype,sizeof(mtype),&flg);CHKERRQ(ierr); 4329 if (flg) newtype = mtype; 4330 4331 ierr = PetscObjectTypeCompare((PetscObject)mat,newtype,&sametype);CHKERRQ(ierr); 4332 ierr = PetscStrcmp(newtype,"same",&issame);CHKERRQ(ierr); 4333 if ((reuse == MAT_INPLACE_MATRIX) && (mat != *M)) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"MAT_INPLACE_MATRIX requires same input and output matrix"); 4334 if ((reuse == MAT_REUSE_MATRIX) && (mat == *M)) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"MAT_REUSE_MATRIX means reuse matrix in final argument, perhaps you mean MAT_INPLACE_MATRIX"); 4335 4336 if ((reuse == MAT_INPLACE_MATRIX) && (issame || sametype)) { 4337 ierr = PetscInfo3(mat,"Early return for inplace %s %d %d\n",((PetscObject)mat)->type_name,sametype,issame);CHKERRQ(ierr); 4338 PetscFunctionReturn(0); 4339 } 4340 4341 /* Cache Mat options because some converter use MatHeaderReplace */ 4342 issymmetric = mat->symmetric; 4343 ishermitian = mat->hermitian; 4344 4345 if ((sametype || issame) && (reuse==MAT_INITIAL_MATRIX) && mat->ops->duplicate) { 4346 ierr = PetscInfo3(mat,"Calling duplicate for initial matrix %s %d %d\n",((PetscObject)mat)->type_name,sametype,issame);CHKERRQ(ierr); 4347 ierr = (*mat->ops->duplicate)(mat,MAT_COPY_VALUES,M);CHKERRQ(ierr); 4348 } else { 4349 PetscErrorCode (*conv)(Mat, MatType,MatReuse,Mat*)=NULL; 4350 const char *prefix[3] = {"seq","mpi",""}; 4351 PetscInt i; 4352 /* 4353 Order of precedence: 4354 0) See if newtype is a superclass of the current matrix. 4355 1) See if a specialized converter is known to the current matrix. 4356 2) See if a specialized converter is known to the desired matrix class. 4357 3) See if a good general converter is registered for the desired class 4358 (as of 6/27/03 only MATMPIADJ falls into this category). 4359 4) See if a good general converter is known for the current matrix. 4360 5) Use a really basic converter. 4361 */ 4362 4363 /* 0) See if newtype is a superclass of the current matrix. 4364 i.e mat is mpiaij and newtype is aij */ 4365 for (i=0; i<2; i++) { 4366 ierr = PetscStrncpy(convname,prefix[i],sizeof(convname));CHKERRQ(ierr); 4367 ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr); 4368 ierr = PetscStrcmp(convname,((PetscObject)mat)->type_name,&flg);CHKERRQ(ierr); 4369 ierr = PetscInfo3(mat,"Check superclass %s %s -> %d\n",convname,((PetscObject)mat)->type_name,flg);CHKERRQ(ierr); 4370 if (flg) { 4371 if (reuse == MAT_INPLACE_MATRIX) { 4372 ierr = PetscInfo(mat,"Early return\n");CHKERRQ(ierr); 4373 PetscFunctionReturn(0); 4374 } else if (reuse == MAT_INITIAL_MATRIX && mat->ops->duplicate) { 4375 ierr = PetscInfo(mat,"Calling MatDuplicate\n");CHKERRQ(ierr); 4376 ierr = (*mat->ops->duplicate)(mat,MAT_COPY_VALUES,M);CHKERRQ(ierr); 4377 PetscFunctionReturn(0); 4378 } else if (reuse == MAT_REUSE_MATRIX && mat->ops->copy) { 4379 ierr = PetscInfo(mat,"Calling MatCopy\n");CHKERRQ(ierr); 4380 ierr = MatCopy(mat,*M,SAME_NONZERO_PATTERN);CHKERRQ(ierr); 4381 PetscFunctionReturn(0); 4382 } 4383 } 4384 } 4385 /* 1) See if a specialized converter is known to the current matrix and the desired class */ 4386 for (i=0; i<3; i++) { 4387 ierr = PetscStrncpy(convname,"MatConvert_",sizeof(convname));CHKERRQ(ierr); 4388 ierr = PetscStrlcat(convname,((PetscObject)mat)->type_name,sizeof(convname));CHKERRQ(ierr); 4389 ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr); 4390 ierr = PetscStrlcat(convname,prefix[i],sizeof(convname));CHKERRQ(ierr); 4391 ierr = PetscStrlcat(convname,issame ? ((PetscObject)mat)->type_name : newtype,sizeof(convname));CHKERRQ(ierr); 4392 ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr); 4393 ierr = PetscObjectQueryFunction((PetscObject)mat,convname,&conv);CHKERRQ(ierr); 4394 ierr = PetscInfo3(mat,"Check specialized (1) %s (%s) -> %d\n",convname,((PetscObject)mat)->type_name,!!conv);CHKERRQ(ierr); 4395 if (conv) goto foundconv; 4396 } 4397 4398 /* 2) See if a specialized converter is known to the desired matrix class. */ 4399 ierr = MatCreate(PetscObjectComm((PetscObject)mat),&B);CHKERRQ(ierr); 4400 ierr = MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->N,mat->cmap->N);CHKERRQ(ierr); 4401 ierr = MatSetType(B,newtype);CHKERRQ(ierr); 4402 for (i=0; i<3; i++) { 4403 ierr = PetscStrncpy(convname,"MatConvert_",sizeof(convname));CHKERRQ(ierr); 4404 ierr = PetscStrlcat(convname,((PetscObject)mat)->type_name,sizeof(convname));CHKERRQ(ierr); 4405 ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr); 4406 ierr = PetscStrlcat(convname,prefix[i],sizeof(convname));CHKERRQ(ierr); 4407 ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr); 4408 ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr); 4409 ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr); 4410 ierr = PetscInfo3(mat,"Check specialized (2) %s (%s) -> %d\n",convname,((PetscObject)B)->type_name,!!conv);CHKERRQ(ierr); 4411 if (conv) { 4412 ierr = MatDestroy(&B);CHKERRQ(ierr); 4413 goto foundconv; 4414 } 4415 } 4416 4417 /* 3) See if a good general converter is registered for the desired class */ 4418 conv = B->ops->convertfrom; 4419 ierr = PetscInfo2(mat,"Check convertfrom (%s) -> %d\n",((PetscObject)B)->type_name,!!conv);CHKERRQ(ierr); 4420 ierr = MatDestroy(&B);CHKERRQ(ierr); 4421 if (conv) goto foundconv; 4422 4423 /* 4) See if a good general converter is known for the current matrix */ 4424 if (mat->ops->convert) conv = mat->ops->convert; 4425 4426 ierr = PetscInfo2(mat,"Check general convert (%s) -> %d\n",((PetscObject)mat)->type_name,!!conv);CHKERRQ(ierr); 4427 if (conv) goto foundconv; 4428 4429 /* 5) Use a really basic converter. */ 4430 ierr = PetscInfo(mat,"Using MatConvert_Basic\n");CHKERRQ(ierr); 4431 conv = MatConvert_Basic; 4432 4433 foundconv: 4434 ierr = PetscLogEventBegin(MAT_Convert,mat,0,0,0);CHKERRQ(ierr); 4435 ierr = (*conv)(mat,newtype,reuse,M);CHKERRQ(ierr); 4436 if (mat->rmap->mapping && mat->cmap->mapping && !(*M)->rmap->mapping && !(*M)->cmap->mapping) { 4437 /* the block sizes must be same if the mappings are copied over */ 4438 (*M)->rmap->bs = mat->rmap->bs; 4439 (*M)->cmap->bs = mat->cmap->bs; 4440 ierr = PetscObjectReference((PetscObject)mat->rmap->mapping);CHKERRQ(ierr); 4441 ierr = PetscObjectReference((PetscObject)mat->cmap->mapping);CHKERRQ(ierr); 4442 (*M)->rmap->mapping = mat->rmap->mapping; 4443 (*M)->cmap->mapping = mat->cmap->mapping; 4444 } 4445 (*M)->stencil.dim = mat->stencil.dim; 4446 (*M)->stencil.noc = mat->stencil.noc; 4447 for (i=0; i<=mat->stencil.dim; i++) { 4448 (*M)->stencil.dims[i] = mat->stencil.dims[i]; 4449 (*M)->stencil.starts[i] = mat->stencil.starts[i]; 4450 } 4451 ierr = PetscLogEventEnd(MAT_Convert,mat,0,0,0);CHKERRQ(ierr); 4452 } 4453 ierr = PetscObjectStateIncrease((PetscObject)*M);CHKERRQ(ierr); 4454 4455 /* Copy Mat options */ 4456 if (issymmetric) { 4457 ierr = MatSetOption(*M,MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr); 4458 } 4459 if (ishermitian) { 4460 ierr = MatSetOption(*M,MAT_HERMITIAN,PETSC_TRUE);CHKERRQ(ierr); 4461 } 4462 PetscFunctionReturn(0); 4463 } 4464 4465 /*@C 4466 MatFactorGetSolverType - Returns name of the package providing the factorization routines 4467 4468 Not Collective 4469 4470 Input Parameter: 4471 . mat - the matrix, must be a factored matrix 4472 4473 Output Parameter: 4474 . type - the string name of the package (do not free this string) 4475 4476 Notes: 4477 In Fortran you pass in a empty string and the package name will be copied into it. 4478 (Make sure the string is long enough) 4479 4480 Level: intermediate 4481 4482 .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable(), MatGetFactor() 4483 @*/ 4484 PetscErrorCode MatFactorGetSolverType(Mat mat, MatSolverType *type) 4485 { 4486 PetscErrorCode ierr, (*conv)(Mat,MatSolverType*); 4487 4488 PetscFunctionBegin; 4489 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4490 PetscValidType(mat,1); 4491 if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Only for factored matrix"); 4492 ierr = PetscObjectQueryFunction((PetscObject)mat,"MatFactorGetSolverType_C",&conv);CHKERRQ(ierr); 4493 if (!conv) { 4494 *type = MATSOLVERPETSC; 4495 } else { 4496 ierr = (*conv)(mat,type);CHKERRQ(ierr); 4497 } 4498 PetscFunctionReturn(0); 4499 } 4500 4501 typedef struct _MatSolverTypeForSpecifcType* MatSolverTypeForSpecifcType; 4502 struct _MatSolverTypeForSpecifcType { 4503 MatType mtype; 4504 /* no entry for MAT_FACTOR_NONE */ 4505 PetscErrorCode (*createfactor[MAT_FACTOR_NUM_TYPES-1])(Mat,MatFactorType,Mat*); 4506 MatSolverTypeForSpecifcType next; 4507 }; 4508 4509 typedef struct _MatSolverTypeHolder* MatSolverTypeHolder; 4510 struct _MatSolverTypeHolder { 4511 char *name; 4512 MatSolverTypeForSpecifcType handlers; 4513 MatSolverTypeHolder next; 4514 }; 4515 4516 static MatSolverTypeHolder MatSolverTypeHolders = NULL; 4517 4518 /*@C 4519 MatSolverTypeRegister - Registers a MatSolverType that works for a particular matrix type 4520 4521 Input Parameters: 4522 + package - name of the package, for example petsc or superlu 4523 . mtype - the matrix type that works with this package 4524 . ftype - the type of factorization supported by the package 4525 - createfactor - routine that will create the factored matrix ready to be used 4526 4527 Level: intermediate 4528 4529 .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable(), MatGetFactor() 4530 @*/ 4531 PetscErrorCode MatSolverTypeRegister(MatSolverType package,MatType mtype,MatFactorType ftype,PetscErrorCode (*createfactor)(Mat,MatFactorType,Mat*)) 4532 { 4533 PetscErrorCode ierr; 4534 MatSolverTypeHolder next = MatSolverTypeHolders,prev = NULL; 4535 PetscBool flg; 4536 MatSolverTypeForSpecifcType inext,iprev = NULL; 4537 4538 PetscFunctionBegin; 4539 ierr = MatInitializePackage();CHKERRQ(ierr); 4540 if (!next) { 4541 ierr = PetscNew(&MatSolverTypeHolders);CHKERRQ(ierr); 4542 ierr = PetscStrallocpy(package,&MatSolverTypeHolders->name);CHKERRQ(ierr); 4543 ierr = PetscNew(&MatSolverTypeHolders->handlers);CHKERRQ(ierr); 4544 ierr = PetscStrallocpy(mtype,(char **)&MatSolverTypeHolders->handlers->mtype);CHKERRQ(ierr); 4545 MatSolverTypeHolders->handlers->createfactor[(int)ftype-1] = createfactor; 4546 PetscFunctionReturn(0); 4547 } 4548 while (next) { 4549 ierr = PetscStrcasecmp(package,next->name,&flg);CHKERRQ(ierr); 4550 if (flg) { 4551 if (!next->handlers) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"MatSolverTypeHolder is missing handlers"); 4552 inext = next->handlers; 4553 while (inext) { 4554 ierr = PetscStrcasecmp(mtype,inext->mtype,&flg);CHKERRQ(ierr); 4555 if (flg) { 4556 inext->createfactor[(int)ftype-1] = createfactor; 4557 PetscFunctionReturn(0); 4558 } 4559 iprev = inext; 4560 inext = inext->next; 4561 } 4562 ierr = PetscNew(&iprev->next);CHKERRQ(ierr); 4563 ierr = PetscStrallocpy(mtype,(char **)&iprev->next->mtype);CHKERRQ(ierr); 4564 iprev->next->createfactor[(int)ftype-1] = createfactor; 4565 PetscFunctionReturn(0); 4566 } 4567 prev = next; 4568 next = next->next; 4569 } 4570 ierr = PetscNew(&prev->next);CHKERRQ(ierr); 4571 ierr = PetscStrallocpy(package,&prev->next->name);CHKERRQ(ierr); 4572 ierr = PetscNew(&prev->next->handlers);CHKERRQ(ierr); 4573 ierr = PetscStrallocpy(mtype,(char **)&prev->next->handlers->mtype);CHKERRQ(ierr); 4574 prev->next->handlers->createfactor[(int)ftype-1] = createfactor; 4575 PetscFunctionReturn(0); 4576 } 4577 4578 /*@C 4579 MatSolveTypeGet - Gets the function that creates the factor matrix if it exist 4580 4581 Input Parameters: 4582 + type - name of the package, for example petsc or superlu 4583 . ftype - the type of factorization supported by the type 4584 - mtype - the matrix type that works with this type 4585 4586 Output Parameters: 4587 + foundtype - PETSC_TRUE if the type was registered 4588 . foundmtype - PETSC_TRUE if the type supports the requested mtype 4589 - createfactor - routine that will create the factored matrix ready to be used or NULL if not found 4590 4591 Level: intermediate 4592 4593 .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable(), MatSolverTypeRegister(), MatGetFactor() 4594 @*/ 4595 PetscErrorCode MatSolverTypeGet(MatSolverType type,MatType mtype,MatFactorType ftype,PetscBool *foundtype,PetscBool *foundmtype,PetscErrorCode (**createfactor)(Mat,MatFactorType,Mat*)) 4596 { 4597 PetscErrorCode ierr; 4598 MatSolverTypeHolder next = MatSolverTypeHolders; 4599 PetscBool flg; 4600 MatSolverTypeForSpecifcType inext; 4601 4602 PetscFunctionBegin; 4603 if (foundtype) *foundtype = PETSC_FALSE; 4604 if (foundmtype) *foundmtype = PETSC_FALSE; 4605 if (createfactor) *createfactor = NULL; 4606 4607 if (type) { 4608 while (next) { 4609 ierr = PetscStrcasecmp(type,next->name,&flg);CHKERRQ(ierr); 4610 if (flg) { 4611 if (foundtype) *foundtype = PETSC_TRUE; 4612 inext = next->handlers; 4613 while (inext) { 4614 ierr = PetscStrbeginswith(mtype,inext->mtype,&flg);CHKERRQ(ierr); 4615 if (flg) { 4616 if (foundmtype) *foundmtype = PETSC_TRUE; 4617 if (createfactor) *createfactor = inext->createfactor[(int)ftype-1]; 4618 PetscFunctionReturn(0); 4619 } 4620 inext = inext->next; 4621 } 4622 } 4623 next = next->next; 4624 } 4625 } else { 4626 while (next) { 4627 inext = next->handlers; 4628 while (inext) { 4629 ierr = PetscStrcmp(mtype,inext->mtype,&flg);CHKERRQ(ierr); 4630 if (flg && inext->createfactor[(int)ftype-1]) { 4631 if (foundtype) *foundtype = PETSC_TRUE; 4632 if (foundmtype) *foundmtype = PETSC_TRUE; 4633 if (createfactor) *createfactor = inext->createfactor[(int)ftype-1]; 4634 PetscFunctionReturn(0); 4635 } 4636 inext = inext->next; 4637 } 4638 next = next->next; 4639 } 4640 /* try with base classes inext->mtype */ 4641 next = MatSolverTypeHolders; 4642 while (next) { 4643 inext = next->handlers; 4644 while (inext) { 4645 ierr = PetscStrbeginswith(mtype,inext->mtype,&flg);CHKERRQ(ierr); 4646 if (flg && inext->createfactor[(int)ftype-1]) { 4647 if (foundtype) *foundtype = PETSC_TRUE; 4648 if (foundmtype) *foundmtype = PETSC_TRUE; 4649 if (createfactor) *createfactor = inext->createfactor[(int)ftype-1]; 4650 PetscFunctionReturn(0); 4651 } 4652 inext = inext->next; 4653 } 4654 next = next->next; 4655 } 4656 } 4657 PetscFunctionReturn(0); 4658 } 4659 4660 PetscErrorCode MatSolverTypeDestroy(void) 4661 { 4662 PetscErrorCode ierr; 4663 MatSolverTypeHolder next = MatSolverTypeHolders,prev; 4664 MatSolverTypeForSpecifcType inext,iprev; 4665 4666 PetscFunctionBegin; 4667 while (next) { 4668 ierr = PetscFree(next->name);CHKERRQ(ierr); 4669 inext = next->handlers; 4670 while (inext) { 4671 ierr = PetscFree(inext->mtype);CHKERRQ(ierr); 4672 iprev = inext; 4673 inext = inext->next; 4674 ierr = PetscFree(iprev);CHKERRQ(ierr); 4675 } 4676 prev = next; 4677 next = next->next; 4678 ierr = PetscFree(prev);CHKERRQ(ierr); 4679 } 4680 MatSolverTypeHolders = NULL; 4681 PetscFunctionReturn(0); 4682 } 4683 4684 /*@C 4685 MatFactorGetCanUseOrdering - Indicates if the factorization can use the ordering provided in MatLUFactorSymbolic(), MatCholeskyFactorSymbolic() 4686 4687 Logically Collective on Mat 4688 4689 Input Parameters: 4690 . mat - the matrix 4691 4692 Output Parameters: 4693 . flg - PETSC_TRUE if uses the ordering 4694 4695 Notes: 4696 Most internal PETSc factorizations use the ordering passed to the factorization routine but external 4697 packages do not, thus we want to skip generating the ordering when it is not needed or used. 4698 4699 Level: developer 4700 4701 .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable(), MatGetFactor(), MatLUFactorSymbolic(), MatCholeskyFactorSymbolic() 4702 @*/ 4703 PetscErrorCode MatFactorGetCanUseOrdering(Mat mat, PetscBool *flg) 4704 { 4705 PetscFunctionBegin; 4706 *flg = mat->canuseordering; 4707 PetscFunctionReturn(0); 4708 } 4709 4710 /*@C 4711 MatFactorGetPreferredOrdering - The preferred ordering for a particular matrix factor object 4712 4713 Logically Collective on Mat 4714 4715 Input Parameters: 4716 . mat - the matrix 4717 4718 Output Parameters: 4719 . otype - the preferred type 4720 4721 Level: developer 4722 4723 .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable(), MatGetFactor(), MatLUFactorSymbolic(), MatCholeskyFactorSymbolic() 4724 @*/ 4725 PetscErrorCode MatFactorGetPreferredOrdering(Mat mat, MatFactorType ftype, MatOrderingType *otype) 4726 { 4727 PetscFunctionBegin; 4728 *otype = mat->preferredordering[ftype]; 4729 if (!*otype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"MatFactor did not have a preferred ordering"); 4730 PetscFunctionReturn(0); 4731 } 4732 4733 /*@C 4734 MatGetFactor - Returns a matrix suitable to calls to MatXXFactorSymbolic() 4735 4736 Collective on Mat 4737 4738 Input Parameters: 4739 + mat - the matrix 4740 . type - name of solver type, for example, superlu, petsc (to use PETSc's default) 4741 - ftype - factor type, MAT_FACTOR_LU, MAT_FACTOR_CHOLESKY, MAT_FACTOR_ICC, MAT_FACTOR_ILU, 4742 4743 Output Parameters: 4744 . f - the factor matrix used with MatXXFactorSymbolic() calls 4745 4746 Notes: 4747 Some PETSc matrix formats have alternative solvers available that are contained in alternative packages 4748 such as pastix, superlu, mumps etc. 4749 4750 PETSc must have been ./configure to use the external solver, using the option --download-package 4751 4752 Developer Notes: 4753 This should actually be called MatCreateFactor() since it creates a new factor object 4754 4755 Level: intermediate 4756 4757 .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable(), MatFactorGetCanUseOrdering(), MatSolverTypeRegister() 4758 @*/ 4759 PetscErrorCode MatGetFactor(Mat mat, MatSolverType type,MatFactorType ftype,Mat *f) 4760 { 4761 PetscErrorCode ierr,(*conv)(Mat,MatFactorType,Mat*); 4762 PetscBool foundtype,foundmtype; 4763 4764 PetscFunctionBegin; 4765 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4766 PetscValidType(mat,1); 4767 4768 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4769 MatCheckPreallocated(mat,1); 4770 4771 ierr = MatSolverTypeGet(type,((PetscObject)mat)->type_name,ftype,&foundtype,&foundmtype,&conv);CHKERRQ(ierr); 4772 if (!foundtype) { 4773 if (type) { 4774 SETERRQ4(PetscObjectComm((PetscObject)mat),PETSC_ERR_MISSING_FACTOR,"Could not locate solver type %s for factorization type %s and matrix type %s. Perhaps you must ./configure with --download-%s",type,MatFactorTypes[ftype],((PetscObject)mat)->type_name,type); 4775 } else { 4776 SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_MISSING_FACTOR,"Could not locate a solver type for factorization type %s and matrix type %s.",MatFactorTypes[ftype],((PetscObject)mat)->type_name); 4777 } 4778 } 4779 if (!foundmtype) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_MISSING_FACTOR,"MatSolverType %s does not support matrix type %s",type,((PetscObject)mat)->type_name); 4780 if (!conv) SETERRQ3(PetscObjectComm((PetscObject)mat),PETSC_ERR_MISSING_FACTOR,"MatSolverType %s does not support factorization type %s for matrix type %s",type,MatFactorTypes[ftype],((PetscObject)mat)->type_name); 4781 4782 ierr = (*conv)(mat,ftype,f);CHKERRQ(ierr); 4783 PetscFunctionReturn(0); 4784 } 4785 4786 /*@C 4787 MatGetFactorAvailable - Returns a a flag if matrix supports particular type and factor type 4788 4789 Not Collective 4790 4791 Input Parameters: 4792 + mat - the matrix 4793 . type - name of solver type, for example, superlu, petsc (to use PETSc's default) 4794 - ftype - factor type, MAT_FACTOR_LU, MAT_FACTOR_CHOLESKY, MAT_FACTOR_ICC, MAT_FACTOR_ILU, 4795 4796 Output Parameter: 4797 . flg - PETSC_TRUE if the factorization is available 4798 4799 Notes: 4800 Some PETSc matrix formats have alternative solvers available that are contained in alternative packages 4801 such as pastix, superlu, mumps etc. 4802 4803 PETSc must have been ./configure to use the external solver, using the option --download-package 4804 4805 Developer Notes: 4806 This should actually be called MatCreateFactorAvailable() since MatGetFactor() creates a new factor object 4807 4808 Level: intermediate 4809 4810 .seealso: MatCopy(), MatDuplicate(), MatGetFactor(), MatSolverTypeRegister() 4811 @*/ 4812 PetscErrorCode MatGetFactorAvailable(Mat mat, MatSolverType type,MatFactorType ftype,PetscBool *flg) 4813 { 4814 PetscErrorCode ierr, (*gconv)(Mat,MatFactorType,Mat*); 4815 4816 PetscFunctionBegin; 4817 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4818 PetscValidType(mat,1); 4819 4820 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4821 MatCheckPreallocated(mat,1); 4822 4823 *flg = PETSC_FALSE; 4824 ierr = MatSolverTypeGet(type,((PetscObject)mat)->type_name,ftype,NULL,NULL,&gconv);CHKERRQ(ierr); 4825 if (gconv) { 4826 *flg = PETSC_TRUE; 4827 } 4828 PetscFunctionReturn(0); 4829 } 4830 4831 #include <petscdmtypes.h> 4832 4833 /*@ 4834 MatDuplicate - Duplicates a matrix including the non-zero structure. 4835 4836 Collective on Mat 4837 4838 Input Parameters: 4839 + mat - the matrix 4840 - op - One of MAT_DO_NOT_COPY_VALUES, MAT_COPY_VALUES, or MAT_SHARE_NONZERO_PATTERN. 4841 See the manual page for MatDuplicateOption for an explanation of these options. 4842 4843 Output Parameter: 4844 . M - pointer to place new matrix 4845 4846 Level: intermediate 4847 4848 Notes: 4849 You cannot change the nonzero pattern for the parent or child matrix if you use MAT_SHARE_NONZERO_PATTERN. 4850 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. 4851 4852 .seealso: MatCopy(), MatConvert(), MatDuplicateOption 4853 @*/ 4854 PetscErrorCode MatDuplicate(Mat mat,MatDuplicateOption op,Mat *M) 4855 { 4856 PetscErrorCode ierr; 4857 Mat B; 4858 PetscInt i; 4859 DM dm; 4860 void (*viewf)(void); 4861 4862 PetscFunctionBegin; 4863 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4864 PetscValidType(mat,1); 4865 PetscValidPointer(M,3); 4866 if (op == MAT_COPY_VALUES && !mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"MAT_COPY_VALUES not allowed for unassembled matrix"); 4867 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4868 MatCheckPreallocated(mat,1); 4869 4870 *M = NULL; 4871 if (!mat->ops->duplicate) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Not written for matrix type %s\n",((PetscObject)mat)->type_name); 4872 ierr = PetscLogEventBegin(MAT_Convert,mat,0,0,0);CHKERRQ(ierr); 4873 ierr = (*mat->ops->duplicate)(mat,op,M);CHKERRQ(ierr); 4874 B = *M; 4875 4876 ierr = MatGetOperation(mat,MATOP_VIEW,&viewf);CHKERRQ(ierr); 4877 if (viewf) { 4878 ierr = MatSetOperation(B,MATOP_VIEW,viewf);CHKERRQ(ierr); 4879 } 4880 4881 B->stencil.dim = mat->stencil.dim; 4882 B->stencil.noc = mat->stencil.noc; 4883 for (i=0; i<=mat->stencil.dim; i++) { 4884 B->stencil.dims[i] = mat->stencil.dims[i]; 4885 B->stencil.starts[i] = mat->stencil.starts[i]; 4886 } 4887 4888 B->nooffproczerorows = mat->nooffproczerorows; 4889 B->nooffprocentries = mat->nooffprocentries; 4890 4891 ierr = PetscObjectQuery((PetscObject) mat, "__PETSc_dm", (PetscObject*) &dm);CHKERRQ(ierr); 4892 if (dm) { 4893 ierr = PetscObjectCompose((PetscObject) B, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr); 4894 } 4895 ierr = PetscLogEventEnd(MAT_Convert,mat,0,0,0);CHKERRQ(ierr); 4896 ierr = PetscObjectStateIncrease((PetscObject)B);CHKERRQ(ierr); 4897 PetscFunctionReturn(0); 4898 } 4899 4900 /*@ 4901 MatGetDiagonal - Gets the diagonal of a matrix. 4902 4903 Logically Collective on Mat 4904 4905 Input Parameters: 4906 + mat - the matrix 4907 - v - the vector for storing the diagonal 4908 4909 Output Parameter: 4910 . v - the diagonal of the matrix 4911 4912 Level: intermediate 4913 4914 Note: 4915 Currently only correct in parallel for square matrices. 4916 4917 .seealso: MatGetRow(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMaxAbs() 4918 @*/ 4919 PetscErrorCode MatGetDiagonal(Mat mat,Vec v) 4920 { 4921 PetscErrorCode ierr; 4922 4923 PetscFunctionBegin; 4924 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4925 PetscValidType(mat,1); 4926 PetscValidHeaderSpecific(v,VEC_CLASSID,2); 4927 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4928 if (!mat->ops->getdiagonal) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 4929 MatCheckPreallocated(mat,1); 4930 4931 ierr = (*mat->ops->getdiagonal)(mat,v);CHKERRQ(ierr); 4932 ierr = PetscObjectStateIncrease((PetscObject)v);CHKERRQ(ierr); 4933 PetscFunctionReturn(0); 4934 } 4935 4936 /*@C 4937 MatGetRowMin - Gets the minimum value (of the real part) of each 4938 row of the matrix 4939 4940 Logically Collective on Mat 4941 4942 Input Parameters: 4943 . mat - the matrix 4944 4945 Output Parameter: 4946 + v - the vector for storing the maximums 4947 - idx - the indices of the column found for each row (optional) 4948 4949 Level: intermediate 4950 4951 Notes: 4952 The result of this call are the same as if one converted the matrix to dense format 4953 and found the minimum value in each row (i.e. the implicit zeros are counted as zeros). 4954 4955 This code is only implemented for a couple of matrix formats. 4956 4957 .seealso: MatGetDiagonal(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMaxAbs(), 4958 MatGetRowMax() 4959 @*/ 4960 PetscErrorCode MatGetRowMin(Mat mat,Vec v,PetscInt idx[]) 4961 { 4962 PetscErrorCode ierr; 4963 4964 PetscFunctionBegin; 4965 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4966 PetscValidType(mat,1); 4967 PetscValidHeaderSpecific(v,VEC_CLASSID,2); 4968 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4969 4970 if (!mat->cmap->N) { 4971 ierr = VecSet(v,PETSC_MAX_REAL);CHKERRQ(ierr); 4972 if (idx) { 4973 PetscInt i,m = mat->rmap->n; 4974 for (i=0; i<m; i++) idx[i] = -1; 4975 } 4976 } else { 4977 if (!mat->ops->getrowmin) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 4978 MatCheckPreallocated(mat,1); 4979 } 4980 ierr = (*mat->ops->getrowmin)(mat,v,idx);CHKERRQ(ierr); 4981 ierr = PetscObjectStateIncrease((PetscObject)v);CHKERRQ(ierr); 4982 PetscFunctionReturn(0); 4983 } 4984 4985 /*@C 4986 MatGetRowMinAbs - Gets the minimum value (in absolute value) of each 4987 row of the matrix 4988 4989 Logically Collective on Mat 4990 4991 Input Parameters: 4992 . mat - the matrix 4993 4994 Output Parameter: 4995 + v - the vector for storing the minimums 4996 - idx - the indices of the column found for each row (or NULL if not needed) 4997 4998 Level: intermediate 4999 5000 Notes: 5001 if a row is completely empty or has only 0.0 values then the idx[] value for that 5002 row is 0 (the first column). 5003 5004 This code is only implemented for a couple of matrix formats. 5005 5006 .seealso: MatGetDiagonal(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMax(), MatGetRowMaxAbs(), MatGetRowMin() 5007 @*/ 5008 PetscErrorCode MatGetRowMinAbs(Mat mat,Vec v,PetscInt idx[]) 5009 { 5010 PetscErrorCode ierr; 5011 5012 PetscFunctionBegin; 5013 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5014 PetscValidType(mat,1); 5015 PetscValidHeaderSpecific(v,VEC_CLASSID,2); 5016 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5017 if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5018 5019 if (!mat->cmap->N) { 5020 ierr = VecSet(v,0.0);CHKERRQ(ierr); 5021 if (idx) { 5022 PetscInt i,m = mat->rmap->n; 5023 for (i=0; i<m; i++) idx[i] = -1; 5024 } 5025 } else { 5026 if (!mat->ops->getrowminabs) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 5027 MatCheckPreallocated(mat,1); 5028 if (idx) {ierr = PetscArrayzero(idx,mat->rmap->n);CHKERRQ(ierr);} 5029 ierr = (*mat->ops->getrowminabs)(mat,v,idx);CHKERRQ(ierr); 5030 } 5031 ierr = PetscObjectStateIncrease((PetscObject)v);CHKERRQ(ierr); 5032 PetscFunctionReturn(0); 5033 } 5034 5035 /*@C 5036 MatGetRowMax - Gets the maximum value (of the real part) of each 5037 row of the matrix 5038 5039 Logically Collective on Mat 5040 5041 Input Parameters: 5042 . mat - the matrix 5043 5044 Output Parameter: 5045 + v - the vector for storing the maximums 5046 - idx - the indices of the column found for each row (optional) 5047 5048 Level: intermediate 5049 5050 Notes: 5051 The result of this call are the same as if one converted the matrix to dense format 5052 and found the minimum value in each row (i.e. the implicit zeros are counted as zeros). 5053 5054 This code is only implemented for a couple of matrix formats. 5055 5056 .seealso: MatGetDiagonal(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMaxAbs(), MatGetRowMin() 5057 @*/ 5058 PetscErrorCode MatGetRowMax(Mat mat,Vec v,PetscInt idx[]) 5059 { 5060 PetscErrorCode ierr; 5061 5062 PetscFunctionBegin; 5063 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5064 PetscValidType(mat,1); 5065 PetscValidHeaderSpecific(v,VEC_CLASSID,2); 5066 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5067 5068 if (!mat->cmap->N) { 5069 ierr = VecSet(v,PETSC_MIN_REAL);CHKERRQ(ierr); 5070 if (idx) { 5071 PetscInt i,m = mat->rmap->n; 5072 for (i=0; i<m; i++) idx[i] = -1; 5073 } 5074 } else { 5075 if (!mat->ops->getrowmax) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 5076 MatCheckPreallocated(mat,1); 5077 ierr = (*mat->ops->getrowmax)(mat,v,idx);CHKERRQ(ierr); 5078 } 5079 ierr = PetscObjectStateIncrease((PetscObject)v);CHKERRQ(ierr); 5080 PetscFunctionReturn(0); 5081 } 5082 5083 /*@C 5084 MatGetRowMaxAbs - Gets the maximum value (in absolute value) of each 5085 row of the matrix 5086 5087 Logically Collective on Mat 5088 5089 Input Parameters: 5090 . mat - the matrix 5091 5092 Output Parameter: 5093 + v - the vector for storing the maximums 5094 - idx - the indices of the column found for each row (or NULL if not needed) 5095 5096 Level: intermediate 5097 5098 Notes: 5099 if a row is completely empty or has only 0.0 values then the idx[] value for that 5100 row is 0 (the first column). 5101 5102 This code is only implemented for a couple of matrix formats. 5103 5104 .seealso: MatGetDiagonal(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMax(), MatGetRowMin() 5105 @*/ 5106 PetscErrorCode MatGetRowMaxAbs(Mat mat,Vec v,PetscInt idx[]) 5107 { 5108 PetscErrorCode ierr; 5109 5110 PetscFunctionBegin; 5111 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5112 PetscValidType(mat,1); 5113 PetscValidHeaderSpecific(v,VEC_CLASSID,2); 5114 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5115 5116 if (!mat->cmap->N) { 5117 ierr = VecSet(v,0.0);CHKERRQ(ierr); 5118 if (idx) { 5119 PetscInt i,m = mat->rmap->n; 5120 for (i=0; i<m; i++) idx[i] = -1; 5121 } 5122 } else { 5123 if (!mat->ops->getrowmaxabs) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 5124 MatCheckPreallocated(mat,1); 5125 if (idx) {ierr = PetscArrayzero(idx,mat->rmap->n);CHKERRQ(ierr);} 5126 ierr = (*mat->ops->getrowmaxabs)(mat,v,idx);CHKERRQ(ierr); 5127 } 5128 ierr = PetscObjectStateIncrease((PetscObject)v);CHKERRQ(ierr); 5129 PetscFunctionReturn(0); 5130 } 5131 5132 /*@ 5133 MatGetRowSum - Gets the sum of each row of the matrix 5134 5135 Logically or Neighborhood Collective on Mat 5136 5137 Input Parameters: 5138 . mat - the matrix 5139 5140 Output Parameter: 5141 . v - the vector for storing the sum of rows 5142 5143 Level: intermediate 5144 5145 Notes: 5146 This code is slow since it is not currently specialized for different formats 5147 5148 .seealso: MatGetDiagonal(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMax(), MatGetRowMin() 5149 @*/ 5150 PetscErrorCode MatGetRowSum(Mat mat, Vec v) 5151 { 5152 Vec ones; 5153 PetscErrorCode ierr; 5154 5155 PetscFunctionBegin; 5156 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5157 PetscValidType(mat,1); 5158 PetscValidHeaderSpecific(v,VEC_CLASSID,2); 5159 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5160 MatCheckPreallocated(mat,1); 5161 ierr = MatCreateVecs(mat,&ones,NULL);CHKERRQ(ierr); 5162 ierr = VecSet(ones,1.);CHKERRQ(ierr); 5163 ierr = MatMult(mat,ones,v);CHKERRQ(ierr); 5164 ierr = VecDestroy(&ones);CHKERRQ(ierr); 5165 PetscFunctionReturn(0); 5166 } 5167 5168 /*@ 5169 MatTranspose - Computes an in-place or out-of-place transpose of a matrix. 5170 5171 Collective on Mat 5172 5173 Input Parameters: 5174 + mat - the matrix to transpose 5175 - reuse - either MAT_INITIAL_MATRIX, MAT_REUSE_MATRIX, or MAT_INPLACE_MATRIX 5176 5177 Output Parameter: 5178 . B - the transpose 5179 5180 Notes: 5181 If you use MAT_INPLACE_MATRIX then you must pass in &mat for B 5182 5183 MAT_REUSE_MATRIX causes the B matrix from a previous call to this function with MAT_INITIAL_MATRIX to be used 5184 5185 Consider using MatCreateTranspose() instead if you only need a matrix that behaves like the transpose, but don't need the storage to be changed. 5186 5187 Level: intermediate 5188 5189 .seealso: MatMultTranspose(), MatMultTransposeAdd(), MatIsTranspose(), MatReuse 5190 @*/ 5191 PetscErrorCode MatTranspose(Mat mat,MatReuse reuse,Mat *B) 5192 { 5193 PetscErrorCode ierr; 5194 5195 PetscFunctionBegin; 5196 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5197 PetscValidType(mat,1); 5198 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5199 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5200 if (!mat->ops->transpose) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 5201 if (reuse == MAT_INPLACE_MATRIX && mat != *B) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"MAT_INPLACE_MATRIX requires last matrix to match first"); 5202 if (reuse == MAT_REUSE_MATRIX && mat == *B) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Perhaps you mean MAT_INPLACE_MATRIX"); 5203 MatCheckPreallocated(mat,1); 5204 5205 ierr = PetscLogEventBegin(MAT_Transpose,mat,0,0,0);CHKERRQ(ierr); 5206 ierr = (*mat->ops->transpose)(mat,reuse,B);CHKERRQ(ierr); 5207 ierr = PetscLogEventEnd(MAT_Transpose,mat,0,0,0);CHKERRQ(ierr); 5208 if (B) {ierr = PetscObjectStateIncrease((PetscObject)*B);CHKERRQ(ierr);} 5209 PetscFunctionReturn(0); 5210 } 5211 5212 /*@ 5213 MatIsTranspose - Test whether a matrix is another one's transpose, 5214 or its own, in which case it tests symmetry. 5215 5216 Collective on Mat 5217 5218 Input Parameter: 5219 + A - the matrix to test 5220 - B - the matrix to test against, this can equal the first parameter 5221 5222 Output Parameters: 5223 . flg - the result 5224 5225 Notes: 5226 Only available for SeqAIJ/MPIAIJ matrices. The sequential algorithm 5227 has a running time of the order of the number of nonzeros; the parallel 5228 test involves parallel copies of the block-offdiagonal parts of the matrix. 5229 5230 Level: intermediate 5231 5232 .seealso: MatTranspose(), MatIsSymmetric(), MatIsHermitian() 5233 @*/ 5234 PetscErrorCode MatIsTranspose(Mat A,Mat B,PetscReal tol,PetscBool *flg) 5235 { 5236 PetscErrorCode ierr,(*f)(Mat,Mat,PetscReal,PetscBool*),(*g)(Mat,Mat,PetscReal,PetscBool*); 5237 5238 PetscFunctionBegin; 5239 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 5240 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 5241 PetscValidBoolPointer(flg,4); 5242 ierr = PetscObjectQueryFunction((PetscObject)A,"MatIsTranspose_C",&f);CHKERRQ(ierr); 5243 ierr = PetscObjectQueryFunction((PetscObject)B,"MatIsTranspose_C",&g);CHKERRQ(ierr); 5244 *flg = PETSC_FALSE; 5245 if (f && g) { 5246 if (f == g) { 5247 ierr = (*f)(A,B,tol,flg);CHKERRQ(ierr); 5248 } else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_NOTSAMETYPE,"Matrices do not have the same comparator for symmetry test"); 5249 } else { 5250 MatType mattype; 5251 if (!f) { 5252 ierr = MatGetType(A,&mattype);CHKERRQ(ierr); 5253 } else { 5254 ierr = MatGetType(B,&mattype);CHKERRQ(ierr); 5255 } 5256 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type %s does not support checking for transpose",mattype); 5257 } 5258 PetscFunctionReturn(0); 5259 } 5260 5261 /*@ 5262 MatHermitianTranspose - Computes an in-place or out-of-place transpose of a matrix in complex conjugate. 5263 5264 Collective on Mat 5265 5266 Input Parameters: 5267 + mat - the matrix to transpose and complex conjugate 5268 - reuse - either MAT_INITIAL_MATRIX, MAT_REUSE_MATRIX, or MAT_INPLACE_MATRIX 5269 5270 Output Parameter: 5271 . B - the Hermitian 5272 5273 Level: intermediate 5274 5275 .seealso: MatTranspose(), MatMultTranspose(), MatMultTransposeAdd(), MatIsTranspose(), MatReuse 5276 @*/ 5277 PetscErrorCode MatHermitianTranspose(Mat mat,MatReuse reuse,Mat *B) 5278 { 5279 PetscErrorCode ierr; 5280 5281 PetscFunctionBegin; 5282 ierr = MatTranspose(mat,reuse,B);CHKERRQ(ierr); 5283 #if defined(PETSC_USE_COMPLEX) 5284 ierr = MatConjugate(*B);CHKERRQ(ierr); 5285 #endif 5286 PetscFunctionReturn(0); 5287 } 5288 5289 /*@ 5290 MatIsHermitianTranspose - Test whether a matrix is another one's Hermitian transpose, 5291 5292 Collective on Mat 5293 5294 Input Parameter: 5295 + A - the matrix to test 5296 - B - the matrix to test against, this can equal the first parameter 5297 5298 Output Parameters: 5299 . flg - the result 5300 5301 Notes: 5302 Only available for SeqAIJ/MPIAIJ matrices. The sequential algorithm 5303 has a running time of the order of the number of nonzeros; the parallel 5304 test involves parallel copies of the block-offdiagonal parts of the matrix. 5305 5306 Level: intermediate 5307 5308 .seealso: MatTranspose(), MatIsSymmetric(), MatIsHermitian(), MatIsTranspose() 5309 @*/ 5310 PetscErrorCode MatIsHermitianTranspose(Mat A,Mat B,PetscReal tol,PetscBool *flg) 5311 { 5312 PetscErrorCode ierr,(*f)(Mat,Mat,PetscReal,PetscBool*),(*g)(Mat,Mat,PetscReal,PetscBool*); 5313 5314 PetscFunctionBegin; 5315 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 5316 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 5317 PetscValidBoolPointer(flg,4); 5318 ierr = PetscObjectQueryFunction((PetscObject)A,"MatIsHermitianTranspose_C",&f);CHKERRQ(ierr); 5319 ierr = PetscObjectQueryFunction((PetscObject)B,"MatIsHermitianTranspose_C",&g);CHKERRQ(ierr); 5320 if (f && g) { 5321 if (f==g) { 5322 ierr = (*f)(A,B,tol,flg);CHKERRQ(ierr); 5323 } else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_NOTSAMETYPE,"Matrices do not have the same comparator for Hermitian test"); 5324 } 5325 PetscFunctionReturn(0); 5326 } 5327 5328 /*@ 5329 MatPermute - Creates a new matrix with rows and columns permuted from the 5330 original. 5331 5332 Collective on Mat 5333 5334 Input Parameters: 5335 + mat - the matrix to permute 5336 . row - row permutation, each processor supplies only the permutation for its rows 5337 - col - column permutation, each processor supplies only the permutation for its columns 5338 5339 Output Parameters: 5340 . B - the permuted matrix 5341 5342 Level: advanced 5343 5344 Note: 5345 The index sets map from row/col of permuted matrix to row/col of original matrix. 5346 The index sets should be on the same communicator as Mat and have the same local sizes. 5347 5348 Developer Note: 5349 If you want to implement MatPermute for a matrix type, and your approach doesn't 5350 exploit the fact that row and col are permutations, consider implementing the 5351 more general MatCreateSubMatrix() instead. 5352 5353 .seealso: MatGetOrdering(), ISAllGather() 5354 5355 @*/ 5356 PetscErrorCode MatPermute(Mat mat,IS row,IS col,Mat *B) 5357 { 5358 PetscErrorCode ierr; 5359 5360 PetscFunctionBegin; 5361 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5362 PetscValidType(mat,1); 5363 PetscValidHeaderSpecific(row,IS_CLASSID,2); 5364 PetscValidHeaderSpecific(col,IS_CLASSID,3); 5365 PetscValidPointer(B,4); 5366 PetscCheckSameComm(mat,1,row,2); 5367 if (row != col) PetscCheckSameComm(row,2,col,3); 5368 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5369 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5370 if (!mat->ops->permute && !mat->ops->createsubmatrix) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatPermute not available for Mat type %s",((PetscObject)mat)->type_name); 5371 MatCheckPreallocated(mat,1); 5372 5373 if (mat->ops->permute) { 5374 ierr = (*mat->ops->permute)(mat,row,col,B);CHKERRQ(ierr); 5375 ierr = PetscObjectStateIncrease((PetscObject)*B);CHKERRQ(ierr); 5376 } else { 5377 ierr = MatCreateSubMatrix(mat, row, col, MAT_INITIAL_MATRIX, B);CHKERRQ(ierr); 5378 } 5379 PetscFunctionReturn(0); 5380 } 5381 5382 /*@ 5383 MatEqual - Compares two matrices. 5384 5385 Collective on Mat 5386 5387 Input Parameters: 5388 + A - the first matrix 5389 - B - the second matrix 5390 5391 Output Parameter: 5392 . flg - PETSC_TRUE if the matrices are equal; PETSC_FALSE otherwise. 5393 5394 Level: intermediate 5395 5396 @*/ 5397 PetscErrorCode MatEqual(Mat A,Mat B,PetscBool *flg) 5398 { 5399 PetscErrorCode ierr; 5400 5401 PetscFunctionBegin; 5402 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 5403 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 5404 PetscValidType(A,1); 5405 PetscValidType(B,2); 5406 PetscValidBoolPointer(flg,3); 5407 PetscCheckSameComm(A,1,B,2); 5408 MatCheckPreallocated(B,2); 5409 if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5410 if (!B->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5411 if (A->rmap->N != B->rmap->N || A->cmap->N != B->cmap->N) SETERRQ4(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Mat A,Mat B: global dim %D %D %D %D",A->rmap->N,B->rmap->N,A->cmap->N,B->cmap->N); 5412 if (!A->ops->equal) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Mat type %s",((PetscObject)A)->type_name); 5413 if (!B->ops->equal) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Mat type %s",((PetscObject)B)->type_name); 5414 if (A->ops->equal != B->ops->equal) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_INCOMP,"A is type: %s\nB is type: %s",((PetscObject)A)->type_name,((PetscObject)B)->type_name); 5415 MatCheckPreallocated(A,1); 5416 5417 ierr = (*A->ops->equal)(A,B,flg);CHKERRQ(ierr); 5418 PetscFunctionReturn(0); 5419 } 5420 5421 /*@ 5422 MatDiagonalScale - Scales a matrix on the left and right by diagonal 5423 matrices that are stored as vectors. Either of the two scaling 5424 matrices can be NULL. 5425 5426 Collective on Mat 5427 5428 Input Parameters: 5429 + mat - the matrix to be scaled 5430 . l - the left scaling vector (or NULL) 5431 - r - the right scaling vector (or NULL) 5432 5433 Notes: 5434 MatDiagonalScale() computes A = LAR, where 5435 L = a diagonal matrix (stored as a vector), R = a diagonal matrix (stored as a vector) 5436 The L scales the rows of the matrix, the R scales the columns of the matrix. 5437 5438 Level: intermediate 5439 5440 .seealso: MatScale(), MatShift(), MatDiagonalSet() 5441 @*/ 5442 PetscErrorCode MatDiagonalScale(Mat mat,Vec l,Vec r) 5443 { 5444 PetscErrorCode ierr; 5445 5446 PetscFunctionBegin; 5447 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5448 PetscValidType(mat,1); 5449 if (l) {PetscValidHeaderSpecific(l,VEC_CLASSID,2);PetscCheckSameComm(mat,1,l,2);} 5450 if (r) {PetscValidHeaderSpecific(r,VEC_CLASSID,3);PetscCheckSameComm(mat,1,r,3);} 5451 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5452 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5453 MatCheckPreallocated(mat,1); 5454 if (!l && !r) PetscFunctionReturn(0); 5455 5456 if (!mat->ops->diagonalscale) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 5457 ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 5458 ierr = (*mat->ops->diagonalscale)(mat,l,r);CHKERRQ(ierr); 5459 ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 5460 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 5461 PetscFunctionReturn(0); 5462 } 5463 5464 /*@ 5465 MatScale - Scales all elements of a matrix by a given number. 5466 5467 Logically Collective on Mat 5468 5469 Input Parameters: 5470 + mat - the matrix to be scaled 5471 - a - the scaling value 5472 5473 Output Parameter: 5474 . mat - the scaled matrix 5475 5476 Level: intermediate 5477 5478 .seealso: MatDiagonalScale() 5479 @*/ 5480 PetscErrorCode MatScale(Mat mat,PetscScalar a) 5481 { 5482 PetscErrorCode ierr; 5483 5484 PetscFunctionBegin; 5485 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5486 PetscValidType(mat,1); 5487 if (a != (PetscScalar)1.0 && !mat->ops->scale) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 5488 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5489 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5490 PetscValidLogicalCollectiveScalar(mat,a,2); 5491 MatCheckPreallocated(mat,1); 5492 5493 ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 5494 if (a != (PetscScalar)1.0) { 5495 ierr = (*mat->ops->scale)(mat,a);CHKERRQ(ierr); 5496 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 5497 } 5498 ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 5499 PetscFunctionReturn(0); 5500 } 5501 5502 /*@ 5503 MatNorm - Calculates various norms of a matrix. 5504 5505 Collective on Mat 5506 5507 Input Parameters: 5508 + mat - the matrix 5509 - type - the type of norm, NORM_1, NORM_FROBENIUS, NORM_INFINITY 5510 5511 Output Parameters: 5512 . nrm - the resulting norm 5513 5514 Level: intermediate 5515 5516 @*/ 5517 PetscErrorCode MatNorm(Mat mat,NormType type,PetscReal *nrm) 5518 { 5519 PetscErrorCode ierr; 5520 5521 PetscFunctionBegin; 5522 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5523 PetscValidType(mat,1); 5524 PetscValidRealPointer(nrm,3); 5525 5526 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5527 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5528 if (!mat->ops->norm) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 5529 MatCheckPreallocated(mat,1); 5530 5531 ierr = (*mat->ops->norm)(mat,type,nrm);CHKERRQ(ierr); 5532 PetscFunctionReturn(0); 5533 } 5534 5535 /* 5536 This variable is used to prevent counting of MatAssemblyBegin() that 5537 are called from within a MatAssemblyEnd(). 5538 */ 5539 static PetscInt MatAssemblyEnd_InUse = 0; 5540 /*@ 5541 MatAssemblyBegin - Begins assembling the matrix. This routine should 5542 be called after completing all calls to MatSetValues(). 5543 5544 Collective on Mat 5545 5546 Input Parameters: 5547 + mat - the matrix 5548 - type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY 5549 5550 Notes: 5551 MatSetValues() generally caches the values. The matrix is ready to 5552 use only after MatAssemblyBegin() and MatAssemblyEnd() have been called. 5553 Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES 5554 in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before 5555 using the matrix. 5556 5557 ALL processes that share a matrix MUST call MatAssemblyBegin() and MatAssemblyEnd() the SAME NUMBER of times, and each time with the 5558 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 5559 a global collective operation requring all processes that share the matrix. 5560 5561 Space for preallocated nonzeros that is not filled by a call to MatSetValues() or a related routine are compressed 5562 out by assembly. If you intend to use that extra space on a subsequent assembly, be sure to insert explicit zeros 5563 before MAT_FINAL_ASSEMBLY so the space is not compressed out. 5564 5565 Level: beginner 5566 5567 .seealso: MatAssemblyEnd(), MatSetValues(), MatAssembled() 5568 @*/ 5569 PetscErrorCode MatAssemblyBegin(Mat mat,MatAssemblyType type) 5570 { 5571 PetscErrorCode ierr; 5572 5573 PetscFunctionBegin; 5574 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5575 PetscValidType(mat,1); 5576 MatCheckPreallocated(mat,1); 5577 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix.\nDid you forget to call MatSetUnfactored()?"); 5578 if (mat->assembled) { 5579 mat->was_assembled = PETSC_TRUE; 5580 mat->assembled = PETSC_FALSE; 5581 } 5582 5583 if (!MatAssemblyEnd_InUse) { 5584 ierr = PetscLogEventBegin(MAT_AssemblyBegin,mat,0,0,0);CHKERRQ(ierr); 5585 if (mat->ops->assemblybegin) {ierr = (*mat->ops->assemblybegin)(mat,type);CHKERRQ(ierr);} 5586 ierr = PetscLogEventEnd(MAT_AssemblyBegin,mat,0,0,0);CHKERRQ(ierr); 5587 } else if (mat->ops->assemblybegin) { 5588 ierr = (*mat->ops->assemblybegin)(mat,type);CHKERRQ(ierr); 5589 } 5590 PetscFunctionReturn(0); 5591 } 5592 5593 /*@ 5594 MatAssembled - Indicates if a matrix has been assembled and is ready for 5595 use; for example, in matrix-vector product. 5596 5597 Not Collective 5598 5599 Input Parameter: 5600 . mat - the matrix 5601 5602 Output Parameter: 5603 . assembled - PETSC_TRUE or PETSC_FALSE 5604 5605 Level: advanced 5606 5607 .seealso: MatAssemblyEnd(), MatSetValues(), MatAssemblyBegin() 5608 @*/ 5609 PetscErrorCode MatAssembled(Mat mat,PetscBool *assembled) 5610 { 5611 PetscFunctionBegin; 5612 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5613 PetscValidPointer(assembled,2); 5614 *assembled = mat->assembled; 5615 PetscFunctionReturn(0); 5616 } 5617 5618 /*@ 5619 MatAssemblyEnd - Completes assembling the matrix. This routine should 5620 be called after MatAssemblyBegin(). 5621 5622 Collective on Mat 5623 5624 Input Parameters: 5625 + mat - the matrix 5626 - type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY 5627 5628 Options Database Keys: 5629 + -mat_view ::ascii_info - Prints info on matrix at conclusion of MatEndAssembly() 5630 . -mat_view ::ascii_info_detail - Prints more detailed info 5631 . -mat_view - Prints matrix in ASCII format 5632 . -mat_view ::ascii_matlab - Prints matrix in Matlab format 5633 . -mat_view draw - PetscDraws nonzero structure of matrix, using MatView() and PetscDrawOpenX(). 5634 . -display <name> - Sets display name (default is host) 5635 . -draw_pause <sec> - Sets number of seconds to pause after display 5636 . -mat_view socket - Sends matrix to socket, can be accessed from Matlab (See Users-Manual: ch_matlab) 5637 . -viewer_socket_machine <machine> - Machine to use for socket 5638 . -viewer_socket_port <port> - Port number to use for socket 5639 - -mat_view binary:filename[:append] - Save matrix to file in binary format 5640 5641 Notes: 5642 MatSetValues() generally caches the values. The matrix is ready to 5643 use only after MatAssemblyBegin() and MatAssemblyEnd() have been called. 5644 Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES 5645 in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before 5646 using the matrix. 5647 5648 Space for preallocated nonzeros that is not filled by a call to MatSetValues() or a related routine are compressed 5649 out by assembly. If you intend to use that extra space on a subsequent assembly, be sure to insert explicit zeros 5650 before MAT_FINAL_ASSEMBLY so the space is not compressed out. 5651 5652 Level: beginner 5653 5654 .seealso: MatAssemblyBegin(), MatSetValues(), PetscDrawOpenX(), PetscDrawCreate(), MatView(), MatAssembled(), PetscViewerSocketOpen() 5655 @*/ 5656 PetscErrorCode MatAssemblyEnd(Mat mat,MatAssemblyType type) 5657 { 5658 PetscErrorCode ierr; 5659 static PetscInt inassm = 0; 5660 PetscBool flg = PETSC_FALSE; 5661 5662 PetscFunctionBegin; 5663 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5664 PetscValidType(mat,1); 5665 5666 inassm++; 5667 MatAssemblyEnd_InUse++; 5668 if (MatAssemblyEnd_InUse == 1) { /* Do the logging only the first time through */ 5669 ierr = PetscLogEventBegin(MAT_AssemblyEnd,mat,0,0,0);CHKERRQ(ierr); 5670 if (mat->ops->assemblyend) { 5671 ierr = (*mat->ops->assemblyend)(mat,type);CHKERRQ(ierr); 5672 } 5673 ierr = PetscLogEventEnd(MAT_AssemblyEnd,mat,0,0,0);CHKERRQ(ierr); 5674 } else if (mat->ops->assemblyend) { 5675 ierr = (*mat->ops->assemblyend)(mat,type);CHKERRQ(ierr); 5676 } 5677 5678 /* Flush assembly is not a true assembly */ 5679 if (type != MAT_FLUSH_ASSEMBLY) { 5680 mat->num_ass++; 5681 mat->assembled = PETSC_TRUE; 5682 mat->ass_nonzerostate = mat->nonzerostate; 5683 } 5684 5685 mat->insertmode = NOT_SET_VALUES; 5686 MatAssemblyEnd_InUse--; 5687 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 5688 if (!mat->symmetric_eternal) { 5689 mat->symmetric_set = PETSC_FALSE; 5690 mat->hermitian_set = PETSC_FALSE; 5691 mat->structurally_symmetric_set = PETSC_FALSE; 5692 } 5693 if (inassm == 1 && type != MAT_FLUSH_ASSEMBLY) { 5694 ierr = MatViewFromOptions(mat,NULL,"-mat_view");CHKERRQ(ierr); 5695 5696 if (mat->checksymmetryonassembly) { 5697 ierr = MatIsSymmetric(mat,mat->checksymmetrytol,&flg);CHKERRQ(ierr); 5698 if (flg) { 5699 ierr = PetscPrintf(PetscObjectComm((PetscObject)mat),"Matrix is symmetric (tolerance %g)\n",(double)mat->checksymmetrytol);CHKERRQ(ierr); 5700 } else { 5701 ierr = PetscPrintf(PetscObjectComm((PetscObject)mat),"Matrix is not symmetric (tolerance %g)\n",(double)mat->checksymmetrytol);CHKERRQ(ierr); 5702 } 5703 } 5704 if (mat->nullsp && mat->checknullspaceonassembly) { 5705 ierr = MatNullSpaceTest(mat->nullsp,mat,NULL);CHKERRQ(ierr); 5706 } 5707 } 5708 inassm--; 5709 PetscFunctionReturn(0); 5710 } 5711 5712 /*@ 5713 MatSetOption - Sets a parameter option for a matrix. Some options 5714 may be specific to certain storage formats. Some options 5715 determine how values will be inserted (or added). Sorted, 5716 row-oriented input will generally assemble the fastest. The default 5717 is row-oriented. 5718 5719 Logically Collective on Mat for certain operations, such as MAT_SPD, not collective for MAT_ROW_ORIENTED, see MatOption 5720 5721 Input Parameters: 5722 + mat - the matrix 5723 . option - the option, one of those listed below (and possibly others), 5724 - flg - turn the option on (PETSC_TRUE) or off (PETSC_FALSE) 5725 5726 Options Describing Matrix Structure: 5727 + MAT_SPD - symmetric positive definite 5728 . MAT_SYMMETRIC - symmetric in terms of both structure and value 5729 . MAT_HERMITIAN - transpose is the complex conjugation 5730 . MAT_STRUCTURALLY_SYMMETRIC - symmetric nonzero structure 5731 - MAT_SYMMETRY_ETERNAL - if you would like the symmetry/Hermitian flag 5732 you set to be kept with all future use of the matrix 5733 including after MatAssemblyBegin/End() which could 5734 potentially change the symmetry structure, i.e. you 5735 KNOW the matrix will ALWAYS have the property you set. 5736 Note that setting this flag alone implies nothing about whether the matrix is symmetric/Hermitian; 5737 the relevant flags must be set independently. 5738 5739 Options For Use with MatSetValues(): 5740 Insert a logically dense subblock, which can be 5741 . MAT_ROW_ORIENTED - row-oriented (default) 5742 5743 Note these options reflect the data you pass in with MatSetValues(); it has 5744 nothing to do with how the data is stored internally in the matrix 5745 data structure. 5746 5747 When (re)assembling a matrix, we can restrict the input for 5748 efficiency/debugging purposes. These options include: 5749 + MAT_NEW_NONZERO_LOCATIONS - additional insertions will be allowed if they generate a new nonzero (slow) 5750 . MAT_FORCE_DIAGONAL_ENTRIES - forces diagonal entries to be allocated 5751 . MAT_IGNORE_OFF_PROC_ENTRIES - drops off-processor entries 5752 . MAT_NEW_NONZERO_LOCATION_ERR - generates an error for new matrix entry 5753 . MAT_USE_HASH_TABLE - uses a hash table to speed up matrix assembly 5754 . MAT_NO_OFF_PROC_ENTRIES - you know each process will only set values for its own rows, will generate an error if 5755 any process sets values for another process. This avoids all reductions in the MatAssembly routines and thus improves 5756 performance for very large process counts. 5757 - MAT_SUBSET_OFF_PROC_ENTRIES - you know that the first assembly after setting this flag will set a superset 5758 of the off-process entries required for all subsequent assemblies. This avoids a rendezvous step in the MatAssembly 5759 functions, instead sending only neighbor messages. 5760 5761 Notes: 5762 Except for MAT_UNUSED_NONZERO_LOCATION_ERR and MAT_ROW_ORIENTED all processes that share the matrix must pass the same value in flg! 5763 5764 Some options are relevant only for particular matrix types and 5765 are thus ignored by others. Other options are not supported by 5766 certain matrix types and will generate an error message if set. 5767 5768 If using a Fortran 77 module to compute a matrix, one may need to 5769 use the column-oriented option (or convert to the row-oriented 5770 format). 5771 5772 MAT_NEW_NONZERO_LOCATIONS set to PETSC_FALSE indicates that any add or insertion 5773 that would generate a new entry in the nonzero structure is instead 5774 ignored. Thus, if memory has not alredy been allocated for this particular 5775 data, then the insertion is ignored. For dense matrices, in which 5776 the entire array is allocated, no entries are ever ignored. 5777 Set after the first MatAssemblyEnd(). If this option is set then the MatAssemblyBegin/End() processes has one less global reduction 5778 5779 MAT_NEW_NONZERO_LOCATION_ERR set to PETSC_TRUE indicates that any add or insertion 5780 that would generate a new entry in the nonzero structure instead produces 5781 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 5782 5783 MAT_NEW_NONZERO_ALLOCATION_ERR set to PETSC_TRUE indicates that any add or insertion 5784 that would generate a new entry that has not been preallocated will 5785 instead produce an error. (Currently supported for AIJ and BAIJ formats 5786 only.) This is a useful flag when debugging matrix memory preallocation. 5787 If this option is set then the MatAssemblyBegin/End() processes has one less global reduction 5788 5789 MAT_IGNORE_OFF_PROC_ENTRIES set to PETSC_TRUE indicates entries destined for 5790 other processors should be dropped, rather than stashed. 5791 This is useful if you know that the "owning" processor is also 5792 always generating the correct matrix entries, so that PETSc need 5793 not transfer duplicate entries generated on another processor. 5794 5795 MAT_USE_HASH_TABLE indicates that a hash table be used to improve the 5796 searches during matrix assembly. When this flag is set, the hash table 5797 is created during the first Matrix Assembly. This hash table is 5798 used the next time through, during MatSetVaules()/MatSetVaulesBlocked() 5799 to improve the searching of indices. MAT_NEW_NONZERO_LOCATIONS flag 5800 should be used with MAT_USE_HASH_TABLE flag. This option is currently 5801 supported by MATMPIBAIJ format only. 5802 5803 MAT_KEEP_NONZERO_PATTERN indicates when MatZeroRows() is called the zeroed entries 5804 are kept in the nonzero structure 5805 5806 MAT_IGNORE_ZERO_ENTRIES - for AIJ/IS matrices this will stop zero values from creating 5807 a zero location in the matrix 5808 5809 MAT_USE_INODES - indicates using inode version of the code - works with AIJ matrix types 5810 5811 MAT_NO_OFF_PROC_ZERO_ROWS - you know each process will only zero its own rows. This avoids all reductions in the 5812 zero row routines and thus improves performance for very large process counts. 5813 5814 MAT_IGNORE_LOWER_TRIANGULAR - For SBAIJ matrices will ignore any insertions you make in the lower triangular 5815 part of the matrix (since they should match the upper triangular part). 5816 5817 MAT_SORTED_FULL - each process provides exactly its local rows; all column indices for a given row are passed in a 5818 single call to MatSetValues(), preallocation is perfect, row oriented, INSERT_VALUES is used. Common 5819 with finite difference schemes with non-periodic boundary conditions. 5820 5821 Level: intermediate 5822 5823 .seealso: MatOption, Mat 5824 5825 @*/ 5826 PetscErrorCode MatSetOption(Mat mat,MatOption op,PetscBool flg) 5827 { 5828 PetscErrorCode ierr; 5829 5830 PetscFunctionBegin; 5831 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5832 if (op > 0) { 5833 PetscValidLogicalCollectiveEnum(mat,op,2); 5834 PetscValidLogicalCollectiveBool(mat,flg,3); 5835 } 5836 5837 if (((int) op) <= MAT_OPTION_MIN || ((int) op) >= MAT_OPTION_MAX) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Options %d is out of range",(int)op); 5838 5839 switch (op) { 5840 case MAT_FORCE_DIAGONAL_ENTRIES: 5841 mat->force_diagonals = flg; 5842 PetscFunctionReturn(0); 5843 case MAT_NO_OFF_PROC_ENTRIES: 5844 mat->nooffprocentries = flg; 5845 PetscFunctionReturn(0); 5846 case MAT_SUBSET_OFF_PROC_ENTRIES: 5847 mat->assembly_subset = flg; 5848 if (!mat->assembly_subset) { /* See the same logic in VecAssembly wrt VEC_SUBSET_OFF_PROC_ENTRIES */ 5849 #if !defined(PETSC_HAVE_MPIUNI) 5850 ierr = MatStashScatterDestroy_BTS(&mat->stash);CHKERRQ(ierr); 5851 #endif 5852 mat->stash.first_assembly_done = PETSC_FALSE; 5853 } 5854 PetscFunctionReturn(0); 5855 case MAT_NO_OFF_PROC_ZERO_ROWS: 5856 mat->nooffproczerorows = flg; 5857 PetscFunctionReturn(0); 5858 case MAT_SPD: 5859 mat->spd_set = PETSC_TRUE; 5860 mat->spd = flg; 5861 if (flg) { 5862 mat->symmetric = PETSC_TRUE; 5863 mat->structurally_symmetric = PETSC_TRUE; 5864 mat->symmetric_set = PETSC_TRUE; 5865 mat->structurally_symmetric_set = PETSC_TRUE; 5866 } 5867 break; 5868 case MAT_SYMMETRIC: 5869 mat->symmetric = flg; 5870 if (flg) mat->structurally_symmetric = PETSC_TRUE; 5871 mat->symmetric_set = PETSC_TRUE; 5872 mat->structurally_symmetric_set = flg; 5873 #if !defined(PETSC_USE_COMPLEX) 5874 mat->hermitian = flg; 5875 mat->hermitian_set = PETSC_TRUE; 5876 #endif 5877 break; 5878 case MAT_HERMITIAN: 5879 mat->hermitian = flg; 5880 if (flg) mat->structurally_symmetric = PETSC_TRUE; 5881 mat->hermitian_set = PETSC_TRUE; 5882 mat->structurally_symmetric_set = flg; 5883 #if !defined(PETSC_USE_COMPLEX) 5884 mat->symmetric = flg; 5885 mat->symmetric_set = PETSC_TRUE; 5886 #endif 5887 break; 5888 case MAT_STRUCTURALLY_SYMMETRIC: 5889 mat->structurally_symmetric = flg; 5890 mat->structurally_symmetric_set = PETSC_TRUE; 5891 break; 5892 case MAT_SYMMETRY_ETERNAL: 5893 mat->symmetric_eternal = flg; 5894 break; 5895 case MAT_STRUCTURE_ONLY: 5896 mat->structure_only = flg; 5897 break; 5898 case MAT_SORTED_FULL: 5899 mat->sortedfull = flg; 5900 break; 5901 default: 5902 break; 5903 } 5904 if (mat->ops->setoption) { 5905 ierr = (*mat->ops->setoption)(mat,op,flg);CHKERRQ(ierr); 5906 } 5907 PetscFunctionReturn(0); 5908 } 5909 5910 /*@ 5911 MatGetOption - Gets a parameter option that has been set for a matrix. 5912 5913 Logically Collective on Mat for certain operations, such as MAT_SPD, not collective for MAT_ROW_ORIENTED, see MatOption 5914 5915 Input Parameters: 5916 + mat - the matrix 5917 - option - the option, this only responds to certain options, check the code for which ones 5918 5919 Output Parameter: 5920 . flg - turn the option on (PETSC_TRUE) or off (PETSC_FALSE) 5921 5922 Notes: 5923 Can only be called after MatSetSizes() and MatSetType() have been set. 5924 5925 Level: intermediate 5926 5927 .seealso: MatOption, MatSetOption() 5928 5929 @*/ 5930 PetscErrorCode MatGetOption(Mat mat,MatOption op,PetscBool *flg) 5931 { 5932 PetscFunctionBegin; 5933 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5934 PetscValidType(mat,1); 5935 5936 if (((int) op) <= MAT_OPTION_MIN || ((int) op) >= MAT_OPTION_MAX) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Options %d is out of range",(int)op); 5937 if (!((PetscObject)mat)->type_name) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_TYPENOTSET,"Cannot get options until type and size have been set, see MatSetType() and MatSetSizes()"); 5938 5939 switch (op) { 5940 case MAT_NO_OFF_PROC_ENTRIES: 5941 *flg = mat->nooffprocentries; 5942 break; 5943 case MAT_NO_OFF_PROC_ZERO_ROWS: 5944 *flg = mat->nooffproczerorows; 5945 break; 5946 case MAT_SYMMETRIC: 5947 *flg = mat->symmetric; 5948 break; 5949 case MAT_HERMITIAN: 5950 *flg = mat->hermitian; 5951 break; 5952 case MAT_STRUCTURALLY_SYMMETRIC: 5953 *flg = mat->structurally_symmetric; 5954 break; 5955 case MAT_SYMMETRY_ETERNAL: 5956 *flg = mat->symmetric_eternal; 5957 break; 5958 case MAT_SPD: 5959 *flg = mat->spd; 5960 break; 5961 default: 5962 break; 5963 } 5964 PetscFunctionReturn(0); 5965 } 5966 5967 /*@ 5968 MatZeroEntries - Zeros all entries of a matrix. For sparse matrices 5969 this routine retains the old nonzero structure. 5970 5971 Logically Collective on Mat 5972 5973 Input Parameters: 5974 . mat - the matrix 5975 5976 Level: intermediate 5977 5978 Notes: 5979 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. 5980 See the Performance chapter of the users manual for information on preallocating matrices. 5981 5982 .seealso: MatZeroRows() 5983 @*/ 5984 PetscErrorCode MatZeroEntries(Mat mat) 5985 { 5986 PetscErrorCode ierr; 5987 5988 PetscFunctionBegin; 5989 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5990 PetscValidType(mat,1); 5991 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5992 if (mat->insertmode != NOT_SET_VALUES) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for matrices where you have set values but not yet assembled"); 5993 if (!mat->ops->zeroentries) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 5994 MatCheckPreallocated(mat,1); 5995 5996 ierr = PetscLogEventBegin(MAT_ZeroEntries,mat,0,0,0);CHKERRQ(ierr); 5997 ierr = (*mat->ops->zeroentries)(mat);CHKERRQ(ierr); 5998 ierr = PetscLogEventEnd(MAT_ZeroEntries,mat,0,0,0);CHKERRQ(ierr); 5999 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 6000 PetscFunctionReturn(0); 6001 } 6002 6003 /*@ 6004 MatZeroRowsColumns - Zeros all entries (except possibly the main diagonal) 6005 of a set of rows and columns of a matrix. 6006 6007 Collective on Mat 6008 6009 Input Parameters: 6010 + mat - the matrix 6011 . numRows - the number of rows to remove 6012 . rows - the global row indices 6013 . diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry) 6014 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 6015 - b - optional vector of right hand side, that will be adjusted by provided solution 6016 6017 Notes: 6018 This does not change the nonzero structure of the matrix, it merely zeros those entries in the matrix. 6019 6020 The user can set a value in the diagonal entry (or for the AIJ and 6021 row formats can optionally remove the main diagonal entry from the 6022 nonzero structure as well, by passing 0.0 as the final argument). 6023 6024 For the parallel case, all processes that share the matrix (i.e., 6025 those in the communicator used for matrix creation) MUST call this 6026 routine, regardless of whether any rows being zeroed are owned by 6027 them. 6028 6029 Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to 6030 list only rows local to itself). 6031 6032 The option MAT_NO_OFF_PROC_ZERO_ROWS does not apply to this routine. 6033 6034 Level: intermediate 6035 6036 .seealso: MatZeroRowsIS(), MatZeroRows(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 6037 MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil() 6038 @*/ 6039 PetscErrorCode MatZeroRowsColumns(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 6040 { 6041 PetscErrorCode ierr; 6042 6043 PetscFunctionBegin; 6044 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6045 PetscValidType(mat,1); 6046 if (numRows) PetscValidIntPointer(rows,3); 6047 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 6048 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6049 if (!mat->ops->zerorowscolumns) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 6050 MatCheckPreallocated(mat,1); 6051 6052 ierr = (*mat->ops->zerorowscolumns)(mat,numRows,rows,diag,x,b);CHKERRQ(ierr); 6053 ierr = MatViewFromOptions(mat,NULL,"-mat_view");CHKERRQ(ierr); 6054 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 6055 PetscFunctionReturn(0); 6056 } 6057 6058 /*@ 6059 MatZeroRowsColumnsIS - Zeros all entries (except possibly the main diagonal) 6060 of a set of rows and columns of a matrix. 6061 6062 Collective on Mat 6063 6064 Input Parameters: 6065 + mat - the matrix 6066 . is - the rows to zero 6067 . diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry) 6068 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 6069 - b - optional vector of right hand side, that will be adjusted by provided solution 6070 6071 Notes: 6072 This does not change the nonzero structure of the matrix, it merely zeros those entries in the matrix. 6073 6074 The user can set a value in the diagonal entry (or for the AIJ and 6075 row formats can optionally remove the main diagonal entry from the 6076 nonzero structure as well, by passing 0.0 as the final argument). 6077 6078 For the parallel case, all processes that share the matrix (i.e., 6079 those in the communicator used for matrix creation) MUST call this 6080 routine, regardless of whether any rows being zeroed are owned by 6081 them. 6082 6083 Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to 6084 list only rows local to itself). 6085 6086 The option MAT_NO_OFF_PROC_ZERO_ROWS does not apply to this routine. 6087 6088 Level: intermediate 6089 6090 .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 6091 MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRows(), MatZeroRowsColumnsStencil() 6092 @*/ 6093 PetscErrorCode MatZeroRowsColumnsIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b) 6094 { 6095 PetscErrorCode ierr; 6096 PetscInt numRows; 6097 const PetscInt *rows; 6098 6099 PetscFunctionBegin; 6100 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6101 PetscValidHeaderSpecific(is,IS_CLASSID,2); 6102 PetscValidType(mat,1); 6103 PetscValidType(is,2); 6104 ierr = ISGetLocalSize(is,&numRows);CHKERRQ(ierr); 6105 ierr = ISGetIndices(is,&rows);CHKERRQ(ierr); 6106 ierr = MatZeroRowsColumns(mat,numRows,rows,diag,x,b);CHKERRQ(ierr); 6107 ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr); 6108 PetscFunctionReturn(0); 6109 } 6110 6111 /*@ 6112 MatZeroRows - Zeros all entries (except possibly the main diagonal) 6113 of a set of rows of a matrix. 6114 6115 Collective on Mat 6116 6117 Input Parameters: 6118 + mat - the matrix 6119 . numRows - the number of rows to remove 6120 . rows - the global row indices 6121 . diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry) 6122 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 6123 - b - optional vector of right hand side, that will be adjusted by provided solution 6124 6125 Notes: 6126 For the AIJ and BAIJ matrix formats this removes the old nonzero structure, 6127 but does not release memory. For the dense and block diagonal 6128 formats this does not alter the nonzero structure. 6129 6130 If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure 6131 of the matrix is not changed (even for AIJ and BAIJ matrices) the values are 6132 merely zeroed. 6133 6134 The user can set a value in the diagonal entry (or for the AIJ and 6135 row formats can optionally remove the main diagonal entry from the 6136 nonzero structure as well, by passing 0.0 as the final argument). 6137 6138 For the parallel case, all processes that share the matrix (i.e., 6139 those in the communicator used for matrix creation) MUST call this 6140 routine, regardless of whether any rows being zeroed are owned by 6141 them. 6142 6143 Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to 6144 list only rows local to itself). 6145 6146 You can call MatSetOption(mat,MAT_NO_OFF_PROC_ZERO_ROWS,PETSC_TRUE) if each process indicates only rows it 6147 owns that are to be zeroed. This saves a global synchronization in the implementation. 6148 6149 Level: intermediate 6150 6151 .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 6152 MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil() 6153 @*/ 6154 PetscErrorCode MatZeroRows(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 6155 { 6156 PetscErrorCode ierr; 6157 6158 PetscFunctionBegin; 6159 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6160 PetscValidType(mat,1); 6161 if (numRows) PetscValidIntPointer(rows,3); 6162 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 6163 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6164 if (!mat->ops->zerorows) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 6165 MatCheckPreallocated(mat,1); 6166 6167 ierr = (*mat->ops->zerorows)(mat,numRows,rows,diag,x,b);CHKERRQ(ierr); 6168 ierr = MatViewFromOptions(mat,NULL,"-mat_view");CHKERRQ(ierr); 6169 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 6170 PetscFunctionReturn(0); 6171 } 6172 6173 /*@ 6174 MatZeroRowsIS - Zeros all entries (except possibly the main diagonal) 6175 of a set of rows of a matrix. 6176 6177 Collective on Mat 6178 6179 Input Parameters: 6180 + mat - the matrix 6181 . is - index set of rows to remove 6182 . diag - value put in all diagonals of eliminated rows 6183 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 6184 - b - optional vector of right hand side, that will be adjusted by provided solution 6185 6186 Notes: 6187 For the AIJ and BAIJ matrix formats this removes the old nonzero structure, 6188 but does not release memory. For the dense and block diagonal 6189 formats this does not alter the nonzero structure. 6190 6191 If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure 6192 of the matrix is not changed (even for AIJ and BAIJ matrices) the values are 6193 merely zeroed. 6194 6195 The user can set a value in the diagonal entry (or for the AIJ and 6196 row formats can optionally remove the main diagonal entry from the 6197 nonzero structure as well, by passing 0.0 as the final argument). 6198 6199 For the parallel case, all processes that share the matrix (i.e., 6200 those in the communicator used for matrix creation) MUST call this 6201 routine, regardless of whether any rows being zeroed are owned by 6202 them. 6203 6204 Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to 6205 list only rows local to itself). 6206 6207 You can call MatSetOption(mat,MAT_NO_OFF_PROC_ZERO_ROWS,PETSC_TRUE) if each process indicates only rows it 6208 owns that are to be zeroed. This saves a global synchronization in the implementation. 6209 6210 Level: intermediate 6211 6212 .seealso: MatZeroRows(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 6213 MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil() 6214 @*/ 6215 PetscErrorCode MatZeroRowsIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b) 6216 { 6217 PetscInt numRows; 6218 const PetscInt *rows; 6219 PetscErrorCode ierr; 6220 6221 PetscFunctionBegin; 6222 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6223 PetscValidType(mat,1); 6224 PetscValidHeaderSpecific(is,IS_CLASSID,2); 6225 ierr = ISGetLocalSize(is,&numRows);CHKERRQ(ierr); 6226 ierr = ISGetIndices(is,&rows);CHKERRQ(ierr); 6227 ierr = MatZeroRows(mat,numRows,rows,diag,x,b);CHKERRQ(ierr); 6228 ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr); 6229 PetscFunctionReturn(0); 6230 } 6231 6232 /*@ 6233 MatZeroRowsStencil - Zeros all entries (except possibly the main diagonal) 6234 of a set of rows of a matrix. These rows must be local to the process. 6235 6236 Collective on Mat 6237 6238 Input Parameters: 6239 + mat - the matrix 6240 . numRows - the number of rows to remove 6241 . rows - the grid coordinates (and component number when dof > 1) for matrix rows 6242 . diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry) 6243 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 6244 - b - optional vector of right hand side, that will be adjusted by provided solution 6245 6246 Notes: 6247 For the AIJ and BAIJ matrix formats this removes the old nonzero structure, 6248 but does not release memory. For the dense and block diagonal 6249 formats this does not alter the nonzero structure. 6250 6251 If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure 6252 of the matrix is not changed (even for AIJ and BAIJ matrices) the values are 6253 merely zeroed. 6254 6255 The user can set a value in the diagonal entry (or for the AIJ and 6256 row formats can optionally remove the main diagonal entry from the 6257 nonzero structure as well, by passing 0.0 as the final argument). 6258 6259 For the parallel case, all processes that share the matrix (i.e., 6260 those in the communicator used for matrix creation) MUST call this 6261 routine, regardless of whether any rows being zeroed are owned by 6262 them. 6263 6264 Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to 6265 list only rows local to itself). 6266 6267 The grid coordinates are across the entire grid, not just the local portion 6268 6269 In Fortran idxm and idxn should be declared as 6270 $ MatStencil idxm(4,m) 6271 and the values inserted using 6272 $ idxm(MatStencil_i,1) = i 6273 $ idxm(MatStencil_j,1) = j 6274 $ idxm(MatStencil_k,1) = k 6275 $ idxm(MatStencil_c,1) = c 6276 etc 6277 6278 For periodic boundary conditions use negative indices for values to the left (below 0; that are to be 6279 obtained by wrapping values from right edge). For values to the right of the last entry using that index plus one 6280 etc to obtain values that obtained by wrapping the values from the left edge. This does not work for anything but the 6281 DM_BOUNDARY_PERIODIC boundary type. 6282 6283 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 6284 a single value per point) you can skip filling those indices. 6285 6286 Level: intermediate 6287 6288 .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsl(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 6289 MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil() 6290 @*/ 6291 PetscErrorCode MatZeroRowsStencil(Mat mat,PetscInt numRows,const MatStencil rows[],PetscScalar diag,Vec x,Vec b) 6292 { 6293 PetscInt dim = mat->stencil.dim; 6294 PetscInt sdim = dim - (1 - (PetscInt) mat->stencil.noc); 6295 PetscInt *dims = mat->stencil.dims+1; 6296 PetscInt *starts = mat->stencil.starts; 6297 PetscInt *dxm = (PetscInt*) rows; 6298 PetscInt *jdxm, i, j, tmp, numNewRows = 0; 6299 PetscErrorCode ierr; 6300 6301 PetscFunctionBegin; 6302 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6303 PetscValidType(mat,1); 6304 if (numRows) PetscValidPointer(rows,3); 6305 6306 ierr = PetscMalloc1(numRows, &jdxm);CHKERRQ(ierr); 6307 for (i = 0; i < numRows; ++i) { 6308 /* Skip unused dimensions (they are ordered k, j, i, c) */ 6309 for (j = 0; j < 3-sdim; ++j) dxm++; 6310 /* Local index in X dir */ 6311 tmp = *dxm++ - starts[0]; 6312 /* Loop over remaining dimensions */ 6313 for (j = 0; j < dim-1; ++j) { 6314 /* If nonlocal, set index to be negative */ 6315 if ((*dxm++ - starts[j+1]) < 0 || tmp < 0) tmp = PETSC_MIN_INT; 6316 /* Update local index */ 6317 else tmp = tmp*dims[j] + *(dxm-1) - starts[j+1]; 6318 } 6319 /* Skip component slot if necessary */ 6320 if (mat->stencil.noc) dxm++; 6321 /* Local row number */ 6322 if (tmp >= 0) { 6323 jdxm[numNewRows++] = tmp; 6324 } 6325 } 6326 ierr = MatZeroRowsLocal(mat,numNewRows,jdxm,diag,x,b);CHKERRQ(ierr); 6327 ierr = PetscFree(jdxm);CHKERRQ(ierr); 6328 PetscFunctionReturn(0); 6329 } 6330 6331 /*@ 6332 MatZeroRowsColumnsStencil - Zeros all row and column entries (except possibly the main diagonal) 6333 of a set of rows and columns of a matrix. 6334 6335 Collective on Mat 6336 6337 Input Parameters: 6338 + mat - the matrix 6339 . numRows - the number of rows/columns to remove 6340 . rows - the grid coordinates (and component number when dof > 1) for matrix rows 6341 . diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry) 6342 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 6343 - b - optional vector of right hand side, that will be adjusted by provided solution 6344 6345 Notes: 6346 For the AIJ and BAIJ matrix formats this removes the old nonzero structure, 6347 but does not release memory. For the dense and block diagonal 6348 formats this does not alter the nonzero structure. 6349 6350 If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure 6351 of the matrix is not changed (even for AIJ and BAIJ matrices) the values are 6352 merely zeroed. 6353 6354 The user can set a value in the diagonal entry (or for the AIJ and 6355 row formats can optionally remove the main diagonal entry from the 6356 nonzero structure as well, by passing 0.0 as the final argument). 6357 6358 For the parallel case, all processes that share the matrix (i.e., 6359 those in the communicator used for matrix creation) MUST call this 6360 routine, regardless of whether any rows being zeroed are owned by 6361 them. 6362 6363 Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to 6364 list only rows local to itself, but the row/column numbers are given in local numbering). 6365 6366 The grid coordinates are across the entire grid, not just the local portion 6367 6368 In Fortran idxm and idxn should be declared as 6369 $ MatStencil idxm(4,m) 6370 and the values inserted using 6371 $ idxm(MatStencil_i,1) = i 6372 $ idxm(MatStencil_j,1) = j 6373 $ idxm(MatStencil_k,1) = k 6374 $ idxm(MatStencil_c,1) = c 6375 etc 6376 6377 For periodic boundary conditions use negative indices for values to the left (below 0; that are to be 6378 obtained by wrapping values from right edge). For values to the right of the last entry using that index plus one 6379 etc to obtain values that obtained by wrapping the values from the left edge. This does not work for anything but the 6380 DM_BOUNDARY_PERIODIC boundary type. 6381 6382 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 6383 a single value per point) you can skip filling those indices. 6384 6385 Level: intermediate 6386 6387 .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 6388 MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRows() 6389 @*/ 6390 PetscErrorCode MatZeroRowsColumnsStencil(Mat mat,PetscInt numRows,const MatStencil rows[],PetscScalar diag,Vec x,Vec b) 6391 { 6392 PetscInt dim = mat->stencil.dim; 6393 PetscInt sdim = dim - (1 - (PetscInt) mat->stencil.noc); 6394 PetscInt *dims = mat->stencil.dims+1; 6395 PetscInt *starts = mat->stencil.starts; 6396 PetscInt *dxm = (PetscInt*) rows; 6397 PetscInt *jdxm, i, j, tmp, numNewRows = 0; 6398 PetscErrorCode ierr; 6399 6400 PetscFunctionBegin; 6401 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6402 PetscValidType(mat,1); 6403 if (numRows) PetscValidPointer(rows,3); 6404 6405 ierr = PetscMalloc1(numRows, &jdxm);CHKERRQ(ierr); 6406 for (i = 0; i < numRows; ++i) { 6407 /* Skip unused dimensions (they are ordered k, j, i, c) */ 6408 for (j = 0; j < 3-sdim; ++j) dxm++; 6409 /* Local index in X dir */ 6410 tmp = *dxm++ - starts[0]; 6411 /* Loop over remaining dimensions */ 6412 for (j = 0; j < dim-1; ++j) { 6413 /* If nonlocal, set index to be negative */ 6414 if ((*dxm++ - starts[j+1]) < 0 || tmp < 0) tmp = PETSC_MIN_INT; 6415 /* Update local index */ 6416 else tmp = tmp*dims[j] + *(dxm-1) - starts[j+1]; 6417 } 6418 /* Skip component slot if necessary */ 6419 if (mat->stencil.noc) dxm++; 6420 /* Local row number */ 6421 if (tmp >= 0) { 6422 jdxm[numNewRows++] = tmp; 6423 } 6424 } 6425 ierr = MatZeroRowsColumnsLocal(mat,numNewRows,jdxm,diag,x,b);CHKERRQ(ierr); 6426 ierr = PetscFree(jdxm);CHKERRQ(ierr); 6427 PetscFunctionReturn(0); 6428 } 6429 6430 /*@C 6431 MatZeroRowsLocal - Zeros all entries (except possibly the main diagonal) 6432 of a set of rows of a matrix; using local numbering of rows. 6433 6434 Collective on Mat 6435 6436 Input Parameters: 6437 + mat - the matrix 6438 . numRows - the number of rows to remove 6439 . rows - the global row indices 6440 . diag - value put in all diagonals of eliminated rows 6441 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 6442 - b - optional vector of right hand side, that will be adjusted by provided solution 6443 6444 Notes: 6445 Before calling MatZeroRowsLocal(), the user must first set the 6446 local-to-global mapping by calling MatSetLocalToGlobalMapping(). 6447 6448 For the AIJ matrix formats this removes the old nonzero structure, 6449 but does not release memory. For the dense and block diagonal 6450 formats this does not alter the nonzero structure. 6451 6452 If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure 6453 of the matrix is not changed (even for AIJ and BAIJ matrices) the values are 6454 merely zeroed. 6455 6456 The user can set a value in the diagonal entry (or for the AIJ and 6457 row formats can optionally remove the main diagonal entry from the 6458 nonzero structure as well, by passing 0.0 as the final argument). 6459 6460 You can call MatSetOption(mat,MAT_NO_OFF_PROC_ZERO_ROWS,PETSC_TRUE) if each process indicates only rows it 6461 owns that are to be zeroed. This saves a global synchronization in the implementation. 6462 6463 Level: intermediate 6464 6465 .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRows(), MatSetOption(), 6466 MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil() 6467 @*/ 6468 PetscErrorCode MatZeroRowsLocal(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 6469 { 6470 PetscErrorCode ierr; 6471 6472 PetscFunctionBegin; 6473 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6474 PetscValidType(mat,1); 6475 if (numRows) PetscValidIntPointer(rows,3); 6476 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 6477 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6478 MatCheckPreallocated(mat,1); 6479 6480 if (mat->ops->zerorowslocal) { 6481 ierr = (*mat->ops->zerorowslocal)(mat,numRows,rows,diag,x,b);CHKERRQ(ierr); 6482 } else { 6483 IS is, newis; 6484 const PetscInt *newRows; 6485 6486 if (!mat->rmap->mapping) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Need to provide local to global mapping to matrix first"); 6487 ierr = ISCreateGeneral(PETSC_COMM_SELF,numRows,rows,PETSC_COPY_VALUES,&is);CHKERRQ(ierr); 6488 ierr = ISLocalToGlobalMappingApplyIS(mat->rmap->mapping,is,&newis);CHKERRQ(ierr); 6489 ierr = ISGetIndices(newis,&newRows);CHKERRQ(ierr); 6490 ierr = (*mat->ops->zerorows)(mat,numRows,newRows,diag,x,b);CHKERRQ(ierr); 6491 ierr = ISRestoreIndices(newis,&newRows);CHKERRQ(ierr); 6492 ierr = ISDestroy(&newis);CHKERRQ(ierr); 6493 ierr = ISDestroy(&is);CHKERRQ(ierr); 6494 } 6495 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 6496 PetscFunctionReturn(0); 6497 } 6498 6499 /*@ 6500 MatZeroRowsLocalIS - Zeros all entries (except possibly the main diagonal) 6501 of a set of rows of a matrix; using local numbering of rows. 6502 6503 Collective on Mat 6504 6505 Input Parameters: 6506 + mat - the matrix 6507 . is - index set of rows to remove 6508 . diag - value put in all diagonals of eliminated rows 6509 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 6510 - b - optional vector of right hand side, that will be adjusted by provided solution 6511 6512 Notes: 6513 Before calling MatZeroRowsLocalIS(), the user must first set the 6514 local-to-global mapping by calling MatSetLocalToGlobalMapping(). 6515 6516 For the AIJ matrix formats this removes the old nonzero structure, 6517 but does not release memory. For the dense and block diagonal 6518 formats this does not alter the nonzero structure. 6519 6520 If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure 6521 of the matrix is not changed (even for AIJ and BAIJ matrices) the values are 6522 merely zeroed. 6523 6524 The user can set a value in the diagonal entry (or for the AIJ and 6525 row formats can optionally remove the main diagonal entry from the 6526 nonzero structure as well, by passing 0.0 as the final argument). 6527 6528 You can call MatSetOption(mat,MAT_NO_OFF_PROC_ZERO_ROWS,PETSC_TRUE) if each process indicates only rows it 6529 owns that are to be zeroed. This saves a global synchronization in the implementation. 6530 6531 Level: intermediate 6532 6533 .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRows(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 6534 MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil() 6535 @*/ 6536 PetscErrorCode MatZeroRowsLocalIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b) 6537 { 6538 PetscErrorCode ierr; 6539 PetscInt numRows; 6540 const PetscInt *rows; 6541 6542 PetscFunctionBegin; 6543 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6544 PetscValidType(mat,1); 6545 PetscValidHeaderSpecific(is,IS_CLASSID,2); 6546 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 6547 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6548 MatCheckPreallocated(mat,1); 6549 6550 ierr = ISGetLocalSize(is,&numRows);CHKERRQ(ierr); 6551 ierr = ISGetIndices(is,&rows);CHKERRQ(ierr); 6552 ierr = MatZeroRowsLocal(mat,numRows,rows,diag,x,b);CHKERRQ(ierr); 6553 ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr); 6554 PetscFunctionReturn(0); 6555 } 6556 6557 /*@ 6558 MatZeroRowsColumnsLocal - Zeros all entries (except possibly the main diagonal) 6559 of a set of rows and columns of a matrix; using local numbering of rows. 6560 6561 Collective on Mat 6562 6563 Input Parameters: 6564 + mat - the matrix 6565 . numRows - the number of rows to remove 6566 . rows - the global row indices 6567 . diag - value put in all diagonals of eliminated rows 6568 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 6569 - b - optional vector of right hand side, that will be adjusted by provided solution 6570 6571 Notes: 6572 Before calling MatZeroRowsColumnsLocal(), the user must first set the 6573 local-to-global mapping by calling MatSetLocalToGlobalMapping(). 6574 6575 The user can set a value in the diagonal entry (or for the AIJ and 6576 row formats can optionally remove the main diagonal entry from the 6577 nonzero structure as well, by passing 0.0 as the final argument). 6578 6579 Level: intermediate 6580 6581 .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 6582 MatZeroRows(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil() 6583 @*/ 6584 PetscErrorCode MatZeroRowsColumnsLocal(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 6585 { 6586 PetscErrorCode ierr; 6587 IS is, newis; 6588 const PetscInt *newRows; 6589 6590 PetscFunctionBegin; 6591 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6592 PetscValidType(mat,1); 6593 if (numRows) PetscValidIntPointer(rows,3); 6594 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 6595 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6596 MatCheckPreallocated(mat,1); 6597 6598 if (!mat->cmap->mapping) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Need to provide local to global mapping to matrix first"); 6599 ierr = ISCreateGeneral(PETSC_COMM_SELF,numRows,rows,PETSC_COPY_VALUES,&is);CHKERRQ(ierr); 6600 ierr = ISLocalToGlobalMappingApplyIS(mat->cmap->mapping,is,&newis);CHKERRQ(ierr); 6601 ierr = ISGetIndices(newis,&newRows);CHKERRQ(ierr); 6602 ierr = (*mat->ops->zerorowscolumns)(mat,numRows,newRows,diag,x,b);CHKERRQ(ierr); 6603 ierr = ISRestoreIndices(newis,&newRows);CHKERRQ(ierr); 6604 ierr = ISDestroy(&newis);CHKERRQ(ierr); 6605 ierr = ISDestroy(&is);CHKERRQ(ierr); 6606 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 6607 PetscFunctionReturn(0); 6608 } 6609 6610 /*@ 6611 MatZeroRowsColumnsLocalIS - Zeros all entries (except possibly the main diagonal) 6612 of a set of rows and columns of a matrix; using local numbering of rows. 6613 6614 Collective on Mat 6615 6616 Input Parameters: 6617 + mat - the matrix 6618 . is - index set of rows to remove 6619 . diag - value put in all diagonals of eliminated rows 6620 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 6621 - b - optional vector of right hand side, that will be adjusted by provided solution 6622 6623 Notes: 6624 Before calling MatZeroRowsColumnsLocalIS(), the user must first set the 6625 local-to-global mapping by calling MatSetLocalToGlobalMapping(). 6626 6627 The user can set a value in the diagonal entry (or for the AIJ and 6628 row formats can optionally remove the main diagonal entry from the 6629 nonzero structure as well, by passing 0.0 as the final argument). 6630 6631 Level: intermediate 6632 6633 .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 6634 MatZeroRowsColumnsLocal(), MatZeroRows(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil() 6635 @*/ 6636 PetscErrorCode MatZeroRowsColumnsLocalIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b) 6637 { 6638 PetscErrorCode ierr; 6639 PetscInt numRows; 6640 const PetscInt *rows; 6641 6642 PetscFunctionBegin; 6643 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6644 PetscValidType(mat,1); 6645 PetscValidHeaderSpecific(is,IS_CLASSID,2); 6646 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 6647 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6648 MatCheckPreallocated(mat,1); 6649 6650 ierr = ISGetLocalSize(is,&numRows);CHKERRQ(ierr); 6651 ierr = ISGetIndices(is,&rows);CHKERRQ(ierr); 6652 ierr = MatZeroRowsColumnsLocal(mat,numRows,rows,diag,x,b);CHKERRQ(ierr); 6653 ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr); 6654 PetscFunctionReturn(0); 6655 } 6656 6657 /*@C 6658 MatGetSize - Returns the numbers of rows and columns in a matrix. 6659 6660 Not Collective 6661 6662 Input Parameter: 6663 . mat - the matrix 6664 6665 Output Parameters: 6666 + m - the number of global rows 6667 - n - the number of global columns 6668 6669 Note: both output parameters can be NULL on input. 6670 6671 Level: beginner 6672 6673 .seealso: MatGetLocalSize() 6674 @*/ 6675 PetscErrorCode MatGetSize(Mat mat,PetscInt *m,PetscInt *n) 6676 { 6677 PetscFunctionBegin; 6678 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6679 if (m) *m = mat->rmap->N; 6680 if (n) *n = mat->cmap->N; 6681 PetscFunctionReturn(0); 6682 } 6683 6684 /*@C 6685 MatGetLocalSize - Returns the number of local rows and local columns 6686 of a matrix, that is the local size of the left and right vectors as returned by MatCreateVecs(). 6687 6688 Not Collective 6689 6690 Input Parameters: 6691 . mat - the matrix 6692 6693 Output Parameters: 6694 + m - the number of local rows 6695 - n - the number of local columns 6696 6697 Note: both output parameters can be NULL on input. 6698 6699 Level: beginner 6700 6701 .seealso: MatGetSize() 6702 @*/ 6703 PetscErrorCode MatGetLocalSize(Mat mat,PetscInt *m,PetscInt *n) 6704 { 6705 PetscFunctionBegin; 6706 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6707 if (m) PetscValidIntPointer(m,2); 6708 if (n) PetscValidIntPointer(n,3); 6709 if (m) *m = mat->rmap->n; 6710 if (n) *n = mat->cmap->n; 6711 PetscFunctionReturn(0); 6712 } 6713 6714 /*@C 6715 MatGetOwnershipRangeColumn - Returns the range of matrix columns associated with rows of a vector one multiplies by that owned by 6716 this processor. (The columns of the "diagonal block") 6717 6718 Not Collective, unless matrix has not been allocated, then collective on Mat 6719 6720 Input Parameters: 6721 . mat - the matrix 6722 6723 Output Parameters: 6724 + m - the global index of the first local column 6725 - n - one more than the global index of the last local column 6726 6727 Notes: 6728 both output parameters can be NULL on input. 6729 6730 Level: developer 6731 6732 .seealso: MatGetOwnershipRange(), MatGetOwnershipRanges(), MatGetOwnershipRangesColumn() 6733 6734 @*/ 6735 PetscErrorCode MatGetOwnershipRangeColumn(Mat mat,PetscInt *m,PetscInt *n) 6736 { 6737 PetscFunctionBegin; 6738 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6739 PetscValidType(mat,1); 6740 if (m) PetscValidIntPointer(m,2); 6741 if (n) PetscValidIntPointer(n,3); 6742 MatCheckPreallocated(mat,1); 6743 if (m) *m = mat->cmap->rstart; 6744 if (n) *n = mat->cmap->rend; 6745 PetscFunctionReturn(0); 6746 } 6747 6748 /*@C 6749 MatGetOwnershipRange - Returns the range of matrix rows owned by 6750 this processor, assuming that the matrix is laid out with the first 6751 n1 rows on the first processor, the next n2 rows on the second, etc. 6752 For certain parallel layouts this range may not be well defined. 6753 6754 Not Collective 6755 6756 Input Parameters: 6757 . mat - the matrix 6758 6759 Output Parameters: 6760 + m - the global index of the first local row 6761 - n - one more than the global index of the last local row 6762 6763 Note: Both output parameters can be NULL on input. 6764 $ This function requires that the matrix be preallocated. If you have not preallocated, consider using 6765 $ PetscSplitOwnership(MPI_Comm comm, PetscInt *n, PetscInt *N) 6766 $ and then MPI_Scan() to calculate prefix sums of the local sizes. 6767 6768 Level: beginner 6769 6770 .seealso: MatGetOwnershipRanges(), MatGetOwnershipRangeColumn(), MatGetOwnershipRangesColumn(), PetscSplitOwnership(), PetscSplitOwnershipBlock() 6771 6772 @*/ 6773 PetscErrorCode MatGetOwnershipRange(Mat mat,PetscInt *m,PetscInt *n) 6774 { 6775 PetscFunctionBegin; 6776 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6777 PetscValidType(mat,1); 6778 if (m) PetscValidIntPointer(m,2); 6779 if (n) PetscValidIntPointer(n,3); 6780 MatCheckPreallocated(mat,1); 6781 if (m) *m = mat->rmap->rstart; 6782 if (n) *n = mat->rmap->rend; 6783 PetscFunctionReturn(0); 6784 } 6785 6786 /*@C 6787 MatGetOwnershipRanges - Returns the range of matrix rows owned by 6788 each process 6789 6790 Not Collective, unless matrix has not been allocated, then collective on Mat 6791 6792 Input Parameters: 6793 . mat - the matrix 6794 6795 Output Parameters: 6796 . ranges - start of each processors portion plus one more than the total length at the end 6797 6798 Level: beginner 6799 6800 .seealso: MatGetOwnershipRange(), MatGetOwnershipRangeColumn(), MatGetOwnershipRangesColumn() 6801 6802 @*/ 6803 PetscErrorCode MatGetOwnershipRanges(Mat mat,const PetscInt **ranges) 6804 { 6805 PetscErrorCode ierr; 6806 6807 PetscFunctionBegin; 6808 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6809 PetscValidType(mat,1); 6810 MatCheckPreallocated(mat,1); 6811 ierr = PetscLayoutGetRanges(mat->rmap,ranges);CHKERRQ(ierr); 6812 PetscFunctionReturn(0); 6813 } 6814 6815 /*@C 6816 MatGetOwnershipRangesColumn - Returns the range of matrix columns associated with rows of a vector one multiplies by that owned by 6817 this processor. (The columns of the "diagonal blocks" for each process) 6818 6819 Not Collective, unless matrix has not been allocated, then collective on Mat 6820 6821 Input Parameters: 6822 . mat - the matrix 6823 6824 Output Parameters: 6825 . ranges - start of each processors portion plus one more then the total length at the end 6826 6827 Level: beginner 6828 6829 .seealso: MatGetOwnershipRange(), MatGetOwnershipRangeColumn(), MatGetOwnershipRanges() 6830 6831 @*/ 6832 PetscErrorCode MatGetOwnershipRangesColumn(Mat mat,const PetscInt **ranges) 6833 { 6834 PetscErrorCode ierr; 6835 6836 PetscFunctionBegin; 6837 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6838 PetscValidType(mat,1); 6839 MatCheckPreallocated(mat,1); 6840 ierr = PetscLayoutGetRanges(mat->cmap,ranges);CHKERRQ(ierr); 6841 PetscFunctionReturn(0); 6842 } 6843 6844 /*@C 6845 MatGetOwnershipIS - Get row and column ownership as index sets 6846 6847 Not Collective 6848 6849 Input Arguments: 6850 . A - matrix of type Elemental or ScaLAPACK 6851 6852 Output Arguments: 6853 + rows - rows in which this process owns elements 6854 - cols - columns in which this process owns elements 6855 6856 Level: intermediate 6857 6858 .seealso: MatGetOwnershipRange(), MatGetOwnershipRangeColumn(), MatSetValues(), MATELEMENTAL 6859 @*/ 6860 PetscErrorCode MatGetOwnershipIS(Mat A,IS *rows,IS *cols) 6861 { 6862 PetscErrorCode ierr,(*f)(Mat,IS*,IS*); 6863 6864 PetscFunctionBegin; 6865 MatCheckPreallocated(A,1); 6866 ierr = PetscObjectQueryFunction((PetscObject)A,"MatGetOwnershipIS_C",&f);CHKERRQ(ierr); 6867 if (f) { 6868 ierr = (*f)(A,rows,cols);CHKERRQ(ierr); 6869 } else { /* Create a standard row-based partition, each process is responsible for ALL columns in their row block */ 6870 if (rows) {ierr = ISCreateStride(PETSC_COMM_SELF,A->rmap->n,A->rmap->rstart,1,rows);CHKERRQ(ierr);} 6871 if (cols) {ierr = ISCreateStride(PETSC_COMM_SELF,A->cmap->N,0,1,cols);CHKERRQ(ierr);} 6872 } 6873 PetscFunctionReturn(0); 6874 } 6875 6876 /*@C 6877 MatILUFactorSymbolic - Performs symbolic ILU factorization of a matrix. 6878 Uses levels of fill only, not drop tolerance. Use MatLUFactorNumeric() 6879 to complete the factorization. 6880 6881 Collective on Mat 6882 6883 Input Parameters: 6884 + mat - the matrix 6885 . row - row permutation 6886 . column - column permutation 6887 - info - structure containing 6888 $ levels - number of levels of fill. 6889 $ expected fill - as ratio of original fill. 6890 $ 1 or 0 - indicating force fill on diagonal (improves robustness for matrices 6891 missing diagonal entries) 6892 6893 Output Parameters: 6894 . fact - new matrix that has been symbolically factored 6895 6896 Notes: 6897 See Users-Manual: ch_mat for additional information about choosing the fill factor for better efficiency. 6898 6899 Most users should employ the simplified KSP interface for linear solvers 6900 instead of working directly with matrix algebra routines such as this. 6901 See, e.g., KSPCreate(). 6902 6903 Level: developer 6904 6905 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor() 6906 MatGetOrdering(), MatFactorInfo 6907 6908 Note: this uses the definition of level of fill as in Y. Saad, 2003 6909 6910 Developer Note: fortran interface is not autogenerated as the f90 6911 interface defintion cannot be generated correctly [due to MatFactorInfo] 6912 6913 References: 6914 Y. Saad, Iterative methods for sparse linear systems Philadelphia: Society for Industrial and Applied Mathematics, 2003 6915 @*/ 6916 PetscErrorCode MatILUFactorSymbolic(Mat fact,Mat mat,IS row,IS col,const MatFactorInfo *info) 6917 { 6918 PetscErrorCode ierr; 6919 6920 PetscFunctionBegin; 6921 PetscValidHeaderSpecific(mat,MAT_CLASSID,2); 6922 PetscValidType(mat,2); 6923 if (row) PetscValidHeaderSpecific(row,IS_CLASSID,3); 6924 if (col) PetscValidHeaderSpecific(col,IS_CLASSID,4); 6925 PetscValidPointer(info,5); 6926 PetscValidPointer(fact,1); 6927 if (info->levels < 0) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Levels of fill negative %D",(PetscInt)info->levels); 6928 if (info->fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %g",(double)info->fill); 6929 if (!fact->ops->ilufactorsymbolic) { 6930 MatSolverType stype; 6931 ierr = MatFactorGetSolverType(fact,&stype);CHKERRQ(ierr); 6932 SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s symbolic ILU using solver type %s",((PetscObject)mat)->type_name,stype); 6933 } 6934 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 6935 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6936 MatCheckPreallocated(mat,2); 6937 6938 if (!fact->trivialsymbolic) {ierr = PetscLogEventBegin(MAT_ILUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr);} 6939 ierr = (fact->ops->ilufactorsymbolic)(fact,mat,row,col,info);CHKERRQ(ierr); 6940 if (!fact->trivialsymbolic) {ierr = PetscLogEventEnd(MAT_ILUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr);} 6941 PetscFunctionReturn(0); 6942 } 6943 6944 /*@C 6945 MatICCFactorSymbolic - Performs symbolic incomplete 6946 Cholesky factorization for a symmetric matrix. Use 6947 MatCholeskyFactorNumeric() to complete the factorization. 6948 6949 Collective on Mat 6950 6951 Input Parameters: 6952 + mat - the matrix 6953 . perm - row and column permutation 6954 - info - structure containing 6955 $ levels - number of levels of fill. 6956 $ expected fill - as ratio of original fill. 6957 6958 Output Parameter: 6959 . fact - the factored matrix 6960 6961 Notes: 6962 Most users should employ the KSP interface for linear solvers 6963 instead of working directly with matrix algebra routines such as this. 6964 See, e.g., KSPCreate(). 6965 6966 Level: developer 6967 6968 .seealso: MatCholeskyFactorNumeric(), MatCholeskyFactor(), MatFactorInfo 6969 6970 Note: this uses the definition of level of fill as in Y. Saad, 2003 6971 6972 Developer Note: fortran interface is not autogenerated as the f90 6973 interface defintion cannot be generated correctly [due to MatFactorInfo] 6974 6975 References: 6976 Y. Saad, Iterative methods for sparse linear systems Philadelphia: Society for Industrial and Applied Mathematics, 2003 6977 @*/ 6978 PetscErrorCode MatICCFactorSymbolic(Mat fact,Mat mat,IS perm,const MatFactorInfo *info) 6979 { 6980 PetscErrorCode ierr; 6981 6982 PetscFunctionBegin; 6983 PetscValidHeaderSpecific(mat,MAT_CLASSID,2); 6984 PetscValidType(mat,2); 6985 if (perm) PetscValidHeaderSpecific(perm,IS_CLASSID,3); 6986 PetscValidPointer(info,4); 6987 PetscValidPointer(fact,1); 6988 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6989 if (info->levels < 0) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Levels negative %D",(PetscInt) info->levels); 6990 if (info->fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %g",(double)info->fill); 6991 if (!(fact)->ops->iccfactorsymbolic) { 6992 MatSolverType stype; 6993 ierr = MatFactorGetSolverType(fact,&stype);CHKERRQ(ierr); 6994 SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s symbolic ICC using solver type %s",((PetscObject)mat)->type_name,stype); 6995 } 6996 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 6997 MatCheckPreallocated(mat,2); 6998 6999 if (!fact->trivialsymbolic) {ierr = PetscLogEventBegin(MAT_ICCFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr);} 7000 ierr = (fact->ops->iccfactorsymbolic)(fact,mat,perm,info);CHKERRQ(ierr); 7001 if (!fact->trivialsymbolic) {ierr = PetscLogEventEnd(MAT_ICCFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr);} 7002 PetscFunctionReturn(0); 7003 } 7004 7005 /*@C 7006 MatCreateSubMatrices - Extracts several submatrices from a matrix. If submat 7007 points to an array of valid matrices, they may be reused to store the new 7008 submatrices. 7009 7010 Collective on Mat 7011 7012 Input Parameters: 7013 + mat - the matrix 7014 . n - the number of submatrixes to be extracted (on this processor, may be zero) 7015 . irow, icol - index sets of rows and columns to extract 7016 - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 7017 7018 Output Parameter: 7019 . submat - the array of submatrices 7020 7021 Notes: 7022 MatCreateSubMatrices() can extract ONLY sequential submatrices 7023 (from both sequential and parallel matrices). Use MatCreateSubMatrix() 7024 to extract a parallel submatrix. 7025 7026 Some matrix types place restrictions on the row and column 7027 indices, such as that they be sorted or that they be equal to each other. 7028 7029 The index sets may not have duplicate entries. 7030 7031 When extracting submatrices from a parallel matrix, each processor can 7032 form a different submatrix by setting the rows and columns of its 7033 individual index sets according to the local submatrix desired. 7034 7035 When finished using the submatrices, the user should destroy 7036 them with MatDestroySubMatrices(). 7037 7038 MAT_REUSE_MATRIX can only be used when the nonzero structure of the 7039 original matrix has not changed from that last call to MatCreateSubMatrices(). 7040 7041 This routine creates the matrices in submat; you should NOT create them before 7042 calling it. It also allocates the array of matrix pointers submat. 7043 7044 For BAIJ matrices the index sets must respect the block structure, that is if they 7045 request one row/column in a block, they must request all rows/columns that are in 7046 that block. For example, if the block size is 2 you cannot request just row 0 and 7047 column 0. 7048 7049 Fortran Note: 7050 The Fortran interface is slightly different from that given below; it 7051 requires one to pass in as submat a Mat (integer) array of size at least n+1. 7052 7053 Level: advanced 7054 7055 .seealso: MatDestroySubMatrices(), MatCreateSubMatrix(), MatGetRow(), MatGetDiagonal(), MatReuse 7056 @*/ 7057 PetscErrorCode MatCreateSubMatrices(Mat mat,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *submat[]) 7058 { 7059 PetscErrorCode ierr; 7060 PetscInt i; 7061 PetscBool eq; 7062 7063 PetscFunctionBegin; 7064 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7065 PetscValidType(mat,1); 7066 if (n) { 7067 PetscValidPointer(irow,3); 7068 PetscValidHeaderSpecific(*irow,IS_CLASSID,3); 7069 PetscValidPointer(icol,4); 7070 PetscValidHeaderSpecific(*icol,IS_CLASSID,4); 7071 } 7072 PetscValidPointer(submat,6); 7073 if (n && scall == MAT_REUSE_MATRIX) { 7074 PetscValidPointer(*submat,6); 7075 PetscValidHeaderSpecific(**submat,MAT_CLASSID,6); 7076 } 7077 if (!mat->ops->createsubmatrices) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 7078 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 7079 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7080 MatCheckPreallocated(mat,1); 7081 7082 ierr = PetscLogEventBegin(MAT_CreateSubMats,mat,0,0,0);CHKERRQ(ierr); 7083 ierr = (*mat->ops->createsubmatrices)(mat,n,irow,icol,scall,submat);CHKERRQ(ierr); 7084 ierr = PetscLogEventEnd(MAT_CreateSubMats,mat,0,0,0);CHKERRQ(ierr); 7085 for (i=0; i<n; i++) { 7086 (*submat)[i]->factortype = MAT_FACTOR_NONE; /* in case in place factorization was previously done on submatrix */ 7087 ierr = ISEqualUnsorted(irow[i],icol[i],&eq);CHKERRQ(ierr); 7088 if (eq) { 7089 ierr = MatPropagateSymmetryOptions(mat,(*submat)[i]);CHKERRQ(ierr); 7090 } 7091 } 7092 PetscFunctionReturn(0); 7093 } 7094 7095 /*@C 7096 MatCreateSubMatricesMPI - Extracts MPI submatrices across a sub communicator of mat (by pairs of IS that may live on subcomms). 7097 7098 Collective on Mat 7099 7100 Input Parameters: 7101 + mat - the matrix 7102 . n - the number of submatrixes to be extracted 7103 . irow, icol - index sets of rows and columns to extract 7104 - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 7105 7106 Output Parameter: 7107 . submat - the array of submatrices 7108 7109 Level: advanced 7110 7111 .seealso: MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRow(), MatGetDiagonal(), MatReuse 7112 @*/ 7113 PetscErrorCode MatCreateSubMatricesMPI(Mat mat,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *submat[]) 7114 { 7115 PetscErrorCode ierr; 7116 PetscInt i; 7117 PetscBool eq; 7118 7119 PetscFunctionBegin; 7120 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7121 PetscValidType(mat,1); 7122 if (n) { 7123 PetscValidPointer(irow,3); 7124 PetscValidHeaderSpecific(*irow,IS_CLASSID,3); 7125 PetscValidPointer(icol,4); 7126 PetscValidHeaderSpecific(*icol,IS_CLASSID,4); 7127 } 7128 PetscValidPointer(submat,6); 7129 if (n && scall == MAT_REUSE_MATRIX) { 7130 PetscValidPointer(*submat,6); 7131 PetscValidHeaderSpecific(**submat,MAT_CLASSID,6); 7132 } 7133 if (!mat->ops->createsubmatricesmpi) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 7134 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 7135 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7136 MatCheckPreallocated(mat,1); 7137 7138 ierr = PetscLogEventBegin(MAT_CreateSubMats,mat,0,0,0);CHKERRQ(ierr); 7139 ierr = (*mat->ops->createsubmatricesmpi)(mat,n,irow,icol,scall,submat);CHKERRQ(ierr); 7140 ierr = PetscLogEventEnd(MAT_CreateSubMats,mat,0,0,0);CHKERRQ(ierr); 7141 for (i=0; i<n; i++) { 7142 ierr = ISEqualUnsorted(irow[i],icol[i],&eq);CHKERRQ(ierr); 7143 if (eq) { 7144 ierr = MatPropagateSymmetryOptions(mat,(*submat)[i]);CHKERRQ(ierr); 7145 } 7146 } 7147 PetscFunctionReturn(0); 7148 } 7149 7150 /*@C 7151 MatDestroyMatrices - Destroys an array of matrices. 7152 7153 Collective on Mat 7154 7155 Input Parameters: 7156 + n - the number of local matrices 7157 - mat - the matrices (note that this is a pointer to the array of matrices) 7158 7159 Level: advanced 7160 7161 Notes: 7162 Frees not only the matrices, but also the array that contains the matrices 7163 In Fortran will not free the array. 7164 7165 .seealso: MatCreateSubMatrices() MatDestroySubMatrices() 7166 @*/ 7167 PetscErrorCode MatDestroyMatrices(PetscInt n,Mat *mat[]) 7168 { 7169 PetscErrorCode ierr; 7170 PetscInt i; 7171 7172 PetscFunctionBegin; 7173 if (!*mat) PetscFunctionReturn(0); 7174 if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Trying to destroy negative number of matrices %D",n); 7175 PetscValidPointer(mat,2); 7176 7177 for (i=0; i<n; i++) { 7178 ierr = MatDestroy(&(*mat)[i]);CHKERRQ(ierr); 7179 } 7180 7181 /* memory is allocated even if n = 0 */ 7182 ierr = PetscFree(*mat);CHKERRQ(ierr); 7183 PetscFunctionReturn(0); 7184 } 7185 7186 /*@C 7187 MatDestroySubMatrices - Destroys a set of matrices obtained with MatCreateSubMatrices(). 7188 7189 Collective on Mat 7190 7191 Input Parameters: 7192 + n - the number of local matrices 7193 - mat - the matrices (note that this is a pointer to the array of matrices, just to match the calling 7194 sequence of MatCreateSubMatrices()) 7195 7196 Level: advanced 7197 7198 Notes: 7199 Frees not only the matrices, but also the array that contains the matrices 7200 In Fortran will not free the array. 7201 7202 .seealso: MatCreateSubMatrices() 7203 @*/ 7204 PetscErrorCode MatDestroySubMatrices(PetscInt n,Mat *mat[]) 7205 { 7206 PetscErrorCode ierr; 7207 Mat mat0; 7208 7209 PetscFunctionBegin; 7210 if (!*mat) PetscFunctionReturn(0); 7211 /* mat[] is an array of length n+1, see MatCreateSubMatrices_xxx() */ 7212 if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Trying to destroy negative number of matrices %D",n); 7213 PetscValidPointer(mat,2); 7214 7215 mat0 = (*mat)[0]; 7216 if (mat0 && mat0->ops->destroysubmatrices) { 7217 ierr = (mat0->ops->destroysubmatrices)(n,mat);CHKERRQ(ierr); 7218 } else { 7219 ierr = MatDestroyMatrices(n,mat);CHKERRQ(ierr); 7220 } 7221 PetscFunctionReturn(0); 7222 } 7223 7224 /*@C 7225 MatGetSeqNonzeroStructure - Extracts the sequential nonzero structure from a matrix. 7226 7227 Collective on Mat 7228 7229 Input Parameters: 7230 . mat - the matrix 7231 7232 Output Parameter: 7233 . matstruct - the sequential matrix with the nonzero structure of mat 7234 7235 Level: intermediate 7236 7237 .seealso: MatDestroySeqNonzeroStructure(), MatCreateSubMatrices(), MatDestroyMatrices() 7238 @*/ 7239 PetscErrorCode MatGetSeqNonzeroStructure(Mat mat,Mat *matstruct) 7240 { 7241 PetscErrorCode ierr; 7242 7243 PetscFunctionBegin; 7244 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7245 PetscValidPointer(matstruct,2); 7246 7247 PetscValidType(mat,1); 7248 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7249 MatCheckPreallocated(mat,1); 7250 7251 if (!mat->ops->getseqnonzerostructure) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Not for matrix type %s\n",((PetscObject)mat)->type_name); 7252 ierr = PetscLogEventBegin(MAT_GetSeqNonzeroStructure,mat,0,0,0);CHKERRQ(ierr); 7253 ierr = (*mat->ops->getseqnonzerostructure)(mat,matstruct);CHKERRQ(ierr); 7254 ierr = PetscLogEventEnd(MAT_GetSeqNonzeroStructure,mat,0,0,0);CHKERRQ(ierr); 7255 PetscFunctionReturn(0); 7256 } 7257 7258 /*@C 7259 MatDestroySeqNonzeroStructure - Destroys matrix obtained with MatGetSeqNonzeroStructure(). 7260 7261 Collective on Mat 7262 7263 Input Parameters: 7264 . mat - the matrix (note that this is a pointer to the array of matrices, just to match the calling 7265 sequence of MatGetSequentialNonzeroStructure()) 7266 7267 Level: advanced 7268 7269 Notes: 7270 Frees not only the matrices, but also the array that contains the matrices 7271 7272 .seealso: MatGetSeqNonzeroStructure() 7273 @*/ 7274 PetscErrorCode MatDestroySeqNonzeroStructure(Mat *mat) 7275 { 7276 PetscErrorCode ierr; 7277 7278 PetscFunctionBegin; 7279 PetscValidPointer(mat,1); 7280 ierr = MatDestroy(mat);CHKERRQ(ierr); 7281 PetscFunctionReturn(0); 7282 } 7283 7284 /*@ 7285 MatIncreaseOverlap - Given a set of submatrices indicated by index sets, 7286 replaces the index sets by larger ones that represent submatrices with 7287 additional overlap. 7288 7289 Collective on Mat 7290 7291 Input Parameters: 7292 + mat - the matrix 7293 . n - the number of index sets 7294 . is - the array of index sets (these index sets will changed during the call) 7295 - ov - the additional overlap requested 7296 7297 Options Database: 7298 . -mat_increase_overlap_scalable - use a scalable algorithm to compute the overlap (supported by MPIAIJ matrix) 7299 7300 Level: developer 7301 7302 .seealso: MatCreateSubMatrices() 7303 @*/ 7304 PetscErrorCode MatIncreaseOverlap(Mat mat,PetscInt n,IS is[],PetscInt ov) 7305 { 7306 PetscErrorCode ierr; 7307 7308 PetscFunctionBegin; 7309 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7310 PetscValidType(mat,1); 7311 if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Must have one or more domains, you have %D",n); 7312 if (n) { 7313 PetscValidPointer(is,3); 7314 PetscValidHeaderSpecific(*is,IS_CLASSID,3); 7315 } 7316 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 7317 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7318 MatCheckPreallocated(mat,1); 7319 7320 if (!ov) PetscFunctionReturn(0); 7321 if (!mat->ops->increaseoverlap) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 7322 ierr = PetscLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr); 7323 ierr = (*mat->ops->increaseoverlap)(mat,n,is,ov);CHKERRQ(ierr); 7324 ierr = PetscLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr); 7325 PetscFunctionReturn(0); 7326 } 7327 7328 PetscErrorCode MatIncreaseOverlapSplit_Single(Mat,IS*,PetscInt); 7329 7330 /*@ 7331 MatIncreaseOverlapSplit - Given a set of submatrices indicated by index sets across 7332 a sub communicator, replaces the index sets by larger ones that represent submatrices with 7333 additional overlap. 7334 7335 Collective on Mat 7336 7337 Input Parameters: 7338 + mat - the matrix 7339 . n - the number of index sets 7340 . is - the array of index sets (these index sets will changed during the call) 7341 - ov - the additional overlap requested 7342 7343 Options Database: 7344 . -mat_increase_overlap_scalable - use a scalable algorithm to compute the overlap (supported by MPIAIJ matrix) 7345 7346 Level: developer 7347 7348 .seealso: MatCreateSubMatrices() 7349 @*/ 7350 PetscErrorCode MatIncreaseOverlapSplit(Mat mat,PetscInt n,IS is[],PetscInt ov) 7351 { 7352 PetscInt i; 7353 PetscErrorCode ierr; 7354 7355 PetscFunctionBegin; 7356 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7357 PetscValidType(mat,1); 7358 if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Must have one or more domains, you have %D",n); 7359 if (n) { 7360 PetscValidPointer(is,3); 7361 PetscValidHeaderSpecific(*is,IS_CLASSID,3); 7362 } 7363 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 7364 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7365 MatCheckPreallocated(mat,1); 7366 if (!ov) PetscFunctionReturn(0); 7367 ierr = PetscLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr); 7368 for (i=0; i<n; i++) { 7369 ierr = MatIncreaseOverlapSplit_Single(mat,&is[i],ov);CHKERRQ(ierr); 7370 } 7371 ierr = PetscLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr); 7372 PetscFunctionReturn(0); 7373 } 7374 7375 /*@ 7376 MatGetBlockSize - Returns the matrix block size. 7377 7378 Not Collective 7379 7380 Input Parameter: 7381 . mat - the matrix 7382 7383 Output Parameter: 7384 . bs - block size 7385 7386 Notes: 7387 Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ. These formats ALWAYS have square block storage in the matrix. 7388 7389 If the block size has not been set yet this routine returns 1. 7390 7391 Level: intermediate 7392 7393 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSizes() 7394 @*/ 7395 PetscErrorCode MatGetBlockSize(Mat mat,PetscInt *bs) 7396 { 7397 PetscFunctionBegin; 7398 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7399 PetscValidIntPointer(bs,2); 7400 *bs = PetscAbs(mat->rmap->bs); 7401 PetscFunctionReturn(0); 7402 } 7403 7404 /*@ 7405 MatGetBlockSizes - Returns the matrix block row and column sizes. 7406 7407 Not Collective 7408 7409 Input Parameter: 7410 . mat - the matrix 7411 7412 Output Parameter: 7413 + rbs - row block size 7414 - cbs - column block size 7415 7416 Notes: 7417 Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ. These formats ALWAYS have square block storage in the matrix. 7418 If you pass a different block size for the columns than the rows, the row block size determines the square block storage. 7419 7420 If a block size has not been set yet this routine returns 1. 7421 7422 Level: intermediate 7423 7424 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSize(), MatSetBlockSizes() 7425 @*/ 7426 PetscErrorCode MatGetBlockSizes(Mat mat,PetscInt *rbs, PetscInt *cbs) 7427 { 7428 PetscFunctionBegin; 7429 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7430 if (rbs) PetscValidIntPointer(rbs,2); 7431 if (cbs) PetscValidIntPointer(cbs,3); 7432 if (rbs) *rbs = PetscAbs(mat->rmap->bs); 7433 if (cbs) *cbs = PetscAbs(mat->cmap->bs); 7434 PetscFunctionReturn(0); 7435 } 7436 7437 /*@ 7438 MatSetBlockSize - Sets the matrix block size. 7439 7440 Logically Collective on Mat 7441 7442 Input Parameters: 7443 + mat - the matrix 7444 - bs - block size 7445 7446 Notes: 7447 Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ. These formats ALWAYS have square block storage in the matrix. 7448 This must be called before MatSetUp() or MatXXXSetPreallocation() (or will default to 1) and the block size cannot be changed later. 7449 7450 For MATMPIAIJ and MATSEQAIJ matrix formats, this function can be called at a later stage, provided that the specified block size 7451 is compatible with the matrix local sizes. 7452 7453 Level: intermediate 7454 7455 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSizes(), MatGetBlockSizes() 7456 @*/ 7457 PetscErrorCode MatSetBlockSize(Mat mat,PetscInt bs) 7458 { 7459 PetscErrorCode ierr; 7460 7461 PetscFunctionBegin; 7462 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7463 PetscValidLogicalCollectiveInt(mat,bs,2); 7464 ierr = MatSetBlockSizes(mat,bs,bs);CHKERRQ(ierr); 7465 PetscFunctionReturn(0); 7466 } 7467 7468 /*@ 7469 MatSetVariableBlockSizes - Sets a diagonal blocks of the matrix that need not be of the same size 7470 7471 Logically Collective on Mat 7472 7473 Input Parameters: 7474 + mat - the matrix 7475 . nblocks - the number of blocks on this process 7476 - bsizes - the block sizes 7477 7478 Notes: 7479 Currently used by PCVPBJACOBI for SeqAIJ matrices 7480 7481 Level: intermediate 7482 7483 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSizes(), MatGetBlockSizes(), MatGetVariableBlockSizes() 7484 @*/ 7485 PetscErrorCode MatSetVariableBlockSizes(Mat mat,PetscInt nblocks,PetscInt *bsizes) 7486 { 7487 PetscErrorCode ierr; 7488 PetscInt i,ncnt = 0, nlocal; 7489 7490 PetscFunctionBegin; 7491 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7492 if (nblocks < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Number of local blocks must be great than or equal to zero"); 7493 ierr = MatGetLocalSize(mat,&nlocal,NULL);CHKERRQ(ierr); 7494 for (i=0; i<nblocks; i++) ncnt += bsizes[i]; 7495 if (ncnt != nlocal) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Sum of local block sizes %D does not equal local size of matrix %D",ncnt,nlocal); 7496 ierr = PetscFree(mat->bsizes);CHKERRQ(ierr); 7497 mat->nblocks = nblocks; 7498 ierr = PetscMalloc1(nblocks,&mat->bsizes);CHKERRQ(ierr); 7499 ierr = PetscArraycpy(mat->bsizes,bsizes,nblocks);CHKERRQ(ierr); 7500 PetscFunctionReturn(0); 7501 } 7502 7503 /*@C 7504 MatGetVariableBlockSizes - Gets a diagonal blocks of the matrix that need not be of the same size 7505 7506 Logically Collective on Mat 7507 7508 Input Parameters: 7509 . mat - the matrix 7510 7511 Output Parameters: 7512 + nblocks - the number of blocks on this process 7513 - bsizes - the block sizes 7514 7515 Notes: Currently not supported from Fortran 7516 7517 Level: intermediate 7518 7519 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSizes(), MatGetBlockSizes(), MatSetVariableBlockSizes() 7520 @*/ 7521 PetscErrorCode MatGetVariableBlockSizes(Mat mat,PetscInt *nblocks,const PetscInt **bsizes) 7522 { 7523 PetscFunctionBegin; 7524 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7525 *nblocks = mat->nblocks; 7526 *bsizes = mat->bsizes; 7527 PetscFunctionReturn(0); 7528 } 7529 7530 /*@ 7531 MatSetBlockSizes - Sets the matrix block row and column sizes. 7532 7533 Logically Collective on Mat 7534 7535 Input Parameters: 7536 + mat - the matrix 7537 . rbs - row block size 7538 - cbs - column block size 7539 7540 Notes: 7541 Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ. These formats ALWAYS have square block storage in the matrix. 7542 If you pass a different block size for the columns than the rows, the row block size determines the square block storage. 7543 This must be called before MatSetUp() or MatXXXSetPreallocation() (or will default to 1) and the block size cannot be changed later. 7544 7545 For MATMPIAIJ and MATSEQAIJ matrix formats, this function can be called at a later stage, provided that the specified block sizes 7546 are compatible with the matrix local sizes. 7547 7548 The row and column block size determine the blocksize of the "row" and "column" vectors returned by MatCreateVecs(). 7549 7550 Level: intermediate 7551 7552 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSize(), MatGetBlockSizes() 7553 @*/ 7554 PetscErrorCode MatSetBlockSizes(Mat mat,PetscInt rbs,PetscInt cbs) 7555 { 7556 PetscErrorCode ierr; 7557 7558 PetscFunctionBegin; 7559 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7560 PetscValidLogicalCollectiveInt(mat,rbs,2); 7561 PetscValidLogicalCollectiveInt(mat,cbs,3); 7562 if (mat->ops->setblocksizes) { 7563 ierr = (*mat->ops->setblocksizes)(mat,rbs,cbs);CHKERRQ(ierr); 7564 } 7565 if (mat->rmap->refcnt) { 7566 ISLocalToGlobalMapping l2g = NULL; 7567 PetscLayout nmap = NULL; 7568 7569 ierr = PetscLayoutDuplicate(mat->rmap,&nmap);CHKERRQ(ierr); 7570 if (mat->rmap->mapping) { 7571 ierr = ISLocalToGlobalMappingDuplicate(mat->rmap->mapping,&l2g);CHKERRQ(ierr); 7572 } 7573 ierr = PetscLayoutDestroy(&mat->rmap);CHKERRQ(ierr); 7574 mat->rmap = nmap; 7575 mat->rmap->mapping = l2g; 7576 } 7577 if (mat->cmap->refcnt) { 7578 ISLocalToGlobalMapping l2g = NULL; 7579 PetscLayout nmap = NULL; 7580 7581 ierr = PetscLayoutDuplicate(mat->cmap,&nmap);CHKERRQ(ierr); 7582 if (mat->cmap->mapping) { 7583 ierr = ISLocalToGlobalMappingDuplicate(mat->cmap->mapping,&l2g);CHKERRQ(ierr); 7584 } 7585 ierr = PetscLayoutDestroy(&mat->cmap);CHKERRQ(ierr); 7586 mat->cmap = nmap; 7587 mat->cmap->mapping = l2g; 7588 } 7589 ierr = PetscLayoutSetBlockSize(mat->rmap,rbs);CHKERRQ(ierr); 7590 ierr = PetscLayoutSetBlockSize(mat->cmap,cbs);CHKERRQ(ierr); 7591 PetscFunctionReturn(0); 7592 } 7593 7594 /*@ 7595 MatSetBlockSizesFromMats - Sets the matrix block row and column sizes to match a pair of matrices 7596 7597 Logically Collective on Mat 7598 7599 Input Parameters: 7600 + mat - the matrix 7601 . fromRow - matrix from which to copy row block size 7602 - fromCol - matrix from which to copy column block size (can be same as fromRow) 7603 7604 Level: developer 7605 7606 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSizes() 7607 @*/ 7608 PetscErrorCode MatSetBlockSizesFromMats(Mat mat,Mat fromRow,Mat fromCol) 7609 { 7610 PetscErrorCode ierr; 7611 7612 PetscFunctionBegin; 7613 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7614 PetscValidHeaderSpecific(fromRow,MAT_CLASSID,2); 7615 PetscValidHeaderSpecific(fromCol,MAT_CLASSID,3); 7616 if (fromRow->rmap->bs > 0) {ierr = PetscLayoutSetBlockSize(mat->rmap,fromRow->rmap->bs);CHKERRQ(ierr);} 7617 if (fromCol->cmap->bs > 0) {ierr = PetscLayoutSetBlockSize(mat->cmap,fromCol->cmap->bs);CHKERRQ(ierr);} 7618 PetscFunctionReturn(0); 7619 } 7620 7621 /*@ 7622 MatResidual - Default routine to calculate the residual. 7623 7624 Collective on Mat 7625 7626 Input Parameters: 7627 + mat - the matrix 7628 . b - the right-hand-side 7629 - x - the approximate solution 7630 7631 Output Parameter: 7632 . r - location to store the residual 7633 7634 Level: developer 7635 7636 .seealso: PCMGSetResidual() 7637 @*/ 7638 PetscErrorCode MatResidual(Mat mat,Vec b,Vec x,Vec r) 7639 { 7640 PetscErrorCode ierr; 7641 7642 PetscFunctionBegin; 7643 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7644 PetscValidHeaderSpecific(b,VEC_CLASSID,2); 7645 PetscValidHeaderSpecific(x,VEC_CLASSID,3); 7646 PetscValidHeaderSpecific(r,VEC_CLASSID,4); 7647 PetscValidType(mat,1); 7648 MatCheckPreallocated(mat,1); 7649 ierr = PetscLogEventBegin(MAT_Residual,mat,0,0,0);CHKERRQ(ierr); 7650 if (!mat->ops->residual) { 7651 ierr = MatMult(mat,x,r);CHKERRQ(ierr); 7652 ierr = VecAYPX(r,-1.0,b);CHKERRQ(ierr); 7653 } else { 7654 ierr = (*mat->ops->residual)(mat,b,x,r);CHKERRQ(ierr); 7655 } 7656 ierr = PetscLogEventEnd(MAT_Residual,mat,0,0,0);CHKERRQ(ierr); 7657 PetscFunctionReturn(0); 7658 } 7659 7660 /*@C 7661 MatGetRowIJ - Returns the compressed row storage i and j indices for sequential matrices. 7662 7663 Collective on Mat 7664 7665 Input Parameters: 7666 + mat - the matrix 7667 . shift - 0 or 1 indicating we want the indices starting at 0 or 1 7668 . symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be symmetrized 7669 - inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the 7670 inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is 7671 always used. 7672 7673 Output Parameters: 7674 + n - number of rows in the (possibly compressed) matrix 7675 . ia - the row pointers; that is ia[0] = 0, ia[row] = ia[row-1] + number of elements in that row of the matrix 7676 . ja - the column indices 7677 - done - indicates if the routine actually worked and returned appropriate ia[] and ja[] arrays; callers 7678 are responsible for handling the case when done == PETSC_FALSE and ia and ja are not set 7679 7680 Level: developer 7681 7682 Notes: 7683 You CANNOT change any of the ia[] or ja[] values. 7684 7685 Use MatRestoreRowIJ() when you are finished accessing the ia[] and ja[] values. 7686 7687 Fortran Notes: 7688 In Fortran use 7689 $ 7690 $ PetscInt ia(1), ja(1) 7691 $ PetscOffset iia, jja 7692 $ call MatGetRowIJ(mat,shift,symmetric,inodecompressed,n,ia,iia,ja,jja,done,ierr) 7693 $ ! Access the ith and jth entries via ia(iia + i) and ja(jja + j) 7694 7695 or 7696 $ 7697 $ PetscInt, pointer :: ia(:),ja(:) 7698 $ call MatGetRowIJF90(mat,shift,symmetric,inodecompressed,n,ia,ja,done,ierr) 7699 $ ! Access the ith and jth entries via ia(i) and ja(j) 7700 7701 .seealso: MatGetColumnIJ(), MatRestoreRowIJ(), MatSeqAIJGetArray() 7702 @*/ 7703 PetscErrorCode MatGetRowIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool *done) 7704 { 7705 PetscErrorCode ierr; 7706 7707 PetscFunctionBegin; 7708 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7709 PetscValidType(mat,1); 7710 PetscValidIntPointer(n,5); 7711 if (ia) PetscValidIntPointer(ia,6); 7712 if (ja) PetscValidIntPointer(ja,7); 7713 PetscValidBoolPointer(done,8); 7714 MatCheckPreallocated(mat,1); 7715 if (!mat->ops->getrowij) *done = PETSC_FALSE; 7716 else { 7717 *done = PETSC_TRUE; 7718 ierr = PetscLogEventBegin(MAT_GetRowIJ,mat,0,0,0);CHKERRQ(ierr); 7719 ierr = (*mat->ops->getrowij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr); 7720 ierr = PetscLogEventEnd(MAT_GetRowIJ,mat,0,0,0);CHKERRQ(ierr); 7721 } 7722 PetscFunctionReturn(0); 7723 } 7724 7725 /*@C 7726 MatGetColumnIJ - Returns the compressed column storage i and j indices for sequential matrices. 7727 7728 Collective on Mat 7729 7730 Input Parameters: 7731 + mat - the matrix 7732 . shift - 1 or zero indicating we want the indices starting at 0 or 1 7733 . symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 7734 symmetrized 7735 . inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the 7736 inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is 7737 always used. 7738 . n - number of columns in the (possibly compressed) matrix 7739 . ia - the column pointers; that is ia[0] = 0, ia[col] = i[col-1] + number of elements in that col of the matrix 7740 - ja - the row indices 7741 7742 Output Parameters: 7743 . done - PETSC_TRUE or PETSC_FALSE, indicating whether the values have been returned 7744 7745 Level: developer 7746 7747 .seealso: MatGetRowIJ(), MatRestoreColumnIJ() 7748 @*/ 7749 PetscErrorCode MatGetColumnIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool *done) 7750 { 7751 PetscErrorCode ierr; 7752 7753 PetscFunctionBegin; 7754 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7755 PetscValidType(mat,1); 7756 PetscValidIntPointer(n,5); 7757 if (ia) PetscValidIntPointer(ia,6); 7758 if (ja) PetscValidIntPointer(ja,7); 7759 PetscValidBoolPointer(done,8); 7760 MatCheckPreallocated(mat,1); 7761 if (!mat->ops->getcolumnij) *done = PETSC_FALSE; 7762 else { 7763 *done = PETSC_TRUE; 7764 ierr = (*mat->ops->getcolumnij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr); 7765 } 7766 PetscFunctionReturn(0); 7767 } 7768 7769 /*@C 7770 MatRestoreRowIJ - Call after you are completed with the ia,ja indices obtained with 7771 MatGetRowIJ(). 7772 7773 Collective on Mat 7774 7775 Input Parameters: 7776 + mat - the matrix 7777 . shift - 1 or zero indicating we want the indices starting at 0 or 1 7778 . symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 7779 symmetrized 7780 . inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the 7781 inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is 7782 always used. 7783 . n - size of (possibly compressed) matrix 7784 . ia - the row pointers 7785 - ja - the column indices 7786 7787 Output Parameters: 7788 . done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned 7789 7790 Note: 7791 This routine zeros out n, ia, and ja. This is to prevent accidental 7792 us of the array after it has been restored. If you pass NULL, it will 7793 not zero the pointers. Use of ia or ja after MatRestoreRowIJ() is invalid. 7794 7795 Level: developer 7796 7797 .seealso: MatGetRowIJ(), MatRestoreColumnIJ() 7798 @*/ 7799 PetscErrorCode MatRestoreRowIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool *done) 7800 { 7801 PetscErrorCode ierr; 7802 7803 PetscFunctionBegin; 7804 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7805 PetscValidType(mat,1); 7806 if (ia) PetscValidIntPointer(ia,6); 7807 if (ja) PetscValidIntPointer(ja,7); 7808 PetscValidBoolPointer(done,8); 7809 MatCheckPreallocated(mat,1); 7810 7811 if (!mat->ops->restorerowij) *done = PETSC_FALSE; 7812 else { 7813 *done = PETSC_TRUE; 7814 ierr = (*mat->ops->restorerowij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr); 7815 if (n) *n = 0; 7816 if (ia) *ia = NULL; 7817 if (ja) *ja = NULL; 7818 } 7819 PetscFunctionReturn(0); 7820 } 7821 7822 /*@C 7823 MatRestoreColumnIJ - Call after you are completed with the ia,ja indices obtained with 7824 MatGetColumnIJ(). 7825 7826 Collective on Mat 7827 7828 Input Parameters: 7829 + mat - the matrix 7830 . shift - 1 or zero indicating we want the indices starting at 0 or 1 7831 - symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 7832 symmetrized 7833 - inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the 7834 inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is 7835 always used. 7836 7837 Output Parameters: 7838 + n - size of (possibly compressed) matrix 7839 . ia - the column pointers 7840 . ja - the row indices 7841 - done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned 7842 7843 Level: developer 7844 7845 .seealso: MatGetColumnIJ(), MatRestoreRowIJ() 7846 @*/ 7847 PetscErrorCode MatRestoreColumnIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool *done) 7848 { 7849 PetscErrorCode ierr; 7850 7851 PetscFunctionBegin; 7852 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7853 PetscValidType(mat,1); 7854 if (ia) PetscValidIntPointer(ia,6); 7855 if (ja) PetscValidIntPointer(ja,7); 7856 PetscValidBoolPointer(done,8); 7857 MatCheckPreallocated(mat,1); 7858 7859 if (!mat->ops->restorecolumnij) *done = PETSC_FALSE; 7860 else { 7861 *done = PETSC_TRUE; 7862 ierr = (*mat->ops->restorecolumnij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr); 7863 if (n) *n = 0; 7864 if (ia) *ia = NULL; 7865 if (ja) *ja = NULL; 7866 } 7867 PetscFunctionReturn(0); 7868 } 7869 7870 /*@C 7871 MatColoringPatch -Used inside matrix coloring routines that 7872 use MatGetRowIJ() and/or MatGetColumnIJ(). 7873 7874 Collective on Mat 7875 7876 Input Parameters: 7877 + mat - the matrix 7878 . ncolors - max color value 7879 . n - number of entries in colorarray 7880 - colorarray - array indicating color for each column 7881 7882 Output Parameters: 7883 . iscoloring - coloring generated using colorarray information 7884 7885 Level: developer 7886 7887 .seealso: MatGetRowIJ(), MatGetColumnIJ() 7888 7889 @*/ 7890 PetscErrorCode MatColoringPatch(Mat mat,PetscInt ncolors,PetscInt n,ISColoringValue colorarray[],ISColoring *iscoloring) 7891 { 7892 PetscErrorCode ierr; 7893 7894 PetscFunctionBegin; 7895 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7896 PetscValidType(mat,1); 7897 PetscValidIntPointer(colorarray,4); 7898 PetscValidPointer(iscoloring,5); 7899 MatCheckPreallocated(mat,1); 7900 7901 if (!mat->ops->coloringpatch) { 7902 ierr = ISColoringCreate(PetscObjectComm((PetscObject)mat),ncolors,n,colorarray,PETSC_OWN_POINTER,iscoloring);CHKERRQ(ierr); 7903 } else { 7904 ierr = (*mat->ops->coloringpatch)(mat,ncolors,n,colorarray,iscoloring);CHKERRQ(ierr); 7905 } 7906 PetscFunctionReturn(0); 7907 } 7908 7909 /*@ 7910 MatSetUnfactored - Resets a factored matrix to be treated as unfactored. 7911 7912 Logically Collective on Mat 7913 7914 Input Parameter: 7915 . mat - the factored matrix to be reset 7916 7917 Notes: 7918 This routine should be used only with factored matrices formed by in-place 7919 factorization via ILU(0) (or by in-place LU factorization for the MATSEQDENSE 7920 format). This option can save memory, for example, when solving nonlinear 7921 systems with a matrix-free Newton-Krylov method and a matrix-based, in-place 7922 ILU(0) preconditioner. 7923 7924 Note that one can specify in-place ILU(0) factorization by calling 7925 .vb 7926 PCType(pc,PCILU); 7927 PCFactorSeUseInPlace(pc); 7928 .ve 7929 or by using the options -pc_type ilu -pc_factor_in_place 7930 7931 In-place factorization ILU(0) can also be used as a local 7932 solver for the blocks within the block Jacobi or additive Schwarz 7933 methods (runtime option: -sub_pc_factor_in_place). See Users-Manual: ch_pc 7934 for details on setting local solver options. 7935 7936 Most users should employ the simplified KSP interface for linear solvers 7937 instead of working directly with matrix algebra routines such as this. 7938 See, e.g., KSPCreate(). 7939 7940 Level: developer 7941 7942 .seealso: PCFactorSetUseInPlace(), PCFactorGetUseInPlace() 7943 7944 @*/ 7945 PetscErrorCode MatSetUnfactored(Mat mat) 7946 { 7947 PetscErrorCode ierr; 7948 7949 PetscFunctionBegin; 7950 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7951 PetscValidType(mat,1); 7952 MatCheckPreallocated(mat,1); 7953 mat->factortype = MAT_FACTOR_NONE; 7954 if (!mat->ops->setunfactored) PetscFunctionReturn(0); 7955 ierr = (*mat->ops->setunfactored)(mat);CHKERRQ(ierr); 7956 PetscFunctionReturn(0); 7957 } 7958 7959 /*MC 7960 MatDenseGetArrayF90 - Accesses a matrix array from Fortran90. 7961 7962 Synopsis: 7963 MatDenseGetArrayF90(Mat x,{Scalar, pointer :: xx_v(:,:)},integer ierr) 7964 7965 Not collective 7966 7967 Input Parameter: 7968 . x - matrix 7969 7970 Output Parameters: 7971 + xx_v - the Fortran90 pointer to the array 7972 - ierr - error code 7973 7974 Example of Usage: 7975 .vb 7976 PetscScalar, pointer xx_v(:,:) 7977 .... 7978 call MatDenseGetArrayF90(x,xx_v,ierr) 7979 a = xx_v(3) 7980 call MatDenseRestoreArrayF90(x,xx_v,ierr) 7981 .ve 7982 7983 Level: advanced 7984 7985 .seealso: MatDenseRestoreArrayF90(), MatDenseGetArray(), MatDenseRestoreArray(), MatSeqAIJGetArrayF90() 7986 7987 M*/ 7988 7989 /*MC 7990 MatDenseRestoreArrayF90 - Restores a matrix array that has been 7991 accessed with MatDenseGetArrayF90(). 7992 7993 Synopsis: 7994 MatDenseRestoreArrayF90(Mat x,{Scalar, pointer :: xx_v(:,:)},integer ierr) 7995 7996 Not collective 7997 7998 Input Parameters: 7999 + x - matrix 8000 - xx_v - the Fortran90 pointer to the array 8001 8002 Output Parameter: 8003 . ierr - error code 8004 8005 Example of Usage: 8006 .vb 8007 PetscScalar, pointer xx_v(:,:) 8008 .... 8009 call MatDenseGetArrayF90(x,xx_v,ierr) 8010 a = xx_v(3) 8011 call MatDenseRestoreArrayF90(x,xx_v,ierr) 8012 .ve 8013 8014 Level: advanced 8015 8016 .seealso: MatDenseGetArrayF90(), MatDenseGetArray(), MatDenseRestoreArray(), MatSeqAIJRestoreArrayF90() 8017 8018 M*/ 8019 8020 /*MC 8021 MatSeqAIJGetArrayF90 - Accesses a matrix array from Fortran90. 8022 8023 Synopsis: 8024 MatSeqAIJGetArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr) 8025 8026 Not collective 8027 8028 Input Parameter: 8029 . x - matrix 8030 8031 Output Parameters: 8032 + xx_v - the Fortran90 pointer to the array 8033 - ierr - error code 8034 8035 Example of Usage: 8036 .vb 8037 PetscScalar, pointer xx_v(:) 8038 .... 8039 call MatSeqAIJGetArrayF90(x,xx_v,ierr) 8040 a = xx_v(3) 8041 call MatSeqAIJRestoreArrayF90(x,xx_v,ierr) 8042 .ve 8043 8044 Level: advanced 8045 8046 .seealso: MatSeqAIJRestoreArrayF90(), MatSeqAIJGetArray(), MatSeqAIJRestoreArray(), MatDenseGetArrayF90() 8047 8048 M*/ 8049 8050 /*MC 8051 MatSeqAIJRestoreArrayF90 - Restores a matrix array that has been 8052 accessed with MatSeqAIJGetArrayF90(). 8053 8054 Synopsis: 8055 MatSeqAIJRestoreArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr) 8056 8057 Not collective 8058 8059 Input Parameters: 8060 + x - matrix 8061 - xx_v - the Fortran90 pointer to the array 8062 8063 Output Parameter: 8064 . ierr - error code 8065 8066 Example of Usage: 8067 .vb 8068 PetscScalar, pointer xx_v(:) 8069 .... 8070 call MatSeqAIJGetArrayF90(x,xx_v,ierr) 8071 a = xx_v(3) 8072 call MatSeqAIJRestoreArrayF90(x,xx_v,ierr) 8073 .ve 8074 8075 Level: advanced 8076 8077 .seealso: MatSeqAIJGetArrayF90(), MatSeqAIJGetArray(), MatSeqAIJRestoreArray(), MatDenseRestoreArrayF90() 8078 8079 M*/ 8080 8081 /*@ 8082 MatCreateSubMatrix - Gets a single submatrix on the same number of processors 8083 as the original matrix. 8084 8085 Collective on Mat 8086 8087 Input Parameters: 8088 + mat - the original matrix 8089 . isrow - parallel IS containing the rows this processor should obtain 8090 . 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. 8091 - cll - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 8092 8093 Output Parameter: 8094 . newmat - the new submatrix, of the same type as the old 8095 8096 Level: advanced 8097 8098 Notes: 8099 The submatrix will be able to be multiplied with vectors using the same layout as iscol. 8100 8101 Some matrix types place restrictions on the row and column indices, such 8102 as that they be sorted or that they be equal to each other. 8103 8104 The index sets may not have duplicate entries. 8105 8106 The first time this is called you should use a cll of MAT_INITIAL_MATRIX, 8107 the MatCreateSubMatrix() routine will create the newmat for you. Any additional calls 8108 to this routine with a mat of the same nonzero structure and with a call of MAT_REUSE_MATRIX 8109 will reuse the matrix generated the first time. You should call MatDestroy() on newmat when 8110 you are finished using it. 8111 8112 The communicator of the newly obtained matrix is ALWAYS the same as the communicator of 8113 the input matrix. 8114 8115 If iscol is NULL then all columns are obtained (not supported in Fortran). 8116 8117 Example usage: 8118 Consider the following 8x8 matrix with 34 non-zero values, that is 8119 assembled across 3 processors. Let's assume that proc0 owns 3 rows, 8120 proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown 8121 as follows: 8122 8123 .vb 8124 1 2 0 | 0 3 0 | 0 4 8125 Proc0 0 5 6 | 7 0 0 | 8 0 8126 9 0 10 | 11 0 0 | 12 0 8127 ------------------------------------- 8128 13 0 14 | 15 16 17 | 0 0 8129 Proc1 0 18 0 | 19 20 21 | 0 0 8130 0 0 0 | 22 23 0 | 24 0 8131 ------------------------------------- 8132 Proc2 25 26 27 | 0 0 28 | 29 0 8133 30 0 0 | 31 32 33 | 0 34 8134 .ve 8135 8136 Suppose isrow = [0 1 | 4 | 6 7] and iscol = [1 2 | 3 4 5 | 6]. The resulting submatrix is 8137 8138 .vb 8139 2 0 | 0 3 0 | 0 8140 Proc0 5 6 | 7 0 0 | 8 8141 ------------------------------- 8142 Proc1 18 0 | 19 20 21 | 0 8143 ------------------------------- 8144 Proc2 26 27 | 0 0 28 | 29 8145 0 0 | 31 32 33 | 0 8146 .ve 8147 8148 .seealso: MatCreateSubMatrices(), MatCreateSubMatricesMPI(), MatCreateSubMatrixVirtual(), MatSubMatrixVirtualUpdate() 8149 @*/ 8150 PetscErrorCode MatCreateSubMatrix(Mat mat,IS isrow,IS iscol,MatReuse cll,Mat *newmat) 8151 { 8152 PetscErrorCode ierr; 8153 PetscMPIInt size; 8154 Mat *local; 8155 IS iscoltmp; 8156 PetscBool flg; 8157 8158 PetscFunctionBegin; 8159 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8160 PetscValidHeaderSpecific(isrow,IS_CLASSID,2); 8161 if (iscol) PetscValidHeaderSpecific(iscol,IS_CLASSID,3); 8162 PetscValidPointer(newmat,5); 8163 if (cll == MAT_REUSE_MATRIX) PetscValidHeaderSpecific(*newmat,MAT_CLASSID,5); 8164 PetscValidType(mat,1); 8165 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 8166 if (cll == MAT_IGNORE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Cannot use MAT_IGNORE_MATRIX"); 8167 8168 MatCheckPreallocated(mat,1); 8169 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);CHKERRMPI(ierr); 8170 8171 if (!iscol || isrow == iscol) { 8172 PetscBool stride; 8173 PetscMPIInt grabentirematrix = 0,grab; 8174 ierr = PetscObjectTypeCompare((PetscObject)isrow,ISSTRIDE,&stride);CHKERRQ(ierr); 8175 if (stride) { 8176 PetscInt first,step,n,rstart,rend; 8177 ierr = ISStrideGetInfo(isrow,&first,&step);CHKERRQ(ierr); 8178 if (step == 1) { 8179 ierr = MatGetOwnershipRange(mat,&rstart,&rend);CHKERRQ(ierr); 8180 if (rstart == first) { 8181 ierr = ISGetLocalSize(isrow,&n);CHKERRQ(ierr); 8182 if (n == rend-rstart) { 8183 grabentirematrix = 1; 8184 } 8185 } 8186 } 8187 } 8188 ierr = MPIU_Allreduce(&grabentirematrix,&grab,1,MPI_INT,MPI_MIN,PetscObjectComm((PetscObject)mat));CHKERRMPI(ierr); 8189 if (grab) { 8190 ierr = PetscInfo(mat,"Getting entire matrix as submatrix\n");CHKERRQ(ierr); 8191 if (cll == MAT_INITIAL_MATRIX) { 8192 *newmat = mat; 8193 ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr); 8194 } 8195 PetscFunctionReturn(0); 8196 } 8197 } 8198 8199 if (!iscol) { 8200 ierr = ISCreateStride(PetscObjectComm((PetscObject)mat),mat->cmap->n,mat->cmap->rstart,1,&iscoltmp);CHKERRQ(ierr); 8201 } else { 8202 iscoltmp = iscol; 8203 } 8204 8205 /* if original matrix is on just one processor then use submatrix generated */ 8206 if (mat->ops->createsubmatrices && !mat->ops->createsubmatrix && size == 1 && cll == MAT_REUSE_MATRIX) { 8207 ierr = MatCreateSubMatrices(mat,1,&isrow,&iscoltmp,MAT_REUSE_MATRIX,&newmat);CHKERRQ(ierr); 8208 goto setproperties; 8209 } else if (mat->ops->createsubmatrices && !mat->ops->createsubmatrix && size == 1) { 8210 ierr = MatCreateSubMatrices(mat,1,&isrow,&iscoltmp,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr); 8211 *newmat = *local; 8212 ierr = PetscFree(local);CHKERRQ(ierr); 8213 goto setproperties; 8214 } else if (!mat->ops->createsubmatrix) { 8215 /* Create a new matrix type that implements the operation using the full matrix */ 8216 ierr = PetscLogEventBegin(MAT_CreateSubMat,mat,0,0,0);CHKERRQ(ierr); 8217 switch (cll) { 8218 case MAT_INITIAL_MATRIX: 8219 ierr = MatCreateSubMatrixVirtual(mat,isrow,iscoltmp,newmat);CHKERRQ(ierr); 8220 break; 8221 case MAT_REUSE_MATRIX: 8222 ierr = MatSubMatrixVirtualUpdate(*newmat,mat,isrow,iscoltmp);CHKERRQ(ierr); 8223 break; 8224 default: SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Invalid MatReuse, must be either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX"); 8225 } 8226 ierr = PetscLogEventEnd(MAT_CreateSubMat,mat,0,0,0);CHKERRQ(ierr); 8227 goto setproperties; 8228 } 8229 8230 if (!mat->ops->createsubmatrix) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 8231 ierr = PetscLogEventBegin(MAT_CreateSubMat,mat,0,0,0);CHKERRQ(ierr); 8232 ierr = (*mat->ops->createsubmatrix)(mat,isrow,iscoltmp,cll,newmat);CHKERRQ(ierr); 8233 ierr = PetscLogEventEnd(MAT_CreateSubMat,mat,0,0,0);CHKERRQ(ierr); 8234 8235 setproperties: 8236 ierr = ISEqualUnsorted(isrow,iscoltmp,&flg);CHKERRQ(ierr); 8237 if (flg) { 8238 ierr = MatPropagateSymmetryOptions(mat,*newmat);CHKERRQ(ierr); 8239 } 8240 if (!iscol) {ierr = ISDestroy(&iscoltmp);CHKERRQ(ierr);} 8241 if (*newmat && cll == MAT_INITIAL_MATRIX) {ierr = PetscObjectStateIncrease((PetscObject)*newmat);CHKERRQ(ierr);} 8242 PetscFunctionReturn(0); 8243 } 8244 8245 /*@ 8246 MatPropagateSymmetryOptions - Propagates symmetry options set on a matrix to another matrix 8247 8248 Not Collective 8249 8250 Input Parameters: 8251 + A - the matrix we wish to propagate options from 8252 - B - the matrix we wish to propagate options to 8253 8254 Level: beginner 8255 8256 Notes: Propagates the options associated to MAT_SYMMETRY_ETERNAL, MAT_STRUCTURALLY_SYMMETRIC, MAT_HERMITIAN, MAT_SPD and MAT_SYMMETRIC 8257 8258 .seealso: MatSetOption() 8259 @*/ 8260 PetscErrorCode MatPropagateSymmetryOptions(Mat A, Mat B) 8261 { 8262 PetscErrorCode ierr; 8263 8264 PetscFunctionBegin; 8265 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 8266 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 8267 if (A->symmetric_eternal) { /* symmetric_eternal does not have a corresponding *set flag */ 8268 ierr = MatSetOption(B,MAT_SYMMETRY_ETERNAL,A->symmetric_eternal);CHKERRQ(ierr); 8269 } 8270 if (A->structurally_symmetric_set) { 8271 ierr = MatSetOption(B,MAT_STRUCTURALLY_SYMMETRIC,A->structurally_symmetric);CHKERRQ(ierr); 8272 } 8273 if (A->hermitian_set) { 8274 ierr = MatSetOption(B,MAT_HERMITIAN,A->hermitian);CHKERRQ(ierr); 8275 } 8276 if (A->spd_set) { 8277 ierr = MatSetOption(B,MAT_SPD,A->spd);CHKERRQ(ierr); 8278 } 8279 if (A->symmetric_set) { 8280 ierr = MatSetOption(B,MAT_SYMMETRIC,A->symmetric);CHKERRQ(ierr); 8281 } 8282 PetscFunctionReturn(0); 8283 } 8284 8285 /*@ 8286 MatStashSetInitialSize - sets the sizes of the matrix stash, that is 8287 used during the assembly process to store values that belong to 8288 other processors. 8289 8290 Not Collective 8291 8292 Input Parameters: 8293 + mat - the matrix 8294 . size - the initial size of the stash. 8295 - bsize - the initial size of the block-stash(if used). 8296 8297 Options Database Keys: 8298 + -matstash_initial_size <size> or <size0,size1,...sizep-1> 8299 - -matstash_block_initial_size <bsize> or <bsize0,bsize1,...bsizep-1> 8300 8301 Level: intermediate 8302 8303 Notes: 8304 The block-stash is used for values set with MatSetValuesBlocked() while 8305 the stash is used for values set with MatSetValues() 8306 8307 Run with the option -info and look for output of the form 8308 MatAssemblyBegin_MPIXXX:Stash has MM entries, uses nn mallocs. 8309 to determine the appropriate value, MM, to use for size and 8310 MatAssemblyBegin_MPIXXX:Block-Stash has BMM entries, uses nn mallocs. 8311 to determine the value, BMM to use for bsize 8312 8313 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), Mat, MatStashGetInfo() 8314 8315 @*/ 8316 PetscErrorCode MatStashSetInitialSize(Mat mat,PetscInt size, PetscInt bsize) 8317 { 8318 PetscErrorCode ierr; 8319 8320 PetscFunctionBegin; 8321 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8322 PetscValidType(mat,1); 8323 ierr = MatStashSetInitialSize_Private(&mat->stash,size);CHKERRQ(ierr); 8324 ierr = MatStashSetInitialSize_Private(&mat->bstash,bsize);CHKERRQ(ierr); 8325 PetscFunctionReturn(0); 8326 } 8327 8328 /*@ 8329 MatInterpolateAdd - w = y + A*x or A'*x depending on the shape of 8330 the matrix 8331 8332 Neighbor-wise Collective on Mat 8333 8334 Input Parameters: 8335 + mat - the matrix 8336 . x,y - the vectors 8337 - w - where the result is stored 8338 8339 Level: intermediate 8340 8341 Notes: 8342 w may be the same vector as y. 8343 8344 This allows one to use either the restriction or interpolation (its transpose) 8345 matrix to do the interpolation 8346 8347 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict() 8348 8349 @*/ 8350 PetscErrorCode MatInterpolateAdd(Mat A,Vec x,Vec y,Vec w) 8351 { 8352 PetscErrorCode ierr; 8353 PetscInt M,N,Ny; 8354 8355 PetscFunctionBegin; 8356 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 8357 PetscValidHeaderSpecific(x,VEC_CLASSID,2); 8358 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 8359 PetscValidHeaderSpecific(w,VEC_CLASSID,4); 8360 ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr); 8361 ierr = VecGetSize(y,&Ny);CHKERRQ(ierr); 8362 if (M == Ny) { 8363 ierr = MatMultAdd(A,x,y,w);CHKERRQ(ierr); 8364 } else { 8365 ierr = MatMultTransposeAdd(A,x,y,w);CHKERRQ(ierr); 8366 } 8367 PetscFunctionReturn(0); 8368 } 8369 8370 /*@ 8371 MatInterpolate - y = A*x or A'*x depending on the shape of 8372 the matrix 8373 8374 Neighbor-wise Collective on Mat 8375 8376 Input Parameters: 8377 + mat - the matrix 8378 - x,y - the vectors 8379 8380 Level: intermediate 8381 8382 Notes: 8383 This allows one to use either the restriction or interpolation (its transpose) 8384 matrix to do the interpolation 8385 8386 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict() 8387 8388 @*/ 8389 PetscErrorCode MatInterpolate(Mat A,Vec x,Vec y) 8390 { 8391 PetscErrorCode ierr; 8392 PetscInt M,N,Ny; 8393 8394 PetscFunctionBegin; 8395 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 8396 PetscValidHeaderSpecific(x,VEC_CLASSID,2); 8397 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 8398 ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr); 8399 ierr = VecGetSize(y,&Ny);CHKERRQ(ierr); 8400 if (M == Ny) { 8401 ierr = MatMult(A,x,y);CHKERRQ(ierr); 8402 } else { 8403 ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr); 8404 } 8405 PetscFunctionReturn(0); 8406 } 8407 8408 /*@ 8409 MatRestrict - y = A*x or A'*x 8410 8411 Neighbor-wise Collective on Mat 8412 8413 Input Parameters: 8414 + mat - the matrix 8415 - x,y - the vectors 8416 8417 Level: intermediate 8418 8419 Notes: 8420 This allows one to use either the restriction or interpolation (its transpose) 8421 matrix to do the restriction 8422 8423 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatInterpolate() 8424 8425 @*/ 8426 PetscErrorCode MatRestrict(Mat A,Vec x,Vec y) 8427 { 8428 PetscErrorCode ierr; 8429 PetscInt M,N,Ny; 8430 8431 PetscFunctionBegin; 8432 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 8433 PetscValidHeaderSpecific(x,VEC_CLASSID,2); 8434 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 8435 ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr); 8436 ierr = VecGetSize(y,&Ny);CHKERRQ(ierr); 8437 if (M == Ny) { 8438 ierr = MatMult(A,x,y);CHKERRQ(ierr); 8439 } else { 8440 ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr); 8441 } 8442 PetscFunctionReturn(0); 8443 } 8444 8445 /*@ 8446 MatMatInterpolateAdd - Y = W + A*X or W + A'*X 8447 8448 Neighbor-wise Collective on Mat 8449 8450 Input Parameters: 8451 + mat - the matrix 8452 - w, x - the input dense matrices 8453 8454 Output Parameters: 8455 . y - the output dense matrix 8456 8457 Level: intermediate 8458 8459 Notes: 8460 This allows one to use either the restriction or interpolation (its transpose) 8461 matrix to do the interpolation. y matrix can be reused if already created with the proper sizes, 8462 otherwise it will be recreated. y must be initialized to NULL if not supplied. 8463 8464 .seealso: MatInterpolateAdd(), MatMatInterpolate(), MatMatRestrict() 8465 8466 @*/ 8467 PetscErrorCode MatMatInterpolateAdd(Mat A,Mat x,Mat w,Mat *y) 8468 { 8469 PetscErrorCode ierr; 8470 PetscInt M,N,Mx,Nx,Mo,My = 0,Ny = 0; 8471 PetscBool trans = PETSC_TRUE; 8472 MatReuse reuse = MAT_INITIAL_MATRIX; 8473 8474 PetscFunctionBegin; 8475 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 8476 PetscValidHeaderSpecific(x,MAT_CLASSID,2); 8477 PetscValidType(x,2); 8478 if (w) PetscValidHeaderSpecific(w,MAT_CLASSID,3); 8479 if (*y) PetscValidHeaderSpecific(*y,MAT_CLASSID,4); 8480 ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr); 8481 ierr = MatGetSize(x,&Mx,&Nx);CHKERRQ(ierr); 8482 if (N == Mx) trans = PETSC_FALSE; 8483 else if (M != Mx) SETERRQ4(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Size mismatch: A %Dx%D, X %Dx%D",M,N,Mx,Nx); 8484 Mo = trans ? N : M; 8485 if (*y) { 8486 ierr = MatGetSize(*y,&My,&Ny);CHKERRQ(ierr); 8487 if (Mo == My && Nx == Ny) { reuse = MAT_REUSE_MATRIX; } 8488 else { 8489 if (w && *y == w) SETERRQ6(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Cannot reuse y and w, size mismatch: A %Dx%D, X %Dx%D, Y %Dx%D",M,N,Mx,Nx,My,Ny); 8490 ierr = MatDestroy(y);CHKERRQ(ierr); 8491 } 8492 } 8493 8494 if (w && *y == w) { /* this is to minimize changes in PCMG */ 8495 PetscBool flg; 8496 8497 ierr = PetscObjectQuery((PetscObject)*y,"__MatMatIntAdd_w",(PetscObject*)&w);CHKERRQ(ierr); 8498 if (w) { 8499 PetscInt My,Ny,Mw,Nw; 8500 8501 ierr = PetscObjectTypeCompare((PetscObject)*y,((PetscObject)w)->type_name,&flg);CHKERRQ(ierr); 8502 ierr = MatGetSize(*y,&My,&Ny);CHKERRQ(ierr); 8503 ierr = MatGetSize(w,&Mw,&Nw);CHKERRQ(ierr); 8504 if (!flg || My != Mw || Ny != Nw) w = NULL; 8505 } 8506 if (!w) { 8507 ierr = MatDuplicate(*y,MAT_COPY_VALUES,&w);CHKERRQ(ierr); 8508 ierr = PetscObjectCompose((PetscObject)*y,"__MatMatIntAdd_w",(PetscObject)w);CHKERRQ(ierr); 8509 ierr = PetscLogObjectParent((PetscObject)*y,(PetscObject)w);CHKERRQ(ierr); 8510 ierr = PetscObjectDereference((PetscObject)w);CHKERRQ(ierr); 8511 } else { 8512 ierr = MatCopy(*y,w,UNKNOWN_NONZERO_PATTERN);CHKERRQ(ierr); 8513 } 8514 } 8515 if (!trans) { 8516 ierr = MatMatMult(A,x,reuse,PETSC_DEFAULT,y);CHKERRQ(ierr); 8517 } else { 8518 ierr = MatTransposeMatMult(A,x,reuse,PETSC_DEFAULT,y);CHKERRQ(ierr); 8519 } 8520 if (w) { 8521 ierr = MatAXPY(*y,1.0,w,UNKNOWN_NONZERO_PATTERN);CHKERRQ(ierr); 8522 } 8523 PetscFunctionReturn(0); 8524 } 8525 8526 /*@ 8527 MatMatInterpolate - Y = A*X or A'*X 8528 8529 Neighbor-wise Collective on Mat 8530 8531 Input Parameters: 8532 + mat - the matrix 8533 - x - the input dense matrix 8534 8535 Output Parameters: 8536 . y - the output dense matrix 8537 8538 Level: intermediate 8539 8540 Notes: 8541 This allows one to use either the restriction or interpolation (its transpose) 8542 matrix to do the interpolation. y matrix can be reused if already created with the proper sizes, 8543 otherwise it will be recreated. y must be initialized to NULL if not supplied. 8544 8545 .seealso: MatInterpolate(), MatRestrict(), MatMatRestrict() 8546 8547 @*/ 8548 PetscErrorCode MatMatInterpolate(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 MatMatRestrict - Y = A*X or A'*X 8559 8560 Neighbor-wise Collective on Mat 8561 8562 Input Parameters: 8563 + mat - the matrix 8564 - x - the input dense matrix 8565 8566 Output Parameters: 8567 . y - the output dense matrix 8568 8569 Level: intermediate 8570 8571 Notes: 8572 This allows one to use either the restriction or interpolation (its transpose) 8573 matrix to do the restriction. y matrix can be reused if already created with the proper sizes, 8574 otherwise it will be recreated. y must be initialized to NULL if not supplied. 8575 8576 .seealso: MatRestrict(), MatInterpolate(), MatMatInterpolate() 8577 @*/ 8578 PetscErrorCode MatMatRestrict(Mat A,Mat x,Mat *y) 8579 { 8580 PetscErrorCode ierr; 8581 8582 PetscFunctionBegin; 8583 ierr = MatMatInterpolateAdd(A,x,NULL,y);CHKERRQ(ierr); 8584 PetscFunctionReturn(0); 8585 } 8586 8587 /*@ 8588 MatGetNullSpace - retrieves the null space of a matrix. 8589 8590 Logically Collective on Mat 8591 8592 Input Parameters: 8593 + mat - the matrix 8594 - nullsp - the null space object 8595 8596 Level: developer 8597 8598 .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace(), MatSetNullSpace() 8599 @*/ 8600 PetscErrorCode MatGetNullSpace(Mat mat, MatNullSpace *nullsp) 8601 { 8602 PetscFunctionBegin; 8603 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8604 PetscValidPointer(nullsp,2); 8605 *nullsp = (mat->symmetric_set && mat->symmetric && !mat->nullsp) ? mat->transnullsp : mat->nullsp; 8606 PetscFunctionReturn(0); 8607 } 8608 8609 /*@ 8610 MatSetNullSpace - attaches a null space to a matrix. 8611 8612 Logically Collective on Mat 8613 8614 Input Parameters: 8615 + mat - the matrix 8616 - nullsp - the null space object 8617 8618 Level: advanced 8619 8620 Notes: 8621 This null space is used by the linear solvers. Overwrites any previous null space that may have been attached 8622 8623 For inconsistent singular systems (linear systems where the right hand side is not in the range of the operator) you also likely should 8624 call MatSetTransposeNullSpace(). This allows the linear system to be solved in a least squares sense. 8625 8626 You can remove the null space by calling this routine with an nullsp of NULL 8627 8628 The fundamental theorem of linear algebra (Gilbert Strang, Introduction to Applied Mathematics, page 72) states that 8629 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). 8630 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 8631 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 8632 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). 8633 8634 Krylov solvers can produce the minimal norm solution to the least squares problem by utilizing MatNullSpaceRemove(). 8635 8636 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 8637 routine also automatically calls MatSetTransposeNullSpace(). 8638 8639 .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace(), MatGetNullSpace(), MatSetTransposeNullSpace(), MatGetTransposeNullSpace(), MatNullSpaceRemove() 8640 @*/ 8641 PetscErrorCode MatSetNullSpace(Mat mat,MatNullSpace nullsp) 8642 { 8643 PetscErrorCode ierr; 8644 8645 PetscFunctionBegin; 8646 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8647 if (nullsp) PetscValidHeaderSpecific(nullsp,MAT_NULLSPACE_CLASSID,2); 8648 if (nullsp) {ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr);} 8649 ierr = MatNullSpaceDestroy(&mat->nullsp);CHKERRQ(ierr); 8650 mat->nullsp = nullsp; 8651 if (mat->symmetric_set && mat->symmetric) { 8652 ierr = MatSetTransposeNullSpace(mat,nullsp);CHKERRQ(ierr); 8653 } 8654 PetscFunctionReturn(0); 8655 } 8656 8657 /*@ 8658 MatGetTransposeNullSpace - retrieves the null space of the transpose of a matrix. 8659 8660 Logically Collective on Mat 8661 8662 Input Parameters: 8663 + mat - the matrix 8664 - nullsp - the null space object 8665 8666 Level: developer 8667 8668 .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace(), MatSetTransposeNullSpace(), MatSetNullSpace(), MatGetNullSpace() 8669 @*/ 8670 PetscErrorCode MatGetTransposeNullSpace(Mat mat, MatNullSpace *nullsp) 8671 { 8672 PetscFunctionBegin; 8673 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8674 PetscValidType(mat,1); 8675 PetscValidPointer(nullsp,2); 8676 *nullsp = (mat->symmetric_set && mat->symmetric && !mat->transnullsp) ? mat->nullsp : mat->transnullsp; 8677 PetscFunctionReturn(0); 8678 } 8679 8680 /*@ 8681 MatSetTransposeNullSpace - attaches a null space to a matrix. 8682 8683 Logically Collective on Mat 8684 8685 Input Parameters: 8686 + mat - the matrix 8687 - nullsp - the null space object 8688 8689 Level: advanced 8690 8691 Notes: 8692 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. 8693 You must also call MatSetNullSpace() 8694 8695 The fundamental theorem of linear algebra (Gilbert Strang, Introduction to Applied Mathematics, page 72) states that 8696 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). 8697 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 8698 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 8699 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). 8700 8701 Krylov solvers can produce the minimal norm solution to the least squares problem by utilizing MatNullSpaceRemove(). 8702 8703 .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace(), MatGetNullSpace(), MatSetNullSpace(), MatGetTransposeNullSpace(), MatNullSpaceRemove() 8704 @*/ 8705 PetscErrorCode MatSetTransposeNullSpace(Mat mat,MatNullSpace nullsp) 8706 { 8707 PetscErrorCode ierr; 8708 8709 PetscFunctionBegin; 8710 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8711 if (nullsp) PetscValidHeaderSpecific(nullsp,MAT_NULLSPACE_CLASSID,2); 8712 if (nullsp) {ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr);} 8713 ierr = MatNullSpaceDestroy(&mat->transnullsp);CHKERRQ(ierr); 8714 mat->transnullsp = nullsp; 8715 PetscFunctionReturn(0); 8716 } 8717 8718 /*@ 8719 MatSetNearNullSpace - attaches a null space to a matrix, which is often the null space (rigid body modes) of the operator without boundary conditions 8720 This null space will be used to provide near null space vectors to a multigrid preconditioner built from this matrix. 8721 8722 Logically Collective on Mat 8723 8724 Input Parameters: 8725 + mat - the matrix 8726 - nullsp - the null space object 8727 8728 Level: advanced 8729 8730 Notes: 8731 Overwrites any previous near null space that may have been attached 8732 8733 You can remove the null space by calling this routine with an nullsp of NULL 8734 8735 .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNullSpace(), MatNullSpaceCreateRigidBody(), MatGetNearNullSpace() 8736 @*/ 8737 PetscErrorCode MatSetNearNullSpace(Mat mat,MatNullSpace nullsp) 8738 { 8739 PetscErrorCode ierr; 8740 8741 PetscFunctionBegin; 8742 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8743 PetscValidType(mat,1); 8744 if (nullsp) PetscValidHeaderSpecific(nullsp,MAT_NULLSPACE_CLASSID,2); 8745 MatCheckPreallocated(mat,1); 8746 if (nullsp) {ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr);} 8747 ierr = MatNullSpaceDestroy(&mat->nearnullsp);CHKERRQ(ierr); 8748 mat->nearnullsp = nullsp; 8749 PetscFunctionReturn(0); 8750 } 8751 8752 /*@ 8753 MatGetNearNullSpace - Get null space attached with MatSetNearNullSpace() 8754 8755 Not Collective 8756 8757 Input Parameter: 8758 . mat - the matrix 8759 8760 Output Parameter: 8761 . nullsp - the null space object, NULL if not set 8762 8763 Level: developer 8764 8765 .seealso: MatSetNearNullSpace(), MatGetNullSpace(), MatNullSpaceCreate() 8766 @*/ 8767 PetscErrorCode MatGetNearNullSpace(Mat mat,MatNullSpace *nullsp) 8768 { 8769 PetscFunctionBegin; 8770 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8771 PetscValidType(mat,1); 8772 PetscValidPointer(nullsp,2); 8773 MatCheckPreallocated(mat,1); 8774 *nullsp = mat->nearnullsp; 8775 PetscFunctionReturn(0); 8776 } 8777 8778 /*@C 8779 MatICCFactor - Performs in-place incomplete Cholesky factorization of matrix. 8780 8781 Collective on Mat 8782 8783 Input Parameters: 8784 + mat - the matrix 8785 . row - row/column permutation 8786 . fill - expected fill factor >= 1.0 8787 - level - level of fill, for ICC(k) 8788 8789 Notes: 8790 Probably really in-place only when level of fill is zero, otherwise allocates 8791 new space to store factored matrix and deletes previous memory. 8792 8793 Most users should employ the simplified KSP interface for linear solvers 8794 instead of working directly with matrix algebra routines such as this. 8795 See, e.g., KSPCreate(). 8796 8797 Level: developer 8798 8799 .seealso: MatICCFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor() 8800 8801 Developer Note: fortran interface is not autogenerated as the f90 8802 interface defintion cannot be generated correctly [due to MatFactorInfo] 8803 8804 @*/ 8805 PetscErrorCode MatICCFactor(Mat mat,IS row,const MatFactorInfo *info) 8806 { 8807 PetscErrorCode ierr; 8808 8809 PetscFunctionBegin; 8810 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8811 PetscValidType(mat,1); 8812 if (row) PetscValidHeaderSpecific(row,IS_CLASSID,2); 8813 PetscValidPointer(info,3); 8814 if (mat->rmap->N != mat->cmap->N) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONG,"matrix must be square"); 8815 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 8816 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 8817 if (!mat->ops->iccfactor) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 8818 MatCheckPreallocated(mat,1); 8819 ierr = (*mat->ops->iccfactor)(mat,row,info);CHKERRQ(ierr); 8820 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 8821 PetscFunctionReturn(0); 8822 } 8823 8824 /*@ 8825 MatDiagonalScaleLocal - Scales columns of a matrix given the scaling values including the 8826 ghosted ones. 8827 8828 Not Collective 8829 8830 Input Parameters: 8831 + mat - the matrix 8832 - diag = the diagonal values, including ghost ones 8833 8834 Level: developer 8835 8836 Notes: 8837 Works only for MPIAIJ and MPIBAIJ matrices 8838 8839 .seealso: MatDiagonalScale() 8840 @*/ 8841 PetscErrorCode MatDiagonalScaleLocal(Mat mat,Vec diag) 8842 { 8843 PetscErrorCode ierr; 8844 PetscMPIInt size; 8845 8846 PetscFunctionBegin; 8847 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8848 PetscValidHeaderSpecific(diag,VEC_CLASSID,2); 8849 PetscValidType(mat,1); 8850 8851 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled"); 8852 ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 8853 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);CHKERRMPI(ierr); 8854 if (size == 1) { 8855 PetscInt n,m; 8856 ierr = VecGetSize(diag,&n);CHKERRQ(ierr); 8857 ierr = MatGetSize(mat,NULL,&m);CHKERRQ(ierr); 8858 if (m == n) { 8859 ierr = MatDiagonalScale(mat,NULL,diag);CHKERRQ(ierr); 8860 } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only supported for sequential matrices when no ghost points/periodic conditions"); 8861 } else { 8862 ierr = PetscUseMethod(mat,"MatDiagonalScaleLocal_C",(Mat,Vec),(mat,diag));CHKERRQ(ierr); 8863 } 8864 ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 8865 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 8866 PetscFunctionReturn(0); 8867 } 8868 8869 /*@ 8870 MatGetInertia - Gets the inertia from a factored matrix 8871 8872 Collective on Mat 8873 8874 Input Parameter: 8875 . mat - the matrix 8876 8877 Output Parameters: 8878 + nneg - number of negative eigenvalues 8879 . nzero - number of zero eigenvalues 8880 - npos - number of positive eigenvalues 8881 8882 Level: advanced 8883 8884 Notes: 8885 Matrix must have been factored by MatCholeskyFactor() 8886 8887 @*/ 8888 PetscErrorCode MatGetInertia(Mat mat,PetscInt *nneg,PetscInt *nzero,PetscInt *npos) 8889 { 8890 PetscErrorCode ierr; 8891 8892 PetscFunctionBegin; 8893 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8894 PetscValidType(mat,1); 8895 if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 8896 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Numeric factor mat is not assembled"); 8897 if (!mat->ops->getinertia) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 8898 ierr = (*mat->ops->getinertia)(mat,nneg,nzero,npos);CHKERRQ(ierr); 8899 PetscFunctionReturn(0); 8900 } 8901 8902 /* ----------------------------------------------------------------*/ 8903 /*@C 8904 MatSolves - Solves A x = b, given a factored matrix, for a collection of vectors 8905 8906 Neighbor-wise Collective on Mats 8907 8908 Input Parameters: 8909 + mat - the factored matrix 8910 - b - the right-hand-side vectors 8911 8912 Output Parameter: 8913 . x - the result vectors 8914 8915 Notes: 8916 The vectors b and x cannot be the same. I.e., one cannot 8917 call MatSolves(A,x,x). 8918 8919 Notes: 8920 Most users should employ the simplified KSP interface for linear solvers 8921 instead of working directly with matrix algebra routines such as this. 8922 See, e.g., KSPCreate(). 8923 8924 Level: developer 8925 8926 .seealso: MatSolveAdd(), MatSolveTranspose(), MatSolveTransposeAdd(), MatSolve() 8927 @*/ 8928 PetscErrorCode MatSolves(Mat mat,Vecs b,Vecs x) 8929 { 8930 PetscErrorCode ierr; 8931 8932 PetscFunctionBegin; 8933 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8934 PetscValidType(mat,1); 8935 if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 8936 if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 8937 if (!mat->rmap->N && !mat->cmap->N) PetscFunctionReturn(0); 8938 8939 if (!mat->ops->solves) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 8940 MatCheckPreallocated(mat,1); 8941 ierr = PetscLogEventBegin(MAT_Solves,mat,0,0,0);CHKERRQ(ierr); 8942 ierr = (*mat->ops->solves)(mat,b,x);CHKERRQ(ierr); 8943 ierr = PetscLogEventEnd(MAT_Solves,mat,0,0,0);CHKERRQ(ierr); 8944 PetscFunctionReturn(0); 8945 } 8946 8947 /*@ 8948 MatIsSymmetric - Test whether a matrix is symmetric 8949 8950 Collective on Mat 8951 8952 Input Parameter: 8953 + A - the matrix to test 8954 - tol - difference between value and its transpose less than this amount counts as equal (use 0.0 for exact transpose) 8955 8956 Output Parameters: 8957 . flg - the result 8958 8959 Notes: 8960 For real numbers MatIsSymmetric() and MatIsHermitian() return identical results 8961 8962 Level: intermediate 8963 8964 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetricKnown() 8965 @*/ 8966 PetscErrorCode MatIsSymmetric(Mat A,PetscReal tol,PetscBool *flg) 8967 { 8968 PetscErrorCode ierr; 8969 8970 PetscFunctionBegin; 8971 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 8972 PetscValidBoolPointer(flg,3); 8973 8974 if (!A->symmetric_set) { 8975 if (!A->ops->issymmetric) { 8976 MatType mattype; 8977 ierr = MatGetType(A,&mattype);CHKERRQ(ierr); 8978 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type %s does not support checking for symmetric",mattype); 8979 } 8980 ierr = (*A->ops->issymmetric)(A,tol,flg);CHKERRQ(ierr); 8981 if (!tol) { 8982 ierr = MatSetOption(A,MAT_SYMMETRIC,*flg);CHKERRQ(ierr); 8983 } 8984 } else if (A->symmetric) { 8985 *flg = PETSC_TRUE; 8986 } else if (!tol) { 8987 *flg = PETSC_FALSE; 8988 } else { 8989 if (!A->ops->issymmetric) { 8990 MatType mattype; 8991 ierr = MatGetType(A,&mattype);CHKERRQ(ierr); 8992 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type %s does not support checking for symmetric",mattype); 8993 } 8994 ierr = (*A->ops->issymmetric)(A,tol,flg);CHKERRQ(ierr); 8995 } 8996 PetscFunctionReturn(0); 8997 } 8998 8999 /*@ 9000 MatIsHermitian - Test whether a matrix is Hermitian 9001 9002 Collective on Mat 9003 9004 Input Parameter: 9005 + A - the matrix to test 9006 - tol - difference between value and its transpose less than this amount counts as equal (use 0.0 for exact Hermitian) 9007 9008 Output Parameters: 9009 . flg - the result 9010 9011 Level: intermediate 9012 9013 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), 9014 MatIsSymmetricKnown(), MatIsSymmetric() 9015 @*/ 9016 PetscErrorCode MatIsHermitian(Mat A,PetscReal tol,PetscBool *flg) 9017 { 9018 PetscErrorCode ierr; 9019 9020 PetscFunctionBegin; 9021 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 9022 PetscValidBoolPointer(flg,3); 9023 9024 if (!A->hermitian_set) { 9025 if (!A->ops->ishermitian) { 9026 MatType mattype; 9027 ierr = MatGetType(A,&mattype);CHKERRQ(ierr); 9028 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type %s does not support checking for hermitian",mattype); 9029 } 9030 ierr = (*A->ops->ishermitian)(A,tol,flg);CHKERRQ(ierr); 9031 if (!tol) { 9032 ierr = MatSetOption(A,MAT_HERMITIAN,*flg);CHKERRQ(ierr); 9033 } 9034 } else if (A->hermitian) { 9035 *flg = PETSC_TRUE; 9036 } else if (!tol) { 9037 *flg = PETSC_FALSE; 9038 } else { 9039 if (!A->ops->ishermitian) { 9040 MatType mattype; 9041 ierr = MatGetType(A,&mattype);CHKERRQ(ierr); 9042 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type %s does not support checking for hermitian",mattype); 9043 } 9044 ierr = (*A->ops->ishermitian)(A,tol,flg);CHKERRQ(ierr); 9045 } 9046 PetscFunctionReturn(0); 9047 } 9048 9049 /*@ 9050 MatIsSymmetricKnown - Checks the flag on the matrix to see if it is symmetric. 9051 9052 Not Collective 9053 9054 Input Parameter: 9055 . A - the matrix to check 9056 9057 Output Parameters: 9058 + set - if the symmetric flag is set (this tells you if the next flag is valid) 9059 - flg - the result 9060 9061 Level: advanced 9062 9063 Note: Does not check the matrix values directly, so this may return unknown (set = PETSC_FALSE). Use MatIsSymmetric() 9064 if you want it explicitly checked 9065 9066 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetric() 9067 @*/ 9068 PetscErrorCode MatIsSymmetricKnown(Mat A,PetscBool *set,PetscBool *flg) 9069 { 9070 PetscFunctionBegin; 9071 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 9072 PetscValidPointer(set,2); 9073 PetscValidBoolPointer(flg,3); 9074 if (A->symmetric_set) { 9075 *set = PETSC_TRUE; 9076 *flg = A->symmetric; 9077 } else { 9078 *set = PETSC_FALSE; 9079 } 9080 PetscFunctionReturn(0); 9081 } 9082 9083 /*@ 9084 MatIsHermitianKnown - Checks the flag on the matrix to see if it is hermitian. 9085 9086 Not Collective 9087 9088 Input Parameter: 9089 . A - the matrix to check 9090 9091 Output Parameters: 9092 + set - if the hermitian flag is set (this tells you if the next flag is valid) 9093 - flg - the result 9094 9095 Level: advanced 9096 9097 Note: Does not check the matrix values directly, so this may return unknown (set = PETSC_FALSE). Use MatIsHermitian() 9098 if you want it explicitly checked 9099 9100 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetric() 9101 @*/ 9102 PetscErrorCode MatIsHermitianKnown(Mat A,PetscBool *set,PetscBool *flg) 9103 { 9104 PetscFunctionBegin; 9105 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 9106 PetscValidPointer(set,2); 9107 PetscValidBoolPointer(flg,3); 9108 if (A->hermitian_set) { 9109 *set = PETSC_TRUE; 9110 *flg = A->hermitian; 9111 } else { 9112 *set = PETSC_FALSE; 9113 } 9114 PetscFunctionReturn(0); 9115 } 9116 9117 /*@ 9118 MatIsStructurallySymmetric - Test whether a matrix is structurally symmetric 9119 9120 Collective on Mat 9121 9122 Input Parameter: 9123 . A - the matrix to test 9124 9125 Output Parameters: 9126 . flg - the result 9127 9128 Level: intermediate 9129 9130 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsSymmetric(), MatSetOption() 9131 @*/ 9132 PetscErrorCode MatIsStructurallySymmetric(Mat A,PetscBool *flg) 9133 { 9134 PetscErrorCode ierr; 9135 9136 PetscFunctionBegin; 9137 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 9138 PetscValidBoolPointer(flg,2); 9139 if (!A->structurally_symmetric_set) { 9140 if (!A->ops->isstructurallysymmetric) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Matrix of type %s does not support checking for structural symmetric",((PetscObject)A)->type_name); 9141 ierr = (*A->ops->isstructurallysymmetric)(A,flg);CHKERRQ(ierr); 9142 ierr = MatSetOption(A,MAT_STRUCTURALLY_SYMMETRIC,*flg);CHKERRQ(ierr); 9143 } else *flg = A->structurally_symmetric; 9144 PetscFunctionReturn(0); 9145 } 9146 9147 /*@ 9148 MatStashGetInfo - Gets how many values are currently in the matrix stash, i.e. need 9149 to be communicated to other processors during the MatAssemblyBegin/End() process 9150 9151 Not collective 9152 9153 Input Parameter: 9154 . vec - the vector 9155 9156 Output Parameters: 9157 + nstash - the size of the stash 9158 . reallocs - the number of additional mallocs incurred. 9159 . bnstash - the size of the block stash 9160 - breallocs - the number of additional mallocs incurred.in the block stash 9161 9162 Level: advanced 9163 9164 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), Mat, MatStashSetInitialSize() 9165 9166 @*/ 9167 PetscErrorCode MatStashGetInfo(Mat mat,PetscInt *nstash,PetscInt *reallocs,PetscInt *bnstash,PetscInt *breallocs) 9168 { 9169 PetscErrorCode ierr; 9170 9171 PetscFunctionBegin; 9172 ierr = MatStashGetInfo_Private(&mat->stash,nstash,reallocs);CHKERRQ(ierr); 9173 ierr = MatStashGetInfo_Private(&mat->bstash,bnstash,breallocs);CHKERRQ(ierr); 9174 PetscFunctionReturn(0); 9175 } 9176 9177 /*@C 9178 MatCreateVecs - Get vector(s) compatible with the matrix, i.e. with the same 9179 parallel layout 9180 9181 Collective on Mat 9182 9183 Input Parameter: 9184 . mat - the matrix 9185 9186 Output Parameter: 9187 + right - (optional) vector that the matrix can be multiplied against 9188 - left - (optional) vector that the matrix vector product can be stored in 9189 9190 Notes: 9191 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(). 9192 9193 Notes: 9194 These are new vectors which are not owned by the Mat, they should be destroyed in VecDestroy() when no longer needed 9195 9196 Level: advanced 9197 9198 .seealso: MatCreate(), VecDestroy() 9199 @*/ 9200 PetscErrorCode MatCreateVecs(Mat mat,Vec *right,Vec *left) 9201 { 9202 PetscErrorCode ierr; 9203 9204 PetscFunctionBegin; 9205 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 9206 PetscValidType(mat,1); 9207 if (mat->ops->getvecs) { 9208 ierr = (*mat->ops->getvecs)(mat,right,left);CHKERRQ(ierr); 9209 } else { 9210 PetscInt rbs,cbs; 9211 ierr = MatGetBlockSizes(mat,&rbs,&cbs);CHKERRQ(ierr); 9212 if (right) { 9213 if (mat->cmap->n < 0) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"PetscLayout for columns not yet setup"); 9214 ierr = VecCreate(PetscObjectComm((PetscObject)mat),right);CHKERRQ(ierr); 9215 ierr = VecSetSizes(*right,mat->cmap->n,PETSC_DETERMINE);CHKERRQ(ierr); 9216 ierr = VecSetBlockSize(*right,cbs);CHKERRQ(ierr); 9217 ierr = VecSetType(*right,mat->defaultvectype);CHKERRQ(ierr); 9218 ierr = PetscLayoutReference(mat->cmap,&(*right)->map);CHKERRQ(ierr); 9219 } 9220 if (left) { 9221 if (mat->rmap->n < 0) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"PetscLayout for rows not yet setup"); 9222 ierr = VecCreate(PetscObjectComm((PetscObject)mat),left);CHKERRQ(ierr); 9223 ierr = VecSetSizes(*left,mat->rmap->n,PETSC_DETERMINE);CHKERRQ(ierr); 9224 ierr = VecSetBlockSize(*left,rbs);CHKERRQ(ierr); 9225 ierr = VecSetType(*left,mat->defaultvectype);CHKERRQ(ierr); 9226 ierr = PetscLayoutReference(mat->rmap,&(*left)->map);CHKERRQ(ierr); 9227 } 9228 } 9229 PetscFunctionReturn(0); 9230 } 9231 9232 /*@C 9233 MatFactorInfoInitialize - Initializes a MatFactorInfo data structure 9234 with default values. 9235 9236 Not Collective 9237 9238 Input Parameters: 9239 . info - the MatFactorInfo data structure 9240 9241 Notes: 9242 The solvers are generally used through the KSP and PC objects, for example 9243 PCLU, PCILU, PCCHOLESKY, PCICC 9244 9245 Level: developer 9246 9247 .seealso: MatFactorInfo 9248 9249 Developer Note: fortran interface is not autogenerated as the f90 9250 interface defintion cannot be generated correctly [due to MatFactorInfo] 9251 9252 @*/ 9253 9254 PetscErrorCode MatFactorInfoInitialize(MatFactorInfo *info) 9255 { 9256 PetscErrorCode ierr; 9257 9258 PetscFunctionBegin; 9259 ierr = PetscMemzero(info,sizeof(MatFactorInfo));CHKERRQ(ierr); 9260 PetscFunctionReturn(0); 9261 } 9262 9263 /*@ 9264 MatFactorSetSchurIS - Set indices corresponding to the Schur complement you wish to have computed 9265 9266 Collective on Mat 9267 9268 Input Parameters: 9269 + mat - the factored matrix 9270 - is - the index set defining the Schur indices (0-based) 9271 9272 Notes: 9273 Call MatFactorSolveSchurComplement() or MatFactorSolveSchurComplementTranspose() after this call to solve a Schur complement system. 9274 9275 You can call MatFactorGetSchurComplement() or MatFactorCreateSchurComplement() after this call. 9276 9277 Level: developer 9278 9279 .seealso: MatGetFactor(), MatFactorGetSchurComplement(), MatFactorRestoreSchurComplement(), MatFactorCreateSchurComplement(), MatFactorSolveSchurComplement(), 9280 MatFactorSolveSchurComplementTranspose(), MatFactorSolveSchurComplement() 9281 9282 @*/ 9283 PetscErrorCode MatFactorSetSchurIS(Mat mat,IS is) 9284 { 9285 PetscErrorCode ierr,(*f)(Mat,IS); 9286 9287 PetscFunctionBegin; 9288 PetscValidType(mat,1); 9289 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 9290 PetscValidType(is,2); 9291 PetscValidHeaderSpecific(is,IS_CLASSID,2); 9292 PetscCheckSameComm(mat,1,is,2); 9293 if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Only for factored matrix"); 9294 ierr = PetscObjectQueryFunction((PetscObject)mat,"MatFactorSetSchurIS_C",&f);CHKERRQ(ierr); 9295 if (!f) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"The selected MatSolverType does not support Schur complement computation. You should use MATSOLVERMUMPS or MATSOLVERMKL_PARDISO"); 9296 ierr = MatDestroy(&mat->schur);CHKERRQ(ierr); 9297 ierr = (*f)(mat,is);CHKERRQ(ierr); 9298 if (!mat->schur) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_PLIB,"Schur complement has not been created"); 9299 PetscFunctionReturn(0); 9300 } 9301 9302 /*@ 9303 MatFactorCreateSchurComplement - Create a Schur complement matrix object using Schur data computed during the factorization step 9304 9305 Logically Collective on Mat 9306 9307 Input Parameters: 9308 + F - the factored matrix obtained by calling MatGetFactor() from PETSc-MUMPS interface 9309 . S - location where to return the Schur complement, can be NULL 9310 - status - the status of the Schur complement matrix, can be NULL 9311 9312 Notes: 9313 You must call MatFactorSetSchurIS() before calling this routine. 9314 9315 The routine provides a copy of the Schur matrix stored within the solver data structures. 9316 The caller must destroy the object when it is no longer needed. 9317 If MatFactorInvertSchurComplement() has been called, the routine gets back the inverse. 9318 9319 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) 9320 9321 Developer Notes: 9322 The reason this routine exists is because the representation of the Schur complement within the factor matrix may be different than a standard PETSc 9323 matrix representation and we normally do not want to use the time or memory to make a copy as a regular PETSc matrix. 9324 9325 See MatCreateSchurComplement() or MatGetSchurComplement() for ways to create virtual or approximate Schur complements. 9326 9327 Level: advanced 9328 9329 References: 9330 9331 .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorGetSchurComplement(), MatFactorSchurStatus 9332 @*/ 9333 PetscErrorCode MatFactorCreateSchurComplement(Mat F,Mat* S,MatFactorSchurStatus* status) 9334 { 9335 PetscErrorCode ierr; 9336 9337 PetscFunctionBegin; 9338 PetscValidHeaderSpecific(F,MAT_CLASSID,1); 9339 if (S) PetscValidPointer(S,2); 9340 if (status) PetscValidPointer(status,3); 9341 if (S) { 9342 PetscErrorCode (*f)(Mat,Mat*); 9343 9344 ierr = PetscObjectQueryFunction((PetscObject)F,"MatFactorCreateSchurComplement_C",&f);CHKERRQ(ierr); 9345 if (f) { 9346 ierr = (*f)(F,S);CHKERRQ(ierr); 9347 } else { 9348 ierr = MatDuplicate(F->schur,MAT_COPY_VALUES,S);CHKERRQ(ierr); 9349 } 9350 } 9351 if (status) *status = F->schur_status; 9352 PetscFunctionReturn(0); 9353 } 9354 9355 /*@ 9356 MatFactorGetSchurComplement - Gets access to a Schur complement matrix using the current Schur data within a factored matrix 9357 9358 Logically Collective on Mat 9359 9360 Input Parameters: 9361 + F - the factored matrix obtained by calling MatGetFactor() 9362 . *S - location where to return the Schur complement, can be NULL 9363 - status - the status of the Schur complement matrix, can be NULL 9364 9365 Notes: 9366 You must call MatFactorSetSchurIS() before calling this routine. 9367 9368 Schur complement mode is currently implemented for sequential matrices. 9369 The routine returns a the Schur Complement stored within the data strutures of the solver. 9370 If MatFactorInvertSchurComplement() has previously been called, the returned matrix is actually the inverse of the Schur complement. 9371 The returned matrix should not be destroyed; the caller should call MatFactorRestoreSchurComplement() when the object is no longer needed. 9372 9373 Use MatFactorCreateSchurComplement() to create a copy of the Schur complement matrix that is within a factored matrix 9374 9375 See MatCreateSchurComplement() or MatGetSchurComplement() for ways to create virtual or approximate Schur complements. 9376 9377 Level: advanced 9378 9379 References: 9380 9381 .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorRestoreSchurComplement(), MatFactorCreateSchurComplement(), MatFactorSchurStatus 9382 @*/ 9383 PetscErrorCode MatFactorGetSchurComplement(Mat F,Mat* S,MatFactorSchurStatus* status) 9384 { 9385 PetscFunctionBegin; 9386 PetscValidHeaderSpecific(F,MAT_CLASSID,1); 9387 if (S) PetscValidPointer(S,2); 9388 if (status) PetscValidPointer(status,3); 9389 if (S) *S = F->schur; 9390 if (status) *status = F->schur_status; 9391 PetscFunctionReturn(0); 9392 } 9393 9394 /*@ 9395 MatFactorRestoreSchurComplement - Restore the Schur complement matrix object obtained from a call to MatFactorGetSchurComplement 9396 9397 Logically Collective on Mat 9398 9399 Input Parameters: 9400 + F - the factored matrix obtained by calling MatGetFactor() 9401 . *S - location where the Schur complement is stored 9402 - status - the status of the Schur complement matrix (see MatFactorSchurStatus) 9403 9404 Notes: 9405 9406 Level: advanced 9407 9408 References: 9409 9410 .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorRestoreSchurComplement(), MatFactorCreateSchurComplement(), MatFactorSchurStatus 9411 @*/ 9412 PetscErrorCode MatFactorRestoreSchurComplement(Mat F,Mat* S,MatFactorSchurStatus status) 9413 { 9414 PetscErrorCode ierr; 9415 9416 PetscFunctionBegin; 9417 PetscValidHeaderSpecific(F,MAT_CLASSID,1); 9418 if (S) { 9419 PetscValidHeaderSpecific(*S,MAT_CLASSID,2); 9420 *S = NULL; 9421 } 9422 F->schur_status = status; 9423 ierr = MatFactorUpdateSchurStatus_Private(F);CHKERRQ(ierr); 9424 PetscFunctionReturn(0); 9425 } 9426 9427 /*@ 9428 MatFactorSolveSchurComplementTranspose - Solve the transpose of the Schur complement system computed during the factorization step 9429 9430 Logically Collective on Mat 9431 9432 Input Parameters: 9433 + F - the factored matrix obtained by calling MatGetFactor() 9434 . rhs - location where the right hand side of the Schur complement system is stored 9435 - sol - location where the solution of the Schur complement system has to be returned 9436 9437 Notes: 9438 The sizes of the vectors should match the size of the Schur complement 9439 9440 Must be called after MatFactorSetSchurIS() 9441 9442 Level: advanced 9443 9444 References: 9445 9446 .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorSolveSchurComplement() 9447 @*/ 9448 PetscErrorCode MatFactorSolveSchurComplementTranspose(Mat F, Vec rhs, Vec sol) 9449 { 9450 PetscErrorCode ierr; 9451 9452 PetscFunctionBegin; 9453 PetscValidType(F,1); 9454 PetscValidType(rhs,2); 9455 PetscValidType(sol,3); 9456 PetscValidHeaderSpecific(F,MAT_CLASSID,1); 9457 PetscValidHeaderSpecific(rhs,VEC_CLASSID,2); 9458 PetscValidHeaderSpecific(sol,VEC_CLASSID,3); 9459 PetscCheckSameComm(F,1,rhs,2); 9460 PetscCheckSameComm(F,1,sol,3); 9461 ierr = MatFactorFactorizeSchurComplement(F);CHKERRQ(ierr); 9462 switch (F->schur_status) { 9463 case MAT_FACTOR_SCHUR_FACTORED: 9464 ierr = MatSolveTranspose(F->schur,rhs,sol);CHKERRQ(ierr); 9465 break; 9466 case MAT_FACTOR_SCHUR_INVERTED: 9467 ierr = MatMultTranspose(F->schur,rhs,sol);CHKERRQ(ierr); 9468 break; 9469 default: 9470 SETERRQ1(PetscObjectComm((PetscObject)F),PETSC_ERR_SUP,"Unhandled MatFactorSchurStatus %D",F->schur_status); 9471 } 9472 PetscFunctionReturn(0); 9473 } 9474 9475 /*@ 9476 MatFactorSolveSchurComplement - Solve the Schur complement system computed during the factorization step 9477 9478 Logically Collective on Mat 9479 9480 Input Parameters: 9481 + F - the factored matrix obtained by calling MatGetFactor() 9482 . rhs - location where the right hand side of the Schur complement system is stored 9483 - sol - location where the solution of the Schur complement system has to be returned 9484 9485 Notes: 9486 The sizes of the vectors should match the size of the Schur complement 9487 9488 Must be called after MatFactorSetSchurIS() 9489 9490 Level: advanced 9491 9492 References: 9493 9494 .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorSolveSchurComplementTranspose() 9495 @*/ 9496 PetscErrorCode MatFactorSolveSchurComplement(Mat F, Vec rhs, Vec sol) 9497 { 9498 PetscErrorCode ierr; 9499 9500 PetscFunctionBegin; 9501 PetscValidType(F,1); 9502 PetscValidType(rhs,2); 9503 PetscValidType(sol,3); 9504 PetscValidHeaderSpecific(F,MAT_CLASSID,1); 9505 PetscValidHeaderSpecific(rhs,VEC_CLASSID,2); 9506 PetscValidHeaderSpecific(sol,VEC_CLASSID,3); 9507 PetscCheckSameComm(F,1,rhs,2); 9508 PetscCheckSameComm(F,1,sol,3); 9509 ierr = MatFactorFactorizeSchurComplement(F);CHKERRQ(ierr); 9510 switch (F->schur_status) { 9511 case MAT_FACTOR_SCHUR_FACTORED: 9512 ierr = MatSolve(F->schur,rhs,sol);CHKERRQ(ierr); 9513 break; 9514 case MAT_FACTOR_SCHUR_INVERTED: 9515 ierr = MatMult(F->schur,rhs,sol);CHKERRQ(ierr); 9516 break; 9517 default: 9518 SETERRQ1(PetscObjectComm((PetscObject)F),PETSC_ERR_SUP,"Unhandled MatFactorSchurStatus %D",F->schur_status); 9519 } 9520 PetscFunctionReturn(0); 9521 } 9522 9523 /*@ 9524 MatFactorInvertSchurComplement - Invert the Schur complement matrix computed during the factorization step 9525 9526 Logically Collective on Mat 9527 9528 Input Parameters: 9529 . F - the factored matrix obtained by calling MatGetFactor() 9530 9531 Notes: 9532 Must be called after MatFactorSetSchurIS(). 9533 9534 Call MatFactorGetSchurComplement() or MatFactorCreateSchurComplement() AFTER this call to actually compute the inverse and get access to it. 9535 9536 Level: advanced 9537 9538 References: 9539 9540 .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorGetSchurComplement(), MatFactorCreateSchurComplement() 9541 @*/ 9542 PetscErrorCode MatFactorInvertSchurComplement(Mat F) 9543 { 9544 PetscErrorCode ierr; 9545 9546 PetscFunctionBegin; 9547 PetscValidType(F,1); 9548 PetscValidHeaderSpecific(F,MAT_CLASSID,1); 9549 if (F->schur_status == MAT_FACTOR_SCHUR_INVERTED) PetscFunctionReturn(0); 9550 ierr = MatFactorFactorizeSchurComplement(F);CHKERRQ(ierr); 9551 ierr = MatFactorInvertSchurComplement_Private(F);CHKERRQ(ierr); 9552 F->schur_status = MAT_FACTOR_SCHUR_INVERTED; 9553 PetscFunctionReturn(0); 9554 } 9555 9556 /*@ 9557 MatFactorFactorizeSchurComplement - Factorize the Schur complement matrix computed during the factorization step 9558 9559 Logically Collective on Mat 9560 9561 Input Parameters: 9562 . F - the factored matrix obtained by calling MatGetFactor() 9563 9564 Notes: 9565 Must be called after MatFactorSetSchurIS(). 9566 9567 Level: advanced 9568 9569 References: 9570 9571 .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorInvertSchurComplement() 9572 @*/ 9573 PetscErrorCode MatFactorFactorizeSchurComplement(Mat F) 9574 { 9575 PetscErrorCode ierr; 9576 9577 PetscFunctionBegin; 9578 PetscValidType(F,1); 9579 PetscValidHeaderSpecific(F,MAT_CLASSID,1); 9580 if (F->schur_status == MAT_FACTOR_SCHUR_INVERTED || F->schur_status == MAT_FACTOR_SCHUR_FACTORED) PetscFunctionReturn(0); 9581 ierr = MatFactorFactorizeSchurComplement_Private(F);CHKERRQ(ierr); 9582 F->schur_status = MAT_FACTOR_SCHUR_FACTORED; 9583 PetscFunctionReturn(0); 9584 } 9585 9586 /*@ 9587 MatPtAP - Creates the matrix product C = P^T * A * P 9588 9589 Neighbor-wise Collective on Mat 9590 9591 Input Parameters: 9592 + A - the matrix 9593 . P - the projection matrix 9594 . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 9595 - fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(P)), use PETSC_DEFAULT if you do not have a good estimate 9596 if the result is a dense matrix this is irrelevent 9597 9598 Output Parameters: 9599 . C - the product matrix 9600 9601 Notes: 9602 C will be created and must be destroyed by the user with MatDestroy(). 9603 9604 For matrix types without special implementation the function fallbacks to MatMatMult() followed by MatTransposeMatMult(). 9605 9606 Level: intermediate 9607 9608 .seealso: MatMatMult(), MatRARt() 9609 @*/ 9610 PetscErrorCode MatPtAP(Mat A,Mat P,MatReuse scall,PetscReal fill,Mat *C) 9611 { 9612 PetscErrorCode ierr; 9613 9614 PetscFunctionBegin; 9615 if (scall == MAT_REUSE_MATRIX) MatCheckProduct(*C,5); 9616 if (scall == MAT_INPLACE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported"); 9617 9618 if (scall == MAT_INITIAL_MATRIX) { 9619 ierr = MatProductCreate(A,P,NULL,C);CHKERRQ(ierr); 9620 ierr = MatProductSetType(*C,MATPRODUCT_PtAP);CHKERRQ(ierr); 9621 ierr = MatProductSetAlgorithm(*C,"default");CHKERRQ(ierr); 9622 ierr = MatProductSetFill(*C,fill);CHKERRQ(ierr); 9623 9624 (*C)->product->api_user = PETSC_TRUE; 9625 ierr = MatProductSetFromOptions(*C);CHKERRQ(ierr); 9626 if (!(*C)->ops->productsymbolic) SETERRQ3(PetscObjectComm((PetscObject)(*C)),PETSC_ERR_SUP,"MatProduct %s not supported for A %s and P %s",MatProductTypes[MATPRODUCT_PtAP],((PetscObject)A)->type_name,((PetscObject)P)->type_name); 9627 ierr = MatProductSymbolic(*C);CHKERRQ(ierr); 9628 } else { /* scall == MAT_REUSE_MATRIX */ 9629 ierr = MatProductReplaceMats(A,P,NULL,*C);CHKERRQ(ierr); 9630 } 9631 9632 ierr = MatProductNumeric(*C);CHKERRQ(ierr); 9633 if (A->symmetric_set && A->symmetric) { 9634 ierr = MatSetOption(*C,MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr); 9635 } 9636 PetscFunctionReturn(0); 9637 } 9638 9639 /*@ 9640 MatRARt - Creates the matrix product C = R * A * R^T 9641 9642 Neighbor-wise Collective on Mat 9643 9644 Input Parameters: 9645 + A - the matrix 9646 . R - the projection matrix 9647 . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 9648 - fill - expected fill as ratio of nnz(C)/nnz(A), use PETSC_DEFAULT if you do not have a good estimate 9649 if the result is a dense matrix this is irrelevent 9650 9651 Output Parameters: 9652 . C - the product matrix 9653 9654 Notes: 9655 C will be created and must be destroyed by the user with MatDestroy(). 9656 9657 This routine is currently only implemented for pairs of AIJ matrices and classes 9658 which inherit from AIJ. Due to PETSc sparse matrix block row distribution among processes, 9659 parallel MatRARt is implemented via explicit transpose of R, which could be very expensive. 9660 We recommend using MatPtAP(). 9661 9662 Level: intermediate 9663 9664 .seealso: MatMatMult(), MatPtAP() 9665 @*/ 9666 PetscErrorCode MatRARt(Mat A,Mat R,MatReuse scall,PetscReal fill,Mat *C) 9667 { 9668 PetscErrorCode ierr; 9669 9670 PetscFunctionBegin; 9671 if (scall == MAT_REUSE_MATRIX) MatCheckProduct(*C,5); 9672 if (scall == MAT_INPLACE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported"); 9673 9674 if (scall == MAT_INITIAL_MATRIX) { 9675 ierr = MatProductCreate(A,R,NULL,C);CHKERRQ(ierr); 9676 ierr = MatProductSetType(*C,MATPRODUCT_RARt);CHKERRQ(ierr); 9677 ierr = MatProductSetAlgorithm(*C,"default");CHKERRQ(ierr); 9678 ierr = MatProductSetFill(*C,fill);CHKERRQ(ierr); 9679 9680 (*C)->product->api_user = PETSC_TRUE; 9681 ierr = MatProductSetFromOptions(*C);CHKERRQ(ierr); 9682 if (!(*C)->ops->productsymbolic) SETERRQ3(PetscObjectComm((PetscObject)(*C)),PETSC_ERR_SUP,"MatProduct %s not supported for A %s and R %s",MatProductTypes[MATPRODUCT_RARt],((PetscObject)A)->type_name,((PetscObject)R)->type_name); 9683 ierr = MatProductSymbolic(*C);CHKERRQ(ierr); 9684 } else { /* scall == MAT_REUSE_MATRIX */ 9685 ierr = MatProductReplaceMats(A,R,NULL,*C);CHKERRQ(ierr); 9686 } 9687 9688 ierr = MatProductNumeric(*C);CHKERRQ(ierr); 9689 if (A->symmetric_set && A->symmetric) { 9690 ierr = MatSetOption(*C,MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr); 9691 } 9692 PetscFunctionReturn(0); 9693 } 9694 9695 static PetscErrorCode MatProduct_Private(Mat A,Mat B,MatReuse scall,PetscReal fill,MatProductType ptype, Mat *C) 9696 { 9697 PetscErrorCode ierr; 9698 9699 PetscFunctionBegin; 9700 if (scall == MAT_INPLACE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported"); 9701 9702 if (scall == MAT_INITIAL_MATRIX) { 9703 ierr = PetscInfo1(A,"Calling MatProduct API with MAT_INITIAL_MATRIX and product type %s\n",MatProductTypes[ptype]);CHKERRQ(ierr); 9704 ierr = MatProductCreate(A,B,NULL,C);CHKERRQ(ierr); 9705 ierr = MatProductSetType(*C,ptype);CHKERRQ(ierr); 9706 ierr = MatProductSetAlgorithm(*C,MATPRODUCTALGORITHM_DEFAULT);CHKERRQ(ierr); 9707 ierr = MatProductSetFill(*C,fill);CHKERRQ(ierr); 9708 9709 (*C)->product->api_user = PETSC_TRUE; 9710 ierr = MatProductSetFromOptions(*C);CHKERRQ(ierr); 9711 ierr = MatProductSymbolic(*C);CHKERRQ(ierr); 9712 } else { /* scall == MAT_REUSE_MATRIX */ 9713 Mat_Product *product = (*C)->product; 9714 9715 ierr = PetscInfo2(A,"Calling MatProduct API with MAT_REUSE_MATRIX %s product present and product type %s\n",product ? "with" : "without",MatProductTypes[ptype]);CHKERRQ(ierr); 9716 if (!product) { 9717 /* user provide the dense matrix *C without calling MatProductCreate() */ 9718 PetscBool isdense; 9719 9720 ierr = PetscObjectBaseTypeCompareAny((PetscObject)(*C),&isdense,MATSEQDENSE,MATMPIDENSE,"");CHKERRQ(ierr); 9721 if (isdense) { 9722 /* user wants to reuse an assembled dense matrix */ 9723 /* Create product -- see MatCreateProduct() */ 9724 ierr = MatProductCreate_Private(A,B,NULL,*C);CHKERRQ(ierr); 9725 product = (*C)->product; 9726 product->fill = fill; 9727 product->api_user = PETSC_TRUE; 9728 product->clear = PETSC_TRUE; 9729 9730 ierr = MatProductSetType(*C,ptype);CHKERRQ(ierr); 9731 ierr = MatProductSetFromOptions(*C);CHKERRQ(ierr); 9732 if (!(*C)->ops->productsymbolic) SETERRQ3(PetscObjectComm((PetscObject)(*C)),PETSC_ERR_SUP,"MatProduct %s not supported for %s and %s",MatProductTypes[ptype],((PetscObject)A)->type_name,((PetscObject)B)->type_name); 9733 ierr = MatProductSymbolic(*C);CHKERRQ(ierr); 9734 } else SETERRQ(PetscObjectComm((PetscObject)(*C)),PETSC_ERR_SUP,"Call MatProductCreate() first"); 9735 } else { /* user may change input matrices A or B when REUSE */ 9736 ierr = MatProductReplaceMats(A,B,NULL,*C);CHKERRQ(ierr); 9737 } 9738 } 9739 ierr = MatProductNumeric(*C);CHKERRQ(ierr); 9740 PetscFunctionReturn(0); 9741 } 9742 9743 /*@ 9744 MatMatMult - Performs Matrix-Matrix Multiplication C=A*B. 9745 9746 Neighbor-wise Collective on Mat 9747 9748 Input Parameters: 9749 + A - the left matrix 9750 . B - the right matrix 9751 . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 9752 - fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if you do not have a good estimate 9753 if the result is a dense matrix this is irrelevent 9754 9755 Output Parameters: 9756 . C - the product matrix 9757 9758 Notes: 9759 Unless scall is MAT_REUSE_MATRIX C will be created. 9760 9761 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 9762 call to this function with MAT_INITIAL_MATRIX. 9763 9764 To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value actually needed. 9765 9766 If you have many matrices with the same non-zero structure to multiply, you should use MatProductCreate()/MatProductSymbolic(C)/ReplaceMats(), and call MatProductNumeric() repeatedly. 9767 9768 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. 9769 9770 Level: intermediate 9771 9772 .seealso: MatTransposeMatMult(), MatMatTransposeMult(), MatPtAP() 9773 @*/ 9774 PetscErrorCode MatMatMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C) 9775 { 9776 PetscErrorCode ierr; 9777 9778 PetscFunctionBegin; 9779 ierr = MatProduct_Private(A,B,scall,fill,MATPRODUCT_AB,C);CHKERRQ(ierr); 9780 PetscFunctionReturn(0); 9781 } 9782 9783 /*@ 9784 MatMatTransposeMult - Performs Matrix-Matrix Multiplication C=A*B^T. 9785 9786 Neighbor-wise Collective on Mat 9787 9788 Input Parameters: 9789 + A - the left matrix 9790 . B - the right matrix 9791 . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 9792 - fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if not known 9793 9794 Output Parameters: 9795 . C - the product matrix 9796 9797 Notes: 9798 C will be created if MAT_INITIAL_MATRIX and must be destroyed by the user with MatDestroy(). 9799 9800 MAT_REUSE_MATRIX can only be used if the matrices A and B have the same nonzero pattern as in the previous call 9801 9802 To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value 9803 actually needed. 9804 9805 This routine is currently only implemented for pairs of SeqAIJ matrices, for the SeqDense class, 9806 and for pairs of MPIDense matrices. 9807 9808 Options Database Keys: 9809 . -matmattransmult_mpidense_mpidense_via {allgatherv,cyclic} - Choose between algorthims for MPIDense matrices: the 9810 first redundantly copies the transposed B matrix on each process and requiers O(log P) communication complexity; 9811 the second never stores more than one portion of the B matrix at a time by requires O(P) communication complexity. 9812 9813 Level: intermediate 9814 9815 .seealso: MatMatMult(), MatTransposeMatMult() MatPtAP() 9816 @*/ 9817 PetscErrorCode MatMatTransposeMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C) 9818 { 9819 PetscErrorCode ierr; 9820 9821 PetscFunctionBegin; 9822 ierr = MatProduct_Private(A,B,scall,fill,MATPRODUCT_ABt,C);CHKERRQ(ierr); 9823 PetscFunctionReturn(0); 9824 } 9825 9826 /*@ 9827 MatTransposeMatMult - Performs Matrix-Matrix Multiplication C=A^T*B. 9828 9829 Neighbor-wise Collective on Mat 9830 9831 Input Parameters: 9832 + A - the left matrix 9833 . B - the right matrix 9834 . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 9835 - fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if not known 9836 9837 Output Parameters: 9838 . C - the product matrix 9839 9840 Notes: 9841 C will be created if MAT_INITIAL_MATRIX and must be destroyed by the user with MatDestroy(). 9842 9843 MAT_REUSE_MATRIX can only be used if the matrices A and B have the same nonzero pattern as in the previous call. 9844 9845 To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value 9846 actually needed. 9847 9848 This routine is currently implemented for pairs of AIJ matrices and pairs of SeqDense matrices and classes 9849 which inherit from SeqAIJ. C will be of same type as the input matrices. 9850 9851 Level: intermediate 9852 9853 .seealso: MatMatMult(), MatMatTransposeMult(), MatPtAP() 9854 @*/ 9855 PetscErrorCode MatTransposeMatMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C) 9856 { 9857 PetscErrorCode ierr; 9858 9859 PetscFunctionBegin; 9860 ierr = MatProduct_Private(A,B,scall,fill,MATPRODUCT_AtB,C);CHKERRQ(ierr); 9861 PetscFunctionReturn(0); 9862 } 9863 9864 /*@ 9865 MatMatMatMult - Performs Matrix-Matrix-Matrix Multiplication D=A*B*C. 9866 9867 Neighbor-wise Collective on Mat 9868 9869 Input Parameters: 9870 + A - the left matrix 9871 . B - the middle matrix 9872 . C - the right matrix 9873 . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 9874 - 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 9875 if the result is a dense matrix this is irrelevent 9876 9877 Output Parameters: 9878 . D - the product matrix 9879 9880 Notes: 9881 Unless scall is MAT_REUSE_MATRIX D will be created. 9882 9883 MAT_REUSE_MATRIX can only be used if the matrices A, B and C have the same nonzero pattern as in the previous call 9884 9885 To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value 9886 actually needed. 9887 9888 If you have many matrices with the same non-zero structure to multiply, you 9889 should use MAT_REUSE_MATRIX in all calls but the first or 9890 9891 Level: intermediate 9892 9893 .seealso: MatMatMult, MatPtAP() 9894 @*/ 9895 PetscErrorCode MatMatMatMult(Mat A,Mat B,Mat C,MatReuse scall,PetscReal fill,Mat *D) 9896 { 9897 PetscErrorCode ierr; 9898 9899 PetscFunctionBegin; 9900 if (scall == MAT_REUSE_MATRIX) MatCheckProduct(*D,6); 9901 if (scall == MAT_INPLACE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported"); 9902 9903 if (scall == MAT_INITIAL_MATRIX) { 9904 ierr = MatProductCreate(A,B,C,D);CHKERRQ(ierr); 9905 ierr = MatProductSetType(*D,MATPRODUCT_ABC);CHKERRQ(ierr); 9906 ierr = MatProductSetAlgorithm(*D,"default");CHKERRQ(ierr); 9907 ierr = MatProductSetFill(*D,fill);CHKERRQ(ierr); 9908 9909 (*D)->product->api_user = PETSC_TRUE; 9910 ierr = MatProductSetFromOptions(*D);CHKERRQ(ierr); 9911 if (!(*D)->ops->productsymbolic) SETERRQ4(PetscObjectComm((PetscObject)(*D)),PETSC_ERR_SUP,"MatProduct %s not supported for A %s, B %s and C %s",MatProductTypes[MATPRODUCT_ABC],((PetscObject)A)->type_name,((PetscObject)B)->type_name,((PetscObject)C)->type_name); 9912 ierr = MatProductSymbolic(*D);CHKERRQ(ierr); 9913 } else { /* user may change input matrices when REUSE */ 9914 ierr = MatProductReplaceMats(A,B,C,*D);CHKERRQ(ierr); 9915 } 9916 ierr = MatProductNumeric(*D);CHKERRQ(ierr); 9917 PetscFunctionReturn(0); 9918 } 9919 9920 /*@ 9921 MatCreateRedundantMatrix - Create redundant matrices and put them into processors of subcommunicators. 9922 9923 Collective on Mat 9924 9925 Input Parameters: 9926 + mat - the matrix 9927 . nsubcomm - the number of subcommunicators (= number of redundant parallel or sequential matrices) 9928 . subcomm - MPI communicator split from the communicator where mat resides in (or MPI_COMM_NULL if nsubcomm is used) 9929 - reuse - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 9930 9931 Output Parameter: 9932 . matredundant - redundant matrix 9933 9934 Notes: 9935 MAT_REUSE_MATRIX can only be used when the nonzero structure of the 9936 original matrix has not changed from that last call to MatCreateRedundantMatrix(). 9937 9938 This routine creates the duplicated matrices in subcommunicators; you should NOT create them before 9939 calling it. 9940 9941 Level: advanced 9942 9943 .seealso: MatDestroy() 9944 @*/ 9945 PetscErrorCode MatCreateRedundantMatrix(Mat mat,PetscInt nsubcomm,MPI_Comm subcomm,MatReuse reuse,Mat *matredundant) 9946 { 9947 PetscErrorCode ierr; 9948 MPI_Comm comm; 9949 PetscMPIInt size; 9950 PetscInt mloc_sub,nloc_sub,rstart,rend,M=mat->rmap->N,N=mat->cmap->N,bs=mat->rmap->bs; 9951 Mat_Redundant *redund=NULL; 9952 PetscSubcomm psubcomm=NULL; 9953 MPI_Comm subcomm_in=subcomm; 9954 Mat *matseq; 9955 IS isrow,iscol; 9956 PetscBool newsubcomm=PETSC_FALSE; 9957 9958 PetscFunctionBegin; 9959 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 9960 if (nsubcomm && reuse == MAT_REUSE_MATRIX) { 9961 PetscValidPointer(*matredundant,5); 9962 PetscValidHeaderSpecific(*matredundant,MAT_CLASSID,5); 9963 } 9964 9965 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);CHKERRMPI(ierr); 9966 if (size == 1 || nsubcomm == 1) { 9967 if (reuse == MAT_INITIAL_MATRIX) { 9968 ierr = MatDuplicate(mat,MAT_COPY_VALUES,matredundant);CHKERRQ(ierr); 9969 } else { 9970 if (*matredundant == mat) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"MAT_REUSE_MATRIX means reuse the matrix passed in as the final argument, not the original matrix"); 9971 ierr = MatCopy(mat,*matredundant,SAME_NONZERO_PATTERN);CHKERRQ(ierr); 9972 } 9973 PetscFunctionReturn(0); 9974 } 9975 9976 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9977 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9978 MatCheckPreallocated(mat,1); 9979 9980 ierr = PetscLogEventBegin(MAT_RedundantMat,mat,0,0,0);CHKERRQ(ierr); 9981 if (subcomm_in == MPI_COMM_NULL && reuse == MAT_INITIAL_MATRIX) { /* get subcomm if user does not provide subcomm */ 9982 /* create psubcomm, then get subcomm */ 9983 ierr = PetscObjectGetComm((PetscObject)mat,&comm);CHKERRQ(ierr); 9984 ierr = MPI_Comm_size(comm,&size);CHKERRMPI(ierr); 9985 if (nsubcomm < 1 || nsubcomm > size) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"nsubcomm must between 1 and %D",size); 9986 9987 ierr = PetscSubcommCreate(comm,&psubcomm);CHKERRQ(ierr); 9988 ierr = PetscSubcommSetNumber(psubcomm,nsubcomm);CHKERRQ(ierr); 9989 ierr = PetscSubcommSetType(psubcomm,PETSC_SUBCOMM_CONTIGUOUS);CHKERRQ(ierr); 9990 ierr = PetscSubcommSetFromOptions(psubcomm);CHKERRQ(ierr); 9991 ierr = PetscCommDuplicate(PetscSubcommChild(psubcomm),&subcomm,NULL);CHKERRQ(ierr); 9992 newsubcomm = PETSC_TRUE; 9993 ierr = PetscSubcommDestroy(&psubcomm);CHKERRQ(ierr); 9994 } 9995 9996 /* get isrow, iscol and a local sequential matrix matseq[0] */ 9997 if (reuse == MAT_INITIAL_MATRIX) { 9998 mloc_sub = PETSC_DECIDE; 9999 nloc_sub = PETSC_DECIDE; 10000 if (bs < 1) { 10001 ierr = PetscSplitOwnership(subcomm,&mloc_sub,&M);CHKERRQ(ierr); 10002 ierr = PetscSplitOwnership(subcomm,&nloc_sub,&N);CHKERRQ(ierr); 10003 } else { 10004 ierr = PetscSplitOwnershipBlock(subcomm,bs,&mloc_sub,&M);CHKERRQ(ierr); 10005 ierr = PetscSplitOwnershipBlock(subcomm,bs,&nloc_sub,&N);CHKERRQ(ierr); 10006 } 10007 ierr = MPI_Scan(&mloc_sub,&rend,1,MPIU_INT,MPI_SUM,subcomm);CHKERRMPI(ierr); 10008 rstart = rend - mloc_sub; 10009 ierr = ISCreateStride(PETSC_COMM_SELF,mloc_sub,rstart,1,&isrow);CHKERRQ(ierr); 10010 ierr = ISCreateStride(PETSC_COMM_SELF,N,0,1,&iscol);CHKERRQ(ierr); 10011 } else { /* reuse == MAT_REUSE_MATRIX */ 10012 if (*matredundant == mat) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"MAT_REUSE_MATRIX means reuse the matrix passed in as the final argument, not the original matrix"); 10013 /* retrieve subcomm */ 10014 ierr = PetscObjectGetComm((PetscObject)(*matredundant),&subcomm);CHKERRQ(ierr); 10015 redund = (*matredundant)->redundant; 10016 isrow = redund->isrow; 10017 iscol = redund->iscol; 10018 matseq = redund->matseq; 10019 } 10020 ierr = MatCreateSubMatrices(mat,1,&isrow,&iscol,reuse,&matseq);CHKERRQ(ierr); 10021 10022 /* get matredundant over subcomm */ 10023 if (reuse == MAT_INITIAL_MATRIX) { 10024 ierr = MatCreateMPIMatConcatenateSeqMat(subcomm,matseq[0],nloc_sub,reuse,matredundant);CHKERRQ(ierr); 10025 10026 /* create a supporting struct and attach it to C for reuse */ 10027 ierr = PetscNewLog(*matredundant,&redund);CHKERRQ(ierr); 10028 (*matredundant)->redundant = redund; 10029 redund->isrow = isrow; 10030 redund->iscol = iscol; 10031 redund->matseq = matseq; 10032 if (newsubcomm) { 10033 redund->subcomm = subcomm; 10034 } else { 10035 redund->subcomm = MPI_COMM_NULL; 10036 } 10037 } else { 10038 ierr = MatCreateMPIMatConcatenateSeqMat(subcomm,matseq[0],PETSC_DECIDE,reuse,matredundant);CHKERRQ(ierr); 10039 } 10040 ierr = PetscLogEventEnd(MAT_RedundantMat,mat,0,0,0);CHKERRQ(ierr); 10041 PetscFunctionReturn(0); 10042 } 10043 10044 /*@C 10045 MatGetMultiProcBlock - Create multiple [bjacobi] 'parallel submatrices' from 10046 a given 'mat' object. Each submatrix can span multiple procs. 10047 10048 Collective on Mat 10049 10050 Input Parameters: 10051 + mat - the matrix 10052 . subcomm - the subcommunicator obtained by com_split(comm) 10053 - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 10054 10055 Output Parameter: 10056 . subMat - 'parallel submatrices each spans a given subcomm 10057 10058 Notes: 10059 The submatrix partition across processors is dictated by 'subComm' a 10060 communicator obtained by com_split(comm). The comm_split 10061 is not restriced to be grouped with consecutive original ranks. 10062 10063 Due the comm_split() usage, the parallel layout of the submatrices 10064 map directly to the layout of the original matrix [wrt the local 10065 row,col partitioning]. So the original 'DiagonalMat' naturally maps 10066 into the 'DiagonalMat' of the subMat, hence it is used directly from 10067 the subMat. However the offDiagMat looses some columns - and this is 10068 reconstructed with MatSetValues() 10069 10070 Level: advanced 10071 10072 .seealso: MatCreateSubMatrices() 10073 @*/ 10074 PetscErrorCode MatGetMultiProcBlock(Mat mat, MPI_Comm subComm, MatReuse scall,Mat *subMat) 10075 { 10076 PetscErrorCode ierr; 10077 PetscMPIInt commsize,subCommSize; 10078 10079 PetscFunctionBegin; 10080 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&commsize);CHKERRMPI(ierr); 10081 ierr = MPI_Comm_size(subComm,&subCommSize);CHKERRMPI(ierr); 10082 if (subCommSize > commsize) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"CommSize %D < SubCommZize %D",commsize,subCommSize); 10083 10084 if (scall == MAT_REUSE_MATRIX && *subMat == mat) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"MAT_REUSE_MATRIX means reuse the matrix passed in as the final argument, not the original matrix"); 10085 ierr = PetscLogEventBegin(MAT_GetMultiProcBlock,mat,0,0,0);CHKERRQ(ierr); 10086 ierr = (*mat->ops->getmultiprocblock)(mat,subComm,scall,subMat);CHKERRQ(ierr); 10087 ierr = PetscLogEventEnd(MAT_GetMultiProcBlock,mat,0,0,0);CHKERRQ(ierr); 10088 PetscFunctionReturn(0); 10089 } 10090 10091 /*@ 10092 MatGetLocalSubMatrix - Gets a reference to a submatrix specified in local numbering 10093 10094 Not Collective 10095 10096 Input Arguments: 10097 + mat - matrix to extract local submatrix from 10098 . isrow - local row indices for submatrix 10099 - iscol - local column indices for submatrix 10100 10101 Output Arguments: 10102 . submat - the submatrix 10103 10104 Level: intermediate 10105 10106 Notes: 10107 The submat should be returned with MatRestoreLocalSubMatrix(). 10108 10109 Depending on the format of mat, the returned submat may not implement MatMult(). Its communicator may be 10110 the same as mat, it may be PETSC_COMM_SELF, or some other subcomm of mat's. 10111 10112 The submat always implements MatSetValuesLocal(). If isrow and iscol have the same block size, then 10113 MatSetValuesBlockedLocal() will also be implemented. 10114 10115 The mat must have had a ISLocalToGlobalMapping provided to it with MatSetLocalToGlobalMapping(). Note that 10116 matrices obtained with DMCreateMatrix() generally already have the local to global mapping provided. 10117 10118 .seealso: MatRestoreLocalSubMatrix(), MatCreateLocalRef(), MatSetLocalToGlobalMapping() 10119 @*/ 10120 PetscErrorCode MatGetLocalSubMatrix(Mat mat,IS isrow,IS iscol,Mat *submat) 10121 { 10122 PetscErrorCode ierr; 10123 10124 PetscFunctionBegin; 10125 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10126 PetscValidHeaderSpecific(isrow,IS_CLASSID,2); 10127 PetscValidHeaderSpecific(iscol,IS_CLASSID,3); 10128 PetscCheckSameComm(isrow,2,iscol,3); 10129 PetscValidPointer(submat,4); 10130 if (!mat->rmap->mapping) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Matrix must have local to global mapping provided before this call"); 10131 10132 if (mat->ops->getlocalsubmatrix) { 10133 ierr = (*mat->ops->getlocalsubmatrix)(mat,isrow,iscol,submat);CHKERRQ(ierr); 10134 } else { 10135 ierr = MatCreateLocalRef(mat,isrow,iscol,submat);CHKERRQ(ierr); 10136 } 10137 PetscFunctionReturn(0); 10138 } 10139 10140 /*@ 10141 MatRestoreLocalSubMatrix - Restores a reference to a submatrix specified in local numbering 10142 10143 Not Collective 10144 10145 Input Arguments: 10146 mat - matrix to extract local submatrix from 10147 isrow - local row indices for submatrix 10148 iscol - local column indices for submatrix 10149 submat - the submatrix 10150 10151 Level: intermediate 10152 10153 .seealso: MatGetLocalSubMatrix() 10154 @*/ 10155 PetscErrorCode MatRestoreLocalSubMatrix(Mat mat,IS isrow,IS iscol,Mat *submat) 10156 { 10157 PetscErrorCode ierr; 10158 10159 PetscFunctionBegin; 10160 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10161 PetscValidHeaderSpecific(isrow,IS_CLASSID,2); 10162 PetscValidHeaderSpecific(iscol,IS_CLASSID,3); 10163 PetscCheckSameComm(isrow,2,iscol,3); 10164 PetscValidPointer(submat,4); 10165 if (*submat) { 10166 PetscValidHeaderSpecific(*submat,MAT_CLASSID,4); 10167 } 10168 10169 if (mat->ops->restorelocalsubmatrix) { 10170 ierr = (*mat->ops->restorelocalsubmatrix)(mat,isrow,iscol,submat);CHKERRQ(ierr); 10171 } else { 10172 ierr = MatDestroy(submat);CHKERRQ(ierr); 10173 } 10174 *submat = NULL; 10175 PetscFunctionReturn(0); 10176 } 10177 10178 /* --------------------------------------------------------*/ 10179 /*@ 10180 MatFindZeroDiagonals - Finds all the rows of a matrix that have zero or no diagonal entry in the matrix 10181 10182 Collective on Mat 10183 10184 Input Parameter: 10185 . mat - the matrix 10186 10187 Output Parameter: 10188 . is - if any rows have zero diagonals this contains the list of them 10189 10190 Level: developer 10191 10192 .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd() 10193 @*/ 10194 PetscErrorCode MatFindZeroDiagonals(Mat mat,IS *is) 10195 { 10196 PetscErrorCode ierr; 10197 10198 PetscFunctionBegin; 10199 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10200 PetscValidType(mat,1); 10201 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 10202 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 10203 10204 if (!mat->ops->findzerodiagonals) { 10205 Vec diag; 10206 const PetscScalar *a; 10207 PetscInt *rows; 10208 PetscInt rStart, rEnd, r, nrow = 0; 10209 10210 ierr = MatCreateVecs(mat, &diag, NULL);CHKERRQ(ierr); 10211 ierr = MatGetDiagonal(mat, diag);CHKERRQ(ierr); 10212 ierr = MatGetOwnershipRange(mat, &rStart, &rEnd);CHKERRQ(ierr); 10213 ierr = VecGetArrayRead(diag, &a);CHKERRQ(ierr); 10214 for (r = 0; r < rEnd-rStart; ++r) if (a[r] == 0.0) ++nrow; 10215 ierr = PetscMalloc1(nrow, &rows);CHKERRQ(ierr); 10216 nrow = 0; 10217 for (r = 0; r < rEnd-rStart; ++r) if (a[r] == 0.0) rows[nrow++] = r+rStart; 10218 ierr = VecRestoreArrayRead(diag, &a);CHKERRQ(ierr); 10219 ierr = VecDestroy(&diag);CHKERRQ(ierr); 10220 ierr = ISCreateGeneral(PetscObjectComm((PetscObject) mat), nrow, rows, PETSC_OWN_POINTER, is);CHKERRQ(ierr); 10221 } else { 10222 ierr = (*mat->ops->findzerodiagonals)(mat, is);CHKERRQ(ierr); 10223 } 10224 PetscFunctionReturn(0); 10225 } 10226 10227 /*@ 10228 MatFindOffBlockDiagonalEntries - Finds all the rows of a matrix that have entries outside of the main diagonal block (defined by the matrix block size) 10229 10230 Collective on Mat 10231 10232 Input Parameter: 10233 . mat - the matrix 10234 10235 Output Parameter: 10236 . is - contains the list of rows with off block diagonal entries 10237 10238 Level: developer 10239 10240 .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd() 10241 @*/ 10242 PetscErrorCode MatFindOffBlockDiagonalEntries(Mat mat,IS *is) 10243 { 10244 PetscErrorCode ierr; 10245 10246 PetscFunctionBegin; 10247 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10248 PetscValidType(mat,1); 10249 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 10250 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 10251 10252 if (!mat->ops->findoffblockdiagonalentries) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s does not have a find off block diagonal entries defined",((PetscObject)mat)->type_name); 10253 ierr = (*mat->ops->findoffblockdiagonalentries)(mat,is);CHKERRQ(ierr); 10254 PetscFunctionReturn(0); 10255 } 10256 10257 /*@C 10258 MatInvertBlockDiagonal - Inverts the block diagonal entries. 10259 10260 Collective on Mat 10261 10262 Input Parameters: 10263 . mat - the matrix 10264 10265 Output Parameters: 10266 . values - the block inverses in column major order (FORTRAN-like) 10267 10268 Note: 10269 This routine is not available from Fortran. 10270 10271 Level: advanced 10272 10273 .seealso: MatInvertBockDiagonalMat 10274 @*/ 10275 PetscErrorCode MatInvertBlockDiagonal(Mat mat,const PetscScalar **values) 10276 { 10277 PetscErrorCode ierr; 10278 10279 PetscFunctionBegin; 10280 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10281 if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 10282 if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 10283 if (!mat->ops->invertblockdiagonal) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported for type %s",((PetscObject)mat)->type_name); 10284 ierr = (*mat->ops->invertblockdiagonal)(mat,values);CHKERRQ(ierr); 10285 PetscFunctionReturn(0); 10286 } 10287 10288 /*@C 10289 MatInvertVariableBlockDiagonal - Inverts the block diagonal entries. 10290 10291 Collective on Mat 10292 10293 Input Parameters: 10294 + mat - the matrix 10295 . nblocks - the number of blocks 10296 - bsizes - the size of each block 10297 10298 Output Parameters: 10299 . values - the block inverses in column major order (FORTRAN-like) 10300 10301 Note: 10302 This routine is not available from Fortran. 10303 10304 Level: advanced 10305 10306 .seealso: MatInvertBockDiagonal() 10307 @*/ 10308 PetscErrorCode MatInvertVariableBlockDiagonal(Mat mat,PetscInt nblocks,const PetscInt *bsizes,PetscScalar *values) 10309 { 10310 PetscErrorCode ierr; 10311 10312 PetscFunctionBegin; 10313 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10314 if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 10315 if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 10316 if (!mat->ops->invertvariableblockdiagonal) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported for type",((PetscObject)mat)->type_name); 10317 ierr = (*mat->ops->invertvariableblockdiagonal)(mat,nblocks,bsizes,values);CHKERRQ(ierr); 10318 PetscFunctionReturn(0); 10319 } 10320 10321 /*@ 10322 MatInvertBlockDiagonalMat - set matrix C to be the inverted block diagonal of matrix A 10323 10324 Collective on Mat 10325 10326 Input Parameters: 10327 . A - the matrix 10328 10329 Output Parameters: 10330 . C - matrix with inverted block diagonal of A. This matrix should be created and may have its type set. 10331 10332 Notes: the blocksize of the matrix is used to determine the blocks on the diagonal of C 10333 10334 Level: advanced 10335 10336 .seealso: MatInvertBockDiagonal() 10337 @*/ 10338 PetscErrorCode MatInvertBlockDiagonalMat(Mat A,Mat C) 10339 { 10340 PetscErrorCode ierr; 10341 const PetscScalar *vals; 10342 PetscInt *dnnz; 10343 PetscInt M,N,m,n,rstart,rend,bs,i,j; 10344 10345 PetscFunctionBegin; 10346 ierr = MatInvertBlockDiagonal(A,&vals);CHKERRQ(ierr); 10347 ierr = MatGetBlockSize(A,&bs);CHKERRQ(ierr); 10348 ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr); 10349 ierr = MatGetLocalSize(A,&m,&n);CHKERRQ(ierr); 10350 ierr = MatSetSizes(C,m,n,M,N);CHKERRQ(ierr); 10351 ierr = MatSetBlockSize(C,bs);CHKERRQ(ierr); 10352 ierr = PetscMalloc1(m/bs,&dnnz);CHKERRQ(ierr); 10353 for (j = 0; j < m/bs; j++) dnnz[j] = 1; 10354 ierr = MatXAIJSetPreallocation(C,bs,dnnz,NULL,NULL,NULL);CHKERRQ(ierr); 10355 ierr = PetscFree(dnnz);CHKERRQ(ierr); 10356 ierr = MatGetOwnershipRange(C,&rstart,&rend);CHKERRQ(ierr); 10357 ierr = MatSetOption(C,MAT_ROW_ORIENTED,PETSC_FALSE);CHKERRQ(ierr); 10358 for (i = rstart/bs; i < rend/bs; i++) { 10359 ierr = MatSetValuesBlocked(C,1,&i,1,&i,&vals[(i-rstart/bs)*bs*bs],INSERT_VALUES);CHKERRQ(ierr); 10360 } 10361 ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 10362 ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 10363 ierr = MatSetOption(C,MAT_ROW_ORIENTED,PETSC_TRUE);CHKERRQ(ierr); 10364 PetscFunctionReturn(0); 10365 } 10366 10367 /*@C 10368 MatTransposeColoringDestroy - Destroys a coloring context for matrix product C=A*B^T that was created 10369 via MatTransposeColoringCreate(). 10370 10371 Collective on MatTransposeColoring 10372 10373 Input Parameter: 10374 . c - coloring context 10375 10376 Level: intermediate 10377 10378 .seealso: MatTransposeColoringCreate() 10379 @*/ 10380 PetscErrorCode MatTransposeColoringDestroy(MatTransposeColoring *c) 10381 { 10382 PetscErrorCode ierr; 10383 MatTransposeColoring matcolor=*c; 10384 10385 PetscFunctionBegin; 10386 if (!matcolor) PetscFunctionReturn(0); 10387 if (--((PetscObject)matcolor)->refct > 0) {matcolor = NULL; PetscFunctionReturn(0);} 10388 10389 ierr = PetscFree3(matcolor->ncolumns,matcolor->nrows,matcolor->colorforrow);CHKERRQ(ierr); 10390 ierr = PetscFree(matcolor->rows);CHKERRQ(ierr); 10391 ierr = PetscFree(matcolor->den2sp);CHKERRQ(ierr); 10392 ierr = PetscFree(matcolor->colorforcol);CHKERRQ(ierr); 10393 ierr = PetscFree(matcolor->columns);CHKERRQ(ierr); 10394 if (matcolor->brows>0) { 10395 ierr = PetscFree(matcolor->lstart);CHKERRQ(ierr); 10396 } 10397 ierr = PetscHeaderDestroy(c);CHKERRQ(ierr); 10398 PetscFunctionReturn(0); 10399 } 10400 10401 /*@C 10402 MatTransColoringApplySpToDen - Given a symbolic matrix product C=A*B^T for which 10403 a MatTransposeColoring context has been created, computes a dense B^T by Apply 10404 MatTransposeColoring to sparse B. 10405 10406 Collective on MatTransposeColoring 10407 10408 Input Parameters: 10409 + B - sparse matrix B 10410 . Btdense - symbolic dense matrix B^T 10411 - coloring - coloring context created with MatTransposeColoringCreate() 10412 10413 Output Parameter: 10414 . Btdense - dense matrix B^T 10415 10416 Level: advanced 10417 10418 Notes: 10419 These are used internally for some implementations of MatRARt() 10420 10421 .seealso: MatTransposeColoringCreate(), MatTransposeColoringDestroy(), MatTransColoringApplyDenToSp() 10422 10423 @*/ 10424 PetscErrorCode MatTransColoringApplySpToDen(MatTransposeColoring coloring,Mat B,Mat Btdense) 10425 { 10426 PetscErrorCode ierr; 10427 10428 PetscFunctionBegin; 10429 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 10430 PetscValidHeaderSpecific(Btdense,MAT_CLASSID,3); 10431 PetscValidHeaderSpecific(coloring,MAT_TRANSPOSECOLORING_CLASSID,1); 10432 10433 if (!B->ops->transcoloringapplysptoden) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported for this matrix type %s",((PetscObject)B)->type_name); 10434 ierr = (B->ops->transcoloringapplysptoden)(coloring,B,Btdense);CHKERRQ(ierr); 10435 PetscFunctionReturn(0); 10436 } 10437 10438 /*@C 10439 MatTransColoringApplyDenToSp - Given a symbolic matrix product Csp=A*B^T for which 10440 a MatTransposeColoring context has been created and a dense matrix Cden=A*Btdense 10441 in which Btdens is obtained from MatTransColoringApplySpToDen(), recover sparse matrix 10442 Csp from Cden. 10443 10444 Collective on MatTransposeColoring 10445 10446 Input Parameters: 10447 + coloring - coloring context created with MatTransposeColoringCreate() 10448 - Cden - matrix product of a sparse matrix and a dense matrix Btdense 10449 10450 Output Parameter: 10451 . Csp - sparse matrix 10452 10453 Level: advanced 10454 10455 Notes: 10456 These are used internally for some implementations of MatRARt() 10457 10458 .seealso: MatTransposeColoringCreate(), MatTransposeColoringDestroy(), MatTransColoringApplySpToDen() 10459 10460 @*/ 10461 PetscErrorCode MatTransColoringApplyDenToSp(MatTransposeColoring matcoloring,Mat Cden,Mat Csp) 10462 { 10463 PetscErrorCode ierr; 10464 10465 PetscFunctionBegin; 10466 PetscValidHeaderSpecific(matcoloring,MAT_TRANSPOSECOLORING_CLASSID,1); 10467 PetscValidHeaderSpecific(Cden,MAT_CLASSID,2); 10468 PetscValidHeaderSpecific(Csp,MAT_CLASSID,3); 10469 10470 if (!Csp->ops->transcoloringapplydentosp) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported for this matrix type %s",((PetscObject)Csp)->type_name); 10471 ierr = (Csp->ops->transcoloringapplydentosp)(matcoloring,Cden,Csp);CHKERRQ(ierr); 10472 ierr = MatAssemblyBegin(Csp,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 10473 ierr = MatAssemblyEnd(Csp,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 10474 PetscFunctionReturn(0); 10475 } 10476 10477 /*@C 10478 MatTransposeColoringCreate - Creates a matrix coloring context for matrix product C=A*B^T. 10479 10480 Collective on Mat 10481 10482 Input Parameters: 10483 + mat - the matrix product C 10484 - iscoloring - the coloring of the matrix; usually obtained with MatColoringCreate() or DMCreateColoring() 10485 10486 Output Parameter: 10487 . color - the new coloring context 10488 10489 Level: intermediate 10490 10491 .seealso: MatTransposeColoringDestroy(), MatTransColoringApplySpToDen(), 10492 MatTransColoringApplyDenToSp() 10493 @*/ 10494 PetscErrorCode MatTransposeColoringCreate(Mat mat,ISColoring iscoloring,MatTransposeColoring *color) 10495 { 10496 MatTransposeColoring c; 10497 MPI_Comm comm; 10498 PetscErrorCode ierr; 10499 10500 PetscFunctionBegin; 10501 ierr = PetscLogEventBegin(MAT_TransposeColoringCreate,mat,0,0,0);CHKERRQ(ierr); 10502 ierr = PetscObjectGetComm((PetscObject)mat,&comm);CHKERRQ(ierr); 10503 ierr = PetscHeaderCreate(c,MAT_TRANSPOSECOLORING_CLASSID,"MatTransposeColoring","Matrix product C=A*B^T via coloring","Mat",comm,MatTransposeColoringDestroy,NULL);CHKERRQ(ierr); 10504 10505 c->ctype = iscoloring->ctype; 10506 if (mat->ops->transposecoloringcreate) { 10507 ierr = (*mat->ops->transposecoloringcreate)(mat,iscoloring,c);CHKERRQ(ierr); 10508 } else SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Code not yet written for matrix type %s",((PetscObject)mat)->type_name); 10509 10510 *color = c; 10511 ierr = PetscLogEventEnd(MAT_TransposeColoringCreate,mat,0,0,0);CHKERRQ(ierr); 10512 PetscFunctionReturn(0); 10513 } 10514 10515 /*@ 10516 MatGetNonzeroState - Returns a 64 bit integer representing the current state of nonzeros in the matrix. If the 10517 matrix has had no new nonzero locations added to the matrix since the previous call then the value will be the 10518 same, otherwise it will be larger 10519 10520 Not Collective 10521 10522 Input Parameter: 10523 . A - the matrix 10524 10525 Output Parameter: 10526 . state - the current state 10527 10528 Notes: 10529 You can only compare states from two different calls to the SAME matrix, you cannot compare calls between 10530 different matrices 10531 10532 Level: intermediate 10533 10534 @*/ 10535 PetscErrorCode MatGetNonzeroState(Mat mat,PetscObjectState *state) 10536 { 10537 PetscFunctionBegin; 10538 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10539 *state = mat->nonzerostate; 10540 PetscFunctionReturn(0); 10541 } 10542 10543 /*@ 10544 MatCreateMPIMatConcatenateSeqMat - Creates a single large PETSc matrix by concatenating sequential 10545 matrices from each processor 10546 10547 Collective 10548 10549 Input Parameters: 10550 + comm - the communicators the parallel matrix will live on 10551 . seqmat - the input sequential matrices 10552 . n - number of local columns (or PETSC_DECIDE) 10553 - reuse - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 10554 10555 Output Parameter: 10556 . mpimat - the parallel matrix generated 10557 10558 Level: advanced 10559 10560 Notes: 10561 The number of columns of the matrix in EACH processor MUST be the same. 10562 10563 @*/ 10564 PetscErrorCode MatCreateMPIMatConcatenateSeqMat(MPI_Comm comm,Mat seqmat,PetscInt n,MatReuse reuse,Mat *mpimat) 10565 { 10566 PetscErrorCode ierr; 10567 10568 PetscFunctionBegin; 10569 if (!seqmat->ops->creatempimatconcatenateseqmat) SETERRQ1(PetscObjectComm((PetscObject)seqmat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)seqmat)->type_name); 10570 if (reuse == MAT_REUSE_MATRIX && seqmat == *mpimat) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"MAT_REUSE_MATRIX means reuse the matrix passed in as the final argument, not the original matrix"); 10571 10572 ierr = PetscLogEventBegin(MAT_Merge,seqmat,0,0,0);CHKERRQ(ierr); 10573 ierr = (*seqmat->ops->creatempimatconcatenateseqmat)(comm,seqmat,n,reuse,mpimat);CHKERRQ(ierr); 10574 ierr = PetscLogEventEnd(MAT_Merge,seqmat,0,0,0);CHKERRQ(ierr); 10575 PetscFunctionReturn(0); 10576 } 10577 10578 /*@ 10579 MatSubdomainsCreateCoalesce - Creates index subdomains by coalescing adjacent 10580 ranks' ownership ranges. 10581 10582 Collective on A 10583 10584 Input Parameters: 10585 + A - the matrix to create subdomains from 10586 - N - requested number of subdomains 10587 10588 Output Parameters: 10589 + n - number of subdomains resulting on this rank 10590 - iss - IS list with indices of subdomains on this rank 10591 10592 Level: advanced 10593 10594 Notes: 10595 number of subdomains must be smaller than the communicator size 10596 @*/ 10597 PetscErrorCode MatSubdomainsCreateCoalesce(Mat A,PetscInt N,PetscInt *n,IS *iss[]) 10598 { 10599 MPI_Comm comm,subcomm; 10600 PetscMPIInt size,rank,color; 10601 PetscInt rstart,rend,k; 10602 PetscErrorCode ierr; 10603 10604 PetscFunctionBegin; 10605 ierr = PetscObjectGetComm((PetscObject)A,&comm);CHKERRQ(ierr); 10606 ierr = MPI_Comm_size(comm,&size);CHKERRMPI(ierr); 10607 ierr = MPI_Comm_rank(comm,&rank);CHKERRMPI(ierr); 10608 if (N < 1 || N >= (PetscInt)size) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"number of subdomains must be > 0 and < %D, got N = %D",size,N); 10609 *n = 1; 10610 k = ((PetscInt)size)/N + ((PetscInt)size%N>0); /* There are up to k ranks to a color */ 10611 color = rank/k; 10612 ierr = MPI_Comm_split(comm,color,rank,&subcomm);CHKERRMPI(ierr); 10613 ierr = PetscMalloc1(1,iss);CHKERRQ(ierr); 10614 ierr = MatGetOwnershipRange(A,&rstart,&rend);CHKERRQ(ierr); 10615 ierr = ISCreateStride(subcomm,rend-rstart,rstart,1,iss[0]);CHKERRQ(ierr); 10616 ierr = MPI_Comm_free(&subcomm);CHKERRMPI(ierr); 10617 PetscFunctionReturn(0); 10618 } 10619 10620 /*@ 10621 MatGalerkin - Constructs the coarse grid problem via Galerkin projection. 10622 10623 If the interpolation and restriction operators are the same, uses MatPtAP. 10624 If they are not the same, use MatMatMatMult. 10625 10626 Once the coarse grid problem is constructed, correct for interpolation operators 10627 that are not of full rank, which can legitimately happen in the case of non-nested 10628 geometric multigrid. 10629 10630 Input Parameters: 10631 + restrct - restriction operator 10632 . dA - fine grid matrix 10633 . interpolate - interpolation operator 10634 . reuse - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 10635 - fill - expected fill, use PETSC_DEFAULT if you do not have a good estimate 10636 10637 Output Parameters: 10638 . A - the Galerkin coarse matrix 10639 10640 Options Database Key: 10641 . -pc_mg_galerkin <both,pmat,mat,none> 10642 10643 Level: developer 10644 10645 .seealso: MatPtAP(), MatMatMatMult() 10646 @*/ 10647 PetscErrorCode MatGalerkin(Mat restrct, Mat dA, Mat interpolate, MatReuse reuse, PetscReal fill, Mat *A) 10648 { 10649 PetscErrorCode ierr; 10650 IS zerorows; 10651 Vec diag; 10652 10653 PetscFunctionBegin; 10654 if (reuse == MAT_INPLACE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported"); 10655 /* Construct the coarse grid matrix */ 10656 if (interpolate == restrct) { 10657 ierr = MatPtAP(dA,interpolate,reuse,fill,A);CHKERRQ(ierr); 10658 } else { 10659 ierr = MatMatMatMult(restrct,dA,interpolate,reuse,fill,A);CHKERRQ(ierr); 10660 } 10661 10662 /* If the interpolation matrix is not of full rank, A will have zero rows. 10663 This can legitimately happen in the case of non-nested geometric multigrid. 10664 In that event, we set the rows of the matrix to the rows of the identity, 10665 ignoring the equations (as the RHS will also be zero). */ 10666 10667 ierr = MatFindZeroRows(*A, &zerorows);CHKERRQ(ierr); 10668 10669 if (zerorows != NULL) { /* if there are any zero rows */ 10670 ierr = MatCreateVecs(*A, &diag, NULL);CHKERRQ(ierr); 10671 ierr = MatGetDiagonal(*A, diag);CHKERRQ(ierr); 10672 ierr = VecISSet(diag, zerorows, 1.0);CHKERRQ(ierr); 10673 ierr = MatDiagonalSet(*A, diag, INSERT_VALUES);CHKERRQ(ierr); 10674 ierr = VecDestroy(&diag);CHKERRQ(ierr); 10675 ierr = ISDestroy(&zerorows);CHKERRQ(ierr); 10676 } 10677 PetscFunctionReturn(0); 10678 } 10679 10680 /*@C 10681 MatSetOperation - Allows user to set a matrix operation for any matrix type 10682 10683 Logically Collective on Mat 10684 10685 Input Parameters: 10686 + mat - the matrix 10687 . op - the name of the operation 10688 - f - the function that provides the operation 10689 10690 Level: developer 10691 10692 Usage: 10693 $ extern PetscErrorCode usermult(Mat,Vec,Vec); 10694 $ ierr = MatCreateXXX(comm,...&A); 10695 $ ierr = MatSetOperation(A,MATOP_MULT,(void(*)(void))usermult); 10696 10697 Notes: 10698 See the file include/petscmat.h for a complete list of matrix 10699 operations, which all have the form MATOP_<OPERATION>, where 10700 <OPERATION> is the name (in all capital letters) of the 10701 user interface routine (e.g., MatMult() -> MATOP_MULT). 10702 10703 All user-provided functions (except for MATOP_DESTROY) should have the same calling 10704 sequence as the usual matrix interface routines, since they 10705 are intended to be accessed via the usual matrix interface 10706 routines, e.g., 10707 $ MatMult(Mat,Vec,Vec) -> usermult(Mat,Vec,Vec) 10708 10709 In particular each function MUST return an error code of 0 on success and 10710 nonzero on failure. 10711 10712 This routine is distinct from MatShellSetOperation() in that it can be called on any matrix type. 10713 10714 .seealso: MatGetOperation(), MatCreateShell(), MatShellSetContext(), MatShellSetOperation() 10715 @*/ 10716 PetscErrorCode MatSetOperation(Mat mat,MatOperation op,void (*f)(void)) 10717 { 10718 PetscFunctionBegin; 10719 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10720 if (op == MATOP_VIEW && !mat->ops->viewnative && f != (void (*)(void))(mat->ops->view)) { 10721 mat->ops->viewnative = mat->ops->view; 10722 } 10723 (((void(**)(void))mat->ops)[op]) = f; 10724 PetscFunctionReturn(0); 10725 } 10726 10727 /*@C 10728 MatGetOperation - Gets a matrix operation for any matrix type. 10729 10730 Not Collective 10731 10732 Input Parameters: 10733 + mat - the matrix 10734 - op - the name of the operation 10735 10736 Output Parameter: 10737 . f - the function that provides the operation 10738 10739 Level: developer 10740 10741 Usage: 10742 $ PetscErrorCode (*usermult)(Mat,Vec,Vec); 10743 $ ierr = MatGetOperation(A,MATOP_MULT,(void(**)(void))&usermult); 10744 10745 Notes: 10746 See the file include/petscmat.h for a complete list of matrix 10747 operations, which all have the form MATOP_<OPERATION>, where 10748 <OPERATION> is the name (in all capital letters) of the 10749 user interface routine (e.g., MatMult() -> MATOP_MULT). 10750 10751 This routine is distinct from MatShellGetOperation() in that it can be called on any matrix type. 10752 10753 .seealso: MatSetOperation(), MatCreateShell(), MatShellGetContext(), MatShellGetOperation() 10754 @*/ 10755 PetscErrorCode MatGetOperation(Mat mat,MatOperation op,void(**f)(void)) 10756 { 10757 PetscFunctionBegin; 10758 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10759 *f = (((void (**)(void))mat->ops)[op]); 10760 PetscFunctionReturn(0); 10761 } 10762 10763 /*@ 10764 MatHasOperation - Determines whether the given matrix supports the particular 10765 operation. 10766 10767 Not Collective 10768 10769 Input Parameters: 10770 + mat - the matrix 10771 - op - the operation, for example, MATOP_GET_DIAGONAL 10772 10773 Output Parameter: 10774 . has - either PETSC_TRUE or PETSC_FALSE 10775 10776 Level: advanced 10777 10778 Notes: 10779 See the file include/petscmat.h for a complete list of matrix 10780 operations, which all have the form MATOP_<OPERATION>, where 10781 <OPERATION> is the name (in all capital letters) of the 10782 user-level routine. E.g., MatNorm() -> MATOP_NORM. 10783 10784 .seealso: MatCreateShell() 10785 @*/ 10786 PetscErrorCode MatHasOperation(Mat mat,MatOperation op,PetscBool *has) 10787 { 10788 PetscErrorCode ierr; 10789 10790 PetscFunctionBegin; 10791 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10792 /* symbolic product can be set before matrix type */ 10793 if (op != MATOP_PRODUCTSYMBOLIC) PetscValidType(mat,1); 10794 PetscValidPointer(has,3); 10795 if (mat->ops->hasoperation) { 10796 ierr = (*mat->ops->hasoperation)(mat,op,has);CHKERRQ(ierr); 10797 } else { 10798 if (((void**)mat->ops)[op]) *has = PETSC_TRUE; 10799 else { 10800 *has = PETSC_FALSE; 10801 if (op == MATOP_CREATE_SUBMATRIX) { 10802 PetscMPIInt size; 10803 10804 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);CHKERRMPI(ierr); 10805 if (size == 1) { 10806 ierr = MatHasOperation(mat,MATOP_CREATE_SUBMATRICES,has);CHKERRQ(ierr); 10807 } 10808 } 10809 } 10810 } 10811 PetscFunctionReturn(0); 10812 } 10813 10814 /*@ 10815 MatHasCongruentLayouts - Determines whether the rows and columns layouts 10816 of the matrix are congruent 10817 10818 Collective on mat 10819 10820 Input Parameters: 10821 . mat - the matrix 10822 10823 Output Parameter: 10824 . cong - either PETSC_TRUE or PETSC_FALSE 10825 10826 Level: beginner 10827 10828 Notes: 10829 10830 .seealso: MatCreate(), MatSetSizes() 10831 @*/ 10832 PetscErrorCode MatHasCongruentLayouts(Mat mat,PetscBool *cong) 10833 { 10834 PetscErrorCode ierr; 10835 10836 PetscFunctionBegin; 10837 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10838 PetscValidType(mat,1); 10839 PetscValidPointer(cong,2); 10840 if (!mat->rmap || !mat->cmap) { 10841 *cong = mat->rmap == mat->cmap ? PETSC_TRUE : PETSC_FALSE; 10842 PetscFunctionReturn(0); 10843 } 10844 if (mat->congruentlayouts == PETSC_DECIDE) { /* first time we compare rows and cols layouts */ 10845 ierr = PetscLayoutCompare(mat->rmap,mat->cmap,cong);CHKERRQ(ierr); 10846 if (*cong) mat->congruentlayouts = 1; 10847 else mat->congruentlayouts = 0; 10848 } else *cong = mat->congruentlayouts ? PETSC_TRUE : PETSC_FALSE; 10849 PetscFunctionReturn(0); 10850 } 10851 10852 PetscErrorCode MatSetInf(Mat A) 10853 { 10854 PetscErrorCode ierr; 10855 10856 PetscFunctionBegin; 10857 if (!A->ops->setinf) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"No support for this operation for this matrix type"); 10858 ierr = (*A->ops->setinf)(A);CHKERRQ(ierr); 10859 PetscFunctionReturn(0); 10860 } 10861