1 2 /* 3 This is where the abstract matrix operations are defined 4 */ 5 6 #include <petsc/private/matimpl.h> /*I "petscmat.h" I*/ 7 #include <petsc/private/isimpl.h> 8 #include <petsc/private/vecimpl.h> 9 10 /* Logging support */ 11 PetscClassId MAT_CLASSID; 12 PetscClassId MAT_COLORING_CLASSID; 13 PetscClassId MAT_FDCOLORING_CLASSID; 14 PetscClassId MAT_TRANSPOSECOLORING_CLASSID; 15 16 PetscLogEvent MAT_Mult, MAT_Mults, MAT_MultConstrained, MAT_MultAdd, MAT_MultTranspose; 17 PetscLogEvent MAT_MultTransposeConstrained, MAT_MultTransposeAdd, MAT_Solve, MAT_Solves, MAT_SolveAdd, MAT_SolveTranspose, MAT_MatSolve,MAT_MatTrSolve; 18 PetscLogEvent MAT_SolveTransposeAdd, MAT_SOR, MAT_ForwardSolve, MAT_BackwardSolve, MAT_LUFactor, MAT_LUFactorSymbolic; 19 PetscLogEvent MAT_LUFactorNumeric, MAT_CholeskyFactor, MAT_CholeskyFactorSymbolic, MAT_CholeskyFactorNumeric, MAT_ILUFactor; 20 PetscLogEvent MAT_ILUFactorSymbolic, MAT_ICCFactorSymbolic, MAT_Copy, MAT_Convert, MAT_Scale, MAT_AssemblyBegin; 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_SetValuesBatch; 36 PetscLogEvent MAT_ViennaCLCopyToGPU; 37 PetscLogEvent MAT_Merge,MAT_Residual,MAT_SetRandom; 38 PetscLogEvent MATCOLORING_Apply,MATCOLORING_Comm,MATCOLORING_Local,MATCOLORING_ISCreate,MATCOLORING_SetUp,MATCOLORING_Weights; 39 40 const char *const MatFactorTypes[] = {"NONE","LU","CHOLESKY","ILU","ICC","ILUDT","MatFactorType","MAT_FACTOR_",0}; 41 42 /*@ 43 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 44 45 Logically Collective on Mat 46 47 Input Parameters: 48 + x - the matrix 49 - rctx - the random number context, formed by PetscRandomCreate(), or NULL and 50 it will create one internally. 51 52 Output Parameter: 53 . x - the matrix 54 55 Example of Usage: 56 .vb 57 PetscRandomCreate(PETSC_COMM_WORLD,&rctx); 58 MatSetRandom(x,rctx); 59 PetscRandomDestroy(rctx); 60 .ve 61 62 Level: intermediate 63 64 65 .seealso: MatZeroEntries(), MatSetValues(), PetscRandomCreate(), PetscRandomDestroy() 66 @*/ 67 PetscErrorCode MatSetRandom(Mat x,PetscRandom rctx) 68 { 69 PetscErrorCode ierr; 70 PetscRandom randObj = NULL; 71 72 PetscFunctionBegin; 73 PetscValidHeaderSpecific(x,MAT_CLASSID,1); 74 if (rctx) PetscValidHeaderSpecific(rctx,PETSC_RANDOM_CLASSID,2); 75 PetscValidType(x,1); 76 77 if (!x->ops->setrandom) SETERRQ1(PetscObjectComm((PetscObject)x),PETSC_ERR_SUP,"Mat type %s",((PetscObject)x)->type_name); 78 79 if (!rctx) { 80 MPI_Comm comm; 81 ierr = PetscObjectGetComm((PetscObject)x,&comm);CHKERRQ(ierr); 82 ierr = PetscRandomCreate(comm,&randObj);CHKERRQ(ierr); 83 ierr = PetscRandomSetFromOptions(randObj);CHKERRQ(ierr); 84 rctx = randObj; 85 } 86 87 ierr = PetscLogEventBegin(MAT_SetRandom,x,rctx,0,0);CHKERRQ(ierr); 88 ierr = (*x->ops->setrandom)(x,rctx);CHKERRQ(ierr); 89 ierr = PetscLogEventEnd(MAT_SetRandom,x,rctx,0,0);CHKERRQ(ierr); 90 91 ierr = MatAssemblyBegin(x, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 92 ierr = MatAssemblyEnd(x, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 93 ierr = PetscRandomDestroy(&randObj);CHKERRQ(ierr); 94 PetscFunctionReturn(0); 95 } 96 97 /*@ 98 MatFactorGetErrorZeroPivot - returns the pivot value that was determined to be zero and the row it occurred in 99 100 Logically Collective on Mat 101 102 Input Parameters: 103 . mat - the factored matrix 104 105 Output Parameter: 106 + pivot - the pivot value computed 107 - row - the row that the zero pivot occurred. Note that this row must be interpreted carefully due to row reorderings and which processes 108 the share the matrix 109 110 Level: advanced 111 112 Notes: 113 This routine does not work for factorizations done with external packages. 114 This routine should only be called if MatGetFactorError() returns a value of MAT_FACTOR_NUMERIC_ZEROPIVOT 115 116 This can be called on non-factored matrices that come from, for example, matrices used in SOR. 117 118 .seealso: MatZeroEntries(), MatFactor(), MatGetFactor(), MatFactorSymbolic(), MatFactorClearError(), MatFactorGetErrorZeroPivot() 119 @*/ 120 PetscErrorCode MatFactorGetErrorZeroPivot(Mat mat,PetscReal *pivot,PetscInt *row) 121 { 122 PetscFunctionBegin; 123 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 124 *pivot = mat->factorerror_zeropivot_value; 125 *row = mat->factorerror_zeropivot_row; 126 PetscFunctionReturn(0); 127 } 128 129 /*@ 130 MatFactorGetError - gets the error code from a factorization 131 132 Logically Collective on Mat 133 134 Input Parameters: 135 . mat - the factored matrix 136 137 Output Parameter: 138 . err - the error code 139 140 Level: advanced 141 142 Notes: 143 This can be called on non-factored matrices that come from, for example, matrices used in SOR. 144 145 .seealso: MatZeroEntries(), MatFactor(), MatGetFactor(), MatFactorSymbolic(), MatFactorClearError(), MatFactorGetErrorZeroPivot() 146 @*/ 147 PetscErrorCode MatFactorGetError(Mat mat,MatFactorError *err) 148 { 149 PetscFunctionBegin; 150 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 151 *err = mat->factorerrortype; 152 PetscFunctionReturn(0); 153 } 154 155 /*@ 156 MatFactorClearError - clears the error code in a factorization 157 158 Logically Collective on Mat 159 160 Input Parameter: 161 . mat - the factored matrix 162 163 Level: developer 164 165 Notes: 166 This can be called on non-factored matrices that come from, for example, matrices used in SOR. 167 168 .seealso: MatZeroEntries(), MatFactor(), MatGetFactor(), MatFactorSymbolic(), MatFactorGetError(), MatFactorGetErrorZeroPivot() 169 @*/ 170 PetscErrorCode MatFactorClearError(Mat mat) 171 { 172 PetscFunctionBegin; 173 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 174 mat->factorerrortype = MAT_FACTOR_NOERROR; 175 mat->factorerror_zeropivot_value = 0.0; 176 mat->factorerror_zeropivot_row = 0; 177 PetscFunctionReturn(0); 178 } 179 180 PETSC_INTERN PetscErrorCode MatFindNonzeroRowsOrCols_Basic(Mat mat,PetscBool cols,PetscReal tol,IS *nonzero) 181 { 182 PetscErrorCode ierr; 183 Vec r,l; 184 const PetscScalar *al; 185 PetscInt i,nz,gnz,N,n; 186 187 PetscFunctionBegin; 188 ierr = MatCreateVecs(mat,&r,&l);CHKERRQ(ierr); 189 if (!cols) { /* nonzero rows */ 190 ierr = MatGetSize(mat,&N,NULL);CHKERRQ(ierr); 191 ierr = MatGetLocalSize(mat,&n,NULL);CHKERRQ(ierr); 192 ierr = VecSet(l,0.0);CHKERRQ(ierr); 193 ierr = VecSetRandom(r,NULL);CHKERRQ(ierr); 194 ierr = MatMult(mat,r,l);CHKERRQ(ierr); 195 ierr = VecGetArrayRead(l,&al);CHKERRQ(ierr); 196 } else { /* nonzero columns */ 197 ierr = MatGetSize(mat,NULL,&N);CHKERRQ(ierr); 198 ierr = MatGetLocalSize(mat,NULL,&n);CHKERRQ(ierr); 199 ierr = VecSet(r,0.0);CHKERRQ(ierr); 200 ierr = VecSetRandom(l,NULL);CHKERRQ(ierr); 201 ierr = MatMultTranspose(mat,l,r);CHKERRQ(ierr); 202 ierr = VecGetArrayRead(r,&al);CHKERRQ(ierr); 203 } 204 if (tol <= 0.0) { for (i=0,nz=0;i<n;i++) if (al[i] != 0.0) nz++; } 205 else { for (i=0,nz=0;i<n;i++) if (PetscAbsScalar(al[i]) > tol) nz++; } 206 ierr = MPIU_Allreduce(&nz,&gnz,1,MPIU_INT,MPI_SUM,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 207 if (gnz != N) { 208 PetscInt *nzr; 209 ierr = PetscMalloc1(nz,&nzr);CHKERRQ(ierr); 210 if (nz) { 211 if (tol < 0) { for (i=0,nz=0;i<n;i++) if (al[i] != 0.0) nzr[nz++] = i; } 212 else { for (i=0,nz=0;i<n;i++) if (PetscAbsScalar(al[i]) > tol) nzr[nz++] = i; } 213 } 214 ierr = ISCreateGeneral(PetscObjectComm((PetscObject)mat),nz,nzr,PETSC_OWN_POINTER,nonzero);CHKERRQ(ierr); 215 } else *nonzero = NULL; 216 if (!cols) { /* nonzero rows */ 217 ierr = VecRestoreArrayRead(l,&al);CHKERRQ(ierr); 218 } else { 219 ierr = VecRestoreArrayRead(r,&al);CHKERRQ(ierr); 220 } 221 ierr = VecDestroy(&l);CHKERRQ(ierr); 222 ierr = VecDestroy(&r);CHKERRQ(ierr); 223 PetscFunctionReturn(0); 224 } 225 226 /*@ 227 MatFindNonzeroRows - Locate all rows that are not completely zero in the matrix 228 229 Input Parameter: 230 . A - the matrix 231 232 Output Parameter: 233 . keptrows - the rows that are not completely zero 234 235 Notes: 236 keptrows is set to NULL if all rows are nonzero. 237 238 Level: intermediate 239 240 @*/ 241 PetscErrorCode MatFindNonzeroRows(Mat mat,IS *keptrows) 242 { 243 PetscErrorCode ierr; 244 245 PetscFunctionBegin; 246 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 247 PetscValidType(mat,1); 248 PetscValidPointer(keptrows,2); 249 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 250 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 251 if (!mat->ops->findnonzerorows) { 252 ierr = MatFindNonzeroRowsOrCols_Basic(mat,PETSC_FALSE,0.0,keptrows);CHKERRQ(ierr); 253 } else { 254 ierr = (*mat->ops->findnonzerorows)(mat,keptrows);CHKERRQ(ierr); 255 } 256 PetscFunctionReturn(0); 257 } 258 259 /*@ 260 MatFindZeroRows - Locate all rows that are completely zero in the matrix 261 262 Input Parameter: 263 . A - the matrix 264 265 Output Parameter: 266 . zerorows - the rows that are completely zero 267 268 Notes: 269 zerorows is set to NULL if no rows are zero. 270 271 Level: intermediate 272 273 @*/ 274 PetscErrorCode MatFindZeroRows(Mat mat,IS *zerorows) 275 { 276 PetscErrorCode ierr; 277 IS keptrows; 278 PetscInt m, n; 279 280 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 281 PetscValidType(mat,1); 282 283 ierr = MatFindNonzeroRows(mat, &keptrows);CHKERRQ(ierr); 284 /* MatFindNonzeroRows sets keptrows to NULL if there are no zero rows. 285 In keeping with this convention, we set zerorows to NULL if there are no zero 286 rows. */ 287 if (keptrows == NULL) { 288 *zerorows = NULL; 289 } else { 290 ierr = MatGetOwnershipRange(mat,&m,&n);CHKERRQ(ierr); 291 ierr = ISComplement(keptrows,m,n,zerorows);CHKERRQ(ierr); 292 ierr = ISDestroy(&keptrows);CHKERRQ(ierr); 293 } 294 PetscFunctionReturn(0); 295 } 296 297 /*@ 298 MatGetDiagonalBlock - Returns the part of the matrix associated with the on-process coupling 299 300 Not Collective 301 302 Input Parameters: 303 . A - the matrix 304 305 Output Parameters: 306 . a - the diagonal part (which is a SEQUENTIAL matrix) 307 308 Notes: 309 see the manual page for MatCreateAIJ() for more information on the "diagonal part" of the matrix. 310 Use caution, as the reference count on the returned matrix is not incremented and it is used as 311 part of the containing MPI Mat's normal operation. 312 313 Level: advanced 314 315 @*/ 316 PetscErrorCode MatGetDiagonalBlock(Mat A,Mat *a) 317 { 318 PetscErrorCode ierr; 319 320 PetscFunctionBegin; 321 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 322 PetscValidType(A,1); 323 PetscValidPointer(a,3); 324 if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 325 if (!A->ops->getdiagonalblock) { 326 PetscMPIInt size; 327 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)A),&size);CHKERRQ(ierr); 328 if (size == 1) { 329 *a = A; 330 PetscFunctionReturn(0); 331 } else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Not coded for this matrix type"); 332 } 333 ierr = (*A->ops->getdiagonalblock)(A,a);CHKERRQ(ierr); 334 PetscFunctionReturn(0); 335 } 336 337 /*@ 338 MatGetTrace - Gets the trace of a matrix. The sum of the diagonal entries. 339 340 Collective on Mat 341 342 Input Parameters: 343 . mat - the matrix 344 345 Output Parameter: 346 . trace - the sum of the diagonal entries 347 348 Level: advanced 349 350 @*/ 351 PetscErrorCode MatGetTrace(Mat mat,PetscScalar *trace) 352 { 353 PetscErrorCode ierr; 354 Vec diag; 355 356 PetscFunctionBegin; 357 ierr = MatCreateVecs(mat,&diag,NULL);CHKERRQ(ierr); 358 ierr = MatGetDiagonal(mat,diag);CHKERRQ(ierr); 359 ierr = VecSum(diag,trace);CHKERRQ(ierr); 360 ierr = VecDestroy(&diag);CHKERRQ(ierr); 361 PetscFunctionReturn(0); 362 } 363 364 /*@ 365 MatRealPart - Zeros out the imaginary part of the matrix 366 367 Logically Collective on Mat 368 369 Input Parameters: 370 . mat - the matrix 371 372 Level: advanced 373 374 375 .seealso: MatImaginaryPart() 376 @*/ 377 PetscErrorCode MatRealPart(Mat mat) 378 { 379 PetscErrorCode ierr; 380 381 PetscFunctionBegin; 382 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 383 PetscValidType(mat,1); 384 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 385 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 386 if (!mat->ops->realpart) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 387 MatCheckPreallocated(mat,1); 388 ierr = (*mat->ops->realpart)(mat);CHKERRQ(ierr); 389 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 390 if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) { 391 mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU; 392 } 393 #endif 394 PetscFunctionReturn(0); 395 } 396 397 /*@C 398 MatGetGhosts - Get the global index of all ghost nodes defined by the sparse matrix 399 400 Collective on Mat 401 402 Input Parameter: 403 . mat - the matrix 404 405 Output Parameters: 406 + nghosts - number of ghosts (note for BAIJ matrices there is one ghost for each block) 407 - ghosts - the global indices of the ghost points 408 409 Notes: 410 the nghosts and ghosts are suitable to pass into VecCreateGhost() 411 412 Level: advanced 413 414 @*/ 415 PetscErrorCode MatGetGhosts(Mat mat,PetscInt *nghosts,const PetscInt *ghosts[]) 416 { 417 PetscErrorCode ierr; 418 419 PetscFunctionBegin; 420 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 421 PetscValidType(mat,1); 422 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 423 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 424 if (!mat->ops->getghosts) { 425 if (nghosts) *nghosts = 0; 426 if (ghosts) *ghosts = 0; 427 } else { 428 ierr = (*mat->ops->getghosts)(mat,nghosts,ghosts);CHKERRQ(ierr); 429 } 430 PetscFunctionReturn(0); 431 } 432 433 434 /*@ 435 MatImaginaryPart - Moves the imaginary part of the matrix to the real part and zeros the imaginary part 436 437 Logically Collective on Mat 438 439 Input Parameters: 440 . mat - the matrix 441 442 Level: advanced 443 444 445 .seealso: MatRealPart() 446 @*/ 447 PetscErrorCode MatImaginaryPart(Mat mat) 448 { 449 PetscErrorCode ierr; 450 451 PetscFunctionBegin; 452 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 453 PetscValidType(mat,1); 454 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 455 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 456 if (!mat->ops->imaginarypart) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 457 MatCheckPreallocated(mat,1); 458 ierr = (*mat->ops->imaginarypart)(mat);CHKERRQ(ierr); 459 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 460 if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) { 461 mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU; 462 } 463 #endif 464 PetscFunctionReturn(0); 465 } 466 467 /*@ 468 MatMissingDiagonal - Determine if sparse matrix is missing a diagonal entry (or block entry for BAIJ matrices) 469 470 Not Collective 471 472 Input Parameter: 473 . mat - the matrix 474 475 Output Parameters: 476 + missing - is any diagonal missing 477 - dd - first diagonal entry that is missing (optional) on this process 478 479 Level: advanced 480 481 482 .seealso: MatRealPart() 483 @*/ 484 PetscErrorCode MatMissingDiagonal(Mat mat,PetscBool *missing,PetscInt *dd) 485 { 486 PetscErrorCode ierr; 487 488 PetscFunctionBegin; 489 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 490 PetscValidType(mat,1); 491 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 492 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 493 if (!mat->ops->missingdiagonal) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 494 ierr = (*mat->ops->missingdiagonal)(mat,missing,dd);CHKERRQ(ierr); 495 PetscFunctionReturn(0); 496 } 497 498 /*@C 499 MatGetRow - Gets a row of a matrix. You MUST call MatRestoreRow() 500 for each row that you get to ensure that your application does 501 not bleed memory. 502 503 Not Collective 504 505 Input Parameters: 506 + mat - the matrix 507 - row - the row to get 508 509 Output Parameters: 510 + ncols - if not NULL, the number of nonzeros in the row 511 . cols - if not NULL, the column numbers 512 - vals - if not NULL, the values 513 514 Notes: 515 This routine is provided for people who need to have direct access 516 to the structure of a matrix. We hope that we provide enough 517 high-level matrix routines that few users will need it. 518 519 MatGetRow() always returns 0-based column indices, regardless of 520 whether the internal representation is 0-based (default) or 1-based. 521 522 For better efficiency, set cols and/or vals to NULL if you do 523 not wish to extract these quantities. 524 525 The user can only examine the values extracted with MatGetRow(); 526 the values cannot be altered. To change the matrix entries, one 527 must use MatSetValues(). 528 529 You can only have one call to MatGetRow() outstanding for a particular 530 matrix at a time, per processor. MatGetRow() can only obtain rows 531 associated with the given processor, it cannot get rows from the 532 other processors; for that we suggest using MatCreateSubMatrices(), then 533 MatGetRow() on the submatrix. The row index passed to MatGetRow() 534 is in the global number of rows. 535 536 Fortran Notes: 537 The calling sequence from Fortran is 538 .vb 539 MatGetRow(matrix,row,ncols,cols,values,ierr) 540 Mat matrix (input) 541 integer row (input) 542 integer ncols (output) 543 integer cols(maxcols) (output) 544 double precision (or double complex) values(maxcols) output 545 .ve 546 where maxcols >= maximum nonzeros in any row of the matrix. 547 548 549 Caution: 550 Do not try to change the contents of the output arrays (cols and vals). 551 In some cases, this may corrupt the matrix. 552 553 Level: advanced 554 555 .seealso: MatRestoreRow(), MatSetValues(), MatGetValues(), MatCreateSubMatrices(), MatGetDiagonal() 556 @*/ 557 PetscErrorCode MatGetRow(Mat mat,PetscInt row,PetscInt *ncols,const PetscInt *cols[],const PetscScalar *vals[]) 558 { 559 PetscErrorCode ierr; 560 PetscInt incols; 561 562 PetscFunctionBegin; 563 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 564 PetscValidType(mat,1); 565 if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 566 if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 567 if (!mat->ops->getrow) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 568 MatCheckPreallocated(mat,1); 569 ierr = PetscLogEventBegin(MAT_GetRow,mat,0,0,0);CHKERRQ(ierr); 570 ierr = (*mat->ops->getrow)(mat,row,&incols,(PetscInt**)cols,(PetscScalar**)vals);CHKERRQ(ierr); 571 if (ncols) *ncols = incols; 572 ierr = PetscLogEventEnd(MAT_GetRow,mat,0,0,0);CHKERRQ(ierr); 573 PetscFunctionReturn(0); 574 } 575 576 /*@ 577 MatConjugate - replaces the matrix values with their complex conjugates 578 579 Logically Collective on Mat 580 581 Input Parameters: 582 . mat - the matrix 583 584 Level: advanced 585 586 .seealso: VecConjugate() 587 @*/ 588 PetscErrorCode MatConjugate(Mat mat) 589 { 590 #if defined(PETSC_USE_COMPLEX) 591 PetscErrorCode ierr; 592 593 PetscFunctionBegin; 594 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 595 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 596 if (!mat->ops->conjugate) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Not provided for this matrix format, send email to petsc-maint@mcs.anl.gov"); 597 ierr = (*mat->ops->conjugate)(mat);CHKERRQ(ierr); 598 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 599 if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) { 600 mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU; 601 } 602 #endif 603 PetscFunctionReturn(0); 604 #else 605 return 0; 606 #endif 607 } 608 609 /*@C 610 MatRestoreRow - Frees any temporary space allocated by MatGetRow(). 611 612 Not Collective 613 614 Input Parameters: 615 + mat - the matrix 616 . row - the row to get 617 . ncols, cols - the number of nonzeros and their columns 618 - vals - if nonzero the column values 619 620 Notes: 621 This routine should be called after you have finished examining the entries. 622 623 This routine zeros out ncols, cols, and vals. This is to prevent accidental 624 us of the array after it has been restored. If you pass NULL, it will 625 not zero the pointers. Use of cols or vals after MatRestoreRow is invalid. 626 627 Fortran Notes: 628 The calling sequence from Fortran is 629 .vb 630 MatRestoreRow(matrix,row,ncols,cols,values,ierr) 631 Mat matrix (input) 632 integer row (input) 633 integer ncols (output) 634 integer cols(maxcols) (output) 635 double precision (or double complex) values(maxcols) output 636 .ve 637 Where maxcols >= maximum nonzeros in any row of the matrix. 638 639 In Fortran MatRestoreRow() MUST be called after MatGetRow() 640 before another call to MatGetRow() can be made. 641 642 Level: advanced 643 644 .seealso: MatGetRow() 645 @*/ 646 PetscErrorCode MatRestoreRow(Mat mat,PetscInt row,PetscInt *ncols,const PetscInt *cols[],const PetscScalar *vals[]) 647 { 648 PetscErrorCode ierr; 649 650 PetscFunctionBegin; 651 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 652 if (ncols) PetscValidIntPointer(ncols,3); 653 if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 654 if (!mat->ops->restorerow) PetscFunctionReturn(0); 655 ierr = (*mat->ops->restorerow)(mat,row,ncols,(PetscInt **)cols,(PetscScalar **)vals);CHKERRQ(ierr); 656 if (ncols) *ncols = 0; 657 if (cols) *cols = NULL; 658 if (vals) *vals = NULL; 659 PetscFunctionReturn(0); 660 } 661 662 /*@ 663 MatGetRowUpperTriangular - Sets a flag to enable calls to MatGetRow() for matrix in MATSBAIJ format. 664 You should call MatRestoreRowUpperTriangular() after calling MatGetRow/MatRestoreRow() to disable the flag. 665 666 Not Collective 667 668 Input Parameters: 669 . mat - the matrix 670 671 Notes: 672 The flag is to ensure that users are aware of MatGetRow() only provides the upper trianglular part of the row for the matrices in MATSBAIJ format. 673 674 Level: advanced 675 676 .seealso: MatRestoreRowUpperTriangular() 677 @*/ 678 PetscErrorCode MatGetRowUpperTriangular(Mat mat) 679 { 680 PetscErrorCode ierr; 681 682 PetscFunctionBegin; 683 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 684 PetscValidType(mat,1); 685 if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 686 if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 687 MatCheckPreallocated(mat,1); 688 if (!mat->ops->getrowuppertriangular) PetscFunctionReturn(0); 689 ierr = (*mat->ops->getrowuppertriangular)(mat);CHKERRQ(ierr); 690 PetscFunctionReturn(0); 691 } 692 693 /*@ 694 MatRestoreRowUpperTriangular - Disable calls to MatGetRow() for matrix in MATSBAIJ format. 695 696 Not Collective 697 698 Input Parameters: 699 . mat - the matrix 700 701 Notes: 702 This routine should be called after you have finished MatGetRow/MatRestoreRow(). 703 704 705 Level: advanced 706 707 .seealso: MatGetRowUpperTriangular() 708 @*/ 709 PetscErrorCode MatRestoreRowUpperTriangular(Mat mat) 710 { 711 PetscErrorCode ierr; 712 713 PetscFunctionBegin; 714 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 715 PetscValidType(mat,1); 716 if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 717 if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 718 MatCheckPreallocated(mat,1); 719 if (!mat->ops->restorerowuppertriangular) PetscFunctionReturn(0); 720 ierr = (*mat->ops->restorerowuppertriangular)(mat);CHKERRQ(ierr); 721 PetscFunctionReturn(0); 722 } 723 724 /*@C 725 MatSetOptionsPrefix - Sets the prefix used for searching for all 726 Mat options in the database. 727 728 Logically Collective on Mat 729 730 Input Parameter: 731 + A - the Mat context 732 - prefix - the prefix to prepend to all option names 733 734 Notes: 735 A hyphen (-) must NOT be given at the beginning of the prefix name. 736 The first character of all runtime options is AUTOMATICALLY the hyphen. 737 738 Level: advanced 739 740 .seealso: MatSetFromOptions() 741 @*/ 742 PetscErrorCode MatSetOptionsPrefix(Mat A,const char prefix[]) 743 { 744 PetscErrorCode ierr; 745 746 PetscFunctionBegin; 747 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 748 ierr = PetscObjectSetOptionsPrefix((PetscObject)A,prefix);CHKERRQ(ierr); 749 PetscFunctionReturn(0); 750 } 751 752 /*@C 753 MatAppendOptionsPrefix - Appends to the prefix used for searching for all 754 Mat options in the database. 755 756 Logically Collective on Mat 757 758 Input Parameters: 759 + A - the Mat context 760 - prefix - the prefix to prepend to all option names 761 762 Notes: 763 A hyphen (-) must NOT be given at the beginning of the prefix name. 764 The first character of all runtime options is AUTOMATICALLY the hyphen. 765 766 Level: advanced 767 768 .seealso: MatGetOptionsPrefix() 769 @*/ 770 PetscErrorCode MatAppendOptionsPrefix(Mat A,const char prefix[]) 771 { 772 PetscErrorCode ierr; 773 774 PetscFunctionBegin; 775 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 776 ierr = PetscObjectAppendOptionsPrefix((PetscObject)A,prefix);CHKERRQ(ierr); 777 PetscFunctionReturn(0); 778 } 779 780 /*@C 781 MatGetOptionsPrefix - Sets the prefix used for searching for all 782 Mat options in the database. 783 784 Not Collective 785 786 Input Parameter: 787 . A - the Mat context 788 789 Output Parameter: 790 . prefix - pointer to the prefix string used 791 792 Notes: 793 On the fortran side, the user should pass in a string 'prefix' of 794 sufficient length to hold the prefix. 795 796 Level: advanced 797 798 .seealso: MatAppendOptionsPrefix() 799 @*/ 800 PetscErrorCode MatGetOptionsPrefix(Mat A,const char *prefix[]) 801 { 802 PetscErrorCode ierr; 803 804 PetscFunctionBegin; 805 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 806 ierr = PetscObjectGetOptionsPrefix((PetscObject)A,prefix);CHKERRQ(ierr); 807 PetscFunctionReturn(0); 808 } 809 810 /*@ 811 MatResetPreallocation - Reset mat to use the original nonzero pattern provided by users. 812 813 Collective on Mat 814 815 Input Parameters: 816 . A - the Mat context 817 818 Notes: 819 The allocated memory will be shrunk after calling MatAssembly with MAT_FINAL_ASSEMBLY. Users can reset the preallocation to access the original memory. 820 Currently support MPIAIJ and SEQAIJ. 821 822 Level: beginner 823 824 .seealso: MatSeqAIJSetPreallocation(), MatMPIAIJSetPreallocation(), MatXAIJSetPreallocation() 825 @*/ 826 PetscErrorCode MatResetPreallocation(Mat A) 827 { 828 PetscErrorCode ierr; 829 830 PetscFunctionBegin; 831 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 832 PetscValidType(A,1); 833 ierr = PetscUseMethod(A,"MatResetPreallocation_C",(Mat),(A));CHKERRQ(ierr); 834 PetscFunctionReturn(0); 835 } 836 837 838 /*@ 839 MatSetUp - Sets up the internal matrix data structures for the later use. 840 841 Collective on Mat 842 843 Input Parameters: 844 . A - the Mat context 845 846 Notes: 847 If the user has not set preallocation for this matrix then a default preallocation that is likely to be inefficient is used. 848 849 If a suitable preallocation routine is used, this function does not need to be called. 850 851 See the Performance chapter of the PETSc users manual for how to preallocate matrices 852 853 Level: beginner 854 855 .seealso: MatCreate(), MatDestroy() 856 @*/ 857 PetscErrorCode MatSetUp(Mat A) 858 { 859 PetscMPIInt size; 860 PetscErrorCode ierr; 861 862 PetscFunctionBegin; 863 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 864 if (!((PetscObject)A)->type_name) { 865 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)A), &size);CHKERRQ(ierr); 866 if (size == 1) { 867 ierr = MatSetType(A, MATSEQAIJ);CHKERRQ(ierr); 868 } else { 869 ierr = MatSetType(A, MATMPIAIJ);CHKERRQ(ierr); 870 } 871 } 872 if (!A->preallocated && A->ops->setup) { 873 ierr = PetscInfo(A,"Warning not preallocating matrix storage\n");CHKERRQ(ierr); 874 ierr = (*A->ops->setup)(A);CHKERRQ(ierr); 875 } 876 ierr = PetscLayoutSetUp(A->rmap);CHKERRQ(ierr); 877 ierr = PetscLayoutSetUp(A->cmap);CHKERRQ(ierr); 878 A->preallocated = PETSC_TRUE; 879 PetscFunctionReturn(0); 880 } 881 882 #if defined(PETSC_HAVE_SAWS) 883 #include <petscviewersaws.h> 884 #endif 885 /*@C 886 MatView - Visualizes a matrix object. 887 888 Collective on Mat 889 890 Input Parameters: 891 + mat - the matrix 892 - viewer - visualization context 893 894 Notes: 895 The available visualization contexts include 896 + PETSC_VIEWER_STDOUT_SELF - for sequential matrices 897 . PETSC_VIEWER_STDOUT_WORLD - for parallel matrices created on PETSC_COMM_WORLD 898 . PETSC_VIEWER_STDOUT_(comm) - for matrices created on MPI communicator comm 899 - PETSC_VIEWER_DRAW_WORLD - graphical display of nonzero structure 900 901 The user can open alternative visualization contexts with 902 + PetscViewerASCIIOpen() - Outputs matrix to a specified file 903 . PetscViewerBinaryOpen() - Outputs matrix in binary to a 904 specified file; corresponding input uses MatLoad() 905 . PetscViewerDrawOpen() - Outputs nonzero matrix structure to 906 an X window display 907 - PetscViewerSocketOpen() - Outputs matrix to Socket viewer. 908 Currently only the sequential dense and AIJ 909 matrix types support the Socket viewer. 910 911 The user can call PetscViewerPushFormat() to specify the output 912 format of ASCII printed objects (when using PETSC_VIEWER_STDOUT_SELF, 913 PETSC_VIEWER_STDOUT_WORLD and PetscViewerASCIIOpen). Available formats include 914 + PETSC_VIEWER_DEFAULT - default, prints matrix contents 915 . PETSC_VIEWER_ASCII_MATLAB - prints matrix contents in Matlab format 916 . PETSC_VIEWER_ASCII_DENSE - prints entire matrix including zeros 917 . PETSC_VIEWER_ASCII_COMMON - prints matrix contents, using a sparse 918 format common among all matrix types 919 . PETSC_VIEWER_ASCII_IMPL - prints matrix contents, using an implementation-specific 920 format (which is in many cases the same as the default) 921 . PETSC_VIEWER_ASCII_INFO - prints basic information about the matrix 922 size and structure (not the matrix entries) 923 - PETSC_VIEWER_ASCII_INFO_DETAIL - prints more detailed information about 924 the matrix structure 925 926 Options Database Keys: 927 + -mat_view ::ascii_info - Prints info on matrix at conclusion of MatAssemblyEnd() 928 . -mat_view ::ascii_info_detail - Prints more detailed info 929 . -mat_view - Prints matrix in ASCII format 930 . -mat_view ::ascii_matlab - Prints matrix in Matlab format 931 . -mat_view draw - PetscDraws nonzero structure of matrix, using MatView() and PetscDrawOpenX(). 932 . -display <name> - Sets display name (default is host) 933 . -draw_pause <sec> - Sets number of seconds to pause after display 934 . -mat_view socket - Sends matrix to socket, can be accessed from Matlab (see Users-Manual: ch_matlab for details) 935 . -viewer_socket_machine <machine> - 936 . -viewer_socket_port <port> - 937 . -mat_view binary - save matrix to file in binary format 938 - -viewer_binary_filename <name> - 939 Level: beginner 940 941 Notes: 942 The ASCII viewers are only recommended for small matrices on at most a moderate number of processes, 943 the program will seemingly hang and take hours for larger matrices, for larger matrices one should use the binary format. 944 945 See the manual page for MatLoad() for the exact format of the binary file when the binary 946 viewer is used. 947 948 See share/petsc/matlab/PetscBinaryRead.m for a Matlab code that can read in the binary file when the binary 949 viewer is used. 950 951 One can use '-mat_view draw -draw_pause -1' to pause the graphical display of matrix nonzero structure, 952 and then use the following mouse functions. 953 + left mouse: zoom in 954 . middle mouse: zoom out 955 - right mouse: continue with the simulation 956 957 .seealso: PetscViewerPushFormat(), PetscViewerASCIIOpen(), PetscViewerDrawOpen(), 958 PetscViewerSocketOpen(), PetscViewerBinaryOpen(), MatLoad() 959 @*/ 960 PetscErrorCode MatView(Mat mat,PetscViewer viewer) 961 { 962 PetscErrorCode ierr; 963 PetscInt rows,cols,rbs,cbs; 964 PetscBool iascii,ibinary,isstring; 965 PetscViewerFormat format; 966 PetscMPIInt size; 967 #if defined(PETSC_HAVE_SAWS) 968 PetscBool issaws; 969 #endif 970 971 PetscFunctionBegin; 972 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 973 PetscValidType(mat,1); 974 if (!viewer) { 975 ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)mat),&viewer);CHKERRQ(ierr); 976 } 977 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 978 PetscCheckSameComm(mat,1,viewer,2); 979 MatCheckPreallocated(mat,1); 980 ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 981 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);CHKERRQ(ierr); 982 if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0); 983 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr); 984 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 985 if (ibinary) { 986 PetscBool mpiio; 987 ierr = PetscViewerBinaryGetUseMPIIO(viewer,&mpiio);CHKERRQ(ierr); 988 if (mpiio) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"PETSc matrix viewers do not support using MPI-IO, turn off that flag"); 989 } 990 991 ierr = PetscLogEventBegin(MAT_View,mat,viewer,0,0);CHKERRQ(ierr); 992 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 993 if ((!iascii || (format != PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL)) && mat->factortype) { 994 SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"No viewers for factored matrix except ASCII info or info_detailed"); 995 } 996 997 #if defined(PETSC_HAVE_SAWS) 998 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr); 999 #endif 1000 if (iascii) { 1001 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ORDER,"Must call MatAssemblyBegin/End() before viewing matrix"); 1002 ierr = PetscObjectPrintClassNamePrefixType((PetscObject)mat,viewer);CHKERRQ(ierr); 1003 if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) { 1004 MatNullSpace nullsp,transnullsp; 1005 1006 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1007 ierr = MatGetSize(mat,&rows,&cols);CHKERRQ(ierr); 1008 ierr = MatGetBlockSizes(mat,&rbs,&cbs);CHKERRQ(ierr); 1009 if (rbs != 1 || cbs != 1) { 1010 if (rbs != cbs) {ierr = PetscViewerASCIIPrintf(viewer,"rows=%D, cols=%D, rbs=%D, cbs = %D\n",rows,cols,rbs,cbs);CHKERRQ(ierr);} 1011 else {ierr = PetscViewerASCIIPrintf(viewer,"rows=%D, cols=%D, bs=%D\n",rows,cols,rbs);CHKERRQ(ierr);} 1012 } else { 1013 ierr = PetscViewerASCIIPrintf(viewer,"rows=%D, cols=%D\n",rows,cols);CHKERRQ(ierr); 1014 } 1015 if (mat->factortype) { 1016 MatSolverType solver; 1017 ierr = MatFactorGetSolverType(mat,&solver);CHKERRQ(ierr); 1018 ierr = PetscViewerASCIIPrintf(viewer,"package used to perform factorization: %s\n",solver);CHKERRQ(ierr); 1019 } 1020 if (mat->ops->getinfo) { 1021 MatInfo info; 1022 ierr = MatGetInfo(mat,MAT_GLOBAL_SUM,&info);CHKERRQ(ierr); 1023 ierr = PetscViewerASCIIPrintf(viewer,"total: nonzeros=%.f, allocated nonzeros=%.f\n",info.nz_used,info.nz_allocated);CHKERRQ(ierr); 1024 ierr = PetscViewerASCIIPrintf(viewer,"total number of mallocs used during MatSetValues calls =%D\n",(PetscInt)info.mallocs);CHKERRQ(ierr); 1025 } 1026 ierr = MatGetNullSpace(mat,&nullsp);CHKERRQ(ierr); 1027 ierr = MatGetTransposeNullSpace(mat,&transnullsp);CHKERRQ(ierr); 1028 if (nullsp) {ierr = PetscViewerASCIIPrintf(viewer," has attached null space\n");CHKERRQ(ierr);} 1029 if (transnullsp && transnullsp != nullsp) {ierr = PetscViewerASCIIPrintf(viewer," has attached transposed null space\n");CHKERRQ(ierr);} 1030 ierr = MatGetNearNullSpace(mat,&nullsp);CHKERRQ(ierr); 1031 if (nullsp) {ierr = PetscViewerASCIIPrintf(viewer," has attached near null space\n");CHKERRQ(ierr);} 1032 } 1033 #if defined(PETSC_HAVE_SAWS) 1034 } else if (issaws) { 1035 PetscMPIInt rank; 1036 1037 ierr = PetscObjectName((PetscObject)mat);CHKERRQ(ierr); 1038 ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); 1039 if (!((PetscObject)mat)->amsmem && !rank) { 1040 ierr = PetscObjectViewSAWs((PetscObject)mat,viewer);CHKERRQ(ierr); 1041 } 1042 #endif 1043 } else if (isstring) { 1044 const char *type; 1045 ierr = MatGetType(mat,&type);CHKERRQ(ierr); 1046 ierr = PetscViewerStringSPrintf(viewer," MatType: %-7.7s",type);CHKERRQ(ierr); 1047 if (mat->ops->view) {ierr = (*mat->ops->view)(mat,viewer);CHKERRQ(ierr);} 1048 } 1049 if ((format == PETSC_VIEWER_NATIVE || format == PETSC_VIEWER_LOAD_BALANCE) && mat->ops->viewnative) { 1050 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1051 ierr = (*mat->ops->viewnative)(mat,viewer);CHKERRQ(ierr); 1052 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1053 } else if (mat->ops->view) { 1054 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1055 ierr = (*mat->ops->view)(mat,viewer);CHKERRQ(ierr); 1056 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1057 } 1058 if (iascii) { 1059 ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 1060 if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) { 1061 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1062 } 1063 } 1064 ierr = PetscLogEventEnd(MAT_View,mat,viewer,0,0);CHKERRQ(ierr); 1065 PetscFunctionReturn(0); 1066 } 1067 1068 #if defined(PETSC_USE_DEBUG) 1069 #include <../src/sys/totalview/tv_data_display.h> 1070 PETSC_UNUSED static int TV_display_type(const struct _p_Mat *mat) 1071 { 1072 TV_add_row("Local rows", "int", &mat->rmap->n); 1073 TV_add_row("Local columns", "int", &mat->cmap->n); 1074 TV_add_row("Global rows", "int", &mat->rmap->N); 1075 TV_add_row("Global columns", "int", &mat->cmap->N); 1076 TV_add_row("Typename", TV_ascii_string_type, ((PetscObject)mat)->type_name); 1077 return TV_format_OK; 1078 } 1079 #endif 1080 1081 /*@C 1082 MatLoad - Loads a matrix that has been stored in binary/HDF5 format 1083 with MatView(). The matrix format is determined from the options database. 1084 Generates a parallel MPI matrix if the communicator has more than one 1085 processor. The default matrix type is AIJ. 1086 1087 Collective on PetscViewer 1088 1089 Input Parameters: 1090 + newmat - the newly loaded matrix, this needs to have been created with MatCreate() 1091 or some related function before a call to MatLoad() 1092 - viewer - binary/HDF5 file viewer 1093 1094 Options Database Keys: 1095 Used with block matrix formats (MATSEQBAIJ, ...) to specify 1096 block size 1097 . -matload_block_size <bs> 1098 1099 Level: beginner 1100 1101 Notes: 1102 If the Mat type has not yet been given then MATAIJ is used, call MatSetFromOptions() on the 1103 Mat before calling this routine if you wish to set it from the options database. 1104 1105 MatLoad() automatically loads into the options database any options 1106 given in the file filename.info where filename is the name of the file 1107 that was passed to the PetscViewerBinaryOpen(). The options in the info 1108 file will be ignored if you use the -viewer_binary_skip_info option. 1109 1110 If the type or size of newmat is not set before a call to MatLoad, PETSc 1111 sets the default matrix type AIJ and sets the local and global sizes. 1112 If type and/or size is already set, then the same are used. 1113 1114 In parallel, each processor can load a subset of rows (or the 1115 entire matrix). This routine is especially useful when a large 1116 matrix is stored on disk and only part of it is desired on each 1117 processor. For example, a parallel solver may access only some of 1118 the rows from each processor. The algorithm used here reads 1119 relatively small blocks of data rather than reading the entire 1120 matrix and then subsetting it. 1121 1122 Viewer's PetscViewerType must be either PETSCVIEWERBINARY or PETSCVIEWERHDF5. 1123 Such viewer can be created using PetscViewerBinaryOpen()/PetscViewerHDF5Open(), 1124 or the sequence like 1125 $ PetscViewer v; 1126 $ PetscViewerCreate(PETSC_COMM_WORLD,&v); 1127 $ PetscViewerSetType(v,PETSCVIEWERBINARY); 1128 $ PetscViewerSetFromOptions(v); 1129 $ PetscViewerFileSetMode(v,FILE_MODE_READ); 1130 $ PetscViewerFileSetName(v,"datafile"); 1131 The optional PetscViewerSetFromOptions() call allows to override PetscViewerSetType() using option 1132 $ -viewer_type {binary,hdf5} 1133 1134 See the example src/ksp/ksp/examples/tutorials/ex27.c with the first approach, 1135 and src/mat/examples/tutorials/ex10.c with the second approach. 1136 1137 Notes about the PETSc binary format: 1138 In case of PETSCVIEWERBINARY, a native PETSc binary format is used. Each of the blocks 1139 is read onto rank 0 and then shipped to its destination rank, one after another. 1140 Multiple objects, both matrices and vectors, can be stored within the same file. 1141 Their PetscObject name is ignored; they are loaded in the order of their storage. 1142 1143 Most users should not need to know the details of the binary storage 1144 format, since MatLoad() and MatView() completely hide these details. 1145 But for anyone who's interested, the standard binary matrix storage 1146 format is 1147 1148 $ int MAT_FILE_CLASSID 1149 $ int number of rows 1150 $ int number of columns 1151 $ int total number of nonzeros 1152 $ int *number nonzeros in each row 1153 $ int *column indices of all nonzeros (starting index is zero) 1154 $ PetscScalar *values of all nonzeros 1155 1156 PETSc automatically does the byte swapping for 1157 machines that store the bytes reversed, e.g. DEC alpha, freebsd, 1158 linux, Windows and the paragon; thus if you write your own binary 1159 read/write routines you have to swap the bytes; see PetscBinaryRead() 1160 and PetscBinaryWrite() to see how this may be done. 1161 1162 Notes about the HDF5 (MATLAB MAT-File Version 7.3) format: 1163 In case of PETSCVIEWERHDF5, a parallel HDF5 reader is used. 1164 Each processor's chunk is loaded independently by its owning rank. 1165 Multiple objects, both matrices and vectors, can be stored within the same file. 1166 They are looked up by their PetscObject name. 1167 1168 As the MATLAB MAT-File Version 7.3 format is also a HDF5 flavor, we decided to use 1169 by default the same structure and naming of the AIJ arrays and column count 1170 within the HDF5 file. This means that a MAT file saved with -v7.3 flag, e.g. 1171 $ save example.mat A b -v7.3 1172 can be directly read by this routine (see Reference 1 for details). 1173 Note that depending on your MATLAB version, this format might be a default, 1174 otherwise you can set it as default in Preferences. 1175 1176 Unless -nocompression flag is used to save the file in MATLAB, 1177 PETSc must be configured with ZLIB package. 1178 1179 See also examples src/mat/examples/tutorials/ex10.c and src/ksp/ksp/examples/tutorials/ex27.c 1180 1181 Current HDF5 (MAT-File) limitations: 1182 This reader currently supports only real MATSEQAIJ, MATMPIAIJ, MATSEQDENSE and MATMPIDENSE matrices. 1183 1184 Corresponding MatView() is not yet implemented. 1185 1186 The loaded matrix is actually a transpose of the original one in MATLAB, 1187 unless you push PETSC_VIEWER_HDF5_MAT format (see examples above). 1188 With this format, matrix is automatically transposed by PETSc, 1189 unless the matrix is marked as SPD or symmetric 1190 (see MatSetOption(), MAT_SPD, MAT_SYMMETRIC). 1191 1192 References: 1193 1. MATLAB(R) Documentation, manual page of save(), https://www.mathworks.com/help/matlab/ref/save.html#btox10b-1-version 1194 1195 .seealso: PetscViewerBinaryOpen(), PetscViewerSetType(), MatView(), VecLoad() 1196 1197 @*/ 1198 PetscErrorCode MatLoad(Mat newmat,PetscViewer viewer) 1199 { 1200 PetscErrorCode ierr; 1201 PetscBool flg; 1202 1203 PetscFunctionBegin; 1204 PetscValidHeaderSpecific(newmat,MAT_CLASSID,1); 1205 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1206 1207 if (!((PetscObject)newmat)->type_name) { 1208 ierr = MatSetType(newmat,MATAIJ);CHKERRQ(ierr); 1209 } 1210 1211 flg = PETSC_FALSE; 1212 ierr = PetscOptionsGetBool(((PetscObject)newmat)->options,((PetscObject)newmat)->prefix,"-matload_symmetric",&flg,NULL);CHKERRQ(ierr); 1213 if (flg) { 1214 ierr = MatSetOption(newmat,MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr); 1215 ierr = MatSetOption(newmat,MAT_SYMMETRY_ETERNAL,PETSC_TRUE);CHKERRQ(ierr); 1216 } 1217 flg = PETSC_FALSE; 1218 ierr = PetscOptionsGetBool(((PetscObject)newmat)->options,((PetscObject)newmat)->prefix,"-matload_spd",&flg,NULL);CHKERRQ(ierr); 1219 if (flg) { 1220 ierr = MatSetOption(newmat,MAT_SPD,PETSC_TRUE);CHKERRQ(ierr); 1221 } 1222 1223 if (!newmat->ops->load) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatLoad is not supported for type"); 1224 ierr = PetscLogEventBegin(MAT_Load,viewer,0,0,0);CHKERRQ(ierr); 1225 ierr = (*newmat->ops->load)(newmat,viewer);CHKERRQ(ierr); 1226 ierr = PetscLogEventEnd(MAT_Load,viewer,0,0,0);CHKERRQ(ierr); 1227 PetscFunctionReturn(0); 1228 } 1229 1230 PetscErrorCode MatDestroy_Redundant(Mat_Redundant **redundant) 1231 { 1232 PetscErrorCode ierr; 1233 Mat_Redundant *redund = *redundant; 1234 PetscInt i; 1235 1236 PetscFunctionBegin; 1237 if (redund){ 1238 if (redund->matseq) { /* via MatCreateSubMatrices() */ 1239 ierr = ISDestroy(&redund->isrow);CHKERRQ(ierr); 1240 ierr = ISDestroy(&redund->iscol);CHKERRQ(ierr); 1241 ierr = MatDestroySubMatrices(1,&redund->matseq);CHKERRQ(ierr); 1242 } else { 1243 ierr = PetscFree2(redund->send_rank,redund->recv_rank);CHKERRQ(ierr); 1244 ierr = PetscFree(redund->sbuf_j);CHKERRQ(ierr); 1245 ierr = PetscFree(redund->sbuf_a);CHKERRQ(ierr); 1246 for (i=0; i<redund->nrecvs; i++) { 1247 ierr = PetscFree(redund->rbuf_j[i]);CHKERRQ(ierr); 1248 ierr = PetscFree(redund->rbuf_a[i]);CHKERRQ(ierr); 1249 } 1250 ierr = PetscFree4(redund->sbuf_nz,redund->rbuf_nz,redund->rbuf_j,redund->rbuf_a);CHKERRQ(ierr); 1251 } 1252 1253 if (redund->subcomm) { 1254 ierr = PetscCommDestroy(&redund->subcomm);CHKERRQ(ierr); 1255 } 1256 ierr = PetscFree(redund);CHKERRQ(ierr); 1257 } 1258 PetscFunctionReturn(0); 1259 } 1260 1261 /*@ 1262 MatDestroy - Frees space taken by a matrix. 1263 1264 Collective on Mat 1265 1266 Input Parameter: 1267 . A - the matrix 1268 1269 Level: beginner 1270 1271 @*/ 1272 PetscErrorCode MatDestroy(Mat *A) 1273 { 1274 PetscErrorCode ierr; 1275 1276 PetscFunctionBegin; 1277 if (!*A) PetscFunctionReturn(0); 1278 PetscValidHeaderSpecific(*A,MAT_CLASSID,1); 1279 if (--((PetscObject)(*A))->refct > 0) {*A = NULL; PetscFunctionReturn(0);} 1280 1281 /* if memory was published with SAWs then destroy it */ 1282 ierr = PetscObjectSAWsViewOff((PetscObject)*A);CHKERRQ(ierr); 1283 if ((*A)->ops->destroy) { 1284 ierr = (*(*A)->ops->destroy)(*A);CHKERRQ(ierr); 1285 } 1286 1287 ierr = PetscFree((*A)->defaultvectype);CHKERRQ(ierr); 1288 ierr = PetscFree((*A)->bsizes);CHKERRQ(ierr); 1289 ierr = PetscFree((*A)->solvertype);CHKERRQ(ierr); 1290 ierr = MatDestroy_Redundant(&(*A)->redundant);CHKERRQ(ierr); 1291 ierr = MatNullSpaceDestroy(&(*A)->nullsp);CHKERRQ(ierr); 1292 ierr = MatNullSpaceDestroy(&(*A)->transnullsp);CHKERRQ(ierr); 1293 ierr = MatNullSpaceDestroy(&(*A)->nearnullsp);CHKERRQ(ierr); 1294 ierr = MatDestroy(&(*A)->schur);CHKERRQ(ierr); 1295 ierr = PetscLayoutDestroy(&(*A)->rmap);CHKERRQ(ierr); 1296 ierr = PetscLayoutDestroy(&(*A)->cmap);CHKERRQ(ierr); 1297 ierr = PetscHeaderDestroy(A);CHKERRQ(ierr); 1298 PetscFunctionReturn(0); 1299 } 1300 1301 /*@C 1302 MatSetValues - Inserts or adds a block of values into a matrix. 1303 These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd() 1304 MUST be called after all calls to MatSetValues() have been completed. 1305 1306 Not Collective 1307 1308 Input Parameters: 1309 + mat - the matrix 1310 . v - a logically two-dimensional array of values 1311 . m, idxm - the number of rows and their global indices 1312 . n, idxn - the number of columns and their global indices 1313 - addv - either ADD_VALUES or INSERT_VALUES, where 1314 ADD_VALUES adds values to any existing entries, and 1315 INSERT_VALUES replaces existing entries with new values 1316 1317 Notes: 1318 If you create the matrix yourself (that is not with a call to DMCreateMatrix()) then you MUST call MatXXXXSetPreallocation() or 1319 MatSetUp() before using this routine 1320 1321 By default the values, v, are row-oriented. See MatSetOption() for other options. 1322 1323 Calls to MatSetValues() with the INSERT_VALUES and ADD_VALUES 1324 options cannot be mixed without intervening calls to the assembly 1325 routines. 1326 1327 MatSetValues() uses 0-based row and column numbers in Fortran 1328 as well as in C. 1329 1330 Negative indices may be passed in idxm and idxn, these rows and columns are 1331 simply ignored. This allows easily inserting element stiffness matrices 1332 with homogeneous Dirchlet boundary conditions that you don't want represented 1333 in the matrix. 1334 1335 Efficiency Alert: 1336 The routine MatSetValuesBlocked() may offer much better efficiency 1337 for users of block sparse formats (MATSEQBAIJ and MATMPIBAIJ). 1338 1339 Level: beginner 1340 1341 Developer Notes: 1342 This is labeled with C so does not automatically generate Fortran stubs and interfaces 1343 because it requires multiple Fortran interfaces depending on which arguments are scalar or arrays. 1344 1345 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(), 1346 InsertMode, INSERT_VALUES, ADD_VALUES 1347 @*/ 1348 PetscErrorCode MatSetValues(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],const PetscScalar v[],InsertMode addv) 1349 { 1350 PetscErrorCode ierr; 1351 #if defined(PETSC_USE_DEBUG) 1352 PetscInt i,j; 1353 #endif 1354 1355 PetscFunctionBeginHot; 1356 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 1357 PetscValidType(mat,1); 1358 if (!m || !n) PetscFunctionReturn(0); /* no values to insert */ 1359 PetscValidIntPointer(idxm,3); 1360 PetscValidIntPointer(idxn,5); 1361 MatCheckPreallocated(mat,1); 1362 1363 if (mat->insertmode == NOT_SET_VALUES) { 1364 mat->insertmode = addv; 1365 } 1366 #if defined(PETSC_USE_DEBUG) 1367 else if (mat->insertmode != addv) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values"); 1368 if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1369 if (!mat->ops->setvalues) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 1370 1371 for (i=0; i<m; i++) { 1372 for (j=0; j<n; j++) { 1373 if (mat->erroriffailure && PetscIsInfOrNanScalar(v[i*n+j])) 1374 #if defined(PETSC_USE_COMPLEX) 1375 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]); 1376 #else 1377 SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_FP,"Inserting %g at matrix entry (%D,%D)",(double)v[i*n+j],idxm[i],idxn[j]); 1378 #endif 1379 } 1380 } 1381 #endif 1382 1383 if (mat->assembled) { 1384 mat->was_assembled = PETSC_TRUE; 1385 mat->assembled = PETSC_FALSE; 1386 } 1387 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 1388 ierr = (*mat->ops->setvalues)(mat,m,idxm,n,idxn,v,addv);CHKERRQ(ierr); 1389 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 1390 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 1391 if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) { 1392 mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU; 1393 } 1394 #endif 1395 PetscFunctionReturn(0); 1396 } 1397 1398 1399 /*@ 1400 MatSetValuesRowLocal - Inserts a row (block row for BAIJ matrices) of nonzero 1401 values into a matrix 1402 1403 Not Collective 1404 1405 Input Parameters: 1406 + mat - the matrix 1407 . row - the (block) row to set 1408 - v - a logically two-dimensional array of values 1409 1410 Notes: 1411 By the values, v, are column-oriented (for the block version) and sorted 1412 1413 All the nonzeros in the row must be provided 1414 1415 The matrix must have previously had its column indices set 1416 1417 The row must belong to this process 1418 1419 Level: intermediate 1420 1421 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(), 1422 InsertMode, INSERT_VALUES, ADD_VALUES, MatSetValues(), MatSetValuesRow(), MatSetLocalToGlobalMapping() 1423 @*/ 1424 PetscErrorCode MatSetValuesRowLocal(Mat mat,PetscInt row,const PetscScalar v[]) 1425 { 1426 PetscErrorCode ierr; 1427 PetscInt globalrow; 1428 1429 PetscFunctionBegin; 1430 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 1431 PetscValidType(mat,1); 1432 PetscValidScalarPointer(v,2); 1433 ierr = ISLocalToGlobalMappingApply(mat->rmap->mapping,1,&row,&globalrow);CHKERRQ(ierr); 1434 ierr = MatSetValuesRow(mat,globalrow,v);CHKERRQ(ierr); 1435 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 1436 if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) { 1437 mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU; 1438 } 1439 #endif 1440 PetscFunctionReturn(0); 1441 } 1442 1443 /*@ 1444 MatSetValuesRow - Inserts a row (block row for BAIJ matrices) of nonzero 1445 values into a matrix 1446 1447 Not Collective 1448 1449 Input Parameters: 1450 + mat - the matrix 1451 . row - the (block) row to set 1452 - 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 1453 1454 Notes: 1455 The values, v, are column-oriented for the block version. 1456 1457 All the nonzeros in the row must be provided 1458 1459 THE MATRIX MUST HAVE PREVIOUSLY HAD ITS COLUMN INDICES SET. IT IS RARE THAT THIS ROUTINE IS USED, usually MatSetValues() is used. 1460 1461 The row must belong to this process 1462 1463 Level: advanced 1464 1465 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(), 1466 InsertMode, INSERT_VALUES, ADD_VALUES, MatSetValues() 1467 @*/ 1468 PetscErrorCode MatSetValuesRow(Mat mat,PetscInt row,const PetscScalar v[]) 1469 { 1470 PetscErrorCode ierr; 1471 1472 PetscFunctionBeginHot; 1473 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 1474 PetscValidType(mat,1); 1475 MatCheckPreallocated(mat,1); 1476 PetscValidScalarPointer(v,2); 1477 #if defined(PETSC_USE_DEBUG) 1478 if (mat->insertmode == ADD_VALUES) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add and insert values"); 1479 if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1480 #endif 1481 mat->insertmode = INSERT_VALUES; 1482 1483 if (mat->assembled) { 1484 mat->was_assembled = PETSC_TRUE; 1485 mat->assembled = PETSC_FALSE; 1486 } 1487 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 1488 if (!mat->ops->setvaluesrow) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 1489 ierr = (*mat->ops->setvaluesrow)(mat,row,v);CHKERRQ(ierr); 1490 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 1491 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 1492 if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) { 1493 mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU; 1494 } 1495 #endif 1496 PetscFunctionReturn(0); 1497 } 1498 1499 /*@ 1500 MatSetValuesStencil - Inserts or adds a block of values into a matrix. 1501 Using structured grid indexing 1502 1503 Not Collective 1504 1505 Input Parameters: 1506 + mat - the matrix 1507 . m - number of rows being entered 1508 . idxm - grid coordinates (and component number when dof > 1) for matrix rows being entered 1509 . n - number of columns being entered 1510 . idxn - grid coordinates (and component number when dof > 1) for matrix columns being entered 1511 . v - a logically two-dimensional array of values 1512 - addv - either ADD_VALUES or INSERT_VALUES, where 1513 ADD_VALUES adds values to any existing entries, and 1514 INSERT_VALUES replaces existing entries with new values 1515 1516 Notes: 1517 By default the values, v, are row-oriented. See MatSetOption() for other options. 1518 1519 Calls to MatSetValuesStencil() with the INSERT_VALUES and ADD_VALUES 1520 options cannot be mixed without intervening calls to the assembly 1521 routines. 1522 1523 The grid coordinates are across the entire grid, not just the local portion 1524 1525 MatSetValuesStencil() uses 0-based row and column numbers in Fortran 1526 as well as in C. 1527 1528 For setting/accessing vector values via array coordinates you can use the DMDAVecGetArray() routine 1529 1530 In order to use this routine you must either obtain the matrix with DMCreateMatrix() 1531 or call MatSetLocalToGlobalMapping() and MatSetStencil() first. 1532 1533 The columns and rows in the stencil passed in MUST be contained within the 1534 ghost region of the given process as set with DMDACreateXXX() or MatSetStencil(). For example, 1535 if you create a DMDA with an overlap of one grid level and on a particular process its first 1536 local nonghost x logical coordinate is 6 (so its first ghost x logical coordinate is 5) the 1537 first i index you can use in your column and row indices in MatSetStencil() is 5. 1538 1539 In Fortran idxm and idxn should be declared as 1540 $ MatStencil idxm(4,m),idxn(4,n) 1541 and the values inserted using 1542 $ idxm(MatStencil_i,1) = i 1543 $ idxm(MatStencil_j,1) = j 1544 $ idxm(MatStencil_k,1) = k 1545 $ idxm(MatStencil_c,1) = c 1546 etc 1547 1548 For periodic boundary conditions use negative indices for values to the left (below 0; that are to be 1549 obtained by wrapping values from right edge). For values to the right of the last entry using that index plus one 1550 etc to obtain values that obtained by wrapping the values from the left edge. This does not work for anything but the 1551 DM_BOUNDARY_PERIODIC boundary type. 1552 1553 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 1554 a single value per point) you can skip filling those indices. 1555 1556 Inspired by the structured grid interface to the HYPRE package 1557 (https://computation.llnl.gov/projects/hypre-scalable-linear-solvers-multigrid-methods) 1558 1559 Efficiency Alert: 1560 The routine MatSetValuesBlockedStencil() may offer much better efficiency 1561 for users of block sparse formats (MATSEQBAIJ and MATMPIBAIJ). 1562 1563 Level: beginner 1564 1565 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal() 1566 MatSetValues(), MatSetValuesBlockedStencil(), MatSetStencil(), DMCreateMatrix(), DMDAVecGetArray(), MatStencil 1567 @*/ 1568 PetscErrorCode MatSetValuesStencil(Mat mat,PetscInt m,const MatStencil idxm[],PetscInt n,const MatStencil idxn[],const PetscScalar v[],InsertMode addv) 1569 { 1570 PetscErrorCode ierr; 1571 PetscInt buf[8192],*bufm=0,*bufn=0,*jdxm,*jdxn; 1572 PetscInt j,i,dim = mat->stencil.dim,*dims = mat->stencil.dims+1,tmp; 1573 PetscInt *starts = mat->stencil.starts,*dxm = (PetscInt*)idxm,*dxn = (PetscInt*)idxn,sdim = dim - (1 - (PetscInt)mat->stencil.noc); 1574 1575 PetscFunctionBegin; 1576 if (!m || !n) PetscFunctionReturn(0); /* no values to insert */ 1577 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 1578 PetscValidType(mat,1); 1579 PetscValidIntPointer(idxm,3); 1580 PetscValidIntPointer(idxn,5); 1581 1582 if ((m+n) <= (PetscInt)(sizeof(buf)/sizeof(PetscInt))) { 1583 jdxm = buf; jdxn = buf+m; 1584 } else { 1585 ierr = PetscMalloc2(m,&bufm,n,&bufn);CHKERRQ(ierr); 1586 jdxm = bufm; jdxn = bufn; 1587 } 1588 for (i=0; i<m; i++) { 1589 for (j=0; j<3-sdim; j++) dxm++; 1590 tmp = *dxm++ - starts[0]; 1591 for (j=0; j<dim-1; j++) { 1592 if ((*dxm++ - starts[j+1]) < 0 || tmp < 0) tmp = -1; 1593 else tmp = tmp*dims[j] + *(dxm-1) - starts[j+1]; 1594 } 1595 if (mat->stencil.noc) dxm++; 1596 jdxm[i] = tmp; 1597 } 1598 for (i=0; i<n; i++) { 1599 for (j=0; j<3-sdim; j++) dxn++; 1600 tmp = *dxn++ - starts[0]; 1601 for (j=0; j<dim-1; j++) { 1602 if ((*dxn++ - starts[j+1]) < 0 || tmp < 0) tmp = -1; 1603 else tmp = tmp*dims[j] + *(dxn-1) - starts[j+1]; 1604 } 1605 if (mat->stencil.noc) dxn++; 1606 jdxn[i] = tmp; 1607 } 1608 ierr = MatSetValuesLocal(mat,m,jdxm,n,jdxn,v,addv);CHKERRQ(ierr); 1609 ierr = PetscFree2(bufm,bufn);CHKERRQ(ierr); 1610 PetscFunctionReturn(0); 1611 } 1612 1613 /*@ 1614 MatSetValuesBlockedStencil - Inserts or adds a block of values into a matrix. 1615 Using structured grid indexing 1616 1617 Not Collective 1618 1619 Input Parameters: 1620 + mat - the matrix 1621 . m - number of rows being entered 1622 . idxm - grid coordinates for matrix rows being entered 1623 . n - number of columns being entered 1624 . idxn - grid coordinates for matrix columns being entered 1625 . v - a logically two-dimensional array of values 1626 - addv - either ADD_VALUES or INSERT_VALUES, where 1627 ADD_VALUES adds values to any existing entries, and 1628 INSERT_VALUES replaces existing entries with new values 1629 1630 Notes: 1631 By default the values, v, are row-oriented and unsorted. 1632 See MatSetOption() for other options. 1633 1634 Calls to MatSetValuesBlockedStencil() with the INSERT_VALUES and ADD_VALUES 1635 options cannot be mixed without intervening calls to the assembly 1636 routines. 1637 1638 The grid coordinates are across the entire grid, not just the local portion 1639 1640 MatSetValuesBlockedStencil() uses 0-based row and column numbers in Fortran 1641 as well as in C. 1642 1643 For setting/accessing vector values via array coordinates you can use the DMDAVecGetArray() routine 1644 1645 In order to use this routine you must either obtain the matrix with DMCreateMatrix() 1646 or call MatSetBlockSize(), MatSetLocalToGlobalMapping() and MatSetStencil() first. 1647 1648 The columns and rows in the stencil passed in MUST be contained within the 1649 ghost region of the given process as set with DMDACreateXXX() or MatSetStencil(). For example, 1650 if you create a DMDA with an overlap of one grid level and on a particular process its first 1651 local nonghost x logical coordinate is 6 (so its first ghost x logical coordinate is 5) the 1652 first i index you can use in your column and row indices in MatSetStencil() is 5. 1653 1654 In Fortran idxm and idxn should be declared as 1655 $ MatStencil idxm(4,m),idxn(4,n) 1656 and the values inserted using 1657 $ idxm(MatStencil_i,1) = i 1658 $ idxm(MatStencil_j,1) = j 1659 $ idxm(MatStencil_k,1) = k 1660 etc 1661 1662 Negative indices may be passed in idxm and idxn, these rows and columns are 1663 simply ignored. This allows easily inserting element stiffness matrices 1664 with homogeneous Dirchlet boundary conditions that you don't want represented 1665 in the matrix. 1666 1667 Inspired by the structured grid interface to the HYPRE package 1668 (https://computation.llnl.gov/projects/hypre-scalable-linear-solvers-multigrid-methods) 1669 1670 Level: beginner 1671 1672 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal() 1673 MatSetValues(), MatSetValuesStencil(), MatSetStencil(), DMCreateMatrix(), DMDAVecGetArray(), MatStencil, 1674 MatSetBlockSize(), MatSetLocalToGlobalMapping() 1675 @*/ 1676 PetscErrorCode MatSetValuesBlockedStencil(Mat mat,PetscInt m,const MatStencil idxm[],PetscInt n,const MatStencil idxn[],const PetscScalar v[],InsertMode addv) 1677 { 1678 PetscErrorCode ierr; 1679 PetscInt buf[8192],*bufm=0,*bufn=0,*jdxm,*jdxn; 1680 PetscInt j,i,dim = mat->stencil.dim,*dims = mat->stencil.dims+1,tmp; 1681 PetscInt *starts = mat->stencil.starts,*dxm = (PetscInt*)idxm,*dxn = (PetscInt*)idxn,sdim = dim - (1 - (PetscInt)mat->stencil.noc); 1682 1683 PetscFunctionBegin; 1684 if (!m || !n) PetscFunctionReturn(0); /* no values to insert */ 1685 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 1686 PetscValidType(mat,1); 1687 PetscValidIntPointer(idxm,3); 1688 PetscValidIntPointer(idxn,5); 1689 PetscValidScalarPointer(v,6); 1690 1691 if ((m+n) <= (PetscInt)(sizeof(buf)/sizeof(PetscInt))) { 1692 jdxm = buf; jdxn = buf+m; 1693 } else { 1694 ierr = PetscMalloc2(m,&bufm,n,&bufn);CHKERRQ(ierr); 1695 jdxm = bufm; jdxn = bufn; 1696 } 1697 for (i=0; i<m; i++) { 1698 for (j=0; j<3-sdim; j++) dxm++; 1699 tmp = *dxm++ - starts[0]; 1700 for (j=0; j<sdim-1; j++) { 1701 if ((*dxm++ - starts[j+1]) < 0 || tmp < 0) tmp = -1; 1702 else tmp = tmp*dims[j] + *(dxm-1) - starts[j+1]; 1703 } 1704 dxm++; 1705 jdxm[i] = tmp; 1706 } 1707 for (i=0; i<n; i++) { 1708 for (j=0; j<3-sdim; j++) dxn++; 1709 tmp = *dxn++ - starts[0]; 1710 for (j=0; j<sdim-1; j++) { 1711 if ((*dxn++ - starts[j+1]) < 0 || tmp < 0) tmp = -1; 1712 else tmp = tmp*dims[j] + *(dxn-1) - starts[j+1]; 1713 } 1714 dxn++; 1715 jdxn[i] = tmp; 1716 } 1717 ierr = MatSetValuesBlockedLocal(mat,m,jdxm,n,jdxn,v,addv);CHKERRQ(ierr); 1718 ierr = PetscFree2(bufm,bufn);CHKERRQ(ierr); 1719 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 1720 if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) { 1721 mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU; 1722 } 1723 #endif 1724 PetscFunctionReturn(0); 1725 } 1726 1727 /*@ 1728 MatSetStencil - Sets the grid information for setting values into a matrix via 1729 MatSetValuesStencil() 1730 1731 Not Collective 1732 1733 Input Parameters: 1734 + mat - the matrix 1735 . dim - dimension of the grid 1, 2, or 3 1736 . dims - number of grid points in x, y, and z direction, including ghost points on your processor 1737 . starts - starting point of ghost nodes on your processor in x, y, and z direction 1738 - dof - number of degrees of freedom per node 1739 1740 1741 Inspired by the structured grid interface to the HYPRE package 1742 (www.llnl.gov/CASC/hyper) 1743 1744 For matrices generated with DMCreateMatrix() this routine is automatically called and so not needed by the 1745 user. 1746 1747 Level: beginner 1748 1749 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal() 1750 MatSetValues(), MatSetValuesBlockedStencil(), MatSetValuesStencil() 1751 @*/ 1752 PetscErrorCode MatSetStencil(Mat mat,PetscInt dim,const PetscInt dims[],const PetscInt starts[],PetscInt dof) 1753 { 1754 PetscInt i; 1755 1756 PetscFunctionBegin; 1757 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 1758 PetscValidIntPointer(dims,3); 1759 PetscValidIntPointer(starts,4); 1760 1761 mat->stencil.dim = dim + (dof > 1); 1762 for (i=0; i<dim; i++) { 1763 mat->stencil.dims[i] = dims[dim-i-1]; /* copy the values in backwards */ 1764 mat->stencil.starts[i] = starts[dim-i-1]; 1765 } 1766 mat->stencil.dims[dim] = dof; 1767 mat->stencil.starts[dim] = 0; 1768 mat->stencil.noc = (PetscBool)(dof == 1); 1769 PetscFunctionReturn(0); 1770 } 1771 1772 /*@C 1773 MatSetValuesBlocked - Inserts or adds a block of values into a matrix. 1774 1775 Not Collective 1776 1777 Input Parameters: 1778 + mat - the matrix 1779 . v - a logically two-dimensional array of values 1780 . m, idxm - the number of block rows and their global block indices 1781 . n, idxn - the number of block columns and their global block indices 1782 - addv - either ADD_VALUES or INSERT_VALUES, where 1783 ADD_VALUES adds values to any existing entries, and 1784 INSERT_VALUES replaces existing entries with new values 1785 1786 Notes: 1787 If you create the matrix yourself (that is not with a call to DMCreateMatrix()) then you MUST call 1788 MatXXXXSetPreallocation() or MatSetUp() before using this routine. 1789 1790 The m and n count the NUMBER of blocks in the row direction and column direction, 1791 NOT the total number of rows/columns; for example, if the block size is 2 and 1792 you are passing in values for rows 2,3,4,5 then m would be 2 (not 4). 1793 The values in idxm would be 1 2; that is the first index for each block divided by 1794 the block size. 1795 1796 Note that you must call MatSetBlockSize() when constructing this matrix (before 1797 preallocating it). 1798 1799 By default the values, v, are row-oriented, so the layout of 1800 v is the same as for MatSetValues(). See MatSetOption() for other options. 1801 1802 Calls to MatSetValuesBlocked() with the INSERT_VALUES and ADD_VALUES 1803 options cannot be mixed without intervening calls to the assembly 1804 routines. 1805 1806 MatSetValuesBlocked() uses 0-based row and column numbers in Fortran 1807 as well as in C. 1808 1809 Negative indices may be passed in idxm and idxn, these rows and columns are 1810 simply ignored. This allows easily inserting element stiffness matrices 1811 with homogeneous Dirchlet boundary conditions that you don't want represented 1812 in the matrix. 1813 1814 Each time an entry is set within a sparse matrix via MatSetValues(), 1815 internal searching must be done to determine where to place the 1816 data in the matrix storage space. By instead inserting blocks of 1817 entries via MatSetValuesBlocked(), the overhead of matrix assembly is 1818 reduced. 1819 1820 Example: 1821 $ Suppose m=n=2 and block size(bs) = 2 The array is 1822 $ 1823 $ 1 2 | 3 4 1824 $ 5 6 | 7 8 1825 $ - - - | - - - 1826 $ 9 10 | 11 12 1827 $ 13 14 | 15 16 1828 $ 1829 $ v[] should be passed in like 1830 $ v[] = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16] 1831 $ 1832 $ If you are not using row oriented storage of v (that is you called MatSetOption(mat,MAT_ROW_ORIENTED,PETSC_FALSE)) then 1833 $ v[] = [1,5,9,13,2,6,10,14,3,7,11,15,4,8,12,16] 1834 1835 Level: intermediate 1836 1837 .seealso: MatSetBlockSize(), MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesBlockedLocal() 1838 @*/ 1839 PetscErrorCode MatSetValuesBlocked(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],const PetscScalar v[],InsertMode addv) 1840 { 1841 PetscErrorCode ierr; 1842 1843 PetscFunctionBeginHot; 1844 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 1845 PetscValidType(mat,1); 1846 if (!m || !n) PetscFunctionReturn(0); /* no values to insert */ 1847 PetscValidIntPointer(idxm,3); 1848 PetscValidIntPointer(idxn,5); 1849 PetscValidScalarPointer(v,6); 1850 MatCheckPreallocated(mat,1); 1851 if (mat->insertmode == NOT_SET_VALUES) { 1852 mat->insertmode = addv; 1853 } 1854 #if defined(PETSC_USE_DEBUG) 1855 else if (mat->insertmode != addv) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values"); 1856 if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1857 if (!mat->ops->setvaluesblocked && !mat->ops->setvalues) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 1858 #endif 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=0,*bufc=0,*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 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 1892 if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) { 1893 mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU; 1894 } 1895 #endif 1896 PetscFunctionReturn(0); 1897 } 1898 1899 /*@ 1900 MatGetValues - Gets a block of values from a matrix. 1901 1902 Not Collective; currently only returns a local block 1903 1904 Input Parameters: 1905 + mat - the matrix 1906 . v - a logically two-dimensional array for storing the values 1907 . m, idxm - the number of rows and their global indices 1908 - n, idxn - the number of columns and their global indices 1909 1910 Notes: 1911 The user must allocate space (m*n PetscScalars) for the values, v. 1912 The values, v, are then returned in a row-oriented format, 1913 analogous to that used by default in MatSetValues(). 1914 1915 MatGetValues() uses 0-based row and column numbers in 1916 Fortran as well as in C. 1917 1918 MatGetValues() requires that the matrix has been assembled 1919 with MatAssemblyBegin()/MatAssemblyEnd(). Thus, calls to 1920 MatSetValues() and MatGetValues() CANNOT be made in succession 1921 without intermediate matrix assembly. 1922 1923 Negative row or column indices will be ignored and those locations in v[] will be 1924 left unchanged. 1925 1926 Level: advanced 1927 1928 .seealso: MatGetRow(), MatCreateSubMatrices(), MatSetValues() 1929 @*/ 1930 PetscErrorCode MatGetValues(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],PetscScalar v[]) 1931 { 1932 PetscErrorCode ierr; 1933 1934 PetscFunctionBegin; 1935 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 1936 PetscValidType(mat,1); 1937 if (!m || !n) PetscFunctionReturn(0); 1938 PetscValidIntPointer(idxm,3); 1939 PetscValidIntPointer(idxn,5); 1940 PetscValidScalarPointer(v,6); 1941 if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1942 if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1943 if (!mat->ops->getvalues) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 1944 MatCheckPreallocated(mat,1); 1945 1946 ierr = PetscLogEventBegin(MAT_GetValues,mat,0,0,0);CHKERRQ(ierr); 1947 ierr = (*mat->ops->getvalues)(mat,m,idxm,n,idxn,v);CHKERRQ(ierr); 1948 ierr = PetscLogEventEnd(MAT_GetValues,mat,0,0,0);CHKERRQ(ierr); 1949 PetscFunctionReturn(0); 1950 } 1951 1952 /*@ 1953 MatSetValuesBatch - Adds (ADD_VALUES) many blocks of values into a matrix at once. The blocks must all be square and 1954 the same size. Currently, this can only be called once and creates the given matrix. 1955 1956 Not Collective 1957 1958 Input Parameters: 1959 + mat - the matrix 1960 . nb - the number of blocks 1961 . bs - the number of rows (and columns) in each block 1962 . rows - a concatenation of the rows for each block 1963 - v - a concatenation of logically two-dimensional arrays of values 1964 1965 Notes: 1966 In the future, we will extend this routine to handle rectangular blocks, and to allow multiple calls for a given matrix. 1967 1968 Level: advanced 1969 1970 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(), 1971 InsertMode, INSERT_VALUES, ADD_VALUES, MatSetValues() 1972 @*/ 1973 PetscErrorCode MatSetValuesBatch(Mat mat, PetscInt nb, PetscInt bs, PetscInt rows[], const PetscScalar v[]) 1974 { 1975 PetscErrorCode ierr; 1976 1977 PetscFunctionBegin; 1978 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 1979 PetscValidType(mat,1); 1980 PetscValidScalarPointer(rows,4); 1981 PetscValidScalarPointer(v,5); 1982 #if defined(PETSC_USE_DEBUG) 1983 if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1984 #endif 1985 1986 ierr = PetscLogEventBegin(MAT_SetValuesBatch,mat,0,0,0);CHKERRQ(ierr); 1987 if (mat->ops->setvaluesbatch) { 1988 ierr = (*mat->ops->setvaluesbatch)(mat,nb,bs,rows,v);CHKERRQ(ierr); 1989 } else { 1990 PetscInt b; 1991 for (b = 0; b < nb; ++b) { 1992 ierr = MatSetValues(mat, bs, &rows[b*bs], bs, &rows[b*bs], &v[b*bs*bs], ADD_VALUES);CHKERRQ(ierr); 1993 } 1994 } 1995 ierr = PetscLogEventEnd(MAT_SetValuesBatch,mat,0,0,0);CHKERRQ(ierr); 1996 PetscFunctionReturn(0); 1997 } 1998 1999 /*@ 2000 MatSetLocalToGlobalMapping - Sets a local-to-global numbering for use by 2001 the routine MatSetValuesLocal() to allow users to insert matrix entries 2002 using a local (per-processor) numbering. 2003 2004 Not Collective 2005 2006 Input Parameters: 2007 + x - the matrix 2008 . rmapping - row mapping created with ISLocalToGlobalMappingCreate() or ISLocalToGlobalMappingCreateIS() 2009 - cmapping - column mapping 2010 2011 Level: intermediate 2012 2013 2014 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesLocal() 2015 @*/ 2016 PetscErrorCode MatSetLocalToGlobalMapping(Mat x,ISLocalToGlobalMapping rmapping,ISLocalToGlobalMapping cmapping) 2017 { 2018 PetscErrorCode ierr; 2019 2020 PetscFunctionBegin; 2021 PetscValidHeaderSpecific(x,MAT_CLASSID,1); 2022 PetscValidType(x,1); 2023 PetscValidHeaderSpecific(rmapping,IS_LTOGM_CLASSID,2); 2024 PetscValidHeaderSpecific(cmapping,IS_LTOGM_CLASSID,3); 2025 2026 if (x->ops->setlocaltoglobalmapping) { 2027 ierr = (*x->ops->setlocaltoglobalmapping)(x,rmapping,cmapping);CHKERRQ(ierr); 2028 } else { 2029 ierr = PetscLayoutSetISLocalToGlobalMapping(x->rmap,rmapping);CHKERRQ(ierr); 2030 ierr = PetscLayoutSetISLocalToGlobalMapping(x->cmap,cmapping);CHKERRQ(ierr); 2031 } 2032 PetscFunctionReturn(0); 2033 } 2034 2035 2036 /*@ 2037 MatGetLocalToGlobalMapping - Gets the local-to-global numbering set by MatSetLocalToGlobalMapping() 2038 2039 Not Collective 2040 2041 Input Parameters: 2042 . A - the matrix 2043 2044 Output Parameters: 2045 + rmapping - row mapping 2046 - cmapping - column mapping 2047 2048 Level: advanced 2049 2050 2051 .seealso: MatSetValuesLocal() 2052 @*/ 2053 PetscErrorCode MatGetLocalToGlobalMapping(Mat A,ISLocalToGlobalMapping *rmapping,ISLocalToGlobalMapping *cmapping) 2054 { 2055 PetscFunctionBegin; 2056 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 2057 PetscValidType(A,1); 2058 if (rmapping) PetscValidPointer(rmapping,2); 2059 if (cmapping) PetscValidPointer(cmapping,3); 2060 if (rmapping) *rmapping = A->rmap->mapping; 2061 if (cmapping) *cmapping = A->cmap->mapping; 2062 PetscFunctionReturn(0); 2063 } 2064 2065 /*@ 2066 MatGetLayouts - Gets the PetscLayout objects for rows and columns 2067 2068 Not Collective 2069 2070 Input Parameters: 2071 . A - the matrix 2072 2073 Output Parameters: 2074 + rmap - row layout 2075 - cmap - column layout 2076 2077 Level: advanced 2078 2079 .seealso: MatCreateVecs(), MatGetLocalToGlobalMapping() 2080 @*/ 2081 PetscErrorCode MatGetLayouts(Mat A,PetscLayout *rmap,PetscLayout *cmap) 2082 { 2083 PetscFunctionBegin; 2084 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 2085 PetscValidType(A,1); 2086 if (rmap) PetscValidPointer(rmap,2); 2087 if (cmap) PetscValidPointer(cmap,3); 2088 if (rmap) *rmap = A->rmap; 2089 if (cmap) *cmap = A->cmap; 2090 PetscFunctionReturn(0); 2091 } 2092 2093 /*@C 2094 MatSetValuesLocal - Inserts or adds values into certain locations of a matrix, 2095 using a local ordering of the nodes. 2096 2097 Not Collective 2098 2099 Input Parameters: 2100 + mat - the matrix 2101 . nrow, irow - number of rows and their local indices 2102 . ncol, icol - number of columns and their local indices 2103 . y - a logically two-dimensional array of values 2104 - addv - either INSERT_VALUES or ADD_VALUES, where 2105 ADD_VALUES adds values to any existing entries, and 2106 INSERT_VALUES replaces existing entries with new values 2107 2108 Notes: 2109 If you create the matrix yourself (that is not with a call to DMCreateMatrix()) then you MUST call MatXXXXSetPreallocation() or 2110 MatSetUp() before using this routine 2111 2112 If you create the matrix yourself (that is not with a call to DMCreateMatrix()) then you MUST call MatSetLocalToGlobalMapping() before using this routine 2113 2114 Calls to MatSetValuesLocal() with the INSERT_VALUES and ADD_VALUES 2115 options cannot be mixed without intervening calls to the assembly 2116 routines. 2117 2118 These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd() 2119 MUST be called after all calls to MatSetValuesLocal() have been completed. 2120 2121 Level: intermediate 2122 2123 Developer Notes: 2124 This is labeled with C so does not automatically generate Fortran stubs and interfaces 2125 because it requires multiple Fortran interfaces depending on which arguments are scalar or arrays. 2126 2127 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetLocalToGlobalMapping(), 2128 MatSetValueLocal() 2129 @*/ 2130 PetscErrorCode MatSetValuesLocal(Mat mat,PetscInt nrow,const PetscInt irow[],PetscInt ncol,const PetscInt icol[],const PetscScalar y[],InsertMode addv) 2131 { 2132 PetscErrorCode ierr; 2133 2134 PetscFunctionBeginHot; 2135 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2136 PetscValidType(mat,1); 2137 MatCheckPreallocated(mat,1); 2138 if (!nrow || !ncol) PetscFunctionReturn(0); /* no values to insert */ 2139 PetscValidIntPointer(irow,3); 2140 PetscValidIntPointer(icol,5); 2141 if (mat->insertmode == NOT_SET_VALUES) { 2142 mat->insertmode = addv; 2143 } 2144 #if defined(PETSC_USE_DEBUG) 2145 else if (mat->insertmode != addv) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values"); 2146 if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2147 if (!mat->ops->setvalueslocal && !mat->ops->setvalues) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 2148 #endif 2149 2150 if (mat->assembled) { 2151 mat->was_assembled = PETSC_TRUE; 2152 mat->assembled = PETSC_FALSE; 2153 } 2154 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 2155 if (mat->ops->setvalueslocal) { 2156 ierr = (*mat->ops->setvalueslocal)(mat,nrow,irow,ncol,icol,y,addv);CHKERRQ(ierr); 2157 } else { 2158 PetscInt buf[8192],*bufr=0,*bufc=0,*irowm,*icolm; 2159 if ((nrow+ncol) <= (PetscInt)(sizeof(buf)/sizeof(PetscInt))) { 2160 irowm = buf; icolm = buf+nrow; 2161 } else { 2162 ierr = PetscMalloc2(nrow,&bufr,ncol,&bufc);CHKERRQ(ierr); 2163 irowm = bufr; icolm = bufc; 2164 } 2165 ierr = ISLocalToGlobalMappingApply(mat->rmap->mapping,nrow,irow,irowm);CHKERRQ(ierr); 2166 ierr = ISLocalToGlobalMappingApply(mat->cmap->mapping,ncol,icol,icolm);CHKERRQ(ierr); 2167 ierr = MatSetValues(mat,nrow,irowm,ncol,icolm,y,addv);CHKERRQ(ierr); 2168 ierr = PetscFree2(bufr,bufc);CHKERRQ(ierr); 2169 } 2170 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 2171 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 2172 if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) { 2173 mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU; 2174 } 2175 #endif 2176 PetscFunctionReturn(0); 2177 } 2178 2179 /*@C 2180 MatSetValuesBlockedLocal - Inserts or adds values into certain locations of a matrix, 2181 using a local ordering of the nodes a block at a time. 2182 2183 Not Collective 2184 2185 Input Parameters: 2186 + x - the matrix 2187 . nrow, irow - number of rows and their local indices 2188 . ncol, icol - number of columns and their local indices 2189 . y - a logically two-dimensional array of values 2190 - addv - either INSERT_VALUES or ADD_VALUES, where 2191 ADD_VALUES adds values to any existing entries, and 2192 INSERT_VALUES replaces existing entries with new values 2193 2194 Notes: 2195 If you create the matrix yourself (that is not with a call to DMCreateMatrix()) then you MUST call MatXXXXSetPreallocation() or 2196 MatSetUp() before using this routine 2197 2198 If you create the matrix yourself (that is not with a call to DMCreateMatrix()) then you MUST call MatSetBlockSize() and MatSetLocalToGlobalMapping() 2199 before using this routineBefore calling MatSetValuesLocal(), the user must first set the 2200 2201 Calls to MatSetValuesBlockedLocal() with the INSERT_VALUES and ADD_VALUES 2202 options cannot be mixed without intervening calls to the assembly 2203 routines. 2204 2205 These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd() 2206 MUST be called after all calls to MatSetValuesBlockedLocal() have been completed. 2207 2208 Level: intermediate 2209 2210 Developer Notes: 2211 This is labeled with C so does not automatically generate Fortran stubs and interfaces 2212 because it requires multiple Fortran interfaces depending on which arguments are scalar or arrays. 2213 2214 .seealso: MatSetBlockSize(), MatSetLocalToGlobalMapping(), MatAssemblyBegin(), MatAssemblyEnd(), 2215 MatSetValuesLocal(), MatSetValuesBlocked() 2216 @*/ 2217 PetscErrorCode MatSetValuesBlockedLocal(Mat mat,PetscInt nrow,const PetscInt irow[],PetscInt ncol,const PetscInt icol[],const PetscScalar y[],InsertMode addv) 2218 { 2219 PetscErrorCode ierr; 2220 2221 PetscFunctionBeginHot; 2222 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2223 PetscValidType(mat,1); 2224 MatCheckPreallocated(mat,1); 2225 if (!nrow || !ncol) PetscFunctionReturn(0); /* no values to insert */ 2226 PetscValidIntPointer(irow,3); 2227 PetscValidIntPointer(icol,5); 2228 PetscValidScalarPointer(y,6); 2229 if (mat->insertmode == NOT_SET_VALUES) { 2230 mat->insertmode = addv; 2231 } 2232 #if defined(PETSC_USE_DEBUG) 2233 else if (mat->insertmode != addv) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values"); 2234 if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2235 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); 2236 #endif 2237 2238 if (mat->assembled) { 2239 mat->was_assembled = PETSC_TRUE; 2240 mat->assembled = PETSC_FALSE; 2241 } 2242 #if defined(PETSC_USE_DEBUG) 2243 /* Condition on the mapping existing, because MatSetValuesBlockedLocal_IS does not require it to be set. */ 2244 if (mat->rmap->mapping) { 2245 PetscInt irbs, rbs; 2246 ierr = MatGetBlockSizes(mat, &rbs, NULL);CHKERRQ(ierr); 2247 ierr = ISLocalToGlobalMappingGetBlockSize(mat->rmap->mapping,&irbs);CHKERRQ(ierr); 2248 if (rbs != irbs) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Different row block sizes! mat %D, row l2g map %D",rbs,irbs); 2249 } 2250 if (mat->cmap->mapping) { 2251 PetscInt icbs, cbs; 2252 ierr = MatGetBlockSizes(mat,NULL,&cbs);CHKERRQ(ierr); 2253 ierr = ISLocalToGlobalMappingGetBlockSize(mat->cmap->mapping,&icbs);CHKERRQ(ierr); 2254 if (cbs != icbs) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Different col block sizes! mat %D, col l2g map %D",cbs,icbs); 2255 } 2256 #endif 2257 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 2258 if (mat->ops->setvaluesblockedlocal) { 2259 ierr = (*mat->ops->setvaluesblockedlocal)(mat,nrow,irow,ncol,icol,y,addv);CHKERRQ(ierr); 2260 } else { 2261 PetscInt buf[8192],*bufr=0,*bufc=0,*irowm,*icolm; 2262 if ((nrow+ncol) <= (PetscInt)(sizeof(buf)/sizeof(PetscInt))) { 2263 irowm = buf; icolm = buf + nrow; 2264 } else { 2265 ierr = PetscMalloc2(nrow,&bufr,ncol,&bufc);CHKERRQ(ierr); 2266 irowm = bufr; icolm = bufc; 2267 } 2268 ierr = ISLocalToGlobalMappingApplyBlock(mat->rmap->mapping,nrow,irow,irowm);CHKERRQ(ierr); 2269 ierr = ISLocalToGlobalMappingApplyBlock(mat->cmap->mapping,ncol,icol,icolm);CHKERRQ(ierr); 2270 ierr = MatSetValuesBlocked(mat,nrow,irowm,ncol,icolm,y,addv);CHKERRQ(ierr); 2271 ierr = PetscFree2(bufr,bufc);CHKERRQ(ierr); 2272 } 2273 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 2274 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 2275 if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) { 2276 mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU; 2277 } 2278 #endif 2279 PetscFunctionReturn(0); 2280 } 2281 2282 /*@ 2283 MatMultDiagonalBlock - Computes the matrix-vector product, y = Dx. Where D is defined by the inode or block structure of the diagonal 2284 2285 Collective on Mat 2286 2287 Input Parameters: 2288 + mat - the matrix 2289 - x - the vector to be multiplied 2290 2291 Output Parameters: 2292 . y - the result 2293 2294 Notes: 2295 The vectors x and y cannot be the same. I.e., one cannot 2296 call MatMult(A,y,y). 2297 2298 Level: developer 2299 2300 .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd() 2301 @*/ 2302 PetscErrorCode MatMultDiagonalBlock(Mat mat,Vec x,Vec y) 2303 { 2304 PetscErrorCode ierr; 2305 2306 PetscFunctionBegin; 2307 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2308 PetscValidType(mat,1); 2309 PetscValidHeaderSpecific(x,VEC_CLASSID,2); 2310 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 2311 2312 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2313 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2314 if (x == y) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors"); 2315 MatCheckPreallocated(mat,1); 2316 2317 if (!mat->ops->multdiagonalblock) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"This matrix type does not have a multiply defined"); 2318 ierr = (*mat->ops->multdiagonalblock)(mat,x,y);CHKERRQ(ierr); 2319 ierr = PetscObjectStateIncrease((PetscObject)y);CHKERRQ(ierr); 2320 PetscFunctionReturn(0); 2321 } 2322 2323 /* --------------------------------------------------------*/ 2324 /*@ 2325 MatMult - Computes the matrix-vector product, y = Ax. 2326 2327 Neighbor-wise Collective on Mat 2328 2329 Input Parameters: 2330 + mat - the matrix 2331 - x - the vector to be multiplied 2332 2333 Output Parameters: 2334 . y - the result 2335 2336 Notes: 2337 The vectors x and y cannot be the same. I.e., one cannot 2338 call MatMult(A,y,y). 2339 2340 Level: beginner 2341 2342 .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd() 2343 @*/ 2344 PetscErrorCode MatMult(Mat mat,Vec x,Vec y) 2345 { 2346 PetscErrorCode ierr; 2347 2348 PetscFunctionBegin; 2349 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2350 PetscValidType(mat,1); 2351 PetscValidHeaderSpecific(x,VEC_CLASSID,2); 2352 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 2353 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2354 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2355 if (x == y) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors"); 2356 #if !defined(PETSC_HAVE_CONSTRAINTS) 2357 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); 2358 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); 2359 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); 2360 #endif 2361 ierr = VecSetErrorIfLocked(y,3);CHKERRQ(ierr); 2362 if (mat->erroriffailure) {ierr = VecValidValues(x,2,PETSC_TRUE);CHKERRQ(ierr);} 2363 MatCheckPreallocated(mat,1); 2364 2365 ierr = VecLockReadPush(x);CHKERRQ(ierr); 2366 if (!mat->ops->mult) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"This matrix type does not have a multiply defined"); 2367 ierr = PetscLogEventBegin(MAT_Mult,mat,x,y,0);CHKERRQ(ierr); 2368 ierr = (*mat->ops->mult)(mat,x,y);CHKERRQ(ierr); 2369 ierr = PetscLogEventEnd(MAT_Mult,mat,x,y,0);CHKERRQ(ierr); 2370 if (mat->erroriffailure) {ierr = VecValidValues(y,3,PETSC_FALSE);CHKERRQ(ierr);} 2371 ierr = VecLockReadPop(x);CHKERRQ(ierr); 2372 PetscFunctionReturn(0); 2373 } 2374 2375 /*@ 2376 MatMultTranspose - Computes matrix transpose times a vector y = A^T * x. 2377 2378 Neighbor-wise Collective on Mat 2379 2380 Input Parameters: 2381 + mat - the matrix 2382 - x - the vector to be multiplied 2383 2384 Output Parameters: 2385 . y - the result 2386 2387 Notes: 2388 The vectors x and y cannot be the same. I.e., one cannot 2389 call MatMultTranspose(A,y,y). 2390 2391 For complex numbers this does NOT compute the Hermitian (complex conjugate) transpose multiple, 2392 use MatMultHermitianTranspose() 2393 2394 Level: beginner 2395 2396 .seealso: MatMult(), MatMultAdd(), MatMultTransposeAdd(), MatMultHermitianTranspose(), MatTranspose() 2397 @*/ 2398 PetscErrorCode MatMultTranspose(Mat mat,Vec x,Vec y) 2399 { 2400 PetscErrorCode ierr; 2401 2402 PetscFunctionBegin; 2403 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2404 PetscValidType(mat,1); 2405 PetscValidHeaderSpecific(x,VEC_CLASSID,2); 2406 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 2407 2408 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2409 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2410 if (x == y) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors"); 2411 #if !defined(PETSC_HAVE_CONSTRAINTS) 2412 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); 2413 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); 2414 #endif 2415 if (mat->erroriffailure) {ierr = VecValidValues(x,2,PETSC_TRUE);CHKERRQ(ierr);} 2416 MatCheckPreallocated(mat,1); 2417 2418 if (!mat->ops->multtranspose) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"This matrix type does not have a multiply transpose defined"); 2419 ierr = PetscLogEventBegin(MAT_MultTranspose,mat,x,y,0);CHKERRQ(ierr); 2420 ierr = VecLockReadPush(x);CHKERRQ(ierr); 2421 ierr = (*mat->ops->multtranspose)(mat,x,y);CHKERRQ(ierr); 2422 ierr = VecLockReadPop(x);CHKERRQ(ierr); 2423 ierr = PetscLogEventEnd(MAT_MultTranspose,mat,x,y,0);CHKERRQ(ierr); 2424 ierr = PetscObjectStateIncrease((PetscObject)y);CHKERRQ(ierr); 2425 if (mat->erroriffailure) {ierr = VecValidValues(y,3,PETSC_FALSE);CHKERRQ(ierr);} 2426 PetscFunctionReturn(0); 2427 } 2428 2429 /*@ 2430 MatMultHermitianTranspose - Computes matrix Hermitian transpose times a vector. 2431 2432 Neighbor-wise Collective on Mat 2433 2434 Input Parameters: 2435 + mat - the matrix 2436 - x - the vector to be multilplied 2437 2438 Output Parameters: 2439 . y - the result 2440 2441 Notes: 2442 The vectors x and y cannot be the same. I.e., one cannot 2443 call MatMultHermitianTranspose(A,y,y). 2444 2445 Also called the conjugate transpose, complex conjugate transpose, or adjoint. 2446 2447 For real numbers MatMultTranspose() and MatMultHermitianTranspose() are identical. 2448 2449 Level: beginner 2450 2451 .seealso: MatMult(), MatMultAdd(), MatMultHermitianTransposeAdd(), MatMultTranspose() 2452 @*/ 2453 PetscErrorCode MatMultHermitianTranspose(Mat mat,Vec x,Vec y) 2454 { 2455 PetscErrorCode ierr; 2456 Vec w; 2457 2458 PetscFunctionBegin; 2459 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2460 PetscValidType(mat,1); 2461 PetscValidHeaderSpecific(x,VEC_CLASSID,2); 2462 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 2463 2464 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2465 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2466 if (x == y) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors"); 2467 #if !defined(PETSC_HAVE_CONSTRAINTS) 2468 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); 2469 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); 2470 #endif 2471 MatCheckPreallocated(mat,1); 2472 2473 ierr = PetscLogEventBegin(MAT_MultHermitianTranspose,mat,x,y,0);CHKERRQ(ierr); 2474 if (mat->ops->multhermitiantranspose) { 2475 ierr = VecLockReadPush(x);CHKERRQ(ierr); 2476 ierr = (*mat->ops->multhermitiantranspose)(mat,x,y);CHKERRQ(ierr); 2477 ierr = VecLockReadPop(x);CHKERRQ(ierr); 2478 } else { 2479 ierr = VecDuplicate(x,&w);CHKERRQ(ierr); 2480 ierr = VecCopy(x,w);CHKERRQ(ierr); 2481 ierr = VecConjugate(w);CHKERRQ(ierr); 2482 ierr = MatMultTranspose(mat,w,y);CHKERRQ(ierr); 2483 ierr = VecDestroy(&w);CHKERRQ(ierr); 2484 ierr = VecConjugate(y);CHKERRQ(ierr); 2485 } 2486 ierr = PetscLogEventEnd(MAT_MultHermitianTranspose,mat,x,y,0);CHKERRQ(ierr); 2487 ierr = PetscObjectStateIncrease((PetscObject)y);CHKERRQ(ierr); 2488 PetscFunctionReturn(0); 2489 } 2490 2491 /*@ 2492 MatMultAdd - Computes v3 = v2 + A * v1. 2493 2494 Neighbor-wise Collective on Mat 2495 2496 Input Parameters: 2497 + mat - the matrix 2498 - v1, v2 - the vectors 2499 2500 Output Parameters: 2501 . v3 - the result 2502 2503 Notes: 2504 The vectors v1 and v3 cannot be the same. I.e., one cannot 2505 call MatMultAdd(A,v1,v2,v1). 2506 2507 Level: beginner 2508 2509 .seealso: MatMultTranspose(), MatMult(), MatMultTransposeAdd() 2510 @*/ 2511 PetscErrorCode MatMultAdd(Mat mat,Vec v1,Vec v2,Vec v3) 2512 { 2513 PetscErrorCode ierr; 2514 2515 PetscFunctionBegin; 2516 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2517 PetscValidType(mat,1); 2518 PetscValidHeaderSpecific(v1,VEC_CLASSID,2); 2519 PetscValidHeaderSpecific(v2,VEC_CLASSID,3); 2520 PetscValidHeaderSpecific(v3,VEC_CLASSID,4); 2521 2522 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2523 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2524 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); 2525 /* 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); 2526 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); */ 2527 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); 2528 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); 2529 if (v1 == v3) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"v1 and v3 must be different vectors"); 2530 MatCheckPreallocated(mat,1); 2531 2532 if (!mat->ops->multadd) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"No MatMultAdd() for matrix type '%s'",((PetscObject)mat)->type_name); 2533 ierr = PetscLogEventBegin(MAT_MultAdd,mat,v1,v2,v3);CHKERRQ(ierr); 2534 ierr = VecLockReadPush(v1);CHKERRQ(ierr); 2535 ierr = (*mat->ops->multadd)(mat,v1,v2,v3);CHKERRQ(ierr); 2536 ierr = VecLockReadPop(v1);CHKERRQ(ierr); 2537 ierr = PetscLogEventEnd(MAT_MultAdd,mat,v1,v2,v3);CHKERRQ(ierr); 2538 ierr = PetscObjectStateIncrease((PetscObject)v3);CHKERRQ(ierr); 2539 PetscFunctionReturn(0); 2540 } 2541 2542 /*@ 2543 MatMultTransposeAdd - Computes v3 = v2 + A' * v1. 2544 2545 Neighbor-wise Collective on Mat 2546 2547 Input Parameters: 2548 + mat - the matrix 2549 - v1, v2 - the vectors 2550 2551 Output Parameters: 2552 . v3 - the result 2553 2554 Notes: 2555 The vectors v1 and v3 cannot be the same. I.e., one cannot 2556 call MatMultTransposeAdd(A,v1,v2,v1). 2557 2558 Level: beginner 2559 2560 .seealso: MatMultTranspose(), MatMultAdd(), MatMult() 2561 @*/ 2562 PetscErrorCode MatMultTransposeAdd(Mat mat,Vec v1,Vec v2,Vec v3) 2563 { 2564 PetscErrorCode ierr; 2565 2566 PetscFunctionBegin; 2567 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2568 PetscValidType(mat,1); 2569 PetscValidHeaderSpecific(v1,VEC_CLASSID,2); 2570 PetscValidHeaderSpecific(v2,VEC_CLASSID,3); 2571 PetscValidHeaderSpecific(v3,VEC_CLASSID,4); 2572 2573 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2574 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2575 if (!mat->ops->multtransposeadd) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 2576 if (v1 == v3) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"v1 and v3 must be different vectors"); 2577 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); 2578 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); 2579 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); 2580 MatCheckPreallocated(mat,1); 2581 2582 ierr = PetscLogEventBegin(MAT_MultTransposeAdd,mat,v1,v2,v3);CHKERRQ(ierr); 2583 ierr = VecLockReadPush(v1);CHKERRQ(ierr); 2584 ierr = (*mat->ops->multtransposeadd)(mat,v1,v2,v3);CHKERRQ(ierr); 2585 ierr = VecLockReadPop(v1);CHKERRQ(ierr); 2586 ierr = PetscLogEventEnd(MAT_MultTransposeAdd,mat,v1,v2,v3);CHKERRQ(ierr); 2587 ierr = PetscObjectStateIncrease((PetscObject)v3);CHKERRQ(ierr); 2588 PetscFunctionReturn(0); 2589 } 2590 2591 /*@ 2592 MatMultHermitianTransposeAdd - Computes v3 = v2 + A^H * v1. 2593 2594 Neighbor-wise Collective on Mat 2595 2596 Input Parameters: 2597 + mat - the matrix 2598 - v1, v2 - the vectors 2599 2600 Output Parameters: 2601 . v3 - the result 2602 2603 Notes: 2604 The vectors v1 and v3 cannot be the same. I.e., one cannot 2605 call MatMultHermitianTransposeAdd(A,v1,v2,v1). 2606 2607 Level: beginner 2608 2609 .seealso: MatMultHermitianTranspose(), MatMultTranspose(), MatMultAdd(), MatMult() 2610 @*/ 2611 PetscErrorCode MatMultHermitianTransposeAdd(Mat mat,Vec v1,Vec v2,Vec v3) 2612 { 2613 PetscErrorCode ierr; 2614 2615 PetscFunctionBegin; 2616 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2617 PetscValidType(mat,1); 2618 PetscValidHeaderSpecific(v1,VEC_CLASSID,2); 2619 PetscValidHeaderSpecific(v2,VEC_CLASSID,3); 2620 PetscValidHeaderSpecific(v3,VEC_CLASSID,4); 2621 2622 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2623 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2624 if (v1 == v3) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"v1 and v3 must be different vectors"); 2625 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); 2626 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); 2627 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); 2628 MatCheckPreallocated(mat,1); 2629 2630 ierr = PetscLogEventBegin(MAT_MultHermitianTransposeAdd,mat,v1,v2,v3);CHKERRQ(ierr); 2631 ierr = VecLockReadPush(v1);CHKERRQ(ierr); 2632 if (mat->ops->multhermitiantransposeadd) { 2633 ierr = (*mat->ops->multhermitiantransposeadd)(mat,v1,v2,v3);CHKERRQ(ierr); 2634 } else { 2635 Vec w,z; 2636 ierr = VecDuplicate(v1,&w);CHKERRQ(ierr); 2637 ierr = VecCopy(v1,w);CHKERRQ(ierr); 2638 ierr = VecConjugate(w);CHKERRQ(ierr); 2639 ierr = VecDuplicate(v3,&z);CHKERRQ(ierr); 2640 ierr = MatMultTranspose(mat,w,z);CHKERRQ(ierr); 2641 ierr = VecDestroy(&w);CHKERRQ(ierr); 2642 ierr = VecConjugate(z);CHKERRQ(ierr); 2643 if (v2 != v3) { 2644 ierr = VecWAXPY(v3,1.0,v2,z);CHKERRQ(ierr); 2645 } else { 2646 ierr = VecAXPY(v3,1.0,z);CHKERRQ(ierr); 2647 } 2648 ierr = VecDestroy(&z);CHKERRQ(ierr); 2649 } 2650 ierr = VecLockReadPop(v1);CHKERRQ(ierr); 2651 ierr = PetscLogEventEnd(MAT_MultHermitianTransposeAdd,mat,v1,v2,v3);CHKERRQ(ierr); 2652 ierr = PetscObjectStateIncrease((PetscObject)v3);CHKERRQ(ierr); 2653 PetscFunctionReturn(0); 2654 } 2655 2656 /*@ 2657 MatMultConstrained - The inner multiplication routine for a 2658 constrained matrix P^T A P. 2659 2660 Neighbor-wise Collective on Mat 2661 2662 Input Parameters: 2663 + mat - the matrix 2664 - x - the vector to be multilplied 2665 2666 Output Parameters: 2667 . y - the result 2668 2669 Notes: 2670 The vectors x and y cannot be the same. I.e., one cannot 2671 call MatMult(A,y,y). 2672 2673 Level: beginner 2674 2675 .seealso: MatMult(), MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd() 2676 @*/ 2677 PetscErrorCode MatMultConstrained(Mat mat,Vec x,Vec y) 2678 { 2679 PetscErrorCode ierr; 2680 2681 PetscFunctionBegin; 2682 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2683 PetscValidHeaderSpecific(x,VEC_CLASSID,2); 2684 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 2685 if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2686 if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2687 if (x == y) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors"); 2688 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); 2689 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); 2690 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); 2691 2692 ierr = PetscLogEventBegin(MAT_MultConstrained,mat,x,y,0);CHKERRQ(ierr); 2693 ierr = VecLockReadPush(x);CHKERRQ(ierr); 2694 ierr = (*mat->ops->multconstrained)(mat,x,y);CHKERRQ(ierr); 2695 ierr = VecLockReadPop(x);CHKERRQ(ierr); 2696 ierr = PetscLogEventEnd(MAT_MultConstrained,mat,x,y,0);CHKERRQ(ierr); 2697 ierr = PetscObjectStateIncrease((PetscObject)y);CHKERRQ(ierr); 2698 PetscFunctionReturn(0); 2699 } 2700 2701 /*@ 2702 MatMultTransposeConstrained - The inner multiplication routine for a 2703 constrained matrix P^T A^T P. 2704 2705 Neighbor-wise Collective on Mat 2706 2707 Input Parameters: 2708 + mat - the matrix 2709 - x - the vector to be multilplied 2710 2711 Output Parameters: 2712 . y - the result 2713 2714 Notes: 2715 The vectors x and y cannot be the same. I.e., one cannot 2716 call MatMult(A,y,y). 2717 2718 Level: beginner 2719 2720 .seealso: MatMult(), MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd() 2721 @*/ 2722 PetscErrorCode MatMultTransposeConstrained(Mat mat,Vec x,Vec y) 2723 { 2724 PetscErrorCode ierr; 2725 2726 PetscFunctionBegin; 2727 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2728 PetscValidHeaderSpecific(x,VEC_CLASSID,2); 2729 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 2730 if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2731 if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2732 if (x == y) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors"); 2733 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); 2734 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); 2735 2736 ierr = PetscLogEventBegin(MAT_MultConstrained,mat,x,y,0);CHKERRQ(ierr); 2737 ierr = (*mat->ops->multtransposeconstrained)(mat,x,y);CHKERRQ(ierr); 2738 ierr = PetscLogEventEnd(MAT_MultConstrained,mat,x,y,0);CHKERRQ(ierr); 2739 ierr = PetscObjectStateIncrease((PetscObject)y);CHKERRQ(ierr); 2740 PetscFunctionReturn(0); 2741 } 2742 2743 /*@C 2744 MatGetFactorType - gets the type of factorization it is 2745 2746 Not Collective 2747 2748 Input Parameters: 2749 . mat - the matrix 2750 2751 Output Parameters: 2752 . t - the type, one of MAT_FACTOR_NONE, MAT_FACTOR_LU, MAT_FACTOR_CHOLESKY, MAT_FACTOR_ILU, MAT_FACTOR_ICC,MAT_FACTOR_ILUDT 2753 2754 Level: intermediate 2755 2756 .seealso: MatFactorType, MatGetFactor(), MatSetFactorType() 2757 @*/ 2758 PetscErrorCode MatGetFactorType(Mat mat,MatFactorType *t) 2759 { 2760 PetscFunctionBegin; 2761 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2762 PetscValidType(mat,1); 2763 PetscValidPointer(t,2); 2764 *t = mat->factortype; 2765 PetscFunctionReturn(0); 2766 } 2767 2768 /*@C 2769 MatSetFactorType - sets the type of factorization it is 2770 2771 Logically Collective on Mat 2772 2773 Input Parameters: 2774 + mat - the matrix 2775 - t - the type, one of MAT_FACTOR_NONE, MAT_FACTOR_LU, MAT_FACTOR_CHOLESKY, MAT_FACTOR_ILU, MAT_FACTOR_ICC,MAT_FACTOR_ILUDT 2776 2777 Level: intermediate 2778 2779 .seealso: MatFactorType, MatGetFactor(), MatGetFactorType() 2780 @*/ 2781 PetscErrorCode MatSetFactorType(Mat mat, MatFactorType t) 2782 { 2783 PetscFunctionBegin; 2784 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2785 PetscValidType(mat,1); 2786 mat->factortype = t; 2787 PetscFunctionReturn(0); 2788 } 2789 2790 /* ------------------------------------------------------------*/ 2791 /*@C 2792 MatGetInfo - Returns information about matrix storage (number of 2793 nonzeros, memory, etc.). 2794 2795 Collective on Mat if MAT_GLOBAL_MAX or MAT_GLOBAL_SUM is used as the flag 2796 2797 Input Parameters: 2798 . mat - the matrix 2799 2800 Output Parameters: 2801 + flag - flag indicating the type of parameters to be returned 2802 (MAT_LOCAL - local matrix, MAT_GLOBAL_MAX - maximum over all processors, 2803 MAT_GLOBAL_SUM - sum over all processors) 2804 - info - matrix information context 2805 2806 Notes: 2807 The MatInfo context contains a variety of matrix data, including 2808 number of nonzeros allocated and used, number of mallocs during 2809 matrix assembly, etc. Additional information for factored matrices 2810 is provided (such as the fill ratio, number of mallocs during 2811 factorization, etc.). Much of this info is printed to PETSC_STDOUT 2812 when using the runtime options 2813 $ -info -mat_view ::ascii_info 2814 2815 Example for C/C++ Users: 2816 See the file ${PETSC_DIR}/include/petscmat.h for a complete list of 2817 data within the MatInfo context. For example, 2818 .vb 2819 MatInfo info; 2820 Mat A; 2821 double mal, nz_a, nz_u; 2822 2823 MatGetInfo(A,MAT_LOCAL,&info); 2824 mal = info.mallocs; 2825 nz_a = info.nz_allocated; 2826 .ve 2827 2828 Example for Fortran Users: 2829 Fortran users should declare info as a double precision 2830 array of dimension MAT_INFO_SIZE, and then extract the parameters 2831 of interest. See the file ${PETSC_DIR}/include/petsc/finclude/petscmat.h 2832 a complete list of parameter names. 2833 .vb 2834 double precision info(MAT_INFO_SIZE) 2835 double precision mal, nz_a 2836 Mat A 2837 integer ierr 2838 2839 call MatGetInfo(A,MAT_LOCAL,info,ierr) 2840 mal = info(MAT_INFO_MALLOCS) 2841 nz_a = info(MAT_INFO_NZ_ALLOCATED) 2842 .ve 2843 2844 Level: intermediate 2845 2846 Developer Note: fortran interface is not autogenerated as the f90 2847 interface defintion cannot be generated correctly [due to MatInfo] 2848 2849 .seealso: MatStashGetInfo() 2850 2851 @*/ 2852 PetscErrorCode MatGetInfo(Mat mat,MatInfoType flag,MatInfo *info) 2853 { 2854 PetscErrorCode ierr; 2855 2856 PetscFunctionBegin; 2857 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2858 PetscValidType(mat,1); 2859 PetscValidPointer(info,3); 2860 if (!mat->ops->getinfo) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 2861 MatCheckPreallocated(mat,1); 2862 ierr = (*mat->ops->getinfo)(mat,flag,info);CHKERRQ(ierr); 2863 PetscFunctionReturn(0); 2864 } 2865 2866 /* 2867 This is used by external packages where it is not easy to get the info from the actual 2868 matrix factorization. 2869 */ 2870 PetscErrorCode MatGetInfo_External(Mat A,MatInfoType flag,MatInfo *info) 2871 { 2872 PetscErrorCode ierr; 2873 2874 PetscFunctionBegin; 2875 ierr = PetscMemzero(info,sizeof(MatInfo));CHKERRQ(ierr); 2876 PetscFunctionReturn(0); 2877 } 2878 2879 /* ----------------------------------------------------------*/ 2880 2881 /*@C 2882 MatLUFactor - Performs in-place LU factorization of matrix. 2883 2884 Collective on Mat 2885 2886 Input Parameters: 2887 + mat - the matrix 2888 . row - row permutation 2889 . col - column permutation 2890 - info - options for factorization, includes 2891 $ fill - expected fill as ratio of original fill. 2892 $ dtcol - pivot tolerance (0 no pivot, 1 full column pivoting) 2893 $ Run with the option -info to determine an optimal value to use 2894 2895 Notes: 2896 Most users should employ the simplified KSP interface for linear solvers 2897 instead of working directly with matrix algebra routines such as this. 2898 See, e.g., KSPCreate(). 2899 2900 This changes the state of the matrix to a factored matrix; it cannot be used 2901 for example with MatSetValues() unless one first calls MatSetUnfactored(). 2902 2903 Level: developer 2904 2905 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor(), 2906 MatGetOrdering(), MatSetUnfactored(), MatFactorInfo, MatGetFactor() 2907 2908 Developer Note: fortran interface is not autogenerated as the f90 2909 interface defintion cannot be generated correctly [due to MatFactorInfo] 2910 2911 @*/ 2912 PetscErrorCode MatLUFactor(Mat mat,IS row,IS col,const MatFactorInfo *info) 2913 { 2914 PetscErrorCode ierr; 2915 MatFactorInfo tinfo; 2916 2917 PetscFunctionBegin; 2918 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2919 if (row) PetscValidHeaderSpecific(row,IS_CLASSID,2); 2920 if (col) PetscValidHeaderSpecific(col,IS_CLASSID,3); 2921 if (info) PetscValidPointer(info,4); 2922 PetscValidType(mat,1); 2923 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2924 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2925 if (!mat->ops->lufactor) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 2926 MatCheckPreallocated(mat,1); 2927 if (!info) { 2928 ierr = MatFactorInfoInitialize(&tinfo);CHKERRQ(ierr); 2929 info = &tinfo; 2930 } 2931 2932 ierr = PetscLogEventBegin(MAT_LUFactor,mat,row,col,0);CHKERRQ(ierr); 2933 ierr = (*mat->ops->lufactor)(mat,row,col,info);CHKERRQ(ierr); 2934 ierr = PetscLogEventEnd(MAT_LUFactor,mat,row,col,0);CHKERRQ(ierr); 2935 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 2936 PetscFunctionReturn(0); 2937 } 2938 2939 /*@C 2940 MatILUFactor - Performs in-place ILU factorization of matrix. 2941 2942 Collective on Mat 2943 2944 Input Parameters: 2945 + mat - the matrix 2946 . row - row permutation 2947 . col - column permutation 2948 - info - structure containing 2949 $ levels - number of levels of fill. 2950 $ expected fill - as ratio of original fill. 2951 $ 1 or 0 - indicating force fill on diagonal (improves robustness for matrices 2952 missing diagonal entries) 2953 2954 Notes: 2955 Probably really in-place only when level of fill is zero, otherwise allocates 2956 new space to store factored matrix and deletes previous memory. 2957 2958 Most users should employ the simplified KSP interface for linear solvers 2959 instead of working directly with matrix algebra routines such as this. 2960 See, e.g., KSPCreate(). 2961 2962 Level: developer 2963 2964 .seealso: MatILUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor(), MatFactorInfo 2965 2966 Developer Note: fortran interface is not autogenerated as the f90 2967 interface defintion cannot be generated correctly [due to MatFactorInfo] 2968 2969 @*/ 2970 PetscErrorCode MatILUFactor(Mat mat,IS row,IS col,const MatFactorInfo *info) 2971 { 2972 PetscErrorCode ierr; 2973 2974 PetscFunctionBegin; 2975 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2976 if (row) PetscValidHeaderSpecific(row,IS_CLASSID,2); 2977 if (col) PetscValidHeaderSpecific(col,IS_CLASSID,3); 2978 PetscValidPointer(info,4); 2979 PetscValidType(mat,1); 2980 if (mat->rmap->N != mat->cmap->N) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONG,"matrix must be square"); 2981 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2982 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2983 if (!mat->ops->ilufactor) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 2984 MatCheckPreallocated(mat,1); 2985 2986 ierr = PetscLogEventBegin(MAT_ILUFactor,mat,row,col,0);CHKERRQ(ierr); 2987 ierr = (*mat->ops->ilufactor)(mat,row,col,info);CHKERRQ(ierr); 2988 ierr = PetscLogEventEnd(MAT_ILUFactor,mat,row,col,0);CHKERRQ(ierr); 2989 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 2990 PetscFunctionReturn(0); 2991 } 2992 2993 /*@C 2994 MatLUFactorSymbolic - Performs symbolic LU factorization of matrix. 2995 Call this routine before calling MatLUFactorNumeric(). 2996 2997 Collective on Mat 2998 2999 Input Parameters: 3000 + fact - the factor matrix obtained with MatGetFactor() 3001 . mat - the matrix 3002 . row, col - row and column permutations 3003 - info - options for factorization, includes 3004 $ fill - expected fill as ratio of original fill. 3005 $ dtcol - pivot tolerance (0 no pivot, 1 full column pivoting) 3006 $ Run with the option -info to determine an optimal value to use 3007 3008 3009 Notes: 3010 See Users-Manual: ch_mat for additional information about choosing the fill factor for better efficiency. 3011 3012 Most users should employ the simplified KSP interface for linear solvers 3013 instead of working directly with matrix algebra routines such as this. 3014 See, e.g., KSPCreate(). 3015 3016 Level: developer 3017 3018 .seealso: MatLUFactor(), MatLUFactorNumeric(), MatCholeskyFactor(), MatFactorInfo, MatFactorInfoInitialize() 3019 3020 Developer Note: fortran interface is not autogenerated as the f90 3021 interface defintion cannot be generated correctly [due to MatFactorInfo] 3022 3023 @*/ 3024 PetscErrorCode MatLUFactorSymbolic(Mat fact,Mat mat,IS row,IS col,const MatFactorInfo *info) 3025 { 3026 PetscErrorCode ierr; 3027 MatFactorInfo tinfo; 3028 3029 PetscFunctionBegin; 3030 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3031 if (row) PetscValidHeaderSpecific(row,IS_CLASSID,2); 3032 if (col) PetscValidHeaderSpecific(col,IS_CLASSID,3); 3033 if (info) PetscValidPointer(info,4); 3034 PetscValidType(mat,1); 3035 PetscValidPointer(fact,5); 3036 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3037 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3038 if (!(fact)->ops->lufactorsymbolic) { 3039 MatSolverType spackage; 3040 ierr = MatFactorGetSolverType(fact,&spackage);CHKERRQ(ierr); 3041 SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s symbolic LU using solver package %s",((PetscObject)mat)->type_name,spackage); 3042 } 3043 MatCheckPreallocated(mat,2); 3044 if (!info) { 3045 ierr = MatFactorInfoInitialize(&tinfo);CHKERRQ(ierr); 3046 info = &tinfo; 3047 } 3048 3049 ierr = PetscLogEventBegin(MAT_LUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr); 3050 ierr = (fact->ops->lufactorsymbolic)(fact,mat,row,col,info);CHKERRQ(ierr); 3051 ierr = PetscLogEventEnd(MAT_LUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr); 3052 ierr = PetscObjectStateIncrease((PetscObject)fact);CHKERRQ(ierr); 3053 PetscFunctionReturn(0); 3054 } 3055 3056 /*@C 3057 MatLUFactorNumeric - Performs numeric LU factorization of a matrix. 3058 Call this routine after first calling MatLUFactorSymbolic(). 3059 3060 Collective on Mat 3061 3062 Input Parameters: 3063 + fact - the factor matrix obtained with MatGetFactor() 3064 . mat - the matrix 3065 - info - options for factorization 3066 3067 Notes: 3068 See MatLUFactor() for in-place factorization. See 3069 MatCholeskyFactorNumeric() for the symmetric, positive definite case. 3070 3071 Most users should employ the simplified KSP interface for linear solvers 3072 instead of working directly with matrix algebra routines such as this. 3073 See, e.g., KSPCreate(). 3074 3075 Level: developer 3076 3077 .seealso: MatLUFactorSymbolic(), MatLUFactor(), MatCholeskyFactor() 3078 3079 Developer Note: fortran interface is not autogenerated as the f90 3080 interface defintion cannot be generated correctly [due to MatFactorInfo] 3081 3082 @*/ 3083 PetscErrorCode MatLUFactorNumeric(Mat fact,Mat mat,const MatFactorInfo *info) 3084 { 3085 MatFactorInfo tinfo; 3086 PetscErrorCode ierr; 3087 3088 PetscFunctionBegin; 3089 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3090 PetscValidType(mat,1); 3091 PetscValidPointer(fact,2); 3092 PetscValidHeaderSpecific(fact,MAT_CLASSID,2); 3093 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3094 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); 3095 3096 if (!(fact)->ops->lufactornumeric) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s numeric LU",((PetscObject)mat)->type_name); 3097 MatCheckPreallocated(mat,2); 3098 if (!info) { 3099 ierr = MatFactorInfoInitialize(&tinfo);CHKERRQ(ierr); 3100 info = &tinfo; 3101 } 3102 3103 ierr = PetscLogEventBegin(MAT_LUFactorNumeric,mat,fact,0,0);CHKERRQ(ierr); 3104 ierr = (fact->ops->lufactornumeric)(fact,mat,info);CHKERRQ(ierr); 3105 ierr = PetscLogEventEnd(MAT_LUFactorNumeric,mat,fact,0,0);CHKERRQ(ierr); 3106 ierr = MatViewFromOptions(fact,NULL,"-mat_factor_view");CHKERRQ(ierr); 3107 ierr = PetscObjectStateIncrease((PetscObject)fact);CHKERRQ(ierr); 3108 PetscFunctionReturn(0); 3109 } 3110 3111 /*@C 3112 MatCholeskyFactor - Performs in-place Cholesky factorization of a 3113 symmetric matrix. 3114 3115 Collective on Mat 3116 3117 Input Parameters: 3118 + mat - the matrix 3119 . perm - row and column permutations 3120 - f - expected fill as ratio of original fill 3121 3122 Notes: 3123 See MatLUFactor() for the nonsymmetric case. See also 3124 MatCholeskyFactorSymbolic(), and MatCholeskyFactorNumeric(). 3125 3126 Most users should employ the simplified KSP interface for linear solvers 3127 instead of working directly with matrix algebra routines such as this. 3128 See, e.g., KSPCreate(). 3129 3130 Level: developer 3131 3132 .seealso: MatLUFactor(), MatCholeskyFactorSymbolic(), MatCholeskyFactorNumeric() 3133 MatGetOrdering() 3134 3135 Developer Note: fortran interface is not autogenerated as the f90 3136 interface defintion cannot be generated correctly [due to MatFactorInfo] 3137 3138 @*/ 3139 PetscErrorCode MatCholeskyFactor(Mat mat,IS perm,const MatFactorInfo *info) 3140 { 3141 PetscErrorCode ierr; 3142 MatFactorInfo tinfo; 3143 3144 PetscFunctionBegin; 3145 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3146 PetscValidType(mat,1); 3147 if (perm) PetscValidHeaderSpecific(perm,IS_CLASSID,2); 3148 if (info) PetscValidPointer(info,3); 3149 if (mat->rmap->N != mat->cmap->N) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONG,"Matrix must be square"); 3150 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3151 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3152 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); 3153 MatCheckPreallocated(mat,1); 3154 if (!info) { 3155 ierr = MatFactorInfoInitialize(&tinfo);CHKERRQ(ierr); 3156 info = &tinfo; 3157 } 3158 3159 ierr = PetscLogEventBegin(MAT_CholeskyFactor,mat,perm,0,0);CHKERRQ(ierr); 3160 ierr = (*mat->ops->choleskyfactor)(mat,perm,info);CHKERRQ(ierr); 3161 ierr = PetscLogEventEnd(MAT_CholeskyFactor,mat,perm,0,0);CHKERRQ(ierr); 3162 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 3163 PetscFunctionReturn(0); 3164 } 3165 3166 /*@C 3167 MatCholeskyFactorSymbolic - Performs symbolic Cholesky factorization 3168 of a symmetric matrix. 3169 3170 Collective on Mat 3171 3172 Input Parameters: 3173 + fact - the factor matrix obtained with MatGetFactor() 3174 . mat - the matrix 3175 . perm - row and column permutations 3176 - info - options for factorization, includes 3177 $ fill - expected fill as ratio of original fill. 3178 $ dtcol - pivot tolerance (0 no pivot, 1 full column pivoting) 3179 $ Run with the option -info to determine an optimal value to use 3180 3181 Notes: 3182 See MatLUFactorSymbolic() for the nonsymmetric case. See also 3183 MatCholeskyFactor() and MatCholeskyFactorNumeric(). 3184 3185 Most users should employ the simplified KSP interface for linear solvers 3186 instead of working directly with matrix algebra routines such as this. 3187 See, e.g., KSPCreate(). 3188 3189 Level: developer 3190 3191 .seealso: MatLUFactorSymbolic(), MatCholeskyFactor(), MatCholeskyFactorNumeric() 3192 MatGetOrdering() 3193 3194 Developer Note: fortran interface is not autogenerated as the f90 3195 interface defintion cannot be generated correctly [due to MatFactorInfo] 3196 3197 @*/ 3198 PetscErrorCode MatCholeskyFactorSymbolic(Mat fact,Mat mat,IS perm,const MatFactorInfo *info) 3199 { 3200 PetscErrorCode ierr; 3201 MatFactorInfo tinfo; 3202 3203 PetscFunctionBegin; 3204 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3205 PetscValidType(mat,1); 3206 if (perm) PetscValidHeaderSpecific(perm,IS_CLASSID,2); 3207 if (info) PetscValidPointer(info,3); 3208 PetscValidPointer(fact,4); 3209 if (mat->rmap->N != mat->cmap->N) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONG,"Matrix must be square"); 3210 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3211 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3212 if (!(fact)->ops->choleskyfactorsymbolic) { 3213 MatSolverType spackage; 3214 ierr = MatFactorGetSolverType(fact,&spackage);CHKERRQ(ierr); 3215 SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s symbolic factor Cholesky using solver package %s",((PetscObject)mat)->type_name,spackage); 3216 } 3217 MatCheckPreallocated(mat,2); 3218 if (!info) { 3219 ierr = MatFactorInfoInitialize(&tinfo);CHKERRQ(ierr); 3220 info = &tinfo; 3221 } 3222 3223 ierr = PetscLogEventBegin(MAT_CholeskyFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr); 3224 ierr = (fact->ops->choleskyfactorsymbolic)(fact,mat,perm,info);CHKERRQ(ierr); 3225 ierr = PetscLogEventEnd(MAT_CholeskyFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr); 3226 ierr = PetscObjectStateIncrease((PetscObject)fact);CHKERRQ(ierr); 3227 PetscFunctionReturn(0); 3228 } 3229 3230 /*@C 3231 MatCholeskyFactorNumeric - Performs numeric Cholesky factorization 3232 of a symmetric matrix. Call this routine after first calling 3233 MatCholeskyFactorSymbolic(). 3234 3235 Collective on Mat 3236 3237 Input Parameters: 3238 + fact - the factor matrix obtained with MatGetFactor() 3239 . mat - the initial matrix 3240 . info - options for factorization 3241 - fact - the symbolic factor of mat 3242 3243 3244 Notes: 3245 Most users should employ the simplified KSP interface for linear solvers 3246 instead of working directly with matrix algebra routines such as this. 3247 See, e.g., KSPCreate(). 3248 3249 Level: developer 3250 3251 .seealso: MatCholeskyFactorSymbolic(), MatCholeskyFactor(), MatLUFactorNumeric() 3252 3253 Developer Note: fortran interface is not autogenerated as the f90 3254 interface defintion cannot be generated correctly [due to MatFactorInfo] 3255 3256 @*/ 3257 PetscErrorCode MatCholeskyFactorNumeric(Mat fact,Mat mat,const MatFactorInfo *info) 3258 { 3259 MatFactorInfo tinfo; 3260 PetscErrorCode ierr; 3261 3262 PetscFunctionBegin; 3263 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3264 PetscValidType(mat,1); 3265 PetscValidPointer(fact,2); 3266 PetscValidHeaderSpecific(fact,MAT_CLASSID,2); 3267 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3268 if (!(fact)->ops->choleskyfactornumeric) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s numeric factor Cholesky",((PetscObject)mat)->type_name); 3269 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); 3270 MatCheckPreallocated(mat,2); 3271 if (!info) { 3272 ierr = MatFactorInfoInitialize(&tinfo);CHKERRQ(ierr); 3273 info = &tinfo; 3274 } 3275 3276 ierr = PetscLogEventBegin(MAT_CholeskyFactorNumeric,mat,fact,0,0);CHKERRQ(ierr); 3277 ierr = (fact->ops->choleskyfactornumeric)(fact,mat,info);CHKERRQ(ierr); 3278 ierr = PetscLogEventEnd(MAT_CholeskyFactorNumeric,mat,fact,0,0);CHKERRQ(ierr); 3279 ierr = MatViewFromOptions(fact,NULL,"-mat_factor_view");CHKERRQ(ierr); 3280 ierr = PetscObjectStateIncrease((PetscObject)fact);CHKERRQ(ierr); 3281 PetscFunctionReturn(0); 3282 } 3283 3284 /* ----------------------------------------------------------------*/ 3285 /*@ 3286 MatSolve - Solves A x = b, given a factored matrix. 3287 3288 Neighbor-wise Collective on Mat 3289 3290 Input Parameters: 3291 + mat - the factored matrix 3292 - b - the right-hand-side vector 3293 3294 Output Parameter: 3295 . x - the result vector 3296 3297 Notes: 3298 The vectors b and x cannot be the same. I.e., one cannot 3299 call MatSolve(A,x,x). 3300 3301 Notes: 3302 Most users should employ the simplified KSP interface for linear solvers 3303 instead of working directly with matrix algebra routines such as this. 3304 See, e.g., KSPCreate(). 3305 3306 Level: developer 3307 3308 .seealso: MatSolveAdd(), MatSolveTranspose(), MatSolveTransposeAdd() 3309 @*/ 3310 PetscErrorCode MatSolve(Mat mat,Vec b,Vec x) 3311 { 3312 PetscErrorCode ierr; 3313 3314 PetscFunctionBegin; 3315 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3316 PetscValidType(mat,1); 3317 PetscValidHeaderSpecific(b,VEC_CLASSID,2); 3318 PetscValidHeaderSpecific(x,VEC_CLASSID,3); 3319 PetscCheckSameComm(mat,1,b,2); 3320 PetscCheckSameComm(mat,1,x,3); 3321 if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 3322 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); 3323 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); 3324 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); 3325 if (!mat->rmap->N && !mat->cmap->N) PetscFunctionReturn(0); 3326 if (!mat->ops->solve) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 3327 MatCheckPreallocated(mat,1); 3328 3329 ierr = PetscLogEventBegin(MAT_Solve,mat,b,x,0);CHKERRQ(ierr); 3330 if (mat->factorerrortype) { 3331 ierr = PetscInfo1(mat,"MatFactorError %D\n",mat->factorerrortype);CHKERRQ(ierr); 3332 ierr = VecSetInf(x);CHKERRQ(ierr); 3333 } else { 3334 if (!mat->ops->solve) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 3335 ierr = (*mat->ops->solve)(mat,b,x);CHKERRQ(ierr); 3336 } 3337 ierr = PetscLogEventEnd(MAT_Solve,mat,b,x,0);CHKERRQ(ierr); 3338 ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr); 3339 PetscFunctionReturn(0); 3340 } 3341 3342 static PetscErrorCode MatMatSolve_Basic(Mat A,Mat B,Mat X, PetscBool trans) 3343 { 3344 PetscErrorCode ierr; 3345 Vec b,x; 3346 PetscInt m,N,i; 3347 PetscScalar *bb,*xx; 3348 3349 PetscFunctionBegin; 3350 ierr = MatDenseGetArray(B,&bb);CHKERRQ(ierr); 3351 ierr = MatDenseGetArray(X,&xx);CHKERRQ(ierr); 3352 ierr = MatGetLocalSize(B,&m,NULL);CHKERRQ(ierr); /* number local rows */ 3353 ierr = MatGetSize(B,NULL,&N);CHKERRQ(ierr); /* total columns in dense matrix */ 3354 ierr = MatCreateVecs(A,&x,&b);CHKERRQ(ierr); 3355 for (i=0; i<N; i++) { 3356 ierr = VecPlaceArray(b,bb + i*m);CHKERRQ(ierr); 3357 ierr = VecPlaceArray(x,xx + i*m);CHKERRQ(ierr); 3358 if (trans) { 3359 ierr = MatSolveTranspose(A,b,x);CHKERRQ(ierr); 3360 } else { 3361 ierr = MatSolve(A,b,x);CHKERRQ(ierr); 3362 } 3363 ierr = VecResetArray(x);CHKERRQ(ierr); 3364 ierr = VecResetArray(b);CHKERRQ(ierr); 3365 } 3366 ierr = VecDestroy(&b);CHKERRQ(ierr); 3367 ierr = VecDestroy(&x);CHKERRQ(ierr); 3368 ierr = MatDenseRestoreArray(B,&bb);CHKERRQ(ierr); 3369 ierr = MatDenseRestoreArray(X,&xx);CHKERRQ(ierr); 3370 PetscFunctionReturn(0); 3371 } 3372 3373 /*@ 3374 MatMatSolve - Solves A X = B, given a factored matrix. 3375 3376 Neighbor-wise Collective on Mat 3377 3378 Input Parameters: 3379 + A - the factored matrix 3380 - B - the right-hand-side matrix (dense matrix) 3381 3382 Output Parameter: 3383 . X - the result matrix (dense matrix) 3384 3385 Notes: 3386 The matrices b and x cannot be the same. I.e., one cannot 3387 call MatMatSolve(A,x,x). 3388 3389 Notes: 3390 Most users should usually employ the simplified KSP interface for linear solvers 3391 instead of working directly with matrix algebra routines such as this. 3392 See, e.g., KSPCreate(). However KSP can only solve for one vector (column of X) 3393 at a time. 3394 3395 When using SuperLU_Dist as a parallel solver PETSc will use the SuperLU_Dist functionality to solve multiple right hand sides simultaneously. For MUMPS 3396 it calls a separate solve for each right hand side since MUMPS does not yet support distributed right hand sides. 3397 3398 Since the resulting matrix X must always be dense we do not support sparse representation of the matrix B. 3399 3400 Level: developer 3401 3402 .seealso: MatMatSolveTranspose(), MatLUFactor(), MatCholeskyFactor() 3403 @*/ 3404 PetscErrorCode MatMatSolve(Mat A,Mat B,Mat X) 3405 { 3406 PetscErrorCode ierr; 3407 3408 PetscFunctionBegin; 3409 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 3410 PetscValidType(A,1); 3411 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 3412 PetscValidHeaderSpecific(X,MAT_CLASSID,3); 3413 PetscCheckSameComm(A,1,B,2); 3414 PetscCheckSameComm(A,1,X,3); 3415 if (X == B) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_IDN,"X and B must be different matrices"); 3416 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); 3417 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); 3418 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"); 3419 if (!A->rmap->N && !A->cmap->N) PetscFunctionReturn(0); 3420 if (!A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 3421 MatCheckPreallocated(A,1); 3422 3423 ierr = PetscLogEventBegin(MAT_MatSolve,A,B,X,0);CHKERRQ(ierr); 3424 if (!A->ops->matsolve) { 3425 ierr = PetscInfo1(A,"Mat type %s using basic MatMatSolve\n",((PetscObject)A)->type_name);CHKERRQ(ierr); 3426 ierr = MatMatSolve_Basic(A,B,X,PETSC_FALSE);CHKERRQ(ierr); 3427 } else { 3428 ierr = (*A->ops->matsolve)(A,B,X);CHKERRQ(ierr); 3429 } 3430 ierr = PetscLogEventEnd(MAT_MatSolve,A,B,X,0);CHKERRQ(ierr); 3431 ierr = PetscObjectStateIncrease((PetscObject)X);CHKERRQ(ierr); 3432 PetscFunctionReturn(0); 3433 } 3434 3435 /*@ 3436 MatMatSolveTranspose - Solves A^T X = B, given a factored matrix. 3437 3438 Neighbor-wise Collective on Mat 3439 3440 Input Parameters: 3441 + A - the factored matrix 3442 - B - the right-hand-side matrix (dense matrix) 3443 3444 Output Parameter: 3445 . X - the result matrix (dense matrix) 3446 3447 Notes: 3448 The matrices B and X cannot be the same. I.e., one cannot 3449 call MatMatSolveTranspose(A,X,X). 3450 3451 Notes: 3452 Most users should usually employ the simplified KSP interface for linear solvers 3453 instead of working directly with matrix algebra routines such as this. 3454 See, e.g., KSPCreate(). However KSP can only solve for one vector (column of X) 3455 at a time. 3456 3457 When using SuperLU_Dist or MUMPS as a parallel solver, PETSc will use their functionality to solve multiple right hand sides simultaneously. 3458 3459 Level: developer 3460 3461 .seealso: MatMatSolve(), MatLUFactor(), MatCholeskyFactor() 3462 @*/ 3463 PetscErrorCode MatMatSolveTranspose(Mat A,Mat B,Mat X) 3464 { 3465 PetscErrorCode ierr; 3466 3467 PetscFunctionBegin; 3468 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 3469 PetscValidType(A,1); 3470 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 3471 PetscValidHeaderSpecific(X,MAT_CLASSID,3); 3472 PetscCheckSameComm(A,1,B,2); 3473 PetscCheckSameComm(A,1,X,3); 3474 if (X == B) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_IDN,"X and B must be different matrices"); 3475 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); 3476 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); 3477 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); 3478 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"); 3479 if (!A->rmap->N && !A->cmap->N) PetscFunctionReturn(0); 3480 if (!A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 3481 MatCheckPreallocated(A,1); 3482 3483 ierr = PetscLogEventBegin(MAT_MatSolve,A,B,X,0);CHKERRQ(ierr); 3484 if (!A->ops->matsolvetranspose) { 3485 ierr = PetscInfo1(A,"Mat type %s using basic MatMatSolveTranspose\n",((PetscObject)A)->type_name);CHKERRQ(ierr); 3486 ierr = MatMatSolve_Basic(A,B,X,PETSC_TRUE);CHKERRQ(ierr); 3487 } else { 3488 ierr = (*A->ops->matsolvetranspose)(A,B,X);CHKERRQ(ierr); 3489 } 3490 ierr = PetscLogEventEnd(MAT_MatSolve,A,B,X,0);CHKERRQ(ierr); 3491 ierr = PetscObjectStateIncrease((PetscObject)X);CHKERRQ(ierr); 3492 PetscFunctionReturn(0); 3493 } 3494 3495 /*@ 3496 MatMatTransposeSolve - Solves A X = B^T, given a factored matrix. 3497 3498 Neighbor-wise Collective on Mat 3499 3500 Input Parameters: 3501 + A - the factored matrix 3502 - Bt - the transpose of right-hand-side matrix 3503 3504 Output Parameter: 3505 . X - the result matrix (dense matrix) 3506 3507 Notes: 3508 Most users should usually employ the simplified KSP interface for linear solvers 3509 instead of working directly with matrix algebra routines such as this. 3510 See, e.g., KSPCreate(). However KSP can only solve for one vector (column of X) 3511 at a time. 3512 3513 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(). 3514 3515 Level: developer 3516 3517 .seealso: MatMatSolve(), MatMatSolveTranspose(), MatLUFactor(), MatCholeskyFactor() 3518 @*/ 3519 PetscErrorCode MatMatTransposeSolve(Mat A,Mat Bt,Mat X) 3520 { 3521 PetscErrorCode ierr; 3522 3523 PetscFunctionBegin; 3524 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 3525 PetscValidType(A,1); 3526 PetscValidHeaderSpecific(Bt,MAT_CLASSID,2); 3527 PetscValidHeaderSpecific(X,MAT_CLASSID,3); 3528 PetscCheckSameComm(A,1,Bt,2); 3529 PetscCheckSameComm(A,1,X,3); 3530 3531 if (X == Bt) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_IDN,"X and B must be different matrices"); 3532 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); 3533 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); 3534 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"); 3535 if (!A->rmap->N && !A->cmap->N) PetscFunctionReturn(0); 3536 if (!A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 3537 MatCheckPreallocated(A,1); 3538 3539 if (!A->ops->mattransposesolve) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Mat type %s",((PetscObject)A)->type_name); 3540 ierr = PetscLogEventBegin(MAT_MatTrSolve,A,Bt,X,0);CHKERRQ(ierr); 3541 ierr = (*A->ops->mattransposesolve)(A,Bt,X);CHKERRQ(ierr); 3542 ierr = PetscLogEventEnd(MAT_MatTrSolve,A,Bt,X,0);CHKERRQ(ierr); 3543 ierr = PetscObjectStateIncrease((PetscObject)X);CHKERRQ(ierr); 3544 PetscFunctionReturn(0); 3545 } 3546 3547 /*@ 3548 MatForwardSolve - Solves L x = b, given a factored matrix, A = LU, or 3549 U^T*D^(1/2) x = b, given a factored symmetric matrix, A = U^T*D*U, 3550 3551 Neighbor-wise Collective on Mat 3552 3553 Input Parameters: 3554 + mat - the factored matrix 3555 - b - the right-hand-side vector 3556 3557 Output Parameter: 3558 . x - the result vector 3559 3560 Notes: 3561 MatSolve() should be used for most applications, as it performs 3562 a forward solve followed by a backward solve. 3563 3564 The vectors b and x cannot be the same, i.e., one cannot 3565 call MatForwardSolve(A,x,x). 3566 3567 For matrix in seqsbaij format with block size larger than 1, 3568 the diagonal blocks are not implemented as D = D^(1/2) * D^(1/2) yet. 3569 MatForwardSolve() solves U^T*D y = b, and 3570 MatBackwardSolve() solves U x = y. 3571 Thus they do not provide a symmetric preconditioner. 3572 3573 Most users should employ the simplified KSP interface for linear solvers 3574 instead of working directly with matrix algebra routines such as this. 3575 See, e.g., KSPCreate(). 3576 3577 Level: developer 3578 3579 .seealso: MatSolve(), MatBackwardSolve() 3580 @*/ 3581 PetscErrorCode MatForwardSolve(Mat mat,Vec b,Vec x) 3582 { 3583 PetscErrorCode ierr; 3584 3585 PetscFunctionBegin; 3586 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3587 PetscValidType(mat,1); 3588 PetscValidHeaderSpecific(b,VEC_CLASSID,2); 3589 PetscValidHeaderSpecific(x,VEC_CLASSID,3); 3590 PetscCheckSameComm(mat,1,b,2); 3591 PetscCheckSameComm(mat,1,x,3); 3592 if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 3593 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); 3594 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); 3595 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); 3596 if (!mat->rmap->N && !mat->cmap->N) PetscFunctionReturn(0); 3597 if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 3598 MatCheckPreallocated(mat,1); 3599 3600 if (!mat->ops->forwardsolve) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 3601 ierr = PetscLogEventBegin(MAT_ForwardSolve,mat,b,x,0);CHKERRQ(ierr); 3602 ierr = (*mat->ops->forwardsolve)(mat,b,x);CHKERRQ(ierr); 3603 ierr = PetscLogEventEnd(MAT_ForwardSolve,mat,b,x,0);CHKERRQ(ierr); 3604 ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr); 3605 PetscFunctionReturn(0); 3606 } 3607 3608 /*@ 3609 MatBackwardSolve - Solves U x = b, given a factored matrix, A = LU. 3610 D^(1/2) U x = b, given a factored symmetric matrix, A = U^T*D*U, 3611 3612 Neighbor-wise Collective on Mat 3613 3614 Input Parameters: 3615 + mat - the factored matrix 3616 - b - the right-hand-side vector 3617 3618 Output Parameter: 3619 . x - the result vector 3620 3621 Notes: 3622 MatSolve() should be used for most applications, as it performs 3623 a forward solve followed by a backward solve. 3624 3625 The vectors b and x cannot be the same. I.e., one cannot 3626 call MatBackwardSolve(A,x,x). 3627 3628 For matrix in seqsbaij format with block size larger than 1, 3629 the diagonal blocks are not implemented as D = D^(1/2) * D^(1/2) yet. 3630 MatForwardSolve() solves U^T*D y = b, and 3631 MatBackwardSolve() solves U x = y. 3632 Thus they do not provide a symmetric preconditioner. 3633 3634 Most users should employ the simplified KSP interface for linear solvers 3635 instead of working directly with matrix algebra routines such as this. 3636 See, e.g., KSPCreate(). 3637 3638 Level: developer 3639 3640 .seealso: MatSolve(), MatForwardSolve() 3641 @*/ 3642 PetscErrorCode MatBackwardSolve(Mat mat,Vec b,Vec x) 3643 { 3644 PetscErrorCode ierr; 3645 3646 PetscFunctionBegin; 3647 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3648 PetscValidType(mat,1); 3649 PetscValidHeaderSpecific(b,VEC_CLASSID,2); 3650 PetscValidHeaderSpecific(x,VEC_CLASSID,3); 3651 PetscCheckSameComm(mat,1,b,2); 3652 PetscCheckSameComm(mat,1,x,3); 3653 if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 3654 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); 3655 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); 3656 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); 3657 if (!mat->rmap->N && !mat->cmap->N) PetscFunctionReturn(0); 3658 if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 3659 MatCheckPreallocated(mat,1); 3660 3661 if (!mat->ops->backwardsolve) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 3662 ierr = PetscLogEventBegin(MAT_BackwardSolve,mat,b,x,0);CHKERRQ(ierr); 3663 ierr = (*mat->ops->backwardsolve)(mat,b,x);CHKERRQ(ierr); 3664 ierr = PetscLogEventEnd(MAT_BackwardSolve,mat,b,x,0);CHKERRQ(ierr); 3665 ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr); 3666 PetscFunctionReturn(0); 3667 } 3668 3669 /*@ 3670 MatSolveAdd - Computes x = y + inv(A)*b, given a factored matrix. 3671 3672 Neighbor-wise Collective on Mat 3673 3674 Input Parameters: 3675 + mat - the factored matrix 3676 . b - the right-hand-side vector 3677 - y - the vector to be added to 3678 3679 Output Parameter: 3680 . x - the result vector 3681 3682 Notes: 3683 The vectors b and x cannot be the same. I.e., one cannot 3684 call MatSolveAdd(A,x,y,x). 3685 3686 Most users should employ the simplified KSP interface for linear solvers 3687 instead of working directly with matrix algebra routines such as this. 3688 See, e.g., KSPCreate(). 3689 3690 Level: developer 3691 3692 .seealso: MatSolve(), MatSolveTranspose(), MatSolveTransposeAdd() 3693 @*/ 3694 PetscErrorCode MatSolveAdd(Mat mat,Vec b,Vec y,Vec x) 3695 { 3696 PetscScalar one = 1.0; 3697 Vec tmp; 3698 PetscErrorCode ierr; 3699 3700 PetscFunctionBegin; 3701 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3702 PetscValidType(mat,1); 3703 PetscValidHeaderSpecific(y,VEC_CLASSID,2); 3704 PetscValidHeaderSpecific(b,VEC_CLASSID,3); 3705 PetscValidHeaderSpecific(x,VEC_CLASSID,4); 3706 PetscCheckSameComm(mat,1,b,2); 3707 PetscCheckSameComm(mat,1,y,2); 3708 PetscCheckSameComm(mat,1,x,3); 3709 if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 3710 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); 3711 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); 3712 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); 3713 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); 3714 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); 3715 if (!mat->rmap->N && !mat->cmap->N) PetscFunctionReturn(0); 3716 if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 3717 MatCheckPreallocated(mat,1); 3718 3719 ierr = PetscLogEventBegin(MAT_SolveAdd,mat,b,x,y);CHKERRQ(ierr); 3720 if (mat->ops->solveadd) { 3721 ierr = (*mat->ops->solveadd)(mat,b,y,x);CHKERRQ(ierr); 3722 } else { 3723 /* do the solve then the add manually */ 3724 if (x != y) { 3725 ierr = MatSolve(mat,b,x);CHKERRQ(ierr); 3726 ierr = VecAXPY(x,one,y);CHKERRQ(ierr); 3727 } else { 3728 ierr = VecDuplicate(x,&tmp);CHKERRQ(ierr); 3729 ierr = PetscLogObjectParent((PetscObject)mat,(PetscObject)tmp);CHKERRQ(ierr); 3730 ierr = VecCopy(x,tmp);CHKERRQ(ierr); 3731 ierr = MatSolve(mat,b,x);CHKERRQ(ierr); 3732 ierr = VecAXPY(x,one,tmp);CHKERRQ(ierr); 3733 ierr = VecDestroy(&tmp);CHKERRQ(ierr); 3734 } 3735 } 3736 ierr = PetscLogEventEnd(MAT_SolveAdd,mat,b,x,y);CHKERRQ(ierr); 3737 ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr); 3738 PetscFunctionReturn(0); 3739 } 3740 3741 /*@ 3742 MatSolveTranspose - Solves A' x = b, given a factored matrix. 3743 3744 Neighbor-wise Collective on Mat 3745 3746 Input Parameters: 3747 + mat - the factored matrix 3748 - b - the right-hand-side vector 3749 3750 Output Parameter: 3751 . x - the result vector 3752 3753 Notes: 3754 The vectors b and x cannot be the same. I.e., one cannot 3755 call MatSolveTranspose(A,x,x). 3756 3757 Most users should employ the simplified KSP interface for linear solvers 3758 instead of working directly with matrix algebra routines such as this. 3759 See, e.g., KSPCreate(). 3760 3761 Level: developer 3762 3763 .seealso: MatSolve(), MatSolveAdd(), MatSolveTransposeAdd() 3764 @*/ 3765 PetscErrorCode MatSolveTranspose(Mat mat,Vec b,Vec x) 3766 { 3767 PetscErrorCode ierr; 3768 3769 PetscFunctionBegin; 3770 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3771 PetscValidType(mat,1); 3772 PetscValidHeaderSpecific(b,VEC_CLASSID,2); 3773 PetscValidHeaderSpecific(x,VEC_CLASSID,3); 3774 PetscCheckSameComm(mat,1,b,2); 3775 PetscCheckSameComm(mat,1,x,3); 3776 if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 3777 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); 3778 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); 3779 if (!mat->rmap->N && !mat->cmap->N) PetscFunctionReturn(0); 3780 if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 3781 MatCheckPreallocated(mat,1); 3782 ierr = PetscLogEventBegin(MAT_SolveTranspose,mat,b,x,0);CHKERRQ(ierr); 3783 if (mat->factorerrortype) { 3784 ierr = PetscInfo1(mat,"MatFactorError %D\n",mat->factorerrortype);CHKERRQ(ierr); 3785 ierr = VecSetInf(x);CHKERRQ(ierr); 3786 } else { 3787 if (!mat->ops->solvetranspose) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s",((PetscObject)mat)->type_name); 3788 ierr = (*mat->ops->solvetranspose)(mat,b,x);CHKERRQ(ierr); 3789 } 3790 ierr = PetscLogEventEnd(MAT_SolveTranspose,mat,b,x,0);CHKERRQ(ierr); 3791 ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr); 3792 PetscFunctionReturn(0); 3793 } 3794 3795 /*@ 3796 MatSolveTransposeAdd - Computes x = y + inv(Transpose(A)) b, given a 3797 factored matrix. 3798 3799 Neighbor-wise Collective on Mat 3800 3801 Input Parameters: 3802 + mat - the factored matrix 3803 . b - the right-hand-side vector 3804 - y - the vector to be added to 3805 3806 Output Parameter: 3807 . x - the result vector 3808 3809 Notes: 3810 The vectors b and x cannot be the same. I.e., one cannot 3811 call MatSolveTransposeAdd(A,x,y,x). 3812 3813 Most users should employ the simplified KSP interface for linear solvers 3814 instead of working directly with matrix algebra routines such as this. 3815 See, e.g., KSPCreate(). 3816 3817 Level: developer 3818 3819 .seealso: MatSolve(), MatSolveAdd(), MatSolveTranspose() 3820 @*/ 3821 PetscErrorCode MatSolveTransposeAdd(Mat mat,Vec b,Vec y,Vec x) 3822 { 3823 PetscScalar one = 1.0; 3824 PetscErrorCode ierr; 3825 Vec tmp; 3826 3827 PetscFunctionBegin; 3828 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3829 PetscValidType(mat,1); 3830 PetscValidHeaderSpecific(y,VEC_CLASSID,2); 3831 PetscValidHeaderSpecific(b,VEC_CLASSID,3); 3832 PetscValidHeaderSpecific(x,VEC_CLASSID,4); 3833 PetscCheckSameComm(mat,1,b,2); 3834 PetscCheckSameComm(mat,1,y,3); 3835 PetscCheckSameComm(mat,1,x,4); 3836 if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 3837 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); 3838 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); 3839 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); 3840 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); 3841 if (!mat->rmap->N && !mat->cmap->N) PetscFunctionReturn(0); 3842 if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 3843 MatCheckPreallocated(mat,1); 3844 3845 ierr = PetscLogEventBegin(MAT_SolveTransposeAdd,mat,b,x,y);CHKERRQ(ierr); 3846 if (mat->ops->solvetransposeadd) { 3847 if (mat->factorerrortype) { 3848 ierr = PetscInfo1(mat,"MatFactorError %D\n",mat->factorerrortype);CHKERRQ(ierr); 3849 ierr = VecSetInf(x);CHKERRQ(ierr); 3850 } else { 3851 ierr = (*mat->ops->solvetransposeadd)(mat,b,y,x);CHKERRQ(ierr); 3852 } 3853 } else { 3854 /* do the solve then the add manually */ 3855 if (x != y) { 3856 ierr = MatSolveTranspose(mat,b,x);CHKERRQ(ierr); 3857 ierr = VecAXPY(x,one,y);CHKERRQ(ierr); 3858 } else { 3859 ierr = VecDuplicate(x,&tmp);CHKERRQ(ierr); 3860 ierr = PetscLogObjectParent((PetscObject)mat,(PetscObject)tmp);CHKERRQ(ierr); 3861 ierr = VecCopy(x,tmp);CHKERRQ(ierr); 3862 ierr = MatSolveTranspose(mat,b,x);CHKERRQ(ierr); 3863 ierr = VecAXPY(x,one,tmp);CHKERRQ(ierr); 3864 ierr = VecDestroy(&tmp);CHKERRQ(ierr); 3865 } 3866 } 3867 ierr = PetscLogEventEnd(MAT_SolveTransposeAdd,mat,b,x,y);CHKERRQ(ierr); 3868 ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr); 3869 PetscFunctionReturn(0); 3870 } 3871 /* ----------------------------------------------------------------*/ 3872 3873 /*@ 3874 MatSOR - Computes relaxation (SOR, Gauss-Seidel) sweeps. 3875 3876 Neighbor-wise Collective on Mat 3877 3878 Input Parameters: 3879 + mat - the matrix 3880 . b - the right hand side 3881 . omega - the relaxation factor 3882 . flag - flag indicating the type of SOR (see below) 3883 . shift - diagonal shift 3884 . its - the number of iterations 3885 - lits - the number of local iterations 3886 3887 Output Parameters: 3888 . x - the solution (can contain an initial guess, use option SOR_ZERO_INITIAL_GUESS to indicate no guess) 3889 3890 SOR Flags: 3891 + SOR_FORWARD_SWEEP - forward SOR 3892 . SOR_BACKWARD_SWEEP - backward SOR 3893 . SOR_SYMMETRIC_SWEEP - SSOR (symmetric SOR) 3894 . SOR_LOCAL_FORWARD_SWEEP - local forward SOR 3895 . SOR_LOCAL_BACKWARD_SWEEP - local forward SOR 3896 . SOR_LOCAL_SYMMETRIC_SWEEP - local SSOR 3897 . SOR_APPLY_UPPER, SOR_APPLY_LOWER - applies 3898 upper/lower triangular part of matrix to 3899 vector (with omega) 3900 - SOR_ZERO_INITIAL_GUESS - zero initial guess 3901 3902 Notes: 3903 SOR_LOCAL_FORWARD_SWEEP, SOR_LOCAL_BACKWARD_SWEEP, and 3904 SOR_LOCAL_SYMMETRIC_SWEEP perform separate independent smoothings 3905 on each processor. 3906 3907 Application programmers will not generally use MatSOR() directly, 3908 but instead will employ the KSP/PC interface. 3909 3910 Notes: 3911 for BAIJ, SBAIJ, and AIJ matrices with Inodes this does a block SOR smoothing, otherwise it does a pointwise smoothing 3912 3913 Notes for Advanced Users: 3914 The flags are implemented as bitwise inclusive or operations. 3915 For example, use (SOR_ZERO_INITIAL_GUESS | SOR_SYMMETRIC_SWEEP) 3916 to specify a zero initial guess for SSOR. 3917 3918 Most users should employ the simplified KSP interface for linear solvers 3919 instead of working directly with matrix algebra routines such as this. 3920 See, e.g., KSPCreate(). 3921 3922 Vectors x and b CANNOT be the same 3923 3924 Developer Note: We should add block SOR support for AIJ matrices with block size set to great than one and no inodes 3925 3926 Level: developer 3927 3928 @*/ 3929 PetscErrorCode MatSOR(Mat mat,Vec b,PetscReal omega,MatSORType flag,PetscReal shift,PetscInt its,PetscInt lits,Vec x) 3930 { 3931 PetscErrorCode ierr; 3932 3933 PetscFunctionBegin; 3934 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3935 PetscValidType(mat,1); 3936 PetscValidHeaderSpecific(b,VEC_CLASSID,2); 3937 PetscValidHeaderSpecific(x,VEC_CLASSID,8); 3938 PetscCheckSameComm(mat,1,b,2); 3939 PetscCheckSameComm(mat,1,x,8); 3940 if (!mat->ops->sor) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 3941 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3942 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3943 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); 3944 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); 3945 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); 3946 if (its <= 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Relaxation requires global its %D positive",its); 3947 if (lits <= 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Relaxation requires local its %D positive",lits); 3948 if (b == x) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"b and x vector cannot be the same"); 3949 3950 MatCheckPreallocated(mat,1); 3951 ierr = PetscLogEventBegin(MAT_SOR,mat,b,x,0);CHKERRQ(ierr); 3952 ierr =(*mat->ops->sor)(mat,b,omega,flag,shift,its,lits,x);CHKERRQ(ierr); 3953 ierr = PetscLogEventEnd(MAT_SOR,mat,b,x,0);CHKERRQ(ierr); 3954 ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr); 3955 PetscFunctionReturn(0); 3956 } 3957 3958 /* 3959 Default matrix copy routine. 3960 */ 3961 PetscErrorCode MatCopy_Basic(Mat A,Mat B,MatStructure str) 3962 { 3963 PetscErrorCode ierr; 3964 PetscInt i,rstart = 0,rend = 0,nz; 3965 const PetscInt *cwork; 3966 const PetscScalar *vwork; 3967 3968 PetscFunctionBegin; 3969 if (B->assembled) { 3970 ierr = MatZeroEntries(B);CHKERRQ(ierr); 3971 } 3972 if (str == SAME_NONZERO_PATTERN) { 3973 ierr = MatGetOwnershipRange(A,&rstart,&rend);CHKERRQ(ierr); 3974 for (i=rstart; i<rend; i++) { 3975 ierr = MatGetRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr); 3976 ierr = MatSetValues(B,1,&i,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr); 3977 ierr = MatRestoreRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr); 3978 } 3979 } else { 3980 ierr = MatAYPX(B,0.0,A,str);CHKERRQ(ierr); 3981 } 3982 ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3983 ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3984 PetscFunctionReturn(0); 3985 } 3986 3987 /*@ 3988 MatCopy - Copies a matrix to another matrix. 3989 3990 Collective on Mat 3991 3992 Input Parameters: 3993 + A - the matrix 3994 - str - SAME_NONZERO_PATTERN or DIFFERENT_NONZERO_PATTERN 3995 3996 Output Parameter: 3997 . B - where the copy is put 3998 3999 Notes: 4000 If you use SAME_NONZERO_PATTERN then the two matrices had better have the 4001 same nonzero pattern or the routine will crash. 4002 4003 MatCopy() copies the matrix entries of a matrix to another existing 4004 matrix (after first zeroing the second matrix). A related routine is 4005 MatConvert(), which first creates a new matrix and then copies the data. 4006 4007 Level: intermediate 4008 4009 .seealso: MatConvert(), MatDuplicate() 4010 4011 @*/ 4012 PetscErrorCode MatCopy(Mat A,Mat B,MatStructure str) 4013 { 4014 PetscErrorCode ierr; 4015 PetscInt i; 4016 4017 PetscFunctionBegin; 4018 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 4019 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 4020 PetscValidType(A,1); 4021 PetscValidType(B,2); 4022 PetscCheckSameComm(A,1,B,2); 4023 MatCheckPreallocated(B,2); 4024 if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4025 if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4026 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); 4027 MatCheckPreallocated(A,1); 4028 if (A == B) PetscFunctionReturn(0); 4029 4030 ierr = PetscLogEventBegin(MAT_Copy,A,B,0,0);CHKERRQ(ierr); 4031 if (A->ops->copy) { 4032 ierr = (*A->ops->copy)(A,B,str);CHKERRQ(ierr); 4033 } else { /* generic conversion */ 4034 ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr); 4035 } 4036 4037 B->stencil.dim = A->stencil.dim; 4038 B->stencil.noc = A->stencil.noc; 4039 for (i=0; i<=A->stencil.dim; i++) { 4040 B->stencil.dims[i] = A->stencil.dims[i]; 4041 B->stencil.starts[i] = A->stencil.starts[i]; 4042 } 4043 4044 ierr = PetscLogEventEnd(MAT_Copy,A,B,0,0);CHKERRQ(ierr); 4045 ierr = PetscObjectStateIncrease((PetscObject)B);CHKERRQ(ierr); 4046 PetscFunctionReturn(0); 4047 } 4048 4049 /*@C 4050 MatConvert - Converts a matrix to another matrix, either of the same 4051 or different type. 4052 4053 Collective on Mat 4054 4055 Input Parameters: 4056 + mat - the matrix 4057 . newtype - new matrix type. Use MATSAME to create a new matrix of the 4058 same type as the original matrix. 4059 - reuse - denotes if the destination matrix is to be created or reused. 4060 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 4061 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). 4062 4063 Output Parameter: 4064 . M - pointer to place new matrix 4065 4066 Notes: 4067 MatConvert() first creates a new matrix and then copies the data from 4068 the first matrix. A related routine is MatCopy(), which copies the matrix 4069 entries of one matrix to another already existing matrix context. 4070 4071 Cannot be used to convert a sequential matrix to parallel or parallel to sequential, 4072 the MPI communicator of the generated matrix is always the same as the communicator 4073 of the input matrix. 4074 4075 Level: intermediate 4076 4077 .seealso: MatCopy(), MatDuplicate() 4078 @*/ 4079 PetscErrorCode MatConvert(Mat mat, MatType newtype,MatReuse reuse,Mat *M) 4080 { 4081 PetscErrorCode ierr; 4082 PetscBool sametype,issame,flg; 4083 char convname[256],mtype[256]; 4084 Mat B; 4085 4086 PetscFunctionBegin; 4087 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4088 PetscValidType(mat,1); 4089 PetscValidPointer(M,3); 4090 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4091 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4092 MatCheckPreallocated(mat,1); 4093 4094 ierr = PetscOptionsGetString(((PetscObject)mat)->options,((PetscObject)mat)->prefix,"-matconvert_type",mtype,256,&flg);CHKERRQ(ierr); 4095 if (flg) { 4096 newtype = mtype; 4097 } 4098 ierr = PetscObjectTypeCompare((PetscObject)mat,newtype,&sametype);CHKERRQ(ierr); 4099 ierr = PetscStrcmp(newtype,"same",&issame);CHKERRQ(ierr); 4100 if ((reuse == MAT_INPLACE_MATRIX) && (mat != *M)) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"MAT_INPLACE_MATRIX requires same input and output matrix"); 4101 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"); 4102 4103 if ((reuse == MAT_INPLACE_MATRIX) && (issame || sametype)) PetscFunctionReturn(0); 4104 4105 if ((sametype || issame) && (reuse==MAT_INITIAL_MATRIX) && mat->ops->duplicate) { 4106 ierr = (*mat->ops->duplicate)(mat,MAT_COPY_VALUES,M);CHKERRQ(ierr); 4107 } else { 4108 PetscErrorCode (*conv)(Mat, MatType,MatReuse,Mat*)=NULL; 4109 const char *prefix[3] = {"seq","mpi",""}; 4110 PetscInt i; 4111 /* 4112 Order of precedence: 4113 0) See if newtype is a superclass of the current matrix. 4114 1) See if a specialized converter is known to the current matrix. 4115 2) See if a specialized converter is known to the desired matrix class. 4116 3) See if a good general converter is registered for the desired class 4117 (as of 6/27/03 only MATMPIADJ falls into this category). 4118 4) See if a good general converter is known for the current matrix. 4119 5) Use a really basic converter. 4120 */ 4121 4122 /* 0) See if newtype is a superclass of the current matrix. 4123 i.e mat is mpiaij and newtype is aij */ 4124 for (i=0; i<2; i++) { 4125 ierr = PetscStrncpy(convname,prefix[i],sizeof(convname));CHKERRQ(ierr); 4126 ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr); 4127 ierr = PetscStrcmp(convname,((PetscObject)mat)->type_name,&flg);CHKERRQ(ierr); 4128 ierr = PetscInfo3(mat,"Check superclass %s %s -> %d\n",convname,((PetscObject)mat)->type_name,flg);CHKERRQ(ierr); 4129 if (flg) { 4130 if (reuse == MAT_INPLACE_MATRIX) { 4131 PetscFunctionReturn(0); 4132 } else if (reuse == MAT_INITIAL_MATRIX && mat->ops->duplicate) { 4133 ierr = (*mat->ops->duplicate)(mat,MAT_COPY_VALUES,M);CHKERRQ(ierr); 4134 PetscFunctionReturn(0); 4135 } else if (reuse == MAT_REUSE_MATRIX && mat->ops->copy) { 4136 ierr = MatCopy(mat,*M,SAME_NONZERO_PATTERN);CHKERRQ(ierr); 4137 PetscFunctionReturn(0); 4138 } 4139 } 4140 } 4141 /* 1) See if a specialized converter is known to the current matrix and the desired class */ 4142 for (i=0; i<3; i++) { 4143 ierr = PetscStrncpy(convname,"MatConvert_",sizeof(convname));CHKERRQ(ierr); 4144 ierr = PetscStrlcat(convname,((PetscObject)mat)->type_name,sizeof(convname));CHKERRQ(ierr); 4145 ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr); 4146 ierr = PetscStrlcat(convname,prefix[i],sizeof(convname));CHKERRQ(ierr); 4147 ierr = PetscStrlcat(convname,issame ? ((PetscObject)mat)->type_name : newtype,sizeof(convname));CHKERRQ(ierr); 4148 ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr); 4149 ierr = PetscObjectQueryFunction((PetscObject)mat,convname,&conv);CHKERRQ(ierr); 4150 ierr = PetscInfo3(mat,"Check specialized (1) %s (%s) -> %d\n",convname,((PetscObject)mat)->type_name,!!conv);CHKERRQ(ierr); 4151 if (conv) goto foundconv; 4152 } 4153 4154 /* 2) See if a specialized converter is known to the desired matrix class. */ 4155 ierr = MatCreate(PetscObjectComm((PetscObject)mat),&B);CHKERRQ(ierr); 4156 ierr = MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->N,mat->cmap->N);CHKERRQ(ierr); 4157 ierr = MatSetType(B,newtype);CHKERRQ(ierr); 4158 for (i=0; i<3; i++) { 4159 ierr = PetscStrncpy(convname,"MatConvert_",sizeof(convname));CHKERRQ(ierr); 4160 ierr = PetscStrlcat(convname,((PetscObject)mat)->type_name,sizeof(convname));CHKERRQ(ierr); 4161 ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr); 4162 ierr = PetscStrlcat(convname,prefix[i],sizeof(convname));CHKERRQ(ierr); 4163 ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr); 4164 ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr); 4165 ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr); 4166 ierr = PetscInfo3(mat,"Check specialized (2) %s (%s) -> %d\n",convname,((PetscObject)B)->type_name,!!conv);CHKERRQ(ierr); 4167 if (conv) { 4168 ierr = MatDestroy(&B);CHKERRQ(ierr); 4169 goto foundconv; 4170 } 4171 } 4172 4173 /* 3) See if a good general converter is registered for the desired class */ 4174 conv = B->ops->convertfrom; 4175 ierr = PetscInfo2(mat,"Check convertfrom (%s) -> %d\n",((PetscObject)B)->type_name,!!conv);CHKERRQ(ierr); 4176 ierr = MatDestroy(&B);CHKERRQ(ierr); 4177 if (conv) goto foundconv; 4178 4179 /* 4) See if a good general converter is known for the current matrix */ 4180 if (mat->ops->convert) { 4181 conv = mat->ops->convert; 4182 } 4183 ierr = PetscInfo2(mat,"Check general convert (%s) -> %d\n",((PetscObject)mat)->type_name,!!conv);CHKERRQ(ierr); 4184 if (conv) goto foundconv; 4185 4186 /* 5) Use a really basic converter. */ 4187 ierr = PetscInfo(mat,"Using MatConvert_Basic\n");CHKERRQ(ierr); 4188 conv = MatConvert_Basic; 4189 4190 foundconv: 4191 ierr = PetscLogEventBegin(MAT_Convert,mat,0,0,0);CHKERRQ(ierr); 4192 ierr = (*conv)(mat,newtype,reuse,M);CHKERRQ(ierr); 4193 if (mat->rmap->mapping && mat->cmap->mapping && !(*M)->rmap->mapping && !(*M)->cmap->mapping) { 4194 /* the block sizes must be same if the mappings are copied over */ 4195 (*M)->rmap->bs = mat->rmap->bs; 4196 (*M)->cmap->bs = mat->cmap->bs; 4197 ierr = PetscObjectReference((PetscObject)mat->rmap->mapping);CHKERRQ(ierr); 4198 ierr = PetscObjectReference((PetscObject)mat->cmap->mapping);CHKERRQ(ierr); 4199 (*M)->rmap->mapping = mat->rmap->mapping; 4200 (*M)->cmap->mapping = mat->cmap->mapping; 4201 } 4202 (*M)->stencil.dim = mat->stencil.dim; 4203 (*M)->stencil.noc = mat->stencil.noc; 4204 for (i=0; i<=mat->stencil.dim; i++) { 4205 (*M)->stencil.dims[i] = mat->stencil.dims[i]; 4206 (*M)->stencil.starts[i] = mat->stencil.starts[i]; 4207 } 4208 ierr = PetscLogEventEnd(MAT_Convert,mat,0,0,0);CHKERRQ(ierr); 4209 } 4210 ierr = PetscObjectStateIncrease((PetscObject)*M);CHKERRQ(ierr); 4211 4212 /* Copy Mat options */ 4213 if (mat->symmetric) {ierr = MatSetOption(*M,MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr);} 4214 if (mat->hermitian) {ierr = MatSetOption(*M,MAT_HERMITIAN,PETSC_TRUE);CHKERRQ(ierr);} 4215 PetscFunctionReturn(0); 4216 } 4217 4218 /*@C 4219 MatFactorGetSolverType - Returns name of the package providing the factorization routines 4220 4221 Not Collective 4222 4223 Input Parameter: 4224 . mat - the matrix, must be a factored matrix 4225 4226 Output Parameter: 4227 . type - the string name of the package (do not free this string) 4228 4229 Notes: 4230 In Fortran you pass in a empty string and the package name will be copied into it. 4231 (Make sure the string is long enough) 4232 4233 Level: intermediate 4234 4235 .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable(), MatGetFactor() 4236 @*/ 4237 PetscErrorCode MatFactorGetSolverType(Mat mat, MatSolverType *type) 4238 { 4239 PetscErrorCode ierr, (*conv)(Mat,MatSolverType*); 4240 4241 PetscFunctionBegin; 4242 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4243 PetscValidType(mat,1); 4244 if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Only for factored matrix"); 4245 ierr = PetscObjectQueryFunction((PetscObject)mat,"MatFactorGetSolverType_C",&conv);CHKERRQ(ierr); 4246 if (!conv) { 4247 *type = MATSOLVERPETSC; 4248 } else { 4249 ierr = (*conv)(mat,type);CHKERRQ(ierr); 4250 } 4251 PetscFunctionReturn(0); 4252 } 4253 4254 typedef struct _MatSolverTypeForSpecifcType* MatSolverTypeForSpecifcType; 4255 struct _MatSolverTypeForSpecifcType { 4256 MatType mtype; 4257 PetscErrorCode (*getfactor[4])(Mat,MatFactorType,Mat*); 4258 MatSolverTypeForSpecifcType next; 4259 }; 4260 4261 typedef struct _MatSolverTypeHolder* MatSolverTypeHolder; 4262 struct _MatSolverTypeHolder { 4263 char *name; 4264 MatSolverTypeForSpecifcType handlers; 4265 MatSolverTypeHolder next; 4266 }; 4267 4268 static MatSolverTypeHolder MatSolverTypeHolders = NULL; 4269 4270 /*@C 4271 MatSolvePackageRegister - Registers a MatSolverType that works for a particular matrix type 4272 4273 Input Parameters: 4274 + package - name of the package, for example petsc or superlu 4275 . mtype - the matrix type that works with this package 4276 . ftype - the type of factorization supported by the package 4277 - getfactor - routine that will create the factored matrix ready to be used 4278 4279 Level: intermediate 4280 4281 .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable() 4282 @*/ 4283 PetscErrorCode MatSolverTypeRegister(MatSolverType package,MatType mtype,MatFactorType ftype,PetscErrorCode (*getfactor)(Mat,MatFactorType,Mat*)) 4284 { 4285 PetscErrorCode ierr; 4286 MatSolverTypeHolder next = MatSolverTypeHolders,prev = NULL; 4287 PetscBool flg; 4288 MatSolverTypeForSpecifcType inext,iprev = NULL; 4289 4290 PetscFunctionBegin; 4291 ierr = MatInitializePackage();CHKERRQ(ierr); 4292 if (!next) { 4293 ierr = PetscNew(&MatSolverTypeHolders);CHKERRQ(ierr); 4294 ierr = PetscStrallocpy(package,&MatSolverTypeHolders->name);CHKERRQ(ierr); 4295 ierr = PetscNew(&MatSolverTypeHolders->handlers);CHKERRQ(ierr); 4296 ierr = PetscStrallocpy(mtype,(char **)&MatSolverTypeHolders->handlers->mtype);CHKERRQ(ierr); 4297 MatSolverTypeHolders->handlers->getfactor[(int)ftype-1] = getfactor; 4298 PetscFunctionReturn(0); 4299 } 4300 while (next) { 4301 ierr = PetscStrcasecmp(package,next->name,&flg);CHKERRQ(ierr); 4302 if (flg) { 4303 if (!next->handlers) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"MatSolverTypeHolder is missing handlers"); 4304 inext = next->handlers; 4305 while (inext) { 4306 ierr = PetscStrcasecmp(mtype,inext->mtype,&flg);CHKERRQ(ierr); 4307 if (flg) { 4308 inext->getfactor[(int)ftype-1] = getfactor; 4309 PetscFunctionReturn(0); 4310 } 4311 iprev = inext; 4312 inext = inext->next; 4313 } 4314 ierr = PetscNew(&iprev->next);CHKERRQ(ierr); 4315 ierr = PetscStrallocpy(mtype,(char **)&iprev->next->mtype);CHKERRQ(ierr); 4316 iprev->next->getfactor[(int)ftype-1] = getfactor; 4317 PetscFunctionReturn(0); 4318 } 4319 prev = next; 4320 next = next->next; 4321 } 4322 ierr = PetscNew(&prev->next);CHKERRQ(ierr); 4323 ierr = PetscStrallocpy(package,&prev->next->name);CHKERRQ(ierr); 4324 ierr = PetscNew(&prev->next->handlers);CHKERRQ(ierr); 4325 ierr = PetscStrallocpy(mtype,(char **)&prev->next->handlers->mtype);CHKERRQ(ierr); 4326 prev->next->handlers->getfactor[(int)ftype-1] = getfactor; 4327 PetscFunctionReturn(0); 4328 } 4329 4330 /*@C 4331 MatSolvePackageGet - Get's the function that creates the factor matrix if it exist 4332 4333 Input Parameters: 4334 + package - name of the package, for example petsc or superlu 4335 . ftype - the type of factorization supported by the package 4336 - mtype - the matrix type that works with this package 4337 4338 Output Parameters: 4339 + foundpackage - PETSC_TRUE if the package was registered 4340 . foundmtype - PETSC_TRUE if the package supports the requested mtype 4341 - getfactor - routine that will create the factored matrix ready to be used or NULL if not found 4342 4343 Level: intermediate 4344 4345 .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable() 4346 @*/ 4347 PetscErrorCode MatSolverTypeGet(MatSolverType package,MatType mtype,MatFactorType ftype,PetscBool *foundpackage,PetscBool *foundmtype,PetscErrorCode (**getfactor)(Mat,MatFactorType,Mat*)) 4348 { 4349 PetscErrorCode ierr; 4350 MatSolverTypeHolder next = MatSolverTypeHolders; 4351 PetscBool flg; 4352 MatSolverTypeForSpecifcType inext; 4353 4354 PetscFunctionBegin; 4355 if (foundpackage) *foundpackage = PETSC_FALSE; 4356 if (foundmtype) *foundmtype = PETSC_FALSE; 4357 if (getfactor) *getfactor = NULL; 4358 4359 if (package) { 4360 while (next) { 4361 ierr = PetscStrcasecmp(package,next->name,&flg);CHKERRQ(ierr); 4362 if (flg) { 4363 if (foundpackage) *foundpackage = PETSC_TRUE; 4364 inext = next->handlers; 4365 while (inext) { 4366 ierr = PetscStrbeginswith(mtype,inext->mtype,&flg);CHKERRQ(ierr); 4367 if (flg) { 4368 if (foundmtype) *foundmtype = PETSC_TRUE; 4369 if (getfactor) *getfactor = inext->getfactor[(int)ftype-1]; 4370 PetscFunctionReturn(0); 4371 } 4372 inext = inext->next; 4373 } 4374 } 4375 next = next->next; 4376 } 4377 } else { 4378 while (next) { 4379 inext = next->handlers; 4380 while (inext) { 4381 ierr = PetscStrbeginswith(mtype,inext->mtype,&flg);CHKERRQ(ierr); 4382 if (flg && inext->getfactor[(int)ftype-1]) { 4383 if (foundpackage) *foundpackage = PETSC_TRUE; 4384 if (foundmtype) *foundmtype = PETSC_TRUE; 4385 if (getfactor) *getfactor = inext->getfactor[(int)ftype-1]; 4386 PetscFunctionReturn(0); 4387 } 4388 inext = inext->next; 4389 } 4390 next = next->next; 4391 } 4392 } 4393 PetscFunctionReturn(0); 4394 } 4395 4396 PetscErrorCode MatSolverTypeDestroy(void) 4397 { 4398 PetscErrorCode ierr; 4399 MatSolverTypeHolder next = MatSolverTypeHolders,prev; 4400 MatSolverTypeForSpecifcType inext,iprev; 4401 4402 PetscFunctionBegin; 4403 while (next) { 4404 ierr = PetscFree(next->name);CHKERRQ(ierr); 4405 inext = next->handlers; 4406 while (inext) { 4407 ierr = PetscFree(inext->mtype);CHKERRQ(ierr); 4408 iprev = inext; 4409 inext = inext->next; 4410 ierr = PetscFree(iprev);CHKERRQ(ierr); 4411 } 4412 prev = next; 4413 next = next->next; 4414 ierr = PetscFree(prev);CHKERRQ(ierr); 4415 } 4416 MatSolverTypeHolders = NULL; 4417 PetscFunctionReturn(0); 4418 } 4419 4420 /*@C 4421 MatGetFactor - Returns a matrix suitable to calls to MatXXFactorSymbolic() 4422 4423 Collective on Mat 4424 4425 Input Parameters: 4426 + mat - the matrix 4427 . type - name of solver type, for example, superlu, petsc (to use PETSc's default) 4428 - ftype - factor type, MAT_FACTOR_LU, MAT_FACTOR_CHOLESKY, MAT_FACTOR_ICC, MAT_FACTOR_ILU, 4429 4430 Output Parameters: 4431 . f - the factor matrix used with MatXXFactorSymbolic() calls 4432 4433 Notes: 4434 Some PETSc matrix formats have alternative solvers available that are contained in alternative packages 4435 such as pastix, superlu, mumps etc. 4436 4437 PETSc must have been ./configure to use the external solver, using the option --download-package 4438 4439 Level: intermediate 4440 4441 .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable() 4442 @*/ 4443 PetscErrorCode MatGetFactor(Mat mat, MatSolverType type,MatFactorType ftype,Mat *f) 4444 { 4445 PetscErrorCode ierr,(*conv)(Mat,MatFactorType,Mat*); 4446 PetscBool foundpackage,foundmtype; 4447 4448 PetscFunctionBegin; 4449 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4450 PetscValidType(mat,1); 4451 4452 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4453 MatCheckPreallocated(mat,1); 4454 4455 ierr = MatSolverTypeGet(type,((PetscObject)mat)->type_name,ftype,&foundpackage,&foundmtype,&conv);CHKERRQ(ierr); 4456 if (!foundpackage) { 4457 if (type) { 4458 SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_MISSING_FACTOR,"Could not locate solver package %s. Perhaps you must ./configure with --download-%s",type,type); 4459 } else { 4460 SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_MISSING_FACTOR,"Could not locate a solver package. Perhaps you must ./configure with --download-<package>"); 4461 } 4462 } 4463 4464 if (!foundmtype) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_MISSING_FACTOR,"MatSolverType %s does not support matrix type %s",type,((PetscObject)mat)->type_name); 4465 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); 4466 4467 #if defined(PETSC_USE_COMPLEX) 4468 if (mat->hermitian && !mat->symmetric && (ftype == MAT_FACTOR_CHOLESKY||ftype == MAT_FACTOR_ICC)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Hermitian CHOLESKY or ICC Factor is not supported"); 4469 #endif 4470 4471 ierr = (*conv)(mat,ftype,f);CHKERRQ(ierr); 4472 PetscFunctionReturn(0); 4473 } 4474 4475 /*@C 4476 MatGetFactorAvailable - Returns a a flag if matrix supports particular package and factor type 4477 4478 Not Collective 4479 4480 Input Parameters: 4481 + mat - the matrix 4482 . type - name of solver type, for example, superlu, petsc (to use PETSc's default) 4483 - ftype - factor type, MAT_FACTOR_LU, MAT_FACTOR_CHOLESKY, MAT_FACTOR_ICC, MAT_FACTOR_ILU, 4484 4485 Output Parameter: 4486 . flg - PETSC_TRUE if the factorization is available 4487 4488 Notes: 4489 Some PETSc matrix formats have alternative solvers available that are contained in alternative packages 4490 such as pastix, superlu, mumps etc. 4491 4492 PETSc must have been ./configure to use the external solver, using the option --download-package 4493 4494 Level: intermediate 4495 4496 .seealso: MatCopy(), MatDuplicate(), MatGetFactor() 4497 @*/ 4498 PetscErrorCode MatGetFactorAvailable(Mat mat, MatSolverType type,MatFactorType ftype,PetscBool *flg) 4499 { 4500 PetscErrorCode ierr, (*gconv)(Mat,MatFactorType,Mat*); 4501 4502 PetscFunctionBegin; 4503 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4504 PetscValidType(mat,1); 4505 4506 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4507 MatCheckPreallocated(mat,1); 4508 4509 *flg = PETSC_FALSE; 4510 ierr = MatSolverTypeGet(type,((PetscObject)mat)->type_name,ftype,NULL,NULL,&gconv);CHKERRQ(ierr); 4511 if (gconv) { 4512 *flg = PETSC_TRUE; 4513 } 4514 PetscFunctionReturn(0); 4515 } 4516 4517 #include <petscdmtypes.h> 4518 4519 /*@ 4520 MatDuplicate - Duplicates a matrix including the non-zero structure. 4521 4522 Collective on Mat 4523 4524 Input Parameters: 4525 + mat - the matrix 4526 - op - One of MAT_DO_NOT_COPY_VALUES, MAT_COPY_VALUES, or MAT_SHARE_NONZERO_PATTERN. 4527 See the manual page for MatDuplicateOption for an explanation of these options. 4528 4529 Output Parameter: 4530 . M - pointer to place new matrix 4531 4532 Level: intermediate 4533 4534 Notes: 4535 You cannot change the nonzero pattern for the parent or child matrix if you use MAT_SHARE_NONZERO_PATTERN. 4536 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. 4537 4538 .seealso: MatCopy(), MatConvert(), MatDuplicateOption 4539 @*/ 4540 PetscErrorCode MatDuplicate(Mat mat,MatDuplicateOption op,Mat *M) 4541 { 4542 PetscErrorCode ierr; 4543 Mat B; 4544 PetscInt i; 4545 DM dm; 4546 void (*viewf)(void); 4547 4548 PetscFunctionBegin; 4549 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4550 PetscValidType(mat,1); 4551 PetscValidPointer(M,3); 4552 if (op == MAT_COPY_VALUES && !mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"MAT_COPY_VALUES not allowed for unassembled matrix"); 4553 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4554 MatCheckPreallocated(mat,1); 4555 4556 *M = 0; 4557 if (!mat->ops->duplicate) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Not written for this matrix type"); 4558 ierr = PetscLogEventBegin(MAT_Convert,mat,0,0,0);CHKERRQ(ierr); 4559 ierr = (*mat->ops->duplicate)(mat,op,M);CHKERRQ(ierr); 4560 B = *M; 4561 4562 ierr = MatGetOperation(mat,MATOP_VIEW,&viewf);CHKERRQ(ierr); 4563 if (viewf) { 4564 ierr = MatSetOperation(B,MATOP_VIEW,viewf);CHKERRQ(ierr); 4565 } 4566 4567 B->stencil.dim = mat->stencil.dim; 4568 B->stencil.noc = mat->stencil.noc; 4569 for (i=0; i<=mat->stencil.dim; i++) { 4570 B->stencil.dims[i] = mat->stencil.dims[i]; 4571 B->stencil.starts[i] = mat->stencil.starts[i]; 4572 } 4573 4574 B->nooffproczerorows = mat->nooffproczerorows; 4575 B->nooffprocentries = mat->nooffprocentries; 4576 4577 ierr = PetscObjectQuery((PetscObject) mat, "__PETSc_dm", (PetscObject*) &dm);CHKERRQ(ierr); 4578 if (dm) { 4579 ierr = PetscObjectCompose((PetscObject) B, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr); 4580 } 4581 ierr = PetscLogEventEnd(MAT_Convert,mat,0,0,0);CHKERRQ(ierr); 4582 ierr = PetscObjectStateIncrease((PetscObject)B);CHKERRQ(ierr); 4583 PetscFunctionReturn(0); 4584 } 4585 4586 /*@ 4587 MatGetDiagonal - Gets the diagonal of a matrix. 4588 4589 Logically Collective on Mat 4590 4591 Input Parameters: 4592 + mat - the matrix 4593 - v - the vector for storing the diagonal 4594 4595 Output Parameter: 4596 . v - the diagonal of the matrix 4597 4598 Level: intermediate 4599 4600 Note: 4601 Currently only correct in parallel for square matrices. 4602 4603 .seealso: MatGetRow(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMaxAbs() 4604 @*/ 4605 PetscErrorCode MatGetDiagonal(Mat mat,Vec v) 4606 { 4607 PetscErrorCode ierr; 4608 4609 PetscFunctionBegin; 4610 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4611 PetscValidType(mat,1); 4612 PetscValidHeaderSpecific(v,VEC_CLASSID,2); 4613 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4614 if (!mat->ops->getdiagonal) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 4615 MatCheckPreallocated(mat,1); 4616 4617 ierr = (*mat->ops->getdiagonal)(mat,v);CHKERRQ(ierr); 4618 ierr = PetscObjectStateIncrease((PetscObject)v);CHKERRQ(ierr); 4619 PetscFunctionReturn(0); 4620 } 4621 4622 /*@C 4623 MatGetRowMin - Gets the minimum value (of the real part) of each 4624 row of the matrix 4625 4626 Logically Collective on Mat 4627 4628 Input Parameters: 4629 . mat - the matrix 4630 4631 Output Parameter: 4632 + v - the vector for storing the maximums 4633 - idx - the indices of the column found for each row (optional) 4634 4635 Level: intermediate 4636 4637 Notes: 4638 The result of this call are the same as if one converted the matrix to dense format 4639 and found the minimum value in each row (i.e. the implicit zeros are counted as zeros). 4640 4641 This code is only implemented for a couple of matrix formats. 4642 4643 .seealso: MatGetDiagonal(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMaxAbs(), 4644 MatGetRowMax() 4645 @*/ 4646 PetscErrorCode MatGetRowMin(Mat mat,Vec v,PetscInt idx[]) 4647 { 4648 PetscErrorCode ierr; 4649 4650 PetscFunctionBegin; 4651 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4652 PetscValidType(mat,1); 4653 PetscValidHeaderSpecific(v,VEC_CLASSID,2); 4654 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4655 if (!mat->ops->getrowmax) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 4656 MatCheckPreallocated(mat,1); 4657 4658 ierr = (*mat->ops->getrowmin)(mat,v,idx);CHKERRQ(ierr); 4659 ierr = PetscObjectStateIncrease((PetscObject)v);CHKERRQ(ierr); 4660 PetscFunctionReturn(0); 4661 } 4662 4663 /*@C 4664 MatGetRowMinAbs - Gets the minimum value (in absolute value) of each 4665 row of the matrix 4666 4667 Logically Collective on Mat 4668 4669 Input Parameters: 4670 . mat - the matrix 4671 4672 Output Parameter: 4673 + v - the vector for storing the minimums 4674 - idx - the indices of the column found for each row (or NULL if not needed) 4675 4676 Level: intermediate 4677 4678 Notes: 4679 if a row is completely empty or has only 0.0 values then the idx[] value for that 4680 row is 0 (the first column). 4681 4682 This code is only implemented for a couple of matrix formats. 4683 4684 .seealso: MatGetDiagonal(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMax(), MatGetRowMaxAbs(), MatGetRowMin() 4685 @*/ 4686 PetscErrorCode MatGetRowMinAbs(Mat mat,Vec v,PetscInt idx[]) 4687 { 4688 PetscErrorCode ierr; 4689 4690 PetscFunctionBegin; 4691 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4692 PetscValidType(mat,1); 4693 PetscValidHeaderSpecific(v,VEC_CLASSID,2); 4694 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4695 if (!mat->ops->getrowminabs) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 4696 MatCheckPreallocated(mat,1); 4697 if (idx) {ierr = PetscArrayzero(idx,mat->rmap->n);CHKERRQ(ierr);} 4698 4699 ierr = (*mat->ops->getrowminabs)(mat,v,idx);CHKERRQ(ierr); 4700 ierr = PetscObjectStateIncrease((PetscObject)v);CHKERRQ(ierr); 4701 PetscFunctionReturn(0); 4702 } 4703 4704 /*@C 4705 MatGetRowMax - Gets the maximum value (of the real part) of each 4706 row of the matrix 4707 4708 Logically Collective on Mat 4709 4710 Input Parameters: 4711 . mat - the matrix 4712 4713 Output Parameter: 4714 + v - the vector for storing the maximums 4715 - idx - the indices of the column found for each row (optional) 4716 4717 Level: intermediate 4718 4719 Notes: 4720 The result of this call are the same as if one converted the matrix to dense format 4721 and found the minimum value in each row (i.e. the implicit zeros are counted as zeros). 4722 4723 This code is only implemented for a couple of matrix formats. 4724 4725 .seealso: MatGetDiagonal(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMaxAbs(), MatGetRowMin() 4726 @*/ 4727 PetscErrorCode MatGetRowMax(Mat mat,Vec v,PetscInt idx[]) 4728 { 4729 PetscErrorCode ierr; 4730 4731 PetscFunctionBegin; 4732 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4733 PetscValidType(mat,1); 4734 PetscValidHeaderSpecific(v,VEC_CLASSID,2); 4735 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4736 if (!mat->ops->getrowmax) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 4737 MatCheckPreallocated(mat,1); 4738 4739 ierr = (*mat->ops->getrowmax)(mat,v,idx);CHKERRQ(ierr); 4740 ierr = PetscObjectStateIncrease((PetscObject)v);CHKERRQ(ierr); 4741 PetscFunctionReturn(0); 4742 } 4743 4744 /*@C 4745 MatGetRowMaxAbs - Gets the maximum value (in absolute value) of each 4746 row of the matrix 4747 4748 Logically Collective on Mat 4749 4750 Input Parameters: 4751 . mat - the matrix 4752 4753 Output Parameter: 4754 + v - the vector for storing the maximums 4755 - idx - the indices of the column found for each row (or NULL if not needed) 4756 4757 Level: intermediate 4758 4759 Notes: 4760 if a row is completely empty or has only 0.0 values then the idx[] value for that 4761 row is 0 (the first column). 4762 4763 This code is only implemented for a couple of matrix formats. 4764 4765 .seealso: MatGetDiagonal(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMax(), MatGetRowMin() 4766 @*/ 4767 PetscErrorCode MatGetRowMaxAbs(Mat mat,Vec v,PetscInt idx[]) 4768 { 4769 PetscErrorCode ierr; 4770 4771 PetscFunctionBegin; 4772 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4773 PetscValidType(mat,1); 4774 PetscValidHeaderSpecific(v,VEC_CLASSID,2); 4775 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4776 if (!mat->ops->getrowmaxabs) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 4777 MatCheckPreallocated(mat,1); 4778 if (idx) {ierr = PetscArrayzero(idx,mat->rmap->n);CHKERRQ(ierr);} 4779 4780 ierr = (*mat->ops->getrowmaxabs)(mat,v,idx);CHKERRQ(ierr); 4781 ierr = PetscObjectStateIncrease((PetscObject)v);CHKERRQ(ierr); 4782 PetscFunctionReturn(0); 4783 } 4784 4785 /*@ 4786 MatGetRowSum - Gets the sum of each row of the matrix 4787 4788 Logically or Neighborhood Collective on Mat 4789 4790 Input Parameters: 4791 . mat - the matrix 4792 4793 Output Parameter: 4794 . v - the vector for storing the sum of rows 4795 4796 Level: intermediate 4797 4798 Notes: 4799 This code is slow since it is not currently specialized for different formats 4800 4801 .seealso: MatGetDiagonal(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMax(), MatGetRowMin() 4802 @*/ 4803 PetscErrorCode MatGetRowSum(Mat mat, Vec v) 4804 { 4805 Vec ones; 4806 PetscErrorCode ierr; 4807 4808 PetscFunctionBegin; 4809 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4810 PetscValidType(mat,1); 4811 PetscValidHeaderSpecific(v,VEC_CLASSID,2); 4812 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4813 MatCheckPreallocated(mat,1); 4814 ierr = MatCreateVecs(mat,&ones,NULL);CHKERRQ(ierr); 4815 ierr = VecSet(ones,1.);CHKERRQ(ierr); 4816 ierr = MatMult(mat,ones,v);CHKERRQ(ierr); 4817 ierr = VecDestroy(&ones);CHKERRQ(ierr); 4818 PetscFunctionReturn(0); 4819 } 4820 4821 /*@ 4822 MatTranspose - Computes an in-place or out-of-place transpose of a matrix. 4823 4824 Collective on Mat 4825 4826 Input Parameter: 4827 + mat - the matrix to transpose 4828 - reuse - either MAT_INITIAL_MATRIX, MAT_REUSE_MATRIX, or MAT_INPLACE_MATRIX 4829 4830 Output Parameters: 4831 . B - the transpose 4832 4833 Notes: 4834 If you use MAT_INPLACE_MATRIX then you must pass in &mat for B 4835 4836 MAT_REUSE_MATRIX causes the B matrix from a previous call to this function with MAT_INITIAL_MATRIX to be used 4837 4838 Consider using MatCreateTranspose() instead if you only need a matrix that behaves like the transpose, but don't need the storage to be changed. 4839 4840 Level: intermediate 4841 4842 .seealso: MatMultTranspose(), MatMultTransposeAdd(), MatIsTranspose(), MatReuse 4843 @*/ 4844 PetscErrorCode MatTranspose(Mat mat,MatReuse reuse,Mat *B) 4845 { 4846 PetscErrorCode ierr; 4847 4848 PetscFunctionBegin; 4849 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4850 PetscValidType(mat,1); 4851 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4852 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4853 if (!mat->ops->transpose) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 4854 if (reuse == MAT_INPLACE_MATRIX && mat != *B) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"MAT_INPLACE_MATRIX requires last matrix to match first"); 4855 if (reuse == MAT_REUSE_MATRIX && mat == *B) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Perhaps you mean MAT_INPLACE_MATRIX"); 4856 MatCheckPreallocated(mat,1); 4857 4858 ierr = PetscLogEventBegin(MAT_Transpose,mat,0,0,0);CHKERRQ(ierr); 4859 ierr = (*mat->ops->transpose)(mat,reuse,B);CHKERRQ(ierr); 4860 ierr = PetscLogEventEnd(MAT_Transpose,mat,0,0,0);CHKERRQ(ierr); 4861 if (B) {ierr = PetscObjectStateIncrease((PetscObject)*B);CHKERRQ(ierr);} 4862 PetscFunctionReturn(0); 4863 } 4864 4865 /*@ 4866 MatIsTranspose - Test whether a matrix is another one's transpose, 4867 or its own, in which case it tests symmetry. 4868 4869 Collective on Mat 4870 4871 Input Parameter: 4872 + A - the matrix to test 4873 - B - the matrix to test against, this can equal the first parameter 4874 4875 Output Parameters: 4876 . flg - the result 4877 4878 Notes: 4879 Only available for SeqAIJ/MPIAIJ matrices. The sequential algorithm 4880 has a running time of the order of the number of nonzeros; the parallel 4881 test involves parallel copies of the block-offdiagonal parts of the matrix. 4882 4883 Level: intermediate 4884 4885 .seealso: MatTranspose(), MatIsSymmetric(), MatIsHermitian() 4886 @*/ 4887 PetscErrorCode MatIsTranspose(Mat A,Mat B,PetscReal tol,PetscBool *flg) 4888 { 4889 PetscErrorCode ierr,(*f)(Mat,Mat,PetscReal,PetscBool*),(*g)(Mat,Mat,PetscReal,PetscBool*); 4890 4891 PetscFunctionBegin; 4892 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 4893 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 4894 PetscValidBoolPointer(flg,3); 4895 ierr = PetscObjectQueryFunction((PetscObject)A,"MatIsTranspose_C",&f);CHKERRQ(ierr); 4896 ierr = PetscObjectQueryFunction((PetscObject)B,"MatIsTranspose_C",&g);CHKERRQ(ierr); 4897 *flg = PETSC_FALSE; 4898 if (f && g) { 4899 if (f == g) { 4900 ierr = (*f)(A,B,tol,flg);CHKERRQ(ierr); 4901 } else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_NOTSAMETYPE,"Matrices do not have the same comparator for symmetry test"); 4902 } else { 4903 MatType mattype; 4904 if (!f) { 4905 ierr = MatGetType(A,&mattype);CHKERRQ(ierr); 4906 } else { 4907 ierr = MatGetType(B,&mattype);CHKERRQ(ierr); 4908 } 4909 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for transpose",mattype); 4910 } 4911 PetscFunctionReturn(0); 4912 } 4913 4914 /*@ 4915 MatHermitianTranspose - Computes an in-place or out-of-place transpose of a matrix in complex conjugate. 4916 4917 Collective on Mat 4918 4919 Input Parameter: 4920 + mat - the matrix to transpose and complex conjugate 4921 - reuse - MAT_INITIAL_MATRIX to create a new matrix, MAT_INPLACE_MATRIX to reuse the first argument to store the transpose 4922 4923 Output Parameters: 4924 . B - the Hermitian 4925 4926 Level: intermediate 4927 4928 .seealso: MatTranspose(), MatMultTranspose(), MatMultTransposeAdd(), MatIsTranspose(), MatReuse 4929 @*/ 4930 PetscErrorCode MatHermitianTranspose(Mat mat,MatReuse reuse,Mat *B) 4931 { 4932 PetscErrorCode ierr; 4933 4934 PetscFunctionBegin; 4935 ierr = MatTranspose(mat,reuse,B);CHKERRQ(ierr); 4936 #if defined(PETSC_USE_COMPLEX) 4937 ierr = MatConjugate(*B);CHKERRQ(ierr); 4938 #endif 4939 PetscFunctionReturn(0); 4940 } 4941 4942 /*@ 4943 MatIsHermitianTranspose - Test whether a matrix is another one's Hermitian transpose, 4944 4945 Collective on Mat 4946 4947 Input Parameter: 4948 + A - the matrix to test 4949 - B - the matrix to test against, this can equal the first parameter 4950 4951 Output Parameters: 4952 . flg - the result 4953 4954 Notes: 4955 Only available for SeqAIJ/MPIAIJ matrices. The sequential algorithm 4956 has a running time of the order of the number of nonzeros; the parallel 4957 test involves parallel copies of the block-offdiagonal parts of the matrix. 4958 4959 Level: intermediate 4960 4961 .seealso: MatTranspose(), MatIsSymmetric(), MatIsHermitian(), MatIsTranspose() 4962 @*/ 4963 PetscErrorCode MatIsHermitianTranspose(Mat A,Mat B,PetscReal tol,PetscBool *flg) 4964 { 4965 PetscErrorCode ierr,(*f)(Mat,Mat,PetscReal,PetscBool*),(*g)(Mat,Mat,PetscReal,PetscBool*); 4966 4967 PetscFunctionBegin; 4968 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 4969 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 4970 PetscValidBoolPointer(flg,3); 4971 ierr = PetscObjectQueryFunction((PetscObject)A,"MatIsHermitianTranspose_C",&f);CHKERRQ(ierr); 4972 ierr = PetscObjectQueryFunction((PetscObject)B,"MatIsHermitianTranspose_C",&g);CHKERRQ(ierr); 4973 if (f && g) { 4974 if (f==g) { 4975 ierr = (*f)(A,B,tol,flg);CHKERRQ(ierr); 4976 } else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_NOTSAMETYPE,"Matrices do not have the same comparator for Hermitian test"); 4977 } 4978 PetscFunctionReturn(0); 4979 } 4980 4981 /*@ 4982 MatPermute - Creates a new matrix with rows and columns permuted from the 4983 original. 4984 4985 Collective on Mat 4986 4987 Input Parameters: 4988 + mat - the matrix to permute 4989 . row - row permutation, each processor supplies only the permutation for its rows 4990 - col - column permutation, each processor supplies only the permutation for its columns 4991 4992 Output Parameters: 4993 . B - the permuted matrix 4994 4995 Level: advanced 4996 4997 Note: 4998 The index sets map from row/col of permuted matrix to row/col of original matrix. 4999 The index sets should be on the same communicator as Mat and have the same local sizes. 5000 5001 .seealso: MatGetOrdering(), ISAllGather() 5002 5003 @*/ 5004 PetscErrorCode MatPermute(Mat mat,IS row,IS col,Mat *B) 5005 { 5006 PetscErrorCode ierr; 5007 5008 PetscFunctionBegin; 5009 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5010 PetscValidType(mat,1); 5011 PetscValidHeaderSpecific(row,IS_CLASSID,2); 5012 PetscValidHeaderSpecific(col,IS_CLASSID,3); 5013 PetscValidPointer(B,4); 5014 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5015 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5016 if (!mat->ops->permute) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatPermute not available for Mat type %s",((PetscObject)mat)->type_name); 5017 MatCheckPreallocated(mat,1); 5018 5019 ierr = (*mat->ops->permute)(mat,row,col,B);CHKERRQ(ierr); 5020 ierr = PetscObjectStateIncrease((PetscObject)*B);CHKERRQ(ierr); 5021 PetscFunctionReturn(0); 5022 } 5023 5024 /*@ 5025 MatEqual - Compares two matrices. 5026 5027 Collective on Mat 5028 5029 Input Parameters: 5030 + A - the first matrix 5031 - B - the second matrix 5032 5033 Output Parameter: 5034 . flg - PETSC_TRUE if the matrices are equal; PETSC_FALSE otherwise. 5035 5036 Level: intermediate 5037 5038 @*/ 5039 PetscErrorCode MatEqual(Mat A,Mat B,PetscBool *flg) 5040 { 5041 PetscErrorCode ierr; 5042 5043 PetscFunctionBegin; 5044 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 5045 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 5046 PetscValidType(A,1); 5047 PetscValidType(B,2); 5048 PetscValidBoolPointer(flg,3); 5049 PetscCheckSameComm(A,1,B,2); 5050 MatCheckPreallocated(B,2); 5051 if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5052 if (!B->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5053 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); 5054 if (!A->ops->equal) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Mat type %s",((PetscObject)A)->type_name); 5055 if (!B->ops->equal) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Mat type %s",((PetscObject)B)->type_name); 5056 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); 5057 MatCheckPreallocated(A,1); 5058 5059 ierr = (*A->ops->equal)(A,B,flg);CHKERRQ(ierr); 5060 PetscFunctionReturn(0); 5061 } 5062 5063 /*@ 5064 MatDiagonalScale - Scales a matrix on the left and right by diagonal 5065 matrices that are stored as vectors. Either of the two scaling 5066 matrices can be NULL. 5067 5068 Collective on Mat 5069 5070 Input Parameters: 5071 + mat - the matrix to be scaled 5072 . l - the left scaling vector (or NULL) 5073 - r - the right scaling vector (or NULL) 5074 5075 Notes: 5076 MatDiagonalScale() computes A = LAR, where 5077 L = a diagonal matrix (stored as a vector), R = a diagonal matrix (stored as a vector) 5078 The L scales the rows of the matrix, the R scales the columns of the matrix. 5079 5080 Level: intermediate 5081 5082 5083 .seealso: MatScale(), MatShift(), MatDiagonalSet() 5084 @*/ 5085 PetscErrorCode MatDiagonalScale(Mat mat,Vec l,Vec r) 5086 { 5087 PetscErrorCode ierr; 5088 5089 PetscFunctionBegin; 5090 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5091 PetscValidType(mat,1); 5092 if (!mat->ops->diagonalscale) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 5093 if (l) {PetscValidHeaderSpecific(l,VEC_CLASSID,2);PetscCheckSameComm(mat,1,l,2);} 5094 if (r) {PetscValidHeaderSpecific(r,VEC_CLASSID,3);PetscCheckSameComm(mat,1,r,3);} 5095 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5096 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5097 MatCheckPreallocated(mat,1); 5098 5099 ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 5100 ierr = (*mat->ops->diagonalscale)(mat,l,r);CHKERRQ(ierr); 5101 ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 5102 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 5103 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 5104 if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) { 5105 mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU; 5106 } 5107 #endif 5108 PetscFunctionReturn(0); 5109 } 5110 5111 /*@ 5112 MatScale - Scales all elements of a matrix by a given number. 5113 5114 Logically Collective on Mat 5115 5116 Input Parameters: 5117 + mat - the matrix to be scaled 5118 - a - the scaling value 5119 5120 Output Parameter: 5121 . mat - the scaled matrix 5122 5123 Level: intermediate 5124 5125 .seealso: MatDiagonalScale() 5126 @*/ 5127 PetscErrorCode MatScale(Mat mat,PetscScalar a) 5128 { 5129 PetscErrorCode ierr; 5130 5131 PetscFunctionBegin; 5132 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5133 PetscValidType(mat,1); 5134 if (a != (PetscScalar)1.0 && !mat->ops->scale) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 5135 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5136 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5137 PetscValidLogicalCollectiveScalar(mat,a,2); 5138 MatCheckPreallocated(mat,1); 5139 5140 ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 5141 if (a != (PetscScalar)1.0) { 5142 ierr = (*mat->ops->scale)(mat,a);CHKERRQ(ierr); 5143 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 5144 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 5145 if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) { 5146 mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU; 5147 } 5148 #endif 5149 } 5150 ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 5151 PetscFunctionReturn(0); 5152 } 5153 5154 /*@ 5155 MatNorm - Calculates various norms of a matrix. 5156 5157 Collective on Mat 5158 5159 Input Parameters: 5160 + mat - the matrix 5161 - type - the type of norm, NORM_1, NORM_FROBENIUS, NORM_INFINITY 5162 5163 Output Parameters: 5164 . nrm - the resulting norm 5165 5166 Level: intermediate 5167 5168 @*/ 5169 PetscErrorCode MatNorm(Mat mat,NormType type,PetscReal *nrm) 5170 { 5171 PetscErrorCode ierr; 5172 5173 PetscFunctionBegin; 5174 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5175 PetscValidType(mat,1); 5176 PetscValidScalarPointer(nrm,3); 5177 5178 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5179 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5180 if (!mat->ops->norm) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 5181 MatCheckPreallocated(mat,1); 5182 5183 ierr = (*mat->ops->norm)(mat,type,nrm);CHKERRQ(ierr); 5184 PetscFunctionReturn(0); 5185 } 5186 5187 /* 5188 This variable is used to prevent counting of MatAssemblyBegin() that 5189 are called from within a MatAssemblyEnd(). 5190 */ 5191 static PetscInt MatAssemblyEnd_InUse = 0; 5192 /*@ 5193 MatAssemblyBegin - Begins assembling the matrix. This routine should 5194 be called after completing all calls to MatSetValues(). 5195 5196 Collective on Mat 5197 5198 Input Parameters: 5199 + mat - the matrix 5200 - type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY 5201 5202 Notes: 5203 MatSetValues() generally caches the values. The matrix is ready to 5204 use only after MatAssemblyBegin() and MatAssemblyEnd() have been called. 5205 Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES 5206 in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before 5207 using the matrix. 5208 5209 ALL processes that share a matrix MUST call MatAssemblyBegin() and MatAssemblyEnd() the SAME NUMBER of times, and each time with the 5210 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 5211 a global collective operation requring all processes that share the matrix. 5212 5213 Space for preallocated nonzeros that is not filled by a call to MatSetValues() or a related routine are compressed 5214 out by assembly. If you intend to use that extra space on a subsequent assembly, be sure to insert explicit zeros 5215 before MAT_FINAL_ASSEMBLY so the space is not compressed out. 5216 5217 Level: beginner 5218 5219 .seealso: MatAssemblyEnd(), MatSetValues(), MatAssembled() 5220 @*/ 5221 PetscErrorCode MatAssemblyBegin(Mat mat,MatAssemblyType type) 5222 { 5223 PetscErrorCode ierr; 5224 5225 PetscFunctionBegin; 5226 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5227 PetscValidType(mat,1); 5228 MatCheckPreallocated(mat,1); 5229 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix.\nDid you forget to call MatSetUnfactored()?"); 5230 if (mat->assembled) { 5231 mat->was_assembled = PETSC_TRUE; 5232 mat->assembled = PETSC_FALSE; 5233 } 5234 5235 if (!MatAssemblyEnd_InUse) { 5236 ierr = PetscLogEventBegin(MAT_AssemblyBegin,mat,0,0,0);CHKERRQ(ierr); 5237 if (mat->ops->assemblybegin) {ierr = (*mat->ops->assemblybegin)(mat,type);CHKERRQ(ierr);} 5238 ierr = PetscLogEventEnd(MAT_AssemblyBegin,mat,0,0,0);CHKERRQ(ierr); 5239 } else if (mat->ops->assemblybegin) { 5240 ierr = (*mat->ops->assemblybegin)(mat,type);CHKERRQ(ierr); 5241 } 5242 PetscFunctionReturn(0); 5243 } 5244 5245 /*@ 5246 MatAssembled - Indicates if a matrix has been assembled and is ready for 5247 use; for example, in matrix-vector product. 5248 5249 Not Collective 5250 5251 Input Parameter: 5252 . mat - the matrix 5253 5254 Output Parameter: 5255 . assembled - PETSC_TRUE or PETSC_FALSE 5256 5257 Level: advanced 5258 5259 .seealso: MatAssemblyEnd(), MatSetValues(), MatAssemblyBegin() 5260 @*/ 5261 PetscErrorCode MatAssembled(Mat mat,PetscBool *assembled) 5262 { 5263 PetscFunctionBegin; 5264 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5265 PetscValidPointer(assembled,2); 5266 *assembled = mat->assembled; 5267 PetscFunctionReturn(0); 5268 } 5269 5270 /*@ 5271 MatAssemblyEnd - Completes assembling the matrix. This routine should 5272 be called after MatAssemblyBegin(). 5273 5274 Collective on Mat 5275 5276 Input Parameters: 5277 + mat - the matrix 5278 - type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY 5279 5280 Options Database Keys: 5281 + -mat_view ::ascii_info - Prints info on matrix at conclusion of MatEndAssembly() 5282 . -mat_view ::ascii_info_detail - Prints more detailed info 5283 . -mat_view - Prints matrix in ASCII format 5284 . -mat_view ::ascii_matlab - Prints matrix in Matlab format 5285 . -mat_view draw - PetscDraws nonzero structure of matrix, using MatView() and PetscDrawOpenX(). 5286 . -display <name> - Sets display name (default is host) 5287 . -draw_pause <sec> - Sets number of seconds to pause after display 5288 . -mat_view socket - Sends matrix to socket, can be accessed from Matlab (See Users-Manual: ch_matlab ) 5289 . -viewer_socket_machine <machine> - Machine to use for socket 5290 . -viewer_socket_port <port> - Port number to use for socket 5291 - -mat_view binary:filename[:append] - Save matrix to file in binary format 5292 5293 Notes: 5294 MatSetValues() generally caches the values. The matrix is ready to 5295 use only after MatAssemblyBegin() and MatAssemblyEnd() have been called. 5296 Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES 5297 in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before 5298 using the matrix. 5299 5300 Space for preallocated nonzeros that is not filled by a call to MatSetValues() or a related routine are compressed 5301 out by assembly. If you intend to use that extra space on a subsequent assembly, be sure to insert explicit zeros 5302 before MAT_FINAL_ASSEMBLY so the space is not compressed out. 5303 5304 Level: beginner 5305 5306 .seealso: MatAssemblyBegin(), MatSetValues(), PetscDrawOpenX(), PetscDrawCreate(), MatView(), MatAssembled(), PetscViewerSocketOpen() 5307 @*/ 5308 PetscErrorCode MatAssemblyEnd(Mat mat,MatAssemblyType type) 5309 { 5310 PetscErrorCode ierr; 5311 static PetscInt inassm = 0; 5312 PetscBool flg = PETSC_FALSE; 5313 5314 PetscFunctionBegin; 5315 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5316 PetscValidType(mat,1); 5317 5318 inassm++; 5319 MatAssemblyEnd_InUse++; 5320 if (MatAssemblyEnd_InUse == 1) { /* Do the logging only the first time through */ 5321 ierr = PetscLogEventBegin(MAT_AssemblyEnd,mat,0,0,0);CHKERRQ(ierr); 5322 if (mat->ops->assemblyend) { 5323 ierr = (*mat->ops->assemblyend)(mat,type);CHKERRQ(ierr); 5324 } 5325 ierr = PetscLogEventEnd(MAT_AssemblyEnd,mat,0,0,0);CHKERRQ(ierr); 5326 } else if (mat->ops->assemblyend) { 5327 ierr = (*mat->ops->assemblyend)(mat,type);CHKERRQ(ierr); 5328 } 5329 5330 /* Flush assembly is not a true assembly */ 5331 if (type != MAT_FLUSH_ASSEMBLY) { 5332 mat->assembled = PETSC_TRUE; 5333 mat->num_ass++; 5334 mat->ass_nonzerostate = mat->nonzerostate; 5335 } 5336 5337 mat->insertmode = NOT_SET_VALUES; 5338 MatAssemblyEnd_InUse--; 5339 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 5340 if (!mat->symmetric_eternal) { 5341 mat->symmetric_set = PETSC_FALSE; 5342 mat->hermitian_set = PETSC_FALSE; 5343 mat->structurally_symmetric_set = PETSC_FALSE; 5344 } 5345 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 5346 if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) { 5347 mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU; 5348 } 5349 #endif 5350 if (inassm == 1 && type != MAT_FLUSH_ASSEMBLY) { 5351 ierr = MatViewFromOptions(mat,NULL,"-mat_view");CHKERRQ(ierr); 5352 5353 if (mat->checksymmetryonassembly) { 5354 ierr = MatIsSymmetric(mat,mat->checksymmetrytol,&flg);CHKERRQ(ierr); 5355 if (flg) { 5356 ierr = PetscPrintf(PetscObjectComm((PetscObject)mat),"Matrix is symmetric (tolerance %g)\n",(double)mat->checksymmetrytol);CHKERRQ(ierr); 5357 } else { 5358 ierr = PetscPrintf(PetscObjectComm((PetscObject)mat),"Matrix is not symmetric (tolerance %g)\n",(double)mat->checksymmetrytol);CHKERRQ(ierr); 5359 } 5360 } 5361 if (mat->nullsp && mat->checknullspaceonassembly) { 5362 ierr = MatNullSpaceTest(mat->nullsp,mat,NULL);CHKERRQ(ierr); 5363 } 5364 } 5365 inassm--; 5366 PetscFunctionReturn(0); 5367 } 5368 5369 /*@ 5370 MatSetOption - Sets a parameter option for a matrix. Some options 5371 may be specific to certain storage formats. Some options 5372 determine how values will be inserted (or added). Sorted, 5373 row-oriented input will generally assemble the fastest. The default 5374 is row-oriented. 5375 5376 Logically Collective on Mat for certain operations, such as MAT_SPD, not collective for MAT_ROW_ORIENTED, see MatOption 5377 5378 Input Parameters: 5379 + mat - the matrix 5380 . option - the option, one of those listed below (and possibly others), 5381 - flg - turn the option on (PETSC_TRUE) or off (PETSC_FALSE) 5382 5383 Options Describing Matrix Structure: 5384 + MAT_SPD - symmetric positive definite 5385 . MAT_SYMMETRIC - symmetric in terms of both structure and value 5386 . MAT_HERMITIAN - transpose is the complex conjugation 5387 . MAT_STRUCTURALLY_SYMMETRIC - symmetric nonzero structure 5388 - MAT_SYMMETRY_ETERNAL - if you would like the symmetry/Hermitian flag 5389 you set to be kept with all future use of the matrix 5390 including after MatAssemblyBegin/End() which could 5391 potentially change the symmetry structure, i.e. you 5392 KNOW the matrix will ALWAYS have the property you set. 5393 5394 5395 Options For Use with MatSetValues(): 5396 Insert a logically dense subblock, which can be 5397 . MAT_ROW_ORIENTED - row-oriented (default) 5398 5399 Note these options reflect the data you pass in with MatSetValues(); it has 5400 nothing to do with how the data is stored internally in the matrix 5401 data structure. 5402 5403 When (re)assembling a matrix, we can restrict the input for 5404 efficiency/debugging purposes. These options include: 5405 + MAT_NEW_NONZERO_LOCATIONS - additional insertions will be allowed if they generate a new nonzero (slow) 5406 . MAT_NEW_DIAGONALS - new diagonals will be allowed (for block diagonal format only) 5407 . MAT_IGNORE_OFF_PROC_ENTRIES - drops off-processor entries 5408 . MAT_NEW_NONZERO_LOCATION_ERR - generates an error for new matrix entry 5409 . MAT_USE_HASH_TABLE - uses a hash table to speed up matrix assembly 5410 . MAT_NO_OFF_PROC_ENTRIES - you know each process will only set values for its own rows, will generate an error if 5411 any process sets values for another process. This avoids all reductions in the MatAssembly routines and thus improves 5412 performance for very large process counts. 5413 - MAT_SUBSET_OFF_PROC_ENTRIES - you know that the first assembly after setting this flag will set a superset 5414 of the off-process entries required for all subsequent assemblies. This avoids a rendezvous step in the MatAssembly 5415 functions, instead sending only neighbor messages. 5416 5417 Notes: 5418 Except for MAT_UNUSED_NONZERO_LOCATION_ERR and MAT_ROW_ORIENTED all processes that share the matrix must pass the same value in flg! 5419 5420 Some options are relevant only for particular matrix types and 5421 are thus ignored by others. Other options are not supported by 5422 certain matrix types and will generate an error message if set. 5423 5424 If using a Fortran 77 module to compute a matrix, one may need to 5425 use the column-oriented option (or convert to the row-oriented 5426 format). 5427 5428 MAT_NEW_NONZERO_LOCATIONS set to PETSC_FALSE indicates that any add or insertion 5429 that would generate a new entry in the nonzero structure is instead 5430 ignored. Thus, if memory has not alredy been allocated for this particular 5431 data, then the insertion is ignored. For dense matrices, in which 5432 the entire array is allocated, no entries are ever ignored. 5433 Set after the first MatAssemblyEnd(). If this option is set then the MatAssemblyBegin/End() processes has one less global reduction 5434 5435 MAT_NEW_NONZERO_LOCATION_ERR set to PETSC_TRUE indicates that any add or insertion 5436 that would generate a new entry in the nonzero structure instead produces 5437 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 5438 5439 MAT_NEW_NONZERO_ALLOCATION_ERR set to PETSC_TRUE indicates that any add or insertion 5440 that would generate a new entry that has not been preallocated will 5441 instead produce an error. (Currently supported for AIJ and BAIJ formats 5442 only.) This is a useful flag when debugging matrix memory preallocation. 5443 If this option is set then the MatAssemblyBegin/End() processes has one less global reduction 5444 5445 MAT_IGNORE_OFF_PROC_ENTRIES set to PETSC_TRUE indicates entries destined for 5446 other processors should be dropped, rather than stashed. 5447 This is useful if you know that the "owning" processor is also 5448 always generating the correct matrix entries, so that PETSc need 5449 not transfer duplicate entries generated on another processor. 5450 5451 MAT_USE_HASH_TABLE indicates that a hash table be used to improve the 5452 searches during matrix assembly. When this flag is set, the hash table 5453 is created during the first Matrix Assembly. This hash table is 5454 used the next time through, during MatSetVaules()/MatSetVaulesBlocked() 5455 to improve the searching of indices. MAT_NEW_NONZERO_LOCATIONS flag 5456 should be used with MAT_USE_HASH_TABLE flag. This option is currently 5457 supported by MATMPIBAIJ format only. 5458 5459 MAT_KEEP_NONZERO_PATTERN indicates when MatZeroRows() is called the zeroed entries 5460 are kept in the nonzero structure 5461 5462 MAT_IGNORE_ZERO_ENTRIES - for AIJ/IS matrices this will stop zero values from creating 5463 a zero location in the matrix 5464 5465 MAT_USE_INODES - indicates using inode version of the code - works with AIJ matrix types 5466 5467 MAT_NO_OFF_PROC_ZERO_ROWS - you know each process will only zero its own rows. This avoids all reductions in the 5468 zero row routines and thus improves performance for very large process counts. 5469 5470 MAT_IGNORE_LOWER_TRIANGULAR - For SBAIJ matrices will ignore any insertions you make in the lower triangular 5471 part of the matrix (since they should match the upper triangular part). 5472 5473 MAT_SORTED_FULL - each process provides exactly its local rows; all column indices for a given row are passed in a 5474 single call to MatSetValues(), preallocation is perfect, row oriented, INSERT_VALUES is used. Common 5475 with finite difference schemes with non-periodic boundary conditions. 5476 Notes: 5477 Can only be called after MatSetSizes() and MatSetType() have been set. 5478 5479 Level: intermediate 5480 5481 .seealso: MatOption, Mat 5482 5483 @*/ 5484 PetscErrorCode MatSetOption(Mat mat,MatOption op,PetscBool flg) 5485 { 5486 PetscErrorCode ierr; 5487 5488 PetscFunctionBegin; 5489 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5490 PetscValidType(mat,1); 5491 if (op > 0) { 5492 PetscValidLogicalCollectiveEnum(mat,op,2); 5493 PetscValidLogicalCollectiveBool(mat,flg,3); 5494 } 5495 5496 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); 5497 if (!((PetscObject)mat)->type_name) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_TYPENOTSET,"Cannot set options until type and size have been set, see MatSetType() and MatSetSizes()"); 5498 5499 switch (op) { 5500 case MAT_NO_OFF_PROC_ENTRIES: 5501 mat->nooffprocentries = flg; 5502 PetscFunctionReturn(0); 5503 break; 5504 case MAT_SUBSET_OFF_PROC_ENTRIES: 5505 mat->assembly_subset = flg; 5506 if (!mat->assembly_subset) { /* See the same logic in VecAssembly wrt VEC_SUBSET_OFF_PROC_ENTRIES */ 5507 #if !defined(PETSC_HAVE_MPIUNI) 5508 ierr = MatStashScatterDestroy_BTS(&mat->stash);CHKERRQ(ierr); 5509 #endif 5510 mat->stash.first_assembly_done = PETSC_FALSE; 5511 } 5512 PetscFunctionReturn(0); 5513 case MAT_NO_OFF_PROC_ZERO_ROWS: 5514 mat->nooffproczerorows = flg; 5515 PetscFunctionReturn(0); 5516 break; 5517 case MAT_SPD: 5518 mat->spd_set = PETSC_TRUE; 5519 mat->spd = flg; 5520 if (flg) { 5521 mat->symmetric = PETSC_TRUE; 5522 mat->structurally_symmetric = PETSC_TRUE; 5523 mat->symmetric_set = PETSC_TRUE; 5524 mat->structurally_symmetric_set = PETSC_TRUE; 5525 } 5526 break; 5527 case MAT_SYMMETRIC: 5528 mat->symmetric = flg; 5529 if (flg) mat->structurally_symmetric = PETSC_TRUE; 5530 mat->symmetric_set = PETSC_TRUE; 5531 mat->structurally_symmetric_set = flg; 5532 #if !defined(PETSC_USE_COMPLEX) 5533 mat->hermitian = flg; 5534 mat->hermitian_set = PETSC_TRUE; 5535 #endif 5536 break; 5537 case MAT_HERMITIAN: 5538 mat->hermitian = flg; 5539 if (flg) mat->structurally_symmetric = PETSC_TRUE; 5540 mat->hermitian_set = PETSC_TRUE; 5541 mat->structurally_symmetric_set = flg; 5542 #if !defined(PETSC_USE_COMPLEX) 5543 mat->symmetric = flg; 5544 mat->symmetric_set = PETSC_TRUE; 5545 #endif 5546 break; 5547 case MAT_STRUCTURALLY_SYMMETRIC: 5548 mat->structurally_symmetric = flg; 5549 mat->structurally_symmetric_set = PETSC_TRUE; 5550 break; 5551 case MAT_SYMMETRY_ETERNAL: 5552 mat->symmetric_eternal = flg; 5553 break; 5554 case MAT_STRUCTURE_ONLY: 5555 mat->structure_only = flg; 5556 break; 5557 case MAT_SORTED_FULL: 5558 mat->sortedfull = flg; 5559 break; 5560 default: 5561 break; 5562 } 5563 if (mat->ops->setoption) { 5564 ierr = (*mat->ops->setoption)(mat,op,flg);CHKERRQ(ierr); 5565 } 5566 PetscFunctionReturn(0); 5567 } 5568 5569 /*@ 5570 MatGetOption - Gets a parameter option that has been set for a matrix. 5571 5572 Logically Collective on Mat for certain operations, such as MAT_SPD, not collective for MAT_ROW_ORIENTED, see MatOption 5573 5574 Input Parameters: 5575 + mat - the matrix 5576 - option - the option, this only responds to certain options, check the code for which ones 5577 5578 Output Parameter: 5579 . flg - turn the option on (PETSC_TRUE) or off (PETSC_FALSE) 5580 5581 Notes: 5582 Can only be called after MatSetSizes() and MatSetType() have been set. 5583 5584 Level: intermediate 5585 5586 .seealso: MatOption, MatSetOption() 5587 5588 @*/ 5589 PetscErrorCode MatGetOption(Mat mat,MatOption op,PetscBool *flg) 5590 { 5591 PetscFunctionBegin; 5592 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5593 PetscValidType(mat,1); 5594 5595 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); 5596 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()"); 5597 5598 switch (op) { 5599 case MAT_NO_OFF_PROC_ENTRIES: 5600 *flg = mat->nooffprocentries; 5601 break; 5602 case MAT_NO_OFF_PROC_ZERO_ROWS: 5603 *flg = mat->nooffproczerorows; 5604 break; 5605 case MAT_SYMMETRIC: 5606 *flg = mat->symmetric; 5607 break; 5608 case MAT_HERMITIAN: 5609 *flg = mat->hermitian; 5610 break; 5611 case MAT_STRUCTURALLY_SYMMETRIC: 5612 *flg = mat->structurally_symmetric; 5613 break; 5614 case MAT_SYMMETRY_ETERNAL: 5615 *flg = mat->symmetric_eternal; 5616 break; 5617 case MAT_SPD: 5618 *flg = mat->spd; 5619 break; 5620 default: 5621 break; 5622 } 5623 PetscFunctionReturn(0); 5624 } 5625 5626 /*@ 5627 MatZeroEntries - Zeros all entries of a matrix. For sparse matrices 5628 this routine retains the old nonzero structure. 5629 5630 Logically Collective on Mat 5631 5632 Input Parameters: 5633 . mat - the matrix 5634 5635 Level: intermediate 5636 5637 Notes: 5638 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. 5639 See the Performance chapter of the users manual for information on preallocating matrices. 5640 5641 .seealso: MatZeroRows() 5642 @*/ 5643 PetscErrorCode MatZeroEntries(Mat mat) 5644 { 5645 PetscErrorCode ierr; 5646 5647 PetscFunctionBegin; 5648 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5649 PetscValidType(mat,1); 5650 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5651 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"); 5652 if (!mat->ops->zeroentries) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 5653 MatCheckPreallocated(mat,1); 5654 5655 ierr = PetscLogEventBegin(MAT_ZeroEntries,mat,0,0,0);CHKERRQ(ierr); 5656 ierr = (*mat->ops->zeroentries)(mat);CHKERRQ(ierr); 5657 ierr = PetscLogEventEnd(MAT_ZeroEntries,mat,0,0,0);CHKERRQ(ierr); 5658 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 5659 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 5660 if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) { 5661 mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU; 5662 } 5663 #endif 5664 PetscFunctionReturn(0); 5665 } 5666 5667 /*@ 5668 MatZeroRowsColumns - Zeros all entries (except possibly the main diagonal) 5669 of a set of rows and columns of a matrix. 5670 5671 Collective on Mat 5672 5673 Input Parameters: 5674 + mat - the matrix 5675 . numRows - the number of rows to remove 5676 . rows - the global row indices 5677 . diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry) 5678 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 5679 - b - optional vector of right hand side, that will be adjusted by provided solution 5680 5681 Notes: 5682 This does not change the nonzero structure of the matrix, it merely zeros those entries in the matrix. 5683 5684 The user can set a value in the diagonal entry (or for the AIJ and 5685 row formats can optionally remove the main diagonal entry from the 5686 nonzero structure as well, by passing 0.0 as the final argument). 5687 5688 For the parallel case, all processes that share the matrix (i.e., 5689 those in the communicator used for matrix creation) MUST call this 5690 routine, regardless of whether any rows being zeroed are owned by 5691 them. 5692 5693 Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to 5694 list only rows local to itself). 5695 5696 The option MAT_NO_OFF_PROC_ZERO_ROWS does not apply to this routine. 5697 5698 Level: intermediate 5699 5700 .seealso: MatZeroRowsIS(), MatZeroRows(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 5701 MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil() 5702 @*/ 5703 PetscErrorCode MatZeroRowsColumns(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 5704 { 5705 PetscErrorCode ierr; 5706 5707 PetscFunctionBegin; 5708 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5709 PetscValidType(mat,1); 5710 if (numRows) PetscValidIntPointer(rows,3); 5711 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5712 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5713 if (!mat->ops->zerorowscolumns) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 5714 MatCheckPreallocated(mat,1); 5715 5716 ierr = (*mat->ops->zerorowscolumns)(mat,numRows,rows,diag,x,b);CHKERRQ(ierr); 5717 ierr = MatViewFromOptions(mat,NULL,"-mat_view");CHKERRQ(ierr); 5718 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 5719 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 5720 if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) { 5721 mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU; 5722 } 5723 #endif 5724 PetscFunctionReturn(0); 5725 } 5726 5727 /*@ 5728 MatZeroRowsColumnsIS - Zeros all entries (except possibly the main diagonal) 5729 of a set of rows and columns of a matrix. 5730 5731 Collective on Mat 5732 5733 Input Parameters: 5734 + mat - the matrix 5735 . is - the rows to zero 5736 . diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry) 5737 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 5738 - b - optional vector of right hand side, that will be adjusted by provided solution 5739 5740 Notes: 5741 This does not change the nonzero structure of the matrix, it merely zeros those entries in the matrix. 5742 5743 The user can set a value in the diagonal entry (or for the AIJ and 5744 row formats can optionally remove the main diagonal entry from the 5745 nonzero structure as well, by passing 0.0 as the final argument). 5746 5747 For the parallel case, all processes that share the matrix (i.e., 5748 those in the communicator used for matrix creation) MUST call this 5749 routine, regardless of whether any rows being zeroed are owned by 5750 them. 5751 5752 Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to 5753 list only rows local to itself). 5754 5755 The option MAT_NO_OFF_PROC_ZERO_ROWS does not apply to this routine. 5756 5757 Level: intermediate 5758 5759 .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 5760 MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRows(), MatZeroRowsColumnsStencil() 5761 @*/ 5762 PetscErrorCode MatZeroRowsColumnsIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b) 5763 { 5764 PetscErrorCode ierr; 5765 PetscInt numRows; 5766 const PetscInt *rows; 5767 5768 PetscFunctionBegin; 5769 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5770 PetscValidHeaderSpecific(is,IS_CLASSID,2); 5771 PetscValidType(mat,1); 5772 PetscValidType(is,2); 5773 ierr = ISGetLocalSize(is,&numRows);CHKERRQ(ierr); 5774 ierr = ISGetIndices(is,&rows);CHKERRQ(ierr); 5775 ierr = MatZeroRowsColumns(mat,numRows,rows,diag,x,b);CHKERRQ(ierr); 5776 ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr); 5777 PetscFunctionReturn(0); 5778 } 5779 5780 /*@ 5781 MatZeroRows - Zeros all entries (except possibly the main diagonal) 5782 of a set of rows of a matrix. 5783 5784 Collective on Mat 5785 5786 Input Parameters: 5787 + mat - the matrix 5788 . numRows - the number of rows to remove 5789 . rows - the global row indices 5790 . diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry) 5791 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 5792 - b - optional vector of right hand side, that will be adjusted by provided solution 5793 5794 Notes: 5795 For the AIJ and BAIJ matrix formats this removes the old nonzero structure, 5796 but does not release memory. For the dense and block diagonal 5797 formats this does not alter the nonzero structure. 5798 5799 If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure 5800 of the matrix is not changed (even for AIJ and BAIJ matrices) the values are 5801 merely zeroed. 5802 5803 The user can set a value in the diagonal entry (or for the AIJ and 5804 row formats can optionally remove the main diagonal entry from the 5805 nonzero structure as well, by passing 0.0 as the final argument). 5806 5807 For the parallel case, all processes that share the matrix (i.e., 5808 those in the communicator used for matrix creation) MUST call this 5809 routine, regardless of whether any rows being zeroed are owned by 5810 them. 5811 5812 Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to 5813 list only rows local to itself). 5814 5815 You can call MatSetOption(mat,MAT_NO_OFF_PROC_ZERO_ROWS,PETSC_TRUE) if each process indicates only rows it 5816 owns that are to be zeroed. This saves a global synchronization in the implementation. 5817 5818 Level: intermediate 5819 5820 .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 5821 MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil() 5822 @*/ 5823 PetscErrorCode MatZeroRows(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 5824 { 5825 PetscErrorCode ierr; 5826 5827 PetscFunctionBegin; 5828 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5829 PetscValidType(mat,1); 5830 if (numRows) PetscValidIntPointer(rows,3); 5831 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5832 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5833 if (!mat->ops->zerorows) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 5834 MatCheckPreallocated(mat,1); 5835 5836 ierr = (*mat->ops->zerorows)(mat,numRows,rows,diag,x,b);CHKERRQ(ierr); 5837 ierr = MatViewFromOptions(mat,NULL,"-mat_view");CHKERRQ(ierr); 5838 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 5839 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 5840 if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) { 5841 mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU; 5842 } 5843 #endif 5844 PetscFunctionReturn(0); 5845 } 5846 5847 /*@ 5848 MatZeroRowsIS - Zeros all entries (except possibly the main diagonal) 5849 of a set of rows of a matrix. 5850 5851 Collective on Mat 5852 5853 Input Parameters: 5854 + mat - the matrix 5855 . is - index set of rows to remove 5856 . diag - value put in all diagonals of eliminated rows 5857 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 5858 - b - optional vector of right hand side, that will be adjusted by provided solution 5859 5860 Notes: 5861 For the AIJ and BAIJ matrix formats this removes the old nonzero structure, 5862 but does not release memory. For the dense and block diagonal 5863 formats this does not alter the nonzero structure. 5864 5865 If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure 5866 of the matrix is not changed (even for AIJ and BAIJ matrices) the values are 5867 merely zeroed. 5868 5869 The user can set a value in the diagonal entry (or for the AIJ and 5870 row formats can optionally remove the main diagonal entry from the 5871 nonzero structure as well, by passing 0.0 as the final argument). 5872 5873 For the parallel case, all processes that share the matrix (i.e., 5874 those in the communicator used for matrix creation) MUST call this 5875 routine, regardless of whether any rows being zeroed are owned by 5876 them. 5877 5878 Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to 5879 list only rows local to itself). 5880 5881 You can call MatSetOption(mat,MAT_NO_OFF_PROC_ZERO_ROWS,PETSC_TRUE) if each process indicates only rows it 5882 owns that are to be zeroed. This saves a global synchronization in the implementation. 5883 5884 Level: intermediate 5885 5886 .seealso: MatZeroRows(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 5887 MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil() 5888 @*/ 5889 PetscErrorCode MatZeroRowsIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b) 5890 { 5891 PetscInt numRows; 5892 const PetscInt *rows; 5893 PetscErrorCode ierr; 5894 5895 PetscFunctionBegin; 5896 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5897 PetscValidType(mat,1); 5898 PetscValidHeaderSpecific(is,IS_CLASSID,2); 5899 ierr = ISGetLocalSize(is,&numRows);CHKERRQ(ierr); 5900 ierr = ISGetIndices(is,&rows);CHKERRQ(ierr); 5901 ierr = MatZeroRows(mat,numRows,rows,diag,x,b);CHKERRQ(ierr); 5902 ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr); 5903 PetscFunctionReturn(0); 5904 } 5905 5906 /*@ 5907 MatZeroRowsStencil - Zeros all entries (except possibly the main diagonal) 5908 of a set of rows of a matrix. These rows must be local to the process. 5909 5910 Collective on Mat 5911 5912 Input Parameters: 5913 + mat - the matrix 5914 . numRows - the number of rows to remove 5915 . rows - the grid coordinates (and component number when dof > 1) for matrix rows 5916 . diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry) 5917 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 5918 - b - optional vector of right hand side, that will be adjusted by provided solution 5919 5920 Notes: 5921 For the AIJ and BAIJ matrix formats this removes the old nonzero structure, 5922 but does not release memory. For the dense and block diagonal 5923 formats this does not alter the nonzero structure. 5924 5925 If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure 5926 of the matrix is not changed (even for AIJ and BAIJ matrices) the values are 5927 merely zeroed. 5928 5929 The user can set a value in the diagonal entry (or for the AIJ and 5930 row formats can optionally remove the main diagonal entry from the 5931 nonzero structure as well, by passing 0.0 as the final argument). 5932 5933 For the parallel case, all processes that share the matrix (i.e., 5934 those in the communicator used for matrix creation) MUST call this 5935 routine, regardless of whether any rows being zeroed are owned by 5936 them. 5937 5938 Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to 5939 list only rows local to itself). 5940 5941 The grid coordinates are across the entire grid, not just the local portion 5942 5943 In Fortran idxm and idxn should be declared as 5944 $ MatStencil idxm(4,m) 5945 and the values inserted using 5946 $ idxm(MatStencil_i,1) = i 5947 $ idxm(MatStencil_j,1) = j 5948 $ idxm(MatStencil_k,1) = k 5949 $ idxm(MatStencil_c,1) = c 5950 etc 5951 5952 For periodic boundary conditions use negative indices for values to the left (below 0; that are to be 5953 obtained by wrapping values from right edge). For values to the right of the last entry using that index plus one 5954 etc to obtain values that obtained by wrapping the values from the left edge. This does not work for anything but the 5955 DM_BOUNDARY_PERIODIC boundary type. 5956 5957 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 5958 a single value per point) you can skip filling those indices. 5959 5960 Level: intermediate 5961 5962 .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsl(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 5963 MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil() 5964 @*/ 5965 PetscErrorCode MatZeroRowsStencil(Mat mat,PetscInt numRows,const MatStencil rows[],PetscScalar diag,Vec x,Vec b) 5966 { 5967 PetscInt dim = mat->stencil.dim; 5968 PetscInt sdim = dim - (1 - (PetscInt) mat->stencil.noc); 5969 PetscInt *dims = mat->stencil.dims+1; 5970 PetscInt *starts = mat->stencil.starts; 5971 PetscInt *dxm = (PetscInt*) rows; 5972 PetscInt *jdxm, i, j, tmp, numNewRows = 0; 5973 PetscErrorCode ierr; 5974 5975 PetscFunctionBegin; 5976 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5977 PetscValidType(mat,1); 5978 if (numRows) PetscValidIntPointer(rows,3); 5979 5980 ierr = PetscMalloc1(numRows, &jdxm);CHKERRQ(ierr); 5981 for (i = 0; i < numRows; ++i) { 5982 /* Skip unused dimensions (they are ordered k, j, i, c) */ 5983 for (j = 0; j < 3-sdim; ++j) dxm++; 5984 /* Local index in X dir */ 5985 tmp = *dxm++ - starts[0]; 5986 /* Loop over remaining dimensions */ 5987 for (j = 0; j < dim-1; ++j) { 5988 /* If nonlocal, set index to be negative */ 5989 if ((*dxm++ - starts[j+1]) < 0 || tmp < 0) tmp = PETSC_MIN_INT; 5990 /* Update local index */ 5991 else tmp = tmp*dims[j] + *(dxm-1) - starts[j+1]; 5992 } 5993 /* Skip component slot if necessary */ 5994 if (mat->stencil.noc) dxm++; 5995 /* Local row number */ 5996 if (tmp >= 0) { 5997 jdxm[numNewRows++] = tmp; 5998 } 5999 } 6000 ierr = MatZeroRowsLocal(mat,numNewRows,jdxm,diag,x,b);CHKERRQ(ierr); 6001 ierr = PetscFree(jdxm);CHKERRQ(ierr); 6002 PetscFunctionReturn(0); 6003 } 6004 6005 /*@ 6006 MatZeroRowsColumnsStencil - Zeros all row and column entries (except possibly the main diagonal) 6007 of a set of rows and columns of a matrix. 6008 6009 Collective on Mat 6010 6011 Input Parameters: 6012 + mat - the matrix 6013 . numRows - the number of rows/columns to remove 6014 . rows - the grid coordinates (and component number when dof > 1) for matrix rows 6015 . diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry) 6016 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 6017 - b - optional vector of right hand side, that will be adjusted by provided solution 6018 6019 Notes: 6020 For the AIJ and BAIJ matrix formats this removes the old nonzero structure, 6021 but does not release memory. For the dense and block diagonal 6022 formats this does not alter the nonzero structure. 6023 6024 If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure 6025 of the matrix is not changed (even for AIJ and BAIJ matrices) the values are 6026 merely zeroed. 6027 6028 The user can set a value in the diagonal entry (or for the AIJ and 6029 row formats can optionally remove the main diagonal entry from the 6030 nonzero structure as well, by passing 0.0 as the final argument). 6031 6032 For the parallel case, all processes that share the matrix (i.e., 6033 those in the communicator used for matrix creation) MUST call this 6034 routine, regardless of whether any rows being zeroed are owned by 6035 them. 6036 6037 Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to 6038 list only rows local to itself, but the row/column numbers are given in local numbering). 6039 6040 The grid coordinates are across the entire grid, not just the local portion 6041 6042 In Fortran idxm and idxn should be declared as 6043 $ MatStencil idxm(4,m) 6044 and the values inserted using 6045 $ idxm(MatStencil_i,1) = i 6046 $ idxm(MatStencil_j,1) = j 6047 $ idxm(MatStencil_k,1) = k 6048 $ idxm(MatStencil_c,1) = c 6049 etc 6050 6051 For periodic boundary conditions use negative indices for values to the left (below 0; that are to be 6052 obtained by wrapping values from right edge). For values to the right of the last entry using that index plus one 6053 etc to obtain values that obtained by wrapping the values from the left edge. This does not work for anything but the 6054 DM_BOUNDARY_PERIODIC boundary type. 6055 6056 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 6057 a single value per point) you can skip filling those indices. 6058 6059 Level: intermediate 6060 6061 .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 6062 MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRows() 6063 @*/ 6064 PetscErrorCode MatZeroRowsColumnsStencil(Mat mat,PetscInt numRows,const MatStencil rows[],PetscScalar diag,Vec x,Vec b) 6065 { 6066 PetscInt dim = mat->stencil.dim; 6067 PetscInt sdim = dim - (1 - (PetscInt) mat->stencil.noc); 6068 PetscInt *dims = mat->stencil.dims+1; 6069 PetscInt *starts = mat->stencil.starts; 6070 PetscInt *dxm = (PetscInt*) rows; 6071 PetscInt *jdxm, i, j, tmp, numNewRows = 0; 6072 PetscErrorCode ierr; 6073 6074 PetscFunctionBegin; 6075 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6076 PetscValidType(mat,1); 6077 if (numRows) PetscValidIntPointer(rows,3); 6078 6079 ierr = PetscMalloc1(numRows, &jdxm);CHKERRQ(ierr); 6080 for (i = 0; i < numRows; ++i) { 6081 /* Skip unused dimensions (they are ordered k, j, i, c) */ 6082 for (j = 0; j < 3-sdim; ++j) dxm++; 6083 /* Local index in X dir */ 6084 tmp = *dxm++ - starts[0]; 6085 /* Loop over remaining dimensions */ 6086 for (j = 0; j < dim-1; ++j) { 6087 /* If nonlocal, set index to be negative */ 6088 if ((*dxm++ - starts[j+1]) < 0 || tmp < 0) tmp = PETSC_MIN_INT; 6089 /* Update local index */ 6090 else tmp = tmp*dims[j] + *(dxm-1) - starts[j+1]; 6091 } 6092 /* Skip component slot if necessary */ 6093 if (mat->stencil.noc) dxm++; 6094 /* Local row number */ 6095 if (tmp >= 0) { 6096 jdxm[numNewRows++] = tmp; 6097 } 6098 } 6099 ierr = MatZeroRowsColumnsLocal(mat,numNewRows,jdxm,diag,x,b);CHKERRQ(ierr); 6100 ierr = PetscFree(jdxm);CHKERRQ(ierr); 6101 PetscFunctionReturn(0); 6102 } 6103 6104 /*@C 6105 MatZeroRowsLocal - Zeros all entries (except possibly the main diagonal) 6106 of a set of rows of a matrix; using local numbering of rows. 6107 6108 Collective on Mat 6109 6110 Input Parameters: 6111 + mat - the matrix 6112 . numRows - the number of rows to remove 6113 . rows - the global row indices 6114 . diag - value put in all diagonals of eliminated rows 6115 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 6116 - b - optional vector of right hand side, that will be adjusted by provided solution 6117 6118 Notes: 6119 Before calling MatZeroRowsLocal(), the user must first set the 6120 local-to-global mapping by calling MatSetLocalToGlobalMapping(). 6121 6122 For the AIJ matrix formats this removes the old nonzero structure, 6123 but does not release memory. For the dense and block diagonal 6124 formats this does not alter the nonzero structure. 6125 6126 If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure 6127 of the matrix is not changed (even for AIJ and BAIJ matrices) the values are 6128 merely zeroed. 6129 6130 The user can set a value in the diagonal entry (or for the AIJ and 6131 row formats can optionally remove the main diagonal entry from the 6132 nonzero structure as well, by passing 0.0 as the final argument). 6133 6134 You can call MatSetOption(mat,MAT_NO_OFF_PROC_ZERO_ROWS,PETSC_TRUE) if each process indicates only rows it 6135 owns that are to be zeroed. This saves a global synchronization in the implementation. 6136 6137 Level: intermediate 6138 6139 .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRows(), MatSetOption(), 6140 MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil() 6141 @*/ 6142 PetscErrorCode MatZeroRowsLocal(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 6143 { 6144 PetscErrorCode ierr; 6145 6146 PetscFunctionBegin; 6147 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6148 PetscValidType(mat,1); 6149 if (numRows) PetscValidIntPointer(rows,3); 6150 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 6151 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6152 MatCheckPreallocated(mat,1); 6153 6154 if (mat->ops->zerorowslocal) { 6155 ierr = (*mat->ops->zerorowslocal)(mat,numRows,rows,diag,x,b);CHKERRQ(ierr); 6156 } else { 6157 IS is, newis; 6158 const PetscInt *newRows; 6159 6160 if (!mat->rmap->mapping) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Need to provide local to global mapping to matrix first"); 6161 ierr = ISCreateGeneral(PETSC_COMM_SELF,numRows,rows,PETSC_COPY_VALUES,&is);CHKERRQ(ierr); 6162 ierr = ISLocalToGlobalMappingApplyIS(mat->rmap->mapping,is,&newis);CHKERRQ(ierr); 6163 ierr = ISGetIndices(newis,&newRows);CHKERRQ(ierr); 6164 ierr = (*mat->ops->zerorows)(mat,numRows,newRows,diag,x,b);CHKERRQ(ierr); 6165 ierr = ISRestoreIndices(newis,&newRows);CHKERRQ(ierr); 6166 ierr = ISDestroy(&newis);CHKERRQ(ierr); 6167 ierr = ISDestroy(&is);CHKERRQ(ierr); 6168 } 6169 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 6170 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 6171 if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) { 6172 mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU; 6173 } 6174 #endif 6175 PetscFunctionReturn(0); 6176 } 6177 6178 /*@ 6179 MatZeroRowsLocalIS - Zeros all entries (except possibly the main diagonal) 6180 of a set of rows of a matrix; using local numbering of rows. 6181 6182 Collective on Mat 6183 6184 Input Parameters: 6185 + mat - the matrix 6186 . is - index set of rows to remove 6187 . diag - value put in all diagonals of eliminated rows 6188 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 6189 - b - optional vector of right hand side, that will be adjusted by provided solution 6190 6191 Notes: 6192 Before calling MatZeroRowsLocalIS(), the user must first set the 6193 local-to-global mapping by calling MatSetLocalToGlobalMapping(). 6194 6195 For the AIJ matrix formats this removes the old nonzero structure, 6196 but does not release memory. For the dense and block diagonal 6197 formats this does not alter the nonzero structure. 6198 6199 If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure 6200 of the matrix is not changed (even for AIJ and BAIJ matrices) the values are 6201 merely zeroed. 6202 6203 The user can set a value in the diagonal entry (or for the AIJ and 6204 row formats can optionally remove the main diagonal entry from the 6205 nonzero structure as well, by passing 0.0 as the final argument). 6206 6207 You can call MatSetOption(mat,MAT_NO_OFF_PROC_ZERO_ROWS,PETSC_TRUE) if each process indicates only rows it 6208 owns that are to be zeroed. This saves a global synchronization in the implementation. 6209 6210 Level: intermediate 6211 6212 .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRows(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 6213 MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil() 6214 @*/ 6215 PetscErrorCode MatZeroRowsLocalIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b) 6216 { 6217 PetscErrorCode ierr; 6218 PetscInt numRows; 6219 const PetscInt *rows; 6220 6221 PetscFunctionBegin; 6222 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6223 PetscValidType(mat,1); 6224 PetscValidHeaderSpecific(is,IS_CLASSID,2); 6225 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 6226 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6227 MatCheckPreallocated(mat,1); 6228 6229 ierr = ISGetLocalSize(is,&numRows);CHKERRQ(ierr); 6230 ierr = ISGetIndices(is,&rows);CHKERRQ(ierr); 6231 ierr = MatZeroRowsLocal(mat,numRows,rows,diag,x,b);CHKERRQ(ierr); 6232 ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr); 6233 PetscFunctionReturn(0); 6234 } 6235 6236 /*@ 6237 MatZeroRowsColumnsLocal - Zeros all entries (except possibly the main diagonal) 6238 of a set of rows and columns of a matrix; using local numbering of rows. 6239 6240 Collective on Mat 6241 6242 Input Parameters: 6243 + mat - the matrix 6244 . numRows - the number of rows to remove 6245 . rows - the global row indices 6246 . diag - value put in all diagonals of eliminated rows 6247 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 6248 - b - optional vector of right hand side, that will be adjusted by provided solution 6249 6250 Notes: 6251 Before calling MatZeroRowsColumnsLocal(), the user must first set the 6252 local-to-global mapping by calling MatSetLocalToGlobalMapping(). 6253 6254 The user can set a value in the diagonal entry (or for the AIJ and 6255 row formats can optionally remove the main diagonal entry from the 6256 nonzero structure as well, by passing 0.0 as the final argument). 6257 6258 Level: intermediate 6259 6260 .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 6261 MatZeroRows(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil() 6262 @*/ 6263 PetscErrorCode MatZeroRowsColumnsLocal(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 6264 { 6265 PetscErrorCode ierr; 6266 IS is, newis; 6267 const PetscInt *newRows; 6268 6269 PetscFunctionBegin; 6270 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6271 PetscValidType(mat,1); 6272 if (numRows) PetscValidIntPointer(rows,3); 6273 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 6274 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6275 MatCheckPreallocated(mat,1); 6276 6277 if (!mat->cmap->mapping) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Need to provide local to global mapping to matrix first"); 6278 ierr = ISCreateGeneral(PETSC_COMM_SELF,numRows,rows,PETSC_COPY_VALUES,&is);CHKERRQ(ierr); 6279 ierr = ISLocalToGlobalMappingApplyIS(mat->cmap->mapping,is,&newis);CHKERRQ(ierr); 6280 ierr = ISGetIndices(newis,&newRows);CHKERRQ(ierr); 6281 ierr = (*mat->ops->zerorowscolumns)(mat,numRows,newRows,diag,x,b);CHKERRQ(ierr); 6282 ierr = ISRestoreIndices(newis,&newRows);CHKERRQ(ierr); 6283 ierr = ISDestroy(&newis);CHKERRQ(ierr); 6284 ierr = ISDestroy(&is);CHKERRQ(ierr); 6285 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 6286 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 6287 if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) { 6288 mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU; 6289 } 6290 #endif 6291 PetscFunctionReturn(0); 6292 } 6293 6294 /*@ 6295 MatZeroRowsColumnsLocalIS - Zeros all entries (except possibly the main diagonal) 6296 of a set of rows and columns of a matrix; using local numbering of rows. 6297 6298 Collective on Mat 6299 6300 Input Parameters: 6301 + mat - the matrix 6302 . is - index set of rows to remove 6303 . diag - value put in all diagonals of eliminated rows 6304 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 6305 - b - optional vector of right hand side, that will be adjusted by provided solution 6306 6307 Notes: 6308 Before calling MatZeroRowsColumnsLocalIS(), the user must first set the 6309 local-to-global mapping by calling MatSetLocalToGlobalMapping(). 6310 6311 The user can set a value in the diagonal entry (or for the AIJ and 6312 row formats can optionally remove the main diagonal entry from the 6313 nonzero structure as well, by passing 0.0 as the final argument). 6314 6315 Level: intermediate 6316 6317 .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 6318 MatZeroRowsColumnsLocal(), MatZeroRows(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil() 6319 @*/ 6320 PetscErrorCode MatZeroRowsColumnsLocalIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b) 6321 { 6322 PetscErrorCode ierr; 6323 PetscInt numRows; 6324 const PetscInt *rows; 6325 6326 PetscFunctionBegin; 6327 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6328 PetscValidType(mat,1); 6329 PetscValidHeaderSpecific(is,IS_CLASSID,2); 6330 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 6331 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6332 MatCheckPreallocated(mat,1); 6333 6334 ierr = ISGetLocalSize(is,&numRows);CHKERRQ(ierr); 6335 ierr = ISGetIndices(is,&rows);CHKERRQ(ierr); 6336 ierr = MatZeroRowsColumnsLocal(mat,numRows,rows,diag,x,b);CHKERRQ(ierr); 6337 ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr); 6338 PetscFunctionReturn(0); 6339 } 6340 6341 /*@C 6342 MatGetSize - Returns the numbers of rows and columns in a matrix. 6343 6344 Not Collective 6345 6346 Input Parameter: 6347 . mat - the matrix 6348 6349 Output Parameters: 6350 + m - the number of global rows 6351 - n - the number of global columns 6352 6353 Note: both output parameters can be NULL on input. 6354 6355 Level: beginner 6356 6357 .seealso: MatGetLocalSize() 6358 @*/ 6359 PetscErrorCode MatGetSize(Mat mat,PetscInt *m,PetscInt *n) 6360 { 6361 PetscFunctionBegin; 6362 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6363 if (m) *m = mat->rmap->N; 6364 if (n) *n = mat->cmap->N; 6365 PetscFunctionReturn(0); 6366 } 6367 6368 /*@C 6369 MatGetLocalSize - Returns the number of rows and columns in a matrix 6370 stored locally. This information may be implementation dependent, so 6371 use with care. 6372 6373 Not Collective 6374 6375 Input Parameters: 6376 . mat - the matrix 6377 6378 Output Parameters: 6379 + m - the number of local rows 6380 - n - the number of local columns 6381 6382 Note: both output parameters can be NULL on input. 6383 6384 Level: beginner 6385 6386 .seealso: MatGetSize() 6387 @*/ 6388 PetscErrorCode MatGetLocalSize(Mat mat,PetscInt *m,PetscInt *n) 6389 { 6390 PetscFunctionBegin; 6391 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6392 if (m) PetscValidIntPointer(m,2); 6393 if (n) PetscValidIntPointer(n,3); 6394 if (m) *m = mat->rmap->n; 6395 if (n) *n = mat->cmap->n; 6396 PetscFunctionReturn(0); 6397 } 6398 6399 /*@C 6400 MatGetOwnershipRangeColumn - Returns the range of matrix columns associated with rows of a vector one multiplies by that owned by 6401 this processor. (The columns of the "diagonal block") 6402 6403 Not Collective, unless matrix has not been allocated, then collective on Mat 6404 6405 Input Parameters: 6406 . mat - the matrix 6407 6408 Output Parameters: 6409 + m - the global index of the first local column 6410 - n - one more than the global index of the last local column 6411 6412 Notes: 6413 both output parameters can be NULL on input. 6414 6415 Level: developer 6416 6417 .seealso: MatGetOwnershipRange(), MatGetOwnershipRanges(), MatGetOwnershipRangesColumn() 6418 6419 @*/ 6420 PetscErrorCode MatGetOwnershipRangeColumn(Mat mat,PetscInt *m,PetscInt *n) 6421 { 6422 PetscFunctionBegin; 6423 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6424 PetscValidType(mat,1); 6425 if (m) PetscValidIntPointer(m,2); 6426 if (n) PetscValidIntPointer(n,3); 6427 MatCheckPreallocated(mat,1); 6428 if (m) *m = mat->cmap->rstart; 6429 if (n) *n = mat->cmap->rend; 6430 PetscFunctionReturn(0); 6431 } 6432 6433 /*@C 6434 MatGetOwnershipRange - Returns the range of matrix rows owned by 6435 this processor, assuming that the matrix is laid out with the first 6436 n1 rows on the first processor, the next n2 rows on the second, etc. 6437 For certain parallel layouts this range may not be well defined. 6438 6439 Not Collective 6440 6441 Input Parameters: 6442 . mat - the matrix 6443 6444 Output Parameters: 6445 + m - the global index of the first local row 6446 - n - one more than the global index of the last local row 6447 6448 Note: Both output parameters can be NULL on input. 6449 $ This function requires that the matrix be preallocated. If you have not preallocated, consider using 6450 $ PetscSplitOwnership(MPI_Comm comm, PetscInt *n, PetscInt *N) 6451 $ and then MPI_Scan() to calculate prefix sums of the local sizes. 6452 6453 Level: beginner 6454 6455 .seealso: MatGetOwnershipRanges(), MatGetOwnershipRangeColumn(), MatGetOwnershipRangesColumn(), PetscSplitOwnership(), PetscSplitOwnershipBlock() 6456 6457 @*/ 6458 PetscErrorCode MatGetOwnershipRange(Mat mat,PetscInt *m,PetscInt *n) 6459 { 6460 PetscFunctionBegin; 6461 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6462 PetscValidType(mat,1); 6463 if (m) PetscValidIntPointer(m,2); 6464 if (n) PetscValidIntPointer(n,3); 6465 MatCheckPreallocated(mat,1); 6466 if (m) *m = mat->rmap->rstart; 6467 if (n) *n = mat->rmap->rend; 6468 PetscFunctionReturn(0); 6469 } 6470 6471 /*@C 6472 MatGetOwnershipRanges - Returns the range of matrix rows owned by 6473 each process 6474 6475 Not Collective, unless matrix has not been allocated, then collective on Mat 6476 6477 Input Parameters: 6478 . mat - the matrix 6479 6480 Output Parameters: 6481 . ranges - start of each processors portion plus one more than the total length at the end 6482 6483 Level: beginner 6484 6485 .seealso: MatGetOwnershipRange(), MatGetOwnershipRangeColumn(), MatGetOwnershipRangesColumn() 6486 6487 @*/ 6488 PetscErrorCode MatGetOwnershipRanges(Mat mat,const PetscInt **ranges) 6489 { 6490 PetscErrorCode ierr; 6491 6492 PetscFunctionBegin; 6493 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6494 PetscValidType(mat,1); 6495 MatCheckPreallocated(mat,1); 6496 ierr = PetscLayoutGetRanges(mat->rmap,ranges);CHKERRQ(ierr); 6497 PetscFunctionReturn(0); 6498 } 6499 6500 /*@C 6501 MatGetOwnershipRangesColumn - Returns the range of matrix columns associated with rows of a vector one multiplies by that owned by 6502 this processor. (The columns of the "diagonal blocks" for each process) 6503 6504 Not Collective, unless matrix has not been allocated, then collective on Mat 6505 6506 Input Parameters: 6507 . mat - the matrix 6508 6509 Output Parameters: 6510 . ranges - start of each processors portion plus one more then the total length at the end 6511 6512 Level: beginner 6513 6514 .seealso: MatGetOwnershipRange(), MatGetOwnershipRangeColumn(), MatGetOwnershipRanges() 6515 6516 @*/ 6517 PetscErrorCode MatGetOwnershipRangesColumn(Mat mat,const PetscInt **ranges) 6518 { 6519 PetscErrorCode ierr; 6520 6521 PetscFunctionBegin; 6522 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6523 PetscValidType(mat,1); 6524 MatCheckPreallocated(mat,1); 6525 ierr = PetscLayoutGetRanges(mat->cmap,ranges);CHKERRQ(ierr); 6526 PetscFunctionReturn(0); 6527 } 6528 6529 /*@C 6530 MatGetOwnershipIS - Get row and column ownership as index sets 6531 6532 Not Collective 6533 6534 Input Arguments: 6535 . A - matrix of type Elemental 6536 6537 Output Arguments: 6538 + rows - rows in which this process owns elements 6539 - cols - columns in which this process owns elements 6540 6541 Level: intermediate 6542 6543 .seealso: MatGetOwnershipRange(), MatGetOwnershipRangeColumn(), MatSetValues(), MATELEMENTAL 6544 @*/ 6545 PetscErrorCode MatGetOwnershipIS(Mat A,IS *rows,IS *cols) 6546 { 6547 PetscErrorCode ierr,(*f)(Mat,IS*,IS*); 6548 6549 PetscFunctionBegin; 6550 MatCheckPreallocated(A,1); 6551 ierr = PetscObjectQueryFunction((PetscObject)A,"MatGetOwnershipIS_C",&f);CHKERRQ(ierr); 6552 if (f) { 6553 ierr = (*f)(A,rows,cols);CHKERRQ(ierr); 6554 } else { /* Create a standard row-based partition, each process is responsible for ALL columns in their row block */ 6555 if (rows) {ierr = ISCreateStride(PETSC_COMM_SELF,A->rmap->n,A->rmap->rstart,1,rows);CHKERRQ(ierr);} 6556 if (cols) {ierr = ISCreateStride(PETSC_COMM_SELF,A->cmap->N,0,1,cols);CHKERRQ(ierr);} 6557 } 6558 PetscFunctionReturn(0); 6559 } 6560 6561 /*@C 6562 MatILUFactorSymbolic - Performs symbolic ILU factorization of a matrix. 6563 Uses levels of fill only, not drop tolerance. Use MatLUFactorNumeric() 6564 to complete the factorization. 6565 6566 Collective on Mat 6567 6568 Input Parameters: 6569 + mat - the matrix 6570 . row - row permutation 6571 . column - column permutation 6572 - info - structure containing 6573 $ levels - number of levels of fill. 6574 $ expected fill - as ratio of original fill. 6575 $ 1 or 0 - indicating force fill on diagonal (improves robustness for matrices 6576 missing diagonal entries) 6577 6578 Output Parameters: 6579 . fact - new matrix that has been symbolically factored 6580 6581 Notes: 6582 See Users-Manual: ch_mat for additional information about choosing the fill factor for better efficiency. 6583 6584 Most users should employ the simplified KSP interface for linear solvers 6585 instead of working directly with matrix algebra routines such as this. 6586 See, e.g., KSPCreate(). 6587 6588 Level: developer 6589 6590 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor() 6591 MatGetOrdering(), MatFactorInfo 6592 6593 Note: this uses the definition of level of fill as in Y. Saad, 2003 6594 6595 Developer Note: fortran interface is not autogenerated as the f90 6596 interface defintion cannot be generated correctly [due to MatFactorInfo] 6597 6598 References: 6599 Y. Saad, Iterative methods for sparse linear systems Philadelphia: Society for Industrial and Applied Mathematics, 2003 6600 @*/ 6601 PetscErrorCode MatILUFactorSymbolic(Mat fact,Mat mat,IS row,IS col,const MatFactorInfo *info) 6602 { 6603 PetscErrorCode ierr; 6604 6605 PetscFunctionBegin; 6606 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6607 PetscValidType(mat,1); 6608 PetscValidHeaderSpecific(row,IS_CLASSID,2); 6609 PetscValidHeaderSpecific(col,IS_CLASSID,3); 6610 PetscValidPointer(info,4); 6611 PetscValidPointer(fact,5); 6612 if (info->levels < 0) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Levels of fill negative %D",(PetscInt)info->levels); 6613 if (info->fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %g",(double)info->fill); 6614 if (!(fact)->ops->ilufactorsymbolic) { 6615 MatSolverType spackage; 6616 ierr = MatFactorGetSolverType(fact,&spackage);CHKERRQ(ierr); 6617 SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s symbolic ILU using solver package %s",((PetscObject)mat)->type_name,spackage); 6618 } 6619 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 6620 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6621 MatCheckPreallocated(mat,2); 6622 6623 ierr = PetscLogEventBegin(MAT_ILUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr); 6624 ierr = (fact->ops->ilufactorsymbolic)(fact,mat,row,col,info);CHKERRQ(ierr); 6625 ierr = PetscLogEventEnd(MAT_ILUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr); 6626 PetscFunctionReturn(0); 6627 } 6628 6629 /*@C 6630 MatICCFactorSymbolic - Performs symbolic incomplete 6631 Cholesky factorization for a symmetric matrix. Use 6632 MatCholeskyFactorNumeric() to complete the factorization. 6633 6634 Collective on Mat 6635 6636 Input Parameters: 6637 + mat - the matrix 6638 . perm - row and column permutation 6639 - info - structure containing 6640 $ levels - number of levels of fill. 6641 $ expected fill - as ratio of original fill. 6642 6643 Output Parameter: 6644 . fact - the factored matrix 6645 6646 Notes: 6647 Most users should employ the KSP interface for linear solvers 6648 instead of working directly with matrix algebra routines such as this. 6649 See, e.g., KSPCreate(). 6650 6651 Level: developer 6652 6653 .seealso: MatCholeskyFactorNumeric(), MatCholeskyFactor(), MatFactorInfo 6654 6655 Note: this uses the definition of level of fill as in Y. Saad, 2003 6656 6657 Developer Note: fortran interface is not autogenerated as the f90 6658 interface defintion cannot be generated correctly [due to MatFactorInfo] 6659 6660 References: 6661 Y. Saad, Iterative methods for sparse linear systems Philadelphia: Society for Industrial and Applied Mathematics, 2003 6662 @*/ 6663 PetscErrorCode MatICCFactorSymbolic(Mat fact,Mat mat,IS perm,const MatFactorInfo *info) 6664 { 6665 PetscErrorCode ierr; 6666 6667 PetscFunctionBegin; 6668 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6669 PetscValidType(mat,1); 6670 PetscValidHeaderSpecific(perm,IS_CLASSID,2); 6671 PetscValidPointer(info,3); 6672 PetscValidPointer(fact,4); 6673 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6674 if (info->levels < 0) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Levels negative %D",(PetscInt) info->levels); 6675 if (info->fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %g",(double)info->fill); 6676 if (!(fact)->ops->iccfactorsymbolic) { 6677 MatSolverType spackage; 6678 ierr = MatFactorGetSolverType(fact,&spackage);CHKERRQ(ierr); 6679 SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s symbolic ICC using solver package %s",((PetscObject)mat)->type_name,spackage); 6680 } 6681 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 6682 MatCheckPreallocated(mat,2); 6683 6684 ierr = PetscLogEventBegin(MAT_ICCFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr); 6685 ierr = (fact->ops->iccfactorsymbolic)(fact,mat,perm,info);CHKERRQ(ierr); 6686 ierr = PetscLogEventEnd(MAT_ICCFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr); 6687 PetscFunctionReturn(0); 6688 } 6689 6690 /*@C 6691 MatCreateSubMatrices - Extracts several submatrices from a matrix. If submat 6692 points to an array of valid matrices, they may be reused to store the new 6693 submatrices. 6694 6695 Collective on Mat 6696 6697 Input Parameters: 6698 + mat - the matrix 6699 . n - the number of submatrixes to be extracted (on this processor, may be zero) 6700 . irow, icol - index sets of rows and columns to extract 6701 - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 6702 6703 Output Parameter: 6704 . submat - the array of submatrices 6705 6706 Notes: 6707 MatCreateSubMatrices() can extract ONLY sequential submatrices 6708 (from both sequential and parallel matrices). Use MatCreateSubMatrix() 6709 to extract a parallel submatrix. 6710 6711 Some matrix types place restrictions on the row and column 6712 indices, such as that they be sorted or that they be equal to each other. 6713 6714 The index sets may not have duplicate entries. 6715 6716 When extracting submatrices from a parallel matrix, each processor can 6717 form a different submatrix by setting the rows and columns of its 6718 individual index sets according to the local submatrix desired. 6719 6720 When finished using the submatrices, the user should destroy 6721 them with MatDestroySubMatrices(). 6722 6723 MAT_REUSE_MATRIX can only be used when the nonzero structure of the 6724 original matrix has not changed from that last call to MatCreateSubMatrices(). 6725 6726 This routine creates the matrices in submat; you should NOT create them before 6727 calling it. It also allocates the array of matrix pointers submat. 6728 6729 For BAIJ matrices the index sets must respect the block structure, that is if they 6730 request one row/column in a block, they must request all rows/columns that are in 6731 that block. For example, if the block size is 2 you cannot request just row 0 and 6732 column 0. 6733 6734 Fortran Note: 6735 The Fortran interface is slightly different from that given below; it 6736 requires one to pass in as submat a Mat (integer) array of size at least n+1. 6737 6738 Level: advanced 6739 6740 6741 .seealso: MatDestroySubMatrices(), MatCreateSubMatrix(), MatGetRow(), MatGetDiagonal(), MatReuse 6742 @*/ 6743 PetscErrorCode MatCreateSubMatrices(Mat mat,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *submat[]) 6744 { 6745 PetscErrorCode ierr; 6746 PetscInt i; 6747 PetscBool eq; 6748 6749 PetscFunctionBegin; 6750 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6751 PetscValidType(mat,1); 6752 if (n) { 6753 PetscValidPointer(irow,3); 6754 PetscValidHeaderSpecific(*irow,IS_CLASSID,3); 6755 PetscValidPointer(icol,4); 6756 PetscValidHeaderSpecific(*icol,IS_CLASSID,4); 6757 } 6758 PetscValidPointer(submat,6); 6759 if (n && scall == MAT_REUSE_MATRIX) { 6760 PetscValidPointer(*submat,6); 6761 PetscValidHeaderSpecific(**submat,MAT_CLASSID,6); 6762 } 6763 if (!mat->ops->createsubmatrices) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 6764 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 6765 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6766 MatCheckPreallocated(mat,1); 6767 6768 ierr = PetscLogEventBegin(MAT_CreateSubMats,mat,0,0,0);CHKERRQ(ierr); 6769 ierr = (*mat->ops->createsubmatrices)(mat,n,irow,icol,scall,submat);CHKERRQ(ierr); 6770 ierr = PetscLogEventEnd(MAT_CreateSubMats,mat,0,0,0);CHKERRQ(ierr); 6771 for (i=0; i<n; i++) { 6772 (*submat)[i]->factortype = MAT_FACTOR_NONE; /* in case in place factorization was previously done on submatrix */ 6773 if (mat->symmetric || mat->structurally_symmetric || mat->hermitian) { 6774 ierr = ISEqual(irow[i],icol[i],&eq);CHKERRQ(ierr); 6775 if (eq) { 6776 if (mat->symmetric) { 6777 ierr = MatSetOption((*submat)[i],MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr); 6778 } else if (mat->hermitian) { 6779 ierr = MatSetOption((*submat)[i],MAT_HERMITIAN,PETSC_TRUE);CHKERRQ(ierr); 6780 } else if (mat->structurally_symmetric) { 6781 ierr = MatSetOption((*submat)[i],MAT_STRUCTURALLY_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr); 6782 } 6783 } 6784 } 6785 } 6786 PetscFunctionReturn(0); 6787 } 6788 6789 /*@C 6790 MatCreateSubMatricesMPI - Extracts MPI submatrices across a sub communicator of mat (by pairs of IS that may live on subcomms). 6791 6792 Collective on Mat 6793 6794 Input Parameters: 6795 + mat - the matrix 6796 . n - the number of submatrixes to be extracted 6797 . irow, icol - index sets of rows and columns to extract 6798 - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 6799 6800 Output Parameter: 6801 . submat - the array of submatrices 6802 6803 Level: advanced 6804 6805 6806 .seealso: MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRow(), MatGetDiagonal(), MatReuse 6807 @*/ 6808 PetscErrorCode MatCreateSubMatricesMPI(Mat mat,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *submat[]) 6809 { 6810 PetscErrorCode ierr; 6811 PetscInt i; 6812 PetscBool eq; 6813 6814 PetscFunctionBegin; 6815 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6816 PetscValidType(mat,1); 6817 if (n) { 6818 PetscValidPointer(irow,3); 6819 PetscValidHeaderSpecific(*irow,IS_CLASSID,3); 6820 PetscValidPointer(icol,4); 6821 PetscValidHeaderSpecific(*icol,IS_CLASSID,4); 6822 } 6823 PetscValidPointer(submat,6); 6824 if (n && scall == MAT_REUSE_MATRIX) { 6825 PetscValidPointer(*submat,6); 6826 PetscValidHeaderSpecific(**submat,MAT_CLASSID,6); 6827 } 6828 if (!mat->ops->createsubmatricesmpi) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 6829 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 6830 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6831 MatCheckPreallocated(mat,1); 6832 6833 ierr = PetscLogEventBegin(MAT_CreateSubMats,mat,0,0,0);CHKERRQ(ierr); 6834 ierr = (*mat->ops->createsubmatricesmpi)(mat,n,irow,icol,scall,submat);CHKERRQ(ierr); 6835 ierr = PetscLogEventEnd(MAT_CreateSubMats,mat,0,0,0);CHKERRQ(ierr); 6836 for (i=0; i<n; i++) { 6837 if (mat->symmetric || mat->structurally_symmetric || mat->hermitian) { 6838 ierr = ISEqual(irow[i],icol[i],&eq);CHKERRQ(ierr); 6839 if (eq) { 6840 if (mat->symmetric) { 6841 ierr = MatSetOption((*submat)[i],MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr); 6842 } else if (mat->hermitian) { 6843 ierr = MatSetOption((*submat)[i],MAT_HERMITIAN,PETSC_TRUE);CHKERRQ(ierr); 6844 } else if (mat->structurally_symmetric) { 6845 ierr = MatSetOption((*submat)[i],MAT_STRUCTURALLY_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr); 6846 } 6847 } 6848 } 6849 } 6850 PetscFunctionReturn(0); 6851 } 6852 6853 /*@C 6854 MatDestroyMatrices - Destroys an array of matrices. 6855 6856 Collective on Mat 6857 6858 Input Parameters: 6859 + n - the number of local matrices 6860 - mat - the matrices (note that this is a pointer to the array of matrices) 6861 6862 Level: advanced 6863 6864 Notes: 6865 Frees not only the matrices, but also the array that contains the matrices 6866 In Fortran will not free the array. 6867 6868 .seealso: MatCreateSubMatrices() MatDestroySubMatrices() 6869 @*/ 6870 PetscErrorCode MatDestroyMatrices(PetscInt n,Mat *mat[]) 6871 { 6872 PetscErrorCode ierr; 6873 PetscInt i; 6874 6875 PetscFunctionBegin; 6876 if (!*mat) PetscFunctionReturn(0); 6877 if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Trying to destroy negative number of matrices %D",n); 6878 PetscValidPointer(mat,2); 6879 6880 for (i=0; i<n; i++) { 6881 ierr = MatDestroy(&(*mat)[i]);CHKERRQ(ierr); 6882 } 6883 6884 /* memory is allocated even if n = 0 */ 6885 ierr = PetscFree(*mat);CHKERRQ(ierr); 6886 PetscFunctionReturn(0); 6887 } 6888 6889 /*@C 6890 MatDestroySubMatrices - Destroys a set of matrices obtained with MatCreateSubMatrices(). 6891 6892 Collective on Mat 6893 6894 Input Parameters: 6895 + n - the number of local matrices 6896 - mat - the matrices (note that this is a pointer to the array of matrices, just to match the calling 6897 sequence of MatCreateSubMatrices()) 6898 6899 Level: advanced 6900 6901 Notes: 6902 Frees not only the matrices, but also the array that contains the matrices 6903 In Fortran will not free the array. 6904 6905 .seealso: MatCreateSubMatrices() 6906 @*/ 6907 PetscErrorCode MatDestroySubMatrices(PetscInt n,Mat *mat[]) 6908 { 6909 PetscErrorCode ierr; 6910 Mat mat0; 6911 6912 PetscFunctionBegin; 6913 if (!*mat) PetscFunctionReturn(0); 6914 /* mat[] is an array of length n+1, see MatCreateSubMatrices_xxx() */ 6915 if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Trying to destroy negative number of matrices %D",n); 6916 PetscValidPointer(mat,2); 6917 6918 mat0 = (*mat)[0]; 6919 if (mat0 && mat0->ops->destroysubmatrices) { 6920 ierr = (mat0->ops->destroysubmatrices)(n,mat);CHKERRQ(ierr); 6921 } else { 6922 ierr = MatDestroyMatrices(n,mat);CHKERRQ(ierr); 6923 } 6924 PetscFunctionReturn(0); 6925 } 6926 6927 /*@C 6928 MatGetSeqNonzeroStructure - Extracts the sequential nonzero structure from a matrix. 6929 6930 Collective on Mat 6931 6932 Input Parameters: 6933 . mat - the matrix 6934 6935 Output Parameter: 6936 . matstruct - the sequential matrix with the nonzero structure of mat 6937 6938 Level: intermediate 6939 6940 .seealso: MatDestroySeqNonzeroStructure(), MatCreateSubMatrices(), MatDestroyMatrices() 6941 @*/ 6942 PetscErrorCode MatGetSeqNonzeroStructure(Mat mat,Mat *matstruct) 6943 { 6944 PetscErrorCode ierr; 6945 6946 PetscFunctionBegin; 6947 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6948 PetscValidPointer(matstruct,2); 6949 6950 PetscValidType(mat,1); 6951 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6952 MatCheckPreallocated(mat,1); 6953 6954 if (!mat->ops->getseqnonzerostructure) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Not for matrix type %s\n",((PetscObject)mat)->type_name); 6955 ierr = PetscLogEventBegin(MAT_GetSeqNonzeroStructure,mat,0,0,0);CHKERRQ(ierr); 6956 ierr = (*mat->ops->getseqnonzerostructure)(mat,matstruct);CHKERRQ(ierr); 6957 ierr = PetscLogEventEnd(MAT_GetSeqNonzeroStructure,mat,0,0,0);CHKERRQ(ierr); 6958 PetscFunctionReturn(0); 6959 } 6960 6961 /*@C 6962 MatDestroySeqNonzeroStructure - Destroys matrix obtained with MatGetSeqNonzeroStructure(). 6963 6964 Collective on Mat 6965 6966 Input Parameters: 6967 . mat - the matrix (note that this is a pointer to the array of matrices, just to match the calling 6968 sequence of MatGetSequentialNonzeroStructure()) 6969 6970 Level: advanced 6971 6972 Notes: 6973 Frees not only the matrices, but also the array that contains the matrices 6974 6975 .seealso: MatGetSeqNonzeroStructure() 6976 @*/ 6977 PetscErrorCode MatDestroySeqNonzeroStructure(Mat *mat) 6978 { 6979 PetscErrorCode ierr; 6980 6981 PetscFunctionBegin; 6982 PetscValidPointer(mat,1); 6983 ierr = MatDestroy(mat);CHKERRQ(ierr); 6984 PetscFunctionReturn(0); 6985 } 6986 6987 /*@ 6988 MatIncreaseOverlap - Given a set of submatrices indicated by index sets, 6989 replaces the index sets by larger ones that represent submatrices with 6990 additional overlap. 6991 6992 Collective on Mat 6993 6994 Input Parameters: 6995 + mat - the matrix 6996 . n - the number of index sets 6997 . is - the array of index sets (these index sets will changed during the call) 6998 - ov - the additional overlap requested 6999 7000 Options Database: 7001 . -mat_increase_overlap_scalable - use a scalable algorithm to compute the overlap (supported by MPIAIJ matrix) 7002 7003 Level: developer 7004 7005 7006 .seealso: MatCreateSubMatrices() 7007 @*/ 7008 PetscErrorCode MatIncreaseOverlap(Mat mat,PetscInt n,IS is[],PetscInt ov) 7009 { 7010 PetscErrorCode ierr; 7011 7012 PetscFunctionBegin; 7013 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7014 PetscValidType(mat,1); 7015 if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Must have one or more domains, you have %D",n); 7016 if (n) { 7017 PetscValidPointer(is,3); 7018 PetscValidHeaderSpecific(*is,IS_CLASSID,3); 7019 } 7020 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 7021 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7022 MatCheckPreallocated(mat,1); 7023 7024 if (!ov) PetscFunctionReturn(0); 7025 if (!mat->ops->increaseoverlap) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 7026 ierr = PetscLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr); 7027 ierr = (*mat->ops->increaseoverlap)(mat,n,is,ov);CHKERRQ(ierr); 7028 ierr = PetscLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr); 7029 PetscFunctionReturn(0); 7030 } 7031 7032 7033 PetscErrorCode MatIncreaseOverlapSplit_Single(Mat,IS*,PetscInt); 7034 7035 /*@ 7036 MatIncreaseOverlapSplit - Given a set of submatrices indicated by index sets across 7037 a sub communicator, replaces the index sets by larger ones that represent submatrices with 7038 additional overlap. 7039 7040 Collective on Mat 7041 7042 Input Parameters: 7043 + mat - the matrix 7044 . n - the number of index sets 7045 . is - the array of index sets (these index sets will changed during the call) 7046 - ov - the additional overlap requested 7047 7048 Options Database: 7049 . -mat_increase_overlap_scalable - use a scalable algorithm to compute the overlap (supported by MPIAIJ matrix) 7050 7051 Level: developer 7052 7053 7054 .seealso: MatCreateSubMatrices() 7055 @*/ 7056 PetscErrorCode MatIncreaseOverlapSplit(Mat mat,PetscInt n,IS is[],PetscInt ov) 7057 { 7058 PetscInt i; 7059 PetscErrorCode ierr; 7060 7061 PetscFunctionBegin; 7062 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7063 PetscValidType(mat,1); 7064 if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Must have one or more domains, you have %D",n); 7065 if (n) { 7066 PetscValidPointer(is,3); 7067 PetscValidHeaderSpecific(*is,IS_CLASSID,3); 7068 } 7069 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 7070 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7071 MatCheckPreallocated(mat,1); 7072 if (!ov) PetscFunctionReturn(0); 7073 ierr = PetscLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr); 7074 for(i=0; i<n; i++){ 7075 ierr = MatIncreaseOverlapSplit_Single(mat,&is[i],ov);CHKERRQ(ierr); 7076 } 7077 ierr = PetscLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr); 7078 PetscFunctionReturn(0); 7079 } 7080 7081 7082 7083 7084 /*@ 7085 MatGetBlockSize - Returns the matrix block size. 7086 7087 Not Collective 7088 7089 Input Parameter: 7090 . mat - the matrix 7091 7092 Output Parameter: 7093 . bs - block size 7094 7095 Notes: 7096 Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ. These formats ALWAYS have square block storage in the matrix. 7097 7098 If the block size has not been set yet this routine returns 1. 7099 7100 Level: intermediate 7101 7102 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSizes() 7103 @*/ 7104 PetscErrorCode MatGetBlockSize(Mat mat,PetscInt *bs) 7105 { 7106 PetscFunctionBegin; 7107 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7108 PetscValidIntPointer(bs,2); 7109 *bs = PetscAbs(mat->rmap->bs); 7110 PetscFunctionReturn(0); 7111 } 7112 7113 /*@ 7114 MatGetBlockSizes - Returns the matrix block row and column sizes. 7115 7116 Not Collective 7117 7118 Input Parameter: 7119 . mat - the matrix 7120 7121 Output Parameter: 7122 + rbs - row block size 7123 - cbs - column block size 7124 7125 Notes: 7126 Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ. These formats ALWAYS have square block storage in the matrix. 7127 If you pass a different block size for the columns than the rows, the row block size determines the square block storage. 7128 7129 If a block size has not been set yet this routine returns 1. 7130 7131 Level: intermediate 7132 7133 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSize(), MatSetBlockSizes() 7134 @*/ 7135 PetscErrorCode MatGetBlockSizes(Mat mat,PetscInt *rbs, PetscInt *cbs) 7136 { 7137 PetscFunctionBegin; 7138 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7139 if (rbs) PetscValidIntPointer(rbs,2); 7140 if (cbs) PetscValidIntPointer(cbs,3); 7141 if (rbs) *rbs = PetscAbs(mat->rmap->bs); 7142 if (cbs) *cbs = PetscAbs(mat->cmap->bs); 7143 PetscFunctionReturn(0); 7144 } 7145 7146 /*@ 7147 MatSetBlockSize - Sets the matrix block size. 7148 7149 Logically Collective on Mat 7150 7151 Input Parameters: 7152 + mat - the matrix 7153 - bs - block size 7154 7155 Notes: 7156 Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ. These formats ALWAYS have square block storage in the matrix. 7157 This must be called before MatSetUp() or MatXXXSetPreallocation() (or will default to 1) and the block size cannot be changed later. 7158 7159 For MATMPIAIJ and MATSEQAIJ matrix formats, this function can be called at a later stage, provided that the specified block size 7160 is compatible with the matrix local sizes. 7161 7162 Level: intermediate 7163 7164 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSizes(), MatGetBlockSizes() 7165 @*/ 7166 PetscErrorCode MatSetBlockSize(Mat mat,PetscInt bs) 7167 { 7168 PetscErrorCode ierr; 7169 7170 PetscFunctionBegin; 7171 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7172 PetscValidLogicalCollectiveInt(mat,bs,2); 7173 ierr = MatSetBlockSizes(mat,bs,bs);CHKERRQ(ierr); 7174 PetscFunctionReturn(0); 7175 } 7176 7177 /*@ 7178 MatSetVariableBlockSizes - Sets a diagonal blocks of the matrix that need not be of the same size 7179 7180 Logically Collective on Mat 7181 7182 Input Parameters: 7183 + mat - the matrix 7184 . nblocks - the number of blocks on this process 7185 - bsizes - the block sizes 7186 7187 Notes: 7188 Currently used by PCVPBJACOBI for SeqAIJ matrices 7189 7190 Level: intermediate 7191 7192 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSizes(), MatGetBlockSizes(), MatGetVariableBlockSizes() 7193 @*/ 7194 PetscErrorCode MatSetVariableBlockSizes(Mat mat,PetscInt nblocks,PetscInt *bsizes) 7195 { 7196 PetscErrorCode ierr; 7197 PetscInt i,ncnt = 0, nlocal; 7198 7199 PetscFunctionBegin; 7200 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7201 if (nblocks < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Number of local blocks must be great than or equal to zero"); 7202 ierr = MatGetLocalSize(mat,&nlocal,NULL);CHKERRQ(ierr); 7203 for (i=0; i<nblocks; i++) ncnt += bsizes[i]; 7204 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); 7205 ierr = PetscFree(mat->bsizes);CHKERRQ(ierr); 7206 mat->nblocks = nblocks; 7207 ierr = PetscMalloc1(nblocks,&mat->bsizes);CHKERRQ(ierr); 7208 ierr = PetscArraycpy(mat->bsizes,bsizes,nblocks);CHKERRQ(ierr); 7209 PetscFunctionReturn(0); 7210 } 7211 7212 /*@C 7213 MatGetVariableBlockSizes - Gets a diagonal blocks of the matrix that need not be of the same size 7214 7215 Logically Collective on Mat 7216 7217 Input Parameters: 7218 . mat - the matrix 7219 7220 Output Parameters: 7221 + nblocks - the number of blocks on this process 7222 - bsizes - the block sizes 7223 7224 Notes: Currently not supported from Fortran 7225 7226 Level: intermediate 7227 7228 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSizes(), MatGetBlockSizes(), MatSetVariableBlockSizes() 7229 @*/ 7230 PetscErrorCode MatGetVariableBlockSizes(Mat mat,PetscInt *nblocks,const PetscInt **bsizes) 7231 { 7232 PetscFunctionBegin; 7233 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7234 *nblocks = mat->nblocks; 7235 *bsizes = mat->bsizes; 7236 PetscFunctionReturn(0); 7237 } 7238 7239 /*@ 7240 MatSetBlockSizes - Sets the matrix block row and column sizes. 7241 7242 Logically Collective on Mat 7243 7244 Input Parameters: 7245 + mat - the matrix 7246 - rbs - row block size 7247 - cbs - column block size 7248 7249 Notes: 7250 Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ. These formats ALWAYS have square block storage in the matrix. 7251 If you pass a different block size for the columns than the rows, the row block size determines the square block storage. 7252 This must be called before MatSetUp() or MatXXXSetPreallocation() (or will default to 1) and the block size cannot be changed later 7253 7254 For MATMPIAIJ and MATSEQAIJ matrix formats, this function can be called at a later stage, provided that the specified block sizes 7255 are compatible with the matrix local sizes. 7256 7257 The row and column block size determine the blocksize of the "row" and "column" vectors returned by MatCreateVecs(). 7258 7259 Level: intermediate 7260 7261 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSize(), MatGetBlockSizes() 7262 @*/ 7263 PetscErrorCode MatSetBlockSizes(Mat mat,PetscInt rbs,PetscInt cbs) 7264 { 7265 PetscErrorCode ierr; 7266 7267 PetscFunctionBegin; 7268 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7269 PetscValidLogicalCollectiveInt(mat,rbs,2); 7270 PetscValidLogicalCollectiveInt(mat,cbs,3); 7271 if (mat->ops->setblocksizes) { 7272 ierr = (*mat->ops->setblocksizes)(mat,rbs,cbs);CHKERRQ(ierr); 7273 } 7274 if (mat->rmap->refcnt) { 7275 ISLocalToGlobalMapping l2g = NULL; 7276 PetscLayout nmap = NULL; 7277 7278 ierr = PetscLayoutDuplicate(mat->rmap,&nmap);CHKERRQ(ierr); 7279 if (mat->rmap->mapping) { 7280 ierr = ISLocalToGlobalMappingDuplicate(mat->rmap->mapping,&l2g);CHKERRQ(ierr); 7281 } 7282 ierr = PetscLayoutDestroy(&mat->rmap);CHKERRQ(ierr); 7283 mat->rmap = nmap; 7284 mat->rmap->mapping = l2g; 7285 } 7286 if (mat->cmap->refcnt) { 7287 ISLocalToGlobalMapping l2g = NULL; 7288 PetscLayout nmap = NULL; 7289 7290 ierr = PetscLayoutDuplicate(mat->cmap,&nmap);CHKERRQ(ierr); 7291 if (mat->cmap->mapping) { 7292 ierr = ISLocalToGlobalMappingDuplicate(mat->cmap->mapping,&l2g);CHKERRQ(ierr); 7293 } 7294 ierr = PetscLayoutDestroy(&mat->cmap);CHKERRQ(ierr); 7295 mat->cmap = nmap; 7296 mat->cmap->mapping = l2g; 7297 } 7298 ierr = PetscLayoutSetBlockSize(mat->rmap,rbs);CHKERRQ(ierr); 7299 ierr = PetscLayoutSetBlockSize(mat->cmap,cbs);CHKERRQ(ierr); 7300 PetscFunctionReturn(0); 7301 } 7302 7303 /*@ 7304 MatSetBlockSizesFromMats - Sets the matrix block row and column sizes to match a pair of matrices 7305 7306 Logically Collective on Mat 7307 7308 Input Parameters: 7309 + mat - the matrix 7310 . fromRow - matrix from which to copy row block size 7311 - fromCol - matrix from which to copy column block size (can be same as fromRow) 7312 7313 Level: developer 7314 7315 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSizes() 7316 @*/ 7317 PetscErrorCode MatSetBlockSizesFromMats(Mat mat,Mat fromRow,Mat fromCol) 7318 { 7319 PetscErrorCode ierr; 7320 7321 PetscFunctionBegin; 7322 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7323 PetscValidHeaderSpecific(fromRow,MAT_CLASSID,2); 7324 PetscValidHeaderSpecific(fromCol,MAT_CLASSID,3); 7325 if (fromRow->rmap->bs > 0) {ierr = PetscLayoutSetBlockSize(mat->rmap,fromRow->rmap->bs);CHKERRQ(ierr);} 7326 if (fromCol->cmap->bs > 0) {ierr = PetscLayoutSetBlockSize(mat->cmap,fromCol->cmap->bs);CHKERRQ(ierr);} 7327 PetscFunctionReturn(0); 7328 } 7329 7330 /*@ 7331 MatResidual - Default routine to calculate the residual. 7332 7333 Collective on Mat 7334 7335 Input Parameters: 7336 + mat - the matrix 7337 . b - the right-hand-side 7338 - x - the approximate solution 7339 7340 Output Parameter: 7341 . r - location to store the residual 7342 7343 Level: developer 7344 7345 .seealso: PCMGSetResidual() 7346 @*/ 7347 PetscErrorCode MatResidual(Mat mat,Vec b,Vec x,Vec r) 7348 { 7349 PetscErrorCode ierr; 7350 7351 PetscFunctionBegin; 7352 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7353 PetscValidHeaderSpecific(b,VEC_CLASSID,2); 7354 PetscValidHeaderSpecific(x,VEC_CLASSID,3); 7355 PetscValidHeaderSpecific(r,VEC_CLASSID,4); 7356 PetscValidType(mat,1); 7357 MatCheckPreallocated(mat,1); 7358 ierr = PetscLogEventBegin(MAT_Residual,mat,0,0,0);CHKERRQ(ierr); 7359 if (!mat->ops->residual) { 7360 ierr = MatMult(mat,x,r);CHKERRQ(ierr); 7361 ierr = VecAYPX(r,-1.0,b);CHKERRQ(ierr); 7362 } else { 7363 ierr = (*mat->ops->residual)(mat,b,x,r);CHKERRQ(ierr); 7364 } 7365 ierr = PetscLogEventEnd(MAT_Residual,mat,0,0,0);CHKERRQ(ierr); 7366 PetscFunctionReturn(0); 7367 } 7368 7369 /*@C 7370 MatGetRowIJ - Returns the compressed row storage i and j indices for sequential matrices. 7371 7372 Collective on Mat 7373 7374 Input Parameters: 7375 + mat - the matrix 7376 . shift - 0 or 1 indicating we want the indices starting at 0 or 1 7377 . symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be symmetrized 7378 - inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the 7379 inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is 7380 always used. 7381 7382 Output Parameters: 7383 + n - number of rows in the (possibly compressed) matrix 7384 . ia - the row pointers; that is ia[0] = 0, ia[row] = ia[row-1] + number of elements in that row of the matrix 7385 . ja - the column indices 7386 - done - indicates if the routine actually worked and returned appropriate ia[] and ja[] arrays; callers 7387 are responsible for handling the case when done == PETSC_FALSE and ia and ja are not set 7388 7389 Level: developer 7390 7391 Notes: 7392 You CANNOT change any of the ia[] or ja[] values. 7393 7394 Use MatRestoreRowIJ() when you are finished accessing the ia[] and ja[] values. 7395 7396 Fortran Notes: 7397 In Fortran use 7398 $ 7399 $ PetscInt ia(1), ja(1) 7400 $ PetscOffset iia, jja 7401 $ call MatGetRowIJ(mat,shift,symmetric,inodecompressed,n,ia,iia,ja,jja,done,ierr) 7402 $ ! Access the ith and jth entries via ia(iia + i) and ja(jja + j) 7403 7404 or 7405 $ 7406 $ PetscInt, pointer :: ia(:),ja(:) 7407 $ call MatGetRowIJF90(mat,shift,symmetric,inodecompressed,n,ia,ja,done,ierr) 7408 $ ! Access the ith and jth entries via ia(i) and ja(j) 7409 7410 .seealso: MatGetColumnIJ(), MatRestoreRowIJ(), MatSeqAIJGetArray() 7411 @*/ 7412 PetscErrorCode MatGetRowIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool *done) 7413 { 7414 PetscErrorCode ierr; 7415 7416 PetscFunctionBegin; 7417 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7418 PetscValidType(mat,1); 7419 PetscValidIntPointer(n,5); 7420 if (ia) PetscValidIntPointer(ia,6); 7421 if (ja) PetscValidIntPointer(ja,7); 7422 PetscValidIntPointer(done,8); 7423 MatCheckPreallocated(mat,1); 7424 if (!mat->ops->getrowij) *done = PETSC_FALSE; 7425 else { 7426 *done = PETSC_TRUE; 7427 ierr = PetscLogEventBegin(MAT_GetRowIJ,mat,0,0,0);CHKERRQ(ierr); 7428 ierr = (*mat->ops->getrowij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr); 7429 ierr = PetscLogEventEnd(MAT_GetRowIJ,mat,0,0,0);CHKERRQ(ierr); 7430 } 7431 PetscFunctionReturn(0); 7432 } 7433 7434 /*@C 7435 MatGetColumnIJ - Returns the compressed column storage i and j indices for sequential matrices. 7436 7437 Collective on Mat 7438 7439 Input Parameters: 7440 + mat - the matrix 7441 . shift - 1 or zero indicating we want the indices starting at 0 or 1 7442 . symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 7443 symmetrized 7444 . inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the 7445 inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is 7446 always used. 7447 . n - number of columns in the (possibly compressed) matrix 7448 . ia - the column pointers; that is ia[0] = 0, ia[col] = i[col-1] + number of elements in that col of the matrix 7449 - ja - the row indices 7450 7451 Output Parameters: 7452 . done - PETSC_TRUE or PETSC_FALSE, indicating whether the values have been returned 7453 7454 Level: developer 7455 7456 .seealso: MatGetRowIJ(), MatRestoreColumnIJ() 7457 @*/ 7458 PetscErrorCode MatGetColumnIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool *done) 7459 { 7460 PetscErrorCode ierr; 7461 7462 PetscFunctionBegin; 7463 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7464 PetscValidType(mat,1); 7465 PetscValidIntPointer(n,4); 7466 if (ia) PetscValidIntPointer(ia,5); 7467 if (ja) PetscValidIntPointer(ja,6); 7468 PetscValidIntPointer(done,7); 7469 MatCheckPreallocated(mat,1); 7470 if (!mat->ops->getcolumnij) *done = PETSC_FALSE; 7471 else { 7472 *done = PETSC_TRUE; 7473 ierr = (*mat->ops->getcolumnij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr); 7474 } 7475 PetscFunctionReturn(0); 7476 } 7477 7478 /*@C 7479 MatRestoreRowIJ - Call after you are completed with the ia,ja indices obtained with 7480 MatGetRowIJ(). 7481 7482 Collective on Mat 7483 7484 Input Parameters: 7485 + mat - the matrix 7486 . shift - 1 or zero indicating we want the indices starting at 0 or 1 7487 . symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 7488 symmetrized 7489 . inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the 7490 inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is 7491 always used. 7492 . n - size of (possibly compressed) matrix 7493 . ia - the row pointers 7494 - ja - the column indices 7495 7496 Output Parameters: 7497 . done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned 7498 7499 Note: 7500 This routine zeros out n, ia, and ja. This is to prevent accidental 7501 us of the array after it has been restored. If you pass NULL, it will 7502 not zero the pointers. Use of ia or ja after MatRestoreRowIJ() is invalid. 7503 7504 Level: developer 7505 7506 .seealso: MatGetRowIJ(), MatRestoreColumnIJ() 7507 @*/ 7508 PetscErrorCode MatRestoreRowIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool *done) 7509 { 7510 PetscErrorCode ierr; 7511 7512 PetscFunctionBegin; 7513 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7514 PetscValidType(mat,1); 7515 if (ia) PetscValidIntPointer(ia,6); 7516 if (ja) PetscValidIntPointer(ja,7); 7517 PetscValidIntPointer(done,8); 7518 MatCheckPreallocated(mat,1); 7519 7520 if (!mat->ops->restorerowij) *done = PETSC_FALSE; 7521 else { 7522 *done = PETSC_TRUE; 7523 ierr = (*mat->ops->restorerowij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr); 7524 if (n) *n = 0; 7525 if (ia) *ia = NULL; 7526 if (ja) *ja = NULL; 7527 } 7528 PetscFunctionReturn(0); 7529 } 7530 7531 /*@C 7532 MatRestoreColumnIJ - Call after you are completed with the ia,ja indices obtained with 7533 MatGetColumnIJ(). 7534 7535 Collective on Mat 7536 7537 Input Parameters: 7538 + mat - the matrix 7539 . shift - 1 or zero indicating we want the indices starting at 0 or 1 7540 - symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 7541 symmetrized 7542 - inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the 7543 inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is 7544 always used. 7545 7546 Output Parameters: 7547 + n - size of (possibly compressed) matrix 7548 . ia - the column pointers 7549 . ja - the row indices 7550 - done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned 7551 7552 Level: developer 7553 7554 .seealso: MatGetColumnIJ(), MatRestoreRowIJ() 7555 @*/ 7556 PetscErrorCode MatRestoreColumnIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool *done) 7557 { 7558 PetscErrorCode ierr; 7559 7560 PetscFunctionBegin; 7561 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7562 PetscValidType(mat,1); 7563 if (ia) PetscValidIntPointer(ia,5); 7564 if (ja) PetscValidIntPointer(ja,6); 7565 PetscValidIntPointer(done,7); 7566 MatCheckPreallocated(mat,1); 7567 7568 if (!mat->ops->restorecolumnij) *done = PETSC_FALSE; 7569 else { 7570 *done = PETSC_TRUE; 7571 ierr = (*mat->ops->restorecolumnij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr); 7572 if (n) *n = 0; 7573 if (ia) *ia = NULL; 7574 if (ja) *ja = NULL; 7575 } 7576 PetscFunctionReturn(0); 7577 } 7578 7579 /*@C 7580 MatColoringPatch -Used inside matrix coloring routines that 7581 use MatGetRowIJ() and/or MatGetColumnIJ(). 7582 7583 Collective on Mat 7584 7585 Input Parameters: 7586 + mat - the matrix 7587 . ncolors - max color value 7588 . n - number of entries in colorarray 7589 - colorarray - array indicating color for each column 7590 7591 Output Parameters: 7592 . iscoloring - coloring generated using colorarray information 7593 7594 Level: developer 7595 7596 .seealso: MatGetRowIJ(), MatGetColumnIJ() 7597 7598 @*/ 7599 PetscErrorCode MatColoringPatch(Mat mat,PetscInt ncolors,PetscInt n,ISColoringValue colorarray[],ISColoring *iscoloring) 7600 { 7601 PetscErrorCode ierr; 7602 7603 PetscFunctionBegin; 7604 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7605 PetscValidType(mat,1); 7606 PetscValidIntPointer(colorarray,4); 7607 PetscValidPointer(iscoloring,5); 7608 MatCheckPreallocated(mat,1); 7609 7610 if (!mat->ops->coloringpatch) { 7611 ierr = ISColoringCreate(PetscObjectComm((PetscObject)mat),ncolors,n,colorarray,PETSC_OWN_POINTER,iscoloring);CHKERRQ(ierr); 7612 } else { 7613 ierr = (*mat->ops->coloringpatch)(mat,ncolors,n,colorarray,iscoloring);CHKERRQ(ierr); 7614 } 7615 PetscFunctionReturn(0); 7616 } 7617 7618 7619 /*@ 7620 MatSetUnfactored - Resets a factored matrix to be treated as unfactored. 7621 7622 Logically Collective on Mat 7623 7624 Input Parameter: 7625 . mat - the factored matrix to be reset 7626 7627 Notes: 7628 This routine should be used only with factored matrices formed by in-place 7629 factorization via ILU(0) (or by in-place LU factorization for the MATSEQDENSE 7630 format). This option can save memory, for example, when solving nonlinear 7631 systems with a matrix-free Newton-Krylov method and a matrix-based, in-place 7632 ILU(0) preconditioner. 7633 7634 Note that one can specify in-place ILU(0) factorization by calling 7635 .vb 7636 PCType(pc,PCILU); 7637 PCFactorSeUseInPlace(pc); 7638 .ve 7639 or by using the options -pc_type ilu -pc_factor_in_place 7640 7641 In-place factorization ILU(0) can also be used as a local 7642 solver for the blocks within the block Jacobi or additive Schwarz 7643 methods (runtime option: -sub_pc_factor_in_place). See Users-Manual: ch_pc 7644 for details on setting local solver options. 7645 7646 Most users should employ the simplified KSP interface for linear solvers 7647 instead of working directly with matrix algebra routines such as this. 7648 See, e.g., KSPCreate(). 7649 7650 Level: developer 7651 7652 .seealso: PCFactorSetUseInPlace(), PCFactorGetUseInPlace() 7653 7654 @*/ 7655 PetscErrorCode MatSetUnfactored(Mat mat) 7656 { 7657 PetscErrorCode ierr; 7658 7659 PetscFunctionBegin; 7660 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7661 PetscValidType(mat,1); 7662 MatCheckPreallocated(mat,1); 7663 mat->factortype = MAT_FACTOR_NONE; 7664 if (!mat->ops->setunfactored) PetscFunctionReturn(0); 7665 ierr = (*mat->ops->setunfactored)(mat);CHKERRQ(ierr); 7666 PetscFunctionReturn(0); 7667 } 7668 7669 /*MC 7670 MatDenseGetArrayF90 - Accesses a matrix array from Fortran90. 7671 7672 Synopsis: 7673 MatDenseGetArrayF90(Mat x,{Scalar, pointer :: xx_v(:,:)},integer ierr) 7674 7675 Not collective 7676 7677 Input Parameter: 7678 . x - matrix 7679 7680 Output Parameters: 7681 + xx_v - the Fortran90 pointer to the array 7682 - ierr - error code 7683 7684 Example of Usage: 7685 .vb 7686 PetscScalar, pointer xx_v(:,:) 7687 .... 7688 call MatDenseGetArrayF90(x,xx_v,ierr) 7689 a = xx_v(3) 7690 call MatDenseRestoreArrayF90(x,xx_v,ierr) 7691 .ve 7692 7693 Level: advanced 7694 7695 .seealso: MatDenseRestoreArrayF90(), MatDenseGetArray(), MatDenseRestoreArray(), MatSeqAIJGetArrayF90() 7696 7697 M*/ 7698 7699 /*MC 7700 MatDenseRestoreArrayF90 - Restores a matrix array that has been 7701 accessed with MatDenseGetArrayF90(). 7702 7703 Synopsis: 7704 MatDenseRestoreArrayF90(Mat x,{Scalar, pointer :: xx_v(:,:)},integer ierr) 7705 7706 Not collective 7707 7708 Input Parameters: 7709 + x - matrix 7710 - xx_v - the Fortran90 pointer to the array 7711 7712 Output Parameter: 7713 . ierr - error code 7714 7715 Example of Usage: 7716 .vb 7717 PetscScalar, pointer xx_v(:,:) 7718 .... 7719 call MatDenseGetArrayF90(x,xx_v,ierr) 7720 a = xx_v(3) 7721 call MatDenseRestoreArrayF90(x,xx_v,ierr) 7722 .ve 7723 7724 Level: advanced 7725 7726 .seealso: MatDenseGetArrayF90(), MatDenseGetArray(), MatDenseRestoreArray(), MatSeqAIJRestoreArrayF90() 7727 7728 M*/ 7729 7730 7731 /*MC 7732 MatSeqAIJGetArrayF90 - Accesses a matrix array from Fortran90. 7733 7734 Synopsis: 7735 MatSeqAIJGetArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr) 7736 7737 Not collective 7738 7739 Input Parameter: 7740 . x - matrix 7741 7742 Output Parameters: 7743 + xx_v - the Fortran90 pointer to the array 7744 - ierr - error code 7745 7746 Example of Usage: 7747 .vb 7748 PetscScalar, pointer xx_v(:) 7749 .... 7750 call MatSeqAIJGetArrayF90(x,xx_v,ierr) 7751 a = xx_v(3) 7752 call MatSeqAIJRestoreArrayF90(x,xx_v,ierr) 7753 .ve 7754 7755 Level: advanced 7756 7757 .seealso: MatSeqAIJRestoreArrayF90(), MatSeqAIJGetArray(), MatSeqAIJRestoreArray(), MatDenseGetArrayF90() 7758 7759 M*/ 7760 7761 /*MC 7762 MatSeqAIJRestoreArrayF90 - Restores a matrix array that has been 7763 accessed with MatSeqAIJGetArrayF90(). 7764 7765 Synopsis: 7766 MatSeqAIJRestoreArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr) 7767 7768 Not collective 7769 7770 Input Parameters: 7771 + x - matrix 7772 - xx_v - the Fortran90 pointer to the array 7773 7774 Output Parameter: 7775 . ierr - error code 7776 7777 Example of Usage: 7778 .vb 7779 PetscScalar, pointer xx_v(:) 7780 .... 7781 call MatSeqAIJGetArrayF90(x,xx_v,ierr) 7782 a = xx_v(3) 7783 call MatSeqAIJRestoreArrayF90(x,xx_v,ierr) 7784 .ve 7785 7786 Level: advanced 7787 7788 .seealso: MatSeqAIJGetArrayF90(), MatSeqAIJGetArray(), MatSeqAIJRestoreArray(), MatDenseRestoreArrayF90() 7789 7790 M*/ 7791 7792 7793 /*@ 7794 MatCreateSubMatrix - Gets a single submatrix on the same number of processors 7795 as the original matrix. 7796 7797 Collective on Mat 7798 7799 Input Parameters: 7800 + mat - the original matrix 7801 . isrow - parallel IS containing the rows this processor should obtain 7802 . 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. 7803 - cll - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 7804 7805 Output Parameter: 7806 . newmat - the new submatrix, of the same type as the old 7807 7808 Level: advanced 7809 7810 Notes: 7811 The submatrix will be able to be multiplied with vectors using the same layout as iscol. 7812 7813 Some matrix types place restrictions on the row and column indices, such 7814 as that they be sorted or that they be equal to each other. 7815 7816 The index sets may not have duplicate entries. 7817 7818 The first time this is called you should use a cll of MAT_INITIAL_MATRIX, 7819 the MatCreateSubMatrix() routine will create the newmat for you. Any additional calls 7820 to this routine with a mat of the same nonzero structure and with a call of MAT_REUSE_MATRIX 7821 will reuse the matrix generated the first time. You should call MatDestroy() on newmat when 7822 you are finished using it. 7823 7824 The communicator of the newly obtained matrix is ALWAYS the same as the communicator of 7825 the input matrix. 7826 7827 If iscol is NULL then all columns are obtained (not supported in Fortran). 7828 7829 Example usage: 7830 Consider the following 8x8 matrix with 34 non-zero values, that is 7831 assembled across 3 processors. Let's assume that proc0 owns 3 rows, 7832 proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown 7833 as follows: 7834 7835 .vb 7836 1 2 0 | 0 3 0 | 0 4 7837 Proc0 0 5 6 | 7 0 0 | 8 0 7838 9 0 10 | 11 0 0 | 12 0 7839 ------------------------------------- 7840 13 0 14 | 15 16 17 | 0 0 7841 Proc1 0 18 0 | 19 20 21 | 0 0 7842 0 0 0 | 22 23 0 | 24 0 7843 ------------------------------------- 7844 Proc2 25 26 27 | 0 0 28 | 29 0 7845 30 0 0 | 31 32 33 | 0 34 7846 .ve 7847 7848 Suppose isrow = [0 1 | 4 | 6 7] and iscol = [1 2 | 3 4 5 | 6]. The resulting submatrix is 7849 7850 .vb 7851 2 0 | 0 3 0 | 0 7852 Proc0 5 6 | 7 0 0 | 8 7853 ------------------------------- 7854 Proc1 18 0 | 19 20 21 | 0 7855 ------------------------------- 7856 Proc2 26 27 | 0 0 28 | 29 7857 0 0 | 31 32 33 | 0 7858 .ve 7859 7860 7861 .seealso: MatCreateSubMatrices() 7862 @*/ 7863 PetscErrorCode MatCreateSubMatrix(Mat mat,IS isrow,IS iscol,MatReuse cll,Mat *newmat) 7864 { 7865 PetscErrorCode ierr; 7866 PetscMPIInt size; 7867 Mat *local; 7868 IS iscoltmp; 7869 7870 PetscFunctionBegin; 7871 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7872 PetscValidHeaderSpecific(isrow,IS_CLASSID,2); 7873 if (iscol) PetscValidHeaderSpecific(iscol,IS_CLASSID,3); 7874 PetscValidPointer(newmat,5); 7875 if (cll == MAT_REUSE_MATRIX) PetscValidHeaderSpecific(*newmat,MAT_CLASSID,5); 7876 PetscValidType(mat,1); 7877 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7878 if (cll == MAT_IGNORE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Cannot use MAT_IGNORE_MATRIX"); 7879 7880 MatCheckPreallocated(mat,1); 7881 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);CHKERRQ(ierr); 7882 7883 if (!iscol || isrow == iscol) { 7884 PetscBool stride; 7885 PetscMPIInt grabentirematrix = 0,grab; 7886 ierr = PetscObjectTypeCompare((PetscObject)isrow,ISSTRIDE,&stride);CHKERRQ(ierr); 7887 if (stride) { 7888 PetscInt first,step,n,rstart,rend; 7889 ierr = ISStrideGetInfo(isrow,&first,&step);CHKERRQ(ierr); 7890 if (step == 1) { 7891 ierr = MatGetOwnershipRange(mat,&rstart,&rend);CHKERRQ(ierr); 7892 if (rstart == first) { 7893 ierr = ISGetLocalSize(isrow,&n);CHKERRQ(ierr); 7894 if (n == rend-rstart) { 7895 grabentirematrix = 1; 7896 } 7897 } 7898 } 7899 } 7900 ierr = MPIU_Allreduce(&grabentirematrix,&grab,1,MPI_INT,MPI_MIN,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 7901 if (grab) { 7902 ierr = PetscInfo(mat,"Getting entire matrix as submatrix\n");CHKERRQ(ierr); 7903 if (cll == MAT_INITIAL_MATRIX) { 7904 *newmat = mat; 7905 ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr); 7906 } 7907 PetscFunctionReturn(0); 7908 } 7909 } 7910 7911 if (!iscol) { 7912 ierr = ISCreateStride(PetscObjectComm((PetscObject)mat),mat->cmap->n,mat->cmap->rstart,1,&iscoltmp);CHKERRQ(ierr); 7913 } else { 7914 iscoltmp = iscol; 7915 } 7916 7917 /* if original matrix is on just one processor then use submatrix generated */ 7918 if (mat->ops->createsubmatrices && !mat->ops->createsubmatrix && size == 1 && cll == MAT_REUSE_MATRIX) { 7919 ierr = MatCreateSubMatrices(mat,1,&isrow,&iscoltmp,MAT_REUSE_MATRIX,&newmat);CHKERRQ(ierr); 7920 goto setproperties; 7921 } else if (mat->ops->createsubmatrices && !mat->ops->createsubmatrix && size == 1) { 7922 ierr = MatCreateSubMatrices(mat,1,&isrow,&iscoltmp,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr); 7923 *newmat = *local; 7924 ierr = PetscFree(local);CHKERRQ(ierr); 7925 goto setproperties; 7926 } else if (!mat->ops->createsubmatrix) { 7927 /* Create a new matrix type that implements the operation using the full matrix */ 7928 ierr = PetscLogEventBegin(MAT_CreateSubMat,mat,0,0,0);CHKERRQ(ierr); 7929 switch (cll) { 7930 case MAT_INITIAL_MATRIX: 7931 ierr = MatCreateSubMatrixVirtual(mat,isrow,iscoltmp,newmat);CHKERRQ(ierr); 7932 break; 7933 case MAT_REUSE_MATRIX: 7934 ierr = MatSubMatrixVirtualUpdate(*newmat,mat,isrow,iscoltmp);CHKERRQ(ierr); 7935 break; 7936 default: SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Invalid MatReuse, must be either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX"); 7937 } 7938 ierr = PetscLogEventEnd(MAT_CreateSubMat,mat,0,0,0);CHKERRQ(ierr); 7939 goto setproperties; 7940 } 7941 7942 if (!mat->ops->createsubmatrix) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 7943 ierr = PetscLogEventBegin(MAT_CreateSubMat,mat,0,0,0);CHKERRQ(ierr); 7944 ierr = (*mat->ops->createsubmatrix)(mat,isrow,iscoltmp,cll,newmat);CHKERRQ(ierr); 7945 ierr = PetscLogEventEnd(MAT_CreateSubMat,mat,0,0,0);CHKERRQ(ierr); 7946 7947 /* Propagate symmetry information for diagonal blocks */ 7948 setproperties: 7949 if (isrow == iscoltmp) { 7950 if (mat->symmetric_set && mat->symmetric) { 7951 ierr = MatSetOption(*newmat,MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr); 7952 } 7953 if (mat->structurally_symmetric_set && mat->structurally_symmetric) { 7954 ierr = MatSetOption(*newmat,MAT_STRUCTURALLY_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr); 7955 } 7956 if (mat->hermitian_set && mat->hermitian) { 7957 ierr = MatSetOption(*newmat,MAT_HERMITIAN,PETSC_TRUE);CHKERRQ(ierr); 7958 } 7959 if (mat->spd_set && mat->spd) { 7960 ierr = MatSetOption(*newmat,MAT_SPD,PETSC_TRUE);CHKERRQ(ierr); 7961 } 7962 } 7963 7964 if (!iscol) {ierr = ISDestroy(&iscoltmp);CHKERRQ(ierr);} 7965 if (*newmat && cll == MAT_INITIAL_MATRIX) {ierr = PetscObjectStateIncrease((PetscObject)*newmat);CHKERRQ(ierr);} 7966 PetscFunctionReturn(0); 7967 } 7968 7969 /*@ 7970 MatStashSetInitialSize - sets the sizes of the matrix stash, that is 7971 used during the assembly process to store values that belong to 7972 other processors. 7973 7974 Not Collective 7975 7976 Input Parameters: 7977 + mat - the matrix 7978 . size - the initial size of the stash. 7979 - bsize - the initial size of the block-stash(if used). 7980 7981 Options Database Keys: 7982 + -matstash_initial_size <size> or <size0,size1,...sizep-1> 7983 - -matstash_block_initial_size <bsize> or <bsize0,bsize1,...bsizep-1> 7984 7985 Level: intermediate 7986 7987 Notes: 7988 The block-stash is used for values set with MatSetValuesBlocked() while 7989 the stash is used for values set with MatSetValues() 7990 7991 Run with the option -info and look for output of the form 7992 MatAssemblyBegin_MPIXXX:Stash has MM entries, uses nn mallocs. 7993 to determine the appropriate value, MM, to use for size and 7994 MatAssemblyBegin_MPIXXX:Block-Stash has BMM entries, uses nn mallocs. 7995 to determine the value, BMM to use for bsize 7996 7997 7998 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), Mat, MatStashGetInfo() 7999 8000 @*/ 8001 PetscErrorCode MatStashSetInitialSize(Mat mat,PetscInt size, PetscInt bsize) 8002 { 8003 PetscErrorCode ierr; 8004 8005 PetscFunctionBegin; 8006 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8007 PetscValidType(mat,1); 8008 ierr = MatStashSetInitialSize_Private(&mat->stash,size);CHKERRQ(ierr); 8009 ierr = MatStashSetInitialSize_Private(&mat->bstash,bsize);CHKERRQ(ierr); 8010 PetscFunctionReturn(0); 8011 } 8012 8013 /*@ 8014 MatInterpolateAdd - w = y + A*x or A'*x depending on the shape of 8015 the matrix 8016 8017 Neighbor-wise Collective on Mat 8018 8019 Input Parameters: 8020 + mat - the matrix 8021 . x,y - the vectors 8022 - w - where the result is stored 8023 8024 Level: intermediate 8025 8026 Notes: 8027 w may be the same vector as y. 8028 8029 This allows one to use either the restriction or interpolation (its transpose) 8030 matrix to do the interpolation 8031 8032 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict() 8033 8034 @*/ 8035 PetscErrorCode MatInterpolateAdd(Mat A,Vec x,Vec y,Vec w) 8036 { 8037 PetscErrorCode ierr; 8038 PetscInt M,N,Ny; 8039 8040 PetscFunctionBegin; 8041 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 8042 PetscValidHeaderSpecific(x,VEC_CLASSID,2); 8043 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 8044 PetscValidHeaderSpecific(w,VEC_CLASSID,4); 8045 PetscValidType(A,1); 8046 MatCheckPreallocated(A,1); 8047 ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr); 8048 ierr = VecGetSize(y,&Ny);CHKERRQ(ierr); 8049 if (M == Ny) { 8050 ierr = MatMultAdd(A,x,y,w);CHKERRQ(ierr); 8051 } else { 8052 ierr = MatMultTransposeAdd(A,x,y,w);CHKERRQ(ierr); 8053 } 8054 PetscFunctionReturn(0); 8055 } 8056 8057 /*@ 8058 MatInterpolate - y = A*x or A'*x depending on the shape of 8059 the matrix 8060 8061 Neighbor-wise Collective on Mat 8062 8063 Input Parameters: 8064 + mat - the matrix 8065 - x,y - the vectors 8066 8067 Level: intermediate 8068 8069 Notes: 8070 This allows one to use either the restriction or interpolation (its transpose) 8071 matrix to do the interpolation 8072 8073 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict() 8074 8075 @*/ 8076 PetscErrorCode MatInterpolate(Mat A,Vec x,Vec y) 8077 { 8078 PetscErrorCode ierr; 8079 PetscInt M,N,Ny; 8080 8081 PetscFunctionBegin; 8082 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 8083 PetscValidHeaderSpecific(x,VEC_CLASSID,2); 8084 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 8085 PetscValidType(A,1); 8086 MatCheckPreallocated(A,1); 8087 ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr); 8088 ierr = VecGetSize(y,&Ny);CHKERRQ(ierr); 8089 if (M == Ny) { 8090 ierr = MatMult(A,x,y);CHKERRQ(ierr); 8091 } else { 8092 ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr); 8093 } 8094 PetscFunctionReturn(0); 8095 } 8096 8097 /*@ 8098 MatRestrict - y = A*x or A'*x 8099 8100 Neighbor-wise Collective on Mat 8101 8102 Input Parameters: 8103 + mat - the matrix 8104 - x,y - the vectors 8105 8106 Level: intermediate 8107 8108 Notes: 8109 This allows one to use either the restriction or interpolation (its transpose) 8110 matrix to do the restriction 8111 8112 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatInterpolate() 8113 8114 @*/ 8115 PetscErrorCode MatRestrict(Mat A,Vec x,Vec y) 8116 { 8117 PetscErrorCode ierr; 8118 PetscInt M,N,Ny; 8119 8120 PetscFunctionBegin; 8121 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 8122 PetscValidHeaderSpecific(x,VEC_CLASSID,2); 8123 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 8124 PetscValidType(A,1); 8125 MatCheckPreallocated(A,1); 8126 8127 ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr); 8128 ierr = VecGetSize(y,&Ny);CHKERRQ(ierr); 8129 if (M == Ny) { 8130 ierr = MatMult(A,x,y);CHKERRQ(ierr); 8131 } else { 8132 ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr); 8133 } 8134 PetscFunctionReturn(0); 8135 } 8136 8137 /*@ 8138 MatGetNullSpace - retrieves the null space of a matrix. 8139 8140 Logically Collective on Mat 8141 8142 Input Parameters: 8143 + mat - the matrix 8144 - nullsp - the null space object 8145 8146 Level: developer 8147 8148 .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace(), MatSetNullSpace() 8149 @*/ 8150 PetscErrorCode MatGetNullSpace(Mat mat, MatNullSpace *nullsp) 8151 { 8152 PetscFunctionBegin; 8153 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8154 PetscValidPointer(nullsp,2); 8155 *nullsp = (mat->symmetric_set && mat->symmetric && !mat->nullsp) ? mat->transnullsp : mat->nullsp; 8156 PetscFunctionReturn(0); 8157 } 8158 8159 /*@ 8160 MatSetNullSpace - attaches a null space to a matrix. 8161 8162 Logically Collective on Mat 8163 8164 Input Parameters: 8165 + mat - the matrix 8166 - nullsp - the null space object 8167 8168 Level: advanced 8169 8170 Notes: 8171 This null space is used by the linear solvers. Overwrites any previous null space that may have been attached 8172 8173 For inconsistent singular systems (linear systems where the right hand side is not in the range of the operator) you also likely should 8174 call MatSetTransposeNullSpace(). This allows the linear system to be solved in a least squares sense. 8175 8176 You can remove the null space by calling this routine with an nullsp of NULL 8177 8178 8179 The fundamental theorem of linear algebra (Gilbert Strang, Introduction to Applied Mathematics, page 72) states that 8180 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). 8181 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 8182 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 8183 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). 8184 8185 Krylov solvers can produce the minimal norm solution to the least squares problem by utilizing MatNullSpaceRemove(). 8186 8187 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 8188 routine also automatically calls MatSetTransposeNullSpace(). 8189 8190 .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace(), MatGetNullSpace(), MatSetTransposeNullSpace(), MatGetTransposeNullSpace(), MatNullSpaceRemove() 8191 @*/ 8192 PetscErrorCode MatSetNullSpace(Mat mat,MatNullSpace nullsp) 8193 { 8194 PetscErrorCode ierr; 8195 8196 PetscFunctionBegin; 8197 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8198 if (nullsp) PetscValidHeaderSpecific(nullsp,MAT_NULLSPACE_CLASSID,2); 8199 if (nullsp) {ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr);} 8200 ierr = MatNullSpaceDestroy(&mat->nullsp);CHKERRQ(ierr); 8201 mat->nullsp = nullsp; 8202 if (mat->symmetric_set && mat->symmetric) { 8203 ierr = MatSetTransposeNullSpace(mat,nullsp);CHKERRQ(ierr); 8204 } 8205 PetscFunctionReturn(0); 8206 } 8207 8208 /*@ 8209 MatGetTransposeNullSpace - retrieves the null space of the transpose of a matrix. 8210 8211 Logically Collective on Mat 8212 8213 Input Parameters: 8214 + mat - the matrix 8215 - nullsp - the null space object 8216 8217 Level: developer 8218 8219 .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace(), MatSetTransposeNullSpace(), MatSetNullSpace(), MatGetNullSpace() 8220 @*/ 8221 PetscErrorCode MatGetTransposeNullSpace(Mat mat, MatNullSpace *nullsp) 8222 { 8223 PetscFunctionBegin; 8224 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8225 PetscValidType(mat,1); 8226 PetscValidPointer(nullsp,2); 8227 *nullsp = (mat->symmetric_set && mat->symmetric && !mat->transnullsp) ? mat->nullsp : mat->transnullsp; 8228 PetscFunctionReturn(0); 8229 } 8230 8231 /*@ 8232 MatSetTransposeNullSpace - attaches a null space to a matrix. 8233 8234 Logically Collective on Mat 8235 8236 Input Parameters: 8237 + mat - the matrix 8238 - nullsp - the null space object 8239 8240 Level: advanced 8241 8242 Notes: 8243 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. 8244 You must also call MatSetNullSpace() 8245 8246 8247 The fundamental theorem of linear algebra (Gilbert Strang, Introduction to Applied Mathematics, page 72) states that 8248 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). 8249 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 8250 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 8251 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). 8252 8253 Krylov solvers can produce the minimal norm solution to the least squares problem by utilizing MatNullSpaceRemove(). 8254 8255 .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace(), MatGetNullSpace(), MatSetNullSpace(), MatGetTransposeNullSpace(), MatNullSpaceRemove() 8256 @*/ 8257 PetscErrorCode MatSetTransposeNullSpace(Mat mat,MatNullSpace nullsp) 8258 { 8259 PetscErrorCode ierr; 8260 8261 PetscFunctionBegin; 8262 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8263 if (nullsp) PetscValidHeaderSpecific(nullsp,MAT_NULLSPACE_CLASSID,2); 8264 if (nullsp) {ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr);} 8265 ierr = MatNullSpaceDestroy(&mat->transnullsp);CHKERRQ(ierr); 8266 mat->transnullsp = nullsp; 8267 PetscFunctionReturn(0); 8268 } 8269 8270 /*@ 8271 MatSetNearNullSpace - attaches a null space to a matrix, which is often the null space (rigid body modes) of the operator without boundary conditions 8272 This null space will be used to provide near null space vectors to a multigrid preconditioner built from this matrix. 8273 8274 Logically Collective on Mat 8275 8276 Input Parameters: 8277 + mat - the matrix 8278 - nullsp - the null space object 8279 8280 Level: advanced 8281 8282 Notes: 8283 Overwrites any previous near null space that may have been attached 8284 8285 You can remove the null space by calling this routine with an nullsp of NULL 8286 8287 .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNullSpace(), MatNullSpaceCreateRigidBody(), MatGetNearNullSpace() 8288 @*/ 8289 PetscErrorCode MatSetNearNullSpace(Mat mat,MatNullSpace nullsp) 8290 { 8291 PetscErrorCode ierr; 8292 8293 PetscFunctionBegin; 8294 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8295 PetscValidType(mat,1); 8296 if (nullsp) PetscValidHeaderSpecific(nullsp,MAT_NULLSPACE_CLASSID,2); 8297 MatCheckPreallocated(mat,1); 8298 if (nullsp) {ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr);} 8299 ierr = MatNullSpaceDestroy(&mat->nearnullsp);CHKERRQ(ierr); 8300 mat->nearnullsp = nullsp; 8301 PetscFunctionReturn(0); 8302 } 8303 8304 /*@ 8305 MatGetNearNullSpace -Get null space attached with MatSetNearNullSpace() 8306 8307 Not Collective 8308 8309 Input Parameters: 8310 . mat - the matrix 8311 8312 Output Parameters: 8313 . nullsp - the null space object, NULL if not set 8314 8315 Level: developer 8316 8317 .seealso: MatSetNearNullSpace(), MatGetNullSpace(), MatNullSpaceCreate() 8318 @*/ 8319 PetscErrorCode MatGetNearNullSpace(Mat mat,MatNullSpace *nullsp) 8320 { 8321 PetscFunctionBegin; 8322 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8323 PetscValidType(mat,1); 8324 PetscValidPointer(nullsp,2); 8325 MatCheckPreallocated(mat,1); 8326 *nullsp = mat->nearnullsp; 8327 PetscFunctionReturn(0); 8328 } 8329 8330 /*@C 8331 MatICCFactor - Performs in-place incomplete Cholesky factorization of matrix. 8332 8333 Collective on Mat 8334 8335 Input Parameters: 8336 + mat - the matrix 8337 . row - row/column permutation 8338 . fill - expected fill factor >= 1.0 8339 - level - level of fill, for ICC(k) 8340 8341 Notes: 8342 Probably really in-place only when level of fill is zero, otherwise allocates 8343 new space to store factored matrix and deletes previous memory. 8344 8345 Most users should employ the simplified KSP interface for linear solvers 8346 instead of working directly with matrix algebra routines such as this. 8347 See, e.g., KSPCreate(). 8348 8349 Level: developer 8350 8351 8352 .seealso: MatICCFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor() 8353 8354 Developer Note: fortran interface is not autogenerated as the f90 8355 interface defintion cannot be generated correctly [due to MatFactorInfo] 8356 8357 @*/ 8358 PetscErrorCode MatICCFactor(Mat mat,IS row,const MatFactorInfo *info) 8359 { 8360 PetscErrorCode ierr; 8361 8362 PetscFunctionBegin; 8363 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8364 PetscValidType(mat,1); 8365 if (row) PetscValidHeaderSpecific(row,IS_CLASSID,2); 8366 PetscValidPointer(info,3); 8367 if (mat->rmap->N != mat->cmap->N) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONG,"matrix must be square"); 8368 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 8369 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 8370 if (!mat->ops->iccfactor) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 8371 MatCheckPreallocated(mat,1); 8372 ierr = (*mat->ops->iccfactor)(mat,row,info);CHKERRQ(ierr); 8373 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 8374 PetscFunctionReturn(0); 8375 } 8376 8377 /*@ 8378 MatDiagonalScaleLocal - Scales columns of a matrix given the scaling values including the 8379 ghosted ones. 8380 8381 Not Collective 8382 8383 Input Parameters: 8384 + mat - the matrix 8385 - diag = the diagonal values, including ghost ones 8386 8387 Level: developer 8388 8389 Notes: 8390 Works only for MPIAIJ and MPIBAIJ matrices 8391 8392 .seealso: MatDiagonalScale() 8393 @*/ 8394 PetscErrorCode MatDiagonalScaleLocal(Mat mat,Vec diag) 8395 { 8396 PetscErrorCode ierr; 8397 PetscMPIInt size; 8398 8399 PetscFunctionBegin; 8400 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8401 PetscValidHeaderSpecific(diag,VEC_CLASSID,2); 8402 PetscValidType(mat,1); 8403 8404 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled"); 8405 ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 8406 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);CHKERRQ(ierr); 8407 if (size == 1) { 8408 PetscInt n,m; 8409 ierr = VecGetSize(diag,&n);CHKERRQ(ierr); 8410 ierr = MatGetSize(mat,0,&m);CHKERRQ(ierr); 8411 if (m == n) { 8412 ierr = MatDiagonalScale(mat,0,diag);CHKERRQ(ierr); 8413 } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only supported for sequential matrices when no ghost points/periodic conditions"); 8414 } else { 8415 ierr = PetscUseMethod(mat,"MatDiagonalScaleLocal_C",(Mat,Vec),(mat,diag));CHKERRQ(ierr); 8416 } 8417 ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 8418 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 8419 PetscFunctionReturn(0); 8420 } 8421 8422 /*@ 8423 MatGetInertia - Gets the inertia from a factored matrix 8424 8425 Collective on Mat 8426 8427 Input Parameter: 8428 . mat - the matrix 8429 8430 Output Parameters: 8431 + nneg - number of negative eigenvalues 8432 . nzero - number of zero eigenvalues 8433 - npos - number of positive eigenvalues 8434 8435 Level: advanced 8436 8437 Notes: 8438 Matrix must have been factored by MatCholeskyFactor() 8439 8440 8441 @*/ 8442 PetscErrorCode MatGetInertia(Mat mat,PetscInt *nneg,PetscInt *nzero,PetscInt *npos) 8443 { 8444 PetscErrorCode ierr; 8445 8446 PetscFunctionBegin; 8447 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8448 PetscValidType(mat,1); 8449 if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 8450 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Numeric factor mat is not assembled"); 8451 if (!mat->ops->getinertia) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 8452 ierr = (*mat->ops->getinertia)(mat,nneg,nzero,npos);CHKERRQ(ierr); 8453 PetscFunctionReturn(0); 8454 } 8455 8456 /* ----------------------------------------------------------------*/ 8457 /*@C 8458 MatSolves - Solves A x = b, given a factored matrix, for a collection of vectors 8459 8460 Neighbor-wise Collective on Mats 8461 8462 Input Parameters: 8463 + mat - the factored matrix 8464 - b - the right-hand-side vectors 8465 8466 Output Parameter: 8467 . x - the result vectors 8468 8469 Notes: 8470 The vectors b and x cannot be the same. I.e., one cannot 8471 call MatSolves(A,x,x). 8472 8473 Notes: 8474 Most users should employ the simplified KSP interface for linear solvers 8475 instead of working directly with matrix algebra routines such as this. 8476 See, e.g., KSPCreate(). 8477 8478 Level: developer 8479 8480 .seealso: MatSolveAdd(), MatSolveTranspose(), MatSolveTransposeAdd(), MatSolve() 8481 @*/ 8482 PetscErrorCode MatSolves(Mat mat,Vecs b,Vecs x) 8483 { 8484 PetscErrorCode ierr; 8485 8486 PetscFunctionBegin; 8487 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8488 PetscValidType(mat,1); 8489 if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 8490 if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 8491 if (!mat->rmap->N && !mat->cmap->N) PetscFunctionReturn(0); 8492 8493 if (!mat->ops->solves) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 8494 MatCheckPreallocated(mat,1); 8495 ierr = PetscLogEventBegin(MAT_Solves,mat,0,0,0);CHKERRQ(ierr); 8496 ierr = (*mat->ops->solves)(mat,b,x);CHKERRQ(ierr); 8497 ierr = PetscLogEventEnd(MAT_Solves,mat,0,0,0);CHKERRQ(ierr); 8498 PetscFunctionReturn(0); 8499 } 8500 8501 /*@ 8502 MatIsSymmetric - Test whether a matrix is symmetric 8503 8504 Collective on Mat 8505 8506 Input Parameter: 8507 + A - the matrix to test 8508 - tol - difference between value and its transpose less than this amount counts as equal (use 0.0 for exact transpose) 8509 8510 Output Parameters: 8511 . flg - the result 8512 8513 Notes: 8514 For real numbers MatIsSymmetric() and MatIsHermitian() return identical results 8515 8516 Level: intermediate 8517 8518 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetricKnown() 8519 @*/ 8520 PetscErrorCode MatIsSymmetric(Mat A,PetscReal tol,PetscBool *flg) 8521 { 8522 PetscErrorCode ierr; 8523 8524 PetscFunctionBegin; 8525 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 8526 PetscValidBoolPointer(flg,2); 8527 8528 if (!A->symmetric_set) { 8529 if (!A->ops->issymmetric) { 8530 MatType mattype; 8531 ierr = MatGetType(A,&mattype);CHKERRQ(ierr); 8532 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for symmetric",mattype); 8533 } 8534 ierr = (*A->ops->issymmetric)(A,tol,flg);CHKERRQ(ierr); 8535 if (!tol) { 8536 A->symmetric_set = PETSC_TRUE; 8537 A->symmetric = *flg; 8538 if (A->symmetric) { 8539 A->structurally_symmetric_set = PETSC_TRUE; 8540 A->structurally_symmetric = PETSC_TRUE; 8541 } 8542 } 8543 } else if (A->symmetric) { 8544 *flg = PETSC_TRUE; 8545 } else if (!tol) { 8546 *flg = PETSC_FALSE; 8547 } else { 8548 if (!A->ops->issymmetric) { 8549 MatType mattype; 8550 ierr = MatGetType(A,&mattype);CHKERRQ(ierr); 8551 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for symmetric",mattype); 8552 } 8553 ierr = (*A->ops->issymmetric)(A,tol,flg);CHKERRQ(ierr); 8554 } 8555 PetscFunctionReturn(0); 8556 } 8557 8558 /*@ 8559 MatIsHermitian - Test whether a matrix is Hermitian 8560 8561 Collective on Mat 8562 8563 Input Parameter: 8564 + A - the matrix to test 8565 - tol - difference between value and its transpose less than this amount counts as equal (use 0.0 for exact Hermitian) 8566 8567 Output Parameters: 8568 . flg - the result 8569 8570 Level: intermediate 8571 8572 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), 8573 MatIsSymmetricKnown(), MatIsSymmetric() 8574 @*/ 8575 PetscErrorCode MatIsHermitian(Mat A,PetscReal tol,PetscBool *flg) 8576 { 8577 PetscErrorCode ierr; 8578 8579 PetscFunctionBegin; 8580 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 8581 PetscValidBoolPointer(flg,2); 8582 8583 if (!A->hermitian_set) { 8584 if (!A->ops->ishermitian) { 8585 MatType mattype; 8586 ierr = MatGetType(A,&mattype);CHKERRQ(ierr); 8587 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for hermitian",mattype); 8588 } 8589 ierr = (*A->ops->ishermitian)(A,tol,flg);CHKERRQ(ierr); 8590 if (!tol) { 8591 A->hermitian_set = PETSC_TRUE; 8592 A->hermitian = *flg; 8593 if (A->hermitian) { 8594 A->structurally_symmetric_set = PETSC_TRUE; 8595 A->structurally_symmetric = PETSC_TRUE; 8596 } 8597 } 8598 } else if (A->hermitian) { 8599 *flg = PETSC_TRUE; 8600 } else if (!tol) { 8601 *flg = PETSC_FALSE; 8602 } else { 8603 if (!A->ops->ishermitian) { 8604 MatType mattype; 8605 ierr = MatGetType(A,&mattype);CHKERRQ(ierr); 8606 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for hermitian",mattype); 8607 } 8608 ierr = (*A->ops->ishermitian)(A,tol,flg);CHKERRQ(ierr); 8609 } 8610 PetscFunctionReturn(0); 8611 } 8612 8613 /*@ 8614 MatIsSymmetricKnown - Checks the flag on the matrix to see if it is symmetric. 8615 8616 Not Collective 8617 8618 Input Parameter: 8619 . A - the matrix to check 8620 8621 Output Parameters: 8622 + set - if the symmetric flag is set (this tells you if the next flag is valid) 8623 - flg - the result 8624 8625 Level: advanced 8626 8627 Note: Does not check the matrix values directly, so this may return unknown (set = PETSC_FALSE). Use MatIsSymmetric() 8628 if you want it explicitly checked 8629 8630 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetric() 8631 @*/ 8632 PetscErrorCode MatIsSymmetricKnown(Mat A,PetscBool *set,PetscBool *flg) 8633 { 8634 PetscFunctionBegin; 8635 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 8636 PetscValidPointer(set,2); 8637 PetscValidBoolPointer(flg,3); 8638 if (A->symmetric_set) { 8639 *set = PETSC_TRUE; 8640 *flg = A->symmetric; 8641 } else { 8642 *set = PETSC_FALSE; 8643 } 8644 PetscFunctionReturn(0); 8645 } 8646 8647 /*@ 8648 MatIsHermitianKnown - Checks the flag on the matrix to see if it is hermitian. 8649 8650 Not Collective 8651 8652 Input Parameter: 8653 . A - the matrix to check 8654 8655 Output Parameters: 8656 + set - if the hermitian flag is set (this tells you if the next flag is valid) 8657 - flg - the result 8658 8659 Level: advanced 8660 8661 Note: Does not check the matrix values directly, so this may return unknown (set = PETSC_FALSE). Use MatIsHermitian() 8662 if you want it explicitly checked 8663 8664 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetric() 8665 @*/ 8666 PetscErrorCode MatIsHermitianKnown(Mat A,PetscBool *set,PetscBool *flg) 8667 { 8668 PetscFunctionBegin; 8669 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 8670 PetscValidPointer(set,2); 8671 PetscValidBoolPointer(flg,3); 8672 if (A->hermitian_set) { 8673 *set = PETSC_TRUE; 8674 *flg = A->hermitian; 8675 } else { 8676 *set = PETSC_FALSE; 8677 } 8678 PetscFunctionReturn(0); 8679 } 8680 8681 /*@ 8682 MatIsStructurallySymmetric - Test whether a matrix is structurally symmetric 8683 8684 Collective on Mat 8685 8686 Input Parameter: 8687 . A - the matrix to test 8688 8689 Output Parameters: 8690 . flg - the result 8691 8692 Level: intermediate 8693 8694 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsSymmetric(), MatSetOption() 8695 @*/ 8696 PetscErrorCode MatIsStructurallySymmetric(Mat A,PetscBool *flg) 8697 { 8698 PetscErrorCode ierr; 8699 8700 PetscFunctionBegin; 8701 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 8702 PetscValidBoolPointer(flg,2); 8703 if (!A->structurally_symmetric_set) { 8704 if (!A->ops->isstructurallysymmetric) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Matrix does not support checking for structural symmetric"); 8705 ierr = (*A->ops->isstructurallysymmetric)(A,&A->structurally_symmetric);CHKERRQ(ierr); 8706 8707 A->structurally_symmetric_set = PETSC_TRUE; 8708 } 8709 *flg = A->structurally_symmetric; 8710 PetscFunctionReturn(0); 8711 } 8712 8713 /*@ 8714 MatStashGetInfo - Gets how many values are currently in the matrix stash, i.e. need 8715 to be communicated to other processors during the MatAssemblyBegin/End() process 8716 8717 Not collective 8718 8719 Input Parameter: 8720 . vec - the vector 8721 8722 Output Parameters: 8723 + nstash - the size of the stash 8724 . reallocs - the number of additional mallocs incurred. 8725 . bnstash - the size of the block stash 8726 - breallocs - the number of additional mallocs incurred.in the block stash 8727 8728 Level: advanced 8729 8730 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), Mat, MatStashSetInitialSize() 8731 8732 @*/ 8733 PetscErrorCode MatStashGetInfo(Mat mat,PetscInt *nstash,PetscInt *reallocs,PetscInt *bnstash,PetscInt *breallocs) 8734 { 8735 PetscErrorCode ierr; 8736 8737 PetscFunctionBegin; 8738 ierr = MatStashGetInfo_Private(&mat->stash,nstash,reallocs);CHKERRQ(ierr); 8739 ierr = MatStashGetInfo_Private(&mat->bstash,bnstash,breallocs);CHKERRQ(ierr); 8740 PetscFunctionReturn(0); 8741 } 8742 8743 /*@C 8744 MatCreateVecs - Get vector(s) compatible with the matrix, i.e. with the same 8745 parallel layout 8746 8747 Collective on Mat 8748 8749 Input Parameter: 8750 . mat - the matrix 8751 8752 Output Parameter: 8753 + right - (optional) vector that the matrix can be multiplied against 8754 - left - (optional) vector that the matrix vector product can be stored in 8755 8756 Notes: 8757 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(). 8758 8759 Notes: 8760 These are new vectors which are not owned by the Mat, they should be destroyed in VecDestroy() when no longer needed 8761 8762 Level: advanced 8763 8764 .seealso: MatCreate(), VecDestroy() 8765 @*/ 8766 PetscErrorCode MatCreateVecs(Mat mat,Vec *right,Vec *left) 8767 { 8768 PetscErrorCode ierr; 8769 8770 PetscFunctionBegin; 8771 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8772 PetscValidType(mat,1); 8773 if (mat->ops->getvecs) { 8774 ierr = (*mat->ops->getvecs)(mat,right,left);CHKERRQ(ierr); 8775 } else { 8776 PetscInt rbs,cbs; 8777 ierr = MatGetBlockSizes(mat,&rbs,&cbs);CHKERRQ(ierr); 8778 if (right) { 8779 if (mat->cmap->n < 0) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"PetscLayout for columns not yet setup"); 8780 ierr = VecCreate(PetscObjectComm((PetscObject)mat),right);CHKERRQ(ierr); 8781 ierr = VecSetSizes(*right,mat->cmap->n,PETSC_DETERMINE);CHKERRQ(ierr); 8782 ierr = VecSetBlockSize(*right,cbs);CHKERRQ(ierr); 8783 ierr = VecSetType(*right,mat->defaultvectype);CHKERRQ(ierr); 8784 ierr = PetscLayoutReference(mat->cmap,&(*right)->map);CHKERRQ(ierr); 8785 } 8786 if (left) { 8787 if (mat->rmap->n < 0) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"PetscLayout for rows not yet setup"); 8788 ierr = VecCreate(PetscObjectComm((PetscObject)mat),left);CHKERRQ(ierr); 8789 ierr = VecSetSizes(*left,mat->rmap->n,PETSC_DETERMINE);CHKERRQ(ierr); 8790 ierr = VecSetBlockSize(*left,rbs);CHKERRQ(ierr); 8791 ierr = VecSetType(*left,mat->defaultvectype);CHKERRQ(ierr); 8792 ierr = PetscLayoutReference(mat->rmap,&(*left)->map);CHKERRQ(ierr); 8793 } 8794 } 8795 PetscFunctionReturn(0); 8796 } 8797 8798 /*@C 8799 MatFactorInfoInitialize - Initializes a MatFactorInfo data structure 8800 with default values. 8801 8802 Not Collective 8803 8804 Input Parameters: 8805 . info - the MatFactorInfo data structure 8806 8807 8808 Notes: 8809 The solvers are generally used through the KSP and PC objects, for example 8810 PCLU, PCILU, PCCHOLESKY, PCICC 8811 8812 Level: developer 8813 8814 .seealso: MatFactorInfo 8815 8816 Developer Note: fortran interface is not autogenerated as the f90 8817 interface defintion cannot be generated correctly [due to MatFactorInfo] 8818 8819 @*/ 8820 8821 PetscErrorCode MatFactorInfoInitialize(MatFactorInfo *info) 8822 { 8823 PetscErrorCode ierr; 8824 8825 PetscFunctionBegin; 8826 ierr = PetscMemzero(info,sizeof(MatFactorInfo));CHKERRQ(ierr); 8827 PetscFunctionReturn(0); 8828 } 8829 8830 /*@ 8831 MatFactorSetSchurIS - Set indices corresponding to the Schur complement you wish to have computed 8832 8833 Collective on Mat 8834 8835 Input Parameters: 8836 + mat - the factored matrix 8837 - is - the index set defining the Schur indices (0-based) 8838 8839 Notes: 8840 Call MatFactorSolveSchurComplement() or MatFactorSolveSchurComplementTranspose() after this call to solve a Schur complement system. 8841 8842 You can call MatFactorGetSchurComplement() or MatFactorCreateSchurComplement() after this call. 8843 8844 Level: developer 8845 8846 .seealso: MatGetFactor(), MatFactorGetSchurComplement(), MatFactorRestoreSchurComplement(), MatFactorCreateSchurComplement(), MatFactorSolveSchurComplement(), 8847 MatFactorSolveSchurComplementTranspose(), MatFactorSolveSchurComplement() 8848 8849 @*/ 8850 PetscErrorCode MatFactorSetSchurIS(Mat mat,IS is) 8851 { 8852 PetscErrorCode ierr,(*f)(Mat,IS); 8853 8854 PetscFunctionBegin; 8855 PetscValidType(mat,1); 8856 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8857 PetscValidType(is,2); 8858 PetscValidHeaderSpecific(is,IS_CLASSID,2); 8859 PetscCheckSameComm(mat,1,is,2); 8860 if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Only for factored matrix"); 8861 ierr = PetscObjectQueryFunction((PetscObject)mat,"MatFactorSetSchurIS_C",&f);CHKERRQ(ierr); 8862 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"); 8863 if (mat->schur) { 8864 ierr = MatDestroy(&mat->schur);CHKERRQ(ierr); 8865 } 8866 ierr = (*f)(mat,is);CHKERRQ(ierr); 8867 if (!mat->schur) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_PLIB,"Schur complement has not been created"); 8868 ierr = MatFactorSetUpInPlaceSchur_Private(mat);CHKERRQ(ierr); 8869 PetscFunctionReturn(0); 8870 } 8871 8872 /*@ 8873 MatFactorCreateSchurComplement - Create a Schur complement matrix object using Schur data computed during the factorization step 8874 8875 Logically Collective on Mat 8876 8877 Input Parameters: 8878 + F - the factored matrix obtained by calling MatGetFactor() from PETSc-MUMPS interface 8879 . S - location where to return the Schur complement, can be NULL 8880 - status - the status of the Schur complement matrix, can be NULL 8881 8882 Notes: 8883 You must call MatFactorSetSchurIS() before calling this routine. 8884 8885 The routine provides a copy of the Schur matrix stored within the solver data structures. 8886 The caller must destroy the object when it is no longer needed. 8887 If MatFactorInvertSchurComplement() has been called, the routine gets back the inverse. 8888 8889 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) 8890 8891 Developer Notes: 8892 The reason this routine exists is because the representation of the Schur complement within the factor matrix may be different than a standard PETSc 8893 matrix representation and we normally do not want to use the time or memory to make a copy as a regular PETSc matrix. 8894 8895 See MatCreateSchurComplement() or MatGetSchurComplement() for ways to create virtual or approximate Schur complements. 8896 8897 Level: advanced 8898 8899 References: 8900 8901 .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorGetSchurComplement(), MatFactorSchurStatus 8902 @*/ 8903 PetscErrorCode MatFactorCreateSchurComplement(Mat F,Mat* S,MatFactorSchurStatus* status) 8904 { 8905 PetscErrorCode ierr; 8906 8907 PetscFunctionBegin; 8908 PetscValidHeaderSpecific(F,MAT_CLASSID,1); 8909 if (S) PetscValidPointer(S,2); 8910 if (status) PetscValidPointer(status,3); 8911 if (S) { 8912 PetscErrorCode (*f)(Mat,Mat*); 8913 8914 ierr = PetscObjectQueryFunction((PetscObject)F,"MatFactorCreateSchurComplement_C",&f);CHKERRQ(ierr); 8915 if (f) { 8916 ierr = (*f)(F,S);CHKERRQ(ierr); 8917 } else { 8918 ierr = MatDuplicate(F->schur,MAT_COPY_VALUES,S);CHKERRQ(ierr); 8919 } 8920 } 8921 if (status) *status = F->schur_status; 8922 PetscFunctionReturn(0); 8923 } 8924 8925 /*@ 8926 MatFactorGetSchurComplement - Gets access to a Schur complement matrix using the current Schur data within a factored matrix 8927 8928 Logically Collective on Mat 8929 8930 Input Parameters: 8931 + F - the factored matrix obtained by calling MatGetFactor() 8932 . *S - location where to return the Schur complement, can be NULL 8933 - status - the status of the Schur complement matrix, can be NULL 8934 8935 Notes: 8936 You must call MatFactorSetSchurIS() before calling this routine. 8937 8938 Schur complement mode is currently implemented for sequential matrices. 8939 The routine returns a the Schur Complement stored within the data strutures of the solver. 8940 If MatFactorInvertSchurComplement() has previously been called, the returned matrix is actually the inverse of the Schur complement. 8941 The returned matrix should not be destroyed; the caller should call MatFactorRestoreSchurComplement() when the object is no longer needed. 8942 8943 Use MatFactorCreateSchurComplement() to create a copy of the Schur complement matrix that is within a factored matrix 8944 8945 See MatCreateSchurComplement() or MatGetSchurComplement() for ways to create virtual or approximate Schur complements. 8946 8947 Level: advanced 8948 8949 References: 8950 8951 .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorRestoreSchurComplement(), MatFactorCreateSchurComplement(), MatFactorSchurStatus 8952 @*/ 8953 PetscErrorCode MatFactorGetSchurComplement(Mat F,Mat* S,MatFactorSchurStatus* status) 8954 { 8955 PetscFunctionBegin; 8956 PetscValidHeaderSpecific(F,MAT_CLASSID,1); 8957 if (S) PetscValidPointer(S,2); 8958 if (status) PetscValidPointer(status,3); 8959 if (S) *S = F->schur; 8960 if (status) *status = F->schur_status; 8961 PetscFunctionReturn(0); 8962 } 8963 8964 /*@ 8965 MatFactorRestoreSchurComplement - Restore the Schur complement matrix object obtained from a call to MatFactorGetSchurComplement 8966 8967 Logically Collective on Mat 8968 8969 Input Parameters: 8970 + F - the factored matrix obtained by calling MatGetFactor() 8971 . *S - location where the Schur complement is stored 8972 - status - the status of the Schur complement matrix (see MatFactorSchurStatus) 8973 8974 Notes: 8975 8976 Level: advanced 8977 8978 References: 8979 8980 .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorRestoreSchurComplement(), MatFactorCreateSchurComplement(), MatFactorSchurStatus 8981 @*/ 8982 PetscErrorCode MatFactorRestoreSchurComplement(Mat F,Mat* S,MatFactorSchurStatus status) 8983 { 8984 PetscErrorCode ierr; 8985 8986 PetscFunctionBegin; 8987 PetscValidHeaderSpecific(F,MAT_CLASSID,1); 8988 if (S) { 8989 PetscValidHeaderSpecific(*S,MAT_CLASSID,2); 8990 *S = NULL; 8991 } 8992 F->schur_status = status; 8993 ierr = MatFactorUpdateSchurStatus_Private(F);CHKERRQ(ierr); 8994 PetscFunctionReturn(0); 8995 } 8996 8997 /*@ 8998 MatFactorSolveSchurComplementTranspose - Solve the transpose of the Schur complement system computed during the factorization step 8999 9000 Logically Collective on Mat 9001 9002 Input Parameters: 9003 + F - the factored matrix obtained by calling MatGetFactor() 9004 . rhs - location where the right hand side of the Schur complement system is stored 9005 - sol - location where the solution of the Schur complement system has to be returned 9006 9007 Notes: 9008 The sizes of the vectors should match the size of the Schur complement 9009 9010 Must be called after MatFactorSetSchurIS() 9011 9012 Level: advanced 9013 9014 References: 9015 9016 .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorSolveSchurComplement() 9017 @*/ 9018 PetscErrorCode MatFactorSolveSchurComplementTranspose(Mat F, Vec rhs, Vec sol) 9019 { 9020 PetscErrorCode ierr; 9021 9022 PetscFunctionBegin; 9023 PetscValidType(F,1); 9024 PetscValidType(rhs,2); 9025 PetscValidType(sol,3); 9026 PetscValidHeaderSpecific(F,MAT_CLASSID,1); 9027 PetscValidHeaderSpecific(rhs,VEC_CLASSID,2); 9028 PetscValidHeaderSpecific(sol,VEC_CLASSID,3); 9029 PetscCheckSameComm(F,1,rhs,2); 9030 PetscCheckSameComm(F,1,sol,3); 9031 ierr = MatFactorFactorizeSchurComplement(F);CHKERRQ(ierr); 9032 switch (F->schur_status) { 9033 case MAT_FACTOR_SCHUR_FACTORED: 9034 ierr = MatSolveTranspose(F->schur,rhs,sol);CHKERRQ(ierr); 9035 break; 9036 case MAT_FACTOR_SCHUR_INVERTED: 9037 ierr = MatMultTranspose(F->schur,rhs,sol);CHKERRQ(ierr); 9038 break; 9039 default: 9040 SETERRQ1(PetscObjectComm((PetscObject)F),PETSC_ERR_SUP,"Unhandled MatFactorSchurStatus %D",F->schur_status); 9041 break; 9042 } 9043 PetscFunctionReturn(0); 9044 } 9045 9046 /*@ 9047 MatFactorSolveSchurComplement - Solve the Schur complement system computed during the factorization step 9048 9049 Logically Collective on Mat 9050 9051 Input Parameters: 9052 + F - the factored matrix obtained by calling MatGetFactor() 9053 . rhs - location where the right hand side of the Schur complement system is stored 9054 - sol - location where the solution of the Schur complement system has to be returned 9055 9056 Notes: 9057 The sizes of the vectors should match the size of the Schur complement 9058 9059 Must be called after MatFactorSetSchurIS() 9060 9061 Level: advanced 9062 9063 References: 9064 9065 .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorSolveSchurComplementTranspose() 9066 @*/ 9067 PetscErrorCode MatFactorSolveSchurComplement(Mat F, Vec rhs, Vec sol) 9068 { 9069 PetscErrorCode ierr; 9070 9071 PetscFunctionBegin; 9072 PetscValidType(F,1); 9073 PetscValidType(rhs,2); 9074 PetscValidType(sol,3); 9075 PetscValidHeaderSpecific(F,MAT_CLASSID,1); 9076 PetscValidHeaderSpecific(rhs,VEC_CLASSID,2); 9077 PetscValidHeaderSpecific(sol,VEC_CLASSID,3); 9078 PetscCheckSameComm(F,1,rhs,2); 9079 PetscCheckSameComm(F,1,sol,3); 9080 ierr = MatFactorFactorizeSchurComplement(F);CHKERRQ(ierr); 9081 switch (F->schur_status) { 9082 case MAT_FACTOR_SCHUR_FACTORED: 9083 ierr = MatSolve(F->schur,rhs,sol);CHKERRQ(ierr); 9084 break; 9085 case MAT_FACTOR_SCHUR_INVERTED: 9086 ierr = MatMult(F->schur,rhs,sol);CHKERRQ(ierr); 9087 break; 9088 default: 9089 SETERRQ1(PetscObjectComm((PetscObject)F),PETSC_ERR_SUP,"Unhandled MatFactorSchurStatus %D",F->schur_status); 9090 break; 9091 } 9092 PetscFunctionReturn(0); 9093 } 9094 9095 /*@ 9096 MatFactorInvertSchurComplement - Invert the Schur complement matrix computed during the factorization step 9097 9098 Logically Collective on Mat 9099 9100 Input Parameters: 9101 . F - the factored matrix obtained by calling MatGetFactor() 9102 9103 Notes: 9104 Must be called after MatFactorSetSchurIS(). 9105 9106 Call MatFactorGetSchurComplement() or MatFactorCreateSchurComplement() AFTER this call to actually compute the inverse and get access to it. 9107 9108 Level: advanced 9109 9110 References: 9111 9112 .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorGetSchurComplement(), MatFactorCreateSchurComplement() 9113 @*/ 9114 PetscErrorCode MatFactorInvertSchurComplement(Mat F) 9115 { 9116 PetscErrorCode ierr; 9117 9118 PetscFunctionBegin; 9119 PetscValidType(F,1); 9120 PetscValidHeaderSpecific(F,MAT_CLASSID,1); 9121 if (F->schur_status == MAT_FACTOR_SCHUR_INVERTED) PetscFunctionReturn(0); 9122 ierr = MatFactorFactorizeSchurComplement(F);CHKERRQ(ierr); 9123 ierr = MatFactorInvertSchurComplement_Private(F);CHKERRQ(ierr); 9124 F->schur_status = MAT_FACTOR_SCHUR_INVERTED; 9125 PetscFunctionReturn(0); 9126 } 9127 9128 /*@ 9129 MatFactorFactorizeSchurComplement - Factorize the Schur complement matrix computed during the factorization step 9130 9131 Logically Collective on Mat 9132 9133 Input Parameters: 9134 . F - the factored matrix obtained by calling MatGetFactor() 9135 9136 Notes: 9137 Must be called after MatFactorSetSchurIS(). 9138 9139 Level: advanced 9140 9141 References: 9142 9143 .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorInvertSchurComplement() 9144 @*/ 9145 PetscErrorCode MatFactorFactorizeSchurComplement(Mat F) 9146 { 9147 PetscErrorCode ierr; 9148 9149 PetscFunctionBegin; 9150 PetscValidType(F,1); 9151 PetscValidHeaderSpecific(F,MAT_CLASSID,1); 9152 if (F->schur_status == MAT_FACTOR_SCHUR_INVERTED || F->schur_status == MAT_FACTOR_SCHUR_FACTORED) PetscFunctionReturn(0); 9153 ierr = MatFactorFactorizeSchurComplement_Private(F);CHKERRQ(ierr); 9154 F->schur_status = MAT_FACTOR_SCHUR_FACTORED; 9155 PetscFunctionReturn(0); 9156 } 9157 9158 PetscErrorCode MatPtAP_Basic(Mat A,Mat P,MatReuse scall,PetscReal fill,Mat *C) 9159 { 9160 Mat AP; 9161 PetscErrorCode ierr; 9162 9163 PetscFunctionBegin; 9164 ierr = PetscInfo2(A,"Mat types %s and %s using basic PtAP\n",((PetscObject)A)->type_name,((PetscObject)P)->type_name);CHKERRQ(ierr); 9165 ierr = MatMatMult(A,P,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&AP);CHKERRQ(ierr); 9166 ierr = MatTransposeMatMult(P,AP,scall,fill,C);CHKERRQ(ierr); 9167 ierr = MatDestroy(&AP);CHKERRQ(ierr); 9168 PetscFunctionReturn(0); 9169 } 9170 9171 /*@ 9172 MatPtAP - Creates the matrix product C = P^T * A * P 9173 9174 Neighbor-wise Collective on Mat 9175 9176 Input Parameters: 9177 + A - the matrix 9178 . P - the projection matrix 9179 . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 9180 - fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(P)), use PETSC_DEFAULT if you do not have a good estimate 9181 if the result is a dense matrix this is irrelevent 9182 9183 Output Parameters: 9184 . C - the product matrix 9185 9186 Notes: 9187 C will be created and must be destroyed by the user with MatDestroy(). 9188 9189 For matrix types without special implementation the function fallbacks to MatMatMult() followed by MatTransposeMatMult(). 9190 9191 Level: intermediate 9192 9193 .seealso: MatPtAPSymbolic(), MatPtAPNumeric(), MatMatMult(), MatRARt() 9194 @*/ 9195 PetscErrorCode MatPtAP(Mat A,Mat P,MatReuse scall,PetscReal fill,Mat *C) 9196 { 9197 PetscErrorCode ierr; 9198 PetscErrorCode (*fA)(Mat,Mat,MatReuse,PetscReal,Mat*); 9199 PetscErrorCode (*fP)(Mat,Mat,MatReuse,PetscReal,Mat*); 9200 PetscErrorCode (*ptap)(Mat,Mat,MatReuse,PetscReal,Mat*)=NULL; 9201 PetscBool sametype; 9202 9203 PetscFunctionBegin; 9204 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 9205 PetscValidType(A,1); 9206 MatCheckPreallocated(A,1); 9207 if (scall == MAT_INPLACE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported"); 9208 if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9209 if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9210 PetscValidHeaderSpecific(P,MAT_CLASSID,2); 9211 PetscValidType(P,2); 9212 MatCheckPreallocated(P,2); 9213 if (!P->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9214 if (P->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9215 9216 if (A->rmap->N != A->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix A must be square, %D != %D",A->rmap->N,A->cmap->N); 9217 if (P->rmap->N != A->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",P->rmap->N,A->cmap->N); 9218 if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0; 9219 if (fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Expected fill=%g must be >= 1.0",(double)fill); 9220 9221 if (scall == MAT_REUSE_MATRIX) { 9222 PetscValidPointer(*C,5); 9223 PetscValidHeaderSpecific(*C,MAT_CLASSID,5); 9224 9225 ierr = PetscLogEventBegin(MAT_PtAP,A,P,0,0);CHKERRQ(ierr); 9226 ierr = PetscLogEventBegin(MAT_PtAPNumeric,A,P,0,0);CHKERRQ(ierr); 9227 if ((*C)->ops->ptapnumeric) { 9228 ierr = (*(*C)->ops->ptapnumeric)(A,P,*C);CHKERRQ(ierr); 9229 } else { 9230 ierr = MatPtAP_Basic(A,P,scall,fill,C); 9231 } 9232 ierr = PetscLogEventEnd(MAT_PtAPNumeric,A,P,0,0);CHKERRQ(ierr); 9233 ierr = PetscLogEventEnd(MAT_PtAP,A,P,0,0);CHKERRQ(ierr); 9234 PetscFunctionReturn(0); 9235 } 9236 9237 if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0; 9238 if (fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Expected fill=%g must be >= 1.0",(double)fill); 9239 9240 fA = A->ops->ptap; 9241 fP = P->ops->ptap; 9242 ierr = PetscStrcmp(((PetscObject)A)->type_name,((PetscObject)P)->type_name,&sametype);CHKERRQ(ierr); 9243 if (fP == fA && sametype) { 9244 ptap = fA; 9245 } else { 9246 /* dispatch based on the type of A and P from their PetscObject's PetscFunctionLists. */ 9247 char ptapname[256]; 9248 ierr = PetscStrncpy(ptapname,"MatPtAP_",sizeof(ptapname));CHKERRQ(ierr); 9249 ierr = PetscStrlcat(ptapname,((PetscObject)A)->type_name,sizeof(ptapname));CHKERRQ(ierr); 9250 ierr = PetscStrlcat(ptapname,"_",sizeof(ptapname));CHKERRQ(ierr); 9251 ierr = PetscStrlcat(ptapname,((PetscObject)P)->type_name,sizeof(ptapname));CHKERRQ(ierr); 9252 ierr = PetscStrlcat(ptapname,"_C",sizeof(ptapname));CHKERRQ(ierr); /* e.g., ptapname = "MatPtAP_seqdense_seqaij_C" */ 9253 ierr = PetscObjectQueryFunction((PetscObject)P,ptapname,&ptap);CHKERRQ(ierr); 9254 } 9255 9256 if (!ptap) ptap = MatPtAP_Basic; 9257 ierr = PetscLogEventBegin(MAT_PtAP,A,P,0,0);CHKERRQ(ierr); 9258 ierr = (*ptap)(A,P,scall,fill,C);CHKERRQ(ierr); 9259 ierr = PetscLogEventEnd(MAT_PtAP,A,P,0,0);CHKERRQ(ierr); 9260 if (A->symmetric_set && A->symmetric) { 9261 ierr = MatSetOption(*C,MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr); 9262 } 9263 PetscFunctionReturn(0); 9264 } 9265 9266 /*@ 9267 MatPtAPNumeric - Computes the matrix product C = P^T * A * P 9268 9269 Neighbor-wise Collective on Mat 9270 9271 Input Parameters: 9272 + A - the matrix 9273 - P - the projection matrix 9274 9275 Output Parameters: 9276 . C - the product matrix 9277 9278 Notes: 9279 C must have been created by calling MatPtAPSymbolic and must be destroyed by 9280 the user using MatDeatroy(). 9281 9282 This routine is currently only implemented for pairs of AIJ matrices and classes 9283 which inherit from AIJ. C will be of type MATAIJ. 9284 9285 Level: intermediate 9286 9287 .seealso: MatPtAP(), MatPtAPSymbolic(), MatMatMultNumeric() 9288 @*/ 9289 PetscErrorCode MatPtAPNumeric(Mat A,Mat P,Mat C) 9290 { 9291 PetscErrorCode ierr; 9292 9293 PetscFunctionBegin; 9294 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 9295 PetscValidType(A,1); 9296 if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9297 if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9298 PetscValidHeaderSpecific(P,MAT_CLASSID,2); 9299 PetscValidType(P,2); 9300 MatCheckPreallocated(P,2); 9301 if (!P->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9302 if (P->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9303 PetscValidHeaderSpecific(C,MAT_CLASSID,3); 9304 PetscValidType(C,3); 9305 MatCheckPreallocated(C,3); 9306 if (C->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9307 if (P->cmap->N!=C->rmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",P->cmap->N,C->rmap->N); 9308 if (P->rmap->N!=A->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",P->rmap->N,A->cmap->N); 9309 if (A->rmap->N!=A->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix 'A' must be square, %D != %D",A->rmap->N,A->cmap->N); 9310 if (P->cmap->N!=C->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",P->cmap->N,C->cmap->N); 9311 MatCheckPreallocated(A,1); 9312 9313 if (!C->ops->ptapnumeric) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_ARG_WRONGSTATE,"MatPtAPNumeric implementation is missing. You should call MatPtAPSymbolic first"); 9314 ierr = PetscLogEventBegin(MAT_PtAPNumeric,A,P,0,0);CHKERRQ(ierr); 9315 ierr = (*C->ops->ptapnumeric)(A,P,C);CHKERRQ(ierr); 9316 ierr = PetscLogEventEnd(MAT_PtAPNumeric,A,P,0,0);CHKERRQ(ierr); 9317 PetscFunctionReturn(0); 9318 } 9319 9320 /*@ 9321 MatPtAPSymbolic - Creates the (i,j) structure of the matrix product C = P^T * A * P 9322 9323 Neighbor-wise Collective on Mat 9324 9325 Input Parameters: 9326 + A - the matrix 9327 - P - the projection matrix 9328 9329 Output Parameters: 9330 . C - the (i,j) structure of the product matrix 9331 9332 Notes: 9333 C will be created and must be destroyed by the user with MatDestroy(). 9334 9335 This routine is currently only implemented for pairs of SeqAIJ matrices and classes 9336 which inherit from SeqAIJ. C will be of type MATSEQAIJ. The product is computed using 9337 this (i,j) structure by calling MatPtAPNumeric(). 9338 9339 Level: intermediate 9340 9341 .seealso: MatPtAP(), MatPtAPNumeric(), MatMatMultSymbolic() 9342 @*/ 9343 PetscErrorCode MatPtAPSymbolic(Mat A,Mat P,PetscReal fill,Mat *C) 9344 { 9345 PetscErrorCode ierr; 9346 9347 PetscFunctionBegin; 9348 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 9349 PetscValidType(A,1); 9350 if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9351 if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9352 if (fill <1.0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Expected fill=%g must be >= 1.0",(double)fill); 9353 PetscValidHeaderSpecific(P,MAT_CLASSID,2); 9354 PetscValidType(P,2); 9355 MatCheckPreallocated(P,2); 9356 if (!P->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9357 if (P->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9358 PetscValidPointer(C,3); 9359 9360 if (P->rmap->N!=A->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",P->rmap->N,A->cmap->N); 9361 if (A->rmap->N!=A->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix 'A' must be square, %D != %D",A->rmap->N,A->cmap->N); 9362 MatCheckPreallocated(A,1); 9363 9364 if (!A->ops->ptapsymbolic) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"MatType %s",((PetscObject)A)->type_name); 9365 ierr = PetscLogEventBegin(MAT_PtAPSymbolic,A,P,0,0);CHKERRQ(ierr); 9366 ierr = (*A->ops->ptapsymbolic)(A,P,fill,C);CHKERRQ(ierr); 9367 ierr = PetscLogEventEnd(MAT_PtAPSymbolic,A,P,0,0);CHKERRQ(ierr); 9368 9369 /* ierr = MatSetBlockSize(*C,A->rmap->bs);CHKERRQ(ierr); NO! this is not always true -ma */ 9370 PetscFunctionReturn(0); 9371 } 9372 9373 /*@ 9374 MatRARt - Creates the matrix product C = R * A * R^T 9375 9376 Neighbor-wise Collective on Mat 9377 9378 Input Parameters: 9379 + A - the matrix 9380 . R - the projection matrix 9381 . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 9382 - fill - expected fill as ratio of nnz(C)/nnz(A), use PETSC_DEFAULT if you do not have a good estimate 9383 if the result is a dense matrix this is irrelevent 9384 9385 Output Parameters: 9386 . C - the product matrix 9387 9388 Notes: 9389 C will be created and must be destroyed by the user with MatDestroy(). 9390 9391 This routine is currently only implemented for pairs of AIJ matrices and classes 9392 which inherit from AIJ. Due to PETSc sparse matrix block row distribution among processes, 9393 parallel MatRARt is implemented via explicit transpose of R, which could be very expensive. 9394 We recommend using MatPtAP(). 9395 9396 Level: intermediate 9397 9398 .seealso: MatRARtSymbolic(), MatRARtNumeric(), MatMatMult(), MatPtAP() 9399 @*/ 9400 PetscErrorCode MatRARt(Mat A,Mat R,MatReuse scall,PetscReal fill,Mat *C) 9401 { 9402 PetscErrorCode ierr; 9403 9404 PetscFunctionBegin; 9405 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 9406 PetscValidType(A,1); 9407 if (scall == MAT_INPLACE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported"); 9408 if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9409 if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9410 PetscValidHeaderSpecific(R,MAT_CLASSID,2); 9411 PetscValidType(R,2); 9412 MatCheckPreallocated(R,2); 9413 if (!R->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9414 if (R->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9415 PetscValidPointer(C,3); 9416 if (R->cmap->N!=A->rmap->N) SETERRQ2(PetscObjectComm((PetscObject)R),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",R->cmap->N,A->rmap->N); 9417 9418 if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0; 9419 if (fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Expected fill=%g must be >= 1.0",(double)fill); 9420 MatCheckPreallocated(A,1); 9421 9422 if (!A->ops->rart) { 9423 Mat Rt; 9424 ierr = MatTranspose(R,MAT_INITIAL_MATRIX,&Rt);CHKERRQ(ierr); 9425 ierr = MatMatMatMult(R,A,Rt,scall,fill,C);CHKERRQ(ierr); 9426 ierr = MatDestroy(&Rt);CHKERRQ(ierr); 9427 PetscFunctionReturn(0); 9428 } 9429 ierr = PetscLogEventBegin(MAT_RARt,A,R,0,0);CHKERRQ(ierr); 9430 ierr = (*A->ops->rart)(A,R,scall,fill,C);CHKERRQ(ierr); 9431 ierr = PetscLogEventEnd(MAT_RARt,A,R,0,0);CHKERRQ(ierr); 9432 PetscFunctionReturn(0); 9433 } 9434 9435 /*@ 9436 MatRARtNumeric - Computes the matrix product C = R * A * R^T 9437 9438 Neighbor-wise Collective on Mat 9439 9440 Input Parameters: 9441 + A - the matrix 9442 - R - the projection matrix 9443 9444 Output Parameters: 9445 . C - the product matrix 9446 9447 Notes: 9448 C must have been created by calling MatRARtSymbolic and must be destroyed by 9449 the user using MatDestroy(). 9450 9451 This routine is currently only implemented for pairs of AIJ matrices and classes 9452 which inherit from AIJ. C will be of type MATAIJ. 9453 9454 Level: intermediate 9455 9456 .seealso: MatRARt(), MatRARtSymbolic(), MatMatMultNumeric() 9457 @*/ 9458 PetscErrorCode MatRARtNumeric(Mat A,Mat R,Mat C) 9459 { 9460 PetscErrorCode ierr; 9461 9462 PetscFunctionBegin; 9463 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 9464 PetscValidType(A,1); 9465 if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9466 if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9467 PetscValidHeaderSpecific(R,MAT_CLASSID,2); 9468 PetscValidType(R,2); 9469 MatCheckPreallocated(R,2); 9470 if (!R->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9471 if (R->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9472 PetscValidHeaderSpecific(C,MAT_CLASSID,3); 9473 PetscValidType(C,3); 9474 MatCheckPreallocated(C,3); 9475 if (C->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9476 if (R->rmap->N!=C->rmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",R->rmap->N,C->rmap->N); 9477 if (R->cmap->N!=A->rmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",R->cmap->N,A->rmap->N); 9478 if (A->rmap->N!=A->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix 'A' must be square, %D != %D",A->rmap->N,A->cmap->N); 9479 if (R->rmap->N!=C->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",R->rmap->N,C->cmap->N); 9480 MatCheckPreallocated(A,1); 9481 9482 ierr = PetscLogEventBegin(MAT_RARtNumeric,A,R,0,0);CHKERRQ(ierr); 9483 ierr = (*A->ops->rartnumeric)(A,R,C);CHKERRQ(ierr); 9484 ierr = PetscLogEventEnd(MAT_RARtNumeric,A,R,0,0);CHKERRQ(ierr); 9485 PetscFunctionReturn(0); 9486 } 9487 9488 /*@ 9489 MatRARtSymbolic - Creates the (i,j) structure of the matrix product C = R * A * R^T 9490 9491 Neighbor-wise Collective on Mat 9492 9493 Input Parameters: 9494 + A - the matrix 9495 - R - the projection matrix 9496 9497 Output Parameters: 9498 . C - the (i,j) structure of the product matrix 9499 9500 Notes: 9501 C will be created and must be destroyed by the user with MatDestroy(). 9502 9503 This routine is currently only implemented for pairs of SeqAIJ matrices and classes 9504 which inherit from SeqAIJ. C will be of type MATSEQAIJ. The product is computed using 9505 this (i,j) structure by calling MatRARtNumeric(). 9506 9507 Level: intermediate 9508 9509 .seealso: MatRARt(), MatRARtNumeric(), MatMatMultSymbolic() 9510 @*/ 9511 PetscErrorCode MatRARtSymbolic(Mat A,Mat R,PetscReal fill,Mat *C) 9512 { 9513 PetscErrorCode ierr; 9514 9515 PetscFunctionBegin; 9516 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 9517 PetscValidType(A,1); 9518 if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9519 if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9520 if (fill <1.0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Expected fill=%g must be >= 1.0",(double)fill); 9521 PetscValidHeaderSpecific(R,MAT_CLASSID,2); 9522 PetscValidType(R,2); 9523 MatCheckPreallocated(R,2); 9524 if (!R->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9525 if (R->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9526 PetscValidPointer(C,3); 9527 9528 if (R->cmap->N!=A->rmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",R->cmap->N,A->rmap->N); 9529 if (A->rmap->N!=A->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix 'A' must be square, %D != %D",A->rmap->N,A->cmap->N); 9530 MatCheckPreallocated(A,1); 9531 ierr = PetscLogEventBegin(MAT_RARtSymbolic,A,R,0,0);CHKERRQ(ierr); 9532 ierr = (*A->ops->rartsymbolic)(A,R,fill,C);CHKERRQ(ierr); 9533 ierr = PetscLogEventEnd(MAT_RARtSymbolic,A,R,0,0);CHKERRQ(ierr); 9534 9535 ierr = MatSetBlockSizes(*C,PetscAbs(R->rmap->bs),PetscAbs(R->rmap->bs));CHKERRQ(ierr); 9536 PetscFunctionReturn(0); 9537 } 9538 9539 /*@ 9540 MatMatMult - Performs Matrix-Matrix Multiplication C=A*B. 9541 9542 Neighbor-wise Collective on Mat 9543 9544 Input Parameters: 9545 + A - the left matrix 9546 . B - the right matrix 9547 . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 9548 - fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if you do not have a good estimate 9549 if the result is a dense matrix this is irrelevent 9550 9551 Output Parameters: 9552 . C - the product matrix 9553 9554 Notes: 9555 Unless scall is MAT_REUSE_MATRIX C will be created. 9556 9557 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 9558 call to this function with either MAT_INITIAL_MATRIX or MatMatMultSymbolic() 9559 9560 To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value 9561 actually needed. 9562 9563 If you have many matrices with the same non-zero structure to multiply, you 9564 should either 9565 $ 1) use MAT_REUSE_MATRIX in all calls but the first or 9566 $ 2) call MatMatMultSymbolic() once and then MatMatMultNumeric() for each product needed 9567 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 9568 with MAT_REUSE_MATRIX, rather than first having MatMatMult() create it for you. You can NEVER do this if the matrix C is sparse. 9569 9570 Level: intermediate 9571 9572 .seealso: MatMatMultSymbolic(), MatMatMultNumeric(), MatTransposeMatMult(), MatMatTransposeMult(), MatPtAP() 9573 @*/ 9574 PetscErrorCode MatMatMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C) 9575 { 9576 PetscErrorCode ierr; 9577 PetscErrorCode (*fA)(Mat,Mat,MatReuse,PetscReal,Mat*); 9578 PetscErrorCode (*fB)(Mat,Mat,MatReuse,PetscReal,Mat*); 9579 PetscErrorCode (*mult)(Mat,Mat,MatReuse,PetscReal,Mat*)=NULL; 9580 Mat T; 9581 PetscBool istrans; 9582 9583 PetscFunctionBegin; 9584 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 9585 PetscValidType(A,1); 9586 MatCheckPreallocated(A,1); 9587 if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9588 if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9589 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 9590 PetscValidType(B,2); 9591 MatCheckPreallocated(B,2); 9592 if (!B->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9593 if (B->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9594 PetscValidPointer(C,3); 9595 if (scall == MAT_INPLACE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported"); 9596 if (B->rmap->N!=A->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",B->rmap->N,A->cmap->N); 9597 ierr = PetscObjectTypeCompare((PetscObject)A,MATTRANSPOSEMAT,&istrans);CHKERRQ(ierr); 9598 if (istrans) { 9599 ierr = MatTransposeGetMat(A,&T);CHKERRQ(ierr); 9600 ierr = MatTransposeMatMult(T,B,scall,fill,C);CHKERRQ(ierr); 9601 PetscFunctionReturn(0); 9602 } else { 9603 ierr = PetscObjectTypeCompare((PetscObject)B,MATTRANSPOSEMAT,&istrans);CHKERRQ(ierr); 9604 if (istrans) { 9605 ierr = MatTransposeGetMat(B,&T);CHKERRQ(ierr); 9606 ierr = MatMatTransposeMult(A,T,scall,fill,C);CHKERRQ(ierr); 9607 PetscFunctionReturn(0); 9608 } 9609 } 9610 if (scall == MAT_REUSE_MATRIX) { 9611 PetscValidPointer(*C,5); 9612 PetscValidHeaderSpecific(*C,MAT_CLASSID,5); 9613 ierr = PetscLogEventBegin(MAT_MatMult,A,B,0,0);CHKERRQ(ierr); 9614 ierr = PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr); 9615 ierr = (*(*C)->ops->matmultnumeric)(A,B,*C);CHKERRQ(ierr); 9616 ierr = PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr); 9617 ierr = PetscLogEventEnd(MAT_MatMult,A,B,0,0);CHKERRQ(ierr); 9618 PetscFunctionReturn(0); 9619 } 9620 if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0; 9621 if (fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Expected fill=%g must be >= 1.0",(double)fill); 9622 9623 fA = A->ops->matmult; 9624 fB = B->ops->matmult; 9625 if (fB == fA) { 9626 if (!fB) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"MatMatMult not supported for B of type %s",((PetscObject)B)->type_name); 9627 mult = fB; 9628 } else { 9629 /* dispatch based on the type of A and B from their PetscObject's PetscFunctionLists. */ 9630 char multname[256]; 9631 ierr = PetscStrncpy(multname,"MatMatMult_",sizeof(multname));CHKERRQ(ierr); 9632 ierr = PetscStrlcat(multname,((PetscObject)A)->type_name,sizeof(multname));CHKERRQ(ierr); 9633 ierr = PetscStrlcat(multname,"_",sizeof(multname));CHKERRQ(ierr); 9634 ierr = PetscStrlcat(multname,((PetscObject)B)->type_name,sizeof(multname));CHKERRQ(ierr); 9635 ierr = PetscStrlcat(multname,"_C",sizeof(multname));CHKERRQ(ierr); /* e.g., multname = "MatMatMult_seqdense_seqaij_C" */ 9636 ierr = PetscObjectQueryFunction((PetscObject)B,multname,&mult);CHKERRQ(ierr); 9637 if (!mult) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_INCOMP,"MatMatMult requires A, %s, to be compatible with B, %s",((PetscObject)A)->type_name,((PetscObject)B)->type_name); 9638 } 9639 ierr = PetscLogEventBegin(MAT_MatMult,A,B,0,0);CHKERRQ(ierr); 9640 ierr = (*mult)(A,B,scall,fill,C);CHKERRQ(ierr); 9641 ierr = PetscLogEventEnd(MAT_MatMult,A,B,0,0);CHKERRQ(ierr); 9642 PetscFunctionReturn(0); 9643 } 9644 9645 /*@ 9646 MatMatMultSymbolic - Performs construction, preallocation, and computes the ij structure 9647 of the matrix-matrix product C=A*B. Call this routine before calling MatMatMultNumeric(). 9648 9649 Neighbor-wise Collective on Mat 9650 9651 Input Parameters: 9652 + A - the left matrix 9653 . B - the right matrix 9654 - fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if you do not have a good estimate, 9655 if C is a dense matrix this is irrelevent 9656 9657 Output Parameters: 9658 . C - the product matrix 9659 9660 Notes: 9661 To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value 9662 actually needed. 9663 9664 This routine is currently implemented for 9665 - pairs of AIJ matrices and classes which inherit from AIJ, C will be of type AIJ 9666 - pairs of AIJ (A) and Dense (B) matrix, C will be of type Dense. 9667 - pairs of Dense (A) and AIJ (B) matrix, C will be of type Dense. 9668 9669 Level: intermediate 9670 9671 Developers Note: There are ways to estimate the number of nonzeros in the resulting product, see for example, https://arxiv.org/abs/1006.4173 9672 We should incorporate them into PETSc. 9673 9674 .seealso: MatMatMult(), MatMatMultNumeric() 9675 @*/ 9676 PetscErrorCode MatMatMultSymbolic(Mat A,Mat B,PetscReal fill,Mat *C) 9677 { 9678 PetscErrorCode ierr; 9679 PetscErrorCode (*Asymbolic)(Mat,Mat,PetscReal,Mat*); 9680 PetscErrorCode (*Bsymbolic)(Mat,Mat,PetscReal,Mat*); 9681 PetscErrorCode (*symbolic)(Mat,Mat,PetscReal,Mat*)=NULL; 9682 9683 PetscFunctionBegin; 9684 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 9685 PetscValidType(A,1); 9686 if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9687 if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9688 9689 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 9690 PetscValidType(B,2); 9691 MatCheckPreallocated(B,2); 9692 if (!B->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9693 if (B->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9694 PetscValidPointer(C,3); 9695 9696 if (B->rmap->N!=A->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",B->rmap->N,A->cmap->N); 9697 if (fill == PETSC_DEFAULT) fill = 2.0; 9698 if (fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Expected fill=%g must be > 1.0",(double)fill); 9699 MatCheckPreallocated(A,1); 9700 9701 Asymbolic = A->ops->matmultsymbolic; 9702 Bsymbolic = B->ops->matmultsymbolic; 9703 if (Asymbolic == Bsymbolic) { 9704 if (!Bsymbolic) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"C=A*B not implemented for B of type %s",((PetscObject)B)->type_name); 9705 symbolic = Bsymbolic; 9706 } else { /* dispatch based on the type of A and B */ 9707 char symbolicname[256]; 9708 ierr = PetscStrncpy(symbolicname,"MatMatMultSymbolic_",sizeof(symbolicname));CHKERRQ(ierr); 9709 ierr = PetscStrlcat(symbolicname,((PetscObject)A)->type_name,sizeof(symbolicname));CHKERRQ(ierr); 9710 ierr = PetscStrlcat(symbolicname,"_",sizeof(symbolicname));CHKERRQ(ierr); 9711 ierr = PetscStrlcat(symbolicname,((PetscObject)B)->type_name,sizeof(symbolicname));CHKERRQ(ierr); 9712 ierr = PetscStrlcat(symbolicname,"_C",sizeof(symbolicname));CHKERRQ(ierr); 9713 ierr = PetscObjectQueryFunction((PetscObject)B,symbolicname,&symbolic);CHKERRQ(ierr); 9714 if (!symbolic) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_INCOMP,"MatMatMultSymbolic requires A, %s, to be compatible with B, %s",((PetscObject)A)->type_name,((PetscObject)B)->type_name); 9715 } 9716 ierr = PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr); 9717 ierr = (*symbolic)(A,B,fill,C);CHKERRQ(ierr); 9718 ierr = PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr); 9719 PetscFunctionReturn(0); 9720 } 9721 9722 /*@ 9723 MatMatMultNumeric - Performs the numeric matrix-matrix product. 9724 Call this routine after first calling MatMatMultSymbolic(). 9725 9726 Neighbor-wise Collective on Mat 9727 9728 Input Parameters: 9729 + A - the left matrix 9730 - B - the right matrix 9731 9732 Output Parameters: 9733 . C - the product matrix, which was created by from MatMatMultSymbolic() or a call to MatMatMult(). 9734 9735 Notes: 9736 C must have been created with MatMatMultSymbolic(). 9737 9738 This routine is currently implemented for 9739 - pairs of AIJ matrices and classes which inherit from AIJ, C will be of type MATAIJ. 9740 - pairs of AIJ (A) and Dense (B) matrix, C will be of type Dense. 9741 - pairs of Dense (A) and AIJ (B) matrix, C will be of type Dense. 9742 9743 Level: intermediate 9744 9745 .seealso: MatMatMult(), MatMatMultSymbolic() 9746 @*/ 9747 PetscErrorCode MatMatMultNumeric(Mat A,Mat B,Mat C) 9748 { 9749 PetscErrorCode ierr; 9750 9751 PetscFunctionBegin; 9752 ierr = MatMatMult(A,B,MAT_REUSE_MATRIX,0.0,&C);CHKERRQ(ierr); 9753 PetscFunctionReturn(0); 9754 } 9755 9756 /*@ 9757 MatMatTransposeMult - Performs Matrix-Matrix Multiplication C=A*B^T. 9758 9759 Neighbor-wise Collective on Mat 9760 9761 Input Parameters: 9762 + A - the left matrix 9763 . B - the right matrix 9764 . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 9765 - fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if not known 9766 9767 Output Parameters: 9768 . C - the product matrix 9769 9770 Notes: 9771 C will be created if MAT_INITIAL_MATRIX and must be destroyed by the user with MatDestroy(). 9772 9773 MAT_REUSE_MATRIX can only be used if the matrices A and B have the same nonzero pattern as in the previous call 9774 9775 To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value 9776 actually needed. 9777 9778 This routine is currently only implemented for pairs of SeqAIJ matrices, for the SeqDense class, 9779 and for pairs of MPIDense matrices. 9780 9781 Options Database Keys: 9782 . -matmattransmult_mpidense_mpidense_via {allgatherv,cyclic} - Choose between algorthims for MPIDense matrices: the 9783 first redundantly copies the transposed B matrix on each process and requiers O(log P) communication complexity; 9784 the second never stores more than one portion of the B matrix at a time by requires O(P) communication complexity. 9785 9786 Level: intermediate 9787 9788 .seealso: MatMatTransposeMultSymbolic(), MatMatTransposeMultNumeric(), MatMatMult(), MatTransposeMatMult() MatPtAP() 9789 @*/ 9790 PetscErrorCode MatMatTransposeMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C) 9791 { 9792 PetscErrorCode ierr; 9793 PetscErrorCode (*fA)(Mat,Mat,MatReuse,PetscReal,Mat*); 9794 PetscErrorCode (*fB)(Mat,Mat,MatReuse,PetscReal,Mat*); 9795 Mat T; 9796 PetscBool istrans; 9797 9798 PetscFunctionBegin; 9799 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 9800 PetscValidType(A,1); 9801 if (scall == MAT_INPLACE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported"); 9802 if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9803 if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9804 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 9805 PetscValidType(B,2); 9806 MatCheckPreallocated(B,2); 9807 if (!B->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9808 if (B->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9809 PetscValidPointer(C,3); 9810 if (B->cmap->N!=A->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, AN %D != BN %D",A->cmap->N,B->cmap->N); 9811 if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0; 9812 if (fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Expected fill=%g must be > 1.0",(double)fill); 9813 MatCheckPreallocated(A,1); 9814 9815 ierr = PetscObjectTypeCompare((PetscObject)B,MATTRANSPOSEMAT,&istrans);CHKERRQ(ierr); 9816 if (istrans) { 9817 ierr = MatTransposeGetMat(B,&T);CHKERRQ(ierr); 9818 ierr = MatMatMult(A,T,scall,fill,C);CHKERRQ(ierr); 9819 PetscFunctionReturn(0); 9820 } 9821 fA = A->ops->mattransposemult; 9822 if (!fA) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"MatMatTransposeMult not supported for A of type %s",((PetscObject)A)->type_name); 9823 fB = B->ops->mattransposemult; 9824 if (!fB) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"MatMatTransposeMult not supported for B of type %s",((PetscObject)B)->type_name); 9825 if (fB!=fA) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_INCOMP,"MatMatTransposeMult requires A, %s, to be compatible with B, %s",((PetscObject)A)->type_name,((PetscObject)B)->type_name); 9826 9827 ierr = PetscLogEventBegin(MAT_MatTransposeMult,A,B,0,0);CHKERRQ(ierr); 9828 if (scall == MAT_INITIAL_MATRIX) { 9829 ierr = PetscLogEventBegin(MAT_MatTransposeMultSymbolic,A,B,0,0);CHKERRQ(ierr); 9830 ierr = (*A->ops->mattransposemultsymbolic)(A,B,fill,C);CHKERRQ(ierr); 9831 ierr = PetscLogEventEnd(MAT_MatTransposeMultSymbolic,A,B,0,0);CHKERRQ(ierr); 9832 } 9833 ierr = PetscLogEventBegin(MAT_MatTransposeMultNumeric,A,B,0,0);CHKERRQ(ierr); 9834 ierr = (*A->ops->mattransposemultnumeric)(A,B,*C);CHKERRQ(ierr); 9835 ierr = PetscLogEventEnd(MAT_MatTransposeMultNumeric,A,B,0,0);CHKERRQ(ierr); 9836 ierr = PetscLogEventEnd(MAT_MatTransposeMult,A,B,0,0);CHKERRQ(ierr); 9837 PetscFunctionReturn(0); 9838 } 9839 9840 /*@ 9841 MatTransposeMatMult - Performs Matrix-Matrix Multiplication C=A^T*B. 9842 9843 Neighbor-wise Collective on Mat 9844 9845 Input Parameters: 9846 + A - the left matrix 9847 . B - the right matrix 9848 . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 9849 - fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if not known 9850 9851 Output Parameters: 9852 . C - the product matrix 9853 9854 Notes: 9855 C will be created if MAT_INITIAL_MATRIX and must be destroyed by the user with MatDestroy(). 9856 9857 MAT_REUSE_MATRIX can only be used if the matrices A and B have the same nonzero pattern as in the previous call 9858 9859 To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value 9860 actually needed. 9861 9862 This routine is currently implemented for pairs of AIJ matrices and pairs of SeqDense matrices and classes 9863 which inherit from SeqAIJ. C will be of same type as the input matrices. 9864 9865 Level: intermediate 9866 9867 .seealso: MatTransposeMatMultSymbolic(), MatTransposeMatMultNumeric(), MatMatMult(), MatMatTransposeMult(), MatPtAP() 9868 @*/ 9869 PetscErrorCode MatTransposeMatMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C) 9870 { 9871 PetscErrorCode ierr; 9872 PetscErrorCode (*fA)(Mat,Mat,MatReuse,PetscReal,Mat*); 9873 PetscErrorCode (*fB)(Mat,Mat,MatReuse,PetscReal,Mat*); 9874 PetscErrorCode (*transposematmult)(Mat,Mat,MatReuse,PetscReal,Mat*) = NULL; 9875 Mat T; 9876 PetscBool istrans; 9877 9878 PetscFunctionBegin; 9879 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 9880 PetscValidType(A,1); 9881 if (scall == MAT_INPLACE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported"); 9882 if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9883 if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9884 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 9885 PetscValidType(B,2); 9886 MatCheckPreallocated(B,2); 9887 if (!B->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9888 if (B->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9889 PetscValidPointer(C,3); 9890 if (B->rmap->N!=A->rmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",B->rmap->N,A->rmap->N); 9891 if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0; 9892 if (fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Expected fill=%g must be > 1.0",(double)fill); 9893 MatCheckPreallocated(A,1); 9894 9895 ierr = PetscObjectTypeCompare((PetscObject)A,MATTRANSPOSEMAT,&istrans);CHKERRQ(ierr); 9896 if (istrans) { 9897 ierr = MatTransposeGetMat(A,&T);CHKERRQ(ierr); 9898 ierr = MatMatMult(T,B,scall,fill,C);CHKERRQ(ierr); 9899 PetscFunctionReturn(0); 9900 } 9901 fA = A->ops->transposematmult; 9902 fB = B->ops->transposematmult; 9903 if (fB==fA) { 9904 if (!fA) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"MatTransposeMatMult not supported for A of type %s",((PetscObject)A)->type_name); 9905 transposematmult = fA; 9906 } else { 9907 /* dispatch based on the type of A and B from their PetscObject's PetscFunctionLists. */ 9908 char multname[256]; 9909 ierr = PetscStrncpy(multname,"MatTransposeMatMult_",sizeof(multname));CHKERRQ(ierr); 9910 ierr = PetscStrlcat(multname,((PetscObject)A)->type_name,sizeof(multname));CHKERRQ(ierr); 9911 ierr = PetscStrlcat(multname,"_",sizeof(multname));CHKERRQ(ierr); 9912 ierr = PetscStrlcat(multname,((PetscObject)B)->type_name,sizeof(multname));CHKERRQ(ierr); 9913 ierr = PetscStrlcat(multname,"_C",sizeof(multname));CHKERRQ(ierr); /* e.g., multname = "MatMatMult_seqdense_seqaij_C" */ 9914 ierr = PetscObjectQueryFunction((PetscObject)B,multname,&transposematmult);CHKERRQ(ierr); 9915 if (!transposematmult) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_INCOMP,"MatTransposeMatMult requires A, %s, to be compatible with B, %s",((PetscObject)A)->type_name,((PetscObject)B)->type_name); 9916 } 9917 ierr = PetscLogEventBegin(MAT_TransposeMatMult,A,B,0,0);CHKERRQ(ierr); 9918 ierr = (*transposematmult)(A,B,scall,fill,C);CHKERRQ(ierr); 9919 ierr = PetscLogEventEnd(MAT_TransposeMatMult,A,B,0,0);CHKERRQ(ierr); 9920 PetscFunctionReturn(0); 9921 } 9922 9923 /*@ 9924 MatMatMatMult - Performs Matrix-Matrix-Matrix Multiplication D=A*B*C. 9925 9926 Neighbor-wise Collective on Mat 9927 9928 Input Parameters: 9929 + A - the left matrix 9930 . B - the middle matrix 9931 . C - the right matrix 9932 . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 9933 - 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 9934 if the result is a dense matrix this is irrelevent 9935 9936 Output Parameters: 9937 . D - the product matrix 9938 9939 Notes: 9940 Unless scall is MAT_REUSE_MATRIX D will be created. 9941 9942 MAT_REUSE_MATRIX can only be used if the matrices A, B and C have the same nonzero pattern as in the previous call 9943 9944 To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value 9945 actually needed. 9946 9947 If you have many matrices with the same non-zero structure to multiply, you 9948 should use MAT_REUSE_MATRIX in all calls but the first or 9949 9950 Level: intermediate 9951 9952 .seealso: MatMatMult, MatPtAP() 9953 @*/ 9954 PetscErrorCode MatMatMatMult(Mat A,Mat B,Mat C,MatReuse scall,PetscReal fill,Mat *D) 9955 { 9956 PetscErrorCode ierr; 9957 PetscErrorCode (*fA)(Mat,Mat,Mat,MatReuse,PetscReal,Mat*); 9958 PetscErrorCode (*fB)(Mat,Mat,Mat,MatReuse,PetscReal,Mat*); 9959 PetscErrorCode (*fC)(Mat,Mat,Mat,MatReuse,PetscReal,Mat*); 9960 PetscErrorCode (*mult)(Mat,Mat,Mat,MatReuse,PetscReal,Mat*)=NULL; 9961 9962 PetscFunctionBegin; 9963 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 9964 PetscValidType(A,1); 9965 MatCheckPreallocated(A,1); 9966 if (scall == MAT_INPLACE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported"); 9967 if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9968 if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9969 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 9970 PetscValidType(B,2); 9971 MatCheckPreallocated(B,2); 9972 if (!B->assembled) SETERRQ(PetscObjectComm((PetscObject)B),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9973 if (B->factortype) SETERRQ(PetscObjectComm((PetscObject)B),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9974 PetscValidHeaderSpecific(C,MAT_CLASSID,3); 9975 PetscValidPointer(C,3); 9976 MatCheckPreallocated(C,3); 9977 if (!C->assembled) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9978 if (C->factortype) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9979 if (B->rmap->N!=A->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)B),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",B->rmap->N,A->cmap->N); 9980 if (C->rmap->N!=B->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)C),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",C->rmap->N,B->cmap->N); 9981 if (scall == MAT_REUSE_MATRIX) { 9982 PetscValidPointer(*D,6); 9983 PetscValidHeaderSpecific(*D,MAT_CLASSID,6); 9984 ierr = PetscLogEventBegin(MAT_MatMatMult,A,B,0,0);CHKERRQ(ierr); 9985 ierr = (*(*D)->ops->matmatmult)(A,B,C,scall,fill,D);CHKERRQ(ierr); 9986 ierr = PetscLogEventEnd(MAT_MatMatMult,A,B,0,0);CHKERRQ(ierr); 9987 PetscFunctionReturn(0); 9988 } 9989 if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0; 9990 if (fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Expected fill=%g must be >= 1.0",(double)fill); 9991 9992 fA = A->ops->matmatmult; 9993 fB = B->ops->matmatmult; 9994 fC = C->ops->matmatmult; 9995 if (fA == fB && fA == fC) { 9996 if (!fA) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"MatMatMatMult not supported for A of type %s",((PetscObject)A)->type_name); 9997 mult = fA; 9998 } else { 9999 /* dispatch based on the type of A, B and C from their PetscObject's PetscFunctionLists. */ 10000 char multname[256]; 10001 ierr = PetscStrncpy(multname,"MatMatMatMult_",sizeof(multname));CHKERRQ(ierr); 10002 ierr = PetscStrlcat(multname,((PetscObject)A)->type_name,sizeof(multname));CHKERRQ(ierr); 10003 ierr = PetscStrlcat(multname,"_",sizeof(multname));CHKERRQ(ierr); 10004 ierr = PetscStrlcat(multname,((PetscObject)B)->type_name,sizeof(multname));CHKERRQ(ierr); 10005 ierr = PetscStrlcat(multname,"_",sizeof(multname));CHKERRQ(ierr); 10006 ierr = PetscStrlcat(multname,((PetscObject)C)->type_name,sizeof(multname));CHKERRQ(ierr); 10007 ierr = PetscStrlcat(multname,"_C",sizeof(multname));CHKERRQ(ierr); 10008 ierr = PetscObjectQueryFunction((PetscObject)B,multname,&mult);CHKERRQ(ierr); 10009 if (!mult) SETERRQ3(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_INCOMP,"MatMatMatMult requires A, %s, to be compatible with B, %s, C, %s",((PetscObject)A)->type_name,((PetscObject)B)->type_name,((PetscObject)C)->type_name); 10010 } 10011 ierr = PetscLogEventBegin(MAT_MatMatMult,A,B,0,0);CHKERRQ(ierr); 10012 ierr = (*mult)(A,B,C,scall,fill,D);CHKERRQ(ierr); 10013 ierr = PetscLogEventEnd(MAT_MatMatMult,A,B,0,0);CHKERRQ(ierr); 10014 PetscFunctionReturn(0); 10015 } 10016 10017 /*@ 10018 MatCreateRedundantMatrix - Create redundant matrices and put them into processors of subcommunicators. 10019 10020 Collective on Mat 10021 10022 Input Parameters: 10023 + mat - the matrix 10024 . nsubcomm - the number of subcommunicators (= number of redundant parallel or sequential matrices) 10025 . subcomm - MPI communicator split from the communicator where mat resides in (or MPI_COMM_NULL if nsubcomm is used) 10026 - reuse - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 10027 10028 Output Parameter: 10029 . matredundant - redundant matrix 10030 10031 Notes: 10032 MAT_REUSE_MATRIX can only be used when the nonzero structure of the 10033 original matrix has not changed from that last call to MatCreateRedundantMatrix(). 10034 10035 This routine creates the duplicated matrices in subcommunicators; you should NOT create them before 10036 calling it. 10037 10038 Level: advanced 10039 10040 10041 .seealso: MatDestroy() 10042 @*/ 10043 PetscErrorCode MatCreateRedundantMatrix(Mat mat,PetscInt nsubcomm,MPI_Comm subcomm,MatReuse reuse,Mat *matredundant) 10044 { 10045 PetscErrorCode ierr; 10046 MPI_Comm comm; 10047 PetscMPIInt size; 10048 PetscInt mloc_sub,nloc_sub,rstart,rend,M=mat->rmap->N,N=mat->cmap->N,bs=mat->rmap->bs; 10049 Mat_Redundant *redund=NULL; 10050 PetscSubcomm psubcomm=NULL; 10051 MPI_Comm subcomm_in=subcomm; 10052 Mat *matseq; 10053 IS isrow,iscol; 10054 PetscBool newsubcomm=PETSC_FALSE; 10055 10056 PetscFunctionBegin; 10057 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10058 if (nsubcomm && reuse == MAT_REUSE_MATRIX) { 10059 PetscValidPointer(*matredundant,5); 10060 PetscValidHeaderSpecific(*matredundant,MAT_CLASSID,5); 10061 } 10062 10063 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);CHKERRQ(ierr); 10064 if (size == 1 || nsubcomm == 1) { 10065 if (reuse == MAT_INITIAL_MATRIX) { 10066 ierr = MatDuplicate(mat,MAT_COPY_VALUES,matredundant);CHKERRQ(ierr); 10067 } else { 10068 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"); 10069 ierr = MatCopy(mat,*matredundant,SAME_NONZERO_PATTERN);CHKERRQ(ierr); 10070 } 10071 PetscFunctionReturn(0); 10072 } 10073 10074 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 10075 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 10076 MatCheckPreallocated(mat,1); 10077 10078 ierr = PetscLogEventBegin(MAT_RedundantMat,mat,0,0,0);CHKERRQ(ierr); 10079 if (subcomm_in == MPI_COMM_NULL && reuse == MAT_INITIAL_MATRIX) { /* get subcomm if user does not provide subcomm */ 10080 /* create psubcomm, then get subcomm */ 10081 ierr = PetscObjectGetComm((PetscObject)mat,&comm);CHKERRQ(ierr); 10082 ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 10083 if (nsubcomm < 1 || nsubcomm > size) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"nsubcomm must between 1 and %D",size); 10084 10085 ierr = PetscSubcommCreate(comm,&psubcomm);CHKERRQ(ierr); 10086 ierr = PetscSubcommSetNumber(psubcomm,nsubcomm);CHKERRQ(ierr); 10087 ierr = PetscSubcommSetType(psubcomm,PETSC_SUBCOMM_CONTIGUOUS);CHKERRQ(ierr); 10088 ierr = PetscSubcommSetFromOptions(psubcomm);CHKERRQ(ierr); 10089 ierr = PetscCommDuplicate(PetscSubcommChild(psubcomm),&subcomm,NULL);CHKERRQ(ierr); 10090 newsubcomm = PETSC_TRUE; 10091 ierr = PetscSubcommDestroy(&psubcomm);CHKERRQ(ierr); 10092 } 10093 10094 /* get isrow, iscol and a local sequential matrix matseq[0] */ 10095 if (reuse == MAT_INITIAL_MATRIX) { 10096 mloc_sub = PETSC_DECIDE; 10097 nloc_sub = PETSC_DECIDE; 10098 if (bs < 1) { 10099 ierr = PetscSplitOwnership(subcomm,&mloc_sub,&M);CHKERRQ(ierr); 10100 ierr = PetscSplitOwnership(subcomm,&nloc_sub,&N);CHKERRQ(ierr); 10101 } else { 10102 ierr = PetscSplitOwnershipBlock(subcomm,bs,&mloc_sub,&M);CHKERRQ(ierr); 10103 ierr = PetscSplitOwnershipBlock(subcomm,bs,&nloc_sub,&N);CHKERRQ(ierr); 10104 } 10105 ierr = MPI_Scan(&mloc_sub,&rend,1,MPIU_INT,MPI_SUM,subcomm);CHKERRQ(ierr); 10106 rstart = rend - mloc_sub; 10107 ierr = ISCreateStride(PETSC_COMM_SELF,mloc_sub,rstart,1,&isrow);CHKERRQ(ierr); 10108 ierr = ISCreateStride(PETSC_COMM_SELF,N,0,1,&iscol);CHKERRQ(ierr); 10109 } else { /* reuse == MAT_REUSE_MATRIX */ 10110 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"); 10111 /* retrieve subcomm */ 10112 ierr = PetscObjectGetComm((PetscObject)(*matredundant),&subcomm);CHKERRQ(ierr); 10113 redund = (*matredundant)->redundant; 10114 isrow = redund->isrow; 10115 iscol = redund->iscol; 10116 matseq = redund->matseq; 10117 } 10118 ierr = MatCreateSubMatrices(mat,1,&isrow,&iscol,reuse,&matseq);CHKERRQ(ierr); 10119 10120 /* get matredundant over subcomm */ 10121 if (reuse == MAT_INITIAL_MATRIX) { 10122 ierr = MatCreateMPIMatConcatenateSeqMat(subcomm,matseq[0],nloc_sub,reuse,matredundant);CHKERRQ(ierr); 10123 10124 /* create a supporting struct and attach it to C for reuse */ 10125 ierr = PetscNewLog(*matredundant,&redund);CHKERRQ(ierr); 10126 (*matredundant)->redundant = redund; 10127 redund->isrow = isrow; 10128 redund->iscol = iscol; 10129 redund->matseq = matseq; 10130 if (newsubcomm) { 10131 redund->subcomm = subcomm; 10132 } else { 10133 redund->subcomm = MPI_COMM_NULL; 10134 } 10135 } else { 10136 ierr = MatCreateMPIMatConcatenateSeqMat(subcomm,matseq[0],PETSC_DECIDE,reuse,matredundant);CHKERRQ(ierr); 10137 } 10138 ierr = PetscLogEventEnd(MAT_RedundantMat,mat,0,0,0);CHKERRQ(ierr); 10139 PetscFunctionReturn(0); 10140 } 10141 10142 /*@C 10143 MatGetMultiProcBlock - Create multiple [bjacobi] 'parallel submatrices' from 10144 a given 'mat' object. Each submatrix can span multiple procs. 10145 10146 Collective on Mat 10147 10148 Input Parameters: 10149 + mat - the matrix 10150 . subcomm - the subcommunicator obtained by com_split(comm) 10151 - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 10152 10153 Output Parameter: 10154 . subMat - 'parallel submatrices each spans a given subcomm 10155 10156 Notes: 10157 The submatrix partition across processors is dictated by 'subComm' a 10158 communicator obtained by com_split(comm). The comm_split 10159 is not restriced to be grouped with consecutive original ranks. 10160 10161 Due the comm_split() usage, the parallel layout of the submatrices 10162 map directly to the layout of the original matrix [wrt the local 10163 row,col partitioning]. So the original 'DiagonalMat' naturally maps 10164 into the 'DiagonalMat' of the subMat, hence it is used directly from 10165 the subMat. However the offDiagMat looses some columns - and this is 10166 reconstructed with MatSetValues() 10167 10168 Level: advanced 10169 10170 10171 .seealso: MatCreateSubMatrices() 10172 @*/ 10173 PetscErrorCode MatGetMultiProcBlock(Mat mat, MPI_Comm subComm, MatReuse scall,Mat *subMat) 10174 { 10175 PetscErrorCode ierr; 10176 PetscMPIInt commsize,subCommSize; 10177 10178 PetscFunctionBegin; 10179 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&commsize);CHKERRQ(ierr); 10180 ierr = MPI_Comm_size(subComm,&subCommSize);CHKERRQ(ierr); 10181 if (subCommSize > commsize) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"CommSize %D < SubCommZize %D",commsize,subCommSize); 10182 10183 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"); 10184 ierr = PetscLogEventBegin(MAT_GetMultiProcBlock,mat,0,0,0);CHKERRQ(ierr); 10185 ierr = (*mat->ops->getmultiprocblock)(mat,subComm,scall,subMat);CHKERRQ(ierr); 10186 ierr = PetscLogEventEnd(MAT_GetMultiProcBlock,mat,0,0,0);CHKERRQ(ierr); 10187 PetscFunctionReturn(0); 10188 } 10189 10190 /*@ 10191 MatGetLocalSubMatrix - Gets a reference to a submatrix specified in local numbering 10192 10193 Not Collective 10194 10195 Input Arguments: 10196 mat - matrix to extract local submatrix from 10197 isrow - local row indices for submatrix 10198 iscol - local column indices for submatrix 10199 10200 Output Arguments: 10201 submat - the submatrix 10202 10203 Level: intermediate 10204 10205 Notes: 10206 The submat should be returned with MatRestoreLocalSubMatrix(). 10207 10208 Depending on the format of mat, the returned submat may not implement MatMult(). Its communicator may be 10209 the same as mat, it may be PETSC_COMM_SELF, or some other subcomm of mat's. 10210 10211 The submat always implements MatSetValuesLocal(). If isrow and iscol have the same block size, then 10212 MatSetValuesBlockedLocal() will also be implemented. 10213 10214 The mat must have had a ISLocalToGlobalMapping provided to it with MatSetLocalToGlobalMapping(). Note that 10215 matrices obtained with DMCreateMatrix() generally already have the local to global mapping provided. 10216 10217 .seealso: MatRestoreLocalSubMatrix(), MatCreateLocalRef(), MatSetLocalToGlobalMapping() 10218 @*/ 10219 PetscErrorCode MatGetLocalSubMatrix(Mat mat,IS isrow,IS iscol,Mat *submat) 10220 { 10221 PetscErrorCode ierr; 10222 10223 PetscFunctionBegin; 10224 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10225 PetscValidHeaderSpecific(isrow,IS_CLASSID,2); 10226 PetscValidHeaderSpecific(iscol,IS_CLASSID,3); 10227 PetscCheckSameComm(isrow,2,iscol,3); 10228 PetscValidPointer(submat,4); 10229 if (!mat->rmap->mapping) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Matrix must have local to global mapping provided before this call"); 10230 10231 if (mat->ops->getlocalsubmatrix) { 10232 ierr = (*mat->ops->getlocalsubmatrix)(mat,isrow,iscol,submat);CHKERRQ(ierr); 10233 } else { 10234 ierr = MatCreateLocalRef(mat,isrow,iscol,submat);CHKERRQ(ierr); 10235 } 10236 PetscFunctionReturn(0); 10237 } 10238 10239 /*@ 10240 MatRestoreLocalSubMatrix - Restores a reference to a submatrix specified in local numbering 10241 10242 Not Collective 10243 10244 Input Arguments: 10245 mat - matrix to extract local submatrix from 10246 isrow - local row indices for submatrix 10247 iscol - local column indices for submatrix 10248 submat - the submatrix 10249 10250 Level: intermediate 10251 10252 .seealso: MatGetLocalSubMatrix() 10253 @*/ 10254 PetscErrorCode MatRestoreLocalSubMatrix(Mat mat,IS isrow,IS iscol,Mat *submat) 10255 { 10256 PetscErrorCode ierr; 10257 10258 PetscFunctionBegin; 10259 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10260 PetscValidHeaderSpecific(isrow,IS_CLASSID,2); 10261 PetscValidHeaderSpecific(iscol,IS_CLASSID,3); 10262 PetscCheckSameComm(isrow,2,iscol,3); 10263 PetscValidPointer(submat,4); 10264 if (*submat) { 10265 PetscValidHeaderSpecific(*submat,MAT_CLASSID,4); 10266 } 10267 10268 if (mat->ops->restorelocalsubmatrix) { 10269 ierr = (*mat->ops->restorelocalsubmatrix)(mat,isrow,iscol,submat);CHKERRQ(ierr); 10270 } else { 10271 ierr = MatDestroy(submat);CHKERRQ(ierr); 10272 } 10273 *submat = NULL; 10274 PetscFunctionReturn(0); 10275 } 10276 10277 /* --------------------------------------------------------*/ 10278 /*@ 10279 MatFindZeroDiagonals - Finds all the rows of a matrix that have zero or no diagonal entry in the matrix 10280 10281 Collective on Mat 10282 10283 Input Parameter: 10284 . mat - the matrix 10285 10286 Output Parameter: 10287 . is - if any rows have zero diagonals this contains the list of them 10288 10289 Level: developer 10290 10291 .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd() 10292 @*/ 10293 PetscErrorCode MatFindZeroDiagonals(Mat mat,IS *is) 10294 { 10295 PetscErrorCode ierr; 10296 10297 PetscFunctionBegin; 10298 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10299 PetscValidType(mat,1); 10300 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 10301 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 10302 10303 if (!mat->ops->findzerodiagonals) { 10304 Vec diag; 10305 const PetscScalar *a; 10306 PetscInt *rows; 10307 PetscInt rStart, rEnd, r, nrow = 0; 10308 10309 ierr = MatCreateVecs(mat, &diag, NULL);CHKERRQ(ierr); 10310 ierr = MatGetDiagonal(mat, diag);CHKERRQ(ierr); 10311 ierr = MatGetOwnershipRange(mat, &rStart, &rEnd);CHKERRQ(ierr); 10312 ierr = VecGetArrayRead(diag, &a);CHKERRQ(ierr); 10313 for (r = 0; r < rEnd-rStart; ++r) if (a[r] == 0.0) ++nrow; 10314 ierr = PetscMalloc1(nrow, &rows);CHKERRQ(ierr); 10315 nrow = 0; 10316 for (r = 0; r < rEnd-rStart; ++r) if (a[r] == 0.0) rows[nrow++] = r+rStart; 10317 ierr = VecRestoreArrayRead(diag, &a);CHKERRQ(ierr); 10318 ierr = VecDestroy(&diag);CHKERRQ(ierr); 10319 ierr = ISCreateGeneral(PetscObjectComm((PetscObject) mat), nrow, rows, PETSC_OWN_POINTER, is);CHKERRQ(ierr); 10320 } else { 10321 ierr = (*mat->ops->findzerodiagonals)(mat, is);CHKERRQ(ierr); 10322 } 10323 PetscFunctionReturn(0); 10324 } 10325 10326 /*@ 10327 MatFindOffBlockDiagonalEntries - Finds all the rows of a matrix that have entries outside of the main diagonal block (defined by the matrix block size) 10328 10329 Collective on Mat 10330 10331 Input Parameter: 10332 . mat - the matrix 10333 10334 Output Parameter: 10335 . is - contains the list of rows with off block diagonal entries 10336 10337 Level: developer 10338 10339 .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd() 10340 @*/ 10341 PetscErrorCode MatFindOffBlockDiagonalEntries(Mat mat,IS *is) 10342 { 10343 PetscErrorCode ierr; 10344 10345 PetscFunctionBegin; 10346 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10347 PetscValidType(mat,1); 10348 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 10349 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 10350 10351 if (!mat->ops->findoffblockdiagonalentries) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"This matrix type does not have a find off block diagonal entries defined"); 10352 ierr = (*mat->ops->findoffblockdiagonalentries)(mat,is);CHKERRQ(ierr); 10353 PetscFunctionReturn(0); 10354 } 10355 10356 /*@C 10357 MatInvertBlockDiagonal - Inverts the block diagonal entries. 10358 10359 Collective on Mat 10360 10361 Input Parameters: 10362 . mat - the matrix 10363 10364 Output Parameters: 10365 . values - the block inverses in column major order (FORTRAN-like) 10366 10367 Note: 10368 This routine is not available from Fortran. 10369 10370 Level: advanced 10371 10372 .seealso: MatInvertBockDiagonalMat 10373 @*/ 10374 PetscErrorCode MatInvertBlockDiagonal(Mat mat,const PetscScalar **values) 10375 { 10376 PetscErrorCode ierr; 10377 10378 PetscFunctionBegin; 10379 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10380 if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 10381 if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 10382 if (!mat->ops->invertblockdiagonal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported"); 10383 ierr = (*mat->ops->invertblockdiagonal)(mat,values);CHKERRQ(ierr); 10384 PetscFunctionReturn(0); 10385 } 10386 10387 /*@C 10388 MatInvertVariableBlockDiagonal - Inverts the block diagonal entries. 10389 10390 Collective on Mat 10391 10392 Input Parameters: 10393 + mat - the matrix 10394 . nblocks - the number of blocks 10395 - bsizes - the size of each block 10396 10397 Output Parameters: 10398 . values - the block inverses in column major order (FORTRAN-like) 10399 10400 Note: 10401 This routine is not available from Fortran. 10402 10403 Level: advanced 10404 10405 .seealso: MatInvertBockDiagonal() 10406 @*/ 10407 PetscErrorCode MatInvertVariableBlockDiagonal(Mat mat,PetscInt nblocks,const PetscInt *bsizes,PetscScalar *values) 10408 { 10409 PetscErrorCode ierr; 10410 10411 PetscFunctionBegin; 10412 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10413 if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 10414 if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 10415 if (!mat->ops->invertvariableblockdiagonal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported"); 10416 ierr = (*mat->ops->invertvariableblockdiagonal)(mat,nblocks,bsizes,values);CHKERRQ(ierr); 10417 PetscFunctionReturn(0); 10418 } 10419 10420 /*@ 10421 MatInvertBlockDiagonalMat - set matrix C to be the inverted block diagonal of matrix A 10422 10423 Collective on Mat 10424 10425 Input Parameters: 10426 . A - the matrix 10427 10428 Output Parameters: 10429 . C - matrix with inverted block diagonal of A. This matrix should be created and may have its type set. 10430 10431 Notes: the blocksize of the matrix is used to determine the blocks on the diagonal of C 10432 10433 Level: advanced 10434 10435 .seealso: MatInvertBockDiagonal() 10436 @*/ 10437 PetscErrorCode MatInvertBlockDiagonalMat(Mat A,Mat C) 10438 { 10439 PetscErrorCode ierr; 10440 const PetscScalar *vals; 10441 PetscInt *dnnz; 10442 PetscInt M,N,m,n,rstart,rend,bs,i,j; 10443 10444 PetscFunctionBegin; 10445 ierr = MatInvertBlockDiagonal(A,&vals);CHKERRQ(ierr); 10446 ierr = MatGetBlockSize(A,&bs);CHKERRQ(ierr); 10447 ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr); 10448 ierr = MatGetLocalSize(A,&m,&n);CHKERRQ(ierr); 10449 ierr = MatSetSizes(C,m,n,M,N);CHKERRQ(ierr); 10450 ierr = MatSetBlockSize(C,bs);CHKERRQ(ierr); 10451 ierr = PetscMalloc1(m/bs,&dnnz);CHKERRQ(ierr); 10452 for (j = 0; j < m/bs; j++) dnnz[j] = 1; 10453 ierr = MatXAIJSetPreallocation(C,bs,dnnz,NULL,NULL,NULL);CHKERRQ(ierr); 10454 ierr = PetscFree(dnnz);CHKERRQ(ierr); 10455 ierr = MatGetOwnershipRange(C,&rstart,&rend);CHKERRQ(ierr); 10456 ierr = MatSetOption(C,MAT_ROW_ORIENTED,PETSC_FALSE);CHKERRQ(ierr); 10457 for (i = rstart/bs; i < rend/bs; i++) { 10458 ierr = MatSetValuesBlocked(C,1,&i,1,&i,&vals[(i-rstart/bs)*bs*bs],INSERT_VALUES);CHKERRQ(ierr); 10459 } 10460 ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 10461 ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 10462 ierr = MatSetOption(C,MAT_ROW_ORIENTED,PETSC_TRUE);CHKERRQ(ierr); 10463 PetscFunctionReturn(0); 10464 } 10465 10466 /*@C 10467 MatTransposeColoringDestroy - Destroys a coloring context for matrix product C=A*B^T that was created 10468 via MatTransposeColoringCreate(). 10469 10470 Collective on MatTransposeColoring 10471 10472 Input Parameter: 10473 . c - coloring context 10474 10475 Level: intermediate 10476 10477 .seealso: MatTransposeColoringCreate() 10478 @*/ 10479 PetscErrorCode MatTransposeColoringDestroy(MatTransposeColoring *c) 10480 { 10481 PetscErrorCode ierr; 10482 MatTransposeColoring matcolor=*c; 10483 10484 PetscFunctionBegin; 10485 if (!matcolor) PetscFunctionReturn(0); 10486 if (--((PetscObject)matcolor)->refct > 0) {matcolor = 0; PetscFunctionReturn(0);} 10487 10488 ierr = PetscFree3(matcolor->ncolumns,matcolor->nrows,matcolor->colorforrow);CHKERRQ(ierr); 10489 ierr = PetscFree(matcolor->rows);CHKERRQ(ierr); 10490 ierr = PetscFree(matcolor->den2sp);CHKERRQ(ierr); 10491 ierr = PetscFree(matcolor->colorforcol);CHKERRQ(ierr); 10492 ierr = PetscFree(matcolor->columns);CHKERRQ(ierr); 10493 if (matcolor->brows>0) { 10494 ierr = PetscFree(matcolor->lstart);CHKERRQ(ierr); 10495 } 10496 ierr = PetscHeaderDestroy(c);CHKERRQ(ierr); 10497 PetscFunctionReturn(0); 10498 } 10499 10500 /*@C 10501 MatTransColoringApplySpToDen - Given a symbolic matrix product C=A*B^T for which 10502 a MatTransposeColoring context has been created, computes a dense B^T by Apply 10503 MatTransposeColoring to sparse B. 10504 10505 Collective on MatTransposeColoring 10506 10507 Input Parameters: 10508 + B - sparse matrix B 10509 . Btdense - symbolic dense matrix B^T 10510 - coloring - coloring context created with MatTransposeColoringCreate() 10511 10512 Output Parameter: 10513 . Btdense - dense matrix B^T 10514 10515 Level: advanced 10516 10517 Notes: 10518 These are used internally for some implementations of MatRARt() 10519 10520 .seealso: MatTransposeColoringCreate(), MatTransposeColoringDestroy(), MatTransColoringApplyDenToSp() 10521 10522 @*/ 10523 PetscErrorCode MatTransColoringApplySpToDen(MatTransposeColoring coloring,Mat B,Mat Btdense) 10524 { 10525 PetscErrorCode ierr; 10526 10527 PetscFunctionBegin; 10528 PetscValidHeaderSpecific(B,MAT_CLASSID,1); 10529 PetscValidHeaderSpecific(Btdense,MAT_CLASSID,2); 10530 PetscValidHeaderSpecific(coloring,MAT_TRANSPOSECOLORING_CLASSID,3); 10531 10532 if (!B->ops->transcoloringapplysptoden) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported for this matrix type %s",((PetscObject)B)->type_name); 10533 ierr = (B->ops->transcoloringapplysptoden)(coloring,B,Btdense);CHKERRQ(ierr); 10534 PetscFunctionReturn(0); 10535 } 10536 10537 /*@C 10538 MatTransColoringApplyDenToSp - Given a symbolic matrix product Csp=A*B^T for which 10539 a MatTransposeColoring context has been created and a dense matrix Cden=A*Btdense 10540 in which Btdens is obtained from MatTransColoringApplySpToDen(), recover sparse matrix 10541 Csp from Cden. 10542 10543 Collective on MatTransposeColoring 10544 10545 Input Parameters: 10546 + coloring - coloring context created with MatTransposeColoringCreate() 10547 - Cden - matrix product of a sparse matrix and a dense matrix Btdense 10548 10549 Output Parameter: 10550 . Csp - sparse matrix 10551 10552 Level: advanced 10553 10554 Notes: 10555 These are used internally for some implementations of MatRARt() 10556 10557 .seealso: MatTransposeColoringCreate(), MatTransposeColoringDestroy(), MatTransColoringApplySpToDen() 10558 10559 @*/ 10560 PetscErrorCode MatTransColoringApplyDenToSp(MatTransposeColoring matcoloring,Mat Cden,Mat Csp) 10561 { 10562 PetscErrorCode ierr; 10563 10564 PetscFunctionBegin; 10565 PetscValidHeaderSpecific(matcoloring,MAT_TRANSPOSECOLORING_CLASSID,1); 10566 PetscValidHeaderSpecific(Cden,MAT_CLASSID,2); 10567 PetscValidHeaderSpecific(Csp,MAT_CLASSID,3); 10568 10569 if (!Csp->ops->transcoloringapplydentosp) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported for this matrix type %s",((PetscObject)Csp)->type_name); 10570 ierr = (Csp->ops->transcoloringapplydentosp)(matcoloring,Cden,Csp);CHKERRQ(ierr); 10571 PetscFunctionReturn(0); 10572 } 10573 10574 /*@C 10575 MatTransposeColoringCreate - Creates a matrix coloring context for matrix product C=A*B^T. 10576 10577 Collective on Mat 10578 10579 Input Parameters: 10580 + mat - the matrix product C 10581 - iscoloring - the coloring of the matrix; usually obtained with MatColoringCreate() or DMCreateColoring() 10582 10583 Output Parameter: 10584 . color - the new coloring context 10585 10586 Level: intermediate 10587 10588 .seealso: MatTransposeColoringDestroy(), MatTransColoringApplySpToDen(), 10589 MatTransColoringApplyDenToSp() 10590 @*/ 10591 PetscErrorCode MatTransposeColoringCreate(Mat mat,ISColoring iscoloring,MatTransposeColoring *color) 10592 { 10593 MatTransposeColoring c; 10594 MPI_Comm comm; 10595 PetscErrorCode ierr; 10596 10597 PetscFunctionBegin; 10598 ierr = PetscLogEventBegin(MAT_TransposeColoringCreate,mat,0,0,0);CHKERRQ(ierr); 10599 ierr = PetscObjectGetComm((PetscObject)mat,&comm);CHKERRQ(ierr); 10600 ierr = PetscHeaderCreate(c,MAT_TRANSPOSECOLORING_CLASSID,"MatTransposeColoring","Matrix product C=A*B^T via coloring","Mat",comm,MatTransposeColoringDestroy,NULL);CHKERRQ(ierr); 10601 10602 c->ctype = iscoloring->ctype; 10603 if (mat->ops->transposecoloringcreate) { 10604 ierr = (*mat->ops->transposecoloringcreate)(mat,iscoloring,c);CHKERRQ(ierr); 10605 } else SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Code not yet written for this matrix type"); 10606 10607 *color = c; 10608 ierr = PetscLogEventEnd(MAT_TransposeColoringCreate,mat,0,0,0);CHKERRQ(ierr); 10609 PetscFunctionReturn(0); 10610 } 10611 10612 /*@ 10613 MatGetNonzeroState - Returns a 64 bit integer representing the current state of nonzeros in the matrix. If the 10614 matrix has had no new nonzero locations added to the matrix since the previous call then the value will be the 10615 same, otherwise it will be larger 10616 10617 Not Collective 10618 10619 Input Parameter: 10620 . A - the matrix 10621 10622 Output Parameter: 10623 . state - the current state 10624 10625 Notes: 10626 You can only compare states from two different calls to the SAME matrix, you cannot compare calls between 10627 different matrices 10628 10629 Level: intermediate 10630 10631 @*/ 10632 PetscErrorCode MatGetNonzeroState(Mat mat,PetscObjectState *state) 10633 { 10634 PetscFunctionBegin; 10635 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10636 *state = mat->nonzerostate; 10637 PetscFunctionReturn(0); 10638 } 10639 10640 /*@ 10641 MatCreateMPIMatConcatenateSeqMat - Creates a single large PETSc matrix by concatenating sequential 10642 matrices from each processor 10643 10644 Collective 10645 10646 Input Parameters: 10647 + comm - the communicators the parallel matrix will live on 10648 . seqmat - the input sequential matrices 10649 . n - number of local columns (or PETSC_DECIDE) 10650 - reuse - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 10651 10652 Output Parameter: 10653 . mpimat - the parallel matrix generated 10654 10655 Level: advanced 10656 10657 Notes: 10658 The number of columns of the matrix in EACH processor MUST be the same. 10659 10660 @*/ 10661 PetscErrorCode MatCreateMPIMatConcatenateSeqMat(MPI_Comm comm,Mat seqmat,PetscInt n,MatReuse reuse,Mat *mpimat) 10662 { 10663 PetscErrorCode ierr; 10664 10665 PetscFunctionBegin; 10666 if (!seqmat->ops->creatempimatconcatenateseqmat) SETERRQ1(PetscObjectComm((PetscObject)seqmat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)seqmat)->type_name); 10667 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"); 10668 10669 ierr = PetscLogEventBegin(MAT_Merge,seqmat,0,0,0);CHKERRQ(ierr); 10670 ierr = (*seqmat->ops->creatempimatconcatenateseqmat)(comm,seqmat,n,reuse,mpimat);CHKERRQ(ierr); 10671 ierr = PetscLogEventEnd(MAT_Merge,seqmat,0,0,0);CHKERRQ(ierr); 10672 PetscFunctionReturn(0); 10673 } 10674 10675 /*@ 10676 MatSubdomainsCreateCoalesce - Creates index subdomains by coalescing adjacent 10677 ranks' ownership ranges. 10678 10679 Collective on A 10680 10681 Input Parameters: 10682 + A - the matrix to create subdomains from 10683 - N - requested number of subdomains 10684 10685 10686 Output Parameters: 10687 + n - number of subdomains resulting on this rank 10688 - iss - IS list with indices of subdomains on this rank 10689 10690 Level: advanced 10691 10692 Notes: 10693 number of subdomains must be smaller than the communicator size 10694 @*/ 10695 PetscErrorCode MatSubdomainsCreateCoalesce(Mat A,PetscInt N,PetscInt *n,IS *iss[]) 10696 { 10697 MPI_Comm comm,subcomm; 10698 PetscMPIInt size,rank,color; 10699 PetscInt rstart,rend,k; 10700 PetscErrorCode ierr; 10701 10702 PetscFunctionBegin; 10703 ierr = PetscObjectGetComm((PetscObject)A,&comm);CHKERRQ(ierr); 10704 ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 10705 ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 10706 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); 10707 *n = 1; 10708 k = ((PetscInt)size)/N + ((PetscInt)size%N>0); /* There are up to k ranks to a color */ 10709 color = rank/k; 10710 ierr = MPI_Comm_split(comm,color,rank,&subcomm);CHKERRQ(ierr); 10711 ierr = PetscMalloc1(1,iss);CHKERRQ(ierr); 10712 ierr = MatGetOwnershipRange(A,&rstart,&rend);CHKERRQ(ierr); 10713 ierr = ISCreateStride(subcomm,rend-rstart,rstart,1,iss[0]);CHKERRQ(ierr); 10714 ierr = MPI_Comm_free(&subcomm);CHKERRQ(ierr); 10715 PetscFunctionReturn(0); 10716 } 10717 10718 /*@ 10719 MatGalerkin - Constructs the coarse grid problem via Galerkin projection. 10720 10721 If the interpolation and restriction operators are the same, uses MatPtAP. 10722 If they are not the same, use MatMatMatMult. 10723 10724 Once the coarse grid problem is constructed, correct for interpolation operators 10725 that are not of full rank, which can legitimately happen in the case of non-nested 10726 geometric multigrid. 10727 10728 Input Parameters: 10729 + restrct - restriction operator 10730 . dA - fine grid matrix 10731 . interpolate - interpolation operator 10732 . reuse - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 10733 - fill - expected fill, use PETSC_DEFAULT if you do not have a good estimate 10734 10735 Output Parameters: 10736 . A - the Galerkin coarse matrix 10737 10738 Options Database Key: 10739 . -pc_mg_galerkin <both,pmat,mat,none> 10740 10741 Level: developer 10742 10743 .seealso: MatPtAP(), MatMatMatMult() 10744 @*/ 10745 PetscErrorCode MatGalerkin(Mat restrct, Mat dA, Mat interpolate, MatReuse reuse, PetscReal fill, Mat *A) 10746 { 10747 PetscErrorCode ierr; 10748 IS zerorows; 10749 Vec diag; 10750 10751 PetscFunctionBegin; 10752 if (reuse == MAT_INPLACE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported"); 10753 /* Construct the coarse grid matrix */ 10754 if (interpolate == restrct) { 10755 ierr = MatPtAP(dA,interpolate,reuse,fill,A);CHKERRQ(ierr); 10756 } else { 10757 ierr = MatMatMatMult(restrct,dA,interpolate,reuse,fill,A);CHKERRQ(ierr); 10758 } 10759 10760 /* If the interpolation matrix is not of full rank, A will have zero rows. 10761 This can legitimately happen in the case of non-nested geometric multigrid. 10762 In that event, we set the rows of the matrix to the rows of the identity, 10763 ignoring the equations (as the RHS will also be zero). */ 10764 10765 ierr = MatFindZeroRows(*A, &zerorows);CHKERRQ(ierr); 10766 10767 if (zerorows != NULL) { /* if there are any zero rows */ 10768 ierr = MatCreateVecs(*A, &diag, NULL);CHKERRQ(ierr); 10769 ierr = MatGetDiagonal(*A, diag);CHKERRQ(ierr); 10770 ierr = VecISSet(diag, zerorows, 1.0);CHKERRQ(ierr); 10771 ierr = MatDiagonalSet(*A, diag, INSERT_VALUES);CHKERRQ(ierr); 10772 ierr = VecDestroy(&diag);CHKERRQ(ierr); 10773 ierr = ISDestroy(&zerorows);CHKERRQ(ierr); 10774 } 10775 PetscFunctionReturn(0); 10776 } 10777 10778 /*@C 10779 MatSetOperation - Allows user to set a matrix operation for any matrix type 10780 10781 Logically Collective on Mat 10782 10783 Input Parameters: 10784 + mat - the matrix 10785 . op - the name of the operation 10786 - f - the function that provides the operation 10787 10788 Level: developer 10789 10790 Usage: 10791 $ extern PetscErrorCode usermult(Mat,Vec,Vec); 10792 $ ierr = MatCreateXXX(comm,...&A); 10793 $ ierr = MatSetOperation(A,MATOP_MULT,(void(*)(void))usermult); 10794 10795 Notes: 10796 See the file include/petscmat.h for a complete list of matrix 10797 operations, which all have the form MATOP_<OPERATION>, where 10798 <OPERATION> is the name (in all capital letters) of the 10799 user interface routine (e.g., MatMult() -> MATOP_MULT). 10800 10801 All user-provided functions (except for MATOP_DESTROY) should have the same calling 10802 sequence as the usual matrix interface routines, since they 10803 are intended to be accessed via the usual matrix interface 10804 routines, e.g., 10805 $ MatMult(Mat,Vec,Vec) -> usermult(Mat,Vec,Vec) 10806 10807 In particular each function MUST return an error code of 0 on success and 10808 nonzero on failure. 10809 10810 This routine is distinct from MatShellSetOperation() in that it can be called on any matrix type. 10811 10812 .seealso: MatGetOperation(), MatCreateShell(), MatShellSetContext(), MatShellSetOperation() 10813 @*/ 10814 PetscErrorCode MatSetOperation(Mat mat,MatOperation op,void (*f)(void)) 10815 { 10816 PetscFunctionBegin; 10817 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10818 if (op == MATOP_VIEW && !mat->ops->viewnative && f != (void (*)(void))(mat->ops->view)) { 10819 mat->ops->viewnative = mat->ops->view; 10820 } 10821 (((void(**)(void))mat->ops)[op]) = f; 10822 PetscFunctionReturn(0); 10823 } 10824 10825 /*@C 10826 MatGetOperation - Gets a matrix operation for any matrix type. 10827 10828 Not Collective 10829 10830 Input Parameters: 10831 + mat - the matrix 10832 - op - the name of the operation 10833 10834 Output Parameter: 10835 . f - the function that provides the operation 10836 10837 Level: developer 10838 10839 Usage: 10840 $ PetscErrorCode (*usermult)(Mat,Vec,Vec); 10841 $ ierr = MatGetOperation(A,MATOP_MULT,(void(**)(void))&usermult); 10842 10843 Notes: 10844 See the file include/petscmat.h for a complete list of matrix 10845 operations, which all have the form MATOP_<OPERATION>, where 10846 <OPERATION> is the name (in all capital letters) of the 10847 user interface routine (e.g., MatMult() -> MATOP_MULT). 10848 10849 This routine is distinct from MatShellGetOperation() in that it can be called on any matrix type. 10850 10851 .seealso: MatSetOperation(), MatCreateShell(), MatShellGetContext(), MatShellGetOperation() 10852 @*/ 10853 PetscErrorCode MatGetOperation(Mat mat,MatOperation op,void(**f)(void)) 10854 { 10855 PetscFunctionBegin; 10856 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10857 *f = (((void (**)(void))mat->ops)[op]); 10858 PetscFunctionReturn(0); 10859 } 10860 10861 /*@ 10862 MatHasOperation - Determines whether the given matrix supports the particular 10863 operation. 10864 10865 Not Collective 10866 10867 Input Parameters: 10868 + mat - the matrix 10869 - op - the operation, for example, MATOP_GET_DIAGONAL 10870 10871 Output Parameter: 10872 . has - either PETSC_TRUE or PETSC_FALSE 10873 10874 Level: advanced 10875 10876 Notes: 10877 See the file include/petscmat.h for a complete list of matrix 10878 operations, which all have the form MATOP_<OPERATION>, where 10879 <OPERATION> is the name (in all capital letters) of the 10880 user-level routine. E.g., MatNorm() -> MATOP_NORM. 10881 10882 .seealso: MatCreateShell() 10883 @*/ 10884 PetscErrorCode MatHasOperation(Mat mat,MatOperation op,PetscBool *has) 10885 { 10886 PetscErrorCode ierr; 10887 10888 PetscFunctionBegin; 10889 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10890 PetscValidType(mat,1); 10891 PetscValidPointer(has,3); 10892 if (mat->ops->hasoperation) { 10893 ierr = (*mat->ops->hasoperation)(mat,op,has);CHKERRQ(ierr); 10894 } else { 10895 if (((void**)mat->ops)[op]) *has = PETSC_TRUE; 10896 else { 10897 *has = PETSC_FALSE; 10898 if (op == MATOP_CREATE_SUBMATRIX) { 10899 PetscMPIInt size; 10900 10901 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);CHKERRQ(ierr); 10902 if (size == 1) { 10903 ierr = MatHasOperation(mat,MATOP_CREATE_SUBMATRICES,has);CHKERRQ(ierr); 10904 } 10905 } 10906 } 10907 } 10908 PetscFunctionReturn(0); 10909 } 10910 10911 /*@ 10912 MatHasCongruentLayouts - Determines whether the rows and columns layouts 10913 of the matrix are congruent 10914 10915 Collective on mat 10916 10917 Input Parameters: 10918 . mat - the matrix 10919 10920 Output Parameter: 10921 . cong - either PETSC_TRUE or PETSC_FALSE 10922 10923 Level: beginner 10924 10925 Notes: 10926 10927 .seealso: MatCreate(), MatSetSizes() 10928 @*/ 10929 PetscErrorCode MatHasCongruentLayouts(Mat mat,PetscBool *cong) 10930 { 10931 PetscErrorCode ierr; 10932 10933 PetscFunctionBegin; 10934 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10935 PetscValidType(mat,1); 10936 PetscValidPointer(cong,2); 10937 if (!mat->rmap || !mat->cmap) { 10938 *cong = mat->rmap == mat->cmap ? PETSC_TRUE : PETSC_FALSE; 10939 PetscFunctionReturn(0); 10940 } 10941 if (mat->congruentlayouts == PETSC_DECIDE) { /* first time we compare rows and cols layouts */ 10942 ierr = PetscLayoutCompare(mat->rmap,mat->cmap,cong);CHKERRQ(ierr); 10943 if (*cong) mat->congruentlayouts = 1; 10944 else mat->congruentlayouts = 0; 10945 } else *cong = mat->congruentlayouts ? PETSC_TRUE : PETSC_FALSE; 10946 PetscFunctionReturn(0); 10947 } 10948 10949 /*@ 10950 MatFreeIntermediateDataStructures - Free intermediate data structures created for reuse, 10951 e.g., matrx product of MatPtAP. 10952 10953 Collective on mat 10954 10955 Input Parameters: 10956 . mat - the matrix 10957 10958 Output Parameter: 10959 . mat - the matrix with intermediate data structures released 10960 10961 Level: advanced 10962 10963 Notes: 10964 10965 .seealso: MatPtAP(), MatMatMult() 10966 @*/ 10967 PetscErrorCode MatFreeIntermediateDataStructures(Mat mat) 10968 { 10969 PetscErrorCode ierr; 10970 10971 PetscFunctionBegin; 10972 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10973 PetscValidType(mat,1); 10974 if (mat->ops->freeintermediatedatastructures) { 10975 ierr = (*mat->ops->freeintermediatedatastructures)(mat);CHKERRQ(ierr); 10976 } 10977 PetscFunctionReturn(0); 10978 } 10979