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