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 if (!mat->ops->solve) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 3286 ierr = (*mat->ops->solve)(mat,b,x);CHKERRQ(ierr); 3287 } 3288 ierr = PetscLogEventEnd(MAT_Solve,mat,b,x,0);CHKERRQ(ierr); 3289 ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr); 3290 PetscFunctionReturn(0); 3291 } 3292 3293 static PetscErrorCode MatMatSolve_Basic(Mat A,Mat B,Mat X, PetscBool trans) 3294 { 3295 PetscErrorCode ierr; 3296 Vec b,x; 3297 PetscInt m,N,i; 3298 PetscScalar *bb,*xx; 3299 PetscBool flg; 3300 3301 PetscFunctionBegin; 3302 ierr = PetscObjectTypeCompareAny((PetscObject)B,&flg,MATSEQDENSE,MATMPIDENSE,NULL);CHKERRQ(ierr); 3303 if (!flg) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONG,"Matrix B must be MATDENSE matrix"); 3304 ierr = PetscObjectTypeCompareAny((PetscObject)X,&flg,MATSEQDENSE,MATMPIDENSE,NULL);CHKERRQ(ierr); 3305 if (!flg) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONG,"Matrix X must be MATDENSE matrix"); 3306 3307 ierr = MatDenseGetArray(B,&bb);CHKERRQ(ierr); 3308 ierr = MatDenseGetArray(X,&xx);CHKERRQ(ierr); 3309 ierr = MatGetLocalSize(B,&m,NULL);CHKERRQ(ierr); /* number local rows */ 3310 ierr = MatGetSize(B,NULL,&N);CHKERRQ(ierr); /* total columns in dense matrix */ 3311 ierr = MatCreateVecs(A,&x,&b);CHKERRQ(ierr); 3312 for (i=0; i<N; i++) { 3313 ierr = VecPlaceArray(b,bb + i*m);CHKERRQ(ierr); 3314 ierr = VecPlaceArray(x,xx + i*m);CHKERRQ(ierr); 3315 if (trans) { 3316 ierr = MatSolveTranspose(A,b,x);CHKERRQ(ierr); 3317 } else { 3318 ierr = MatSolve(A,b,x);CHKERRQ(ierr); 3319 } 3320 ierr = VecResetArray(x);CHKERRQ(ierr); 3321 ierr = VecResetArray(b);CHKERRQ(ierr); 3322 } 3323 ierr = VecDestroy(&b);CHKERRQ(ierr); 3324 ierr = VecDestroy(&x);CHKERRQ(ierr); 3325 ierr = MatDenseRestoreArray(B,&bb);CHKERRQ(ierr); 3326 ierr = MatDenseRestoreArray(X,&xx);CHKERRQ(ierr); 3327 PetscFunctionReturn(0); 3328 } 3329 3330 /*@ 3331 MatMatSolve - Solves A X = B, given a factored matrix. 3332 3333 Neighbor-wise Collective on Mat 3334 3335 Input Parameters: 3336 + A - the factored matrix 3337 - B - the right-hand-side matrix (dense matrix) 3338 3339 Output Parameter: 3340 . X - the result matrix (dense matrix) 3341 3342 Notes: 3343 The matrices b and x cannot be the same. I.e., one cannot 3344 call MatMatSolve(A,x,x). 3345 3346 Notes: 3347 Most users should usually employ the simplified KSP interface for linear solvers 3348 instead of working directly with matrix algebra routines such as this. 3349 See, e.g., KSPCreate(). However KSP can only solve for one vector (column of X) 3350 at a time. 3351 3352 When using SuperLU_Dist as a parallel solver PETSc will use the SuperLU_Dist functionality to solve multiple right hand sides simultaneously. For MUMPS 3353 it calls a separate solve for each right hand side since MUMPS does not yet support distributed right hand sides. 3354 3355 Since the resulting matrix X must always be dense we do not support sparse representation of the matrix B. 3356 3357 Level: developer 3358 3359 Concepts: matrices^triangular solves 3360 3361 .seealso: MatMatSolveTranspose(), MatLUFactor(), MatCholeskyFactor() 3362 @*/ 3363 PetscErrorCode MatMatSolve(Mat A,Mat B,Mat X) 3364 { 3365 PetscErrorCode ierr; 3366 3367 PetscFunctionBegin; 3368 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 3369 PetscValidType(A,1); 3370 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 3371 PetscValidHeaderSpecific(X,MAT_CLASSID,3); 3372 PetscCheckSameComm(A,1,B,2); 3373 PetscCheckSameComm(A,1,X,3); 3374 if (X == B) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_IDN,"X and B must be different matrices"); 3375 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); 3376 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); 3377 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"); 3378 if (!A->rmap->N && !A->cmap->N) PetscFunctionReturn(0); 3379 if (!A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 3380 MatCheckPreallocated(A,1); 3381 3382 ierr = PetscLogEventBegin(MAT_MatSolve,A,B,X,0);CHKERRQ(ierr); 3383 if (!A->ops->matsolve) { 3384 ierr = PetscInfo1(A,"Mat type %s using basic MatMatSolve\n",((PetscObject)A)->type_name);CHKERRQ(ierr); 3385 ierr = MatMatSolve_Basic(A,B,X,PETSC_FALSE);CHKERRQ(ierr); 3386 } else { 3387 ierr = (*A->ops->matsolve)(A,B,X);CHKERRQ(ierr); 3388 } 3389 ierr = PetscLogEventEnd(MAT_MatSolve,A,B,X,0);CHKERRQ(ierr); 3390 ierr = PetscObjectStateIncrease((PetscObject)X);CHKERRQ(ierr); 3391 PetscFunctionReturn(0); 3392 } 3393 3394 /*@ 3395 MatMatSolveTranspose - Solves A^T X = B, given a factored matrix. 3396 3397 Neighbor-wise Collective on Mat 3398 3399 Input Parameters: 3400 + A - the factored matrix 3401 - B - the right-hand-side matrix (dense matrix) 3402 3403 Output Parameter: 3404 . X - the result matrix (dense matrix) 3405 3406 Notes: 3407 The matrices B and X cannot be the same. I.e., one cannot 3408 call MatMatSolveTranspose(A,X,X). 3409 3410 Notes: 3411 Most users should usually employ the simplified KSP interface for linear solvers 3412 instead of working directly with matrix algebra routines such as this. 3413 See, e.g., KSPCreate(). However KSP can only solve for one vector (column of X) 3414 at a time. 3415 3416 When using SuperLU_Dist or MUMPS as a parallel solver, PETSc will use their functionality to solve multiple right hand sides simultaneously. 3417 3418 Level: developer 3419 3420 Concepts: matrices^triangular solves 3421 3422 .seealso: MatMatSolve(), MatLUFactor(), MatCholeskyFactor() 3423 @*/ 3424 PetscErrorCode MatMatSolveTranspose(Mat A,Mat B,Mat X) 3425 { 3426 PetscErrorCode ierr; 3427 3428 PetscFunctionBegin; 3429 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 3430 PetscValidType(A,1); 3431 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 3432 PetscValidHeaderSpecific(X,MAT_CLASSID,3); 3433 PetscCheckSameComm(A,1,B,2); 3434 PetscCheckSameComm(A,1,X,3); 3435 if (X == B) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_IDN,"X and B must be different matrices"); 3436 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); 3437 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); 3438 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); 3439 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"); 3440 if (!A->rmap->N && !A->cmap->N) PetscFunctionReturn(0); 3441 if (!A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 3442 MatCheckPreallocated(A,1); 3443 3444 ierr = PetscLogEventBegin(MAT_MatSolve,A,B,X,0);CHKERRQ(ierr); 3445 if (!A->ops->matsolvetranspose) { 3446 ierr = PetscInfo1(A,"Mat type %s using basic MatMatSolveTranspose\n",((PetscObject)A)->type_name);CHKERRQ(ierr); 3447 ierr = MatMatSolve_Basic(A,B,X,PETSC_TRUE);CHKERRQ(ierr); 3448 } else { 3449 ierr = (*A->ops->matsolvetranspose)(A,B,X);CHKERRQ(ierr); 3450 } 3451 ierr = PetscLogEventEnd(MAT_MatSolve,A,B,X,0);CHKERRQ(ierr); 3452 ierr = PetscObjectStateIncrease((PetscObject)X);CHKERRQ(ierr); 3453 PetscFunctionReturn(0); 3454 } 3455 3456 /*@ 3457 MatMatTransposeSolve - Solves A X = B^T, given a factored matrix. 3458 3459 Neighbor-wise Collective on Mat 3460 3461 Input Parameters: 3462 + A - the factored matrix 3463 - Bt - the transpose of right-hand-side matrix 3464 3465 Output Parameter: 3466 . X - the result matrix (dense matrix) 3467 3468 Notes: 3469 Most users should usually employ the simplified KSP interface for linear solvers 3470 instead of working directly with matrix algebra routines such as this. 3471 See, e.g., KSPCreate(). However KSP can only solve for one vector (column of X) 3472 at a time. 3473 3474 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(). 3475 3476 Level: developer 3477 3478 Concepts: matrices^triangular solves 3479 3480 .seealso: MatMatSolve(), MatMatSolveTranspose(), MatLUFactor(), MatCholeskyFactor() 3481 @*/ 3482 PetscErrorCode MatMatTransposeSolve(Mat A,Mat Bt,Mat X) 3483 { 3484 PetscErrorCode ierr; 3485 3486 PetscFunctionBegin; 3487 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 3488 PetscValidType(A,1); 3489 PetscValidHeaderSpecific(Bt,MAT_CLASSID,2); 3490 PetscValidHeaderSpecific(X,MAT_CLASSID,3); 3491 PetscCheckSameComm(A,1,Bt,2); 3492 PetscCheckSameComm(A,1,X,3); 3493 3494 if (X == Bt) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_IDN,"X and B must be different matrices"); 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 if (!A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 3500 MatCheckPreallocated(A,1); 3501 3502 if (!A->ops->mattransposesolve) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Mat type %s",((PetscObject)A)->type_name); 3503 ierr = PetscLogEventBegin(MAT_MatTrSolve,A,Bt,X,0);CHKERRQ(ierr); 3504 ierr = (*A->ops->mattransposesolve)(A,Bt,X);CHKERRQ(ierr); 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->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); 3559 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); 3560 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); 3561 if (!mat->rmap->N && !mat->cmap->N) PetscFunctionReturn(0); 3562 if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 3563 MatCheckPreallocated(mat,1); 3564 3565 if (!mat->ops->forwardsolve) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 3566 ierr = PetscLogEventBegin(MAT_ForwardSolve,mat,b,x,0);CHKERRQ(ierr); 3567 ierr = (*mat->ops->forwardsolve)(mat,b,x);CHKERRQ(ierr); 3568 ierr = PetscLogEventEnd(MAT_ForwardSolve,mat,b,x,0);CHKERRQ(ierr); 3569 ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr); 3570 PetscFunctionReturn(0); 3571 } 3572 3573 /*@ 3574 MatBackwardSolve - Solves U x = b, given a factored matrix, A = LU. 3575 D^(1/2) U x = b, given a factored symmetric matrix, A = U^T*D*U, 3576 3577 Neighbor-wise Collective on Mat and Vec 3578 3579 Input Parameters: 3580 + mat - the factored matrix 3581 - b - the right-hand-side vector 3582 3583 Output Parameter: 3584 . x - the result vector 3585 3586 Notes: 3587 MatSolve() should be used for most applications, as it performs 3588 a forward solve followed by a backward solve. 3589 3590 The vectors b and x cannot be the same. I.e., one cannot 3591 call MatBackwardSolve(A,x,x). 3592 3593 For matrix in seqsbaij format with block size larger than 1, 3594 the diagonal blocks are not implemented as D = D^(1/2) * D^(1/2) yet. 3595 MatForwardSolve() solves U^T*D y = b, and 3596 MatBackwardSolve() solves U x = y. 3597 Thus they do not provide a symmetric preconditioner. 3598 3599 Most users should employ the simplified KSP interface for linear solvers 3600 instead of working directly with matrix algebra routines such as this. 3601 See, e.g., KSPCreate(). 3602 3603 Level: developer 3604 3605 Concepts: matrices^backward solves 3606 3607 .seealso: MatSolve(), MatForwardSolve() 3608 @*/ 3609 PetscErrorCode MatBackwardSolve(Mat mat,Vec b,Vec x) 3610 { 3611 PetscErrorCode ierr; 3612 3613 PetscFunctionBegin; 3614 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3615 PetscValidType(mat,1); 3616 PetscValidHeaderSpecific(b,VEC_CLASSID,2); 3617 PetscValidHeaderSpecific(x,VEC_CLASSID,3); 3618 PetscCheckSameComm(mat,1,b,2); 3619 PetscCheckSameComm(mat,1,x,3); 3620 if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 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 if (!mat->ops->backwardsolve) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 3629 ierr = PetscLogEventBegin(MAT_BackwardSolve,mat,b,x,0);CHKERRQ(ierr); 3630 ierr = (*mat->ops->backwardsolve)(mat,b,x);CHKERRQ(ierr); 3631 ierr = PetscLogEventEnd(MAT_BackwardSolve,mat,b,x,0);CHKERRQ(ierr); 3632 ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr); 3633 PetscFunctionReturn(0); 3634 } 3635 3636 /*@ 3637 MatSolveAdd - Computes x = y + inv(A)*b, given a factored matrix. 3638 3639 Neighbor-wise Collective on Mat and Vec 3640 3641 Input Parameters: 3642 + mat - the factored matrix 3643 . b - the right-hand-side vector 3644 - y - the vector to be added to 3645 3646 Output Parameter: 3647 . x - the result vector 3648 3649 Notes: 3650 The vectors b and x cannot be the same. I.e., one cannot 3651 call MatSolveAdd(A,x,y,x). 3652 3653 Most users should employ the simplified KSP interface for linear solvers 3654 instead of working directly with matrix algebra routines such as this. 3655 See, e.g., KSPCreate(). 3656 3657 Level: developer 3658 3659 Concepts: matrices^triangular solves 3660 3661 .seealso: MatSolve(), MatSolveTranspose(), MatSolveTransposeAdd() 3662 @*/ 3663 PetscErrorCode MatSolveAdd(Mat mat,Vec b,Vec y,Vec x) 3664 { 3665 PetscScalar one = 1.0; 3666 Vec tmp; 3667 PetscErrorCode ierr; 3668 3669 PetscFunctionBegin; 3670 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3671 PetscValidType(mat,1); 3672 PetscValidHeaderSpecific(y,VEC_CLASSID,2); 3673 PetscValidHeaderSpecific(b,VEC_CLASSID,3); 3674 PetscValidHeaderSpecific(x,VEC_CLASSID,4); 3675 PetscCheckSameComm(mat,1,b,2); 3676 PetscCheckSameComm(mat,1,y,2); 3677 PetscCheckSameComm(mat,1,x,3); 3678 if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 3679 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); 3680 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); 3681 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); 3682 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); 3683 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); 3684 if (!mat->rmap->N && !mat->cmap->N) PetscFunctionReturn(0); 3685 if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 3686 MatCheckPreallocated(mat,1); 3687 3688 ierr = PetscLogEventBegin(MAT_SolveAdd,mat,b,x,y);CHKERRQ(ierr); 3689 if (mat->ops->solveadd) { 3690 ierr = (*mat->ops->solveadd)(mat,b,y,x);CHKERRQ(ierr); 3691 } else { 3692 /* do the solve then the add manually */ 3693 if (x != y) { 3694 ierr = MatSolve(mat,b,x);CHKERRQ(ierr); 3695 ierr = VecAXPY(x,one,y);CHKERRQ(ierr); 3696 } else { 3697 ierr = VecDuplicate(x,&tmp);CHKERRQ(ierr); 3698 ierr = PetscLogObjectParent((PetscObject)mat,(PetscObject)tmp);CHKERRQ(ierr); 3699 ierr = VecCopy(x,tmp);CHKERRQ(ierr); 3700 ierr = MatSolve(mat,b,x);CHKERRQ(ierr); 3701 ierr = VecAXPY(x,one,tmp);CHKERRQ(ierr); 3702 ierr = VecDestroy(&tmp);CHKERRQ(ierr); 3703 } 3704 } 3705 ierr = PetscLogEventEnd(MAT_SolveAdd,mat,b,x,y);CHKERRQ(ierr); 3706 ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr); 3707 PetscFunctionReturn(0); 3708 } 3709 3710 /*@ 3711 MatSolveTranspose - Solves A' x = b, given a factored matrix. 3712 3713 Neighbor-wise Collective on Mat and Vec 3714 3715 Input Parameters: 3716 + mat - the factored matrix 3717 - b - the right-hand-side vector 3718 3719 Output Parameter: 3720 . x - the result vector 3721 3722 Notes: 3723 The vectors b and x cannot be the same. I.e., one cannot 3724 call MatSolveTranspose(A,x,x). 3725 3726 Most users should employ the simplified KSP interface for linear solvers 3727 instead of working directly with matrix algebra routines such as this. 3728 See, e.g., KSPCreate(). 3729 3730 Level: developer 3731 3732 Concepts: matrices^triangular solves 3733 3734 .seealso: MatSolve(), MatSolveAdd(), MatSolveTransposeAdd() 3735 @*/ 3736 PetscErrorCode MatSolveTranspose(Mat mat,Vec b,Vec x) 3737 { 3738 PetscErrorCode ierr; 3739 3740 PetscFunctionBegin; 3741 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3742 PetscValidType(mat,1); 3743 PetscValidHeaderSpecific(b,VEC_CLASSID,2); 3744 PetscValidHeaderSpecific(x,VEC_CLASSID,3); 3745 PetscCheckSameComm(mat,1,b,2); 3746 PetscCheckSameComm(mat,1,x,3); 3747 if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 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 if (!mat->ops->solvetranspose) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s",((PetscObject)mat)->type_name); 3759 ierr = (*mat->ops->solvetranspose)(mat,b,x);CHKERRQ(ierr); 3760 } 3761 ierr = PetscLogEventEnd(MAT_SolveTranspose,mat,b,x,0);CHKERRQ(ierr); 3762 ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr); 3763 PetscFunctionReturn(0); 3764 } 3765 3766 /*@ 3767 MatSolveTransposeAdd - Computes x = y + inv(Transpose(A)) b, given a 3768 factored matrix. 3769 3770 Neighbor-wise Collective on Mat and Vec 3771 3772 Input Parameters: 3773 + mat - the factored matrix 3774 . b - the right-hand-side vector 3775 - y - the vector to be added to 3776 3777 Output Parameter: 3778 . x - the result vector 3779 3780 Notes: 3781 The vectors b and x cannot be the same. I.e., one cannot 3782 call MatSolveTransposeAdd(A,x,y,x). 3783 3784 Most users should employ the simplified KSP interface for linear solvers 3785 instead of working directly with matrix algebra routines such as this. 3786 See, e.g., KSPCreate(). 3787 3788 Level: developer 3789 3790 Concepts: matrices^triangular solves 3791 3792 .seealso: MatSolve(), MatSolveAdd(), MatSolveTranspose() 3793 @*/ 3794 PetscErrorCode MatSolveTransposeAdd(Mat mat,Vec b,Vec y,Vec x) 3795 { 3796 PetscScalar one = 1.0; 3797 PetscErrorCode ierr; 3798 Vec tmp; 3799 3800 PetscFunctionBegin; 3801 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3802 PetscValidType(mat,1); 3803 PetscValidHeaderSpecific(y,VEC_CLASSID,2); 3804 PetscValidHeaderSpecific(b,VEC_CLASSID,3); 3805 PetscValidHeaderSpecific(x,VEC_CLASSID,4); 3806 PetscCheckSameComm(mat,1,b,2); 3807 PetscCheckSameComm(mat,1,y,3); 3808 PetscCheckSameComm(mat,1,x,4); 3809 if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 3810 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); 3811 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); 3812 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); 3813 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); 3814 if (!mat->rmap->N && !mat->cmap->N) PetscFunctionReturn(0); 3815 if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 3816 MatCheckPreallocated(mat,1); 3817 3818 ierr = PetscLogEventBegin(MAT_SolveTransposeAdd,mat,b,x,y);CHKERRQ(ierr); 3819 if (mat->ops->solvetransposeadd) { 3820 if (mat->factorerrortype) { 3821 ierr = PetscInfo1(mat,"MatFactorError %D\n",mat->factorerrortype);CHKERRQ(ierr); 3822 ierr = VecSetInf(x);CHKERRQ(ierr); 3823 } else { 3824 ierr = (*mat->ops->solvetransposeadd)(mat,b,y,x);CHKERRQ(ierr); 3825 } 3826 } else { 3827 /* do the solve then the add manually */ 3828 if (x != y) { 3829 ierr = MatSolveTranspose(mat,b,x);CHKERRQ(ierr); 3830 ierr = VecAXPY(x,one,y);CHKERRQ(ierr); 3831 } else { 3832 ierr = VecDuplicate(x,&tmp);CHKERRQ(ierr); 3833 ierr = PetscLogObjectParent((PetscObject)mat,(PetscObject)tmp);CHKERRQ(ierr); 3834 ierr = VecCopy(x,tmp);CHKERRQ(ierr); 3835 ierr = MatSolveTranspose(mat,b,x);CHKERRQ(ierr); 3836 ierr = VecAXPY(x,one,tmp);CHKERRQ(ierr); 3837 ierr = VecDestroy(&tmp);CHKERRQ(ierr); 3838 } 3839 } 3840 ierr = PetscLogEventEnd(MAT_SolveTransposeAdd,mat,b,x,y);CHKERRQ(ierr); 3841 ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr); 3842 PetscFunctionReturn(0); 3843 } 3844 /* ----------------------------------------------------------------*/ 3845 3846 /*@ 3847 MatSOR - Computes relaxation (SOR, Gauss-Seidel) sweeps. 3848 3849 Neighbor-wise Collective on Mat and Vec 3850 3851 Input Parameters: 3852 + mat - the matrix 3853 . b - the right hand side 3854 . omega - the relaxation factor 3855 . flag - flag indicating the type of SOR (see below) 3856 . shift - diagonal shift 3857 . its - the number of iterations 3858 - lits - the number of local iterations 3859 3860 Output Parameters: 3861 . x - the solution (can contain an initial guess, use option SOR_ZERO_INITIAL_GUESS to indicate no guess) 3862 3863 SOR Flags: 3864 . SOR_FORWARD_SWEEP - forward SOR 3865 . SOR_BACKWARD_SWEEP - backward SOR 3866 . SOR_SYMMETRIC_SWEEP - SSOR (symmetric SOR) 3867 . SOR_LOCAL_FORWARD_SWEEP - local forward SOR 3868 . SOR_LOCAL_BACKWARD_SWEEP - local forward SOR 3869 . SOR_LOCAL_SYMMETRIC_SWEEP - local SSOR 3870 . SOR_APPLY_UPPER, SOR_APPLY_LOWER - applies 3871 upper/lower triangular part of matrix to 3872 vector (with omega) 3873 . SOR_ZERO_INITIAL_GUESS - zero initial guess 3874 3875 Notes: 3876 SOR_LOCAL_FORWARD_SWEEP, SOR_LOCAL_BACKWARD_SWEEP, and 3877 SOR_LOCAL_SYMMETRIC_SWEEP perform separate independent smoothings 3878 on each processor. 3879 3880 Application programmers will not generally use MatSOR() directly, 3881 but instead will employ the KSP/PC interface. 3882 3883 Notes: 3884 for BAIJ, SBAIJ, and AIJ matrices with Inodes this does a block SOR smoothing, otherwise it does a pointwise smoothing 3885 3886 Notes for Advanced Users: 3887 The flags are implemented as bitwise inclusive or operations. 3888 For example, use (SOR_ZERO_INITIAL_GUESS | SOR_SYMMETRIC_SWEEP) 3889 to specify a zero initial guess for SSOR. 3890 3891 Most users should employ the simplified KSP interface for linear solvers 3892 instead of working directly with matrix algebra routines such as this. 3893 See, e.g., KSPCreate(). 3894 3895 Vectors x and b CANNOT be the same 3896 3897 Developer Note: We should add block SOR support for AIJ matrices with block size set to great than one and no inodes 3898 3899 Level: developer 3900 3901 Concepts: matrices^relaxation 3902 Concepts: matrices^SOR 3903 Concepts: matrices^Gauss-Seidel 3904 3905 @*/ 3906 PetscErrorCode MatSOR(Mat mat,Vec b,PetscReal omega,MatSORType flag,PetscReal shift,PetscInt its,PetscInt lits,Vec x) 3907 { 3908 PetscErrorCode ierr; 3909 3910 PetscFunctionBegin; 3911 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3912 PetscValidType(mat,1); 3913 PetscValidHeaderSpecific(b,VEC_CLASSID,2); 3914 PetscValidHeaderSpecific(x,VEC_CLASSID,8); 3915 PetscCheckSameComm(mat,1,b,2); 3916 PetscCheckSameComm(mat,1,x,8); 3917 if (!mat->ops->sor) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 3918 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3919 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3920 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); 3921 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); 3922 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); 3923 if (its <= 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Relaxation requires global its %D positive",its); 3924 if (lits <= 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Relaxation requires local its %D positive",lits); 3925 if (b == x) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"b and x vector cannot be the same"); 3926 3927 MatCheckPreallocated(mat,1); 3928 ierr = PetscLogEventBegin(MAT_SOR,mat,b,x,0);CHKERRQ(ierr); 3929 ierr =(*mat->ops->sor)(mat,b,omega,flag,shift,its,lits,x);CHKERRQ(ierr); 3930 ierr = PetscLogEventEnd(MAT_SOR,mat,b,x,0);CHKERRQ(ierr); 3931 ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr); 3932 PetscFunctionReturn(0); 3933 } 3934 3935 /* 3936 Default matrix copy routine. 3937 */ 3938 PetscErrorCode MatCopy_Basic(Mat A,Mat B,MatStructure str) 3939 { 3940 PetscErrorCode ierr; 3941 PetscInt i,rstart = 0,rend = 0,nz; 3942 const PetscInt *cwork; 3943 const PetscScalar *vwork; 3944 3945 PetscFunctionBegin; 3946 if (B->assembled) { 3947 ierr = MatZeroEntries(B);CHKERRQ(ierr); 3948 } 3949 ierr = MatGetOwnershipRange(A,&rstart,&rend);CHKERRQ(ierr); 3950 for (i=rstart; i<rend; i++) { 3951 ierr = MatGetRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr); 3952 ierr = MatSetValues(B,1,&i,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr); 3953 ierr = MatRestoreRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr); 3954 } 3955 ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3956 ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3957 PetscFunctionReturn(0); 3958 } 3959 3960 /*@ 3961 MatCopy - Copys a matrix to another matrix. 3962 3963 Collective on Mat 3964 3965 Input Parameters: 3966 + A - the matrix 3967 - str - SAME_NONZERO_PATTERN or DIFFERENT_NONZERO_PATTERN 3968 3969 Output Parameter: 3970 . B - where the copy is put 3971 3972 Notes: 3973 If you use SAME_NONZERO_PATTERN then the two matrices had better have the 3974 same nonzero pattern or the routine will crash. 3975 3976 MatCopy() copies the matrix entries of a matrix to another existing 3977 matrix (after first zeroing the second matrix). A related routine is 3978 MatConvert(), which first creates a new matrix and then copies the data. 3979 3980 Level: intermediate 3981 3982 Concepts: matrices^copying 3983 3984 .seealso: MatConvert(), MatDuplicate() 3985 3986 @*/ 3987 PetscErrorCode MatCopy(Mat A,Mat B,MatStructure str) 3988 { 3989 PetscErrorCode ierr; 3990 PetscInt i; 3991 3992 PetscFunctionBegin; 3993 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 3994 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 3995 PetscValidType(A,1); 3996 PetscValidType(B,2); 3997 PetscCheckSameComm(A,1,B,2); 3998 MatCheckPreallocated(B,2); 3999 if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4000 if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4001 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); 4002 MatCheckPreallocated(A,1); 4003 if (A == B) PetscFunctionReturn(0); 4004 4005 ierr = PetscLogEventBegin(MAT_Copy,A,B,0,0);CHKERRQ(ierr); 4006 if (A->ops->copy) { 4007 ierr = (*A->ops->copy)(A,B,str);CHKERRQ(ierr); 4008 } else { /* generic conversion */ 4009 ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr); 4010 } 4011 4012 B->stencil.dim = A->stencil.dim; 4013 B->stencil.noc = A->stencil.noc; 4014 for (i=0; i<=A->stencil.dim; i++) { 4015 B->stencil.dims[i] = A->stencil.dims[i]; 4016 B->stencil.starts[i] = A->stencil.starts[i]; 4017 } 4018 4019 ierr = PetscLogEventEnd(MAT_Copy,A,B,0,0);CHKERRQ(ierr); 4020 ierr = PetscObjectStateIncrease((PetscObject)B);CHKERRQ(ierr); 4021 PetscFunctionReturn(0); 4022 } 4023 4024 /*@C 4025 MatConvert - Converts a matrix to another matrix, either of the same 4026 or different type. 4027 4028 Collective on Mat 4029 4030 Input Parameters: 4031 + mat - the matrix 4032 . newtype - new matrix type. Use MATSAME to create a new matrix of the 4033 same type as the original matrix. 4034 - reuse - denotes if the destination matrix is to be created or reused. 4035 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 4036 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). 4037 4038 Output Parameter: 4039 . M - pointer to place new matrix 4040 4041 Notes: 4042 MatConvert() first creates a new matrix and then copies the data from 4043 the first matrix. A related routine is MatCopy(), which copies the matrix 4044 entries of one matrix to another already existing matrix context. 4045 4046 Cannot be used to convert a sequential matrix to parallel or parallel to sequential, 4047 the MPI communicator of the generated matrix is always the same as the communicator 4048 of the input matrix. 4049 4050 Level: intermediate 4051 4052 Concepts: matrices^converting between storage formats 4053 4054 .seealso: MatCopy(), MatDuplicate() 4055 @*/ 4056 PetscErrorCode MatConvert(Mat mat, MatType newtype,MatReuse reuse,Mat *M) 4057 { 4058 PetscErrorCode ierr; 4059 PetscBool sametype,issame,flg; 4060 char convname[256],mtype[256]; 4061 Mat B; 4062 4063 PetscFunctionBegin; 4064 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4065 PetscValidType(mat,1); 4066 PetscValidPointer(M,3); 4067 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4068 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4069 MatCheckPreallocated(mat,1); 4070 4071 ierr = PetscOptionsGetString(((PetscObject)mat)->options,((PetscObject)mat)->prefix,"-matconvert_type",mtype,256,&flg);CHKERRQ(ierr); 4072 if (flg) { 4073 newtype = mtype; 4074 } 4075 ierr = PetscObjectTypeCompare((PetscObject)mat,newtype,&sametype);CHKERRQ(ierr); 4076 ierr = PetscStrcmp(newtype,"same",&issame);CHKERRQ(ierr); 4077 if ((reuse == MAT_INPLACE_MATRIX) && (mat != *M)) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"MAT_INPLACE_MATRIX requires same input and output matrix"); 4078 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"); 4079 4080 if ((reuse == MAT_INPLACE_MATRIX) && (issame || sametype)) PetscFunctionReturn(0); 4081 4082 if ((sametype || issame) && (reuse==MAT_INITIAL_MATRIX) && mat->ops->duplicate) { 4083 ierr = (*mat->ops->duplicate)(mat,MAT_COPY_VALUES,M);CHKERRQ(ierr); 4084 } else { 4085 PetscErrorCode (*conv)(Mat, MatType,MatReuse,Mat*)=NULL; 4086 const char *prefix[3] = {"seq","mpi",""}; 4087 PetscInt i; 4088 /* 4089 Order of precedence: 4090 1) See if a specialized converter is known to the current matrix. 4091 2) See if a specialized converter is known to the desired matrix class. 4092 3) See if a good general converter is registered for the desired class 4093 (as of 6/27/03 only MATMPIADJ falls into this category). 4094 4) See if a good general converter is known for the current matrix. 4095 5) Use a really basic converter. 4096 */ 4097 4098 /* 1) See if a specialized converter is known to the current matrix and the desired class */ 4099 for (i=0; i<3; i++) { 4100 ierr = PetscStrncpy(convname,"MatConvert_",sizeof(convname));CHKERRQ(ierr); 4101 ierr = PetscStrlcat(convname,((PetscObject)mat)->type_name,sizeof(convname));CHKERRQ(ierr); 4102 ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr); 4103 ierr = PetscStrlcat(convname,prefix[i],sizeof(convname));CHKERRQ(ierr); 4104 ierr = PetscStrlcat(convname,issame ? ((PetscObject)mat)->type_name : newtype,sizeof(convname));CHKERRQ(ierr); 4105 ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr); 4106 ierr = PetscObjectQueryFunction((PetscObject)mat,convname,&conv);CHKERRQ(ierr); 4107 if (conv) goto foundconv; 4108 } 4109 4110 /* 2) See if a specialized converter is known to the desired matrix class. */ 4111 ierr = MatCreate(PetscObjectComm((PetscObject)mat),&B);CHKERRQ(ierr); 4112 ierr = MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->N,mat->cmap->N);CHKERRQ(ierr); 4113 ierr = MatSetType(B,newtype);CHKERRQ(ierr); 4114 for (i=0; i<3; i++) { 4115 ierr = PetscStrncpy(convname,"MatConvert_",sizeof(convname));CHKERRQ(ierr); 4116 ierr = PetscStrlcat(convname,((PetscObject)mat)->type_name,sizeof(convname));CHKERRQ(ierr); 4117 ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr); 4118 ierr = PetscStrlcat(convname,prefix[i],sizeof(convname));CHKERRQ(ierr); 4119 ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr); 4120 ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr); 4121 ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr); 4122 if (conv) { 4123 ierr = MatDestroy(&B);CHKERRQ(ierr); 4124 goto foundconv; 4125 } 4126 } 4127 4128 /* 3) See if a good general converter is registered for the desired class */ 4129 conv = B->ops->convertfrom; 4130 ierr = MatDestroy(&B);CHKERRQ(ierr); 4131 if (conv) goto foundconv; 4132 4133 /* 4) See if a good general converter is known for the current matrix */ 4134 if (mat->ops->convert) { 4135 conv = mat->ops->convert; 4136 } 4137 if (conv) goto foundconv; 4138 4139 /* 5) Use a really basic converter. */ 4140 conv = MatConvert_Basic; 4141 4142 foundconv: 4143 ierr = PetscLogEventBegin(MAT_Convert,mat,0,0,0);CHKERRQ(ierr); 4144 ierr = (*conv)(mat,newtype,reuse,M);CHKERRQ(ierr); 4145 if (mat->rmap->mapping && mat->cmap->mapping && !(*M)->rmap->mapping && !(*M)->cmap->mapping) { 4146 /* the block sizes must be same if the mappings are copied over */ 4147 (*M)->rmap->bs = mat->rmap->bs; 4148 (*M)->cmap->bs = mat->cmap->bs; 4149 ierr = PetscObjectReference((PetscObject)mat->rmap->mapping);CHKERRQ(ierr); 4150 ierr = PetscObjectReference((PetscObject)mat->cmap->mapping);CHKERRQ(ierr); 4151 (*M)->rmap->mapping = mat->rmap->mapping; 4152 (*M)->cmap->mapping = mat->cmap->mapping; 4153 } 4154 (*M)->stencil.dim = mat->stencil.dim; 4155 (*M)->stencil.noc = mat->stencil.noc; 4156 for (i=0; i<=mat->stencil.dim; i++) { 4157 (*M)->stencil.dims[i] = mat->stencil.dims[i]; 4158 (*M)->stencil.starts[i] = mat->stencil.starts[i]; 4159 } 4160 ierr = PetscLogEventEnd(MAT_Convert,mat,0,0,0);CHKERRQ(ierr); 4161 } 4162 ierr = PetscObjectStateIncrease((PetscObject)*M);CHKERRQ(ierr); 4163 4164 /* Copy Mat options */ 4165 if (mat->symmetric) {ierr = MatSetOption(*M,MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr);} 4166 if (mat->hermitian) {ierr = MatSetOption(*M,MAT_HERMITIAN,PETSC_TRUE);CHKERRQ(ierr);} 4167 PetscFunctionReturn(0); 4168 } 4169 4170 /*@C 4171 MatFactorGetSolverType - Returns name of the package providing the factorization routines 4172 4173 Not Collective 4174 4175 Input Parameter: 4176 . mat - the matrix, must be a factored matrix 4177 4178 Output Parameter: 4179 . type - the string name of the package (do not free this string) 4180 4181 Notes: 4182 In Fortran you pass in a empty string and the package name will be copied into it. 4183 (Make sure the string is long enough) 4184 4185 Level: intermediate 4186 4187 .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable(), MatGetFactor() 4188 @*/ 4189 PetscErrorCode MatFactorGetSolverType(Mat mat, MatSolverType *type) 4190 { 4191 PetscErrorCode ierr, (*conv)(Mat,MatSolverType*); 4192 4193 PetscFunctionBegin; 4194 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4195 PetscValidType(mat,1); 4196 if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Only for factored matrix"); 4197 ierr = PetscObjectQueryFunction((PetscObject)mat,"MatFactorGetSolverType_C",&conv);CHKERRQ(ierr); 4198 if (!conv) { 4199 *type = MATSOLVERPETSC; 4200 } else { 4201 ierr = (*conv)(mat,type);CHKERRQ(ierr); 4202 } 4203 PetscFunctionReturn(0); 4204 } 4205 4206 typedef struct _MatSolverTypeForSpecifcType* MatSolverTypeForSpecifcType; 4207 struct _MatSolverTypeForSpecifcType { 4208 MatType mtype; 4209 PetscErrorCode (*getfactor[4])(Mat,MatFactorType,Mat*); 4210 MatSolverTypeForSpecifcType next; 4211 }; 4212 4213 typedef struct _MatSolverTypeHolder* MatSolverTypeHolder; 4214 struct _MatSolverTypeHolder { 4215 char *name; 4216 MatSolverTypeForSpecifcType handlers; 4217 MatSolverTypeHolder next; 4218 }; 4219 4220 static MatSolverTypeHolder MatSolverTypeHolders = NULL; 4221 4222 /*@C 4223 MatSolvePackageRegister - Registers a MatSolverType that works for a particular matrix type 4224 4225 Input Parameters: 4226 + package - name of the package, for example petsc or superlu 4227 . mtype - the matrix type that works with this package 4228 . ftype - the type of factorization supported by the package 4229 - getfactor - routine that will create the factored matrix ready to be used 4230 4231 Level: intermediate 4232 4233 .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable() 4234 @*/ 4235 PetscErrorCode MatSolverTypeRegister(MatSolverType package,MatType mtype,MatFactorType ftype,PetscErrorCode (*getfactor)(Mat,MatFactorType,Mat*)) 4236 { 4237 PetscErrorCode ierr; 4238 MatSolverTypeHolder next = MatSolverTypeHolders,prev; 4239 PetscBool flg; 4240 MatSolverTypeForSpecifcType inext,iprev = NULL; 4241 4242 PetscFunctionBegin; 4243 if (!next) { 4244 ierr = PetscNew(&MatSolverTypeHolders);CHKERRQ(ierr); 4245 ierr = PetscStrallocpy(package,&MatSolverTypeHolders->name);CHKERRQ(ierr); 4246 ierr = PetscNew(&MatSolverTypeHolders->handlers);CHKERRQ(ierr); 4247 ierr = PetscStrallocpy(mtype,(char **)&MatSolverTypeHolders->handlers->mtype);CHKERRQ(ierr); 4248 MatSolverTypeHolders->handlers->getfactor[(int)ftype-1] = getfactor; 4249 PetscFunctionReturn(0); 4250 } 4251 while (next) { 4252 ierr = PetscStrcasecmp(package,next->name,&flg);CHKERRQ(ierr); 4253 if (flg) { 4254 if (!next->handlers) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"MatSolverTypeHolder is missing handlers"); 4255 inext = next->handlers; 4256 while (inext) { 4257 ierr = PetscStrcasecmp(mtype,inext->mtype,&flg);CHKERRQ(ierr); 4258 if (flg) { 4259 inext->getfactor[(int)ftype-1] = getfactor; 4260 PetscFunctionReturn(0); 4261 } 4262 iprev = inext; 4263 inext = inext->next; 4264 } 4265 ierr = PetscNew(&iprev->next);CHKERRQ(ierr); 4266 ierr = PetscStrallocpy(mtype,(char **)&iprev->next->mtype);CHKERRQ(ierr); 4267 iprev->next->getfactor[(int)ftype-1] = getfactor; 4268 PetscFunctionReturn(0); 4269 } 4270 prev = next; 4271 next = next->next; 4272 } 4273 ierr = PetscNew(&prev->next);CHKERRQ(ierr); 4274 ierr = PetscStrallocpy(package,&prev->next->name);CHKERRQ(ierr); 4275 ierr = PetscNew(&prev->next->handlers);CHKERRQ(ierr); 4276 ierr = PetscStrallocpy(mtype,(char **)&prev->next->handlers->mtype);CHKERRQ(ierr); 4277 prev->next->handlers->getfactor[(int)ftype-1] = getfactor; 4278 PetscFunctionReturn(0); 4279 } 4280 4281 /*@C 4282 MatSolvePackageGet - Get's the function that creates the factor matrix if it exist 4283 4284 Input Parameters: 4285 + package - name of the package, for example petsc or superlu 4286 . ftype - the type of factorization supported by the package 4287 - mtype - the matrix type that works with this package 4288 4289 Output Parameters: 4290 + foundpackage - PETSC_TRUE if the package was registered 4291 . foundmtype - PETSC_TRUE if the package supports the requested mtype 4292 - getfactor - routine that will create the factored matrix ready to be used or NULL if not found 4293 4294 Level: intermediate 4295 4296 .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable() 4297 @*/ 4298 PetscErrorCode MatSolverTypeGet(MatSolverType package,MatType mtype,MatFactorType ftype,PetscBool *foundpackage,PetscBool *foundmtype,PetscErrorCode (**getfactor)(Mat,MatFactorType,Mat*)) 4299 { 4300 PetscErrorCode ierr; 4301 MatSolverTypeHolder next = MatSolverTypeHolders; 4302 PetscBool flg; 4303 MatSolverTypeForSpecifcType inext; 4304 4305 PetscFunctionBegin; 4306 if (foundpackage) *foundpackage = PETSC_FALSE; 4307 if (foundmtype) *foundmtype = PETSC_FALSE; 4308 if (getfactor) *getfactor = NULL; 4309 4310 if (package) { 4311 while (next) { 4312 ierr = PetscStrcasecmp(package,next->name,&flg);CHKERRQ(ierr); 4313 if (flg) { 4314 if (foundpackage) *foundpackage = PETSC_TRUE; 4315 inext = next->handlers; 4316 while (inext) { 4317 ierr = PetscStrbeginswith(mtype,inext->mtype,&flg);CHKERRQ(ierr); 4318 if (flg) { 4319 if (foundmtype) *foundmtype = PETSC_TRUE; 4320 if (getfactor) *getfactor = inext->getfactor[(int)ftype-1]; 4321 PetscFunctionReturn(0); 4322 } 4323 inext = inext->next; 4324 } 4325 } 4326 next = next->next; 4327 } 4328 } else { 4329 while (next) { 4330 inext = next->handlers; 4331 while (inext) { 4332 ierr = PetscStrbeginswith(mtype,inext->mtype,&flg);CHKERRQ(ierr); 4333 if (flg && inext->getfactor[(int)ftype-1]) { 4334 if (foundpackage) *foundpackage = PETSC_TRUE; 4335 if (foundmtype) *foundmtype = PETSC_TRUE; 4336 if (getfactor) *getfactor = inext->getfactor[(int)ftype-1]; 4337 PetscFunctionReturn(0); 4338 } 4339 inext = inext->next; 4340 } 4341 next = next->next; 4342 } 4343 } 4344 PetscFunctionReturn(0); 4345 } 4346 4347 PetscErrorCode MatSolverTypeDestroy(void) 4348 { 4349 PetscErrorCode ierr; 4350 MatSolverTypeHolder next = MatSolverTypeHolders,prev; 4351 MatSolverTypeForSpecifcType inext,iprev; 4352 4353 PetscFunctionBegin; 4354 while (next) { 4355 ierr = PetscFree(next->name);CHKERRQ(ierr); 4356 inext = next->handlers; 4357 while (inext) { 4358 ierr = PetscFree(inext->mtype);CHKERRQ(ierr); 4359 iprev = inext; 4360 inext = inext->next; 4361 ierr = PetscFree(iprev);CHKERRQ(ierr); 4362 } 4363 prev = next; 4364 next = next->next; 4365 ierr = PetscFree(prev);CHKERRQ(ierr); 4366 } 4367 MatSolverTypeHolders = NULL; 4368 PetscFunctionReturn(0); 4369 } 4370 4371 /*@C 4372 MatGetFactor - Returns a matrix suitable to calls to MatXXFactorSymbolic() 4373 4374 Collective on Mat 4375 4376 Input Parameters: 4377 + mat - the matrix 4378 . type - name of solver type, for example, superlu, petsc (to use PETSc's default) 4379 - ftype - factor type, MAT_FACTOR_LU, MAT_FACTOR_CHOLESKY, MAT_FACTOR_ICC, MAT_FACTOR_ILU, 4380 4381 Output Parameters: 4382 . f - the factor matrix used with MatXXFactorSymbolic() calls 4383 4384 Notes: 4385 Some PETSc matrix formats have alternative solvers available that are contained in alternative packages 4386 such as pastix, superlu, mumps etc. 4387 4388 PETSc must have been ./configure to use the external solver, using the option --download-package 4389 4390 Level: intermediate 4391 4392 .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable() 4393 @*/ 4394 PetscErrorCode MatGetFactor(Mat mat, MatSolverType type,MatFactorType ftype,Mat *f) 4395 { 4396 PetscErrorCode ierr,(*conv)(Mat,MatFactorType,Mat*); 4397 PetscBool foundpackage,foundmtype; 4398 4399 PetscFunctionBegin; 4400 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4401 PetscValidType(mat,1); 4402 4403 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4404 MatCheckPreallocated(mat,1); 4405 4406 ierr = MatSolverTypeGet(type,((PetscObject)mat)->type_name,ftype,&foundpackage,&foundmtype,&conv);CHKERRQ(ierr); 4407 if (!foundpackage) { 4408 if (type) { 4409 SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_MISSING_FACTOR,"Could not locate solver package %s. Perhaps you must ./configure with --download-%s",type,type); 4410 } else { 4411 SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_MISSING_FACTOR,"Could not locate a solver package. Perhaps you must ./configure with --download-<package>"); 4412 } 4413 } 4414 4415 if (!foundmtype) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_MISSING_FACTOR,"MatSolverType %s does not support matrix type %s",type,((PetscObject)mat)->type_name); 4416 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); 4417 4418 #if defined(PETSC_USE_COMPLEX) 4419 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"); 4420 #endif 4421 4422 ierr = (*conv)(mat,ftype,f);CHKERRQ(ierr); 4423 PetscFunctionReturn(0); 4424 } 4425 4426 /*@C 4427 MatGetFactorAvailable - Returns a a flag if matrix supports particular package and factor type 4428 4429 Not Collective 4430 4431 Input Parameters: 4432 + mat - the matrix 4433 . type - name of solver type, for example, superlu, petsc (to use PETSc's default) 4434 - ftype - factor type, MAT_FACTOR_LU, MAT_FACTOR_CHOLESKY, MAT_FACTOR_ICC, MAT_FACTOR_ILU, 4435 4436 Output Parameter: 4437 . flg - PETSC_TRUE if the factorization is available 4438 4439 Notes: 4440 Some PETSc matrix formats have alternative solvers available that are contained in alternative packages 4441 such as pastix, superlu, mumps etc. 4442 4443 PETSc must have been ./configure to use the external solver, using the option --download-package 4444 4445 Level: intermediate 4446 4447 .seealso: MatCopy(), MatDuplicate(), MatGetFactor() 4448 @*/ 4449 PetscErrorCode MatGetFactorAvailable(Mat mat, MatSolverType type,MatFactorType ftype,PetscBool *flg) 4450 { 4451 PetscErrorCode ierr, (*gconv)(Mat,MatFactorType,Mat*); 4452 4453 PetscFunctionBegin; 4454 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4455 PetscValidType(mat,1); 4456 4457 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4458 MatCheckPreallocated(mat,1); 4459 4460 *flg = PETSC_FALSE; 4461 ierr = MatSolverTypeGet(type,((PetscObject)mat)->type_name,ftype,NULL,NULL,&gconv);CHKERRQ(ierr); 4462 if (gconv) { 4463 *flg = PETSC_TRUE; 4464 } 4465 PetscFunctionReturn(0); 4466 } 4467 4468 #include <petscdmtypes.h> 4469 4470 /*@ 4471 MatDuplicate - Duplicates a matrix including the non-zero structure. 4472 4473 Collective on Mat 4474 4475 Input Parameters: 4476 + mat - the matrix 4477 - op - One of MAT_DO_NOT_COPY_VALUES, MAT_COPY_VALUES, or MAT_SHARE_NONZERO_PATTERN. 4478 See the manual page for MatDuplicateOption for an explanation of these options. 4479 4480 Output Parameter: 4481 . M - pointer to place new matrix 4482 4483 Level: intermediate 4484 4485 Concepts: matrices^duplicating 4486 4487 Notes: 4488 You cannot change the nonzero pattern for the parent or child matrix if you use MAT_SHARE_NONZERO_PATTERN. 4489 4490 .seealso: MatCopy(), MatConvert(), MatDuplicateOption 4491 @*/ 4492 PetscErrorCode MatDuplicate(Mat mat,MatDuplicateOption op,Mat *M) 4493 { 4494 PetscErrorCode ierr; 4495 Mat B; 4496 PetscInt i; 4497 DM dm; 4498 void (*viewf)(void); 4499 4500 PetscFunctionBegin; 4501 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4502 PetscValidType(mat,1); 4503 PetscValidPointer(M,3); 4504 if (op == MAT_COPY_VALUES && !mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"MAT_COPY_VALUES not allowed for unassembled matrix"); 4505 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4506 MatCheckPreallocated(mat,1); 4507 4508 *M = 0; 4509 if (!mat->ops->duplicate) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Not written for this matrix type"); 4510 ierr = PetscLogEventBegin(MAT_Convert,mat,0,0,0);CHKERRQ(ierr); 4511 ierr = (*mat->ops->duplicate)(mat,op,M);CHKERRQ(ierr); 4512 B = *M; 4513 4514 ierr = MatGetOperation(mat,MATOP_VIEW,&viewf);CHKERRQ(ierr); 4515 if (viewf) { 4516 ierr = MatSetOperation(B,MATOP_VIEW,viewf);CHKERRQ(ierr); 4517 } 4518 4519 B->stencil.dim = mat->stencil.dim; 4520 B->stencil.noc = mat->stencil.noc; 4521 for (i=0; i<=mat->stencil.dim; i++) { 4522 B->stencil.dims[i] = mat->stencil.dims[i]; 4523 B->stencil.starts[i] = mat->stencil.starts[i]; 4524 } 4525 4526 B->nooffproczerorows = mat->nooffproczerorows; 4527 B->nooffprocentries = mat->nooffprocentries; 4528 4529 ierr = PetscObjectQuery((PetscObject) mat, "__PETSc_dm", (PetscObject*) &dm);CHKERRQ(ierr); 4530 if (dm) { 4531 ierr = PetscObjectCompose((PetscObject) B, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr); 4532 } 4533 ierr = PetscLogEventEnd(MAT_Convert,mat,0,0,0);CHKERRQ(ierr); 4534 ierr = PetscObjectStateIncrease((PetscObject)B);CHKERRQ(ierr); 4535 PetscFunctionReturn(0); 4536 } 4537 4538 /*@ 4539 MatGetDiagonal - Gets the diagonal of a matrix. 4540 4541 Logically Collective on Mat and Vec 4542 4543 Input Parameters: 4544 + mat - the matrix 4545 - v - the vector for storing the diagonal 4546 4547 Output Parameter: 4548 . v - the diagonal of the matrix 4549 4550 Level: intermediate 4551 4552 Note: 4553 Currently only correct in parallel for square matrices. 4554 4555 Concepts: matrices^accessing diagonals 4556 4557 .seealso: MatGetRow(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMaxAbs() 4558 @*/ 4559 PetscErrorCode MatGetDiagonal(Mat mat,Vec v) 4560 { 4561 PetscErrorCode ierr; 4562 4563 PetscFunctionBegin; 4564 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4565 PetscValidType(mat,1); 4566 PetscValidHeaderSpecific(v,VEC_CLASSID,2); 4567 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4568 if (!mat->ops->getdiagonal) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 4569 MatCheckPreallocated(mat,1); 4570 4571 ierr = (*mat->ops->getdiagonal)(mat,v);CHKERRQ(ierr); 4572 ierr = PetscObjectStateIncrease((PetscObject)v);CHKERRQ(ierr); 4573 PetscFunctionReturn(0); 4574 } 4575 4576 /*@C 4577 MatGetRowMin - Gets the minimum value (of the real part) of each 4578 row of the matrix 4579 4580 Logically Collective on Mat and Vec 4581 4582 Input Parameters: 4583 . mat - the matrix 4584 4585 Output Parameter: 4586 + v - the vector for storing the maximums 4587 - idx - the indices of the column found for each row (optional) 4588 4589 Level: intermediate 4590 4591 Notes: 4592 The result of this call are the same as if one converted the matrix to dense format 4593 and found the minimum value in each row (i.e. the implicit zeros are counted as zeros). 4594 4595 This code is only implemented for a couple of matrix formats. 4596 4597 Concepts: matrices^getting row maximums 4598 4599 .seealso: MatGetDiagonal(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMaxAbs(), 4600 MatGetRowMax() 4601 @*/ 4602 PetscErrorCode MatGetRowMin(Mat mat,Vec v,PetscInt idx[]) 4603 { 4604 PetscErrorCode ierr; 4605 4606 PetscFunctionBegin; 4607 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4608 PetscValidType(mat,1); 4609 PetscValidHeaderSpecific(v,VEC_CLASSID,2); 4610 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4611 if (!mat->ops->getrowmax) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 4612 MatCheckPreallocated(mat,1); 4613 4614 ierr = (*mat->ops->getrowmin)(mat,v,idx);CHKERRQ(ierr); 4615 ierr = PetscObjectStateIncrease((PetscObject)v);CHKERRQ(ierr); 4616 PetscFunctionReturn(0); 4617 } 4618 4619 /*@C 4620 MatGetRowMinAbs - Gets the minimum value (in absolute value) of each 4621 row of the matrix 4622 4623 Logically Collective on Mat and Vec 4624 4625 Input Parameters: 4626 . mat - the matrix 4627 4628 Output Parameter: 4629 + v - the vector for storing the minimums 4630 - idx - the indices of the column found for each row (or NULL if not needed) 4631 4632 Level: intermediate 4633 4634 Notes: 4635 if a row is completely empty or has only 0.0 values then the idx[] value for that 4636 row is 0 (the first column). 4637 4638 This code is only implemented for a couple of matrix formats. 4639 4640 Concepts: matrices^getting row maximums 4641 4642 .seealso: MatGetDiagonal(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMax(), MatGetRowMaxAbs(), MatGetRowMin() 4643 @*/ 4644 PetscErrorCode MatGetRowMinAbs(Mat mat,Vec v,PetscInt idx[]) 4645 { 4646 PetscErrorCode ierr; 4647 4648 PetscFunctionBegin; 4649 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4650 PetscValidType(mat,1); 4651 PetscValidHeaderSpecific(v,VEC_CLASSID,2); 4652 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4653 if (!mat->ops->getrowminabs) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 4654 MatCheckPreallocated(mat,1); 4655 if (idx) {ierr = PetscMemzero(idx,mat->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);} 4656 4657 ierr = (*mat->ops->getrowminabs)(mat,v,idx);CHKERRQ(ierr); 4658 ierr = PetscObjectStateIncrease((PetscObject)v);CHKERRQ(ierr); 4659 PetscFunctionReturn(0); 4660 } 4661 4662 /*@C 4663 MatGetRowMax - Gets the maximum value (of the real part) of each 4664 row of the matrix 4665 4666 Logically Collective on Mat and Vec 4667 4668 Input Parameters: 4669 . mat - the matrix 4670 4671 Output Parameter: 4672 + v - the vector for storing the maximums 4673 - idx - the indices of the column found for each row (optional) 4674 4675 Level: intermediate 4676 4677 Notes: 4678 The result of this call are the same as if one converted the matrix to dense format 4679 and found the minimum value in each row (i.e. the implicit zeros are counted as zeros). 4680 4681 This code is only implemented for a couple of matrix formats. 4682 4683 Concepts: matrices^getting row maximums 4684 4685 .seealso: MatGetDiagonal(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMaxAbs(), MatGetRowMin() 4686 @*/ 4687 PetscErrorCode MatGetRowMax(Mat mat,Vec v,PetscInt idx[]) 4688 { 4689 PetscErrorCode ierr; 4690 4691 PetscFunctionBegin; 4692 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4693 PetscValidType(mat,1); 4694 PetscValidHeaderSpecific(v,VEC_CLASSID,2); 4695 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4696 if (!mat->ops->getrowmax) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 4697 MatCheckPreallocated(mat,1); 4698 4699 ierr = (*mat->ops->getrowmax)(mat,v,idx);CHKERRQ(ierr); 4700 ierr = PetscObjectStateIncrease((PetscObject)v);CHKERRQ(ierr); 4701 PetscFunctionReturn(0); 4702 } 4703 4704 /*@C 4705 MatGetRowMaxAbs - Gets the maximum value (in absolute value) of each 4706 row of the matrix 4707 4708 Logically Collective on Mat and Vec 4709 4710 Input Parameters: 4711 . mat - the matrix 4712 4713 Output Parameter: 4714 + v - the vector for storing the maximums 4715 - idx - the indices of the column found for each row (or NULL if not needed) 4716 4717 Level: intermediate 4718 4719 Notes: 4720 if a row is completely empty or has only 0.0 values then the idx[] value for that 4721 row is 0 (the first column). 4722 4723 This code is only implemented for a couple of matrix formats. 4724 4725 Concepts: matrices^getting row maximums 4726 4727 .seealso: MatGetDiagonal(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMax(), MatGetRowMin() 4728 @*/ 4729 PetscErrorCode MatGetRowMaxAbs(Mat mat,Vec v,PetscInt idx[]) 4730 { 4731 PetscErrorCode ierr; 4732 4733 PetscFunctionBegin; 4734 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4735 PetscValidType(mat,1); 4736 PetscValidHeaderSpecific(v,VEC_CLASSID,2); 4737 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4738 if (!mat->ops->getrowmaxabs) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 4739 MatCheckPreallocated(mat,1); 4740 if (idx) {ierr = PetscMemzero(idx,mat->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);} 4741 4742 ierr = (*mat->ops->getrowmaxabs)(mat,v,idx);CHKERRQ(ierr); 4743 ierr = PetscObjectStateIncrease((PetscObject)v);CHKERRQ(ierr); 4744 PetscFunctionReturn(0); 4745 } 4746 4747 /*@ 4748 MatGetRowSum - Gets the sum of each row of the matrix 4749 4750 Logically or Neighborhood Collective on Mat and Vec 4751 4752 Input Parameters: 4753 . mat - the matrix 4754 4755 Output Parameter: 4756 . v - the vector for storing the sum of rows 4757 4758 Level: intermediate 4759 4760 Notes: 4761 This code is slow since it is not currently specialized for different formats 4762 4763 Concepts: matrices^getting row sums 4764 4765 .seealso: MatGetDiagonal(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMax(), MatGetRowMin() 4766 @*/ 4767 PetscErrorCode MatGetRowSum(Mat mat, Vec v) 4768 { 4769 Vec ones; 4770 PetscErrorCode ierr; 4771 4772 PetscFunctionBegin; 4773 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4774 PetscValidType(mat,1); 4775 PetscValidHeaderSpecific(v,VEC_CLASSID,2); 4776 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4777 MatCheckPreallocated(mat,1); 4778 ierr = MatCreateVecs(mat,&ones,NULL);CHKERRQ(ierr); 4779 ierr = VecSet(ones,1.);CHKERRQ(ierr); 4780 ierr = MatMult(mat,ones,v);CHKERRQ(ierr); 4781 ierr = VecDestroy(&ones);CHKERRQ(ierr); 4782 PetscFunctionReturn(0); 4783 } 4784 4785 /*@ 4786 MatTranspose - Computes an in-place or out-of-place transpose of a matrix. 4787 4788 Collective on Mat 4789 4790 Input Parameter: 4791 + mat - the matrix to transpose 4792 - reuse - either MAT_INITIAL_MATRIX, MAT_REUSE_MATRIX, or MAT_INPLACE_MATRIX 4793 4794 Output Parameters: 4795 . B - the transpose 4796 4797 Notes: 4798 If you use MAT_INPLACE_MATRIX then you must pass in &mat for B 4799 4800 MAT_REUSE_MATRIX causes the B matrix from a previous call to this function with MAT_INITIAL_MATRIX to be used 4801 4802 Consider using MatCreateTranspose() instead if you only need a matrix that behaves like the transpose, but don't need the storage to be changed. 4803 4804 Level: intermediate 4805 4806 Concepts: matrices^transposing 4807 4808 .seealso: MatMultTranspose(), MatMultTransposeAdd(), MatIsTranspose(), MatReuse 4809 @*/ 4810 PetscErrorCode MatTranspose(Mat mat,MatReuse reuse,Mat *B) 4811 { 4812 PetscErrorCode ierr; 4813 4814 PetscFunctionBegin; 4815 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4816 PetscValidType(mat,1); 4817 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4818 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4819 if (!mat->ops->transpose) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 4820 if (reuse == MAT_INPLACE_MATRIX && mat != *B) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"MAT_INPLACE_MATRIX requires last matrix to match first"); 4821 if (reuse == MAT_REUSE_MATRIX && mat == *B) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Perhaps you mean MAT_INPLACE_MATRIX"); 4822 MatCheckPreallocated(mat,1); 4823 4824 ierr = PetscLogEventBegin(MAT_Transpose,mat,0,0,0);CHKERRQ(ierr); 4825 ierr = (*mat->ops->transpose)(mat,reuse,B);CHKERRQ(ierr); 4826 ierr = PetscLogEventEnd(MAT_Transpose,mat,0,0,0);CHKERRQ(ierr); 4827 if (B) {ierr = PetscObjectStateIncrease((PetscObject)*B);CHKERRQ(ierr);} 4828 PetscFunctionReturn(0); 4829 } 4830 4831 /*@ 4832 MatIsTranspose - Test whether a matrix is another one's transpose, 4833 or its own, in which case it tests symmetry. 4834 4835 Collective on Mat 4836 4837 Input Parameter: 4838 + A - the matrix to test 4839 - B - the matrix to test against, this can equal the first parameter 4840 4841 Output Parameters: 4842 . flg - the result 4843 4844 Notes: 4845 Only available for SeqAIJ/MPIAIJ matrices. The sequential algorithm 4846 has a running time of the order of the number of nonzeros; the parallel 4847 test involves parallel copies of the block-offdiagonal parts of the matrix. 4848 4849 Level: intermediate 4850 4851 Concepts: matrices^transposing, matrix^symmetry 4852 4853 .seealso: MatTranspose(), MatIsSymmetric(), MatIsHermitian() 4854 @*/ 4855 PetscErrorCode MatIsTranspose(Mat A,Mat B,PetscReal tol,PetscBool *flg) 4856 { 4857 PetscErrorCode ierr,(*f)(Mat,Mat,PetscReal,PetscBool*),(*g)(Mat,Mat,PetscReal,PetscBool*); 4858 4859 PetscFunctionBegin; 4860 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 4861 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 4862 PetscValidPointer(flg,3); 4863 ierr = PetscObjectQueryFunction((PetscObject)A,"MatIsTranspose_C",&f);CHKERRQ(ierr); 4864 ierr = PetscObjectQueryFunction((PetscObject)B,"MatIsTranspose_C",&g);CHKERRQ(ierr); 4865 *flg = PETSC_FALSE; 4866 if (f && g) { 4867 if (f == g) { 4868 ierr = (*f)(A,B,tol,flg);CHKERRQ(ierr); 4869 } else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_NOTSAMETYPE,"Matrices do not have the same comparator for symmetry test"); 4870 } else { 4871 MatType mattype; 4872 if (!f) { 4873 ierr = MatGetType(A,&mattype);CHKERRQ(ierr); 4874 } else { 4875 ierr = MatGetType(B,&mattype);CHKERRQ(ierr); 4876 } 4877 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for transpose",mattype); 4878 } 4879 PetscFunctionReturn(0); 4880 } 4881 4882 /*@ 4883 MatHermitianTranspose - Computes an in-place or out-of-place transpose of a matrix in complex conjugate. 4884 4885 Collective on Mat 4886 4887 Input Parameter: 4888 + mat - the matrix to transpose and complex conjugate 4889 - reuse - MAT_INITIAL_MATRIX to create a new matrix, MAT_INPLACE_MATRIX to reuse the first argument to store the transpose 4890 4891 Output Parameters: 4892 . B - the Hermitian 4893 4894 Level: intermediate 4895 4896 Concepts: matrices^transposing, complex conjugatex 4897 4898 .seealso: MatTranspose(), MatMultTranspose(), MatMultTransposeAdd(), MatIsTranspose(), MatReuse 4899 @*/ 4900 PetscErrorCode MatHermitianTranspose(Mat mat,MatReuse reuse,Mat *B) 4901 { 4902 PetscErrorCode ierr; 4903 4904 PetscFunctionBegin; 4905 ierr = MatTranspose(mat,reuse,B);CHKERRQ(ierr); 4906 #if defined(PETSC_USE_COMPLEX) 4907 ierr = MatConjugate(*B);CHKERRQ(ierr); 4908 #endif 4909 PetscFunctionReturn(0); 4910 } 4911 4912 /*@ 4913 MatIsHermitianTranspose - Test whether a matrix is another one's Hermitian transpose, 4914 4915 Collective on Mat 4916 4917 Input Parameter: 4918 + A - the matrix to test 4919 - B - the matrix to test against, this can equal the first parameter 4920 4921 Output Parameters: 4922 . flg - the result 4923 4924 Notes: 4925 Only available for SeqAIJ/MPIAIJ matrices. The sequential algorithm 4926 has a running time of the order of the number of nonzeros; the parallel 4927 test involves parallel copies of the block-offdiagonal parts of the matrix. 4928 4929 Level: intermediate 4930 4931 Concepts: matrices^transposing, matrix^symmetry 4932 4933 .seealso: MatTranspose(), MatIsSymmetric(), MatIsHermitian(), MatIsTranspose() 4934 @*/ 4935 PetscErrorCode MatIsHermitianTranspose(Mat A,Mat B,PetscReal tol,PetscBool *flg) 4936 { 4937 PetscErrorCode ierr,(*f)(Mat,Mat,PetscReal,PetscBool*),(*g)(Mat,Mat,PetscReal,PetscBool*); 4938 4939 PetscFunctionBegin; 4940 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 4941 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 4942 PetscValidPointer(flg,3); 4943 ierr = PetscObjectQueryFunction((PetscObject)A,"MatIsHermitianTranspose_C",&f);CHKERRQ(ierr); 4944 ierr = PetscObjectQueryFunction((PetscObject)B,"MatIsHermitianTranspose_C",&g);CHKERRQ(ierr); 4945 if (f && g) { 4946 if (f==g) { 4947 ierr = (*f)(A,B,tol,flg);CHKERRQ(ierr); 4948 } else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_NOTSAMETYPE,"Matrices do not have the same comparator for Hermitian test"); 4949 } 4950 PetscFunctionReturn(0); 4951 } 4952 4953 /*@ 4954 MatPermute - Creates a new matrix with rows and columns permuted from the 4955 original. 4956 4957 Collective on Mat 4958 4959 Input Parameters: 4960 + mat - the matrix to permute 4961 . row - row permutation, each processor supplies only the permutation for its rows 4962 - col - column permutation, each processor supplies only the permutation for its columns 4963 4964 Output Parameters: 4965 . B - the permuted matrix 4966 4967 Level: advanced 4968 4969 Note: 4970 The index sets map from row/col of permuted matrix to row/col of original matrix. 4971 The index sets should be on the same communicator as Mat and have the same local sizes. 4972 4973 Concepts: matrices^permuting 4974 4975 .seealso: MatGetOrdering(), ISAllGather() 4976 4977 @*/ 4978 PetscErrorCode MatPermute(Mat mat,IS row,IS col,Mat *B) 4979 { 4980 PetscErrorCode ierr; 4981 4982 PetscFunctionBegin; 4983 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 4984 PetscValidType(mat,1); 4985 PetscValidHeaderSpecific(row,IS_CLASSID,2); 4986 PetscValidHeaderSpecific(col,IS_CLASSID,3); 4987 PetscValidPointer(B,4); 4988 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4989 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4990 if (!mat->ops->permute) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatPermute not available for Mat type %s",((PetscObject)mat)->type_name); 4991 MatCheckPreallocated(mat,1); 4992 4993 ierr = (*mat->ops->permute)(mat,row,col,B);CHKERRQ(ierr); 4994 ierr = PetscObjectStateIncrease((PetscObject)*B);CHKERRQ(ierr); 4995 PetscFunctionReturn(0); 4996 } 4997 4998 /*@ 4999 MatEqual - Compares two matrices. 5000 5001 Collective on Mat 5002 5003 Input Parameters: 5004 + A - the first matrix 5005 - B - the second matrix 5006 5007 Output Parameter: 5008 . flg - PETSC_TRUE if the matrices are equal; PETSC_FALSE otherwise. 5009 5010 Level: intermediate 5011 5012 Concepts: matrices^equality between 5013 @*/ 5014 PetscErrorCode MatEqual(Mat A,Mat B,PetscBool *flg) 5015 { 5016 PetscErrorCode ierr; 5017 5018 PetscFunctionBegin; 5019 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 5020 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 5021 PetscValidType(A,1); 5022 PetscValidType(B,2); 5023 PetscValidIntPointer(flg,3); 5024 PetscCheckSameComm(A,1,B,2); 5025 MatCheckPreallocated(B,2); 5026 if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5027 if (!B->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5028 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); 5029 if (!A->ops->equal) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Mat type %s",((PetscObject)A)->type_name); 5030 if (!B->ops->equal) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Mat type %s",((PetscObject)B)->type_name); 5031 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); 5032 MatCheckPreallocated(A,1); 5033 5034 ierr = (*A->ops->equal)(A,B,flg);CHKERRQ(ierr); 5035 PetscFunctionReturn(0); 5036 } 5037 5038 /*@ 5039 MatDiagonalScale - Scales a matrix on the left and right by diagonal 5040 matrices that are stored as vectors. Either of the two scaling 5041 matrices can be NULL. 5042 5043 Collective on Mat 5044 5045 Input Parameters: 5046 + mat - the matrix to be scaled 5047 . l - the left scaling vector (or NULL) 5048 - r - the right scaling vector (or NULL) 5049 5050 Notes: 5051 MatDiagonalScale() computes A = LAR, where 5052 L = a diagonal matrix (stored as a vector), R = a diagonal matrix (stored as a vector) 5053 The L scales the rows of the matrix, the R scales the columns of the matrix. 5054 5055 Level: intermediate 5056 5057 Concepts: matrices^diagonal scaling 5058 Concepts: diagonal scaling of matrices 5059 5060 .seealso: MatScale(), MatShift(), MatDiagonalSet() 5061 @*/ 5062 PetscErrorCode MatDiagonalScale(Mat mat,Vec l,Vec r) 5063 { 5064 PetscErrorCode ierr; 5065 5066 PetscFunctionBegin; 5067 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5068 PetscValidType(mat,1); 5069 if (!mat->ops->diagonalscale) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 5070 if (l) {PetscValidHeaderSpecific(l,VEC_CLASSID,2);PetscCheckSameComm(mat,1,l,2);} 5071 if (r) {PetscValidHeaderSpecific(r,VEC_CLASSID,3);PetscCheckSameComm(mat,1,r,3);} 5072 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5073 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5074 MatCheckPreallocated(mat,1); 5075 5076 ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 5077 ierr = (*mat->ops->diagonalscale)(mat,l,r);CHKERRQ(ierr); 5078 ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 5079 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 5080 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_VECCUDA) 5081 if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) { 5082 mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU; 5083 } 5084 #endif 5085 PetscFunctionReturn(0); 5086 } 5087 5088 /*@ 5089 MatScale - Scales all elements of a matrix by a given number. 5090 5091 Logically Collective on Mat 5092 5093 Input Parameters: 5094 + mat - the matrix to be scaled 5095 - a - the scaling value 5096 5097 Output Parameter: 5098 . mat - the scaled matrix 5099 5100 Level: intermediate 5101 5102 Concepts: matrices^scaling all entries 5103 5104 .seealso: MatDiagonalScale() 5105 @*/ 5106 PetscErrorCode MatScale(Mat mat,PetscScalar a) 5107 { 5108 PetscErrorCode ierr; 5109 5110 PetscFunctionBegin; 5111 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5112 PetscValidType(mat,1); 5113 if (a != (PetscScalar)1.0 && !mat->ops->scale) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 5114 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5115 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5116 PetscValidLogicalCollectiveScalar(mat,a,2); 5117 MatCheckPreallocated(mat,1); 5118 5119 ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 5120 if (a != (PetscScalar)1.0) { 5121 ierr = (*mat->ops->scale)(mat,a);CHKERRQ(ierr); 5122 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 5123 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_VECCUDA) 5124 if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) { 5125 mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU; 5126 } 5127 #endif 5128 } 5129 ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 5130 PetscFunctionReturn(0); 5131 } 5132 5133 static PetscErrorCode MatNorm_Basic(Mat A,NormType type,PetscReal *nrm) 5134 { 5135 PetscErrorCode ierr; 5136 5137 PetscFunctionBegin; 5138 if (type == NORM_1 || type == NORM_INFINITY) { 5139 Vec l,r; 5140 5141 ierr = MatCreateVecs(A,&r,&l);CHKERRQ(ierr); 5142 if (type == NORM_INFINITY) { 5143 ierr = VecSet(r,1.);CHKERRQ(ierr); 5144 ierr = MatMult(A,r,l);CHKERRQ(ierr); 5145 ierr = VecNorm(l,NORM_INFINITY,nrm);CHKERRQ(ierr); 5146 } else { 5147 ierr = VecSet(l,1.);CHKERRQ(ierr); 5148 ierr = MatMultTranspose(A,l,r);CHKERRQ(ierr); 5149 ierr = VecNorm(r,NORM_INFINITY,nrm);CHKERRQ(ierr); 5150 } 5151 ierr = VecDestroy(&l);CHKERRQ(ierr); 5152 ierr = VecDestroy(&r);CHKERRQ(ierr); 5153 } else SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Matrix class %s, norm type %d",((PetscObject)A)->type_name,type); 5154 PetscFunctionReturn(0); 5155 } 5156 5157 /*@ 5158 MatNorm - Calculates various norms of a matrix. 5159 5160 Collective on Mat 5161 5162 Input Parameters: 5163 + mat - the matrix 5164 - type - the type of norm, NORM_1, NORM_FROBENIUS, NORM_INFINITY 5165 5166 Output Parameters: 5167 . nrm - the resulting norm 5168 5169 Level: intermediate 5170 5171 Concepts: matrices^norm 5172 Concepts: norm^of matrix 5173 @*/ 5174 PetscErrorCode MatNorm(Mat mat,NormType type,PetscReal *nrm) 5175 { 5176 PetscErrorCode ierr; 5177 5178 PetscFunctionBegin; 5179 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5180 PetscValidType(mat,1); 5181 PetscValidLogicalCollectiveEnum(mat,type,2); 5182 PetscValidScalarPointer(nrm,3); 5183 5184 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5185 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5186 MatCheckPreallocated(mat,1); 5187 5188 if (!mat->ops->norm) { 5189 ierr = MatNorm_Basic(mat,type,nrm);CHKERRQ(ierr); 5190 } else { 5191 ierr = (*mat->ops->norm)(mat,type,nrm);CHKERRQ(ierr); 5192 } 5193 PetscFunctionReturn(0); 5194 } 5195 5196 /* 5197 This variable is used to prevent counting of MatAssemblyBegin() that 5198 are called from within a MatAssemblyEnd(). 5199 */ 5200 static PetscInt MatAssemblyEnd_InUse = 0; 5201 /*@ 5202 MatAssemblyBegin - Begins assembling the matrix. This routine should 5203 be called after completing all calls to MatSetValues(). 5204 5205 Collective on Mat 5206 5207 Input Parameters: 5208 + mat - the matrix 5209 - type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY 5210 5211 Notes: 5212 MatSetValues() generally caches the values. The matrix is ready to 5213 use only after MatAssemblyBegin() and MatAssemblyEnd() have been called. 5214 Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES 5215 in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before 5216 using the matrix. 5217 5218 ALL processes that share a matrix MUST call MatAssemblyBegin() and MatAssemblyEnd() the SAME NUMBER of times, and each time with the 5219 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 5220 a global collective operation requring all processes that share the matrix. 5221 5222 Space for preallocated nonzeros that is not filled by a call to MatSetValues() or a related routine are compressed 5223 out by assembly. If you intend to use that extra space on a subsequent assembly, be sure to insert explicit zeros 5224 before MAT_FINAL_ASSEMBLY so the space is not compressed out. 5225 5226 Level: beginner 5227 5228 Concepts: matrices^assembling 5229 5230 .seealso: MatAssemblyEnd(), MatSetValues(), MatAssembled() 5231 @*/ 5232 PetscErrorCode MatAssemblyBegin(Mat mat,MatAssemblyType type) 5233 { 5234 PetscErrorCode ierr; 5235 5236 PetscFunctionBegin; 5237 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5238 PetscValidType(mat,1); 5239 MatCheckPreallocated(mat,1); 5240 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix.\nDid you forget to call MatSetUnfactored()?"); 5241 if (mat->assembled) { 5242 mat->was_assembled = PETSC_TRUE; 5243 mat->assembled = PETSC_FALSE; 5244 } 5245 if (!MatAssemblyEnd_InUse) { 5246 ierr = PetscLogEventBegin(MAT_AssemblyBegin,mat,0,0,0);CHKERRQ(ierr); 5247 if (mat->ops->assemblybegin) {ierr = (*mat->ops->assemblybegin)(mat,type);CHKERRQ(ierr);} 5248 ierr = PetscLogEventEnd(MAT_AssemblyBegin,mat,0,0,0);CHKERRQ(ierr); 5249 } else if (mat->ops->assemblybegin) { 5250 ierr = (*mat->ops->assemblybegin)(mat,type);CHKERRQ(ierr); 5251 } 5252 PetscFunctionReturn(0); 5253 } 5254 5255 /*@ 5256 MatAssembled - Indicates if a matrix has been assembled and is ready for 5257 use; for example, in matrix-vector product. 5258 5259 Not Collective 5260 5261 Input Parameter: 5262 . mat - the matrix 5263 5264 Output Parameter: 5265 . assembled - PETSC_TRUE or PETSC_FALSE 5266 5267 Level: advanced 5268 5269 Concepts: matrices^assembled? 5270 5271 .seealso: MatAssemblyEnd(), MatSetValues(), MatAssemblyBegin() 5272 @*/ 5273 PetscErrorCode MatAssembled(Mat mat,PetscBool *assembled) 5274 { 5275 PetscFunctionBegin; 5276 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5277 PetscValidType(mat,1); 5278 PetscValidPointer(assembled,2); 5279 *assembled = mat->assembled; 5280 PetscFunctionReturn(0); 5281 } 5282 5283 /*@ 5284 MatAssemblyEnd - Completes assembling the matrix. This routine should 5285 be called after MatAssemblyBegin(). 5286 5287 Collective on Mat 5288 5289 Input Parameters: 5290 + mat - the matrix 5291 - type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY 5292 5293 Options Database Keys: 5294 + -mat_view ::ascii_info - Prints info on matrix at conclusion of MatEndAssembly() 5295 . -mat_view ::ascii_info_detail - Prints more detailed info 5296 . -mat_view - Prints matrix in ASCII format 5297 . -mat_view ::ascii_matlab - Prints matrix in Matlab format 5298 . -mat_view draw - PetscDraws nonzero structure of matrix, using MatView() and PetscDrawOpenX(). 5299 . -display <name> - Sets display name (default is host) 5300 . -draw_pause <sec> - Sets number of seconds to pause after display 5301 . -mat_view socket - Sends matrix to socket, can be accessed from Matlab (See Users-Manual: ch_matlab ) 5302 . -viewer_socket_machine <machine> - Machine to use for socket 5303 . -viewer_socket_port <port> - Port number to use for socket 5304 - -mat_view binary:filename[:append] - Save matrix to file in binary format 5305 5306 Notes: 5307 MatSetValues() generally caches the values. The matrix is ready to 5308 use only after MatAssemblyBegin() and MatAssemblyEnd() have been called. 5309 Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES 5310 in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before 5311 using the matrix. 5312 5313 Space for preallocated nonzeros that is not filled by a call to MatSetValues() or a related routine are compressed 5314 out by assembly. If you intend to use that extra space on a subsequent assembly, be sure to insert explicit zeros 5315 before MAT_FINAL_ASSEMBLY so the space is not compressed out. 5316 5317 Level: beginner 5318 5319 .seealso: MatAssemblyBegin(), MatSetValues(), PetscDrawOpenX(), PetscDrawCreate(), MatView(), MatAssembled(), PetscViewerSocketOpen() 5320 @*/ 5321 PetscErrorCode MatAssemblyEnd(Mat mat,MatAssemblyType type) 5322 { 5323 PetscErrorCode ierr; 5324 static PetscInt inassm = 0; 5325 PetscBool flg = PETSC_FALSE; 5326 5327 PetscFunctionBegin; 5328 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5329 PetscValidType(mat,1); 5330 5331 inassm++; 5332 MatAssemblyEnd_InUse++; 5333 if (MatAssemblyEnd_InUse == 1) { /* Do the logging only the first time through */ 5334 ierr = PetscLogEventBegin(MAT_AssemblyEnd,mat,0,0,0);CHKERRQ(ierr); 5335 if (mat->ops->assemblyend) { 5336 ierr = (*mat->ops->assemblyend)(mat,type);CHKERRQ(ierr); 5337 } 5338 ierr = PetscLogEventEnd(MAT_AssemblyEnd,mat,0,0,0);CHKERRQ(ierr); 5339 } else if (mat->ops->assemblyend) { 5340 ierr = (*mat->ops->assemblyend)(mat,type);CHKERRQ(ierr); 5341 } 5342 5343 /* Flush assembly is not a true assembly */ 5344 if (type != MAT_FLUSH_ASSEMBLY) { 5345 mat->assembled = PETSC_TRUE; mat->num_ass++; 5346 } 5347 mat->insertmode = NOT_SET_VALUES; 5348 MatAssemblyEnd_InUse--; 5349 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 5350 if (!mat->symmetric_eternal) { 5351 mat->symmetric_set = PETSC_FALSE; 5352 mat->hermitian_set = PETSC_FALSE; 5353 mat->structurally_symmetric_set = PETSC_FALSE; 5354 } 5355 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_VECCUDA) 5356 if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) { 5357 mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU; 5358 } 5359 #endif 5360 if (inassm == 1 && type != MAT_FLUSH_ASSEMBLY) { 5361 ierr = MatViewFromOptions(mat,NULL,"-mat_view");CHKERRQ(ierr); 5362 5363 if (mat->checksymmetryonassembly) { 5364 ierr = MatIsSymmetric(mat,mat->checksymmetrytol,&flg);CHKERRQ(ierr); 5365 if (flg) { 5366 ierr = PetscPrintf(PetscObjectComm((PetscObject)mat),"Matrix is symmetric (tolerance %g)\n",(double)mat->checksymmetrytol);CHKERRQ(ierr); 5367 } else { 5368 ierr = PetscPrintf(PetscObjectComm((PetscObject)mat),"Matrix is not symmetric (tolerance %g)\n",(double)mat->checksymmetrytol);CHKERRQ(ierr); 5369 } 5370 } 5371 if (mat->nullsp && mat->checknullspaceonassembly) { 5372 ierr = MatNullSpaceTest(mat->nullsp,mat,NULL);CHKERRQ(ierr); 5373 } 5374 } 5375 inassm--; 5376 PetscFunctionReturn(0); 5377 } 5378 5379 /*@ 5380 MatSetOption - Sets a parameter option for a matrix. Some options 5381 may be specific to certain storage formats. Some options 5382 determine how values will be inserted (or added). Sorted, 5383 row-oriented input will generally assemble the fastest. The default 5384 is row-oriented. 5385 5386 Logically Collective on Mat for certain operations, such as MAT_SPD, not collective for MAT_ROW_ORIENTED, see MatOption 5387 5388 Input Parameters: 5389 + mat - the matrix 5390 . option - the option, one of those listed below (and possibly others), 5391 - flg - turn the option on (PETSC_TRUE) or off (PETSC_FALSE) 5392 5393 Options Describing Matrix Structure: 5394 + MAT_SPD - symmetric positive definite 5395 . MAT_SYMMETRIC - symmetric in terms of both structure and value 5396 . MAT_HERMITIAN - transpose is the complex conjugation 5397 . MAT_STRUCTURALLY_SYMMETRIC - symmetric nonzero structure 5398 - MAT_SYMMETRY_ETERNAL - if you would like the symmetry/Hermitian flag 5399 you set to be kept with all future use of the matrix 5400 including after MatAssemblyBegin/End() which could 5401 potentially change the symmetry structure, i.e. you 5402 KNOW the matrix will ALWAYS have the property you set. 5403 5404 5405 Options For Use with MatSetValues(): 5406 Insert a logically dense subblock, which can be 5407 . MAT_ROW_ORIENTED - row-oriented (default) 5408 5409 Note these options reflect the data you pass in with MatSetValues(); it has 5410 nothing to do with how the data is stored internally in the matrix 5411 data structure. 5412 5413 When (re)assembling a matrix, we can restrict the input for 5414 efficiency/debugging purposes. These options include: 5415 + MAT_NEW_NONZERO_LOCATIONS - additional insertions will be allowed if they generate a new nonzero (slow) 5416 . MAT_NEW_DIAGONALS - new diagonals will be allowed (for block diagonal format only) 5417 . MAT_IGNORE_OFF_PROC_ENTRIES - drops off-processor entries 5418 . MAT_NEW_NONZERO_LOCATION_ERR - generates an error for new matrix entry 5419 . MAT_USE_HASH_TABLE - uses a hash table to speed up matrix assembly 5420 . MAT_NO_OFF_PROC_ENTRIES - you know each process will only set values for its own rows, will generate an error if 5421 any process sets values for another process. This avoids all reductions in the MatAssembly routines and thus improves 5422 performance for very large process counts. 5423 - MAT_SUBSET_OFF_PROC_ENTRIES - you know that the first assembly after setting this flag will set a superset 5424 of the off-process entries required for all subsequent assemblies. This avoids a rendezvous step in the MatAssembly 5425 functions, instead sending only neighbor messages. 5426 5427 Notes: 5428 Except for MAT_UNUSED_NONZERO_LOCATION_ERR and MAT_ROW_ORIENTED all processes that share the matrix must pass the same value in flg! 5429 5430 Some options are relevant only for particular matrix types and 5431 are thus ignored by others. Other options are not supported by 5432 certain matrix types and will generate an error message if set. 5433 5434 If using a Fortran 77 module to compute a matrix, one may need to 5435 use the column-oriented option (or convert to the row-oriented 5436 format). 5437 5438 MAT_NEW_NONZERO_LOCATIONS set to PETSC_FALSE indicates that any add or insertion 5439 that would generate a new entry in the nonzero structure is instead 5440 ignored. Thus, if memory has not alredy been allocated for this particular 5441 data, then the insertion is ignored. For dense matrices, in which 5442 the entire array is allocated, no entries are ever ignored. 5443 Set after the first MatAssemblyEnd(). If this option is set then the MatAssemblyBegin/End() processes has one less global reduction 5444 5445 MAT_NEW_NONZERO_LOCATION_ERR set to PETSC_TRUE indicates that any add or insertion 5446 that would generate a new entry in the nonzero structure instead produces 5447 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 5448 5449 MAT_NEW_NONZERO_ALLOCATION_ERR set to PETSC_TRUE indicates that any add or insertion 5450 that would generate a new entry that has not been preallocated will 5451 instead produce an error. (Currently supported for AIJ and BAIJ formats 5452 only.) This is a useful flag when debugging matrix memory preallocation. 5453 If this option is set then the MatAssemblyBegin/End() processes has one less global reduction 5454 5455 MAT_IGNORE_OFF_PROC_ENTRIES set to PETSC_TRUE indicates entries destined for 5456 other processors should be dropped, rather than stashed. 5457 This is useful if you know that the "owning" processor is also 5458 always generating the correct matrix entries, so that PETSc need 5459 not transfer duplicate entries generated on another processor. 5460 5461 MAT_USE_HASH_TABLE indicates that a hash table be used to improve the 5462 searches during matrix assembly. When this flag is set, the hash table 5463 is created during the first Matrix Assembly. This hash table is 5464 used the next time through, during MatSetVaules()/MatSetVaulesBlocked() 5465 to improve the searching of indices. MAT_NEW_NONZERO_LOCATIONS flag 5466 should be used with MAT_USE_HASH_TABLE flag. This option is currently 5467 supported by MATMPIBAIJ format only. 5468 5469 MAT_KEEP_NONZERO_PATTERN indicates when MatZeroRows() is called the zeroed entries 5470 are kept in the nonzero structure 5471 5472 MAT_IGNORE_ZERO_ENTRIES - for AIJ/IS matrices this will stop zero values from creating 5473 a zero location in the matrix 5474 5475 MAT_USE_INODES - indicates using inode version of the code - works with AIJ matrix types 5476 5477 MAT_NO_OFF_PROC_ZERO_ROWS - you know each process will only zero its own rows. This avoids all reductions in the 5478 zero row routines and thus improves performance for very large process counts. 5479 5480 MAT_IGNORE_LOWER_TRIANGULAR - For SBAIJ matrices will ignore any insertions you make in the lower triangular 5481 part of the matrix (since they should match the upper triangular part). 5482 5483 Notes: 5484 Can only be called after MatSetSizes() and MatSetType() have been set. 5485 5486 Level: intermediate 5487 5488 Concepts: matrices^setting options 5489 5490 .seealso: MatOption, Mat 5491 5492 @*/ 5493 PetscErrorCode MatSetOption(Mat mat,MatOption op,PetscBool flg) 5494 { 5495 PetscErrorCode ierr; 5496 5497 PetscFunctionBegin; 5498 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5499 PetscValidType(mat,1); 5500 if (op > 0) { 5501 PetscValidLogicalCollectiveEnum(mat,op,2); 5502 PetscValidLogicalCollectiveBool(mat,flg,3); 5503 } 5504 5505 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); 5506 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()"); 5507 5508 switch (op) { 5509 case MAT_NO_OFF_PROC_ENTRIES: 5510 mat->nooffprocentries = flg; 5511 PetscFunctionReturn(0); 5512 break; 5513 case MAT_SUBSET_OFF_PROC_ENTRIES: 5514 mat->subsetoffprocentries = flg; 5515 PetscFunctionReturn(0); 5516 case MAT_NO_OFF_PROC_ZERO_ROWS: 5517 mat->nooffproczerorows = flg; 5518 PetscFunctionReturn(0); 5519 break; 5520 case MAT_SPD: 5521 mat->spd_set = PETSC_TRUE; 5522 mat->spd = flg; 5523 if (flg) { 5524 mat->symmetric = PETSC_TRUE; 5525 mat->structurally_symmetric = PETSC_TRUE; 5526 mat->symmetric_set = PETSC_TRUE; 5527 mat->structurally_symmetric_set = PETSC_TRUE; 5528 } 5529 break; 5530 case MAT_SYMMETRIC: 5531 mat->symmetric = flg; 5532 if (flg) mat->structurally_symmetric = PETSC_TRUE; 5533 mat->symmetric_set = PETSC_TRUE; 5534 mat->structurally_symmetric_set = flg; 5535 #if !defined(PETSC_USE_COMPLEX) 5536 mat->hermitian = flg; 5537 mat->hermitian_set = PETSC_TRUE; 5538 #endif 5539 break; 5540 case MAT_HERMITIAN: 5541 mat->hermitian = flg; 5542 if (flg) mat->structurally_symmetric = PETSC_TRUE; 5543 mat->hermitian_set = PETSC_TRUE; 5544 mat->structurally_symmetric_set = flg; 5545 #if !defined(PETSC_USE_COMPLEX) 5546 mat->symmetric = flg; 5547 mat->symmetric_set = PETSC_TRUE; 5548 #endif 5549 break; 5550 case MAT_STRUCTURALLY_SYMMETRIC: 5551 mat->structurally_symmetric = flg; 5552 mat->structurally_symmetric_set = PETSC_TRUE; 5553 break; 5554 case MAT_SYMMETRY_ETERNAL: 5555 mat->symmetric_eternal = flg; 5556 break; 5557 case MAT_STRUCTURE_ONLY: 5558 mat->structure_only = flg; 5559 break; 5560 default: 5561 break; 5562 } 5563 if (mat->ops->setoption) { 5564 ierr = (*mat->ops->setoption)(mat,op,flg);CHKERRQ(ierr); 5565 } 5566 PetscFunctionReturn(0); 5567 } 5568 5569 /*@ 5570 MatGetOption - Gets a parameter option that has been set for a matrix. 5571 5572 Logically Collective on Mat for certain operations, such as MAT_SPD, not collective for MAT_ROW_ORIENTED, see MatOption 5573 5574 Input Parameters: 5575 + mat - the matrix 5576 - option - the option, this only responds to certain options, check the code for which ones 5577 5578 Output Parameter: 5579 . flg - turn the option on (PETSC_TRUE) or off (PETSC_FALSE) 5580 5581 Notes: 5582 Can only be called after MatSetSizes() and MatSetType() have been set. 5583 5584 Level: intermediate 5585 5586 Concepts: matrices^setting options 5587 5588 .seealso: MatOption, MatSetOption() 5589 5590 @*/ 5591 PetscErrorCode MatGetOption(Mat mat,MatOption op,PetscBool *flg) 5592 { 5593 PetscFunctionBegin; 5594 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5595 PetscValidType(mat,1); 5596 5597 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); 5598 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()"); 5599 5600 switch (op) { 5601 case MAT_NO_OFF_PROC_ENTRIES: 5602 *flg = mat->nooffprocentries; 5603 break; 5604 case MAT_NO_OFF_PROC_ZERO_ROWS: 5605 *flg = mat->nooffproczerorows; 5606 break; 5607 case MAT_SYMMETRIC: 5608 *flg = mat->symmetric; 5609 break; 5610 case MAT_HERMITIAN: 5611 *flg = mat->hermitian; 5612 break; 5613 case MAT_STRUCTURALLY_SYMMETRIC: 5614 *flg = mat->structurally_symmetric; 5615 break; 5616 case MAT_SYMMETRY_ETERNAL: 5617 *flg = mat->symmetric_eternal; 5618 break; 5619 case MAT_SPD: 5620 *flg = mat->spd; 5621 break; 5622 default: 5623 break; 5624 } 5625 PetscFunctionReturn(0); 5626 } 5627 5628 /*@ 5629 MatZeroEntries - Zeros all entries of a matrix. For sparse matrices 5630 this routine retains the old nonzero structure. 5631 5632 Logically Collective on Mat 5633 5634 Input Parameters: 5635 . mat - the matrix 5636 5637 Level: intermediate 5638 5639 Notes: 5640 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. 5641 See the Performance chapter of the users manual for information on preallocating matrices. 5642 5643 Concepts: matrices^zeroing 5644 5645 .seealso: MatZeroRows() 5646 @*/ 5647 PetscErrorCode MatZeroEntries(Mat mat) 5648 { 5649 PetscErrorCode ierr; 5650 5651 PetscFunctionBegin; 5652 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5653 PetscValidType(mat,1); 5654 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5655 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"); 5656 if (!mat->ops->zeroentries) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 5657 MatCheckPreallocated(mat,1); 5658 5659 ierr = PetscLogEventBegin(MAT_ZeroEntries,mat,0,0,0);CHKERRQ(ierr); 5660 ierr = (*mat->ops->zeroentries)(mat);CHKERRQ(ierr); 5661 ierr = PetscLogEventEnd(MAT_ZeroEntries,mat,0,0,0);CHKERRQ(ierr); 5662 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 5663 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_VECCUDA) 5664 if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) { 5665 mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU; 5666 } 5667 #endif 5668 PetscFunctionReturn(0); 5669 } 5670 5671 /*@ 5672 MatZeroRowsColumns - Zeros all entries (except possibly the main diagonal) 5673 of a set of rows and columns of a matrix. 5674 5675 Collective on Mat 5676 5677 Input Parameters: 5678 + mat - the matrix 5679 . numRows - the number of rows to remove 5680 . rows - the global row indices 5681 . diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry) 5682 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 5683 - b - optional vector of right hand side, that will be adjusted by provided solution 5684 5685 Notes: 5686 This does not change the nonzero structure of the matrix, it merely zeros those entries in the matrix. 5687 5688 The user can set a value in the diagonal entry (or for the AIJ and 5689 row formats can optionally remove the main diagonal entry from the 5690 nonzero structure as well, by passing 0.0 as the final argument). 5691 5692 For the parallel case, all processes that share the matrix (i.e., 5693 those in the communicator used for matrix creation) MUST call this 5694 routine, regardless of whether any rows being zeroed are owned by 5695 them. 5696 5697 Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to 5698 list only rows local to itself). 5699 5700 The option MAT_NO_OFF_PROC_ZERO_ROWS does not apply to this routine. 5701 5702 Level: intermediate 5703 5704 Concepts: matrices^zeroing rows 5705 5706 .seealso: MatZeroRowsIS(), MatZeroRows(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 5707 MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil() 5708 @*/ 5709 PetscErrorCode MatZeroRowsColumns(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 5710 { 5711 PetscErrorCode ierr; 5712 5713 PetscFunctionBegin; 5714 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5715 PetscValidType(mat,1); 5716 if (numRows) PetscValidIntPointer(rows,3); 5717 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5718 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5719 if (!mat->ops->zerorowscolumns) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 5720 MatCheckPreallocated(mat,1); 5721 5722 ierr = (*mat->ops->zerorowscolumns)(mat,numRows,rows,diag,x,b);CHKERRQ(ierr); 5723 ierr = MatViewFromOptions(mat,NULL,"-mat_view");CHKERRQ(ierr); 5724 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 5725 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_VECCUDA) 5726 if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) { 5727 mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU; 5728 } 5729 #endif 5730 PetscFunctionReturn(0); 5731 } 5732 5733 /*@ 5734 MatZeroRowsColumnsIS - Zeros all entries (except possibly the main diagonal) 5735 of a set of rows and columns of a matrix. 5736 5737 Collective on Mat 5738 5739 Input Parameters: 5740 + mat - the matrix 5741 . is - the rows to zero 5742 . diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry) 5743 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 5744 - b - optional vector of right hand side, that will be adjusted by provided solution 5745 5746 Notes: 5747 This does not change the nonzero structure of the matrix, it merely zeros those entries in the matrix. 5748 5749 The user can set a value in the diagonal entry (or for the AIJ and 5750 row formats can optionally remove the main diagonal entry from the 5751 nonzero structure as well, by passing 0.0 as the final argument). 5752 5753 For the parallel case, all processes that share the matrix (i.e., 5754 those in the communicator used for matrix creation) MUST call this 5755 routine, regardless of whether any rows being zeroed are owned by 5756 them. 5757 5758 Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to 5759 list only rows local to itself). 5760 5761 The option MAT_NO_OFF_PROC_ZERO_ROWS does not apply to this routine. 5762 5763 Level: intermediate 5764 5765 Concepts: matrices^zeroing rows 5766 5767 .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 5768 MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRows(), MatZeroRowsColumnsStencil() 5769 @*/ 5770 PetscErrorCode MatZeroRowsColumnsIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b) 5771 { 5772 PetscErrorCode ierr; 5773 PetscInt numRows; 5774 const PetscInt *rows; 5775 5776 PetscFunctionBegin; 5777 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5778 PetscValidHeaderSpecific(is,IS_CLASSID,2); 5779 PetscValidType(mat,1); 5780 PetscValidType(is,2); 5781 ierr = ISGetLocalSize(is,&numRows);CHKERRQ(ierr); 5782 ierr = ISGetIndices(is,&rows);CHKERRQ(ierr); 5783 ierr = MatZeroRowsColumns(mat,numRows,rows,diag,x,b);CHKERRQ(ierr); 5784 ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr); 5785 PetscFunctionReturn(0); 5786 } 5787 5788 /*@ 5789 MatZeroRows - Zeros all entries (except possibly the main diagonal) 5790 of a set of rows of a matrix. 5791 5792 Collective on Mat 5793 5794 Input Parameters: 5795 + mat - the matrix 5796 . numRows - the number of rows to remove 5797 . rows - the global row indices 5798 . diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry) 5799 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 5800 - b - optional vector of right hand side, that will be adjusted by provided solution 5801 5802 Notes: 5803 For the AIJ and BAIJ matrix formats this removes the old nonzero structure, 5804 but does not release memory. For the dense and block diagonal 5805 formats this does not alter the nonzero structure. 5806 5807 If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure 5808 of the matrix is not changed (even for AIJ and BAIJ matrices) the values are 5809 merely zeroed. 5810 5811 The user can set a value in the diagonal entry (or for the AIJ and 5812 row formats can optionally remove the main diagonal entry from the 5813 nonzero structure as well, by passing 0.0 as the final argument). 5814 5815 For the parallel case, all processes that share the matrix (i.e., 5816 those in the communicator used for matrix creation) MUST call this 5817 routine, regardless of whether any rows being zeroed are owned by 5818 them. 5819 5820 Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to 5821 list only rows local to itself). 5822 5823 You can call MatSetOption(mat,MAT_NO_OFF_PROC_ZERO_ROWS,PETSC_TRUE) if each process indicates only rows it 5824 owns that are to be zeroed. This saves a global synchronization in the implementation. 5825 5826 Level: intermediate 5827 5828 Concepts: matrices^zeroing rows 5829 5830 .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 5831 MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil() 5832 @*/ 5833 PetscErrorCode MatZeroRows(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 5834 { 5835 PetscErrorCode ierr; 5836 5837 PetscFunctionBegin; 5838 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5839 PetscValidType(mat,1); 5840 if (numRows) PetscValidIntPointer(rows,3); 5841 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 5842 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 5843 if (!mat->ops->zerorows) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 5844 MatCheckPreallocated(mat,1); 5845 5846 ierr = (*mat->ops->zerorows)(mat,numRows,rows,diag,x,b);CHKERRQ(ierr); 5847 ierr = MatViewFromOptions(mat,NULL,"-mat_view");CHKERRQ(ierr); 5848 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 5849 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_VECCUDA) 5850 if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) { 5851 mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU; 5852 } 5853 #endif 5854 PetscFunctionReturn(0); 5855 } 5856 5857 /*@ 5858 MatZeroRowsIS - Zeros all entries (except possibly the main diagonal) 5859 of a set of rows of a matrix. 5860 5861 Collective on Mat 5862 5863 Input Parameters: 5864 + mat - the matrix 5865 . is - index set of rows to remove 5866 . diag - value put in all diagonals of eliminated rows 5867 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 5868 - b - optional vector of right hand side, that will be adjusted by provided solution 5869 5870 Notes: 5871 For the AIJ and BAIJ matrix formats this removes the old nonzero structure, 5872 but does not release memory. For the dense and block diagonal 5873 formats this does not alter the nonzero structure. 5874 5875 If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure 5876 of the matrix is not changed (even for AIJ and BAIJ matrices) the values are 5877 merely zeroed. 5878 5879 The user can set a value in the diagonal entry (or for the AIJ and 5880 row formats can optionally remove the main diagonal entry from the 5881 nonzero structure as well, by passing 0.0 as the final argument). 5882 5883 For the parallel case, all processes that share the matrix (i.e., 5884 those in the communicator used for matrix creation) MUST call this 5885 routine, regardless of whether any rows being zeroed are owned by 5886 them. 5887 5888 Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to 5889 list only rows local to itself). 5890 5891 You can call MatSetOption(mat,MAT_NO_OFF_PROC_ZERO_ROWS,PETSC_TRUE) if each process indicates only rows it 5892 owns that are to be zeroed. This saves a global synchronization in the implementation. 5893 5894 Level: intermediate 5895 5896 Concepts: matrices^zeroing rows 5897 5898 .seealso: MatZeroRows(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 5899 MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil() 5900 @*/ 5901 PetscErrorCode MatZeroRowsIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b) 5902 { 5903 PetscInt numRows; 5904 const PetscInt *rows; 5905 PetscErrorCode ierr; 5906 5907 PetscFunctionBegin; 5908 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5909 PetscValidType(mat,1); 5910 PetscValidHeaderSpecific(is,IS_CLASSID,2); 5911 ierr = ISGetLocalSize(is,&numRows);CHKERRQ(ierr); 5912 ierr = ISGetIndices(is,&rows);CHKERRQ(ierr); 5913 ierr = MatZeroRows(mat,numRows,rows,diag,x,b);CHKERRQ(ierr); 5914 ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr); 5915 PetscFunctionReturn(0); 5916 } 5917 5918 /*@ 5919 MatZeroRowsStencil - Zeros all entries (except possibly the main diagonal) 5920 of a set of rows of a matrix. These rows must be local to the process. 5921 5922 Collective on Mat 5923 5924 Input Parameters: 5925 + mat - the matrix 5926 . numRows - the number of rows to remove 5927 . rows - the grid coordinates (and component number when dof > 1) for matrix rows 5928 . diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry) 5929 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 5930 - b - optional vector of right hand side, that will be adjusted by provided solution 5931 5932 Notes: 5933 For the AIJ and BAIJ matrix formats this removes the old nonzero structure, 5934 but does not release memory. For the dense and block diagonal 5935 formats this does not alter the nonzero structure. 5936 5937 If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure 5938 of the matrix is not changed (even for AIJ and BAIJ matrices) the values are 5939 merely zeroed. 5940 5941 The user can set a value in the diagonal entry (or for the AIJ and 5942 row formats can optionally remove the main diagonal entry from the 5943 nonzero structure as well, by passing 0.0 as the final argument). 5944 5945 For the parallel case, all processes that share the matrix (i.e., 5946 those in the communicator used for matrix creation) MUST call this 5947 routine, regardless of whether any rows being zeroed are owned by 5948 them. 5949 5950 Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to 5951 list only rows local to itself). 5952 5953 The grid coordinates are across the entire grid, not just the local portion 5954 5955 In Fortran idxm and idxn should be declared as 5956 $ MatStencil idxm(4,m) 5957 and the values inserted using 5958 $ idxm(MatStencil_i,1) = i 5959 $ idxm(MatStencil_j,1) = j 5960 $ idxm(MatStencil_k,1) = k 5961 $ idxm(MatStencil_c,1) = c 5962 etc 5963 5964 For periodic boundary conditions use negative indices for values to the left (below 0; that are to be 5965 obtained by wrapping values from right edge). For values to the right of the last entry using that index plus one 5966 etc to obtain values that obtained by wrapping the values from the left edge. This does not work for anything but the 5967 DM_BOUNDARY_PERIODIC boundary type. 5968 5969 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 5970 a single value per point) you can skip filling those indices. 5971 5972 Level: intermediate 5973 5974 Concepts: matrices^zeroing rows 5975 5976 .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsl(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 5977 MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil() 5978 @*/ 5979 PetscErrorCode MatZeroRowsStencil(Mat mat,PetscInt numRows,const MatStencil rows[],PetscScalar diag,Vec x,Vec b) 5980 { 5981 PetscInt dim = mat->stencil.dim; 5982 PetscInt sdim = dim - (1 - (PetscInt) mat->stencil.noc); 5983 PetscInt *dims = mat->stencil.dims+1; 5984 PetscInt *starts = mat->stencil.starts; 5985 PetscInt *dxm = (PetscInt*) rows; 5986 PetscInt *jdxm, i, j, tmp, numNewRows = 0; 5987 PetscErrorCode ierr; 5988 5989 PetscFunctionBegin; 5990 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5991 PetscValidType(mat,1); 5992 if (numRows) PetscValidIntPointer(rows,3); 5993 5994 ierr = PetscMalloc1(numRows, &jdxm);CHKERRQ(ierr); 5995 for (i = 0; i < numRows; ++i) { 5996 /* Skip unused dimensions (they are ordered k, j, i, c) */ 5997 for (j = 0; j < 3-sdim; ++j) dxm++; 5998 /* Local index in X dir */ 5999 tmp = *dxm++ - starts[0]; 6000 /* Loop over remaining dimensions */ 6001 for (j = 0; j < dim-1; ++j) { 6002 /* If nonlocal, set index to be negative */ 6003 if ((*dxm++ - starts[j+1]) < 0 || tmp < 0) tmp = PETSC_MIN_INT; 6004 /* Update local index */ 6005 else tmp = tmp*dims[j] + *(dxm-1) - starts[j+1]; 6006 } 6007 /* Skip component slot if necessary */ 6008 if (mat->stencil.noc) dxm++; 6009 /* Local row number */ 6010 if (tmp >= 0) { 6011 jdxm[numNewRows++] = tmp; 6012 } 6013 } 6014 ierr = MatZeroRowsLocal(mat,numNewRows,jdxm,diag,x,b);CHKERRQ(ierr); 6015 ierr = PetscFree(jdxm);CHKERRQ(ierr); 6016 PetscFunctionReturn(0); 6017 } 6018 6019 /*@ 6020 MatZeroRowsColumnsStencil - Zeros all row and column entries (except possibly the main diagonal) 6021 of a set of rows and columns of a matrix. 6022 6023 Collective on Mat 6024 6025 Input Parameters: 6026 + mat - the matrix 6027 . numRows - the number of rows/columns to remove 6028 . rows - the grid coordinates (and component number when dof > 1) for matrix rows 6029 . diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry) 6030 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 6031 - b - optional vector of right hand side, that will be adjusted by provided solution 6032 6033 Notes: 6034 For the AIJ and BAIJ matrix formats this removes the old nonzero structure, 6035 but does not release memory. For the dense and block diagonal 6036 formats this does not alter the nonzero structure. 6037 6038 If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure 6039 of the matrix is not changed (even for AIJ and BAIJ matrices) the values are 6040 merely zeroed. 6041 6042 The user can set a value in the diagonal entry (or for the AIJ and 6043 row formats can optionally remove the main diagonal entry from the 6044 nonzero structure as well, by passing 0.0 as the final argument). 6045 6046 For the parallel case, all processes that share the matrix (i.e., 6047 those in the communicator used for matrix creation) MUST call this 6048 routine, regardless of whether any rows being zeroed are owned by 6049 them. 6050 6051 Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to 6052 list only rows local to itself, but the row/column numbers are given in local numbering). 6053 6054 The grid coordinates are across the entire grid, not just the local portion 6055 6056 In Fortran idxm and idxn should be declared as 6057 $ MatStencil idxm(4,m) 6058 and the values inserted using 6059 $ idxm(MatStencil_i,1) = i 6060 $ idxm(MatStencil_j,1) = j 6061 $ idxm(MatStencil_k,1) = k 6062 $ idxm(MatStencil_c,1) = c 6063 etc 6064 6065 For periodic boundary conditions use negative indices for values to the left (below 0; that are to be 6066 obtained by wrapping values from right edge). For values to the right of the last entry using that index plus one 6067 etc to obtain values that obtained by wrapping the values from the left edge. This does not work for anything but the 6068 DM_BOUNDARY_PERIODIC boundary type. 6069 6070 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 6071 a single value per point) you can skip filling those indices. 6072 6073 Level: intermediate 6074 6075 Concepts: matrices^zeroing rows 6076 6077 .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 6078 MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRows() 6079 @*/ 6080 PetscErrorCode MatZeroRowsColumnsStencil(Mat mat,PetscInt numRows,const MatStencil rows[],PetscScalar diag,Vec x,Vec b) 6081 { 6082 PetscInt dim = mat->stencil.dim; 6083 PetscInt sdim = dim - (1 - (PetscInt) mat->stencil.noc); 6084 PetscInt *dims = mat->stencil.dims+1; 6085 PetscInt *starts = mat->stencil.starts; 6086 PetscInt *dxm = (PetscInt*) rows; 6087 PetscInt *jdxm, i, j, tmp, numNewRows = 0; 6088 PetscErrorCode ierr; 6089 6090 PetscFunctionBegin; 6091 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6092 PetscValidType(mat,1); 6093 if (numRows) PetscValidIntPointer(rows,3); 6094 6095 ierr = PetscMalloc1(numRows, &jdxm);CHKERRQ(ierr); 6096 for (i = 0; i < numRows; ++i) { 6097 /* Skip unused dimensions (they are ordered k, j, i, c) */ 6098 for (j = 0; j < 3-sdim; ++j) dxm++; 6099 /* Local index in X dir */ 6100 tmp = *dxm++ - starts[0]; 6101 /* Loop over remaining dimensions */ 6102 for (j = 0; j < dim-1; ++j) { 6103 /* If nonlocal, set index to be negative */ 6104 if ((*dxm++ - starts[j+1]) < 0 || tmp < 0) tmp = PETSC_MIN_INT; 6105 /* Update local index */ 6106 else tmp = tmp*dims[j] + *(dxm-1) - starts[j+1]; 6107 } 6108 /* Skip component slot if necessary */ 6109 if (mat->stencil.noc) dxm++; 6110 /* Local row number */ 6111 if (tmp >= 0) { 6112 jdxm[numNewRows++] = tmp; 6113 } 6114 } 6115 ierr = MatZeroRowsColumnsLocal(mat,numNewRows,jdxm,diag,x,b);CHKERRQ(ierr); 6116 ierr = PetscFree(jdxm);CHKERRQ(ierr); 6117 PetscFunctionReturn(0); 6118 } 6119 6120 /*@ 6121 MatZeroRowsLocal - Zeros all entries (except possibly the main diagonal) 6122 of a set of rows of a matrix; using local numbering of rows. 6123 6124 Collective on Mat 6125 6126 Input Parameters: 6127 + mat - the matrix 6128 . numRows - the number of rows to remove 6129 . rows - the global row indices 6130 . diag - value put in all diagonals of eliminated rows 6131 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 6132 - b - optional vector of right hand side, that will be adjusted by provided solution 6133 6134 Notes: 6135 Before calling MatZeroRowsLocal(), the user must first set the 6136 local-to-global mapping by calling MatSetLocalToGlobalMapping(). 6137 6138 For the AIJ matrix formats this removes the old nonzero structure, 6139 but does not release memory. For the dense and block diagonal 6140 formats this does not alter the nonzero structure. 6141 6142 If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure 6143 of the matrix is not changed (even for AIJ and BAIJ matrices) the values are 6144 merely zeroed. 6145 6146 The user can set a value in the diagonal entry (or for the AIJ and 6147 row formats can optionally remove the main diagonal entry from the 6148 nonzero structure as well, by passing 0.0 as the final argument). 6149 6150 You can call MatSetOption(mat,MAT_NO_OFF_PROC_ZERO_ROWS,PETSC_TRUE) if each process indicates only rows it 6151 owns that are to be zeroed. This saves a global synchronization in the implementation. 6152 6153 Level: intermediate 6154 6155 Concepts: matrices^zeroing 6156 6157 .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRows(), MatSetOption(), 6158 MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil() 6159 @*/ 6160 PetscErrorCode MatZeroRowsLocal(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 6161 { 6162 PetscErrorCode ierr; 6163 6164 PetscFunctionBegin; 6165 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6166 PetscValidType(mat,1); 6167 if (numRows) PetscValidIntPointer(rows,3); 6168 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 6169 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6170 MatCheckPreallocated(mat,1); 6171 6172 if (mat->ops->zerorowslocal) { 6173 ierr = (*mat->ops->zerorowslocal)(mat,numRows,rows,diag,x,b);CHKERRQ(ierr); 6174 } else { 6175 IS is, newis; 6176 const PetscInt *newRows; 6177 6178 if (!mat->rmap->mapping) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Need to provide local to global mapping to matrix first"); 6179 ierr = ISCreateGeneral(PETSC_COMM_SELF,numRows,rows,PETSC_COPY_VALUES,&is);CHKERRQ(ierr); 6180 ierr = ISLocalToGlobalMappingApplyIS(mat->rmap->mapping,is,&newis);CHKERRQ(ierr); 6181 ierr = ISGetIndices(newis,&newRows);CHKERRQ(ierr); 6182 ierr = (*mat->ops->zerorows)(mat,numRows,newRows,diag,x,b);CHKERRQ(ierr); 6183 ierr = ISRestoreIndices(newis,&newRows);CHKERRQ(ierr); 6184 ierr = ISDestroy(&newis);CHKERRQ(ierr); 6185 ierr = ISDestroy(&is);CHKERRQ(ierr); 6186 } 6187 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 6188 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_VECCUDA) 6189 if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) { 6190 mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU; 6191 } 6192 #endif 6193 PetscFunctionReturn(0); 6194 } 6195 6196 /*@ 6197 MatZeroRowsLocalIS - Zeros all entries (except possibly the main diagonal) 6198 of a set of rows of a matrix; using local numbering of rows. 6199 6200 Collective on Mat 6201 6202 Input Parameters: 6203 + mat - the matrix 6204 . is - index set of rows to remove 6205 . diag - value put in all diagonals of eliminated rows 6206 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 6207 - b - optional vector of right hand side, that will be adjusted by provided solution 6208 6209 Notes: 6210 Before calling MatZeroRowsLocalIS(), the user must first set the 6211 local-to-global mapping by calling MatSetLocalToGlobalMapping(). 6212 6213 For the AIJ matrix formats this removes the old nonzero structure, 6214 but does not release memory. For the dense and block diagonal 6215 formats this does not alter the nonzero structure. 6216 6217 If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure 6218 of the matrix is not changed (even for AIJ and BAIJ matrices) the values are 6219 merely zeroed. 6220 6221 The user can set a value in the diagonal entry (or for the AIJ and 6222 row formats can optionally remove the main diagonal entry from the 6223 nonzero structure as well, by passing 0.0 as the final argument). 6224 6225 You can call MatSetOption(mat,MAT_NO_OFF_PROC_ZERO_ROWS,PETSC_TRUE) if each process indicates only rows it 6226 owns that are to be zeroed. This saves a global synchronization in the implementation. 6227 6228 Level: intermediate 6229 6230 Concepts: matrices^zeroing 6231 6232 .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRows(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 6233 MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil() 6234 @*/ 6235 PetscErrorCode MatZeroRowsLocalIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b) 6236 { 6237 PetscErrorCode ierr; 6238 PetscInt numRows; 6239 const PetscInt *rows; 6240 6241 PetscFunctionBegin; 6242 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6243 PetscValidType(mat,1); 6244 PetscValidHeaderSpecific(is,IS_CLASSID,2); 6245 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 6246 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6247 MatCheckPreallocated(mat,1); 6248 6249 ierr = ISGetLocalSize(is,&numRows);CHKERRQ(ierr); 6250 ierr = ISGetIndices(is,&rows);CHKERRQ(ierr); 6251 ierr = MatZeroRowsLocal(mat,numRows,rows,diag,x,b);CHKERRQ(ierr); 6252 ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr); 6253 PetscFunctionReturn(0); 6254 } 6255 6256 /*@ 6257 MatZeroRowsColumnsLocal - Zeros all entries (except possibly the main diagonal) 6258 of a set of rows and columns of a matrix; using local numbering of rows. 6259 6260 Collective on Mat 6261 6262 Input Parameters: 6263 + mat - the matrix 6264 . numRows - the number of rows to remove 6265 . rows - the global row indices 6266 . diag - value put in all diagonals of eliminated rows 6267 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 6268 - b - optional vector of right hand side, that will be adjusted by provided solution 6269 6270 Notes: 6271 Before calling MatZeroRowsColumnsLocal(), the user must first set the 6272 local-to-global mapping by calling MatSetLocalToGlobalMapping(). 6273 6274 The user can set a value in the diagonal entry (or for the AIJ and 6275 row formats can optionally remove the main diagonal entry from the 6276 nonzero structure as well, by passing 0.0 as the final argument). 6277 6278 Level: intermediate 6279 6280 Concepts: matrices^zeroing 6281 6282 .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 6283 MatZeroRows(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil() 6284 @*/ 6285 PetscErrorCode MatZeroRowsColumnsLocal(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 6286 { 6287 PetscErrorCode ierr; 6288 IS is, newis; 6289 const PetscInt *newRows; 6290 6291 PetscFunctionBegin; 6292 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6293 PetscValidType(mat,1); 6294 if (numRows) PetscValidIntPointer(rows,3); 6295 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 6296 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6297 MatCheckPreallocated(mat,1); 6298 6299 if (!mat->cmap->mapping) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Need to provide local to global mapping to matrix first"); 6300 ierr = ISCreateGeneral(PETSC_COMM_SELF,numRows,rows,PETSC_COPY_VALUES,&is);CHKERRQ(ierr); 6301 ierr = ISLocalToGlobalMappingApplyIS(mat->cmap->mapping,is,&newis);CHKERRQ(ierr); 6302 ierr = ISGetIndices(newis,&newRows);CHKERRQ(ierr); 6303 ierr = (*mat->ops->zerorowscolumns)(mat,numRows,newRows,diag,x,b);CHKERRQ(ierr); 6304 ierr = ISRestoreIndices(newis,&newRows);CHKERRQ(ierr); 6305 ierr = ISDestroy(&newis);CHKERRQ(ierr); 6306 ierr = ISDestroy(&is);CHKERRQ(ierr); 6307 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 6308 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_VECCUDA) 6309 if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) { 6310 mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU; 6311 } 6312 #endif 6313 PetscFunctionReturn(0); 6314 } 6315 6316 /*@ 6317 MatZeroRowsColumnsLocalIS - Zeros all entries (except possibly the main diagonal) 6318 of a set of rows and columns of a matrix; using local numbering of rows. 6319 6320 Collective on Mat 6321 6322 Input Parameters: 6323 + mat - the matrix 6324 . is - index set of rows to remove 6325 . diag - value put in all diagonals of eliminated rows 6326 . x - optional vector of solutions for zeroed rows (other entries in vector are not used) 6327 - b - optional vector of right hand side, that will be adjusted by provided solution 6328 6329 Notes: 6330 Before calling MatZeroRowsColumnsLocalIS(), the user must first set the 6331 local-to-global mapping by calling MatSetLocalToGlobalMapping(). 6332 6333 The user can set a value in the diagonal entry (or for the AIJ and 6334 row formats can optionally remove the main diagonal entry from the 6335 nonzero structure as well, by passing 0.0 as the final argument). 6336 6337 Level: intermediate 6338 6339 Concepts: matrices^zeroing 6340 6341 .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), 6342 MatZeroRowsColumnsLocal(), MatZeroRows(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil() 6343 @*/ 6344 PetscErrorCode MatZeroRowsColumnsLocalIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b) 6345 { 6346 PetscErrorCode ierr; 6347 PetscInt numRows; 6348 const PetscInt *rows; 6349 6350 PetscFunctionBegin; 6351 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6352 PetscValidType(mat,1); 6353 PetscValidHeaderSpecific(is,IS_CLASSID,2); 6354 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 6355 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6356 MatCheckPreallocated(mat,1); 6357 6358 ierr = ISGetLocalSize(is,&numRows);CHKERRQ(ierr); 6359 ierr = ISGetIndices(is,&rows);CHKERRQ(ierr); 6360 ierr = MatZeroRowsColumnsLocal(mat,numRows,rows,diag,x,b);CHKERRQ(ierr); 6361 ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr); 6362 PetscFunctionReturn(0); 6363 } 6364 6365 /*@C 6366 MatGetSize - Returns the numbers of rows and columns in a matrix. 6367 6368 Not Collective 6369 6370 Input Parameter: 6371 . mat - the matrix 6372 6373 Output Parameters: 6374 + m - the number of global rows 6375 - n - the number of global columns 6376 6377 Note: both output parameters can be NULL on input. 6378 6379 Level: beginner 6380 6381 Concepts: matrices^size 6382 6383 .seealso: MatGetLocalSize() 6384 @*/ 6385 PetscErrorCode MatGetSize(Mat mat,PetscInt *m,PetscInt *n) 6386 { 6387 PetscFunctionBegin; 6388 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6389 if (m) *m = mat->rmap->N; 6390 if (n) *n = mat->cmap->N; 6391 PetscFunctionReturn(0); 6392 } 6393 6394 /*@C 6395 MatGetLocalSize - Returns the number of rows and columns in a matrix 6396 stored locally. This information may be implementation dependent, so 6397 use with care. 6398 6399 Not Collective 6400 6401 Input Parameters: 6402 . mat - the matrix 6403 6404 Output Parameters: 6405 + m - the number of local rows 6406 - n - the number of local columns 6407 6408 Note: both output parameters can be NULL on input. 6409 6410 Level: beginner 6411 6412 Concepts: matrices^local size 6413 6414 .seealso: MatGetSize() 6415 @*/ 6416 PetscErrorCode MatGetLocalSize(Mat mat,PetscInt *m,PetscInt *n) 6417 { 6418 PetscFunctionBegin; 6419 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6420 if (m) PetscValidIntPointer(m,2); 6421 if (n) PetscValidIntPointer(n,3); 6422 if (m) *m = mat->rmap->n; 6423 if (n) *n = mat->cmap->n; 6424 PetscFunctionReturn(0); 6425 } 6426 6427 /*@C 6428 MatGetOwnershipRangeColumn - Returns the range of matrix columns associated with rows of a vector one multiplies by that owned by 6429 this processor. (The columns of the "diagonal block") 6430 6431 Not Collective, unless matrix has not been allocated, then collective on Mat 6432 6433 Input Parameters: 6434 . mat - the matrix 6435 6436 Output Parameters: 6437 + m - the global index of the first local column 6438 - n - one more than the global index of the last local column 6439 6440 Notes: 6441 both output parameters can be NULL on input. 6442 6443 Level: developer 6444 6445 Concepts: matrices^column ownership 6446 6447 .seealso: MatGetOwnershipRange(), MatGetOwnershipRanges(), MatGetOwnershipRangesColumn() 6448 6449 @*/ 6450 PetscErrorCode MatGetOwnershipRangeColumn(Mat mat,PetscInt *m,PetscInt *n) 6451 { 6452 PetscFunctionBegin; 6453 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6454 PetscValidType(mat,1); 6455 if (m) PetscValidIntPointer(m,2); 6456 if (n) PetscValidIntPointer(n,3); 6457 MatCheckPreallocated(mat,1); 6458 if (m) *m = mat->cmap->rstart; 6459 if (n) *n = mat->cmap->rend; 6460 PetscFunctionReturn(0); 6461 } 6462 6463 /*@C 6464 MatGetOwnershipRange - Returns the range of matrix rows owned by 6465 this processor, assuming that the matrix is laid out with the first 6466 n1 rows on the first processor, the next n2 rows on the second, etc. 6467 For certain parallel layouts this range may not be well defined. 6468 6469 Not Collective 6470 6471 Input Parameters: 6472 . mat - the matrix 6473 6474 Output Parameters: 6475 + m - the global index of the first local row 6476 - n - one more than the global index of the last local row 6477 6478 Note: Both output parameters can be NULL on input. 6479 $ This function requires that the matrix be preallocated. If you have not preallocated, consider using 6480 $ PetscSplitOwnership(MPI_Comm comm, PetscInt *n, PetscInt *N) 6481 $ and then MPI_Scan() to calculate prefix sums of the local sizes. 6482 6483 Level: beginner 6484 6485 Concepts: matrices^row ownership 6486 6487 .seealso: MatGetOwnershipRanges(), MatGetOwnershipRangeColumn(), MatGetOwnershipRangesColumn(), PetscSplitOwnership(), PetscSplitOwnershipBlock() 6488 6489 @*/ 6490 PetscErrorCode MatGetOwnershipRange(Mat mat,PetscInt *m,PetscInt *n) 6491 { 6492 PetscFunctionBegin; 6493 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6494 PetscValidType(mat,1); 6495 if (m) PetscValidIntPointer(m,2); 6496 if (n) PetscValidIntPointer(n,3); 6497 MatCheckPreallocated(mat,1); 6498 if (m) *m = mat->rmap->rstart; 6499 if (n) *n = mat->rmap->rend; 6500 PetscFunctionReturn(0); 6501 } 6502 6503 /*@C 6504 MatGetOwnershipRanges - Returns the range of matrix rows owned by 6505 each process 6506 6507 Not Collective, unless matrix has not been allocated, then collective on Mat 6508 6509 Input Parameters: 6510 . mat - the matrix 6511 6512 Output Parameters: 6513 . ranges - start of each processors portion plus one more than the total length at the end 6514 6515 Level: beginner 6516 6517 Concepts: matrices^row ownership 6518 6519 .seealso: MatGetOwnershipRange(), MatGetOwnershipRangeColumn(), MatGetOwnershipRangesColumn() 6520 6521 @*/ 6522 PetscErrorCode MatGetOwnershipRanges(Mat mat,const PetscInt **ranges) 6523 { 6524 PetscErrorCode ierr; 6525 6526 PetscFunctionBegin; 6527 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6528 PetscValidType(mat,1); 6529 MatCheckPreallocated(mat,1); 6530 ierr = PetscLayoutGetRanges(mat->rmap,ranges);CHKERRQ(ierr); 6531 PetscFunctionReturn(0); 6532 } 6533 6534 /*@C 6535 MatGetOwnershipRangesColumn - Returns the range of matrix columns associated with rows of a vector one multiplies by that owned by 6536 this processor. (The columns of the "diagonal blocks" for each process) 6537 6538 Not Collective, unless matrix has not been allocated, then collective on Mat 6539 6540 Input Parameters: 6541 . mat - the matrix 6542 6543 Output Parameters: 6544 . ranges - start of each processors portion plus one more then the total length at the end 6545 6546 Level: beginner 6547 6548 Concepts: matrices^column ownership 6549 6550 .seealso: MatGetOwnershipRange(), MatGetOwnershipRangeColumn(), MatGetOwnershipRanges() 6551 6552 @*/ 6553 PetscErrorCode MatGetOwnershipRangesColumn(Mat mat,const PetscInt **ranges) 6554 { 6555 PetscErrorCode ierr; 6556 6557 PetscFunctionBegin; 6558 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6559 PetscValidType(mat,1); 6560 MatCheckPreallocated(mat,1); 6561 ierr = PetscLayoutGetRanges(mat->cmap,ranges);CHKERRQ(ierr); 6562 PetscFunctionReturn(0); 6563 } 6564 6565 /*@C 6566 MatGetOwnershipIS - Get row and column ownership as index sets 6567 6568 Not Collective 6569 6570 Input Arguments: 6571 . A - matrix of type Elemental 6572 6573 Output Arguments: 6574 + rows - rows in which this process owns elements 6575 . cols - columns in which this process owns elements 6576 6577 Level: intermediate 6578 6579 .seealso: MatGetOwnershipRange(), MatGetOwnershipRangeColumn(), MatSetValues(), MATELEMENTAL 6580 @*/ 6581 PetscErrorCode MatGetOwnershipIS(Mat A,IS *rows,IS *cols) 6582 { 6583 PetscErrorCode ierr,(*f)(Mat,IS*,IS*); 6584 6585 PetscFunctionBegin; 6586 MatCheckPreallocated(A,1); 6587 ierr = PetscObjectQueryFunction((PetscObject)A,"MatGetOwnershipIS_C",&f);CHKERRQ(ierr); 6588 if (f) { 6589 ierr = (*f)(A,rows,cols);CHKERRQ(ierr); 6590 } else { /* Create a standard row-based partition, each process is responsible for ALL columns in their row block */ 6591 if (rows) {ierr = ISCreateStride(PETSC_COMM_SELF,A->rmap->n,A->rmap->rstart,1,rows);CHKERRQ(ierr);} 6592 if (cols) {ierr = ISCreateStride(PETSC_COMM_SELF,A->cmap->N,0,1,cols);CHKERRQ(ierr);} 6593 } 6594 PetscFunctionReturn(0); 6595 } 6596 6597 /*@C 6598 MatILUFactorSymbolic - Performs symbolic ILU factorization of a matrix. 6599 Uses levels of fill only, not drop tolerance. Use MatLUFactorNumeric() 6600 to complete the factorization. 6601 6602 Collective on Mat 6603 6604 Input Parameters: 6605 + mat - the matrix 6606 . row - row permutation 6607 . column - column permutation 6608 - info - structure containing 6609 $ levels - number of levels of fill. 6610 $ expected fill - as ratio of original fill. 6611 $ 1 or 0 - indicating force fill on diagonal (improves robustness for matrices 6612 missing diagonal entries) 6613 6614 Output Parameters: 6615 . fact - new matrix that has been symbolically factored 6616 6617 Notes: 6618 See Users-Manual: ch_mat for additional information about choosing the fill factor for better efficiency. 6619 6620 Most users should employ the simplified KSP interface for linear solvers 6621 instead of working directly with matrix algebra routines such as this. 6622 See, e.g., KSPCreate(). 6623 6624 Level: developer 6625 6626 Concepts: matrices^symbolic LU factorization 6627 Concepts: matrices^factorization 6628 Concepts: LU^symbolic factorization 6629 6630 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor() 6631 MatGetOrdering(), MatFactorInfo 6632 6633 Developer Note: fortran interface is not autogenerated as the f90 6634 interface defintion cannot be generated correctly [due to MatFactorInfo] 6635 6636 @*/ 6637 PetscErrorCode MatILUFactorSymbolic(Mat fact,Mat mat,IS row,IS col,const MatFactorInfo *info) 6638 { 6639 PetscErrorCode ierr; 6640 6641 PetscFunctionBegin; 6642 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6643 PetscValidType(mat,1); 6644 PetscValidHeaderSpecific(row,IS_CLASSID,2); 6645 PetscValidHeaderSpecific(col,IS_CLASSID,3); 6646 PetscValidPointer(info,4); 6647 PetscValidPointer(fact,5); 6648 if (info->levels < 0) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Levels of fill negative %D",(PetscInt)info->levels); 6649 if (info->fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %g",(double)info->fill); 6650 if (!(fact)->ops->ilufactorsymbolic) { 6651 MatSolverType spackage; 6652 ierr = MatFactorGetSolverType(fact,&spackage);CHKERRQ(ierr); 6653 SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s symbolic ILU using solver package %s",((PetscObject)mat)->type_name,spackage); 6654 } 6655 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 6656 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6657 MatCheckPreallocated(mat,2); 6658 6659 ierr = PetscLogEventBegin(MAT_ILUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr); 6660 ierr = (fact->ops->ilufactorsymbolic)(fact,mat,row,col,info);CHKERRQ(ierr); 6661 ierr = PetscLogEventEnd(MAT_ILUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr); 6662 PetscFunctionReturn(0); 6663 } 6664 6665 /*@C 6666 MatICCFactorSymbolic - Performs symbolic incomplete 6667 Cholesky factorization for a symmetric matrix. Use 6668 MatCholeskyFactorNumeric() to complete the factorization. 6669 6670 Collective on Mat 6671 6672 Input Parameters: 6673 + mat - the matrix 6674 . perm - row and column permutation 6675 - info - structure containing 6676 $ levels - number of levels of fill. 6677 $ expected fill - as ratio of original fill. 6678 6679 Output Parameter: 6680 . fact - the factored matrix 6681 6682 Notes: 6683 Most users should employ the KSP interface for linear solvers 6684 instead of working directly with matrix algebra routines such as this. 6685 See, e.g., KSPCreate(). 6686 6687 Level: developer 6688 6689 Concepts: matrices^symbolic incomplete Cholesky factorization 6690 Concepts: matrices^factorization 6691 Concepts: Cholsky^symbolic factorization 6692 6693 .seealso: MatCholeskyFactorNumeric(), MatCholeskyFactor(), MatFactorInfo 6694 6695 Developer Note: fortran interface is not autogenerated as the f90 6696 interface defintion cannot be generated correctly [due to MatFactorInfo] 6697 6698 @*/ 6699 PetscErrorCode MatICCFactorSymbolic(Mat fact,Mat mat,IS perm,const MatFactorInfo *info) 6700 { 6701 PetscErrorCode ierr; 6702 6703 PetscFunctionBegin; 6704 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6705 PetscValidType(mat,1); 6706 PetscValidHeaderSpecific(perm,IS_CLASSID,2); 6707 PetscValidPointer(info,3); 6708 PetscValidPointer(fact,4); 6709 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6710 if (info->levels < 0) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Levels negative %D",(PetscInt) info->levels); 6711 if (info->fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %g",(double)info->fill); 6712 if (!(fact)->ops->iccfactorsymbolic) { 6713 MatSolverType spackage; 6714 ierr = MatFactorGetSolverType(fact,&spackage);CHKERRQ(ierr); 6715 SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s symbolic ICC using solver package %s",((PetscObject)mat)->type_name,spackage); 6716 } 6717 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 6718 MatCheckPreallocated(mat,2); 6719 6720 ierr = PetscLogEventBegin(MAT_ICCFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr); 6721 ierr = (fact->ops->iccfactorsymbolic)(fact,mat,perm,info);CHKERRQ(ierr); 6722 ierr = PetscLogEventEnd(MAT_ICCFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr); 6723 PetscFunctionReturn(0); 6724 } 6725 6726 /*@C 6727 MatCreateSubMatrices - Extracts several submatrices from a matrix. If submat 6728 points to an array of valid matrices, they may be reused to store the new 6729 submatrices. 6730 6731 Collective on Mat 6732 6733 Input Parameters: 6734 + mat - the matrix 6735 . n - the number of submatrixes to be extracted (on this processor, may be zero) 6736 . irow, icol - index sets of rows and columns to extract 6737 - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 6738 6739 Output Parameter: 6740 . submat - the array of submatrices 6741 6742 Notes: 6743 MatCreateSubMatrices() can extract ONLY sequential submatrices 6744 (from both sequential and parallel matrices). Use MatCreateSubMatrix() 6745 to extract a parallel submatrix. 6746 6747 Some matrix types place restrictions on the row and column 6748 indices, such as that they be sorted or that they be equal to each other. 6749 6750 The index sets may not have duplicate entries. 6751 6752 When extracting submatrices from a parallel matrix, each processor can 6753 form a different submatrix by setting the rows and columns of its 6754 individual index sets according to the local submatrix desired. 6755 6756 When finished using the submatrices, the user should destroy 6757 them with MatDestroySubMatrices(). 6758 6759 MAT_REUSE_MATRIX can only be used when the nonzero structure of the 6760 original matrix has not changed from that last call to MatCreateSubMatrices(). 6761 6762 This routine creates the matrices in submat; you should NOT create them before 6763 calling it. It also allocates the array of matrix pointers submat. 6764 6765 For BAIJ matrices the index sets must respect the block structure, that is if they 6766 request one row/column in a block, they must request all rows/columns that are in 6767 that block. For example, if the block size is 2 you cannot request just row 0 and 6768 column 0. 6769 6770 Fortran Note: 6771 The Fortran interface is slightly different from that given below; it 6772 requires one to pass in as submat a Mat (integer) array of size at least n+1. 6773 6774 Level: advanced 6775 6776 Concepts: matrices^accessing submatrices 6777 Concepts: submatrices 6778 6779 .seealso: MatDestroySubMatrices(), MatCreateSubMatrix(), MatGetRow(), MatGetDiagonal(), MatReuse 6780 @*/ 6781 PetscErrorCode MatCreateSubMatrices(Mat mat,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *submat[]) 6782 { 6783 PetscErrorCode ierr; 6784 PetscInt i; 6785 PetscBool eq; 6786 6787 PetscFunctionBegin; 6788 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6789 PetscValidType(mat,1); 6790 if (n) { 6791 PetscValidPointer(irow,3); 6792 PetscValidHeaderSpecific(*irow,IS_CLASSID,3); 6793 PetscValidPointer(icol,4); 6794 PetscValidHeaderSpecific(*icol,IS_CLASSID,4); 6795 } 6796 PetscValidPointer(submat,6); 6797 if (n && scall == MAT_REUSE_MATRIX) { 6798 PetscValidPointer(*submat,6); 6799 PetscValidHeaderSpecific(**submat,MAT_CLASSID,6); 6800 } 6801 if (!mat->ops->createsubmatrices) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 6802 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 6803 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6804 MatCheckPreallocated(mat,1); 6805 6806 ierr = PetscLogEventBegin(MAT_CreateSubMats,mat,0,0,0);CHKERRQ(ierr); 6807 ierr = (*mat->ops->createsubmatrices)(mat,n,irow,icol,scall,submat);CHKERRQ(ierr); 6808 ierr = PetscLogEventEnd(MAT_CreateSubMats,mat,0,0,0);CHKERRQ(ierr); 6809 for (i=0; i<n; i++) { 6810 (*submat)[i]->factortype = MAT_FACTOR_NONE; /* in case in place factorization was previously done on submatrix */ 6811 if (mat->symmetric || mat->structurally_symmetric || mat->hermitian) { 6812 ierr = ISEqual(irow[i],icol[i],&eq);CHKERRQ(ierr); 6813 if (eq) { 6814 if (mat->symmetric) { 6815 ierr = MatSetOption((*submat)[i],MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr); 6816 } else if (mat->hermitian) { 6817 ierr = MatSetOption((*submat)[i],MAT_HERMITIAN,PETSC_TRUE);CHKERRQ(ierr); 6818 } else if (mat->structurally_symmetric) { 6819 ierr = MatSetOption((*submat)[i],MAT_STRUCTURALLY_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr); 6820 } 6821 } 6822 } 6823 } 6824 PetscFunctionReturn(0); 6825 } 6826 6827 /*@C 6828 MatCreateSubMatricesMPI - Extracts MPI submatrices across a sub communicator of mat (by pairs of IS that may live on subcomms). 6829 6830 Collective on Mat 6831 6832 Input Parameters: 6833 + mat - the matrix 6834 . n - the number of submatrixes to be extracted 6835 . irow, icol - index sets of rows and columns to extract 6836 - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 6837 6838 Output Parameter: 6839 . submat - the array of submatrices 6840 6841 Level: advanced 6842 6843 Concepts: matrices^accessing submatrices 6844 Concepts: submatrices 6845 6846 .seealso: MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRow(), MatGetDiagonal(), MatReuse 6847 @*/ 6848 PetscErrorCode MatCreateSubMatricesMPI(Mat mat,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *submat[]) 6849 { 6850 PetscErrorCode ierr; 6851 PetscInt i; 6852 PetscBool eq; 6853 6854 PetscFunctionBegin; 6855 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6856 PetscValidType(mat,1); 6857 if (n) { 6858 PetscValidPointer(irow,3); 6859 PetscValidHeaderSpecific(*irow,IS_CLASSID,3); 6860 PetscValidPointer(icol,4); 6861 PetscValidHeaderSpecific(*icol,IS_CLASSID,4); 6862 } 6863 PetscValidPointer(submat,6); 6864 if (n && scall == MAT_REUSE_MATRIX) { 6865 PetscValidPointer(*submat,6); 6866 PetscValidHeaderSpecific(**submat,MAT_CLASSID,6); 6867 } 6868 if (!mat->ops->createsubmatricesmpi) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 6869 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 6870 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6871 MatCheckPreallocated(mat,1); 6872 6873 ierr = PetscLogEventBegin(MAT_CreateSubMats,mat,0,0,0);CHKERRQ(ierr); 6874 ierr = (*mat->ops->createsubmatricesmpi)(mat,n,irow,icol,scall,submat);CHKERRQ(ierr); 6875 ierr = PetscLogEventEnd(MAT_CreateSubMats,mat,0,0,0);CHKERRQ(ierr); 6876 for (i=0; i<n; i++) { 6877 if (mat->symmetric || mat->structurally_symmetric || mat->hermitian) { 6878 ierr = ISEqual(irow[i],icol[i],&eq);CHKERRQ(ierr); 6879 if (eq) { 6880 if (mat->symmetric) { 6881 ierr = MatSetOption((*submat)[i],MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr); 6882 } else if (mat->hermitian) { 6883 ierr = MatSetOption((*submat)[i],MAT_HERMITIAN,PETSC_TRUE);CHKERRQ(ierr); 6884 } else if (mat->structurally_symmetric) { 6885 ierr = MatSetOption((*submat)[i],MAT_STRUCTURALLY_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr); 6886 } 6887 } 6888 } 6889 } 6890 PetscFunctionReturn(0); 6891 } 6892 6893 /*@C 6894 MatDestroyMatrices - Destroys an array of matrices. 6895 6896 Collective on Mat 6897 6898 Input Parameters: 6899 + n - the number of local matrices 6900 - mat - the matrices (note that this is a pointer to the array of matrices) 6901 6902 Level: advanced 6903 6904 Notes: 6905 Frees not only the matrices, but also the array that contains the matrices 6906 In Fortran will not free the array. 6907 6908 .seealso: MatCreateSubMatrices() MatDestroySubMatrices() 6909 @*/ 6910 PetscErrorCode MatDestroyMatrices(PetscInt n,Mat *mat[]) 6911 { 6912 PetscErrorCode ierr; 6913 PetscInt i; 6914 6915 PetscFunctionBegin; 6916 if (!*mat) PetscFunctionReturn(0); 6917 if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Trying to destroy negative number of matrices %D",n); 6918 PetscValidPointer(mat,2); 6919 6920 for (i=0; i<n; i++) { 6921 ierr = MatDestroy(&(*mat)[i]);CHKERRQ(ierr); 6922 } 6923 6924 /* memory is allocated even if n = 0 */ 6925 ierr = PetscFree(*mat);CHKERRQ(ierr); 6926 PetscFunctionReturn(0); 6927 } 6928 6929 /*@C 6930 MatDestroySubMatrices - Destroys a set of matrices obtained with MatCreateSubMatrices(). 6931 6932 Collective on Mat 6933 6934 Input Parameters: 6935 + n - the number of local matrices 6936 - mat - the matrices (note that this is a pointer to the array of matrices, just to match the calling 6937 sequence of MatCreateSubMatrices()) 6938 6939 Level: advanced 6940 6941 Notes: 6942 Frees not only the matrices, but also the array that contains the matrices 6943 In Fortran will not free the array. 6944 6945 .seealso: MatCreateSubMatrices() 6946 @*/ 6947 PetscErrorCode MatDestroySubMatrices(PetscInt n,Mat *mat[]) 6948 { 6949 PetscErrorCode ierr; 6950 Mat mat0; 6951 6952 PetscFunctionBegin; 6953 if (!*mat) PetscFunctionReturn(0); 6954 /* mat[] is an array of length n+1, see MatCreateSubMatrices_xxx() */ 6955 if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Trying to destroy negative number of matrices %D",n); 6956 PetscValidPointer(mat,2); 6957 6958 mat0 = (*mat)[0]; 6959 if (mat0 && mat0->ops->destroysubmatrices) { 6960 ierr = (mat0->ops->destroysubmatrices)(n,mat);CHKERRQ(ierr); 6961 } else { 6962 ierr = MatDestroyMatrices(n,mat);CHKERRQ(ierr); 6963 } 6964 PetscFunctionReturn(0); 6965 } 6966 6967 /*@C 6968 MatGetSeqNonzeroStructure - Extracts the sequential nonzero structure from a matrix. 6969 6970 Collective on Mat 6971 6972 Input Parameters: 6973 . mat - the matrix 6974 6975 Output Parameter: 6976 . matstruct - the sequential matrix with the nonzero structure of mat 6977 6978 Level: intermediate 6979 6980 .seealso: MatDestroySeqNonzeroStructure(), MatCreateSubMatrices(), MatDestroyMatrices() 6981 @*/ 6982 PetscErrorCode MatGetSeqNonzeroStructure(Mat mat,Mat *matstruct) 6983 { 6984 PetscErrorCode ierr; 6985 6986 PetscFunctionBegin; 6987 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6988 PetscValidPointer(matstruct,2); 6989 6990 PetscValidType(mat,1); 6991 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6992 MatCheckPreallocated(mat,1); 6993 6994 if (!mat->ops->getseqnonzerostructure) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Not for matrix type %s\n",((PetscObject)mat)->type_name); 6995 ierr = PetscLogEventBegin(MAT_GetSeqNonzeroStructure,mat,0,0,0);CHKERRQ(ierr); 6996 ierr = (*mat->ops->getseqnonzerostructure)(mat,matstruct);CHKERRQ(ierr); 6997 ierr = PetscLogEventEnd(MAT_GetSeqNonzeroStructure,mat,0,0,0);CHKERRQ(ierr); 6998 PetscFunctionReturn(0); 6999 } 7000 7001 /*@C 7002 MatDestroySeqNonzeroStructure - Destroys matrix obtained with MatGetSeqNonzeroStructure(). 7003 7004 Collective on Mat 7005 7006 Input Parameters: 7007 . mat - the matrix (note that this is a pointer to the array of matrices, just to match the calling 7008 sequence of MatGetSequentialNonzeroStructure()) 7009 7010 Level: advanced 7011 7012 Notes: 7013 Frees not only the matrices, but also the array that contains the matrices 7014 7015 .seealso: MatGetSeqNonzeroStructure() 7016 @*/ 7017 PetscErrorCode MatDestroySeqNonzeroStructure(Mat *mat) 7018 { 7019 PetscErrorCode ierr; 7020 7021 PetscFunctionBegin; 7022 PetscValidPointer(mat,1); 7023 ierr = MatDestroy(mat);CHKERRQ(ierr); 7024 PetscFunctionReturn(0); 7025 } 7026 7027 /*@ 7028 MatIncreaseOverlap - Given a set of submatrices indicated by index sets, 7029 replaces the index sets by larger ones that represent submatrices with 7030 additional overlap. 7031 7032 Collective on Mat 7033 7034 Input Parameters: 7035 + mat - the matrix 7036 . n - the number of index sets 7037 . is - the array of index sets (these index sets will changed during the call) 7038 - ov - the additional overlap requested 7039 7040 Options Database: 7041 . -mat_increase_overlap_scalable - use a scalable algorithm to compute the overlap (supported by MPIAIJ matrix) 7042 7043 Level: developer 7044 7045 Concepts: overlap 7046 Concepts: ASM^computing overlap 7047 7048 .seealso: MatCreateSubMatrices() 7049 @*/ 7050 PetscErrorCode MatIncreaseOverlap(Mat mat,PetscInt n,IS is[],PetscInt ov) 7051 { 7052 PetscErrorCode ierr; 7053 7054 PetscFunctionBegin; 7055 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7056 PetscValidType(mat,1); 7057 if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Must have one or more domains, you have %D",n); 7058 if (n) { 7059 PetscValidPointer(is,3); 7060 PetscValidHeaderSpecific(*is,IS_CLASSID,3); 7061 } 7062 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 7063 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7064 MatCheckPreallocated(mat,1); 7065 7066 if (!ov) PetscFunctionReturn(0); 7067 if (!mat->ops->increaseoverlap) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 7068 ierr = PetscLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr); 7069 ierr = (*mat->ops->increaseoverlap)(mat,n,is,ov);CHKERRQ(ierr); 7070 ierr = PetscLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr); 7071 PetscFunctionReturn(0); 7072 } 7073 7074 7075 PetscErrorCode MatIncreaseOverlapSplit_Single(Mat,IS*,PetscInt); 7076 7077 /*@ 7078 MatIncreaseOverlapSplit - Given a set of submatrices indicated by index sets across 7079 a sub communicator, replaces the index sets by larger ones that represent submatrices with 7080 additional overlap. 7081 7082 Collective on Mat 7083 7084 Input Parameters: 7085 + mat - the matrix 7086 . n - the number of index sets 7087 . is - the array of index sets (these index sets will changed during the call) 7088 - ov - the additional overlap requested 7089 7090 Options Database: 7091 . -mat_increase_overlap_scalable - use a scalable algorithm to compute the overlap (supported by MPIAIJ matrix) 7092 7093 Level: developer 7094 7095 Concepts: overlap 7096 Concepts: ASM^computing overlap 7097 7098 .seealso: MatCreateSubMatrices() 7099 @*/ 7100 PetscErrorCode MatIncreaseOverlapSplit(Mat mat,PetscInt n,IS is[],PetscInt ov) 7101 { 7102 PetscInt i; 7103 PetscErrorCode ierr; 7104 7105 PetscFunctionBegin; 7106 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7107 PetscValidType(mat,1); 7108 if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Must have one or more domains, you have %D",n); 7109 if (n) { 7110 PetscValidPointer(is,3); 7111 PetscValidHeaderSpecific(*is,IS_CLASSID,3); 7112 } 7113 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 7114 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7115 MatCheckPreallocated(mat,1); 7116 if (!ov) PetscFunctionReturn(0); 7117 ierr = PetscLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr); 7118 for(i=0; i<n; i++){ 7119 ierr = MatIncreaseOverlapSplit_Single(mat,&is[i],ov);CHKERRQ(ierr); 7120 } 7121 ierr = PetscLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr); 7122 PetscFunctionReturn(0); 7123 } 7124 7125 7126 7127 7128 /*@ 7129 MatGetBlockSize - Returns the matrix block size. 7130 7131 Not Collective 7132 7133 Input Parameter: 7134 . mat - the matrix 7135 7136 Output Parameter: 7137 . bs - block size 7138 7139 Notes: 7140 Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ. These formats ALWAYS have square block storage in the matrix. 7141 7142 If the block size has not been set yet this routine returns 1. 7143 7144 Level: intermediate 7145 7146 Concepts: matrices^block size 7147 7148 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSizes() 7149 @*/ 7150 PetscErrorCode MatGetBlockSize(Mat mat,PetscInt *bs) 7151 { 7152 PetscFunctionBegin; 7153 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7154 PetscValidIntPointer(bs,2); 7155 *bs = PetscAbs(mat->rmap->bs); 7156 PetscFunctionReturn(0); 7157 } 7158 7159 /*@ 7160 MatGetBlockSizes - Returns the matrix block row and column sizes. 7161 7162 Not Collective 7163 7164 Input Parameter: 7165 . mat - the matrix 7166 7167 Output Parameter: 7168 . rbs - row block size 7169 . cbs - column block size 7170 7171 Notes: 7172 Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ. These formats ALWAYS have square block storage in the matrix. 7173 If you pass a different block size for the columns than the rows, the row block size determines the square block storage. 7174 7175 If a block size has not been set yet this routine returns 1. 7176 7177 Level: intermediate 7178 7179 Concepts: matrices^block size 7180 7181 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSize(), MatSetBlockSizes() 7182 @*/ 7183 PetscErrorCode MatGetBlockSizes(Mat mat,PetscInt *rbs, PetscInt *cbs) 7184 { 7185 PetscFunctionBegin; 7186 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7187 if (rbs) PetscValidIntPointer(rbs,2); 7188 if (cbs) PetscValidIntPointer(cbs,3); 7189 if (rbs) *rbs = PetscAbs(mat->rmap->bs); 7190 if (cbs) *cbs = PetscAbs(mat->cmap->bs); 7191 PetscFunctionReturn(0); 7192 } 7193 7194 /*@ 7195 MatSetBlockSize - Sets the matrix block size. 7196 7197 Logically Collective on Mat 7198 7199 Input Parameters: 7200 + mat - the matrix 7201 - bs - block size 7202 7203 Notes: 7204 Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ. These formats ALWAYS have square block storage in the matrix. 7205 This must be called before MatSetUp() or MatXXXSetPreallocation() (or will default to 1) and the block size cannot be changed later. 7206 7207 For MATMPIAIJ and MATSEQAIJ matrix formats, this function can be called at a later stage, provided that the specified block size 7208 is compatible with the matrix local sizes. 7209 7210 Level: intermediate 7211 7212 Concepts: matrices^block size 7213 7214 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSizes(), MatGetBlockSizes() 7215 @*/ 7216 PetscErrorCode MatSetBlockSize(Mat mat,PetscInt bs) 7217 { 7218 PetscErrorCode ierr; 7219 7220 PetscFunctionBegin; 7221 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7222 PetscValidLogicalCollectiveInt(mat,bs,2); 7223 ierr = MatSetBlockSizes(mat,bs,bs);CHKERRQ(ierr); 7224 PetscFunctionReturn(0); 7225 } 7226 7227 /*@ 7228 MatSetVariableBlockSizes - Sets a diagonal blocks of the matrix that need not be of the same size 7229 7230 Logically Collective on Mat 7231 7232 Input Parameters: 7233 + mat - the matrix 7234 . nblocks - the number of blocks on this process 7235 - bsizes - the block sizes 7236 7237 Notes: 7238 Currently used by PCVPBJACOBI for SeqAIJ matrices 7239 7240 Level: intermediate 7241 7242 Concepts: matrices^block size 7243 7244 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSizes(), MatGetBlockSizes(), MatGetVariableBlockSizes() 7245 @*/ 7246 PetscErrorCode MatSetVariableBlockSizes(Mat mat,PetscInt nblocks,PetscInt *bsizes) 7247 { 7248 PetscErrorCode ierr; 7249 PetscInt i,ncnt = 0, nlocal; 7250 7251 PetscFunctionBegin; 7252 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7253 if (nblocks < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Number of local blocks must be great than or equal to zero"); 7254 ierr = MatGetLocalSize(mat,&nlocal,NULL);CHKERRQ(ierr); 7255 for (i=0; i<nblocks; i++) ncnt += bsizes[i]; 7256 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); 7257 ierr = PetscFree(mat->bsizes);CHKERRQ(ierr); 7258 mat->nblocks = nblocks; 7259 ierr = PetscMalloc1(nblocks,&mat->bsizes);CHKERRQ(ierr); 7260 ierr = PetscMemcpy(mat->bsizes,bsizes,nblocks*sizeof(PetscInt));CHKERRQ(ierr); 7261 PetscFunctionReturn(0); 7262 } 7263 7264 /*@C 7265 MatGetVariableBlockSizes - Gets a diagonal blocks of the matrix that need not be of the same size 7266 7267 Logically Collective on Mat 7268 7269 Input Parameters: 7270 . mat - the matrix 7271 7272 Output Parameters: 7273 + nblocks - the number of blocks on this process 7274 - bsizes - the block sizes 7275 7276 Notes: Currently not supported from Fortran 7277 7278 Level: intermediate 7279 7280 Concepts: matrices^block size 7281 7282 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSizes(), MatGetBlockSizes(), MatSetVariableBlockSizes() 7283 @*/ 7284 PetscErrorCode MatGetVariableBlockSizes(Mat mat,PetscInt *nblocks,const PetscInt **bsizes) 7285 { 7286 PetscFunctionBegin; 7287 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7288 *nblocks = mat->nblocks; 7289 *bsizes = mat->bsizes; 7290 PetscFunctionReturn(0); 7291 } 7292 7293 /*@ 7294 MatSetBlockSizes - Sets the matrix block row and column sizes. 7295 7296 Logically Collective on Mat 7297 7298 Input Parameters: 7299 + mat - the matrix 7300 - rbs - row block size 7301 - cbs - column block size 7302 7303 Notes: 7304 Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ. These formats ALWAYS have square block storage in the matrix. 7305 If you pass a different block size for the columns than the rows, the row block size determines the square block storage. 7306 This must be called before MatSetUp() or MatXXXSetPreallocation() (or will default to 1) and the block size cannot be changed later 7307 7308 For MATMPIAIJ and MATSEQAIJ matrix formats, this function can be called at a later stage, provided that the specified block sizes 7309 are compatible with the matrix local sizes. 7310 7311 The row and column block size determine the blocksize of the "row" and "column" vectors returned by MatCreateVecs(). 7312 7313 Level: intermediate 7314 7315 Concepts: matrices^block size 7316 7317 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSize(), MatGetBlockSizes() 7318 @*/ 7319 PetscErrorCode MatSetBlockSizes(Mat mat,PetscInt rbs,PetscInt cbs) 7320 { 7321 PetscErrorCode ierr; 7322 7323 PetscFunctionBegin; 7324 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7325 PetscValidLogicalCollectiveInt(mat,rbs,2); 7326 PetscValidLogicalCollectiveInt(mat,cbs,3); 7327 if (mat->ops->setblocksizes) { 7328 ierr = (*mat->ops->setblocksizes)(mat,rbs,cbs);CHKERRQ(ierr); 7329 } 7330 if (mat->rmap->refcnt) { 7331 ISLocalToGlobalMapping l2g = NULL; 7332 PetscLayout nmap = NULL; 7333 7334 ierr = PetscLayoutDuplicate(mat->rmap,&nmap);CHKERRQ(ierr); 7335 if (mat->rmap->mapping) { 7336 ierr = ISLocalToGlobalMappingDuplicate(mat->rmap->mapping,&l2g);CHKERRQ(ierr); 7337 } 7338 ierr = PetscLayoutDestroy(&mat->rmap);CHKERRQ(ierr); 7339 mat->rmap = nmap; 7340 mat->rmap->mapping = l2g; 7341 } 7342 if (mat->cmap->refcnt) { 7343 ISLocalToGlobalMapping l2g = NULL; 7344 PetscLayout nmap = NULL; 7345 7346 ierr = PetscLayoutDuplicate(mat->cmap,&nmap);CHKERRQ(ierr); 7347 if (mat->cmap->mapping) { 7348 ierr = ISLocalToGlobalMappingDuplicate(mat->cmap->mapping,&l2g);CHKERRQ(ierr); 7349 } 7350 ierr = PetscLayoutDestroy(&mat->cmap);CHKERRQ(ierr); 7351 mat->cmap = nmap; 7352 mat->cmap->mapping = l2g; 7353 } 7354 ierr = PetscLayoutSetBlockSize(mat->rmap,rbs);CHKERRQ(ierr); 7355 ierr = PetscLayoutSetBlockSize(mat->cmap,cbs);CHKERRQ(ierr); 7356 PetscFunctionReturn(0); 7357 } 7358 7359 /*@ 7360 MatSetBlockSizesFromMats - Sets the matrix block row and column sizes to match a pair of matrices 7361 7362 Logically Collective on Mat 7363 7364 Input Parameters: 7365 + mat - the matrix 7366 . fromRow - matrix from which to copy row block size 7367 - fromCol - matrix from which to copy column block size (can be same as fromRow) 7368 7369 Level: developer 7370 7371 Concepts: matrices^block size 7372 7373 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSizes() 7374 @*/ 7375 PetscErrorCode MatSetBlockSizesFromMats(Mat mat,Mat fromRow,Mat fromCol) 7376 { 7377 PetscErrorCode ierr; 7378 7379 PetscFunctionBegin; 7380 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7381 PetscValidHeaderSpecific(fromRow,MAT_CLASSID,2); 7382 PetscValidHeaderSpecific(fromCol,MAT_CLASSID,3); 7383 if (fromRow->rmap->bs > 0) {ierr = PetscLayoutSetBlockSize(mat->rmap,fromRow->rmap->bs);CHKERRQ(ierr);} 7384 if (fromCol->cmap->bs > 0) {ierr = PetscLayoutSetBlockSize(mat->cmap,fromCol->cmap->bs);CHKERRQ(ierr);} 7385 PetscFunctionReturn(0); 7386 } 7387 7388 /*@ 7389 MatResidual - Default routine to calculate the residual. 7390 7391 Collective on Mat and Vec 7392 7393 Input Parameters: 7394 + mat - the matrix 7395 . b - the right-hand-side 7396 - x - the approximate solution 7397 7398 Output Parameter: 7399 . r - location to store the residual 7400 7401 Level: developer 7402 7403 .keywords: MG, default, multigrid, residual 7404 7405 .seealso: PCMGSetResidual() 7406 @*/ 7407 PetscErrorCode MatResidual(Mat mat,Vec b,Vec x,Vec r) 7408 { 7409 PetscErrorCode ierr; 7410 7411 PetscFunctionBegin; 7412 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7413 PetscValidHeaderSpecific(b,VEC_CLASSID,2); 7414 PetscValidHeaderSpecific(x,VEC_CLASSID,3); 7415 PetscValidHeaderSpecific(r,VEC_CLASSID,4); 7416 PetscValidType(mat,1); 7417 MatCheckPreallocated(mat,1); 7418 ierr = PetscLogEventBegin(MAT_Residual,mat,0,0,0);CHKERRQ(ierr); 7419 if (!mat->ops->residual) { 7420 ierr = MatMult(mat,x,r);CHKERRQ(ierr); 7421 ierr = VecAYPX(r,-1.0,b);CHKERRQ(ierr); 7422 } else { 7423 ierr = (*mat->ops->residual)(mat,b,x,r);CHKERRQ(ierr); 7424 } 7425 ierr = PetscLogEventEnd(MAT_Residual,mat,0,0,0);CHKERRQ(ierr); 7426 PetscFunctionReturn(0); 7427 } 7428 7429 /*@C 7430 MatGetRowIJ - Returns the compressed row storage i and j indices for sequential matrices. 7431 7432 Collective on Mat 7433 7434 Input Parameters: 7435 + mat - the matrix 7436 . shift - 0 or 1 indicating we want the indices starting at 0 or 1 7437 . symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be symmetrized 7438 - inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the 7439 inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is 7440 always used. 7441 7442 Output Parameters: 7443 + n - number of rows in the (possibly compressed) matrix 7444 . ia - the row pointers [of length n+1] 7445 . ja - the column indices 7446 - done - indicates if the routine actually worked and returned appropriate ia[] and ja[] arrays; callers 7447 are responsible for handling the case when done == PETSC_FALSE and ia and ja are not set 7448 7449 Level: developer 7450 7451 Notes: 7452 You CANNOT change any of the ia[] or ja[] values. 7453 7454 Use MatRestoreRowIJ() when you are finished accessing the ia[] and ja[] values. 7455 7456 Fortran Notes: 7457 In Fortran use 7458 $ 7459 $ PetscInt ia(1), ja(1) 7460 $ PetscOffset iia, jja 7461 $ call MatGetRowIJ(mat,shift,symmetric,inodecompressed,n,ia,iia,ja,jja,done,ierr) 7462 $ ! Access the ith and jth entries via ia(iia + i) and ja(jja + j) 7463 7464 or 7465 $ 7466 $ PetscInt, pointer :: ia(:),ja(:) 7467 $ call MatGetRowIJF90(mat,shift,symmetric,inodecompressed,n,ia,ja,done,ierr) 7468 $ ! Access the ith and jth entries via ia(i) and ja(j) 7469 7470 .seealso: MatGetColumnIJ(), MatRestoreRowIJ(), MatSeqAIJGetArray() 7471 @*/ 7472 PetscErrorCode MatGetRowIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool *done) 7473 { 7474 PetscErrorCode ierr; 7475 7476 PetscFunctionBegin; 7477 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7478 PetscValidType(mat,1); 7479 PetscValidIntPointer(n,5); 7480 if (ia) PetscValidIntPointer(ia,6); 7481 if (ja) PetscValidIntPointer(ja,7); 7482 PetscValidIntPointer(done,8); 7483 MatCheckPreallocated(mat,1); 7484 if (!mat->ops->getrowij) *done = PETSC_FALSE; 7485 else { 7486 *done = PETSC_TRUE; 7487 ierr = PetscLogEventBegin(MAT_GetRowIJ,mat,0,0,0);CHKERRQ(ierr); 7488 ierr = (*mat->ops->getrowij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr); 7489 ierr = PetscLogEventEnd(MAT_GetRowIJ,mat,0,0,0);CHKERRQ(ierr); 7490 } 7491 PetscFunctionReturn(0); 7492 } 7493 7494 /*@C 7495 MatGetColumnIJ - Returns the compressed column storage i and j indices for sequential matrices. 7496 7497 Collective on Mat 7498 7499 Input Parameters: 7500 + mat - the matrix 7501 . shift - 1 or zero indicating we want the indices starting at 0 or 1 7502 . symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 7503 symmetrized 7504 . inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the 7505 inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is 7506 always used. 7507 . n - number of columns in the (possibly compressed) matrix 7508 . ia - the column pointers 7509 - ja - the row indices 7510 7511 Output Parameters: 7512 . done - PETSC_TRUE or PETSC_FALSE, indicating whether the values have been returned 7513 7514 Note: 7515 This routine zeros out n, ia, and ja. This is to prevent accidental 7516 us of the array after it has been restored. If you pass NULL, it will 7517 not zero the pointers. Use of ia or ja after MatRestoreColumnIJ() is invalid. 7518 7519 Level: developer 7520 7521 .seealso: MatGetRowIJ(), MatRestoreColumnIJ() 7522 @*/ 7523 PetscErrorCode MatGetColumnIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool *done) 7524 { 7525 PetscErrorCode ierr; 7526 7527 PetscFunctionBegin; 7528 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7529 PetscValidType(mat,1); 7530 PetscValidIntPointer(n,4); 7531 if (ia) PetscValidIntPointer(ia,5); 7532 if (ja) PetscValidIntPointer(ja,6); 7533 PetscValidIntPointer(done,7); 7534 MatCheckPreallocated(mat,1); 7535 if (!mat->ops->getcolumnij) *done = PETSC_FALSE; 7536 else { 7537 *done = PETSC_TRUE; 7538 ierr = (*mat->ops->getcolumnij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr); 7539 } 7540 PetscFunctionReturn(0); 7541 } 7542 7543 /*@C 7544 MatRestoreRowIJ - Call after you are completed with the ia,ja indices obtained with 7545 MatGetRowIJ(). 7546 7547 Collective on Mat 7548 7549 Input Parameters: 7550 + mat - the matrix 7551 . shift - 1 or zero indicating we want the indices starting at 0 or 1 7552 . symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 7553 symmetrized 7554 . inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the 7555 inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is 7556 always used. 7557 . n - size of (possibly compressed) matrix 7558 . ia - the row pointers 7559 - ja - the column indices 7560 7561 Output Parameters: 7562 . done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned 7563 7564 Note: 7565 This routine zeros out n, ia, and ja. This is to prevent accidental 7566 us of the array after it has been restored. If you pass NULL, it will 7567 not zero the pointers. Use of ia or ja after MatRestoreRowIJ() is invalid. 7568 7569 Level: developer 7570 7571 .seealso: MatGetRowIJ(), MatRestoreColumnIJ() 7572 @*/ 7573 PetscErrorCode MatRestoreRowIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool *done) 7574 { 7575 PetscErrorCode ierr; 7576 7577 PetscFunctionBegin; 7578 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7579 PetscValidType(mat,1); 7580 if (ia) PetscValidIntPointer(ia,6); 7581 if (ja) PetscValidIntPointer(ja,7); 7582 PetscValidIntPointer(done,8); 7583 MatCheckPreallocated(mat,1); 7584 7585 if (!mat->ops->restorerowij) *done = PETSC_FALSE; 7586 else { 7587 *done = PETSC_TRUE; 7588 ierr = (*mat->ops->restorerowij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr); 7589 if (n) *n = 0; 7590 if (ia) *ia = NULL; 7591 if (ja) *ja = NULL; 7592 } 7593 PetscFunctionReturn(0); 7594 } 7595 7596 /*@C 7597 MatRestoreColumnIJ - Call after you are completed with the ia,ja indices obtained with 7598 MatGetColumnIJ(). 7599 7600 Collective on Mat 7601 7602 Input Parameters: 7603 + mat - the matrix 7604 . shift - 1 or zero indicating we want the indices starting at 0 or 1 7605 - symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 7606 symmetrized 7607 - inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the 7608 inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is 7609 always used. 7610 7611 Output Parameters: 7612 + n - size of (possibly compressed) matrix 7613 . ia - the column pointers 7614 . ja - the row indices 7615 - done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned 7616 7617 Level: developer 7618 7619 .seealso: MatGetColumnIJ(), MatRestoreRowIJ() 7620 @*/ 7621 PetscErrorCode MatRestoreColumnIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool *done) 7622 { 7623 PetscErrorCode ierr; 7624 7625 PetscFunctionBegin; 7626 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7627 PetscValidType(mat,1); 7628 if (ia) PetscValidIntPointer(ia,5); 7629 if (ja) PetscValidIntPointer(ja,6); 7630 PetscValidIntPointer(done,7); 7631 MatCheckPreallocated(mat,1); 7632 7633 if (!mat->ops->restorecolumnij) *done = PETSC_FALSE; 7634 else { 7635 *done = PETSC_TRUE; 7636 ierr = (*mat->ops->restorecolumnij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr); 7637 if (n) *n = 0; 7638 if (ia) *ia = NULL; 7639 if (ja) *ja = NULL; 7640 } 7641 PetscFunctionReturn(0); 7642 } 7643 7644 /*@C 7645 MatColoringPatch -Used inside matrix coloring routines that 7646 use MatGetRowIJ() and/or MatGetColumnIJ(). 7647 7648 Collective on Mat 7649 7650 Input Parameters: 7651 + mat - the matrix 7652 . ncolors - max color value 7653 . n - number of entries in colorarray 7654 - colorarray - array indicating color for each column 7655 7656 Output Parameters: 7657 . iscoloring - coloring generated using colorarray information 7658 7659 Level: developer 7660 7661 .seealso: MatGetRowIJ(), MatGetColumnIJ() 7662 7663 @*/ 7664 PetscErrorCode MatColoringPatch(Mat mat,PetscInt ncolors,PetscInt n,ISColoringValue colorarray[],ISColoring *iscoloring) 7665 { 7666 PetscErrorCode ierr; 7667 7668 PetscFunctionBegin; 7669 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7670 PetscValidType(mat,1); 7671 PetscValidIntPointer(colorarray,4); 7672 PetscValidPointer(iscoloring,5); 7673 MatCheckPreallocated(mat,1); 7674 7675 if (!mat->ops->coloringpatch) { 7676 ierr = ISColoringCreate(PetscObjectComm((PetscObject)mat),ncolors,n,colorarray,PETSC_OWN_POINTER,iscoloring);CHKERRQ(ierr); 7677 } else { 7678 ierr = (*mat->ops->coloringpatch)(mat,ncolors,n,colorarray,iscoloring);CHKERRQ(ierr); 7679 } 7680 PetscFunctionReturn(0); 7681 } 7682 7683 7684 /*@ 7685 MatSetUnfactored - Resets a factored matrix to be treated as unfactored. 7686 7687 Logically Collective on Mat 7688 7689 Input Parameter: 7690 . mat - the factored matrix to be reset 7691 7692 Notes: 7693 This routine should be used only with factored matrices formed by in-place 7694 factorization via ILU(0) (or by in-place LU factorization for the MATSEQDENSE 7695 format). This option can save memory, for example, when solving nonlinear 7696 systems with a matrix-free Newton-Krylov method and a matrix-based, in-place 7697 ILU(0) preconditioner. 7698 7699 Note that one can specify in-place ILU(0) factorization by calling 7700 .vb 7701 PCType(pc,PCILU); 7702 PCFactorSeUseInPlace(pc); 7703 .ve 7704 or by using the options -pc_type ilu -pc_factor_in_place 7705 7706 In-place factorization ILU(0) can also be used as a local 7707 solver for the blocks within the block Jacobi or additive Schwarz 7708 methods (runtime option: -sub_pc_factor_in_place). See Users-Manual: ch_pc 7709 for details on setting local solver options. 7710 7711 Most users should employ the simplified KSP interface for linear solvers 7712 instead of working directly with matrix algebra routines such as this. 7713 See, e.g., KSPCreate(). 7714 7715 Level: developer 7716 7717 .seealso: PCFactorSetUseInPlace(), PCFactorGetUseInPlace() 7718 7719 Concepts: matrices^unfactored 7720 7721 @*/ 7722 PetscErrorCode MatSetUnfactored(Mat mat) 7723 { 7724 PetscErrorCode ierr; 7725 7726 PetscFunctionBegin; 7727 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7728 PetscValidType(mat,1); 7729 MatCheckPreallocated(mat,1); 7730 mat->factortype = MAT_FACTOR_NONE; 7731 if (!mat->ops->setunfactored) PetscFunctionReturn(0); 7732 ierr = (*mat->ops->setunfactored)(mat);CHKERRQ(ierr); 7733 PetscFunctionReturn(0); 7734 } 7735 7736 /*MC 7737 MatDenseGetArrayF90 - Accesses a matrix array from Fortran90. 7738 7739 Synopsis: 7740 MatDenseGetArrayF90(Mat x,{Scalar, pointer :: xx_v(:,:)},integer ierr) 7741 7742 Not collective 7743 7744 Input Parameter: 7745 . x - matrix 7746 7747 Output Parameters: 7748 + xx_v - the Fortran90 pointer to the array 7749 - ierr - error code 7750 7751 Example of Usage: 7752 .vb 7753 PetscScalar, pointer xx_v(:,:) 7754 .... 7755 call MatDenseGetArrayF90(x,xx_v,ierr) 7756 a = xx_v(3) 7757 call MatDenseRestoreArrayF90(x,xx_v,ierr) 7758 .ve 7759 7760 Level: advanced 7761 7762 .seealso: MatDenseRestoreArrayF90(), MatDenseGetArray(), MatDenseRestoreArray(), MatSeqAIJGetArrayF90() 7763 7764 Concepts: matrices^accessing array 7765 7766 M*/ 7767 7768 /*MC 7769 MatDenseRestoreArrayF90 - Restores a matrix array that has been 7770 accessed with MatDenseGetArrayF90(). 7771 7772 Synopsis: 7773 MatDenseRestoreArrayF90(Mat x,{Scalar, pointer :: xx_v(:,:)},integer ierr) 7774 7775 Not collective 7776 7777 Input Parameters: 7778 + x - matrix 7779 - xx_v - the Fortran90 pointer to the array 7780 7781 Output Parameter: 7782 . ierr - error code 7783 7784 Example of Usage: 7785 .vb 7786 PetscScalar, pointer xx_v(:,:) 7787 .... 7788 call MatDenseGetArrayF90(x,xx_v,ierr) 7789 a = xx_v(3) 7790 call MatDenseRestoreArrayF90(x,xx_v,ierr) 7791 .ve 7792 7793 Level: advanced 7794 7795 .seealso: MatDenseGetArrayF90(), MatDenseGetArray(), MatDenseRestoreArray(), MatSeqAIJRestoreArrayF90() 7796 7797 M*/ 7798 7799 7800 /*MC 7801 MatSeqAIJGetArrayF90 - Accesses a matrix array from Fortran90. 7802 7803 Synopsis: 7804 MatSeqAIJGetArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr) 7805 7806 Not collective 7807 7808 Input Parameter: 7809 . x - matrix 7810 7811 Output Parameters: 7812 + xx_v - the Fortran90 pointer to the array 7813 - ierr - error code 7814 7815 Example of Usage: 7816 .vb 7817 PetscScalar, pointer xx_v(:) 7818 .... 7819 call MatSeqAIJGetArrayF90(x,xx_v,ierr) 7820 a = xx_v(3) 7821 call MatSeqAIJRestoreArrayF90(x,xx_v,ierr) 7822 .ve 7823 7824 Level: advanced 7825 7826 .seealso: MatSeqAIJRestoreArrayF90(), MatSeqAIJGetArray(), MatSeqAIJRestoreArray(), MatDenseGetArrayF90() 7827 7828 Concepts: matrices^accessing array 7829 7830 M*/ 7831 7832 /*MC 7833 MatSeqAIJRestoreArrayF90 - Restores a matrix array that has been 7834 accessed with MatSeqAIJGetArrayF90(). 7835 7836 Synopsis: 7837 MatSeqAIJRestoreArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr) 7838 7839 Not collective 7840 7841 Input Parameters: 7842 + x - matrix 7843 - xx_v - the Fortran90 pointer to the array 7844 7845 Output Parameter: 7846 . ierr - error code 7847 7848 Example of Usage: 7849 .vb 7850 PetscScalar, pointer xx_v(:) 7851 .... 7852 call MatSeqAIJGetArrayF90(x,xx_v,ierr) 7853 a = xx_v(3) 7854 call MatSeqAIJRestoreArrayF90(x,xx_v,ierr) 7855 .ve 7856 7857 Level: advanced 7858 7859 .seealso: MatSeqAIJGetArrayF90(), MatSeqAIJGetArray(), MatSeqAIJRestoreArray(), MatDenseRestoreArrayF90() 7860 7861 M*/ 7862 7863 7864 /*@ 7865 MatCreateSubMatrix - Gets a single submatrix on the same number of processors 7866 as the original matrix. 7867 7868 Collective on Mat 7869 7870 Input Parameters: 7871 + mat - the original matrix 7872 . isrow - parallel IS containing the rows this processor should obtain 7873 . 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. 7874 - cll - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 7875 7876 Output Parameter: 7877 . newmat - the new submatrix, of the same type as the old 7878 7879 Level: advanced 7880 7881 Notes: 7882 The submatrix will be able to be multiplied with vectors using the same layout as iscol. 7883 7884 Some matrix types place restrictions on the row and column indices, such 7885 as that they be sorted or that they be equal to each other. 7886 7887 The index sets may not have duplicate entries. 7888 7889 The first time this is called you should use a cll of MAT_INITIAL_MATRIX, 7890 the MatCreateSubMatrix() routine will create the newmat for you. Any additional calls 7891 to this routine with a mat of the same nonzero structure and with a call of MAT_REUSE_MATRIX 7892 will reuse the matrix generated the first time. You should call MatDestroy() on newmat when 7893 you are finished using it. 7894 7895 The communicator of the newly obtained matrix is ALWAYS the same as the communicator of 7896 the input matrix. 7897 7898 If iscol is NULL then all columns are obtained (not supported in Fortran). 7899 7900 Example usage: 7901 Consider the following 8x8 matrix with 34 non-zero values, that is 7902 assembled across 3 processors. Let's assume that proc0 owns 3 rows, 7903 proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown 7904 as follows: 7905 7906 .vb 7907 1 2 0 | 0 3 0 | 0 4 7908 Proc0 0 5 6 | 7 0 0 | 8 0 7909 9 0 10 | 11 0 0 | 12 0 7910 ------------------------------------- 7911 13 0 14 | 15 16 17 | 0 0 7912 Proc1 0 18 0 | 19 20 21 | 0 0 7913 0 0 0 | 22 23 0 | 24 0 7914 ------------------------------------- 7915 Proc2 25 26 27 | 0 0 28 | 29 0 7916 30 0 0 | 31 32 33 | 0 34 7917 .ve 7918 7919 Suppose isrow = [0 1 | 4 | 6 7] and iscol = [1 2 | 3 4 5 | 6]. The resulting submatrix is 7920 7921 .vb 7922 2 0 | 0 3 0 | 0 7923 Proc0 5 6 | 7 0 0 | 8 7924 ------------------------------- 7925 Proc1 18 0 | 19 20 21 | 0 7926 ------------------------------- 7927 Proc2 26 27 | 0 0 28 | 29 7928 0 0 | 31 32 33 | 0 7929 .ve 7930 7931 7932 Concepts: matrices^submatrices 7933 7934 .seealso: MatCreateSubMatrices() 7935 @*/ 7936 PetscErrorCode MatCreateSubMatrix(Mat mat,IS isrow,IS iscol,MatReuse cll,Mat *newmat) 7937 { 7938 PetscErrorCode ierr; 7939 PetscMPIInt size; 7940 Mat *local; 7941 IS iscoltmp; 7942 7943 PetscFunctionBegin; 7944 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7945 PetscValidHeaderSpecific(isrow,IS_CLASSID,2); 7946 if (iscol) PetscValidHeaderSpecific(iscol,IS_CLASSID,3); 7947 PetscValidPointer(newmat,5); 7948 if (cll == MAT_REUSE_MATRIX) PetscValidHeaderSpecific(*newmat,MAT_CLASSID,5); 7949 PetscValidType(mat,1); 7950 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7951 if (cll == MAT_IGNORE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Cannot use MAT_IGNORE_MATRIX"); 7952 7953 MatCheckPreallocated(mat,1); 7954 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);CHKERRQ(ierr); 7955 7956 if (!iscol || isrow == iscol) { 7957 PetscBool stride; 7958 PetscMPIInt grabentirematrix = 0,grab; 7959 ierr = PetscObjectTypeCompare((PetscObject)isrow,ISSTRIDE,&stride);CHKERRQ(ierr); 7960 if (stride) { 7961 PetscInt first,step,n,rstart,rend; 7962 ierr = ISStrideGetInfo(isrow,&first,&step);CHKERRQ(ierr); 7963 if (step == 1) { 7964 ierr = MatGetOwnershipRange(mat,&rstart,&rend);CHKERRQ(ierr); 7965 if (rstart == first) { 7966 ierr = ISGetLocalSize(isrow,&n);CHKERRQ(ierr); 7967 if (n == rend-rstart) { 7968 grabentirematrix = 1; 7969 } 7970 } 7971 } 7972 } 7973 ierr = MPIU_Allreduce(&grabentirematrix,&grab,1,MPI_INT,MPI_MIN,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 7974 if (grab) { 7975 ierr = PetscInfo(mat,"Getting entire matrix as submatrix\n");CHKERRQ(ierr); 7976 if (cll == MAT_INITIAL_MATRIX) { 7977 *newmat = mat; 7978 ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr); 7979 } 7980 PetscFunctionReturn(0); 7981 } 7982 } 7983 7984 if (!iscol) { 7985 ierr = ISCreateStride(PetscObjectComm((PetscObject)mat),mat->cmap->n,mat->cmap->rstart,1,&iscoltmp);CHKERRQ(ierr); 7986 } else { 7987 iscoltmp = iscol; 7988 } 7989 7990 /* if original matrix is on just one processor then use submatrix generated */ 7991 if (mat->ops->createsubmatrices && !mat->ops->createsubmatrix && size == 1 && cll == MAT_REUSE_MATRIX) { 7992 ierr = MatCreateSubMatrices(mat,1,&isrow,&iscoltmp,MAT_REUSE_MATRIX,&newmat);CHKERRQ(ierr); 7993 if (!iscol) {ierr = ISDestroy(&iscoltmp);CHKERRQ(ierr);} 7994 PetscFunctionReturn(0); 7995 } else if (mat->ops->createsubmatrices && !mat->ops->createsubmatrix && size == 1) { 7996 ierr = MatCreateSubMatrices(mat,1,&isrow,&iscoltmp,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr); 7997 *newmat = *local; 7998 ierr = PetscFree(local);CHKERRQ(ierr); 7999 if (!iscol) {ierr = ISDestroy(&iscoltmp);CHKERRQ(ierr);} 8000 PetscFunctionReturn(0); 8001 } else if (!mat->ops->createsubmatrix) { 8002 /* Create a new matrix type that implements the operation using the full matrix */ 8003 ierr = PetscLogEventBegin(MAT_CreateSubMat,mat,0,0,0);CHKERRQ(ierr); 8004 switch (cll) { 8005 case MAT_INITIAL_MATRIX: 8006 ierr = MatCreateSubMatrixVirtual(mat,isrow,iscoltmp,newmat);CHKERRQ(ierr); 8007 break; 8008 case MAT_REUSE_MATRIX: 8009 ierr = MatSubMatrixVirtualUpdate(*newmat,mat,isrow,iscoltmp);CHKERRQ(ierr); 8010 break; 8011 default: SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Invalid MatReuse, must be either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX"); 8012 } 8013 ierr = PetscLogEventEnd(MAT_CreateSubMat,mat,0,0,0);CHKERRQ(ierr); 8014 if (!iscol) {ierr = ISDestroy(&iscoltmp);CHKERRQ(ierr);} 8015 PetscFunctionReturn(0); 8016 } 8017 8018 if (!mat->ops->createsubmatrix) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 8019 ierr = PetscLogEventBegin(MAT_CreateSubMat,mat,0,0,0);CHKERRQ(ierr); 8020 ierr = (*mat->ops->createsubmatrix)(mat,isrow,iscoltmp,cll,newmat);CHKERRQ(ierr); 8021 ierr = PetscLogEventEnd(MAT_CreateSubMat,mat,0,0,0);CHKERRQ(ierr); 8022 8023 /* Propagate symmetry information for diagonal blocks */ 8024 if (isrow == iscoltmp) { 8025 if (mat->symmetric_set && mat->symmetric) { 8026 ierr = MatSetOption(*newmat,MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr); 8027 } 8028 if (mat->structurally_symmetric_set && mat->structurally_symmetric) { 8029 ierr = MatSetOption(*newmat,MAT_STRUCTURALLY_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr); 8030 } 8031 if (mat->hermitian_set && mat->hermitian) { 8032 ierr = MatSetOption(*newmat,MAT_HERMITIAN,PETSC_TRUE);CHKERRQ(ierr); 8033 } 8034 if (mat->spd_set && mat->spd) { 8035 ierr = MatSetOption(*newmat,MAT_SPD,PETSC_TRUE);CHKERRQ(ierr); 8036 } 8037 } 8038 8039 if (!iscol) {ierr = ISDestroy(&iscoltmp);CHKERRQ(ierr);} 8040 if (*newmat && cll == MAT_INITIAL_MATRIX) {ierr = PetscObjectStateIncrease((PetscObject)*newmat);CHKERRQ(ierr);} 8041 PetscFunctionReturn(0); 8042 } 8043 8044 /*@ 8045 MatStashSetInitialSize - sets the sizes of the matrix stash, that is 8046 used during the assembly process to store values that belong to 8047 other processors. 8048 8049 Not Collective 8050 8051 Input Parameters: 8052 + mat - the matrix 8053 . size - the initial size of the stash. 8054 - bsize - the initial size of the block-stash(if used). 8055 8056 Options Database Keys: 8057 + -matstash_initial_size <size> or <size0,size1,...sizep-1> 8058 - -matstash_block_initial_size <bsize> or <bsize0,bsize1,...bsizep-1> 8059 8060 Level: intermediate 8061 8062 Notes: 8063 The block-stash is used for values set with MatSetValuesBlocked() while 8064 the stash is used for values set with MatSetValues() 8065 8066 Run with the option -info and look for output of the form 8067 MatAssemblyBegin_MPIXXX:Stash has MM entries, uses nn mallocs. 8068 to determine the appropriate value, MM, to use for size and 8069 MatAssemblyBegin_MPIXXX:Block-Stash has BMM entries, uses nn mallocs. 8070 to determine the value, BMM to use for bsize 8071 8072 Concepts: stash^setting matrix size 8073 Concepts: matrices^stash 8074 8075 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), Mat, MatStashGetInfo() 8076 8077 @*/ 8078 PetscErrorCode MatStashSetInitialSize(Mat mat,PetscInt size, PetscInt bsize) 8079 { 8080 PetscErrorCode ierr; 8081 8082 PetscFunctionBegin; 8083 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8084 PetscValidType(mat,1); 8085 ierr = MatStashSetInitialSize_Private(&mat->stash,size);CHKERRQ(ierr); 8086 ierr = MatStashSetInitialSize_Private(&mat->bstash,bsize);CHKERRQ(ierr); 8087 PetscFunctionReturn(0); 8088 } 8089 8090 /*@ 8091 MatInterpolateAdd - w = y + A*x or A'*x depending on the shape of 8092 the matrix 8093 8094 Neighbor-wise Collective on Mat 8095 8096 Input Parameters: 8097 + mat - the matrix 8098 . x,y - the vectors 8099 - w - where the result is stored 8100 8101 Level: intermediate 8102 8103 Notes: 8104 w may be the same vector as y. 8105 8106 This allows one to use either the restriction or interpolation (its transpose) 8107 matrix to do the interpolation 8108 8109 Concepts: interpolation 8110 8111 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict() 8112 8113 @*/ 8114 PetscErrorCode MatInterpolateAdd(Mat A,Vec x,Vec y,Vec w) 8115 { 8116 PetscErrorCode ierr; 8117 PetscInt M,N,Ny; 8118 8119 PetscFunctionBegin; 8120 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 8121 PetscValidHeaderSpecific(x,VEC_CLASSID,2); 8122 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 8123 PetscValidHeaderSpecific(w,VEC_CLASSID,4); 8124 PetscValidType(A,1); 8125 MatCheckPreallocated(A,1); 8126 ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr); 8127 ierr = VecGetSize(y,&Ny);CHKERRQ(ierr); 8128 if (M == Ny) { 8129 ierr = MatMultAdd(A,x,y,w);CHKERRQ(ierr); 8130 } else { 8131 ierr = MatMultTransposeAdd(A,x,y,w);CHKERRQ(ierr); 8132 } 8133 PetscFunctionReturn(0); 8134 } 8135 8136 /*@ 8137 MatInterpolate - y = A*x or A'*x depending on the shape of 8138 the matrix 8139 8140 Neighbor-wise Collective on Mat 8141 8142 Input Parameters: 8143 + mat - the matrix 8144 - x,y - the vectors 8145 8146 Level: intermediate 8147 8148 Notes: 8149 This allows one to use either the restriction or interpolation (its transpose) 8150 matrix to do the interpolation 8151 8152 Concepts: matrices^interpolation 8153 8154 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict() 8155 8156 @*/ 8157 PetscErrorCode MatInterpolate(Mat A,Vec x,Vec y) 8158 { 8159 PetscErrorCode ierr; 8160 PetscInt M,N,Ny; 8161 8162 PetscFunctionBegin; 8163 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 8164 PetscValidHeaderSpecific(x,VEC_CLASSID,2); 8165 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 8166 PetscValidType(A,1); 8167 MatCheckPreallocated(A,1); 8168 ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr); 8169 ierr = VecGetSize(y,&Ny);CHKERRQ(ierr); 8170 if (M == Ny) { 8171 ierr = MatMult(A,x,y);CHKERRQ(ierr); 8172 } else { 8173 ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr); 8174 } 8175 PetscFunctionReturn(0); 8176 } 8177 8178 /*@ 8179 MatRestrict - y = A*x or A'*x 8180 8181 Neighbor-wise Collective on Mat 8182 8183 Input Parameters: 8184 + mat - the matrix 8185 - x,y - the vectors 8186 8187 Level: intermediate 8188 8189 Notes: 8190 This allows one to use either the restriction or interpolation (its transpose) 8191 matrix to do the restriction 8192 8193 Concepts: matrices^restriction 8194 8195 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatInterpolate() 8196 8197 @*/ 8198 PetscErrorCode MatRestrict(Mat A,Vec x,Vec y) 8199 { 8200 PetscErrorCode ierr; 8201 PetscInt M,N,Ny; 8202 8203 PetscFunctionBegin; 8204 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 8205 PetscValidHeaderSpecific(x,VEC_CLASSID,2); 8206 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 8207 PetscValidType(A,1); 8208 MatCheckPreallocated(A,1); 8209 8210 ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr); 8211 ierr = VecGetSize(y,&Ny);CHKERRQ(ierr); 8212 if (M == Ny) { 8213 ierr = MatMult(A,x,y);CHKERRQ(ierr); 8214 } else { 8215 ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr); 8216 } 8217 PetscFunctionReturn(0); 8218 } 8219 8220 /*@ 8221 MatGetNullSpace - retrieves the null space of a matrix. 8222 8223 Logically Collective on Mat and MatNullSpace 8224 8225 Input Parameters: 8226 + mat - the matrix 8227 - nullsp - the null space object 8228 8229 Level: developer 8230 8231 Concepts: null space^attaching to matrix 8232 8233 .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace(), MatSetNullSpace() 8234 @*/ 8235 PetscErrorCode MatGetNullSpace(Mat mat, MatNullSpace *nullsp) 8236 { 8237 PetscFunctionBegin; 8238 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8239 PetscValidPointer(nullsp,2); 8240 *nullsp = (mat->symmetric_set && mat->symmetric && !mat->nullsp) ? mat->transnullsp : mat->nullsp; 8241 PetscFunctionReturn(0); 8242 } 8243 8244 /*@ 8245 MatSetNullSpace - attaches a null space to a matrix. 8246 8247 Logically Collective on Mat and MatNullSpace 8248 8249 Input Parameters: 8250 + mat - the matrix 8251 - nullsp - the null space object 8252 8253 Level: advanced 8254 8255 Notes: 8256 This null space is used by the linear solvers. Overwrites any previous null space that may have been attached 8257 8258 For inconsistent singular systems (linear systems where the right hand side is not in the range of the operator) you also likely should 8259 call MatSetTransposeNullSpace(). This allows the linear system to be solved in a least squares sense. 8260 8261 You can remove the null space by calling this routine with an nullsp of NULL 8262 8263 8264 The fundamental theorem of linear algebra (Gilbert Strang, Introduction to Applied Mathematics, page 72) states that 8265 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). 8266 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 8267 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 8268 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). 8269 8270 Krylov solvers can produce the minimal norm solution to the least squares problem by utilizing MatNullSpaceRemove(). 8271 8272 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 8273 routine also automatically calls MatSetTransposeNullSpace(). 8274 8275 Concepts: null space^attaching to matrix 8276 8277 .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace(), MatGetNullSpace(), MatSetTransposeNullSpace(), MatGetTransposeNullSpace(), MatNullSpaceRemove() 8278 @*/ 8279 PetscErrorCode MatSetNullSpace(Mat mat,MatNullSpace nullsp) 8280 { 8281 PetscErrorCode ierr; 8282 8283 PetscFunctionBegin; 8284 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8285 if (nullsp) PetscValidHeaderSpecific(nullsp,MAT_NULLSPACE_CLASSID,2); 8286 if (nullsp) {ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr);} 8287 ierr = MatNullSpaceDestroy(&mat->nullsp);CHKERRQ(ierr); 8288 mat->nullsp = nullsp; 8289 if (mat->symmetric_set && mat->symmetric) { 8290 ierr = MatSetTransposeNullSpace(mat,nullsp);CHKERRQ(ierr); 8291 } 8292 PetscFunctionReturn(0); 8293 } 8294 8295 /*@ 8296 MatGetTransposeNullSpace - retrieves the null space of the transpose of a matrix. 8297 8298 Logically Collective on Mat and MatNullSpace 8299 8300 Input Parameters: 8301 + mat - the matrix 8302 - nullsp - the null space object 8303 8304 Level: developer 8305 8306 Concepts: null space^attaching to matrix 8307 8308 .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace(), MatSetTransposeNullSpace(), MatSetNullSpace(), MatGetNullSpace() 8309 @*/ 8310 PetscErrorCode MatGetTransposeNullSpace(Mat mat, MatNullSpace *nullsp) 8311 { 8312 PetscFunctionBegin; 8313 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8314 PetscValidType(mat,1); 8315 PetscValidPointer(nullsp,2); 8316 *nullsp = (mat->symmetric_set && mat->symmetric && !mat->transnullsp) ? mat->nullsp : mat->transnullsp; 8317 PetscFunctionReturn(0); 8318 } 8319 8320 /*@ 8321 MatSetTransposeNullSpace - attaches a null space to a matrix. 8322 8323 Logically Collective on Mat and MatNullSpace 8324 8325 Input Parameters: 8326 + mat - the matrix 8327 - nullsp - the null space object 8328 8329 Level: advanced 8330 8331 Notes: 8332 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. 8333 You must also call MatSetNullSpace() 8334 8335 8336 The fundamental theorem of linear algebra (Gilbert Strang, Introduction to Applied Mathematics, page 72) states that 8337 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). 8338 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 8339 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 8340 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). 8341 8342 Krylov solvers can produce the minimal norm solution to the least squares problem by utilizing MatNullSpaceRemove(). 8343 8344 Concepts: null space^attaching to matrix 8345 8346 .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace(), MatGetNullSpace(), MatSetNullSpace(), MatGetTransposeNullSpace(), MatNullSpaceRemove() 8347 @*/ 8348 PetscErrorCode MatSetTransposeNullSpace(Mat mat,MatNullSpace nullsp) 8349 { 8350 PetscErrorCode ierr; 8351 8352 PetscFunctionBegin; 8353 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8354 PetscValidType(mat,1); 8355 PetscValidHeaderSpecific(nullsp,MAT_NULLSPACE_CLASSID,2); 8356 MatCheckPreallocated(mat,1); 8357 ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr); 8358 ierr = MatNullSpaceDestroy(&mat->transnullsp);CHKERRQ(ierr); 8359 mat->transnullsp = nullsp; 8360 PetscFunctionReturn(0); 8361 } 8362 8363 /*@ 8364 MatSetNearNullSpace - attaches a null space to a matrix, which is often the null space (rigid body modes) of the operator without boundary conditions 8365 This null space will be used to provide near null space vectors to a multigrid preconditioner built from this matrix. 8366 8367 Logically Collective on Mat and MatNullSpace 8368 8369 Input Parameters: 8370 + mat - the matrix 8371 - nullsp - the null space object 8372 8373 Level: advanced 8374 8375 Notes: 8376 Overwrites any previous near null space that may have been attached 8377 8378 You can remove the null space by calling this routine with an nullsp of NULL 8379 8380 Concepts: null space^attaching to matrix 8381 8382 .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNullSpace(), MatNullSpaceCreateRigidBody(), MatGetNearNullSpace() 8383 @*/ 8384 PetscErrorCode MatSetNearNullSpace(Mat mat,MatNullSpace nullsp) 8385 { 8386 PetscErrorCode ierr; 8387 8388 PetscFunctionBegin; 8389 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8390 PetscValidType(mat,1); 8391 if (nullsp) PetscValidHeaderSpecific(nullsp,MAT_NULLSPACE_CLASSID,2); 8392 MatCheckPreallocated(mat,1); 8393 if (nullsp) {ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr);} 8394 ierr = MatNullSpaceDestroy(&mat->nearnullsp);CHKERRQ(ierr); 8395 mat->nearnullsp = nullsp; 8396 PetscFunctionReturn(0); 8397 } 8398 8399 /*@ 8400 MatGetNearNullSpace -Get null space attached with MatSetNearNullSpace() 8401 8402 Not Collective 8403 8404 Input Parameters: 8405 . mat - the matrix 8406 8407 Output Parameters: 8408 . nullsp - the null space object, NULL if not set 8409 8410 Level: developer 8411 8412 Concepts: null space^attaching to matrix 8413 8414 .seealso: MatSetNearNullSpace(), MatGetNullSpace(), MatNullSpaceCreate() 8415 @*/ 8416 PetscErrorCode MatGetNearNullSpace(Mat mat,MatNullSpace *nullsp) 8417 { 8418 PetscFunctionBegin; 8419 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8420 PetscValidType(mat,1); 8421 PetscValidPointer(nullsp,2); 8422 MatCheckPreallocated(mat,1); 8423 *nullsp = mat->nearnullsp; 8424 PetscFunctionReturn(0); 8425 } 8426 8427 /*@C 8428 MatICCFactor - Performs in-place incomplete Cholesky factorization of matrix. 8429 8430 Collective on Mat 8431 8432 Input Parameters: 8433 + mat - the matrix 8434 . row - row/column permutation 8435 . fill - expected fill factor >= 1.0 8436 - level - level of fill, for ICC(k) 8437 8438 Notes: 8439 Probably really in-place only when level of fill is zero, otherwise allocates 8440 new space to store factored matrix and deletes previous memory. 8441 8442 Most users should employ the simplified KSP interface for linear solvers 8443 instead of working directly with matrix algebra routines such as this. 8444 See, e.g., KSPCreate(). 8445 8446 Level: developer 8447 8448 Concepts: matrices^incomplete Cholesky factorization 8449 Concepts: Cholesky factorization 8450 8451 .seealso: MatICCFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor() 8452 8453 Developer Note: fortran interface is not autogenerated as the f90 8454 interface defintion cannot be generated correctly [due to MatFactorInfo] 8455 8456 @*/ 8457 PetscErrorCode MatICCFactor(Mat mat,IS row,const MatFactorInfo *info) 8458 { 8459 PetscErrorCode ierr; 8460 8461 PetscFunctionBegin; 8462 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8463 PetscValidType(mat,1); 8464 if (row) PetscValidHeaderSpecific(row,IS_CLASSID,2); 8465 PetscValidPointer(info,3); 8466 if (mat->rmap->N != mat->cmap->N) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONG,"matrix must be square"); 8467 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 8468 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 8469 if (!mat->ops->iccfactor) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 8470 MatCheckPreallocated(mat,1); 8471 ierr = (*mat->ops->iccfactor)(mat,row,info);CHKERRQ(ierr); 8472 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 8473 PetscFunctionReturn(0); 8474 } 8475 8476 /*@ 8477 MatDiagonalScaleLocal - Scales columns of a matrix given the scaling values including the 8478 ghosted ones. 8479 8480 Not Collective 8481 8482 Input Parameters: 8483 + mat - the matrix 8484 - diag = the diagonal values, including ghost ones 8485 8486 Level: developer 8487 8488 Notes: 8489 Works only for MPIAIJ and MPIBAIJ matrices 8490 8491 .seealso: MatDiagonalScale() 8492 @*/ 8493 PetscErrorCode MatDiagonalScaleLocal(Mat mat,Vec diag) 8494 { 8495 PetscErrorCode ierr; 8496 PetscMPIInt size; 8497 8498 PetscFunctionBegin; 8499 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8500 PetscValidHeaderSpecific(diag,VEC_CLASSID,2); 8501 PetscValidType(mat,1); 8502 8503 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled"); 8504 ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 8505 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);CHKERRQ(ierr); 8506 if (size == 1) { 8507 PetscInt n,m; 8508 ierr = VecGetSize(diag,&n);CHKERRQ(ierr); 8509 ierr = MatGetSize(mat,0,&m);CHKERRQ(ierr); 8510 if (m == n) { 8511 ierr = MatDiagonalScale(mat,0,diag);CHKERRQ(ierr); 8512 } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only supported for sequential matrices when no ghost points/periodic conditions"); 8513 } else { 8514 ierr = PetscUseMethod(mat,"MatDiagonalScaleLocal_C",(Mat,Vec),(mat,diag));CHKERRQ(ierr); 8515 } 8516 ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 8517 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 8518 PetscFunctionReturn(0); 8519 } 8520 8521 /*@ 8522 MatGetInertia - Gets the inertia from a factored matrix 8523 8524 Collective on Mat 8525 8526 Input Parameter: 8527 . mat - the matrix 8528 8529 Output Parameters: 8530 + nneg - number of negative eigenvalues 8531 . nzero - number of zero eigenvalues 8532 - npos - number of positive eigenvalues 8533 8534 Level: advanced 8535 8536 Notes: 8537 Matrix must have been factored by MatCholeskyFactor() 8538 8539 8540 @*/ 8541 PetscErrorCode MatGetInertia(Mat mat,PetscInt *nneg,PetscInt *nzero,PetscInt *npos) 8542 { 8543 PetscErrorCode ierr; 8544 8545 PetscFunctionBegin; 8546 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8547 PetscValidType(mat,1); 8548 if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 8549 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Numeric factor mat is not assembled"); 8550 if (!mat->ops->getinertia) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 8551 ierr = (*mat->ops->getinertia)(mat,nneg,nzero,npos);CHKERRQ(ierr); 8552 PetscFunctionReturn(0); 8553 } 8554 8555 /* ----------------------------------------------------------------*/ 8556 /*@C 8557 MatSolves - Solves A x = b, given a factored matrix, for a collection of vectors 8558 8559 Neighbor-wise Collective on Mat and Vecs 8560 8561 Input Parameters: 8562 + mat - the factored matrix 8563 - b - the right-hand-side vectors 8564 8565 Output Parameter: 8566 . x - the result vectors 8567 8568 Notes: 8569 The vectors b and x cannot be the same. I.e., one cannot 8570 call MatSolves(A,x,x). 8571 8572 Notes: 8573 Most users should employ the simplified KSP interface for linear solvers 8574 instead of working directly with matrix algebra routines such as this. 8575 See, e.g., KSPCreate(). 8576 8577 Level: developer 8578 8579 Concepts: matrices^triangular solves 8580 8581 .seealso: MatSolveAdd(), MatSolveTranspose(), MatSolveTransposeAdd(), MatSolve() 8582 @*/ 8583 PetscErrorCode MatSolves(Mat mat,Vecs b,Vecs x) 8584 { 8585 PetscErrorCode ierr; 8586 8587 PetscFunctionBegin; 8588 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8589 PetscValidType(mat,1); 8590 if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 8591 if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 8592 if (!mat->rmap->N && !mat->cmap->N) PetscFunctionReturn(0); 8593 8594 if (!mat->ops->solves) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 8595 MatCheckPreallocated(mat,1); 8596 ierr = PetscLogEventBegin(MAT_Solves,mat,0,0,0);CHKERRQ(ierr); 8597 ierr = (*mat->ops->solves)(mat,b,x);CHKERRQ(ierr); 8598 ierr = PetscLogEventEnd(MAT_Solves,mat,0,0,0);CHKERRQ(ierr); 8599 PetscFunctionReturn(0); 8600 } 8601 8602 /*@ 8603 MatIsSymmetric - Test whether a matrix is symmetric 8604 8605 Collective on Mat 8606 8607 Input Parameter: 8608 + A - the matrix to test 8609 - tol - difference between value and its transpose less than this amount counts as equal (use 0.0 for exact transpose) 8610 8611 Output Parameters: 8612 . flg - the result 8613 8614 Notes: 8615 For real numbers MatIsSymmetric() and MatIsHermitian() return identical results 8616 8617 Level: intermediate 8618 8619 Concepts: matrix^symmetry 8620 8621 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetricKnown() 8622 @*/ 8623 PetscErrorCode MatIsSymmetric(Mat A,PetscReal tol,PetscBool *flg) 8624 { 8625 PetscErrorCode ierr; 8626 8627 PetscFunctionBegin; 8628 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 8629 PetscValidPointer(flg,2); 8630 8631 if (!A->symmetric_set) { 8632 if (!A->ops->issymmetric) { 8633 MatType mattype; 8634 ierr = MatGetType(A,&mattype);CHKERRQ(ierr); 8635 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for symmetric",mattype); 8636 } 8637 ierr = (*A->ops->issymmetric)(A,tol,flg);CHKERRQ(ierr); 8638 if (!tol) { 8639 A->symmetric_set = PETSC_TRUE; 8640 A->symmetric = *flg; 8641 if (A->symmetric) { 8642 A->structurally_symmetric_set = PETSC_TRUE; 8643 A->structurally_symmetric = PETSC_TRUE; 8644 } 8645 } 8646 } else if (A->symmetric) { 8647 *flg = PETSC_TRUE; 8648 } else if (!tol) { 8649 *flg = PETSC_FALSE; 8650 } else { 8651 if (!A->ops->issymmetric) { 8652 MatType mattype; 8653 ierr = MatGetType(A,&mattype);CHKERRQ(ierr); 8654 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for symmetric",mattype); 8655 } 8656 ierr = (*A->ops->issymmetric)(A,tol,flg);CHKERRQ(ierr); 8657 } 8658 PetscFunctionReturn(0); 8659 } 8660 8661 /*@ 8662 MatIsHermitian - Test whether a matrix is Hermitian 8663 8664 Collective on Mat 8665 8666 Input Parameter: 8667 + A - the matrix to test 8668 - tol - difference between value and its transpose less than this amount counts as equal (use 0.0 for exact Hermitian) 8669 8670 Output Parameters: 8671 . flg - the result 8672 8673 Level: intermediate 8674 8675 Concepts: matrix^symmetry 8676 8677 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), 8678 MatIsSymmetricKnown(), MatIsSymmetric() 8679 @*/ 8680 PetscErrorCode MatIsHermitian(Mat A,PetscReal tol,PetscBool *flg) 8681 { 8682 PetscErrorCode ierr; 8683 8684 PetscFunctionBegin; 8685 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 8686 PetscValidPointer(flg,2); 8687 8688 if (!A->hermitian_set) { 8689 if (!A->ops->ishermitian) { 8690 MatType mattype; 8691 ierr = MatGetType(A,&mattype);CHKERRQ(ierr); 8692 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for hermitian",mattype); 8693 } 8694 ierr = (*A->ops->ishermitian)(A,tol,flg);CHKERRQ(ierr); 8695 if (!tol) { 8696 A->hermitian_set = PETSC_TRUE; 8697 A->hermitian = *flg; 8698 if (A->hermitian) { 8699 A->structurally_symmetric_set = PETSC_TRUE; 8700 A->structurally_symmetric = PETSC_TRUE; 8701 } 8702 } 8703 } else if (A->hermitian) { 8704 *flg = PETSC_TRUE; 8705 } else if (!tol) { 8706 *flg = PETSC_FALSE; 8707 } else { 8708 if (!A->ops->ishermitian) { 8709 MatType mattype; 8710 ierr = MatGetType(A,&mattype);CHKERRQ(ierr); 8711 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for hermitian",mattype); 8712 } 8713 ierr = (*A->ops->ishermitian)(A,tol,flg);CHKERRQ(ierr); 8714 } 8715 PetscFunctionReturn(0); 8716 } 8717 8718 /*@ 8719 MatIsSymmetricKnown - Checks the flag on the matrix to see if it is symmetric. 8720 8721 Not Collective 8722 8723 Input Parameter: 8724 . A - the matrix to check 8725 8726 Output Parameters: 8727 + set - if the symmetric flag is set (this tells you if the next flag is valid) 8728 - flg - the result 8729 8730 Level: advanced 8731 8732 Concepts: matrix^symmetry 8733 8734 Note: Does not check the matrix values directly, so this may return unknown (set = PETSC_FALSE). Use MatIsSymmetric() 8735 if you want it explicitly checked 8736 8737 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetric() 8738 @*/ 8739 PetscErrorCode MatIsSymmetricKnown(Mat A,PetscBool *set,PetscBool *flg) 8740 { 8741 PetscFunctionBegin; 8742 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 8743 PetscValidPointer(set,2); 8744 PetscValidPointer(flg,3); 8745 if (A->symmetric_set) { 8746 *set = PETSC_TRUE; 8747 *flg = A->symmetric; 8748 } else { 8749 *set = PETSC_FALSE; 8750 } 8751 PetscFunctionReturn(0); 8752 } 8753 8754 /*@ 8755 MatIsHermitianKnown - Checks the flag on the matrix to see if it is hermitian. 8756 8757 Not Collective 8758 8759 Input Parameter: 8760 . A - the matrix to check 8761 8762 Output Parameters: 8763 + set - if the hermitian flag is set (this tells you if the next flag is valid) 8764 - flg - the result 8765 8766 Level: advanced 8767 8768 Concepts: matrix^symmetry 8769 8770 Note: Does not check the matrix values directly, so this may return unknown (set = PETSC_FALSE). Use MatIsHermitian() 8771 if you want it explicitly checked 8772 8773 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetric() 8774 @*/ 8775 PetscErrorCode MatIsHermitianKnown(Mat A,PetscBool *set,PetscBool *flg) 8776 { 8777 PetscFunctionBegin; 8778 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 8779 PetscValidPointer(set,2); 8780 PetscValidPointer(flg,3); 8781 if (A->hermitian_set) { 8782 *set = PETSC_TRUE; 8783 *flg = A->hermitian; 8784 } else { 8785 *set = PETSC_FALSE; 8786 } 8787 PetscFunctionReturn(0); 8788 } 8789 8790 /*@ 8791 MatIsStructurallySymmetric - Test whether a matrix is structurally symmetric 8792 8793 Collective on Mat 8794 8795 Input Parameter: 8796 . A - the matrix to test 8797 8798 Output Parameters: 8799 . flg - the result 8800 8801 Level: intermediate 8802 8803 Concepts: matrix^symmetry 8804 8805 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsSymmetric(), MatSetOption() 8806 @*/ 8807 PetscErrorCode MatIsStructurallySymmetric(Mat A,PetscBool *flg) 8808 { 8809 PetscErrorCode ierr; 8810 8811 PetscFunctionBegin; 8812 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 8813 PetscValidPointer(flg,2); 8814 if (!A->structurally_symmetric_set) { 8815 if (!A->ops->isstructurallysymmetric) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Matrix does not support checking for structural symmetric"); 8816 ierr = (*A->ops->isstructurallysymmetric)(A,&A->structurally_symmetric);CHKERRQ(ierr); 8817 8818 A->structurally_symmetric_set = PETSC_TRUE; 8819 } 8820 *flg = A->structurally_symmetric; 8821 PetscFunctionReturn(0); 8822 } 8823 8824 /*@ 8825 MatStashGetInfo - Gets how many values are currently in the matrix stash, i.e. need 8826 to be communicated to other processors during the MatAssemblyBegin/End() process 8827 8828 Not collective 8829 8830 Input Parameter: 8831 . vec - the vector 8832 8833 Output Parameters: 8834 + nstash - the size of the stash 8835 . reallocs - the number of additional mallocs incurred. 8836 . bnstash - the size of the block stash 8837 - breallocs - the number of additional mallocs incurred.in the block stash 8838 8839 Level: advanced 8840 8841 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), Mat, MatStashSetInitialSize() 8842 8843 @*/ 8844 PetscErrorCode MatStashGetInfo(Mat mat,PetscInt *nstash,PetscInt *reallocs,PetscInt *bnstash,PetscInt *breallocs) 8845 { 8846 PetscErrorCode ierr; 8847 8848 PetscFunctionBegin; 8849 ierr = MatStashGetInfo_Private(&mat->stash,nstash,reallocs);CHKERRQ(ierr); 8850 ierr = MatStashGetInfo_Private(&mat->bstash,bnstash,breallocs);CHKERRQ(ierr); 8851 PetscFunctionReturn(0); 8852 } 8853 8854 /*@C 8855 MatCreateVecs - Get vector(s) compatible with the matrix, i.e. with the same 8856 parallel layout 8857 8858 Collective on Mat 8859 8860 Input Parameter: 8861 . mat - the matrix 8862 8863 Output Parameter: 8864 + right - (optional) vector that the matrix can be multiplied against 8865 - left - (optional) vector that the matrix vector product can be stored in 8866 8867 Notes: 8868 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(). 8869 8870 Notes: 8871 These are new vectors which are not owned by the Mat, they should be destroyed in VecDestroy() when no longer needed 8872 8873 Level: advanced 8874 8875 .seealso: MatCreate(), VecDestroy() 8876 @*/ 8877 PetscErrorCode MatCreateVecs(Mat mat,Vec *right,Vec *left) 8878 { 8879 PetscErrorCode ierr; 8880 8881 PetscFunctionBegin; 8882 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8883 PetscValidType(mat,1); 8884 if (mat->ops->getvecs) { 8885 ierr = (*mat->ops->getvecs)(mat,right,left);CHKERRQ(ierr); 8886 } else { 8887 PetscInt rbs,cbs; 8888 ierr = MatGetBlockSizes(mat,&rbs,&cbs);CHKERRQ(ierr); 8889 if (right) { 8890 if (mat->cmap->n < 0) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"PetscLayout for columns not yet setup"); 8891 ierr = VecCreate(PetscObjectComm((PetscObject)mat),right);CHKERRQ(ierr); 8892 ierr = VecSetSizes(*right,mat->cmap->n,PETSC_DETERMINE);CHKERRQ(ierr); 8893 ierr = VecSetBlockSize(*right,cbs);CHKERRQ(ierr); 8894 ierr = VecSetType(*right,VECSTANDARD);CHKERRQ(ierr); 8895 ierr = PetscLayoutReference(mat->cmap,&(*right)->map);CHKERRQ(ierr); 8896 } 8897 if (left) { 8898 if (mat->rmap->n < 0) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"PetscLayout for rows not yet setup"); 8899 ierr = VecCreate(PetscObjectComm((PetscObject)mat),left);CHKERRQ(ierr); 8900 ierr = VecSetSizes(*left,mat->rmap->n,PETSC_DETERMINE);CHKERRQ(ierr); 8901 ierr = VecSetBlockSize(*left,rbs);CHKERRQ(ierr); 8902 ierr = VecSetType(*left,VECSTANDARD);CHKERRQ(ierr); 8903 ierr = PetscLayoutReference(mat->rmap,&(*left)->map);CHKERRQ(ierr); 8904 } 8905 } 8906 PetscFunctionReturn(0); 8907 } 8908 8909 /*@C 8910 MatFactorInfoInitialize - Initializes a MatFactorInfo data structure 8911 with default values. 8912 8913 Not Collective 8914 8915 Input Parameters: 8916 . info - the MatFactorInfo data structure 8917 8918 8919 Notes: 8920 The solvers are generally used through the KSP and PC objects, for example 8921 PCLU, PCILU, PCCHOLESKY, PCICC 8922 8923 Level: developer 8924 8925 .seealso: MatFactorInfo 8926 8927 Developer Note: fortran interface is not autogenerated as the f90 8928 interface defintion cannot be generated correctly [due to MatFactorInfo] 8929 8930 @*/ 8931 8932 PetscErrorCode MatFactorInfoInitialize(MatFactorInfo *info) 8933 { 8934 PetscErrorCode ierr; 8935 8936 PetscFunctionBegin; 8937 ierr = PetscMemzero(info,sizeof(MatFactorInfo));CHKERRQ(ierr); 8938 PetscFunctionReturn(0); 8939 } 8940 8941 /*@ 8942 MatFactorSetSchurIS - Set indices corresponding to the Schur complement you wish to have computed 8943 8944 Collective on Mat 8945 8946 Input Parameters: 8947 + mat - the factored matrix 8948 - is - the index set defining the Schur indices (0-based) 8949 8950 Notes: 8951 Call MatFactorSolveSchurComplement() or MatFactorSolveSchurComplementTranspose() after this call to solve a Schur complement system. 8952 8953 You can call MatFactorGetSchurComplement() or MatFactorCreateSchurComplement() after this call. 8954 8955 Level: developer 8956 8957 Concepts: 8958 8959 .seealso: MatGetFactor(), MatFactorGetSchurComplement(), MatFactorRestoreSchurComplement(), MatFactorCreateSchurComplement(), MatFactorSolveSchurComplement(), 8960 MatFactorSolveSchurComplementTranspose(), MatFactorSolveSchurComplement() 8961 8962 @*/ 8963 PetscErrorCode MatFactorSetSchurIS(Mat mat,IS is) 8964 { 8965 PetscErrorCode ierr,(*f)(Mat,IS); 8966 8967 PetscFunctionBegin; 8968 PetscValidType(mat,1); 8969 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8970 PetscValidType(is,2); 8971 PetscValidHeaderSpecific(is,IS_CLASSID,2); 8972 PetscCheckSameComm(mat,1,is,2); 8973 if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Only for factored matrix"); 8974 ierr = PetscObjectQueryFunction((PetscObject)mat,"MatFactorSetSchurIS_C",&f);CHKERRQ(ierr); 8975 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"); 8976 if (mat->schur) { 8977 ierr = MatDestroy(&mat->schur);CHKERRQ(ierr); 8978 } 8979 ierr = (*f)(mat,is);CHKERRQ(ierr); 8980 if (!mat->schur) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_PLIB,"Schur complement has not been created"); 8981 ierr = MatFactorSetUpInPlaceSchur_Private(mat);CHKERRQ(ierr); 8982 PetscFunctionReturn(0); 8983 } 8984 8985 /*@ 8986 MatFactorCreateSchurComplement - Create a Schur complement matrix object using Schur data computed during the factorization step 8987 8988 Logically Collective on Mat 8989 8990 Input Parameters: 8991 + F - the factored matrix obtained by calling MatGetFactor() from PETSc-MUMPS interface 8992 . S - location where to return the Schur complement, can be NULL 8993 - status - the status of the Schur complement matrix, can be NULL 8994 8995 Notes: 8996 You must call MatFactorSetSchurIS() before calling this routine. 8997 8998 The routine provides a copy of the Schur matrix stored within the solver data structures. 8999 The caller must destroy the object when it is no longer needed. 9000 If MatFactorInvertSchurComplement() has been called, the routine gets back the inverse. 9001 9002 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) 9003 9004 Developer Notes: 9005 The reason this routine exists is because the representation of the Schur complement within the factor matrix may be different than a standard PETSc 9006 matrix representation and we normally do not want to use the time or memory to make a copy as a regular PETSc matrix. 9007 9008 See MatCreateSchurComplement() or MatGetSchurComplement() for ways to create virtual or approximate Schur complements. 9009 9010 Level: advanced 9011 9012 References: 9013 9014 .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorGetSchurComplement(), MatFactorSchurStatus 9015 @*/ 9016 PetscErrorCode MatFactorCreateSchurComplement(Mat F,Mat* S,MatFactorSchurStatus* status) 9017 { 9018 PetscErrorCode ierr; 9019 9020 PetscFunctionBegin; 9021 PetscValidHeaderSpecific(F,MAT_CLASSID,1); 9022 if (S) PetscValidPointer(S,2); 9023 if (status) PetscValidPointer(status,3); 9024 if (S) { 9025 PetscErrorCode (*f)(Mat,Mat*); 9026 9027 ierr = PetscObjectQueryFunction((PetscObject)F,"MatFactorCreateSchurComplement_C",&f);CHKERRQ(ierr); 9028 if (f) { 9029 ierr = (*f)(F,S);CHKERRQ(ierr); 9030 } else { 9031 ierr = MatDuplicate(F->schur,MAT_COPY_VALUES,S);CHKERRQ(ierr); 9032 } 9033 } 9034 if (status) *status = F->schur_status; 9035 PetscFunctionReturn(0); 9036 } 9037 9038 /*@ 9039 MatFactorGetSchurComplement - Gets access to a Schur complement matrix using the current Schur data within a factored matrix 9040 9041 Logically Collective on Mat 9042 9043 Input Parameters: 9044 + F - the factored matrix obtained by calling MatGetFactor() 9045 . *S - location where to return the Schur complement, can be NULL 9046 - status - the status of the Schur complement matrix, can be NULL 9047 9048 Notes: 9049 You must call MatFactorSetSchurIS() before calling this routine. 9050 9051 Schur complement mode is currently implemented for sequential matrices. 9052 The routine returns a the Schur Complement stored within the data strutures of the solver. 9053 If MatFactorInvertSchurComplement() has previously been called, the returned matrix is actually the inverse of the Schur complement. 9054 The returned matrix should not be destroyed; the caller should call MatFactorRestoreSchurComplement() when the object is no longer needed. 9055 9056 Use MatFactorCreateSchurComplement() to create a copy of the Schur complement matrix that is within a factored matrix 9057 9058 See MatCreateSchurComplement() or MatGetSchurComplement() for ways to create virtual or approximate Schur complements. 9059 9060 Level: advanced 9061 9062 References: 9063 9064 .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorRestoreSchurComplement(), MatFactorCreateSchurComplement(), MatFactorSchurStatus 9065 @*/ 9066 PetscErrorCode MatFactorGetSchurComplement(Mat F,Mat* S,MatFactorSchurStatus* status) 9067 { 9068 PetscFunctionBegin; 9069 PetscValidHeaderSpecific(F,MAT_CLASSID,1); 9070 if (S) PetscValidPointer(S,2); 9071 if (status) PetscValidPointer(status,3); 9072 if (S) *S = F->schur; 9073 if (status) *status = F->schur_status; 9074 PetscFunctionReturn(0); 9075 } 9076 9077 /*@ 9078 MatFactorRestoreSchurComplement - Restore the Schur complement matrix object obtained from a call to MatFactorGetSchurComplement 9079 9080 Logically Collective on Mat 9081 9082 Input Parameters: 9083 + F - the factored matrix obtained by calling MatGetFactor() 9084 . *S - location where the Schur complement is stored 9085 - status - the status of the Schur complement matrix (see MatFactorSchurStatus) 9086 9087 Notes: 9088 9089 Level: advanced 9090 9091 References: 9092 9093 .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorRestoreSchurComplement(), MatFactorCreateSchurComplement(), MatFactorSchurStatus 9094 @*/ 9095 PetscErrorCode MatFactorRestoreSchurComplement(Mat F,Mat* S,MatFactorSchurStatus status) 9096 { 9097 PetscErrorCode ierr; 9098 9099 PetscFunctionBegin; 9100 PetscValidHeaderSpecific(F,MAT_CLASSID,1); 9101 if (S) { 9102 PetscValidHeaderSpecific(*S,MAT_CLASSID,2); 9103 *S = NULL; 9104 } 9105 F->schur_status = status; 9106 ierr = MatFactorUpdateSchurStatus_Private(F);CHKERRQ(ierr); 9107 PetscFunctionReturn(0); 9108 } 9109 9110 /*@ 9111 MatFactorSolveSchurComplementTranspose - Solve the transpose of the Schur complement system computed during the factorization step 9112 9113 Logically Collective on Mat 9114 9115 Input Parameters: 9116 + F - the factored matrix obtained by calling MatGetFactor() 9117 . rhs - location where the right hand side of the Schur complement system is stored 9118 - sol - location where the solution of the Schur complement system has to be returned 9119 9120 Notes: 9121 The sizes of the vectors should match the size of the Schur complement 9122 9123 Must be called after MatFactorSetSchurIS() 9124 9125 Level: advanced 9126 9127 References: 9128 9129 .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorSolveSchurComplement() 9130 @*/ 9131 PetscErrorCode MatFactorSolveSchurComplementTranspose(Mat F, Vec rhs, Vec sol) 9132 { 9133 PetscErrorCode ierr; 9134 9135 PetscFunctionBegin; 9136 PetscValidType(F,1); 9137 PetscValidType(rhs,2); 9138 PetscValidType(sol,3); 9139 PetscValidHeaderSpecific(F,MAT_CLASSID,1); 9140 PetscValidHeaderSpecific(rhs,VEC_CLASSID,2); 9141 PetscValidHeaderSpecific(sol,VEC_CLASSID,3); 9142 PetscCheckSameComm(F,1,rhs,2); 9143 PetscCheckSameComm(F,1,sol,3); 9144 ierr = MatFactorFactorizeSchurComplement(F);CHKERRQ(ierr); 9145 switch (F->schur_status) { 9146 case MAT_FACTOR_SCHUR_FACTORED: 9147 ierr = MatSolveTranspose(F->schur,rhs,sol);CHKERRQ(ierr); 9148 break; 9149 case MAT_FACTOR_SCHUR_INVERTED: 9150 ierr = MatMultTranspose(F->schur,rhs,sol);CHKERRQ(ierr); 9151 break; 9152 default: 9153 SETERRQ1(PetscObjectComm((PetscObject)F),PETSC_ERR_SUP,"Unhandled MatFactorSchurStatus %D",F->schur_status); 9154 break; 9155 } 9156 PetscFunctionReturn(0); 9157 } 9158 9159 /*@ 9160 MatFactorSolveSchurComplement - Solve the Schur complement system computed during the factorization step 9161 9162 Logically Collective on Mat 9163 9164 Input Parameters: 9165 + F - the factored matrix obtained by calling MatGetFactor() 9166 . rhs - location where the right hand side of the Schur complement system is stored 9167 - sol - location where the solution of the Schur complement system has to be returned 9168 9169 Notes: 9170 The sizes of the vectors should match the size of the Schur complement 9171 9172 Must be called after MatFactorSetSchurIS() 9173 9174 Level: advanced 9175 9176 References: 9177 9178 .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorSolveSchurComplementTranspose() 9179 @*/ 9180 PetscErrorCode MatFactorSolveSchurComplement(Mat F, Vec rhs, Vec sol) 9181 { 9182 PetscErrorCode ierr; 9183 9184 PetscFunctionBegin; 9185 PetscValidType(F,1); 9186 PetscValidType(rhs,2); 9187 PetscValidType(sol,3); 9188 PetscValidHeaderSpecific(F,MAT_CLASSID,1); 9189 PetscValidHeaderSpecific(rhs,VEC_CLASSID,2); 9190 PetscValidHeaderSpecific(sol,VEC_CLASSID,3); 9191 PetscCheckSameComm(F,1,rhs,2); 9192 PetscCheckSameComm(F,1,sol,3); 9193 ierr = MatFactorFactorizeSchurComplement(F);CHKERRQ(ierr); 9194 switch (F->schur_status) { 9195 case MAT_FACTOR_SCHUR_FACTORED: 9196 ierr = MatSolve(F->schur,rhs,sol);CHKERRQ(ierr); 9197 break; 9198 case MAT_FACTOR_SCHUR_INVERTED: 9199 ierr = MatMult(F->schur,rhs,sol);CHKERRQ(ierr); 9200 break; 9201 default: 9202 SETERRQ1(PetscObjectComm((PetscObject)F),PETSC_ERR_SUP,"Unhandled MatFactorSchurStatus %D",F->schur_status); 9203 break; 9204 } 9205 PetscFunctionReturn(0); 9206 } 9207 9208 /*@ 9209 MatFactorInvertSchurComplement - Invert the Schur complement matrix computed during the factorization step 9210 9211 Logically Collective on Mat 9212 9213 Input Parameters: 9214 + F - the factored matrix obtained by calling MatGetFactor() 9215 9216 Notes: 9217 Must be called after MatFactorSetSchurIS(). 9218 9219 Call MatFactorGetSchurComplement() or MatFactorCreateSchurComplement() AFTER this call to actually compute the inverse and get access to it. 9220 9221 Level: advanced 9222 9223 References: 9224 9225 .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorGetSchurComplement(), MatFactorCreateSchurComplement() 9226 @*/ 9227 PetscErrorCode MatFactorInvertSchurComplement(Mat F) 9228 { 9229 PetscErrorCode ierr; 9230 9231 PetscFunctionBegin; 9232 PetscValidType(F,1); 9233 PetscValidHeaderSpecific(F,MAT_CLASSID,1); 9234 if (F->schur_status == MAT_FACTOR_SCHUR_INVERTED) PetscFunctionReturn(0); 9235 ierr = MatFactorFactorizeSchurComplement(F);CHKERRQ(ierr); 9236 ierr = MatFactorInvertSchurComplement_Private(F);CHKERRQ(ierr); 9237 F->schur_status = MAT_FACTOR_SCHUR_INVERTED; 9238 PetscFunctionReturn(0); 9239 } 9240 9241 /*@ 9242 MatFactorFactorizeSchurComplement - Factorize the Schur complement matrix computed during the factorization step 9243 9244 Logically Collective on Mat 9245 9246 Input Parameters: 9247 + F - the factored matrix obtained by calling MatGetFactor() 9248 9249 Notes: 9250 Must be called after MatFactorSetSchurIS(). 9251 9252 Level: advanced 9253 9254 References: 9255 9256 .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorInvertSchurComplement() 9257 @*/ 9258 PetscErrorCode MatFactorFactorizeSchurComplement(Mat F) 9259 { 9260 PetscErrorCode ierr; 9261 9262 PetscFunctionBegin; 9263 PetscValidType(F,1); 9264 PetscValidHeaderSpecific(F,MAT_CLASSID,1); 9265 if (F->schur_status == MAT_FACTOR_SCHUR_INVERTED || F->schur_status == MAT_FACTOR_SCHUR_FACTORED) PetscFunctionReturn(0); 9266 ierr = MatFactorFactorizeSchurComplement_Private(F);CHKERRQ(ierr); 9267 F->schur_status = MAT_FACTOR_SCHUR_FACTORED; 9268 PetscFunctionReturn(0); 9269 } 9270 9271 /*@ 9272 MatPtAP - Creates the matrix product C = P^T * A * P 9273 9274 Neighbor-wise Collective on Mat 9275 9276 Input Parameters: 9277 + A - the matrix 9278 . P - the projection matrix 9279 . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 9280 - fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(P)), use PETSC_DEFAULT if you do not have a good estimate 9281 if the result is a dense matrix this is irrelevent 9282 9283 Output Parameters: 9284 . C - the product matrix 9285 9286 Notes: 9287 C will be created and must be destroyed by the user with MatDestroy(). 9288 9289 This routine is currently only implemented for pairs of sequential dense matrices, AIJ matrices and classes 9290 which inherit from AIJ. 9291 9292 Level: intermediate 9293 9294 .seealso: MatPtAPSymbolic(), MatPtAPNumeric(), MatMatMult(), MatRARt() 9295 @*/ 9296 PetscErrorCode MatPtAP(Mat A,Mat P,MatReuse scall,PetscReal fill,Mat *C) 9297 { 9298 PetscErrorCode ierr; 9299 PetscErrorCode (*fA)(Mat,Mat,MatReuse,PetscReal,Mat*); 9300 PetscErrorCode (*fP)(Mat,Mat,MatReuse,PetscReal,Mat*); 9301 PetscErrorCode (*ptap)(Mat,Mat,MatReuse,PetscReal,Mat*)=NULL; 9302 PetscBool sametype; 9303 9304 PetscFunctionBegin; 9305 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 9306 PetscValidType(A,1); 9307 MatCheckPreallocated(A,1); 9308 if (scall == MAT_INPLACE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported"); 9309 if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9310 if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9311 PetscValidHeaderSpecific(P,MAT_CLASSID,2); 9312 PetscValidType(P,2); 9313 MatCheckPreallocated(P,2); 9314 if (!P->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9315 if (P->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9316 9317 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); 9318 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); 9319 if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0; 9320 if (fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Expected fill=%g must be >= 1.0",(double)fill); 9321 9322 if (scall == MAT_REUSE_MATRIX) { 9323 PetscValidPointer(*C,5); 9324 PetscValidHeaderSpecific(*C,MAT_CLASSID,5); 9325 9326 if (!(*C)->ops->ptapnumeric) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_ARG_WRONGSTATE,"MatPtAPNumeric implementation is missing. You cannot use MAT_REUSE_MATRIX"); 9327 ierr = PetscLogEventBegin(MAT_PtAP,A,P,0,0);CHKERRQ(ierr); 9328 ierr = PetscLogEventBegin(MAT_PtAPNumeric,A,P,0,0);CHKERRQ(ierr); 9329 ierr = (*(*C)->ops->ptapnumeric)(A,P,*C);CHKERRQ(ierr); 9330 ierr = PetscLogEventEnd(MAT_PtAPNumeric,A,P,0,0);CHKERRQ(ierr); 9331 ierr = PetscLogEventEnd(MAT_PtAP,A,P,0,0);CHKERRQ(ierr); 9332 PetscFunctionReturn(0); 9333 } 9334 9335 if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0; 9336 if (fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Expected fill=%g must be >= 1.0",(double)fill); 9337 9338 fA = A->ops->ptap; 9339 fP = P->ops->ptap; 9340 ierr = PetscStrcmp(((PetscObject)A)->type_name,((PetscObject)P)->type_name,&sametype);CHKERRQ(ierr); 9341 if (fP == fA && sametype) { 9342 if (!fA) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"MatPtAP not supported for A of type %s",((PetscObject)A)->type_name); 9343 ptap = fA; 9344 } else { 9345 /* dispatch based on the type of A and P from their PetscObject's PetscFunctionLists. */ 9346 char ptapname[256]; 9347 ierr = PetscStrncpy(ptapname,"MatPtAP_",sizeof(ptapname));CHKERRQ(ierr); 9348 ierr = PetscStrlcat(ptapname,((PetscObject)A)->type_name,sizeof(ptapname));CHKERRQ(ierr); 9349 ierr = PetscStrlcat(ptapname,"_",sizeof(ptapname));CHKERRQ(ierr); 9350 ierr = PetscStrlcat(ptapname,((PetscObject)P)->type_name,sizeof(ptapname));CHKERRQ(ierr); 9351 ierr = PetscStrlcat(ptapname,"_C",sizeof(ptapname));CHKERRQ(ierr); /* e.g., ptapname = "MatPtAP_seqdense_seqaij_C" */ 9352 ierr = PetscObjectQueryFunction((PetscObject)P,ptapname,&ptap);CHKERRQ(ierr); 9353 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); 9354 } 9355 9356 ierr = PetscLogEventBegin(MAT_PtAP,A,P,0,0);CHKERRQ(ierr); 9357 ierr = (*ptap)(A,P,scall,fill,C);CHKERRQ(ierr); 9358 ierr = PetscLogEventEnd(MAT_PtAP,A,P,0,0);CHKERRQ(ierr); 9359 PetscFunctionReturn(0); 9360 } 9361 9362 /*@ 9363 MatPtAPNumeric - Computes the matrix product C = P^T * A * P 9364 9365 Neighbor-wise Collective on Mat 9366 9367 Input Parameters: 9368 + A - the matrix 9369 - P - the projection matrix 9370 9371 Output Parameters: 9372 . C - the product matrix 9373 9374 Notes: 9375 C must have been created by calling MatPtAPSymbolic and must be destroyed by 9376 the user using MatDeatroy(). 9377 9378 This routine is currently only implemented for pairs of AIJ matrices and classes 9379 which inherit from AIJ. C will be of type MATAIJ. 9380 9381 Level: intermediate 9382 9383 .seealso: MatPtAP(), MatPtAPSymbolic(), MatMatMultNumeric() 9384 @*/ 9385 PetscErrorCode MatPtAPNumeric(Mat A,Mat P,Mat C) 9386 { 9387 PetscErrorCode ierr; 9388 9389 PetscFunctionBegin; 9390 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 9391 PetscValidType(A,1); 9392 if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9393 if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9394 PetscValidHeaderSpecific(P,MAT_CLASSID,2); 9395 PetscValidType(P,2); 9396 MatCheckPreallocated(P,2); 9397 if (!P->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9398 if (P->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9399 PetscValidHeaderSpecific(C,MAT_CLASSID,3); 9400 PetscValidType(C,3); 9401 MatCheckPreallocated(C,3); 9402 if (C->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9403 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); 9404 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); 9405 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); 9406 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); 9407 MatCheckPreallocated(A,1); 9408 9409 if (!C->ops->ptapnumeric) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_ARG_WRONGSTATE,"MatPtAPNumeric implementation is missing. You should call MatPtAPSymbolic first"); 9410 ierr = PetscLogEventBegin(MAT_PtAPNumeric,A,P,0,0);CHKERRQ(ierr); 9411 ierr = (*C->ops->ptapnumeric)(A,P,C);CHKERRQ(ierr); 9412 ierr = PetscLogEventEnd(MAT_PtAPNumeric,A,P,0,0);CHKERRQ(ierr); 9413 PetscFunctionReturn(0); 9414 } 9415 9416 /*@ 9417 MatPtAPSymbolic - Creates the (i,j) structure of the matrix product C = P^T * A * P 9418 9419 Neighbor-wise Collective on Mat 9420 9421 Input Parameters: 9422 + A - the matrix 9423 - P - the projection matrix 9424 9425 Output Parameters: 9426 . C - the (i,j) structure of the product matrix 9427 9428 Notes: 9429 C will be created and must be destroyed by the user with MatDestroy(). 9430 9431 This routine is currently only implemented for pairs of SeqAIJ matrices and classes 9432 which inherit from SeqAIJ. C will be of type MATSEQAIJ. The product is computed using 9433 this (i,j) structure by calling MatPtAPNumeric(). 9434 9435 Level: intermediate 9436 9437 .seealso: MatPtAP(), MatPtAPNumeric(), MatMatMultSymbolic() 9438 @*/ 9439 PetscErrorCode MatPtAPSymbolic(Mat A,Mat P,PetscReal fill,Mat *C) 9440 { 9441 PetscErrorCode ierr; 9442 9443 PetscFunctionBegin; 9444 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 9445 PetscValidType(A,1); 9446 if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9447 if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9448 if (fill <1.0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Expected fill=%g must be >= 1.0",(double)fill); 9449 PetscValidHeaderSpecific(P,MAT_CLASSID,2); 9450 PetscValidType(P,2); 9451 MatCheckPreallocated(P,2); 9452 if (!P->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9453 if (P->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9454 PetscValidPointer(C,3); 9455 9456 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); 9457 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); 9458 MatCheckPreallocated(A,1); 9459 9460 if (!A->ops->ptapsymbolic) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"MatType %s",((PetscObject)A)->type_name); 9461 ierr = PetscLogEventBegin(MAT_PtAPSymbolic,A,P,0,0);CHKERRQ(ierr); 9462 ierr = (*A->ops->ptapsymbolic)(A,P,fill,C);CHKERRQ(ierr); 9463 ierr = PetscLogEventEnd(MAT_PtAPSymbolic,A,P,0,0);CHKERRQ(ierr); 9464 9465 /* ierr = MatSetBlockSize(*C,A->rmap->bs);CHKERRQ(ierr); NO! this is not always true -ma */ 9466 PetscFunctionReturn(0); 9467 } 9468 9469 /*@ 9470 MatRARt - Creates the matrix product C = R * A * R^T 9471 9472 Neighbor-wise Collective on Mat 9473 9474 Input Parameters: 9475 + A - the matrix 9476 . R - the projection matrix 9477 . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 9478 - fill - expected fill as ratio of nnz(C)/nnz(A), use PETSC_DEFAULT if you do not have a good estimate 9479 if the result is a dense matrix this is irrelevent 9480 9481 Output Parameters: 9482 . C - the product matrix 9483 9484 Notes: 9485 C will be created and must be destroyed by the user with MatDestroy(). 9486 9487 This routine is currently only implemented for pairs of AIJ matrices and classes 9488 which inherit from AIJ. Due to PETSc sparse matrix block row distribution among processes, 9489 parallel MatRARt is implemented via explicit transpose of R, which could be very expensive. 9490 We recommend using MatPtAP(). 9491 9492 Level: intermediate 9493 9494 .seealso: MatRARtSymbolic(), MatRARtNumeric(), MatMatMult(), MatPtAP() 9495 @*/ 9496 PetscErrorCode MatRARt(Mat A,Mat R,MatReuse scall,PetscReal fill,Mat *C) 9497 { 9498 PetscErrorCode ierr; 9499 9500 PetscFunctionBegin; 9501 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 9502 PetscValidType(A,1); 9503 if (scall == MAT_INPLACE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported"); 9504 if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9505 if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9506 PetscValidHeaderSpecific(R,MAT_CLASSID,2); 9507 PetscValidType(R,2); 9508 MatCheckPreallocated(R,2); 9509 if (!R->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9510 if (R->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9511 PetscValidPointer(C,3); 9512 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); 9513 9514 if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0; 9515 if (fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Expected fill=%g must be >= 1.0",(double)fill); 9516 MatCheckPreallocated(A,1); 9517 9518 if (!A->ops->rart) { 9519 Mat Rt; 9520 ierr = MatTranspose(R,MAT_INITIAL_MATRIX,&Rt);CHKERRQ(ierr); 9521 ierr = MatMatMatMult(R,A,Rt,scall,fill,C);CHKERRQ(ierr); 9522 ierr = MatDestroy(&Rt);CHKERRQ(ierr); 9523 PetscFunctionReturn(0); 9524 } 9525 ierr = PetscLogEventBegin(MAT_RARt,A,R,0,0);CHKERRQ(ierr); 9526 ierr = (*A->ops->rart)(A,R,scall,fill,C);CHKERRQ(ierr); 9527 ierr = PetscLogEventEnd(MAT_RARt,A,R,0,0);CHKERRQ(ierr); 9528 PetscFunctionReturn(0); 9529 } 9530 9531 /*@ 9532 MatRARtNumeric - Computes the matrix product C = R * A * R^T 9533 9534 Neighbor-wise Collective on Mat 9535 9536 Input Parameters: 9537 + A - the matrix 9538 - R - the projection matrix 9539 9540 Output Parameters: 9541 . C - the product matrix 9542 9543 Notes: 9544 C must have been created by calling MatRARtSymbolic and must be destroyed by 9545 the user using MatDestroy(). 9546 9547 This routine is currently only implemented for pairs of AIJ matrices and classes 9548 which inherit from AIJ. C will be of type MATAIJ. 9549 9550 Level: intermediate 9551 9552 .seealso: MatRARt(), MatRARtSymbolic(), MatMatMultNumeric() 9553 @*/ 9554 PetscErrorCode MatRARtNumeric(Mat A,Mat R,Mat C) 9555 { 9556 PetscErrorCode ierr; 9557 9558 PetscFunctionBegin; 9559 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 9560 PetscValidType(A,1); 9561 if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9562 if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9563 PetscValidHeaderSpecific(R,MAT_CLASSID,2); 9564 PetscValidType(R,2); 9565 MatCheckPreallocated(R,2); 9566 if (!R->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9567 if (R->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9568 PetscValidHeaderSpecific(C,MAT_CLASSID,3); 9569 PetscValidType(C,3); 9570 MatCheckPreallocated(C,3); 9571 if (C->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9572 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); 9573 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); 9574 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); 9575 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); 9576 MatCheckPreallocated(A,1); 9577 9578 ierr = PetscLogEventBegin(MAT_RARtNumeric,A,R,0,0);CHKERRQ(ierr); 9579 ierr = (*A->ops->rartnumeric)(A,R,C);CHKERRQ(ierr); 9580 ierr = PetscLogEventEnd(MAT_RARtNumeric,A,R,0,0);CHKERRQ(ierr); 9581 PetscFunctionReturn(0); 9582 } 9583 9584 /*@ 9585 MatRARtSymbolic - Creates the (i,j) structure of the matrix product C = R * A * R^T 9586 9587 Neighbor-wise Collective on Mat 9588 9589 Input Parameters: 9590 + A - the matrix 9591 - R - the projection matrix 9592 9593 Output Parameters: 9594 . C - the (i,j) structure of the product matrix 9595 9596 Notes: 9597 C will be created and must be destroyed by the user with MatDestroy(). 9598 9599 This routine is currently only implemented for pairs of SeqAIJ matrices and classes 9600 which inherit from SeqAIJ. C will be of type MATSEQAIJ. The product is computed using 9601 this (i,j) structure by calling MatRARtNumeric(). 9602 9603 Level: intermediate 9604 9605 .seealso: MatRARt(), MatRARtNumeric(), MatMatMultSymbolic() 9606 @*/ 9607 PetscErrorCode MatRARtSymbolic(Mat A,Mat R,PetscReal fill,Mat *C) 9608 { 9609 PetscErrorCode ierr; 9610 9611 PetscFunctionBegin; 9612 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 9613 PetscValidType(A,1); 9614 if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9615 if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9616 if (fill <1.0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Expected fill=%g must be >= 1.0",(double)fill); 9617 PetscValidHeaderSpecific(R,MAT_CLASSID,2); 9618 PetscValidType(R,2); 9619 MatCheckPreallocated(R,2); 9620 if (!R->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9621 if (R->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9622 PetscValidPointer(C,3); 9623 9624 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); 9625 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); 9626 MatCheckPreallocated(A,1); 9627 ierr = PetscLogEventBegin(MAT_RARtSymbolic,A,R,0,0);CHKERRQ(ierr); 9628 ierr = (*A->ops->rartsymbolic)(A,R,fill,C);CHKERRQ(ierr); 9629 ierr = PetscLogEventEnd(MAT_RARtSymbolic,A,R,0,0);CHKERRQ(ierr); 9630 9631 ierr = MatSetBlockSizes(*C,PetscAbs(R->rmap->bs),PetscAbs(R->rmap->bs));CHKERRQ(ierr); 9632 PetscFunctionReturn(0); 9633 } 9634 9635 /*@ 9636 MatMatMult - Performs Matrix-Matrix Multiplication C=A*B. 9637 9638 Neighbor-wise Collective on Mat 9639 9640 Input Parameters: 9641 + A - the left matrix 9642 . B - the right matrix 9643 . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 9644 - fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if you do not have a good estimate 9645 if the result is a dense matrix this is irrelevent 9646 9647 Output Parameters: 9648 . C - the product matrix 9649 9650 Notes: 9651 Unless scall is MAT_REUSE_MATRIX C will be created. 9652 9653 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 9654 call to this function with either MAT_INITIAL_MATRIX or MatMatMultSymbolic() 9655 9656 To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value 9657 actually needed. 9658 9659 If you have many matrices with the same non-zero structure to multiply, you 9660 should either 9661 $ 1) use MAT_REUSE_MATRIX in all calls but the first or 9662 $ 2) call MatMatMultSymbolic() once and then MatMatMultNumeric() for each product needed 9663 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 9664 with MAT_REUSE_MATRIX, rather than first having MatMatMult() create it for you. You can NEVER do this if the matrix C is sparse. 9665 9666 Level: intermediate 9667 9668 .seealso: MatMatMultSymbolic(), MatMatMultNumeric(), MatTransposeMatMult(), MatMatTransposeMult(), MatPtAP() 9669 @*/ 9670 PetscErrorCode MatMatMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C) 9671 { 9672 PetscErrorCode ierr; 9673 PetscErrorCode (*fA)(Mat,Mat,MatReuse,PetscReal,Mat*); 9674 PetscErrorCode (*fB)(Mat,Mat,MatReuse,PetscReal,Mat*); 9675 PetscErrorCode (*mult)(Mat,Mat,MatReuse,PetscReal,Mat*)=NULL; 9676 9677 PetscFunctionBegin; 9678 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 9679 PetscValidType(A,1); 9680 MatCheckPreallocated(A,1); 9681 if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9682 if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9683 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 9684 PetscValidType(B,2); 9685 MatCheckPreallocated(B,2); 9686 if (!B->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9687 if (B->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9688 PetscValidPointer(C,3); 9689 if (scall == MAT_INPLACE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported"); 9690 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); 9691 if (scall == MAT_REUSE_MATRIX) { 9692 PetscValidPointer(*C,5); 9693 PetscValidHeaderSpecific(*C,MAT_CLASSID,5); 9694 ierr = PetscLogEventBegin(MAT_MatMult,A,B,0,0);CHKERRQ(ierr); 9695 ierr = PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr); 9696 ierr = (*(*C)->ops->matmultnumeric)(A,B,*C);CHKERRQ(ierr); 9697 ierr = PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr); 9698 ierr = PetscLogEventEnd(MAT_MatMult,A,B,0,0);CHKERRQ(ierr); 9699 PetscFunctionReturn(0); 9700 } 9701 if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0; 9702 if (fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Expected fill=%g must be >= 1.0",(double)fill); 9703 9704 fA = A->ops->matmult; 9705 fB = B->ops->matmult; 9706 if (fB == fA) { 9707 if (!fB) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"MatMatMult not supported for B of type %s",((PetscObject)B)->type_name); 9708 mult = fB; 9709 } else { 9710 /* dispatch based on the type of A and B from their PetscObject's PetscFunctionLists. */ 9711 char multname[256]; 9712 ierr = PetscStrncpy(multname,"MatMatMult_",sizeof(multname));CHKERRQ(ierr); 9713 ierr = PetscStrlcat(multname,((PetscObject)A)->type_name,sizeof(multname));CHKERRQ(ierr); 9714 ierr = PetscStrlcat(multname,"_",sizeof(multname));CHKERRQ(ierr); 9715 ierr = PetscStrlcat(multname,((PetscObject)B)->type_name,sizeof(multname));CHKERRQ(ierr); 9716 ierr = PetscStrlcat(multname,"_C",sizeof(multname));CHKERRQ(ierr); /* e.g., multname = "MatMatMult_seqdense_seqaij_C" */ 9717 ierr = PetscObjectQueryFunction((PetscObject)B,multname,&mult);CHKERRQ(ierr); 9718 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); 9719 } 9720 ierr = PetscLogEventBegin(MAT_MatMult,A,B,0,0);CHKERRQ(ierr); 9721 ierr = (*mult)(A,B,scall,fill,C);CHKERRQ(ierr); 9722 ierr = PetscLogEventEnd(MAT_MatMult,A,B,0,0);CHKERRQ(ierr); 9723 PetscFunctionReturn(0); 9724 } 9725 9726 /*@ 9727 MatMatMultSymbolic - Performs construction, preallocation, and computes the ij structure 9728 of the matrix-matrix product C=A*B. Call this routine before calling MatMatMultNumeric(). 9729 9730 Neighbor-wise Collective on Mat 9731 9732 Input Parameters: 9733 + A - the left matrix 9734 . B - the right matrix 9735 - fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if you do not have a good estimate, 9736 if C is a dense matrix this is irrelevent 9737 9738 Output Parameters: 9739 . C - the product matrix 9740 9741 Notes: 9742 Unless scall is MAT_REUSE_MATRIX C will be created. 9743 9744 To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value 9745 actually needed. 9746 9747 This routine is currently implemented for 9748 - pairs of AIJ matrices and classes which inherit from AIJ, C will be of type AIJ 9749 - pairs of AIJ (A) and Dense (B) matrix, C will be of type Dense. 9750 - pairs of Dense (A) and AIJ (B) matrix, C will be of type Dense. 9751 9752 Level: intermediate 9753 9754 Developers Note: There are ways to estimate the number of nonzeros in the resulting product, see for example, http://arxiv.org/abs/1006.4173 9755 We should incorporate them into PETSc. 9756 9757 .seealso: MatMatMult(), MatMatMultNumeric() 9758 @*/ 9759 PetscErrorCode MatMatMultSymbolic(Mat A,Mat B,PetscReal fill,Mat *C) 9760 { 9761 PetscErrorCode ierr; 9762 PetscErrorCode (*Asymbolic)(Mat,Mat,PetscReal,Mat*); 9763 PetscErrorCode (*Bsymbolic)(Mat,Mat,PetscReal,Mat*); 9764 PetscErrorCode (*symbolic)(Mat,Mat,PetscReal,Mat*)=NULL; 9765 9766 PetscFunctionBegin; 9767 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 9768 PetscValidType(A,1); 9769 if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9770 if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9771 9772 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 9773 PetscValidType(B,2); 9774 MatCheckPreallocated(B,2); 9775 if (!B->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9776 if (B->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9777 PetscValidPointer(C,3); 9778 9779 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); 9780 if (fill == PETSC_DEFAULT) fill = 2.0; 9781 if (fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Expected fill=%g must be > 1.0",(double)fill); 9782 MatCheckPreallocated(A,1); 9783 9784 Asymbolic = A->ops->matmultsymbolic; 9785 Bsymbolic = B->ops->matmultsymbolic; 9786 if (Asymbolic == Bsymbolic) { 9787 if (!Bsymbolic) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"C=A*B not implemented for B of type %s",((PetscObject)B)->type_name); 9788 symbolic = Bsymbolic; 9789 } else { /* dispatch based on the type of A and B */ 9790 char symbolicname[256]; 9791 ierr = PetscStrncpy(symbolicname,"MatMatMultSymbolic_",sizeof(symbolicname));CHKERRQ(ierr); 9792 ierr = PetscStrlcat(symbolicname,((PetscObject)A)->type_name,sizeof(symbolicname));CHKERRQ(ierr); 9793 ierr = PetscStrlcat(symbolicname,"_",sizeof(symbolicname));CHKERRQ(ierr); 9794 ierr = PetscStrlcat(symbolicname,((PetscObject)B)->type_name,sizeof(symbolicname));CHKERRQ(ierr); 9795 ierr = PetscStrlcat(symbolicname,"_C",sizeof(symbolicname));CHKERRQ(ierr); 9796 ierr = PetscObjectQueryFunction((PetscObject)B,symbolicname,&symbolic);CHKERRQ(ierr); 9797 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); 9798 } 9799 ierr = PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr); 9800 ierr = (*symbolic)(A,B,fill,C);CHKERRQ(ierr); 9801 ierr = PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr); 9802 PetscFunctionReturn(0); 9803 } 9804 9805 /*@ 9806 MatMatMultNumeric - Performs the numeric matrix-matrix product. 9807 Call this routine after first calling MatMatMultSymbolic(). 9808 9809 Neighbor-wise Collective on Mat 9810 9811 Input Parameters: 9812 + A - the left matrix 9813 - B - the right matrix 9814 9815 Output Parameters: 9816 . C - the product matrix, which was created by from MatMatMultSymbolic() or a call to MatMatMult(). 9817 9818 Notes: 9819 C must have been created with MatMatMultSymbolic(). 9820 9821 This routine is currently implemented for 9822 - pairs of AIJ matrices and classes which inherit from AIJ, C will be of type MATAIJ. 9823 - pairs of AIJ (A) and Dense (B) matrix, C will be of type Dense. 9824 - pairs of Dense (A) and AIJ (B) matrix, C will be of type Dense. 9825 9826 Level: intermediate 9827 9828 .seealso: MatMatMult(), MatMatMultSymbolic() 9829 @*/ 9830 PetscErrorCode MatMatMultNumeric(Mat A,Mat B,Mat C) 9831 { 9832 PetscErrorCode ierr; 9833 9834 PetscFunctionBegin; 9835 ierr = MatMatMult(A,B,MAT_REUSE_MATRIX,0.0,&C);CHKERRQ(ierr); 9836 PetscFunctionReturn(0); 9837 } 9838 9839 /*@ 9840 MatMatTransposeMult - Performs Matrix-Matrix Multiplication C=A*B^T. 9841 9842 Neighbor-wise Collective on Mat 9843 9844 Input Parameters: 9845 + A - the left matrix 9846 . B - the right matrix 9847 . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 9848 - fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if not known 9849 9850 Output Parameters: 9851 . C - the product matrix 9852 9853 Notes: 9854 C will be created if MAT_INITIAL_MATRIX and must be destroyed by the user with MatDestroy(). 9855 9856 MAT_REUSE_MATRIX can only be used if the matrices A and B have the same nonzero pattern as in the previous call 9857 9858 To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value 9859 actually needed. 9860 9861 This routine is currently only implemented for pairs of SeqAIJ matrices and for the SeqDense class. 9862 9863 Level: intermediate 9864 9865 .seealso: MatMatTransposeMultSymbolic(), MatMatTransposeMultNumeric(), MatMatMult(), MatTransposeMatMult() MatPtAP() 9866 @*/ 9867 PetscErrorCode MatMatTransposeMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C) 9868 { 9869 PetscErrorCode ierr; 9870 PetscErrorCode (*fA)(Mat,Mat,MatReuse,PetscReal,Mat*); 9871 PetscErrorCode (*fB)(Mat,Mat,MatReuse,PetscReal,Mat*); 9872 9873 PetscFunctionBegin; 9874 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 9875 PetscValidType(A,1); 9876 if (scall == MAT_INPLACE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported"); 9877 if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9878 if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9879 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 9880 PetscValidType(B,2); 9881 MatCheckPreallocated(B,2); 9882 if (!B->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9883 if (B->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9884 PetscValidPointer(C,3); 9885 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); 9886 if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0; 9887 if (fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Expected fill=%g must be > 1.0",(double)fill); 9888 MatCheckPreallocated(A,1); 9889 9890 fA = A->ops->mattransposemult; 9891 if (!fA) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"MatMatTransposeMult not supported for A of type %s",((PetscObject)A)->type_name); 9892 fB = B->ops->mattransposemult; 9893 if (!fB) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"MatMatTransposeMult not supported for B of type %s",((PetscObject)B)->type_name); 9894 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); 9895 9896 ierr = PetscLogEventBegin(MAT_MatTransposeMult,A,B,0,0);CHKERRQ(ierr); 9897 if (scall == MAT_INITIAL_MATRIX) { 9898 ierr = PetscLogEventBegin(MAT_MatTransposeMultSymbolic,A,B,0,0);CHKERRQ(ierr); 9899 ierr = (*A->ops->mattransposemultsymbolic)(A,B,fill,C);CHKERRQ(ierr); 9900 ierr = PetscLogEventEnd(MAT_MatTransposeMultSymbolic,A,B,0,0);CHKERRQ(ierr); 9901 } 9902 ierr = PetscLogEventBegin(MAT_MatTransposeMultNumeric,A,B,0,0);CHKERRQ(ierr); 9903 ierr = (*A->ops->mattransposemultnumeric)(A,B,*C);CHKERRQ(ierr); 9904 ierr = PetscLogEventEnd(MAT_MatTransposeMultNumeric,A,B,0,0);CHKERRQ(ierr); 9905 ierr = PetscLogEventEnd(MAT_MatTransposeMult,A,B,0,0);CHKERRQ(ierr); 9906 PetscFunctionReturn(0); 9907 } 9908 9909 /*@ 9910 MatTransposeMatMult - Performs Matrix-Matrix Multiplication C=A^T*B. 9911 9912 Neighbor-wise Collective on Mat 9913 9914 Input Parameters: 9915 + A - the left matrix 9916 . B - the right matrix 9917 . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 9918 - fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if not known 9919 9920 Output Parameters: 9921 . C - the product matrix 9922 9923 Notes: 9924 C will be created if MAT_INITIAL_MATRIX and must be destroyed by the user with MatDestroy(). 9925 9926 MAT_REUSE_MATRIX can only be used if the matrices A and B have the same nonzero pattern as in the previous call 9927 9928 To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value 9929 actually needed. 9930 9931 This routine is currently implemented for pairs of AIJ matrices and pairs of SeqDense matrices and classes 9932 which inherit from SeqAIJ. C will be of same type as the input matrices. 9933 9934 Level: intermediate 9935 9936 .seealso: MatTransposeMatMultSymbolic(), MatTransposeMatMultNumeric(), MatMatMult(), MatMatTransposeMult(), MatPtAP() 9937 @*/ 9938 PetscErrorCode MatTransposeMatMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C) 9939 { 9940 PetscErrorCode ierr; 9941 PetscErrorCode (*fA)(Mat,Mat,MatReuse,PetscReal,Mat*); 9942 PetscErrorCode (*fB)(Mat,Mat,MatReuse,PetscReal,Mat*); 9943 PetscErrorCode (*transposematmult)(Mat,Mat,MatReuse,PetscReal,Mat*) = NULL; 9944 9945 PetscFunctionBegin; 9946 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 9947 PetscValidType(A,1); 9948 if (scall == MAT_INPLACE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported"); 9949 if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9950 if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9951 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 9952 PetscValidType(B,2); 9953 MatCheckPreallocated(B,2); 9954 if (!B->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 9955 if (B->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 9956 PetscValidPointer(C,3); 9957 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); 9958 if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0; 9959 if (fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Expected fill=%g must be > 1.0",(double)fill); 9960 MatCheckPreallocated(A,1); 9961 9962 fA = A->ops->transposematmult; 9963 fB = B->ops->transposematmult; 9964 if (fB==fA) { 9965 if (!fA) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"MatTransposeMatMult not supported for A of type %s",((PetscObject)A)->type_name); 9966 transposematmult = fA; 9967 } else { 9968 /* dispatch based on the type of A and B from their PetscObject's PetscFunctionLists. */ 9969 char multname[256]; 9970 ierr = PetscStrncpy(multname,"MatTransposeMatMult_",sizeof(multname));CHKERRQ(ierr); 9971 ierr = PetscStrlcat(multname,((PetscObject)A)->type_name,sizeof(multname));CHKERRQ(ierr); 9972 ierr = PetscStrlcat(multname,"_",sizeof(multname));CHKERRQ(ierr); 9973 ierr = PetscStrlcat(multname,((PetscObject)B)->type_name,sizeof(multname));CHKERRQ(ierr); 9974 ierr = PetscStrlcat(multname,"_C",sizeof(multname));CHKERRQ(ierr); /* e.g., multname = "MatMatMult_seqdense_seqaij_C" */ 9975 ierr = PetscObjectQueryFunction((PetscObject)B,multname,&transposematmult);CHKERRQ(ierr); 9976 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); 9977 } 9978 ierr = PetscLogEventBegin(MAT_TransposeMatMult,A,B,0,0);CHKERRQ(ierr); 9979 ierr = (*transposematmult)(A,B,scall,fill,C);CHKERRQ(ierr); 9980 ierr = PetscLogEventEnd(MAT_TransposeMatMult,A,B,0,0);CHKERRQ(ierr); 9981 PetscFunctionReturn(0); 9982 } 9983 9984 /*@ 9985 MatMatMatMult - Performs Matrix-Matrix-Matrix Multiplication D=A*B*C. 9986 9987 Neighbor-wise Collective on Mat 9988 9989 Input Parameters: 9990 + A - the left matrix 9991 . B - the middle matrix 9992 . C - the right matrix 9993 . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 9994 - 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 9995 if the result is a dense matrix this is irrelevent 9996 9997 Output Parameters: 9998 . D - the product matrix 9999 10000 Notes: 10001 Unless scall is MAT_REUSE_MATRIX D will be created. 10002 10003 MAT_REUSE_MATRIX can only be used if the matrices A, B and C have the same nonzero pattern as in the previous call 10004 10005 To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value 10006 actually needed. 10007 10008 If you have many matrices with the same non-zero structure to multiply, you 10009 should use MAT_REUSE_MATRIX in all calls but the first or 10010 10011 Level: intermediate 10012 10013 .seealso: MatMatMult, MatPtAP() 10014 @*/ 10015 PetscErrorCode MatMatMatMult(Mat A,Mat B,Mat C,MatReuse scall,PetscReal fill,Mat *D) 10016 { 10017 PetscErrorCode ierr; 10018 PetscErrorCode (*fA)(Mat,Mat,Mat,MatReuse,PetscReal,Mat*); 10019 PetscErrorCode (*fB)(Mat,Mat,Mat,MatReuse,PetscReal,Mat*); 10020 PetscErrorCode (*fC)(Mat,Mat,Mat,MatReuse,PetscReal,Mat*); 10021 PetscErrorCode (*mult)(Mat,Mat,Mat,MatReuse,PetscReal,Mat*)=NULL; 10022 10023 PetscFunctionBegin; 10024 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 10025 PetscValidType(A,1); 10026 MatCheckPreallocated(A,1); 10027 if (scall == MAT_INPLACE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported"); 10028 if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 10029 if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 10030 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 10031 PetscValidType(B,2); 10032 MatCheckPreallocated(B,2); 10033 if (!B->assembled) SETERRQ(PetscObjectComm((PetscObject)B),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 10034 if (B->factortype) SETERRQ(PetscObjectComm((PetscObject)B),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 10035 PetscValidHeaderSpecific(C,MAT_CLASSID,3); 10036 PetscValidPointer(C,3); 10037 MatCheckPreallocated(C,3); 10038 if (!C->assembled) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 10039 if (C->factortype) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 10040 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); 10041 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); 10042 if (scall == MAT_REUSE_MATRIX) { 10043 PetscValidPointer(*D,6); 10044 PetscValidHeaderSpecific(*D,MAT_CLASSID,6); 10045 ierr = PetscLogEventBegin(MAT_MatMatMult,A,B,0,0);CHKERRQ(ierr); 10046 ierr = (*(*D)->ops->matmatmult)(A,B,C,scall,fill,D);CHKERRQ(ierr); 10047 ierr = PetscLogEventEnd(MAT_MatMatMult,A,B,0,0);CHKERRQ(ierr); 10048 PetscFunctionReturn(0); 10049 } 10050 if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0; 10051 if (fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Expected fill=%g must be >= 1.0",(double)fill); 10052 10053 fA = A->ops->matmatmult; 10054 fB = B->ops->matmatmult; 10055 fC = C->ops->matmatmult; 10056 if (fA == fB && fA == fC) { 10057 if (!fA) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"MatMatMatMult not supported for A of type %s",((PetscObject)A)->type_name); 10058 mult = fA; 10059 } else { 10060 /* dispatch based on the type of A, B and C from their PetscObject's PetscFunctionLists. */ 10061 char multname[256]; 10062 ierr = PetscStrncpy(multname,"MatMatMatMult_",sizeof(multname));CHKERRQ(ierr); 10063 ierr = PetscStrlcat(multname,((PetscObject)A)->type_name,sizeof(multname));CHKERRQ(ierr); 10064 ierr = PetscStrlcat(multname,"_",sizeof(multname));CHKERRQ(ierr); 10065 ierr = PetscStrlcat(multname,((PetscObject)B)->type_name,sizeof(multname));CHKERRQ(ierr); 10066 ierr = PetscStrlcat(multname,"_",sizeof(multname));CHKERRQ(ierr); 10067 ierr = PetscStrlcat(multname,((PetscObject)C)->type_name,sizeof(multname));CHKERRQ(ierr); 10068 ierr = PetscStrlcat(multname,"_C",sizeof(multname));CHKERRQ(ierr); 10069 ierr = PetscObjectQueryFunction((PetscObject)B,multname,&mult);CHKERRQ(ierr); 10070 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); 10071 } 10072 ierr = PetscLogEventBegin(MAT_MatMatMult,A,B,0,0);CHKERRQ(ierr); 10073 ierr = (*mult)(A,B,C,scall,fill,D);CHKERRQ(ierr); 10074 ierr = PetscLogEventEnd(MAT_MatMatMult,A,B,0,0);CHKERRQ(ierr); 10075 PetscFunctionReturn(0); 10076 } 10077 10078 /*@ 10079 MatCreateRedundantMatrix - Create redundant matrices and put them into processors of subcommunicators. 10080 10081 Collective on Mat 10082 10083 Input Parameters: 10084 + mat - the matrix 10085 . nsubcomm - the number of subcommunicators (= number of redundant parallel or sequential matrices) 10086 . subcomm - MPI communicator split from the communicator where mat resides in (or MPI_COMM_NULL if nsubcomm is used) 10087 - reuse - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 10088 10089 Output Parameter: 10090 . matredundant - redundant matrix 10091 10092 Notes: 10093 MAT_REUSE_MATRIX can only be used when the nonzero structure of the 10094 original matrix has not changed from that last call to MatCreateRedundantMatrix(). 10095 10096 This routine creates the duplicated matrices in subcommunicators; you should NOT create them before 10097 calling it. 10098 10099 Level: advanced 10100 10101 Concepts: subcommunicator 10102 Concepts: duplicate matrix 10103 10104 .seealso: MatDestroy() 10105 @*/ 10106 PetscErrorCode MatCreateRedundantMatrix(Mat mat,PetscInt nsubcomm,MPI_Comm subcomm,MatReuse reuse,Mat *matredundant) 10107 { 10108 PetscErrorCode ierr; 10109 MPI_Comm comm; 10110 PetscMPIInt size; 10111 PetscInt mloc_sub,nloc_sub,rstart,rend,M=mat->rmap->N,N=mat->cmap->N,bs=mat->rmap->bs; 10112 Mat_Redundant *redund=NULL; 10113 PetscSubcomm psubcomm=NULL; 10114 MPI_Comm subcomm_in=subcomm; 10115 Mat *matseq; 10116 IS isrow,iscol; 10117 PetscBool newsubcomm=PETSC_FALSE; 10118 10119 PetscFunctionBegin; 10120 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10121 if (nsubcomm && reuse == MAT_REUSE_MATRIX) { 10122 PetscValidPointer(*matredundant,5); 10123 PetscValidHeaderSpecific(*matredundant,MAT_CLASSID,5); 10124 } 10125 10126 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);CHKERRQ(ierr); 10127 if (size == 1 || nsubcomm == 1) { 10128 if (reuse == MAT_INITIAL_MATRIX) { 10129 ierr = MatDuplicate(mat,MAT_COPY_VALUES,matredundant);CHKERRQ(ierr); 10130 } else { 10131 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"); 10132 ierr = MatCopy(mat,*matredundant,SAME_NONZERO_PATTERN);CHKERRQ(ierr); 10133 } 10134 PetscFunctionReturn(0); 10135 } 10136 10137 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 10138 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 10139 MatCheckPreallocated(mat,1); 10140 10141 ierr = PetscLogEventBegin(MAT_RedundantMat,mat,0,0,0);CHKERRQ(ierr); 10142 if (subcomm_in == MPI_COMM_NULL && reuse == MAT_INITIAL_MATRIX) { /* get subcomm if user does not provide subcomm */ 10143 /* create psubcomm, then get subcomm */ 10144 ierr = PetscObjectGetComm((PetscObject)mat,&comm);CHKERRQ(ierr); 10145 ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 10146 if (nsubcomm < 1 || nsubcomm > size) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"nsubcomm must between 1 and %D",size); 10147 10148 ierr = PetscSubcommCreate(comm,&psubcomm);CHKERRQ(ierr); 10149 ierr = PetscSubcommSetNumber(psubcomm,nsubcomm);CHKERRQ(ierr); 10150 ierr = PetscSubcommSetType(psubcomm,PETSC_SUBCOMM_CONTIGUOUS);CHKERRQ(ierr); 10151 ierr = PetscSubcommSetFromOptions(psubcomm);CHKERRQ(ierr); 10152 ierr = PetscCommDuplicate(PetscSubcommChild(psubcomm),&subcomm,NULL);CHKERRQ(ierr); 10153 newsubcomm = PETSC_TRUE; 10154 ierr = PetscSubcommDestroy(&psubcomm);CHKERRQ(ierr); 10155 } 10156 10157 /* get isrow, iscol and a local sequential matrix matseq[0] */ 10158 if (reuse == MAT_INITIAL_MATRIX) { 10159 mloc_sub = PETSC_DECIDE; 10160 nloc_sub = PETSC_DECIDE; 10161 if (bs < 1) { 10162 ierr = PetscSplitOwnership(subcomm,&mloc_sub,&M);CHKERRQ(ierr); 10163 ierr = PetscSplitOwnership(subcomm,&nloc_sub,&N);CHKERRQ(ierr); 10164 } else { 10165 ierr = PetscSplitOwnershipBlock(subcomm,bs,&mloc_sub,&M);CHKERRQ(ierr); 10166 ierr = PetscSplitOwnershipBlock(subcomm,bs,&nloc_sub,&N);CHKERRQ(ierr); 10167 } 10168 ierr = MPI_Scan(&mloc_sub,&rend,1,MPIU_INT,MPI_SUM,subcomm);CHKERRQ(ierr); 10169 rstart = rend - mloc_sub; 10170 ierr = ISCreateStride(PETSC_COMM_SELF,mloc_sub,rstart,1,&isrow);CHKERRQ(ierr); 10171 ierr = ISCreateStride(PETSC_COMM_SELF,N,0,1,&iscol);CHKERRQ(ierr); 10172 } else { /* reuse == MAT_REUSE_MATRIX */ 10173 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"); 10174 /* retrieve subcomm */ 10175 ierr = PetscObjectGetComm((PetscObject)(*matredundant),&subcomm);CHKERRQ(ierr); 10176 redund = (*matredundant)->redundant; 10177 isrow = redund->isrow; 10178 iscol = redund->iscol; 10179 matseq = redund->matseq; 10180 } 10181 ierr = MatCreateSubMatrices(mat,1,&isrow,&iscol,reuse,&matseq);CHKERRQ(ierr); 10182 10183 /* get matredundant over subcomm */ 10184 if (reuse == MAT_INITIAL_MATRIX) { 10185 ierr = MatCreateMPIMatConcatenateSeqMat(subcomm,matseq[0],nloc_sub,reuse,matredundant);CHKERRQ(ierr); 10186 10187 /* create a supporting struct and attach it to C for reuse */ 10188 ierr = PetscNewLog(*matredundant,&redund);CHKERRQ(ierr); 10189 (*matredundant)->redundant = redund; 10190 redund->isrow = isrow; 10191 redund->iscol = iscol; 10192 redund->matseq = matseq; 10193 if (newsubcomm) { 10194 redund->subcomm = subcomm; 10195 } else { 10196 redund->subcomm = MPI_COMM_NULL; 10197 } 10198 } else { 10199 ierr = MatCreateMPIMatConcatenateSeqMat(subcomm,matseq[0],PETSC_DECIDE,reuse,matredundant);CHKERRQ(ierr); 10200 } 10201 ierr = PetscLogEventEnd(MAT_RedundantMat,mat,0,0,0);CHKERRQ(ierr); 10202 PetscFunctionReturn(0); 10203 } 10204 10205 /*@C 10206 MatGetMultiProcBlock - Create multiple [bjacobi] 'parallel submatrices' from 10207 a given 'mat' object. Each submatrix can span multiple procs. 10208 10209 Collective on Mat 10210 10211 Input Parameters: 10212 + mat - the matrix 10213 . subcomm - the subcommunicator obtained by com_split(comm) 10214 - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 10215 10216 Output Parameter: 10217 . subMat - 'parallel submatrices each spans a given subcomm 10218 10219 Notes: 10220 The submatrix partition across processors is dictated by 'subComm' a 10221 communicator obtained by com_split(comm). The comm_split 10222 is not restriced to be grouped with consecutive original ranks. 10223 10224 Due the comm_split() usage, the parallel layout of the submatrices 10225 map directly to the layout of the original matrix [wrt the local 10226 row,col partitioning]. So the original 'DiagonalMat' naturally maps 10227 into the 'DiagonalMat' of the subMat, hence it is used directly from 10228 the subMat. However the offDiagMat looses some columns - and this is 10229 reconstructed with MatSetValues() 10230 10231 Level: advanced 10232 10233 Concepts: subcommunicator 10234 Concepts: submatrices 10235 10236 .seealso: MatCreateSubMatrices() 10237 @*/ 10238 PetscErrorCode MatGetMultiProcBlock(Mat mat, MPI_Comm subComm, MatReuse scall,Mat *subMat) 10239 { 10240 PetscErrorCode ierr; 10241 PetscMPIInt commsize,subCommSize; 10242 10243 PetscFunctionBegin; 10244 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&commsize);CHKERRQ(ierr); 10245 ierr = MPI_Comm_size(subComm,&subCommSize);CHKERRQ(ierr); 10246 if (subCommSize > commsize) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"CommSize %D < SubCommZize %D",commsize,subCommSize); 10247 10248 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"); 10249 ierr = PetscLogEventBegin(MAT_GetMultiProcBlock,mat,0,0,0);CHKERRQ(ierr); 10250 ierr = (*mat->ops->getmultiprocblock)(mat,subComm,scall,subMat);CHKERRQ(ierr); 10251 ierr = PetscLogEventEnd(MAT_GetMultiProcBlock,mat,0,0,0);CHKERRQ(ierr); 10252 PetscFunctionReturn(0); 10253 } 10254 10255 /*@ 10256 MatGetLocalSubMatrix - Gets a reference to a submatrix specified in local numbering 10257 10258 Not Collective 10259 10260 Input Arguments: 10261 mat - matrix to extract local submatrix from 10262 isrow - local row indices for submatrix 10263 iscol - local column indices for submatrix 10264 10265 Output Arguments: 10266 submat - the submatrix 10267 10268 Level: intermediate 10269 10270 Notes: 10271 The submat should be returned with MatRestoreLocalSubMatrix(). 10272 10273 Depending on the format of mat, the returned submat may not implement MatMult(). Its communicator may be 10274 the same as mat, it may be PETSC_COMM_SELF, or some other subcomm of mat's. 10275 10276 The submat always implements MatSetValuesLocal(). If isrow and iscol have the same block size, then 10277 MatSetValuesBlockedLocal() will also be implemented. 10278 10279 The mat must have had a ISLocalToGlobalMapping provided to it with MatSetLocalToGlobalMapping(). Note that 10280 matrices obtained with DMCreateMatrix() generally already have the local to global mapping provided. 10281 10282 .seealso: MatRestoreLocalSubMatrix(), MatCreateLocalRef(), MatSetLocalToGlobalMapping() 10283 @*/ 10284 PetscErrorCode MatGetLocalSubMatrix(Mat mat,IS isrow,IS iscol,Mat *submat) 10285 { 10286 PetscErrorCode ierr; 10287 10288 PetscFunctionBegin; 10289 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10290 PetscValidHeaderSpecific(isrow,IS_CLASSID,2); 10291 PetscValidHeaderSpecific(iscol,IS_CLASSID,3); 10292 PetscCheckSameComm(isrow,2,iscol,3); 10293 PetscValidPointer(submat,4); 10294 if (!mat->rmap->mapping) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Matrix must have local to global mapping provided before this call"); 10295 10296 if (mat->ops->getlocalsubmatrix) { 10297 ierr = (*mat->ops->getlocalsubmatrix)(mat,isrow,iscol,submat);CHKERRQ(ierr); 10298 } else { 10299 ierr = MatCreateLocalRef(mat,isrow,iscol,submat);CHKERRQ(ierr); 10300 } 10301 PetscFunctionReturn(0); 10302 } 10303 10304 /*@ 10305 MatRestoreLocalSubMatrix - Restores a reference to a submatrix specified in local numbering 10306 10307 Not Collective 10308 10309 Input Arguments: 10310 mat - matrix to extract local submatrix from 10311 isrow - local row indices for submatrix 10312 iscol - local column indices for submatrix 10313 submat - the submatrix 10314 10315 Level: intermediate 10316 10317 .seealso: MatGetLocalSubMatrix() 10318 @*/ 10319 PetscErrorCode MatRestoreLocalSubMatrix(Mat mat,IS isrow,IS iscol,Mat *submat) 10320 { 10321 PetscErrorCode ierr; 10322 10323 PetscFunctionBegin; 10324 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10325 PetscValidHeaderSpecific(isrow,IS_CLASSID,2); 10326 PetscValidHeaderSpecific(iscol,IS_CLASSID,3); 10327 PetscCheckSameComm(isrow,2,iscol,3); 10328 PetscValidPointer(submat,4); 10329 if (*submat) { 10330 PetscValidHeaderSpecific(*submat,MAT_CLASSID,4); 10331 } 10332 10333 if (mat->ops->restorelocalsubmatrix) { 10334 ierr = (*mat->ops->restorelocalsubmatrix)(mat,isrow,iscol,submat);CHKERRQ(ierr); 10335 } else { 10336 ierr = MatDestroy(submat);CHKERRQ(ierr); 10337 } 10338 *submat = NULL; 10339 PetscFunctionReturn(0); 10340 } 10341 10342 /* --------------------------------------------------------*/ 10343 /*@ 10344 MatFindZeroDiagonals - Finds all the rows of a matrix that have zero or no diagonal entry in the matrix 10345 10346 Collective on Mat 10347 10348 Input Parameter: 10349 . mat - the matrix 10350 10351 Output Parameter: 10352 . is - if any rows have zero diagonals this contains the list of them 10353 10354 Level: developer 10355 10356 Concepts: matrix-vector product 10357 10358 .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd() 10359 @*/ 10360 PetscErrorCode MatFindZeroDiagonals(Mat mat,IS *is) 10361 { 10362 PetscErrorCode ierr; 10363 10364 PetscFunctionBegin; 10365 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10366 PetscValidType(mat,1); 10367 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 10368 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 10369 10370 if (!mat->ops->findzerodiagonals) { 10371 Vec diag; 10372 const PetscScalar *a; 10373 PetscInt *rows; 10374 PetscInt rStart, rEnd, r, nrow = 0; 10375 10376 ierr = MatCreateVecs(mat, &diag, NULL);CHKERRQ(ierr); 10377 ierr = MatGetDiagonal(mat, diag);CHKERRQ(ierr); 10378 ierr = MatGetOwnershipRange(mat, &rStart, &rEnd);CHKERRQ(ierr); 10379 ierr = VecGetArrayRead(diag, &a);CHKERRQ(ierr); 10380 for (r = 0; r < rEnd-rStart; ++r) if (a[r] == 0.0) ++nrow; 10381 ierr = PetscMalloc1(nrow, &rows);CHKERRQ(ierr); 10382 nrow = 0; 10383 for (r = 0; r < rEnd-rStart; ++r) if (a[r] == 0.0) rows[nrow++] = r+rStart; 10384 ierr = VecRestoreArrayRead(diag, &a);CHKERRQ(ierr); 10385 ierr = VecDestroy(&diag);CHKERRQ(ierr); 10386 ierr = ISCreateGeneral(PetscObjectComm((PetscObject) mat), nrow, rows, PETSC_OWN_POINTER, is);CHKERRQ(ierr); 10387 } else { 10388 ierr = (*mat->ops->findzerodiagonals)(mat, is);CHKERRQ(ierr); 10389 } 10390 PetscFunctionReturn(0); 10391 } 10392 10393 /*@ 10394 MatFindOffBlockDiagonalEntries - Finds all the rows of a matrix that have entries outside of the main diagonal block (defined by the matrix block size) 10395 10396 Collective on Mat 10397 10398 Input Parameter: 10399 . mat - the matrix 10400 10401 Output Parameter: 10402 . is - contains the list of rows with off block diagonal entries 10403 10404 Level: developer 10405 10406 Concepts: matrix-vector product 10407 10408 .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd() 10409 @*/ 10410 PetscErrorCode MatFindOffBlockDiagonalEntries(Mat mat,IS *is) 10411 { 10412 PetscErrorCode ierr; 10413 10414 PetscFunctionBegin; 10415 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10416 PetscValidType(mat,1); 10417 if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 10418 if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 10419 10420 if (!mat->ops->findoffblockdiagonalentries) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"This matrix type does not have a find off block diagonal entries defined"); 10421 ierr = (*mat->ops->findoffblockdiagonalentries)(mat,is);CHKERRQ(ierr); 10422 PetscFunctionReturn(0); 10423 } 10424 10425 /*@C 10426 MatInvertBlockDiagonal - Inverts the block diagonal entries. 10427 10428 Collective on Mat 10429 10430 Input Parameters: 10431 . mat - the matrix 10432 10433 Output Parameters: 10434 . values - the block inverses in column major order (FORTRAN-like) 10435 10436 Note: 10437 This routine is not available from Fortran. 10438 10439 Level: advanced 10440 10441 .seealso: MatInvertBockDiagonalMat 10442 @*/ 10443 PetscErrorCode MatInvertBlockDiagonal(Mat mat,const PetscScalar **values) 10444 { 10445 PetscErrorCode ierr; 10446 10447 PetscFunctionBegin; 10448 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10449 if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 10450 if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 10451 if (!mat->ops->invertblockdiagonal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported"); 10452 ierr = (*mat->ops->invertblockdiagonal)(mat,values);CHKERRQ(ierr); 10453 PetscFunctionReturn(0); 10454 } 10455 10456 /*@C 10457 MatInvertVariableBlockDiagonal - Inverts the block diagonal entries. 10458 10459 Collective on Mat 10460 10461 Input Parameters: 10462 + mat - the matrix 10463 . nblocks - the number of blocks 10464 - bsizes - the size of each block 10465 10466 Output Parameters: 10467 . values - the block inverses in column major order (FORTRAN-like) 10468 10469 Note: 10470 This routine is not available from Fortran. 10471 10472 Level: advanced 10473 10474 .seealso: MatInvertBockDiagonal() 10475 @*/ 10476 PetscErrorCode MatInvertVariableBlockDiagonal(Mat mat,PetscInt nblocks,const PetscInt *bsizes,PetscScalar *values) 10477 { 10478 PetscErrorCode ierr; 10479 10480 PetscFunctionBegin; 10481 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10482 if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 10483 if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 10484 if (!mat->ops->invertvariableblockdiagonal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported"); 10485 ierr = (*mat->ops->invertvariableblockdiagonal)(mat,nblocks,bsizes,values);CHKERRQ(ierr); 10486 PetscFunctionReturn(0); 10487 } 10488 10489 /*@ 10490 MatInvertBlockDiagonalMat - set matrix C to be the inverted block diagonal of matrix A 10491 10492 Collective on Mat 10493 10494 Input Parameters: 10495 . A - the matrix 10496 10497 Output Parameters: 10498 . C - matrix with inverted block diagonal of A. This matrix should be created and may have its type set. 10499 10500 Notes: the blocksize of the matrix is used to determine the blocks on the diagonal of C 10501 10502 Level: advanced 10503 10504 .seealso: MatInvertBockDiagonal() 10505 @*/ 10506 PetscErrorCode MatInvertBlockDiagonalMat(Mat A,Mat C) 10507 { 10508 PetscErrorCode ierr; 10509 const PetscScalar *vals; 10510 PetscInt *dnnz; 10511 PetscInt M,N,m,n,rstart,rend,bs,i,j; 10512 10513 PetscFunctionBegin; 10514 ierr = MatInvertBlockDiagonal(A,&vals);CHKERRQ(ierr); 10515 ierr = MatGetBlockSize(A,&bs);CHKERRQ(ierr); 10516 ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr); 10517 ierr = MatGetLocalSize(A,&m,&n);CHKERRQ(ierr); 10518 ierr = MatSetSizes(C,m,n,M,N);CHKERRQ(ierr); 10519 ierr = MatSetBlockSize(C,bs);CHKERRQ(ierr); 10520 ierr = PetscMalloc1(m/bs,&dnnz);CHKERRQ(ierr); 10521 for(j = 0; j < m/bs; j++) { 10522 dnnz[j] = 1; 10523 } 10524 ierr = MatXAIJSetPreallocation(C,bs,dnnz,NULL,NULL,NULL);CHKERRQ(ierr); 10525 ierr = PetscFree(dnnz);CHKERRQ(ierr); 10526 ierr = MatGetOwnershipRange(C,&rstart,&rend);CHKERRQ(ierr); 10527 ierr = MatSetOption(C,MAT_ROW_ORIENTED,PETSC_FALSE);CHKERRQ(ierr); 10528 for (i = rstart/bs; i < rend/bs; i++) { 10529 ierr = MatSetValuesBlocked(C,1,&i,1,&i,&vals[(i-rstart/bs)*bs*bs],INSERT_VALUES);CHKERRQ(ierr); 10530 } 10531 ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 10532 ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 10533 ierr = MatSetOption(C,MAT_ROW_ORIENTED,PETSC_TRUE);CHKERRQ(ierr); 10534 PetscFunctionReturn(0); 10535 } 10536 10537 /*@C 10538 MatTransposeColoringDestroy - Destroys a coloring context for matrix product C=A*B^T that was created 10539 via MatTransposeColoringCreate(). 10540 10541 Collective on MatTransposeColoring 10542 10543 Input Parameter: 10544 . c - coloring context 10545 10546 Level: intermediate 10547 10548 .seealso: MatTransposeColoringCreate() 10549 @*/ 10550 PetscErrorCode MatTransposeColoringDestroy(MatTransposeColoring *c) 10551 { 10552 PetscErrorCode ierr; 10553 MatTransposeColoring matcolor=*c; 10554 10555 PetscFunctionBegin; 10556 if (!matcolor) PetscFunctionReturn(0); 10557 if (--((PetscObject)matcolor)->refct > 0) {matcolor = 0; PetscFunctionReturn(0);} 10558 10559 ierr = PetscFree3(matcolor->ncolumns,matcolor->nrows,matcolor->colorforrow);CHKERRQ(ierr); 10560 ierr = PetscFree(matcolor->rows);CHKERRQ(ierr); 10561 ierr = PetscFree(matcolor->den2sp);CHKERRQ(ierr); 10562 ierr = PetscFree(matcolor->colorforcol);CHKERRQ(ierr); 10563 ierr = PetscFree(matcolor->columns);CHKERRQ(ierr); 10564 if (matcolor->brows>0) { 10565 ierr = PetscFree(matcolor->lstart);CHKERRQ(ierr); 10566 } 10567 ierr = PetscHeaderDestroy(c);CHKERRQ(ierr); 10568 PetscFunctionReturn(0); 10569 } 10570 10571 /*@C 10572 MatTransColoringApplySpToDen - Given a symbolic matrix product C=A*B^T for which 10573 a MatTransposeColoring context has been created, computes a dense B^T by Apply 10574 MatTransposeColoring to sparse B. 10575 10576 Collective on MatTransposeColoring 10577 10578 Input Parameters: 10579 + B - sparse matrix B 10580 . Btdense - symbolic dense matrix B^T 10581 - coloring - coloring context created with MatTransposeColoringCreate() 10582 10583 Output Parameter: 10584 . Btdense - dense matrix B^T 10585 10586 Level: advanced 10587 10588 Notes: 10589 These are used internally for some implementations of MatRARt() 10590 10591 .seealso: MatTransposeColoringCreate(), MatTransposeColoringDestroy(), MatTransColoringApplyDenToSp() 10592 10593 .keywords: coloring 10594 @*/ 10595 PetscErrorCode MatTransColoringApplySpToDen(MatTransposeColoring coloring,Mat B,Mat Btdense) 10596 { 10597 PetscErrorCode ierr; 10598 10599 PetscFunctionBegin; 10600 PetscValidHeaderSpecific(B,MAT_CLASSID,1); 10601 PetscValidHeaderSpecific(Btdense,MAT_CLASSID,2); 10602 PetscValidHeaderSpecific(coloring,MAT_TRANSPOSECOLORING_CLASSID,3); 10603 10604 if (!B->ops->transcoloringapplysptoden) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported for this matrix type %s",((PetscObject)B)->type_name); 10605 ierr = (B->ops->transcoloringapplysptoden)(coloring,B,Btdense);CHKERRQ(ierr); 10606 PetscFunctionReturn(0); 10607 } 10608 10609 /*@C 10610 MatTransColoringApplyDenToSp - Given a symbolic matrix product Csp=A*B^T for which 10611 a MatTransposeColoring context has been created and a dense matrix Cden=A*Btdense 10612 in which Btdens is obtained from MatTransColoringApplySpToDen(), recover sparse matrix 10613 Csp from Cden. 10614 10615 Collective on MatTransposeColoring 10616 10617 Input Parameters: 10618 + coloring - coloring context created with MatTransposeColoringCreate() 10619 - Cden - matrix product of a sparse matrix and a dense matrix Btdense 10620 10621 Output Parameter: 10622 . Csp - sparse matrix 10623 10624 Level: advanced 10625 10626 Notes: 10627 These are used internally for some implementations of MatRARt() 10628 10629 .seealso: MatTransposeColoringCreate(), MatTransposeColoringDestroy(), MatTransColoringApplySpToDen() 10630 10631 .keywords: coloring 10632 @*/ 10633 PetscErrorCode MatTransColoringApplyDenToSp(MatTransposeColoring matcoloring,Mat Cden,Mat Csp) 10634 { 10635 PetscErrorCode ierr; 10636 10637 PetscFunctionBegin; 10638 PetscValidHeaderSpecific(matcoloring,MAT_TRANSPOSECOLORING_CLASSID,1); 10639 PetscValidHeaderSpecific(Cden,MAT_CLASSID,2); 10640 PetscValidHeaderSpecific(Csp,MAT_CLASSID,3); 10641 10642 if (!Csp->ops->transcoloringapplydentosp) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported for this matrix type %s",((PetscObject)Csp)->type_name); 10643 ierr = (Csp->ops->transcoloringapplydentosp)(matcoloring,Cden,Csp);CHKERRQ(ierr); 10644 PetscFunctionReturn(0); 10645 } 10646 10647 /*@C 10648 MatTransposeColoringCreate - Creates a matrix coloring context for matrix product C=A*B^T. 10649 10650 Collective on Mat 10651 10652 Input Parameters: 10653 + mat - the matrix product C 10654 - iscoloring - the coloring of the matrix; usually obtained with MatColoringCreate() or DMCreateColoring() 10655 10656 Output Parameter: 10657 . color - the new coloring context 10658 10659 Level: intermediate 10660 10661 .seealso: MatTransposeColoringDestroy(), MatTransColoringApplySpToDen(), 10662 MatTransColoringApplyDenToSp() 10663 @*/ 10664 PetscErrorCode MatTransposeColoringCreate(Mat mat,ISColoring iscoloring,MatTransposeColoring *color) 10665 { 10666 MatTransposeColoring c; 10667 MPI_Comm comm; 10668 PetscErrorCode ierr; 10669 10670 PetscFunctionBegin; 10671 ierr = PetscLogEventBegin(MAT_TransposeColoringCreate,mat,0,0,0);CHKERRQ(ierr); 10672 ierr = PetscObjectGetComm((PetscObject)mat,&comm);CHKERRQ(ierr); 10673 ierr = PetscHeaderCreate(c,MAT_TRANSPOSECOLORING_CLASSID,"MatTransposeColoring","Matrix product C=A*B^T via coloring","Mat",comm,MatTransposeColoringDestroy,NULL);CHKERRQ(ierr); 10674 10675 c->ctype = iscoloring->ctype; 10676 if (mat->ops->transposecoloringcreate) { 10677 ierr = (*mat->ops->transposecoloringcreate)(mat,iscoloring,c);CHKERRQ(ierr); 10678 } else SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Code not yet written for this matrix type"); 10679 10680 *color = c; 10681 ierr = PetscLogEventEnd(MAT_TransposeColoringCreate,mat,0,0,0);CHKERRQ(ierr); 10682 PetscFunctionReturn(0); 10683 } 10684 10685 /*@ 10686 MatGetNonzeroState - Returns a 64 bit integer representing the current state of nonzeros in the matrix. If the 10687 matrix has had no new nonzero locations added to the matrix since the previous call then the value will be the 10688 same, otherwise it will be larger 10689 10690 Not Collective 10691 10692 Input Parameter: 10693 . A - the matrix 10694 10695 Output Parameter: 10696 . state - the current state 10697 10698 Notes: 10699 You can only compare states from two different calls to the SAME matrix, you cannot compare calls between 10700 different matrices 10701 10702 Level: intermediate 10703 10704 @*/ 10705 PetscErrorCode MatGetNonzeroState(Mat mat,PetscObjectState *state) 10706 { 10707 PetscFunctionBegin; 10708 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10709 *state = mat->nonzerostate; 10710 PetscFunctionReturn(0); 10711 } 10712 10713 /*@ 10714 MatCreateMPIMatConcatenateSeqMat - Creates a single large PETSc matrix by concatenating sequential 10715 matrices from each processor 10716 10717 Collective on MPI_Comm 10718 10719 Input Parameters: 10720 + comm - the communicators the parallel matrix will live on 10721 . seqmat - the input sequential matrices 10722 . n - number of local columns (or PETSC_DECIDE) 10723 - reuse - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 10724 10725 Output Parameter: 10726 . mpimat - the parallel matrix generated 10727 10728 Level: advanced 10729 10730 Notes: 10731 The number of columns of the matrix in EACH processor MUST be the same. 10732 10733 @*/ 10734 PetscErrorCode MatCreateMPIMatConcatenateSeqMat(MPI_Comm comm,Mat seqmat,PetscInt n,MatReuse reuse,Mat *mpimat) 10735 { 10736 PetscErrorCode ierr; 10737 10738 PetscFunctionBegin; 10739 if (!seqmat->ops->creatempimatconcatenateseqmat) SETERRQ1(PetscObjectComm((PetscObject)seqmat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)seqmat)->type_name); 10740 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"); 10741 10742 ierr = PetscLogEventBegin(MAT_Merge,seqmat,0,0,0);CHKERRQ(ierr); 10743 ierr = (*seqmat->ops->creatempimatconcatenateseqmat)(comm,seqmat,n,reuse,mpimat);CHKERRQ(ierr); 10744 ierr = PetscLogEventEnd(MAT_Merge,seqmat,0,0,0);CHKERRQ(ierr); 10745 PetscFunctionReturn(0); 10746 } 10747 10748 /*@ 10749 MatSubdomainsCreateCoalesce - Creates index subdomains by coalescing adjacent 10750 ranks' ownership ranges. 10751 10752 Collective on A 10753 10754 Input Parameters: 10755 + A - the matrix to create subdomains from 10756 - N - requested number of subdomains 10757 10758 10759 Output Parameters: 10760 + n - number of subdomains resulting on this rank 10761 - iss - IS list with indices of subdomains on this rank 10762 10763 Level: advanced 10764 10765 Notes: 10766 number of subdomains must be smaller than the communicator size 10767 @*/ 10768 PetscErrorCode MatSubdomainsCreateCoalesce(Mat A,PetscInt N,PetscInt *n,IS *iss[]) 10769 { 10770 MPI_Comm comm,subcomm; 10771 PetscMPIInt size,rank,color; 10772 PetscInt rstart,rend,k; 10773 PetscErrorCode ierr; 10774 10775 PetscFunctionBegin; 10776 ierr = PetscObjectGetComm((PetscObject)A,&comm);CHKERRQ(ierr); 10777 ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 10778 ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 10779 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); 10780 *n = 1; 10781 k = ((PetscInt)size)/N + ((PetscInt)size%N>0); /* There are up to k ranks to a color */ 10782 color = rank/k; 10783 ierr = MPI_Comm_split(comm,color,rank,&subcomm);CHKERRQ(ierr); 10784 ierr = PetscMalloc1(1,iss);CHKERRQ(ierr); 10785 ierr = MatGetOwnershipRange(A,&rstart,&rend);CHKERRQ(ierr); 10786 ierr = ISCreateStride(subcomm,rend-rstart,rstart,1,iss[0]);CHKERRQ(ierr); 10787 ierr = MPI_Comm_free(&subcomm);CHKERRQ(ierr); 10788 PetscFunctionReturn(0); 10789 } 10790 10791 /*@ 10792 MatGalerkin - Constructs the coarse grid problem via Galerkin projection. 10793 10794 If the interpolation and restriction operators are the same, uses MatPtAP. 10795 If they are not the same, use MatMatMatMult. 10796 10797 Once the coarse grid problem is constructed, correct for interpolation operators 10798 that are not of full rank, which can legitimately happen in the case of non-nested 10799 geometric multigrid. 10800 10801 Input Parameters: 10802 + restrct - restriction operator 10803 . dA - fine grid matrix 10804 . interpolate - interpolation operator 10805 . reuse - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 10806 - fill - expected fill, use PETSC_DEFAULT if you do not have a good estimate 10807 10808 Output Parameters: 10809 . A - the Galerkin coarse matrix 10810 10811 Options Database Key: 10812 . -pc_mg_galerkin <both,pmat,mat,none> 10813 10814 Level: developer 10815 10816 .keywords: MG, multigrid, Galerkin 10817 10818 .seealso: MatPtAP(), MatMatMatMult() 10819 @*/ 10820 PetscErrorCode MatGalerkin(Mat restrct, Mat dA, Mat interpolate, MatReuse reuse, PetscReal fill, Mat *A) 10821 { 10822 PetscErrorCode ierr; 10823 IS zerorows; 10824 Vec diag; 10825 10826 PetscFunctionBegin; 10827 if (reuse == MAT_INPLACE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported"); 10828 /* Construct the coarse grid matrix */ 10829 if (interpolate == restrct) { 10830 ierr = MatPtAP(dA,interpolate,reuse,fill,A);CHKERRQ(ierr); 10831 } else { 10832 ierr = MatMatMatMult(restrct,dA,interpolate,reuse,fill,A);CHKERRQ(ierr); 10833 } 10834 10835 /* If the interpolation matrix is not of full rank, A will have zero rows. 10836 This can legitimately happen in the case of non-nested geometric multigrid. 10837 In that event, we set the rows of the matrix to the rows of the identity, 10838 ignoring the equations (as the RHS will also be zero). */ 10839 10840 ierr = MatFindZeroRows(*A, &zerorows);CHKERRQ(ierr); 10841 10842 if (zerorows != NULL) { /* if there are any zero rows */ 10843 ierr = MatCreateVecs(*A, &diag, NULL);CHKERRQ(ierr); 10844 ierr = MatGetDiagonal(*A, diag);CHKERRQ(ierr); 10845 ierr = VecISSet(diag, zerorows, 1.0);CHKERRQ(ierr); 10846 ierr = MatDiagonalSet(*A, diag, INSERT_VALUES);CHKERRQ(ierr); 10847 ierr = VecDestroy(&diag);CHKERRQ(ierr); 10848 ierr = ISDestroy(&zerorows);CHKERRQ(ierr); 10849 } 10850 PetscFunctionReturn(0); 10851 } 10852 10853 /*@C 10854 MatSetOperation - Allows user to set a matrix operation for any matrix type 10855 10856 Logically Collective on Mat 10857 10858 Input Parameters: 10859 + mat - the matrix 10860 . op - the name of the operation 10861 - f - the function that provides the operation 10862 10863 Level: developer 10864 10865 Usage: 10866 $ extern PetscErrorCode usermult(Mat,Vec,Vec); 10867 $ ierr = MatCreateXXX(comm,...&A); 10868 $ ierr = MatSetOperation(A,MATOP_MULT,(void(*)(void))usermult); 10869 10870 Notes: 10871 See the file include/petscmat.h for a complete list of matrix 10872 operations, which all have the form MATOP_<OPERATION>, where 10873 <OPERATION> is the name (in all capital letters) of the 10874 user interface routine (e.g., MatMult() -> MATOP_MULT). 10875 10876 All user-provided functions (except for MATOP_DESTROY) should have the same calling 10877 sequence as the usual matrix interface routines, since they 10878 are intended to be accessed via the usual matrix interface 10879 routines, e.g., 10880 $ MatMult(Mat,Vec,Vec) -> usermult(Mat,Vec,Vec) 10881 10882 In particular each function MUST return an error code of 0 on success and 10883 nonzero on failure. 10884 10885 This routine is distinct from MatShellSetOperation() in that it can be called on any matrix type. 10886 10887 .keywords: matrix, set, operation 10888 10889 .seealso: MatGetOperation(), MatCreateShell(), MatShellSetContext(), MatShellSetOperation() 10890 @*/ 10891 PetscErrorCode MatSetOperation(Mat mat,MatOperation op,void (*f)(void)) 10892 { 10893 PetscFunctionBegin; 10894 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10895 if (op == MATOP_VIEW && !mat->ops->viewnative && f != (void (*)(void))(mat->ops->view)) { 10896 mat->ops->viewnative = mat->ops->view; 10897 } 10898 (((void(**)(void))mat->ops)[op]) = f; 10899 PetscFunctionReturn(0); 10900 } 10901 10902 /*@C 10903 MatGetOperation - Gets a matrix operation for any matrix type. 10904 10905 Not Collective 10906 10907 Input Parameters: 10908 + mat - the matrix 10909 - op - the name of the operation 10910 10911 Output Parameter: 10912 . f - the function that provides the operation 10913 10914 Level: developer 10915 10916 Usage: 10917 $ PetscErrorCode (*usermult)(Mat,Vec,Vec); 10918 $ ierr = MatGetOperation(A,MATOP_MULT,(void(**)(void))&usermult); 10919 10920 Notes: 10921 See the file include/petscmat.h for a complete list of matrix 10922 operations, which all have the form MATOP_<OPERATION>, where 10923 <OPERATION> is the name (in all capital letters) of the 10924 user interface routine (e.g., MatMult() -> MATOP_MULT). 10925 10926 This routine is distinct from MatShellGetOperation() in that it can be called on any matrix type. 10927 10928 .keywords: matrix, get, operation 10929 10930 .seealso: MatSetOperation(), MatCreateShell(), MatShellGetContext(), MatShellGetOperation() 10931 @*/ 10932 PetscErrorCode MatGetOperation(Mat mat,MatOperation op,void(**f)(void)) 10933 { 10934 PetscFunctionBegin; 10935 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10936 *f = (((void (**)(void))mat->ops)[op]); 10937 PetscFunctionReturn(0); 10938 } 10939 10940 /*@ 10941 MatHasOperation - Determines whether the given matrix supports the particular 10942 operation. 10943 10944 Not Collective 10945 10946 Input Parameters: 10947 + mat - the matrix 10948 - op - the operation, for example, MATOP_GET_DIAGONAL 10949 10950 Output Parameter: 10951 . has - either PETSC_TRUE or PETSC_FALSE 10952 10953 Level: advanced 10954 10955 Notes: 10956 See the file include/petscmat.h for a complete list of matrix 10957 operations, which all have the form MATOP_<OPERATION>, where 10958 <OPERATION> is the name (in all capital letters) of the 10959 user-level routine. E.g., MatNorm() -> MATOP_NORM. 10960 10961 .keywords: matrix, has, operation 10962 10963 .seealso: MatCreateShell() 10964 @*/ 10965 PetscErrorCode MatHasOperation(Mat mat,MatOperation op,PetscBool *has) 10966 { 10967 PetscErrorCode ierr; 10968 10969 PetscFunctionBegin; 10970 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 10971 PetscValidType(mat,1); 10972 PetscValidPointer(has,3); 10973 if (mat->ops->hasoperation) { 10974 ierr = (*mat->ops->hasoperation)(mat,op,has);CHKERRQ(ierr); 10975 } else { 10976 if (((void**)mat->ops)[op]) *has = PETSC_TRUE; 10977 else { 10978 *has = PETSC_FALSE; 10979 if (op == MATOP_CREATE_SUBMATRIX) { 10980 PetscMPIInt size; 10981 10982 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);CHKERRQ(ierr); 10983 if (size == 1) { 10984 ierr = MatHasOperation(mat,MATOP_CREATE_SUBMATRICES,has);CHKERRQ(ierr); 10985 } 10986 } 10987 } 10988 } 10989 PetscFunctionReturn(0); 10990 } 10991 10992 /*@ 10993 MatHasCongruentLayouts - Determines whether the rows and columns layouts 10994 of the matrix are congruent 10995 10996 Collective on mat 10997 10998 Input Parameters: 10999 . mat - the matrix 11000 11001 Output Parameter: 11002 . cong - either PETSC_TRUE or PETSC_FALSE 11003 11004 Level: beginner 11005 11006 Notes: 11007 11008 .keywords: matrix, has 11009 11010 .seealso: MatCreate(), MatSetSizes() 11011 @*/ 11012 PetscErrorCode MatHasCongruentLayouts(Mat mat,PetscBool *cong) 11013 { 11014 PetscErrorCode ierr; 11015 11016 PetscFunctionBegin; 11017 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 11018 PetscValidType(mat,1); 11019 PetscValidPointer(cong,2); 11020 if (!mat->rmap || !mat->cmap) { 11021 *cong = mat->rmap == mat->cmap ? PETSC_TRUE : PETSC_FALSE; 11022 PetscFunctionReturn(0); 11023 } 11024 if (mat->congruentlayouts == PETSC_DECIDE) { /* first time we compare rows and cols layouts */ 11025 ierr = PetscLayoutCompare(mat->rmap,mat->cmap,cong);CHKERRQ(ierr); 11026 if (*cong) mat->congruentlayouts = 1; 11027 else mat->congruentlayouts = 0; 11028 } else *cong = mat->congruentlayouts ? PETSC_TRUE : PETSC_FALSE; 11029 PetscFunctionReturn(0); 11030 } 11031