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