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 /*@ 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 if (A->spptr){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 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 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 PetscValidHeaderSpecific(perm,IS_CLASSID,2); 2876 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 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 PetscFunctionReturn(0); 6389 } 6390 6391 #undef __FUNCT__ 6392 #define __FUNCT__ "MatGetSeqNonzeroStructure" 6393 /*@C 6394 MatGetSeqNonzeroStructure - Extracts the sequential nonzero structure from a matrix. 6395 6396 Collective on Mat 6397 6398 Input Parameters: 6399 . mat - the matrix 6400 6401 Output Parameter: 6402 . matstruct - the sequential matrix with the nonzero structure of mat 6403 6404 Level: intermediate 6405 6406 .seealso: MatDestroySeqNonzeroStructure(), MatGetSubMatrices(), MatDestroyMatrices() 6407 @*/ 6408 PetscErrorCode MatGetSeqNonzeroStructure(Mat mat,Mat *matstruct) 6409 { 6410 PetscErrorCode ierr; 6411 6412 PetscFunctionBegin; 6413 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6414 PetscValidPointer(matstruct,2); 6415 6416 PetscValidType(mat,1); 6417 if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6418 ierr = MatPreallocated(mat);CHKERRQ(ierr); 6419 6420 if (!mat->ops->getseqnonzerostructure) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Not for matrix type %s\n",((PetscObject)mat)->type_name); 6421 ierr = PetscLogEventBegin(MAT_GetSeqNonzeroStructure,mat,0,0,0);CHKERRQ(ierr); 6422 ierr = (*mat->ops->getseqnonzerostructure)(mat,matstruct);CHKERRQ(ierr); 6423 ierr = PetscLogEventEnd(MAT_GetSeqNonzeroStructure,mat,0,0,0);CHKERRQ(ierr); 6424 PetscFunctionReturn(0); 6425 } 6426 6427 #undef __FUNCT__ 6428 #define __FUNCT__ "MatDestroySeqNonzeroStructure" 6429 /*@C 6430 MatDestroySeqNonzeroStructure - Destroys matrix obtained with MatGetSeqNonzeroStructure(). 6431 6432 Collective on Mat 6433 6434 Input Parameters: 6435 . mat - the matrix (note that this is a pointer to the array of matrices, just to match the calling 6436 sequence of MatGetSequentialNonzeroStructure()) 6437 6438 Level: advanced 6439 6440 Notes: Frees not only the matrices, but also the array that contains the matrices 6441 6442 .seealso: MatGetSeqNonzeroStructure() 6443 @*/ 6444 PetscErrorCode MatDestroySeqNonzeroStructure(Mat *mat) 6445 { 6446 PetscErrorCode ierr; 6447 6448 PetscFunctionBegin; 6449 PetscValidPointer(mat,1); 6450 ierr = MatDestroy(*mat);CHKERRQ(ierr); 6451 PetscFunctionReturn(0); 6452 } 6453 6454 #undef __FUNCT__ 6455 #define __FUNCT__ "MatIncreaseOverlap" 6456 /*@ 6457 MatIncreaseOverlap - Given a set of submatrices indicated by index sets, 6458 replaces the index sets by larger ones that represent submatrices with 6459 additional overlap. 6460 6461 Collective on Mat 6462 6463 Input Parameters: 6464 + mat - the matrix 6465 . n - the number of index sets 6466 . is - the array of index sets (these index sets will changed during the call) 6467 - ov - the additional overlap requested 6468 6469 Level: developer 6470 6471 Concepts: overlap 6472 Concepts: ASM^computing overlap 6473 6474 .seealso: MatGetSubMatrices() 6475 @*/ 6476 PetscErrorCode MatIncreaseOverlap(Mat mat,PetscInt n,IS is[],PetscInt ov) 6477 { 6478 PetscErrorCode ierr; 6479 6480 PetscFunctionBegin; 6481 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6482 PetscValidType(mat,1); 6483 if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Must have one or more domains, you have %D",n); 6484 if (n) { 6485 PetscValidPointer(is,3); 6486 PetscValidHeaderSpecific(*is,IS_CLASSID,3); 6487 } 6488 if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 6489 if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 6490 ierr = MatPreallocated(mat);CHKERRQ(ierr); 6491 6492 if (!ov) PetscFunctionReturn(0); 6493 if (!mat->ops->increaseoverlap) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 6494 ierr = PetscLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr); 6495 ierr = (*mat->ops->increaseoverlap)(mat,n,is,ov);CHKERRQ(ierr); 6496 ierr = PetscLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr); 6497 PetscFunctionReturn(0); 6498 } 6499 6500 #undef __FUNCT__ 6501 #define __FUNCT__ "MatGetBlockSize" 6502 /*@ 6503 MatGetBlockSize - Returns the matrix block size; useful especially for the 6504 block row and block diagonal formats. 6505 6506 Not Collective 6507 6508 Input Parameter: 6509 . mat - the matrix 6510 6511 Output Parameter: 6512 . bs - block size 6513 6514 Notes: 6515 Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ 6516 6517 Level: intermediate 6518 6519 Concepts: matrices^block size 6520 6521 .seealso: MatCreateSeqBAIJ(), MatCreateMPIBAIJ() 6522 @*/ 6523 PetscErrorCode MatGetBlockSize(Mat mat,PetscInt *bs) 6524 { 6525 PetscErrorCode ierr; 6526 6527 PetscFunctionBegin; 6528 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6529 PetscValidType(mat,1); 6530 PetscValidIntPointer(bs,2); 6531 ierr = MatPreallocated(mat);CHKERRQ(ierr); 6532 *bs = mat->rmap->bs; 6533 PetscFunctionReturn(0); 6534 } 6535 6536 #undef __FUNCT__ 6537 #define __FUNCT__ "MatSetBlockSize" 6538 /*@ 6539 MatSetBlockSize - Sets the matrix block size; for many matrix types you 6540 cannot use this and MUST set the blocksize when you preallocate the matrix 6541 6542 Logically Collective on Mat 6543 6544 Input Parameters: 6545 + mat - the matrix 6546 - bs - block size 6547 6548 Notes: 6549 For BAIJ matrices, this just checks that the block size agrees with the BAIJ size, 6550 it is not possible to change BAIJ block sizes after preallocation. 6551 6552 Level: intermediate 6553 6554 Concepts: matrices^block size 6555 6556 .seealso: MatCreateSeqBAIJ(), MatCreateMPIBAIJ(), MatGetBlockSize() 6557 @*/ 6558 PetscErrorCode MatSetBlockSize(Mat mat,PetscInt bs) 6559 { 6560 PetscErrorCode ierr; 6561 6562 PetscFunctionBegin; 6563 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6564 PetscValidType(mat,1); 6565 PetscValidLogicalCollectiveInt(mat,bs,2); 6566 ierr = MatPreallocated(mat);CHKERRQ(ierr); 6567 if (bs < 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Block size %D, must be positive",bs); 6568 if (mat->ops->setblocksize) { 6569 ierr = (*mat->ops->setblocksize)(mat,bs);CHKERRQ(ierr); 6570 } else { 6571 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Cannot set the blocksize for matrix type %s",((PetscObject)mat)->type_name); 6572 } 6573 PetscFunctionReturn(0); 6574 } 6575 6576 #undef __FUNCT__ 6577 #define __FUNCT__ "MatGetRowIJ" 6578 /*@C 6579 MatGetRowIJ - Returns the compressed row storage i and j indices for sequential matrices. 6580 6581 Collective on Mat 6582 6583 Input Parameters: 6584 + mat - the matrix 6585 . shift - 0 or 1 indicating we want the indices starting at 0 or 1 6586 . symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 6587 symmetrized 6588 - inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the 6589 inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is 6590 always used. 6591 6592 Output Parameters: 6593 + n - number of rows in the (possibly compressed) matrix 6594 . ia - the row pointers [of length n+1] 6595 . ja - the column indices 6596 - done - indicates if the routine actually worked and returned appropriate ia[] and ja[] arrays; callers 6597 are responsible for handling the case when done == PETSC_FALSE and ia and ja are not set 6598 6599 Level: developer 6600 6601 Notes: You CANNOT change any of the ia[] or ja[] values. 6602 6603 Use MatRestoreRowIJ() when you are finished accessing the ia[] and ja[] values 6604 6605 Fortran Node 6606 6607 In Fortran use 6608 $ PetscInt ia(1), ja(1) 6609 $ PetscOffset iia, jja 6610 $ call MatGetRowIJ(mat,shift,symmetric,inodecompressed,n,ia,iia,ja,jja,done,ierr) 6611 $ 6612 $ or 6613 $ 6614 $ PetscScalar, pointer :: xx_v(:) 6615 $ call MatGetRowIJF90(mat,shift,symmetric,inodecompressed,n,ia,ja,done,ierr) 6616 6617 6618 Acess the ith and jth entries via ia(iia + i) and ja(jja + j) 6619 6620 .seealso: MatGetColumnIJ(), MatRestoreRowIJ(), MatGetArray() 6621 @*/ 6622 PetscErrorCode MatGetRowIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt* ja[],PetscBool *done) 6623 { 6624 PetscErrorCode ierr; 6625 6626 PetscFunctionBegin; 6627 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6628 PetscValidType(mat,1); 6629 PetscValidIntPointer(n,4); 6630 if (ia) PetscValidIntPointer(ia,5); 6631 if (ja) PetscValidIntPointer(ja,6); 6632 PetscValidIntPointer(done,7); 6633 ierr = MatPreallocated(mat);CHKERRQ(ierr); 6634 if (!mat->ops->getrowij) *done = PETSC_FALSE; 6635 else { 6636 *done = PETSC_TRUE; 6637 ierr = PetscLogEventBegin(MAT_GetRowIJ,mat,0,0,0);CHKERRQ(ierr); 6638 ierr = (*mat->ops->getrowij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr); 6639 ierr = PetscLogEventEnd(MAT_GetRowIJ,mat,0,0,0);CHKERRQ(ierr); 6640 } 6641 PetscFunctionReturn(0); 6642 } 6643 6644 #undef __FUNCT__ 6645 #define __FUNCT__ "MatGetColumnIJ" 6646 /*@C 6647 MatGetColumnIJ - Returns the compressed column storage i and j indices for sequential matrices. 6648 6649 Collective on Mat 6650 6651 Input Parameters: 6652 + mat - the matrix 6653 . shift - 1 or zero indicating we want the indices starting at 0 or 1 6654 . symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 6655 symmetrized 6656 - inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the 6657 inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is 6658 always used. 6659 6660 Output Parameters: 6661 + n - number of columns in the (possibly compressed) matrix 6662 . ia - the column pointers 6663 . ja - the row indices 6664 - done - PETSC_TRUE or PETSC_FALSE, indicating whether the values have been returned 6665 6666 Level: developer 6667 6668 .seealso: MatGetRowIJ(), MatRestoreColumnIJ() 6669 @*/ 6670 PetscErrorCode MatGetColumnIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt* ja[],PetscBool *done) 6671 { 6672 PetscErrorCode ierr; 6673 6674 PetscFunctionBegin; 6675 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6676 PetscValidType(mat,1); 6677 PetscValidIntPointer(n,4); 6678 if (ia) PetscValidIntPointer(ia,5); 6679 if (ja) PetscValidIntPointer(ja,6); 6680 PetscValidIntPointer(done,7); 6681 ierr = MatPreallocated(mat);CHKERRQ(ierr); 6682 if (!mat->ops->getcolumnij) *done = PETSC_FALSE; 6683 else { 6684 *done = PETSC_TRUE; 6685 ierr = (*mat->ops->getcolumnij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr); 6686 } 6687 PetscFunctionReturn(0); 6688 } 6689 6690 #undef __FUNCT__ 6691 #define __FUNCT__ "MatRestoreRowIJ" 6692 /*@C 6693 MatRestoreRowIJ - Call after you are completed with the ia,ja indices obtained with 6694 MatGetRowIJ(). 6695 6696 Collective on Mat 6697 6698 Input Parameters: 6699 + mat - the matrix 6700 . shift - 1 or zero indicating we want the indices starting at 0 or 1 6701 . symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 6702 symmetrized 6703 - inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the 6704 inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is 6705 always used. 6706 6707 Output Parameters: 6708 + n - size of (possibly compressed) matrix 6709 . ia - the row pointers 6710 . ja - the column indices 6711 - done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned 6712 6713 Level: developer 6714 6715 .seealso: MatGetRowIJ(), MatRestoreColumnIJ() 6716 @*/ 6717 PetscErrorCode MatRestoreRowIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt* ja[],PetscBool *done) 6718 { 6719 PetscErrorCode ierr; 6720 6721 PetscFunctionBegin; 6722 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6723 PetscValidType(mat,1); 6724 if (ia) PetscValidIntPointer(ia,5); 6725 if (ja) PetscValidIntPointer(ja,6); 6726 PetscValidIntPointer(done,7); 6727 ierr = MatPreallocated(mat);CHKERRQ(ierr); 6728 6729 if (!mat->ops->restorerowij) *done = PETSC_FALSE; 6730 else { 6731 *done = PETSC_TRUE; 6732 ierr = (*mat->ops->restorerowij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr); 6733 } 6734 PetscFunctionReturn(0); 6735 } 6736 6737 #undef __FUNCT__ 6738 #define __FUNCT__ "MatRestoreColumnIJ" 6739 /*@C 6740 MatRestoreColumnIJ - Call after you are completed with the ia,ja indices obtained with 6741 MatGetColumnIJ(). 6742 6743 Collective on Mat 6744 6745 Input Parameters: 6746 + mat - the matrix 6747 . shift - 1 or zero indicating we want the indices starting at 0 or 1 6748 - symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 6749 symmetrized 6750 - inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the 6751 inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is 6752 always used. 6753 6754 Output Parameters: 6755 + n - size of (possibly compressed) matrix 6756 . ia - the column pointers 6757 . ja - the row indices 6758 - done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned 6759 6760 Level: developer 6761 6762 .seealso: MatGetColumnIJ(), MatRestoreRowIJ() 6763 @*/ 6764 PetscErrorCode MatRestoreColumnIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt* ja[],PetscBool *done) 6765 { 6766 PetscErrorCode ierr; 6767 6768 PetscFunctionBegin; 6769 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6770 PetscValidType(mat,1); 6771 if (ia) PetscValidIntPointer(ia,5); 6772 if (ja) PetscValidIntPointer(ja,6); 6773 PetscValidIntPointer(done,7); 6774 ierr = MatPreallocated(mat);CHKERRQ(ierr); 6775 6776 if (!mat->ops->restorecolumnij) *done = PETSC_FALSE; 6777 else { 6778 *done = PETSC_TRUE; 6779 ierr = (*mat->ops->restorecolumnij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr); 6780 } 6781 PetscFunctionReturn(0); 6782 } 6783 6784 #undef __FUNCT__ 6785 #define __FUNCT__ "MatColoringPatch" 6786 /*@C 6787 MatColoringPatch -Used inside matrix coloring routines that 6788 use MatGetRowIJ() and/or MatGetColumnIJ(). 6789 6790 Collective on Mat 6791 6792 Input Parameters: 6793 + mat - the matrix 6794 . ncolors - max color value 6795 . n - number of entries in colorarray 6796 - colorarray - array indicating color for each column 6797 6798 Output Parameters: 6799 . iscoloring - coloring generated using colorarray information 6800 6801 Level: developer 6802 6803 .seealso: MatGetRowIJ(), MatGetColumnIJ() 6804 6805 @*/ 6806 PetscErrorCode MatColoringPatch(Mat mat,PetscInt ncolors,PetscInt n,ISColoringValue colorarray[],ISColoring *iscoloring) 6807 { 6808 PetscErrorCode ierr; 6809 6810 PetscFunctionBegin; 6811 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6812 PetscValidType(mat,1); 6813 PetscValidIntPointer(colorarray,4); 6814 PetscValidPointer(iscoloring,5); 6815 ierr = MatPreallocated(mat);CHKERRQ(ierr); 6816 6817 if (!mat->ops->coloringpatch){ 6818 ierr = ISColoringCreate(((PetscObject)mat)->comm,ncolors,n,colorarray,iscoloring);CHKERRQ(ierr); 6819 } else { 6820 ierr = (*mat->ops->coloringpatch)(mat,ncolors,n,colorarray,iscoloring);CHKERRQ(ierr); 6821 } 6822 PetscFunctionReturn(0); 6823 } 6824 6825 6826 #undef __FUNCT__ 6827 #define __FUNCT__ "MatSetUnfactored" 6828 /*@ 6829 MatSetUnfactored - Resets a factored matrix to be treated as unfactored. 6830 6831 Logically Collective on Mat 6832 6833 Input Parameter: 6834 . mat - the factored matrix to be reset 6835 6836 Notes: 6837 This routine should be used only with factored matrices formed by in-place 6838 factorization via ILU(0) (or by in-place LU factorization for the MATSEQDENSE 6839 format). This option can save memory, for example, when solving nonlinear 6840 systems with a matrix-free Newton-Krylov method and a matrix-based, in-place 6841 ILU(0) preconditioner. 6842 6843 Note that one can specify in-place ILU(0) factorization by calling 6844 .vb 6845 PCType(pc,PCILU); 6846 PCFactorSeUseInPlace(pc); 6847 .ve 6848 or by using the options -pc_type ilu -pc_factor_in_place 6849 6850 In-place factorization ILU(0) can also be used as a local 6851 solver for the blocks within the block Jacobi or additive Schwarz 6852 methods (runtime option: -sub_pc_factor_in_place). See the discussion 6853 of these preconditioners in the <a href="../../docs/manual.pdf#ch_pc">PC chapter of the users manual</a> for details on setting 6854 local solver options. 6855 6856 Most users should employ the simplified KSP interface for linear solvers 6857 instead of working directly with matrix algebra routines such as this. 6858 See, e.g., KSPCreate(). 6859 6860 Level: developer 6861 6862 .seealso: PCFactorSetUseInPlace() 6863 6864 Concepts: matrices^unfactored 6865 6866 @*/ 6867 PetscErrorCode MatSetUnfactored(Mat mat) 6868 { 6869 PetscErrorCode ierr; 6870 6871 PetscFunctionBegin; 6872 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 6873 PetscValidType(mat,1); 6874 ierr = MatPreallocated(mat);CHKERRQ(ierr); 6875 mat->factortype = MAT_FACTOR_NONE; 6876 if (!mat->ops->setunfactored) PetscFunctionReturn(0); 6877 ierr = (*mat->ops->setunfactored)(mat);CHKERRQ(ierr); 6878 PetscFunctionReturn(0); 6879 } 6880 6881 /*MC 6882 MatGetArrayF90 - Accesses a matrix array from Fortran90. 6883 6884 Synopsis: 6885 MatGetArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr) 6886 6887 Not collective 6888 6889 Input Parameter: 6890 . x - matrix 6891 6892 Output Parameters: 6893 + xx_v - the Fortran90 pointer to the array 6894 - ierr - error code 6895 6896 Example of Usage: 6897 .vb 6898 PetscScalar, pointer xx_v(:) 6899 .... 6900 call MatGetArrayF90(x,xx_v,ierr) 6901 a = xx_v(3) 6902 call MatRestoreArrayF90(x,xx_v,ierr) 6903 .ve 6904 6905 Notes: 6906 Not yet supported for all F90 compilers 6907 6908 Level: advanced 6909 6910 .seealso: MatRestoreArrayF90(), MatGetArray(), MatRestoreArray() 6911 6912 Concepts: matrices^accessing array 6913 6914 M*/ 6915 6916 /*MC 6917 MatRestoreArrayF90 - Restores a matrix array that has been 6918 accessed with MatGetArrayF90(). 6919 6920 Synopsis: 6921 MatRestoreArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr) 6922 6923 Not collective 6924 6925 Input Parameters: 6926 + x - matrix 6927 - xx_v - the Fortran90 pointer to the array 6928 6929 Output Parameter: 6930 . ierr - error code 6931 6932 Example of Usage: 6933 .vb 6934 PetscScalar, pointer xx_v(:) 6935 .... 6936 call MatGetArrayF90(x,xx_v,ierr) 6937 a = xx_v(3) 6938 call MatRestoreArrayF90(x,xx_v,ierr) 6939 .ve 6940 6941 Notes: 6942 Not yet supported for all F90 compilers 6943 6944 Level: advanced 6945 6946 .seealso: MatGetArrayF90(), MatGetArray(), MatRestoreArray() 6947 6948 M*/ 6949 6950 6951 #undef __FUNCT__ 6952 #define __FUNCT__ "MatGetSubMatrix" 6953 /*@ 6954 MatGetSubMatrix - Gets a single submatrix on the same number of processors 6955 as the original matrix. 6956 6957 Collective on Mat 6958 6959 Input Parameters: 6960 + mat - the original matrix 6961 . isrow - parallel IS containing the rows this processor should obtain 6962 . 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. 6963 - cll - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 6964 6965 Output Parameter: 6966 . newmat - the new submatrix, of the same type as the old 6967 6968 Level: advanced 6969 6970 Notes: 6971 The submatrix will be able to be multiplied with vectors using the same layout as iscol. 6972 6973 The rows in isrow will be sorted into the same order as the original matrix on each process. 6974 6975 The first time this is called you should use a cll of MAT_INITIAL_MATRIX, 6976 the MatGetSubMatrix() routine will create the newmat for you. Any additional calls 6977 to this routine with a mat of the same nonzero structure and with a call of MAT_REUSE_MATRIX 6978 will reuse the matrix generated the first time. You should call MatDestroy() on newmat when 6979 you are finished using it. 6980 6981 The communicator of the newly obtained matrix is ALWAYS the same as the communicator of 6982 the input matrix. 6983 6984 If iscol is PETSC_NULL then all columns are obtained (not supported in Fortran). 6985 6986 Example usage: 6987 Consider the following 8x8 matrix with 34 non-zero values, that is 6988 assembled across 3 processors. Let's assume that proc0 owns 3 rows, 6989 proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown 6990 as follows: 6991 6992 .vb 6993 1 2 0 | 0 3 0 | 0 4 6994 Proc0 0 5 6 | 7 0 0 | 8 0 6995 9 0 10 | 11 0 0 | 12 0 6996 ------------------------------------- 6997 13 0 14 | 15 16 17 | 0 0 6998 Proc1 0 18 0 | 19 20 21 | 0 0 6999 0 0 0 | 22 23 0 | 24 0 7000 ------------------------------------- 7001 Proc2 25 26 27 | 0 0 28 | 29 0 7002 30 0 0 | 31 32 33 | 0 34 7003 .ve 7004 7005 Suppose isrow = [0 1 | 4 | 6 7] and iscol = [1 2 | 3 4 5 | 6]. The resulting submatrix is 7006 7007 .vb 7008 2 0 | 0 3 0 | 0 7009 Proc0 5 6 | 7 0 0 | 8 7010 ------------------------------- 7011 Proc1 18 0 | 19 20 21 | 0 7012 ------------------------------- 7013 Proc2 26 27 | 0 0 28 | 29 7014 0 0 | 31 32 33 | 0 7015 .ve 7016 7017 7018 Concepts: matrices^submatrices 7019 7020 .seealso: MatGetSubMatrices() 7021 @*/ 7022 PetscErrorCode MatGetSubMatrix(Mat mat,IS isrow,IS iscol,MatReuse cll,Mat *newmat) 7023 { 7024 PetscErrorCode ierr; 7025 PetscMPIInt size; 7026 Mat *local; 7027 IS iscoltmp; 7028 7029 PetscFunctionBegin; 7030 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7031 PetscValidHeaderSpecific(isrow,IS_CLASSID,2); 7032 if (iscol) PetscValidHeaderSpecific(iscol,IS_CLASSID,3); 7033 PetscValidPointer(newmat,5); 7034 if (cll == MAT_REUSE_MATRIX) PetscValidHeaderSpecific(*newmat,MAT_CLASSID,5); 7035 PetscValidType(mat,1); 7036 if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7037 ierr = MatPreallocated(mat);CHKERRQ(ierr); 7038 ierr = MPI_Comm_size(((PetscObject)mat)->comm,&size);CHKERRQ(ierr); 7039 7040 if (!iscol) { 7041 ierr = ISCreateStride(((PetscObject)mat)->comm,mat->cmap->n,mat->cmap->rstart,1,&iscoltmp);CHKERRQ(ierr); 7042 } else { 7043 iscoltmp = iscol; 7044 } 7045 7046 /* if original matrix is on just one processor then use submatrix generated */ 7047 if (mat->ops->getsubmatrices && !mat->ops->getsubmatrix && size == 1 && cll == MAT_REUSE_MATRIX) { 7048 ierr = MatGetSubMatrices(mat,1,&isrow,&iscoltmp,MAT_REUSE_MATRIX,&newmat);CHKERRQ(ierr); 7049 if (!iscol) {ierr = ISDestroy(iscoltmp);CHKERRQ(ierr);} 7050 PetscFunctionReturn(0); 7051 } else if (mat->ops->getsubmatrices && !mat->ops->getsubmatrix && size == 1) { 7052 ierr = MatGetSubMatrices(mat,1,&isrow,&iscoltmp,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr); 7053 *newmat = *local; 7054 ierr = PetscFree(local);CHKERRQ(ierr); 7055 if (!iscol) {ierr = ISDestroy(iscoltmp);CHKERRQ(ierr);} 7056 PetscFunctionReturn(0); 7057 } else if (!mat->ops->getsubmatrix) { 7058 /* Create a new matrix type that implements the operation using the full matrix */ 7059 switch (cll) { 7060 case MAT_INITIAL_MATRIX: 7061 ierr = MatCreateSubMatrix(mat,isrow,iscoltmp,newmat);CHKERRQ(ierr); 7062 break; 7063 case MAT_REUSE_MATRIX: 7064 ierr = MatSubMatrixUpdate(*newmat,mat,isrow,iscoltmp);CHKERRQ(ierr); 7065 break; 7066 default: SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Invalid MatReuse, must be either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX"); 7067 } 7068 if (!iscol) {ierr = ISDestroy(iscoltmp);CHKERRQ(ierr);} 7069 PetscFunctionReturn(0); 7070 } 7071 7072 if (!mat->ops->getsubmatrix) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 7073 ierr = (*mat->ops->getsubmatrix)(mat,isrow,iscoltmp,cll,newmat);CHKERRQ(ierr); 7074 if (!iscol) {ierr = ISDestroy(iscoltmp);CHKERRQ(ierr);} 7075 ierr = PetscObjectStateIncrease((PetscObject)*newmat);CHKERRQ(ierr); 7076 PetscFunctionReturn(0); 7077 } 7078 7079 #undef __FUNCT__ 7080 #define __FUNCT__ "MatStashSetInitialSize" 7081 /*@ 7082 MatStashSetInitialSize - sets the sizes of the matrix stash, that is 7083 used during the assembly process to store values that belong to 7084 other processors. 7085 7086 Not Collective 7087 7088 Input Parameters: 7089 + mat - the matrix 7090 . size - the initial size of the stash. 7091 - bsize - the initial size of the block-stash(if used). 7092 7093 Options Database Keys: 7094 + -matstash_initial_size <size> or <size0,size1,...sizep-1> 7095 - -matstash_block_initial_size <bsize> or <bsize0,bsize1,...bsizep-1> 7096 7097 Level: intermediate 7098 7099 Notes: 7100 The block-stash is used for values set with MatSetValuesBlocked() while 7101 the stash is used for values set with MatSetValues() 7102 7103 Run with the option -info and look for output of the form 7104 MatAssemblyBegin_MPIXXX:Stash has MM entries, uses nn mallocs. 7105 to determine the appropriate value, MM, to use for size and 7106 MatAssemblyBegin_MPIXXX:Block-Stash has BMM entries, uses nn mallocs. 7107 to determine the value, BMM to use for bsize 7108 7109 Concepts: stash^setting matrix size 7110 Concepts: matrices^stash 7111 7112 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), Mat, MatStashGetInfo() 7113 7114 @*/ 7115 PetscErrorCode MatStashSetInitialSize(Mat mat,PetscInt size, PetscInt bsize) 7116 { 7117 PetscErrorCode ierr; 7118 7119 PetscFunctionBegin; 7120 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7121 PetscValidType(mat,1); 7122 ierr = MatStashSetInitialSize_Private(&mat->stash,size);CHKERRQ(ierr); 7123 ierr = MatStashSetInitialSize_Private(&mat->bstash,bsize);CHKERRQ(ierr); 7124 PetscFunctionReturn(0); 7125 } 7126 7127 #undef __FUNCT__ 7128 #define __FUNCT__ "MatInterpolateAdd" 7129 /*@ 7130 MatInterpolateAdd - w = y + A*x or A'*x depending on the shape of 7131 the matrix 7132 7133 Neighbor-wise Collective on Mat 7134 7135 Input Parameters: 7136 + mat - the matrix 7137 . x,y - the vectors 7138 - w - where the result is stored 7139 7140 Level: intermediate 7141 7142 Notes: 7143 w may be the same vector as y. 7144 7145 This allows one to use either the restriction or interpolation (its transpose) 7146 matrix to do the interpolation 7147 7148 Concepts: interpolation 7149 7150 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict() 7151 7152 @*/ 7153 PetscErrorCode MatInterpolateAdd(Mat A,Vec x,Vec y,Vec w) 7154 { 7155 PetscErrorCode ierr; 7156 PetscInt M,N,Ny; 7157 7158 PetscFunctionBegin; 7159 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 7160 PetscValidHeaderSpecific(x,VEC_CLASSID,2); 7161 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 7162 PetscValidHeaderSpecific(w,VEC_CLASSID,4); 7163 PetscValidType(A,1); 7164 ierr = MatPreallocated(A);CHKERRQ(ierr); 7165 ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr); 7166 ierr = VecGetSize(y,&Ny);CHKERRQ(ierr); 7167 if (M == Ny) { 7168 ierr = MatMultAdd(A,x,y,w);CHKERRQ(ierr); 7169 } else { 7170 ierr = MatMultTransposeAdd(A,x,y,w);CHKERRQ(ierr); 7171 } 7172 PetscFunctionReturn(0); 7173 } 7174 7175 #undef __FUNCT__ 7176 #define __FUNCT__ "MatInterpolate" 7177 /*@ 7178 MatInterpolate - y = A*x or A'*x depending on the shape of 7179 the matrix 7180 7181 Neighbor-wise Collective on Mat 7182 7183 Input Parameters: 7184 + mat - the matrix 7185 - x,y - the vectors 7186 7187 Level: intermediate 7188 7189 Notes: 7190 This allows one to use either the restriction or interpolation (its transpose) 7191 matrix to do the interpolation 7192 7193 Concepts: matrices^interpolation 7194 7195 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict() 7196 7197 @*/ 7198 PetscErrorCode MatInterpolate(Mat A,Vec x,Vec y) 7199 { 7200 PetscErrorCode ierr; 7201 PetscInt M,N,Ny; 7202 7203 PetscFunctionBegin; 7204 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 7205 PetscValidHeaderSpecific(x,VEC_CLASSID,2); 7206 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 7207 PetscValidType(A,1); 7208 ierr = MatPreallocated(A);CHKERRQ(ierr); 7209 ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr); 7210 ierr = VecGetSize(y,&Ny);CHKERRQ(ierr); 7211 if (M == Ny) { 7212 ierr = MatMult(A,x,y);CHKERRQ(ierr); 7213 } else { 7214 ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr); 7215 } 7216 PetscFunctionReturn(0); 7217 } 7218 7219 #undef __FUNCT__ 7220 #define __FUNCT__ "MatRestrict" 7221 /*@ 7222 MatRestrict - y = A*x or A'*x 7223 7224 Neighbor-wise Collective on Mat 7225 7226 Input Parameters: 7227 + mat - the matrix 7228 - x,y - the vectors 7229 7230 Level: intermediate 7231 7232 Notes: 7233 This allows one to use either the restriction or interpolation (its transpose) 7234 matrix to do the restriction 7235 7236 Concepts: matrices^restriction 7237 7238 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatInterpolate() 7239 7240 @*/ 7241 PetscErrorCode MatRestrict(Mat A,Vec x,Vec y) 7242 { 7243 PetscErrorCode ierr; 7244 PetscInt M,N,Ny; 7245 7246 PetscFunctionBegin; 7247 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 7248 PetscValidHeaderSpecific(x,VEC_CLASSID,2); 7249 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 7250 PetscValidType(A,1); 7251 ierr = MatPreallocated(A);CHKERRQ(ierr); 7252 7253 ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr); 7254 ierr = VecGetSize(y,&Ny);CHKERRQ(ierr); 7255 if (M == Ny) { 7256 ierr = MatMult(A,x,y);CHKERRQ(ierr); 7257 } else { 7258 ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr); 7259 } 7260 PetscFunctionReturn(0); 7261 } 7262 7263 #undef __FUNCT__ 7264 #define __FUNCT__ "MatNullSpaceAttach" 7265 /*@ 7266 MatNullSpaceAttach - attaches a null space to a matrix. 7267 This null space will be removed from the resulting vector whenever 7268 MatMult() is called 7269 7270 Logically Collective on Mat and MatNullSpace 7271 7272 Input Parameters: 7273 + mat - the matrix 7274 - nullsp - the null space object 7275 7276 Level: developer 7277 7278 Notes: 7279 Overwrites any previous null space that may have been attached 7280 7281 Concepts: null space^attaching to matrix 7282 7283 .seealso: MatCreate(), MatNullSpaceCreate() 7284 @*/ 7285 PetscErrorCode MatNullSpaceAttach(Mat mat,MatNullSpace nullsp) 7286 { 7287 PetscErrorCode ierr; 7288 7289 PetscFunctionBegin; 7290 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7291 PetscValidType(mat,1); 7292 PetscValidHeaderSpecific(nullsp,MAT_NULLSPACE_CLASSID,2); 7293 ierr = MatPreallocated(mat);CHKERRQ(ierr); 7294 ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr); 7295 if (mat->nullsp) { ierr = MatNullSpaceDestroy(mat->nullsp);CHKERRQ(ierr); } 7296 mat->nullsp = nullsp; 7297 PetscFunctionReturn(0); 7298 } 7299 7300 #undef __FUNCT__ 7301 #define __FUNCT__ "MatICCFactor" 7302 /*@C 7303 MatICCFactor - Performs in-place incomplete Cholesky factorization of matrix. 7304 7305 Collective on Mat 7306 7307 Input Parameters: 7308 + mat - the matrix 7309 . row - row/column permutation 7310 . fill - expected fill factor >= 1.0 7311 - level - level of fill, for ICC(k) 7312 7313 Notes: 7314 Probably really in-place only when level of fill is zero, otherwise allocates 7315 new space to store factored matrix and deletes previous memory. 7316 7317 Most users should employ the simplified KSP interface for linear solvers 7318 instead of working directly with matrix algebra routines such as this. 7319 See, e.g., KSPCreate(). 7320 7321 Level: developer 7322 7323 Concepts: matrices^incomplete Cholesky factorization 7324 Concepts: Cholesky factorization 7325 7326 .seealso: MatICCFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor() 7327 7328 Developer Note: fortran interface is not autogenerated as the f90 7329 interface defintion cannot be generated correctly [due to MatFactorInfo] 7330 7331 @*/ 7332 PetscErrorCode MatICCFactor(Mat mat,IS row,const MatFactorInfo* info) 7333 { 7334 PetscErrorCode ierr; 7335 7336 PetscFunctionBegin; 7337 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7338 PetscValidType(mat,1); 7339 if (row) PetscValidHeaderSpecific(row,IS_CLASSID,2); 7340 PetscValidPointer(info,3); 7341 if (mat->rmap->N != mat->cmap->N) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONG,"matrix must be square"); 7342 if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 7343 if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7344 if (!mat->ops->iccfactor) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 7345 ierr = MatPreallocated(mat);CHKERRQ(ierr); 7346 ierr = (*mat->ops->iccfactor)(mat,row,info);CHKERRQ(ierr); 7347 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 7348 PetscFunctionReturn(0); 7349 } 7350 7351 #undef __FUNCT__ 7352 #define __FUNCT__ "MatSetValuesAdic" 7353 /*@ 7354 MatSetValuesAdic - Sets values computed with ADIC automatic differentiation into a matrix. 7355 7356 Not Collective 7357 7358 Input Parameters: 7359 + mat - the matrix 7360 - v - the values compute with ADIC 7361 7362 Level: developer 7363 7364 Notes: 7365 Must call MatSetColoring() before using this routine. Also this matrix must already 7366 have its nonzero pattern determined. 7367 7368 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(), 7369 MatSetValues(), MatSetColoring(), MatSetValuesAdifor() 7370 @*/ 7371 PetscErrorCode MatSetValuesAdic(Mat mat,void *v) 7372 { 7373 PetscErrorCode ierr; 7374 7375 PetscFunctionBegin; 7376 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7377 PetscValidType(mat,1); 7378 PetscValidPointer(mat,2); 7379 7380 if (!mat->assembled) { 7381 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled"); 7382 } 7383 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 7384 if (!mat->ops->setvaluesadic) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 7385 ierr = (*mat->ops->setvaluesadic)(mat,v);CHKERRQ(ierr); 7386 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 7387 ierr = MatView_Private(mat);CHKERRQ(ierr); 7388 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 7389 PetscFunctionReturn(0); 7390 } 7391 7392 7393 #undef __FUNCT__ 7394 #define __FUNCT__ "MatSetColoring" 7395 /*@ 7396 MatSetColoring - Sets a coloring used by calls to MatSetValuesAdic() 7397 7398 Not Collective 7399 7400 Input Parameters: 7401 + mat - the matrix 7402 - coloring - the coloring 7403 7404 Level: developer 7405 7406 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(), 7407 MatSetValues(), MatSetValuesAdic() 7408 @*/ 7409 PetscErrorCode MatSetColoring(Mat mat,ISColoring coloring) 7410 { 7411 PetscErrorCode ierr; 7412 7413 PetscFunctionBegin; 7414 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7415 PetscValidType(mat,1); 7416 PetscValidPointer(coloring,2); 7417 7418 if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled"); 7419 if (!mat->ops->setcoloring) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 7420 ierr = (*mat->ops->setcoloring)(mat,coloring);CHKERRQ(ierr); 7421 PetscFunctionReturn(0); 7422 } 7423 7424 #undef __FUNCT__ 7425 #define __FUNCT__ "MatSetValuesAdifor" 7426 /*@ 7427 MatSetValuesAdifor - Sets values computed with automatic differentiation into a matrix. 7428 7429 Not Collective 7430 7431 Input Parameters: 7432 + mat - the matrix 7433 . nl - leading dimension of v 7434 - v - the values compute with ADIFOR 7435 7436 Level: developer 7437 7438 Notes: 7439 Must call MatSetColoring() before using this routine. Also this matrix must already 7440 have its nonzero pattern determined. 7441 7442 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(), 7443 MatSetValues(), MatSetColoring() 7444 @*/ 7445 PetscErrorCode MatSetValuesAdifor(Mat mat,PetscInt nl,void *v) 7446 { 7447 PetscErrorCode ierr; 7448 7449 PetscFunctionBegin; 7450 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7451 PetscValidType(mat,1); 7452 PetscValidPointer(v,3); 7453 7454 if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled"); 7455 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 7456 if (!mat->ops->setvaluesadifor) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 7457 ierr = (*mat->ops->setvaluesadifor)(mat,nl,v);CHKERRQ(ierr); 7458 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 7459 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 7460 PetscFunctionReturn(0); 7461 } 7462 7463 #undef __FUNCT__ 7464 #define __FUNCT__ "MatDiagonalScaleLocal" 7465 /*@ 7466 MatDiagonalScaleLocal - Scales columns of a matrix given the scaling values including the 7467 ghosted ones. 7468 7469 Not Collective 7470 7471 Input Parameters: 7472 + mat - the matrix 7473 - diag = the diagonal values, including ghost ones 7474 7475 Level: developer 7476 7477 Notes: Works only for MPIAIJ and MPIBAIJ matrices 7478 7479 .seealso: MatDiagonalScale() 7480 @*/ 7481 PetscErrorCode MatDiagonalScaleLocal(Mat mat,Vec diag) 7482 { 7483 PetscErrorCode ierr; 7484 PetscMPIInt size; 7485 7486 PetscFunctionBegin; 7487 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7488 PetscValidHeaderSpecific(diag,VEC_CLASSID,2); 7489 PetscValidType(mat,1); 7490 7491 if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled"); 7492 ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 7493 ierr = MPI_Comm_size(((PetscObject)mat)->comm,&size);CHKERRQ(ierr); 7494 if (size == 1) { 7495 PetscInt n,m; 7496 ierr = VecGetSize(diag,&n);CHKERRQ(ierr); 7497 ierr = MatGetSize(mat,0,&m);CHKERRQ(ierr); 7498 if (m == n) { 7499 ierr = MatDiagonalScale(mat,0,diag);CHKERRQ(ierr); 7500 } else { 7501 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only supported for sequential matrices when no ghost points/periodic conditions"); 7502 } 7503 } else { 7504 ierr = PetscUseMethod(mat,"MatDiagonalScaleLocal_C",(Mat,Vec),(mat,diag));CHKERRQ(ierr); 7505 } 7506 ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 7507 ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr); 7508 PetscFunctionReturn(0); 7509 } 7510 7511 #undef __FUNCT__ 7512 #define __FUNCT__ "MatGetInertia" 7513 /*@ 7514 MatGetInertia - Gets the inertia from a factored matrix 7515 7516 Collective on Mat 7517 7518 Input Parameter: 7519 . mat - the matrix 7520 7521 Output Parameters: 7522 + nneg - number of negative eigenvalues 7523 . nzero - number of zero eigenvalues 7524 - npos - number of positive eigenvalues 7525 7526 Level: advanced 7527 7528 Notes: Matrix must have been factored by MatCholeskyFactor() 7529 7530 7531 @*/ 7532 PetscErrorCode MatGetInertia(Mat mat,PetscInt *nneg,PetscInt *nzero,PetscInt *npos) 7533 { 7534 PetscErrorCode ierr; 7535 7536 PetscFunctionBegin; 7537 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7538 PetscValidType(mat,1); 7539 if (!mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 7540 if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Numeric factor mat is not assembled"); 7541 if (!mat->ops->getinertia) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 7542 ierr = (*mat->ops->getinertia)(mat,nneg,nzero,npos);CHKERRQ(ierr); 7543 PetscFunctionReturn(0); 7544 } 7545 7546 /* ----------------------------------------------------------------*/ 7547 #undef __FUNCT__ 7548 #define __FUNCT__ "MatSolves" 7549 /*@C 7550 MatSolves - Solves A x = b, given a factored matrix, for a collection of vectors 7551 7552 Neighbor-wise Collective on Mat and Vecs 7553 7554 Input Parameters: 7555 + mat - the factored matrix 7556 - b - the right-hand-side vectors 7557 7558 Output Parameter: 7559 . x - the result vectors 7560 7561 Notes: 7562 The vectors b and x cannot be the same. I.e., one cannot 7563 call MatSolves(A,x,x). 7564 7565 Notes: 7566 Most users should employ the simplified KSP interface for linear solvers 7567 instead of working directly with matrix algebra routines such as this. 7568 See, e.g., KSPCreate(). 7569 7570 Level: developer 7571 7572 Concepts: matrices^triangular solves 7573 7574 .seealso: MatSolveAdd(), MatSolveTranspose(), MatSolveTransposeAdd(), MatSolve() 7575 @*/ 7576 PetscErrorCode MatSolves(Mat mat,Vecs b,Vecs x) 7577 { 7578 PetscErrorCode ierr; 7579 7580 PetscFunctionBegin; 7581 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7582 PetscValidType(mat,1); 7583 if (x == b) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 7584 if (!mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 7585 if (!mat->rmap->N && !mat->cmap->N) PetscFunctionReturn(0); 7586 7587 if (!mat->ops->solves) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 7588 ierr = MatPreallocated(mat);CHKERRQ(ierr); 7589 ierr = PetscLogEventBegin(MAT_Solves,mat,0,0,0);CHKERRQ(ierr); 7590 ierr = (*mat->ops->solves)(mat,b,x);CHKERRQ(ierr); 7591 ierr = PetscLogEventEnd(MAT_Solves,mat,0,0,0);CHKERRQ(ierr); 7592 PetscFunctionReturn(0); 7593 } 7594 7595 #undef __FUNCT__ 7596 #define __FUNCT__ "MatIsSymmetric" 7597 /*@ 7598 MatIsSymmetric - Test whether a matrix is symmetric 7599 7600 Collective on Mat 7601 7602 Input Parameter: 7603 + A - the matrix to test 7604 - tol - difference between value and its transpose less than this amount counts as equal (use 0.0 for exact transpose) 7605 7606 Output Parameters: 7607 . flg - the result 7608 7609 Level: intermediate 7610 7611 Concepts: matrix^symmetry 7612 7613 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetricKnown() 7614 @*/ 7615 PetscErrorCode MatIsSymmetric(Mat A,PetscReal tol,PetscBool *flg) 7616 { 7617 PetscErrorCode ierr; 7618 7619 PetscFunctionBegin; 7620 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 7621 PetscValidPointer(flg,2); 7622 7623 if (!A->symmetric_set) { 7624 if (!A->ops->issymmetric) { 7625 const MatType mattype; 7626 ierr = MatGetType(A,&mattype);CHKERRQ(ierr); 7627 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for symmetric",mattype); 7628 } 7629 ierr = (*A->ops->issymmetric)(A,tol,flg);CHKERRQ(ierr); 7630 if (!tol) { 7631 A->symmetric_set = PETSC_TRUE; 7632 A->symmetric = *flg; 7633 if (A->symmetric) { 7634 A->structurally_symmetric_set = PETSC_TRUE; 7635 A->structurally_symmetric = PETSC_TRUE; 7636 } 7637 } 7638 } else if (A->symmetric) { 7639 *flg = PETSC_TRUE; 7640 } else if (!tol) { 7641 *flg = PETSC_FALSE; 7642 } else { 7643 if (!A->ops->issymmetric) { 7644 const MatType mattype; 7645 ierr = MatGetType(A,&mattype);CHKERRQ(ierr); 7646 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for symmetric",mattype); 7647 } 7648 ierr = (*A->ops->issymmetric)(A,tol,flg);CHKERRQ(ierr); 7649 } 7650 PetscFunctionReturn(0); 7651 } 7652 7653 #undef __FUNCT__ 7654 #define __FUNCT__ "MatIsHermitian" 7655 /*@ 7656 MatIsHermitian - Test whether a matrix is Hermitian 7657 7658 Collective on Mat 7659 7660 Input Parameter: 7661 + A - the matrix to test 7662 - tol - difference between value and its transpose less than this amount counts as equal (use 0.0 for exact Hermitian) 7663 7664 Output Parameters: 7665 . flg - the result 7666 7667 Level: intermediate 7668 7669 Concepts: matrix^symmetry 7670 7671 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetricKnown() 7672 @*/ 7673 PetscErrorCode MatIsHermitian(Mat A,PetscReal tol,PetscBool *flg) 7674 { 7675 PetscErrorCode ierr; 7676 7677 PetscFunctionBegin; 7678 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 7679 PetscValidPointer(flg,2); 7680 7681 if (!A->hermitian_set) { 7682 if (!A->ops->ishermitian) { 7683 const MatType mattype; 7684 ierr = MatGetType(A,&mattype);CHKERRQ(ierr); 7685 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for hermitian",mattype); 7686 } 7687 ierr = (*A->ops->ishermitian)(A,tol,flg);CHKERRQ(ierr); 7688 if (!tol) { 7689 A->hermitian_set = PETSC_TRUE; 7690 A->hermitian = *flg; 7691 if (A->hermitian) { 7692 A->structurally_symmetric_set = PETSC_TRUE; 7693 A->structurally_symmetric = PETSC_TRUE; 7694 } 7695 } 7696 } else if (A->hermitian) { 7697 *flg = PETSC_TRUE; 7698 } else if (!tol) { 7699 *flg = PETSC_FALSE; 7700 } else { 7701 if (!A->ops->ishermitian) { 7702 const MatType mattype; 7703 ierr = MatGetType(A,&mattype);CHKERRQ(ierr); 7704 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for hermitian",mattype); 7705 } 7706 ierr = (*A->ops->ishermitian)(A,tol,flg);CHKERRQ(ierr); 7707 } 7708 PetscFunctionReturn(0); 7709 } 7710 7711 #undef __FUNCT__ 7712 #define __FUNCT__ "MatIsSymmetricKnown" 7713 /*@ 7714 MatIsSymmetricKnown - Checks the flag on the matrix to see if it is symmetric. 7715 7716 Not Collective 7717 7718 Input Parameter: 7719 . A - the matrix to check 7720 7721 Output Parameters: 7722 + set - if the symmetric flag is set (this tells you if the next flag is valid) 7723 - flg - the result 7724 7725 Level: advanced 7726 7727 Concepts: matrix^symmetry 7728 7729 Note: Does not check the matrix values directly, so this may return unknown (set = PETSC_FALSE). Use MatIsSymmetric() 7730 if you want it explicitly checked 7731 7732 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetric() 7733 @*/ 7734 PetscErrorCode MatIsSymmetricKnown(Mat A,PetscBool *set,PetscBool *flg) 7735 { 7736 PetscFunctionBegin; 7737 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 7738 PetscValidPointer(set,2); 7739 PetscValidPointer(flg,3); 7740 if (A->symmetric_set) { 7741 *set = PETSC_TRUE; 7742 *flg = A->symmetric; 7743 } else { 7744 *set = PETSC_FALSE; 7745 } 7746 PetscFunctionReturn(0); 7747 } 7748 7749 #undef __FUNCT__ 7750 #define __FUNCT__ "MatIsHermitianKnown" 7751 /*@ 7752 MatIsHermitianKnown - Checks the flag on the matrix to see if it is hermitian. 7753 7754 Not Collective 7755 7756 Input Parameter: 7757 . A - the matrix to check 7758 7759 Output Parameters: 7760 + set - if the hermitian flag is set (this tells you if the next flag is valid) 7761 - flg - the result 7762 7763 Level: advanced 7764 7765 Concepts: matrix^symmetry 7766 7767 Note: Does not check the matrix values directly, so this may return unknown (set = PETSC_FALSE). Use MatIsHermitian() 7768 if you want it explicitly checked 7769 7770 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetric() 7771 @*/ 7772 PetscErrorCode MatIsHermitianKnown(Mat A,PetscBool *set,PetscBool *flg) 7773 { 7774 PetscFunctionBegin; 7775 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 7776 PetscValidPointer(set,2); 7777 PetscValidPointer(flg,3); 7778 if (A->hermitian_set) { 7779 *set = PETSC_TRUE; 7780 *flg = A->hermitian; 7781 } else { 7782 *set = PETSC_FALSE; 7783 } 7784 PetscFunctionReturn(0); 7785 } 7786 7787 #undef __FUNCT__ 7788 #define __FUNCT__ "MatIsStructurallySymmetric" 7789 /*@ 7790 MatIsStructurallySymmetric - Test whether a matrix is structurally symmetric 7791 7792 Collective on Mat 7793 7794 Input Parameter: 7795 . A - the matrix to test 7796 7797 Output Parameters: 7798 . flg - the result 7799 7800 Level: intermediate 7801 7802 Concepts: matrix^symmetry 7803 7804 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsSymmetric(), MatSetOption() 7805 @*/ 7806 PetscErrorCode MatIsStructurallySymmetric(Mat A,PetscBool *flg) 7807 { 7808 PetscErrorCode ierr; 7809 7810 PetscFunctionBegin; 7811 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 7812 PetscValidPointer(flg,2); 7813 if (!A->structurally_symmetric_set) { 7814 if (!A->ops->isstructurallysymmetric) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_SUP,"Matrix does not support checking for structural symmetric"); 7815 ierr = (*A->ops->isstructurallysymmetric)(A,&A->structurally_symmetric);CHKERRQ(ierr); 7816 A->structurally_symmetric_set = PETSC_TRUE; 7817 } 7818 *flg = A->structurally_symmetric; 7819 PetscFunctionReturn(0); 7820 } 7821 7822 #undef __FUNCT__ 7823 #define __FUNCT__ "MatStashGetInfo" 7824 extern PetscErrorCode MatStashGetInfo_Private(MatStash*,PetscInt*,PetscInt*); 7825 /*@ 7826 MatStashGetInfo - Gets how many values are currently in the vector stash, i.e. need 7827 to be communicated to other processors during the MatAssemblyBegin/End() process 7828 7829 Not collective 7830 7831 Input Parameter: 7832 . vec - the vector 7833 7834 Output Parameters: 7835 + nstash - the size of the stash 7836 . reallocs - the number of additional mallocs incurred. 7837 . bnstash - the size of the block stash 7838 - breallocs - the number of additional mallocs incurred.in the block stash 7839 7840 Level: advanced 7841 7842 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), Mat, MatStashSetInitialSize() 7843 7844 @*/ 7845 PetscErrorCode MatStashGetInfo(Mat mat,PetscInt *nstash,PetscInt *reallocs,PetscInt *bnstash,PetscInt *breallocs) 7846 { 7847 PetscErrorCode ierr; 7848 PetscFunctionBegin; 7849 ierr = MatStashGetInfo_Private(&mat->stash,nstash,reallocs);CHKERRQ(ierr); 7850 ierr = MatStashGetInfo_Private(&mat->bstash,bnstash,breallocs);CHKERRQ(ierr); 7851 PetscFunctionReturn(0); 7852 } 7853 7854 #undef __FUNCT__ 7855 #define __FUNCT__ "MatGetVecs" 7856 /*@C 7857 MatGetVecs - Get vector(s) compatible with the matrix, i.e. with the same 7858 parallel layout 7859 7860 Collective on Mat 7861 7862 Input Parameter: 7863 . mat - the matrix 7864 7865 Output Parameter: 7866 + right - (optional) vector that the matrix can be multiplied against 7867 - left - (optional) vector that the matrix vector product can be stored in 7868 7869 Level: advanced 7870 7871 .seealso: MatCreate() 7872 @*/ 7873 PetscErrorCode MatGetVecs(Mat mat,Vec *right,Vec *left) 7874 { 7875 PetscErrorCode ierr; 7876 7877 PetscFunctionBegin; 7878 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 7879 PetscValidType(mat,1); 7880 ierr = MatPreallocated(mat);CHKERRQ(ierr); 7881 if (mat->ops->getvecs) { 7882 ierr = (*mat->ops->getvecs)(mat,right,left);CHKERRQ(ierr); 7883 } else { 7884 PetscMPIInt size; 7885 ierr = MPI_Comm_size(((PetscObject)mat)->comm, &size);CHKERRQ(ierr); 7886 if (right) { 7887 ierr = VecCreate(((PetscObject)mat)->comm,right);CHKERRQ(ierr); 7888 ierr = VecSetSizes(*right,mat->cmap->n,PETSC_DETERMINE);CHKERRQ(ierr); 7889 ierr = VecSetBlockSize(*right,mat->rmap->bs);CHKERRQ(ierr); 7890 if (size > 1) { 7891 /* New vectors uses Mat cmap and does not create a new one */ 7892 ierr = PetscLayoutDestroy((*right)->map);CHKERRQ(ierr); 7893 (*right)->map = mat->cmap; 7894 mat->cmap->refcnt++; 7895 7896 ierr = VecSetType(*right,VECMPI);CHKERRQ(ierr); 7897 } else {ierr = VecSetType(*right,VECSEQ);CHKERRQ(ierr);} 7898 } 7899 if (left) { 7900 ierr = VecCreate(((PetscObject)mat)->comm,left);CHKERRQ(ierr); 7901 ierr = VecSetSizes(*left,mat->rmap->n,PETSC_DETERMINE);CHKERRQ(ierr); 7902 ierr = VecSetBlockSize(*left,mat->rmap->bs);CHKERRQ(ierr); 7903 if (size > 1) { 7904 /* New vectors uses Mat rmap and does not create a new one */ 7905 ierr = PetscLayoutDestroy((*left)->map);CHKERRQ(ierr); 7906 (*left)->map = mat->rmap; 7907 mat->rmap->refcnt++; 7908 7909 ierr = VecSetType(*left,VECMPI);CHKERRQ(ierr); 7910 } else {ierr = VecSetType(*left,VECSEQ);CHKERRQ(ierr);} 7911 } 7912 } 7913 if (mat->rmapping) { 7914 if (right) {ierr = VecSetLocalToGlobalMapping(*right,mat->cmapping);CHKERRQ(ierr);} 7915 if (left) {ierr = VecSetLocalToGlobalMapping(*left,mat->rmapping);CHKERRQ(ierr);} 7916 } 7917 if (mat->rbmapping) { 7918 if (right) {ierr = VecSetLocalToGlobalMappingBlock(*right,mat->cbmapping);CHKERRQ(ierr);} 7919 if (left) {ierr = VecSetLocalToGlobalMappingBlock(*left,mat->rbmapping);CHKERRQ(ierr);} 7920 } 7921 PetscFunctionReturn(0); 7922 } 7923 7924 #undef __FUNCT__ 7925 #define __FUNCT__ "MatFactorInfoInitialize" 7926 /*@C 7927 MatFactorInfoInitialize - Initializes a MatFactorInfo data structure 7928 with default values. 7929 7930 Not Collective 7931 7932 Input Parameters: 7933 . info - the MatFactorInfo data structure 7934 7935 7936 Notes: The solvers are generally used through the KSP and PC objects, for example 7937 PCLU, PCILU, PCCHOLESKY, PCICC 7938 7939 Level: developer 7940 7941 .seealso: MatFactorInfo 7942 7943 Developer Note: fortran interface is not autogenerated as the f90 7944 interface defintion cannot be generated correctly [due to MatFactorInfo] 7945 7946 @*/ 7947 7948 PetscErrorCode MatFactorInfoInitialize(MatFactorInfo *info) 7949 { 7950 PetscErrorCode ierr; 7951 7952 PetscFunctionBegin; 7953 ierr = PetscMemzero(info,sizeof(MatFactorInfo));CHKERRQ(ierr); 7954 PetscFunctionReturn(0); 7955 } 7956 7957 #undef __FUNCT__ 7958 #define __FUNCT__ "MatPtAP" 7959 /*@ 7960 MatPtAP - Creates the matrix product C = P^T * A * P 7961 7962 Neighbor-wise Collective on Mat 7963 7964 Input Parameters: 7965 + A - the matrix 7966 . P - the projection matrix 7967 . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 7968 - fill - expected fill as ratio of nnz(C)/nnz(A) 7969 7970 Output Parameters: 7971 . C - the product matrix 7972 7973 Notes: 7974 C will be created and must be destroyed by the user with MatDestroy(). 7975 7976 This routine is currently only implemented for pairs of AIJ matrices and classes 7977 which inherit from AIJ. 7978 7979 Level: intermediate 7980 7981 .seealso: MatPtAPSymbolic(), MatPtAPNumeric(), MatMatMult() 7982 @*/ 7983 PetscErrorCode MatPtAP(Mat A,Mat P,MatReuse scall,PetscReal fill,Mat *C) 7984 { 7985 PetscErrorCode ierr; 7986 7987 PetscFunctionBegin; 7988 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 7989 PetscValidType(A,1); 7990 if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 7991 if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7992 PetscValidHeaderSpecific(P,MAT_CLASSID,2); 7993 PetscValidType(P,2); 7994 ierr = MatPreallocated(P);CHKERRQ(ierr); 7995 if (!P->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 7996 if (P->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 7997 PetscValidPointer(C,3); 7998 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); 7999 if (fill < 1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be >= 1.0",fill); 8000 ierr = MatPreallocated(A);CHKERRQ(ierr); 8001 8002 if (!A->ops->ptap) { 8003 const MatType mattype; 8004 ierr = MatGetType(A,&mattype);CHKERRQ(ierr); 8005 SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"Matrix of type <%s> does not support PtAP",mattype); 8006 } 8007 ierr = PetscLogEventBegin(MAT_PtAP,A,P,0,0);CHKERRQ(ierr); 8008 ierr = (*A->ops->ptap)(A,P,scall,fill,C);CHKERRQ(ierr); 8009 ierr = PetscLogEventEnd(MAT_PtAP,A,P,0,0);CHKERRQ(ierr); 8010 8011 PetscFunctionReturn(0); 8012 } 8013 8014 #undef __FUNCT__ 8015 #define __FUNCT__ "MatPtAPNumeric" 8016 /*@ 8017 MatPtAPNumeric - Computes the matrix product C = P^T * A * P 8018 8019 Neighbor-wise Collective on Mat 8020 8021 Input Parameters: 8022 + A - the matrix 8023 - P - the projection matrix 8024 8025 Output Parameters: 8026 . C - the product matrix 8027 8028 Notes: 8029 C must have been created by calling MatPtAPSymbolic and must be destroyed by 8030 the user using MatDeatroy(). 8031 8032 This routine is currently only implemented for pairs of AIJ matrices and classes 8033 which inherit from AIJ. C will be of type MATAIJ. 8034 8035 Level: intermediate 8036 8037 .seealso: MatPtAP(), MatPtAPSymbolic(), MatMatMultNumeric() 8038 @*/ 8039 PetscErrorCode MatPtAPNumeric(Mat A,Mat P,Mat C) 8040 { 8041 PetscErrorCode ierr; 8042 8043 PetscFunctionBegin; 8044 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 8045 PetscValidType(A,1); 8046 if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 8047 if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 8048 PetscValidHeaderSpecific(P,MAT_CLASSID,2); 8049 PetscValidType(P,2); 8050 ierr = MatPreallocated(P);CHKERRQ(ierr); 8051 if (!P->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 8052 if (P->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 8053 PetscValidHeaderSpecific(C,MAT_CLASSID,3); 8054 PetscValidType(C,3); 8055 ierr = MatPreallocated(C);CHKERRQ(ierr); 8056 if (C->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 8057 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); 8058 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); 8059 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); 8060 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); 8061 ierr = MatPreallocated(A);CHKERRQ(ierr); 8062 8063 ierr = PetscLogEventBegin(MAT_PtAPNumeric,A,P,0,0);CHKERRQ(ierr); 8064 ierr = (*A->ops->ptapnumeric)(A,P,C);CHKERRQ(ierr); 8065 ierr = PetscLogEventEnd(MAT_PtAPNumeric,A,P,0,0);CHKERRQ(ierr); 8066 PetscFunctionReturn(0); 8067 } 8068 8069 #undef __FUNCT__ 8070 #define __FUNCT__ "MatPtAPSymbolic" 8071 /*@ 8072 MatPtAPSymbolic - Creates the (i,j) structure of the matrix product C = P^T * A * P 8073 8074 Neighbor-wise Collective on Mat 8075 8076 Input Parameters: 8077 + A - the matrix 8078 - P - the projection matrix 8079 8080 Output Parameters: 8081 . C - the (i,j) structure of the product matrix 8082 8083 Notes: 8084 C will be created and must be destroyed by the user with MatDestroy(). 8085 8086 This routine is currently only implemented for pairs of SeqAIJ matrices and classes 8087 which inherit from SeqAIJ. C will be of type MATSEQAIJ. The product is computed using 8088 this (i,j) structure by calling MatPtAPNumeric(). 8089 8090 Level: intermediate 8091 8092 .seealso: MatPtAP(), MatPtAPNumeric(), MatMatMultSymbolic() 8093 @*/ 8094 PetscErrorCode MatPtAPSymbolic(Mat A,Mat P,PetscReal fill,Mat *C) 8095 { 8096 PetscErrorCode ierr; 8097 8098 PetscFunctionBegin; 8099 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 8100 PetscValidType(A,1); 8101 if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 8102 if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 8103 if (fill <1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be >= 1.0",fill); 8104 PetscValidHeaderSpecific(P,MAT_CLASSID,2); 8105 PetscValidType(P,2); 8106 ierr = MatPreallocated(P);CHKERRQ(ierr); 8107 if (!P->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 8108 if (P->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 8109 PetscValidPointer(C,3); 8110 8111 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); 8112 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); 8113 ierr = MatPreallocated(A);CHKERRQ(ierr); 8114 ierr = PetscLogEventBegin(MAT_PtAPSymbolic,A,P,0,0);CHKERRQ(ierr); 8115 ierr = (*A->ops->ptapsymbolic)(A,P,fill,C);CHKERRQ(ierr); 8116 ierr = PetscLogEventEnd(MAT_PtAPSymbolic,A,P,0,0);CHKERRQ(ierr); 8117 8118 ierr = MatSetBlockSize(*C,A->rmap->bs);CHKERRQ(ierr); 8119 8120 PetscFunctionReturn(0); 8121 } 8122 8123 #undef __FUNCT__ 8124 #define __FUNCT__ "MatMatMult" 8125 /*@ 8126 MatMatMult - Performs Matrix-Matrix Multiplication C=A*B. 8127 8128 Neighbor-wise Collective on Mat 8129 8130 Input Parameters: 8131 + A - the left matrix 8132 . B - the right matrix 8133 . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 8134 - fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if you do not have a good estimate 8135 if the result is a dense matrix this is irrelevent 8136 8137 Output Parameters: 8138 . C - the product matrix 8139 8140 Notes: 8141 Unless scall is MAT_REUSE_MATRIX C will be created. 8142 8143 MAT_REUSE_MATRIX can only be used if the matrices A and B have the same nonzero pattern as in the previous call 8144 8145 To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value 8146 actually needed. 8147 8148 If you have many matrices with the same non-zero structure to multiply, you 8149 should either 8150 $ 1) use MAT_REUSE_MATRIX in all calls but the first or 8151 $ 2) call MatMatMultSymbolic() once and then MatMatMultNumeric() for each product needed 8152 8153 Level: intermediate 8154 8155 .seealso: MatMatMultSymbolic(), MatMatMultNumeric(), MatPtAP() 8156 @*/ 8157 PetscErrorCode MatMatMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C) 8158 { 8159 PetscErrorCode ierr; 8160 PetscErrorCode (*fA)(Mat,Mat,MatReuse,PetscReal,Mat*); 8161 PetscErrorCode (*fB)(Mat,Mat,MatReuse,PetscReal,Mat*); 8162 PetscErrorCode (*mult)(Mat,Mat,MatReuse,PetscReal,Mat *)=PETSC_NULL; 8163 8164 PetscFunctionBegin; 8165 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 8166 PetscValidType(A,1); 8167 if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 8168 if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 8169 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 8170 PetscValidType(B,2); 8171 ierr = MatPreallocated(B);CHKERRQ(ierr); 8172 if (!B->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 8173 if (B->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 8174 PetscValidPointer(C,3); 8175 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); 8176 if (scall == MAT_REUSE_MATRIX){ 8177 PetscValidPointer(*C,5); 8178 PetscValidHeaderSpecific(*C,MAT_CLASSID,5); 8179 } 8180 if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0; 8181 if (fill < 1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be >= 1.0",fill); 8182 ierr = MatPreallocated(A);CHKERRQ(ierr); 8183 8184 fA = A->ops->matmult; 8185 fB = B->ops->matmult; 8186 if (fB == fA) { 8187 if (!fB) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"MatMatMult not supported for B of type %s",((PetscObject)B)->type_name); 8188 mult = fB; 8189 } else { 8190 /* dispatch based on the type of A and B */ 8191 char multname[256]; 8192 ierr = PetscStrcpy(multname,"MatMatMult_");CHKERRQ(ierr); 8193 ierr = PetscStrcat(multname,((PetscObject)A)->type_name);CHKERRQ(ierr); 8194 ierr = PetscStrcat(multname,"_");CHKERRQ(ierr); 8195 ierr = PetscStrcat(multname,((PetscObject)B)->type_name);CHKERRQ(ierr); 8196 ierr = PetscStrcat(multname,"_C");CHKERRQ(ierr); /* e.g., multname = "MatMatMult_seqdense_seqaij_C" */ 8197 ierr = PetscObjectQueryFunction((PetscObject)B,multname,(void (**)(void))&mult);CHKERRQ(ierr); 8198 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); 8199 } 8200 ierr = PetscLogEventBegin(MAT_MatMult,A,B,0,0);CHKERRQ(ierr); 8201 ierr = (*mult)(A,B,scall,fill,C);CHKERRQ(ierr); 8202 ierr = PetscLogEventEnd(MAT_MatMult,A,B,0,0);CHKERRQ(ierr); 8203 PetscFunctionReturn(0); 8204 } 8205 8206 #undef __FUNCT__ 8207 #define __FUNCT__ "MatMatMultSymbolic" 8208 /*@ 8209 MatMatMultSymbolic - Performs construction, preallocation, and computes the ij structure 8210 of the matrix-matrix product C=A*B. Call this routine before calling MatMatMultNumeric(). 8211 8212 Neighbor-wise Collective on Mat 8213 8214 Input Parameters: 8215 + A - the left matrix 8216 . B - the right matrix 8217 - fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if you do not have a good estimate, 8218 if C is a dense matrix this is irrelevent 8219 8220 Output Parameters: 8221 . C - the product matrix 8222 8223 Notes: 8224 Unless scall is MAT_REUSE_MATRIX C will be created. 8225 8226 To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value 8227 actually needed. 8228 8229 This routine is currently implemented for 8230 - pairs of AIJ matrices and classes which inherit from AIJ, C will be of type AIJ 8231 - pairs of AIJ (A) and Dense (B) matrix, C will be of type Dense. 8232 - pairs of Dense (A) and AIJ (B) matrix, C will be of type Dense. 8233 8234 Level: intermediate 8235 8236 Developers Note: There are ways to estimate the number of nonzeros in the resulting product, see for example, http://arxiv.org/abs/1006.4173 8237 We should incorporate them into PETSc. 8238 8239 .seealso: MatMatMult(), MatMatMultNumeric() 8240 @*/ 8241 PetscErrorCode MatMatMultSymbolic(Mat A,Mat B,PetscReal fill,Mat *C) 8242 { 8243 PetscErrorCode ierr; 8244 PetscErrorCode (*Asymbolic)(Mat,Mat,PetscReal,Mat *); 8245 PetscErrorCode (*Bsymbolic)(Mat,Mat,PetscReal,Mat *); 8246 PetscErrorCode (*symbolic)(Mat,Mat,PetscReal,Mat *)=PETSC_NULL; 8247 8248 PetscFunctionBegin; 8249 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 8250 PetscValidType(A,1); 8251 if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 8252 if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 8253 8254 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 8255 PetscValidType(B,2); 8256 ierr = MatPreallocated(B);CHKERRQ(ierr); 8257 if (!B->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 8258 if (B->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 8259 PetscValidPointer(C,3); 8260 8261 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); 8262 if (fill == PETSC_DEFAULT) fill = 2.0; 8263 if (fill < 1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be > 1.0",fill); 8264 ierr = MatPreallocated(A);CHKERRQ(ierr); 8265 8266 Asymbolic = A->ops->matmultsymbolic; 8267 Bsymbolic = B->ops->matmultsymbolic; 8268 if (Asymbolic == Bsymbolic){ 8269 if (!Bsymbolic) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"C=A*B not implemented for B of type %s",((PetscObject)B)->type_name); 8270 symbolic = Bsymbolic; 8271 } else { /* dispatch based on the type of A and B */ 8272 char symbolicname[256]; 8273 ierr = PetscStrcpy(symbolicname,"MatMatMultSymbolic_");CHKERRQ(ierr); 8274 ierr = PetscStrcat(symbolicname,((PetscObject)A)->type_name);CHKERRQ(ierr); 8275 ierr = PetscStrcat(symbolicname,"_");CHKERRQ(ierr); 8276 ierr = PetscStrcat(symbolicname,((PetscObject)B)->type_name);CHKERRQ(ierr); 8277 ierr = PetscStrcat(symbolicname,"_C");CHKERRQ(ierr); 8278 ierr = PetscObjectQueryFunction((PetscObject)B,symbolicname,(void (**)(void))&symbolic);CHKERRQ(ierr); 8279 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); 8280 } 8281 ierr = PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr); 8282 ierr = (*symbolic)(A,B,fill,C);CHKERRQ(ierr); 8283 ierr = PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr); 8284 PetscFunctionReturn(0); 8285 } 8286 8287 #undef __FUNCT__ 8288 #define __FUNCT__ "MatMatMultNumeric" 8289 /*@ 8290 MatMatMultNumeric - Performs the numeric matrix-matrix product. 8291 Call this routine after first calling MatMatMultSymbolic(). 8292 8293 Neighbor-wise Collective on Mat 8294 8295 Input Parameters: 8296 + A - the left matrix 8297 - B - the right matrix 8298 8299 Output Parameters: 8300 . C - the product matrix, which was created by from MatMatMultSymbolic() or a call to MatMatMult(). 8301 8302 Notes: 8303 C must have been created with MatMatMultSymbolic(). 8304 8305 This routine is currently implemented for 8306 - pairs of AIJ matrices and classes which inherit from AIJ, C will be of type MATAIJ. 8307 - pairs of AIJ (A) and Dense (B) matrix, C will be of type Dense. 8308 - pairs of Dense (A) and AIJ (B) matrix, C will be of type Dense. 8309 8310 Level: intermediate 8311 8312 .seealso: MatMatMult(), MatMatMultSymbolic() 8313 @*/ 8314 PetscErrorCode MatMatMultNumeric(Mat A,Mat B,Mat C) 8315 { 8316 PetscErrorCode ierr; 8317 PetscErrorCode (*Anumeric)(Mat,Mat,Mat); 8318 PetscErrorCode (*Bnumeric)(Mat,Mat,Mat); 8319 PetscErrorCode (*numeric)(Mat,Mat,Mat)=PETSC_NULL; 8320 8321 PetscFunctionBegin; 8322 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 8323 PetscValidType(A,1); 8324 if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 8325 if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 8326 8327 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 8328 PetscValidType(B,2); 8329 ierr = MatPreallocated(B);CHKERRQ(ierr); 8330 if (!B->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 8331 if (B->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 8332 8333 PetscValidHeaderSpecific(C,MAT_CLASSID,3); 8334 PetscValidType(C,3); 8335 ierr = MatPreallocated(C);CHKERRQ(ierr); 8336 if (!C->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 8337 if (C->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 8338 8339 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); 8340 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); 8341 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); 8342 ierr = MatPreallocated(A);CHKERRQ(ierr); 8343 8344 Anumeric = A->ops->matmultnumeric; 8345 Bnumeric = B->ops->matmultnumeric; 8346 if (Anumeric == Bnumeric){ 8347 if (!Bnumeric) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"MatMatMultNumeric not supported for B of type %s",((PetscObject)B)->type_name); 8348 numeric = Bnumeric; 8349 } else { 8350 char numericname[256]; 8351 ierr = PetscStrcpy(numericname,"MatMatMultNumeric_");CHKERRQ(ierr); 8352 ierr = PetscStrcat(numericname,((PetscObject)A)->type_name);CHKERRQ(ierr); 8353 ierr = PetscStrcat(numericname,"_");CHKERRQ(ierr); 8354 ierr = PetscStrcat(numericname,((PetscObject)B)->type_name);CHKERRQ(ierr); 8355 ierr = PetscStrcat(numericname,"_C");CHKERRQ(ierr); 8356 ierr = PetscObjectQueryFunction((PetscObject)B,numericname,(void (**)(void))&numeric);CHKERRQ(ierr); 8357 if (!numeric) 8358 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); 8359 } 8360 ierr = PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr); 8361 ierr = (*numeric)(A,B,C);CHKERRQ(ierr); 8362 ierr = PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr); 8363 PetscFunctionReturn(0); 8364 } 8365 8366 #undef __FUNCT__ 8367 #define __FUNCT__ "MatMatMultTranspose" 8368 /*@ 8369 MatMatMultTranspose - Performs Matrix-Matrix Multiplication C=A^T*B. 8370 8371 Neighbor-wise Collective on Mat 8372 8373 Input Parameters: 8374 + A - the left matrix 8375 . B - the right matrix 8376 . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 8377 - fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if not known 8378 8379 Output Parameters: 8380 . C - the product matrix 8381 8382 Notes: 8383 C will be created if MAT_INITIAL_MATRIX and must be destroyed by the user with MatDestroy(). 8384 8385 MAT_REUSE_MATRIX can only be used if the matrices A and B have the same nonzero pattern as in the previous call 8386 8387 To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value 8388 actually needed. 8389 8390 This routine is currently only implemented for pairs of SeqAIJ matrices and pairs of SeqDense matrices and classes 8391 which inherit from SeqAIJ. C will be of type MATSEQAIJ. 8392 8393 Level: intermediate 8394 8395 .seealso: MatMatMultTransposeSymbolic(), MatMatMultTransposeNumeric(), MatPtAP() 8396 @*/ 8397 PetscErrorCode MatMatMultTranspose(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C) 8398 { 8399 PetscErrorCode ierr; 8400 PetscErrorCode (*fA)(Mat,Mat,MatReuse,PetscReal,Mat*); 8401 PetscErrorCode (*fB)(Mat,Mat,MatReuse,PetscReal,Mat*); 8402 8403 PetscFunctionBegin; 8404 PetscValidHeaderSpecific(A,MAT_CLASSID,1); 8405 PetscValidType(A,1); 8406 if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 8407 if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 8408 PetscValidHeaderSpecific(B,MAT_CLASSID,2); 8409 PetscValidType(B,2); 8410 ierr = MatPreallocated(B);CHKERRQ(ierr); 8411 if (!B->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 8412 if (B->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 8413 PetscValidPointer(C,3); 8414 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); 8415 if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0; 8416 if (fill < 1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be > 1.0",fill); 8417 ierr = MatPreallocated(A);CHKERRQ(ierr); 8418 8419 fA = A->ops->matmulttranspose; 8420 if (!fA) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"MatMatMultTranspose not supported for A of type %s",((PetscObject)A)->type_name); 8421 fB = B->ops->matmulttranspose; 8422 if (!fB) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"MatMatMultTranspose not supported for B of type %s",((PetscObject)B)->type_name); 8423 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); 8424 8425 ierr = PetscLogEventBegin(MAT_MatMultTranspose,A,B,0,0);CHKERRQ(ierr); 8426 ierr = (*A->ops->matmulttranspose)(A,B,scall,fill,C);CHKERRQ(ierr); 8427 ierr = PetscLogEventEnd(MAT_MatMultTranspose,A,B,0,0);CHKERRQ(ierr); 8428 8429 PetscFunctionReturn(0); 8430 } 8431 8432 #undef __FUNCT__ 8433 #define __FUNCT__ "MatGetRedundantMatrix" 8434 /*@C 8435 MatGetRedundantMatrix - Create redundant matrices and put them into processors of subcommunicators. 8436 8437 Collective on Mat 8438 8439 Input Parameters: 8440 + mat - the matrix 8441 . nsubcomm - the number of subcommunicators (= number of redundant parallel or sequential matrices) 8442 . subcomm - MPI communicator split from the communicator where mat resides in 8443 . mlocal_red - number of local rows of the redundant matrix 8444 - reuse - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 8445 8446 Output Parameter: 8447 . matredundant - redundant matrix 8448 8449 Notes: 8450 MAT_REUSE_MATRIX can only be used when the nonzero structure of the 8451 original matrix has not changed from that last call to MatGetRedundantMatrix(). 8452 8453 This routine creates the duplicated matrices in subcommunicators; you should NOT create them before 8454 calling it. 8455 8456 Only MPIAIJ matrix is supported. 8457 8458 Level: advanced 8459 8460 Concepts: subcommunicator 8461 Concepts: duplicate matrix 8462 8463 .seealso: MatDestroy() 8464 @*/ 8465 PetscErrorCode MatGetRedundantMatrix(Mat mat,PetscInt nsubcomm,MPI_Comm subcomm,PetscInt mlocal_red,MatReuse reuse,Mat *matredundant) 8466 { 8467 PetscErrorCode ierr; 8468 8469 PetscFunctionBegin; 8470 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8471 if (nsubcomm && reuse == MAT_REUSE_MATRIX) { 8472 PetscValidPointer(*matredundant,6); 8473 PetscValidHeaderSpecific(*matredundant,MAT_CLASSID,6); 8474 } 8475 if (!mat->ops->getredundantmatrix) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name); 8476 if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 8477 if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 8478 ierr = MatPreallocated(mat);CHKERRQ(ierr); 8479 8480 ierr = PetscLogEventBegin(MAT_GetRedundantMatrix,mat,0,0,0);CHKERRQ(ierr); 8481 ierr = (*mat->ops->getredundantmatrix)(mat,nsubcomm,subcomm,mlocal_red,reuse,matredundant);CHKERRQ(ierr); 8482 ierr = PetscLogEventEnd(MAT_GetRedundantMatrix,mat,0,0,0);CHKERRQ(ierr); 8483 PetscFunctionReturn(0); 8484 } 8485 8486 #undef __FUNCT__ 8487 #define __FUNCT__ "MatGetMultiProcBlock" 8488 /*@C 8489 MatGetMultiProcBlock - Create multiple [bjacobi] 'parallel submatrices' from 8490 a given 'mat' object. Each submatrix can span multiple procs. 8491 8492 Collective on Mat 8493 8494 Input Parameters: 8495 + mat - the matrix 8496 - subcomm - the subcommunicator obtained by com_split(comm) 8497 8498 Output Parameter: 8499 . subMat - 'parallel submatrices each spans a given subcomm 8500 8501 Notes: 8502 The submatrix partition across processors is dicated by 'subComm' a 8503 communicator obtained by com_split(comm). The comm_split 8504 is not restriced to be grouped with consequitive original ranks. 8505 8506 Due the comm_split() usage, the parallel layout of the submatrices 8507 map directly to the layout of the original matrix [wrt the local 8508 row,col partitioning]. So the original 'DiagonalMat' naturally maps 8509 into the 'DiagonalMat' of the subMat, hence it is used directly from 8510 the subMat. However the offDiagMat looses some columns - and this is 8511 reconstructed with MatSetValues() 8512 8513 Level: advanced 8514 8515 Concepts: subcommunicator 8516 Concepts: submatrices 8517 8518 .seealso: MatGetSubMatrices() 8519 @*/ 8520 PetscErrorCode MatGetMultiProcBlock(Mat mat, MPI_Comm subComm, Mat* subMat) 8521 { 8522 PetscErrorCode ierr; 8523 PetscMPIInt commsize,subCommSize; 8524 8525 PetscFunctionBegin; 8526 ierr = MPI_Comm_size(((PetscObject)mat)->comm,&commsize);CHKERRQ(ierr); 8527 ierr = MPI_Comm_size(subComm,&subCommSize);CHKERRQ(ierr); 8528 if (subCommSize > commsize) SETERRQ2(((PetscObject)mat)->comm,PETSC_ERR_ARG_OUTOFRANGE,"CommSize %D < SubCommZize %D",commsize,subCommSize); 8529 8530 ierr = PetscLogEventBegin(MAT_GetMultiProcBlock,mat,0,0,0);CHKERRQ(ierr); 8531 ierr = (*mat->ops->getmultiprocblock)(mat,subComm,subMat);CHKERRQ(ierr); 8532 ierr = PetscLogEventEnd(MAT_GetMultiProcBlock,mat,0,0,0);CHKERRQ(ierr); 8533 PetscFunctionReturn(0); 8534 } 8535 8536 #undef __FUNCT__ 8537 #define __FUNCT__ "MatGetLocalSubMatrix" 8538 /*@ 8539 MatGetLocalSubMatrix - Gets a reference to a submatrix specified in local numbering 8540 8541 Not Collective 8542 8543 Input Arguments: 8544 mat - matrix to extract local submatrix from 8545 isrow - local row indices for submatrix 8546 iscol - local column indices for submatrix 8547 8548 Output Arguments: 8549 submat - the submatrix 8550 8551 Level: intermediate 8552 8553 Notes: 8554 The submat should be returned with MatRestoreLocalSubMatrix(). 8555 8556 Depending on the format of mat, the returned submat may not implement MatMult(). Its communicator may be 8557 the same as mat, it may be PETSC_COMM_SELF, or some other subcomm of mat's. 8558 8559 The submat always implements MatSetValuesLocal(). If isrow and iscol have the same block size, then 8560 MatSetValuesBlockedLocal() will also be implemented. 8561 8562 .seealso: MatRestoreLocalSubMatrix(), MatCreateLocalRef() 8563 @*/ 8564 PetscErrorCode MatGetLocalSubMatrix(Mat mat,IS isrow,IS iscol,Mat *submat) 8565 { 8566 PetscErrorCode ierr; 8567 8568 PetscFunctionBegin; 8569 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8570 PetscValidHeaderSpecific(isrow,IS_CLASSID,2); 8571 PetscValidHeaderSpecific(iscol,IS_CLASSID,3); 8572 PetscCheckSameComm(isrow,2,iscol,3); 8573 PetscValidPointer(submat,4); 8574 8575 if (mat->ops->getlocalsubmatrix) { 8576 ierr = (*mat->ops->getlocalsubmatrix)(mat,isrow,iscol,submat);CHKERRQ(ierr); 8577 } else { 8578 ierr = MatCreateLocalRef(mat,isrow,iscol,submat);CHKERRQ(ierr); 8579 } 8580 PetscFunctionReturn(0); 8581 } 8582 8583 #undef __FUNCT__ 8584 #define __FUNCT__ "MatRestoreLocalSubMatrix" 8585 /*@ 8586 MatRestoreLocalSubMatrix - Restores a reference to a submatrix specified in local numbering 8587 8588 Not Collective 8589 8590 Input Arguments: 8591 mat - matrix to extract local submatrix from 8592 isrow - local row indices for submatrix 8593 iscol - local column indices for submatrix 8594 submat - the submatrix 8595 8596 Level: intermediate 8597 8598 .seealso: MatGetLocalSubMatrix() 8599 @*/ 8600 PetscErrorCode MatRestoreLocalSubMatrix(Mat mat,IS isrow,IS iscol,Mat *submat) 8601 { 8602 PetscErrorCode ierr; 8603 8604 PetscFunctionBegin; 8605 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8606 PetscValidHeaderSpecific(isrow,IS_CLASSID,2); 8607 PetscValidHeaderSpecific(iscol,IS_CLASSID,3); 8608 PetscCheckSameComm(isrow,2,iscol,3); 8609 PetscValidPointer(submat,4); 8610 if (*submat) {PetscValidHeaderSpecific(*submat,MAT_CLASSID,4);} 8611 8612 if (mat->ops->restorelocalsubmatrix) { 8613 ierr = (*mat->ops->restorelocalsubmatrix)(mat,isrow,iscol,submat);CHKERRQ(ierr); 8614 } else { 8615 ierr = MatDestroy(*submat);CHKERRQ(ierr); 8616 } 8617 *submat = PETSC_NULL; 8618 PetscFunctionReturn(0); 8619 } 8620 8621 /* --------------------------------------------------------*/ 8622 #undef __FUNCT__ 8623 #define __FUNCT__ "MatFindZeroDiagonals" 8624 /*@ 8625 MatFindZeroDiagonals - Finds all the rows of a matrix that have zero or no entry in the matrix 8626 8627 Collective on Mat 8628 8629 Input Parameter: 8630 . mat - the matrix 8631 8632 Output Parameter: 8633 . is - if any rows have zero diagonals this contains the list of them 8634 8635 Level: developer 8636 8637 Concepts: matrix-vector product 8638 8639 .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd() 8640 @*/ 8641 PetscErrorCode MatFindZeroDiagonals(Mat mat,IS *is) 8642 { 8643 PetscErrorCode ierr; 8644 8645 PetscFunctionBegin; 8646 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 8647 PetscValidType(mat,1); 8648 if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 8649 if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 8650 8651 if (!mat->ops->findzerodiagonals) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_SUP,"This matrix type does not have a find zero diagonals defined"); 8652 ierr = (*mat->ops->findzerodiagonals)(mat,is);CHKERRQ(ierr); 8653 PetscFunctionReturn(0); 8654 } 8655