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