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