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