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