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