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