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