1 2 #ifndef lint 3 static char vcid[] = "$Id: matrix.c,v 1.207 1996/11/19 16:30:42 bsmith Exp curfman $"; 4 #endif 5 6 /* 7 This is where the abstract matrix operations are defined 8 */ 9 10 #include "petsc.h" 11 #include "src/mat/matimpl.h" /*I "mat.h" I*/ 12 #include "src/vec/vecimpl.h" 13 #include "pinclude/pviewer.h" 14 #include "draw.h" 15 16 17 /*@C 18 MatGetRow - Gets a row of a matrix. You MUST call MatRestoreRow() 19 for each row that you get to ensure that your application does 20 not bleed memory. 21 22 Input Parameters: 23 . mat - the matrix 24 . row - the row to get 25 26 Output Parameters: 27 . ncols - the number of nonzeros in the row 28 . cols - if nonzero, the column numbers 29 . vals - if nonzero, the values 30 31 Notes: 32 This routine is provided for people who need to have direct access 33 to the structure of a matrix. We hope that we provide enough 34 high-level matrix routines that few users will need it. 35 36 For better efficiency, set cols and/or vals to PETSC_NULL if you do 37 not wish to extract these quantities. 38 39 The user can only examine the values extracted with MatGetRow(); 40 the values cannot be altered. To change the matrix entries, one 41 must use MatSetValues(). 42 43 Caution: 44 Do not try to change the contents of the output arrays (cols and vals). 45 In some cases, this may corrupt the matrix. 46 47 .keywords: matrix, row, get, extract 48 49 .seealso: MatRestoreRow(), MatSetValues() 50 @*/ 51 int MatGetRow(Mat mat,int row,int *ncols,int **cols,Scalar **vals) 52 { 53 int ierr; 54 PetscValidHeaderSpecific(mat,MAT_COOKIE); 55 PetscValidIntPointer(ncols); 56 if (!mat->assembled) SETERRQ(1,"MatGetRow:Not for unassembled matrix"); 57 if (mat->factor) SETERRQ(1,"MatGetRow:Not for factored matrix"); 58 if (!mat->ops.getrow) SETERRQ(PETSC_ERR_SUP,"MatGetRow"); 59 PLogEventBegin(MAT_GetRow,mat,0,0,0); 60 ierr = (*mat->ops.getrow)(mat,row,ncols,cols,vals); CHKERRQ(ierr); 61 PLogEventEnd(MAT_GetRow,mat,0,0,0); 62 return 0; 63 } 64 65 /*@C 66 MatRestoreRow - Frees any temporary space allocated by MatGetRow(). 67 68 Input Parameters: 69 . mat - the matrix 70 . row - the row to get 71 . ncols, cols - the number of nonzeros and their columns 72 . vals - if nonzero the column values 73 74 .keywords: matrix, row, restore 75 76 .seealso: MatGetRow() 77 @*/ 78 int MatRestoreRow(Mat mat,int row,int *ncols,int **cols,Scalar **vals) 79 { 80 PetscValidHeaderSpecific(mat,MAT_COOKIE); 81 PetscValidIntPointer(ncols); 82 if (!mat->assembled) SETERRQ(1,"MatRestoreRow:Not for unassembled matrix"); 83 if (!mat->ops.restorerow) return 0; 84 return (*mat->ops.restorerow)(mat,row,ncols,cols,vals); 85 } 86 /*@ 87 MatView - Visualizes a matrix object. 88 89 Input Parameters: 90 . mat - the matrix 91 . ptr - visualization context 92 93 Notes: 94 The available visualization contexts include 95 $ VIEWER_STDOUT_SELF - standard output (default) 96 $ VIEWER_STDOUT_WORLD - synchronized standard 97 $ output where only the first processor opens 98 $ the file. All other processors send their 99 $ data to the first processor to print. 100 101 The user can open alternative vistualization contexts with 102 $ ViewerFileOpenASCII() - output to a specified file 103 $ ViewerFileOpenBinary() - output in binary to a 104 $ specified file; corresponding input uses MatLoad() 105 $ ViewerDrawOpenX() - output nonzero matrix structure to 106 $ an X window display 107 $ ViewerMatlabOpen() - output matrix to Matlab viewer. 108 $ Currently only the sequential dense and AIJ 109 $ matrix types support the Matlab viewer. 110 111 The user can call ViewerSetFormat() to specify the output 112 format of ASCII printed objects (when using VIEWER_STDOUT_SELF, 113 VIEWER_STDOUT_WORLD and ViewerFileOpenASCII). Available formats include 114 $ VIEWER_FORMAT_ASCII_DEFAULT - default, prints matrix contents 115 $ VIEWER_FORMAT_ASCII_MATLAB - Matlab format 116 $ VIEWER_FORMAT_ASCII_IMPL - implementation-specific format 117 $ (which is in many cases the same as the default) 118 $ VIEWER_FORMAT_ASCII_INFO - basic information about the matrix 119 $ size and structure (not the matrix entries) 120 $ VIEWER_FORMAT_ASCII_INFO_LONG - more detailed information about the 121 $ matrix structure 122 123 .keywords: matrix, view, visualize, output, print, write, draw 124 125 .seealso: ViewerSetFormat(), ViewerFileOpenASCII(), ViewerDrawOpenX(), 126 ViewerMatlabOpen(), ViewerFileOpenBinary(), MatLoad() 127 @*/ 128 int MatView(Mat mat,Viewer viewer) 129 { 130 int format, ierr, rows, cols; 131 FILE *fd; 132 char *cstr; 133 ViewerType vtype; 134 MPI_Comm comm = mat->comm; 135 136 PetscValidHeaderSpecific(mat,MAT_COOKIE); 137 if (viewer) PetscValidHeaderSpecific(viewer,VIEWER_COOKIE); 138 if (!mat->assembled) SETERRQ(1,"MatView:Not for unassembled matrix"); 139 140 if (!viewer) { 141 viewer = VIEWER_STDOUT_SELF; 142 } 143 144 ierr = ViewerGetType(viewer,&vtype); 145 if (vtype == ASCII_FILE_VIEWER || vtype == ASCII_FILES_VIEWER) { 146 ierr = ViewerGetFormat(viewer,&format); CHKERRQ(ierr); 147 ierr = ViewerASCIIGetPointer(viewer,&fd); CHKERRQ(ierr); 148 if (format == VIEWER_FORMAT_ASCII_INFO || format == VIEWER_FORMAT_ASCII_INFO_LONG) { 149 PetscFPrintf(comm,fd,"Matrix Object:\n"); 150 ierr = MatGetType(mat,PETSC_NULL,&cstr); CHKERRQ(ierr); 151 ierr = MatGetSize(mat,&rows,&cols); CHKERRQ(ierr); 152 PetscFPrintf(comm,fd," type=%s, rows=%d, cols=%d\n",cstr,rows,cols); 153 if (mat->ops.getinfo) { 154 MatInfo info; 155 ierr = MatGetInfo(mat,MAT_GLOBAL_SUM,&info); CHKERRQ(ierr); 156 PetscFPrintf(comm,fd," total: nonzeros=%d, allocated nonzeros=%d\n", 157 (int)info.nz_used,(int)info.nz_allocated); 158 } 159 } 160 } 161 if (mat->view) {ierr = (*mat->view)((PetscObject)mat,viewer); CHKERRQ(ierr);} 162 return 0; 163 } 164 165 /*@C 166 MatDestroy - Frees space taken by a matrix. 167 168 Input Parameter: 169 . mat - the matrix 170 171 .keywords: matrix, destroy 172 @*/ 173 int MatDestroy(Mat mat) 174 { 175 int ierr; 176 PetscValidHeaderSpecific(mat,MAT_COOKIE); 177 ierr = (*mat->destroy)((PetscObject)mat); CHKERRQ(ierr); 178 return 0; 179 } 180 /*@ 181 MatValid - Checks whether a matrix object is valid. 182 183 Input Parameter: 184 . m - the matrix to check 185 186 Output Parameter: 187 flg - flag indicating matrix status, either 188 $ PETSC_TRUE if matrix is valid; 189 $ PETSC_FALSE otherwise. 190 191 .keywords: matrix, valid 192 @*/ 193 int MatValid(Mat m,PetscTruth *flg) 194 { 195 PetscValidIntPointer(flg); 196 if (!m) *flg = PETSC_FALSE; 197 else if (m->cookie != MAT_COOKIE) *flg = PETSC_FALSE; 198 else *flg = PETSC_TRUE; 199 return 0; 200 } 201 202 /*@ 203 MatSetValues - Inserts or adds a block of values into a matrix. 204 These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd() 205 MUST be called after all calls to MatSetValues() have been completed. 206 207 Input Parameters: 208 . mat - the matrix 209 . v - a logically two-dimensional array of values 210 . m, indexm - the number of rows and their global indices 211 . n, indexn - the number of columns and their global indices 212 . addv - either ADD_VALUES or INSERT_VALUES, where 213 $ ADD_VALUES - adds values to any existing entries 214 $ INSERT_VALUES - replaces existing entries with new values 215 216 Notes: 217 By default the values, v, are row-oriented and unsorted. 218 See MatSetOptions() for other options. 219 220 Calls to MatSetValues() with the INSERT_VALUES and ADD_VALUES 221 options cannot be mixed without intervening calls to the assembly 222 routines. 223 224 MatSetValues() uses 0-based row and column numbers in Fortran 225 as well as in C. 226 227 .keywords: matrix, insert, add, set, values 228 229 .seealso: MatSetOptions(), MatAssemblyBegin(), MatAssemblyEnd() 230 @*/ 231 int MatSetValues(Mat mat,int m,int *idxm,int n,int *idxn,Scalar *v,InsertMode addv) 232 { 233 int ierr; 234 PetscValidHeaderSpecific(mat,MAT_COOKIE); 235 if (!m || !n) return 0; /* no values to insert */ 236 PetscValidIntPointer(idxm); 237 PetscValidIntPointer(idxn); 238 PetscValidScalarPointer(v); 239 if (mat->factor) SETERRQ(1,"MatSetValues:Not for factored matrix"); 240 241 if (mat->assembled) { 242 mat->was_assembled = PETSC_TRUE; 243 mat->assembled = PETSC_FALSE; 244 } 245 PLogEventBegin(MAT_SetValues,mat,0,0,0); 246 ierr = (*mat->ops.setvalues)(mat,m,idxm,n,idxn,v,addv);CHKERRQ(ierr); 247 PLogEventEnd(MAT_SetValues,mat,0,0,0); 248 return 0; 249 } 250 251 /*@ 252 MatGetValues - Gets a block of values from a matrix. 253 254 Input Parameters: 255 . mat - the matrix 256 . v - a logically two-dimensional array for storing the values 257 . m, indexm - the number of rows and their global indices 258 . n, indexn - the number of columns and their global indices 259 260 Notes: 261 The user must allocate space (m*n Scalars) for the values, v. 262 The values, v, are then returned in a row-oriented format, 263 analogous to that used by default in MatSetValues(). 264 265 MatGetValues() uses 0-based row and column numbers in 266 Fortran as well as in C. 267 268 .keywords: matrix, get, values 269 270 .seealso: MatGetRow(), MatGetSubmatrices(), MatSetValues() 271 @*/ 272 int MatGetValues(Mat mat,int m,int *idxm,int n,int *idxn,Scalar *v) 273 { 274 int ierr; 275 276 PetscValidHeaderSpecific(mat,MAT_COOKIE); 277 PetscValidIntPointer(idxm); 278 PetscValidIntPointer(idxn); 279 PetscValidScalarPointer(v); 280 if (!mat->assembled) SETERRQ(1,"MatGetValues:Not for unassembled matrix"); 281 if (mat->factor) SETERRQ(1,"MatGetValues:Not for factored matrix"); 282 if (!mat->ops.getvalues) SETERRQ(PETSC_ERR_SUP,"MatGetValues"); 283 284 PLogEventBegin(MAT_GetValues,mat,0,0,0); 285 ierr = (*mat->ops.getvalues)(mat,m,idxm,n,idxn,v); CHKERRQ(ierr); 286 PLogEventEnd(MAT_GetValues,mat,0,0,0); 287 return 0; 288 } 289 290 /*@ 291 MatSetLocalToGlobalMapping - Sets a local numbering to global numbering used 292 by the routine MatSetValuesLocal() to allow users to insert matrices entries 293 using a local (per-processor) numbering. 294 295 Input Parameters: 296 . x - the matrix 297 . n - number of local indices 298 . indices - global index for each local index 299 300 .keywords: matrix, set, values, local ordering 301 302 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesLocal() 303 @*/ 304 int MatSetLocalToGlobalMapping(Mat x, int n,int *indices) 305 { 306 int ierr; 307 PetscValidHeaderSpecific(x,MAT_COOKIE); 308 PetscValidIntPointer(indices); 309 310 if (x->mapping) { 311 SETERRQ(1,"MatSetLocalToGlobalMapping:Mapping already set for matrix"); 312 } 313 314 ierr = ISLocalToGlobalMappingCreate(n,indices,&x->mapping);CHKERRQ(ierr); 315 return 0; 316 } 317 318 /*@ 319 MatSetValuesLocal - Inserts or adds values into certain locations of a matrix, 320 using a local ordering of the nodes. 321 322 Input Parameters: 323 . x - matrix to insert in 324 . nrow - number of row elements to add 325 . irow - row indices where to add 326 . ncol - number of column elements to add 327 . icol - column indices where to add 328 . y - array of values 329 . iora - either INSERT_VALUES or ADD_VALUES 330 331 Notes: 332 Calls to MatSetValuesLocal() with the INSERT_VALUES and ADD_VALUES 333 options cannot be mixed without intervening calls to the assembly 334 routines. 335 These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd() 336 MUST be called after all calls to MatSetValuesLocal() have been completed. 337 338 .keywords: matrix, set, values, local ordering 339 340 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetLocalToGlobalMapping() 341 @*/ 342 int MatSetValuesLocal(Mat x,int nrow,int *irow,int ncol, int *icol,Scalar *y,InsertMode iora) 343 { 344 int ierr,irowm[128],icolm[128]; 345 346 PetscValidHeaderSpecific(x,MAT_COOKIE); 347 PetscValidIntPointer(irow); 348 PetscValidIntPointer(icol); 349 PetscValidScalarPointer(y); 350 if (!x->mapping) { 351 SETERRQ(1,"MatSetValuesLocal:Local to global never set with MatSetLocalToGlobalMapping"); 352 } 353 if (nrow > 128 || ncol > 128) { 354 SETERRQ(1,"MatSetValuesLocal:Number indices must be <= 128"); 355 } 356 if (x->factor) SETERRQ(1,"MatSetValues:Not for factored matrix"); 357 358 if (x->assembled) { 359 x->was_assembled = PETSC_TRUE; 360 x->assembled = PETSC_FALSE; 361 } 362 PLogEventBegin(MAT_SetValues,x,0,0,0); 363 ISLocalToGlobalMappingApply(x->mapping,nrow,irow,irowm); 364 ISLocalToGlobalMappingApply(x->mapping,ncol,icol,icolm); 365 ierr = (*x->ops.setvalues)(x,nrow,irowm,ncol,icolm,y,iora);CHKERRQ(ierr); 366 PLogEventEnd(MAT_SetValues,x,0,0,0); 367 return 0; 368 } 369 370 /* --------------------------------------------------------*/ 371 /*@ 372 MatMult - Computes the matrix-vector product, y = Ax. 373 374 Input Parameters: 375 . mat - the matrix 376 . x - the vector to be multilplied 377 378 Output Parameters: 379 . y - the result 380 381 Notes: 382 The vectors x and y cannot be the same. I.e., one cannot 383 call MatMult(A,y,y). 384 385 .keywords: matrix, multiply, matrix-vector product 386 387 .seealso: MatMultTrans(), MatMultAdd(), MatMultTransAdd() 388 @*/ 389 int MatMult(Mat mat,Vec x,Vec y) 390 { 391 int ierr; 392 PetscValidHeaderSpecific(mat,MAT_COOKIE); 393 PetscValidHeaderSpecific(x,VEC_COOKIE);PetscValidHeaderSpecific(y,VEC_COOKIE); 394 if (!mat->assembled) SETERRQ(1,"MatMult:Not for unassembled matrix"); 395 if (mat->factor) SETERRQ(1,"MatMult:Not for factored matrix"); 396 if (x == y) SETERRQ(1,"MatMult:x and y must be different vectors"); 397 if (mat->N != x->N) SETERRQ(PETSC_ERR_SIZ,"MatMult:Mat mat,Vec x: global dim"); 398 if (mat->M != y->N) SETERRQ(PETSC_ERR_SIZ,"MatMult:Mat mat,Vec y: global dim"); 399 if (mat->m != y->n) SETERRQ(PETSC_ERR_SIZ,"MatMult:Mat mat,Vec y: local dim"); 400 401 PLogEventBegin(MAT_Mult,mat,x,y,0); 402 ierr = (*mat->ops.mult)(mat,x,y); CHKERRQ(ierr); 403 PLogEventEnd(MAT_Mult,mat,x,y,0); 404 405 return 0; 406 } 407 /*@ 408 MatMultTrans - Computes matrix transpose times a vector. 409 410 Input Parameters: 411 . mat - the matrix 412 . x - the vector to be multilplied 413 414 Output Parameters: 415 . y - the result 416 417 Notes: 418 The vectors x and y cannot be the same. I.e., one cannot 419 call MatMultTrans(A,y,y). 420 421 .keywords: matrix, multiply, matrix-vector product, transpose 422 423 .seealso: MatMult(), MatMultAdd(), MatMultTransAdd() 424 @*/ 425 int MatMultTrans(Mat mat,Vec x,Vec y) 426 { 427 int ierr; 428 PetscValidHeaderSpecific(mat,MAT_COOKIE); 429 PetscValidHeaderSpecific(x,VEC_COOKIE); PetscValidHeaderSpecific(y,VEC_COOKIE); 430 if (!mat->assembled) SETERRQ(1,"MatMultTrans:Not for unassembled matrix"); 431 if (mat->factor) SETERRQ(1,"MatMult:Not for factored matrix"); 432 if (x == y) SETERRQ(1,"MatMultTrans:x and y must be different vectors"); 433 if (mat->M != x->N) SETERRQ(PETSC_ERR_SIZ,"MatMultTrans:Mat mat,Vec x: global dim"); 434 if (mat->N != y->N) SETERRQ(PETSC_ERR_SIZ,"MatMultTrans:Mat mat,Vec y: global dim"); 435 PLogEventBegin(MAT_MultTrans,mat,x,y,0); 436 ierr = (*mat->ops.multtrans)(mat,x,y); CHKERRQ(ierr); 437 PLogEventEnd(MAT_MultTrans,mat,x,y,0); 438 return 0; 439 } 440 /*@ 441 MatMultAdd - Computes v3 = v2 + A * v1. 442 443 Input Parameters: 444 . mat - the matrix 445 . v1, v2 - the vectors 446 447 Output Parameters: 448 . v3 - the result 449 450 Notes: 451 The vectors v1 and v3 cannot be the same. I.e., one cannot 452 call MatMultAdd(A,v1,v2,v1). 453 454 .keywords: matrix, multiply, matrix-vector product, add 455 456 .seealso: MatMultTrans(), MatMult(), MatMultTransAdd() 457 @*/ 458 int MatMultAdd(Mat mat,Vec v1,Vec v2,Vec v3) 459 { 460 int ierr; 461 PetscValidHeaderSpecific(mat,MAT_COOKIE);PetscValidHeaderSpecific(v1,VEC_COOKIE); 462 PetscValidHeaderSpecific(v2,VEC_COOKIE); PetscValidHeaderSpecific(v3,VEC_COOKIE); 463 if (!mat->assembled) SETERRQ(1,"MatMultAdd:Not for unassembled matrix"); 464 if (mat->factor) SETERRQ(1,"MatMult:Not for factored matrix"); 465 if (mat->N != v1->N) SETERRQ(PETSC_ERR_SIZ,"MatMultAdd:Mat mat,Vec v1: global dim"); 466 if (mat->M != v2->N) SETERRQ(PETSC_ERR_SIZ,"MatMultAdd:Mat mat,Vec v2: global dim"); 467 if (mat->M != v3->N) SETERRQ(PETSC_ERR_SIZ,"MatMultAdd:Mat mat,Vec v3: global dim"); 468 if (mat->m != v3->n) SETERRQ(PETSC_ERR_SIZ,"MatMultAdd:Mat mat,Vec v3: local dim"); 469 if (mat->m != v2->n) SETERRQ(PETSC_ERR_SIZ,"MatMultAdd:Mat mat,Vec v2: local dim"); 470 if (v1 == v3) SETERRQ(1,"MatMultAdd:v1 and v3 must be different vectors"); 471 472 PLogEventBegin(MAT_MultAdd,mat,v1,v2,v3); 473 ierr = (*mat->ops.multadd)(mat,v1,v2,v3); CHKERRQ(ierr); 474 PLogEventEnd(MAT_MultAdd,mat,v1,v2,v3); 475 return 0; 476 } 477 /*@ 478 MatMultTransAdd - Computes v3 = v2 + A' * v1. 479 480 Input Parameters: 481 . mat - the matrix 482 . v1, v2 - the vectors 483 484 Output Parameters: 485 . v3 - the result 486 487 Notes: 488 The vectors v1 and v3 cannot be the same. I.e., one cannot 489 call MatMultTransAdd(A,v1,v2,v1). 490 491 .keywords: matrix, multiply, matrix-vector product, transpose, add 492 493 .seealso: MatMultTrans(), MatMultAdd(), MatMult() 494 @*/ 495 int MatMultTransAdd(Mat mat,Vec v1,Vec v2,Vec v3) 496 { 497 int ierr; 498 PetscValidHeaderSpecific(mat,MAT_COOKIE);PetscValidHeaderSpecific(v1,VEC_COOKIE); 499 PetscValidHeaderSpecific(v2,VEC_COOKIE);PetscValidHeaderSpecific(v3,VEC_COOKIE); 500 if (!mat->assembled) SETERRQ(1,"MatMultTransAdd:Not for unassembled matrix"); 501 if (mat->factor) SETERRQ(1,"MatMult:Not for factored matrix"); 502 if (!mat->ops.multtransadd) SETERRQ(PETSC_ERR_SUP,"MatMultTransAdd"); 503 if (v1 == v3) SETERRQ(1,"MatMultTransAdd:v1 and v3 must be different vectors"); 504 if (mat->M != v1->N) SETERRQ(PETSC_ERR_SIZ,"MatMultTransAdd:Mat mat,Vec v1: global dim"); 505 if (mat->N != v2->N) SETERRQ(PETSC_ERR_SIZ,"MatMultTransAdd:Mat mat,Vec v2: global dim"); 506 if (mat->N != v3->N) SETERRQ(PETSC_ERR_SIZ,"MatMultTransAdd:Mat mat,Vec v3: global dim"); 507 508 PLogEventBegin(MAT_MultTransAdd,mat,v1,v2,v3); 509 ierr = (*mat->ops.multtransadd)(mat,v1,v2,v3); CHKERRQ(ierr); 510 PLogEventEnd(MAT_MultTransAdd,mat,v1,v2,v3); 511 return 0; 512 } 513 /* ------------------------------------------------------------*/ 514 /*@C 515 MatGetInfo - Returns information about matrix storage (number of 516 nonzeros, memory, etc.). 517 518 Input Parameters: 519 . mat - the matrix 520 521 Output Parameters: 522 . flag - flag indicating the type of parameters to be returned 523 $ flag = MAT_LOCAL: local matrix 524 $ flag = MAT_GLOBAL_MAX: maximum over all processors 525 $ flag = MAT_GLOBAL_SUM: sum over all processors 526 . info - matrix information context 527 528 Notes: 529 The MatInfo context contains a variety of matrix data, including 530 number of nonzeros allocated and used, number of mallocs during 531 matrix assembly, etc. Additional information for factored matrices 532 is provided (such as the fill ratio, number of mallocs during 533 factorization, etc.). Much of this info is printed to STDOUT 534 when using the runtime options 535 $ -log_info -mat_view_info 536 537 Example for C/C++ Users: 538 See the file $(PETSC_DIR)/include/mat.h for a complete list of 539 data within the MatInfo context. For example, 540 $ 541 $ MatInfo *info; 542 $ Mat A; 543 $ double mal, nz_a, nz_u; 544 $ 545 $ MatGetInfo(A,MAT_LOCAL,&info); 546 $ mal = info->mallocs; 547 $ nz_a = info->nz_allocated; 548 $ 549 550 Example for Fortran Users: 551 Fortran users should declare info as a double precision 552 array of dimension MAT_INFO_SIZE, and then extract the parameters 553 of interest. See the file $(PETSC_DIR)/include/FINCLUDE/mat.h 554 a complete list of parameter names. 555 $ 556 $ double precision info(MAT_INFO_SIZE) 557 $ double precision mal, nz_a 558 $ Mat A 559 $ integer ierr 560 $ 561 $ call MatGetInfo(A,MAT_LOCAL,info,ierr) 562 $ mal = info(MAT_INFO_MALLOCS) 563 $ nz_a = info(MAT_INFO_NZ_ALLOCATED) 564 $ 565 566 .keywords: matrix, get, info, storage, nonzeros, memory, fill 567 @*/ 568 int MatGetInfo(Mat mat,MatInfoType flag,MatInfo *info) 569 { 570 PetscValidHeaderSpecific(mat,MAT_COOKIE); 571 PetscValidPointer(info); 572 if (!mat->ops.getinfo) SETERRQ(PETSC_ERR_SUP,"MatGetInfo"); 573 return (*mat->ops.getinfo)(mat,flag,info); 574 } 575 /* ----------------------------------------------------------*/ 576 /*@ 577 MatILUDTFactor - Performs a drop tolerance ILU factorization. 578 579 Input Parameters: 580 . mat - the matrix 581 . dt - the drop tolerance 582 . maxnz - the maximum number of nonzeros per row allowed? 583 . row - row permutation 584 . col - column permutation 585 586 Output Parameters: 587 . fact - the factored matrix 588 589 .keywords: matrix, factor, LU, in-place 590 591 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor() 592 @*/ 593 int MatILUDTFactor(Mat mat,double dt,int maxnz,IS row,IS col,Mat *fact) 594 { 595 int ierr; 596 PetscValidHeaderSpecific(mat,MAT_COOKIE); 597 PetscValidPointer(fact); 598 if (!mat->assembled) SETERRQ(1,"MatILUDTFactor:Not for unassembled matrix"); 599 if (mat->factor) SETERRQ(1,"MatILUDTFactor:Not for factored matrix"); 600 if (!mat->ops.iludtfactor) SETERRQ(PETSC_ERR_SUP,"MatILUDTFactor"); 601 602 PLogEventBegin(MAT_ILUFactor,mat,row,col,0); 603 ierr = (*mat->ops.iludtfactor)(mat,dt,maxnz,row,col,fact); CHKERRQ(ierr); 604 PLogEventEnd(MAT_ILUFactor,mat,row,col,0); 605 606 return 0; 607 } 608 609 /*@ 610 MatLUFactor - Performs in-place LU factorization of matrix. 611 612 Input Parameters: 613 . mat - the matrix 614 . row - row permutation 615 . col - column permutation 616 . f - expected fill as ratio of original fill. 617 618 .keywords: matrix, factor, LU, in-place 619 620 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor() 621 @*/ 622 int MatLUFactor(Mat mat,IS row,IS col,double f) 623 { 624 int ierr; 625 PetscValidHeaderSpecific(mat,MAT_COOKIE); 626 if (mat->M != mat->N) SETERRQ(1,"MatLUFactor:matrix must be square"); 627 if (!mat->assembled) SETERRQ(1,"MatLUFactor:Not for unassembled matrix"); 628 if (mat->factor) SETERRQ(1,"MatLUFactor:Not for factored matrix"); 629 if (!mat->ops.lufactor) SETERRQ(PETSC_ERR_SUP,"MatLUFactor"); 630 631 PLogEventBegin(MAT_LUFactor,mat,row,col,0); 632 ierr = (*mat->ops.lufactor)(mat,row,col,f); CHKERRQ(ierr); 633 PLogEventEnd(MAT_LUFactor,mat,row,col,0); 634 return 0; 635 } 636 /*@ 637 MatILUFactor - Performs in-place ILU factorization of matrix. 638 639 Input Parameters: 640 . mat - the matrix 641 . row - row permutation 642 . col - column permutation 643 . f - expected fill as ratio of original fill. 644 . level - number of levels of fill. 645 646 Note: probably really only in-place when level is zero. 647 .keywords: matrix, factor, ILU, in-place 648 649 .seealso: MatILUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor() 650 @*/ 651 int MatILUFactor(Mat mat,IS row,IS col,double f,int level) 652 { 653 int ierr; 654 PetscValidHeaderSpecific(mat,MAT_COOKIE); 655 if (mat->M != mat->N) SETERRQ(1,"MatILUFactor:matrix must be square"); 656 if (!mat->assembled) SETERRQ(1,"MatILUFactor:Not for unassembled matrix"); 657 if (mat->factor) SETERRQ(1,"MatILUFactor:Not for factored matrix"); 658 if (!mat->ops.ilufactor) SETERRQ(PETSC_ERR_SUP,"MatILUFactor"); 659 660 PLogEventBegin(MAT_ILUFactor,mat,row,col,0); 661 ierr = (*mat->ops.ilufactor)(mat,row,col,f,level); CHKERRQ(ierr); 662 PLogEventEnd(MAT_ILUFactor,mat,row,col,0); 663 return 0; 664 } 665 666 /*@ 667 MatLUFactorSymbolic - Performs symbolic LU factorization of matrix. 668 Call this routine before calling MatLUFactorNumeric(). 669 670 Input Parameters: 671 . mat - the matrix 672 . row, col - row and column permutations 673 . f - expected fill as ratio of the original number of nonzeros, 674 for example 3.0; choosing this parameter well can result in 675 more efficient use of time and space. 676 677 Output Parameter: 678 . fact - new matrix that has been symbolically factored 679 680 Options Database Key: 681 $ -mat_lu_fill <f>, where f is the fill ratio 682 683 Notes: 684 See the file $(PETSC_DIR)/Performace for additional information about 685 choosing the fill factor for better efficiency. 686 687 .keywords: matrix, factor, LU, symbolic, fill 688 689 .seealso: MatLUFactor(), MatLUFactorNumeric(), MatCholeskyFactor() 690 @*/ 691 int MatLUFactorSymbolic(Mat mat,IS row,IS col,double f,Mat *fact) 692 { 693 int ierr,flg; 694 PetscValidHeaderSpecific(mat,MAT_COOKIE); 695 if (mat->M != mat->N) SETERRQ(1,"MatLUFactorSymbolic:matrix must be square"); 696 if (!fact) SETERRQ(1,"MatLUFactorSymbolic:Missing factor matrix argument"); 697 if (!mat->assembled) SETERRQ(1,"MatLUFactorSymbolic:Not for unassembled matrix"); 698 if (mat->factor) SETERRQ(1,"MatLUFactorSymbolic:Not for factored matrix"); 699 if (!mat->ops.lufactorsymbolic) SETERRQ(PETSC_ERR_SUP,"MatLUFactorSymbolic"); 700 701 ierr = OptionsGetDouble(PETSC_NULL,"-mat_lu_fill",&f,&flg); CHKERRQ(ierr); 702 PLogEventBegin(MAT_LUFactorSymbolic,mat,row,col,0); 703 ierr = (*mat->ops.lufactorsymbolic)(mat,row,col,f,fact); CHKERRQ(ierr); 704 PLogEventEnd(MAT_LUFactorSymbolic,mat,row,col,0); 705 return 0; 706 } 707 /*@ 708 MatLUFactorNumeric - Performs numeric LU factorization of a matrix. 709 Call this routine after first calling MatLUFactorSymbolic(). 710 711 Input Parameters: 712 . mat - the matrix 713 . row, col - row and column permutations 714 715 Output Parameters: 716 . fact - symbolically factored matrix that must have been generated 717 by MatLUFactorSymbolic() 718 719 Notes: 720 See MatLUFactor() for in-place factorization. See 721 MatCholeskyFactorNumeric() for the symmetric, positive definite case. 722 723 .keywords: matrix, factor, LU, numeric 724 725 .seealso: MatLUFactorSymbolic(), MatLUFactor(), MatCholeskyFactor() 726 @*/ 727 int MatLUFactorNumeric(Mat mat,Mat *fact) 728 { 729 int ierr,flg; 730 731 PetscValidHeaderSpecific(mat,MAT_COOKIE); 732 if (!fact) SETERRQ(1,"MatLUFactorNumeric:Missing factor matrix argument"); 733 PetscValidHeaderSpecific(*fact,MAT_COOKIE); 734 if (!mat->assembled) SETERRQ(1,"MatLUFactorNumeric:Not for unassembled matrix"); 735 if (mat->M != (*fact)->M || mat->N != (*fact)->N) 736 SETERRQ(PETSC_ERR_SIZ,"MatLUFactorNumeric:Mat mat,Mat *fact: global dim"); 737 if (!mat->ops.lufactornumeric) SETERRQ(PETSC_ERR_SUP,"MatLUFactorNumeric"); 738 739 PLogEventBegin(MAT_LUFactorNumeric,mat,*fact,0,0); 740 ierr = (*mat->ops.lufactornumeric)(mat,fact); CHKERRQ(ierr); 741 PLogEventEnd(MAT_LUFactorNumeric,mat,*fact,0,0); 742 ierr = OptionsHasName(PETSC_NULL,"-mat_view_draw",&flg); CHKERRQ(ierr); 743 if (flg) { 744 Viewer viewer; 745 ierr = ViewerDrawOpenX((*fact)->comm,0,0,0,0,300,300,&viewer);CHKERRQ(ierr); 746 ierr = MatView(*fact,viewer); CHKERRQ(ierr); 747 ierr = ViewerFlush(viewer); CHKERRQ(ierr); 748 ierr = ViewerDestroy(viewer); CHKERRQ(ierr); 749 } 750 return 0; 751 } 752 753 /*@ 754 MatCholeskyFactor - Performs in-place Cholesky factorization of a 755 symmetric matrix. 756 757 Input Parameters: 758 . mat - the matrix 759 . perm - row and column permutations 760 . f - expected fill as ratio of original fill 761 762 Notes: 763 See MatLUFactor() for the nonsymmetric case. See also 764 MatCholeskyFactorSymbolic(), and MatCholeskyFactorNumeric(). 765 766 .keywords: matrix, factor, in-place, Cholesky 767 768 .seealso: MatLUFactor(), MatCholeskyFactorSymbolic(), MatCholeskyFactorNumeric() 769 @*/ 770 int MatCholeskyFactor(Mat mat,IS perm,double f) 771 { 772 int ierr; 773 PetscValidHeaderSpecific(mat,MAT_COOKIE); 774 if (mat->M != mat->N) SETERRQ(1,"MatCholeskyFactor:matrix must be square"); 775 if (!mat->assembled) SETERRQ(1,"MatCholeskyFactor:Not for unassembled matrix"); 776 if (mat->factor) SETERRQ(1,"MatCholeskyFactor:Not for factored matrix"); 777 if (!mat->ops.choleskyfactor) SETERRQ(PETSC_ERR_SUP,"MatCholeskyFactor"); 778 779 PLogEventBegin(MAT_CholeskyFactor,mat,perm,0,0); 780 ierr = (*mat->ops.choleskyfactor)(mat,perm,f); CHKERRQ(ierr); 781 PLogEventEnd(MAT_CholeskyFactor,mat,perm,0,0); 782 return 0; 783 } 784 785 /*@ 786 MatCholeskyFactorSymbolic - Performs symbolic Cholesky factorization 787 of a symmetric matrix. 788 789 Input Parameters: 790 . mat - the matrix 791 . perm - row and column permutations 792 . f - expected fill as ratio of original 793 794 Output Parameter: 795 . fact - the factored matrix 796 797 Notes: 798 See MatLUFactorSymbolic() for the nonsymmetric case. See also 799 MatCholeskyFactor() and MatCholeskyFactorNumeric(). 800 801 .keywords: matrix, factor, factorization, symbolic, Cholesky 802 803 .seealso: MatLUFactorSymbolic(), MatCholeskyFactor(), MatCholeskyFactorNumeric() 804 @*/ 805 int MatCholeskyFactorSymbolic(Mat mat,IS perm,double f,Mat *fact) 806 { 807 int ierr; 808 PetscValidHeaderSpecific(mat,MAT_COOKIE); 809 PetscValidPointer(fact); 810 if (mat->M != mat->N) SETERRQ(1,"MatCholeskyFactorSymbolic:matrix must be square"); 811 if (!mat->assembled) SETERRQ(1,"MatCholeskyFactorSymbolic:Not for unassembled matrix"); 812 if (mat->factor) SETERRQ(1,"MatCholeskyFactorSymbolic:Not for factored matrix"); 813 if (!mat->ops.choleskyfactorsymbolic)SETERRQ(PETSC_ERR_SUP,"MatCholeskyFactorSymbolic"); 814 815 PLogEventBegin(MAT_CholeskyFactorSymbolic,mat,perm,0,0); 816 ierr = (*mat->ops.choleskyfactorsymbolic)(mat,perm,f,fact); CHKERRQ(ierr); 817 PLogEventEnd(MAT_CholeskyFactorSymbolic,mat,perm,0,0); 818 return 0; 819 } 820 821 /*@ 822 MatCholeskyFactorNumeric - Performs numeric Cholesky factorization 823 of a symmetric matrix. Call this routine after first calling 824 MatCholeskyFactorSymbolic(). 825 826 Input Parameter: 827 . mat - the initial matrix 828 829 Output Parameter: 830 . fact - the factored matrix 831 832 .keywords: matrix, factor, numeric, Cholesky 833 834 .seealso: MatCholeskyFactorSymbolic(), MatCholeskyFactor(), MatLUFactorNumeric() 835 @*/ 836 int MatCholeskyFactorNumeric(Mat mat,Mat *fact) 837 { 838 int ierr; 839 PetscValidHeaderSpecific(mat,MAT_COOKIE); 840 if (!fact) SETERRQ(1,"MatCholeskyFactorNumeric:Missing factor matrix argument"); 841 if (!mat->ops.choleskyfactornumeric) SETERRQ(PETSC_ERR_SUP,"MatCholeskyFactorNumeric"); 842 if (!mat->assembled) SETERRQ(1,"MatCholeskyFactorNumeric:Not for unassembled matrix"); 843 if (mat->M != (*fact)->M || mat->N != (*fact)->N) 844 SETERRQ(PETSC_ERR_SIZ,"MatCholeskyFactorNumeric:Mat mat,Mat *fact: global dim"); 845 846 PLogEventBegin(MAT_CholeskyFactorNumeric,mat,*fact,0,0); 847 ierr = (*mat->ops.choleskyfactornumeric)(mat,fact); CHKERRQ(ierr); 848 PLogEventEnd(MAT_CholeskyFactorNumeric,mat,*fact,0,0); 849 return 0; 850 } 851 852 /* ----------------------------------------------------------------*/ 853 /*@ 854 MatSolve - Solves A x = b, given a factored matrix. 855 856 Input Parameters: 857 . mat - the factored matrix 858 . b - the right-hand-side vector 859 860 Output Parameter: 861 . x - the result vector 862 863 Notes: 864 The vectors b and x cannot be the same. I.e., one cannot 865 call MatSolve(A,x,x). 866 867 .keywords: matrix, linear system, solve, LU, Cholesky, triangular solve 868 869 .seealso: MatSolveAdd(), MatSolveTrans(), MatSolveTransAdd() 870 @*/ 871 int MatSolve(Mat mat,Vec b,Vec x) 872 { 873 int ierr; 874 PetscValidHeaderSpecific(mat,MAT_COOKIE); 875 PetscValidHeaderSpecific(b,VEC_COOKIE); PetscValidHeaderSpecific(x,VEC_COOKIE); 876 if (x == b) SETERRQ(1,"MatSolve:x and b must be different vectors"); 877 if (!mat->factor) SETERRQ(1,"MatSolve:Unfactored matrix"); 878 if (mat->N != x->N) SETERRQ(PETSC_ERR_SIZ,"MatSolve:Mat mat,Vec x: global dim"); 879 if (mat->M != b->N) SETERRQ(PETSC_ERR_SIZ,"MatSolve:Mat mat,Vec b: global dim"); 880 if (mat->m != b->n) SETERRQ(PETSC_ERR_SIZ,"MatSolve:Mat mat,Vec b: local dim"); 881 882 if (!mat->ops.solve) SETERRQ(PETSC_ERR_SUP,"MatSolve"); 883 PLogEventBegin(MAT_Solve,mat,b,x,0); 884 ierr = (*mat->ops.solve)(mat,b,x); CHKERRQ(ierr); 885 PLogEventEnd(MAT_Solve,mat,b,x,0); 886 return 0; 887 } 888 889 /* @ 890 MatForwardSolve - Solves L x = b, given a factored matrix, A = LU. 891 892 Input Parameters: 893 . mat - the factored matrix 894 . b - the right-hand-side vector 895 896 Output Parameter: 897 . x - the result vector 898 899 Notes: 900 MatSolve() should be used for most applications, as it performs 901 a forward solve followed by a backward solve. 902 903 The vectors b and x cannot be the same. I.e., one cannot 904 call MatForwardSolve(A,x,x). 905 906 .keywords: matrix, forward, LU, Cholesky, triangular solve 907 908 .seealso: MatSolve(), MatBackwardSolve() 909 @ */ 910 int MatForwardSolve(Mat mat,Vec b,Vec x) 911 { 912 int ierr; 913 PetscValidHeaderSpecific(mat,MAT_COOKIE); 914 PetscValidHeaderSpecific(b,VEC_COOKIE); PetscValidHeaderSpecific(x,VEC_COOKIE); 915 if (x == b) SETERRQ(1,"MatForwardSolve:x and b must be different vectors"); 916 if (!mat->factor) SETERRQ(1,"MatForwardSolve:Unfactored matrix"); 917 if (!mat->ops.forwardsolve) SETERRQ(PETSC_ERR_SUP,"MatForwardSolve"); 918 if (mat->N != x->N) SETERRQ(PETSC_ERR_SIZ,"MatForwardSolve:Mat mat,Vec x: global dim"); 919 if (mat->M != b->N) SETERRQ(PETSC_ERR_SIZ,"MatForwardSolve:Mat mat,Vec b: global dim"); 920 if (mat->m != b->n) SETERRQ(PETSC_ERR_SIZ,"MatForwardSolve:Mat mat,Vec b: local dim"); 921 922 PLogEventBegin(MAT_ForwardSolve,mat,b,x,0); 923 ierr = (*mat->ops.forwardsolve)(mat,b,x); CHKERRQ(ierr); 924 PLogEventEnd(MAT_ForwardSolve,mat,b,x,0); 925 return 0; 926 } 927 928 /* @ 929 MatBackwardSolve - Solves U x = b, given a factored matrix, A = LU. 930 931 Input Parameters: 932 . mat - the factored matrix 933 . b - the right-hand-side vector 934 935 Output Parameter: 936 . x - the result vector 937 938 Notes: 939 MatSolve() should be used for most applications, as it performs 940 a forward solve followed by a backward solve. 941 942 The vectors b and x cannot be the same. I.e., one cannot 943 call MatBackwardSolve(A,x,x). 944 945 .keywords: matrix, backward, LU, Cholesky, triangular solve 946 947 .seealso: MatSolve(), MatForwardSolve() 948 @ */ 949 int MatBackwardSolve(Mat mat,Vec b,Vec x) 950 { 951 int ierr; 952 PetscValidHeaderSpecific(mat,MAT_COOKIE); 953 PetscValidHeaderSpecific(b,VEC_COOKIE); PetscValidHeaderSpecific(x,VEC_COOKIE); 954 if (x == b) SETERRQ(1,"MatBackwardSolve:x and b must be different vectors"); 955 if (!mat->factor) SETERRQ(1,"MatBackwardSolve:Unfactored matrix"); 956 if (!mat->ops.backwardsolve) SETERRQ(PETSC_ERR_SUP,"MatBackwardSolve"); 957 if (mat->N != x->N) SETERRQ(PETSC_ERR_SIZ,"MatBackwardSolve:Mat mat,Vec x: global dim"); 958 if (mat->M != b->N) SETERRQ(PETSC_ERR_SIZ,"MatBackwardSolve:Mat mat,Vec b: global dim"); 959 if (mat->m != b->n) SETERRQ(PETSC_ERR_SIZ,"MatBackwardSolve:Mat mat,Vec b: local dim"); 960 961 PLogEventBegin(MAT_BackwardSolve,mat,b,x,0); 962 ierr = (*mat->ops.backwardsolve)(mat,b,x); CHKERRQ(ierr); 963 PLogEventEnd(MAT_BackwardSolve,mat,b,x,0); 964 return 0; 965 } 966 967 /*@ 968 MatSolveAdd - Computes x = y + inv(A)*b, given a factored matrix. 969 970 Input Parameters: 971 . mat - the factored matrix 972 . b - the right-hand-side vector 973 . y - the vector to be added to 974 975 Output Parameter: 976 . x - the result vector 977 978 Notes: 979 The vectors b and x cannot be the same. I.e., one cannot 980 call MatSolveAdd(A,x,y,x). 981 982 .keywords: matrix, linear system, solve, LU, Cholesky, add 983 984 .seealso: MatSolve(), MatSolveTrans(), MatSolveTransAdd() 985 @*/ 986 int MatSolveAdd(Mat mat,Vec b,Vec y,Vec x) 987 { 988 Scalar one = 1.0; 989 Vec tmp; 990 int ierr; 991 PetscValidHeaderSpecific(mat,MAT_COOKIE);PetscValidHeaderSpecific(y,VEC_COOKIE); 992 PetscValidHeaderSpecific(b,VEC_COOKIE); PetscValidHeaderSpecific(x,VEC_COOKIE); 993 if (x == b) SETERRQ(1,"MatSolveAdd:x and b must be different vectors"); 994 if (!mat->factor) SETERRQ(1,"MatSolveAdd:Unfactored matrix"); 995 if (mat->N != x->N) SETERRQ(PETSC_ERR_SIZ,"MatSolveAdd:Mat mat,Vec x: global dim"); 996 if (mat->M != b->N) SETERRQ(PETSC_ERR_SIZ,"MatSolveAdd:Mat mat,Vec b: global dim"); 997 if (mat->M != y->N) SETERRQ(PETSC_ERR_SIZ,"MatSolveAdd:Mat mat,Vec y: global dim"); 998 if (mat->m != b->n) SETERRQ(PETSC_ERR_SIZ,"MatSolveAdd:Mat mat,Vec b: local dim"); 999 if (x->n != y->n) SETERRQ(PETSC_ERR_SIZ,"MatSolveAdd:Vec x,Vec y: local dim"); 1000 1001 PLogEventBegin(MAT_SolveAdd,mat,b,x,y); 1002 if (mat->ops.solveadd) { 1003 ierr = (*mat->ops.solveadd)(mat,b,y,x); CHKERRQ(ierr); 1004 } 1005 else { 1006 /* do the solve then the add manually */ 1007 if (x != y) { 1008 ierr = MatSolve(mat,b,x); CHKERRQ(ierr); 1009 ierr = VecAXPY(&one,y,x); CHKERRQ(ierr); 1010 } 1011 else { 1012 ierr = VecDuplicate(x,&tmp); CHKERRQ(ierr); 1013 PLogObjectParent(mat,tmp); 1014 ierr = VecCopy(x,tmp); CHKERRQ(ierr); 1015 ierr = MatSolve(mat,b,x); CHKERRQ(ierr); 1016 ierr = VecAXPY(&one,tmp,x); CHKERRQ(ierr); 1017 ierr = VecDestroy(tmp); CHKERRQ(ierr); 1018 } 1019 } 1020 PLogEventEnd(MAT_SolveAdd,mat,b,x,y); 1021 return 0; 1022 } 1023 1024 /*@ 1025 MatSolveTrans - Solves A' x = b, given a factored matrix. 1026 1027 Input Parameters: 1028 . mat - the factored matrix 1029 . b - the right-hand-side vector 1030 1031 Output Parameter: 1032 . x - the result vector 1033 1034 Notes: 1035 The vectors b and x cannot be the same. I.e., one cannot 1036 call MatSolveTrans(A,x,x). 1037 1038 .keywords: matrix, linear system, solve, LU, Cholesky, transpose 1039 1040 .seealso: MatSolve(), MatSolveAdd(), MatSolveTransAdd() 1041 @*/ 1042 int MatSolveTrans(Mat mat,Vec b,Vec x) 1043 { 1044 int ierr; 1045 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1046 PetscValidHeaderSpecific(b,VEC_COOKIE); PetscValidHeaderSpecific(x,VEC_COOKIE); 1047 if (!mat->factor) SETERRQ(1,"MatSolveTrans:Unfactored matrix"); 1048 if (x == b) SETERRQ(1,"MatSolveTrans:x and b must be different vectors"); 1049 if (!mat->ops.solvetrans) SETERRQ(PETSC_ERR_SUP,"MatSolveTrans"); 1050 if (mat->M != x->N) SETERRQ(PETSC_ERR_SIZ,"MatSolveTrans:Mat mat,Vec x: global dim"); 1051 if (mat->N != b->N) SETERRQ(PETSC_ERR_SIZ,"MatSolveTrans:Mat mat,Vec b: global dim"); 1052 1053 PLogEventBegin(MAT_SolveTrans,mat,b,x,0); 1054 ierr = (*mat->ops.solvetrans)(mat,b,x); CHKERRQ(ierr); 1055 PLogEventEnd(MAT_SolveTrans,mat,b,x,0); 1056 return 0; 1057 } 1058 1059 /*@ 1060 MatSolveTransAdd - Computes x = y + inv(trans(A)) b, given a 1061 factored matrix. 1062 1063 Input Parameters: 1064 . mat - the factored matrix 1065 . b - the right-hand-side vector 1066 . y - the vector to be added to 1067 1068 Output Parameter: 1069 . x - the result vector 1070 1071 Notes: 1072 The vectors b and x cannot be the same. I.e., one cannot 1073 call MatSolveTransAdd(A,x,y,x). 1074 1075 .keywords: matrix, linear system, solve, LU, Cholesky, transpose, add 1076 1077 .seealso: MatSolve(), MatSolveAdd(), MatSolveTrans() 1078 @*/ 1079 int MatSolveTransAdd(Mat mat,Vec b,Vec y,Vec x) 1080 { 1081 Scalar one = 1.0; 1082 int ierr; 1083 Vec tmp; 1084 PetscValidHeaderSpecific(mat,MAT_COOKIE);PetscValidHeaderSpecific(y,VEC_COOKIE); 1085 PetscValidHeaderSpecific(b,VEC_COOKIE); PetscValidHeaderSpecific(x,VEC_COOKIE); 1086 if (x == b) SETERRQ(1,"MatSolveTransAdd:x and b must be different vectors"); 1087 if (!mat->factor) SETERRQ(1,"MatSolveTransAdd:Unfactored matrix"); 1088 if (mat->M != x->N) SETERRQ(PETSC_ERR_SIZ,"MatSolveTransAdd:Mat mat,Vec x: global dim"); 1089 if (mat->N != b->N) SETERRQ(PETSC_ERR_SIZ,"MatSolveTransAdd:Mat mat,Vec b: global dim"); 1090 if (mat->N != y->N) SETERRQ(PETSC_ERR_SIZ,"MatSolveTransAdd:Mat mat,Vec y: global dim"); 1091 if (x->n != y->n) SETERRQ(PETSC_ERR_SIZ,"MatSolveTransAdd:Vec x,Vec y: local dim"); 1092 1093 PLogEventBegin(MAT_SolveTransAdd,mat,b,x,y); 1094 if (mat->ops.solvetransadd) { 1095 ierr = (*mat->ops.solvetransadd)(mat,b,y,x); CHKERRQ(ierr); 1096 } 1097 else { 1098 /* do the solve then the add manually */ 1099 if (x != y) { 1100 ierr = MatSolveTrans(mat,b,x); CHKERRQ(ierr); 1101 ierr = VecAXPY(&one,y,x); CHKERRQ(ierr); 1102 } 1103 else { 1104 ierr = VecDuplicate(x,&tmp); CHKERRQ(ierr); 1105 PLogObjectParent(mat,tmp); 1106 ierr = VecCopy(x,tmp); CHKERRQ(ierr); 1107 ierr = MatSolveTrans(mat,b,x); CHKERRQ(ierr); 1108 ierr = VecAXPY(&one,tmp,x); CHKERRQ(ierr); 1109 ierr = VecDestroy(tmp); CHKERRQ(ierr); 1110 } 1111 } 1112 PLogEventEnd(MAT_SolveTransAdd,mat,b,x,y); 1113 return 0; 1114 } 1115 /* ----------------------------------------------------------------*/ 1116 1117 /*@ 1118 MatRelax - Computes one relaxation sweep. 1119 1120 Input Parameters: 1121 . mat - the matrix 1122 . b - the right hand side 1123 . omega - the relaxation factor 1124 . flag - flag indicating the type of SOR, one of 1125 $ SOR_FORWARD_SWEEP 1126 $ SOR_BACKWARD_SWEEP 1127 $ SOR_SYMMETRIC_SWEEP (SSOR method) 1128 $ SOR_LOCAL_FORWARD_SWEEP 1129 $ SOR_LOCAL_BACKWARD_SWEEP 1130 $ SOR_LOCAL_SYMMETRIC_SWEEP (local SSOR) 1131 $ SOR_APPLY_UPPER, SOR_APPLY_LOWER - applies 1132 $ upper/lower triangular part of matrix to 1133 $ vector (with omega) 1134 $ SOR_ZERO_INITIAL_GUESS - zero initial guess 1135 . shift - diagonal shift 1136 . its - the number of iterations 1137 1138 Output Parameters: 1139 . x - the solution (can contain an initial guess) 1140 1141 Notes: 1142 SOR_LOCAL_FORWARD_SWEEP, SOR_LOCAL_BACKWARD_SWEEP, and 1143 SOR_LOCAL_SYMMETRIC_SWEEP perform seperate independent smoothings 1144 on each processor. 1145 1146 Application programmers will not generally use MatRelax() directly, 1147 but instead will employ the SLES/PC interface. 1148 1149 Notes for Advanced Users: 1150 The flags are implemented as bitwise inclusive or operations. 1151 For example, use (SOR_ZERO_INITIAL_GUESS | SOR_SYMMETRIC_SWEEP) 1152 to specify a zero initial guess for SSOR. 1153 1154 .keywords: matrix, relax, relaxation, sweep 1155 @*/ 1156 int MatRelax(Mat mat,Vec b,double omega,MatSORType flag,double shift, 1157 int its,Vec x) 1158 { 1159 int ierr; 1160 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1161 PetscValidHeaderSpecific(b,VEC_COOKIE); PetscValidHeaderSpecific(x,VEC_COOKIE); 1162 if (!mat->ops.relax) SETERRQ(PETSC_ERR_SUP,"MatRelax"); 1163 if (!mat->assembled) SETERRQ(1,"MatRelax:Not for unassembled matrix"); 1164 if (mat->factor) SETERRQ(1,"MatRelax:Not for factored matrix"); 1165 if (mat->N != x->N) SETERRQ(PETSC_ERR_SIZ,"MatRelax:Mat mat,Vec x: global dim"); 1166 if (mat->M != b->N) SETERRQ(PETSC_ERR_SIZ,"MatRelax:Mat mat,Vec b: global dim"); 1167 if (mat->m != b->n) SETERRQ(PETSC_ERR_SIZ,"MatRelax:Mat mat,Vec b: local dim"); 1168 1169 PLogEventBegin(MAT_Relax,mat,b,x,0); 1170 ierr =(*mat->ops.relax)(mat,b,omega,flag,shift,its,x); CHKERRQ(ierr); 1171 PLogEventEnd(MAT_Relax,mat,b,x,0); 1172 return 0; 1173 } 1174 1175 /* 1176 Default matrix copy routine. 1177 */ 1178 int MatCopy_Basic(Mat A,Mat B) 1179 { 1180 int ierr,i,rstart,rend,nz,*cwork; 1181 Scalar *vwork; 1182 1183 ierr = MatZeroEntries(B); CHKERRQ(ierr); 1184 ierr = MatGetOwnershipRange(A,&rstart,&rend); CHKERRQ(ierr); 1185 for (i=rstart; i<rend; i++) { 1186 ierr = MatGetRow(A,i,&nz,&cwork,&vwork); CHKERRQ(ierr); 1187 ierr = MatSetValues(B,1,&i,nz,cwork,vwork,INSERT_VALUES); CHKERRQ(ierr); 1188 ierr = MatRestoreRow(A,i,&nz,&cwork,&vwork); CHKERRQ(ierr); 1189 } 1190 ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 1191 ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 1192 return 0; 1193 } 1194 1195 /*@C 1196 MatCopy - Copys a matrix to another matrix. 1197 1198 Input Parameters: 1199 . A - the matrix 1200 1201 Output Parameter: 1202 . B - where the copy is put 1203 1204 Notes: 1205 MatCopy() copies the matrix entries of a matrix to another existing 1206 matrix (after first zeroing the second matrix). A related routine is 1207 MatConvert(), which first creates a new matrix and then copies the data. 1208 1209 .keywords: matrix, copy, convert 1210 1211 .seealso: MatConvert() 1212 @*/ 1213 int MatCopy(Mat A,Mat B) 1214 { 1215 int ierr; 1216 PetscValidHeaderSpecific(A,MAT_COOKIE); PetscValidHeaderSpecific(B,MAT_COOKIE); 1217 if (!A->assembled) SETERRQ(1,"MatCopy:Not for unassembled matrix"); 1218 if (A->factor) SETERRQ(1,"MatCopy:Not for factored matrix"); 1219 if (A->M != B->M || A->N != B->N) SETERRQ(PETSC_ERR_SIZ,"MatCopy:Mat A,Mat B: global dim"); 1220 1221 PLogEventBegin(MAT_Copy,A,B,0,0); 1222 if (A->ops.copy) { 1223 ierr = (*A->ops.copy)(A,B); CHKERRQ(ierr); 1224 } 1225 else { /* generic conversion */ 1226 ierr = MatCopy_Basic(A,B); CHKERRQ(ierr); 1227 } 1228 PLogEventEnd(MAT_Copy,A,B,0,0); 1229 return 0; 1230 } 1231 1232 /*@C 1233 MatConvert - Converts a matrix to another matrix, either of the same 1234 or different type. 1235 1236 Input Parameters: 1237 . mat - the matrix 1238 . newtype - new matrix type. Use MATSAME to create a new matrix of the 1239 same type as the original matrix. 1240 1241 Output Parameter: 1242 . M - pointer to place new matrix 1243 1244 Notes: 1245 MatConvert() first creates a new matrix and then copies the data from 1246 the first matrix. A related routine is MatCopy(), which copies the matrix 1247 entries of one matrix to another already existing matrix context. 1248 1249 .keywords: matrix, copy, convert 1250 1251 .seealso: MatCopy() 1252 @*/ 1253 int MatConvert(Mat mat,MatType newtype,Mat *M) 1254 { 1255 int ierr; 1256 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1257 if (!M) SETERRQ(1,"MatConvert:Bad new matrix address"); 1258 if (!mat->assembled) SETERRQ(1,"MatConvert:Not for unassembled matrix"); 1259 if (mat->factor) SETERRQ(1,"MatConvert:Not for factored matrix"); 1260 1261 PLogEventBegin(MAT_Convert,mat,0,0,0); 1262 if (newtype == mat->type || newtype == MATSAME) { 1263 if (mat->ops.convertsametype) { /* customized copy */ 1264 ierr = (*mat->ops.convertsametype)(mat,M,COPY_VALUES); CHKERRQ(ierr); 1265 } 1266 else { /* generic conversion */ 1267 ierr = MatConvert_Basic(mat,newtype,M); CHKERRQ(ierr); 1268 } 1269 } 1270 else if (mat->ops.convert) { /* customized conversion */ 1271 ierr = (*mat->ops.convert)(mat,newtype,M); CHKERRQ(ierr); 1272 } 1273 else { /* generic conversion */ 1274 ierr = MatConvert_Basic(mat,newtype,M); CHKERRQ(ierr); 1275 } 1276 PLogEventEnd(MAT_Convert,mat,0,0,0); 1277 return 0; 1278 } 1279 1280 /*@ 1281 MatGetDiagonal - Gets the diagonal of a matrix. 1282 1283 Input Parameters: 1284 . mat - the matrix 1285 . v - the vector for storing the diagonal 1286 1287 Output Parameter: 1288 . v - the diagonal of the matrix 1289 1290 .keywords: matrix, get, diagonal 1291 @*/ 1292 int MatGetDiagonal(Mat mat,Vec v) 1293 { 1294 PetscValidHeaderSpecific(mat,MAT_COOKIE);PetscValidHeaderSpecific(v,VEC_COOKIE); 1295 if (!mat->assembled) SETERRQ(1,"MatGetDiagonal:Not for unassembled matrix"); 1296 if (mat->factor) SETERRQ(1,"MatGetDiagonal:Not for factored matrix"); 1297 if (!mat->ops.getdiagonal) SETERRQ(PETSC_ERR_SUP,"MatGetDiagonal"); 1298 return (*mat->ops.getdiagonal)(mat,v); 1299 } 1300 1301 /*@C 1302 MatTranspose - Computes an in-place or out-of-place transpose of a matrix. 1303 1304 Input Parameter: 1305 . mat - the matrix to transpose 1306 1307 Output Parameters: 1308 . B - the transpose (or pass in PETSC_NULL for an in-place transpose) 1309 1310 .keywords: matrix, transpose 1311 1312 .seealso: MatMultTrans(), MatMultTransAdd() 1313 @*/ 1314 int MatTranspose(Mat mat,Mat *B) 1315 { 1316 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1317 if (!mat->assembled) SETERRQ(1,"MatTranspose:Not for unassembled matrix"); 1318 if (mat->factor) SETERRQ(1,"MatTranspose:Not for factored matrix"); 1319 if (!mat->ops.transpose) SETERRQ(PETSC_ERR_SUP,"MatTranspose"); 1320 return (*mat->ops.transpose)(mat,B); 1321 } 1322 1323 /*@ 1324 MatEqual - Compares two matrices. 1325 1326 Input Parameters: 1327 . A - the first matrix 1328 . B - the second matrix 1329 1330 Output Parameter: 1331 . flg : PETSC_TRUE if the matrices are equal; 1332 PETSC_FALSE otherwise. 1333 1334 .keywords: matrix, equal, equivalent 1335 @*/ 1336 int MatEqual(Mat A,Mat B,PetscTruth *flg) 1337 { 1338 PetscValidHeaderSpecific(A,MAT_COOKIE); PetscValidHeaderSpecific(B,MAT_COOKIE); 1339 PetscValidIntPointer(flg); 1340 if (!A->assembled) SETERRQ(1,"MatEqual:Not for unassembled matrix"); 1341 if (!B->assembled) SETERRQ(1,"MatEqual:Not for unassembled matrix"); 1342 if (A->M != B->M || A->N != B->N) SETERRQ(PETSC_ERR_SIZ,"MatCopy:Mat A,Mat B: global dim"); 1343 if (!A->ops.equal) SETERRQ(PETSC_ERR_SUP,"MatEqual"); 1344 return (*A->ops.equal)(A,B,flg); 1345 } 1346 1347 /*@ 1348 MatDiagonalScale - Scales a matrix on the left and right by diagonal 1349 matrices that are stored as vectors. Either of the two scaling 1350 matrices can be PETSC_NULL. 1351 1352 Input Parameters: 1353 . mat - the matrix to be scaled 1354 . l - the left scaling vector (or PETSC_NULL) 1355 . r - the right scaling vector (or PETSC_NULL) 1356 1357 Notes: 1358 MatDiagonalScale() computes A <- LAR, where 1359 $ L = a diagonal matrix 1360 $ R = a diagonal matrix 1361 1362 .keywords: matrix, diagonal, scale 1363 1364 .seealso: MatDiagonalScale() 1365 @*/ 1366 int MatDiagonalScale(Mat mat,Vec l,Vec r) 1367 { 1368 int ierr; 1369 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1370 if (!mat->ops.diagonalscale) SETERRQ(PETSC_ERR_SUP,"MatDiagonalScale"); 1371 if (l) PetscValidHeaderSpecific(l,VEC_COOKIE); 1372 if (r) PetscValidHeaderSpecific(r,VEC_COOKIE); 1373 if (!mat->assembled) SETERRQ(1,"MatDiagonalScale:Not for unassembled matrix"); 1374 if (mat->factor) SETERRQ(1,"MatDiagonalScale:Not for factored matrix"); 1375 1376 PLogEventBegin(MAT_Scale,mat,0,0,0); 1377 ierr = (*mat->ops.diagonalscale)(mat,l,r); CHKERRQ(ierr); 1378 PLogEventEnd(MAT_Scale,mat,0,0,0); 1379 return 0; 1380 } 1381 1382 /*@ 1383 MatScale - Scales all elements of a matrix by a given number. 1384 1385 Input Parameters: 1386 . mat - the matrix to be scaled 1387 . a - the scaling value 1388 1389 Output Parameter: 1390 . mat - the scaled matrix 1391 1392 .keywords: matrix, scale 1393 1394 .seealso: MatDiagonalScale() 1395 @*/ 1396 int MatScale(Scalar *a,Mat mat) 1397 { 1398 int ierr; 1399 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1400 PetscValidScalarPointer(a); 1401 if (!mat->ops.scale) SETERRQ(PETSC_ERR_SUP,"MatScale"); 1402 if (!mat->assembled) SETERRQ(1,"MatScale:Not for unassembled matrix"); 1403 if (mat->factor) SETERRQ(1,"MatScale:Not for factored matrix"); 1404 1405 PLogEventBegin(MAT_Scale,mat,0,0,0); 1406 ierr = (*mat->ops.scale)(a,mat); CHKERRQ(ierr); 1407 PLogEventEnd(MAT_Scale,mat,0,0,0); 1408 return 0; 1409 } 1410 1411 /*@ 1412 MatNorm - Calculates various norms of a matrix. 1413 1414 Input Parameters: 1415 . mat - the matrix 1416 . type - the type of norm, NORM_1, NORM_2, NORM_FROBENIUS, NORM_INFINITY 1417 1418 Output Parameters: 1419 . norm - the resulting norm 1420 1421 .keywords: matrix, norm, Frobenius 1422 @*/ 1423 int MatNorm(Mat mat,NormType type,double *norm) 1424 { 1425 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1426 PetscValidScalarPointer(norm); 1427 1428 if (!norm) SETERRQ(1,"MatNorm:bad addess for value"); 1429 if (!mat->assembled) SETERRQ(1,"MatNorm:Not for unassembled matrix"); 1430 if (mat->factor) SETERRQ(1,"MatNorm:Not for factored matrix"); 1431 if (!mat->ops.norm) SETERRQ(PETSC_ERR_SUP,"MatNorm:Not for this matrix type"); 1432 return (*mat->ops.norm)(mat,type,norm); 1433 } 1434 1435 /* 1436 This variable is used to prevent counting of MatAssemblyBegin() that 1437 are called from within a MatAssemblyEnd(). 1438 */ 1439 static int MatAssemblyEnd_InUse = 0; 1440 /*@ 1441 MatAssemblyBegin - Begins assembling the matrix. This routine should 1442 be called after completing all calls to MatSetValues(). 1443 1444 Input Parameters: 1445 . mat - the matrix 1446 . type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY 1447 1448 Notes: 1449 MatSetValues() generally caches the values. The matrix is ready to 1450 use only after MatAssemblyBegin() and MatAssemblyEnd() have been called. 1451 Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES 1452 in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before 1453 using the matrix. 1454 1455 .keywords: matrix, assembly, assemble, begin 1456 1457 .seealso: MatAssemblyEnd(), MatSetValues() 1458 @*/ 1459 int MatAssemblyBegin(Mat mat,MatAssemblyType type) 1460 { 1461 int ierr; 1462 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1463 if (mat->factor) SETERRQ(1,"MatAssemblyBegin:Not for factored matrix"); 1464 if (mat->assembled) { 1465 mat->was_assembled = PETSC_TRUE; 1466 mat->assembled = PETSC_FALSE; 1467 } 1468 if (!MatAssemblyEnd_InUse) { 1469 PLogEventBegin(MAT_AssemblyBegin,mat,0,0,0); 1470 if (mat->ops.assemblybegin){ierr = (*mat->ops.assemblybegin)(mat,type);CHKERRQ(ierr);} 1471 PLogEventEnd(MAT_AssemblyBegin,mat,0,0,0); 1472 } else { 1473 if (mat->ops.assemblybegin){ierr = (*mat->ops.assemblybegin)(mat,type);CHKERRQ(ierr);} 1474 } 1475 return 0; 1476 } 1477 1478 /*@ 1479 MatAssemblyEnd - Completes assembling the matrix. This routine should 1480 be called after MatAssemblyBegin(). 1481 1482 Input Parameters: 1483 . mat - the matrix 1484 . type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY 1485 1486 Options Database Keys: 1487 $ -mat_view_info : Prints info on matrix at 1488 $ conclusion of MatEndAssembly() 1489 $ -mat_view_info_detailed: Prints more detailed info. 1490 $ -mat_view : Prints matrix in ASCII format. 1491 $ -mat_view_matlab : Prints matrix in Matlab format. 1492 $ -mat_view_draw : Draws nonzero structure of matrix, 1493 $ using MatView() and DrawOpenX(). 1494 $ -display <name> : Set display name (default is host) 1495 $ -draw_pause <sec> : Set number of seconds to pause after display 1496 1497 Notes: 1498 MatSetValues() generally caches the values. The matrix is ready to 1499 use only after MatAssemblyBegin() and MatAssemblyEnd() have been called. 1500 Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES 1501 in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before 1502 using the matrix. 1503 1504 .keywords: matrix, assembly, assemble, end 1505 1506 .seealso: MatAssemblyBegin(), MatSetValues(), DrawOpenX(), MatView() 1507 @*/ 1508 int MatAssemblyEnd(Mat mat,MatAssemblyType type) 1509 { 1510 int ierr,flg; 1511 static int inassm = 0; 1512 1513 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1514 inassm++; 1515 MatAssemblyEnd_InUse++; 1516 PLogEventBegin(MAT_AssemblyEnd,mat,0,0,0); 1517 if (mat->ops.assemblyend) { 1518 ierr = (*mat->ops.assemblyend)(mat,type); CHKERRQ(ierr); 1519 } 1520 mat->assembled = PETSC_TRUE; mat->num_ass++; 1521 PLogEventEnd(MAT_AssemblyEnd,mat,0,0,0); 1522 MatAssemblyEnd_InUse--; 1523 1524 if (inassm == 1) { 1525 ierr = OptionsHasName(PETSC_NULL,"-mat_view_info",&flg); CHKERRQ(ierr); 1526 if (flg) { 1527 Viewer viewer; 1528 ierr = ViewerFileOpenASCII(mat->comm,"stdout",&viewer);CHKERRQ(ierr); 1529 ierr = ViewerSetFormat(viewer,VIEWER_FORMAT_ASCII_INFO,0);CHKERRQ(ierr); 1530 ierr = MatView(mat,viewer); CHKERRQ(ierr); 1531 ierr = ViewerDestroy(viewer); CHKERRQ(ierr); 1532 } 1533 ierr = OptionsHasName(PETSC_NULL,"-mat_view_info_detailed",&flg);CHKERRQ(ierr); 1534 if (flg) { 1535 Viewer viewer; 1536 ierr = ViewerFileOpenASCII(mat->comm,"stdout",&viewer);CHKERRQ(ierr); 1537 ierr = ViewerSetFormat(viewer,VIEWER_FORMAT_ASCII_INFO_LONG,0);CHKERRQ(ierr); 1538 ierr = MatView(mat,viewer); CHKERRQ(ierr); 1539 ierr = ViewerDestroy(viewer); CHKERRQ(ierr); 1540 } 1541 ierr = OptionsHasName(PETSC_NULL,"-mat_view",&flg); CHKERRQ(ierr); 1542 if (flg) { 1543 Viewer viewer; 1544 ierr = ViewerFileOpenASCII(mat->comm,"stdout",&viewer);CHKERRQ(ierr); 1545 ierr = MatView(mat,viewer); CHKERRQ(ierr); 1546 ierr = ViewerDestroy(viewer); CHKERRQ(ierr); 1547 } 1548 ierr = OptionsHasName(PETSC_NULL,"-mat_view_matlab",&flg); CHKERRQ(ierr); 1549 if (flg) { 1550 Viewer viewer; 1551 ierr = ViewerFileOpenASCII(mat->comm,"stdout",&viewer);CHKERRQ(ierr); 1552 ierr = ViewerSetFormat(viewer,VIEWER_FORMAT_ASCII_MATLAB,"M");CHKERRQ(ierr); 1553 ierr = MatView(mat,viewer); CHKERRQ(ierr); 1554 ierr = ViewerDestroy(viewer); CHKERRQ(ierr); 1555 } 1556 ierr = OptionsHasName(PETSC_NULL,"-mat_view_draw",&flg); CHKERRQ(ierr); 1557 if (flg) { 1558 Viewer viewer; 1559 ierr = ViewerDrawOpenX(mat->comm,0,0,0,0,300,300,&viewer); CHKERRQ(ierr); 1560 ierr = MatView(mat,viewer); CHKERRQ(ierr); 1561 ierr = ViewerFlush(viewer); CHKERRQ(ierr); 1562 ierr = ViewerDestroy(viewer); CHKERRQ(ierr); 1563 } 1564 } 1565 inassm--; 1566 return 0; 1567 } 1568 1569 /*@ 1570 MatCompress - Tries to store the matrix in as little space as 1571 possible. May fail if memory is already fully used, since it 1572 tries to allocate new space. 1573 1574 Input Parameters: 1575 . mat - the matrix 1576 1577 .keywords: matrix, compress 1578 @*/ 1579 int MatCompress(Mat mat) 1580 { 1581 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1582 if (mat->ops.compress) return (*mat->ops.compress)(mat); 1583 return 0; 1584 } 1585 1586 /*@ 1587 MatSetOption - Sets a parameter option for a matrix. Some options 1588 may be specific to certain storage formats. Some options 1589 determine how values will be inserted (or added). Sorted, 1590 row-oriented input will generally assemble the fastest. The default 1591 is row-oriented, nonsorted input. 1592 1593 Input Parameters: 1594 . mat - the matrix 1595 . option - the option, one of the following: 1596 $ MAT_ROW_ORIENTED 1597 $ MAT_COLUMN_ORIENTED, 1598 $ MAT_ROWS_SORTED, 1599 $ MAT_COLUMNS_SORTED, 1600 $ MAT_NO_NEW_NONZERO_LOCATIONS, 1601 $ MAT_YES_NEW_NONZERO_LOCATIONS, 1602 $ MAT_SYMMETRIC, 1603 $ MAT_STRUCTURALLY_SYMMETRIC, 1604 $ MAT_NO_NEW_DIAGONALS, 1605 $ MAT_YES_NEW_DIAGONALS, 1606 $ MAT_IGNORE_OFF_PROCESSOR_ENTRIES 1607 $ and possibly others. 1608 1609 Notes: 1610 Some options are relevant only for particular matrix types and 1611 are thus ignored by others. Other options are not supported by 1612 certain matrix types and will generate an error message if set. 1613 1614 If using a Fortran 77 module to compute a matrix, one may need to 1615 use the column-oriented option (or convert to the row-oriented 1616 format). 1617 1618 MAT_NO_NEW_NONZERO_LOCATIONS indicates that any add or insertion 1619 that will generate a new entry in the nonzero structure is ignored. 1620 What this means is if memory is not allocated for this particular 1621 lot, then the insertion is ignored. For dense matrices, where 1622 the entire array is allocated, no entries are ever ignored. 1623 1624 MAT_IGNORE_OFF_PROCESSOR_ENTRIES indicates entries destined for 1625 other processors are dropped, rather then stashed. 1626 1627 .keywords: matrix, option, row-oriented, column-oriented, sorted, nonzero 1628 @*/ 1629 int MatSetOption(Mat mat,MatOption op) 1630 { 1631 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1632 if (mat->ops.setoption) return (*mat->ops.setoption)(mat,op); 1633 return 0; 1634 } 1635 1636 /*@ 1637 MatZeroEntries - Zeros all entries of a matrix. For sparse matrices 1638 this routine retains the old nonzero structure. 1639 1640 Input Parameters: 1641 . mat - the matrix 1642 1643 .keywords: matrix, zero, entries 1644 1645 .seealso: MatZeroRows() 1646 @*/ 1647 int MatZeroEntries(Mat mat) 1648 { 1649 int ierr; 1650 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1651 if (mat->factor) SETERRQ(1,"MatZeroEntries:Not for factored matrix"); 1652 if (!mat->ops.zeroentries) SETERRQ(PETSC_ERR_SUP,"MatZeroEntries"); 1653 1654 PLogEventBegin(MAT_ZeroEntries,mat,0,0,0); 1655 ierr = (*mat->ops.zeroentries)(mat); CHKERRQ(ierr); 1656 PLogEventEnd(MAT_ZeroEntries,mat,0,0,0); 1657 return 0; 1658 } 1659 1660 /*@ 1661 MatZeroRows - Zeros all entries (except possibly the main diagonal) 1662 of a set of rows of a matrix. 1663 1664 Input Parameters: 1665 . mat - the matrix 1666 . is - index set of rows to remove 1667 . diag - pointer to value put in all diagonals of eliminated rows. 1668 Note that diag is not a pointer to an array, but merely a 1669 pointer to a single value. 1670 1671 Notes: 1672 For the AIJ matrix formats this removes the old nonzero structure, 1673 but does not release memory. For the dense and block diagonal 1674 formats this does not alter the nonzero structure. 1675 1676 The user can set a value in the diagonal entry (or for the AIJ and 1677 row formats can optionally remove the main diagonal entry from the 1678 nonzero structure as well, by passing a null pointer as the final 1679 argument). 1680 1681 .keywords: matrix, zero, rows, boundary conditions 1682 1683 .seealso: MatZeroEntries(), 1684 @*/ 1685 int MatZeroRows(Mat mat,IS is, Scalar *diag) 1686 { 1687 int ierr; 1688 1689 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1690 PetscValidHeaderSpecific(is,IS_COOKIE); 1691 if (diag) PetscValidScalarPointer(diag); 1692 if (!mat->assembled) SETERRQ(1,"MatZeroRows:Not for unassembled matrix"); 1693 if (mat->factor) SETERRQ(1,"MatZeroRows:Not for factored matrix"); 1694 if (!mat->ops.zerorows) SETERRQ(PETSC_ERR_SUP,"MatZeroRows"); 1695 1696 ierr = (*mat->ops.zerorows)(mat,is,diag); CHKERRQ(ierr); 1697 return 0; 1698 } 1699 1700 /*@ 1701 MatZeroRowsLocal - Zeros all entries (except possibly the main diagonal) 1702 of a set of rows of a matrix; using local numbering of rows. 1703 1704 Input Parameters: 1705 . mat - the matrix 1706 . is - index set of rows to remove 1707 . diag - pointer to value put in all diagonals of eliminated rows. 1708 Note that diag is not a pointer to an array, but merely a 1709 pointer to a single value. 1710 1711 Notes: 1712 For the AIJ matrix formats this removes the old nonzero structure, 1713 but does not release memory. For the dense and block diagonal 1714 formats this does not alter the nonzero structure. 1715 1716 The user can set a value in the diagonal entry (or for the AIJ and 1717 row formats can optionally remove the main diagonal entry from the 1718 nonzero structure as well, by passing a null pointer as the final 1719 argument). 1720 1721 .keywords: matrix, zero, rows, boundary conditions 1722 1723 .seealso: MatZeroEntries(), 1724 @*/ 1725 int MatZeroRowsLocal(Mat mat,IS is, Scalar *diag) 1726 { 1727 int ierr; 1728 IS newis; 1729 1730 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1731 PetscValidHeaderSpecific(is,IS_COOKIE); 1732 if (diag) PetscValidScalarPointer(diag); 1733 if (!mat->assembled) SETERRQ(1,"MatZeroRows:Not for unassembled matrix"); 1734 if (mat->factor) SETERRQ(1,"MatZeroRows:Not for factored matrix"); 1735 if (!mat->ops.zerorows) SETERRQ(PETSC_ERR_SUP,"MatZeroRows"); 1736 1737 ierr = ISLocalToGlobalMappingApplyIS(mat->mapping,is,&newis); CHKERRQ(ierr); 1738 ierr = (*mat->ops.zerorows)(mat,newis,diag); CHKERRQ(ierr); 1739 ierr = ISDestroy(newis); 1740 return 0; 1741 } 1742 1743 /*@ 1744 MatGetSize - Returns the numbers of rows and columns in a matrix. 1745 1746 Input Parameter: 1747 . mat - the matrix 1748 1749 Output Parameters: 1750 . m - the number of global rows 1751 . n - the number of global columns 1752 1753 .keywords: matrix, dimension, size, rows, columns, global, get 1754 1755 .seealso: MatGetLocalSize() 1756 @*/ 1757 int MatGetSize(Mat mat,int *m,int* n) 1758 { 1759 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1760 PetscValidIntPointer(m); 1761 PetscValidIntPointer(n); 1762 return (*mat->ops.getsize)(mat,m,n); 1763 } 1764 1765 /*@ 1766 MatGetLocalSize - Returns the number of rows and columns in a matrix 1767 stored locally. This information may be implementation dependent, so 1768 use with care. 1769 1770 Input Parameters: 1771 . mat - the matrix 1772 1773 Output Parameters: 1774 . m - the number of local rows 1775 . n - the number of local columns 1776 1777 .keywords: matrix, dimension, size, local, rows, columns, get 1778 1779 .seealso: MatGetSize() 1780 @*/ 1781 int MatGetLocalSize(Mat mat,int *m,int* n) 1782 { 1783 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1784 PetscValidIntPointer(m); 1785 PetscValidIntPointer(n); 1786 return (*mat->ops.getlocalsize)(mat,m,n); 1787 } 1788 1789 /*@ 1790 MatGetOwnershipRange - Returns the range of matrix rows owned by 1791 this processor, assuming that the matrix is laid out with the first 1792 n1 rows on the first processor, the next n2 rows on the second, etc. 1793 For certain parallel layouts this range may not be well defined. 1794 1795 Input Parameters: 1796 . mat - the matrix 1797 1798 Output Parameters: 1799 . m - the first local row 1800 . n - one more then the last local row 1801 1802 .keywords: matrix, get, range, ownership 1803 @*/ 1804 int MatGetOwnershipRange(Mat mat,int *m,int* n) 1805 { 1806 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1807 PetscValidIntPointer(m); 1808 PetscValidIntPointer(n); 1809 if (!mat->ops.getownershiprange) SETERRQ(PETSC_ERR_SUP,"MatGetOwnershipRange"); 1810 return (*mat->ops.getownershiprange)(mat,m,n); 1811 } 1812 1813 /*@ 1814 MatILUFactorSymbolic - Performs symbolic ILU factorization of a matrix. 1815 Uses levels of fill only, not drop tolerance. Use MatLUFactorNumeric() 1816 to complete the factorization. 1817 1818 Input Parameters: 1819 . mat - the matrix 1820 . row - row permutation 1821 . column - column permutation 1822 . fill - number of levels of fill 1823 . f - expected fill as ratio of the original number of nonzeros, 1824 for example 3.0; choosing this parameter well can result in 1825 more efficient use of time and space. 1826 1827 Output Parameters: 1828 . fact - new matrix that has been symbolically factored 1829 1830 Options Database Key: 1831 $ -mat_ilu_fill <f>, where f is the fill ratio 1832 1833 Notes: 1834 See the file $(PETSC_DIR)/Performace for additional information about 1835 choosing the fill factor for better efficiency. 1836 1837 .keywords: matrix, factor, incomplete, ILU, symbolic, fill 1838 1839 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric() 1840 @*/ 1841 int MatILUFactorSymbolic(Mat mat,IS row,IS col,double f,int fill,Mat *fact) 1842 { 1843 int ierr,flg; 1844 1845 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1846 if (fill < 0) SETERRQ(1,"MatILUFactorSymbolic:Levels of fill negative"); 1847 if (!fact) SETERRQ(1,"MatILUFactorSymbolic:Fact argument is missing"); 1848 if (!mat->ops.ilufactorsymbolic) SETERRQ(PETSC_ERR_SUP,"MatILUFactorSymbolic"); 1849 if (!mat->assembled) SETERRQ(1,"MatILUFactorSymbolic:Not for unassembled matrix"); 1850 if (mat->factor) SETERRQ(1,"MatILUFactorSymbolic:Not for factored matrix"); 1851 1852 ierr = OptionsGetDouble(PETSC_NULL,"-mat_ilu_fill",&f,&flg); CHKERRQ(ierr); 1853 PLogEventBegin(MAT_ILUFactorSymbolic,mat,row,col,0); 1854 ierr = (*mat->ops.ilufactorsymbolic)(mat,row,col,f,fill,fact); CHKERRQ(ierr); 1855 PLogEventEnd(MAT_ILUFactorSymbolic,mat,row,col,0); 1856 return 0; 1857 } 1858 1859 /*@ 1860 MatIncompleteCholeskyFactorSymbolic - Performs symbolic incomplete 1861 Cholesky factorization for a symmetric matrix. Use 1862 MatCholeskyFactorNumeric() to complete the factorization. 1863 1864 Input Parameters: 1865 . mat - the matrix 1866 . perm - row and column permutation 1867 . fill - levels of fill 1868 . f - expected fill as ratio of original fill 1869 1870 Output Parameter: 1871 . fact - the factored matrix 1872 1873 Note: Currently only no-fill factorization is supported. 1874 1875 .keywords: matrix, factor, incomplete, ICC, Cholesky, symbolic, fill 1876 1877 .seealso: MatCholeskyFactorNumeric(), MatCholeskyFactor() 1878 @*/ 1879 int MatIncompleteCholeskyFactorSymbolic(Mat mat,IS perm,double f,int fill, 1880 Mat *fact) 1881 { 1882 int ierr; 1883 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1884 if (mat->factor) SETERRQ(1,"MatIncompleteCholeskyFactorSymbolic:Not for factored matrix"); 1885 if (fill < 0) SETERRQ(1,"MatIncompleteCholeskyFactorSymbolic:Fill negative"); 1886 if (!fact) SETERRQ(1,"MatIncompleteCholeskyFactorSymbolic:Missing fact argument"); 1887 if (!mat->ops.incompletecholeskyfactorsymbolic) 1888 SETERRQ(PETSC_ERR_SUP,"MatIncompleteCholeskyFactorSymbolic"); 1889 if (!mat->assembled) 1890 SETERRQ(1,"MatIncompleteCholeskyFactorSymbolic:Not for unassembled matrix"); 1891 1892 PLogEventBegin(MAT_IncompleteCholeskyFactorSymbolic,mat,perm,0,0); 1893 ierr = (*mat->ops.incompletecholeskyfactorsymbolic)(mat,perm,f,fill,fact);CHKERRQ(ierr); 1894 PLogEventEnd(MAT_IncompleteCholeskyFactorSymbolic,mat,perm,0,0); 1895 return 0; 1896 } 1897 1898 /*@C 1899 MatGetArray - Returns a pointer to the element values in the matrix. 1900 This routine is implementation dependent, and may not even work for 1901 certain matrix types. You MUST call MatRestoreArray() when you no 1902 longer need to access the array. 1903 1904 Input Parameter: 1905 . mat - the matrix 1906 1907 Output Parameter: 1908 . v - the location of the values 1909 1910 Fortran Note: 1911 The Fortran interface is slightly different from that given below. 1912 See the Fortran chapter of the users manual and 1913 petsc/src/mat/examples for details. 1914 1915 .keywords: matrix, array, elements, values 1916 1917 .seealso: MatRestoreArray() 1918 @*/ 1919 int MatGetArray(Mat mat,Scalar **v) 1920 { 1921 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1922 if (!v) SETERRQ(1,"MatGetArray:Bad input, array pointer location"); 1923 if (!mat->ops.getarray) SETERRQ(PETSC_ERR_SUP,"MatGetArray"); 1924 return (*mat->ops.getarray)(mat,v); 1925 } 1926 1927 /*@C 1928 MatRestoreArray - Restores the matrix after MatGetArray has been called. 1929 1930 Input Parameter: 1931 . mat - the matrix 1932 . v - the location of the values 1933 1934 Fortran Note: 1935 The Fortran interface is slightly different from that given below. 1936 See the users manual and petsc/src/mat/examples for details. 1937 1938 .keywords: matrix, array, elements, values, resrore 1939 1940 .seealso: MatGetArray() 1941 @*/ 1942 int MatRestoreArray(Mat mat,Scalar **v) 1943 { 1944 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1945 if (!v) SETERRQ(1,"MatRestoreArray:Bad input, array pointer location"); 1946 if (!mat->ops.restorearray) SETERRQ(PETSC_ERR_SUP,"MatResroreArray"); 1947 return (*mat->ops.restorearray)(mat,v); 1948 } 1949 1950 /*@C 1951 MatGetSubMatrices - Extracts several submatrices from a matrix. If submat 1952 points to an array of valid matrices, it may be reused. 1953 1954 Input Parameters: 1955 . mat - the matrix 1956 . irow, icol - index sets of rows and columns to extract 1957 . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 1958 1959 Output Parameter: 1960 . submat - the array of submatrices 1961 1962 Limitations: 1963 Currently, MatGetSubMatrices() can extract only sequential submatrices 1964 (from both sequential and parallel matrices). 1965 1966 Notes: 1967 When extracting submatrices from a parallel matrix, each processor can 1968 form a different submatrix by setting the rows and columns of its 1969 individual index sets according to the local submatrix desired. 1970 1971 When finished using the submatrices, the user should destroy 1972 them with MatDestroySubMatrices(). 1973 1974 .keywords: matrix, get, submatrix, submatrices 1975 1976 .seealso: MatDestroyMatrices() 1977 @*/ 1978 int MatGetSubMatrices(Mat mat,int n,IS *irow,IS *icol,MatGetSubMatrixCall scall, 1979 Mat **submat) 1980 { 1981 int ierr; 1982 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1983 if (!mat->ops.getsubmatrices) SETERRQ(PETSC_ERR_SUP,"MatGetSubMatrices"); 1984 if (!mat->assembled) SETERRQ(1,"MatGetSubMatrices:Not for unassembled matrix"); 1985 1986 PLogEventBegin(MAT_GetSubMatrices,mat,0,0,0); 1987 ierr = (*mat->ops.getsubmatrices)(mat,n,irow,icol,scall,submat); CHKERRQ(ierr); 1988 PLogEventEnd(MAT_GetSubMatrices,mat,0,0,0); 1989 1990 return 0; 1991 } 1992 1993 /*@C 1994 MatDestroyMatrices - Destroys a set of matrices obtained with MatGetSubMatrices(). 1995 1996 Input Parameters: 1997 . n - the number of local matrices 1998 . mat - the matrices 1999 2000 .keywords: matrix, destroy, submatrix, submatrices 2001 2002 .seealso: MatGetSubMatrices() 2003 @*/ 2004 int MatDestroyMatrices(int n,Mat **mat) 2005 { 2006 int ierr,i; 2007 2008 PetscValidPointer(mat); 2009 for ( i=0; i<n; i++ ) { 2010 ierr = MatDestroy((*mat)[i]); CHKERRQ(ierr); 2011 } 2012 PetscFree(*mat); 2013 return 0; 2014 } 2015 2016 /*@ 2017 MatIncreaseOverlap - Given a set of submatrices indicated by index sets, 2018 replaces the index by larger ones that represent submatrices with more 2019 overlap. 2020 2021 Input Parameters: 2022 . mat - the matrix 2023 . n - the number of index sets 2024 . is - the array of pointers to index sets 2025 . ov - the additional overlap requested 2026 2027 .keywords: matrix, overlap, Schwarz 2028 2029 .seealso: MatGetSubMatrices() 2030 @*/ 2031 int MatIncreaseOverlap(Mat mat,int n, IS *is,int ov) 2032 { 2033 int ierr; 2034 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2035 if (!mat->assembled) SETERRQ(1,"MatIncreaseOverlap:Not for unassembled matrix"); 2036 if (mat->factor) SETERRQ(1,"MatIncreaseOverlap:Not for factored matrix"); 2037 2038 if (ov == 0) return 0; 2039 if (!mat->ops.increaseoverlap) SETERRQ(PETSC_ERR_SUP,"MatIncreaseOverlap"); 2040 PLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0); 2041 ierr = (*mat->ops.increaseoverlap)(mat,n,is,ov); CHKERRQ(ierr); 2042 PLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0); 2043 return 0; 2044 } 2045 2046 /*@ 2047 MatPrintHelp - Prints all the options for the matrix. 2048 2049 Input Parameter: 2050 . mat - the matrix 2051 2052 Options Database Keys: 2053 $ -help, -h 2054 2055 .keywords: mat, help 2056 2057 .seealso: MatCreate(), MatCreateXXX() 2058 @*/ 2059 int MatPrintHelp(Mat mat) 2060 { 2061 static int called = 0; 2062 MPI_Comm comm; 2063 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2064 2065 comm = mat->comm; 2066 if (!called) { 2067 PetscPrintf(comm,"General matrix options:\n"); 2068 PetscPrintf(comm," -mat_view_info : view basic matrix info during MatAssemblyEnd()\n"); 2069 PetscPrintf(comm," -mat_view_info_detailed : view detailed matrix info during MatAssemblyEnd()\n"); 2070 PetscPrintf(comm," -mat_view_draw : draw nonzero matrix structure during MatAssemblyEnd()\n"); 2071 PetscPrintf(comm," -draw_pause <sec> : set seconds of display pause\n"); 2072 PetscPrintf(comm," -display <name> : set alternate display\n"); 2073 called = 1; 2074 } 2075 if (mat->ops.printhelp) (*mat->ops.printhelp)(mat); 2076 return 0; 2077 } 2078 2079 /*@ 2080 MatGetBlockSize - Returns the matrix block size; useful especially for the 2081 block row and block diagonal formats. 2082 2083 Input Parameter: 2084 . mat - the matrix 2085 2086 Output Parameter: 2087 . bs - block size 2088 2089 Notes: 2090 $ block diagonal formats: MATSEQBDIAG, MATMPIBDIAG 2091 $ block row formats: MATSEQBAIJ, MATMPIBAIJ 2092 2093 .keywords: matrix, get, block, size 2094 2095 .seealso: MatCreateSeqBAIJ(), MatCreateMPIBAIJ(), MatCreateSeqBDiag(), MatCreateMPIBDiag() 2096 @*/ 2097 int MatGetBlockSize(Mat mat,int *bs) 2098 { 2099 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2100 PetscValidIntPointer(bs); 2101 if (!mat->ops.getblocksize) SETERRQ(PETSC_ERR_SUP,"MatGetBlockSize"); 2102 return (*mat->ops.getblocksize)(mat,bs); 2103 } 2104 2105 /*@C 2106 MatGetRowIJ - Returns the compress row storage i and j indices for sequential matrices. 2107 EXPERTS ONLY. 2108 2109 Input Parameters: 2110 . mat - the matrix 2111 . shift - 1 or zero indicating we want the indices starting at 0 or 1 2112 . symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 2113 symmetrized 2114 2115 Output Parameters: 2116 . n - number of rows and columns in the (possibly compressed) matrix 2117 . ia - the row indices 2118 . ja - the column indices 2119 . done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned 2120 @*/ 2121 int MatGetRowIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int **ia,int** ja,PetscTruth *done) 2122 { 2123 int ierr; 2124 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2125 if (ia) PetscValidIntPointer(ia); 2126 if (ja) PetscValidIntPointer(ja); 2127 PetscValidIntPointer(done); 2128 if (!mat->ops.getrowij) *done = PETSC_FALSE; 2129 else { 2130 *done = PETSC_TRUE; 2131 ierr = (*mat->ops.getrowij)(mat,shift,symmetric,n,ia,ja,done); CHKERRQ(ierr); 2132 } 2133 return 0; 2134 } 2135 2136 /*@C 2137 MatGetColumnIJ - Returns the compress Column storage i and j indices for sequential matrices. 2138 EXPERTS ONLY. 2139 2140 Input Parameters: 2141 . mat - the matrix 2142 . shift - 1 or zero indicating we want the indices starting at 0 or 1 2143 . symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 2144 symmetrized 2145 2146 Output Parameters: 2147 . n - number of Columns and columns in the (possibly compressed) matrix 2148 . ia - the Column indices 2149 . ja - the column indices 2150 . done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned 2151 @*/ 2152 int MatGetColumnIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int **ia,int** ja,PetscTruth *done) 2153 { 2154 int ierr; 2155 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2156 if (ia) PetscValidIntPointer(ia); 2157 if (ja) PetscValidIntPointer(ja); 2158 PetscValidIntPointer(done); 2159 2160 if (!mat->ops.getcolumnij) *done = PETSC_FALSE; 2161 else { 2162 *done = PETSC_TRUE; 2163 ierr = (*mat->ops.getcolumnij)(mat,shift,symmetric,n,ia,ja,done); CHKERRQ(ierr); 2164 } 2165 return 0; 2166 } 2167 2168 /*@C 2169 MatRestoreRowIJ - Call after you are completed with the ia,ja indices obtained with 2170 MatGetRowIJ(). EXPERTS ONLY. 2171 2172 Input Parameters: 2173 . mat - the matrix 2174 . shift - 1 or zero indicating we want the indices starting at 0 or 1 2175 . symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 2176 symmetrized 2177 2178 Output Parameters: 2179 . n - size of (possibly compressed) matrix 2180 . ia - the row indices 2181 . ja - the column indices 2182 . done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned 2183 @*/ 2184 int MatRestoreRowIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int **ia,int** ja,PetscTruth *done) 2185 { 2186 int ierr; 2187 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2188 if (ia) PetscValidIntPointer(ia); 2189 if (ja) PetscValidIntPointer(ja); 2190 PetscValidIntPointer(done); 2191 2192 if (!mat->ops.restorerowij) *done = PETSC_FALSE; 2193 else { 2194 *done = PETSC_TRUE; 2195 ierr = (*mat->ops.restorerowij)(mat,shift,symmetric,n,ia,ja,done); CHKERRQ(ierr); 2196 } 2197 return 0; 2198 } 2199 2200 /*@C 2201 MatRestoreColumnIJ - Call after you are completed with the ia,ja indices obtained with 2202 MatGetColumnIJ(). EXPERTS ONLY. 2203 2204 Input Parameters: 2205 . mat - the matrix 2206 . shift - 1 or zero indicating we want the indices starting at 0 or 1 2207 . symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 2208 symmetrized 2209 2210 Output Parameters: 2211 . n - size of (possibly compressed) matrix 2212 . ia - the Column indices 2213 . ja - the column indices 2214 . done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned 2215 @*/ 2216 int MatRestoreColumnIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int **ia,int** ja,PetscTruth *done) 2217 { 2218 int ierr; 2219 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2220 if (ia) PetscValidIntPointer(ia); 2221 if (ja) PetscValidIntPointer(ja); 2222 PetscValidIntPointer(done); 2223 2224 if (!mat->ops.restorecolumnij) *done = PETSC_FALSE; 2225 else { 2226 *done = PETSC_TRUE; 2227 ierr = (*mat->ops.restorecolumnij)(mat,shift,symmetric,n,ia,ja,done); CHKERRQ(ierr); 2228 } 2229 return 0; 2230 } 2231 2232 /*@C 2233 MatColoringPatch - EXPERTS ONLY, used inside matrix coloring routines that 2234 use matGetRowIJ() and/or MatGetColumnIJ(). 2235 2236 Input Parameters: 2237 . mat - the matrix 2238 . n - number of colors 2239 . colorarray - array indicating color for each column 2240 2241 Output Parameters: 2242 . iscoloring - coloring generated using colorarray information 2243 2244 @*/ 2245 int MatColoringPatch(Mat mat,int n,int *colorarray,ISColoring *iscoloring) 2246 { 2247 int ierr; 2248 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2249 PetscValidIntPointer(colorarray); 2250 2251 if (!mat->ops.coloringpatch) {SETERRQ(PETSC_ERR_SUP,"MatColoringPatch");} 2252 else { 2253 ierr = (*mat->ops.coloringpatch)(mat,n,colorarray,iscoloring); CHKERRQ(ierr); 2254 } 2255 return 0; 2256 } 2257 2258 2259