xref: /petsc/src/mat/interface/matrix.c (revision 5edf65166e22df6feda2ed6bd8a6e348419dee17)
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     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, 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, 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     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 supplies only the permutation for its columns
4496 
4497    Output Parameters:
4498 .  B - the permuted matrix
4499 
4500    Level: advanced
4501 
4502    Note:
4503    The index sets map from row/col of permuted matrix to row/col of original matrix.
4504    The index sets should be on the same communicator as Mat and have the same local sizes.
4505 
4506    Concepts: matrices^permuting
4507 
4508 .seealso: MatGetOrdering(), ISAllGather()
4509 
4510 @*/
4511 PetscErrorCode  MatPermute(Mat mat,IS row,IS col,Mat *B)
4512 {
4513   PetscErrorCode ierr;
4514 
4515   PetscFunctionBegin;
4516   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
4517   PetscValidType(mat,1);
4518   PetscValidHeaderSpecific(row,IS_CLASSID,2);
4519   PetscValidHeaderSpecific(col,IS_CLASSID,3);
4520   PetscValidPointer(B,4);
4521   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
4522   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
4523   if (!mat->ops->permute) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatPermute not available for Mat type %s",((PetscObject)mat)->type_name);
4524   MatCheckPreallocated(mat,1);
4525 
4526   ierr = (*mat->ops->permute)(mat,row,col,B);CHKERRQ(ierr);
4527   ierr = PetscObjectStateIncrease((PetscObject)*B);CHKERRQ(ierr);
4528   PetscFunctionReturn(0);
4529 }
4530 
4531 #undef __FUNCT__
4532 #define __FUNCT__ "MatEqual"
4533 /*@
4534    MatEqual - Compares two matrices.
4535 
4536    Collective on Mat
4537 
4538    Input Parameters:
4539 +  A - the first matrix
4540 -  B - the second matrix
4541 
4542    Output Parameter:
4543 .  flg - PETSC_TRUE if the matrices are equal; PETSC_FALSE otherwise.
4544 
4545    Level: intermediate
4546 
4547    Concepts: matrices^equality between
4548 @*/
4549 PetscErrorCode  MatEqual(Mat A,Mat B,PetscBool  *flg)
4550 {
4551   PetscErrorCode ierr;
4552 
4553   PetscFunctionBegin;
4554   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
4555   PetscValidHeaderSpecific(B,MAT_CLASSID,2);
4556   PetscValidType(A,1);
4557   PetscValidType(B,2);
4558   PetscValidIntPointer(flg,3);
4559   PetscCheckSameComm(A,1,B,2);
4560   MatCheckPreallocated(B,2);
4561   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
4562   if (!B->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
4563   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);
4564   if (!A->ops->equal) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)A)->type_name);
4565   if (!B->ops->equal) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)B)->type_name);
4566   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);
4567   MatCheckPreallocated(A,1);
4568 
4569   ierr = (*A->ops->equal)(A,B,flg);CHKERRQ(ierr);
4570   PetscFunctionReturn(0);
4571 }
4572 
4573 #undef __FUNCT__
4574 #define __FUNCT__ "MatDiagonalScale"
4575 /*@
4576    MatDiagonalScale - Scales a matrix on the left and right by diagonal
4577    matrices that are stored as vectors.  Either of the two scaling
4578    matrices can be PETSC_NULL.
4579 
4580    Collective on Mat
4581 
4582    Input Parameters:
4583 +  mat - the matrix to be scaled
4584 .  l - the left scaling vector (or PETSC_NULL)
4585 -  r - the right scaling vector (or PETSC_NULL)
4586 
4587    Notes:
4588    MatDiagonalScale() computes A = LAR, where
4589    L = a diagonal matrix (stored as a vector), R = a diagonal matrix (stored as a vector)
4590    The L scales the rows of the matrix, the R scales the columns of the matrix.
4591 
4592    Level: intermediate
4593 
4594    Concepts: matrices^diagonal scaling
4595    Concepts: diagonal scaling of matrices
4596 
4597 .seealso: MatScale()
4598 @*/
4599 PetscErrorCode  MatDiagonalScale(Mat mat,Vec l,Vec r)
4600 {
4601   PetscErrorCode ierr;
4602 
4603   PetscFunctionBegin;
4604   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
4605   PetscValidType(mat,1);
4606   if (!mat->ops->diagonalscale) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
4607   if (l) {PetscValidHeaderSpecific(l,VEC_CLASSID,2);PetscCheckSameComm(mat,1,l,2);}
4608   if (r) {PetscValidHeaderSpecific(r,VEC_CLASSID,3);PetscCheckSameComm(mat,1,r,3);}
4609   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
4610   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
4611   MatCheckPreallocated(mat,1);
4612 
4613   ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
4614   ierr = (*mat->ops->diagonalscale)(mat,l,r);CHKERRQ(ierr);
4615   ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
4616   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
4617 #if defined(PETSC_HAVE_CUSP)
4618   if (mat->valid_GPU_matrix != PETSC_CUSP_UNALLOCATED) {
4619     mat->valid_GPU_matrix = PETSC_CUSP_CPU;
4620   }
4621 #endif
4622   PetscFunctionReturn(0);
4623 }
4624 
4625 #undef __FUNCT__
4626 #define __FUNCT__ "MatScale"
4627 /*@
4628     MatScale - Scales all elements of a matrix by a given number.
4629 
4630     Logically Collective on Mat
4631 
4632     Input Parameters:
4633 +   mat - the matrix to be scaled
4634 -   a  - the scaling value
4635 
4636     Output Parameter:
4637 .   mat - the scaled matrix
4638 
4639     Level: intermediate
4640 
4641     Concepts: matrices^scaling all entries
4642 
4643 .seealso: MatDiagonalScale()
4644 @*/
4645 PetscErrorCode  MatScale(Mat mat,PetscScalar a)
4646 {
4647   PetscErrorCode ierr;
4648 
4649   PetscFunctionBegin;
4650   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
4651   PetscValidType(mat,1);
4652   if (a != (PetscScalar)1.0 && !mat->ops->scale) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
4653   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
4654   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
4655   PetscValidLogicalCollectiveScalar(mat,a,2);
4656   MatCheckPreallocated(mat,1);
4657 
4658   ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
4659   if (a != (PetscScalar)1.0) {
4660     ierr = (*mat->ops->scale)(mat,a);CHKERRQ(ierr);
4661     ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
4662   }
4663   ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
4664 #if defined(PETSC_HAVE_CUSP)
4665   if (mat->valid_GPU_matrix != PETSC_CUSP_UNALLOCATED) {
4666     mat->valid_GPU_matrix = PETSC_CUSP_CPU;
4667   }
4668 #endif
4669   PetscFunctionReturn(0);
4670 }
4671 
4672 #undef __FUNCT__
4673 #define __FUNCT__ "MatNorm"
4674 /*@
4675    MatNorm - Calculates various norms of a matrix.
4676 
4677    Collective on Mat
4678 
4679    Input Parameters:
4680 +  mat - the matrix
4681 -  type - the type of norm, NORM_1, NORM_FROBENIUS, NORM_INFINITY
4682 
4683    Output Parameters:
4684 .  nrm - the resulting norm
4685 
4686    Level: intermediate
4687 
4688    Concepts: matrices^norm
4689    Concepts: norm^of matrix
4690 @*/
4691 PetscErrorCode  MatNorm(Mat mat,NormType type,PetscReal *nrm)
4692 {
4693   PetscErrorCode ierr;
4694 
4695   PetscFunctionBegin;
4696   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
4697   PetscValidType(mat,1);
4698   PetscValidScalarPointer(nrm,3);
4699 
4700   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
4701   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
4702   if (!mat->ops->norm) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
4703   MatCheckPreallocated(mat,1);
4704 
4705   ierr = (*mat->ops->norm)(mat,type,nrm);CHKERRQ(ierr);
4706   PetscFunctionReturn(0);
4707 }
4708 
4709 /*
4710      This variable is used to prevent counting of MatAssemblyBegin() that
4711    are called from within a MatAssemblyEnd().
4712 */
4713 static PetscInt MatAssemblyEnd_InUse = 0;
4714 #undef __FUNCT__
4715 #define __FUNCT__ "MatAssemblyBegin"
4716 /*@
4717    MatAssemblyBegin - Begins assembling the matrix.  This routine should
4718    be called after completing all calls to MatSetValues().
4719 
4720    Collective on Mat
4721 
4722    Input Parameters:
4723 +  mat - the matrix
4724 -  type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY
4725 
4726    Notes:
4727    MatSetValues() generally caches the values.  The matrix is ready to
4728    use only after MatAssemblyBegin() and MatAssemblyEnd() have been called.
4729    Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES
4730    in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before
4731    using the matrix.
4732 
4733    Space for preallocated nonzeros that is not filled by a call to MatSetValues() or a related routine are compressed
4734    out by assembly. If you intend to use that extra space on a subsequent assembly, be sure to insert explicit zeros
4735    before MAT_FINAL_ASSEMBLY so the space is not compressed out.
4736 
4737    Level: beginner
4738 
4739    Concepts: matrices^assembling
4740 
4741 .seealso: MatAssemblyEnd(), MatSetValues(), MatAssembled()
4742 @*/
4743 PetscErrorCode  MatAssemblyBegin(Mat mat,MatAssemblyType type)
4744 {
4745   PetscErrorCode ierr;
4746 
4747   PetscFunctionBegin;
4748   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
4749   PetscValidType(mat,1);
4750   MatCheckPreallocated(mat,1);
4751   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix.\nDid you forget to call MatSetUnfactored()?");
4752   if (mat->assembled) {
4753     mat->was_assembled = PETSC_TRUE;
4754     mat->assembled     = PETSC_FALSE;
4755   }
4756   if (!MatAssemblyEnd_InUse) {
4757     ierr = PetscLogEventBegin(MAT_AssemblyBegin,mat,0,0,0);CHKERRQ(ierr);
4758     if (mat->ops->assemblybegin) {ierr = (*mat->ops->assemblybegin)(mat,type);CHKERRQ(ierr);}
4759     ierr = PetscLogEventEnd(MAT_AssemblyBegin,mat,0,0,0);CHKERRQ(ierr);
4760   } else {
4761     if (mat->ops->assemblybegin) {ierr = (*mat->ops->assemblybegin)(mat,type);CHKERRQ(ierr);}
4762   }
4763   PetscFunctionReturn(0);
4764 }
4765 
4766 #undef __FUNCT__
4767 #define __FUNCT__ "MatAssembled"
4768 /*@
4769    MatAssembled - Indicates if a matrix has been assembled and is ready for
4770      use; for example, in matrix-vector product.
4771 
4772    Not Collective
4773 
4774    Input Parameter:
4775 .  mat - the matrix
4776 
4777    Output Parameter:
4778 .  assembled - PETSC_TRUE or PETSC_FALSE
4779 
4780    Level: advanced
4781 
4782    Concepts: matrices^assembled?
4783 
4784 .seealso: MatAssemblyEnd(), MatSetValues(), MatAssemblyBegin()
4785 @*/
4786 PetscErrorCode  MatAssembled(Mat mat,PetscBool  *assembled)
4787 {
4788   PetscFunctionBegin;
4789   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
4790   PetscValidType(mat,1);
4791   PetscValidPointer(assembled,2);
4792   *assembled = mat->assembled;
4793   PetscFunctionReturn(0);
4794 }
4795 
4796 #undef __FUNCT__
4797 #define __FUNCT__ "MatView_Private"
4798 /*
4799     Processes command line options to determine if/how a matrix
4800   is to be viewed. Called by MatAssemblyEnd() and MatLoad().
4801 */
4802 PetscErrorCode MatView_Private(Mat mat)
4803 {
4804   PetscErrorCode   ierr;
4805   PetscBool        flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE,flg4 = PETSC_FALSE,flg6 = PETSC_FALSE,flg7 = PETSC_FALSE,flg8 = PETSC_FALSE;
4806   static PetscBool incall = PETSC_FALSE;
4807 #if defined(PETSC_USE_SOCKET_VIEWER)
4808   PetscBool        flg5 = PETSC_FALSE;
4809 #endif
4810 
4811   PetscFunctionBegin;
4812   if (incall) PetscFunctionReturn(0);
4813   incall = PETSC_TRUE;
4814   ierr = PetscObjectOptionsBegin((PetscObject)mat);CHKERRQ(ierr);
4815     ierr = PetscOptionsBool("-mat_view_info","Information on matrix size","MatView",flg1,&flg1,PETSC_NULL);CHKERRQ(ierr);
4816     ierr = PetscOptionsBool("-mat_view_info_detailed","Nonzeros in the matrix","MatView",flg2,&flg2,PETSC_NULL);CHKERRQ(ierr);
4817     ierr = PetscOptionsBool("-mat_view","Print matrix to stdout","MatView",flg3,&flg3,PETSC_NULL);CHKERRQ(ierr);
4818     ierr = PetscOptionsBool("-mat_view_matlab","Print matrix to stdout in a format Matlab can read","MatView",flg4,&flg4,PETSC_NULL);CHKERRQ(ierr);
4819 #if defined(PETSC_USE_SOCKET_VIEWER)
4820     ierr = PetscOptionsBool("-mat_view_socket","Send matrix to socket (can be read from matlab)","MatView",flg5,&flg5,PETSC_NULL);CHKERRQ(ierr);
4821 #endif
4822     ierr = PetscOptionsBool("-mat_view_binary","Save matrix to file in binary format","MatView",flg6,&flg6,PETSC_NULL);CHKERRQ(ierr);
4823     ierr = PetscOptionsBool("-mat_view_draw","Draw the matrix nonzero structure","MatView",flg7,&flg7,PETSC_NULL);CHKERRQ(ierr);
4824   ierr = PetscOptionsEnd();CHKERRQ(ierr);
4825 
4826   if (flg1) {
4827     PetscViewer viewer;
4828 
4829     ierr = PetscViewerASCIIGetStdout(((PetscObject)mat)->comm,&viewer);CHKERRQ(ierr);
4830     ierr = PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_INFO);CHKERRQ(ierr);
4831     ierr = MatView(mat,viewer);CHKERRQ(ierr);
4832     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
4833   }
4834   if (flg2) {
4835     PetscViewer viewer;
4836 
4837     ierr = PetscViewerASCIIGetStdout(((PetscObject)mat)->comm,&viewer);CHKERRQ(ierr);
4838     ierr = PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_INFO_DETAIL);CHKERRQ(ierr);
4839     ierr = MatView(mat,viewer);CHKERRQ(ierr);
4840     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
4841   }
4842   if (flg3) {
4843     PetscViewer viewer;
4844 
4845     ierr = PetscViewerASCIIGetStdout(((PetscObject)mat)->comm,&viewer);CHKERRQ(ierr);
4846     ierr = MatView(mat,viewer);CHKERRQ(ierr);
4847   }
4848   if (flg4) {
4849     PetscViewer viewer;
4850 
4851     ierr = PetscViewerASCIIGetStdout(((PetscObject)mat)->comm,&viewer);CHKERRQ(ierr);
4852     ierr = PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB);CHKERRQ(ierr);
4853     ierr = MatView(mat,viewer);CHKERRQ(ierr);
4854     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
4855   }
4856 #if defined(PETSC_USE_SOCKET_VIEWER)
4857   if (flg5) {
4858     ierr = MatView(mat,PETSC_VIEWER_SOCKET_(((PetscObject)mat)->comm));CHKERRQ(ierr);
4859     ierr = PetscViewerFlush(PETSC_VIEWER_SOCKET_(((PetscObject)mat)->comm));CHKERRQ(ierr);
4860   }
4861 #endif
4862   if (flg6) {
4863     ierr = MatView(mat,PETSC_VIEWER_BINARY_(((PetscObject)mat)->comm));CHKERRQ(ierr);
4864     ierr = PetscViewerFlush(PETSC_VIEWER_BINARY_(((PetscObject)mat)->comm));CHKERRQ(ierr);
4865   }
4866   if (flg7) {
4867     ierr = PetscOptionsGetBool(((PetscObject)mat)->prefix,"-mat_view_contour",&flg8,PETSC_NULL);CHKERRQ(ierr);
4868     if (flg8) {
4869       PetscViewerPushFormat(PETSC_VIEWER_DRAW_(((PetscObject)mat)->comm),PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr);
4870     }
4871     ierr = MatView(mat,PETSC_VIEWER_DRAW_(((PetscObject)mat)->comm));CHKERRQ(ierr);
4872     ierr = PetscViewerFlush(PETSC_VIEWER_DRAW_(((PetscObject)mat)->comm));CHKERRQ(ierr);
4873     if (flg8) {
4874       PetscViewerPopFormat(PETSC_VIEWER_DRAW_(((PetscObject)mat)->comm));CHKERRQ(ierr);
4875     }
4876   }
4877   incall = PETSC_FALSE;
4878   PetscFunctionReturn(0);
4879 }
4880 
4881 #undef __FUNCT__
4882 #define __FUNCT__ "MatAssemblyEnd"
4883 /*@
4884    MatAssemblyEnd - Completes assembling the matrix.  This routine should
4885    be called after MatAssemblyBegin().
4886 
4887    Collective on Mat
4888 
4889    Input Parameters:
4890 +  mat - the matrix
4891 -  type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY
4892 
4893    Options Database Keys:
4894 +  -mat_view_info - Prints info on matrix at conclusion of MatEndAssembly()
4895 .  -mat_view_info_detailed - Prints more detailed info
4896 .  -mat_view - Prints matrix in ASCII format
4897 .  -mat_view_matlab - Prints matrix in Matlab format
4898 .  -mat_view_draw - PetscDraws nonzero structure of matrix, using MatView() and PetscDrawOpenX().
4899 .  -display <name> - Sets display name (default is host)
4900 .  -draw_pause <sec> - Sets number of seconds to pause after display
4901 .  -mat_view_socket - Sends matrix to socket, can be accessed from Matlab (See the <a href="../../docs/manual.pdf">users manual</a>)
4902 .  -viewer_socket_machine <machine>
4903 .  -viewer_socket_port <port>
4904 .  -mat_view_binary - save matrix to file in binary format
4905 -  -viewer_binary_filename <name>
4906 
4907    Notes:
4908    MatSetValues() generally caches the values.  The matrix is ready to
4909    use only after MatAssemblyBegin() and MatAssemblyEnd() have been called.
4910    Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES
4911    in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before
4912    using the matrix.
4913 
4914    Space for preallocated nonzeros that is not filled by a call to MatSetValues() or a related routine are compressed
4915    out by assembly. If you intend to use that extra space on a subsequent assembly, be sure to insert explicit zeros
4916    before MAT_FINAL_ASSEMBLY so the space is not compressed out.
4917 
4918    Level: beginner
4919 
4920 .seealso: MatAssemblyBegin(), MatSetValues(), PetscDrawOpenX(), PetscDrawCreate(), MatView(), MatAssembled(), PetscViewerSocketOpen()
4921 @*/
4922 PetscErrorCode  MatAssemblyEnd(Mat mat,MatAssemblyType type)
4923 {
4924   PetscErrorCode  ierr;
4925   static PetscInt inassm = 0;
4926   PetscBool       flg = PETSC_FALSE;
4927 
4928   PetscFunctionBegin;
4929   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
4930   PetscValidType(mat,1);
4931 
4932   inassm++;
4933   MatAssemblyEnd_InUse++;
4934   if (MatAssemblyEnd_InUse == 1) { /* Do the logging only the first time through */
4935     ierr = PetscLogEventBegin(MAT_AssemblyEnd,mat,0,0,0);CHKERRQ(ierr);
4936     if (mat->ops->assemblyend) {
4937       ierr = (*mat->ops->assemblyend)(mat,type);CHKERRQ(ierr);
4938     }
4939     ierr = PetscLogEventEnd(MAT_AssemblyEnd,mat,0,0,0);CHKERRQ(ierr);
4940   } else {
4941     if (mat->ops->assemblyend) {
4942       ierr = (*mat->ops->assemblyend)(mat,type);CHKERRQ(ierr);
4943     }
4944   }
4945 
4946   /* Flush assembly is not a true assembly */
4947   if (type != MAT_FLUSH_ASSEMBLY) {
4948     mat->assembled  = PETSC_TRUE; mat->num_ass++;
4949   }
4950   mat->insertmode = NOT_SET_VALUES;
4951   MatAssemblyEnd_InUse--;
4952   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
4953   if (!mat->symmetric_eternal) {
4954     mat->symmetric_set              = PETSC_FALSE;
4955     mat->hermitian_set              = PETSC_FALSE;
4956     mat->structurally_symmetric_set = PETSC_FALSE;
4957   }
4958 #if defined(PETSC_HAVE_CUSP)
4959   if (mat->valid_GPU_matrix != PETSC_CUSP_UNALLOCATED) {
4960     mat->valid_GPU_matrix = PETSC_CUSP_CPU;
4961   }
4962 #endif
4963   if (inassm == 1 && type != MAT_FLUSH_ASSEMBLY) {
4964     ierr = MatView_Private(mat);CHKERRQ(ierr);
4965     ierr = PetscOptionsHasName(((PetscObject)mat)->prefix,"-mat_is_symmetric",&flg);CHKERRQ(ierr);
4966     if (flg) {
4967       PetscReal tol = 0.0;
4968       ierr = PetscOptionsGetReal(((PetscObject)mat)->prefix,"-mat_is_symmetric",&tol,PETSC_NULL);CHKERRQ(ierr);
4969       ierr = MatIsSymmetric(mat,tol,&flg);CHKERRQ(ierr);
4970       if (flg) {
4971         ierr = PetscPrintf(((PetscObject)mat)->comm,"Matrix is symmetric (tolerance %G)\n",tol);CHKERRQ(ierr);
4972       } else {
4973         ierr = PetscPrintf(((PetscObject)mat)->comm,"Matrix is not symmetric (tolerance %G)\n",tol);CHKERRQ(ierr);
4974       }
4975     }
4976     if (mat->nullsp) {
4977       ierr = PetscOptionsGetBool(PETSC_NULL,"-mat_null_space_test",&flg,PETSC_NULL);CHKERRQ(ierr);
4978       if (flg) {
4979         ierr = MatNullSpaceTest(mat->nullsp,mat,PETSC_NULL);CHKERRQ(ierr);
4980       }
4981     }
4982   }
4983   inassm--;
4984   PetscFunctionReturn(0);
4985 }
4986 
4987 #undef __FUNCT__
4988 #define __FUNCT__ "MatSetOption"
4989 /*@
4990    MatSetOption - Sets a parameter option for a matrix. Some options
4991    may be specific to certain storage formats.  Some options
4992    determine how values will be inserted (or added). Sorted,
4993    row-oriented input will generally assemble the fastest. The default
4994    is row-oriented.
4995 
4996    Logically Collective on Mat for certain operations, such as MAT_SPD, not collective for MAT_ROW_ORIENTED, see MatOption
4997 
4998    Input Parameters:
4999 +  mat - the matrix
5000 .  option - the option, one of those listed below (and possibly others),
5001 -  flg - turn the option on (PETSC_TRUE) or off (PETSC_FALSE)
5002 
5003   Options Describing Matrix Structure:
5004 +    MAT_SPD - symmetric positive definite
5005 -    MAT_SYMMETRIC - symmetric in terms of both structure and value
5006 .    MAT_HERMITIAN - transpose is the complex conjugation
5007 .    MAT_STRUCTURALLY_SYMMETRIC - symmetric nonzero structure
5008 -    MAT_SYMMETRY_ETERNAL - if you would like the symmetry/Hermitian flag
5009                             you set to be kept with all future use of the matrix
5010                             including after MatAssemblyBegin/End() which could
5011                             potentially change the symmetry structure, i.e. you
5012                             KNOW the matrix will ALWAYS have the property you set.
5013 
5014 
5015    Options For Use with MatSetValues():
5016    Insert a logically dense subblock, which can be
5017 .    MAT_ROW_ORIENTED - row-oriented (default)
5018 
5019    Note these options reflect the data you pass in with MatSetValues(); it has
5020    nothing to do with how the data is stored internally in the matrix
5021    data structure.
5022 
5023    When (re)assembling a matrix, we can restrict the input for
5024    efficiency/debugging purposes.  These options include
5025 +    MAT_NEW_NONZERO_LOCATIONS - additional insertions will be
5026         allowed if they generate a new nonzero
5027 .    MAT_NEW_DIAGONALS - new diagonals will be allowed (for block diagonal format only)
5028 .    MAT_IGNORE_OFF_PROC_ENTRIES - drops off-processor entries
5029 .    MAT_NEW_NONZERO_LOCATION_ERR - generates an error for new matrix entry
5030 .    MAT_USE_HASH_TABLE - uses a hash table to speed up matrix assembly
5031 +    MAT_NO_OFF_PROC_ENTRIES - you know each process will only set values for its own rows, will generate an error if
5032         any process sets values for another process. This avoids all reductions in the MatAssembly routines and thus improves
5033         performance for very large process counts.
5034 
5035    Notes:
5036    Some options are relevant only for particular matrix types and
5037    are thus ignored by others.  Other options are not supported by
5038    certain matrix types and will generate an error message if set.
5039 
5040    If using a Fortran 77 module to compute a matrix, one may need to
5041    use the column-oriented option (or convert to the row-oriented
5042    format).
5043 
5044    MAT_NEW_NONZERO_LOCATIONS set to PETSC_FALSE indicates that any add or insertion
5045    that would generate a new entry in the nonzero structure is instead
5046    ignored.  Thus, if memory has not alredy been allocated for this particular
5047    data, then the insertion is ignored. For dense matrices, in which
5048    the entire array is allocated, no entries are ever ignored.
5049    Set after the first MatAssemblyEnd()
5050 
5051    MAT_NEW_NONZERO_LOCATION_ERR indicates that any add or insertion
5052    that would generate a new entry in the nonzero structure instead produces
5053    an error. (Currently supported for AIJ and BAIJ formats only.)
5054    This is a useful flag when using SAME_NONZERO_PATTERN in calling
5055    KSPSetOperators() to ensure that the nonzero pattern truely does
5056    remain unchanged. Set after the first MatAssemblyEnd()
5057 
5058    MAT_NEW_NONZERO_ALLOCATION_ERR indicates that any add or insertion
5059    that would generate a new entry that has not been preallocated will
5060    instead produce an error. (Currently supported for AIJ and BAIJ formats
5061    only.) This is a useful flag when debugging matrix memory preallocation.
5062 
5063    MAT_IGNORE_OFF_PROC_ENTRIES indicates entries destined for
5064    other processors should be dropped, rather than stashed.
5065    This is useful if you know that the "owning" processor is also
5066    always generating the correct matrix entries, so that PETSc need
5067    not transfer duplicate entries generated on another processor.
5068 
5069    MAT_USE_HASH_TABLE indicates that a hash table be used to improve the
5070    searches during matrix assembly. When this flag is set, the hash table
5071    is created during the first Matrix Assembly. This hash table is
5072    used the next time through, during MatSetVaules()/MatSetVaulesBlocked()
5073    to improve the searching of indices. MAT_NEW_NONZERO_LOCATIONS flag
5074    should be used with MAT_USE_HASH_TABLE flag. This option is currently
5075    supported by MATMPIBAIJ format only.
5076 
5077    MAT_KEEP_NONZERO_PATTERN indicates when MatZeroRows() is called the zeroed entries
5078    are kept in the nonzero structure
5079 
5080    MAT_IGNORE_ZERO_ENTRIES - for AIJ/IS matrices this will stop zero values from creating
5081    a zero location in the matrix
5082 
5083    MAT_USE_INODES - indicates using inode version of the code - works with AIJ and
5084    ROWBS matrix types
5085 
5086    MAT_NO_OFF_PROC_ZERO_ROWS - you know each process will only zero its own rows. This avoids all reductions in the
5087         zero row routines and thus improves performance for very large process counts.
5088 
5089    MAT_IGNORE_LOWER_TRIANGULAR - For SBAIJ matrices will ignore any insertions you make in the lower triangular
5090         part of the matrix (since they should match the upper triangular part).
5091 
5092    Notes: Can only be called after MatSetSizes() and MatSetType() have been set.
5093 
5094    Level: intermediate
5095 
5096    Concepts: matrices^setting options
5097 
5098 .seealso:  MatOption, Mat
5099 
5100 @*/
5101 PetscErrorCode  MatSetOption(Mat mat,MatOption op,PetscBool  flg)
5102 {
5103   PetscErrorCode ierr;
5104 
5105   PetscFunctionBegin;
5106   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5107   PetscValidType(mat,1);
5108   if (op > 0) PetscValidLogicalCollectiveEnum(mat,op,2);
5109   PetscValidLogicalCollectiveBool(mat,flg,3);
5110 
5111   if (((int) op) <= MAT_OPTION_MIN || ((int) op) >= MAT_OPTION_MAX) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Options %d is out of range",(int)op);
5112   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()");
5113 
5114   switch (op) {
5115   case MAT_NO_OFF_PROC_ENTRIES:
5116     mat->nooffprocentries                = flg;
5117     PetscFunctionReturn(0);
5118     break;
5119   case MAT_NO_OFF_PROC_ZERO_ROWS:
5120     mat->nooffproczerorows               = flg;
5121     PetscFunctionReturn(0);
5122     break;
5123   case MAT_SPD:
5124     mat->spd_set                         = PETSC_TRUE;
5125     mat->spd                             = flg;
5126     if (flg) {
5127       mat->symmetric                     = PETSC_TRUE;
5128       mat->structurally_symmetric        = PETSC_TRUE;
5129       mat->symmetric_set                 = PETSC_TRUE;
5130       mat->structurally_symmetric_set    = PETSC_TRUE;
5131     }
5132     break;
5133   case MAT_SYMMETRIC:
5134     mat->symmetric                       = flg;
5135     if (flg) mat->structurally_symmetric = PETSC_TRUE;
5136     mat->symmetric_set                   = PETSC_TRUE;
5137     mat->structurally_symmetric_set      = flg;
5138     break;
5139   case MAT_HERMITIAN:
5140     mat->hermitian                       = flg;
5141     if (flg) mat->structurally_symmetric = PETSC_TRUE;
5142     mat->hermitian_set                   = PETSC_TRUE;
5143     mat->structurally_symmetric_set      = flg;
5144     break;
5145   case MAT_STRUCTURALLY_SYMMETRIC:
5146     mat->structurally_symmetric          = flg;
5147     mat->structurally_symmetric_set      = PETSC_TRUE;
5148     break;
5149   case MAT_SYMMETRY_ETERNAL:
5150     mat->symmetric_eternal               = flg;
5151     break;
5152   default:
5153     break;
5154   }
5155   if (mat->ops->setoption) {
5156     ierr = (*mat->ops->setoption)(mat,op,flg);CHKERRQ(ierr);
5157   }
5158   PetscFunctionReturn(0);
5159 }
5160 
5161 #undef __FUNCT__
5162 #define __FUNCT__ "MatZeroEntries"
5163 /*@
5164    MatZeroEntries - Zeros all entries of a matrix.  For sparse matrices
5165    this routine retains the old nonzero structure.
5166 
5167    Logically Collective on Mat
5168 
5169    Input Parameters:
5170 .  mat - the matrix
5171 
5172    Level: intermediate
5173 
5174    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.
5175    See the Performance chapter of the users manual for information on preallocating matrices.
5176 
5177    Concepts: matrices^zeroing
5178 
5179 .seealso: MatZeroRows()
5180 @*/
5181 PetscErrorCode  MatZeroEntries(Mat mat)
5182 {
5183   PetscErrorCode ierr;
5184 
5185   PetscFunctionBegin;
5186   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5187   PetscValidType(mat,1);
5188   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
5189   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");
5190   if (!mat->ops->zeroentries) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
5191   MatCheckPreallocated(mat,1);
5192 
5193   ierr = PetscLogEventBegin(MAT_ZeroEntries,mat,0,0,0);CHKERRQ(ierr);
5194   ierr = (*mat->ops->zeroentries)(mat);CHKERRQ(ierr);
5195   ierr = PetscLogEventEnd(MAT_ZeroEntries,mat,0,0,0);CHKERRQ(ierr);
5196   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
5197 #if defined(PETSC_HAVE_CUSP)
5198   if (mat->valid_GPU_matrix != PETSC_CUSP_UNALLOCATED) {
5199     mat->valid_GPU_matrix = PETSC_CUSP_CPU;
5200   }
5201 #endif
5202   PetscFunctionReturn(0);
5203 }
5204 
5205 #undef __FUNCT__
5206 #define __FUNCT__ "MatZeroRowsColumns"
5207 /*@C
5208    MatZeroRowsColumns - Zeros all entries (except possibly the main diagonal)
5209    of a set of rows and columns of a matrix.
5210 
5211    Collective on Mat
5212 
5213    Input Parameters:
5214 +  mat - the matrix
5215 .  numRows - the number of rows to remove
5216 .  rows - the global row indices
5217 .  diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry)
5218 .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
5219 -  b - optional vector of right hand side, that will be adjusted by provided solution
5220 
5221    Notes:
5222    This does not change the nonzero structure of the matrix, it merely zeros those entries in the matrix.
5223 
5224    The user can set a value in the diagonal entry (or for the AIJ and
5225    row formats can optionally remove the main diagonal entry from the
5226    nonzero structure as well, by passing 0.0 as the final argument).
5227 
5228    For the parallel case, all processes that share the matrix (i.e.,
5229    those in the communicator used for matrix creation) MUST call this
5230    routine, regardless of whether any rows being zeroed are owned by
5231    them.
5232 
5233    Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to
5234    list only rows local to itself).
5235 
5236    The option MAT_NO_OFF_PROC_ZERO_ROWS does not apply to this routine.
5237 
5238    Level: intermediate
5239 
5240    Concepts: matrices^zeroing rows
5241 
5242 .seealso: MatZeroRowsIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), MatZeroRowsColumnsIS()
5243 @*/
5244 PetscErrorCode  MatZeroRowsColumns(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
5245 {
5246   PetscErrorCode ierr;
5247 
5248   PetscFunctionBegin;
5249   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5250   PetscValidType(mat,1);
5251   if (numRows) PetscValidIntPointer(rows,3);
5252   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
5253   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
5254   if (!mat->ops->zerorowscolumns) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
5255   MatCheckPreallocated(mat,1);
5256 
5257   ierr = (*mat->ops->zerorowscolumns)(mat,numRows,rows,diag,x,b);CHKERRQ(ierr);
5258   ierr = MatView_Private(mat);CHKERRQ(ierr);
5259   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
5260 #if defined(PETSC_HAVE_CUSP)
5261   if (mat->valid_GPU_matrix != PETSC_CUSP_UNALLOCATED) {
5262     mat->valid_GPU_matrix = PETSC_CUSP_CPU;
5263   }
5264 #endif
5265   PetscFunctionReturn(0);
5266 }
5267 
5268 #undef __FUNCT__
5269 #define __FUNCT__ "MatZeroRowsColumnsIS"
5270 /*@C
5271    MatZeroRowsColumnsIS - Zeros all entries (except possibly the main diagonal)
5272    of a set of rows and columns of a matrix.
5273 
5274    Collective on Mat
5275 
5276    Input Parameters:
5277 +  mat - the matrix
5278 .  is - the rows to zero
5279 .  diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry)
5280 .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
5281 -  b - optional vector of right hand side, that will be adjusted by provided solution
5282 
5283    Notes:
5284    This does not change the nonzero structure of the matrix, it merely zeros those entries in the matrix.
5285 
5286    The user can set a value in the diagonal entry (or for the AIJ and
5287    row formats can optionally remove the main diagonal entry from the
5288    nonzero structure as well, by passing 0.0 as the final argument).
5289 
5290    For the parallel case, all processes that share the matrix (i.e.,
5291    those in the communicator used for matrix creation) MUST call this
5292    routine, regardless of whether any rows being zeroed are owned by
5293    them.
5294 
5295    Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to
5296    list only rows local to itself).
5297 
5298    The option MAT_NO_OFF_PROC_ZERO_ROWS does not apply to this routine.
5299 
5300    Level: intermediate
5301 
5302    Concepts: matrices^zeroing rows
5303 
5304 .seealso: MatZeroRowsIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), MatZeroRowsColumns()
5305 @*/
5306 PetscErrorCode  MatZeroRowsColumnsIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b)
5307 {
5308   PetscErrorCode ierr;
5309   PetscInt       numRows;
5310   const PetscInt *rows;
5311 
5312   PetscFunctionBegin;
5313   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5314   PetscValidHeaderSpecific(is,IS_CLASSID,2);
5315   PetscValidType(mat,1);
5316   PetscValidType(is,2);
5317   ierr = ISGetLocalSize(is,&numRows);CHKERRQ(ierr);
5318   ierr = ISGetIndices(is,&rows);CHKERRQ(ierr);
5319   ierr = MatZeroRowsColumns(mat,numRows,rows,diag,x,b);CHKERRQ(ierr);
5320   ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr);
5321   PetscFunctionReturn(0);
5322 }
5323 
5324 #undef __FUNCT__
5325 #define __FUNCT__ "MatZeroRows"
5326 /*@C
5327    MatZeroRows - Zeros all entries (except possibly the main diagonal)
5328    of a set of rows of a matrix.
5329 
5330    Collective on Mat
5331 
5332    Input Parameters:
5333 +  mat - the matrix
5334 .  numRows - the number of rows to remove
5335 .  rows - the global row indices
5336 .  diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry)
5337 .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
5338 -  b - optional vector of right hand side, that will be adjusted by provided solution
5339 
5340    Notes:
5341    For the AIJ and BAIJ matrix formats this removes the old nonzero structure,
5342    but does not release memory.  For the dense and block diagonal
5343    formats this does not alter the nonzero structure.
5344 
5345    If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure
5346    of the matrix is not changed (even for AIJ and BAIJ matrices) the values are
5347    merely zeroed.
5348 
5349    The user can set a value in the diagonal entry (or for the AIJ and
5350    row formats can optionally remove the main diagonal entry from the
5351    nonzero structure as well, by passing 0.0 as the final argument).
5352 
5353    For the parallel case, all processes that share the matrix (i.e.,
5354    those in the communicator used for matrix creation) MUST call this
5355    routine, regardless of whether any rows being zeroed are owned by
5356    them.
5357 
5358    Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to
5359    list only rows local to itself).
5360 
5361    You can call MatSetOption(mat,MAT_NO_OFF_PROC_ZERO_ROWS,PETSC_TRUE) if each process indicates only rows it
5362    owns that are to be zeroed. This saves a global synchronization in the implementation.
5363 
5364    Level: intermediate
5365 
5366    Concepts: matrices^zeroing rows
5367 
5368 .seealso: MatZeroRowsIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption()
5369 @*/
5370 PetscErrorCode  MatZeroRows(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
5371 {
5372   PetscErrorCode ierr;
5373 
5374   PetscFunctionBegin;
5375   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5376   PetscValidType(mat,1);
5377   if (numRows) PetscValidIntPointer(rows,3);
5378   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
5379   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
5380   if (!mat->ops->zerorows) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
5381   MatCheckPreallocated(mat,1);
5382 
5383   ierr = (*mat->ops->zerorows)(mat,numRows,rows,diag,x,b);CHKERRQ(ierr);
5384   ierr = MatView_Private(mat);CHKERRQ(ierr);
5385   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
5386 #if defined(PETSC_HAVE_CUSP)
5387   if (mat->valid_GPU_matrix != PETSC_CUSP_UNALLOCATED) {
5388     mat->valid_GPU_matrix = PETSC_CUSP_CPU;
5389   }
5390 #endif
5391   PetscFunctionReturn(0);
5392 }
5393 
5394 #undef __FUNCT__
5395 #define __FUNCT__ "MatZeroRowsIS"
5396 /*@C
5397    MatZeroRowsIS - Zeros all entries (except possibly the main diagonal)
5398    of a set of rows of a matrix.
5399 
5400    Collective on Mat
5401 
5402    Input Parameters:
5403 +  mat - the matrix
5404 .  is - index set of rows to remove
5405 .  diag - value put in all diagonals of eliminated rows
5406 .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
5407 -  b - optional vector of right hand side, that will be adjusted by provided solution
5408 
5409    Notes:
5410    For the AIJ and BAIJ matrix formats this removes the old nonzero structure,
5411    but does not release memory.  For the dense and block diagonal
5412    formats this does not alter the nonzero structure.
5413 
5414    If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure
5415    of the matrix is not changed (even for AIJ and BAIJ matrices) the values are
5416    merely zeroed.
5417 
5418    The user can set a value in the diagonal entry (or for the AIJ and
5419    row formats can optionally remove the main diagonal entry from the
5420    nonzero structure as well, by passing 0.0 as the final argument).
5421 
5422    For the parallel case, all processes that share the matrix (i.e.,
5423    those in the communicator used for matrix creation) MUST call this
5424    routine, regardless of whether any rows being zeroed are owned by
5425    them.
5426 
5427    Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to
5428    list only rows local to itself).
5429 
5430    You can call MatSetOption(mat,MAT_NO_OFF_PROC_ZERO_ROWS,PETSC_TRUE) if each process indicates only rows it
5431    owns that are to be zeroed. This saves a global synchronization in the implementation.
5432 
5433    Level: intermediate
5434 
5435    Concepts: matrices^zeroing rows
5436 
5437 .seealso: MatZeroRows(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption()
5438 @*/
5439 PetscErrorCode  MatZeroRowsIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b)
5440 {
5441   PetscInt       numRows;
5442   const PetscInt *rows;
5443   PetscErrorCode ierr;
5444 
5445   PetscFunctionBegin;
5446   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5447   PetscValidType(mat,1);
5448   PetscValidHeaderSpecific(is,IS_CLASSID,2);
5449   ierr = ISGetLocalSize(is,&numRows);CHKERRQ(ierr);
5450   ierr = ISGetIndices(is,&rows);CHKERRQ(ierr);
5451   ierr = MatZeroRows(mat,numRows,rows,diag,x,b);CHKERRQ(ierr);
5452   ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr);
5453   PetscFunctionReturn(0);
5454 }
5455 
5456 #undef __FUNCT__
5457 #define __FUNCT__ "MatZeroRowsStencil"
5458 /*@C
5459    MatZeroRowsStencil - Zeros all entries (except possibly the main diagonal)
5460    of a set of rows of a matrix. These rows must be local to the process.
5461 
5462    Collective on Mat
5463 
5464    Input Parameters:
5465 +  mat - the matrix
5466 .  numRows - the number of rows to remove
5467 .  rows - the grid coordinates (and component number when dof > 1) for matrix rows
5468 .  diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry)
5469 .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
5470 -  b - optional vector of right hand side, that will be adjusted by provided solution
5471 
5472    Notes:
5473    For the AIJ and BAIJ matrix formats this removes the old nonzero structure,
5474    but does not release memory.  For the dense and block diagonal
5475    formats this does not alter the nonzero structure.
5476 
5477    If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure
5478    of the matrix is not changed (even for AIJ and BAIJ matrices) the values are
5479    merely zeroed.
5480 
5481    The user can set a value in the diagonal entry (or for the AIJ and
5482    row formats can optionally remove the main diagonal entry from the
5483    nonzero structure as well, by passing 0.0 as the final argument).
5484 
5485    For the parallel case, all processes that share the matrix (i.e.,
5486    those in the communicator used for matrix creation) MUST call this
5487    routine, regardless of whether any rows being zeroed are owned by
5488    them.
5489 
5490    Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to
5491    list only rows local to itself).
5492 
5493    The grid coordinates are across the entire grid, not just the local portion
5494 
5495    In Fortran idxm and idxn should be declared as
5496 $     MatStencil idxm(4,m)
5497    and the values inserted using
5498 $    idxm(MatStencil_i,1) = i
5499 $    idxm(MatStencil_j,1) = j
5500 $    idxm(MatStencil_k,1) = k
5501 $    idxm(MatStencil_c,1) = c
5502    etc
5503 
5504    For periodic boundary conditions use negative indices for values to the left (below 0; that are to be
5505    obtained by wrapping values from right edge). For values to the right of the last entry using that index plus one
5506    etc to obtain values that obtained by wrapping the values from the left edge. This does not work for anything but the
5507    DMDA_BOUNDARY_PERIODIC boundary type.
5508 
5509    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
5510    a single value per point) you can skip filling those indices.
5511 
5512    Level: intermediate
5513 
5514    Concepts: matrices^zeroing rows
5515 
5516 .seealso: MatZeroRows(), MatZeroRowsIS(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption()
5517 @*/
5518 PetscErrorCode  MatZeroRowsStencil(Mat mat,PetscInt numRows,const MatStencil rows[],PetscScalar diag,Vec x,Vec b)
5519 {
5520   PetscInt        dim    = mat->stencil.dim;
5521   PetscInt        sdim   = dim - (1 - (PetscInt) mat->stencil.noc);
5522   PetscInt       *dims   = mat->stencil.dims+1;
5523   PetscInt       *starts = mat->stencil.starts;
5524   PetscInt       *dxm    = (PetscInt *) rows;
5525   PetscInt       *jdxm, i, j, tmp, numNewRows = 0;
5526   PetscErrorCode ierr;
5527 
5528   PetscFunctionBegin;
5529   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5530   PetscValidType(mat,1);
5531   if (numRows) PetscValidIntPointer(rows,3);
5532 
5533   ierr = PetscMalloc(numRows*sizeof(PetscInt), &jdxm);CHKERRQ(ierr);
5534   for (i = 0; i < numRows; ++i) {
5535     /* Skip unused dimensions (they are ordered k, j, i, c) */
5536     for (j = 0; j < 3-sdim; ++j) dxm++;
5537     /* Local index in X dir */
5538     tmp = *dxm++ - starts[0];
5539     /* Loop over remaining dimensions */
5540     for (j = 0; j < dim-1; ++j) {
5541       /* If nonlocal, set index to be negative */
5542       if ((*dxm++ - starts[j+1]) < 0 || tmp < 0) tmp = PETSC_MIN_INT;
5543       /* Update local index */
5544       else                                       tmp = tmp*dims[j] + *(dxm-1) - starts[j+1];
5545     }
5546     /* Skip component slot if necessary */
5547     if (mat->stencil.noc) dxm++;
5548     /* Local row number */
5549     if (tmp >= 0) {
5550       jdxm[numNewRows++] = tmp;
5551     }
5552   }
5553   ierr = MatZeroRowsLocal(mat,numNewRows,jdxm,diag,x,b);CHKERRQ(ierr);
5554   ierr = PetscFree(jdxm);CHKERRQ(ierr);
5555   PetscFunctionReturn(0);
5556 }
5557 
5558 #undef __FUNCT__
5559 #define __FUNCT__ "MatZeroRowsColumnsStencil"
5560 /*@C
5561    MatZeroRowsColumnsStencil - Zeros all row and column entries (except possibly the main diagonal)
5562    of a set of rows and columns of a matrix.
5563 
5564    Collective on Mat
5565 
5566    Input Parameters:
5567 +  mat - the matrix
5568 .  numRows - the number of rows/columns to remove
5569 .  rows - the grid coordinates (and component number when dof > 1) for matrix rows
5570 .  diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry)
5571 .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
5572 -  b - optional vector of right hand side, that will be adjusted by provided solution
5573 
5574    Notes:
5575    For the AIJ and BAIJ matrix formats this removes the old nonzero structure,
5576    but does not release memory.  For the dense and block diagonal
5577    formats this does not alter the nonzero structure.
5578 
5579    If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure
5580    of the matrix is not changed (even for AIJ and BAIJ matrices) the values are
5581    merely zeroed.
5582 
5583    The user can set a value in the diagonal entry (or for the AIJ and
5584    row formats can optionally remove the main diagonal entry from the
5585    nonzero structure as well, by passing 0.0 as the final argument).
5586 
5587    For the parallel case, all processes that share the matrix (i.e.,
5588    those in the communicator used for matrix creation) MUST call this
5589    routine, regardless of whether any rows being zeroed are owned by
5590    them.
5591 
5592    Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to
5593    list only rows local to itself, but the row/column numbers are given in local numbering).
5594 
5595    The grid coordinates are across the entire grid, not just the local portion
5596 
5597    In Fortran idxm and idxn should be declared as
5598 $     MatStencil idxm(4,m)
5599    and the values inserted using
5600 $    idxm(MatStencil_i,1) = i
5601 $    idxm(MatStencil_j,1) = j
5602 $    idxm(MatStencil_k,1) = k
5603 $    idxm(MatStencil_c,1) = c
5604    etc
5605 
5606    For periodic boundary conditions use negative indices for values to the left (below 0; that are to be
5607    obtained by wrapping values from right edge). For values to the right of the last entry using that index plus one
5608    etc to obtain values that obtained by wrapping the values from the left edge. This does not work for anything but the
5609    DMDA_BOUNDARY_PERIODIC boundary type.
5610 
5611    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
5612    a single value per point) you can skip filling those indices.
5613 
5614    Level: intermediate
5615 
5616    Concepts: matrices^zeroing rows
5617 
5618 .seealso: MatZeroRows(), MatZeroRowsIS(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption()
5619 @*/
5620 PetscErrorCode  MatZeroRowsColumnsStencil(Mat mat,PetscInt numRows,const MatStencil rows[],PetscScalar diag,Vec x,Vec b)
5621 {
5622   PetscInt       dim    = mat->stencil.dim;
5623   PetscInt       sdim   = dim - (1 - (PetscInt) mat->stencil.noc);
5624   PetscInt       *dims   = mat->stencil.dims+1;
5625   PetscInt       *starts = mat->stencil.starts;
5626   PetscInt       *dxm    = (PetscInt *) rows;
5627   PetscInt       *jdxm, i, j, tmp, numNewRows = 0;
5628   PetscErrorCode ierr;
5629 
5630   PetscFunctionBegin;
5631   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5632   PetscValidType(mat,1);
5633   if (numRows) PetscValidIntPointer(rows,3);
5634 
5635   ierr = PetscMalloc(numRows*sizeof(PetscInt), &jdxm);CHKERRQ(ierr);
5636   for (i = 0; i < numRows; ++i) {
5637     /* Skip unused dimensions (they are ordered k, j, i, c) */
5638     for (j = 0; j < 3-sdim; ++j) dxm++;
5639     /* Local index in X dir */
5640     tmp = *dxm++ - starts[0];
5641     /* Loop over remaining dimensions */
5642     for (j = 0; j < dim-1; ++j) {
5643       /* If nonlocal, set index to be negative */
5644       if ((*dxm++ - starts[j+1]) < 0 || tmp < 0) tmp = PETSC_MIN_INT;
5645       /* Update local index */
5646       else                                       tmp = tmp*dims[j] + *(dxm-1) - starts[j+1];
5647     }
5648     /* Skip component slot if necessary */
5649     if (mat->stencil.noc) dxm++;
5650     /* Local row number */
5651     if (tmp >= 0) {
5652       jdxm[numNewRows++] = tmp;
5653     }
5654   }
5655   ierr = MatZeroRowsColumnsLocal(mat,numNewRows,jdxm,diag,x,b);CHKERRQ(ierr);
5656   ierr = PetscFree(jdxm);CHKERRQ(ierr);
5657   PetscFunctionReturn(0);
5658 }
5659 
5660 #undef __FUNCT__
5661 #define __FUNCT__ "MatZeroRowsLocal"
5662 /*@C
5663    MatZeroRowsLocal - Zeros all entries (except possibly the main diagonal)
5664    of a set of rows of a matrix; using local numbering of rows.
5665 
5666    Collective on Mat
5667 
5668    Input Parameters:
5669 +  mat - the matrix
5670 .  numRows - the number of rows to remove
5671 .  rows - the global row indices
5672 .  diag - value put in all diagonals of eliminated rows
5673 .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
5674 -  b - optional vector of right hand side, that will be adjusted by provided solution
5675 
5676    Notes:
5677    Before calling MatZeroRowsLocal(), the user must first set the
5678    local-to-global mapping by calling MatSetLocalToGlobalMapping().
5679 
5680    For the AIJ matrix formats this removes the old nonzero structure,
5681    but does not release memory.  For the dense and block diagonal
5682    formats this does not alter the nonzero structure.
5683 
5684    If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure
5685    of the matrix is not changed (even for AIJ and BAIJ matrices) the values are
5686    merely zeroed.
5687 
5688    The user can set a value in the diagonal entry (or for the AIJ and
5689    row formats can optionally remove the main diagonal entry from the
5690    nonzero structure as well, by passing 0.0 as the final argument).
5691 
5692    You can call MatSetOption(mat,MAT_NO_OFF_PROC_ZERO_ROWS,PETSC_TRUE) if each process indicates only rows it
5693    owns that are to be zeroed. This saves a global synchronization in the implementation.
5694 
5695    Level: intermediate
5696 
5697    Concepts: matrices^zeroing
5698 
5699 .seealso: MatZeroRows(), MatZeroRowsLocalIS(), MatZeroEntries(), MatZeroRows(), MatSetLocalToGlobalMapping
5700 @*/
5701 PetscErrorCode  MatZeroRowsLocal(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
5702 {
5703   PetscErrorCode ierr;
5704   PetscMPIInt    size;
5705 
5706   PetscFunctionBegin;
5707   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5708   PetscValidType(mat,1);
5709   if (numRows) PetscValidIntPointer(rows,3);
5710   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
5711   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
5712   MatCheckPreallocated(mat,1);
5713 
5714   ierr = MPI_Comm_size(((PetscObject)mat)->comm,&size);CHKERRQ(ierr);
5715   if (mat->ops->zerorowslocal) {
5716     ierr = (*mat->ops->zerorowslocal)(mat,numRows,rows,diag,x,b);CHKERRQ(ierr);
5717   } else if (size == 1) {
5718     ierr = (*mat->ops->zerorows)(mat,numRows,rows,diag,x,b);CHKERRQ(ierr);
5719   } else {
5720     IS             is, newis;
5721     const PetscInt *newRows;
5722 
5723     if (!mat->rmap->mapping) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide local to global mapping to matrix first");
5724     ierr = ISCreateGeneral(PETSC_COMM_SELF,numRows,rows,PETSC_COPY_VALUES,&is);CHKERRQ(ierr);
5725     ierr = ISLocalToGlobalMappingApplyIS(mat->rmap->mapping,is,&newis);CHKERRQ(ierr);
5726     ierr = ISGetIndices(newis,&newRows);CHKERRQ(ierr);
5727     ierr = (*mat->ops->zerorows)(mat,numRows,newRows,diag,x,b);CHKERRQ(ierr);
5728     ierr = ISRestoreIndices(newis,&newRows);CHKERRQ(ierr);
5729     ierr = ISDestroy(&newis);CHKERRQ(ierr);
5730     ierr = ISDestroy(&is);CHKERRQ(ierr);
5731   }
5732   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
5733 #if defined(PETSC_HAVE_CUSP)
5734   if (mat->valid_GPU_matrix != PETSC_CUSP_UNALLOCATED) {
5735     mat->valid_GPU_matrix = PETSC_CUSP_CPU;
5736   }
5737 #endif
5738   PetscFunctionReturn(0);
5739 }
5740 
5741 #undef __FUNCT__
5742 #define __FUNCT__ "MatZeroRowsLocalIS"
5743 /*@C
5744    MatZeroRowsLocalIS - Zeros all entries (except possibly the main diagonal)
5745    of a set of rows of a matrix; using local numbering of rows.
5746 
5747    Collective on Mat
5748 
5749    Input Parameters:
5750 +  mat - the matrix
5751 .  is - index set of rows to remove
5752 .  diag - value put in all diagonals of eliminated rows
5753 .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
5754 -  b - optional vector of right hand side, that will be adjusted by provided solution
5755 
5756    Notes:
5757    Before calling MatZeroRowsLocalIS(), the user must first set the
5758    local-to-global mapping by calling MatSetLocalToGlobalMapping().
5759 
5760    For the AIJ matrix formats this removes the old nonzero structure,
5761    but does not release memory.  For the dense and block diagonal
5762    formats this does not alter the nonzero structure.
5763 
5764    If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure
5765    of the matrix is not changed (even for AIJ and BAIJ matrices) the values are
5766    merely zeroed.
5767 
5768    The user can set a value in the diagonal entry (or for the AIJ and
5769    row formats can optionally remove the main diagonal entry from the
5770    nonzero structure as well, by passing 0.0 as the final argument).
5771 
5772    You can call MatSetOption(mat,MAT_NO_OFF_PROC_ZERO_ROWS,PETSC_TRUE) if each process indicates only rows it
5773    owns that are to be zeroed. This saves a global synchronization in the implementation.
5774 
5775    Level: intermediate
5776 
5777    Concepts: matrices^zeroing
5778 
5779 .seealso: MatZeroRows(), MatZeroRowsLocal(), MatZeroEntries(), MatZeroRows(), MatSetLocalToGlobalMapping
5780 @*/
5781 PetscErrorCode  MatZeroRowsLocalIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b)
5782 {
5783   PetscErrorCode ierr;
5784   PetscInt       numRows;
5785   const PetscInt *rows;
5786 
5787   PetscFunctionBegin;
5788   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5789   PetscValidType(mat,1);
5790   PetscValidHeaderSpecific(is,IS_CLASSID,2);
5791   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
5792   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
5793   MatCheckPreallocated(mat,1);
5794 
5795   ierr = ISGetLocalSize(is,&numRows);CHKERRQ(ierr);
5796   ierr = ISGetIndices(is,&rows);CHKERRQ(ierr);
5797   ierr = MatZeroRowsLocal(mat,numRows,rows,diag,x,b);CHKERRQ(ierr);
5798   ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr);
5799   PetscFunctionReturn(0);
5800 }
5801 
5802 #undef __FUNCT__
5803 #define __FUNCT__ "MatZeroRowsColumnsLocal"
5804 /*@C
5805    MatZeroRowsColumnsLocal - Zeros all entries (except possibly the main diagonal)
5806    of a set of rows and columns of a matrix; using local numbering of rows.
5807 
5808    Collective on Mat
5809 
5810    Input Parameters:
5811 +  mat - the matrix
5812 .  numRows - the number of rows to remove
5813 .  rows - the global row indices
5814 .  diag - value put in all diagonals of eliminated rows
5815 .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
5816 -  b - optional vector of right hand side, that will be adjusted by provided solution
5817 
5818    Notes:
5819    Before calling MatZeroRowsColumnsLocal(), the user must first set the
5820    local-to-global mapping by calling MatSetLocalToGlobalMapping().
5821 
5822    The user can set a value in the diagonal entry (or for the AIJ and
5823    row formats can optionally remove the main diagonal entry from the
5824    nonzero structure as well, by passing 0.0 as the final argument).
5825 
5826    Level: intermediate
5827 
5828    Concepts: matrices^zeroing
5829 
5830 .seealso: MatZeroRows(), MatZeroRowsLocalIS(), MatZeroEntries(), MatZeroRows(), MatSetLocalToGlobalMapping
5831 @*/
5832 PetscErrorCode  MatZeroRowsColumnsLocal(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
5833 {
5834   PetscErrorCode ierr;
5835   PetscMPIInt    size;
5836 
5837   PetscFunctionBegin;
5838   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5839   PetscValidType(mat,1);
5840   if (numRows) PetscValidIntPointer(rows,3);
5841   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
5842   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
5843   MatCheckPreallocated(mat,1);
5844 
5845   ierr = MPI_Comm_size(((PetscObject)mat)->comm,&size);CHKERRQ(ierr);
5846   if (size == 1) {
5847     ierr = (*mat->ops->zerorowscolumns)(mat,numRows,rows,diag,x,b);CHKERRQ(ierr);
5848   } else {
5849     IS             is, newis;
5850     const PetscInt *newRows;
5851 
5852     if (!mat->cmap->mapping) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide local to global mapping to matrix first");
5853     ierr = ISCreateGeneral(PETSC_COMM_SELF,numRows,rows,PETSC_COPY_VALUES,&is);CHKERRQ(ierr);
5854     ierr = ISLocalToGlobalMappingApplyIS(mat->cmap->mapping,is,&newis);CHKERRQ(ierr);
5855     ierr = ISGetIndices(newis,&newRows);CHKERRQ(ierr);
5856     ierr = (*mat->ops->zerorowscolumns)(mat,numRows,newRows,diag,x,b);CHKERRQ(ierr);
5857     ierr = ISRestoreIndices(newis,&newRows);CHKERRQ(ierr);
5858     ierr = ISDestroy(&newis);CHKERRQ(ierr);
5859     ierr = ISDestroy(&is);CHKERRQ(ierr);
5860   }
5861   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
5862 #if defined(PETSC_HAVE_CUSP)
5863   if (mat->valid_GPU_matrix != PETSC_CUSP_UNALLOCATED) {
5864     mat->valid_GPU_matrix = PETSC_CUSP_CPU;
5865   }
5866 #endif
5867   PetscFunctionReturn(0);
5868 }
5869 
5870 #undef __FUNCT__
5871 #define __FUNCT__ "MatZeroRowsColumnsLocalIS"
5872 /*@C
5873    MatZeroRowsColumnsLocalIS - Zeros all entries (except possibly the main diagonal)
5874    of a set of rows and columns of a matrix; using local numbering of rows.
5875 
5876    Collective on Mat
5877 
5878    Input Parameters:
5879 +  mat - the matrix
5880 .  is - index set of rows to remove
5881 .  diag - value put in all diagonals of eliminated rows
5882 .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
5883 -  b - optional vector of right hand side, that will be adjusted by provided solution
5884 
5885    Notes:
5886    Before calling MatZeroRowsColumnsLocalIS(), the user must first set the
5887    local-to-global mapping by calling MatSetLocalToGlobalMapping().
5888 
5889    The user can set a value in the diagonal entry (or for the AIJ and
5890    row formats can optionally remove the main diagonal entry from the
5891    nonzero structure as well, by passing 0.0 as the final argument).
5892 
5893    Level: intermediate
5894 
5895    Concepts: matrices^zeroing
5896 
5897 .seealso: MatZeroRows(), MatZeroRowsLocal(), MatZeroEntries(), MatZeroRows(), MatSetLocalToGlobalMapping
5898 @*/
5899 PetscErrorCode  MatZeroRowsColumnsLocalIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b)
5900 {
5901   PetscErrorCode ierr;
5902   PetscInt       numRows;
5903   const PetscInt *rows;
5904 
5905   PetscFunctionBegin;
5906   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5907   PetscValidType(mat,1);
5908   PetscValidHeaderSpecific(is,IS_CLASSID,2);
5909   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
5910   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
5911   MatCheckPreallocated(mat,1);
5912 
5913   ierr = ISGetLocalSize(is,&numRows);CHKERRQ(ierr);
5914   ierr = ISGetIndices(is,&rows);CHKERRQ(ierr);
5915   ierr = MatZeroRowsColumnsLocal(mat,numRows,rows,diag,x,b);CHKERRQ(ierr);
5916   ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr);
5917   PetscFunctionReturn(0);
5918 }
5919 
5920 #undef __FUNCT__
5921 #define __FUNCT__ "MatGetSize"
5922 /*@
5923    MatGetSize - Returns the numbers of rows and columns in a matrix.
5924 
5925    Not Collective
5926 
5927    Input Parameter:
5928 .  mat - the matrix
5929 
5930    Output Parameters:
5931 +  m - the number of global rows
5932 -  n - the number of global columns
5933 
5934    Note: both output parameters can be PETSC_NULL on input.
5935 
5936    Level: beginner
5937 
5938    Concepts: matrices^size
5939 
5940 .seealso: MatGetLocalSize()
5941 @*/
5942 PetscErrorCode  MatGetSize(Mat mat,PetscInt *m,PetscInt *n)
5943 {
5944   PetscFunctionBegin;
5945   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5946   if (m) *m = mat->rmap->N;
5947   if (n) *n = mat->cmap->N;
5948   PetscFunctionReturn(0);
5949 }
5950 
5951 #undef __FUNCT__
5952 #define __FUNCT__ "MatGetLocalSize"
5953 /*@
5954    MatGetLocalSize - Returns the number of rows and columns in a matrix
5955    stored locally.  This information may be implementation dependent, so
5956    use with care.
5957 
5958    Not Collective
5959 
5960    Input Parameters:
5961 .  mat - the matrix
5962 
5963    Output Parameters:
5964 +  m - the number of local rows
5965 -  n - the number of local columns
5966 
5967    Note: both output parameters can be PETSC_NULL on input.
5968 
5969    Level: beginner
5970 
5971    Concepts: matrices^local size
5972 
5973 .seealso: MatGetSize()
5974 @*/
5975 PetscErrorCode  MatGetLocalSize(Mat mat,PetscInt *m,PetscInt *n)
5976 {
5977   PetscFunctionBegin;
5978   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5979   if (m) PetscValidIntPointer(m,2);
5980   if (n) PetscValidIntPointer(n,3);
5981   if (m) *m = mat->rmap->n;
5982   if (n) *n = mat->cmap->n;
5983   PetscFunctionReturn(0);
5984 }
5985 
5986 #undef __FUNCT__
5987 #define __FUNCT__ "MatGetOwnershipRangeColumn"
5988 /*@
5989    MatGetOwnershipRangeColumn - Returns the range of matrix columns associated with rows of a vector one multiplies by that owned by
5990    this processor. (The columns of the "diagonal block")
5991 
5992    Not Collective, unless matrix has not been allocated, then collective on Mat
5993 
5994    Input Parameters:
5995 .  mat - the matrix
5996 
5997    Output Parameters:
5998 +  m - the global index of the first local column
5999 -  n - one more than the global index of the last local column
6000 
6001    Notes: both output parameters can be PETSC_NULL on input.
6002 
6003    Level: developer
6004 
6005    Concepts: matrices^column ownership
6006 
6007 .seealso:  MatGetOwnershipRange(), MatGetOwnershipRanges(), MatGetOwnershipRangesColumn()
6008 
6009 @*/
6010 PetscErrorCode  MatGetOwnershipRangeColumn(Mat mat,PetscInt *m,PetscInt *n)
6011 {
6012   PetscFunctionBegin;
6013   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6014   PetscValidType(mat,1);
6015   if (m) PetscValidIntPointer(m,2);
6016   if (n) PetscValidIntPointer(n,3);
6017   MatCheckPreallocated(mat,1);
6018   if (m) *m = mat->cmap->rstart;
6019   if (n) *n = mat->cmap->rend;
6020   PetscFunctionReturn(0);
6021 }
6022 
6023 #undef __FUNCT__
6024 #define __FUNCT__ "MatGetOwnershipRange"
6025 /*@
6026    MatGetOwnershipRange - Returns the range of matrix rows owned by
6027    this processor, assuming that the matrix is laid out with the first
6028    n1 rows on the first processor, the next n2 rows on the second, etc.
6029    For certain parallel layouts this range may not be well defined.
6030 
6031    Not Collective, unless matrix has not been allocated, then collective on Mat
6032 
6033    Input Parameters:
6034 .  mat - the matrix
6035 
6036    Output Parameters:
6037 +  m - the global index of the first local row
6038 -  n - one more than the global index of the last local row
6039 
6040    Note: both output parameters can be PETSC_NULL on input.
6041 
6042    Level: beginner
6043 
6044    Concepts: matrices^row ownership
6045 
6046 .seealso:   MatGetOwnershipRanges(), MatGetOwnershipRangeColumn(), MatGetOwnershipRangesColumn()
6047 
6048 @*/
6049 PetscErrorCode  MatGetOwnershipRange(Mat mat,PetscInt *m,PetscInt *n)
6050 {
6051   PetscFunctionBegin;
6052   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6053   PetscValidType(mat,1);
6054   if (m) PetscValidIntPointer(m,2);
6055   if (n) PetscValidIntPointer(n,3);
6056   MatCheckPreallocated(mat,1);
6057   if (m) *m = mat->rmap->rstart;
6058   if (n) *n = mat->rmap->rend;
6059   PetscFunctionReturn(0);
6060 }
6061 
6062 #undef __FUNCT__
6063 #define __FUNCT__ "MatGetOwnershipRanges"
6064 /*@C
6065    MatGetOwnershipRanges - Returns the range of matrix rows owned by
6066    each process
6067 
6068    Not Collective, unless matrix has not been allocated, then collective on Mat
6069 
6070    Input Parameters:
6071 .  mat - the matrix
6072 
6073    Output Parameters:
6074 .  ranges - start of each processors portion plus one more then the total length at the end
6075 
6076    Level: beginner
6077 
6078    Concepts: matrices^row ownership
6079 
6080 .seealso:   MatGetOwnershipRange(), MatGetOwnershipRangeColumn(), MatGetOwnershipRangesColumn()
6081 
6082 @*/
6083 PetscErrorCode  MatGetOwnershipRanges(Mat mat,const PetscInt **ranges)
6084 {
6085   PetscErrorCode ierr;
6086 
6087   PetscFunctionBegin;
6088   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6089   PetscValidType(mat,1);
6090   MatCheckPreallocated(mat,1);
6091   ierr = PetscLayoutGetRanges(mat->rmap,ranges);CHKERRQ(ierr);
6092   PetscFunctionReturn(0);
6093 }
6094 
6095 #undef __FUNCT__
6096 #define __FUNCT__ "MatGetOwnershipRangesColumn"
6097 /*@C
6098    MatGetOwnershipRangesColumn - Returns the range of matrix columns associated with rows of a vector one multiplies by that owned by
6099    this processor. (The columns of the "diagonal blocks" for each process)
6100 
6101    Not Collective, unless matrix has not been allocated, then collective on Mat
6102 
6103    Input Parameters:
6104 .  mat - the matrix
6105 
6106    Output Parameters:
6107 .  ranges - start of each processors portion plus one more then the total length at the end
6108 
6109    Level: beginner
6110 
6111    Concepts: matrices^column ownership
6112 
6113 .seealso:   MatGetOwnershipRange(), MatGetOwnershipRangeColumn(), MatGetOwnershipRanges()
6114 
6115 @*/
6116 PetscErrorCode  MatGetOwnershipRangesColumn(Mat mat,const PetscInt **ranges)
6117 {
6118   PetscErrorCode ierr;
6119 
6120   PetscFunctionBegin;
6121   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6122   PetscValidType(mat,1);
6123   MatCheckPreallocated(mat,1);
6124   ierr = PetscLayoutGetRanges(mat->cmap,ranges);CHKERRQ(ierr);
6125   PetscFunctionReturn(0);
6126 }
6127 
6128 #undef __FUNCT__
6129 #define __FUNCT__ "MatGetOwnershipIS"
6130 /*@C
6131    MatGetOwnershipIS - Get row and column ownership as index sets
6132 
6133    Not Collective
6134 
6135    Input Arguments:
6136 .  A - matrix of type Elemental
6137 
6138    Output Arguments:
6139 +  rows - rows in which this process owns elements
6140 .  cols - columns in which this process owns elements
6141 
6142    Level: intermediate
6143 
6144 .seealso: MatGetOwnershipRange(), MatGetOwnershipRangeColumn(), MatSetValues(), MATELEMENTAL, MatSetValues()
6145 @*/
6146 PetscErrorCode MatGetOwnershipIS(Mat A,IS *rows,IS *cols)
6147 {
6148   PetscErrorCode ierr,(*f)(Mat,IS*,IS*);
6149 
6150   PetscFunctionBegin;
6151   MatCheckPreallocated(A,1);
6152   ierr = PetscObjectQueryFunction((PetscObject)A,"MatGetOwnershipIS_C",(PetscVoidStarFunction)&f);CHKERRQ(ierr);
6153   if (f) {
6154     ierr = (*f)(A,rows,cols);CHKERRQ(ierr);
6155   } else {   /* Create a standard row-based partition, each process is responsible for ALL columns in their row block */
6156     if (rows) {ierr = ISCreateStride(PETSC_COMM_SELF,A->rmap->n,A->rmap->rstart,1,rows);CHKERRQ(ierr);}
6157     if (cols) {ierr = ISCreateStride(PETSC_COMM_SELF,A->cmap->N,0,1,cols);CHKERRQ(ierr);}
6158   }
6159   PetscFunctionReturn(0);
6160 }
6161 
6162 #undef __FUNCT__
6163 #define __FUNCT__ "MatILUFactorSymbolic"
6164 /*@C
6165    MatILUFactorSymbolic - Performs symbolic ILU factorization of a matrix.
6166    Uses levels of fill only, not drop tolerance. Use MatLUFactorNumeric()
6167    to complete the factorization.
6168 
6169    Collective on Mat
6170 
6171    Input Parameters:
6172 +  mat - the matrix
6173 .  row - row permutation
6174 .  column - column permutation
6175 -  info - structure containing
6176 $      levels - number of levels of fill.
6177 $      expected fill - as ratio of original fill.
6178 $      1 or 0 - indicating force fill on diagonal (improves robustness for matrices
6179                 missing diagonal entries)
6180 
6181    Output Parameters:
6182 .  fact - new matrix that has been symbolically factored
6183 
6184    Notes:
6185    See the <a href="../../docs/manual.pdf">users manual</a>  for additional information about
6186    choosing the fill factor for better efficiency.
6187 
6188    Most users should employ the simplified KSP interface for linear solvers
6189    instead of working directly with matrix algebra routines such as this.
6190    See, e.g., KSPCreate().
6191 
6192    Level: developer
6193 
6194   Concepts: matrices^symbolic LU factorization
6195   Concepts: matrices^factorization
6196   Concepts: LU^symbolic factorization
6197 
6198 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor()
6199           MatGetOrdering(), MatFactorInfo
6200 
6201     Developer Note: fortran interface is not autogenerated as the f90
6202     interface defintion cannot be generated correctly [due to MatFactorInfo]
6203 
6204 @*/
6205 PetscErrorCode  MatILUFactorSymbolic(Mat fact,Mat mat,IS row,IS col,const MatFactorInfo *info)
6206 {
6207   PetscErrorCode ierr;
6208 
6209   PetscFunctionBegin;
6210   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6211   PetscValidType(mat,1);
6212   PetscValidHeaderSpecific(row,IS_CLASSID,2);
6213   PetscValidHeaderSpecific(col,IS_CLASSID,3);
6214   PetscValidPointer(info,4);
6215   PetscValidPointer(fact,5);
6216   if (info->levels < 0) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Levels of fill negative %D",(PetscInt)info->levels);
6217   if (info->fill < 1.0) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %G",info->fill);
6218   if (!(fact)->ops->ilufactorsymbolic) {
6219     const MatSolverPackage spackage;
6220     ierr = MatFactorGetSolverPackage(fact,&spackage);CHKERRQ(ierr);
6221     SETERRQ2(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Matrix type %s symbolic ILU using solver package %s",((PetscObject)mat)->type_name,spackage);
6222   }
6223   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
6224   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6225   MatCheckPreallocated(mat,2);
6226 
6227   ierr = PetscLogEventBegin(MAT_ILUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr);
6228   ierr = (fact->ops->ilufactorsymbolic)(fact,mat,row,col,info);CHKERRQ(ierr);
6229   ierr = PetscLogEventEnd(MAT_ILUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr);
6230   PetscFunctionReturn(0);
6231 }
6232 
6233 #undef __FUNCT__
6234 #define __FUNCT__ "MatICCFactorSymbolic"
6235 /*@C
6236    MatICCFactorSymbolic - Performs symbolic incomplete
6237    Cholesky factorization for a symmetric matrix.  Use
6238    MatCholeskyFactorNumeric() to complete the factorization.
6239 
6240    Collective on Mat
6241 
6242    Input Parameters:
6243 +  mat - the matrix
6244 .  perm - row and column permutation
6245 -  info - structure containing
6246 $      levels - number of levels of fill.
6247 $      expected fill - as ratio of original fill.
6248 
6249    Output Parameter:
6250 .  fact - the factored matrix
6251 
6252    Notes:
6253    Most users should employ the KSP interface for linear solvers
6254    instead of working directly with matrix algebra routines such as this.
6255    See, e.g., KSPCreate().
6256 
6257    Level: developer
6258 
6259   Concepts: matrices^symbolic incomplete Cholesky factorization
6260   Concepts: matrices^factorization
6261   Concepts: Cholsky^symbolic factorization
6262 
6263 .seealso: MatCholeskyFactorNumeric(), MatCholeskyFactor(), MatFactorInfo
6264 
6265     Developer Note: fortran interface is not autogenerated as the f90
6266     interface defintion cannot be generated correctly [due to MatFactorInfo]
6267 
6268 @*/
6269 PetscErrorCode  MatICCFactorSymbolic(Mat fact,Mat mat,IS perm,const MatFactorInfo *info)
6270 {
6271   PetscErrorCode ierr;
6272 
6273   PetscFunctionBegin;
6274   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6275   PetscValidType(mat,1);
6276   PetscValidHeaderSpecific(perm,IS_CLASSID,2);
6277   PetscValidPointer(info,3);
6278   PetscValidPointer(fact,4);
6279   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6280   if (info->levels < 0) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Levels negative %D",(PetscInt) info->levels);
6281   if (info->fill < 1.0) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %G",info->fill);
6282   if (!(fact)->ops->iccfactorsymbolic) {
6283     const MatSolverPackage spackage;
6284     ierr = MatFactorGetSolverPackage(fact,&spackage);CHKERRQ(ierr);
6285     SETERRQ2(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Matrix type %s symbolic ICC using solver package %s",((PetscObject)mat)->type_name,spackage);
6286   }
6287   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
6288   MatCheckPreallocated(mat,2);
6289 
6290   ierr = PetscLogEventBegin(MAT_ICCFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr);
6291   ierr = (fact->ops->iccfactorsymbolic)(fact,mat,perm,info);CHKERRQ(ierr);
6292   ierr = PetscLogEventEnd(MAT_ICCFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr);
6293   PetscFunctionReturn(0);
6294 }
6295 
6296 #undef __FUNCT__
6297 #define __FUNCT__ "MatGetSubMatrices"
6298 /*@C
6299    MatGetSubMatrices - Extracts several submatrices from a matrix. If submat
6300    points to an array of valid matrices, they may be reused to store the new
6301    submatrices.
6302 
6303    Collective on Mat
6304 
6305    Input Parameters:
6306 +  mat - the matrix
6307 .  n   - the number of submatrixes to be extracted (on this processor, may be zero)
6308 .  irow, icol - index sets of rows and columns to extract (must be sorted)
6309 -  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
6310 
6311    Output Parameter:
6312 .  submat - the array of submatrices
6313 
6314    Notes:
6315    MatGetSubMatrices() can extract ONLY sequential submatrices
6316    (from both sequential and parallel matrices). Use MatGetSubMatrix()
6317    to extract a parallel submatrix.
6318 
6319    Currently both row and column indices must be sorted to guarantee
6320    correctness with all matrix types.
6321 
6322    When extracting submatrices from a parallel matrix, each processor can
6323    form a different submatrix by setting the rows and columns of its
6324    individual index sets according to the local submatrix desired.
6325 
6326    When finished using the submatrices, the user should destroy
6327    them with MatDestroyMatrices().
6328 
6329    MAT_REUSE_MATRIX can only be used when the nonzero structure of the
6330    original matrix has not changed from that last call to MatGetSubMatrices().
6331 
6332    This routine creates the matrices in submat; you should NOT create them before
6333    calling it. It also allocates the array of matrix pointers submat.
6334 
6335    For BAIJ matrices the index sets must respect the block structure, that is if they
6336    request one row/column in a block, they must request all rows/columns that are in
6337    that block. For example, if the block size is 2 you cannot request just row 0 and
6338    column 0.
6339 
6340    Fortran Note:
6341    The Fortran interface is slightly different from that given below; it
6342    requires one to pass in  as submat a Mat (integer) array of size at least m.
6343 
6344    Level: advanced
6345 
6346    Concepts: matrices^accessing submatrices
6347    Concepts: submatrices
6348 
6349 .seealso: MatDestroyMatrices(), MatGetSubMatrix(), MatGetRow(), MatGetDiagonal(), MatReuse
6350 @*/
6351 PetscErrorCode  MatGetSubMatrices(Mat mat,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *submat[])
6352 {
6353   PetscErrorCode ierr;
6354   PetscInt       i;
6355   PetscBool      eq;
6356 
6357   PetscFunctionBegin;
6358   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6359   PetscValidType(mat,1);
6360   if (n) {
6361     PetscValidPointer(irow,3);
6362     PetscValidHeaderSpecific(*irow,IS_CLASSID,3);
6363     PetscValidPointer(icol,4);
6364     PetscValidHeaderSpecific(*icol,IS_CLASSID,4);
6365   }
6366   PetscValidPointer(submat,6);
6367   if (n && scall == MAT_REUSE_MATRIX) {
6368     PetscValidPointer(*submat,6);
6369     PetscValidHeaderSpecific(**submat,MAT_CLASSID,6);
6370   }
6371   if (!mat->ops->getsubmatrices) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
6372   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
6373   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6374   MatCheckPreallocated(mat,1);
6375 
6376   ierr = PetscLogEventBegin(MAT_GetSubMatrices,mat,0,0,0);CHKERRQ(ierr);
6377   ierr = (*mat->ops->getsubmatrices)(mat,n,irow,icol,scall,submat);CHKERRQ(ierr);
6378   ierr = PetscLogEventEnd(MAT_GetSubMatrices,mat,0,0,0);CHKERRQ(ierr);
6379   for (i=0; i<n; i++) {
6380     if (mat->symmetric || mat->structurally_symmetric || mat->hermitian) {
6381       ierr = ISEqual(irow[i],icol[i],&eq);CHKERRQ(ierr);
6382       if (eq) {
6383 	if (mat->symmetric) {
6384 	  ierr = MatSetOption((*submat)[i],MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr);
6385 	} else if (mat->hermitian) {
6386 	  ierr = MatSetOption((*submat)[i],MAT_HERMITIAN,PETSC_TRUE);CHKERRQ(ierr);
6387 	} else if (mat->structurally_symmetric) {
6388 	  ierr = MatSetOption((*submat)[i],MAT_STRUCTURALLY_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr);
6389 	}
6390       }
6391     }
6392   }
6393   PetscFunctionReturn(0);
6394 }
6395 
6396 #undef __FUNCT__
6397 #define __FUNCT__ "MatGetSubMatricesParallel"
6398 PetscErrorCode  MatGetSubMatricesParallel(Mat mat,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *submat[])
6399 {
6400   PetscErrorCode ierr;
6401   PetscInt       i;
6402   PetscBool      eq;
6403 
6404   PetscFunctionBegin;
6405   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6406   PetscValidType(mat,1);
6407   if (n) {
6408     PetscValidPointer(irow,3);
6409     PetscValidHeaderSpecific(*irow,IS_CLASSID,3);
6410     PetscValidPointer(icol,4);
6411     PetscValidHeaderSpecific(*icol,IS_CLASSID,4);
6412   }
6413   PetscValidPointer(submat,6);
6414   if (n && scall == MAT_REUSE_MATRIX) {
6415     PetscValidPointer(*submat,6);
6416     PetscValidHeaderSpecific(**submat,MAT_CLASSID,6);
6417   }
6418   if (!mat->ops->getsubmatricesparallel) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
6419   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
6420   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6421   MatCheckPreallocated(mat,1);
6422 
6423   ierr = PetscLogEventBegin(MAT_GetSubMatrices,mat,0,0,0);CHKERRQ(ierr);
6424   ierr = (*mat->ops->getsubmatricesparallel)(mat,n,irow,icol,scall,submat);CHKERRQ(ierr);
6425   ierr = PetscLogEventEnd(MAT_GetSubMatrices,mat,0,0,0);CHKERRQ(ierr);
6426   for (i=0; i<n; i++) {
6427     if (mat->symmetric || mat->structurally_symmetric || mat->hermitian) {
6428       ierr = ISEqual(irow[i],icol[i],&eq);CHKERRQ(ierr);
6429       if (eq) {
6430 	if (mat->symmetric) {
6431 	  ierr = MatSetOption((*submat)[i],MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr);
6432 	} else if (mat->hermitian) {
6433 	  ierr = MatSetOption((*submat)[i],MAT_HERMITIAN,PETSC_TRUE);CHKERRQ(ierr);
6434 	} else if (mat->structurally_symmetric) {
6435 	  ierr = MatSetOption((*submat)[i],MAT_STRUCTURALLY_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr);
6436 	}
6437       }
6438     }
6439   }
6440   PetscFunctionReturn(0);
6441 }
6442 
6443 #undef __FUNCT__
6444 #define __FUNCT__ "MatDestroyMatrices"
6445 /*@C
6446    MatDestroyMatrices - Destroys a set of matrices obtained with MatGetSubMatrices().
6447 
6448    Collective on Mat
6449 
6450    Input Parameters:
6451 +  n - the number of local matrices
6452 -  mat - the matrices (note that this is a pointer to the array of matrices, just to match the calling
6453                        sequence of MatGetSubMatrices())
6454 
6455    Level: advanced
6456 
6457     Notes: Frees not only the matrices, but also the array that contains the matrices
6458            In Fortran will not free the array.
6459 
6460 .seealso: MatGetSubMatrices()
6461 @*/
6462 PetscErrorCode  MatDestroyMatrices(PetscInt n,Mat *mat[])
6463 {
6464   PetscErrorCode ierr;
6465   PetscInt       i;
6466 
6467   PetscFunctionBegin;
6468   if (!*mat) PetscFunctionReturn(0);
6469   if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Trying to destroy negative number of matrices %D",n);
6470   PetscValidPointer(mat,2);
6471   for (i=0; i<n; i++) {
6472     ierr = MatDestroy(&(*mat)[i]);CHKERRQ(ierr);
6473   }
6474   /* memory is allocated even if n = 0 */
6475   ierr = PetscFree(*mat);CHKERRQ(ierr);
6476   *mat = PETSC_NULL;
6477   PetscFunctionReturn(0);
6478 }
6479 
6480 #undef __FUNCT__
6481 #define __FUNCT__ "MatGetSeqNonzeroStructure"
6482 /*@C
6483    MatGetSeqNonzeroStructure - Extracts the sequential nonzero structure from a matrix.
6484 
6485    Collective on Mat
6486 
6487    Input Parameters:
6488 .  mat - the matrix
6489 
6490    Output Parameter:
6491 .  matstruct - the sequential matrix with the nonzero structure of mat
6492 
6493   Level: intermediate
6494 
6495 .seealso: MatDestroySeqNonzeroStructure(), MatGetSubMatrices(), MatDestroyMatrices()
6496 @*/
6497 PetscErrorCode  MatGetSeqNonzeroStructure(Mat mat,Mat *matstruct)
6498 {
6499   PetscErrorCode ierr;
6500 
6501   PetscFunctionBegin;
6502   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6503   PetscValidPointer(matstruct,2);
6504 
6505   PetscValidType(mat,1);
6506   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6507   MatCheckPreallocated(mat,1);
6508 
6509   if (!mat->ops->getseqnonzerostructure) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Not for matrix type %s\n",((PetscObject)mat)->type_name);
6510   ierr = PetscLogEventBegin(MAT_GetSeqNonzeroStructure,mat,0,0,0);CHKERRQ(ierr);
6511   ierr = (*mat->ops->getseqnonzerostructure)(mat,matstruct);CHKERRQ(ierr);
6512   ierr = PetscLogEventEnd(MAT_GetSeqNonzeroStructure,mat,0,0,0);CHKERRQ(ierr);
6513   PetscFunctionReturn(0);
6514 }
6515 
6516 #undef __FUNCT__
6517 #define __FUNCT__ "MatDestroySeqNonzeroStructure"
6518 /*@C
6519    MatDestroySeqNonzeroStructure - Destroys matrix obtained with MatGetSeqNonzeroStructure().
6520 
6521    Collective on Mat
6522 
6523    Input Parameters:
6524 .  mat - the matrix (note that this is a pointer to the array of matrices, just to match the calling
6525                        sequence of MatGetSequentialNonzeroStructure())
6526 
6527    Level: advanced
6528 
6529     Notes: Frees not only the matrices, but also the array that contains the matrices
6530 
6531 .seealso: MatGetSeqNonzeroStructure()
6532 @*/
6533 PetscErrorCode  MatDestroySeqNonzeroStructure(Mat *mat)
6534 {
6535   PetscErrorCode ierr;
6536 
6537   PetscFunctionBegin;
6538   PetscValidPointer(mat,1);
6539   ierr = MatDestroy(mat);CHKERRQ(ierr);
6540   PetscFunctionReturn(0);
6541 }
6542 
6543 #undef __FUNCT__
6544 #define __FUNCT__ "MatIncreaseOverlap"
6545 /*@
6546    MatIncreaseOverlap - Given a set of submatrices indicated by index sets,
6547    replaces the index sets by larger ones that represent submatrices with
6548    additional overlap.
6549 
6550    Collective on Mat
6551 
6552    Input Parameters:
6553 +  mat - the matrix
6554 .  n   - the number of index sets
6555 .  is  - the array of index sets (these index sets will changed during the call)
6556 -  ov  - the additional overlap requested
6557 
6558    Level: developer
6559 
6560    Concepts: overlap
6561    Concepts: ASM^computing overlap
6562 
6563 .seealso: MatGetSubMatrices()
6564 @*/
6565 PetscErrorCode  MatIncreaseOverlap(Mat mat,PetscInt n,IS is[],PetscInt ov)
6566 {
6567   PetscErrorCode ierr;
6568 
6569   PetscFunctionBegin;
6570   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6571   PetscValidType(mat,1);
6572   if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Must have one or more domains, you have %D",n);
6573   if (n) {
6574     PetscValidPointer(is,3);
6575     PetscValidHeaderSpecific(*is,IS_CLASSID,3);
6576   }
6577   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
6578   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6579   MatCheckPreallocated(mat,1);
6580 
6581   if (!ov) PetscFunctionReturn(0);
6582   if (!mat->ops->increaseoverlap) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
6583   ierr = PetscLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr);
6584   ierr = (*mat->ops->increaseoverlap)(mat,n,is,ov);CHKERRQ(ierr);
6585   ierr = PetscLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr);
6586   PetscFunctionReturn(0);
6587 }
6588 
6589 #undef __FUNCT__
6590 #define __FUNCT__ "MatGetBlockSize"
6591 /*@
6592    MatGetBlockSize - Returns the matrix block size; useful especially for the
6593    block row and block diagonal formats.
6594 
6595    Not Collective
6596 
6597    Input Parameter:
6598 .  mat - the matrix
6599 
6600    Output Parameter:
6601 .  bs - block size
6602 
6603    Notes:
6604    Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ
6605 
6606    Level: intermediate
6607 
6608    Concepts: matrices^block size
6609 
6610 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSizes()
6611 @*/
6612 PetscErrorCode  MatGetBlockSize(Mat mat,PetscInt *bs)
6613 {
6614   PetscFunctionBegin;
6615   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6616   PetscValidType(mat,1);
6617   PetscValidIntPointer(bs,2);
6618   MatCheckPreallocated(mat,1);
6619   *bs = mat->rmap->bs;
6620   PetscFunctionReturn(0);
6621 }
6622 
6623 #undef __FUNCT__
6624 #define __FUNCT__ "MatGetBlockSizes"
6625 /*@
6626    MatGetBlockSizes - Returns the matrix block row and column sizes;
6627    useful especially for the block row and block diagonal formats.
6628 
6629    Not Collective
6630 
6631    Input Parameter:
6632 .  mat - the matrix
6633 
6634    Output Parameter:
6635 .  rbs - row block size
6636 .  cbs - coumn block size
6637 
6638    Notes:
6639    Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ
6640 
6641    Level: intermediate
6642 
6643    Concepts: matrices^block size
6644 
6645 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize()
6646 @*/
6647 PetscErrorCode  MatGetBlockSizes(Mat mat,PetscInt *rbs, PetscInt *cbs)
6648 {
6649   PetscFunctionBegin;
6650   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6651   PetscValidType(mat,1);
6652   if (rbs) PetscValidIntPointer(rbs,2);
6653   if (cbs) PetscValidIntPointer(cbs,3);
6654   MatCheckPreallocated(mat,1);
6655   if (rbs) *rbs = mat->rmap->bs;
6656   if (cbs) *cbs = mat->cmap->bs;
6657   PetscFunctionReturn(0);
6658 }
6659 
6660 #undef __FUNCT__
6661 #define __FUNCT__ "MatSetBlockSize"
6662 /*@
6663    MatSetBlockSize - Sets the matrix block size.
6664 
6665    Logically Collective on Mat
6666 
6667    Input Parameters:
6668 +  mat - the matrix
6669 -  bs - block size
6670 
6671    Notes:
6672      This must be called before MatSetUp() or MatXXXSetPreallocation() (or will default to 1) and the block size cannot be changed later
6673 
6674    Level: intermediate
6675 
6676    Concepts: matrices^block size
6677 
6678 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize()
6679 @*/
6680 PetscErrorCode  MatSetBlockSize(Mat mat,PetscInt bs)
6681 {
6682   PetscErrorCode ierr;
6683 
6684   PetscFunctionBegin;
6685   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6686   PetscValidLogicalCollectiveInt(mat,bs,2);
6687   ierr = PetscLayoutSetBlockSize(mat->rmap,bs);CHKERRQ(ierr);
6688   ierr = PetscLayoutSetBlockSize(mat->cmap,bs);CHKERRQ(ierr);
6689   PetscFunctionReturn(0);
6690 }
6691 
6692 #undef __FUNCT__
6693 #define __FUNCT__ "MatSetBlockSizes"
6694 /*@
6695    MatSetBlockSizes - Sets the matrix block row and column sizes.
6696 
6697    Logically Collective on Mat
6698 
6699    Input Parameters:
6700 +  mat - the matrix
6701 -  rbs - row block size
6702 -  cbs - column block size
6703 
6704    Notes:
6705      This must be called before MatSetUp() or MatXXXSetPreallocation() (or will default to 1) and the block size cannot be changed later
6706 
6707    Level: intermediate
6708 
6709    Concepts: matrices^block size
6710 
6711 .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize()
6712 @*/
6713 PetscErrorCode  MatSetBlockSizes(Mat mat,PetscInt rbs,PetscInt cbs)
6714 {
6715   PetscErrorCode ierr;
6716 
6717   PetscFunctionBegin;
6718   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6719   PetscValidLogicalCollectiveInt(mat,rbs,2);
6720   ierr = PetscLayoutSetBlockSize(mat->rmap,rbs);CHKERRQ(ierr);
6721   ierr = PetscLayoutSetBlockSize(mat->cmap,cbs);CHKERRQ(ierr);
6722   PetscFunctionReturn(0);
6723 }
6724 
6725 #undef __FUNCT__
6726 #define __FUNCT__ "MatGetRowIJ"
6727 /*@C
6728     MatGetRowIJ - Returns the compressed row storage i and j indices for sequential matrices.
6729 
6730    Collective on Mat
6731 
6732     Input Parameters:
6733 +   mat - the matrix
6734 .   shift -  0 or 1 indicating we want the indices starting at 0 or 1
6735 .   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be   symmetrized
6736 -   inodecompressed - PETSC_TRUE or PETSC_FALSE  indicating if the nonzero structure of the
6737                  inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is
6738                  always used.
6739 
6740     Output Parameters:
6741 +   n - number of rows in the (possibly compressed) matrix
6742 .   ia - the row pointers [of length n+1]
6743 .   ja - the column indices
6744 -   done - indicates if the routine actually worked and returned appropriate ia[] and ja[] arrays; callers
6745            are responsible for handling the case when done == PETSC_FALSE and ia and ja are not set
6746 
6747     Level: developer
6748 
6749     Notes: You CANNOT change any of the ia[] or ja[] values.
6750 
6751            Use MatRestoreRowIJ() when you are finished accessing the ia[] and ja[] values
6752 
6753     Fortran Node
6754 
6755            In Fortran use
6756 $           PetscInt ia(1), ja(1)
6757 $           PetscOffset iia, jja
6758 $      call MatGetRowIJ(mat,shift,symmetric,inodecompressed,n,ia,iia,ja,jja,done,ierr)
6759 $
6760 $          or
6761 $
6762 $           PetscScalar, pointer :: xx_v(:)
6763 $    call  MatGetRowIJF90(mat,shift,symmetric,inodecompressed,n,ia,ja,done,ierr)
6764 
6765 
6766        Acess the ith and jth entries via ia(iia + i) and ja(jja + j)
6767 
6768 .seealso: MatGetColumnIJ(), MatRestoreRowIJ(), MatSeqAIJGetArray()
6769 @*/
6770 PetscErrorCode MatGetRowIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
6771 {
6772   PetscErrorCode ierr;
6773 
6774   PetscFunctionBegin;
6775   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6776   PetscValidType(mat,1);
6777   PetscValidIntPointer(n,4);
6778   if (ia) PetscValidIntPointer(ia,5);
6779   if (ja) PetscValidIntPointer(ja,6);
6780   PetscValidIntPointer(done,7);
6781   MatCheckPreallocated(mat,1);
6782   if (!mat->ops->getrowij) *done = PETSC_FALSE;
6783   else {
6784     *done = PETSC_TRUE;
6785     ierr = PetscLogEventBegin(MAT_GetRowIJ,mat,0,0,0);CHKERRQ(ierr);
6786     ierr  = (*mat->ops->getrowij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr);
6787     ierr = PetscLogEventEnd(MAT_GetRowIJ,mat,0,0,0);CHKERRQ(ierr);
6788   }
6789   PetscFunctionReturn(0);
6790 }
6791 
6792 #undef __FUNCT__
6793 #define __FUNCT__ "MatGetColumnIJ"
6794 /*@C
6795     MatGetColumnIJ - Returns the compressed column storage i and j indices for sequential matrices.
6796 
6797     Collective on Mat
6798 
6799     Input Parameters:
6800 +   mat - the matrix
6801 .   shift - 1 or zero indicating we want the indices starting at 0 or 1
6802 .   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
6803                 symmetrized
6804 -   inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the
6805                  inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is
6806                  always used.
6807 
6808     Output Parameters:
6809 +   n - number of columns in the (possibly compressed) matrix
6810 .   ia - the column pointers
6811 .   ja - the row indices
6812 -   done - PETSC_TRUE or PETSC_FALSE, indicating whether the values have been returned
6813 
6814     Level: developer
6815 
6816 .seealso: MatGetRowIJ(), MatRestoreColumnIJ()
6817 @*/
6818 PetscErrorCode MatGetColumnIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
6819 {
6820   PetscErrorCode ierr;
6821 
6822   PetscFunctionBegin;
6823   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6824   PetscValidType(mat,1);
6825   PetscValidIntPointer(n,4);
6826   if (ia) PetscValidIntPointer(ia,5);
6827   if (ja) PetscValidIntPointer(ja,6);
6828   PetscValidIntPointer(done,7);
6829   MatCheckPreallocated(mat,1);
6830   if (!mat->ops->getcolumnij) *done = PETSC_FALSE;
6831   else {
6832     *done = PETSC_TRUE;
6833     ierr  = (*mat->ops->getcolumnij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr);
6834   }
6835   PetscFunctionReturn(0);
6836 }
6837 
6838 #undef __FUNCT__
6839 #define __FUNCT__ "MatRestoreRowIJ"
6840 /*@C
6841     MatRestoreRowIJ - Call after you are completed with the ia,ja indices obtained with
6842     MatGetRowIJ().
6843 
6844     Collective on Mat
6845 
6846     Input Parameters:
6847 +   mat - the matrix
6848 .   shift - 1 or zero indicating we want the indices starting at 0 or 1
6849 .   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
6850                 symmetrized
6851 -   inodecompressed -  PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the
6852                  inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is
6853                  always used.
6854 
6855     Output Parameters:
6856 +   n - size of (possibly compressed) matrix
6857 .   ia - the row pointers
6858 .   ja - the column indices
6859 -   done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned
6860 
6861     Level: developer
6862 
6863 .seealso: MatGetRowIJ(), MatRestoreColumnIJ()
6864 @*/
6865 PetscErrorCode MatRestoreRowIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
6866 {
6867   PetscErrorCode ierr;
6868 
6869   PetscFunctionBegin;
6870   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6871   PetscValidType(mat,1);
6872   if (ia) PetscValidIntPointer(ia,5);
6873   if (ja) PetscValidIntPointer(ja,6);
6874   PetscValidIntPointer(done,7);
6875   MatCheckPreallocated(mat,1);
6876 
6877   if (!mat->ops->restorerowij) *done = PETSC_FALSE;
6878   else {
6879     *done = PETSC_TRUE;
6880     ierr  = (*mat->ops->restorerowij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr);
6881   }
6882   PetscFunctionReturn(0);
6883 }
6884 
6885 #undef __FUNCT__
6886 #define __FUNCT__ "MatRestoreColumnIJ"
6887 /*@C
6888     MatRestoreColumnIJ - Call after you are completed with the ia,ja indices obtained with
6889     MatGetColumnIJ().
6890 
6891     Collective on Mat
6892 
6893     Input Parameters:
6894 +   mat - the matrix
6895 .   shift - 1 or zero indicating we want the indices starting at 0 or 1
6896 -   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
6897                 symmetrized
6898 -   inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the
6899                  inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is
6900                  always used.
6901 
6902     Output Parameters:
6903 +   n - size of (possibly compressed) matrix
6904 .   ia - the column pointers
6905 .   ja - the row indices
6906 -   done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned
6907 
6908     Level: developer
6909 
6910 .seealso: MatGetColumnIJ(), MatRestoreRowIJ()
6911 @*/
6912 PetscErrorCode MatRestoreColumnIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
6913 {
6914   PetscErrorCode ierr;
6915 
6916   PetscFunctionBegin;
6917   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6918   PetscValidType(mat,1);
6919   if (ia) PetscValidIntPointer(ia,5);
6920   if (ja) PetscValidIntPointer(ja,6);
6921   PetscValidIntPointer(done,7);
6922   MatCheckPreallocated(mat,1);
6923 
6924   if (!mat->ops->restorecolumnij) *done = PETSC_FALSE;
6925   else {
6926     *done = PETSC_TRUE;
6927     ierr  = (*mat->ops->restorecolumnij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr);
6928   }
6929   PetscFunctionReturn(0);
6930 }
6931 
6932 #undef __FUNCT__
6933 #define __FUNCT__ "MatColoringPatch"
6934 /*@C
6935     MatColoringPatch -Used inside matrix coloring routines that
6936     use MatGetRowIJ() and/or MatGetColumnIJ().
6937 
6938     Collective on Mat
6939 
6940     Input Parameters:
6941 +   mat - the matrix
6942 .   ncolors - max color value
6943 .   n   - number of entries in colorarray
6944 -   colorarray - array indicating color for each column
6945 
6946     Output Parameters:
6947 .   iscoloring - coloring generated using colorarray information
6948 
6949     Level: developer
6950 
6951 .seealso: MatGetRowIJ(), MatGetColumnIJ()
6952 
6953 @*/
6954 PetscErrorCode  MatColoringPatch(Mat mat,PetscInt ncolors,PetscInt n,ISColoringValue colorarray[],ISColoring *iscoloring)
6955 {
6956   PetscErrorCode ierr;
6957 
6958   PetscFunctionBegin;
6959   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
6960   PetscValidType(mat,1);
6961   PetscValidIntPointer(colorarray,4);
6962   PetscValidPointer(iscoloring,5);
6963   MatCheckPreallocated(mat,1);
6964 
6965   if (!mat->ops->coloringpatch) {
6966     ierr = ISColoringCreate(((PetscObject)mat)->comm,ncolors,n,colorarray,iscoloring);CHKERRQ(ierr);
6967   } else {
6968     ierr = (*mat->ops->coloringpatch)(mat,ncolors,n,colorarray,iscoloring);CHKERRQ(ierr);
6969   }
6970   PetscFunctionReturn(0);
6971 }
6972 
6973 
6974 #undef __FUNCT__
6975 #define __FUNCT__ "MatSetUnfactored"
6976 /*@
6977    MatSetUnfactored - Resets a factored matrix to be treated as unfactored.
6978 
6979    Logically Collective on Mat
6980 
6981    Input Parameter:
6982 .  mat - the factored matrix to be reset
6983 
6984    Notes:
6985    This routine should be used only with factored matrices formed by in-place
6986    factorization via ILU(0) (or by in-place LU factorization for the MATSEQDENSE
6987    format).  This option can save memory, for example, when solving nonlinear
6988    systems with a matrix-free Newton-Krylov method and a matrix-based, in-place
6989    ILU(0) preconditioner.
6990 
6991    Note that one can specify in-place ILU(0) factorization by calling
6992 .vb
6993      PCType(pc,PCILU);
6994      PCFactorSeUseInPlace(pc);
6995 .ve
6996    or by using the options -pc_type ilu -pc_factor_in_place
6997 
6998    In-place factorization ILU(0) can also be used as a local
6999    solver for the blocks within the block Jacobi or additive Schwarz
7000    methods (runtime option: -sub_pc_factor_in_place).  See the discussion
7001    of these preconditioners in the <a href="../../docs/manual.pdf#ch_pc">PC chapter of the users manual</a> for details on setting
7002    local solver options.
7003 
7004    Most users should employ the simplified KSP interface for linear solvers
7005    instead of working directly with matrix algebra routines such as this.
7006    See, e.g., KSPCreate().
7007 
7008    Level: developer
7009 
7010 .seealso: PCFactorSetUseInPlace()
7011 
7012    Concepts: matrices^unfactored
7013 
7014 @*/
7015 PetscErrorCode  MatSetUnfactored(Mat mat)
7016 {
7017   PetscErrorCode ierr;
7018 
7019   PetscFunctionBegin;
7020   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7021   PetscValidType(mat,1);
7022   MatCheckPreallocated(mat,1);
7023   mat->factortype = MAT_FACTOR_NONE;
7024   if (!mat->ops->setunfactored) PetscFunctionReturn(0);
7025   ierr = (*mat->ops->setunfactored)(mat);CHKERRQ(ierr);
7026   PetscFunctionReturn(0);
7027 }
7028 
7029 /*MC
7030     MatDenseGetArrayF90 - Accesses a matrix array from Fortran90.
7031 
7032     Synopsis:
7033     MatDenseGetArrayF90(Mat x,{Scalar, pointer :: xx_v(:,:)},integer ierr)
7034 
7035     Not collective
7036 
7037     Input Parameter:
7038 .   x - matrix
7039 
7040     Output Parameters:
7041 +   xx_v - the Fortran90 pointer to the array
7042 -   ierr - error code
7043 
7044     Example of Usage:
7045 .vb
7046       PetscScalar, pointer xx_v(:,:)
7047       ....
7048       call MatDenseGetArrayF90(x,xx_v,ierr)
7049       a = xx_v(3)
7050       call MatDenseRestoreArrayF90(x,xx_v,ierr)
7051 .ve
7052 
7053     Level: advanced
7054 
7055 .seealso:  MatDenseRestoreArrayF90(), MatDenseGetArray(), MatDenseRestoreArray()
7056 
7057     Concepts: matrices^accessing array
7058 
7059 M*/
7060 
7061 /*MC
7062     MatDenseRestoreArrayF90 - Restores a matrix array that has been
7063     accessed with MatGetArrayF90().
7064 
7065     Synopsis:
7066     MatDenseRestoreArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr)
7067 
7068     Not collective
7069 
7070     Input Parameters:
7071 +   x - matrix
7072 -   xx_v - the Fortran90 pointer to the array
7073 
7074     Output Parameter:
7075 .   ierr - error code
7076 
7077     Example of Usage:
7078 .vb
7079        PetscScalar, pointer xx_v(:)
7080        ....
7081        call MatDenseGetArrayF90(x,xx_v,ierr)
7082        a = xx_v(3)
7083        call MatDenseRestoreArrayF90(x,xx_v,ierr)
7084 .ve
7085 
7086     Level: advanced
7087 
7088 .seealso:  MatDenseGetArrayF90(), MatDenseGetArray(), MatDenseRestoreArray()
7089 
7090 M*/
7091 
7092 
7093 #undef __FUNCT__
7094 #define __FUNCT__ "MatGetSubMatrix"
7095 /*@
7096     MatGetSubMatrix - Gets a single submatrix on the same number of processors
7097                       as the original matrix.
7098 
7099     Collective on Mat
7100 
7101     Input Parameters:
7102 +   mat - the original matrix
7103 .   isrow - parallel IS containing the rows this processor should obtain
7104 .   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.
7105 -   cll - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
7106 
7107     Output Parameter:
7108 .   newmat - the new submatrix, of the same type as the old
7109 
7110     Level: advanced
7111 
7112     Notes:
7113     The submatrix will be able to be multiplied with vectors using the same layout as iscol.
7114 
7115     The rows in isrow will be sorted into the same order as the original matrix on each process.
7116 
7117       The first time this is called you should use a cll of MAT_INITIAL_MATRIX,
7118    the MatGetSubMatrix() routine will create the newmat for you. Any additional calls
7119    to this routine with a mat of the same nonzero structure and with a call of MAT_REUSE_MATRIX
7120    will reuse the matrix generated the first time.  You should call MatDestroy() on newmat when
7121    you are finished using it.
7122 
7123     The communicator of the newly obtained matrix is ALWAYS the same as the communicator of
7124     the input matrix.
7125 
7126     If iscol is PETSC_NULL then all columns are obtained (not supported in Fortran).
7127 
7128    Example usage:
7129    Consider the following 8x8 matrix with 34 non-zero values, that is
7130    assembled across 3 processors. Let's assume that proc0 owns 3 rows,
7131    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
7132    as follows:
7133 
7134 .vb
7135             1  2  0  |  0  3  0  |  0  4
7136     Proc0   0  5  6  |  7  0  0  |  8  0
7137             9  0 10  | 11  0  0  | 12  0
7138     -------------------------------------
7139            13  0 14  | 15 16 17  |  0  0
7140     Proc1   0 18  0  | 19 20 21  |  0  0
7141             0  0  0  | 22 23  0  | 24  0
7142     -------------------------------------
7143     Proc2  25 26 27  |  0  0 28  | 29  0
7144            30  0  0  | 31 32 33  |  0 34
7145 .ve
7146 
7147     Suppose isrow = [0 1 | 4 | 6 7] and iscol = [1 2 | 3 4 5 | 6].  The resulting submatrix is
7148 
7149 .vb
7150             2  0  |  0  3  0  |  0
7151     Proc0   5  6  |  7  0  0  |  8
7152     -------------------------------
7153     Proc1  18  0  | 19 20 21  |  0
7154     -------------------------------
7155     Proc2  26 27  |  0  0 28  | 29
7156             0  0  | 31 32 33  |  0
7157 .ve
7158 
7159 
7160     Concepts: matrices^submatrices
7161 
7162 .seealso: MatGetSubMatrices()
7163 @*/
7164 PetscErrorCode  MatGetSubMatrix(Mat mat,IS isrow,IS iscol,MatReuse cll,Mat *newmat)
7165 {
7166   PetscErrorCode ierr;
7167   PetscMPIInt    size;
7168   Mat            *local;
7169   IS             iscoltmp;
7170 
7171   PetscFunctionBegin;
7172   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7173   PetscValidHeaderSpecific(isrow,IS_CLASSID,2);
7174   if (iscol) PetscValidHeaderSpecific(iscol,IS_CLASSID,3);
7175   PetscValidPointer(newmat,5);
7176   if (cll == MAT_REUSE_MATRIX) PetscValidHeaderSpecific(*newmat,MAT_CLASSID,5);
7177   PetscValidType(mat,1);
7178   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7179   MatCheckPreallocated(mat,1);
7180   ierr = MPI_Comm_size(((PetscObject)mat)->comm,&size);CHKERRQ(ierr);
7181 
7182   if (!iscol) {
7183     ierr = ISCreateStride(((PetscObject)mat)->comm,mat->cmap->n,mat->cmap->rstart,1,&iscoltmp);CHKERRQ(ierr);
7184   } else {
7185     iscoltmp = iscol;
7186   }
7187 
7188   /* if original matrix is on just one processor then use submatrix generated */
7189   if (mat->ops->getsubmatrices && !mat->ops->getsubmatrix && size == 1 && cll == MAT_REUSE_MATRIX) {
7190     ierr = MatGetSubMatrices(mat,1,&isrow,&iscoltmp,MAT_REUSE_MATRIX,&newmat);CHKERRQ(ierr);
7191     if (!iscol) {ierr = ISDestroy(&iscoltmp);CHKERRQ(ierr);}
7192     PetscFunctionReturn(0);
7193   } else if (mat->ops->getsubmatrices && !mat->ops->getsubmatrix && size == 1) {
7194     ierr    = MatGetSubMatrices(mat,1,&isrow,&iscoltmp,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr);
7195     *newmat = *local;
7196     ierr    = PetscFree(local);CHKERRQ(ierr);
7197     if (!iscol) {ierr = ISDestroy(&iscoltmp);CHKERRQ(ierr);}
7198     PetscFunctionReturn(0);
7199   } else if (!mat->ops->getsubmatrix) {
7200     /* Create a new matrix type that implements the operation using the full matrix */
7201     switch (cll) {
7202       case MAT_INITIAL_MATRIX:
7203         ierr = MatCreateSubMatrix(mat,isrow,iscoltmp,newmat);CHKERRQ(ierr);
7204         break;
7205       case MAT_REUSE_MATRIX:
7206         ierr = MatSubMatrixUpdate(*newmat,mat,isrow,iscoltmp);CHKERRQ(ierr);
7207         break;
7208       default: SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Invalid MatReuse, must be either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX");
7209     }
7210     if (!iscol) {ierr = ISDestroy(&iscoltmp);CHKERRQ(ierr);}
7211     PetscFunctionReturn(0);
7212   }
7213 
7214   if (!mat->ops->getsubmatrix) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
7215   ierr = (*mat->ops->getsubmatrix)(mat,isrow,iscoltmp,cll,newmat);CHKERRQ(ierr);
7216   if (!iscol) {ierr = ISDestroy(&iscoltmp);CHKERRQ(ierr);}
7217   if (*newmat && cll == MAT_INITIAL_MATRIX) {ierr = PetscObjectStateIncrease((PetscObject)*newmat);CHKERRQ(ierr);}
7218   PetscFunctionReturn(0);
7219 }
7220 
7221 #undef __FUNCT__
7222 #define __FUNCT__ "MatStashSetInitialSize"
7223 /*@
7224    MatStashSetInitialSize - sets the sizes of the matrix stash, that is
7225    used during the assembly process to store values that belong to
7226    other processors.
7227 
7228    Not Collective
7229 
7230    Input Parameters:
7231 +  mat   - the matrix
7232 .  size  - the initial size of the stash.
7233 -  bsize - the initial size of the block-stash(if used).
7234 
7235    Options Database Keys:
7236 +   -matstash_initial_size <size> or <size0,size1,...sizep-1>
7237 -   -matstash_block_initial_size <bsize>  or <bsize0,bsize1,...bsizep-1>
7238 
7239    Level: intermediate
7240 
7241    Notes:
7242      The block-stash is used for values set with MatSetValuesBlocked() while
7243      the stash is used for values set with MatSetValues()
7244 
7245      Run with the option -info and look for output of the form
7246      MatAssemblyBegin_MPIXXX:Stash has MM entries, uses nn mallocs.
7247      to determine the appropriate value, MM, to use for size and
7248      MatAssemblyBegin_MPIXXX:Block-Stash has BMM entries, uses nn mallocs.
7249      to determine the value, BMM to use for bsize
7250 
7251    Concepts: stash^setting matrix size
7252    Concepts: matrices^stash
7253 
7254 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), Mat, MatStashGetInfo()
7255 
7256 @*/
7257 PetscErrorCode  MatStashSetInitialSize(Mat mat,PetscInt size, PetscInt bsize)
7258 {
7259   PetscErrorCode ierr;
7260 
7261   PetscFunctionBegin;
7262   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7263   PetscValidType(mat,1);
7264   ierr = MatStashSetInitialSize_Private(&mat->stash,size);CHKERRQ(ierr);
7265   ierr = MatStashSetInitialSize_Private(&mat->bstash,bsize);CHKERRQ(ierr);
7266   PetscFunctionReturn(0);
7267 }
7268 
7269 #undef __FUNCT__
7270 #define __FUNCT__ "MatInterpolateAdd"
7271 /*@
7272    MatInterpolateAdd - w = y + A*x or A'*x depending on the shape of
7273      the matrix
7274 
7275    Neighbor-wise Collective on Mat
7276 
7277    Input Parameters:
7278 +  mat   - the matrix
7279 .  x,y - the vectors
7280 -  w - where the result is stored
7281 
7282    Level: intermediate
7283 
7284    Notes:
7285     w may be the same vector as y.
7286 
7287     This allows one to use either the restriction or interpolation (its transpose)
7288     matrix to do the interpolation
7289 
7290     Concepts: interpolation
7291 
7292 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict()
7293 
7294 @*/
7295 PetscErrorCode  MatInterpolateAdd(Mat A,Vec x,Vec y,Vec w)
7296 {
7297   PetscErrorCode ierr;
7298   PetscInt       M,N,Ny;
7299 
7300   PetscFunctionBegin;
7301   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
7302   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
7303   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
7304   PetscValidHeaderSpecific(w,VEC_CLASSID,4);
7305   PetscValidType(A,1);
7306   MatCheckPreallocated(A,1);
7307   ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr);
7308   ierr = VecGetSize(y,&Ny);CHKERRQ(ierr);
7309   if (M == Ny) {
7310     ierr = MatMultAdd(A,x,y,w);CHKERRQ(ierr);
7311   } else {
7312     ierr = MatMultTransposeAdd(A,x,y,w);CHKERRQ(ierr);
7313   }
7314   PetscFunctionReturn(0);
7315 }
7316 
7317 #undef __FUNCT__
7318 #define __FUNCT__ "MatInterpolate"
7319 /*@
7320    MatInterpolate - y = A*x or A'*x depending on the shape of
7321      the matrix
7322 
7323    Neighbor-wise Collective on Mat
7324 
7325    Input Parameters:
7326 +  mat   - the matrix
7327 -  x,y - the vectors
7328 
7329    Level: intermediate
7330 
7331    Notes:
7332     This allows one to use either the restriction or interpolation (its transpose)
7333     matrix to do the interpolation
7334 
7335    Concepts: matrices^interpolation
7336 
7337 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict()
7338 
7339 @*/
7340 PetscErrorCode  MatInterpolate(Mat A,Vec x,Vec y)
7341 {
7342   PetscErrorCode ierr;
7343   PetscInt       M,N,Ny;
7344 
7345   PetscFunctionBegin;
7346   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
7347   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
7348   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
7349   PetscValidType(A,1);
7350   MatCheckPreallocated(A,1);
7351   ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr);
7352   ierr = VecGetSize(y,&Ny);CHKERRQ(ierr);
7353   if (M == Ny) {
7354     ierr = MatMult(A,x,y);CHKERRQ(ierr);
7355   } else {
7356     ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr);
7357   }
7358   PetscFunctionReturn(0);
7359 }
7360 
7361 #undef __FUNCT__
7362 #define __FUNCT__ "MatRestrict"
7363 /*@
7364    MatRestrict - y = A*x or A'*x
7365 
7366    Neighbor-wise Collective on Mat
7367 
7368    Input Parameters:
7369 +  mat   - the matrix
7370 -  x,y - the vectors
7371 
7372    Level: intermediate
7373 
7374    Notes:
7375     This allows one to use either the restriction or interpolation (its transpose)
7376     matrix to do the restriction
7377 
7378    Concepts: matrices^restriction
7379 
7380 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatInterpolate()
7381 
7382 @*/
7383 PetscErrorCode  MatRestrict(Mat A,Vec x,Vec y)
7384 {
7385   PetscErrorCode ierr;
7386   PetscInt       M,N,Ny;
7387 
7388   PetscFunctionBegin;
7389   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
7390   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
7391   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
7392   PetscValidType(A,1);
7393   MatCheckPreallocated(A,1);
7394 
7395   ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr);
7396   ierr = VecGetSize(y,&Ny);CHKERRQ(ierr);
7397   if (M == Ny) {
7398     ierr = MatMult(A,x,y);CHKERRQ(ierr);
7399   } else {
7400     ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr);
7401   }
7402   PetscFunctionReturn(0);
7403 }
7404 
7405 #undef __FUNCT__
7406 #define __FUNCT__ "MatGetNullSpace"
7407 /*@
7408    MatGetNullSpace - retrieves the null space to a matrix.
7409 
7410    Logically Collective on Mat and MatNullSpace
7411 
7412    Input Parameters:
7413 +  mat - the matrix
7414 -  nullsp - the null space object
7415 
7416    Level: developer
7417 
7418    Notes:
7419       This null space is used by solvers. Overwrites any previous null space that may have been attached
7420 
7421    Concepts: null space^attaching to matrix
7422 
7423 .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace()
7424 @*/
7425 PetscErrorCode MatGetNullSpace(Mat mat, MatNullSpace *nullsp)
7426 {
7427   PetscFunctionBegin;
7428   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7429   PetscValidType(mat,1);
7430   PetscValidPointer(nullsp,2);
7431   *nullsp = mat->nullsp;
7432   PetscFunctionReturn(0);
7433 }
7434 
7435 #undef __FUNCT__
7436 #define __FUNCT__ "MatSetNullSpace"
7437 /*@
7438    MatSetNullSpace - attaches a null space to a matrix.
7439         This null space will be removed from the resulting vector whenever
7440         MatMult() is called
7441 
7442    Logically Collective on Mat and MatNullSpace
7443 
7444    Input Parameters:
7445 +  mat - the matrix
7446 -  nullsp - the null space object
7447 
7448    Level: advanced
7449 
7450    Notes:
7451       This null space is used by solvers. Overwrites any previous null space that may have been attached
7452 
7453    Concepts: null space^attaching to matrix
7454 
7455 .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace()
7456 @*/
7457 PetscErrorCode  MatSetNullSpace(Mat mat,MatNullSpace nullsp)
7458 {
7459   PetscErrorCode ierr;
7460 
7461   PetscFunctionBegin;
7462   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7463   PetscValidType(mat,1);
7464   PetscValidHeaderSpecific(nullsp,MAT_NULLSPACE_CLASSID,2);
7465   MatCheckPreallocated(mat,1);
7466   ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr);
7467   ierr = MatNullSpaceDestroy(&mat->nullsp);CHKERRQ(ierr);
7468   mat->nullsp = nullsp;
7469   PetscFunctionReturn(0);
7470 }
7471 
7472 #undef __FUNCT__
7473 #define __FUNCT__ "MatSetNearNullSpace"
7474 /*@
7475    MatSetNearNullSpace - attaches a null space to a matrix.
7476         This null space will be used to provide near null space vectors to a multigrid preconditioner built from this matrix.
7477 
7478    Logically Collective on Mat and MatNullSpace
7479 
7480    Input Parameters:
7481 +  mat - the matrix
7482 -  nullsp - the null space object
7483 
7484    Level: advanced
7485 
7486    Notes:
7487       Overwrites any previous near null space that may have been attached
7488 
7489    Concepts: null space^attaching to matrix
7490 
7491 .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNullSpace()
7492 @*/
7493 PetscErrorCode MatSetNearNullSpace(Mat mat,MatNullSpace nullsp)
7494 {
7495   PetscErrorCode ierr;
7496 
7497   PetscFunctionBegin;
7498   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7499   PetscValidType(mat,1);
7500   PetscValidHeaderSpecific(nullsp,MAT_NULLSPACE_CLASSID,2);
7501   MatCheckPreallocated(mat,1);
7502   ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr);
7503   ierr = MatNullSpaceDestroy(&mat->nearnullsp);CHKERRQ(ierr);
7504   mat->nearnullsp = nullsp;
7505   PetscFunctionReturn(0);
7506 }
7507 
7508 #undef __FUNCT__
7509 #define __FUNCT__ "MatGetNearNullSpace"
7510 /*@
7511    MatGetNearNullSpace -Get null space attached with MatSetNearNullSpace()
7512 
7513    Not Collective
7514 
7515    Input Parameters:
7516 .  mat - the matrix
7517 
7518    Output Parameters:
7519 .  nullsp - the null space object, PETSC_NULL if not set
7520 
7521    Level: developer
7522 
7523    Concepts: null space^attaching to matrix
7524 
7525 .seealso: MatSetNearNullSpace(), MatGetNullSpace()
7526 @*/
7527 PetscErrorCode MatGetNearNullSpace(Mat mat,MatNullSpace *nullsp)
7528 {
7529   PetscFunctionBegin;
7530   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7531   PetscValidType(mat,1);
7532   PetscValidPointer(nullsp,2);
7533   MatCheckPreallocated(mat,1);
7534   *nullsp = mat->nearnullsp;
7535   PetscFunctionReturn(0);
7536 }
7537 
7538 #undef __FUNCT__
7539 #define __FUNCT__ "MatICCFactor"
7540 /*@C
7541    MatICCFactor - Performs in-place incomplete Cholesky factorization of matrix.
7542 
7543    Collective on Mat
7544 
7545    Input Parameters:
7546 +  mat - the matrix
7547 .  row - row/column permutation
7548 .  fill - expected fill factor >= 1.0
7549 -  level - level of fill, for ICC(k)
7550 
7551    Notes:
7552    Probably really in-place only when level of fill is zero, otherwise allocates
7553    new space to store factored matrix and deletes previous memory.
7554 
7555    Most users should employ the simplified KSP interface for linear solvers
7556    instead of working directly with matrix algebra routines such as this.
7557    See, e.g., KSPCreate().
7558 
7559    Level: developer
7560 
7561    Concepts: matrices^incomplete Cholesky factorization
7562    Concepts: Cholesky factorization
7563 
7564 .seealso: MatICCFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor()
7565 
7566     Developer Note: fortran interface is not autogenerated as the f90
7567     interface defintion cannot be generated correctly [due to MatFactorInfo]
7568 
7569 @*/
7570 PetscErrorCode  MatICCFactor(Mat mat,IS row,const MatFactorInfo* info)
7571 {
7572   PetscErrorCode ierr;
7573 
7574   PetscFunctionBegin;
7575   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7576   PetscValidType(mat,1);
7577   if (row) PetscValidHeaderSpecific(row,IS_CLASSID,2);
7578   PetscValidPointer(info,3);
7579   if (mat->rmap->N != mat->cmap->N) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONG,"matrix must be square");
7580   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7581   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7582   if (!mat->ops->iccfactor) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
7583   MatCheckPreallocated(mat,1);
7584   ierr = (*mat->ops->iccfactor)(mat,row,info);CHKERRQ(ierr);
7585   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
7586   PetscFunctionReturn(0);
7587 }
7588 
7589 #undef __FUNCT__
7590 #define __FUNCT__ "MatSetValuesAdic"
7591 /*@
7592    MatSetValuesAdic - Sets values computed with ADIC automatic differentiation into a matrix.
7593 
7594    Not Collective
7595 
7596    Input Parameters:
7597 +  mat - the matrix
7598 -  v - the values compute with ADIC
7599 
7600    Level: developer
7601 
7602    Notes:
7603      Must call MatSetColoring() before using this routine. Also this matrix must already
7604      have its nonzero pattern determined.
7605 
7606 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
7607           MatSetValues(), MatSetColoring(), MatSetValuesAdifor()
7608 @*/
7609 PetscErrorCode  MatSetValuesAdic(Mat mat,void *v)
7610 {
7611   PetscErrorCode ierr;
7612 
7613   PetscFunctionBegin;
7614   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7615   PetscValidType(mat,1);
7616   PetscValidPointer(mat,2);
7617 
7618   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled");
7619   ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
7620   if (!mat->ops->setvaluesadic) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
7621   ierr = (*mat->ops->setvaluesadic)(mat,v);CHKERRQ(ierr);
7622   ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
7623   ierr = MatView_Private(mat);CHKERRQ(ierr);
7624   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
7625   PetscFunctionReturn(0);
7626 }
7627 
7628 
7629 #undef __FUNCT__
7630 #define __FUNCT__ "MatSetColoring"
7631 /*@
7632    MatSetColoring - Sets a coloring used by calls to MatSetValuesAdic()
7633 
7634    Not Collective
7635 
7636    Input Parameters:
7637 +  mat - the matrix
7638 -  coloring - the coloring
7639 
7640    Level: developer
7641 
7642 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
7643           MatSetValues(), MatSetValuesAdic()
7644 @*/
7645 PetscErrorCode  MatSetColoring(Mat mat,ISColoring coloring)
7646 {
7647   PetscErrorCode ierr;
7648 
7649   PetscFunctionBegin;
7650   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7651   PetscValidType(mat,1);
7652   PetscValidPointer(coloring,2);
7653 
7654   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled");
7655   if (!mat->ops->setcoloring) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
7656   ierr = (*mat->ops->setcoloring)(mat,coloring);CHKERRQ(ierr);
7657   PetscFunctionReturn(0);
7658 }
7659 
7660 #undef __FUNCT__
7661 #define __FUNCT__ "MatSetValuesAdifor"
7662 /*@
7663    MatSetValuesAdifor - Sets values computed with automatic differentiation into a matrix.
7664 
7665    Not Collective
7666 
7667    Input Parameters:
7668 +  mat - the matrix
7669 .  nl - leading dimension of v
7670 -  v - the values compute with ADIFOR
7671 
7672    Level: developer
7673 
7674    Notes:
7675      Must call MatSetColoring() before using this routine. Also this matrix must already
7676      have its nonzero pattern determined.
7677 
7678 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
7679           MatSetValues(), MatSetColoring()
7680 @*/
7681 PetscErrorCode  MatSetValuesAdifor(Mat mat,PetscInt nl,void *v)
7682 {
7683   PetscErrorCode ierr;
7684 
7685   PetscFunctionBegin;
7686   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7687   PetscValidType(mat,1);
7688   PetscValidPointer(v,3);
7689 
7690   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled");
7691   ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
7692   if (!mat->ops->setvaluesadifor) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
7693   ierr = (*mat->ops->setvaluesadifor)(mat,nl,v);CHKERRQ(ierr);
7694   ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
7695   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
7696   PetscFunctionReturn(0);
7697 }
7698 
7699 #undef __FUNCT__
7700 #define __FUNCT__ "MatDiagonalScaleLocal"
7701 /*@
7702    MatDiagonalScaleLocal - Scales columns of a matrix given the scaling values including the
7703          ghosted ones.
7704 
7705    Not Collective
7706 
7707    Input Parameters:
7708 +  mat - the matrix
7709 -  diag = the diagonal values, including ghost ones
7710 
7711    Level: developer
7712 
7713    Notes: Works only for MPIAIJ and MPIBAIJ matrices
7714 
7715 .seealso: MatDiagonalScale()
7716 @*/
7717 PetscErrorCode  MatDiagonalScaleLocal(Mat mat,Vec diag)
7718 {
7719   PetscErrorCode ierr;
7720   PetscMPIInt    size;
7721 
7722   PetscFunctionBegin;
7723   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7724   PetscValidHeaderSpecific(diag,VEC_CLASSID,2);
7725   PetscValidType(mat,1);
7726 
7727   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled");
7728   ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
7729   ierr = MPI_Comm_size(((PetscObject)mat)->comm,&size);CHKERRQ(ierr);
7730   if (size == 1) {
7731     PetscInt n,m;
7732     ierr = VecGetSize(diag,&n);CHKERRQ(ierr);
7733     ierr = MatGetSize(mat,0,&m);CHKERRQ(ierr);
7734     if (m == n) {
7735       ierr = MatDiagonalScale(mat,0,diag);CHKERRQ(ierr);
7736     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only supported for sequential matrices when no ghost points/periodic conditions");
7737   } else {
7738     ierr = PetscUseMethod(mat,"MatDiagonalScaleLocal_C",(Mat,Vec),(mat,diag));CHKERRQ(ierr);
7739   }
7740   ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
7741   ierr = PetscObjectStateIncrease((PetscObject)mat);CHKERRQ(ierr);
7742   PetscFunctionReturn(0);
7743 }
7744 
7745 #undef __FUNCT__
7746 #define __FUNCT__ "MatGetInertia"
7747 /*@
7748    MatGetInertia - Gets the inertia from a factored matrix
7749 
7750    Collective on Mat
7751 
7752    Input Parameter:
7753 .  mat - the matrix
7754 
7755    Output Parameters:
7756 +   nneg - number of negative eigenvalues
7757 .   nzero - number of zero eigenvalues
7758 -   npos - number of positive eigenvalues
7759 
7760    Level: advanced
7761 
7762    Notes: Matrix must have been factored by MatCholeskyFactor()
7763 
7764 
7765 @*/
7766 PetscErrorCode  MatGetInertia(Mat mat,PetscInt *nneg,PetscInt *nzero,PetscInt *npos)
7767 {
7768   PetscErrorCode ierr;
7769 
7770   PetscFunctionBegin;
7771   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7772   PetscValidType(mat,1);
7773   if (!mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
7774   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Numeric factor mat is not assembled");
7775   if (!mat->ops->getinertia) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
7776   ierr = (*mat->ops->getinertia)(mat,nneg,nzero,npos);CHKERRQ(ierr);
7777   PetscFunctionReturn(0);
7778 }
7779 
7780 /* ----------------------------------------------------------------*/
7781 #undef __FUNCT__
7782 #define __FUNCT__ "MatSolves"
7783 /*@C
7784    MatSolves - Solves A x = b, given a factored matrix, for a collection of vectors
7785 
7786    Neighbor-wise Collective on Mat and Vecs
7787 
7788    Input Parameters:
7789 +  mat - the factored matrix
7790 -  b - the right-hand-side vectors
7791 
7792    Output Parameter:
7793 .  x - the result vectors
7794 
7795    Notes:
7796    The vectors b and x cannot be the same.  I.e., one cannot
7797    call MatSolves(A,x,x).
7798 
7799    Notes:
7800    Most users should employ the simplified KSP interface for linear solvers
7801    instead of working directly with matrix algebra routines such as this.
7802    See, e.g., KSPCreate().
7803 
7804    Level: developer
7805 
7806    Concepts: matrices^triangular solves
7807 
7808 .seealso: MatSolveAdd(), MatSolveTranspose(), MatSolveTransposeAdd(), MatSolve()
7809 @*/
7810 PetscErrorCode  MatSolves(Mat mat,Vecs b,Vecs x)
7811 {
7812   PetscErrorCode ierr;
7813 
7814   PetscFunctionBegin;
7815   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
7816   PetscValidType(mat,1);
7817   if (x == b) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_IDN,"x and b must be different vectors");
7818   if (!mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
7819   if (!mat->rmap->N && !mat->cmap->N) PetscFunctionReturn(0);
7820 
7821   if (!mat->ops->solves) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
7822   MatCheckPreallocated(mat,1);
7823   ierr = PetscLogEventBegin(MAT_Solves,mat,0,0,0);CHKERRQ(ierr);
7824   ierr = (*mat->ops->solves)(mat,b,x);CHKERRQ(ierr);
7825   ierr = PetscLogEventEnd(MAT_Solves,mat,0,0,0);CHKERRQ(ierr);
7826   PetscFunctionReturn(0);
7827 }
7828 
7829 #undef __FUNCT__
7830 #define __FUNCT__ "MatIsSymmetric"
7831 /*@
7832    MatIsSymmetric - Test whether a matrix is symmetric
7833 
7834    Collective on Mat
7835 
7836    Input Parameter:
7837 +  A - the matrix to test
7838 -  tol - difference between value and its transpose less than this amount counts as equal (use 0.0 for exact transpose)
7839 
7840    Output Parameters:
7841 .  flg - the result
7842 
7843    Notes: For real numbers MatIsSymmetric() and MatIsHermitian() return identical results
7844 
7845    Level: intermediate
7846 
7847    Concepts: matrix^symmetry
7848 
7849 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetricKnown()
7850 @*/
7851 PetscErrorCode  MatIsSymmetric(Mat A,PetscReal tol,PetscBool  *flg)
7852 {
7853   PetscErrorCode ierr;
7854 
7855   PetscFunctionBegin;
7856   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
7857   PetscValidPointer(flg,2);
7858 
7859   if (!A->symmetric_set) {
7860     if (!A->ops->issymmetric) {
7861       MatType mattype;
7862       ierr = MatGetType(A,&mattype);CHKERRQ(ierr);
7863       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for symmetric",mattype);
7864     }
7865     ierr = (*A->ops->issymmetric)(A,tol,flg);CHKERRQ(ierr);
7866     if (!tol) {
7867       A->symmetric_set = PETSC_TRUE;
7868       A->symmetric = *flg;
7869       if (A->symmetric) {
7870 	A->structurally_symmetric_set = PETSC_TRUE;
7871 	A->structurally_symmetric     = PETSC_TRUE;
7872       }
7873     }
7874   } else if (A->symmetric) {
7875     *flg = PETSC_TRUE;
7876   } else if (!tol) {
7877     *flg = PETSC_FALSE;
7878   } else {
7879     if (!A->ops->issymmetric) {
7880       MatType mattype;
7881       ierr = MatGetType(A,&mattype);CHKERRQ(ierr);
7882       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for symmetric",mattype);
7883     }
7884     ierr = (*A->ops->issymmetric)(A,tol,flg);CHKERRQ(ierr);
7885   }
7886   PetscFunctionReturn(0);
7887 }
7888 
7889 #undef __FUNCT__
7890 #define __FUNCT__ "MatIsHermitian"
7891 /*@
7892    MatIsHermitian - Test whether a matrix is Hermitian
7893 
7894    Collective on Mat
7895 
7896    Input Parameter:
7897 +  A - the matrix to test
7898 -  tol - difference between value and its transpose less than this amount counts as equal (use 0.0 for exact Hermitian)
7899 
7900    Output Parameters:
7901 .  flg - the result
7902 
7903    Level: intermediate
7904 
7905    Concepts: matrix^symmetry
7906 
7907 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(),
7908           MatIsSymmetricKnown(), MatIsSymmetric()
7909 @*/
7910 PetscErrorCode  MatIsHermitian(Mat A,PetscReal tol,PetscBool  *flg)
7911 {
7912   PetscErrorCode ierr;
7913 
7914   PetscFunctionBegin;
7915   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
7916   PetscValidPointer(flg,2);
7917 
7918   if (!A->hermitian_set) {
7919     if (!A->ops->ishermitian) {
7920       MatType mattype;
7921       ierr = MatGetType(A,&mattype);CHKERRQ(ierr);
7922       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for hermitian",mattype);
7923     }
7924     ierr = (*A->ops->ishermitian)(A,tol,flg);CHKERRQ(ierr);
7925     if (!tol) {
7926       A->hermitian_set = PETSC_TRUE;
7927       A->hermitian = *flg;
7928       if (A->hermitian) {
7929 	A->structurally_symmetric_set = PETSC_TRUE;
7930 	A->structurally_symmetric     = PETSC_TRUE;
7931       }
7932     }
7933   } else if (A->hermitian) {
7934     *flg = PETSC_TRUE;
7935   } else if (!tol) {
7936     *flg = PETSC_FALSE;
7937   } else {
7938     if (!A->ops->ishermitian) {
7939       MatType mattype;
7940       ierr = MatGetType(A,&mattype);CHKERRQ(ierr);
7941       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for hermitian",mattype);
7942     }
7943     ierr = (*A->ops->ishermitian)(A,tol,flg);CHKERRQ(ierr);
7944   }
7945   PetscFunctionReturn(0);
7946 }
7947 
7948 #undef __FUNCT__
7949 #define __FUNCT__ "MatIsSymmetricKnown"
7950 /*@
7951    MatIsSymmetricKnown - Checks the flag on the matrix to see if it is symmetric.
7952 
7953    Not Collective
7954 
7955    Input Parameter:
7956 .  A - the matrix to check
7957 
7958    Output Parameters:
7959 +  set - if the symmetric flag is set (this tells you if the next flag is valid)
7960 -  flg - the result
7961 
7962    Level: advanced
7963 
7964    Concepts: matrix^symmetry
7965 
7966    Note: Does not check the matrix values directly, so this may return unknown (set = PETSC_FALSE). Use MatIsSymmetric()
7967          if you want it explicitly checked
7968 
7969 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetric()
7970 @*/
7971 PetscErrorCode  MatIsSymmetricKnown(Mat A,PetscBool  *set,PetscBool  *flg)
7972 {
7973   PetscFunctionBegin;
7974   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
7975   PetscValidPointer(set,2);
7976   PetscValidPointer(flg,3);
7977   if (A->symmetric_set) {
7978     *set = PETSC_TRUE;
7979     *flg = A->symmetric;
7980   } else {
7981     *set = PETSC_FALSE;
7982   }
7983   PetscFunctionReturn(0);
7984 }
7985 
7986 #undef __FUNCT__
7987 #define __FUNCT__ "MatIsHermitianKnown"
7988 /*@
7989    MatIsHermitianKnown - Checks the flag on the matrix to see if it is hermitian.
7990 
7991    Not Collective
7992 
7993    Input Parameter:
7994 .  A - the matrix to check
7995 
7996    Output Parameters:
7997 +  set - if the hermitian flag is set (this tells you if the next flag is valid)
7998 -  flg - the result
7999 
8000    Level: advanced
8001 
8002    Concepts: matrix^symmetry
8003 
8004    Note: Does not check the matrix values directly, so this may return unknown (set = PETSC_FALSE). Use MatIsHermitian()
8005          if you want it explicitly checked
8006 
8007 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetric()
8008 @*/
8009 PetscErrorCode  MatIsHermitianKnown(Mat A,PetscBool  *set,PetscBool  *flg)
8010 {
8011   PetscFunctionBegin;
8012   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8013   PetscValidPointer(set,2);
8014   PetscValidPointer(flg,3);
8015   if (A->hermitian_set) {
8016     *set = PETSC_TRUE;
8017     *flg = A->hermitian;
8018   } else {
8019     *set = PETSC_FALSE;
8020   }
8021   PetscFunctionReturn(0);
8022 }
8023 
8024 #undef __FUNCT__
8025 #define __FUNCT__ "MatIsStructurallySymmetric"
8026 /*@
8027    MatIsStructurallySymmetric - Test whether a matrix is structurally symmetric
8028 
8029    Collective on Mat
8030 
8031    Input Parameter:
8032 .  A - the matrix to test
8033 
8034    Output Parameters:
8035 .  flg - the result
8036 
8037    Level: intermediate
8038 
8039    Concepts: matrix^symmetry
8040 
8041 .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsSymmetric(), MatSetOption()
8042 @*/
8043 PetscErrorCode  MatIsStructurallySymmetric(Mat A,PetscBool  *flg)
8044 {
8045   PetscErrorCode ierr;
8046 
8047   PetscFunctionBegin;
8048   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8049   PetscValidPointer(flg,2);
8050   if (!A->structurally_symmetric_set) {
8051     if (!A->ops->isstructurallysymmetric) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_SUP,"Matrix does not support checking for structural symmetric");
8052     ierr = (*A->ops->isstructurallysymmetric)(A,&A->structurally_symmetric);CHKERRQ(ierr);
8053     A->structurally_symmetric_set = PETSC_TRUE;
8054   }
8055   *flg = A->structurally_symmetric;
8056   PetscFunctionReturn(0);
8057 }
8058 
8059 #undef __FUNCT__
8060 #define __FUNCT__ "MatStashGetInfo"
8061 extern PetscErrorCode MatStashGetInfo_Private(MatStash*,PetscInt*,PetscInt*);
8062 /*@
8063    MatStashGetInfo - Gets how many values are currently in the matrix stash, i.e. need
8064        to be communicated to other processors during the MatAssemblyBegin/End() process
8065 
8066     Not collective
8067 
8068    Input Parameter:
8069 .   vec - the vector
8070 
8071    Output Parameters:
8072 +   nstash   - the size of the stash
8073 .   reallocs - the number of additional mallocs incurred.
8074 .   bnstash   - the size of the block stash
8075 -   breallocs - the number of additional mallocs incurred.in the block stash
8076 
8077    Level: advanced
8078 
8079 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), Mat, MatStashSetInitialSize()
8080 
8081 @*/
8082 PetscErrorCode  MatStashGetInfo(Mat mat,PetscInt *nstash,PetscInt *reallocs,PetscInt *bnstash,PetscInt *breallocs)
8083 {
8084   PetscErrorCode ierr;
8085 
8086   PetscFunctionBegin;
8087   ierr = MatStashGetInfo_Private(&mat->stash,nstash,reallocs);CHKERRQ(ierr);
8088   ierr = MatStashGetInfo_Private(&mat->bstash,bnstash,breallocs);CHKERRQ(ierr);
8089   PetscFunctionReturn(0);
8090 }
8091 
8092 #undef __FUNCT__
8093 #define __FUNCT__ "MatGetVecs"
8094 /*@C
8095    MatGetVecs - Get vector(s) compatible with the matrix, i.e. with the same
8096      parallel layout
8097 
8098    Collective on Mat
8099 
8100    Input Parameter:
8101 .  mat - the matrix
8102 
8103    Output Parameter:
8104 +   right - (optional) vector that the matrix can be multiplied against
8105 -   left - (optional) vector that the matrix vector product can be stored in
8106 
8107   Level: advanced
8108 
8109 .seealso: MatCreate()
8110 @*/
8111 PetscErrorCode  MatGetVecs(Mat mat,Vec *right,Vec *left)
8112 {
8113   PetscErrorCode ierr;
8114 
8115   PetscFunctionBegin;
8116   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
8117   PetscValidType(mat,1);
8118   MatCheckPreallocated(mat,1);
8119   if (mat->ops->getvecs) {
8120     ierr = (*mat->ops->getvecs)(mat,right,left);CHKERRQ(ierr);
8121   } else {
8122     PetscMPIInt size;
8123     ierr = MPI_Comm_size(((PetscObject)mat)->comm, &size);CHKERRQ(ierr);
8124     if (right) {
8125       ierr = VecCreate(((PetscObject)mat)->comm,right);CHKERRQ(ierr);
8126       ierr = VecSetSizes(*right,mat->cmap->n,PETSC_DETERMINE);CHKERRQ(ierr);
8127       ierr = VecSetBlockSize(*right,mat->rmap->bs);CHKERRQ(ierr);
8128       ierr = VecSetType(*right,VECSTANDARD);CHKERRQ(ierr);
8129       ierr = PetscLayoutReference(mat->cmap,&(*right)->map);CHKERRQ(ierr);
8130     }
8131     if (left) {
8132       ierr = VecCreate(((PetscObject)mat)->comm,left);CHKERRQ(ierr);
8133       ierr = VecSetSizes(*left,mat->rmap->n,PETSC_DETERMINE);CHKERRQ(ierr);
8134       ierr = VecSetBlockSize(*left,mat->rmap->bs);CHKERRQ(ierr);
8135       ierr = VecSetType(*left,VECSTANDARD);CHKERRQ(ierr);
8136       ierr = PetscLayoutReference(mat->rmap,&(*left)->map);CHKERRQ(ierr);
8137     }
8138   }
8139   PetscFunctionReturn(0);
8140 }
8141 
8142 #undef __FUNCT__
8143 #define __FUNCT__ "MatFactorInfoInitialize"
8144 /*@C
8145    MatFactorInfoInitialize - Initializes a MatFactorInfo data structure
8146      with default values.
8147 
8148    Not Collective
8149 
8150    Input Parameters:
8151 .    info - the MatFactorInfo data structure
8152 
8153 
8154    Notes: The solvers are generally used through the KSP and PC objects, for example
8155           PCLU, PCILU, PCCHOLESKY, PCICC
8156 
8157    Level: developer
8158 
8159 .seealso: MatFactorInfo
8160 
8161     Developer Note: fortran interface is not autogenerated as the f90
8162     interface defintion cannot be generated correctly [due to MatFactorInfo]
8163 
8164 @*/
8165 
8166 PetscErrorCode  MatFactorInfoInitialize(MatFactorInfo *info)
8167 {
8168   PetscErrorCode ierr;
8169 
8170   PetscFunctionBegin;
8171   ierr = PetscMemzero(info,sizeof(MatFactorInfo));CHKERRQ(ierr);
8172   PetscFunctionReturn(0);
8173 }
8174 
8175 #undef __FUNCT__
8176 #define __FUNCT__ "MatPtAP"
8177 /*@
8178    MatPtAP - Creates the matrix product C = P^T * A * P
8179 
8180    Neighbor-wise Collective on Mat
8181 
8182    Input Parameters:
8183 +  A - the matrix
8184 .  P - the projection matrix
8185 .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
8186 -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(P))
8187 
8188    Output Parameters:
8189 .  C - the product matrix
8190 
8191    Notes:
8192    C will be created and must be destroyed by the user with MatDestroy().
8193 
8194    This routine is currently only implemented for pairs of AIJ matrices and classes
8195    which inherit from AIJ.
8196 
8197    Level: intermediate
8198 
8199 .seealso: MatPtAPSymbolic(), MatPtAPNumeric(), MatMatMult(), MatRARt()
8200 @*/
8201 PetscErrorCode  MatPtAP(Mat A,Mat P,MatReuse scall,PetscReal fill,Mat *C)
8202 {
8203   PetscErrorCode ierr;
8204   PetscErrorCode (*fA)(Mat,Mat,MatReuse,PetscReal,Mat*);
8205   PetscErrorCode (*fP)(Mat,Mat,MatReuse,PetscReal,Mat*);
8206   PetscErrorCode (*ptap)(Mat,Mat,MatReuse,PetscReal,Mat*)=PETSC_NULL;
8207   PetscBool      viatranspose=PETSC_FALSE;
8208 
8209   PetscFunctionBegin;
8210   ierr = PetscOptionsGetBool(((PetscObject)A)->prefix,"-matptap_viatranspose",&viatranspose,PETSC_NULL);CHKERRQ(ierr);
8211   if (viatranspose && scall == MAT_REUSE_MATRIX) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_SUP,"Not supported yet");
8212 
8213   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8214   PetscValidType(A,1);
8215   MatCheckPreallocated(A,1);
8216   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8217   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8218   PetscValidHeaderSpecific(P,MAT_CLASSID,2);
8219   PetscValidType(P,2);
8220   MatCheckPreallocated(P,2);
8221   if (!P->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8222   if (P->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8223 
8224   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);
8225   if (fill < 1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be >= 1.0",fill);
8226 
8227   if (scall == MAT_REUSE_MATRIX) {
8228     PetscValidPointer(*C,5);
8229     PetscValidHeaderSpecific(*C,MAT_CLASSID,5);
8230     ierr = PetscLogEventBegin(MAT_PtAP,A,P,0,0);CHKERRQ(ierr);
8231     ierr = (*(*C)->ops->ptap)(A,P,scall,fill,C);CHKERRQ(ierr);
8232     ierr = PetscLogEventEnd(MAT_PtAP,A,P,0,0);CHKERRQ(ierr);
8233     PetscFunctionReturn(0);
8234   }
8235   if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0;
8236   if (fill < 1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be >= 1.0",fill);
8237 
8238   fA = A->ops->ptap;
8239   fP = P->ops->ptap;
8240   if (fP == fA) {
8241     if (!fA) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"MatPtAP not supported for A of type %s",((PetscObject)A)->type_name);
8242     ptap = fA;
8243   } else {
8244     /* dispatch based on the type of A and P from their PetscObject's PetscFLists. */
8245     char  ptapname[256];
8246     ierr = PetscStrcpy(ptapname,"MatPtAP_");CHKERRQ(ierr);
8247     ierr = PetscStrcat(ptapname,((PetscObject)A)->type_name);CHKERRQ(ierr);
8248     ierr = PetscStrcat(ptapname,"_");CHKERRQ(ierr);
8249     ierr = PetscStrcat(ptapname,((PetscObject)P)->type_name);CHKERRQ(ierr);
8250     ierr = PetscStrcat(ptapname,"_C");CHKERRQ(ierr); /* e.g., ptapname = "MatPtAP_seqdense_seqaij_C" */
8251     ierr = PetscObjectQueryFunction((PetscObject)P,ptapname,(void (**)(void))&ptap);CHKERRQ(ierr);
8252     if (!ptap) {
8253       /* dual dispatch using MatQueryOp */
8254       ierr = MatQueryOp(((PetscObject)A)->comm, (PetscVoidFunction*)(&ptap), "MatPtAP",2,((PetscObject)A)->type_name,((PetscObject)P)->type_name); CHKERRQ(ierr);
8255       if (!ptap) SETERRQ2(((PetscObject)A)->comm,PETSC_ERR_ARG_INCOMP,"MatPtAP requires A, %s, to be compatible with P, %s",((PetscObject)A)->type_name,((PetscObject)P)->type_name);
8256     }
8257   }
8258 
8259   ierr = PetscLogEventBegin(MAT_PtAP,A,P,0,0);CHKERRQ(ierr);
8260   if (viatranspose) {
8261     Mat AP,Pt;
8262     ierr = MatMatMult(A,P,MAT_INITIAL_MATRIX,PETSC_DECIDE,&AP);CHKERRQ(ierr);
8263     ierr = MatTranspose(P,MAT_INITIAL_MATRIX,&Pt);CHKERRQ(ierr);
8264     ierr = MatMatMult(Pt,AP,scall,fill,C);CHKERRQ(ierr);
8265     ierr = MatDestroy(&Pt);CHKERRQ(ierr);
8266     ierr = MatDestroy(&AP);CHKERRQ(ierr);
8267   } else {
8268     ierr = (*ptap)(A,P,scall,fill,C);CHKERRQ(ierr);
8269   }
8270   ierr = PetscLogEventEnd(MAT_PtAP,A,P,0,0);CHKERRQ(ierr);
8271   PetscFunctionReturn(0);
8272 }
8273 
8274 #undef __FUNCT__
8275 #define __FUNCT__ "MatPtAPNumeric"
8276 /*@
8277    MatPtAPNumeric - Computes 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 product matrix
8287 
8288    Notes:
8289    C must have been created by calling MatPtAPSymbolic and must be destroyed by
8290    the user using MatDeatroy().
8291 
8292    This routine is currently only implemented for pairs of AIJ matrices and classes
8293    which inherit from AIJ.  C will be of type MATAIJ.
8294 
8295    Level: intermediate
8296 
8297 .seealso: MatPtAP(), MatPtAPSymbolic(), MatMatMultNumeric()
8298 @*/
8299 PetscErrorCode  MatPtAPNumeric(Mat A,Mat P,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   PetscValidHeaderSpecific(P,MAT_CLASSID,2);
8309   PetscValidType(P,2);
8310   MatCheckPreallocated(P,2);
8311   if (!P->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8312   if (P->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8313   PetscValidHeaderSpecific(C,MAT_CLASSID,3);
8314   PetscValidType(C,3);
8315   MatCheckPreallocated(C,3);
8316   if (C->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8317   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);
8318   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);
8319   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);
8320   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);
8321   MatCheckPreallocated(A,1);
8322 
8323   ierr = PetscLogEventBegin(MAT_PtAPNumeric,A,P,0,0);CHKERRQ(ierr);
8324   ierr = (*A->ops->ptapnumeric)(A,P,C);CHKERRQ(ierr);
8325   ierr = PetscLogEventEnd(MAT_PtAPNumeric,A,P,0,0);CHKERRQ(ierr);
8326   PetscFunctionReturn(0);
8327 }
8328 
8329 #undef __FUNCT__
8330 #define __FUNCT__ "MatPtAPSymbolic"
8331 /*@
8332    MatPtAPSymbolic - Creates the (i,j) structure of the matrix product C = P^T * A * P
8333 
8334    Neighbor-wise Collective on Mat
8335 
8336    Input Parameters:
8337 +  A - the matrix
8338 -  P - the projection matrix
8339 
8340    Output Parameters:
8341 .  C - the (i,j) structure of the product matrix
8342 
8343    Notes:
8344    C will be created and must be destroyed by the user with MatDestroy().
8345 
8346    This routine is currently only implemented for pairs of SeqAIJ matrices and classes
8347    which inherit from SeqAIJ.  C will be of type MATSEQAIJ.  The product is computed using
8348    this (i,j) structure by calling MatPtAPNumeric().
8349 
8350    Level: intermediate
8351 
8352 .seealso: MatPtAP(), MatPtAPNumeric(), MatMatMultSymbolic()
8353 @*/
8354 PetscErrorCode  MatPtAPSymbolic(Mat A,Mat P,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   if (fill <1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be >= 1.0",fill);
8364   PetscValidHeaderSpecific(P,MAT_CLASSID,2);
8365   PetscValidType(P,2);
8366   MatCheckPreallocated(P,2);
8367   if (!P->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8368   if (P->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8369   PetscValidPointer(C,3);
8370 
8371   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);
8372   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);
8373   MatCheckPreallocated(A,1);
8374   ierr = PetscLogEventBegin(MAT_PtAPSymbolic,A,P,0,0);CHKERRQ(ierr);
8375   ierr = (*A->ops->ptapsymbolic)(A,P,fill,C);CHKERRQ(ierr);
8376   ierr = PetscLogEventEnd(MAT_PtAPSymbolic,A,P,0,0);CHKERRQ(ierr);
8377 
8378   /* ierr = MatSetBlockSize(*C,A->rmap->bs);CHKERRQ(ierr); NO! this is not always true -ma */
8379 
8380   PetscFunctionReturn(0);
8381 }
8382 
8383 #undef __FUNCT__
8384 #define __FUNCT__ "MatRARt"
8385 /*@
8386    MatRARt - Creates the matrix product C = R * A * R^T
8387 
8388    Neighbor-wise Collective on Mat
8389 
8390    Input Parameters:
8391 +  A - the matrix
8392 .  R - the projection matrix
8393 .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
8394 -  fill - expected fill as ratio of nnz(C)/nnz(A)
8395 
8396    Output Parameters:
8397 .  C - the product matrix
8398 
8399    Notes:
8400    C will be created and must be destroyed by the user with MatDestroy().
8401 
8402    This routine is currently only implemented for pairs of AIJ matrices and classes
8403    which inherit from AIJ.
8404 
8405    Level: intermediate
8406 
8407 .seealso: MatRARtSymbolic(), MatRARtNumeric(), MatMatMult(), MatPtAP()
8408 @*/
8409 PetscErrorCode  MatRARt(Mat A,Mat R,MatReuse scall,PetscReal fill,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   PetscValidPointer(C,3);
8424   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);
8425   if (fill < 1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be >= 1.0",fill);
8426   MatCheckPreallocated(A,1);
8427 
8428   if (!A->ops->rart) {
8429     MatType mattype;
8430     ierr = MatGetType(A,&mattype);CHKERRQ(ierr);
8431     SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"Matrix of type <%s> does not support RARt",mattype);
8432   }
8433   ierr = PetscLogEventBegin(MAT_RARt,A,R,0,0);CHKERRQ(ierr);
8434   ierr = (*A->ops->rart)(A,R,scall,fill,C);CHKERRQ(ierr);
8435   ierr = PetscLogEventEnd(MAT_RARt,A,R,0,0);CHKERRQ(ierr);
8436   PetscFunctionReturn(0);
8437 }
8438 
8439 #undef __FUNCT__
8440 #define __FUNCT__ "MatRARtNumeric"
8441 /*@
8442    MatRARtNumeric - Computes 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 product matrix
8452 
8453    Notes:
8454    C must have been created by calling MatRARtSymbolic and must be destroyed by
8455    the user using MatDeatroy().
8456 
8457    This routine is currently only implemented for pairs of AIJ matrices and classes
8458    which inherit from AIJ.  C will be of type MATAIJ.
8459 
8460    Level: intermediate
8461 
8462 .seealso: MatRARt(), MatRARtSymbolic(), MatMatMultNumeric()
8463 @*/
8464 PetscErrorCode  MatRARtNumeric(Mat A,Mat R,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   PetscValidHeaderSpecific(R,MAT_CLASSID,2);
8474   PetscValidType(R,2);
8475   MatCheckPreallocated(R,2);
8476   if (!R->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8477   if (R->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8478   PetscValidHeaderSpecific(C,MAT_CLASSID,3);
8479   PetscValidType(C,3);
8480   MatCheckPreallocated(C,3);
8481   if (C->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8482   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);
8483   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);
8484   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);
8485   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);
8486   MatCheckPreallocated(A,1);
8487 
8488   ierr = PetscLogEventBegin(MAT_RARtNumeric,A,R,0,0);CHKERRQ(ierr);
8489   ierr = (*A->ops->rartnumeric)(A,R,C);CHKERRQ(ierr);
8490   ierr = PetscLogEventEnd(MAT_RARtNumeric,A,R,0,0);CHKERRQ(ierr);
8491   PetscFunctionReturn(0);
8492 }
8493 
8494 #undef __FUNCT__
8495 #define __FUNCT__ "MatRARtSymbolic"
8496 /*@
8497    MatRARtSymbolic - Creates the (i,j) structure of the matrix product C = R * A * R^T
8498 
8499    Neighbor-wise Collective on Mat
8500 
8501    Input Parameters:
8502 +  A - the matrix
8503 -  R - the projection matrix
8504 
8505    Output Parameters:
8506 .  C - the (i,j) structure of the product matrix
8507 
8508    Notes:
8509    C will be created and must be destroyed by the user with MatDestroy().
8510 
8511    This routine is currently only implemented for pairs of SeqAIJ matrices and classes
8512    which inherit from SeqAIJ.  C will be of type MATSEQAIJ.  The product is computed using
8513    this (i,j) structure by calling MatRARtNumeric().
8514 
8515    Level: intermediate
8516 
8517 .seealso: MatRARt(), MatRARtNumeric(), MatMatMultSymbolic()
8518 @*/
8519 PetscErrorCode  MatRARtSymbolic(Mat A,Mat R,PetscReal fill,Mat *C)
8520 {
8521   PetscErrorCode ierr;
8522 
8523   PetscFunctionBegin;
8524   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8525   PetscValidType(A,1);
8526   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8527   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8528   if (fill <1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be >= 1.0",fill);
8529   PetscValidHeaderSpecific(R,MAT_CLASSID,2);
8530   PetscValidType(R,2);
8531   MatCheckPreallocated(R,2);
8532   if (!R->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8533   if (R->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8534   PetscValidPointer(C,3);
8535 
8536   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);
8537   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);
8538   MatCheckPreallocated(A,1);
8539   ierr = PetscLogEventBegin(MAT_RARtSymbolic,A,R,0,0);CHKERRQ(ierr);
8540   ierr = (*A->ops->rartsymbolic)(A,R,fill,C);CHKERRQ(ierr);
8541   ierr = PetscLogEventEnd(MAT_RARtSymbolic,A,R,0,0);CHKERRQ(ierr);
8542 
8543   ierr = MatSetBlockSize(*C,A->rmap->bs);CHKERRQ(ierr);
8544   PetscFunctionReturn(0);
8545 }
8546 
8547 extern PetscErrorCode MatQueryOp(MPI_Comm comm, void (**function)(void), const char op[], PetscInt numArgs, ...);
8548 
8549 #undef __FUNCT__
8550 #define __FUNCT__ "MatMatMult"
8551 /*@
8552    MatMatMult - Performs Matrix-Matrix Multiplication C=A*B.
8553 
8554    Neighbor-wise Collective on Mat
8555 
8556    Input Parameters:
8557 +  A - the left matrix
8558 .  B - the right matrix
8559 .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
8560 -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if you do not have a good estimate
8561           if the result is a dense matrix this is irrelevent
8562 
8563    Output Parameters:
8564 .  C - the product matrix
8565 
8566    Notes:
8567    Unless scall is MAT_REUSE_MATRIX C will be created.
8568 
8569    MAT_REUSE_MATRIX can only be used if the matrices A and B have the same nonzero pattern as in the previous call
8570 
8571    To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value
8572    actually needed.
8573 
8574    If you have many matrices with the same non-zero structure to multiply, you
8575    should either
8576 $   1) use MAT_REUSE_MATRIX in all calls but the first or
8577 $   2) call MatMatMultSymbolic() once and then MatMatMultNumeric() for each product needed
8578 
8579    Level: intermediate
8580 
8581 .seealso: MatMatMultSymbolic(), MatMatMultNumeric(), MatTransposeMatMult(),  MatMatTransposeMult(), MatPtAP()
8582 @*/
8583 PetscErrorCode  MatMatMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
8584 {
8585   PetscErrorCode ierr;
8586   PetscErrorCode (*fA)(Mat,Mat,MatReuse,PetscReal,Mat*);
8587   PetscErrorCode (*fB)(Mat,Mat,MatReuse,PetscReal,Mat*);
8588   PetscErrorCode (*mult)(Mat,Mat,MatReuse,PetscReal,Mat *)=PETSC_NULL;
8589 
8590   PetscFunctionBegin;
8591   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8592   PetscValidType(A,1);
8593   MatCheckPreallocated(A,1);
8594   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8595   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8596   PetscValidHeaderSpecific(B,MAT_CLASSID,2);
8597   PetscValidType(B,2);
8598   MatCheckPreallocated(B,2);
8599   if (!B->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8600   if (B->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8601   PetscValidPointer(C,3);
8602   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);
8603   if (scall == MAT_REUSE_MATRIX) {
8604     PetscValidPointer(*C,5);
8605     PetscValidHeaderSpecific(*C,MAT_CLASSID,5);
8606     ierr = PetscLogEventBegin(MAT_MatMult,A,B,0,0);CHKERRQ(ierr);
8607     ierr = (*(*C)->ops->matmult)(A,B,scall,fill,C);CHKERRQ(ierr);
8608     ierr = PetscLogEventEnd(MAT_MatMult,A,B,0,0);CHKERRQ(ierr);
8609     PetscFunctionReturn(0);
8610   }
8611   if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0;
8612   if (fill < 1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be >= 1.0",fill);
8613 
8614   fA = A->ops->matmult;
8615   fB = B->ops->matmult;
8616   if (fB == fA) {
8617     if (!fB) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"MatMatMult not supported for B of type %s",((PetscObject)B)->type_name);
8618     mult = fB;
8619   } else {
8620     /* dispatch based on the type of A and B from their PetscObject's PetscFLists. */
8621     char  multname[256];
8622     ierr = PetscStrcpy(multname,"MatMatMult_");CHKERRQ(ierr);
8623     ierr = PetscStrcat(multname,((PetscObject)A)->type_name);CHKERRQ(ierr);
8624     ierr = PetscStrcat(multname,"_");CHKERRQ(ierr);
8625     ierr = PetscStrcat(multname,((PetscObject)B)->type_name);CHKERRQ(ierr);
8626     ierr = PetscStrcat(multname,"_C");CHKERRQ(ierr); /* e.g., multname = "MatMatMult_seqdense_seqaij_C" */
8627     ierr = PetscObjectQueryFunction((PetscObject)B,multname,(void (**)(void))&mult);CHKERRQ(ierr);
8628     if (!mult) {
8629       /* dual dispatch using MatQueryOp */
8630       ierr = MatQueryOp(((PetscObject)A)->comm, (PetscVoidFunction*)(&mult), "MatMatMult",2,((PetscObject)A)->type_name,((PetscObject)B)->type_name); CHKERRQ(ierr);
8631       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);
8632     }
8633   }
8634   ierr = PetscLogEventBegin(MAT_MatMult,A,B,0,0);CHKERRQ(ierr);
8635   ierr = (*mult)(A,B,scall,fill,C);CHKERRQ(ierr);
8636   ierr = PetscLogEventEnd(MAT_MatMult,A,B,0,0);CHKERRQ(ierr);
8637   PetscFunctionReturn(0);
8638 }
8639 
8640 #undef __FUNCT__
8641 #define __FUNCT__ "MatMatMultSymbolic"
8642 /*@
8643    MatMatMultSymbolic - Performs construction, preallocation, and computes the ij structure
8644    of the matrix-matrix product C=A*B.  Call this routine before calling MatMatMultNumeric().
8645 
8646    Neighbor-wise Collective on Mat
8647 
8648    Input Parameters:
8649 +  A - the left matrix
8650 .  B - the right matrix
8651 -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if you do not have a good estimate,
8652       if C is a dense matrix this is irrelevent
8653 
8654    Output Parameters:
8655 .  C - the product matrix
8656 
8657    Notes:
8658    Unless scall is MAT_REUSE_MATRIX C will be created.
8659 
8660    To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value
8661    actually needed.
8662 
8663    This routine is currently implemented for
8664     - pairs of AIJ matrices and classes which inherit from AIJ, C will be of type AIJ
8665     - pairs of AIJ (A) and Dense (B) matrix, C will be of type Dense.
8666     - pairs of Dense (A) and AIJ (B) matrix, C will be of type Dense.
8667 
8668    Level: intermediate
8669 
8670    Developers Note: There are ways to estimate the number of nonzeros in the resulting product, see for example, http://arxiv.org/abs/1006.4173
8671      We should incorporate them into PETSc.
8672 
8673 .seealso: MatMatMult(), MatMatMultNumeric()
8674 @*/
8675 PetscErrorCode  MatMatMultSymbolic(Mat A,Mat B,PetscReal fill,Mat *C)
8676 {
8677   PetscErrorCode ierr;
8678   PetscErrorCode (*Asymbolic)(Mat,Mat,PetscReal,Mat *);
8679   PetscErrorCode (*Bsymbolic)(Mat,Mat,PetscReal,Mat *);
8680   PetscErrorCode (*symbolic)(Mat,Mat,PetscReal,Mat *)=PETSC_NULL;
8681 
8682   PetscFunctionBegin;
8683   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8684   PetscValidType(A,1);
8685   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8686   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8687 
8688   PetscValidHeaderSpecific(B,MAT_CLASSID,2);
8689   PetscValidType(B,2);
8690   MatCheckPreallocated(B,2);
8691   if (!B->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8692   if (B->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8693   PetscValidPointer(C,3);
8694 
8695   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);
8696   if (fill == PETSC_DEFAULT) fill = 2.0;
8697   if (fill < 1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be > 1.0",fill);
8698   MatCheckPreallocated(A,1);
8699 
8700   Asymbolic = A->ops->matmultsymbolic;
8701   Bsymbolic = B->ops->matmultsymbolic;
8702   if (Asymbolic == Bsymbolic) {
8703     if (!Bsymbolic) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"C=A*B not implemented for B of type %s",((PetscObject)B)->type_name);
8704     symbolic = Bsymbolic;
8705   } else { /* dispatch based on the type of A and B */
8706     char  symbolicname[256];
8707     ierr = PetscStrcpy(symbolicname,"MatMatMultSymbolic_");CHKERRQ(ierr);
8708     ierr = PetscStrcat(symbolicname,((PetscObject)A)->type_name);CHKERRQ(ierr);
8709     ierr = PetscStrcat(symbolicname,"_");CHKERRQ(ierr);
8710     ierr = PetscStrcat(symbolicname,((PetscObject)B)->type_name);CHKERRQ(ierr);
8711     ierr = PetscStrcat(symbolicname,"_C");CHKERRQ(ierr);
8712     ierr = PetscObjectQueryFunction((PetscObject)B,symbolicname,(void (**)(void))&symbolic);CHKERRQ(ierr);
8713     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);
8714   }
8715   ierr = PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr);
8716   ierr = (*symbolic)(A,B,fill,C);CHKERRQ(ierr);
8717   ierr = PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr);
8718   PetscFunctionReturn(0);
8719 }
8720 
8721 #undef __FUNCT__
8722 #define __FUNCT__ "MatMatMultNumeric"
8723 /*@
8724    MatMatMultNumeric - Performs the numeric matrix-matrix product.
8725    Call this routine after first calling MatMatMultSymbolic().
8726 
8727    Neighbor-wise Collective on Mat
8728 
8729    Input Parameters:
8730 +  A - the left matrix
8731 -  B - the right matrix
8732 
8733    Output Parameters:
8734 .  C - the product matrix, which was created by from MatMatMultSymbolic() or a call to MatMatMult().
8735 
8736    Notes:
8737    C must have been created with MatMatMultSymbolic().
8738 
8739    This routine is currently implemented for
8740     - pairs of AIJ matrices and classes which inherit from AIJ, C will be of type MATAIJ.
8741     - pairs of AIJ (A) and Dense (B) matrix, C will be of type Dense.
8742     - pairs of Dense (A) and AIJ (B) matrix, C will be of type Dense.
8743 
8744    Level: intermediate
8745 
8746 .seealso: MatMatMult(), MatMatMultSymbolic()
8747 @*/
8748 PetscErrorCode  MatMatMultNumeric(Mat A,Mat B,Mat C)
8749 {
8750   PetscErrorCode ierr;
8751   PetscErrorCode (*Anumeric)(Mat,Mat,Mat);
8752   PetscErrorCode (*Bnumeric)(Mat,Mat,Mat);
8753   PetscErrorCode (*numeric)(Mat,Mat,Mat)=PETSC_NULL;
8754 
8755   PetscFunctionBegin;
8756   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8757   PetscValidType(A,1);
8758   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8759   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8760 
8761   PetscValidHeaderSpecific(B,MAT_CLASSID,2);
8762   PetscValidType(B,2);
8763   MatCheckPreallocated(B,2);
8764   if (!B->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8765   if (B->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8766 
8767   PetscValidHeaderSpecific(C,MAT_CLASSID,3);
8768   PetscValidType(C,3);
8769   MatCheckPreallocated(C,3);
8770   if (!C->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8771   if (C->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8772 
8773   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);
8774   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);
8775   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);
8776   MatCheckPreallocated(A,1);
8777 
8778   Anumeric = A->ops->matmultnumeric;
8779   Bnumeric = B->ops->matmultnumeric;
8780   if (Anumeric == Bnumeric) {
8781     if (!Bnumeric) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"MatMatMultNumeric not supported for B of type %s",((PetscObject)B)->type_name);
8782     numeric = Bnumeric;
8783   } else {
8784     char  numericname[256];
8785     ierr = PetscStrcpy(numericname,"MatMatMultNumeric_");CHKERRQ(ierr);
8786     ierr = PetscStrcat(numericname,((PetscObject)A)->type_name);CHKERRQ(ierr);
8787     ierr = PetscStrcat(numericname,"_");CHKERRQ(ierr);
8788     ierr = PetscStrcat(numericname,((PetscObject)B)->type_name);CHKERRQ(ierr);
8789     ierr = PetscStrcat(numericname,"_C");CHKERRQ(ierr);
8790     ierr = PetscObjectQueryFunction((PetscObject)B,numericname,(void (**)(void))&numeric);CHKERRQ(ierr);
8791     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);
8792   }
8793   ierr = PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr);
8794   ierr = (*numeric)(A,B,C);CHKERRQ(ierr);
8795   ierr = PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr);
8796   PetscFunctionReturn(0);
8797 }
8798 
8799 #undef __FUNCT__
8800 #define __FUNCT__ "MatMatTransposeMult"
8801 /*@
8802    MatMatTransposeMult - Performs Matrix-Matrix Multiplication C=A*B^T.
8803 
8804    Neighbor-wise Collective on Mat
8805 
8806    Input Parameters:
8807 +  A - the left matrix
8808 .  B - the right matrix
8809 .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
8810 -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if not known
8811 
8812    Output Parameters:
8813 .  C - the product matrix
8814 
8815    Notes:
8816    C will be created if MAT_INITIAL_MATRIX and must be destroyed by the user with MatDestroy().
8817 
8818    MAT_REUSE_MATRIX can only be used if the matrices A and B have the same nonzero pattern as in the previous call
8819 
8820   To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value
8821    actually needed.
8822 
8823    This routine is currently only implemented for pairs of SeqAIJ matrices.  C will be of type MATSEQAIJ.
8824 
8825    Level: intermediate
8826 
8827 .seealso: MatMatTransposeMultSymbolic(), MatMatTransposeMultNumeric(), MatMatMult(), MatTransposeMatMult() MatPtAP()
8828 @*/
8829 PetscErrorCode  MatMatTransposeMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
8830 {
8831   PetscErrorCode ierr;
8832   PetscErrorCode (*fA)(Mat,Mat,MatReuse,PetscReal,Mat*);
8833   PetscErrorCode (*fB)(Mat,Mat,MatReuse,PetscReal,Mat*);
8834 
8835   PetscFunctionBegin;
8836   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8837   PetscValidType(A,1);
8838   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8839   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8840   PetscValidHeaderSpecific(B,MAT_CLASSID,2);
8841   PetscValidType(B,2);
8842   MatCheckPreallocated(B,2);
8843   if (!B->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8844   if (B->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8845   PetscValidPointer(C,3);
8846   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);
8847   if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0;
8848   if (fill < 1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be > 1.0",fill);
8849   MatCheckPreallocated(A,1);
8850 
8851   fA = A->ops->mattransposemult;
8852   if (!fA) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"MatMatTransposeMult not supported for A of type %s",((PetscObject)A)->type_name);
8853   fB = B->ops->mattransposemult;
8854   if (!fB) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"MatMatTransposeMult not supported for B of type %s",((PetscObject)B)->type_name);
8855   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);
8856 
8857   if (scall == MAT_INITIAL_MATRIX) {
8858     ierr = PetscLogEventBegin(MAT_MatTransposeMultSymbolic,A,B,0,0);CHKERRQ(ierr);
8859     ierr = (*A->ops->mattransposemultsymbolic)(A,B,fill,C);CHKERRQ(ierr);
8860     ierr = PetscLogEventEnd(MAT_MatTransposeMultSymbolic,A,B,0,0);CHKERRQ(ierr);
8861   }
8862   ierr = PetscLogEventBegin(MAT_MatTransposeMultNumeric,A,B,0,0);CHKERRQ(ierr);
8863   ierr = (*A->ops->mattransposemultnumeric)(A,B,*C);CHKERRQ(ierr);
8864   ierr = PetscLogEventEnd(MAT_MatTransposeMultNumeric,A,B,0,0);CHKERRQ(ierr);
8865   PetscFunctionReturn(0);
8866 }
8867 
8868 #undef __FUNCT__
8869 #define __FUNCT__ "MatTransposeMatMult"
8870 /*@
8871    MatTransposeMatMult - Performs Matrix-Matrix Multiplication C=A^T*B.
8872 
8873    Neighbor-wise Collective on Mat
8874 
8875    Input Parameters:
8876 +  A - the left matrix
8877 .  B - the right matrix
8878 .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
8879 -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if not known
8880 
8881    Output Parameters:
8882 .  C - the product matrix
8883 
8884    Notes:
8885    C will be created if MAT_INITIAL_MATRIX and must be destroyed by the user with MatDestroy().
8886 
8887    MAT_REUSE_MATRIX can only be used if the matrices A and B have the same nonzero pattern as in the previous call
8888 
8889   To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value
8890    actually needed.
8891 
8892    This routine is currently implemented for pairs of AIJ matrices and pairs of SeqDense matrices and classes
8893    which inherit from SeqAIJ.  C will be of same type as the input matrices.
8894 
8895    Level: intermediate
8896 
8897 .seealso: MatTransposeMatMultSymbolic(), MatTransposeMatMultNumeric(), MatMatMult(), MatMatTransposeMult(), MatPtAP()
8898 @*/
8899 PetscErrorCode  MatTransposeMatMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
8900 {
8901   PetscErrorCode ierr;
8902   PetscErrorCode (*fA)(Mat,Mat,MatReuse,PetscReal,Mat*);
8903   PetscErrorCode (*fB)(Mat,Mat,MatReuse,PetscReal,Mat*);
8904   PetscErrorCode (*transposematmult)(Mat,Mat,MatReuse,PetscReal,Mat*);
8905 
8906   PetscFunctionBegin;
8907   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
8908   PetscValidType(A,1);
8909   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8910   if (A->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8911   PetscValidHeaderSpecific(B,MAT_CLASSID,2);
8912   PetscValidType(B,2);
8913   MatCheckPreallocated(B,2);
8914   if (!B->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8915   if (B->factortype) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8916   PetscValidPointer(C,3);
8917   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);
8918   if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0;
8919   if (fill < 1.0) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Expected fill=%G must be > 1.0",fill);
8920   MatCheckPreallocated(A,1);
8921 
8922   fA = A->ops->transposematmult;
8923   if (!fA) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"MatTransposeMatMult not supported for A of type %s",((PetscObject)A)->type_name);
8924   fB = B->ops->transposematmult;
8925   if (!fB) SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"MatTransposeMatMult not supported for B of type %s",((PetscObject)B)->type_name);
8926   if (fB==fA) {
8927     transposematmult = fA;
8928   } else {
8929     /* dual dispatch using MatQueryOp */
8930     ierr = MatQueryOp(((PetscObject)A)->comm, (PetscVoidFunction*)(&transposematmult), "MatTansposeMatMult",2,((PetscObject)A)->type_name,((PetscObject)B)->type_name); CHKERRQ(ierr);
8931     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);
8932   }
8933   ierr = PetscLogEventBegin(MAT_TransposeMatMult,A,B,0,0);CHKERRQ(ierr);
8934   ierr = (*transposematmult)(A,B,scall,fill,C);CHKERRQ(ierr);
8935   ierr = PetscLogEventEnd(MAT_TransposeMatMult,A,B,0,0);CHKERRQ(ierr);
8936   PetscFunctionReturn(0);
8937 }
8938 
8939 #undef __FUNCT__
8940 #define __FUNCT__ "MatGetRedundantMatrix"
8941 /*@C
8942    MatGetRedundantMatrix - Create redundant matrices and put them into processors of subcommunicators.
8943 
8944    Collective on Mat
8945 
8946    Input Parameters:
8947 +  mat - the matrix
8948 .  nsubcomm - the number of subcommunicators (= number of redundant parallel or sequential matrices)
8949 .  subcomm - MPI communicator split from the communicator where mat resides in
8950 .  mlocal_red - number of local rows of the redundant matrix
8951 -  reuse - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
8952 
8953    Output Parameter:
8954 .  matredundant - redundant matrix
8955 
8956    Notes:
8957    MAT_REUSE_MATRIX can only be used when the nonzero structure of the
8958    original matrix has not changed from that last call to MatGetRedundantMatrix().
8959 
8960    This routine creates the duplicated matrices in subcommunicators; you should NOT create them before
8961    calling it.
8962 
8963    Only MPIAIJ matrix is supported.
8964 
8965    Level: advanced
8966 
8967    Concepts: subcommunicator
8968    Concepts: duplicate matrix
8969 
8970 .seealso: MatDestroy()
8971 @*/
8972 PetscErrorCode  MatGetRedundantMatrix(Mat mat,PetscInt nsubcomm,MPI_Comm subcomm,PetscInt mlocal_red,MatReuse reuse,Mat *matredundant)
8973 {
8974   PetscErrorCode ierr;
8975 
8976   PetscFunctionBegin;
8977   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
8978   if (nsubcomm && reuse == MAT_REUSE_MATRIX) {
8979     PetscValidPointer(*matredundant,6);
8980     PetscValidHeaderSpecific(*matredundant,MAT_CLASSID,6);
8981   }
8982   if (!mat->ops->getredundantmatrix) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
8983   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8984   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8985   MatCheckPreallocated(mat,1);
8986 
8987   ierr = PetscLogEventBegin(MAT_GetRedundantMatrix,mat,0,0,0);CHKERRQ(ierr);
8988   ierr = (*mat->ops->getredundantmatrix)(mat,nsubcomm,subcomm,mlocal_red,reuse,matredundant);CHKERRQ(ierr);
8989   ierr = PetscLogEventEnd(MAT_GetRedundantMatrix,mat,0,0,0);CHKERRQ(ierr);
8990   PetscFunctionReturn(0);
8991 }
8992 
8993 #undef __FUNCT__
8994 #define __FUNCT__ "MatGetMultiProcBlock"
8995 /*@C
8996    MatGetMultiProcBlock - Create multiple [bjacobi] 'parallel submatrices' from
8997    a given 'mat' object. Each submatrix can span multiple procs.
8998 
8999    Collective on Mat
9000 
9001    Input Parameters:
9002 +  mat - the matrix
9003 .  subcomm - the subcommunicator obtained by com_split(comm)
9004 -  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
9005 
9006    Output Parameter:
9007 .  subMat - 'parallel submatrices each spans a given subcomm
9008 
9009   Notes:
9010   The submatrix partition across processors is dicated by 'subComm' a
9011   communicator obtained by com_split(comm). The comm_split
9012   is not restriced to be grouped with consequitive original ranks.
9013 
9014   Due the comm_split() usage, the parallel layout of the submatrices
9015   map directly to the layout of the original matrix [wrt the local
9016   row,col partitioning]. So the original 'DiagonalMat' naturally maps
9017   into the 'DiagonalMat' of the subMat, hence it is used directly from
9018   the subMat. However the offDiagMat looses some columns - and this is
9019   reconstructed with MatSetValues()
9020 
9021   Level: advanced
9022 
9023   Concepts: subcommunicator
9024   Concepts: submatrices
9025 
9026 .seealso: MatGetSubMatrices()
9027 @*/
9028 PetscErrorCode   MatGetMultiProcBlock(Mat mat, MPI_Comm subComm, MatReuse scall,Mat* subMat)
9029 {
9030   PetscErrorCode ierr;
9031   PetscMPIInt    commsize,subCommSize;
9032 
9033   PetscFunctionBegin;
9034   ierr = MPI_Comm_size(((PetscObject)mat)->comm,&commsize);CHKERRQ(ierr);
9035   ierr = MPI_Comm_size(subComm,&subCommSize);CHKERRQ(ierr);
9036   if (subCommSize > commsize) SETERRQ2(((PetscObject)mat)->comm,PETSC_ERR_ARG_OUTOFRANGE,"CommSize %D < SubCommZize %D",commsize,subCommSize);
9037 
9038   ierr = PetscLogEventBegin(MAT_GetMultiProcBlock,mat,0,0,0);CHKERRQ(ierr);
9039   ierr = (*mat->ops->getmultiprocblock)(mat,subComm,scall,subMat);CHKERRQ(ierr);
9040   ierr = PetscLogEventEnd(MAT_GetMultiProcBlock,mat,0,0,0);CHKERRQ(ierr);
9041   PetscFunctionReturn(0);
9042 }
9043 
9044 #undef __FUNCT__
9045 #define __FUNCT__ "MatGetLocalSubMatrix"
9046 /*@
9047    MatGetLocalSubMatrix - Gets a reference to a submatrix specified in local numbering
9048 
9049    Not Collective
9050 
9051    Input Arguments:
9052    mat - matrix to extract local submatrix from
9053    isrow - local row indices for submatrix
9054    iscol - local column indices for submatrix
9055 
9056    Output Arguments:
9057    submat - the submatrix
9058 
9059    Level: intermediate
9060 
9061    Notes:
9062    The submat should be returned with MatRestoreLocalSubMatrix().
9063 
9064    Depending on the format of mat, the returned submat may not implement MatMult().  Its communicator may be
9065    the same as mat, it may be PETSC_COMM_SELF, or some other subcomm of mat's.
9066 
9067    The submat always implements MatSetValuesLocal().  If isrow and iscol have the same block size, then
9068    MatSetValuesBlockedLocal() will also be implemented.
9069 
9070 .seealso: MatRestoreLocalSubMatrix(), MatCreateLocalRef()
9071 @*/
9072 PetscErrorCode  MatGetLocalSubMatrix(Mat mat,IS isrow,IS iscol,Mat *submat)
9073 {
9074   PetscErrorCode ierr;
9075 
9076   PetscFunctionBegin;
9077   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
9078   PetscValidHeaderSpecific(isrow,IS_CLASSID,2);
9079   PetscValidHeaderSpecific(iscol,IS_CLASSID,3);
9080   PetscCheckSameComm(isrow,2,iscol,3);
9081   PetscValidPointer(submat,4);
9082 
9083   if (mat->ops->getlocalsubmatrix) {
9084     ierr = (*mat->ops->getlocalsubmatrix)(mat,isrow,iscol,submat);CHKERRQ(ierr);
9085   } else {
9086     ierr = MatCreateLocalRef(mat,isrow,iscol,submat);CHKERRQ(ierr);
9087   }
9088   PetscFunctionReturn(0);
9089 }
9090 
9091 #undef __FUNCT__
9092 #define __FUNCT__ "MatRestoreLocalSubMatrix"
9093 /*@
9094    MatRestoreLocalSubMatrix - Restores a reference to a submatrix specified in local numbering
9095 
9096    Not Collective
9097 
9098    Input Arguments:
9099    mat - matrix to extract local submatrix from
9100    isrow - local row indices for submatrix
9101    iscol - local column indices for submatrix
9102    submat - the submatrix
9103 
9104    Level: intermediate
9105 
9106 .seealso: MatGetLocalSubMatrix()
9107 @*/
9108 PetscErrorCode  MatRestoreLocalSubMatrix(Mat mat,IS isrow,IS iscol,Mat *submat)
9109 {
9110   PetscErrorCode ierr;
9111 
9112   PetscFunctionBegin;
9113   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
9114   PetscValidHeaderSpecific(isrow,IS_CLASSID,2);
9115   PetscValidHeaderSpecific(iscol,IS_CLASSID,3);
9116   PetscCheckSameComm(isrow,2,iscol,3);
9117   PetscValidPointer(submat,4);
9118   if (*submat) {PetscValidHeaderSpecific(*submat,MAT_CLASSID,4);}
9119 
9120   if (mat->ops->restorelocalsubmatrix) {
9121     ierr = (*mat->ops->restorelocalsubmatrix)(mat,isrow,iscol,submat);CHKERRQ(ierr);
9122   } else {
9123     ierr = MatDestroy(submat);CHKERRQ(ierr);
9124   }
9125   *submat = PETSC_NULL;
9126   PetscFunctionReturn(0);
9127 }
9128 
9129 /* --------------------------------------------------------*/
9130 #undef __FUNCT__
9131 #define __FUNCT__ "MatFindZeroDiagonals"
9132 /*@
9133    MatFindZeroDiagonals - Finds all the rows of a matrix that have zero or no entry in the matrix
9134 
9135    Collective on Mat
9136 
9137    Input Parameter:
9138 .  mat - the matrix
9139 
9140    Output Parameter:
9141 .  is - if any rows have zero diagonals this contains the list of them
9142 
9143    Level: developer
9144 
9145    Concepts: matrix-vector product
9146 
9147 .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd()
9148 @*/
9149 PetscErrorCode  MatFindZeroDiagonals(Mat mat,IS *is)
9150 {
9151   PetscErrorCode ierr;
9152 
9153   PetscFunctionBegin;
9154   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
9155   PetscValidType(mat,1);
9156   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9157   if (mat->factortype) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9158 
9159   if (!mat->ops->findzerodiagonals) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_SUP,"This matrix type does not have a find zero diagonals defined");
9160   ierr = (*mat->ops->findzerodiagonals)(mat,is);CHKERRQ(ierr);
9161   PetscFunctionReturn(0);
9162 }
9163 
9164 #undef __FUNCT__
9165 #define __FUNCT__ "MatInvertBlockDiagonal"
9166 /*@C
9167   MatInvertBlockDiagonal - Inverts the block diagonal entries.
9168 
9169   Collective on Mat
9170 
9171   Input Parameters:
9172 . mat - the matrix
9173 
9174   Output Parameters:
9175 . values - the block inverses in column major order (FORTRAN-like)
9176 
9177    Note:
9178    This routine is not available from Fortran.
9179 
9180   Level: advanced
9181 @*/
9182 PetscErrorCode MatInvertBlockDiagonal(Mat mat,const PetscScalar **values)
9183 {
9184   PetscErrorCode ierr;
9185 
9186   PetscFunctionBegin;
9187   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
9188   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9189   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9190   if (!mat->ops->invertblockdiagonal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported");
9191   ierr = (*mat->ops->invertblockdiagonal)(mat,values);CHKERRQ(ierr);
9192   PetscFunctionReturn(0);
9193 }
9194 
9195 #undef __FUNCT__
9196 #define __FUNCT__ "MatTransposeColoringDestroy"
9197 /*@C
9198     MatTransposeColoringDestroy - Destroys a coloring context for matrix product C=A*B^T that was created
9199     via MatTransposeColoringCreate().
9200 
9201     Collective on MatTransposeColoring
9202 
9203     Input Parameter:
9204 .   c - coloring context
9205 
9206     Level: intermediate
9207 
9208 .seealso: MatTransposeColoringCreate()
9209 @*/
9210 PetscErrorCode  MatTransposeColoringDestroy(MatTransposeColoring *c)
9211 {
9212   PetscErrorCode       ierr;
9213   MatTransposeColoring matcolor=*c;
9214 
9215   PetscFunctionBegin;
9216   if (!matcolor) PetscFunctionReturn(0);
9217   if (--((PetscObject)matcolor)->refct > 0) {matcolor = 0; PetscFunctionReturn(0);}
9218 
9219   ierr = PetscFree(matcolor->ncolumns);CHKERRQ(ierr);
9220   ierr = PetscFree(matcolor->nrows);CHKERRQ(ierr);
9221   ierr = PetscFree(matcolor->colorforrow);CHKERRQ(ierr);
9222   ierr = PetscFree2(matcolor->rows,matcolor->columnsforspidx);CHKERRQ(ierr);
9223   ierr = PetscFree(matcolor->colorforcol);CHKERRQ(ierr);
9224   ierr = PetscFree(matcolor->columns);CHKERRQ(ierr);
9225   ierr = PetscHeaderDestroy(c);CHKERRQ(ierr);
9226   PetscFunctionReturn(0);
9227 }
9228 
9229 #undef __FUNCT__
9230 #define __FUNCT__ "MatTransColoringApplySpToDen"
9231 /*@C
9232     MatTransColoringApplySpToDen - Given a symbolic matrix product C=A*B^T for which
9233     a MatTransposeColoring context has been created, computes a dense B^T by Apply
9234     MatTransposeColoring to sparse B.
9235 
9236     Collective on MatTransposeColoring
9237 
9238     Input Parameters:
9239 +   B - sparse matrix B
9240 .   Btdense - symbolic dense matrix B^T
9241 -   coloring - coloring context created with MatTransposeColoringCreate()
9242 
9243     Output Parameter:
9244 .   Btdense - dense matrix B^T
9245 
9246     Options Database Keys:
9247 +    -mat_transpose_coloring_view - Activates basic viewing or coloring
9248 .    -mat_transpose_coloring_view_draw - Activates drawing of coloring
9249 -    -mat_transpose_coloring_view_info - Activates viewing of coloring info
9250 
9251     Level: intermediate
9252 
9253 .seealso: MatTransposeColoringCreate(), MatTransposeColoringDestroy()
9254 
9255 .keywords: coloring
9256 @*/
9257 PetscErrorCode MatTransColoringApplySpToDen(MatTransposeColoring coloring,Mat B,Mat Btdense)
9258 {
9259   PetscErrorCode ierr;
9260 
9261   PetscFunctionBegin;
9262   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
9263   PetscValidHeaderSpecific(Btdense,MAT_CLASSID,2);
9264   PetscValidHeaderSpecific(coloring,MAT_TRANSPOSECOLORING_CLASSID,3);
9265 
9266   if (!B->ops->transcoloringapplysptoden) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported for this matrix type %s",((PetscObject)B)->type_name);
9267   ierr = (B->ops->transcoloringapplysptoden)(coloring,B,Btdense);CHKERRQ(ierr);
9268   PetscFunctionReturn(0);
9269 }
9270 
9271 #undef __FUNCT__
9272 #define __FUNCT__ "MatTransColoringApplyDenToSp"
9273 /*@C
9274     MatTransColoringApplyDenToSp - Given a symbolic matrix product Csp=A*B^T for which
9275     a MatTransposeColoring context has been created and a dense matrix Cden=A*Btdense
9276     in which Btdens is obtained from MatTransColoringApplySpToDen(), recover sparse matrix
9277     Csp from Cden.
9278 
9279     Collective on MatTransposeColoring
9280 
9281     Input Parameters:
9282 +   coloring - coloring context created with MatTransposeColoringCreate()
9283 -   Cden - matrix product of a sparse matrix and a dense matrix Btdense
9284 
9285     Output Parameter:
9286 .   Csp - sparse matrix
9287 
9288     Options Database Keys:
9289 +    -mat_multtranspose_coloring_view - Activates basic viewing or coloring
9290 .    -mat_multtranspose_coloring_view_draw - Activates drawing of coloring
9291 -    -mat_multtranspose_coloring_view_info - Activates viewing of coloring info
9292 
9293     Level: intermediate
9294 
9295 .seealso: MatTransposeColoringCreate(), MatTransposeColoringDestroy(), MatTransColoringApplySpToDen()
9296 
9297 .keywords: coloring
9298 @*/
9299 PetscErrorCode MatTransColoringApplyDenToSp(MatTransposeColoring matcoloring,Mat Cden,Mat Csp)
9300 {
9301   PetscErrorCode ierr;
9302 
9303   PetscFunctionBegin;
9304   PetscValidHeaderSpecific(matcoloring,MAT_TRANSPOSECOLORING_CLASSID,1);
9305   PetscValidHeaderSpecific(Cden,MAT_CLASSID,2);
9306   PetscValidHeaderSpecific(Csp,MAT_CLASSID,3);
9307 
9308   if (!Csp->ops->transcoloringapplydentosp) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported for this matrix type %s",((PetscObject)Csp)->type_name);
9309   ierr = (Csp->ops->transcoloringapplydentosp)(matcoloring,Cden,Csp);CHKERRQ(ierr);
9310   PetscFunctionReturn(0);
9311 }
9312 
9313 #undef __FUNCT__
9314 #define __FUNCT__ "MatTransposeColoringCreate"
9315 /*@C
9316    MatTransposeColoringCreate - Creates a matrix coloring context for matrix product C=A*B^T.
9317 
9318    Collective on Mat
9319 
9320    Input Parameters:
9321 +  mat - the matrix product C
9322 -  iscoloring - the coloring of the matrix; usually obtained with MatGetColoring() or DMCreateColoring()
9323 
9324     Output Parameter:
9325 .   color - the new coloring context
9326 
9327     Level: intermediate
9328 
9329 .seealso: MatTransposeColoringDestroy(), MatTransposeColoringSetFromOptions(), MatTransColoringApplySpToDen(),
9330            MatTransColoringApplyDenToSp(), MatTransposeColoringView(),
9331 @*/
9332 PetscErrorCode  MatTransposeColoringCreate(Mat mat,ISColoring iscoloring,MatTransposeColoring *color)
9333 {
9334   MatTransposeColoring c;
9335   MPI_Comm             comm;
9336   PetscErrorCode       ierr;
9337 
9338   PetscFunctionBegin;
9339   ierr = PetscLogEventBegin(MAT_TransposeColoringCreate,mat,0,0,0);CHKERRQ(ierr);
9340   ierr = PetscObjectGetComm((PetscObject)mat,&comm);CHKERRQ(ierr);
9341   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);
9342 
9343   c->ctype = iscoloring->ctype;
9344   if (mat->ops->transposecoloringcreate) {
9345     ierr = (*mat->ops->transposecoloringcreate)(mat,iscoloring,c);CHKERRQ(ierr);
9346   } else SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Code not yet written for this matrix type");
9347 
9348   *color = c;
9349   ierr = PetscLogEventEnd(MAT_TransposeColoringCreate,mat,0,0,0);CHKERRQ(ierr);
9350   PetscFunctionReturn(0);
9351 }
9352