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