xref: /petsc/src/mat/interface/matrix.c (revision bab1f7e6b50cd785a0b0b97eb05bc2a37568c746)
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(), MatZeroRowsStencil(), 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(), MatZeroRowsStencil(), 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__ "MatZeroRowsStencil"
5177 /*@C
5178    MatZeroRowsStencil - Zeros all entries (except possibly the main diagonal)
5179    of a set of rows of a matrix.
5180 
5181    Collective on Mat
5182 
5183    Input Parameters:
5184 +  mat - the matrix
5185 .  numRows - the number of rows to remove
5186 .  rows - the grid coordinates (and component number when dof > 1) for matrix rows
5187 -  diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry)
5188 
5189    Notes:
5190    For the AIJ and BAIJ matrix formats this removes the old nonzero structure,
5191    but does not release memory.  For the dense and block diagonal
5192    formats this does not alter the nonzero structure.
5193 
5194    If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure
5195    of the matrix is not changed (even for AIJ and BAIJ matrices) the values are
5196    merely zeroed.
5197 
5198    The user can set a value in the diagonal entry (or for the AIJ and
5199    row formats can optionally remove the main diagonal entry from the
5200    nonzero structure as well, by passing 0.0 as the final argument).
5201 
5202    For the parallel case, all processes that share the matrix (i.e.,
5203    those in the communicator used for matrix creation) MUST call this
5204    routine, regardless of whether any rows being zeroed are owned by
5205    them.
5206 
5207    Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to
5208    list only rows local to itself).
5209 
5210    The grid coordinates are across the entire grid, not just the local portion
5211 
5212    In Fortran idxm and idxn should be declared as
5213 $     MatStencil idxm(4,m)
5214    and the values inserted using
5215 $    idxm(MatStencil_i,1) = i
5216 $    idxm(MatStencil_j,1) = j
5217 $    idxm(MatStencil_k,1) = k
5218 $    idxm(MatStencil_c,1) = c
5219    etc
5220 
5221    For periodic boundary conditions use negative indices for values to the left (below 0; that are to be
5222    obtained by wrapping values from right edge). For values to the right of the last entry using that index plus one
5223    etc to obtain values that obtained by wrapping the values from the left edge. This does not work for the DA_NONPERIODIC
5224    wrap.
5225 
5226    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
5227    a single value per point) you can skip filling those indices.
5228 
5229    Level: intermediate
5230 
5231    Concepts: matrices^zeroing rows
5232 
5233 .seealso: MatZeroRows(), MatZeroRowsIS(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption()
5234 @*/
5235 PetscErrorCode PETSCMAT_DLLEXPORT MatZeroRowsStencil(Mat mat,PetscInt numRows,const MatStencil rows[],PetscScalar diag)
5236 {
5237   PetscInt       dim    = mat->stencil.dim;
5238   PetscInt       sdim   = dim - (1 - (PetscInt) mat->stencil.noc);
5239   PetscInt      *dims   = mat->stencil.dims+1;
5240   PetscInt      *starts = mat->stencil.starts;
5241   PetscInt      *dxm    = (PetscInt *) rows;
5242   PetscInt      *jdxm, i, j, tmp;
5243   PetscErrorCode ierr;
5244 
5245   PetscFunctionBegin;
5246   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5247   PetscValidType(mat,1);
5248   if (numRows) PetscValidIntPointer(rows,3);
5249 
5250   ierr = PetscMalloc(numRows * sizeof(PetscInt), &jdxm);CHKERRQ(ierr);
5251   for(i = 0; i < numRows; ++i) {
5252     for(j = 0; j < 3-sdim; ++j) dxm++;
5253     tmp = *dxm++ - starts[0];
5254     for(j = 0; j < dim-1; ++j) {
5255       if ((*dxm++ - starts[j+1]) < 0 || tmp < 0) tmp = PETSC_MIN_INT;
5256       else                                       tmp = tmp*dims[j] + *(dxm-1) - starts[j+1];
5257     }
5258     if (mat->stencil.noc) dxm++;
5259     jdxm[i] = tmp;
5260   }
5261   ierr = MatZeroRowsLocal(mat,numRows,jdxm,diag);CHKERRQ(ierr);
5262   ierr = PetscFree(jdxm);CHKERRQ(ierr);
5263   PetscFunctionReturn(0);
5264 }
5265 
5266 #undef __FUNCT__
5267 #define __FUNCT__ "MatZeroRowsLocal"
5268 /*@C
5269    MatZeroRowsLocal - Zeros all entries (except possibly the main diagonal)
5270    of a set of rows of a matrix; using local numbering of rows.
5271 
5272    Logically Collective on Mat
5273 
5274    Input Parameters:
5275 +  mat - the matrix
5276 .  numRows - the number of rows to remove
5277 .  rows - the global row indices
5278 -  diag - value put in all diagonals of eliminated rows
5279 
5280    Notes:
5281    Before calling MatZeroRowsLocal(), the user must first set the
5282    local-to-global mapping by calling MatSetLocalToGlobalMapping().
5283 
5284    For the AIJ matrix formats this removes the old nonzero structure,
5285    but does not release memory.  For the dense and block diagonal
5286    formats this does not alter the nonzero structure.
5287 
5288    If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure
5289    of the matrix is not changed (even for AIJ and BAIJ matrices) the values are
5290    merely zeroed.
5291 
5292    The user can set a value in the diagonal entry (or for the AIJ and
5293    row formats can optionally remove the main diagonal entry from the
5294    nonzero structure as well, by passing 0.0 as the final argument).
5295 
5296    Level: intermediate
5297 
5298    Concepts: matrices^zeroing
5299 
5300 .seealso: MatZeroRows(), MatZeroRowsLocalIS(), MatZeroEntries(), MatZeroRows(), MatSetLocalToGlobalMapping
5301 @*/
5302 PetscErrorCode PETSCMAT_DLLEXPORT MatZeroRowsLocal(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag)
5303 {
5304   PetscErrorCode ierr;
5305 
5306   PetscFunctionBegin;
5307   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5308   PetscValidType(mat,1);
5309   if (numRows) PetscValidIntPointer(rows,3);
5310   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
5311   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
5312   ierr = MatPreallocated(mat);CHKERRQ(ierr);
5313 
5314   if (mat->ops->zerorowslocal) {
5315     ierr = (*mat->ops->zerorowslocal)(mat,numRows,rows,diag);CHKERRQ(ierr);
5316   } else {
5317     IS             is, newis;
5318     const PetscInt *newRows;
5319 
5320     if (!mat->mapping) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide local to global mapping to matrix first");
5321     ierr = ISCreateGeneral(PETSC_COMM_SELF,numRows,rows,&is);CHKERRQ(ierr);
5322     ierr = ISLocalToGlobalMappingApplyIS(mat->mapping,is,&newis);CHKERRQ(ierr);
5323     ierr = ISGetIndices(newis,&newRows);CHKERRQ(ierr);
5324     ierr = (*mat->ops->zerorows)(mat,numRows,newRows,diag);CHKERRQ(ierr);
5325     ierr = ISRestoreIndices(newis,&newRows);CHKERRQ(ierr);
5326     ierr = ISDestroy(newis);CHKERRQ(ierr);
5327     ierr = ISDestroy(is);CHKERRQ(ierr);
5328   }
5329   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
5330   PetscFunctionReturn(0);
5331 }
5332 
5333 #undef __FUNCT__
5334 #define __FUNCT__ "MatZeroRowsLocalIS"
5335 /*@C
5336    MatZeroRowsLocalIS - Zeros all entries (except possibly the main diagonal)
5337    of a set of rows of a matrix; using local numbering of rows.
5338 
5339    Logically Collective on Mat
5340 
5341    Input Parameters:
5342 +  mat - the matrix
5343 .  is - index set of rows to remove
5344 -  diag - value put in all diagonals of eliminated rows
5345 
5346    Notes:
5347    Before calling MatZeroRowsLocalIS(), the user must first set the
5348    local-to-global mapping by calling MatSetLocalToGlobalMapping().
5349 
5350    For the AIJ matrix formats this removes the old nonzero structure,
5351    but does not release memory.  For the dense and block diagonal
5352    formats this does not alter the nonzero structure.
5353 
5354    If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure
5355    of the matrix is not changed (even for AIJ and BAIJ matrices) the values are
5356    merely zeroed.
5357 
5358    The user can set a value in the diagonal entry (or for the AIJ and
5359    row formats can optionally remove the main diagonal entry from the
5360    nonzero structure as well, by passing 0.0 as the final argument).
5361 
5362    Level: intermediate
5363 
5364    Concepts: matrices^zeroing
5365 
5366 .seealso: MatZeroRows(), MatZeroRowsLocal(), MatZeroEntries(), MatZeroRows(), MatSetLocalToGlobalMapping
5367 @*/
5368 PetscErrorCode PETSCMAT_DLLEXPORT MatZeroRowsLocalIS(Mat mat,IS is,PetscScalar diag)
5369 {
5370   PetscErrorCode ierr;
5371   PetscInt       numRows;
5372   const PetscInt *rows;
5373 
5374   PetscFunctionBegin;
5375   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5376   PetscValidType(mat,1);
5377   PetscValidHeaderSpecific(is,IS_CLASSID,2);
5378   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
5379   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
5380   ierr = MatPreallocated(mat);CHKERRQ(ierr);
5381 
5382   ierr = ISGetLocalSize(is,&numRows);CHKERRQ(ierr);
5383   ierr = ISGetIndices(is,&rows);CHKERRQ(ierr);
5384   ierr = MatZeroRowsLocal(mat,numRows,rows,diag);CHKERRQ(ierr);
5385   ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr);
5386   PetscFunctionReturn(0);
5387 }
5388 
5389 #undef __FUNCT__
5390 #define __FUNCT__ "MatGetSize"
5391 /*@
5392    MatGetSize - Returns the numbers of rows and columns in a matrix.
5393 
5394    Not Collective
5395 
5396    Input Parameter:
5397 .  mat - the matrix
5398 
5399    Output Parameters:
5400 +  m - the number of global rows
5401 -  n - the number of global columns
5402 
5403    Note: both output parameters can be PETSC_NULL on input.
5404 
5405    Level: beginner
5406 
5407    Concepts: matrices^size
5408 
5409 .seealso: MatGetLocalSize()
5410 @*/
5411 PetscErrorCode PETSCMAT_DLLEXPORT MatGetSize(Mat mat,PetscInt *m,PetscInt* n)
5412 {
5413   PetscFunctionBegin;
5414   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5415   if (m) *m = mat->rmap->N;
5416   if (n) *n = mat->cmap->N;
5417   PetscFunctionReturn(0);
5418 }
5419 
5420 #undef __FUNCT__
5421 #define __FUNCT__ "MatGetLocalSize"
5422 /*@
5423    MatGetLocalSize - Returns the number of rows and columns in a matrix
5424    stored locally.  This information may be implementation dependent, so
5425    use with care.
5426 
5427    Not Collective
5428 
5429    Input Parameters:
5430 .  mat - the matrix
5431 
5432    Output Parameters:
5433 +  m - the number of local rows
5434 -  n - the number of local columns
5435 
5436    Note: both output parameters can be PETSC_NULL on input.
5437 
5438    Level: beginner
5439 
5440    Concepts: matrices^local size
5441 
5442 .seealso: MatGetSize()
5443 @*/
5444 PetscErrorCode PETSCMAT_DLLEXPORT MatGetLocalSize(Mat mat,PetscInt *m,PetscInt* n)
5445 {
5446   PetscFunctionBegin;
5447   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5448   if (m) PetscValidIntPointer(m,2);
5449   if (n) PetscValidIntPointer(n,3);
5450   if (m) *m = mat->rmap->n;
5451   if (n) *n = mat->cmap->n;
5452   PetscFunctionReturn(0);
5453 }
5454 
5455 #undef __FUNCT__
5456 #define __FUNCT__ "MatGetOwnershipRangeColumn"
5457 /*@
5458    MatGetOwnershipRangeColumn - Returns the range of matrix columns owned by
5459    this processor.
5460 
5461    Not Collective, unless matrix has not been allocated, then collective on Mat
5462 
5463    Input Parameters:
5464 .  mat - the matrix
5465 
5466    Output Parameters:
5467 +  m - the global index of the first local column
5468 -  n - one more than the global index of the last local column
5469 
5470    Notes: both output parameters can be PETSC_NULL on input.
5471 
5472    Level: developer
5473 
5474    Concepts: matrices^column ownership
5475 
5476 .seealso:  MatGetOwnershipRange(), MatGetOwnershipRanges(), MatGetOwnershipRangesColumn()
5477 
5478 @*/
5479 PetscErrorCode PETSCMAT_DLLEXPORT MatGetOwnershipRangeColumn(Mat mat,PetscInt *m,PetscInt* n)
5480 {
5481   PetscErrorCode ierr;
5482 
5483   PetscFunctionBegin;
5484   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5485   PetscValidType(mat,1);
5486   if (m) PetscValidIntPointer(m,2);
5487   if (n) PetscValidIntPointer(n,3);
5488   ierr = MatPreallocated(mat);CHKERRQ(ierr);
5489   if (m) *m = mat->cmap->rstart;
5490   if (n) *n = mat->cmap->rend;
5491   PetscFunctionReturn(0);
5492 }
5493 
5494 #undef __FUNCT__
5495 #define __FUNCT__ "MatGetOwnershipRange"
5496 /*@
5497    MatGetOwnershipRange - Returns the range of matrix rows owned by
5498    this processor, assuming that the matrix is laid out with the first
5499    n1 rows on the first processor, the next n2 rows on the second, etc.
5500    For certain parallel layouts this range may not be well defined.
5501 
5502    Not Collective, unless matrix has not been allocated, then collective on Mat
5503 
5504    Input Parameters:
5505 .  mat - the matrix
5506 
5507    Output Parameters:
5508 +  m - the global index of the first local row
5509 -  n - one more than the global index of the last local row
5510 
5511    Note: both output parameters can be PETSC_NULL on input.
5512 
5513    Level: beginner
5514 
5515    Concepts: matrices^row ownership
5516 
5517 .seealso:   MatGetOwnershipRanges(), MatGetOwnershipRangeColumn(), MatGetOwnershipRangesColumn()
5518 
5519 @*/
5520 PetscErrorCode PETSCMAT_DLLEXPORT MatGetOwnershipRange(Mat mat,PetscInt *m,PetscInt* n)
5521 {
5522   PetscErrorCode ierr;
5523 
5524   PetscFunctionBegin;
5525   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5526   PetscValidType(mat,1);
5527   if (m) PetscValidIntPointer(m,2);
5528   if (n) PetscValidIntPointer(n,3);
5529   ierr = MatPreallocated(mat);CHKERRQ(ierr);
5530   if (m) *m = mat->rmap->rstart;
5531   if (n) *n = mat->rmap->rend;
5532   PetscFunctionReturn(0);
5533 }
5534 
5535 #undef __FUNCT__
5536 #define __FUNCT__ "MatGetOwnershipRanges"
5537 /*@C
5538    MatGetOwnershipRanges - Returns the range of matrix rows owned by
5539    each process
5540 
5541    Not Collective, unless matrix has not been allocated, then collective on Mat
5542 
5543    Input Parameters:
5544 .  mat - the matrix
5545 
5546    Output Parameters:
5547 .  ranges - start of each processors portion plus one more then the total length at the end
5548 
5549    Level: beginner
5550 
5551    Concepts: matrices^row ownership
5552 
5553 .seealso:   MatGetOwnershipRange(), MatGetOwnershipRangeColumn(), MatGetOwnershipRangesColumn()
5554 
5555 @*/
5556 PetscErrorCode PETSCMAT_DLLEXPORT MatGetOwnershipRanges(Mat mat,const PetscInt **ranges)
5557 {
5558   PetscErrorCode ierr;
5559 
5560   PetscFunctionBegin;
5561   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5562   PetscValidType(mat,1);
5563   ierr = MatPreallocated(mat);CHKERRQ(ierr);
5564   ierr = PetscLayoutGetRanges(mat->rmap,ranges);CHKERRQ(ierr);
5565   PetscFunctionReturn(0);
5566 }
5567 
5568 #undef __FUNCT__
5569 #define __FUNCT__ "MatGetOwnershipRangesColumn"
5570 /*@C
5571    MatGetOwnershipRangesColumn - Returns the range of local columns for each process
5572 
5573    Not Collective, unless matrix has not been allocated, then collective on Mat
5574 
5575    Input Parameters:
5576 .  mat - the matrix
5577 
5578    Output Parameters:
5579 .  ranges - start of each processors portion plus one more then the total length at the end
5580 
5581    Level: beginner
5582 
5583    Concepts: matrices^column ownership
5584 
5585 .seealso:   MatGetOwnershipRange(), MatGetOwnershipRangeColumn(), MatGetOwnershipRanges()
5586 
5587 @*/
5588 PetscErrorCode PETSCMAT_DLLEXPORT MatGetOwnershipRangesColumn(Mat mat,const PetscInt **ranges)
5589 {
5590   PetscErrorCode ierr;
5591 
5592   PetscFunctionBegin;
5593   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5594   PetscValidType(mat,1);
5595   ierr = MatPreallocated(mat);CHKERRQ(ierr);
5596   ierr = PetscLayoutGetRanges(mat->cmap,ranges);CHKERRQ(ierr);
5597   PetscFunctionReturn(0);
5598 }
5599 
5600 #undef __FUNCT__
5601 #define __FUNCT__ "MatILUFactorSymbolic"
5602 /*@C
5603    MatILUFactorSymbolic - Performs symbolic ILU factorization of a matrix.
5604    Uses levels of fill only, not drop tolerance. Use MatLUFactorNumeric()
5605    to complete the factorization.
5606 
5607    Collective on Mat
5608 
5609    Input Parameters:
5610 +  mat - the matrix
5611 .  row - row permutation
5612 .  column - column permutation
5613 -  info - structure containing
5614 $      levels - number of levels of fill.
5615 $      expected fill - as ratio of original fill.
5616 $      1 or 0 - indicating force fill on diagonal (improves robustness for matrices
5617                 missing diagonal entries)
5618 
5619    Output Parameters:
5620 .  fact - new matrix that has been symbolically factored
5621 
5622    Notes:
5623    See the users manual for additional information about
5624    choosing the fill factor for better efficiency.
5625 
5626    Most users should employ the simplified KSP interface for linear solvers
5627    instead of working directly with matrix algebra routines such as this.
5628    See, e.g., KSPCreate().
5629 
5630    Level: developer
5631 
5632   Concepts: matrices^symbolic LU factorization
5633   Concepts: matrices^factorization
5634   Concepts: LU^symbolic factorization
5635 
5636 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor()
5637           MatGetOrdering(), MatFactorInfo
5638 
5639     Developer Note: fortran interface is not autogenerated as the f90
5640     interface defintion cannot be generated correctly [due to MatFactorInfo]
5641 
5642 @*/
5643 PetscErrorCode PETSCMAT_DLLEXPORT MatILUFactorSymbolic(Mat fact,Mat mat,IS row,IS col,const MatFactorInfo *info)
5644 {
5645   PetscErrorCode ierr;
5646 
5647   PetscFunctionBegin;
5648   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5649   PetscValidType(mat,1);
5650   PetscValidHeaderSpecific(row,IS_CLASSID,2);
5651   PetscValidHeaderSpecific(col,IS_CLASSID,3);
5652   PetscValidPointer(info,4);
5653   PetscValidPointer(fact,5);
5654   if (info->levels < 0) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Levels of fill negative %D",(PetscInt)info->levels);
5655   if (info->fill < 1.0) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %G",info->fill);
5656   if (!(fact)->ops->ilufactorsymbolic) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Matrix type %s  symbolic ILU",((PetscObject)mat)->type_name);
5657   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
5658   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
5659   ierr = MatPreallocated(mat);CHKERRQ(ierr);
5660 
5661   ierr = PetscLogEventBegin(MAT_ILUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr);
5662   ierr = (fact->ops->ilufactorsymbolic)(fact,mat,row,col,info);CHKERRQ(ierr);
5663   ierr = PetscLogEventEnd(MAT_ILUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr);
5664   PetscFunctionReturn(0);
5665 }
5666 
5667 #undef __FUNCT__
5668 #define __FUNCT__ "MatICCFactorSymbolic"
5669 /*@C
5670    MatICCFactorSymbolic - Performs symbolic incomplete
5671    Cholesky factorization for a symmetric matrix.  Use
5672    MatCholeskyFactorNumeric() to complete the factorization.
5673 
5674    Collective on Mat
5675 
5676    Input Parameters:
5677 +  mat - the matrix
5678 .  perm - row and column permutation
5679 -  info - structure containing
5680 $      levels - number of levels of fill.
5681 $      expected fill - as ratio of original fill.
5682 
5683    Output Parameter:
5684 .  fact - the factored matrix
5685 
5686    Notes:
5687    Most users should employ the KSP interface for linear solvers
5688    instead of working directly with matrix algebra routines such as this.
5689    See, e.g., KSPCreate().
5690 
5691    Level: developer
5692 
5693   Concepts: matrices^symbolic incomplete Cholesky factorization
5694   Concepts: matrices^factorization
5695   Concepts: Cholsky^symbolic factorization
5696 
5697 .seealso: MatCholeskyFactorNumeric(), MatCholeskyFactor(), MatFactorInfo
5698 
5699     Developer Note: fortran interface is not autogenerated as the f90
5700     interface defintion cannot be generated correctly [due to MatFactorInfo]
5701 
5702 @*/
5703 PetscErrorCode PETSCMAT_DLLEXPORT MatICCFactorSymbolic(Mat fact,Mat mat,IS perm,const MatFactorInfo *info)
5704 {
5705   PetscErrorCode ierr;
5706 
5707   PetscFunctionBegin;
5708   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5709   PetscValidType(mat,1);
5710   PetscValidHeaderSpecific(perm,IS_CLASSID,2);
5711   PetscValidPointer(info,3);
5712   PetscValidPointer(fact,4);
5713   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
5714   if (info->levels < 0) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Levels negative %D",(PetscInt) info->levels);
5715   if (info->fill < 1.0) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %G",info->fill);
5716   if (!(fact)->ops->iccfactorsymbolic) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Matrix type %s  symbolic ICC",((PetscObject)mat)->type_name);
5717   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
5718   ierr = MatPreallocated(mat);CHKERRQ(ierr);
5719 
5720   ierr = PetscLogEventBegin(MAT_ICCFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr);
5721   ierr = (fact->ops->iccfactorsymbolic)(fact,mat,perm,info);CHKERRQ(ierr);
5722   ierr = PetscLogEventEnd(MAT_ICCFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr);
5723   PetscFunctionReturn(0);
5724 }
5725 
5726 #undef __FUNCT__
5727 #define __FUNCT__ "MatGetArray"
5728 /*@C
5729    MatGetArray - Returns a pointer to the element values in the matrix.
5730    The result of this routine is dependent on the underlying matrix data
5731    structure, and may not even work for certain matrix types.  You MUST
5732    call MatRestoreArray() when you no longer need to access the array.
5733 
5734    Not Collective
5735 
5736    Input Parameter:
5737 .  mat - the matrix
5738 
5739    Output Parameter:
5740 .  v - the location of the values
5741 
5742 
5743    Fortran Note:
5744    This routine is used differently from Fortran, e.g.,
5745 .vb
5746         Mat         mat
5747         PetscScalar mat_array(1)
5748         PetscOffset i_mat
5749         PetscErrorCode ierr
5750         call MatGetArray(mat,mat_array,i_mat,ierr)
5751 
5752   C  Access first local entry in matrix; note that array is
5753   C  treated as one dimensional
5754         value = mat_array(i_mat + 1)
5755 
5756         [... other code ...]
5757         call MatRestoreArray(mat,mat_array,i_mat,ierr)
5758 .ve
5759 
5760    See the Fortran chapter of the users manual and
5761    petsc/src/mat/examples/tests for details.
5762 
5763    Level: advanced
5764 
5765    Concepts: matrices^access array
5766 
5767 .seealso: MatRestoreArray(), MatGetArrayF90(), MatGetRowIJ()
5768 @*/
5769 PetscErrorCode PETSCMAT_DLLEXPORT MatGetArray(Mat mat,PetscScalar *v[])
5770 {
5771   PetscErrorCode ierr;
5772 
5773   PetscFunctionBegin;
5774   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5775   PetscValidType(mat,1);
5776   PetscValidPointer(v,2);
5777   if (!mat->ops->getarray) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
5778   ierr = MatPreallocated(mat);CHKERRQ(ierr);
5779   ierr = (*mat->ops->getarray)(mat,v);CHKERRQ(ierr);
5780   CHKMEMQ;
5781   PetscFunctionReturn(0);
5782 }
5783 
5784 #undef __FUNCT__
5785 #define __FUNCT__ "MatRestoreArray"
5786 /*@C
5787    MatRestoreArray - Restores the matrix after MatGetArray() has been called.
5788 
5789    Not Collective
5790 
5791    Input Parameter:
5792 +  mat - the matrix
5793 -  v - the location of the values
5794 
5795    Fortran Note:
5796    This routine is used differently from Fortran, e.g.,
5797 .vb
5798         Mat         mat
5799         PetscScalar mat_array(1)
5800         PetscOffset i_mat
5801         PetscErrorCode ierr
5802         call MatGetArray(mat,mat_array,i_mat,ierr)
5803 
5804   C  Access first local entry in matrix; note that array is
5805   C  treated as one dimensional
5806         value = mat_array(i_mat + 1)
5807 
5808         [... other code ...]
5809         call MatRestoreArray(mat,mat_array,i_mat,ierr)
5810 .ve
5811 
5812    See the Fortran chapter of the users manual and
5813    petsc/src/mat/examples/tests for details
5814 
5815    Level: advanced
5816 
5817 .seealso: MatGetArray(), MatRestoreArrayF90()
5818 @*/
5819 PetscErrorCode PETSCMAT_DLLEXPORT MatRestoreArray(Mat mat,PetscScalar *v[])
5820 {
5821   PetscErrorCode ierr;
5822 
5823   PetscFunctionBegin;
5824   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5825   PetscValidType(mat,1);
5826   PetscValidPointer(v,2);
5827 #if defined(PETSC_USE_DEBUG)
5828   CHKMEMQ;
5829 #endif
5830   if (!mat->ops->restorearray) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
5831   ierr = (*mat->ops->restorearray)(mat,v);CHKERRQ(ierr);
5832   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
5833   PetscFunctionReturn(0);
5834 }
5835 
5836 #undef __FUNCT__
5837 #define __FUNCT__ "MatGetSubMatrices"
5838 /*@C
5839    MatGetSubMatrices - Extracts several submatrices from a matrix. If submat
5840    points to an array of valid matrices, they may be reused to store the new
5841    submatrices.
5842 
5843    Collective on Mat
5844 
5845    Input Parameters:
5846 +  mat - the matrix
5847 .  n   - the number of submatrixes to be extracted (on this processor, may be zero)
5848 .  irow, icol - index sets of rows and columns to extract
5849 -  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
5850 
5851    Output Parameter:
5852 .  submat - the array of submatrices
5853 
5854    Notes:
5855    MatGetSubMatrices() can extract ONLY sequential submatrices
5856    (from both sequential and parallel matrices). Use MatGetSubMatrix()
5857    to extract a parallel submatrix.
5858 
5859    When extracting submatrices from a parallel matrix, each processor can
5860    form a different submatrix by setting the rows and columns of its
5861    individual index sets according to the local submatrix desired.
5862 
5863    When finished using the submatrices, the user should destroy
5864    them with MatDestroyMatrices().
5865 
5866    MAT_REUSE_MATRIX can only be used when the nonzero structure of the
5867    original matrix has not changed from that last call to MatGetSubMatrices().
5868 
5869    This routine creates the matrices in submat; you should NOT create them before
5870    calling it. It also allocates the array of matrix pointers submat.
5871 
5872    For BAIJ matrices the index sets must respect the block structure, that is if they
5873    request one row/column in a block, they must request all rows/columns that are in
5874    that block. For example, if the block size is 2 you cannot request just row 0 and
5875    column 0.
5876 
5877    Fortran Note:
5878    The Fortran interface is slightly different from that given below; it
5879    requires one to pass in  as submat a Mat (integer) array of size at least m.
5880 
5881    Level: advanced
5882 
5883    Concepts: matrices^accessing submatrices
5884    Concepts: submatrices
5885 
5886 .seealso: MatDestroyMatrices(), MatGetSubMatrix(), MatGetRow(), MatGetDiagonal(), MatReuse
5887 @*/
5888 PetscErrorCode PETSCMAT_DLLEXPORT MatGetSubMatrices(Mat mat,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *submat[])
5889 {
5890   PetscErrorCode ierr;
5891   PetscInt        i;
5892   PetscTruth      eq;
5893 
5894   PetscFunctionBegin;
5895   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5896   PetscValidType(mat,1);
5897   if (n) {
5898     PetscValidPointer(irow,3);
5899     PetscValidHeaderSpecific(*irow,IS_CLASSID,3);
5900     PetscValidPointer(icol,4);
5901     PetscValidHeaderSpecific(*icol,IS_CLASSID,4);
5902   }
5903   PetscValidPointer(submat,6);
5904   if (n && scall == MAT_REUSE_MATRIX) {
5905     PetscValidPointer(*submat,6);
5906     PetscValidHeaderSpecific(**submat,MAT_CLASSID,6);
5907   }
5908   if (!mat->ops->getsubmatrices) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
5909   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
5910   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
5911   ierr = MatPreallocated(mat);CHKERRQ(ierr);
5912 
5913   ierr = PetscLogEventBegin(MAT_GetSubMatrices,mat,0,0,0);CHKERRQ(ierr);
5914   ierr = (*mat->ops->getsubmatrices)(mat,n,irow,icol,scall,submat);CHKERRQ(ierr);
5915   ierr = PetscLogEventEnd(MAT_GetSubMatrices,mat,0,0,0);CHKERRQ(ierr);
5916   for (i=0; i<n; i++) {
5917     if (mat->symmetric || mat->structurally_symmetric || mat->hermitian) {
5918       ierr = ISEqual(irow[i],icol[i],&eq);CHKERRQ(ierr);
5919       if (eq) {
5920 	if (mat->symmetric){
5921 	  ierr = MatSetOption((*submat)[i],MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr);
5922 	} else if (mat->hermitian) {
5923 	  ierr = MatSetOption((*submat)[i],MAT_HERMITIAN,PETSC_TRUE);CHKERRQ(ierr);
5924 	} else if (mat->structurally_symmetric) {
5925 	  ierr = MatSetOption((*submat)[i],MAT_STRUCTURALLY_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr);
5926 	}
5927       }
5928     }
5929   }
5930   PetscFunctionReturn(0);
5931 }
5932 
5933 #undef __FUNCT__
5934 #define __FUNCT__ "MatDestroyMatrices"
5935 /*@C
5936    MatDestroyMatrices - Destroys a set of matrices obtained with MatGetSubMatrices().
5937 
5938    Collective on Mat
5939 
5940    Input Parameters:
5941 +  n - the number of local matrices
5942 -  mat - the matrices (note that this is a pointer to the array of matrices, just to match the calling
5943                        sequence of MatGetSubMatrices())
5944 
5945    Level: advanced
5946 
5947     Notes: Frees not only the matrices, but also the array that contains the matrices
5948            In Fortran will not free the array.
5949 
5950 .seealso: MatGetSubMatrices()
5951 @*/
5952 PetscErrorCode PETSCMAT_DLLEXPORT MatDestroyMatrices(PetscInt n,Mat *mat[])
5953 {
5954   PetscErrorCode ierr;
5955   PetscInt       i;
5956 
5957   PetscFunctionBegin;
5958   if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Trying to destroy negative number of matrices %D",n);
5959   PetscValidPointer(mat,2);
5960   for (i=0; i<n; i++) {
5961     ierr = MatDestroy((*mat)[i]);CHKERRQ(ierr);
5962   }
5963   /* memory is allocated even if n = 0 */
5964   ierr = PetscFree(*mat);CHKERRQ(ierr);
5965   PetscFunctionReturn(0);
5966 }
5967 
5968 #undef __FUNCT__
5969 #define __FUNCT__ "MatGetSeqNonzeroStructure"
5970 /*@C
5971    MatGetSeqNonzeroStructure - Extracts the sequential nonzero structure from a matrix.
5972 
5973    Collective on Mat
5974 
5975    Input Parameters:
5976 .  mat - the matrix
5977 
5978    Output Parameter:
5979 .  matstruct - the sequential matrix with the nonzero structure of mat
5980 
5981   Level: intermediate
5982 
5983 .seealso: MatDestroySeqNonzeroStructure(), MatGetSubMatrices(), MatDestroyMatrices()
5984 @*/
5985 PetscErrorCode PETSCMAT_DLLEXPORT MatGetSeqNonzeroStructure(Mat mat,Mat *matstruct)
5986 {
5987   PetscErrorCode ierr;
5988 
5989   PetscFunctionBegin;
5990   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5991   PetscValidPointer(matstruct,2);
5992 
5993   PetscValidType(mat,1);
5994   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
5995   ierr = MatPreallocated(mat);CHKERRQ(ierr);
5996 
5997   if (!mat->ops->getseqnonzerostructure) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Not for matrix type %s\n",((PetscObject)mat)->type_name);
5998   ierr = PetscLogEventBegin(MAT_GetSeqNonzeroStructure,mat,0,0,0);CHKERRQ(ierr);
5999   ierr = (*mat->ops->getseqnonzerostructure)(mat,matstruct);CHKERRQ(ierr);
6000   ierr = PetscLogEventEnd(MAT_GetSeqNonzeroStructure,mat,0,0,0);CHKERRQ(ierr);
6001   PetscFunctionReturn(0);
6002 }
6003 
6004 #undef __FUNCT__
6005 #define __FUNCT__ "MatDestroySeqNonzeroStructure"
6006 /*@C
6007    MatDestroySeqNonzeroStructure - Destroys matrix obtained with MatGetSeqNonzeroStructure().
6008 
6009    Collective on Mat
6010 
6011    Input Parameters:
6012 .  mat - the matrix (note that this is a pointer to the array of matrices, just to match the calling
6013                        sequence of MatGetSequentialNonzeroStructure())
6014 
6015    Level: advanced
6016 
6017     Notes: Frees not only the matrices, but also the array that contains the matrices
6018 
6019 .seealso: MatGetSeqNonzeroStructure()
6020 @*/
6021 PetscErrorCode PETSCMAT_DLLEXPORT MatDestroySeqNonzeroStructure(Mat *mat)
6022 {
6023   PetscErrorCode ierr;
6024 
6025   PetscFunctionBegin;
6026   PetscValidPointer(mat,1);
6027   ierr = MatDestroy(*mat);CHKERRQ(ierr);
6028   PetscFunctionReturn(0);
6029 }
6030 
6031 #undef __FUNCT__
6032 #define __FUNCT__ "MatIncreaseOverlap"
6033 /*@
6034    MatIncreaseOverlap - Given a set of submatrices indicated by index sets,
6035    replaces the index sets by larger ones that represent submatrices with
6036    additional overlap.
6037 
6038    Collective on Mat
6039 
6040    Input Parameters:
6041 +  mat - the matrix
6042 .  n   - the number of index sets
6043 .  is  - the array of index sets (these index sets will changed during the call)
6044 -  ov  - the additional overlap requested
6045 
6046    Level: developer
6047 
6048    Concepts: overlap
6049    Concepts: ASM^computing overlap
6050 
6051 .seealso: MatGetSubMatrices()
6052 @*/
6053 PetscErrorCode PETSCMAT_DLLEXPORT MatIncreaseOverlap(Mat mat,PetscInt n,IS is[],PetscInt ov)
6054 {
6055   PetscErrorCode ierr;
6056 
6057   PetscFunctionBegin;
6058   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6059   PetscValidType(mat,1);
6060   if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Must have one or more domains, you have %D",n);
6061   if (n) {
6062     PetscValidPointer(is,3);
6063     PetscValidHeaderSpecific(*is,IS_CLASSID,3);
6064   }
6065   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
6066   if (mat->factortype)     SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6067   ierr = MatPreallocated(mat);CHKERRQ(ierr);
6068 
6069   if (!ov) PetscFunctionReturn(0);
6070   if (!mat->ops->increaseoverlap) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
6071   ierr = PetscLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr);
6072   ierr = (*mat->ops->increaseoverlap)(mat,n,is,ov);CHKERRQ(ierr);
6073   ierr = PetscLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr);
6074   PetscFunctionReturn(0);
6075 }
6076 
6077 #undef __FUNCT__
6078 #define __FUNCT__ "MatGetBlockSize"
6079 /*@
6080    MatGetBlockSize - Returns the matrix block size; useful especially for the
6081    block row and block diagonal formats.
6082 
6083    Not Collective
6084 
6085    Input Parameter:
6086 .  mat - the matrix
6087 
6088    Output Parameter:
6089 .  bs - block size
6090 
6091    Notes:
6092    Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ
6093 
6094    Level: intermediate
6095 
6096    Concepts: matrices^block size
6097 
6098 .seealso: MatCreateSeqBAIJ(), MatCreateMPIBAIJ()
6099 @*/
6100 PetscErrorCode PETSCMAT_DLLEXPORT MatGetBlockSize(Mat mat,PetscInt *bs)
6101 {
6102   PetscErrorCode ierr;
6103 
6104   PetscFunctionBegin;
6105   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6106   PetscValidType(mat,1);
6107   PetscValidIntPointer(bs,2);
6108   ierr = MatPreallocated(mat);CHKERRQ(ierr);
6109   *bs = mat->rmap->bs;
6110   PetscFunctionReturn(0);
6111 }
6112 
6113 #undef __FUNCT__
6114 #define __FUNCT__ "MatSetBlockSize"
6115 /*@
6116    MatSetBlockSize - Sets the matrix block size; for many matrix types you
6117      cannot use this and MUST set the blocksize when you preallocate the matrix
6118 
6119    Logically Collective on Mat
6120 
6121    Input Parameters:
6122 +  mat - the matrix
6123 -  bs - block size
6124 
6125    Notes:
6126      For BAIJ matrices, this just checks that the block size agrees with the BAIJ size,
6127      it is not possible to change BAIJ block sizes after preallocation.
6128 
6129    Level: intermediate
6130 
6131    Concepts: matrices^block size
6132 
6133 .seealso: MatCreateSeqBAIJ(), MatCreateMPIBAIJ(), MatGetBlockSize()
6134 @*/
6135 PetscErrorCode PETSCMAT_DLLEXPORT MatSetBlockSize(Mat mat,PetscInt bs)
6136 {
6137   PetscErrorCode ierr;
6138 
6139   PetscFunctionBegin;
6140   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6141   PetscValidType(mat,1);
6142   PetscValidLogicalCollectiveInt(mat,bs,2);
6143   ierr = MatPreallocated(mat);CHKERRQ(ierr);
6144   if (bs < 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Block size %d, must be positive",bs);
6145   if (mat->ops->setblocksize) {
6146     ierr = (*mat->ops->setblocksize)(mat,bs);CHKERRQ(ierr);
6147   } else {
6148     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Cannot set the blocksize for matrix type %s",((PetscObject)mat)->type_name);
6149   }
6150   PetscFunctionReturn(0);
6151 }
6152 
6153 #undef __FUNCT__
6154 #define __FUNCT__ "MatGetRowIJ"
6155 /*@C
6156     MatGetRowIJ - Returns the compressed row storage i and j indices for sequential matrices.
6157 
6158    Collective on Mat
6159 
6160     Input Parameters:
6161 +   mat - the matrix
6162 .   shift -  0 or 1 indicating we want the indices starting at 0 or 1
6163 .   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
6164                 symmetrized
6165 -   inodecompressed - PETSC_TRUE or PETSC_FALSE  indicating if the nonzero structure of the
6166                  inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is
6167                  always used.
6168 
6169     Output Parameters:
6170 +   n - number of rows in the (possibly compressed) matrix
6171 .   ia - the row pointers [of length n+1]
6172 .   ja - the column indices
6173 -   done - indicates if the routine actually worked and returned appropriate ia[] and ja[] arrays; callers
6174            are responsible for handling the case when done == PETSC_FALSE and ia and ja are not set
6175 
6176     Level: developer
6177 
6178     Notes: You CANNOT change any of the ia[] or ja[] values.
6179 
6180            Use MatRestoreRowIJ() when you are finished accessing the ia[] and ja[] values
6181 
6182     Fortran Node
6183 
6184            In Fortran use
6185 $           PetscInt ia(1), ja(1)
6186 $           PetscOffset iia, jja
6187 $      call MatGetRowIJ(mat,shift,symmetric,inodecompressed,n,ia,iia,ja,jja,done,ierr)
6188 $
6189 $          or
6190 $
6191 $           PetscScalar, pointer :: xx_v(:)
6192 $    call  MatGetRowIJF90(mat,shift,symmetric,inodecompressed,n,ia,ja,done,ierr)
6193 
6194 
6195        Acess the ith and jth entries via ia(iia + i) and ja(jja + j)
6196 
6197 .seealso: MatGetColumnIJ(), MatRestoreRowIJ(), MatGetArray()
6198 @*/
6199 PetscErrorCode PETSCMAT_DLLEXPORT MatGetRowIJ(Mat mat,PetscInt shift,PetscTruth symmetric,PetscTruth inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt* ja[],PetscTruth *done)
6200 {
6201   PetscErrorCode ierr;
6202 
6203   PetscFunctionBegin;
6204   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6205   PetscValidType(mat,1);
6206   PetscValidIntPointer(n,4);
6207   if (ia) PetscValidIntPointer(ia,5);
6208   if (ja) PetscValidIntPointer(ja,6);
6209   PetscValidIntPointer(done,7);
6210   ierr = MatPreallocated(mat);CHKERRQ(ierr);
6211   if (!mat->ops->getrowij) *done = PETSC_FALSE;
6212   else {
6213     *done = PETSC_TRUE;
6214     ierr = PetscLogEventBegin(MAT_GetRowIJ,mat,0,0,0);CHKERRQ(ierr);
6215     ierr  = (*mat->ops->getrowij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr);
6216     ierr = PetscLogEventEnd(MAT_GetRowIJ,mat,0,0,0);CHKERRQ(ierr);
6217   }
6218   PetscFunctionReturn(0);
6219 }
6220 
6221 #undef __FUNCT__
6222 #define __FUNCT__ "MatGetColumnIJ"
6223 /*@C
6224     MatGetColumnIJ - Returns the compressed column storage i and j indices for sequential matrices.
6225 
6226     Collective on Mat
6227 
6228     Input Parameters:
6229 +   mat - the matrix
6230 .   shift - 1 or zero indicating we want the indices starting at 0 or 1
6231 .   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
6232                 symmetrized
6233 -   inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the
6234                  inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is
6235                  always used.
6236 
6237     Output Parameters:
6238 +   n - number of columns in the (possibly compressed) matrix
6239 .   ia - the column pointers
6240 .   ja - the row indices
6241 -   done - PETSC_TRUE or PETSC_FALSE, indicating whether the values have been returned
6242 
6243     Level: developer
6244 
6245 .seealso: MatGetRowIJ(), MatRestoreColumnIJ()
6246 @*/
6247 PetscErrorCode PETSCMAT_DLLEXPORT MatGetColumnIJ(Mat mat,PetscInt shift,PetscTruth symmetric,PetscTruth inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt* ja[],PetscTruth *done)
6248 {
6249   PetscErrorCode ierr;
6250 
6251   PetscFunctionBegin;
6252   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6253   PetscValidType(mat,1);
6254   PetscValidIntPointer(n,4);
6255   if (ia) PetscValidIntPointer(ia,5);
6256   if (ja) PetscValidIntPointer(ja,6);
6257   PetscValidIntPointer(done,7);
6258   ierr = MatPreallocated(mat);CHKERRQ(ierr);
6259   if (!mat->ops->getcolumnij) *done = PETSC_FALSE;
6260   else {
6261     *done = PETSC_TRUE;
6262     ierr  = (*mat->ops->getcolumnij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr);
6263   }
6264   PetscFunctionReturn(0);
6265 }
6266 
6267 #undef __FUNCT__
6268 #define __FUNCT__ "MatRestoreRowIJ"
6269 /*@C
6270     MatRestoreRowIJ - Call after you are completed with the ia,ja indices obtained with
6271     MatGetRowIJ().
6272 
6273     Collective on Mat
6274 
6275     Input Parameters:
6276 +   mat - the matrix
6277 .   shift - 1 or zero indicating we want the indices starting at 0 or 1
6278 .   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
6279                 symmetrized
6280 -   inodecompressed -  PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the
6281                  inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is
6282                  always used.
6283 
6284     Output Parameters:
6285 +   n - size of (possibly compressed) matrix
6286 .   ia - the row pointers
6287 .   ja - the column indices
6288 -   done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned
6289 
6290     Level: developer
6291 
6292 .seealso: MatGetRowIJ(), MatRestoreColumnIJ()
6293 @*/
6294 PetscErrorCode PETSCMAT_DLLEXPORT MatRestoreRowIJ(Mat mat,PetscInt shift,PetscTruth symmetric,PetscTruth inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt* ja[],PetscTruth *done)
6295 {
6296   PetscErrorCode ierr;
6297 
6298   PetscFunctionBegin;
6299   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6300   PetscValidType(mat,1);
6301   if (ia) PetscValidIntPointer(ia,5);
6302   if (ja) PetscValidIntPointer(ja,6);
6303   PetscValidIntPointer(done,7);
6304   ierr = MatPreallocated(mat);CHKERRQ(ierr);
6305 
6306   if (!mat->ops->restorerowij) *done = PETSC_FALSE;
6307   else {
6308     *done = PETSC_TRUE;
6309     ierr  = (*mat->ops->restorerowij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr);
6310   }
6311   PetscFunctionReturn(0);
6312 }
6313 
6314 #undef __FUNCT__
6315 #define __FUNCT__ "MatRestoreColumnIJ"
6316 /*@C
6317     MatRestoreColumnIJ - Call after you are completed with the ia,ja indices obtained with
6318     MatGetColumnIJ().
6319 
6320     Collective on Mat
6321 
6322     Input Parameters:
6323 +   mat - the matrix
6324 .   shift - 1 or zero indicating we want the indices starting at 0 or 1
6325 -   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
6326                 symmetrized
6327 -   inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the
6328                  inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is
6329                  always used.
6330 
6331     Output Parameters:
6332 +   n - size of (possibly compressed) matrix
6333 .   ia - the column pointers
6334 .   ja - the row indices
6335 -   done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned
6336 
6337     Level: developer
6338 
6339 .seealso: MatGetColumnIJ(), MatRestoreRowIJ()
6340 @*/
6341 PetscErrorCode PETSCMAT_DLLEXPORT MatRestoreColumnIJ(Mat mat,PetscInt shift,PetscTruth symmetric,PetscTruth inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt* ja[],PetscTruth *done)
6342 {
6343   PetscErrorCode ierr;
6344 
6345   PetscFunctionBegin;
6346   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6347   PetscValidType(mat,1);
6348   if (ia) PetscValidIntPointer(ia,5);
6349   if (ja) PetscValidIntPointer(ja,6);
6350   PetscValidIntPointer(done,7);
6351   ierr = MatPreallocated(mat);CHKERRQ(ierr);
6352 
6353   if (!mat->ops->restorecolumnij) *done = PETSC_FALSE;
6354   else {
6355     *done = PETSC_TRUE;
6356     ierr  = (*mat->ops->restorecolumnij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr);
6357   }
6358   PetscFunctionReturn(0);
6359 }
6360 
6361 #undef __FUNCT__
6362 #define __FUNCT__ "MatColoringPatch"
6363 /*@C
6364     MatColoringPatch -Used inside matrix coloring routines that
6365     use MatGetRowIJ() and/or MatGetColumnIJ().
6366 
6367     Collective on Mat
6368 
6369     Input Parameters:
6370 +   mat - the matrix
6371 .   ncolors - max color value
6372 .   n   - number of entries in colorarray
6373 -   colorarray - array indicating color for each column
6374 
6375     Output Parameters:
6376 .   iscoloring - coloring generated using colorarray information
6377 
6378     Level: developer
6379 
6380 .seealso: MatGetRowIJ(), MatGetColumnIJ()
6381 
6382 @*/
6383 PetscErrorCode PETSCMAT_DLLEXPORT MatColoringPatch(Mat mat,PetscInt ncolors,PetscInt n,ISColoringValue colorarray[],ISColoring *iscoloring)
6384 {
6385   PetscErrorCode ierr;
6386 
6387   PetscFunctionBegin;
6388   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6389   PetscValidType(mat,1);
6390   PetscValidIntPointer(colorarray,4);
6391   PetscValidPointer(iscoloring,5);
6392   ierr = MatPreallocated(mat);CHKERRQ(ierr);
6393 
6394   if (!mat->ops->coloringpatch){
6395     ierr = ISColoringCreate(((PetscObject)mat)->comm,ncolors,n,colorarray,iscoloring);CHKERRQ(ierr);
6396   } else {
6397     ierr = (*mat->ops->coloringpatch)(mat,ncolors,n,colorarray,iscoloring);CHKERRQ(ierr);
6398   }
6399   PetscFunctionReturn(0);
6400 }
6401 
6402 
6403 #undef __FUNCT__
6404 #define __FUNCT__ "MatSetUnfactored"
6405 /*@
6406    MatSetUnfactored - Resets a factored matrix to be treated as unfactored.
6407 
6408    Logically Collective on Mat
6409 
6410    Input Parameter:
6411 .  mat - the factored matrix to be reset
6412 
6413    Notes:
6414    This routine should be used only with factored matrices formed by in-place
6415    factorization via ILU(0) (or by in-place LU factorization for the MATSEQDENSE
6416    format).  This option can save memory, for example, when solving nonlinear
6417    systems with a matrix-free Newton-Krylov method and a matrix-based, in-place
6418    ILU(0) preconditioner.
6419 
6420    Note that one can specify in-place ILU(0) factorization by calling
6421 .vb
6422      PCType(pc,PCILU);
6423      PCFactorSeUseInPlace(pc);
6424 .ve
6425    or by using the options -pc_type ilu -pc_factor_in_place
6426 
6427    In-place factorization ILU(0) can also be used as a local
6428    solver for the blocks within the block Jacobi or additive Schwarz
6429    methods (runtime option: -sub_pc_factor_in_place).  See the discussion
6430    of these preconditioners in the users manual for details on setting
6431    local solver options.
6432 
6433    Most users should employ the simplified KSP interface for linear solvers
6434    instead of working directly with matrix algebra routines such as this.
6435    See, e.g., KSPCreate().
6436 
6437    Level: developer
6438 
6439 .seealso: PCFactorSetUseInPlace()
6440 
6441    Concepts: matrices^unfactored
6442 
6443 @*/
6444 PetscErrorCode PETSCMAT_DLLEXPORT MatSetUnfactored(Mat mat)
6445 {
6446   PetscErrorCode ierr;
6447 
6448   PetscFunctionBegin;
6449   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6450   PetscValidType(mat,1);
6451   ierr = MatPreallocated(mat);CHKERRQ(ierr);
6452   mat->factortype = MAT_FACTOR_NONE;
6453   if (!mat->ops->setunfactored) PetscFunctionReturn(0);
6454   ierr = (*mat->ops->setunfactored)(mat);CHKERRQ(ierr);
6455   PetscFunctionReturn(0);
6456 }
6457 
6458 /*MC
6459     MatGetArrayF90 - Accesses a matrix array from Fortran90.
6460 
6461     Synopsis:
6462     MatGetArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr)
6463 
6464     Not collective
6465 
6466     Input Parameter:
6467 .   x - matrix
6468 
6469     Output Parameters:
6470 +   xx_v - the Fortran90 pointer to the array
6471 -   ierr - error code
6472 
6473     Example of Usage:
6474 .vb
6475       PetscScalar, pointer xx_v(:)
6476       ....
6477       call MatGetArrayF90(x,xx_v,ierr)
6478       a = xx_v(3)
6479       call MatRestoreArrayF90(x,xx_v,ierr)
6480 .ve
6481 
6482     Notes:
6483     Not yet supported for all F90 compilers
6484 
6485     Level: advanced
6486 
6487 .seealso:  MatRestoreArrayF90(), MatGetArray(), MatRestoreArray()
6488 
6489     Concepts: matrices^accessing array
6490 
6491 M*/
6492 
6493 /*MC
6494     MatRestoreArrayF90 - Restores a matrix array that has been
6495     accessed with MatGetArrayF90().
6496 
6497     Synopsis:
6498     MatRestoreArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr)
6499 
6500     Not collective
6501 
6502     Input Parameters:
6503 +   x - matrix
6504 -   xx_v - the Fortran90 pointer to the array
6505 
6506     Output Parameter:
6507 .   ierr - error code
6508 
6509     Example of Usage:
6510 .vb
6511        PetscScalar, pointer xx_v(:)
6512        ....
6513        call MatGetArrayF90(x,xx_v,ierr)
6514        a = xx_v(3)
6515        call MatRestoreArrayF90(x,xx_v,ierr)
6516 .ve
6517 
6518     Notes:
6519     Not yet supported for all F90 compilers
6520 
6521     Level: advanced
6522 
6523 .seealso:  MatGetArrayF90(), MatGetArray(), MatRestoreArray()
6524 
6525 M*/
6526 
6527 
6528 #undef __FUNCT__
6529 #define __FUNCT__ "MatGetSubMatrix"
6530 /*@
6531     MatGetSubMatrix - Gets a single submatrix on the same number of processors
6532                       as the original matrix.
6533 
6534     Collective on Mat
6535 
6536     Input Parameters:
6537 +   mat - the original matrix
6538 .   isrow - parallel IS containing the rows this processor should obtain
6539 .   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.
6540 -   cll - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
6541 
6542     Output Parameter:
6543 .   newmat - the new submatrix, of the same type as the old
6544 
6545     Level: advanced
6546 
6547     Notes:
6548     The submatrix will be able to be multiplied with vectors using the same layout as iscol.
6549 
6550     The rows in isrow will be sorted into the same order as the original matrix on each process.
6551 
6552       The first time this is called you should use a cll of MAT_INITIAL_MATRIX,
6553    the MatGetSubMatrix() routine will create the newmat for you. Any additional calls
6554    to this routine with a mat of the same nonzero structure and with a call of MAT_REUSE_MATRIX
6555    will reuse the matrix generated the first time.  You should call MatDestroy() on newmat when
6556    you are finished using it.
6557 
6558     The communicator of the newly obtained matrix is ALWAYS the same as the communicator of
6559     the input matrix.
6560 
6561     If iscol is PETSC_NULL then all columns are obtained (not supported in Fortran).
6562 
6563    Example usage:
6564    Consider the following 8x8 matrix with 34 non-zero values, that is
6565    assembled across 3 processors. Lets assume that proc0 owns 3 rows,
6566    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
6567    as follows:
6568 
6569 .vb
6570             1  2  0  |  0  3  0  |  0  4
6571     Proc0   0  5  6  |  7  0  0  |  8  0
6572             9  0 10  | 11  0  0  | 12  0
6573     -------------------------------------
6574            13  0 14  | 15 16 17  |  0  0
6575     Proc1   0 18  0  | 19 20 21  |  0  0
6576             0  0  0  | 22 23  0  | 24  0
6577     -------------------------------------
6578     Proc2  25 26 27  |  0  0 28  | 29  0
6579            30  0  0  | 31 32 33  |  0 34
6580 .ve
6581 
6582     Suppose isrow = [0 1 | 4 | 6 7] and iscol = [1 2 | 3 4 5 | 6].  The resulting submatrix is
6583 
6584 .vb
6585             2  0  |  0  3  0  |  0
6586     Proc0   5  6  |  7  0  0  |  8
6587     -------------------------------
6588     Proc1  18  0  | 19 20 21  |  0
6589     -------------------------------
6590     Proc2  26 27  |  0  0 28  | 29
6591             0  0  | 31 32 33  |  0
6592 .ve
6593 
6594 
6595     Concepts: matrices^submatrices
6596 
6597 .seealso: MatGetSubMatrices()
6598 @*/
6599 PetscErrorCode PETSCMAT_DLLEXPORT MatGetSubMatrix(Mat mat,IS isrow,IS iscol,MatReuse cll,Mat *newmat)
6600 {
6601   PetscErrorCode ierr;
6602   PetscMPIInt    size;
6603   Mat            *local;
6604   IS             iscoltmp;
6605 
6606   PetscFunctionBegin;
6607   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6608   PetscValidHeaderSpecific(isrow,IS_CLASSID,2);
6609   if (iscol) PetscValidHeaderSpecific(iscol,IS_CLASSID,3);
6610   PetscValidPointer(newmat,5);
6611   if (cll == MAT_REUSE_MATRIX) PetscValidHeaderSpecific(*newmat,MAT_CLASSID,5);
6612   PetscValidType(mat,1);
6613   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6614   ierr = MatPreallocated(mat);CHKERRQ(ierr);
6615   ierr = MPI_Comm_size(((PetscObject)mat)->comm,&size);CHKERRQ(ierr);
6616 
6617   if (!iscol) {
6618     ierr = ISCreateStride(((PetscObject)mat)->comm,mat->cmap->n,mat->cmap->rstart,1,&iscoltmp);CHKERRQ(ierr);
6619   } else {
6620     iscoltmp = iscol;
6621   }
6622 
6623   /* if original matrix is on just one processor then use submatrix generated */
6624   if (mat->ops->getsubmatrices && !mat->ops->getsubmatrix && size == 1 && cll == MAT_REUSE_MATRIX) {
6625     ierr = MatGetSubMatrices(mat,1,&isrow,&iscoltmp,MAT_REUSE_MATRIX,&newmat);CHKERRQ(ierr);
6626     if (!iscol) {ierr = ISDestroy(iscoltmp);CHKERRQ(ierr);}
6627     PetscFunctionReturn(0);
6628   } else if (mat->ops->getsubmatrices && !mat->ops->getsubmatrix && size == 1) {
6629     ierr    = MatGetSubMatrices(mat,1,&isrow,&iscoltmp,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr);
6630     *newmat = *local;
6631     ierr    = PetscFree(local);CHKERRQ(ierr);
6632     if (!iscol) {ierr = ISDestroy(iscoltmp);CHKERRQ(ierr);}
6633     PetscFunctionReturn(0);
6634   } else if (!mat->ops->getsubmatrix) {
6635     /* Create a new matrix type that implements the operation using the full matrix */
6636     switch (cll) {
6637       case MAT_INITIAL_MATRIX:
6638         ierr = MatCreateSubMatrix(mat,isrow,iscoltmp,newmat);CHKERRQ(ierr);
6639         break;
6640       case MAT_REUSE_MATRIX:
6641         ierr = MatSubMatrixUpdate(*newmat,mat,isrow,iscoltmp);CHKERRQ(ierr);
6642         break;
6643       default: SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Invalid MatReuse, must be either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX");
6644     }
6645     if (!iscol) {ierr = ISDestroy(iscoltmp);CHKERRQ(ierr);}
6646     PetscFunctionReturn(0);
6647   }
6648 
6649   if (!mat->ops->getsubmatrix) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
6650   ierr = (*mat->ops->getsubmatrix)(mat,isrow,iscoltmp,cll,newmat);CHKERRQ(ierr);
6651   if (!iscol) {ierr = ISDestroy(iscoltmp);CHKERRQ(ierr);}
6652   ierr = PetscObjectStateIncrease((PetscObject)*newmat);CHKERRQ(ierr);
6653   PetscFunctionReturn(0);
6654 }
6655 
6656 #undef __FUNCT__
6657 #define __FUNCT__ "MatStashSetInitialSize"
6658 /*@
6659    MatStashSetInitialSize - sets the sizes of the matrix stash, that is
6660    used during the assembly process to store values that belong to
6661    other processors.
6662 
6663    Not Collective
6664 
6665    Input Parameters:
6666 +  mat   - the matrix
6667 .  size  - the initial size of the stash.
6668 -  bsize - the initial size of the block-stash(if used).
6669 
6670    Options Database Keys:
6671 +   -matstash_initial_size <size> or <size0,size1,...sizep-1>
6672 -   -matstash_block_initial_size <bsize>  or <bsize0,bsize1,...bsizep-1>
6673 
6674    Level: intermediate
6675 
6676    Notes:
6677      The block-stash is used for values set with MatSetValuesBlocked() while
6678      the stash is used for values set with MatSetValues()
6679 
6680      Run with the option -info and look for output of the form
6681      MatAssemblyBegin_MPIXXX:Stash has MM entries, uses nn mallocs.
6682      to determine the appropriate value, MM, to use for size and
6683      MatAssemblyBegin_MPIXXX:Block-Stash has BMM entries, uses nn mallocs.
6684      to determine the value, BMM to use for bsize
6685 
6686    Concepts: stash^setting matrix size
6687    Concepts: matrices^stash
6688 
6689 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), Mat, MatStashGetInfo()
6690 
6691 @*/
6692 PetscErrorCode PETSCMAT_DLLEXPORT MatStashSetInitialSize(Mat mat,PetscInt size, PetscInt bsize)
6693 {
6694   PetscErrorCode ierr;
6695 
6696   PetscFunctionBegin;
6697   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6698   PetscValidType(mat,1);
6699   ierr = MatStashSetInitialSize_Private(&mat->stash,size);CHKERRQ(ierr);
6700   ierr = MatStashSetInitialSize_Private(&mat->bstash,bsize);CHKERRQ(ierr);
6701   PetscFunctionReturn(0);
6702 }
6703 
6704 #undef __FUNCT__
6705 #define __FUNCT__ "MatInterpolateAdd"
6706 /*@
6707    MatInterpolateAdd - w = y + A*x or A'*x depending on the shape of
6708      the matrix
6709 
6710    Neighbor-wise Collective on Mat
6711 
6712    Input Parameters:
6713 +  mat   - the matrix
6714 .  x,y - the vectors
6715 -  w - where the result is stored
6716 
6717    Level: intermediate
6718 
6719    Notes:
6720     w may be the same vector as y.
6721 
6722     This allows one to use either the restriction or interpolation (its transpose)
6723     matrix to do the interpolation
6724 
6725     Concepts: interpolation
6726 
6727 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict()
6728 
6729 @*/
6730 PetscErrorCode PETSCMAT_DLLEXPORT MatInterpolateAdd(Mat A,Vec x,Vec y,Vec w)
6731 {
6732   PetscErrorCode ierr;
6733   PetscInt       M,N;
6734 
6735   PetscFunctionBegin;
6736   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
6737   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
6738   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
6739   PetscValidHeaderSpecific(w,VEC_CLASSID,4);
6740   PetscValidType(A,1);
6741   ierr = MatPreallocated(A);CHKERRQ(ierr);
6742   ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr);
6743   if (N > M) {
6744     ierr = MatMultTransposeAdd(A,x,y,w);CHKERRQ(ierr);
6745   } else {
6746     ierr = MatMultAdd(A,x,y,w);CHKERRQ(ierr);
6747   }
6748   PetscFunctionReturn(0);
6749 }
6750 
6751 #undef __FUNCT__
6752 #define __FUNCT__ "MatInterpolate"
6753 /*@
6754    MatInterpolate - y = A*x or A'*x depending on the shape of
6755      the matrix
6756 
6757    Neighbor-wise Collective on Mat
6758 
6759    Input Parameters:
6760 +  mat   - the matrix
6761 -  x,y - the vectors
6762 
6763    Level: intermediate
6764 
6765    Notes:
6766     This allows one to use either the restriction or interpolation (its transpose)
6767     matrix to do the interpolation
6768 
6769    Concepts: matrices^interpolation
6770 
6771 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict()
6772 
6773 @*/
6774 PetscErrorCode PETSCMAT_DLLEXPORT MatInterpolate(Mat A,Vec x,Vec y)
6775 {
6776   PetscErrorCode ierr;
6777   PetscInt       M,N;
6778 
6779   PetscFunctionBegin;
6780   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
6781   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
6782   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
6783   PetscValidType(A,1);
6784   ierr = MatPreallocated(A);CHKERRQ(ierr);
6785   ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr);
6786   if (N > M) {
6787     ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr);
6788   } else {
6789     ierr = MatMult(A,x,y);CHKERRQ(ierr);
6790   }
6791   PetscFunctionReturn(0);
6792 }
6793 
6794 #undef __FUNCT__
6795 #define __FUNCT__ "MatRestrict"
6796 /*@
6797    MatRestrict - y = A*x or A'*x
6798 
6799    Neighbor-wise Collective on Mat
6800 
6801    Input Parameters:
6802 +  mat   - the matrix
6803 -  x,y - the vectors
6804 
6805    Level: intermediate
6806 
6807    Notes:
6808     This allows one to use either the restriction or interpolation (its transpose)
6809     matrix to do the restriction
6810 
6811    Concepts: matrices^restriction
6812 
6813 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatInterpolate()
6814 
6815 @*/
6816 PetscErrorCode PETSCMAT_DLLEXPORT MatRestrict(Mat A,Vec x,Vec y)
6817 {
6818   PetscErrorCode ierr;
6819   PetscInt       M,N;
6820 
6821   PetscFunctionBegin;
6822   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
6823   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
6824   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
6825   PetscValidType(A,1);
6826   ierr = MatPreallocated(A);CHKERRQ(ierr);
6827 
6828   ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr);
6829   if (N > M) {
6830     ierr = MatMult(A,x,y);CHKERRQ(ierr);
6831   } else {
6832     ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr);
6833   }
6834   PetscFunctionReturn(0);
6835 }
6836 
6837 #undef __FUNCT__
6838 #define __FUNCT__ "MatNullSpaceAttach"
6839 /*@
6840    MatNullSpaceAttach - attaches a null space to a matrix.
6841         This null space will be removed from the resulting vector whenever
6842         MatMult() is called
6843 
6844    Logically Collective on Mat and MatNullSpace
6845 
6846    Input Parameters:
6847 +  mat - the matrix
6848 -  nullsp - the null space object
6849 
6850    Level: developer
6851 
6852    Notes:
6853       Overwrites any previous null space that may have been attached
6854 
6855    Concepts: null space^attaching to matrix
6856 
6857 .seealso: MatCreate(), MatNullSpaceCreate()
6858 @*/
6859 PetscErrorCode PETSCMAT_DLLEXPORT MatNullSpaceAttach(Mat mat,MatNullSpace nullsp)
6860 {
6861   PetscErrorCode ierr;
6862 
6863   PetscFunctionBegin;
6864   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6865   PetscValidType(mat,1);
6866   PetscValidHeaderSpecific(nullsp,MAT_NULLSPACE_CLASSID,2);
6867   ierr = MatPreallocated(mat);CHKERRQ(ierr);
6868   ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr);
6869   if (mat->nullsp) { ierr = MatNullSpaceDestroy(mat->nullsp);CHKERRQ(ierr); }
6870   mat->nullsp = nullsp;
6871   PetscFunctionReturn(0);
6872 }
6873 
6874 #undef __FUNCT__
6875 #define __FUNCT__ "MatICCFactor"
6876 /*@C
6877    MatICCFactor - Performs in-place incomplete Cholesky factorization of matrix.
6878 
6879    Collective on Mat
6880 
6881    Input Parameters:
6882 +  mat - the matrix
6883 .  row - row/column permutation
6884 .  fill - expected fill factor >= 1.0
6885 -  level - level of fill, for ICC(k)
6886 
6887    Notes:
6888    Probably really in-place only when level of fill is zero, otherwise allocates
6889    new space to store factored matrix and deletes previous memory.
6890 
6891    Most users should employ the simplified KSP interface for linear solvers
6892    instead of working directly with matrix algebra routines such as this.
6893    See, e.g., KSPCreate().
6894 
6895    Level: developer
6896 
6897    Concepts: matrices^incomplete Cholesky factorization
6898    Concepts: Cholesky factorization
6899 
6900 .seealso: MatICCFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor()
6901 
6902     Developer Note: fortran interface is not autogenerated as the f90
6903     interface defintion cannot be generated correctly [due to MatFactorInfo]
6904 
6905 @*/
6906 PetscErrorCode PETSCMAT_DLLEXPORT MatICCFactor(Mat mat,IS row,const MatFactorInfo* info)
6907 {
6908   PetscErrorCode ierr;
6909 
6910   PetscFunctionBegin;
6911   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6912   PetscValidType(mat,1);
6913   if (row) PetscValidHeaderSpecific(row,IS_CLASSID,2);
6914   PetscValidPointer(info,3);
6915   if (mat->rmap->N != mat->cmap->N) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONG,"matrix must be square");
6916   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
6917   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6918   if (!mat->ops->iccfactor) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
6919   ierr = MatPreallocated(mat);CHKERRQ(ierr);
6920   ierr = (*mat->ops->iccfactor)(mat,row,info);CHKERRQ(ierr);
6921   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
6922   PetscFunctionReturn(0);
6923 }
6924 
6925 #undef __FUNCT__
6926 #define __FUNCT__ "MatSetValuesAdic"
6927 /*@
6928    MatSetValuesAdic - Sets values computed with ADIC automatic differentiation into a matrix.
6929 
6930    Not Collective
6931 
6932    Input Parameters:
6933 +  mat - the matrix
6934 -  v - the values compute with ADIC
6935 
6936    Level: developer
6937 
6938    Notes:
6939      Must call MatSetColoring() before using this routine. Also this matrix must already
6940      have its nonzero pattern determined.
6941 
6942 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
6943           MatSetValues(), MatSetColoring(), MatSetValuesAdifor()
6944 @*/
6945 PetscErrorCode PETSCMAT_DLLEXPORT MatSetValuesAdic(Mat mat,void *v)
6946 {
6947   PetscErrorCode ierr;
6948 
6949   PetscFunctionBegin;
6950   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6951   PetscValidType(mat,1);
6952   PetscValidPointer(mat,2);
6953 
6954   if (!mat->assembled) {
6955     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled");
6956   }
6957   ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
6958   if (!mat->ops->setvaluesadic) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
6959   ierr = (*mat->ops->setvaluesadic)(mat,v);CHKERRQ(ierr);
6960   ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
6961   ierr = MatView_Private(mat);CHKERRQ(ierr);
6962   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
6963   PetscFunctionReturn(0);
6964 }
6965 
6966 
6967 #undef __FUNCT__
6968 #define __FUNCT__ "MatSetColoring"
6969 /*@
6970    MatSetColoring - Sets a coloring used by calls to MatSetValuesAdic()
6971 
6972    Not Collective
6973 
6974    Input Parameters:
6975 +  mat - the matrix
6976 -  coloring - the coloring
6977 
6978    Level: developer
6979 
6980 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
6981           MatSetValues(), MatSetValuesAdic()
6982 @*/
6983 PetscErrorCode PETSCMAT_DLLEXPORT MatSetColoring(Mat mat,ISColoring coloring)
6984 {
6985   PetscErrorCode ierr;
6986 
6987   PetscFunctionBegin;
6988   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6989   PetscValidType(mat,1);
6990   PetscValidPointer(coloring,2);
6991 
6992   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled");
6993   if (!mat->ops->setcoloring) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
6994   ierr = (*mat->ops->setcoloring)(mat,coloring);CHKERRQ(ierr);
6995   PetscFunctionReturn(0);
6996 }
6997 
6998 #undef __FUNCT__
6999 #define __FUNCT__ "MatSetValuesAdifor"
7000 /*@
7001    MatSetValuesAdifor - Sets values computed with automatic differentiation into a matrix.
7002 
7003    Not Collective
7004 
7005    Input Parameters:
7006 +  mat - the matrix
7007 .  nl - leading dimension of v
7008 -  v - the values compute with ADIFOR
7009 
7010    Level: developer
7011 
7012    Notes:
7013      Must call MatSetColoring() before using this routine. Also this matrix must already
7014      have its nonzero pattern determined.
7015 
7016 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
7017           MatSetValues(), MatSetColoring()
7018 @*/
7019 PetscErrorCode PETSCMAT_DLLEXPORT MatSetValuesAdifor(Mat mat,PetscInt nl,void *v)
7020 {
7021   PetscErrorCode ierr;
7022 
7023   PetscFunctionBegin;
7024   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7025   PetscValidType(mat,1);
7026   PetscValidPointer(v,3);
7027 
7028   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled");
7029   ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
7030   if (!mat->ops->setvaluesadifor) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
7031   ierr = (*mat->ops->setvaluesadifor)(mat,nl,v);CHKERRQ(ierr);
7032   ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
7033   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
7034   PetscFunctionReturn(0);
7035 }
7036 
7037 #undef __FUNCT__
7038 #define __FUNCT__ "MatDiagonalScaleLocal"
7039 /*@
7040    MatDiagonalScaleLocal - Scales columns of a matrix given the scaling values including the
7041          ghosted ones.
7042 
7043    Not Collective
7044 
7045    Input Parameters:
7046 +  mat - the matrix
7047 -  diag = the diagonal values, including ghost ones
7048 
7049    Level: developer
7050 
7051    Notes: Works only for MPIAIJ and MPIBAIJ matrices
7052 
7053 .seealso: MatDiagonalScale()
7054 @*/
7055 PetscErrorCode PETSCMAT_DLLEXPORT MatDiagonalScaleLocal(Mat mat,Vec diag)
7056 {
7057   PetscErrorCode ierr;
7058   PetscMPIInt    size;
7059 
7060   PetscFunctionBegin;
7061   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7062   PetscValidHeaderSpecific(diag,VEC_CLASSID,2);
7063   PetscValidType(mat,1);
7064 
7065   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled");
7066   ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
7067   ierr = MPI_Comm_size(((PetscObject)mat)->comm,&size);CHKERRQ(ierr);
7068   if (size == 1) {
7069     PetscInt n,m;
7070     ierr = VecGetSize(diag,&n);CHKERRQ(ierr);
7071     ierr = MatGetSize(mat,0,&m);CHKERRQ(ierr);
7072     if (m == n) {
7073       ierr = MatDiagonalScale(mat,0,diag);CHKERRQ(ierr);
7074     } else {
7075       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only supported for sequential matrices when no ghost points/periodic conditions");
7076     }
7077   } else {
7078     PetscErrorCode (*f)(Mat,Vec);
7079     ierr = PetscObjectQueryFunction((PetscObject)mat,"MatDiagonalScaleLocal_C",(void (**)(void))&f);CHKERRQ(ierr);
7080     if (f) {
7081       ierr = (*f)(mat,diag);CHKERRQ(ierr);
7082     } else {
7083       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only supported for MPIAIJ and MPIBAIJ parallel matrices");
7084     }
7085   }
7086   ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
7087   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
7088   PetscFunctionReturn(0);
7089 }
7090 
7091 #undef __FUNCT__
7092 #define __FUNCT__ "MatGetInertia"
7093 /*@
7094    MatGetInertia - Gets the inertia from a factored matrix
7095 
7096    Collective on Mat
7097 
7098    Input Parameter:
7099 .  mat - the matrix
7100 
7101    Output Parameters:
7102 +   nneg - number of negative eigenvalues
7103 .   nzero - number of zero eigenvalues
7104 -   npos - number of positive eigenvalues
7105 
7106    Level: advanced
7107 
7108    Notes: Matrix must have been factored by MatCholeskyFactor()
7109 
7110 
7111 @*/
7112 PetscErrorCode PETSCMAT_DLLEXPORT MatGetInertia(Mat mat,PetscInt *nneg,PetscInt *nzero,PetscInt *npos)
7113 {
7114   PetscErrorCode ierr;
7115 
7116   PetscFunctionBegin;
7117   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7118   PetscValidType(mat,1);
7119   if (!mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
7120   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Numeric factor mat is not assembled");
7121   if (!mat->ops->getinertia) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
7122   ierr = (*mat->ops->getinertia)(mat,nneg,nzero,npos);CHKERRQ(ierr);
7123   PetscFunctionReturn(0);
7124 }
7125 
7126 /* ----------------------------------------------------------------*/
7127 #undef __FUNCT__
7128 #define __FUNCT__ "MatSolves"
7129 /*@C
7130    MatSolves - Solves A x = b, given a factored matrix, for a collection of vectors
7131 
7132    Neighbor-wise Collective on Mat and Vecs
7133 
7134    Input Parameters:
7135 +  mat - the factored matrix
7136 -  b - the right-hand-side vectors
7137 
7138    Output Parameter:
7139 .  x - the result vectors
7140 
7141    Notes:
7142    The vectors b and x cannot be the same.  I.e., one cannot
7143    call MatSolves(A,x,x).
7144 
7145    Notes:
7146    Most users should employ the simplified KSP interface for linear solvers
7147    instead of working directly with matrix algebra routines such as this.
7148    See, e.g., KSPCreate().
7149 
7150    Level: developer
7151 
7152    Concepts: matrices^triangular solves
7153 
7154 .seealso: MatSolveAdd(), MatSolveTranspose(), MatSolveTransposeAdd(), MatSolve()
7155 @*/
7156 PetscErrorCode PETSCMAT_DLLEXPORT MatSolves(Mat mat,Vecs b,Vecs x)
7157 {
7158   PetscErrorCode ierr;
7159 
7160   PetscFunctionBegin;
7161   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7162   PetscValidType(mat,1);
7163   if (x == b) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_IDN,"x and b must be different vectors");
7164   if (!mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
7165   if (!mat->rmap->N && !mat->cmap->N) PetscFunctionReturn(0);
7166 
7167   if (!mat->ops->solves) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
7168   ierr = MatPreallocated(mat);CHKERRQ(ierr);
7169   ierr = PetscLogEventBegin(MAT_Solves,mat,0,0,0);CHKERRQ(ierr);
7170   ierr = (*mat->ops->solves)(mat,b,x);CHKERRQ(ierr);
7171   ierr = PetscLogEventEnd(MAT_Solves,mat,0,0,0);CHKERRQ(ierr);
7172   PetscFunctionReturn(0);
7173 }
7174 
7175 #undef __FUNCT__
7176 #define __FUNCT__ "MatIsSymmetric"
7177 /*@
7178    MatIsSymmetric - Test whether a matrix is symmetric
7179 
7180    Collective on Mat
7181 
7182    Input Parameter:
7183 +  A - the matrix to test
7184 -  tol - difference between value and its transpose less than this amount counts as equal (use 0.0 for exact transpose)
7185 
7186    Output Parameters:
7187 .  flg - the result
7188 
7189    Level: intermediate
7190 
7191    Concepts: matrix^symmetry
7192 
7193 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetricKnown()
7194 @*/
7195 PetscErrorCode PETSCMAT_DLLEXPORT MatIsSymmetric(Mat A,PetscReal tol,PetscTruth *flg)
7196 {
7197   PetscErrorCode ierr;
7198 
7199   PetscFunctionBegin;
7200   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
7201   PetscValidPointer(flg,2);
7202 
7203   if (!A->symmetric_set) {
7204     if (!A->ops->issymmetric) {
7205       const MatType mattype;
7206       ierr = MatGetType(A,&mattype);CHKERRQ(ierr);
7207       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for symmetric",mattype);
7208     }
7209     ierr = (*A->ops->issymmetric)(A,tol,flg);CHKERRQ(ierr);
7210     if (!tol) {
7211       A->symmetric_set = PETSC_TRUE;
7212       A->symmetric = *flg;
7213       if (A->symmetric) {
7214 	A->structurally_symmetric_set = PETSC_TRUE;
7215 	A->structurally_symmetric     = PETSC_TRUE;
7216       }
7217     }
7218   } else if (A->symmetric) {
7219     *flg = PETSC_TRUE;
7220   } else if (!tol) {
7221     *flg = PETSC_FALSE;
7222   } else {
7223     if (!A->ops->issymmetric) {
7224       const MatType mattype;
7225       ierr = MatGetType(A,&mattype);CHKERRQ(ierr);
7226       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for symmetric",mattype);
7227     }
7228     ierr = (*A->ops->issymmetric)(A,tol,flg);CHKERRQ(ierr);
7229   }
7230   PetscFunctionReturn(0);
7231 }
7232 
7233 #undef __FUNCT__
7234 #define __FUNCT__ "MatIsHermitian"
7235 /*@
7236    MatIsHermitian - Test whether a matrix is Hermitian
7237 
7238    Collective on Mat
7239 
7240    Input Parameter:
7241 +  A - the matrix to test
7242 -  tol - difference between value and its transpose less than this amount counts as equal (use 0.0 for exact Hermitian)
7243 
7244    Output Parameters:
7245 .  flg - the result
7246 
7247    Level: intermediate
7248 
7249    Concepts: matrix^symmetry
7250 
7251 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetricKnown()
7252 @*/
7253 PetscErrorCode PETSCMAT_DLLEXPORT MatIsHermitian(Mat A,PetscReal tol,PetscTruth *flg)
7254 {
7255   PetscErrorCode ierr;
7256 
7257   PetscFunctionBegin;
7258   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
7259   PetscValidPointer(flg,2);
7260 
7261   if (!A->hermitian_set) {
7262     if (!A->ops->ishermitian) {
7263       const MatType mattype;
7264       ierr = MatGetType(A,&mattype);CHKERRQ(ierr);
7265       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for hermitian",mattype);
7266     }
7267     ierr = (*A->ops->ishermitian)(A,tol,flg);CHKERRQ(ierr);
7268     if (!tol) {
7269       A->hermitian_set = PETSC_TRUE;
7270       A->hermitian = *flg;
7271       if (A->hermitian) {
7272 	A->structurally_symmetric_set = PETSC_TRUE;
7273 	A->structurally_symmetric     = PETSC_TRUE;
7274       }
7275     }
7276   } else if (A->hermitian) {
7277     *flg = PETSC_TRUE;
7278   } else if (!tol) {
7279     *flg = PETSC_FALSE;
7280   } else {
7281     if (!A->ops->ishermitian) {
7282       const MatType mattype;
7283       ierr = MatGetType(A,&mattype);CHKERRQ(ierr);
7284       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for hermitian",mattype);
7285     }
7286     ierr = (*A->ops->ishermitian)(A,tol,flg);CHKERRQ(ierr);
7287   }
7288   PetscFunctionReturn(0);
7289 }
7290 
7291 #undef __FUNCT__
7292 #define __FUNCT__ "MatIsSymmetricKnown"
7293 /*@
7294    MatIsSymmetricKnown - Checks the flag on the matrix to see if it is symmetric.
7295 
7296    Not Collective
7297 
7298    Input Parameter:
7299 .  A - the matrix to check
7300 
7301    Output Parameters:
7302 +  set - if the symmetric flag is set (this tells you if the next flag is valid)
7303 -  flg - the result
7304 
7305    Level: advanced
7306 
7307    Concepts: matrix^symmetry
7308 
7309    Note: Does not check the matrix values directly, so this may return unknown (set = PETSC_FALSE). Use MatIsSymmetric()
7310          if you want it explicitly checked
7311 
7312 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetric()
7313 @*/
7314 PetscErrorCode PETSCMAT_DLLEXPORT MatIsSymmetricKnown(Mat A,PetscTruth *set,PetscTruth *flg)
7315 {
7316   PetscFunctionBegin;
7317   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
7318   PetscValidPointer(set,2);
7319   PetscValidPointer(flg,3);
7320   if (A->symmetric_set) {
7321     *set = PETSC_TRUE;
7322     *flg = A->symmetric;
7323   } else {
7324     *set = PETSC_FALSE;
7325   }
7326   PetscFunctionReturn(0);
7327 }
7328 
7329 #undef __FUNCT__
7330 #define __FUNCT__ "MatIsHermitianKnown"
7331 /*@
7332    MatIsHermitianKnown - Checks the flag on the matrix to see if it is hermitian.
7333 
7334    Not Collective
7335 
7336    Input Parameter:
7337 .  A - the matrix to check
7338 
7339    Output Parameters:
7340 +  set - if the hermitian flag is set (this tells you if the next flag is valid)
7341 -  flg - the result
7342 
7343    Level: advanced
7344 
7345    Concepts: matrix^symmetry
7346 
7347    Note: Does not check the matrix values directly, so this may return unknown (set = PETSC_FALSE). Use MatIsHermitian()
7348          if you want it explicitly checked
7349 
7350 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetric()
7351 @*/
7352 PetscErrorCode PETSCMAT_DLLEXPORT MatIsHermitianKnown(Mat A,PetscTruth *set,PetscTruth *flg)
7353 {
7354   PetscFunctionBegin;
7355   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
7356   PetscValidPointer(set,2);
7357   PetscValidPointer(flg,3);
7358   if (A->hermitian_set) {
7359     *set = PETSC_TRUE;
7360     *flg = A->hermitian;
7361   } else {
7362     *set = PETSC_FALSE;
7363   }
7364   PetscFunctionReturn(0);
7365 }
7366 
7367 #undef __FUNCT__
7368 #define __FUNCT__ "MatIsStructurallySymmetric"
7369 /*@
7370    MatIsStructurallySymmetric - Test whether a matrix is structurally symmetric
7371 
7372    Collective on Mat
7373 
7374    Input Parameter:
7375 .  A - the matrix to test
7376 
7377    Output Parameters:
7378 .  flg - the result
7379 
7380    Level: intermediate
7381 
7382    Concepts: matrix^symmetry
7383 
7384 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsSymmetric(), MatSetOption()
7385 @*/
7386 PetscErrorCode PETSCMAT_DLLEXPORT MatIsStructurallySymmetric(Mat A,PetscTruth *flg)
7387 {
7388   PetscErrorCode ierr;
7389 
7390   PetscFunctionBegin;
7391   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
7392   PetscValidPointer(flg,2);
7393   if (!A->structurally_symmetric_set) {
7394     if (!A->ops->isstructurallysymmetric) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_SUP,"Matrix does not support checking for structural symmetric");
7395     ierr = (*A->ops->isstructurallysymmetric)(A,&A->structurally_symmetric);CHKERRQ(ierr);
7396     A->structurally_symmetric_set = PETSC_TRUE;
7397   }
7398   *flg = A->structurally_symmetric;
7399   PetscFunctionReturn(0);
7400 }
7401 
7402 #undef __FUNCT__
7403 #define __FUNCT__ "MatStashGetInfo"
7404 extern PetscErrorCode MatStashGetInfo_Private(MatStash*,PetscInt*,PetscInt*);
7405 /*@
7406    MatStashGetInfo - Gets how many values are currently in the vector stash, i.e. need
7407        to be communicated to other processors during the MatAssemblyBegin/End() process
7408 
7409     Not collective
7410 
7411    Input Parameter:
7412 .   vec - the vector
7413 
7414    Output Parameters:
7415 +   nstash   - the size of the stash
7416 .   reallocs - the number of additional mallocs incurred.
7417 .   bnstash   - the size of the block stash
7418 -   breallocs - the number of additional mallocs incurred.in the block stash
7419 
7420    Level: advanced
7421 
7422 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), Mat, MatStashSetInitialSize()
7423 
7424 @*/
7425 PetscErrorCode PETSCMAT_DLLEXPORT MatStashGetInfo(Mat mat,PetscInt *nstash,PetscInt *reallocs,PetscInt *bnstash,PetscInt *breallocs)
7426 {
7427   PetscErrorCode ierr;
7428   PetscFunctionBegin;
7429   ierr = MatStashGetInfo_Private(&mat->stash,nstash,reallocs);CHKERRQ(ierr);
7430   ierr = MatStashGetInfo_Private(&mat->bstash,bnstash,breallocs);CHKERRQ(ierr);
7431   PetscFunctionReturn(0);
7432 }
7433 
7434 #undef __FUNCT__
7435 #define __FUNCT__ "MatGetVecs"
7436 /*@C
7437    MatGetVecs - Get vector(s) compatible with the matrix, i.e. with the same
7438      parallel layout
7439 
7440    Collective on Mat
7441 
7442    Input Parameter:
7443 .  mat - the matrix
7444 
7445    Output Parameter:
7446 +   right - (optional) vector that the matrix can be multiplied against
7447 -   left - (optional) vector that the matrix vector product can be stored in
7448 
7449   Level: advanced
7450 
7451 .seealso: MatCreate()
7452 @*/
7453 PetscErrorCode PETSCMAT_DLLEXPORT MatGetVecs(Mat mat,Vec *right,Vec *left)
7454 {
7455   PetscErrorCode ierr;
7456 
7457   PetscFunctionBegin;
7458   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7459   PetscValidType(mat,1);
7460   ierr = MatPreallocated(mat);CHKERRQ(ierr);
7461   if (mat->ops->getvecs) {
7462     ierr = (*mat->ops->getvecs)(mat,right,left);CHKERRQ(ierr);
7463   } else {
7464     PetscMPIInt size;
7465     ierr = MPI_Comm_size(((PetscObject)mat)->comm, &size);CHKERRQ(ierr);
7466     if (right) {
7467       ierr = VecCreate(((PetscObject)mat)->comm,right);CHKERRQ(ierr);
7468       ierr = VecSetSizes(*right,mat->cmap->n,PETSC_DETERMINE);CHKERRQ(ierr);
7469       ierr = VecSetBlockSize(*right,mat->rmap->bs);CHKERRQ(ierr);
7470       if (size > 1) {
7471         /* New vectors uses Mat cmap and does not create a new one */
7472 	ierr = PetscLayoutDestroy((*right)->map);CHKERRQ(ierr);
7473 	(*right)->map = mat->cmap;
7474 	mat->cmap->refcnt++;
7475 
7476         ierr = VecSetType(*right,VECMPI);CHKERRQ(ierr);
7477       } else {ierr = VecSetType(*right,VECSEQ);CHKERRQ(ierr);}
7478     }
7479     if (left) {
7480       ierr = VecCreate(((PetscObject)mat)->comm,left);CHKERRQ(ierr);
7481       ierr = VecSetSizes(*left,mat->rmap->n,PETSC_DETERMINE);CHKERRQ(ierr);
7482       ierr = VecSetBlockSize(*left,mat->rmap->bs);CHKERRQ(ierr);
7483       if (size > 1) {
7484         /* New vectors uses Mat rmap and does not create a new one */
7485 	ierr = PetscLayoutDestroy((*left)->map);CHKERRQ(ierr);
7486 	(*left)->map = mat->rmap;
7487 	mat->rmap->refcnt++;
7488 
7489         ierr = VecSetType(*left,VECMPI);CHKERRQ(ierr);
7490       } else {ierr = VecSetType(*left,VECSEQ);CHKERRQ(ierr);}
7491     }
7492   }
7493   if (mat->mapping) {
7494     if (right) {ierr = VecSetLocalToGlobalMapping(*right,mat->mapping);CHKERRQ(ierr);}
7495     if (left) {ierr = VecSetLocalToGlobalMapping(*left,mat->mapping);CHKERRQ(ierr);}
7496   }
7497   if (mat->bmapping) {
7498     if (right) {ierr = VecSetLocalToGlobalMappingBlock(*right,mat->bmapping);CHKERRQ(ierr);}
7499     if (left) {ierr = VecSetLocalToGlobalMappingBlock(*left,mat->bmapping);CHKERRQ(ierr);}
7500   }
7501   PetscFunctionReturn(0);
7502 }
7503 
7504 #undef __FUNCT__
7505 #define __FUNCT__ "MatFactorInfoInitialize"
7506 /*@C
7507    MatFactorInfoInitialize - Initializes a MatFactorInfo data structure
7508      with default values.
7509 
7510    Not Collective
7511 
7512    Input Parameters:
7513 .    info - the MatFactorInfo data structure
7514 
7515 
7516    Notes: The solvers are generally used through the KSP and PC objects, for example
7517           PCLU, PCILU, PCCHOLESKY, PCICC
7518 
7519    Level: developer
7520 
7521 .seealso: MatFactorInfo
7522 
7523     Developer Note: fortran interface is not autogenerated as the f90
7524     interface defintion cannot be generated correctly [due to MatFactorInfo]
7525 
7526 @*/
7527 
7528 PetscErrorCode PETSCMAT_DLLEXPORT MatFactorInfoInitialize(MatFactorInfo *info)
7529 {
7530   PetscErrorCode ierr;
7531 
7532   PetscFunctionBegin;
7533   ierr = PetscMemzero(info,sizeof(MatFactorInfo));CHKERRQ(ierr);
7534   PetscFunctionReturn(0);
7535 }
7536 
7537 #undef __FUNCT__
7538 #define __FUNCT__ "MatPtAP"
7539 /*@
7540    MatPtAP - Creates the matrix product C = P^T * A * P
7541 
7542    Neighbor-wise Collective on Mat
7543 
7544    Input Parameters:
7545 +  A - the matrix
7546 .  P - the projection matrix
7547 .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
7548 -  fill - expected fill as ratio of nnz(C)/nnz(A)
7549 
7550    Output Parameters:
7551 .  C - the product matrix
7552 
7553    Notes:
7554    C will be created and must be destroyed by the user with MatDestroy().
7555 
7556    This routine is currently only implemented for pairs of AIJ matrices and classes
7557    which inherit from AIJ.
7558 
7559    Level: intermediate
7560 
7561 .seealso: MatPtAPSymbolic(), MatPtAPNumeric(), MatMatMult()
7562 @*/
7563 PetscErrorCode PETSCMAT_DLLEXPORT MatPtAP(Mat A,Mat P,MatReuse scall,PetscReal fill,Mat *C)
7564 {
7565   PetscErrorCode ierr;
7566 
7567   PetscFunctionBegin;
7568   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
7569   PetscValidType(A,1);
7570   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7571   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7572   PetscValidHeaderSpecific(P,MAT_CLASSID,2);
7573   PetscValidType(P,2);
7574   ierr = MatPreallocated(P);CHKERRQ(ierr);
7575   if (!P->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7576   if (P->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7577   PetscValidPointer(C,3);
7578   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);
7579   if (fill < 1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be >= 1.0",fill);
7580   ierr = MatPreallocated(A);CHKERRQ(ierr);
7581 
7582   if (!A->ops->ptap) {
7583     const MatType mattype;
7584     ierr = MatGetType(A,&mattype);CHKERRQ(ierr);
7585     SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"Matrix of type <%s> does not support PtAP",mattype);
7586   }
7587   ierr = PetscLogEventBegin(MAT_PtAP,A,P,0,0);CHKERRQ(ierr);
7588   ierr = (*A->ops->ptap)(A,P,scall,fill,C);CHKERRQ(ierr);
7589   ierr = PetscLogEventEnd(MAT_PtAP,A,P,0,0);CHKERRQ(ierr);
7590 
7591   PetscFunctionReturn(0);
7592 }
7593 
7594 #undef __FUNCT__
7595 #define __FUNCT__ "MatPtAPNumeric"
7596 /*@
7597    MatPtAPNumeric - Computes the matrix product C = P^T * A * P
7598 
7599    Neighbor-wise Collective on Mat
7600 
7601    Input Parameters:
7602 +  A - the matrix
7603 -  P - the projection matrix
7604 
7605    Output Parameters:
7606 .  C - the product matrix
7607 
7608    Notes:
7609    C must have been created by calling MatPtAPSymbolic and must be destroyed by
7610    the user using MatDeatroy().
7611 
7612    This routine is currently only implemented for pairs of AIJ matrices and classes
7613    which inherit from AIJ.  C will be of type MATAIJ.
7614 
7615    Level: intermediate
7616 
7617 .seealso: MatPtAP(), MatPtAPSymbolic(), MatMatMultNumeric()
7618 @*/
7619 PetscErrorCode PETSCMAT_DLLEXPORT MatPtAPNumeric(Mat A,Mat P,Mat C)
7620 {
7621   PetscErrorCode ierr;
7622 
7623   PetscFunctionBegin;
7624   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
7625   PetscValidType(A,1);
7626   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7627   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7628   PetscValidHeaderSpecific(P,MAT_CLASSID,2);
7629   PetscValidType(P,2);
7630   ierr = MatPreallocated(P);CHKERRQ(ierr);
7631   if (!P->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7632   if (P->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7633   PetscValidHeaderSpecific(C,MAT_CLASSID,3);
7634   PetscValidType(C,3);
7635   ierr = MatPreallocated(C);CHKERRQ(ierr);
7636   if (C->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7637   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);
7638   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);
7639   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);
7640   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);
7641   ierr = MatPreallocated(A);CHKERRQ(ierr);
7642 
7643   ierr = PetscLogEventBegin(MAT_PtAPNumeric,A,P,0,0);CHKERRQ(ierr);
7644   ierr = (*A->ops->ptapnumeric)(A,P,C);CHKERRQ(ierr);
7645   ierr = PetscLogEventEnd(MAT_PtAPNumeric,A,P,0,0);CHKERRQ(ierr);
7646   PetscFunctionReturn(0);
7647 }
7648 
7649 #undef __FUNCT__
7650 #define __FUNCT__ "MatPtAPSymbolic"
7651 /*@
7652    MatPtAPSymbolic - Creates the (i,j) structure of the matrix product C = P^T * A * P
7653 
7654    Neighbor-wise Collective on Mat
7655 
7656    Input Parameters:
7657 +  A - the matrix
7658 -  P - the projection matrix
7659 
7660    Output Parameters:
7661 .  C - the (i,j) structure of the product matrix
7662 
7663    Notes:
7664    C will be created and must be destroyed by the user with MatDestroy().
7665 
7666    This routine is currently only implemented for pairs of SeqAIJ matrices and classes
7667    which inherit from SeqAIJ.  C will be of type MATSEQAIJ.  The product is computed using
7668    this (i,j) structure by calling MatPtAPNumeric().
7669 
7670    Level: intermediate
7671 
7672 .seealso: MatPtAP(), MatPtAPNumeric(), MatMatMultSymbolic()
7673 @*/
7674 PetscErrorCode PETSCMAT_DLLEXPORT MatPtAPSymbolic(Mat A,Mat P,PetscReal fill,Mat *C)
7675 {
7676   PetscErrorCode ierr;
7677 
7678   PetscFunctionBegin;
7679   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
7680   PetscValidType(A,1);
7681   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7682   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7683   if (fill <1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be >= 1.0",fill);
7684   PetscValidHeaderSpecific(P,MAT_CLASSID,2);
7685   PetscValidType(P,2);
7686   ierr = MatPreallocated(P);CHKERRQ(ierr);
7687   if (!P->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7688   if (P->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7689   PetscValidPointer(C,3);
7690 
7691   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);
7692   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);
7693   ierr = MatPreallocated(A);CHKERRQ(ierr);
7694   ierr = PetscLogEventBegin(MAT_PtAPSymbolic,A,P,0,0);CHKERRQ(ierr);
7695   ierr = (*A->ops->ptapsymbolic)(A,P,fill,C);CHKERRQ(ierr);
7696   ierr = PetscLogEventEnd(MAT_PtAPSymbolic,A,P,0,0);CHKERRQ(ierr);
7697 
7698   ierr = MatSetBlockSize(*C,A->rmap->bs);CHKERRQ(ierr);
7699 
7700   PetscFunctionReturn(0);
7701 }
7702 
7703 #undef __FUNCT__
7704 #define __FUNCT__ "MatMatMult"
7705 /*@
7706    MatMatMult - Performs Matrix-Matrix Multiplication C=A*B.
7707 
7708    Neighbor-wise Collective on Mat
7709 
7710    Input Parameters:
7711 +  A - the left matrix
7712 .  B - the right matrix
7713 .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
7714 -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if you do not have a good estimate
7715           if the result is a dense matrix this is irrelevent
7716 
7717    Output Parameters:
7718 .  C - the product matrix
7719 
7720    Notes:
7721    Unless scall is MAT_REUSE_MATRIX C will be created.
7722 
7723    MAT_REUSE_MATRIX can only be used if the matrices A and B have the same nonzero pattern as in the previous call
7724 
7725    To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value
7726    actually needed.
7727 
7728    If you have many matrices with the same non-zero structure to multiply, you
7729    should either
7730 $   1) use MAT_REUSE_MATRIX in all calls but the first or
7731 $   2) call MatMatMultSymbolic() once and then MatMatMultNumeric() for each product needed
7732 
7733    Level: intermediate
7734 
7735 .seealso: MatMatMultSymbolic(), MatMatMultNumeric(), MatPtAP()
7736 @*/
7737 PetscErrorCode PETSCMAT_DLLEXPORT MatMatMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
7738 {
7739   PetscErrorCode ierr;
7740   PetscErrorCode (*fA)(Mat,Mat,MatReuse,PetscReal,Mat*);
7741   PetscErrorCode (*fB)(Mat,Mat,MatReuse,PetscReal,Mat*);
7742   PetscErrorCode (*mult)(Mat,Mat,MatReuse,PetscReal,Mat *)=PETSC_NULL;
7743 
7744   PetscFunctionBegin;
7745   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
7746   PetscValidType(A,1);
7747   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7748   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7749   PetscValidHeaderSpecific(B,MAT_CLASSID,2);
7750   PetscValidType(B,2);
7751   ierr = MatPreallocated(B);CHKERRQ(ierr);
7752   if (!B->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7753   if (B->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7754   PetscValidPointer(C,3);
7755   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);
7756   if (scall == MAT_REUSE_MATRIX){
7757     PetscValidPointer(*C,5);
7758     PetscValidHeaderSpecific(*C,MAT_CLASSID,5);
7759   }
7760   if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0;
7761   if (fill < 1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be >= 1.0",fill);
7762   ierr = MatPreallocated(A);CHKERRQ(ierr);
7763 
7764   fA = A->ops->matmult;
7765   fB = B->ops->matmult;
7766   if (fB == fA) {
7767     if (!fB) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"MatMatMult not supported for B of type %s",((PetscObject)B)->type_name);
7768     mult = fB;
7769   } else {
7770     /* dispatch based on the type of A and B */
7771     char  multname[256];
7772     ierr = PetscStrcpy(multname,"MatMatMult_");CHKERRQ(ierr);
7773     ierr = PetscStrcat(multname,((PetscObject)A)->type_name);CHKERRQ(ierr);
7774     ierr = PetscStrcat(multname,"_");CHKERRQ(ierr);
7775     ierr = PetscStrcat(multname,((PetscObject)B)->type_name);CHKERRQ(ierr);
7776     ierr = PetscStrcat(multname,"_C");CHKERRQ(ierr); /* e.g., multname = "MatMatMult_seqdense_seqaij_C" */
7777     ierr = PetscObjectQueryFunction((PetscObject)B,multname,(void (**)(void))&mult);CHKERRQ(ierr);
7778     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);
7779   }
7780   ierr = PetscLogEventBegin(MAT_MatMult,A,B,0,0);CHKERRQ(ierr);
7781   ierr = (*mult)(A,B,scall,fill,C);CHKERRQ(ierr);
7782   ierr = PetscLogEventEnd(MAT_MatMult,A,B,0,0);CHKERRQ(ierr);
7783   PetscFunctionReturn(0);
7784 }
7785 
7786 #undef __FUNCT__
7787 #define __FUNCT__ "MatMatMultSymbolic"
7788 /*@
7789    MatMatMultSymbolic - Performs construction, preallocation, and computes the ij structure
7790    of the matrix-matrix product C=A*B.  Call this routine before calling MatMatMultNumeric().
7791 
7792    Neighbor-wise Collective on Mat
7793 
7794    Input Parameters:
7795 +  A - the left matrix
7796 .  B - the right matrix
7797 -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if you do not have a good estimate,
7798       if C is a dense matrix this is irrelevent
7799 
7800    Output Parameters:
7801 .  C - the product matrix
7802 
7803    Notes:
7804    Unless scall is MAT_REUSE_MATRIX C will be created.
7805 
7806    To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value
7807    actually needed.
7808 
7809    This routine is currently implemented for
7810     - pairs of AIJ matrices and classes which inherit from AIJ, C will be of type AIJ
7811     - pairs of AIJ (A) and Dense (B) matrix, C will be of type Dense.
7812     - pairs of Dense (A) and AIJ (B) matrix, C will be of type Dense.
7813 
7814    Level: intermediate
7815 
7816    Developers Note: There are ways to estimate the number of nonzeros in the resulting product, see for example, http://arxiv.org/abs/1006.4173
7817      We should incorporate them into PETSc.
7818 
7819 .seealso: MatMatMult(), MatMatMultNumeric()
7820 @*/
7821 PetscErrorCode PETSCMAT_DLLEXPORT MatMatMultSymbolic(Mat A,Mat B,PetscReal fill,Mat *C)
7822 {
7823   PetscErrorCode ierr;
7824   PetscErrorCode (*Asymbolic)(Mat,Mat,PetscReal,Mat *);
7825   PetscErrorCode (*Bsymbolic)(Mat,Mat,PetscReal,Mat *);
7826   PetscErrorCode (*symbolic)(Mat,Mat,PetscReal,Mat *)=PETSC_NULL;
7827 
7828   PetscFunctionBegin;
7829   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
7830   PetscValidType(A,1);
7831   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7832   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7833 
7834   PetscValidHeaderSpecific(B,MAT_CLASSID,2);
7835   PetscValidType(B,2);
7836   ierr = MatPreallocated(B);CHKERRQ(ierr);
7837   if (!B->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7838   if (B->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7839   PetscValidPointer(C,3);
7840 
7841   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);
7842   if (fill == PETSC_DEFAULT) fill = 2.0;
7843   if (fill < 1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be > 1.0",fill);
7844   ierr = MatPreallocated(A);CHKERRQ(ierr);
7845 
7846   Asymbolic = A->ops->matmultsymbolic;
7847   Bsymbolic = B->ops->matmultsymbolic;
7848   if (Asymbolic == Bsymbolic){
7849     if (!Bsymbolic) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"C=A*B not implemented for B of type %s",((PetscObject)B)->type_name);
7850     symbolic = Bsymbolic;
7851   } else { /* dispatch based on the type of A and B */
7852     char  symbolicname[256];
7853     ierr = PetscStrcpy(symbolicname,"MatMatMultSymbolic_");CHKERRQ(ierr);
7854     ierr = PetscStrcat(symbolicname,((PetscObject)A)->type_name);CHKERRQ(ierr);
7855     ierr = PetscStrcat(symbolicname,"_");CHKERRQ(ierr);
7856     ierr = PetscStrcat(symbolicname,((PetscObject)B)->type_name);CHKERRQ(ierr);
7857     ierr = PetscStrcat(symbolicname,"_C");CHKERRQ(ierr);
7858     ierr = PetscObjectQueryFunction((PetscObject)B,symbolicname,(void (**)(void))&symbolic);CHKERRQ(ierr);
7859     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);
7860   }
7861   ierr = PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr);
7862   ierr = (*symbolic)(A,B,fill,C);CHKERRQ(ierr);
7863   ierr = PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr);
7864   PetscFunctionReturn(0);
7865 }
7866 
7867 #undef __FUNCT__
7868 #define __FUNCT__ "MatMatMultNumeric"
7869 /*@
7870    MatMatMultNumeric - Performs the numeric matrix-matrix product.
7871    Call this routine after first calling MatMatMultSymbolic().
7872 
7873    Neighbor-wise Collective on Mat
7874 
7875    Input Parameters:
7876 +  A - the left matrix
7877 -  B - the right matrix
7878 
7879    Output Parameters:
7880 .  C - the product matrix, which was created by from MatMatMultSymbolic() or a call to MatMatMult().
7881 
7882    Notes:
7883    C must have been created with MatMatMultSymbolic().
7884 
7885    This routine is currently implemented for
7886     - pairs of AIJ matrices and classes which inherit from AIJ, C will be of type MATAIJ.
7887     - pairs of AIJ (A) and Dense (B) matrix, C will be of type Dense.
7888     - pairs of Dense (A) and AIJ (B) matrix, C will be of type Dense.
7889 
7890    Level: intermediate
7891 
7892 .seealso: MatMatMult(), MatMatMultSymbolic()
7893 @*/
7894 PetscErrorCode PETSCMAT_DLLEXPORT MatMatMultNumeric(Mat A,Mat B,Mat C)
7895 {
7896   PetscErrorCode ierr;
7897   PetscErrorCode (*Anumeric)(Mat,Mat,Mat);
7898   PetscErrorCode (*Bnumeric)(Mat,Mat,Mat);
7899   PetscErrorCode (*numeric)(Mat,Mat,Mat)=PETSC_NULL;
7900 
7901   PetscFunctionBegin;
7902   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
7903   PetscValidType(A,1);
7904   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7905   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7906 
7907   PetscValidHeaderSpecific(B,MAT_CLASSID,2);
7908   PetscValidType(B,2);
7909   ierr = MatPreallocated(B);CHKERRQ(ierr);
7910   if (!B->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7911   if (B->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7912 
7913   PetscValidHeaderSpecific(C,MAT_CLASSID,3);
7914   PetscValidType(C,3);
7915   ierr = MatPreallocated(C);CHKERRQ(ierr);
7916   if (!C->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7917   if (C->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7918 
7919   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);
7920   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);
7921   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);
7922   ierr = MatPreallocated(A);CHKERRQ(ierr);
7923 
7924   Anumeric = A->ops->matmultnumeric;
7925   Bnumeric = B->ops->matmultnumeric;
7926   if (Anumeric == Bnumeric){
7927     if (!Bnumeric) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"MatMatMultNumeric not supported for B of type %s",((PetscObject)B)->type_name);
7928     numeric = Bnumeric;
7929   } else {
7930     char  numericname[256];
7931     ierr = PetscStrcpy(numericname,"MatMatMultNumeric_");CHKERRQ(ierr);
7932     ierr = PetscStrcat(numericname,((PetscObject)A)->type_name);CHKERRQ(ierr);
7933     ierr = PetscStrcat(numericname,"_");CHKERRQ(ierr);
7934     ierr = PetscStrcat(numericname,((PetscObject)B)->type_name);CHKERRQ(ierr);
7935     ierr = PetscStrcat(numericname,"_C");CHKERRQ(ierr);
7936     ierr = PetscObjectQueryFunction((PetscObject)B,numericname,(void (**)(void))&numeric);CHKERRQ(ierr);
7937     if (!numeric)
7938       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);
7939   }
7940   ierr = PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr);
7941   ierr = (*numeric)(A,B,C);CHKERRQ(ierr);
7942   ierr = PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr);
7943   PetscFunctionReturn(0);
7944 }
7945 
7946 #undef __FUNCT__
7947 #define __FUNCT__ "MatMatMultTranspose"
7948 /*@
7949    MatMatMultTranspose - Performs Matrix-Matrix Multiplication C=A^T*B.
7950 
7951    Neighbor-wise Collective on Mat
7952 
7953    Input Parameters:
7954 +  A - the left matrix
7955 .  B - the right matrix
7956 .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
7957 -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if not known
7958 
7959    Output Parameters:
7960 .  C - the product matrix
7961 
7962    Notes:
7963    C will be created if MAT_INITIAL_MATRIX and must be destroyed by the user with MatDestroy().
7964 
7965    MAT_REUSE_MATRIX can only be used if the matrices A and B have the same nonzero pattern as in the previous call
7966 
7967   To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value
7968    actually needed.
7969 
7970    This routine is currently only implemented for pairs of SeqAIJ matrices and pairs of SeqDense matrices and classes
7971    which inherit from SeqAIJ.  C will be of type MATSEQAIJ.
7972 
7973    Level: intermediate
7974 
7975 .seealso: MatMatMultTransposeSymbolic(), MatMatMultTransposeNumeric(), MatPtAP()
7976 @*/
7977 PetscErrorCode PETSCMAT_DLLEXPORT MatMatMultTranspose(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
7978 {
7979   PetscErrorCode ierr;
7980   PetscErrorCode (*fA)(Mat,Mat,MatReuse,PetscReal,Mat*);
7981   PetscErrorCode (*fB)(Mat,Mat,MatReuse,PetscReal,Mat*);
7982 
7983   PetscFunctionBegin;
7984   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
7985   PetscValidType(A,1);
7986   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7987   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7988   PetscValidHeaderSpecific(B,MAT_CLASSID,2);
7989   PetscValidType(B,2);
7990   ierr = MatPreallocated(B);CHKERRQ(ierr);
7991   if (!B->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7992   if (B->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7993   PetscValidPointer(C,3);
7994   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);
7995   if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0;
7996   if (fill < 1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be > 1.0",fill);
7997   ierr = MatPreallocated(A);CHKERRQ(ierr);
7998 
7999   fA = A->ops->matmulttranspose;
8000   if (!fA) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"MatMatMultTranspose not supported for A of type %s",((PetscObject)A)->type_name);
8001   fB = B->ops->matmulttranspose;
8002   if (!fB) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"MatMatMultTranspose not supported for B of type %s",((PetscObject)B)->type_name);
8003   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);
8004 
8005   ierr = PetscLogEventBegin(MAT_MatMultTranspose,A,B,0,0);CHKERRQ(ierr);
8006   ierr = (*A->ops->matmulttranspose)(A,B,scall,fill,C);CHKERRQ(ierr);
8007   ierr = PetscLogEventEnd(MAT_MatMultTranspose,A,B,0,0);CHKERRQ(ierr);
8008 
8009   PetscFunctionReturn(0);
8010 }
8011 
8012 #undef __FUNCT__
8013 #define __FUNCT__ "MatGetRedundantMatrix"
8014 /*@C
8015    MatGetRedundantMatrix - Create redundant matrices and put them into processors of subcommunicators.
8016 
8017    Collective on Mat
8018 
8019    Input Parameters:
8020 +  mat - the matrix
8021 .  nsubcomm - the number of subcommunicators (= number of redundant pareallel or sequential matrices)
8022 .  subcomm - MPI communicator split from the communicator where mat resides in
8023 .  mlocal_red - number of local rows of the redundant matrix
8024 -  reuse - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
8025 
8026    Output Parameter:
8027 .  matredundant - redundant matrix
8028 
8029    Notes:
8030    MAT_REUSE_MATRIX can only be used when the nonzero structure of the
8031    original matrix has not changed from that last call to MatGetRedundantMatrix().
8032 
8033    This routine creates the duplicated matrices in subcommunicators; you should NOT create them before
8034    calling it.
8035 
8036    Only MPIAIJ matrix is supported.
8037 
8038    Level: advanced
8039 
8040    Concepts: subcommunicator
8041    Concepts: duplicate matrix
8042 
8043 .seealso: MatDestroy()
8044 @*/
8045 PetscErrorCode PETSCMAT_DLLEXPORT MatGetRedundantMatrix(Mat mat,PetscInt nsubcomm,MPI_Comm subcomm,PetscInt mlocal_red,MatReuse reuse,Mat *matredundant)
8046 {
8047   PetscErrorCode ierr;
8048 
8049   PetscFunctionBegin;
8050   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
8051   if (nsubcomm && reuse == MAT_REUSE_MATRIX) {
8052     PetscValidPointer(*matredundant,6);
8053     PetscValidHeaderSpecific(*matredundant,MAT_CLASSID,6);
8054   }
8055   if (!mat->ops->getredundantmatrix) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
8056   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8057   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8058   ierr = MatPreallocated(mat);CHKERRQ(ierr);
8059 
8060   ierr = PetscLogEventBegin(MAT_GetRedundantMatrix,mat,0,0,0);CHKERRQ(ierr);
8061   ierr = (*mat->ops->getredundantmatrix)(mat,nsubcomm,subcomm,mlocal_red,reuse,matredundant);CHKERRQ(ierr);
8062   ierr = PetscLogEventEnd(MAT_GetRedundantMatrix,mat,0,0,0);CHKERRQ(ierr);
8063   PetscFunctionReturn(0);
8064 }
8065