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