1 /*$Id: matrix.c,v 1.414 2001/09/28 18:57:28 balay Exp $*/ 2 3 /* 4 This is where the abstract matrix operations are defined 5 */ 6 7 #include "src/mat/matimpl.h" /*I "petscmat.h" I*/ 8 #include "src/vec/vecimpl.h" 9 10 /* Logging support */ 11 int MAT_COOKIE; 12 int MATSNESMFCTX_COOKIE; 13 int MAT_Mult, MAT_MultMatrixFree, MAT_MultMultiple, MAT_MultConstrained, MAT_MultAdd, MAT_MultTranspose; 14 int MAT_MultTransposeConstrained, MAT_MultTransposeAdd, MAT_Solve, MAT_SolveMultiple, MAT_SolveAdd, MAT_SolveTranspose; 15 int MAT_SolveTransposeAdd, MAT_Relax, MAT_ForwardSolve, MAT_BackwardSolve, MAT_LUFactor, MAT_LUFactorSymbolic; 16 int MAT_LUFactorNumeric, MAT_CholeskyFactor, MAT_CholeskyFactorSymbolic, MAT_CholeskyFactorNumeric, MAT_ILUFactor; 17 int MAT_ILUFactorSymbolic, MAT_ICCFactorSymbolic, MAT_Copy, MAT_Convert, MAT_Scale, MAT_AssemblyBegin; 18 int MAT_AssemblyEnd, MAT_SetValues, MAT_GetValues, MAT_GetRow, MAT_GetSubMatrices, MAT_GetColoring, MAT_GetOrdering; 19 int MAT_IncreaseOverlap, MAT_Partitioning, MAT_ZeroEntries, MAT_Load, MAT_View, MAT_AXPY, MAT_FDColoringCreate; 20 int MAT_FDColoringApply,MAT_Transpose; 21 22 #undef __FUNCT__ 23 #define __FUNCT__ "MatGetRow" 24 /*@C 25 MatGetRow - Gets a row of a matrix. You MUST call MatRestoreRow() 26 for each row that you get to ensure that your application does 27 not bleed memory. 28 29 Not Collective 30 31 Input Parameters: 32 + mat - the matrix 33 - row - the row to get 34 35 Output Parameters: 36 + ncols - the number of nonzeros in the row 37 . cols - if not NULL, the column numbers 38 - vals - if not NULL, the values 39 40 Notes: 41 This routine is provided for people who need to have direct access 42 to the structure of a matrix. We hope that we provide enough 43 high-level matrix routines that few users will need it. 44 45 MatGetRow() always returns 0-based column indices, regardless of 46 whether the internal representation is 0-based (default) or 1-based. 47 48 For better efficiency, set cols and/or vals to PETSC_NULL if you do 49 not wish to extract these quantities. 50 51 The user can only examine the values extracted with MatGetRow(); 52 the values cannot be altered. To change the matrix entries, one 53 must use MatSetValues(). 54 55 You can only have one call to MatGetRow() outstanding for a particular 56 matrix at a time, per processor. MatGetRow() can only obtained rows 57 associated with the given processor, it cannot get rows from the 58 other processors; for that we suggest using MatGetSubMatrices(), then 59 MatGetRow() on the submatrix. The row indix passed to MatGetRows() 60 is in the global number of rows. 61 62 Fortran Notes: 63 The calling sequence from Fortran is 64 .vb 65 MatGetRow(matrix,row,ncols,cols,values,ierr) 66 Mat matrix (input) 67 integer row (input) 68 integer ncols (output) 69 integer cols(maxcols) (output) 70 double precision (or double complex) values(maxcols) output 71 .ve 72 where maxcols >= maximum nonzeros in any row of the matrix. 73 74 Caution: 75 Do not try to change the contents of the output arrays (cols and vals). 76 In some cases, this may corrupt the matrix. 77 78 Level: advanced 79 80 Concepts: matrices^row access 81 82 .seealso: MatRestoreRow(), MatSetValues(), MatGetValues(), MatGetSubmatrices(), MatGetDiagonal() 83 @*/ 84 int MatGetRow(Mat mat,int row,int *ncols,int **cols,PetscScalar **vals) 85 { 86 int ierr; 87 88 PetscFunctionBegin; 89 PetscValidHeaderSpecific(mat,MAT_COOKIE); 90 PetscValidIntPointer(ncols); 91 PetscValidType(mat); 92 MatPreallocated(mat); 93 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 94 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 95 if (!mat->ops->getrow) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 96 ierr = PetscLogEventBegin(MAT_GetRow,mat,0,0,0);CHKERRQ(ierr); 97 ierr = (*mat->ops->getrow)(mat,row,ncols,cols,vals);CHKERRQ(ierr); 98 ierr = PetscLogEventEnd(MAT_GetRow,mat,0,0,0);CHKERRQ(ierr); 99 PetscFunctionReturn(0); 100 } 101 102 #undef __FUNCT__ 103 #define __FUNCT__ "MatRestoreRow" 104 /*@C 105 MatRestoreRow - Frees any temporary space allocated by MatGetRow(). 106 107 Not Collective 108 109 Input Parameters: 110 + mat - the matrix 111 . row - the row to get 112 . ncols, cols - the number of nonzeros and their columns 113 - vals - if nonzero the column values 114 115 Notes: 116 This routine should be called after you have finished examining the entries. 117 118 Fortran Notes: 119 The calling sequence from Fortran is 120 .vb 121 MatRestoreRow(matrix,row,ncols,cols,values,ierr) 122 Mat matrix (input) 123 integer row (input) 124 integer ncols (output) 125 integer cols(maxcols) (output) 126 double precision (or double complex) values(maxcols) output 127 .ve 128 Where maxcols >= maximum nonzeros in any row of the matrix. 129 130 In Fortran MatRestoreRow() MUST be called after MatGetRow() 131 before another call to MatGetRow() can be made. 132 133 Level: advanced 134 135 .seealso: MatGetRow() 136 @*/ 137 int MatRestoreRow(Mat mat,int row,int *ncols,int **cols,PetscScalar **vals) 138 { 139 int ierr; 140 141 PetscFunctionBegin; 142 PetscValidHeaderSpecific(mat,MAT_COOKIE); 143 PetscValidIntPointer(ncols); 144 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 145 if (!mat->ops->restorerow) PetscFunctionReturn(0); 146 ierr = (*mat->ops->restorerow)(mat,row,ncols,cols,vals);CHKERRQ(ierr); 147 PetscFunctionReturn(0); 148 } 149 150 #undef __FUNCT__ 151 #define __FUNCT__ "MatView" 152 /*@C 153 MatView - Visualizes a matrix object. 154 155 Collective on Mat 156 157 Input Parameters: 158 + mat - the matrix 159 - ptr - visualization context 160 161 Notes: 162 The available visualization contexts include 163 + PETSC_VIEWER_STDOUT_SELF - standard output (default) 164 . PETSC_VIEWER_STDOUT_WORLD - synchronized standard 165 output where only the first processor opens 166 the file. All other processors send their 167 data to the first processor to print. 168 - PETSC_VIEWER_DRAW_WORLD - graphical display of nonzero structure 169 170 The user can open alternative visualization contexts with 171 + PetscViewerASCIIOpen() - Outputs matrix to a specified file 172 . PetscViewerBinaryOpen() - Outputs matrix in binary to a 173 specified file; corresponding input uses MatLoad() 174 . PetscViewerDrawOpen() - Outputs nonzero matrix structure to 175 an X window display 176 - PetscViewerSocketOpen() - Outputs matrix to Socket viewer. 177 Currently only the sequential dense and AIJ 178 matrix types support the Socket viewer. 179 180 The user can call PetscViewerSetFormat() to specify the output 181 format of ASCII printed objects (when using PETSC_VIEWER_STDOUT_SELF, 182 PETSC_VIEWER_STDOUT_WORLD and PetscViewerASCIIOpen). Available formats include 183 + PETSC_VIEWER_ASCII_DEFAULT - default, prints matrix contents 184 . PETSC_VIEWER_ASCII_MATLAB - prints matrix contents in Matlab format 185 . PETSC_VIEWER_ASCII_DENSE - prints entire matrix including zeros 186 . PETSC_VIEWER_ASCII_COMMON - prints matrix contents, using a sparse 187 format common among all matrix types 188 . PETSC_VIEWER_ASCII_IMPL - prints matrix contents, using an implementation-specific 189 format (which is in many cases the same as the default) 190 . PETSC_VIEWER_ASCII_INFO - prints basic information about the matrix 191 size and structure (not the matrix entries) 192 - PETSC_VIEWER_ASCII_INFO_LONG - prints more detailed information about 193 the matrix structure 194 195 Level: beginner 196 197 Concepts: matrices^viewing 198 Concepts: matrices^plotting 199 Concepts: matrices^printing 200 201 .seealso: PetscViewerSetFormat(), PetscViewerASCIIOpen(), PetscViewerDrawOpen(), 202 PetscViewerSocketOpen(), PetscViewerBinaryOpen(), MatLoad() 203 @*/ 204 int MatView(Mat mat,PetscViewer viewer) 205 { 206 int ierr,rows,cols; 207 PetscTruth isascii; 208 char *cstr; 209 PetscViewerFormat format; 210 211 PetscFunctionBegin; 212 PetscValidHeaderSpecific(mat,MAT_COOKIE); 213 PetscValidType(mat); 214 MatPreallocated(mat); 215 if (!viewer) viewer = PETSC_VIEWER_STDOUT_(mat->comm); 216 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_COOKIE); 217 PetscCheckSameComm(mat,viewer); 218 if (!mat->assembled) SETERRQ(1,"Must call MatAssemblyBegin/End() before viewing matrix"); 219 220 ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr); 221 if (isascii) { 222 ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 223 if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_LONG) { 224 if (mat->prefix) { 225 ierr = PetscViewerASCIIPrintf(viewer,"Matrix Object:(%s)\n",mat->prefix);CHKERRQ(ierr); 226 } else { 227 ierr = PetscViewerASCIIPrintf(viewer,"Matrix Object:\n");CHKERRQ(ierr); 228 } 229 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 230 ierr = MatGetType(mat,&cstr);CHKERRQ(ierr); 231 ierr = MatGetSize(mat,&rows,&cols);CHKERRQ(ierr); 232 ierr = PetscViewerASCIIPrintf(viewer,"type=%s, rows=%d, cols=%d\n",cstr,rows,cols);CHKERRQ(ierr); 233 if (mat->ops->getinfo) { 234 MatInfo info; 235 ierr = MatGetInfo(mat,MAT_GLOBAL_SUM,&info);CHKERRQ(ierr); 236 ierr = PetscViewerASCIIPrintf(viewer,"total: nonzeros=%d, allocated nonzeros=%d\n", 237 (int)info.nz_used,(int)info.nz_allocated);CHKERRQ(ierr); 238 } 239 } 240 } 241 if (mat->ops->view) { 242 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 243 ierr = (*mat->ops->view)(mat,viewer);CHKERRQ(ierr); 244 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 245 } else if (!isascii) { 246 SETERRQ1(1,"Viewer type %s not supported",((PetscObject)viewer)->type_name); 247 } 248 if (isascii) { 249 ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 250 if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_LONG) { 251 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 252 } 253 } 254 PetscFunctionReturn(0); 255 } 256 257 #undef __FUNCT__ 258 #define __FUNCT__ "MatScaleSystem" 259 /*@C 260 MatScaleSystem - Scale a vector solution and right hand side to 261 match the scaling of a scaled matrix. 262 263 Collective on Mat 264 265 Input Parameter: 266 + mat - the matrix 267 . x - solution vector (or PETSC_NULL) 268 + b - right hand side vector (or PETSC_NULL) 269 270 271 Notes: 272 For AIJ, BAIJ, and BDiag matrix formats, the matrices are not 273 internally scaled, so this does nothing. For MPIROWBS it 274 permutes and diagonally scales. 275 276 The SLES methods automatically call this routine when required 277 (via PCPreSolve()) so it is rarely used directly. 278 279 Level: Developer 280 281 Concepts: matrices^scaling 282 283 .seealso: MatUseScaledForm(), MatUnScaleSystem() 284 @*/ 285 int MatScaleSystem(Mat mat,Vec x,Vec b) 286 { 287 int ierr; 288 289 PetscFunctionBegin; 290 PetscValidHeaderSpecific(mat,MAT_COOKIE); 291 PetscValidType(mat); 292 MatPreallocated(mat); 293 if (x) {PetscValidHeaderSpecific(x,VEC_COOKIE);PetscCheckSameComm(mat,x);} 294 if (b) {PetscValidHeaderSpecific(b,VEC_COOKIE);PetscCheckSameComm(mat,b);} 295 296 if (mat->ops->scalesystem) { 297 ierr = (*mat->ops->scalesystem)(mat,x,b);CHKERRQ(ierr); 298 } 299 PetscFunctionReturn(0); 300 } 301 302 #undef __FUNCT__ 303 #define __FUNCT__ "MatUnScaleSystem" 304 /*@C 305 MatUnScaleSystem - Unscales a vector solution and right hand side to 306 match the original scaling of a scaled matrix. 307 308 Collective on Mat 309 310 Input Parameter: 311 + mat - the matrix 312 . x - solution vector (or PETSC_NULL) 313 + b - right hand side vector (or PETSC_NULL) 314 315 316 Notes: 317 For AIJ, BAIJ, and BDiag matrix formats, the matrices are not 318 internally scaled, so this does nothing. For MPIROWBS it 319 permutes and diagonally scales. 320 321 The SLES methods automatically call this routine when required 322 (via PCPreSolve()) so it is rarely used directly. 323 324 Level: Developer 325 326 .seealso: MatUseScaledForm(), MatScaleSystem() 327 @*/ 328 int MatUnScaleSystem(Mat mat,Vec x,Vec b) 329 { 330 int ierr; 331 332 PetscFunctionBegin; 333 PetscValidHeaderSpecific(mat,MAT_COOKIE); 334 PetscValidType(mat); 335 MatPreallocated(mat); 336 if (x) {PetscValidHeaderSpecific(x,VEC_COOKIE);PetscCheckSameComm(mat,x);} 337 if (b) {PetscValidHeaderSpecific(b,VEC_COOKIE);PetscCheckSameComm(mat,b);} 338 if (mat->ops->unscalesystem) { 339 ierr = (*mat->ops->unscalesystem)(mat,x,b);CHKERRQ(ierr); 340 } 341 PetscFunctionReturn(0); 342 } 343 344 #undef __FUNCT__ 345 #define __FUNCT__ "MatUseScaledForm" 346 /*@C 347 MatUseScaledForm - For matrix storage formats that scale the 348 matrix (for example MPIRowBS matrices are diagonally scaled on 349 assembly) indicates matrix operations (MatMult() etc) are 350 applied using the scaled matrix. 351 352 Collective on Mat 353 354 Input Parameter: 355 + mat - the matrix 356 - scaled - PETSC_TRUE for applying the scaled, PETSC_FALSE for 357 applying the original matrix 358 359 Notes: 360 For scaled matrix formats, applying the original, unscaled matrix 361 will be slightly more expensive 362 363 Level: Developer 364 365 .seealso: MatScaleSystem(), MatUnScaleSystem() 366 @*/ 367 int MatUseScaledForm(Mat mat,PetscTruth scaled) 368 { 369 int ierr; 370 371 PetscFunctionBegin; 372 PetscValidHeaderSpecific(mat,MAT_COOKIE); 373 PetscValidType(mat); 374 MatPreallocated(mat); 375 if (mat->ops->usescaledform) { 376 ierr = (*mat->ops->usescaledform)(mat,scaled);CHKERRQ(ierr); 377 } 378 PetscFunctionReturn(0); 379 } 380 381 #undef __FUNCT__ 382 #define __FUNCT__ "MatDestroy" 383 /*@C 384 MatDestroy - Frees space taken by a matrix. 385 386 Collective on Mat 387 388 Input Parameter: 389 . A - the matrix 390 391 Level: beginner 392 393 @*/ 394 int MatDestroy(Mat A) 395 { 396 int ierr; 397 398 PetscFunctionBegin; 399 PetscValidHeaderSpecific(A,MAT_COOKIE); 400 PetscValidType(A); 401 MatPreallocated(A); 402 if (--A->refct > 0) PetscFunctionReturn(0); 403 404 /* if memory was published with AMS then destroy it */ 405 ierr = PetscObjectDepublish(A);CHKERRQ(ierr); 406 if (A->mapping) { 407 ierr = ISLocalToGlobalMappingDestroy(A->mapping);CHKERRQ(ierr); 408 } 409 if (A->bmapping) { 410 ierr = ISLocalToGlobalMappingDestroy(A->bmapping);CHKERRQ(ierr); 411 } 412 if (A->rmap) { 413 ierr = PetscMapDestroy(A->rmap);CHKERRQ(ierr); 414 } 415 if (A->cmap) { 416 ierr = PetscMapDestroy(A->cmap);CHKERRQ(ierr); 417 } 418 419 ierr = (*A->ops->destroy)(A);CHKERRQ(ierr); 420 PetscLogObjectDestroy(A); 421 PetscHeaderDestroy(A); 422 PetscFunctionReturn(0); 423 } 424 425 #undef __FUNCT__ 426 #define __FUNCT__ "MatValid" 427 /*@ 428 MatValid - Checks whether a matrix object is valid. 429 430 Collective on Mat 431 432 Input Parameter: 433 . m - the matrix to check 434 435 Output Parameter: 436 flg - flag indicating matrix status, either 437 PETSC_TRUE if matrix is valid, or PETSC_FALSE otherwise. 438 439 Level: developer 440 441 Concepts: matrices^validity 442 @*/ 443 int MatValid(Mat m,PetscTruth *flg) 444 { 445 PetscFunctionBegin; 446 PetscValidIntPointer(flg); 447 if (!m) *flg = PETSC_FALSE; 448 else if (m->cookie != MAT_COOKIE) *flg = PETSC_FALSE; 449 else *flg = PETSC_TRUE; 450 PetscFunctionReturn(0); 451 } 452 453 #undef __FUNCT__ 454 #define __FUNCT__ "MatSetValues" 455 /*@ 456 MatSetValues - Inserts or adds a block of values into a matrix. 457 These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd() 458 MUST be called after all calls to MatSetValues() have been completed. 459 460 Not Collective 461 462 Input Parameters: 463 + mat - the matrix 464 . v - a logically two-dimensional array of values 465 . m, idxm - the number of rows and their global indices 466 . n, idxn - the number of columns and their global indices 467 - addv - either ADD_VALUES or INSERT_VALUES, where 468 ADD_VALUES adds values to any existing entries, and 469 INSERT_VALUES replaces existing entries with new values 470 471 Notes: 472 By default the values, v, are row-oriented and unsorted. 473 See MatSetOption() for other options. 474 475 Calls to MatSetValues() with the INSERT_VALUES and ADD_VALUES 476 options cannot be mixed without intervening calls to the assembly 477 routines. 478 479 MatSetValues() uses 0-based row and column numbers in Fortran 480 as well as in C. 481 482 Negative indices may be passed in idxm and idxn, these rows and columns are 483 simply ignored. This allows easily inserting element stiffness matrices 484 with homogeneous Dirchlet boundary conditions that you don't want represented 485 in the matrix. 486 487 Efficiency Alert: 488 The routine MatSetValuesBlocked() may offer much better efficiency 489 for users of block sparse formats (MATSEQBAIJ and MATMPIBAIJ). 490 491 Level: beginner 492 493 Concepts: matrices^putting entries in 494 495 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal() 496 @*/ 497 int MatSetValues(Mat mat,int m,int *idxm,int n,int *idxn,PetscScalar *v,InsertMode addv) 498 { 499 int ierr; 500 501 PetscFunctionBegin; 502 if (!m || !n) PetscFunctionReturn(0); /* no values to insert */ 503 PetscValidHeaderSpecific(mat,MAT_COOKIE); 504 PetscValidType(mat); 505 MatPreallocated(mat); 506 PetscValidIntPointer(idxm); 507 PetscValidIntPointer(idxn); 508 PetscValidScalarPointer(v); 509 if (mat->insertmode == NOT_SET_VALUES) { 510 mat->insertmode = addv; 511 } 512 #if defined(PETSC_USE_BOPT_g) 513 else if (mat->insertmode != addv) { 514 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values"); 515 } 516 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 517 #endif 518 519 if (mat->assembled) { 520 mat->was_assembled = PETSC_TRUE; 521 mat->assembled = PETSC_FALSE; 522 } 523 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 524 if (!mat->ops->setvalues) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 525 ierr = (*mat->ops->setvalues)(mat,m,idxm,n,idxn,v,addv);CHKERRQ(ierr); 526 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 527 PetscFunctionReturn(0); 528 } 529 530 #undef __FUNCT__ 531 #define __FUNCT__ "MatSetValuesStencil" 532 /*@C 533 MatSetValuesStencil - Inserts or adds a block of values into a matrix. 534 Using structured grid indexing 535 536 Not Collective 537 538 Input Parameters: 539 + mat - the matrix 540 . v - a logically two-dimensional array of values 541 . m - number of rows being entered 542 . idxm - grid coordinates (and component number when dof > 1) for matrix rows being entered 543 . n - number of columns being entered 544 . idxn - grid coordinates (and component number when dof > 1) for matrix columns being entered 545 - addv - either ADD_VALUES or INSERT_VALUES, where 546 ADD_VALUES adds values to any existing entries, and 547 INSERT_VALUES replaces existing entries with new values 548 549 Notes: 550 By default the values, v, are row-oriented and unsorted. 551 See MatSetOption() for other options. 552 553 Calls to MatSetValuesStencil() with the INSERT_VALUES and ADD_VALUES 554 options cannot be mixed without intervening calls to the assembly 555 routines. 556 557 The grid coordinates are across the entire grid, not just the local portion 558 559 MatSetValuesStencil() uses 0-based row and column numbers in Fortran 560 as well as in C. 561 562 In order to use this routine you must either obtain the matrix with DAGetMatrix() 563 or call MatSetLocalToGlobalMapping() and MatSetStencil() first. 564 565 In Fortran idxm and idxn should be declared as 566 $ MatStencil idxm(4,m),idxn(4,n) 567 and the values inserted using 568 $ idxm(MatStencil_i,1) = i 569 $ idxm(MatStencil_j,1) = j 570 $ idxm(MatStencil_k,1) = k 571 $ idxm(MatStencil_c,1) = c 572 etc 573 574 Negative indices may be passed in idxm and idxn, these rows and columns are 575 simply ignored. This allows easily inserting element stiffness matrices 576 with homogeneous Dirchlet boundary conditions that you don't want represented 577 in the matrix. 578 579 Inspired by the structured grid interface to the HYPRE package 580 (http://www.llnl.gov/CASC/hypre) 581 582 Efficiency Alert: 583 The routine MatSetValuesBlockedStencil() may offer much better efficiency 584 for users of block sparse formats (MATSEQBAIJ and MATMPIBAIJ). 585 586 Level: beginner 587 588 Concepts: matrices^putting entries in 589 590 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal() 591 MatSetValues(), MatSetValuesBlockedStencil(), MatSetStencil(), DAGetMatrix() 592 @*/ 593 int MatSetValuesStencil(Mat mat,int m,MatStencil *idxm,int n,MatStencil *idxn,PetscScalar *v,InsertMode addv) 594 { 595 int j,i,ierr,jdxm[128],jdxn[256],dim = mat->stencil.dim,*dims = mat->stencil.dims+1,tmp; 596 int *starts = mat->stencil.starts,*dxm = (int*)idxm,*dxn = (int*)idxn,sdim = dim - (1 - (int)mat->stencil.noc); 597 598 PetscFunctionBegin; 599 if (!m || !n) PetscFunctionReturn(0); /* no values to insert */ 600 PetscValidHeaderSpecific(mat,MAT_COOKIE); 601 PetscValidType(mat); 602 PetscValidIntPointer(idxm); 603 PetscValidIntPointer(idxn); 604 PetscValidScalarPointer(v); 605 606 if (m > 128) SETERRQ1(1,"Can only set 128 rows at a time; trying to set %d",m); 607 if (n > 128) SETERRQ1(1,"Can only set 256 columns at a time; trying to set %d",n); 608 609 for (i=0; i<m; i++) { 610 for (j=0; j<3-sdim; j++) dxm++; 611 tmp = *dxm++ - starts[0]; 612 for (j=0; j<dim-1; j++) { 613 tmp = tmp*dims[j] + *dxm++ - starts[j+1]; 614 } 615 if (mat->stencil.noc) dxm++; 616 jdxm[i] = tmp; 617 } 618 for (i=0; i<n; i++) { 619 for (j=0; j<3-sdim; j++) dxn++; 620 tmp = *dxn++ - starts[0]; 621 for (j=0; j<dim-1; j++) { 622 tmp = tmp*dims[j] + *dxn++ - starts[j+1]; 623 } 624 if (mat->stencil.noc) dxn++; 625 jdxn[i] = tmp; 626 } 627 ierr = MatSetValuesLocal(mat,m,jdxm,n,jdxn,v,addv);CHKERRQ(ierr); 628 PetscFunctionReturn(0); 629 } 630 631 #undef __FUNCT__ 632 #define __FUNCT__ "MatSetStencil" 633 /*@ 634 MatSetStencil - Sets the grid information for setting values into a matrix via 635 MatSetStencil() 636 637 Not Collective 638 639 Input Parameters: 640 + mat - the matrix 641 . dim - dimension of the grid 1,2, or 3 642 . dims - number of grid points in x, y, and z direction, including ghost points on your processor 643 . starts - starting point of ghost nodes on your processor in x, y, and z direction 644 - dof - number of degrees of freedom per node 645 646 647 Inspired by the structured grid interface to the HYPRE package 648 (www.llnl.gov/CASC/hyper) 649 650 Level: beginner 651 652 Concepts: matrices^putting entries in 653 654 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal() 655 MatSetValues(), MatSetValuesBlockedStencil(), MatSetValuesStencil() 656 @*/ 657 int MatSetStencil(Mat mat,int dim,int *dims,int *starts,int dof) 658 { 659 int i; 660 661 PetscFunctionBegin; 662 PetscValidHeaderSpecific(mat,MAT_COOKIE); 663 PetscValidIntPointer(dims); 664 PetscValidIntPointer(starts); 665 666 mat->stencil.dim = dim + (dof > 1); 667 for (i=0; i<dim; i++) { 668 mat->stencil.dims[i] = dims[dim-i-1]; /* copy the values in backwards */ 669 mat->stencil.starts[i] = starts[dim-i-1]; 670 } 671 mat->stencil.dims[dim] = dof; 672 mat->stencil.starts[dim] = 0; 673 mat->stencil.noc = (PetscTruth)(dof == 1); 674 PetscFunctionReturn(0); 675 } 676 677 #undef __FUNCT__ 678 #define __FUNCT__ "MatSetValuesBlocked" 679 /*@ 680 MatSetValuesBlocked - Inserts or adds a block of values into a matrix. 681 682 Not Collective 683 684 Input Parameters: 685 + mat - the matrix 686 . v - a logically two-dimensional array of values 687 . m, idxm - the number of block rows and their global block indices 688 . n, idxn - the number of block columns and their global block indices 689 - addv - either ADD_VALUES or INSERT_VALUES, where 690 ADD_VALUES adds values to any existing entries, and 691 INSERT_VALUES replaces existing entries with new values 692 693 Notes: 694 By default the values, v, are row-oriented and unsorted. So the layout of 695 v is the same as for MatSetValues(). See MatSetOption() for other options. 696 697 Calls to MatSetValuesBlocked() with the INSERT_VALUES and ADD_VALUES 698 options cannot be mixed without intervening calls to the assembly 699 routines. 700 701 MatSetValuesBlocked() uses 0-based row and column numbers in Fortran 702 as well as in C. 703 704 Negative indices may be passed in idxm and idxn, these rows and columns are 705 simply ignored. This allows easily inserting element stiffness matrices 706 with homogeneous Dirchlet boundary conditions that you don't want represented 707 in the matrix. 708 709 Each time an entry is set within a sparse matrix via MatSetValues(), 710 internal searching must be done to determine where to place the the 711 data in the matrix storage space. By instead inserting blocks of 712 entries via MatSetValuesBlocked(), the overhead of matrix assembly is 713 reduced. 714 715 Restrictions: 716 MatSetValuesBlocked() is currently supported only for the block AIJ 717 matrix format (MATSEQBAIJ and MATMPIBAIJ, which are created via 718 MatCreateSeqBAIJ() and MatCreateMPIBAIJ()). 719 720 Level: intermediate 721 722 Concepts: matrices^putting entries in blocked 723 724 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesBlockedLocal() 725 @*/ 726 int MatSetValuesBlocked(Mat mat,int m,int *idxm,int n,int *idxn,PetscScalar *v,InsertMode addv) 727 { 728 int ierr; 729 730 PetscFunctionBegin; 731 if (!m || !n) PetscFunctionReturn(0); /* no values to insert */ 732 PetscValidHeaderSpecific(mat,MAT_COOKIE); 733 PetscValidType(mat); 734 MatPreallocated(mat); 735 PetscValidIntPointer(idxm); 736 PetscValidIntPointer(idxn); 737 PetscValidScalarPointer(v); 738 if (mat->insertmode == NOT_SET_VALUES) { 739 mat->insertmode = addv; 740 } 741 #if defined(PETSC_USE_BOPT_g) 742 else if (mat->insertmode != addv) { 743 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values"); 744 } 745 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 746 #endif 747 748 if (mat->assembled) { 749 mat->was_assembled = PETSC_TRUE; 750 mat->assembled = PETSC_FALSE; 751 } 752 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 753 if (!mat->ops->setvaluesblocked) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 754 ierr = (*mat->ops->setvaluesblocked)(mat,m,idxm,n,idxn,v,addv);CHKERRQ(ierr); 755 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 756 PetscFunctionReturn(0); 757 } 758 759 /*MC 760 MatSetValue - Set a single entry into a matrix. 761 762 Synopsis: 763 int MatSetValue(Mat m,int row,int col,PetscScalar value,InsertMode mode); 764 765 Not collective 766 767 Input Parameters: 768 + m - the matrix 769 . row - the row location of the entry 770 . col - the column location of the entry 771 . value - the value to insert 772 - mode - either INSERT_VALUES or ADD_VALUES 773 774 Notes: 775 For efficiency one should use MatSetValues() and set several or many 776 values simultaneously if possible. 777 778 Note that VecSetValue() does NOT return an error code (since this 779 is checked internally). 780 781 Level: beginner 782 783 .seealso: MatSetValues() 784 M*/ 785 786 #undef __FUNCT__ 787 #define __FUNCT__ "MatGetValues" 788 /*@ 789 MatGetValues - Gets a block of values from a matrix. 790 791 Not Collective; currently only returns a local block 792 793 Input Parameters: 794 + mat - the matrix 795 . v - a logically two-dimensional array for storing the values 796 . m, idxm - the number of rows and their global indices 797 - n, idxn - the number of columns and their global indices 798 799 Notes: 800 The user must allocate space (m*n PetscScalars) for the values, v. 801 The values, v, are then returned in a row-oriented format, 802 analogous to that used by default in MatSetValues(). 803 804 MatGetValues() uses 0-based row and column numbers in 805 Fortran as well as in C. 806 807 MatGetValues() requires that the matrix has been assembled 808 with MatAssemblyBegin()/MatAssemblyEnd(). Thus, calls to 809 MatSetValues() and MatGetValues() CANNOT be made in succession 810 without intermediate matrix assembly. 811 812 Level: advanced 813 814 Concepts: matrices^accessing values 815 816 .seealso: MatGetRow(), MatGetSubmatrices(), MatSetValues() 817 @*/ 818 int MatGetValues(Mat mat,int m,int *idxm,int n,int *idxn,PetscScalar *v) 819 { 820 int ierr; 821 822 PetscFunctionBegin; 823 PetscValidHeaderSpecific(mat,MAT_COOKIE); 824 PetscValidType(mat); 825 MatPreallocated(mat); 826 PetscValidIntPointer(idxm); 827 PetscValidIntPointer(idxn); 828 PetscValidScalarPointer(v); 829 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 830 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 831 if (!mat->ops->getvalues) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 832 833 ierr = PetscLogEventBegin(MAT_GetValues,mat,0,0,0);CHKERRQ(ierr); 834 ierr = (*mat->ops->getvalues)(mat,m,idxm,n,idxn,v);CHKERRQ(ierr); 835 ierr = PetscLogEventEnd(MAT_GetValues,mat,0,0,0);CHKERRQ(ierr); 836 PetscFunctionReturn(0); 837 } 838 839 #undef __FUNCT__ 840 #define __FUNCT__ "MatSetLocalToGlobalMapping" 841 /*@ 842 MatSetLocalToGlobalMapping - Sets a local-to-global numbering for use by 843 the routine MatSetValuesLocal() to allow users to insert matrix entries 844 using a local (per-processor) numbering. 845 846 Not Collective 847 848 Input Parameters: 849 + x - the matrix 850 - mapping - mapping created with ISLocalToGlobalMappingCreate() 851 or ISLocalToGlobalMappingCreateIS() 852 853 Level: intermediate 854 855 Concepts: matrices^local to global mapping 856 Concepts: local to global mapping^for matrices 857 858 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesLocal() 859 @*/ 860 int MatSetLocalToGlobalMapping(Mat x,ISLocalToGlobalMapping mapping) 861 { 862 int ierr; 863 PetscFunctionBegin; 864 PetscValidHeaderSpecific(x,MAT_COOKIE); 865 PetscValidType(x); 866 MatPreallocated(x); 867 PetscValidHeaderSpecific(mapping,IS_LTOGM_COOKIE); 868 if (x->mapping) { 869 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Mapping already set for matrix"); 870 } 871 872 if (x->ops->setlocaltoglobalmapping) { 873 ierr = (*x->ops->setlocaltoglobalmapping)(x,mapping);CHKERRQ(ierr); 874 } else { 875 x->mapping = mapping; 876 ierr = PetscObjectReference((PetscObject)mapping);CHKERRQ(ierr); 877 } 878 PetscFunctionReturn(0); 879 } 880 881 #undef __FUNCT__ 882 #define __FUNCT__ "MatSetLocalToGlobalMappingBlock" 883 /*@ 884 MatSetLocalToGlobalMappingBlock - Sets a local-to-global numbering for use 885 by the routine MatSetValuesBlockedLocal() to allow users to insert matrix 886 entries using a local (per-processor) numbering. 887 888 Not Collective 889 890 Input Parameters: 891 + x - the matrix 892 - mapping - mapping created with ISLocalToGlobalMappingCreate() or 893 ISLocalToGlobalMappingCreateIS() 894 895 Level: intermediate 896 897 Concepts: matrices^local to global mapping blocked 898 Concepts: local to global mapping^for matrices, blocked 899 900 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesBlockedLocal(), 901 MatSetValuesBlocked(), MatSetValuesLocal() 902 @*/ 903 int MatSetLocalToGlobalMappingBlock(Mat x,ISLocalToGlobalMapping mapping) 904 { 905 int ierr; 906 PetscFunctionBegin; 907 PetscValidHeaderSpecific(x,MAT_COOKIE); 908 PetscValidType(x); 909 MatPreallocated(x); 910 PetscValidHeaderSpecific(mapping,IS_LTOGM_COOKIE); 911 if (x->bmapping) { 912 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Mapping already set for matrix"); 913 } 914 915 x->bmapping = mapping; 916 ierr = PetscObjectReference((PetscObject)mapping);CHKERRQ(ierr); 917 PetscFunctionReturn(0); 918 } 919 920 #undef __FUNCT__ 921 #define __FUNCT__ "MatSetValuesLocal" 922 /*@ 923 MatSetValuesLocal - Inserts or adds values into certain locations of a matrix, 924 using a local ordering of the nodes. 925 926 Not Collective 927 928 Input Parameters: 929 + x - the matrix 930 . nrow, irow - number of rows and their local indices 931 . ncol, icol - number of columns and their local indices 932 . y - a logically two-dimensional array of values 933 - addv - either INSERT_VALUES or ADD_VALUES, where 934 ADD_VALUES adds values to any existing entries, and 935 INSERT_VALUES replaces existing entries with new values 936 937 Notes: 938 Before calling MatSetValuesLocal(), the user must first set the 939 local-to-global mapping by calling MatSetLocalToGlobalMapping(). 940 941 Calls to MatSetValuesLocal() with the INSERT_VALUES and ADD_VALUES 942 options cannot be mixed without intervening calls to the assembly 943 routines. 944 945 These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd() 946 MUST be called after all calls to MatSetValuesLocal() have been completed. 947 948 Level: intermediate 949 950 Concepts: matrices^putting entries in with local numbering 951 952 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetLocalToGlobalMapping(), 953 MatSetValueLocal() 954 @*/ 955 int MatSetValuesLocal(Mat mat,int nrow,int *irow,int ncol,int *icol,PetscScalar *y,InsertMode addv) 956 { 957 int ierr,irowm[2048],icolm[2048]; 958 959 PetscFunctionBegin; 960 PetscValidHeaderSpecific(mat,MAT_COOKIE); 961 PetscValidType(mat); 962 MatPreallocated(mat); 963 PetscValidIntPointer(irow); 964 PetscValidIntPointer(icol); 965 PetscValidScalarPointer(y); 966 967 if (mat->insertmode == NOT_SET_VALUES) { 968 mat->insertmode = addv; 969 } 970 #if defined(PETSC_USE_BOPT_g) 971 else if (mat->insertmode != addv) { 972 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values"); 973 } 974 if (!mat->ops->setvalueslocal && (nrow > 2048 || ncol > 2048)) { 975 SETERRQ2(PETSC_ERR_SUP,"Number column/row indices must be <= 2048: are %d %d",nrow,ncol); 976 } 977 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 978 #endif 979 980 if (mat->assembled) { 981 mat->was_assembled = PETSC_TRUE; 982 mat->assembled = PETSC_FALSE; 983 } 984 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 985 if (!mat->ops->setvalueslocal) { 986 ierr = ISLocalToGlobalMappingApply(mat->mapping,nrow,irow,irowm);CHKERRQ(ierr); 987 ierr = ISLocalToGlobalMappingApply(mat->mapping,ncol,icol,icolm);CHKERRQ(ierr); 988 ierr = (*mat->ops->setvalues)(mat,nrow,irowm,ncol,icolm,y,addv);CHKERRQ(ierr); 989 } else { 990 ierr = (*mat->ops->setvalueslocal)(mat,nrow,irow,ncol,icol,y,addv);CHKERRQ(ierr); 991 } 992 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 993 PetscFunctionReturn(0); 994 } 995 996 #undef __FUNCT__ 997 #define __FUNCT__ "MatSetValuesBlockedLocal" 998 /*@ 999 MatSetValuesBlockedLocal - Inserts or adds values into certain locations of a matrix, 1000 using a local ordering of the nodes a block at a time. 1001 1002 Not Collective 1003 1004 Input Parameters: 1005 + x - the matrix 1006 . nrow, irow - number of rows and their local indices 1007 . ncol, icol - number of columns and their local indices 1008 . y - a logically two-dimensional array of values 1009 - addv - either INSERT_VALUES or ADD_VALUES, where 1010 ADD_VALUES adds values to any existing entries, and 1011 INSERT_VALUES replaces existing entries with new values 1012 1013 Notes: 1014 Before calling MatSetValuesBlockedLocal(), the user must first set the 1015 local-to-global mapping by calling MatSetLocalToGlobalMappingBlock(), 1016 where the mapping MUST be set for matrix blocks, not for matrix elements. 1017 1018 Calls to MatSetValuesBlockedLocal() with the INSERT_VALUES and ADD_VALUES 1019 options cannot be mixed without intervening calls to the assembly 1020 routines. 1021 1022 These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd() 1023 MUST be called after all calls to MatSetValuesBlockedLocal() have been completed. 1024 1025 Level: intermediate 1026 1027 Concepts: matrices^putting blocked values in with local numbering 1028 1029 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesLocal(), MatSetLocalToGlobalMappingBlock(), MatSetValuesBlocked() 1030 @*/ 1031 int MatSetValuesBlockedLocal(Mat mat,int nrow,int *irow,int ncol,int *icol,PetscScalar *y,InsertMode addv) 1032 { 1033 int ierr,irowm[2048],icolm[2048]; 1034 1035 PetscFunctionBegin; 1036 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1037 PetscValidType(mat); 1038 MatPreallocated(mat); 1039 PetscValidIntPointer(irow); 1040 PetscValidIntPointer(icol); 1041 PetscValidScalarPointer(y); 1042 if (mat->insertmode == NOT_SET_VALUES) { 1043 mat->insertmode = addv; 1044 } 1045 #if defined(PETSC_USE_BOPT_g) 1046 else if (mat->insertmode != addv) { 1047 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values"); 1048 } 1049 if (!mat->bmapping) { 1050 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Local to global never set with MatSetLocalToGlobalMappingBlock()"); 1051 } 1052 if (nrow > 2048 || ncol > 2048) { 1053 SETERRQ2(PETSC_ERR_SUP,"Number column/row indices must be <= 2048: are %d %d",nrow,ncol); 1054 } 1055 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1056 #endif 1057 1058 if (mat->assembled) { 1059 mat->was_assembled = PETSC_TRUE; 1060 mat->assembled = PETSC_FALSE; 1061 } 1062 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 1063 ierr = ISLocalToGlobalMappingApply(mat->bmapping,nrow,irow,irowm);CHKERRQ(ierr); 1064 ierr = ISLocalToGlobalMappingApply(mat->bmapping,ncol,icol,icolm);CHKERRQ(ierr); 1065 ierr = (*mat->ops->setvaluesblocked)(mat,nrow,irowm,ncol,icolm,y,addv);CHKERRQ(ierr); 1066 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 1067 PetscFunctionReturn(0); 1068 } 1069 1070 /* --------------------------------------------------------*/ 1071 #undef __FUNCT__ 1072 #define __FUNCT__ "MatMult" 1073 /*@ 1074 MatMult - Computes the matrix-vector product, y = Ax. 1075 1076 Collective on Mat and Vec 1077 1078 Input Parameters: 1079 + mat - the matrix 1080 - x - the vector to be multilplied 1081 1082 Output Parameters: 1083 . y - the result 1084 1085 Notes: 1086 The vectors x and y cannot be the same. I.e., one cannot 1087 call MatMult(A,y,y). 1088 1089 Level: beginner 1090 1091 Concepts: matrix-vector product 1092 1093 .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd() 1094 @*/ 1095 int MatMult(Mat mat,Vec x,Vec y) 1096 { 1097 int ierr; 1098 1099 PetscFunctionBegin; 1100 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1101 PetscValidType(mat); 1102 MatPreallocated(mat); 1103 PetscValidHeaderSpecific(x,VEC_COOKIE); 1104 PetscValidHeaderSpecific(y,VEC_COOKIE); 1105 1106 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1107 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1108 if (x == y) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors"); 1109 #ifndef PETSC_HAVE_CONSTRAINTS 1110 if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N); 1111 if (mat->M != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %d %d",mat->M,y->N); 1112 if (mat->m != y->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: local dim %d %d",mat->m,y->n); 1113 #endif 1114 1115 if (mat->nullsp) { 1116 ierr = MatNullSpaceRemove(mat->nullsp,x,&x);CHKERRQ(ierr); 1117 } 1118 1119 ierr = PetscLogEventBegin(MAT_Mult,mat,x,y,0);CHKERRQ(ierr); 1120 ierr = (*mat->ops->mult)(mat,x,y);CHKERRQ(ierr); 1121 ierr = PetscLogEventEnd(MAT_Mult,mat,x,y,0);CHKERRQ(ierr); 1122 1123 if (mat->nullsp) { 1124 ierr = MatNullSpaceRemove(mat->nullsp,y,PETSC_NULL);CHKERRQ(ierr); 1125 } 1126 PetscFunctionReturn(0); 1127 } 1128 1129 #undef __FUNCT__ 1130 #define __FUNCT__ "MatMultTranspose" 1131 /*@ 1132 MatMultTranspose - Computes matrix transpose times a vector. 1133 1134 Collective on Mat and Vec 1135 1136 Input Parameters: 1137 + mat - the matrix 1138 - x - the vector to be multilplied 1139 1140 Output Parameters: 1141 . y - the result 1142 1143 Notes: 1144 The vectors x and y cannot be the same. I.e., one cannot 1145 call MatMultTranspose(A,y,y). 1146 1147 Level: beginner 1148 1149 Concepts: matrix vector product^transpose 1150 1151 .seealso: MatMult(), MatMultAdd(), MatMultTransposeAdd() 1152 @*/ 1153 int MatMultTranspose(Mat mat,Vec x,Vec y) 1154 { 1155 int ierr; 1156 1157 PetscFunctionBegin; 1158 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1159 PetscValidType(mat); 1160 MatPreallocated(mat); 1161 PetscValidHeaderSpecific(x,VEC_COOKIE); 1162 PetscValidHeaderSpecific(y,VEC_COOKIE); 1163 1164 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1165 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1166 if (x == y) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors"); 1167 #ifndef PETSC_HAVE_CONSTRAINTS 1168 if (mat->M != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->M,x->N); 1169 if (mat->N != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %d %d",mat->N,y->N); 1170 #endif 1171 1172 if (!mat->ops->multtranspose) SETERRQ(PETSC_ERR_SUP, "Operation not supported"); 1173 ierr = PetscLogEventBegin(MAT_MultTranspose,mat,x,y,0);CHKERRQ(ierr); 1174 if (!mat->ops->multtranspose) SETERRQ(PETSC_ERR_SUP,"This matrix type does not have a multiply tranpose defined"); 1175 ierr = (*mat->ops->multtranspose)(mat,x,y);CHKERRQ(ierr); 1176 ierr = PetscLogEventEnd(MAT_MultTranspose,mat,x,y,0);CHKERRQ(ierr); 1177 PetscFunctionReturn(0); 1178 } 1179 1180 #undef __FUNCT__ 1181 #define __FUNCT__ "MatMultAdd" 1182 /*@ 1183 MatMultAdd - Computes v3 = v2 + A * v1. 1184 1185 Collective on Mat and Vec 1186 1187 Input Parameters: 1188 + mat - the matrix 1189 - v1, v2 - the vectors 1190 1191 Output Parameters: 1192 . v3 - the result 1193 1194 Notes: 1195 The vectors v1 and v3 cannot be the same. I.e., one cannot 1196 call MatMultAdd(A,v1,v2,v1). 1197 1198 Level: beginner 1199 1200 Concepts: matrix vector product^addition 1201 1202 .seealso: MatMultTranspose(), MatMult(), MatMultTransposeAdd() 1203 @*/ 1204 int MatMultAdd(Mat mat,Vec v1,Vec v2,Vec v3) 1205 { 1206 int ierr; 1207 1208 PetscFunctionBegin; 1209 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1210 PetscValidType(mat); 1211 MatPreallocated(mat); 1212 PetscValidHeaderSpecific(v1,VEC_COOKIE); 1213 PetscValidHeaderSpecific(v2,VEC_COOKIE); 1214 PetscValidHeaderSpecific(v3,VEC_COOKIE); 1215 1216 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1217 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1218 if (mat->N != v1->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v1: global dim %d %d",mat->N,v1->N); 1219 if (mat->M != v2->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v2: global dim %d %d",mat->M,v2->N); 1220 if (mat->M != v3->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v3: global dim %d %d",mat->M,v3->N); 1221 if (mat->m != v3->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v3: local dim %d %d",mat->m,v3->n); 1222 if (mat->m != v2->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v2: local dim %d %d",mat->m,v2->n); 1223 if (v1 == v3) SETERRQ(PETSC_ERR_ARG_IDN,"v1 and v3 must be different vectors"); 1224 1225 ierr = PetscLogEventBegin(MAT_MultAdd,mat,v1,v2,v3);CHKERRQ(ierr); 1226 ierr = (*mat->ops->multadd)(mat,v1,v2,v3);CHKERRQ(ierr); 1227 ierr = PetscLogEventEnd(MAT_MultAdd,mat,v1,v2,v3);CHKERRQ(ierr); 1228 PetscFunctionReturn(0); 1229 } 1230 1231 #undef __FUNCT__ 1232 #define __FUNCT__ "MatMultTransposeAdd" 1233 /*@ 1234 MatMultTransposeAdd - Computes v3 = v2 + A' * v1. 1235 1236 Collective on Mat and Vec 1237 1238 Input Parameters: 1239 + mat - the matrix 1240 - v1, v2 - the vectors 1241 1242 Output Parameters: 1243 . v3 - the result 1244 1245 Notes: 1246 The vectors v1 and v3 cannot be the same. I.e., one cannot 1247 call MatMultTransposeAdd(A,v1,v2,v1). 1248 1249 Level: beginner 1250 1251 Concepts: matrix vector product^transpose and addition 1252 1253 .seealso: MatMultTranspose(), MatMultAdd(), MatMult() 1254 @*/ 1255 int MatMultTransposeAdd(Mat mat,Vec v1,Vec v2,Vec v3) 1256 { 1257 int ierr; 1258 1259 PetscFunctionBegin; 1260 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1261 PetscValidType(mat); 1262 MatPreallocated(mat); 1263 PetscValidHeaderSpecific(v1,VEC_COOKIE); 1264 PetscValidHeaderSpecific(v2,VEC_COOKIE); 1265 PetscValidHeaderSpecific(v3,VEC_COOKIE); 1266 1267 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1268 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1269 if (!mat->ops->multtransposeadd) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 1270 if (v1 == v3) SETERRQ(PETSC_ERR_ARG_IDN,"v1 and v3 must be different vectors"); 1271 if (mat->M != v1->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v1: global dim %d %d",mat->M,v1->N); 1272 if (mat->N != v2->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v2: global dim %d %d",mat->N,v2->N); 1273 if (mat->N != v3->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v3: global dim %d %d",mat->N,v3->N); 1274 1275 ierr = PetscLogEventBegin(MAT_MultTransposeAdd,mat,v1,v2,v3);CHKERRQ(ierr); 1276 ierr = (*mat->ops->multtransposeadd)(mat,v1,v2,v3);CHKERRQ(ierr); 1277 ierr = PetscLogEventEnd(MAT_MultTransposeAdd,mat,v1,v2,v3);CHKERRQ(ierr); 1278 PetscFunctionReturn(0); 1279 } 1280 1281 #undef __FUNCT__ 1282 #define __FUNCT__ "MatMultConstrained" 1283 /*@ 1284 MatMultConstrained - The inner multiplication routine for a 1285 constrained matrix P^T A P. 1286 1287 Collective on Mat and Vec 1288 1289 Input Parameters: 1290 + mat - the matrix 1291 - x - the vector to be multilplied 1292 1293 Output Parameters: 1294 . y - the result 1295 1296 Notes: 1297 The vectors x and y cannot be the same. I.e., one cannot 1298 call MatMult(A,y,y). 1299 1300 Level: beginner 1301 1302 .keywords: matrix, multiply, matrix-vector product, constraint 1303 .seealso: MatMult(), MatMultTrans(), MatMultAdd(), MatMultTransAdd() 1304 @*/ 1305 int MatMultConstrained(Mat mat,Vec x,Vec y) 1306 { 1307 int ierr; 1308 1309 PetscFunctionBegin; 1310 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1311 PetscValidHeaderSpecific(x,VEC_COOKIE);PetscValidHeaderSpecific(y,VEC_COOKIE); 1312 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1313 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1314 if (x == y) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors"); 1315 if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N); 1316 if (mat->M != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %d %d",mat->M,y->N); 1317 if (mat->m != y->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: local dim %d %d",mat->m,y->n); 1318 1319 ierr = PetscLogEventBegin(MAT_MultConstrained,mat,x,y,0);CHKERRQ(ierr); 1320 ierr = (*mat->ops->multconstrained)(mat,x,y); CHKERRQ(ierr); 1321 ierr = PetscLogEventEnd(MAT_MultConstrained,mat,x,y,0);CHKERRQ(ierr); 1322 1323 PetscFunctionReturn(0); 1324 } 1325 1326 #undef __FUNCT__ 1327 #define __FUNCT__ "MatMultTransposeConstrained" 1328 /*@ 1329 MatMultTransposeConstrained - The inner multiplication routine for a 1330 constrained matrix P^T A^T P. 1331 1332 Collective on Mat and Vec 1333 1334 Input Parameters: 1335 + mat - the matrix 1336 - x - the vector to be multilplied 1337 1338 Output Parameters: 1339 . y - the result 1340 1341 Notes: 1342 The vectors x and y cannot be the same. I.e., one cannot 1343 call MatMult(A,y,y). 1344 1345 Level: beginner 1346 1347 .keywords: matrix, multiply, matrix-vector product, constraint 1348 .seealso: MatMult(), MatMultTrans(), MatMultAdd(), MatMultTransAdd() 1349 @*/ 1350 int MatMultTransposeConstrained(Mat mat,Vec x,Vec y) 1351 { 1352 int ierr; 1353 1354 PetscFunctionBegin; 1355 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1356 PetscValidHeaderSpecific(x,VEC_COOKIE);PetscValidHeaderSpecific(y,VEC_COOKIE); 1357 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1358 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1359 if (x == y) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors"); 1360 if (mat->M != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N); 1361 if (mat->N != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %d %d",mat->M,y->N); 1362 1363 ierr = PetscLogEventBegin(MAT_MultConstrained,mat,x,y,0);CHKERRQ(ierr); 1364 ierr = (*mat->ops->multtransposeconstrained)(mat,x,y);CHKERRQ(ierr); 1365 ierr = PetscLogEventEnd(MAT_MultConstrained,mat,x,y,0);CHKERRQ(ierr); 1366 1367 PetscFunctionReturn(0); 1368 } 1369 /* ------------------------------------------------------------*/ 1370 #undef __FUNCT__ 1371 #define __FUNCT__ "MatGetInfo" 1372 /*@C 1373 MatGetInfo - Returns information about matrix storage (number of 1374 nonzeros, memory, etc.). 1375 1376 Collective on Mat if MAT_GLOBAL_MAX or MAT_GLOBAL_SUM is used 1377 as the flag 1378 1379 Input Parameters: 1380 . mat - the matrix 1381 1382 Output Parameters: 1383 + flag - flag indicating the type of parameters to be returned 1384 (MAT_LOCAL - local matrix, MAT_GLOBAL_MAX - maximum over all processors, 1385 MAT_GLOBAL_SUM - sum over all processors) 1386 - info - matrix information context 1387 1388 Notes: 1389 The MatInfo context contains a variety of matrix data, including 1390 number of nonzeros allocated and used, number of mallocs during 1391 matrix assembly, etc. Additional information for factored matrices 1392 is provided (such as the fill ratio, number of mallocs during 1393 factorization, etc.). Much of this info is printed to STDOUT 1394 when using the runtime options 1395 $ -log_info -mat_view_info 1396 1397 Example for C/C++ Users: 1398 See the file ${PETSC_DIR}/include/petscmat.h for a complete list of 1399 data within the MatInfo context. For example, 1400 .vb 1401 MatInfo info; 1402 Mat A; 1403 double mal, nz_a, nz_u; 1404 1405 MatGetInfo(A,MAT_LOCAL,&info); 1406 mal = info.mallocs; 1407 nz_a = info.nz_allocated; 1408 .ve 1409 1410 Example for Fortran Users: 1411 Fortran users should declare info as a double precision 1412 array of dimension MAT_INFO_SIZE, and then extract the parameters 1413 of interest. See the file ${PETSC_DIR}/include/finclude/petscmat.h 1414 a complete list of parameter names. 1415 .vb 1416 double precision info(MAT_INFO_SIZE) 1417 double precision mal, nz_a 1418 Mat A 1419 integer ierr 1420 1421 call MatGetInfo(A,MAT_LOCAL,info,ierr) 1422 mal = info(MAT_INFO_MALLOCS) 1423 nz_a = info(MAT_INFO_NZ_ALLOCATED) 1424 .ve 1425 1426 Level: intermediate 1427 1428 Concepts: matrices^getting information on 1429 1430 @*/ 1431 int MatGetInfo(Mat mat,MatInfoType flag,MatInfo *info) 1432 { 1433 int ierr; 1434 1435 PetscFunctionBegin; 1436 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1437 PetscValidType(mat); 1438 MatPreallocated(mat); 1439 PetscValidPointer(info); 1440 if (!mat->ops->getinfo) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 1441 ierr = (*mat->ops->getinfo)(mat,flag,info);CHKERRQ(ierr); 1442 PetscFunctionReturn(0); 1443 } 1444 1445 /* ----------------------------------------------------------*/ 1446 #undef __FUNCT__ 1447 #define __FUNCT__ "MatILUDTFactor" 1448 /*@C 1449 MatILUDTFactor - Performs a drop tolerance ILU factorization. 1450 1451 Collective on Mat 1452 1453 Input Parameters: 1454 + mat - the matrix 1455 . info - information about the factorization to be done 1456 . row - row permutation 1457 - col - column permutation 1458 1459 Output Parameters: 1460 . fact - the factored matrix 1461 1462 Level: developer 1463 1464 Notes: 1465 Most users should employ the simplified SLES interface for linear solvers 1466 instead of working directly with matrix algebra routines such as this. 1467 See, e.g., SLESCreate(). 1468 1469 This is currently only supported for the SeqAIJ matrix format using code 1470 from Yousef Saad's SPARSEKIT2 package (translated to C with f2c) and/or 1471 Matlab. SPARSEKIT2 is copyrighted by Yousef Saad with the GNU copyright 1472 and thus can be distributed with PETSc. 1473 1474 Concepts: matrices^ILUDT factorization 1475 1476 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor(), MatILUInfo 1477 @*/ 1478 int MatILUDTFactor(Mat mat,MatILUInfo *info,IS row,IS col,Mat *fact) 1479 { 1480 int ierr; 1481 1482 PetscFunctionBegin; 1483 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1484 PetscValidType(mat); 1485 MatPreallocated(mat); 1486 PetscValidPointer(fact); 1487 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1488 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1489 if (!mat->ops->iludtfactor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 1490 1491 ierr = PetscLogEventBegin(MAT_ILUFactor,mat,row,col,0);CHKERRQ(ierr); 1492 ierr = (*mat->ops->iludtfactor)(mat,info,row,col,fact);CHKERRQ(ierr); 1493 ierr = PetscLogEventEnd(MAT_ILUFactor,mat,row,col,0);CHKERRQ(ierr); 1494 1495 PetscFunctionReturn(0); 1496 } 1497 1498 #undef __FUNCT__ 1499 #define __FUNCT__ "MatLUFactor" 1500 /*@ 1501 MatLUFactor - Performs in-place LU factorization of matrix. 1502 1503 Collective on Mat 1504 1505 Input Parameters: 1506 + mat - the matrix 1507 . row - row permutation 1508 . col - column permutation 1509 - info - options for factorization, includes 1510 $ fill - expected fill as ratio of original fill. 1511 $ dtcol - pivot tolerance (0 no pivot, 1 full column pivoting) 1512 $ Run with the option -log_info to determine an optimal value to use 1513 1514 Notes: 1515 Most users should employ the simplified SLES interface for linear solvers 1516 instead of working directly with matrix algebra routines such as this. 1517 See, e.g., SLESCreate(). 1518 1519 This changes the state of the matrix to a factored matrix; it cannot be used 1520 for example with MatSetValues() unless one first calls MatSetUnfactored(). 1521 1522 Level: developer 1523 1524 Concepts: matrices^LU factorization 1525 1526 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor(), 1527 MatGetOrdering(), MatSetUnfactored(), MatLUInfo 1528 1529 @*/ 1530 int MatLUFactor(Mat mat,IS row,IS col,MatLUInfo *info) 1531 { 1532 int ierr; 1533 1534 PetscFunctionBegin; 1535 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1536 PetscValidType(mat); 1537 MatPreallocated(mat); 1538 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1539 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1540 if (!mat->ops->lufactor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 1541 1542 ierr = PetscLogEventBegin(MAT_LUFactor,mat,row,col,0);CHKERRQ(ierr); 1543 ierr = (*mat->ops->lufactor)(mat,row,col,info);CHKERRQ(ierr); 1544 ierr = PetscLogEventEnd(MAT_LUFactor,mat,row,col,0);CHKERRQ(ierr); 1545 PetscFunctionReturn(0); 1546 } 1547 1548 #undef __FUNCT__ 1549 #define __FUNCT__ "MatILUFactor" 1550 /*@ 1551 MatILUFactor - Performs in-place ILU factorization of matrix. 1552 1553 Collective on Mat 1554 1555 Input Parameters: 1556 + mat - the matrix 1557 . row - row permutation 1558 . col - column permutation 1559 - info - structure containing 1560 $ levels - number of levels of fill. 1561 $ expected fill - as ratio of original fill. 1562 $ 1 or 0 - indicating force fill on diagonal (improves robustness for matrices 1563 missing diagonal entries) 1564 1565 Notes: 1566 Probably really in-place only when level of fill is zero, otherwise allocates 1567 new space to store factored matrix and deletes previous memory. 1568 1569 Most users should employ the simplified SLES interface for linear solvers 1570 instead of working directly with matrix algebra routines such as this. 1571 See, e.g., SLESCreate(). 1572 1573 Level: developer 1574 1575 Concepts: matrices^ILU factorization 1576 1577 .seealso: MatILUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor(), MatILUInfo 1578 @*/ 1579 int MatILUFactor(Mat mat,IS row,IS col,MatILUInfo *info) 1580 { 1581 int ierr; 1582 1583 PetscFunctionBegin; 1584 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1585 PetscValidType(mat); 1586 MatPreallocated(mat); 1587 if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,"matrix must be square"); 1588 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1589 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1590 if (!mat->ops->ilufactor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 1591 1592 ierr = PetscLogEventBegin(MAT_ILUFactor,mat,row,col,0);CHKERRQ(ierr); 1593 ierr = (*mat->ops->ilufactor)(mat,row,col,info);CHKERRQ(ierr); 1594 ierr = PetscLogEventEnd(MAT_ILUFactor,mat,row,col,0);CHKERRQ(ierr); 1595 PetscFunctionReturn(0); 1596 } 1597 1598 #undef __FUNCT__ 1599 #define __FUNCT__ "MatLUFactorSymbolic" 1600 /*@ 1601 MatLUFactorSymbolic - Performs symbolic LU factorization of matrix. 1602 Call this routine before calling MatLUFactorNumeric(). 1603 1604 Collective on Mat 1605 1606 Input Parameters: 1607 + mat - the matrix 1608 . row, col - row and column permutations 1609 - info - options for factorization, includes 1610 $ fill - expected fill as ratio of original fill. 1611 $ dtcol - pivot tolerance (0 no pivot, 1 full column pivoting) 1612 $ Run with the option -log_info to determine an optimal value to use 1613 1614 Output Parameter: 1615 . fact - new matrix that has been symbolically factored 1616 1617 Notes: 1618 See the users manual for additional information about 1619 choosing the fill factor for better efficiency. 1620 1621 Most users should employ the simplified SLES interface for linear solvers 1622 instead of working directly with matrix algebra routines such as this. 1623 See, e.g., SLESCreate(). 1624 1625 Level: developer 1626 1627 Concepts: matrices^LU symbolic factorization 1628 1629 .seealso: MatLUFactor(), MatLUFactorNumeric(), MatCholeskyFactor(), MatLUInfo 1630 @*/ 1631 int MatLUFactorSymbolic(Mat mat,IS row,IS col,MatLUInfo *info,Mat *fact) 1632 { 1633 int ierr; 1634 1635 PetscFunctionBegin; 1636 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1637 PetscValidType(mat); 1638 MatPreallocated(mat); 1639 PetscValidPointer(fact); 1640 PetscValidHeaderSpecific(row,IS_COOKIE); 1641 PetscValidHeaderSpecific(col,IS_COOKIE); 1642 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1643 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1644 if (!mat->ops->lufactorsymbolic) SETERRQ1(PETSC_ERR_SUP,"Matrix type %s symbolic LU",mat->type_name); 1645 1646 ierr = PetscLogEventBegin(MAT_LUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr); 1647 ierr = (*mat->ops->lufactorsymbolic)(mat,row,col,info,fact);CHKERRQ(ierr); 1648 ierr = PetscLogEventEnd(MAT_LUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr); 1649 PetscFunctionReturn(0); 1650 } 1651 1652 #undef __FUNCT__ 1653 #define __FUNCT__ "MatLUFactorNumeric" 1654 /*@ 1655 MatLUFactorNumeric - Performs numeric LU factorization of a matrix. 1656 Call this routine after first calling MatLUFactorSymbolic(). 1657 1658 Collective on Mat 1659 1660 Input Parameters: 1661 + mat - the matrix 1662 - fact - the matrix generated for the factor, from MatLUFactorSymbolic() 1663 1664 Notes: 1665 See MatLUFactor() for in-place factorization. See 1666 MatCholeskyFactorNumeric() for the symmetric, positive definite case. 1667 1668 Most users should employ the simplified SLES interface for linear solvers 1669 instead of working directly with matrix algebra routines such as this. 1670 See, e.g., SLESCreate(). 1671 1672 Level: developer 1673 1674 Concepts: matrices^LU numeric factorization 1675 1676 .seealso: MatLUFactorSymbolic(), MatLUFactor(), MatCholeskyFactor() 1677 @*/ 1678 int MatLUFactorNumeric(Mat mat,Mat *fact) 1679 { 1680 int ierr; 1681 PetscTruth flg; 1682 1683 PetscFunctionBegin; 1684 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1685 PetscValidType(mat); 1686 MatPreallocated(mat); 1687 PetscValidPointer(fact); 1688 PetscValidHeaderSpecific(*fact,MAT_COOKIE); 1689 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1690 if (mat->M != (*fact)->M || mat->N != (*fact)->N) { 1691 SETERRQ4(PETSC_ERR_ARG_SIZ,"Mat mat,Mat *fact: global dimensions are different %d should = %d %d should = %d", 1692 mat->M,(*fact)->M,mat->N,(*fact)->N); 1693 } 1694 if (!(*fact)->ops->lufactornumeric) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 1695 1696 ierr = PetscLogEventBegin(MAT_LUFactorNumeric,mat,*fact,0,0);CHKERRQ(ierr); 1697 ierr = (*(*fact)->ops->lufactornumeric)(mat,fact);CHKERRQ(ierr); 1698 ierr = PetscLogEventEnd(MAT_LUFactorNumeric,mat,*fact,0,0);CHKERRQ(ierr); 1699 ierr = PetscOptionsHasName(PETSC_NULL,"-mat_view_draw",&flg);CHKERRQ(ierr); 1700 if (flg) { 1701 ierr = PetscOptionsHasName(PETSC_NULL,"-mat_view_contour",&flg);CHKERRQ(ierr); 1702 if (flg) { 1703 ierr = PetscViewerPushFormat(PETSC_VIEWER_DRAW_(mat->comm),PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr); 1704 } 1705 ierr = MatView(*fact,PETSC_VIEWER_DRAW_(mat->comm));CHKERRQ(ierr); 1706 ierr = PetscViewerFlush(PETSC_VIEWER_DRAW_(mat->comm));CHKERRQ(ierr); 1707 if (flg) { 1708 ierr = PetscViewerPopFormat(PETSC_VIEWER_DRAW_(mat->comm));CHKERRQ(ierr); 1709 } 1710 } 1711 PetscFunctionReturn(0); 1712 } 1713 1714 #undef __FUNCT__ 1715 #define __FUNCT__ "MatCholeskyFactor" 1716 /*@ 1717 MatCholeskyFactor - Performs in-place Cholesky factorization of a 1718 symmetric matrix. 1719 1720 Collective on Mat 1721 1722 Input Parameters: 1723 + mat - the matrix 1724 . perm - row and column permutations 1725 - f - expected fill as ratio of original fill 1726 1727 Notes: 1728 See MatLUFactor() for the nonsymmetric case. See also 1729 MatCholeskyFactorSymbolic(), and MatCholeskyFactorNumeric(). 1730 1731 Most users should employ the simplified SLES interface for linear solvers 1732 instead of working directly with matrix algebra routines such as this. 1733 See, e.g., SLESCreate(). 1734 1735 Level: developer 1736 1737 Concepts: matrices^Cholesky factorization 1738 1739 .seealso: MatLUFactor(), MatCholeskyFactorSymbolic(), MatCholeskyFactorNumeric() 1740 MatGetOrdering() 1741 1742 @*/ 1743 int MatCholeskyFactor(Mat mat,IS perm,PetscReal f) 1744 { 1745 int ierr; 1746 1747 PetscFunctionBegin; 1748 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1749 PetscValidType(mat); 1750 MatPreallocated(mat); 1751 PetscValidHeaderSpecific(perm,IS_COOKIE); 1752 if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,"Matrix must be square"); 1753 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1754 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1755 if (!mat->ops->choleskyfactor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 1756 1757 ierr = PetscLogEventBegin(MAT_CholeskyFactor,mat,perm,0,0);CHKERRQ(ierr); 1758 ierr = (*mat->ops->choleskyfactor)(mat,perm,f);CHKERRQ(ierr); 1759 ierr = PetscLogEventEnd(MAT_CholeskyFactor,mat,perm,0,0);CHKERRQ(ierr); 1760 PetscFunctionReturn(0); 1761 } 1762 1763 #undef __FUNCT__ 1764 #define __FUNCT__ "MatCholeskyFactorSymbolic" 1765 /*@ 1766 MatCholeskyFactorSymbolic - Performs symbolic Cholesky factorization 1767 of a symmetric matrix. 1768 1769 Collective on Mat 1770 1771 Input Parameters: 1772 + mat - the matrix 1773 . perm - row and column permutations 1774 - f - expected fill as ratio of original 1775 1776 Output Parameter: 1777 . fact - the factored matrix 1778 1779 Notes: 1780 See MatLUFactorSymbolic() for the nonsymmetric case. See also 1781 MatCholeskyFactor() and MatCholeskyFactorNumeric(). 1782 1783 Most users should employ the simplified SLES interface for linear solvers 1784 instead of working directly with matrix algebra routines such as this. 1785 See, e.g., SLESCreate(). 1786 1787 Level: developer 1788 1789 Concepts: matrices^Cholesky symbolic factorization 1790 1791 .seealso: MatLUFactorSymbolic(), MatCholeskyFactor(), MatCholeskyFactorNumeric() 1792 MatGetOrdering() 1793 1794 @*/ 1795 int MatCholeskyFactorSymbolic(Mat mat,IS perm,PetscReal f,Mat *fact) 1796 { 1797 int ierr; 1798 1799 PetscFunctionBegin; 1800 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1801 PetscValidType(mat); 1802 MatPreallocated(mat); 1803 PetscValidPointer(fact); 1804 if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,"Matrix must be square"); 1805 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1806 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1807 if (!mat->ops->choleskyfactorsymbolic) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 1808 1809 ierr = PetscLogEventBegin(MAT_CholeskyFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr); 1810 ierr = (*mat->ops->choleskyfactorsymbolic)(mat,perm,f,fact);CHKERRQ(ierr); 1811 ierr = PetscLogEventEnd(MAT_CholeskyFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr); 1812 PetscFunctionReturn(0); 1813 } 1814 1815 #undef __FUNCT__ 1816 #define __FUNCT__ "MatCholeskyFactorNumeric" 1817 /*@ 1818 MatCholeskyFactorNumeric - Performs numeric Cholesky factorization 1819 of a symmetric matrix. Call this routine after first calling 1820 MatCholeskyFactorSymbolic(). 1821 1822 Collective on Mat 1823 1824 Input Parameter: 1825 . mat - the initial matrix 1826 1827 Output Parameter: 1828 . fact - the factored matrix 1829 1830 Notes: 1831 Most users should employ the simplified SLES interface for linear solvers 1832 instead of working directly with matrix algebra routines such as this. 1833 See, e.g., SLESCreate(). 1834 1835 Level: developer 1836 1837 Concepts: matrices^Cholesky numeric factorization 1838 1839 .seealso: MatCholeskyFactorSymbolic(), MatCholeskyFactor(), MatLUFactorNumeric() 1840 @*/ 1841 int MatCholeskyFactorNumeric(Mat mat,Mat *fact) 1842 { 1843 int ierr; 1844 1845 PetscFunctionBegin; 1846 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1847 PetscValidType(mat); 1848 MatPreallocated(mat); 1849 PetscValidPointer(fact); 1850 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1851 if (!(*fact)->ops->choleskyfactornumeric) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 1852 if (mat->M != (*fact)->M || mat->N != (*fact)->N) { 1853 SETERRQ4(PETSC_ERR_ARG_SIZ,"Mat mat,Mat *fact: global dim %d should = %d %d should = %d", 1854 mat->M,(*fact)->M,mat->N,(*fact)->N); 1855 } 1856 1857 ierr = PetscLogEventBegin(MAT_CholeskyFactorNumeric,mat,*fact,0,0);CHKERRQ(ierr); 1858 ierr = (*(*fact)->ops->choleskyfactornumeric)(mat,fact);CHKERRQ(ierr); 1859 ierr = PetscLogEventEnd(MAT_CholeskyFactorNumeric,mat,*fact,0,0);CHKERRQ(ierr); 1860 PetscFunctionReturn(0); 1861 } 1862 1863 /* ----------------------------------------------------------------*/ 1864 #undef __FUNCT__ 1865 #define __FUNCT__ "MatSolve" 1866 /*@ 1867 MatSolve - Solves A x = b, given a factored matrix. 1868 1869 Collective on Mat and Vec 1870 1871 Input Parameters: 1872 + mat - the factored matrix 1873 - b - the right-hand-side vector 1874 1875 Output Parameter: 1876 . x - the result vector 1877 1878 Notes: 1879 The vectors b and x cannot be the same. I.e., one cannot 1880 call MatSolve(A,x,x). 1881 1882 Notes: 1883 Most users should employ the simplified SLES interface for linear solvers 1884 instead of working directly with matrix algebra routines such as this. 1885 See, e.g., SLESCreate(). 1886 1887 Level: developer 1888 1889 Concepts: matrices^triangular solves 1890 1891 .seealso: MatSolveAdd(), MatSolveTranspose(), MatSolveTransposeAdd() 1892 @*/ 1893 int MatSolve(Mat mat,Vec b,Vec x) 1894 { 1895 int ierr; 1896 1897 PetscFunctionBegin; 1898 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1899 PetscValidType(mat); 1900 MatPreallocated(mat); 1901 PetscValidHeaderSpecific(b,VEC_COOKIE); 1902 PetscValidHeaderSpecific(x,VEC_COOKIE); 1903 PetscCheckSameComm(mat,b); 1904 PetscCheckSameComm(mat,x); 1905 if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 1906 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 1907 if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N); 1908 if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->M,b->N); 1909 if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %d %d",mat->m,b->n); 1910 if (mat->M == 0 && mat->N == 0) PetscFunctionReturn(0); 1911 1912 if (!mat->ops->solve) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 1913 ierr = PetscLogEventBegin(MAT_Solve,mat,b,x,0);CHKERRQ(ierr); 1914 ierr = (*mat->ops->solve)(mat,b,x);CHKERRQ(ierr); 1915 ierr = PetscLogEventEnd(MAT_Solve,mat,b,x,0);CHKERRQ(ierr); 1916 PetscFunctionReturn(0); 1917 } 1918 1919 #undef __FUNCT__ 1920 #define __FUNCT__ "MatForwardSolve" 1921 /* @ 1922 MatForwardSolve - Solves L x = b, given a factored matrix, A = LU. 1923 1924 Collective on Mat and Vec 1925 1926 Input Parameters: 1927 + mat - the factored matrix 1928 - b - the right-hand-side vector 1929 1930 Output Parameter: 1931 . x - the result vector 1932 1933 Notes: 1934 MatSolve() should be used for most applications, as it performs 1935 a forward solve followed by a backward solve. 1936 1937 The vectors b and x cannot be the same. I.e., one cannot 1938 call MatForwardSolve(A,x,x). 1939 1940 Most users should employ the simplified SLES interface for linear solvers 1941 instead of working directly with matrix algebra routines such as this. 1942 See, e.g., SLESCreate(). 1943 1944 Level: developer 1945 1946 Concepts: matrices^forward solves 1947 1948 .seealso: MatSolve(), MatBackwardSolve() 1949 @ */ 1950 int MatForwardSolve(Mat mat,Vec b,Vec x) 1951 { 1952 int ierr; 1953 1954 PetscFunctionBegin; 1955 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1956 PetscValidType(mat); 1957 MatPreallocated(mat); 1958 PetscValidHeaderSpecific(b,VEC_COOKIE); 1959 PetscValidHeaderSpecific(x,VEC_COOKIE); 1960 PetscCheckSameComm(mat,b); 1961 PetscCheckSameComm(mat,x); 1962 if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 1963 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 1964 if (!mat->ops->forwardsolve) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 1965 if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N); 1966 if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->M,b->N); 1967 if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %d %d",mat->m,b->n); 1968 1969 ierr = PetscLogEventBegin(MAT_ForwardSolve,mat,b,x,0);CHKERRQ(ierr); 1970 ierr = (*mat->ops->forwardsolve)(mat,b,x);CHKERRQ(ierr); 1971 ierr = PetscLogEventEnd(MAT_ForwardSolve,mat,b,x,0);CHKERRQ(ierr); 1972 PetscFunctionReturn(0); 1973 } 1974 1975 #undef __FUNCT__ 1976 #define __FUNCT__ "MatBackwardSolve" 1977 /* @ 1978 MatBackwardSolve - Solves U x = b, given a factored matrix, A = LU. 1979 1980 Collective on Mat and Vec 1981 1982 Input Parameters: 1983 + mat - the factored matrix 1984 - b - the right-hand-side vector 1985 1986 Output Parameter: 1987 . x - the result vector 1988 1989 Notes: 1990 MatSolve() should be used for most applications, as it performs 1991 a forward solve followed by a backward solve. 1992 1993 The vectors b and x cannot be the same. I.e., one cannot 1994 call MatBackwardSolve(A,x,x). 1995 1996 Most users should employ the simplified SLES interface for linear solvers 1997 instead of working directly with matrix algebra routines such as this. 1998 See, e.g., SLESCreate(). 1999 2000 Level: developer 2001 2002 Concepts: matrices^backward solves 2003 2004 .seealso: MatSolve(), MatForwardSolve() 2005 @ */ 2006 int MatBackwardSolve(Mat mat,Vec b,Vec x) 2007 { 2008 int ierr; 2009 2010 PetscFunctionBegin; 2011 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2012 PetscValidType(mat); 2013 MatPreallocated(mat); 2014 PetscValidHeaderSpecific(b,VEC_COOKIE); 2015 PetscValidHeaderSpecific(x,VEC_COOKIE); 2016 PetscCheckSameComm(mat,b); 2017 PetscCheckSameComm(mat,x); 2018 if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 2019 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 2020 if (!mat->ops->backwardsolve) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 2021 if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N); 2022 if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->M,b->N); 2023 if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %d %d",mat->m,b->n); 2024 2025 ierr = PetscLogEventBegin(MAT_BackwardSolve,mat,b,x,0);CHKERRQ(ierr); 2026 ierr = (*mat->ops->backwardsolve)(mat,b,x);CHKERRQ(ierr); 2027 ierr = PetscLogEventEnd(MAT_BackwardSolve,mat,b,x,0);CHKERRQ(ierr); 2028 PetscFunctionReturn(0); 2029 } 2030 2031 #undef __FUNCT__ 2032 #define __FUNCT__ "MatSolveAdd" 2033 /*@ 2034 MatSolveAdd - Computes x = y + inv(A)*b, given a factored matrix. 2035 2036 Collective on Mat and Vec 2037 2038 Input Parameters: 2039 + mat - the factored matrix 2040 . b - the right-hand-side vector 2041 - y - the vector to be added to 2042 2043 Output Parameter: 2044 . x - the result vector 2045 2046 Notes: 2047 The vectors b and x cannot be the same. I.e., one cannot 2048 call MatSolveAdd(A,x,y,x). 2049 2050 Most users should employ the simplified SLES interface for linear solvers 2051 instead of working directly with matrix algebra routines such as this. 2052 See, e.g., SLESCreate(). 2053 2054 Level: developer 2055 2056 Concepts: matrices^triangular solves 2057 2058 .seealso: MatSolve(), MatSolveTranspose(), MatSolveTransposeAdd() 2059 @*/ 2060 int MatSolveAdd(Mat mat,Vec b,Vec y,Vec x) 2061 { 2062 PetscScalar one = 1.0; 2063 Vec tmp; 2064 int ierr; 2065 2066 PetscFunctionBegin; 2067 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2068 PetscValidType(mat); 2069 MatPreallocated(mat); 2070 PetscValidHeaderSpecific(y,VEC_COOKIE); 2071 PetscValidHeaderSpecific(b,VEC_COOKIE); 2072 PetscValidHeaderSpecific(x,VEC_COOKIE); 2073 PetscCheckSameComm(mat,b); 2074 PetscCheckSameComm(mat,y); 2075 PetscCheckSameComm(mat,x); 2076 if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 2077 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 2078 if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N); 2079 if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->M,b->N); 2080 if (mat->M != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %d %d",mat->M,y->N); 2081 if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %d %d",mat->m,b->n); 2082 if (x->n != y->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Vec x,Vec y: local dim %d %d",x->n,y->n); 2083 2084 ierr = PetscLogEventBegin(MAT_SolveAdd,mat,b,x,y);CHKERRQ(ierr); 2085 if (mat->ops->solveadd) { 2086 ierr = (*mat->ops->solveadd)(mat,b,y,x);CHKERRQ(ierr); 2087 } else { 2088 /* do the solve then the add manually */ 2089 if (x != y) { 2090 ierr = MatSolve(mat,b,x);CHKERRQ(ierr); 2091 ierr = VecAXPY(&one,y,x);CHKERRQ(ierr); 2092 } else { 2093 ierr = VecDuplicate(x,&tmp);CHKERRQ(ierr); 2094 PetscLogObjectParent(mat,tmp); 2095 ierr = VecCopy(x,tmp);CHKERRQ(ierr); 2096 ierr = MatSolve(mat,b,x);CHKERRQ(ierr); 2097 ierr = VecAXPY(&one,tmp,x);CHKERRQ(ierr); 2098 ierr = VecDestroy(tmp);CHKERRQ(ierr); 2099 } 2100 } 2101 ierr = PetscLogEventEnd(MAT_SolveAdd,mat,b,x,y);CHKERRQ(ierr); 2102 PetscFunctionReturn(0); 2103 } 2104 2105 #undef __FUNCT__ 2106 #define __FUNCT__ "MatSolveTranspose" 2107 /*@ 2108 MatSolveTranspose - Solves A' x = b, given a factored matrix. 2109 2110 Collective on Mat and Vec 2111 2112 Input Parameters: 2113 + mat - the factored matrix 2114 - b - the right-hand-side vector 2115 2116 Output Parameter: 2117 . x - the result vector 2118 2119 Notes: 2120 The vectors b and x cannot be the same. I.e., one cannot 2121 call MatSolveTranspose(A,x,x). 2122 2123 Most users should employ the simplified SLES interface for linear solvers 2124 instead of working directly with matrix algebra routines such as this. 2125 See, e.g., SLESCreate(). 2126 2127 Level: developer 2128 2129 Concepts: matrices^triangular solves 2130 2131 .seealso: MatSolve(), MatSolveAdd(), MatSolveTransposeAdd() 2132 @*/ 2133 int MatSolveTranspose(Mat mat,Vec b,Vec x) 2134 { 2135 int ierr; 2136 2137 PetscFunctionBegin; 2138 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2139 PetscValidType(mat); 2140 MatPreallocated(mat); 2141 PetscValidHeaderSpecific(b,VEC_COOKIE); 2142 PetscValidHeaderSpecific(x,VEC_COOKIE); 2143 PetscCheckSameComm(mat,b); 2144 PetscCheckSameComm(mat,x); 2145 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 2146 if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 2147 if (!mat->ops->solvetranspose) SETERRQ1(PETSC_ERR_SUP,"Matrix type %s",mat->type_name); 2148 if (mat->M != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->M,x->N); 2149 if (mat->N != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->N,b->N); 2150 2151 ierr = PetscLogEventBegin(MAT_SolveTranspose,mat,b,x,0);CHKERRQ(ierr); 2152 ierr = (*mat->ops->solvetranspose)(mat,b,x);CHKERRQ(ierr); 2153 ierr = PetscLogEventEnd(MAT_SolveTranspose,mat,b,x,0);CHKERRQ(ierr); 2154 PetscFunctionReturn(0); 2155 } 2156 2157 #undef __FUNCT__ 2158 #define __FUNCT__ "MatSolveTransposeAdd" 2159 /*@ 2160 MatSolveTransposeAdd - Computes x = y + inv(Transpose(A)) b, given a 2161 factored matrix. 2162 2163 Collective on Mat and Vec 2164 2165 Input Parameters: 2166 + mat - the factored matrix 2167 . b - the right-hand-side vector 2168 - y - the vector to be added to 2169 2170 Output Parameter: 2171 . x - the result vector 2172 2173 Notes: 2174 The vectors b and x cannot be the same. I.e., one cannot 2175 call MatSolveTransposeAdd(A,x,y,x). 2176 2177 Most users should employ the simplified SLES interface for linear solvers 2178 instead of working directly with matrix algebra routines such as this. 2179 See, e.g., SLESCreate(). 2180 2181 Level: developer 2182 2183 Concepts: matrices^triangular solves 2184 2185 .seealso: MatSolve(), MatSolveAdd(), MatSolveTranspose() 2186 @*/ 2187 int MatSolveTransposeAdd(Mat mat,Vec b,Vec y,Vec x) 2188 { 2189 PetscScalar one = 1.0; 2190 int ierr; 2191 Vec tmp; 2192 2193 PetscFunctionBegin; 2194 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2195 PetscValidType(mat); 2196 MatPreallocated(mat); 2197 PetscValidHeaderSpecific(y,VEC_COOKIE); 2198 PetscValidHeaderSpecific(b,VEC_COOKIE); 2199 PetscValidHeaderSpecific(x,VEC_COOKIE); 2200 PetscCheckSameComm(mat,b); 2201 PetscCheckSameComm(mat,y); 2202 PetscCheckSameComm(mat,x); 2203 if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 2204 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 2205 if (mat->M != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->M,x->N); 2206 if (mat->N != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->N,b->N); 2207 if (mat->N != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %d %d",mat->N,y->N); 2208 if (x->n != y->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Vec x,Vec y: local dim %d %d",x->n,y->n); 2209 2210 ierr = PetscLogEventBegin(MAT_SolveTransposeAdd,mat,b,x,y);CHKERRQ(ierr); 2211 if (mat->ops->solvetransposeadd) { 2212 ierr = (*mat->ops->solvetransposeadd)(mat,b,y,x);CHKERRQ(ierr); 2213 } else { 2214 /* do the solve then the add manually */ 2215 if (x != y) { 2216 ierr = MatSolveTranspose(mat,b,x);CHKERRQ(ierr); 2217 ierr = VecAXPY(&one,y,x);CHKERRQ(ierr); 2218 } else { 2219 ierr = VecDuplicate(x,&tmp);CHKERRQ(ierr); 2220 PetscLogObjectParent(mat,tmp); 2221 ierr = VecCopy(x,tmp);CHKERRQ(ierr); 2222 ierr = MatSolveTranspose(mat,b,x);CHKERRQ(ierr); 2223 ierr = VecAXPY(&one,tmp,x);CHKERRQ(ierr); 2224 ierr = VecDestroy(tmp);CHKERRQ(ierr); 2225 } 2226 } 2227 ierr = PetscLogEventEnd(MAT_SolveTransposeAdd,mat,b,x,y);CHKERRQ(ierr); 2228 PetscFunctionReturn(0); 2229 } 2230 /* ----------------------------------------------------------------*/ 2231 2232 #undef __FUNCT__ 2233 #define __FUNCT__ "MatRelax" 2234 /*@ 2235 MatRelax - Computes one relaxation sweep. 2236 2237 Collective on Mat and Vec 2238 2239 Input Parameters: 2240 + mat - the matrix 2241 . b - the right hand side 2242 . omega - the relaxation factor 2243 . flag - flag indicating the type of SOR (see below) 2244 . shift - diagonal shift 2245 - its - the number of iterations 2246 - lits - the number of local iterations 2247 2248 Output Parameters: 2249 . x - the solution (can contain an initial guess) 2250 2251 SOR Flags: 2252 . SOR_FORWARD_SWEEP - forward SOR 2253 . SOR_BACKWARD_SWEEP - backward SOR 2254 . SOR_SYMMETRIC_SWEEP - SSOR (symmetric SOR) 2255 . SOR_LOCAL_FORWARD_SWEEP - local forward SOR 2256 . SOR_LOCAL_BACKWARD_SWEEP - local forward SOR 2257 . SOR_LOCAL_SYMMETRIC_SWEEP - local SSOR 2258 . SOR_APPLY_UPPER, SOR_APPLY_LOWER - applies 2259 upper/lower triangular part of matrix to 2260 vector (with omega) 2261 . SOR_ZERO_INITIAL_GUESS - zero initial guess 2262 2263 Notes: 2264 SOR_LOCAL_FORWARD_SWEEP, SOR_LOCAL_BACKWARD_SWEEP, and 2265 SOR_LOCAL_SYMMETRIC_SWEEP perform seperate independent smoothings 2266 on each processor. 2267 2268 Application programmers will not generally use MatRelax() directly, 2269 but instead will employ the SLES/PC interface. 2270 2271 Notes for Advanced Users: 2272 The flags are implemented as bitwise inclusive or operations. 2273 For example, use (SOR_ZERO_INITIAL_GUESS | SOR_SYMMETRIC_SWEEP) 2274 to specify a zero initial guess for SSOR. 2275 2276 Most users should employ the simplified SLES interface for linear solvers 2277 instead of working directly with matrix algebra routines such as this. 2278 See, e.g., SLESCreate(). 2279 2280 Level: developer 2281 2282 Concepts: matrices^relaxation 2283 Concepts: matrices^SOR 2284 Concepts: matrices^Gauss-Seidel 2285 2286 @*/ 2287 int MatRelax(Mat mat,Vec b,PetscReal omega,MatSORType flag,PetscReal shift,int its,int lits,Vec x) 2288 { 2289 int ierr; 2290 2291 PetscFunctionBegin; 2292 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2293 PetscValidType(mat); 2294 MatPreallocated(mat); 2295 PetscValidHeaderSpecific(b,VEC_COOKIE); 2296 PetscValidHeaderSpecific(x,VEC_COOKIE); 2297 PetscCheckSameComm(mat,b); 2298 PetscCheckSameComm(mat,x); 2299 if (!mat->ops->relax) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 2300 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2301 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2302 if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N); 2303 if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->M,b->N); 2304 if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %d %d",mat->m,b->n); 2305 2306 ierr = PetscLogEventBegin(MAT_Relax,mat,b,x,0);CHKERRQ(ierr); 2307 ierr =(*mat->ops->relax)(mat,b,omega,flag,shift,its,lits,x);CHKERRQ(ierr); 2308 ierr = PetscLogEventEnd(MAT_Relax,mat,b,x,0);CHKERRQ(ierr); 2309 PetscFunctionReturn(0); 2310 } 2311 2312 #undef __FUNCT__ 2313 #define __FUNCT__ "MatCopy_Basic" 2314 /* 2315 Default matrix copy routine. 2316 */ 2317 int MatCopy_Basic(Mat A,Mat B,MatStructure str) 2318 { 2319 int ierr,i,rstart,rend,nz,*cwork; 2320 PetscScalar *vwork; 2321 2322 PetscFunctionBegin; 2323 ierr = MatZeroEntries(B);CHKERRQ(ierr); 2324 ierr = MatGetOwnershipRange(A,&rstart,&rend);CHKERRQ(ierr); 2325 for (i=rstart; i<rend; i++) { 2326 ierr = MatGetRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr); 2327 ierr = MatSetValues(B,1,&i,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr); 2328 ierr = MatRestoreRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr); 2329 } 2330 ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2331 ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2332 PetscFunctionReturn(0); 2333 } 2334 2335 #undef __FUNCT__ 2336 #define __FUNCT__ "MatCopy" 2337 /*@C 2338 MatCopy - Copys a matrix to another matrix. 2339 2340 Collective on Mat 2341 2342 Input Parameters: 2343 + A - the matrix 2344 - str - SAME_NONZERO_PATTERN or DIFFERENT_NONZERO_PATTERN 2345 2346 Output Parameter: 2347 . B - where the copy is put 2348 2349 Notes: 2350 If you use SAME_NONZERO_PATTERN then the two matrices had better have the 2351 same nonzero pattern or the routine will crash. 2352 2353 MatCopy() copies the matrix entries of a matrix to another existing 2354 matrix (after first zeroing the second matrix). A related routine is 2355 MatConvert(), which first creates a new matrix and then copies the data. 2356 2357 Level: intermediate 2358 2359 Concepts: matrices^copying 2360 2361 .seealso: MatConvert() 2362 @*/ 2363 int MatCopy(Mat A,Mat B,MatStructure str) 2364 { 2365 int ierr; 2366 2367 PetscFunctionBegin; 2368 PetscValidHeaderSpecific(A,MAT_COOKIE); 2369 PetscValidHeaderSpecific(B,MAT_COOKIE); 2370 PetscValidType(A); 2371 MatPreallocated(A); 2372 PetscValidType(B); 2373 MatPreallocated(B); 2374 PetscCheckSameComm(A,B); 2375 if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2376 if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2377 if (A->M != B->M || A->N != B->N) SETERRQ4(PETSC_ERR_ARG_SIZ,"Mat A,Mat B: global dim (%d,%d) (%d,%d)",A->M,B->M, 2378 A->N,B->N); 2379 2380 ierr = PetscLogEventBegin(MAT_Copy,A,B,0,0);CHKERRQ(ierr); 2381 if (A->ops->copy) { 2382 ierr = (*A->ops->copy)(A,B,str);CHKERRQ(ierr); 2383 } else { /* generic conversion */ 2384 ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr); 2385 } 2386 ierr = PetscLogEventEnd(MAT_Copy,A,B,0,0);CHKERRQ(ierr); 2387 PetscFunctionReturn(0); 2388 } 2389 2390 #include "petscsys.h" 2391 PetscTruth MatConvertRegisterAllCalled = PETSC_FALSE; 2392 PetscFList MatConvertList = 0; 2393 2394 #undef __FUNCT__ 2395 #define __FUNCT__ "MatConvertRegister" 2396 /*@C 2397 MatConvertRegister - Allows one to register a routine that reads matrices 2398 from a binary file for a particular matrix type. 2399 2400 Not Collective 2401 2402 Input Parameters: 2403 + type - the type of matrix (defined in include/petscmat.h), for example, MATSEQAIJ. 2404 - Converter - the function that reads the matrix from the binary file. 2405 2406 Level: developer 2407 2408 .seealso: MatConvertRegisterAll(), MatConvert() 2409 2410 @*/ 2411 int MatConvertRegister(char *sname,char *path,char *name,int (*function)(Mat,MatType,Mat*)) 2412 { 2413 int ierr; 2414 char fullname[256]; 2415 2416 PetscFunctionBegin; 2417 ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr); 2418 ierr = PetscFListAdd(&MatConvertList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr); 2419 PetscFunctionReturn(0); 2420 } 2421 2422 #undef __FUNCT__ 2423 #define __FUNCT__ "MatConvert" 2424 /*@C 2425 MatConvert - Converts a matrix to another matrix, either of the same 2426 or different type. 2427 2428 Collective on Mat 2429 2430 Input Parameters: 2431 + mat - the matrix 2432 - newtype - new matrix type. Use MATSAME to create a new matrix of the 2433 same type as the original matrix. 2434 2435 Output Parameter: 2436 . M - pointer to place new matrix 2437 2438 Notes: 2439 MatConvert() first creates a new matrix and then copies the data from 2440 the first matrix. A related routine is MatCopy(), which copies the matrix 2441 entries of one matrix to another already existing matrix context. 2442 2443 Level: intermediate 2444 2445 Concepts: matrices^converting between storage formats 2446 2447 .seealso: MatCopy(), MatDuplicate() 2448 @*/ 2449 int MatConvert(Mat mat,MatType newtype,Mat *M) 2450 { 2451 int ierr; 2452 PetscTruth sametype,issame,flg; 2453 char convname[256],mtype[256]; 2454 2455 PetscFunctionBegin; 2456 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2457 PetscValidType(mat); 2458 MatPreallocated(mat); 2459 PetscValidPointer(M); 2460 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2461 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2462 2463 ierr = PetscOptionsGetString(PETSC_NULL,"-matconvert_type",mtype,256,&flg);CHKERRQ(ierr); 2464 if (flg) { 2465 newtype = mtype; 2466 } 2467 ierr = PetscLogEventBegin(MAT_Convert,mat,0,0,0);CHKERRQ(ierr); 2468 2469 ierr = PetscTypeCompare((PetscObject)mat,newtype,&sametype);CHKERRQ(ierr); 2470 ierr = PetscStrcmp(newtype,"same",&issame);CHKERRQ(ierr); 2471 if ((sametype || issame) && mat->ops->duplicate) { 2472 ierr = (*mat->ops->duplicate)(mat,MAT_COPY_VALUES,M);CHKERRQ(ierr); 2473 } else { 2474 int (*conv)(Mat,MatType,Mat*); 2475 if (!MatConvertRegisterAllCalled) { 2476 ierr = MatConvertRegisterAll(PETSC_NULL);CHKERRQ(ierr); 2477 } 2478 ierr = PetscFListFind(mat->comm,MatConvertList,newtype,(void(**)(void))&conv);CHKERRQ(ierr); 2479 if (conv) { 2480 ierr = (*conv)(mat,newtype,M);CHKERRQ(ierr); 2481 } else { 2482 ierr = PetscStrcpy(convname,"MatConvert_");CHKERRQ(ierr); 2483 ierr = PetscStrcat(convname,mat->type_name);CHKERRQ(ierr); 2484 ierr = PetscStrcat(convname,"_");CHKERRQ(ierr); 2485 ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr); 2486 ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr); 2487 ierr = PetscObjectQueryFunction((PetscObject)mat,convname,(void (**)(void))&conv);CHKERRQ(ierr); 2488 if (conv) { 2489 ierr = (*conv)(mat,newtype,M);CHKERRQ(ierr); 2490 } else { 2491 if (mat->ops->convert) { 2492 ierr = (*mat->ops->convert)(mat,newtype,M);CHKERRQ(ierr); 2493 } else { 2494 ierr = MatConvert_Basic(mat,newtype,M);CHKERRQ(ierr); 2495 } 2496 } 2497 } 2498 } 2499 ierr = PetscLogEventEnd(MAT_Convert,mat,0,0,0);CHKERRQ(ierr); 2500 PetscFunctionReturn(0); 2501 } 2502 2503 2504 #undef __FUNCT__ 2505 #define __FUNCT__ "MatDuplicate" 2506 /*@C 2507 MatDuplicate - Duplicates a matrix including the non-zero structure. 2508 2509 Collective on Mat 2510 2511 Input Parameters: 2512 + mat - the matrix 2513 - op - either MAT_DO_NOT_COPY_VALUES or MAT_COPY_VALUES, cause it to copy nonzero 2514 values as well or not 2515 2516 Output Parameter: 2517 . M - pointer to place new matrix 2518 2519 Level: intermediate 2520 2521 Concepts: matrices^duplicating 2522 2523 .seealso: MatCopy(), MatConvert() 2524 @*/ 2525 int MatDuplicate(Mat mat,MatDuplicateOption op,Mat *M) 2526 { 2527 int ierr; 2528 2529 PetscFunctionBegin; 2530 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2531 PetscValidType(mat); 2532 MatPreallocated(mat); 2533 PetscValidPointer(M); 2534 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2535 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2536 2537 *M = 0; 2538 ierr = PetscLogEventBegin(MAT_Convert,mat,0,0,0);CHKERRQ(ierr); 2539 if (!mat->ops->duplicate) { 2540 SETERRQ(PETSC_ERR_SUP,"Not written for this matrix type"); 2541 } 2542 ierr = (*mat->ops->duplicate)(mat,op,M);CHKERRQ(ierr); 2543 ierr = PetscLogEventEnd(MAT_Convert,mat,0,0,0);CHKERRQ(ierr); 2544 PetscFunctionReturn(0); 2545 } 2546 2547 #undef __FUNCT__ 2548 #define __FUNCT__ "MatGetDiagonal" 2549 /*@ 2550 MatGetDiagonal - Gets the diagonal of a matrix. 2551 2552 Collective on Mat and Vec 2553 2554 Input Parameters: 2555 + mat - the matrix 2556 - v - the vector for storing the diagonal 2557 2558 Output Parameter: 2559 . v - the diagonal of the matrix 2560 2561 Notes: 2562 For the SeqAIJ matrix format, this routine may also be called 2563 on a LU factored matrix; in that case it routines the reciprocal of 2564 the diagonal entries in U. It returns the entries permuted by the 2565 row and column permutation used during the symbolic factorization. 2566 2567 Level: intermediate 2568 2569 Concepts: matrices^accessing diagonals 2570 2571 .seealso: MatGetRow(), MatGetSubmatrices(), MatGetSubmatrix(), MatGetRowMax() 2572 @*/ 2573 int MatGetDiagonal(Mat mat,Vec v) 2574 { 2575 int ierr; 2576 2577 PetscFunctionBegin; 2578 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2579 PetscValidType(mat); 2580 MatPreallocated(mat); 2581 PetscValidHeaderSpecific(v,VEC_COOKIE); 2582 /* PetscCheckSameComm(mat,v); Could be MPI vector but Seq matrix cause of two submatrix storage */ 2583 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2584 if (!mat->ops->getdiagonal) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 2585 2586 ierr = (*mat->ops->getdiagonal)(mat,v);CHKERRQ(ierr); 2587 PetscFunctionReturn(0); 2588 } 2589 2590 #undef __FUNCT__ 2591 #define __FUNCT__ "MatGetRowMax" 2592 /*@ 2593 MatGetRowMax - Gets the maximum value (in absolute value) of each 2594 row of the matrix 2595 2596 Collective on Mat and Vec 2597 2598 Input Parameters: 2599 . mat - the matrix 2600 2601 Output Parameter: 2602 . v - the vector for storing the maximums 2603 2604 Level: intermediate 2605 2606 Concepts: matrices^getting row maximums 2607 2608 .seealso: MatGetDiagonal(), MatGetSubmatrices(), MatGetSubmatrix() 2609 @*/ 2610 int MatGetRowMax(Mat mat,Vec v) 2611 { 2612 int ierr; 2613 2614 PetscFunctionBegin; 2615 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2616 PetscValidType(mat); 2617 MatPreallocated(mat); 2618 PetscValidHeaderSpecific(v,VEC_COOKIE); 2619 /* PetscCheckSameComm(mat,v); Could be MPI vector but Seq matrix cause of two submatrix storage */ 2620 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2621 if (!mat->ops->getrowmax) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 2622 2623 ierr = (*mat->ops->getrowmax)(mat,v);CHKERRQ(ierr); 2624 PetscFunctionReturn(0); 2625 } 2626 2627 #undef __FUNCT__ 2628 #define __FUNCT__ "MatTranspose" 2629 /*@C 2630 MatTranspose - Computes an in-place or out-of-place transpose of a matrix. 2631 2632 Collective on Mat 2633 2634 Input Parameter: 2635 . mat - the matrix to transpose 2636 2637 Output Parameters: 2638 . B - the transpose (or pass in PETSC_NULL for an in-place transpose) 2639 2640 Level: intermediate 2641 2642 Concepts: matrices^transposing 2643 2644 .seealso: MatMultTranspose(), MatMultTransposeAdd() 2645 @*/ 2646 int MatTranspose(Mat mat,Mat *B) 2647 { 2648 int ierr; 2649 2650 PetscFunctionBegin; 2651 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2652 PetscValidType(mat); 2653 MatPreallocated(mat); 2654 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2655 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2656 if (!mat->ops->transpose) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 2657 2658 ierr = PetscLogEventBegin(MAT_Transpose,mat,0,0,0);CHKERRQ(ierr); 2659 ierr = (*mat->ops->transpose)(mat,B);CHKERRQ(ierr); 2660 ierr = PetscLogEventEnd(MAT_Transpose,mat,0,0,0);CHKERRQ(ierr); 2661 PetscFunctionReturn(0); 2662 } 2663 2664 #undef __FUNCT__ 2665 #define __FUNCT__ "MatPermute" 2666 /*@C 2667 MatPermute - Creates a new matrix with rows and columns permuted from the 2668 original. 2669 2670 Collective on Mat 2671 2672 Input Parameters: 2673 + mat - the matrix to permute 2674 . row - row permutation, each processor supplies only the permutation for its rows 2675 - col - column permutation, each processor needs the entire column permutation, that is 2676 this is the same size as the total number of columns in the matrix 2677 2678 Output Parameters: 2679 . B - the permuted matrix 2680 2681 Level: advanced 2682 2683 Concepts: matrices^permuting 2684 2685 .seealso: MatGetOrdering() 2686 @*/ 2687 int MatPermute(Mat mat,IS row,IS col,Mat *B) 2688 { 2689 int ierr; 2690 2691 PetscFunctionBegin; 2692 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2693 PetscValidType(mat); 2694 MatPreallocated(mat); 2695 PetscValidHeaderSpecific(row,IS_COOKIE); 2696 PetscValidHeaderSpecific(col,IS_COOKIE); 2697 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2698 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2699 if (!mat->ops->permute) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 2700 ierr = (*mat->ops->permute)(mat,row,col,B);CHKERRQ(ierr); 2701 PetscFunctionReturn(0); 2702 } 2703 2704 #undef __FUNCT__ 2705 #define __FUNCT__ "MatPermuteSparsify" 2706 /*@C 2707 MatPermuteSparsify - Creates a new matrix with rows and columns permuted from the 2708 original and sparsified to the prescribed tolerance. 2709 2710 Collective on Mat 2711 2712 Input Parameters: 2713 + A - The matrix to permute 2714 . band - The half-bandwidth of the sparsified matrix, or PETSC_DECIDE 2715 . frac - The half-bandwidth as a fraction of the total size, or 0.0 2716 . tol - The drop tolerance 2717 . rowp - The row permutation 2718 - colp - The column permutation 2719 2720 Output Parameter: 2721 . B - The permuted, sparsified matrix 2722 2723 Level: advanced 2724 2725 Note: 2726 The default behavior (band = PETSC_DECIDE and frac = 0.0) is to 2727 restrict the half-bandwidth of the resulting matrix to 5% of the 2728 total matrix size. 2729 2730 .keywords: matrix, permute, sparsify 2731 2732 .seealso: MatGetOrdering(), MatPermute() 2733 @*/ 2734 int MatPermuteSparsify(Mat A, int band, PetscReal frac, PetscReal tol, IS rowp, IS colp, Mat *B) 2735 { 2736 IS irowp, icolp; 2737 int *rows, *cols; 2738 int M, N, locRowStart, locRowEnd; 2739 int nz, newNz; 2740 int *cwork, *cnew; 2741 PetscScalar *vwork, *vnew; 2742 int bw, size; 2743 int row, locRow, newRow, col, newCol; 2744 int ierr; 2745 2746 PetscFunctionBegin; 2747 PetscValidHeaderSpecific(A, MAT_COOKIE); 2748 PetscValidHeaderSpecific(rowp, IS_COOKIE); 2749 PetscValidHeaderSpecific(colp, IS_COOKIE); 2750 if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE, "Not for unassembled matrix"); 2751 if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix"); 2752 if (!A->ops->permutesparsify) { 2753 ierr = MatGetSize(A, &M, &N); CHKERRQ(ierr); 2754 ierr = MatGetOwnershipRange(A, &locRowStart, &locRowEnd); CHKERRQ(ierr); 2755 ierr = ISGetSize(rowp, &size); CHKERRQ(ierr); 2756 if (size != M) SETERRQ2(PETSC_ERR_ARG_WRONG, "Wrong size %d for row permutation, should be %d", size, M); 2757 ierr = ISGetSize(colp, &size); CHKERRQ(ierr); 2758 if (size != N) SETERRQ2(PETSC_ERR_ARG_WRONG, "Wrong size %d for column permutation, should be %d", size, N); 2759 ierr = ISInvertPermutation(rowp, 0, &irowp); CHKERRQ(ierr); 2760 ierr = ISGetIndices(irowp, &rows); CHKERRQ(ierr); 2761 ierr = ISInvertPermutation(colp, 0, &icolp); CHKERRQ(ierr); 2762 ierr = ISGetIndices(icolp, &cols); CHKERRQ(ierr); 2763 ierr = PetscMalloc(N * sizeof(int), &cnew); CHKERRQ(ierr); 2764 ierr = PetscMalloc(N * sizeof(PetscScalar), &vnew); CHKERRQ(ierr); 2765 2766 /* Setup bandwidth to include */ 2767 if (band == PETSC_DECIDE) { 2768 if (frac <= 0.0) 2769 bw = (int) (M * 0.05); 2770 else 2771 bw = (int) (M * frac); 2772 } else { 2773 if (band <= 0) SETERRQ(PETSC_ERR_ARG_WRONG, "Bandwidth must be a positive integer"); 2774 bw = band; 2775 } 2776 2777 /* Put values into new matrix */ 2778 ierr = MatDuplicate(A, MAT_DO_NOT_COPY_VALUES, B); CHKERRQ(ierr); 2779 for(row = locRowStart, locRow = 0; row < locRowEnd; row++, locRow++) { 2780 ierr = MatGetRow(A, row, &nz, &cwork, &vwork); CHKERRQ(ierr); 2781 newRow = rows[locRow]+locRowStart; 2782 for(col = 0, newNz = 0; col < nz; col++) { 2783 newCol = cols[cwork[col]]; 2784 if ((newCol >= newRow - bw) && (newCol < newRow + bw) && (PetscAbsScalar(vwork[col]) >= tol)) { 2785 cnew[newNz] = newCol; 2786 vnew[newNz] = vwork[col]; 2787 newNz++; 2788 } 2789 } 2790 ierr = MatSetValues(*B, 1, &newRow, newNz, cnew, vnew, INSERT_VALUES); CHKERRQ(ierr); 2791 ierr = MatRestoreRow(A, row, &nz, &cwork, &vwork); CHKERRQ(ierr); 2792 } 2793 ierr = PetscFree(cnew); CHKERRQ(ierr); 2794 ierr = PetscFree(vnew); CHKERRQ(ierr); 2795 ierr = MatAssemblyBegin(*B, MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 2796 ierr = MatAssemblyEnd(*B, MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 2797 ierr = ISRestoreIndices(irowp, &rows); CHKERRQ(ierr); 2798 ierr = ISRestoreIndices(icolp, &cols); CHKERRQ(ierr); 2799 ierr = ISDestroy(irowp); CHKERRQ(ierr); 2800 ierr = ISDestroy(icolp); CHKERRQ(ierr); 2801 } else { 2802 ierr = (*A->ops->permutesparsify)(A, band, frac, tol, rowp, colp, B); CHKERRQ(ierr); 2803 } 2804 PetscFunctionReturn(0); 2805 } 2806 2807 #undef __FUNCT__ 2808 #define __FUNCT__ "MatEqual" 2809 /*@ 2810 MatEqual - Compares two matrices. 2811 2812 Collective on Mat 2813 2814 Input Parameters: 2815 + A - the first matrix 2816 - B - the second matrix 2817 2818 Output Parameter: 2819 . flg - PETSC_TRUE if the matrices are equal; PETSC_FALSE otherwise. 2820 2821 Level: intermediate 2822 2823 Concepts: matrices^equality between 2824 @*/ 2825 int MatEqual(Mat A,Mat B,PetscTruth *flg) 2826 { 2827 int ierr; 2828 2829 PetscFunctionBegin; 2830 PetscValidHeaderSpecific(A,MAT_COOKIE); 2831 PetscValidHeaderSpecific(B,MAT_COOKIE); 2832 PetscValidType(A); 2833 MatPreallocated(A); 2834 PetscValidType(B); 2835 MatPreallocated(B); 2836 PetscValidIntPointer(flg); 2837 PetscCheckSameComm(A,B); 2838 if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2839 if (!B->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2840 if (A->M != B->M || A->N != B->N) SETERRQ4(PETSC_ERR_ARG_SIZ,"Mat A,Mat B: global dim %d %d %d %d",A->M,B->M,A->N,B->N); 2841 if (!A->ops->equal) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",A->type_name); 2842 ierr = (*A->ops->equal)(A,B,flg);CHKERRQ(ierr); 2843 PetscFunctionReturn(0); 2844 } 2845 2846 #undef __FUNCT__ 2847 #define __FUNCT__ "MatDiagonalScale" 2848 /*@ 2849 MatDiagonalScale - Scales a matrix on the left and right by diagonal 2850 matrices that are stored as vectors. Either of the two scaling 2851 matrices can be PETSC_NULL. 2852 2853 Collective on Mat 2854 2855 Input Parameters: 2856 + mat - the matrix to be scaled 2857 . l - the left scaling vector (or PETSC_NULL) 2858 - r - the right scaling vector (or PETSC_NULL) 2859 2860 Notes: 2861 MatDiagonalScale() computes A = LAR, where 2862 L = a diagonal matrix, R = a diagonal matrix 2863 2864 Level: intermediate 2865 2866 Concepts: matrices^diagonal scaling 2867 Concepts: diagonal scaling of matrices 2868 2869 .seealso: MatScale() 2870 @*/ 2871 int MatDiagonalScale(Mat mat,Vec l,Vec r) 2872 { 2873 int ierr; 2874 2875 PetscFunctionBegin; 2876 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2877 PetscValidType(mat); 2878 MatPreallocated(mat); 2879 if (!mat->ops->diagonalscale) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 2880 if (l) {PetscValidHeaderSpecific(l,VEC_COOKIE);PetscCheckSameComm(mat,l);} 2881 if (r) {PetscValidHeaderSpecific(r,VEC_COOKIE);PetscCheckSameComm(mat,r);} 2882 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2883 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2884 2885 ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 2886 ierr = (*mat->ops->diagonalscale)(mat,l,r);CHKERRQ(ierr); 2887 ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 2888 PetscFunctionReturn(0); 2889 } 2890 2891 #undef __FUNCT__ 2892 #define __FUNCT__ "MatScale" 2893 /*@ 2894 MatScale - Scales all elements of a matrix by a given number. 2895 2896 Collective on Mat 2897 2898 Input Parameters: 2899 + mat - the matrix to be scaled 2900 - a - the scaling value 2901 2902 Output Parameter: 2903 . mat - the scaled matrix 2904 2905 Level: intermediate 2906 2907 Concepts: matrices^scaling all entries 2908 2909 .seealso: MatDiagonalScale() 2910 @*/ 2911 int MatScale(PetscScalar *a,Mat mat) 2912 { 2913 int ierr; 2914 2915 PetscFunctionBegin; 2916 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2917 PetscValidType(mat); 2918 MatPreallocated(mat); 2919 PetscValidScalarPointer(a); 2920 if (!mat->ops->scale) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 2921 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2922 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2923 2924 ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 2925 ierr = (*mat->ops->scale)(a,mat);CHKERRQ(ierr); 2926 ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 2927 PetscFunctionReturn(0); 2928 } 2929 2930 #undef __FUNCT__ 2931 #define __FUNCT__ "MatNorm" 2932 /*@ 2933 MatNorm - Calculates various norms of a matrix. 2934 2935 Collective on Mat 2936 2937 Input Parameters: 2938 + mat - the matrix 2939 - type - the type of norm, NORM_1, NORM_FROBENIUS, NORM_INFINITY 2940 2941 Output Parameters: 2942 . nrm - the resulting norm 2943 2944 Level: intermediate 2945 2946 Concepts: matrices^norm 2947 Concepts: norm^of matrix 2948 @*/ 2949 int MatNorm(Mat mat,NormType type,PetscReal *nrm) 2950 { 2951 int ierr; 2952 2953 PetscFunctionBegin; 2954 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2955 PetscValidType(mat); 2956 MatPreallocated(mat); 2957 PetscValidScalarPointer(nrm); 2958 2959 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2960 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2961 if (!mat->ops->norm) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 2962 ierr = (*mat->ops->norm)(mat,type,nrm);CHKERRQ(ierr); 2963 PetscFunctionReturn(0); 2964 } 2965 2966 /* 2967 This variable is used to prevent counting of MatAssemblyBegin() that 2968 are called from within a MatAssemblyEnd(). 2969 */ 2970 static int MatAssemblyEnd_InUse = 0; 2971 #undef __FUNCT__ 2972 #define __FUNCT__ "MatAssemblyBegin" 2973 /*@ 2974 MatAssemblyBegin - Begins assembling the matrix. This routine should 2975 be called after completing all calls to MatSetValues(). 2976 2977 Collective on Mat 2978 2979 Input Parameters: 2980 + mat - the matrix 2981 - type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY 2982 2983 Notes: 2984 MatSetValues() generally caches the values. The matrix is ready to 2985 use only after MatAssemblyBegin() and MatAssemblyEnd() have been called. 2986 Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES 2987 in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before 2988 using the matrix. 2989 2990 Level: beginner 2991 2992 Concepts: matrices^assembling 2993 2994 .seealso: MatAssemblyEnd(), MatSetValues(), MatAssembled() 2995 @*/ 2996 int MatAssemblyBegin(Mat mat,MatAssemblyType type) 2997 { 2998 int ierr; 2999 3000 PetscFunctionBegin; 3001 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3002 PetscValidType(mat); 3003 MatPreallocated(mat); 3004 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix.\nDid you forget to call MatSetUnfactored()?"); 3005 if (mat->assembled) { 3006 mat->was_assembled = PETSC_TRUE; 3007 mat->assembled = PETSC_FALSE; 3008 } 3009 if (!MatAssemblyEnd_InUse) { 3010 ierr = PetscLogEventBegin(MAT_AssemblyBegin,mat,0,0,0);CHKERRQ(ierr); 3011 if (mat->ops->assemblybegin){ierr = (*mat->ops->assemblybegin)(mat,type);CHKERRQ(ierr);} 3012 ierr = PetscLogEventEnd(MAT_AssemblyBegin,mat,0,0,0);CHKERRQ(ierr); 3013 } else { 3014 if (mat->ops->assemblybegin){ierr = (*mat->ops->assemblybegin)(mat,type);CHKERRQ(ierr);} 3015 } 3016 PetscFunctionReturn(0); 3017 } 3018 3019 #undef __FUNCT__ 3020 #define __FUNCT__ "MatAssembed" 3021 /*@ 3022 MatAssembled - Indicates if a matrix has been assembled and is ready for 3023 use; for example, in matrix-vector product. 3024 3025 Collective on Mat 3026 3027 Input Parameter: 3028 . mat - the matrix 3029 3030 Output Parameter: 3031 . assembled - PETSC_TRUE or PETSC_FALSE 3032 3033 Level: advanced 3034 3035 Concepts: matrices^assembled? 3036 3037 .seealso: MatAssemblyEnd(), MatSetValues(), MatAssemblyBegin() 3038 @*/ 3039 int MatAssembled(Mat mat,PetscTruth *assembled) 3040 { 3041 PetscFunctionBegin; 3042 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3043 PetscValidType(mat); 3044 MatPreallocated(mat); 3045 *assembled = mat->assembled; 3046 PetscFunctionReturn(0); 3047 } 3048 3049 #undef __FUNCT__ 3050 #define __FUNCT__ "MatView_Private" 3051 /* 3052 Processes command line options to determine if/how a matrix 3053 is to be viewed. Called by MatAssemblyEnd() and MatLoad(). 3054 */ 3055 int MatView_Private(Mat mat) 3056 { 3057 int ierr; 3058 PetscTruth flg; 3059 3060 PetscFunctionBegin; 3061 ierr = PetscOptionsHasName(mat->prefix,"-mat_view_info",&flg);CHKERRQ(ierr); 3062 if (flg) { 3063 ierr = PetscViewerPushFormat(PETSC_VIEWER_STDOUT_(mat->comm),PETSC_VIEWER_ASCII_INFO);CHKERRQ(ierr); 3064 ierr = MatView(mat,PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr); 3065 ierr = PetscViewerPopFormat(PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr); 3066 } 3067 ierr = PetscOptionsHasName(mat->prefix,"-mat_view_info_detailed",&flg);CHKERRQ(ierr); 3068 if (flg) { 3069 ierr = PetscViewerPushFormat(PETSC_VIEWER_STDOUT_(mat->comm),PETSC_VIEWER_ASCII_INFO_LONG);CHKERRQ(ierr); 3070 ierr = MatView(mat,PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr); 3071 ierr = PetscViewerPopFormat(PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr); 3072 } 3073 ierr = PetscOptionsHasName(mat->prefix,"-mat_view",&flg);CHKERRQ(ierr); 3074 if (flg) { 3075 ierr = MatView(mat,PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr); 3076 } 3077 ierr = PetscOptionsHasName(mat->prefix,"-mat_view_matlab",&flg);CHKERRQ(ierr); 3078 if (flg) { 3079 ierr = PetscViewerPushFormat(PETSC_VIEWER_STDOUT_(mat->comm),PETSC_VIEWER_ASCII_MATLAB);CHKERRQ(ierr); 3080 ierr = MatView(mat,PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr); 3081 ierr = PetscViewerPopFormat(PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr); 3082 } 3083 ierr = PetscOptionsHasName(mat->prefix,"-mat_view_draw",&flg);CHKERRQ(ierr); 3084 if (flg) { 3085 ierr = PetscOptionsHasName(mat->prefix,"-mat_view_contour",&flg);CHKERRQ(ierr); 3086 if (flg) { 3087 PetscViewerPushFormat(PETSC_VIEWER_DRAW_(mat->comm),PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr); 3088 } 3089 ierr = MatView(mat,PETSC_VIEWER_DRAW_(mat->comm));CHKERRQ(ierr); 3090 ierr = PetscViewerFlush(PETSC_VIEWER_DRAW_(mat->comm));CHKERRQ(ierr); 3091 if (flg) { 3092 PetscViewerPopFormat(PETSC_VIEWER_DRAW_(mat->comm));CHKERRQ(ierr); 3093 } 3094 } 3095 ierr = PetscOptionsHasName(mat->prefix,"-mat_view_socket",&flg);CHKERRQ(ierr); 3096 if (flg) { 3097 ierr = MatView(mat,PETSC_VIEWER_SOCKET_(mat->comm));CHKERRQ(ierr); 3098 ierr = PetscViewerFlush(PETSC_VIEWER_SOCKET_(mat->comm));CHKERRQ(ierr); 3099 } 3100 ierr = PetscOptionsHasName(mat->prefix,"-mat_view_binary",&flg);CHKERRQ(ierr); 3101 if (flg) { 3102 ierr = MatView(mat,PETSC_VIEWER_BINARY_(mat->comm));CHKERRQ(ierr); 3103 ierr = PetscViewerFlush(PETSC_VIEWER_BINARY_(mat->comm));CHKERRQ(ierr); 3104 } 3105 PetscFunctionReturn(0); 3106 } 3107 3108 #undef __FUNCT__ 3109 #define __FUNCT__ "MatAssemblyEnd" 3110 /*@ 3111 MatAssemblyEnd - Completes assembling the matrix. This routine should 3112 be called after MatAssemblyBegin(). 3113 3114 Collective on Mat 3115 3116 Input Parameters: 3117 + mat - the matrix 3118 - type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY 3119 3120 Options Database Keys: 3121 + -mat_view_info - Prints info on matrix at conclusion of MatEndAssembly() 3122 . -mat_view_info_detailed - Prints more detailed info 3123 . -mat_view - Prints matrix in ASCII format 3124 . -mat_view_matlab - Prints matrix in Matlab format 3125 . -mat_view_draw - PetscDraws nonzero structure of matrix, using MatView() and PetscDrawOpenX(). 3126 . -display <name> - Sets display name (default is host) 3127 - -draw_pause <sec> - Sets number of seconds to pause after display 3128 3129 Notes: 3130 MatSetValues() generally caches the values. The matrix is ready to 3131 use only after MatAssemblyBegin() and MatAssemblyEnd() have been called. 3132 Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES 3133 in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before 3134 using the matrix. 3135 3136 Level: beginner 3137 3138 .seealso: MatAssemblyBegin(), MatSetValues(), PetscDrawOpenX(), MatView(), MatAssembled() 3139 @*/ 3140 int MatAssemblyEnd(Mat mat,MatAssemblyType type) 3141 { 3142 int ierr; 3143 static int inassm = 0; 3144 3145 PetscFunctionBegin; 3146 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3147 PetscValidType(mat); 3148 MatPreallocated(mat); 3149 3150 inassm++; 3151 MatAssemblyEnd_InUse++; 3152 if (MatAssemblyEnd_InUse == 1) { /* Do the logging only the first time through */ 3153 ierr = PetscLogEventBegin(MAT_AssemblyEnd,mat,0,0,0);CHKERRQ(ierr); 3154 if (mat->ops->assemblyend) { 3155 ierr = (*mat->ops->assemblyend)(mat,type);CHKERRQ(ierr); 3156 } 3157 ierr = PetscLogEventEnd(MAT_AssemblyEnd,mat,0,0,0);CHKERRQ(ierr); 3158 } else { 3159 if (mat->ops->assemblyend) { 3160 ierr = (*mat->ops->assemblyend)(mat,type);CHKERRQ(ierr); 3161 } 3162 } 3163 3164 /* Flush assembly is not a true assembly */ 3165 if (type != MAT_FLUSH_ASSEMBLY) { 3166 mat->assembled = PETSC_TRUE; mat->num_ass++; 3167 } 3168 mat->insertmode = NOT_SET_VALUES; 3169 MatAssemblyEnd_InUse--; 3170 3171 if (inassm == 1 && type != MAT_FLUSH_ASSEMBLY) { 3172 ierr = MatView_Private(mat);CHKERRQ(ierr); 3173 } 3174 inassm--; 3175 PetscFunctionReturn(0); 3176 } 3177 3178 3179 #undef __FUNCT__ 3180 #define __FUNCT__ "MatCompress" 3181 /*@ 3182 MatCompress - Tries to store the matrix in as little space as 3183 possible. May fail if memory is already fully used, since it 3184 tries to allocate new space. 3185 3186 Collective on Mat 3187 3188 Input Parameters: 3189 . mat - the matrix 3190 3191 Level: advanced 3192 3193 @*/ 3194 int MatCompress(Mat mat) 3195 { 3196 int ierr; 3197 3198 PetscFunctionBegin; 3199 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3200 PetscValidType(mat); 3201 MatPreallocated(mat); 3202 if (mat->ops->compress) {ierr = (*mat->ops->compress)(mat);CHKERRQ(ierr);} 3203 PetscFunctionReturn(0); 3204 } 3205 3206 #undef __FUNCT__ 3207 #define __FUNCT__ "MatSetOption" 3208 /*@ 3209 MatSetOption - Sets a parameter option for a matrix. Some options 3210 may be specific to certain storage formats. Some options 3211 determine how values will be inserted (or added). Sorted, 3212 row-oriented input will generally assemble the fastest. The default 3213 is row-oriented, nonsorted input. 3214 3215 Collective on Mat 3216 3217 Input Parameters: 3218 + mat - the matrix 3219 - option - the option, one of those listed below (and possibly others), 3220 e.g., MAT_ROWS_SORTED, MAT_NEW_NONZERO_LOCATION_ERR 3221 3222 Options Describing Matrix Structure: 3223 + MAT_SYMMETRIC - symmetric in terms of both structure and value 3224 - MAT_STRUCTURALLY_SYMMETRIC - symmetric nonzero structure 3225 3226 Options For Use with MatSetValues(): 3227 Insert a logically dense subblock, which can be 3228 + MAT_ROW_ORIENTED - row-oriented (default) 3229 . MAT_COLUMN_ORIENTED - column-oriented 3230 . MAT_ROWS_SORTED - sorted by row 3231 . MAT_ROWS_UNSORTED - not sorted by row (default) 3232 . MAT_COLUMNS_SORTED - sorted by column 3233 - MAT_COLUMNS_UNSORTED - not sorted by column (default) 3234 3235 Not these options reflect the data you pass in with MatSetValues(); it has 3236 nothing to do with how the data is stored internally in the matrix 3237 data structure. 3238 3239 When (re)assembling a matrix, we can restrict the input for 3240 efficiency/debugging purposes. These options include 3241 + MAT_NO_NEW_NONZERO_LOCATIONS - additional insertions will not be 3242 allowed if they generate a new nonzero 3243 . MAT_YES_NEW_NONZERO_LOCATIONS - additional insertions will be allowed 3244 . MAT_NO_NEW_DIAGONALS - additional insertions will not be allowed if 3245 they generate a nonzero in a new diagonal (for block diagonal format only) 3246 . MAT_YES_NEW_DIAGONALS - new diagonals will be allowed (for block diagonal format only) 3247 . MAT_IGNORE_OFF_PROC_ENTRIES - drops off-processor entries 3248 . MAT_NEW_NONZERO_LOCATION_ERR - generates an error for new matrix entry 3249 - MAT_USE_HASH_TABLE - uses a hash table to speed up matrix assembly 3250 3251 Notes: 3252 Some options are relevant only for particular matrix types and 3253 are thus ignored by others. Other options are not supported by 3254 certain matrix types and will generate an error message if set. 3255 3256 If using a Fortran 77 module to compute a matrix, one may need to 3257 use the column-oriented option (or convert to the row-oriented 3258 format). 3259 3260 MAT_NO_NEW_NONZERO_LOCATIONS indicates that any add or insertion 3261 that would generate a new entry in the nonzero structure is instead 3262 ignored. Thus, if memory has not alredy been allocated for this particular 3263 data, then the insertion is ignored. For dense matrices, in which 3264 the entire array is allocated, no entries are ever ignored. 3265 Set after the first MatAssemblyEnd() 3266 3267 MAT_NEW_NONZERO_LOCATION_ERR indicates that any add or insertion 3268 that would generate a new entry in the nonzero structure instead produces 3269 an error. (Currently supported for AIJ and BAIJ formats only.) 3270 This is a useful flag when using SAME_NONZERO_PATTERN in calling 3271 SLESSetOperators() to ensure that the nonzero pattern truely does 3272 remain unchanged. Set after the first MatAssemblyEnd() 3273 3274 MAT_NEW_NONZERO_ALLOCATION_ERR indicates that any add or insertion 3275 that would generate a new entry that has not been preallocated will 3276 instead produce an error. (Currently supported for AIJ and BAIJ formats 3277 only.) This is a useful flag when debugging matrix memory preallocation. 3278 3279 MAT_IGNORE_OFF_PROC_ENTRIES indicates entries destined for 3280 other processors should be dropped, rather than stashed. 3281 This is useful if you know that the "owning" processor is also 3282 always generating the correct matrix entries, so that PETSc need 3283 not transfer duplicate entries generated on another processor. 3284 3285 MAT_USE_HASH_TABLE indicates that a hash table be used to improve the 3286 searches during matrix assembly. When this flag is set, the hash table 3287 is created during the first Matrix Assembly. This hash table is 3288 used the next time through, during MatSetVaules()/MatSetVaulesBlocked() 3289 to improve the searching of indices. MAT_NO_NEW_NONZERO_LOCATIONS flag 3290 should be used with MAT_USE_HASH_TABLE flag. This option is currently 3291 supported by MATMPIBAIJ format only. 3292 3293 MAT_KEEP_ZEROED_ROWS indicates when MatZeroRows() is called the zeroed entries 3294 are kept in the nonzero structure 3295 3296 MAT_IGNORE_ZERO_ENTRIES - when using ADD_VALUES for AIJ matrices this will stop 3297 zero values from creating a zero location in the matrix 3298 3299 MAT_USE_INODES - indicates using inode version of the code - works with AIJ and 3300 ROWBS matrix types 3301 3302 MAT_DO_NOT_USE_INODES - indicates not using inode version of the code - works 3303 with AIJ and ROWBS matrix types 3304 3305 Level: intermediate 3306 3307 Concepts: matrices^setting options 3308 3309 @*/ 3310 int MatSetOption(Mat mat,MatOption op) 3311 { 3312 int ierr; 3313 3314 PetscFunctionBegin; 3315 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3316 PetscValidType(mat); 3317 MatPreallocated(mat); 3318 switch (op) { 3319 case MAT_SYMMETRIC: 3320 mat->symmetric = PETSC_TRUE; 3321 mat->structurally_symmetric = PETSC_TRUE; 3322 break; 3323 case MAT_STRUCTURALLY_SYMMETRIC: 3324 mat->structurally_symmetric = PETSC_TRUE; 3325 break; 3326 default: 3327 if (mat->ops->setoption) {ierr = (*mat->ops->setoption)(mat,op);CHKERRQ(ierr);} 3328 break; 3329 } 3330 PetscFunctionReturn(0); 3331 } 3332 3333 #undef __FUNCT__ 3334 #define __FUNCT__ "MatZeroEntries" 3335 /*@ 3336 MatZeroEntries - Zeros all entries of a matrix. For sparse matrices 3337 this routine retains the old nonzero structure. 3338 3339 Collective on Mat 3340 3341 Input Parameters: 3342 . mat - the matrix 3343 3344 Level: intermediate 3345 3346 Concepts: matrices^zeroing 3347 3348 .seealso: MatZeroRows() 3349 @*/ 3350 int MatZeroEntries(Mat mat) 3351 { 3352 int ierr; 3353 3354 PetscFunctionBegin; 3355 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3356 PetscValidType(mat); 3357 MatPreallocated(mat); 3358 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3359 if (!mat->ops->zeroentries) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 3360 3361 ierr = PetscLogEventBegin(MAT_ZeroEntries,mat,0,0,0);CHKERRQ(ierr); 3362 ierr = (*mat->ops->zeroentries)(mat);CHKERRQ(ierr); 3363 ierr = PetscLogEventEnd(MAT_ZeroEntries,mat,0,0,0);CHKERRQ(ierr); 3364 PetscFunctionReturn(0); 3365 } 3366 3367 #undef __FUNCT__ 3368 #define __FUNCT__ "MatZeroRows" 3369 /*@C 3370 MatZeroRows - Zeros all entries (except possibly the main diagonal) 3371 of a set of rows of a matrix. 3372 3373 Collective on Mat 3374 3375 Input Parameters: 3376 + mat - the matrix 3377 . is - index set of rows to remove 3378 - diag - pointer to value put in all diagonals of eliminated rows. 3379 Note that diag is not a pointer to an array, but merely a 3380 pointer to a single value. 3381 3382 Notes: 3383 For the AIJ and BAIJ matrix formats this removes the old nonzero structure, 3384 but does not release memory. For the dense and block diagonal 3385 formats this does not alter the nonzero structure. 3386 3387 If the option MatSetOption(mat,MAT_KEEP_ZEROED_ROWS) the nonzero structure 3388 of the matrix is not changed (even for AIJ and BAIJ matrices) the values are 3389 merely zeroed. 3390 3391 The user can set a value in the diagonal entry (or for the AIJ and 3392 row formats can optionally remove the main diagonal entry from the 3393 nonzero structure as well, by passing a null pointer (PETSC_NULL 3394 in C or PETSC_NULL_SCALAR in Fortran) as the final argument). 3395 3396 For the parallel case, all processes that share the matrix (i.e., 3397 those in the communicator used for matrix creation) MUST call this 3398 routine, regardless of whether any rows being zeroed are owned by 3399 them. 3400 3401 3402 Level: intermediate 3403 3404 Concepts: matrices^zeroing rows 3405 3406 .seealso: MatZeroEntries(), MatZeroRowsLocal(), MatSetOption() 3407 @*/ 3408 int MatZeroRows(Mat mat,IS is,PetscScalar *diag) 3409 { 3410 int ierr; 3411 3412 PetscFunctionBegin; 3413 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3414 PetscValidType(mat); 3415 MatPreallocated(mat); 3416 PetscValidHeaderSpecific(is,IS_COOKIE); 3417 if (diag) PetscValidScalarPointer(diag); 3418 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3419 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3420 if (!mat->ops->zerorows) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 3421 3422 ierr = (*mat->ops->zerorows)(mat,is,diag);CHKERRQ(ierr); 3423 ierr = MatView_Private(mat);CHKERRQ(ierr); 3424 PetscFunctionReturn(0); 3425 } 3426 3427 #undef __FUNCT__ 3428 #define __FUNCT__ "MatZeroRowsLocal" 3429 /*@C 3430 MatZeroRowsLocal - Zeros all entries (except possibly the main diagonal) 3431 of a set of rows of a matrix; using local numbering of rows. 3432 3433 Collective on Mat 3434 3435 Input Parameters: 3436 + mat - the matrix 3437 . is - index set of rows to remove 3438 - diag - pointer to value put in all diagonals of eliminated rows. 3439 Note that diag is not a pointer to an array, but merely a 3440 pointer to a single value. 3441 3442 Notes: 3443 Before calling MatZeroRowsLocal(), the user must first set the 3444 local-to-global mapping by calling MatSetLocalToGlobalMapping(). 3445 3446 For the AIJ matrix formats this removes the old nonzero structure, 3447 but does not release memory. For the dense and block diagonal 3448 formats this does not alter the nonzero structure. 3449 3450 If the option MatSetOption(mat,MAT_KEEP_ZEROED_ROWS) the nonzero structure 3451 of the matrix is not changed (even for AIJ and BAIJ matrices) the values are 3452 merely zeroed. 3453 3454 The user can set a value in the diagonal entry (or for the AIJ and 3455 row formats can optionally remove the main diagonal entry from the 3456 nonzero structure as well, by passing a null pointer (PETSC_NULL 3457 in C or PETSC_NULL_SCALAR in Fortran) as the final argument). 3458 3459 Level: intermediate 3460 3461 Concepts: matrices^zeroing 3462 3463 .seealso: MatZeroEntries(), MatZeroRows(), MatSetLocalToGlobalMapping 3464 @*/ 3465 int MatZeroRowsLocal(Mat mat,IS is,PetscScalar *diag) 3466 { 3467 int ierr; 3468 IS newis; 3469 3470 PetscFunctionBegin; 3471 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3472 PetscValidType(mat); 3473 MatPreallocated(mat); 3474 PetscValidHeaderSpecific(is,IS_COOKIE); 3475 if (diag) PetscValidScalarPointer(diag); 3476 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3477 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3478 3479 if (mat->ops->zerorowslocal) { 3480 ierr = (*mat->ops->zerorowslocal)(mat,is,diag);CHKERRQ(ierr); 3481 } else { 3482 if (!mat->mapping) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Need to provide local to global mapping to matrix first"); 3483 ierr = ISLocalToGlobalMappingApplyIS(mat->mapping,is,&newis);CHKERRQ(ierr); 3484 ierr = (*mat->ops->zerorows)(mat,newis,diag);CHKERRQ(ierr); 3485 ierr = ISDestroy(newis);CHKERRQ(ierr); 3486 } 3487 PetscFunctionReturn(0); 3488 } 3489 3490 #undef __FUNCT__ 3491 #define __FUNCT__ "MatGetSize" 3492 /*@ 3493 MatGetSize - Returns the numbers of rows and columns in a matrix. 3494 3495 Not Collective 3496 3497 Input Parameter: 3498 . mat - the matrix 3499 3500 Output Parameters: 3501 + m - the number of global rows 3502 - n - the number of global columns 3503 3504 Level: beginner 3505 3506 Concepts: matrices^size 3507 3508 .seealso: MatGetLocalSize() 3509 @*/ 3510 int MatGetSize(Mat mat,int *m,int* n) 3511 { 3512 PetscFunctionBegin; 3513 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3514 if (m) *m = mat->M; 3515 if (n) *n = mat->N; 3516 PetscFunctionReturn(0); 3517 } 3518 3519 #undef __FUNCT__ 3520 #define __FUNCT__ "MatGetLocalSize" 3521 /*@ 3522 MatGetLocalSize - Returns the number of rows and columns in a matrix 3523 stored locally. This information may be implementation dependent, so 3524 use with care. 3525 3526 Not Collective 3527 3528 Input Parameters: 3529 . mat - the matrix 3530 3531 Output Parameters: 3532 + m - the number of local rows 3533 - n - the number of local columns 3534 3535 Level: beginner 3536 3537 Concepts: matrices^local size 3538 3539 .seealso: MatGetSize() 3540 @*/ 3541 int MatGetLocalSize(Mat mat,int *m,int* n) 3542 { 3543 PetscFunctionBegin; 3544 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3545 if (m) *m = mat->m; 3546 if (n) *n = mat->n; 3547 PetscFunctionReturn(0); 3548 } 3549 3550 #undef __FUNCT__ 3551 #define __FUNCT__ "MatGetOwnershipRange" 3552 /*@ 3553 MatGetOwnershipRange - Returns the range of matrix rows owned by 3554 this processor, assuming that the matrix is laid out with the first 3555 n1 rows on the first processor, the next n2 rows on the second, etc. 3556 For certain parallel layouts this range may not be well defined. 3557 3558 Not Collective 3559 3560 Input Parameters: 3561 . mat - the matrix 3562 3563 Output Parameters: 3564 + m - the global index of the first local row 3565 - n - one more than the global index of the last local row 3566 3567 Level: beginner 3568 3569 Concepts: matrices^row ownership 3570 @*/ 3571 int MatGetOwnershipRange(Mat mat,int *m,int* n) 3572 { 3573 int ierr; 3574 3575 PetscFunctionBegin; 3576 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3577 PetscValidType(mat); 3578 MatPreallocated(mat); 3579 if (m) PetscValidIntPointer(m); 3580 if (n) PetscValidIntPointer(n); 3581 ierr = PetscMapGetLocalRange(mat->rmap,m,n);CHKERRQ(ierr); 3582 PetscFunctionReturn(0); 3583 } 3584 3585 #undef __FUNCT__ 3586 #define __FUNCT__ "MatILUFactorSymbolic" 3587 /*@ 3588 MatILUFactorSymbolic - Performs symbolic ILU factorization of a matrix. 3589 Uses levels of fill only, not drop tolerance. Use MatLUFactorNumeric() 3590 to complete the factorization. 3591 3592 Collective on Mat 3593 3594 Input Parameters: 3595 + mat - the matrix 3596 . row - row permutation 3597 . column - column permutation 3598 - info - structure containing 3599 $ levels - number of levels of fill. 3600 $ expected fill - as ratio of original fill. 3601 $ 1 or 0 - indicating force fill on diagonal (improves robustness for matrices 3602 missing diagonal entries) 3603 3604 Output Parameters: 3605 . fact - new matrix that has been symbolically factored 3606 3607 Notes: 3608 See the users manual for additional information about 3609 choosing the fill factor for better efficiency. 3610 3611 Most users should employ the simplified SLES interface for linear solvers 3612 instead of working directly with matrix algebra routines such as this. 3613 See, e.g., SLESCreate(). 3614 3615 Level: developer 3616 3617 Concepts: matrices^symbolic LU factorization 3618 Concepts: matrices^factorization 3619 Concepts: LU^symbolic factorization 3620 3621 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor() 3622 MatGetOrdering(), MatILUInfo 3623 3624 @*/ 3625 int MatILUFactorSymbolic(Mat mat,IS row,IS col,MatILUInfo *info,Mat *fact) 3626 { 3627 int ierr; 3628 3629 PetscFunctionBegin; 3630 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3631 PetscValidType(mat); 3632 MatPreallocated(mat); 3633 PetscValidPointer(fact); 3634 PetscValidHeaderSpecific(row,IS_COOKIE); 3635 PetscValidHeaderSpecific(col,IS_COOKIE); 3636 if (info && info->levels < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Levels of fill negative %d",(int)info->levels); 3637 if (info && info->fill < 1.0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %g",info->fill); 3638 if (!mat->ops->ilufactorsymbolic) SETERRQ1(PETSC_ERR_SUP,"Matrix type %s symbolic ILU",mat->type_name); 3639 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3640 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3641 3642 ierr = PetscLogEventBegin(MAT_ILUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr); 3643 ierr = (*mat->ops->ilufactorsymbolic)(mat,row,col,info,fact);CHKERRQ(ierr); 3644 ierr = PetscLogEventEnd(MAT_ILUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr); 3645 PetscFunctionReturn(0); 3646 } 3647 3648 #undef __FUNCT__ 3649 #define __FUNCT__ "MatICCFactorSymbolic" 3650 /*@ 3651 MatICCFactorSymbolic - Performs symbolic incomplete 3652 Cholesky factorization for a symmetric matrix. Use 3653 MatCholeskyFactorNumeric() to complete the factorization. 3654 3655 Collective on Mat 3656 3657 Input Parameters: 3658 + mat - the matrix 3659 . perm - row and column permutation 3660 . fill - levels of fill 3661 - f - expected fill as ratio of original fill 3662 3663 Output Parameter: 3664 . fact - the factored matrix 3665 3666 Notes: 3667 Currently only no-fill factorization is supported. 3668 3669 Most users should employ the simplified SLES interface for linear solvers 3670 instead of working directly with matrix algebra routines such as this. 3671 See, e.g., SLESCreate(). 3672 3673 Level: developer 3674 3675 Concepts: matrices^symbolic incomplete Cholesky factorization 3676 Concepts: matrices^factorization 3677 Concepts: Cholsky^symbolic factorization 3678 3679 .seealso: MatCholeskyFactorNumeric(), MatCholeskyFactor() 3680 @*/ 3681 int MatICCFactorSymbolic(Mat mat,IS perm,PetscReal f,int fill,Mat *fact) 3682 { 3683 int ierr; 3684 3685 PetscFunctionBegin; 3686 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3687 PetscValidType(mat); 3688 MatPreallocated(mat); 3689 PetscValidPointer(fact); 3690 PetscValidHeaderSpecific(perm,IS_COOKIE); 3691 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3692 if (fill < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Fill negative %d",fill); 3693 if (f < 1.0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %g",f); 3694 if (!mat->ops->iccfactorsymbolic) SETERRQ1(PETSC_ERR_SUP,"Matrix type %s symbolic ICC",mat->type_name); 3695 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3696 3697 ierr = PetscLogEventBegin(MAT_ICCFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr); 3698 ierr = (*mat->ops->iccfactorsymbolic)(mat,perm,f,fill,fact);CHKERRQ(ierr); 3699 ierr = PetscLogEventEnd(MAT_ICCFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr); 3700 PetscFunctionReturn(0); 3701 } 3702 3703 #undef __FUNCT__ 3704 #define __FUNCT__ "MatGetArray" 3705 /*@C 3706 MatGetArray - Returns a pointer to the element values in the matrix. 3707 The result of this routine is dependent on the underlying matrix data 3708 structure, and may not even work for certain matrix types. You MUST 3709 call MatRestoreArray() when you no longer need to access the array. 3710 3711 Not Collective 3712 3713 Input Parameter: 3714 . mat - the matrix 3715 3716 Output Parameter: 3717 . v - the location of the values 3718 3719 3720 Fortran Note: 3721 This routine is used differently from Fortran, e.g., 3722 .vb 3723 Mat mat 3724 PetscScalar mat_array(1) 3725 PetscOffset i_mat 3726 int ierr 3727 call MatGetArray(mat,mat_array,i_mat,ierr) 3728 3729 C Access first local entry in matrix; note that array is 3730 C treated as one dimensional 3731 value = mat_array(i_mat + 1) 3732 3733 [... other code ...] 3734 call MatRestoreArray(mat,mat_array,i_mat,ierr) 3735 .ve 3736 3737 See the Fortran chapter of the users manual and 3738 petsc/src/mat/examples/tests for details. 3739 3740 Level: advanced 3741 3742 Concepts: matrices^access array 3743 3744 .seealso: MatRestoreArray(), MatGetArrayF90() 3745 @*/ 3746 int MatGetArray(Mat mat,PetscScalar **v) 3747 { 3748 int ierr; 3749 3750 PetscFunctionBegin; 3751 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3752 PetscValidType(mat); 3753 MatPreallocated(mat); 3754 PetscValidPointer(v); 3755 if (!mat->ops->getarray) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 3756 ierr = (*mat->ops->getarray)(mat,v);CHKERRQ(ierr); 3757 PetscFunctionReturn(0); 3758 } 3759 3760 #undef __FUNCT__ 3761 #define __FUNCT__ "MatRestoreArray" 3762 /*@C 3763 MatRestoreArray - Restores the matrix after MatGetArray() has been called. 3764 3765 Not Collective 3766 3767 Input Parameter: 3768 + mat - the matrix 3769 - v - the location of the values 3770 3771 Fortran Note: 3772 This routine is used differently from Fortran, e.g., 3773 .vb 3774 Mat mat 3775 PetscScalar mat_array(1) 3776 PetscOffset i_mat 3777 int ierr 3778 call MatGetArray(mat,mat_array,i_mat,ierr) 3779 3780 C Access first local entry in matrix; note that array is 3781 C treated as one dimensional 3782 value = mat_array(i_mat + 1) 3783 3784 [... other code ...] 3785 call MatRestoreArray(mat,mat_array,i_mat,ierr) 3786 .ve 3787 3788 See the Fortran chapter of the users manual and 3789 petsc/src/mat/examples/tests for details 3790 3791 Level: advanced 3792 3793 .seealso: MatGetArray(), MatRestoreArrayF90() 3794 @*/ 3795 int MatRestoreArray(Mat mat,PetscScalar **v) 3796 { 3797 int ierr; 3798 3799 PetscFunctionBegin; 3800 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3801 PetscValidType(mat); 3802 MatPreallocated(mat); 3803 PetscValidPointer(v); 3804 if (!mat->ops->restorearray) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 3805 ierr = (*mat->ops->restorearray)(mat,v);CHKERRQ(ierr); 3806 PetscFunctionReturn(0); 3807 } 3808 3809 #undef __FUNCT__ 3810 #define __FUNCT__ "MatGetSubMatrices" 3811 /*@C 3812 MatGetSubMatrices - Extracts several submatrices from a matrix. If submat 3813 points to an array of valid matrices, they may be reused to store the new 3814 submatrices. 3815 3816 Collective on Mat 3817 3818 Input Parameters: 3819 + mat - the matrix 3820 . n - the number of submatrixes to be extracted (on this processor, may be zero) 3821 . irow, icol - index sets of rows and columns to extract 3822 - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 3823 3824 Output Parameter: 3825 . submat - the array of submatrices 3826 3827 Notes: 3828 MatGetSubMatrices() can extract only sequential submatrices 3829 (from both sequential and parallel matrices). Use MatGetSubMatrix() 3830 to extract a parallel submatrix. 3831 3832 When extracting submatrices from a parallel matrix, each processor can 3833 form a different submatrix by setting the rows and columns of its 3834 individual index sets according to the local submatrix desired. 3835 3836 When finished using the submatrices, the user should destroy 3837 them with MatDestroyMatrices(). 3838 3839 MAT_REUSE_MATRIX can only be used when the nonzero structure of the 3840 original matrix has not changed from that last call to MatGetSubMatrices(). 3841 3842 This routine creates the matrices submat; you should NOT create them before 3843 calling it. 3844 3845 Fortran Note: 3846 The Fortran interface is slightly different from that given below; it 3847 requires one to pass in as submat a Mat (integer) array of size at least m. 3848 3849 Level: advanced 3850 3851 Concepts: matrices^accessing submatrices 3852 Concepts: submatrices 3853 3854 .seealso: MatDestroyMatrices(), MatGetSubMatrix(), MatGetRow(), MatGetDiagonal() 3855 @*/ 3856 int MatGetSubMatrices(Mat mat,int n,IS *irow,IS *icol,MatReuse scall,Mat **submat) 3857 { 3858 int ierr; 3859 3860 PetscFunctionBegin; 3861 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3862 PetscValidType(mat); 3863 MatPreallocated(mat); 3864 if (!mat->ops->getsubmatrices) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 3865 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3866 3867 ierr = PetscLogEventBegin(MAT_GetSubMatrices,mat,0,0,0);CHKERRQ(ierr); 3868 ierr = (*mat->ops->getsubmatrices)(mat,n,irow,icol,scall,submat);CHKERRQ(ierr); 3869 ierr = PetscLogEventEnd(MAT_GetSubMatrices,mat,0,0,0);CHKERRQ(ierr); 3870 PetscFunctionReturn(0); 3871 } 3872 3873 #undef __FUNCT__ 3874 #define __FUNCT__ "MatDestroyMatrices" 3875 /*@C 3876 MatDestroyMatrices - Destroys a set of matrices obtained with MatGetSubMatrices(). 3877 3878 Collective on Mat 3879 3880 Input Parameters: 3881 + n - the number of local matrices 3882 - mat - the matrices 3883 3884 Level: advanced 3885 3886 Notes: Frees not only the matrices, but also the array that contains the matrices 3887 3888 .seealso: MatGetSubMatrices() 3889 @*/ 3890 int MatDestroyMatrices(int n,Mat **mat) 3891 { 3892 int ierr,i; 3893 3894 PetscFunctionBegin; 3895 if (n < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Trying to destroy negative number of matrices %d",n); 3896 PetscValidPointer(mat); 3897 for (i=0; i<n; i++) { 3898 ierr = MatDestroy((*mat)[i]);CHKERRQ(ierr); 3899 } 3900 /* memory is allocated even if n = 0 */ 3901 ierr = PetscFree(*mat);CHKERRQ(ierr); 3902 PetscFunctionReturn(0); 3903 } 3904 3905 #undef __FUNCT__ 3906 #define __FUNCT__ "MatIncreaseOverlap" 3907 /*@ 3908 MatIncreaseOverlap - Given a set of submatrices indicated by index sets, 3909 replaces the index sets by larger ones that represent submatrices with 3910 additional overlap. 3911 3912 Collective on Mat 3913 3914 Input Parameters: 3915 + mat - the matrix 3916 . n - the number of index sets 3917 . is - the array of pointers to index sets 3918 - ov - the additional overlap requested 3919 3920 Level: developer 3921 3922 Concepts: overlap 3923 Concepts: ASM^computing overlap 3924 3925 .seealso: MatGetSubMatrices() 3926 @*/ 3927 int MatIncreaseOverlap(Mat mat,int n,IS *is,int ov) 3928 { 3929 int ierr; 3930 3931 PetscFunctionBegin; 3932 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3933 PetscValidType(mat); 3934 MatPreallocated(mat); 3935 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3936 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3937 3938 if (!ov) PetscFunctionReturn(0); 3939 if (!mat->ops->increaseoverlap) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 3940 ierr = PetscLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr); 3941 ierr = (*mat->ops->increaseoverlap)(mat,n,is,ov);CHKERRQ(ierr); 3942 ierr = PetscLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr); 3943 PetscFunctionReturn(0); 3944 } 3945 3946 #undef __FUNCT__ 3947 #define __FUNCT__ "MatPrintHelp" 3948 /*@ 3949 MatPrintHelp - Prints all the options for the matrix. 3950 3951 Collective on Mat 3952 3953 Input Parameter: 3954 . mat - the matrix 3955 3956 Options Database Keys: 3957 + -help - Prints matrix options 3958 - -h - Prints matrix options 3959 3960 Level: developer 3961 3962 .seealso: MatCreate(), MatCreateXXX() 3963 @*/ 3964 int MatPrintHelp(Mat mat) 3965 { 3966 static PetscTruth called = PETSC_FALSE; 3967 int ierr; 3968 MPI_Comm comm; 3969 3970 PetscFunctionBegin; 3971 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3972 PetscValidType(mat); 3973 MatPreallocated(mat); 3974 3975 comm = mat->comm; 3976 if (!called) { 3977 ierr = (*PetscHelpPrintf)(comm,"General matrix options:\n");CHKERRQ(ierr); 3978 ierr = (*PetscHelpPrintf)(comm," -mat_view_info: view basic matrix info during MatAssemblyEnd()\n");CHKERRQ(ierr); 3979 ierr = (*PetscHelpPrintf)(comm," -mat_view_info_detailed: view detailed matrix info during MatAssemblyEnd()\n");CHKERRQ(ierr); 3980 ierr = (*PetscHelpPrintf)(comm," -mat_view_draw: draw nonzero matrix structure during MatAssemblyEnd()\n");CHKERRQ(ierr); 3981 ierr = (*PetscHelpPrintf)(comm," -draw_pause <sec>: set seconds of display pause\n");CHKERRQ(ierr); 3982 ierr = (*PetscHelpPrintf)(comm," -display <name>: set alternate display\n");CHKERRQ(ierr); 3983 called = PETSC_TRUE; 3984 } 3985 if (mat->ops->printhelp) { 3986 ierr = (*mat->ops->printhelp)(mat);CHKERRQ(ierr); 3987 } 3988 PetscFunctionReturn(0); 3989 } 3990 3991 #undef __FUNCT__ 3992 #define __FUNCT__ "MatGetBlockSize" 3993 /*@ 3994 MatGetBlockSize - Returns the matrix block size; useful especially for the 3995 block row and block diagonal formats. 3996 3997 Not Collective 3998 3999 Input Parameter: 4000 . mat - the matrix 4001 4002 Output Parameter: 4003 . bs - block size 4004 4005 Notes: 4006 Block diagonal formats are MATSEQBDIAG, MATMPIBDIAG. 4007 Block row formats are MATSEQBAIJ, MATMPIBAIJ 4008 4009 Level: intermediate 4010 4011 Concepts: matrices^block size 4012 4013 .seealso: MatCreateSeqBAIJ(), MatCreateMPIBAIJ(), MatCreateSeqBDiag(), MatCreateMPIBDiag() 4014 @*/ 4015 int MatGetBlockSize(Mat mat,int *bs) 4016 { 4017 int ierr; 4018 4019 PetscFunctionBegin; 4020 PetscValidHeaderSpecific(mat,MAT_COOKIE); 4021 PetscValidType(mat); 4022 MatPreallocated(mat); 4023 PetscValidIntPointer(bs); 4024 if (!mat->ops->getblocksize) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 4025 ierr = (*mat->ops->getblocksize)(mat,bs);CHKERRQ(ierr); 4026 PetscFunctionReturn(0); 4027 } 4028 4029 #undef __FUNCT__ 4030 #define __FUNCT__ "MatGetRowIJ" 4031 /*@C 4032 MatGetRowIJ - Returns the compressed row storage i and j indices for sequential matrices. 4033 4034 Collective on Mat 4035 4036 Input Parameters: 4037 + mat - the matrix 4038 . shift - 0 or 1 indicating we want the indices starting at 0 or 1 4039 - symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 4040 symmetrized 4041 4042 Output Parameters: 4043 + n - number of rows in the (possibly compressed) matrix 4044 . ia - the row pointers 4045 . ja - the column indices 4046 - done - PETSC_TRUE or PETSC_FALSE, indicating whether the values have been returned 4047 4048 Level: developer 4049 4050 .seealso: MatGetColumnIJ(), MatRestoreRowIJ() 4051 @*/ 4052 int MatGetRowIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int **ia,int** ja,PetscTruth *done) 4053 { 4054 int ierr; 4055 4056 PetscFunctionBegin; 4057 PetscValidHeaderSpecific(mat,MAT_COOKIE); 4058 PetscValidType(mat); 4059 MatPreallocated(mat); 4060 if (ia) PetscValidIntPointer(ia); 4061 if (ja) PetscValidIntPointer(ja); 4062 PetscValidIntPointer(done); 4063 if (!mat->ops->getrowij) *done = PETSC_FALSE; 4064 else { 4065 *done = PETSC_TRUE; 4066 ierr = (*mat->ops->getrowij)(mat,shift,symmetric,n,ia,ja,done);CHKERRQ(ierr); 4067 } 4068 PetscFunctionReturn(0); 4069 } 4070 4071 #undef __FUNCT__ 4072 #define __FUNCT__ "MatGetColumnIJ" 4073 /*@C 4074 MatGetColumnIJ - Returns the compressed column storage i and j indices for sequential matrices. 4075 4076 Collective on Mat 4077 4078 Input Parameters: 4079 + mat - the matrix 4080 . shift - 1 or zero indicating we want the indices starting at 0 or 1 4081 - symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 4082 symmetrized 4083 4084 Output Parameters: 4085 + n - number of columns in the (possibly compressed) matrix 4086 . ia - the column pointers 4087 . ja - the row indices 4088 - done - PETSC_TRUE or PETSC_FALSE, indicating whether the values have been returned 4089 4090 Level: developer 4091 4092 .seealso: MatGetRowIJ(), MatRestoreColumnIJ() 4093 @*/ 4094 int MatGetColumnIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int **ia,int** ja,PetscTruth *done) 4095 { 4096 int ierr; 4097 4098 PetscFunctionBegin; 4099 PetscValidHeaderSpecific(mat,MAT_COOKIE); 4100 PetscValidType(mat); 4101 MatPreallocated(mat); 4102 if (ia) PetscValidIntPointer(ia); 4103 if (ja) PetscValidIntPointer(ja); 4104 PetscValidIntPointer(done); 4105 4106 if (!mat->ops->getcolumnij) *done = PETSC_FALSE; 4107 else { 4108 *done = PETSC_TRUE; 4109 ierr = (*mat->ops->getcolumnij)(mat,shift,symmetric,n,ia,ja,done);CHKERRQ(ierr); 4110 } 4111 PetscFunctionReturn(0); 4112 } 4113 4114 #undef __FUNCT__ 4115 #define __FUNCT__ "MatRestoreRowIJ" 4116 /*@C 4117 MatRestoreRowIJ - Call after you are completed with the ia,ja indices obtained with 4118 MatGetRowIJ(). 4119 4120 Collective on Mat 4121 4122 Input Parameters: 4123 + mat - the matrix 4124 . shift - 1 or zero indicating we want the indices starting at 0 or 1 4125 - symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 4126 symmetrized 4127 4128 Output Parameters: 4129 + n - size of (possibly compressed) matrix 4130 . ia - the row pointers 4131 . ja - the column indices 4132 - done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned 4133 4134 Level: developer 4135 4136 .seealso: MatGetRowIJ(), MatRestoreColumnIJ() 4137 @*/ 4138 int MatRestoreRowIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int **ia,int** ja,PetscTruth *done) 4139 { 4140 int ierr; 4141 4142 PetscFunctionBegin; 4143 PetscValidHeaderSpecific(mat,MAT_COOKIE); 4144 PetscValidType(mat); 4145 MatPreallocated(mat); 4146 if (ia) PetscValidIntPointer(ia); 4147 if (ja) PetscValidIntPointer(ja); 4148 PetscValidIntPointer(done); 4149 4150 if (!mat->ops->restorerowij) *done = PETSC_FALSE; 4151 else { 4152 *done = PETSC_TRUE; 4153 ierr = (*mat->ops->restorerowij)(mat,shift,symmetric,n,ia,ja,done);CHKERRQ(ierr); 4154 } 4155 PetscFunctionReturn(0); 4156 } 4157 4158 #undef __FUNCT__ 4159 #define __FUNCT__ "MatRestoreColumnIJ" 4160 /*@C 4161 MatRestoreColumnIJ - Call after you are completed with the ia,ja indices obtained with 4162 MatGetColumnIJ(). 4163 4164 Collective on Mat 4165 4166 Input Parameters: 4167 + mat - the matrix 4168 . shift - 1 or zero indicating we want the indices starting at 0 or 1 4169 - symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 4170 symmetrized 4171 4172 Output Parameters: 4173 + n - size of (possibly compressed) matrix 4174 . ia - the column pointers 4175 . ja - the row indices 4176 - done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned 4177 4178 Level: developer 4179 4180 .seealso: MatGetColumnIJ(), MatRestoreRowIJ() 4181 @*/ 4182 int MatRestoreColumnIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int **ia,int** ja,PetscTruth *done) 4183 { 4184 int ierr; 4185 4186 PetscFunctionBegin; 4187 PetscValidHeaderSpecific(mat,MAT_COOKIE); 4188 PetscValidType(mat); 4189 MatPreallocated(mat); 4190 if (ia) PetscValidIntPointer(ia); 4191 if (ja) PetscValidIntPointer(ja); 4192 PetscValidIntPointer(done); 4193 4194 if (!mat->ops->restorecolumnij) *done = PETSC_FALSE; 4195 else { 4196 *done = PETSC_TRUE; 4197 ierr = (*mat->ops->restorecolumnij)(mat,shift,symmetric,n,ia,ja,done);CHKERRQ(ierr); 4198 } 4199 PetscFunctionReturn(0); 4200 } 4201 4202 #undef __FUNCT__ 4203 #define __FUNCT__ "MatColoringPatch" 4204 /*@C 4205 MatColoringPatch -Used inside matrix coloring routines that 4206 use MatGetRowIJ() and/or MatGetColumnIJ(). 4207 4208 Collective on Mat 4209 4210 Input Parameters: 4211 + mat - the matrix 4212 . n - number of colors 4213 - colorarray - array indicating color for each column 4214 4215 Output Parameters: 4216 . iscoloring - coloring generated using colorarray information 4217 4218 Level: developer 4219 4220 .seealso: MatGetRowIJ(), MatGetColumnIJ() 4221 4222 @*/ 4223 int MatColoringPatch(Mat mat,int n,int ncolors,int *colorarray,ISColoring *iscoloring) 4224 { 4225 int ierr; 4226 4227 PetscFunctionBegin; 4228 PetscValidHeaderSpecific(mat,MAT_COOKIE); 4229 PetscValidType(mat); 4230 MatPreallocated(mat); 4231 PetscValidIntPointer(colorarray); 4232 4233 if (!mat->ops->coloringpatch){ 4234 ierr = ISColoringCreate(mat->comm,n,colorarray,iscoloring);CHKERRQ(ierr); 4235 } else { 4236 ierr = (*mat->ops->coloringpatch)(mat,n,ncolors,colorarray,iscoloring);CHKERRQ(ierr); 4237 } 4238 PetscFunctionReturn(0); 4239 } 4240 4241 4242 #undef __FUNCT__ 4243 #define __FUNCT__ "MatSetUnfactored" 4244 /*@ 4245 MatSetUnfactored - Resets a factored matrix to be treated as unfactored. 4246 4247 Collective on Mat 4248 4249 Input Parameter: 4250 . mat - the factored matrix to be reset 4251 4252 Notes: 4253 This routine should be used only with factored matrices formed by in-place 4254 factorization via ILU(0) (or by in-place LU factorization for the MATSEQDENSE 4255 format). This option can save memory, for example, when solving nonlinear 4256 systems with a matrix-free Newton-Krylov method and a matrix-based, in-place 4257 ILU(0) preconditioner. 4258 4259 Note that one can specify in-place ILU(0) factorization by calling 4260 .vb 4261 PCType(pc,PCILU); 4262 PCILUSeUseInPlace(pc); 4263 .ve 4264 or by using the options -pc_type ilu -pc_ilu_in_place 4265 4266 In-place factorization ILU(0) can also be used as a local 4267 solver for the blocks within the block Jacobi or additive Schwarz 4268 methods (runtime option: -sub_pc_ilu_in_place). See the discussion 4269 of these preconditioners in the users manual for details on setting 4270 local solver options. 4271 4272 Most users should employ the simplified SLES interface for linear solvers 4273 instead of working directly with matrix algebra routines such as this. 4274 See, e.g., SLESCreate(). 4275 4276 Level: developer 4277 4278 .seealso: PCILUSetUseInPlace(), PCLUSetUseInPlace() 4279 4280 Concepts: matrices^unfactored 4281 4282 @*/ 4283 int MatSetUnfactored(Mat mat) 4284 { 4285 int ierr; 4286 4287 PetscFunctionBegin; 4288 PetscValidHeaderSpecific(mat,MAT_COOKIE); 4289 PetscValidType(mat); 4290 MatPreallocated(mat); 4291 mat->factor = 0; 4292 if (!mat->ops->setunfactored) PetscFunctionReturn(0); 4293 ierr = (*mat->ops->setunfactored)(mat);CHKERRQ(ierr); 4294 PetscFunctionReturn(0); 4295 } 4296 4297 /*MC 4298 MatGetArrayF90 - Accesses a matrix array from Fortran90. 4299 4300 Synopsis: 4301 MatGetArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr) 4302 4303 Not collective 4304 4305 Input Parameter: 4306 . x - matrix 4307 4308 Output Parameters: 4309 + xx_v - the Fortran90 pointer to the array 4310 - ierr - error code 4311 4312 Example of Usage: 4313 .vb 4314 PetscScalar, pointer xx_v(:) 4315 .... 4316 call MatGetArrayF90(x,xx_v,ierr) 4317 a = xx_v(3) 4318 call MatRestoreArrayF90(x,xx_v,ierr) 4319 .ve 4320 4321 Notes: 4322 Not yet supported for all F90 compilers 4323 4324 Level: advanced 4325 4326 .seealso: MatRestoreArrayF90(), MatGetArray(), MatRestoreArray() 4327 4328 Concepts: matrices^accessing array 4329 4330 M*/ 4331 4332 /*MC 4333 MatRestoreArrayF90 - Restores a matrix array that has been 4334 accessed with MatGetArrayF90(). 4335 4336 Synopsis: 4337 MatRestoreArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr) 4338 4339 Not collective 4340 4341 Input Parameters: 4342 + x - matrix 4343 - xx_v - the Fortran90 pointer to the array 4344 4345 Output Parameter: 4346 . ierr - error code 4347 4348 Example of Usage: 4349 .vb 4350 PetscScalar, pointer xx_v(:) 4351 .... 4352 call MatGetArrayF90(x,xx_v,ierr) 4353 a = xx_v(3) 4354 call MatRestoreArrayF90(x,xx_v,ierr) 4355 .ve 4356 4357 Notes: 4358 Not yet supported for all F90 compilers 4359 4360 Level: advanced 4361 4362 .seealso: MatGetArrayF90(), MatGetArray(), MatRestoreArray() 4363 4364 M*/ 4365 4366 4367 #undef __FUNCT__ 4368 #define __FUNCT__ "MatGetSubMatrix" 4369 /*@ 4370 MatGetSubMatrix - Gets a single submatrix on the same number of processors 4371 as the original matrix. 4372 4373 Collective on Mat 4374 4375 Input Parameters: 4376 + mat - the original matrix 4377 . isrow - rows this processor should obtain 4378 . iscol - columns for all processors you wish to keep 4379 . csize - number of columns "local" to this processor (does nothing for sequential 4380 matrices). This should match the result from VecGetLocalSize(x,...) if you 4381 plan to use the matrix in a A*x; alternatively, you can use PETSC_DECIDE 4382 - cll - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 4383 4384 Output Parameter: 4385 . newmat - the new submatrix, of the same type as the old 4386 4387 Level: advanced 4388 4389 Notes: the iscol argument MUST be the same on each processor. You might be 4390 able to create the iscol argument with ISAllGather(). 4391 4392 The first time this is called you should use a cll of MAT_INITIAL_MATRIX, 4393 the MatGetSubMatrix() routine will create the newmat for you. Any additional calls 4394 to this routine with a mat of the same nonzero structure will reuse the matrix 4395 generated the first time. 4396 4397 Concepts: matrices^submatrices 4398 4399 .seealso: MatGetSubMatrices(), ISAllGather() 4400 @*/ 4401 int MatGetSubMatrix(Mat mat,IS isrow,IS iscol,int csize,MatReuse cll,Mat *newmat) 4402 { 4403 int ierr, size; 4404 Mat *local; 4405 4406 PetscFunctionBegin; 4407 PetscValidType(mat); 4408 MatPreallocated(mat); 4409 ierr = MPI_Comm_size(mat->comm,&size);CHKERRQ(ierr); 4410 4411 /* if original matrix is on just one processor then use submatrix generated */ 4412 if (!mat->ops->getsubmatrix && size == 1 && cll == MAT_REUSE_MATRIX) { 4413 ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&newmat);CHKERRQ(ierr); 4414 PetscFunctionReturn(0); 4415 } else if (!mat->ops->getsubmatrix && size == 1) { 4416 ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr); 4417 *newmat = *local; 4418 ierr = PetscFree(local);CHKERRQ(ierr); 4419 PetscFunctionReturn(0); 4420 } 4421 4422 if (!mat->ops->getsubmatrix) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 4423 ierr = (*mat->ops->getsubmatrix)(mat,isrow,iscol,csize,cll,newmat);CHKERRQ(ierr); 4424 PetscFunctionReturn(0); 4425 } 4426 4427 #undef __FUNCT__ 4428 #define __FUNCT__ "MatGetPetscMaps" 4429 /*@C 4430 MatGetPetscMaps - Returns the maps associated with the matrix. 4431 4432 Not Collective 4433 4434 Input Parameter: 4435 . mat - the matrix 4436 4437 Output Parameters: 4438 + rmap - the row (right) map 4439 - cmap - the column (left) map 4440 4441 Level: developer 4442 4443 Concepts: maps^getting from matrix 4444 4445 @*/ 4446 int MatGetPetscMaps(Mat mat,PetscMap *rmap,PetscMap *cmap) 4447 { 4448 int ierr; 4449 4450 PetscFunctionBegin; 4451 PetscValidHeaderSpecific(mat,MAT_COOKIE); 4452 PetscValidType(mat); 4453 MatPreallocated(mat); 4454 ierr = (*mat->ops->getmaps)(mat,rmap,cmap);CHKERRQ(ierr); 4455 PetscFunctionReturn(0); 4456 } 4457 4458 /* 4459 Version that works for all PETSc matrices 4460 */ 4461 #undef __FUNCT__ 4462 #define __FUNCT__ "MatGetPetscMaps_Petsc" 4463 int MatGetPetscMaps_Petsc(Mat mat,PetscMap *rmap,PetscMap *cmap) 4464 { 4465 PetscFunctionBegin; 4466 if (rmap) *rmap = mat->rmap; 4467 if (cmap) *cmap = mat->cmap; 4468 PetscFunctionReturn(0); 4469 } 4470 4471 #undef __FUNCT__ 4472 #define __FUNCT__ "MatSetStashInitialSize" 4473 /*@ 4474 MatSetStashInitialSize - sets the sizes of the matrix stash, that is 4475 used during the assembly process to store values that belong to 4476 other processors. 4477 4478 Not Collective 4479 4480 Input Parameters: 4481 + mat - the matrix 4482 . size - the initial size of the stash. 4483 - bsize - the initial size of the block-stash(if used). 4484 4485 Options Database Keys: 4486 + -matstash_initial_size <size> or <size0,size1,...sizep-1> 4487 - -matstash_block_initial_size <bsize> or <bsize0,bsize1,...bsizep-1> 4488 4489 Level: intermediate 4490 4491 Notes: 4492 The block-stash is used for values set with VecSetValuesBlocked() while 4493 the stash is used for values set with VecSetValues() 4494 4495 Run with the option -log_info and look for output of the form 4496 MatAssemblyBegin_MPIXXX:Stash has MM entries, uses nn mallocs. 4497 to determine the appropriate value, MM, to use for size and 4498 MatAssemblyBegin_MPIXXX:Block-Stash has BMM entries, uses nn mallocs. 4499 to determine the value, BMM to use for bsize 4500 4501 Concepts: stash^setting matrix size 4502 Concepts: matrices^stash 4503 4504 @*/ 4505 int MatSetStashInitialSize(Mat mat,int size, int bsize) 4506 { 4507 int ierr; 4508 4509 PetscFunctionBegin; 4510 PetscValidHeaderSpecific(mat,MAT_COOKIE); 4511 PetscValidType(mat); 4512 MatPreallocated(mat); 4513 ierr = MatStashSetInitialSize_Private(&mat->stash,size);CHKERRQ(ierr); 4514 ierr = MatStashSetInitialSize_Private(&mat->bstash,bsize);CHKERRQ(ierr); 4515 PetscFunctionReturn(0); 4516 } 4517 4518 #undef __FUNCT__ 4519 #define __FUNCT__ "MatInterpolateAdd" 4520 /*@ 4521 MatInterpolateAdd - w = y + A*x or A'*x depending on the shape of 4522 the matrix 4523 4524 Collective on Mat 4525 4526 Input Parameters: 4527 + mat - the matrix 4528 . x,y - the vectors 4529 - w - where the result is stored 4530 4531 Level: intermediate 4532 4533 Notes: 4534 w may be the same vector as y. 4535 4536 This allows one to use either the restriction or interpolation (its transpose) 4537 matrix to do the interpolation 4538 4539 Concepts: interpolation 4540 4541 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict() 4542 4543 @*/ 4544 int MatInterpolateAdd(Mat A,Vec x,Vec y,Vec w) 4545 { 4546 int M,N,ierr; 4547 4548 PetscFunctionBegin; 4549 PetscValidType(A); 4550 MatPreallocated(A); 4551 ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr); 4552 if (N > M) { 4553 ierr = MatMultTransposeAdd(A,x,y,w);CHKERRQ(ierr); 4554 } else { 4555 ierr = MatMultAdd(A,x,y,w);CHKERRQ(ierr); 4556 } 4557 PetscFunctionReturn(0); 4558 } 4559 4560 #undef __FUNCT__ 4561 #define __FUNCT__ "MatInterpolate" 4562 /*@ 4563 MatInterpolate - y = A*x or A'*x depending on the shape of 4564 the matrix 4565 4566 Collective on Mat 4567 4568 Input Parameters: 4569 + mat - the matrix 4570 - x,y - the vectors 4571 4572 Level: intermediate 4573 4574 Notes: 4575 This allows one to use either the restriction or interpolation (its transpose) 4576 matrix to do the interpolation 4577 4578 Concepts: matrices^interpolation 4579 4580 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict() 4581 4582 @*/ 4583 int MatInterpolate(Mat A,Vec x,Vec y) 4584 { 4585 int M,N,ierr; 4586 4587 PetscFunctionBegin; 4588 PetscValidType(A); 4589 MatPreallocated(A); 4590 ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr); 4591 if (N > M) { 4592 ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr); 4593 } else { 4594 ierr = MatMult(A,x,y);CHKERRQ(ierr); 4595 } 4596 PetscFunctionReturn(0); 4597 } 4598 4599 #undef __FUNCT__ 4600 #define __FUNCT__ "MatRestrict" 4601 /*@ 4602 MatRestrict - y = A*x or A'*x 4603 4604 Collective on Mat 4605 4606 Input Parameters: 4607 + mat - the matrix 4608 - x,y - the vectors 4609 4610 Level: intermediate 4611 4612 Notes: 4613 This allows one to use either the restriction or interpolation (its transpose) 4614 matrix to do the restriction 4615 4616 Concepts: matrices^restriction 4617 4618 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatInterpolate() 4619 4620 @*/ 4621 int MatRestrict(Mat A,Vec x,Vec y) 4622 { 4623 int M,N,ierr; 4624 4625 PetscFunctionBegin; 4626 PetscValidType(A); 4627 MatPreallocated(A); 4628 ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr); 4629 if (N > M) { 4630 ierr = MatMult(A,x,y);CHKERRQ(ierr); 4631 } else { 4632 ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr); 4633 } 4634 PetscFunctionReturn(0); 4635 } 4636 4637 #undef __FUNCT__ 4638 #define __FUNCT__ "MatNullSpaceAttach" 4639 /*@C 4640 MatNullSpaceAttach - attaches a null space to a matrix. 4641 This null space will be removed from the resulting vector whenever 4642 MatMult() is called 4643 4644 Collective on Mat 4645 4646 Input Parameters: 4647 + mat - the matrix 4648 - nullsp - the null space object 4649 4650 Level: developer 4651 4652 Notes: 4653 Overwrites any previous null space that may have been attached 4654 4655 Concepts: null space^attaching to matrix 4656 4657 .seealso: MatCreate(), MatNullSpaceCreate() 4658 @*/ 4659 int MatNullSpaceAttach(Mat mat,MatNullSpace nullsp) 4660 { 4661 int ierr; 4662 4663 PetscFunctionBegin; 4664 PetscValidHeaderSpecific(mat,MAT_COOKIE); 4665 PetscValidType(mat); 4666 MatPreallocated(mat); 4667 PetscValidHeaderSpecific(nullsp,MAT_NULLSPACE_COOKIE); 4668 4669 if (mat->nullsp) { 4670 ierr = MatNullSpaceDestroy(mat->nullsp);CHKERRQ(ierr); 4671 } 4672 mat->nullsp = nullsp; 4673 ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr); 4674 PetscFunctionReturn(0); 4675 } 4676 4677 #undef __FUNCT__ 4678 #define __FUNCT__ "MatICCFactor" 4679 /*@ 4680 MatICCFactor - Performs in-place incomplete Cholesky factorization of matrix. 4681 4682 Collective on Mat 4683 4684 Input Parameters: 4685 + mat - the matrix 4686 . row - row/column permutation 4687 . fill - expected fill factor >= 1.0 4688 - level - level of fill, for ICC(k) 4689 4690 Notes: 4691 Probably really in-place only when level of fill is zero, otherwise allocates 4692 new space to store factored matrix and deletes previous memory. 4693 4694 Most users should employ the simplified SLES interface for linear solvers 4695 instead of working directly with matrix algebra routines such as this. 4696 See, e.g., SLESCreate(). 4697 4698 Level: developer 4699 4700 Concepts: matrices^incomplete Cholesky factorization 4701 Concepts: Cholesky factorization 4702 4703 .seealso: MatICCFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor() 4704 @*/ 4705 int MatICCFactor(Mat mat,IS row,PetscReal fill,int level) 4706 { 4707 int ierr; 4708 4709 PetscFunctionBegin; 4710 PetscValidHeaderSpecific(mat,MAT_COOKIE); 4711 PetscValidType(mat); 4712 MatPreallocated(mat); 4713 if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,"matrix must be square"); 4714 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4715 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4716 if (!mat->ops->iccfactor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 4717 ierr = (*mat->ops->iccfactor)(mat,row,fill,level);CHKERRQ(ierr); 4718 PetscFunctionReturn(0); 4719 } 4720 4721 #undef __FUNCT__ 4722 #define __FUNCT__ "MatSetValuesAdic" 4723 /*@ 4724 MatSetValuesAdic - Sets values computed with ADIC automatic differentiation into a matrix. 4725 4726 Not Collective 4727 4728 Input Parameters: 4729 + mat - the matrix 4730 - v - the values compute with ADIC 4731 4732 Level: developer 4733 4734 Notes: 4735 Must call MatSetColoring() before using this routine. Also this matrix must already 4736 have its nonzero pattern determined. 4737 4738 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(), 4739 MatSetValues(), MatSetColoring(), MatSetValuesAdifor() 4740 @*/ 4741 int MatSetValuesAdic(Mat mat,void *v) 4742 { 4743 int ierr; 4744 4745 PetscFunctionBegin; 4746 PetscValidHeaderSpecific(mat,MAT_COOKIE); 4747 PetscValidType(mat); 4748 4749 if (!mat->assembled) { 4750 SETERRQ(1,"Matrix must be already assembled"); 4751 } 4752 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 4753 if (!mat->ops->setvaluesadic) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 4754 ierr = (*mat->ops->setvaluesadic)(mat,v);CHKERRQ(ierr); 4755 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 4756 ierr = MatView_Private(mat);CHKERRQ(ierr); 4757 PetscFunctionReturn(0); 4758 } 4759 4760 4761 #undef __FUNCT__ 4762 #define __FUNCT__ "MatSetColoring" 4763 /*@ 4764 MatSetColoring - Sets a coloring used by calls to MatSetValuesAdic() 4765 4766 Not Collective 4767 4768 Input Parameters: 4769 + mat - the matrix 4770 - coloring - the coloring 4771 4772 Level: developer 4773 4774 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(), 4775 MatSetValues(), MatSetValuesAdic() 4776 @*/ 4777 int MatSetColoring(Mat mat,ISColoring coloring) 4778 { 4779 int ierr; 4780 4781 PetscFunctionBegin; 4782 PetscValidHeaderSpecific(mat,MAT_COOKIE); 4783 PetscValidType(mat); 4784 4785 if (!mat->assembled) { 4786 SETERRQ(1,"Matrix must be already assembled"); 4787 } 4788 if (!mat->ops->setcoloring) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 4789 ierr = (*mat->ops->setcoloring)(mat,coloring);CHKERRQ(ierr); 4790 PetscFunctionReturn(0); 4791 } 4792 4793 #undef __FUNCT__ 4794 #define __FUNCT__ "MatSetValuesAdifor" 4795 /*@ 4796 MatSetValuesAdifor - Sets values computed with automatic differentiation into a matrix. 4797 4798 Not Collective 4799 4800 Input Parameters: 4801 + mat - the matrix 4802 . nl - leading dimension of v 4803 - v - the values compute with ADIFOR 4804 4805 Level: developer 4806 4807 Notes: 4808 Must call MatSetColoring() before using this routine. Also this matrix must already 4809 have its nonzero pattern determined. 4810 4811 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(), 4812 MatSetValues(), MatSetColoring() 4813 @*/ 4814 int MatSetValuesAdifor(Mat mat,int nl,void *v) 4815 { 4816 int ierr; 4817 4818 PetscFunctionBegin; 4819 PetscValidHeaderSpecific(mat,MAT_COOKIE); 4820 PetscValidType(mat); 4821 4822 if (!mat->assembled) { 4823 SETERRQ(1,"Matrix must be already assembled"); 4824 } 4825 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 4826 if (!mat->ops->setvaluesadifor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 4827 ierr = (*mat->ops->setvaluesadifor)(mat,nl,v);CHKERRQ(ierr); 4828 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 4829 PetscFunctionReturn(0); 4830 } 4831