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