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