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