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