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