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