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