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