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