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