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