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