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