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