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